xref: /linux/drivers/usb/dwc3/dwc3-keystone.c (revision e5c86679d5e864947a52fb31e45a425dea3e7fa9)
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 
111 	error = clk_prepare_enable(kdwc->clk);
112 	if (error < 0) {
113 		dev_err(kdwc->dev, "unable to enable usb clock, error %d\n",
114 			error);
115 		return error;
116 	}
117 
118 	irq = platform_get_irq(pdev, 0);
119 	if (irq < 0) {
120 		dev_err(&pdev->dev, "missing irq\n");
121 		error = irq;
122 		goto err_irq;
123 	}
124 
125 	error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
126 			dev_name(dev), kdwc);
127 	if (error) {
128 		dev_err(dev, "failed to request IRQ #%d --> %d\n",
129 				irq, error);
130 		goto err_irq;
131 	}
132 
133 	kdwc3_enable_irqs(kdwc);
134 
135 	error = of_platform_populate(node, NULL, NULL, dev);
136 	if (error) {
137 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
138 		goto err_core;
139 	}
140 
141 	return 0;
142 
143 err_core:
144 	kdwc3_disable_irqs(kdwc);
145 err_irq:
146 	clk_disable_unprepare(kdwc->clk);
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 	clk_disable_unprepare(kdwc->clk);
167 	platform_set_drvdata(pdev, NULL);
168 
169 	return 0;
170 }
171 
172 static const struct of_device_id kdwc3_of_match[] = {
173 	{ .compatible = "ti,keystone-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