ehci-vf.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*
  2. * Copyright (c) 2015 Sanchayan Maity <sanchayan.maity@toradex.com>
  3. * Copyright (C) 2015 Toradex AG
  4. *
  5. * Based on ehci-mx6 driver
  6. *
  7. * SPDX-License-Identifier: GPL-2.0+
  8. */
  9. #include <common.h>
  10. #include <dm.h>
  11. #include <usb.h>
  12. #include <errno.h>
  13. #include <linux/compiler.h>
  14. #include <asm/io.h>
  15. #include <asm-generic/gpio.h>
  16. #include <asm/arch/clock.h>
  17. #include <asm/arch/imx-regs.h>
  18. #include <asm/arch/crm_regs.h>
  19. #include <asm/imx-common/iomux-v3.h>
  20. #include <asm/imx-common/regs-usbphy.h>
  21. #include <usb/ehci-ci.h>
  22. #include <libfdt.h>
  23. #include <fdtdec.h>
  24. #include "ehci.h"
  25. #define USB_NC_REG_OFFSET 0x00000800
  26. #define ANADIG_PLL_CTRL_EN_USB_CLKS (1 << 6)
  27. #define UCTRL_OVER_CUR_POL (1 << 8) /* OTG Polarity of Overcurrent */
  28. #define UCTRL_OVER_CUR_DIS (1 << 7) /* Disable OTG Overcurrent Detection */
  29. /* USBCMD */
  30. #define UCMD_RUN_STOP (1 << 0) /* controller run/stop */
  31. #define UCMD_RESET (1 << 1) /* controller reset */
  32. DECLARE_GLOBAL_DATA_PTR;
  33. static const unsigned phy_bases[] = {
  34. USB_PHY0_BASE_ADDR,
  35. USB_PHY1_BASE_ADDR,
  36. };
  37. static const unsigned nc_reg_bases[] = {
  38. USBC0_BASE_ADDR,
  39. USBC1_BASE_ADDR,
  40. };
  41. static void usb_internal_phy_clock_gate(int index)
  42. {
  43. void __iomem *phy_reg;
  44. phy_reg = (void __iomem *)phy_bases[index];
  45. clrbits_le32(phy_reg + USBPHY_CTRL, USBPHY_CTRL_CLKGATE);
  46. }
  47. static void usb_power_config(int index)
  48. {
  49. struct anadig_reg __iomem *anadig =
  50. (struct anadig_reg __iomem *)ANADIG_BASE_ADDR;
  51. void __iomem *pll_ctrl;
  52. switch (index) {
  53. case 0:
  54. pll_ctrl = &anadig->pll3_ctrl;
  55. clrbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_BYPASS);
  56. setbits_le32(pll_ctrl, ANADIG_PLL3_CTRL_ENABLE
  57. | ANADIG_PLL3_CTRL_POWERDOWN
  58. | ANADIG_PLL_CTRL_EN_USB_CLKS);
  59. break;
  60. case 1:
  61. pll_ctrl = &anadig->pll7_ctrl;
  62. clrbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_BYPASS);
  63. setbits_le32(pll_ctrl, ANADIG_PLL7_CTRL_ENABLE
  64. | ANADIG_PLL7_CTRL_POWERDOWN
  65. | ANADIG_PLL_CTRL_EN_USB_CLKS);
  66. break;
  67. default:
  68. return;
  69. }
  70. }
  71. static void usb_phy_enable(int index, struct usb_ehci *ehci)
  72. {
  73. void __iomem *phy_reg;
  74. void __iomem *phy_ctrl;
  75. void __iomem *usb_cmd;
  76. phy_reg = (void __iomem *)phy_bases[index];
  77. phy_ctrl = (void __iomem *)(phy_reg + USBPHY_CTRL);
  78. usb_cmd = (void __iomem *)&ehci->usbcmd;
  79. /* Stop then Reset */
  80. clrbits_le32(usb_cmd, UCMD_RUN_STOP);
  81. while (readl(usb_cmd) & UCMD_RUN_STOP)
  82. ;
  83. setbits_le32(usb_cmd, UCMD_RESET);
  84. while (readl(usb_cmd) & UCMD_RESET)
  85. ;
  86. /* Reset USBPHY module */
  87. setbits_le32(phy_ctrl, USBPHY_CTRL_SFTRST);
  88. udelay(10);
  89. /* Remove CLKGATE and SFTRST */
  90. clrbits_le32(phy_ctrl, USBPHY_CTRL_CLKGATE | USBPHY_CTRL_SFTRST);
  91. udelay(10);
  92. /* Power up the PHY */
  93. writel(0, phy_reg + USBPHY_PWD);
  94. /* Enable FS/LS device */
  95. setbits_le32(phy_ctrl, USBPHY_CTRL_ENUTMILEVEL2 |
  96. USBPHY_CTRL_ENUTMILEVEL3);
  97. }
  98. static void usb_oc_config(int index)
  99. {
  100. void __iomem *ctrl;
  101. ctrl = (void __iomem *)(nc_reg_bases[index] + USB_NC_REG_OFFSET);
  102. setbits_le32(ctrl, UCTRL_OVER_CUR_POL);
  103. setbits_le32(ctrl, UCTRL_OVER_CUR_DIS);
  104. }
  105. int __weak board_usb_phy_mode(int port)
  106. {
  107. return 0;
  108. }
  109. int __weak board_ehci_hcd_init(int port)
  110. {
  111. return 0;
  112. }
  113. int ehci_vf_common_init(struct usb_ehci *ehci, int index)
  114. {
  115. int ret;
  116. /* Do board specific initialisation */
  117. ret = board_ehci_hcd_init(index);
  118. if (ret)
  119. return ret;
  120. usb_power_config(index);
  121. usb_oc_config(index);
  122. usb_internal_phy_clock_gate(index);
  123. usb_phy_enable(index, ehci);
  124. return 0;
  125. }
  126. #ifndef CONFIG_DM_USB
  127. int ehci_hcd_init(int index, enum usb_init_type init,
  128. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  129. {
  130. struct usb_ehci *ehci;
  131. enum usb_init_type type;
  132. int ret;
  133. if (index >= ARRAY_SIZE(nc_reg_bases))
  134. return -EINVAL;
  135. ehci = (struct usb_ehci *)nc_reg_bases[index];
  136. ret = ehci_vf_common_init(index);
  137. if (ret)
  138. return ret;
  139. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  140. *hcor = (struct ehci_hcor *)((uint32_t)*hccr +
  141. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  142. type = board_usb_phy_mode(index);
  143. if (type != init)
  144. return -ENODEV;
  145. if (init == USB_INIT_DEVICE) {
  146. setbits_le32(&ehci->usbmode, CM_DEVICE);
  147. writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
  148. setbits_le32(&ehci->portsc, USB_EN);
  149. } else if (init == USB_INIT_HOST) {
  150. setbits_le32(&ehci->usbmode, CM_HOST);
  151. writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
  152. setbits_le32(&ehci->portsc, USB_EN);
  153. }
  154. return 0;
  155. }
  156. int ehci_hcd_stop(int index)
  157. {
  158. return 0;
  159. }
  160. #else
  161. /* Possible port types (dual role mode) */
  162. enum dr_mode {
  163. DR_MODE_NONE = 0,
  164. DR_MODE_HOST, /* supports host operation */
  165. DR_MODE_DEVICE, /* supports device operation */
  166. DR_MODE_OTG, /* supports both */
  167. };
  168. struct ehci_vf_priv_data {
  169. struct ehci_ctrl ctrl;
  170. struct usb_ehci *ehci;
  171. struct gpio_desc cdet_gpio;
  172. enum usb_init_type init_type;
  173. enum dr_mode dr_mode;
  174. u32 portnr;
  175. };
  176. static int vf_usb_ofdata_to_platdata(struct udevice *dev)
  177. {
  178. struct ehci_vf_priv_data *priv = dev_get_priv(dev);
  179. const void *dt_blob = gd->fdt_blob;
  180. int node = dev->of_offset;
  181. const char *mode;
  182. priv->portnr = dev->seq;
  183. priv->ehci = (struct usb_ehci *)dev_get_addr(dev);
  184. mode = fdt_getprop(dt_blob, node, "dr_mode", NULL);
  185. if (mode) {
  186. if (0 == strcmp(mode, "host")) {
  187. priv->dr_mode = DR_MODE_HOST;
  188. priv->init_type = USB_INIT_HOST;
  189. } else if (0 == strcmp(mode, "peripheral")) {
  190. priv->dr_mode = DR_MODE_DEVICE;
  191. priv->init_type = USB_INIT_DEVICE;
  192. } else if (0 == strcmp(mode, "otg")) {
  193. priv->dr_mode = DR_MODE_OTG;
  194. /*
  195. * We set init_type to device by default when OTG
  196. * mode is requested. If a valid gpio is provided
  197. * we will switch the init_type based on the state
  198. * of the gpio pin.
  199. */
  200. priv->init_type = USB_INIT_DEVICE;
  201. } else {
  202. debug("%s: Cannot decode dr_mode '%s'\n",
  203. __func__, mode);
  204. return -EINVAL;
  205. }
  206. } else {
  207. priv->dr_mode = DR_MODE_HOST;
  208. priv->init_type = USB_INIT_HOST;
  209. }
  210. if (priv->dr_mode == DR_MODE_OTG) {
  211. gpio_request_by_name_nodev(dt_blob, node, "fsl,cdet-gpio", 0,
  212. &priv->cdet_gpio, GPIOD_IS_IN);
  213. if (dm_gpio_is_valid(&priv->cdet_gpio)) {
  214. if (dm_gpio_get_value(&priv->cdet_gpio))
  215. priv->init_type = USB_INIT_DEVICE;
  216. else
  217. priv->init_type = USB_INIT_HOST;
  218. }
  219. }
  220. return 0;
  221. }
  222. static int vf_init_after_reset(struct ehci_ctrl *dev)
  223. {
  224. struct ehci_vf_priv_data *priv = dev->priv;
  225. enum usb_init_type type = priv->init_type;
  226. struct usb_ehci *ehci = priv->ehci;
  227. int ret;
  228. ret = ehci_vf_common_init(priv->ehci, priv->portnr);
  229. if (ret)
  230. return ret;
  231. if (type == USB_INIT_DEVICE)
  232. return 0;
  233. setbits_le32(&ehci->usbmode, CM_HOST);
  234. writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
  235. setbits_le32(&ehci->portsc, USB_EN);
  236. mdelay(10);
  237. return 0;
  238. }
  239. static const struct ehci_ops vf_ehci_ops = {
  240. .init_after_reset = vf_init_after_reset
  241. };
  242. static int vf_usb_bind(struct udevice *dev)
  243. {
  244. static int num_controllers;
  245. /*
  246. * Without this hack, if we return ENODEV for USB Controller 0, on
  247. * probe for the next controller, USB Controller 1 will be given a
  248. * sequence number of 0. This conflicts with our requirement of
  249. * sequence numbers while initialising the peripherals.
  250. */
  251. dev->req_seq = num_controllers;
  252. num_controllers++;
  253. return 0;
  254. }
  255. static int ehci_usb_probe(struct udevice *dev)
  256. {
  257. struct usb_platdata *plat = dev_get_platdata(dev);
  258. struct ehci_vf_priv_data *priv = dev_get_priv(dev);
  259. struct usb_ehci *ehci = priv->ehci;
  260. struct ehci_hccr *hccr;
  261. struct ehci_hcor *hcor;
  262. int ret;
  263. ret = ehci_vf_common_init(ehci, priv->portnr);
  264. if (ret)
  265. return ret;
  266. if (priv->init_type != plat->init_type)
  267. return -ENODEV;
  268. if (priv->init_type == USB_INIT_HOST) {
  269. setbits_le32(&ehci->usbmode, CM_HOST);
  270. writel((PORT_PTS_UTMI | PORT_PTS_PTW), &ehci->portsc);
  271. setbits_le32(&ehci->portsc, USB_EN);
  272. }
  273. mdelay(10);
  274. hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  275. hcor = (struct ehci_hcor *)((uint32_t)hccr +
  276. HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
  277. return ehci_register(dev, hccr, hcor, &vf_ehci_ops, 0, priv->init_type);
  278. }
  279. static const struct udevice_id vf_usb_ids[] = {
  280. { .compatible = "fsl,vf610-usb" },
  281. { }
  282. };
  283. U_BOOT_DRIVER(usb_ehci) = {
  284. .name = "ehci_vf",
  285. .id = UCLASS_USB,
  286. .of_match = vf_usb_ids,
  287. .bind = vf_usb_bind,
  288. .probe = ehci_usb_probe,
  289. .remove = ehci_deregister,
  290. .ops = &ehci_usb_ops,
  291. .ofdata_to_platdata = vf_usb_ofdata_to_platdata,
  292. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  293. .priv_auto_alloc_size = sizeof(struct ehci_vf_priv_data),
  294. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  295. };
  296. #endif