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 drvusbohci
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /** @file
|
---|
33 | * @brief OHCI driver
|
---|
34 | */
|
---|
35 |
|
---|
36 | #include <errno.h>
|
---|
37 | #include <str_error.h>
|
---|
38 | #include <ddf/interrupt.h>
|
---|
39 | #include <usb_iface.h>
|
---|
40 | #include <usb/usb.h>
|
---|
41 | #include <usb/ddfiface.h>
|
---|
42 | #include <usb/debug.h>
|
---|
43 |
|
---|
44 | #include "ohci.h"
|
---|
45 | #include "res.h"
|
---|
46 | #include "hc.h"
|
---|
47 |
|
---|
48 |
|
---|
49 |
|
---|
50 | /** IRQ handling callback, identifies device
|
---|
51 | *
|
---|
52 | * @param[in] dev DDF instance of the device to use.
|
---|
53 | * @param[in] iid (Unused).
|
---|
54 | * @param[in] call Pointer to the call that represents interrupt.
|
---|
55 | */
|
---|
56 | static void irq_handler(ddf_dev_t *dev, ipc_callid_t iid, ipc_call_t *call)
|
---|
57 | {
|
---|
58 | assert(dev);
|
---|
59 | hcd_t *hcd = dev_to_hcd(dev);
|
---|
60 | if (!hcd || !hcd->private_data) {
|
---|
61 | usb_log_warning("Interrupt on device that is not ready.\n");
|
---|
62 | return;
|
---|
63 | }
|
---|
64 |
|
---|
65 | const uint16_t status = IPC_GET_ARG1(*call);
|
---|
66 | hc_interrupt(hcd->private_data, status);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /** Initialize hc and rh ddf structures and their respective drivers.
|
---|
70 | *
|
---|
71 | * @param[in] device DDF instance of the device to use.
|
---|
72 | * @param[in] instance OHCI structure to use.
|
---|
73 | *
|
---|
74 | * This function does all the preparatory work for hc and rh drivers:
|
---|
75 | * - gets device hw resources
|
---|
76 | * - disables OHCI legacy support
|
---|
77 | * - asks for interrupt
|
---|
78 | * - registers interrupt handler
|
---|
79 | */
|
---|
80 | int device_setup_ohci(ddf_dev_t *device)
|
---|
81 | {
|
---|
82 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
83 | if (ret != EOK) { \
|
---|
84 | usb_log_error(message); \
|
---|
85 | return ret; \
|
---|
86 | }
|
---|
87 |
|
---|
88 | uintptr_t reg_base = 0;
|
---|
89 | size_t reg_size = 0;
|
---|
90 | int irq = 0;
|
---|
91 |
|
---|
92 | int ret = get_my_registers(device, ®_base, ®_size, &irq);
|
---|
93 | CHECK_RET_RETURN(ret, "Failed to get register memory addresses for %"
|
---|
94 | PRIun ": %s.\n", ddf_dev_get_handle(device), str_error(ret));
|
---|
95 |
|
---|
96 | usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
|
---|
97 | (void *) reg_base, reg_size, irq);
|
---|
98 |
|
---|
99 | const size_t ranges_count = hc_irq_pio_range_count();
|
---|
100 | const size_t cmds_count = hc_irq_cmd_count();
|
---|
101 | irq_pio_range_t irq_ranges[ranges_count];
|
---|
102 | irq_cmd_t irq_cmds[cmds_count];
|
---|
103 | irq_code_t irq_code = {
|
---|
104 | .rangecount = ranges_count,
|
---|
105 | .ranges = irq_ranges,
|
---|
106 | .cmdcount = cmds_count,
|
---|
107 | .cmds = irq_cmds
|
---|
108 | };
|
---|
109 |
|
---|
110 | ret = hc_get_irq_code(irq_ranges, sizeof(irq_ranges), irq_cmds,
|
---|
111 | sizeof(irq_cmds), reg_base, reg_size);
|
---|
112 | CHECK_RET_RETURN(ret, "Failed to gen IRQ code: %s.\n", str_error(ret));
|
---|
113 |
|
---|
114 | /* Register handler to avoid interrupt lockup */
|
---|
115 | ret = register_interrupt_handler(device, irq, irq_handler, &irq_code);
|
---|
116 | CHECK_RET_RETURN(ret,
|
---|
117 | "Failed to register irq handler: %s.\n", str_error(ret));
|
---|
118 |
|
---|
119 | /* Try to enable interrupts */
|
---|
120 | bool interrupts = false;
|
---|
121 | ret = enable_interrupts(device);
|
---|
122 | if (ret != EOK) {
|
---|
123 | usb_log_warning("Failed to enable interrupts: %s."
|
---|
124 | " Falling back to polling\n", str_error(ret));
|
---|
125 | /* We don't need that handler */
|
---|
126 | unregister_interrupt_handler(device, irq);
|
---|
127 | } else {
|
---|
128 | usb_log_debug("Hw interrupts enabled.\n");
|
---|
129 | interrupts = true;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /* Initialize generic HCD driver */
|
---|
133 | ret = hcd_setup_device(device, NULL);
|
---|
134 | if (ret != EOK) {
|
---|
135 | unregister_interrupt_handler(device, irq);
|
---|
136 | return ret;
|
---|
137 | }
|
---|
138 |
|
---|
139 |
|
---|
140 | // TODO: Undo hcd_setup_device
|
---|
141 | #define CHECK_RET_CLEAN_RETURN(ret, message...) \
|
---|
142 | if (ret != EOK) { \
|
---|
143 | unregister_interrupt_handler(device, irq); \
|
---|
144 | CHECK_RET_RETURN(ret, message); \
|
---|
145 | } else (void)0
|
---|
146 |
|
---|
147 | hc_t *hc_impl = malloc(sizeof(hc_t));
|
---|
148 | ret = hc_impl ? EOK : ENOMEM;
|
---|
149 | CHECK_RET_CLEAN_RETURN(ret, "Failed to allocate driver structure.\n");
|
---|
150 |
|
---|
151 | /* Initialize OHCI HC */
|
---|
152 | ret = hc_init(hc_impl, reg_base, reg_size, interrupts);
|
---|
153 | CHECK_RET_CLEAN_RETURN(ret, "Failed to init hc: %s.\n", str_error(ret));
|
---|
154 |
|
---|
155 | /* Connect OHCI to generic HCD */
|
---|
156 | hcd_set_implementation(dev_to_hcd(device), hc_impl,
|
---|
157 | hc_schedule, ohci_endpoint_init, ohci_endpoint_fini);
|
---|
158 |
|
---|
159 | /* HC should be running OK. We can add root hub */
|
---|
160 | ret = hcd_setup_hub(dev_to_hcd(device), &hc_impl->rh.address, device);
|
---|
161 | CHECK_RET_CLEAN_RETURN(ret,
|
---|
162 | "Failed to register OHCI root hub: %s.\n", str_error(ret));
|
---|
163 |
|
---|
164 | return ret;
|
---|
165 | }
|
---|
166 | /**
|
---|
167 | * @}
|
---|
168 | */
|
---|