blob: faa562510f857a663eb96bac6d1d0d4389e51a11 [file] [log] [blame]
Manu Gautam91223e02011-11-08 15:27:22 +05301/* ehci-msm2.c - HSUSB Host Controller Driver Implementation
2 *
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05303 * Copyright (c) 2008-2013, The Linux Foundation. All rights reserved.
Manu Gautam91223e02011-11-08 15:27:22 +05304 *
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>
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053035#include <linux/of.h>
Manu Gautam91223e02011-11-08 15:27:22 +053036#include <mach/clk.h>
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070037#include <mach/msm_xo.h>
Manu Gautam91223e02011-11-08 15:27:22 +053038#include <mach/msm_iomap.h>
39
40#define MSM_USB_BASE (hcd->regs)
41
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070042#define PDEV_NAME_LEN 20
43
Manu Gautam91223e02011-11-08 15:27:22 +053044struct msm_hcd {
45 struct ehci_hcd ehci;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053046 spinlock_t wakeup_lock;
Manu Gautam91223e02011-11-08 15:27:22 +053047 struct device *dev;
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +053048 struct clk *xo_clk;
Manu Gautam91223e02011-11-08 15:27:22 +053049 struct clk *iface_clk;
50 struct clk *core_clk;
51 struct clk *alt_core_clk;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053052 struct clk *phy_sleep_clk;
Manu Gautam91223e02011-11-08 15:27:22 +053053 struct regulator *hsusb_vddcx;
54 struct regulator *hsusb_3p3;
55 struct regulator *hsusb_1p8;
56 struct regulator *vbus;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -070057 struct msm_xo_voter *xo_handle;
Manu Gautam91223e02011-11-08 15:27:22 +053058 bool async_int;
Hemant Kumar56925352012-02-13 16:59:52 -080059 bool vbus_on;
Manu Gautam91223e02011-11-08 15:27:22 +053060 atomic_t in_lpm;
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +053061 int pmic_gpio_dp_irq;
62 bool pmic_gpio_dp_irq_enabled;
63 uint32_t pmic_gpio_int_cnt;
64 atomic_t pm_usage_cnt;
Manu Gautam91223e02011-11-08 15:27:22 +053065 struct wake_lock wlock;
Chiranjeevi Velempatice169582012-08-25 14:15:41 +053066 struct work_struct phy_susp_fail_work;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +053067 int async_irq;
68 bool async_irq_enabled;
69 uint32_t async_int_cnt;
Manu Gautam91223e02011-11-08 15:27:22 +053070};
71
72static inline struct msm_hcd *hcd_to_mhcd(struct usb_hcd *hcd)
73{
74 return (struct msm_hcd *) (hcd->hcd_priv);
75}
76
77static inline struct usb_hcd *mhcd_to_hcd(struct msm_hcd *mhcd)
78{
79 return container_of((void *) mhcd, struct usb_hcd, hcd_priv);
80}
81
82#define HSUSB_PHY_3P3_VOL_MIN 3050000 /* uV */
83#define HSUSB_PHY_3P3_VOL_MAX 3300000 /* uV */
84#define HSUSB_PHY_3P3_HPM_LOAD 50000 /* uA */
85
86#define HSUSB_PHY_1P8_VOL_MIN 1800000 /* uV */
87#define HSUSB_PHY_1P8_VOL_MAX 1800000 /* uV */
88#define HSUSB_PHY_1P8_HPM_LOAD 50000 /* uA */
89
90#define HSUSB_PHY_VDD_DIG_VOL_MIN 1045000 /* uV */
91#define HSUSB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
92#define HSUSB_PHY_VDD_DIG_LOAD 49360 /* uA */
93
94static int msm_ehci_init_vddcx(struct msm_hcd *mhcd, int init)
95{
96 int ret = 0;
97
98 if (!init)
99 goto disable_reg;
100
Mayank Rana2f416c22012-03-24 05:23:25 +0530101 mhcd->hsusb_vddcx = devm_regulator_get(mhcd->dev, "HSUSB_VDDCX");
Manu Gautam91223e02011-11-08 15:27:22 +0530102 if (IS_ERR(mhcd->hsusb_vddcx)) {
103 dev_err(mhcd->dev, "unable to get ehci vddcx\n");
104 return PTR_ERR(mhcd->hsusb_vddcx);
105 }
106
107 ret = regulator_set_voltage(mhcd->hsusb_vddcx,
108 HSUSB_PHY_VDD_DIG_VOL_MIN,
109 HSUSB_PHY_VDD_DIG_VOL_MAX);
110 if (ret) {
111 dev_err(mhcd->dev, "unable to set the voltage"
112 "for ehci vddcx\n");
Mayank Rana2f416c22012-03-24 05:23:25 +0530113 return ret;
Manu Gautam91223e02011-11-08 15:27:22 +0530114 }
115
116 ret = regulator_set_optimum_mode(mhcd->hsusb_vddcx,
117 HSUSB_PHY_VDD_DIG_LOAD);
118 if (ret < 0) {
119 dev_err(mhcd->dev, "%s: Unable to set optimum mode of the"
120 " regulator: VDDCX\n", __func__);
121 goto reg_optimum_mode_err;
122 }
123
124 ret = regulator_enable(mhcd->hsusb_vddcx);
125 if (ret) {
126 dev_err(mhcd->dev, "unable to enable ehci vddcx\n");
127 goto reg_enable_err;
128 }
129
130 return 0;
131
132disable_reg:
133 regulator_disable(mhcd->hsusb_vddcx);
134reg_enable_err:
135 regulator_set_optimum_mode(mhcd->hsusb_vddcx, 0);
136reg_optimum_mode_err:
137 regulator_set_voltage(mhcd->hsusb_vddcx, 0,
138 HSUSB_PHY_VDD_DIG_VOL_MIN);
Manu Gautam91223e02011-11-08 15:27:22 +0530139 return ret;
140
141}
142
143static int msm_ehci_ldo_init(struct msm_hcd *mhcd, int init)
144{
145 int rc = 0;
146
147 if (!init)
148 goto put_1p8;
149
Mayank Rana2f416c22012-03-24 05:23:25 +0530150 mhcd->hsusb_3p3 = devm_regulator_get(mhcd->dev, "HSUSB_3p3");
Manu Gautam91223e02011-11-08 15:27:22 +0530151 if (IS_ERR(mhcd->hsusb_3p3)) {
152 dev_err(mhcd->dev, "unable to get hsusb 3p3\n");
153 return PTR_ERR(mhcd->hsusb_3p3);
154 }
155
156 rc = regulator_set_voltage(mhcd->hsusb_3p3,
157 HSUSB_PHY_3P3_VOL_MIN, HSUSB_PHY_3P3_VOL_MAX);
158 if (rc) {
159 dev_err(mhcd->dev, "unable to set voltage level for"
160 "hsusb 3p3\n");
Mayank Rana2f416c22012-03-24 05:23:25 +0530161 return rc;
Manu Gautam91223e02011-11-08 15:27:22 +0530162 }
Mayank Rana2f416c22012-03-24 05:23:25 +0530163 mhcd->hsusb_1p8 = devm_regulator_get(mhcd->dev, "HSUSB_1p8");
Manu Gautam91223e02011-11-08 15:27:22 +0530164 if (IS_ERR(mhcd->hsusb_1p8)) {
165 dev_err(mhcd->dev, "unable to get hsusb 1p8\n");
166 rc = PTR_ERR(mhcd->hsusb_1p8);
167 goto put_3p3_lpm;
168 }
169 rc = regulator_set_voltage(mhcd->hsusb_1p8,
170 HSUSB_PHY_1P8_VOL_MIN, HSUSB_PHY_1P8_VOL_MAX);
171 if (rc) {
172 dev_err(mhcd->dev, "unable to set voltage level for"
173 "hsusb 1p8\n");
174 goto put_1p8;
175 }
176
177 return 0;
178
179put_1p8:
180 regulator_set_voltage(mhcd->hsusb_1p8, 0, HSUSB_PHY_1P8_VOL_MAX);
Manu Gautam91223e02011-11-08 15:27:22 +0530181put_3p3_lpm:
182 regulator_set_voltage(mhcd->hsusb_3p3, 0, HSUSB_PHY_3P3_VOL_MAX);
Manu Gautam91223e02011-11-08 15:27:22 +0530183
184 return rc;
185}
186
187#ifdef CONFIG_PM_SLEEP
Hemant Kumar441356be2012-02-13 16:12:49 -0800188#define HSUSB_PHY_SUSP_DIG_VOL_P50 500000
189#define HSUSB_PHY_SUSP_DIG_VOL_P75 750000
Manu Gautam91223e02011-11-08 15:27:22 +0530190static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
191{
Hemant Kumar441356be2012-02-13 16:12:49 -0800192 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530193 int max_vol = HSUSB_PHY_VDD_DIG_VOL_MAX;
194 int min_vol;
195 int ret;
196
Hemant Kumar441356be2012-02-13 16:12:49 -0800197 pdata = mhcd->dev->platform_data;
198
Manu Gautam91223e02011-11-08 15:27:22 +0530199 if (high)
200 min_vol = HSUSB_PHY_VDD_DIG_VOL_MIN;
Hemant Kumar441356be2012-02-13 16:12:49 -0800201 else if (pdata && pdata->dock_connect_irq &&
202 !irq_read_line(pdata->dock_connect_irq))
203 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P75;
Manu Gautam91223e02011-11-08 15:27:22 +0530204 else
Hemant Kumar441356be2012-02-13 16:12:49 -0800205 min_vol = HSUSB_PHY_SUSP_DIG_VOL_P50;
Manu Gautam91223e02011-11-08 15:27:22 +0530206
207 ret = regulator_set_voltage(mhcd->hsusb_vddcx, min_vol, max_vol);
208 if (ret) {
209 dev_err(mhcd->dev, "%s: unable to set the voltage of regulator"
210 " HSUSB_VDDCX\n", __func__);
211 return ret;
212 }
213
214 dev_dbg(mhcd->dev, "%s: min_vol:%d max_vol:%d\n", __func__, min_vol,
215 max_vol);
216
217 return ret;
218}
219#else
220static int msm_ehci_config_vddcx(struct msm_hcd *mhcd, int high)
221{
222 return 0;
223}
224#endif
225
Manu Gautam91223e02011-11-08 15:27:22 +0530226static void msm_ehci_vbus_power(struct msm_hcd *mhcd, bool on)
227{
228 int ret;
229
230 if (!mhcd->vbus) {
231 pr_err("vbus is NULL.");
232 return;
233 }
Hemant Kumar56925352012-02-13 16:59:52 -0800234
235 if (mhcd->vbus_on == on)
236 return;
237
Manu Gautam91223e02011-11-08 15:27:22 +0530238 if (on) {
239 ret = regulator_enable(mhcd->vbus);
240 if (ret) {
241 pr_err("unable to enable vbus\n");
242 return;
243 }
Hemant Kumar56925352012-02-13 16:59:52 -0800244 mhcd->vbus_on = true;
Manu Gautam91223e02011-11-08 15:27:22 +0530245 } else {
246 ret = regulator_disable(mhcd->vbus);
247 if (ret) {
248 pr_err("unable to disable vbus\n");
249 return;
250 }
Hemant Kumar56925352012-02-13 16:59:52 -0800251 mhcd->vbus_on = false;
Manu Gautam91223e02011-11-08 15:27:22 +0530252 }
253}
254
Hemant Kumar56925352012-02-13 16:59:52 -0800255static irqreturn_t msm_ehci_dock_connect_irq(int irq, void *data)
256{
257 const struct msm_usb_host_platform_data *pdata;
258 struct msm_hcd *mhcd = data;
259 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
260
261 pdata = mhcd->dev->platform_data;
262
263 if (atomic_read(&mhcd->in_lpm))
264 usb_hcd_resume_root_hub(hcd);
265
266 if (irq_read_line(pdata->dock_connect_irq)) {
267 dev_dbg(mhcd->dev, "%s:Dock removed disable vbus\n", __func__);
268 msm_ehci_vbus_power(mhcd, 0);
269 } else {
270 dev_dbg(mhcd->dev, "%s:Dock connected enable vbus\n", __func__);
271 msm_ehci_vbus_power(mhcd, 1);
272 }
273
274 return IRQ_HANDLED;
275}
276
277static int msm_ehci_init_vbus(struct msm_hcd *mhcd, int init)
278{
279 int rc = 0;
280 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
281 const struct msm_usb_host_platform_data *pdata;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530282 int ret = 0;
Hemant Kumar56925352012-02-13 16:59:52 -0800283
284 pdata = mhcd->dev->platform_data;
285
286 if (!init) {
Hemant Kumar56925352012-02-13 16:59:52 -0800287 if (pdata && pdata->dock_connect_irq)
288 free_irq(pdata->dock_connect_irq, mhcd);
289 return rc;
290 }
291
Mayank Rana2f416c22012-03-24 05:23:25 +0530292 mhcd->vbus = devm_regulator_get(mhcd->dev, "vbus");
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530293 ret = PTR_ERR(mhcd->vbus);
294 if (ret == -EPROBE_DEFER) {
295 pr_debug("failed to get vbus handle, defer probe\n");
296 return ret;
297 } else if (IS_ERR(mhcd->vbus)) {
Hemant Kumar56925352012-02-13 16:59:52 -0800298 pr_err("Unable to get vbus\n");
299 return -ENODEV;
300 }
301
302 if (pdata) {
303 hcd->power_budget = pdata->power_budget;
304
305 if (pdata->dock_connect_irq) {
306 rc = request_threaded_irq(pdata->dock_connect_irq, NULL,
307 msm_ehci_dock_connect_irq,
308 IRQF_TRIGGER_FALLING |
309 IRQF_TRIGGER_RISING |
310 IRQF_ONESHOT, "msm_ehci_host", mhcd);
311 if (!rc)
312 enable_irq_wake(pdata->dock_connect_irq);
313 }
314 }
315 return rc;
316}
317
Manu Gautam91223e02011-11-08 15:27:22 +0530318static int msm_ehci_ldo_enable(struct msm_hcd *mhcd, int on)
319{
320 int ret = 0;
321
322 if (IS_ERR(mhcd->hsusb_1p8)) {
323 dev_err(mhcd->dev, "%s: HSUSB_1p8 is not initialized\n",
324 __func__);
325 return -ENODEV;
326 }
327
328 if (IS_ERR(mhcd->hsusb_3p3)) {
329 dev_err(mhcd->dev, "%s: HSUSB_3p3 is not initialized\n",
330 __func__);
331 return -ENODEV;
332 }
333
334 if (on) {
335 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8,
336 HSUSB_PHY_1P8_HPM_LOAD);
337 if (ret < 0) {
338 dev_err(mhcd->dev, "%s: Unable to set HPM of the"
339 " regulator: HSUSB_1p8\n", __func__);
340 return ret;
341 }
342
343 ret = regulator_enable(mhcd->hsusb_1p8);
344 if (ret) {
345 dev_err(mhcd->dev, "%s: unable to enable the hsusb"
346 " 1p8\n", __func__);
347 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
348 return ret;
349 }
350
351 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3,
352 HSUSB_PHY_3P3_HPM_LOAD);
353 if (ret < 0) {
354 dev_err(mhcd->dev, "%s: Unable to set HPM of the "
355 "regulator: HSUSB_3p3\n", __func__);
356 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
357 regulator_disable(mhcd->hsusb_1p8);
358 return ret;
359 }
360
361 ret = regulator_enable(mhcd->hsusb_3p3);
362 if (ret) {
363 dev_err(mhcd->dev, "%s: unable to enable the "
364 "hsusb 3p3\n", __func__);
365 regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
366 regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
367 regulator_disable(mhcd->hsusb_1p8);
368 return ret;
369 }
370
371 } else {
372 ret = regulator_disable(mhcd->hsusb_1p8);
373 if (ret) {
374 dev_err(mhcd->dev, "%s: unable to disable the "
375 "hsusb 1p8\n", __func__);
376 return ret;
377 }
378
379 ret = regulator_set_optimum_mode(mhcd->hsusb_1p8, 0);
380 if (ret < 0)
381 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
382 "regulator: HSUSB_1p8\n", __func__);
383
384 ret = regulator_disable(mhcd->hsusb_3p3);
385 if (ret) {
386 dev_err(mhcd->dev, "%s: unable to disable the "
387 "hsusb 3p3\n", __func__);
388 return ret;
389 }
390 ret = regulator_set_optimum_mode(mhcd->hsusb_3p3, 0);
391 if (ret < 0)
392 dev_err(mhcd->dev, "%s: Unable to set LPM of the "
393 "regulator: HSUSB_3p3\n", __func__);
394 }
395
396 dev_dbg(mhcd->dev, "reg (%s)\n", on ? "HPM" : "LPM");
397
398 return ret < 0 ? ret : 0;
399}
400
401
402#define ULPI_IO_TIMEOUT_USECS (10 * 1000)
403static int msm_ulpi_read(struct msm_hcd *mhcd, u32 reg)
404{
405 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
406 unsigned long timeout;
407
408 /* initiate read operation */
409 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
410 USB_ULPI_VIEWPORT);
411
412 /* wait for completion */
413 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
414 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
415 if (time_after(jiffies, timeout)) {
416 dev_err(mhcd->dev, "msm_ulpi_read: timeout %08x\n",
417 readl_relaxed(USB_ULPI_VIEWPORT));
418 return -ETIMEDOUT;
419 }
420 udelay(1);
421 }
422
423 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
424}
425
426
427static int msm_ulpi_write(struct msm_hcd *mhcd, u32 val, u32 reg)
428{
429 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
430 unsigned long timeout;
431
432 /* initiate write operation */
433 writel_relaxed(ULPI_RUN | ULPI_WRITE |
434 ULPI_ADDR(reg) | ULPI_DATA(val),
435 USB_ULPI_VIEWPORT);
436
437 /* wait for completion */
438 timeout = jiffies + usecs_to_jiffies(ULPI_IO_TIMEOUT_USECS);
439 while (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN) {
440 if (time_after(jiffies, timeout)) {
441 dev_err(mhcd->dev, "msm_ulpi_write: timeout\n");
442 return -ETIMEDOUT;
443 }
444 udelay(1);
445 }
446
447 return 0;
448}
449
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530450/**
451 * Do hard reset to USB hardware block using one of reset methodology based
452 * on availablity of alt_core_clk. There are two kinds of hardware resets.
453 * 1. Conventional synchronous reset where clocks to blocks to be ON while
454 * issuing the reset. 2. Asynchronous reset which requires clocks to be OFF.
455 */
Manu Gautam91223e02011-11-08 15:27:22 +0530456static int msm_ehci_link_clk_reset(struct msm_hcd *mhcd, bool assert)
457{
458 int ret;
459
460 if (assert) {
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530461 if (!IS_ERR(mhcd->alt_core_clk)) {
462 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_ASSERT);
463 } else {
464 /* Using asynchronous block reset to the hardware */
465 clk_disable(mhcd->iface_clk);
466 clk_disable(mhcd->core_clk);
467 ret = clk_reset(mhcd->core_clk, CLK_RESET_ASSERT);
468 }
Manu Gautam91223e02011-11-08 15:27:22 +0530469 if (ret)
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530470 dev_err(mhcd->dev, "usb clk assert failed\n");
Manu Gautam91223e02011-11-08 15:27:22 +0530471 } else {
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530472 if (!IS_ERR(mhcd->alt_core_clk)) {
473 ret = clk_reset(mhcd->alt_core_clk, CLK_RESET_DEASSERT);
474 } else {
475 ret = clk_reset(mhcd->core_clk, CLK_RESET_DEASSERT);
476 ndelay(200);
477 clk_enable(mhcd->core_clk);
478 clk_enable(mhcd->iface_clk);
479 }
Manu Gautam91223e02011-11-08 15:27:22 +0530480 if (ret)
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530481 dev_err(mhcd->dev, "usb clk deassert failed\n");
Manu Gautam91223e02011-11-08 15:27:22 +0530482 }
483
484 return ret;
485}
486
487static int msm_ehci_phy_reset(struct msm_hcd *mhcd)
488{
489 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530490 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530491 u32 val;
492 int ret;
493 int retries;
494
495 ret = msm_ehci_link_clk_reset(mhcd, 1);
496 if (ret)
497 return ret;
498
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530499 usleep_range(10, 12);
Manu Gautam91223e02011-11-08 15:27:22 +0530500
501 ret = msm_ehci_link_clk_reset(mhcd, 0);
502 if (ret)
503 return ret;
504
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530505 pdata = mhcd->dev->platform_data;
506 if (pdata && pdata->use_sec_phy)
507 /* select secondary phy if offset is set for USB operation */
508 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
509 USB_PHY_CTRL2);
Manu Gautam91223e02011-11-08 15:27:22 +0530510 val = readl_relaxed(USB_PORTSC) & ~PORTSC_PTS_MASK;
511 writel_relaxed(val | PORTSC_PTS_ULPI, USB_PORTSC);
512
513 for (retries = 3; retries > 0; retries--) {
514 ret = msm_ulpi_write(mhcd, ULPI_FUNC_CTRL_SUSPENDM,
515 ULPI_CLR(ULPI_FUNC_CTRL));
516 if (!ret)
517 break;
518 }
519 if (!retries)
520 return -ETIMEDOUT;
521
522 /* Wakeup the PHY with a reg-access for calibration */
523 for (retries = 3; retries > 0; retries--) {
524 ret = msm_ulpi_read(mhcd, ULPI_DEBUG);
525 if (ret != -ETIMEDOUT)
526 break;
527 }
528 if (!retries)
529 return -ETIMEDOUT;
530
531 dev_info(mhcd->dev, "phy_reset: success\n");
532
533 return 0;
534}
535
536#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
537static int msm_hsusb_reset(struct msm_hcd *mhcd)
538{
539 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530540 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530541 unsigned long timeout;
542 int ret;
543
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530544 if (!IS_ERR(mhcd->alt_core_clk))
545 clk_prepare_enable(mhcd->alt_core_clk);
546
Manu Gautam91223e02011-11-08 15:27:22 +0530547 ret = msm_ehci_phy_reset(mhcd);
548 if (ret) {
549 dev_err(mhcd->dev, "phy_reset failed\n");
550 return ret;
551 }
552
553 writel_relaxed(USBCMD_RESET, USB_USBCMD);
554
555 timeout = jiffies + usecs_to_jiffies(LINK_RESET_TIMEOUT_USEC);
556 while (readl_relaxed(USB_USBCMD) & USBCMD_RESET) {
557 if (time_after(jiffies, timeout))
558 return -ETIMEDOUT;
559 udelay(1);
560 }
561
562 /* select ULPI phy */
563 writel_relaxed(0x80000000, USB_PORTSC);
564
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530565 pdata = mhcd->dev->platform_data;
566 if (pdata && pdata->use_sec_phy)
567 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
568 USB_PHY_CTRL2);
569
Manu Gautam91223e02011-11-08 15:27:22 +0530570 msleep(100);
571
572 writel_relaxed(0x0, USB_AHBBURST);
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530573 writel_relaxed(0x08, USB_AHBMODE);
Manu Gautam91223e02011-11-08 15:27:22 +0530574
575 /* Ensure that RESET operation is completed before turning off clock */
576 mb();
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530577
578 if (!IS_ERR(mhcd->alt_core_clk))
579 clk_disable_unprepare(mhcd->alt_core_clk);
Manu Gautam91223e02011-11-08 15:27:22 +0530580
581 /*rising edge interrupts with Dp rise and fall enabled*/
582 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_RISE);
583 msm_ulpi_write(mhcd, ULPI_INT_DP, ULPI_USB_INT_EN_FALL);
584
585 /*Clear the PHY interrupts by reading the PHY interrupt latch register*/
586 msm_ulpi_read(mhcd, ULPI_USB_INT_LATCH);
587
588 return 0;
589}
590
Chiranjeevi Velempatice169582012-08-25 14:15:41 +0530591static void msm_ehci_phy_susp_fail_work(struct work_struct *w)
592{
593 struct msm_hcd *mhcd = container_of(w, struct msm_hcd,
594 phy_susp_fail_work);
595 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
596
597 msm_ehci_vbus_power(mhcd, 0);
598 usb_remove_hcd(hcd);
599 msm_hsusb_reset(mhcd);
600 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
601 msm_ehci_vbus_power(mhcd, 1);
602}
603
Manu Gautam91223e02011-11-08 15:27:22 +0530604#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
605#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
606
607#ifdef CONFIG_PM_SLEEP
608static int msm_ehci_suspend(struct msm_hcd *mhcd)
609{
610 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
611 unsigned long timeout;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700612 int ret;
Manu Gautam91223e02011-11-08 15:27:22 +0530613 u32 portsc;
614
615 if (atomic_read(&mhcd->in_lpm)) {
616 dev_dbg(mhcd->dev, "%s called in lpm\n", __func__);
617 return 0;
618 }
619
620 disable_irq(hcd->irq);
621
622 /* Set the PHCD bit, only if it is not set by the controller.
623 * PHY may take some time or even fail to enter into low power
624 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
625 * in failure case.
626 */
627 portsc = readl_relaxed(USB_PORTSC);
628 if (!(portsc & PORTSC_PHCD)) {
629 writel_relaxed(portsc | PORTSC_PHCD,
630 USB_PORTSC);
631
632 timeout = jiffies + usecs_to_jiffies(PHY_SUSPEND_TIMEOUT_USEC);
633 while (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD)) {
634 if (time_after(jiffies, timeout)) {
635 dev_err(mhcd->dev, "Unable to suspend PHY\n");
Chiranjeevi Velempatice169582012-08-25 14:15:41 +0530636 schedule_work(&mhcd->phy_susp_fail_work);
637 return -ETIMEDOUT;
Manu Gautam91223e02011-11-08 15:27:22 +0530638 }
639 udelay(1);
640 }
641 }
642
643 /*
644 * PHY has capability to generate interrupt asynchronously in low
645 * power mode (LPM). This interrupt is level triggered. So USB IRQ
646 * line must be disabled till async interrupt enable bit is cleared
647 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
648 * block data communication from PHY.
649 */
650 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
651 ULPI_STP_CTRL, USB_USBCMD);
652
653 /*
654 * Ensure that hardware is put in low power mode before
655 * clocks are turned OFF and VDD is allowed to minimize.
656 */
657 mb();
658
659 clk_disable_unprepare(mhcd->iface_clk);
660 clk_disable_unprepare(mhcd->core_clk);
661
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700662 /* usb phy does not require TCXO clock, hence vote for TCXO disable */
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +0530663 if (!IS_ERR(mhcd->xo_clk)) {
664 clk_disable_unprepare(mhcd->xo_clk);
665 } else {
666 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
667 if (ret)
668 dev_err(mhcd->dev, "%s failed to devote for TCXO %d\n",
669 __func__, ret);
670 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700671
Manu Gautam91223e02011-11-08 15:27:22 +0530672 msm_ehci_config_vddcx(mhcd, 0);
673
674 atomic_set(&mhcd->in_lpm, 1);
675 enable_irq(hcd->irq);
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530676 if (mhcd->pmic_gpio_dp_irq) {
677 mhcd->pmic_gpio_dp_irq_enabled = 1;
678 enable_irq_wake(mhcd->pmic_gpio_dp_irq);
679 enable_irq(mhcd->pmic_gpio_dp_irq);
680 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530681 if (mhcd->async_irq) {
682 mhcd->async_irq_enabled = 1;
683 enable_irq_wake(mhcd->async_irq);
684 enable_irq(mhcd->async_irq);
685 }
Manu Gautam91223e02011-11-08 15:27:22 +0530686 wake_unlock(&mhcd->wlock);
687
688 dev_info(mhcd->dev, "EHCI USB in low power mode\n");
689
690 return 0;
691}
692
693static int msm_ehci_resume(struct msm_hcd *mhcd)
694{
695 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
696 unsigned long timeout;
697 unsigned temp;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700698 int ret;
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530699 unsigned long flags;
Manu Gautam91223e02011-11-08 15:27:22 +0530700
701 if (!atomic_read(&mhcd->in_lpm)) {
702 dev_dbg(mhcd->dev, "%s called in !in_lpm\n", __func__);
703 return 0;
704 }
705
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530706 if (mhcd->pmic_gpio_dp_irq_enabled) {
707 disable_irq_wake(mhcd->pmic_gpio_dp_irq);
708 disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
709 mhcd->pmic_gpio_dp_irq_enabled = 0;
710 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530711 spin_lock_irqsave(&mhcd->wakeup_lock, flags);
712 if (mhcd->async_irq_enabled) {
713 disable_irq_wake(mhcd->async_irq);
714 disable_irq_nosync(mhcd->async_irq);
715 mhcd->async_irq_enabled = 0;
716 }
717 spin_unlock_irqrestore(&mhcd->wakeup_lock, flags);
718
Manu Gautam91223e02011-11-08 15:27:22 +0530719 wake_lock(&mhcd->wlock);
720
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700721 /* Vote for TCXO when waking up the phy */
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +0530722 if (!IS_ERR(mhcd->xo_clk)) {
723 clk_prepare_enable(mhcd->xo_clk);
724 } else {
725 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
726 if (ret)
727 dev_err(mhcd->dev, "%s failed to vote for TCXO D0 %d\n",
728 __func__, ret);
729 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -0700730
Manu Gautam91223e02011-11-08 15:27:22 +0530731 clk_prepare_enable(mhcd->core_clk);
732 clk_prepare_enable(mhcd->iface_clk);
733
734 msm_ehci_config_vddcx(mhcd, 1);
735
736 temp = readl_relaxed(USB_USBCMD);
737 temp &= ~ASYNC_INTR_CTRL;
738 temp &= ~ULPI_STP_CTRL;
739 writel_relaxed(temp, USB_USBCMD);
740
741 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
742 goto skip_phy_resume;
743
744 temp = readl_relaxed(USB_PORTSC) & ~PORTSC_PHCD;
745 writel_relaxed(temp, USB_PORTSC);
746
747 timeout = jiffies + usecs_to_jiffies(PHY_RESUME_TIMEOUT_USEC);
748 while ((readl_relaxed(USB_PORTSC) & PORTSC_PHCD) ||
749 !(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE)) {
750 if (time_after(jiffies, timeout)) {
751 /*This is a fatal error. Reset the link and PHY*/
752 dev_err(mhcd->dev, "Unable to resume USB. Resetting the h/w\n");
753 msm_hsusb_reset(mhcd);
754 break;
755 }
756 udelay(1);
757 }
758
759skip_phy_resume:
760
Chiranjeevi Velempati35d46ab2012-07-18 20:36:53 +0530761 usb_hcd_resume_root_hub(hcd);
Manu Gautam91223e02011-11-08 15:27:22 +0530762 atomic_set(&mhcd->in_lpm, 0);
763
764 if (mhcd->async_int) {
765 mhcd->async_int = false;
766 pm_runtime_put_noidle(mhcd->dev);
767 enable_irq(hcd->irq);
768 }
769
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530770 if (atomic_read(&mhcd->pm_usage_cnt)) {
771 atomic_set(&mhcd->pm_usage_cnt, 0);
772 pm_runtime_put_noidle(mhcd->dev);
773 }
774
Manu Gautam91223e02011-11-08 15:27:22 +0530775 dev_info(mhcd->dev, "EHCI USB exited from low power mode\n");
776
777 return 0;
778}
779#endif
780
781static irqreturn_t msm_ehci_irq(struct usb_hcd *hcd)
782{
783 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
784
785 if (atomic_read(&mhcd->in_lpm)) {
786 disable_irq_nosync(hcd->irq);
787 mhcd->async_int = true;
788 pm_runtime_get(mhcd->dev);
789 return IRQ_HANDLED;
790 }
791
792 return ehci_irq(hcd);
793}
794
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530795static irqreturn_t msm_async_irq(int irq, void *data)
796{
797 struct msm_hcd *mhcd = data;
798 int ret;
799
800 mhcd->async_int_cnt++;
801 dev_dbg(mhcd->dev, "%s: hsusb host remote wakeup interrupt cnt: %u\n",
802 __func__, mhcd->async_int_cnt);
803
804 wake_lock(&mhcd->wlock);
805
806 spin_lock(&mhcd->wakeup_lock);
807 if (mhcd->async_irq_enabled) {
808 mhcd->async_irq_enabled = 0;
809 disable_irq_wake(irq);
810 disable_irq_nosync(irq);
811 }
812 spin_unlock(&mhcd->wakeup_lock);
813
814 if (!atomic_read(&mhcd->pm_usage_cnt)) {
815 ret = pm_runtime_get(mhcd->dev);
816 if ((ret == 1) || (ret == -EINPROGRESS))
817 pm_runtime_put_noidle(mhcd->dev);
818 else
819 atomic_set(&mhcd->pm_usage_cnt, 1);
820 }
821
822 return IRQ_HANDLED;
823}
824
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +0530825static irqreturn_t msm_ehci_host_wakeup_irq(int irq, void *data)
826{
827
828 struct msm_hcd *mhcd = data;
829
830 mhcd->pmic_gpio_int_cnt++;
831 dev_dbg(mhcd->dev, "%s: hsusb host remote wakeup interrupt cnt: %u\n",
832 __func__, mhcd->pmic_gpio_int_cnt);
833
834
835 wake_lock(&mhcd->wlock);
836
837 if (mhcd->pmic_gpio_dp_irq_enabled) {
838 mhcd->pmic_gpio_dp_irq_enabled = 0;
839 disable_irq_wake(irq);
840 disable_irq_nosync(irq);
841 }
842
843 if (!atomic_read(&mhcd->pm_usage_cnt)) {
844 atomic_set(&mhcd->pm_usage_cnt, 1);
845 pm_runtime_get(mhcd->dev);
846 }
847
848 return IRQ_HANDLED;
849}
850
Manu Gautam91223e02011-11-08 15:27:22 +0530851static int msm_ehci_reset(struct usb_hcd *hcd)
852{
853 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530854 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
855 struct msm_usb_host_platform_data *pdata;
Manu Gautam91223e02011-11-08 15:27:22 +0530856 int retval;
857
858 ehci->caps = USB_CAPLENGTH;
859 ehci->regs = USB_CAPLENGTH +
860 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
861 dbg_hcs_params(ehci, "reset");
862 dbg_hcc_params(ehci, "reset");
863
864 /* cache the data to minimize the chip reads*/
865 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
866
867 hcd->has_tt = 1;
868 ehci->sbrn = HCD_USB2;
869
870 retval = ehci_halt(ehci);
871 if (retval)
872 return retval;
873
874 /* data structure init */
875 retval = ehci_init(hcd);
876 if (retval)
877 return retval;
878
879 retval = ehci_reset(ehci);
880 if (retval)
881 return retval;
882
883 /* bursts of unspecified length. */
884 writel_relaxed(0, USB_AHBBURST);
885 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530886 writel_relaxed(0x08, USB_AHBMODE);
Manu Gautam91223e02011-11-08 15:27:22 +0530887 /* Disable streaming mode and select host mode */
888 writel_relaxed(0x13, USB_USBMODE);
889
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530890 pdata = mhcd->dev->platform_data;
891 if (pdata && pdata->use_sec_phy)
892 writel_relaxed(readl_relaxed(USB_PHY_CTRL2) | (1<<16),
893 USB_PHY_CTRL2);
894
Manu Gautam91223e02011-11-08 15:27:22 +0530895 ehci_port_power(ehci, 1);
896 return 0;
897}
898
899static struct hc_driver msm_hc2_driver = {
900 .description = hcd_name,
901 .product_desc = "Qualcomm EHCI Host Controller",
902 .hcd_priv_size = sizeof(struct msm_hcd),
903
904 /*
905 * generic hardware linkage
906 */
907 .irq = msm_ehci_irq,
908 .flags = HCD_USB2 | HCD_MEMORY,
909
910 .reset = msm_ehci_reset,
911 .start = ehci_run,
912
913 .stop = ehci_stop,
914 .shutdown = ehci_shutdown,
915
916 /*
917 * managing i/o requests and associated device resources
918 */
919 .urb_enqueue = ehci_urb_enqueue,
920 .urb_dequeue = ehci_urb_dequeue,
921 .endpoint_disable = ehci_endpoint_disable,
922 .endpoint_reset = ehci_endpoint_reset,
923 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
924
925 /*
926 * scheduling support
927 */
928 .get_frame_number = ehci_get_frame,
929
930 /*
931 * root hub support
932 */
933 .hub_status_data = ehci_hub_status_data,
934 .hub_control = ehci_hub_control,
935 .relinquish_port = ehci_relinquish_port,
936 .port_handed_over = ehci_port_handed_over,
937
938 /*
939 * PM support
940 */
941 .bus_suspend = ehci_bus_suspend,
942 .bus_resume = ehci_bus_resume,
943};
944
945static int msm_ehci_init_clocks(struct msm_hcd *mhcd, u32 init)
946{
947 int ret = 0;
948
949 if (!init)
950 goto put_clocks;
951
952 /* 60MHz alt_core_clk is for LINK to be used during PHY RESET */
953 mhcd->alt_core_clk = clk_get(mhcd->dev, "alt_core_clk");
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530954 if (IS_ERR(mhcd->alt_core_clk))
955 dev_dbg(mhcd->dev, "failed to get alt_core_clk\n");
956 else
957 clk_set_rate(mhcd->alt_core_clk, 60000000);
Manu Gautam91223e02011-11-08 15:27:22 +0530958
959 /* iface_clk is required for data transfers */
960 mhcd->iface_clk = clk_get(mhcd->dev, "iface_clk");
961 if (IS_ERR(mhcd->iface_clk)) {
962 dev_err(mhcd->dev, "failed to get iface_clk\n");
963 ret = PTR_ERR(mhcd->iface_clk);
964 goto put_alt_core_clk;
965 }
966
967 /* Link's protocol engine is based on pclk which must
968 * be running >55Mhz and frequency should also not change.
969 * Hence, vote for maximum clk frequency on its source
970 */
971 mhcd->core_clk = clk_get(mhcd->dev, "core_clk");
972 if (IS_ERR(mhcd->core_clk)) {
973 dev_err(mhcd->dev, "failed to get core_clk\n");
974 ret = PTR_ERR(mhcd->core_clk);
975 goto put_iface_clk;
976 }
977 clk_set_rate(mhcd->core_clk, INT_MAX);
978
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530979 mhcd->phy_sleep_clk = clk_get(mhcd->dev, "sleep_clk");
980 if (IS_ERR(mhcd->phy_sleep_clk))
981 dev_dbg(mhcd->dev, "failed to get sleep_clk\n");
982 else
983 clk_prepare_enable(mhcd->phy_sleep_clk);
984
Manu Gautam91223e02011-11-08 15:27:22 +0530985 clk_prepare_enable(mhcd->core_clk);
986 clk_prepare_enable(mhcd->iface_clk);
987
988 return 0;
989
990put_clocks:
Jack Pham35fba312013-01-21 19:19:21 -0800991 if (!atomic_read(&mhcd->in_lpm)) {
992 clk_disable_unprepare(mhcd->iface_clk);
993 clk_disable_unprepare(mhcd->core_clk);
994 }
Manu Gautam91223e02011-11-08 15:27:22 +0530995 clk_put(mhcd->core_clk);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +0530996 if (!IS_ERR(mhcd->phy_sleep_clk)) {
997 clk_disable_unprepare(mhcd->phy_sleep_clk);
998 clk_put(mhcd->phy_sleep_clk);
999 }
Manu Gautam91223e02011-11-08 15:27:22 +05301000put_iface_clk:
1001 clk_put(mhcd->iface_clk);
1002put_alt_core_clk:
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301003 if (!IS_ERR(mhcd->alt_core_clk))
1004 clk_put(mhcd->alt_core_clk);
Manu Gautam91223e02011-11-08 15:27:22 +05301005
1006 return ret;
1007}
1008
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301009struct msm_usb_host_platform_data *ehci_msm2_dt_to_pdata(
1010 struct platform_device *pdev)
1011{
1012 struct device_node *node = pdev->dev.of_node;
1013 struct msm_usb_host_platform_data *pdata;
1014
1015 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1016 if (!pdata) {
1017 dev_err(&pdev->dev, "unable to allocate platform data\n");
1018 return NULL;
1019 }
1020
1021 pdata->use_sec_phy = of_property_read_bool(node,
1022 "qcom,usb2-enable-hsphy2");
1023 of_property_read_u32(node, "qcom,usb2-power-budget",
1024 &pdata->power_budget);
1025 return pdata;
1026}
1027
1028static u64 ehci_msm_dma_mask = DMA_BIT_MASK(64);
Manu Gautam91223e02011-11-08 15:27:22 +05301029static int __devinit ehci_msm2_probe(struct platform_device *pdev)
1030{
1031 struct usb_hcd *hcd;
1032 struct resource *res;
1033 struct msm_hcd *mhcd;
Hemant Kumar56925352012-02-13 16:59:52 -08001034 const struct msm_usb_host_platform_data *pdata;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001035 char pdev_name[PDEV_NAME_LEN];
Manu Gautam91223e02011-11-08 15:27:22 +05301036 int ret;
1037
1038 dev_dbg(&pdev->dev, "ehci_msm2 probe\n");
1039
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301040 if (pdev->dev.of_node) {
1041 dev_dbg(&pdev->dev, "device tree enabled\n");
1042 pdev->dev.platform_data = ehci_msm2_dt_to_pdata(pdev);
1043 }
1044
1045 if (!pdev->dev.platform_data)
1046 dev_dbg(&pdev->dev, "No platform data given\n");
1047
1048 if (!pdev->dev.dma_mask)
1049 pdev->dev.dma_mask = &ehci_msm_dma_mask;
1050 if (!pdev->dev.coherent_dma_mask)
1051 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1052
Manu Gautam91223e02011-11-08 15:27:22 +05301053 hcd = usb_create_hcd(&msm_hc2_driver, &pdev->dev,
1054 dev_name(&pdev->dev));
1055 if (!hcd) {
1056 dev_err(&pdev->dev, "Unable to create HCD\n");
1057 return -ENOMEM;
1058 }
1059
1060 hcd->irq = platform_get_irq(pdev, 0);
1061 if (hcd->irq < 0) {
1062 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1063 ret = hcd->irq;
1064 goto put_hcd;
1065 }
1066
1067 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1068 if (!res) {
1069 dev_err(&pdev->dev, "Unable to get memory resource\n");
1070 ret = -ENODEV;
1071 goto put_hcd;
1072 }
1073
1074 hcd->rsrc_start = res->start;
1075 hcd->rsrc_len = resource_size(res);
1076 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1077 if (!hcd->regs) {
1078 dev_err(&pdev->dev, "ioremap failed\n");
1079 ret = -ENOMEM;
1080 goto put_hcd;
1081 }
1082
1083 mhcd = hcd_to_mhcd(hcd);
1084 mhcd->dev = &pdev->dev;
1085
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301086 spin_lock_init(&mhcd->wakeup_lock);
1087
1088 mhcd->async_irq = platform_get_irq_byname(pdev, "async_irq");
1089 if (mhcd->async_irq < 0) {
1090 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
1091 mhcd->async_irq = 0;
1092 } else {
1093 ret = request_irq(mhcd->async_irq, msm_async_irq,
1094 IRQF_TRIGGER_RISING, "msm_ehci_host", mhcd);
1095 if (ret) {
1096 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
1097 goto unmap;
1098 }
1099 disable_irq(mhcd->async_irq);
1100 }
1101
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001102 snprintf(pdev_name, PDEV_NAME_LEN, "%s.%d", pdev->name, pdev->id);
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301103 mhcd->xo_clk = clk_get(&pdev->dev, "xo");
1104 if (!IS_ERR(mhcd->xo_clk)) {
1105 ret = clk_prepare_enable(mhcd->xo_clk);
1106 } else {
1107 mhcd->xo_handle = msm_xo_get(MSM_XO_TCXO_D0, pdev_name);
1108 if (IS_ERR(mhcd->xo_handle)) {
1109 dev_err(&pdev->dev, "%s fail to get handle for X0 D0\n",
1110 __func__);
1111 ret = PTR_ERR(mhcd->xo_handle);
1112 goto free_async_irq;
1113 } else {
1114 ret = msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_ON);
1115 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001116 }
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001117 if (ret) {
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301118 dev_err(&pdev->dev, "%s failed to vote for TCXO %d\n",
1119 __func__, ret);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001120 goto free_xo_handle;
1121 }
1122
Manu Gautam91223e02011-11-08 15:27:22 +05301123 ret = msm_ehci_init_clocks(mhcd, 1);
1124 if (ret) {
1125 dev_err(&pdev->dev, "unable to initialize clocks\n");
1126 ret = -ENODEV;
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001127 goto devote_xo_handle;
Manu Gautam91223e02011-11-08 15:27:22 +05301128 }
1129
1130 ret = msm_ehci_init_vddcx(mhcd, 1);
1131 if (ret) {
1132 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
1133 ret = -ENODEV;
1134 goto deinit_clocks;
1135 }
1136
1137 ret = msm_ehci_config_vddcx(mhcd, 1);
1138 if (ret) {
1139 dev_err(&pdev->dev, "hsusb vddcx configuration failed\n");
1140 goto deinit_vddcx;
1141 }
1142
1143 ret = msm_ehci_ldo_init(mhcd, 1);
1144 if (ret) {
1145 dev_err(&pdev->dev, "hsusb vreg configuration failed\n");
1146 goto deinit_vddcx;
1147 }
1148
1149 ret = msm_ehci_ldo_enable(mhcd, 1);
1150 if (ret) {
1151 dev_err(&pdev->dev, "hsusb vreg enable failed\n");
1152 goto deinit_ldo;
1153 }
1154
1155 ret = msm_ehci_init_vbus(mhcd, 1);
1156 if (ret) {
1157 dev_err(&pdev->dev, "unable to get vbus\n");
1158 goto disable_ldo;
1159 }
1160
1161 ret = msm_hsusb_reset(mhcd);
1162 if (ret) {
1163 dev_err(&pdev->dev, "hsusb PHY initialization failed\n");
1164 goto vbus_deinit;
1165 }
1166
1167 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
1168 if (ret) {
1169 dev_err(&pdev->dev, "unable to register HCD\n");
1170 goto vbus_deinit;
1171 }
1172
Hemant Kumar56925352012-02-13 16:59:52 -08001173 pdata = mhcd->dev->platform_data;
1174 if (pdata && (!pdata->dock_connect_irq ||
1175 !irq_read_line(pdata->dock_connect_irq)))
1176 msm_ehci_vbus_power(mhcd, 1);
Manu Gautam91223e02011-11-08 15:27:22 +05301177
1178 device_init_wakeup(&pdev->dev, 1);
1179 wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
1180 wake_lock(&mhcd->wlock);
Chiranjeevi Velempatice169582012-08-25 14:15:41 +05301181 INIT_WORK(&mhcd->phy_susp_fail_work, msm_ehci_phy_susp_fail_work);
Manu Gautam91223e02011-11-08 15:27:22 +05301182 /*
1183 * This pdev->dev is assigned parent of root-hub by USB core,
1184 * hence, runtime framework automatically calls this driver's
1185 * runtime APIs based on root-hub's state.
1186 */
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +05301187 /* configure pmic_gpio_irq for D+ change */
1188 if (pdata && pdata->pmic_gpio_dp_irq)
1189 mhcd->pmic_gpio_dp_irq = pdata->pmic_gpio_dp_irq;
1190 if (mhcd->pmic_gpio_dp_irq) {
1191 ret = request_threaded_irq(mhcd->pmic_gpio_dp_irq, NULL,
1192 msm_ehci_host_wakeup_irq,
1193 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1194 "msm_ehci_host_wakeup", mhcd);
1195 if (!ret) {
1196 disable_irq_nosync(mhcd->pmic_gpio_dp_irq);
1197 } else {
1198 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
1199 mhcd->pmic_gpio_dp_irq, ret);
1200 mhcd->pmic_gpio_dp_irq = 0;
1201 }
1202 }
Manu Gautam91223e02011-11-08 15:27:22 +05301203 pm_runtime_set_active(&pdev->dev);
1204 pm_runtime_enable(&pdev->dev);
1205
1206 return 0;
1207
1208vbus_deinit:
1209 msm_ehci_init_vbus(mhcd, 0);
1210disable_ldo:
1211 msm_ehci_ldo_enable(mhcd, 0);
1212deinit_ldo:
1213 msm_ehci_ldo_init(mhcd, 0);
1214deinit_vddcx:
1215 msm_ehci_init_vddcx(mhcd, 0);
1216deinit_clocks:
1217 msm_ehci_init_clocks(mhcd, 0);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001218devote_xo_handle:
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301219 if (!IS_ERR(mhcd->xo_clk))
1220 clk_disable_unprepare(mhcd->xo_clk);
1221 else
1222 msm_xo_mode_vote(mhcd->xo_handle, MSM_XO_MODE_OFF);
Hemant Kumar8c0f2a82012-05-03 19:17:26 -07001223free_xo_handle:
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301224 if (!IS_ERR(mhcd->xo_clk))
1225 clk_put(mhcd->xo_clk);
1226 else
1227 msm_xo_put(mhcd->xo_handle);
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301228free_async_irq:
1229 if (mhcd->async_irq)
1230 free_irq(mhcd->async_irq, mhcd);
Manu Gautam91223e02011-11-08 15:27:22 +05301231unmap:
1232 iounmap(hcd->regs);
1233put_hcd:
1234 usb_put_hcd(hcd);
1235
1236 return ret;
1237}
1238
1239static int __devexit ehci_msm2_remove(struct platform_device *pdev)
1240{
1241 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1242 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1243
Vijayavardhan Vennapusa4fa13692012-08-02 14:35:03 +05301244 if (mhcd->pmic_gpio_dp_irq) {
1245 if (mhcd->pmic_gpio_dp_irq_enabled)
1246 disable_irq_wake(mhcd->pmic_gpio_dp_irq);
1247 free_irq(mhcd->pmic_gpio_dp_irq, mhcd);
1248 }
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301249 if (mhcd->async_irq) {
1250 if (mhcd->async_irq_enabled)
1251 disable_irq_wake(mhcd->async_irq);
1252 free_irq(mhcd->async_irq, mhcd);
1253 }
Manu Gautam91223e02011-11-08 15:27:22 +05301254 device_init_wakeup(&pdev->dev, 0);
Manu Gautam91223e02011-11-08 15:27:22 +05301255 pm_runtime_set_suspended(&pdev->dev);
1256
1257 usb_remove_hcd(hcd);
Hemant Kumar56925352012-02-13 16:59:52 -08001258
Vijayavardhan Vennapusa16fcaf92013-03-04 10:24:21 +05301259 if (!IS_ERR(mhcd->xo_clk)) {
1260 clk_disable_unprepare(mhcd->xo_clk);
1261 clk_put(mhcd->xo_clk);
1262 } else {
1263 msm_xo_put(mhcd->xo_handle);
1264 }
Manu Gautam91223e02011-11-08 15:27:22 +05301265 msm_ehci_vbus_power(mhcd, 0);
1266 msm_ehci_init_vbus(mhcd, 0);
1267 msm_ehci_ldo_enable(mhcd, 0);
1268 msm_ehci_ldo_init(mhcd, 0);
1269 msm_ehci_init_vddcx(mhcd, 0);
1270
1271 msm_ehci_init_clocks(mhcd, 0);
1272 wake_lock_destroy(&mhcd->wlock);
1273 iounmap(hcd->regs);
1274 usb_put_hcd(hcd);
1275
1276 return 0;
1277}
1278
1279#ifdef CONFIG_PM_SLEEP
1280static int ehci_msm2_pm_suspend(struct device *dev)
1281{
1282 struct usb_hcd *hcd = dev_get_drvdata(dev);
1283 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1284
1285 dev_dbg(dev, "ehci-msm2 PM suspend\n");
1286
1287 if (device_may_wakeup(dev))
1288 enable_irq_wake(hcd->irq);
1289
1290 return msm_ehci_suspend(mhcd);
1291
1292}
1293
1294static int ehci_msm2_pm_resume(struct device *dev)
1295{
1296 int ret;
1297 struct usb_hcd *hcd = dev_get_drvdata(dev);
1298 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1299
1300 dev_dbg(dev, "ehci-msm2 PM resume\n");
1301
1302 if (device_may_wakeup(dev))
1303 disable_irq_wake(hcd->irq);
1304
1305 ret = msm_ehci_resume(mhcd);
1306 if (ret)
1307 return ret;
1308
1309 /* Bring the device to full powered state upon system resume */
1310 pm_runtime_disable(dev);
1311 pm_runtime_set_active(dev);
1312 pm_runtime_enable(dev);
1313
1314 return 0;
1315}
1316#endif
1317
1318#ifdef CONFIG_PM_RUNTIME
1319static int ehci_msm2_runtime_idle(struct device *dev)
1320{
1321 dev_dbg(dev, "EHCI runtime idle\n");
1322
1323 return 0;
1324}
1325
1326static int ehci_msm2_runtime_suspend(struct device *dev)
1327{
1328 struct usb_hcd *hcd = dev_get_drvdata(dev);
1329 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1330
1331 dev_dbg(dev, "EHCI runtime suspend\n");
1332 return msm_ehci_suspend(mhcd);
1333}
1334
1335static int ehci_msm2_runtime_resume(struct device *dev)
1336{
1337 struct usb_hcd *hcd = dev_get_drvdata(dev);
1338 struct msm_hcd *mhcd = hcd_to_mhcd(hcd);
1339
1340 dev_dbg(dev, "EHCI runtime resume\n");
1341 return msm_ehci_resume(mhcd);
1342}
1343#endif
1344
1345#ifdef CONFIG_PM
1346static const struct dev_pm_ops ehci_msm2_dev_pm_ops = {
1347 SET_SYSTEM_SLEEP_PM_OPS(ehci_msm2_pm_suspend, ehci_msm2_pm_resume)
1348 SET_RUNTIME_PM_OPS(ehci_msm2_runtime_suspend, ehci_msm2_runtime_resume,
1349 ehci_msm2_runtime_idle)
1350};
1351#endif
1352
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301353static const struct of_device_id ehci_msm2_dt_match[] = {
1354 { .compatible = "qcom,ehci-host",
1355 },
1356 {}
1357};
1358
Manu Gautam91223e02011-11-08 15:27:22 +05301359static struct platform_driver ehci_msm2_driver = {
1360 .probe = ehci_msm2_probe,
1361 .remove = __devexit_p(ehci_msm2_remove),
1362 .driver = {
1363 .name = "msm_ehci_host",
1364#ifdef CONFIG_PM
1365 .pm = &ehci_msm2_dev_pm_ops,
1366#endif
Vijayavardhan Vennapusa1f5da0b2013-01-08 20:03:57 +05301367 .of_match_table = ehci_msm2_dt_match,
Manu Gautam91223e02011-11-08 15:27:22 +05301368 },
1369};