blob: a797d51ecbe83fd9daa651d435a9931219333b4f [file] [log] [blame]
Daniel Mack7e8d5cd2009-10-28 01:14:59 +01001/*
2 * Copyright (c) 2008 Sascha Hauer <s.hauer@pengutronix.de>, Pengutronix
3 * Copyright (c) 2009 Daniel Mack <daniel@caiaq.de>
4 *
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful, but
11 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
12 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
18 */
19
20#include <linux/platform_device.h>
21#include <linux/clk.h>
22#include <linux/delay.h>
23#include <linux/usb/otg.h>
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010024#include <linux/usb/ulpi.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090025#include <linux/slab.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010026
Shawn Guo03a1d6b2011-07-21 22:24:10 +080027#include <mach/hardware.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010028#include <mach/mxc_ehci.h>
29
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010030#include <asm/mach-types.h>
31
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010032#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010033
34struct ehci_mxc_priv {
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +010035 struct clk *usbclk, *ahbclk, *phy1clk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010036 struct usb_hcd *hcd;
37};
38
39/* called during probe() after chip reset completes */
40static int ehci_mxc_setup(struct usb_hcd *hcd)
41{
42 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
43 int retval;
44
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010045 dbg_hcs_params(ehci, "reset");
46 dbg_hcc_params(ehci, "reset");
47
48 /* cache this readonly data; minimize chip reads */
49 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
50
Matthieu CASTET65fd4272010-09-06 18:26:56 +020051 hcd->has_tt = 1;
52
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010053 retval = ehci_halt(ehci);
54 if (retval)
55 return retval;
56
57 /* data structure init */
58 retval = ehci_init(hcd);
59 if (retval)
60 return retval;
61
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010062 ehci->sbrn = 0x20;
63
64 ehci_reset(ehci);
65
66 ehci_port_power(ehci, 0);
67 return 0;
68}
69
70static const struct hc_driver ehci_mxc_hc_driver = {
71 .description = hcd_name,
72 .product_desc = "Freescale On-Chip EHCI Host Controller",
73 .hcd_priv_size = sizeof(struct ehci_hcd),
74
75 /*
76 * generic hardware linkage
77 */
78 .irq = ehci_irq,
79 .flags = HCD_USB2 | HCD_MEMORY,
80
81 /*
82 * basic lifecycle operations
83 */
84 .reset = ehci_mxc_setup,
85 .start = ehci_run,
86 .stop = ehci_stop,
87 .shutdown = ehci_shutdown,
88
89 /*
90 * managing i/o requests and associated device resources
91 */
92 .urb_enqueue = ehci_urb_enqueue,
93 .urb_dequeue = ehci_urb_dequeue,
94 .endpoint_disable = ehci_endpoint_disable,
Paul Mundtb48d7f52010-11-30 17:57:02 +090095 .endpoint_reset = ehci_endpoint_reset,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010096
97 /*
98 * scheduling support
99 */
100 .get_frame_number = ehci_get_frame,
101
102 /*
103 * root hub support
104 */
105 .hub_status_data = ehci_hub_status_data,
106 .hub_control = ehci_hub_control,
107 .bus_suspend = ehci_bus_suspend,
108 .bus_resume = ehci_bus_resume,
109 .relinquish_port = ehci_relinquish_port,
110 .port_handed_over = ehci_port_handed_over,
Paul Mundtb48d7f52010-11-30 17:57:02 +0900111
112 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100113};
114
115static int ehci_mxc_drv_probe(struct platform_device *pdev)
116{
117 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
118 struct usb_hcd *hcd;
119 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +0100120 int irq, ret;
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100121 unsigned int flags;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100122 struct ehci_mxc_priv *priv;
123 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200124 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100125
126 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
127
128 if (!pdata) {
129 dev_err(dev, "No platform data given, bailing out.\n");
130 return -EINVAL;
131 }
132
133 irq = platform_get_irq(pdev, 0);
134
135 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
136 if (!hcd)
137 return -ENOMEM;
138
139 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
140 if (!priv) {
141 ret = -ENOMEM;
142 goto err_alloc;
143 }
144
145 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
146 if (!res) {
147 dev_err(dev, "Found HC with no register addr. Check setup!\n");
148 ret = -ENODEV;
149 goto err_get_resource;
150 }
151
152 hcd->rsrc_start = res->start;
153 hcd->rsrc_len = resource_size(res);
154
155 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
156 dev_dbg(dev, "controller already in use\n");
157 ret = -EBUSY;
158 goto err_request_mem;
159 }
160
161 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
162 if (!hcd->regs) {
163 dev_err(dev, "error mapping memory\n");
164 ret = -EFAULT;
165 goto err_ioremap;
166 }
167
168 /* enable clocks */
169 priv->usbclk = clk_get(dev, "usb");
170 if (IS_ERR(priv->usbclk)) {
171 ret = PTR_ERR(priv->usbclk);
172 goto err_clk;
173 }
174 clk_enable(priv->usbclk);
175
Eric Bénard4ea0af02010-06-08 11:02:58 +0200176 if (!cpu_is_mx35() && !cpu_is_mx25()) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100177 priv->ahbclk = clk_get(dev, "usb_ahb");
178 if (IS_ERR(priv->ahbclk)) {
179 ret = PTR_ERR(priv->ahbclk);
180 goto err_clk_ahb;
181 }
182 clk_enable(priv->ahbclk);
183 }
184
Eric Bénard3bb80292011-01-13 14:53:17 +0100185 /* "dr" device has its own clock on i.MX51 */
186 if (cpu_is_mx51() && (pdev->id == 0)) {
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100187 priv->phy1clk = clk_get(dev, "usb_phy1");
188 if (IS_ERR(priv->phy1clk)) {
189 ret = PTR_ERR(priv->phy1clk);
190 goto err_clk_phy;
191 }
192 clk_enable(priv->phy1clk);
193 }
194
195
196 /* call platform specific init function */
197 if (pdata->init) {
198 ret = pdata->init(pdev);
199 if (ret) {
200 dev_err(dev, "platform init failed\n");
201 goto err_init;
202 }
203 /* platforms need some time to settle changed IO settings */
204 mdelay(10);
205 }
206
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200207 ehci = hcd_to_ehci(hcd);
208
209 /* EHCI registers start at offset 0x100 */
210 ehci->caps = hcd->regs + 0x100;
211 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200212 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200213
214 /* set up the PORTSCx register */
215 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
216
217 /* is this really needed? */
218 msleep(10);
219
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100220 /* Initialize the transceiver */
221 if (pdata->otg) {
222 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200223 ret = usb_phy_init(pdata->otg);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200224 if (ret) {
225 dev_err(dev, "unable to init transceiver, probably missing\n");
226 ret = -ENODEV;
227 goto err_add;
228 }
Heikki Krogerus6e13c652012-02-13 13:24:20 +0200229 ret = otg_set_vbus(pdata->otg->otg, 1);
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200230 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100231 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200232 goto err_add;
233 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100234 }
235
236 priv->hcd = hcd;
237 platform_set_drvdata(pdev, priv);
238
Yong Zhangb5dd18d2011-09-07 16:10:52 +0800239 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100240 if (ret)
241 goto err_add;
242
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100243 if (pdata->otg) {
244 /*
245 * efikamx and efikasb have some hardware bug which is
246 * preventing usb to work unless CHRGVBUS is set.
247 * It's in violation of USB specs
248 */
249 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200250 flags = usb_phy_io_read(pdata->otg,
251 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100252 flags |= ULPI_OTG_CTRL_CHRGVBUS;
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200253 ret = usb_phy_io_write(pdata->otg, flags,
254 ULPI_OTG_CTRL);
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100255 if (ret) {
256 dev_err(dev, "unable to set CHRVBUS\n");
257 goto err_add;
258 }
259 }
260 }
261
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100262 return 0;
263
264err_add:
265 if (pdata && pdata->exit)
266 pdata->exit(pdev);
267err_init:
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100268 if (priv->phy1clk) {
269 clk_disable(priv->phy1clk);
270 clk_put(priv->phy1clk);
271 }
272err_clk_phy:
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100273 if (priv->ahbclk) {
274 clk_disable(priv->ahbclk);
275 clk_put(priv->ahbclk);
276 }
277err_clk_ahb:
278 clk_disable(priv->usbclk);
279 clk_put(priv->usbclk);
280err_clk:
281 iounmap(hcd->regs);
282err_ioremap:
283 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
284err_request_mem:
285err_get_resource:
286 kfree(priv);
287err_alloc:
288 usb_put_hcd(hcd);
289 return ret;
290}
291
292static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
293{
294 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
295 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
296 struct usb_hcd *hcd = priv->hcd;
297
298 if (pdata && pdata->exit)
299 pdata->exit(pdev);
300
301 if (pdata->otg)
Heikki Krogerusb96d3b02012-02-13 13:24:18 +0200302 usb_phy_shutdown(pdata->otg);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100303
304 usb_remove_hcd(hcd);
305 iounmap(hcd->regs);
306 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
307 usb_put_hcd(hcd);
308 platform_set_drvdata(pdev, NULL);
309
310 clk_disable(priv->usbclk);
311 clk_put(priv->usbclk);
312 if (priv->ahbclk) {
313 clk_disable(priv->ahbclk);
314 clk_put(priv->ahbclk);
315 }
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100316 if (priv->phy1clk) {
317 clk_disable(priv->phy1clk);
318 clk_put(priv->phy1clk);
319 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100320
321 kfree(priv);
322
323 return 0;
324}
325
326static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
327{
328 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
329 struct usb_hcd *hcd = priv->hcd;
330
331 if (hcd->driver->shutdown)
332 hcd->driver->shutdown(hcd);
333}
334
335MODULE_ALIAS("platform:mxc-ehci");
336
337static struct platform_driver ehci_mxc_driver = {
338 .probe = ehci_mxc_drv_probe,
339 .remove = __exit_p(ehci_mxc_drv_remove),
340 .shutdown = ehci_mxc_drv_shutdown,
341 .driver = {
342 .name = "mxc-ehci",
343 },
344};