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 | #include <usb/usbdevice.h>
|
---|
43 |
|
---|
44 | #include "hc.h"
|
---|
45 |
|
---|
46 | static int interrupt_emulator(hc_t *instance);
|
---|
47 | static void hc_gain_control(hc_t *instance);
|
---|
48 | static void hc_init_hw(hc_t *instance);
|
---|
49 | static int hc_init_transfer_lists(hc_t *instance);
|
---|
50 | static int hc_init_memory(hc_t *instance);
|
---|
51 | /*----------------------------------------------------------------------------*/
|
---|
52 | int hc_register_hub(hc_t *instance, ddf_fun_t *hub_fun)
|
---|
53 | {
|
---|
54 | assert(instance);
|
---|
55 | assert(hub_fun);
|
---|
56 |
|
---|
57 | usb_address_t hub_address =
|
---|
58 | device_keeper_get_free_address(&instance->manager, USB_SPEED_FULL);
|
---|
59 | instance->rh.address = hub_address;
|
---|
60 | usb_device_keeper_bind(
|
---|
61 | &instance->manager, hub_address, hub_fun->handle);
|
---|
62 |
|
---|
63 | endpoint_t *ep = malloc(sizeof(endpoint_t));
|
---|
64 | assert(ep);
|
---|
65 | int ret = endpoint_init(ep, hub_address, 0, USB_DIRECTION_BOTH,
|
---|
66 | USB_TRANSFER_CONTROL, USB_SPEED_FULL, 64);
|
---|
67 | assert(ret == EOK);
|
---|
68 | ret = usb_endpoint_manager_register_ep(&instance->ep_manager, ep, 0);
|
---|
69 | assert(ret == EOK);
|
---|
70 |
|
---|
71 | char *match_str = NULL;
|
---|
72 | ret = asprintf(&match_str, "usb&class=hub");
|
---|
73 | // ret = (match_str == NULL) ? ret : EOK;
|
---|
74 | if (ret < 0) {
|
---|
75 | usb_log_error(
|
---|
76 | "Failed(%d) to create root hub match-id string.\n", ret);
|
---|
77 | return ret;
|
---|
78 | }
|
---|
79 |
|
---|
80 | ret = ddf_fun_add_match_id(hub_fun, match_str, 100);
|
---|
81 | if (ret != EOK) {
|
---|
82 | usb_log_error("Failed add create root hub match-id.\n");
|
---|
83 | }
|
---|
84 | return ret;
|
---|
85 | }
|
---|
86 | /*----------------------------------------------------------------------------*/
|
---|
87 | int hc_init(hc_t *instance, ddf_fun_t *fun, ddf_dev_t *dev,
|
---|
88 | uintptr_t regs, size_t reg_size, bool interrupts)
|
---|
89 | {
|
---|
90 | assert(instance);
|
---|
91 | int ret = EOK;
|
---|
92 | #define CHECK_RET_RETURN(ret, message...) \
|
---|
93 | if (ret != EOK) { \
|
---|
94 | usb_log_error(message); \
|
---|
95 | return ret; \
|
---|
96 | } else (void)0
|
---|
97 |
|
---|
98 | ret = pio_enable((void*)regs, reg_size, (void**)&instance->registers);
|
---|
99 | CHECK_RET_RETURN(ret,
|
---|
100 | "Failed(%d) to gain access to device registers: %s.\n",
|
---|
101 | ret, str_error(ret));
|
---|
102 |
|
---|
103 | instance->ddf_instance = fun;
|
---|
104 | usb_device_keeper_init(&instance->manager);
|
---|
105 | ret = usb_endpoint_manager_init(&instance->ep_manager,
|
---|
106 | BANDWIDTH_AVAILABLE_USB11);
|
---|
107 | CHECK_RET_RETURN(ret, "Failed to initialize endpoint manager: %s.\n",
|
---|
108 | ret, str_error(ret));
|
---|
109 |
|
---|
110 | if (!interrupts) {
|
---|
111 | instance->interrupt_emulator =
|
---|
112 | fibril_create((int(*)(void*))interrupt_emulator, instance);
|
---|
113 | fibril_add_ready(instance->interrupt_emulator);
|
---|
114 | }
|
---|
115 |
|
---|
116 | hc_gain_control(instance);
|
---|
117 |
|
---|
118 | rh_init(&instance->rh, dev, instance->registers);
|
---|
119 |
|
---|
120 | hc_init_memory(instance);
|
---|
121 | hc_init_hw(instance);
|
---|
122 |
|
---|
123 | /* TODO: implement */
|
---|
124 | return EOK;
|
---|
125 | }
|
---|
126 | /*----------------------------------------------------------------------------*/
|
---|
127 | int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
|
---|
128 | {
|
---|
129 | assert(instance);
|
---|
130 | assert(batch);
|
---|
131 | if (batch->target.address == instance->rh.address) {
|
---|
132 | return rh_request(&instance->rh, batch);
|
---|
133 | }
|
---|
134 | /* TODO: implement */
|
---|
135 | return ENOTSUP;
|
---|
136 | }
|
---|
137 | /*----------------------------------------------------------------------------*/
|
---|
138 | void hc_interrupt(hc_t *instance, uint32_t status)
|
---|
139 | {
|
---|
140 | assert(instance);
|
---|
141 | if (status == 0)
|
---|
142 | return;
|
---|
143 | if (status & IS_RHSC)
|
---|
144 | rh_interrupt(&instance->rh);
|
---|
145 |
|
---|
146 | usb_log_info("OHCI interrupt: %x.\n", status);
|
---|
147 |
|
---|
148 | /* TODO: Check for further interrupt causes */
|
---|
149 | /* TODO: implement */
|
---|
150 | }
|
---|
151 | /*----------------------------------------------------------------------------*/
|
---|
152 | int interrupt_emulator(hc_t *instance)
|
---|
153 | {
|
---|
154 | assert(instance);
|
---|
155 | usb_log_info("Started interrupt emulator.\n");
|
---|
156 | while (1) {
|
---|
157 | const uint32_t status = instance->registers->interrupt_status;
|
---|
158 | instance->registers->interrupt_status = status;
|
---|
159 | hc_interrupt(instance, status);
|
---|
160 | async_usleep(1000);
|
---|
161 | }
|
---|
162 | return EOK;
|
---|
163 | }
|
---|
164 | /*----------------------------------------------------------------------------*/
|
---|
165 | void hc_gain_control(hc_t *instance)
|
---|
166 | {
|
---|
167 | assert(instance);
|
---|
168 | /* Interrupt routing enabled => smm driver is active */
|
---|
169 | if (instance->registers->control & C_IR) {
|
---|
170 | usb_log_info("Found SMM driver requesting ownership change.\n");
|
---|
171 | instance->registers->command_status |= CS_OCR;
|
---|
172 | while (instance->registers->control & C_IR) {
|
---|
173 | async_usleep(1000);
|
---|
174 | }
|
---|
175 | usb_log_info("Ownership taken from SMM driver.\n");
|
---|
176 | return;
|
---|
177 | }
|
---|
178 |
|
---|
179 | const unsigned hc_status =
|
---|
180 | (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
|
---|
181 | /* Interrupt routing disabled && status != USB_RESET => BIOS active */
|
---|
182 | if (hc_status != C_HCFS_RESET) {
|
---|
183 | usb_log_info("Found BIOS driver.\n");
|
---|
184 | if (hc_status == C_HCFS_OPERATIONAL) {
|
---|
185 | usb_log_info("HC operational(BIOS).\n");
|
---|
186 | return;
|
---|
187 | }
|
---|
188 | /* HC is suspended assert resume for 20ms */
|
---|
189 | instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
|
---|
190 | async_usleep(20000);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 |
|
---|
194 | /* HC is in reset (hw startup) => no other driver
|
---|
195 | * maintain reset for at least the time specified in USB spec (50 ms)*/
|
---|
196 | async_usleep(50000);
|
---|
197 |
|
---|
198 | /* turn off legacy emulation */
|
---|
199 | volatile uint32_t *ohci_emulation_reg =
|
---|
200 | (uint32_t*)((char*)instance->registers + 0x100);
|
---|
201 | usb_log_info("OHCI legacy register status %p: %x.\n",
|
---|
202 | ohci_emulation_reg, *ohci_emulation_reg);
|
---|
203 | *ohci_emulation_reg = 0;
|
---|
204 |
|
---|
205 | }
|
---|
206 | /*----------------------------------------------------------------------------*/
|
---|
207 | void hc_init_hw(hc_t *instance)
|
---|
208 | {
|
---|
209 | assert(instance);
|
---|
210 | const uint32_t fm_interval = instance->registers->fm_interval;
|
---|
211 |
|
---|
212 | /* reset hc */
|
---|
213 | instance->registers->command_status = CS_HCR;
|
---|
214 | async_usleep(10);
|
---|
215 |
|
---|
216 | /* restore fm_interval */
|
---|
217 | instance->registers->fm_interval = fm_interval;
|
---|
218 | assert((instance->registers->command_status & CS_HCR) == 0);
|
---|
219 |
|
---|
220 | /* hc is now in suspend state */
|
---|
221 |
|
---|
222 | /* enable queues */
|
---|
223 | instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
|
---|
224 | /* TODO: enable interrupts */
|
---|
225 | /* set periodic start to 90% */
|
---|
226 | instance->registers->periodic_start = (fm_interval / 10) * 9;
|
---|
227 |
|
---|
228 | instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
|
---|
229 | usb_log_info("OHCI HC up and running.\n");
|
---|
230 | }
|
---|
231 | /*----------------------------------------------------------------------------*/
|
---|
232 | int hc_init_transfer_lists(hc_t *instance)
|
---|
233 | {
|
---|
234 | assert(instance);
|
---|
235 |
|
---|
236 | #define SETUP_TRANSFER_LIST(type, name) \
|
---|
237 | do { \
|
---|
238 | int ret = transfer_list_init(&instance->type, name); \
|
---|
239 | if (ret != EOK) { \
|
---|
240 | usb_log_error("Failed(%d) to setup %s transfer list.\n", \
|
---|
241 | ret, name); \
|
---|
242 | transfer_list_fini(&instance->transfers_isochronous); \
|
---|
243 | transfer_list_fini(&instance->transfers_interrupt); \
|
---|
244 | transfer_list_fini(&instance->transfers_control); \
|
---|
245 | transfer_list_fini(&instance->transfers_bulk); \
|
---|
246 | } \
|
---|
247 | } while (0)
|
---|
248 |
|
---|
249 | SETUP_TRANSFER_LIST(transfers_isochronous, "ISOCHRONOUS");
|
---|
250 | SETUP_TRANSFER_LIST(transfers_interrupt, "INTERRUPT");
|
---|
251 | SETUP_TRANSFER_LIST(transfers_control, "CONTROL");
|
---|
252 | SETUP_TRANSFER_LIST(transfers_bulk, "BULK");
|
---|
253 |
|
---|
254 | transfer_list_set_next(&instance->transfers_interrupt,
|
---|
255 | &instance->transfers_isochronous);
|
---|
256 |
|
---|
257 | /* Assign pointers to be used during scheduling */
|
---|
258 | instance->transfers[USB_TRANSFER_INTERRUPT] =
|
---|
259 | &instance->transfers_interrupt;
|
---|
260 | instance->transfers[USB_TRANSFER_ISOCHRONOUS] =
|
---|
261 | &instance->transfers_interrupt;
|
---|
262 | instance->transfers[USB_TRANSFER_CONTROL] =
|
---|
263 | &instance->transfers_control;
|
---|
264 | instance->transfers[USB_TRANSFER_BULK] =
|
---|
265 | &instance->transfers_bulk;
|
---|
266 |
|
---|
267 | return EOK;
|
---|
268 | #undef CHECK_RET_CLEAR_RETURN
|
---|
269 | }
|
---|
270 | /*----------------------------------------------------------------------------*/
|
---|
271 | int hc_init_memory(hc_t *instance)
|
---|
272 | {
|
---|
273 | assert(instance);
|
---|
274 | /* init queues */
|
---|
275 | hc_init_transfer_lists(instance);
|
---|
276 |
|
---|
277 | /* init HCCA */
|
---|
278 | instance->hcca = malloc32(sizeof(hcca_t));
|
---|
279 | if (instance->hcca == NULL)
|
---|
280 | return ENOMEM;
|
---|
281 | bzero(instance->hcca, sizeof(hcca_t));
|
---|
282 | instance->registers->hcca = addr_to_phys(instance->hcca);
|
---|
283 |
|
---|
284 | /* use queues */
|
---|
285 | instance->registers->bulk_head = instance->transfers_bulk.list_head_pa;
|
---|
286 | instance->registers->control_head =
|
---|
287 | instance->transfers_control.list_head_pa;
|
---|
288 |
|
---|
289 | unsigned i = 0;
|
---|
290 | for (; i < 32; ++i) {
|
---|
291 | instance->hcca->int_ep[i] =
|
---|
292 | instance->transfers_interrupt.list_head_pa;
|
---|
293 | }
|
---|
294 |
|
---|
295 | return EOK;
|
---|
296 | }
|
---|
297 | /**
|
---|
298 | * @}
|
---|
299 | */
|
---|