mpfs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * PolarFire SoC (MPFS) MUSB Glue Layer
  4. *
  5. * Copyright (c) 2020-2022 Microchip Corporation. All rights reserved.
  6. * Based on {omap2430,tusb6010,ux500}.c
  7. *
  8. */
  9. #include <linux/clk.h>
  10. #include <linux/dma-mapping.h>
  11. #include <linux/err.h>
  12. #include <linux/io.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/of.h>
  16. #include <linux/platform_device.h>
  17. #include <linux/usb/usb_phy_generic.h>
  18. #include "musb_core.h"
  19. #include "musb_dma.h"
  20. #define MPFS_MUSB_MAX_EP_NUM 8
  21. #define MPFS_MUSB_RAM_BITS 12
  22. struct mpfs_glue {
  23. struct device *dev;
  24. struct platform_device *musb;
  25. struct platform_device *phy;
  26. struct clk *clk;
  27. };
  28. static struct musb_fifo_cfg mpfs_musb_mode_cfg[] = {
  29. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  30. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  31. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  32. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  33. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  34. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  35. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 1024, },
  36. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 4096, },
  37. };
  38. static const struct musb_hdrc_config mpfs_musb_hdrc_config = {
  39. .fifo_cfg = mpfs_musb_mode_cfg,
  40. .fifo_cfg_size = ARRAY_SIZE(mpfs_musb_mode_cfg),
  41. .multipoint = true,
  42. .dyn_fifo = true,
  43. .num_eps = MPFS_MUSB_MAX_EP_NUM,
  44. .ram_bits = MPFS_MUSB_RAM_BITS,
  45. };
  46. static void mpfs_musb_set_vbus(struct musb *musb, int is_on)
  47. {
  48. u8 devctl;
  49. /*
  50. * HDRC controls CPEN, but beware current surges during device
  51. * connect. They can trigger transient overcurrent conditions
  52. * that must be ignored.
  53. */
  54. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  55. if (is_on) {
  56. musb->is_active = 1;
  57. musb->xceiv->otg->default_a = 1;
  58. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  59. devctl |= MUSB_DEVCTL_SESSION;
  60. MUSB_HST_MODE(musb);
  61. } else {
  62. musb->is_active = 0;
  63. /*
  64. * NOTE: skipping A_WAIT_VFALL -> A_IDLE and
  65. * jumping right to B_IDLE...
  66. */
  67. musb->xceiv->otg->default_a = 0;
  68. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  69. devctl &= ~MUSB_DEVCTL_SESSION;
  70. MUSB_DEV_MODE(musb);
  71. }
  72. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  73. dev_dbg(musb->controller, "VBUS %s, devctl %02x\n",
  74. usb_otg_state_string(musb->xceiv->otg->state),
  75. musb_readb(musb->mregs, MUSB_DEVCTL));
  76. }
  77. #define POLL_SECONDS 2
  78. static void otg_timer(struct timer_list *t)
  79. {
  80. struct musb *musb = from_timer(musb, t, dev_timer);
  81. void __iomem *mregs = musb->mregs;
  82. u8 devctl;
  83. unsigned long flags;
  84. /*
  85. * We poll because PolarFire SoC won't expose several OTG-critical
  86. * status change events (from the transceiver) otherwise.
  87. */
  88. devctl = musb_readb(mregs, MUSB_DEVCTL);
  89. dev_dbg(musb->controller, "Poll devctl %02x (%s)\n", devctl,
  90. usb_otg_state_string(musb->xceiv->otg->state));
  91. spin_lock_irqsave(&musb->lock, flags);
  92. switch (musb->xceiv->otg->state) {
  93. case OTG_STATE_A_WAIT_BCON:
  94. devctl &= ~MUSB_DEVCTL_SESSION;
  95. musb_writeb(musb->mregs, MUSB_DEVCTL, devctl);
  96. devctl = musb_readb(musb->mregs, MUSB_DEVCTL);
  97. if (devctl & MUSB_DEVCTL_BDEVICE) {
  98. musb->xceiv->otg->state = OTG_STATE_B_IDLE;
  99. MUSB_DEV_MODE(musb);
  100. mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
  101. } else {
  102. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  103. MUSB_HST_MODE(musb);
  104. }
  105. break;
  106. case OTG_STATE_A_WAIT_VFALL:
  107. if (devctl & MUSB_DEVCTL_VBUS) {
  108. mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
  109. break;
  110. }
  111. musb->xceiv->otg->state = OTG_STATE_A_WAIT_VRISE;
  112. break;
  113. case OTG_STATE_B_IDLE:
  114. /*
  115. * There's no ID-changed IRQ, so we have no good way to tell
  116. * when to switch to the A-Default state machine (by setting
  117. * the DEVCTL.Session bit).
  118. *
  119. * Workaround: whenever we're in B_IDLE, try setting the
  120. * session flag every few seconds. If it works, ID was
  121. * grounded and we're now in the A-Default state machine.
  122. *
  123. * NOTE: setting the session flag is _supposed_ to trigger
  124. * SRP but clearly it doesn't.
  125. */
  126. musb_writeb(mregs, MUSB_DEVCTL, devctl | MUSB_DEVCTL_SESSION);
  127. devctl = musb_readb(mregs, MUSB_DEVCTL);
  128. if (devctl & MUSB_DEVCTL_BDEVICE)
  129. mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
  130. else
  131. musb->xceiv->otg->state = OTG_STATE_A_IDLE;
  132. break;
  133. default:
  134. break;
  135. }
  136. spin_unlock_irqrestore(&musb->lock, flags);
  137. }
  138. static void __maybe_unused mpfs_musb_try_idle(struct musb *musb, unsigned long timeout)
  139. {
  140. static unsigned long last_timer;
  141. if (timeout == 0)
  142. timeout = jiffies + msecs_to_jiffies(3);
  143. /* Never idle if active, or when VBUS timeout is not set as host */
  144. if (musb->is_active || (musb->a_wait_bcon == 0 &&
  145. musb->xceiv->otg->state == OTG_STATE_A_WAIT_BCON)) {
  146. dev_dbg(musb->controller, "%s active, deleting timer\n",
  147. usb_otg_state_string(musb->xceiv->otg->state));
  148. del_timer(&musb->dev_timer);
  149. last_timer = jiffies;
  150. return;
  151. }
  152. if (time_after(last_timer, timeout) && timer_pending(&musb->dev_timer)) {
  153. dev_dbg(musb->controller, "Longer idle timer already pending, ignoring...\n");
  154. return;
  155. }
  156. last_timer = timeout;
  157. dev_dbg(musb->controller, "%s inactive, starting idle timer for %u ms\n",
  158. usb_otg_state_string(musb->xceiv->otg->state),
  159. jiffies_to_msecs(timeout - jiffies));
  160. mod_timer(&musb->dev_timer, timeout);
  161. }
  162. static irqreturn_t mpfs_musb_interrupt(int irq, void *__hci)
  163. {
  164. unsigned long flags;
  165. irqreturn_t ret = IRQ_NONE;
  166. struct musb *musb = __hci;
  167. spin_lock_irqsave(&musb->lock, flags);
  168. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  169. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  170. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  171. if (musb->int_usb || musb->int_tx || musb->int_rx) {
  172. musb_writeb(musb->mregs, MUSB_INTRUSB, musb->int_usb);
  173. musb_writew(musb->mregs, MUSB_INTRTX, musb->int_tx);
  174. musb_writew(musb->mregs, MUSB_INTRRX, musb->int_rx);
  175. ret = musb_interrupt(musb);
  176. }
  177. /* Poll for ID change */
  178. if (musb->xceiv->otg->state == OTG_STATE_B_IDLE)
  179. mod_timer(&musb->dev_timer, jiffies + POLL_SECONDS * HZ);
  180. spin_unlock_irqrestore(&musb->lock, flags);
  181. return ret;
  182. }
  183. static int mpfs_musb_init(struct musb *musb)
  184. {
  185. struct device *dev = musb->controller;
  186. musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  187. if (IS_ERR(musb->xceiv)) {
  188. dev_err(dev, "HS UDC: no transceiver configured\n");
  189. return PTR_ERR(musb->xceiv);
  190. }
  191. timer_setup(&musb->dev_timer, otg_timer, 0);
  192. musb->dyn_fifo = true;
  193. musb->isr = mpfs_musb_interrupt;
  194. musb_platform_set_vbus(musb, 1);
  195. return 0;
  196. }
  197. static int mpfs_musb_exit(struct musb *musb)
  198. {
  199. del_timer_sync(&musb->dev_timer);
  200. return 0;
  201. }
  202. static const struct musb_platform_ops mpfs_ops = {
  203. .quirks = MUSB_DMA_INVENTRA,
  204. .init = mpfs_musb_init,
  205. .exit = mpfs_musb_exit,
  206. .fifo_mode = 2,
  207. #ifdef CONFIG_USB_INVENTRA_DMA
  208. .dma_init = musbhs_dma_controller_create,
  209. .dma_exit = musbhs_dma_controller_destroy,
  210. #endif
  211. #ifndef CONFIG_USB_MUSB_HOST
  212. .try_idle = mpfs_musb_try_idle,
  213. #endif
  214. .set_vbus = mpfs_musb_set_vbus
  215. };
  216. static int mpfs_probe(struct platform_device *pdev)
  217. {
  218. struct musb_hdrc_platform_data *pdata = dev_get_platdata(&pdev->dev);
  219. struct mpfs_glue *glue;
  220. struct platform_device *musb_pdev;
  221. struct device *dev = &pdev->dev;
  222. struct clk *clk;
  223. int ret;
  224. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  225. if (!glue)
  226. return -ENOMEM;
  227. musb_pdev = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  228. if (!musb_pdev) {
  229. dev_err(dev, "failed to allocate musb device\n");
  230. return -ENOMEM;
  231. }
  232. clk = devm_clk_get(&pdev->dev, NULL);
  233. if (IS_ERR(clk)) {
  234. dev_err(&pdev->dev, "failed to get clock\n");
  235. ret = PTR_ERR(clk);
  236. goto err_phy_release;
  237. }
  238. ret = clk_prepare_enable(clk);
  239. if (ret) {
  240. dev_err(&pdev->dev, "failed to enable clock\n");
  241. goto err_phy_release;
  242. }
  243. musb_pdev->dev.parent = dev;
  244. musb_pdev->dev.coherent_dma_mask = DMA_BIT_MASK(39);
  245. musb_pdev->dev.dma_mask = &musb_pdev->dev.coherent_dma_mask;
  246. device_set_of_node_from_dev(&musb_pdev->dev, dev);
  247. glue->dev = dev;
  248. glue->musb = musb_pdev;
  249. glue->clk = clk;
  250. pdata = devm_kzalloc(dev, sizeof(*pdata), GFP_KERNEL);
  251. if (!pdata) {
  252. ret = -ENOMEM;
  253. goto err_clk_disable;
  254. }
  255. pdata->config = &mpfs_musb_hdrc_config;
  256. pdata->platform_ops = &mpfs_ops;
  257. pdata->extvbus = device_property_read_bool(dev, "microchip,ext-vbus-drv");
  258. pdata->mode = usb_get_dr_mode(dev);
  259. if (pdata->mode == USB_DR_MODE_UNKNOWN) {
  260. dev_info(dev, "No dr_mode property found, defaulting to otg\n");
  261. pdata->mode = USB_DR_MODE_OTG;
  262. }
  263. glue->phy = usb_phy_generic_register();
  264. if (IS_ERR(glue->phy)) {
  265. dev_err(dev, "failed to register usb-phy %ld\n",
  266. PTR_ERR(glue->phy));
  267. ret = PTR_ERR(glue->phy);
  268. goto err_clk_disable;
  269. }
  270. platform_set_drvdata(pdev, glue);
  271. ret = platform_device_add_resources(musb_pdev, pdev->resource, pdev->num_resources);
  272. if (ret) {
  273. dev_err(dev, "failed to add resources\n");
  274. goto err_clk_disable;
  275. }
  276. ret = platform_device_add_data(musb_pdev, pdata, sizeof(*pdata));
  277. if (ret) {
  278. dev_err(dev, "failed to add platform_data\n");
  279. goto err_clk_disable;
  280. }
  281. ret = platform_device_add(musb_pdev);
  282. if (ret) {
  283. dev_err(dev, "failed to register musb device\n");
  284. goto err_clk_disable;
  285. }
  286. dev_info(&pdev->dev, "Registered MPFS MUSB driver\n");
  287. return 0;
  288. err_clk_disable:
  289. clk_disable_unprepare(clk);
  290. err_phy_release:
  291. usb_phy_generic_unregister(glue->phy);
  292. platform_device_put(musb_pdev);
  293. return ret;
  294. }
  295. static void mpfs_remove(struct platform_device *pdev)
  296. {
  297. struct mpfs_glue *glue = platform_get_drvdata(pdev);
  298. clk_disable_unprepare(glue->clk);
  299. platform_device_unregister(glue->musb);
  300. usb_phy_generic_unregister(pdev);
  301. }
  302. #ifdef CONFIG_OF
  303. static const struct of_device_id mpfs_id_table[] = {
  304. { .compatible = "microchip,mpfs-musb" },
  305. { }
  306. };
  307. MODULE_DEVICE_TABLE(of, mpfs_id_table);
  308. #endif
  309. static struct platform_driver mpfs_musb_driver = {
  310. .probe = mpfs_probe,
  311. .remove_new = mpfs_remove,
  312. .driver = {
  313. .name = "mpfs-musb",
  314. .of_match_table = of_match_ptr(mpfs_id_table)
  315. },
  316. };
  317. module_platform_driver(mpfs_musb_driver);
  318. MODULE_DESCRIPTION("PolarFire SoC MUSB Glue Layer");
  319. MODULE_LICENSE("GPL");