blob: 9e33c5d85596ce358d80dbba3f752109589a7006 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vivek Gautam4912dcc2013-09-14 14:02:45 +05302/*
3 * USB HOST XHCI Controller stack
4 *
5 * Based on xHCI host controller driver in linux-kernel
6 * by Sarah Sharp.
7 *
8 * Copyright (C) 2008 Intel Corp.
9 * Author: Sarah Sharp
10 *
11 * Copyright (C) 2013 Samsung Electronics Co.Ltd
12 * Authors: Vivek Gautam <gautam.vivek@samsung.com>
13 * Vikas Sajjan <vikas.sajjan@samsung.com>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053014 */
15
16/**
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.
20 */
21
22#include <common.h>
Simon Glass63334482019-11-14 12:57:39 -070023#include <cpu_func.h>
Simon Glass49b41832015-03-25 12:22:53 -060024#include <dm.h>
Sean Anderson429ce522020-10-04 21:39:53 -040025#include <dm/device_compat.h>
Simon Glass0f2af882020-05-10 11:40:05 -060026#include <log.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053027#include <malloc.h>
Sean Anderson429ce522020-10-04 21:39:53 -040028#include <usb.h>
29#include <usb/xhci.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053030#include <watchdog.h>
Sean Anderson429ce522020-10-04 21:39:53 -040031#include <asm/byteorder.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053032#include <asm/cache.h>
33#include <asm/unaligned.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060034#include <linux/bitops.h>
Simon Glassc06c1be2020-05-10 11:40:08 -060035#include <linux/bug.h>
Simon Glassdbd79542020-05-10 11:40:11 -060036#include <linux/delay.h>
Masahiro Yamada64e4f7f2016-09-21 11:28:57 +090037#include <linux/errno.h>
developer14bb3502020-09-08 19:00:03 +020038#include <linux/iopoll.h>
Vivek Gautam4912dcc2013-09-14 14:02:45 +053039
Vivek Gautam4912dcc2013-09-14 14:02:45 +053040static 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 = {
48 {
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 */
Bin Meng0d66b3a2017-07-19 21:50:00 +080055 { /* Device removable */
56 } /* at most 7 ports! XXX */
Vivek Gautam4912dcc2013-09-14 14:02:45 +053057 },
58 {
59 0x12, /* bLength */
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 */
70 2, /* iProduct */
71 0, /* iSerialNumber */
72 1 /* bNumConfigurations: 1 */
73 },
74 {
75 0x9,
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 */
82 0 /* bMaxPower */
83 },
84 {
85 0x9, /* bLength */
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 */
93 0 /* iInterface */
94 },
95 {
96 0x7, /* bLength */
97 5, /* bDescriptorType: UDESC_ENDPOINT */
98 0x81, /* bEndpointAddress: IN endpoint 1 */
99 3, /* bmAttributes: UE_INTERRUPT */
100 8, /* wMaxPacketSize */
101 255 /* bInterval */
102 },
103 {
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 */
108 0x00,
109 /* ss_wBytesPerInterval: 15 bits for max 15 ports */
110 cpu_to_le16(0x02),
111 },
112};
113
Simon Glassa49e27b2015-03-25 12:22:49 -0600114struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
115{
Simon Glass49b41832015-03-25 12:22:53 -0600116 struct udevice *dev;
117
118 /* Find the USB controller */
119 for (dev = udev->dev;
120 device_get_uclass_id(dev) != UCLASS_USB;
121 dev = dev->parent)
122 ;
123 return dev_get_priv(dev);
Simon Glassa49e27b2015-03-25 12:22:49 -0600124}
125
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530126/**
127 * Waits for as per specified amount of time
128 * for the "result" to match with "done"
129 *
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
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100134 * Return: 0 if handshake is success else < 0 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530135 */
developer14bb3502020-09-08 19:00:03 +0200136static int
137handshake(uint32_t volatile *ptr, uint32_t mask, uint32_t done, int usec)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530138{
139 uint32_t result;
developer14bb3502020-09-08 19:00:03 +0200140 int ret;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530141
developer14bb3502020-09-08 19:00:03 +0200142 ret = readx_poll_sleep_timeout(xhci_readl, ptr, result,
143 (result & mask) == done || result == U32_MAX,
144 1, usec);
145 if (result == U32_MAX) /* card removed */
146 return -ENODEV;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530147
developer14bb3502020-09-08 19:00:03 +0200148 return ret;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530149}
150
151/**
152 * Set the run bit and wait for the host to be running.
153 *
154 * @param hcor pointer to host controller operation registers
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100155 * Return: status of the Handshake
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530156 */
157static int xhci_start(struct xhci_hcor *hcor)
158{
159 u32 temp;
160 int ret;
161
162 puts("Starting the controller\n");
163 temp = xhci_readl(&hcor->or_usbcmd);
164 temp |= (CMD_RUN);
165 xhci_writel(&hcor->or_usbcmd, temp);
166
167 /*
168 * Wait for the HCHalted Status bit to be 0 to indicate the host is
169 * running.
170 */
171 ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
172 if (ret)
173 debug("Host took too long to start, "
174 "waited %u microseconds.\n",
175 XHCI_MAX_HALT_USEC);
176 return ret;
177}
178
179/**
180 * Resets the XHCI Controller
181 *
182 * @param hcor pointer to host controller operation registers
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100183 * Return: -EBUSY if XHCI Controller is not halted else status of handshake
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530184 */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900185static int xhci_reset(struct xhci_hcor *hcor)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530186{
187 u32 cmd;
188 u32 state;
189 int ret;
190
191 /* Halting the Host first */
Sergey Temerkhanov65bb4542015-08-17 15:38:07 +0300192 debug("// Halt the HC: %p\n", hcor);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530193 state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
194 if (!state) {
195 cmd = xhci_readl(&hcor->or_usbcmd);
196 cmd &= ~CMD_RUN;
197 xhci_writel(&hcor->or_usbcmd, cmd);
198 }
199
200 ret = handshake(&hcor->or_usbsts,
201 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
202 if (ret) {
203 printf("Host not halted after %u microseconds.\n",
204 XHCI_MAX_HALT_USEC);
205 return -EBUSY;
206 }
207
208 debug("// Reset the HC\n");
209 cmd = xhci_readl(&hcor->or_usbcmd);
210 cmd |= CMD_RESET;
211 xhci_writel(&hcor->or_usbcmd, cmd);
212
213 ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
214 if (ret)
215 return ret;
216
217 /*
218 * xHCI cannot write to any doorbells or operational registers other
219 * than status until the "Controller Not Ready" flag is cleared.
220 */
221 return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
222}
223
224/**
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.
228 *
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)
233 *
234 * @param desc USB enpdoint Descriptor
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100235 * Return: index of the Endpoint
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530236 */
237static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
238{
239 unsigned int index;
240
241 if (usb_endpoint_xfer_control(desc))
242 index = (unsigned int)(usb_endpoint_num(desc) * 2);
243 else
244 index = (unsigned int)((usb_endpoint_num(desc) * 2) -
245 (usb_endpoint_dir_in(desc) ? 0 : 1));
246
247 return index;
248}
249
Bin Meng87033f02017-09-18 06:40:47 -0700250/*
251 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
252 * microframes, rounded down to nearest power of 2.
253 */
254static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
255 unsigned int min_exponent,
256 unsigned int max_exponent)
257{
258 unsigned int interval;
259
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);
266
267 return interval;
268}
269
270static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
271 struct usb_endpoint_descriptor *endpt_desc)
272{
273 if (endpt_desc->bInterval == 0)
274 return 0;
275
276 return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
277}
278
279static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
280 struct usb_endpoint_descriptor *endpt_desc)
281{
282 return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
283}
284
285/*
286 * Convert interval expressed as 2^(bInterval - 1) == interval into
287 * straight exponent value 2^n == interval.
288 */
289static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
290 struct usb_endpoint_descriptor *endpt_desc)
291{
292 unsigned int interval;
293
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");
299
300 if (udev->speed == USB_SPEED_FULL) {
301 /*
302 * Full speed isoc endpoints specify interval in frames,
303 * not microframes. We are using microframes everywhere,
304 * so adjust accordingly.
305 */
306 interval += 3; /* 1 frame = 2^3 uframes */
307 }
308
309 return interval;
310}
311
312/*
313 * Return the polling or NAK interval.
314 *
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.
317 *
318 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
319 * is set to 0.
320 */
321static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
322 struct usb_endpoint_descriptor *endpt_desc)
323{
324 unsigned int interval = 0;
325
326 switch (udev->speed) {
327 case USB_SPEED_HIGH:
328 /* Max NAK rate */
329 if (usb_endpoint_xfer_control(endpt_desc) ||
330 usb_endpoint_xfer_bulk(endpt_desc)) {
331 interval = xhci_parse_microframe_interval(udev,
332 endpt_desc);
333 break;
334 }
335 /* Fall through - SS and HS isoc/int have same decoding */
336
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,
341 endpt_desc);
342 }
343 break;
344
345 case USB_SPEED_FULL:
346 if (usb_endpoint_xfer_isoc(endpt_desc)) {
347 interval = xhci_parse_exponent_interval(udev,
348 endpt_desc);
349 break;
350 }
351 /*
352 * Fall through for interrupt endpoint interval decoding
353 * since it uses the same rules as low speed interrupt
354 * endpoints.
355 */
356
357 case USB_SPEED_LOW:
358 if (usb_endpoint_xfer_int(endpt_desc) ||
359 usb_endpoint_xfer_isoc(endpt_desc)) {
360 interval = xhci_parse_frame_interval(udev, endpt_desc);
361 }
362 break;
363
364 default:
365 BUG();
366 }
367
368 return interval;
369}
370
371/*
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.
376 */
377static 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)
380{
381 if (udev->speed < USB_SPEED_SUPER ||
382 !usb_endpoint_xfer_isoc(endpt_desc))
383 return 0;
384
385 return ss_ep_comp_desc->bmAttributes;
386}
387
Bin Mengbdedd2a2017-09-18 06:40:48 -0700388static 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)
391{
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;
395
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;
400
401 return 0;
402}
403
Bin Meng87033f02017-09-18 06:40:47 -0700404/*
405 * Return the maximum endpoint service interval time (ESIT) payload.
406 * Basically, this is the maxpacket size, multiplied by the burst size
407 * and mult size.
408 */
409static 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)
412{
413 int max_burst;
414 int max_packet;
415
416 /* Only applies for interrupt or isochronous endpoints */
417 if (usb_endpoint_xfer_control(endpt_desc) ||
418 usb_endpoint_xfer_bulk(endpt_desc))
419 return 0;
420
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);
424
425 max_packet = usb_endpoint_maxp(endpt_desc);
426 max_burst = usb_endpoint_maxp_mult(endpt_desc);
427
428 /* A 0 in max burst means 1 transfer per ESIT */
429 return max_packet * max_burst;
430}
431
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530432/**
433 * Issue a configure endpoint command or evaluate context command
434 * and wait for it to finish.
435 *
436 * @param udev pointer to the Device Data Structure
437 * @param ctx_change flag to indicate the Context has changed or NOT
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100438 * Return: 0 on success, -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530439 */
440static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
441{
442 struct xhci_container_ctx *in_ctx;
443 struct xhci_virt_device *virt_dev;
Simon Glassa49e27b2015-03-25 12:22:49 -0600444 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530445 union xhci_trb *event;
446
447 virt_dev = ctrl->devs[udev->slot_id];
448 in_ctx = virt_dev->in_ctx;
449
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300450 xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
Mark Kettenisfac410c2023-01-21 20:27:55 +0100451 xhci_queue_command(ctrl, in_ctx->dma, udev->slot_id, 0,
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530452 ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
453 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
454 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
455 != udev->slot_id);
456
457 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
458 case COMP_SUCCESS:
459 debug("Successful %s command\n",
460 ctx_change ? "Evaluate Context" : "Configure Endpoint");
461 break;
462 default:
463 printf("ERROR: %s command returned completion code %d.\n",
464 ctx_change ? "Evaluate Context" : "Configure Endpoint",
465 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
466 return -EINVAL;
467 }
468
469 xhci_acknowledge_event(ctrl);
470
471 return 0;
472}
473
474/**
475 * Configure the endpoint, programming the device contexts.
476 *
477 * @param udev pointer to the USB device structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100478 * Return: returns the status of the xhci_configure_endpoints
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530479 */
480static int xhci_set_configuration(struct usb_device *udev)
481{
482 struct xhci_container_ctx *in_ctx;
483 struct xhci_container_ctx *out_ctx;
484 struct xhci_input_control_ctx *ctrl_ctx;
485 struct xhci_slot_ctx *slot_ctx;
486 struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
487 int cur_ep;
488 int max_ep_flag = 0;
489 int ep_index;
490 unsigned int dir;
491 unsigned int ep_type;
Simon Glassa49e27b2015-03-25 12:22:49 -0600492 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530493 int num_of_ep;
494 int ep_flag = 0;
495 u64 trb_64 = 0;
496 int slot_id = udev->slot_id;
497 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
498 struct usb_interface *ifdesc;
Bin Meng87033f02017-09-18 06:40:47 -0700499 u32 max_esit_payload;
500 unsigned int interval;
501 unsigned int mult;
Bin Mengbdedd2a2017-09-18 06:40:48 -0700502 unsigned int max_burst;
Bin Meng87033f02017-09-18 06:40:47 -0700503 unsigned int avg_trb_len;
Bin Meng7c3b76d2017-09-18 06:40:49 -0700504 unsigned int err_count = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530505
506 out_ctx = virt_dev->out_ctx;
507 in_ctx = virt_dev->in_ctx;
508
509 num_of_ep = udev->config.if_desc[0].no_of_ep;
510 ifdesc = &udev->config.if_desc[0];
511
512 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Bin Mengec0501b2017-07-19 21:49:56 +0800513 /* Initialize the input context control */
514 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530515 ctrl_ctx->drop_flags = 0;
516
517 /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
518 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
519 ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
520 ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
521 if (max_ep_flag < ep_flag)
522 max_ep_flag = ep_flag;
523 }
524
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300525 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530526
527 /* slot context */
528 xhci_slot_copy(ctrl, in_ctx, out_ctx);
529 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
Bin Menga1ae60e2018-05-23 23:40:50 -0700530 slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530531 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
532
533 xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
534
535 /* filling up ep contexts */
536 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
537 struct usb_endpoint_descriptor *endpt_desc = NULL;
Bin Meng87033f02017-09-18 06:40:47 -0700538 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530539
540 endpt_desc = &ifdesc->ep_desc[cur_ep];
Bin Meng87033f02017-09-18 06:40:47 -0700541 ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530542 trb_64 = 0;
543
Bin Meng87033f02017-09-18 06:40:47 -0700544 /*
545 * Get values to fill the endpoint context, mostly from ep
546 * descriptor. The average TRB buffer lengt for bulk endpoints
547 * is unclear as we have no clue on scatter gather list entry
548 * size. For Isoc and Int, set it to max available.
549 * See xHCI 1.1 spec 4.14.1.1 for details.
550 */
551 max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
552 ss_ep_comp_desc);
553 interval = xhci_get_endpoint_interval(udev, endpt_desc);
554 mult = xhci_get_endpoint_mult(udev, endpt_desc,
555 ss_ep_comp_desc);
Bin Mengbdedd2a2017-09-18 06:40:48 -0700556 max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
557 ss_ep_comp_desc);
Bin Meng87033f02017-09-18 06:40:47 -0700558 avg_trb_len = max_esit_payload;
559
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530560 ep_index = xhci_get_ep_index(endpt_desc);
561 ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
562
563 /* Allocate the ep rings */
Nicolas Saenz Julienne4033aa32021-01-12 13:55:28 +0100564 virt_dev->eps[ep_index].ring = xhci_ring_alloc(ctrl, 1, true);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530565 if (!virt_dev->eps[ep_index].ring)
566 return -ENOMEM;
567
568 /*NOTE: ep_desc[0] actually represents EP1 and so on */
569 dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
570 ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
Bin Meng87033f02017-09-18 06:40:47 -0700571
572 ep_ctx[ep_index]->ep_info =
573 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
574 EP_INTERVAL(interval) | EP_MULT(mult));
575
developer99634222020-09-08 19:00:02 +0200576 ep_ctx[ep_index]->ep_info2 = cpu_to_le32(EP_TYPE(ep_type));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530577 ep_ctx[ep_index]->ep_info2 |=
578 cpu_to_le32(MAX_PACKET
579 (get_unaligned(&endpt_desc->wMaxPacketSize)));
580
Bin Meng7c3b76d2017-09-18 06:40:49 -0700581 /* Allow 3 retries for everything but isoc, set CErr = 3 */
582 if (!usb_endpoint_xfer_isoc(endpt_desc))
583 err_count = 3;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530584 ep_ctx[ep_index]->ep_info2 |=
Bin Mengbdedd2a2017-09-18 06:40:48 -0700585 cpu_to_le32(MAX_BURST(max_burst) |
Bin Meng7c3b76d2017-09-18 06:40:49 -0700586 ERROR_COUNT(err_count));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530587
Mark Kettenisfac410c2023-01-21 20:27:55 +0100588 trb_64 = xhci_trb_virt_to_dma(virt_dev->eps[ep_index].ring->enq_seg,
589 virt_dev->eps[ep_index].ring->enqueue);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530590 ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
591 virt_dev->eps[ep_index].ring->cycle_state);
Bin Meng87033f02017-09-18 06:40:47 -0700592
Bin Mengc03fb202017-09-18 06:40:50 -0700593 /*
594 * xHCI spec 6.2.3:
595 * 'Average TRB Length' should be 8 for control endpoints.
596 */
597 if (usb_endpoint_xfer_control(endpt_desc))
598 avg_trb_len = 8;
Bin Meng87033f02017-09-18 06:40:47 -0700599 ep_ctx[ep_index]->tx_info =
600 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
601 EP_AVG_TRB_LENGTH(avg_trb_len));
developer507fc9b2020-05-02 11:35:18 +0200602
603 /*
604 * The MediaTek xHCI defines some extra SW parameters which
605 * are put into reserved DWs in Slot and Endpoint Contexts
606 * for synchronous endpoints.
607 */
developer80390532020-09-08 18:59:57 +0200608 if (ctrl->quirks & XHCI_MTK_HOST) {
developer507fc9b2020-05-02 11:35:18 +0200609 ep_ctx[ep_index]->reserved[0] =
610 cpu_to_le32(EP_BPKTS(1) | EP_BBM(1));
611 }
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530612 }
613
614 return xhci_configure_endpoints(udev, false);
615}
616
617/**
618 * Issue an Address Device command (which will issue a SetAddress request to
619 * the device).
620 *
621 * @param udev pointer to the Device Data Structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100622 * Return: 0 if successful else error code on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530623 */
Simon Glass4ec422c2015-03-25 12:22:51 -0600624static int xhci_address_device(struct usb_device *udev, int root_portnr)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530625{
626 int ret = 0;
Simon Glassa49e27b2015-03-25 12:22:49 -0600627 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530628 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;
633
634 virt_dev = ctrl->devs[slot_id];
635
636 /*
637 * This is the first Set Address since device plug-in
638 * so setting up the slot context.
639 */
Simon Glass4ec422c2015-03-25 12:22:51 -0600640 debug("Setting up addressable devices %p\n", ctrl->dcbaa);
Bin Meng1459ce62017-07-19 21:51:14 +0800641 xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530642
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;
646
Mark Kettenisfac410c2023-01-21 20:27:55 +0100647 xhci_queue_command(ctrl, virt_dev->in_ctx->dma,
648 slot_id, 0, TRB_ADDR_DEV);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530649 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
650 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
651
652 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
653 case COMP_CTX_STATE:
654 case COMP_EBADSLT:
655 printf("Setup ERROR: address device command for slot %d.\n",
656 slot_id);
657 ret = -EINVAL;
658 break;
659 case COMP_TX_ERR:
660 puts("Device not responding to set address.\n");
661 ret = -EPROTO;
662 break;
663 case COMP_DEV_ERR:
664 puts("ERROR: Incompatible device"
665 "for address device command.\n");
666 ret = -ENODEV;
667 break;
668 case COMP_SUCCESS:
669 debug("Successful Address Device command\n");
670 udev->status = 0;
671 break;
672 default:
673 printf("ERROR: unexpected command completion code 0x%x.\n",
674 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
675 ret = -EINVAL;
676 break;
677 }
678
679 xhci_acknowledge_event(ctrl);
680
681 if (ret < 0)
682 /*
683 * TODO: Unsuccessful Address Device command shall leave the
684 * slot in default state. So, issue Disable Slot command now.
685 */
686 return ret;
687
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300688 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
689 virt_dev->out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530690 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
691
692 debug("xHC internal address is: %d\n",
693 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
694
695 return 0;
696}
697
698/**
699 * Issue Enable slot command to the controller to allocate
700 * device slot and assign the slot id. It fails if the xHC
701 * ran out of device slots, the Enable Slot command timed out,
702 * or allocating memory failed.
703 *
704 * @param udev pointer to the Device Data Structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100705 * Return: Returns 0 on succes else return error code on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530706 */
Masahiro Yamada6d8e4332017-06-22 16:35:14 +0900707static int _xhci_alloc_device(struct usb_device *udev)
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530708{
Simon Glassa49e27b2015-03-25 12:22:49 -0600709 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530710 union xhci_trb *event;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530711 int ret;
712
713 /*
714 * Root hub will be first device to be initailized.
715 * If this device is root-hub, don't do any xHC related
716 * stuff.
717 */
718 if (ctrl->rootdev == 0) {
719 udev->speed = USB_SPEED_SUPER;
720 return 0;
721 }
722
Mark Kettenisfac410c2023-01-21 20:27:55 +0100723 xhci_queue_command(ctrl, 0, 0, 0, TRB_ENABLE_SLOT);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530724 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
725 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
726 != COMP_SUCCESS);
727
728 udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
729
730 xhci_acknowledge_event(ctrl);
731
Simon Glass88a37842015-03-25 12:22:50 -0600732 ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530733 if (ret < 0) {
734 /*
735 * TODO: Unsuccessful Address Device command shall leave
736 * the slot in default. So, issue Disable Slot command now.
737 */
738 puts("Could not allocate xHCI USB device data structures\n");
739 return ret;
740 }
741
742 return 0;
743}
744
745/*
746 * Full speed devices may have a max packet size greater than 8 bytes, but the
747 * USB core doesn't know that until it reads the first 8 bytes of the
748 * descriptor. If the usb_device's max packet size changes after that point,
749 * we need to issue an evaluate context command and wait on it.
750 *
751 * @param udev pointer to the Device Data Structure
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100752 * Return: returns the status of the xhci_configure_endpoints
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530753 */
754int xhci_check_maxpacket(struct usb_device *udev)
755{
Simon Glassa49e27b2015-03-25 12:22:49 -0600756 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530757 unsigned int slot_id = udev->slot_id;
758 int ep_index = 0; /* control endpoint */
759 struct xhci_container_ctx *in_ctx;
760 struct xhci_container_ctx *out_ctx;
761 struct xhci_input_control_ctx *ctrl_ctx;
762 struct xhci_ep_ctx *ep_ctx;
763 int max_packet_size;
764 int hw_max_packet_size;
765 int ret = 0;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530766
767 out_ctx = ctrl->devs[slot_id]->out_ctx;
Sergey Temerkhanov38593462015-04-01 17:18:45 +0300768 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530769
770 ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
771 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Bin Meng7c92b772017-09-18 06:40:44 -0700772 max_packet_size = udev->epmaxpacketin[0];
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530773 if (hw_max_packet_size != max_packet_size) {
774 debug("Max Packet Size for ep 0 changed.\n");
775 debug("Max packet size in usb_device = %d\n", max_packet_size);
776 debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
777 debug("Issuing evaluate context command.\n");
778
779 /* Set up the modified control endpoint 0 */
780 xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
781 ctrl->devs[slot_id]->out_ctx, ep_index);
782 in_ctx = ctrl->devs[slot_id]->in_ctx;
783 ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
developer99634222020-09-08 19:00:02 +0200784 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET(MAX_PACKET_MASK));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530785 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
786
787 /*
788 * Set up the input context flags for the command
789 * FIXME: This won't work if a non-default control endpoint
790 * changes max packet sizes.
791 */
792 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
793 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
794 ctrl_ctx->drop_flags = 0;
795
796 ret = xhci_configure_endpoints(udev, true);
797 }
798 return ret;
799}
800
801/**
802 * Clears the Change bits of the Port Status Register
803 *
804 * @param wValue request value
805 * @param wIndex request index
806 * @param addr address of posrt status register
807 * @param port_status state of port status register
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100808 * Return: none
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530809 */
810static void xhci_clear_port_change_bit(u16 wValue,
811 u16 wIndex, volatile uint32_t *addr, u32 port_status)
812{
813 char *port_change_bit;
814 u32 status;
815
816 switch (wValue) {
817 case USB_PORT_FEAT_C_RESET:
818 status = PORT_RC;
819 port_change_bit = "reset";
820 break;
821 case USB_PORT_FEAT_C_CONNECTION:
822 status = PORT_CSC;
823 port_change_bit = "connect";
824 break;
825 case USB_PORT_FEAT_C_OVER_CURRENT:
826 status = PORT_OCC;
827 port_change_bit = "over-current";
828 break;
829 case USB_PORT_FEAT_C_ENABLE:
830 status = PORT_PEC;
831 port_change_bit = "enable/disable";
832 break;
833 case USB_PORT_FEAT_C_SUSPEND:
834 status = PORT_PLC;
835 port_change_bit = "suspend/resume";
836 break;
837 default:
838 /* Should never happen */
839 return;
840 }
841
842 /* Change bits are all write 1 to clear */
843 xhci_writel(addr, port_status | status);
844
845 port_status = xhci_readl(addr);
846 debug("clear port %s change, actual port %d status = 0x%x\n",
847 port_change_bit, wIndex, port_status);
848}
849
850/**
851 * Save Read Only (RO) bits and save read/write bits where
852 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
853 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
854 *
855 * @param state state of the Port Status and Control Regsiter
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100856 * Return: a value that would result in the port being in the
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530857 * same state, if the value was written to the port
858 * status control register.
859 */
860static u32 xhci_port_state_to_neutral(u32 state)
861{
862 /* Save read-only status and port state */
863 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
864}
865
866/**
867 * Submits the Requests to the XHCI Host Controller
868 *
869 * @param udev pointer to the USB device structure
870 * @param pipe contains the DIR_IN or OUT , devnum
871 * @param buffer buffer to be read/written based on the request
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +0100872 * Return: returns 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530873 */
874static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
875 void *buffer, struct devrequest *req)
876{
877 uint8_t tmpbuf[4];
878 u16 typeReq;
879 void *srcptr = NULL;
880 int len, srclen;
881 uint32_t reg;
882 volatile uint32_t *status_reg;
Simon Glassa49e27b2015-03-25 12:22:49 -0600883 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Bin Meng749de4c2017-07-19 21:50:03 +0800884 struct xhci_hccr *hccr = ctrl->hccr;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530885 struct xhci_hcor *hcor = ctrl->hcor;
Bin Meng749de4c2017-07-19 21:50:03 +0800886 int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530887
Jeroen Hofsteeb351e462014-06-12 00:31:27 +0200888 if ((req->requesttype & USB_RT_PORT) &&
Bin Meng749de4c2017-07-19 21:50:03 +0800889 le16_to_cpu(req->index) > max_ports) {
890 printf("The request port(%d) exceeds maximum port number\n",
891 le16_to_cpu(req->index) - 1);
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530892 return -EINVAL;
893 }
894
895 status_reg = (volatile uint32_t *)
896 (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
897 srclen = 0;
898
899 typeReq = req->request | req->requesttype << 8;
900
901 switch (typeReq) {
902 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
903 switch (le16_to_cpu(req->value) >> 8) {
904 case USB_DT_DEVICE:
905 debug("USB_DT_DEVICE request\n");
906 srcptr = &descriptor.device;
907 srclen = 0x12;
908 break;
909 case USB_DT_CONFIG:
910 debug("USB_DT_CONFIG config\n");
911 srcptr = &descriptor.config;
912 srclen = 0x19;
913 break;
914 case USB_DT_STRING:
915 debug("USB_DT_STRING config\n");
916 switch (le16_to_cpu(req->value) & 0xff) {
917 case 0: /* Language */
918 srcptr = "\4\3\11\4";
919 srclen = 4;
920 break;
921 case 1: /* Vendor String */
Simon Glassb113f6e2015-03-25 12:22:54 -0600922 srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530923 srclen = 14;
924 break;
925 case 2: /* Product Name */
926 srcptr = "\52\3X\0H\0C\0I\0 "
927 "\0H\0o\0s\0t\0 "
928 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
929 srclen = 42;
930 break;
931 default:
932 printf("unknown value DT_STRING %x\n",
933 le16_to_cpu(req->value));
934 goto unknown;
935 }
936 break;
937 default:
938 printf("unknown value %x\n", le16_to_cpu(req->value));
939 goto unknown;
940 }
941 break;
942 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
943 switch (le16_to_cpu(req->value) >> 8) {
944 case USB_DT_HUB:
Bin Menge8930c42017-07-19 21:49:58 +0800945 case USB_DT_SS_HUB:
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530946 debug("USB_DT_HUB config\n");
Mark Kettenis4eb77d32023-01-21 20:28:00 +0100947 srcptr = &ctrl->hub_desc;
Vivek Gautam4912dcc2013-09-14 14:02:45 +0530948 srclen = 0x8;
949 break;
950 default:
951 printf("unknown value %x\n", le16_to_cpu(req->value));
952 goto unknown;
953 }
954 break;
955 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
956 debug("USB_REQ_SET_ADDRESS\n");
957 ctrl->rootdev = le16_to_cpu(req->value);
958 break;
959 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
960 /* Do nothing */
961 break;
962 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
963 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
964 tmpbuf[1] = 0;
965 srcptr = tmpbuf;
966 srclen = 2;
967 break;
968 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
969 memset(tmpbuf, 0, 4);
970 reg = xhci_readl(status_reg);
971 if (reg & PORT_CONNECT) {
972 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
973 switch (reg & DEV_SPEED_MASK) {
974 case XDEV_FS:
975 debug("SPEED = FULLSPEED\n");
976 break;
977 case XDEV_LS:
978 debug("SPEED = LOWSPEED\n");
979 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
980 break;
981 case XDEV_HS:
982 debug("SPEED = HIGHSPEED\n");
983 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
984 break;
985 case XDEV_SS:
986 debug("SPEED = SUPERSPEED\n");
987 tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
988 break;
989 }
990 }
991 if (reg & PORT_PE)
992 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
993 if ((reg & PORT_PLS_MASK) == XDEV_U3)
994 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
995 if (reg & PORT_OC)
996 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
997 if (reg & PORT_RESET)
998 tmpbuf[0] |= USB_PORT_STAT_RESET;
999 if (reg & PORT_POWER)
1000 /*
1001 * XXX: This Port power bit (for USB 3.0 hub)
1002 * we are faking in USB 2.0 hub port status;
1003 * since there's a change in bit positions in
1004 * two:
1005 * USB 2.0 port status PP is at position[8]
1006 * USB 3.0 port status PP is at position[9]
1007 * So, we are still keeping it at position [8]
1008 */
1009 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
1010 if (reg & PORT_CSC)
1011 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
1012 if (reg & PORT_PEC)
1013 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
1014 if (reg & PORT_OCC)
1015 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
1016 if (reg & PORT_RC)
1017 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
1018
1019 srcptr = tmpbuf;
1020 srclen = 4;
1021 break;
1022 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1023 reg = xhci_readl(status_reg);
1024 reg = xhci_port_state_to_neutral(reg);
1025 switch (le16_to_cpu(req->value)) {
1026 case USB_PORT_FEAT_ENABLE:
1027 reg |= PORT_PE;
1028 xhci_writel(status_reg, reg);
1029 break;
1030 case USB_PORT_FEAT_POWER:
1031 reg |= PORT_POWER;
1032 xhci_writel(status_reg, reg);
1033 break;
1034 case USB_PORT_FEAT_RESET:
1035 reg |= PORT_RESET;
1036 xhci_writel(status_reg, reg);
1037 break;
1038 default:
1039 printf("unknown feature %x\n", le16_to_cpu(req->value));
1040 goto unknown;
1041 }
1042 break;
1043 case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1044 reg = xhci_readl(status_reg);
1045 reg = xhci_port_state_to_neutral(reg);
1046 switch (le16_to_cpu(req->value)) {
1047 case USB_PORT_FEAT_ENABLE:
1048 reg &= ~PORT_PE;
1049 break;
1050 case USB_PORT_FEAT_POWER:
1051 reg &= ~PORT_POWER;
1052 break;
1053 case USB_PORT_FEAT_C_RESET:
1054 case USB_PORT_FEAT_C_CONNECTION:
1055 case USB_PORT_FEAT_C_OVER_CURRENT:
1056 case USB_PORT_FEAT_C_ENABLE:
1057 xhci_clear_port_change_bit((le16_to_cpu(req->value)),
1058 le16_to_cpu(req->index),
1059 status_reg, reg);
1060 break;
1061 default:
1062 printf("unknown feature %x\n", le16_to_cpu(req->value));
1063 goto unknown;
1064 }
1065 xhci_writel(status_reg, reg);
1066 break;
1067 default:
1068 puts("Unknown request\n");
1069 goto unknown;
1070 }
1071
1072 debug("scrlen = %d\n req->length = %d\n",
1073 srclen, le16_to_cpu(req->length));
1074
Masahiro Yamadadb204642014-11-07 03:03:31 +09001075 len = min(srclen, (int)le16_to_cpu(req->length));
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301076
1077 if (srcptr != NULL && len > 0)
1078 memcpy(buffer, srcptr, len);
1079 else
1080 debug("Len is 0\n");
1081
1082 udev->act_len = len;
1083 udev->status = 0;
1084
1085 return 0;
1086
1087unknown:
1088 udev->act_len = 0;
1089 udev->status = USB_ST_STALLED;
1090
1091 return -ENODEV;
1092}
1093
1094/**
1095 * Submits the INT request to XHCI Host cotroller
1096 *
1097 * @param udev pointer to the USB device
1098 * @param pipe contains the DIR_IN or OUT , devnum
1099 * @param buffer buffer to be read/written based on the request
1100 * @param length length of the buffer
1101 * @param interval interval of the interrupt
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001102 * Return: 0
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301103 */
Simon Glass49b41832015-03-25 12:22:53 -06001104static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001105 void *buffer, int length, int interval,
1106 bool nonblock)
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301107{
Bin Meng2bc748c2017-09-18 06:40:41 -07001108 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1109 printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1110 return -EINVAL;
1111 }
1112
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301113 /*
Bin Meng2bc748c2017-09-18 06:40:41 -07001114 * xHCI uses normal TRBs for both bulk and interrupt. When the
1115 * interrupt endpoint is to be serviced, the xHC will consume
1116 * (at most) one TD. A TD (comprised of sg list entries) can
1117 * take several service intervals to transmit.
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301118 */
Bin Meng2bc748c2017-09-18 06:40:41 -07001119 return xhci_bulk_tx(udev, pipe, length, buffer);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301120}
1121
1122/**
1123 * submit the BULK type of request to the USB Device
1124 *
1125 * @param udev pointer to the USB device
1126 * @param pipe contains the DIR_IN or OUT , devnum
1127 * @param buffer buffer to be read/written based on the request
1128 * @param length length of the buffer
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001129 * Return: returns 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301130 */
Simon Glass49b41832015-03-25 12:22:53 -06001131static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1132 void *buffer, int length)
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301133{
1134 if (usb_pipetype(pipe) != PIPE_BULK) {
1135 printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1136 return -EINVAL;
1137 }
1138
1139 return xhci_bulk_tx(udev, pipe, length, buffer);
1140}
1141
1142/**
1143 * submit the control type of request to the Root hub/Device based on the devnum
1144 *
1145 * @param udev pointer to the USB device
1146 * @param pipe contains the DIR_IN or OUT , devnum
1147 * @param buffer buffer to be read/written based on the request
1148 * @param length length of the buffer
1149 * @param setup Request type
Simon Glass4ec422c2015-03-25 12:22:51 -06001150 * @param root_portnr Root port number that this device is on
Heinrich Schuchardt47b4c022022-01-19 18:05:50 +01001151 * Return: returns 0 if successful else -1 on failure
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301152 */
Simon Glass4ec422c2015-03-25 12:22:51 -06001153static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
1154 void *buffer, int length,
1155 struct devrequest *setup, int root_portnr)
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301156{
Simon Glassa49e27b2015-03-25 12:22:49 -06001157 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301158 int ret = 0;
1159
1160 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1161 printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
1162 return -EINVAL;
1163 }
1164
1165 if (usb_pipedevice(pipe) == ctrl->rootdev)
1166 return xhci_submit_root(udev, pipe, buffer, setup);
1167
Ted Chena2f4f9a2016-03-18 17:56:52 +10301168 if (setup->request == USB_REQ_SET_ADDRESS &&
1169 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
Simon Glass4ec422c2015-03-25 12:22:51 -06001170 return xhci_address_device(udev, root_portnr);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301171
Ted Chena2f4f9a2016-03-18 17:56:52 +10301172 if (setup->request == USB_REQ_SET_CONFIGURATION &&
1173 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301174 ret = xhci_set_configuration(udev);
1175 if (ret) {
1176 puts("Failed to configure xHCI endpoint\n");
1177 return ret;
1178 }
1179 }
1180
1181 return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
1182}
1183
Simon Glass686a8122015-03-25 12:22:52 -06001184static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301185{
Simon Glass686a8122015-03-25 12:22:52 -06001186 struct xhci_hccr *hccr;
1187 struct xhci_hcor *hcor;
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301188 uint32_t val;
1189 uint32_t val2;
1190 uint32_t reg;
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301191
Simon Glass686a8122015-03-25 12:22:52 -06001192 hccr = ctrl->hccr;
1193 hcor = ctrl->hcor;
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301194 /*
1195 * Program the Number of Device Slots Enabled field in the CONFIG
1196 * register with the max value of slots the HC can handle.
1197 */
1198 val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
1199 val2 = xhci_readl(&hcor->or_config);
1200 val |= (val2 & ~HCS_SLOTS_MASK);
1201 xhci_writel(&hcor->or_config, val);
1202
1203 /* initializing xhci data structures */
1204 if (xhci_mem_init(ctrl, hccr, hcor) < 0)
1205 return -ENOMEM;
Mark Kettenis4eb77d32023-01-21 20:28:00 +01001206 ctrl->hub_desc = descriptor.hub;
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301207
1208 reg = xhci_readl(&hccr->cr_hcsparams1);
Mark Kettenis4eb77d32023-01-21 20:28:00 +01001209 ctrl->hub_desc.bNbrPorts = HCS_MAX_PORTS(reg);
1210 printf("Register %x NbrPorts %d\n", reg, ctrl->hub_desc.bNbrPorts);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301211
1212 /* Port Indicators */
1213 reg = xhci_readl(&hccr->cr_hccparams);
1214 if (HCS_INDICATOR(reg))
Mark Kettenis4eb77d32023-01-21 20:28:00 +01001215 put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
1216 | 0x80, &ctrl->hub_desc.wHubCharacteristics);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301217
1218 /* Port Power Control */
1219 if (HCC_PPC(reg))
Mark Kettenis4eb77d32023-01-21 20:28:00 +01001220 put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics)
1221 | 0x01, &ctrl->hub_desc.wHubCharacteristics);
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301222
1223 if (xhci_start(hcor)) {
1224 xhci_reset(hcor);
1225 return -ENODEV;
1226 }
1227
1228 /* Zero'ing IRQ control register and IRQ pending register */
1229 xhci_writel(&ctrl->ir_set->irq_control, 0x0);
1230 xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
1231
1232 reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
1233 printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
developerd1c2da42020-09-08 18:59:55 +02001234 ctrl->hci_version = reg;
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301235
Simon Glass686a8122015-03-25 12:22:52 -06001236 return 0;
1237}
1238
1239static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1240{
1241 u32 temp;
1242
1243 xhci_reset(ctrl->hcor);
1244
1245 debug("// Disabling event ring interrupts\n");
1246 temp = xhci_readl(&ctrl->hcor->or_usbsts);
1247 xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1248 temp = xhci_readl(&ctrl->ir_set->irq_pending);
1249 xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
Vivek Gautam4912dcc2013-09-14 14:02:45 +05301250
1251 return 0;
1252}
1253
Simon Glass49b41832015-03-25 12:22:53 -06001254static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1255 unsigned long pipe, void *buffer, int length,
1256 struct devrequest *setup)
1257{
1258 struct usb_device *uhop;
1259 struct udevice *hub;
1260 int root_portnr = 0;
1261
1262 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1263 dev->name, udev, udev->dev->name, udev->portnr);
1264 hub = udev->dev;
1265 if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1266 /* Figure out our port number on the root hub */
Bin Meng5ecfd5d2017-07-19 21:51:11 +08001267 if (usb_hub_is_root_hub(hub)) {
Simon Glass49b41832015-03-25 12:22:53 -06001268 root_portnr = udev->portnr;
1269 } else {
Bin Meng5ecfd5d2017-07-19 21:51:11 +08001270 while (!usb_hub_is_root_hub(hub->parent))
Simon Glass49b41832015-03-25 12:22:53 -06001271 hub = hub->parent;
Simon Glassde44acf2015-09-28 23:32:01 -06001272 uhop = dev_get_parent_priv(hub);
Simon Glass49b41832015-03-25 12:22:53 -06001273 root_portnr = uhop->portnr;
1274 }
1275 }
1276/*
1277 struct usb_device *hop = udev;
1278
1279 if (hop->parent)
1280 while (hop->parent->parent)
1281 hop = hop->parent;
1282*/
1283 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1284 root_portnr);
1285}
1286
1287static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1288 unsigned long pipe, void *buffer, int length)
1289{
1290 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1291 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1292}
1293
1294static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1295 unsigned long pipe, void *buffer, int length,
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001296 int interval, bool nonblock)
Simon Glass49b41832015-03-25 12:22:53 -06001297{
1298 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
Michal Suchanek1c95b9f2019-08-18 10:55:27 +02001299 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1300 nonblock);
Simon Glass49b41832015-03-25 12:22:53 -06001301}
1302
1303static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1304{
1305 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1306 return _xhci_alloc_device(udev);
1307}
1308
Bin Meng2b6f4c52017-07-19 21:51:19 +08001309static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1310{
1311 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1312 struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1313 struct xhci_virt_device *virt_dev;
1314 struct xhci_input_control_ctx *ctrl_ctx;
1315 struct xhci_container_ctx *out_ctx;
1316 struct xhci_container_ctx *in_ctx;
1317 struct xhci_slot_ctx *slot_ctx;
1318 int slot_id = udev->slot_id;
1319 unsigned think_time;
1320
1321 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1322
1323 /* Ignore root hubs */
1324 if (usb_hub_is_root_hub(udev->dev))
1325 return 0;
1326
1327 virt_dev = ctrl->devs[slot_id];
1328 BUG_ON(!virt_dev);
1329
1330 out_ctx = virt_dev->out_ctx;
1331 in_ctx = virt_dev->in_ctx;
1332
1333 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1334 /* Initialize the input context control */
Bin Meng03760fe2018-05-23 23:40:47 -07001335 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Bin Meng2b6f4c52017-07-19 21:51:19 +08001336 ctrl_ctx->drop_flags = 0;
1337
1338 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1339
1340 /* slot context */
1341 xhci_slot_copy(ctrl, in_ctx, out_ctx);
1342 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1343
1344 /* Update hub related fields */
1345 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Bin Meng18f5bcd2018-05-23 23:40:49 -07001346 /*
1347 * refer to section 6.2.2: MTT should be 0 for full speed hub,
1348 * but it may be already set to 1 when setup an xHCI virtual
1349 * device, so clear it anyway.
1350 */
1351 if (hub->tt.multi)
Bin Meng2b6f4c52017-07-19 21:51:19 +08001352 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Meng18f5bcd2018-05-23 23:40:49 -07001353 else if (udev->speed == USB_SPEED_FULL)
1354 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
Bin Meng2b6f4c52017-07-19 21:51:19 +08001355 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1356 /*
1357 * Set TT think time - convert from ns to FS bit times.
1358 * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1359 *
1360 * 0 = 8 FS bit times, 1 = 16 FS bit times,
1361 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1362 *
1363 * This field shall be 0 if the device is not a high-spped hub.
1364 */
1365 think_time = hub->tt.think_time;
1366 if (think_time != 0)
1367 think_time = (think_time / 666) - 1;
1368 if (udev->speed == USB_SPEED_HIGH)
1369 slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
Bin Mengd0383982018-05-23 23:40:48 -07001370 slot_ctx->dev_state = 0;
Bin Meng2b6f4c52017-07-19 21:51:19 +08001371
1372 return xhci_configure_endpoints(udev, false);
1373}
1374
Bin Meng1bc4ce92017-09-07 06:13:18 -07001375static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1376{
1377 /*
1378 * xHCD allocates one segment which includes 64 TRBs for each endpoint
1379 * and the last TRB in this segment is configured as a link TRB to form
1380 * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1381 * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1382 * Hence the maximum number of TRBs we can use in one transfer is 62.
1383 */
1384 *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1385
1386 return 0;
1387}
1388
Simon Glass49b41832015-03-25 12:22:53 -06001389int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1390 struct xhci_hcor *hcor)
1391{
1392 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1393 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1394 int ret;
1395
1396 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1397 ctrl, hccr, hcor);
1398
1399 ctrl->dev = dev;
1400
1401 /*
1402 * XHCI needs to issue a Address device command to setup
1403 * proper device context structures, before it can interact
1404 * with the device. So a get_descriptor will fail before any
1405 * of that is done for XHCI unlike EHCI.
1406 */
1407 priv->desc_before_addr = false;
1408
1409 ret = xhci_reset(hcor);
1410 if (ret)
1411 goto err;
1412
1413 ctrl->hccr = hccr;
1414 ctrl->hcor = hcor;
1415 ret = xhci_lowlevel_init(ctrl);
1416 if (ret)
1417 goto err;
1418
1419 return 0;
1420err:
1421 free(ctrl);
1422 debug("%s: failed, ret=%d\n", __func__, ret);
1423 return ret;
1424}
1425
1426int xhci_deregister(struct udevice *dev)
1427{
1428 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1429
1430 xhci_lowlevel_stop(ctrl);
1431 xhci_cleanup(ctrl);
1432
1433 return 0;
1434}
1435
1436struct dm_usb_ops xhci_usb_ops = {
1437 .control = xhci_submit_control_msg,
1438 .bulk = xhci_submit_bulk_msg,
1439 .interrupt = xhci_submit_int_msg,
1440 .alloc_device = xhci_alloc_device,
Bin Meng2b6f4c52017-07-19 21:51:19 +08001441 .update_hub_device = xhci_update_hub_device,
Bin Meng1bc4ce92017-09-07 06:13:18 -07001442 .get_max_xfer_size = xhci_get_max_xfer_size,
Simon Glass49b41832015-03-25 12:22:53 -06001443};