ehci-fsl.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * (C) Copyright 2009, 2011, 2016 Freescale Semiconductor, Inc.
  3. *
  4. * (C) Copyright 2008, Excito Elektronik i Sk=E5ne AB
  5. *
  6. * Author: Tor Krill tor@excito.com
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <pci.h>
  12. #include <usb.h>
  13. #include <asm/io.h>
  14. #include <usb/ehci-ci.h>
  15. #include <hwconfig.h>
  16. #include <fsl_usb.h>
  17. #include <fdt_support.h>
  18. #include <dm.h>
  19. #include "ehci.h"
  20. DECLARE_GLOBAL_DATA_PTR;
  21. #ifndef CONFIG_USB_MAX_CONTROLLER_COUNT
  22. #define CONFIG_USB_MAX_CONTROLLER_COUNT 1
  23. #endif
  24. #ifdef CONFIG_DM_USB
  25. struct ehci_fsl_priv {
  26. struct ehci_ctrl ehci;
  27. fdt_addr_t hcd_base;
  28. char *phy_type;
  29. };
  30. #endif
  31. static void set_txfifothresh(struct usb_ehci *, u32);
  32. #ifdef CONFIG_DM_USB
  33. static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
  34. struct ehci_hccr *hccr, struct ehci_hcor *hcor);
  35. #else
  36. static int ehci_fsl_init(int index, struct usb_ehci *ehci,
  37. struct ehci_hccr *hccr, struct ehci_hcor *hcor);
  38. #endif
  39. /* Check USB PHY clock valid */
  40. static int usb_phy_clk_valid(struct usb_ehci *ehci)
  41. {
  42. if (!((in_be32(&ehci->control) & PHY_CLK_VALID) ||
  43. in_be32(&ehci->prictrl))) {
  44. printf("USB PHY clock invalid!\n");
  45. return 0;
  46. } else {
  47. return 1;
  48. }
  49. }
  50. #ifdef CONFIG_DM_USB
  51. static int ehci_fsl_ofdata_to_platdata(struct udevice *dev)
  52. {
  53. struct ehci_fsl_priv *priv = dev_get_priv(dev);
  54. const void *prop;
  55. prop = fdt_getprop(gd->fdt_blob, dev->of_offset, "phy_type",
  56. NULL);
  57. if (prop) {
  58. priv->phy_type = (char *)prop;
  59. debug("phy_type %s\n", priv->phy_type);
  60. }
  61. return 0;
  62. }
  63. static int ehci_fsl_init_after_reset(struct ehci_ctrl *ctrl)
  64. {
  65. struct usb_ehci *ehci = NULL;
  66. struct ehci_fsl_priv *priv = container_of(ctrl, struct ehci_fsl_priv,
  67. ehci);
  68. ehci = (struct usb_ehci *)priv->hcd_base;
  69. if (ehci_fsl_init(priv, ehci, priv->ehci.hccr, priv->ehci.hcor) < 0)
  70. return -ENXIO;
  71. return 0;
  72. }
  73. static const struct ehci_ops fsl_ehci_ops = {
  74. .init_after_reset = ehci_fsl_init_after_reset,
  75. };
  76. static int ehci_fsl_probe(struct udevice *dev)
  77. {
  78. struct ehci_fsl_priv *priv = dev_get_priv(dev);
  79. struct usb_ehci *ehci = NULL;
  80. struct ehci_hccr *hccr;
  81. struct ehci_hcor *hcor;
  82. /*
  83. * Get the base address for EHCI controller from the device node
  84. */
  85. priv->hcd_base = dev_get_addr(dev);
  86. if (priv->hcd_base == FDT_ADDR_T_NONE) {
  87. debug("Can't get the EHCI register base address\n");
  88. return -ENXIO;
  89. }
  90. ehci = (struct usb_ehci *)priv->hcd_base;
  91. hccr = (struct ehci_hccr *)(&ehci->caplength);
  92. hcor = (struct ehci_hcor *)
  93. ((u32)hccr + HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
  94. if (ehci_fsl_init(priv, ehci, hccr, hcor) < 0)
  95. return -ENXIO;
  96. debug("ehci-fsl: init hccr %x and hcor %x hc_length %d\n",
  97. (u32)hccr, (u32)hcor,
  98. (u32)HC_LENGTH(ehci_readl(&hccr->cr_capbase)));
  99. return ehci_register(dev, hccr, hcor, &fsl_ehci_ops, 0, USB_INIT_HOST);
  100. }
  101. static const struct udevice_id ehci_usb_ids[] = {
  102. { .compatible = "fsl-usb2-mph", },
  103. { .compatible = "fsl-usb2-dr", },
  104. { }
  105. };
  106. U_BOOT_DRIVER(ehci_fsl) = {
  107. .name = "ehci_fsl",
  108. .id = UCLASS_USB,
  109. .of_match = ehci_usb_ids,
  110. .ofdata_to_platdata = ehci_fsl_ofdata_to_platdata,
  111. .probe = ehci_fsl_probe,
  112. .remove = ehci_deregister,
  113. .ops = &ehci_usb_ops,
  114. .platdata_auto_alloc_size = sizeof(struct usb_platdata),
  115. .priv_auto_alloc_size = sizeof(struct ehci_fsl_priv),
  116. .flags = DM_FLAG_ALLOC_PRIV_DMA,
  117. };
  118. #else
  119. /*
  120. * Create the appropriate control structures to manage
  121. * a new EHCI host controller.
  122. *
  123. * Excerpts from linux ehci fsl driver.
  124. */
  125. int ehci_hcd_init(int index, enum usb_init_type init,
  126. struct ehci_hccr **hccr, struct ehci_hcor **hcor)
  127. {
  128. struct usb_ehci *ehci = NULL;
  129. switch (index) {
  130. case 0:
  131. ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB1_ADDR;
  132. break;
  133. case 1:
  134. ehci = (struct usb_ehci *)CONFIG_SYS_FSL_USB2_ADDR;
  135. break;
  136. default:
  137. printf("ERROR: wrong controller index!!\n");
  138. return -EINVAL;
  139. };
  140. *hccr = (struct ehci_hccr *)((uint32_t)&ehci->caplength);
  141. *hcor = (struct ehci_hcor *)((uint32_t) *hccr +
  142. HC_LENGTH(ehci_readl(&(*hccr)->cr_capbase)));
  143. return ehci_fsl_init(index, ehci, *hccr, *hcor);
  144. }
  145. /*
  146. * Destroy the appropriate control structures corresponding
  147. * the the EHCI host controller.
  148. */
  149. int ehci_hcd_stop(int index)
  150. {
  151. return 0;
  152. }
  153. #endif
  154. #ifdef CONFIG_DM_USB
  155. static int ehci_fsl_init(struct ehci_fsl_priv *priv, struct usb_ehci *ehci,
  156. struct ehci_hccr *hccr, struct ehci_hcor *hcor)
  157. #else
  158. static int ehci_fsl_init(int index, struct usb_ehci *ehci,
  159. struct ehci_hccr *hccr, struct ehci_hcor *hcor)
  160. #endif
  161. {
  162. const char *phy_type = NULL;
  163. #ifndef CONFIG_DM_USB
  164. size_t len;
  165. char current_usb_controller[5];
  166. #endif
  167. #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
  168. char usb_phy[5];
  169. usb_phy[0] = '\0';
  170. #endif
  171. if (has_erratum_a007075()) {
  172. /*
  173. * A 5ms delay is needed after applying soft-reset to the
  174. * controller to let external ULPI phy come out of reset.
  175. * This delay needs to be added before re-initializing
  176. * the controller after soft-resetting completes
  177. */
  178. mdelay(5);
  179. }
  180. /* Set to Host mode */
  181. setbits_le32(&ehci->usbmode, CM_HOST);
  182. out_be32(&ehci->snoop1, SNOOP_SIZE_2GB);
  183. out_be32(&ehci->snoop2, 0x80000000 | SNOOP_SIZE_2GB);
  184. /* Init phy */
  185. #ifdef CONFIG_DM_USB
  186. if (priv->phy_type)
  187. phy_type = priv->phy_type;
  188. #else
  189. memset(current_usb_controller, '\0', 5);
  190. snprintf(current_usb_controller, sizeof(current_usb_controller),
  191. "usb%d", index+1);
  192. if (hwconfig_sub(current_usb_controller, "phy_type"))
  193. phy_type = hwconfig_subarg(current_usb_controller,
  194. "phy_type", &len);
  195. #endif
  196. else
  197. phy_type = getenv("usb_phy_type");
  198. if (!phy_type) {
  199. #ifdef CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY
  200. /* if none specified assume internal UTMI */
  201. strcpy(usb_phy, "utmi");
  202. phy_type = usb_phy;
  203. #else
  204. printf("WARNING: USB phy type not defined !!\n");
  205. return -1;
  206. #endif
  207. }
  208. if (!strncmp(phy_type, "utmi", 4)) {
  209. #if defined(CONFIG_SYS_FSL_USB_INTERNAL_UTMI_PHY)
  210. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  211. PHY_CLK_SEL_UTMI);
  212. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  213. UTMI_PHY_EN);
  214. udelay(1000); /* delay required for PHY Clk to appear */
  215. #endif
  216. out_le32(&(hcor)->or_portsc[0], PORT_PTS_UTMI);
  217. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  218. USB_EN);
  219. } else {
  220. clrsetbits_be32(&ehci->control, CONTROL_REGISTER_W1C_MASK,
  221. PHY_CLK_SEL_ULPI);
  222. clrsetbits_be32(&ehci->control, UTMI_PHY_EN |
  223. CONTROL_REGISTER_W1C_MASK, USB_EN);
  224. udelay(1000); /* delay required for PHY Clk to appear */
  225. if (!usb_phy_clk_valid(ehci))
  226. return -EINVAL;
  227. out_le32(&(hcor)->or_portsc[0], PORT_PTS_ULPI);
  228. }
  229. out_be32(&ehci->prictrl, 0x0000000c);
  230. out_be32(&ehci->age_cnt_limit, 0x00000040);
  231. out_be32(&ehci->sictrl, 0x00000001);
  232. in_le32(&ehci->usbmode);
  233. if (has_erratum_a007798())
  234. set_txfifothresh(ehci, TXFIFOTHRESH);
  235. if (has_erratum_a004477()) {
  236. /*
  237. * When reset is issued while any ULPI transaction is ongoing
  238. * then it may result to corruption of ULPI Function Control
  239. * Register which eventually causes phy clock to enter low
  240. * power mode which stops the clock. Thus delay is required
  241. * before reset to let ongoing ULPI transaction complete.
  242. */
  243. udelay(1);
  244. }
  245. return 0;
  246. }
  247. /*
  248. * Setting the value of TXFIFO_THRESH field in TXFILLTUNING register
  249. * to counter DDR latencies in writing data into Tx buffer.
  250. * This prevents Tx buffer from getting underrun
  251. */
  252. static void set_txfifothresh(struct usb_ehci *ehci, u32 txfifo_thresh)
  253. {
  254. u32 cmd;
  255. cmd = ehci_readl(&ehci->txfilltuning);
  256. cmd &= ~TXFIFO_THRESH_MASK;
  257. cmd |= TXFIFO_THRESH(txfifo_thresh);
  258. ehci_writel(&ehci->txfilltuning, cmd);
  259. }