ehci-oxnas.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  1. /*
  2. * drivers/usb/host/ehci-oxnas.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/of.h>
  14. #include <linux/of_address.h>
  15. #include <linux/of_irq.h>
  16. #include <linux/usb.h>
  17. #include <linux/usb/hcd.h>
  18. #include <linux/dma-mapping.h>
  19. #include <linux/clk.h>
  20. #include <linux/reset.h>
  21. #include <mach/hardware.h>
  22. #include <mach/utils.h>
  23. #include "ehci.h"
  24. struct oxnas_hcd {
  25. struct clk *clk;
  26. struct clk *refsrc;
  27. struct clk *phyref;
  28. int use_pllb;
  29. int use_phya;
  30. struct reset_control *rst_host;
  31. struct reset_control *rst_phya;
  32. struct reset_control *rst_phyb;
  33. };
  34. #define DRIVER_DESC "Oxnas On-Chip EHCI Host Controller"
  35. static struct hc_driver __read_mostly oxnas_hc_driver;
  36. static void start_oxnas_usb_ehci(struct oxnas_hcd *oxnas)
  37. {
  38. u32 reg;
  39. if (oxnas->use_pllb) {
  40. /* enable pllb */
  41. clk_prepare_enable(oxnas->refsrc);
  42. /* enable ref600 */
  43. clk_prepare_enable(oxnas->phyref);
  44. /* 600MHz pllb divider for 12MHz */
  45. writel(PLLB_DIV_INT(50) | PLLB_DIV_FRAC(0),
  46. SEC_CTRL_PLLB_DIV_CTRL);
  47. } else {
  48. /* ref 300 divider for 12MHz */
  49. writel(REF300_DIV_INT(25) | REF300_DIV_FRAC(0),
  50. SYS_CTRL_REF300_DIV);
  51. }
  52. /* Ensure the USB block is properly reset */
  53. reset_control_reset(oxnas->rst_host);
  54. reset_control_reset(oxnas->rst_phya);
  55. reset_control_reset(oxnas->rst_phyb);
  56. /* Force the high speed clock to be generated all the time, via serial
  57. programming of the USB HS PHY */
  58. writel((2UL << USBHSPHY_TEST_ADD) |
  59. (0xe0UL << USBHSPHY_TEST_DIN), SYS_CTRL_USBHSPHY_CTRL);
  60. writel((1UL << USBHSPHY_TEST_CLK) |
  61. (2UL << USBHSPHY_TEST_ADD) |
  62. (0xe0UL << USBHSPHY_TEST_DIN), SYS_CTRL_USBHSPHY_CTRL);
  63. writel((0xfUL << USBHSPHY_TEST_ADD) |
  64. (0xaaUL << USBHSPHY_TEST_DIN), SYS_CTRL_USBHSPHY_CTRL);
  65. writel((1UL << USBHSPHY_TEST_CLK) |
  66. (0xfUL << USBHSPHY_TEST_ADD) |
  67. (0xaaUL << USBHSPHY_TEST_DIN), SYS_CTRL_USBHSPHY_CTRL);
  68. if (oxnas->use_pllb) /* use pllb clock */
  69. writel(USB_CLK_INTERNAL | USB_INT_CLK_PLLB, SYS_CTRL_USB_CTRL);
  70. else /* use ref300 derived clock */
  71. writel(USB_CLK_INTERNAL | USB_INT_CLK_REF300,
  72. SYS_CTRL_USB_CTRL);
  73. if (oxnas->use_phya) {
  74. /* Configure USB PHYA as a host */
  75. reg = readl(SYS_CTRL_USB_CTRL);
  76. reg &= ~USBAMUX_DEVICE;
  77. writel(reg, SYS_CTRL_USB_CTRL);
  78. }
  79. /* Enable the clock to the USB block */
  80. clk_prepare_enable(oxnas->clk);
  81. }
  82. static void stop_oxnas_usb_ehci(struct oxnas_hcd *oxnas)
  83. {
  84. reset_control_assert(oxnas->rst_host);
  85. reset_control_assert(oxnas->rst_phya);
  86. reset_control_assert(oxnas->rst_phyb);
  87. if (oxnas->use_pllb) {
  88. clk_disable_unprepare(oxnas->phyref);
  89. clk_disable_unprepare(oxnas->refsrc);
  90. }
  91. clk_disable_unprepare(oxnas->clk);
  92. }
  93. static int ehci_oxnas_reset(struct usb_hcd *hcd)
  94. {
  95. #define txttfill_tuning reserved2[0]
  96. struct ehci_hcd *ehci;
  97. u32 tmp;
  98. int retval = ehci_setup(hcd);
  99. if (retval)
  100. return retval;
  101. ehci = hcd_to_ehci(hcd);
  102. tmp = ehci_readl(ehci, &ehci->regs->txfill_tuning);
  103. tmp &= ~0x00ff0000;
  104. tmp |= 0x003f0000; /* set burst pre load count to 0x40 (63 * 4 bytes) */
  105. tmp |= 0x16; /* set sheduler overhead to 22 * 1.267us (HS) or 22 * 6.33us (FS/LS)*/
  106. ehci_writel(ehci, tmp, &ehci->regs->txfill_tuning);
  107. tmp = ehci_readl(ehci, &ehci->regs->txttfill_tuning);
  108. tmp |= 0x2; /* set sheduler overhead to 2 * 6.333us */
  109. ehci_writel(ehci, tmp, &ehci->regs->txttfill_tuning);
  110. return retval;
  111. }
  112. static int ehci_oxnas_drv_probe(struct platform_device *ofdev)
  113. {
  114. struct device_node *np = ofdev->dev.of_node;
  115. struct usb_hcd *hcd;
  116. struct ehci_hcd *ehci;
  117. struct resource res;
  118. struct oxnas_hcd *oxnas;
  119. int irq, err;
  120. struct reset_control *rstc;
  121. if (usb_disabled())
  122. return -ENODEV;
  123. if (!ofdev->dev.dma_mask)
  124. ofdev->dev.dma_mask = &ofdev->dev.coherent_dma_mask;
  125. if (!ofdev->dev.coherent_dma_mask)
  126. ofdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  127. hcd = usb_create_hcd(&oxnas_hc_driver, &ofdev->dev,
  128. dev_name(&ofdev->dev));
  129. if (!hcd)
  130. return -ENOMEM;
  131. err = of_address_to_resource(np, 0, &res);
  132. if (err)
  133. goto err_res;
  134. hcd->rsrc_start = res.start;
  135. hcd->rsrc_len = resource_size(&res);
  136. hcd->regs = devm_ioremap_resource(&ofdev->dev, &res);
  137. if (IS_ERR(hcd->regs)) {
  138. dev_err(&ofdev->dev, "devm_ioremap_resource failed\n");
  139. err = PTR_ERR(hcd->regs);
  140. goto err_ioremap;
  141. }
  142. oxnas = (struct oxnas_hcd *)hcd_to_ehci(hcd)->priv;
  143. oxnas->use_pllb = of_property_read_bool(np, "plxtech,ehci_use_pllb");
  144. oxnas->use_phya = of_property_read_bool(np, "plxtech,ehci_use_phya");
  145. oxnas->clk = of_clk_get_by_name(np, "usb");
  146. if (IS_ERR(oxnas->clk)) {
  147. err = PTR_ERR(oxnas->clk);
  148. goto err_clk;
  149. }
  150. if (oxnas->use_pllb) {
  151. oxnas->refsrc = of_clk_get_by_name(np, "refsrc");
  152. if (IS_ERR(oxnas->refsrc)) {
  153. err = PTR_ERR(oxnas->refsrc);
  154. goto err_refsrc;
  155. }
  156. oxnas->phyref = of_clk_get_by_name(np, "phyref");
  157. if (IS_ERR(oxnas->refsrc)) {
  158. err = PTR_ERR(oxnas->refsrc);
  159. goto err_phyref;
  160. }
  161. } else {
  162. oxnas->refsrc = NULL;
  163. oxnas->phyref = NULL;
  164. }
  165. rstc = devm_reset_control_get(&ofdev->dev, "host");
  166. if (IS_ERR(rstc)) {
  167. err = PTR_ERR(rstc);
  168. goto err_rst;
  169. }
  170. oxnas->rst_host = rstc;
  171. rstc = devm_reset_control_get(&ofdev->dev, "phya");
  172. if (IS_ERR(rstc)) {
  173. err = PTR_ERR(rstc);
  174. goto err_rst;
  175. }
  176. oxnas->rst_phya = rstc;
  177. rstc = devm_reset_control_get(&ofdev->dev, "phyb");
  178. if (IS_ERR(rstc)) {
  179. err = PTR_ERR(rstc);
  180. goto err_rst;
  181. }
  182. oxnas->rst_phyb = rstc;
  183. irq = irq_of_parse_and_map(np, 0);
  184. if (!irq) {
  185. dev_err(&ofdev->dev, "irq_of_parse_and_map failed\n");
  186. err = -EBUSY;
  187. goto err_irq;
  188. }
  189. hcd->has_tt = 1;
  190. ehci = hcd_to_ehci(hcd);
  191. ehci->caps = hcd->regs;
  192. start_oxnas_usb_ehci(oxnas);
  193. err = usb_add_hcd(hcd, irq, IRQF_SHARED);
  194. if (err)
  195. goto err_hcd;
  196. return 0;
  197. err_hcd:
  198. stop_oxnas_usb_ehci(oxnas);
  199. err_irq:
  200. err_rst:
  201. if (oxnas->phyref)
  202. clk_put(oxnas->phyref);
  203. err_phyref:
  204. if (oxnas->refsrc)
  205. clk_put(oxnas->refsrc);
  206. err_refsrc:
  207. clk_put(oxnas->clk);
  208. err_clk:
  209. err_ioremap:
  210. err_res:
  211. usb_put_hcd(hcd);
  212. return err;
  213. }
  214. static int ehci_oxnas_drv_remove(struct platform_device *pdev)
  215. {
  216. struct usb_hcd *hcd = platform_get_drvdata(pdev);
  217. struct oxnas_hcd *oxnas = (struct oxnas_hcd *)hcd_to_ehci(hcd)->priv;
  218. usb_remove_hcd(hcd);
  219. if (oxnas->use_pllb) {
  220. clk_disable_unprepare(oxnas->phyref);
  221. clk_put(oxnas->phyref);
  222. clk_disable_unprepare(oxnas->refsrc);
  223. clk_put(oxnas->refsrc);
  224. }
  225. clk_disable_unprepare(oxnas->clk);
  226. usb_put_hcd(hcd);
  227. return 0;
  228. }
  229. static const struct of_device_id oxnas_ehci_dt_ids[] = {
  230. { .compatible = "plxtech,nas782x-ehci" },
  231. { /* sentinel */ }
  232. };
  233. MODULE_DEVICE_TABLE(of, oxnas_ehci_dt_ids);
  234. static struct platform_driver ehci_oxnas_driver = {
  235. .probe = ehci_oxnas_drv_probe,
  236. .remove = ehci_oxnas_drv_remove,
  237. .shutdown = usb_hcd_platform_shutdown,
  238. .driver.name = "oxnas-ehci",
  239. .driver.of_match_table = oxnas_ehci_dt_ids,
  240. };
  241. static const struct ehci_driver_overrides oxnas_overrides __initconst = {
  242. .reset = ehci_oxnas_reset,
  243. .extra_priv_size = sizeof(struct oxnas_hcd),
  244. };
  245. static int __init ehci_oxnas_init(void)
  246. {
  247. if (usb_disabled())
  248. return -ENODEV;
  249. ehci_init_driver(&oxnas_hc_driver, &oxnas_overrides);
  250. return platform_driver_register(&ehci_oxnas_driver);
  251. }
  252. module_init(ehci_oxnas_init);
  253. static void __exit ehci_oxnas_cleanup(void)
  254. {
  255. platform_driver_unregister(&ehci_oxnas_driver);
  256. }
  257. module_exit(ehci_oxnas_cleanup);
  258. MODULE_DESCRIPTION(DRIVER_DESC);
  259. MODULE_ALIAS("platform:oxnas-ehci");
  260. MODULE_LICENSE("GPL");