1 | /*
|
---|
2 | * Copyright (c) 2017 Jaroslav Jindrak
|
---|
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"
|
---|
39 | #include "hw_struct/regs.h"
|
---|
40 | #include "scratchpad.h"
|
---|
41 |
|
---|
42 | static inline unsigned xhci_scratchpad_count(xhci_hc_t *hc)
|
---|
43 | {
|
---|
44 | unsigned lo, hi;
|
---|
45 |
|
---|
46 | lo = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_LO);
|
---|
47 | hi = XHCI_REG_RD(hc->cap_regs, XHCI_CAP_MAX_SPBUF_HI);
|
---|
48 |
|
---|
49 | return (hi << 5) | lo;
|
---|
50 | }
|
---|
51 |
|
---|
52 | int xhci_scratchpad_alloc(xhci_hc_t *hc)
|
---|
53 | {
|
---|
54 | const unsigned num_bufs = xhci_scratchpad_count(hc);
|
---|
55 |
|
---|
56 | if (!num_bufs)
|
---|
57 | return EOK;
|
---|
58 |
|
---|
59 | if (dma_buffer_alloc(&hc->scratchpad_array, num_bufs * sizeof(uint64_t)))
|
---|
60 | return ENOMEM;
|
---|
61 | uint64_t *phys_array = hc->scratchpad_array.virt;
|
---|
62 |
|
---|
63 | hc->dcbaa[0] = host2xhci(64, hc->scratchpad_array.phys);
|
---|
64 |
|
---|
65 | hc->scratchpad_buffers = calloc(num_bufs, sizeof(dma_buffer_t));
|
---|
66 | if (!hc->scratchpad_buffers)
|
---|
67 | goto err_dma;
|
---|
68 |
|
---|
69 | for (unsigned i = 0; i < num_bufs; ++i) {
|
---|
70 | dma_buffer_t *cur = &hc->scratchpad_buffers[i];
|
---|
71 | if (dma_buffer_alloc(cur, PAGE_SIZE))
|
---|
72 | goto err_buffers;
|
---|
73 |
|
---|
74 | memset(cur->virt, 0, PAGE_SIZE);
|
---|
75 | phys_array[i] = host2xhci(64, cur->phys);
|
---|
76 | }
|
---|
77 |
|
---|
78 |
|
---|
79 | usb_log_debug2("Allocated %d scratchpad buffers.", num_bufs);
|
---|
80 |
|
---|
81 | return EOK;
|
---|
82 |
|
---|
83 | err_buffers:
|
---|
84 | for (unsigned i = 0; i < num_bufs; ++i)
|
---|
85 | dma_buffer_free(&hc->scratchpad_buffers[i]);
|
---|
86 | free(hc->scratchpad_buffers);
|
---|
87 | err_dma:
|
---|
88 | hc->dcbaa[0] = 0;
|
---|
89 | dma_buffer_free(&hc->scratchpad_array);
|
---|
90 | return ENOMEM;
|
---|
91 | }
|
---|
92 |
|
---|
93 | void xhci_scratchpad_free(xhci_hc_t *hc)
|
---|
94 | {
|
---|
95 | const unsigned num_bufs = xhci_scratchpad_count(hc);
|
---|
96 |
|
---|
97 | if (!num_bufs)
|
---|
98 | return;
|
---|
99 |
|
---|
100 | hc->dcbaa[0] = 0;
|
---|
101 | dma_buffer_free(&hc->scratchpad_array);
|
---|
102 | for (unsigned i = 0; i < num_bufs; ++i)
|
---|
103 | dma_buffer_free(&hc->scratchpad_buffers[i]);
|
---|
104 | free(hc->scratchpad_buffers);
|
---|
105 | return;
|
---|
106 | }
|
---|
107 |
|
---|
108 | /**
|
---|
109 | * @}
|
---|
110 | */
|
---|