Merge tag 'timer' of git://git.kernel.org/pub/scm/linux/kernel/git/arm/arm-soc
[deliverable/linux.git] / drivers / usb / host / ehci-omap.c
CommitLineData
54ab2b02 1/*
4792a15b 2 * ehci-omap.c - driver for USBHOST on OMAP3/4 processors
54ab2b02 3 *
4792a15b
AG
4 * Bus Glue for the EHCI controllers in OMAP3/4
5 * Tested on several OMAP3 boards, and OMAP4 Pandaboard
54ab2b02 6 *
19403165 7 * Copyright (C) 2007-2011 Texas Instruments, Inc.
54ab2b02 8 * Author: Vikram Pandita <vikram.pandita@ti.com>
4792a15b 9 * Author: Anand Gadiyar <gadiyar@ti.com>
19403165 10 * Author: Keshava Munegowda <keshava_mgowda@ti.com>
54ab2b02
FB
11 *
12 * Copyright (C) 2009 Nokia Corporation
13 * Contact: Felipe Balbi <felipe.balbi@nokia.com>
14 *
15 * Based on "ehci-fsl.c" and "ehci-au1xxx.c" ehci glue layers
16 *
17 * This program is free software; you can redistribute it and/or modify
18 * it under the terms of the GNU General Public License as published by
19 * the Free Software Foundation; either version 2 of the License, or
20 * (at your option) any later version.
21 *
22 * This program is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * GNU General Public License for more details.
26 *
27 * You should have received a copy of the GNU General Public License
28 * along with this program; if not, write to the Free Software
29 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 *
19403165 31 * TODO (last updated Feb 27, 2010):
54ab2b02
FB
32 * - add kernel-doc
33 * - enable AUTOIDLE
54ab2b02 34 * - add suspend/resume
4792a15b
AG
35 * - add HSIC and TLL support
36 * - convert to use hwmod and runtime PM
54ab2b02
FB
37 */
38
39#include <linux/platform_device.h>
5a0e3ad6 40#include <linux/slab.h>
5aa4af2c 41#include <linux/usb/ulpi.h>
c76f782c 42#include <plat/usb.h>
6e3d4bec 43#include <linux/regulator/consumer.h>
6c984b06 44#include <linux/pm_runtime.h>
1fcb57d0 45#include <linux/gpio.h>
354ab856 46#include <linux/clk.h>
54ab2b02 47
54ab2b02 48/* EHCI Register Set */
572538de
AG
49#define EHCI_INSNREG04 (0xA0)
50#define EHCI_INSNREG04_DISABLE_UNSUSPEND (1 << 5)
54ab2b02
FB
51#define EHCI_INSNREG05_ULPI (0xA4)
52#define EHCI_INSNREG05_ULPI_CONTROL_SHIFT 31
53#define EHCI_INSNREG05_ULPI_PORTSEL_SHIFT 24
54#define EHCI_INSNREG05_ULPI_OPSEL_SHIFT 22
55#define EHCI_INSNREG05_ULPI_REGADD_SHIFT 16
56#define EHCI_INSNREG05_ULPI_EXTREGADD_SHIFT 8
57#define EHCI_INSNREG05_ULPI_WRDATA_SHIFT 0
58
354ab856
AG
59/* Errata i693 */
60static struct clk *utmi_p1_fck;
61static struct clk *utmi_p2_fck;
62static struct clk *xclk60mhsp1_ck;
63static struct clk *xclk60mhsp2_ck;
64static struct clk *usbhost_p1_fck;
65static struct clk *usbhost_p2_fck;
66static struct clk *init_60m_fclk;
67
19403165 68/*-------------------------------------------------------------------------*/
4792a15b 69
19403165 70static const struct hc_driver ehci_omap_hc_driver;
4792a15b 71
a42ccdc1 72
19403165 73static inline void ehci_write(void __iomem *base, u32 reg, u32 val)
54ab2b02
FB
74{
75 __raw_writel(val, base + reg);
76}
77
19403165 78static inline u32 ehci_read(void __iomem *base, u32 reg)
54ab2b02
FB
79{
80 return __raw_readl(base + reg);
81}
82
354ab856
AG
83/* Erratum i693 workaround sequence */
84static void omap_ehci_erratum_i693(struct ehci_hcd *ehci)
85{
86 int ret = 0;
87
88 /* Switch to the internal 60 MHz clock */
89 ret = clk_set_parent(utmi_p1_fck, init_60m_fclk);
90 if (ret != 0)
91 ehci_err(ehci, "init_60m_fclk set parent"
92 "failed error:%d\n", ret);
93
94 ret = clk_set_parent(utmi_p2_fck, init_60m_fclk);
95 if (ret != 0)
96 ehci_err(ehci, "init_60m_fclk set parent"
97 "failed error:%d\n", ret);
98
99 clk_enable(usbhost_p1_fck);
100 clk_enable(usbhost_p2_fck);
101
102 /* Wait 1ms and switch back to the external clock */
103 mdelay(1);
104 ret = clk_set_parent(utmi_p1_fck, xclk60mhsp1_ck);
105 if (ret != 0)
106 ehci_err(ehci, "xclk60mhsp1_ck set parent"
107 "failed error:%d\n", ret);
108
109 ret = clk_set_parent(utmi_p2_fck, xclk60mhsp2_ck);
110 if (ret != 0)
111 ehci_err(ehci, "xclk60mhsp2_ck set parent"
112 "failed error:%d\n", ret);
113
114 clk_disable(usbhost_p1_fck);
115 clk_disable(usbhost_p2_fck);
116}
117
19403165 118static void omap_ehci_soft_phy_reset(struct platform_device *pdev, u8 port)
5aa4af2c 119{
19403165 120 struct usb_hcd *hcd = dev_get_drvdata(&pdev->dev);
5aa4af2c
AKG
121 unsigned long timeout = jiffies + msecs_to_jiffies(1000);
122 unsigned reg = 0;
123
124 reg = ULPI_FUNC_CTRL_RESET
125 /* FUNCTION_CTRL_SET register */
126 | (ULPI_SET(ULPI_FUNC_CTRL) << EHCI_INSNREG05_ULPI_REGADD_SHIFT)
127 /* Write */
128 | (2 << EHCI_INSNREG05_ULPI_OPSEL_SHIFT)
129 /* PORTn */
130 | ((port + 1) << EHCI_INSNREG05_ULPI_PORTSEL_SHIFT)
131 /* start ULPI access*/
132 | (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT);
133
19403165 134 ehci_write(hcd->regs, EHCI_INSNREG05_ULPI, reg);
5aa4af2c
AKG
135
136 /* Wait for ULPI access completion */
19403165 137 while ((ehci_read(hcd->regs, EHCI_INSNREG05_ULPI)
5aa4af2c
AKG
138 & (1 << EHCI_INSNREG05_ULPI_CONTROL_SHIFT))) {
139 cpu_relax();
140
141 if (time_after(jiffies, timeout)) {
19403165 142 dev_dbg(&pdev->dev, "phy reset operation timed out\n");
5aa4af2c
AKG
143 break;
144 }
145 }
146}
147
354ab856
AG
148static int omap_ehci_hub_control(
149 struct usb_hcd *hcd,
150 u16 typeReq,
151 u16 wValue,
152 u16 wIndex,
153 char *buf,
154 u16 wLength
155)
156{
157 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
158 u32 __iomem *status_reg = &ehci->regs->port_status[
159 (wIndex & 0xff) - 1];
160 u32 temp;
161 unsigned long flags;
162 int retval = 0;
163
164 spin_lock_irqsave(&ehci->lock, flags);
165
166 if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
167 temp = ehci_readl(ehci, status_reg);
168 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
169 retval = -EPIPE;
170 goto done;
171 }
172
173 temp &= ~PORT_WKCONN_E;
174 temp |= PORT_WKDISC_E | PORT_WKOC_E;
175 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
176
177 omap_ehci_erratum_i693(ehci);
178
179 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
180 goto done;
181 }
182
183 spin_unlock_irqrestore(&ehci->lock, flags);
184
185 /* Handle the hub control events here */
186 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
187done:
188 spin_unlock_irqrestore(&ehci->lock, flags);
189 return retval;
190}
191
d4aefec5
ML
192static void disable_put_regulator(
193 struct ehci_hcd_omap_platform_data *pdata)
194{
195 int i;
196
197 for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
198 if (pdata->regulator[i]) {
199 regulator_disable(pdata->regulator[i]);
200 regulator_put(pdata->regulator[i]);
201 }
202 }
203}
54ab2b02
FB
204
205/* configure so an HC device and id are always provided */
206/* always called with process context; sleeping is OK */
207
208/**
209 * ehci_hcd_omap_probe - initialize TI-based HCDs
210 *
211 * Allocates basic resources for this USB host controller, and
212 * then invokes the start() method for the HCD associated with it
213 * through the hotplug entry's driver_data.
214 */
215static int ehci_hcd_omap_probe(struct platform_device *pdev)
216{
19403165
KM
217 struct device *dev = &pdev->dev;
218 struct ehci_hcd_omap_platform_data *pdata = dev->platform_data;
219 struct resource *res;
220 struct usb_hcd *hcd;
221 void __iomem *regs;
222 struct ehci_hcd *omap_ehci;
223 int ret = -ENODEV;
224 int irq;
6e3d4bec
KM
225 int i;
226 char supply[7];
54ab2b02 227
19403165
KM
228 if (usb_disabled())
229 return -ENODEV;
54ab2b02 230
19403165
KM
231 if (!dev->parent) {
232 dev_err(dev, "Missing parent device\n");
233 return -ENODEV;
54ab2b02
FB
234 }
235
19403165
KM
236 irq = platform_get_irq_byname(pdev, "ehci-irq");
237 if (irq < 0) {
238 dev_err(dev, "EHCI irq failed\n");
239 return -ENODEV;
240 }
54ab2b02 241
19403165
KM
242 res = platform_get_resource_byname(pdev,
243 IORESOURCE_MEM, "ehci");
244 if (!res) {
245 dev_err(dev, "UHH EHCI get resource failed\n");
246 return -ENODEV;
247 }
248
249 regs = ioremap(res->start, resource_size(res));
250 if (!regs) {
251 dev_err(dev, "UHH EHCI ioremap failed\n");
252 return -ENOMEM;
54ab2b02
FB
253 }
254
19403165
KM
255 hcd = usb_create_hcd(&ehci_omap_hc_driver, dev,
256 dev_name(dev));
54ab2b02 257 if (!hcd) {
19403165 258 dev_err(dev, "failed to create hcd with err %d\n", ret);
54ab2b02 259 ret = -ENOMEM;
19403165 260 goto err_io;
54ab2b02
FB
261 }
262
54ab2b02
FB
263 hcd->rsrc_start = res->start;
264 hcd->rsrc_len = resource_size(res);
19403165 265 hcd->regs = regs;
54ab2b02 266
6e3d4bec
KM
267 /* get ehci regulator and enable */
268 for (i = 0 ; i < OMAP3_HS_USB_PORTS ; i++) {
269 if (pdata->port_mode[i] != OMAP_EHCI_PORT_MODE_PHY) {
270 pdata->regulator[i] = NULL;
271 continue;
272 }
273 snprintf(supply, sizeof(supply), "hsusb%d", i);
274 pdata->regulator[i] = regulator_get(dev, supply);
275 if (IS_ERR(pdata->regulator[i])) {
276 pdata->regulator[i] = NULL;
277 dev_dbg(dev,
278 "failed to get ehci port%d regulator\n", i);
279 } else {
280 regulator_enable(pdata->regulator[i]);
281 }
282 }
283
c05995c3 284 /* Hold PHYs in reset while initializing EHCI controller */
1fcb57d0
KM
285 if (pdata->phy_reset) {
286 if (gpio_is_valid(pdata->reset_gpio_port[0]))
c05995c3 287 gpio_set_value_cansleep(pdata->reset_gpio_port[0], 0);
1fcb57d0
KM
288
289 if (gpio_is_valid(pdata->reset_gpio_port[1]))
c05995c3 290 gpio_set_value_cansleep(pdata->reset_gpio_port[1], 0);
1fcb57d0
KM
291
292 /* Hold the PHY in RESET for enough time till DIR is high */
293 udelay(10);
294 }
295
6c984b06
KM
296 pm_runtime_enable(dev);
297 pm_runtime_get_sync(dev);
54ab2b02 298
19403165
KM
299 /*
300 * An undocumented "feature" in the OMAP3 EHCI controller,
301 * causes suspended ports to be taken out of suspend when
302 * the USBCMD.Run/Stop bit is cleared (for example when
303 * we do ehci_bus_suspend).
304 * This breaks suspend-resume if the root-hub is allowed
305 * to suspend. Writing 1 to this undocumented register bit
306 * disables this feature and restores normal behavior.
307 */
308 ehci_write(regs, EHCI_INSNREG04,
309 EHCI_INSNREG04_DISABLE_UNSUSPEND);
54ab2b02 310
19403165
KM
311 /* Soft reset the PHY using PHY reset command over ULPI */
312 if (pdata->port_mode[0] == OMAP_EHCI_PORT_MODE_PHY)
313 omap_ehci_soft_phy_reset(pdev, 0);
314 if (pdata->port_mode[1] == OMAP_EHCI_PORT_MODE_PHY)
315 omap_ehci_soft_phy_reset(pdev, 1);
88114266 316
19403165
KM
317 omap_ehci = hcd_to_ehci(hcd);
318 omap_ehci->sbrn = 0x20;
54ab2b02 319
19403165
KM
320 /* we know this is the memory we want, no need to ioremap again */
321 omap_ehci->caps = hcd->regs;
322 omap_ehci->regs = hcd->regs
c430131a 323 + HC_LENGTH(ehci, readl(&omap_ehci->caps->hc_capbase));
54ab2b02 324
19403165
KM
325 dbg_hcs_params(omap_ehci, "reset");
326 dbg_hcc_params(omap_ehci, "reset");
bdb581bd 327
54ab2b02 328 /* cache this readonly data; minimize chip reads */
19403165 329 omap_ehci->hcs_params = readl(&omap_ehci->caps->hcs_params);
54ab2b02 330
876e0df9 331 ehci_reset(omap_ehci);
c05995c3
RD
332 ret = usb_add_hcd(hcd, irq, IRQF_SHARED);
333 if (ret) {
334 dev_err(dev, "failed to add hcd with err %d\n", ret);
335 goto err_add_hcd;
336 }
876e0df9 337
1fcb57d0
KM
338 if (pdata->phy_reset) {
339 /* Hold the PHY in RESET for enough time till
340 * PHY is settled and ready
341 */
342 udelay(10);
343
344 if (gpio_is_valid(pdata->reset_gpio_port[0]))
7b18389d 345 gpio_set_value_cansleep(pdata->reset_gpio_port[0], 1);
1fcb57d0
KM
346
347 if (gpio_is_valid(pdata->reset_gpio_port[1]))
7b18389d 348 gpio_set_value_cansleep(pdata->reset_gpio_port[1], 1);
1fcb57d0
KM
349 }
350
3aa2ae74
RD
351 /* root ports should always stay powered */
352 ehci_port_power(omap_ehci, 1);
353
354ab856
AG
354 /* get clocks */
355 utmi_p1_fck = clk_get(dev, "utmi_p1_gfclk");
356 if (IS_ERR(utmi_p1_fck)) {
357 ret = PTR_ERR(utmi_p1_fck);
358 dev_err(dev, "utmi_p1_gfclk failed error:%d\n", ret);
359 goto err_add_hcd;
360 }
361
362 xclk60mhsp1_ck = clk_get(dev, "xclk60mhsp1_ck");
363 if (IS_ERR(xclk60mhsp1_ck)) {
364 ret = PTR_ERR(xclk60mhsp1_ck);
365 dev_err(dev, "xclk60mhsp1_ck failed error:%d\n", ret);
366 goto err_utmi_p1_fck;
367 }
368
369 utmi_p2_fck = clk_get(dev, "utmi_p2_gfclk");
370 if (IS_ERR(utmi_p2_fck)) {
371 ret = PTR_ERR(utmi_p2_fck);
372 dev_err(dev, "utmi_p2_gfclk failed error:%d\n", ret);
373 goto err_xclk60mhsp1_ck;
374 }
375
376 xclk60mhsp2_ck = clk_get(dev, "xclk60mhsp2_ck");
377 if (IS_ERR(xclk60mhsp2_ck)) {
378 ret = PTR_ERR(xclk60mhsp2_ck);
379 dev_err(dev, "xclk60mhsp2_ck failed error:%d\n", ret);
380 goto err_utmi_p2_fck;
381 }
382
383 usbhost_p1_fck = clk_get(dev, "usb_host_hs_utmi_p1_clk");
384 if (IS_ERR(usbhost_p1_fck)) {
385 ret = PTR_ERR(usbhost_p1_fck);
386 dev_err(dev, "usbhost_p1_fck failed error:%d\n", ret);
387 goto err_xclk60mhsp2_ck;
388 }
389
390 usbhost_p2_fck = clk_get(dev, "usb_host_hs_utmi_p2_clk");
391 if (IS_ERR(usbhost_p2_fck)) {
392 ret = PTR_ERR(usbhost_p2_fck);
393 dev_err(dev, "usbhost_p2_fck failed error:%d\n", ret);
394 goto err_usbhost_p1_fck;
395 }
396
397 init_60m_fclk = clk_get(dev, "init_60m_fclk");
398 if (IS_ERR(init_60m_fclk)) {
399 ret = PTR_ERR(init_60m_fclk);
400 dev_err(dev, "init_60m_fclk failed error:%d\n", ret);
401 goto err_usbhost_p2_fck;
402 }
403
54ab2b02
FB
404 return 0;
405
354ab856
AG
406err_usbhost_p2_fck:
407 clk_put(usbhost_p2_fck);
408
409err_usbhost_p1_fck:
410 clk_put(usbhost_p1_fck);
411
412err_xclk60mhsp2_ck:
413 clk_put(xclk60mhsp2_ck);
414
415err_utmi_p2_fck:
416 clk_put(utmi_p2_fck);
417
418err_xclk60mhsp1_ck:
419 clk_put(xclk60mhsp1_ck);
420
421err_utmi_p1_fck:
422 clk_put(utmi_p1_fck);
423
54ab2b02 424err_add_hcd:
d4aefec5 425 disable_put_regulator(pdata);
6c984b06 426 pm_runtime_put_sync(dev);
54ab2b02 427
19403165 428err_io:
d4aefec5 429 iounmap(regs);
54ab2b02
FB
430 return ret;
431}
432
54ab2b02
FB
433
434/**
435 * ehci_hcd_omap_remove - shutdown processing for EHCI HCDs
436 * @pdev: USB Host Controller being removed
437 *
438 * Reverses the effect of usb_ehci_hcd_omap_probe(), first invoking
439 * the HCD's stop() method. It is always called from a thread
440 * context, normally "rmmod", "apmd", or something similar.
441 */
442static int ehci_hcd_omap_remove(struct platform_device *pdev)
443{
1fcb57d0
KM
444 struct device *dev = &pdev->dev;
445 struct usb_hcd *hcd = dev_get_drvdata(dev);
446 struct ehci_hcd_omap_platform_data *pdata = dev->platform_data;
54ab2b02
FB
447
448 usb_remove_hcd(hcd);
d4aefec5
ML
449 disable_put_regulator(dev->platform_data);
450 iounmap(hcd->regs);
54ab2b02 451 usb_put_hcd(hcd);
354ab856
AG
452
453 clk_put(utmi_p1_fck);
454 clk_put(utmi_p2_fck);
455 clk_put(xclk60mhsp1_ck);
456 clk_put(xclk60mhsp2_ck);
457 clk_put(usbhost_p1_fck);
458 clk_put(usbhost_p2_fck);
459 clk_put(init_60m_fclk);
460
6c984b06
KM
461 pm_runtime_put_sync(dev);
462 pm_runtime_disable(dev);
463
1fcb57d0
KM
464 if (pdata->phy_reset) {
465 if (gpio_is_valid(pdata->reset_gpio_port[0]))
466 gpio_free(pdata->reset_gpio_port[0]);
467
468 if (gpio_is_valid(pdata->reset_gpio_port[1]))
469 gpio_free(pdata->reset_gpio_port[1]);
470 }
54ab2b02
FB
471 return 0;
472}
473
474static void ehci_hcd_omap_shutdown(struct platform_device *pdev)
475{
19403165 476 struct usb_hcd *hcd = dev_get_drvdata(&pdev->dev);
54ab2b02
FB
477
478 if (hcd->driver->shutdown)
479 hcd->driver->shutdown(hcd);
480}
481
482static struct platform_driver ehci_hcd_omap_driver = {
483 .probe = ehci_hcd_omap_probe,
484 .remove = ehci_hcd_omap_remove,
485 .shutdown = ehci_hcd_omap_shutdown,
486 /*.suspend = ehci_hcd_omap_suspend, */
487 /*.resume = ehci_hcd_omap_resume, */
488 .driver = {
489 .name = "ehci-omap",
490 }
491};
492
493/*-------------------------------------------------------------------------*/
494
495static const struct hc_driver ehci_omap_hc_driver = {
496 .description = hcd_name,
497 .product_desc = "OMAP-EHCI Host Controller",
498 .hcd_priv_size = sizeof(struct ehci_hcd),
499
500 /*
501 * generic hardware linkage
502 */
503 .irq = ehci_irq,
504 .flags = HCD_MEMORY | HCD_USB2,
505
506 /*
507 * basic lifecycle operations
508 */
509 .reset = ehci_init,
510 .start = ehci_run,
511 .stop = ehci_stop,
512 .shutdown = ehci_shutdown,
513
514 /*
515 * managing i/o requests and associated device resources
516 */
517 .urb_enqueue = ehci_urb_enqueue,
518 .urb_dequeue = ehci_urb_dequeue,
519 .endpoint_disable = ehci_endpoint_disable,
520 .endpoint_reset = ehci_endpoint_reset,
521
522 /*
523 * scheduling support
524 */
525 .get_frame_number = ehci_get_frame,
526
527 /*
528 * root hub support
529 */
530 .hub_status_data = ehci_hub_status_data,
354ab856 531 .hub_control = omap_ehci_hub_control,
54ab2b02
FB
532 .bus_suspend = ehci_bus_suspend,
533 .bus_resume = ehci_bus_resume,
534
535 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
536};
537
538MODULE_ALIAS("platform:omap-ehci");
539MODULE_AUTHOR("Texas Instruments, Inc.");
540MODULE_AUTHOR("Felipe Balbi <felipe.balbi@nokia.com>");
541
This page took 0.223719 seconds and 5 git commands to generate.