source: mainline/uspace/drv/ohci/hc.c@ 9f104af4

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

More debug output and some fixes

Add mutex for scheduling and removing of batches
batch_data initialize the first TD not the second
disable and enable both isochronous and interrupt processing when scheduling periodic transfer.
undef the right macro in transfer_list initialization

  • Property mode set to 100644
File size: 13.1 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#include <usb/usbdevice.h>
43
44#include "hc.h"
45
46static int interrupt_emulator(hc_t *instance);
47static void hc_gain_control(hc_t *instance);
48static void hc_init_hw(hc_t *instance);
49static int hc_init_transfer_lists(hc_t *instance);
50static int hc_init_memory(hc_t *instance);
51/*----------------------------------------------------------------------------*/
52int 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/*----------------------------------------------------------------------------*/
87int 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...) \
93if (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 hc_gain_control(instance);
111 ret = hc_init_memory(instance);
112 CHECK_RET_RETURN(ret, "Failed to create OHCI memory structures:%s.\n",
113 ret, str_error(ret));
114 hc_init_hw(instance);
115 fibril_mutex_initialize(&instance->guard);
116
117 rh_init(&instance->rh, dev, instance->registers);
118
119 if (!interrupts) {
120 instance->interrupt_emulator =
121 fibril_create((int(*)(void*))interrupt_emulator, instance);
122 fibril_add_ready(instance->interrupt_emulator);
123 }
124
125 return EOK;
126}
127/*----------------------------------------------------------------------------*/
128int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
129{
130 assert(instance);
131 assert(batch);
132
133 /* check for root hub communication */
134 if (batch->target.address == instance->rh.address) {
135 return rh_request(&instance->rh, batch);
136 }
137
138 fibril_mutex_lock(&instance->guard);
139 switch (batch->transfer_type) {
140 case USB_TRANSFER_CONTROL:
141 instance->registers->control &= ~C_CLE;
142 transfer_list_add_batch(
143 instance->transfers[batch->transfer_type], batch);
144 instance->registers->command_status |= CS_CLF;
145 usb_log_debug2("Set CS control transfer filled: %x.\n",
146 instance->registers->command_status);
147 instance->registers->control_current = 0;
148 instance->registers->control |= C_CLE;
149 break;
150 case USB_TRANSFER_BULK:
151 instance->registers->control &= ~C_BLE;
152 transfer_list_add_batch(
153 instance->transfers[batch->transfer_type], batch);
154 instance->registers->command_status |= CS_BLF;
155 usb_log_debug2("Set bulk transfer filled: %x.\n",
156 instance->registers->command_status);
157 instance->registers->control |= C_BLE;
158 break;
159 case USB_TRANSFER_INTERRUPT:
160 case USB_TRANSFER_ISOCHRONOUS:
161 instance->registers->control &= (~C_PLE & ~C_IE);
162 transfer_list_add_batch(
163 instance->transfers[batch->transfer_type], batch);
164 instance->registers->control |= C_PLE | C_IE;
165 usb_log_debug2("Added periodic transfer: %x.\n",
166 instance->registers->periodic_current);
167 break;
168 default:
169 break;
170 }
171 fibril_mutex_unlock(&instance->guard);
172 return EOK;
173}
174/*----------------------------------------------------------------------------*/
175void hc_interrupt(hc_t *instance, uint32_t status)
176{
177 assert(instance);
178 if ((status & ~IS_SF) == 0) /* ignore sof status */
179 return;
180 if (status & IS_RHSC)
181 rh_interrupt(&instance->rh);
182
183 usb_log_debug("OHCI interrupt: %x.\n", status);
184
185 if (status & IS_WDH) {
186 fibril_mutex_lock(&instance->guard);
187 usb_log_debug2("HCCA: %p-%p(%p).\n", instance->hcca,
188 instance->registers->hcca, addr_to_phys(instance->hcca));
189 usb_log_debug2("Periodic current: %p.\n",
190 instance->registers->periodic_current);
191 LIST_INITIALIZE(done);
192 transfer_list_remove_finished(
193 &instance->transfers_interrupt, &done);
194 transfer_list_remove_finished(
195 &instance->transfers_isochronous, &done);
196 transfer_list_remove_finished(
197 &instance->transfers_control, &done);
198 transfer_list_remove_finished(
199 &instance->transfers_bulk, &done);
200
201 while (!list_empty(&done)) {
202 link_t *item = done.next;
203 list_remove(item);
204 usb_transfer_batch_t *batch =
205 list_get_instance(item, usb_transfer_batch_t, link);
206 usb_transfer_batch_finish(batch);
207 }
208 fibril_mutex_unlock(&instance->guard);
209 }
210}
211/*----------------------------------------------------------------------------*/
212int interrupt_emulator(hc_t *instance)
213{
214 assert(instance);
215 usb_log_info("Started interrupt emulator.\n");
216 while (1) {
217 const uint32_t status = instance->registers->interrupt_status;
218 instance->registers->interrupt_status = status;
219 hc_interrupt(instance, status);
220 async_usleep(50000);
221 }
222 return EOK;
223}
224/*----------------------------------------------------------------------------*/
225void hc_gain_control(hc_t *instance)
226{
227 assert(instance);
228 /* Turn off legacy emulation */
229 volatile uint32_t *ohci_emulation_reg =
230 (uint32_t*)((char*)instance->registers + 0x100);
231 usb_log_debug("OHCI legacy register %p: %x.\n",
232 ohci_emulation_reg, *ohci_emulation_reg);
233 *ohci_emulation_reg = 0;
234
235 /* Interrupt routing enabled => smm driver is active */
236 if (instance->registers->control & C_IR) {
237 usb_log_debug("SMM driver: request ownership change.\n");
238 instance->registers->command_status |= CS_OCR;
239 while (instance->registers->control & C_IR) {
240 async_usleep(1000);
241 }
242 usb_log_info("SMM driver: Ownership taken.\n");
243 return;
244 }
245
246 const unsigned hc_status =
247 (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
248 /* Interrupt routing disabled && status != USB_RESET => BIOS active */
249 if (hc_status != C_HCFS_RESET) {
250 usb_log_debug("BIOS driver found.\n");
251 if (hc_status == C_HCFS_OPERATIONAL) {
252 usb_log_info("BIOS driver: HC operational.\n");
253 return;
254 }
255 /* HC is suspended assert resume for 20ms */
256 instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
257 async_usleep(20000);
258 usb_log_info("BIOS driver: HC resumed.\n");
259 return;
260 }
261
262 /* HC is in reset (hw startup) => no other driver
263 * maintain reset for at least the time specified in USB spec (50 ms)*/
264 usb_log_info("HC found in reset.\n");
265 async_usleep(50000);
266}
267/*----------------------------------------------------------------------------*/
268void hc_init_hw(hc_t *instance)
269{
270 /* OHCI guide page 42 */
271 assert(instance);
272 usb_log_debug2("Started hc initialization routine.\n");
273
274 /* Save contents of fm_interval register */
275 const uint32_t fm_interval = instance->registers->fm_interval;
276 usb_log_debug2("Old value of HcFmInterval: %x.\n", fm_interval);
277
278 /* Reset hc */
279 usb_log_debug2("HC reset.\n");
280 size_t time = 0;
281 instance->registers->command_status = CS_HCR;
282 while (instance->registers->command_status & CS_HCR) {
283 async_usleep(10);
284 time += 10;
285 }
286 usb_log_debug2("HC reset complete in %zu us.\n", time);
287
288 /* Restore fm_interval */
289 instance->registers->fm_interval = fm_interval;
290 assert((instance->registers->command_status & CS_HCR) == 0);
291
292 /* hc is now in suspend state */
293 usb_log_debug2("HC should be in suspend state(%x).\n",
294 instance->registers->control);
295
296 /* Use HCCA */
297 instance->registers->hcca = addr_to_phys(instance->hcca);
298
299 /* Use queues */
300 instance->registers->bulk_head = instance->transfers_bulk.list_head_pa;
301 usb_log_debug2("Bulk HEAD set to: %p(%p).\n",
302 instance->transfers_bulk.list_head,
303 instance->transfers_bulk.list_head_pa);
304
305 instance->registers->control_head =
306 instance->transfers_control.list_head_pa;
307 usb_log_debug2("Control HEAD set to: %p(%p).\n",
308 instance->transfers_control.list_head,
309 instance->transfers_control.list_head_pa);
310
311 /* Enable queues */
312 instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
313 usb_log_debug2("All queues enabled(%x).\n",
314 instance->registers->control);
315
316 /* Disable interrupts */
317 instance->registers->interrupt_disable = I_SF | I_OC;
318 usb_log_debug2("Disabling interrupts: %x.\n",
319 instance->registers->interrupt_disable);
320 instance->registers->interrupt_disable = I_MI;
321 usb_log_debug2("Enabled interrupts: %x.\n",
322 instance->registers->interrupt_enable);
323
324 /* Set periodic start to 90% */
325 uint32_t frame_length = ((fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK);
326 instance->registers->periodic_start = (frame_length / 10) * 9;
327 usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).\n",
328 instance->registers->periodic_start,
329 instance->registers->periodic_start, frame_length);
330
331 instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
332 usb_log_info("OHCI HC up and running(%x).\n",
333 instance->registers->control);
334}
335/*----------------------------------------------------------------------------*/
336int hc_init_transfer_lists(hc_t *instance)
337{
338 assert(instance);
339
340#define SETUP_TRANSFER_LIST(type, name) \
341do { \
342 int ret = transfer_list_init(&instance->type, name); \
343 if (ret != EOK) { \
344 usb_log_error("Failed(%d) to setup %s transfer list.\n", \
345 ret, name); \
346 transfer_list_fini(&instance->transfers_isochronous); \
347 transfer_list_fini(&instance->transfers_interrupt); \
348 transfer_list_fini(&instance->transfers_control); \
349 transfer_list_fini(&instance->transfers_bulk); \
350 } \
351} while (0)
352
353 SETUP_TRANSFER_LIST(transfers_isochronous, "ISOCHRONOUS");
354 SETUP_TRANSFER_LIST(transfers_interrupt, "INTERRUPT");
355 SETUP_TRANSFER_LIST(transfers_control, "CONTROL");
356 SETUP_TRANSFER_LIST(transfers_bulk, "BULK");
357#undef SETUP_TRANSFER_LIST
358 transfer_list_set_next(&instance->transfers_interrupt,
359 &instance->transfers_isochronous);
360
361 /* Assign pointers to be used during scheduling */
362 instance->transfers[USB_TRANSFER_INTERRUPT] =
363 &instance->transfers_interrupt;
364 instance->transfers[USB_TRANSFER_ISOCHRONOUS] =
365 &instance->transfers_interrupt;
366 instance->transfers[USB_TRANSFER_CONTROL] =
367 &instance->transfers_control;
368 instance->transfers[USB_TRANSFER_BULK] =
369 &instance->transfers_bulk;
370
371 return EOK;
372}
373/*----------------------------------------------------------------------------*/
374int hc_init_memory(hc_t *instance)
375{
376 assert(instance);
377 /* Init queues */
378 hc_init_transfer_lists(instance);
379
380 /*Init HCCA */
381 instance->hcca = malloc32(sizeof(hcca_t));
382 if (instance->hcca == NULL)
383 return ENOMEM;
384 bzero(instance->hcca, sizeof(hcca_t));
385 usb_log_debug2("OHCI HCCA initialized at %p.\n", instance->hcca);
386
387 unsigned i = 0;
388 for (; i < 32; ++i) {
389 instance->hcca->int_ep[i] =
390 instance->transfers_interrupt.list_head_pa;
391 }
392 usb_log_debug2("Interrupt HEADs set to: %p(%p).\n",
393 instance->transfers_interrupt.list_head,
394 instance->transfers_interrupt.list_head_pa);
395
396 return EOK;
397}
398/**
399 * @}
400 */
Note: See TracBrowser for help on using the repository browser.