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> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 25 | #include <log.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 26 | #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 Glass | 4dcacfc | 2020-05-10 11:40:13 -0600 | [diff] [blame] | 32 | #include <linux/bitops.h> |
Simon Glass | c06c1be | 2020-05-10 11:40:08 -0600 | [diff] [blame] | 33 | #include <linux/bug.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 34 | #include <linux/delay.h> |
Masahiro Yamada | 64e4f7f | 2016-09-21 11:28:57 +0900 | [diff] [blame] | 35 | #include <linux/errno.h> |
Jean-Jacques Hiblot | ad4142b | 2019-09-11 11:33:46 +0200 | [diff] [blame] | 36 | #include <usb/xhci.h> |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 37 | |
| 38 | #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT |
| 39 | #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 |
| 40 | #endif |
| 41 | |
| 42 | static 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 Meng | 0d66b3a | 2017-07-19 21:50:00 +0800 | [diff] [blame] | 57 | { /* Device removable */ |
| 58 | } /* at most 7 ports! XXX */ |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 59 | }, |
| 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 Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 116 | #if !CONFIG_IS_ENABLED(DM_USB) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 117 | static struct xhci_ctrl xhcic[CONFIG_USB_MAX_CONTROLLER_COUNT]; |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 118 | #endif |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 119 | |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 120 | struct xhci_ctrl *xhci_get_ctrl(struct usb_device *udev) |
| 121 | { |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 122 | #if CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 123 | 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 Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 132 | return udev->controller; |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 133 | #endif |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 134 | } |
| 135 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 136 | /** |
| 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 | */ |
| 146 | static 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 | */ |
| 171 | static 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 | |
Nicolas Saenz Julienne | 566107e | 2020-06-29 18:37:25 +0200 | [diff] [blame^] | 193 | #if CONFIG_IS_ENABLED(DM_USB) |
| 194 | /** |
| 195 | * Resets XHCI Hardware |
| 196 | * |
| 197 | * @param ctrl pointer to host controller |
| 198 | * @return 0 if OK, or a negative error code. |
| 199 | */ |
| 200 | static int xhci_reset_hw(struct xhci_ctrl *ctrl) |
| 201 | { |
| 202 | int ret; |
| 203 | |
| 204 | ret = reset_get_by_index(ctrl->dev, 0, &ctrl->reset); |
| 205 | if (ret && ret != -ENOENT && ret != -ENOTSUPP) { |
| 206 | dev_err(ctrl->dev, "failed to get reset\n"); |
| 207 | return ret; |
| 208 | } |
| 209 | |
| 210 | if (reset_valid(&ctrl->reset)) { |
| 211 | ret = reset_assert(&ctrl->reset); |
| 212 | if (ret) |
| 213 | return ret; |
| 214 | |
| 215 | ret = reset_deassert(&ctrl->reset); |
| 216 | if (ret) |
| 217 | return ret; |
| 218 | } |
| 219 | |
| 220 | return 0; |
| 221 | } |
| 222 | #endif |
| 223 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 224 | /** |
| 225 | * Resets the XHCI Controller |
| 226 | * |
| 227 | * @param hcor pointer to host controller operation registers |
| 228 | * @return -EBUSY if XHCI Controller is not halted else status of handshake |
| 229 | */ |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 230 | static int xhci_reset(struct xhci_hcor *hcor) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 231 | { |
| 232 | u32 cmd; |
| 233 | u32 state; |
| 234 | int ret; |
| 235 | |
| 236 | /* Halting the Host first */ |
Sergey Temerkhanov | 65bb454 | 2015-08-17 15:38:07 +0300 | [diff] [blame] | 237 | debug("// Halt the HC: %p\n", hcor); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 238 | state = xhci_readl(&hcor->or_usbsts) & STS_HALT; |
| 239 | if (!state) { |
| 240 | cmd = xhci_readl(&hcor->or_usbcmd); |
| 241 | cmd &= ~CMD_RUN; |
| 242 | xhci_writel(&hcor->or_usbcmd, cmd); |
| 243 | } |
| 244 | |
| 245 | ret = handshake(&hcor->or_usbsts, |
| 246 | STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC); |
| 247 | if (ret) { |
| 248 | printf("Host not halted after %u microseconds.\n", |
| 249 | XHCI_MAX_HALT_USEC); |
| 250 | return -EBUSY; |
| 251 | } |
| 252 | |
| 253 | debug("// Reset the HC\n"); |
| 254 | cmd = xhci_readl(&hcor->or_usbcmd); |
| 255 | cmd |= CMD_RESET; |
| 256 | xhci_writel(&hcor->or_usbcmd, cmd); |
| 257 | |
| 258 | ret = handshake(&hcor->or_usbcmd, CMD_RESET, 0, XHCI_MAX_RESET_USEC); |
| 259 | if (ret) |
| 260 | return ret; |
| 261 | |
| 262 | /* |
| 263 | * xHCI cannot write to any doorbells or operational registers other |
| 264 | * than status until the "Controller Not Ready" flag is cleared. |
| 265 | */ |
| 266 | return handshake(&hcor->or_usbsts, STS_CNR, 0, XHCI_MAX_RESET_USEC); |
| 267 | } |
| 268 | |
| 269 | /** |
| 270 | * Used for passing endpoint bitmasks between the core and HCDs. |
| 271 | * Find the index for an endpoint given its descriptor. |
| 272 | * Use the return value to right shift 1 for the bitmask. |
| 273 | * |
| 274 | * Index = (epnum * 2) + direction - 1, |
| 275 | * where direction = 0 for OUT, 1 for IN. |
| 276 | * For control endpoints, the IN index is used (OUT index is unused), so |
| 277 | * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2) |
| 278 | * |
| 279 | * @param desc USB enpdoint Descriptor |
| 280 | * @return index of the Endpoint |
| 281 | */ |
| 282 | static unsigned int xhci_get_ep_index(struct usb_endpoint_descriptor *desc) |
| 283 | { |
| 284 | unsigned int index; |
| 285 | |
| 286 | if (usb_endpoint_xfer_control(desc)) |
| 287 | index = (unsigned int)(usb_endpoint_num(desc) * 2); |
| 288 | else |
| 289 | index = (unsigned int)((usb_endpoint_num(desc) * 2) - |
| 290 | (usb_endpoint_dir_in(desc) ? 0 : 1)); |
| 291 | |
| 292 | return index; |
| 293 | } |
| 294 | |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 295 | /* |
| 296 | * Convert bInterval expressed in microframes (in 1-255 range) to exponent of |
| 297 | * microframes, rounded down to nearest power of 2. |
| 298 | */ |
| 299 | static unsigned int xhci_microframes_to_exponent(unsigned int desc_interval, |
| 300 | unsigned int min_exponent, |
| 301 | unsigned int max_exponent) |
| 302 | { |
| 303 | unsigned int interval; |
| 304 | |
| 305 | interval = fls(desc_interval) - 1; |
| 306 | interval = clamp_val(interval, min_exponent, max_exponent); |
| 307 | if ((1 << interval) != desc_interval) |
| 308 | debug("rounding interval to %d microframes, "\ |
| 309 | "ep desc says %d microframes\n", |
| 310 | 1 << interval, desc_interval); |
| 311 | |
| 312 | return interval; |
| 313 | } |
| 314 | |
| 315 | static unsigned int xhci_parse_microframe_interval(struct usb_device *udev, |
| 316 | struct usb_endpoint_descriptor *endpt_desc) |
| 317 | { |
| 318 | if (endpt_desc->bInterval == 0) |
| 319 | return 0; |
| 320 | |
| 321 | return xhci_microframes_to_exponent(endpt_desc->bInterval, 0, 15); |
| 322 | } |
| 323 | |
| 324 | static unsigned int xhci_parse_frame_interval(struct usb_device *udev, |
| 325 | struct usb_endpoint_descriptor *endpt_desc) |
| 326 | { |
| 327 | return xhci_microframes_to_exponent(endpt_desc->bInterval * 8, 3, 10); |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * Convert interval expressed as 2^(bInterval - 1) == interval into |
| 332 | * straight exponent value 2^n == interval. |
| 333 | */ |
| 334 | static unsigned int xhci_parse_exponent_interval(struct usb_device *udev, |
| 335 | struct usb_endpoint_descriptor *endpt_desc) |
| 336 | { |
| 337 | unsigned int interval; |
| 338 | |
| 339 | interval = clamp_val(endpt_desc->bInterval, 1, 16) - 1; |
| 340 | if (interval != endpt_desc->bInterval - 1) |
| 341 | debug("ep %#x - rounding interval to %d %sframes\n", |
| 342 | endpt_desc->bEndpointAddress, 1 << interval, |
| 343 | udev->speed == USB_SPEED_FULL ? "" : "micro"); |
| 344 | |
| 345 | if (udev->speed == USB_SPEED_FULL) { |
| 346 | /* |
| 347 | * Full speed isoc endpoints specify interval in frames, |
| 348 | * not microframes. We are using microframes everywhere, |
| 349 | * so adjust accordingly. |
| 350 | */ |
| 351 | interval += 3; /* 1 frame = 2^3 uframes */ |
| 352 | } |
| 353 | |
| 354 | return interval; |
| 355 | } |
| 356 | |
| 357 | /* |
| 358 | * Return the polling or NAK interval. |
| 359 | * |
| 360 | * The polling interval is expressed in "microframes". If xHCI's Interval field |
| 361 | * is set to N, it will service the endpoint every 2^(Interval)*125us. |
| 362 | * |
| 363 | * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval |
| 364 | * is set to 0. |
| 365 | */ |
| 366 | static unsigned int xhci_get_endpoint_interval(struct usb_device *udev, |
| 367 | struct usb_endpoint_descriptor *endpt_desc) |
| 368 | { |
| 369 | unsigned int interval = 0; |
| 370 | |
| 371 | switch (udev->speed) { |
| 372 | case USB_SPEED_HIGH: |
| 373 | /* Max NAK rate */ |
| 374 | if (usb_endpoint_xfer_control(endpt_desc) || |
| 375 | usb_endpoint_xfer_bulk(endpt_desc)) { |
| 376 | interval = xhci_parse_microframe_interval(udev, |
| 377 | endpt_desc); |
| 378 | break; |
| 379 | } |
| 380 | /* Fall through - SS and HS isoc/int have same decoding */ |
| 381 | |
| 382 | case USB_SPEED_SUPER: |
| 383 | if (usb_endpoint_xfer_int(endpt_desc) || |
| 384 | usb_endpoint_xfer_isoc(endpt_desc)) { |
| 385 | interval = xhci_parse_exponent_interval(udev, |
| 386 | endpt_desc); |
| 387 | } |
| 388 | break; |
| 389 | |
| 390 | case USB_SPEED_FULL: |
| 391 | if (usb_endpoint_xfer_isoc(endpt_desc)) { |
| 392 | interval = xhci_parse_exponent_interval(udev, |
| 393 | endpt_desc); |
| 394 | break; |
| 395 | } |
| 396 | /* |
| 397 | * Fall through for interrupt endpoint interval decoding |
| 398 | * since it uses the same rules as low speed interrupt |
| 399 | * endpoints. |
| 400 | */ |
| 401 | |
| 402 | case USB_SPEED_LOW: |
| 403 | if (usb_endpoint_xfer_int(endpt_desc) || |
| 404 | usb_endpoint_xfer_isoc(endpt_desc)) { |
| 405 | interval = xhci_parse_frame_interval(udev, endpt_desc); |
| 406 | } |
| 407 | break; |
| 408 | |
| 409 | default: |
| 410 | BUG(); |
| 411 | } |
| 412 | |
| 413 | return interval; |
| 414 | } |
| 415 | |
| 416 | /* |
| 417 | * The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps. |
| 418 | * High speed endpoint descriptors can define "the number of additional |
| 419 | * transaction opportunities per microframe", but that goes in the Max Burst |
| 420 | * endpoint context field. |
| 421 | */ |
| 422 | static u32 xhci_get_endpoint_mult(struct usb_device *udev, |
| 423 | struct usb_endpoint_descriptor *endpt_desc, |
| 424 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 425 | { |
| 426 | if (udev->speed < USB_SPEED_SUPER || |
| 427 | !usb_endpoint_xfer_isoc(endpt_desc)) |
| 428 | return 0; |
| 429 | |
| 430 | return ss_ep_comp_desc->bmAttributes; |
| 431 | } |
| 432 | |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 433 | static u32 xhci_get_endpoint_max_burst(struct usb_device *udev, |
| 434 | struct usb_endpoint_descriptor *endpt_desc, |
| 435 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 436 | { |
| 437 | /* Super speed and Plus have max burst in ep companion desc */ |
| 438 | if (udev->speed >= USB_SPEED_SUPER) |
| 439 | return ss_ep_comp_desc->bMaxBurst; |
| 440 | |
| 441 | if (udev->speed == USB_SPEED_HIGH && |
| 442 | (usb_endpoint_xfer_isoc(endpt_desc) || |
| 443 | usb_endpoint_xfer_int(endpt_desc))) |
| 444 | return usb_endpoint_maxp_mult(endpt_desc) - 1; |
| 445 | |
| 446 | return 0; |
| 447 | } |
| 448 | |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 449 | /* |
| 450 | * Return the maximum endpoint service interval time (ESIT) payload. |
| 451 | * Basically, this is the maxpacket size, multiplied by the burst size |
| 452 | * and mult size. |
| 453 | */ |
| 454 | static u32 xhci_get_max_esit_payload(struct usb_device *udev, |
| 455 | struct usb_endpoint_descriptor *endpt_desc, |
| 456 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc) |
| 457 | { |
| 458 | int max_burst; |
| 459 | int max_packet; |
| 460 | |
| 461 | /* Only applies for interrupt or isochronous endpoints */ |
| 462 | if (usb_endpoint_xfer_control(endpt_desc) || |
| 463 | usb_endpoint_xfer_bulk(endpt_desc)) |
| 464 | return 0; |
| 465 | |
| 466 | /* SuperSpeed Isoc ep with less than 48k per esit */ |
| 467 | if (udev->speed >= USB_SPEED_SUPER) |
| 468 | return le16_to_cpu(ss_ep_comp_desc->wBytesPerInterval); |
| 469 | |
| 470 | max_packet = usb_endpoint_maxp(endpt_desc); |
| 471 | max_burst = usb_endpoint_maxp_mult(endpt_desc); |
| 472 | |
| 473 | /* A 0 in max burst means 1 transfer per ESIT */ |
| 474 | return max_packet * max_burst; |
| 475 | } |
| 476 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 477 | /** |
| 478 | * Issue a configure endpoint command or evaluate context command |
| 479 | * and wait for it to finish. |
| 480 | * |
| 481 | * @param udev pointer to the Device Data Structure |
| 482 | * @param ctx_change flag to indicate the Context has changed or NOT |
| 483 | * @return 0 on success, -1 on failure |
| 484 | */ |
| 485 | static int xhci_configure_endpoints(struct usb_device *udev, bool ctx_change) |
| 486 | { |
| 487 | struct xhci_container_ctx *in_ctx; |
| 488 | struct xhci_virt_device *virt_dev; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 489 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 490 | union xhci_trb *event; |
| 491 | |
| 492 | virt_dev = ctrl->devs[udev->slot_id]; |
| 493 | in_ctx = virt_dev->in_ctx; |
| 494 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 495 | xhci_flush_cache((uintptr_t)in_ctx->bytes, in_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 496 | xhci_queue_command(ctrl, in_ctx->bytes, udev->slot_id, 0, |
| 497 | ctx_change ? TRB_EVAL_CONTEXT : TRB_CONFIG_EP); |
| 498 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
| 499 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) |
| 500 | != udev->slot_id); |
| 501 | |
| 502 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { |
| 503 | case COMP_SUCCESS: |
| 504 | debug("Successful %s command\n", |
| 505 | ctx_change ? "Evaluate Context" : "Configure Endpoint"); |
| 506 | break; |
| 507 | default: |
| 508 | printf("ERROR: %s command returned completion code %d.\n", |
| 509 | ctx_change ? "Evaluate Context" : "Configure Endpoint", |
| 510 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); |
| 511 | return -EINVAL; |
| 512 | } |
| 513 | |
| 514 | xhci_acknowledge_event(ctrl); |
| 515 | |
| 516 | return 0; |
| 517 | } |
| 518 | |
| 519 | /** |
| 520 | * Configure the endpoint, programming the device contexts. |
| 521 | * |
| 522 | * @param udev pointer to the USB device structure |
| 523 | * @return returns the status of the xhci_configure_endpoints |
| 524 | */ |
| 525 | static int xhci_set_configuration(struct usb_device *udev) |
| 526 | { |
| 527 | struct xhci_container_ctx *in_ctx; |
| 528 | struct xhci_container_ctx *out_ctx; |
| 529 | struct xhci_input_control_ctx *ctrl_ctx; |
| 530 | struct xhci_slot_ctx *slot_ctx; |
| 531 | struct xhci_ep_ctx *ep_ctx[MAX_EP_CTX_NUM]; |
| 532 | int cur_ep; |
| 533 | int max_ep_flag = 0; |
| 534 | int ep_index; |
| 535 | unsigned int dir; |
| 536 | unsigned int ep_type; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 537 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 538 | int num_of_ep; |
| 539 | int ep_flag = 0; |
| 540 | u64 trb_64 = 0; |
| 541 | int slot_id = udev->slot_id; |
| 542 | struct xhci_virt_device *virt_dev = ctrl->devs[slot_id]; |
| 543 | struct usb_interface *ifdesc; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 544 | u32 max_esit_payload; |
| 545 | unsigned int interval; |
| 546 | unsigned int mult; |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 547 | unsigned int max_burst; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 548 | unsigned int avg_trb_len; |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 549 | unsigned int err_count = 0; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 550 | |
| 551 | out_ctx = virt_dev->out_ctx; |
| 552 | in_ctx = virt_dev->in_ctx; |
| 553 | |
| 554 | num_of_ep = udev->config.if_desc[0].no_of_ep; |
| 555 | ifdesc = &udev->config.if_desc[0]; |
| 556 | |
| 557 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
Bin Meng | ec0501b | 2017-07-19 21:49:56 +0800 | [diff] [blame] | 558 | /* Initialize the input context control */ |
| 559 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 560 | ctrl_ctx->drop_flags = 0; |
| 561 | |
| 562 | /* EP_FLAG gives values 1 & 4 for EP1OUT and EP2IN */ |
| 563 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { |
| 564 | ep_flag = xhci_get_ep_index(&ifdesc->ep_desc[cur_ep]); |
| 565 | ctrl_ctx->add_flags |= cpu_to_le32(1 << (ep_flag + 1)); |
| 566 | if (max_ep_flag < ep_flag) |
| 567 | max_ep_flag = ep_flag; |
| 568 | } |
| 569 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 570 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 571 | |
| 572 | /* slot context */ |
| 573 | xhci_slot_copy(ctrl, in_ctx, out_ctx); |
| 574 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); |
Bin Meng | a1ae60e | 2018-05-23 23:40:50 -0700 | [diff] [blame] | 575 | slot_ctx->dev_info &= ~(cpu_to_le32(LAST_CTX_MASK)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 576 | slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(max_ep_flag + 1) | 0); |
| 577 | |
| 578 | xhci_endpoint_copy(ctrl, in_ctx, out_ctx, 0); |
| 579 | |
| 580 | /* filling up ep contexts */ |
| 581 | for (cur_ep = 0; cur_ep < num_of_ep; cur_ep++) { |
| 582 | struct usb_endpoint_descriptor *endpt_desc = NULL; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 583 | struct usb_ss_ep_comp_descriptor *ss_ep_comp_desc = NULL; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 584 | |
| 585 | endpt_desc = &ifdesc->ep_desc[cur_ep]; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 586 | ss_ep_comp_desc = &ifdesc->ss_ep_comp_desc[cur_ep]; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 587 | trb_64 = 0; |
| 588 | |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 589 | /* |
| 590 | * Get values to fill the endpoint context, mostly from ep |
| 591 | * descriptor. The average TRB buffer lengt for bulk endpoints |
| 592 | * is unclear as we have no clue on scatter gather list entry |
| 593 | * size. For Isoc and Int, set it to max available. |
| 594 | * See xHCI 1.1 spec 4.14.1.1 for details. |
| 595 | */ |
| 596 | max_esit_payload = xhci_get_max_esit_payload(udev, endpt_desc, |
| 597 | ss_ep_comp_desc); |
| 598 | interval = xhci_get_endpoint_interval(udev, endpt_desc); |
| 599 | mult = xhci_get_endpoint_mult(udev, endpt_desc, |
| 600 | ss_ep_comp_desc); |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 601 | max_burst = xhci_get_endpoint_max_burst(udev, endpt_desc, |
| 602 | ss_ep_comp_desc); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 603 | avg_trb_len = max_esit_payload; |
| 604 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 605 | ep_index = xhci_get_ep_index(endpt_desc); |
| 606 | ep_ctx[ep_index] = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); |
| 607 | |
| 608 | /* Allocate the ep rings */ |
| 609 | virt_dev->eps[ep_index].ring = xhci_ring_alloc(1, true); |
| 610 | if (!virt_dev->eps[ep_index].ring) |
| 611 | return -ENOMEM; |
| 612 | |
| 613 | /*NOTE: ep_desc[0] actually represents EP1 and so on */ |
| 614 | dir = (((endpt_desc->bEndpointAddress) & (0x80)) >> 7); |
| 615 | ep_type = (((endpt_desc->bmAttributes) & (0x3)) | (dir << 2)); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 616 | |
| 617 | ep_ctx[ep_index]->ep_info = |
| 618 | cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) | |
| 619 | EP_INTERVAL(interval) | EP_MULT(mult)); |
| 620 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 621 | ep_ctx[ep_index]->ep_info2 = |
| 622 | cpu_to_le32(ep_type << EP_TYPE_SHIFT); |
| 623 | ep_ctx[ep_index]->ep_info2 |= |
| 624 | cpu_to_le32(MAX_PACKET |
| 625 | (get_unaligned(&endpt_desc->wMaxPacketSize))); |
| 626 | |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 627 | /* Allow 3 retries for everything but isoc, set CErr = 3 */ |
| 628 | if (!usb_endpoint_xfer_isoc(endpt_desc)) |
| 629 | err_count = 3; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 630 | ep_ctx[ep_index]->ep_info2 |= |
Bin Meng | bdedd2a | 2017-09-18 06:40:48 -0700 | [diff] [blame] | 631 | cpu_to_le32(MAX_BURST(max_burst) | |
Bin Meng | 7c3b76d | 2017-09-18 06:40:49 -0700 | [diff] [blame] | 632 | ERROR_COUNT(err_count)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 633 | |
| 634 | trb_64 = (uintptr_t) |
| 635 | virt_dev->eps[ep_index].ring->enqueue; |
| 636 | ep_ctx[ep_index]->deq = cpu_to_le64(trb_64 | |
| 637 | virt_dev->eps[ep_index].ring->cycle_state); |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 638 | |
Bin Meng | c03fb20 | 2017-09-18 06:40:50 -0700 | [diff] [blame] | 639 | /* |
| 640 | * xHCI spec 6.2.3: |
| 641 | * 'Average TRB Length' should be 8 for control endpoints. |
| 642 | */ |
| 643 | if (usb_endpoint_xfer_control(endpt_desc)) |
| 644 | avg_trb_len = 8; |
Bin Meng | 87033f0 | 2017-09-18 06:40:47 -0700 | [diff] [blame] | 645 | ep_ctx[ep_index]->tx_info = |
| 646 | cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) | |
| 647 | EP_AVG_TRB_LENGTH(avg_trb_len)); |
developer | 507fc9b | 2020-05-02 11:35:18 +0200 | [diff] [blame] | 648 | |
| 649 | /* |
| 650 | * The MediaTek xHCI defines some extra SW parameters which |
| 651 | * are put into reserved DWs in Slot and Endpoint Contexts |
| 652 | * for synchronous endpoints. |
| 653 | */ |
| 654 | if (IS_ENABLED(CONFIG_USB_XHCI_MTK)) { |
| 655 | ep_ctx[ep_index]->reserved[0] = |
| 656 | cpu_to_le32(EP_BPKTS(1) | EP_BBM(1)); |
| 657 | } |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 658 | } |
| 659 | |
| 660 | return xhci_configure_endpoints(udev, false); |
| 661 | } |
| 662 | |
| 663 | /** |
| 664 | * Issue an Address Device command (which will issue a SetAddress request to |
| 665 | * the device). |
| 666 | * |
| 667 | * @param udev pointer to the Device Data Structure |
| 668 | * @return 0 if successful else error code on failure |
| 669 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 670 | static int xhci_address_device(struct usb_device *udev, int root_portnr) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 671 | { |
| 672 | int ret = 0; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 673 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 674 | struct xhci_slot_ctx *slot_ctx; |
| 675 | struct xhci_input_control_ctx *ctrl_ctx; |
| 676 | struct xhci_virt_device *virt_dev; |
| 677 | int slot_id = udev->slot_id; |
| 678 | union xhci_trb *event; |
| 679 | |
| 680 | virt_dev = ctrl->devs[slot_id]; |
| 681 | |
| 682 | /* |
| 683 | * This is the first Set Address since device plug-in |
| 684 | * so setting up the slot context. |
| 685 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 686 | debug("Setting up addressable devices %p\n", ctrl->dcbaa); |
Bin Meng | 1459ce6 | 2017-07-19 21:51:14 +0800 | [diff] [blame] | 687 | xhci_setup_addressable_virt_dev(ctrl, udev, root_portnr); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 688 | |
| 689 | ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx); |
| 690 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG); |
| 691 | ctrl_ctx->drop_flags = 0; |
| 692 | |
| 693 | xhci_queue_command(ctrl, (void *)ctrl_ctx, slot_id, 0, TRB_ADDR_DEV); |
| 694 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
| 695 | BUG_ON(TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)) != slot_id); |
| 696 | |
| 697 | switch (GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))) { |
| 698 | case COMP_CTX_STATE: |
| 699 | case COMP_EBADSLT: |
| 700 | printf("Setup ERROR: address device command for slot %d.\n", |
| 701 | slot_id); |
| 702 | ret = -EINVAL; |
| 703 | break; |
| 704 | case COMP_TX_ERR: |
| 705 | puts("Device not responding to set address.\n"); |
| 706 | ret = -EPROTO; |
| 707 | break; |
| 708 | case COMP_DEV_ERR: |
| 709 | puts("ERROR: Incompatible device" |
| 710 | "for address device command.\n"); |
| 711 | ret = -ENODEV; |
| 712 | break; |
| 713 | case COMP_SUCCESS: |
| 714 | debug("Successful Address Device command\n"); |
| 715 | udev->status = 0; |
| 716 | break; |
| 717 | default: |
| 718 | printf("ERROR: unexpected command completion code 0x%x.\n", |
| 719 | GET_COMP_CODE(le32_to_cpu(event->event_cmd.status))); |
| 720 | ret = -EINVAL; |
| 721 | break; |
| 722 | } |
| 723 | |
| 724 | xhci_acknowledge_event(ctrl); |
| 725 | |
| 726 | if (ret < 0) |
| 727 | /* |
| 728 | * TODO: Unsuccessful Address Device command shall leave the |
| 729 | * slot in default state. So, issue Disable Slot command now. |
| 730 | */ |
| 731 | return ret; |
| 732 | |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 733 | xhci_inval_cache((uintptr_t)virt_dev->out_ctx->bytes, |
| 734 | virt_dev->out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 735 | slot_ctx = xhci_get_slot_ctx(ctrl, virt_dev->out_ctx); |
| 736 | |
| 737 | debug("xHC internal address is: %d\n", |
| 738 | le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK); |
| 739 | |
| 740 | return 0; |
| 741 | } |
| 742 | |
| 743 | /** |
| 744 | * Issue Enable slot command to the controller to allocate |
| 745 | * device slot and assign the slot id. It fails if the xHC |
| 746 | * ran out of device slots, the Enable Slot command timed out, |
| 747 | * or allocating memory failed. |
| 748 | * |
| 749 | * @param udev pointer to the Device Data Structure |
| 750 | * @return Returns 0 on succes else return error code on failure |
| 751 | */ |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 752 | static int _xhci_alloc_device(struct usb_device *udev) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 753 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 754 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 755 | union xhci_trb *event; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 756 | int ret; |
| 757 | |
| 758 | /* |
| 759 | * Root hub will be first device to be initailized. |
| 760 | * If this device is root-hub, don't do any xHC related |
| 761 | * stuff. |
| 762 | */ |
| 763 | if (ctrl->rootdev == 0) { |
| 764 | udev->speed = USB_SPEED_SUPER; |
| 765 | return 0; |
| 766 | } |
| 767 | |
| 768 | xhci_queue_command(ctrl, NULL, 0, 0, TRB_ENABLE_SLOT); |
| 769 | event = xhci_wait_for_event(ctrl, TRB_COMPLETION); |
| 770 | BUG_ON(GET_COMP_CODE(le32_to_cpu(event->event_cmd.status)) |
| 771 | != COMP_SUCCESS); |
| 772 | |
| 773 | udev->slot_id = TRB_TO_SLOT_ID(le32_to_cpu(event->event_cmd.flags)); |
| 774 | |
| 775 | xhci_acknowledge_event(ctrl); |
| 776 | |
Simon Glass | 88a3784 | 2015-03-25 12:22:50 -0600 | [diff] [blame] | 777 | ret = xhci_alloc_virt_device(ctrl, udev->slot_id); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 778 | if (ret < 0) { |
| 779 | /* |
| 780 | * TODO: Unsuccessful Address Device command shall leave |
| 781 | * the slot in default. So, issue Disable Slot command now. |
| 782 | */ |
| 783 | puts("Could not allocate xHCI USB device data structures\n"); |
| 784 | return ret; |
| 785 | } |
| 786 | |
| 787 | return 0; |
| 788 | } |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 789 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 790 | #if !CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 791 | int usb_alloc_device(struct usb_device *udev) |
| 792 | { |
| 793 | return _xhci_alloc_device(udev); |
| 794 | } |
| 795 | #endif |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 796 | |
| 797 | /* |
| 798 | * Full speed devices may have a max packet size greater than 8 bytes, but the |
| 799 | * USB core doesn't know that until it reads the first 8 bytes of the |
| 800 | * descriptor. If the usb_device's max packet size changes after that point, |
| 801 | * we need to issue an evaluate context command and wait on it. |
| 802 | * |
| 803 | * @param udev pointer to the Device Data Structure |
| 804 | * @return returns the status of the xhci_configure_endpoints |
| 805 | */ |
| 806 | int xhci_check_maxpacket(struct usb_device *udev) |
| 807 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 808 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 809 | unsigned int slot_id = udev->slot_id; |
| 810 | int ep_index = 0; /* control endpoint */ |
| 811 | struct xhci_container_ctx *in_ctx; |
| 812 | struct xhci_container_ctx *out_ctx; |
| 813 | struct xhci_input_control_ctx *ctrl_ctx; |
| 814 | struct xhci_ep_ctx *ep_ctx; |
| 815 | int max_packet_size; |
| 816 | int hw_max_packet_size; |
| 817 | int ret = 0; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 818 | |
| 819 | out_ctx = ctrl->devs[slot_id]->out_ctx; |
Sergey Temerkhanov | 3859346 | 2015-04-01 17:18:45 +0300 | [diff] [blame] | 820 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 821 | |
| 822 | ep_ctx = xhci_get_ep_ctx(ctrl, out_ctx, ep_index); |
| 823 | 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] | 824 | max_packet_size = udev->epmaxpacketin[0]; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 825 | if (hw_max_packet_size != max_packet_size) { |
| 826 | debug("Max Packet Size for ep 0 changed.\n"); |
| 827 | debug("Max packet size in usb_device = %d\n", max_packet_size); |
| 828 | debug("Max packet size in xHCI HW = %d\n", hw_max_packet_size); |
| 829 | debug("Issuing evaluate context command.\n"); |
| 830 | |
| 831 | /* Set up the modified control endpoint 0 */ |
| 832 | xhci_endpoint_copy(ctrl, ctrl->devs[slot_id]->in_ctx, |
| 833 | ctrl->devs[slot_id]->out_ctx, ep_index); |
| 834 | in_ctx = ctrl->devs[slot_id]->in_ctx; |
| 835 | ep_ctx = xhci_get_ep_ctx(ctrl, in_ctx, ep_index); |
Bin Meng | 7c92b77 | 2017-09-18 06:40:44 -0700 | [diff] [blame] | 836 | ep_ctx->ep_info2 &= cpu_to_le32(~((0xffff & MAX_PACKET_MASK) |
| 837 | << MAX_PACKET_SHIFT)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 838 | ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size)); |
| 839 | |
| 840 | /* |
| 841 | * Set up the input context flags for the command |
| 842 | * FIXME: This won't work if a non-default control endpoint |
| 843 | * changes max packet sizes. |
| 844 | */ |
| 845 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
| 846 | ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG); |
| 847 | ctrl_ctx->drop_flags = 0; |
| 848 | |
| 849 | ret = xhci_configure_endpoints(udev, true); |
| 850 | } |
| 851 | return ret; |
| 852 | } |
| 853 | |
| 854 | /** |
| 855 | * Clears the Change bits of the Port Status Register |
| 856 | * |
| 857 | * @param wValue request value |
| 858 | * @param wIndex request index |
| 859 | * @param addr address of posrt status register |
| 860 | * @param port_status state of port status register |
| 861 | * @return none |
| 862 | */ |
| 863 | static void xhci_clear_port_change_bit(u16 wValue, |
| 864 | u16 wIndex, volatile uint32_t *addr, u32 port_status) |
| 865 | { |
| 866 | char *port_change_bit; |
| 867 | u32 status; |
| 868 | |
| 869 | switch (wValue) { |
| 870 | case USB_PORT_FEAT_C_RESET: |
| 871 | status = PORT_RC; |
| 872 | port_change_bit = "reset"; |
| 873 | break; |
| 874 | case USB_PORT_FEAT_C_CONNECTION: |
| 875 | status = PORT_CSC; |
| 876 | port_change_bit = "connect"; |
| 877 | break; |
| 878 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 879 | status = PORT_OCC; |
| 880 | port_change_bit = "over-current"; |
| 881 | break; |
| 882 | case USB_PORT_FEAT_C_ENABLE: |
| 883 | status = PORT_PEC; |
| 884 | port_change_bit = "enable/disable"; |
| 885 | break; |
| 886 | case USB_PORT_FEAT_C_SUSPEND: |
| 887 | status = PORT_PLC; |
| 888 | port_change_bit = "suspend/resume"; |
| 889 | break; |
| 890 | default: |
| 891 | /* Should never happen */ |
| 892 | return; |
| 893 | } |
| 894 | |
| 895 | /* Change bits are all write 1 to clear */ |
| 896 | xhci_writel(addr, port_status | status); |
| 897 | |
| 898 | port_status = xhci_readl(addr); |
| 899 | debug("clear port %s change, actual port %d status = 0x%x\n", |
| 900 | port_change_bit, wIndex, port_status); |
| 901 | } |
| 902 | |
| 903 | /** |
| 904 | * Save Read Only (RO) bits and save read/write bits where |
| 905 | * writing a 0 clears the bit and writing a 1 sets the bit (RWS). |
| 906 | * For all other types (RW1S, RW1CS, RW, and RZ), writing a '0' has no effect. |
| 907 | * |
| 908 | * @param state state of the Port Status and Control Regsiter |
| 909 | * @return a value that would result in the port being in the |
| 910 | * same state, if the value was written to the port |
| 911 | * status control register. |
| 912 | */ |
| 913 | static u32 xhci_port_state_to_neutral(u32 state) |
| 914 | { |
| 915 | /* Save read-only status and port state */ |
| 916 | return (state & XHCI_PORT_RO) | (state & XHCI_PORT_RWS); |
| 917 | } |
| 918 | |
| 919 | /** |
| 920 | * Submits the Requests to the XHCI Host Controller |
| 921 | * |
| 922 | * @param udev pointer to the USB device structure |
| 923 | * @param pipe contains the DIR_IN or OUT , devnum |
| 924 | * @param buffer buffer to be read/written based on the request |
| 925 | * @return returns 0 if successful else -1 on failure |
| 926 | */ |
| 927 | static int xhci_submit_root(struct usb_device *udev, unsigned long pipe, |
| 928 | void *buffer, struct devrequest *req) |
| 929 | { |
| 930 | uint8_t tmpbuf[4]; |
| 931 | u16 typeReq; |
| 932 | void *srcptr = NULL; |
| 933 | int len, srclen; |
| 934 | uint32_t reg; |
| 935 | volatile uint32_t *status_reg; |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 936 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 937 | struct xhci_hccr *hccr = ctrl->hccr; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 938 | struct xhci_hcor *hcor = ctrl->hcor; |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 939 | int max_ports = HCS_MAX_PORTS(xhci_readl(&hccr->cr_hcsparams1)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 940 | |
Jeroen Hofstee | b351e46 | 2014-06-12 00:31:27 +0200 | [diff] [blame] | 941 | if ((req->requesttype & USB_RT_PORT) && |
Bin Meng | 749de4c | 2017-07-19 21:50:03 +0800 | [diff] [blame] | 942 | le16_to_cpu(req->index) > max_ports) { |
| 943 | printf("The request port(%d) exceeds maximum port number\n", |
| 944 | le16_to_cpu(req->index) - 1); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 945 | return -EINVAL; |
| 946 | } |
| 947 | |
| 948 | status_reg = (volatile uint32_t *) |
| 949 | (&hcor->portregs[le16_to_cpu(req->index) - 1].or_portsc); |
| 950 | srclen = 0; |
| 951 | |
| 952 | typeReq = req->request | req->requesttype << 8; |
| 953 | |
| 954 | switch (typeReq) { |
| 955 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 956 | switch (le16_to_cpu(req->value) >> 8) { |
| 957 | case USB_DT_DEVICE: |
| 958 | debug("USB_DT_DEVICE request\n"); |
| 959 | srcptr = &descriptor.device; |
| 960 | srclen = 0x12; |
| 961 | break; |
| 962 | case USB_DT_CONFIG: |
| 963 | debug("USB_DT_CONFIG config\n"); |
| 964 | srcptr = &descriptor.config; |
| 965 | srclen = 0x19; |
| 966 | break; |
| 967 | case USB_DT_STRING: |
| 968 | debug("USB_DT_STRING config\n"); |
| 969 | switch (le16_to_cpu(req->value) & 0xff) { |
| 970 | case 0: /* Language */ |
| 971 | srcptr = "\4\3\11\4"; |
| 972 | srclen = 4; |
| 973 | break; |
| 974 | case 1: /* Vendor String */ |
Simon Glass | b113f6e | 2015-03-25 12:22:54 -0600 | [diff] [blame] | 975 | srcptr = "\16\3U\0-\0B\0o\0o\0t\0"; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 976 | srclen = 14; |
| 977 | break; |
| 978 | case 2: /* Product Name */ |
| 979 | srcptr = "\52\3X\0H\0C\0I\0 " |
| 980 | "\0H\0o\0s\0t\0 " |
| 981 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 982 | srclen = 42; |
| 983 | break; |
| 984 | default: |
| 985 | printf("unknown value DT_STRING %x\n", |
| 986 | le16_to_cpu(req->value)); |
| 987 | goto unknown; |
| 988 | } |
| 989 | break; |
| 990 | default: |
| 991 | printf("unknown value %x\n", le16_to_cpu(req->value)); |
| 992 | goto unknown; |
| 993 | } |
| 994 | break; |
| 995 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 996 | switch (le16_to_cpu(req->value) >> 8) { |
| 997 | case USB_DT_HUB: |
Bin Meng | e8930c4 | 2017-07-19 21:49:58 +0800 | [diff] [blame] | 998 | case USB_DT_SS_HUB: |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 999 | debug("USB_DT_HUB config\n"); |
| 1000 | srcptr = &descriptor.hub; |
| 1001 | srclen = 0x8; |
| 1002 | break; |
| 1003 | default: |
| 1004 | printf("unknown value %x\n", le16_to_cpu(req->value)); |
| 1005 | goto unknown; |
| 1006 | } |
| 1007 | break; |
| 1008 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
| 1009 | debug("USB_REQ_SET_ADDRESS\n"); |
| 1010 | ctrl->rootdev = le16_to_cpu(req->value); |
| 1011 | break; |
| 1012 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
| 1013 | /* Do nothing */ |
| 1014 | break; |
| 1015 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 1016 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 1017 | tmpbuf[1] = 0; |
| 1018 | srcptr = tmpbuf; |
| 1019 | srclen = 2; |
| 1020 | break; |
| 1021 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
| 1022 | memset(tmpbuf, 0, 4); |
| 1023 | reg = xhci_readl(status_reg); |
| 1024 | if (reg & PORT_CONNECT) { |
| 1025 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 1026 | switch (reg & DEV_SPEED_MASK) { |
| 1027 | case XDEV_FS: |
| 1028 | debug("SPEED = FULLSPEED\n"); |
| 1029 | break; |
| 1030 | case XDEV_LS: |
| 1031 | debug("SPEED = LOWSPEED\n"); |
| 1032 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 1033 | break; |
| 1034 | case XDEV_HS: |
| 1035 | debug("SPEED = HIGHSPEED\n"); |
| 1036 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 1037 | break; |
| 1038 | case XDEV_SS: |
| 1039 | debug("SPEED = SUPERSPEED\n"); |
| 1040 | tmpbuf[1] |= USB_PORT_STAT_SUPER_SPEED >> 8; |
| 1041 | break; |
| 1042 | } |
| 1043 | } |
| 1044 | if (reg & PORT_PE) |
| 1045 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 1046 | if ((reg & PORT_PLS_MASK) == XDEV_U3) |
| 1047 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 1048 | if (reg & PORT_OC) |
| 1049 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
| 1050 | if (reg & PORT_RESET) |
| 1051 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
| 1052 | if (reg & PORT_POWER) |
| 1053 | /* |
| 1054 | * XXX: This Port power bit (for USB 3.0 hub) |
| 1055 | * we are faking in USB 2.0 hub port status; |
| 1056 | * since there's a change in bit positions in |
| 1057 | * two: |
| 1058 | * USB 2.0 port status PP is at position[8] |
| 1059 | * USB 3.0 port status PP is at position[9] |
| 1060 | * So, we are still keeping it at position [8] |
| 1061 | */ |
| 1062 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
| 1063 | if (reg & PORT_CSC) |
| 1064 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 1065 | if (reg & PORT_PEC) |
| 1066 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 1067 | if (reg & PORT_OCC) |
| 1068 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
| 1069 | if (reg & PORT_RC) |
| 1070 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
| 1071 | |
| 1072 | srcptr = tmpbuf; |
| 1073 | srclen = 4; |
| 1074 | break; |
| 1075 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 1076 | reg = xhci_readl(status_reg); |
| 1077 | reg = xhci_port_state_to_neutral(reg); |
| 1078 | switch (le16_to_cpu(req->value)) { |
| 1079 | case USB_PORT_FEAT_ENABLE: |
| 1080 | reg |= PORT_PE; |
| 1081 | xhci_writel(status_reg, reg); |
| 1082 | break; |
| 1083 | case USB_PORT_FEAT_POWER: |
| 1084 | reg |= PORT_POWER; |
| 1085 | xhci_writel(status_reg, reg); |
| 1086 | break; |
| 1087 | case USB_PORT_FEAT_RESET: |
| 1088 | reg |= PORT_RESET; |
| 1089 | xhci_writel(status_reg, reg); |
| 1090 | break; |
| 1091 | default: |
| 1092 | printf("unknown feature %x\n", le16_to_cpu(req->value)); |
| 1093 | goto unknown; |
| 1094 | } |
| 1095 | break; |
| 1096 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 1097 | reg = xhci_readl(status_reg); |
| 1098 | reg = xhci_port_state_to_neutral(reg); |
| 1099 | switch (le16_to_cpu(req->value)) { |
| 1100 | case USB_PORT_FEAT_ENABLE: |
| 1101 | reg &= ~PORT_PE; |
| 1102 | break; |
| 1103 | case USB_PORT_FEAT_POWER: |
| 1104 | reg &= ~PORT_POWER; |
| 1105 | break; |
| 1106 | case USB_PORT_FEAT_C_RESET: |
| 1107 | case USB_PORT_FEAT_C_CONNECTION: |
| 1108 | case USB_PORT_FEAT_C_OVER_CURRENT: |
| 1109 | case USB_PORT_FEAT_C_ENABLE: |
| 1110 | xhci_clear_port_change_bit((le16_to_cpu(req->value)), |
| 1111 | le16_to_cpu(req->index), |
| 1112 | status_reg, reg); |
| 1113 | break; |
| 1114 | default: |
| 1115 | printf("unknown feature %x\n", le16_to_cpu(req->value)); |
| 1116 | goto unknown; |
| 1117 | } |
| 1118 | xhci_writel(status_reg, reg); |
| 1119 | break; |
| 1120 | default: |
| 1121 | puts("Unknown request\n"); |
| 1122 | goto unknown; |
| 1123 | } |
| 1124 | |
| 1125 | debug("scrlen = %d\n req->length = %d\n", |
| 1126 | srclen, le16_to_cpu(req->length)); |
| 1127 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 1128 | len = min(srclen, (int)le16_to_cpu(req->length)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1129 | |
| 1130 | if (srcptr != NULL && len > 0) |
| 1131 | memcpy(buffer, srcptr, len); |
| 1132 | else |
| 1133 | debug("Len is 0\n"); |
| 1134 | |
| 1135 | udev->act_len = len; |
| 1136 | udev->status = 0; |
| 1137 | |
| 1138 | return 0; |
| 1139 | |
| 1140 | unknown: |
| 1141 | udev->act_len = 0; |
| 1142 | udev->status = USB_ST_STALLED; |
| 1143 | |
| 1144 | return -ENODEV; |
| 1145 | } |
| 1146 | |
| 1147 | /** |
| 1148 | * Submits the INT request to XHCI Host cotroller |
| 1149 | * |
| 1150 | * @param udev pointer to the USB device |
| 1151 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1152 | * @param buffer buffer to be read/written based on the request |
| 1153 | * @param length length of the buffer |
| 1154 | * @param interval interval of the interrupt |
| 1155 | * @return 0 |
| 1156 | */ |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1157 | 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] | 1158 | void *buffer, int length, int interval, |
| 1159 | bool nonblock) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1160 | { |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1161 | if (usb_pipetype(pipe) != PIPE_INTERRUPT) { |
| 1162 | printf("non-interrupt pipe (type=%lu)", usb_pipetype(pipe)); |
| 1163 | return -EINVAL; |
| 1164 | } |
| 1165 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1166 | /* |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1167 | * xHCI uses normal TRBs for both bulk and interrupt. When the |
| 1168 | * interrupt endpoint is to be serviced, the xHC will consume |
| 1169 | * (at most) one TD. A TD (comprised of sg list entries) can |
| 1170 | * take several service intervals to transmit. |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1171 | */ |
Bin Meng | 2bc748c | 2017-09-18 06:40:41 -0700 | [diff] [blame] | 1172 | return xhci_bulk_tx(udev, pipe, length, buffer); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1173 | } |
| 1174 | |
| 1175 | /** |
| 1176 | * submit the BULK type of request to the USB Device |
| 1177 | * |
| 1178 | * @param udev pointer to the USB device |
| 1179 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1180 | * @param buffer buffer to be read/written based on the request |
| 1181 | * @param length length of the buffer |
| 1182 | * @return returns 0 if successful else -1 on failure |
| 1183 | */ |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1184 | static int _xhci_submit_bulk_msg(struct usb_device *udev, unsigned long pipe, |
| 1185 | void *buffer, int length) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1186 | { |
| 1187 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 1188 | printf("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 1189 | return -EINVAL; |
| 1190 | } |
| 1191 | |
| 1192 | return xhci_bulk_tx(udev, pipe, length, buffer); |
| 1193 | } |
| 1194 | |
| 1195 | /** |
| 1196 | * submit the control type of request to the Root hub/Device based on the devnum |
| 1197 | * |
| 1198 | * @param udev pointer to the USB device |
| 1199 | * @param pipe contains the DIR_IN or OUT , devnum |
| 1200 | * @param buffer buffer to be read/written based on the request |
| 1201 | * @param length length of the buffer |
| 1202 | * @param setup Request type |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1203 | * @param root_portnr Root port number that this device is on |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1204 | * @return returns 0 if successful else -1 on failure |
| 1205 | */ |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1206 | static int _xhci_submit_control_msg(struct usb_device *udev, unsigned long pipe, |
| 1207 | void *buffer, int length, |
| 1208 | struct devrequest *setup, int root_portnr) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1209 | { |
Simon Glass | a49e27b | 2015-03-25 12:22:49 -0600 | [diff] [blame] | 1210 | struct xhci_ctrl *ctrl = xhci_get_ctrl(udev); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1211 | int ret = 0; |
| 1212 | |
| 1213 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 1214 | printf("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 1215 | return -EINVAL; |
| 1216 | } |
| 1217 | |
| 1218 | if (usb_pipedevice(pipe) == ctrl->rootdev) |
| 1219 | return xhci_submit_root(udev, pipe, buffer, setup); |
| 1220 | |
Ted Chen | a2f4f9a | 2016-03-18 17:56:52 +1030 | [diff] [blame] | 1221 | if (setup->request == USB_REQ_SET_ADDRESS && |
| 1222 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1223 | return xhci_address_device(udev, root_portnr); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1224 | |
Ted Chen | a2f4f9a | 2016-03-18 17:56:52 +1030 | [diff] [blame] | 1225 | if (setup->request == USB_REQ_SET_CONFIGURATION && |
| 1226 | (setup->requesttype & USB_TYPE_MASK) == USB_TYPE_STANDARD) { |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1227 | ret = xhci_set_configuration(udev); |
| 1228 | if (ret) { |
| 1229 | puts("Failed to configure xHCI endpoint\n"); |
| 1230 | return ret; |
| 1231 | } |
| 1232 | } |
| 1233 | |
| 1234 | return xhci_ctrl_tx(udev, pipe, setup, length, buffer); |
| 1235 | } |
| 1236 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1237 | static int xhci_lowlevel_init(struct xhci_ctrl *ctrl) |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1238 | { |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1239 | struct xhci_hccr *hccr; |
| 1240 | struct xhci_hcor *hcor; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1241 | uint32_t val; |
| 1242 | uint32_t val2; |
| 1243 | uint32_t 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 | hccr = ctrl->hccr; |
| 1246 | hcor = ctrl->hcor; |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1247 | /* |
| 1248 | * Program the Number of Device Slots Enabled field in the CONFIG |
| 1249 | * register with the max value of slots the HC can handle. |
| 1250 | */ |
| 1251 | val = (xhci_readl(&hccr->cr_hcsparams1) & HCS_SLOTS_MASK); |
| 1252 | val2 = xhci_readl(&hcor->or_config); |
| 1253 | val |= (val2 & ~HCS_SLOTS_MASK); |
| 1254 | xhci_writel(&hcor->or_config, val); |
| 1255 | |
| 1256 | /* initializing xhci data structures */ |
| 1257 | if (xhci_mem_init(ctrl, hccr, hcor) < 0) |
| 1258 | return -ENOMEM; |
| 1259 | |
| 1260 | reg = xhci_readl(&hccr->cr_hcsparams1); |
| 1261 | descriptor.hub.bNbrPorts = ((reg & HCS_MAX_PORTS_MASK) >> |
| 1262 | HCS_MAX_PORTS_SHIFT); |
| 1263 | printf("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); |
| 1264 | |
| 1265 | /* Port Indicators */ |
| 1266 | reg = xhci_readl(&hccr->cr_hccparams); |
| 1267 | if (HCS_INDICATOR(reg)) |
| 1268 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1269 | | 0x80, &descriptor.hub.wHubCharacteristics); |
| 1270 | |
| 1271 | /* Port Power Control */ |
| 1272 | if (HCC_PPC(reg)) |
| 1273 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1274 | | 0x01, &descriptor.hub.wHubCharacteristics); |
| 1275 | |
| 1276 | if (xhci_start(hcor)) { |
| 1277 | xhci_reset(hcor); |
| 1278 | return -ENODEV; |
| 1279 | } |
| 1280 | |
| 1281 | /* Zero'ing IRQ control register and IRQ pending register */ |
| 1282 | xhci_writel(&ctrl->ir_set->irq_control, 0x0); |
| 1283 | xhci_writel(&ctrl->ir_set->irq_pending, 0x0); |
| 1284 | |
| 1285 | reg = HC_VERSION(xhci_readl(&hccr->cr_capbase)); |
| 1286 | printf("USB XHCI %x.%02x\n", reg >> 8, reg & 0xff); |
| 1287 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | static int xhci_lowlevel_stop(struct xhci_ctrl *ctrl) |
| 1292 | { |
| 1293 | u32 temp; |
| 1294 | |
| 1295 | xhci_reset(ctrl->hcor); |
| 1296 | |
| 1297 | debug("// Disabling event ring interrupts\n"); |
| 1298 | temp = xhci_readl(&ctrl->hcor->or_usbsts); |
| 1299 | xhci_writel(&ctrl->hcor->or_usbsts, temp & ~STS_EINT); |
| 1300 | temp = xhci_readl(&ctrl->ir_set->irq_pending); |
| 1301 | xhci_writel(&ctrl->ir_set->irq_pending, ER_IRQ_DISABLE(temp)); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1302 | |
| 1303 | return 0; |
| 1304 | } |
| 1305 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1306 | #if !CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 4ec422c | 2015-03-25 12:22:51 -0600 | [diff] [blame] | 1307 | int submit_control_msg(struct usb_device *udev, unsigned long pipe, |
| 1308 | void *buffer, int length, struct devrequest *setup) |
| 1309 | { |
| 1310 | struct usb_device *hop = udev; |
| 1311 | |
| 1312 | if (hop->parent) |
| 1313 | while (hop->parent->parent) |
| 1314 | hop = hop->parent; |
| 1315 | |
| 1316 | return _xhci_submit_control_msg(udev, pipe, buffer, length, setup, |
| 1317 | hop->portnr); |
| 1318 | } |
| 1319 | |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1320 | int submit_bulk_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
| 1321 | int length) |
| 1322 | { |
| 1323 | return _xhci_submit_bulk_msg(udev, pipe, buffer, length); |
| 1324 | } |
| 1325 | |
| 1326 | int submit_int_msg(struct usb_device *udev, unsigned long pipe, void *buffer, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1327 | int length, int interval, bool nonblock) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1328 | { |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1329 | return _xhci_submit_int_msg(udev, pipe, buffer, length, interval, |
| 1330 | nonblock); |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1331 | } |
| 1332 | |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1333 | /** |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1334 | * Intialises the XHCI host controller |
| 1335 | * and allocates the necessary data structures |
| 1336 | * |
| 1337 | * @param index index to the host controller data structure |
| 1338 | * @return pointer to the intialised controller |
| 1339 | */ |
| 1340 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
| 1341 | { |
| 1342 | struct xhci_hccr *hccr; |
| 1343 | struct xhci_hcor *hcor; |
| 1344 | struct xhci_ctrl *ctrl; |
| 1345 | int ret; |
| 1346 | |
Sergey Temerkhanov | 65bb454 | 2015-08-17 15:38:07 +0300 | [diff] [blame] | 1347 | *controller = NULL; |
| 1348 | |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1349 | if (xhci_hcd_init(index, &hccr, (struct xhci_hcor **)&hcor) != 0) |
| 1350 | return -ENODEV; |
| 1351 | |
| 1352 | if (xhci_reset(hcor) != 0) |
| 1353 | return -ENODEV; |
| 1354 | |
| 1355 | ctrl = &xhcic[index]; |
| 1356 | |
| 1357 | ctrl->hccr = hccr; |
| 1358 | ctrl->hcor = hcor; |
| 1359 | |
| 1360 | ret = xhci_lowlevel_init(ctrl); |
| 1361 | |
Sergey Temerkhanov | 65bb454 | 2015-08-17 15:38:07 +0300 | [diff] [blame] | 1362 | if (ret) { |
| 1363 | ctrl->hccr = NULL; |
| 1364 | ctrl->hcor = NULL; |
| 1365 | } else { |
| 1366 | *controller = &xhcic[index]; |
| 1367 | } |
Simon Glass | 686a812 | 2015-03-25 12:22:52 -0600 | [diff] [blame] | 1368 | |
| 1369 | return ret; |
| 1370 | } |
| 1371 | |
| 1372 | /** |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1373 | * Stops the XHCI host controller |
| 1374 | * and cleans up all the related data structures |
| 1375 | * |
| 1376 | * @param index index to the host controller data structure |
| 1377 | * @return none |
| 1378 | */ |
| 1379 | int usb_lowlevel_stop(int index) |
| 1380 | { |
| 1381 | struct xhci_ctrl *ctrl = (xhcic + index); |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1382 | |
Sergey Temerkhanov | 65bb454 | 2015-08-17 15:38:07 +0300 | [diff] [blame] | 1383 | if (ctrl->hcor) { |
| 1384 | xhci_lowlevel_stop(ctrl); |
| 1385 | xhci_hcd_stop(index); |
| 1386 | xhci_cleanup(ctrl); |
| 1387 | } |
Vivek Gautam | 4912dcc | 2013-09-14 14:02:45 +0530 | [diff] [blame] | 1388 | |
| 1389 | return 0; |
| 1390 | } |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1391 | #endif /* CONFIG_IS_ENABLED(DM_USB) */ |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1392 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1393 | #if CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1394 | |
| 1395 | static int xhci_submit_control_msg(struct udevice *dev, struct usb_device *udev, |
| 1396 | unsigned long pipe, void *buffer, int length, |
| 1397 | struct devrequest *setup) |
| 1398 | { |
| 1399 | struct usb_device *uhop; |
| 1400 | struct udevice *hub; |
| 1401 | int root_portnr = 0; |
| 1402 | |
| 1403 | debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__, |
| 1404 | dev->name, udev, udev->dev->name, udev->portnr); |
| 1405 | hub = udev->dev; |
| 1406 | if (device_get_uclass_id(hub) == UCLASS_USB_HUB) { |
| 1407 | /* Figure out our port number on the root hub */ |
Bin Meng | 5ecfd5d | 2017-07-19 21:51:11 +0800 | [diff] [blame] | 1408 | if (usb_hub_is_root_hub(hub)) { |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1409 | root_portnr = udev->portnr; |
| 1410 | } else { |
Bin Meng | 5ecfd5d | 2017-07-19 21:51:11 +0800 | [diff] [blame] | 1411 | while (!usb_hub_is_root_hub(hub->parent)) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1412 | hub = hub->parent; |
Simon Glass | de44acf | 2015-09-28 23:32:01 -0600 | [diff] [blame] | 1413 | uhop = dev_get_parent_priv(hub); |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1414 | root_portnr = uhop->portnr; |
| 1415 | } |
| 1416 | } |
| 1417 | /* |
| 1418 | struct usb_device *hop = udev; |
| 1419 | |
| 1420 | if (hop->parent) |
| 1421 | while (hop->parent->parent) |
| 1422 | hop = hop->parent; |
| 1423 | */ |
| 1424 | return _xhci_submit_control_msg(udev, pipe, buffer, length, setup, |
| 1425 | root_portnr); |
| 1426 | } |
| 1427 | |
| 1428 | static int xhci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, |
| 1429 | unsigned long pipe, void *buffer, int length) |
| 1430 | { |
| 1431 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1432 | return _xhci_submit_bulk_msg(udev, pipe, buffer, length); |
| 1433 | } |
| 1434 | |
| 1435 | static int xhci_submit_int_msg(struct udevice *dev, struct usb_device *udev, |
| 1436 | unsigned long pipe, void *buffer, int length, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1437 | int interval, bool nonblock) |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1438 | { |
| 1439 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1440 | return _xhci_submit_int_msg(udev, pipe, buffer, length, interval, |
| 1441 | nonblock); |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1442 | } |
| 1443 | |
| 1444 | static int xhci_alloc_device(struct udevice *dev, struct usb_device *udev) |
| 1445 | { |
| 1446 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1447 | return _xhci_alloc_device(udev); |
| 1448 | } |
| 1449 | |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1450 | static int xhci_update_hub_device(struct udevice *dev, struct usb_device *udev) |
| 1451 | { |
| 1452 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1453 | struct usb_hub_device *hub = dev_get_uclass_priv(udev->dev); |
| 1454 | struct xhci_virt_device *virt_dev; |
| 1455 | struct xhci_input_control_ctx *ctrl_ctx; |
| 1456 | struct xhci_container_ctx *out_ctx; |
| 1457 | struct xhci_container_ctx *in_ctx; |
| 1458 | struct xhci_slot_ctx *slot_ctx; |
| 1459 | int slot_id = udev->slot_id; |
| 1460 | unsigned think_time; |
| 1461 | |
| 1462 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1463 | |
| 1464 | /* Ignore root hubs */ |
| 1465 | if (usb_hub_is_root_hub(udev->dev)) |
| 1466 | return 0; |
| 1467 | |
| 1468 | virt_dev = ctrl->devs[slot_id]; |
| 1469 | BUG_ON(!virt_dev); |
| 1470 | |
| 1471 | out_ctx = virt_dev->out_ctx; |
| 1472 | in_ctx = virt_dev->in_ctx; |
| 1473 | |
| 1474 | ctrl_ctx = xhci_get_input_control_ctx(in_ctx); |
| 1475 | /* Initialize the input context control */ |
Bin Meng | 03760fe | 2018-05-23 23:40:47 -0700 | [diff] [blame] | 1476 | ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG); |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1477 | ctrl_ctx->drop_flags = 0; |
| 1478 | |
| 1479 | xhci_inval_cache((uintptr_t)out_ctx->bytes, out_ctx->size); |
| 1480 | |
| 1481 | /* slot context */ |
| 1482 | xhci_slot_copy(ctrl, in_ctx, out_ctx); |
| 1483 | slot_ctx = xhci_get_slot_ctx(ctrl, in_ctx); |
| 1484 | |
| 1485 | /* Update hub related fields */ |
| 1486 | slot_ctx->dev_info |= cpu_to_le32(DEV_HUB); |
Bin Meng | 18f5bcd | 2018-05-23 23:40:49 -0700 | [diff] [blame] | 1487 | /* |
| 1488 | * refer to section 6.2.2: MTT should be 0 for full speed hub, |
| 1489 | * but it may be already set to 1 when setup an xHCI virtual |
| 1490 | * device, so clear it anyway. |
| 1491 | */ |
| 1492 | if (hub->tt.multi) |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1493 | slot_ctx->dev_info |= cpu_to_le32(DEV_MTT); |
Bin Meng | 18f5bcd | 2018-05-23 23:40:49 -0700 | [diff] [blame] | 1494 | else if (udev->speed == USB_SPEED_FULL) |
| 1495 | slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT); |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1496 | slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(udev->maxchild)); |
| 1497 | /* |
| 1498 | * Set TT think time - convert from ns to FS bit times. |
| 1499 | * Note 8 FS bit times == (8 bits / 12000000 bps) ~= 666ns |
| 1500 | * |
| 1501 | * 0 = 8 FS bit times, 1 = 16 FS bit times, |
| 1502 | * 2 = 24 FS bit times, 3 = 32 FS bit times. |
| 1503 | * |
| 1504 | * This field shall be 0 if the device is not a high-spped hub. |
| 1505 | */ |
| 1506 | think_time = hub->tt.think_time; |
| 1507 | if (think_time != 0) |
| 1508 | think_time = (think_time / 666) - 1; |
| 1509 | if (udev->speed == USB_SPEED_HIGH) |
| 1510 | slot_ctx->tt_info |= cpu_to_le32(TT_THINK_TIME(think_time)); |
Bin Meng | d038398 | 2018-05-23 23:40:48 -0700 | [diff] [blame] | 1511 | slot_ctx->dev_state = 0; |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1512 | |
| 1513 | return xhci_configure_endpoints(udev, false); |
| 1514 | } |
| 1515 | |
Bin Meng | 1bc4ce9 | 2017-09-07 06:13:18 -0700 | [diff] [blame] | 1516 | static int xhci_get_max_xfer_size(struct udevice *dev, size_t *size) |
| 1517 | { |
| 1518 | /* |
| 1519 | * xHCD allocates one segment which includes 64 TRBs for each endpoint |
| 1520 | * and the last TRB in this segment is configured as a link TRB to form |
| 1521 | * a TRB ring. Each TRB can transfer up to 64K bytes, however data |
| 1522 | * buffers referenced by transfer TRBs shall not span 64KB boundaries. |
| 1523 | * Hence the maximum number of TRBs we can use in one transfer is 62. |
| 1524 | */ |
| 1525 | *size = (TRBS_PER_SEGMENT - 2) * TRB_MAX_BUFF_SIZE; |
| 1526 | |
| 1527 | return 0; |
| 1528 | } |
| 1529 | |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1530 | int xhci_register(struct udevice *dev, struct xhci_hccr *hccr, |
| 1531 | struct xhci_hcor *hcor) |
| 1532 | { |
| 1533 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1534 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
| 1535 | int ret; |
| 1536 | |
| 1537 | debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p\n", __func__, dev->name, |
| 1538 | ctrl, hccr, hcor); |
| 1539 | |
| 1540 | ctrl->dev = dev; |
| 1541 | |
Nicolas Saenz Julienne | 566107e | 2020-06-29 18:37:25 +0200 | [diff] [blame^] | 1542 | ret = xhci_reset_hw(ctrl); |
| 1543 | if (ret) |
| 1544 | goto err; |
| 1545 | |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1546 | /* |
| 1547 | * XHCI needs to issue a Address device command to setup |
| 1548 | * proper device context structures, before it can interact |
| 1549 | * with the device. So a get_descriptor will fail before any |
| 1550 | * of that is done for XHCI unlike EHCI. |
| 1551 | */ |
| 1552 | priv->desc_before_addr = false; |
| 1553 | |
| 1554 | ret = xhci_reset(hcor); |
| 1555 | if (ret) |
| 1556 | goto err; |
| 1557 | |
| 1558 | ctrl->hccr = hccr; |
| 1559 | ctrl->hcor = hcor; |
| 1560 | ret = xhci_lowlevel_init(ctrl); |
| 1561 | if (ret) |
| 1562 | goto err; |
| 1563 | |
| 1564 | return 0; |
| 1565 | err: |
| 1566 | free(ctrl); |
| 1567 | debug("%s: failed, ret=%d\n", __func__, ret); |
| 1568 | return ret; |
| 1569 | } |
| 1570 | |
| 1571 | int xhci_deregister(struct udevice *dev) |
| 1572 | { |
| 1573 | struct xhci_ctrl *ctrl = dev_get_priv(dev); |
| 1574 | |
| 1575 | xhci_lowlevel_stop(ctrl); |
| 1576 | xhci_cleanup(ctrl); |
| 1577 | |
| 1578 | return 0; |
| 1579 | } |
| 1580 | |
| 1581 | struct dm_usb_ops xhci_usb_ops = { |
| 1582 | .control = xhci_submit_control_msg, |
| 1583 | .bulk = xhci_submit_bulk_msg, |
| 1584 | .interrupt = xhci_submit_int_msg, |
| 1585 | .alloc_device = xhci_alloc_device, |
Bin Meng | 2b6f4c5 | 2017-07-19 21:51:19 +0800 | [diff] [blame] | 1586 | .update_hub_device = xhci_update_hub_device, |
Bin Meng | 1bc4ce9 | 2017-09-07 06:13:18 -0700 | [diff] [blame] | 1587 | .get_max_xfer_size = xhci_get_max_xfer_size, |
Simon Glass | 49b4183 | 2015-03-25 12:22:53 -0600 | [diff] [blame] | 1588 | }; |
| 1589 | |
| 1590 | #endif |