2 * platform.c - DesignWare HS OTG Controller platform driver
4 * Copyright (C) Matthijs Kooijman <matthijs@stdin.nl>
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions, and the following disclaimer,
11 * without modification.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The names of the above-listed copyright holders may not be used
16 * to endorse or promote products derived from this software without
17 * specific prior written permission.
19 * ALTERNATIVELY, this software may be distributed under the terms of the
20 * GNU General Public License ("GPL") as published by the Free Software
21 * Foundation; either version 2 of the License, or (at your option) any
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS
25 * IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
26 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
27 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
28 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
29 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
30 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
32 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
33 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
34 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 #include <linux/kernel.h>
38 #include <linux/module.h>
39 #include <linux/slab.h>
40 #include <linux/device.h>
41 #include <linux/dma-mapping.h>
42 #include <linux/of_device.h>
43 #include <linux/mutex.h>
44 #include <linux/platform_device.h>
46 #include <linux/usb/of.h>
51 static const char dwc2_driver_name[] = "dwc2";
53 static const struct dwc2_core_params params_bcm2835 = {
54 .otg_cap = 0, /* HNP/SRP capable */
55 .otg_ver = 0, /* 1.3 */
58 .speed = 0, /* High Speed */
59 .enable_dynamic_fifo = 1,
60 .en_multiple_tx_fifo = 1,
61 .host_rx_fifo_size = 774, /* 774 DWORDs */
62 .host_nperio_tx_fifo_size = 256, /* 256 DWORDs */
63 .host_perio_tx_fifo_size = 512, /* 512 DWORDs */
64 .max_transfer_size = 65535,
65 .max_packet_count = 511,
67 .phy_type = 1, /* UTMI */
68 .phy_utmi_width = 8, /* 8 bits */
69 .phy_ulpi_ddr = 0, /* Single */
70 .phy_ulpi_ext_vbus = 0,
73 .host_support_fs_ls_low_power = 0,
74 .host_ls_low_power_phy_clk = 0, /* 48 MHz */
81 static const struct dwc2_core_params params_rk3066 = {
82 .otg_cap = 2, /* non-HNP/non-SRP */
87 .enable_dynamic_fifo = 1,
88 .en_multiple_tx_fifo = -1,
89 .host_rx_fifo_size = 520, /* 520 DWORDs */
90 .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
91 .host_perio_tx_fifo_size = 256, /* 256 DWORDs */
92 .max_transfer_size = 65535,
93 .max_packet_count = -1,
98 .phy_ulpi_ext_vbus = -1,
101 .host_support_fs_ls_low_power = -1,
102 .host_ls_low_power_phy_clk = -1,
105 .ahbcfg = 0x7, /* INCR16 */
110 * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
113 * @dev: Platform device
115 * This routine is called, for example, when the rmmod command is executed. The
116 * device may or may not be electrically present. If it is present, the driver
117 * stops device processing. Any resources used on behalf of this device are
120 static int dwc2_driver_remove(struct platform_device *dev)
122 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
124 dwc2_hcd_remove(hsotg);
125 s3c_hsotg_remove(hsotg);
130 static const struct of_device_id dwc2_of_match_table[] = {
131 { .compatible = "brcm,bcm2835-usb", .data = ¶ms_bcm2835 },
132 { .compatible = "rockchip,rk3066-usb", .data = ¶ms_rk3066 },
133 { .compatible = "snps,dwc2", .data = NULL },
134 { .compatible = "samsung,s3c6400-hsotg", .data = NULL},
137 MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
140 * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg
143 * @dev: Platform device
145 * This routine creates the driver components required to control the device
146 * (core, HCD, and PCD) and initializes the device. The driver components are
147 * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
148 * in the device private data. This allows the driver to access the dwc2_hsotg
149 * structure on subsequent calls to driver methods for this device.
151 static int dwc2_driver_probe(struct platform_device *dev)
153 const struct of_device_id *match;
154 const struct dwc2_core_params *params;
155 struct dwc2_core_params defparams;
156 struct dwc2_hsotg *hsotg;
157 struct resource *res;
159 struct usb_phy *uphy;
163 match = of_match_device(dwc2_of_match_table, &dev->dev);
164 if (match && match->data) {
165 params = match->data;
167 /* Default all params to autodetect */
168 dwc2_set_all_params(&defparams, -1);
172 * Disable descriptor dma mode by default as the HW can support
173 * it, but does not support it for SPLIT transactions.
175 defparams.dma_desc_enable = 0;
178 hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL);
182 hsotg->dev = &dev->dev;
185 * Use reasonable defaults so platforms don't have to provide these.
187 if (!dev->dev.dma_mask)
188 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
189 retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
193 irq = platform_get_irq(dev, 0);
195 dev_err(&dev->dev, "missing IRQ resource\n");
199 dev_dbg(hsotg->dev, "registering common handler for irq%d\n",
201 retval = devm_request_irq(hsotg->dev, irq,
202 dwc2_handle_common_intr, IRQF_SHARED,
203 dev_name(hsotg->dev), hsotg);
207 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
208 hsotg->regs = devm_ioremap_resource(&dev->dev, res);
209 if (IS_ERR(hsotg->regs))
210 return PTR_ERR(hsotg->regs);
212 dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n",
213 (unsigned long)res->start, hsotg->regs);
215 hsotg->dr_mode = of_usb_get_dr_mode(dev->dev.of_node);
218 * Attempt to find a generic PHY, then look for an old style
221 phy = devm_phy_get(&dev->dev, "usb2-phy");
224 uphy = devm_usb_get_phy(&dev->dev, USB_PHY_TYPE_USB2);
231 phy_power_on(hsotg->phy);
232 phy_init(hsotg->phy);
235 spin_lock_init(&hsotg->lock);
236 mutex_init(&hsotg->init_mutex);
237 retval = dwc2_gadget_init(hsotg, irq);
240 retval = dwc2_hcd_init(hsotg, irq, params);
244 platform_set_drvdata(dev, hsotg);
249 static int __maybe_unused dwc2_suspend(struct device *dev)
251 struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
254 if (dwc2_is_device_mode(dwc2)) {
255 ret = s3c_hsotg_suspend(dwc2);
257 if (dwc2->lx_state == DWC2_L0)
260 phy_power_off(dwc2->phy);
266 static int __maybe_unused dwc2_resume(struct device *dev)
268 struct dwc2_hsotg *dwc2 = dev_get_drvdata(dev);
271 if (dwc2_is_device_mode(dwc2)) {
272 ret = s3c_hsotg_resume(dwc2);
274 phy_power_on(dwc2->phy);
281 static const struct dev_pm_ops dwc2_dev_pm_ops = {
282 SET_SYSTEM_SLEEP_PM_OPS(dwc2_suspend, dwc2_resume)
285 static struct platform_driver dwc2_platform_driver = {
287 .name = dwc2_driver_name,
288 .of_match_table = dwc2_of_match_table,
289 .pm = &dwc2_dev_pm_ops,
291 .probe = dwc2_driver_probe,
292 .remove = dwc2_driver_remove,
295 module_platform_driver(dwc2_platform_driver);
297 MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue");
298 MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>");
299 MODULE_LICENSE("Dual BSD/GPL");