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 struct xhci_event_trb *xhci_poll_event(struct xhci_hcd *xhcd,
231 struct xhci_event_trb *event;
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);
279 event->flags = cpu_to_le32(xhcd->ering.cycle_state);
281 index = xhcd->ering.deq - (uint64_t)xhcd->ering.trbs;
282 val = xhcd->ering.trbs_dma;
283 val += (index % XHCI_EVENT_TRBS_SIZE);
284 if (!(index % XHCI_EVENT_TRBS_SIZE)) {
285 xhcd->ering.deq = (uint64_t)xhcd->ering.trbs;
286 xhcd->ering.cycle_state = xhcd->ering.cycle_state ? 0 : 1;
287 dprintf("Rounding %d\n", xhcd->ering.cycle_state);
289 dprintf("Update start %x deq %x index %d\n",
290 xhcd->ering.trbs_dma, val, index/sizeof(*event));
291 write_reg64(&xhcd->run_regs->irs[0].erdp, val);
295 static void xhci_send_cmd(struct xhci_hcd *xhcd, uint32_t field1,
296 uint32_t field2, uint32_t field3, uint32_t field4)
298 struct xhci_db_regs *dbr;
299 struct xhci_command_trb *cmd;
300 uint32_t val, cycle_state;
303 cmd = (struct xhci_command_trb *)xhcd->crseg.enq;
305 cmd->field[0] = cpu_to_le32(field1);
306 cmd->field[1] = cpu_to_le32(field2);
307 cmd->field[2] = cpu_to_le32(field3);
309 val = le32_to_cpu(cmd->field[3]);
310 cycle_state = (val & 0x1) ? 0 : 1;
311 val = field4 | cycle_state;
312 cmd->field[3] = cpu_to_le32(val);
314 dprintf("CMD %016lx val %08x cycle_state %d field1 %08x, field2 %08x, field3 %08x field4 %08x\n",
315 cmd, val, cycle_state,
316 le32_to_cpu(cmd->field[0]),
317 le32_to_cpu(cmd->field[1]),
318 le32_to_cpu(cmd->field[2]),
319 le32_to_cpu(cmd->field[3])
322 /* Ring the doorbell */
323 write_reg32(&dbr->db[0], 0);
324 xhci_poll_event(xhcd, 0);
326 xhcd->crseg.enq = (uint64_t)cmd;
330 static void xhci_send_enable_slot(struct xhci_hcd *xhcd, uint32_t port)
332 uint32_t field1, field2, field3, field4;
337 field4 = TRB_CMD_TYPE(TRB_ENABLE_SLOT);
338 xhci_send_cmd(xhcd, field1, field2, field3, field4);
341 static void xhci_send_addr_device(struct xhci_hcd *xhcd, uint32_t slot_id,
344 uint32_t field1, field2, field3, field4;
346 dprintf("Address device %lx, low %x, high %x\n", dma_in_ctx,
347 TRB_ADDR_LOW(dma_in_ctx),
348 TRB_ADDR_HIGH(dma_in_ctx));
349 field1 = TRB_ADDR_LOW(dma_in_ctx) & ~0xF;
350 field2 = TRB_ADDR_HIGH(dma_in_ctx);
352 field4 = TRB_CMD_TYPE(TRB_ADDRESS_DEV) | TRB_CMD_SLOT_ID(slot_id);
353 xhci_send_cmd(xhcd, field1, field2, field3, field4);
356 static uint32_t xhci_get_epno(struct usb_pipe *pipe)
359 x_epno = pipe->dir | 2 * pipe->epno;
360 dprintf("EPno %d:%d DIR %d\n", pipe->epno, x_epno, pipe->dir);
364 static void xhci_configure_ep(struct xhci_hcd *xhcd, uint32_t slot_id,
367 uint32_t field1, field2, field3, field4;
369 dprintf("Configure EP %lx, low %x, high %x\n", dma_in_ctx,
370 TRB_ADDR_LOW(dma_in_ctx),
371 TRB_ADDR_HIGH(dma_in_ctx));
372 field1 = TRB_ADDR_LOW(dma_in_ctx) & ~0xF;
373 field2 = TRB_ADDR_HIGH(dma_in_ctx);
375 field4 = TRB_CMD_TYPE(TRB_CONFIG_EP) | TRB_CMD_SLOT_ID(slot_id);
376 xhci_send_cmd(xhcd, field1, field2, field3, field4);
379 static void xhci_init_seg(struct xhci_seg *seg, uint32_t size, uint32_t type)
381 struct xhci_link_trb *link;
383 seg->size = size / XHCI_TRB_SIZE;
386 seg->cycle_state = 1;
387 seg->enq = (uint64_t)seg->trbs;
388 seg->deq = (uint64_t)seg->trbs;
389 memset((void *)seg->trbs, 0, size);
391 link =(struct xhci_link_trb *) (seg->trbs + seg->size - 1);
392 link->addr = cpu_to_le64(seg->trbs_dma);
394 link->field3 = cpu_to_le32(0x1 | TRB_CMD_TYPE(TRB_LINK));
398 static bool xhci_alloc_seg(struct xhci_seg *seg, uint32_t size, uint32_t type)
400 seg->trbs = (union xhci_trb *)SLOF_dma_alloc(size);
402 dprintf("Alloc failed\n");
405 xhci_init_seg(seg, size, type);
406 seg->trbs_dma = SLOF_dma_map_in((void *)seg->trbs, size, false);
408 dprintf(" TRBs %016lX TRBS-DMA %016lX\n", seg->trbs, seg->trbs_dma);
412 static void xhci_free_seg(struct xhci_seg *seg, uint32_t size)
415 dprintf(" TRBs %016lX TRBS-DMA %016lX size %x\n", seg->trbs, seg->trbs_dma, size);
416 SLOF_dma_map_out(seg->trbs_dma, (void *)seg->trbs, size);
417 SLOF_dma_free((void *)seg->trbs, size);
419 memset(seg, 0, sizeof(*seg));
422 #define CTX_SIZE(x) ( (x) ? 64 : 32 )
424 static bool xhci_alloc_ctx(struct xhci_ctx *ctx, uint32_t size, uint32_t type)
426 ctx->addr = (uint8_t *)SLOF_dma_alloc(size);
428 dprintf("Alloc failed\n");
433 memset((void *)ctx->addr, 0, size);
434 ctx->dma_addr = SLOF_dma_map_in((void *)ctx->addr, size, false);
435 dprintf("ctx %llx, ctx_dma %llx\n", ctx->addr, ctx->dma_addr);
439 static struct xhci_control_ctx *xhci_get_control_ctx(struct xhci_ctx *ctx)
441 if (ctx->type == XHCI_CTX_TYPE_INPUT)
442 return (struct xhci_control_ctx *) ctx->addr;
446 static struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_ctx *ctx, uint32_t ctx_size)
450 if (ctx->type == XHCI_CTX_TYPE_INPUT)
452 return (struct xhci_slot_ctx *)(ctx->addr + offset);
455 static struct xhci_ep_ctx *xhci_get_ep0_ctx(struct xhci_ctx *ctx, uint32_t ctx_size)
460 if (ctx->type == XHCI_CTX_TYPE_INPUT)
462 return (struct xhci_ep_ctx *)(ctx->addr + offset);
465 static struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_ctx *ctx, uint32_t ctx_size,
470 offset = ctx_size * epno;
471 if (ctx->type == XHCI_CTX_TYPE_INPUT)
473 return (struct xhci_ep_ctx *)(ctx->addr + offset);
476 static void xhci_free_ctx(struct xhci_ctx *ctx, uint32_t size)
478 SLOF_dma_map_out(ctx->dma_addr, (void *)ctx->addr, size);
479 SLOF_dma_free((void *)ctx->addr, size);
482 static uint32_t usb_control_max_packet(uint32_t speed)
484 uint32_t max_packet = 0;
497 case USB_SUPER_SPEED:
501 /* should not reach here */
502 dprintf("Unknown speed\n");
507 static bool xhci_alloc_dev(struct xhci_hcd *xhcd, uint32_t slot_id, uint32_t port)
510 struct xhci_dev *xdev;
511 struct xhci_slot_ctx *slot;
512 struct xhci_control_ctx *ctrl;
513 struct xhci_ep_ctx *ep0;
514 uint32_t ctx_size, val;
518 ctx_size = CTX_SIZE(xhcd->hcc_csz_64);
519 xdev = &xhcd->xdevs[slot_id];
520 xdev->slot_id = slot_id;
521 xdev->ctx_size = ctx_size;
523 /* 4.3.3 Device Slot initialization */
525 if (!xhci_alloc_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE, XHCI_CTX_TYPE_INPUT)) {
526 dprintf("Failed allocating in_ctx\n");
531 ctrl = xhci_get_control_ctx(&xdev->in_ctx);
532 ctrl->a_flags = cpu_to_le32(0x3); /* A0, A1 */
536 slot = xhci_get_slot_ctx(&xdev->in_ctx, ctx_size);
538 val = LAST_CONTEXT(1) | SLOT_SPEED_SS | (newport << 16); /* FIXME speed, read from PS */
539 slot->field1 = cpu_to_le32(val);
540 slot->field2 = cpu_to_le32(ROOT_HUB_PORT(newport)); /* FIXME how to get port no */
543 if (!xhci_alloc_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE, TYPE_CTRL)) {
544 dprintf("Failed allocating control\n");
549 ep0 = xhci_get_ep0_ctx(&xdev->in_ctx, ctx_size);
551 max_packet = usb_control_max_packet(USB_SUPER_SPEED);
553 val = EP_TYPE(EP_CTRL) | MAX_BURST(0) | ERROR_COUNT(3) |
554 MAX_PACKET_SIZE(max_packet);
555 ep0->field2 = cpu_to_le32(val);;
556 ep0->deq_addr = cpu_to_le64(xdev->control.trbs_dma | xdev->control.cycle_state);
557 ep0->field4 = cpu_to_le32(8);
560 if (!xhci_alloc_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE, XHCI_CTX_TYPE_DEVICE)) {
561 dprintf("Failed allocating out_ctx\n");
562 goto fail_control_seg;
566 xhcd->dcbaap[slot_id] = cpu_to_le64(xdev->out_ctx.dma_addr);
569 slot = xhci_get_slot_ctx(&xdev->out_ctx, ctx_size);
570 ep0 = xhci_get_ep0_ctx(&xdev->out_ctx, ctx_size);
572 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
573 xhci_send_addr_device(xhcd, slot_id, xdev->in_ctx.dma_addr);
575 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
577 dprintf("EP0 f0 %08X f1 %08X %016lX %08X\n",
578 le32_to_cpu(ep0->field1),
579 le32_to_cpu(ep0->field2),
580 le64_to_cpu(ep0->deq_addr),
581 le32_to_cpu(ep0->field4));
583 /* Step 9 - configure ep */
584 ctrl->a_flags = cpu_to_le32(0x1); /* A0 */
586 xhci_configure_ep(xhcd, slot_id, xdev->in_ctx.dma_addr);
588 dprintf("Slot State %x \n", SLOT_STATE(le32_to_cpu(slot->field4)));
589 dprintf("USB Device address %d \n", USB_DEV_ADDRESS(le32_to_cpu(slot->field4)));
590 dprintf("EP0 f0 %08X f1 %08X %016lX %08X\n",
591 le32_to_cpu(ep0->field1),
592 le32_to_cpu(ep0->field2),
593 le64_to_cpu(ep0->deq_addr),
594 le32_to_cpu(ep0->field4));
596 dev = usb_devpool_get();
597 dprintf("allocated device %p\n", dev);
598 dev->hcidev = xhcd->hcidev;
599 dev->speed = USB_SUPER_SPEED;
600 dev->addr = USB_DEV_ADDRESS(slot->field4);
604 if (setup_new_device(dev, newport))
607 xhci_free_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE);
609 xhci_free_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE);
611 xhci_free_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE);
615 static void xhci_free_dev(struct xhci_dev *xdev)
617 xhci_free_seg(&xdev->bulk_in, XHCI_DATA_TRBS_SIZE);
618 xhci_free_seg(&xdev->bulk_out, XHCI_DATA_TRBS_SIZE);
619 xhci_free_seg(&xdev->control, XHCI_CONTROL_TRBS_SIZE);
620 xhci_free_ctx(&xdev->in_ctx, XHCI_CTX_BUF_SIZE);
621 xhci_free_ctx(&xdev->out_ctx, XHCI_CTX_BUF_SIZE);
624 static bool usb3_dev_init(struct xhci_hcd *xhcd, uint32_t port)
626 /* Device enable slot */
627 xhci_send_enable_slot(xhcd, port);
628 if (!xhcd->slot_id) {
629 dprintf("Unable to get slot id\n");
632 dprintf("SLOT ID: %d\n", xhcd->slot_id);
633 if (!xhci_alloc_dev(xhcd, xhcd->slot_id, port)) {
634 dprintf("Unable to allocate device\n");
640 static int xhci_hub_check_ports(struct xhci_hcd *xhcd)
642 uint32_t num_ports, portsc, i;
643 struct xhci_op_regs *op;
644 struct xhci_port_regs *prs;
645 struct xhci_cap_regs *cap;
647 uint32_t *xecp_addr, *base;
648 uint32_t port_off = 1, port_cnt;
653 cap = xhcd->cap_regs;
654 port_cnt = num_ports = read_reg32(&cap->hcsparams1) >> 24;
656 /* Read the xHCI extented capability to find usb3 ports and offset*/
657 xecp_off = XHCI_HCCPARAMS_XECP(read_reg32(&cap->hccparams));
658 base = (uint32_t *)cap;
659 while (xecp_off > 0) {
660 xecp_addr = base + xecp_off;
661 dprintf(stderr, "xecp_off %d %p %p \n", xecp_off, base, xecp_addr);
663 if (XHCI_XECP_CAP_ID(read_reg32(xecp_addr)) == XHCI_XECP_CAP_SP &&
664 XHCI_XECP_CAP_SP_MJ(read_reg32(xecp_addr)) == 3 &&
665 XHCI_XECP_CAP_SP_MN(read_reg32(xecp_addr)) == 0) {
666 port_cnt = XHCI_XECP_CAP_SP_PC(read_reg32(xecp_addr + 2));
667 port_off = XHCI_XECP_CAP_SP_PO(read_reg32(xecp_addr + 2));
668 dprintf(stderr, "PortCount %d Portoffset %d\n", port_cnt, port_off);
671 xecp_off = XHCI_XECP_NEXT_PTR(read_reg32(xecp_addr));
673 if (port_off == 0) /* port_off should always start from 1 */
675 for (i = (port_off - 1); i < (port_off + port_cnt - 1); i++) {
677 portsc = read_reg32(&prs->portsc);
678 if ((portsc & PORTSC_CCS) &&
679 (portsc & PORTSC_PP) &&
680 (portsc & PORTSC_PED)) {
681 /* Device present and enabled */
682 dprintf("Device present on port %d\n", i);
684 portsc = read_reg32(&prs->portsc);
685 portsc = portsc | PORTSC_PR;
686 write_reg32(&prs->portsc, portsc);
687 /* FIXME poll for port event */
689 xhci_poll_event(xhcd, 0);
690 portsc = read_reg32(&prs->portsc);
691 if (portsc & ~PORTSC_PRC) {
692 dprintf("Port reset complete %d\n", i);
694 print_port_status(prs);
695 if (!usb3_dev_init(xhcd, (i - (port_off - 1)))) {
696 dprintf("USB device initialization failed\n");
704 static bool xhci_hcd_init(struct xhci_hcd *xhcd)
706 struct xhci_op_regs *op;
707 struct xhci_int_regs *irs;
712 dprintf("NULL pointer\n");
717 irs = &xhcd->run_regs->irs[0];
718 if (!xhci_hcd_reset(op)) {
719 dprintf("Reset failed\n");
723 write_reg32(&op->config, XHCI_CONFIG_MAX_SLOT);
724 reg = read_reg32(&xhcd->cap_regs->hccparams);
725 /* 64byte context !! */
726 xhcd->hcc_csz_64 = (reg & XHCI_HCCPARAMS_CSZ) ? 1 : 0;
728 if (xhcd->hcc_csz_64) {
729 printf("usb-xhci: 64 Byte context not supported\n");
733 * 6.1 Device Context Base Address Array
735 * Allocate memory and initialize
737 xhcd->dcbaap = (uint64_t *)SLOF_dma_alloc(XHCI_DCBAAP_MAX_SIZE);
739 dprintf("Alloc failed\n");
742 memset((void *)xhcd->dcbaap, 0, XHCI_DCBAAP_MAX_SIZE);
743 xhcd->dcbaap_dma = SLOF_dma_map_in((void *)xhcd->dcbaap,
744 XHCI_DCBAAP_MAX_SIZE, false);
745 dprintf("dcbaap %llx, dcbaap_phys %llx\n", xhcd->dcbaap, xhcd->dcbaap_dma);
746 write_reg64(&op->dcbaap, xhcd->dcbaap_dma);
749 * Command Ring Control - TRB
750 * FIXME - better way to allocate it...
752 if (!xhci_alloc_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE, TYPE_COMMAND))
755 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
756 val = val | (xhcd->crseg.trbs_dma & XHCI_CRCR_CRP_MASK);
757 write_reg64(&op->crcr, val);
760 * Event Ring Control - TRB
761 * Allocate event TRBS
763 if (!xhci_alloc_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE, TYPE_EVENT))
767 * Populate event ring segment table.
768 * Note: only using one segment.
770 xhcd->erst.entries = SLOF_dma_alloc(XHCI_EVENT_TRBS_SIZE);
771 if (!xhcd->erst.entries)
773 xhcd->erst.dma = SLOF_dma_map_in((void *)xhcd->erst.entries,
774 XHCI_EVENT_TRBS_SIZE, false);
775 xhcd->erst.num_segs = XHCI_ERST_NUM_SEGS;
777 /* populate entries[0] */
778 write_reg64(&xhcd->erst.entries->addr, xhcd->ering.trbs_dma);
779 write_reg32(&xhcd->erst.entries->size, xhcd->ering.size);
780 write_reg32(&xhcd->erst.entries->reserved, 0);
783 val = read_reg64(&irs->erdp) & ~XHCI_ERDP_MASK;
784 val = val | (xhcd->ering.trbs_dma & XHCI_ERDP_MASK);
785 write_reg64(&irs->erdp, val);
787 /* populate erstsz */
788 val = read_reg32(&irs->erstsz) & ~XHCI_ERST_SIZE_MASK;
789 val = val | xhcd->erst.num_segs;
790 write_reg32(&irs->erstsz, val);
792 /* Now write the erstba */
793 val = read_reg64(&irs->erstba) & ~XHCI_ERST_ADDR_MASK;
794 val = val | (xhcd->erst.dma & XHCI_ERST_ADDR_MASK);
795 write_reg64(&irs->erstba, val);
797 dprintf("ERDP %llx TRB-DMA %llx\n", read_reg64(&irs->erdp),
798 xhcd->ering.trbs_dma);
799 dprintf("ERST %llx, ERST DMA %llx, size %d\n",
800 (uint64_t)xhcd->erst.entries, xhcd->erst.dma,
801 xhcd->erst.num_segs);
804 if (!xhci_hcd_set_runstop(op, true))
805 goto fail_erst_entries;
807 if (!xhci_hub_check_ports(xhcd))
808 goto fail_erst_entries;
812 write_reg64(&irs->erstba, 0);
814 SLOF_dma_map_out(xhcd->erst.dma, (void *)xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
815 SLOF_dma_free((void *)xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
817 xhci_free_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE);
819 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
820 write_reg64(&op->crcr, val);
822 xhci_free_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE);
824 write_reg64(&op->dcbaap, 0);
826 SLOF_dma_map_out(xhcd->dcbaap_dma, (void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
827 SLOF_dma_free((void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
832 static bool xhci_hcd_exit(struct xhci_hcd *xhcd)
834 struct xhci_op_regs *op;
835 struct xhci_int_regs *irs;
840 dprintf("NULL pointer\n");
845 if (!xhci_hcd_set_runstop(op, false)) {
846 dprintf("NULL pointer\n");
849 for (i = 1; i < XHCI_CONFIG_MAX_SLOT; i++) {
850 if (xhcd->xdevs[i].dev)
851 xhci_free_dev(&xhcd->xdevs[i]);
854 irs = &xhcd->run_regs->irs[0];
855 write_reg64(&irs->erstba, 0);
857 if (xhcd->erst.entries) {
858 SLOF_dma_map_out(xhcd->erst.dma, xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
859 SLOF_dma_free(xhcd->erst.entries, XHCI_EVENT_TRBS_SIZE);
861 xhci_free_seg(&xhcd->ering, XHCI_EVENT_TRBS_SIZE);
863 val = read_reg64(&op->crcr) & ~XHCI_CRCR_CRP_MASK;
864 write_reg64(&op->crcr, val);
865 xhci_free_seg(&xhcd->crseg, XHCI_CRCR_CRP_SIZE);
866 write_reg64(&op->dcbaap, 0);
868 SLOF_dma_map_out(xhcd->dcbaap_dma, (void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
869 SLOF_dma_free((void *)xhcd->dcbaap, XHCI_DCBAAP_MAX_SIZE);
874 static void xhci_init(struct usb_hcd_dev *hcidev)
876 struct xhci_hcd *xhcd;
878 printf(" XHCI: Initializing\n");
879 dprintf("device base address %p\n", hcidev->base);
881 hcidev->base = (void *)((uint64_t)hcidev->base & ~7);
882 xhcd = SLOF_alloc_mem(sizeof(*xhcd));
884 printf("usb-xhci: Unable to allocate memory\n");
887 memset(xhcd, 0, sizeof(*xhcd));
889 hcidev->nextaddr = 1;
891 xhcd->hcidev = hcidev;
892 xhcd->cap_regs = (struct xhci_cap_regs *)(hcidev->base);
893 xhcd->op_regs = (struct xhci_op_regs *)(hcidev->base +
894 read_reg8(&xhcd->cap_regs->caplength));
895 xhcd->run_regs = (struct xhci_run_regs *)(hcidev->base +
896 read_reg32(&xhcd->cap_regs->rtsoff));
897 xhcd->db_regs = (struct xhci_db_regs *)(hcidev->base +
898 read_reg32(&xhcd->cap_regs->dboff));
899 dump_xhci_regs(xhcd);
900 if (!xhci_hcd_init(xhcd))
901 printf("usb-xhci: failed to initialize XHCI controller.\n");
902 dump_xhci_regs(xhcd);
905 static void xhci_exit(struct usb_hcd_dev *hcidev)
907 struct xhci_hcd *xhcd;
909 dprintf("%s: enter \n", __func__);
910 if (!hcidev && !hcidev->priv) {
916 SLOF_free_mem(xhcd, sizeof(*xhcd));
920 static void fill_trb_buff(struct xhci_command_trb *cmd, uint32_t field1,
921 uint32_t field2, uint32_t field3, uint32_t field4)
923 uint32_t val, cycle_state;
925 cmd->field[0] = cpu_to_le32(field1);
926 cmd->field[1] = cpu_to_le32(field2);
927 cmd->field[2] = cpu_to_le32(field3);
929 val = le32_to_cpu(cmd->field[3]);
930 cycle_state = (val & 0x1) ? 0 : 1;
931 val = cycle_state | (field4 & ~0x1);
932 cmd->field[3] = cpu_to_le32(val);
934 dprintf("CMD %016lx val %08x cycle_state %d field1 %08x, field2 %08x, field3 %08x field4 %08x\n",
935 cmd, val, cycle_state,
936 le32_to_cpu(cmd->field[0]),
937 le32_to_cpu(cmd->field[1]),
938 le32_to_cpu(cmd->field[2]),
939 le32_to_cpu(cmd->field[3])
945 static void fill_setup_trb(struct xhci_command_trb *cmd, struct usb_dev_req *req,
948 uint32_t field1, field2, field3, field4 = 0;
950 uint32_t datalen = 0, pid = 0;
952 req_raw = *((uint64_t *)req);
953 dprintf("%lx %lx \n", *((uint64_t *)req), req_raw);
954 /* req_raw is already in right byte order... */
955 field1 = cpu_to_le32(TRB_ADDR_HIGH(req_raw));
956 field2 = cpu_to_le32(TRB_ADDR_LOW(req_raw));
957 field3 = 8; /* ALWAYS 8 */
959 datalen = cpu_to_le16(req->wLength);
961 pid = (req->bmRequestType & REQT_DIR_IN) ? 3 : 2;
962 field4 = TRB_TRT(pid);
964 field4 |= TRB_CMD_TYPE(TRB_SETUP_STAGE) | TRB_IDT;
965 fill_trb_buff(cmd, field1, field2, field3, field4);
968 static void fill_setup_data(struct xhci_command_trb *cmd, void *data,
969 uint32_t size, uint32_t dir)
971 uint32_t field1, field2, field3, field4;
973 field1 = TRB_ADDR_LOW(data);
974 field2 = TRB_ADDR_HIGH(data);
978 field4 |= TRB_CMD_TYPE(TRB_DATA_STAGE);
979 fill_trb_buff(cmd, field1, field2, field3, field4);
982 static void fill_status_trb(struct xhci_command_trb *cmd, uint32_t dir)
984 uint32_t field1, field2, field3, field4;
992 field4 |= TRB_CMD_TYPE(TRB_STATUS_STAGE) | TRB_IOC;
993 fill_trb_buff(cmd, field1, field2, field3, field4);
996 static void fill_normal_trb(struct xhci_transfer_trb *trb, void *data,
999 uint32_t field1, field2, field3, field4;
1001 field1 = TRB_ADDR_LOW(data);
1002 field2 = TRB_ADDR_HIGH(data);
1004 field4 = TRB_CMD_TYPE(TRB_NORMAL) | TRB_IOC;
1005 fill_trb_buff((struct xhci_command_trb *)trb, field1, field2, field3, field4);
1008 static int xhci_send_ctrl(struct usb_pipe *pipe, struct usb_dev_req *req, void *data)
1010 struct xhci_dev *xdev;
1011 struct xhci_seg *ctrl;
1012 struct xhci_hcd *xhcd;
1013 struct xhci_command_trb *cmd;
1014 struct xhci_db_regs *dbr;
1015 long req_phys = 0, data_phys = 0;
1017 uint32_t slot_id, pid = 0, datalen = 0;
1019 if (!pipe->dev || !pipe->dev->hcidev) {
1020 dprintf(" NULL pointer\n");
1024 xdev = pipe->dev->priv;
1025 slot_id = xdev->slot_id;
1026 ctrl = &xdev->control;
1027 xhcd = (struct xhci_hcd *)pipe->dev->hcidev->priv;
1028 dbr = xhcd->db_regs;
1029 if (!ctrl || !xdev || !xhcd) {
1030 dprintf(" NULL pointer\n");
1034 cmd = (struct xhci_command_trb *)ctrl->enq;
1035 req_phys = SLOF_dma_map_in(req, sizeof(struct usb_dev_req), true);
1036 fill_setup_trb(cmd, req, sizeof(*req));
1039 datalen = cpu_to_le16(req->wLength);
1043 data_phys = SLOF_dma_map_in(data, datalen, true);
1044 fill_setup_data(cmd, (void *) data_phys, datalen, pid);
1048 fill_status_trb(cmd, pid);
1051 /* Ring the doorbell - ep0 */
1052 write_reg32(&dbr->db[slot_id], 1);
1053 if (!xhci_poll_event(xhcd, 0)) {
1054 dprintf("Command failed\n");
1057 ctrl->enq = (uint64_t) cmd;
1058 SLOF_dma_map_out(req_phys, req, sizeof(struct usb_dev_req));
1060 SLOF_dma_map_out(data_phys, data, datalen);
1064 static inline struct xhci_pipe *xhci_pipe_get_xpipe(struct usb_pipe *pipe)
1066 struct xhci_pipe *xpipe;
1067 xpipe = container_of(pipe, struct xhci_pipe, pipe);
1068 dprintf("%s: xpipe is %p\n", __func__, xpipe);
1072 static inline struct xhci_seg *xhci_pipe_get_seg(struct usb_pipe *pipe)
1074 struct xhci_pipe *xpipe;
1075 xpipe = xhci_pipe_get_xpipe(pipe);
1079 static inline void *xhci_get_trb(struct xhci_seg *seg)
1083 struct xhci_link_trb *link;
1085 enq = val = seg->enq;
1086 val = val + XHCI_TRB_SIZE;
1087 size = seg->size * XHCI_TRB_SIZE;
1088 /* TRBs being a cyclic buffer, here we cycle back to beginning. */
1089 if ((val % size) == 0) {
1090 seg->enq = (uint64_t)seg->trbs;
1092 seg->enq = seg->enq + XHCI_TRB_SIZE;
1094 seg->cycle_state ^= seg->cycle_state;
1095 link = (struct xhci_link_trb *) (seg->trbs + seg->size - 1);
1096 link->addr = cpu_to_le64(seg->trbs_dma);
1098 link->field3 = cpu_to_le32(0x1 | TRB_CMD_TYPE(TRB_LINK));
1102 seg->enq = seg->enq + XHCI_TRB_SIZE;
1108 static int xhci_transfer_bulk(struct usb_pipe *pipe, void *td, void *td_phys,
1109 void *data, int datalen)
1111 struct xhci_dev *xdev;
1112 struct xhci_seg *seg;
1113 struct xhci_hcd *xhcd;
1114 struct xhci_transfer_trb *trb;
1115 struct xhci_db_regs *dbr;
1117 uint32_t slot_id, epno;
1119 if (!pipe->dev || !pipe->dev->hcidev) {
1120 dprintf(" NULL pointer\n");
1121 dprintf(" pipe dev %p hcidev %p\n", pipe->dev, pipe->dev->hcidev);
1125 xdev = pipe->dev->priv;
1126 slot_id = xdev->slot_id;
1127 seg = xhci_pipe_get_seg(pipe);
1128 xhcd = (struct xhci_hcd *)pipe->dev->hcidev->priv;
1129 dbr = xhcd->db_regs;
1130 if (!seg || !xdev || !xhcd) {
1131 dprintf(" NULL pointer\n");
1132 dprintf(" seg %p xdev %p xhcd %p\n", seg, xdev, xhcd);
1136 if (datalen > XHCI_MAX_BULK_SIZE) {
1137 printf("usb-xhci: bulk transfer size too big\n");
1141 trb = xhci_get_trb(seg);
1142 fill_normal_trb(trb, (void *)data, datalen);
1144 epno = xhci_get_epno(pipe);
1145 write_reg32(&dbr->db[slot_id], epno);
1146 if (!xhci_poll_event(xhcd, 0)) {
1147 dprintf("Bulk failed\n");
1158 static int xhci_alloc_pipe_pool(struct xhci_hcd *xhcd)
1160 struct xhci_pipe *xpipe, *curr, *prev;
1161 unsigned int i, count;
1162 long xpipe_phys = 0;
1164 count = XHCI_PIPE_POOL_SIZE/sizeof(*xpipe);
1165 xhcd->pool = xpipe = SLOF_dma_alloc(XHCI_PIPE_POOL_SIZE);
1168 xhcd->pool_phys = xpipe_phys = SLOF_dma_map_in(xpipe, XHCI_PIPE_POOL_SIZE, true);
1169 dprintf("%s: xpipe %p, xpipe_phys %lx\n", __func__, xpipe, xpipe_phys);
1171 /* Although an array, link them */
1172 for (i = 0, curr = xpipe, prev = NULL; i < count; i++, curr++) {
1174 prev->pipe.next = &curr->pipe;
1175 curr->pipe.next = NULL;
1179 if (!xhcd->freelist)
1180 xhcd->freelist = &xpipe->pipe;
1182 xhcd->end->next = &xpipe->pipe;
1183 xhcd->end = &prev->pipe;
1188 static void xhci_init_bulk_ep(struct usb_dev *dev, struct usb_pipe *pipe)
1190 struct xhci_hcd *xhcd;
1191 struct xhci_dev *xdev;
1192 struct xhci_seg *seg;
1193 struct xhci_pipe *xpipe;
1194 struct xhci_control_ctx *ctrl;
1195 struct xhci_ep_ctx *ep;
1196 uint32_t x_epno, val, type;
1198 if (!pipe || !dev || !dev->priv)
1202 xhcd = dev->hcidev->priv;
1203 dprintf("dir %d\n", pipe->dir);
1204 seg = xhci_pipe_get_seg(pipe);
1205 xpipe = xhci_pipe_get_xpipe(pipe);
1208 seg = &xdev->bulk_in;
1212 seg = &xdev->bulk_out;
1216 if (!xhci_alloc_seg(seg, XHCI_DATA_TRBS_SIZE, TYPE_BULK)) {
1217 dprintf("Failed allocating seg\n");
1220 xhci_init_seg(seg, XHCI_DATA_TRBS_SIZE, TYPE_BULK);
1223 pipe->mps = XHCI_MAX_BULK_SIZE;
1224 ctrl = xhci_get_control_ctx(&xdev->in_ctx);
1225 x_epno = xhci_get_epno(pipe);
1226 ep = xhci_get_ep_ctx(&xdev->in_ctx, xdev->ctx_size, x_epno);
1227 val = EP_TYPE(type) | MAX_BURST(0) | ERROR_COUNT(3) |
1228 MAX_PACKET_SIZE(pipe->mps);
1229 ep->field2 = cpu_to_le32(val);;
1230 ep->deq_addr = cpu_to_le64(seg->trbs_dma | seg->cycle_state);
1231 ep->field4 = cpu_to_le32(8);
1232 ctrl->a_flags = cpu_to_le32(BIT(x_epno) | 0x1);
1234 xhci_configure_ep(xhcd, xdev->slot_id, xdev->in_ctx.dma_addr);
1238 static struct usb_pipe* xhci_get_pipe(struct usb_dev *dev, struct usb_ep_descr *ep, char *buf, size_t len)
1240 struct xhci_hcd *xhcd;
1241 struct usb_pipe *new = NULL;
1246 xhcd = (struct xhci_hcd *)dev->hcidev->priv;
1247 if (!xhcd->freelist) {
1248 dprintf("usb-xhci: %s allocating pool\n", __func__);
1249 if (xhci_alloc_pipe_pool(xhcd))
1253 new = xhcd->freelist;
1254 xhcd->freelist = xhcd->freelist->next;
1255 if (!xhcd->freelist)
1258 memset(new, 0, sizeof(*new));
1261 new->type = ep->bmAttributes & USB_EP_TYPE_MASK;
1262 new->speed = dev->speed;
1263 new->mps = ep->wMaxPacketSize;
1264 new->dir = (ep->bEndpointAddress & 0x80) >> 7;
1265 new->epno = ep->bEndpointAddress & 0x0f;
1267 if (new->type == USB_EP_TYPE_BULK)
1268 xhci_init_bulk_ep(dev, new);
1273 static void xhci_put_pipe(struct usb_pipe *pipe)
1275 struct xhci_hcd *xhcd;
1276 struct xhci_pipe *xpipe;
1278 dprintf("usb-xhci: %s enter - %p\n", __func__, pipe);
1279 if (!pipe || !pipe->dev)
1281 xhcd = pipe->dev->hcidev->priv;
1283 dprintf("dir %d\n", pipe->dir);
1284 if (pipe->type == USB_EP_TYPE_BULK) {
1285 xpipe = xhci_pipe_get_xpipe(pipe);
1289 xhcd->end->next = pipe;
1291 xhcd->freelist = pipe;
1296 memset(pipe, 0, sizeof(*pipe));
1298 dprintf("usb-xhci: %s exit\n", __func__);
1301 struct usb_hcd_ops xhci_ops = {
1305 .usb_type = USB_XHCI,
1306 .get_pipe = xhci_get_pipe,
1307 .put_pipe = xhci_put_pipe,
1308 .send_ctrl = xhci_send_ctrl,
1309 .transfer_bulk = xhci_transfer_bulk,
1313 void usb_xhci_register(void)
1315 usb_hcd_register(&xhci_ops);