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