dwc3-keystone.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /**
  2. * dwc3-keystone.c - Keystone Specific Glue layer
  3. *
  4. * Copyright (C) 2010-2013 Texas Instruments Incorporated - http://www.ti.com
  5. *
  6. * Author: WingMan Kwok <w-kwok2@ti.com>
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License version 2 of
  10. * the License as published by the Free Software Foundation.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. */
  17. #include <linux/clk.h>
  18. #include <linux/module.h>
  19. #include <linux/kernel.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/platform_device.h>
  22. #include <linux/dma-mapping.h>
  23. #include <linux/io.h>
  24. #include <linux/of_platform.h>
  25. /* USBSS register offsets */
  26. #define USBSS_REVISION 0x0000
  27. #define USBSS_SYSCONFIG 0x0010
  28. #define USBSS_IRQ_EOI 0x0018
  29. #define USBSS_IRQSTATUS_RAW_0 0x0020
  30. #define USBSS_IRQSTATUS_0 0x0024
  31. #define USBSS_IRQENABLE_SET_0 0x0028
  32. #define USBSS_IRQENABLE_CLR_0 0x002c
  33. /* IRQ register bits */
  34. #define USBSS_IRQ_EOI_LINE(n) BIT(n)
  35. #define USBSS_IRQ_EVENT_ST BIT(0)
  36. #define USBSS_IRQ_COREIRQ_EN BIT(0)
  37. #define USBSS_IRQ_COREIRQ_CLR BIT(0)
  38. struct dwc3_keystone {
  39. struct device *dev;
  40. struct clk *clk;
  41. void __iomem *usbss;
  42. };
  43. static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
  44. {
  45. return readl(base + offset);
  46. }
  47. static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
  48. {
  49. writel(value, base + offset);
  50. }
  51. static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
  52. {
  53. u32 val;
  54. val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
  55. val |= USBSS_IRQ_COREIRQ_EN;
  56. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
  57. }
  58. static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
  59. {
  60. u32 val;
  61. val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
  62. val &= ~USBSS_IRQ_COREIRQ_EN;
  63. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
  64. }
  65. static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
  66. {
  67. struct dwc3_keystone *kdwc = _kdwc;
  68. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
  69. kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
  70. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
  71. kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
  72. return IRQ_HANDLED;
  73. }
  74. static int kdwc3_probe(struct platform_device *pdev)
  75. {
  76. struct device *dev = &pdev->dev;
  77. struct device_node *node = pdev->dev.of_node;
  78. struct dwc3_keystone *kdwc;
  79. struct resource *res;
  80. int error, irq;
  81. kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
  82. if (!kdwc)
  83. return -ENOMEM;
  84. platform_set_drvdata(pdev, kdwc);
  85. kdwc->dev = dev;
  86. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  87. kdwc->usbss = devm_ioremap_resource(dev, res);
  88. if (IS_ERR(kdwc->usbss))
  89. return PTR_ERR(kdwc->usbss);
  90. kdwc->clk = devm_clk_get(kdwc->dev, "usb");
  91. if (IS_ERR(kdwc->clk)) {
  92. dev_err(kdwc->dev, "unable to get usb clock\n");
  93. return PTR_ERR(kdwc->clk);
  94. }
  95. error = clk_prepare_enable(kdwc->clk);
  96. if (error < 0) {
  97. dev_err(kdwc->dev, "unable to enable usb clock, error %d\n",
  98. error);
  99. return error;
  100. }
  101. irq = platform_get_irq(pdev, 0);
  102. if (irq < 0) {
  103. dev_err(&pdev->dev, "missing irq\n");
  104. error = irq;
  105. goto err_irq;
  106. }
  107. error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
  108. dev_name(dev), kdwc);
  109. if (error) {
  110. dev_err(dev, "failed to request IRQ #%d --> %d\n",
  111. irq, error);
  112. goto err_irq;
  113. }
  114. kdwc3_enable_irqs(kdwc);
  115. error = of_platform_populate(node, NULL, NULL, dev);
  116. if (error) {
  117. dev_err(&pdev->dev, "failed to create dwc3 core\n");
  118. goto err_core;
  119. }
  120. return 0;
  121. err_core:
  122. kdwc3_disable_irqs(kdwc);
  123. err_irq:
  124. clk_disable_unprepare(kdwc->clk);
  125. return error;
  126. }
  127. static int kdwc3_remove_core(struct device *dev, void *c)
  128. {
  129. struct platform_device *pdev = to_platform_device(dev);
  130. platform_device_unregister(pdev);
  131. return 0;
  132. }
  133. static int kdwc3_remove(struct platform_device *pdev)
  134. {
  135. struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
  136. kdwc3_disable_irqs(kdwc);
  137. device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
  138. clk_disable_unprepare(kdwc->clk);
  139. platform_set_drvdata(pdev, NULL);
  140. return 0;
  141. }
  142. static const struct of_device_id kdwc3_of_match[] = {
  143. { .compatible = "ti,keystone-dwc3", },
  144. {},
  145. };
  146. MODULE_DEVICE_TABLE(of, kdwc3_of_match);
  147. static struct platform_driver kdwc3_driver = {
  148. .probe = kdwc3_probe,
  149. .remove = kdwc3_remove,
  150. .driver = {
  151. .name = "keystone-dwc3",
  152. .of_match_table = kdwc3_of_match,
  153. },
  154. };
  155. module_platform_driver(kdwc3_driver);
  156. MODULE_ALIAS("platform:keystone-dwc3");
  157. MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
  158. MODULE_LICENSE("GPL v2");
  159. MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");