blob: 4318efbd078594d94de42d0eb608e784caf6d202 [file] [log] [blame]
Manu Gautam91223e02011-11-08 15:27:22 +05301/* ehci-msm2.c - HSUSB Host Controller Driver Implementation
2 *
3 * Copyright (c) 2008-2012, Code Aurora Forum. All rights reserved.
4 *
5 * Partly derived from ehci-fsl.c and ehci-hcd.c
6 * Copyright (c) 2000-2004 by David Brownell
7 * Copyright (c) 2005 MontaVista Software
8 *
9 * All source code in this file is licensed under the following license except
10 * where indicated.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you can find it at http://www.fsf.org
23 */
24
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/err.h>
28#include <linux/wakelock.h>
29#include <linux/pm_runtime.h>
30#include <linux/regulator/consumer.h>
31
32#include <linux/usb/ulpi.h>
33#include <linux/usb/msm_hsusb_hw.h>
34#include <linux/usb/msm_hsusb.h>
35#include <mach/clk.h>
36#include <mach/msm_iomap.h>
37
38#define MSM_USB_BASE (hcd->regs)
39
40struct msm_hcd {
41 struct ehci_hcd ehci;
42 struct device *dev;
43 struct clk *iface_clk;
44 struct clk *core_clk;
45 struct clk *alt_core_clk;
46 struct regulator *hsusb_vddcx;
47 struct regulator *hsusb_3p3;
48 struct regulator *hsusb_1p8;
49 struct regulator *vbus;
50 bool async_int;
Hemant Kumar56925352012-02-13 16:59:52 -080051 bool vbus_on;
Manu Gautam91223e02011-11-08 15:27:22 +053052 atomic_t in_lpm;
53 struct wake_lock wlock;
54};
55
56static inline struct msm_hcd *hcd_to_mhcd(struct usb_hcd *hcd)
57{
58 return (struct msm_hcd *) (hcd->hcd_priv);
59}
60
61static inline struct usb_hcd *mhcd_to_hcd(struct msm_hcd *mhcd)
62{
63 return container_of((void *) mhcd, struct usb_hcd, hcd_priv);
64}
65
66#define HSUSB_PHY_3P3_VOL_MIN 3050000 /* uV */
67#define HSUSB_PHY_3P3_VOL_MAX 3300000 /* uV */
68#define HSUSB_PHY_3P3_HPM_LOAD 50000 /* uA */
69
70#define HSUSB_PHY_1P8_VOL_MIN 1800000 /* uV */
71#define HSUSB_PHY_1P8_VOL_MAX 1800000 /* uV */
72#define HSUSB_PHY_1P8_HPM_LOAD 50000 /* uA */
73
74#define HSUSB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
75#define HSUSB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
76#define HSUSB_PHY_VDD_DIG_LOAD 49360 /* uA */
77
78static int msm_ehci_init_vddcx(struct msm_hcd *mhcd, int init)
79{
80 int ret = 0;
81
82 if (!init)
83 goto disable_reg;
84
85 mhcd->hsusb_vddcx = regulator_get(mhcd->dev, "HSUSB_VDDCX");
86 if (IS_ERR(mhcd->hsusb_vddcx)) {
87 dev_err(mhcd->dev, "unable to get ehci vddcx\n");
88 return PTR_ERR(mhcd->hsusb_vddcx);
89 }
90
91 ret = regulator_set_voltage(mhcd->hsusb_vddcx,
92 HSUSB_PHY_VDD_DIG_VOL_MIN,
93 HSUSB_PHY_VDD_DIG_VOL_MAX);
94 if (ret) {
95 dev_err(mhcd->dev, "unable to set the voltage"
96 "for ehci vddcx\n");
97 goto reg_set_voltage_err;
98 }
99
100 ret = regulator_set_optimum_mode(mhcd->hsusb_vddcx,
101 HSUSB_PHY_VDD_DIG_LOAD);
102 if (ret < 0) {
103 dev_err(mhcd->dev, "%s: Unable to set optimum mode of the"
104 " regulator: VDDCX\n", __func__);
105 goto reg_optimum_mode_err;
106 }
107
108 ret = regulator_enable(mhcd->hsusb_vddcx);
109 if (ret) {
110 dev_err(mhcd->dev, "unable to enable ehci vddcx\n");
111 goto reg_enable_err;
112 }
113
114 return 0;
115
116disable_reg:
117 regulator_disable(mhcd->hsusb_vddcx);
118reg_enable_err:
119 regulator_set_optimum_mode(mhcd->hsusb_vddcx, 0);
120reg_optimum_mode_err:
121 regulator_set_voltage(mhcd->hsusb_vddcx, 0,
122 HSUSB_PHY_VDD_DIG_VOL_MIN);
123reg_set_voltage_err:
124 regulator_put(mhcd->hsusb_vddcx);
125
126 return ret;
127
128}
129
130static int msm_ehci_ldo_init(struct msm_hcd *mhcd, int init)
131{
132 int rc = 0;
133
134 if (!init)
135 goto put_1p8;
136
137 mhcd->hsusb_3p3 = regulator_get(mhcd->dev, "HSUSB_3p3");
138 if (IS_ERR(mhcd->hsusb_3p3)) {
139 dev_err(mhcd->dev, "unable to get hsusb 3p3\n");
140 return PTR_ERR(mhcd->hsusb_3p3);
141 }
142
143 rc = regulator_set_voltage(mhcd->hsusb_3p3,
144 HSUSB_PHY_3P3_VOL_MIN, HSUSB_PHY_3P3_VOL_MAX);
145 if (rc) {
146 dev_err(mhcd->dev, "unable to set voltage level for"
147 "hsusb 3p3\n");
148 goto put_3p3;
149 }
150 mhcd->hsusb_1p8 = regulator_get(mhcd->dev, "HSUSB_1p8");
151 if (IS_ERR(mhcd->hsusb_1p8)) {
152 dev_err(mhcd->dev, "unable to get hsusb 1p8\n");
153 rc = PTR_ERR(mhcd->hsusb_1p8);
154 goto put_3p3_lpm;
155 }
156 rc = regulator_set_voltage(mhcd->hsusb_1p8,
157 HSUSB_PHY_1P8_VOL_MIN, HSUSB_PHY_1P8_VOL_MAX);
158 if (rc) {
159 dev_err(mhcd->dev, "unable to set voltage level for"
160 "hsusb 1p8\n");
161 goto put_1p8;
162 }
163
164 return 0;
165
166put_1p8:
167 regulator_set_voltage(mhcd->hsusb_1p8, 0, HSUSB_PHY_1P8_VOL_MAX);
168 regulator_put(mhcd->hsusb_1p8);
169put_3p3_lpm:
170 regulator_set_voltage(mhcd->hsusb_3p3, 0, HSUSB_PHY_3P3_VOL_MAX);
171put_3p3:
172 regulator_put(mhcd->hsusb_3p3);
173
174 return rc;
175}
176
177#ifdef CONFIG_PM_SLEEP
Hemant Kumar441356be2012-02-13 16:12:49 -0800178#define HSUSB_PHY_SUSP_DIG_VOL_P50 500000
179#define HSUSB_PHY_SUSP_DIG_VOL_P75 750000
Manu Gautam91223e02011-11-08 15:27:22 +0530180static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
181{
Hemant Kumar441356be2012-02-13 16:12:49 -0800182 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530183 int max_vol = HSUSB_PHY_VDD_DIG_VOL_MAX;
184 int min_vol;
185 int ret;
186
Hemant Kumar441356be2012-02-13 16:12:49 -0800187 pdata = mhcd->dev->platform_data;
188
Manu Gautam91223e02011-11-08 15:27:22 +0530189 if (high)
190 min_vol = HSUSB_PHY_VDD_DIG_VOL_MIN;
Hemant Kumar441356be2012-02-13 16:12:49 -0800191 else if (pdata && pdata->dock_connect_irq &&
192 !irq_read_line(pdata->dock_connect_irq))
193 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P75;
Manu Gautam91223e02011-11-08 15:27:22 +0530194 else
Hemant Kumar441356be2012-02-13 16:12:49 -0800195 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P50;
Manu Gautam91223e02011-11-08 15:27:22 +0530196
197 ret = regulator_set_voltage(mhcd->hsusb_vddcx, min_vol, max_vol);
198 if (ret) {
199 dev_err(mhcd->dev, "%s: unable to set the voltage of regulator"
200 " HSUSB_VDDCX\n", __func__);
201 return ret;
202 }
203
204 dev_dbg(mhcd->dev, "%s: min_vol:%d max_vol:%d\n", __func__, min_vol,
205 max_vol);
206
207 return ret;
208}
209#else
210static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
211{
212 return 0;
213}
214#endif
215
Manu Gautam91223e02011-11-08 15:27:22 +0530216static void msm_ehci_vbus_power(struct msm_hcd *mhcd, bool on)
217{
218 int ret;
219
220 if (!mhcd->vbus) {
221 pr_err("vbus is NULL.");
222 return;
223 }
Hemant Kumar56925352012-02-13 16:59:52 -0800224
225 if (mhcd->vbus_on == on)
226 return;
227
Manu Gautam91223e02011-11-08 15:27:22 +0530228 if (on) {
229 ret = regulator_enable(mhcd->vbus);
230 if (ret) {
231 pr_err("unable to enable vbus\n");
232 return;
233 }
Hemant Kumar56925352012-02-13 16:59:52 -0800234 mhcd->vbus_on = true;
Manu Gautam91223e02011-11-08 15:27:22 +0530235 } else {
236 ret = regulator_disable(mhcd->vbus);
237 if (ret) {
238 pr_err("unable to disable vbus\n");
239 return;
240 }
Hemant Kumar56925352012-02-13 16:59:52 -0800241 mhcd->vbus_on = false;
Manu Gautam91223e02011-11-08 15:27:22 +0530242 }
243}
244
Hemant Kumar56925352012-02-13 16:59:52 -0800245static irqreturn_t msm_ehci_dock_connect_irq(int irq, void *data)
246{
247 const struct msm_usb_host_platform_data *pdata;
248 struct msm_hcd *mhcd = data;
249 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
250
251 pdata = mhcd->dev->platform_data;
252
253 if (atomic_read(&mhcd->in_lpm))
254 usb_hcd_resume_root_hub(hcd);
255
256 if (irq_read_line(pdata->dock_connect_irq)) {
257 dev_dbg(mhcd->dev, "%s:Dock removed disable vbus\n", __func__);
258 msm_ehci_vbus_power(mhcd, 0);
259 } else {
260 dev_dbg(mhcd->dev, "%s:Dock connected enable vbus\n", __func__);
261 msm_ehci_vbus_power(mhcd, 1);
262 }
263
264 return IRQ_HANDLED;
265}
266
267static int msm_ehci_init_vbus(struct msm_hcd *mhcd, int init)
268{
269 int rc = 0;
270 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
271 const struct msm_usb_host_platform_data *pdata;
272
273 pdata = mhcd->dev->platform_data;
274
275 if (!init) {
276 regulator_put(mhcd->vbus);
277 if (pdata && pdata->dock_connect_irq)
278 free_irq(pdata->dock_connect_irq, mhcd);
279 return rc;
280 }
281
282 mhcd->vbus = regulator_get(mhcd->dev, "vbus");
283 if (IS_ERR(mhcd->vbus)) {
284 pr_err("Unable to get vbus\n");
285 return -ENODEV;
286 }
287
288 if (pdata) {
289 hcd->power_budget = pdata->power_budget;
290
291 if (pdata->dock_connect_irq) {
292 rc = request_threaded_irq(pdata->dock_connect_irq, NULL,
293 msm_ehci_dock_connect_irq,
294 IRQF_TRIGGER_FALLING |
295 IRQF_TRIGGER_RISING |
296 IRQF_ONESHOT, "msm_ehci_host", mhcd);
297 if (!rc)
298 enable_irq_wake(pdata->dock_connect_irq);
299 }
300 }
301 return rc;
302}
303
Manu Gautam91223e02011-11-08 15:27:22 +0530304static int msm_ehci_ldo_enable(struct msm_hcd *mhcd, int on)
305{
306 int ret = 0;
307
308 if (IS_ERR(mhcd->hsusb_1p8)) {
309 dev_err(mhcd->dev, "%s: HSUSB_1p8 is not initialized\n",
310 __func__);
311 return -ENODEV;
312 }
313
314 if (IS_ERR(mhcd->hsusb_3p3)) {
315 dev_err(mhcd->dev, "%s: HSUSB_3p3 is not initialized\n",
316 __func__);
317 return -ENODEV;
318 }
319
320 if (on) {
321 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8,
322 HSUSB_PHY_1P8_HPM_LOAD);
323 if (ret < 0) {
324 dev_err(mhcd->dev, "%s: Unable to set HPM of the"
325 " regulator: HSUSB_1p8\n", __func__);
326 return ret;
327 }
328
329 ret = regulator_enable(mhcd->hsusb_1p8);
330 if (ret) {
331 dev_err(mhcd->dev, "%s: unable to enable the hsusb"
332 " 1p8\n", __func__);
333 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
334 return ret;
335 }
336
337 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3,
338 HSUSB_PHY_3P3_HPM_LOAD);
339 if (ret < 0) {
340 dev_err(mhcd->dev, "%s: Unable to set HPM of the "
341 "regulator: HSUSB_3p3\n", __func__);
342 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
343 regulator_disable(mhcd->hsusb_1p8);
344 return ret;
345 }
346
347 ret = regulator_enable(mhcd->hsusb_3p3);
348 if (ret) {
349 dev_err(mhcd->dev, "%s: unable to enable the "
350 "hsusb 3p3\n", __func__);
351 regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
352 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
353 regulator_disable(mhcd->hsusb_1p8);
354 return ret;
355 }
356
357 } else {
358 ret = regulator_disable(mhcd->hsusb_1p8);
359 if (ret) {
360 dev_err(mhcd->dev, "%s: unable to disable the "
361 "hsusb 1p8\n", __func__);
362 return ret;
363 }
364
365 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
366 if (ret < 0)
367 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
368 "regulator: HSUSB_1p8\n", __func__);
369
370 ret = regulator_disable(mhcd->hsusb_3p3);
371 if (ret) {
372 dev_err(mhcd->dev, "%s: unable to disable the "
373 "hsusb 3p3\n", __func__);
374 return ret;
375 }
376 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
377 if (ret < 0)
378 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
379 "regulator: HSUSB_3p3\n", __func__);
380 }
381
382 dev_dbg(mhcd->dev, "reg (%s)\n", on ? "HPM" : "LPM");
383
384 return ret < 0 ? ret : 0;
385}
386
387
388#define ULPI_IO_TIMEOUT_USECS (10 * 1000)
389static int msm_ulpi_read(struct msm_hcd *mhcd, u32 reg)
390{
391 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
392 unsigned long timeout;
393
394 /* initiate read operation */
395 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
396 USB_ULPI_VIEWPORT);
397
398 /* wait for completion */
399 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
400 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
401 if (time_after(jiffies, timeout)) {
402 dev_err(mhcd->dev, "msm_ulpi_read: timeout %08x\n",
403 readl_relaxed(USB_ULPI_VIEWPORT));
404 return -ETIMEDOUT;
405 }
406 udelay(1);
407 }
408
409 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
410}
411
412
413static int msm_ulpi_write(struct msm_hcd *mhcd, u32 val, u32 reg)
414{
415 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
416 unsigned long timeout;
417
418 /* initiate write operation */
419 writel_relaxed(ULPI_RUN | ULPI_WRITE |
420 ULPI_ADDR(reg) | ULPI_DATA(val),
421 USB_ULPI_VIEWPORT);
422
423 /* wait for completion */
424 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
425 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
426 if (time_after(jiffies, timeout)) {
427 dev_err(mhcd->dev, "msm_ulpi_write: timeout\n");
428 return -ETIMEDOUT;
429 }
430 udelay(1);
431 }
432
433 return 0;
434}
435
436static int msm_ehci_link_clk_reset(struct msm_hcd *mhcd, bool assert)
437{
438 int ret;
439
440 if (assert) {
441 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_ASSERT);
442 if (ret)
443 dev_err(mhcd->dev, "usb alt_core_clk assert failed\n");
444 } else {
445 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_DEASSERT);
446 if (ret)
447 dev_err(mhcd->dev, "usb alt_core_clk deassert failed\n");
448 }
449
450 return ret;
451}
452
453static int msm_ehci_phy_reset(struct msm_hcd *mhcd)
454{
455 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
456 u32 val;
457 int ret;
458 int retries;
459
460 ret = msm_ehci_link_clk_reset(mhcd, 1);
461 if (ret)
462 return ret;
463
464 udelay(1);
465
466 ret = msm_ehci_link_clk_reset(mhcd, 0);
467 if (ret)
468 return ret;
469
470 val = readl_relaxed(USB_PORTSC) & ~PORTSC_PTS_MASK;
471 writel_relaxed(val | PORTSC_PTS_ULPI, USB_PORTSC);
472
473 for (retries = 3; retries > 0; retries--) {
474 ret = msm_ulpi_write(mhcd, ULPI_FUNC_CTRL_SUSPENDM,
475 ULPI_CLR(ULPI_FUNC_CTRL));
476 if (!ret)
477 break;
478 }
479 if (!retries)
480 return -ETIMEDOUT;
481
482 /* Wakeup the PHY with a reg-access for calibration */
483 for (retries = 3; retries > 0; retries--) {
484 ret = msm_ulpi_read(mhcd, ULPI_DEBUG);
485 if (ret != -ETIMEDOUT)
486 break;
487 }
488 if (!retries)
489 return -ETIMEDOUT;
490
491 dev_info(mhcd->dev, "phy_reset: success\n");
492
493 return 0;
494}
495
496#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
497static int msm_hsusb_reset(struct msm_hcd *mhcd)
498{
499 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
500 unsigned long timeout;
501 int ret;
502
503 clk_prepare_enable(mhcd->alt_core_clk);
504 ret = msm_ehci_phy_reset(mhcd);
505 if (ret) {
506 dev_err(mhcd->dev, "phy_reset failed\n");
507 return ret;
508 }
509
510 writel_relaxed(USBCMD_RESET, USB_USBCMD);
511
512 timeout = jiffies + usecs_to_jiffies(LINK_RESET_TIMEOUT_USEC);
513 while (readl_relaxed(USB_USBCMD) & USBCMD_RESET) {
514 if (time_after(jiffies, timeout))
515 return -ETIMEDOUT;
516 udelay(1);
517 }
518
519 /* select ULPI phy */
520 writel_relaxed(0x80000000, USB_PORTSC);
521
522 msleep(100);
523
524 writel_relaxed(0x0, USB_AHBBURST);
525 writel_relaxed(0x00, USB_AHBMODE);
526
527 /* Ensure that RESET operation is completed before turning off clock */
528 mb();
529 clk_disable_unprepare(mhcd->alt_core_clk);
530
531 /*rising edge interrupts with Dp rise and fall enabled*/
532 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_RISE);
533 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_FALL);
534
535 /*Clear the PHY interrupts by reading the PHY interrupt latch register*/
536 msm_ulpi_read(mhcd, ULPI_USB_INT_LATCH);
537
538 return 0;
539}
540
541#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
542#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
543
544#ifdef CONFIG_PM_SLEEP
545static int msm_ehci_suspend(struct msm_hcd *mhcd)
546{
547 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
548 unsigned long timeout;
549 u32 portsc;
550
551 if (atomic_read(&mhcd->in_lpm)) {
552 dev_dbg(mhcd->dev, "%s called in lpm\n", __func__);
553 return 0;
554 }
555
556 disable_irq(hcd->irq);
557
558 /* Set the PHCD bit, only if it is not set by the controller.
559 * PHY may take some time or even fail to enter into low power
560 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
561 * in failure case.
562 */
563 portsc = readl_relaxed(USB_PORTSC);
564 if (!(portsc & PORTSC_PHCD)) {
565 writel_relaxed(portsc | PORTSC_PHCD,
566 USB_PORTSC);
567
568 timeout = jiffies + usecs_to_jiffies(PHY_SUSPEND_TIMEOUT_USEC);
569 while (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD)) {
570 if (time_after(jiffies, timeout)) {
571 dev_err(mhcd->dev, "Unable to suspend PHY\n");
572 msm_hsusb_reset(mhcd);
573 break;
574 }
575 udelay(1);
576 }
577 }
578
579 /*
580 * PHY has capability to generate interrupt asynchronously in low
581 * power mode (LPM). This interrupt is level triggered. So USB IRQ
582 * line must be disabled till async interrupt enable bit is cleared
583 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
584 * block data communication from PHY.
585 */
586 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
587 ULPI_STP_CTRL, USB_USBCMD);
588
589 /*
590 * Ensure that hardware is put in low power mode before
591 * clocks are turned OFF and VDD is allowed to minimize.
592 */
593 mb();
594
595 clk_disable_unprepare(mhcd->iface_clk);
596 clk_disable_unprepare(mhcd->core_clk);
597
598 msm_ehci_config_vddcx(mhcd, 0);
599
600 atomic_set(&mhcd->in_lpm, 1);
601 enable_irq(hcd->irq);
602 wake_unlock(&mhcd->wlock);
603
604 dev_info(mhcd->dev, "EHCI USB in low power mode\n");
605
606 return 0;
607}
608
609static int msm_ehci_resume(struct msm_hcd *mhcd)
610{
611 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
612 unsigned long timeout;
613 unsigned temp;
614
615 if (!atomic_read(&mhcd->in_lpm)) {
616 dev_dbg(mhcd->dev, "%s called in !in_lpm\n", __func__);
617 return 0;
618 }
619
620 wake_lock(&mhcd->wlock);
621
622 clk_prepare_enable(mhcd->core_clk);
623 clk_prepare_enable(mhcd->iface_clk);
624
625 msm_ehci_config_vddcx(mhcd, 1);
626
627 temp = readl_relaxed(USB_USBCMD);
628 temp &= ~ASYNC_INTR_CTRL;
629 temp &= ~ULPI_STP_CTRL;
630 writel_relaxed(temp, USB_USBCMD);
631
632 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
633 goto skip_phy_resume;
634
635 temp = readl_relaxed(USB_PORTSC) & ~PORTSC_PHCD;
636 writel_relaxed(temp, USB_PORTSC);
637
638 timeout = jiffies + usecs_to_jiffies(PHY_RESUME_TIMEOUT_USEC);
639 while ((readl_relaxed(USB_PORTSC) & PORTSC_PHCD) ||
640 !(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE)) {
641 if (time_after(jiffies, timeout)) {
642 /*This is a fatal error. Reset the link and PHY*/
643 dev_err(mhcd->dev, "Unable to resume USB. Resetting the h/w\n");
644 msm_hsusb_reset(mhcd);
645 break;
646 }
647 udelay(1);
648 }
649
650skip_phy_resume:
651
652 atomic_set(&mhcd->in_lpm, 0);
653
654 if (mhcd->async_int) {
655 mhcd->async_int = false;
656 pm_runtime_put_noidle(mhcd->dev);
657 enable_irq(hcd->irq);
658 }
659
660 dev_info(mhcd->dev, "EHCI USB exited from low power mode\n");
661
662 return 0;
663}
664#endif
665
666static irqreturn_t msm_ehci_irq(struct usb_hcd *hcd)
667{
668 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
669
670 if (atomic_read(&mhcd->in_lpm)) {
671 disable_irq_nosync(hcd->irq);
672 mhcd->async_int = true;
673 pm_runtime_get(mhcd->dev);
674 return IRQ_HANDLED;
675 }
676
677 return ehci_irq(hcd);
678}
679
680static int msm_ehci_reset(struct usb_hcd *hcd)
681{
682 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
683 int retval;
684
685 ehci->caps = USB_CAPLENGTH;
686 ehci->regs = USB_CAPLENGTH +
687 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
688 dbg_hcs_params(ehci, "reset");
689 dbg_hcc_params(ehci, "reset");
690
691 /* cache the data to minimize the chip reads*/
692 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
693
694 hcd->has_tt = 1;
695 ehci->sbrn = HCD_USB2;
696
697 retval = ehci_halt(ehci);
698 if (retval)
699 return retval;
700
701 /* data structure init */
702 retval = ehci_init(hcd);
703 if (retval)
704 return retval;
705
706 retval = ehci_reset(ehci);
707 if (retval)
708 return retval;
709
710 /* bursts of unspecified length. */
711 writel_relaxed(0, USB_AHBBURST);
712 /* Use the AHB transactor */
713 writel_relaxed(0, USB_AHBMODE);
714 /* Disable streaming mode and select host mode */
715 writel_relaxed(0x13, USB_USBMODE);
716
717 ehci_port_power(ehci, 1);
718 return 0;
719}
720
721static struct hc_driver msm_hc2_driver = {
722 .description = hcd_name,
723 .product_desc = "Qualcomm EHCI Host Controller",
724 .hcd_priv_size = sizeof(struct msm_hcd),
725
726 /*
727 * generic hardware linkage
728 */
729 .irq = msm_ehci_irq,
730 .flags = HCD_USB2 | HCD_MEMORY,
731
732 .reset = msm_ehci_reset,
733 .start = ehci_run,
734
735 .stop = ehci_stop,
736 .shutdown = ehci_shutdown,
737
738 /*
739 * managing i/o requests and associated device resources
740 */
741 .urb_enqueue = ehci_urb_enqueue,
742 .urb_dequeue = ehci_urb_dequeue,
743 .endpoint_disable = ehci_endpoint_disable,
744 .endpoint_reset = ehci_endpoint_reset,
745 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
746
747 /*
748 * scheduling support
749 */
750 .get_frame_number = ehci_get_frame,
751
752 /*
753 * root hub support
754 */
755 .hub_status_data = ehci_hub_status_data,
756 .hub_control = ehci_hub_control,
757 .relinquish_port = ehci_relinquish_port,
758 .port_handed_over = ehci_port_handed_over,
759
760 /*
761 * PM support
762 */
763 .bus_suspend = ehci_bus_suspend,
764 .bus_resume = ehci_bus_resume,
765};
766
767static int msm_ehci_init_clocks(struct msm_hcd *mhcd, u32 init)
768{
769 int ret = 0;
770
771 if (!init)
772 goto put_clocks;
773
774 /* 60MHz alt_core_clk is for LINK to be used during PHY RESET */
775 mhcd->alt_core_clk = clk_get(mhcd->dev, "alt_core_clk");
776 if (IS_ERR(mhcd->alt_core_clk)) {
777 dev_err(mhcd->dev, "failed to get alt_core_clk\n");
778 ret = PTR_ERR(mhcd->alt_core_clk);
779 return ret;
780 }
781 clk_set_rate(mhcd->alt_core_clk, 60000000);
782
783 /* iface_clk is required for data transfers */
784 mhcd->iface_clk = clk_get(mhcd->dev, "iface_clk");
785 if (IS_ERR(mhcd->iface_clk)) {
786 dev_err(mhcd->dev, "failed to get iface_clk\n");
787 ret = PTR_ERR(mhcd->iface_clk);
788 goto put_alt_core_clk;
789 }
790
791 /* Link's protocol engine is based on pclk which must
792 * be running >55Mhz and frequency should also not change.
793 * Hence, vote for maximum clk frequency on its source
794 */
795 mhcd->core_clk = clk_get(mhcd->dev, "core_clk");
796 if (IS_ERR(mhcd->core_clk)) {
797 dev_err(mhcd->dev, "failed to get core_clk\n");
798 ret = PTR_ERR(mhcd->core_clk);
799 goto put_iface_clk;
800 }
801 clk_set_rate(mhcd->core_clk, INT_MAX);
802
803 clk_prepare_enable(mhcd->core_clk);
804 clk_prepare_enable(mhcd->iface_clk);
805
806 return 0;
807
808put_clocks:
809 clk_disable_unprepare(mhcd->iface_clk);
810 clk_disable_unprepare(mhcd->core_clk);
811 clk_put(mhcd->core_clk);
812put_iface_clk:
813 clk_put(mhcd->iface_clk);
814put_alt_core_clk:
815 clk_put(mhcd->alt_core_clk);
816
817 return ret;
818}
819
820static int __devinit ehci_msm2_probe(struct platform_device *pdev)
821{
822 struct usb_hcd *hcd;
823 struct resource *res;
824 struct msm_hcd *mhcd;
Hemant Kumar56925352012-02-13 16:59:52 -0800825 const struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530826 int ret;
827
828 dev_dbg(&pdev->dev, "ehci_msm2 probe\n");
829
830 hcd = usb_create_hcd(&msm_hc2_driver, &pdev->dev,
831 dev_name(&pdev->dev));
832 if (!hcd) {
833 dev_err(&pdev->dev, "Unable to create HCD\n");
834 return -ENOMEM;
835 }
836
837 hcd->irq = platform_get_irq(pdev, 0);
838 if (hcd->irq < 0) {
839 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
840 ret = hcd->irq;
841 goto put_hcd;
842 }
843
844 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
845 if (!res) {
846 dev_err(&pdev->dev, "Unable to get memory resource\n");
847 ret = -ENODEV;
848 goto put_hcd;
849 }
850
851 hcd->rsrc_start = res->start;
852 hcd->rsrc_len = resource_size(res);
853 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
854 if (!hcd->regs) {
855 dev_err(&pdev->dev, "ioremap failed\n");
856 ret = -ENOMEM;
857 goto put_hcd;
858 }
859
860 mhcd = hcd_to_mhcd(hcd);
861 mhcd->dev = &pdev->dev;
862
863 ret = msm_ehci_init_clocks(mhcd, 1);
864 if (ret) {
865 dev_err(&pdev->dev, "unable to initialize clocks\n");
866 ret = -ENODEV;
867 goto unmap;
868 }
869
870 ret = msm_ehci_init_vddcx(mhcd, 1);
871 if (ret) {
872 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
873 ret = -ENODEV;
874 goto deinit_clocks;
875 }
876
877 ret = msm_ehci_config_vddcx(mhcd, 1);
878 if (ret) {
879 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
880 goto deinit_vddcx;
881 }
882
883 ret = msm_ehci_ldo_init(mhcd, 1);
884 if (ret) {
885 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
886 goto deinit_vddcx;
887 }
888
889 ret = msm_ehci_ldo_enable(mhcd, 1);
890 if (ret) {
891 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
892 goto deinit_ldo;
893 }
894
895 ret = msm_ehci_init_vbus(mhcd, 1);
896 if (ret) {
897 dev_err(&pdev->dev, "unable to get vbus\n");
898 goto disable_ldo;
899 }
900
901 ret = msm_hsusb_reset(mhcd);
902 if (ret) {
903 dev_err(&pdev->dev, "hsusb PHY initialization failed\n");
904 goto vbus_deinit;
905 }
906
907 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
908 if (ret) {
909 dev_err(&pdev->dev, "unable to register HCD\n");
910 goto vbus_deinit;
911 }
912
Hemant Kumar56925352012-02-13 16:59:52 -0800913 pdata = mhcd->dev->platform_data;
914 if (pdata && (!pdata->dock_connect_irq ||
915 !irq_read_line(pdata->dock_connect_irq)))
916 msm_ehci_vbus_power(mhcd, 1);
Manu Gautam91223e02011-11-08 15:27:22 +0530917
918 device_init_wakeup(&pdev->dev, 1);
919 wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
920 wake_lock(&mhcd->wlock);
921 /*
922 * This pdev->dev is assigned parent of root-hub by USB core,
923 * hence, runtime framework automatically calls this driver's
924 * runtime APIs based on root-hub's state.
925 */
926 pm_runtime_set_active(&pdev->dev);
927 pm_runtime_enable(&pdev->dev);
928
929 return 0;
930
931vbus_deinit:
932 msm_ehci_init_vbus(mhcd, 0);
933disable_ldo:
934 msm_ehci_ldo_enable(mhcd, 0);
935deinit_ldo:
936 msm_ehci_ldo_init(mhcd, 0);
937deinit_vddcx:
938 msm_ehci_init_vddcx(mhcd, 0);
939deinit_clocks:
940 msm_ehci_init_clocks(mhcd, 0);
941unmap:
942 iounmap(hcd->regs);
943put_hcd:
944 usb_put_hcd(hcd);
945
946 return ret;
947}
948
949static int __devexit ehci_msm2_remove(struct platform_device *pdev)
950{
951 struct usb_hcd *hcd = platform_get_drvdata(pdev);
952 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
953
954 device_init_wakeup(&pdev->dev, 0);
955 pm_runtime_disable(&pdev->dev);
956 pm_runtime_set_suspended(&pdev->dev);
957
958 usb_remove_hcd(hcd);
Hemant Kumar56925352012-02-13 16:59:52 -0800959
Manu Gautam91223e02011-11-08 15:27:22 +0530960 msm_ehci_vbus_power(mhcd, 0);
961 msm_ehci_init_vbus(mhcd, 0);
962 msm_ehci_ldo_enable(mhcd, 0);
963 msm_ehci_ldo_init(mhcd, 0);
964 msm_ehci_init_vddcx(mhcd, 0);
965
966 msm_ehci_init_clocks(mhcd, 0);
967 wake_lock_destroy(&mhcd->wlock);
968 iounmap(hcd->regs);
969 usb_put_hcd(hcd);
970
971 return 0;
972}
973
974#ifdef CONFIG_PM_SLEEP
975static int ehci_msm2_pm_suspend(struct device *dev)
976{
977 struct usb_hcd *hcd = dev_get_drvdata(dev);
978 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
979
980 dev_dbg(dev, "ehci-msm2 PM suspend\n");
981
982 if (device_may_wakeup(dev))
983 enable_irq_wake(hcd->irq);
984
985 return msm_ehci_suspend(mhcd);
986
987}
988
989static int ehci_msm2_pm_resume(struct device *dev)
990{
991 int ret;
992 struct usb_hcd *hcd = dev_get_drvdata(dev);
993 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
994
995 dev_dbg(dev, "ehci-msm2 PM resume\n");
996
997 if (device_may_wakeup(dev))
998 disable_irq_wake(hcd->irq);
999
1000 ret = msm_ehci_resume(mhcd);
1001 if (ret)
1002 return ret;
1003
1004 /* Bring the device to full powered state upon system resume */
1005 pm_runtime_disable(dev);
1006 pm_runtime_set_active(dev);
1007 pm_runtime_enable(dev);
1008
1009 return 0;
1010}
1011#endif
1012
1013#ifdef CONFIG_PM_RUNTIME
1014static int ehci_msm2_runtime_idle(struct device *dev)
1015{
1016 dev_dbg(dev, "EHCI runtime idle\n");
1017
1018 return 0;
1019}
1020
1021static int ehci_msm2_runtime_suspend(struct device *dev)
1022{
1023 struct usb_hcd *hcd = dev_get_drvdata(dev);
1024 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1025
1026 dev_dbg(dev, "EHCI runtime suspend\n");
1027 return msm_ehci_suspend(mhcd);
1028}
1029
1030static int ehci_msm2_runtime_resume(struct device *dev)
1031{
1032 struct usb_hcd *hcd = dev_get_drvdata(dev);
1033 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1034
1035 dev_dbg(dev, "EHCI runtime resume\n");
1036 return msm_ehci_resume(mhcd);
1037}
1038#endif
1039
1040#ifdef CONFIG_PM
1041static const struct dev_pm_ops ehci_msm2_dev_pm_ops = {
1042 SET_SYSTEM_SLEEP_PM_OPS(ehci_msm2_pm_suspend, ehci_msm2_pm_resume)
1043 SET_RUNTIME_PM_OPS(ehci_msm2_runtime_suspend, ehci_msm2_runtime_resume,
1044 ehci_msm2_runtime_idle)
1045};
1046#endif
1047
1048static struct platform_driver ehci_msm2_driver = {
1049 .probe = ehci_msm2_probe,
1050 .remove = __devexit_p(ehci_msm2_remove),
1051 .driver = {
1052 .name = "msm_ehci_host",
1053#ifdef CONFIG_PM
1054 .pm = &ehci_msm2_dev_pm_ops,
1055#endif
1056 },
1057};