Merge tag 'ceph-for-4.13-rc8' of git://github.com/ceph/ceph-client
[linux/fpc-iii.git] / drivers / usb / dwc3 / dwc3-keystone.c
blob12ee23f53cdde17ea924344ff7e84d88cf9881cb
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 struct dwc3_keystone {
43 struct device *dev;
44 struct clk *clk;
45 void __iomem *usbss;
48 static inline u32 kdwc3_readl(void __iomem *base, u32 offset)
50 return readl(base + offset);
53 static inline void kdwc3_writel(void __iomem *base, u32 offset, u32 value)
55 writel(value, base + offset);
58 static void kdwc3_enable_irqs(struct dwc3_keystone *kdwc)
60 u32 val;
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);
67 static void kdwc3_disable_irqs(struct dwc3_keystone *kdwc)
69 u32 val;
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);
76 static irqreturn_t dwc3_keystone_interrupt(int irq, void *_kdwc)
78 struct dwc3_keystone *kdwc = _kdwc;
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));
85 return IRQ_HANDLED;
88 static int kdwc3_probe(struct platform_device *pdev)
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;
96 kdwc = devm_kzalloc(dev, sizeof(*kdwc), GFP_KERNEL);
97 if (!kdwc)
98 return -ENOMEM;
100 platform_set_drvdata(pdev, kdwc);
102 kdwc->dev = dev;
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);
109 kdwc->clk = devm_clk_get(kdwc->dev, "usb");
110 if (IS_ERR(kdwc->clk)) {
111 dev_err(kdwc->dev, "unable to get usb clock\n");
112 return PTR_ERR(kdwc->clk);
115 error = clk_prepare_enable(kdwc->clk);
116 if (error < 0) {
117 dev_err(kdwc->dev, "unable to enable usb clock, error %d\n",
118 error);
119 return error;
122 irq = platform_get_irq(pdev, 0);
123 if (irq < 0) {
124 dev_err(&pdev->dev, "missing irq\n");
125 error = irq;
126 goto err_irq;
129 error = devm_request_irq(dev, irq, dwc3_keystone_interrupt, IRQF_SHARED,
130 dev_name(dev), kdwc);
131 if (error) {
132 dev_err(dev, "failed to request IRQ #%d --> %d\n",
133 irq, error);
134 goto err_irq;
137 kdwc3_enable_irqs(kdwc);
139 error = of_platform_populate(node, NULL, NULL, dev);
140 if (error) {
141 dev_err(&pdev->dev, "failed to create dwc3 core\n");
142 goto err_core;
145 return 0;
147 err_core:
148 kdwc3_disable_irqs(kdwc);
149 err_irq:
150 clk_disable_unprepare(kdwc->clk);
152 return error;
155 static int kdwc3_remove_core(struct device *dev, void *c)
157 struct platform_device *pdev = to_platform_device(dev);
159 platform_device_unregister(pdev);
161 return 0;
164 static int kdwc3_remove(struct platform_device *pdev)
166 struct dwc3_keystone *kdwc = platform_get_drvdata(pdev);
168 kdwc3_disable_irqs(kdwc);
169 device_for_each_child(&pdev->dev, NULL, kdwc3_remove_core);
170 clk_disable_unprepare(kdwc->clk);
171 platform_set_drvdata(pdev, NULL);
173 return 0;
176 static const struct of_device_id kdwc3_of_match[] = {
177 { .compatible = "ti,keystone-dwc3", },
180 MODULE_DEVICE_TABLE(of, kdwc3_of_match);
182 static struct platform_driver kdwc3_driver = {
183 .probe = kdwc3_probe,
184 .remove = kdwc3_remove,
185 .driver = {
186 .name = "keystone-dwc3",
187 .of_match_table = kdwc3_of_match,
191 module_platform_driver(kdwc3_driver);
193 MODULE_ALIAS("platform:keystone-dwc3");
194 MODULE_AUTHOR("WingMan Kwok <w-kwok2@ti.com>");
195 MODULE_LICENSE("GPL v2");
196 MODULE_DESCRIPTION("DesignWare USB3 KEYSTONE Glue Layer");