Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2015 Sanchayan Maity <sanchayan.maity@toradex.com> |
| 4 | * Copyright (C) 2015 Toradex AG |
| 5 | * |
| 6 | * Based on ehci-mx6 driver |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 10 | #include <dm.h> |
Simon Glass | f7ae49f | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 11 | #include <log.h> |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 12 | #include <usb.h> |
| 13 | #include <errno.h> |
Simon Glass | 401d1c4 | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 14 | #include <asm/global_data.h> |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 15 | #include <linux/compiler.h> |
| 16 | #include <asm/io.h> |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 17 | #include <asm-generic/gpio.h> |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 18 | #include <asm/arch/clock.h> |
| 19 | #include <asm/arch/imx-regs.h> |
| 20 | #include <asm/arch/crm_regs.h> |
Stefano Babic | 552a848 | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 21 | #include <asm/mach-imx/iomux-v3.h> |
| 22 | #include <asm/mach-imx/regs-usbphy.h> |
Simon Glass | c05ed00 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 23 | #include <linux/delay.h> |
Mateusz Kulikowski | e162c6b | 2016-03-31 23:12:23 +0200 | [diff] [blame] | 24 | #include <usb/ehci-ci.h> |
Masahiro Yamada | b08c8c4 | 2018-03-05 01:20:11 +0900 | [diff] [blame] | 25 | #include <linux/libfdt.h> |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 26 | #include <fdtdec.h> |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 27 | |
| 28 | #include "ehci.h" |
| 29 | |
| 30 | #define USB_NC_REG_OFFSET 0x00000800 |
| 31 | |
| 32 | #define ANADIG_PLL_CTRL_EN_USB_CLKS (1 << 6) |
| 33 | |
| 34 | #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */ |
| 35 | #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */ |
| 36 | |
| 37 | /* USBCMD */ |
| 38 | #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */ |
| 39 | #define UCMD_RESET (1 << 1) /* controller reset */ |
| 40 | |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 41 | DECLARE_GLOBAL_DATA_PTR; |
| 42 | |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 43 | static const unsigned phy_bases[] = { |
| 44 | USB_PHY0_BASE_ADDR, |
| 45 | USB_PHY1_BASE_ADDR, |
| 46 | }; |
| 47 | |
| 48 | static const unsigned nc_reg_bases[] = { |
| 49 | USBC0_BASE_ADDR, |
| 50 | USBC1_BASE_ADDR, |
| 51 | }; |
| 52 | |
| 53 | static void usb_internal_phy_clock_gate(int index) |
| 54 | { |
| 55 | void __iomem *phy_reg; |
| 56 | |
| 57 | phy_reg = (void __iomem *)phy_bases[index]; |
| 58 | clrbits_le32(phy_reg + USBPHY_CTRL, USBPHY_CTRL_CLKGATE); |
| 59 | } |
| 60 | |
| 61 | static void usb_power_config(int index) |
| 62 | { |
| 63 | struct anadig_reg __iomem *anadig = |
| 64 | (struct anadig_reg __iomem *)ANADIG_BASE_ADDR; |
| 65 | void __iomem *pll_ctrl; |
| 66 | |
| 67 | switch (index) { |
| 68 | case 0: |
| 69 | pll_ctrl = &anadig->pll3_ctrl; |
| 70 | clrbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_BYPASS); |
| 71 | setbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_ENABLE |
| 72 | | ANADIG_PLL3_CTRL_POWERDOWN |
| 73 | | ANADIG_PLL_CTRL_EN_USB_CLKS); |
| 74 | break; |
| 75 | case 1: |
| 76 | pll_ctrl = &anadig->pll7_ctrl; |
| 77 | clrbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_BYPASS); |
| 78 | setbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_ENABLE |
| 79 | | ANADIG_PLL7_CTRL_POWERDOWN |
| 80 | | ANADIG_PLL_CTRL_EN_USB_CLKS); |
| 81 | break; |
| 82 | default: |
| 83 | return; |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | static void usb_phy_enable(int index, struct usb_ehci *ehci) |
| 88 | { |
| 89 | void __iomem *phy_reg; |
| 90 | void __iomem *phy_ctrl; |
| 91 | void __iomem *usb_cmd; |
| 92 | |
| 93 | phy_reg = (void __iomem *)phy_bases[index]; |
| 94 | phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); |
| 95 | usb_cmd = (void __iomem *)&ehci->usbcmd; |
| 96 | |
| 97 | /* Stop then Reset */ |
| 98 | clrbits_le32(usb_cmd, UCMD_RUN_STOP); |
| 99 | while (readl(usb_cmd) & UCMD_RUN_STOP) |
| 100 | ; |
| 101 | |
| 102 | setbits_le32(usb_cmd, UCMD_RESET); |
| 103 | while (readl(usb_cmd) & UCMD_RESET) |
| 104 | ; |
| 105 | |
| 106 | /* Reset USBPHY module */ |
| 107 | setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); |
| 108 | udelay(10); |
| 109 | |
| 110 | /* Remove CLKGATE and SFTRST */ |
| 111 | clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); |
| 112 | udelay(10); |
| 113 | |
| 114 | /* Power up the PHY */ |
| 115 | writel(0, phy_reg + USBPHY_PWD); |
| 116 | |
| 117 | /* Enable FS/LS device */ |
| 118 | setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | |
| 119 | USBPHY_CTRL_ENUTMILEVEL3); |
| 120 | } |
| 121 | |
| 122 | static void usb_oc_config(int index) |
| 123 | { |
| 124 | void __iomem *ctrl; |
| 125 | |
| 126 | ctrl = (void __iomem *)(nc_reg_bases[index] + USB_NC_REG_OFFSET); |
| 127 | |
| 128 | setbits_le32(ctrl, UCTRL_OVER_CUR_POL); |
| 129 | setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); |
| 130 | } |
| 131 | |
Sanchayan Maity | 08c11cb | 2015-10-26 18:28:50 +0530 | [diff] [blame] | 132 | int __weak board_usb_phy_mode(int port) |
| 133 | { |
| 134 | return 0; |
| 135 | } |
| 136 | |
Sanchayan Maity | 60ed286 | 2015-06-01 18:37:24 +0530 | [diff] [blame] | 137 | int __weak board_ehci_hcd_init(int port) |
| 138 | { |
| 139 | return 0; |
| 140 | } |
| 141 | |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 142 | int ehci_vf_common_init(struct usb_ehci *ehci, int index) |
| 143 | { |
| 144 | int ret; |
| 145 | |
| 146 | /* Do board specific initialisation */ |
| 147 | ret = board_ehci_hcd_init(index); |
| 148 | if (ret) |
| 149 | return ret; |
| 150 | |
| 151 | usb_power_config(index); |
| 152 | usb_oc_config(index); |
| 153 | usb_internal_phy_clock_gate(index); |
| 154 | usb_phy_enable(index, ehci); |
| 155 | |
| 156 | return 0; |
| 157 | } |
| 158 | |
Sven Schwermer | fd09c20 | 2018-11-21 08:43:56 +0100 | [diff] [blame] | 159 | #if !CONFIG_IS_ENABLED(DM_USB) |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 160 | int ehci_hcd_init(int index, enum usb_init_type init, |
| 161 | struct ehci_hccr **hccr, struct ehci_hcor **hcor) |
| 162 | { |
| 163 | struct usb_ehci *ehci; |
Sanchayan Maity | 08c11cb | 2015-10-26 18:28:50 +0530 | [diff] [blame] | 164 | enum usb_init_type type; |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 165 | int ret; |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 166 | |
| 167 | if (index >= ARRAY_SIZE(nc_reg_bases)) |
| 168 | return -EINVAL; |
| 169 | |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 170 | ehci = (struct usb_ehci *)nc_reg_bases[index]; |
| 171 | |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 172 | ret = ehci_vf_common_init(index); |
| 173 | if (ret) |
| 174 | return ret; |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 175 | |
| 176 | *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 177 | *hcor = (struct ehci_hcor *)((uint32_t)*hccr + |
| 178 | HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); |
| 179 | |
Sanchayan Maity | 08c11cb | 2015-10-26 18:28:50 +0530 | [diff] [blame] | 180 | type = board_usb_phy_mode(index); |
| 181 | if (type != init) |
| 182 | return -ENODEV; |
| 183 | |
Sanchayan Maity | a94bb7a | 2015-04-15 16:24:27 +0530 | [diff] [blame] | 184 | if (init == USB_INIT_DEVICE) { |
| 185 | setbits_le32(&ehci->usbmode, CM_DEVICE); |
| 186 | writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc); |
| 187 | setbits_le32(&ehci->portsc, USB_EN); |
| 188 | } else if (init == USB_INIT_HOST) { |
| 189 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 190 | writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc); |
| 191 | setbits_le32(&ehci->portsc, USB_EN); |
| 192 | } |
| 193 | |
| 194 | return 0; |
| 195 | } |
| 196 | |
| 197 | int ehci_hcd_stop(int index) |
| 198 | { |
| 199 | return 0; |
| 200 | } |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 201 | #else |
| 202 | /* Possible port types (dual role mode) */ |
| 203 | enum dr_mode { |
| 204 | DR_MODE_NONE = 0, |
| 205 | DR_MODE_HOST, /* supports host operation */ |
| 206 | DR_MODE_DEVICE, /* supports device operation */ |
| 207 | DR_MODE_OTG, /* supports both */ |
| 208 | }; |
| 209 | |
| 210 | struct ehci_vf_priv_data { |
| 211 | struct ehci_ctrl ctrl; |
| 212 | struct usb_ehci *ehci; |
| 213 | struct gpio_desc cdet_gpio; |
| 214 | enum usb_init_type init_type; |
| 215 | enum dr_mode dr_mode; |
| 216 | u32 portnr; |
| 217 | }; |
| 218 | |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 219 | static int vf_usb_of_to_plat(struct udevice *dev) |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 220 | { |
| 221 | struct ehci_vf_priv_data *priv = dev_get_priv(dev); |
| 222 | const void *dt_blob = gd->fdt_blob; |
Simon Glass | e160f7d | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 223 | int node = dev_of_offset(dev); |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 224 | const char *mode; |
| 225 | |
Simon Glass | 8b85dfc | 2020-12-16 21:20:07 -0700 | [diff] [blame] | 226 | priv->portnr = dev_seq(dev); |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 227 | |
Masahiro Yamada | 8613c8d | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 228 | priv->ehci = dev_read_addr_ptr(dev); |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 229 | mode = fdt_getprop(dt_blob, node, "dr_mode", NULL); |
| 230 | if (mode) { |
| 231 | if (0 == strcmp(mode, "host")) { |
| 232 | priv->dr_mode = DR_MODE_HOST; |
| 233 | priv->init_type = USB_INIT_HOST; |
| 234 | } else if (0 == strcmp(mode, "peripheral")) { |
| 235 | priv->dr_mode = DR_MODE_DEVICE; |
| 236 | priv->init_type = USB_INIT_DEVICE; |
| 237 | } else if (0 == strcmp(mode, "otg")) { |
| 238 | priv->dr_mode = DR_MODE_OTG; |
| 239 | /* |
| 240 | * We set init_type to device by default when OTG |
| 241 | * mode is requested. If a valid gpio is provided |
| 242 | * we will switch the init_type based on the state |
| 243 | * of the gpio pin. |
| 244 | */ |
| 245 | priv->init_type = USB_INIT_DEVICE; |
| 246 | } else { |
| 247 | debug("%s: Cannot decode dr_mode '%s'\n", |
| 248 | __func__, mode); |
| 249 | return -EINVAL; |
| 250 | } |
| 251 | } else { |
| 252 | priv->dr_mode = DR_MODE_HOST; |
| 253 | priv->init_type = USB_INIT_HOST; |
| 254 | } |
| 255 | |
| 256 | if (priv->dr_mode == DR_MODE_OTG) { |
Simon Glass | 150c5af | 2017-05-30 21:47:09 -0600 | [diff] [blame] | 257 | gpio_request_by_name_nodev(offset_to_ofnode(node), |
| 258 | "fsl,cdet-gpio", 0, &priv->cdet_gpio, |
| 259 | GPIOD_IS_IN); |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 260 | if (dm_gpio_is_valid(&priv->cdet_gpio)) { |
| 261 | if (dm_gpio_get_value(&priv->cdet_gpio)) |
| 262 | priv->init_type = USB_INIT_DEVICE; |
| 263 | else |
| 264 | priv->init_type = USB_INIT_HOST; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | static int vf_init_after_reset(struct ehci_ctrl *dev) |
| 272 | { |
| 273 | struct ehci_vf_priv_data *priv = dev->priv; |
| 274 | enum usb_init_type type = priv->init_type; |
| 275 | struct usb_ehci *ehci = priv->ehci; |
| 276 | int ret; |
| 277 | |
| 278 | ret = ehci_vf_common_init(priv->ehci, priv->portnr); |
| 279 | if (ret) |
| 280 | return ret; |
| 281 | |
| 282 | if (type == USB_INIT_DEVICE) |
| 283 | return 0; |
| 284 | |
| 285 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 286 | writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc); |
| 287 | setbits_le32(&ehci->portsc, USB_EN); |
| 288 | |
| 289 | mdelay(10); |
| 290 | |
| 291 | return 0; |
| 292 | } |
| 293 | |
| 294 | static const struct ehci_ops vf_ehci_ops = { |
| 295 | .init_after_reset = vf_init_after_reset |
| 296 | }; |
| 297 | |
| 298 | static int vf_usb_bind(struct udevice *dev) |
| 299 | { |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 300 | /* |
| 301 | * Without this hack, if we return ENODEV for USB Controller 0, on |
| 302 | * probe for the next controller, USB Controller 1 will be given a |
| 303 | * sequence number of 0. This conflicts with our requirement of |
| 304 | * sequence numbers while initialising the peripherals. |
Simon Glass | b27347f | 2020-12-16 21:20:21 -0700 | [diff] [blame] | 305 | * |
| 306 | * FIXME: Check that this still works OK with the new sequence numbers |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 307 | */ |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 308 | |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int ehci_usb_probe(struct udevice *dev) |
| 313 | { |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 314 | struct usb_plat *plat = dev_get_plat(dev); |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 315 | struct ehci_vf_priv_data *priv = dev_get_priv(dev); |
| 316 | struct usb_ehci *ehci = priv->ehci; |
| 317 | struct ehci_hccr *hccr; |
| 318 | struct ehci_hcor *hcor; |
| 319 | int ret; |
| 320 | |
| 321 | ret = ehci_vf_common_init(ehci, priv->portnr); |
| 322 | if (ret) |
| 323 | return ret; |
| 324 | |
| 325 | if (priv->init_type != plat->init_type) |
| 326 | return -ENODEV; |
| 327 | |
| 328 | if (priv->init_type == USB_INIT_HOST) { |
| 329 | setbits_le32(&ehci->usbmode, CM_HOST); |
| 330 | writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc); |
| 331 | setbits_le32(&ehci->portsc, USB_EN); |
| 332 | } |
| 333 | |
| 334 | mdelay(10); |
| 335 | |
| 336 | hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); |
| 337 | hcor = (struct ehci_hcor *)((uint32_t)hccr + |
| 338 | HC_LENGTH(ehci_readl(&hccr->cr_capbase))); |
| 339 | |
| 340 | return ehci_register(dev, hccr, hcor, &vf_ehci_ops, 0, priv->init_type); |
| 341 | } |
| 342 | |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 343 | static const struct udevice_id vf_usb_ids[] = { |
| 344 | { .compatible = "fsl,vf610-usb" }, |
| 345 | { } |
| 346 | }; |
| 347 | |
| 348 | U_BOOT_DRIVER(usb_ehci) = { |
| 349 | .name = "ehci_vf", |
| 350 | .id = UCLASS_USB, |
| 351 | .of_match = vf_usb_ids, |
| 352 | .bind = vf_usb_bind, |
| 353 | .probe = ehci_usb_probe, |
Masahiro Yamada | 99e2df4 | 2016-10-14 10:29:37 +0900 | [diff] [blame] | 354 | .remove = ehci_deregister, |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 355 | .ops = &ehci_usb_ops, |
Simon Glass | d1998a9 | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 356 | .of_to_plat = vf_usb_of_to_plat, |
Simon Glass | 8a8d24b | 2020-12-03 16:55:23 -0700 | [diff] [blame] | 357 | .plat_auto = sizeof(struct usb_plat), |
Simon Glass | 41575d8 | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 358 | .priv_auto = sizeof(struct ehci_vf_priv_data), |
Sanchayan Maity | 0885cdb | 2016-08-09 23:44:59 +0530 | [diff] [blame] | 359 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 360 | }; |
| 361 | #endif |