Merge branch 'usb-linus' of git://git.kernel.org/pub/scm/linux/kernel/git/gregkh...
[pandora-kernel.git] / drivers / usb / host / ehci-mxc.c
1 /*
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>
24 #include <linux/slab.h>
25
26 #include <mach/mxc_ehci.h>
27
28 #define ULPI_VIEWPORT_OFFSET    0x170
29
30 struct ehci_mxc_priv {
31         struct clk *usbclk, *ahbclk;
32         struct usb_hcd *hcd;
33 };
34
35 /* called during probe() after chip reset completes */
36 static int ehci_mxc_setup(struct usb_hcd *hcd)
37 {
38         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
39         struct device *dev = hcd->self.controller;
40         struct mxc_usbh_platform_data *pdata = dev_get_platdata(dev);
41         int retval;
42
43         /* EHCI registers start at offset 0x100 */
44         ehci->caps = hcd->regs + 0x100;
45         ehci->regs = hcd->regs + 0x100 +
46             HC_LENGTH(ehci_readl(ehci, &ehci->caps->hc_capbase));
47         dbg_hcs_params(ehci, "reset");
48         dbg_hcc_params(ehci, "reset");
49
50         /* cache this readonly data; minimize chip reads */
51         ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
52
53         hcd->has_tt = 1;
54
55         retval = ehci_halt(ehci);
56         if (retval)
57                 return retval;
58
59         /* data structure init */
60         retval = ehci_init(hcd);
61         if (retval)
62                 return retval;
63
64         ehci->sbrn = 0x20;
65
66         ehci_reset(ehci);
67
68         /* set up the PORTSCx register */
69         ehci_writel(ehci, pdata->portsc, &ehci->regs->port_status[0]);
70
71         /* is this really needed? */
72         msleep(10);
73
74         ehci_port_power(ehci, 0);
75         return 0;
76 }
77
78 static const struct hc_driver ehci_mxc_hc_driver = {
79         .description = hcd_name,
80         .product_desc = "Freescale On-Chip EHCI Host Controller",
81         .hcd_priv_size = sizeof(struct ehci_hcd),
82
83         /*
84          * generic hardware linkage
85          */
86         .irq = ehci_irq,
87         .flags = HCD_USB2 | HCD_MEMORY,
88
89         /*
90          * basic lifecycle operations
91          */
92         .reset = ehci_mxc_setup,
93         .start = ehci_run,
94         .stop = ehci_stop,
95         .shutdown = ehci_shutdown,
96
97         /*
98          * managing i/o requests and associated device resources
99          */
100         .urb_enqueue = ehci_urb_enqueue,
101         .urb_dequeue = ehci_urb_dequeue,
102         .endpoint_disable = ehci_endpoint_disable,
103
104         /*
105          * scheduling support
106          */
107         .get_frame_number = ehci_get_frame,
108
109         /*
110          * root hub support
111          */
112         .hub_status_data = ehci_hub_status_data,
113         .hub_control = ehci_hub_control,
114         .bus_suspend = ehci_bus_suspend,
115         .bus_resume = ehci_bus_resume,
116         .relinquish_port = ehci_relinquish_port,
117         .port_handed_over = ehci_port_handed_over,
118 };
119
120 static int ehci_mxc_drv_probe(struct platform_device *pdev)
121 {
122         struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
123         struct usb_hcd *hcd;
124         struct resource *res;
125         int irq, ret;
126         struct ehci_mxc_priv *priv;
127         struct device *dev = &pdev->dev;
128
129         dev_info(&pdev->dev, "initializing i.MX USB Controller\n");
130
131         if (!pdata) {
132                 dev_err(dev, "No platform data given, bailing out.\n");
133                 return -EINVAL;
134         }
135
136         irq = platform_get_irq(pdev, 0);
137
138         hcd = usb_create_hcd(&ehci_mxc_hc_driver, dev, dev_name(dev));
139         if (!hcd)
140                 return -ENOMEM;
141
142         priv = kzalloc(sizeof(*priv), GFP_KERNEL);
143         if (!priv) {
144                 ret = -ENOMEM;
145                 goto err_alloc;
146         }
147
148         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149         if (!res) {
150                 dev_err(dev, "Found HC with no register addr. Check setup!\n");
151                 ret = -ENODEV;
152                 goto err_get_resource;
153         }
154
155         hcd->rsrc_start = res->start;
156         hcd->rsrc_len = resource_size(res);
157
158         if (!request_mem_region(hcd->rsrc_start, hcd->rsrc_len, hcd_name)) {
159                 dev_dbg(dev, "controller already in use\n");
160                 ret = -EBUSY;
161                 goto err_request_mem;
162         }
163
164         hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
165         if (!hcd->regs) {
166                 dev_err(dev, "error mapping memory\n");
167                 ret = -EFAULT;
168                 goto err_ioremap;
169         }
170
171         /* call platform specific init function */
172         if (pdata->init) {
173                 ret = pdata->init(pdev);
174                 if (ret) {
175                         dev_err(dev, "platform init failed\n");
176                         goto err_init;
177                 }
178                 /* platforms need some time to settle changed IO settings */
179                 mdelay(10);
180         }
181
182         /* enable clocks */
183         priv->usbclk = clk_get(dev, "usb");
184         if (IS_ERR(priv->usbclk)) {
185                 ret = PTR_ERR(priv->usbclk);
186                 goto err_clk;
187         }
188         clk_enable(priv->usbclk);
189
190         if (!cpu_is_mx35() && !cpu_is_mx25()) {
191                 priv->ahbclk = clk_get(dev, "usb_ahb");
192                 if (IS_ERR(priv->ahbclk)) {
193                         ret = PTR_ERR(priv->ahbclk);
194                         goto err_clk_ahb;
195                 }
196                 clk_enable(priv->ahbclk);
197         }
198
199         /* setup specific usb hw */
200         ret = mxc_initialize_usb_hw(pdev->id, pdata->flags);
201         if (ret < 0)
202                 goto err_init;
203
204         /* Initialize the transceiver */
205         if (pdata->otg) {
206                 pdata->otg->io_priv = hcd->regs + ULPI_VIEWPORT_OFFSET;
207                 ret = otg_init(pdata->otg);
208                 if (ret) {
209                         dev_err(dev, "unable to init transceiver, probably missing\n");
210                         ret = -ENODEV;
211                         goto err_add;
212                 }
213                 ret = otg_set_vbus(pdata->otg, 1);
214                 if (ret) {
215                         dev_err(dev, "unable to enable vbus on transceiver\n");
216                         goto err_add;
217                 }
218         }
219
220         priv->hcd = hcd;
221         platform_set_drvdata(pdev, priv);
222
223         ret = usb_add_hcd(hcd, irq, IRQF_DISABLED | IRQF_SHARED);
224         if (ret)
225                 goto err_add;
226
227         return 0;
228
229 err_add:
230         if (pdata && pdata->exit)
231                 pdata->exit(pdev);
232 err_init:
233         if (priv->ahbclk) {
234                 clk_disable(priv->ahbclk);
235                 clk_put(priv->ahbclk);
236         }
237 err_clk_ahb:
238         clk_disable(priv->usbclk);
239         clk_put(priv->usbclk);
240 err_clk:
241         iounmap(hcd->regs);
242 err_ioremap:
243         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
244 err_request_mem:
245 err_get_resource:
246         kfree(priv);
247 err_alloc:
248         usb_put_hcd(hcd);
249         return ret;
250 }
251
252 static int __exit ehci_mxc_drv_remove(struct platform_device *pdev)
253 {
254         struct mxc_usbh_platform_data *pdata = pdev->dev.platform_data;
255         struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
256         struct usb_hcd *hcd = priv->hcd;
257
258         if (pdata && pdata->exit)
259                 pdata->exit(pdev);
260
261         if (pdata->otg)
262                 otg_shutdown(pdata->otg);
263
264         usb_remove_hcd(hcd);
265         iounmap(hcd->regs);
266         release_mem_region(hcd->rsrc_start, hcd->rsrc_len);
267         usb_put_hcd(hcd);
268         platform_set_drvdata(pdev, NULL);
269
270         clk_disable(priv->usbclk);
271         clk_put(priv->usbclk);
272         if (priv->ahbclk) {
273                 clk_disable(priv->ahbclk);
274                 clk_put(priv->ahbclk);
275         }
276
277         kfree(priv);
278
279         return 0;
280 }
281
282 static void ehci_mxc_drv_shutdown(struct platform_device *pdev)
283 {
284         struct ehci_mxc_priv *priv = platform_get_drvdata(pdev);
285         struct usb_hcd *hcd = priv->hcd;
286
287         if (hcd->driver->shutdown)
288                 hcd->driver->shutdown(hcd);
289 }
290
291 MODULE_ALIAS("platform:mxc-ehci");
292
293 static struct platform_driver ehci_mxc_driver = {
294         .probe = ehci_mxc_drv_probe,
295         .remove = __exit_p(ehci_mxc_drv_remove),
296         .shutdown = ehci_mxc_drv_shutdown,
297         .driver = {
298                    .name = "mxc-ehci",
299         },
300 };