[b19131c5] | 1 | /*
|
---|
[e0a5d4c] | 2 | * Copyright (c) 2018 Jaroslav Jindrak, Ondrej Hlavaty
|
---|
[b19131c5] | 3 | * All rights reserved.
|
---|
| 4 | *
|
---|
| 5 | * Redistribution and use in source and binary forms, with or without
|
---|
| 6 | * modification, are permitted provided that the following conditions
|
---|
| 7 | * are met:
|
---|
| 8 | *
|
---|
| 9 | * - Redistributions of source code must retain the above copyright
|
---|
| 10 | * notice, this list of conditions and the following disclaimer.
|
---|
| 11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
| 12 | * notice, this list of conditions and the following disclaimer in the
|
---|
| 13 | * documentation and/or other materials provided with the distribution.
|
---|
| 14 | * - The name of the author may not be used to endorse or promote products
|
---|
| 15 | * derived from this software without specific prior written permission.
|
---|
| 16 | *
|
---|
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
| 27 | */
|
---|
| 28 |
|
---|
| 29 | /** @addtogroup drvusbxhci
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 | /** @file
|
---|
| 33 | * Scratchpad buffer array bookkeeping.
|
---|
| 34 | */
|
---|
| 35 |
|
---|
| 36 | #include <errno.h>
|
---|
| 37 | #include <usb/debug.h>
|
---|
| 38 | #include "hc.h"
|
---|
[83fb72e] | 39 | #include <align.h>
|
---|
[b19131c5] | 40 | #include "hw_struct/regs.h"
|
---|
| 41 | #include "scratchpad.h"
|
---|
| 42 |
|
---|
[eb928c4] | 43 | /**
|
---|
| 44 | * Get the number of scratchpad buffers needed.
|
---|
| 45 | */
|
---|
[b19131c5] | 46 | static inline unsigned xhci_scratchpad_count(xhci_hc_t *hc)
|
---|
| 47 | {
|
---|
| 48 | unsigned lo, hi;
|
---|
| 49 |
|
---|
| 50 | lo = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_LO);
|
---|
| 51 | hi = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_HI);
|
---|
| 52 |
|
---|
| 53 | return (hi << 5) | lo;
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[eb928c4] | 56 | /**
|
---|
| 57 | * Allocate all scratchpad buffers, and configure the xHC.
|
---|
| 58 | */
|
---|
[45457265] | 59 | errno_t xhci_scratchpad_alloc(xhci_hc_t *hc)
|
---|
[b19131c5] | 60 | {
|
---|
[b80c1ab] | 61 | const unsigned num_bufs = xhci_scratchpad_count(hc);
|
---|
[b19131c5] | 62 |
|
---|
| 63 | if (!num_bufs)
|
---|
| 64 | return EOK;
|
---|
| 65 |
|
---|
[83fb72e] | 66 | const unsigned array_size = ALIGN_UP(num_bufs * sizeof(uint64_t), PAGE_SIZE);
|
---|
| 67 | const size_t size = array_size + num_bufs * PAGE_SIZE;
|
---|
[b19131c5] | 68 |
|
---|
[83fb72e] | 69 | if (dma_buffer_alloc(&hc->scratchpad_array, size))
|
---|
| 70 | return ENOMEM;
|
---|
[fd9f4ffe] | 71 |
|
---|
[83fb72e] | 72 | memset(hc->scratchpad_array.virt, 0, size);
|
---|
[fd9f4ffe] | 73 |
|
---|
[1d758fc] | 74 | const char *base = hc->scratchpad_array.virt + array_size;
|
---|
[83fb72e] | 75 | uint64_t *array = hc->scratchpad_array.virt;
|
---|
[b19131c5] | 76 |
|
---|
[1d758fc] | 77 | for (unsigned i = 0; i < num_bufs; ++i) {
|
---|
| 78 | array[i] = host2xhci(64, dma_buffer_phys(&hc->scratchpad_array,
|
---|
[1433ecda] | 79 | base + i * PAGE_SIZE));
|
---|
[1d758fc] | 80 | }
|
---|
[b19131c5] | 81 |
|
---|
[1d758fc] | 82 | hc->dcbaa[0] = host2xhci(64, dma_buffer_phys_base(&hc->scratchpad_array));
|
---|
[fd9f4ffe] | 83 |
|
---|
[defaab2] | 84 | usb_log_debug("Allocated %d scratchpad buffers.", num_bufs);
|
---|
[b19131c5] | 85 |
|
---|
| 86 | return EOK;
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[eb928c4] | 89 | /**
|
---|
| 90 | * Deallocate the scratchpads and deconfigure xHC.
|
---|
| 91 | */
|
---|
[b19131c5] | 92 | void xhci_scratchpad_free(xhci_hc_t *hc)
|
---|
| 93 | {
|
---|
[b80c1ab] | 94 | const unsigned num_bufs = xhci_scratchpad_count(hc);
|
---|
[834d354] | 95 |
|
---|
| 96 | if (!num_bufs)
|
---|
[b19131c5] | 97 | return;
|
---|
| 98 |
|
---|
[73e5b62] | 99 | hc->dcbaa[0] = 0;
|
---|
[b80c1ab] | 100 | dma_buffer_free(&hc->scratchpad_array);
|
---|
[b19131c5] | 101 | return;
|
---|
| 102 | }
|
---|
| 103 |
|
---|
| 104 | /**
|
---|
| 105 | * @}
|
---|
| 106 | */
|
---|