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 int ehci_orion_drv_probe(struct platform_device *pdev)
  119. {
  120. struct orion_ehci_data *pd = pdev->dev.platform_data;
  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. if (!pdev->dev.dma_mask)
  157. pdev->dev.dma_mask = &pdev->dev.coherent_dma_mask;
  158. if (!pdev->dev.coherent_dma_mask)
  159. pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  160. if (!request_mem_region(res->start, resource_size(res),
  161. ehci_orion_hc_driver.description)) {
  162. dev_dbg(&pdev->dev, "controller already in use\n");
  163. err = -EBUSY;
  164. goto err1;
  165. }
  166. regs = ioremap(res->start, resource_size(res));
  167. if (regs == NULL) {
  168. dev_dbg(&pdev->dev, "error mapping memory\n");
  169. err = -EFAULT;
  170. goto err2;
  171. }
  172. /* Not all platforms can gate the clock, so it is not
  173. an error if the clock does not exists. */
  174. clk = clk_get(&pdev->dev, NULL);
  175. if (!IS_ERR(clk)) {
  176. clk_prepare_enable(clk);
  177. clk_put(clk);
  178. }
  179. hcd = usb_create_hcd(&ehci_orion_hc_driver,
  180. &pdev->dev, dev_name(&pdev->dev));
  181. if (!hcd) {
  182. err = -ENOMEM;
  183. goto err3;
  184. }
  185. hcd->rsrc_start = res->start;
  186. hcd->rsrc_len = resource_size(res);
  187. hcd->regs = regs;
  188. ehci = hcd_to_ehci(hcd);
  189. ehci->caps = hcd->regs + 0x100;
  190. hcd->has_tt = 1;
  191. /*
  192. * (Re-)program MBUS remapping windows if we are asked to.
  193. */
  194. dram = mv_mbus_dram_info();
  195. if (dram)
  196. ehci_orion_conf_mbus_windows(hcd, dram);
  197. /*
  198. * setup Orion USB controller.
  199. */
  200. if (pdev->dev.of_node)
  201. phy_version = EHCI_PHY_NA;
  202. else
  203. phy_version = pd->phy_version;
  204. switch (phy_version) {
  205. case EHCI_PHY_NA: /* dont change USB phy settings */
  206. break;
  207. case EHCI_PHY_ORION:
  208. orion_usb_phy_v1_setup(hcd);
  209. break;
  210. case EHCI_PHY_DD:
  211. case EHCI_PHY_KW:
  212. default:
  213. printk(KERN_WARNING "Orion ehci -USB phy version isn't supported.\n");
  214. }
  215. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  216. if (err)
  217. goto err4;
  218. return 0;
  219. err4:
  220. usb_put_hcd(hcd);
  221. err3:
  222. if (!IS_ERR(clk)) {
  223. clk_disable_unprepare(clk);
  224. clk_put(clk);
  225. }
  226. iounmap(regs);
  227. err2:
  228. release_mem_region(res->start, resource_size(res));
  229. err1:
  230. dev_err(&pdev->dev, "init %s fail, %d\n",
  231. dev_name(&pdev->dev), err);
  232. return err;
  233. }
  234. static int ehci_orion_drv_remove(struct platform_device *pdev)
  235. {
  236. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  237. struct clk *clk;
  238. usb_remove_hcd(hcd);
  239. iounmap(hcd->regs);
  240. release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
  241. usb_put_hcd(hcd);
  242. clk = clk_get(&pdev->dev, NULL);
  243. if (!IS_ERR(clk)) {
  244. clk_disable_unprepare(clk);
  245. clk_put(clk);
  246. }
  247. return 0;
  248. }
  249. static const struct of_device_id ehci_orion_dt_ids[] = {
  250. { .compatible = "marvell,orion-ehci", },
  251. {},
  252. };
  253. MODULE_DEVICE_TABLE(of, ehci_orion_dt_ids);
  254. static struct platform_driver ehci_orion_driver = {
  255. .probe = ehci_orion_drv_probe,
  256. .remove = ehci_orion_drv_remove,
  257. .shutdown = usb_hcd_platform_shutdown,
  258. .driver = {
  259. .name = "orion-ehci",
  260. .owner = THIS_MODULE,
  261. .of_match_table = ehci_orion_dt_ids,
  262. },
  263. };
  264. static int __init ehci_orion_init(void)
  265. {
  266. if (usb_disabled())
  267. return -ENODEV;
  268. pr_info("%s: " DRIVER_DESC "\n", hcd_name);
  269. ehci_init_driver(&ehci_orion_hc_driver, NULL);
  270. return platform_driver_register(&ehci_orion_driver);
  271. }
  272. module_init(ehci_orion_init);
  273. static void __exit ehci_orion_cleanup(void)
  274. {
  275. platform_driver_unregister(&ehci_orion_driver);
  276. }
  277. module_exit(ehci_orion_cleanup);
  278. MODULE_DESCRIPTION(DRIVER_DESC);
  279. MODULE_ALIAS("platform:orion-ehci");
  280. MODULE_AUTHOR("Tzachi Perelstein");
  281. MODULE_LICENSE("GPL v2");