1 /*****************************************************************************
2 * Copyright (c) 2013 IBM Corporation
4 * This program and the accompanying materials
5 * are made available under the terms of the BSD License
6 * which accompanies this distribution, and is available at
7 * http://www.opensource.org/licenses/bsd-license.php
10 * IBM Corporation - initial implementation
11 *****************************************************************************/
23 #define dprintf(_x ...) do { printf("%s: ", __func__); printf(_x); } while (0)
25 #define dprintf(_x ...)
28 static void dump_xhci_regs(struct xhci_hcd *xhcd)
31 struct xhci_cap_regs *cap;
32 struct xhci_op_regs *op;
33 struct xhci_run_regs *run;
40 dprintf(" - CAPLENGTH %02X\n", read_reg8 (&cap->caplength));
41 dprintf(" - HCIVERSION %04X\n", read_reg16(&cap->hciversion));
42 dprintf(" - HCSPARAMS1 %08X\n", read_reg32(&cap->hcsparams1));
43 dprintf(" - HCSPARAMS2 %08X\n", read_reg32(&cap->hcsparams2));
44 dprintf(" - HCSPARAMS3 %08X\n", read_reg32(&cap->hcsparams3));
45 dprintf(" - HCCPARAMS %08X\n", read_reg32(&cap->hccparams));
46 dprintf(" - DBOFF %08X\n", read_reg32(&cap->dboff));
47 dprintf(" - RTSOFF %08X\n", read_reg32(&cap->rtsoff));
50 dprintf(" - USBCMD %08X\n", read_reg32(&op->usbcmd));
51 dprintf(" - USBSTS %08X\n", read_reg32(&op->usbsts));
52 dprintf(" - PAGESIZE %08X\n", read_reg32(&op->pagesize));
53 dprintf(" - DNCTRL %08X\n", read_reg32(&op->dnctrl));
54 dprintf(" - CRCR %016llX\n", read_reg64(&op->crcr));
55 dprintf(" - DCBAAP %016llX\n", read_reg64(&op->dcbaap));
56 dprintf(" - CONFIG %08X\n", read_reg32(&op->config));
59 dprintf(" - MFINDEX %08X\n", read_reg32(&run->mfindex));
64 static void print_port_status(struct xhci_port_regs *prs)
68 uint32_t CCS, PED, PP, PLS, i, PR = 0;
70 portsc = read_reg32(&prs->portsc);
71 dprintf("portsc %08x portpmsc %08x portli %08x\n",
73 read_reg32(&prs->portpmsc),
74 read_reg32(&prs->portli));
76 if (portsc & PORTSC_CCS) {
80 if (portsc & PORTSC_PED) {
84 if (portsc & PORTSC_OCA)
86 if (portsc & PORTSC_PR)
88 PLS = (portsc & PORTSC_PLS_MASK) >> 5;
89 printf("PLS:%d ", PLS);
90 if (portsc & PORTSC_PP) {
94 printf("PS:%d ", (portsc & PORTSC_PS_MASK) >> 10);
95 printf("PIC:%d ", (portsc & PORTSC_PIC_MASK) >> 14);
96 if (portsc & PORTSC_LWS)
98 if (portsc & PORTSC_CSC)
100 if (portsc & PORTSC_PEC)
102 if (portsc & PORTSC_WRC)
104 if (portsc & PORTSC_OCC)
106 if (portsc & PORTSC_PRC)
108 if (portsc & PORTSC_PLC)
110 if (portsc & PORTSC_CEC)
112 if (portsc & PORTSC_CAS)
114 if (portsc & PORTSC_WCE)
116 if (portsc & PORTSC_WDE)
118 if (portsc & PORTSC_WOE)
120 if (portsc & PORTSC_DR)
122 if (portsc & PORTSC_WPR)
126 for (i = 0 ; i < (sizeof(ps_array_usb3)/sizeof(struct port_state)); i++) {
127 if (PP == ps_array_usb3[i].PP) {
128 if (CCS == ps_array_usb3[i].CCS) {
129 if (PED == ps_array_usb3[i].PED) {
130 if (PR == ps_array_usb3[i].PR) {
131 dprintf("%s - PLS %d\n", ps_array_usb3[i].state, PLS);
142 static inline bool xhci_is_hc_ready(uint32_t *usbsts)
144 return !(read_reg32(usbsts) & XHCI_USBSTS_CNR);
147 static inline bool xhci_wait_for_cnr(uint32_t *usbsts)
150 * Note: The xHC should halt within 16 ms. of software clearing the
152 * Give some more time... 32ms
155 dprintf("Waiting for Controller ready ..");
156 while (!xhci_is_hc_ready(usbsts)) {
160 dprintf(" failed %08X\n", read_reg32(usbsts));
169 static bool xhci_hcd_set_runstop(struct xhci_op_regs *op, bool run_req)
173 dprintf("Request %s\n", run_req ? "RUN" : "STOP");
174 if (!xhci_is_hc_ready(&op->usbsts)) {
175 dprintf("Controller not ready\n");
179 reg = read_reg32(&op->usbcmd);
184 dprintf("writing %08X\n", reg);
185 write_reg32(&op->usbcmd, reg);
187 xhci_wait_for_cnr(&op->usbsts);
191 static bool xhci_hcd_reset(struct xhci_op_regs *op)
195 /* Check if the controller is halted, else halt it */
196 if (!(read_reg32(&op->usbsts) & XHCI_USBSTS_HCH)) {
197 dprintf("HCHalted not set\n");
198 if (!xhci_hcd_set_runstop(op, false))
202 if (read_reg32(&op->usbsts) & XHCI_USBSTS_CNR) {
203 dprintf("Controller not ready\n");
207 reg = read_reg32(&op->usbcmd) | XHCI_USBCMD_HCRST;
208 /* Ready to Reset the controller now */
209 write_reg32(&op->usbcmd, reg);
210 xhci_wait_for_cnr(&op->usbsts);
214 static void xhci_handle_cmd_completion(struct xhci_hcd *xhcd,
215 struct xhci_event_trb *event)
217 uint32_t flags, slot_id, status;
219 status = le32_to_cpu(event->status);
220 flags = le32_to_cpu(event->flags);
221 slot_id = TRB_SLOT_ID(flags);
222 if (TRB_STATUS(status) == COMP_SUCCESS)
223 xhcd->slot_id = slot_id;
228 static uint64_t xhci_poll_event(struct xhci_hcd *xhcd,
231 struct xhci_event_trb *event;
232 uint64_t val, retval = 0;
233 uint32_t flags, time;
237 event = (struct xhci_event_trb *)xhcd->ering.deq;
238 flags = le32_to_cpu(event->flags);
240 dprintf("Reading from event ptr %p %08x\n", event, flags);
241 time = SLOF_GetTimer() + USB_TIMEOUT;
243 while ((flags & TRB_CYCLE_STATE) != xhcd->ering.cycle_state) {
245 flags = le32_to_cpu(event->flags);
246 if (time < SLOF_GetTimer())
251 flags = le32_to_cpu(event->flags);
252 switch(TRB_TYPE(flags))
254 case TRB_CMD_COMPLETION:
255 dprintf("CMD Completion\n");
256 xhci_handle_cmd_completion(xhcd, event);
258 case TRB_PORT_STATUS:
259 dprintf("Port status event\n");
261 case TRB_TRANSFER_EVENT:
262 dprintf("XFER event addr %16lx, status %08x, flags %08x\n",
263 le64_to_cpu(event->addr),
264 le32_to_cpu(event->status),
265 le32_to_cpu(event->flags));
268 printf("TRB_TYPE %d\n", TRB_TYPE(flags));
269 dprintf("Event addr %16lx, status %08x, flags %08x state %d\n",
270 le64_to_cpu(event->addr),
271 le32_to_cpu(event->status),
272 flags, xhcd->ering.cycle_state);
275 xhcd->ering.deq = (uint64_t) (event + 1);
276 retval = le64_to_cpu(event->addr);
280 event->flags = cpu_to_le32(xhcd->ering.cycle_state);
282 index = xhcd->ering.deq - (uint64_t)xhcd->ering.trbs;
283 val = xhcd->ering.trbs_dma;
284 val += (index % XHCI_EVENT_TRBS_SIZE);
285 if (!(index % XHCI_EVENT_TRBS_SIZE)) {
286 xhcd->ering.deq = (uint64_t)xhcd->ering.trbs;
287 xhcd->ering.cycle_state = xhcd->ering.cycle_state ? 0 : 1;
288 dprintf("Rounding %d\n", xhcd->ering.cycle_state);
290 dprintf("Update start %x deq %x index %d\n",
291 xhcd->ering.trbs_dma, val, index/sizeof(*event));
292 write_reg64(&xhcd->run_regs->irs[0].erdp, val);
295 return (uint64_t)event;
300 static void xhci_send_cmd(struct xhci_hcd *xhcd, uint32_t field1,
301 uint32_t field2, uint32_t field3, uint32_t field4)
303 struct xhci_db_regs *dbr;
304 struct xhci_command_trb *cmd;
305 uint32_t val, cycle_state;
308 cmd = (struct xhci_command_trb *)xhcd->crseg.enq;
310 cmd->field[0] = cpu_to_le32(field1);
311 cmd->field[1] = cpu_to_le32(field2);
312 cmd->field[2] = cpu_to_le32(field3);
314 val = le32_to_cpu(cmd->field[3]);
315 cycle_state = (val & 0x1) ? 0 : 1;
316 val = field4 | cycle_state;
317 cmd->field[3] = cpu_to_le32(val);
319 dprintf("CMD %016lx val %08x cycle_state %d field1 %08x, field2 %08x, field3 %08x field4 %08x\n",
320 cmd, val, cycle_state,
321 le32_to_cpu(cmd->field[0]),
322 le32_to_cpu(cmd->field[1]),
323 le32_to_cpu(cmd->field[2]),
324 le32_to_cpu(cmd->field[3])
327 /* Ring the doorbell */
328 write_reg32(&dbr->db[0], 0);
329 xhci_poll_event(xhcd, 0);
331 xhcd->crseg.enq = (uint64_t)cmd;
335 static void xhci_send_enable_slot(struct xhci_hcd *xhcd, uint32_t port)
337 uint32_t field1, field2, field3, field4;
342 field4 = TRB_CMD_TYPE(TRB_ENABLE_SLOT);
343 xhci_send_cmd(xhcd, field1, field2, field3, field4);
346 static void xhci_send_addr_device(struct xhci_hcd *xhcd, uint32_t slot_id,
349 uint32_t field1, field2, field3, field4;
351 dprintf("Address device %lx, low %x, high %x\n", dma_in_ctx,
352 TRB_ADDR_LOW(dma_in_ctx),
353 TRB_ADDR_HIGH(dma_in_ctx));
354 field1 = TRB_ADDR_LOW(dma_in_ctx) & ~0xF;
355 field2 = TRB_ADDR_HIGH(dma_in_ctx);
357 field4 = TRB_CMD_TYPE(TRB_ADDRESS_DEV) | TRB_CMD_SLOT_ID(slot_id);
358 xhci_send_cmd(xhcd, field1, field2, field3, field4);
361 static uint32_t xhci_get_epno(struct usb_pipe *pipe)
364 x_epno = pipe->dir | 2 * pipe->epno;
365 dprintf("EPno %d:%d DIR %d\n", pipe->epno, x_epno, pipe->dir);
369 static void xhci_configure_ep(struct xhci_hcd *xhcd, uint32_t slot_id,
372 uint32_t field1, field2, field3, field4;
374 dprintf("Configure EP %lx, low %x, high %x\n", dma_in_ctx,
375 TRB_ADDR_LOW(dma_in_ctx),
376 TRB_ADDR_HIGH(dma_in_ctx));
377 field1 = TRB_ADDR_LOW(dma_in_ctx) & ~0xF;
378 field2 = TRB_ADDR_HIGH(dma_in_ctx);
380 field4 = TRB_CMD_TYPE(TRB_CONFIG_EP) | TRB_CMD_SLOT_ID(slot_id);
381 xhci_send_cmd(xhcd, field1, field2, field3, field4);
384 static void xhci_init_seg(struct xhci_seg *seg, uint32_t size, uint32_t type)
386 struct xhci_link_trb *link;
388 seg->size = size / XHCI_TRB_SIZE;
391 seg->cycle_state = 1;
392 seg->enq = (uint64_t)seg->trbs;
393 seg->deq = (uint64_t)seg->trbs;
394 memset((void *)seg->trbs, 0, size);
396 if (type != TYPE_EVENT) {
397 link =(struct xhci_link_trb *) (seg->trbs + seg->size - 1);
398 link->addr = cpu_to_le64(seg->trbs_dma);
400 link->field3 = cpu_to_le32(0x1 | TRB_CMD_TYPE(TRB_LINK));
405 static bool xhci_alloc_seg(struct xhci_seg *seg, uint32_t size, uint32_t type)
407 seg->trbs = (union xhci_trb *)SLOF_dma_alloc(size);
409 dprintf("Alloc failed\n");
412 xhci_init_seg(seg, size, type);
413 seg->trbs_dma = SLOF_dma_map_in((void *)seg->trbs, size, false);
415 dprintf(" TRBs %016lX TRBS-DMA %016lX\n", seg->trbs, seg->trbs_dma);
419 static void xhci_free_seg(struct xhci_seg *seg, uint32_t size)
422 dprintf(" TRBs %016lX TRBS-DMA %016lX size %x\n", seg->trbs, seg->trbs_dma, size);
423 SLOF_dma_map_out(seg->trbs_dma, (void *)seg->trbs, size);
424 SLOF_dma_free((void *)seg->trbs, size);
426 memset(seg, 0, sizeof(*seg));
429 #define CTX_SIZE(x) ( (x) ? 64 : 32 )
431 static bool xhci_alloc_ctx(struct xhci_ctx *ctx, uint32_t size, uint32_t type)
433 ctx->addr = (uint8_t *)SLOF_dma_alloc(size);
435 dprintf("Alloc failed\n");
440 memset((void *)ctx->addr, 0, size);
441 ctx->dma_addr = SLOF_dma_map_in((void *)ctx->addr, size, false);
442 dprintf("ctx %llx, ctx_dma %llx\n", ctx->addr, ctx->dma_addr);
446 static struct xhci_control_ctx *xhci_get_control_ctx(struct xhci_ctx *ctx)
448 if (ctx->type == XHCI_CTX_TYPE_INPUT)
449 return (struct xhci_control_ctx *) ctx->addr;
453 static struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctx *ctx, uint32_t ctx_size)
457 if (ctx->type == XHCI_CTX_TYPE_INPUT)
459 return (struct xhci_slot_ctx *)(ctx->addr + offset);
462 static struct xhci_ep_ctx *xhci_get_ep0_ctx(struct xhci_ctx *ctx, uint32_t ctx_size)
467 if (ctx->type == XHCI_CTX_TYPE_INPUT)
469 return (struct xhci_ep_ctx *)(ctx->addr + offset);
472 static struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctx *ctx, uint32_t ctx_size,
477 offset = ctx_size * epno;
478 if (ctx->type == XHCI_CTX_TYPE_INPUT)
480 return (struct xhci_ep_ctx *)(ctx->addr + offset);
483 static void xhci_free_ctx(struct xhci_ctx *ctx, uint32_t size)
485 SLOF_dma_map_out(ctx->dma_addr, (void *)ctx->addr, size);
486 SLOF_dma_free((void *)ctx->addr, size);
489 static uint32_t usb_control_max_packet(uint32_t speed)
491 uint32_t max_packet = 0;
504 case USB_SUPER_SPEED:
508 /* should not reach here */
509 dprintf("Unknown speed\n");
514 static bool xhci_alloc_dev(struct xhci_hcd *xhcd, uint32_t slot_id, uint32_t port)
517 struct xhci_dev *xdev;
518 struct xhci_slot_ctx *slot;
519 struct xhci_control_ctx *ctrl;
520 struct xhci_ep_ctx *ep0;
521 uint32_t ctx_size, val;
525 ctx_size = CTX_SIZE(xhcd->hcc_csz_64);
526 xdev = &xhcd->xdevs[slot_id];
527 xdev->slot_id = slot_id;
528 xdev->ctx_size = ctx_size;
530 /* 4.3.3 Device Slot initialization */
532 if (!xhci_alloc_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE, XHCI_CTX_TYPE_INPUT)) {
533 dprintf("Failed allocating in_ctx\n");
538 ctrl = xhci_get_control_ctx(&xdev->in_ctx);
539 ctrl->a_flags = cpu_to_le32(0x3); /* A0, A1 */
543 slot = xhci_get_slot_ctx(&xdev->in_ctx, ctx_size);
545 val = LAST_CONTEXT(1) | SLOT_SPEED_SS | (newport << 16); /* FIXME speed, read from PS */
546 slot->field1 = cpu_to_le32(val);
547 slot->field2 = cpu_to_le32(ROOT_HUB_PORT(newport)); /* FIXME how to get port no */
550 if (!xhci_alloc_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE, TYPE_CTRL)) {
551 dprintf("Failed allocating control\n");
556 ep0 = xhci_get_ep0_ctx(&xdev->in_ctx, ctx_size);
558 max_packet = usb_control_max_packet(USB_SUPER_SPEED);
560 val = EP_TYPE(EP_CTRL) | MAX_BURST(0) | ERROR_COUNT(3) |
561 MAX_PACKET_SIZE(max_packet);
562 ep0->field2 = cpu_to_le32(val);;
563 ep0->deq_addr = cpu_to_le64(xdev->control.trbs_dma | xdev->control.cycle_state);
564 ep0->field4 = cpu_to_le32(8);
567 if (!xhci_alloc_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE, XHCI_CTX_TYPE_DEVICE)) {
568 dprintf("Failed allocating out_ctx\n");
569 goto fail_control_seg;
573 xhcd->dcbaap[slot_id] = cpu_to_le64(xdev->out_ctx.dma_addr);
576 slot = xhci_get_slot_ctx(&xdev->out_ctx, ctx_size);
577 ep0 = xhci_get_ep0_ctx(&xdev->out_ctx, ctx_size);
579 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
580 xhci_send_addr_device(xhcd, slot_id, xdev->in_ctx.dma_addr);
582 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
584 dprintf("EP0 f0 %08X f1 %08X %016lX %08X\n",
585 le32_to_cpu(ep0->field1),
586 le32_to_cpu(ep0->field2),
587 le64_to_cpu(ep0->deq_addr),
588 le32_to_cpu(ep0->field4));
590 /* Step 9 - configure ep */
591 ctrl->a_flags = cpu_to_le32(0x1); /* A0 */
593 xhci_configure_ep(xhcd, slot_id, xdev->in_ctx.dma_addr);
595 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
596 dprintf("USB Device address %d \n", USB_DEV_ADDRESS(le32_to_cpu(slot->field4)));
597 dprintf("EP0 f0 %08X f1 %08X %016lX %08X\n",
598 le32_to_cpu(ep0->field1),
599 le32_to_cpu(ep0->field2),
600 le64_to_cpu(ep0->deq_addr),
601 le32_to_cpu(ep0->field4));
603 dev = usb_devpool_get();
604 dprintf("allocated device %p\n", dev);
605 dev->hcidev = xhcd->hcidev;
606 dev->speed = USB_SUPER_SPEED;
607 dev->addr = USB_DEV_ADDRESS(slot->field4);
611 if (usb_setup_new_device(dev, newport)) {
612 usb_slof_populate_new_device(dev);
616 xhci_free_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE);
618 xhci_free_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE);
620 xhci_free_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE);
624 static void xhci_free_dev(struct xhci_dev *xdev)
626 xhci_free_seg(&xdev->bulk_in, XHCI_DATA_TRBS_SIZE);
627 xhci_free_seg(&xdev->bulk_out, XHCI_DATA_TRBS_SIZE);
628 xhci_free_seg(&xdev->intr, XHCI_INTR_TRBS_SIZE);
629 xhci_free_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE);
630 xhci_free_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE);
631 xhci_free_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE);
634 static bool usb3_dev_init(struct xhci_hcd *xhcd, uint32_t port)
636 /* Device enable slot */
637 xhci_send_enable_slot(xhcd, port);
638 if (!xhcd->slot_id) {
639 dprintf("Unable to get slot id\n");
642 dprintf("SLOT ID: %d\n", xhcd->slot_id);
643 if (!xhci_alloc_dev(xhcd, xhcd->slot_id, port)) {
644 dprintf("Unable to allocate device\n");
650 static int xhci_device_present(uint32_t portsc, uint32_t usb_ver)
652 if (usb_ver == USB_XHCI) {
653 /* Device present and enabled state */
654 if ((portsc & PORTSC_CCS) &&
655 (portsc & PORTSC_PP) &&
656 (portsc & PORTSC_PED)) {
659 } else if (usb_ver == USB_EHCI) {
660 /* Device present and in disabled state */
661 if ((portsc & PORTSC_CCS) && (portsc & PORTSC_CSC))
667 static int xhci_port_scan(struct xhci_hcd *xhcd,
670 uint32_t num_ports, portsc, i;
671 struct xhci_op_regs *op;
672 struct xhci_port_regs *prs;
673 struct xhci_cap_regs *cap;
675 uint32_t *xecp_addr, *base;
676 uint32_t port_off = 0, port_cnt;
681 cap = xhcd->cap_regs;
682 port_cnt = num_ports = read_reg32(&cap->hcsparams1) >> 24;
684 /* Read the xHCI extented capability to find usb3 ports and offset*/
685 xecp_off = XHCI_HCCPARAMS_XECP(read_reg32(&cap->hccparams));
686 base = (uint32_t *)cap;
687 while (xecp_off > 0) {
688 xecp_addr = base + xecp_off;
689 dprintf("xecp_off %d %p %p \n", xecp_off, base, xecp_addr);
691 if (XHCI_XECP_CAP_ID(read_reg32(xecp_addr)) == XHCI_XECP_CAP_SP &&
692 XHCI_XECP_CAP_SP_MJ(read_reg32(xecp_addr)) == usb_ver &&
693 XHCI_XECP_CAP_SP_MN(read_reg32(xecp_addr)) == 0) {
694 port_cnt = XHCI_XECP_CAP_SP_PC(read_reg32(xecp_addr + 2));
695 port_off = XHCI_XECP_CAP_SP_PO(read_reg32(xecp_addr + 2));
696 dprintf("PortCount %d Portoffset %d\n", port_cnt, port_off);
699 xecp_off = XHCI_XECP_NEXT_PTR(read_reg32(xecp_addr));
701 if (port_off == 0) /* port_off should always start from 1 */
703 for (i = (port_off - 1); i < (port_off + port_cnt - 1); i++) {
705 portsc = read_reg32(&prs->portsc);
706 if (xhci_device_present(portsc, usb_ver)) {
708 dprintf("Device present on port %d\n", i);
710 portsc = read_reg32(&prs->portsc);
711 portsc = portsc | PORTSC_PR;
712 write_reg32(&prs->portsc, portsc);
713 /* FIXME poll for port event */
715 xhci_poll_event(xhcd, 0);
716 portsc = read_reg32(&prs->portsc);
717 if (portsc & ~PORTSC_PRC) {
718 dprintf("Port reset complete %d\n", i);
720 print_port_status(prs);
721 if (!usb3_dev_init(xhcd, (i - (port_off - 1)))) {
722 dprintf("USB device initialization failed\n");
730 static int xhci_hub_check_ports(struct xhci_hcd *xhcd)
732 return xhci_port_scan(xhcd, USB_XHCI) | xhci_port_scan(xhcd, USB_EHCI);
735 static bool xhci_hcd_init(struct xhci_hcd *xhcd)
737 struct xhci_op_regs *op;
738 struct xhci_int_regs *irs;
743 dprintf("NULL pointer\n");
748 irs = &xhcd->run_regs->irs[0];
749 if (!xhci_hcd_reset(op)) {
750 dprintf("Reset failed\n");
754 write_reg32(&op->config, XHCI_CONFIG_MAX_SLOT);
755 reg = read_reg32(&xhcd->cap_regs->hccparams);
756 /* 64byte context !! */
757 xhcd->hcc_csz_64 = (reg & XHCI_HCCPARAMS_CSZ) ? 1 : 0;
759 if (xhcd->hcc_csz_64) {
760 printf("usb-xhci: 64 Byte context not supported\n");
764 * 6.1 Device Context Base Address Array
766 * Allocate memory and initialize
768 xhcd->dcbaap = (uint64_t *)SLOF_dma_alloc(XHCI_DCBAAP_MAX_SIZE);
770 dprintf("Alloc failed\n");
773 memset((void *)xhcd->dcbaap, 0, XHCI_DCBAAP_MAX_SIZE);
774 xhcd->dcbaap_dma = SLOF_dma_map_in((void *)xhcd->dcbaap,
775 XHCI_DCBAAP_MAX_SIZE, false);
776 dprintf("dcbaap %llx, dcbaap_phys %llx\n", xhcd->dcbaap, xhcd->dcbaap_dma);
777 write_reg64(&op->dcbaap, xhcd->dcbaap_dma);
780 * Command Ring Control - TRB
781 * FIXME - better way to allocate it...
783 if (!xhci_alloc_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE, TYPE_COMMAND))
786 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
787 val = val | (xhcd->crseg.trbs_dma & XHCI_CRCR_CRP_MASK);
788 write_reg64(&op->crcr, val);
791 * Event Ring Control - TRB
792 * Allocate event TRBS
794 if (!xhci_alloc_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE, TYPE_EVENT))
798 * Populate event ring segment table.
799 * Note: only using one segment.
801 xhcd->erst.entries = SLOF_dma_alloc(XHCI_EVENT_TRBS_SIZE);
802 if (!xhcd->erst.entries)
804 xhcd->erst.dma = SLOF_dma_map_in((void *)xhcd->erst.entries,
805 XHCI_EVENT_TRBS_SIZE, false);
806 xhcd->erst.num_segs = XHCI_ERST_NUM_SEGS;
808 /* populate entries[0] */
809 write_reg64(&xhcd->erst.entries->addr, xhcd->ering.trbs_dma);
810 write_reg32(&xhcd->erst.entries->size, xhcd->ering.size);
811 write_reg32(&xhcd->erst.entries->reserved, 0);
814 val = read_reg64(&irs->erdp) & ~XHCI_ERDP_MASK;
815 val = val | (xhcd->ering.trbs_dma & XHCI_ERDP_MASK);
816 write_reg64(&irs->erdp, val);
818 /* populate erstsz */
819 val = read_reg32(&irs->erstsz) & ~XHCI_ERST_SIZE_MASK;
820 val = val | xhcd->erst.num_segs;
821 write_reg32(&irs->erstsz, val);
823 /* Now write the erstba */
824 val = read_reg64(&irs->erstba) & ~XHCI_ERST_ADDR_MASK;
825 val = val | (xhcd->erst.dma & XHCI_ERST_ADDR_MASK);
826 write_reg64(&irs->erstba, val);
828 dprintf("ERDP %llx TRB-DMA %llx\n", read_reg64(&irs->erdp),
829 xhcd->ering.trbs_dma);
830 dprintf("ERST %llx, ERST DMA %llx, size %d\n",
831 (uint64_t)xhcd->erst.entries, xhcd->erst.dma,
832 xhcd->erst.num_segs);
835 if (!xhci_hcd_set_runstop(op, true))
836 goto fail_erst_entries;
838 if (!xhci_hub_check_ports(xhcd))
839 goto fail_erst_entries;
843 write_reg64(&irs->erstba, 0);
845 SLOF_dma_map_out(xhcd->erst.dma, (void *)xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
846 SLOF_dma_free((void *)xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
848 xhci_free_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE);
850 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
851 write_reg64(&op->crcr, val);
853 xhci_free_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE);
855 write_reg64(&op->dcbaap, 0);
857 SLOF_dma_map_out(xhcd->dcbaap_dma, (void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
858 SLOF_dma_free((void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
863 static bool xhci_hcd_exit(struct xhci_hcd *xhcd)
865 struct xhci_op_regs *op;
866 struct xhci_int_regs *irs;
871 dprintf("NULL pointer\n");
876 if (!xhci_hcd_set_runstop(op, false)) {
877 dprintf("NULL pointer\n");
880 for (i = 1; i < XHCI_CONFIG_MAX_SLOT; i++) {
881 if (xhcd->xdevs[i].dev)
882 xhci_free_dev(&xhcd->xdevs[i]);
885 irs = &xhcd->run_regs->irs[0];
886 write_reg64(&irs->erstba, 0);
888 if (xhcd->erst.entries) {
889 SLOF_dma_map_out(xhcd->erst.dma, xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
890 SLOF_dma_free(xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
892 xhci_free_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE);
894 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
895 write_reg64(&op->crcr, val);
896 xhci_free_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE);
897 write_reg64(&op->dcbaap, 0);
899 SLOF_dma_map_out(xhcd->dcbaap_dma, (void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
900 SLOF_dma_free((void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
904 * QEMU implementation of XHCI doesn't implement halt
905 * properly. It basically says that it's halted immediately
906 * but doesn't actually terminate ongoing activities and
907 * DMAs. This needs to be fixed in QEMU.
909 * For now, wait for 50ms grace time till qemu stops using
917 static void xhci_init(struct usb_hcd_dev *hcidev)
919 struct xhci_hcd *xhcd;
921 printf(" XHCI: Initializing\n");
922 dprintf("device base address %p\n", hcidev->base);
924 hcidev->base = (void *)((uint64_t)hcidev->base & ~7);
925 xhcd = SLOF_alloc_mem(sizeof(*xhcd));
927 printf("usb-xhci: Unable to allocate memory\n");
930 memset(xhcd, 0, sizeof(*xhcd));
932 hcidev->nextaddr = 1;
934 xhcd->hcidev = hcidev;
935 xhcd->cap_regs = (struct xhci_cap_regs *)(hcidev->base);
936 xhcd->op_regs = (struct xhci_op_regs *)(hcidev->base +
937 read_reg8(&xhcd->cap_regs->caplength));
938 xhcd->run_regs = (struct xhci_run_regs *)(hcidev->base +
939 read_reg32(&xhcd->cap_regs->rtsoff));
940 xhcd->db_regs = (struct xhci_db_regs *)(hcidev->base +
941 read_reg32(&xhcd->cap_regs->dboff));
942 dump_xhci_regs(xhcd);
943 if (!xhci_hcd_init(xhcd))
944 printf("usb-xhci: failed to initialize XHCI controller.\n");
945 dump_xhci_regs(xhcd);
948 static void xhci_exit(struct usb_hcd_dev *hcidev)
950 struct xhci_hcd *xhcd;
952 dprintf("%s: enter \n", __func__);
953 if (!hcidev && !hcidev->priv) {
959 SLOF_free_mem(xhcd, sizeof(*xhcd));
963 static void fill_trb_buff(struct xhci_command_trb *cmd, uint32_t field1,
964 uint32_t field2, uint32_t field3, uint32_t field4)
966 uint32_t val, cycle_state;
968 cmd->field[0] = cpu_to_le32(field1);
969 cmd->field[1] = cpu_to_le32(field2);
970 cmd->field[2] = cpu_to_le32(field3);
972 val = le32_to_cpu(cmd->field[3]);
973 cycle_state = (val & 0x1) ? 0 : 1;
974 val = cycle_state | (field4 & ~0x1);
975 cmd->field[3] = cpu_to_le32(val);
977 dprintf("CMD %016lx val %08x cycle_state %d field1 %08x, field2 %08x, field3 %08x field4 %08x\n",
978 cmd, val, cycle_state,
979 le32_to_cpu(cmd->field[0]),
980 le32_to_cpu(cmd->field[1]),
981 le32_to_cpu(cmd->field[2]),
982 le32_to_cpu(cmd->field[3])
988 static void fill_setup_trb(struct xhci_command_trb *cmd, struct usb_dev_req *req,
991 uint32_t field1, field2, field3, field4 = 0;
993 uint32_t datalen = 0, pid = 0;
995 req_raw = *((uint64_t *)req);
996 dprintf("%lx %lx \n", *((uint64_t *)req), req_raw);
997 /* req_raw is already in right byte order... */
998 field1 = cpu_to_le32(TRB_ADDR_HIGH(req_raw));
999 field2 = cpu_to_le32(TRB_ADDR_LOW(req_raw));
1000 field3 = 8; /* ALWAYS 8 */
1002 datalen = cpu_to_le16(req->wLength);
1004 pid = (req->bmRequestType & REQT_DIR_IN) ? 3 : 2;
1005 field4 = TRB_TRT(pid);
1007 field4 |= TRB_CMD_TYPE(TRB_SETUP_STAGE) | TRB_IDT;
1008 fill_trb_buff(cmd, field1, field2, field3, field4);
1011 static void fill_setup_data(struct xhci_command_trb *cmd, void *data,
1012 uint32_t size, uint32_t dir)
1014 uint32_t field1, field2, field3, field4;
1016 field1 = TRB_ADDR_LOW(data);
1017 field2 = TRB_ADDR_HIGH(data);
1020 field4 = TRB_DIR_IN;
1021 field4 |= TRB_CMD_TYPE(TRB_DATA_STAGE);
1022 fill_trb_buff(cmd, field1, field2, field3, field4);
1025 static void fill_status_trb(struct xhci_command_trb *cmd, uint32_t dir)
1027 uint32_t field1, field2, field3, field4;
1033 field4 = TRB_DIR_IN;
1035 field4 |= TRB_CMD_TYPE(TRB_STATUS_STAGE) | TRB_IOC;
1036 fill_trb_buff(cmd, field1, field2, field3, field4);
1039 static void fill_normal_trb(struct xhci_transfer_trb *trb, void *data,
1042 uint32_t field1, field2, field3, field4;
1044 field1 = TRB_ADDR_LOW(data);
1045 field2 = TRB_ADDR_HIGH(data);
1047 field4 = TRB_CMD_TYPE(TRB_NORMAL) | TRB_IOC;
1048 fill_trb_buff((struct xhci_command_trb *)trb, field1, field2, field3, field4);
1051 static int xhci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
1053 struct xhci_dev *xdev;
1054 struct xhci_seg *ctrl;
1055 struct xhci_hcd *xhcd;
1056 struct xhci_command_trb *cmd;
1057 struct xhci_db_regs *dbr;
1058 long req_phys = 0, data_phys = 0;
1060 uint32_t slot_id, pid = 0, datalen = 0;
1062 if (!pipe->dev || !pipe->dev->hcidev) {
1063 dprintf(" NULL pointer\n");
1067 xdev = pipe->dev->priv;
1068 slot_id = xdev->slot_id;
1069 ctrl = &xdev->control;
1070 xhcd = (struct xhci_hcd *)pipe->dev->hcidev->priv;
1071 dbr = xhcd->db_regs;
1072 if (!ctrl || !xdev || !xhcd) {
1073 dprintf(" NULL pointer\n");
1077 cmd = (struct xhci_command_trb *)ctrl->enq;
1078 req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
1079 fill_setup_trb(cmd, req, sizeof(*req));
1082 datalen = cpu_to_le16(req->wLength);
1086 data_phys = SLOF_dma_map_in(data, datalen, true);
1087 fill_setup_data(cmd, (void *) data_phys, datalen, pid);
1091 fill_status_trb(cmd, pid);
1094 /* Ring the doorbell - ep0 */
1095 write_reg32(&dbr->db[slot_id], 1);
1096 if (!xhci_poll_event(xhcd, 0)) {
1097 dprintf("Command failed\n");
1100 ctrl->enq = (uint64_t) cmd;
1101 SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
1103 SLOF_dma_map_out(data_phys, data, datalen);
1107 static inline struct xhci_pipe *xhci_pipe_get_xpipe(struct usb_pipe *pipe)
1109 struct xhci_pipe *xpipe;
1110 xpipe = container_of(pipe, struct xhci_pipe, pipe);
1111 dprintf("%s: xpipe is %p\n", __func__, xpipe);
1115 static inline struct xhci_seg *xhci_pipe_get_seg(struct usb_pipe *pipe)
1117 struct xhci_pipe *xpipe;
1118 xpipe = xhci_pipe_get_xpipe(pipe);
1122 static inline void *xhci_get_trb(struct xhci_seg *seg)
1126 struct xhci_link_trb *link;
1128 enq = val = seg->enq;
1129 val = val + XHCI_TRB_SIZE;
1130 index = (enq - (uint64_t)seg->trbs) / XHCI_TRB_SIZE + 1;
1131 dprintf("%s: enq %llx, val %llx %x\n", __func__, enq, val, index);
1132 /* TRBs being a cyclic buffer, here we cycle back to beginning. */
1133 if (index == (seg->size - 1)) {
1134 dprintf("%s: rounding \n", __func__);
1135 seg->enq = (uint64_t)seg->trbs;
1136 seg->cycle_state ^= seg->cycle_state;
1137 link = (struct xhci_link_trb *) (seg->trbs + seg->size - 1);
1138 link->addr = cpu_to_le64(seg->trbs_dma);
1140 link->field3 = cpu_to_le32(0x1 | TRB_CMD_TYPE(TRB_LINK));
1144 seg->enq = seg->enq + XHCI_TRB_SIZE;
1150 static uint64_t xhci_get_trb_phys(struct xhci_seg *seg, uint64_t trb)
1152 return seg->trbs_dma + (trb - (uint64_t)seg->trbs);
1155 static int usb_kb = false;
1156 static int xhci_transfer_bulk(struct usb_pipe *pipe, void *td, void *td_phys,
1157 void *data, int datalen)
1159 struct xhci_dev *xdev;
1160 struct xhci_seg *seg;
1161 struct xhci_hcd *xhcd;
1162 struct xhci_transfer_trb *trb;
1163 struct xhci_db_regs *dbr;
1165 uint32_t slot_id, epno, time;
1166 uint64_t trb_phys, event_phys;
1168 if (!pipe->dev || !pipe->dev->hcidev) {
1169 dprintf(" NULL pointer\n");
1170 dprintf(" pipe dev %p hcidev %p\n", pipe->dev, pipe->dev->hcidev);
1174 xdev = pipe->dev->priv;
1175 slot_id = xdev->slot_id;
1176 seg = xhci_pipe_get_seg(pipe);
1177 xhcd = (struct xhci_hcd *)pipe->dev->hcidev->priv;
1178 dbr = xhcd->db_regs;
1179 if (!seg || !xdev || !xhcd) {
1180 dprintf(" NULL pointer\n");
1181 dprintf(" seg %p xdev %p xhcd %p\n", seg, xdev, xhcd);
1185 if (datalen > XHCI_MAX_BULK_SIZE) {
1186 printf("usb-xhci: bulk transfer size too big\n");
1190 trb = xhci_get_trb(seg);
1191 trb_phys = xhci_get_trb_phys(seg, (uint64_t)trb);
1192 fill_normal_trb(trb, (void *)data, datalen);
1194 epno = xhci_get_epno(pipe);
1195 write_reg32(&dbr->db[slot_id], epno);
1197 time = SLOF_GetTimer() + USB_TIMEOUT;
1199 event_phys = xhci_poll_event(xhcd, 0);
1200 if (event_phys == trb_phys) {
1202 } else if (event_phys == 0) { /* polling timed out */
1208 /* transfer timed out */
1209 if (time < SLOF_GetTimer())
1220 static int xhci_alloc_pipe_pool(struct xhci_hcd *xhcd)
1222 struct xhci_pipe *xpipe, *curr, *prev;
1223 unsigned int i, count;
1224 long xpipe_phys = 0;
1226 count = XHCI_PIPE_POOL_SIZE/sizeof(*xpipe);
1227 xhcd->pool = xpipe = SLOF_dma_alloc(XHCI_PIPE_POOL_SIZE);
1230 xhcd->pool_phys = xpipe_phys = SLOF_dma_map_in(xpipe, XHCI_PIPE_POOL_SIZE, true);
1231 dprintf("%s: xpipe %p, xpipe_phys %lx\n", __func__, xpipe, xpipe_phys);
1233 /* Although an array, link them */
1234 for (i = 0, curr = xpipe, prev = NULL; i < count; i++, curr++) {
1236 prev->pipe.next = &curr->pipe;
1237 curr->pipe.next = NULL;
1241 if (!xhcd->freelist)
1242 xhcd->freelist = &xpipe->pipe;
1244 xhcd->end->next = &xpipe->pipe;
1245 xhcd->end = &prev->pipe;
1250 static void xhci_init_bulk_ep(struct usb_dev *dev, struct usb_pipe *pipe)
1252 struct xhci_hcd *xhcd;
1253 struct xhci_dev *xdev;
1254 struct xhci_seg *seg;
1255 struct xhci_pipe *xpipe;
1256 struct xhci_control_ctx *ctrl;
1257 struct xhci_ep_ctx *ep;
1258 uint32_t x_epno, val, type;
1260 if (!pipe || !dev || !dev->priv)
1264 xhcd = dev->hcidev->priv;
1265 dprintf("dir %d\n", pipe->dir);
1266 seg = xhci_pipe_get_seg(pipe);
1267 xpipe = xhci_pipe_get_xpipe(pipe);
1270 seg = &xdev->bulk_in;
1274 seg = &xdev->bulk_out;
1278 if (!xhci_alloc_seg(seg, XHCI_DATA_TRBS_SIZE, TYPE_BULK)) {
1279 printf("usb-xhci: allocation failed for bulk endpoint\n");
1283 xhci_init_seg(seg, XHCI_DATA_TRBS_SIZE, TYPE_BULK);
1286 pipe->mps = XHCI_MAX_BULK_SIZE;
1287 ctrl = xhci_get_control_ctx(&xdev->in_ctx);
1288 x_epno = xhci_get_epno(pipe);
1289 ep = xhci_get_ep_ctx(&xdev->in_ctx, xdev->ctx_size, x_epno);
1290 val = EP_TYPE(type) | MAX_BURST(0) | ERROR_COUNT(3) |
1291 MAX_PACKET_SIZE(pipe->mps);
1292 ep->field2 = cpu_to_le32(val);;
1293 ep->deq_addr = cpu_to_le64(seg->trbs_dma | seg->cycle_state);
1294 ep->field4 = cpu_to_le32(8);
1295 ctrl->a_flags = cpu_to_le32(BIT(x_epno) | 0x1);
1297 xhci_configure_ep(xhcd, xdev->slot_id, xdev->in_ctx.dma_addr);
1301 static int xhci_get_pipe_intr(struct usb_pipe *pipe,
1302 struct xhci_hcd *xhcd,
1303 char *buf, size_t len)
1305 struct xhci_dev *xdev;
1306 struct xhci_seg *seg;
1307 struct xhci_pipe *xpipe;
1308 struct xhci_control_ctx *ctrl;
1309 struct xhci_ep_ctx *ep;
1310 uint32_t x_epno, val, type;
1311 struct usb_dev *dev;
1312 struct xhci_transfer_trb *trb;
1315 if (dev->class != DEV_HID_KEYB)
1320 seg = xhci_pipe_get_seg(pipe);
1321 xpipe = xhci_pipe_get_xpipe(pipe);
1326 if (!xhci_alloc_seg(seg, XHCI_INTR_TRBS_SIZE, TYPE_BULK)) {
1327 printf("usb-xhci: allocation failed for interrupt endpoint\n");
1331 xhci_init_seg(seg, XHCI_EVENT_TRBS_SIZE, TYPE_BULK);
1335 xpipe->buf_phys = SLOF_dma_map_in(buf, len, false);
1336 xpipe->buflen = len;
1338 ctrl = xhci_get_control_ctx(&xdev->in_ctx);
1339 x_epno = xhci_get_epno(pipe);
1340 ep = xhci_get_ep_ctx(&xdev->in_ctx, xdev->ctx_size, x_epno);
1341 val = EP_TYPE(type) | MAX_BURST(0) | ERROR_COUNT(3) |
1342 MAX_PACKET_SIZE(pipe->mps);
1343 ep->field2 = cpu_to_le32(val);
1344 ep->deq_addr = cpu_to_le64(seg->trbs_dma | seg->cycle_state);
1345 ep->field4 = cpu_to_le32(8);
1346 ctrl->a_flags = cpu_to_le32(BIT(x_epno) | 0x1);
1348 xhci_configure_ep(xhcd, xdev->slot_id, xdev->in_ctx.dma_addr);
1351 trb = xhci_get_trb(seg);
1352 fill_normal_trb(trb, (void *)xpipe->buf_phys, pipe->mps);
1356 static struct usb_pipe* xhci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep, char *buf, size_t len)
1358 struct xhci_hcd *xhcd;
1359 struct usb_pipe *new = NULL;
1364 xhcd = (struct xhci_hcd *)dev->hcidev->priv;
1365 if (!xhcd->freelist) {
1366 dprintf("usb-xhci: %s allocating pool\n", __func__);
1367 if (xhci_alloc_pipe_pool(xhcd))
1371 new = xhcd->freelist;
1372 xhcd->freelist = xhcd->freelist->next;
1373 if (!xhcd->freelist)
1376 memset(new, 0, sizeof(*new));
1379 new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
1380 new->speed = dev->speed;
1381 new->mps = ep->wMaxPacketSize;
1382 new->dir = (ep->bEndpointAddress & 0x80) >> 7;
1383 new->epno = ep->bEndpointAddress & 0x0f;
1385 if (new->type == USB_EP_TYPE_INTR) {
1386 if (!xhci_get_pipe_intr(new, xhcd, buf, len)) {
1387 printf("usb-xhci: %s alloc_intr failed %p\n",
1391 if (new->type == USB_EP_TYPE_BULK)
1392 xhci_init_bulk_ep(dev, new);
1397 static void xhci_put_pipe(struct usb_pipe *pipe)
1399 struct xhci_hcd *xhcd;
1400 struct xhci_pipe *xpipe;
1402 dprintf("usb-xhci: %s enter - %p\n", __func__, pipe);
1403 if (!pipe || !pipe->dev)
1405 xhcd = pipe->dev->hcidev->priv;
1407 dprintf("dir %d\n", pipe->dir);
1408 if (pipe->type == USB_EP_TYPE_BULK) {
1409 xpipe = xhci_pipe_get_xpipe(pipe);
1411 } else if (pipe->type == USB_EP_TYPE_INTR) {
1412 xpipe = xhci_pipe_get_xpipe(pipe);
1413 SLOF_dma_map_out(xpipe->buf_phys, xpipe->buf, xpipe->buflen);
1417 xhcd->end->next = pipe;
1419 xhcd->freelist = pipe;
1424 memset(pipe, 0, sizeof(*pipe));
1426 dprintf("usb-xhci: %s exit\n", __func__);
1429 static int xhci_poll_intr(struct usb_pipe *pipe, uint8_t *data)
1431 struct xhci_transfer_trb *trb;
1432 struct xhci_seg *seg;
1433 struct xhci_pipe *xpipe;
1434 struct xhci_dev *xdev;
1435 struct xhci_hcd *xhcd;
1436 struct xhci_db_regs *dbr;
1438 uint8_t *buf, ret = 1;
1440 if (!pipe || !pipe->dev || !pipe->dev->hcidev)
1442 xdev = pipe->dev->priv;
1443 xhcd = (struct xhci_hcd *)pipe->dev->hcidev->priv;
1444 x_epno = xhci_get_epno(pipe);
1445 seg = xhci_pipe_get_seg(pipe);
1446 xpipe = xhci_pipe_get_xpipe(pipe);
1448 if (usb_kb == true) {
1449 /* This event was consumed by bulk transfer */
1457 /* Ring the doorbell - x_epno */
1458 dbr = xhcd->db_regs;
1459 write_reg32(&dbr->db[xdev->slot_id], x_epno);
1460 if (!xhci_poll_event(xhcd, 0)) {
1461 printf("poll intr failed\n");
1465 memcpy(data, buf, 8);
1468 trb = xhci_get_trb(seg);
1469 fill_normal_trb(trb, (void *)xpipe->buf_phys, pipe->mps);
1474 struct usb_hcd_ops xhci_ops = {
1478 .usb_type = USB_XHCI,
1479 .get_pipe = xhci_get_pipe,
1480 .put_pipe = xhci_put_pipe,
1481 .poll_intr = xhci_poll_intr,
1482 .send_ctrl = xhci_send_ctrl,
1483 .transfer_bulk = xhci_transfer_bulk,
1487 void usb_xhci_register(void)
1489 usb_hcd_register(&xhci_ops);