15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
227088e00SAditya Srivastava /*
3943befc3SWingMan Kwok * dwc3-keystone.c - Keystone Specific Glue layer
4943befc3SWingMan Kwok *
510623b87SAlexander A. Klimov * Copyright (C) 2010-2013 Texas Instruments Incorporated - https://www.ti.com
6943befc3SWingMan Kwok *
7943befc3SWingMan Kwok * Author: WingMan Kwok <w-kwok2@ti.com>
8943befc3SWingMan Kwok */
9943befc3SWingMan Kwok
10943befc3SWingMan Kwok #include <linux/module.h>
11943befc3SWingMan Kwok #include <linux/kernel.h>
12943befc3SWingMan Kwok #include <linux/interrupt.h>
13943befc3SWingMan Kwok #include <linux/platform_device.h>
14943befc3SWingMan Kwok #include <linux/dma-mapping.h>
15943befc3SWingMan Kwok #include <linux/io.h>
16*484468fbSRob Herring #include <linux/of.h>
17943befc3SWingMan Kwok #include <linux/of_platform.h>
181c11e74eSRoger Quadros #include <linux/phy/phy.h>
19c14af973SFranklin S Cooper Jr #include <linux/pm_runtime.h>
20943befc3SWingMan Kwok
21943befc3SWingMan Kwok /* USBSS register offsets */
22943befc3SWingMan Kwok #define USBSS_REVISION 0x0000
23943befc3SWingMan Kwok #define USBSS_SYSCONFIG 0x0010
24943befc3SWingMan Kwok #define USBSS_IRQ_EOI 0x0018
25943befc3SWingMan Kwok #define USBSS_IRQSTATUS_RAW_0 0x0020
26943befc3SWingMan Kwok #define USBSS_IRQSTATUS_0 0x0024
27943befc3SWingMan Kwok #define USBSS_IRQENABLE_SET_0 0x0028
28943befc3SWingMan Kwok #define USBSS_IRQENABLE_CLR_0 0x002c
29943befc3SWingMan Kwok
30943befc3SWingMan Kwok /* IRQ register bits */
31943befc3SWingMan Kwok #define USBSS_IRQ_EOI_LINE(n) BIT(n)
32943befc3SWingMan Kwok #define USBSS_IRQ_EVENT_ST BIT(0)
33943befc3SWingMan Kwok #define USBSS_IRQ_COREIRQ_EN BIT(0)
34943befc3SWingMan Kwok #define USBSS_IRQ_COREIRQ_CLR BIT(0)
35943befc3SWingMan Kwok
36943befc3SWingMan Kwok struct dwc3_keystone {
37943befc3SWingMan Kwok struct device *dev;
38943befc3SWingMan Kwok void __iomem *usbss;
391c11e74eSRoger Quadros struct phy *usb3_phy;
40943befc3SWingMan Kwok };
41943befc3SWingMan Kwok
kdwc3_readl(void __iomem * base,u32 offset)42943befc3SWingMan Kwok static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
43943befc3SWingMan Kwok {
44943befc3SWingMan Kwok return readl(base + offset);
45943befc3SWingMan Kwok }
46943befc3SWingMan Kwok
kdwc3_writel(void __iomem * base,u32 offset,u32 value)47943befc3SWingMan Kwok static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
48943befc3SWingMan Kwok {
49943befc3SWingMan Kwok writel(value, base + offset);
50943befc3SWingMan Kwok }
51943befc3SWingMan Kwok
kdwc3_enable_irqs(struct dwc3_keystone * kdwc)52943befc3SWingMan Kwok static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
53943befc3SWingMan Kwok {
54943befc3SWingMan Kwok u32 val;
55943befc3SWingMan Kwok
56943befc3SWingMan Kwok val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
57943befc3SWingMan Kwok val |= USBSS_IRQ_COREIRQ_EN;
58943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
59943befc3SWingMan Kwok }
60943befc3SWingMan Kwok
kdwc3_disable_irqs(struct dwc3_keystone * kdwc)61943befc3SWingMan Kwok static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
62943befc3SWingMan Kwok {
63943befc3SWingMan Kwok u32 val;
64943befc3SWingMan Kwok
65943befc3SWingMan Kwok val = kdwc3_readl(kdwc->usbss, USBSS_IRQENABLE_SET_0);
66943befc3SWingMan Kwok val &= ~USBSS_IRQ_COREIRQ_EN;
67943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, val);
68943befc3SWingMan Kwok }
69943befc3SWingMan Kwok
dwc3_keystone_interrupt(int irq,void * _kdwc)70943befc3SWingMan Kwok static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
71943befc3SWingMan Kwok {
72943befc3SWingMan Kwok struct dwc3_keystone *kdwc = _kdwc;
73943befc3SWingMan Kwok
74943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_CLR_0, USBSS_IRQ_COREIRQ_CLR);
75943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQSTATUS_0, USBSS_IRQ_EVENT_ST);
76943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQENABLE_SET_0, USBSS_IRQ_COREIRQ_EN);
77943befc3SWingMan Kwok kdwc3_writel(kdwc->usbss, USBSS_IRQ_EOI, USBSS_IRQ_EOI_LINE(0));
78943befc3SWingMan Kwok
79943befc3SWingMan Kwok return IRQ_HANDLED;
80943befc3SWingMan Kwok }
81943befc3SWingMan Kwok
kdwc3_probe(struct platform_device * pdev)82943befc3SWingMan Kwok static int kdwc3_probe(struct platform_device *pdev)
83943befc3SWingMan Kwok {
84943befc3SWingMan Kwok struct device *dev = &pdev->dev;
85943befc3SWingMan Kwok struct device_node *node = pdev->dev.of_node;
86943befc3SWingMan Kwok struct dwc3_keystone *kdwc;
87943befc3SWingMan Kwok int error, irq;
88943befc3SWingMan Kwok
89943befc3SWingMan Kwok kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
90943befc3SWingMan Kwok if (!kdwc)
91943befc3SWingMan Kwok return -ENOMEM;
92943befc3SWingMan Kwok
93943befc3SWingMan Kwok platform_set_drvdata(pdev, kdwc);
94943befc3SWingMan Kwok
95943befc3SWingMan Kwok kdwc->dev = dev;
96943befc3SWingMan Kwok
975b76f6a0SYueHaibing kdwc->usbss = devm_platform_ioremap_resource(pdev, 0);
98943befc3SWingMan Kwok if (IS_ERR(kdwc->usbss))
99943befc3SWingMan Kwok return PTR_ERR(kdwc->usbss);
100943befc3SWingMan Kwok
1011c11e74eSRoger Quadros /* PSC dependency on AM65 needs SERDES0 to be powered before USB0 */
1021c11e74eSRoger Quadros kdwc->usb3_phy = devm_phy_optional_get(dev, "usb3-phy");
103370e3d5bSAndy Shevchenko if (IS_ERR(kdwc->usb3_phy))
104370e3d5bSAndy Shevchenko return dev_err_probe(dev, PTR_ERR(kdwc->usb3_phy), "couldn't get usb3 phy\n");
1051c11e74eSRoger Quadros
1061c11e74eSRoger Quadros phy_pm_runtime_get_sync(kdwc->usb3_phy);
1071c11e74eSRoger Quadros
1081c11e74eSRoger Quadros error = phy_reset(kdwc->usb3_phy);
1091c11e74eSRoger Quadros if (error < 0) {
1101c11e74eSRoger Quadros dev_err(dev, "usb3 phy reset failed: %d\n", error);
1111c11e74eSRoger Quadros return error;
1121c11e74eSRoger Quadros }
1131c11e74eSRoger Quadros
1141c11e74eSRoger Quadros error = phy_init(kdwc->usb3_phy);
1151c11e74eSRoger Quadros if (error < 0) {
1161c11e74eSRoger Quadros dev_err(dev, "usb3 phy init failed: %d\n", error);
1171c11e74eSRoger Quadros return error;
1181c11e74eSRoger Quadros }
1191c11e74eSRoger Quadros
1201c11e74eSRoger Quadros error = phy_power_on(kdwc->usb3_phy);
1211c11e74eSRoger Quadros if (error < 0) {
1221c11e74eSRoger Quadros dev_err(dev, "usb3 phy power on failed: %d\n", error);
1231c11e74eSRoger Quadros phy_exit(kdwc->usb3_phy);
1241c11e74eSRoger Quadros return error;
1251c11e74eSRoger Quadros }
1261c11e74eSRoger Quadros
1271c11e74eSRoger Quadros pm_runtime_enable(kdwc->dev);
128c14af973SFranklin S Cooper Jr error = pm_runtime_get_sync(kdwc->dev);
129943befc3SWingMan Kwok if (error < 0) {
130c14af973SFranklin S Cooper Jr dev_err(kdwc->dev, "pm_runtime_get_sync failed, error %d\n",
131943befc3SWingMan Kwok error);
132c14af973SFranklin S Cooper Jr goto err_irq;
133943befc3SWingMan Kwok }
134943befc3SWingMan Kwok
135eca6b494SRoger Quadros /* IRQ processing not required currently for AM65 */
136eca6b494SRoger Quadros if (of_device_is_compatible(node, "ti,am654-dwc3"))
137eca6b494SRoger Quadros goto skip_irq;
138eca6b494SRoger Quadros
139943befc3SWingMan Kwok irq = platform_get_irq(pdev, 0);
140943befc3SWingMan Kwok if (irq < 0) {
14162c60697SJulia Lawall error = irq;
142943befc3SWingMan Kwok goto err_irq;
143943befc3SWingMan Kwok }
144943befc3SWingMan Kwok
145943befc3SWingMan Kwok error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
146943befc3SWingMan Kwok dev_name(dev), kdwc);
147943befc3SWingMan Kwok if (error) {
148943befc3SWingMan Kwok dev_err(dev, "failed to request IRQ #%d --> %d\n",
149943befc3SWingMan Kwok irq, error);
150943befc3SWingMan Kwok goto err_irq;
151943befc3SWingMan Kwok }
152943befc3SWingMan Kwok
153943befc3SWingMan Kwok kdwc3_enable_irqs(kdwc);
154943befc3SWingMan Kwok
155eca6b494SRoger Quadros skip_irq:
156943befc3SWingMan Kwok error = of_platform_populate(node, NULL, NULL, dev);
157943befc3SWingMan Kwok if (error) {
158943befc3SWingMan Kwok dev_err(&pdev->dev, "failed to create dwc3 core\n");
159943befc3SWingMan Kwok goto err_core;
160943befc3SWingMan Kwok }
161943befc3SWingMan Kwok
162943befc3SWingMan Kwok return 0;
163943befc3SWingMan Kwok
164943befc3SWingMan Kwok err_core:
165943befc3SWingMan Kwok kdwc3_disable_irqs(kdwc);
166943befc3SWingMan Kwok err_irq:
167c14af973SFranklin S Cooper Jr pm_runtime_put_sync(kdwc->dev);
168c14af973SFranklin S Cooper Jr pm_runtime_disable(kdwc->dev);
1691c11e74eSRoger Quadros phy_power_off(kdwc->usb3_phy);
1701c11e74eSRoger Quadros phy_exit(kdwc->usb3_phy);
1711c11e74eSRoger Quadros phy_pm_runtime_put_sync(kdwc->usb3_phy);
172943befc3SWingMan Kwok
173943befc3SWingMan Kwok return error;
174943befc3SWingMan Kwok }
175943befc3SWingMan Kwok
kdwc3_remove_core(struct device * dev,void * c)176943befc3SWingMan Kwok static int kdwc3_remove_core(struct device *dev, void *c)
177943befc3SWingMan Kwok {
178943befc3SWingMan Kwok struct platform_device *pdev = to_platform_device(dev);
179943befc3SWingMan Kwok
180943befc3SWingMan Kwok platform_device_unregister(pdev);
181943befc3SWingMan Kwok
182943befc3SWingMan Kwok return 0;
183943befc3SWingMan Kwok }
184943befc3SWingMan Kwok
kdwc3_remove(struct platform_device * pdev)185039e3dedSUwe Kleine-König static void kdwc3_remove(struct platform_device *pdev)
186943befc3SWingMan Kwok {
187943befc3SWingMan Kwok struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
188eca6b494SRoger Quadros struct device_node *node = pdev->dev.of_node;
189943befc3SWingMan Kwok
190eca6b494SRoger Quadros if (!of_device_is_compatible(node, "ti,am654-dwc3"))
191943befc3SWingMan Kwok kdwc3_disable_irqs(kdwc);
192eca6b494SRoger Quadros
193943befc3SWingMan Kwok device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
194c14af973SFranklin S Cooper Jr pm_runtime_put_sync(kdwc->dev);
195c14af973SFranklin S Cooper Jr pm_runtime_disable(kdwc->dev);
196c14af973SFranklin S Cooper Jr
1971c11e74eSRoger Quadros phy_power_off(kdwc->usb3_phy);
1981c11e74eSRoger Quadros phy_exit(kdwc->usb3_phy);
1991c11e74eSRoger Quadros phy_pm_runtime_put_sync(kdwc->usb3_phy);
200943befc3SWingMan Kwok }
201943befc3SWingMan Kwok
202943befc3SWingMan Kwok static const struct of_device_id kdwc3_of_match[] = {
203943befc3SWingMan Kwok { .compatible = "ti,keystone-dwc3", },
204eca6b494SRoger Quadros { .compatible = "ti,am654-dwc3" },
205943befc3SWingMan Kwok {},
206943befc3SWingMan Kwok };
207943befc3SWingMan Kwok MODULE_DEVICE_TABLE(of, kdwc3_of_match);
208943befc3SWingMan Kwok
209943befc3SWingMan Kwok static struct platform_driver kdwc3_driver = {
210943befc3SWingMan Kwok .probe = kdwc3_probe,
211039e3dedSUwe Kleine-König .remove_new = kdwc3_remove,
212943befc3SWingMan Kwok .driver = {
213943befc3SWingMan Kwok .name = "keystone-dwc3",
214943befc3SWingMan Kwok .of_match_table = kdwc3_of_match,
215943befc3SWingMan Kwok },
216943befc3SWingMan Kwok };
217943befc3SWingMan Kwok
218943befc3SWingMan Kwok module_platform_driver(kdwc3_driver);
219943befc3SWingMan Kwok
220943befc3SWingMan Kwok MODULE_ALIAS("platform:keystone-dwc3");
221943befc3SWingMan Kwok MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
222943befc3SWingMan Kwok MODULE_LICENSE("GPL v2");
223943befc3SWingMan Kwok MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");
224