PM / sleep: Asynchronous threads for suspend_noirq
[linux/fpc-iii.git] / drivers / usb / dwc3 / dwc3-keystone.c
blob1fad1618df6e8b742785635e8582fd9663d65d54
1 /**
2 * dwc3-keystone.c - Keystone Specific Glue layer
4 * Copyright (C) 2010-2013 Texas Instruments Incorporated - http://www.ti.com
6 * Author: WingMan Kwok <w-kwok2@ti.com>
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.
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.
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>
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
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)
42 static u64 kdwc3_dma_mask;
44 struct dwc3_keystone {
45 struct device *dev;
46 struct clk *clk;
47 void __iomem *usbss;
50 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
52 return readl(base + offset);
55 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
57 writel(value, base + offset);
60 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
62 u32 val;
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);
69 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
71 u32 val;
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);
78 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
80 struct dwc3_keystone *kdwc = _kdwc;
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));
87 return IRQ_HANDLED;
90 static int kdwc3_probe(struct platform_device *pdev)
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;
98 kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
99 if (!kdwc)
100 return -ENOMEM;
102 platform_set_drvdata(pdev, kdwc);
104 kdwc->dev = dev;
106 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
107 if (!res) {
108 dev_err(dev, "missing usbss resource\n");
109 return -EINVAL;
112 kdwc->usbss = devm_ioremap_resource(dev, res);
113 if (IS_ERR(kdwc->usbss))
114 return PTR_ERR(kdwc->usbss);
116 kdwc3_dma_mask = dma_get_mask(dev);
117 dev->dma_mask = &kdwc3_dma_mask;
119 kdwc->clk = devm_clk_get(kdwc->dev, "usb");
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;
128 irq = platform_get_irq(pdev, 0);
129 if (irq < 0) {
130 dev_err(&pdev->dev, "missing irq\n");
131 goto err_irq;
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;
142 kdwc3_enable_irqs(kdwc);
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;
150 return 0;
152 err_core:
153 kdwc3_disable_irqs(kdwc);
154 err_irq:
155 clk_disable_unprepare(kdwc->clk);
157 return error;
160 static int kdwc3_remove_core(struct device *dev, void *c)
162 struct platform_device *pdev = to_platform_device(dev);
164 platform_device_unregister(pdev);
166 return 0;
169 static int kdwc3_remove(struct platform_device *pdev)
171 struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
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);
178 return 0;
181 static const struct of_device_id kdwc3_of_match[] = {
182 { .compatible = "ti,keystone-dwc3", },
185 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
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,
197 module_platform_driver(kdwc3_driver);
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");