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