xref: /openbmc/linux/drivers/usb/dwc3/dwc3-keystone.c (revision 981ab3f1)
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/clk.h>
19 #include <linux/module.h>
20 #include <linux/kernel.h>
21 #include <linux/interrupt.h>
22 #include <linux/platform_device.h>
23 #include <linux/dma-mapping.h>
24 #include <linux/io.h>
25 #include <linux/of_platform.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 	struct clk			*clk;
45 	void __iomem			*usbss;
46 };
47 
48 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
49 {
50 	return readl(base + offset);
51 }
52 
53 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
54 {
55 	writel(value, base + offset);
56 }
57 
58 static void kdwc3_enable_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 void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
68 {
69 	u32 val;
70 
71 	val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
72 	val &= ~USBSS_IRQ_COREIRQ_EN;
73 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
74 }
75 
76 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
77 {
78 	struct dwc3_keystone	*kdwc = _kdwc;
79 
80 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
81 	kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
82 	kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
83 	kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
84 
85 	return IRQ_HANDLED;
86 }
87 
88 static int kdwc3_probe(struct platform_device *pdev)
89 {
90 	struct device		*dev = &pdev->dev;
91 	struct device_node	*node = pdev->dev.of_node;
92 	struct dwc3_keystone	*kdwc;
93 	struct resource		*res;
94 	int			error, irq;
95 
96 	kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
97 	if (!kdwc)
98 		return -ENOMEM;
99 
100 	platform_set_drvdata(pdev, kdwc);
101 
102 	kdwc->dev = dev;
103 
104 	res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
105 	kdwc->usbss = devm_ioremap_resource(dev, res);
106 	if (IS_ERR(kdwc->usbss))
107 		return PTR_ERR(kdwc->usbss);
108 
109 	kdwc->clk = devm_clk_get(kdwc->dev, "usb");
110 	if (IS_ERR(kdwc->clk)) {
111 		dev_err(kdwc->dev, "unable to get usb clock\n");
112 		return PTR_ERR(kdwc->clk);
113 	}
114 
115 	error = clk_prepare_enable(kdwc->clk);
116 	if (error < 0) {
117 		dev_err(kdwc->dev, "unable to enable usb clock, error %d\n",
118 			error);
119 		return error;
120 	}
121 
122 	irq = platform_get_irq(pdev, 0);
123 	if (irq < 0) {
124 		dev_err(&pdev->dev, "missing irq\n");
125 		error = irq;
126 		goto err_irq;
127 	}
128 
129 	error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
130 			dev_name(dev), kdwc);
131 	if (error) {
132 		dev_err(dev, "failed to request IRQ #%d --> %d\n",
133 				irq, error);
134 		goto err_irq;
135 	}
136 
137 	kdwc3_enable_irqs(kdwc);
138 
139 	error = of_platform_populate(node, NULL, NULL, dev);
140 	if (error) {
141 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
142 		goto err_core;
143 	}
144 
145 	return 0;
146 
147 err_core:
148 	kdwc3_disable_irqs(kdwc);
149 err_irq:
150 	clk_disable_unprepare(kdwc->clk);
151 
152 	return error;
153 }
154 
155 static int kdwc3_remove_core(struct device *dev, void *c)
156 {
157 	struct platform_device *pdev = to_platform_device(dev);
158 
159 	platform_device_unregister(pdev);
160 
161 	return 0;
162 }
163 
164 static int kdwc3_remove(struct platform_device *pdev)
165 {
166 	struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
167 
168 	kdwc3_disable_irqs(kdwc);
169 	device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
170 	clk_disable_unprepare(kdwc->clk);
171 	platform_set_drvdata(pdev, NULL);
172 
173 	return 0;
174 }
175 
176 static const struct of_device_id kdwc3_of_match[] = {
177 	{ .compatible = "ti,keystone-dwc3", },
178 	{},
179 };
180 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
181 
182 static struct platform_driver kdwc3_driver = {
183 	.probe		= kdwc3_probe,
184 	.remove		= kdwc3_remove,
185 	.driver		= {
186 		.name	= "keystone-dwc3",
187 		.of_match_table	= kdwc3_of_match,
188 	},
189 };
190 
191 module_platform_driver(kdwc3_driver);
192 
193 MODULE_ALIAS("platform:keystone-dwc3");
194 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
195 MODULE_LICENSE("GPL v2");
196 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");
197