xref: /openbmc/linux/drivers/usb/dwc3/dwc3-keystone.c (revision 20e2fc42)
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 
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>
18 
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
27 
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)
33 
34 struct dwc3_keystone {
35 	struct device			*dev;
36 	void __iomem			*usbss;
37 };
38 
39 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
40 {
41 	return readl(base + offset);
42 }
43 
44 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
45 {
46 	writel(value, base + offset);
47 }
48 
49 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
50 {
51 	u32 val;
52 
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 
58 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
59 {
60 	u32 val;
61 
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);
65 }
66 
67 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
68 {
69 	struct dwc3_keystone	*kdwc = _kdwc;
70 
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));
75 
76 	return IRQ_HANDLED;
77 }
78 
79 static int kdwc3_probe(struct platform_device *pdev)
80 {
81 	struct device		*dev = &pdev->dev;
82 	struct device_node	*node = pdev->dev.of_node;
83 	struct dwc3_keystone	*kdwc;
84 	int			error, irq;
85 
86 	kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
87 	if (!kdwc)
88 		return -ENOMEM;
89 
90 	platform_set_drvdata(pdev, kdwc);
91 
92 	kdwc->dev = dev;
93 
94 	kdwc->usbss = devm_platform_ioremap_resource(pdev, 0);
95 	if (IS_ERR(kdwc->usbss))
96 		return PTR_ERR(kdwc->usbss);
97 
98 	pm_runtime_enable(kdwc->dev);
99 
100 	error = pm_runtime_get_sync(kdwc->dev);
101 	if (error < 0) {
102 		dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
103 			error);
104 		goto err_irq;
105 	}
106 
107 	/* IRQ processing not required currently for AM65 */
108 	if (of_device_is_compatible(node, "ti,am654-dwc3"))
109 		goto skip_irq;
110 
111 	irq = platform_get_irq(pdev, 0);
112 	if (irq < 0) {
113 		error = irq;
114 		goto err_irq;
115 	}
116 
117 	error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
118 			dev_name(dev), kdwc);
119 	if (error) {
120 		dev_err(dev, "failed to request IRQ #%d --> %d\n",
121 				irq, error);
122 		goto err_irq;
123 	}
124 
125 	kdwc3_enable_irqs(kdwc);
126 
127 skip_irq:
128 	error = of_platform_populate(node, NULL, NULL, dev);
129 	if (error) {
130 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
131 		goto err_core;
132 	}
133 
134 	return 0;
135 
136 err_core:
137 	kdwc3_disable_irqs(kdwc);
138 err_irq:
139 	pm_runtime_put_sync(kdwc->dev);
140 	pm_runtime_disable(kdwc->dev);
141 
142 	return error;
143 }
144 
145 static int kdwc3_remove_core(struct device *dev, void *c)
146 {
147 	struct platform_device *pdev = to_platform_device(dev);
148 
149 	platform_device_unregister(pdev);
150 
151 	return 0;
152 }
153 
154 static int kdwc3_remove(struct platform_device *pdev)
155 {
156 	struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
157 	struct device_node *node = pdev->dev.of_node;
158 
159 	if (!of_device_is_compatible(node, "ti,am654-dwc3"))
160 		kdwc3_disable_irqs(kdwc);
161 
162 	device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
163 	pm_runtime_put_sync(kdwc->dev);
164 	pm_runtime_disable(kdwc->dev);
165 
166 	platform_set_drvdata(pdev, NULL);
167 
168 	return 0;
169 }
170 
171 static const struct of_device_id kdwc3_of_match[] = {
172 	{ .compatible = "ti,keystone-dwc3", },
173 	{ .compatible = "ti,am654-dwc3" },
174 	{},
175 };
176 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
177 
178 static struct platform_driver kdwc3_driver = {
179 	.probe		= kdwc3_probe,
180 	.remove		= kdwc3_remove,
181 	.driver		= {
182 		.name	= "keystone-dwc3",
183 		.of_match_table	= kdwc3_of_match,
184 	},
185 };
186 
187 module_platform_driver(kdwc3_driver);
188 
189 MODULE_ALIAS("platform:keystone-dwc3");
190 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
191 MODULE_LICENSE("GPL v2");
192 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");
193