ehci-mx6.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 "ehci.h"
  18. #define USB_OTGREGS_OFFSET 0x000
  19. #define USB_H1REGS_OFFSET 0x200
  20. #define USB_H2REGS_OFFSET 0x400
  21. #define USB_H3REGS_OFFSET 0x600
  22. #define USB_OTHERREGS_OFFSET 0x800
  23. #define USB_H1_CTRL_OFFSET 0x04
  24. #define USBPHY_CTRL 0x00000030
  25. #define USBPHY_CTRL_SET 0x00000034
  26. #define USBPHY_CTRL_CLR 0x00000038
  27. #define USBPHY_CTRL_TOG 0x0000003c
  28. #define USBPHY_PWD 0x00000000
  29. #define USBPHY_CTRL_SFTRST 0x80000000
  30. #define USBPHY_CTRL_CLKGATE 0x40000000
  31. #define USBPHY_CTRL_ENUTMILEVEL3 0x00008000
  32. #define USBPHY_CTRL_ENUTMILEVEL2 0x00004000
  33. #define USBPHY_CTRL_OTG_ID 0x08000000
  34. #define ANADIG_USB2_CHRG_DETECT_EN_B 0x00100000
  35. #define ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B 0x00080000
  36. #define ANADIG_USB2_PLL_480_CTRL_BYPASS 0x00010000
  37. #define ANADIG_USB2_PLL_480_CTRL_ENABLE 0x00002000
  38. #define ANADIG_USB2_PLL_480_CTRL_POWER 0x00001000
  39. #define ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS 0x00000040
  40. #define USBNC_OFFSET 0x200
  41. #define USBNC_PHYSTATUS_ID_DIG (1 << 4) /* otg_id status */
  42. #define USBNC_PHYCFG2_ACAENB (1 << 4) /* otg_id detection enable */
  43. #define UCTRL_PM (1 << 9) /* OTG Power Mask */
  44. #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
  45. #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
  46. /* USBCMD */
  47. #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
  48. #define UCMD_RESET (1 << 1) /* controller reset */
  49. #if defined(CONFIG_MX6)
  50. static const unsigned phy_bases[] = {
  51. USB_PHY0_BASE_ADDR,
  52. USB_PHY1_BASE_ADDR,
  53. };
  54. static void usb_internal_phy_clock_gate(int index, int on)
  55. {
  56. void __iomem *phy_reg;
  57. if (index >= ARRAY_SIZE(phy_bases))
  58. return;
  59. phy_reg = (void __iomem *)phy_bases[index];
  60. phy_reg += on ? USBPHY_CTRL_CLR : USBPHY_CTRL_SET;
  61. writel(USBPHY_CTRL_CLKGATE, phy_reg);
  62. }
  63. static void usb_power_config(int index)
  64. {
  65. struct anatop_regs __iomem *anatop =
  66. (struct anatop_regs __iomem *)ANATOP_BASE_ADDR;
  67. void __iomem *chrg_detect;
  68. void __iomem *pll_480_ctrl_clr;
  69. void __iomem *pll_480_ctrl_set;
  70. switch (index) {
  71. case 0:
  72. chrg_detect = &anatop->usb1_chrg_detect;
  73. pll_480_ctrl_clr = &anatop->usb1_pll_480_ctrl_clr;
  74. pll_480_ctrl_set = &anatop->usb1_pll_480_ctrl_set;
  75. break;
  76. case 1:
  77. chrg_detect = &anatop->usb2_chrg_detect;
  78. pll_480_ctrl_clr = &anatop->usb2_pll_480_ctrl_clr;
  79. pll_480_ctrl_set = &anatop->usb2_pll_480_ctrl_set;
  80. break;
  81. default:
  82. return;
  83. }
  84. /*
  85. * Some phy and power's special controls
  86. * 1. The external charger detector needs to be disabled
  87. * or the signal at DP will be poor
  88. * 2. The PLL's power and output to usb
  89. * is totally controlled by IC, so the Software only needs
  90. * to enable them at initializtion.
  91. */
  92. writel(ANADIG_USB2_CHRG_DETECT_EN_B |
  93. ANADIG_USB2_CHRG_DETECT_CHK_CHRG_B,
  94. chrg_detect);
  95. writel(ANADIG_USB2_PLL_480_CTRL_BYPASS,
  96. pll_480_ctrl_clr);
  97. writel(ANADIG_USB2_PLL_480_CTRL_ENABLE |
  98. ANADIG_USB2_PLL_480_CTRL_POWER |
  99. ANADIG_USB2_PLL_480_CTRL_EN_USB_CLKS,
  100. pll_480_ctrl_set);
  101. }
  102. /* Return 0 : host node, <>0 : device mode */
  103. static int usb_phy_enable(int index, struct usb_ehci *ehci)
  104. {
  105. void __iomem *phy_reg;
  106. void __iomem *phy_ctrl;
  107. void __iomem *usb_cmd;
  108. int ret;
  109. if (index >= ARRAY_SIZE(phy_bases))
  110. return 0;
  111. phy_reg = (void __iomem *)phy_bases[index];
  112. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  113. usb_cmd = (void __iomem *)&ehci->usbcmd;
  114. /* Stop then Reset */
  115. clrbits_le32(usb_cmd, UCMD_RUN_STOP);
  116. ret = wait_for_bit(__func__, usb_cmd, UCMD_RUN_STOP, false, 10000,
  117. false);
  118. if (ret)
  119. return ret;
  120. setbits_le32(usb_cmd, UCMD_RESET);
  121. ret = wait_for_bit(__func__, usb_cmd, UCMD_RESET, false, 10000, false);
  122. if (ret)
  123. return ret;
  124. /* Reset USBPHY module */
  125. setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
  126. udelay(10);
  127. /* Remove CLKGATE and SFTRST */
  128. clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
  129. udelay(10);
  130. /* Power up the PHY */
  131. writel(0, phy_reg + USBPHY_PWD);
  132. /* enable FS/LS device */
  133. setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
  134. USBPHY_CTRL_ENUTMILEVEL3);
  135. return 0;
  136. }
  137. int usb_phy_mode(int port)
  138. {
  139. void __iomem *phy_reg;
  140. void __iomem *phy_ctrl;
  141. u32 val;
  142. phy_reg = (void __iomem *)phy_bases[port];
  143. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  144. val = readl(phy_ctrl);
  145. if (val & USBPHY_CTRL_OTG_ID)
  146. return USB_INIT_DEVICE;
  147. else
  148. return USB_INIT_HOST;
  149. }
  150. /* Base address for this IP block is 0x02184800 */
  151. struct usbnc_regs {
  152. u32 ctrl[4]; /* otg/host1-3 */
  153. u32 uh2_hsic_ctrl;
  154. u32 uh3_hsic_ctrl;
  155. u32 otg_phy_ctrl_0;
  156. u32 uh1_phy_ctrl_0;
  157. };
  158. #elif defined(CONFIG_MX7)
  159. struct usbnc_regs {
  160. u32 ctrl1;
  161. u32 ctrl2;
  162. u32 reserve1[10];
  163. u32 phy_cfg1;
  164. u32 phy_cfg2;
  165. u32 reserve2;
  166. u32 phy_status;
  167. u32 reserve3[4];
  168. u32 adp_cfg1;
  169. u32 adp_cfg2;
  170. u32 adp_status;
  171. };
  172. static void usb_power_config(int index)
  173. {
  174. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  175. (0x10000 * index) + USBNC_OFFSET);
  176. void __iomem *phy_cfg2 = (void __iomem *)(&usbnc->phy_cfg2);
  177. /*
  178. * Clear the ACAENB to enable usb_otg_id detection,
  179. * otherwise it is the ACA detection enabled.
  180. */
  181. clrbits_le32(phy_cfg2, USBNC_PHYCFG2_ACAENB);
  182. }
  183. int usb_phy_mode(int port)
  184. {
  185. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  186. (0x10000 * port) + USBNC_OFFSET);
  187. void __iomem *status = (void __iomem *)(&usbnc->phy_status);
  188. u32 val;
  189. val = readl(status);
  190. if (val & USBNC_PHYSTATUS_ID_DIG)
  191. return USB_INIT_DEVICE;
  192. else
  193. return USB_INIT_HOST;
  194. }
  195. #endif
  196. static void usb_oc_config(int index)
  197. {
  198. #if defined(CONFIG_MX6)
  199. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  200. USB_OTHERREGS_OFFSET);
  201. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl[index]);
  202. #elif defined(CONFIG_MX7)
  203. struct usbnc_regs *usbnc = (struct usbnc_regs *)(USB_BASE_ADDR +
  204. (0x10000 * index) + USBNC_OFFSET);
  205. void __iomem *ctrl = (void __iomem *)(&usbnc->ctrl1);
  206. #endif
  207. #if CONFIG_MACH_TYPE == MACH_TYPE_MX6Q_ARM2
  208. /* mx6qarm2 seems to required a different setting*/
  209. clrbits_le32(ctrl, UCTRL_OVER_CUR_POL);
  210. #else
  211. setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
  212. #endif
  213. #if defined(CONFIG_MX6)
  214. setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
  215. #elif defined(CONFIG_MX7)
  216. setbits_le32(ctrl, UCTRL_OVER_CUR_DIS | UCTRL_PM);
  217. #endif
  218. }
  219. /**
  220. * board_usb_phy_mode - override usb phy mode
  221. * @port: usb host/otg port
  222. *
  223. * Target board specific, override usb_phy_mode.
  224. * When usb-otg is used as usb host port, iomux pad usb_otg_id can be
  225. * left disconnected in this case usb_phy_mode will not be able to identify
  226. * the phy mode that usb port is used.
  227. * Machine file overrides board_usb_phy_mode.
  228. *
  229. * Return: USB_INIT_DEVICE or USB_INIT_HOST
  230. */
  231. int __weak board_usb_phy_mode(int port)
  232. {
  233. return usb_phy_mode(port);
  234. }
  235. /**
  236. * board_ehci_hcd_init - set usb vbus voltage
  237. * @port: usb otg port
  238. *
  239. * Target board specific, setup iomux pad to setup supply vbus voltage
  240. * for usb otg port. Machine board file overrides board_ehci_hcd_init
  241. *
  242. * Return: 0 Success
  243. */
  244. int __weak board_ehci_hcd_init(int port)
  245. {
  246. return 0;
  247. }
  248. /**
  249. * board_ehci_power - enables/disables usb vbus voltage
  250. * @port: usb otg port
  251. * @on: on/off vbus voltage
  252. *
  253. * Enables/disables supply vbus voltage for usb otg port.
  254. * Machine board file overrides board_ehci_power
  255. *
  256. * Return: 0 Success
  257. */
  258. int __weak board_ehci_power(int port, int on)
  259. {
  260. return 0;
  261. }
  262. int ehci_hcd_init(int index, enum usb_init_type init,
  263. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  264. {
  265. enum usb_init_type type;
  266. #if defined(CONFIG_MX6)
  267. u32 controller_spacing = 0x200;
  268. #elif defined(CONFIG_MX7)
  269. u32 controller_spacing = 0x10000;
  270. #endif
  271. struct usb_ehci *ehci = (struct usb_ehci *)(USB_BASE_ADDR +
  272. (controller_spacing * index));
  273. int ret;
  274. if (index > 3)
  275. return -EINVAL;
  276. enable_usboh3_clk(1);
  277. mdelay(1);
  278. /* Do board specific initialization */
  279. ret = board_ehci_hcd_init(index);
  280. if (ret)
  281. return ret;
  282. usb_power_config(index);
  283. usb_oc_config(index);
  284. #if defined(CONFIG_MX6)
  285. usb_internal_phy_clock_gate(index, 1);
  286. usb_phy_enable(index, ehci);
  287. #endif
  288. type = board_usb_phy_mode(index);
  289. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  290. *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
  291. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  292. if ((type == init) || (type == USB_INIT_DEVICE))
  293. board_ehci_power(index, (type == USB_INIT_DEVICE) ? 0 : 1);
  294. if (type != init)
  295. return -ENODEV;
  296. if (type == USB_INIT_DEVICE)
  297. return 0;
  298. setbits_le32(&ehci->usbmode, CM_HOST);
  299. writel(CONFIG_MXC_USB_PORTSC, &ehci->portsc);
  300. setbits_le32(&ehci->portsc, USB_EN);
  301. mdelay(10);
  302. return 0;
  303. }
  304. int ehci_hcd_stop(int index)
  305. {
  306. return 0;
  307. }