jz4740.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Ingenic JZ4740 "glue layer"
  4. *
  5. * Copyright (C) 2013, Apelete Seketeli <[email protected]>
  6. */
  7. #include <linux/clk.h>
  8. #include <linux/dma-mapping.h>
  9. #include <linux/errno.h>
  10. #include <linux/kernel.h>
  11. #include <linux/module.h>
  12. #include <linux/of_device.h>
  13. #include <linux/platform_device.h>
  14. #include <linux/usb/role.h>
  15. #include <linux/usb/usb_phy_generic.h>
  16. #include "musb_core.h"
  17. struct jz4740_glue {
  18. struct platform_device *pdev;
  19. struct musb *musb;
  20. struct clk *clk;
  21. struct usb_role_switch *role_sw;
  22. };
  23. static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci)
  24. {
  25. unsigned long flags;
  26. irqreturn_t retval = IRQ_NONE, retval_dma = IRQ_NONE;
  27. struct musb *musb = __hci;
  28. if (IS_ENABLED(CONFIG_USB_INVENTRA_DMA) && musb->dma_controller)
  29. retval_dma = dma_controller_irq(irq, musb->dma_controller);
  30. spin_lock_irqsave(&musb->lock, flags);
  31. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  32. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  33. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  34. /*
  35. * The controller is gadget only, the state of the host mode IRQ bits is
  36. * undefined. Mask them to make sure that the musb driver core will
  37. * never see them set
  38. */
  39. musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME |
  40. MUSB_INTR_RESET | MUSB_INTR_SOF;
  41. if (musb->int_usb || musb->int_tx || musb->int_rx)
  42. retval = musb_interrupt(musb);
  43. spin_unlock_irqrestore(&musb->lock, flags);
  44. if (retval == IRQ_HANDLED || retval_dma == IRQ_HANDLED)
  45. return IRQ_HANDLED;
  46. return IRQ_NONE;
  47. }
  48. static struct musb_fifo_cfg jz4740_musb_fifo_cfg[] = {
  49. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  50. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  51. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, },
  52. };
  53. static const struct musb_hdrc_config jz4740_musb_config = {
  54. /* Silicon does not implement USB OTG. */
  55. .multipoint = 0,
  56. /* Max EPs scanned, driver will decide which EP can be used. */
  57. .num_eps = 4,
  58. /* RAMbits needed to configure EPs from table */
  59. .ram_bits = 9,
  60. .fifo_cfg = jz4740_musb_fifo_cfg,
  61. .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg),
  62. };
  63. static int jz4740_musb_role_switch_set(struct usb_role_switch *sw,
  64. enum usb_role role)
  65. {
  66. struct jz4740_glue *glue = usb_role_switch_get_drvdata(sw);
  67. struct usb_phy *phy = glue->musb->xceiv;
  68. switch (role) {
  69. case USB_ROLE_NONE:
  70. atomic_notifier_call_chain(&phy->notifier, USB_EVENT_NONE, phy);
  71. break;
  72. case USB_ROLE_DEVICE:
  73. atomic_notifier_call_chain(&phy->notifier, USB_EVENT_VBUS, phy);
  74. break;
  75. case USB_ROLE_HOST:
  76. atomic_notifier_call_chain(&phy->notifier, USB_EVENT_ID, phy);
  77. break;
  78. }
  79. return 0;
  80. }
  81. static int jz4740_musb_init(struct musb *musb)
  82. {
  83. struct device *dev = musb->controller->parent;
  84. struct jz4740_glue *glue = dev_get_drvdata(dev);
  85. struct usb_role_switch_desc role_sw_desc = {
  86. .set = jz4740_musb_role_switch_set,
  87. .driver_data = glue,
  88. .fwnode = dev_fwnode(dev),
  89. };
  90. glue->musb = musb;
  91. if (dev->of_node)
  92. musb->xceiv = devm_usb_get_phy_by_phandle(dev, "phys", 0);
  93. else
  94. musb->xceiv = devm_usb_get_phy(dev, USB_PHY_TYPE_USB2);
  95. if (IS_ERR(musb->xceiv))
  96. return dev_err_probe(dev, PTR_ERR(musb->xceiv),
  97. "No transceiver configured\n");
  98. glue->role_sw = usb_role_switch_register(dev, &role_sw_desc);
  99. if (IS_ERR(glue->role_sw)) {
  100. dev_err(dev, "Failed to register USB role switch\n");
  101. return PTR_ERR(glue->role_sw);
  102. }
  103. /*
  104. * Silicon does not implement ConfigData register.
  105. * Set dyn_fifo to avoid reading EP config from hardware.
  106. */
  107. musb->dyn_fifo = true;
  108. musb->isr = jz4740_musb_interrupt;
  109. return 0;
  110. }
  111. static int jz4740_musb_exit(struct musb *musb)
  112. {
  113. struct jz4740_glue *glue = dev_get_drvdata(musb->controller->parent);
  114. usb_role_switch_unregister(glue->role_sw);
  115. return 0;
  116. }
  117. static const struct musb_platform_ops jz4740_musb_ops = {
  118. .quirks = MUSB_DMA_INVENTRA | MUSB_INDEXED_EP,
  119. .fifo_mode = 2,
  120. .init = jz4740_musb_init,
  121. .exit = jz4740_musb_exit,
  122. #ifdef CONFIG_USB_INVENTRA_DMA
  123. .dma_init = musbhs_dma_controller_create_noirq,
  124. .dma_exit = musbhs_dma_controller_destroy,
  125. #endif
  126. };
  127. static const struct musb_hdrc_platform_data jz4740_musb_pdata = {
  128. .mode = MUSB_PERIPHERAL,
  129. .config = &jz4740_musb_config,
  130. .platform_ops = &jz4740_musb_ops,
  131. };
  132. static struct musb_fifo_cfg jz4770_musb_fifo_cfg[] = {
  133. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  134. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  135. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 512, },
  136. { .hw_ep_num = 2, .style = FIFO_RX, .maxpacket = 512, },
  137. { .hw_ep_num = 3, .style = FIFO_TX, .maxpacket = 512, },
  138. { .hw_ep_num = 3, .style = FIFO_RX, .maxpacket = 512, },
  139. { .hw_ep_num = 4, .style = FIFO_TX, .maxpacket = 512, },
  140. { .hw_ep_num = 4, .style = FIFO_RX, .maxpacket = 512, },
  141. { .hw_ep_num = 5, .style = FIFO_TX, .maxpacket = 512, },
  142. { .hw_ep_num = 5, .style = FIFO_RX, .maxpacket = 512, },
  143. };
  144. static struct musb_hdrc_config jz4770_musb_config = {
  145. .multipoint = 1,
  146. .num_eps = 11,
  147. .ram_bits = 11,
  148. .fifo_cfg = jz4770_musb_fifo_cfg,
  149. .fifo_cfg_size = ARRAY_SIZE(jz4770_musb_fifo_cfg),
  150. };
  151. static const struct musb_hdrc_platform_data jz4770_musb_pdata = {
  152. .mode = MUSB_PERIPHERAL, /* TODO: support OTG */
  153. .config = &jz4770_musb_config,
  154. .platform_ops = &jz4740_musb_ops,
  155. };
  156. static int jz4740_probe(struct platform_device *pdev)
  157. {
  158. struct device *dev = &pdev->dev;
  159. const struct musb_hdrc_platform_data *pdata;
  160. struct platform_device *musb;
  161. struct jz4740_glue *glue;
  162. struct clk *clk;
  163. int ret;
  164. glue = devm_kzalloc(dev, sizeof(*glue), GFP_KERNEL);
  165. if (!glue)
  166. return -ENOMEM;
  167. pdata = of_device_get_match_data(dev);
  168. if (!pdata) {
  169. dev_err(dev, "missing platform data\n");
  170. return -EINVAL;
  171. }
  172. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  173. if (!musb) {
  174. dev_err(dev, "failed to allocate musb device\n");
  175. return -ENOMEM;
  176. }
  177. clk = devm_clk_get(dev, "udc");
  178. if (IS_ERR(clk)) {
  179. dev_err(dev, "failed to get clock\n");
  180. ret = PTR_ERR(clk);
  181. goto err_platform_device_put;
  182. }
  183. ret = clk_prepare_enable(clk);
  184. if (ret) {
  185. dev_err(dev, "failed to enable clock\n");
  186. goto err_platform_device_put;
  187. }
  188. musb->dev.parent = dev;
  189. musb->dev.dma_mask = &musb->dev.coherent_dma_mask;
  190. musb->dev.coherent_dma_mask = DMA_BIT_MASK(32);
  191. device_set_of_node_from_dev(&musb->dev, dev);
  192. glue->pdev = musb;
  193. glue->clk = clk;
  194. platform_set_drvdata(pdev, glue);
  195. ret = platform_device_add_resources(musb, pdev->resource,
  196. pdev->num_resources);
  197. if (ret) {
  198. dev_err(dev, "failed to add resources\n");
  199. goto err_clk_disable;
  200. }
  201. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  202. if (ret) {
  203. dev_err(dev, "failed to add platform_data\n");
  204. goto err_clk_disable;
  205. }
  206. ret = platform_device_add(musb);
  207. if (ret) {
  208. dev_err(dev, "failed to register musb device\n");
  209. goto err_clk_disable;
  210. }
  211. return 0;
  212. err_clk_disable:
  213. clk_disable_unprepare(clk);
  214. err_platform_device_put:
  215. platform_device_put(musb);
  216. return ret;
  217. }
  218. static int jz4740_remove(struct platform_device *pdev)
  219. {
  220. struct jz4740_glue *glue = platform_get_drvdata(pdev);
  221. platform_device_unregister(glue->pdev);
  222. clk_disable_unprepare(glue->clk);
  223. return 0;
  224. }
  225. static const struct of_device_id jz4740_musb_of_match[] = {
  226. { .compatible = "ingenic,jz4740-musb", .data = &jz4740_musb_pdata },
  227. { .compatible = "ingenic,jz4770-musb", .data = &jz4770_musb_pdata },
  228. { /* sentinel */ },
  229. };
  230. MODULE_DEVICE_TABLE(of, jz4740_musb_of_match);
  231. static struct platform_driver jz4740_driver = {
  232. .probe = jz4740_probe,
  233. .remove = jz4740_remove,
  234. .driver = {
  235. .name = "musb-jz4740",
  236. .of_match_table = jz4740_musb_of_match,
  237. },
  238. };
  239. MODULE_DESCRIPTION("JZ4740 MUSB Glue Layer");
  240. MODULE_AUTHOR("Apelete Seketeli <[email protected]>");
  241. MODULE_LICENSE("GPL v2");
  242. module_platform_driver(jz4740_driver);