blob: 8a87a6af21c7dbfbce7a1ec30dc4c21c21fd7cca [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
Chiranjeevi Velempati35d46ab2012-07-18 20:36:53 +0530663 usb_hcd_resume_root_hub(hcd);
Manu Gautam91223e02011-11-08 15:27:22 +0530664 atomic_set(&mhcd->in_lpm, 0);
665
666 if (mhcd->async_int) {
667 mhcd->async_int = false;
668 pm_runtime_put_noidle(mhcd->dev);
669 enable_irq(hcd->irq);
670 }
671
672 dev_info(mhcd->dev, "EHCI USB exited from low power mode\n");
673
674 return 0;
675}
676#endif
677
678static irqreturn_t msm_ehci_irq(struct usb_hcd *hcd)
679{
680 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
681
682 if (atomic_read(&mhcd->in_lpm)) {
683 disable_irq_nosync(hcd->irq);
684 mhcd->async_int = true;
685 pm_runtime_get(mhcd->dev);
686 return IRQ_HANDLED;
687 }
688
689 return ehci_irq(hcd);
690}
691
692static int msm_ehci_reset(struct usb_hcd *hcd)
693{
694 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
695 int retval;
696
697 ehci->caps = USB_CAPLENGTH;
698 ehci->regs = USB_CAPLENGTH +
699 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
700 dbg_hcs_params(ehci, "reset");
701 dbg_hcc_params(ehci, "reset");
702
703 /* cache the data to minimize the chip reads*/
704 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
705
706 hcd->has_tt = 1;
707 ehci->sbrn = HCD_USB2;
708
709 retval = ehci_halt(ehci);
710 if (retval)
711 return retval;
712
713 /* data structure init */
714 retval = ehci_init(hcd);
715 if (retval)
716 return retval;
717
718 retval = ehci_reset(ehci);
719 if (retval)
720 return retval;
721
722 /* bursts of unspecified length. */
723 writel_relaxed(0, USB_AHBBURST);
724 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530725 writel_relaxed(0x08, USB_AHBMODE);
Manu Gautam91223e02011-11-08 15:27:22 +0530726 /* Disable streaming mode and select host mode */
727 writel_relaxed(0x13, USB_USBMODE);
728
729 ehci_port_power(ehci, 1);
730 return 0;
731}
732
733static struct hc_driver msm_hc2_driver = {
734 .description = hcd_name,
735 .product_desc = "Qualcomm EHCI Host Controller",
736 .hcd_priv_size = sizeof(struct msm_hcd),
737
738 /*
739 * generic hardware linkage
740 */
741 .irq = msm_ehci_irq,
742 .flags = HCD_USB2 | HCD_MEMORY,
743
744 .reset = msm_ehci_reset,
745 .start = ehci_run,
746
747 .stop = ehci_stop,
748 .shutdown = ehci_shutdown,
749
750 /*
751 * managing i/o requests and associated device resources
752 */
753 .urb_enqueue = ehci_urb_enqueue,
754 .urb_dequeue = ehci_urb_dequeue,
755 .endpoint_disable = ehci_endpoint_disable,
756 .endpoint_reset = ehci_endpoint_reset,
757 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
758
759 /*
760 * scheduling support
761 */
762 .get_frame_number = ehci_get_frame,
763
764 /*
765 * root hub support
766 */
767 .hub_status_data = ehci_hub_status_data,
768 .hub_control = ehci_hub_control,
769 .relinquish_port = ehci_relinquish_port,
770 .port_handed_over = ehci_port_handed_over,
771
772 /*
773 * PM support
774 */
775 .bus_suspend = ehci_bus_suspend,
776 .bus_resume = ehci_bus_resume,
777};
778
779static int msm_ehci_init_clocks(struct msm_hcd *mhcd, u32 init)
780{
781 int ret = 0;
782
783 if (!init)
784 goto put_clocks;
785
786 /* 60MHz alt_core_clk is for LINK to be used during PHY RESET */
787 mhcd->alt_core_clk = clk_get(mhcd->dev, "alt_core_clk");
788 if (IS_ERR(mhcd->alt_core_clk)) {
789 dev_err(mhcd->dev, "failed to get alt_core_clk\n");
790 ret = PTR_ERR(mhcd->alt_core_clk);
791 return ret;
792 }
793 clk_set_rate(mhcd->alt_core_clk, 60000000);
794
795 /* iface_clk is required for data transfers */
796 mhcd->iface_clk = clk_get(mhcd->dev, "iface_clk");
797 if (IS_ERR(mhcd->iface_clk)) {
798 dev_err(mhcd->dev, "failed to get iface_clk\n");
799 ret = PTR_ERR(mhcd->iface_clk);
800 goto put_alt_core_clk;
801 }
802
803 /* Link's protocol engine is based on pclk which must
804 * be running >55Mhz and frequency should also not change.
805 * Hence, vote for maximum clk frequency on its source
806 */
807 mhcd->core_clk = clk_get(mhcd->dev, "core_clk");
808 if (IS_ERR(mhcd->core_clk)) {
809 dev_err(mhcd->dev, "failed to get core_clk\n");
810 ret = PTR_ERR(mhcd->core_clk);
811 goto put_iface_clk;
812 }
813 clk_set_rate(mhcd->core_clk, INT_MAX);
814
815 clk_prepare_enable(mhcd->core_clk);
816 clk_prepare_enable(mhcd->iface_clk);
817
818 return 0;
819
820put_clocks:
821 clk_disable_unprepare(mhcd->iface_clk);
822 clk_disable_unprepare(mhcd->core_clk);
823 clk_put(mhcd->core_clk);
824put_iface_clk:
825 clk_put(mhcd->iface_clk);
826put_alt_core_clk:
827 clk_put(mhcd->alt_core_clk);
828
829 return ret;
830}
831
832static int __devinit ehci_msm2_probe(struct platform_device *pdev)
833{
834 struct usb_hcd *hcd;
835 struct resource *res;
836 struct msm_hcd *mhcd;
Hemant Kumar56925352012-02-13 16:59:52 -0800837 const struct msm_usb_host_platform_data *pdata;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700838 char pdev_name[PDEV_NAME_LEN];
Manu Gautam91223e02011-11-08 15:27:22 +0530839 int ret;
840
841 dev_dbg(&pdev->dev, "ehci_msm2 probe\n");
842
843 hcd = usb_create_hcd(&msm_hc2_driver, &pdev->dev,
844 dev_name(&pdev->dev));
845 if (!hcd) {
846 dev_err(&pdev->dev, "Unable to create HCD\n");
847 return -ENOMEM;
848 }
849
850 hcd->irq = platform_get_irq(pdev, 0);
851 if (hcd->irq < 0) {
852 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
853 ret = hcd->irq;
854 goto put_hcd;
855 }
856
857 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
858 if (!res) {
859 dev_err(&pdev->dev, "Unable to get memory resource\n");
860 ret = -ENODEV;
861 goto put_hcd;
862 }
863
864 hcd->rsrc_start = res->start;
865 hcd->rsrc_len = resource_size(res);
866 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
867 if (!hcd->regs) {
868 dev_err(&pdev->dev, "ioremap failed\n");
869 ret = -ENOMEM;
870 goto put_hcd;
871 }
872
873 mhcd = hcd_to_mhcd(hcd);
874 mhcd->dev = &pdev->dev;
875
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700876 snprintf(pdev_name, PDEV_NAME_LEN, "%s.%d", pdev->name, pdev->id);
877 mhcd->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, pdev_name);
878 if (IS_ERR(mhcd->xo_handle)) {
879 dev_err(&pdev->dev, "%s not able to get the handle "
880 "to vote for TCXO D0 buffer\n", __func__);
881 ret = PTR_ERR(mhcd->xo_handle);
882 goto unmap;
883 }
884
885 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
886 if (ret) {
887 dev_err(&pdev->dev, "%s failed to vote for TCXO "
888 "D0 buffer%d\n", __func__, ret);
889 goto free_xo_handle;
890 }
891
Manu Gautam91223e02011-11-08 15:27:22 +0530892 ret = msm_ehci_init_clocks(mhcd, 1);
893 if (ret) {
894 dev_err(&pdev->dev, "unable to initialize clocks\n");
895 ret = -ENODEV;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700896 goto devote_xo_handle;
Manu Gautam91223e02011-11-08 15:27:22 +0530897 }
898
899 ret = msm_ehci_init_vddcx(mhcd, 1);
900 if (ret) {
901 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
902 ret = -ENODEV;
903 goto deinit_clocks;
904 }
905
906 ret = msm_ehci_config_vddcx(mhcd, 1);
907 if (ret) {
908 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
909 goto deinit_vddcx;
910 }
911
912 ret = msm_ehci_ldo_init(mhcd, 1);
913 if (ret) {
914 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
915 goto deinit_vddcx;
916 }
917
918 ret = msm_ehci_ldo_enable(mhcd, 1);
919 if (ret) {
920 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
921 goto deinit_ldo;
922 }
923
924 ret = msm_ehci_init_vbus(mhcd, 1);
925 if (ret) {
926 dev_err(&pdev->dev, "unable to get vbus\n");
927 goto disable_ldo;
928 }
929
930 ret = msm_hsusb_reset(mhcd);
931 if (ret) {
932 dev_err(&pdev->dev, "hsusb PHY initialization failed\n");
933 goto vbus_deinit;
934 }
935
936 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
937 if (ret) {
938 dev_err(&pdev->dev, "unable to register HCD\n");
939 goto vbus_deinit;
940 }
941
Hemant Kumar56925352012-02-13 16:59:52 -0800942 pdata = mhcd->dev->platform_data;
943 if (pdata && (!pdata->dock_connect_irq ||
944 !irq_read_line(pdata->dock_connect_irq)))
945 msm_ehci_vbus_power(mhcd, 1);
Manu Gautam91223e02011-11-08 15:27:22 +0530946
947 device_init_wakeup(&pdev->dev, 1);
948 wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
949 wake_lock(&mhcd->wlock);
950 /*
951 * This pdev->dev is assigned parent of root-hub by USB core,
952 * hence, runtime framework automatically calls this driver's
953 * runtime APIs based on root-hub's state.
954 */
955 pm_runtime_set_active(&pdev->dev);
956 pm_runtime_enable(&pdev->dev);
957
958 return 0;
959
960vbus_deinit:
961 msm_ehci_init_vbus(mhcd, 0);
962disable_ldo:
963 msm_ehci_ldo_enable(mhcd, 0);
964deinit_ldo:
965 msm_ehci_ldo_init(mhcd, 0);
966deinit_vddcx:
967 msm_ehci_init_vddcx(mhcd, 0);
968deinit_clocks:
969 msm_ehci_init_clocks(mhcd, 0);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700970devote_xo_handle:
971 msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
972free_xo_handle:
973 msm_xo_put(mhcd->xo_handle);
Manu Gautam91223e02011-11-08 15:27:22 +0530974unmap:
975 iounmap(hcd->regs);
976put_hcd:
977 usb_put_hcd(hcd);
978
979 return ret;
980}
981
982static int __devexit ehci_msm2_remove(struct platform_device *pdev)
983{
984 struct usb_hcd *hcd = platform_get_drvdata(pdev);
985 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
986
987 device_init_wakeup(&pdev->dev, 0);
988 pm_runtime_disable(&pdev->dev);
989 pm_runtime_set_suspended(&pdev->dev);
990
991 usb_remove_hcd(hcd);
Hemant Kumar56925352012-02-13 16:59:52 -0800992
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700993 msm_xo_put(mhcd->xo_handle);
Manu Gautam91223e02011-11-08 15:27:22 +0530994 msm_ehci_vbus_power(mhcd, 0);
995 msm_ehci_init_vbus(mhcd, 0);
996 msm_ehci_ldo_enable(mhcd, 0);
997 msm_ehci_ldo_init(mhcd, 0);
998 msm_ehci_init_vddcx(mhcd, 0);
999
1000 msm_ehci_init_clocks(mhcd, 0);
1001 wake_lock_destroy(&mhcd->wlock);
1002 iounmap(hcd->regs);
1003 usb_put_hcd(hcd);
1004
1005 return 0;
1006}
1007
1008#ifdef CONFIG_PM_SLEEP
1009static int ehci_msm2_pm_suspend(struct device *dev)
1010{
1011 struct usb_hcd *hcd = dev_get_drvdata(dev);
1012 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1013
1014 dev_dbg(dev, "ehci-msm2 PM suspend\n");
1015
1016 if (device_may_wakeup(dev))
1017 enable_irq_wake(hcd->irq);
1018
1019 return msm_ehci_suspend(mhcd);
1020
1021}
1022
1023static int ehci_msm2_pm_resume(struct device *dev)
1024{
1025 int ret;
1026 struct usb_hcd *hcd = dev_get_drvdata(dev);
1027 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1028
1029 dev_dbg(dev, "ehci-msm2 PM resume\n");
1030
1031 if (device_may_wakeup(dev))
1032 disable_irq_wake(hcd->irq);
1033
1034 ret = msm_ehci_resume(mhcd);
1035 if (ret)
1036 return ret;
1037
1038 /* Bring the device to full powered state upon system resume */
1039 pm_runtime_disable(dev);
1040 pm_runtime_set_active(dev);
1041 pm_runtime_enable(dev);
1042
1043 return 0;
1044}
1045#endif
1046
1047#ifdef CONFIG_PM_RUNTIME
1048static int ehci_msm2_runtime_idle(struct device *dev)
1049{
1050 dev_dbg(dev, "EHCI runtime idle\n");
1051
1052 return 0;
1053}
1054
1055static int ehci_msm2_runtime_suspend(struct device *dev)
1056{
1057 struct usb_hcd *hcd = dev_get_drvdata(dev);
1058 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1059
1060 dev_dbg(dev, "EHCI runtime suspend\n");
1061 return msm_ehci_suspend(mhcd);
1062}
1063
1064static int ehci_msm2_runtime_resume(struct device *dev)
1065{
1066 struct usb_hcd *hcd = dev_get_drvdata(dev);
1067 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1068
1069 dev_dbg(dev, "EHCI runtime resume\n");
1070 return msm_ehci_resume(mhcd);
1071}
1072#endif
1073
1074#ifdef CONFIG_PM
1075static const struct dev_pm_ops ehci_msm2_dev_pm_ops = {
1076 SET_SYSTEM_SLEEP_PM_OPS(ehci_msm2_pm_suspend, ehci_msm2_pm_resume)
1077 SET_RUNTIME_PM_OPS(ehci_msm2_runtime_suspend, ehci_msm2_runtime_resume,
1078 ehci_msm2_runtime_idle)
1079};
1080#endif
1081
1082static struct platform_driver ehci_msm2_driver = {
1083 .probe = ehci_msm2_probe,
1084 .remove = __devexit_p(ehci_msm2_remove),
1085 .driver = {
1086 .name = "msm_ehci_host",
1087#ifdef CONFIG_PM
1088 .pm = &ehci_msm2_dev_pm_ops,
1089#endif
1090 },
1091};