source: mainline/uspace/drv/bus/usb/ohci/hc.c@ 7c3fb9b

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 7c3fb9b was 7c3fb9b, checked in by Jiri Svoboda <jiri@…>, 7 years ago

Fix block comment formatting (ccheck).

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