source: mainline/uspace/drv/ohci/hc.c@ a43f1d18

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

Set filled flag when scheduling transfers

  • Property mode set to 100644
File size: 10.0 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 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/*----------------------------------------------------------------------------*/
127int hc_schedule(hc_t *instance, usb_transfer_batch_t *batch)
128{
129 assert(instance);
130 assert(batch);
131
132 /* check for root hub communication */
133 if (batch->target.address == instance->rh.address) {
134 return rh_request(&instance->rh, batch);
135 }
136
137 transfer_list_add_batch(
138 instance->transfers[batch->transfer_type], batch);
139
140 switch (batch->transfer_type) {
141 case USB_TRANSFER_CONTROL:
142 instance->registers->command_status |= CS_CLF;
143 break;
144 case USB_TRANSFER_BULK:
145 instance->registers->command_status |= CS_BLF;
146 break;
147 default:
148 break;
149 }
150 return EOK;
151}
152/*----------------------------------------------------------------------------*/
153void hc_interrupt(hc_t *instance, uint32_t status)
154{
155 assert(instance);
156 if (status == 0)
157 return;
158 if (status & IS_RHSC)
159 rh_interrupt(&instance->rh);
160
161 usb_log_info("OHCI interrupt: %x.\n", status);
162
163 LIST_INITIALIZE(done);
164 transfer_list_remove_finished(&instance->transfers_interrupt, &done);
165 transfer_list_remove_finished(&instance->transfers_isochronous, &done);
166 transfer_list_remove_finished(&instance->transfers_control, &done);
167 transfer_list_remove_finished(&instance->transfers_bulk, &done);
168
169 while (!list_empty(&done)) {
170 link_t *item = done.next;
171 list_remove(item);
172 usb_transfer_batch_t *batch =
173 list_get_instance(item, usb_transfer_batch_t, link);
174 usb_transfer_batch_finish(batch);
175 }
176}
177/*----------------------------------------------------------------------------*/
178int interrupt_emulator(hc_t *instance)
179{
180 assert(instance);
181 usb_log_info("Started interrupt emulator.\n");
182 while (1) {
183 const uint32_t status = instance->registers->interrupt_status;
184 instance->registers->interrupt_status = status;
185 hc_interrupt(instance, status);
186 async_usleep(1000);
187 }
188 return EOK;
189}
190/*----------------------------------------------------------------------------*/
191void hc_gain_control(hc_t *instance)
192{
193 assert(instance);
194 /* Interrupt routing enabled => smm driver is active */
195 if (instance->registers->control & C_IR) {
196 usb_log_info("Found SMM driver requesting ownership change.\n");
197 instance->registers->command_status |= CS_OCR;
198 while (instance->registers->control & C_IR) {
199 async_usleep(1000);
200 }
201 usb_log_info("Ownership taken from SMM driver.\n");
202 return;
203 }
204
205 const unsigned hc_status =
206 (instance->registers->control >> C_HCFS_SHIFT) & C_HCFS_MASK;
207 /* Interrupt routing disabled && status != USB_RESET => BIOS active */
208 if (hc_status != C_HCFS_RESET) {
209 usb_log_info("Found BIOS driver.\n");
210 if (hc_status == C_HCFS_OPERATIONAL) {
211 usb_log_info("HC operational(BIOS).\n");
212 return;
213 }
214 /* HC is suspended assert resume for 20ms */
215 instance->registers->control &= (C_HCFS_RESUME << C_HCFS_SHIFT);
216 async_usleep(20000);
217 return;
218 }
219
220 /* HC is in reset (hw startup) => no other driver
221 * maintain reset for at least the time specified in USB spec (50 ms)*/
222 async_usleep(50000);
223
224 /* turn off legacy emulation */
225 volatile uint32_t *ohci_emulation_reg =
226 (uint32_t*)((char*)instance->registers + 0x100);
227 usb_log_info("OHCI legacy register status %p: %x.\n",
228 ohci_emulation_reg, *ohci_emulation_reg);
229 *ohci_emulation_reg = 0;
230
231}
232/*----------------------------------------------------------------------------*/
233void hc_init_hw(hc_t *instance)
234{
235 assert(instance);
236 const uint32_t fm_interval = instance->registers->fm_interval;
237
238 /* reset hc */
239 instance->registers->command_status = CS_HCR;
240 async_usleep(10);
241
242 /* restore fm_interval */
243 instance->registers->fm_interval = fm_interval;
244 assert((instance->registers->command_status & CS_HCR) == 0);
245
246 /* hc is now in suspend state */
247
248 /* enable queues */
249 instance->registers->control |= (C_PLE | C_IE | C_CLE | C_BLE);
250 /* TODO: enable interrupts */
251 /* set periodic start to 90% */
252 instance->registers->periodic_start = (fm_interval / 10) * 9;
253
254 instance->registers->control &= (C_HCFS_OPERATIONAL << C_HCFS_SHIFT);
255 usb_log_info("OHCI HC up and running.\n");
256}
257/*----------------------------------------------------------------------------*/
258int hc_init_transfer_lists(hc_t *instance)
259{
260 assert(instance);
261
262#define SETUP_TRANSFER_LIST(type, name) \
263do { \
264 int ret = transfer_list_init(&instance->type, name); \
265 if (ret != EOK) { \
266 usb_log_error("Failed(%d) to setup %s transfer list.\n", \
267 ret, name); \
268 transfer_list_fini(&instance->transfers_isochronous); \
269 transfer_list_fini(&instance->transfers_interrupt); \
270 transfer_list_fini(&instance->transfers_control); \
271 transfer_list_fini(&instance->transfers_bulk); \
272 } \
273} while (0)
274
275 SETUP_TRANSFER_LIST(transfers_isochronous, "ISOCHRONOUS");
276 SETUP_TRANSFER_LIST(transfers_interrupt, "INTERRUPT");
277 SETUP_TRANSFER_LIST(transfers_control, "CONTROL");
278 SETUP_TRANSFER_LIST(transfers_bulk, "BULK");
279
280 transfer_list_set_next(&instance->transfers_interrupt,
281 &instance->transfers_isochronous);
282
283 /* Assign pointers to be used during scheduling */
284 instance->transfers[USB_TRANSFER_INTERRUPT] =
285 &instance->transfers_interrupt;
286 instance->transfers[USB_TRANSFER_ISOCHRONOUS] =
287 &instance->transfers_interrupt;
288 instance->transfers[USB_TRANSFER_CONTROL] =
289 &instance->transfers_control;
290 instance->transfers[USB_TRANSFER_BULK] =
291 &instance->transfers_bulk;
292
293 return EOK;
294#undef CHECK_RET_CLEAR_RETURN
295}
296/*----------------------------------------------------------------------------*/
297int hc_init_memory(hc_t *instance)
298{
299 assert(instance);
300 /* init queues */
301 hc_init_transfer_lists(instance);
302
303 /* init HCCA */
304 instance->hcca = malloc32(sizeof(hcca_t));
305 if (instance->hcca == NULL)
306 return ENOMEM;
307 bzero(instance->hcca, sizeof(hcca_t));
308 instance->registers->hcca = addr_to_phys(instance->hcca);
309
310 /* use queues */
311 instance->registers->bulk_head = instance->transfers_bulk.list_head_pa;
312 instance->registers->control_head =
313 instance->transfers_control.list_head_pa;
314
315 unsigned i = 0;
316 for (; i < 32; ++i) {
317 instance->hcca->int_ep[i] =
318 instance->transfers_interrupt.list_head_pa;
319 }
320
321 return EOK;
322}
323/**
324 * @}
325 */
Note: See TracBrowser for help on using the repository browser.