blob: 253658e214eeb3dc4f29b69064fc1c063b697686 [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);
Amit Blay1dfbbcc2013-05-26 17:33:13 +03001065 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301066 int retval;
1067
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001068 mehci->timer = USB_HS_GPTIMER_BASE;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301069 ehci->caps = USB_CAPLENGTH;
1070 ehci->regs = USB_CAPLENGTH +
1071 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
1072 dbg_hcs_params(ehci, "reset");
1073 dbg_hcc_params(ehci, "reset");
1074
1075 /* cache the data to minimize the chip reads*/
1076 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
1077
1078 hcd->has_tt = 1;
1079 ehci->sbrn = HCD_USB2;
1080
1081 retval = ehci_halt(ehci);
1082 if (retval)
1083 return retval;
1084
1085 /* data structure init */
1086 retval = ehci_init(hcd);
1087 if (retval)
1088 return retval;
1089
1090 retval = ehci_reset(ehci);
1091 if (retval)
1092 return retval;
1093
1094 /* bursts of unspecified length. */
1095 writel_relaxed(0, USB_AHBBURST);
Amit Blay1dfbbcc2013-05-26 17:33:13 +03001096
1097 /* Use the AHB transactor and configure async bridge bypass */
1098#define MSM_USB_ASYNC_BRIDGE_BYPASS BIT(31)
1099 if (pdata->ahb_async_bridge_bypass)
1100 writel_relaxed(0x08 | MSM_USB_ASYNC_BRIDGE_BYPASS, USB_AHBMODE);
1101 else
1102 writel_relaxed(0x08, USB_AHBMODE);
1103
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301104 /* Disable streaming mode and select host mode */
1105 writel_relaxed(0x13, USB_USBMODE);
1106
1107 ehci_port_power(ehci, 1);
1108 return 0;
1109}
1110
Jack Phamc0d41732012-09-28 17:45:38 -07001111#ifdef CONFIG_PM
1112
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301113#define RESET_RETRY_LIMIT 3
1114#define RESET_SIGNAL_TIME_SOF_USEC (50 * 1000)
1115#define RESET_SIGNAL_TIME_USEC (20 * 1000)
1116static void ehci_hsic_reset_sof_bug_handler(struct usb_hcd *hcd, u32 val)
1117{
1118 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1119 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1120 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1121 u32 __iomem *status_reg = &ehci->regs->port_status[0];
Pavankumar Kondetifc7d1d82013-03-05 09:43:22 +05301122 u32 cmd;
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301123 unsigned long flags;
1124 int retries = 0, ret, cnt = RESET_SIGNAL_TIME_USEC;
1125
1126 if (pdata && pdata->swfi_latency)
1127 pm_qos_update_request(&mehci->pm_qos_req_dma,
1128 pdata->swfi_latency + 1);
1129
1130 mehci->bus_reset = 1;
Pavankumar Kondetifc7d1d82013-03-05 09:43:22 +05301131
1132 /* Halt the controller */
1133 cmd = ehci_readl(ehci, &ehci->regs->command);
1134 cmd &= ~CMD_RUN;
1135 ehci_writel(ehci, cmd, &ehci->regs->command);
1136 ret = handshake(ehci, &ehci->regs->status, STS_HALT,
1137 STS_HALT, 16 * 125);
1138 if (ret) {
1139 pr_err("halt handshake fatal error\n");
1140 dbg_log_event(NULL, "HALT: fatal", 0);
1141 goto fail;
1142 }
1143
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301144retry:
1145 retries++;
1146 dbg_log_event(NULL, "RESET: start", retries);
1147 pr_debug("reset begin %d\n", retries);
1148 mehci->reset_again = 0;
1149 spin_lock_irqsave(&ehci->lock, flags);
1150 ehci_writel(ehci, val, status_reg);
1151 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_USEC - 1),
1152 &mehci->timer->gptimer0_ld);
1153 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1154 &mehci->timer->gptimer0_ctrl);
1155 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1156 &ehci->regs->intr_enable);
1157
1158 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_SOF_USEC - 1),
1159 &mehci->timer->gptimer1_ld);
1160 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1161 &mehci->timer->gptimer1_ctrl);
1162
1163 spin_unlock_irqrestore(&ehci->lock, flags);
1164 wait_for_completion(&mehci->gpt0_completion);
1165
1166 if (!mehci->reset_again)
1167 goto done;
1168
1169 if (handshake(ehci, status_reg, PORT_RESET, 0, 10 * 1000)) {
1170 pr_err("reset handshake fatal error\n");
1171 dbg_log_event(NULL, "RESET: fatal", retries);
1172 goto fail;
1173 }
1174
1175 if (retries < RESET_RETRY_LIMIT)
1176 goto retry;
1177
1178 /* complete reset in tight loop */
1179 pr_info("RESET in tight loop\n");
1180 dbg_log_event(NULL, "RESET: tight", 0);
1181
1182 spin_lock_irqsave(&ehci->lock, flags);
1183 ehci_writel(ehci, val, status_reg);
1184 while (cnt--)
1185 udelay(1);
1186 ret = msm_hsic_reset_done(hcd);
1187 spin_unlock_irqrestore(&ehci->lock, flags);
1188 if (ret) {
1189 pr_err("RESET in tight loop failed\n");
1190 dbg_log_event(NULL, "RESET: tight failed", 0);
1191 goto fail;
1192 }
1193
1194done:
1195 dbg_log_event(NULL, "RESET: done", retries);
1196 pr_debug("reset completed\n");
1197fail:
1198 mehci->bus_reset = 0;
1199 if (pdata && pdata->swfi_latency)
1200 pm_qos_update_request(&mehci->pm_qos_req_dma,
1201 PM_QOS_DEFAULT_VALUE);
1202}
1203
Hemant Kumar45d211b2012-05-31 17:58:43 -07001204static int ehci_hsic_bus_suspend(struct usb_hcd *hcd)
1205{
Pavankumar Kondetia723f742012-09-14 14:02:36 +05301206 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1207
1208 if (!(readl_relaxed(USB_PORTSC) & PORT_PE)) {
1209 dbg_log_event(NULL, "RH suspend attempt failed", 0);
1210 dev_dbg(mehci->dev, "%s:port is not enabled skip suspend\n",
1211 __func__);
1212 return -EAGAIN;
1213 }
1214
Hemant Kumar45d211b2012-05-31 17:58:43 -07001215 dbg_log_event(NULL, "Suspend RH", 0);
1216 return ehci_bus_suspend(hcd);
1217}
1218
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001219#define RESUME_RETRY_LIMIT 3
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301220#define RESUME_SIGNAL_TIME_USEC (21 * 1000)
1221#define RESUME_SIGNAL_TIME_SOF_USEC (23 * 1000)
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001222static int msm_hsic_resume_thread(void *data)
1223{
1224 struct msm_hsic_hcd *mehci = data;
1225 struct usb_hcd *hcd = hsic_to_hcd(mehci);
1226 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1227 u32 temp;
1228 unsigned long resume_needed = 0;
1229 int retry_cnt = 0;
1230 int tight_resume = 0;
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301231 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001232
1233 dbg_log_event(NULL, "Resume RH", 0);
1234
1235 /* keep delay between bus states */
1236 if (time_before(jiffies, ehci->next_statechange))
1237 usleep_range(5000, 5000);
1238
1239 spin_lock_irq(&ehci->lock);
1240 if (!HCD_HW_ACCESSIBLE(hcd)) {
1241 spin_unlock_irq(&ehci->lock);
1242 mehci->resume_status = -ESHUTDOWN;
1243 complete(&mehci->rt_completion);
1244 return 0;
1245 }
1246
1247 if (unlikely(ehci->debug)) {
1248 if (!dbgp_reset_prep())
1249 ehci->debug = NULL;
1250 else
1251 dbgp_external_startup();
1252 }
1253
1254 /* at least some APM implementations will try to deliver
1255 * IRQs right away, so delay them until we're ready.
1256 */
1257 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1258
1259 /* re-init operational registers */
1260 ehci_writel(ehci, 0, &ehci->regs->segment);
1261 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
1262 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1263
1264 /*CMD_RUN will be set after, PORT_RESUME gets cleared*/
1265 if (ehci->resume_sof_bug)
1266 ehci->command &= ~CMD_RUN;
1267
1268 /* restore CMD_RUN, framelist size, and irq threshold */
1269 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1270
1271 /* manually resume the ports we suspended during bus_suspend() */
1272resume_again:
1273 if (retry_cnt >= RESUME_RETRY_LIMIT) {
1274 pr_info("retry count(%d) reached max, resume in tight loop\n",
1275 retry_cnt);
1276 tight_resume = 1;
1277 }
1278
1279
1280 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1281 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1282 if (test_bit(0, &ehci->bus_suspended) && (temp & PORT_SUSPEND)) {
1283 temp |= PORT_RESUME;
1284 set_bit(0, &resume_needed);
1285 }
1286 dbg_log_event(NULL, "FPR: Set", temp);
1287 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1288
1289 /* HSIC controller has a h/w bug due to which it can try to send SOFs
1290 * (start of frames) during port resume resulting in phy lockup. HSIC hw
1291 * controller in MSM clears FPR bit after driving the resume signal for
1292 * 20ms. Workaround is to stop SOFs before driving resume and then start
1293 * sending SOFs immediately. Need to send SOFs within 3ms of resume
1294 * completion otherwise peripheral may enter undefined state. As
1295 * usleep_range does not gurantee exact sleep time, GPTimer is used to
1296 * to time the resume sequence. If driver exceeds allowable time SOFs,
1297 * repeat the resume process.
1298 */
1299 if (ehci->resume_sof_bug && resume_needed) {
1300 if (!tight_resume) {
1301 mehci->resume_again = 0;
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301302 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_USEC - 1),
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001303 &mehci->timer->gptimer0_ld);
1304 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1305 &mehci->timer->gptimer0_ctrl);
1306 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1307 &ehci->regs->intr_enable);
1308
Pavankumar Kondetic4f817d2013-01-29 19:08:12 +05301309 ehci_writel(ehci, GPT_LD(
1310 RESUME_SIGNAL_TIME_SOF_USEC - 1),
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001311 &mehci->timer->gptimer1_ld);
1312 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1313 &mehci->timer->gptimer1_ctrl);
1314
1315 spin_unlock_irq(&ehci->lock);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301316 if (pdata && pdata->swfi_latency)
1317 pm_qos_update_request(&mehci->pm_qos_req_dma,
1318 pdata->swfi_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001319 wait_for_completion(&mehci->gpt0_completion);
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001320 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301321 pm_qos_update_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001322 pdata->standalone_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001323 spin_lock_irq(&ehci->lock);
1324 } else {
1325 dbg_log_event(NULL, "FPR: Tightloop", 0);
1326 /* do the resume in a tight loop */
1327 handshake(ehci, &ehci->regs->port_status[0],
1328 PORT_RESUME, 0, 22 * 1000);
1329 ehci_writel(ehci, ehci_readl(ehci,
1330 &ehci->regs->command) | CMD_RUN,
1331 &ehci->regs->command);
1332 }
1333
1334 if (mehci->resume_again) {
1335 int temp;
1336
1337 dbg_log_event(NULL, "FPR: Re-Resume", retry_cnt);
1338 pr_info("FPR: retry count: %d\n", retry_cnt);
1339 spin_unlock_irq(&ehci->lock);
1340 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1341 temp &= ~PORT_RWC_BITS;
1342 temp |= PORT_SUSPEND;
1343 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1344 /* Keep the bus idle for 5ms so that peripheral
1345 * can detect and initiate suspend
1346 */
1347 usleep_range(5000, 5000);
1348 dbg_log_event(NULL,
1349 "FPR: RResume",
1350 ehci_readl(ehci, &ehci->regs->port_status[0]));
1351 spin_lock_irq(&ehci->lock);
1352 mehci->resume_again = 0;
1353 retry_cnt++;
1354 goto resume_again;
1355 }
1356 }
1357
1358 dbg_log_event(NULL, "FPR: RT-Done", 0);
1359 mehci->resume_status = 1;
1360 spin_unlock_irq(&ehci->lock);
1361
1362 complete(&mehci->rt_completion);
1363
1364 return 0;
1365}
1366
Hemant Kumar45d211b2012-05-31 17:58:43 -07001367static int ehci_hsic_bus_resume(struct usb_hcd *hcd)
1368{
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001369 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1370 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1371 u32 temp;
1372 struct task_struct *resume_thread = NULL;
Vamsi Krishnabef91832013-03-11 14:10:20 -07001373 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1374
1375 if (pdata->resume_gpio)
1376 gpio_direction_output(pdata->resume_gpio, 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001377
Manu Gautam3c598392013-03-22 16:59:10 +05301378 if (!mehci->ehci.resume_sof_bug) {
1379 ehci_bus_resume(hcd);
1380 } else {
1381 mehci->resume_status = 0;
1382 resume_thread = kthread_run(msm_hsic_resume_thread,
1383 mehci, "hsic_resume_thread");
1384 if (IS_ERR(resume_thread)) {
1385 pr_err("Error creating resume thread:%lu\n",
1386 PTR_ERR(resume_thread));
1387 return PTR_ERR(resume_thread);
1388 }
1389
1390 wait_for_completion(&mehci->rt_completion);
1391
1392 if (mehci->resume_status < 0)
1393 return mehci->resume_status;
1394
1395 dbg_log_event(NULL, "FPR: Wokeup", 0);
1396 spin_lock_irq(&ehci->lock);
1397 (void) ehci_readl(ehci, &ehci->regs->command);
1398
1399 temp = 0;
1400 if (ehci->async->qh_next.qh)
1401 temp |= CMD_ASE;
1402 if (ehci->periodic_sched)
1403 temp |= CMD_PSE;
1404 if (temp) {
1405 ehci->command |= temp;
1406 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1407 }
1408
1409 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
1410 hcd->state = HC_STATE_RUNNING;
1411 ehci->rh_state = EHCI_RH_RUNNING;
1412 ehci->command |= CMD_RUN;
1413
1414 /* Now we can safely re-enable irqs */
1415 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1416
1417 spin_unlock_irq(&ehci->lock);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001418 }
1419
Vamsi Krishnabef91832013-03-11 14:10:20 -07001420 if (pdata->resume_gpio)
1421 gpio_direction_output(pdata->resume_gpio, 0);
1422
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001423 return 0;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001424}
1425
Jack Phamc0d41732012-09-28 17:45:38 -07001426#else
1427
1428#define ehci_hsic_bus_suspend NULL
1429#define ehci_hsic_bus_resume NULL
1430
1431#endif /* CONFIG_PM */
1432
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001433static void ehci_msm_set_autosuspend_delay(struct usb_device *dev)
1434{
1435 if (!dev->parent) /*for root hub no delay*/
1436 pm_runtime_set_autosuspend_delay(&dev->dev, 0);
1437 else
1438 pm_runtime_set_autosuspend_delay(&dev->dev, 200);
1439}
1440
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001441static int ehci_msm_urb_enqueue(struct usb_hcd *hcd, struct urb *urb,
1442 gfp_t mem_flags)
1443{
1444 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1445 struct usb_host_bam_type *usb_host_bam =
1446 (struct usb_host_bam_type *)urb->priv_data;
1447
1448 if (usb_host_bam && mehci && mehci->enable_hbm)
1449 return hbm_urb_enqueue(hcd, urb, mem_flags);
1450 return ehci_urb_enqueue(hcd, urb, mem_flags);
1451}
1452
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301453static struct hc_driver msm_hsic_driver = {
1454 .description = hcd_name,
1455 .product_desc = "Qualcomm EHCI Host Controller using HSIC",
1456 .hcd_priv_size = sizeof(struct msm_hsic_hcd),
1457
1458 /*
1459 * generic hardware linkage
1460 */
1461 .irq = msm_hsic_irq,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001462 .flags = HCD_USB2 | HCD_MEMORY | HCD_OLD_ENUM,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301463
1464 .reset = ehci_hsic_reset,
1465 .start = ehci_run,
1466
1467 .stop = ehci_stop,
1468 .shutdown = ehci_shutdown,
1469
1470 /*
1471 * managing i/o requests and associated device resources
1472 */
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001473 .urb_enqueue = ehci_msm_urb_enqueue,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301474 .urb_dequeue = ehci_urb_dequeue,
1475 .endpoint_disable = ehci_endpoint_disable,
1476 .endpoint_reset = ehci_endpoint_reset,
1477 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1478
1479 /*
1480 * scheduling support
1481 */
1482 .get_frame_number = ehci_get_frame,
1483
1484 /*
1485 * root hub support
1486 */
1487 .hub_status_data = ehci_hub_status_data,
1488 .hub_control = ehci_hub_control,
1489 .relinquish_port = ehci_relinquish_port,
1490 .port_handed_over = ehci_port_handed_over,
1491
1492 /*
1493 * PM support
1494 */
Hemant Kumar45d211b2012-05-31 17:58:43 -07001495 .bus_suspend = ehci_hsic_bus_suspend,
1496 .bus_resume = ehci_hsic_bus_resume,
1497
Hemant Kumar4d50a432012-08-15 09:06:35 -07001498 .log_urb = dbg_log_event,
Hemant Kumar105d07f2012-07-02 15:33:07 -07001499 .dump_regs = dump_hsic_regs,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001500
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001501 .set_autosuspend_delay = ehci_msm_set_autosuspend_delay,
Pavankumar Kondeti23720492013-01-29 14:41:58 +05301502 .reset_sof_bug_handler = ehci_hsic_reset_sof_bug_handler,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301503};
1504
1505static int msm_hsic_init_clocks(struct msm_hsic_hcd *mehci, u32 init)
1506{
1507 int ret = 0;
1508
1509 if (!init)
1510 goto put_clocks;
1511
Lena Salman8c8ba382012-02-14 15:59:31 +02001512 /*core_clk is required for LINK protocol engine
1513 *clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001514 mehci->core_clk = clk_get(mehci->dev, "core_clk");
1515 if (IS_ERR(mehci->core_clk)) {
1516 dev_err(mehci->dev, "failed to get core_clk\n");
1517 ret = PTR_ERR(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301518 return ret;
1519 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301520
Ofir Cohen07944aa2012-12-23 13:41:57 +02001521 /* alt_core_clk is for LINK to be used during PHY RESET in
1522 * targets on which link does NOT use asynchronous reset methodology.
Lena Salman8c8ba382012-02-14 15:59:31 +02001523 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001524 mehci->alt_core_clk = clk_get(mehci->dev, "alt_core_clk");
Manu Gautam863b74132012-11-21 14:30:04 +05301525 if (IS_ERR(mehci->alt_core_clk))
1526 dev_dbg(mehci->dev, "failed to get alt_core_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301527
Lena Salman8c8ba382012-02-14 15:59:31 +02001528 /* phy_clk is required for HSIC PHY operation
1529 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001530 mehci->phy_clk = clk_get(mehci->dev, "phy_clk");
1531 if (IS_ERR(mehci->phy_clk)) {
1532 dev_err(mehci->dev, "failed to get phy_clk\n");
1533 ret = PTR_ERR(mehci->phy_clk);
1534 goto put_alt_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301535 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301536
1537 /* 10MHz cal_clk is required for calibration of I/O pads */
Manu Gautam5143b252012-01-05 19:25:23 -08001538 mehci->cal_clk = clk_get(mehci->dev, "cal_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301539 if (IS_ERR(mehci->cal_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001540 dev_err(mehci->dev, "failed to get cal_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301541 ret = PTR_ERR(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001542 goto put_phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301543 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301544
1545 /* ahb_clk is required for data transfers */
Manu Gautam5143b252012-01-05 19:25:23 -08001546 mehci->ahb_clk = clk_get(mehci->dev, "iface_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301547 if (IS_ERR(mehci->ahb_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001548 dev_err(mehci->dev, "failed to get iface_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301549 ret = PTR_ERR(mehci->ahb_clk);
1550 goto put_cal_clk;
1551 }
1552
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001553 /*
1554 * Inactivity_clk is required for hsic bam inactivity timer.
1555 * This clock is not compulsory and is defined in clock lookup
1556 * only for targets that need to use the inactivity timer feature.
1557 */
1558 mehci->inactivity_clk = clk_get(mehci->dev, "inactivity_clk");
1559 if (IS_ERR(mehci->inactivity_clk))
1560 dev_dbg(mehci->dev, "failed to get inactivity_clk\n");
1561
Manu Gautam28b1bac2012-01-30 16:43:06 +05301562 clk_prepare_enable(mehci->core_clk);
1563 clk_prepare_enable(mehci->phy_clk);
1564 clk_prepare_enable(mehci->cal_clk);
1565 clk_prepare_enable(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001566 if (!IS_ERR(mehci->inactivity_clk))
1567 clk_prepare_enable(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301568
1569 return 0;
1570
1571put_clocks:
Jack Phamfd193eb2012-02-22 15:38:08 -08001572 if (!atomic_read(&mehci->in_lpm)) {
1573 clk_disable_unprepare(mehci->core_clk);
1574 clk_disable_unprepare(mehci->phy_clk);
1575 clk_disable_unprepare(mehci->cal_clk);
1576 clk_disable_unprepare(mehci->ahb_clk);
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001577 if (!IS_ERR(mehci->inactivity_clk))
1578 clk_disable_unprepare(mehci->inactivity_clk);
Jack Phamfd193eb2012-02-22 15:38:08 -08001579 }
Ido Shayevitz5dfbb132013-04-18 13:22:07 +03001580 if (!IS_ERR(mehci->inactivity_clk))
1581 clk_put(mehci->inactivity_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301582 clk_put(mehci->ahb_clk);
1583put_cal_clk:
1584 clk_put(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001585put_phy_clk:
1586 clk_put(mehci->phy_clk);
1587put_alt_core_clk:
Manu Gautam863b74132012-11-21 14:30:04 +05301588 if (!IS_ERR(mehci->alt_core_clk))
1589 clk_put(mehci->alt_core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001590 clk_put(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301591
1592 return ret;
1593}
Vamsi Krishna34f01582011-12-14 19:54:42 -08001594static irqreturn_t hsic_peripheral_status_change(int irq, void *dev_id)
1595{
1596 struct msm_hsic_hcd *mehci = dev_id;
1597
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001598 pr_debug("%s: mehci:%p dev_id:%p\n", __func__, mehci, dev_id);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001599
1600 if (mehci)
1601 msm_hsic_config_gpios(mehci, 0);
1602
1603 return IRQ_HANDLED;
1604}
1605
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001606static irqreturn_t msm_hsic_wakeup_irq(int irq, void *data)
1607{
1608 struct msm_hsic_hcd *mehci = data;
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301609 int ret;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001610
Manu Gautam3fb60ca2013-02-13 18:33:33 +05301611 if (irq == mehci->async_irq) {
1612 mehci->async_int_cnt++;
1613 dbg_log_event(NULL, "Remote Wakeup (ASYNC) IRQ",
1614 mehci->async_int_cnt);
1615 } else {
1616 mehci->wakeup_int_cnt++;
1617 dbg_log_event(NULL, "Remote Wakeup IRQ", mehci->wakeup_int_cnt);
1618 }
1619 dev_dbg(mehci->dev, "%s: hsic remote wakeup interrupt %d cnt: %u, %u\n",
1620 __func__, irq, mehci->wakeup_int_cnt, mehci->async_int_cnt);
Hemant Kumar6fd65032012-05-23 13:02:24 -07001621
Sudhir Sharma1673e302012-08-27 17:37:24 -07001622 wake_lock(&mehci->wlock);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001623
Manu Gautam3fb60ca2013-02-13 18:33:33 +05301624 if (mehci->wakeup_irq) {
1625 spin_lock(&mehci->wakeup_lock);
1626 if (mehci->wakeup_irq_enabled) {
1627 mehci->wakeup_irq_enabled = 0;
1628 disable_irq_wake(irq);
1629 disable_irq_nosync(irq);
1630 }
1631 spin_unlock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001632 }
1633
Sudhir Sharma1673e302012-08-27 17:37:24 -07001634 if (!atomic_read(&mehci->pm_usage_cnt)) {
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301635 ret = pm_runtime_get(mehci->dev);
1636 /*
1637 * HSIC runtime resume can race with us.
1638 * if we are active (ret == 1) or resuming
1639 * (ret == -EINPROGRESS), decrement the
1640 * PM usage counter before returning.
1641 */
1642 if ((ret == 1) || (ret == -EINPROGRESS))
1643 pm_runtime_put_noidle(mehci->dev);
1644 else
1645 atomic_set(&mehci->pm_usage_cnt, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07001646 }
1647
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001648 return IRQ_HANDLED;
1649}
1650
Hemant Kumare6275972012-02-29 20:06:21 -08001651static int ehci_hsic_msm_bus_show(struct seq_file *s, void *unused)
1652{
1653 if (debug_bus_voting_enabled)
1654 seq_printf(s, "enabled\n");
1655 else
1656 seq_printf(s, "disabled\n");
1657
1658 return 0;
1659}
1660
1661static int ehci_hsic_msm_bus_open(struct inode *inode, struct file *file)
1662{
1663 return single_open(file, ehci_hsic_msm_bus_show, inode->i_private);
1664}
1665
1666static ssize_t ehci_hsic_msm_bus_write(struct file *file,
1667 const char __user *ubuf, size_t count, loff_t *ppos)
1668{
1669 char buf[8];
1670 int ret;
1671 struct seq_file *s = file->private_data;
1672 struct msm_hsic_hcd *mehci = s->private;
1673
1674 memset(buf, 0x00, sizeof(buf));
1675
1676 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1677 return -EFAULT;
1678
1679 if (!strncmp(buf, "enable", 6)) {
1680 /* Do not vote here. Let hsic driver decide when to vote */
1681 debug_bus_voting_enabled = true;
1682 } else {
1683 debug_bus_voting_enabled = false;
1684 if (mehci->bus_perf_client) {
1685 ret = msm_bus_scale_client_update_request(
1686 mehci->bus_perf_client, 0);
1687 if (ret)
1688 dev_err(mehci->dev, "%s: Failed to devote "
1689 "for bus bw %d\n", __func__, ret);
1690 }
1691 }
1692
1693 return count;
1694}
1695
1696const struct file_operations ehci_hsic_msm_bus_fops = {
1697 .open = ehci_hsic_msm_bus_open,
1698 .read = seq_read,
1699 .write = ehci_hsic_msm_bus_write,
1700 .llseek = seq_lseek,
1701 .release = single_release,
1702};
1703
Hemant Kumar6fd65032012-05-23 13:02:24 -07001704static int ehci_hsic_msm_wakeup_cnt_show(struct seq_file *s, void *unused)
1705{
1706 struct msm_hsic_hcd *mehci = s->private;
1707
1708 seq_printf(s, "%u\n", mehci->wakeup_int_cnt);
1709
1710 return 0;
1711}
1712
1713static int ehci_hsic_msm_wakeup_cnt_open(struct inode *inode, struct file *f)
1714{
1715 return single_open(f, ehci_hsic_msm_wakeup_cnt_show, inode->i_private);
1716}
1717
1718const struct file_operations ehci_hsic_msm_wakeup_cnt_fops = {
1719 .open = ehci_hsic_msm_wakeup_cnt_open,
1720 .read = seq_read,
1721 .llseek = seq_lseek,
1722 .release = single_release,
1723};
1724
Hemant Kumar45d211b2012-05-31 17:58:43 -07001725static int ehci_hsic_msm_data_events_show(struct seq_file *s, void *unused)
1726{
1727 unsigned long flags;
1728 unsigned i;
1729
1730 read_lock_irqsave(&dbg_hsic_data.lck, flags);
1731
1732 i = dbg_hsic_data.idx;
1733 for (dbg_inc(&i); i != dbg_hsic_data.idx; dbg_inc(&i)) {
1734 if (!strnlen(dbg_hsic_data.buf[i], DBG_MSG_LEN))
1735 continue;
1736 seq_printf(s, "%s\n", dbg_hsic_data.buf[i]);
1737 }
1738
1739 read_unlock_irqrestore(&dbg_hsic_data.lck, flags);
1740
1741 return 0;
1742}
1743
1744static int ehci_hsic_msm_data_events_open(struct inode *inode, struct file *f)
1745{
1746 return single_open(f, ehci_hsic_msm_data_events_show, inode->i_private);
1747}
1748
1749const struct file_operations ehci_hsic_msm_dbg_data_fops = {
1750 .open = ehci_hsic_msm_data_events_open,
1751 .read = seq_read,
1752 .llseek = seq_lseek,
1753 .release = single_release,
1754};
1755
1756static int ehci_hsic_msm_ctrl_events_show(struct seq_file *s, void *unused)
1757{
1758 unsigned long flags;
1759 unsigned i;
1760
1761 read_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
1762
1763 i = dbg_hsic_ctrl.idx;
1764 for (dbg_inc(&i); i != dbg_hsic_ctrl.idx; dbg_inc(&i)) {
1765 if (!strnlen(dbg_hsic_ctrl.buf[i], DBG_MSG_LEN))
1766 continue;
1767 seq_printf(s, "%s\n", dbg_hsic_ctrl.buf[i]);
1768 }
1769
1770 read_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
1771
1772 return 0;
1773}
1774
1775static int ehci_hsic_msm_ctrl_events_open(struct inode *inode, struct file *f)
1776{
1777 return single_open(f, ehci_hsic_msm_ctrl_events_show, inode->i_private);
1778}
1779
1780const struct file_operations ehci_hsic_msm_dbg_ctrl_fops = {
1781 .open = ehci_hsic_msm_ctrl_events_open,
1782 .read = seq_read,
1783 .llseek = seq_lseek,
1784 .release = single_release,
1785};
1786
Hemant Kumare6275972012-02-29 20:06:21 -08001787static struct dentry *ehci_hsic_msm_dbg_root;
1788static int ehci_hsic_msm_debugfs_init(struct msm_hsic_hcd *mehci)
1789{
1790 struct dentry *ehci_hsic_msm_dentry;
1791
1792 ehci_hsic_msm_dbg_root = debugfs_create_dir("ehci_hsic_msm_dbg", NULL);
1793
1794 if (!ehci_hsic_msm_dbg_root || IS_ERR(ehci_hsic_msm_dbg_root))
1795 return -ENODEV;
1796
1797 ehci_hsic_msm_dentry = debugfs_create_file("bus_voting",
1798 S_IRUGO | S_IWUSR,
1799 ehci_hsic_msm_dbg_root, mehci,
1800 &ehci_hsic_msm_bus_fops);
1801
1802 if (!ehci_hsic_msm_dentry) {
1803 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1804 return -ENODEV;
1805 }
1806
Hemant Kumar6fd65032012-05-23 13:02:24 -07001807 ehci_hsic_msm_dentry = debugfs_create_file("wakeup_cnt",
1808 S_IRUGO,
1809 ehci_hsic_msm_dbg_root, mehci,
1810 &ehci_hsic_msm_wakeup_cnt_fops);
1811
1812 if (!ehci_hsic_msm_dentry) {
1813 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1814 return -ENODEV;
1815 }
1816
Hemant Kumar45d211b2012-05-31 17:58:43 -07001817 ehci_hsic_msm_dentry = debugfs_create_file("show_ctrl_events",
1818 S_IRUGO,
1819 ehci_hsic_msm_dbg_root, mehci,
1820 &ehci_hsic_msm_dbg_ctrl_fops);
1821
1822 if (!ehci_hsic_msm_dentry) {
1823 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1824 return -ENODEV;
1825 }
1826
1827 ehci_hsic_msm_dentry = debugfs_create_file("show_data_events",
1828 S_IRUGO,
1829 ehci_hsic_msm_dbg_root, mehci,
1830 &ehci_hsic_msm_dbg_data_fops);
1831
1832 if (!ehci_hsic_msm_dentry) {
1833 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1834 return -ENODEV;
1835 }
1836
Hemant Kumare6275972012-02-29 20:06:21 -08001837 return 0;
1838}
1839
1840static void ehci_hsic_msm_debugfs_cleanup(void)
1841{
1842 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1843}
1844
Manu Gautam863b74132012-11-21 14:30:04 +05301845struct msm_hsic_host_platform_data *msm_hsic_dt_to_pdata(
1846 struct platform_device *pdev)
1847{
1848 struct device_node *node = pdev->dev.of_node;
1849 struct msm_hsic_host_platform_data *pdata;
Ido Shayevitzca5dc492013-01-09 14:49:59 +02001850 int res_gpio;
Manu Gautam863b74132012-11-21 14:30:04 +05301851
1852 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1853 if (!pdata) {
1854 dev_err(&pdev->dev, "unable to allocate platform data\n");
1855 return NULL;
1856 }
Manu Gautam863b74132012-11-21 14:30:04 +05301857
Ido Shayevitzca5dc492013-01-09 14:49:59 +02001858 res_gpio = of_get_named_gpio(node, "hsic,strobe-gpio", 0);
1859 if (res_gpio < 0)
1860 res_gpio = 0;
1861 pdata->strobe = res_gpio;
1862
1863 res_gpio = of_get_named_gpio(node, "hsic,data-gpio", 0);
1864 if (res_gpio < 0)
1865 res_gpio = 0;
1866 pdata->data = res_gpio;
Manu Gautam863b74132012-11-21 14:30:04 +05301867
Vamsi Krishnabef91832013-03-11 14:10:20 -07001868 res_gpio = of_get_named_gpio(node, "hsic,resume-gpio", 0);
1869 if (res_gpio < 0)
1870 res_gpio = 0;
1871 pdata->resume_gpio = res_gpio;
1872
Manu Gautam3c598392013-03-22 16:59:10 +05301873 pdata->phy_sof_workaround = of_property_read_bool(node,
1874 "qcom,phy-sof-workaround");
Manu Gautam863b74132012-11-21 14:30:04 +05301875 pdata->ignore_cal_pad_config = of_property_read_bool(node,
1876 "hsic,ignore-cal-pad-config");
1877 of_property_read_u32(node, "hsic,strobe-pad-offset",
1878 &pdata->strobe_pad_offset);
1879 of_property_read_u32(node, "hsic,data-pad-offset",
1880 &pdata->data_pad_offset);
Ming-yi Lin9eed5e02013-07-18 14:06:24 +08001881 of_property_read_u32(node, "hsic,reset-delay",
1882 &pdata->reset_delay);
Vamsi Krishnafaee5da2013-06-14 12:41:10 -07001883 of_property_read_u32(node, "hsic,log2-itc",
1884 &pdata->log2_irq_thresh);
1885 if (pdata->log2_irq_thresh > 6)
1886 pdata->log2_irq_thresh = 0;
Manu Gautam863b74132012-11-21 14:30:04 +05301887
Ido Shayevitz95c46882013-01-20 13:47:46 +02001888 pdata->bus_scale_table = msm_bus_cl_get_pdata(pdev);
1889
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001890 pdata->pool_64_bit_align = of_property_read_bool(node,
1891 "qcom,pool-64-bit-align");
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001892 pdata->enable_hbm = of_property_read_bool(node,
1893 "qcom,enable-hbm");
Shimrit Malichi08f07342013-04-24 13:09:12 +03001894 pdata->disable_park_mode = (of_property_read_bool(node,
1895 "qcom,disable-park-mode"));
Ido Shayevitzaa165c52013-05-12 16:01:28 +02001896 pdata->consider_ipa_handshake = (of_property_read_bool(node,
1897 "hsic,consider-ipa-handshake"));
Amit Blay1dfbbcc2013-05-26 17:33:13 +03001898 pdata->ahb_async_bridge_bypass = of_property_read_bool(node,
1899 "qcom,ahb-async-bridge-bypass");
Vamsi Krishna4f66c2c2013-06-21 11:33:59 -07001900 pdata->disable_cerr = of_property_read_bool(node,
1901 "hsic,disable-cerr");
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001902
Manu Gautam863b74132012-11-21 14:30:04 +05301903 return pdata;
1904}
1905
1906
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301907static int __devinit ehci_hsic_msm_probe(struct platform_device *pdev)
1908{
1909 struct usb_hcd *hcd;
1910 struct resource *res;
1911 struct msm_hsic_hcd *mehci;
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +05301912 struct msm_hsic_host_platform_data *pdata;
Manu Gautam16562792013-03-13 07:04:11 +05301913 unsigned long wakeup_irq_flags = 0;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301914 int ret;
1915
1916 dev_dbg(&pdev->dev, "ehci_msm-hsic probe\n");
1917
Manu Gautam863b74132012-11-21 14:30:04 +05301918 if (pdev->dev.of_node) {
1919 dev_dbg(&pdev->dev, "device tree enabled\n");
1920 pdev->dev.platform_data = msm_hsic_dt_to_pdata(pdev);
1921 dev_set_name(&pdev->dev, ehci_msm_hsic_driver.driver.name);
Manu Gautam16562792013-03-13 07:04:11 +05301922 } else {
1923 /* explicitly pass wakeup_irq flag for !DT */
1924 wakeup_irq_flags = IRQF_TRIGGER_HIGH;
Manu Gautam863b74132012-11-21 14:30:04 +05301925 }
1926 if (!pdev->dev.platform_data)
1927 dev_dbg(&pdev->dev, "No platform data given\n");
1928
Ofir Cohen4cc55372012-11-20 11:17:24 +02001929 if (!pdev->dev.dma_mask)
1930 pdev->dev.dma_mask = &ehci_msm_hsic_dma_mask;
1931 if (!pdev->dev.coherent_dma_mask)
1932 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1933
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301934 /* After parent device's probe is executed, it will be put in suspend
1935 * mode. When child device's probe is called, driver core is not
1936 * resuming parent device due to which parent will be in suspend even
1937 * though child is active. Hence resume the parent device explicitly.
1938 */
1939 if (pdev->dev.parent)
1940 pm_runtime_get_sync(pdev->dev.parent);
1941
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301942 hcd = usb_create_hcd(&msm_hsic_driver, &pdev->dev,
1943 dev_name(&pdev->dev));
1944 if (!hcd) {
1945 dev_err(&pdev->dev, "Unable to create HCD\n");
1946 return -ENOMEM;
1947 }
1948
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05301949 hcd_to_bus(hcd)->skip_resume = true;
1950
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301951 hcd->irq = platform_get_irq(pdev, 0);
1952 if (hcd->irq < 0) {
1953 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1954 ret = hcd->irq;
1955 goto put_hcd;
1956 }
1957
1958 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1959 if (!res) {
1960 dev_err(&pdev->dev, "Unable to get memory resource\n");
1961 ret = -ENODEV;
1962 goto put_hcd;
1963 }
1964
1965 hcd->rsrc_start = res->start;
1966 hcd->rsrc_len = resource_size(res);
1967 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1968 if (!hcd->regs) {
1969 dev_err(&pdev->dev, "ioremap failed\n");
1970 ret = -ENOMEM;
1971 goto put_hcd;
1972 }
1973
1974 mehci = hcd_to_hsic(hcd);
1975 mehci->dev = &pdev->dev;
Hemant Kumar30d361c2012-08-20 14:44:40 -07001976 pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301977
Hemant Kumard343c012012-09-06 19:57:14 -07001978 spin_lock_init(&mehci->wakeup_lock);
1979
Manu Gautam3c598392013-03-22 16:59:10 +05301980 if (pdata->phy_sof_workaround) {
1981 mehci->ehci.susp_sof_bug = 1;
1982 mehci->ehci.reset_sof_bug = 1;
1983 mehci->ehci.resume_sof_bug = 1;
1984 }
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001985
Ming-yi Lin9eed5e02013-07-18 14:06:24 +08001986 if (pdata->reset_delay)
1987 mehci->ehci.reset_delay = pdata->reset_delay;
1988
Shimrit Malichi36a0c852013-03-10 11:39:10 +02001989 mehci->ehci.pool_64_bit_align = pdata->pool_64_bit_align;
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02001990 mehci->enable_hbm = pdata->enable_hbm;
Hemant Kumare4040492012-06-21 17:35:42 -07001991
Vamsi Krishna4f66c2c2013-06-21 11:33:59 -07001992 if (pdata) {
Hemant Kumar30d361c2012-08-20 14:44:40 -07001993 mehci->ehci.log2_irq_thresh = pdata->log2_irq_thresh;
Vamsi Krishna4f66c2c2013-06-21 11:33:59 -07001994 mehci->ehci.disable_cerr = pdata->disable_cerr;
1995 }
Hemant Kumar933e0402012-05-22 11:11:40 -07001996
Ofir Cohen4cc55372012-11-20 11:17:24 +02001997 ret = msm_hsic_init_gdsc(mehci, 1);
1998 if (ret) {
1999 dev_err(&pdev->dev, "unable to initialize GDSC\n");
2000 ret = -ENODEV;
2001 goto put_hcd;
2002 }
2003
Vamsi Krishna34f01582011-12-14 19:54:42 -08002004 res = platform_get_resource_byname(pdev,
2005 IORESOURCE_IRQ,
2006 "peripheral_status_irq");
2007 if (res)
2008 mehci->peripheral_status_irq = res->start;
2009
Jack Pham0cc75c42012-10-10 02:03:50 +02002010 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "wakeup");
Hemant Kumar6fd65032012-05-23 13:02:24 -07002011 if (res) {
Jack Pham0cc75c42012-10-10 02:03:50 +02002012 mehci->wakeup_irq = res->start;
Hemant Kumar6fd65032012-05-23 13:02:24 -07002013 dev_dbg(mehci->dev, "wakeup_irq: %d\n", mehci->wakeup_irq);
2014 }
2015
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302016 ret = msm_hsic_init_clocks(mehci, 1);
2017 if (ret) {
2018 dev_err(&pdev->dev, "unable to initialize clocks\n");
2019 ret = -ENODEV;
2020 goto unmap;
2021 }
2022
2023 ret = msm_hsic_init_vddcx(mehci, 1);
2024 if (ret) {
2025 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
2026 ret = -ENODEV;
2027 goto deinit_clocks;
2028 }
2029
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07002030 init_completion(&mehci->rt_completion);
2031 init_completion(&mehci->gpt0_completion);
Ofir Cohen5dddb152012-11-14 11:18:50 +02002032
2033 msm_hsic_phy_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302034
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002035 ehci_wq = create_singlethread_workqueue("ehci_wq");
2036 if (!ehci_wq) {
2037 dev_err(&pdev->dev, "unable to create workqueue\n");
2038 ret = -ENOMEM;
2039 goto deinit_vddcx;
2040 }
2041
2042 INIT_WORK(&mehci->bus_vote_w, ehci_hsic_bus_vote_w);
2043
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302044 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
2045 if (ret) {
2046 dev_err(&pdev->dev, "unable to register HCD\n");
Ofir Cohen5dddb152012-11-14 11:18:50 +02002047 goto destroy_wq;
2048 }
2049
2050 ret = msm_hsic_start(mehci);
2051 if (ret) {
2052 dev_err(&pdev->dev, "unable to initialize PHY\n");
2053 goto destroy_wq;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302054 }
2055
2056 device_init_wakeup(&pdev->dev, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07002057 wake_lock_init(&mehci->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
2058 wake_lock(&mehci->wlock);
Vamsi Krishna34f01582011-12-14 19:54:42 -08002059
2060 if (mehci->peripheral_status_irq) {
2061 ret = request_threaded_irq(mehci->peripheral_status_irq,
2062 NULL, hsic_peripheral_status_change,
2063 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
2064 | IRQF_SHARED,
2065 "hsic_peripheral_status", mehci);
2066 if (ret)
2067 dev_err(&pdev->dev, "%s:request_irq:%d failed:%d",
2068 __func__, mehci->peripheral_status_irq, ret);
2069 }
2070
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002071 /* configure wakeup irq */
Hemant Kumar6fd65032012-05-23 13:02:24 -07002072 if (mehci->wakeup_irq) {
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07002073 /* In case if wakeup gpio is pulled high at this point
2074 * remote wakeup interrupt fires right after request_irq.
2075 * Remote wake up interrupt only needs to be enabled when
2076 * HSIC bus goes to suspend.
2077 */
2078 irq_set_status_flags(mehci->wakeup_irq, IRQ_NOAUTOEN);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002079 ret = request_irq(mehci->wakeup_irq, msm_hsic_wakeup_irq,
Manu Gautam16562792013-03-13 07:04:11 +05302080 wakeup_irq_flags,
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002081 "msm_hsic_wakeup", mehci);
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07002082 if (ret) {
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002083 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
2084 mehci->wakeup_irq, ret);
2085 mehci->wakeup_irq = 0;
2086 }
2087 }
2088
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302089 mehci->async_irq = platform_get_irq_byname(pdev, "async_irq");
2090 if (mehci->async_irq < 0) {
2091 dev_dbg(&pdev->dev, "platform_get_irq for async_int failed\n");
2092 mehci->async_irq = 0;
2093 } else {
2094 ret = request_irq(mehci->async_irq, msm_hsic_wakeup_irq,
2095 IRQF_TRIGGER_RISING, "msm_hsic_async", mehci);
2096 if (ret) {
2097 dev_err(&pdev->dev, "request irq failed (ASYNC INT)\n");
2098 mehci->async_irq = 0;
Manu Gautam16562792013-03-13 07:04:11 +05302099 } else if (!mehci->wakeup_irq) {
2100 /* Async IRQ is used only in absence of dedicated irq */
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302101 enable_irq_wake(mehci->async_irq);
2102 }
2103 }
2104
Hemant Kumare6275972012-02-29 20:06:21 -08002105 ret = ehci_hsic_msm_debugfs_init(mehci);
2106 if (ret)
2107 dev_dbg(&pdev->dev, "mode debugfs file is"
2108 "not available\n");
2109
2110 if (pdata && pdata->bus_scale_table) {
2111 mehci->bus_perf_client =
2112 msm_bus_scale_register_client(pdata->bus_scale_table);
2113 /* Configure BUS performance parameters for MAX bandwidth */
2114 if (mehci->bus_perf_client) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002115 mehci->bus_vote = true;
2116 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -08002117 } else {
2118 dev_err(&pdev->dev, "%s: Failed to register BUS "
2119 "scaling client!!\n", __func__);
2120 }
2121 }
2122
Hemant Kumar105d07f2012-07-02 15:33:07 -07002123 __mehci = mehci;
2124
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002125 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302126 pm_qos_add_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002127 PM_QOS_CPU_DMA_LATENCY, pdata->standalone_latency + 1);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302128
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302129 /*
2130 * This pdev->dev is assigned parent of root-hub by USB core,
2131 * hence, runtime framework automatically calls this driver's
2132 * runtime APIs based on root-hub's state.
2133 */
2134 pm_runtime_set_active(&pdev->dev);
2135 pm_runtime_enable(&pdev->dev);
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05302136 /* Decrement the parent device's counter after probe.
2137 * As child is active, parent will not be put into
2138 * suspend mode.
2139 */
2140 if (pdev->dev.parent)
2141 pm_runtime_put_sync(pdev->dev.parent);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302142
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002143 if (mehci->enable_hbm)
Shimrit Malichi08f07342013-04-24 13:09:12 +03002144 hbm_init(hcd, pdata->disable_park_mode);
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002145
Jack Pham06306d52013-06-06 15:28:13 -07002146 if (pdata && pdata->consider_ipa_handshake)
2147 msm_bam_set_hsic_host_dev(&pdev->dev);
Ido Shayevitzaa165c52013-05-12 16:01:28 +02002148
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302149 return 0;
2150
Ofir Cohen5dddb152012-11-14 11:18:50 +02002151destroy_wq:
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002152 destroy_workqueue(ehci_wq);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302153deinit_vddcx:
2154 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02002155 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302156deinit_clocks:
2157 msm_hsic_init_clocks(mehci, 0);
2158unmap:
2159 iounmap(hcd->regs);
2160put_hcd:
2161 usb_put_hcd(hcd);
2162
2163 return ret;
2164}
2165
2166static int __devexit ehci_hsic_msm_remove(struct platform_device *pdev)
2167{
2168 struct usb_hcd *hcd = platform_get_drvdata(pdev);
2169 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302170 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
2171
Jack Pham06306d52013-06-06 15:28:13 -07002172 if (pdata && pdata->consider_ipa_handshake)
2173 msm_bam_set_hsic_host_dev(NULL);
Ido Shayevitzaa165c52013-05-12 16:01:28 +02002174
Ido Shayevitz65943492013-02-20 18:55:59 +02002175 /* If the device was removed no need to call pm_runtime_disable */
2176 if (pdev->dev.power.power_state.event != PM_EVENT_INVALID)
2177 pm_runtime_disable(&pdev->dev);
2178
2179 pm_runtime_set_suspended(&pdev->dev);
2180
Shimrit Malichi82d7cbc2013-03-12 15:36:54 +02002181 if (mehci->enable_hbm)
2182 hbm_uninit();
2183
Pavankumar Kondeti21a840e2013-02-12 21:43:56 +05302184 /* Remove the HCD prior to releasing our resources. */
2185 usb_remove_hcd(hcd);
2186
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07002187 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05302188 pm_qos_remove_request(&mehci->pm_qos_req_dma);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302189
Vamsi Krishna34f01582011-12-14 19:54:42 -08002190 if (mehci->peripheral_status_irq)
2191 free_irq(mehci->peripheral_status_irq, mehci);
Jack Phamfe441ea2012-03-23 17:03:15 -07002192
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002193 if (mehci->wakeup_irq) {
Jack Phamfe441ea2012-03-23 17:03:15 -07002194 if (mehci->wakeup_irq_enabled)
2195 disable_irq_wake(mehci->wakeup_irq);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08002196 free_irq(mehci->wakeup_irq, mehci);
2197 }
Vamsi Krishna34f01582011-12-14 19:54:42 -08002198
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302199 if (mehci->async_irq) {
Manu Gautam1a1a9c62013-03-19 17:37:11 +05302200 /* Async IRQ is used only in absence of dedicated wakeup irq */
2201 if (!mehci->wakeup_irq)
2202 disable_irq_wake(mehci->async_irq);
Manu Gautam3fb60ca2013-02-13 18:33:33 +05302203 free_irq(mehci->async_irq, mehci);
2204 }
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002205 /*
2206 * If the update request is called after unregister, the request will
2207 * fail. Results are undefined if unregister is called in the middle of
2208 * update request.
2209 */
2210 mehci->bus_vote = false;
2211 cancel_work_sync(&mehci->bus_vote_w);
2212
Hemant Kumare6275972012-02-29 20:06:21 -08002213 if (mehci->bus_perf_client)
2214 msm_bus_scale_unregister_client(mehci->bus_perf_client);
2215
2216 ehci_hsic_msm_debugfs_cleanup();
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302217 device_init_wakeup(&pdev->dev, 0);
Ido Shayevitze50d9142013-02-17 20:43:26 +02002218
Hemant Kumar2309eaa2012-08-14 16:46:42 -07002219 destroy_workqueue(ehci_wq);
2220
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05302221 msm_hsic_config_gpios(mehci, 0);
Vamsi Krishnabef91832013-03-11 14:10:20 -07002222
2223 if (pdata && pdata->resume_gpio)
2224 gpio_free(pdata->resume_gpio);
2225
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302226 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02002227 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302228
2229 msm_hsic_init_clocks(mehci, 0);
Sudhir Sharma1673e302012-08-27 17:37:24 -07002230 wake_lock_destroy(&mehci->wlock);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302231 iounmap(hcd->regs);
2232 usb_put_hcd(hcd);
2233
2234 return 0;
2235}
2236
2237#ifdef CONFIG_PM_SLEEP
2238static int msm_hsic_pm_suspend(struct device *dev)
2239{
2240 struct usb_hcd *hcd = dev_get_drvdata(dev);
2241 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2242
2243 dev_dbg(dev, "ehci-msm-hsic PM suspend\n");
2244
Hemant Kumar45d211b2012-05-31 17:58:43 -07002245 dbg_log_event(NULL, "PM Suspend", 0);
2246
Pavankumar Kondeti1081f3e2013-04-25 13:51:24 +05302247 if (!atomic_read(&mehci->in_lpm)) {
2248 dev_info(dev, "abort suspend\n");
2249 dbg_log_event(NULL, "PM Suspend abort", 0);
2250 return -EBUSY;
2251 }
2252
Manu Gautam16562792013-03-13 07:04:11 +05302253 if (device_may_wakeup(dev) && !mehci->async_irq)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302254 enable_irq_wake(hcd->irq);
2255
Pavankumar Kondeti1081f3e2013-04-25 13:51:24 +05302256 return 0;
Jack Phamfe441ea2012-03-23 17:03:15 -07002257}
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302258
Jack Pham16b06f82012-08-14 20:03:59 -07002259static int msm_hsic_pm_suspend_noirq(struct device *dev)
2260{
2261 struct usb_hcd *hcd = dev_get_drvdata(dev);
2262 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2263
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05302264 if (atomic_read(&mehci->async_int)) {
Jack Pham16b06f82012-08-14 20:03:59 -07002265 dev_dbg(dev, "suspend_noirq: Aborting due to pending interrupt\n");
2266 return -EBUSY;
2267 }
2268
2269 return 0;
2270}
2271
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302272static int msm_hsic_pm_resume(struct device *dev)
2273{
2274 int ret;
2275 struct usb_hcd *hcd = dev_get_drvdata(dev);
2276 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2277
Jack Pham16b06f82012-08-14 20:03:59 -07002278 dev_dbg(dev, "ehci-msm-hsic PM resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002279 dbg_log_event(NULL, "PM Resume", 0);
2280
Manu Gautam16562792013-03-13 07:04:11 +05302281 if (device_may_wakeup(dev) && !mehci->async_irq)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302282 disable_irq_wake(hcd->irq);
2283
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05302284 /*
2285 * Keep HSIC in Low Power Mode if system is resumed
2286 * by any other wakeup source. HSIC is resumed later
2287 * when remote wakeup is received or interface driver
2288 * start I/O.
2289 */
Pavankumar Kondeti41d004c2012-11-09 10:54:00 +05302290 if (!atomic_read(&mehci->pm_usage_cnt) &&
Pavankumar Kondeti822fbe62013-01-28 17:04:39 +05302291 !atomic_read(&mehci->async_int) &&
Pavankumar Kondeti41d004c2012-11-09 10:54:00 +05302292 pm_runtime_suspended(dev))
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05302293 return 0;
2294
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302295 ret = msm_hsic_resume(mehci);
2296 if (ret)
2297 return ret;
2298
2299 /* Bring the device to full powered state upon system resume */
2300 pm_runtime_disable(dev);
2301 pm_runtime_set_active(dev);
2302 pm_runtime_enable(dev);
2303
2304 return 0;
2305}
2306#endif
2307
2308#ifdef CONFIG_PM_RUNTIME
2309static int msm_hsic_runtime_idle(struct device *dev)
2310{
2311 dev_dbg(dev, "EHCI runtime idle\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302312 return 0;
2313}
2314
2315static int msm_hsic_runtime_suspend(struct device *dev)
2316{
2317 struct usb_hcd *hcd = dev_get_drvdata(dev);
2318 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2319
2320 dev_dbg(dev, "EHCI runtime suspend\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002321
2322 dbg_log_event(NULL, "Run Time PM Suspend", 0);
2323
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302324 return msm_hsic_suspend(mehci);
2325}
2326
2327static int msm_hsic_runtime_resume(struct device *dev)
2328{
2329 struct usb_hcd *hcd = dev_get_drvdata(dev);
2330 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2331
2332 dev_dbg(dev, "EHCI runtime resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002333
2334 dbg_log_event(NULL, "Run Time PM Resume", 0);
2335
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302336 return msm_hsic_resume(mehci);
2337}
2338#endif
2339
2340#ifdef CONFIG_PM
2341static const struct dev_pm_ops msm_hsic_dev_pm_ops = {
2342 SET_SYSTEM_SLEEP_PM_OPS(msm_hsic_pm_suspend, msm_hsic_pm_resume)
Jack Pham16b06f82012-08-14 20:03:59 -07002343 .suspend_noirq = msm_hsic_pm_suspend_noirq,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302344 SET_RUNTIME_PM_OPS(msm_hsic_runtime_suspend, msm_hsic_runtime_resume,
2345 msm_hsic_runtime_idle)
2346};
2347#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002348static const struct of_device_id hsic_host_dt_match[] = {
2349 { .compatible = "qcom,hsic-host",
2350 },
2351 {}
2352};
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302353static struct platform_driver ehci_msm_hsic_driver = {
2354 .probe = ehci_hsic_msm_probe,
2355 .remove = __devexit_p(ehci_hsic_msm_remove),
2356 .driver = {
2357 .name = "msm_hsic_host",
2358#ifdef CONFIG_PM
2359 .pm = &msm_hsic_dev_pm_ops,
2360#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002361 .of_match_table = hsic_host_dt_match,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302362 },
2363};