blob: 443bee0740704f64fb18b3a92fa3c344b8891e9a [file] [log] [blame]
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301/* ehci-msm-hsic.c - HSUSB Host Controller Driver Implementation
2 *
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05303 * Copyright (c) 2011-2013, Linux Foundation. All rights reserved.
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +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>
Hemant Kumare6275972012-02-29 20:06:21 -080028#include <linux/debugfs.h>
29#include <linux/seq_file.h>
Ajay Dudaniab3bf192012-08-28 09:58:04 -070030#include <linux/wakelock.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053031#include <linux/pm_runtime.h>
32#include <linux/regulator/consumer.h>
33
34#include <linux/usb/msm_hsusb_hw.h>
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +053035#include <linux/usb/msm_hsusb.h>
36#include <linux/gpio.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030037#include <linux/spinlock.h>
Ajay Dudani11ab1d52012-08-17 17:12:26 -070038#include <linux/kthread.h>
39#include <linux/wait.h>
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +053040#include <linux/pm_qos.h>
Hemant Kumar5e386632012-08-30 14:23:38 -070041#include <linux/irq.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030042
43#include <mach/msm_bus.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053044#include <mach/clk.h>
45#include <mach/msm_iomap.h>
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +053046#include <mach/msm_xo.h>
Vamsi Krishna34f01582011-12-14 19:54:42 -080047#include <linux/spinlock.h>
Hemant Kumar45d211b2012-05-31 17:58:43 -070048#include <linux/cpu.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030049#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053050
51#define MSM_USB_BASE (hcd->regs)
Hemant Kumar105d07f2012-07-02 15:33:07 -070052#define USB_REG_START_OFFSET 0x90
53#define USB_REG_END_OFFSET 0x250
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053054
Hemant Kumar2309eaa2012-08-14 16:46:42 -070055static struct workqueue_struct *ehci_wq;
Ajay Dudani11ab1d52012-08-17 17:12:26 -070056struct ehci_timer {
57#define GPT_LD(p) ((p) & 0x00FFFFFF)
58 u32 gptimer0_ld;
59#define GPT_RUN BIT(31)
60#define GPT_RESET BIT(30)
61#define GPT_MODE BIT(24)
62#define GPT_CNT(p) ((p) & 0x00FFFFFF)
63 u32 gptimer0_ctrl;
64
65 u32 gptimer1_ld;
66 u32 gptimer1_ctrl;
67};
Hemant Kumar2309eaa2012-08-14 16:46:42 -070068
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053069struct msm_hsic_hcd {
70 struct ehci_hcd ehci;
Hemant Kumar4cd49e12012-09-06 19:57:14 -070071 spinlock_t wakeup_lock;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053072 struct device *dev;
73 struct clk *ahb_clk;
Manu Gautam5143b252012-01-05 19:25:23 -080074 struct clk *core_clk;
75 struct clk *alt_core_clk;
76 struct clk *phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053077 struct clk *cal_clk;
78 struct regulator *hsic_vddcx;
Pavankumar Kondetibbd05822013-01-28 17:04:39 +053079 atomic_t async_int;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053080 atomic_t in_lpm;
Ajay Dudaniab3bf192012-08-28 09:58:04 -070081 struct wake_lock wlock;
Vamsi Krishna34f01582011-12-14 19:54:42 -080082 int peripheral_status_irq;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -080083 int wakeup_irq;
Hemant Kumar6fd65032012-05-23 13:02:24 -070084 int wakeup_gpio;
Jack Phamfe441ea2012-03-23 17:03:15 -070085 bool wakeup_irq_enabled;
Ajay Dudaniab3bf192012-08-28 09:58:04 -070086 atomic_t pm_usage_cnt;
Hemant Kumare6275972012-02-29 20:06:21 -080087 uint32_t bus_perf_client;
Hemant Kumar6fd65032012-05-23 13:02:24 -070088 uint32_t wakeup_int_cnt;
Amit Blayd6ea6102012-06-07 16:26:24 +030089 enum usb_vdd_type vdd_type;
Hemant Kumar2309eaa2012-08-14 16:46:42 -070090
91 struct work_struct bus_vote_w;
92 bool bus_vote;
Ajay Dudani11ab1d52012-08-17 17:12:26 -070093
94 /* gp timer */
95 struct ehci_timer __iomem *timer;
96 struct completion gpt0_completion;
97 struct completion rt_completion;
98 int resume_status;
99 int resume_again;
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530100 int bus_reset;
101 int reset_again;
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +0530102
103 struct pm_qos_request pm_qos_req_dma;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530104};
105
Hemant Kumar105d07f2012-07-02 15:33:07 -0700106struct msm_hsic_hcd *__mehci;
107
Hemant Kumare6275972012-02-29 20:06:21 -0800108static bool debug_bus_voting_enabled = true;
Hemant Kumar45d211b2012-05-31 17:58:43 -0700109
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700110static unsigned int enable_payload_log = 1;
111module_param(enable_payload_log, uint, S_IRUGO | S_IWUSR);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700112static unsigned int enable_dbg_log = 1;
113module_param(enable_dbg_log, uint, S_IRUGO | S_IWUSR);
114/*by default log ep0 and efs sync ep*/
115static unsigned int ep_addr_rxdbg_mask = 9;
116module_param(ep_addr_rxdbg_mask, uint, S_IRUGO | S_IWUSR);
117static unsigned int ep_addr_txdbg_mask = 9;
118module_param(ep_addr_txdbg_mask, uint, S_IRUGO | S_IWUSR);
119
120/* Maximum debug message length */
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700121#define DBG_MSG_LEN 128UL
Hemant Kumar45d211b2012-05-31 17:58:43 -0700122
123/* Maximum number of messages */
124#define DBG_MAX_MSG 256UL
125
126#define TIME_BUF_LEN 20
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700127#define HEX_DUMP_LEN 72
Hemant Kumar45d211b2012-05-31 17:58:43 -0700128
129enum event_type {
130 EVENT_UNDEF = -1,
131 URB_SUBMIT,
132 URB_COMPLETE,
133 EVENT_NONE,
134};
135
136#define EVENT_STR_LEN 5
137
Hemant Kumar45d211b2012-05-31 17:58:43 -0700138static enum event_type str_to_event(const char *name)
139{
140 if (!strncasecmp("S", name, EVENT_STR_LEN))
141 return URB_SUBMIT;
142 if (!strncasecmp("C", name, EVENT_STR_LEN))
143 return URB_COMPLETE;
144 if (!strncasecmp("", name, EVENT_STR_LEN))
145 return EVENT_NONE;
146
147 return EVENT_UNDEF;
148}
149
150/*log ep0 activity*/
151static struct {
152 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
153 unsigned idx; /* index */
154 rwlock_t lck; /* lock */
155} dbg_hsic_ctrl = {
156 .idx = 0,
157 .lck = __RW_LOCK_UNLOCKED(lck)
158};
159
160static struct {
161 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
162 unsigned idx; /* index */
163 rwlock_t lck; /* lock */
164} dbg_hsic_data = {
165 .idx = 0,
166 .lck = __RW_LOCK_UNLOCKED(lck)
167};
168
169/**
170 * dbg_inc: increments debug event index
171 * @idx: buffer index
172 */
173static void dbg_inc(unsigned *idx)
174{
175 *idx = (*idx + 1) & (DBG_MAX_MSG-1);
176}
177
178/*get_timestamp - returns time of day in us */
179static char *get_timestamp(char *tbuf)
180{
181 unsigned long long t;
182 unsigned long nanosec_rem;
183
184 t = cpu_clock(smp_processor_id());
185 nanosec_rem = do_div(t, 1000000000)/1000;
186 scnprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu] ", (unsigned long)t,
187 nanosec_rem);
188 return tbuf;
189}
190
191static int allow_dbg_log(int ep_addr)
192{
193 int dir, num;
194
195 dir = ep_addr & USB_DIR_IN ? USB_DIR_IN : USB_DIR_OUT;
196 num = ep_addr & ~USB_DIR_IN;
197 num = 1 << num;
198
199 if ((dir == USB_DIR_IN) && (num & ep_addr_rxdbg_mask))
200 return 1;
201 if ((dir == USB_DIR_OUT) && (num & ep_addr_txdbg_mask))
202 return 1;
203
204 return 0;
205}
206
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700207static char *get_hex_data(char *dbuf, struct urb *urb, int event, int status)
208{
209 int ep_addr = urb->ep->desc.bEndpointAddress;
210 char *ubuf = urb->transfer_buffer;
211 size_t len = event ? \
212 urb->actual_length : urb->transfer_buffer_length;
213
214 if (status == -EINPROGRESS)
215 status = 0;
216
217 /*Only dump ep in completions and epout submissions*/
218 if (len && !status &&
219 (((ep_addr & USB_DIR_IN) && event) ||
220 (!(ep_addr & USB_DIR_IN) && !event))) {
221 if (len >= 32)
222 len = 32;
223 hex_dump_to_buffer(ubuf, len, 32, 4, dbuf, HEX_DUMP_LEN, 0);
224 } else {
225 dbuf = "";
226 }
227
228 return dbuf;
229}
230
Hemant Kumar45d211b2012-05-31 17:58:43 -0700231static void dbg_log_event(struct urb *urb, char * event, unsigned extra)
232{
233 unsigned long flags;
234 int ep_addr;
235 char tbuf[TIME_BUF_LEN];
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700236 char dbuf[HEX_DUMP_LEN];
Hemant Kumar45d211b2012-05-31 17:58:43 -0700237
238 if (!enable_dbg_log)
239 return;
240
241 if (!urb) {
242 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
243 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx], DBG_MSG_LEN,
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700244 "%s: %s : %u", get_timestamp(tbuf), event, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700245 dbg_inc(&dbg_hsic_ctrl.idx);
246 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
247 return;
248 }
249
250 ep_addr = urb->ep->desc.bEndpointAddress;
251 if (!allow_dbg_log(ep_addr))
252 return;
253
254 if ((ep_addr & 0x0f) == 0x0) {
255 /*submit event*/
256 if (!str_to_event(event)) {
257 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
258 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
259 DBG_MSG_LEN, "%s: [%s : %p]:[%s] "
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700260 "%02x %02x %04x %04x %04x %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700261 get_timestamp(tbuf), event, urb,
262 (ep_addr & USB_DIR_IN) ? "in" : "out",
263 urb->setup_packet[0], urb->setup_packet[1],
264 (urb->setup_packet[3] << 8) |
265 urb->setup_packet[2],
266 (urb->setup_packet[5] << 8) |
267 urb->setup_packet[4],
268 (urb->setup_packet[7] << 8) |
269 urb->setup_packet[6],
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700270 urb->transfer_buffer_length, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700271
272 dbg_inc(&dbg_hsic_ctrl.idx);
273 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
274 } else {
275 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
276 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700277 DBG_MSG_LEN, "%s: [%s : %p]:[%s] %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700278 get_timestamp(tbuf), event, urb,
279 (ep_addr & USB_DIR_IN) ? "in" : "out",
280 urb->actual_length, extra);
281
282 dbg_inc(&dbg_hsic_ctrl.idx);
283 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
284 }
285 } else {
286 write_lock_irqsave(&dbg_hsic_data.lck, flags);
287 scnprintf(dbg_hsic_data.buf[dbg_hsic_data.idx], DBG_MSG_LEN,
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700288 "%s: [%s : %p]:ep%d[%s] %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700289 get_timestamp(tbuf), event, urb, ep_addr & 0x0f,
290 (ep_addr & USB_DIR_IN) ? "in" : "out",
291 str_to_event(event) ? urb->actual_length :
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700292 urb->transfer_buffer_length, extra,
293 enable_payload_log ? get_hex_data(dbuf, urb,
294 str_to_event(event), extra) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700295
296 dbg_inc(&dbg_hsic_data.idx);
297 write_unlock_irqrestore(&dbg_hsic_data.lck, flags);
298 }
299}
300
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530301static inline struct msm_hsic_hcd *hcd_to_hsic(struct usb_hcd *hcd)
302{
303 return (struct msm_hsic_hcd *) (hcd->hcd_priv);
304}
305
306static inline struct usb_hcd *hsic_to_hcd(struct msm_hsic_hcd *mehci)
307{
308 return container_of((void *) mehci, struct usb_hcd, hcd_priv);
309}
310
Hemant Kumar105d07f2012-07-02 15:33:07 -0700311static void dump_hsic_regs(struct usb_hcd *hcd)
312{
313 int i;
314 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
315
316 if (atomic_read(&mehci->in_lpm))
317 return;
318
319 for (i = USB_REG_START_OFFSET; i <= USB_REG_END_OFFSET; i += 0x10)
320 pr_info("%p: %08x\t%08x\t%08x\t%08x\n", hcd->regs + i,
321 readl_relaxed(hcd->regs + i),
322 readl_relaxed(hcd->regs + i + 4),
323 readl_relaxed(hcd->regs + i + 8),
324 readl_relaxed(hcd->regs + i + 0xc));
325}
326
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530327#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
328
Amit Blayd6ea6102012-06-07 16:26:24 +0300329#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Hemant Kumar266d9d52012-10-17 13:48:10 -0700330#define USB_PHY_VDD_DIG_VOL_MIN 945000 /* uV */
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700331#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530332
Lena Salman8c8ba382012-02-14 15:59:31 +0200333#define HSIC_DBG1_REG 0x38
334
Amit Blayd6ea6102012-06-07 16:26:24 +0300335static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
336 { /* VDD_CX CORNER Voting */
337 [VDD_NONE] = RPM_VREG_CORNER_NONE,
338 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
339 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
340 },
341 { /* VDD_CX Voltage Voting */
342 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
343 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
344 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
345 },
346};
347
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530348static int msm_hsic_init_vddcx(struct msm_hsic_hcd *mehci, int init)
349{
350 int ret = 0;
Amit Blayd6ea6102012-06-07 16:26:24 +0300351 int none_vol, min_vol, max_vol;
352
353 if (!mehci->hsic_vddcx) {
354 mehci->vdd_type = VDDCX_CORNER;
355 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
356 "hsic_vdd_dig");
357 if (IS_ERR(mehci->hsic_vddcx)) {
358 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
359 "HSIC_VDDCX");
360 if (IS_ERR(mehci->hsic_vddcx)) {
361 dev_err(mehci->dev, "unable to get hsic vddcx\n");
362 return PTR_ERR(mehci->hsic_vddcx);
363 }
364 mehci->vdd_type = VDDCX;
365 }
366 }
367
368 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
369 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
370 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530371
372 if (!init)
373 goto disable_reg;
374
Amit Blayd6ea6102012-06-07 16:26:24 +0300375 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530376 if (ret) {
377 dev_err(mehci->dev, "unable to set the voltage"
378 "for hsic vddcx\n");
Mayank Rana189ac052012-03-24 04:35:02 +0530379 return ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530380 }
381
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530382 ret = regulator_enable(mehci->hsic_vddcx);
383 if (ret) {
384 dev_err(mehci->dev, "unable to enable hsic vddcx\n");
385 goto reg_enable_err;
386 }
387
388 return 0;
389
390disable_reg:
391 regulator_disable(mehci->hsic_vddcx);
392reg_enable_err:
Amit Blayd6ea6102012-06-07 16:26:24 +0300393 regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
394
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530395 return ret;
396
397}
398
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530399static int __maybe_unused ulpi_read(struct msm_hsic_hcd *mehci, u32 reg)
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700400{
401 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Devin Kim01e5a5b2012-08-30 02:52:45 -0700402 int cnt = 0;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700403
404 /* initiate read operation */
405 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
406 USB_ULPI_VIEWPORT);
407
408 /* wait for completion */
Devin Kim01e5a5b2012-08-30 02:52:45 -0700409 while (cnt < ULPI_IO_TIMEOUT_USEC) {
410 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
411 break;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700412 udelay(1);
Devin Kim01e5a5b2012-08-30 02:52:45 -0700413 cnt++;
414 }
415
416 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
417 dev_err(mehci->dev, "ulpi_read: timeout ULPI_VIEWPORT: %08x\n",
418 readl_relaxed(USB_ULPI_VIEWPORT));
419 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
420 readl_relaxed(USB_PORTSC),
421 readl_relaxed(USB_USBCMD),
422 readl_relaxed(USB_FRINDEX));
423
424 /*frame counter increments afte 125us*/
425 udelay(130);
426 dev_err(mehci->dev, "ulpi_read: FRINDEX: %08x\n",
427 readl_relaxed(USB_FRINDEX));
428 return -ETIMEDOUT;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700429 }
430
431 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
432}
433
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530434static int ulpi_write(struct msm_hsic_hcd *mehci, u32 val, u32 reg)
435{
436 struct usb_hcd *hcd = hsic_to_hcd(mehci);
437 int cnt = 0;
438
439 /* initiate write operation */
440 writel_relaxed(ULPI_RUN | ULPI_WRITE |
441 ULPI_ADDR(reg) | ULPI_DATA(val),
442 USB_ULPI_VIEWPORT);
443
444 /* wait for completion */
445 while (cnt < ULPI_IO_TIMEOUT_USEC) {
446 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
447 break;
448 udelay(1);
449 cnt++;
450 }
451
452 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Devin Kim01e5a5b2012-08-30 02:52:45 -0700453 dev_err(mehci->dev, "ulpi_write: timeout ULPI_VIEWPORT: %08x\n",
454 readl_relaxed(USB_ULPI_VIEWPORT));
455 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
456 readl_relaxed(USB_PORTSC),
457 readl_relaxed(USB_USBCMD),
458 readl_relaxed(USB_FRINDEX));
459
460 /*frame counter increments afte 125us*/
461 udelay(130);
462 dev_err(mehci->dev, "ulpi_write: FRINDEX: %08x\n",
463 readl_relaxed(USB_FRINDEX));
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530464 return -ETIMEDOUT;
465 }
466
467 return 0;
468}
469
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530470static int msm_hsic_config_gpios(struct msm_hsic_hcd *mehci, int gpio_en)
471{
472 int rc = 0;
473 struct msm_hsic_host_platform_data *pdata;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800474 static int gpio_status;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530475
476 pdata = mehci->dev->platform_data;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800477
Lena Salman8c8ba382012-02-14 15:59:31 +0200478 if (!pdata || !pdata->strobe || !pdata->data)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530479 return rc;
480
Vamsi Krishna34f01582011-12-14 19:54:42 -0800481 if (gpio_status == gpio_en)
482 return 0;
483
484 gpio_status = gpio_en;
485
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530486 if (!gpio_en)
487 goto free_gpio;
488
489 rc = gpio_request(pdata->strobe, "HSIC_STROBE_GPIO");
490 if (rc < 0) {
491 dev_err(mehci->dev, "gpio request failed for HSIC STROBE\n");
492 return rc;
493 }
494
495 rc = gpio_request(pdata->data, "HSIC_DATA_GPIO");
496 if (rc < 0) {
497 dev_err(mehci->dev, "gpio request failed for HSIC DATA\n");
498 goto free_strobe;
499 }
500
Hemant Kumar6fd65032012-05-23 13:02:24 -0700501 if (mehci->wakeup_gpio) {
502 rc = gpio_request(mehci->wakeup_gpio, "HSIC_WAKEUP_GPIO");
503 if (rc < 0) {
504 dev_err(mehci->dev, "gpio request failed for HSIC WAKEUP\n");
505 goto free_data;
506 }
507 }
508
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530509 return 0;
510
511free_gpio:
Hemant Kumar6fd65032012-05-23 13:02:24 -0700512 if (mehci->wakeup_gpio)
513 gpio_free(mehci->wakeup_gpio);
514free_data:
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530515 gpio_free(pdata->data);
516free_strobe:
517 gpio_free(pdata->strobe);
518
519 return rc;
520}
521
Vamsi Krishna64b48612012-06-14 16:08:11 -0700522static void msm_hsic_clk_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530523{
524 int ret;
525
Manu Gautam5143b252012-01-05 19:25:23 -0800526 ret = clk_reset(mehci->core_clk, CLK_RESET_ASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530527 if (ret) {
Vamsi Krishna64b48612012-06-14 16:08:11 -0700528 dev_err(mehci->dev, "hsic clk assert failed:%d\n", ret);
529 return;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530530 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700531 clk_disable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530532
Manu Gautam5143b252012-01-05 19:25:23 -0800533 ret = clk_reset(mehci->core_clk, CLK_RESET_DEASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530534 if (ret)
Vamsi Krishna64b48612012-06-14 16:08:11 -0700535 dev_err(mehci->dev, "hsic clk deassert failed:%d\n", ret);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530536
Vamsi Krishna64b48612012-06-14 16:08:11 -0700537 usleep_range(10000, 12000);
538
539 clk_enable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530540}
541
Vamsi Krishna64b48612012-06-14 16:08:11 -0700542#define HSIC_STROBE_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C0)
543#define HSIC_DATA_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C4)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530544#define HSIC_CAL_PAD_CTL (MSM_TLMM_BASE+0x20C8)
545#define HSIC_LV_MODE 0x04
546#define HSIC_PAD_CALIBRATION 0xA8
547#define HSIC_GPIO_PAD_VAL 0x0A0AAA10
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530548#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
549static int msm_hsic_reset(struct msm_hsic_hcd *mehci)
550{
551 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530552 int ret;
Lena Salman8c8ba382012-02-14 15:59:31 +0200553 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530554
Vamsi Krishna64b48612012-06-14 16:08:11 -0700555 msm_hsic_clk_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530556
Vamsi Krishna64b48612012-06-14 16:08:11 -0700557 /* select ulpi phy */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530558 writel_relaxed(0x80000000, USB_PORTSC);
559
Vamsi Krishna64b48612012-06-14 16:08:11 -0700560 mb();
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530561
Lena Salman8c8ba382012-02-14 15:59:31 +0200562 /* HSIC init sequence when HSIC signals (Strobe/Data) are
563 routed via GPIOs */
564 if (pdata && pdata->strobe && pdata->data) {
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530565
Lena Salman8c8ba382012-02-14 15:59:31 +0200566 /* Enable LV_MODE in HSIC_CAL_PAD_CTL register */
567 writel_relaxed(HSIC_LV_MODE, HSIC_CAL_PAD_CTL);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530568
Vamsi Krishna64b48612012-06-14 16:08:11 -0700569 mb();
570
Lena Salman8c8ba382012-02-14 15:59:31 +0200571 /*set periodic calibration interval to ~2.048sec in
572 HSIC_IO_CAL_REG */
573 ulpi_write(mehci, 0xFF, 0x33);
574
575 /* Enable periodic IO calibration in HSIC_CFG register */
576 ulpi_write(mehci, HSIC_PAD_CALIBRATION, 0x30);
577
Vamsi Krishna64b48612012-06-14 16:08:11 -0700578 /* Configure GPIO pins for HSIC functionality mode */
Lena Salman8c8ba382012-02-14 15:59:31 +0200579 ret = msm_hsic_config_gpios(mehci, 1);
580 if (ret) {
581 dev_err(mehci->dev, " gpio configuarion failed\n");
582 return ret;
583 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700584 /* Set LV_MODE=0x1 and DCC=0x2 in HSIC_GPIO PAD_CTL register */
585 writel_relaxed(HSIC_GPIO_PAD_VAL, HSIC_STROBE_GPIO_PAD_CTL);
586 writel_relaxed(HSIC_GPIO_PAD_VAL, HSIC_DATA_GPIO_PAD_CTL);
587
588 mb();
589
Lena Salman8c8ba382012-02-14 15:59:31 +0200590 /* Enable HSIC mode in HSIC_CFG register */
591 ulpi_write(mehci, 0x01, 0x31);
592 } else {
593 /* HSIC init sequence when HSIC signals (Strobe/Data) are routed
594 via dedicated I/O */
595
596 /* programmable length of connect signaling (33.2ns) */
597 ret = ulpi_write(mehci, 3, HSIC_DBG1_REG);
598 if (ret) {
599 pr_err("%s: Unable to program length of connect "
600 "signaling\n", __func__);
601 }
602
603 /*set periodic calibration interval to ~2.048sec in
604 HSIC_IO_CAL_REG */
605 ulpi_write(mehci, 0xFF, 0x33);
606
607 /* Enable HSIC mode in HSIC_CFG register */
608 ulpi_write(mehci, 0xA9, 0x30);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530609 }
610
Hemant Kumar6fd65032012-05-23 13:02:24 -0700611 /*disable auto resume*/
612 ulpi_write(mehci, ULPI_IFC_CTRL_AUTORESUME, ULPI_CLR(ULPI_IFC_CTRL));
613
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530614 return 0;
615}
616
617#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
618#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
619
620#ifdef CONFIG_PM_SLEEP
621static int msm_hsic_suspend(struct msm_hsic_hcd *mehci)
622{
623 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530624 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530625 u32 val;
Amit Blayd6ea6102012-06-07 16:26:24 +0300626 int none_vol, max_vol;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530627
628 if (atomic_read(&mehci->in_lpm)) {
629 dev_dbg(mehci->dev, "%s called in lpm\n", __func__);
630 return 0;
631 }
632
633 disable_irq(hcd->irq);
Jack Phambe05fbb2012-05-16 10:56:26 -0700634
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700635 /* make sure we don't race against a remote wakeup */
636 if (test_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags) ||
Jack Phambe05fbb2012-05-16 10:56:26 -0700637 readl_relaxed(USB_PORTSC) & PORT_RESUME) {
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700638 dev_dbg(mehci->dev, "wakeup pending, aborting suspend\n");
Jack Phambe05fbb2012-05-16 10:56:26 -0700639 enable_irq(hcd->irq);
640 return -EBUSY;
641 }
642
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530643 /*
644 * PHY may take some time or even fail to enter into low power
645 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
646 * in failure case.
647 */
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700648 val = readl_relaxed(USB_PORTSC);
649 val &= ~PORT_RWC_BITS;
650 val |= PORTSC_PHCD;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530651 writel_relaxed(val, USB_PORTSC);
652 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
653 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
654 break;
655 udelay(1);
656 cnt++;
657 }
658
659 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
660 dev_err(mehci->dev, "Unable to suspend PHY\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530661 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530662 msm_hsic_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530663 }
664
665 /*
666 * PHY has capability to generate interrupt asynchronously in low
667 * power mode (LPM). This interrupt is level triggered. So USB IRQ
668 * line must be disabled till async interrupt enable bit is cleared
669 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
Pavankumar Kondeti1c1d82b2013-01-28 21:37:59 +0530670 * block data communication from PHY. Enable asynchronous interrupt
671 * only when wakeup gpio IRQ is not present.
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530672 */
Pavankumar Kondeti1c1d82b2013-01-28 21:37:59 +0530673 if (mehci->wakeup_irq)
674 writel_relaxed(readl_relaxed(USB_USBCMD) |
675 ULPI_STP_CTRL, USB_USBCMD);
676 else
677 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530678 ULPI_STP_CTRL, USB_USBCMD);
679
680 /*
681 * Ensure that hardware is put in low power mode before
682 * clocks are turned OFF and VDD is allowed to minimize.
683 */
684 mb();
685
Manu Gautam28b1bac2012-01-30 16:43:06 +0530686 clk_disable_unprepare(mehci->core_clk);
687 clk_disable_unprepare(mehci->phy_clk);
688 clk_disable_unprepare(mehci->cal_clk);
689 clk_disable_unprepare(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530690
Amit Blayd6ea6102012-06-07 16:26:24 +0300691 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
692 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
693
694 ret = regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700695 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300696 dev_err(mehci->dev, "unable to set vddcx voltage for VDD MIN\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700697
Hemant Kumare6275972012-02-29 20:06:21 -0800698 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700699 mehci->bus_vote = false;
700 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800701 }
702
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530703 atomic_set(&mehci->in_lpm, 1);
704 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700705
706 mehci->wakeup_irq_enabled = 1;
707 enable_irq_wake(mehci->wakeup_irq);
708 enable_irq(mehci->wakeup_irq);
709
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700710 wake_unlock(&mehci->wlock);
711
Devin Kim476bbd72012-07-19 18:11:11 -0700712 dev_dbg(mehci->dev, "HSIC-USB in low power mode\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530713
714 return 0;
715}
716
717static int msm_hsic_resume(struct msm_hsic_hcd *mehci)
718{
719 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530720 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530721 unsigned temp;
Amit Blayd6ea6102012-06-07 16:26:24 +0300722 int min_vol, max_vol;
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700723 unsigned long flags;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530724
725 if (!atomic_read(&mehci->in_lpm)) {
726 dev_dbg(mehci->dev, "%s called in !in_lpm\n", __func__);
727 return 0;
728 }
729
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530730 /* Handles race with Async interrupt */
731 disable_irq(hcd->irq);
732
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700733 spin_lock_irqsave(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700734 if (mehci->wakeup_irq_enabled) {
735 disable_irq_wake(mehci->wakeup_irq);
736 disable_irq_nosync(mehci->wakeup_irq);
737 mehci->wakeup_irq_enabled = 0;
738 }
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700739 spin_unlock_irqrestore(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700740
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700741 wake_lock(&mehci->wlock);
742
Hemant Kumare6275972012-02-29 20:06:21 -0800743 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700744 mehci->bus_vote = true;
745 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800746 }
747
Amit Blayd6ea6102012-06-07 16:26:24 +0300748 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
749 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
750
751 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700752 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300753 dev_err(mehci->dev, "unable to set nominal vddcx voltage (no VDD MIN)\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700754
Manu Gautam28b1bac2012-01-30 16:43:06 +0530755 clk_prepare_enable(mehci->core_clk);
756 clk_prepare_enable(mehci->phy_clk);
757 clk_prepare_enable(mehci->cal_clk);
758 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530759
760 temp = readl_relaxed(USB_USBCMD);
761 temp &= ~ASYNC_INTR_CTRL;
762 temp &= ~ULPI_STP_CTRL;
763 writel_relaxed(temp, USB_USBCMD);
764
765 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
766 goto skip_phy_resume;
767
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700768 temp = readl_relaxed(USB_PORTSC);
769 temp &= ~(PORT_RWC_BITS | PORTSC_PHCD);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530770 writel_relaxed(temp, USB_PORTSC);
771 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
772 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD) &&
773 (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE))
774 break;
775 udelay(1);
776 cnt++;
777 }
778
779 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
780 /*
781 * This is a fatal error. Reset the link and
782 * PHY to make hsic working.
783 */
784 dev_err(mehci->dev, "Unable to resume USB. Reset the hsic\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530785 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530786 msm_hsic_reset(mehci);
787 }
788
789skip_phy_resume:
790
Hemant Kumar6fd65032012-05-23 13:02:24 -0700791 usb_hcd_resume_root_hub(hcd);
792
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530793 atomic_set(&mehci->in_lpm, 0);
794
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530795 if (atomic_read(&mehci->async_int)) {
796 atomic_set(&mehci->async_int, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530797 pm_runtime_put_noidle(mehci->dev);
Jack Phamdd5ad792012-07-26 10:31:03 -0700798 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700799 }
800
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700801 if (atomic_read(&mehci->pm_usage_cnt)) {
802 atomic_set(&mehci->pm_usage_cnt, 0);
803 pm_runtime_put_noidle(mehci->dev);
804 }
Jack Phamdd5ad792012-07-26 10:31:03 -0700805
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530806 enable_irq(hcd->irq);
Devin Kim476bbd72012-07-19 18:11:11 -0700807 dev_dbg(mehci->dev, "HSIC-USB exited from low power mode\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530808
809 return 0;
810}
811#endif
812
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700813static void ehci_hsic_bus_vote_w(struct work_struct *w)
814{
815 struct msm_hsic_hcd *mehci =
816 container_of(w, struct msm_hsic_hcd, bus_vote_w);
817 int ret;
818
819 ret = msm_bus_scale_client_update_request(mehci->bus_perf_client,
820 mehci->bus_vote);
821 if (ret)
822 dev_err(mehci->dev, "%s: Failed to vote for bus bandwidth %d\n",
823 __func__, ret);
824}
825
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530826static int msm_hsic_reset_done(struct usb_hcd *hcd)
827{
828 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
829 u32 __iomem *status_reg = &ehci->regs->port_status[0];
830 int ret;
831
832 ehci_writel(ehci, ehci_readl(ehci, status_reg) & ~(PORT_RWC_BITS |
833 PORT_RESET), status_reg);
834
835 ret = handshake(ehci, status_reg, PORT_RESET, 0, 1 * 1000);
836
837 if (ret)
838 pr_err("reset handshake failed in %s\n", __func__);
839 else
840 ehci_writel(ehci, ehci_readl(ehci, &ehci->regs->command) |
841 CMD_RUN, &ehci->regs->command);
842
843 return ret;
844}
845
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700846#define STS_GPTIMER0_INTERRUPT BIT(24)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530847static irqreturn_t msm_hsic_irq(struct usb_hcd *hcd)
848{
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700849 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530850 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700851 u32 status;
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530852 int ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530853
854 if (atomic_read(&mehci->in_lpm)) {
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700855 dev_dbg(mehci->dev, "phy async intr\n");
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530856 dbg_log_event(NULL, "Async IRQ", 0);
857 ret = pm_runtime_get(mehci->dev);
858 if ((ret == 1) || (ret == -EINPROGRESS)) {
859 pm_runtime_put_noidle(mehci->dev);
860 } else {
861 disable_irq_nosync(hcd->irq);
862 atomic_set(&mehci->async_int, 1);
863 }
864
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530865 return IRQ_HANDLED;
866 }
867
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700868 status = ehci_readl(ehci, &ehci->regs->status);
869
870 if (status & STS_GPTIMER0_INTERRUPT) {
871 int timeleft;
872
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530873 dbg_log_event(NULL, "FPR: gpt0_isr", mehci->bus_reset);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700874
875 timeleft = GPT_CNT(ehci_readl(ehci,
876 &mehci->timer->gptimer1_ctrl));
877 if (timeleft) {
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530878 if (mehci->bus_reset) {
879 ret = msm_hsic_reset_done(hcd);
880 if (ret) {
881 mehci->reset_again = 1;
882 dbg_log_event(NULL, "RESET: fail", 0);
883 }
884 } else {
885 ehci_writel(ehci, ehci_readl(ehci,
886 &ehci->regs->command) | CMD_RUN,
887 &ehci->regs->command);
888 }
889 } else {
890 if (mehci->bus_reset)
891 mehci->reset_again = 1;
892 else
893 mehci->resume_again = 1;
894 }
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700895
896 dbg_log_event(NULL, "FPR: timeleft", timeleft);
897
898 complete(&mehci->gpt0_completion);
899 ehci_writel(ehci, STS_GPTIMER0_INTERRUPT, &ehci->regs->status);
900 }
901
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530902 return ehci_irq(hcd);
903}
904
905static int ehci_hsic_reset(struct usb_hcd *hcd)
906{
907 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700908 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530909 int retval;
910
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700911 mehci->timer = USB_HS_GPTIMER_BASE;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530912 ehci->caps = USB_CAPLENGTH;
913 ehci->regs = USB_CAPLENGTH +
914 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
915 dbg_hcs_params(ehci, "reset");
916 dbg_hcc_params(ehci, "reset");
917
918 /* cache the data to minimize the chip reads*/
919 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
920
921 hcd->has_tt = 1;
922 ehci->sbrn = HCD_USB2;
923
924 retval = ehci_halt(ehci);
925 if (retval)
926 return retval;
927
928 /* data structure init */
929 retval = ehci_init(hcd);
930 if (retval)
931 return retval;
932
933 retval = ehci_reset(ehci);
934 if (retval)
935 return retval;
936
937 /* bursts of unspecified length. */
938 writel_relaxed(0, USB_AHBBURST);
939 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530940 writel_relaxed(0x08, USB_AHBMODE);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530941 /* Disable streaming mode and select host mode */
942 writel_relaxed(0x13, USB_USBMODE);
943
944 ehci_port_power(ehci, 1);
945 return 0;
946}
947
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530948#define RESET_RETRY_LIMIT 3
949#define RESET_SIGNAL_TIME_SOF_USEC (50 * 1000)
950#define RESET_SIGNAL_TIME_USEC (20 * 1000)
951static void ehci_hsic_reset_sof_bug_handler(struct usb_hcd *hcd, u32 val)
952{
953 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
954 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
955 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
956 u32 __iomem *status_reg = &ehci->regs->port_status[0];
957 unsigned long flags;
958 int retries = 0, ret, cnt = RESET_SIGNAL_TIME_USEC;
959
960 if (pdata && pdata->swfi_latency)
961 pm_qos_update_request(&mehci->pm_qos_req_dma,
962 pdata->swfi_latency + 1);
963
964 mehci->bus_reset = 1;
965retry:
966 retries++;
967 dbg_log_event(NULL, "RESET: start", retries);
968 pr_debug("reset begin %d\n", retries);
969 mehci->reset_again = 0;
970 spin_lock_irqsave(&ehci->lock, flags);
971 ehci_writel(ehci, val, status_reg);
972 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_USEC - 1),
973 &mehci->timer->gptimer0_ld);
974 ehci_writel(ehci, GPT_RESET | GPT_RUN,
975 &mehci->timer->gptimer0_ctrl);
976 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
977 &ehci->regs->intr_enable);
978
979 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_SOF_USEC - 1),
980 &mehci->timer->gptimer1_ld);
981 ehci_writel(ehci, GPT_RESET | GPT_RUN,
982 &mehci->timer->gptimer1_ctrl);
983
984 spin_unlock_irqrestore(&ehci->lock, flags);
985 wait_for_completion(&mehci->gpt0_completion);
986
987 if (!mehci->reset_again)
988 goto done;
989
990 if (handshake(ehci, status_reg, PORT_RESET, 0, 10 * 1000)) {
991 pr_err("reset handshake fatal error\n");
992 dbg_log_event(NULL, "RESET: fatal", retries);
993 goto fail;
994 }
995
996 if (retries < RESET_RETRY_LIMIT)
997 goto retry;
998
999 /* complete reset in tight loop */
1000 pr_info("RESET in tight loop\n");
1001 dbg_log_event(NULL, "RESET: tight", 0);
1002
1003 spin_lock_irqsave(&ehci->lock, flags);
1004 ehci_writel(ehci, val, status_reg);
1005 while (cnt--)
1006 udelay(1);
1007 ret = msm_hsic_reset_done(hcd);
1008 spin_unlock_irqrestore(&ehci->lock, flags);
1009 if (ret) {
1010 pr_err("RESET in tight loop failed\n");
1011 dbg_log_event(NULL, "RESET: tight failed", 0);
1012 goto fail;
1013 }
1014
1015done:
1016 dbg_log_event(NULL, "RESET: done", retries);
1017 pr_debug("reset completed\n");
1018fail:
1019 mehci->bus_reset = 0;
1020 if (pdata && pdata->swfi_latency)
1021 pm_qos_update_request(&mehci->pm_qos_req_dma,
1022 PM_QOS_DEFAULT_VALUE);
1023}
1024
Hemant Kumar45d211b2012-05-31 17:58:43 -07001025static int ehci_hsic_bus_suspend(struct usb_hcd *hcd)
1026{
Pavankumar Kondeti3c137392012-09-14 14:02:36 +05301027 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1028
1029 if (!(readl_relaxed(USB_PORTSC) & PORT_PE)) {
1030 dbg_log_event(NULL, "RH suspend attempt failed", 0);
1031 dev_dbg(mehci->dev, "%s:port is not enabled skip suspend\n",
1032 __func__);
1033 return -EAGAIN;
1034 }
1035
Hemant Kumar45d211b2012-05-31 17:58:43 -07001036 dbg_log_event(NULL, "Suspend RH", 0);
1037 return ehci_bus_suspend(hcd);
1038}
1039
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001040#define RESUME_RETRY_LIMIT 3
1041#define RESUME_SIGNAL_TIME_MS (21 * 999)
1042#define RESUME_SIGNAL_TIME_SOF_MS (23 * 999)
1043static int msm_hsic_resume_thread(void *data)
1044{
1045 struct msm_hsic_hcd *mehci = data;
1046 struct usb_hcd *hcd = hsic_to_hcd(mehci);
1047 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1048 u32 temp;
1049 unsigned long resume_needed = 0;
1050 int retry_cnt = 0;
1051 int tight_resume = 0;
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301052 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001053
1054 dbg_log_event(NULL, "Resume RH", 0);
1055
1056 /* keep delay between bus states */
1057 if (time_before(jiffies, ehci->next_statechange))
1058 usleep_range(5000, 5000);
1059
1060 spin_lock_irq(&ehci->lock);
1061 if (!HCD_HW_ACCESSIBLE(hcd)) {
1062 spin_unlock_irq(&ehci->lock);
1063 mehci->resume_status = -ESHUTDOWN;
1064 complete(&mehci->rt_completion);
1065 return 0;
1066 }
1067
1068 if (unlikely(ehci->debug)) {
1069 if (!dbgp_reset_prep())
1070 ehci->debug = NULL;
1071 else
1072 dbgp_external_startup();
1073 }
1074
1075 /* at least some APM implementations will try to deliver
1076 * IRQs right away, so delay them until we're ready.
1077 */
1078 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1079
1080 /* re-init operational registers */
1081 ehci_writel(ehci, 0, &ehci->regs->segment);
1082 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
1083 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1084
1085 /*CMD_RUN will be set after, PORT_RESUME gets cleared*/
1086 if (ehci->resume_sof_bug)
1087 ehci->command &= ~CMD_RUN;
1088
1089 /* restore CMD_RUN, framelist size, and irq threshold */
1090 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1091
1092 /* manually resume the ports we suspended during bus_suspend() */
1093resume_again:
1094 if (retry_cnt >= RESUME_RETRY_LIMIT) {
1095 pr_info("retry count(%d) reached max, resume in tight loop\n",
1096 retry_cnt);
1097 tight_resume = 1;
1098 }
1099
1100
1101 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1102 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1103 if (test_bit(0, &ehci->bus_suspended) && (temp & PORT_SUSPEND)) {
1104 temp |= PORT_RESUME;
1105 set_bit(0, &resume_needed);
1106 }
1107 dbg_log_event(NULL, "FPR: Set", temp);
1108 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1109
1110 /* HSIC controller has a h/w bug due to which it can try to send SOFs
1111 * (start of frames) during port resume resulting in phy lockup. HSIC hw
1112 * controller in MSM clears FPR bit after driving the resume signal for
1113 * 20ms. Workaround is to stop SOFs before driving resume and then start
1114 * sending SOFs immediately. Need to send SOFs within 3ms of resume
1115 * completion otherwise peripheral may enter undefined state. As
1116 * usleep_range does not gurantee exact sleep time, GPTimer is used to
1117 * to time the resume sequence. If driver exceeds allowable time SOFs,
1118 * repeat the resume process.
1119 */
1120 if (ehci->resume_sof_bug && resume_needed) {
1121 if (!tight_resume) {
1122 mehci->resume_again = 0;
1123 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_MS),
1124 &mehci->timer->gptimer0_ld);
1125 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1126 &mehci->timer->gptimer0_ctrl);
1127 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1128 &ehci->regs->intr_enable);
1129
1130 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_SOF_MS),
1131 &mehci->timer->gptimer1_ld);
1132 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1133 &mehci->timer->gptimer1_ctrl);
1134
1135 spin_unlock_irq(&ehci->lock);
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301136 if (pdata && pdata->swfi_latency)
1137 pm_qos_update_request(&mehci->pm_qos_req_dma,
1138 pdata->swfi_latency + 1);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001139 wait_for_completion(&mehci->gpt0_completion);
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301140 if (pdata && pdata->swfi_latency)
1141 pm_qos_update_request(&mehci->pm_qos_req_dma,
1142 PM_QOS_DEFAULT_VALUE);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001143 spin_lock_irq(&ehci->lock);
1144 } else {
1145 dbg_log_event(NULL, "FPR: Tightloop", 0);
1146 /* do the resume in a tight loop */
1147 handshake(ehci, &ehci->regs->port_status[0],
1148 PORT_RESUME, 0, 22 * 1000);
1149 ehci_writel(ehci, ehci_readl(ehci,
1150 &ehci->regs->command) | CMD_RUN,
1151 &ehci->regs->command);
1152 }
1153
1154 if (mehci->resume_again) {
1155 int temp;
1156
1157 dbg_log_event(NULL, "FPR: Re-Resume", retry_cnt);
1158 pr_info("FPR: retry count: %d\n", retry_cnt);
1159 spin_unlock_irq(&ehci->lock);
1160 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1161 temp &= ~PORT_RWC_BITS;
1162 temp |= PORT_SUSPEND;
1163 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1164 /* Keep the bus idle for 5ms so that peripheral
1165 * can detect and initiate suspend
1166 */
1167 usleep_range(5000, 5000);
1168 dbg_log_event(NULL,
1169 "FPR: RResume",
1170 ehci_readl(ehci, &ehci->regs->port_status[0]));
1171 spin_lock_irq(&ehci->lock);
1172 mehci->resume_again = 0;
1173 retry_cnt++;
1174 goto resume_again;
1175 }
1176 }
1177
1178 dbg_log_event(NULL, "FPR: RT-Done", 0);
1179 mehci->resume_status = 1;
1180 spin_unlock_irq(&ehci->lock);
1181
1182 complete(&mehci->rt_completion);
1183
1184 return 0;
1185}
1186
Hemant Kumar45d211b2012-05-31 17:58:43 -07001187static int ehci_hsic_bus_resume(struct usb_hcd *hcd)
1188{
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001189 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1190 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1191 u32 temp;
1192 struct task_struct *resume_thread = NULL;
1193
1194 mehci->resume_status = 0;
1195 resume_thread = kthread_run(msm_hsic_resume_thread,
1196 mehci, "hsic_resume_thread");
1197 if (IS_ERR(resume_thread)) {
1198 pr_err("Error creating resume thread:%lu\n",
1199 PTR_ERR(resume_thread));
1200 return PTR_ERR(resume_thread);
1201 }
1202
1203 wait_for_completion(&mehci->rt_completion);
1204
1205 if (mehci->resume_status < 0)
1206 return mehci->resume_status;
1207
1208 dbg_log_event(NULL, "FPR: Wokeup", 0);
1209 spin_lock_irq(&ehci->lock);
1210 (void) ehci_readl(ehci, &ehci->regs->command);
1211
1212 temp = 0;
1213 if (ehci->async->qh_next.qh)
1214 temp |= CMD_ASE;
1215 if (ehci->periodic_sched)
1216 temp |= CMD_PSE;
1217 if (temp) {
1218 ehci->command |= temp;
1219 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1220 }
1221
1222 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
1223 hcd->state = HC_STATE_RUNNING;
1224 ehci->rh_state = EHCI_RH_RUNNING;
1225
1226 /* Now we can safely re-enable irqs */
1227 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1228
1229 spin_unlock_irq(&ehci->lock);
1230
1231 return 0;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001232}
1233
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301234static struct hc_driver msm_hsic_driver = {
1235 .description = hcd_name,
1236 .product_desc = "Qualcomm EHCI Host Controller using HSIC",
1237 .hcd_priv_size = sizeof(struct msm_hsic_hcd),
1238
1239 /*
1240 * generic hardware linkage
1241 */
1242 .irq = msm_hsic_irq,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001243 .flags = HCD_USB2 | HCD_MEMORY | HCD_OLD_ENUM,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301244
1245 .reset = ehci_hsic_reset,
1246 .start = ehci_run,
1247
1248 .stop = ehci_stop,
1249 .shutdown = ehci_shutdown,
1250
1251 /*
1252 * managing i/o requests and associated device resources
1253 */
Hemant Kumardf2d84d2012-08-15 09:06:35 -07001254 .urb_enqueue = ehci_urb_enqueue,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301255 .urb_dequeue = ehci_urb_dequeue,
1256 .endpoint_disable = ehci_endpoint_disable,
1257 .endpoint_reset = ehci_endpoint_reset,
1258 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1259
1260 /*
1261 * scheduling support
1262 */
1263 .get_frame_number = ehci_get_frame,
1264
1265 /*
1266 * root hub support
1267 */
1268 .hub_status_data = ehci_hub_status_data,
1269 .hub_control = ehci_hub_control,
1270 .relinquish_port = ehci_relinquish_port,
1271 .port_handed_over = ehci_port_handed_over,
1272
1273 /*
1274 * PM support
1275 */
Hemant Kumar45d211b2012-05-31 17:58:43 -07001276 .bus_suspend = ehci_hsic_bus_suspend,
1277 .bus_resume = ehci_hsic_bus_resume,
1278
Hemant Kumardf2d84d2012-08-15 09:06:35 -07001279 .log_urb = dbg_log_event,
Hemant Kumar105d07f2012-07-02 15:33:07 -07001280 .dump_regs = dump_hsic_regs,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001281
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +05301282 .reset_sof_bug_handler = ehci_hsic_reset_sof_bug_handler,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301283};
1284
1285static int msm_hsic_init_clocks(struct msm_hsic_hcd *mehci, u32 init)
1286{
1287 int ret = 0;
1288
1289 if (!init)
1290 goto put_clocks;
1291
Lena Salman8c8ba382012-02-14 15:59:31 +02001292 /*core_clk is required for LINK protocol engine
1293 *clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001294 mehci->core_clk = clk_get(mehci->dev, "core_clk");
1295 if (IS_ERR(mehci->core_clk)) {
1296 dev_err(mehci->dev, "failed to get core_clk\n");
1297 ret = PTR_ERR(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301298 return ret;
1299 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301300
Lena Salman8c8ba382012-02-14 15:59:31 +02001301 /* alt_core_clk is for LINK to be used during PHY RESET
1302 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001303 mehci->alt_core_clk = clk_get(mehci->dev, "alt_core_clk");
1304 if (IS_ERR(mehci->alt_core_clk)) {
1305 dev_err(mehci->dev, "failed to core_clk\n");
1306 ret = PTR_ERR(mehci->alt_core_clk);
1307 goto put_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301308 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301309
Lena Salman8c8ba382012-02-14 15:59:31 +02001310 /* phy_clk is required for HSIC PHY operation
1311 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001312 mehci->phy_clk = clk_get(mehci->dev, "phy_clk");
1313 if (IS_ERR(mehci->phy_clk)) {
1314 dev_err(mehci->dev, "failed to get phy_clk\n");
1315 ret = PTR_ERR(mehci->phy_clk);
1316 goto put_alt_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301317 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301318
1319 /* 10MHz cal_clk is required for calibration of I/O pads */
Manu Gautam5143b252012-01-05 19:25:23 -08001320 mehci->cal_clk = clk_get(mehci->dev, "cal_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301321 if (IS_ERR(mehci->cal_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001322 dev_err(mehci->dev, "failed to get cal_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301323 ret = PTR_ERR(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001324 goto put_phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301325 }
1326 clk_set_rate(mehci->cal_clk, 10000000);
1327
1328 /* ahb_clk is required for data transfers */
Manu Gautam5143b252012-01-05 19:25:23 -08001329 mehci->ahb_clk = clk_get(mehci->dev, "iface_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301330 if (IS_ERR(mehci->ahb_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001331 dev_err(mehci->dev, "failed to get iface_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301332 ret = PTR_ERR(mehci->ahb_clk);
1333 goto put_cal_clk;
1334 }
1335
Manu Gautam28b1bac2012-01-30 16:43:06 +05301336 clk_prepare_enable(mehci->core_clk);
1337 clk_prepare_enable(mehci->phy_clk);
1338 clk_prepare_enable(mehci->cal_clk);
1339 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301340
1341 return 0;
1342
1343put_clocks:
Jack Phamfd193eb2012-02-22 15:38:08 -08001344 if (!atomic_read(&mehci->in_lpm)) {
1345 clk_disable_unprepare(mehci->core_clk);
1346 clk_disable_unprepare(mehci->phy_clk);
1347 clk_disable_unprepare(mehci->cal_clk);
1348 clk_disable_unprepare(mehci->ahb_clk);
1349 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301350 clk_put(mehci->ahb_clk);
1351put_cal_clk:
1352 clk_put(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001353put_phy_clk:
1354 clk_put(mehci->phy_clk);
1355put_alt_core_clk:
1356 clk_put(mehci->alt_core_clk);
1357put_core_clk:
1358 clk_put(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301359
1360 return ret;
1361}
Vamsi Krishna34f01582011-12-14 19:54:42 -08001362static irqreturn_t hsic_peripheral_status_change(int irq, void *dev_id)
1363{
1364 struct msm_hsic_hcd *mehci = dev_id;
1365
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001366 pr_debug("%s: mehci:%p dev_id:%p\n", __func__, mehci, dev_id);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001367
1368 if (mehci)
1369 msm_hsic_config_gpios(mehci, 0);
1370
1371 return IRQ_HANDLED;
1372}
1373
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001374static irqreturn_t msm_hsic_wakeup_irq(int irq, void *data)
1375{
1376 struct msm_hsic_hcd *mehci = data;
Ajay Dudanid666daf2012-09-27 12:04:12 +05301377 int ret;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001378
Hemant Kumar6fd65032012-05-23 13:02:24 -07001379 mehci->wakeup_int_cnt++;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001380 dbg_log_event(NULL, "Remote Wakeup IRQ", mehci->wakeup_int_cnt);
Hemant Kumar6fd65032012-05-23 13:02:24 -07001381 dev_dbg(mehci->dev, "%s: hsic remote wakeup interrupt cnt: %u\n",
1382 __func__, mehci->wakeup_int_cnt);
1383
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001384 wake_lock(&mehci->wlock);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001385
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001386 spin_lock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001387 if (mehci->wakeup_irq_enabled) {
1388 mehci->wakeup_irq_enabled = 0;
1389 disable_irq_wake(irq);
1390 disable_irq_nosync(irq);
1391 }
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001392 spin_unlock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001393
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001394 if (!atomic_read(&mehci->pm_usage_cnt)) {
Ajay Dudanid666daf2012-09-27 12:04:12 +05301395 ret = pm_runtime_get(mehci->dev);
1396 /*
1397 * HSIC runtime resume can race with us.
1398 * if we are active (ret == 1) or resuming
1399 * (ret == -EINPROGRESS), decrement the
1400 * PM usage counter before returning.
1401 */
1402 if ((ret == 1) || (ret == -EINPROGRESS))
1403 pm_runtime_put_noidle(mehci->dev);
1404 else
1405 atomic_set(&mehci->pm_usage_cnt, 1);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001406 }
1407
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001408 return IRQ_HANDLED;
1409}
1410
Hemant Kumare6275972012-02-29 20:06:21 -08001411static int ehci_hsic_msm_bus_show(struct seq_file *s, void *unused)
1412{
1413 if (debug_bus_voting_enabled)
1414 seq_printf(s, "enabled\n");
1415 else
1416 seq_printf(s, "disabled\n");
1417
1418 return 0;
1419}
1420
1421static int ehci_hsic_msm_bus_open(struct inode *inode, struct file *file)
1422{
1423 return single_open(file, ehci_hsic_msm_bus_show, inode->i_private);
1424}
1425
1426static ssize_t ehci_hsic_msm_bus_write(struct file *file,
1427 const char __user *ubuf, size_t count, loff_t *ppos)
1428{
1429 char buf[8];
1430 int ret;
1431 struct seq_file *s = file->private_data;
1432 struct msm_hsic_hcd *mehci = s->private;
1433
1434 memset(buf, 0x00, sizeof(buf));
1435
1436 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1437 return -EFAULT;
1438
1439 if (!strncmp(buf, "enable", 6)) {
1440 /* Do not vote here. Let hsic driver decide when to vote */
1441 debug_bus_voting_enabled = true;
1442 } else {
1443 debug_bus_voting_enabled = false;
1444 if (mehci->bus_perf_client) {
1445 ret = msm_bus_scale_client_update_request(
1446 mehci->bus_perf_client, 0);
1447 if (ret)
1448 dev_err(mehci->dev, "%s: Failed to devote "
1449 "for bus bw %d\n", __func__, ret);
1450 }
1451 }
1452
1453 return count;
1454}
1455
1456const struct file_operations ehci_hsic_msm_bus_fops = {
1457 .open = ehci_hsic_msm_bus_open,
1458 .read = seq_read,
1459 .write = ehci_hsic_msm_bus_write,
1460 .llseek = seq_lseek,
1461 .release = single_release,
1462};
1463
Hemant Kumar6fd65032012-05-23 13:02:24 -07001464static int ehci_hsic_msm_wakeup_cnt_show(struct seq_file *s, void *unused)
1465{
1466 struct msm_hsic_hcd *mehci = s->private;
1467
1468 seq_printf(s, "%u\n", mehci->wakeup_int_cnt);
1469
1470 return 0;
1471}
1472
1473static int ehci_hsic_msm_wakeup_cnt_open(struct inode *inode, struct file *f)
1474{
1475 return single_open(f, ehci_hsic_msm_wakeup_cnt_show, inode->i_private);
1476}
1477
1478const struct file_operations ehci_hsic_msm_wakeup_cnt_fops = {
1479 .open = ehci_hsic_msm_wakeup_cnt_open,
1480 .read = seq_read,
1481 .llseek = seq_lseek,
1482 .release = single_release,
1483};
1484
Hemant Kumar45d211b2012-05-31 17:58:43 -07001485static int ehci_hsic_msm_data_events_show(struct seq_file *s, void *unused)
1486{
1487 unsigned long flags;
1488 unsigned i;
1489
1490 read_lock_irqsave(&dbg_hsic_data.lck, flags);
1491
1492 i = dbg_hsic_data.idx;
1493 for (dbg_inc(&i); i != dbg_hsic_data.idx; dbg_inc(&i)) {
1494 if (!strnlen(dbg_hsic_data.buf[i], DBG_MSG_LEN))
1495 continue;
1496 seq_printf(s, "%s\n", dbg_hsic_data.buf[i]);
1497 }
1498
1499 read_unlock_irqrestore(&dbg_hsic_data.lck, flags);
1500
1501 return 0;
1502}
1503
1504static int ehci_hsic_msm_data_events_open(struct inode *inode, struct file *f)
1505{
1506 return single_open(f, ehci_hsic_msm_data_events_show, inode->i_private);
1507}
1508
1509const struct file_operations ehci_hsic_msm_dbg_data_fops = {
1510 .open = ehci_hsic_msm_data_events_open,
1511 .read = seq_read,
1512 .llseek = seq_lseek,
1513 .release = single_release,
1514};
1515
1516static int ehci_hsic_msm_ctrl_events_show(struct seq_file *s, void *unused)
1517{
1518 unsigned long flags;
1519 unsigned i;
1520
1521 read_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
1522
1523 i = dbg_hsic_ctrl.idx;
1524 for (dbg_inc(&i); i != dbg_hsic_ctrl.idx; dbg_inc(&i)) {
1525 if (!strnlen(dbg_hsic_ctrl.buf[i], DBG_MSG_LEN))
1526 continue;
1527 seq_printf(s, "%s\n", dbg_hsic_ctrl.buf[i]);
1528 }
1529
1530 read_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
1531
1532 return 0;
1533}
1534
1535static int ehci_hsic_msm_ctrl_events_open(struct inode *inode, struct file *f)
1536{
1537 return single_open(f, ehci_hsic_msm_ctrl_events_show, inode->i_private);
1538}
1539
1540const struct file_operations ehci_hsic_msm_dbg_ctrl_fops = {
1541 .open = ehci_hsic_msm_ctrl_events_open,
1542 .read = seq_read,
1543 .llseek = seq_lseek,
1544 .release = single_release,
1545};
1546
Hemant Kumare6275972012-02-29 20:06:21 -08001547static struct dentry *ehci_hsic_msm_dbg_root;
1548static int ehci_hsic_msm_debugfs_init(struct msm_hsic_hcd *mehci)
1549{
1550 struct dentry *ehci_hsic_msm_dentry;
1551
1552 ehci_hsic_msm_dbg_root = debugfs_create_dir("ehci_hsic_msm_dbg", NULL);
1553
1554 if (!ehci_hsic_msm_dbg_root || IS_ERR(ehci_hsic_msm_dbg_root))
1555 return -ENODEV;
1556
1557 ehci_hsic_msm_dentry = debugfs_create_file("bus_voting",
1558 S_IRUGO | S_IWUSR,
1559 ehci_hsic_msm_dbg_root, mehci,
1560 &ehci_hsic_msm_bus_fops);
1561
1562 if (!ehci_hsic_msm_dentry) {
1563 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1564 return -ENODEV;
1565 }
1566
Hemant Kumar6fd65032012-05-23 13:02:24 -07001567 ehci_hsic_msm_dentry = debugfs_create_file("wakeup_cnt",
1568 S_IRUGO,
1569 ehci_hsic_msm_dbg_root, mehci,
1570 &ehci_hsic_msm_wakeup_cnt_fops);
1571
1572 if (!ehci_hsic_msm_dentry) {
1573 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1574 return -ENODEV;
1575 }
1576
Hemant Kumar45d211b2012-05-31 17:58:43 -07001577 ehci_hsic_msm_dentry = debugfs_create_file("show_ctrl_events",
1578 S_IRUGO,
1579 ehci_hsic_msm_dbg_root, mehci,
1580 &ehci_hsic_msm_dbg_ctrl_fops);
1581
1582 if (!ehci_hsic_msm_dentry) {
1583 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1584 return -ENODEV;
1585 }
1586
1587 ehci_hsic_msm_dentry = debugfs_create_file("show_data_events",
1588 S_IRUGO,
1589 ehci_hsic_msm_dbg_root, mehci,
1590 &ehci_hsic_msm_dbg_data_fops);
1591
1592 if (!ehci_hsic_msm_dentry) {
1593 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1594 return -ENODEV;
1595 }
1596
Hemant Kumare6275972012-02-29 20:06:21 -08001597 return 0;
1598}
1599
1600static void ehci_hsic_msm_debugfs_cleanup(void)
1601{
1602 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1603}
1604
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301605static int __devinit ehci_hsic_msm_probe(struct platform_device *pdev)
1606{
1607 struct usb_hcd *hcd;
1608 struct resource *res;
1609 struct msm_hsic_hcd *mehci;
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +05301610 struct msm_hsic_host_platform_data *pdata;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301611 int ret;
1612
1613 dev_dbg(&pdev->dev, "ehci_msm-hsic probe\n");
1614
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301615 /* After parent device's probe is executed, it will be put in suspend
1616 * mode. When child device's probe is called, driver core is not
1617 * resuming parent device due to which parent will be in suspend even
1618 * though child is active. Hence resume the parent device explicitly.
1619 */
1620 if (pdev->dev.parent)
1621 pm_runtime_get_sync(pdev->dev.parent);
1622
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301623 hcd = usb_create_hcd(&msm_hsic_driver, &pdev->dev,
1624 dev_name(&pdev->dev));
1625 if (!hcd) {
1626 dev_err(&pdev->dev, "Unable to create HCD\n");
1627 return -ENOMEM;
1628 }
1629
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301630 hcd_to_bus(hcd)->skip_resume = true;
1631
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301632 hcd->irq = platform_get_irq(pdev, 0);
1633 if (hcd->irq < 0) {
1634 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1635 ret = hcd->irq;
1636 goto put_hcd;
1637 }
1638
1639 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1640 if (!res) {
1641 dev_err(&pdev->dev, "Unable to get memory resource\n");
1642 ret = -ENODEV;
1643 goto put_hcd;
1644 }
1645
1646 hcd->rsrc_start = res->start;
1647 hcd->rsrc_len = resource_size(res);
1648 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1649 if (!hcd->regs) {
1650 dev_err(&pdev->dev, "ioremap failed\n");
1651 ret = -ENOMEM;
1652 goto put_hcd;
1653 }
1654
1655 mehci = hcd_to_hsic(hcd);
1656 mehci->dev = &pdev->dev;
Ajay Dudanic4e40db2012-08-20 14:44:40 -07001657 pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301658
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001659 spin_lock_init(&mehci->wakeup_lock);
1660
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001661 mehci->ehci.susp_sof_bug = 1;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001662 mehci->ehci.reset_sof_bug = 1;
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001663
Hemant Kumare4040492012-06-21 17:35:42 -07001664 mehci->ehci.resume_sof_bug = 1;
1665
Ajay Dudanic4e40db2012-08-20 14:44:40 -07001666 if (pdata)
1667 mehci->ehci.log2_irq_thresh = pdata->log2_irq_thresh;
Hemant Kumar933e0402012-05-22 11:11:40 -07001668
Vamsi Krishna34f01582011-12-14 19:54:42 -08001669 res = platform_get_resource_byname(pdev,
1670 IORESOURCE_IRQ,
1671 "peripheral_status_irq");
1672 if (res)
1673 mehci->peripheral_status_irq = res->start;
1674
Hemant Kumar6fd65032012-05-23 13:02:24 -07001675 res = platform_get_resource_byname(pdev, IORESOURCE_IO, "wakeup");
1676 if (res) {
1677 mehci->wakeup_gpio = res->start;
1678 mehci->wakeup_irq = MSM_GPIO_TO_INT(res->start);
1679 dev_dbg(mehci->dev, "wakeup_irq: %d\n", mehci->wakeup_irq);
1680 }
1681
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301682 ret = msm_hsic_init_clocks(mehci, 1);
1683 if (ret) {
1684 dev_err(&pdev->dev, "unable to initialize clocks\n");
1685 ret = -ENODEV;
1686 goto unmap;
1687 }
1688
1689 ret = msm_hsic_init_vddcx(mehci, 1);
1690 if (ret) {
1691 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
1692 ret = -ENODEV;
1693 goto deinit_clocks;
1694 }
1695
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001696 init_completion(&mehci->rt_completion);
1697 init_completion(&mehci->gpt0_completion);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301698 ret = msm_hsic_reset(mehci);
1699 if (ret) {
1700 dev_err(&pdev->dev, "unable to initialize PHY\n");
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301701 goto deinit_vddcx;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301702 }
1703
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001704 ehci_wq = create_singlethread_workqueue("ehci_wq");
1705 if (!ehci_wq) {
1706 dev_err(&pdev->dev, "unable to create workqueue\n");
1707 ret = -ENOMEM;
1708 goto deinit_vddcx;
1709 }
1710
1711 INIT_WORK(&mehci->bus_vote_w, ehci_hsic_bus_vote_w);
1712
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301713 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
1714 if (ret) {
1715 dev_err(&pdev->dev, "unable to register HCD\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301716 goto unconfig_gpio;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301717 }
1718
1719 device_init_wakeup(&pdev->dev, 1);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001720 wake_lock_init(&mehci->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
1721 wake_lock(&mehci->wlock);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001722
1723 if (mehci->peripheral_status_irq) {
1724 ret = request_threaded_irq(mehci->peripheral_status_irq,
1725 NULL, hsic_peripheral_status_change,
1726 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1727 | IRQF_SHARED,
1728 "hsic_peripheral_status", mehci);
1729 if (ret)
1730 dev_err(&pdev->dev, "%s:request_irq:%d failed:%d",
1731 __func__, mehci->peripheral_status_irq, ret);
1732 }
1733
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001734 /* configure wakeup irq */
Hemant Kumar6fd65032012-05-23 13:02:24 -07001735 if (mehci->wakeup_irq) {
Hemant Kumar5e386632012-08-30 14:23:38 -07001736 /* In case if wakeup gpio is pulled high at this point
1737 * remote wakeup interrupt fires right after request_irq.
1738 * Remote wake up interrupt only needs to be enabled when
1739 * HSIC bus goes to suspend.
1740 */
1741 irq_set_status_flags(mehci->wakeup_irq, IRQ_NOAUTOEN);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001742 ret = request_irq(mehci->wakeup_irq, msm_hsic_wakeup_irq,
Hemant Kumar6fd65032012-05-23 13:02:24 -07001743 IRQF_TRIGGER_HIGH,
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001744 "msm_hsic_wakeup", mehci);
Hemant Kumar5e386632012-08-30 14:23:38 -07001745 if (ret) {
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001746 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
1747 mehci->wakeup_irq, ret);
1748 mehci->wakeup_irq = 0;
1749 }
1750 }
1751
Hemant Kumare6275972012-02-29 20:06:21 -08001752 ret = ehci_hsic_msm_debugfs_init(mehci);
1753 if (ret)
1754 dev_dbg(&pdev->dev, "mode debugfs file is"
1755 "not available\n");
1756
1757 if (pdata && pdata->bus_scale_table) {
1758 mehci->bus_perf_client =
1759 msm_bus_scale_register_client(pdata->bus_scale_table);
1760 /* Configure BUS performance parameters for MAX bandwidth */
1761 if (mehci->bus_perf_client) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001762 mehci->bus_vote = true;
1763 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -08001764 } else {
1765 dev_err(&pdev->dev, "%s: Failed to register BUS "
1766 "scaling client!!\n", __func__);
1767 }
1768 }
1769
Hemant Kumar105d07f2012-07-02 15:33:07 -07001770 __mehci = mehci;
1771
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301772 if (pdata && pdata->swfi_latency)
1773 pm_qos_add_request(&mehci->pm_qos_req_dma,
1774 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1775
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301776 /*
1777 * This pdev->dev is assigned parent of root-hub by USB core,
1778 * hence, runtime framework automatically calls this driver's
1779 * runtime APIs based on root-hub's state.
1780 */
1781 pm_runtime_set_active(&pdev->dev);
1782 pm_runtime_enable(&pdev->dev);
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301783 /* Decrement the parent device's counter after probe.
1784 * As child is active, parent will not be put into
1785 * suspend mode.
1786 */
1787 if (pdev->dev.parent)
1788 pm_runtime_put_sync(pdev->dev.parent);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301789
1790 return 0;
1791
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301792unconfig_gpio:
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001793 destroy_workqueue(ehci_wq);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301794 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301795deinit_vddcx:
1796 msm_hsic_init_vddcx(mehci, 0);
1797deinit_clocks:
1798 msm_hsic_init_clocks(mehci, 0);
1799unmap:
1800 iounmap(hcd->regs);
1801put_hcd:
1802 usb_put_hcd(hcd);
1803
1804 return ret;
1805}
1806
1807static int __devexit ehci_hsic_msm_remove(struct platform_device *pdev)
1808{
1809 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1810 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301811 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1812
1813 if (pdata && pdata->swfi_latency)
1814 pm_qos_remove_request(&mehci->pm_qos_req_dma);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301815
Vamsi Krishna34f01582011-12-14 19:54:42 -08001816 if (mehci->peripheral_status_irq)
1817 free_irq(mehci->peripheral_status_irq, mehci);
Jack Phamfe441ea2012-03-23 17:03:15 -07001818
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001819 if (mehci->wakeup_irq) {
Jack Phamfe441ea2012-03-23 17:03:15 -07001820 if (mehci->wakeup_irq_enabled)
1821 disable_irq_wake(mehci->wakeup_irq);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001822 free_irq(mehci->wakeup_irq, mehci);
1823 }
Vamsi Krishna34f01582011-12-14 19:54:42 -08001824
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001825 /*
1826 * If the update request is called after unregister, the request will
1827 * fail. Results are undefined if unregister is called in the middle of
1828 * update request.
1829 */
1830 mehci->bus_vote = false;
1831 cancel_work_sync(&mehci->bus_vote_w);
1832
Hemant Kumare6275972012-02-29 20:06:21 -08001833 if (mehci->bus_perf_client)
1834 msm_bus_scale_unregister_client(mehci->bus_perf_client);
1835
1836 ehci_hsic_msm_debugfs_cleanup();
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301837 device_init_wakeup(&pdev->dev, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301838 pm_runtime_set_suspended(&pdev->dev);
1839
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001840 destroy_workqueue(ehci_wq);
1841
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301842 usb_remove_hcd(hcd);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301843 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301844 msm_hsic_init_vddcx(mehci, 0);
1845
1846 msm_hsic_init_clocks(mehci, 0);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001847 wake_lock_destroy(&mehci->wlock);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301848 iounmap(hcd->regs);
1849 usb_put_hcd(hcd);
1850
1851 return 0;
1852}
1853
1854#ifdef CONFIG_PM_SLEEP
1855static int msm_hsic_pm_suspend(struct device *dev)
1856{
Jack Phambe05fbb2012-05-16 10:56:26 -07001857 int ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301858 struct usb_hcd *hcd = dev_get_drvdata(dev);
1859 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1860
1861 dev_dbg(dev, "ehci-msm-hsic PM suspend\n");
1862
Hemant Kumar45d211b2012-05-31 17:58:43 -07001863 dbg_log_event(NULL, "PM Suspend", 0);
1864
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301865 if (device_may_wakeup(dev))
1866 enable_irq_wake(hcd->irq);
1867
Jack Phambe05fbb2012-05-16 10:56:26 -07001868 ret = msm_hsic_suspend(mehci);
1869
1870 if (ret && device_may_wakeup(dev))
1871 disable_irq_wake(hcd->irq);
1872
1873 return ret;
Jack Phamfe441ea2012-03-23 17:03:15 -07001874}
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301875
Jack Pham16b06f82012-08-14 20:03:59 -07001876static int msm_hsic_pm_suspend_noirq(struct device *dev)
1877{
1878 struct usb_hcd *hcd = dev_get_drvdata(dev);
1879 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1880
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05301881 if (atomic_read(&mehci->async_int)) {
Jack Pham16b06f82012-08-14 20:03:59 -07001882 dev_dbg(dev, "suspend_noirq: Aborting due to pending interrupt\n");
1883 return -EBUSY;
1884 }
1885
1886 return 0;
1887}
1888
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301889static int msm_hsic_pm_resume(struct device *dev)
1890{
1891 int ret;
1892 struct usb_hcd *hcd = dev_get_drvdata(dev);
1893 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1894
Jack Pham16b06f82012-08-14 20:03:59 -07001895 dev_dbg(dev, "ehci-msm-hsic PM resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001896 dbg_log_event(NULL, "PM Resume", 0);
1897
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301898 if (device_may_wakeup(dev))
1899 disable_irq_wake(hcd->irq);
1900
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301901 /*
1902 * Keep HSIC in Low Power Mode if system is resumed
1903 * by any other wakeup source. HSIC is resumed later
1904 * when remote wakeup is received or interface driver
1905 * start I/O.
1906 */
Pavankumar Kondeti98089c02012-11-09 10:54:00 +05301907 if (!atomic_read(&mehci->pm_usage_cnt) &&
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05301908 !atomic_read(&mehci->async_int) &&
Pavankumar Kondeti98089c02012-11-09 10:54:00 +05301909 pm_runtime_suspended(dev))
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301910 return 0;
1911
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301912 ret = msm_hsic_resume(mehci);
1913 if (ret)
1914 return ret;
1915
1916 /* Bring the device to full powered state upon system resume */
1917 pm_runtime_disable(dev);
1918 pm_runtime_set_active(dev);
1919 pm_runtime_enable(dev);
1920
1921 return 0;
1922}
1923#endif
1924
1925#ifdef CONFIG_PM_RUNTIME
1926static int msm_hsic_runtime_idle(struct device *dev)
1927{
1928 dev_dbg(dev, "EHCI runtime idle\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301929 return 0;
1930}
1931
1932static int msm_hsic_runtime_suspend(struct device *dev)
1933{
1934 struct usb_hcd *hcd = dev_get_drvdata(dev);
1935 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1936
1937 dev_dbg(dev, "EHCI runtime suspend\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001938
1939 dbg_log_event(NULL, "Run Time PM Suspend", 0);
1940
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301941 return msm_hsic_suspend(mehci);
1942}
1943
1944static int msm_hsic_runtime_resume(struct device *dev)
1945{
1946 struct usb_hcd *hcd = dev_get_drvdata(dev);
1947 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1948
1949 dev_dbg(dev, "EHCI runtime resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001950
1951 dbg_log_event(NULL, "Run Time PM Resume", 0);
1952
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301953 return msm_hsic_resume(mehci);
1954}
1955#endif
1956
1957#ifdef CONFIG_PM
1958static const struct dev_pm_ops msm_hsic_dev_pm_ops = {
1959 SET_SYSTEM_SLEEP_PM_OPS(msm_hsic_pm_suspend, msm_hsic_pm_resume)
Jack Pham16b06f82012-08-14 20:03:59 -07001960 .suspend_noirq = msm_hsic_pm_suspend_noirq,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301961 SET_RUNTIME_PM_OPS(msm_hsic_runtime_suspend, msm_hsic_runtime_resume,
1962 msm_hsic_runtime_idle)
1963};
1964#endif
1965
1966static struct platform_driver ehci_msm_hsic_driver = {
1967 .probe = ehci_hsic_msm_probe,
1968 .remove = __devexit_p(ehci_hsic_msm_remove),
1969 .driver = {
1970 .name = "msm_hsic_host",
1971#ifdef CONFIG_PM
1972 .pm = &msm_hsic_dev_pm_ops,
1973#endif
1974 },
1975};