ehci-orion.c 8.1 KB

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