ehci-mx6.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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. #include <common.h>
  8. #include <usb.h>
  9. #include <errno.h>
  10. #include <wait_bit.h>
  11. #include <linux/compiler.h>
  12. #include <usb/ehci-ci.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/imx-regs.h>
  15. #include <asm/arch/clock.h>
  16. #include <asm/imx-common/iomux-v3.h>
  17. #include <dm.h>
  18. #include "ehci.h"
  19. #define USB_OTGREGS_OFFSET 0x000
  20. #define USB_H1REGS_OFFSET 0x200
  21. #define USB_H2REGS_OFFSET 0x400
  22. #define USB_H3REGS_OFFSET 0x600
  23. #define USB_OTHERREGS_OFFSET 0x800
  24. #define USB_H1_CTRL_OFFSET 0x04
  25. #define USBPHY_CTRL 0x00000030
  26. #define USBPHY_CTRL_SET 0x00000034
  27. #define USBPHY_CTRL_CLR 0x00000038
  28. #define USBPHY_CTRL_TOG 0x0000003c
  29. #define USBPHY_PWD 0x00000000
  30. #define USBPHY_CTRL_SFTRST 0x80000000
  31. #define USBPHY_CTRL_CLKGATE 0x40000000
  32. #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
  33. #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
  34. #define USBPHY_CTRL_OTG_ID 0x08000000
  35. #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
  36. #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
  37. #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
  38. #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
  39. #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
  40. #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
  41. #define USBNC_OFFSET 0x200
  42. #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
  43. #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
  44. #define UCTRL_PWR_POL (1 << 9) /* OTG Polarity of Power Pin */
  45. #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
  46. #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
  47. /* USBCMD */
  48. #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
  49. #define UCMD_RESET (1 << 1) /* controller reset */
  50. #if defined(CONFIG_MX6)
  51. static const unsigned phy_bases[] = {
  52. USB_PHY0_BASE_ADDR,
  53. USB_PHY1_BASE_ADDR,
  54. };
  55. static void usb_internal_phy_clock_gate(int index, int on)
  56. {
  57. void __iomem *phy_reg;
  58. if (index >= ARRAY_SIZE(phy_bases))
  59. return;
  60. phy_reg = (void __iomem *)phy_bases[index];
  61. phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
  62. writel(USBPHY_CTRL_CLKGATE, phy_reg);
  63. }
  64. static void usb_power_config(int index)
  65. {
  66. struct anatop_regs __iomem *anatop =
  67. (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
  68. void __iomem *chrg_detect;
  69. void __iomem *pll_480_ctrl_clr;
  70. void __iomem *pll_480_ctrl_set;
  71. switch (index) {
  72. case 0:
  73. chrg_detect = &anatop->usb1_chrg_detect;
  74. pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
  75. pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
  76. break;
  77. case 1:
  78. chrg_detect = &anatop->usb2_chrg_detect;
  79. pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
  80. pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
  81. break;
  82. default:
  83. return;
  84. }
  85. /*
  86. * Some phy and power's special controls
  87. * 1. The external charger detector needs to be disabled
  88. * or the signal at DP will be poor
  89. * 2. The PLL's power and output to usb
  90. * is totally controlled by IC, so the Software only needs
  91. * to enable them at initializtion.
  92. */
  93. writel(ANADIG_USB2_CHRG_DETECT_EN_B |
  94. ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
  95. chrg_detect);
  96. writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
  97. pll_480_ctrl_clr);
  98. writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
  99. ANADIG_USB2_PLL_480_CTRL_POWER |
  100. ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
  101. pll_480_ctrl_set);
  102. }
  103. /* Return 0 : host node, <>0 : device mode */
  104. static int usb_phy_enable(int index, struct usb_ehci *ehci)
  105. {
  106. void __iomem *phy_reg;
  107. void __iomem *phy_ctrl;
  108. void __iomem *usb_cmd;
  109. int ret;
  110. if (index >= ARRAY_SIZE(phy_bases))
  111. return 0;
  112. phy_reg = (void __iomem *)phy_bases[index];
  113. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  114. usb_cmd = (void __iomem *)&ehci->usbcmd;
  115. /* Stop then Reset */
  116. clrbits_le32(usb_cmd, UCMD_RUN_STOP);
  117. ret = wait_for_bit(__func__, usb_cmd, UCMD_RUN_STOP, false, 10000,
  118. false);
  119. if (ret)
  120. return ret;
  121. setbits_le32(usb_cmd, UCMD_RESET);
  122. ret = wait_for_bit(__func__, usb_cmd, UCMD_RESET, false, 10000, false);
  123. if (ret)
  124. return ret;
  125. /* Reset USBPHY module */
  126. setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
  127. udelay(10);
  128. /* Remove CLKGATE and SFTRST */
  129. clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
  130. udelay(10);
  131. /* Power up the PHY */
  132. writel(0, phy_reg + USBPHY_PWD);
  133. /* enable FS/LS device */
  134. setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
  135. USBPHY_CTRL_ENUTMILEVEL3);
  136. return 0;
  137. }
  138. int usb_phy_mode(int port)
  139. {
  140. void __iomem *phy_reg;
  141. void __iomem *phy_ctrl;
  142. u32 val;
  143. phy_reg = (void __iomem *)phy_bases[port];
  144. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  145. val = readl(phy_ctrl);
  146. if (val & USBPHY_CTRL_OTG_ID)
  147. return USB_INIT_DEVICE;
  148. else
  149. return USB_INIT_HOST;
  150. }
  151. /* Base address for this IP block is 0x02184800 */
  152. struct usbnc_regs {
  153. u32 ctrl[4]; /* otg/host1-3 */
  154. u32 uh2_hsic_ctrl;
  155. u32 uh3_hsic_ctrl;
  156. u32 otg_phy_ctrl_0;
  157. u32 uh1_phy_ctrl_0;
  158. };
  159. #elif defined(CONFIG_MX7)
  160. struct usbnc_regs {
  161. u32 ctrl1;
  162. u32 ctrl2;
  163. u32 reserve1[10];
  164. u32 phy_cfg1;
  165. u32 phy_cfg2;
  166. u32 reserve2;
  167. u32 phy_status;
  168. u32 reserve3[4];
  169. u32 adp_cfg1;
  170. u32 adp_cfg2;
  171. u32 adp_status;
  172. };
  173. static void usb_power_config(int index)
  174. {
  175. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  176. (0x10000 * index) + USBNC_OFFSET);
  177. void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
  178. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
  179. /*
  180. * Clear the ACAENB to enable usb_otg_id detection,
  181. * otherwise it is the ACA detection enabled.
  182. */
  183. clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
  184. /* Set power polarity to high active */
  185. #ifdef CONFIG_MXC_USB_OTG_HACTIVE
  186. setbits_le32(ctrl, UCTRL_PWR_POL);
  187. #else
  188. clrbits_le32(ctrl, UCTRL_PWR_POL);
  189. #endif
  190. }
  191. int usb_phy_mode(int port)
  192. {
  193. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  194. (0x10000 * port) + USBNC_OFFSET);
  195. void __iomem *status = (void __iomem *)(&usbnc->phy_status);
  196. u32 val;
  197. val = readl(status);
  198. if (val & USBNC_PHYSTATUS_ID_DIG)
  199. return USB_INIT_DEVICE;
  200. else
  201. return USB_INIT_HOST;
  202. }
  203. #endif
  204. static void usb_oc_config(int index)
  205. {
  206. #if defined(CONFIG_MX6)
  207. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  208. USB_OTHERREGS_OFFSET);
  209. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
  210. #elif defined(CONFIG_MX7)
  211. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  212. (0x10000 * index) + USBNC_OFFSET);
  213. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
  214. #endif
  215. #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
  216. /* mx6qarm2 seems to required a different setting*/
  217. clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
  218. #else
  219. setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
  220. #endif
  221. setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
  222. }
  223. /**
  224. * board_usb_phy_mode - override usb phy mode
  225. * @port: usb host/otg port
  226. *
  227. * Target board specific, override usb_phy_mode.
  228. * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
  229. * left disconnected in this case usb_phy_mode will not be able to identify
  230. * the phy mode that usb port is used.
  231. * Machine file overrides board_usb_phy_mode.
  232. *
  233. * Return: USB_INIT_DEVICE or USB_INIT_HOST
  234. */
  235. int __weak board_usb_phy_mode(int port)
  236. {
  237. return usb_phy_mode(port);
  238. }
  239. /**
  240. * board_ehci_hcd_init - set usb vbus voltage
  241. * @port: usb otg port
  242. *
  243. * Target board specific, setup iomux pad to setup supply vbus voltage
  244. * for usb otg port. Machine board file overrides board_ehci_hcd_init
  245. *
  246. * Return: 0 Success
  247. */
  248. int __weak board_ehci_hcd_init(int port)
  249. {
  250. return 0;
  251. }
  252. /**
  253. * board_ehci_power - enables/disables usb vbus voltage
  254. * @port: usb otg port
  255. * @on: on/off vbus voltage
  256. *
  257. * Enables/disables supply vbus voltage for usb otg port.
  258. * Machine board file overrides board_ehci_power
  259. *
  260. * Return: 0 Success
  261. */
  262. int __weak board_ehci_power(int port, int on)
  263. {
  264. return 0;
  265. }
  266. int ehci_mx6_common_init(struct usb_ehci *ehci, int index)
  267. {
  268. int ret;
  269. enable_usboh3_clk(1);
  270. mdelay(1);
  271. /* Do board specific initialization */
  272. ret = board_ehci_hcd_init(index);
  273. if (ret)
  274. return ret;
  275. usb_power_config(index);
  276. usb_oc_config(index);
  277. #if defined(CONFIG_MX6)
  278. usb_internal_phy_clock_gate(index, 1);
  279. usb_phy_enable(index, ehci);
  280. #endif
  281. return 0;
  282. }
  283. #ifndef CONFIG_DM_USB
  284. int ehci_hcd_init(int index, enum usb_init_type init,
  285. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  286. {
  287. enum usb_init_type type;
  288. #if defined(CONFIG_MX6)
  289. u32 controller_spacing = 0x200;
  290. #elif defined(CONFIG_MX7)
  291. u32 controller_spacing = 0x10000;
  292. #endif
  293. struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
  294. (controller_spacing * index));
  295. int ret;
  296. if (index > 3)
  297. return -EINVAL;
  298. ret = ehci_mx6_common_init(ehci, index);
  299. if (ret)
  300. return ret;
  301. type = board_usb_phy_mode(index);
  302. if (hccr && hcor) {
  303. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  304. *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
  305. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  306. }
  307. if ((type == init) || (type == USB_INIT_DEVICE))
  308. board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
  309. if (type != init)
  310. return -ENODEV;
  311. if (type == USB_INIT_DEVICE)
  312. return 0;
  313. setbits_le32(&ehci->usbmode, CM_HOST);
  314. writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
  315. setbits_le32(&ehci->portsc, USB_EN);
  316. mdelay(10);
  317. return 0;
  318. }
  319. int ehci_hcd_stop(int index)
  320. {
  321. return 0;
  322. }
  323. #else
  324. struct ehci_mx6_priv_data {
  325. struct ehci_ctrl ctrl;
  326. struct usb_ehci *ehci;
  327. enum usb_init_type init_type;
  328. int portnr;
  329. };
  330. static int mx6_init_after_reset(struct ehci_ctrl *dev)
  331. {
  332. struct ehci_mx6_priv_data *priv = dev->priv;
  333. enum usb_init_type type = priv->init_type;
  334. struct usb_ehci *ehci = priv->ehci;
  335. int ret;
  336. ret = ehci_mx6_common_init(priv->ehci, priv->portnr);
  337. if (ret)
  338. return ret;
  339. board_ehci_power(priv->portnr, (type == USB_INIT_DEVICE) ? 0 : 1);
  340. if (type == USB_INIT_DEVICE)
  341. return 0;
  342. setbits_le32(&ehci->usbmode, CM_HOST);
  343. writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
  344. setbits_le32(&ehci->portsc, USB_EN);
  345. mdelay(10);
  346. return 0;
  347. }
  348. static const struct ehci_ops mx6_ehci_ops = {
  349. .init_after_reset = mx6_init_after_reset
  350. };
  351. static int ehci_usb_probe(struct udevice *dev)
  352. {
  353. struct usb_platdata *plat = dev_get_platdata(dev);
  354. struct usb_ehci *ehci = (struct usb_ehci *)dev_get_addr(dev);
  355. struct ehci_mx6_priv_data *priv = dev_get_priv(dev);
  356. struct ehci_hccr *hccr;
  357. struct ehci_hcor *hcor;
  358. int ret;
  359. priv->ehci = ehci;
  360. priv->portnr = dev->seq;
  361. priv->init_type = plat->init_type;
  362. ret = ehci_mx6_common_init(ehci, priv->portnr);
  363. if (ret)
  364. return ret;
  365. board_ehci_power(priv->portnr, (priv->init_type == USB_INIT_DEVICE) ? 0 : 1);
  366. if (priv->init_type == USB_INIT_HOST) {
  367. setbits_le32(&ehci->usbmode, CM_HOST);
  368. writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
  369. setbits_le32(&ehci->portsc, USB_EN);
  370. }
  371. mdelay(10);
  372. hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  373. hcor = (struct ehci_hcor *)((uint32_t)hccr +
  374. HC_LENGTH(ehci_readl(&(hccr)->cr_capbase)));
  375. return ehci_register(dev, hccr, hcor, &mx6_ehci_ops, 0, priv->init_type);
  376. }
  377. static const struct udevice_id mx6_usb_ids[] = {
  378. { .compatible = "fsl,imx27-usb" },
  379. { }
  380. };
  381. U_BOOT_DRIVER(usb_mx6) = {
  382. .name = "ehci_mx6",
  383. .id = UCLASS_USB,
  384. .of_match = mx6_usb_ids,
  385. .probe = ehci_usb_probe,
  386. .remove = ehci_deregister,
  387. .ops = &ehci_usb_ops,
  388. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  389. .priv_auto_alloc_size = sizeof(struct ehci_mx6_priv_data),
  390. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  391. };
  392. #endif