blob: 0ea977865f986543be76afb87c39fa85207f2cf0 [file] [log] [blame]
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301/* ehci-msm-hsic.c - HSUSB Host Controller Driver Implementation
2 *
Ido Shayevitz95c46882013-01-20 13:47:46 +02003 * Copyright (c) 2011-2013, The 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>
Sudhir Sharma1673e302012-08-27 17:37:24 -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
Manu Gautam863b74132012-11-21 14:30:04 +053034#include <linux/usb/ulpi.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053035#include <linux/usb/msm_hsusb_hw.h>
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +053036#include <linux/usb/msm_hsusb.h>
37#include <linux/gpio.h>
Manu Gautam863b74132012-11-21 14:30:04 +053038#include <linux/of_gpio.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030039#include <linux/spinlock.h>
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -070040#include <linux/irq.h>
Vamsi Krishnafce1bd22012-08-17 17:12:26 -070041#include <linux/kthread.h>
42#include <linux/wait.h>
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +053043#include <linux/pm_qos.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030044
45#include <mach/msm_bus.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053046#include <mach/clk.h>
47#include <mach/msm_iomap.h>
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +053048#include <mach/msm_xo.h>
Vamsi Krishna34f01582011-12-14 19:54:42 -080049#include <linux/spinlock.h>
Hemant Kumar45d211b2012-05-31 17:58:43 -070050#include <linux/cpu.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030051#include <mach/rpm-regulator.h>
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +020052#include "hbm.c"
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053053
54#define MSM_USB_BASE (hcd->regs)
Hemant Kumar105d07f2012-07-02 15:33:07 -070055#define USB_REG_START_OFFSET 0x90
56#define USB_REG_END_OFFSET 0x250
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053057
Hemant Kumar2309eaa2012-08-14 16:46:42 -070058static struct workqueue_struct *ehci_wq;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -070059struct ehci_timer {
60#define GPT_LD(p) ((p) & 0x00FFFFFF)
61 u32 gptimer0_ld;
62#define GPT_RUN BIT(31)
63#define GPT_RESET BIT(30)
64#define GPT_MODE BIT(24)
65#define GPT_CNT(p) ((p) & 0x00FFFFFF)
66 u32 gptimer0_ctrl;
67
68 u32 gptimer1_ld;
69 u32 gptimer1_ctrl;
70};
Hemant Kumar2309eaa2012-08-14 16:46:42 -070071
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053072struct msm_hsic_hcd {
73 struct ehci_hcd ehci;
Hemant Kumard343c012012-09-06 19:57:14 -070074 spinlock_t wakeup_lock;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053075 struct device *dev;
76 struct clk *ahb_clk;
Manu Gautam5143b252012-01-05 19:25:23 -080077 struct clk *core_clk;
78 struct clk *alt_core_clk;
79 struct clk *phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053080 struct clk *cal_clk;
Ido Shayevitz5dfbb132013-04-18 13:22:07 +030081 struct clk *inactivity_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053082 struct regulator *hsic_vddcx;
Ofir Cohen4cc55372012-11-20 11:17:24 +020083 struct regulator *hsic_gdsc;
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +053084 atomic_t async_int;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053085 atomic_t in_lpm;
Sudhir Sharma1673e302012-08-27 17:37:24 -070086 struct wake_lock wlock;
Vamsi Krishna34f01582011-12-14 19:54:42 -080087 int peripheral_status_irq;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -080088 int wakeup_irq;
Jack Phamfe441ea2012-03-23 17:03:15 -070089 bool wakeup_irq_enabled;
Manu Gautam3fb60ca2013-02-13 18:33:33 +053090 int async_irq;
91 uint32_t async_int_cnt;
Sudhir Sharma1673e302012-08-27 17:37:24 -070092 atomic_t pm_usage_cnt;
Hemant Kumare6275972012-02-29 20:06:21 -080093 uint32_t bus_perf_client;
Hemant Kumar6fd65032012-05-23 13:02:24 -070094 uint32_t wakeup_int_cnt;
Amit Blayd6ea6102012-06-07 16:26:24 +030095 enum usb_vdd_type vdd_type;
Hemant Kumar2309eaa2012-08-14 16:46:42 -070096
97 struct work_struct bus_vote_w;
98 bool bus_vote;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -070099
100 /* gp timer */
101 struct ehci_timer __iomem *timer;
102 struct completion gpt0_completion;
103 struct completion rt_completion;
104 int resume_status;
105 int resume_again;
Pavankumar Kondeti23720492013-01-29 14:41:58 +0530106 int bus_reset;
107 int reset_again;
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +0530108
109 struct pm_qos_request pm_qos_req_dma;
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +0200110 unsigned enable_hbm:1;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530111};
112
Hemant Kumar105d07f2012-07-02 15:33:07 -0700113struct msm_hsic_hcd *__mehci;
114
Hemant Kumare6275972012-02-29 20:06:21 -0800115static bool debug_bus_voting_enabled = true;
Ofir Cohen4cc55372012-11-20 11:17:24 +0200116static u64 ehci_msm_hsic_dma_mask = DMA_BIT_MASK(32);
117
Manu Gautam863b74132012-11-21 14:30:04 +0530118static struct platform_driver ehci_msm_hsic_driver;
Hemant Kumar45d211b2012-05-31 17:58:43 -0700119
Hemant Kumar4d50a432012-08-15 09:06:35 -0700120static unsigned int enable_payload_log = 1;
121module_param(enable_payload_log, uint, S_IRUGO | S_IWUSR);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700122static unsigned int enable_dbg_log = 1;
123module_param(enable_dbg_log, uint, S_IRUGO | S_IWUSR);
124/*by default log ep0 and efs sync ep*/
125static unsigned int ep_addr_rxdbg_mask = 9;
126module_param(ep_addr_rxdbg_mask, uint, S_IRUGO | S_IWUSR);
127static unsigned int ep_addr_txdbg_mask = 9;
128module_param(ep_addr_txdbg_mask, uint, S_IRUGO | S_IWUSR);
129
130/* Maximum debug message length */
Hemant Kumar4d50a432012-08-15 09:06:35 -0700131#define DBG_MSG_LEN 128UL
Hemant Kumar45d211b2012-05-31 17:58:43 -0700132
133/* Maximum number of messages */
134#define DBG_MAX_MSG 256UL
135
136#define TIME_BUF_LEN 20
Hemant Kumar4d50a432012-08-15 09:06:35 -0700137#define HEX_DUMP_LEN 72
Hemant Kumar45d211b2012-05-31 17:58:43 -0700138
139enum event_type {
140 EVENT_UNDEF = -1,
141 URB_SUBMIT,
142 URB_COMPLETE,
143 EVENT_NONE,
144};
145
146#define EVENT_STR_LEN 5
147
Hemant Kumar45d211b2012-05-31 17:58:43 -0700148static enum event_type str_to_event(const char *name)
149{
150 if (!strncasecmp("S", name, EVENT_STR_LEN))
151 return URB_SUBMIT;
152 if (!strncasecmp("C", name, EVENT_STR_LEN))
153 return URB_COMPLETE;
154 if (!strncasecmp("", name, EVENT_STR_LEN))
155 return EVENT_NONE;
156
157 return EVENT_UNDEF;
158}
159
160/*log ep0 activity*/
161static struct {
162 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
163 unsigned idx; /* index */
164 rwlock_t lck; /* lock */
165} dbg_hsic_ctrl = {
166 .idx = 0,
167 .lck = __RW_LOCK_UNLOCKED(lck)
168};
169
170static struct {
171 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
172 unsigned idx; /* index */
173 rwlock_t lck; /* lock */
174} dbg_hsic_data = {
175 .idx = 0,
176 .lck = __RW_LOCK_UNLOCKED(lck)
177};
178
179/**
180 * dbg_inc: increments debug event index
181 * @idx: buffer index
182 */
183static void dbg_inc(unsigned *idx)
184{
185 *idx = (*idx + 1) & (DBG_MAX_MSG-1);
186}
187
188/*get_timestamp - returns time of day in us */
189static char *get_timestamp(char *tbuf)
190{
191 unsigned long long t;
192 unsigned long nanosec_rem;
193
194 t = cpu_clock(smp_processor_id());
195 nanosec_rem = do_div(t, 1000000000)/1000;
196 scnprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu] ", (unsigned long)t,
197 nanosec_rem);
198 return tbuf;
199}
200
201static int allow_dbg_log(int ep_addr)
202{
203 int dir, num;
204
205 dir = ep_addr & USB_DIR_IN ? USB_DIR_IN : USB_DIR_OUT;
206 num = ep_addr & ~USB_DIR_IN;
207 num = 1 << num;
208
209 if ((dir == USB_DIR_IN) && (num & ep_addr_rxdbg_mask))
210 return 1;
211 if ((dir == USB_DIR_OUT) && (num & ep_addr_txdbg_mask))
212 return 1;
213
214 return 0;
215}
216
Hemant Kumar4d50a432012-08-15 09:06:35 -0700217static char *get_hex_data(char *dbuf, struct urb *urb, int event, int status)
218{
219 int ep_addr = urb->ep->desc.bEndpointAddress;
220 char *ubuf = urb->transfer_buffer;
221 size_t len = event ? \
222 urb->actual_length : urb->transfer_buffer_length;
223
224 if (status == -EINPROGRESS)
225 status = 0;
226
227 /*Only dump ep in completions and epout submissions*/
228 if (len && !status &&
229 (((ep_addr & USB_DIR_IN) && event) ||
230 (!(ep_addr & USB_DIR_IN) && !event))) {
231 if (len >= 32)
232 len = 32;
233 hex_dump_to_buffer(ubuf, len, 32, 4, dbuf, HEX_DUMP_LEN, 0);
234 } else {
235 dbuf = "";
236 }
237
238 return dbuf;
239}
240
Hemant Kumar45d211b2012-05-31 17:58:43 -0700241static void dbg_log_event(struct urb *urb, char * event, unsigned extra)
242{
243 unsigned long flags;
244 int ep_addr;
245 char tbuf[TIME_BUF_LEN];
Hemant Kumar4d50a432012-08-15 09:06:35 -0700246 char dbuf[HEX_DUMP_LEN];
Hemant Kumar45d211b2012-05-31 17:58:43 -0700247
248 if (!enable_dbg_log)
249 return;
250
251 if (!urb) {
252 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
253 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx], DBG_MSG_LEN,
Hemant Kumar4d50a432012-08-15 09:06:35 -0700254 "%s: %s : %u", get_timestamp(tbuf), event, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700255 dbg_inc(&dbg_hsic_ctrl.idx);
256 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
257 return;
258 }
259
260 ep_addr = urb->ep->desc.bEndpointAddress;
261 if (!allow_dbg_log(ep_addr))
262 return;
263
264 if ((ep_addr & 0x0f) == 0x0) {
265 /*submit event*/
266 if (!str_to_event(event)) {
267 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
268 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
269 DBG_MSG_LEN, "%s: [%s : %p]:[%s] "
Hemant Kumar4d50a432012-08-15 09:06:35 -0700270 "%02x %02x %04x %04x %04x %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700271 get_timestamp(tbuf), event, urb,
272 (ep_addr & USB_DIR_IN) ? "in" : "out",
273 urb->setup_packet[0], urb->setup_packet[1],
274 (urb->setup_packet[3] << 8) |
275 urb->setup_packet[2],
276 (urb->setup_packet[5] << 8) |
277 urb->setup_packet[4],
278 (urb->setup_packet[7] << 8) |
279 urb->setup_packet[6],
Hemant Kumar4d50a432012-08-15 09:06:35 -0700280 urb->transfer_buffer_length, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700281
282 dbg_inc(&dbg_hsic_ctrl.idx);
283 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
284 } else {
285 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
286 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
Hemant Kumar4d50a432012-08-15 09:06:35 -0700287 DBG_MSG_LEN, "%s: [%s : %p]:[%s] %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700288 get_timestamp(tbuf), event, urb,
289 (ep_addr & USB_DIR_IN) ? "in" : "out",
290 urb->actual_length, extra);
291
292 dbg_inc(&dbg_hsic_ctrl.idx);
293 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
294 }
295 } else {
296 write_lock_irqsave(&dbg_hsic_data.lck, flags);
297 scnprintf(dbg_hsic_data.buf[dbg_hsic_data.idx], DBG_MSG_LEN,
Hemant Kumar4d50a432012-08-15 09:06:35 -0700298 "%s: [%s : %p]:ep%d[%s] %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700299 get_timestamp(tbuf), event, urb, ep_addr & 0x0f,
300 (ep_addr & USB_DIR_IN) ? "in" : "out",
301 str_to_event(event) ? urb->actual_length :
Hemant Kumar4d50a432012-08-15 09:06:35 -0700302 urb->transfer_buffer_length, extra,
303 enable_payload_log ? get_hex_data(dbuf, urb,
304 str_to_event(event), extra) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700305
306 dbg_inc(&dbg_hsic_data.idx);
307 write_unlock_irqrestore(&dbg_hsic_data.lck, flags);
308 }
309}
310
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530311static inline struct msm_hsic_hcd *hcd_to_hsic(struct usb_hcd *hcd)
312{
313 return (struct msm_hsic_hcd *) (hcd->hcd_priv);
314}
315
316static inline struct usb_hcd *hsic_to_hcd(struct msm_hsic_hcd *mehci)
317{
318 return container_of((void *) mehci, struct usb_hcd, hcd_priv);
319}
320
Hemant Kumar105d07f2012-07-02 15:33:07 -0700321static void dump_hsic_regs(struct usb_hcd *hcd)
322{
323 int i;
324 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
325
326 if (atomic_read(&mehci->in_lpm))
327 return;
328
329 for (i = USB_REG_START_OFFSET; i <= USB_REG_END_OFFSET; i += 0x10)
330 pr_info("%p: %08x\t%08x\t%08x\t%08x\n", hcd->regs + i,
331 readl_relaxed(hcd->regs + i),
332 readl_relaxed(hcd->regs + i + 4),
333 readl_relaxed(hcd->regs + i + 8),
334 readl_relaxed(hcd->regs + i + 0xc));
335}
336
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530337#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
338
Amit Blayd6ea6102012-06-07 16:26:24 +0300339#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Hemant Kumar3b743cd2012-10-17 13:48:10 -0700340#define USB_PHY_VDD_DIG_VOL_MIN 945000 /* uV */
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700341#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530342
Lena Salman8c8ba382012-02-14 15:59:31 +0200343#define HSIC_DBG1_REG 0x38
344
Amit Blayd6ea6102012-06-07 16:26:24 +0300345static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
346 { /* VDD_CX CORNER Voting */
347 [VDD_NONE] = RPM_VREG_CORNER_NONE,
348 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
349 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
350 },
351 { /* VDD_CX Voltage Voting */
352 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
353 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
354 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
355 },
356};
357
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530358static int msm_hsic_init_vddcx(struct msm_hsic_hcd *mehci, int init)
359{
360 int ret = 0;
Amit Blayd6ea6102012-06-07 16:26:24 +0300361 int none_vol, min_vol, max_vol;
362
363 if (!mehci->hsic_vddcx) {
364 mehci->vdd_type = VDDCX_CORNER;
365 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
366 "hsic_vdd_dig");
367 if (IS_ERR(mehci->hsic_vddcx)) {
368 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
369 "HSIC_VDDCX");
370 if (IS_ERR(mehci->hsic_vddcx)) {
371 dev_err(mehci->dev, "unable to get hsic vddcx\n");
372 return PTR_ERR(mehci->hsic_vddcx);
373 }
374 mehci->vdd_type = VDDCX;
375 }
376 }
377
378 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
379 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
380 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530381
382 if (!init)
383 goto disable_reg;
384
Amit Blayd6ea6102012-06-07 16:26:24 +0300385 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530386 if (ret) {
387 dev_err(mehci->dev, "unable to set the voltage"
388 "for hsic vddcx\n");
Mayank Rana189ac052012-03-24 04:35:02 +0530389 return ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530390 }
391
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530392 ret = regulator_enable(mehci->hsic_vddcx);
393 if (ret) {
394 dev_err(mehci->dev, "unable to enable hsic vddcx\n");
395 goto reg_enable_err;
396 }
397
398 return 0;
399
400disable_reg:
401 regulator_disable(mehci->hsic_vddcx);
402reg_enable_err:
Amit Blayd6ea6102012-06-07 16:26:24 +0300403 regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
404
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530405 return ret;
406
407}
408
Ofir Cohen4cc55372012-11-20 11:17:24 +0200409/* Global Distributed Switch Controller (GDSC) init */
410static int msm_hsic_init_gdsc(struct msm_hsic_hcd *mehci, int init)
411{
412 int ret = 0;
413
414 if (IS_ERR(mehci->hsic_gdsc))
415 return 0;
416
417 if (!mehci->hsic_gdsc) {
418 mehci->hsic_gdsc = devm_regulator_get(mehci->dev,
419 "HSIC_GDSC");
420 if (IS_ERR(mehci->hsic_gdsc))
421 return 0;
422 }
423
424 if (init) {
425 ret = regulator_enable(mehci->hsic_gdsc);
426 if (ret) {
427 dev_err(mehci->dev, "unable to enable hsic gdsc\n");
428 return ret;
429 }
430 } else {
431 regulator_disable(mehci->hsic_gdsc);
432 }
433
434 return 0;
435
436}
437
Pavankumar Kondeti23720492013-01-29 14:41:58 +0530438static int __maybe_unused ulpi_read(struct msm_hsic_hcd *mehci, u32 reg)
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700439{
440 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700441 int cnt = 0;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700442
443 /* initiate read operation */
444 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
445 USB_ULPI_VIEWPORT);
446
447 /* wait for completion */
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700448 while (cnt < ULPI_IO_TIMEOUT_USEC) {
449 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
450 break;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700451 udelay(1);
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700452 cnt++;
453 }
454
455 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
456 dev_err(mehci->dev, "ulpi_read: timeout ULPI_VIEWPORT: %08x\n",
457 readl_relaxed(USB_ULPI_VIEWPORT));
458 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
459 readl_relaxed(USB_PORTSC),
460 readl_relaxed(USB_USBCMD),
461 readl_relaxed(USB_FRINDEX));
462
463 /*frame counter increments afte 125us*/
464 udelay(130);
465 dev_err(mehci->dev, "ulpi_read: FRINDEX: %08x\n",
466 readl_relaxed(USB_FRINDEX));
467 return -ETIMEDOUT;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700468 }
469
470 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
471}
472
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530473static int ulpi_write(struct msm_hsic_hcd *mehci, u32 val, u32 reg)
474{
475 struct usb_hcd *hcd = hsic_to_hcd(mehci);
476 int cnt = 0;
477
478 /* initiate write operation */
479 writel_relaxed(ULPI_RUN | ULPI_WRITE |
480 ULPI_ADDR(reg) | ULPI_DATA(val),
481 USB_ULPI_VIEWPORT);
482
483 /* wait for completion */
484 while (cnt < ULPI_IO_TIMEOUT_USEC) {
485 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
486 break;
487 udelay(1);
488 cnt++;
489 }
490
491 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700492 dev_err(mehci->dev, "ulpi_write: timeout ULPI_VIEWPORT: %08x\n",
493 readl_relaxed(USB_ULPI_VIEWPORT));
494 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
495 readl_relaxed(USB_PORTSC),
496 readl_relaxed(USB_USBCMD),
497 readl_relaxed(USB_FRINDEX));
498
499 /*frame counter increments afte 125us*/
500 udelay(130);
501 dev_err(mehci->dev, "ulpi_write: FRINDEX: %08x\n",
502 readl_relaxed(USB_FRINDEX));
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530503 return -ETIMEDOUT;
504 }
505
506 return 0;
507}
508
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530509static int msm_hsic_config_gpios(struct msm_hsic_hcd *mehci, int gpio_en)
510{
511 int rc = 0;
512 struct msm_hsic_host_platform_data *pdata;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800513 static int gpio_status;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530514
515 pdata = mehci->dev->platform_data;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800516
Lena Salman8c8ba382012-02-14 15:59:31 +0200517 if (!pdata || !pdata->strobe || !pdata->data)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530518 return rc;
519
Vamsi Krishna34f01582011-12-14 19:54:42 -0800520 if (gpio_status == gpio_en)
521 return 0;
522
523 gpio_status = gpio_en;
524
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530525 if (!gpio_en)
526 goto free_gpio;
527
528 rc = gpio_request(pdata->strobe, "HSIC_STROBE_GPIO");
529 if (rc < 0) {
530 dev_err(mehci->dev, "gpio request failed for HSIC STROBE\n");
531 return rc;
532 }
533
534 rc = gpio_request(pdata->data, "HSIC_DATA_GPIO");
535 if (rc < 0) {
536 dev_err(mehci->dev, "gpio request failed for HSIC DATA\n");
537 goto free_strobe;
Hemant Kumar6fd65032012-05-23 13:02:24 -0700538 }
539
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530540 return 0;
541
542free_gpio:
543 gpio_free(pdata->data);
544free_strobe:
545 gpio_free(pdata->strobe);
546
547 return rc;
548}
549
Vamsi Krishna64b48612012-06-14 16:08:11 -0700550static void msm_hsic_clk_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530551{
552 int ret;
553
Ofir Cohen07944aa2012-12-23 13:41:57 +0200554 /* alt_core_clk exists in targets that do not use asynchronous reset */
555 if (!IS_ERR(mehci->alt_core_clk)) {
556 ret = clk_reset(mehci->core_clk, CLK_RESET_ASSERT);
557 if (ret) {
558 dev_err(mehci->dev, "hsic clk assert failed:%d\n", ret);
559 return;
560 }
561
562 /* Since a hw bug, turn off the clock before complete reset */
563 clk_disable(mehci->core_clk);
564
565 ret = clk_reset(mehci->core_clk, CLK_RESET_DEASSERT);
566 if (ret)
567 dev_err(mehci->dev, "hsic clk deassert failed:%d\n",
568 ret);
569
570 usleep_range(10000, 12000);
571
572 clk_enable(mehci->core_clk);
573 } else {
574 /* Using asynchronous block reset to the hardware */
575 clk_disable_unprepare(mehci->core_clk);
576 clk_disable_unprepare(mehci->phy_clk);
577 clk_disable_unprepare(mehci->cal_clk);
578 clk_disable_unprepare(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +0300579 if (!IS_ERR(mehci->inactivity_clk))
580 clk_disable_unprepare(mehci->inactivity_clk);
Ofir Cohen07944aa2012-12-23 13:41:57 +0200581
582 ret = clk_reset(mehci->core_clk, CLK_RESET_ASSERT);
583 if (ret) {
584 dev_err(mehci->dev, "hsic clk assert failed:%d\n", ret);
585 return;
586 }
587 usleep_range(10000, 12000);
588
589 ret = clk_reset(mehci->core_clk, CLK_RESET_DEASSERT);
590 if (ret)
591 dev_err(mehci->dev, "hsic clk deassert failed:%d\n",
592 ret);
593 /*
594 * Required delay between the deassertion and
595 * clock enablement.
596 */
597 ndelay(200);
598 clk_prepare_enable(mehci->core_clk);
599 clk_prepare_enable(mehci->phy_clk);
600 clk_prepare_enable(mehci->cal_clk);
601 clk_prepare_enable(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +0300602 if (!IS_ERR(mehci->inactivity_clk))
603 clk_prepare_enable(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530604 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530605}
606
Vamsi Krishna64b48612012-06-14 16:08:11 -0700607#define HSIC_STROBE_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C0)
608#define HSIC_DATA_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C4)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530609#define HSIC_CAL_PAD_CTL (MSM_TLMM_BASE+0x20C8)
610#define HSIC_LV_MODE 0x04
611#define HSIC_PAD_CALIBRATION 0xA8
612#define HSIC_GPIO_PAD_VAL 0x0A0AAA10
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530613#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Ofir Cohen5dddb152012-11-14 11:18:50 +0200614
615static void msm_hsic_phy_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530616{
617 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530618
Vamsi Krishna64b48612012-06-14 16:08:11 -0700619 msm_hsic_clk_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530620
Vamsi Krishna64b48612012-06-14 16:08:11 -0700621 /* select ulpi phy */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530622 writel_relaxed(0x80000000, USB_PORTSC);
Vamsi Krishna64b48612012-06-14 16:08:11 -0700623 mb();
Ofir Cohen5dddb152012-11-14 11:18:50 +0200624}
625
626static int msm_hsic_start(struct msm_hsic_hcd *mehci)
627{
628 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
629 int ret;
Manu Gautam863b74132012-11-21 14:30:04 +0530630 void __iomem *reg;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530631
Vamsi Krishnabef91832013-03-11 14:10:20 -0700632 if (pdata && pdata->resume_gpio) {
633 ret = gpio_request(pdata->resume_gpio, "HSIC_RESUME_GPIO");
634 if (ret < 0) {
635 dev_err(mehci->dev,
636 "gpio req failed for hsic resume:%d\n", ret);
637 pdata->resume_gpio = 0;
638 }
639 }
640
Lena Salman8c8ba382012-02-14 15:59:31 +0200641 /* HSIC init sequence when HSIC signals (Strobe/Data) are
642 routed via GPIOs */
643 if (pdata && pdata->strobe && pdata->data) {
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530644
Manu Gautam863b74132012-11-21 14:30:04 +0530645 if (!pdata->ignore_cal_pad_config) {
646 /* Enable LV_MODE in HSIC_CAL_PAD_CTL register */
647 writel_relaxed(HSIC_LV_MODE, HSIC_CAL_PAD_CTL);
648 mb();
649 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700650
Lena Salman8c8ba382012-02-14 15:59:31 +0200651 /*set periodic calibration interval to ~2.048sec in
652 HSIC_IO_CAL_REG */
653 ulpi_write(mehci, 0xFF, 0x33);
654
655 /* Enable periodic IO calibration in HSIC_CFG register */
656 ulpi_write(mehci, HSIC_PAD_CALIBRATION, 0x30);
657
Vamsi Krishna64b48612012-06-14 16:08:11 -0700658 /* Configure GPIO pins for HSIC functionality mode */
Lena Salman8c8ba382012-02-14 15:59:31 +0200659 ret = msm_hsic_config_gpios(mehci, 1);
660 if (ret) {
661 dev_err(mehci->dev, " gpio configuarion failed\n");
Vamsi Krishnabef91832013-03-11 14:10:20 -0700662 goto free_resume_gpio;
Lena Salman8c8ba382012-02-14 15:59:31 +0200663 }
Manu Gautam863b74132012-11-21 14:30:04 +0530664 if (pdata->strobe_pad_offset) {
665 /* Set CORE_CTL_EN in STROBE GPIO PAD_CTL register */
666 reg = MSM_TLMM_BASE + pdata->strobe_pad_offset;
667 writel_relaxed(readl_relaxed(reg) | 0x2000000, reg);
668 } else {
669 /* Set LV_MODE=0x1 and DCC=0x2 in STROBE GPIO PAD_CTL */
670 reg = HSIC_STROBE_GPIO_PAD_CTL;
671 writel_relaxed(HSIC_GPIO_PAD_VAL, reg);
672 }
673
674 if (pdata->data_pad_offset) {
675 /* Set CORE_CTL_EN in HSIC_DATA GPIO PAD_CTL register */
676 reg = MSM_TLMM_BASE + pdata->data_pad_offset;
677 writel_relaxed(readl_relaxed(reg) | 0x2000000, reg);
678 } else {
679 /* Set LV_MODE=0x1 and DCC=0x2 in STROBE GPIO PAD_CTL */
680 reg = HSIC_DATA_GPIO_PAD_CTL;
681 writel_relaxed(HSIC_GPIO_PAD_VAL, reg);
682 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700683
684 mb();
685
Lena Salman8c8ba382012-02-14 15:59:31 +0200686 /* Enable HSIC mode in HSIC_CFG register */
687 ulpi_write(mehci, 0x01, 0x31);
688 } else {
689 /* HSIC init sequence when HSIC signals (Strobe/Data) are routed
690 via dedicated I/O */
691
692 /* programmable length of connect signaling (33.2ns) */
693 ret = ulpi_write(mehci, 3, HSIC_DBG1_REG);
694 if (ret) {
695 pr_err("%s: Unable to program length of connect "
696 "signaling\n", __func__);
697 }
698
699 /*set periodic calibration interval to ~2.048sec in
700 HSIC_IO_CAL_REG */
701 ulpi_write(mehci, 0xFF, 0x33);
702
703 /* Enable HSIC mode in HSIC_CFG register */
704 ulpi_write(mehci, 0xA9, 0x30);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530705 }
706
Hemant Kumar6fd65032012-05-23 13:02:24 -0700707 /*disable auto resume*/
708 ulpi_write(mehci, ULPI_IFC_CTRL_AUTORESUME, ULPI_CLR(ULPI_IFC_CTRL));
709
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530710 return 0;
Vamsi Krishnabef91832013-03-11 14:10:20 -0700711
712free_resume_gpio:
713 if (pdata && pdata->resume_gpio)
714 gpio_free(pdata->resume_gpio);
715
716 return ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530717}
718
719#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
720#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
721
722#ifdef CONFIG_PM_SLEEP
Ofir Cohen5dddb152012-11-14 11:18:50 +0200723static int msm_hsic_reset(struct msm_hsic_hcd *mehci)
724{
725 /* reset HSIC phy */
726 msm_hsic_phy_reset(mehci);
727
728 /* HSIC init procedure (caliberation) */
729 return msm_hsic_start(mehci);
730}
731
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530732static int msm_hsic_suspend(struct msm_hsic_hcd *mehci)
733{
734 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530735 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530736 u32 val;
Amit Blayd6ea6102012-06-07 16:26:24 +0300737 int none_vol, max_vol;
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700738 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530739
740 if (atomic_read(&mehci->in_lpm)) {
741 dev_dbg(mehci->dev, "%s called in lpm\n", __func__);
742 return 0;
743 }
744
745 disable_irq(hcd->irq);
Jack Phambe05fbb2012-05-16 10:56:26 -0700746
Sudhir Sharma1673e302012-08-27 17:37:24 -0700747 /* make sure we don't race against a remote wakeup */
748 if (test_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags) ||
Jack Phambe05fbb2012-05-16 10:56:26 -0700749 readl_relaxed(USB_PORTSC) & PORT_RESUME) {
Sudhir Sharma1673e302012-08-27 17:37:24 -0700750 dev_dbg(mehci->dev, "wakeup pending, aborting suspend\n");
Jack Phambe05fbb2012-05-16 10:56:26 -0700751 enable_irq(hcd->irq);
752 return -EBUSY;
753 }
754
Ido Shayevitzaa165c52013-05-12 16:01:28 +0200755 if (pdata->consider_ipa_handshake) {
756 dev_dbg(mehci->dev, "%s:Wait for resources release\n",
757 __func__);
758 if (!msm_bam_hsic_lpm_ok()) {
759 dev_dbg(mehci->dev, "%s:Prod+Cons not released\n",
760 __func__);
761 enable_irq(hcd->irq);
762 return -EBUSY;
763 }
764 dev_dbg(mehci->dev, "%s:Prod+Cons resources released\n",
765 __func__);
766 }
767
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530768 /*
769 * PHY may take some time or even fail to enter into low power
770 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
771 * in failure case.
772 */
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700773 val = readl_relaxed(USB_PORTSC);
774 val &= ~PORT_RWC_BITS;
775 val |= PORTSC_PHCD;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530776 writel_relaxed(val, USB_PORTSC);
777 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
778 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
779 break;
780 udelay(1);
781 cnt++;
782 }
783
784 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
785 dev_err(mehci->dev, "Unable to suspend PHY\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530786 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530787 msm_hsic_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530788 }
789
790 /*
791 * PHY has capability to generate interrupt asynchronously in low
792 * power mode (LPM). This interrupt is level triggered. So USB IRQ
793 * line must be disabled till async interrupt enable bit is cleared
794 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
Pavankumar Kondeti0c9f29c2013-01-28 21:37:59 +0530795 * block data communication from PHY. Enable asynchronous interrupt
796 * only when wakeup gpio IRQ is not present.
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530797 */
Pavankumar Kondeti0c9f29c2013-01-28 21:37:59 +0530798 if (mehci->wakeup_irq)
799 writel_relaxed(readl_relaxed(USB_USBCMD) |
800 ULPI_STP_CTRL, USB_USBCMD);
801 else
802 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530803 ULPI_STP_CTRL, USB_USBCMD);
804
805 /*
806 * Ensure that hardware is put in low power mode before
807 * clocks are turned OFF and VDD is allowed to minimize.
808 */
809 mb();
810
Manu Gautam28b1bac2012-01-30 16:43:06 +0530811 clk_disable_unprepare(mehci->core_clk);
812 clk_disable_unprepare(mehci->phy_clk);
813 clk_disable_unprepare(mehci->cal_clk);
814 clk_disable_unprepare(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +0300815 if (!IS_ERR(mehci->inactivity_clk))
816 clk_disable_unprepare(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530817
Amit Blayd6ea6102012-06-07 16:26:24 +0300818 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
819 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
820
821 ret = regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700822 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300823 dev_err(mehci->dev, "unable to set vddcx voltage for VDD MIN\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700824
Hemant Kumare6275972012-02-29 20:06:21 -0800825 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700826 mehci->bus_vote = false;
827 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800828 }
829
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530830 atomic_set(&mehci->in_lpm, 1);
831 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700832
Manu Gautam3fb60ca2013-02-13 18:33:33 +0530833 if (mehci->wakeup_irq) {
834 mehci->wakeup_irq_enabled = 1;
835 enable_irq_wake(mehci->wakeup_irq);
836 enable_irq(mehci->wakeup_irq);
837 }
Hemant Kumar6fd65032012-05-23 13:02:24 -0700838
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700839 if (pdata && pdata->standalone_latency)
840 pm_qos_update_request(&mehci->pm_qos_req_dma,
841 PM_QOS_DEFAULT_VALUE);
842
Sudhir Sharma1673e302012-08-27 17:37:24 -0700843 wake_unlock(&mehci->wlock);
844
Ido Shayevitzaa165c52013-05-12 16:01:28 +0200845 dev_info(mehci->dev, "HSIC-USB in low power mode\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530846
847 return 0;
848}
849
850static int msm_hsic_resume(struct msm_hsic_hcd *mehci)
851{
852 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530853 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530854 unsigned temp;
Amit Blayd6ea6102012-06-07 16:26:24 +0300855 int min_vol, max_vol;
Hemant Kumard343c012012-09-06 19:57:14 -0700856 unsigned long flags;
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700857 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530858
859 if (!atomic_read(&mehci->in_lpm)) {
860 dev_dbg(mehci->dev, "%s called in !in_lpm\n", __func__);
861 return 0;
862 }
863
Ido Shayevitzaa165c52013-05-12 16:01:28 +0200864 if (pdata->consider_ipa_handshake) {
865 dev_dbg(mehci->dev, "%s:Wait for producer resource\n",
866 __func__);
867 msm_bam_wait_for_hsic_prod_granted();
868 dev_dbg(mehci->dev, "%s:Producer resource obtained\n",
869 __func__);
870 }
871
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +0530872 /* Handles race with Async interrupt */
873 disable_irq(hcd->irq);
874
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700875 if (pdata && pdata->standalone_latency)
876 pm_qos_update_request(&mehci->pm_qos_req_dma,
877 pdata->standalone_latency + 1);
878
Manu Gautam3fb60ca2013-02-13 18:33:33 +0530879 if (mehci->wakeup_irq) {
880 spin_lock_irqsave(&mehci->wakeup_lock, flags);
881 if (mehci->wakeup_irq_enabled) {
882 disable_irq_wake(mehci->wakeup_irq);
883 disable_irq_nosync(mehci->wakeup_irq);
884 mehci->wakeup_irq_enabled = 0;
885 }
886 spin_unlock_irqrestore(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700887 }
888
Sudhir Sharma1673e302012-08-27 17:37:24 -0700889 wake_lock(&mehci->wlock);
890
Hemant Kumare6275972012-02-29 20:06:21 -0800891 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700892 mehci->bus_vote = true;
893 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800894 }
895
Amit Blayd6ea6102012-06-07 16:26:24 +0300896 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
897 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
898
899 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700900 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300901 dev_err(mehci->dev, "unable to set nominal vddcx voltage (no VDD MIN)\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700902
Manu Gautam28b1bac2012-01-30 16:43:06 +0530903 clk_prepare_enable(mehci->core_clk);
904 clk_prepare_enable(mehci->phy_clk);
905 clk_prepare_enable(mehci->cal_clk);
906 clk_prepare_enable(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +0300907 if (!IS_ERR(mehci->inactivity_clk))
908 clk_prepare_enable(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530909
910 temp = readl_relaxed(USB_USBCMD);
911 temp &= ~ASYNC_INTR_CTRL;
912 temp &= ~ULPI_STP_CTRL;
913 writel_relaxed(temp, USB_USBCMD);
914
915 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
916 goto skip_phy_resume;
917
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700918 temp = readl_relaxed(USB_PORTSC);
919 temp &= ~(PORT_RWC_BITS | PORTSC_PHCD);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530920 writel_relaxed(temp, USB_PORTSC);
921 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
922 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD) &&
923 (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE))
924 break;
925 udelay(1);
926 cnt++;
927 }
928
929 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
930 /*
931 * This is a fatal error. Reset the link and
932 * PHY to make hsic working.
933 */
934 dev_err(mehci->dev, "Unable to resume USB. Reset the hsic\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530935 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530936 msm_hsic_reset(mehci);
937 }
938
939skip_phy_resume:
940
Hemant Kumar6fd65032012-05-23 13:02:24 -0700941 usb_hcd_resume_root_hub(hcd);
942
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530943 atomic_set(&mehci->in_lpm, 0);
944
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +0530945 if (atomic_read(&mehci->async_int)) {
946 atomic_set(&mehci->async_int, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530947 pm_runtime_put_noidle(mehci->dev);
Jack Phamdd5ad792012-07-26 10:31:03 -0700948 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700949 }
950
Sudhir Sharma1673e302012-08-27 17:37:24 -0700951 if (atomic_read(&mehci->pm_usage_cnt)) {
952 atomic_set(&mehci->pm_usage_cnt, 0);
953 pm_runtime_put_noidle(mehci->dev);
954 }
Jack Phamdd5ad792012-07-26 10:31:03 -0700955
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +0530956 enable_irq(hcd->irq);
Ido Shayevitzaa165c52013-05-12 16:01:28 +0200957 dev_info(mehci->dev, "HSIC-USB exited from low power mode\n");
958
959 if (pdata->consider_ipa_handshake) {
960 dev_dbg(mehci->dev, "%s:Notify usb bam on resume complete\n",
961 __func__);
962 msm_bam_hsic_notify_on_resume();
963 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530964
965 return 0;
966}
967#endif
968
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700969static void ehci_hsic_bus_vote_w(struct work_struct *w)
970{
971 struct msm_hsic_hcd *mehci =
972 container_of(w, struct msm_hsic_hcd, bus_vote_w);
973 int ret;
974
975 ret = msm_bus_scale_client_update_request(mehci->bus_perf_client,
976 mehci->bus_vote);
977 if (ret)
978 dev_err(mehci->dev, "%s: Failed to vote for bus bandwidth %d\n",
979 __func__, ret);
980}
981
Pavankumar Kondeti23720492013-01-29 14:41:58 +0530982static int msm_hsic_reset_done(struct usb_hcd *hcd)
983{
984 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
985 u32 __iomem *status_reg = &ehci->regs->port_status[0];
986 int ret;
987
988 ehci_writel(ehci, ehci_readl(ehci, status_reg) & ~(PORT_RWC_BITS |
989 PORT_RESET), status_reg);
990
991 ret = handshake(ehci, status_reg, PORT_RESET, 0, 1 * 1000);
992
993 if (ret)
994 pr_err("reset handshake failed in %s\n", __func__);
995 else
996 ehci_writel(ehci, ehci_readl(ehci, &ehci->regs->command) |
997 CMD_RUN, &ehci->regs->command);
998
999 return ret;
1000}
1001
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001002#define STS_GPTIMER0_INTERRUPT BIT(24)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301003static irqreturn_t msm_hsic_irq(struct usb_hcd *hcd)
1004{
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001005 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301006 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001007 u32 status;
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05301008 int ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301009
1010 if (atomic_read(&mehci->in_lpm)) {
Hemant Kumar3dbc5b32012-05-09 15:36:11 -07001011 dev_dbg(mehci->dev, "phy async intr\n");
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05301012 dbg_log_event(NULL, "Async IRQ", 0);
1013 ret = pm_runtime_get(mehci->dev);
1014 if ((ret == 1) || (ret == -EINPROGRESS)) {
1015 pm_runtime_put_noidle(mehci->dev);
1016 } else {
1017 disable_irq_nosync(hcd->irq);
1018 atomic_set(&mehci->async_int, 1);
1019 }
1020
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301021 return IRQ_HANDLED;
1022 }
1023
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001024 status = ehci_readl(ehci, &ehci->regs->status);
1025
1026 if (status & STS_GPTIMER0_INTERRUPT) {
1027 int timeleft;
1028
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301029 dbg_log_event(NULL, "FPR: gpt0_isr", mehci->bus_reset);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001030
1031 timeleft = GPT_CNT(ehci_readl(ehci,
1032 &mehci->timer->gptimer1_ctrl));
1033 if (timeleft) {
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301034 if (mehci->bus_reset) {
1035 ret = msm_hsic_reset_done(hcd);
1036 if (ret) {
1037 mehci->reset_again = 1;
1038 dbg_log_event(NULL, "RESET: fail", 0);
1039 }
1040 } else {
1041 ehci_writel(ehci, ehci_readl(ehci,
1042 &ehci->regs->command) | CMD_RUN,
1043 &ehci->regs->command);
1044 }
1045 } else {
1046 if (mehci->bus_reset)
1047 mehci->reset_again = 1;
1048 else
1049 mehci->resume_again = 1;
1050 }
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001051
1052 dbg_log_event(NULL, "FPR: timeleft", timeleft);
1053
1054 complete(&mehci->gpt0_completion);
1055 ehci_writel(ehci, STS_GPTIMER0_INTERRUPT, &ehci->regs->status);
1056 }
1057
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301058 return ehci_irq(hcd);
1059}
1060
1061static int ehci_hsic_reset(struct usb_hcd *hcd)
1062{
1063 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001064 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301065 int retval;
1066
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001067 mehci->timer = USB_HS_GPTIMER_BASE;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301068 ehci->caps = USB_CAPLENGTH;
1069 ehci->regs = USB_CAPLENGTH +
1070 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
1071 dbg_hcs_params(ehci, "reset");
1072 dbg_hcc_params(ehci, "reset");
1073
1074 /* cache the data to minimize the chip reads*/
1075 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
1076
1077 hcd->has_tt = 1;
1078 ehci->sbrn = HCD_USB2;
1079
1080 retval = ehci_halt(ehci);
1081 if (retval)
1082 return retval;
1083
1084 /* data structure init */
1085 retval = ehci_init(hcd);
1086 if (retval)
1087 return retval;
1088
1089 retval = ehci_reset(ehci);
1090 if (retval)
1091 return retval;
1092
1093 /* bursts of unspecified length. */
1094 writel_relaxed(0, USB_AHBBURST);
1095 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +05301096 writel_relaxed(0x08, USB_AHBMODE);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301097 /* Disable streaming mode and select host mode */
1098 writel_relaxed(0x13, USB_USBMODE);
1099
1100 ehci_port_power(ehci, 1);
1101 return 0;
1102}
1103
Jack Phamc0d41732012-09-28 17:45:38 -07001104#ifdef CONFIG_PM
1105
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301106#define RESET_RETRY_LIMIT 3
1107#define RESET_SIGNAL_TIME_SOF_USEC (50 * 1000)
1108#define RESET_SIGNAL_TIME_USEC (20 * 1000)
1109static void ehci_hsic_reset_sof_bug_handler(struct usb_hcd *hcd, u32 val)
1110{
1111 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1112 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1113 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1114 u32 __iomem *status_reg = &ehci->regs->port_status[0];
Pavankumar Kondetifc7d1d82013-03-05 09:43:22 +05301115 u32 cmd;
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301116 unsigned long flags;
1117 int retries = 0, ret, cnt = RESET_SIGNAL_TIME_USEC;
1118
1119 if (pdata && pdata->swfi_latency)
1120 pm_qos_update_request(&mehci->pm_qos_req_dma,
1121 pdata->swfi_latency + 1);
1122
1123 mehci->bus_reset = 1;
Pavankumar Kondetifc7d1d82013-03-05 09:43:22 +05301124
1125 /* Halt the controller */
1126 cmd = ehci_readl(ehci, &ehci->regs->command);
1127 cmd &= ~CMD_RUN;
1128 ehci_writel(ehci, cmd, &ehci->regs->command);
1129 ret = handshake(ehci, &ehci->regs->status, STS_HALT,
1130 STS_HALT, 16 * 125);
1131 if (ret) {
1132 pr_err("halt handshake fatal error\n");
1133 dbg_log_event(NULL, "HALT: fatal", 0);
1134 goto fail;
1135 }
1136
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301137retry:
1138 retries++;
1139 dbg_log_event(NULL, "RESET: start", retries);
1140 pr_debug("reset begin %d\n", retries);
1141 mehci->reset_again = 0;
1142 spin_lock_irqsave(&ehci->lock, flags);
1143 ehci_writel(ehci, val, status_reg);
1144 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_USEC - 1),
1145 &mehci->timer->gptimer0_ld);
1146 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1147 &mehci->timer->gptimer0_ctrl);
1148 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1149 &ehci->regs->intr_enable);
1150
1151 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_SOF_USEC - 1),
1152 &mehci->timer->gptimer1_ld);
1153 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1154 &mehci->timer->gptimer1_ctrl);
1155
1156 spin_unlock_irqrestore(&ehci->lock, flags);
1157 wait_for_completion(&mehci->gpt0_completion);
1158
1159 if (!mehci->reset_again)
1160 goto done;
1161
1162 if (handshake(ehci, status_reg, PORT_RESET, 0, 10 * 1000)) {
1163 pr_err("reset handshake fatal error\n");
1164 dbg_log_event(NULL, "RESET: fatal", retries);
1165 goto fail;
1166 }
1167
1168 if (retries < RESET_RETRY_LIMIT)
1169 goto retry;
1170
1171 /* complete reset in tight loop */
1172 pr_info("RESET in tight loop\n");
1173 dbg_log_event(NULL, "RESET: tight", 0);
1174
1175 spin_lock_irqsave(&ehci->lock, flags);
1176 ehci_writel(ehci, val, status_reg);
1177 while (cnt--)
1178 udelay(1);
1179 ret = msm_hsic_reset_done(hcd);
1180 spin_unlock_irqrestore(&ehci->lock, flags);
1181 if (ret) {
1182 pr_err("RESET in tight loop failed\n");
1183 dbg_log_event(NULL, "RESET: tight failed", 0);
1184 goto fail;
1185 }
1186
1187done:
1188 dbg_log_event(NULL, "RESET: done", retries);
1189 pr_debug("reset completed\n");
1190fail:
1191 mehci->bus_reset = 0;
1192 if (pdata && pdata->swfi_latency)
1193 pm_qos_update_request(&mehci->pm_qos_req_dma,
1194 PM_QOS_DEFAULT_VALUE);
1195}
1196
Hemant Kumar45d211b2012-05-31 17:58:43 -07001197static int ehci_hsic_bus_suspend(struct usb_hcd *hcd)
1198{
Pavankumar Kondetia723f742012-09-14 14:02:36 +05301199 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1200
1201 if (!(readl_relaxed(USB_PORTSC) & PORT_PE)) {
1202 dbg_log_event(NULL, "RH suspend attempt failed", 0);
1203 dev_dbg(mehci->dev, "%s:port is not enabled skip suspend\n",
1204 __func__);
1205 return -EAGAIN;
1206 }
1207
Hemant Kumar45d211b2012-05-31 17:58:43 -07001208 dbg_log_event(NULL, "Suspend RH", 0);
1209 return ehci_bus_suspend(hcd);
1210}
1211
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001212#define RESUME_RETRY_LIMIT 3
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301213#define RESUME_SIGNAL_TIME_USEC (21 * 1000)
1214#define RESUME_SIGNAL_TIME_SOF_USEC (23 * 1000)
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001215static int msm_hsic_resume_thread(void *data)
1216{
1217 struct msm_hsic_hcd *mehci = data;
1218 struct usb_hcd *hcd = hsic_to_hcd(mehci);
1219 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1220 u32 temp;
1221 unsigned long resume_needed = 0;
1222 int retry_cnt = 0;
1223 int tight_resume = 0;
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301224 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001225
1226 dbg_log_event(NULL, "Resume RH", 0);
1227
1228 /* keep delay between bus states */
1229 if (time_before(jiffies, ehci->next_statechange))
1230 usleep_range(5000, 5000);
1231
1232 spin_lock_irq(&ehci->lock);
1233 if (!HCD_HW_ACCESSIBLE(hcd)) {
1234 spin_unlock_irq(&ehci->lock);
1235 mehci->resume_status = -ESHUTDOWN;
1236 complete(&mehci->rt_completion);
1237 return 0;
1238 }
1239
1240 if (unlikely(ehci->debug)) {
1241 if (!dbgp_reset_prep())
1242 ehci->debug = NULL;
1243 else
1244 dbgp_external_startup();
1245 }
1246
1247 /* at least some APM implementations will try to deliver
1248 * IRQs right away, so delay them until we're ready.
1249 */
1250 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1251
1252 /* re-init operational registers */
1253 ehci_writel(ehci, 0, &ehci->regs->segment);
1254 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
1255 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1256
1257 /*CMD_RUN will be set after, PORT_RESUME gets cleared*/
1258 if (ehci->resume_sof_bug)
1259 ehci->command &= ~CMD_RUN;
1260
1261 /* restore CMD_RUN, framelist size, and irq threshold */
1262 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1263
1264 /* manually resume the ports we suspended during bus_suspend() */
1265resume_again:
1266 if (retry_cnt >= RESUME_RETRY_LIMIT) {
1267 pr_info("retry count(%d) reached max, resume in tight loop\n",
1268 retry_cnt);
1269 tight_resume = 1;
1270 }
1271
1272
1273 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1274 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1275 if (test_bit(0, &ehci->bus_suspended) && (temp & PORT_SUSPEND)) {
1276 temp |= PORT_RESUME;
1277 set_bit(0, &resume_needed);
1278 }
1279 dbg_log_event(NULL, "FPR: Set", temp);
1280 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1281
1282 /* HSIC controller has a h/w bug due to which it can try to send SOFs
1283 * (start of frames) during port resume resulting in phy lockup. HSIC hw
1284 * controller in MSM clears FPR bit after driving the resume signal for
1285 * 20ms. Workaround is to stop SOFs before driving resume and then start
1286 * sending SOFs immediately. Need to send SOFs within 3ms of resume
1287 * completion otherwise peripheral may enter undefined state. As
1288 * usleep_range does not gurantee exact sleep time, GPTimer is used to
1289 * to time the resume sequence. If driver exceeds allowable time SOFs,
1290 * repeat the resume process.
1291 */
1292 if (ehci->resume_sof_bug && resume_needed) {
1293 if (!tight_resume) {
1294 mehci->resume_again = 0;
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301295 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_USEC - 1),
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001296 &mehci->timer->gptimer0_ld);
1297 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1298 &mehci->timer->gptimer0_ctrl);
1299 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1300 &ehci->regs->intr_enable);
1301
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301302 ehci_writel(ehci, GPT_LD(
1303 RESUME_SIGNAL_TIME_SOF_USEC - 1),
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001304 &mehci->timer->gptimer1_ld);
1305 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1306 &mehci->timer->gptimer1_ctrl);
1307
1308 spin_unlock_irq(&ehci->lock);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301309 if (pdata && pdata->swfi_latency)
1310 pm_qos_update_request(&mehci->pm_qos_req_dma,
1311 pdata->swfi_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001312 wait_for_completion(&mehci->gpt0_completion);
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001313 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301314 pm_qos_update_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001315 pdata->standalone_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001316 spin_lock_irq(&ehci->lock);
1317 } else {
1318 dbg_log_event(NULL, "FPR: Tightloop", 0);
1319 /* do the resume in a tight loop */
1320 handshake(ehci, &ehci->regs->port_status[0],
1321 PORT_RESUME, 0, 22 * 1000);
1322 ehci_writel(ehci, ehci_readl(ehci,
1323 &ehci->regs->command) | CMD_RUN,
1324 &ehci->regs->command);
1325 }
1326
1327 if (mehci->resume_again) {
1328 int temp;
1329
1330 dbg_log_event(NULL, "FPR: Re-Resume", retry_cnt);
1331 pr_info("FPR: retry count: %d\n", retry_cnt);
1332 spin_unlock_irq(&ehci->lock);
1333 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1334 temp &= ~PORT_RWC_BITS;
1335 temp |= PORT_SUSPEND;
1336 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1337 /* Keep the bus idle for 5ms so that peripheral
1338 * can detect and initiate suspend
1339 */
1340 usleep_range(5000, 5000);
1341 dbg_log_event(NULL,
1342 "FPR: RResume",
1343 ehci_readl(ehci, &ehci->regs->port_status[0]));
1344 spin_lock_irq(&ehci->lock);
1345 mehci->resume_again = 0;
1346 retry_cnt++;
1347 goto resume_again;
1348 }
1349 }
1350
1351 dbg_log_event(NULL, "FPR: RT-Done", 0);
1352 mehci->resume_status = 1;
1353 spin_unlock_irq(&ehci->lock);
1354
1355 complete(&mehci->rt_completion);
1356
1357 return 0;
1358}
1359
Hemant Kumar45d211b2012-05-31 17:58:43 -07001360static int ehci_hsic_bus_resume(struct usb_hcd *hcd)
1361{
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001362 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1363 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1364 u32 temp;
1365 struct task_struct *resume_thread = NULL;
Vamsi Krishnabef91832013-03-11 14:10:20 -07001366 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1367
1368 if (pdata->resume_gpio)
1369 gpio_direction_output(pdata->resume_gpio, 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001370
Manu Gautam3c598392013-03-22 16:59:10 +05301371 if (!mehci->ehci.resume_sof_bug) {
1372 ehci_bus_resume(hcd);
1373 } else {
1374 mehci->resume_status = 0;
1375 resume_thread = kthread_run(msm_hsic_resume_thread,
1376 mehci, "hsic_resume_thread");
1377 if (IS_ERR(resume_thread)) {
1378 pr_err("Error creating resume thread:%lu\n",
1379 PTR_ERR(resume_thread));
1380 return PTR_ERR(resume_thread);
1381 }
1382
1383 wait_for_completion(&mehci->rt_completion);
1384
1385 if (mehci->resume_status < 0)
1386 return mehci->resume_status;
1387
1388 dbg_log_event(NULL, "FPR: Wokeup", 0);
1389 spin_lock_irq(&ehci->lock);
1390 (void) ehci_readl(ehci, &ehci->regs->command);
1391
1392 temp = 0;
1393 if (ehci->async->qh_next.qh)
1394 temp |= CMD_ASE;
1395 if (ehci->periodic_sched)
1396 temp |= CMD_PSE;
1397 if (temp) {
1398 ehci->command |= temp;
1399 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1400 }
1401
1402 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
1403 hcd->state = HC_STATE_RUNNING;
1404 ehci->rh_state = EHCI_RH_RUNNING;
1405 ehci->command |= CMD_RUN;
1406
1407 /* Now we can safely re-enable irqs */
1408 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1409
1410 spin_unlock_irq(&ehci->lock);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001411 }
1412
Vamsi Krishnabef91832013-03-11 14:10:20 -07001413 if (pdata->resume_gpio)
1414 gpio_direction_output(pdata->resume_gpio, 0);
1415
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001416 return 0;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001417}
1418
Jack Phamc0d41732012-09-28 17:45:38 -07001419#else
1420
1421#define ehci_hsic_bus_suspend NULL
1422#define ehci_hsic_bus_resume NULL
1423
1424#endif /* CONFIG_PM */
1425
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001426static void ehci_msm_set_autosuspend_delay(struct usb_device *dev)
1427{
1428 if (!dev->parent) /*for root hub no delay*/
1429 pm_runtime_set_autosuspend_delay(&dev->dev, 0);
1430 else
1431 pm_runtime_set_autosuspend_delay(&dev->dev, 200);
1432}
1433
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001434static int ehci_msm_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1435 gfp_t mem_flags)
1436{
1437 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1438 struct usb_host_bam_type *usb_host_bam =
1439 (struct usb_host_bam_type *)urb->priv_data;
1440
1441 if (usb_host_bam && mehci && mehci->enable_hbm)
1442 return hbm_urb_enqueue(hcd, urb, mem_flags);
1443 return ehci_urb_enqueue(hcd, urb, mem_flags);
1444}
1445
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301446static struct hc_driver msm_hsic_driver = {
1447 .description = hcd_name,
1448 .product_desc = "Qualcomm EHCI Host Controller using HSIC",
1449 .hcd_priv_size = sizeof(struct msm_hsic_hcd),
1450
1451 /*
1452 * generic hardware linkage
1453 */
1454 .irq = msm_hsic_irq,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001455 .flags = HCD_USB2 | HCD_MEMORY | HCD_OLD_ENUM,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301456
1457 .reset = ehci_hsic_reset,
1458 .start = ehci_run,
1459
1460 .stop = ehci_stop,
1461 .shutdown = ehci_shutdown,
1462
1463 /*
1464 * managing i/o requests and associated device resources
1465 */
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001466 .urb_enqueue = ehci_msm_urb_enqueue,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301467 .urb_dequeue = ehci_urb_dequeue,
1468 .endpoint_disable = ehci_endpoint_disable,
1469 .endpoint_reset = ehci_endpoint_reset,
1470 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1471
1472 /*
1473 * scheduling support
1474 */
1475 .get_frame_number = ehci_get_frame,
1476
1477 /*
1478 * root hub support
1479 */
1480 .hub_status_data = ehci_hub_status_data,
1481 .hub_control = ehci_hub_control,
1482 .relinquish_port = ehci_relinquish_port,
1483 .port_handed_over = ehci_port_handed_over,
1484
1485 /*
1486 * PM support
1487 */
Hemant Kumar45d211b2012-05-31 17:58:43 -07001488 .bus_suspend = ehci_hsic_bus_suspend,
1489 .bus_resume = ehci_hsic_bus_resume,
1490
Hemant Kumar4d50a432012-08-15 09:06:35 -07001491 .log_urb = dbg_log_event,
Hemant Kumar105d07f2012-07-02 15:33:07 -07001492 .dump_regs = dump_hsic_regs,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001493
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001494 .set_autosuspend_delay = ehci_msm_set_autosuspend_delay,
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301495 .reset_sof_bug_handler = ehci_hsic_reset_sof_bug_handler,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301496};
1497
1498static int msm_hsic_init_clocks(struct msm_hsic_hcd *mehci, u32 init)
1499{
1500 int ret = 0;
1501
1502 if (!init)
1503 goto put_clocks;
1504
Lena Salman8c8ba382012-02-14 15:59:31 +02001505 /*core_clk is required for LINK protocol engine
1506 *clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001507 mehci->core_clk = clk_get(mehci->dev, "core_clk");
1508 if (IS_ERR(mehci->core_clk)) {
1509 dev_err(mehci->dev, "failed to get core_clk\n");
1510 ret = PTR_ERR(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301511 return ret;
1512 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301513
Ofir Cohen07944aa2012-12-23 13:41:57 +02001514 /* alt_core_clk is for LINK to be used during PHY RESET in
1515 * targets on which link does NOT use asynchronous reset methodology.
Lena Salman8c8ba382012-02-14 15:59:31 +02001516 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001517 mehci->alt_core_clk = clk_get(mehci->dev, "alt_core_clk");
Manu Gautam863b74132012-11-21 14:30:04 +05301518 if (IS_ERR(mehci->alt_core_clk))
1519 dev_dbg(mehci->dev, "failed to get alt_core_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301520
Lena Salman8c8ba382012-02-14 15:59:31 +02001521 /* phy_clk is required for HSIC PHY operation
1522 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001523 mehci->phy_clk = clk_get(mehci->dev, "phy_clk");
1524 if (IS_ERR(mehci->phy_clk)) {
1525 dev_err(mehci->dev, "failed to get phy_clk\n");
1526 ret = PTR_ERR(mehci->phy_clk);
1527 goto put_alt_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301528 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301529
1530 /* 10MHz cal_clk is required for calibration of I/O pads */
Manu Gautam5143b252012-01-05 19:25:23 -08001531 mehci->cal_clk = clk_get(mehci->dev, "cal_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301532 if (IS_ERR(mehci->cal_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001533 dev_err(mehci->dev, "failed to get cal_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301534 ret = PTR_ERR(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001535 goto put_phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301536 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301537
1538 /* ahb_clk is required for data transfers */
Manu Gautam5143b252012-01-05 19:25:23 -08001539 mehci->ahb_clk = clk_get(mehci->dev, "iface_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301540 if (IS_ERR(mehci->ahb_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001541 dev_err(mehci->dev, "failed to get iface_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301542 ret = PTR_ERR(mehci->ahb_clk);
1543 goto put_cal_clk;
1544 }
1545
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001546 /*
1547 * Inactivity_clk is required for hsic bam inactivity timer.
1548 * This clock is not compulsory and is defined in clock lookup
1549 * only for targets that need to use the inactivity timer feature.
1550 */
1551 mehci->inactivity_clk = clk_get(mehci->dev, "inactivity_clk");
1552 if (IS_ERR(mehci->inactivity_clk))
1553 dev_dbg(mehci->dev, "failed to get inactivity_clk\n");
1554
Manu Gautam28b1bac2012-01-30 16:43:06 +05301555 clk_prepare_enable(mehci->core_clk);
1556 clk_prepare_enable(mehci->phy_clk);
1557 clk_prepare_enable(mehci->cal_clk);
1558 clk_prepare_enable(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001559 if (!IS_ERR(mehci->inactivity_clk))
1560 clk_prepare_enable(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301561
1562 return 0;
1563
1564put_clocks:
Jack Phamfd193eb2012-02-22 15:38:08 -08001565 if (!atomic_read(&mehci->in_lpm)) {
1566 clk_disable_unprepare(mehci->core_clk);
1567 clk_disable_unprepare(mehci->phy_clk);
1568 clk_disable_unprepare(mehci->cal_clk);
1569 clk_disable_unprepare(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001570 if (!IS_ERR(mehci->inactivity_clk))
1571 clk_disable_unprepare(mehci->inactivity_clk);
Jack Phamfd193eb2012-02-22 15:38:08 -08001572 }
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001573 if (!IS_ERR(mehci->inactivity_clk))
1574 clk_put(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301575 clk_put(mehci->ahb_clk);
1576put_cal_clk:
1577 clk_put(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001578put_phy_clk:
1579 clk_put(mehci->phy_clk);
1580put_alt_core_clk:
Manu Gautam863b74132012-11-21 14:30:04 +05301581 if (!IS_ERR(mehci->alt_core_clk))
1582 clk_put(mehci->alt_core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001583 clk_put(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301584
1585 return ret;
1586}
Vamsi Krishna34f01582011-12-14 19:54:42 -08001587static irqreturn_t hsic_peripheral_status_change(int irq, void *dev_id)
1588{
1589 struct msm_hsic_hcd *mehci = dev_id;
1590
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001591 pr_debug("%s: mehci:%p dev_id:%p\n", __func__, mehci, dev_id);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001592
1593 if (mehci)
1594 msm_hsic_config_gpios(mehci, 0);
1595
1596 return IRQ_HANDLED;
1597}
1598
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001599static irqreturn_t msm_hsic_wakeup_irq(int irq, void *data)
1600{
1601 struct msm_hsic_hcd *mehci = data;
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301602 int ret;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001603
Manu Gautam3fb60ca2013-02-13 18:33:33 +05301604 if (irq == mehci->async_irq) {
1605 mehci->async_int_cnt++;
1606 dbg_log_event(NULL, "Remote Wakeup (ASYNC) IRQ",
1607 mehci->async_int_cnt);
1608 } else {
1609 mehci->wakeup_int_cnt++;
1610 dbg_log_event(NULL, "Remote Wakeup IRQ", mehci->wakeup_int_cnt);
1611 }
1612 dev_dbg(mehci->dev, "%s: hsic remote wakeup interrupt %d cnt: %u, %u\n",
1613 __func__, irq, mehci->wakeup_int_cnt, mehci->async_int_cnt);
Hemant Kumar6fd65032012-05-23 13:02:24 -07001614
Sudhir Sharma1673e302012-08-27 17:37:24 -07001615 wake_lock(&mehci->wlock);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001616
Manu Gautam3fb60ca2013-02-13 18:33:33 +05301617 if (mehci->wakeup_irq) {
1618 spin_lock(&mehci->wakeup_lock);
1619 if (mehci->wakeup_irq_enabled) {
1620 mehci->wakeup_irq_enabled = 0;
1621 disable_irq_wake(irq);
1622 disable_irq_nosync(irq);
1623 }
1624 spin_unlock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001625 }
1626
Sudhir Sharma1673e302012-08-27 17:37:24 -07001627 if (!atomic_read(&mehci->pm_usage_cnt)) {
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301628 ret = pm_runtime_get(mehci->dev);
1629 /*
1630 * HSIC runtime resume can race with us.
1631 * if we are active (ret == 1) or resuming
1632 * (ret == -EINPROGRESS), decrement the
1633 * PM usage counter before returning.
1634 */
1635 if ((ret == 1) || (ret == -EINPROGRESS))
1636 pm_runtime_put_noidle(mehci->dev);
1637 else
1638 atomic_set(&mehci->pm_usage_cnt, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07001639 }
1640
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001641 return IRQ_HANDLED;
1642}
1643
Hemant Kumare6275972012-02-29 20:06:21 -08001644static int ehci_hsic_msm_bus_show(struct seq_file *s, void *unused)
1645{
1646 if (debug_bus_voting_enabled)
1647 seq_printf(s, "enabled\n");
1648 else
1649 seq_printf(s, "disabled\n");
1650
1651 return 0;
1652}
1653
1654static int ehci_hsic_msm_bus_open(struct inode *inode, struct file *file)
1655{
1656 return single_open(file, ehci_hsic_msm_bus_show, inode->i_private);
1657}
1658
1659static ssize_t ehci_hsic_msm_bus_write(struct file *file,
1660 const char __user *ubuf, size_t count, loff_t *ppos)
1661{
1662 char buf[8];
1663 int ret;
1664 struct seq_file *s = file->private_data;
1665 struct msm_hsic_hcd *mehci = s->private;
1666
1667 memset(buf, 0x00, sizeof(buf));
1668
1669 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1670 return -EFAULT;
1671
1672 if (!strncmp(buf, "enable", 6)) {
1673 /* Do not vote here. Let hsic driver decide when to vote */
1674 debug_bus_voting_enabled = true;
1675 } else {
1676 debug_bus_voting_enabled = false;
1677 if (mehci->bus_perf_client) {
1678 ret = msm_bus_scale_client_update_request(
1679 mehci->bus_perf_client, 0);
1680 if (ret)
1681 dev_err(mehci->dev, "%s: Failed to devote "
1682 "for bus bw %d\n", __func__, ret);
1683 }
1684 }
1685
1686 return count;
1687}
1688
1689const struct file_operations ehci_hsic_msm_bus_fops = {
1690 .open = ehci_hsic_msm_bus_open,
1691 .read = seq_read,
1692 .write = ehci_hsic_msm_bus_write,
1693 .llseek = seq_lseek,
1694 .release = single_release,
1695};
1696
Hemant Kumar6fd65032012-05-23 13:02:24 -07001697static int ehci_hsic_msm_wakeup_cnt_show(struct seq_file *s, void *unused)
1698{
1699 struct msm_hsic_hcd *mehci = s->private;
1700
1701 seq_printf(s, "%u\n", mehci->wakeup_int_cnt);
1702
1703 return 0;
1704}
1705
1706static int ehci_hsic_msm_wakeup_cnt_open(struct inode *inode, struct file *f)
1707{
1708 return single_open(f, ehci_hsic_msm_wakeup_cnt_show, inode->i_private);
1709}
1710
1711const struct file_operations ehci_hsic_msm_wakeup_cnt_fops = {
1712 .open = ehci_hsic_msm_wakeup_cnt_open,
1713 .read = seq_read,
1714 .llseek = seq_lseek,
1715 .release = single_release,
1716};
1717
Hemant Kumar45d211b2012-05-31 17:58:43 -07001718static int ehci_hsic_msm_data_events_show(struct seq_file *s, void *unused)
1719{
1720 unsigned long flags;
1721 unsigned i;
1722
1723 read_lock_irqsave(&dbg_hsic_data.lck, flags);
1724
1725 i = dbg_hsic_data.idx;
1726 for (dbg_inc(&i); i != dbg_hsic_data.idx; dbg_inc(&i)) {
1727 if (!strnlen(dbg_hsic_data.buf[i], DBG_MSG_LEN))
1728 continue;
1729 seq_printf(s, "%s\n", dbg_hsic_data.buf[i]);
1730 }
1731
1732 read_unlock_irqrestore(&dbg_hsic_data.lck, flags);
1733
1734 return 0;
1735}
1736
1737static int ehci_hsic_msm_data_events_open(struct inode *inode, struct file *f)
1738{
1739 return single_open(f, ehci_hsic_msm_data_events_show, inode->i_private);
1740}
1741
1742const struct file_operations ehci_hsic_msm_dbg_data_fops = {
1743 .open = ehci_hsic_msm_data_events_open,
1744 .read = seq_read,
1745 .llseek = seq_lseek,
1746 .release = single_release,
1747};
1748
1749static int ehci_hsic_msm_ctrl_events_show(struct seq_file *s, void *unused)
1750{
1751 unsigned long flags;
1752 unsigned i;
1753
1754 read_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
1755
1756 i = dbg_hsic_ctrl.idx;
1757 for (dbg_inc(&i); i != dbg_hsic_ctrl.idx; dbg_inc(&i)) {
1758 if (!strnlen(dbg_hsic_ctrl.buf[i], DBG_MSG_LEN))
1759 continue;
1760 seq_printf(s, "%s\n", dbg_hsic_ctrl.buf[i]);
1761 }
1762
1763 read_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
1764
1765 return 0;
1766}
1767
1768static int ehci_hsic_msm_ctrl_events_open(struct inode *inode, struct file *f)
1769{
1770 return single_open(f, ehci_hsic_msm_ctrl_events_show, inode->i_private);
1771}
1772
1773const struct file_operations ehci_hsic_msm_dbg_ctrl_fops = {
1774 .open = ehci_hsic_msm_ctrl_events_open,
1775 .read = seq_read,
1776 .llseek = seq_lseek,
1777 .release = single_release,
1778};
1779
Hemant Kumare6275972012-02-29 20:06:21 -08001780static struct dentry *ehci_hsic_msm_dbg_root;
1781static int ehci_hsic_msm_debugfs_init(struct msm_hsic_hcd *mehci)
1782{
1783 struct dentry *ehci_hsic_msm_dentry;
1784
1785 ehci_hsic_msm_dbg_root = debugfs_create_dir("ehci_hsic_msm_dbg", NULL);
1786
1787 if (!ehci_hsic_msm_dbg_root || IS_ERR(ehci_hsic_msm_dbg_root))
1788 return -ENODEV;
1789
1790 ehci_hsic_msm_dentry = debugfs_create_file("bus_voting",
1791 S_IRUGO | S_IWUSR,
1792 ehci_hsic_msm_dbg_root, mehci,
1793 &ehci_hsic_msm_bus_fops);
1794
1795 if (!ehci_hsic_msm_dentry) {
1796 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1797 return -ENODEV;
1798 }
1799
Hemant Kumar6fd65032012-05-23 13:02:24 -07001800 ehci_hsic_msm_dentry = debugfs_create_file("wakeup_cnt",
1801 S_IRUGO,
1802 ehci_hsic_msm_dbg_root, mehci,
1803 &ehci_hsic_msm_wakeup_cnt_fops);
1804
1805 if (!ehci_hsic_msm_dentry) {
1806 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1807 return -ENODEV;
1808 }
1809
Hemant Kumar45d211b2012-05-31 17:58:43 -07001810 ehci_hsic_msm_dentry = debugfs_create_file("show_ctrl_events",
1811 S_IRUGO,
1812 ehci_hsic_msm_dbg_root, mehci,
1813 &ehci_hsic_msm_dbg_ctrl_fops);
1814
1815 if (!ehci_hsic_msm_dentry) {
1816 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1817 return -ENODEV;
1818 }
1819
1820 ehci_hsic_msm_dentry = debugfs_create_file("show_data_events",
1821 S_IRUGO,
1822 ehci_hsic_msm_dbg_root, mehci,
1823 &ehci_hsic_msm_dbg_data_fops);
1824
1825 if (!ehci_hsic_msm_dentry) {
1826 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1827 return -ENODEV;
1828 }
1829
Hemant Kumare6275972012-02-29 20:06:21 -08001830 return 0;
1831}
1832
1833static void ehci_hsic_msm_debugfs_cleanup(void)
1834{
1835 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1836}
1837
Manu Gautam863b74132012-11-21 14:30:04 +05301838struct msm_hsic_host_platform_data *msm_hsic_dt_to_pdata(
1839 struct platform_device *pdev)
1840{
1841 struct device_node *node = pdev->dev.of_node;
1842 struct msm_hsic_host_platform_data *pdata;
Ido Shayevitzca5dc492013-01-09 14:49:59 +02001843 int res_gpio;
Manu Gautam863b74132012-11-21 14:30:04 +05301844
1845 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1846 if (!pdata) {
1847 dev_err(&pdev->dev, "unable to allocate platform data\n");
1848 return NULL;
1849 }
Manu Gautam863b74132012-11-21 14:30:04 +05301850
Ido Shayevitzca5dc492013-01-09 14:49:59 +02001851 res_gpio = of_get_named_gpio(node, "hsic,strobe-gpio", 0);
1852 if (res_gpio < 0)
1853 res_gpio = 0;
1854 pdata->strobe = res_gpio;
1855
1856 res_gpio = of_get_named_gpio(node, "hsic,data-gpio", 0);
1857 if (res_gpio < 0)
1858 res_gpio = 0;
1859 pdata->data = res_gpio;
Manu Gautam863b74132012-11-21 14:30:04 +05301860
Vamsi Krishnabef91832013-03-11 14:10:20 -07001861 res_gpio = of_get_named_gpio(node, "hsic,resume-gpio", 0);
1862 if (res_gpio < 0)
1863 res_gpio = 0;
1864 pdata->resume_gpio = res_gpio;
1865
Manu Gautam3c598392013-03-22 16:59:10 +05301866 pdata->phy_sof_workaround = of_property_read_bool(node,
1867 "qcom,phy-sof-workaround");
Manu Gautam863b74132012-11-21 14:30:04 +05301868 pdata->ignore_cal_pad_config = of_property_read_bool(node,
1869 "hsic,ignore-cal-pad-config");
1870 of_property_read_u32(node, "hsic,strobe-pad-offset",
1871 &pdata->strobe_pad_offset);
1872 of_property_read_u32(node, "hsic,data-pad-offset",
1873 &pdata->data_pad_offset);
1874
Ido Shayevitz95c46882013-01-20 13:47:46 +02001875 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
1876
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001877 pdata->pool_64_bit_align = of_property_read_bool(node,
1878 "qcom,pool-64-bit-align");
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001879 pdata->enable_hbm = of_property_read_bool(node,
1880 "qcom,enable-hbm");
Shimrit Malichi08f07342013-04-24 13:09:12 +03001881 pdata->disable_park_mode = (of_property_read_bool(node,
1882 "qcom,disable-park-mode"));
Ido Shayevitzaa165c52013-05-12 16:01:28 +02001883 pdata->consider_ipa_handshake = (of_property_read_bool(node,
1884 "hsic,consider-ipa-handshake"));
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001885
Manu Gautam863b74132012-11-21 14:30:04 +05301886 return pdata;
1887}
1888
1889
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301890static int __devinit ehci_hsic_msm_probe(struct platform_device *pdev)
1891{
1892 struct usb_hcd *hcd;
1893 struct resource *res;
1894 struct msm_hsic_hcd *mehci;
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +05301895 struct msm_hsic_host_platform_data *pdata;
Manu Gautam16562792013-03-13 07:04:11 +05301896 unsigned long wakeup_irq_flags = 0;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301897 int ret;
1898
1899 dev_dbg(&pdev->dev, "ehci_msm-hsic probe\n");
1900
Manu Gautam863b74132012-11-21 14:30:04 +05301901 if (pdev->dev.of_node) {
1902 dev_dbg(&pdev->dev, "device tree enabled\n");
1903 pdev->dev.platform_data = msm_hsic_dt_to_pdata(pdev);
1904 dev_set_name(&pdev->dev, ehci_msm_hsic_driver.driver.name);
Manu Gautam16562792013-03-13 07:04:11 +05301905 } else {
1906 /* explicitly pass wakeup_irq flag for !DT */
1907 wakeup_irq_flags = IRQF_TRIGGER_HIGH;
Manu Gautam863b74132012-11-21 14:30:04 +05301908 }
1909 if (!pdev->dev.platform_data)
1910 dev_dbg(&pdev->dev, "No platform data given\n");
1911
Ofir Cohen4cc55372012-11-20 11:17:24 +02001912 if (!pdev->dev.dma_mask)
1913 pdev->dev.dma_mask = &ehci_msm_hsic_dma_mask;
1914 if (!pdev->dev.coherent_dma_mask)
1915 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1916
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301917 /* After parent device's probe is executed, it will be put in suspend
1918 * mode. When child device's probe is called, driver core is not
1919 * resuming parent device due to which parent will be in suspend even
1920 * though child is active. Hence resume the parent device explicitly.
1921 */
1922 if (pdev->dev.parent)
1923 pm_runtime_get_sync(pdev->dev.parent);
1924
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301925 hcd = usb_create_hcd(&msm_hsic_driver, &pdev->dev,
1926 dev_name(&pdev->dev));
1927 if (!hcd) {
1928 dev_err(&pdev->dev, "Unable to create HCD\n");
1929 return -ENOMEM;
1930 }
1931
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05301932 hcd_to_bus(hcd)->skip_resume = true;
1933
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301934 hcd->irq = platform_get_irq(pdev, 0);
1935 if (hcd->irq < 0) {
1936 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1937 ret = hcd->irq;
1938 goto put_hcd;
1939 }
1940
1941 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1942 if (!res) {
1943 dev_err(&pdev->dev, "Unable to get memory resource\n");
1944 ret = -ENODEV;
1945 goto put_hcd;
1946 }
1947
1948 hcd->rsrc_start = res->start;
1949 hcd->rsrc_len = resource_size(res);
1950 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1951 if (!hcd->regs) {
1952 dev_err(&pdev->dev, "ioremap failed\n");
1953 ret = -ENOMEM;
1954 goto put_hcd;
1955 }
1956
1957 mehci = hcd_to_hsic(hcd);
1958 mehci->dev = &pdev->dev;
Hemant Kumar30d361c2012-08-20 14:44:40 -07001959 pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301960
Hemant Kumard343c012012-09-06 19:57:14 -07001961 spin_lock_init(&mehci->wakeup_lock);
1962
Manu Gautam3c598392013-03-22 16:59:10 +05301963 if (pdata->phy_sof_workaround) {
1964 mehci->ehci.susp_sof_bug = 1;
1965 mehci->ehci.reset_sof_bug = 1;
1966 mehci->ehci.resume_sof_bug = 1;
1967 }
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001968
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001969 mehci->ehci.pool_64_bit_align = pdata->pool_64_bit_align;
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001970 mehci->enable_hbm = pdata->enable_hbm;
Hemant Kumare4040492012-06-21 17:35:42 -07001971
Hemant Kumar30d361c2012-08-20 14:44:40 -07001972 if (pdata)
1973 mehci->ehci.log2_irq_thresh = pdata->log2_irq_thresh;
Hemant Kumar933e0402012-05-22 11:11:40 -07001974
Ofir Cohen4cc55372012-11-20 11:17:24 +02001975 ret = msm_hsic_init_gdsc(mehci, 1);
1976 if (ret) {
1977 dev_err(&pdev->dev, "unable to initialize GDSC\n");
1978 ret = -ENODEV;
1979 goto put_hcd;
1980 }
1981
Vamsi Krishna34f01582011-12-14 19:54:42 -08001982 res = platform_get_resource_byname(pdev,
1983 IORESOURCE_IRQ,
1984 "peripheral_status_irq");
1985 if (res)
1986 mehci->peripheral_status_irq = res->start;
1987
Jack Pham0cc75c42012-10-10 02:03:50 +02001988 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "wakeup");
Hemant Kumar6fd65032012-05-23 13:02:24 -07001989 if (res) {
Jack Pham0cc75c42012-10-10 02:03:50 +02001990 mehci->wakeup_irq = res->start;
Hemant Kumar6fd65032012-05-23 13:02:24 -07001991 dev_dbg(mehci->dev, "wakeup_irq: %d\n", mehci->wakeup_irq);
1992 }
1993
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301994 ret = msm_hsic_init_clocks(mehci, 1);
1995 if (ret) {
1996 dev_err(&pdev->dev, "unable to initialize clocks\n");
1997 ret = -ENODEV;
1998 goto unmap;
1999 }
2000
2001 ret = msm_hsic_init_vddcx(mehci, 1);
2002 if (ret) {
2003 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
2004 ret = -ENODEV;
2005 goto deinit_clocks;
2006 }
2007
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07002008 init_completion(&mehci->rt_completion);
2009 init_completion(&mehci->gpt0_completion);
Ofir Cohen5dddb152012-11-14 11:18:50 +02002010
2011 msm_hsic_phy_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302012
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002013 ehci_wq = create_singlethread_workqueue("ehci_wq");
2014 if (!ehci_wq) {
2015 dev_err(&pdev->dev, "unable to create workqueue\n");
2016 ret = -ENOMEM;
2017 goto deinit_vddcx;
2018 }
2019
2020 INIT_WORK(&mehci->bus_vote_w, ehci_hsic_bus_vote_w);
2021
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302022 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
2023 if (ret) {
2024 dev_err(&pdev->dev, "unable to register HCD\n");
Ofir Cohen5dddb152012-11-14 11:18:50 +02002025 goto destroy_wq;
2026 }
2027
2028 ret = msm_hsic_start(mehci);
2029 if (ret) {
2030 dev_err(&pdev->dev, "unable to initialize PHY\n");
2031 goto destroy_wq;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302032 }
2033
2034 device_init_wakeup(&pdev->dev, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07002035 wake_lock_init(&mehci->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
2036 wake_lock(&mehci->wlock);
Vamsi Krishna34f01582011-12-14 19:54:42 -08002037
2038 if (mehci->peripheral_status_irq) {
2039 ret = request_threaded_irq(mehci->peripheral_status_irq,
2040 NULL, hsic_peripheral_status_change,
2041 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2042 | IRQF_SHARED,
2043 "hsic_peripheral_status", mehci);
2044 if (ret)
2045 dev_err(&pdev->dev, "%s:request_irq:%d failed:%d",
2046 __func__, mehci->peripheral_status_irq, ret);
2047 }
2048
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002049 /* configure wakeup irq */
Hemant Kumar6fd65032012-05-23 13:02:24 -07002050 if (mehci->wakeup_irq) {
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07002051 /* In case if wakeup gpio is pulled high at this point
2052 * remote wakeup interrupt fires right after request_irq.
2053 * Remote wake up interrupt only needs to be enabled when
2054 * HSIC bus goes to suspend.
2055 */
2056 irq_set_status_flags(mehci->wakeup_irq, IRQ_NOAUTOEN);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002057 ret = request_irq(mehci->wakeup_irq, msm_hsic_wakeup_irq,
Manu Gautam16562792013-03-13 07:04:11 +05302058 wakeup_irq_flags,
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002059 "msm_hsic_wakeup", mehci);
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07002060 if (ret) {
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002061 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
2062 mehci->wakeup_irq, ret);
2063 mehci->wakeup_irq = 0;
2064 }
2065 }
2066
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302067 mehci->async_irq = platform_get_irq_byname(pdev, "async_irq");
2068 if (mehci->async_irq < 0) {
2069 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
2070 mehci->async_irq = 0;
2071 } else {
2072 ret = request_irq(mehci->async_irq, msm_hsic_wakeup_irq,
2073 IRQF_TRIGGER_RISING, "msm_hsic_async", mehci);
2074 if (ret) {
2075 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
2076 mehci->async_irq = 0;
Manu Gautam16562792013-03-13 07:04:11 +05302077 } else if (!mehci->wakeup_irq) {
2078 /* Async IRQ is used only in absence of dedicated irq */
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302079 enable_irq_wake(mehci->async_irq);
2080 }
2081 }
2082
Hemant Kumare6275972012-02-29 20:06:21 -08002083 ret = ehci_hsic_msm_debugfs_init(mehci);
2084 if (ret)
2085 dev_dbg(&pdev->dev, "mode debugfs file is"
2086 "not available\n");
2087
2088 if (pdata && pdata->bus_scale_table) {
2089 mehci->bus_perf_client =
2090 msm_bus_scale_register_client(pdata->bus_scale_table);
2091 /* Configure BUS performance parameters for MAX bandwidth */
2092 if (mehci->bus_perf_client) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002093 mehci->bus_vote = true;
2094 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -08002095 } else {
2096 dev_err(&pdev->dev, "%s: Failed to register BUS "
2097 "scaling client!!\n", __func__);
2098 }
2099 }
2100
Hemant Kumar105d07f2012-07-02 15:33:07 -07002101 __mehci = mehci;
2102
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002103 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302104 pm_qos_add_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002105 PM_QOS_CPU_DMA_LATENCY, pdata->standalone_latency + 1);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302106
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302107 /*
2108 * This pdev->dev is assigned parent of root-hub by USB core,
2109 * hence, runtime framework automatically calls this driver's
2110 * runtime APIs based on root-hub's state.
2111 */
2112 pm_runtime_set_active(&pdev->dev);
2113 pm_runtime_enable(&pdev->dev);
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05302114 /* Decrement the parent device's counter after probe.
2115 * As child is active, parent will not be put into
2116 * suspend mode.
2117 */
2118 if (pdev->dev.parent)
2119 pm_runtime_put_sync(pdev->dev.parent);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302120
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002121 if (mehci->enable_hbm)
Shimrit Malichi08f07342013-04-24 13:09:12 +03002122 hbm_init(hcd, pdata->disable_park_mode);
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002123
Ido Shayevitzaa165c52013-05-12 16:01:28 +02002124 msm_bam_set_hsic_host_dev(&pdev->dev);
2125
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302126 return 0;
2127
Ofir Cohen5dddb152012-11-14 11:18:50 +02002128destroy_wq:
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002129 destroy_workqueue(ehci_wq);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302130deinit_vddcx:
2131 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02002132 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302133deinit_clocks:
2134 msm_hsic_init_clocks(mehci, 0);
2135unmap:
2136 iounmap(hcd->regs);
2137put_hcd:
2138 usb_put_hcd(hcd);
2139
2140 return ret;
2141}
2142
2143static int __devexit ehci_hsic_msm_remove(struct platform_device *pdev)
2144{
2145 struct usb_hcd *hcd = platform_get_drvdata(pdev);
2146 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302147 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
2148
Ido Shayevitzaa165c52013-05-12 16:01:28 +02002149 msm_bam_set_hsic_host_dev(NULL);
2150
Ido Shayevitz65943492013-02-20 18:55:59 +02002151 /* If the device was removed no need to call pm_runtime_disable */
2152 if (pdev->dev.power.power_state.event != PM_EVENT_INVALID)
2153 pm_runtime_disable(&pdev->dev);
2154
2155 pm_runtime_set_suspended(&pdev->dev);
2156
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002157 if (mehci->enable_hbm)
2158 hbm_uninit();
2159
Pavankumar Kondeti21a840e2013-02-12 21:43:56 +05302160 /* Remove the HCD prior to releasing our resources. */
2161 usb_remove_hcd(hcd);
2162
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002163 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302164 pm_qos_remove_request(&mehci->pm_qos_req_dma);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302165
Vamsi Krishna34f01582011-12-14 19:54:42 -08002166 if (mehci->peripheral_status_irq)
2167 free_irq(mehci->peripheral_status_irq, mehci);
Jack Phamfe441ea2012-03-23 17:03:15 -07002168
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002169 if (mehci->wakeup_irq) {
Jack Phamfe441ea2012-03-23 17:03:15 -07002170 if (mehci->wakeup_irq_enabled)
2171 disable_irq_wake(mehci->wakeup_irq);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002172 free_irq(mehci->wakeup_irq, mehci);
2173 }
Vamsi Krishna34f01582011-12-14 19:54:42 -08002174
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302175 if (mehci->async_irq) {
Manu Gautam1a1a9c62013-03-19 17:37:11 +05302176 /* Async IRQ is used only in absence of dedicated wakeup irq */
2177 if (!mehci->wakeup_irq)
2178 disable_irq_wake(mehci->async_irq);
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302179 free_irq(mehci->async_irq, mehci);
2180 }
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002181 /*
2182 * If the update request is called after unregister, the request will
2183 * fail. Results are undefined if unregister is called in the middle of
2184 * update request.
2185 */
2186 mehci->bus_vote = false;
2187 cancel_work_sync(&mehci->bus_vote_w);
2188
Hemant Kumare6275972012-02-29 20:06:21 -08002189 if (mehci->bus_perf_client)
2190 msm_bus_scale_unregister_client(mehci->bus_perf_client);
2191
2192 ehci_hsic_msm_debugfs_cleanup();
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302193 device_init_wakeup(&pdev->dev, 0);
Ido Shayevitze50d9142013-02-17 20:43:26 +02002194
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002195 destroy_workqueue(ehci_wq);
2196
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05302197 msm_hsic_config_gpios(mehci, 0);
Vamsi Krishnabef91832013-03-11 14:10:20 -07002198
2199 if (pdata && pdata->resume_gpio)
2200 gpio_free(pdata->resume_gpio);
2201
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302202 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02002203 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302204
2205 msm_hsic_init_clocks(mehci, 0);
Sudhir Sharma1673e302012-08-27 17:37:24 -07002206 wake_lock_destroy(&mehci->wlock);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302207 iounmap(hcd->regs);
2208 usb_put_hcd(hcd);
2209
2210 return 0;
2211}
2212
2213#ifdef CONFIG_PM_SLEEP
2214static int msm_hsic_pm_suspend(struct device *dev)
2215{
2216 struct usb_hcd *hcd = dev_get_drvdata(dev);
2217 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2218
2219 dev_dbg(dev, "ehci-msm-hsic PM suspend\n");
2220
Hemant Kumar45d211b2012-05-31 17:58:43 -07002221 dbg_log_event(NULL, "PM Suspend", 0);
2222
Pavankumar Kondeti1081f3e2013-04-25 13:51:24 +05302223 if (!atomic_read(&mehci->in_lpm)) {
2224 dev_info(dev, "abort suspend\n");
2225 dbg_log_event(NULL, "PM Suspend abort", 0);
2226 return -EBUSY;
2227 }
2228
Manu Gautam16562792013-03-13 07:04:11 +05302229 if (device_may_wakeup(dev) && !mehci->async_irq)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302230 enable_irq_wake(hcd->irq);
2231
Pavankumar Kondeti1081f3e2013-04-25 13:51:24 +05302232 return 0;
Jack Phamfe441ea2012-03-23 17:03:15 -07002233}
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302234
Jack Pham16b06f82012-08-14 20:03:59 -07002235static int msm_hsic_pm_suspend_noirq(struct device *dev)
2236{
2237 struct usb_hcd *hcd = dev_get_drvdata(dev);
2238 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2239
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05302240 if (atomic_read(&mehci->async_int)) {
Jack Pham16b06f82012-08-14 20:03:59 -07002241 dev_dbg(dev, "suspend_noirq: Aborting due to pending interrupt\n");
2242 return -EBUSY;
2243 }
2244
2245 return 0;
2246}
2247
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302248static int msm_hsic_pm_resume(struct device *dev)
2249{
2250 int ret;
2251 struct usb_hcd *hcd = dev_get_drvdata(dev);
2252 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2253
Jack Pham16b06f82012-08-14 20:03:59 -07002254 dev_dbg(dev, "ehci-msm-hsic PM resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002255 dbg_log_event(NULL, "PM Resume", 0);
2256
Manu Gautam16562792013-03-13 07:04:11 +05302257 if (device_may_wakeup(dev) && !mehci->async_irq)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302258 disable_irq_wake(hcd->irq);
2259
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05302260 /*
2261 * Keep HSIC in Low Power Mode if system is resumed
2262 * by any other wakeup source. HSIC is resumed later
2263 * when remote wakeup is received or interface driver
2264 * start I/O.
2265 */
Pavankumar Kondeti41d004c2012-11-09 10:54:00 +05302266 if (!atomic_read(&mehci->pm_usage_cnt) &&
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05302267 !atomic_read(&mehci->async_int) &&
Pavankumar Kondeti41d004c2012-11-09 10:54:00 +05302268 pm_runtime_suspended(dev))
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05302269 return 0;
2270
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302271 ret = msm_hsic_resume(mehci);
2272 if (ret)
2273 return ret;
2274
2275 /* Bring the device to full powered state upon system resume */
2276 pm_runtime_disable(dev);
2277 pm_runtime_set_active(dev);
2278 pm_runtime_enable(dev);
2279
2280 return 0;
2281}
2282#endif
2283
2284#ifdef CONFIG_PM_RUNTIME
2285static int msm_hsic_runtime_idle(struct device *dev)
2286{
2287 dev_dbg(dev, "EHCI runtime idle\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302288 return 0;
2289}
2290
2291static int msm_hsic_runtime_suspend(struct device *dev)
2292{
2293 struct usb_hcd *hcd = dev_get_drvdata(dev);
2294 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2295
2296 dev_dbg(dev, "EHCI runtime suspend\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002297
2298 dbg_log_event(NULL, "Run Time PM Suspend", 0);
2299
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302300 return msm_hsic_suspend(mehci);
2301}
2302
2303static int msm_hsic_runtime_resume(struct device *dev)
2304{
2305 struct usb_hcd *hcd = dev_get_drvdata(dev);
2306 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2307
2308 dev_dbg(dev, "EHCI runtime resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002309
2310 dbg_log_event(NULL, "Run Time PM Resume", 0);
2311
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302312 return msm_hsic_resume(mehci);
2313}
2314#endif
2315
2316#ifdef CONFIG_PM
2317static const struct dev_pm_ops msm_hsic_dev_pm_ops = {
2318 SET_SYSTEM_SLEEP_PM_OPS(msm_hsic_pm_suspend, msm_hsic_pm_resume)
Jack Pham16b06f82012-08-14 20:03:59 -07002319 .suspend_noirq = msm_hsic_pm_suspend_noirq,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302320 SET_RUNTIME_PM_OPS(msm_hsic_runtime_suspend, msm_hsic_runtime_resume,
2321 msm_hsic_runtime_idle)
2322};
2323#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002324static const struct of_device_id hsic_host_dt_match[] = {
2325 { .compatible = "qcom,hsic-host",
2326 },
2327 {}
2328};
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302329static struct platform_driver ehci_msm_hsic_driver = {
2330 .probe = ehci_hsic_msm_probe,
2331 .remove = __devexit_p(ehci_hsic_msm_remove),
2332 .driver = {
2333 .name = "msm_hsic_host",
2334#ifdef CONFIG_PM
2335 .pm = &msm_hsic_dev_pm_ops,
2336#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002337 .of_match_table = hsic_host_dt_match,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302338 },
2339};