jz4740.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Ingenic JZ4740 "glue layer"
  4. *
  5. * Copyright (C) 2013, Apelete Seketeli <apelete@seketeli.net>
  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/platform_device.h>
  13. #include <linux/usb/usb_phy_generic.h>
  14. #include "musb_core.h"
  15. struct jz4740_glue {
  16. struct device *dev;
  17. struct platform_device *musb;
  18. struct clk *clk;
  19. };
  20. static irqreturn_t jz4740_musb_interrupt(int irq, void *__hci)
  21. {
  22. unsigned long flags;
  23. irqreturn_t retval = IRQ_NONE;
  24. struct musb *musb = __hci;
  25. spin_lock_irqsave(&musb->lock, flags);
  26. musb->int_usb = musb_readb(musb->mregs, MUSB_INTRUSB);
  27. musb->int_tx = musb_readw(musb->mregs, MUSB_INTRTX);
  28. musb->int_rx = musb_readw(musb->mregs, MUSB_INTRRX);
  29. /*
  30. * The controller is gadget only, the state of the host mode IRQ bits is
  31. * undefined. Mask them to make sure that the musb driver core will
  32. * never see them set
  33. */
  34. musb->int_usb &= MUSB_INTR_SUSPEND | MUSB_INTR_RESUME |
  35. MUSB_INTR_RESET | MUSB_INTR_SOF;
  36. if (musb->int_usb || musb->int_tx || musb->int_rx)
  37. retval = musb_interrupt(musb);
  38. spin_unlock_irqrestore(&musb->lock, flags);
  39. return retval;
  40. }
  41. static struct musb_fifo_cfg jz4740_musb_fifo_cfg[] = {
  42. { .hw_ep_num = 1, .style = FIFO_TX, .maxpacket = 512, },
  43. { .hw_ep_num = 1, .style = FIFO_RX, .maxpacket = 512, },
  44. { .hw_ep_num = 2, .style = FIFO_TX, .maxpacket = 64, },
  45. };
  46. static const struct musb_hdrc_config jz4740_musb_config = {
  47. /* Silicon does not implement USB OTG. */
  48. .multipoint = 0,
  49. /* Max EPs scanned, driver will decide which EP can be used. */
  50. .num_eps = 4,
  51. /* RAMbits needed to configure EPs from table */
  52. .ram_bits = 9,
  53. .fifo_cfg = jz4740_musb_fifo_cfg,
  54. .fifo_cfg_size = ARRAY_SIZE(jz4740_musb_fifo_cfg),
  55. };
  56. static struct musb_hdrc_platform_data jz4740_musb_platform_data = {
  57. .mode = MUSB_PERIPHERAL,
  58. .config = &jz4740_musb_config,
  59. };
  60. static int jz4740_musb_init(struct musb *musb)
  61. {
  62. usb_phy_generic_register();
  63. musb->xceiv = usb_get_phy(USB_PHY_TYPE_USB2);
  64. if (IS_ERR(musb->xceiv)) {
  65. pr_err("HS UDC: no transceiver configured\n");
  66. return PTR_ERR(musb->xceiv);
  67. }
  68. /* Silicon does not implement ConfigData register.
  69. * Set dyn_fifo to avoid reading EP config from hardware.
  70. */
  71. musb->dyn_fifo = true;
  72. musb->isr = jz4740_musb_interrupt;
  73. return 0;
  74. }
  75. static int jz4740_musb_exit(struct musb *musb)
  76. {
  77. usb_put_phy(musb->xceiv);
  78. return 0;
  79. }
  80. /*
  81. * DMA has not been confirmed to work with CONFIG_USB_INVENTRA_DMA,
  82. * so let's not set up the dma function pointers yet.
  83. */
  84. static const struct musb_platform_ops jz4740_musb_ops = {
  85. .quirks = MUSB_DMA_INVENTRA | MUSB_INDEXED_EP,
  86. .fifo_mode = 2,
  87. .init = jz4740_musb_init,
  88. .exit = jz4740_musb_exit,
  89. };
  90. static int jz4740_probe(struct platform_device *pdev)
  91. {
  92. struct musb_hdrc_platform_data *pdata = &jz4740_musb_platform_data;
  93. struct platform_device *musb;
  94. struct jz4740_glue *glue;
  95. struct clk *clk;
  96. int ret;
  97. glue = devm_kzalloc(&pdev->dev, sizeof(*glue), GFP_KERNEL);
  98. if (!glue)
  99. return -ENOMEM;
  100. musb = platform_device_alloc("musb-hdrc", PLATFORM_DEVID_AUTO);
  101. if (!musb) {
  102. dev_err(&pdev->dev, "failed to allocate musb device\n");
  103. return -ENOMEM;
  104. }
  105. clk = devm_clk_get(&pdev->dev, "udc");
  106. if (IS_ERR(clk)) {
  107. dev_err(&pdev->dev, "failed to get clock\n");
  108. ret = PTR_ERR(clk);
  109. goto err_platform_device_put;
  110. }
  111. ret = clk_prepare_enable(clk);
  112. if (ret) {
  113. dev_err(&pdev->dev, "failed to enable clock\n");
  114. goto err_platform_device_put;
  115. }
  116. musb->dev.parent = &pdev->dev;
  117. glue->dev = &pdev->dev;
  118. glue->musb = musb;
  119. glue->clk = clk;
  120. pdata->platform_ops = &jz4740_musb_ops;
  121. platform_set_drvdata(pdev, glue);
  122. ret = platform_device_add_resources(musb, pdev->resource,
  123. pdev->num_resources);
  124. if (ret) {
  125. dev_err(&pdev->dev, "failed to add resources\n");
  126. goto err_clk_disable;
  127. }
  128. ret = platform_device_add_data(musb, pdata, sizeof(*pdata));
  129. if (ret) {
  130. dev_err(&pdev->dev, "failed to add platform_data\n");
  131. goto err_clk_disable;
  132. }
  133. ret = platform_device_add(musb);
  134. if (ret) {
  135. dev_err(&pdev->dev, "failed to register musb device\n");
  136. goto err_clk_disable;
  137. }
  138. return 0;
  139. err_clk_disable:
  140. clk_disable_unprepare(clk);
  141. err_platform_device_put:
  142. platform_device_put(musb);
  143. return ret;
  144. }
  145. static int jz4740_remove(struct platform_device *pdev)
  146. {
  147. struct jz4740_glue *glue = platform_get_drvdata(pdev);
  148. platform_device_unregister(glue->musb);
  149. usb_phy_generic_unregister(pdev);
  150. clk_disable_unprepare(glue->clk);
  151. return 0;
  152. }
  153. static struct platform_driver jz4740_driver = {
  154. .probe = jz4740_probe,
  155. .remove = jz4740_remove,
  156. .driver = {
  157. .name = "musb-jz4740",
  158. },
  159. };
  160. MODULE_DESCRIPTION("JZ4740 MUSB Glue Layer");
  161. MODULE_AUTHOR("Apelete Seketeli <apelete@seketeli.net>");
  162. MODULE_LICENSE("GPL v2");
  163. module_platform_driver(jz4740_driver);