jz4740.c 5.2 KB

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