1 | /*
|
---|
2 | * Copyright (c) 2011 Jan Vesely
|
---|
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 drvusbehcihc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief EHCI Host controller driver routines
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <assert.h>
|
---|
37 | #include <async.h>
|
---|
38 | #include <errno.h>
|
---|
39 | #include <macros.h>
|
---|
40 | #include <mem.h>
|
---|
41 | #include <stdlib.h>
|
---|
42 | #include <str_error.h>
|
---|
43 | #include <sys/types.h>
|
---|
44 |
|
---|
45 | #include <usb/debug.h>
|
---|
46 | #include <usb/usb.h>
|
---|
47 |
|
---|
48 | //#include "ehci_endpoint.h"
|
---|
49 | //#include "ehci_batch.h"
|
---|
50 | #include "utils/malloc32.h"
|
---|
51 |
|
---|
52 | #include "hc.h"
|
---|
53 |
|
---|
54 | #define EHCI_USED_INTERRUPTS \
|
---|
55 | (USB_INTR_IRQ_FLAG | USB_INTR_ERR_IRQ_FLAG | USB_INTR_PORT_CHANGE_FLAG)
|
---|
56 |
|
---|
57 | static const irq_pio_range_t ehci_pio_ranges[] = {
|
---|
58 | {
|
---|
59 | .base = 0,
|
---|
60 | .size = sizeof(ehci_regs_t)
|
---|
61 | }
|
---|
62 | };
|
---|
63 |
|
---|
64 | static const irq_cmd_t ehci_irq_commands[] = {
|
---|
65 | {
|
---|
66 | .cmd = CMD_PIO_READ_32,
|
---|
67 | .dstarg = 1,
|
---|
68 | .addr = NULL
|
---|
69 | },
|
---|
70 | {
|
---|
71 | .cmd = CMD_AND,
|
---|
72 | .srcarg = 1,
|
---|
73 | .dstarg = 2,
|
---|
74 | .value = 0
|
---|
75 | },
|
---|
76 | {
|
---|
77 | .cmd = CMD_PREDICATE,
|
---|
78 | .srcarg = 2,
|
---|
79 | .value = 2
|
---|
80 | },
|
---|
81 | {
|
---|
82 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
83 | .srcarg = 1,
|
---|
84 | .addr = NULL
|
---|
85 | },
|
---|
86 | {
|
---|
87 | .cmd = CMD_ACCEPT
|
---|
88 | }
|
---|
89 | };
|
---|
90 |
|
---|
91 | static void hc_gain_control(hc_t *instance);
|
---|
92 | static void hc_start(hc_t *instance);
|
---|
93 | static int hc_init_memory(hc_t *instance);
|
---|
94 | static int interrupt_emulator(hc_t *instance);
|
---|
95 |
|
---|
96 | /** Generate IRQ code.
|
---|
97 | * @param[out] ranges PIO ranges buffer.
|
---|
98 | * @param[in] ranges_size Size of the ranges buffer (bytes).
|
---|
99 | * @param[out] cmds Commands buffer.
|
---|
100 | * @param[in] cmds_size Size of the commands buffer (bytes).
|
---|
101 | * @param[in] regs Device's register range.
|
---|
102 | *
|
---|
103 | * @return Error code.
|
---|
104 | */
|
---|
105 | int hc_gen_irq_code(irq_code_t *code, addr_range_t *regs)
|
---|
106 | {
|
---|
107 | assert(code);
|
---|
108 | if (RNGSZ(*regs) < sizeof(ehci_regs_t))
|
---|
109 | return EOVERFLOW;
|
---|
110 |
|
---|
111 | code->ranges = malloc(sizeof(ehci_pio_ranges));
|
---|
112 | if (code->ranges == NULL)
|
---|
113 | return ENOMEM;
|
---|
114 |
|
---|
115 | code->cmds = malloc(sizeof(ehci_irq_commands));
|
---|
116 | if (code->cmds == NULL) {
|
---|
117 | free(code->ranges);
|
---|
118 | return ENOMEM;
|
---|
119 | }
|
---|
120 |
|
---|
121 | code->rangecount = ARRAY_SIZE(ehci_pio_ranges);
|
---|
122 | code->cmdcount = ARRAY_SIZE(ehci_irq_commands);
|
---|
123 |
|
---|
124 | memcpy(code->ranges, ehci_pio_ranges, sizeof(ehci_pio_ranges));
|
---|
125 | code->ranges[0].base = RNGABS(*regs);
|
---|
126 |
|
---|
127 | memcpy(code->cmds, ehci_irq_commands, sizeof(ehci_irq_commands));
|
---|
128 | ehci_caps_regs_t *caps = NULL;
|
---|
129 | int ret = pio_enable_range(regs, (void**)&caps);
|
---|
130 | if (ret != EOK) {
|
---|
131 | return ret;
|
---|
132 | }
|
---|
133 | ehci_regs_t *registers =
|
---|
134 | (ehci_regs_t *)(RNGABSPTR(*regs) + EHCI_RD8(caps->caplength));
|
---|
135 | code->cmds[0].addr = (void *) ®isters->usbsts;
|
---|
136 | code->cmds[3].addr = (void *) ®isters->usbsts;
|
---|
137 | EHCI_WR(code->cmds[1].value, EHCI_USED_INTERRUPTS);
|
---|
138 |
|
---|
139 | return EOK;
|
---|
140 | }
|
---|
141 |
|
---|
142 | /** Initialize EHCI hc driver structure
|
---|
143 | *
|
---|
144 | * @param[in] instance Memory place for the structure.
|
---|
145 | * @param[in] regs Device's I/O registers range.
|
---|
146 | * @param[in] interrupts True if w interrupts should be used
|
---|
147 | * @return Error code
|
---|
148 | */
|
---|
149 | int hc_init(hc_t *instance, addr_range_t *regs, bool interrupts)
|
---|
150 | {
|
---|
151 | assert(instance);
|
---|
152 |
|
---|
153 | int ret = pio_enable_range(regs, (void **) &instance->caps);
|
---|
154 | if (ret != EOK) {
|
---|
155 | usb_log_error("Failed to gain access to device registers: %s.\n",
|
---|
156 | str_error(ret));
|
---|
157 | return ret;
|
---|
158 | }
|
---|
159 | instance->registers =
|
---|
160 | (void*)instance->caps + EHCI_RD8(instance->caps->caplength);
|
---|
161 |
|
---|
162 | list_initialize(&instance->pending_batches);
|
---|
163 | fibril_mutex_initialize(&instance->guard);
|
---|
164 |
|
---|
165 | ret = hc_init_memory(instance);
|
---|
166 | if (ret != EOK) {
|
---|
167 | usb_log_error("Failed to create EHCI memory structures: %s.\n",
|
---|
168 | str_error(ret));
|
---|
169 | return ret;
|
---|
170 | }
|
---|
171 |
|
---|
172 | hc_gain_control(instance);
|
---|
173 |
|
---|
174 | if (!interrupts) {
|
---|
175 | instance->interrupt_emulator =
|
---|
176 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
177 | fibril_add_ready(instance->interrupt_emulator);
|
---|
178 | }
|
---|
179 |
|
---|
180 | ehci_rh_init(
|
---|
181 | &instance->rh, instance->caps, instance->registers, "ehci rh");
|
---|
182 | hc_start(instance);
|
---|
183 |
|
---|
184 | return EOK;
|
---|
185 | }
|
---|
186 |
|
---|
187 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
188 | {
|
---|
189 | }
|
---|
190 |
|
---|
191 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
192 | {
|
---|
193 | }
|
---|
194 |
|
---|
195 | /** Add USB transfer to the schedule.
|
---|
196 | *
|
---|
197 | * @param[in] instance EHCI hc driver structure.
|
---|
198 | * @param[in] batch Batch representing the transfer.
|
---|
199 | * @return Error code.
|
---|
200 | */
|
---|
201 | int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
---|
202 | {
|
---|
203 | assert(hcd);
|
---|
204 | hc_t *instance = hcd->driver.data;
|
---|
205 | assert(instance);
|
---|
206 |
|
---|
207 | /* Check for root hub communication */
|
---|
208 | if (batch->ep->address == ehci_rh_get_address(&instance->rh)) {
|
---|
209 | usb_log_debug("EHCI root hub request.\n");
|
---|
210 | return ehci_rh_schedule(&instance->rh, batch);
|
---|
211 | }
|
---|
212 | return ENOTSUP;
|
---|
213 | }
|
---|
214 |
|
---|
215 | /** Interrupt handling routine
|
---|
216 | *
|
---|
217 | * @param[in] instance EHCI hc driver structure.
|
---|
218 | * @param[in] status Value of the status register at the time of interrupt.
|
---|
219 | */
|
---|
220 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
221 | {
|
---|
222 | status = EHCI_RD(status);
|
---|
223 | assert(instance);
|
---|
224 | if (status & USB_STS_PORT_CHANGE_FLAG) {
|
---|
225 | ehci_rh_interrupt(&instance->rh);
|
---|
226 | }
|
---|
227 | }
|
---|
228 |
|
---|
229 | /** Check status register regularly
|
---|
230 | *
|
---|
231 | * @param[in] instance EHCI hc driver structure.
|
---|
232 | * @return Error code
|
---|
233 | */
|
---|
234 | int interrupt_emulator(hc_t *instance)
|
---|
235 | {
|
---|
236 | assert(instance);
|
---|
237 | usb_log_info("Started interrupt emulator.\n");
|
---|
238 | while (1) {
|
---|
239 | // const uint32_t status = instance->registers->interrupt_status;
|
---|
240 | // instance->registers->interrupt_status = status;
|
---|
241 | // hc_interrupt(instance, status);
|
---|
242 | async_usleep(10000);
|
---|
243 | }
|
---|
244 | return EOK;
|
---|
245 | }
|
---|
246 |
|
---|
247 | /** Turn off any (BIOS)driver that might be in control of the device.
|
---|
248 | *
|
---|
249 | * This function implements routines described in chapter 5.1.1.3 of the EHCI
|
---|
250 | * specification (page 40, pdf page 54).
|
---|
251 | *
|
---|
252 | * @param[in] instance EHCI hc driver structure.
|
---|
253 | */
|
---|
254 | void hc_gain_control(hc_t *instance)
|
---|
255 | {
|
---|
256 | assert(instance);
|
---|
257 | }
|
---|
258 |
|
---|
259 | /** EHCI hw initialization routine.
|
---|
260 | *
|
---|
261 | * @param[in] instance EHCI hc driver structure.
|
---|
262 | */
|
---|
263 | void hc_start(hc_t *instance)
|
---|
264 | {
|
---|
265 | }
|
---|
266 |
|
---|
267 | /** Initialize memory structures used by the EHCI hcd.
|
---|
268 | *
|
---|
269 | * @param[in] instance EHCI hc driver structure.
|
---|
270 | * @return Error code.
|
---|
271 | */
|
---|
272 | int hc_init_memory(hc_t *instance)
|
---|
273 | {
|
---|
274 | return EOK;
|
---|
275 | }
|
---|
276 |
|
---|
277 | /**
|
---|
278 | * @}
|
---|
279 | */
|
---|