source: mainline/uspace/drv/bus/usb/ohci/hc.c@ 60d3f35

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 60d3f35 was c6f82e5, checked in by Ondřej Hlavatý <aearsis@…>, 8 years ago

libusbhost: do not try to handle the toggle bit in a generic way

  • Property mode set to 100644
File size: 17.6 KB
RevLine 
[41b96b4]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 */
[8486c07]28
[41b96b4]29/** @addtogroup drvusbohcihc
30 * @{
31 */
32/** @file
33 * @brief OHCI Host controller driver routines
34 */
[8486c07]35
[0d4b110]36#include <assert.h>
37#include <async.h>
[41b96b4]38#include <errno.h>
[0d4b110]39#include <macros.h>
40#include <mem.h>
41#include <stdlib.h>
[41b96b4]42#include <str_error.h>
[8d2dd7f2]43#include <stddef.h>
44#include <stdint.h>
[41b96b4]45
46#include <usb/debug.h>
[c6f82e5]47#include <usb/host/utility.h>
[41b96b4]48#include <usb/usb.h>
49
[e6b9182]50#include "ohci_bus.h"
[0d4b110]51#include "ohci_batch.h"
52
53#include "hc.h"
[41b96b4]54
[561112f]55#define OHCI_USED_INTERRUPTS \
56 (I_SO | I_WDH | I_UE | I_RHSC)
[1ecc5de]57
[d57122c]58static const irq_pio_range_t ohci_pio_ranges[] = {
59 {
[8486c07]60 .base = 0,
[d57122c]61 .size = sizeof(ohci_regs_t)
62 }
63};
64
65static const irq_cmd_t ohci_irq_commands[] = {
[8486c07]66 {
67 .cmd = CMD_PIO_READ_32,
68 .dstarg = 1,
69 .addr = NULL
70 },
71 {
72 .cmd = CMD_AND,
73 .srcarg = 1,
74 .dstarg = 2,
[ea8b91d]75 .value = 0
[8486c07]76 },
77 {
78 .cmd = CMD_PREDICATE,
79 .srcarg = 2,
80 .value = 2
81 },
82 {
83 .cmd = CMD_PIO_WRITE_A_32,
84 .srcarg = 1,
85 .addr = NULL
86 },
87 {
88 .cmd = CMD_ACCEPT
89 }
[1ecc5de]90};
91
[6b6e3ed3]92static int hc_init_transfer_lists(hc_t *instance);
[344925c]93static int hc_init_memory(hc_t *instance);
[76fbd9a]94
[d57122c]95/** Generate IRQ code.
96 * @param[out] ranges PIO ranges buffer.
97 * @param[in] ranges_size Size of the ranges buffer (bytes).
98 * @param[out] cmds Commands buffer.
99 * @param[in] cmds_size Size of the commands buffer (bytes).
[ba4a03a5]100 * @param[in] hw_res Device's resources.
[1cb4f05]101 *
102 * @return Error code.
103 */
[32fb6bce]104int hc_gen_irq_code(irq_code_t *code, hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
[1cb4f05]105{
[6210a333]106 assert(code);
[ba4a03a5]107 assert(hw_res);
108
109 if (hw_res->irqs.count != 1 || hw_res->mem_ranges.count != 1)
110 return EINVAL;
111
112 const addr_range_t regs = hw_res->mem_ranges.ranges[0];
113
114 if (RNGSZ(regs) < sizeof(ohci_regs_t))
[1cb4f05]115 return EOVERFLOW;
116
[6210a333]117 code->ranges = malloc(sizeof(ohci_pio_ranges));
118 if (code->ranges == NULL)
119 return ENOMEM;
[1cb4f05]120
[6210a333]121 code->cmds = malloc(sizeof(ohci_irq_commands));
122 if (code->cmds == NULL) {
123 free(code->ranges);
124 return ENOMEM;
125 }
126
127 code->rangecount = ARRAY_SIZE(ohci_pio_ranges);
128 code->cmdcount = ARRAY_SIZE(ohci_irq_commands);
129
130 memcpy(code->ranges, ohci_pio_ranges, sizeof(ohci_pio_ranges));
[ba4a03a5]131 code->ranges[0].base = RNGABS(regs);
[6210a333]132
133 memcpy(code->cmds, ohci_irq_commands, sizeof(ohci_irq_commands));
[ba4a03a5]134 ohci_regs_t *registers = (ohci_regs_t *) RNGABSPTR(regs);
[6210a333]135 code->cmds[0].addr = (void *) &registers->interrupt_status;
136 code->cmds[3].addr = (void *) &registers->interrupt_status;
137 OHCI_WR(code->cmds[1].value, OHCI_USED_INTERRUPTS);
[1cb4f05]138
[a1732929]139 usb_log_debug("Memory mapped regs at %p (size %zu), IRQ %d.",
[ba4a03a5]140 RNGABSPTR(regs), RNGSZ(regs), hw_res->irqs.irqs[0]);
141
142 return hw_res->irqs.irqs[0];
[1cb4f05]143}
[76fbd9a]144
[02cacce]145/** Initialize OHCI hc driver structure
146 *
147 * @param[in] instance Memory place for the structure.
[7813516]148 * @param[in] regs Device's resources
[02cacce]149 * @param[in] interrupts True if w interrupts should be used
150 * @return Error code
151 */
[32fb6bce]152int hc_add(hc_device_t *hcd, const hw_res_list_parsed_t *hw_res)
[41b96b4]153{
[32fb6bce]154 hc_t *instance = hcd_to_hc(hcd);
[7813516]155 assert(hw_res);
156 if (hw_res->mem_ranges.count != 1 ||
157 hw_res->mem_ranges.ranges[0].size < sizeof(ohci_regs_t))
158 return EINVAL;
[1cb4f05]159
[7813516]160 int ret = pio_enable_range(&hw_res->mem_ranges.ranges[0],
161 (void **) &instance->registers);
[6340a6ff]162 if (ret != EOK) {
[a1732929]163 usb_log_error("Failed to gain access to registers: %s.",
[6340a6ff]164 str_error(ret));
165 return ret;
166 }
[a1732929]167 usb_log_debug("Device registers at %" PRIx64 " (%zuB) accessible.",
[7813516]168 hw_res->mem_ranges.ranges[0].address.absolute,
169 hw_res->mem_ranges.ranges[0].size);
[c2be0e5]170
[d60115a]171 list_initialize(&instance->pending_endpoints);
[6340a6ff]172 fibril_mutex_initialize(&instance->guard);
[e7bc999]173
[8790650]174 ret = hc_init_memory(instance);
[6340a6ff]175 if (ret != EOK) {
[a1732929]176 usb_log_error("Failed to create OHCI memory structures: %s.",
[6340a6ff]177 str_error(ret));
[58563585]178 // TODO: We should disable pio access here
[6340a6ff]179 return ret;
180 }
[2c617b0]181
[8627377]182 return EOK;
[a6d1bc1]183}
[76fbd9a]184
[7813516]185/** Safely dispose host controller internal structures
186 *
187 * @param[in] instance Host controller structure to use.
188 */
[32fb6bce]189int hc_gone(hc_device_t *instance)
[7813516]190{
191 assert(instance);
192 /* TODO: implement*/
[32fb6bce]193 return ENOTSUP;
194}
[7813516]195
[57e06ef]196void hc_enqueue_endpoint(hc_t *instance, const endpoint_t *ep)
[620c710]197{
[57e06ef]198 assert(instance);
199 assert(ep);
200
[620c710]201 endpoint_list_t *list = &instance->lists[ep->transfer_type];
202 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
[57e06ef]203 assert(list);
204 assert(ohci_ep);
205
[620c710]206 /* Enqueue ep */
207 switch (ep->transfer_type) {
208 case USB_TRANSFER_CONTROL:
[bfc5c9dd]209 OHCI_CLR(instance->registers->control, C_CLE);
[620c710]210 endpoint_list_add_ep(list, ohci_ep);
[bfc5c9dd]211 OHCI_WR(instance->registers->control_current, 0);
212 OHCI_SET(instance->registers->control, C_CLE);
[620c710]213 break;
214 case USB_TRANSFER_BULK:
[bfc5c9dd]215 OHCI_CLR(instance->registers->control, C_BLE);
[f974519]216 endpoint_list_add_ep(list, ohci_ep);
[bfc5c9dd]217 OHCI_WR(instance->registers->bulk_current, 0);
218 OHCI_SET(instance->registers->control, C_BLE);
[620c710]219 break;
220 case USB_TRANSFER_ISOCHRONOUS:
221 case USB_TRANSFER_INTERRUPT:
[bfc5c9dd]222 OHCI_CLR(instance->registers->control, C_PLE | C_IE);
[f974519]223 endpoint_list_add_ep(list, ohci_ep);
[bfc5c9dd]224 OHCI_SET(instance->registers->control, C_PLE | C_IE);
[620c710]225 break;
226 }
227}
[76fbd9a]228
[57e06ef]229void hc_dequeue_endpoint(hc_t *instance, const endpoint_t *ep)
[620c710]230{
[57e06ef]231 assert(instance);
232 assert(ep);
233
[620c710]234 /* Dequeue ep */
235 endpoint_list_t *list = &instance->lists[ep->transfer_type];
236 ohci_endpoint_t *ohci_ep = ohci_endpoint_get(ep);
[57e06ef]237
238 assert(list);
239 assert(ohci_ep);
[620c710]240 switch (ep->transfer_type) {
241 case USB_TRANSFER_CONTROL:
[bfc5c9dd]242 OHCI_CLR(instance->registers->control, C_CLE);
[620c710]243 endpoint_list_remove_ep(list, ohci_ep);
[bfc5c9dd]244 OHCI_WR(instance->registers->control_current, 0);
245 OHCI_SET(instance->registers->control, C_CLE);
[620c710]246 break;
247 case USB_TRANSFER_BULK:
[bfc5c9dd]248 OHCI_CLR(instance->registers->control, C_BLE);
[620c710]249 endpoint_list_remove_ep(list, ohci_ep);
[bfc5c9dd]250 OHCI_WR(instance->registers->bulk_current, 0);
251 OHCI_SET(instance->registers->control, C_BLE);
[620c710]252 break;
253 case USB_TRANSFER_ISOCHRONOUS:
254 case USB_TRANSFER_INTERRUPT:
[bfc5c9dd]255 OHCI_CLR(instance->registers->control, C_PLE | C_IE);
[620c710]256 endpoint_list_remove_ep(list, ohci_ep);
[bfc5c9dd]257 OHCI_SET(instance->registers->control, C_PLE | C_IE);
[620c710]258 break;
259 default:
260 break;
261 }
262}
[76fbd9a]263
[32fb6bce]264int ohci_hc_status(bus_t *bus_base, uint32_t *status)
[e26a9d95]265{
[32fb6bce]266 assert(bus_base);
[e26a9d95]267 assert(status);
268
[32fb6bce]269 ohci_bus_t *bus = (ohci_bus_t *) bus_base;
270 hc_t *hc = bus->hc;
271 assert(hc);
272
273 if (hc->registers){
274 *status = OHCI_RD(hc->registers->interrupt_status);
275 OHCI_WR(hc->registers->interrupt_status, *status);
[e26a9d95]276 }
277 return EOK;
278}
279
[02cacce]280/** Add USB transfer to the schedule.
281 *
[fccf289]282 * @param[in] hcd HCD driver structure.
[02cacce]283 * @param[in] batch Batch representing the transfer.
284 * @return Error code.
285 */
[32fb6bce]286int ohci_hc_schedule(usb_transfer_batch_t *batch)
[41b96b4]287{
[32fb6bce]288 assert(batch);
289
290 ohci_bus_t *bus = (ohci_bus_t *) endpoint_get_bus(batch->ep);
291 hc_t *hc = bus->hc;
292 assert(hc);
[9ff5ff82]293
[02cacce]294 /* Check for root hub communication */
[32fb6bce]295 if (batch->target.address == ohci_rh_get_address(&hc->rh)) {
[a1732929]296 usb_log_debug("OHCI root hub request.");
[32fb6bce]297 return ohci_rh_schedule(&hc->rh, batch);
[41b96b4]298 }
[9c10e51]299 ohci_transfer_batch_t *ohci_batch = ohci_transfer_batch_get(batch);
300 if (!ohci_batch)
301 return ENOMEM;
[7013b14]302
[5fd9c30]303 const int err = ohci_transfer_batch_prepare(ohci_batch);
304 if (err)
305 return err;
306
[d60115a]307 endpoint_t *ep = batch->ep;
308 ohci_endpoint_t * const ohci_ep = ohci_endpoint_get(ep);
309
310 /* creating local reference */
311 endpoint_add_ref(ep);
312
313 fibril_mutex_lock(&ep->guard);
314 endpoint_activate_locked(ep, batch);
[9c10e51]315 ohci_transfer_batch_commit(ohci_batch);
[d60115a]316 fibril_mutex_unlock(&ep->guard);
[02cacce]317
318 /* Control and bulk schedules need a kick to start working */
319 switch (batch->ep->transfer_type)
320 {
[9ff5ff82]321 case USB_TRANSFER_CONTROL:
[32fb6bce]322 OHCI_SET(hc->registers->command_status, CS_CLF);
[9ff5ff82]323 break;
324 case USB_TRANSFER_BULK:
[32fb6bce]325 OHCI_SET(hc->registers->command_status, CS_BLF);
[9ff5ff82]326 break;
327 default:
328 break;
329 }
[d60115a]330
331 fibril_mutex_lock(&hc->guard);
332 list_append(&ohci_ep->pending_link, &hc->pending_endpoints);
[32fb6bce]333 fibril_mutex_unlock(&hc->guard);
[d60115a]334
[4c28d17]335 return EOK;
[41b96b4]336}
[76fbd9a]337
[02cacce]338/** Interrupt handling routine
339 *
[fccf289]340 * @param[in] hcd HCD driver structure.
[02cacce]341 * @param[in] status Value of the status register at the time of interrupt.
342 */
[32fb6bce]343void ohci_hc_interrupt(bus_t *bus_base, uint32_t status)
[41b96b4]344{
[32fb6bce]345 assert(bus_base);
346
347 ohci_bus_t *bus = (ohci_bus_t *) bus_base;
348 hc_t *hc = bus->hc;
349 assert(hc);
350
[d1ca752]351 status = OHCI_RD(status);
[32fb6bce]352 assert(hc);
[561112f]353 if ((status & ~I_SF) == 0) /* ignore sof status */
[eaf1e3d]354 return;
[a1732929]355 usb_log_debug2("OHCI(%p) interrupt: %x.", hc, status);
[561112f]356 if (status & I_RHSC)
[32fb6bce]357 ohci_rh_interrupt(&hc->rh);
[7d6a676]358
[561112f]359 if (status & I_WDH) {
[32fb6bce]360 fibril_mutex_lock(&hc->guard);
[a1732929]361 usb_log_debug2("HCCA: %p-%#" PRIx32 " (%p).", hc->hcca,
[32fb6bce]362 OHCI_RD(hc->registers->hcca),
363 (void *) addr_to_phys(hc->hcca));
[a1732929]364 usb_log_debug2("Periodic current: %#" PRIx32 ".",
[32fb6bce]365 OHCI_RD(hc->registers->periodic_current));
[eaf1e3d]366
[d60115a]367 list_foreach_safe(hc->pending_endpoints, current, next) {
368 ohci_endpoint_t *ep
369 = list_get_instance(current, ohci_endpoint_t, pending_link);
370
371 fibril_mutex_lock(&ep->base.guard);
372 ohci_transfer_batch_t *batch
373 = ohci_transfer_batch_get(ep->base.active_batch);
374 assert(batch);
[7013b14]375
[5fd9c30]376 if (ohci_transfer_batch_check_completed(batch)) {
[d60115a]377 endpoint_deactivate_locked(&ep->base);
[d6522dd]378 list_remove(current);
[d60115a]379 endpoint_del_ref(&ep->base);
[c6f82e5]380 hc_reset_toggles(&batch->base, &ohci_ep_toggle_reset);
[5fd9c30]381 usb_transfer_batch_finish(&batch->base);
[7013b14]382 }
[d60115a]383 fibril_mutex_unlock(&ep->base.guard);
[eaf1e3d]384 }
[32fb6bce]385 fibril_mutex_unlock(&hc->guard);
[4c28d17]386 }
[68b9f148]387
388 if (status & I_UE) {
[a1732929]389 usb_log_fatal("Error like no other!");
[32fb6bce]390 hc_start(&hc->base);
[68b9f148]391 }
392
[41b96b4]393}
[76fbd9a]394
[02cacce]395/** Turn off any (BIOS)driver that might be in control of the device.
[78ab6d4]396 *
397 * This function implements routines described in chapter 5.1.1.3 of the OHCI
398 * specification (page 40, pdf page 54).
[02cacce]399 *
400 * @param[in] instance OHCI hc driver structure.
401 */
[32fb6bce]402int hc_gain_control(hc_device_t *hcd)
[2c617b0]403{
[32fb6bce]404 hc_t *instance = hcd_to_hc(hcd);
[78ab6d4]405
[a1732929]406 usb_log_debug("Requesting OHCI control.");
[bfc5c9dd]407 if (OHCI_RD(instance->registers->revision) & R_LEGACY_FLAG) {
[78ab6d4]408 /* Turn off legacy emulation, it should be enough to zero
409 * the lowest bit, but it caused problems. Thus clear all
410 * except GateA20 (causes restart on some hw).
411 * See page 145 of the specs for details.
412 */
413 volatile uint32_t *ohci_emulation_reg =
414 (uint32_t*)((char*)instance->registers + LEGACY_REGS_OFFSET);
[a1732929]415 usb_log_debug("OHCI legacy register %p: %x.",
[bfc5c9dd]416 ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
[78ab6d4]417 /* Zero everything but A20State */
[58563585]418 // TODO: should we ack interrupts before doing this?
[bfc5c9dd]419 OHCI_CLR(*ohci_emulation_reg, ~0x100);
[78ab6d4]420 usb_log_debug(
[a1732929]421 "OHCI legacy register (should be 0 or 0x100) %p: %x.",
[bfc5c9dd]422 ohci_emulation_reg, OHCI_RD(*ohci_emulation_reg));
[78ab6d4]423 }
[112d159]424
[2c617b0]425 /* Interrupt routing enabled => smm driver is active */
[bfc5c9dd]426 if (OHCI_RD(instance->registers->control) & C_IR) {
[a1732929]427 usb_log_debug("SMM driver: request ownership change.");
[58563585]428 // TODO: should we ack interrupts before doing this?
[bfc5c9dd]429 OHCI_SET(instance->registers->command_status, CS_OCR);
[78ab6d4]430 /* Hope that SMM actually knows its stuff or we can hang here */
[f5bfd98]431 while (OHCI_RD(instance->registers->control) & C_IR) {
[2c617b0]432 async_usleep(1000);
433 }
[a1732929]434 usb_log_info("SMM driver: Ownership taken.");
[78ab6d4]435 C_HCFS_SET(instance->registers->control, C_HCFS_RESET);
[5d07f54]436 async_usleep(50000);
[32fb6bce]437 return EOK;
[2c617b0]438 }
[8486c07]439
[78ab6d4]440 const unsigned hc_status = C_HCFS_GET(instance->registers->control);
[2c617b0]441 /* Interrupt routing disabled && status != USB_RESET => BIOS active */
442 if (hc_status != C_HCFS_RESET) {
[a1732929]443 usb_log_debug("BIOS driver found.");
[2c617b0]444 if (hc_status == C_HCFS_OPERATIONAL) {
[a1732929]445 usb_log_info("BIOS driver: HC operational.");
[32fb6bce]446 return EOK;
[2c617b0]447 }
[bfc5c9dd]448 /* HC is suspended assert resume for 20ms */
[78ab6d4]449 C_HCFS_SET(instance->registers->control, C_HCFS_RESUME);
[2c617b0]450 async_usleep(20000);
[a1732929]451 usb_log_info("BIOS driver: HC resumed.");
[32fb6bce]452 return EOK;
[2c617b0]453 }
454
455 /* HC is in reset (hw startup) => no other driver
456 * maintain reset for at least the time specified in USB spec (50 ms)*/
[a1732929]457 usb_log_debug("Host controller found in reset state.");
[2c617b0]458 async_usleep(50000);
[32fb6bce]459 return EOK;
[2c617b0]460}
[76fbd9a]461
[02cacce]462/** OHCI hw initialization routine.
463 *
464 * @param[in] instance OHCI hc driver structure.
465 */
[32fb6bce]466int hc_start(hc_device_t *hcd)
[2c617b0]467{
[32fb6bce]468 hc_t *instance = hcd_to_hc(hcd);
[e4d7363]469 ohci_rh_init(&instance->rh, instance->registers, "ohci rh");
470
[112d159]471 /* OHCI guide page 42 */
[2c617b0]472 assert(instance);
[a1732929]473 usb_log_debug2("Started hc initialization routine.");
[112d159]474
475 /* Save contents of fm_interval register */
[bfc5c9dd]476 const uint32_t fm_interval = OHCI_RD(instance->registers->fm_interval);
[a1732929]477 usb_log_debug2("Old value of HcFmInterval: %x.", fm_interval);
[344925c]478
[112d159]479 /* Reset hc */
[a1732929]480 usb_log_debug2("HC reset.");
[112d159]481 size_t time = 0;
[bfc5c9dd]482 OHCI_WR(instance->registers->command_status, CS_HCR);
483 while (OHCI_RD(instance->registers->command_status) & CS_HCR) {
[112d159]484 async_usleep(10);
485 time += 10;
486 }
[a1732929]487 usb_log_debug2("HC reset complete in %zu us.", time);
[344925c]488
[112d159]489 /* Restore fm_interval */
[bfc5c9dd]490 OHCI_WR(instance->registers->fm_interval, fm_interval);
491 assert((OHCI_RD(instance->registers->command_status) & CS_HCR) == 0);
[344925c]492
[2c617b0]493 /* hc is now in suspend state */
[a1732929]494 usb_log_debug2("HC should be in suspend state(%x).",
[bfc5c9dd]495 OHCI_RD(instance->registers->control));
[344925c]496
[78d4e1f]497 /* Use HCCA */
[bfc5c9dd]498 OHCI_WR(instance->registers->hcca, addr_to_phys(instance->hcca));
[78d4e1f]499
500 /* Use queues */
[bfc5c9dd]501 OHCI_WR(instance->registers->bulk_head,
502 instance->lists[USB_TRANSFER_BULK].list_head_pa);
[a1732929]503 usb_log_debug2("Bulk HEAD set to: %p (%#" PRIx32 ").",
[5a2c42b]504 instance->lists[USB_TRANSFER_BULK].list_head,
505 instance->lists[USB_TRANSFER_BULK].list_head_pa);
[78d4e1f]506
[bfc5c9dd]507 OHCI_WR(instance->registers->control_head,
508 instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
[a1732929]509 usb_log_debug2("Control HEAD set to: %p (%#" PRIx32 ").",
[5a2c42b]510 instance->lists[USB_TRANSFER_CONTROL].list_head,
511 instance->lists[USB_TRANSFER_CONTROL].list_head_pa);
[78d4e1f]512
[112d159]513 /* Enable queues */
[65eac7b]514 OHCI_SET(instance->registers->control, (C_PLE | C_IE | C_CLE | C_BLE));
[a1732929]515 usb_log_debug("Queues enabled(%x).",
[65eac7b]516 OHCI_RD(instance->registers->control));
[112d159]517
[561112f]518 /* Enable interrupts */
[32fb6bce]519 if (instance->base.irq_cap >= 0) {
[a5361fb]520 OHCI_WR(instance->registers->interrupt_enable,
521 OHCI_USED_INTERRUPTS);
[a1732929]522 usb_log_debug("Enabled interrupts: %x.",
[a5361fb]523 OHCI_RD(instance->registers->interrupt_enable));
524 OHCI_WR(instance->registers->interrupt_enable, I_MI);
525 }
[112d159]526
527 /* Set periodic start to 90% */
[bfc5c9dd]528 const uint32_t frame_length =
529 (fm_interval >> FMI_FI_SHIFT) & FMI_FI_MASK;
530 OHCI_WR(instance->registers->periodic_start,
531 ((frame_length / 10) * 9) & PS_MASK << PS_SHIFT);
[a1732929]532 usb_log_debug2("All periodic start set to: %x(%u - 90%% of %d).",
[bfc5c9dd]533 OHCI_RD(instance->registers->periodic_start),
534 OHCI_RD(instance->registers->periodic_start), frame_length);
[78ab6d4]535 C_HCFS_SET(instance->registers->control, C_HCFS_OPERATIONAL);
[a1732929]536 usb_log_debug("OHCI HC up and running (ctl_reg=0x%x).",
[bfc5c9dd]537 OHCI_RD(instance->registers->control));
[32fb6bce]538
539 return EOK;
[2c617b0]540}
[76fbd9a]541
[02cacce]542/** Initialize schedule queues
543 *
544 * @param[in] instance OHCI hc driver structure
545 * @return Error code
546 */
[6b6e3ed3]547int hc_init_transfer_lists(hc_t *instance)
548{
549 assert(instance);
[5a2c42b]550#define SETUP_ENDPOINT_LIST(type) \
[344925c]551do { \
[5a2c42b]552 const char *name = usb_str_transfer_type(type); \
[6340a6ff]553 const int ret = endpoint_list_init(&instance->lists[type], name); \
[6b6e3ed3]554 if (ret != EOK) { \
[a1732929]555 usb_log_error("Failed to setup %s endpoint list: %s.", \
[1cb4f05]556 name, str_error(ret)); \
[68b9f148]557 endpoint_list_fini(&instance->lists[USB_TRANSFER_ISOCHRONOUS]);\
[5a2c42b]558 endpoint_list_fini(&instance->lists[USB_TRANSFER_INTERRUPT]); \
559 endpoint_list_fini(&instance->lists[USB_TRANSFER_CONTROL]); \
560 endpoint_list_fini(&instance->lists[USB_TRANSFER_BULK]); \
[70c85320]561 return ret; \
[344925c]562 } \
563} while (0)
[6b6e3ed3]564
[5a2c42b]565 SETUP_ENDPOINT_LIST(USB_TRANSFER_ISOCHRONOUS);
566 SETUP_ENDPOINT_LIST(USB_TRANSFER_INTERRUPT);
567 SETUP_ENDPOINT_LIST(USB_TRANSFER_CONTROL);
568 SETUP_ENDPOINT_LIST(USB_TRANSFER_BULK);
569#undef SETUP_ENDPOINT_LIST
570 endpoint_list_set_next(&instance->lists[USB_TRANSFER_INTERRUPT],
571 &instance->lists[USB_TRANSFER_ISOCHRONOUS]);
[6b6e3ed3]572
573 return EOK;
574}
[76fbd9a]575
[02cacce]576/** Initialize memory structures used by the OHCI hcd.
577 *
578 * @param[in] instance OHCI hc driver structure.
579 * @return Error code.
580 */
[344925c]581int hc_init_memory(hc_t *instance)
582{
583 assert(instance);
[5d07f54]584
[acdb5bac]585 memset(&instance->rh, 0, sizeof(instance->rh));
[8790650]586 /* Init queues */
[32fb6bce]587 int ret = hc_init_transfer_lists(instance);
[8953514]588 if (ret != EOK) {
589 return ret;
590 }
[344925c]591
[8790650]592 /*Init HCCA */
[f8dfb40]593 instance->hcca = hcca_get();
[344925c]594 if (instance->hcca == NULL)
595 return ENOMEM;
[a1732929]596 usb_log_debug2("OHCI HCCA initialized at %p.", instance->hcca);
[344925c]597
[1b90e90]598 for (unsigned i = 0; i < HCCA_INT_EP_COUNT; ++i) {
599 hcca_set_int_ep(instance->hcca, i,
[65eac7b]600 instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
[344925c]601 }
[a1732929]602 usb_log_debug2("Interrupt HEADs set to: %p (%#" PRIx32 ").",
[5a2c42b]603 instance->lists[USB_TRANSFER_INTERRUPT].list_head,
604 instance->lists[USB_TRANSFER_INTERRUPT].list_head_pa);
[344925c]605
[32fb6bce]606 if ((ret = ohci_bus_init(&instance->bus, instance))) {
607 usb_log_error("HC(%p): Failed to setup bus : %s",
608 instance, str_error(ret));
609 return ret;
610 }
611
612 hc_device_setup(&instance->base, (bus_t *) &instance->bus);
613
[344925c]614 return EOK;
615}
[1ecc5de]616
[41b96b4]617/**
618 * @}
619 */
Note: See TracBrowser for help on using the repository browser.