dwc3-keystone.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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/module.h>
  18. #include <linux/kernel.h>
  19. #include <linux/interrupt.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/dma-mapping.h>
  22. #include <linux/io.h>
  23. #include <linux/of_platform.h>
  24. #include <linux/pm_runtime.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. void __iomem *usbss;
  41. };
  42. static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
  43. {
  44. return readl(base + offset);
  45. }
  46. static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
  47. {
  48. writel(value, base + offset);
  49. }
  50. static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
  51. {
  52. u32 val;
  53. val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
  54. val |= USBSS_IRQ_COREIRQ_EN;
  55. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
  56. }
  57. static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
  58. {
  59. u32 val;
  60. val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
  61. val &= ~USBSS_IRQ_COREIRQ_EN;
  62. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
  63. }
  64. static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
  65. {
  66. struct dwc3_keystone *kdwc = _kdwc;
  67. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
  68. kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
  69. kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
  70. kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
  71. return IRQ_HANDLED;
  72. }
  73. static int kdwc3_probe(struct platform_device *pdev)
  74. {
  75. struct device *dev = &pdev->dev;
  76. struct device_node *node = pdev->dev.of_node;
  77. struct dwc3_keystone *kdwc;
  78. struct resource *res;
  79. int error, irq;
  80. kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
  81. if (!kdwc)
  82. return -ENOMEM;
  83. platform_set_drvdata(pdev, kdwc);
  84. kdwc->dev = dev;
  85. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  86. kdwc->usbss = devm_ioremap_resource(dev, res);
  87. if (IS_ERR(kdwc->usbss))
  88. return PTR_ERR(kdwc->usbss);
  89. pm_runtime_enable(kdwc->dev);
  90. error = pm_runtime_get_sync(kdwc->dev);
  91. if (error < 0) {
  92. dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
  93. error);
  94. goto err_irq;
  95. }
  96. irq = platform_get_irq(pdev, 0);
  97. if (irq < 0) {
  98. dev_err(&pdev->dev, "missing irq\n");
  99. error = irq;
  100. goto err_irq;
  101. }
  102. error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
  103. dev_name(dev), kdwc);
  104. if (error) {
  105. dev_err(dev, "failed to request IRQ #%d --> %d\n",
  106. irq, error);
  107. goto err_irq;
  108. }
  109. kdwc3_enable_irqs(kdwc);
  110. error = of_platform_populate(node, NULL, NULL, dev);
  111. if (error) {
  112. dev_err(&pdev->dev, "failed to create dwc3 core\n");
  113. goto err_core;
  114. }
  115. return 0;
  116. err_core:
  117. kdwc3_disable_irqs(kdwc);
  118. err_irq:
  119. pm_runtime_put_sync(kdwc->dev);
  120. pm_runtime_disable(kdwc->dev);
  121. return error;
  122. }
  123. static int kdwc3_remove_core(struct device *dev, void *c)
  124. {
  125. struct platform_device *pdev = to_platform_device(dev);
  126. platform_device_unregister(pdev);
  127. return 0;
  128. }
  129. static int kdwc3_remove(struct platform_device *pdev)
  130. {
  131. struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
  132. kdwc3_disable_irqs(kdwc);
  133. device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
  134. pm_runtime_put_sync(kdwc->dev);
  135. pm_runtime_disable(kdwc->dev);
  136. platform_set_drvdata(pdev, NULL);
  137. return 0;
  138. }
  139. static const struct of_device_id kdwc3_of_match[] = {
  140. { .compatible = "ti,keystone-dwc3", },
  141. {},
  142. };
  143. MODULE_DEVICE_TABLE(of, kdwc3_of_match);
  144. static struct platform_driver kdwc3_driver = {
  145. .probe = kdwc3_probe,
  146. .remove = kdwc3_remove,
  147. .driver = {
  148. .name = "keystone-dwc3",
  149. .of_match_table = kdwc3_of_match,
  150. },
  151. };
  152. module_platform_driver(kdwc3_driver);
  153. MODULE_ALIAS("platform:keystone-dwc3");
  154. MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
  155. MODULE_LICENSE("GPL v2");
  156. MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");