ehci-orion.c 7.2 KB

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