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