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 drvusbohcihc
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief OHCI Host controller driver routines
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <adt/list.h>
|
---|
39 | #include <libarch/ddi.h>
|
---|
40 |
|
---|
41 | #include <usb/debug.h>
|
---|
42 | #include <usb/usb.h>
|
---|
43 |
|
---|
44 | #include "hc.h"
|
---|
45 | #include "ohci_endpoint.h"
|
---|
46 |
|
---|
47 | #define OHCI_USED_INTERRUPTS \
|
---|
48 | (I_SO | I_WDH | I_UE | I_RHSC)
|
---|
49 |
|
---|
50 | static const irq_pio_range_t ohci_pio_ranges[] = {
|
---|
51 | {
|
---|
52 | .base = 0,
|
---|
53 | .size = sizeof(ohci_regs_t)
|
---|
54 | }
|
---|
55 | };
|
---|
56 |
|
---|
57 | static const irq_cmd_t ohci_irq_commands[] = {
|
---|
58 | {
|
---|
59 | .cmd = CMD_PIO_READ_32,
|
---|
60 | .dstarg = 1,
|
---|
61 | .addr = NULL
|
---|
62 | },
|
---|
63 | {
|
---|
64 | .cmd = CMD_AND,
|
---|
65 | .srcarg = 1,
|
---|
66 | .dstarg = 2,
|
---|
67 | .value = 0
|
---|
68 | },
|
---|
69 | {
|
---|
70 | .cmd = CMD_PREDICATE,
|
---|
71 | .srcarg = 2,
|
---|
72 | .value = 2
|
---|
73 | },
|
---|
74 | {
|
---|
75 | .cmd = CMD_PIO_WRITE_A_32,
|
---|
76 | .srcarg = 1,
|
---|
77 | .addr = NULL
|
---|
78 | },
|
---|
79 | {
|
---|
80 | .cmd = CMD_ACCEPT
|
---|
81 | }
|
---|
82 | };
|
---|
83 |
|
---|
84 | static void hc_gain_control(hc_t *instance);
|
---|
85 | static void hc_start(hc_t *instance);
|
---|
86 | static int hc_init_transfer_lists(hc_t *instance);
|
---|
87 | static int hc_init_memory(hc_t *instance);
|
---|
88 | static int interrupt_emulator(hc_t *instance);
|
---|
89 |
|
---|
90 | /** Get number of PIO ranges used in IRQ code.
|
---|
91 | * @return Number of ranges.
|
---|
92 | */
|
---|
93 | size_t hc_irq_pio_range_count(void)
|
---|
94 | {
|
---|
95 | return sizeof(ohci_pio_ranges) / sizeof(irq_pio_range_t);
|
---|
96 | }
|
---|
97 |
|
---|
98 | /** Get number of commands used in IRQ code.
|
---|
99 | * @return Number of commands.
|
---|
100 | */
|
---|
101 | size_t hc_irq_cmd_count(void)
|
---|
102 | {
|
---|
103 | return sizeof(ohci_irq_commands) / sizeof(irq_cmd_t);
|
---|
104 | }
|
---|
105 |
|
---|
106 | /** Generate IRQ code.
|
---|
107 | * @param[out] ranges PIO ranges buffer.
|
---|
108 | * @param[in] ranges_size Size of the ranges buffer (bytes).
|
---|
109 | * @param[out] cmds Commands buffer.
|
---|
110 | * @param[in] cmds_size Size of the commands buffer (bytes).
|
---|
111 | * @param[in] regs Physical address of device's registers.
|
---|
112 | * @param[in] reg_size Size of the register area (bytes).
|
---|
113 | *
|
---|
114 | * @return Error code.
|
---|
115 | */
|
---|
116 | int
|
---|
117 | hc_get_irq_code(irq_pio_range_t ranges[], size_t ranges_size, irq_cmd_t cmds[],
|
---|
118 | size_t cmds_size, uintptr_t regs, size_t reg_size)
|
---|
119 | {
|
---|
120 | if ((ranges_size < sizeof(ohci_pio_ranges)) ||
|
---|
121 | (cmds_size < sizeof(ohci_irq_commands)) ||
|
---|
122 | (reg_size < sizeof(ohci_regs_t)))
|
---|
123 | return EOVERFLOW;
|
---|
124 |
|
---|
125 | memcpy(ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
|
---|
126 | ranges[0].base = regs;
|
---|
127 |
|
---|
128 | memcpy(cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
|
---|
129 | ohci_regs_t *registers = (ohci_regs_t *) regs;
|
---|
130 | cmds[0].addr = (void *) ®isters->interrupt_status;
|
---|
131 | cmds[3].addr = (void *) ®isters->interrupt_status;
|
---|
132 | OHCI_WR(cmds[1].value, OHCI_USED_INTERRUPTS);
|
---|
133 |
|
---|
134 | return EOK;
|
---|
135 | }
|
---|
136 |
|
---|
137 | /** Initialize OHCI hc driver structure
|
---|
138 | *
|
---|
139 | * @param[in] instance Memory place for the structure.
|
---|
140 | * @param[in] regs Address of the memory mapped I/O registers.
|
---|
141 | * @param[in] reg_size Size of the memory mapped area.
|
---|
142 | * @param[in] interrupts True if w interrupts should be used
|
---|
143 | * @return Error code
|
---|
144 | */
|
---|
145 | int hc_init(hc_t *instance, uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
146 | {
|
---|
147 | assert(instance);
|
---|
148 |
|
---|
149 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
150 | if (ret != EOK) { \
|
---|
151 | usb_log_error(message); \
|
---|
152 | return ret; \
|
---|
153 | } else (void)0
|
---|
154 |
|
---|
155 | int ret =
|
---|
156 | pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
157 | CHECK_RET_RETURN(ret,
|
---|
158 | "Failed to gain access to device registers: %s.\n", str_error(ret));
|
---|
159 |
|
---|
160 | list_initialize(&instance->pending_batches);
|
---|
161 |
|
---|
162 | ret = hc_init_memory(instance);
|
---|
163 | CHECK_RET_RETURN(ret, "Failed to create OHCI memory structures: %s.\n",
|
---|
164 | str_error(ret));
|
---|
165 | #undef CHECK_RET_RETURN
|
---|
166 |
|
---|
167 | fibril_mutex_initialize(&instance->guard);
|
---|
168 |
|
---|
169 | hc_gain_control(instance);
|
---|
170 |
|
---|
171 | if (!interrupts) {
|
---|
172 | instance->interrupt_emulator =
|
---|
173 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
174 | fibril_add_ready(instance->interrupt_emulator);
|
---|
175 | }
|
---|
176 |
|
---|
177 | rh_init(&instance->rh, instance->registers);
|
---|
178 | hc_start(instance);
|
---|
179 |
|
---|
180 | return EOK;
|
---|
181 | }
|
---|
182 |
|
---|
183 | void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
184 | {
|
---|
185 | assert(instance);
|
---|
186 | assert(ep);
|
---|
187 |
|
---|
188 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
---|
189 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
190 | assert(list);
|
---|
191 | assert(ohci_ep);
|
---|
192 |
|
---|
193 | /* Enqueue ep */
|
---|
194 | switch (ep->transfer_type) {
|
---|
195 | case USB_TRANSFER_CONTROL:
|
---|
196 | OHCI_CLR(instance->registers->control, C_CLE);
|
---|
197 | endpoint_list_add_ep(list, ohci_ep);
|
---|
198 | OHCI_WR(instance->registers->control_current, 0);
|
---|
199 | OHCI_SET(instance->registers->control, C_CLE);
|
---|
200 | break;
|
---|
201 | case USB_TRANSFER_BULK:
|
---|
202 | OHCI_CLR(instance->registers->control, C_BLE);
|
---|
203 | endpoint_list_add_ep(list, ohci_ep);
|
---|
204 | OHCI_WR(instance->registers->bulk_current, 0);
|
---|
205 | OHCI_SET(instance->registers->control, C_BLE);
|
---|
206 | break;
|
---|
207 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
208 | case USB_TRANSFER_INTERRUPT:
|
---|
209 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
---|
210 | endpoint_list_add_ep(list, ohci_ep);
|
---|
211 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
---|
212 | break;
|
---|
213 | }
|
---|
214 | }
|
---|
215 |
|
---|
216 | void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
|
---|
217 | {
|
---|
218 | assert(instance);
|
---|
219 | assert(ep);
|
---|
220 |
|
---|
221 | /* Dequeue ep */
|
---|
222 | endpoint_list_t *list = &instance->lists[ep->transfer_type];
|
---|
223 | ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
|
---|
224 |
|
---|
225 | assert(list);
|
---|
226 | assert(ohci_ep);
|
---|
227 | switch (ep->transfer_type) {
|
---|
228 | case USB_TRANSFER_CONTROL:
|
---|
229 | OHCI_CLR(instance->registers->control, C_CLE);
|
---|
230 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
231 | OHCI_WR(instance->registers->control_current, 0);
|
---|
232 | OHCI_SET(instance->registers->control, C_CLE);
|
---|
233 | break;
|
---|
234 | case USB_TRANSFER_BULK:
|
---|
235 | OHCI_CLR(instance->registers->control, C_BLE);
|
---|
236 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
237 | OHCI_WR(instance->registers->bulk_current, 0);
|
---|
238 | OHCI_SET(instance->registers->control, C_BLE);
|
---|
239 | break;
|
---|
240 | case USB_TRANSFER_ISOCHRONOUS:
|
---|
241 | case USB_TRANSFER_INTERRUPT:
|
---|
242 | OHCI_CLR(instance->registers->control, C_PLE | C_IE);
|
---|
243 | endpoint_list_remove_ep(list, ohci_ep);
|
---|
244 | OHCI_SET(instance->registers->control, C_PLE | C_IE);
|
---|
245 | break;
|
---|
246 | default:
|
---|
247 | break;
|
---|
248 | }
|
---|
249 | }
|
---|
250 |
|
---|
251 | /** Add USB transfer to the schedule.
|
---|
252 | *
|
---|
253 | * @param[in] instance OHCI hc driver structure.
|
---|
254 | * @param[in] batch Batch representing the transfer.
|
---|
255 | * @return Error code.
|
---|
256 | */
|
---|
257 | int hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
|
---|
258 | {
|
---|
259 | assert(hcd);
|
---|
260 | hc_t *instance = hcd->private_data;
|
---|
261 | assert(instance);
|
---|
262 |
|
---|
263 | /* Check for root hub communication */
|
---|
264 | if (batch->ep->address == instance->rh.address) {
|
---|
265 | usb_log_debug("OHCI root hub request.\n");
|
---|
266 | rh_request(&instance->rh, batch);
|
---|
267 | return EOK;
|
---|
268 | }
|
---|
269 | ohci_transfer_batch_t *ohci_batch = ohci_transfer_batch_get(batch);
|
---|
270 | if (!ohci_batch)
|
---|
271 | return ENOMEM;
|
---|
272 |
|
---|
273 | fibril_mutex_lock(&instance->guard);
|
---|
274 | list_append(&ohci_batch->link, &instance->pending_batches);
|
---|
275 | ohci_transfer_batch_commit(ohci_batch);
|
---|
276 |
|
---|
277 | /* Control and bulk schedules need a kick to start working */
|
---|
278 | switch (batch->ep->transfer_type)
|
---|
279 | {
|
---|
280 | case USB_TRANSFER_CONTROL:
|
---|
281 | OHCI_SET(instance->registers->command_status, CS_CLF);
|
---|
282 | break;
|
---|
283 | case USB_TRANSFER_BULK:
|
---|
284 | OHCI_SET(instance->registers->command_status, CS_BLF);
|
---|
285 | break;
|
---|
286 | default:
|
---|
287 | break;
|
---|
288 | }
|
---|
289 | fibril_mutex_unlock(&instance->guard);
|
---|
290 | return EOK;
|
---|
291 | }
|
---|
292 |
|
---|
293 | /** Interrupt handling routine
|
---|
294 | *
|
---|
295 | * @param[in] instance OHCI hc driver structure.
|
---|
296 | * @param[in] status Value of the status register at the time of interrupt.
|
---|
297 | */
|
---|
298 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
299 | {
|
---|
300 | status = OHCI_RD(status);
|
---|
301 | assert(instance);
|
---|
302 | if ((status & ~I_SF) == 0) /* ignore sof status */
|
---|
303 | return;
|
---|
304 | usb_log_debug2("OHCI(%p) interrupt: %x.\n", instance, status);
|
---|
305 | if (status & I_RHSC)
|
---|
306 | rh_interrupt(&instance->rh);
|
---|
307 |
|
---|
308 | if (status & I_WDH) {
|
---|
309 | fibril_mutex_lock(&instance->guard);
|
---|
310 | usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).\n", instance->hcca,
|
---|
311 | OHCI_RD(instance->registers->hcca),
|
---|
312 | (void *) addr_to_phys(instance->hcca));
|
---|
313 | usb_log_debug2("Periodic current: %#" PRIx32 ".\n",
|
---|
314 | OHCI_RD(instance->registers->periodic_current));
|
---|
315 |
|
---|
316 | link_t *current = list_first(&instance->pending_batches);
|
---|
317 | while (current && current != &instance->pending_batches.head) {
|
---|
318 | link_t *next = current->next;
|
---|
319 | ohci_transfer_batch_t *batch =
|
---|
320 | ohci_transfer_batch_from_link(current);
|
---|
321 |
|
---|
322 | if (ohci_transfer_batch_is_complete(batch)) {
|
---|
323 | list_remove(current);
|
---|
324 | ohci_transfer_batch_finish_dispose(batch);
|
---|
325 | }
|
---|
326 |
|
---|
327 | current = next;
|
---|
328 | }
|
---|
329 | fibril_mutex_unlock(&instance->guard);
|
---|
330 | }
|
---|
331 |
|
---|
332 | if (status & I_UE) {
|
---|
333 | usb_log_fatal("Error like no other!\n");
|
---|
334 | hc_start(instance);
|
---|
335 | }
|
---|
336 |
|
---|
337 | }
|
---|
338 |
|
---|
339 | /** Check status register regularly
|
---|
340 | *
|
---|
341 | * @param[in] instance OHCI hc driver structure.
|
---|
342 | * @return Error code
|
---|
343 | */
|
---|
344 | int interrupt_emulator(hc_t *instance)
|
---|
345 | {
|
---|
346 | assert(instance);
|
---|
347 | usb_log_info("Started interrupt emulator.\n");
|
---|
348 | while (1) {
|
---|
349 | const uint32_t status = instance->registers->interrupt_status;
|
---|
350 | instance->registers->interrupt_status = status;
|
---|
351 | hc_interrupt(instance, status);
|
---|
352 | async_usleep(10000);
|
---|
353 | }
|
---|
354 | return EOK;
|
---|
355 | }
|
---|
356 |
|
---|
357 | /** Turn off any (BIOS)driver that might be in control of the device.
|
---|
358 | *
|
---|
359 | * This function implements routines described in chapter 5.1.1.3 of the OHCI
|
---|
360 | * specification (page 40, pdf page 54).
|
---|
361 | *
|
---|
362 | * @param[in] instance OHCI hc driver structure.
|
---|
363 | */
|
---|
364 | void hc_gain_control(hc_t *instance)
|
---|
365 | {
|
---|
366 | assert(instance);
|
---|
367 |
|
---|
368 | usb_log_debug("Requesting OHCI control.\n");
|
---|
369 | if (OHCI_RD(instance->registers->revision) & R_LEGACY_FLAG) {
|
---|
370 | /* Turn off legacy emulation, it should be enough to zero
|
---|
371 | * the lowest bit, but it caused problems. Thus clear all
|
---|
372 | * except GateA20 (causes restart on some hw).
|
---|
373 | * See page 145 of the specs for details.
|
---|
374 | */
|
---|
375 | volatile uint32_t *ohci_emulation_reg =
|
---|
376 | (uint32_t*)((char*)instance->registers + LEGACY_REGS_OFFSET);
|
---|
377 | usb_log_debug("OHCI legacy register %p: %x.\n",
|
---|
378 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
---|
379 | /* Zero everything but A20State */
|
---|
380 | OHCI_CLR(*ohci_emulation_reg, ~0x100);
|
---|
381 | usb_log_debug(
|
---|
382 | "OHCI legacy register (should be 0 or 0x100) %p: %x.\n",
|
---|
383 | ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* Interrupt routing enabled => smm driver is active */
|
---|
387 | if (OHCI_RD(instance->registers->control) & C_IR) {
|
---|
388 | usb_log_debug("SMM driver: request ownership change.\n");
|
---|
389 | OHCI_SET(instance->registers->command_status, CS_OCR);
|
---|
390 | /* Hope that SMM actually knows its stuff or we can hang here */
|
---|
391 | while (OHCI_RD(instance->registers->control & C_IR)) {
|
---|
392 | async_usleep(1000);
|
---|
393 | }
|
---|
394 | usb_log_info("SMM driver: Ownership taken.\n");
|
---|
395 | C_HCFS_SET(instance->registers->control, C_HCFS_RESET);
|
---|
396 | async_usleep(50000);
|
---|
397 | return;
|
---|
398 | }
|
---|
399 |
|
---|
400 | const unsigned hc_status = C_HCFS_GET(instance->registers->control);
|
---|
401 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
402 | if (hc_status != C_HCFS_RESET) {
|
---|
403 | usb_log_debug("BIOS driver found.\n");
|
---|
404 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
405 | usb_log_info("BIOS driver: HC operational.\n");
|
---|
406 | return;
|
---|
407 | }
|
---|
408 | /* HC is suspended assert resume for 20ms */
|
---|
409 | C_HCFS_SET(instance->registers->control, C_HCFS_RESUME);
|
---|
410 | async_usleep(20000);
|
---|
411 | usb_log_info("BIOS driver: HC resumed.\n");
|
---|
412 | return;
|
---|
413 | }
|
---|
414 |
|
---|
415 | /* HC is in reset (hw startup) => no other driver
|
---|
416 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
417 | usb_log_debug("Host controller found in reset state.\n");
|
---|
418 | async_usleep(50000);
|
---|
419 | }
|
---|
420 |
|
---|
421 | /** OHCI hw initialization routine.
|
---|
422 | *
|
---|
423 | * @param[in] instance OHCI hc driver structure.
|
---|
424 | */
|
---|
425 | void hc_start(hc_t *instance)
|
---|
426 | {
|
---|
427 | /* OHCI guide page 42 */
|
---|
428 | assert(instance);
|
---|
429 | usb_log_debug2("Started hc initialization routine.\n");
|
---|
430 |
|
---|
431 | /* Save contents of fm_interval register */
|
---|
432 | const uint32_t fm_interval = OHCI_RD(instance->registers->fm_interval);
|
---|
433 | usb_log_debug2("Old value of HcFmInterval: %x.\n", fm_interval);
|
---|
434 |
|
---|
435 | /* Reset hc */
|
---|
436 | usb_log_debug2("HC reset.\n");
|
---|
437 | size_t time = 0;
|
---|
438 | OHCI_WR(instance->registers->command_status, CS_HCR);
|
---|
439 | while (OHCI_RD(instance->registers->command_status) & CS_HCR) {
|
---|
440 | async_usleep(10);
|
---|
441 | time += 10;
|
---|
442 | }
|
---|
443 | usb_log_debug2("HC reset complete in %zu us.\n", time);
|
---|
444 |
|
---|
445 | /* Restore fm_interval */
|
---|
446 | OHCI_WR(instance->registers->fm_interval, fm_interval);
|
---|
447 | assert((OHCI_RD(instance->registers->command_status) & CS_HCR) == 0);
|
---|
448 |
|
---|
449 | /* hc is now in suspend state */
|
---|
450 | usb_log_debug2("HC should be in suspend state(%x).\n",
|
---|
451 | OHCI_RD(instance->registers->control));
|
---|
452 |
|
---|
453 | /* Use HCCA */
|
---|
454 | OHCI_WR(instance->registers->hcca, addr_to_phys(instance->hcca));
|
---|
455 |
|
---|
456 | /* Use queues */
|
---|
457 | OHCI_WR(instance->registers->bulk_head,
|
---|
458 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
---|
459 | usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").\n",
|
---|
460 | instance->lists[USB_TRANSFER_BULK].list_head,
|
---|
461 | instance->lists[USB_TRANSFER_BULK].list_head_pa);
|
---|
462 |
|
---|
463 | OHCI_WR(instance->registers->control_head,
|
---|
464 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
---|
465 | usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").\n",
|
---|
466 | instance->lists[USB_TRANSFER_CONTROL].list_head,
|
---|
467 | instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
|
---|
468 |
|
---|
469 | /* Enable queues */
|
---|
470 | OHCI_SET(instance->registers->control, (C_PLE | C_IE | C_CLE | C_BLE));
|
---|
471 | usb_log_debug("Queues enabled(%x).\n",
|
---|
472 | OHCI_RD(instance->registers->control));
|
---|
473 |
|
---|
474 | /* Enable interrupts */
|
---|
475 | OHCI_WR(instance->registers->interrupt_enable, OHCI_USED_INTERRUPTS);
|
---|
476 | usb_log_debug("Enabled interrupts: %x.\n",
|
---|
477 | OHCI_RD(instance->registers->interrupt_enable));
|
---|
478 | OHCI_WR(instance->registers->interrupt_enable, I_MI);
|
---|
479 |
|
---|
480 | /* Set periodic start to 90% */
|
---|
481 | const uint32_t frame_length =
|
---|
482 | (fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK;
|
---|
483 | OHCI_WR(instance->registers->periodic_start,
|
---|
484 | ((frame_length / 10) * 9) & PS_MASK << PS_SHIFT);
|
---|
485 | usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).\n",
|
---|
486 | OHCI_RD(instance->registers->periodic_start),
|
---|
487 | OHCI_RD(instance->registers->periodic_start), frame_length);
|
---|
488 | C_HCFS_SET(instance->registers->control, C_HCFS_OPERATIONAL);
|
---|
489 | usb_log_debug("OHCI HC up and running (ctl_reg=0x%x).\n",
|
---|
490 | OHCI_RD(instance->registers->control));
|
---|
491 | }
|
---|
492 |
|
---|
493 | /** Initialize schedule queues
|
---|
494 | *
|
---|
495 | * @param[in] instance OHCI hc driver structure
|
---|
496 | * @return Error code
|
---|
497 | */
|
---|
498 | int hc_init_transfer_lists(hc_t *instance)
|
---|
499 | {
|
---|
500 | assert(instance);
|
---|
501 | #define SETUP_ENDPOINT_LIST(type) \
|
---|
502 | do { \
|
---|
503 | const char *name = usb_str_transfer_type(type); \
|
---|
504 | int ret = endpoint_list_init(&instance->lists[type], name); \
|
---|
505 | if (ret != EOK) { \
|
---|
506 | usb_log_error("Failed to setup %s endpoint list: %s.\n", \
|
---|
507 | name, str_error(ret)); \
|
---|
508 | endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]);\
|
---|
509 | endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
|
---|
510 | endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
|
---|
511 | endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
|
---|
512 | return ret; \
|
---|
513 | } \
|
---|
514 | } while (0)
|
---|
515 |
|
---|
516 | SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
|
---|
517 | SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
|
---|
518 | SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
|
---|
519 | SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
|
---|
520 | #undef SETUP_ENDPOINT_LIST
|
---|
521 | endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
|
---|
522 | &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
|
---|
523 |
|
---|
524 | return EOK;
|
---|
525 | }
|
---|
526 |
|
---|
527 | /** Initialize memory structures used by the OHCI hcd.
|
---|
528 | *
|
---|
529 | * @param[in] instance OHCI hc driver structure.
|
---|
530 | * @return Error code.
|
---|
531 | */
|
---|
532 | int hc_init_memory(hc_t *instance)
|
---|
533 | {
|
---|
534 | assert(instance);
|
---|
535 |
|
---|
536 | bzero(&instance->rh, sizeof(instance->rh));
|
---|
537 | /* Init queues */
|
---|
538 | const int ret = hc_init_transfer_lists(instance);
|
---|
539 | if (ret != EOK) {
|
---|
540 | return ret;
|
---|
541 | }
|
---|
542 |
|
---|
543 | /*Init HCCA */
|
---|
544 | instance->hcca = hcca_get();
|
---|
545 | if (instance->hcca == NULL)
|
---|
546 | return ENOMEM;
|
---|
547 | usb_log_debug2("OHCI HCCA initialized at %p.\n", instance->hcca);
|
---|
548 |
|
---|
549 | for (unsigned i = 0; i < HCCA_INT_EP_COUNT; ++i) {
|
---|
550 | hcca_set_int_ep(instance->hcca, i,
|
---|
551 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
---|
552 | }
|
---|
553 | usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").\n",
|
---|
554 | instance->lists[USB_TRANSFER_INTERRUPT].list_head,
|
---|
555 | instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
|
---|
556 |
|
---|
557 | return EOK;
|
---|
558 | }
|
---|
559 |
|
---|
560 | /**
|
---|
561 | * @}
|
---|
562 | */
|
---|