blob: ebd29545715b95205f1bfc45a35b002f7ee9b126 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Vivek Gautam5853e132013-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 Gautam5853e132013-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 Glass1eb69ae2019-11-14 12:57:39 -070023#include <cpu_func.h>
Simon Glassa5762fe2015-03-25 12:22:53 -060024#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060025#include <log.h>
Vivek Gautam5853e132013-09-14 14:02:45 +053026#include <asm/byteorder.h>
27#include <usb.h>
28#include <malloc.h>
29#include <watchdog.h>
30#include <asm/cache.h>
31#include <asm/unaligned.h>
Simon Glasscd93d622020-05-10 11:40:13 -060032#include <linux/bitops.h>
Simon Glasseb41d8a2020-05-10 11:40:08 -060033#include <linux/bug.h>
Simon Glassc05ed002020-05-10 11:40:11 -060034#include <linux/delay.h>
Masahiro Yamada5d97dff2016-09-21 11:28:57 +090035#include <linux/errno.h>
Jean-Jacques Hiblot1708a122019-09-11 11:33:46 +020036#include <usb/xhci.h>
Vivek Gautam5853e132013-09-14 14:02:45 +053037
38#ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
39#define CONFIG_USB_MAX_CONTROLLER_COUNT 1
40#endif
41
42static struct descriptor {
43 struct usb_hub_descriptor hub;
44 struct usb_device_descriptor device;
45 struct usb_config_descriptor config;
46 struct usb_interface_descriptor interface;
47 struct usb_endpoint_descriptor endpoint;
48 struct usb_ss_ep_comp_descriptor ep_companion;
49} __attribute__ ((packed)) descriptor = {
50 {
51 0xc, /* bDescLength */
52 0x2a, /* bDescriptorType: hub descriptor */
53 2, /* bNrPorts -- runtime modified */
54 cpu_to_le16(0x8), /* wHubCharacteristics */
55 10, /* bPwrOn2PwrGood */
56 0, /* bHubCntrCurrent */
Bin Meng337fc7e2017-07-19 21:50:00 +080057 { /* Device removable */
58 } /* at most 7 ports! XXX */
Vivek Gautam5853e132013-09-14 14:02:45 +053059 },
60 {
61 0x12, /* bLength */
62 1, /* bDescriptorType: UDESC_DEVICE */
63 cpu_to_le16(0x0300), /* bcdUSB: v3.0 */
64 9, /* bDeviceClass: UDCLASS_HUB */
65 0, /* bDeviceSubClass: UDSUBCLASS_HUB */
66 3, /* bDeviceProtocol: UDPROTO_SSHUBSTT */
67 9, /* bMaxPacketSize: 512 bytes 2^9 */
68 0x0000, /* idVendor */
69 0x0000, /* idProduct */
70 cpu_to_le16(0x0100), /* bcdDevice */
71 1, /* iManufacturer */
72 2, /* iProduct */
73 0, /* iSerialNumber */
74 1 /* bNumConfigurations: 1 */
75 },
76 {
77 0x9,
78 2, /* bDescriptorType: UDESC_CONFIG */
79 cpu_to_le16(0x1f), /* includes SS endpoint descriptor */
80 1, /* bNumInterface */
81 1, /* bConfigurationValue */
82 0, /* iConfiguration */
83 0x40, /* bmAttributes: UC_SELF_POWER */
84 0 /* bMaxPower */
85 },
86 {
87 0x9, /* bLength */
88 4, /* bDescriptorType: UDESC_INTERFACE */
89 0, /* bInterfaceNumber */
90 0, /* bAlternateSetting */
91 1, /* bNumEndpoints */
92 9, /* bInterfaceClass: UICLASS_HUB */
93 0, /* bInterfaceSubClass: UISUBCLASS_HUB */
94 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */
95 0 /* iInterface */
96 },
97 {
98 0x7, /* bLength */
99 5, /* bDescriptorType: UDESC_ENDPOINT */
100 0x81, /* bEndpointAddress: IN endpoint 1 */
101 3, /* bmAttributes: UE_INTERRUPT */
102 8, /* wMaxPacketSize */
103 255 /* bInterval */
104 },
105 {
106 0x06, /* ss_bLength */
107 0x30, /* ss_bDescriptorType: SS EP Companion */
108 0x00, /* ss_bMaxBurst: allows 1 TX between ACKs */
109 /* ss_bmAttributes: 1 packet per service interval */
110 0x00,
111 /* ss_wBytesPerInterval: 15 bits for max 15 ports */
112 cpu_to_le16(0x02),
113 },
114};
115
Sven Schwermerfd09c202018-11-21 08:43:56 +0100116#if !CONFIG_IS_ENABLED(DM_USB)
Vivek Gautam5853e132013-09-14 14:02:45 +0530117static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT];
Simon Glassa5762fe2015-03-25 12:22:53 -0600118#endif
Vivek Gautam5853e132013-09-14 14:02:45 +0530119
Simon Glass7c1deec2015-03-25 12:22:49 -0600120struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev)
121{
Sven Schwermerfd09c202018-11-21 08:43:56 +0100122#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa5762fe2015-03-25 12:22:53 -0600123 struct udevice *dev;
124
125 /* Find the USB controller */
126 for (dev = udev->dev;
127 device_get_uclass_id(dev) != UCLASS_USB;
128 dev = dev->parent)
129 ;
130 return dev_get_priv(dev);
131#else
Simon Glass7c1deec2015-03-25 12:22:49 -0600132 return udev->controller;
Simon Glassa5762fe2015-03-25 12:22:53 -0600133#endif
Simon Glass7c1deec2015-03-25 12:22:49 -0600134}
135
Vivek Gautam5853e132013-09-14 14:02:45 +0530136/**
137 * Waits for as per specified amount of time
138 * for the "result" to match with "done"
139 *
140 * @param ptr pointer to the register to be read
141 * @param mask mask for the value read
142 * @param done value to be campared with result
143 * @param usec time to wait till
144 * @return 0 if handshake is success else < 0 on failure
145 */
146static int handshake(uint32_t volatile *ptr, uint32_t mask,
147 uint32_t done, int usec)
148{
149 uint32_t result;
150
151 do {
152 result = xhci_readl(ptr);
153 if (result == ~(uint32_t)0)
154 return -ENODEV;
155 result &= mask;
156 if (result == done)
157 return 0;
158 usec--;
159 udelay(1);
160 } while (usec > 0);
161
162 return -ETIMEDOUT;
163}
164
165/**
166 * Set the run bit and wait for the host to be running.
167 *
168 * @param hcor pointer to host controller operation registers
169 * @return status of the Handshake
170 */
171static int xhci_start(struct xhci_hcor *hcor)
172{
173 u32 temp;
174 int ret;
175
176 puts("Starting the controller\n");
177 temp = xhci_readl(&hcor->or_usbcmd);
178 temp |= (CMD_RUN);
179 xhci_writel(&hcor->or_usbcmd, temp);
180
181 /*
182 * Wait for the HCHalted Status bit to be 0 to indicate the host is
183 * running.
184 */
185 ret = handshake(&hcor->or_usbsts, STS_HALT, 0, XHCI_MAX_HALT_USEC);
186 if (ret)
187 debug("Host took too long to start, "
188 "waited %u microseconds.\n",
189 XHCI_MAX_HALT_USEC);
190 return ret;
191}
192
193/**
194 * Resets the XHCI Controller
195 *
196 * @param hcor pointer to host controller operation registers
197 * @return -EBUSY if XHCI Controller is not halted else status of handshake
198 */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900199static int xhci_reset(struct xhci_hcor *hcor)
Vivek Gautam5853e132013-09-14 14:02:45 +0530200{
201 u32 cmd;
202 u32 state;
203 int ret;
204
205 /* Halting the Host first */
Sergey Temerkhanova5ccda42015-08-17 15:38:07 +0300206 debug("// Halt the HC: %p\n", hcor);
Vivek Gautam5853e132013-09-14 14:02:45 +0530207 state = xhci_readl(&hcor->or_usbsts) & STS_HALT;
208 if (!state) {
209 cmd = xhci_readl(&hcor->or_usbcmd);
210 cmd &= ~CMD_RUN;
211 xhci_writel(&hcor->or_usbcmd, cmd);
212 }
213
214 ret = handshake(&hcor->or_usbsts,
215 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
216 if (ret) {
217 printf("Host not halted after %u microseconds.\n",
218 XHCI_MAX_HALT_USEC);
219 return -EBUSY;
220 }
221
222 debug("// Reset the HC\n");
223 cmd = xhci_readl(&hcor->or_usbcmd);
224 cmd |= CMD_RESET;
225 xhci_writel(&hcor->or_usbcmd, cmd);
226
227 ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC);
228 if (ret)
229 return ret;
230
231 /*
232 * xHCI cannot write to any doorbells or operational registers other
233 * than status until the "Controller Not Ready" flag is cleared.
234 */
235 return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC);
236}
237
238/**
239 * Used for passing endpoint bitmasks between the core and HCDs.
240 * Find the index for an endpoint given its descriptor.
241 * Use the return value to right shift 1 for the bitmask.
242 *
243 * Index = (epnum * 2) + direction - 1,
244 * where direction = 0 for OUT, 1 for IN.
245 * For control endpoints, the IN index is used (OUT index is unused), so
246 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
247 *
248 * @param desc USB enpdoint Descriptor
249 * @return index of the Endpoint
250 */
251static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc)
252{
253 unsigned int index;
254
255 if (usb_endpoint_xfer_control(desc))
256 index = (unsigned int)(usb_endpoint_num(desc) * 2);
257 else
258 index = (unsigned int)((usb_endpoint_num(desc) * 2) -
259 (usb_endpoint_dir_in(desc) ? 0 : 1));
260
261 return index;
262}
263
Bin Mengf51966b2017-09-18 06:40:47 -0700264/*
265 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
266 * microframes, rounded down to nearest power of 2.
267 */
268static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval,
269 unsigned int min_exponent,
270 unsigned int max_exponent)
271{
272 unsigned int interval;
273
274 interval = fls(desc_interval) - 1;
275 interval = clamp_val(interval, min_exponent, max_exponent);
276 if ((1 << interval) != desc_interval)
277 debug("rounding interval to %d microframes, "\
278 "ep desc says %d microframes\n",
279 1 << interval, desc_interval);
280
281 return interval;
282}
283
284static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
285 struct usb_endpoint_descriptor *endpt_desc)
286{
287 if (endpt_desc->bInterval == 0)
288 return 0;
289
290 return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15);
291}
292
293static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
294 struct usb_endpoint_descriptor *endpt_desc)
295{
296 return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10);
297}
298
299/*
300 * Convert interval expressed as 2^(bInterval - 1) == interval into
301 * straight exponent value 2^n == interval.
302 */
303static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
304 struct usb_endpoint_descriptor *endpt_desc)
305{
306 unsigned int interval;
307
308 interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1;
309 if (interval != endpt_desc->bInterval - 1)
310 debug("ep %#x - rounding interval to %d %sframes\n",
311 endpt_desc->bEndpointAddress, 1 << interval,
312 udev->speed == USB_SPEED_FULL ? "" : "micro");
313
314 if (udev->speed == USB_SPEED_FULL) {
315 /*
316 * Full speed isoc endpoints specify interval in frames,
317 * not microframes. We are using microframes everywhere,
318 * so adjust accordingly.
319 */
320 interval += 3; /* 1 frame = 2^3 uframes */
321 }
322
323 return interval;
324}
325
326/*
327 * Return the polling or NAK interval.
328 *
329 * The polling interval is expressed in "microframes". If xHCI's Interval field
330 * is set to N, it will service the endpoint every 2^(Interval)*125us.
331 *
332 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
333 * is set to 0.
334 */
335static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
336 struct usb_endpoint_descriptor *endpt_desc)
337{
338 unsigned int interval = 0;
339
340 switch (udev->speed) {
341 case USB_SPEED_HIGH:
342 /* Max NAK rate */
343 if (usb_endpoint_xfer_control(endpt_desc) ||
344 usb_endpoint_xfer_bulk(endpt_desc)) {
345 interval = xhci_parse_microframe_interval(udev,
346 endpt_desc);
347 break;
348 }
349 /* Fall through - SS and HS isoc/int have same decoding */
350
351 case USB_SPEED_SUPER:
352 if (usb_endpoint_xfer_int(endpt_desc) ||
353 usb_endpoint_xfer_isoc(endpt_desc)) {
354 interval = xhci_parse_exponent_interval(udev,
355 endpt_desc);
356 }
357 break;
358
359 case USB_SPEED_FULL:
360 if (usb_endpoint_xfer_isoc(endpt_desc)) {
361 interval = xhci_parse_exponent_interval(udev,
362 endpt_desc);
363 break;
364 }
365 /*
366 * Fall through for interrupt endpoint interval decoding
367 * since it uses the same rules as low speed interrupt
368 * endpoints.
369 */
370
371 case USB_SPEED_LOW:
372 if (usb_endpoint_xfer_int(endpt_desc) ||
373 usb_endpoint_xfer_isoc(endpt_desc)) {
374 interval = xhci_parse_frame_interval(udev, endpt_desc);
375 }
376 break;
377
378 default:
379 BUG();
380 }
381
382 return interval;
383}
384
385/*
386 * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
387 * High speed endpoint descriptors can define "the number of additional
388 * transaction opportunities per microframe", but that goes in the Max Burst
389 * endpoint context field.
390 */
391static u32 xhci_get_endpoint_mult(struct usb_device *udev,
392 struct usb_endpoint_descriptor *endpt_desc,
393 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
394{
395 if (udev->speed < USB_SPEED_SUPER ||
396 !usb_endpoint_xfer_isoc(endpt_desc))
397 return 0;
398
399 return ss_ep_comp_desc->bmAttributes;
400}
401
Bin Mengfa483b22017-09-18 06:40:48 -0700402static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
403 struct usb_endpoint_descriptor *endpt_desc,
404 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
405{
406 /* Super speed and Plus have max burst in ep companion desc */
407 if (udev->speed >= USB_SPEED_SUPER)
408 return ss_ep_comp_desc->bMaxBurst;
409
410 if (udev->speed == USB_SPEED_HIGH &&
411 (usb_endpoint_xfer_isoc(endpt_desc) ||
412 usb_endpoint_xfer_int(endpt_desc)))
413 return usb_endpoint_maxp_mult(endpt_desc) - 1;
414
415 return 0;
416}
417
Bin Mengf51966b2017-09-18 06:40:47 -0700418/*
419 * Return the maximum endpoint service interval time (ESIT) payload.
420 * Basically, this is the maxpacket size, multiplied by the burst size
421 * and mult size.
422 */
423static u32 xhci_get_max_esit_payload(struct usb_device *udev,
424 struct usb_endpoint_descriptor *endpt_desc,
425 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc)
426{
427 int max_burst;
428 int max_packet;
429
430 /* Only applies for interrupt or isochronous endpoints */
431 if (usb_endpoint_xfer_control(endpt_desc) ||
432 usb_endpoint_xfer_bulk(endpt_desc))
433 return 0;
434
435 /* SuperSpeed Isoc ep with less than 48k per esit */
436 if (udev->speed >= USB_SPEED_SUPER)
437 return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval);
438
439 max_packet = usb_endpoint_maxp(endpt_desc);
440 max_burst = usb_endpoint_maxp_mult(endpt_desc);
441
442 /* A 0 in max burst means 1 transfer per ESIT */
443 return max_packet * max_burst;
444}
445
Vivek Gautam5853e132013-09-14 14:02:45 +0530446/**
447 * Issue a configure endpoint command or evaluate context command
448 * and wait for it to finish.
449 *
450 * @param udev pointer to the Device Data Structure
451 * @param ctx_change flag to indicate the Context has changed or NOT
452 * @return 0 on success, -1 on failure
453 */
454static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change)
455{
456 struct xhci_container_ctx *in_ctx;
457 struct xhci_virt_device *virt_dev;
Simon Glass7c1deec2015-03-25 12:22:49 -0600458 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +0530459 union xhci_trb *event;
460
461 virt_dev = ctrl->devs[udev->slot_id];
462 in_ctx = virt_dev->in_ctx;
463
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300464 xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size);
Vivek Gautam5853e132013-09-14 14:02:45 +0530465 xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0,
466 ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP);
467 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
468 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags))
469 != udev->slot_id);
470
471 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
472 case COMP_SUCCESS:
473 debug("Successful %s command\n",
474 ctx_change ? "Evaluate Context" : "Configure Endpoint");
475 break;
476 default:
477 printf("ERROR: %s command returned completion code %d.\n",
478 ctx_change ? "Evaluate Context" : "Configure Endpoint",
479 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
480 return -EINVAL;
481 }
482
483 xhci_acknowledge_event(ctrl);
484
485 return 0;
486}
487
488/**
489 * Configure the endpoint, programming the device contexts.
490 *
491 * @param udev pointer to the USB device structure
492 * @return returns the status of the xhci_configure_endpoints
493 */
494static int xhci_set_configuration(struct usb_device *udev)
495{
496 struct xhci_container_ctx *in_ctx;
497 struct xhci_container_ctx *out_ctx;
498 struct xhci_input_control_ctx *ctrl_ctx;
499 struct xhci_slot_ctx *slot_ctx;
500 struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM];
501 int cur_ep;
502 int max_ep_flag = 0;
503 int ep_index;
504 unsigned int dir;
505 unsigned int ep_type;
Simon Glass7c1deec2015-03-25 12:22:49 -0600506 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +0530507 int num_of_ep;
508 int ep_flag = 0;
509 u64 trb_64 = 0;
510 int slot_id = udev->slot_id;
511 struct xhci_virt_device *virt_dev = ctrl->devs[slot_id];
512 struct usb_interface *ifdesc;
Bin Mengf51966b2017-09-18 06:40:47 -0700513 u32 max_esit_payload;
514 unsigned int interval;
515 unsigned int mult;
Bin Mengfa483b22017-09-18 06:40:48 -0700516 unsigned int max_burst;
Bin Mengf51966b2017-09-18 06:40:47 -0700517 unsigned int avg_trb_len;
Bin Mengab2b7272017-09-18 06:40:49 -0700518 unsigned int err_count = 0;
Vivek Gautam5853e132013-09-14 14:02:45 +0530519
520 out_ctx = virt_dev->out_ctx;
521 in_ctx = virt_dev->in_ctx;
522
523 num_of_ep = udev->config.if_desc[0].no_of_ep;
524 ifdesc = &udev->config.if_desc[0];
525
526 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Bin Mengaab0db02017-07-19 21:49:56 +0800527 /* Initialize the input context control */
528 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Vivek Gautam5853e132013-09-14 14:02:45 +0530529 ctrl_ctx->drop_flags = 0;
530
531 /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */
532 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
533 ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]);
534 ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1));
535 if (max_ep_flag < ep_flag)
536 max_ep_flag = ep_flag;
537 }
538
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300539 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
Vivek Gautam5853e132013-09-14 14:02:45 +0530540
541 /* slot context */
542 xhci_slot_copy(ctrl, in_ctx, out_ctx);
543 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
Bin Menge4040662018-05-23 23:40:50 -0700544 slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK));
Vivek Gautam5853e132013-09-14 14:02:45 +0530545 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0);
546
547 xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0);
548
549 /* filling up ep contexts */
550 for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) {
551 struct usb_endpoint_descriptor *endpt_desc = NULL;
Bin Mengf51966b2017-09-18 06:40:47 -0700552 struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL;
Vivek Gautam5853e132013-09-14 14:02:45 +0530553
554 endpt_desc = &ifdesc->ep_desc[cur_ep];
Bin Mengf51966b2017-09-18 06:40:47 -0700555 ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep];
Vivek Gautam5853e132013-09-14 14:02:45 +0530556 trb_64 = 0;
557
Bin Mengf51966b2017-09-18 06:40:47 -0700558 /*
559 * Get values to fill the endpoint context, mostly from ep
560 * descriptor. The average TRB buffer lengt for bulk endpoints
561 * is unclear as we have no clue on scatter gather list entry
562 * size. For Isoc and Int, set it to max available.
563 * See xHCI 1.1 spec 4.14.1.1 for details.
564 */
565 max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc,
566 ss_ep_comp_desc);
567 interval = xhci_get_endpoint_interval(udev, endpt_desc);
568 mult = xhci_get_endpoint_mult(udev, endpt_desc,
569 ss_ep_comp_desc);
Bin Mengfa483b22017-09-18 06:40:48 -0700570 max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc,
571 ss_ep_comp_desc);
Bin Mengf51966b2017-09-18 06:40:47 -0700572 avg_trb_len = max_esit_payload;
573
Vivek Gautam5853e132013-09-14 14:02:45 +0530574 ep_index = xhci_get_ep_index(endpt_desc);
575 ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
576
577 /* Allocate the ep rings */
578 virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true);
579 if (!virt_dev->eps[ep_index].ring)
580 return -ENOMEM;
581
582 /*NOTE: ep_desc[0] actually represents EP1 and so on */
583 dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7);
584 ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2));
Bin Mengf51966b2017-09-18 06:40:47 -0700585
586 ep_ctx[ep_index]->ep_info =
587 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
588 EP_INTERVAL(interval) | EP_MULT(mult));
589
Vivek Gautam5853e132013-09-14 14:02:45 +0530590 ep_ctx[ep_index]->ep_info2 =
591 cpu_to_le32(ep_type << EP_TYPE_SHIFT);
592 ep_ctx[ep_index]->ep_info2 |=
593 cpu_to_le32(MAX_PACKET
594 (get_unaligned(&endpt_desc->wMaxPacketSize)));
595
Bin Mengab2b7272017-09-18 06:40:49 -0700596 /* Allow 3 retries for everything but isoc, set CErr = 3 */
597 if (!usb_endpoint_xfer_isoc(endpt_desc))
598 err_count = 3;
Vivek Gautam5853e132013-09-14 14:02:45 +0530599 ep_ctx[ep_index]->ep_info2 |=
Bin Mengfa483b22017-09-18 06:40:48 -0700600 cpu_to_le32(MAX_BURST(max_burst) |
Bin Mengab2b7272017-09-18 06:40:49 -0700601 ERROR_COUNT(err_count));
Vivek Gautam5853e132013-09-14 14:02:45 +0530602
603 trb_64 = (uintptr_t)
604 virt_dev->eps[ep_index].ring->enqueue;
605 ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 |
606 virt_dev->eps[ep_index].ring->cycle_state);
Bin Mengf51966b2017-09-18 06:40:47 -0700607
Bin Mengfae35852017-09-18 06:40:50 -0700608 /*
609 * xHCI spec 6.2.3:
610 * 'Average TRB Length' should be 8 for control endpoints.
611 */
612 if (usb_endpoint_xfer_control(endpt_desc))
613 avg_trb_len = 8;
Bin Mengf51966b2017-09-18 06:40:47 -0700614 ep_ctx[ep_index]->tx_info =
615 cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
616 EP_AVG_TRB_LENGTH(avg_trb_len));
Chunfeng Yun74102832020-05-02 11:35:18 +0200617
618 /*
619 * The MediaTek xHCI defines some extra SW parameters which
620 * are put into reserved DWs in Slot and Endpoint Contexts
621 * for synchronous endpoints.
622 */
623 if (IS_ENABLED(CONFIG_USB_XHCI_MTK)) {
624 ep_ctx[ep_index]->reserved[0] =
625 cpu_to_le32(EP_BPKTS(1) | EP_BBM(1));
626 }
Vivek Gautam5853e132013-09-14 14:02:45 +0530627 }
628
629 return xhci_configure_endpoints(udev, false);
630}
631
632/**
633 * Issue an Address Device command (which will issue a SetAddress request to
634 * the device).
635 *
636 * @param udev pointer to the Device Data Structure
637 * @return 0 if successful else error code on failure
638 */
Simon Glass5dd75e32015-03-25 12:22:51 -0600639static int xhci_address_device(struct usb_device *udev, int root_portnr)
Vivek Gautam5853e132013-09-14 14:02:45 +0530640{
641 int ret = 0;
Simon Glass7c1deec2015-03-25 12:22:49 -0600642 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +0530643 struct xhci_slot_ctx *slot_ctx;
644 struct xhci_input_control_ctx *ctrl_ctx;
645 struct xhci_virt_device *virt_dev;
646 int slot_id = udev->slot_id;
647 union xhci_trb *event;
648
649 virt_dev = ctrl->devs[slot_id];
650
651 /*
652 * This is the first Set Address since device plug-in
653 * so setting up the slot context.
654 */
Simon Glass5dd75e32015-03-25 12:22:51 -0600655 debug("Setting up addressable devices %p\n", ctrl->dcbaa);
Bin Mengdaec4692017-07-19 21:51:14 +0800656 xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr);
Vivek Gautam5853e132013-09-14 14:02:45 +0530657
658 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
659 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
660 ctrl_ctx->drop_flags = 0;
661
662 xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV);
663 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
664 BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id);
665
666 switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) {
667 case COMP_CTX_STATE:
668 case COMP_EBADSLT:
669 printf("Setup ERROR: address device command for slot %d.\n",
670 slot_id);
671 ret = -EINVAL;
672 break;
673 case COMP_TX_ERR:
674 puts("Device not responding to set address.\n");
675 ret = -EPROTO;
676 break;
677 case COMP_DEV_ERR:
678 puts("ERROR: Incompatible device"
679 "for address device command.\n");
680 ret = -ENODEV;
681 break;
682 case COMP_SUCCESS:
683 debug("Successful Address Device command\n");
684 udev->status = 0;
685 break;
686 default:
687 printf("ERROR: unexpected command completion code 0x%x.\n",
688 GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)));
689 ret = -EINVAL;
690 break;
691 }
692
693 xhci_acknowledge_event(ctrl);
694
695 if (ret < 0)
696 /*
697 * TODO: Unsuccessful Address Device command shall leave the
698 * slot in default state. So, issue Disable Slot command now.
699 */
700 return ret;
701
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300702 xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes,
703 virt_dev->out_ctx->size);
Vivek Gautam5853e132013-09-14 14:02:45 +0530704 slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx);
705
706 debug("xHC internal address is: %d\n",
707 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
708
709 return 0;
710}
711
712/**
713 * Issue Enable slot command to the controller to allocate
714 * device slot and assign the slot id. It fails if the xHC
715 * ran out of device slots, the Enable Slot command timed out,
716 * or allocating memory failed.
717 *
718 * @param udev pointer to the Device Data Structure
719 * @return Returns 0 on succes else return error code on failure
720 */
Masahiro Yamada121a4d12017-06-22 16:35:14 +0900721static int _xhci_alloc_device(struct usb_device *udev)
Vivek Gautam5853e132013-09-14 14:02:45 +0530722{
Simon Glass7c1deec2015-03-25 12:22:49 -0600723 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +0530724 union xhci_trb *event;
Vivek Gautam5853e132013-09-14 14:02:45 +0530725 int ret;
726
727 /*
728 * Root hub will be first device to be initailized.
729 * If this device is root-hub, don't do any xHC related
730 * stuff.
731 */
732 if (ctrl->rootdev == 0) {
733 udev->speed = USB_SPEED_SUPER;
734 return 0;
735 }
736
737 xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT);
738 event = xhci_wait_for_event(ctrl, TRB_COMPLETION);
739 BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))
740 != COMP_SUCCESS);
741
742 udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags));
743
744 xhci_acknowledge_event(ctrl);
745
Simon Glass7e0c5ee2015-03-25 12:22:50 -0600746 ret = xhci_alloc_virt_device(ctrl, udev->slot_id);
Vivek Gautam5853e132013-09-14 14:02:45 +0530747 if (ret < 0) {
748 /*
749 * TODO: Unsuccessful Address Device command shall leave
750 * the slot in default. So, issue Disable Slot command now.
751 */
752 puts("Could not allocate xHCI USB device data structures\n");
753 return ret;
754 }
755
756 return 0;
757}
758
Sven Schwermerfd09c202018-11-21 08:43:56 +0100759#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glassa5762fe2015-03-25 12:22:53 -0600760int usb_alloc_device(struct usb_device *udev)
761{
762 return _xhci_alloc_device(udev);
763}
764#endif
765
Vivek Gautam5853e132013-09-14 14:02:45 +0530766/*
767 * Full speed devices may have a max packet size greater than 8 bytes, but the
768 * USB core doesn't know that until it reads the first 8 bytes of the
769 * descriptor. If the usb_device's max packet size changes after that point,
770 * we need to issue an evaluate context command and wait on it.
771 *
772 * @param udev pointer to the Device Data Structure
773 * @return returns the status of the xhci_configure_endpoints
774 */
775int xhci_check_maxpacket(struct usb_device *udev)
776{
Simon Glass7c1deec2015-03-25 12:22:49 -0600777 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +0530778 unsigned int slot_id = udev->slot_id;
779 int ep_index = 0; /* control endpoint */
780 struct xhci_container_ctx *in_ctx;
781 struct xhci_container_ctx *out_ctx;
782 struct xhci_input_control_ctx *ctrl_ctx;
783 struct xhci_ep_ctx *ep_ctx;
784 int max_packet_size;
785 int hw_max_packet_size;
786 int ret = 0;
Vivek Gautam5853e132013-09-14 14:02:45 +0530787
788 out_ctx = ctrl->devs[slot_id]->out_ctx;
Sergey Temerkhanov421a5a02015-04-01 17:18:45 +0300789 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
Vivek Gautam5853e132013-09-14 14:02:45 +0530790
791 ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index);
792 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Bin Mengb5aa8572017-09-18 06:40:44 -0700793 max_packet_size = udev->epmaxpacketin[0];
Vivek Gautam5853e132013-09-14 14:02:45 +0530794 if (hw_max_packet_size != max_packet_size) {
795 debug("Max Packet Size for ep 0 changed.\n");
796 debug("Max packet size in usb_device = %d\n", max_packet_size);
797 debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size);
798 debug("Issuing evaluate context command.\n");
799
800 /* Set up the modified control endpoint 0 */
801 xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx,
802 ctrl->devs[slot_id]->out_ctx, ep_index);
803 in_ctx = ctrl->devs[slot_id]->in_ctx;
804 ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index);
Bin Mengb5aa8572017-09-18 06:40:44 -0700805 ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK)
806 << MAX_PACKET_SHIFT));
Vivek Gautam5853e132013-09-14 14:02:45 +0530807 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
808
809 /*
810 * Set up the input context flags for the command
811 * FIXME: This won't work if a non-default control endpoint
812 * changes max packet sizes.
813 */
814 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
815 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
816 ctrl_ctx->drop_flags = 0;
817
818 ret = xhci_configure_endpoints(udev, true);
819 }
820 return ret;
821}
822
823/**
824 * Clears the Change bits of the Port Status Register
825 *
826 * @param wValue request value
827 * @param wIndex request index
828 * @param addr address of posrt status register
829 * @param port_status state of port status register
830 * @return none
831 */
832static void xhci_clear_port_change_bit(u16 wValue,
833 u16 wIndex, volatile uint32_t *addr, u32 port_status)
834{
835 char *port_change_bit;
836 u32 status;
837
838 switch (wValue) {
839 case USB_PORT_FEAT_C_RESET:
840 status = PORT_RC;
841 port_change_bit = "reset";
842 break;
843 case USB_PORT_FEAT_C_CONNECTION:
844 status = PORT_CSC;
845 port_change_bit = "connect";
846 break;
847 case USB_PORT_FEAT_C_OVER_CURRENT:
848 status = PORT_OCC;
849 port_change_bit = "over-current";
850 break;
851 case USB_PORT_FEAT_C_ENABLE:
852 status = PORT_PEC;
853 port_change_bit = "enable/disable";
854 break;
855 case USB_PORT_FEAT_C_SUSPEND:
856 status = PORT_PLC;
857 port_change_bit = "suspend/resume";
858 break;
859 default:
860 /* Should never happen */
861 return;
862 }
863
864 /* Change bits are all write 1 to clear */
865 xhci_writel(addr, port_status | status);
866
867 port_status = xhci_readl(addr);
868 debug("clear port %s change, actual port %d status = 0x%x\n",
869 port_change_bit, wIndex, port_status);
870}
871
872/**
873 * Save Read Only (RO) bits and save read/write bits where
874 * writing a 0 clears the bit and writing a 1 sets the bit (RWS).
875 * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect.
876 *
877 * @param state state of the Port Status and Control Regsiter
878 * @return a value that would result in the port being in the
879 * same state, if the value was written to the port
880 * status control register.
881 */
882static u32 xhci_port_state_to_neutral(u32 state)
883{
884 /* Save read-only status and port state */
885 return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS);
886}
887
888/**
889 * Submits the Requests to the XHCI Host Controller
890 *
891 * @param udev pointer to the USB device structure
892 * @param pipe contains the DIR_IN or OUT , devnum
893 * @param buffer buffer to be read/written based on the request
894 * @return returns 0 if successful else -1 on failure
895 */
896static int xhci_submit_root(struct usb_device *udev, unsigned long pipe,
897 void *buffer, struct devrequest *req)
898{
899 uint8_t tmpbuf[4];
900 u16 typeReq;
901 void *srcptr = NULL;
902 int len, srclen;
903 uint32_t reg;
904 volatile uint32_t *status_reg;
Simon Glass7c1deec2015-03-25 12:22:49 -0600905 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Bin Meng72746712017-07-19 21:50:03 +0800906 struct xhci_hccr *hccr = ctrl->hccr;
Vivek Gautam5853e132013-09-14 14:02:45 +0530907 struct xhci_hcor *hcor = ctrl->hcor;
Bin Meng72746712017-07-19 21:50:03 +0800908 int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1));
Vivek Gautam5853e132013-09-14 14:02:45 +0530909
Jeroen Hofstee25d19362014-06-12 00:31:27 +0200910 if ((req->requesttype & USB_RT_PORT) &&
Bin Meng72746712017-07-19 21:50:03 +0800911 le16_to_cpu(req->index) > max_ports) {
912 printf("The request port(%d) exceeds maximum port number\n",
913 le16_to_cpu(req->index) - 1);
Vivek Gautam5853e132013-09-14 14:02:45 +0530914 return -EINVAL;
915 }
916
917 status_reg = (volatile uint32_t *)
918 (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc);
919 srclen = 0;
920
921 typeReq = req->request | req->requesttype << 8;
922
923 switch (typeReq) {
924 case DeviceRequest | USB_REQ_GET_DESCRIPTOR:
925 switch (le16_to_cpu(req->value) >> 8) {
926 case USB_DT_DEVICE:
927 debug("USB_DT_DEVICE request\n");
928 srcptr = &descriptor.device;
929 srclen = 0x12;
930 break;
931 case USB_DT_CONFIG:
932 debug("USB_DT_CONFIG config\n");
933 srcptr = &descriptor.config;
934 srclen = 0x19;
935 break;
936 case USB_DT_STRING:
937 debug("USB_DT_STRING config\n");
938 switch (le16_to_cpu(req->value) & 0xff) {
939 case 0: /* Language */
940 srcptr = "\4\3\11\4";
941 srclen = 4;
942 break;
943 case 1: /* Vendor String */
Simon Glassf161c172015-03-25 12:22:54 -0600944 srcptr = "\16\3U\0-\0B\0o\0o\0t\0";
Vivek Gautam5853e132013-09-14 14:02:45 +0530945 srclen = 14;
946 break;
947 case 2: /* Product Name */
948 srcptr = "\52\3X\0H\0C\0I\0 "
949 "\0H\0o\0s\0t\0 "
950 "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0";
951 srclen = 42;
952 break;
953 default:
954 printf("unknown value DT_STRING %x\n",
955 le16_to_cpu(req->value));
956 goto unknown;
957 }
958 break;
959 default:
960 printf("unknown value %x\n", le16_to_cpu(req->value));
961 goto unknown;
962 }
963 break;
964 case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8):
965 switch (le16_to_cpu(req->value) >> 8) {
966 case USB_DT_HUB:
Bin Mengf3421192017-07-19 21:49:58 +0800967 case USB_DT_SS_HUB:
Vivek Gautam5853e132013-09-14 14:02:45 +0530968 debug("USB_DT_HUB config\n");
969 srcptr = &descriptor.hub;
970 srclen = 0x8;
971 break;
972 default:
973 printf("unknown value %x\n", le16_to_cpu(req->value));
974 goto unknown;
975 }
976 break;
977 case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8):
978 debug("USB_REQ_SET_ADDRESS\n");
979 ctrl->rootdev = le16_to_cpu(req->value);
980 break;
981 case DeviceOutRequest | USB_REQ_SET_CONFIGURATION:
982 /* Do nothing */
983 break;
984 case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8):
985 tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */
986 tmpbuf[1] = 0;
987 srcptr = tmpbuf;
988 srclen = 2;
989 break;
990 case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8):
991 memset(tmpbuf, 0, 4);
992 reg = xhci_readl(status_reg);
993 if (reg & PORT_CONNECT) {
994 tmpbuf[0] |= USB_PORT_STAT_CONNECTION;
995 switch (reg & DEV_SPEED_MASK) {
996 case XDEV_FS:
997 debug("SPEED = FULLSPEED\n");
998 break;
999 case XDEV_LS:
1000 debug("SPEED = LOWSPEED\n");
1001 tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8;
1002 break;
1003 case XDEV_HS:
1004 debug("SPEED = HIGHSPEED\n");
1005 tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8;
1006 break;
1007 case XDEV_SS:
1008 debug("SPEED = SUPERSPEED\n");
1009 tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8;
1010 break;
1011 }
1012 }
1013 if (reg & PORT_PE)
1014 tmpbuf[0] |= USB_PORT_STAT_ENABLE;
1015 if ((reg & PORT_PLS_MASK) == XDEV_U3)
1016 tmpbuf[0] |= USB_PORT_STAT_SUSPEND;
1017 if (reg & PORT_OC)
1018 tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT;
1019 if (reg & PORT_RESET)
1020 tmpbuf[0] |= USB_PORT_STAT_RESET;
1021 if (reg & PORT_POWER)
1022 /*
1023 * XXX: This Port power bit (for USB 3.0 hub)
1024 * we are faking in USB 2.0 hub port status;
1025 * since there's a change in bit positions in
1026 * two:
1027 * USB 2.0 port status PP is at position[8]
1028 * USB 3.0 port status PP is at position[9]
1029 * So, we are still keeping it at position [8]
1030 */
1031 tmpbuf[1] |= USB_PORT_STAT_POWER >> 8;
1032 if (reg & PORT_CSC)
1033 tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION;
1034 if (reg & PORT_PEC)
1035 tmpbuf[2] |= USB_PORT_STAT_C_ENABLE;
1036 if (reg & PORT_OCC)
1037 tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT;
1038 if (reg & PORT_RC)
1039 tmpbuf[2] |= USB_PORT_STAT_C_RESET;
1040
1041 srcptr = tmpbuf;
1042 srclen = 4;
1043 break;
1044 case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8):
1045 reg = xhci_readl(status_reg);
1046 reg = xhci_port_state_to_neutral(reg);
1047 switch (le16_to_cpu(req->value)) {
1048 case USB_PORT_FEAT_ENABLE:
1049 reg |= PORT_PE;
1050 xhci_writel(status_reg, reg);
1051 break;
1052 case USB_PORT_FEAT_POWER:
1053 reg |= PORT_POWER;
1054 xhci_writel(status_reg, reg);
1055 break;
1056 case USB_PORT_FEAT_RESET:
1057 reg |= PORT_RESET;
1058 xhci_writel(status_reg, reg);
1059 break;
1060 default:
1061 printf("unknown feature %x\n", le16_to_cpu(req->value));
1062 goto unknown;
1063 }
1064 break;
1065 case USB_REQ_CLEAR_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:
1070 reg &= ~PORT_PE;
1071 break;
1072 case USB_PORT_FEAT_POWER:
1073 reg &= ~PORT_POWER;
1074 break;
1075 case USB_PORT_FEAT_C_RESET:
1076 case USB_PORT_FEAT_C_CONNECTION:
1077 case USB_PORT_FEAT_C_OVER_CURRENT:
1078 case USB_PORT_FEAT_C_ENABLE:
1079 xhci_clear_port_change_bit((le16_to_cpu(req->value)),
1080 le16_to_cpu(req->index),
1081 status_reg, reg);
1082 break;
1083 default:
1084 printf("unknown feature %x\n", le16_to_cpu(req->value));
1085 goto unknown;
1086 }
1087 xhci_writel(status_reg, reg);
1088 break;
1089 default:
1090 puts("Unknown request\n");
1091 goto unknown;
1092 }
1093
1094 debug("scrlen = %d\n req->length = %d\n",
1095 srclen, le16_to_cpu(req->length));
1096
Masahiro Yamadab4141192014-11-07 03:03:31 +09001097 len = min(srclen, (int)le16_to_cpu(req->length));
Vivek Gautam5853e132013-09-14 14:02:45 +05301098
1099 if (srcptr != NULL && len > 0)
1100 memcpy(buffer, srcptr, len);
1101 else
1102 debug("Len is 0\n");
1103
1104 udev->act_len = len;
1105 udev->status = 0;
1106
1107 return 0;
1108
1109unknown:
1110 udev->act_len = 0;
1111 udev->status = USB_ST_STALLED;
1112
1113 return -ENODEV;
1114}
1115
1116/**
1117 * Submits the INT request to XHCI Host cotroller
1118 *
1119 * @param udev pointer to the USB device
1120 * @param pipe contains the DIR_IN or OUT , devnum
1121 * @param buffer buffer to be read/written based on the request
1122 * @param length length of the buffer
1123 * @param interval interval of the interrupt
1124 * @return 0
1125 */
Simon Glassa5762fe2015-03-25 12:22:53 -06001126static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe,
Michal Suchanek34371212019-08-18 10:55:27 +02001127 void *buffer, int length, int interval,
1128 bool nonblock)
Vivek Gautam5853e132013-09-14 14:02:45 +05301129{
Bin Meng1897d602017-09-18 06:40:41 -07001130 if (usb_pipetype(pipe) != PIPE_INTERRUPT) {
1131 printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe));
1132 return -EINVAL;
1133 }
1134
Vivek Gautam5853e132013-09-14 14:02:45 +05301135 /*
Bin Meng1897d602017-09-18 06:40:41 -07001136 * xHCI uses normal TRBs for both bulk and interrupt. When the
1137 * interrupt endpoint is to be serviced, the xHC will consume
1138 * (at most) one TD. A TD (comprised of sg list entries) can
1139 * take several service intervals to transmit.
Vivek Gautam5853e132013-09-14 14:02:45 +05301140 */
Bin Meng1897d602017-09-18 06:40:41 -07001141 return xhci_bulk_tx(udev, pipe, length, buffer);
Vivek Gautam5853e132013-09-14 14:02:45 +05301142}
1143
1144/**
1145 * submit the BULK type of request to the USB Device
1146 *
1147 * @param udev pointer to the USB device
1148 * @param pipe contains the DIR_IN or OUT , devnum
1149 * @param buffer buffer to be read/written based on the request
1150 * @param length length of the buffer
1151 * @return returns 0 if successful else -1 on failure
1152 */
Simon Glassa5762fe2015-03-25 12:22:53 -06001153static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe,
1154 void *buffer, int length)
Vivek Gautam5853e132013-09-14 14:02:45 +05301155{
1156 if (usb_pipetype(pipe) != PIPE_BULK) {
1157 printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe));
1158 return -EINVAL;
1159 }
1160
1161 return xhci_bulk_tx(udev, pipe, length, buffer);
1162}
1163
1164/**
1165 * submit the control type of request to the Root hub/Device based on the devnum
1166 *
1167 * @param udev pointer to the USB device
1168 * @param pipe contains the DIR_IN or OUT , devnum
1169 * @param buffer buffer to be read/written based on the request
1170 * @param length length of the buffer
1171 * @param setup Request type
Simon Glass5dd75e32015-03-25 12:22:51 -06001172 * @param root_portnr Root port number that this device is on
Vivek Gautam5853e132013-09-14 14:02:45 +05301173 * @return returns 0 if successful else -1 on failure
1174 */
Simon Glass5dd75e32015-03-25 12:22:51 -06001175static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe,
1176 void *buffer, int length,
1177 struct devrequest *setup, int root_portnr)
Vivek Gautam5853e132013-09-14 14:02:45 +05301178{
Simon Glass7c1deec2015-03-25 12:22:49 -06001179 struct xhci_ctrl *ctrl = xhci_get_ctrl(udev);
Vivek Gautam5853e132013-09-14 14:02:45 +05301180 int ret = 0;
1181
1182 if (usb_pipetype(pipe) != PIPE_CONTROL) {
1183 printf("non-control pipe (type=%lu)", usb_pipetype(pipe));
1184 return -EINVAL;
1185 }
1186
1187 if (usb_pipedevice(pipe) == ctrl->rootdev)
1188 return xhci_submit_root(udev, pipe, buffer, setup);
1189
Ted Chen1b108882016-03-18 17:56:52 +10301190 if (setup->request == USB_REQ_SET_ADDRESS &&
1191 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD)
Simon Glass5dd75e32015-03-25 12:22:51 -06001192 return xhci_address_device(udev, root_portnr);
Vivek Gautam5853e132013-09-14 14:02:45 +05301193
Ted Chen1b108882016-03-18 17:56:52 +10301194 if (setup->request == USB_REQ_SET_CONFIGURATION &&
1195 (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) {
Vivek Gautam5853e132013-09-14 14:02:45 +05301196 ret = xhci_set_configuration(udev);
1197 if (ret) {
1198 puts("Failed to configure xHCI endpoint\n");
1199 return ret;
1200 }
1201 }
1202
1203 return xhci_ctrl_tx(udev, pipe, setup, length, buffer);
1204}
1205
Simon Glass779d1262015-03-25 12:22:52 -06001206static int xhci_lowlevel_init(struct xhci_ctrl *ctrl)
Vivek Gautam5853e132013-09-14 14:02:45 +05301207{
Simon Glass779d1262015-03-25 12:22:52 -06001208 struct xhci_hccr *hccr;
1209 struct xhci_hcor *hcor;
Vivek Gautam5853e132013-09-14 14:02:45 +05301210 uint32_t val;
1211 uint32_t val2;
1212 uint32_t reg;
Vivek Gautam5853e132013-09-14 14:02:45 +05301213
Simon Glass779d1262015-03-25 12:22:52 -06001214 hccr = ctrl->hccr;
1215 hcor = ctrl->hcor;
Vivek Gautam5853e132013-09-14 14:02:45 +05301216 /*
1217 * Program the Number of Device Slots Enabled field in the CONFIG
1218 * register with the max value of slots the HC can handle.
1219 */
1220 val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK);
1221 val2 = xhci_readl(&hcor->or_config);
1222 val |= (val2 & ~HCS_SLOTS_MASK);
1223 xhci_writel(&hcor->or_config, val);
1224
1225 /* initializing xhci data structures */
1226 if (xhci_mem_init(ctrl, hccr, hcor) < 0)
1227 return -ENOMEM;
1228
1229 reg = xhci_readl(&hccr->cr_hcsparams1);
1230 descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >>
1231 HCS_MAX_PORTS_SHIFT);
1232 printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts);
1233
1234 /* Port Indicators */
1235 reg = xhci_readl(&hccr->cr_hccparams);
1236 if (HCS_INDICATOR(reg))
1237 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1238 | 0x80, &descriptor.hub.wHubCharacteristics);
1239
1240 /* Port Power Control */
1241 if (HCC_PPC(reg))
1242 put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics)
1243 | 0x01, &descriptor.hub.wHubCharacteristics);
1244
1245 if (xhci_start(hcor)) {
1246 xhci_reset(hcor);
1247 return -ENODEV;
1248 }
1249
1250 /* Zero'ing IRQ control register and IRQ pending register */
1251 xhci_writel(&ctrl->ir_set->irq_control, 0x0);
1252 xhci_writel(&ctrl->ir_set->irq_pending, 0x0);
1253
1254 reg = HC_VERSION(xhci_readl(&hccr->cr_capbase));
1255 printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff);
1256
Simon Glass779d1262015-03-25 12:22:52 -06001257 return 0;
1258}
1259
1260static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl)
1261{
1262 u32 temp;
1263
1264 xhci_reset(ctrl->hcor);
1265
1266 debug("// Disabling event ring interrupts\n");
1267 temp = xhci_readl(&ctrl->hcor->or_usbsts);
1268 xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT);
1269 temp = xhci_readl(&ctrl->ir_set->irq_pending);
1270 xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp));
Vivek Gautam5853e132013-09-14 14:02:45 +05301271
1272 return 0;
1273}
1274
Sven Schwermerfd09c202018-11-21 08:43:56 +01001275#if !CONFIG_IS_ENABLED(DM_USB)
Simon Glass5dd75e32015-03-25 12:22:51 -06001276int submit_control_msg(struct usb_device *udev, unsigned long pipe,
1277 void *buffer, int length, struct devrequest *setup)
1278{
1279 struct usb_device *hop = udev;
1280
1281 if (hop->parent)
1282 while (hop->parent->parent)
1283 hop = hop->parent;
1284
1285 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1286 hop->portnr);
1287}
1288
Simon Glassa5762fe2015-03-25 12:22:53 -06001289int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
1290 int length)
1291{
1292 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1293}
1294
1295int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer,
Michal Suchanek34371212019-08-18 10:55:27 +02001296 int length, int interval, bool nonblock)
Simon Glassa5762fe2015-03-25 12:22:53 -06001297{
Michal Suchanek34371212019-08-18 10:55:27 +02001298 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1299 nonblock);
Simon Glassa5762fe2015-03-25 12:22:53 -06001300}
1301
Vivek Gautam5853e132013-09-14 14:02:45 +05301302/**
Simon Glass779d1262015-03-25 12:22:52 -06001303 * Intialises the XHCI host controller
1304 * and allocates the necessary data structures
1305 *
1306 * @param index index to the host controller data structure
1307 * @return pointer to the intialised controller
1308 */
1309int usb_lowlevel_init(int index, enum usb_init_type init, void **controller)
1310{
1311 struct xhci_hccr *hccr;
1312 struct xhci_hcor *hcor;
1313 struct xhci_ctrl *ctrl;
1314 int ret;
1315
Sergey Temerkhanova5ccda42015-08-17 15:38:07 +03001316 *controller = NULL;
1317
Simon Glass779d1262015-03-25 12:22:52 -06001318 if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0)
1319 return -ENODEV;
1320
1321 if (xhci_reset(hcor) != 0)
1322 return -ENODEV;
1323
1324 ctrl = &xhcic[index];
1325
1326 ctrl->hccr = hccr;
1327 ctrl->hcor = hcor;
1328
1329 ret = xhci_lowlevel_init(ctrl);
1330
Sergey Temerkhanova5ccda42015-08-17 15:38:07 +03001331 if (ret) {
1332 ctrl->hccr = NULL;
1333 ctrl->hcor = NULL;
1334 } else {
1335 *controller = &xhcic[index];
1336 }
Simon Glass779d1262015-03-25 12:22:52 -06001337
1338 return ret;
1339}
1340
1341/**
Vivek Gautam5853e132013-09-14 14:02:45 +05301342 * Stops the XHCI host controller
1343 * and cleans up all the related data structures
1344 *
1345 * @param index index to the host controller data structure
1346 * @return none
1347 */
1348int usb_lowlevel_stop(int index)
1349{
1350 struct xhci_ctrl *ctrl = (xhcic + index);
Vivek Gautam5853e132013-09-14 14:02:45 +05301351
Sergey Temerkhanova5ccda42015-08-17 15:38:07 +03001352 if (ctrl->hcor) {
1353 xhci_lowlevel_stop(ctrl);
1354 xhci_hcd_stop(index);
1355 xhci_cleanup(ctrl);
1356 }
Vivek Gautam5853e132013-09-14 14:02:45 +05301357
1358 return 0;
1359}
Sven Schwermerfd09c202018-11-21 08:43:56 +01001360#endif /* CONFIG_IS_ENABLED(DM_USB) */
Simon Glassa5762fe2015-03-25 12:22:53 -06001361
Sven Schwermerfd09c202018-11-21 08:43:56 +01001362#if CONFIG_IS_ENABLED(DM_USB)
Simon Glassa5762fe2015-03-25 12:22:53 -06001363
1364static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev,
1365 unsigned long pipe, void *buffer, int length,
1366 struct devrequest *setup)
1367{
1368 struct usb_device *uhop;
1369 struct udevice *hub;
1370 int root_portnr = 0;
1371
1372 debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__,
1373 dev->name, udev, udev->dev->name, udev->portnr);
1374 hub = udev->dev;
1375 if (device_get_uclass_id(hub) == UCLASS_USB_HUB) {
1376 /* Figure out our port number on the root hub */
Bin Meng46c1d492017-07-19 21:51:11 +08001377 if (usb_hub_is_root_hub(hub)) {
Simon Glassa5762fe2015-03-25 12:22:53 -06001378 root_portnr = udev->portnr;
1379 } else {
Bin Meng46c1d492017-07-19 21:51:11 +08001380 while (!usb_hub_is_root_hub(hub->parent))
Simon Glassa5762fe2015-03-25 12:22:53 -06001381 hub = hub->parent;
Simon Glassbcbe3d12015-09-28 23:32:01 -06001382 uhop = dev_get_parent_priv(hub);
Simon Glassa5762fe2015-03-25 12:22:53 -06001383 root_portnr = uhop->portnr;
1384 }
1385 }
1386/*
1387 struct usb_device *hop = udev;
1388
1389 if (hop->parent)
1390 while (hop->parent->parent)
1391 hop = hop->parent;
1392*/
1393 return _xhci_submit_control_msg(udev, pipe, buffer, length, setup,
1394 root_portnr);
1395}
1396
1397static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev,
1398 unsigned long pipe, void *buffer, int length)
1399{
1400 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1401 return _xhci_submit_bulk_msg(udev, pipe, buffer, length);
1402}
1403
1404static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev,
1405 unsigned long pipe, void *buffer, int length,
Michal Suchanek34371212019-08-18 10:55:27 +02001406 int interval, bool nonblock)
Simon Glassa5762fe2015-03-25 12:22:53 -06001407{
1408 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
Michal Suchanek34371212019-08-18 10:55:27 +02001409 return _xhci_submit_int_msg(udev, pipe, buffer, length, interval,
1410 nonblock);
Simon Glassa5762fe2015-03-25 12:22:53 -06001411}
1412
1413static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev)
1414{
1415 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1416 return _xhci_alloc_device(udev);
1417}
1418
Bin Mengd228ca32017-07-19 21:51:19 +08001419static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev)
1420{
1421 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1422 struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev);
1423 struct xhci_virt_device *virt_dev;
1424 struct xhci_input_control_ctx *ctrl_ctx;
1425 struct xhci_container_ctx *out_ctx;
1426 struct xhci_container_ctx *in_ctx;
1427 struct xhci_slot_ctx *slot_ctx;
1428 int slot_id = udev->slot_id;
1429 unsigned think_time;
1430
1431 debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev);
1432
1433 /* Ignore root hubs */
1434 if (usb_hub_is_root_hub(udev->dev))
1435 return 0;
1436
1437 virt_dev = ctrl->devs[slot_id];
1438 BUG_ON(!virt_dev);
1439
1440 out_ctx = virt_dev->out_ctx;
1441 in_ctx = virt_dev->in_ctx;
1442
1443 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1444 /* Initialize the input context control */
Bin Meng793c8192018-05-23 23:40:47 -07001445 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Bin Mengd228ca32017-07-19 21:51:19 +08001446 ctrl_ctx->drop_flags = 0;
1447
1448 xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size);
1449
1450 /* slot context */
1451 xhci_slot_copy(ctrl, in_ctx, out_ctx);
1452 slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx);
1453
1454 /* Update hub related fields */
1455 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Bin Mengeaaefb02018-05-23 23:40:49 -07001456 /*
1457 * refer to section 6.2.2: MTT should be 0 for full speed hub,
1458 * but it may be already set to 1 when setup an xHCI virtual
1459 * device, so clear it anyway.
1460 */
1461 if (hub->tt.multi)
Bin Mengd228ca32017-07-19 21:51:19 +08001462 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Bin Mengeaaefb02018-05-23 23:40:49 -07001463 else if (udev->speed == USB_SPEED_FULL)
1464 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
Bin Mengd228ca32017-07-19 21:51:19 +08001465 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild));
1466 /*
1467 * Set TT think time - convert from ns to FS bit times.
1468 * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns
1469 *
1470 * 0 = 8 FS bit times, 1 = 16 FS bit times,
1471 * 2 = 24 FS bit times, 3 = 32 FS bit times.
1472 *
1473 * This field shall be 0 if the device is not a high-spped hub.
1474 */
1475 think_time = hub->tt.think_time;
1476 if (think_time != 0)
1477 think_time = (think_time / 666) - 1;
1478 if (udev->speed == USB_SPEED_HIGH)
1479 slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time));
Bin Mengae751b02018-05-23 23:40:48 -07001480 slot_ctx->dev_state = 0;
Bin Mengd228ca32017-07-19 21:51:19 +08001481
1482 return xhci_configure_endpoints(udev, false);
1483}
1484
Bin Meng022ceac2017-09-07 06:13:18 -07001485static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size)
1486{
1487 /*
1488 * xHCD allocates one segment which includes 64 TRBs for each endpoint
1489 * and the last TRB in this segment is configured as a link TRB to form
1490 * a TRB ring. Each TRB can transfer up to 64K bytes, however data
1491 * buffers referenced by transfer TRBs shall not span 64KB boundaries.
1492 * Hence the maximum number of TRBs we can use in one transfer is 62.
1493 */
1494 *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE;
1495
1496 return 0;
1497}
1498
Simon Glassa5762fe2015-03-25 12:22:53 -06001499int xhci_register(struct udevice *dev, struct xhci_hccr *hccr,
1500 struct xhci_hcor *hcor)
1501{
1502 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1503 struct usb_bus_priv *priv = dev_get_uclass_priv(dev);
1504 int ret;
1505
1506 debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name,
1507 ctrl, hccr, hcor);
1508
1509 ctrl->dev = dev;
1510
1511 /*
1512 * XHCI needs to issue a Address device command to setup
1513 * proper device context structures, before it can interact
1514 * with the device. So a get_descriptor will fail before any
1515 * of that is done for XHCI unlike EHCI.
1516 */
1517 priv->desc_before_addr = false;
1518
1519 ret = xhci_reset(hcor);
1520 if (ret)
1521 goto err;
1522
1523 ctrl->hccr = hccr;
1524 ctrl->hcor = hcor;
1525 ret = xhci_lowlevel_init(ctrl);
1526 if (ret)
1527 goto err;
1528
1529 return 0;
1530err:
1531 free(ctrl);
1532 debug("%s: failed, ret=%d\n", __func__, ret);
1533 return ret;
1534}
1535
1536int xhci_deregister(struct udevice *dev)
1537{
1538 struct xhci_ctrl *ctrl = dev_get_priv(dev);
1539
1540 xhci_lowlevel_stop(ctrl);
1541 xhci_cleanup(ctrl);
1542
1543 return 0;
1544}
1545
1546struct dm_usb_ops xhci_usb_ops = {
1547 .control = xhci_submit_control_msg,
1548 .bulk = xhci_submit_bulk_msg,
1549 .interrupt = xhci_submit_int_msg,
1550 .alloc_device = xhci_alloc_device,
Bin Mengd228ca32017-07-19 21:51:19 +08001551 .update_hub_device = xhci_update_hub_device,
Bin Meng022ceac2017-09-07 06:13:18 -07001552 .get_max_xfer_size = xhci_get_max_xfer_size,
Simon Glassa5762fe2015-03-25 12:22:53 -06001553};
1554
1555#endif