blob: 121dbdafc06b7f0f189d0d438387eb256482daf3 [file] [log] [blame]
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -07001/*
2 * platform.c - DesignWare HS OTG Controller platform driver
3 *
4 * Copyright (C) Matthijs Kooijman <matthijs@stdin.nl>
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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.
18 *
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
22 * later version.
23 *
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.
35 */
36
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>
Stephen Warren831eae62013-11-26 18:58:01 -070042#include <linux/of_device.h>
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -070043#include <linux/platform_device.h>
44
Kever Yangc0155b92014-08-06 09:01:50 +080045#include <linux/usb/of.h>
46
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -070047#include "core.h"
48#include "hcd.h"
49
50static const char dwc2_driver_name[] = "dwc2";
51
Stephen Warren831eae62013-11-26 18:58:01 -070052static const struct dwc2_core_params params_bcm2835 = {
53 .otg_cap = 0, /* HNP/SRP capable */
54 .otg_ver = 0, /* 1.3 */
55 .dma_enable = 1,
56 .dma_desc_enable = 0,
57 .speed = 0, /* High Speed */
58 .enable_dynamic_fifo = 1,
59 .en_multiple_tx_fifo = 1,
60 .host_rx_fifo_size = 774, /* 774 DWORDs */
61 .host_nperio_tx_fifo_size = 256, /* 256 DWORDs */
62 .host_perio_tx_fifo_size = 512, /* 512 DWORDs */
63 .max_transfer_size = 65535,
64 .max_packet_count = 511,
65 .host_channels = 8,
66 .phy_type = 1, /* UTMI */
67 .phy_utmi_width = 8, /* 8 bits */
68 .phy_ulpi_ddr = 0, /* Single */
69 .phy_ulpi_ext_vbus = 0,
70 .i2c_enable = 0,
71 .ulpi_fs_ls = 0,
72 .host_support_fs_ls_low_power = 0,
73 .host_ls_low_power_phy_clk = 0, /* 48 MHz */
74 .ts_dline = 0,
75 .reload_ctl = 0,
76 .ahbcfg = 0x10,
Stephen Warren58b179d2013-12-03 20:56:05 -070077 .uframe_sched = 0,
Stephen Warren831eae62013-11-26 18:58:01 -070078};
79
Kever Yang95083142014-08-08 11:55:57 +080080static const struct dwc2_core_params params_rk3066 = {
81 .otg_cap = 2, /* non-HNP/non-SRP */
82 .otg_ver = -1,
83 .dma_enable = -1,
84 .dma_desc_enable = 0,
85 .speed = -1,
86 .enable_dynamic_fifo = 1,
87 .en_multiple_tx_fifo = -1,
88 .host_rx_fifo_size = 520, /* 520 DWORDs */
89 .host_nperio_tx_fifo_size = 128, /* 128 DWORDs */
90 .host_perio_tx_fifo_size = 256, /* 256 DWORDs */
91 .max_transfer_size = 65535,
92 .max_packet_count = -1,
93 .host_channels = -1,
94 .phy_type = -1,
95 .phy_utmi_width = -1,
96 .phy_ulpi_ddr = -1,
97 .phy_ulpi_ext_vbus = -1,
98 .i2c_enable = -1,
99 .ulpi_fs_ls = -1,
100 .host_support_fs_ls_low_power = -1,
101 .host_ls_low_power_phy_clk = -1,
102 .ts_dline = -1,
103 .reload_ctl = -1,
104 .ahbcfg = 0x7, /* INCR16 */
105 .uframe_sched = -1,
106};
107
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700108/**
109 * dwc2_driver_remove() - Called when the DWC_otg core is unregistered with the
110 * DWC_otg driver
111 *
112 * @dev: Platform device
113 *
114 * This routine is called, for example, when the rmmod command is executed. The
115 * device may or may not be electrically present. If it is present, the driver
116 * stops device processing. Any resources used on behalf of this device are
117 * freed.
118 */
119static int dwc2_driver_remove(struct platform_device *dev)
120{
121 struct dwc2_hsotg *hsotg = platform_get_drvdata(dev);
122
123 dwc2_hcd_remove(hsotg);
124
125 return 0;
126}
127
Stephen Warren831eae62013-11-26 18:58:01 -0700128static const struct of_device_id dwc2_of_match_table[] = {
129 { .compatible = "brcm,bcm2835-usb", .data = &params_bcm2835 },
Kever Yang95083142014-08-08 11:55:57 +0800130 { .compatible = "rockchip,rk3066-usb", .data = &params_rk3066 },
Stephen Warren831eae62013-11-26 18:58:01 -0700131 { .compatible = "snps,dwc2", .data = NULL },
132 {},
133};
134MODULE_DEVICE_TABLE(of, dwc2_of_match_table);
135
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700136/**
137 * dwc2_driver_probe() - Called when the DWC_otg core is bound to the DWC_otg
138 * driver
139 *
140 * @dev: Platform device
141 *
142 * This routine creates the driver components required to control the device
143 * (core, HCD, and PCD) and initializes the device. The driver components are
144 * stored in a dwc2_hsotg structure. A reference to the dwc2_hsotg is saved
145 * in the device private data. This allows the driver to access the dwc2_hsotg
146 * structure on subsequent calls to driver methods for this device.
147 */
148static int dwc2_driver_probe(struct platform_device *dev)
149{
Stephen Warren831eae62013-11-26 18:58:01 -0700150 const struct of_device_id *match;
151 const struct dwc2_core_params *params;
152 struct dwc2_core_params defparams;
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700153 struct dwc2_hsotg *hsotg;
154 struct resource *res;
155 int retval;
156 int irq;
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700157
Andre Heider861e0f52014-02-04 21:27:44 +0100158 if (usb_disabled())
159 return -ENODEV;
160
Stephen Warren831eae62013-11-26 18:58:01 -0700161 match = of_match_device(dwc2_of_match_table, &dev->dev);
162 if (match && match->data) {
163 params = match->data;
164 } else {
165 /* Default all params to autodetect */
166 dwc2_set_all_params(&defparams, -1);
167 params = &defparams;
Dinh Nguyen8b3e2332014-05-07 08:30:33 -0500168
169 /*
170 * Disable descriptor dma mode by default as the HW can support
171 * it, but does not support it for SPLIT transactions.
172 */
173 defparams.dma_desc_enable = 0;
Stephen Warren831eae62013-11-26 18:58:01 -0700174 }
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700175
176 hsotg = devm_kzalloc(&dev->dev, sizeof(*hsotg), GFP_KERNEL);
177 if (!hsotg)
178 return -ENOMEM;
179
180 hsotg->dev = &dev->dev;
181
Matthijs Kooijman642f2ec2013-05-17 10:52:55 +0200182 /*
183 * Use reasonable defaults so platforms don't have to provide these.
184 */
185 if (!dev->dev.dma_mask)
186 dev->dev.dma_mask = &dev->dev.coherent_dma_mask;
Russell King4cdbb4f2013-06-10 16:56:16 +0100187 retval = dma_set_coherent_mask(&dev->dev, DMA_BIT_MASK(32));
188 if (retval)
189 return retval;
Matthijs Kooijman642f2ec2013-05-17 10:52:55 +0200190
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700191 irq = platform_get_irq(dev, 0);
192 if (irq < 0) {
193 dev_err(&dev->dev, "missing IRQ resource\n");
Rashika Kheria097f2612013-10-26 23:12:19 +0530194 return irq;
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700195 }
196
197 res = platform_get_resource(dev, IORESOURCE_MEM, 0);
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700198 hsotg->regs = devm_ioremap_resource(&dev->dev, res);
199 if (IS_ERR(hsotg->regs))
200 return PTR_ERR(hsotg->regs);
201
202 dev_dbg(&dev->dev, "mapped PA %08lx to VA %p\n",
203 (unsigned long)res->start, hsotg->regs);
204
Kever Yangc0155b92014-08-06 09:01:50 +0800205 hsotg->dr_mode = of_usb_get_dr_mode(dev->dev.of_node);
206
Stephen Warren831eae62013-11-26 18:58:01 -0700207 retval = dwc2_hcd_init(hsotg, irq, params);
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700208 if (retval)
209 return retval;
210
211 platform_set_drvdata(dev, hsotg);
212
213 return retval;
214}
215
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700216static struct platform_driver dwc2_platform_driver = {
217 .driver = {
Geert Uytterhoeven1c126bc2013-11-12 20:07:19 +0100218 .name = dwc2_driver_name,
Matthijs Kooijman5b9974b2013-04-22 14:00:19 -0700219 .of_match_table = dwc2_of_match_table,
220 },
221 .probe = dwc2_driver_probe,
222 .remove = dwc2_driver_remove,
223};
224
225module_platform_driver(dwc2_platform_driver);
226
227MODULE_DESCRIPTION("DESIGNWARE HS OTG Platform Glue");
228MODULE_AUTHOR("Matthijs Kooijman <matthijs@stdin.nl>");
229MODULE_LICENSE("Dual BSD/GPL");