Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 2 | /*- |
| 3 | * Copyright (c) 2007-2008, Juniper Networks, Inc. |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 2008, Excito Elektronik i Skåne AB |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 5 | * Copyright (c) 2008, Michael Trimarchi <trimarchimichael@yahoo.it> |
| 6 | * |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 7 | * All rights reserved. |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 8 | */ |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 9 | #include <common.h> |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 10 | #include <cpu_func.h> |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 11 | #include <dm.h> |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 12 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 13 | #include <log.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 14 | #include <asm/byteorder.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <asm/cache.h> |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 16 | #include <asm/unaligned.h> |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 17 | #include <usb.h> |
| 18 | #include <asm/io.h> |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 19 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
Stefan Roese | 86b34cf | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 21 | #include <watchdog.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 22 | #include <dm/device_compat.h> |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 23 | #include <linux/compiler.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 24 | #include <linux/delay.h> |
Jean-Christophe PLAGNIOL-VILLARD | 8f6bcf4 | 2009-04-03 12:46:58 +0200 | [diff] [blame] | 25 | |
| 26 | #include "ehci.h" |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 27 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 28 | #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT |
| 29 | #define CONFIG_USB_MAX_CONTROLLER_COUNT 1 |
| 30 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 31 | |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 32 | /* |
| 33 | * EHCI spec page 20 says that the HC may take up to 16 uFrames (= 4ms) to halt. |
| 34 | * Let's time out after 8 to have a little safety margin on top of that. |
| 35 | */ |
| 36 | #define HCHALT_TIMEOUT (8 * 1000) |
| 37 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 38 | #if !CONFIG_IS_ENABLED(DM_USB) |
Marek Vasut | fd349a1 | 2013-07-10 03:16:31 +0200 | [diff] [blame] | 39 | static struct ehci_ctrl ehcic[CONFIG_USB_MAX_CONTROLLER_COUNT]; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 40 | #endif |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 41 | |
| 42 | #define ALIGN_END_ADDR(type, ptr, size) \ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 43 | ((unsigned long)(ptr) + roundup((size) * sizeof(type), USB_DMA_MINALIGN)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 44 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 45 | static struct descriptor { |
| 46 | struct usb_hub_descriptor hub; |
| 47 | struct usb_device_descriptor device; |
| 48 | struct usb_linux_config_descriptor config; |
| 49 | struct usb_linux_interface_descriptor interface; |
| 50 | struct usb_endpoint_descriptor endpoint; |
| 51 | } __attribute__ ((packed)) descriptor = { |
| 52 | { |
| 53 | 0x8, /* bDescLength */ |
| 54 | 0x29, /* bDescriptorType: hub descriptor */ |
| 55 | 2, /* bNrPorts -- runtime modified */ |
| 56 | 0, /* wHubCharacteristics */ |
Vincent Palatin | 8277b50 | 2011-12-05 14:52:22 -0800 | [diff] [blame] | 57 | 10, /* bPwrOn2PwrGood */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 58 | 0, /* bHubCntrCurrent */ |
Bin Meng | 0d66b3a | 2017-07-19 21:50:00 +0800 | [diff] [blame] | 59 | { /* Device removable */ |
| 60 | } /* at most 7 ports! XXX */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 61 | }, |
| 62 | { |
| 63 | 0x12, /* bLength */ |
| 64 | 1, /* bDescriptorType: UDESC_DEVICE */ |
Sergei Shtylyov | fa30a27 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 65 | cpu_to_le16(0x0200), /* bcdUSB: v2.0 */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 66 | 9, /* bDeviceClass: UDCLASS_HUB */ |
| 67 | 0, /* bDeviceSubClass: UDSUBCLASS_HUB */ |
| 68 | 1, /* bDeviceProtocol: UDPROTO_HSHUBSTT */ |
| 69 | 64, /* bMaxPacketSize: 64 bytes */ |
| 70 | 0x0000, /* idVendor */ |
| 71 | 0x0000, /* idProduct */ |
Sergei Shtylyov | fa30a27 | 2010-02-27 21:29:42 +0300 | [diff] [blame] | 72 | cpu_to_le16(0x0100), /* bcdDevice */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 73 | 1, /* iManufacturer */ |
| 74 | 2, /* iProduct */ |
| 75 | 0, /* iSerialNumber */ |
| 76 | 1 /* bNumConfigurations: 1 */ |
| 77 | }, |
| 78 | { |
| 79 | 0x9, |
| 80 | 2, /* bDescriptorType: UDESC_CONFIG */ |
| 81 | cpu_to_le16(0x19), |
| 82 | 1, /* bNumInterface */ |
| 83 | 1, /* bConfigurationValue */ |
| 84 | 0, /* iConfiguration */ |
| 85 | 0x40, /* bmAttributes: UC_SELF_POWER */ |
| 86 | 0 /* bMaxPower */ |
| 87 | }, |
| 88 | { |
| 89 | 0x9, /* bLength */ |
| 90 | 4, /* bDescriptorType: UDESC_INTERFACE */ |
| 91 | 0, /* bInterfaceNumber */ |
| 92 | 0, /* bAlternateSetting */ |
| 93 | 1, /* bNumEndpoints */ |
| 94 | 9, /* bInterfaceClass: UICLASS_HUB */ |
| 95 | 0, /* bInterfaceSubClass: UISUBCLASS_HUB */ |
| 96 | 0, /* bInterfaceProtocol: UIPROTO_HSHUBSTT */ |
| 97 | 0 /* iInterface */ |
| 98 | }, |
| 99 | { |
| 100 | 0x7, /* bLength */ |
| 101 | 5, /* bDescriptorType: UDESC_ENDPOINT */ |
| 102 | 0x81, /* bEndpointAddress: |
| 103 | * UE_DIR_IN | EHCI_INTR_ENDPT |
| 104 | */ |
| 105 | 3, /* bmAttributes: UE_INTERRUPT */ |
Tom Rix | 83b9e1d | 2009-10-31 12:37:38 -0500 | [diff] [blame] | 106 | 8, /* wMaxPacketSize */ |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 107 | 255 /* bInterval */ |
| 108 | }, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 109 | }; |
| 110 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 111 | #if defined(CONFIG_EHCI_IS_TDI) |
| 112 | #define ehci_is_TDI() (1) |
| 113 | #else |
| 114 | #define ehci_is_TDI() (0) |
| 115 | #endif |
| 116 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 117 | static struct ehci_ctrl *ehci_get_ctrl(struct usb_device *udev) |
| 118 | { |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 119 | #if CONFIG_IS_ENABLED(DM_USB) |
Hans de Goede | 6be39d1 | 2015-05-05 11:54:33 +0200 | [diff] [blame] | 120 | return dev_get_priv(usb_get_bus(udev->dev)); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 121 | #else |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 122 | return udev->controller; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 123 | #endif |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 124 | } |
| 125 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 126 | static int ehci_get_port_speed(struct ehci_ctrl *ctrl, uint32_t reg) |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 127 | { |
| 128 | return PORTSC_PSPD(reg); |
| 129 | } |
| 130 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 131 | static void ehci_set_usbmode(struct ehci_ctrl *ctrl) |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 132 | { |
| 133 | uint32_t tmp; |
| 134 | uint32_t *reg_ptr; |
| 135 | |
Simon Glass | 2d387ab | 2015-03-25 12:22:23 -0600 | [diff] [blame] | 136 | reg_ptr = (uint32_t *)((u8 *)&ctrl->hcor->or_usbcmd + USBMODE); |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 137 | tmp = ehci_readl(reg_ptr); |
| 138 | tmp |= USBMODE_CM_HC; |
| 139 | #if defined(CONFIG_EHCI_MMIO_BIG_ENDIAN) |
| 140 | tmp |= USBMODE_BE; |
Marek Vasut | d9fa048 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 141 | #else |
| 142 | tmp &= ~USBMODE_BE; |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 143 | #endif |
| 144 | ehci_writel(reg_ptr, tmp); |
| 145 | } |
| 146 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 147 | static void ehci_powerup_fixup(struct ehci_ctrl *ctrl, uint32_t *status_reg, |
Simon Glass | cc0dc6b | 2015-03-25 12:22:21 -0600 | [diff] [blame] | 148 | uint32_t *reg) |
Marek Vasut | 0973477 | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 149 | { |
| 150 | mdelay(50); |
| 151 | } |
| 152 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 153 | static uint32_t *ehci_get_portsc_register(struct ehci_ctrl *ctrl, int port) |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 154 | { |
Bin Meng | c6336ee | 2017-07-19 21:50:05 +0800 | [diff] [blame] | 155 | int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams)); |
| 156 | |
| 157 | if (port < 0 || port >= max_ports) { |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 158 | /* Printing the message would cause a scan failure! */ |
Bin Meng | c6336ee | 2017-07-19 21:50:05 +0800 | [diff] [blame] | 159 | debug("The request port(%u) exceeds maximum port number\n", |
| 160 | port); |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 161 | return NULL; |
| 162 | } |
| 163 | |
Simon Glass | dfbf186 | 2015-03-25 12:22:24 -0600 | [diff] [blame] | 164 | return (uint32_t *)&ctrl->hcor->or_portsc[port]; |
Simon Glass | 0bec128 | 2015-03-25 12:22:17 -0600 | [diff] [blame] | 165 | } |
| 166 | |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 167 | static int handshake(uint32_t *ptr, uint32_t mask, uint32_t done, int usec) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 168 | { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 169 | uint32_t result; |
| 170 | do { |
| 171 | result = ehci_readl(ptr); |
Wolfgang Denk | cdc5a7a | 2010-10-22 14:23:00 +0200 | [diff] [blame] | 172 | udelay(5); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 173 | if (result == ~(uint32_t)0) |
| 174 | return -1; |
| 175 | result &= mask; |
| 176 | if (result == done) |
| 177 | return 0; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 178 | usec--; |
| 179 | } while (usec > 0); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 180 | return -1; |
| 181 | } |
| 182 | |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 183 | static int ehci_reset(struct ehci_ctrl *ctrl) |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 184 | { |
| 185 | uint32_t cmd; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 186 | int ret = 0; |
| 187 | |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 188 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Stefan Roese | 745af44 | 2010-11-26 15:44:00 +0100 | [diff] [blame] | 189 | cmd = (cmd & ~CMD_RUN) | CMD_RESET; |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 190 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 191 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbcmd, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 192 | CMD_RESET, 0, 250 * 1000); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 193 | if (ret < 0) { |
| 194 | printf("EHCI fail to reset\n"); |
| 195 | goto out; |
| 196 | } |
| 197 | |
Jim Lin | 54f3dfe | 2013-03-27 00:52:32 +0000 | [diff] [blame] | 198 | if (ehci_is_TDI()) |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 199 | ctrl->ops.set_usb_mode(ctrl); |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 200 | |
| 201 | #ifdef CONFIG_USB_EHCI_TXFIFO_THRESH |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 202 | cmd = ehci_readl(&ctrl->hcor->or_txfilltuning); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 203 | cmd &= ~TXFIFO_THRESH_MASK; |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 204 | cmd |= TXFIFO_THRESH(CONFIG_USB_EHCI_TXFIFO_THRESH); |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 205 | ehci_writel(&ctrl->hcor->or_txfilltuning, cmd); |
Simon Glass | 5978cdb | 2012-02-27 10:52:47 +0000 | [diff] [blame] | 206 | #endif |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 207 | out: |
| 208 | return ret; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 209 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 210 | |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 211 | static int ehci_shutdown(struct ehci_ctrl *ctrl) |
| 212 | { |
| 213 | int i, ret = 0; |
| 214 | uint32_t cmd, reg; |
Bin Meng | c6336ee | 2017-07-19 21:50:05 +0800 | [diff] [blame] | 215 | int max_ports = HCS_N_PORTS(ehci_readl(&ctrl->hccr->cr_hcsparams)); |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 216 | |
| 217 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Peng Fan | ba397ba | 2016-06-15 13:15:46 +0800 | [diff] [blame] | 218 | /* If not run, directly return */ |
| 219 | if (!(cmd & CMD_RUN)) |
| 220 | return 0; |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 221 | cmd &= ~(CMD_PSE | CMD_ASE); |
| 222 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 223 | ret = handshake(&ctrl->hcor->or_usbsts, STS_ASS | STS_PSS, 0, |
| 224 | 100 * 1000); |
| 225 | |
| 226 | if (!ret) { |
Bin Meng | c6336ee | 2017-07-19 21:50:05 +0800 | [diff] [blame] | 227 | for (i = 0; i < max_ports; i++) { |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 228 | reg = ehci_readl(&ctrl->hcor->or_portsc[i]); |
| 229 | reg |= EHCI_PS_SUSP; |
| 230 | ehci_writel(&ctrl->hcor->or_portsc[i], reg); |
| 231 | } |
| 232 | |
| 233 | cmd &= ~CMD_RUN; |
| 234 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 235 | ret = handshake(&ctrl->hcor->or_usbsts, STS_HALT, STS_HALT, |
| 236 | HCHALT_TIMEOUT); |
| 237 | } |
| 238 | |
| 239 | if (ret) |
| 240 | puts("EHCI failed to shut down host controller.\n"); |
| 241 | |
| 242 | return ret; |
| 243 | } |
| 244 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 245 | static int ehci_td_buffer(struct qTD *td, void *buf, size_t sz) |
| 246 | { |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 247 | uint32_t delta, next; |
Marek Vasut | cadf42c | 2016-02-26 19:23:27 +0100 | [diff] [blame] | 248 | unsigned long addr = (unsigned long)buf; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 249 | int idx; |
| 250 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 251 | if (addr != ALIGN(addr, ARCH_DMA_MINALIGN)) |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 252 | debug("EHCI-HCD: Misaligned buffer address (%p)\n", buf); |
| 253 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 254 | flush_dcache_range(addr, ALIGN(addr + sz, ARCH_DMA_MINALIGN)); |
| 255 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 256 | idx = 0; |
Benoît Thébaudeau | e68f48a | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 257 | while (idx < QT_BUFFER_CNT) { |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 258 | td->qt_buffer[idx] = cpu_to_hc32(virt_to_phys((void *)addr)); |
Wolfgang Denk | ebb829f | 2010-10-19 16:13:15 +0200 | [diff] [blame] | 259 | td->qt_buffer_hi[idx] = 0; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 260 | next = (addr + EHCI_PAGE_SIZE) & ~(EHCI_PAGE_SIZE - 1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 261 | delta = next - addr; |
| 262 | if (delta >= sz) |
| 263 | break; |
| 264 | sz -= delta; |
| 265 | addr = next; |
| 266 | idx++; |
| 267 | } |
| 268 | |
Benoît Thébaudeau | e68f48a | 2012-07-19 22:16:38 +0200 | [diff] [blame] | 269 | if (idx == QT_BUFFER_CNT) { |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 270 | printf("out of buffer pointers (%zu bytes left)\n", sz); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 271 | return -1; |
| 272 | } |
| 273 | |
| 274 | return 0; |
| 275 | } |
| 276 | |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 277 | static inline u8 ehci_encode_speed(enum usb_device_speed speed) |
| 278 | { |
| 279 | #define QH_HIGH_SPEED 2 |
| 280 | #define QH_FULL_SPEED 0 |
| 281 | #define QH_LOW_SPEED 1 |
| 282 | if (speed == USB_SPEED_HIGH) |
| 283 | return QH_HIGH_SPEED; |
| 284 | if (speed == USB_SPEED_LOW) |
| 285 | return QH_LOW_SPEED; |
| 286 | return QH_FULL_SPEED; |
| 287 | } |
| 288 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 289 | static void ehci_update_endpt2_dev_n_port(struct usb_device *udev, |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 290 | struct QH *qh) |
| 291 | { |
Stefan Brüns | a010568 | 2015-12-22 01:21:03 +0100 | [diff] [blame] | 292 | uint8_t portnr = 0; |
| 293 | uint8_t hubaddr = 0; |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 294 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 295 | if (udev->speed != USB_SPEED_LOW && udev->speed != USB_SPEED_FULL) |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 296 | return; |
| 297 | |
Stefan Brüns | a010568 | 2015-12-22 01:21:03 +0100 | [diff] [blame] | 298 | usb_find_usb2_hub_address_port(udev, &hubaddr, &portnr); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 299 | |
Stefan Brüns | a010568 | 2015-12-22 01:21:03 +0100 | [diff] [blame] | 300 | qh->qh_endpt2 |= cpu_to_hc32(QH_ENDPT2_PORTNUM(portnr) | |
| 301 | QH_ENDPT2_HUBADDR(hubaddr)); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 302 | } |
| 303 | |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 304 | static int ehci_enable_async(struct ehci_ctrl *ctrl) |
| 305 | { |
| 306 | u32 cmd; |
| 307 | int ret; |
| 308 | |
| 309 | /* Enable async. schedule. */ |
| 310 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
| 311 | if (cmd & CMD_ASE) |
| 312 | return 0; |
| 313 | |
| 314 | cmd |= CMD_ASE; |
| 315 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 316 | |
| 317 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, STS_ASS, |
| 318 | 100 * 1000); |
| 319 | if (ret < 0) |
| 320 | printf("EHCI fail timeout STS_ASS set\n"); |
| 321 | |
| 322 | return ret; |
| 323 | } |
| 324 | |
| 325 | static int ehci_disable_async(struct ehci_ctrl *ctrl) |
| 326 | { |
| 327 | u32 cmd; |
| 328 | int ret; |
| 329 | |
| 330 | if (ctrl->async_locked) |
| 331 | return 0; |
| 332 | |
| 333 | /* Disable async schedule. */ |
| 334 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
| 335 | if (!(cmd & CMD_ASE)) |
| 336 | return 0; |
| 337 | |
| 338 | cmd &= ~CMD_ASE; |
| 339 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
| 340 | |
| 341 | ret = handshake((uint32_t *)&ctrl->hcor->or_usbsts, STS_ASS, 0, |
| 342 | 100 * 1000); |
| 343 | if (ret < 0) |
| 344 | printf("EHCI fail timeout STS_ASS reset\n"); |
| 345 | |
| 346 | return ret; |
| 347 | } |
| 348 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 349 | static int |
| 350 | ehci_submit_async(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 351 | int length, struct devrequest *req) |
| 352 | { |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 353 | ALLOC_ALIGN_BUFFER(struct QH, qh, 1, USB_DMA_MINALIGN); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 354 | struct qTD *qtd; |
| 355 | int qtd_count = 0; |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 356 | int qtd_counter = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 357 | volatile struct qTD *vtd; |
| 358 | unsigned long ts; |
| 359 | uint32_t *tdp; |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 360 | uint32_t endpt, maxpacket, token, usbsts, qhtoken; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 361 | uint32_t c, toggle; |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 362 | int timeout; |
Michael Trimarchi | 6d4b91c | 2008-12-31 10:33:22 +0100 | [diff] [blame] | 363 | int ret = 0; |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 364 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 365 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 366 | debug("dev=%p, pipe=%lx, buffer=%p, length=%d, req=%p\n", dev, pipe, |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 367 | buffer, length, req); |
| 368 | if (req != NULL) |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 369 | debug("req=%u (%#x), type=%u (%#x), value=%u (%#x), index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 370 | req->request, req->request, |
| 371 | req->requesttype, req->requesttype, |
| 372 | le16_to_cpu(req->value), le16_to_cpu(req->value), |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 373 | le16_to_cpu(req->index)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 374 | |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 375 | #define PKT_ALIGN 512 |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 376 | /* |
| 377 | * The USB transfer is split into qTD transfers. Eeach qTD transfer is |
| 378 | * described by a transfer descriptor (the qTD). The qTDs form a linked |
| 379 | * list with a queue head (QH). |
| 380 | * |
| 381 | * Each qTD transfer starts with a new USB packet, i.e. a packet cannot |
| 382 | * have its beginning in a qTD transfer and its end in the following |
| 383 | * one, so the qTD transfer lengths have to be chosen accordingly. |
| 384 | * |
| 385 | * Each qTD transfer uses up to QT_BUFFER_CNT data buffers, mapped to |
| 386 | * single pages. The first data buffer can start at any offset within a |
| 387 | * page (not considering the cache-line alignment issues), while the |
| 388 | * following buffers must be page-aligned. There is no alignment |
| 389 | * constraint on the size of a qTD transfer. |
| 390 | */ |
| 391 | if (req != NULL) |
| 392 | /* 1 qTD will be needed for SETUP, and 1 for ACK. */ |
| 393 | qtd_count += 1 + 1; |
| 394 | if (length > 0 || req == NULL) { |
| 395 | /* |
| 396 | * Determine the qTD transfer size that will be used for the |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 397 | * data payload (not considering the first qTD transfer, which |
| 398 | * may be longer or shorter, and the final one, which may be |
| 399 | * shorter). |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 400 | * |
| 401 | * In order to keep each packet within a qTD transfer, the qTD |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 402 | * transfer size is aligned to PKT_ALIGN, which is a multiple of |
| 403 | * wMaxPacketSize (except in some cases for interrupt transfers, |
| 404 | * see comment in submit_int_msg()). |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 405 | * |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 406 | * By default, i.e. if the input buffer is aligned to PKT_ALIGN, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 407 | * QT_BUFFER_CNT full pages will be used. |
| 408 | */ |
| 409 | int xfr_sz = QT_BUFFER_CNT; |
| 410 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 411 | * However, if the input buffer is not aligned to PKT_ALIGN, the |
| 412 | * qTD transfer size will be one page shorter, and the first qTD |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 413 | * data buffer of each transfer will be page-unaligned. |
| 414 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 415 | if ((unsigned long)buffer & (PKT_ALIGN - 1)) |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 416 | xfr_sz--; |
| 417 | /* Convert the qTD transfer size to bytes. */ |
| 418 | xfr_sz *= EHCI_PAGE_SIZE; |
| 419 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 420 | * Approximate by excess the number of qTDs that will be |
| 421 | * required for the data payload. The exact formula is way more |
| 422 | * complicated and saves at most 2 qTDs, i.e. a total of 128 |
| 423 | * bytes. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 424 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 425 | qtd_count += 2 + length / xfr_sz; |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 426 | } |
| 427 | /* |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 428 | * Threshold value based on the worst-case total size of the allocated qTDs for |
| 429 | * a mass-storage transfer of 65535 blocks of 512 bytes. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 430 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 431 | #if CONFIG_SYS_MALLOC_LEN <= 64 + 128 * 1024 |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 432 | #warning CONFIG_SYS_MALLOC_LEN may be too small for EHCI |
| 433 | #endif |
| 434 | qtd = memalign(USB_DMA_MINALIGN, qtd_count * sizeof(struct qTD)); |
| 435 | if (qtd == NULL) { |
| 436 | printf("unable to allocate TDs\n"); |
| 437 | return -1; |
| 438 | } |
| 439 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 440 | memset(qh, 0, sizeof(struct QH)); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 441 | memset(qtd, 0, qtd_count * sizeof(*qtd)); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 442 | |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 443 | toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 444 | |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 445 | /* |
| 446 | * Setup QH (3.6 in ehci-r10.pdf) |
| 447 | * |
| 448 | * qh_link ................. 03-00 H |
| 449 | * qh_endpt1 ............... 07-04 H |
| 450 | * qh_endpt2 ............... 0B-08 H |
| 451 | * - qh_curtd |
| 452 | * qh_overlay.qt_next ...... 13-10 H |
| 453 | * - qh_overlay.qt_altnext |
| 454 | */ |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 455 | qh->qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH); |
Ilya Yanok | a1cf10f | 2012-11-06 13:48:20 +0000 | [diff] [blame] | 456 | c = (dev->speed != USB_SPEED_HIGH) && !usb_pipeendpoint(pipe); |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 457 | maxpacket = usb_maxpacket(dev, pipe); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 458 | endpt = QH_ENDPT1_RL(8) | QH_ENDPT1_C(c) | |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 459 | QH_ENDPT1_MAXPKTLEN(maxpacket) | QH_ENDPT1_H(0) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 460 | QH_ENDPT1_DTC(QH_ENDPT1_DTC_DT_FROM_QTD) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 461 | QH_ENDPT1_ENDPT(usb_pipeendpoint(pipe)) | QH_ENDPT1_I(0) | |
| 462 | QH_ENDPT1_DEVADDR(usb_pipedevice(pipe)); |
Chris Packham | 434f058 | 2018-10-04 20:03:53 +1300 | [diff] [blame] | 463 | |
| 464 | /* Force FS for fsl HS quirk */ |
| 465 | if (!ctrl->has_fsl_erratum_a005275) |
| 466 | endpt |= QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)); |
| 467 | else |
| 468 | endpt |= QH_ENDPT1_EPS(ehci_encode_speed(QH_FULL_SPEED)); |
| 469 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 470 | qh->qh_endpt1 = cpu_to_hc32(endpt); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 471 | endpt = QH_ENDPT2_MULT(1) | QH_ENDPT2_UFCMASK(0) | QH_ENDPT2_UFSMASK(0); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 472 | qh->qh_endpt2 = cpu_to_hc32(endpt); |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 473 | ehci_update_endpt2_dev_n_port(dev, qh); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 474 | qh->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
Stephen Warren | 1907e5a | 2014-02-07 09:53:50 -0700 | [diff] [blame] | 475 | qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 476 | |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 477 | tdp = &qh->qh_overlay.qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 478 | if (req != NULL) { |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 479 | /* |
| 480 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 481 | * |
| 482 | * qt_next ................ 03-00 H |
| 483 | * qt_altnext ............. 07-04 H |
| 484 | * qt_token ............... 0B-08 H |
| 485 | * |
| 486 | * [ buffer, buffer_hi ] loaded with "req". |
| 487 | */ |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 488 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 489 | qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 490 | token = QT_TOKEN_DT(0) | QT_TOKEN_TOTALBYTES(sizeof(*req)) | |
| 491 | QT_TOKEN_IOC(0) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 492 | QT_TOKEN_PID(QT_TOKEN_PID_SETUP) | |
| 493 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 494 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 495 | if (ehci_td_buffer(&qtd[qtd_counter], req, sizeof(*req))) { |
| 496 | printf("unable to construct SETUP TD\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 497 | goto fail; |
| 498 | } |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 499 | /* Update previous qTD! */ |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 500 | *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter])); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 501 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 502 | toggle = 1; |
| 503 | } |
| 504 | |
| 505 | if (length > 0 || req == NULL) { |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 506 | uint8_t *buf_ptr = buffer; |
| 507 | int left_length = length; |
| 508 | |
| 509 | do { |
| 510 | /* |
| 511 | * Determine the size of this qTD transfer. By default, |
| 512 | * QT_BUFFER_CNT full pages can be used. |
| 513 | */ |
| 514 | int xfr_bytes = QT_BUFFER_CNT * EHCI_PAGE_SIZE; |
| 515 | /* |
| 516 | * However, if the input buffer is not page-aligned, the |
| 517 | * portion of the first page before the buffer start |
| 518 | * offset within that page is unusable. |
| 519 | */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 520 | xfr_bytes -= (unsigned long)buf_ptr & (EHCI_PAGE_SIZE - 1); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 521 | /* |
| 522 | * In order to keep each packet within a qTD transfer, |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 523 | * align the qTD transfer size to PKT_ALIGN. |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 524 | */ |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 525 | xfr_bytes &= ~(PKT_ALIGN - 1); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 526 | /* |
| 527 | * This transfer may be shorter than the available qTD |
| 528 | * transfer size that has just been computed. |
| 529 | */ |
| 530 | xfr_bytes = min(xfr_bytes, left_length); |
| 531 | |
| 532 | /* |
| 533 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 534 | * |
| 535 | * qt_next ................ 03-00 H |
| 536 | * qt_altnext ............. 07-04 H |
| 537 | * qt_token ............... 0B-08 H |
| 538 | * |
| 539 | * [ buffer, buffer_hi ] loaded with "buffer". |
| 540 | */ |
| 541 | qtd[qtd_counter].qt_next = |
| 542 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 543 | qtd[qtd_counter].qt_altnext = |
| 544 | cpu_to_hc32(QT_NEXT_TERMINATE); |
| 545 | token = QT_TOKEN_DT(toggle) | |
| 546 | QT_TOKEN_TOTALBYTES(xfr_bytes) | |
| 547 | QT_TOKEN_IOC(req == NULL) | QT_TOKEN_CPAGE(0) | |
| 548 | QT_TOKEN_CERR(3) | |
| 549 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 550 | QT_TOKEN_PID_IN : QT_TOKEN_PID_OUT) | |
| 551 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
| 552 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
| 553 | if (ehci_td_buffer(&qtd[qtd_counter], buf_ptr, |
| 554 | xfr_bytes)) { |
| 555 | printf("unable to construct DATA TD\n"); |
| 556 | goto fail; |
| 557 | } |
| 558 | /* Update previous qTD! */ |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 559 | *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter])); |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 560 | tdp = &qtd[qtd_counter++].qt_next; |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 561 | /* |
| 562 | * Data toggle has to be adjusted since the qTD transfer |
| 563 | * size is not always an even multiple of |
| 564 | * wMaxPacketSize. |
| 565 | */ |
| 566 | if ((xfr_bytes / maxpacket) & 1) |
| 567 | toggle ^= 1; |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 568 | buf_ptr += xfr_bytes; |
| 569 | left_length -= xfr_bytes; |
| 570 | } while (left_length > 0); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 571 | } |
| 572 | |
| 573 | if (req != NULL) { |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 574 | /* |
| 575 | * Setup request qTD (3.5 in ehci-r10.pdf) |
| 576 | * |
| 577 | * qt_next ................ 03-00 H |
| 578 | * qt_altnext ............. 07-04 H |
| 579 | * qt_token ............... 0B-08 H |
| 580 | */ |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 581 | qtd[qtd_counter].qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 582 | qtd[qtd_counter].qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 4e23df1 | 2012-08-10 18:27:23 +0200 | [diff] [blame] | 583 | token = QT_TOKEN_DT(1) | QT_TOKEN_TOTALBYTES(0) | |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 584 | QT_TOKEN_IOC(1) | QT_TOKEN_CPAGE(0) | QT_TOKEN_CERR(3) | |
| 585 | QT_TOKEN_PID(usb_pipein(pipe) ? |
| 586 | QT_TOKEN_PID_OUT : QT_TOKEN_PID_IN) | |
| 587 | QT_TOKEN_STATUS(QT_TOKEN_STATUS_ACTIVE); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 588 | qtd[qtd_counter].qt_token = cpu_to_hc32(token); |
Marek Vasut | 285c8b3 | 2012-04-09 04:13:00 +0200 | [diff] [blame] | 589 | /* Update previous qTD! */ |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 590 | *tdp = cpu_to_hc32(virt_to_phys(&qtd[qtd_counter])); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 591 | tdp = &qtd[qtd_counter++].qt_next; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 592 | } |
| 593 | |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 594 | ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(qh) | QH_LINK_TYPE_QH); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 595 | |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 596 | /* Flush dcache */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 597 | flush_dcache_range((unsigned long)&ctrl->qh_list, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 598 | ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 599 | flush_dcache_range((unsigned long)qh, ALIGN_END_ADDR(struct QH, qh, 1)); |
| 600 | flush_dcache_range((unsigned long)qtd, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 601 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 602 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 603 | usbsts = ehci_readl(&ctrl->hcor->or_usbsts); |
| 604 | ehci_writel(&ctrl->hcor->or_usbsts, (usbsts & 0x3f)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 605 | |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 606 | ret = ehci_enable_async(ctrl); |
| 607 | if (ret) |
| 608 | goto fail; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 609 | |
| 610 | /* Wait for TDs to be processed. */ |
| 611 | ts = get_timer(0); |
Marek Vasut | 4f66831 | 2012-04-08 23:32:05 +0200 | [diff] [blame] | 612 | vtd = &qtd[qtd_counter - 1]; |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 613 | timeout = USB_TIMEOUT_MS(pipe); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 614 | do { |
Stefan Roese | 25983c1 | 2009-01-21 17:12:19 +0100 | [diff] [blame] | 615 | /* Invalidate dcache */ |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 616 | invalidate_dcache_range((unsigned long)&ctrl->qh_list, |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 617 | ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 618 | invalidate_dcache_range((unsigned long)qh, |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 619 | ALIGN_END_ADDR(struct QH, qh, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 620 | invalidate_dcache_range((unsigned long)qtd, |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 621 | ALIGN_END_ADDR(struct qTD, qtd, qtd_count)); |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 622 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 623 | token = hc32_to_cpu(vtd->qt_token); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 624 | if (!(QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 625 | break; |
Stefan Roese | 86b34cf | 2010-11-26 15:43:28 +0100 | [diff] [blame] | 626 | WATCHDOG_RESET(); |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 627 | } while (get_timer(ts) < timeout); |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 628 | qhtoken = hc32_to_cpu(qh->qh_overlay.qt_token); |
| 629 | |
| 630 | ctrl->qh_list.qh_link = cpu_to_hc32(virt_to_phys(&ctrl->qh_list) | QH_LINK_TYPE_QH); |
| 631 | flush_dcache_range((unsigned long)&ctrl->qh_list, |
| 632 | ALIGN_END_ADDR(struct QH, &ctrl->qh_list, 1)); |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 633 | |
Ilya Yanok | fb11371 | 2012-07-15 04:43:49 +0000 | [diff] [blame] | 634 | /* |
| 635 | * Invalidate the memory area occupied by buffer |
| 636 | * Don't try to fix the buffer alignment, if it isn't properly |
| 637 | * aligned it's upper layer's fault so let invalidate_dcache_range() |
| 638 | * vow about it. But we have to fix the length as it's actual |
| 639 | * transfer length and can be unaligned. This is potentially |
| 640 | * dangerous operation, it's responsibility of the calling |
| 641 | * code to make sure enough space is reserved. |
| 642 | */ |
Dirk Behme | 78c7356 | 2017-11-17 15:28:36 +0100 | [diff] [blame] | 643 | if (buffer != NULL && length > 0) |
| 644 | invalidate_dcache_range((unsigned long)buffer, |
| 645 | ALIGN((unsigned long)buffer + length, ARCH_DMA_MINALIGN)); |
Marek Vasut | ff24dc3 | 2012-04-09 04:07:46 +0200 | [diff] [blame] | 646 | |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 647 | /* Check that the TD processing happened */ |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 648 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) |
Simon Glass | fd7f513 | 2011-02-07 14:42:16 -0800 | [diff] [blame] | 649 | printf("EHCI timed out on TD - token=%#x\n", token); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 650 | |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 651 | ret = ehci_disable_async(ctrl); |
| 652 | if (ret) |
| 653 | goto fail; |
| 654 | |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 655 | if (!(QT_TOKEN_GET_STATUS(qhtoken) & QT_TOKEN_STATUS_ACTIVE)) { |
| 656 | debug("TOKEN=%#x\n", qhtoken); |
| 657 | switch (QT_TOKEN_GET_STATUS(qhtoken) & |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 658 | ~(QT_TOKEN_STATUS_SPLITXSTATE | QT_TOKEN_STATUS_PERR)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 659 | case 0: |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 660 | toggle = QT_TOKEN_GET_DT(qhtoken); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 661 | usb_settoggle(dev, usb_pipeendpoint(pipe), |
| 662 | usb_pipeout(pipe), toggle); |
| 663 | dev->status = 0; |
| 664 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 665 | case QT_TOKEN_STATUS_HALTED: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 666 | dev->status = USB_ST_STALLED; |
| 667 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 668 | case QT_TOKEN_STATUS_ACTIVE | QT_TOKEN_STATUS_DATBUFERR: |
| 669 | case QT_TOKEN_STATUS_DATBUFERR: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 670 | dev->status = USB_ST_BUF_ERR; |
| 671 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 672 | case QT_TOKEN_STATUS_HALTED | QT_TOKEN_STATUS_BABBLEDET: |
| 673 | case QT_TOKEN_STATUS_BABBLEDET: |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 674 | dev->status = USB_ST_BABBLE_DET; |
| 675 | break; |
| 676 | default: |
| 677 | dev->status = USB_ST_CRC_ERR; |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 678 | if (QT_TOKEN_GET_STATUS(qhtoken) & QT_TOKEN_STATUS_HALTED) |
Anatolij Gustschin | e1e0931 | 2010-11-02 11:47:29 +0100 | [diff] [blame] | 679 | dev->status |= USB_ST_STALLED; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 680 | break; |
| 681 | } |
Marek Vasut | 569c2a5 | 2019-10-06 16:13:38 +0200 | [diff] [blame] | 682 | dev->act_len = length - QT_TOKEN_GET_TOTALBYTES(qhtoken); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 683 | } else { |
| 684 | dev->act_len = 0; |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 685 | #ifndef CONFIG_USB_EHCI_FARADAY |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 686 | debug("dev=%u, usbsts=%#x, p[1]=%#x, p[2]=%#x\n", |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 687 | dev->devnum, ehci_readl(&ctrl->hcor->or_usbsts), |
| 688 | ehci_readl(&ctrl->hcor->or_portsc[0]), |
| 689 | ehci_readl(&ctrl->hcor->or_portsc[1])); |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 690 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 691 | } |
| 692 | |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 693 | free(qtd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 694 | return (dev->status != USB_ST_NOT_PROC) ? 0 : -1; |
| 695 | |
| 696 | fail: |
Benoît Thébaudeau | b39f8b5 | 2012-08-10 18:22:32 +0200 | [diff] [blame] | 697 | free(qtd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 698 | return -1; |
| 699 | } |
| 700 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 701 | static int ehci_submit_root(struct usb_device *dev, unsigned long pipe, |
| 702 | void *buffer, int length, struct devrequest *req) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 703 | { |
| 704 | uint8_t tmpbuf[4]; |
| 705 | u16 typeReq; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 706 | void *srcptr = NULL; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 707 | int len, srclen; |
| 708 | uint32_t reg; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 709 | uint32_t *status_reg; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 710 | int port = le16_to_cpu(req->index) & 0xff; |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 711 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 712 | |
| 713 | srclen = 0; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 714 | |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 715 | debug("req=%u (%#x), type=%u (%#x), value=%u, index=%u\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 716 | req->request, req->request, |
| 717 | req->requesttype, req->requesttype, |
| 718 | le16_to_cpu(req->value), le16_to_cpu(req->index)); |
| 719 | |
Prafulla Wadaskar | 2281029 | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 720 | typeReq = req->request | req->requesttype << 8; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 721 | |
Prafulla Wadaskar | 2281029 | 2009-07-17 19:56:30 +0530 | [diff] [blame] | 722 | switch (typeReq) { |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 723 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
| 724 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
| 725 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 726 | status_reg = ctrl->ops.get_portsc_register(ctrl, port - 1); |
Kuo-Jung Su | 6a656df | 2013-05-15 15:29:21 +0800 | [diff] [blame] | 727 | if (!status_reg) |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 728 | return -1; |
Kuo-Jung Su | 9930e9f | 2013-05-15 15:29:20 +0800 | [diff] [blame] | 729 | break; |
| 730 | default: |
| 731 | status_reg = NULL; |
| 732 | break; |
| 733 | } |
| 734 | |
| 735 | switch (typeReq) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 736 | case DeviceRequest | USB_REQ_GET_DESCRIPTOR: |
| 737 | switch (le16_to_cpu(req->value) >> 8) { |
| 738 | case USB_DT_DEVICE: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 739 | debug("USB_DT_DEVICE request\n"); |
| 740 | srcptr = &descriptor.device; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 741 | srclen = descriptor.device.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 742 | break; |
| 743 | case USB_DT_CONFIG: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 744 | debug("USB_DT_CONFIG config\n"); |
| 745 | srcptr = &descriptor.config; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 746 | srclen = descriptor.config.bLength + |
| 747 | descriptor.interface.bLength + |
| 748 | descriptor.endpoint.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 749 | break; |
| 750 | case USB_DT_STRING: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 751 | debug("USB_DT_STRING config\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 752 | switch (le16_to_cpu(req->value) & 0xff) { |
| 753 | case 0: /* Language */ |
| 754 | srcptr = "\4\3\1\0"; |
| 755 | srclen = 4; |
| 756 | break; |
| 757 | case 1: /* Vendor */ |
| 758 | srcptr = "\16\3u\0-\0b\0o\0o\0t\0"; |
| 759 | srclen = 14; |
| 760 | break; |
| 761 | case 2: /* Product */ |
| 762 | srcptr = "\52\3E\0H\0C\0I\0 " |
| 763 | "\0H\0o\0s\0t\0 " |
| 764 | "\0C\0o\0n\0t\0r\0o\0l\0l\0e\0r\0"; |
| 765 | srclen = 42; |
| 766 | break; |
| 767 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 768 | debug("unknown value DT_STRING %x\n", |
| 769 | le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 770 | goto unknown; |
| 771 | } |
| 772 | break; |
| 773 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 774 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 775 | goto unknown; |
| 776 | } |
| 777 | break; |
| 778 | case USB_REQ_GET_DESCRIPTOR | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 779 | switch (le16_to_cpu(req->value) >> 8) { |
| 780 | case USB_DT_HUB: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 781 | debug("USB_DT_HUB config\n"); |
| 782 | srcptr = &descriptor.hub; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 783 | srclen = descriptor.hub.bLength; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 784 | break; |
| 785 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 786 | debug("unknown value %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 787 | goto unknown; |
| 788 | } |
| 789 | break; |
| 790 | case USB_REQ_SET_ADDRESS | (USB_RECIP_DEVICE << 8): |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 791 | debug("USB_REQ_SET_ADDRESS\n"); |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 792 | ctrl->rootdev = le16_to_cpu(req->value); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 793 | break; |
| 794 | case DeviceOutRequest | USB_REQ_SET_CONFIGURATION: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 795 | debug("USB_REQ_SET_CONFIGURATION\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 796 | /* Nothing to do */ |
| 797 | break; |
| 798 | case USB_REQ_GET_STATUS | ((USB_DIR_IN | USB_RT_HUB) << 8): |
| 799 | tmpbuf[0] = 1; /* USB_STATUS_SELFPOWERED */ |
| 800 | tmpbuf[1] = 0; |
| 801 | srcptr = tmpbuf; |
| 802 | srclen = 2; |
| 803 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 804 | case USB_REQ_GET_STATUS | ((USB_RT_PORT | USB_DIR_IN) << 8): |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 805 | memset(tmpbuf, 0, 4); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 806 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 807 | if (reg & EHCI_PS_CS) |
| 808 | tmpbuf[0] |= USB_PORT_STAT_CONNECTION; |
| 809 | if (reg & EHCI_PS_PE) |
| 810 | tmpbuf[0] |= USB_PORT_STAT_ENABLE; |
| 811 | if (reg & EHCI_PS_SUSP) |
| 812 | tmpbuf[0] |= USB_PORT_STAT_SUSPEND; |
| 813 | if (reg & EHCI_PS_OCA) |
| 814 | tmpbuf[0] |= USB_PORT_STAT_OVERCURRENT; |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 815 | if (reg & EHCI_PS_PR) |
| 816 | tmpbuf[0] |= USB_PORT_STAT_RESET; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 817 | if (reg & EHCI_PS_PP) |
| 818 | tmpbuf[1] |= USB_PORT_STAT_POWER >> 8; |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 819 | |
| 820 | if (ehci_is_TDI()) { |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 821 | switch (ctrl->ops.get_port_speed(ctrl, reg)) { |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 822 | case PORTSC_PSPD_FS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 823 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 824 | case PORTSC_PSPD_LS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 825 | tmpbuf[1] |= USB_PORT_STAT_LOW_SPEED >> 8; |
| 826 | break; |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 827 | case PORTSC_PSPD_HS: |
Stefan Roese | 497f184 | 2009-01-21 17:12:01 +0100 | [diff] [blame] | 828 | default: |
| 829 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 830 | break; |
| 831 | } |
| 832 | } else { |
| 833 | tmpbuf[1] |= USB_PORT_STAT_HIGH_SPEED >> 8; |
| 834 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 835 | |
| 836 | if (reg & EHCI_PS_CSC) |
| 837 | tmpbuf[2] |= USB_PORT_STAT_C_CONNECTION; |
| 838 | if (reg & EHCI_PS_PEC) |
| 839 | tmpbuf[2] |= USB_PORT_STAT_C_ENABLE; |
| 840 | if (reg & EHCI_PS_OCC) |
| 841 | tmpbuf[2] |= USB_PORT_STAT_C_OVERCURRENT; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 842 | if (ctrl->portreset & (1 << port)) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 843 | tmpbuf[2] |= USB_PORT_STAT_C_RESET; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 844 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 845 | srcptr = tmpbuf; |
| 846 | srclen = 4; |
| 847 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 848 | case USB_REQ_SET_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 849 | reg = ehci_readl(status_reg); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 850 | reg &= ~EHCI_PS_CLEAR; |
| 851 | switch (le16_to_cpu(req->value)) { |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 852 | case USB_PORT_FEAT_ENABLE: |
| 853 | reg |= EHCI_PS_PE; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 854 | ehci_writel(status_reg, reg); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 855 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 856 | case USB_PORT_FEAT_POWER: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 857 | if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) { |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 858 | reg |= EHCI_PS_PP; |
| 859 | ehci_writel(status_reg, reg); |
| 860 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 861 | break; |
| 862 | case USB_PORT_FEAT_RESET: |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 863 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) == EHCI_PS_CS && |
| 864 | !ehci_is_TDI() && |
| 865 | EHCI_PS_IS_LOWSPEED(reg)) { |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 866 | /* Low speed device, give up ownership. */ |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 867 | debug("port %d low speed --> companion\n", |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 868 | port - 1); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 869 | reg |= EHCI_PS_PO; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 870 | ehci_writel(status_reg, reg); |
Hans de Goede | 63f34ca | 2015-05-10 14:10:16 +0200 | [diff] [blame] | 871 | return -ENXIO; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 872 | } else { |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 873 | int ret; |
| 874 | |
Chris Packham | 434f058 | 2018-10-04 20:03:53 +1300 | [diff] [blame] | 875 | /* Disable chirp for HS erratum */ |
| 876 | if (ctrl->has_fsl_erratum_a005275) |
| 877 | reg |= PORTSC_FSL_PFSC; |
| 878 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 879 | reg |= EHCI_PS_PR; |
| 880 | reg &= ~EHCI_PS_PE; |
| 881 | ehci_writel(status_reg, reg); |
| 882 | /* |
| 883 | * caller must wait, then call GetPortStatus |
| 884 | * usb 2.0 specification say 50 ms resets on |
| 885 | * root |
| 886 | */ |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 887 | ctrl->ops.powerup_fixup(ctrl, status_reg, ®); |
Marek Vasut | 0973477 | 2011-07-11 02:37:01 +0200 | [diff] [blame] | 888 | |
Chris Zhang | fddf6d6 | 2010-01-06 13:34:04 -0800 | [diff] [blame] | 889 | ehci_writel(status_reg, reg & ~EHCI_PS_PR); |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 890 | /* |
| 891 | * A host controller must terminate the reset |
| 892 | * and stabilize the state of the port within |
| 893 | * 2 milliseconds |
| 894 | */ |
| 895 | ret = handshake(status_reg, EHCI_PS_PR, 0, |
| 896 | 2 * 1000); |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 897 | if (!ret) { |
| 898 | reg = ehci_readl(status_reg); |
| 899 | if ((reg & (EHCI_PS_PE | EHCI_PS_CS)) |
| 900 | == EHCI_PS_CS && !ehci_is_TDI()) { |
| 901 | debug("port %d full speed --> companion\n", port - 1); |
| 902 | reg &= ~EHCI_PS_CLEAR; |
| 903 | reg |= EHCI_PS_PO; |
| 904 | ehci_writel(status_reg, reg); |
Hans de Goede | 63f34ca | 2015-05-10 14:10:16 +0200 | [diff] [blame] | 905 | return -ENXIO; |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 906 | } else { |
| 907 | ctrl->portreset |= 1 << port; |
| 908 | } |
| 909 | } else { |
Sergei Shtylyov | 23dec68 | 2010-02-27 21:33:21 +0300 | [diff] [blame] | 910 | printf("port(%d) reset error\n", |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 911 | port - 1); |
Hans de Goede | b5b3ef2 | 2015-05-10 14:10:13 +0200 | [diff] [blame] | 912 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 913 | } |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 914 | break; |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 915 | case USB_PORT_FEAT_TEST: |
Julius Werner | 5c1a1ad | 2013-09-24 10:53:07 -0700 | [diff] [blame] | 916 | ehci_shutdown(ctrl); |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 917 | reg &= ~(0xf << 16); |
| 918 | reg |= ((le16_to_cpu(req->index) >> 8) & 0xf) << 16; |
| 919 | ehci_writel(status_reg, reg); |
| 920 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 921 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 922 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 923 | goto unknown; |
| 924 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 925 | /* unblock posted writes */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 926 | (void) ehci_readl(&ctrl->hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 927 | break; |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 928 | case USB_REQ_CLEAR_FEATURE | ((USB_DIR_OUT | USB_RT_PORT) << 8): |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 929 | reg = ehci_readl(status_reg); |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 930 | reg &= ~EHCI_PS_CLEAR; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 931 | switch (le16_to_cpu(req->value)) { |
| 932 | case USB_PORT_FEAT_ENABLE: |
| 933 | reg &= ~EHCI_PS_PE; |
| 934 | break; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 935 | case USB_PORT_FEAT_C_ENABLE: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 936 | reg |= EHCI_PS_PE; |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 937 | break; |
| 938 | case USB_PORT_FEAT_POWER: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 939 | if (HCS_PPC(ehci_readl(&ctrl->hccr->cr_hcsparams))) |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 940 | reg &= ~EHCI_PS_PP; |
| 941 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 942 | case USB_PORT_FEAT_C_CONNECTION: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 943 | reg |= EHCI_PS_CSC; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 944 | break; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 945 | case USB_PORT_FEAT_OVER_CURRENT: |
Simon Glass | 0554ba5 | 2013-05-10 19:49:00 -0700 | [diff] [blame] | 946 | reg |= EHCI_PS_OCC; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 947 | break; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 948 | case USB_PORT_FEAT_C_RESET: |
Julius Werner | d404670 | 2013-02-28 18:08:40 +0000 | [diff] [blame] | 949 | ctrl->portreset &= ~(1 << port); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 950 | break; |
| 951 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 952 | debug("unknown feature %x\n", le16_to_cpu(req->value)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 953 | goto unknown; |
| 954 | } |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 955 | ehci_writel(status_reg, reg); |
| 956 | /* unblock posted write */ |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 957 | (void) ehci_readl(&ctrl->hcor->or_usbcmd); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 958 | break; |
| 959 | default: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 960 | debug("Unknown request\n"); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 961 | goto unknown; |
| 962 | } |
| 963 | |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 964 | mdelay(1); |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 965 | len = min3(srclen, (int)le16_to_cpu(req->length), length); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 966 | if (srcptr != NULL && len > 0) |
| 967 | memcpy(buffer, srcptr, len); |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 968 | else |
| 969 | debug("Len is 0\n"); |
| 970 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 971 | dev->act_len = len; |
| 972 | dev->status = 0; |
| 973 | return 0; |
| 974 | |
| 975 | unknown: |
michael | 0a32610 | 2008-12-10 17:55:19 +0100 | [diff] [blame] | 976 | debug("requesttype=%x, request=%x, value=%x, index=%x, length=%x\n", |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 977 | req->requesttype, req->request, le16_to_cpu(req->value), |
| 978 | le16_to_cpu(req->index), le16_to_cpu(req->length)); |
| 979 | |
| 980 | dev->act_len = 0; |
| 981 | dev->status = USB_ST_STALLED; |
| 982 | return -1; |
| 983 | } |
| 984 | |
Masahiro Yamada | 6d8e433 | 2017-06-22 16:35:14 +0900 | [diff] [blame] | 985 | static const struct ehci_ops default_ehci_ops = { |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 986 | .set_usb_mode = ehci_set_usbmode, |
| 987 | .get_port_speed = ehci_get_port_speed, |
| 988 | .powerup_fixup = ehci_powerup_fixup, |
| 989 | .get_portsc_register = ehci_get_portsc_register, |
| 990 | }; |
| 991 | |
| 992 | static void ehci_setup_ops(struct ehci_ctrl *ctrl, const struct ehci_ops *ops) |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 993 | { |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 994 | if (!ops) { |
| 995 | ctrl->ops = default_ehci_ops; |
| 996 | } else { |
| 997 | ctrl->ops = *ops; |
| 998 | if (!ctrl->ops.set_usb_mode) |
| 999 | ctrl->ops.set_usb_mode = ehci_set_usbmode; |
| 1000 | if (!ctrl->ops.get_port_speed) |
| 1001 | ctrl->ops.get_port_speed = ehci_get_port_speed; |
| 1002 | if (!ctrl->ops.powerup_fixup) |
| 1003 | ctrl->ops.powerup_fixup = ehci_powerup_fixup; |
| 1004 | if (!ctrl->ops.get_portsc_register) |
| 1005 | ctrl->ops.get_portsc_register = |
| 1006 | ehci_get_portsc_register; |
| 1007 | } |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 1008 | } |
| 1009 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1010 | #if !CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 1011 | void ehci_set_controller_priv(int index, void *priv, const struct ehci_ops *ops) |
| 1012 | { |
| 1013 | struct ehci_ctrl *ctrl = &ehcic[index]; |
| 1014 | |
| 1015 | ctrl->priv = priv; |
| 1016 | ehci_setup_ops(ctrl, ops); |
| 1017 | } |
| 1018 | |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 1019 | void *ehci_get_controller_priv(int index) |
| 1020 | { |
| 1021 | return ehcic[index].priv; |
| 1022 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1023 | #endif |
Simon Glass | 0851caa | 2015-03-25 12:22:19 -0600 | [diff] [blame] | 1024 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1025 | static int ehci_common_init(struct ehci_ctrl *ctrl, uint tweaks) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1026 | { |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1027 | struct QH *qh_list; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1028 | struct QH *periodic; |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1029 | uint32_t reg; |
| 1030 | uint32_t cmd; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1031 | int i; |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1032 | |
Vincent Palatin | 0d6f77c | 2012-12-12 17:55:22 -0800 | [diff] [blame] | 1033 | /* Set the high address word (aka segment) for 64-bit controller */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1034 | if (ehci_readl(&ctrl->hccr->cr_hccparams) & 1) |
| 1035 | ehci_writel(&ctrl->hcor->or_ctrldssegment, 0); |
Stefan Roese | 2e98fc7 | 2009-01-21 17:12:10 +0100 | [diff] [blame] | 1036 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1037 | qh_list = &ctrl->qh_list; |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1038 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1039 | /* Set head of reclaim list */ |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 1040 | memset(qh_list, 0, sizeof(*qh_list)); |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 1041 | qh_list->qh_link = cpu_to_hc32(virt_to_phys(qh_list) | QH_LINK_TYPE_QH); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 1042 | qh_list->qh_endpt1 = cpu_to_hc32(QH_ENDPT1_H(1) | |
| 1043 | QH_ENDPT1_EPS(USB_SPEED_HIGH)); |
Tom Rini | 2cabcf7 | 2012-07-15 22:14:24 +0000 | [diff] [blame] | 1044 | qh_list->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1045 | qh_list->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Benoît Thébaudeau | 458fb1e | 2012-08-10 18:22:11 +0200 | [diff] [blame] | 1046 | qh_list->qh_overlay.qt_token = |
| 1047 | cpu_to_hc32(QT_TOKEN_STATUS(QT_TOKEN_STATUS_HALTED)); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1048 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1049 | flush_dcache_range((unsigned long)qh_list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1050 | ALIGN_END_ADDR(struct QH, qh_list, 1)); |
| 1051 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1052 | /* Set async. queue head pointer. */ |
Marek Vasut | df0b624 | 2016-01-23 21:04:46 +0100 | [diff] [blame] | 1053 | ehci_writel(&ctrl->hcor->or_asynclistaddr, virt_to_phys(qh_list)); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1054 | |
| 1055 | /* |
| 1056 | * Set up periodic list |
| 1057 | * Step 1: Parent QH for all periodic transfers. |
| 1058 | */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1059 | ctrl->periodic_schedules = 0; |
| 1060 | periodic = &ctrl->periodic_queue; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1061 | memset(periodic, 0, sizeof(*periodic)); |
| 1062 | periodic->qh_link = cpu_to_hc32(QH_LINK_TERMINATE); |
| 1063 | periodic->qh_overlay.qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1064 | periodic->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1065 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1066 | flush_dcache_range((unsigned long)periodic, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1067 | ALIGN_END_ADDR(struct QH, periodic, 1)); |
| 1068 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1069 | /* |
| 1070 | * Step 2: Setup frame-list: Every microframe, USB tries the same list. |
| 1071 | * In particular, device specifications on polling frequency |
| 1072 | * are disregarded. Keyboards seem to send NAK/NYet reliably |
| 1073 | * when polled with an empty buffer. |
| 1074 | * |
| 1075 | * Split Transactions will be spread across microframes using |
| 1076 | * S-mask and C-mask. |
| 1077 | */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1078 | if (ctrl->periodic_list == NULL) |
| 1079 | ctrl->periodic_list = memalign(4096, 1024 * 4); |
Nikita Kiryanov | 2f13e44 | 2013-07-29 13:27:40 +0300 | [diff] [blame] | 1080 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1081 | if (!ctrl->periodic_list) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1082 | return -ENOMEM; |
| 1083 | for (i = 0; i < 1024; i++) { |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1084 | ctrl->periodic_list[i] = cpu_to_hc32((unsigned long)periodic |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1085 | | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1086 | } |
| 1087 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1088 | flush_dcache_range((unsigned long)ctrl->periodic_list, |
| 1089 | ALIGN_END_ADDR(uint32_t, ctrl->periodic_list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1090 | 1024)); |
| 1091 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1092 | /* Set periodic list base address */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1093 | ehci_writel(&ctrl->hcor->or_periodiclistbase, |
| 1094 | (unsigned long)ctrl->periodic_list); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1095 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1096 | reg = ehci_readl(&ctrl->hccr->cr_hcsparams); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1097 | descriptor.hub.bNbrPorts = HCS_N_PORTS(reg); |
Lucas Stach | f5b3408 | 2012-09-28 00:26:19 +0200 | [diff] [blame] | 1098 | debug("Register %x NbrPorts %d\n", reg, descriptor.hub.bNbrPorts); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1099 | /* Port Indicators */ |
| 1100 | if (HCS_INDICATOR(reg)) |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 1101 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1102 | | 0x80, &descriptor.hub.wHubCharacteristics); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1103 | /* Port Power Control */ |
| 1104 | if (HCS_PPC(reg)) |
Lucas Stach | 835e11e | 2012-09-06 08:00:13 +0200 | [diff] [blame] | 1105 | put_unaligned(get_unaligned(&descriptor.hub.wHubCharacteristics) |
| 1106 | | 0x01, &descriptor.hub.wHubCharacteristics); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1107 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1108 | /* Start the host controller. */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1109 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Wolfgang Denk | fb718e1 | 2009-02-12 00:08:39 +0100 | [diff] [blame] | 1110 | /* |
| 1111 | * Philips, Intel, and maybe others need CMD_RUN before the |
| 1112 | * root hub will detect new devices (why?); NEC doesn't |
| 1113 | */ |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1114 | cmd &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET); |
| 1115 | cmd |= CMD_RUN; |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1116 | ehci_writel(&ctrl->hcor->or_usbcmd, cmd); |
michael | 0bf2a03 | 2008-12-11 13:43:55 +0100 | [diff] [blame] | 1117 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1118 | if (!(tweaks & EHCI_TWEAK_NO_INIT_CF)) { |
| 1119 | /* take control over the ports */ |
| 1120 | cmd = ehci_readl(&ctrl->hcor->or_configflag); |
| 1121 | cmd |= FLAG_CF; |
| 1122 | ehci_writel(&ctrl->hcor->or_configflag, cmd); |
| 1123 | } |
Kuo-Jung Su | b5d59de | 2013-05-15 15:29:23 +0800 | [diff] [blame] | 1124 | |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1125 | /* unblock posted write */ |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1126 | cmd = ehci_readl(&ctrl->hcor->or_usbcmd); |
Mike Frysinger | 60ce19a | 2012-03-05 13:47:00 +0000 | [diff] [blame] | 1127 | mdelay(5); |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1128 | reg = HC_VERSION(ehci_readl(&ctrl->hccr->cr_capbase)); |
Remy Böhmer | 33e8748 | 2008-12-13 22:51:58 +0100 | [diff] [blame] | 1129 | printf("USB EHCI %x.%02x\n", reg >> 8, reg & 0xff); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1130 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1131 | return 0; |
| 1132 | } |
| 1133 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1134 | #if !CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1135 | int usb_lowlevel_stop(int index) |
| 1136 | { |
| 1137 | ehci_shutdown(&ehcic[index]); |
| 1138 | return ehci_hcd_stop(index); |
| 1139 | } |
| 1140 | |
| 1141 | int usb_lowlevel_init(int index, enum usb_init_type init, void **controller) |
| 1142 | { |
| 1143 | struct ehci_ctrl *ctrl = &ehcic[index]; |
| 1144 | uint tweaks = 0; |
| 1145 | int rc; |
| 1146 | |
Simon Glass | dc9f3ed | 2015-03-25 12:22:27 -0600 | [diff] [blame] | 1147 | /** |
| 1148 | * Set ops to default_ehci_ops, ehci_hcd_init should call |
| 1149 | * ehci_set_controller_priv to change any of these function pointers. |
| 1150 | */ |
| 1151 | ctrl->ops = default_ehci_ops; |
| 1152 | |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1153 | rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor); |
| 1154 | if (rc) |
| 1155 | return rc; |
Heinrich Schuchardt | ab4304b | 2017-11-20 19:33:39 +0100 | [diff] [blame] | 1156 | if (!ctrl->hccr || !ctrl->hcor) |
| 1157 | return -1; |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1158 | if (init == USB_INIT_DEVICE) |
| 1159 | goto done; |
| 1160 | |
| 1161 | /* EHCI spec section 4.1 */ |
Simon Glass | 302696b | 2015-03-25 12:22:28 -0600 | [diff] [blame] | 1162 | if (ehci_reset(ctrl)) |
Simon Glass | ccc40fd | 2015-03-25 12:22:26 -0600 | [diff] [blame] | 1163 | return -1; |
| 1164 | |
| 1165 | #if defined(CONFIG_EHCI_HCD_INIT_AFTER_RESET) |
| 1166 | rc = ehci_hcd_init(index, init, &ctrl->hccr, &ctrl->hcor); |
| 1167 | if (rc) |
| 1168 | return rc; |
| 1169 | #endif |
| 1170 | #ifdef CONFIG_USB_EHCI_FARADAY |
| 1171 | tweaks |= EHCI_TWEAK_NO_INIT_CF; |
| 1172 | #endif |
| 1173 | rc = ehci_common_init(ctrl, tweaks); |
| 1174 | if (rc) |
| 1175 | return rc; |
| 1176 | |
| 1177 | ctrl->rootdev = 0; |
Troy Kisky | 7d6bbb9 | 2013-10-10 15:27:57 -0700 | [diff] [blame] | 1178 | done: |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1179 | *controller = &ehcic[index]; |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1180 | return 0; |
| 1181 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1182 | #endif |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1183 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1184 | static int _ehci_submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 1185 | void *buffer, int length) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1186 | { |
| 1187 | |
| 1188 | if (usb_pipetype(pipe) != PIPE_BULK) { |
| 1189 | debug("non-bulk pipe (type=%lu)", usb_pipetype(pipe)); |
| 1190 | return -1; |
| 1191 | } |
| 1192 | return ehci_submit_async(dev, pipe, buffer, length, NULL); |
| 1193 | } |
| 1194 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1195 | static int _ehci_submit_control_msg(struct usb_device *dev, unsigned long pipe, |
| 1196 | void *buffer, int length, |
| 1197 | struct devrequest *setup) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1198 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1199 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1200 | |
| 1201 | if (usb_pipetype(pipe) != PIPE_CONTROL) { |
| 1202 | debug("non-control pipe (type=%lu)", usb_pipetype(pipe)); |
| 1203 | return -1; |
| 1204 | } |
| 1205 | |
Lucas Stach | 3494a4c | 2012-09-26 00:14:35 +0200 | [diff] [blame] | 1206 | if (usb_pipedevice(pipe) == ctrl->rootdev) { |
| 1207 | if (!ctrl->rootdev) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1208 | dev->speed = USB_SPEED_HIGH; |
| 1209 | return ehci_submit_root(dev, pipe, buffer, length, setup); |
| 1210 | } |
| 1211 | return ehci_submit_async(dev, pipe, buffer, length, setup); |
| 1212 | } |
| 1213 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1214 | struct int_queue { |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1215 | int elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1216 | unsigned long pipe; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1217 | struct QH *first; |
| 1218 | struct QH *current; |
| 1219 | struct QH *last; |
| 1220 | struct qTD *tds; |
| 1221 | }; |
| 1222 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1223 | #define NEXT_QH(qh) (struct QH *)((unsigned long)hc32_to_cpu((qh)->qh_link) & ~0x1f) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1224 | |
| 1225 | static int |
| 1226 | enable_periodic(struct ehci_ctrl *ctrl) |
| 1227 | { |
| 1228 | uint32_t cmd; |
| 1229 | struct ehci_hcor *hcor = ctrl->hcor; |
| 1230 | int ret; |
| 1231 | |
| 1232 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 1233 | cmd |= CMD_PSE; |
| 1234 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 1235 | |
| 1236 | ret = handshake((uint32_t *)&hcor->or_usbsts, |
| 1237 | STS_PSS, STS_PSS, 100 * 1000); |
| 1238 | if (ret < 0) { |
| 1239 | printf("EHCI failed: timeout when enabling periodic list\n"); |
| 1240 | return -ETIMEDOUT; |
| 1241 | } |
| 1242 | udelay(1000); |
| 1243 | return 0; |
| 1244 | } |
| 1245 | |
| 1246 | static int |
| 1247 | disable_periodic(struct ehci_ctrl *ctrl) |
| 1248 | { |
| 1249 | uint32_t cmd; |
| 1250 | struct ehci_hcor *hcor = ctrl->hcor; |
| 1251 | int ret; |
| 1252 | |
| 1253 | cmd = ehci_readl(&hcor->or_usbcmd); |
| 1254 | cmd &= ~CMD_PSE; |
| 1255 | ehci_writel(&hcor->or_usbcmd, cmd); |
| 1256 | |
| 1257 | ret = handshake((uint32_t *)&hcor->or_usbsts, |
| 1258 | STS_PSS, 0, 100 * 1000); |
| 1259 | if (ret < 0) { |
| 1260 | printf("EHCI failed: timeout when disabling periodic list\n"); |
| 1261 | return -ETIMEDOUT; |
| 1262 | } |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1266 | static struct int_queue *_ehci_create_int_queue(struct usb_device *dev, |
| 1267 | unsigned long pipe, int queuesize, int elementsize, |
| 1268 | void *buffer, int interval) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1269 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1270 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1271 | struct int_queue *result = NULL; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1272 | uint32_t i, toggle; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1273 | |
Hans de Goede | 7f7cb73 | 2014-09-24 14:06:04 +0200 | [diff] [blame] | 1274 | /* |
| 1275 | * Interrupt transfers requiring several transactions are not supported |
| 1276 | * because bInterval is ignored. |
| 1277 | * |
| 1278 | * Also, ehci_submit_async() relies on wMaxPacketSize being a power of 2 |
| 1279 | * <= PKT_ALIGN if several qTDs are required, while the USB |
| 1280 | * specification does not constrain this for interrupt transfers. That |
| 1281 | * means that ehci_submit_async() would support interrupt transfers |
| 1282 | * requiring several transactions only as long as the transfer size does |
| 1283 | * not require more than a single qTD. |
| 1284 | */ |
| 1285 | if (elementsize > usb_maxpacket(dev, pipe)) { |
| 1286 | printf("%s: xfers requiring several transactions are not supported.\n", |
| 1287 | __func__); |
| 1288 | return NULL; |
| 1289 | } |
| 1290 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1291 | debug("Enter create_int_queue\n"); |
| 1292 | if (usb_pipetype(pipe) != PIPE_INTERRUPT) { |
| 1293 | debug("non-interrupt pipe (type=%lu)", usb_pipetype(pipe)); |
| 1294 | return NULL; |
| 1295 | } |
| 1296 | |
| 1297 | /* limit to 4 full pages worth of data - |
| 1298 | * we can safely fit them in a single TD, |
| 1299 | * no matter the alignment |
| 1300 | */ |
| 1301 | if (elementsize >= 16384) { |
| 1302 | debug("too large elements for interrupt transfers\n"); |
| 1303 | return NULL; |
| 1304 | } |
| 1305 | |
| 1306 | result = malloc(sizeof(*result)); |
| 1307 | if (!result) { |
| 1308 | debug("ehci intr queue: out of memory\n"); |
| 1309 | goto fail1; |
| 1310 | } |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1311 | result->elementsize = elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1312 | result->pipe = pipe; |
Stephen Warren | d7fe61d | 2014-02-06 13:13:06 -0700 | [diff] [blame] | 1313 | result->first = memalign(USB_DMA_MINALIGN, |
| 1314 | sizeof(struct QH) * queuesize); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1315 | if (!result->first) { |
| 1316 | debug("ehci intr queue: out of memory\n"); |
| 1317 | goto fail2; |
| 1318 | } |
| 1319 | result->current = result->first; |
| 1320 | result->last = result->first + queuesize - 1; |
Stephen Warren | d7fe61d | 2014-02-06 13:13:06 -0700 | [diff] [blame] | 1321 | result->tds = memalign(USB_DMA_MINALIGN, |
| 1322 | sizeof(struct qTD) * queuesize); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1323 | if (!result->tds) { |
| 1324 | debug("ehci intr queue: out of memory\n"); |
| 1325 | goto fail3; |
| 1326 | } |
| 1327 | memset(result->first, 0, sizeof(struct QH) * queuesize); |
| 1328 | memset(result->tds, 0, sizeof(struct qTD) * queuesize); |
| 1329 | |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1330 | toggle = usb_gettoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe)); |
| 1331 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1332 | for (i = 0; i < queuesize; i++) { |
| 1333 | struct QH *qh = result->first + i; |
| 1334 | struct qTD *td = result->tds + i; |
| 1335 | void **buf = &qh->buffer; |
| 1336 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1337 | qh->qh_link = cpu_to_hc32((unsigned long)(qh+1) | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1338 | if (i == queuesize - 1) |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1339 | qh->qh_link = cpu_to_hc32(QH_LINK_TERMINATE); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1340 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1341 | qh->qh_overlay.qt_next = cpu_to_hc32((unsigned long)td); |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1342 | qh->qh_overlay.qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1343 | qh->qh_endpt1 = |
| 1344 | cpu_to_hc32((0 << 28) | /* No NAK reload (ehci 4.9) */ |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1345 | (usb_maxpacket(dev, pipe) << 16) | /* MPS */ |
| 1346 | (1 << 14) | |
| 1347 | QH_ENDPT1_EPS(ehci_encode_speed(dev->speed)) | |
| 1348 | (usb_pipeendpoint(pipe) << 8) | /* Endpoint Number */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1349 | (usb_pipedevice(pipe) << 0)); |
| 1350 | qh->qh_endpt2 = cpu_to_hc32((1 << 30) | /* 1 Tx per mframe */ |
| 1351 | (1 << 0)); /* S-mask: microframe 0 */ |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1352 | if (dev->speed == USB_SPEED_LOW || |
| 1353 | dev->speed == USB_SPEED_FULL) { |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 1354 | /* C-mask: microframes 2-4 */ |
| 1355 | qh->qh_endpt2 |= cpu_to_hc32((0x1c << 8)); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1356 | } |
Hans de Goede | da16677 | 2014-09-20 16:51:22 +0200 | [diff] [blame] | 1357 | ehci_update_endpt2_dev_n_port(dev, qh); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1358 | |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1359 | td->qt_next = cpu_to_hc32(QT_NEXT_TERMINATE); |
| 1360 | td->qt_altnext = cpu_to_hc32(QT_NEXT_TERMINATE); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1361 | debug("communication direction is '%s'\n", |
| 1362 | usb_pipein(pipe) ? "in" : "out"); |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1363 | td->qt_token = cpu_to_hc32( |
| 1364 | QT_TOKEN_DT(toggle) | |
| 1365 | (elementsize << 16) | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1366 | ((usb_pipein(pipe) ? 1 : 0) << 8) | /* IN/OUT token */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1367 | 0x80); /* active */ |
| 1368 | td->qt_buffer[0] = |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1369 | cpu_to_hc32((unsigned long)buffer + i * elementsize); |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1370 | td->qt_buffer[1] = |
| 1371 | cpu_to_hc32((td->qt_buffer[0] + 0x1000) & ~0xfff); |
| 1372 | td->qt_buffer[2] = |
| 1373 | cpu_to_hc32((td->qt_buffer[0] + 0x2000) & ~0xfff); |
| 1374 | td->qt_buffer[3] = |
| 1375 | cpu_to_hc32((td->qt_buffer[0] + 0x3000) & ~0xfff); |
| 1376 | td->qt_buffer[4] = |
| 1377 | cpu_to_hc32((td->qt_buffer[0] + 0x4000) & ~0xfff); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1378 | |
| 1379 | *buf = buffer + i * elementsize; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1380 | toggle ^= 1; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1381 | } |
| 1382 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1383 | flush_dcache_range((unsigned long)buffer, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1384 | ALIGN_END_ADDR(char, buffer, |
| 1385 | queuesize * elementsize)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1386 | flush_dcache_range((unsigned long)result->first, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1387 | ALIGN_END_ADDR(struct QH, result->first, |
| 1388 | queuesize)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1389 | flush_dcache_range((unsigned long)result->tds, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1390 | ALIGN_END_ADDR(struct qTD, result->tds, |
| 1391 | queuesize)); |
| 1392 | |
Hans de Goede | 8ba55ed | 2014-09-24 14:06:03 +0200 | [diff] [blame] | 1393 | if (ctrl->periodic_schedules > 0) { |
| 1394 | if (disable_periodic(ctrl) < 0) { |
| 1395 | debug("FATAL: periodic should never fail, but did"); |
| 1396 | goto fail3; |
| 1397 | } |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
| 1400 | /* hook up to periodic list */ |
| 1401 | struct QH *list = &ctrl->periodic_queue; |
| 1402 | result->last->qh_link = list->qh_link; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1403 | list->qh_link = cpu_to_hc32((unsigned long)result->first | QH_LINK_TYPE_QH); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1404 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1405 | flush_dcache_range((unsigned long)result->last, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1406 | ALIGN_END_ADDR(struct QH, result->last, 1)); |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1407 | flush_dcache_range((unsigned long)list, |
Stephen Warren | 36dad66 | 2013-05-24 15:03:17 -0600 | [diff] [blame] | 1408 | ALIGN_END_ADDR(struct QH, list, 1)); |
| 1409 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1410 | if (enable_periodic(ctrl) < 0) { |
| 1411 | debug("FATAL: periodic should never fail, but did"); |
| 1412 | goto fail3; |
| 1413 | } |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1414 | ctrl->periodic_schedules++; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1415 | |
| 1416 | debug("Exit create_int_queue\n"); |
| 1417 | return result; |
| 1418 | fail3: |
Heinrich Schuchardt | 4b88d6f | 2020-04-19 12:02:28 +0200 | [diff] [blame] | 1419 | free(result->tds); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1420 | fail2: |
Heinrich Schuchardt | 4b88d6f | 2020-04-19 12:02:28 +0200 | [diff] [blame] | 1421 | free(result->first); |
| 1422 | free(result); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1423 | fail1: |
| 1424 | return NULL; |
| 1425 | } |
| 1426 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1427 | static void *_ehci_poll_int_queue(struct usb_device *dev, |
| 1428 | struct int_queue *queue) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1429 | { |
| 1430 | struct QH *cur = queue->current; |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1431 | struct qTD *cur_td; |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1432 | uint32_t token, toggle; |
| 1433 | unsigned long pipe = queue->pipe; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1434 | |
| 1435 | /* depleted queue */ |
| 1436 | if (cur == NULL) { |
| 1437 | debug("Exit poll_int_queue with completed queue\n"); |
| 1438 | return NULL; |
| 1439 | } |
| 1440 | /* still active */ |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1441 | cur_td = &queue->tds[queue->current - queue->first]; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1442 | invalidate_dcache_range((unsigned long)cur_td, |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1443 | ALIGN_END_ADDR(struct qTD, cur_td, 1)); |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1444 | token = hc32_to_cpu(cur_td->qt_token); |
| 1445 | if (QT_TOKEN_GET_STATUS(token) & QT_TOKEN_STATUS_ACTIVE) { |
| 1446 | debug("Exit poll_int_queue with no completed intr transfer. token is %x\n", token); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1447 | return NULL; |
| 1448 | } |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1449 | |
| 1450 | toggle = QT_TOKEN_GET_DT(token); |
| 1451 | usb_settoggle(dev, usb_pipeendpoint(pipe), usb_pipeout(pipe), toggle); |
| 1452 | |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1453 | if (!(cur->qh_link & QH_LINK_TERMINATE)) |
| 1454 | queue->current++; |
| 1455 | else |
| 1456 | queue->current = NULL; |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1457 | |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1458 | invalidate_dcache_range((unsigned long)cur->buffer, |
Hans de Goede | 8c5c5ca | 2014-09-24 14:06:05 +0200 | [diff] [blame] | 1459 | ALIGN_END_ADDR(char, cur->buffer, |
| 1460 | queue->elementsize)); |
| 1461 | |
Hans de Goede | 9db174c | 2014-09-20 16:51:24 +0200 | [diff] [blame] | 1462 | debug("Exit poll_int_queue with completed intr transfer. token is %x at %p (first at %p)\n", |
Hans de Goede | 61a5a1c | 2015-06-18 22:34:33 +0200 | [diff] [blame] | 1463 | token, cur, queue->first); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1464 | return cur->buffer; |
| 1465 | } |
| 1466 | |
| 1467 | /* Do not free buffers associated with QHs, they're owned by someone else */ |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1468 | static int _ehci_destroy_int_queue(struct usb_device *dev, |
| 1469 | struct int_queue *queue) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1470 | { |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1471 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1472 | int result = -1; |
| 1473 | unsigned long timeout; |
| 1474 | |
| 1475 | if (disable_periodic(ctrl) < 0) { |
| 1476 | debug("FATAL: periodic should never fail, but did"); |
| 1477 | goto out; |
| 1478 | } |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1479 | ctrl->periodic_schedules--; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1480 | |
| 1481 | struct QH *cur = &ctrl->periodic_queue; |
| 1482 | timeout = get_timer(0) + 500; /* abort after 500ms */ |
Adrian Cox | 29d0587 | 2014-04-10 13:29:45 +0100 | [diff] [blame] | 1483 | while (!(cur->qh_link & cpu_to_hc32(QH_LINK_TERMINATE))) { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1484 | debug("considering %p, with qh_link %x\n", cur, cur->qh_link); |
| 1485 | if (NEXT_QH(cur) == queue->first) { |
| 1486 | debug("found candidate. removing from chain\n"); |
| 1487 | cur->qh_link = queue->last->qh_link; |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1488 | flush_dcache_range((unsigned long)cur, |
Hans de Goede | 8e00cf6 | 2014-09-20 16:51:23 +0200 | [diff] [blame] | 1489 | ALIGN_END_ADDR(struct QH, cur, 1)); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1490 | result = 0; |
| 1491 | break; |
| 1492 | } |
| 1493 | cur = NEXT_QH(cur); |
| 1494 | if (get_timer(0) > timeout) { |
| 1495 | printf("Timeout destroying interrupt endpoint queue\n"); |
| 1496 | result = -1; |
| 1497 | goto out; |
| 1498 | } |
| 1499 | } |
| 1500 | |
Hans de Goede | 8f5f4f7 | 2014-09-20 16:51:25 +0200 | [diff] [blame] | 1501 | if (ctrl->periodic_schedules > 0) { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1502 | result = enable_periodic(ctrl); |
| 1503 | if (result < 0) |
| 1504 | debug("FATAL: periodic should never fail, but did"); |
| 1505 | } |
| 1506 | |
| 1507 | out: |
| 1508 | free(queue->tds); |
| 1509 | free(queue->first); |
| 1510 | free(queue); |
| 1511 | |
| 1512 | return result; |
| 1513 | } |
| 1514 | |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1515 | static int _ehci_submit_int_msg(struct usb_device *dev, unsigned long pipe, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1516 | void *buffer, int length, int interval, |
| 1517 | bool nonblock) |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1518 | { |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1519 | void *backbuffer; |
| 1520 | struct int_queue *queue; |
| 1521 | unsigned long timeout; |
| 1522 | int result = 0, ret; |
| 1523 | |
Michael Trimarchi | 241f751 | 2008-11-28 13:20:46 +0100 | [diff] [blame] | 1524 | debug("dev=%p, pipe=%lu, buffer=%p, length=%d, interval=%d", |
| 1525 | dev, pipe, buffer, length, interval); |
Benoît Thébaudeau | 58c4dfb | 2012-08-09 23:50:44 +0200 | [diff] [blame] | 1526 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1527 | queue = _ehci_create_int_queue(dev, pipe, 1, length, buffer, interval); |
Hans de Goede | 7f7cb73 | 2014-09-24 14:06:04 +0200 | [diff] [blame] | 1528 | if (!queue) |
| 1529 | return -1; |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1530 | |
| 1531 | timeout = get_timer(0) + USB_TIMEOUT_MS(pipe); |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1532 | while ((backbuffer = _ehci_poll_int_queue(dev, queue)) == NULL) |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1533 | if (get_timer(0) > timeout) { |
| 1534 | printf("Timeout poll on interrupt endpoint\n"); |
| 1535 | result = -ETIMEDOUT; |
| 1536 | break; |
| 1537 | } |
| 1538 | |
| 1539 | if (backbuffer != buffer) { |
Rob Herring | f14d54b | 2015-03-17 15:46:37 -0500 | [diff] [blame] | 1540 | debug("got wrong buffer back (%p instead of %p)\n", |
| 1541 | backbuffer, buffer); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1542 | return -EINVAL; |
| 1543 | } |
| 1544 | |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1545 | ret = _ehci_destroy_int_queue(dev, queue); |
Patrick Georgi | e55fdac | 2013-03-06 14:08:31 +0000 | [diff] [blame] | 1546 | if (ret < 0) |
| 1547 | return ret; |
| 1548 | |
| 1549 | /* everything worked out fine */ |
| 1550 | return result; |
Marek Vasut | 9b315fe | 2011-09-25 21:07:56 +0200 | [diff] [blame] | 1551 | } |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1552 | |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 1553 | static int _ehci_lock_async(struct ehci_ctrl *ctrl, int lock) |
| 1554 | { |
| 1555 | ctrl->async_locked = lock; |
| 1556 | |
| 1557 | if (lock) |
| 1558 | return 0; |
| 1559 | |
| 1560 | return ehci_disable_async(ctrl); |
| 1561 | } |
| 1562 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1563 | #if !CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1564 | int submit_bulk_msg(struct usb_device *dev, unsigned long pipe, |
| 1565 | void *buffer, int length) |
| 1566 | { |
| 1567 | return _ehci_submit_bulk_msg(dev, pipe, buffer, length); |
| 1568 | } |
| 1569 | |
| 1570 | int submit_control_msg(struct usb_device *dev, unsigned long pipe, void *buffer, |
| 1571 | int length, struct devrequest *setup) |
| 1572 | { |
| 1573 | return _ehci_submit_control_msg(dev, pipe, buffer, length, setup); |
| 1574 | } |
| 1575 | |
| 1576 | int submit_int_msg(struct usb_device *dev, unsigned long pipe, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1577 | void *buffer, int length, int interval, bool nonblock) |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1578 | { |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1579 | return _ehci_submit_int_msg(dev, pipe, buffer, length, interval, |
| 1580 | nonblock); |
Simon Glass | cb7cf60 | 2015-03-25 12:22:25 -0600 | [diff] [blame] | 1581 | } |
Hans de Goede | 53ca9de | 2015-05-11 20:43:52 +0200 | [diff] [blame] | 1582 | |
| 1583 | struct int_queue *create_int_queue(struct usb_device *dev, |
| 1584 | unsigned long pipe, int queuesize, int elementsize, |
| 1585 | void *buffer, int interval) |
| 1586 | { |
| 1587 | return _ehci_create_int_queue(dev, pipe, queuesize, elementsize, |
| 1588 | buffer, interval); |
| 1589 | } |
| 1590 | |
| 1591 | void *poll_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 1592 | { |
| 1593 | return _ehci_poll_int_queue(dev, queue); |
| 1594 | } |
| 1595 | |
| 1596 | int destroy_int_queue(struct usb_device *dev, struct int_queue *queue) |
| 1597 | { |
| 1598 | return _ehci_destroy_int_queue(dev, queue); |
| 1599 | } |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 1600 | |
| 1601 | int usb_lock_async(struct usb_device *dev, int lock) |
| 1602 | { |
| 1603 | struct ehci_ctrl *ctrl = ehci_get_ctrl(dev); |
| 1604 | |
| 1605 | return _ehci_lock_async(ctrl, lock); |
| 1606 | } |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1607 | #endif |
| 1608 | |
Sven Schwermer | 8a3cb9f1 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 1609 | #if CONFIG_IS_ENABLED(DM_USB) |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1610 | static int ehci_submit_control_msg(struct udevice *dev, struct usb_device *udev, |
| 1611 | unsigned long pipe, void *buffer, int length, |
| 1612 | struct devrequest *setup) |
| 1613 | { |
| 1614 | debug("%s: dev='%s', udev=%p, udev->dev='%s', portnr=%d\n", __func__, |
| 1615 | dev->name, udev, udev->dev->name, udev->portnr); |
| 1616 | |
| 1617 | return _ehci_submit_control_msg(udev, pipe, buffer, length, setup); |
| 1618 | } |
| 1619 | |
| 1620 | static int ehci_submit_bulk_msg(struct udevice *dev, struct usb_device *udev, |
| 1621 | unsigned long pipe, void *buffer, int length) |
| 1622 | { |
| 1623 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1624 | return _ehci_submit_bulk_msg(udev, pipe, buffer, length); |
| 1625 | } |
| 1626 | |
| 1627 | static int ehci_submit_int_msg(struct udevice *dev, struct usb_device *udev, |
| 1628 | unsigned long pipe, void *buffer, int length, |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1629 | int interval, bool nonblock) |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1630 | { |
| 1631 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
Michal Suchanek | 1c95b9f | 2019-08-18 10:55:27 +0200 | [diff] [blame] | 1632 | return _ehci_submit_int_msg(udev, pipe, buffer, length, interval, |
| 1633 | nonblock); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1634 | } |
| 1635 | |
Hans de Goede | 0a7fa27 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 1636 | static struct int_queue *ehci_create_int_queue(struct udevice *dev, |
| 1637 | struct usb_device *udev, unsigned long pipe, int queuesize, |
| 1638 | int elementsize, void *buffer, int interval) |
| 1639 | { |
| 1640 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1641 | return _ehci_create_int_queue(udev, pipe, queuesize, elementsize, |
| 1642 | buffer, interval); |
| 1643 | } |
| 1644 | |
| 1645 | static void *ehci_poll_int_queue(struct udevice *dev, struct usb_device *udev, |
| 1646 | struct int_queue *queue) |
| 1647 | { |
| 1648 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1649 | return _ehci_poll_int_queue(udev, queue); |
| 1650 | } |
| 1651 | |
| 1652 | static int ehci_destroy_int_queue(struct udevice *dev, struct usb_device *udev, |
| 1653 | struct int_queue *queue) |
| 1654 | { |
| 1655 | debug("%s: dev='%s', udev=%p\n", __func__, dev->name, udev); |
| 1656 | return _ehci_destroy_int_queue(udev, queue); |
| 1657 | } |
| 1658 | |
Bin Meng | 0441b87 | 2017-09-07 06:13:19 -0700 | [diff] [blame] | 1659 | static int ehci_get_max_xfer_size(struct udevice *dev, size_t *size) |
| 1660 | { |
| 1661 | /* |
| 1662 | * EHCD can handle any transfer length as long as there is enough |
| 1663 | * free heap space left, hence set the theoretical max number here. |
| 1664 | */ |
| 1665 | *size = SIZE_MAX; |
| 1666 | |
| 1667 | return 0; |
| 1668 | } |
| 1669 | |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 1670 | static int ehci_lock_async(struct udevice *dev, int lock) |
| 1671 | { |
| 1672 | struct ehci_ctrl *ctrl = dev_get_priv(dev); |
| 1673 | |
| 1674 | return _ehci_lock_async(ctrl, lock); |
| 1675 | } |
| 1676 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1677 | int ehci_register(struct udevice *dev, struct ehci_hccr *hccr, |
| 1678 | struct ehci_hcor *hcor, const struct ehci_ops *ops, |
| 1679 | uint tweaks, enum usb_init_type init) |
| 1680 | { |
Hans de Goede | 76bc7f4 | 2015-05-05 11:54:35 +0200 | [diff] [blame] | 1681 | struct usb_bus_priv *priv = dev_get_uclass_priv(dev); |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1682 | struct ehci_ctrl *ctrl = dev_get_priv(dev); |
Heinrich Schuchardt | ab4304b | 2017-11-20 19:33:39 +0100 | [diff] [blame] | 1683 | int ret = -1; |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1684 | |
| 1685 | debug("%s: dev='%s', ctrl=%p, hccr=%p, hcor=%p, init=%d\n", __func__, |
| 1686 | dev->name, ctrl, hccr, hcor, init); |
| 1687 | |
Heinrich Schuchardt | ab4304b | 2017-11-20 19:33:39 +0100 | [diff] [blame] | 1688 | if (!ctrl || !hccr || !hcor) |
| 1689 | goto err; |
| 1690 | |
Hans de Goede | 76bc7f4 | 2015-05-05 11:54:35 +0200 | [diff] [blame] | 1691 | priv->desc_before_addr = true; |
| 1692 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1693 | ehci_setup_ops(ctrl, ops); |
| 1694 | ctrl->hccr = hccr; |
| 1695 | ctrl->hcor = hcor; |
| 1696 | ctrl->priv = ctrl; |
| 1697 | |
Stephen Warren | 71eced3 | 2015-08-20 17:38:05 -0600 | [diff] [blame] | 1698 | ctrl->init = init; |
| 1699 | if (ctrl->init == USB_INIT_DEVICE) |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1700 | goto done; |
Stephen Warren | 71eced3 | 2015-08-20 17:38:05 -0600 | [diff] [blame] | 1701 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1702 | ret = ehci_reset(ctrl); |
| 1703 | if (ret) |
| 1704 | goto err; |
| 1705 | |
Mateusz Kulikowski | 3e13f39 | 2016-04-03 13:38:26 +0200 | [diff] [blame] | 1706 | if (ctrl->ops.init_after_reset) { |
| 1707 | ret = ctrl->ops.init_after_reset(ctrl); |
Mateusz Kulikowski | aab5a5a | 2016-03-31 23:12:17 +0200 | [diff] [blame] | 1708 | if (ret) |
| 1709 | goto err; |
| 1710 | } |
| 1711 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1712 | ret = ehci_common_init(ctrl, tweaks); |
| 1713 | if (ret) |
| 1714 | goto err; |
| 1715 | done: |
| 1716 | return 0; |
| 1717 | err: |
| 1718 | free(ctrl); |
| 1719 | debug("%s: failed, ret=%d\n", __func__, ret); |
| 1720 | return ret; |
| 1721 | } |
| 1722 | |
| 1723 | int ehci_deregister(struct udevice *dev) |
| 1724 | { |
| 1725 | struct ehci_ctrl *ctrl = dev_get_priv(dev); |
| 1726 | |
Stephen Warren | 71eced3 | 2015-08-20 17:38:05 -0600 | [diff] [blame] | 1727 | if (ctrl->init == USB_INIT_DEVICE) |
| 1728 | return 0; |
| 1729 | |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1730 | ehci_shutdown(ctrl); |
| 1731 | |
| 1732 | return 0; |
| 1733 | } |
| 1734 | |
| 1735 | struct dm_usb_ops ehci_usb_ops = { |
| 1736 | .control = ehci_submit_control_msg, |
| 1737 | .bulk = ehci_submit_bulk_msg, |
| 1738 | .interrupt = ehci_submit_int_msg, |
Hans de Goede | 0a7fa27 | 2015-05-10 14:10:18 +0200 | [diff] [blame] | 1739 | .create_int_queue = ehci_create_int_queue, |
| 1740 | .poll_int_queue = ehci_poll_int_queue, |
| 1741 | .destroy_int_queue = ehci_destroy_int_queue, |
Bin Meng | 0441b87 | 2017-09-07 06:13:19 -0700 | [diff] [blame] | 1742 | .get_max_xfer_size = ehci_get_max_xfer_size, |
Marek Vasut | 118a903 | 2020-04-06 14:29:44 +0200 | [diff] [blame] | 1743 | .lock_async = ehci_lock_async, |
Simon Glass | a194b25 | 2015-03-25 12:22:29 -0600 | [diff] [blame] | 1744 | }; |
| 1745 | |
| 1746 | #endif |
Marek Vasut | d9af6cd | 2018-08-08 14:29:55 +0200 | [diff] [blame] | 1747 | |
| 1748 | #ifdef CONFIG_PHY |
| 1749 | int ehci_setup_phy(struct udevice *dev, struct phy *phy, int index) |
| 1750 | { |
| 1751 | int ret; |
| 1752 | |
| 1753 | if (!phy) |
| 1754 | return 0; |
| 1755 | |
| 1756 | ret = generic_phy_get_by_index(dev, index, phy); |
| 1757 | if (ret) { |
| 1758 | if (ret != -ENOENT) { |
| 1759 | dev_err(dev, "failed to get usb phy\n"); |
| 1760 | return ret; |
| 1761 | } |
| 1762 | } else { |
| 1763 | ret = generic_phy_init(phy); |
| 1764 | if (ret) { |
Patrick Delaunay | f62e739 | 2020-07-03 17:36:43 +0200 | [diff] [blame] | 1765 | dev_dbg(dev, "failed to init usb phy\n"); |
Marek Vasut | d9af6cd | 2018-08-08 14:29:55 +0200 | [diff] [blame] | 1766 | return ret; |
| 1767 | } |
| 1768 | |
| 1769 | ret = generic_phy_power_on(phy); |
| 1770 | if (ret) { |
Patrick Delaunay | f62e739 | 2020-07-03 17:36:43 +0200 | [diff] [blame] | 1771 | dev_dbg(dev, "failed to power on usb phy\n"); |
Marek Vasut | d9af6cd | 2018-08-08 14:29:55 +0200 | [diff] [blame] | 1772 | return generic_phy_exit(phy); |
| 1773 | } |
| 1774 | } |
| 1775 | |
| 1776 | return 0; |
| 1777 | } |
| 1778 | |
| 1779 | int ehci_shutdown_phy(struct udevice *dev, struct phy *phy) |
| 1780 | { |
| 1781 | int ret = 0; |
| 1782 | |
| 1783 | if (!phy) |
| 1784 | return 0; |
| 1785 | |
| 1786 | if (generic_phy_valid(phy)) { |
| 1787 | ret = generic_phy_power_off(phy); |
| 1788 | if (ret) { |
Patrick Delaunay | f62e739 | 2020-07-03 17:36:43 +0200 | [diff] [blame] | 1789 | dev_dbg(dev, "failed to power off usb phy\n"); |
Marek Vasut | d9af6cd | 2018-08-08 14:29:55 +0200 | [diff] [blame] | 1790 | return ret; |
| 1791 | } |
| 1792 | |
| 1793 | ret = generic_phy_exit(phy); |
| 1794 | if (ret) { |
Patrick Delaunay | f62e739 | 2020-07-03 17:36:43 +0200 | [diff] [blame] | 1795 | dev_dbg(dev, "failed to power off usb phy\n"); |
Marek Vasut | d9af6cd | 2018-08-08 14:29:55 +0200 | [diff] [blame] | 1796 | return ret; |
| 1797 | } |
| 1798 | } |
| 1799 | |
| 1800 | return 0; |
| 1801 | } |
| 1802 | #else |
| 1803 | int ehci_setup_phy(struct udevice *dev, struct phy *phy, int index) |
| 1804 | { |
| 1805 | return 0; |
| 1806 | } |
| 1807 | |
| 1808 | int ehci_shutdown_phy(struct udevice *dev, struct phy *phy) |
| 1809 | { |
| 1810 | return 0; |
| 1811 | } |
| 1812 | #endif |