dwc3-keystone.c 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // SPDX-License-Identifier: GPL-2.0
  2. /**
  3. * dwc3-keystone.c - Keystone Specific Glue layer
  4. *
  5. * Copyright (C) 2010-2013 Texas Instruments Incorporated - http://www.ti.com
  6. *
  7. * Author: WingMan Kwok <w-kwok2@ti.com>
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2 of
  11. * the License as published by the Free Software Foundation.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. */
  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. #include <linux/pm_runtime.h>
  26. /* USBSS register offsets */
  27. #define USBSS_REVISION 0x0000
  28. #define USBSS_SYSCONFIG 0x0010
  29. #define USBSS_IRQ_EOI 0x0018
  30. #define USBSS_IRQSTATUS_RAW_0 0x0020
  31. #define USBSS_IRQSTATUS_0 0x0024
  32. #define USBSS_IRQENABLE_SET_0 0x0028
  33. #define USBSS_IRQENABLE_CLR_0 0x002c
  34. /* IRQ register bits */
  35. #define USBSS_IRQ_EOI_LINE(n) BIT(n)
  36. #define USBSS_IRQ_EVENT_ST BIT(0)
  37. #define USBSS_IRQ_COREIRQ_EN BIT(0)
  38. #define USBSS_IRQ_COREIRQ_CLR BIT(0)
  39. struct dwc3_keystone {
  40. struct device *dev;
  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. pm_runtime_enable(kdwc->dev);
  91. error = pm_runtime_get_sync(kdwc->dev);
  92. if (error < 0) {
  93. dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
  94. error);
  95. goto err_irq;
  96. }
  97. irq = platform_get_irq(pdev, 0);
  98. if (irq < 0) {
  99. dev_err(&pdev->dev, "missing irq\n");
  100. error = irq;
  101. goto err_irq;
  102. }
  103. error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
  104. dev_name(dev), kdwc);
  105. if (error) {
  106. dev_err(dev, "failed to request IRQ #%d --> %d\n",
  107. irq, error);
  108. goto err_irq;
  109. }
  110. kdwc3_enable_irqs(kdwc);
  111. error = of_platform_populate(node, NULL, NULL, dev);
  112. if (error) {
  113. dev_err(&pdev->dev, "failed to create dwc3 core\n");
  114. goto err_core;
  115. }
  116. return 0;
  117. err_core:
  118. kdwc3_disable_irqs(kdwc);
  119. err_irq:
  120. pm_runtime_put_sync(kdwc->dev);
  121. pm_runtime_disable(kdwc->dev);
  122. return error;
  123. }
  124. static int kdwc3_remove_core(struct device *dev, void *c)
  125. {
  126. struct platform_device *pdev = to_platform_device(dev);
  127. platform_device_unregister(pdev);
  128. return 0;
  129. }
  130. static int kdwc3_remove(struct platform_device *pdev)
  131. {
  132. struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
  133. kdwc3_disable_irqs(kdwc);
  134. device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
  135. pm_runtime_put_sync(kdwc->dev);
  136. pm_runtime_disable(kdwc->dev);
  137. platform_set_drvdata(pdev, NULL);
  138. return 0;
  139. }
  140. static const struct of_device_id kdwc3_of_match[] = {
  141. { .compatible = "ti,keystone-dwc3", },
  142. {},
  143. };
  144. MODULE_DEVICE_TABLE(of, kdwc3_of_match);
  145. static struct platform_driver kdwc3_driver = {
  146. .probe = kdwc3_probe,
  147. .remove = kdwc3_remove,
  148. .driver = {
  149. .name = "keystone-dwc3",
  150. .of_match_table = kdwc3_of_match,
  151. },
  152. };
  153. module_platform_driver(kdwc3_driver);
  154. MODULE_ALIAS("platform:keystone-dwc3");
  155. MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
  156. MODULE_LICENSE("GPL v2");
  157. MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");