source: mainline/uspace/drv/bus/usb/ohci/hc.c@ 7bce1fc

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7bce1fc was 9c10e51, checked in by Jan Vesely <jano.vesely@…>, 14 years ago

ohci: use driver specific structure instead of the generic one

ohci: do not create hw structures if communicating with root hub

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