xref: /openbmc/linux/drivers/usb/dwc3/dwc3-keystone.c (revision c819e2cf)
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 	kdwc->usbss = devm_ioremap_resource(dev, res);
108 	if (IS_ERR(kdwc->usbss))
109 		return PTR_ERR(kdwc->usbss);
110 
111 	kdwc3_dma_mask = dma_get_mask(dev);
112 	dev->dma_mask = &kdwc3_dma_mask;
113 
114 	kdwc->clk = devm_clk_get(kdwc->dev, "usb");
115 
116 	error = clk_prepare_enable(kdwc->clk);
117 	if (error < 0) {
118 		dev_dbg(kdwc->dev, "unable to enable usb clock, err %d\n",
119 			error);
120 		return error;
121 	}
122 
123 	irq = platform_get_irq(pdev, 0);
124 	if (irq < 0) {
125 		dev_err(&pdev->dev, "missing irq\n");
126 		error = irq;
127 		goto err_irq;
128 	}
129 
130 	error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
131 			dev_name(dev), kdwc);
132 	if (error) {
133 		dev_err(dev, "failed to request IRQ #%d --> %d\n",
134 				irq, error);
135 		goto err_irq;
136 	}
137 
138 	kdwc3_enable_irqs(kdwc);
139 
140 	error = of_platform_populate(node, NULL, NULL, dev);
141 	if (error) {
142 		dev_err(&pdev->dev, "failed to create dwc3 core\n");
143 		goto err_core;
144 	}
145 
146 	return 0;
147 
148 err_core:
149 	kdwc3_disable_irqs(kdwc);
150 err_irq:
151 	clk_disable_unprepare(kdwc->clk);
152 
153 	return error;
154 }
155 
156 static int kdwc3_remove_core(struct device *dev, void *c)
157 {
158 	struct platform_device *pdev = to_platform_device(dev);
159 
160 	platform_device_unregister(pdev);
161 
162 	return 0;
163 }
164 
165 static int kdwc3_remove(struct platform_device *pdev)
166 {
167 	struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
168 
169 	kdwc3_disable_irqs(kdwc);
170 	device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
171 	clk_disable_unprepare(kdwc->clk);
172 	platform_set_drvdata(pdev, NULL);
173 
174 	return 0;
175 }
176 
177 static const struct of_device_id kdwc3_of_match[] = {
178 	{ .compatible = "ti,keystone-dwc3", },
179 	{},
180 };
181 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
182 
183 static struct platform_driver kdwc3_driver = {
184 	.probe		= kdwc3_probe,
185 	.remove		= kdwc3_remove,
186 	.driver		= {
187 		.name	= "keystone-dwc3",
188 		.of_match_table	= kdwc3_of_match,
189 	},
190 };
191 
192 module_platform_driver(kdwc3_driver);
193 
194 MODULE_ALIAS("platform:keystone-dwc3");
195 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
196 MODULE_LICENSE("GPL v2");
197 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");
198