ehci-orion.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. /*
  2. * drivers/usb/host/ehci-orion.c
  3. *
  4. * Tzachi Perelstein <tzachi@marvell.com>
  5. *
  6. * This file is licensed under the terms of the GNU General Public
  7. * License version 2. This program is licensed "as is" without any
  8. * warranty of any kind, whether express or implied.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/platform_device.h>
  13. #include <linux/mbus.h>
  14. #include <linux/clk.h>
  15. #include <linux/platform_data/usb-ehci-orion.h>
  16. #include <linux/of.h>
  17. #include <linux/phy/phy.h>
  18. #include <linux/of_device.h>
  19. #include <linux/of_irq.h>
  20. #include <linux/usb.h>
  21. #include <linux/usb/hcd.h>
  22. #include <linux/io.h>
  23. #include <linux/dma-mapping.h>
  24. #include "ehci.h"
  25. #define rdl(off) readl_relaxed(hcd->regs + (off))
  26. #define wrl(off, val) writel_relaxed((val), hcd->regs + (off))
  27. #define USB_CMD 0x140
  28. #define USB_MODE 0x1a8
  29. #define USB_CAUSE 0x310
  30. #define USB_MASK 0x314
  31. #define USB_WINDOW_CTRL(i) (0x320 + ((i) << 4))
  32. #define USB_WINDOW_BASE(i) (0x324 + ((i) << 4))
  33. #define USB_IPG 0x360
  34. #define USB_PHY_PWR_CTRL 0x400
  35. #define USB_PHY_TX_CTRL 0x420
  36. #define USB_PHY_RX_CTRL 0x430
  37. #define USB_PHY_IVREF_CTRL 0x440
  38. #define USB_PHY_TST_GRP_CTRL 0x450
  39. #define DRIVER_DESC "EHCI orion driver"
  40. #define hcd_to_orion_priv(h) ((struct orion_ehci_hcd *)hcd_to_ehci(h)->priv)
  41. struct orion_ehci_hcd {
  42. struct clk *clk;
  43. struct phy *phy;
  44. };
  45. static const char hcd_name[] = "ehci-orion";
  46. static struct hc_driver __read_mostly ehci_orion_hc_driver;
  47. /*
  48. * Implement Orion USB controller specification guidelines
  49. */
  50. static void orion_usb_phy_v1_setup(struct usb_hcd *hcd)
  51. {
  52. /* The below GLs are according to the Orion Errata document */
  53. /*
  54. * Clear interrupt cause and mask
  55. */
  56. wrl(USB_CAUSE, 0);
  57. wrl(USB_MASK, 0);
  58. /*
  59. * Reset controller
  60. */
  61. wrl(USB_CMD, rdl(USB_CMD) | 0x2);
  62. while (rdl(USB_CMD) & 0x2);
  63. /*
  64. * GL# USB-10: Set IPG for non start of frame packets
  65. * Bits[14:8]=0xc
  66. */
  67. wrl(USB_IPG, (rdl(USB_IPG) & ~0x7f00) | 0xc00);
  68. /*
  69. * GL# USB-9: USB 2.0 Power Control
  70. * BG_VSEL[7:6]=0x1
  71. */
  72. wrl(USB_PHY_PWR_CTRL, (rdl(USB_PHY_PWR_CTRL) & ~0xc0)| 0x40);
  73. /*
  74. * GL# USB-1: USB PHY Tx Control - force calibration to '8'
  75. * TXDATA_BLOCK_EN[21]=0x1, EXT_RCAL_EN[13]=0x1, IMP_CAL[6:3]=0x8
  76. */
  77. wrl(USB_PHY_TX_CTRL, (rdl(USB_PHY_TX_CTRL) & ~0x78) | 0x202040);
  78. /*
  79. * GL# USB-3 GL# USB-9: USB PHY Rx Control
  80. * RXDATA_BLOCK_LENGHT[31:30]=0x3, EDGE_DET_SEL[27:26]=0,
  81. * CDR_FASTLOCK_EN[21]=0, DISCON_THRESHOLD[9:8]=0, SQ_THRESH[7:4]=0x1
  82. */
  83. wrl(USB_PHY_RX_CTRL, (rdl(USB_PHY_RX_CTRL) & ~0xc2003f0) | 0xc0000010);
  84. /*
  85. * GL# USB-3 GL# USB-9: USB PHY IVREF Control
  86. * PLLVDD12[1:0]=0x2, RXVDD[5:4]=0x3, Reserved[19]=0
  87. */
  88. wrl(USB_PHY_IVREF_CTRL, (rdl(USB_PHY_IVREF_CTRL) & ~0x80003 ) | 0x32);
  89. /*
  90. * GL# USB-3 GL# USB-9: USB PHY Test Group Control
  91. * REG_FIFO_SQ_RST[15]=0
  92. */
  93. wrl(USB_PHY_TST_GRP_CTRL, rdl(USB_PHY_TST_GRP_CTRL) & ~0x8000);
  94. /*
  95. * Stop and reset controller
  96. */
  97. wrl(USB_CMD, rdl(USB_CMD) & ~0x1);
  98. wrl(USB_CMD, rdl(USB_CMD) | 0x2);
  99. while (rdl(USB_CMD) & 0x2);
  100. /*
  101. * GL# USB-5 Streaming disable REG_USB_MODE[4]=1
  102. * TBD: This need to be done after each reset!
  103. * GL# USB-4 Setup USB Host mode
  104. */
  105. wrl(USB_MODE, 0x13);
  106. }
  107. static void
  108. ehci_orion_conf_mbus_windows(struct usb_hcd *hcd,
  109. const struct mbus_dram_target_info *dram)
  110. {
  111. int i;
  112. for (i = 0; i < 4; i++) {
  113. wrl(USB_WINDOW_CTRL(i), 0);
  114. wrl(USB_WINDOW_BASE(i), 0);
  115. }
  116. for (i = 0; i < dram->num_cs; i++) {
  117. const struct mbus_dram_window *cs = dram->cs + i;
  118. wrl(USB_WINDOW_CTRL(i), ((cs->size - 1) & 0xffff0000) |
  119. (cs->mbus_attr << 8) |
  120. (dram->mbus_dram_target_id << 4) | 1);
  121. wrl(USB_WINDOW_BASE(i), cs->base);
  122. }
  123. }
  124. static const struct ehci_driver_overrides orion_overrides __initconst = {
  125. .extra_priv_size = sizeof(struct orion_ehci_hcd),
  126. };
  127. static int ehci_orion_drv_probe(struct platform_device *pdev)
  128. {
  129. struct orion_ehci_data *pd = dev_get_platdata(&pdev->dev);
  130. const struct mbus_dram_target_info *dram;
  131. struct resource *res;
  132. struct usb_hcd *hcd;
  133. struct ehci_hcd *ehci;
  134. void __iomem *regs;
  135. int irq, err;
  136. enum orion_ehci_phy_ver phy_version;
  137. struct orion_ehci_hcd *priv;
  138. if (usb_disabled())
  139. return -ENODEV;
  140. pr_debug("Initializing Orion-SoC USB Host Controller\n");
  141. irq = platform_get_irq(pdev, 0);
  142. if (irq <= 0) {
  143. dev_err(&pdev->dev,
  144. "Found HC with no IRQ. Check %s setup!\n",
  145. dev_name(&pdev->dev));
  146. err = -ENODEV;
  147. goto err;
  148. }
  149. /*
  150. * Right now device-tree probed devices don't get dma_mask
  151. * set. Since shared usb code relies on it, set it here for
  152. * now. Once we have dma capability bindings this can go away.
  153. */
  154. err = dma_coerce_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(32));
  155. if (err)
  156. goto err;
  157. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  158. regs = devm_ioremap_resource(&pdev->dev, res);
  159. if (IS_ERR(regs)) {
  160. err = PTR_ERR(regs);
  161. goto err;
  162. }
  163. hcd = usb_create_hcd(&ehci_orion_hc_driver,
  164. &pdev->dev, dev_name(&pdev->dev));
  165. if (!hcd) {
  166. err = -ENOMEM;
  167. goto err;
  168. }
  169. hcd->rsrc_start = res->start;
  170. hcd->rsrc_len = resource_size(res);
  171. hcd->regs = regs;
  172. ehci = hcd_to_ehci(hcd);
  173. ehci->caps = hcd->regs + 0x100;
  174. hcd->has_tt = 1;
  175. priv = hcd_to_orion_priv(hcd);
  176. /*
  177. * Not all platforms can gate the clock, so it is not an error if
  178. * the clock does not exists.
  179. */
  180. priv->clk = devm_clk_get(&pdev->dev, NULL);
  181. if (!IS_ERR(priv->clk))
  182. clk_prepare_enable(priv->clk);
  183. priv->phy = devm_phy_optional_get(&pdev->dev, "usb");
  184. if (IS_ERR(priv->phy)) {
  185. err = PTR_ERR(priv->phy);
  186. goto err_phy_get;
  187. } else {
  188. err = phy_init(priv->phy);
  189. if (err)
  190. goto err_phy_init;
  191. err = phy_power_on(priv->phy);
  192. if (err)
  193. goto err_phy_power_on;
  194. }
  195. /*
  196. * (Re-)program MBUS remapping windows if we are asked to.
  197. */
  198. dram = mv_mbus_dram_info();
  199. if (dram)
  200. ehci_orion_conf_mbus_windows(hcd, dram);
  201. /*
  202. * setup Orion USB controller.
  203. */
  204. if (pdev->dev.of_node)
  205. phy_version = EHCI_PHY_NA;
  206. else
  207. phy_version = pd->phy_version;
  208. switch (phy_version) {
  209. case EHCI_PHY_NA: /* dont change USB phy settings */
  210. break;
  211. case EHCI_PHY_ORION:
  212. orion_usb_phy_v1_setup(hcd);
  213. break;
  214. case EHCI_PHY_DD:
  215. case EHCI_PHY_KW:
  216. default:
  217. dev_warn(&pdev->dev, "USB phy version isn't supported.\n");
  218. }
  219. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  220. if (err)
  221. goto err_add_hcd;
  222. device_wakeup_enable(hcd->self.controller);
  223. return 0;
  224. err_add_hcd:
  225. if (!IS_ERR(priv->phy))
  226. phy_power_off(priv->phy);
  227. err_phy_power_on:
  228. if (!IS_ERR(priv->phy))
  229. phy_exit(priv->phy);
  230. err_phy_init:
  231. err_phy_get:
  232. if (!IS_ERR(priv->clk))
  233. clk_disable_unprepare(priv->clk);
  234. usb_put_hcd(hcd);
  235. err:
  236. dev_err(&pdev->dev, "init %s fail, %d\n",
  237. dev_name(&pdev->dev), err);
  238. return err;
  239. }
  240. static int ehci_orion_drv_remove(struct platform_device *pdev)
  241. {
  242. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  243. struct orion_ehci_hcd *priv = hcd_to_orion_priv(hcd);
  244. usb_remove_hcd(hcd);
  245. if (!IS_ERR(priv->phy)) {
  246. phy_power_off(priv->phy);
  247. phy_exit(priv->phy);
  248. }
  249. if (!IS_ERR(priv->clk))
  250. clk_disable_unprepare(priv->clk);
  251. usb_put_hcd(hcd);
  252. return 0;
  253. }
  254. static const struct of_device_id ehci_orion_dt_ids[] = {
  255. { .compatible = "marvell,orion-ehci", },
  256. {},
  257. };
  258. MODULE_DEVICE_TABLE(of, ehci_orion_dt_ids);
  259. static struct platform_driver ehci_orion_driver = {
  260. .probe = ehci_orion_drv_probe,
  261. .remove = ehci_orion_drv_remove,
  262. .shutdown = usb_hcd_platform_shutdown,
  263. .driver = {
  264. .name = "orion-ehci",
  265. .of_match_table = ehci_orion_dt_ids,
  266. },
  267. };
  268. static int __init ehci_orion_init(void)
  269. {
  270. if (usb_disabled())
  271. return -ENODEV;
  272. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  273. ehci_init_driver(&ehci_orion_hc_driver, &orion_overrides);
  274. return platform_driver_register(&ehci_orion_driver);
  275. }
  276. module_init(ehci_orion_init);
  277. static void __exit ehci_orion_cleanup(void)
  278. {
  279. platform_driver_unregister(&ehci_orion_driver);
  280. }
  281. module_exit(ehci_orion_cleanup);
  282. MODULE_DESCRIPTION(DRIVER_DESC);
  283. MODULE_ALIAS("platform:orion-ehci");
  284. MODULE_AUTHOR("Tzachi Perelstein");
  285. MODULE_LICENSE("GPL v2");