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