source: mainline/uspace/drv/bus/usb/ehci/hc.c@ 763dbcb

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

ehci: Implement dequeue.

Includes async list doorbell interrupt handling

  • Property mode set to 100644
File size: 11.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
29/** @addtogroup drvusbehcihc
30 * @{
31 */
32/** @file
33 * @brief EHCI Host controller driver routines
34 */
35
36#include <assert.h>
37#include <async.h>
38#include <errno.h>
39#include <macros.h>
40#include <mem.h>
41#include <stdlib.h>
42#include <str_error.h>
43#include <sys/types.h>
44
45#include <usb/debug.h>
46#include <usb/usb.h>
47
48//#include "ehci_endpoint.h"
49//#include "ehci_batch.h"
50#include "utils/malloc32.h"
51
52#include "hc.h"
53
54#define EHCI_USED_INTERRUPTS \
55 (USB_INTR_IRQ_FLAG | USB_INTR_ERR_IRQ_FLAG | USB_INTR_PORT_CHANGE_FLAG)
56
57static const irq_pio_range_t ehci_pio_ranges[] = {
58 {
59 .base = 0,
60 .size = sizeof(ehci_regs_t)
61 }
62};
63
64static const irq_cmd_t ehci_irq_commands[] = {
65 {
66 .cmd = CMD_PIO_READ_32,
67 .dstarg = 1,
68 .addr = NULL
69 },
70 {
71 .cmd = CMD_AND,
72 .srcarg = 1,
73 .dstarg = 2,
74 .value = 0
75 },
76 {
77 .cmd = CMD_PREDICATE,
78 .srcarg = 2,
79 .value = 2
80 },
81 {
82 .cmd = CMD_PIO_WRITE_A_32,
83 .srcarg = 1,
84 .addr = NULL
85 },
86 {
87 .cmd = CMD_ACCEPT
88 }
89};
90
91static void hc_start(hc_t *instance);
92static int hc_init_memory(hc_t *instance);
93
94/** Generate IRQ code.
95 * @param[out] ranges PIO ranges buffer.
96 * @param[in] hw_res Device's resources.
97 *
98 * @return Error code.
99 */
100int ehci_hc_gen_irq_code(irq_code_t *code, const hw_res_list_parsed_t *hw_res)
101{
102 assert(code);
103 assert(hw_res);
104
105 if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1)
106 return EINVAL;
107
108 addr_range_t regs = hw_res->mem_ranges.ranges[0];
109
110 if (RNGSZ(regs) < sizeof(ehci_regs_t))
111 return EOVERFLOW;
112
113 code->ranges = malloc(sizeof(ehci_pio_ranges));
114 if (code->ranges == NULL)
115 return ENOMEM;
116
117 code->cmds = malloc(sizeof(ehci_irq_commands));
118 if (code->cmds == NULL) {
119 free(code->ranges);
120 return ENOMEM;
121 }
122
123 code->rangecount = ARRAY_SIZE(ehci_pio_ranges);
124 code->cmdcount = ARRAY_SIZE(ehci_irq_commands);
125
126 memcpy(code->ranges, ehci_pio_ranges, sizeof(ehci_pio_ranges));
127 code->ranges[0].base = RNGABS(regs);
128
129 memcpy(code->cmds, ehci_irq_commands, sizeof(ehci_irq_commands));
130 ehci_caps_regs_t *caps = NULL;
131 int ret = pio_enable_range(&regs, (void**)&caps);
132 if (ret != EOK) {
133 return ret;
134 }
135 ehci_regs_t *registers =
136 (ehci_regs_t *)(RNGABSPTR(regs) + EHCI_RD8(caps->caplength));
137 code->cmds[0].addr = (void *) &registers->usbsts;
138 code->cmds[3].addr = (void *) &registers->usbsts;
139 EHCI_WR(code->cmds[1].value, EHCI_USED_INTERRUPTS);
140
141 usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.\n",
142 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
143
144 return hw_res->irqs.irqs[0];
145}
146
147/** Initialize EHCI hc driver structure
148 *
149 * @param[in] instance Memory place for the structure.
150 * @param[in] regs Device's I/O registers range.
151 * @param[in] interrupts True if w interrupts should be used
152 * @return Error code
153 */
154int hc_init(hc_t *instance, const hw_res_list_parsed_t *hw_res, bool interrupts)
155{
156 assert(instance);
157 assert(hw_res);
158 if (hw_res->mem_ranges.count != 1 ||
159 hw_res->mem_ranges.ranges[0].size <
160 (sizeof(ehci_caps_regs_t) + sizeof(ehci_regs_t)))
161 return EINVAL;
162
163 int ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
164 (void **)&instance->caps);
165 if (ret != EOK) {
166 usb_log_error("Failed to gain access to device registers: %s.\n",
167 str_error(ret));
168 return ret;
169 }
170 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.\n",
171 hw_res->mem_ranges.ranges[0].address.absolute,
172 hw_res->mem_ranges.ranges[0].size);
173 instance->registers =
174 (void*)instance->caps + EHCI_RD8(instance->caps->caplength);
175
176 list_initialize(&instance->pending_batches);
177 fibril_mutex_initialize(&instance->guard);
178 fibril_condvar_initialize(&instance->async_doorbell);
179
180 ret = hc_init_memory(instance);
181 if (ret != EOK) {
182 usb_log_error("Failed to create EHCI memory structures: %s.\n",
183 str_error(ret));
184 return ret;
185 }
186
187 ehci_rh_init(
188 &instance->rh, instance->caps, instance->registers, "ehci rh");
189 hc_start(instance);
190
191 return EOK;
192}
193
194/** Safely dispose host controller internal structures
195 *
196 * @param[in] instance Host controller structure to use.
197 */
198void hc_fini(hc_t *instance)
199{
200 assert(instance);
201 //TODO: stop the hw
202#if 0
203 endpoint_list_fini(&instance->async_list);
204 endpoint_list_fini(&instance->int_list);
205 return_page(instance->periodic_list_base);
206#endif
207};
208
209void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
210{
211 assert(instance);
212 assert(ep);
213 ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
214 switch (ep->transfer_type)
215 {
216 case USB_TRANSFER_CONTROL:
217 endpoint_list_prepend_ep(&instance->async_list, ehci_ep);
218 break;
219 case USB_TRANSFER_BULK:
220 endpoint_list_append_ep(&instance->async_list, ehci_ep);
221 break;
222 case USB_TRANSFER_INTERRUPT:
223 endpoint_list_append_ep(&instance->int_list, ehci_ep);
224 break;
225 case USB_TRANSFER_ISOCHRONOUS:
226 /* NOT SUPPORTED */
227 break;
228 }
229}
230
231void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
232{
233 assert(instance);
234 assert(ep);
235 ehci_endpoint_t *ehci_ep = ehci_endpoint_get(ep);
236 switch (ep->transfer_type)
237 {
238 case USB_TRANSFER_INTERRUPT:
239 endpoint_list_append_ep(&instance->int_list, ehci_ep);
240 /* Fall through */
241 case USB_TRANSFER_ISOCHRONOUS:
242 /* NOT SUPPORTED */
243 return;
244 case USB_TRANSFER_CONTROL:
245 case USB_TRANSFER_BULK:
246 endpoint_list_remove_ep(&instance->async_list, ehci_ep);
247 break;
248 }
249 fibril_mutex_lock(&instance->guard);
250 EHCI_SET(instance->registers->usbcmd, USB_CMD_ASYNC_SCHEDULE_FLAG);
251 fibril_condvar_wait(&instance->async_doorbell, &instance->guard);
252 fibril_mutex_unlock(&instance->guard);
253}
254
255int ehci_hc_status(hcd_t *hcd, uint32_t *status)
256{
257 assert(hcd);
258 hc_t *instance = hcd->driver.data;
259 assert(instance);
260 assert(status);
261 *status = 0;
262 if (instance->registers) {
263 *status = EHCI_RD(instance->registers->usbsts);
264 EHCI_WR(instance->registers->usbsts, *status);
265 }
266 return EOK;
267}
268
269/** Add USB transfer to the schedule.
270 *
271 * @param[in] hcd HCD driver structure.
272 * @param[in] batch Batch representing the transfer.
273 * @return Error code.
274 */
275int ehci_hc_schedule(hcd_t *hcd, usb_transfer_batch_t *batch)
276{
277 assert(hcd);
278 hc_t *instance = hcd->driver.data;
279 assert(instance);
280
281 /* Check for root hub communication */
282 if (batch->ep->address == ehci_rh_get_address(&instance->rh)) {
283 usb_log_debug("EHCI root hub request.\n");
284 return ehci_rh_schedule(&instance->rh, batch);
285 }
286 return ENOTSUP;
287}
288
289/** Interrupt handling routine
290 *
291 * @param[in] hcd HCD driver structure.
292 * @param[in] status Value of the status register at the time of interrupt.
293 */
294void ehci_hc_interrupt(hcd_t *hcd, uint32_t status)
295{
296 assert(hcd);
297 hc_t *instance = hcd->driver.data;
298 status = EHCI_RD(status);
299 assert(instance);
300 if (status & USB_STS_PORT_CHANGE_FLAG) {
301 ehci_rh_interrupt(&instance->rh);
302 }
303 if (status & USB_STS_ASYNC_SCHED_FLAG) {
304 fibril_condvar_signal(&instance->async_doorbell);
305 }
306}
307
308/** EHCI hw initialization routine.
309 *
310 * @param[in] instance EHCI hc driver structure.
311 */
312void hc_start(hc_t *instance)
313{
314 assert(instance);
315 /* Turn of the HC if it's running, Reseting a running device is
316 * undefined */
317 if (!(EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG)) {
318 /* disable all interrupts */
319 EHCI_WR(instance->registers->usbintr, 0);
320 /* ack all interrupts */
321 EHCI_WR(instance->registers->usbsts, 0x3f);
322 /* Stop HC hw */
323 EHCI_WR(instance->registers->usbcmd, 0);
324 /* Wait until hc is halted */
325 while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0) {
326 async_usleep(1);
327 }
328 usb_log_info("EHCI turned off.\n");
329 } else {
330 usb_log_info("EHCI was not running.\n");
331 }
332
333 /* Hw initialization sequence, see page 53 (pdf 63) */
334 EHCI_SET(instance->registers->usbcmd, USB_CMD_HC_RESET_FLAG);
335 while (EHCI_RD(instance->registers->usbcmd) & USB_CMD_HC_RESET_FLAG) {
336 async_usleep(1);
337 }
338 /* Enable interrupts */
339 EHCI_WR(instance->registers->usbintr, USB_INTR_PORT_CHANGE_FLAG | USB_INTR_IRQ_FLAG | USB_INTR_ASYNC_ADVANCE_FLAG);
340 /* Use the lowest 4G segment */
341 EHCI_WR(instance->registers->ctrldssegment, 0);
342 /* Set periodic list */
343 assert(instance->periodic_list_base);
344 const uintptr_t phys_base =
345 addr_to_phys((void*)instance->periodic_list_base);
346 assert((phys_base & USB_PERIODIC_LIST_BASE_MASK) == phys_base);
347 EHCI_WR(instance->registers->periodiclistbase, phys_base);
348
349 /* start hc and get all ports */
350 EHCI_SET(instance->registers->usbcmd, USB_CMD_RUN_FLAG);
351 EHCI_SET(instance->registers->configflag, USB_CONFIG_FLAG_FLAG);
352
353#if 0
354 /*
355 * TURN OFF EHCI FOR NOW
356 */
357 usb_log_debug("USBCMD value: %x.\n",
358 EHCI_RD(instance->registers->usbcmd));
359 if (EHCI_RD(instance->registers->usbcmd) & USB_CMD_RUN_FLAG) {
360 /* disable all interrupts */
361 EHCI_WR(instance->registers->usbintr, 0);
362 /* ack all interrupts */
363 EHCI_WR(instance->registers->usbsts, 0x3f);
364 /* release RH ports */
365 EHCI_WR(instance->registers->configflag, 0);
366 EHCI_WR(instance->registers->usbcmd, 0);
367 /* Wait until hc is halted */
368 while ((EHCI_RD(instance->registers->usbsts) & USB_STS_HC_HALTED_FLAG) == 0);
369 usb_log_info("EHCI turned off.\n");
370 } else {
371 usb_log_info("EHCI was not running.\n");
372 }
373#endif
374 usb_log_debug("Registers: \n"
375 "\t USBCMD(%p): %x(0x00080000 = at least 1ms between interrupts)\n"
376 "\t USBSTS(%p): %x(0x00001000 = HC halted)\n"
377 "\t USBINT(%p): %x(0x0 = no interrupts).\n"
378 "\t CONFIG(%p): %x(0x0 = ports controlled by companion hc).\n",
379 &instance->registers->usbcmd, EHCI_RD(instance->registers->usbcmd),
380 &instance->registers->usbsts, EHCI_RD(instance->registers->usbsts),
381 &instance->registers->usbintr, EHCI_RD(instance->registers->usbintr),
382 &instance->registers->configflag, EHCI_RD(instance->registers->configflag));
383}
384
385/** Initialize memory structures used by the EHCI hcd.
386 *
387 * @param[in] instance EHCI hc driver structure.
388 * @return Error code.
389 */
390int hc_init_memory(hc_t *instance)
391{
392 assert(instance);
393 int ret = endpoint_list_init(&instance->async_list, "ASYNC");
394 if (ret != EOK) {
395 usb_log_error("Failed to setup ASYNC list: %s", str_error(ret));
396 return ret;
397 }
398
399 ret = endpoint_list_init(&instance->int_list, "INT");
400 if (ret != EOK) {
401 usb_log_error("Failed to setup INT list: %s", str_error(ret));
402 endpoint_list_fini(&instance->async_list);
403 return ret;
404 }
405
406 /* Take 1024 periodic list heads, we ignore low mem options */
407 instance->periodic_list_base = get_page();
408 if (!instance->periodic_list_base) {
409 usb_log_error("Failed to get ISO schedule page.");
410 endpoint_list_fini(&instance->async_list);
411 endpoint_list_fini(&instance->int_list);
412 return ENOMEM;
413 }
414 for (unsigned i = 0;
415 i < PAGE_SIZE/sizeof(instance->periodic_list_base[0]); ++i)
416 {
417 /* Disable everything for now */
418 instance->periodic_list_base[i] =
419 LINK_POINTER_QH(instance->int_list.list_head_pa);
420 }
421 return EOK;
422}
423
424/**
425 * @}
426 */
Note: See TracBrowser for help on using the repository browser.