ehci-oxnas.c 9.6 KB

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