lkdtm: Add Control Flow Integrity test
[linux/fpc-iii.git] / drivers / usb / dwc3 / dwc3-keystone.c
blobcbee5fb9b9fb02d2e40e795d6e4276f0cdeaba6d
1 // SPDX-License-Identifier: GPL-2.0
2 /**
3 * dwc3-keystone.c - Keystone Specific Glue layer
5 * Copyright (C) 2010-2013 Texas Instruments Incorporated - http://www.ti.com
7 * Author: WingMan Kwok <w-kwok2@ti.com>
8 */
10 #include <linux/module.h>
11 #include <linux/kernel.h>
12 #include <linux/interrupt.h>
13 #include <linux/platform_device.h>
14 #include <linux/dma-mapping.h>
15 #include <linux/io.h>
16 #include <linux/of_platform.h>
17 #include <linux/pm_runtime.h>
19 /* USBSS register offsets */
20 #define USBSS_REVISION 0x0000
21 #define USBSS_SYSCONFIG 0x0010
22 #define USBSS_IRQ_EOI 0x0018
23 #define USBSS_IRQSTATUS_RAW_0 0x0020
24 #define USBSS_IRQSTATUS_0 0x0024
25 #define USBSS_IRQENABLE_SET_0 0x0028
26 #define USBSS_IRQENABLE_CLR_0 0x002c
28 /* IRQ register bits */
29 #define USBSS_IRQ_EOI_LINE(n) BIT(n)
30 #define USBSS_IRQ_EVENT_ST BIT(0)
31 #define USBSS_IRQ_COREIRQ_EN BIT(0)
32 #define USBSS_IRQ_COREIRQ_CLR BIT(0)
34 struct dwc3_keystone {
35 struct device *dev;
36 void __iomem *usbss;
39 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
41 return readl(base + offset);
44 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
46 writel(value, base + offset);
49 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
51 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);
58 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
60 u32 val;
62 val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
63 val &= ~USBSS_IRQ_COREIRQ_EN;
64 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
67 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
69 struct dwc3_keystone *kdwc = _kdwc;
71 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
72 kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
73 kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
74 kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
76 return IRQ_HANDLED;
79 static int kdwc3_probe(struct platform_device *pdev)
81 struct device *dev = &pdev->dev;
82 struct device_node *node = pdev->dev.of_node;
83 struct dwc3_keystone *kdwc;
84 struct resource *res;
85 int error, irq;
87 kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
88 if (!kdwc)
89 return -ENOMEM;
91 platform_set_drvdata(pdev, kdwc);
93 kdwc->dev = dev;
95 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
96 kdwc->usbss = devm_ioremap_resource(dev, res);
97 if (IS_ERR(kdwc->usbss))
98 return PTR_ERR(kdwc->usbss);
100 pm_runtime_enable(kdwc->dev);
102 error = pm_runtime_get_sync(kdwc->dev);
103 if (error < 0) {
104 dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
105 error);
106 goto err_irq;
109 /* IRQ processing not required currently for AM65 */
110 if (of_device_is_compatible(node, "ti,am654-dwc3"))
111 goto skip_irq;
113 irq = platform_get_irq(pdev, 0);
114 if (irq < 0) {
115 dev_err(&pdev->dev, "missing irq\n");
116 error = irq;
117 goto err_irq;
120 error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
121 dev_name(dev), kdwc);
122 if (error) {
123 dev_err(dev, "failed to request IRQ #%d --> %d\n",
124 irq, error);
125 goto err_irq;
128 kdwc3_enable_irqs(kdwc);
130 skip_irq:
131 error = of_platform_populate(node, NULL, NULL, dev);
132 if (error) {
133 dev_err(&pdev->dev, "failed to create dwc3 core\n");
134 goto err_core;
137 return 0;
139 err_core:
140 kdwc3_disable_irqs(kdwc);
141 err_irq:
142 pm_runtime_put_sync(kdwc->dev);
143 pm_runtime_disable(kdwc->dev);
145 return error;
148 static int kdwc3_remove_core(struct device *dev, void *c)
150 struct platform_device *pdev = to_platform_device(dev);
152 platform_device_unregister(pdev);
154 return 0;
157 static int kdwc3_remove(struct platform_device *pdev)
159 struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
160 struct device_node *node = pdev->dev.of_node;
162 if (!of_device_is_compatible(node, "ti,am654-dwc3"))
163 kdwc3_disable_irqs(kdwc);
165 device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
166 pm_runtime_put_sync(kdwc->dev);
167 pm_runtime_disable(kdwc->dev);
169 platform_set_drvdata(pdev, NULL);
171 return 0;
174 static const struct of_device_id kdwc3_of_match[] = {
175 { .compatible = "ti,keystone-dwc3", },
176 { .compatible = "ti,am654-dwc3" },
179 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
181 static struct platform_driver kdwc3_driver = {
182 .probe = kdwc3_probe,
183 .remove = kdwc3_remove,
184 .driver = {
185 .name = "keystone-dwc3",
186 .of_match_table = kdwc3_of_match,
190 module_platform_driver(kdwc3_driver);
192 MODULE_ALIAS("platform:keystone-dwc3");
193 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
194 MODULE_LICENSE("GPL v2");
195 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");