1 /* 2 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de> 3 * Copyright (C) 2010 Freescale Semiconductor, Inc. 4 * 5 * SPDX-License-Identifier: GPL-2.0+ 6 */ 7 8 #include <common.h> 9 #include <usb.h> 10 #include <errno.h> 11 #include <wait_bit.h> 12 #include <linux/compiler.h> 13 #include <usb/ehci-ci.h> 14 #include <asm/io.h> 15 #include <asm/arch/imx-regs.h> 16 #include <asm/arch/clock.h> 17 #include <asm/mach-imx/iomux-v3.h> 18 #include <asm/mach-imx/sys_proto.h> 19 #include <dm.h> 20 #include <asm/mach-types.h> 21 #include <power/regulator.h> 22 #include <linux/usb/otg.h> 23 24 #include "ehci.h" 25 26 DECLARE_GLOBAL_DATA_PTR; 27 28 #define USB_OTGREGS_OFFSET 0x000 29 #define USB_H1REGS_OFFSET 0x200 30 #define USB_H2REGS_OFFSET 0x400 31 #define USB_H3REGS_OFFSET 0x600 32 #define USB_OTHERREGS_OFFSET 0x800 33 34 #define USB_H1_CTRL_OFFSET 0x04 35 36 #define USBPHY_CTRL 0x00000030 37 #define USBPHY_CTRL_SET 0x00000034 38 #define USBPHY_CTRL_CLR 0x00000038 39 #define USBPHY_CTRL_TOG 0x0000003c 40 41 #define USBPHY_PWD 0x00000000 42 #define USBPHY_CTRL_SFTRST 0x80000000 43 #define USBPHY_CTRL_CLKGATE 0x40000000 44 #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000 45 #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000 46 #define USBPHY_CTRL_OTG_ID 0x08000000 47 48 #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000 49 #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000 50 51 #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000 52 #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000 53 #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000 54 #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040 55 56 #define USBNC_OFFSET 0x200 57 #define USBNC_PHY_STATUS_OFFSET 0x23C 58 #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */ 59 #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */ 60 #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */ 61 #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */ 62 #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */ 63 64 /* USBCMD */ 65 #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */ 66 #define UCMD_RESET (1 << 1) /* controller reset */ 67 68 #if defined(CONFIG_MX6) 69 static const unsigned phy_bases[] = { 70 USB_PHY0_BASE_ADDR, 71 USB_PHY1_BASE_ADDR, 72 }; 73 74 static void usb_internal_phy_clock_gate(int index, int on) 75 { 76 void __iomem *phy_reg; 77 78 if (index >= ARRAY_SIZE(phy_bases)) 79 return; 80 81 phy_reg = (void __iomem *)phy_bases[index]; 82 phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET; 83 writel(USBPHY_CTRL_CLKGATE, phy_reg); 84 } 85 86 static void usb_power_config(int index) 87 { 88 struct anatop_regs __iomem *anatop = 89 (struct anatop_regs __iomem *)ANATOP_BASE_ADDR; 90 void __iomem *chrg_detect; 91 void __iomem *pll_480_ctrl_clr; 92 void __iomem *pll_480_ctrl_set; 93 94 switch (index) { 95 case 0: 96 chrg_detect = &anatop->usb1_chrg_detect; 97 pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr; 98 pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set; 99 break; 100 case 1: 101 chrg_detect = &anatop->usb2_chrg_detect; 102 pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr; 103 pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set; 104 break; 105 default: 106 return; 107 } 108 /* 109 * Some phy and power's special controls 110 * 1. The external charger detector needs to be disabled 111 * or the signal at DP will be poor 112 * 2. The PLL's power and output to usb 113 * is totally controlled by IC, so the Software only needs 114 * to enable them at initializtion. 115 */ 116 writel(ANADIG_USB2_CHRG_DETECT_EN_B | 117 ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B, 118 chrg_detect); 119 120 writel(ANADIG_USB2_PLL_480_CTRL_BYPASS, 121 pll_480_ctrl_clr); 122 123 writel(ANADIG_USB2_PLL_480_CTRL_ENABLE | 124 ANADIG_USB2_PLL_480_CTRL_POWER | 125 ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS, 126 pll_480_ctrl_set); 127 } 128 129 /* Return 0 : host node, <>0 : device mode */ 130 static int usb_phy_enable(int index, struct usb_ehci *ehci) 131 { 132 void __iomem *phy_reg; 133 void __iomem *phy_ctrl; 134 void __iomem *usb_cmd; 135 int ret; 136 137 if (index >= ARRAY_SIZE(phy_bases)) 138 return 0; 139 140 phy_reg = (void __iomem *)phy_bases[index]; 141 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); 142 usb_cmd = (void __iomem *)&ehci->usbcmd; 143 144 /* Stop then Reset */ 145 clrbits_le32(usb_cmd, UCMD_RUN_STOP); 146 ret = wait_for_bit_le32(usb_cmd, UCMD_RUN_STOP, false, 10000, false); 147 if (ret) 148 return ret; 149 150 setbits_le32(usb_cmd, UCMD_RESET); 151 ret = wait_for_bit_le32(usb_cmd, UCMD_RESET, false, 10000, false); 152 if (ret) 153 return ret; 154 155 /* Reset USBPHY module */ 156 setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST); 157 udelay(10); 158 159 /* Remove CLKGATE and SFTRST */ 160 clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST); 161 udelay(10); 162 163 /* Power up the PHY */ 164 writel(0, phy_reg + USBPHY_PWD); 165 /* enable FS/LS device */ 166 setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 | 167 USBPHY_CTRL_ENUTMILEVEL3); 168 169 return 0; 170 } 171 172 int usb_phy_mode(int port) 173 { 174 void __iomem *phy_reg; 175 void __iomem *phy_ctrl; 176 u32 val; 177 178 phy_reg = (void __iomem *)phy_bases[port]; 179 phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL); 180 181 val = readl(phy_ctrl); 182 183 if (val & USBPHY_CTRL_OTG_ID) 184 return USB_INIT_DEVICE; 185 else 186 return USB_INIT_HOST; 187 } 188 189 /* Base address for this IP block is 0x02184800 */ 190 struct usbnc_regs { 191 u32 ctrl[4]; /* otg/host1-3 */ 192 u32 uh2_hsic_ctrl; 193 u32 uh3_hsic_ctrl; 194 u32 otg_phy_ctrl_0; 195 u32 uh1_phy_ctrl_0; 196 }; 197 #elif defined(CONFIG_MX7) 198 struct usbnc_regs { 199 u32 ctrl1; 200 u32 ctrl2; 201 u32 reserve1[10]; 202 u32 phy_cfg1; 203 u32 phy_cfg2; 204 u32 reserve2; 205 u32 phy_status; 206 u32 reserve3[4]; 207 u32 adp_cfg1; 208 u32 adp_cfg2; 209 u32 adp_status; 210 }; 211 212 static void usb_power_config(int index) 213 { 214 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + 215 (0x10000 * index) + USBNC_OFFSET); 216 void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2); 217 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); 218 219 /* 220 * Clear the ACAENB to enable usb_otg_id detection, 221 * otherwise it is the ACA detection enabled. 222 */ 223 clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB); 224 225 /* Set power polarity to high active */ 226 #ifdef CONFIG_MXC_USB_OTG_HACTIVE 227 setbits_le32(ctrl, UCTRL_PWR_POL); 228 #else 229 clrbits_le32(ctrl, UCTRL_PWR_POL); 230 #endif 231 } 232 233 int usb_phy_mode(int port) 234 { 235 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + 236 (0x10000 * port) + USBNC_OFFSET); 237 void __iomem *status = (void __iomem *)(&usbnc->phy_status); 238 u32 val; 239 240 val = readl(status); 241 242 if (val & USBNC_PHYSTATUS_ID_DIG) 243 return USB_INIT_DEVICE; 244 else 245 return USB_INIT_HOST; 246 } 247 #endif 248 249 static void usb_oc_config(int index) 250 { 251 #if defined(CONFIG_MX6) 252 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + 253 USB_OTHERREGS_OFFSET); 254 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]); 255 #elif defined(CONFIG_MX7) 256 struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR + 257 (0x10000 * index) + USBNC_OFFSET); 258 void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1); 259 #endif 260 261 #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2 262 /* mx6qarm2 seems to required a different setting*/ 263 clrbits_le32(ctrl, UCTRL_OVER_CUR_POL); 264 #else 265 setbits_le32(ctrl, UCTRL_OVER_CUR_POL); 266 #endif 267 268 setbits_le32(ctrl, UCTRL_OVER_CUR_DIS); 269 } 270 271 /** 272 * board_usb_phy_mode - override usb phy mode 273 * @port: usb host/otg port 274 * 275 * Target board specific, override usb_phy_mode. 276 * When usb-otg is used as usb host port, iomux pad usb_otg_id can be 277 * left disconnected in this case usb_phy_mode will not be able to identify 278 * the phy mode that usb port is used. 279 * Machine file overrides board_usb_phy_mode. 280 * 281 * Return: USB_INIT_DEVICE or USB_INIT_HOST 282 */ 283 int __weak board_usb_phy_mode(int port) 284 { 285 return usb_phy_mode(port); 286 } 287 288 /** 289 * board_ehci_hcd_init - set usb vbus voltage 290 * @port: usb otg port 291 * 292 * Target board specific, setup iomux pad to setup supply vbus voltage 293 * for usb otg port. Machine board file overrides board_ehci_hcd_init 294 * 295 * Return: 0 Success 296 */ 297 int __weak board_ehci_hcd_init(int port) 298 { 299 return 0; 300 } 301 302 /** 303 * board_ehci_power - enables/disables usb vbus voltage 304 * @port: usb otg port 305 * @on: on/off vbus voltage 306 * 307 * Enables/disables supply vbus voltage for usb otg port. 308 * Machine board file overrides board_ehci_power 309 * 310 * Return: 0 Success 311 */ 312 int __weak board_ehci_power(int port, int on) 313 { 314 return 0; 315 } 316 317 int ehci_mx6_common_init(struct usb_ehci *ehci, int index) 318 { 319 int ret; 320 321 enable_usboh3_clk(1); 322 mdelay(1); 323 324 /* Do board specific initialization */ 325 ret = board_ehci_hcd_init(index); 326 if (ret) 327 return ret; 328 329 usb_power_config(index); 330 usb_oc_config(index); 331 332 #if defined(CONFIG_MX6) 333 usb_internal_phy_clock_gate(index, 1); 334 usb_phy_enable(index, ehci); 335 #endif 336 337 return 0; 338 } 339 340 #if !CONFIG_IS_ENABLED(DM_USB) 341 int ehci_hcd_init(int index, enum usb_init_type init, 342 struct ehci_hccr **hccr, struct ehci_hcor **hcor) 343 { 344 enum usb_init_type type; 345 #if defined(CONFIG_MX6) 346 u32 controller_spacing = 0x200; 347 #elif defined(CONFIG_MX7) 348 u32 controller_spacing = 0x10000; 349 #endif 350 struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR + 351 (controller_spacing * index)); 352 int ret; 353 354 if (index > 3) 355 return -EINVAL; 356 357 ret = ehci_mx6_common_init(ehci, index); 358 if (ret) 359 return ret; 360 361 type = board_usb_phy_mode(index); 362 363 if (hccr && hcor) { 364 *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); 365 *hcor = (struct ehci_hcor *)((uint32_t)*hccr + 366 HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase))); 367 } 368 369 if ((type == init) || (type == USB_INIT_DEVICE)) 370 board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1); 371 if (type != init) 372 return -ENODEV; 373 if (type == USB_INIT_DEVICE) 374 return 0; 375 376 setbits_le32(&ehci->usbmode, CM_HOST); 377 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); 378 setbits_le32(&ehci->portsc, USB_EN); 379 380 mdelay(10); 381 382 return 0; 383 } 384 385 int ehci_hcd_stop(int index) 386 { 387 return 0; 388 } 389 #else 390 struct ehci_mx6_priv_data { 391 struct ehci_ctrl ctrl; 392 struct usb_ehci *ehci; 393 struct udevice *vbus_supply; 394 enum usb_init_type init_type; 395 int portnr; 396 }; 397 398 static int mx6_init_after_reset(struct ehci_ctrl *dev) 399 { 400 struct ehci_mx6_priv_data *priv = dev->priv; 401 enum usb_init_type type = priv->init_type; 402 struct usb_ehci *ehci = priv->ehci; 403 int ret; 404 405 ret = ehci_mx6_common_init(priv->ehci, priv->portnr); 406 if (ret) 407 return ret; 408 409 #if CONFIG_IS_ENABLED(DM_REGULATOR) 410 if (priv->vbus_supply) { 411 ret = regulator_set_enable(priv->vbus_supply, 412 (type == USB_INIT_DEVICE) ? 413 false : true); 414 if (ret) { 415 puts("Error enabling VBUS supply\n"); 416 return ret; 417 } 418 } 419 #endif 420 421 if (type == USB_INIT_DEVICE) 422 return 0; 423 424 setbits_le32(&ehci->usbmode, CM_HOST); 425 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); 426 setbits_le32(&ehci->portsc, USB_EN); 427 428 mdelay(10); 429 430 return 0; 431 } 432 433 static const struct ehci_ops mx6_ehci_ops = { 434 .init_after_reset = mx6_init_after_reset 435 }; 436 437 static int ehci_usb_phy_mode(struct udevice *dev) 438 { 439 struct usb_platdata *plat = dev_get_platdata(dev); 440 void *__iomem addr = (void *__iomem)devfdt_get_addr(dev); 441 void *__iomem phy_ctrl, *__iomem phy_status; 442 const void *blob = gd->fdt_blob; 443 int offset = dev_of_offset(dev), phy_off; 444 u32 val; 445 446 /* 447 * About fsl,usbphy, Refer to 448 * Documentation/devicetree/bindings/usb/ci-hdrc-usb2.txt. 449 */ 450 if (is_mx6()) { 451 phy_off = fdtdec_lookup_phandle(blob, 452 offset, 453 "fsl,usbphy"); 454 if (phy_off < 0) 455 return -EINVAL; 456 457 addr = (void __iomem *)fdtdec_get_addr(blob, phy_off, 458 "reg"); 459 if ((fdt_addr_t)addr == FDT_ADDR_T_NONE) 460 return -EINVAL; 461 462 phy_ctrl = (void __iomem *)(addr + USBPHY_CTRL); 463 val = readl(phy_ctrl); 464 465 if (val & USBPHY_CTRL_OTG_ID) 466 plat->init_type = USB_INIT_DEVICE; 467 else 468 plat->init_type = USB_INIT_HOST; 469 } else if (is_mx7()) { 470 phy_status = (void __iomem *)(addr + 471 USBNC_PHY_STATUS_OFFSET); 472 val = readl(phy_status); 473 474 if (val & USBNC_PHYSTATUS_ID_DIG) 475 plat->init_type = USB_INIT_DEVICE; 476 else 477 plat->init_type = USB_INIT_HOST; 478 } else { 479 return -EINVAL; 480 } 481 482 return 0; 483 } 484 485 static int ehci_usb_ofdata_to_platdata(struct udevice *dev) 486 { 487 struct usb_platdata *plat = dev_get_platdata(dev); 488 enum usb_dr_mode dr_mode; 489 490 dr_mode = usb_get_dr_mode(dev_of_offset(dev)); 491 492 switch (dr_mode) { 493 case USB_DR_MODE_HOST: 494 plat->init_type = USB_INIT_HOST; 495 break; 496 case USB_DR_MODE_PERIPHERAL: 497 plat->init_type = USB_INIT_DEVICE; 498 break; 499 case USB_DR_MODE_OTG: 500 case USB_DR_MODE_UNKNOWN: 501 return ehci_usb_phy_mode(dev); 502 }; 503 504 return 0; 505 } 506 507 static int ehci_usb_probe(struct udevice *dev) 508 { 509 struct usb_platdata *plat = dev_get_platdata(dev); 510 struct usb_ehci *ehci = (struct usb_ehci *)devfdt_get_addr(dev); 511 struct ehci_mx6_priv_data *priv = dev_get_priv(dev); 512 enum usb_init_type type = plat->init_type; 513 struct ehci_hccr *hccr; 514 struct ehci_hcor *hcor; 515 int ret; 516 517 priv->ehci = ehci; 518 priv->portnr = dev->seq; 519 priv->init_type = type; 520 521 #if CONFIG_IS_ENABLED(DM_REGULATOR) 522 ret = device_get_supply_regulator(dev, "vbus-supply", 523 &priv->vbus_supply); 524 if (ret) 525 debug("%s: No vbus supply\n", dev->name); 526 #endif 527 ret = ehci_mx6_common_init(ehci, priv->portnr); 528 if (ret) 529 return ret; 530 531 #if CONFIG_IS_ENABLED(DM_REGULATOR) 532 if (priv->vbus_supply) { 533 ret = regulator_set_enable(priv->vbus_supply, 534 (type == USB_INIT_DEVICE) ? 535 false : true); 536 if (ret) { 537 puts("Error enabling VBUS supply\n"); 538 return ret; 539 } 540 } 541 #endif 542 543 if (priv->init_type == USB_INIT_HOST) { 544 setbits_le32(&ehci->usbmode, CM_HOST); 545 writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc); 546 setbits_le32(&ehci->portsc, USB_EN); 547 } 548 549 mdelay(10); 550 551 hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength); 552 hcor = (struct ehci_hcor *)((uint32_t)hccr + 553 HC_LENGTH(ehci_readl(&(hccr)->cr_capbase))); 554 555 return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type); 556 } 557 558 static const struct udevice_id mx6_usb_ids[] = { 559 { .compatible = "fsl,imx27-usb" }, 560 { } 561 }; 562 563 U_BOOT_DRIVER(usb_mx6) = { 564 .name = "ehci_mx6", 565 .id = UCLASS_USB, 566 .of_match = mx6_usb_ids, 567 .ofdata_to_platdata = ehci_usb_ofdata_to_platdata, 568 .probe = ehci_usb_probe, 569 .remove = ehci_deregister, 570 .ops = &ehci_usb_ops, 571 .platdata_auto_alloc_size = sizeof(struct usb_platdata), 572 .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data), 573 .flags = DM_FLAG_ALLOC_PRIV_DMA, 574 }; 575 #endif 576