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