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