ehci-mx6.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  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 <linux/compiler.h>
  11. #include <usb/ehci-fsl.h>
  12. #include <asm/io.h>
  13. #include <asm/arch/imx-regs.h>
  14. #include <asm/arch/clock.h>
  15. #include <asm/imx-common/iomux-v3.h>
  16. #include "ehci.h"
  17. #define USB_OTGREGS_OFFSET 0x000
  18. #define USB_H1REGS_OFFSET 0x200
  19. #define USB_H2REGS_OFFSET 0x400
  20. #define USB_H3REGS_OFFSET 0x600
  21. #define USB_OTHERREGS_OFFSET 0x800
  22. #define USB_H1_CTRL_OFFSET 0x04
  23. #define USBPHY_CTRL 0x00000030
  24. #define USBPHY_CTRL_SET 0x00000034
  25. #define USBPHY_CTRL_CLR 0x00000038
  26. #define USBPHY_CTRL_TOG 0x0000003c
  27. #define USBPHY_PWD 0x00000000
  28. #define USBPHY_CTRL_SFTRST 0x80000000
  29. #define USBPHY_CTRL_CLKGATE 0x40000000
  30. #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
  31. #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
  32. #define USBPHY_CTRL_OTG_ID 0x08000000
  33. #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
  34. #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
  35. #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
  36. #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
  37. #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
  38. #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
  39. #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
  40. #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
  41. /* USBCMD */
  42. #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
  43. #define UCMD_RESET (1 << 1) /* controller reset */
  44. static const unsigned phy_bases[] = {
  45. USB_PHY0_BASE_ADDR,
  46. USB_PHY1_BASE_ADDR,
  47. };
  48. static void usb_internal_phy_clock_gate(int index, int on)
  49. {
  50. void __iomem *phy_reg;
  51. if (index >= ARRAY_SIZE(phy_bases))
  52. return;
  53. phy_reg = (void __iomem *)phy_bases[index];
  54. phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
  55. __raw_writel(USBPHY_CTRL_CLKGATE, phy_reg);
  56. }
  57. static void usb_power_config(int index)
  58. {
  59. struct anatop_regs __iomem *anatop =
  60. (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
  61. void __iomem *chrg_detect;
  62. void __iomem *pll_480_ctrl_clr;
  63. void __iomem *pll_480_ctrl_set;
  64. switch (index) {
  65. case 0:
  66. chrg_detect = &anatop->usb1_chrg_detect;
  67. pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
  68. pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
  69. break;
  70. case 1:
  71. chrg_detect = &anatop->usb2_chrg_detect;
  72. pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
  73. pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
  74. break;
  75. default:
  76. return;
  77. }
  78. /*
  79. * Some phy and power's special controls
  80. * 1. The external charger detector needs to be disabled
  81. * or the signal at DP will be poor
  82. * 2. The PLL's power and output to usb
  83. * is totally controlled by IC, so the Software only needs
  84. * to enable them at initializtion.
  85. */
  86. __raw_writel(ANADIG_USB2_CHRG_DETECT_EN_B |
  87. ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
  88. chrg_detect);
  89. __raw_writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
  90. pll_480_ctrl_clr);
  91. __raw_writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
  92. ANADIG_USB2_PLL_480_CTRL_POWER |
  93. ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
  94. pll_480_ctrl_set);
  95. }
  96. /* Return 0 : host node, <>0 : device mode */
  97. static int usb_phy_enable(int index, struct usb_ehci *ehci)
  98. {
  99. void __iomem *phy_reg;
  100. void __iomem *phy_ctrl;
  101. void __iomem *usb_cmd;
  102. u32 val;
  103. if (index >= ARRAY_SIZE(phy_bases))
  104. return 0;
  105. phy_reg = (void __iomem *)phy_bases[index];
  106. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  107. usb_cmd = (void __iomem *)&ehci->usbcmd;
  108. /* Stop then Reset */
  109. val = __raw_readl(usb_cmd);
  110. val &= ~UCMD_RUN_STOP;
  111. __raw_writel(val, usb_cmd);
  112. while (__raw_readl(usb_cmd) & UCMD_RUN_STOP)
  113. ;
  114. val = __raw_readl(usb_cmd);
  115. val |= UCMD_RESET;
  116. __raw_writel(val, usb_cmd);
  117. while (__raw_readl(usb_cmd) & UCMD_RESET)
  118. ;
  119. /* Reset USBPHY module */
  120. val = __raw_readl(phy_ctrl);
  121. val |= USBPHY_CTRL_SFTRST;
  122. __raw_writel(val, phy_ctrl);
  123. udelay(10);
  124. /* Remove CLKGATE and SFTRST */
  125. val = __raw_readl(phy_ctrl);
  126. val &= ~(USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
  127. __raw_writel(val, phy_ctrl);
  128. udelay(10);
  129. /* Power up the PHY */
  130. __raw_writel(0, phy_reg + USBPHY_PWD);
  131. /* enable FS/LS device */
  132. val = __raw_readl(phy_ctrl);
  133. val |= (USBPHY_CTRL_ENUTMILEVEL2 | USBPHY_CTRL_ENUTMILEVEL3);
  134. __raw_writel(val, phy_ctrl);
  135. return 0;
  136. }
  137. /* Base address for this IP block is 0x02184800 */
  138. struct usbnc_regs {
  139. u32 ctrl[4]; /* otg/host1-3 */
  140. u32 uh2_hsic_ctrl;
  141. u32 uh3_hsic_ctrl;
  142. u32 otg_phy_ctrl_0;
  143. u32 uh1_phy_ctrl_0;
  144. };
  145. static void usb_oc_config(int index)
  146. {
  147. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  148. USB_OTHERREGS_OFFSET);
  149. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
  150. u32 val;
  151. val = __raw_readl(ctrl);
  152. #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
  153. /* mx6qarm2 seems to required a different setting*/
  154. val &= ~UCTRL_OVER_CUR_POL;
  155. #else
  156. val |= UCTRL_OVER_CUR_POL;
  157. #endif
  158. __raw_writel(val, ctrl);
  159. val = __raw_readl(ctrl);
  160. val |= UCTRL_OVER_CUR_DIS;
  161. __raw_writel(val, ctrl);
  162. }
  163. int usb_phy_mode(int port)
  164. {
  165. void __iomem *phy_reg;
  166. void __iomem *phy_ctrl;
  167. u32 val;
  168. phy_reg = (void __iomem *)phy_bases[port];
  169. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  170. val = __raw_readl(phy_ctrl);
  171. if (val & USBPHY_CTRL_OTG_ID)
  172. return USB_INIT_DEVICE;
  173. else
  174. return USB_INIT_HOST;
  175. }
  176. int __weak board_usb_phy_mode(int port)
  177. {
  178. return usb_phy_mode(port);
  179. }
  180. int __weak board_ehci_hcd_init(int port)
  181. {
  182. return 0;
  183. }
  184. int __weak board_ehci_power(int port, int on)
  185. {
  186. return 0;
  187. }
  188. int ehci_hcd_init(int index, enum usb_init_type init,
  189. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  190. {
  191. enum usb_init_type type;
  192. struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
  193. (0x200 * index));
  194. if (index > 3)
  195. return -EINVAL;
  196. enable_usboh3_clk(1);
  197. mdelay(1);
  198. /* Do board specific initialization */
  199. board_ehci_hcd_init(index);
  200. usb_power_config(index);
  201. usb_oc_config(index);
  202. usb_internal_phy_clock_gate(index, 1);
  203. usb_phy_enable(index, ehci);
  204. type = board_usb_phy_mode(index);
  205. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  206. *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
  207. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  208. if ((type == init) || (type == USB_INIT_DEVICE))
  209. board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
  210. if (type != init)
  211. return -ENODEV;
  212. if (type == USB_INIT_DEVICE)
  213. return 0;
  214. setbits_le32(&ehci->usbmode, CM_HOST);
  215. __raw_writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
  216. setbits_le32(&ehci->portsc, USB_EN);
  217. mdelay(10);
  218. return 0;
  219. }
  220. int ehci_hcd_stop(int index)
  221. {
  222. return 0;
  223. }