ehci-orion.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331
  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 u64 ehci_orion_dma_mask = DMA_BIT_MASK(32);
  119. static int ehci_orion_drv_probe(struct platform_device *pdev)
  120. {
  121. struct orion_ehci_data *pd = pdev->dev.platform_data;
  122. const struct mbus_dram_target_info *dram;
  123. struct resource *res;
  124. struct usb_hcd *hcd;
  125. struct ehci_hcd *ehci;
  126. struct clk *clk;
  127. void __iomem *regs;
  128. int irq, err;
  129. enum orion_ehci_phy_ver phy_version;
  130. if (usb_disabled())
  131. return -ENODEV;
  132. pr_debug("Initializing Orion-SoC USB Host Controller\n");
  133. if (pdev->dev.of_node)
  134. irq = irq_of_parse_and_map(pdev->dev.of_node, 0);
  135. else
  136. irq = platform_get_irq(pdev, 0);
  137. if (irq <= 0) {
  138. dev_err(&pdev->dev,
  139. "Found HC with no IRQ. Check %s setup!\n",
  140. dev_name(&pdev->dev));
  141. err = -ENODEV;
  142. goto err1;
  143. }
  144. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  145. if (!res) {
  146. dev_err(&pdev->dev,
  147. "Found HC with no register addr. Check %s setup!\n",
  148. dev_name(&pdev->dev));
  149. err = -ENODEV;
  150. goto err1;
  151. }
  152. /*
  153. * Right now device-tree probed devices don't get dma_mask
  154. * set. Since shared usb code relies on it, set it here for
  155. * now. Once we have dma capability bindings this can go away.
  156. */
  157. if (!pdev->dev.dma_mask)
  158. pdev->dev.dma_mask = &ehci_orion_dma_mask;
  159. if (!request_mem_region(res->start, resource_size(res),
  160. ehci_orion_hc_driver.description)) {
  161. dev_dbg(&pdev->dev, "controller already in use\n");
  162. err = -EBUSY;
  163. goto err1;
  164. }
  165. regs = ioremap(res->start, resource_size(res));
  166. if (regs == NULL) {
  167. dev_dbg(&pdev->dev, "error mapping memory\n");
  168. err = -EFAULT;
  169. goto err2;
  170. }
  171. /* Not all platforms can gate the clock, so it is not
  172. an error if the clock does not exists. */
  173. clk = clk_get(&pdev->dev, NULL);
  174. if (!IS_ERR(clk)) {
  175. clk_prepare_enable(clk);
  176. clk_put(clk);
  177. }
  178. hcd = usb_create_hcd(&ehci_orion_hc_driver,
  179. &pdev->dev, dev_name(&pdev->dev));
  180. if (!hcd) {
  181. err = -ENOMEM;
  182. goto err3;
  183. }
  184. hcd->rsrc_start = res->start;
  185. hcd->rsrc_len = resource_size(res);
  186. hcd->regs = regs;
  187. ehci = hcd_to_ehci(hcd);
  188. ehci->caps = hcd->regs + 0x100;
  189. hcd->has_tt = 1;
  190. /*
  191. * (Re-)program MBUS remapping windows if we are asked to.
  192. */
  193. dram = mv_mbus_dram_info();
  194. if (dram)
  195. ehci_orion_conf_mbus_windows(hcd, dram);
  196. /*
  197. * setup Orion USB controller.
  198. */
  199. if (pdev->dev.of_node)
  200. phy_version = EHCI_PHY_NA;
  201. else
  202. phy_version = pd->phy_version;
  203. switch (phy_version) {
  204. case EHCI_PHY_NA: /* dont change USB phy settings */
  205. break;
  206. case EHCI_PHY_ORION:
  207. orion_usb_phy_v1_setup(hcd);
  208. break;
  209. case EHCI_PHY_DD:
  210. case EHCI_PHY_KW:
  211. default:
  212. printk(KERN_WARNING "Orion ehci -USB phy version isn't supported.\n");
  213. }
  214. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  215. if (err)
  216. goto err4;
  217. return 0;
  218. err4:
  219. usb_put_hcd(hcd);
  220. err3:
  221. if (!IS_ERR(clk)) {
  222. clk_disable_unprepare(clk);
  223. clk_put(clk);
  224. }
  225. iounmap(regs);
  226. err2:
  227. release_mem_region(res->start, resource_size(res));
  228. err1:
  229. dev_err(&pdev->dev, "init %s fail, %d\n",
  230. dev_name(&pdev->dev), err);
  231. return err;
  232. }
  233. static int ehci_orion_drv_remove(struct platform_device *pdev)
  234. {
  235. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  236. struct clk *clk;
  237. usb_remove_hcd(hcd);
  238. iounmap(hcd->regs);
  239. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  240. usb_put_hcd(hcd);
  241. clk = clk_get(&pdev->dev, NULL);
  242. if (!IS_ERR(clk)) {
  243. clk_disable_unprepare(clk);
  244. clk_put(clk);
  245. }
  246. return 0;
  247. }
  248. static const struct of_device_id ehci_orion_dt_ids[] = {
  249. { .compatible = "marvell,orion-ehci", },
  250. {},
  251. };
  252. MODULE_DEVICE_TABLE(of, ehci_orion_dt_ids);
  253. static struct platform_driver ehci_orion_driver = {
  254. .probe = ehci_orion_drv_probe,
  255. .remove = ehci_orion_drv_remove,
  256. .shutdown = usb_hcd_platform_shutdown,
  257. .driver = {
  258. .name = "orion-ehci",
  259. .owner = THIS_MODULE,
  260. .of_match_table = of_match_ptr(ehci_orion_dt_ids),
  261. },
  262. };
  263. static int __init ehci_orion_init(void)
  264. {
  265. if (usb_disabled())
  266. return -ENODEV;
  267. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  268. ehci_init_driver(&ehci_orion_hc_driver, NULL);
  269. return platform_driver_register(&ehci_orion_driver);
  270. }
  271. module_init(ehci_orion_init);
  272. static void __exit ehci_orion_cleanup(void)
  273. {
  274. platform_driver_unregister(&ehci_orion_driver);
  275. }
  276. module_exit(ehci_orion_cleanup);
  277. MODULE_DESCRIPTION(DRIVER_DESC);
  278. MODULE_ALIAS("platform:orion-ehci");
  279. MODULE_AUTHOR("Tzachi Perelstein");
  280. MODULE_LICENSE("GPL v2");