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