blob: 0c058be35a3887d5a36b9ffa005873f6824e9457 [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
27#include <mach/mxc_ehci.h>
28
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +010029#include <asm/mach-types.h>
30
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010031#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010032
33struct ehci_mxc_priv {
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +010034 struct clk *usbclk, *ahbclk, *phy1clk;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010035 struct usb_hcd *hcd;
36};
37
38/* called during probe() after chip reset completes */
39static int ehci_mxc_setup(struct usb_hcd *hcd)
40{
41 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
42 int retval;
43
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010044 dbg_hcs_params(ehci, "reset");
45 dbg_hcc_params(ehci, "reset");
46
47 /* cache this readonly data; minimize chip reads */
48 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
49
Matthieu CASTET65fd4272010-09-06 18:26:56 +020050 hcd->has_tt = 1;
51
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010052 retval = ehci_halt(ehci);
53 if (retval)
54 return retval;
55
56 /* data structure init */
57 retval = ehci_init(hcd);
58 if (retval)
59 return retval;
60
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010061 ehci->sbrn = 0x20;
62
63 ehci_reset(ehci);
64
65 ehci_port_power(ehci, 0);
66 return 0;
67}
68
69static const struct hc_driver ehci_mxc_hc_driver = {
70 .description = hcd_name,
71 .product_desc = "Freescale On-Chip EHCI Host Controller",
72 .hcd_priv_size = sizeof(struct ehci_hcd),
73
74 /*
75 * generic hardware linkage
76 */
77 .irq = ehci_irq,
78 .flags = HCD_USB2 | HCD_MEMORY,
79
80 /*
81 * basic lifecycle operations
82 */
83 .reset = ehci_mxc_setup,
84 .start = ehci_run,
85 .stop = ehci_stop,
86 .shutdown = ehci_shutdown,
87
88 /*
89 * managing i/o requests and associated device resources
90 */
91 .urb_enqueue = ehci_urb_enqueue,
92 .urb_dequeue = ehci_urb_dequeue,
93 .endpoint_disable = ehci_endpoint_disable,
Paul Mundtb48d7f52010-11-30 17:57:02 +090094 .endpoint_reset = ehci_endpoint_reset,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010095
96 /*
97 * scheduling support
98 */
99 .get_frame_number = ehci_get_frame,
100
101 /*
102 * root hub support
103 */
104 .hub_status_data = ehci_hub_status_data,
105 .hub_control = ehci_hub_control,
106 .bus_suspend = ehci_bus_suspend,
107 .bus_resume = ehci_bus_resume,
108 .relinquish_port = ehci_relinquish_port,
109 .port_handed_over = ehci_port_handed_over,
Paul Mundtb48d7f52010-11-30 17:57:02 +0900110
111 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100112};
113
114static int ehci_mxc_drv_probe(struct platform_device *pdev)
115{
116 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
117 struct usb_hcd *hcd;
118 struct resource *res;
Uwe Kleine-König724c8522010-11-02 10:30:57 +0100119 int irq, ret;
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100120 unsigned int flags;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100121 struct ehci_mxc_priv *priv;
122 struct device *dev = &pdev->dev;
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200123 struct ehci_hcd *ehci;
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100124
125 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
126
127 if (!pdata) {
128 dev_err(dev, "No platform data given, bailing out.\n");
129 return -EINVAL;
130 }
131
132 irq = platform_get_irq(pdev, 0);
133
134 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
135 if (!hcd)
136 return -ENOMEM;
137
138 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
139 if (!priv) {
140 ret = -ENOMEM;
141 goto err_alloc;
142 }
143
144 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
145 if (!res) {
146 dev_err(dev, "Found HC with no register addr. Check setup!\n");
147 ret = -ENODEV;
148 goto err_get_resource;
149 }
150
151 hcd->rsrc_start = res->start;
152 hcd->rsrc_len = resource_size(res);
153
154 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
155 dev_dbg(dev, "controller already in use\n");
156 ret = -EBUSY;
157 goto err_request_mem;
158 }
159
160 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
161 if (!hcd->regs) {
162 dev_err(dev, "error mapping memory\n");
163 ret = -EFAULT;
164 goto err_ioremap;
165 }
166
167 /* enable clocks */
168 priv->usbclk = clk_get(dev, "usb");
169 if (IS_ERR(priv->usbclk)) {
170 ret = PTR_ERR(priv->usbclk);
171 goto err_clk;
172 }
173 clk_enable(priv->usbclk);
174
Eric Bénard4ea0af02010-06-08 11:02:58 +0200175 if (!cpu_is_mx35() && !cpu_is_mx25()) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100176 priv->ahbclk = clk_get(dev, "usb_ahb");
177 if (IS_ERR(priv->ahbclk)) {
178 ret = PTR_ERR(priv->ahbclk);
179 goto err_clk_ahb;
180 }
181 clk_enable(priv->ahbclk);
182 }
183
Eric Bénard3bb80292011-01-13 14:53:17 +0100184 /* "dr" device has its own clock on i.MX51 */
185 if (cpu_is_mx51() && (pdev->id == 0)) {
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100186 priv->phy1clk = clk_get(dev, "usb_phy1");
187 if (IS_ERR(priv->phy1clk)) {
188 ret = PTR_ERR(priv->phy1clk);
189 goto err_clk_phy;
190 }
191 clk_enable(priv->phy1clk);
192 }
193
194
195 /* call platform specific init function */
196 if (pdata->init) {
197 ret = pdata->init(pdev);
198 if (ret) {
199 dev_err(dev, "platform init failed\n");
200 goto err_init;
201 }
202 /* platforms need some time to settle changed IO settings */
203 mdelay(10);
204 }
205
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200206 ehci = hcd_to_ehci(hcd);
207
208 /* EHCI registers start at offset 0x100 */
209 ehci->caps = hcd->regs + 0x100;
210 ehci->regs = hcd->regs + 0x100 +
Jan Anderssonc4301312011-05-03 20:11:57 +0200211 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
Fabio Estevam0247a7b2010-12-15 22:31:28 -0200212
213 /* set up the PORTSCx register */
214 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
215
216 /* is this really needed? */
217 msleep(10);
218
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100219 /* Initialize the transceiver */
220 if (pdata->otg) {
221 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200222 ret = otg_init(pdata->otg);
223 if (ret) {
224 dev_err(dev, "unable to init transceiver, probably missing\n");
225 ret = -ENODEV;
226 goto err_add;
227 }
228 ret = otg_set_vbus(pdata->otg, 1);
229 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100230 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200231 goto err_add;
232 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100233 }
234
235 priv->hcd = hcd;
236 platform_set_drvdata(pdev, priv);
237
238 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
239 if (ret)
240 goto err_add;
241
Arnaud Patard (Rtp)a464dc42011-01-18 00:04:12 +0100242 if (pdata->otg) {
243 /*
244 * efikamx and efikasb have some hardware bug which is
245 * preventing usb to work unless CHRGVBUS is set.
246 * It's in violation of USB specs
247 */
248 if (machine_is_mx51_efikamx() || machine_is_mx51_efikasb()) {
249 flags = otg_io_read(pdata->otg, ULPI_OTG_CTRL);
250 flags |= ULPI_OTG_CTRL_CHRGVBUS;
251 ret = otg_io_write(pdata->otg, flags, ULPI_OTG_CTRL);
252 if (ret) {
253 dev_err(dev, "unable to set CHRVBUS\n");
254 goto err_add;
255 }
256 }
257 }
258
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100259 return 0;
260
261err_add:
262 if (pdata && pdata->exit)
263 pdata->exit(pdev);
264err_init:
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100265 if (priv->phy1clk) {
266 clk_disable(priv->phy1clk);
267 clk_put(priv->phy1clk);
268 }
269err_clk_phy:
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100270 if (priv->ahbclk) {
271 clk_disable(priv->ahbclk);
272 clk_put(priv->ahbclk);
273 }
274err_clk_ahb:
275 clk_disable(priv->usbclk);
276 clk_put(priv->usbclk);
277err_clk:
278 iounmap(hcd->regs);
279err_ioremap:
280 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
281err_request_mem:
282err_get_resource:
283 kfree(priv);
284err_alloc:
285 usb_put_hcd(hcd);
286 return ret;
287}
288
289static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
290{
291 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
292 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
293 struct usb_hcd *hcd = priv->hcd;
294
295 if (pdata && pdata->exit)
296 pdata->exit(pdev);
297
298 if (pdata->otg)
299 otg_shutdown(pdata->otg);
300
301 usb_remove_hcd(hcd);
302 iounmap(hcd->regs);
303 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
304 usb_put_hcd(hcd);
305 platform_set_drvdata(pdev, NULL);
306
307 clk_disable(priv->usbclk);
308 clk_put(priv->usbclk);
309 if (priv->ahbclk) {
310 clk_disable(priv->ahbclk);
311 clk_put(priv->ahbclk);
312 }
Arnaud Patard (Rtp)711669e2010-12-20 16:48:58 +0100313 if (priv->phy1clk) {
314 clk_disable(priv->phy1clk);
315 clk_put(priv->phy1clk);
316 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100317
318 kfree(priv);
319
320 return 0;
321}
322
323static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
324{
325 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
326 struct usb_hcd *hcd = priv->hcd;
327
328 if (hcd->driver->shutdown)
329 hcd->driver->shutdown(hcd);
330}
331
332MODULE_ALIAS("platform:mxc-ehci");
333
334static struct platform_driver ehci_mxc_driver = {
335 .probe = ehci_mxc_drv_probe,
336 .remove = __exit_p(ehci_mxc_drv_remove),
337 .shutdown = ehci_mxc_drv_shutdown,
338 .driver = {
339 .name = "mxc-ehci",
340 },
341};