blob: 802fcf6f88396a8da3a598b2261a82747d1a591c [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>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090024#include <linux/slab.h>
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010025
26#include <mach/mxc_ehci.h>
27
28#define ULPI_VIEWPORT_OFFSET 0x170
Daniel Mack7e8d5cd2009-10-28 01:14:59 +010029
30struct ehci_mxc_priv {
31 struct clk *usbclk, *ahbclk;
32 struct usb_hcd *hcd;
33};
34
35/* called during probe() after chip reset completes */
36static int ehci_mxc_setup(struct usb_hcd *hcd)
37{
38 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
39 int retval;
40
41 /* EHCI registers start at offset 0x100 */
42 ehci->caps = hcd->regs + 0x100;
43 ehci->regs = hcd->regs + 0x100 +
44 HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
45 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;
120 int irq, ret, temp;
121 struct ehci_mxc_priv *priv;
122 struct device *dev = &pdev->dev;
123
124 dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
125
126 if (!pdata) {
127 dev_err(dev, "No platform data given, bailing out.\n");
128 return -EINVAL;
129 }
130
131 irq = platform_get_irq(pdev, 0);
132
133 hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
134 if (!hcd)
135 return -ENOMEM;
136
137 priv = kzalloc(sizeof(*priv), GFP_KERNEL);
138 if (!priv) {
139 ret = -ENOMEM;
140 goto err_alloc;
141 }
142
143 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
144 if (!res) {
145 dev_err(dev, "Found HC with no register addr. Check setup!\n");
146 ret = -ENODEV;
147 goto err_get_resource;
148 }
149
150 hcd->rsrc_start = res->start;
151 hcd->rsrc_len = resource_size(res);
152
153 if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
154 dev_dbg(dev, "controller already in use\n");
155 ret = -EBUSY;
156 goto err_request_mem;
157 }
158
159 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
160 if (!hcd->regs) {
161 dev_err(dev, "error mapping memory\n");
162 ret = -EFAULT;
163 goto err_ioremap;
164 }
165
Valentin Longchamp2f0e40a2010-01-20 19:43:23 +0100166 /* call platform specific init function */
167 if (pdata->init) {
168 ret = pdata->init(pdev);
169 if (ret) {
170 dev_err(dev, "platform init failed\n");
171 goto err_init;
172 }
173 /* platforms need some time to settle changed IO settings */
174 mdelay(10);
175 }
176
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100177 /* enable clocks */
178 priv->usbclk = clk_get(dev, "usb");
179 if (IS_ERR(priv->usbclk)) {
180 ret = PTR_ERR(priv->usbclk);
181 goto err_clk;
182 }
183 clk_enable(priv->usbclk);
184
Eric Bénard4ea0af02010-06-08 11:02:58 +0200185 if (!cpu_is_mx35() && !cpu_is_mx25()) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100186 priv->ahbclk = clk_get(dev, "usb_ahb");
187 if (IS_ERR(priv->ahbclk)) {
188 ret = PTR_ERR(priv->ahbclk);
189 goto err_clk_ahb;
190 }
191 clk_enable(priv->ahbclk);
192 }
193
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100194 /* set up the PORTSCx register */
Matthieu CASTET65fd4272010-09-06 18:26:56 +0200195 ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100196 mdelay(10);
197
Dinh Nguyen5a25ad82010-04-30 15:48:26 -0500198 /* setup specific usb hw */
199 ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100200 if (ret < 0)
201 goto err_init;
202
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100203 /* Initialize the transceiver */
204 if (pdata->otg) {
205 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200206 ret = otg_init(pdata->otg);
207 if (ret) {
208 dev_err(dev, "unable to init transceiver, probably missing\n");
209 ret = -ENODEV;
210 goto err_add;
211 }
212 ret = otg_set_vbus(pdata->otg, 1);
213 if (ret) {
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100214 dev_err(dev, "unable to enable vbus on transceiver\n");
Wolfram Sang4c9715d2010-06-15 12:34:23 +0200215 goto err_add;
216 }
Daniel Mack7e8d5cd2009-10-28 01:14:59 +0100217 }
218
219 priv->hcd = hcd;
220 platform_set_drvdata(pdev, priv);
221
222 ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
223 if (ret)
224 goto err_add;
225
226 return 0;
227
228err_add:
229 if (pdata && pdata->exit)
230 pdata->exit(pdev);
231err_init:
232 if (priv->ahbclk) {
233 clk_disable(priv->ahbclk);
234 clk_put(priv->ahbclk);
235 }
236err_clk_ahb:
237 clk_disable(priv->usbclk);
238 clk_put(priv->usbclk);
239err_clk:
240 iounmap(hcd->regs);
241err_ioremap:
242 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
243err_request_mem:
244err_get_resource:
245 kfree(priv);
246err_alloc:
247 usb_put_hcd(hcd);
248 return ret;
249}
250
251static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
252{
253 struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
254 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
255 struct usb_hcd *hcd = priv->hcd;
256
257 if (pdata && pdata->exit)
258 pdata->exit(pdev);
259
260 if (pdata->otg)
261 otg_shutdown(pdata->otg);
262
263 usb_remove_hcd(hcd);
264 iounmap(hcd->regs);
265 release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
266 usb_put_hcd(hcd);
267 platform_set_drvdata(pdev, NULL);
268
269 clk_disable(priv->usbclk);
270 clk_put(priv->usbclk);
271 if (priv->ahbclk) {
272 clk_disable(priv->ahbclk);
273 clk_put(priv->ahbclk);
274 }
275
276 kfree(priv);
277
278 return 0;
279}
280
281static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
282{
283 struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
284 struct usb_hcd *hcd = priv->hcd;
285
286 if (hcd->driver->shutdown)
287 hcd->driver->shutdown(hcd);
288}
289
290MODULE_ALIAS("platform:mxc-ehci");
291
292static struct platform_driver ehci_mxc_driver = {
293 .probe = ehci_mxc_drv_probe,
294 .remove = __exit_p(ehci_mxc_drv_remove),
295 .shutdown = ehci_mxc_drv_shutdown,
296 .driver = {
297 .name = "mxc-ehci",
298 },
299};