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>
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
{
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
)
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
)
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));
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
;
98 kdwc
= devm_kzalloc(dev
, sizeof(*kdwc
), GFP_KERNEL
);
102 platform_set_drvdata(pdev
, kdwc
);
106 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
108 dev_err(dev
, "missing usbss resource\n");
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
);
123 dev_dbg(kdwc
->dev
, "unable to enable usb clock, err %d\n",
128 irq
= platform_get_irq(pdev
, 0);
130 dev_err(&pdev
->dev
, "missing irq\n");
134 error
= devm_request_irq(dev
, irq
, dwc3_keystone_interrupt
, IRQF_SHARED
,
135 dev_name(dev
), kdwc
);
137 dev_err(dev
, "failed to request IRQ #%d --> %d\n",
142 kdwc3_enable_irqs(kdwc
);
144 error
= of_platform_populate(node
, NULL
, NULL
, dev
);
146 dev_err(&pdev
->dev
, "failed to create dwc3 core\n");
153 kdwc3_disable_irqs(kdwc
);
155 clk_disable_unprepare(kdwc
->clk
);
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
);
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
);
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
,
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");