1 // SPDX-License-Identifier: GPL-2.0+
3 * USB HOST XHCI Controller stack
5 * Based on xHCI host controller driver in linux-kernel
8 * Copyright (C) 2008 Intel Corp.
11 * Copyright (C) 2013 Samsung Electronics Co.Ltd
12 * Authors: Vivek Gautam <gautam.vivek@samsung.com>
13 * Vikas Sajjan <vikas.sajjan@samsung.com>
17 * This file gives the xhci stack for usb3.0 looking into
18 * xhci specification Rev1.0 (5/21/10).
19 * The quirk devices support hasn't been given yet.
24 #include <asm/byteorder.h>
28 #include <asm/cache.h>
29 #include <asm/unaligned.h>
30 #include <linux/errno.h>
33 #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
34 #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
37 static struct descriptor {
38 struct usb_hub_descriptor hub;
39 struct usb_device_descriptor device;
40 struct usb_config_descriptor config;
41 struct usb_interface_descriptor interface;
42 struct usb_endpoint_descriptor endpoint;
43 struct usb_ss_ep_comp_descriptor ep_companion;
44 } __attribute__ ((packed)) descriptor = {
46 0xc, /* bDescLength */
47 0x2a, /* bDescriptorType: hub descriptor */
48 2, /* bNrPorts -- runtime modified */
49 cpu_to_le16(0x8), /* wHubCharacteristics */
50 10, /* bPwrOn2PwrGood */
51 0, /* bHubCntrCurrent */
52 { /* Device removable */
53 } /* at most 7 ports! XXX */
57 1, /* bDescriptorType: UDESC_DEVICE */
58 cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
59 9, /* bDeviceClass: UDCLASS_HUB */
60 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
61 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
62 9, /* bMaxPacketSize: 512 bytes 2^9 */
63 0x0000, /* idVendor */
64 0x0000, /* idProduct */
65 cpu_to_le16(0x0100), /* bcdDevice */
66 1, /* iManufacturer */
68 0, /* iSerialNumber */
69 1 /* bNumConfigurations: 1 */
73 2, /* bDescriptorType: UDESC_CONFIG */
74 cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
75 1, /* bNumInterface */
76 1, /* bConfigurationValue */
77 0, /* iConfiguration */
78 0x40, /* bmAttributes: UC_SELF_POWER */
83 4, /* bDescriptorType: UDESC_INTERFACE */
84 0, /* bInterfaceNumber */
85 0, /* bAlternateSetting */
86 1, /* bNumEndpoints */
87 9, /* bInterfaceClass: UICLASS_HUB */
88 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
89 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
94 5, /* bDescriptorType: UDESC_ENDPOINT */
95 0x81, /* bEndpointAddress: IN endpoint 1 */
96 3, /* bmAttributes: UE_INTERRUPT */
97 8, /* wMaxPacketSize */
101 0x06, /* ss_bLength */
102 0x30, /* ss_bDescriptorType: SS EP Companion */
103 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
104 /* ss_bmAttributes: 1 packet per service interval */
106 /* ss_wBytesPerInterval: 15 bits for max 15 ports */
111 #ifndef CONFIG_DM_USB
112 static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
115 struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
120 /* Find the USB controller */
121 for (dev = udev->dev;
122 device_get_uclass_id(dev) != UCLASS_USB;
125 return dev_get_priv(dev);
127 return udev->controller;
132 * Waits for as per specified amount of time
133 * for the "result" to match with "done"
135 * @param ptr pointer to the register to be read
136 * @param mask mask for the value read
137 * @param done value to be campared with result
138 * @param usec time to wait till
139 * @return 0 if handshake is success else < 0 on failure
141 static int handshake(uint32_t volatile *ptr, uint32_t mask,
142 uint32_t done, int usec)
147 result = xhci_readl(ptr);
148 if (result == ~(uint32_t)0)
161 * Set the run bit and wait for the host to be running.
163 * @param hcor pointer to host controller operation registers
164 * @return status of the Handshake
166 static int xhci_start(struct xhci_hcor *hcor)
171 puts("Starting the controller\n");
172 temp = xhci_readl(&hcor->or_usbcmd);
174 xhci_writel(&hcor->or_usbcmd, temp);
177 * Wait for the HCHalted Status bit to be 0 to indicate the host is
180 ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
182 debug("Host took too long to start, "
183 "waited %u microseconds.\n",
189 * Resets the XHCI Controller
191 * @param hcor pointer to host controller operation registers
192 * @return -EBUSY if XHCI Controller is not halted else status of handshake
194 static int xhci_reset(struct xhci_hcor *hcor)
200 /* Halting the Host first */
201 debug("// Halt the HC: %p\n", hcor);
202 state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
204 cmd = xhci_readl(&hcor->or_usbcmd);
206 xhci_writel(&hcor->or_usbcmd, cmd);
209 ret = handshake(&hcor->or_usbsts,
210 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
212 printf("Host not halted after %u microseconds.\n",
217 debug("// Reset the HC\n");
218 cmd = xhci_readl(&hcor->or_usbcmd);
220 xhci_writel(&hcor->or_usbcmd, cmd);
222 ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
227 * xHCI cannot write to any doorbells or operational registers other
228 * than status until the "Controller Not Ready" flag is cleared.
230 return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
234 * Used for passing endpoint bitmasks between the core and HCDs.
235 * Find the index for an endpoint given its descriptor.
236 * Use the return value to right shift 1 for the bitmask.
238 * Index = (epnum * 2) + direction - 1,
239 * where direction = 0 for OUT, 1 for IN.
240 * For control endpoints, the IN index is used (OUT index is unused), so
241 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
243 * @param desc USB enpdoint Descriptor
244 * @return index of the Endpoint
246 static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
250 if (usb_endpoint_xfer_control(desc))
251 index = (unsigned int)(usb_endpoint_num(desc) * 2);
253 index = (unsigned int)((usb_endpoint_num(desc) * 2) -
254 (usb_endpoint_dir_in(desc) ? 0 : 1));
260 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
261 * microframes, rounded down to nearest power of 2.
263 static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
264 unsigned int min_exponent,
265 unsigned int max_exponent)
267 unsigned int interval;
269 interval = fls(desc_interval) - 1;
270 interval = clamp_val(interval, min_exponent, max_exponent);
271 if ((1 << interval) != desc_interval)
272 debug("rounding interval to %d microframes, "\
273 "ep desc says %d microframes\n",
274 1 << interval, desc_interval);
279 static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
280 struct usb_endpoint_descriptor *endpt_desc)
282 if (endpt_desc->bInterval == 0)
285 return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
288 static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
289 struct usb_endpoint_descriptor *endpt_desc)
291 return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
295 * Convert interval expressed as 2^(bInterval - 1) == interval into
296 * straight exponent value 2^n == interval.
298 static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
299 struct usb_endpoint_descriptor *endpt_desc)
301 unsigned int interval;
303 interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
304 if (interval != endpt_desc->bInterval - 1)
305 debug("ep %#x - rounding interval to %d %sframes\n",
306 endpt_desc->bEndpointAddress, 1 << interval,
307 udev->speed == USB_SPEED_FULL ? "" : "micro");
309 if (udev->speed == USB_SPEED_FULL) {
311 * Full speed isoc endpoints specify interval in frames,
312 * not microframes. We are using microframes everywhere,
313 * so adjust accordingly.
315 interval += 3; /* 1 frame = 2^3 uframes */
322 * Return the polling or NAK interval.
324 * The polling interval is expressed in "microframes". If xHCI's Interval field
325 * is set to N, it will service the endpoint every 2^(Interval)*125us.
327 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
330 static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
331 struct usb_endpoint_descriptor *endpt_desc)
333 unsigned int interval = 0;
335 switch (udev->speed) {
338 if (usb_endpoint_xfer_control(endpt_desc) ||
339 usb_endpoint_xfer_bulk(endpt_desc)) {
340 interval = xhci_parse_microframe_interval(udev,
344 /* Fall through - SS and HS isoc/int have same decoding */
346 case USB_SPEED_SUPER:
347 if (usb_endpoint_xfer_int(endpt_desc) ||
348 usb_endpoint_xfer_isoc(endpt_desc)) {
349 interval = xhci_parse_exponent_interval(udev,
355 if (usb_endpoint_xfer_isoc(endpt_desc)) {
356 interval = xhci_parse_exponent_interval(udev,
361 * Fall through for interrupt endpoint interval decoding
362 * since it uses the same rules as low speed interrupt
367 if (usb_endpoint_xfer_int(endpt_desc) ||
368 usb_endpoint_xfer_isoc(endpt_desc)) {
369 interval = xhci_parse_frame_interval(udev, endpt_desc);
381 * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
382 * High speed endpoint descriptors can define "the number of additional
383 * transaction opportunities per microframe", but that goes in the Max Burst
384 * endpoint context field.
386 static u32 xhci_get_endpoint_mult(struct usb_device *udev,
387 struct usb_endpoint_descriptor *endpt_desc,
388 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
390 if (udev->speed < USB_SPEED_SUPER ||
391 !usb_endpoint_xfer_isoc(endpt_desc))
394 return ss_ep_comp_desc->bmAttributes;
397 static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
398 struct usb_endpoint_descriptor *endpt_desc,
399 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
401 /* Super speed and Plus have max burst in ep companion desc */
402 if (udev->speed >= USB_SPEED_SUPER)
403 return ss_ep_comp_desc->bMaxBurst;
405 if (udev->speed == USB_SPEED_HIGH &&
406 (usb_endpoint_xfer_isoc(endpt_desc) ||
407 usb_endpoint_xfer_int(endpt_desc)))
408 return usb_endpoint_maxp_mult(endpt_desc) - 1;
414 * Return the maximum endpoint service interval time (ESIT) payload.
415 * Basically, this is the maxpacket size, multiplied by the burst size
418 static u32 xhci_get_max_esit_payload(struct usb_device *udev,
419 struct usb_endpoint_descriptor *endpt_desc,
420 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
425 /* Only applies for interrupt or isochronous endpoints */
426 if (usb_endpoint_xfer_control(endpt_desc) ||
427 usb_endpoint_xfer_bulk(endpt_desc))
430 /* SuperSpeed Isoc ep with less than 48k per esit */
431 if (udev->speed >= USB_SPEED_SUPER)
432 return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
434 max_packet = usb_endpoint_maxp(endpt_desc);
435 max_burst = usb_endpoint_maxp_mult(endpt_desc);
437 /* A 0 in max burst means 1 transfer per ESIT */
438 return max_packet * max_burst;
442 * Issue a configure endpoint command or evaluate context command
443 * and wait for it to finish.
445 * @param udev pointer to the Device Data Structure
446 * @param ctx_change flag to indicate the Context has changed or NOT
447 * @return 0 on success, -1 on failure
449 static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
451 struct xhci_container_ctx *in_ctx;
452 struct xhci_virt_device *virt_dev;
453 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
454 union xhci_trb *event;
456 virt_dev = ctrl->devs[udev->slot_id];
457 in_ctx = virt_dev->in_ctx;
459 xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
460 xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
461 ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
462 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
463 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
466 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
468 debug("Successful %s command\n",
469 ctx_change ? "Evaluate Context" : "Configure Endpoint");
472 printf("ERROR: %s command returned completion code %d.\n",
473 ctx_change ? "Evaluate Context" : "Configure Endpoint",
474 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
478 xhci_acknowledge_event(ctrl);
484 * Configure the endpoint, programming the device contexts.
486 * @param udev pointer to the USB device structure
487 * @return returns the status of the xhci_configure_endpoints
489 static int xhci_set_configuration(struct usb_device *udev)
491 struct xhci_container_ctx *in_ctx;
492 struct xhci_container_ctx *out_ctx;
493 struct xhci_input_control_ctx *ctrl_ctx;
494 struct xhci_slot_ctx *slot_ctx;
495 struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
500 unsigned int ep_type;
501 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
505 int slot_id = udev->slot_id;
506 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
507 struct usb_interface *ifdesc;
508 u32 max_esit_payload;
509 unsigned int interval;
511 unsigned int max_burst;
512 unsigned int avg_trb_len;
513 unsigned int err_count = 0;
515 out_ctx = virt_dev->out_ctx;
516 in_ctx = virt_dev->in_ctx;
518 num_of_ep = udev->config.if_desc[0].no_of_ep;
519 ifdesc = &udev->config.if_desc[0];
521 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
522 /* Initialize the input context control */
523 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
524 ctrl_ctx->drop_flags = 0;
526 /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
527 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
528 ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
529 ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
530 if (max_ep_flag < ep_flag)
531 max_ep_flag = ep_flag;
534 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
537 xhci_slot_copy(ctrl, in_ctx, out_ctx);
538 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
539 slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
540 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
542 xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
544 /* filling up ep contexts */
545 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
546 struct usb_endpoint_descriptor *endpt_desc = NULL;
547 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
549 endpt_desc = &ifdesc->ep_desc[cur_ep];
550 ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
554 * Get values to fill the endpoint context, mostly from ep
555 * descriptor. The average TRB buffer lengt for bulk endpoints
556 * is unclear as we have no clue on scatter gather list entry
557 * size. For Isoc and Int, set it to max available.
558 * See xHCI 1.1 spec 4.14.1.1 for details.
560 max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
562 interval = xhci_get_endpoint_interval(udev, endpt_desc);
563 mult = xhci_get_endpoint_mult(udev, endpt_desc,
565 max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
567 avg_trb_len = max_esit_payload;
569 ep_index = xhci_get_ep_index(endpt_desc);
570 ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
572 /* Allocate the ep rings */
573 virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
574 if (!virt_dev->eps[ep_index].ring)
577 /*NOTE: ep_desc[0] actually represents EP1 and so on */
578 dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
579 ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
581 ep_ctx[ep_index]->ep_info =
582 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
583 EP_INTERVAL(interval) | EP_MULT(mult));
585 ep_ctx[ep_index]->ep_info2 =
586 cpu_to_le32(ep_type << EP_TYPE_SHIFT);
587 ep_ctx[ep_index]->ep_info2 |=
588 cpu_to_le32(MAX_PACKET
589 (get_unaligned(&endpt_desc->wMaxPacketSize)));
591 /* Allow 3 retries for everything but isoc, set CErr = 3 */
592 if (!usb_endpoint_xfer_isoc(endpt_desc))
594 ep_ctx[ep_index]->ep_info2 |=
595 cpu_to_le32(MAX_BURST(max_burst) |
596 ERROR_COUNT(err_count));
599 virt_dev->eps[ep_index].ring->enqueue;
600 ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
601 virt_dev->eps[ep_index].ring->cycle_state);
605 * 'Average TRB Length' should be 8 for control endpoints.
607 if (usb_endpoint_xfer_control(endpt_desc))
609 ep_ctx[ep_index]->tx_info =
610 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
611 EP_AVG_TRB_LENGTH(avg_trb_len));
614 return xhci_configure_endpoints(udev, false);
618 * Issue an Address Device command (which will issue a SetAddress request to
621 * @param udev pointer to the Device Data Structure
622 * @return 0 if successful else error code on failure
624 static int xhci_address_device(struct usb_device *udev, int root_portnr)
627 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
628 struct xhci_slot_ctx *slot_ctx;
629 struct xhci_input_control_ctx *ctrl_ctx;
630 struct xhci_virt_device *virt_dev;
631 int slot_id = udev->slot_id;
632 union xhci_trb *event;
634 virt_dev = ctrl->devs[slot_id];
637 * This is the first Set Address since device plug-in
638 * so setting up the slot context.
640 debug("Setting up addressable devices %p\n", ctrl->dcbaa);
641 xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
643 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
644 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
645 ctrl_ctx->drop_flags = 0;
647 xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
648 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
649 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
651 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
654 printf("Setup ERROR: address device command for slot %d.\n",
659 puts("Device not responding to set address.\n");
663 puts("ERROR: Incompatible device"
664 "for address device command.\n");
668 debug("Successful Address Device command\n");
672 printf("ERROR: unexpected command completion code 0x%x.\n",
673 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
678 xhci_acknowledge_event(ctrl);
682 * TODO: Unsuccessful Address Device command shall leave the
683 * slot in default state. So, issue Disable Slot command now.
687 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
688 virt_dev->out_ctx->size);
689 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
691 debug("xHC internal address is: %d\n",
692 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
698 * Issue Enable slot command to the controller to allocate
699 * device slot and assign the slot id. It fails if the xHC
700 * ran out of device slots, the Enable Slot command timed out,
701 * or allocating memory failed.
703 * @param udev pointer to the Device Data Structure
704 * @return Returns 0 on succes else return error code on failure
706 static int _xhci_alloc_device(struct usb_device *udev)
708 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
709 union xhci_trb *event;
713 * Root hub will be first device to be initailized.
714 * If this device is root-hub, don't do any xHC related
717 if (ctrl->rootdev == 0) {
718 udev->speed = USB_SPEED_SUPER;
722 xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
723 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
724 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
727 udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
729 xhci_acknowledge_event(ctrl);
731 ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
734 * TODO: Unsuccessful Address Device command shall leave
735 * the slot in default. So, issue Disable Slot command now.
737 puts("Could not allocate xHCI USB device data structures\n");
744 #ifndef CONFIG_DM_USB
745 int usb_alloc_device(struct usb_device *udev)
747 return _xhci_alloc_device(udev);
752 * Full speed devices may have a max packet size greater than 8 bytes, but the
753 * USB core doesn't know that until it reads the first 8 bytes of the
754 * descriptor. If the usb_device's max packet size changes after that point,
755 * we need to issue an evaluate context command and wait on it.
757 * @param udev pointer to the Device Data Structure
758 * @return returns the status of the xhci_configure_endpoints
760 int xhci_check_maxpacket(struct usb_device *udev)
762 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
763 unsigned int slot_id = udev->slot_id;
764 int ep_index = 0; /* control endpoint */
765 struct xhci_container_ctx *in_ctx;
766 struct xhci_container_ctx *out_ctx;
767 struct xhci_input_control_ctx *ctrl_ctx;
768 struct xhci_ep_ctx *ep_ctx;
770 int hw_max_packet_size;
773 out_ctx = ctrl->devs[slot_id]->out_ctx;
774 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
776 ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
777 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
778 max_packet_size = udev->epmaxpacketin[0];
779 if (hw_max_packet_size != max_packet_size) {
780 debug("Max Packet Size for ep 0 changed.\n");
781 debug("Max packet size in usb_device = %d\n", max_packet_size);
782 debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
783 debug("Issuing evaluate context command.\n");
785 /* Set up the modified control endpoint 0 */
786 xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
787 ctrl->devs[slot_id]->out_ctx, ep_index);
788 in_ctx = ctrl->devs[slot_id]->in_ctx;
789 ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
790 ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
791 << MAX_PACKET_SHIFT));
792 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
795 * Set up the input context flags for the command
796 * FIXME: This won't work if a non-default control endpoint
797 * changes max packet sizes.
799 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
800 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
801 ctrl_ctx->drop_flags = 0;
803 ret = xhci_configure_endpoints(udev, true);
809 * Clears the Change bits of the Port Status Register
811 * @param wValue request value
812 * @param wIndex request index
813 * @param addr address of posrt status register
814 * @param port_status state of port status register
817 static void xhci_clear_port_change_bit(u16 wValue,
818 u16 wIndex, volatile uint32_t *addr, u32 port_status)
820 char *port_change_bit;
824 case USB_PORT_FEAT_C_RESET:
826 port_change_bit = "reset";
828 case USB_PORT_FEAT_C_CONNECTION:
830 port_change_bit = "connect";
832 case USB_PORT_FEAT_C_OVER_CURRENT:
834 port_change_bit = "over-current";
836 case USB_PORT_FEAT_C_ENABLE:
838 port_change_bit = "enable/disable";
840 case USB_PORT_FEAT_C_SUSPEND:
842 port_change_bit = "suspend/resume";
845 /* Should never happen */
849 /* Change bits are all write 1 to clear */
850 xhci_writel(addr, port_status | status);
852 port_status = xhci_readl(addr);
853 debug("clear port %s change, actual port %d status = 0x%x\n",
854 port_change_bit, wIndex, port_status);
858 * Save Read Only (RO) bits and save read/write bits where
859 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
860 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
862 * @param state state of the Port Status and Control Regsiter
863 * @return a value that would result in the port being in the
864 * same state, if the value was written to the port
865 * status control register.
867 static u32 xhci_port_state_to_neutral(u32 state)
869 /* Save read-only status and port state */
870 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
874 * Submits the Requests to the XHCI Host Controller
876 * @param udev pointer to the USB device structure
877 * @param pipe contains the DIR_IN or OUT , devnum
878 * @param buffer buffer to be read/written based on the request
879 * @return returns 0 if successful else -1 on failure
881 static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
882 void *buffer, struct devrequest *req)
889 volatile uint32_t *status_reg;
890 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
891 struct xhci_hccr *hccr = ctrl->hccr;
892 struct xhci_hcor *hcor = ctrl->hcor;
893 int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
895 if ((req->requesttype & USB_RT_PORT) &&
896 le16_to_cpu(req->index) > max_ports) {
897 printf("The request port(%d) exceeds maximum port number\n",
898 le16_to_cpu(req->index) - 1);
902 status_reg = (volatile uint32_t *)
903 (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
906 typeReq = req->request | req->requesttype << 8;
909 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
910 switch (le16_to_cpu(req->value) >> 8) {
912 debug("USB_DT_DEVICE request\n");
913 srcptr = &descriptor.device;
917 debug("USB_DT_CONFIG config\n");
918 srcptr = &descriptor.config;
922 debug("USB_DT_STRING config\n");
923 switch (le16_to_cpu(req->value) & 0xff) {
924 case 0: /* Language */
925 srcptr = "\4\3\11\4";
928 case 1: /* Vendor String */
929 srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
932 case 2: /* Product Name */
933 srcptr = "\52\3X\0H\0C\0I\0 "
935 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
939 printf("unknown value DT_STRING %x\n",
940 le16_to_cpu(req->value));
945 printf("unknown value %x\n", le16_to_cpu(req->value));
949 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
950 switch (le16_to_cpu(req->value) >> 8) {
953 debug("USB_DT_HUB config\n");
954 srcptr = &descriptor.hub;
958 printf("unknown value %x\n", le16_to_cpu(req->value));
962 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
963 debug("USB_REQ_SET_ADDRESS\n");
964 ctrl->rootdev = le16_to_cpu(req->value);
966 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
969 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
970 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
975 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
976 memset(tmpbuf, 0, 4);
977 reg = xhci_readl(status_reg);
978 if (reg & PORT_CONNECT) {
979 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
980 switch (reg & DEV_SPEED_MASK) {
982 debug("SPEED = FULLSPEED\n");
985 debug("SPEED = LOWSPEED\n");
986 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
989 debug("SPEED = HIGHSPEED\n");
990 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
993 debug("SPEED = SUPERSPEED\n");
994 tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
999 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
1000 if ((reg & PORT_PLS_MASK) == XDEV_U3)
1001 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
1003 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
1004 if (reg & PORT_RESET)
1005 tmpbuf[0] |= USB_PORT_STAT_RESET;
1006 if (reg & PORT_POWER)
1008 * XXX: This Port power bit (for USB 3.0 hub)
1009 * we are faking in USB 2.0 hub port status;
1010 * since there's a change in bit positions in
1012 * USB 2.0 port status PP is at position[8]
1013 * USB 3.0 port status PP is at position[9]
1014 * So, we are still keeping it at position [8]
1016 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
1018 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
1020 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
1022 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
1024 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
1029 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1030 reg = xhci_readl(status_reg);
1031 reg = xhci_port_state_to_neutral(reg);
1032 switch (le16_to_cpu(req->value)) {
1033 case USB_PORT_FEAT_ENABLE:
1035 xhci_writel(status_reg, reg);
1037 case USB_PORT_FEAT_POWER:
1039 xhci_writel(status_reg, reg);
1041 case USB_PORT_FEAT_RESET:
1043 xhci_writel(status_reg, reg);
1046 printf("unknown feature %x\n", le16_to_cpu(req->value));
1050 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1051 reg = xhci_readl(status_reg);
1052 reg = xhci_port_state_to_neutral(reg);
1053 switch (le16_to_cpu(req->value)) {
1054 case USB_PORT_FEAT_ENABLE:
1057 case USB_PORT_FEAT_POWER:
1060 case USB_PORT_FEAT_C_RESET:
1061 case USB_PORT_FEAT_C_CONNECTION:
1062 case USB_PORT_FEAT_C_OVER_CURRENT:
1063 case USB_PORT_FEAT_C_ENABLE:
1064 xhci_clear_port_change_bit((le16_to_cpu(req->value)),
1065 le16_to_cpu(req->index),
1069 printf("unknown feature %x\n", le16_to_cpu(req->value));
1072 xhci_writel(status_reg, reg);
1075 puts("Unknown request\n");
1079 debug("scrlen = %d\n req->length = %d\n",
1080 srclen, le16_to_cpu(req->length));
1082 len = min(srclen, (int)le16_to_cpu(req->length));
1084 if (srcptr != NULL && len > 0)
1085 memcpy(buffer, srcptr, len);
1087 debug("Len is 0\n");
1089 udev->act_len = len;
1096 udev->status = USB_ST_STALLED;
1102 * Submits the INT request to XHCI Host cotroller
1104 * @param udev pointer to the USB device
1105 * @param pipe contains the DIR_IN or OUT , devnum
1106 * @param buffer buffer to be read/written based on the request
1107 * @param length length of the buffer
1108 * @param interval interval of the interrupt
1111 static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
1112 void *buffer, int length, int interval)
1114 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1115 printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1120 * xHCI uses normal TRBs for both bulk and interrupt. When the
1121 * interrupt endpoint is to be serviced, the xHC will consume
1122 * (at most) one TD. A TD (comprised of sg list entries) can
1123 * take several service intervals to transmit.
1125 return xhci_bulk_tx(udev, pipe, length, buffer);
1129 * submit the BULK type of request to the USB Device
1131 * @param udev pointer to the USB device
1132 * @param pipe contains the DIR_IN or OUT , devnum
1133 * @param buffer buffer to be read/written based on the request
1134 * @param length length of the buffer
1135 * @return returns 0 if successful else -1 on failure
1137 static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1138 void *buffer, int length)
1140 if (usb_pipetype(pipe) != PIPE_BULK) {
1141 printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1145 return xhci_bulk_tx(udev, pipe, length, buffer);
1149 * submit the control type of request to the Root hub/Device based on the devnum
1151 * @param udev pointer to the USB device
1152 * @param pipe contains the DIR_IN or OUT , devnum
1153 * @param buffer buffer to be read/written based on the request
1154 * @param length length of the buffer
1155 * @param setup Request type
1156 * @param root_portnr Root port number that this device is on
1157 * @return returns 0 if successful else -1 on failure
1159 static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
1160 void *buffer, int length,
1161 struct devrequest *setup, int root_portnr)
1163 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
1166 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1167 printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
1171 if (usb_pipedevice(pipe) == ctrl->rootdev)
1172 return xhci_submit_root(udev, pipe, buffer, setup);
1174 if (setup->request == USB_REQ_SET_ADDRESS &&
1175 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
1176 return xhci_address_device(udev, root_portnr);
1178 if (setup->request == USB_REQ_SET_CONFIGURATION &&
1179 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
1180 ret = xhci_set_configuration(udev);
1182 puts("Failed to configure xHCI endpoint\n");
1187 return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
1190 static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
1192 struct xhci_hccr *hccr;
1193 struct xhci_hcor *hcor;
1201 * Program the Number of Device Slots Enabled field in the CONFIG
1202 * register with the max value of slots the HC can handle.
1204 val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
1205 val2 = xhci_readl(&hcor->or_config);
1206 val |= (val2 & ~HCS_SLOTS_MASK);
1207 xhci_writel(&hcor->or_config, val);
1209 /* initializing xhci data structures */
1210 if (xhci_mem_init(ctrl, hccr, hcor) < 0)
1213 reg = xhci_readl(&hccr->cr_hcsparams1);
1214 descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
1215 HCS_MAX_PORTS_SHIFT);
1216 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
1218 /* Port Indicators */
1219 reg = xhci_readl(&hccr->cr_hccparams);
1220 if (HCS_INDICATOR(reg))
1221 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1222 | 0x80, &descriptor.hub.wHubCharacteristics);
1224 /* Port Power Control */
1226 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1227 | 0x01, &descriptor.hub.wHubCharacteristics);
1229 if (xhci_start(hcor)) {
1234 /* Zero'ing IRQ control register and IRQ pending register */
1235 xhci_writel(&ctrl->ir_set->irq_control, 0x0);
1236 xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
1238 reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
1239 printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
1244 static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1248 xhci_reset(ctrl->hcor);
1250 debug("// Disabling event ring interrupts\n");
1251 temp = xhci_readl(&ctrl->hcor->or_usbsts);
1252 xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1253 temp = xhci_readl(&ctrl->ir_set->irq_pending);
1254 xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
1259 #ifndef CONFIG_DM_USB
1260 int submit_control_msg(struct usb_device *udev, unsigned long pipe,
1261 void *buffer, int length, struct devrequest *setup)
1263 struct usb_device *hop = udev;
1266 while (hop->parent->parent)
1269 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1273 int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1276 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1279 int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1280 int length, int interval)
1282 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
1286 * Intialises the XHCI host controller
1287 * and allocates the necessary data structures
1289 * @param index index to the host controller data structure
1290 * @return pointer to the intialised controller
1292 int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1294 struct xhci_hccr *hccr;
1295 struct xhci_hcor *hcor;
1296 struct xhci_ctrl *ctrl;
1301 if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1304 if (xhci_reset(hcor) != 0)
1307 ctrl = &xhcic[index];
1312 ret = xhci_lowlevel_init(ctrl);
1318 *controller = &xhcic[index];
1325 * Stops the XHCI host controller
1326 * and cleans up all the related data structures
1328 * @param index index to the host controller data structure
1331 int usb_lowlevel_stop(int index)
1333 struct xhci_ctrl *ctrl = (xhcic + index);
1336 xhci_lowlevel_stop(ctrl);
1337 xhci_hcd_stop(index);
1343 #endif /* CONFIG_DM_USB */
1345 #ifdef CONFIG_DM_USB
1347 static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1348 unsigned long pipe, void *buffer, int length,
1349 struct devrequest *setup)
1351 struct usb_device *uhop;
1352 struct udevice *hub;
1353 int root_portnr = 0;
1355 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1356 dev->name, udev, udev->dev->name, udev->portnr);
1358 if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1359 /* Figure out our port number on the root hub */
1360 if (usb_hub_is_root_hub(hub)) {
1361 root_portnr = udev->portnr;
1363 while (!usb_hub_is_root_hub(hub->parent))
1365 uhop = dev_get_parent_priv(hub);
1366 root_portnr = uhop->portnr;
1370 struct usb_device *hop = udev;
1373 while (hop->parent->parent)
1376 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1380 static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1381 unsigned long pipe, void *buffer, int length)
1383 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1384 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1387 static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1388 unsigned long pipe, void *buffer, int length,
1391 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1392 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval);
1395 static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1397 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1398 return _xhci_alloc_device(udev);
1401 static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1403 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1404 struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1405 struct xhci_virt_device *virt_dev;
1406 struct xhci_input_control_ctx *ctrl_ctx;
1407 struct xhci_container_ctx *out_ctx;
1408 struct xhci_container_ctx *in_ctx;
1409 struct xhci_slot_ctx *slot_ctx;
1410 int slot_id = udev->slot_id;
1411 unsigned think_time;
1413 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1415 /* Ignore root hubs */
1416 if (usb_hub_is_root_hub(udev->dev))
1419 virt_dev = ctrl->devs[slot_id];
1422 out_ctx = virt_dev->out_ctx;
1423 in_ctx = virt_dev->in_ctx;
1425 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1426 /* Initialize the input context control */
1427 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1428 ctrl_ctx->drop_flags = 0;
1430 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1433 xhci_slot_copy(ctrl, in_ctx, out_ctx);
1434 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1436 /* Update hub related fields */
1437 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
1439 * refer to section 6.2.2: MTT should be 0 for full speed hub,
1440 * but it may be already set to 1 when setup an xHCI virtual
1441 * device, so clear it anyway.
1444 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1445 else if (udev->speed == USB_SPEED_FULL)
1446 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
1447 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1449 * Set TT think time - convert from ns to FS bit times.
1450 * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1452 * 0 = 8 FS bit times, 1 = 16 FS bit times,
1453 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1455 * This field shall be 0 if the device is not a high-spped hub.
1457 think_time = hub->tt.think_time;
1458 if (think_time != 0)
1459 think_time = (think_time / 666) - 1;
1460 if (udev->speed == USB_SPEED_HIGH)
1461 slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
1462 slot_ctx->dev_state = 0;
1464 return xhci_configure_endpoints(udev, false);
1467 static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1470 * xHCD allocates one segment which includes 64 TRBs for each endpoint
1471 * and the last TRB in this segment is configured as a link TRB to form
1472 * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1473 * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1474 * Hence the maximum number of TRBs we can use in one transfer is 62.
1476 *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1481 int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1482 struct xhci_hcor *hcor)
1484 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1485 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1488 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1494 * XHCI needs to issue a Address device command to setup
1495 * proper device context structures, before it can interact
1496 * with the device. So a get_descriptor will fail before any
1497 * of that is done for XHCI unlike EHCI.
1499 priv->desc_before_addr = false;
1501 ret = xhci_reset(hcor);
1507 ret = xhci_lowlevel_init(ctrl);
1514 debug("%s: failed, ret=%d\n", __func__, ret);
1518 int xhci_deregister(struct udevice *dev)
1520 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1522 xhci_lowlevel_stop(ctrl);
1528 struct dm_usb_ops xhci_usb_ops = {
1529 .control = xhci_submit_control_msg,
1530 .bulk = xhci_submit_bulk_msg,
1531 .interrupt = xhci_submit_int_msg,
1532 .alloc_device = xhci_alloc_device,
1533 .update_hub_device = xhci_update_hub_device,
1534 .get_max_xfer_size = xhci_get_max_xfer_size,