Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 2 | /* |
| 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 Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 14 | */ |
| 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 Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 23 | #include <cpu_func.h> |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 24 | #include <dm.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 25 | #include <dm/device_compat.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 26 | #include <log.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 27 | #include <malloc.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 28 | #include <usb.h> |
| 29 | #include <usb/xhci.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 30 | #include <watchdog.h> |
Sean Anderson | 429ce52 | 2020-10-04 21:39:53 -0400 | [diff] [blame] | 31 | #include <asm/byteorder.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 32 | #include <asm/cache.h> |
| 33 | #include <asm/unaligned.h> |
Simon Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 34 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 35 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 36 | #include <linux/delay.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 37 | #include <linux/errno.h> |
developer | 14bb350 | 2020-09-08 19:00:03 +0200 | [diff] [blame] | 38 | #include <linux/iopoll.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 39 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 40 | static struct descriptor { |
| 41 | struct usb_hub_descriptor hub; |
| 42 | struct usb_device_descriptor device; |
| 43 | struct usb_config_descriptor config; |
| 44 | struct usb_interface_descriptor interface; |
| 45 | struct usb_endpoint_descriptor endpoint; |
| 46 | struct usb_ss_ep_comp_descriptor ep_companion; |
| 47 | } __attribute__ ((packed)) descriptor = { |
| 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 Meng | 0d66b3a | 2017-07-19 21:50:00 +0800 | [diff] [blame] | 55 | { /* Device removable */ |
| 56 | } /* at most 7 ports! XXX */ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 57 | }, |
| 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 Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 114 | struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev) |
| 115 | { |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 116 | 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 Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 126 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 134 | * Return: 0 if handshake is success else < 0 on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 135 | */ |
developer | 14bb350 | 2020-09-08 19:00:03 +0200 | [diff] [blame] | 136 | static int |
| 137 | handshake(uint32_t volatile *ptr, uint32_t mask, uint32_t done, int usec) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 138 | { |
| 139 | uint32_t result; |
developer | 14bb350 | 2020-09-08 19:00:03 +0200 | [diff] [blame] | 140 | int ret; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 141 | |
developer | 14bb350 | 2020-09-08 19:00:03 +0200 | [diff] [blame] | 142 | 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 Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 147 | |
developer | 14bb350 | 2020-09-08 19:00:03 +0200 | [diff] [blame] | 148 | return ret; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 149 | } |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 155 | * Return: status of the Handshake |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 156 | */ |
| 157 | static 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 183 | * Return: -EBUSY if XHCI Controller is not halted else status of handshake |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 184 | */ |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 185 | static int xhci_reset(struct xhci_hcor *hcor) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 186 | { |
| 187 | u32 cmd; |
| 188 | u32 state; |
| 189 | int ret; |
| 190 | |
| 191 | /* Halting the Host first */ |
Sergey Temerkhanov | 65bb454 | 2015-08-17 15:38:07 +0300 | [diff] [blame] | 192 | debug("// Halt the HC: %p\n", hcor); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 193 | 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 235 | * Return: index of the Endpoint |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 236 | */ |
| 237 | static 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 Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 250 | /* |
| 251 | * Convert bInterval expressed in microframes (in 1-255 range) to exponent of |
| 252 | * microframes, rounded down to nearest power of 2. |
| 253 | */ |
| 254 | static 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 | |
| 270 | static 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 | |
| 279 | static 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 | */ |
| 289 | static 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 | */ |
| 321 | static 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 | */ |
| 377 | static u32 xhci_get_endpoint_mult(struct usb_device *udev, |
| 378 | struct usb_endpoint_descriptor *endpt_desc, |
| 379 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 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 Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 388 | static u32 xhci_get_endpoint_max_burst(struct usb_device *udev, |
| 389 | struct usb_endpoint_descriptor *endpt_desc, |
| 390 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 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 Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 404 | /* |
| 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 | */ |
| 409 | static u32 xhci_get_max_esit_payload(struct usb_device *udev, |
| 410 | struct usb_endpoint_descriptor *endpt_desc, |
| 411 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 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 Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 432 | /** |
| 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 Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 438 | * Return: 0 on success, -1 on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 439 | */ |
| 440 | static 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 Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 444 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 445 | union xhci_trb *event; |
| 446 | |
| 447 | virt_dev = ctrl->devs[udev->slot_id]; |
| 448 | in_ctx = virt_dev->in_ctx; |
| 449 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 450 | xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size); |
Mark Kettenis | fac410c | 2023-01-21 20:27:55 +0100 | [diff] [blame] | 451 | xhci_queue_command(ctrl, in_ctx->dma, udev->slot_id, 0, |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 452 | ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP); |
| 453 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
Hector Martin | c5d7dac | 2023-10-29 15:37:38 +0900 | [diff] [blame] | 454 | if (!event) |
| 455 | return -ETIMEDOUT; |
| 456 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 457 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) |
| 458 | != udev->slot_id); |
| 459 | |
| 460 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { |
| 461 | case COMP_SUCCESS: |
| 462 | debug("Successful %s command\n", |
| 463 | ctx_change ? "Evaluate Context" : "Configure Endpoint"); |
| 464 | break; |
| 465 | default: |
| 466 | printf("ERROR: %s command returned completion code %d.\n", |
| 467 | ctx_change ? "Evaluate Context" : "Configure Endpoint", |
| 468 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); |
| 469 | return -EINVAL; |
| 470 | } |
| 471 | |
| 472 | xhci_acknowledge_event(ctrl); |
| 473 | |
| 474 | return 0; |
| 475 | } |
| 476 | |
| 477 | /** |
| 478 | * Configure the endpoint, programming the device contexts. |
| 479 | * |
| 480 | * @param udev pointer to the USB device structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 481 | * Return: returns the status of the xhci_configure_endpoints |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 482 | */ |
| 483 | static int xhci_set_configuration(struct usb_device *udev) |
| 484 | { |
| 485 | struct xhci_container_ctx *in_ctx; |
| 486 | struct xhci_container_ctx *out_ctx; |
| 487 | struct xhci_input_control_ctx *ctrl_ctx; |
| 488 | struct xhci_slot_ctx *slot_ctx; |
| 489 | struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM]; |
| 490 | int cur_ep; |
| 491 | int max_ep_flag = 0; |
| 492 | int ep_index; |
| 493 | unsigned int dir; |
| 494 | unsigned int ep_type; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 495 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 496 | int num_of_ep; |
| 497 | int ep_flag = 0; |
| 498 | u64 trb_64 = 0; |
| 499 | int slot_id = udev->slot_id; |
| 500 | struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; |
| 501 | struct usb_interface *ifdesc; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 502 | u32 max_esit_payload; |
| 503 | unsigned int interval; |
| 504 | unsigned int mult; |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 505 | unsigned int max_burst; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 506 | unsigned int avg_trb_len; |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 507 | unsigned int err_count = 0; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 508 | |
| 509 | out_ctx = virt_dev->out_ctx; |
| 510 | in_ctx = virt_dev->in_ctx; |
| 511 | |
| 512 | num_of_ep = udev->config.if_desc[0].no_of_ep; |
| 513 | ifdesc = &udev->config.if_desc[0]; |
| 514 | |
| 515 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
Bin Meng | ec0501b | 2017-07-19 21:49:56 +0800 | [diff] [blame] | 516 | /* Initialize the input context control */ |
| 517 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 518 | ctrl_ctx->drop_flags = 0; |
| 519 | |
| 520 | /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */ |
| 521 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { |
| 522 | ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]); |
| 523 | ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1)); |
| 524 | if (max_ep_flag < ep_flag) |
| 525 | max_ep_flag = ep_flag; |
| 526 | } |
| 527 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 528 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 529 | |
| 530 | /* slot context */ |
| 531 | xhci_slot_copy(ctrl, in_ctx, out_ctx); |
| 532 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); |
Bin Meng | a1ae60e | 2018-05-23 23:40:50 -0700 | [diff] [blame] | 533 | slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 534 | slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0); |
| 535 | |
| 536 | xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0); |
| 537 | |
| 538 | /* filling up ep contexts */ |
| 539 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { |
| 540 | struct usb_endpoint_descriptor *endpt_desc = NULL; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 541 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 542 | |
| 543 | endpt_desc = &ifdesc->ep_desc[cur_ep]; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 544 | ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep]; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 545 | trb_64 = 0; |
| 546 | |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 547 | /* |
| 548 | * Get values to fill the endpoint context, mostly from ep |
| 549 | * descriptor. The average TRB buffer lengt for bulk endpoints |
| 550 | * is unclear as we have no clue on scatter gather list entry |
| 551 | * size. For Isoc and Int, set it to max available. |
| 552 | * See xHCI 1.1 spec 4.14.1.1 for details. |
| 553 | */ |
| 554 | max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc, |
| 555 | ss_ep_comp_desc); |
| 556 | interval = xhci_get_endpoint_interval(udev, endpt_desc); |
| 557 | mult = xhci_get_endpoint_mult(udev, endpt_desc, |
| 558 | ss_ep_comp_desc); |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 559 | max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc, |
| 560 | ss_ep_comp_desc); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 561 | avg_trb_len = max_esit_payload; |
| 562 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 563 | ep_index = xhci_get_ep_index(endpt_desc); |
| 564 | ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); |
| 565 | |
| 566 | /* Allocate the ep rings */ |
Nicolas Saenz Julienne | 4033aa3 | 2021-01-12 13:55:28 +0100 | [diff] [blame] | 567 | virt_dev->eps[ep_index].ring = xhci_ring_alloc(ctrl, 1, true); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 568 | if (!virt_dev->eps[ep_index].ring) |
| 569 | return -ENOMEM; |
| 570 | |
| 571 | /*NOTE: ep_desc[0] actually represents EP1 and so on */ |
| 572 | dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7); |
| 573 | ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2)); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 574 | |
| 575 | ep_ctx[ep_index]->ep_info = |
| 576 | cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) | |
| 577 | EP_INTERVAL(interval) | EP_MULT(mult)); |
| 578 | |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 579 | ep_ctx[ep_index]->ep_info2 = cpu_to_le32(EP_TYPE(ep_type)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 580 | ep_ctx[ep_index]->ep_info2 |= |
| 581 | cpu_to_le32(MAX_PACKET |
| 582 | (get_unaligned(&endpt_desc->wMaxPacketSize))); |
| 583 | |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 584 | /* Allow 3 retries for everything but isoc, set CErr = 3 */ |
| 585 | if (!usb_endpoint_xfer_isoc(endpt_desc)) |
| 586 | err_count = 3; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 587 | ep_ctx[ep_index]->ep_info2 |= |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 588 | cpu_to_le32(MAX_BURST(max_burst) | |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 589 | ERROR_COUNT(err_count)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 590 | |
Mark Kettenis | fac410c | 2023-01-21 20:27:55 +0100 | [diff] [blame] | 591 | trb_64 = xhci_trb_virt_to_dma(virt_dev->eps[ep_index].ring->enq_seg, |
| 592 | virt_dev->eps[ep_index].ring->enqueue); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 593 | ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 | |
| 594 | virt_dev->eps[ep_index].ring->cycle_state); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 595 | |
Bin Meng | c03fb20 | 2017-09-18 06:40:50 -0700 | [diff] [blame] | 596 | /* |
| 597 | * xHCI spec 6.2.3: |
| 598 | * 'Average TRB Length' should be 8 for control endpoints. |
| 599 | */ |
| 600 | if (usb_endpoint_xfer_control(endpt_desc)) |
| 601 | avg_trb_len = 8; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 602 | ep_ctx[ep_index]->tx_info = |
| 603 | cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) | |
| 604 | EP_AVG_TRB_LENGTH(avg_trb_len)); |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 605 | |
| 606 | /* |
| 607 | * The MediaTek xHCI defines some extra SW parameters which |
| 608 | * are put into reserved DWs in Slot and Endpoint Contexts |
| 609 | * for synchronous endpoints. |
| 610 | */ |
developer | 8039053 | 2020-09-08 18:59:57 +0200 | [diff] [blame] | 611 | if (ctrl->quirks & XHCI_MTK_HOST) { |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 612 | ep_ctx[ep_index]->reserved[0] = |
| 613 | cpu_to_le32(EP_BPKTS(1) | EP_BBM(1)); |
| 614 | } |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 615 | } |
| 616 | |
| 617 | return xhci_configure_endpoints(udev, false); |
| 618 | } |
| 619 | |
| 620 | /** |
| 621 | * Issue an Address Device command (which will issue a SetAddress request to |
| 622 | * the device). |
| 623 | * |
| 624 | * @param udev pointer to the Device Data Structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 625 | * Return: 0 if successful else error code on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 626 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 627 | static int xhci_address_device(struct usb_device *udev, int root_portnr) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 628 | { |
| 629 | int ret = 0; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 630 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 631 | struct xhci_slot_ctx *slot_ctx; |
| 632 | struct xhci_input_control_ctx *ctrl_ctx; |
| 633 | struct xhci_virt_device *virt_dev; |
| 634 | int slot_id = udev->slot_id; |
| 635 | union xhci_trb *event; |
| 636 | |
| 637 | virt_dev = ctrl->devs[slot_id]; |
| 638 | |
| 639 | /* |
| 640 | * This is the first Set Address since device plug-in |
| 641 | * so setting up the slot context. |
| 642 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 643 | debug("Setting up addressable devices %p\n", ctrl->dcbaa); |
Bin Meng | 1459ce6 | 2017-07-19 21:51:14 +0800 | [diff] [blame] | 644 | xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 645 | |
| 646 | ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); |
| 647 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); |
| 648 | ctrl_ctx->drop_flags = 0; |
| 649 | |
Mark Kettenis | fac410c | 2023-01-21 20:27:55 +0100 | [diff] [blame] | 650 | xhci_queue_command(ctrl, virt_dev->in_ctx->dma, |
| 651 | slot_id, 0, TRB_ADDR_DEV); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 652 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
Hector Martin | c5d7dac | 2023-10-29 15:37:38 +0900 | [diff] [blame] | 653 | if (!event) |
| 654 | return -ETIMEDOUT; |
| 655 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 656 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id); |
| 657 | |
| 658 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { |
| 659 | case COMP_CTX_STATE: |
| 660 | case COMP_EBADSLT: |
| 661 | printf("Setup ERROR: address device command for slot %d.\n", |
| 662 | slot_id); |
| 663 | ret = -EINVAL; |
| 664 | break; |
| 665 | case COMP_TX_ERR: |
| 666 | puts("Device not responding to set address.\n"); |
| 667 | ret = -EPROTO; |
| 668 | break; |
| 669 | case COMP_DEV_ERR: |
| 670 | puts("ERROR: Incompatible device" |
| 671 | "for address device command.\n"); |
| 672 | ret = -ENODEV; |
| 673 | break; |
| 674 | case COMP_SUCCESS: |
| 675 | debug("Successful Address Device command\n"); |
| 676 | udev->status = 0; |
| 677 | break; |
| 678 | default: |
| 679 | printf("ERROR: unexpected command completion code 0x%x.\n", |
| 680 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); |
| 681 | ret = -EINVAL; |
| 682 | break; |
| 683 | } |
| 684 | |
| 685 | xhci_acknowledge_event(ctrl); |
| 686 | |
| 687 | if (ret < 0) |
| 688 | /* |
| 689 | * TODO: Unsuccessful Address Device command shall leave the |
| 690 | * slot in default state. So, issue Disable Slot command now. |
| 691 | */ |
| 692 | return ret; |
| 693 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 694 | xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, |
| 695 | virt_dev->out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 696 | slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx); |
| 697 | |
| 698 | debug("xHC internal address is: %d\n", |
| 699 | le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); |
| 700 | |
| 701 | return 0; |
| 702 | } |
| 703 | |
| 704 | /** |
| 705 | * Issue Enable slot command to the controller to allocate |
| 706 | * device slot and assign the slot id. It fails if the xHC |
| 707 | * ran out of device slots, the Enable Slot command timed out, |
| 708 | * or allocating memory failed. |
| 709 | * |
| 710 | * @param udev pointer to the Device Data Structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 711 | * Return: Returns 0 on succes else return error code on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 712 | */ |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 713 | static int _xhci_alloc_device(struct usb_device *udev) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 714 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 715 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 716 | union xhci_trb *event; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 717 | int ret; |
| 718 | |
| 719 | /* |
| 720 | * Root hub will be first device to be initailized. |
| 721 | * If this device is root-hub, don't do any xHC related |
| 722 | * stuff. |
| 723 | */ |
| 724 | if (ctrl->rootdev == 0) { |
| 725 | udev->speed = USB_SPEED_SUPER; |
| 726 | return 0; |
| 727 | } |
| 728 | |
Mark Kettenis | fac410c | 2023-01-21 20:27:55 +0100 | [diff] [blame] | 729 | xhci_queue_command(ctrl, 0, 0, 0, TRB_ENABLE_SLOT); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 730 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
Hector Martin | c5d7dac | 2023-10-29 15:37:38 +0900 | [diff] [blame] | 731 | if (!event) |
| 732 | return -ETIMEDOUT; |
| 733 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 734 | BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)) |
| 735 | != COMP_SUCCESS); |
| 736 | |
| 737 | udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)); |
| 738 | |
| 739 | xhci_acknowledge_event(ctrl); |
| 740 | |
Simon Glass | 88a3784 | 2015-03-25 12:22:50 -0600 | [diff] [blame] | 741 | ret = xhci_alloc_virt_device(ctrl, udev->slot_id); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 742 | if (ret < 0) { |
| 743 | /* |
| 744 | * TODO: Unsuccessful Address Device command shall leave |
| 745 | * the slot in default. So, issue Disable Slot command now. |
| 746 | */ |
| 747 | puts("Could not allocate xHCI USB device data structures\n"); |
| 748 | return ret; |
| 749 | } |
| 750 | |
| 751 | return 0; |
| 752 | } |
| 753 | |
| 754 | /* |
| 755 | * Full speed devices may have a max packet size greater than 8 bytes, but the |
| 756 | * USB core doesn't know that until it reads the first 8 bytes of the |
| 757 | * descriptor. If the usb_device's max packet size changes after that point, |
| 758 | * we need to issue an evaluate context command and wait on it. |
| 759 | * |
| 760 | * @param udev pointer to the Device Data Structure |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 761 | * Return: returns the status of the xhci_configure_endpoints |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 762 | */ |
| 763 | int xhci_check_maxpacket(struct usb_device *udev) |
| 764 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 765 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 766 | unsigned int slot_id = udev->slot_id; |
| 767 | int ep_index = 0; /* control endpoint */ |
| 768 | struct xhci_container_ctx *in_ctx; |
| 769 | struct xhci_container_ctx *out_ctx; |
| 770 | struct xhci_input_control_ctx *ctrl_ctx; |
| 771 | struct xhci_ep_ctx *ep_ctx; |
| 772 | int max_packet_size; |
| 773 | int hw_max_packet_size; |
| 774 | int ret = 0; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 775 | |
| 776 | out_ctx = ctrl->devs[slot_id]->out_ctx; |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 777 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 778 | |
| 779 | ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); |
| 780 | hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2)); |
Bin Meng | 7c92b77 | 2017-09-18 06:40:44 -0700 | [diff] [blame] | 781 | max_packet_size = udev->epmaxpacketin[0]; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 782 | if (hw_max_packet_size != max_packet_size) { |
| 783 | debug("Max Packet Size for ep 0 changed.\n"); |
| 784 | debug("Max packet size in usb_device = %d\n", max_packet_size); |
| 785 | debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size); |
| 786 | debug("Issuing evaluate context command.\n"); |
| 787 | |
| 788 | /* Set up the modified control endpoint 0 */ |
| 789 | xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx, |
| 790 | ctrl->devs[slot_id]->out_ctx, ep_index); |
| 791 | in_ctx = ctrl->devs[slot_id]->in_ctx; |
| 792 | ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); |
developer | 9963422 | 2020-09-08 19:00:02 +0200 | [diff] [blame] | 793 | ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET(MAX_PACKET_MASK)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 794 | ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); |
| 795 | |
| 796 | /* |
| 797 | * Set up the input context flags for the command |
| 798 | * FIXME: This won't work if a non-default control endpoint |
| 799 | * changes max packet sizes. |
| 800 | */ |
| 801 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
| 802 | ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); |
| 803 | ctrl_ctx->drop_flags = 0; |
| 804 | |
| 805 | ret = xhci_configure_endpoints(udev, true); |
| 806 | } |
| 807 | return ret; |
| 808 | } |
| 809 | |
| 810 | /** |
| 811 | * Clears the Change bits of the Port Status Register |
| 812 | * |
| 813 | * @param wValue request value |
| 814 | * @param wIndex request index |
| 815 | * @param addr address of posrt status register |
| 816 | * @param port_status state of port status register |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 817 | * Return: none |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 818 | */ |
| 819 | static void xhci_clear_port_change_bit(u16 wValue, |
| 820 | u16 wIndex, volatile uint32_t *addr, u32 port_status) |
| 821 | { |
| 822 | char *port_change_bit; |
| 823 | u32 status; |
| 824 | |
| 825 | switch (wValue) { |
| 826 | case USB_PORT_FEAT_C_RESET: |
| 827 | status = PORT_RC; |
| 828 | port_change_bit = "reset"; |
| 829 | break; |
| 830 | case USB_PORT_FEAT_C_CONNECTION: |
| 831 | status = PORT_CSC; |
| 832 | port_change_bit = "connect"; |
| 833 | break; |
| 834 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 835 | status = PORT_OCC; |
| 836 | port_change_bit = "over-current"; |
| 837 | break; |
| 838 | case USB_PORT_FEAT_C_ENABLE: |
| 839 | status = PORT_PEC; |
| 840 | port_change_bit = "enable/disable"; |
| 841 | break; |
| 842 | case USB_PORT_FEAT_C_SUSPEND: |
| 843 | status = PORT_PLC; |
| 844 | port_change_bit = "suspend/resume"; |
| 845 | break; |
| 846 | default: |
| 847 | /* Should never happen */ |
| 848 | return; |
| 849 | } |
| 850 | |
| 851 | /* Change bits are all write 1 to clear */ |
| 852 | xhci_writel(addr, port_status | status); |
| 853 | |
| 854 | port_status = xhci_readl(addr); |
| 855 | debug("clear port %s change, actual port %d status = 0x%x\n", |
| 856 | port_change_bit, wIndex, port_status); |
| 857 | } |
| 858 | |
| 859 | /** |
| 860 | * Save Read Only (RO) bits and save read/write bits where |
| 861 | * writing a 0 clears the bit and writing a 1 sets the bit (RWS). |
| 862 | * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. |
| 863 | * |
| 864 | * @param state state of the Port Status and Control Regsiter |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 865 | * Return: a value that would result in the port being in the |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 866 | * same state, if the value was written to the port |
| 867 | * status control register. |
| 868 | */ |
| 869 | static u32 xhci_port_state_to_neutral(u32 state) |
| 870 | { |
| 871 | /* Save read-only status and port state */ |
| 872 | return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS); |
| 873 | } |
| 874 | |
| 875 | /** |
| 876 | * Submits the Requests to the XHCI Host Controller |
| 877 | * |
| 878 | * @param udev pointer to the USB device structure |
| 879 | * @param pipe contains the DIR_IN or OUT , devnum |
| 880 | * @param buffer buffer to be read/written based on the request |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 881 | * Return: returns 0 if successful else -1 on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 882 | */ |
| 883 | static int xhci_submit_root(struct usb_device *udev, unsigned long pipe, |
| 884 | void *buffer, struct devrequest *req) |
| 885 | { |
| 886 | uint8_t tmpbuf[4]; |
| 887 | u16 typeReq; |
| 888 | void *srcptr = NULL; |
| 889 | int len, srclen; |
| 890 | uint32_t reg; |
| 891 | volatile uint32_t *status_reg; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 892 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 893 | struct xhci_hccr *hccr = ctrl->hccr; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 894 | struct xhci_hcor *hcor = ctrl->hcor; |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 895 | int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 896 | |
Jeroen Hofstee | b351e46 | 2014-06-12 00:31:27 +0200 | [diff] [blame] | 897 | if ((req->requesttype & USB_RT_PORT) && |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 898 | le16_to_cpu(req->index) > max_ports) { |
| 899 | printf("The request port(%d) exceeds maximum port number\n", |
| 900 | le16_to_cpu(req->index) - 1); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 901 | return -EINVAL; |
| 902 | } |
| 903 | |
| 904 | status_reg = (volatile uint32_t *) |
| 905 | (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc); |
| 906 | srclen = 0; |
| 907 | |
| 908 | typeReq = req->request | req->requesttype << 8; |
| 909 | |
| 910 | switch (typeReq) { |
| 911 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 912 | switch (le16_to_cpu(req->value) >> 8) { |
| 913 | case USB_DT_DEVICE: |
| 914 | debug("USB_DT_DEVICE request\n"); |
| 915 | srcptr = &descriptor.device; |
| 916 | srclen = 0x12; |
| 917 | break; |
| 918 | case USB_DT_CONFIG: |
| 919 | debug("USB_DT_CONFIG config\n"); |
| 920 | srcptr = &descriptor.config; |
| 921 | srclen = 0x19; |
| 922 | break; |
| 923 | case USB_DT_STRING: |
| 924 | debug("USB_DT_STRING config\n"); |
| 925 | switch (le16_to_cpu(req->value) & 0xff) { |
| 926 | case 0: /* Language */ |
| 927 | srcptr = "\4\3\11\4"; |
| 928 | srclen = 4; |
| 929 | break; |
| 930 | case 1: /* Vendor String */ |
Simon Glass | b113f6e | 2015-03-25 12:22:54 -0600 | [diff] [blame] | 931 | srcptr = "\16\3U\0-\0B\0o\0o\0t\0"; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 932 | srclen = 14; |
| 933 | break; |
| 934 | case 2: /* Product Name */ |
| 935 | srcptr = "\52\3X\0H\0C\0I\0 " |
| 936 | "\0H\0o\0s\0t\0 " |
| 937 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 938 | srclen = 42; |
| 939 | break; |
| 940 | default: |
| 941 | printf("unknown value DT_STRING %x\n", |
| 942 | le16_to_cpu(req->value)); |
| 943 | goto unknown; |
| 944 | } |
| 945 | break; |
| 946 | default: |
| 947 | printf("unknown value %x\n", le16_to_cpu(req->value)); |
| 948 | goto unknown; |
| 949 | } |
| 950 | break; |
| 951 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 952 | switch (le16_to_cpu(req->value) >> 8) { |
| 953 | case USB_DT_HUB: |
Bin Meng | e8930c4 | 2017-07-19 21:49:58 +0800 | [diff] [blame] | 954 | case USB_DT_SS_HUB: |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 955 | debug("USB_DT_HUB config\n"); |
Mark Kettenis | 4eb77d3 | 2023-01-21 20:28:00 +0100 | [diff] [blame] | 956 | srcptr = &ctrl->hub_desc; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 957 | srclen = 0x8; |
| 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_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
| 965 | debug("USB_REQ_SET_ADDRESS\n"); |
| 966 | ctrl->rootdev = le16_to_cpu(req->value); |
| 967 | break; |
| 968 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 969 | /* Do nothing */ |
| 970 | break; |
| 971 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 972 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 973 | tmpbuf[1] = 0; |
| 974 | srcptr = tmpbuf; |
| 975 | srclen = 2; |
| 976 | break; |
| 977 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
| 978 | memset(tmpbuf, 0, 4); |
| 979 | reg = xhci_readl(status_reg); |
| 980 | if (reg & PORT_CONNECT) { |
| 981 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 982 | switch (reg & DEV_SPEED_MASK) { |
| 983 | case XDEV_FS: |
| 984 | debug("SPEED = FULLSPEED\n"); |
| 985 | break; |
| 986 | case XDEV_LS: |
| 987 | debug("SPEED = LOWSPEED\n"); |
| 988 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 989 | break; |
| 990 | case XDEV_HS: |
| 991 | debug("SPEED = HIGHSPEED\n"); |
| 992 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 993 | break; |
| 994 | case XDEV_SS: |
| 995 | debug("SPEED = SUPERSPEED\n"); |
| 996 | tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8; |
| 997 | break; |
| 998 | } |
| 999 | } |
| 1000 | if (reg & PORT_PE) |
| 1001 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 1002 | if ((reg & PORT_PLS_MASK) == XDEV_U3) |
| 1003 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 1004 | if (reg & PORT_OC) |
| 1005 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
| 1006 | if (reg & PORT_RESET) |
| 1007 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
| 1008 | if (reg & PORT_POWER) |
| 1009 | /* |
| 1010 | * XXX: This Port power bit (for USB 3.0 hub) |
| 1011 | * we are faking in USB 2.0 hub port status; |
| 1012 | * since there's a change in bit positions in |
| 1013 | * two: |
| 1014 | * USB 2.0 port status PP is at position[8] |
| 1015 | * USB 3.0 port status PP is at position[9] |
| 1016 | * So, we are still keeping it at position [8] |
| 1017 | */ |
| 1018 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
| 1019 | if (reg & PORT_CSC) |
| 1020 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 1021 | if (reg & PORT_PEC) |
| 1022 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 1023 | if (reg & PORT_OCC) |
| 1024 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
| 1025 | if (reg & PORT_RC) |
| 1026 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
| 1027 | |
| 1028 | srcptr = tmpbuf; |
| 1029 | srclen = 4; |
| 1030 | break; |
| 1031 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 1032 | reg = xhci_readl(status_reg); |
| 1033 | reg = xhci_port_state_to_neutral(reg); |
| 1034 | switch (le16_to_cpu(req->value)) { |
| 1035 | case USB_PORT_FEAT_ENABLE: |
| 1036 | reg |= PORT_PE; |
| 1037 | xhci_writel(status_reg, reg); |
| 1038 | break; |
| 1039 | case USB_PORT_FEAT_POWER: |
| 1040 | reg |= PORT_POWER; |
| 1041 | xhci_writel(status_reg, reg); |
| 1042 | break; |
| 1043 | case USB_PORT_FEAT_RESET: |
| 1044 | reg |= PORT_RESET; |
| 1045 | xhci_writel(status_reg, reg); |
| 1046 | break; |
| 1047 | default: |
| 1048 | printf("unknown feature %x\n", le16_to_cpu(req->value)); |
| 1049 | goto unknown; |
| 1050 | } |
| 1051 | break; |
| 1052 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 1053 | reg = xhci_readl(status_reg); |
| 1054 | reg = xhci_port_state_to_neutral(reg); |
| 1055 | switch (le16_to_cpu(req->value)) { |
| 1056 | case USB_PORT_FEAT_ENABLE: |
| 1057 | reg &= ~PORT_PE; |
| 1058 | break; |
| 1059 | case USB_PORT_FEAT_POWER: |
| 1060 | reg &= ~PORT_POWER; |
| 1061 | break; |
| 1062 | case USB_PORT_FEAT_C_RESET: |
| 1063 | case USB_PORT_FEAT_C_CONNECTION: |
| 1064 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 1065 | case USB_PORT_FEAT_C_ENABLE: |
| 1066 | xhci_clear_port_change_bit((le16_to_cpu(req->value)), |
| 1067 | le16_to_cpu(req->index), |
| 1068 | status_reg, reg); |
| 1069 | break; |
| 1070 | default: |
| 1071 | printf("unknown feature %x\n", le16_to_cpu(req->value)); |
| 1072 | goto unknown; |
| 1073 | } |
| 1074 | xhci_writel(status_reg, reg); |
| 1075 | break; |
| 1076 | default: |
| 1077 | puts("Unknown request\n"); |
| 1078 | goto unknown; |
| 1079 | } |
| 1080 | |
| 1081 | debug("scrlen = %d\n req->length = %d\n", |
| 1082 | srclen, le16_to_cpu(req->length)); |
| 1083 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 1084 | len = min(srclen, (int)le16_to_cpu(req->length)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1085 | |
| 1086 | if (srcptr != NULL && len > 0) |
| 1087 | memcpy(buffer, srcptr, len); |
| 1088 | else |
| 1089 | debug("Len is 0\n"); |
| 1090 | |
| 1091 | udev->act_len = len; |
| 1092 | udev->status = 0; |
| 1093 | |
| 1094 | return 0; |
| 1095 | |
| 1096 | unknown: |
| 1097 | udev->act_len = 0; |
| 1098 | udev->status = USB_ST_STALLED; |
| 1099 | |
| 1100 | return -ENODEV; |
| 1101 | } |
| 1102 | |
| 1103 | /** |
| 1104 | * Submits the INT request to XHCI Host cotroller |
| 1105 | * |
| 1106 | * @param udev pointer to the USB device |
| 1107 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1108 | * @param buffer buffer to be read/written based on the request |
| 1109 | * @param length length of the buffer |
| 1110 | * @param interval interval of the interrupt |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1111 | * Return: 0 |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1112 | */ |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1113 | static int _xhci_submit_int_msg(struct usb_device *udev, unsigned long pipe, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1114 | void *buffer, int length, int interval, |
| 1115 | bool nonblock) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1116 | { |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1117 | if (usb_pipetype(pipe) != PIPE_INTERRUPT) { |
| 1118 | printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe)); |
| 1119 | return -EINVAL; |
| 1120 | } |
| 1121 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1122 | /* |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1123 | * xHCI uses normal TRBs for both bulk and interrupt. When the |
| 1124 | * interrupt endpoint is to be serviced, the xHC will consume |
| 1125 | * (at most) one TD. A TD (comprised of sg list entries) can |
| 1126 | * take several service intervals to transmit. |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1127 | */ |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1128 | return xhci_bulk_tx(udev, pipe, length, buffer); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1129 | } |
| 1130 | |
| 1131 | /** |
| 1132 | * submit the BULK type of request to the USB Device |
| 1133 | * |
| 1134 | * @param udev pointer to the USB device |
| 1135 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1136 | * @param buffer buffer to be read/written based on the request |
| 1137 | * @param length length of the buffer |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1138 | * Return: returns 0 if successful else -1 on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1139 | */ |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1140 | static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe, |
| 1141 | void *buffer, int length) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1142 | { |
| 1143 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 1144 | printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 1145 | return -EINVAL; |
| 1146 | } |
| 1147 | |
| 1148 | return xhci_bulk_tx(udev, pipe, length, buffer); |
| 1149 | } |
| 1150 | |
| 1151 | /** |
| 1152 | * submit the control type of request to the Root hub/Device based on the devnum |
| 1153 | * |
| 1154 | * @param udev pointer to the USB device |
| 1155 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1156 | * @param buffer buffer to be read/written based on the request |
| 1157 | * @param length length of the buffer |
| 1158 | * @param setup Request type |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1159 | * @param root_portnr Root port number that this device is on |
Heinrich Schuchardt | 47b4c02 | 2022-01-19 18:05:50 +0100 | [diff] [blame] | 1160 | * Return: returns 0 if successful else -1 on failure |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1161 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1162 | static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe, |
| 1163 | void *buffer, int length, |
| 1164 | struct devrequest *setup, int root_portnr) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1165 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 1166 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1167 | int ret = 0; |
| 1168 | |
| 1169 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 1170 | printf("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 1171 | return -EINVAL; |
| 1172 | } |
| 1173 | |
| 1174 | if (usb_pipedevice(pipe) == ctrl->rootdev) |
| 1175 | return xhci_submit_root(udev, pipe, buffer, setup); |
| 1176 | |
Ted Chen | a2f4f9a | 2016-03-18 17:56:52 +1030 | [diff] [blame] | 1177 | if (setup->request == USB_REQ_SET_ADDRESS && |
| 1178 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1179 | return xhci_address_device(udev, root_portnr); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1180 | |
Ted Chen | a2f4f9a | 2016-03-18 17:56:52 +1030 | [diff] [blame] | 1181 | if (setup->request == USB_REQ_SET_CONFIGURATION && |
| 1182 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1183 | ret = xhci_set_configuration(udev); |
| 1184 | if (ret) { |
| 1185 | puts("Failed to configure xHCI endpoint\n"); |
| 1186 | return ret; |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | return xhci_ctrl_tx(udev, pipe, setup, length, buffer); |
| 1191 | } |
| 1192 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1193 | static int xhci_lowlevel_init(struct xhci_ctrl *ctrl) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1194 | { |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1195 | struct xhci_hccr *hccr; |
| 1196 | struct xhci_hcor *hcor; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1197 | uint32_t val; |
| 1198 | uint32_t val2; |
| 1199 | uint32_t reg; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1200 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1201 | hccr = ctrl->hccr; |
| 1202 | hcor = ctrl->hcor; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1203 | /* |
| 1204 | * Program the Number of Device Slots Enabled field in the CONFIG |
| 1205 | * register with the max value of slots the HC can handle. |
| 1206 | */ |
| 1207 | val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK); |
| 1208 | val2 = xhci_readl(&hcor->or_config); |
| 1209 | val |= (val2 & ~HCS_SLOTS_MASK); |
| 1210 | xhci_writel(&hcor->or_config, val); |
| 1211 | |
| 1212 | /* initializing xhci data structures */ |
| 1213 | if (xhci_mem_init(ctrl, hccr, hcor) < 0) |
| 1214 | return -ENOMEM; |
Mark Kettenis | 4eb77d3 | 2023-01-21 20:28:00 +0100 | [diff] [blame] | 1215 | ctrl->hub_desc = descriptor.hub; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1216 | |
| 1217 | reg = xhci_readl(&hccr->cr_hcsparams1); |
Mark Kettenis | 4eb77d3 | 2023-01-21 20:28:00 +0100 | [diff] [blame] | 1218 | ctrl->hub_desc.bNbrPorts = HCS_MAX_PORTS(reg); |
| 1219 | printf("Register %x NbrPorts %d\n", reg, ctrl->hub_desc.bNbrPorts); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1220 | |
| 1221 | /* Port Indicators */ |
| 1222 | reg = xhci_readl(&hccr->cr_hccparams); |
| 1223 | if (HCS_INDICATOR(reg)) |
Mark Kettenis | 4eb77d3 | 2023-01-21 20:28:00 +0100 | [diff] [blame] | 1224 | put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics) |
| 1225 | | 0x80, &ctrl->hub_desc.wHubCharacteristics); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1226 | |
| 1227 | /* Port Power Control */ |
| 1228 | if (HCC_PPC(reg)) |
Mark Kettenis | 4eb77d3 | 2023-01-21 20:28:00 +0100 | [diff] [blame] | 1229 | put_unaligned(get_unaligned(&ctrl->hub_desc.wHubCharacteristics) |
| 1230 | | 0x01, &ctrl->hub_desc.wHubCharacteristics); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1231 | |
| 1232 | if (xhci_start(hcor)) { |
| 1233 | xhci_reset(hcor); |
| 1234 | return -ENODEV; |
| 1235 | } |
| 1236 | |
| 1237 | /* Zero'ing IRQ control register and IRQ pending register */ |
| 1238 | xhci_writel(&ctrl->ir_set->irq_control, 0x0); |
| 1239 | xhci_writel(&ctrl->ir_set->irq_pending, 0x0); |
| 1240 | |
| 1241 | reg = HC_VERSION(xhci_readl(&hccr->cr_capbase)); |
| 1242 | printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff); |
developer | d1c2da4 | 2020-09-08 18:59:55 +0200 | [diff] [blame] | 1243 | ctrl->hci_version = reg; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1244 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1245 | return 0; |
| 1246 | } |
| 1247 | |
| 1248 | static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl) |
| 1249 | { |
| 1250 | u32 temp; |
| 1251 | |
| 1252 | xhci_reset(ctrl->hcor); |
| 1253 | |
| 1254 | debug("// Disabling event ring interrupts\n"); |
| 1255 | temp = xhci_readl(&ctrl->hcor->or_usbsts); |
| 1256 | xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT); |
| 1257 | temp = xhci_readl(&ctrl->ir_set->irq_pending); |
| 1258 | xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1259 | |
| 1260 | return 0; |
| 1261 | } |
| 1262 | |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1263 | static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev, |
| 1264 | unsigned long pipe, void *buffer, int length, |
| 1265 | struct devrequest *setup) |
| 1266 | { |
| 1267 | struct usb_device *uhop; |
| 1268 | struct udevice *hub; |
| 1269 | int root_portnr = 0; |
| 1270 | |
| 1271 | debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__, |
| 1272 | dev->name, udev, udev->dev->name, udev->portnr); |
| 1273 | hub = udev->dev; |
| 1274 | if (device_get_uclass_id(hub) == UCLASS_USB_HUB) { |
| 1275 | /* Figure out our port number on the root hub */ |
Bin Meng | 5ecfd5d | 2017-07-19 21:51:11 +0800 | [diff] [blame] | 1276 | if (usb_hub_is_root_hub(hub)) { |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1277 | root_portnr = udev->portnr; |
| 1278 | } else { |
Bin Meng | 5ecfd5d | 2017-07-19 21:51:11 +0800 | [diff] [blame] | 1279 | while (!usb_hub_is_root_hub(hub->parent)) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1280 | hub = hub->parent; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 1281 | uhop = dev_get_parent_priv(hub); |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1282 | root_portnr = uhop->portnr; |
| 1283 | } |
| 1284 | } |
| 1285 | /* |
| 1286 | struct usb_device *hop = udev; |
| 1287 | |
| 1288 | if (hop->parent) |
| 1289 | while (hop->parent->parent) |
| 1290 | hop = hop->parent; |
| 1291 | */ |
| 1292 | return _xhci_submit_control_msg(udev, pipe, buffer, length, setup, |
| 1293 | root_portnr); |
| 1294 | } |
| 1295 | |
| 1296 | static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, |
| 1297 | unsigned long pipe, void *buffer, int length) |
| 1298 | { |
| 1299 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1300 | return _xhci_submit_bulk_msg(udev, pipe, buffer, length); |
| 1301 | } |
| 1302 | |
| 1303 | static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev, |
| 1304 | unsigned long pipe, void *buffer, int length, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1305 | int interval, bool nonblock) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1306 | { |
| 1307 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1308 | return _xhci_submit_int_msg(udev, pipe, buffer, length, interval, |
| 1309 | nonblock); |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev) |
| 1313 | { |
| 1314 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1315 | return _xhci_alloc_device(udev); |
| 1316 | } |
| 1317 | |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1318 | static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev) |
| 1319 | { |
| 1320 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1321 | struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev); |
| 1322 | struct xhci_virt_device *virt_dev; |
| 1323 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1324 | struct xhci_container_ctx *out_ctx; |
| 1325 | struct xhci_container_ctx *in_ctx; |
| 1326 | struct xhci_slot_ctx *slot_ctx; |
| 1327 | int slot_id = udev->slot_id; |
| 1328 | unsigned think_time; |
| 1329 | |
| 1330 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1331 | |
| 1332 | /* Ignore root hubs */ |
| 1333 | if (usb_hub_is_root_hub(udev->dev)) |
| 1334 | return 0; |
| 1335 | |
| 1336 | virt_dev = ctrl->devs[slot_id]; |
| 1337 | BUG_ON(!virt_dev); |
| 1338 | |
| 1339 | out_ctx = virt_dev->out_ctx; |
| 1340 | in_ctx = virt_dev->in_ctx; |
| 1341 | |
| 1342 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
| 1343 | /* Initialize the input context control */ |
Bin Meng | 03760fe | 2018-05-23 23:40:47 -0700 | [diff] [blame] | 1344 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1345 | ctrl_ctx->drop_flags = 0; |
| 1346 | |
| 1347 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
| 1348 | |
| 1349 | /* slot context */ |
| 1350 | xhci_slot_copy(ctrl, in_ctx, out_ctx); |
| 1351 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); |
| 1352 | |
| 1353 | /* Update hub related fields */ |
| 1354 | slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); |
Bin Meng | 18f5bcd | 2018-05-23 23:40:49 -0700 | [diff] [blame] | 1355 | /* |
| 1356 | * refer to section 6.2.2: MTT should be 0 for full speed hub, |
| 1357 | * but it may be already set to 1 when setup an xHCI virtual |
| 1358 | * device, so clear it anyway. |
| 1359 | */ |
| 1360 | if (hub->tt.multi) |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1361 | slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); |
Bin Meng | 18f5bcd | 2018-05-23 23:40:49 -0700 | [diff] [blame] | 1362 | else if (udev->speed == USB_SPEED_FULL) |
| 1363 | slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1364 | slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild)); |
| 1365 | /* |
| 1366 | * Set TT think time - convert from ns to FS bit times. |
| 1367 | * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns |
| 1368 | * |
| 1369 | * 0 = 8 FS bit times, 1 = 16 FS bit times, |
| 1370 | * 2 = 24 FS bit times, 3 = 32 FS bit times. |
| 1371 | * |
| 1372 | * This field shall be 0 if the device is not a high-spped hub. |
| 1373 | */ |
| 1374 | think_time = hub->tt.think_time; |
| 1375 | if (think_time != 0) |
| 1376 | think_time = (think_time / 666) - 1; |
| 1377 | if (udev->speed == USB_SPEED_HIGH) |
| 1378 | slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time)); |
Bin Meng | d038398 | 2018-05-23 23:40:48 -0700 | [diff] [blame] | 1379 | slot_ctx->dev_state = 0; |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1380 | |
| 1381 | return xhci_configure_endpoints(udev, false); |
| 1382 | } |
| 1383 | |
Bin Meng | 1bc4ce9 | 2017-09-07 06:13:18 -0700 | [diff] [blame] | 1384 | static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size) |
| 1385 | { |
| 1386 | /* |
| 1387 | * xHCD allocates one segment which includes 64 TRBs for each endpoint |
| 1388 | * and the last TRB in this segment is configured as a link TRB to form |
| 1389 | * a TRB ring. Each TRB can transfer up to 64K bytes, however data |
| 1390 | * buffers referenced by transfer TRBs shall not span 64KB boundaries. |
| 1391 | * Hence the maximum number of TRBs we can use in one transfer is 62. |
| 1392 | */ |
| 1393 | *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE; |
| 1394 | |
| 1395 | return 0; |
| 1396 | } |
| 1397 | |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1398 | int xhci_register(struct udevice *dev, struct xhci_hccr *hccr, |
| 1399 | struct xhci_hcor *hcor) |
| 1400 | { |
| 1401 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1402 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
| 1403 | int ret; |
| 1404 | |
| 1405 | debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name, |
| 1406 | ctrl, hccr, hcor); |
| 1407 | |
| 1408 | ctrl->dev = dev; |
| 1409 | |
| 1410 | /* |
| 1411 | * XHCI needs to issue a Address device command to setup |
| 1412 | * proper device context structures, before it can interact |
| 1413 | * with the device. So a get_descriptor will fail before any |
| 1414 | * of that is done for XHCI unlike EHCI. |
| 1415 | */ |
| 1416 | priv->desc_before_addr = false; |
| 1417 | |
| 1418 | ret = xhci_reset(hcor); |
| 1419 | if (ret) |
| 1420 | goto err; |
| 1421 | |
| 1422 | ctrl->hccr = hccr; |
| 1423 | ctrl->hcor = hcor; |
| 1424 | ret = xhci_lowlevel_init(ctrl); |
| 1425 | if (ret) |
| 1426 | goto err; |
| 1427 | |
| 1428 | return 0; |
| 1429 | err: |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1430 | debug("%s: failed, ret=%d\n", __func__, ret); |
| 1431 | return ret; |
| 1432 | } |
| 1433 | |
| 1434 | int xhci_deregister(struct udevice *dev) |
| 1435 | { |
| 1436 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1437 | |
| 1438 | xhci_lowlevel_stop(ctrl); |
| 1439 | xhci_cleanup(ctrl); |
| 1440 | |
| 1441 | return 0; |
| 1442 | } |
| 1443 | |
| 1444 | struct dm_usb_ops xhci_usb_ops = { |
| 1445 | .control = xhci_submit_control_msg, |
| 1446 | .bulk = xhci_submit_bulk_msg, |
| 1447 | .interrupt = xhci_submit_int_msg, |
| 1448 | .alloc_device = xhci_alloc_device, |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1449 | .update_hub_device = xhci_update_hub_device, |
Bin Meng | 1bc4ce9 | 2017-09-07 06:13:18 -0700 | [diff] [blame] | 1450 | .get_max_xfer_size = xhci_get_max_xfer_size, |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1451 | }; |