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.
25 #include <dm/device_compat.h>
31 #include <asm/byteorder.h>
32 #include <asm/cache.h>
33 #include <asm/unaligned.h>
34 #include <linux/bitops.h>
35 #include <linux/bug.h>
36 #include <linux/delay.h>
37 #include <linux/errno.h>
38 #include <linux/iopoll.h>
40 static struct descriptor
{
41 struct usb_hub_descriptor hub
;
42 struct usb_device_descriptor device
;
43 struct usb_config_descriptor config
;
44 struct usb_interface_descriptor interface
;
45 struct usb_endpoint_descriptor endpoint
;
46 struct usb_ss_ep_comp_descriptor ep_companion
;
47 } __attribute__ ((packed
)) descriptor
= {
49 0xc, /* bDescLength */
50 0x2a, /* bDescriptorType: hub descriptor */
51 2, /* bNrPorts -- runtime modified */
52 cpu_to_le16(0x8), /* wHubCharacteristics */
53 10, /* bPwrOn2PwrGood */
54 0, /* bHubCntrCurrent */
55 { /* Device removable */
56 } /* at most 7 ports! XXX */
60 1, /* bDescriptorType: UDESC_DEVICE */
61 cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
62 9, /* bDeviceClass: UDCLASS_HUB */
63 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
64 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
65 9, /* bMaxPacketSize: 512 bytes 2^9 */
66 0x0000, /* idVendor */
67 0x0000, /* idProduct */
68 cpu_to_le16(0x0100), /* bcdDevice */
69 1, /* iManufacturer */
71 0, /* iSerialNumber */
72 1 /* bNumConfigurations: 1 */
76 2, /* bDescriptorType: UDESC_CONFIG */
77 cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
78 1, /* bNumInterface */
79 1, /* bConfigurationValue */
80 0, /* iConfiguration */
81 0x40, /* bmAttributes: UC_SELF_POWER */
86 4, /* bDescriptorType: UDESC_INTERFACE */
87 0, /* bInterfaceNumber */
88 0, /* bAlternateSetting */
89 1, /* bNumEndpoints */
90 9, /* bInterfaceClass: UICLASS_HUB */
91 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
92 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
97 5, /* bDescriptorType: UDESC_ENDPOINT */
98 0x81, /* bEndpointAddress: IN endpoint 1 */
99 3, /* bmAttributes: UE_INTERRUPT */
100 8, /* wMaxPacketSize */
104 0x06, /* ss_bLength */
105 0x30, /* ss_bDescriptorType: SS EP Companion */
106 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
107 /* ss_bmAttributes: 1 packet per service interval */
109 /* ss_wBytesPerInterval: 15 bits for max 15 ports */
114 struct xhci_ctrl
*xhci_get_ctrl(struct usb_device
*udev
)
118 /* Find the USB controller */
119 for (dev
= udev
->dev
;
120 device_get_uclass_id(dev
) != UCLASS_USB
;
123 return dev_get_priv(dev
);
127 * Waits for as per specified amount of time
128 * for the "result" to match with "done"
130 * @param ptr pointer to the register to be read
131 * @param mask mask for the value read
132 * @param done value to be campared with result
133 * @param usec time to wait till
134 * Return: 0 if handshake is success else < 0 on failure
137 handshake(uint32_t volatile *ptr
, uint32_t mask
, uint32_t done
, int usec
)
142 ret
= readx_poll_sleep_timeout(xhci_readl
, ptr
, result
,
143 (result
& mask
) == done
|| result
== U32_MAX
,
145 if (result
== U32_MAX
) /* card removed */
152 * Set the run bit and wait for the host to be running.
154 * @param hcor pointer to host controller operation registers
155 * Return: status of the Handshake
157 static int xhci_start(struct xhci_hcor
*hcor
)
162 puts("Starting the controller\n");
163 temp
= xhci_readl(&hcor
->or_usbcmd
);
165 xhci_writel(&hcor
->or_usbcmd
, temp
);
168 * Wait for the HCHalted Status bit to be 0 to indicate the host is
171 ret
= handshake(&hcor
->or_usbsts
, STS_HALT
, 0, XHCI_MAX_HALT_USEC
);
173 debug("Host took too long to start, "
174 "waited %u microseconds.\n",
180 * Resets the XHCI Controller
182 * @param hcor pointer to host controller operation registers
183 * Return: -EBUSY if XHCI Controller is not halted else status of handshake
185 static int xhci_reset(struct xhci_hcor
*hcor
)
191 /* Halting the Host first */
192 debug("// Halt the HC: %p\n", hcor
);
193 state
= xhci_readl(&hcor
->or_usbsts
) & STS_HALT
;
195 cmd
= xhci_readl(&hcor
->or_usbcmd
);
197 xhci_writel(&hcor
->or_usbcmd
, cmd
);
200 ret
= handshake(&hcor
->or_usbsts
,
201 STS_HALT
, STS_HALT
, XHCI_MAX_HALT_USEC
);
203 printf("Host not halted after %u microseconds.\n",
208 debug("// Reset the HC\n");
209 cmd
= xhci_readl(&hcor
->or_usbcmd
);
211 xhci_writel(&hcor
->or_usbcmd
, cmd
);
213 ret
= handshake(&hcor
->or_usbcmd
, CMD_RESET
, 0, XHCI_MAX_RESET_USEC
);
218 * xHCI cannot write to any doorbells or operational registers other
219 * than status until the "Controller Not Ready" flag is cleared.
221 return handshake(&hcor
->or_usbsts
, STS_CNR
, 0, XHCI_MAX_RESET_USEC
);
225 * Used for passing endpoint bitmasks between the core and HCDs.
226 * Find the index for an endpoint given its descriptor.
227 * Use the return value to right shift 1 for the bitmask.
229 * Index = (epnum * 2) + direction - 1,
230 * where direction = 0 for OUT, 1 for IN.
231 * For control endpoints, the IN index is used (OUT index is unused), so
232 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
234 * @param desc USB enpdoint Descriptor
235 * Return: index of the Endpoint
237 static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor
*desc
)
241 if (usb_endpoint_xfer_control(desc
))
242 index
= (unsigned int)(usb_endpoint_num(desc
) * 2);
244 index
= (unsigned int)((usb_endpoint_num(desc
) * 2) -
245 (usb_endpoint_dir_in(desc
) ? 0 : 1));
251 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
252 * microframes, rounded down to nearest power of 2.
254 static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval
,
255 unsigned int min_exponent
,
256 unsigned int max_exponent
)
258 unsigned int interval
;
260 interval
= fls(desc_interval
) - 1;
261 interval
= clamp_val(interval
, min_exponent
, max_exponent
);
262 if ((1 << interval
) != desc_interval
)
263 debug("rounding interval to %d microframes, "\
264 "ep desc says %d microframes\n",
265 1 << interval
, desc_interval
);
270 static unsigned int xhci_parse_microframe_interval(struct usb_device
*udev
,
271 struct usb_endpoint_descriptor
*endpt_desc
)
273 if (endpt_desc
->bInterval
== 0)
276 return xhci_microframes_to_exponent(endpt_desc
->bInterval
, 0, 15);
279 static unsigned int xhci_parse_frame_interval(struct usb_device
*udev
,
280 struct usb_endpoint_descriptor
*endpt_desc
)
282 return xhci_microframes_to_exponent(endpt_desc
->bInterval
* 8, 3, 10);
286 * Convert interval expressed as 2^(bInterval - 1) == interval into
287 * straight exponent value 2^n == interval.
289 static unsigned int xhci_parse_exponent_interval(struct usb_device
*udev
,
290 struct usb_endpoint_descriptor
*endpt_desc
)
292 unsigned int interval
;
294 interval
= clamp_val(endpt_desc
->bInterval
, 1, 16) - 1;
295 if (interval
!= endpt_desc
->bInterval
- 1)
296 debug("ep %#x - rounding interval to %d %sframes\n",
297 endpt_desc
->bEndpointAddress
, 1 << interval
,
298 udev
->speed
== USB_SPEED_FULL
? "" : "micro");
300 if (udev
->speed
== USB_SPEED_FULL
) {
302 * Full speed isoc endpoints specify interval in frames,
303 * not microframes. We are using microframes everywhere,
304 * so adjust accordingly.
306 interval
+= 3; /* 1 frame = 2^3 uframes */
313 * Return the polling or NAK interval.
315 * The polling interval is expressed in "microframes". If xHCI's Interval field
316 * is set to N, it will service the endpoint every 2^(Interval)*125us.
318 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
321 static unsigned int xhci_get_endpoint_interval(struct usb_device
*udev
,
322 struct usb_endpoint_descriptor
*endpt_desc
)
324 unsigned int interval
= 0;
326 switch (udev
->speed
) {
329 if (usb_endpoint_xfer_control(endpt_desc
) ||
330 usb_endpoint_xfer_bulk(endpt_desc
)) {
331 interval
= xhci_parse_microframe_interval(udev
,
335 /* Fall through - SS and HS isoc/int have same decoding */
337 case USB_SPEED_SUPER
:
338 if (usb_endpoint_xfer_int(endpt_desc
) ||
339 usb_endpoint_xfer_isoc(endpt_desc
)) {
340 interval
= xhci_parse_exponent_interval(udev
,
346 if (usb_endpoint_xfer_isoc(endpt_desc
)) {
347 interval
= xhci_parse_exponent_interval(udev
,
352 * Fall through for interrupt endpoint interval decoding
353 * since it uses the same rules as low speed interrupt
358 if (usb_endpoint_xfer_int(endpt_desc
) ||
359 usb_endpoint_xfer_isoc(endpt_desc
)) {
360 interval
= xhci_parse_frame_interval(udev
, endpt_desc
);
372 * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
373 * High speed endpoint descriptors can define "the number of additional
374 * transaction opportunities per microframe", but that goes in the Max Burst
375 * endpoint context field.
377 static u32
xhci_get_endpoint_mult(struct usb_device
*udev
,
378 struct usb_endpoint_descriptor
*endpt_desc
,
379 struct usb_ss_ep_comp_descriptor
*ss_ep_comp_desc
)
381 if (udev
->speed
< USB_SPEED_SUPER
||
382 !usb_endpoint_xfer_isoc(endpt_desc
))
385 return ss_ep_comp_desc
->bmAttributes
;
388 static u32
xhci_get_endpoint_max_burst(struct usb_device
*udev
,
389 struct usb_endpoint_descriptor
*endpt_desc
,
390 struct usb_ss_ep_comp_descriptor
*ss_ep_comp_desc
)
392 /* Super speed and Plus have max burst in ep companion desc */
393 if (udev
->speed
>= USB_SPEED_SUPER
)
394 return ss_ep_comp_desc
->bMaxBurst
;
396 if (udev
->speed
== USB_SPEED_HIGH
&&
397 (usb_endpoint_xfer_isoc(endpt_desc
) ||
398 usb_endpoint_xfer_int(endpt_desc
)))
399 return usb_endpoint_maxp_mult(endpt_desc
) - 1;
405 * Return the maximum endpoint service interval time (ESIT) payload.
406 * Basically, this is the maxpacket size, multiplied by the burst size
409 static u32
xhci_get_max_esit_payload(struct usb_device
*udev
,
410 struct usb_endpoint_descriptor
*endpt_desc
,
411 struct usb_ss_ep_comp_descriptor
*ss_ep_comp_desc
)
416 /* Only applies for interrupt or isochronous endpoints */
417 if (usb_endpoint_xfer_control(endpt_desc
) ||
418 usb_endpoint_xfer_bulk(endpt_desc
))
421 /* SuperSpeed Isoc ep with less than 48k per esit */
422 if (udev
->speed
>= USB_SPEED_SUPER
)
423 return le16_to_cpu(ss_ep_comp_desc
->wBytesPerInterval
);
425 max_packet
= usb_endpoint_maxp(endpt_desc
);
426 max_burst
= usb_endpoint_maxp_mult(endpt_desc
);
428 /* A 0 in max burst means 1 transfer per ESIT */
429 return max_packet
* max_burst
;
433 * Issue a configure endpoint command or evaluate context command
434 * and wait for it to finish.
436 * @param udev pointer to the Device Data Structure
437 * @param ctx_change flag to indicate the Context has changed or NOT
438 * Return: 0 on success, -1 on failure
440 static int xhci_configure_endpoints(struct usb_device
*udev
, bool ctx_change
)
442 struct xhci_container_ctx
*in_ctx
;
443 struct xhci_virt_device
*virt_dev
;
444 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
445 union xhci_trb
*event
;
447 virt_dev
= ctrl
->devs
[udev
->slot_id
];
448 in_ctx
= virt_dev
->in_ctx
;
450 xhci_flush_cache((uintptr_t)in_ctx
->bytes
, in_ctx
->size
);
451 xhci_queue_command(ctrl
, in_ctx
->dma
, udev
->slot_id
, 0,
452 ctx_change
? TRB_EVAL_CONTEXT
: TRB_CONFIG_EP
);
453 event
= xhci_wait_for_event(ctrl
, TRB_COMPLETION
);
457 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event
->event_cmd
.flags
))
460 switch (GET_COMP_CODE(le32_to_cpu(event
->event_cmd
.status
))) {
462 debug("Successful %s command\n",
463 ctx_change
? "Evaluate Context" : "Configure Endpoint");
466 printf("ERROR: %s command returned completion code %d.\n",
467 ctx_change
? "Evaluate Context" : "Configure Endpoint",
468 GET_COMP_CODE(le32_to_cpu(event
->event_cmd
.status
)));
472 xhci_acknowledge_event(ctrl
);
478 * Fill endpoint contexts for interface descriptor ifdesc.
480 * @param udev pointer to the USB device structure
481 * @param ctrl pointer to the xhci pravte device structure
482 * @param virt_dev pointer to the xhci virtual device structure
483 * @param ifdesc pointer to the USB interface config descriptor
484 * Return: returns the status of xhci_init_ep_contexts_if
486 static int xhci_init_ep_contexts_if(struct usb_device
*udev
,
487 struct xhci_ctrl
*ctrl
,
488 struct xhci_virt_device
*virt_dev
,
489 struct usb_interface
*ifdesc
492 struct xhci_ep_ctx
*ep_ctx
[MAX_EP_CTX_NUM
];
496 unsigned int ep_type
;
498 u32 max_esit_payload
;
499 unsigned int interval
;
501 unsigned int max_burst
;
502 unsigned int avg_trb_len
;
503 unsigned int err_count
= 0;
504 int num_of_ep
= ifdesc
->no_of_ep
;
506 for (cur_ep
= 0; cur_ep
< num_of_ep
; cur_ep
++) {
507 struct usb_endpoint_descriptor
*endpt_desc
= NULL
;
508 struct usb_ss_ep_comp_descriptor
*ss_ep_comp_desc
= NULL
;
510 endpt_desc
= &ifdesc
->ep_desc
[cur_ep
];
511 ss_ep_comp_desc
= &ifdesc
->ss_ep_comp_desc
[cur_ep
];
515 * Get values to fill the endpoint context, mostly from ep
516 * descriptor. The average TRB buffer lengt for bulk endpoints
517 * is unclear as we have no clue on scatter gather list entry
518 * size. For Isoc and Int, set it to max available.
519 * See xHCI 1.1 spec 4.14.1.1 for details.
521 max_esit_payload
= xhci_get_max_esit_payload(udev
, endpt_desc
,
523 interval
= xhci_get_endpoint_interval(udev
, endpt_desc
);
524 mult
= xhci_get_endpoint_mult(udev
, endpt_desc
,
526 max_burst
= xhci_get_endpoint_max_burst(udev
, endpt_desc
,
528 avg_trb_len
= max_esit_payload
;
530 ep_index
= xhci_get_ep_index(endpt_desc
);
531 ep_ctx
[ep_index
] = xhci_get_ep_ctx(ctrl
, virt_dev
->in_ctx
,
534 /* Allocate the ep rings */
535 virt_dev
->eps
[ep_index
].ring
= xhci_ring_alloc(ctrl
, 1, true);
536 if (!virt_dev
->eps
[ep_index
].ring
)
539 /*NOTE: ep_desc[0] actually represents EP1 and so on */
540 dir
= (((endpt_desc
->bEndpointAddress
) & (0x80)) >> 7);
541 ep_type
= (((endpt_desc
->bmAttributes
) & (0x3)) | (dir
<< 2));
543 ep_ctx
[ep_index
]->ep_info
=
544 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload
) |
545 EP_INTERVAL(interval
) | EP_MULT(mult
));
547 ep_ctx
[ep_index
]->ep_info2
= cpu_to_le32(EP_TYPE(ep_type
));
548 ep_ctx
[ep_index
]->ep_info2
|=
549 cpu_to_le32(MAX_PACKET
550 (get_unaligned(&endpt_desc
->wMaxPacketSize
)));
552 /* Allow 3 retries for everything but isoc, set CErr = 3 */
553 if (!usb_endpoint_xfer_isoc(endpt_desc
))
555 ep_ctx
[ep_index
]->ep_info2
|=
556 cpu_to_le32(MAX_BURST(max_burst
) |
557 ERROR_COUNT(err_count
));
559 trb_64
= xhci_trb_virt_to_dma(virt_dev
->eps
[ep_index
].ring
->enq_seg
,
560 virt_dev
->eps
[ep_index
].ring
->enqueue
);
561 ep_ctx
[ep_index
]->deq
= cpu_to_le64(trb_64
|
562 virt_dev
->eps
[ep_index
].ring
->cycle_state
);
566 * 'Average TRB Length' should be 8 for control endpoints.
568 if (usb_endpoint_xfer_control(endpt_desc
))
570 ep_ctx
[ep_index
]->tx_info
=
571 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload
) |
572 EP_AVG_TRB_LENGTH(avg_trb_len
));
575 * The MediaTek xHCI defines some extra SW parameters which
576 * are put into reserved DWs in Slot and Endpoint Contexts
577 * for synchronous endpoints.
579 if (ctrl
->quirks
& XHCI_MTK_HOST
) {
580 ep_ctx
[ep_index
]->reserved
[0] =
581 cpu_to_le32(EP_BPKTS(1) | EP_BBM(1));
589 * Configure the endpoint, programming the device contexts.
591 * @param udev pointer to the USB device structure
592 * Return: returns the status of the xhci_configure_endpoints
594 static int xhci_set_configuration(struct usb_device
*udev
)
596 struct xhci_container_ctx
*out_ctx
;
597 struct xhci_container_ctx
*in_ctx
;
598 struct xhci_input_control_ctx
*ctrl_ctx
;
599 struct xhci_slot_ctx
*slot_ctx
;
603 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
606 int slot_id
= udev
->slot_id
;
607 struct xhci_virt_device
*virt_dev
= ctrl
->devs
[slot_id
];
608 struct usb_interface
*ifdesc
;
610 unsigned int max_ifnum
= min((unsigned int)USB_MAX_ACTIVE_INTERFACES
,
611 (unsigned int)udev
->config
.no_of_if
);
613 out_ctx
= virt_dev
->out_ctx
;
614 in_ctx
= virt_dev
->in_ctx
;
616 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
617 /* Initialize the input context control */
618 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
);
619 ctrl_ctx
->drop_flags
= 0;
621 for (ifnum
= 0; ifnum
< max_ifnum
; ifnum
++) {
622 ifdesc
= &udev
->config
.if_desc
[ifnum
];
623 num_of_ep
= ifdesc
->no_of_ep
;
624 /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
625 for (cur_ep
= 0; cur_ep
< num_of_ep
; cur_ep
++) {
626 ep_flag
= xhci_get_ep_index(&ifdesc
->ep_desc
[cur_ep
]);
627 ctrl_ctx
->add_flags
|= cpu_to_le32(1 << (ep_flag
+ 1));
628 if (max_ep_flag
< ep_flag
)
629 max_ep_flag
= ep_flag
;
633 xhci_inval_cache((uintptr_t)out_ctx
->bytes
, out_ctx
->size
);
636 xhci_slot_copy(ctrl
, in_ctx
, out_ctx
);
637 slot_ctx
= xhci_get_slot_ctx(ctrl
, in_ctx
);
638 slot_ctx
->dev_info
&= ~(cpu_to_le32(LAST_CTX_MASK
));
639 slot_ctx
->dev_info
|= cpu_to_le32(LAST_CTX(max_ep_flag
+ 1) | 0);
641 xhci_endpoint_copy(ctrl
, in_ctx
, out_ctx
, 0);
643 /* filling up ep contexts */
644 for (ifnum
= 0; ifnum
< max_ifnum
; ifnum
++) {
645 ifdesc
= &udev
->config
.if_desc
[ifnum
];
646 err
= xhci_init_ep_contexts_if(udev
, ctrl
, virt_dev
, ifdesc
);
651 return xhci_configure_endpoints(udev
, false);
655 * Issue an Address Device command (which will issue a SetAddress request to
658 * @param udev pointer to the Device Data Structure
659 * Return: 0 if successful else error code on failure
661 static int xhci_address_device(struct usb_device
*udev
, int root_portnr
)
664 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
665 struct xhci_slot_ctx
*slot_ctx
;
666 struct xhci_input_control_ctx
*ctrl_ctx
;
667 struct xhci_virt_device
*virt_dev
;
668 int slot_id
= udev
->slot_id
;
669 union xhci_trb
*event
;
671 virt_dev
= ctrl
->devs
[slot_id
];
674 * This is the first Set Address since device plug-in
675 * so setting up the slot context.
677 debug("Setting up addressable devices %p\n", ctrl
->dcbaa
);
678 xhci_setup_addressable_virt_dev(ctrl
, udev
, root_portnr
);
680 ctrl_ctx
= xhci_get_input_control_ctx(virt_dev
->in_ctx
);
681 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
| EP0_FLAG
);
682 ctrl_ctx
->drop_flags
= 0;
684 xhci_queue_command(ctrl
, virt_dev
->in_ctx
->dma
,
685 slot_id
, 0, TRB_ADDR_DEV
);
686 event
= xhci_wait_for_event(ctrl
, TRB_COMPLETION
);
690 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event
->event_cmd
.flags
)) != slot_id
);
692 switch (GET_COMP_CODE(le32_to_cpu(event
->event_cmd
.status
))) {
695 printf("Setup ERROR: address device command for slot %d.\n",
700 puts("Device not responding to set address.\n");
704 puts("ERROR: Incompatible device"
705 "for address device command.\n");
709 debug("Successful Address Device command\n");
713 printf("ERROR: unexpected command completion code 0x%x.\n",
714 GET_COMP_CODE(le32_to_cpu(event
->event_cmd
.status
)));
719 xhci_acknowledge_event(ctrl
);
723 * TODO: Unsuccessful Address Device command shall leave the
724 * slot in default state. So, issue Disable Slot command now.
728 xhci_inval_cache((uintptr_t)virt_dev
->out_ctx
->bytes
,
729 virt_dev
->out_ctx
->size
);
730 slot_ctx
= xhci_get_slot_ctx(ctrl
, virt_dev
->out_ctx
);
732 debug("xHC internal address is: %d\n",
733 le32_to_cpu(slot_ctx
->dev_state
) & DEV_ADDR_MASK
);
739 * Issue Enable slot command to the controller to allocate
740 * device slot and assign the slot id. It fails if the xHC
741 * ran out of device slots, the Enable Slot command timed out,
742 * or allocating memory failed.
744 * @param udev pointer to the Device Data Structure
745 * Return: Returns 0 on succes else return error code on failure
747 static int _xhci_alloc_device(struct usb_device
*udev
)
749 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
750 union xhci_trb
*event
;
754 * Root hub will be first device to be initailized.
755 * If this device is root-hub, don't do any xHC related
758 if (ctrl
->rootdev
== 0) {
759 udev
->speed
= USB_SPEED_SUPER
;
763 xhci_queue_command(ctrl
, 0, 0, 0, TRB_ENABLE_SLOT
);
764 event
= xhci_wait_for_event(ctrl
, TRB_COMPLETION
);
768 BUG_ON(GET_COMP_CODE(le32_to_cpu(event
->event_cmd
.status
))
771 udev
->slot_id
= TRB_TO_SLOT_ID(le32_to_cpu(event
->event_cmd
.flags
));
773 xhci_acknowledge_event(ctrl
);
775 ret
= xhci_alloc_virt_device(ctrl
, udev
->slot_id
);
778 * TODO: Unsuccessful Address Device command shall leave
779 * the slot in default. So, issue Disable Slot command now.
781 puts("Could not allocate xHCI USB device data structures\n");
789 * Full speed devices may have a max packet size greater than 8 bytes, but the
790 * USB core doesn't know that until it reads the first 8 bytes of the
791 * descriptor. If the usb_device's max packet size changes after that point,
792 * we need to issue an evaluate context command and wait on it.
794 * @param udev pointer to the Device Data Structure
795 * Return: returns the status of the xhci_configure_endpoints
797 int xhci_check_maxpacket(struct usb_device
*udev
)
799 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
800 unsigned int slot_id
= udev
->slot_id
;
801 int ep_index
= 0; /* control endpoint */
802 struct xhci_container_ctx
*in_ctx
;
803 struct xhci_container_ctx
*out_ctx
;
804 struct xhci_input_control_ctx
*ctrl_ctx
;
805 struct xhci_ep_ctx
*ep_ctx
;
807 int hw_max_packet_size
;
810 out_ctx
= ctrl
->devs
[slot_id
]->out_ctx
;
811 xhci_inval_cache((uintptr_t)out_ctx
->bytes
, out_ctx
->size
);
813 ep_ctx
= xhci_get_ep_ctx(ctrl
, out_ctx
, ep_index
);
814 hw_max_packet_size
= MAX_PACKET_DECODED(le32_to_cpu(ep_ctx
->ep_info2
));
815 max_packet_size
= udev
->epmaxpacketin
[0];
816 if (hw_max_packet_size
!= max_packet_size
) {
817 debug("Max Packet Size for ep 0 changed.\n");
818 debug("Max packet size in usb_device = %d\n", max_packet_size
);
819 debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size
);
820 debug("Issuing evaluate context command.\n");
822 /* Set up the modified control endpoint 0 */
823 xhci_endpoint_copy(ctrl
, ctrl
->devs
[slot_id
]->in_ctx
,
824 ctrl
->devs
[slot_id
]->out_ctx
, ep_index
);
825 in_ctx
= ctrl
->devs
[slot_id
]->in_ctx
;
826 ep_ctx
= xhci_get_ep_ctx(ctrl
, in_ctx
, ep_index
);
827 ep_ctx
->ep_info2
&= cpu_to_le32(~MAX_PACKET(MAX_PACKET_MASK
));
828 ep_ctx
->ep_info2
|= cpu_to_le32(MAX_PACKET(max_packet_size
));
831 * Set up the input context flags for the command
832 * FIXME: This won't work if a non-default control endpoint
833 * changes max packet sizes.
835 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
836 ctrl_ctx
->add_flags
= cpu_to_le32(EP0_FLAG
);
837 ctrl_ctx
->drop_flags
= 0;
839 ret
= xhci_configure_endpoints(udev
, true);
845 * Clears the Change bits of the Port Status Register
847 * @param wValue request value
848 * @param wIndex request index
849 * @param addr address of posrt status register
850 * @param port_status state of port status register
853 static void xhci_clear_port_change_bit(u16 wValue
,
854 u16 wIndex
, volatile uint32_t *addr
, u32 port_status
)
856 char *port_change_bit
;
860 case USB_PORT_FEAT_C_RESET
:
862 port_change_bit
= "reset";
864 case USB_PORT_FEAT_C_CONNECTION
:
866 port_change_bit
= "connect";
868 case USB_PORT_FEAT_C_OVER_CURRENT
:
870 port_change_bit
= "over-current";
872 case USB_PORT_FEAT_C_ENABLE
:
874 port_change_bit
= "enable/disable";
876 case USB_PORT_FEAT_C_SUSPEND
:
878 port_change_bit
= "suspend/resume";
881 /* Should never happen */
885 /* Change bits are all write 1 to clear */
886 xhci_writel(addr
, port_status
| status
);
888 port_status
= xhci_readl(addr
);
889 debug("clear port %s change, actual port %d status = 0x%x\n",
890 port_change_bit
, wIndex
, port_status
);
894 * Save Read Only (RO) bits and save read/write bits where
895 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
896 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
898 * @param state state of the Port Status and Control Regsiter
899 * Return: a value that would result in the port being in the
900 * same state, if the value was written to the port
901 * status control register.
903 static u32
xhci_port_state_to_neutral(u32 state
)
905 /* Save read-only status and port state */
906 return (state
& XHCI_PORT_RO
) | (state
& XHCI_PORT_RWS
);
910 * Submits the Requests to the XHCI Host Controller
912 * @param udev pointer to the USB device structure
913 * @param pipe contains the DIR_IN or OUT , devnum
914 * @param buffer buffer to be read/written based on the request
915 * Return: returns 0 if successful else -1 on failure
917 static int xhci_submit_root(struct usb_device
*udev
, unsigned long pipe
,
918 void *buffer
, struct devrequest
*req
)
925 volatile uint32_t *status_reg
;
926 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
927 struct xhci_hccr
*hccr
= ctrl
->hccr
;
928 struct xhci_hcor
*hcor
= ctrl
->hcor
;
929 int max_ports
= HCS_MAX_PORTS(xhci_readl(&hccr
->cr_hcsparams1
));
931 if ((req
->requesttype
& USB_RT_PORT
) &&
932 le16_to_cpu(req
->index
) > max_ports
) {
933 printf("The request port(%d) exceeds maximum port number\n",
934 le16_to_cpu(req
->index
) - 1);
938 status_reg
= (volatile uint32_t *)
939 (&hcor
->portregs
[le16_to_cpu(req
->index
) - 1].or_portsc
);
942 typeReq
= req
->request
| req
->requesttype
<< 8;
945 case DeviceRequest
| USB_REQ_GET_DESCRIPTOR
:
946 switch (le16_to_cpu(req
->value
) >> 8) {
948 debug("USB_DT_DEVICE request\n");
949 srcptr
= &descriptor
.device
;
953 debug("USB_DT_CONFIG config\n");
954 srcptr
= &descriptor
.config
;
958 debug("USB_DT_STRING config\n");
959 switch (le16_to_cpu(req
->value
) & 0xff) {
960 case 0: /* Language */
961 srcptr
= "\4\3\11\4";
964 case 1: /* Vendor String */
965 srcptr
= "\16\3U\0-\0B\0o\0o\0t\0";
968 case 2: /* Product Name */
969 srcptr
= "\52\3X\0H\0C\0I\0 "
971 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
975 printf("unknown value DT_STRING %x\n",
976 le16_to_cpu(req
->value
));
981 printf("unknown value %x\n", le16_to_cpu(req
->value
));
985 case USB_REQ_GET_DESCRIPTOR
| ((USB_DIR_IN
| USB_RT_HUB
) << 8):
986 switch (le16_to_cpu(req
->value
) >> 8) {
989 debug("USB_DT_HUB config\n");
990 srcptr
= &ctrl
->hub_desc
;
994 printf("unknown value %x\n", le16_to_cpu(req
->value
));
998 case USB_REQ_SET_ADDRESS
| (USB_RECIP_DEVICE
<< 8):
999 debug("USB_REQ_SET_ADDRESS\n");
1000 ctrl
->rootdev
= le16_to_cpu(req
->value
);
1002 case DeviceOutRequest
| USB_REQ_SET_CONFIGURATION
:
1005 case USB_REQ_GET_STATUS
| ((USB_DIR_IN
| USB_RT_HUB
) << 8):
1006 tmpbuf
[0] = 1; /* USB_STATUS_SELFPOWERED */
1011 case USB_REQ_GET_STATUS
| ((USB_RT_PORT
| USB_DIR_IN
) << 8):
1012 memset(tmpbuf
, 0, 4);
1013 reg
= xhci_readl(status_reg
);
1014 if (reg
& PORT_CONNECT
) {
1015 tmpbuf
[0] |= USB_PORT_STAT_CONNECTION
;
1016 switch (reg
& DEV_SPEED_MASK
) {
1018 debug("SPEED = FULLSPEED\n");
1021 debug("SPEED = LOWSPEED\n");
1022 tmpbuf
[1] |= USB_PORT_STAT_LOW_SPEED
>> 8;
1025 debug("SPEED = HIGHSPEED\n");
1026 tmpbuf
[1] |= USB_PORT_STAT_HIGH_SPEED
>> 8;
1029 debug("SPEED = SUPERSPEED\n");
1030 tmpbuf
[1] |= USB_PORT_STAT_SUPER_SPEED
>> 8;
1035 tmpbuf
[0] |= USB_PORT_STAT_ENABLE
;
1036 if ((reg
& PORT_PLS_MASK
) == XDEV_U3
)
1037 tmpbuf
[0] |= USB_PORT_STAT_SUSPEND
;
1039 tmpbuf
[0] |= USB_PORT_STAT_OVERCURRENT
;
1040 if (reg
& PORT_RESET
)
1041 tmpbuf
[0] |= USB_PORT_STAT_RESET
;
1042 if (reg
& PORT_POWER
)
1044 * XXX: This Port power bit (for USB 3.0 hub)
1045 * we are faking in USB 2.0 hub port status;
1046 * since there's a change in bit positions in
1048 * USB 2.0 port status PP is at position[8]
1049 * USB 3.0 port status PP is at position[9]
1050 * So, we are still keeping it at position [8]
1052 tmpbuf
[1] |= USB_PORT_STAT_POWER
>> 8;
1054 tmpbuf
[2] |= USB_PORT_STAT_C_CONNECTION
;
1056 tmpbuf
[2] |= USB_PORT_STAT_C_ENABLE
;
1058 tmpbuf
[2] |= USB_PORT_STAT_C_OVERCURRENT
;
1060 tmpbuf
[2] |= USB_PORT_STAT_C_RESET
;
1065 case USB_REQ_SET_FEATURE
| ((USB_DIR_OUT
| USB_RT_PORT
) << 8):
1066 reg
= xhci_readl(status_reg
);
1067 reg
= xhci_port_state_to_neutral(reg
);
1068 switch (le16_to_cpu(req
->value
)) {
1069 case USB_PORT_FEAT_ENABLE
:
1071 xhci_writel(status_reg
, reg
);
1073 case USB_PORT_FEAT_POWER
:
1075 xhci_writel(status_reg
, reg
);
1077 case USB_PORT_FEAT_RESET
:
1079 xhci_writel(status_reg
, reg
);
1082 printf("unknown feature %x\n", le16_to_cpu(req
->value
));
1086 case USB_REQ_CLEAR_FEATURE
| ((USB_DIR_OUT
| USB_RT_PORT
) << 8):
1087 reg
= xhci_readl(status_reg
);
1088 reg
= xhci_port_state_to_neutral(reg
);
1089 switch (le16_to_cpu(req
->value
)) {
1090 case USB_PORT_FEAT_ENABLE
:
1093 case USB_PORT_FEAT_POWER
:
1096 case USB_PORT_FEAT_C_RESET
:
1097 case USB_PORT_FEAT_C_CONNECTION
:
1098 case USB_PORT_FEAT_C_OVER_CURRENT
:
1099 case USB_PORT_FEAT_C_ENABLE
:
1100 xhci_clear_port_change_bit((le16_to_cpu(req
->value
)),
1101 le16_to_cpu(req
->index
),
1105 printf("unknown feature %x\n", le16_to_cpu(req
->value
));
1108 xhci_writel(status_reg
, reg
);
1111 puts("Unknown request\n");
1115 debug("scrlen = %d\n req->length = %d\n",
1116 srclen
, le16_to_cpu(req
->length
));
1118 len
= min(srclen
, (int)le16_to_cpu(req
->length
));
1120 if (srcptr
!= NULL
&& len
> 0)
1121 memcpy(buffer
, srcptr
, len
);
1123 debug("Len is 0\n");
1125 udev
->act_len
= len
;
1132 udev
->status
= USB_ST_STALLED
;
1138 * Submits the INT request to XHCI Host cotroller
1140 * @param udev pointer to the USB device
1141 * @param pipe contains the DIR_IN or OUT , devnum
1142 * @param buffer buffer to be read/written based on the request
1143 * @param length length of the buffer
1144 * @param interval interval of the interrupt
1147 static int _xhci_submit_int_msg(struct usb_device
*udev
, unsigned long pipe
,
1148 void *buffer
, int length
, int interval
,
1151 if (usb_pipetype(pipe
) != PIPE_INTERRUPT
) {
1152 printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe
));
1157 * xHCI uses normal TRBs for both bulk and interrupt. When the
1158 * interrupt endpoint is to be serviced, the xHC will consume
1159 * (at most) one TD. A TD (comprised of sg list entries) can
1160 * take several service intervals to transmit.
1162 return xhci_bulk_tx(udev
, pipe
, length
, buffer
);
1166 * submit the BULK type of request to the USB Device
1168 * @param udev pointer to the USB device
1169 * @param pipe contains the DIR_IN or OUT , devnum
1170 * @param buffer buffer to be read/written based on the request
1171 * @param length length of the buffer
1172 * Return: returns 0 if successful else -1 on failure
1174 static int _xhci_submit_bulk_msg(struct usb_device
*udev
, unsigned long pipe
,
1175 void *buffer
, int length
)
1177 if (usb_pipetype(pipe
) != PIPE_BULK
) {
1178 printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe
));
1182 return xhci_bulk_tx(udev
, pipe
, length
, buffer
);
1186 * submit the control type of request to the Root hub/Device based on the devnum
1188 * @param udev pointer to the USB device
1189 * @param pipe contains the DIR_IN or OUT , devnum
1190 * @param buffer buffer to be read/written based on the request
1191 * @param length length of the buffer
1192 * @param setup Request type
1193 * @param root_portnr Root port number that this device is on
1194 * Return: returns 0 if successful else -1 on failure
1196 static int _xhci_submit_control_msg(struct usb_device
*udev
, unsigned long pipe
,
1197 void *buffer
, int length
,
1198 struct devrequest
*setup
, int root_portnr
)
1200 struct xhci_ctrl
*ctrl
= xhci_get_ctrl(udev
);
1203 if (usb_pipetype(pipe
) != PIPE_CONTROL
) {
1204 printf("non-control pipe (type=%lu)", usb_pipetype(pipe
));
1208 if (usb_pipedevice(pipe
) == ctrl
->rootdev
)
1209 return xhci_submit_root(udev
, pipe
, buffer
, setup
);
1211 if (setup
->request
== USB_REQ_SET_ADDRESS
&&
1212 (setup
->requesttype
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
)
1213 return xhci_address_device(udev
, root_portnr
);
1215 if (setup
->request
== USB_REQ_SET_CONFIGURATION
&&
1216 (setup
->requesttype
& USB_TYPE_MASK
) == USB_TYPE_STANDARD
) {
1217 ret
= xhci_set_configuration(udev
);
1219 puts("Failed to configure xHCI endpoint\n");
1224 return xhci_ctrl_tx(udev
, pipe
, setup
, length
, buffer
);
1227 static int xhci_lowlevel_init(struct xhci_ctrl
*ctrl
)
1229 struct xhci_hccr
*hccr
;
1230 struct xhci_hcor
*hcor
;
1238 * Program the Number of Device Slots Enabled field in the CONFIG
1239 * register with the max value of slots the HC can handle.
1241 val
= (xhci_readl(&hccr
->cr_hcsparams1
) & HCS_SLOTS_MASK
);
1242 val2
= xhci_readl(&hcor
->or_config
);
1243 val
|= (val2
& ~HCS_SLOTS_MASK
);
1244 xhci_writel(&hcor
->or_config
, val
);
1246 /* initializing xhci data structures */
1247 if (xhci_mem_init(ctrl
, hccr
, hcor
) < 0)
1249 ctrl
->hub_desc
= descriptor
.hub
;
1251 reg
= xhci_readl(&hccr
->cr_hcsparams1
);
1252 ctrl
->hub_desc
.bNbrPorts
= HCS_MAX_PORTS(reg
);
1253 printf("Register %x NbrPorts %d\n", reg
, ctrl
->hub_desc
.bNbrPorts
);
1255 /* Port Indicators */
1256 reg
= xhci_readl(&hccr
->cr_hccparams
);
1257 if (HCS_INDICATOR(reg
))
1258 put_unaligned(get_unaligned(&ctrl
->hub_desc
.wHubCharacteristics
)
1259 | 0x80, &ctrl
->hub_desc
.wHubCharacteristics
);
1261 /* Port Power Control */
1263 put_unaligned(get_unaligned(&ctrl
->hub_desc
.wHubCharacteristics
)
1264 | 0x01, &ctrl
->hub_desc
.wHubCharacteristics
);
1266 if (xhci_start(hcor
)) {
1271 /* Zero'ing IRQ control register and IRQ pending register */
1272 xhci_writel(&ctrl
->ir_set
->irq_control
, 0x0);
1273 xhci_writel(&ctrl
->ir_set
->irq_pending
, 0x0);
1275 reg
= HC_VERSION(xhci_readl(&hccr
->cr_capbase
));
1276 printf("USB XHCI %x.%02x\n", reg
>> 8, reg
& 0xff);
1277 ctrl
->hci_version
= reg
;
1282 static int xhci_lowlevel_stop(struct xhci_ctrl
*ctrl
)
1286 xhci_reset(ctrl
->hcor
);
1288 debug("// Disabling event ring interrupts\n");
1289 temp
= xhci_readl(&ctrl
->hcor
->or_usbsts
);
1290 xhci_writel(&ctrl
->hcor
->or_usbsts
, temp
& ~STS_EINT
);
1291 temp
= xhci_readl(&ctrl
->ir_set
->irq_pending
);
1292 xhci_writel(&ctrl
->ir_set
->irq_pending
, ER_IRQ_DISABLE(temp
));
1297 static int xhci_submit_control_msg(struct udevice
*dev
, struct usb_device
*udev
,
1298 unsigned long pipe
, void *buffer
, int length
,
1299 struct devrequest
*setup
)
1301 struct usb_device
*uhop
;
1302 struct udevice
*hub
;
1303 int root_portnr
= 0;
1305 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__
,
1306 dev
->name
, udev
, udev
->dev
->name
, udev
->portnr
);
1308 if (device_get_uclass_id(hub
) == UCLASS_USB_HUB
) {
1309 /* Figure out our port number on the root hub */
1310 if (usb_hub_is_root_hub(hub
)) {
1311 root_portnr
= udev
->portnr
;
1313 while (!usb_hub_is_root_hub(hub
->parent
))
1315 uhop
= dev_get_parent_priv(hub
);
1316 root_portnr
= uhop
->portnr
;
1320 struct usb_device *hop = udev;
1323 while (hop->parent->parent)
1326 return _xhci_submit_control_msg(udev
, pipe
, buffer
, length
, setup
,
1330 static int xhci_submit_bulk_msg(struct udevice
*dev
, struct usb_device
*udev
,
1331 unsigned long pipe
, void *buffer
, int length
)
1333 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1334 return _xhci_submit_bulk_msg(udev
, pipe
, buffer
, length
);
1337 static int xhci_submit_int_msg(struct udevice
*dev
, struct usb_device
*udev
,
1338 unsigned long pipe
, void *buffer
, int length
,
1339 int interval
, bool nonblock
)
1341 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1342 return _xhci_submit_int_msg(udev
, pipe
, buffer
, length
, interval
,
1346 static int xhci_alloc_device(struct udevice
*dev
, struct usb_device
*udev
)
1348 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1349 return _xhci_alloc_device(udev
);
1352 static int xhci_update_hub_device(struct udevice
*dev
, struct usb_device
*udev
)
1354 struct xhci_ctrl
*ctrl
= dev_get_priv(dev
);
1355 struct usb_hub_device
*hub
= dev_get_uclass_priv(udev
->dev
);
1356 struct xhci_virt_device
*virt_dev
;
1357 struct xhci_input_control_ctx
*ctrl_ctx
;
1358 struct xhci_container_ctx
*out_ctx
;
1359 struct xhci_container_ctx
*in_ctx
;
1360 struct xhci_slot_ctx
*slot_ctx
;
1361 int slot_id
= udev
->slot_id
;
1362 unsigned think_time
;
1364 debug("%s: dev='%s', udev=%p\n", __func__
, dev
->name
, udev
);
1366 /* Ignore root hubs */
1367 if (usb_hub_is_root_hub(udev
->dev
))
1370 virt_dev
= ctrl
->devs
[slot_id
];
1373 out_ctx
= virt_dev
->out_ctx
;
1374 in_ctx
= virt_dev
->in_ctx
;
1376 ctrl_ctx
= xhci_get_input_control_ctx(in_ctx
);
1377 /* Initialize the input context control */
1378 ctrl_ctx
->add_flags
= cpu_to_le32(SLOT_FLAG
);
1379 ctrl_ctx
->drop_flags
= 0;
1381 xhci_inval_cache((uintptr_t)out_ctx
->bytes
, out_ctx
->size
);
1384 xhci_slot_copy(ctrl
, in_ctx
, out_ctx
);
1385 slot_ctx
= xhci_get_slot_ctx(ctrl
, in_ctx
);
1387 /* Update hub related fields */
1388 slot_ctx
->dev_info
|= cpu_to_le32(DEV_HUB
);
1390 * refer to section 6.2.2: MTT should be 0 for full speed hub,
1391 * but it may be already set to 1 when setup an xHCI virtual
1392 * device, so clear it anyway.
1395 slot_ctx
->dev_info
|= cpu_to_le32(DEV_MTT
);
1396 else if (udev
->speed
== USB_SPEED_FULL
)
1397 slot_ctx
->dev_info
&= cpu_to_le32(~DEV_MTT
);
1398 slot_ctx
->dev_info2
|= cpu_to_le32(XHCI_MAX_PORTS(udev
->maxchild
));
1400 * Set TT think time - convert from ns to FS bit times.
1401 * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1403 * 0 = 8 FS bit times, 1 = 16 FS bit times,
1404 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1406 * This field shall be 0 if the device is not a high-spped hub.
1408 think_time
= hub
->tt
.think_time
;
1409 if (think_time
!= 0)
1410 think_time
= (think_time
/ 666) - 1;
1411 if (udev
->speed
== USB_SPEED_HIGH
)
1412 slot_ctx
->tt_info
|= cpu_to_le32(TT_THINK_TIME(think_time
));
1413 slot_ctx
->dev_state
= 0;
1415 return xhci_configure_endpoints(udev
, false);
1418 static int xhci_get_max_xfer_size(struct udevice
*dev
, size_t *size
)
1421 * xHCD allocates one segment which includes 64 TRBs for each endpoint
1422 * and the last TRB in this segment is configured as a link TRB to form
1423 * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1424 * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1425 * Hence the maximum number of TRBs we can use in one transfer is 62.
1427 *size
= (TRBS_PER_SEGMENT
- 2) * TRB_MAX_BUFF_SIZE
;
1432 int xhci_register(struct udevice
*dev
, struct xhci_hccr
*hccr
,
1433 struct xhci_hcor
*hcor
)
1435 struct xhci_ctrl
*ctrl
= dev_get_priv(dev
);
1436 struct usb_bus_priv
*priv
= dev_get_uclass_priv(dev
);
1439 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__
, dev
->name
,
1445 * XHCI needs to issue a Address device command to setup
1446 * proper device context structures, before it can interact
1447 * with the device. So a get_descriptor will fail before any
1448 * of that is done for XHCI unlike EHCI.
1450 priv
->desc_before_addr
= false;
1452 ret
= xhci_reset(hcor
);
1458 ret
= xhci_lowlevel_init(ctrl
);
1464 debug("%s: failed, ret=%d\n", __func__
, ret
);
1468 int xhci_deregister(struct udevice
*dev
)
1470 struct xhci_ctrl
*ctrl
= dev_get_priv(dev
);
1472 xhci_lowlevel_stop(ctrl
);
1478 struct dm_usb_ops xhci_usb_ops
= {
1479 .control
= xhci_submit_control_msg
,
1480 .bulk
= xhci_submit_bulk_msg
,
1481 .interrupt
= xhci_submit_int_msg
,
1482 .alloc_device
= xhci_alloc_device
,
1483 .update_hub_device
= xhci_update_hub_device
,
1484 .get_max_xfer_size
= xhci_get_max_xfer_size
,