blob: 57598c8eacb7c4bcaefb01439b4a10bc5279583e [file] [log] [blame]
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301/* ehci-msm-hsic.c - HSUSB Host Controller Driver Implementation
2 *
Manu Gautam5143b252012-01-05 19:25:23 -08003 * Copyright (c) 2011-2012, Code Aurora Forum. 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>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053052
53#define MSM_USB_BASE (hcd->regs)
Hemant Kumar105d07f2012-07-02 15:33:07 -070054#define USB_REG_START_OFFSET 0x90
55#define USB_REG_END_OFFSET 0x250
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053056
Hemant Kumar2309eaa2012-08-14 16:46:42 -070057static struct workqueue_struct *ehci_wq;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -070058struct ehci_timer {
59#define GPT_LD(p) ((p) & 0x00FFFFFF)
60 u32 gptimer0_ld;
61#define GPT_RUN BIT(31)
62#define GPT_RESET BIT(30)
63#define GPT_MODE BIT(24)
64#define GPT_CNT(p) ((p) & 0x00FFFFFF)
65 u32 gptimer0_ctrl;
66
67 u32 gptimer1_ld;
68 u32 gptimer1_ctrl;
69};
Hemant Kumar2309eaa2012-08-14 16:46:42 -070070
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053071struct msm_hsic_hcd {
72 struct ehci_hcd ehci;
Hemant Kumard343c012012-09-06 19:57:14 -070073 spinlock_t wakeup_lock;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053074 struct device *dev;
75 struct clk *ahb_clk;
Manu Gautam5143b252012-01-05 19:25:23 -080076 struct clk *core_clk;
77 struct clk *alt_core_clk;
78 struct clk *phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053079 struct clk *cal_clk;
80 struct regulator *hsic_vddcx;
Ofir Cohen4cc55372012-11-20 11:17:24 +020081 struct regulator *hsic_gdsc;
Sudhir Sharma1673e302012-08-27 17:37:24 -070082 bool async_int;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053083 atomic_t in_lpm;
Sudhir Sharma1673e302012-08-27 17:37:24 -070084 struct wake_lock wlock;
Vamsi Krishna34f01582011-12-14 19:54:42 -080085 int peripheral_status_irq;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -080086 int wakeup_irq;
Jack Phamfe441ea2012-03-23 17:03:15 -070087 bool wakeup_irq_enabled;
Sudhir Sharma1673e302012-08-27 17:37:24 -070088 atomic_t pm_usage_cnt;
Hemant Kumare6275972012-02-29 20:06:21 -080089 uint32_t bus_perf_client;
Hemant Kumar6fd65032012-05-23 13:02:24 -070090 uint32_t wakeup_int_cnt;
Amit Blayd6ea6102012-06-07 16:26:24 +030091 enum usb_vdd_type vdd_type;
Hemant Kumar2309eaa2012-08-14 16:46:42 -070092
93 struct work_struct bus_vote_w;
94 bool bus_vote;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -070095
96 /* gp timer */
97 struct ehci_timer __iomem *timer;
98 struct completion gpt0_completion;
99 struct completion rt_completion;
100 int resume_status;
101 int resume_again;
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +0530102
103 struct pm_qos_request pm_qos_req_dma;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530104};
105
Hemant Kumar105d07f2012-07-02 15:33:07 -0700106struct msm_hsic_hcd *__mehci;
107
Hemant Kumare6275972012-02-29 20:06:21 -0800108static bool debug_bus_voting_enabled = true;
Ofir Cohen4cc55372012-11-20 11:17:24 +0200109static u64 ehci_msm_hsic_dma_mask = DMA_BIT_MASK(32);
110
Manu Gautam863b74132012-11-21 14:30:04 +0530111static struct platform_driver ehci_msm_hsic_driver;
Hemant Kumar45d211b2012-05-31 17:58:43 -0700112
Hemant Kumar4d50a432012-08-15 09:06:35 -0700113static unsigned int enable_payload_log = 1;
114module_param(enable_payload_log, uint, S_IRUGO | S_IWUSR);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700115static unsigned int enable_dbg_log = 1;
116module_param(enable_dbg_log, uint, S_IRUGO | S_IWUSR);
117/*by default log ep0 and efs sync ep*/
118static unsigned int ep_addr_rxdbg_mask = 9;
119module_param(ep_addr_rxdbg_mask, uint, S_IRUGO | S_IWUSR);
120static unsigned int ep_addr_txdbg_mask = 9;
121module_param(ep_addr_txdbg_mask, uint, S_IRUGO | S_IWUSR);
122
123/* Maximum debug message length */
Hemant Kumar4d50a432012-08-15 09:06:35 -0700124#define DBG_MSG_LEN 128UL
Hemant Kumar45d211b2012-05-31 17:58:43 -0700125
126/* Maximum number of messages */
127#define DBG_MAX_MSG 256UL
128
129#define TIME_BUF_LEN 20
Hemant Kumar4d50a432012-08-15 09:06:35 -0700130#define HEX_DUMP_LEN 72
Hemant Kumar45d211b2012-05-31 17:58:43 -0700131
132enum event_type {
133 EVENT_UNDEF = -1,
134 URB_SUBMIT,
135 URB_COMPLETE,
136 EVENT_NONE,
137};
138
139#define EVENT_STR_LEN 5
140
Hemant Kumar45d211b2012-05-31 17:58:43 -0700141static enum event_type str_to_event(const char *name)
142{
143 if (!strncasecmp("S", name, EVENT_STR_LEN))
144 return URB_SUBMIT;
145 if (!strncasecmp("C", name, EVENT_STR_LEN))
146 return URB_COMPLETE;
147 if (!strncasecmp("", name, EVENT_STR_LEN))
148 return EVENT_NONE;
149
150 return EVENT_UNDEF;
151}
152
153/*log ep0 activity*/
154static struct {
155 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
156 unsigned idx; /* index */
157 rwlock_t lck; /* lock */
158} dbg_hsic_ctrl = {
159 .idx = 0,
160 .lck = __RW_LOCK_UNLOCKED(lck)
161};
162
163static struct {
164 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
165 unsigned idx; /* index */
166 rwlock_t lck; /* lock */
167} dbg_hsic_data = {
168 .idx = 0,
169 .lck = __RW_LOCK_UNLOCKED(lck)
170};
171
172/**
173 * dbg_inc: increments debug event index
174 * @idx: buffer index
175 */
176static void dbg_inc(unsigned *idx)
177{
178 *idx = (*idx + 1) & (DBG_MAX_MSG-1);
179}
180
181/*get_timestamp - returns time of day in us */
182static char *get_timestamp(char *tbuf)
183{
184 unsigned long long t;
185 unsigned long nanosec_rem;
186
187 t = cpu_clock(smp_processor_id());
188 nanosec_rem = do_div(t, 1000000000)/1000;
189 scnprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu] ", (unsigned long)t,
190 nanosec_rem);
191 return tbuf;
192}
193
194static int allow_dbg_log(int ep_addr)
195{
196 int dir, num;
197
198 dir = ep_addr & USB_DIR_IN ? USB_DIR_IN : USB_DIR_OUT;
199 num = ep_addr & ~USB_DIR_IN;
200 num = 1 << num;
201
202 if ((dir == USB_DIR_IN) && (num & ep_addr_rxdbg_mask))
203 return 1;
204 if ((dir == USB_DIR_OUT) && (num & ep_addr_txdbg_mask))
205 return 1;
206
207 return 0;
208}
209
Hemant Kumar4d50a432012-08-15 09:06:35 -0700210static char *get_hex_data(char *dbuf, struct urb *urb, int event, int status)
211{
212 int ep_addr = urb->ep->desc.bEndpointAddress;
213 char *ubuf = urb->transfer_buffer;
214 size_t len = event ? \
215 urb->actual_length : urb->transfer_buffer_length;
216
217 if (status == -EINPROGRESS)
218 status = 0;
219
220 /*Only dump ep in completions and epout submissions*/
221 if (len && !status &&
222 (((ep_addr & USB_DIR_IN) && event) ||
223 (!(ep_addr & USB_DIR_IN) && !event))) {
224 if (len >= 32)
225 len = 32;
226 hex_dump_to_buffer(ubuf, len, 32, 4, dbuf, HEX_DUMP_LEN, 0);
227 } else {
228 dbuf = "";
229 }
230
231 return dbuf;
232}
233
Hemant Kumar45d211b2012-05-31 17:58:43 -0700234static void dbg_log_event(struct urb *urb, char * event, unsigned extra)
235{
236 unsigned long flags;
237 int ep_addr;
238 char tbuf[TIME_BUF_LEN];
Hemant Kumar4d50a432012-08-15 09:06:35 -0700239 char dbuf[HEX_DUMP_LEN];
Hemant Kumar45d211b2012-05-31 17:58:43 -0700240
241 if (!enable_dbg_log)
242 return;
243
244 if (!urb) {
245 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
246 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx], DBG_MSG_LEN,
Hemant Kumar4d50a432012-08-15 09:06:35 -0700247 "%s: %s : %u", get_timestamp(tbuf), event, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700248 dbg_inc(&dbg_hsic_ctrl.idx);
249 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
250 return;
251 }
252
253 ep_addr = urb->ep->desc.bEndpointAddress;
254 if (!allow_dbg_log(ep_addr))
255 return;
256
257 if ((ep_addr & 0x0f) == 0x0) {
258 /*submit event*/
259 if (!str_to_event(event)) {
260 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
261 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
262 DBG_MSG_LEN, "%s: [%s : %p]:[%s] "
Hemant Kumar4d50a432012-08-15 09:06:35 -0700263 "%02x %02x %04x %04x %04x %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700264 get_timestamp(tbuf), event, urb,
265 (ep_addr & USB_DIR_IN) ? "in" : "out",
266 urb->setup_packet[0], urb->setup_packet[1],
267 (urb->setup_packet[3] << 8) |
268 urb->setup_packet[2],
269 (urb->setup_packet[5] << 8) |
270 urb->setup_packet[4],
271 (urb->setup_packet[7] << 8) |
272 urb->setup_packet[6],
Hemant Kumar4d50a432012-08-15 09:06:35 -0700273 urb->transfer_buffer_length, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700274
275 dbg_inc(&dbg_hsic_ctrl.idx);
276 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
277 } else {
278 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
279 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
Hemant Kumar4d50a432012-08-15 09:06:35 -0700280 DBG_MSG_LEN, "%s: [%s : %p]:[%s] %u %d",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700281 get_timestamp(tbuf), event, urb,
282 (ep_addr & USB_DIR_IN) ? "in" : "out",
283 urb->actual_length, extra);
284
285 dbg_inc(&dbg_hsic_ctrl.idx);
286 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
287 }
288 } else {
289 write_lock_irqsave(&dbg_hsic_data.lck, flags);
290 scnprintf(dbg_hsic_data.buf[dbg_hsic_data.idx], DBG_MSG_LEN,
Hemant Kumar4d50a432012-08-15 09:06:35 -0700291 "%s: [%s : %p]:ep%d[%s] %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700292 get_timestamp(tbuf), event, urb, ep_addr & 0x0f,
293 (ep_addr & USB_DIR_IN) ? "in" : "out",
294 str_to_event(event) ? urb->actual_length :
Hemant Kumar4d50a432012-08-15 09:06:35 -0700295 urb->transfer_buffer_length, extra,
296 enable_payload_log ? get_hex_data(dbuf, urb,
297 str_to_event(event), extra) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700298
299 dbg_inc(&dbg_hsic_data.idx);
300 write_unlock_irqrestore(&dbg_hsic_data.lck, flags);
301 }
302}
303
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530304static inline struct msm_hsic_hcd *hcd_to_hsic(struct usb_hcd *hcd)
305{
306 return (struct msm_hsic_hcd *) (hcd->hcd_priv);
307}
308
309static inline struct usb_hcd *hsic_to_hcd(struct msm_hsic_hcd *mehci)
310{
311 return container_of((void *) mehci, struct usb_hcd, hcd_priv);
312}
313
Hemant Kumar105d07f2012-07-02 15:33:07 -0700314static void dump_hsic_regs(struct usb_hcd *hcd)
315{
316 int i;
317 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
318
319 if (atomic_read(&mehci->in_lpm))
320 return;
321
322 for (i = USB_REG_START_OFFSET; i <= USB_REG_END_OFFSET; i += 0x10)
323 pr_info("%p: %08x\t%08x\t%08x\t%08x\n", hcd->regs + i,
324 readl_relaxed(hcd->regs + i),
325 readl_relaxed(hcd->regs + i + 4),
326 readl_relaxed(hcd->regs + i + 8),
327 readl_relaxed(hcd->regs + i + 0xc));
328}
329
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530330#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
331
Amit Blayd6ea6102012-06-07 16:26:24 +0300332#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Hemant Kumar3b743cd2012-10-17 13:48:10 -0700333#define USB_PHY_VDD_DIG_VOL_MIN 945000 /* uV */
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700334#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530335
Lena Salman8c8ba382012-02-14 15:59:31 +0200336#define HSIC_DBG1_REG 0x38
337
Amit Blayd6ea6102012-06-07 16:26:24 +0300338static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
339 { /* VDD_CX CORNER Voting */
340 [VDD_NONE] = RPM_VREG_CORNER_NONE,
341 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
342 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
343 },
344 { /* VDD_CX Voltage Voting */
345 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
346 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
347 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
348 },
349};
350
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530351static int msm_hsic_init_vddcx(struct msm_hsic_hcd *mehci, int init)
352{
353 int ret = 0;
Amit Blayd6ea6102012-06-07 16:26:24 +0300354 int none_vol, min_vol, max_vol;
355
356 if (!mehci->hsic_vddcx) {
357 mehci->vdd_type = VDDCX_CORNER;
358 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
359 "hsic_vdd_dig");
360 if (IS_ERR(mehci->hsic_vddcx)) {
361 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
362 "HSIC_VDDCX");
363 if (IS_ERR(mehci->hsic_vddcx)) {
364 dev_err(mehci->dev, "unable to get hsic vddcx\n");
365 return PTR_ERR(mehci->hsic_vddcx);
366 }
367 mehci->vdd_type = VDDCX;
368 }
369 }
370
371 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
372 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
373 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530374
375 if (!init)
376 goto disable_reg;
377
Amit Blayd6ea6102012-06-07 16:26:24 +0300378 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530379 if (ret) {
380 dev_err(mehci->dev, "unable to set the voltage"
381 "for hsic vddcx\n");
Mayank Rana189ac052012-03-24 04:35:02 +0530382 return ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530383 }
384
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530385 ret = regulator_enable(mehci->hsic_vddcx);
386 if (ret) {
387 dev_err(mehci->dev, "unable to enable hsic vddcx\n");
388 goto reg_enable_err;
389 }
390
391 return 0;
392
393disable_reg:
394 regulator_disable(mehci->hsic_vddcx);
395reg_enable_err:
Amit Blayd6ea6102012-06-07 16:26:24 +0300396 regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
397
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530398 return ret;
399
400}
401
Ofir Cohen4cc55372012-11-20 11:17:24 +0200402/* Global Distributed Switch Controller (GDSC) init */
403static int msm_hsic_init_gdsc(struct msm_hsic_hcd *mehci, int init)
404{
405 int ret = 0;
406
407 if (IS_ERR(mehci->hsic_gdsc))
408 return 0;
409
410 if (!mehci->hsic_gdsc) {
411 mehci->hsic_gdsc = devm_regulator_get(mehci->dev,
412 "HSIC_GDSC");
413 if (IS_ERR(mehci->hsic_gdsc))
414 return 0;
415 }
416
417 if (init) {
418 ret = regulator_enable(mehci->hsic_gdsc);
419 if (ret) {
420 dev_err(mehci->dev, "unable to enable hsic gdsc\n");
421 return ret;
422 }
423 } else {
424 regulator_disable(mehci->hsic_gdsc);
425 }
426
427 return 0;
428
429}
430
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700431static int ulpi_read(struct msm_hsic_hcd *mehci, u32 reg)
432{
433 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700434 int cnt = 0;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700435
436 /* initiate read operation */
437 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
438 USB_ULPI_VIEWPORT);
439
440 /* wait for completion */
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700441 while (cnt < ULPI_IO_TIMEOUT_USEC) {
442 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
443 break;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700444 udelay(1);
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700445 cnt++;
446 }
447
448 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
449 dev_err(mehci->dev, "ulpi_read: timeout ULPI_VIEWPORT: %08x\n",
450 readl_relaxed(USB_ULPI_VIEWPORT));
451 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
452 readl_relaxed(USB_PORTSC),
453 readl_relaxed(USB_USBCMD),
454 readl_relaxed(USB_FRINDEX));
455
456 /*frame counter increments afte 125us*/
457 udelay(130);
458 dev_err(mehci->dev, "ulpi_read: FRINDEX: %08x\n",
459 readl_relaxed(USB_FRINDEX));
460 return -ETIMEDOUT;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700461 }
462
463 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
464}
465
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530466static int ulpi_write(struct msm_hsic_hcd *mehci, u32 val, u32 reg)
467{
468 struct usb_hcd *hcd = hsic_to_hcd(mehci);
469 int cnt = 0;
470
471 /* initiate write operation */
472 writel_relaxed(ULPI_RUN | ULPI_WRITE |
473 ULPI_ADDR(reg) | ULPI_DATA(val),
474 USB_ULPI_VIEWPORT);
475
476 /* wait for completion */
477 while (cnt < ULPI_IO_TIMEOUT_USEC) {
478 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
479 break;
480 udelay(1);
481 cnt++;
482 }
483
484 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Hemant Kumar8fdc3982012-08-21 12:22:27 -0700485 dev_err(mehci->dev, "ulpi_write: timeout ULPI_VIEWPORT: %08x\n",
486 readl_relaxed(USB_ULPI_VIEWPORT));
487 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
488 readl_relaxed(USB_PORTSC),
489 readl_relaxed(USB_USBCMD),
490 readl_relaxed(USB_FRINDEX));
491
492 /*frame counter increments afte 125us*/
493 udelay(130);
494 dev_err(mehci->dev, "ulpi_write: FRINDEX: %08x\n",
495 readl_relaxed(USB_FRINDEX));
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530496 return -ETIMEDOUT;
497 }
498
499 return 0;
500}
501
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700502#define HSIC_DBG1 0X38
503#define ULPI_MANUAL_ENABLE BIT(4)
504#define ULPI_LINESTATE_DATA BIT(5)
505#define ULPI_LINESTATE_STROBE BIT(6)
506static void ehci_msm_enable_ulpi_control(struct usb_hcd *hcd, u32 linestate)
507{
508 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
509 int val;
510
511 switch (linestate) {
512 case PORT_RESET:
513 val = ulpi_read(mehci, HSIC_DBG1);
514 val |= ULPI_MANUAL_ENABLE;
515 val &= ~(ULPI_LINESTATE_DATA | ULPI_LINESTATE_STROBE);
516 ulpi_write(mehci, val, HSIC_DBG1);
517 break;
518 default:
519 pr_info("%s: Unknown linestate:%0x\n", __func__, linestate);
520 }
521}
522
523static void ehci_msm_disable_ulpi_control(struct usb_hcd *hcd)
524{
525 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
526 int val;
527
528 val = ulpi_read(mehci, HSIC_DBG1);
529 val &= ~ULPI_MANUAL_ENABLE;
530 ulpi_write(mehci, val, HSIC_DBG1);
531}
532
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530533static int msm_hsic_config_gpios(struct msm_hsic_hcd *mehci, int gpio_en)
534{
535 int rc = 0;
536 struct msm_hsic_host_platform_data *pdata;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800537 static int gpio_status;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530538
539 pdata = mehci->dev->platform_data;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800540
Lena Salman8c8ba382012-02-14 15:59:31 +0200541 if (!pdata || !pdata->strobe || !pdata->data)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530542 return rc;
543
Vamsi Krishna34f01582011-12-14 19:54:42 -0800544 if (gpio_status == gpio_en)
545 return 0;
546
547 gpio_status = gpio_en;
548
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530549 if (!gpio_en)
550 goto free_gpio;
551
552 rc = gpio_request(pdata->strobe, "HSIC_STROBE_GPIO");
553 if (rc < 0) {
554 dev_err(mehci->dev, "gpio request failed for HSIC STROBE\n");
555 return rc;
556 }
557
558 rc = gpio_request(pdata->data, "HSIC_DATA_GPIO");
559 if (rc < 0) {
560 dev_err(mehci->dev, "gpio request failed for HSIC DATA\n");
561 goto free_strobe;
Hemant Kumar6fd65032012-05-23 13:02:24 -0700562 }
563
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530564 return 0;
565
566free_gpio:
567 gpio_free(pdata->data);
568free_strobe:
569 gpio_free(pdata->strobe);
570
571 return rc;
572}
573
Vamsi Krishna64b48612012-06-14 16:08:11 -0700574static void msm_hsic_clk_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530575{
576 int ret;
577
Manu Gautam5143b252012-01-05 19:25:23 -0800578 ret = clk_reset(mehci->core_clk, CLK_RESET_ASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530579 if (ret) {
Vamsi Krishna64b48612012-06-14 16:08:11 -0700580 dev_err(mehci->dev, "hsic clk assert failed:%d\n", ret);
581 return;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530582 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700583 clk_disable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530584
Manu Gautam5143b252012-01-05 19:25:23 -0800585 ret = clk_reset(mehci->core_clk, CLK_RESET_DEASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530586 if (ret)
Vamsi Krishna64b48612012-06-14 16:08:11 -0700587 dev_err(mehci->dev, "hsic clk deassert failed:%d\n", ret);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530588
Vamsi Krishna64b48612012-06-14 16:08:11 -0700589 usleep_range(10000, 12000);
590
591 clk_enable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530592}
593
Vamsi Krishna64b48612012-06-14 16:08:11 -0700594#define HSIC_STROBE_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C0)
595#define HSIC_DATA_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C4)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530596#define HSIC_CAL_PAD_CTL (MSM_TLMM_BASE+0x20C8)
597#define HSIC_LV_MODE 0x04
598#define HSIC_PAD_CALIBRATION 0xA8
599#define HSIC_GPIO_PAD_VAL 0x0A0AAA10
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530600#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
Ofir Cohen5dddb152012-11-14 11:18:50 +0200601
602static void msm_hsic_phy_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530603{
604 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530605
Vamsi Krishna64b48612012-06-14 16:08:11 -0700606 msm_hsic_clk_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530607
Vamsi Krishna64b48612012-06-14 16:08:11 -0700608 /* select ulpi phy */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530609 writel_relaxed(0x80000000, USB_PORTSC);
Vamsi Krishna64b48612012-06-14 16:08:11 -0700610 mb();
Ofir Cohen5dddb152012-11-14 11:18:50 +0200611}
612
613static int msm_hsic_start(struct msm_hsic_hcd *mehci)
614{
615 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
616 int ret;
Manu Gautam863b74132012-11-21 14:30:04 +0530617 void __iomem *reg;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530618
Lena Salman8c8ba382012-02-14 15:59:31 +0200619 /* HSIC init sequence when HSIC signals (Strobe/Data) are
620 routed via GPIOs */
621 if (pdata && pdata->strobe && pdata->data) {
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530622
Manu Gautam863b74132012-11-21 14:30:04 +0530623 if (!pdata->ignore_cal_pad_config) {
624 /* Enable LV_MODE in HSIC_CAL_PAD_CTL register */
625 writel_relaxed(HSIC_LV_MODE, HSIC_CAL_PAD_CTL);
626 mb();
627 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700628
Lena Salman8c8ba382012-02-14 15:59:31 +0200629 /*set periodic calibration interval to ~2.048sec in
630 HSIC_IO_CAL_REG */
631 ulpi_write(mehci, 0xFF, 0x33);
632
633 /* Enable periodic IO calibration in HSIC_CFG register */
634 ulpi_write(mehci, HSIC_PAD_CALIBRATION, 0x30);
635
Vamsi Krishna64b48612012-06-14 16:08:11 -0700636 /* Configure GPIO pins for HSIC functionality mode */
Lena Salman8c8ba382012-02-14 15:59:31 +0200637 ret = msm_hsic_config_gpios(mehci, 1);
638 if (ret) {
639 dev_err(mehci->dev, " gpio configuarion failed\n");
640 return ret;
641 }
Manu Gautam863b74132012-11-21 14:30:04 +0530642 if (pdata->strobe_pad_offset) {
643 /* Set CORE_CTL_EN in STROBE GPIO PAD_CTL register */
644 reg = MSM_TLMM_BASE + pdata->strobe_pad_offset;
645 writel_relaxed(readl_relaxed(reg) | 0x2000000, reg);
646 } else {
647 /* Set LV_MODE=0x1 and DCC=0x2 in STROBE GPIO PAD_CTL */
648 reg = HSIC_STROBE_GPIO_PAD_CTL;
649 writel_relaxed(HSIC_GPIO_PAD_VAL, reg);
650 }
651
652 if (pdata->data_pad_offset) {
653 /* Set CORE_CTL_EN in HSIC_DATA GPIO PAD_CTL register */
654 reg = MSM_TLMM_BASE + pdata->data_pad_offset;
655 writel_relaxed(readl_relaxed(reg) | 0x2000000, reg);
656 } else {
657 /* Set LV_MODE=0x1 and DCC=0x2 in STROBE GPIO PAD_CTL */
658 reg = HSIC_DATA_GPIO_PAD_CTL;
659 writel_relaxed(HSIC_GPIO_PAD_VAL, reg);
660 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700661
662 mb();
663
Lena Salman8c8ba382012-02-14 15:59:31 +0200664 /* Enable HSIC mode in HSIC_CFG register */
665 ulpi_write(mehci, 0x01, 0x31);
666 } else {
667 /* HSIC init sequence when HSIC signals (Strobe/Data) are routed
668 via dedicated I/O */
669
670 /* programmable length of connect signaling (33.2ns) */
671 ret = ulpi_write(mehci, 3, HSIC_DBG1_REG);
672 if (ret) {
673 pr_err("%s: Unable to program length of connect "
674 "signaling\n", __func__);
675 }
676
677 /*set periodic calibration interval to ~2.048sec in
678 HSIC_IO_CAL_REG */
679 ulpi_write(mehci, 0xFF, 0x33);
680
681 /* Enable HSIC mode in HSIC_CFG register */
682 ulpi_write(mehci, 0xA9, 0x30);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530683 }
684
Hemant Kumar6fd65032012-05-23 13:02:24 -0700685 /*disable auto resume*/
686 ulpi_write(mehci, ULPI_IFC_CTRL_AUTORESUME, ULPI_CLR(ULPI_IFC_CTRL));
687
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530688 return 0;
689}
690
691#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
692#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
693
694#ifdef CONFIG_PM_SLEEP
Ofir Cohen5dddb152012-11-14 11:18:50 +0200695static int msm_hsic_reset(struct msm_hsic_hcd *mehci)
696{
697 /* reset HSIC phy */
698 msm_hsic_phy_reset(mehci);
699
700 /* HSIC init procedure (caliberation) */
701 return msm_hsic_start(mehci);
702}
703
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530704static int msm_hsic_suspend(struct msm_hsic_hcd *mehci)
705{
706 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530707 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530708 u32 val;
Amit Blayd6ea6102012-06-07 16:26:24 +0300709 int none_vol, max_vol;
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700710 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530711
712 if (atomic_read(&mehci->in_lpm)) {
713 dev_dbg(mehci->dev, "%s called in lpm\n", __func__);
714 return 0;
715 }
716
717 disable_irq(hcd->irq);
Jack Phambe05fbb2012-05-16 10:56:26 -0700718
Sudhir Sharma1673e302012-08-27 17:37:24 -0700719 /* make sure we don't race against a remote wakeup */
720 if (test_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags) ||
Jack Phambe05fbb2012-05-16 10:56:26 -0700721 readl_relaxed(USB_PORTSC) & PORT_RESUME) {
Sudhir Sharma1673e302012-08-27 17:37:24 -0700722 dev_dbg(mehci->dev, "wakeup pending, aborting suspend\n");
Jack Phambe05fbb2012-05-16 10:56:26 -0700723 enable_irq(hcd->irq);
724 return -EBUSY;
725 }
726
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530727 /*
728 * PHY may take some time or even fail to enter into low power
729 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
730 * in failure case.
731 */
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700732 val = readl_relaxed(USB_PORTSC);
733 val &= ~PORT_RWC_BITS;
734 val |= PORTSC_PHCD;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530735 writel_relaxed(val, USB_PORTSC);
736 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
737 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
738 break;
739 udelay(1);
740 cnt++;
741 }
742
743 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
744 dev_err(mehci->dev, "Unable to suspend PHY\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530745 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530746 msm_hsic_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530747 }
748
749 /*
750 * PHY has capability to generate interrupt asynchronously in low
751 * power mode (LPM). This interrupt is level triggered. So USB IRQ
752 * line must be disabled till async interrupt enable bit is cleared
753 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
754 * block data communication from PHY.
755 */
756 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
757 ULPI_STP_CTRL, USB_USBCMD);
758
759 /*
760 * Ensure that hardware is put in low power mode before
761 * clocks are turned OFF and VDD is allowed to minimize.
762 */
763 mb();
764
Manu Gautam28b1bac2012-01-30 16:43:06 +0530765 clk_disable_unprepare(mehci->core_clk);
766 clk_disable_unprepare(mehci->phy_clk);
767 clk_disable_unprepare(mehci->cal_clk);
768 clk_disable_unprepare(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530769
Amit Blayd6ea6102012-06-07 16:26:24 +0300770 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
771 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
772
773 ret = regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700774 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300775 dev_err(mehci->dev, "unable to set vddcx voltage for VDD MIN\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700776
Hemant Kumare6275972012-02-29 20:06:21 -0800777 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700778 mehci->bus_vote = false;
779 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800780 }
781
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530782 atomic_set(&mehci->in_lpm, 1);
783 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700784
785 mehci->wakeup_irq_enabled = 1;
786 enable_irq_wake(mehci->wakeup_irq);
787 enable_irq(mehci->wakeup_irq);
788
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700789 if (pdata && pdata->standalone_latency)
790 pm_qos_update_request(&mehci->pm_qos_req_dma,
791 PM_QOS_DEFAULT_VALUE);
792
Sudhir Sharma1673e302012-08-27 17:37:24 -0700793 wake_unlock(&mehci->wlock);
794
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530795 dev_info(mehci->dev, "HSIC-USB in low power mode\n");
796
797 return 0;
798}
799
800static int msm_hsic_resume(struct msm_hsic_hcd *mehci)
801{
802 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530803 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530804 unsigned temp;
Amit Blayd6ea6102012-06-07 16:26:24 +0300805 int min_vol, max_vol;
Hemant Kumard343c012012-09-06 19:57:14 -0700806 unsigned long flags;
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700807 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530808
809 if (!atomic_read(&mehci->in_lpm)) {
810 dev_dbg(mehci->dev, "%s called in !in_lpm\n", __func__);
811 return 0;
812 }
813
Hemant Kumarbc8bdf62012-10-17 12:29:51 -0700814 if (pdata && pdata->standalone_latency)
815 pm_qos_update_request(&mehci->pm_qos_req_dma,
816 pdata->standalone_latency + 1);
817
Hemant Kumard343c012012-09-06 19:57:14 -0700818 spin_lock_irqsave(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700819 if (mehci->wakeup_irq_enabled) {
820 disable_irq_wake(mehci->wakeup_irq);
821 disable_irq_nosync(mehci->wakeup_irq);
822 mehci->wakeup_irq_enabled = 0;
823 }
Hemant Kumard343c012012-09-06 19:57:14 -0700824 spin_unlock_irqrestore(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700825
Sudhir Sharma1673e302012-08-27 17:37:24 -0700826 wake_lock(&mehci->wlock);
827
Hemant Kumare6275972012-02-29 20:06:21 -0800828 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700829 mehci->bus_vote = true;
830 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800831 }
832
Amit Blayd6ea6102012-06-07 16:26:24 +0300833 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
834 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
835
836 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700837 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300838 dev_err(mehci->dev, "unable to set nominal vddcx voltage (no VDD MIN)\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700839
Manu Gautam28b1bac2012-01-30 16:43:06 +0530840 clk_prepare_enable(mehci->core_clk);
841 clk_prepare_enable(mehci->phy_clk);
842 clk_prepare_enable(mehci->cal_clk);
843 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530844
845 temp = readl_relaxed(USB_USBCMD);
846 temp &= ~ASYNC_INTR_CTRL;
847 temp &= ~ULPI_STP_CTRL;
848 writel_relaxed(temp, USB_USBCMD);
849
850 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
851 goto skip_phy_resume;
852
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700853 temp = readl_relaxed(USB_PORTSC);
854 temp &= ~(PORT_RWC_BITS | PORTSC_PHCD);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530855 writel_relaxed(temp, USB_PORTSC);
856 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
857 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD) &&
858 (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE))
859 break;
860 udelay(1);
861 cnt++;
862 }
863
864 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
865 /*
866 * This is a fatal error. Reset the link and
867 * PHY to make hsic working.
868 */
869 dev_err(mehci->dev, "Unable to resume USB. Reset the hsic\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530870 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530871 msm_hsic_reset(mehci);
872 }
873
874skip_phy_resume:
875
Hemant Kumar6fd65032012-05-23 13:02:24 -0700876 usb_hcd_resume_root_hub(hcd);
877
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530878 atomic_set(&mehci->in_lpm, 0);
879
880 if (mehci->async_int) {
881 mehci->async_int = false;
882 pm_runtime_put_noidle(mehci->dev);
Jack Phamdd5ad792012-07-26 10:31:03 -0700883 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700884 }
885
Sudhir Sharma1673e302012-08-27 17:37:24 -0700886 if (atomic_read(&mehci->pm_usage_cnt)) {
887 atomic_set(&mehci->pm_usage_cnt, 0);
888 pm_runtime_put_noidle(mehci->dev);
889 }
Jack Phamdd5ad792012-07-26 10:31:03 -0700890
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530891 dev_info(mehci->dev, "HSIC-USB exited from low power mode\n");
892
893 return 0;
894}
895#endif
896
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700897static void ehci_hsic_bus_vote_w(struct work_struct *w)
898{
899 struct msm_hsic_hcd *mehci =
900 container_of(w, struct msm_hsic_hcd, bus_vote_w);
901 int ret;
902
903 ret = msm_bus_scale_client_update_request(mehci->bus_perf_client,
904 mehci->bus_vote);
905 if (ret)
906 dev_err(mehci->dev, "%s: Failed to vote for bus bandwidth %d\n",
907 __func__, ret);
908}
909
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700910#define STS_GPTIMER0_INTERRUPT BIT(24)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530911static irqreturn_t msm_hsic_irq(struct usb_hcd *hcd)
912{
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700913 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530914 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700915 u32 status;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530916
917 if (atomic_read(&mehci->in_lpm)) {
918 disable_irq_nosync(hcd->irq);
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700919 dev_dbg(mehci->dev, "phy async intr\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530920 mehci->async_int = true;
921 pm_runtime_get(mehci->dev);
922 return IRQ_HANDLED;
923 }
924
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700925 status = ehci_readl(ehci, &ehci->regs->status);
926
927 if (status & STS_GPTIMER0_INTERRUPT) {
928 int timeleft;
929
930 dbg_log_event(NULL, "FPR: gpt0_isr", 0);
931
932 timeleft = GPT_CNT(ehci_readl(ehci,
933 &mehci->timer->gptimer1_ctrl));
934 if (timeleft) {
935 ehci_writel(ehci, ehci_readl(ehci,
936 &ehci->regs->command) | CMD_RUN,
937 &ehci->regs->command);
938 } else
939 mehci->resume_again = 1;
940
941 dbg_log_event(NULL, "FPR: timeleft", timeleft);
942
943 complete(&mehci->gpt0_completion);
944 ehci_writel(ehci, STS_GPTIMER0_INTERRUPT, &ehci->regs->status);
945 }
946
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530947 return ehci_irq(hcd);
948}
949
950static int ehci_hsic_reset(struct usb_hcd *hcd)
951{
952 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700953 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530954 int retval;
955
Vamsi Krishnafce1bd22012-08-17 17:12:26 -0700956 mehci->timer = USB_HS_GPTIMER_BASE;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530957 ehci->caps = USB_CAPLENGTH;
958 ehci->regs = USB_CAPLENGTH +
959 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
960 dbg_hcs_params(ehci, "reset");
961 dbg_hcc_params(ehci, "reset");
962
963 /* cache the data to minimize the chip reads*/
964 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
965
966 hcd->has_tt = 1;
967 ehci->sbrn = HCD_USB2;
968
969 retval = ehci_halt(ehci);
970 if (retval)
971 return retval;
972
973 /* data structure init */
974 retval = ehci_init(hcd);
975 if (retval)
976 return retval;
977
978 retval = ehci_reset(ehci);
979 if (retval)
980 return retval;
981
982 /* bursts of unspecified length. */
983 writel_relaxed(0, USB_AHBBURST);
984 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530985 writel_relaxed(0x08, USB_AHBMODE);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530986 /* Disable streaming mode and select host mode */
987 writel_relaxed(0x13, USB_USBMODE);
988
989 ehci_port_power(ehci, 1);
990 return 0;
991}
992
Jack Phamc0d41732012-09-28 17:45:38 -0700993#ifdef CONFIG_PM
994
Hemant Kumar45d211b2012-05-31 17:58:43 -0700995static int ehci_hsic_bus_suspend(struct usb_hcd *hcd)
996{
Pavankumar Kondetia723f742012-09-14 14:02:36 +0530997 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
998
999 if (!(readl_relaxed(USB_PORTSC) & PORT_PE)) {
1000 dbg_log_event(NULL, "RH suspend attempt failed", 0);
1001 dev_dbg(mehci->dev, "%s:port is not enabled skip suspend\n",
1002 __func__);
1003 return -EAGAIN;
1004 }
1005
Hemant Kumar45d211b2012-05-31 17:58:43 -07001006 dbg_log_event(NULL, "Suspend RH", 0);
1007 return ehci_bus_suspend(hcd);
1008}
1009
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001010#define RESUME_RETRY_LIMIT 3
1011#define RESUME_SIGNAL_TIME_MS (21 * 999)
1012#define RESUME_SIGNAL_TIME_SOF_MS (23 * 999)
1013static int msm_hsic_resume_thread(void *data)
1014{
1015 struct msm_hsic_hcd *mehci = data;
1016 struct usb_hcd *hcd = hsic_to_hcd(mehci);
1017 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1018 u32 temp;
1019 unsigned long resume_needed = 0;
1020 int retry_cnt = 0;
1021 int tight_resume = 0;
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301022 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001023
1024 dbg_log_event(NULL, "Resume RH", 0);
1025
1026 /* keep delay between bus states */
1027 if (time_before(jiffies, ehci->next_statechange))
1028 usleep_range(5000, 5000);
1029
1030 spin_lock_irq(&ehci->lock);
1031 if (!HCD_HW_ACCESSIBLE(hcd)) {
1032 spin_unlock_irq(&ehci->lock);
1033 mehci->resume_status = -ESHUTDOWN;
1034 complete(&mehci->rt_completion);
1035 return 0;
1036 }
1037
1038 if (unlikely(ehci->debug)) {
1039 if (!dbgp_reset_prep())
1040 ehci->debug = NULL;
1041 else
1042 dbgp_external_startup();
1043 }
1044
1045 /* at least some APM implementations will try to deliver
1046 * IRQs right away, so delay them until we're ready.
1047 */
1048 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
1049
1050 /* re-init operational registers */
1051 ehci_writel(ehci, 0, &ehci->regs->segment);
1052 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
1053 ehci_writel(ehci, (u32) ehci->async->qh_dma, &ehci->regs->async_next);
1054
1055 /*CMD_RUN will be set after, PORT_RESUME gets cleared*/
1056 if (ehci->resume_sof_bug)
1057 ehci->command &= ~CMD_RUN;
1058
1059 /* restore CMD_RUN, framelist size, and irq threshold */
1060 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1061
1062 /* manually resume the ports we suspended during bus_suspend() */
1063resume_again:
1064 if (retry_cnt >= RESUME_RETRY_LIMIT) {
1065 pr_info("retry count(%d) reached max, resume in tight loop\n",
1066 retry_cnt);
1067 tight_resume = 1;
1068 }
1069
1070
1071 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1072 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1073 if (test_bit(0, &ehci->bus_suspended) && (temp & PORT_SUSPEND)) {
1074 temp |= PORT_RESUME;
1075 set_bit(0, &resume_needed);
1076 }
1077 dbg_log_event(NULL, "FPR: Set", temp);
1078 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1079
1080 /* HSIC controller has a h/w bug due to which it can try to send SOFs
1081 * (start of frames) during port resume resulting in phy lockup. HSIC hw
1082 * controller in MSM clears FPR bit after driving the resume signal for
1083 * 20ms. Workaround is to stop SOFs before driving resume and then start
1084 * sending SOFs immediately. Need to send SOFs within 3ms of resume
1085 * completion otherwise peripheral may enter undefined state. As
1086 * usleep_range does not gurantee exact sleep time, GPTimer is used to
1087 * to time the resume sequence. If driver exceeds allowable time SOFs,
1088 * repeat the resume process.
1089 */
1090 if (ehci->resume_sof_bug && resume_needed) {
1091 if (!tight_resume) {
1092 mehci->resume_again = 0;
1093 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_MS),
1094 &mehci->timer->gptimer0_ld);
1095 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1096 &mehci->timer->gptimer0_ctrl);
1097 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1098 &ehci->regs->intr_enable);
1099
1100 ehci_writel(ehci, GPT_LD(RESUME_SIGNAL_TIME_SOF_MS),
1101 &mehci->timer->gptimer1_ld);
1102 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1103 &mehci->timer->gptimer1_ctrl);
1104
1105 spin_unlock_irq(&ehci->lock);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301106 if (pdata && pdata->swfi_latency)
1107 pm_qos_update_request(&mehci->pm_qos_req_dma,
1108 pdata->swfi_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001109 wait_for_completion(&mehci->gpt0_completion);
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001110 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301111 pm_qos_update_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001112 pdata->standalone_latency + 1);
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001113 spin_lock_irq(&ehci->lock);
1114 } else {
1115 dbg_log_event(NULL, "FPR: Tightloop", 0);
1116 /* do the resume in a tight loop */
1117 handshake(ehci, &ehci->regs->port_status[0],
1118 PORT_RESUME, 0, 22 * 1000);
1119 ehci_writel(ehci, ehci_readl(ehci,
1120 &ehci->regs->command) | CMD_RUN,
1121 &ehci->regs->command);
1122 }
1123
1124 if (mehci->resume_again) {
1125 int temp;
1126
1127 dbg_log_event(NULL, "FPR: Re-Resume", retry_cnt);
1128 pr_info("FPR: retry count: %d\n", retry_cnt);
1129 spin_unlock_irq(&ehci->lock);
1130 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
1131 temp &= ~PORT_RWC_BITS;
1132 temp |= PORT_SUSPEND;
1133 ehci_writel(ehci, temp, &ehci->regs->port_status[0]);
1134 /* Keep the bus idle for 5ms so that peripheral
1135 * can detect and initiate suspend
1136 */
1137 usleep_range(5000, 5000);
1138 dbg_log_event(NULL,
1139 "FPR: RResume",
1140 ehci_readl(ehci, &ehci->regs->port_status[0]));
1141 spin_lock_irq(&ehci->lock);
1142 mehci->resume_again = 0;
1143 retry_cnt++;
1144 goto resume_again;
1145 }
1146 }
1147
1148 dbg_log_event(NULL, "FPR: RT-Done", 0);
1149 mehci->resume_status = 1;
1150 spin_unlock_irq(&ehci->lock);
1151
1152 complete(&mehci->rt_completion);
1153
1154 return 0;
1155}
1156
Hemant Kumar45d211b2012-05-31 17:58:43 -07001157static int ehci_hsic_bus_resume(struct usb_hcd *hcd)
1158{
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001159 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1160 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1161 u32 temp;
1162 struct task_struct *resume_thread = NULL;
1163
1164 mehci->resume_status = 0;
1165 resume_thread = kthread_run(msm_hsic_resume_thread,
1166 mehci, "hsic_resume_thread");
1167 if (IS_ERR(resume_thread)) {
1168 pr_err("Error creating resume thread:%lu\n",
1169 PTR_ERR(resume_thread));
1170 return PTR_ERR(resume_thread);
1171 }
1172
1173 wait_for_completion(&mehci->rt_completion);
1174
1175 if (mehci->resume_status < 0)
1176 return mehci->resume_status;
1177
1178 dbg_log_event(NULL, "FPR: Wokeup", 0);
1179 spin_lock_irq(&ehci->lock);
1180 (void) ehci_readl(ehci, &ehci->regs->command);
1181
1182 temp = 0;
1183 if (ehci->async->qh_next.qh)
1184 temp |= CMD_ASE;
1185 if (ehci->periodic_sched)
1186 temp |= CMD_PSE;
1187 if (temp) {
1188 ehci->command |= temp;
1189 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1190 }
1191
1192 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
1193 hcd->state = HC_STATE_RUNNING;
1194 ehci->rh_state = EHCI_RH_RUNNING;
1195
1196 /* Now we can safely re-enable irqs */
1197 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1198
1199 spin_unlock_irq(&ehci->lock);
1200
1201 return 0;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001202}
1203
Jack Phamc0d41732012-09-28 17:45:38 -07001204#else
1205
1206#define ehci_hsic_bus_suspend NULL
1207#define ehci_hsic_bus_resume NULL
1208
1209#endif /* CONFIG_PM */
1210
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001211static void ehci_msm_set_autosuspend_delay(struct usb_device *dev)
1212{
1213 if (!dev->parent) /*for root hub no delay*/
1214 pm_runtime_set_autosuspend_delay(&dev->dev, 0);
1215 else
1216 pm_runtime_set_autosuspend_delay(&dev->dev, 200);
1217}
1218
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301219static struct hc_driver msm_hsic_driver = {
1220 .description = hcd_name,
1221 .product_desc = "Qualcomm EHCI Host Controller using HSIC",
1222 .hcd_priv_size = sizeof(struct msm_hsic_hcd),
1223
1224 /*
1225 * generic hardware linkage
1226 */
1227 .irq = msm_hsic_irq,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001228 .flags = HCD_USB2 | HCD_MEMORY | HCD_OLD_ENUM,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301229
1230 .reset = ehci_hsic_reset,
1231 .start = ehci_run,
1232
1233 .stop = ehci_stop,
1234 .shutdown = ehci_shutdown,
1235
1236 /*
1237 * managing i/o requests and associated device resources
1238 */
Hemant Kumar4d50a432012-08-15 09:06:35 -07001239 .urb_enqueue = ehci_urb_enqueue,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301240 .urb_dequeue = ehci_urb_dequeue,
1241 .endpoint_disable = ehci_endpoint_disable,
1242 .endpoint_reset = ehci_endpoint_reset,
1243 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1244
1245 /*
1246 * scheduling support
1247 */
1248 .get_frame_number = ehci_get_frame,
1249
1250 /*
1251 * root hub support
1252 */
1253 .hub_status_data = ehci_hub_status_data,
1254 .hub_control = ehci_hub_control,
1255 .relinquish_port = ehci_relinquish_port,
1256 .port_handed_over = ehci_port_handed_over,
1257
1258 /*
1259 * PM support
1260 */
Hemant Kumar45d211b2012-05-31 17:58:43 -07001261 .bus_suspend = ehci_hsic_bus_suspend,
1262 .bus_resume = ehci_hsic_bus_resume,
1263
Hemant Kumar4d50a432012-08-15 09:06:35 -07001264 .log_urb = dbg_log_event,
Hemant Kumar105d07f2012-07-02 15:33:07 -07001265 .dump_regs = dump_hsic_regs,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001266
1267 .enable_ulpi_control = ehci_msm_enable_ulpi_control,
1268 .disable_ulpi_control = ehci_msm_disable_ulpi_control,
Hemant Kumara3d4a6e2012-10-16 17:57:08 -07001269
1270 .set_autosuspend_delay = ehci_msm_set_autosuspend_delay,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301271};
1272
1273static int msm_hsic_init_clocks(struct msm_hsic_hcd *mehci, u32 init)
1274{
1275 int ret = 0;
1276
1277 if (!init)
1278 goto put_clocks;
1279
Lena Salman8c8ba382012-02-14 15:59:31 +02001280 /*core_clk is required for LINK protocol engine
1281 *clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001282 mehci->core_clk = clk_get(mehci->dev, "core_clk");
1283 if (IS_ERR(mehci->core_clk)) {
1284 dev_err(mehci->dev, "failed to get core_clk\n");
1285 ret = PTR_ERR(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301286 return ret;
1287 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301288
Lena Salman8c8ba382012-02-14 15:59:31 +02001289 /* alt_core_clk is for LINK to be used during PHY RESET
1290 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001291 mehci->alt_core_clk = clk_get(mehci->dev, "alt_core_clk");
Manu Gautam863b74132012-11-21 14:30:04 +05301292 if (IS_ERR(mehci->alt_core_clk))
1293 dev_dbg(mehci->dev, "failed to get alt_core_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301294
Lena Salman8c8ba382012-02-14 15:59:31 +02001295 /* phy_clk is required for HSIC PHY operation
1296 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001297 mehci->phy_clk = clk_get(mehci->dev, "phy_clk");
1298 if (IS_ERR(mehci->phy_clk)) {
1299 dev_err(mehci->dev, "failed to get phy_clk\n");
1300 ret = PTR_ERR(mehci->phy_clk);
1301 goto put_alt_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301302 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301303
1304 /* 10MHz cal_clk is required for calibration of I/O pads */
Manu Gautam5143b252012-01-05 19:25:23 -08001305 mehci->cal_clk = clk_get(mehci->dev, "cal_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301306 if (IS_ERR(mehci->cal_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001307 dev_err(mehci->dev, "failed to get cal_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301308 ret = PTR_ERR(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001309 goto put_phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301310 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301311
1312 /* ahb_clk is required for data transfers */
Manu Gautam5143b252012-01-05 19:25:23 -08001313 mehci->ahb_clk = clk_get(mehci->dev, "iface_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301314 if (IS_ERR(mehci->ahb_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001315 dev_err(mehci->dev, "failed to get iface_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301316 ret = PTR_ERR(mehci->ahb_clk);
1317 goto put_cal_clk;
1318 }
1319
Manu Gautam28b1bac2012-01-30 16:43:06 +05301320 clk_prepare_enable(mehci->core_clk);
1321 clk_prepare_enable(mehci->phy_clk);
1322 clk_prepare_enable(mehci->cal_clk);
1323 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301324
1325 return 0;
1326
1327put_clocks:
Jack Phamfd193eb2012-02-22 15:38:08 -08001328 if (!atomic_read(&mehci->in_lpm)) {
1329 clk_disable_unprepare(mehci->core_clk);
1330 clk_disable_unprepare(mehci->phy_clk);
1331 clk_disable_unprepare(mehci->cal_clk);
1332 clk_disable_unprepare(mehci->ahb_clk);
1333 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301334 clk_put(mehci->ahb_clk);
1335put_cal_clk:
1336 clk_put(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001337put_phy_clk:
1338 clk_put(mehci->phy_clk);
1339put_alt_core_clk:
Manu Gautam863b74132012-11-21 14:30:04 +05301340 if (!IS_ERR(mehci->alt_core_clk))
1341 clk_put(mehci->alt_core_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001342 clk_put(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301343
1344 return ret;
1345}
Vamsi Krishna34f01582011-12-14 19:54:42 -08001346static irqreturn_t hsic_peripheral_status_change(int irq, void *dev_id)
1347{
1348 struct msm_hsic_hcd *mehci = dev_id;
1349
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001350 pr_debug("%s: mehci:%p dev_id:%p\n", __func__, mehci, dev_id);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001351
1352 if (mehci)
1353 msm_hsic_config_gpios(mehci, 0);
1354
1355 return IRQ_HANDLED;
1356}
1357
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001358static irqreturn_t msm_hsic_wakeup_irq(int irq, void *data)
1359{
1360 struct msm_hsic_hcd *mehci = data;
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301361 int ret;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001362
Hemant Kumar6fd65032012-05-23 13:02:24 -07001363 mehci->wakeup_int_cnt++;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001364 dbg_log_event(NULL, "Remote Wakeup IRQ", mehci->wakeup_int_cnt);
Hemant Kumar6fd65032012-05-23 13:02:24 -07001365 dev_dbg(mehci->dev, "%s: hsic remote wakeup interrupt cnt: %u\n",
1366 __func__, mehci->wakeup_int_cnt);
1367
Sudhir Sharma1673e302012-08-27 17:37:24 -07001368 wake_lock(&mehci->wlock);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001369
Hemant Kumard343c012012-09-06 19:57:14 -07001370 spin_lock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001371 if (mehci->wakeup_irq_enabled) {
1372 mehci->wakeup_irq_enabled = 0;
1373 disable_irq_wake(irq);
1374 disable_irq_nosync(irq);
1375 }
Hemant Kumard343c012012-09-06 19:57:14 -07001376 spin_unlock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001377
Sudhir Sharma1673e302012-08-27 17:37:24 -07001378 if (!atomic_read(&mehci->pm_usage_cnt)) {
Pavankumar Kondeti968cf272012-09-27 12:04:12 +05301379 ret = pm_runtime_get(mehci->dev);
1380 /*
1381 * HSIC runtime resume can race with us.
1382 * if we are active (ret == 1) or resuming
1383 * (ret == -EINPROGRESS), decrement the
1384 * PM usage counter before returning.
1385 */
1386 if ((ret == 1) || (ret == -EINPROGRESS))
1387 pm_runtime_put_noidle(mehci->dev);
1388 else
1389 atomic_set(&mehci->pm_usage_cnt, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07001390 }
1391
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001392 return IRQ_HANDLED;
1393}
1394
Hemant Kumare6275972012-02-29 20:06:21 -08001395static int ehci_hsic_msm_bus_show(struct seq_file *s, void *unused)
1396{
1397 if (debug_bus_voting_enabled)
1398 seq_printf(s, "enabled\n");
1399 else
1400 seq_printf(s, "disabled\n");
1401
1402 return 0;
1403}
1404
1405static int ehci_hsic_msm_bus_open(struct inode *inode, struct file *file)
1406{
1407 return single_open(file, ehci_hsic_msm_bus_show, inode->i_private);
1408}
1409
1410static ssize_t ehci_hsic_msm_bus_write(struct file *file,
1411 const char __user *ubuf, size_t count, loff_t *ppos)
1412{
1413 char buf[8];
1414 int ret;
1415 struct seq_file *s = file->private_data;
1416 struct msm_hsic_hcd *mehci = s->private;
1417
1418 memset(buf, 0x00, sizeof(buf));
1419
1420 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1421 return -EFAULT;
1422
1423 if (!strncmp(buf, "enable", 6)) {
1424 /* Do not vote here. Let hsic driver decide when to vote */
1425 debug_bus_voting_enabled = true;
1426 } else {
1427 debug_bus_voting_enabled = false;
1428 if (mehci->bus_perf_client) {
1429 ret = msm_bus_scale_client_update_request(
1430 mehci->bus_perf_client, 0);
1431 if (ret)
1432 dev_err(mehci->dev, "%s: Failed to devote "
1433 "for bus bw %d\n", __func__, ret);
1434 }
1435 }
1436
1437 return count;
1438}
1439
1440const struct file_operations ehci_hsic_msm_bus_fops = {
1441 .open = ehci_hsic_msm_bus_open,
1442 .read = seq_read,
1443 .write = ehci_hsic_msm_bus_write,
1444 .llseek = seq_lseek,
1445 .release = single_release,
1446};
1447
Hemant Kumar6fd65032012-05-23 13:02:24 -07001448static int ehci_hsic_msm_wakeup_cnt_show(struct seq_file *s, void *unused)
1449{
1450 struct msm_hsic_hcd *mehci = s->private;
1451
1452 seq_printf(s, "%u\n", mehci->wakeup_int_cnt);
1453
1454 return 0;
1455}
1456
1457static int ehci_hsic_msm_wakeup_cnt_open(struct inode *inode, struct file *f)
1458{
1459 return single_open(f, ehci_hsic_msm_wakeup_cnt_show, inode->i_private);
1460}
1461
1462const struct file_operations ehci_hsic_msm_wakeup_cnt_fops = {
1463 .open = ehci_hsic_msm_wakeup_cnt_open,
1464 .read = seq_read,
1465 .llseek = seq_lseek,
1466 .release = single_release,
1467};
1468
Hemant Kumar45d211b2012-05-31 17:58:43 -07001469static int ehci_hsic_msm_data_events_show(struct seq_file *s, void *unused)
1470{
1471 unsigned long flags;
1472 unsigned i;
1473
1474 read_lock_irqsave(&dbg_hsic_data.lck, flags);
1475
1476 i = dbg_hsic_data.idx;
1477 for (dbg_inc(&i); i != dbg_hsic_data.idx; dbg_inc(&i)) {
1478 if (!strnlen(dbg_hsic_data.buf[i], DBG_MSG_LEN))
1479 continue;
1480 seq_printf(s, "%s\n", dbg_hsic_data.buf[i]);
1481 }
1482
1483 read_unlock_irqrestore(&dbg_hsic_data.lck, flags);
1484
1485 return 0;
1486}
1487
1488static int ehci_hsic_msm_data_events_open(struct inode *inode, struct file *f)
1489{
1490 return single_open(f, ehci_hsic_msm_data_events_show, inode->i_private);
1491}
1492
1493const struct file_operations ehci_hsic_msm_dbg_data_fops = {
1494 .open = ehci_hsic_msm_data_events_open,
1495 .read = seq_read,
1496 .llseek = seq_lseek,
1497 .release = single_release,
1498};
1499
1500static int ehci_hsic_msm_ctrl_events_show(struct seq_file *s, void *unused)
1501{
1502 unsigned long flags;
1503 unsigned i;
1504
1505 read_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
1506
1507 i = dbg_hsic_ctrl.idx;
1508 for (dbg_inc(&i); i != dbg_hsic_ctrl.idx; dbg_inc(&i)) {
1509 if (!strnlen(dbg_hsic_ctrl.buf[i], DBG_MSG_LEN))
1510 continue;
1511 seq_printf(s, "%s\n", dbg_hsic_ctrl.buf[i]);
1512 }
1513
1514 read_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
1515
1516 return 0;
1517}
1518
1519static int ehci_hsic_msm_ctrl_events_open(struct inode *inode, struct file *f)
1520{
1521 return single_open(f, ehci_hsic_msm_ctrl_events_show, inode->i_private);
1522}
1523
1524const struct file_operations ehci_hsic_msm_dbg_ctrl_fops = {
1525 .open = ehci_hsic_msm_ctrl_events_open,
1526 .read = seq_read,
1527 .llseek = seq_lseek,
1528 .release = single_release,
1529};
1530
Hemant Kumare6275972012-02-29 20:06:21 -08001531static struct dentry *ehci_hsic_msm_dbg_root;
1532static int ehci_hsic_msm_debugfs_init(struct msm_hsic_hcd *mehci)
1533{
1534 struct dentry *ehci_hsic_msm_dentry;
1535
1536 ehci_hsic_msm_dbg_root = debugfs_create_dir("ehci_hsic_msm_dbg", NULL);
1537
1538 if (!ehci_hsic_msm_dbg_root || IS_ERR(ehci_hsic_msm_dbg_root))
1539 return -ENODEV;
1540
1541 ehci_hsic_msm_dentry = debugfs_create_file("bus_voting",
1542 S_IRUGO | S_IWUSR,
1543 ehci_hsic_msm_dbg_root, mehci,
1544 &ehci_hsic_msm_bus_fops);
1545
1546 if (!ehci_hsic_msm_dentry) {
1547 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1548 return -ENODEV;
1549 }
1550
Hemant Kumar6fd65032012-05-23 13:02:24 -07001551 ehci_hsic_msm_dentry = debugfs_create_file("wakeup_cnt",
1552 S_IRUGO,
1553 ehci_hsic_msm_dbg_root, mehci,
1554 &ehci_hsic_msm_wakeup_cnt_fops);
1555
1556 if (!ehci_hsic_msm_dentry) {
1557 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1558 return -ENODEV;
1559 }
1560
Hemant Kumar45d211b2012-05-31 17:58:43 -07001561 ehci_hsic_msm_dentry = debugfs_create_file("show_ctrl_events",
1562 S_IRUGO,
1563 ehci_hsic_msm_dbg_root, mehci,
1564 &ehci_hsic_msm_dbg_ctrl_fops);
1565
1566 if (!ehci_hsic_msm_dentry) {
1567 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1568 return -ENODEV;
1569 }
1570
1571 ehci_hsic_msm_dentry = debugfs_create_file("show_data_events",
1572 S_IRUGO,
1573 ehci_hsic_msm_dbg_root, mehci,
1574 &ehci_hsic_msm_dbg_data_fops);
1575
1576 if (!ehci_hsic_msm_dentry) {
1577 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1578 return -ENODEV;
1579 }
1580
Hemant Kumare6275972012-02-29 20:06:21 -08001581 return 0;
1582}
1583
1584static void ehci_hsic_msm_debugfs_cleanup(void)
1585{
1586 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1587}
1588
Manu Gautam863b74132012-11-21 14:30:04 +05301589struct msm_hsic_host_platform_data *msm_hsic_dt_to_pdata(
1590 struct platform_device *pdev)
1591{
1592 struct device_node *node = pdev->dev.of_node;
1593 struct msm_hsic_host_platform_data *pdata;
1594
1595 pdata = devm_kzalloc(&pdev->dev, sizeof(*pdata), GFP_KERNEL);
1596 if (!pdata) {
1597 dev_err(&pdev->dev, "unable to allocate platform data\n");
1598 return NULL;
1599 }
1600 pdata->strobe = of_get_named_gpio(node, "hsic,strobe-gpio", 0);
1601 if (pdata->strobe < 0)
1602 pdata->strobe = 0;
1603
1604 pdata->data = of_get_named_gpio(node, "hsic,data-gpio", 0);
1605 if (pdata->data < 0)
1606 pdata->data = 0;
1607
1608 pdata->ignore_cal_pad_config = of_property_read_bool(node,
1609 "hsic,ignore-cal-pad-config");
1610 of_property_read_u32(node, "hsic,strobe-pad-offset",
1611 &pdata->strobe_pad_offset);
1612 of_property_read_u32(node, "hsic,data-pad-offset",
1613 &pdata->data_pad_offset);
1614
1615 return pdata;
1616}
1617
1618
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301619static int __devinit ehci_hsic_msm_probe(struct platform_device *pdev)
1620{
1621 struct usb_hcd *hcd;
1622 struct resource *res;
1623 struct msm_hsic_hcd *mehci;
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +05301624 struct msm_hsic_host_platform_data *pdata;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301625 int ret;
1626
1627 dev_dbg(&pdev->dev, "ehci_msm-hsic probe\n");
1628
Manu Gautam863b74132012-11-21 14:30:04 +05301629 if (pdev->dev.of_node) {
1630 dev_dbg(&pdev->dev, "device tree enabled\n");
1631 pdev->dev.platform_data = msm_hsic_dt_to_pdata(pdev);
1632 dev_set_name(&pdev->dev, ehci_msm_hsic_driver.driver.name);
1633 }
1634 if (!pdev->dev.platform_data)
1635 dev_dbg(&pdev->dev, "No platform data given\n");
1636
Ofir Cohen4cc55372012-11-20 11:17:24 +02001637 if (!pdev->dev.dma_mask)
1638 pdev->dev.dma_mask = &ehci_msm_hsic_dma_mask;
1639 if (!pdev->dev.coherent_dma_mask)
1640 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
1641
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301642 /* After parent device's probe is executed, it will be put in suspend
1643 * mode. When child device's probe is called, driver core is not
1644 * resuming parent device due to which parent will be in suspend even
1645 * though child is active. Hence resume the parent device explicitly.
1646 */
1647 if (pdev->dev.parent)
1648 pm_runtime_get_sync(pdev->dev.parent);
1649
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301650 hcd = usb_create_hcd(&msm_hsic_driver, &pdev->dev,
1651 dev_name(&pdev->dev));
1652 if (!hcd) {
1653 dev_err(&pdev->dev, "Unable to create HCD\n");
1654 return -ENOMEM;
1655 }
1656
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05301657 hcd_to_bus(hcd)->skip_resume = true;
1658
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301659 hcd->irq = platform_get_irq(pdev, 0);
1660 if (hcd->irq < 0) {
1661 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1662 ret = hcd->irq;
1663 goto put_hcd;
1664 }
1665
1666 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1667 if (!res) {
1668 dev_err(&pdev->dev, "Unable to get memory resource\n");
1669 ret = -ENODEV;
1670 goto put_hcd;
1671 }
1672
1673 hcd->rsrc_start = res->start;
1674 hcd->rsrc_len = resource_size(res);
1675 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1676 if (!hcd->regs) {
1677 dev_err(&pdev->dev, "ioremap failed\n");
1678 ret = -ENOMEM;
1679 goto put_hcd;
1680 }
1681
1682 mehci = hcd_to_hsic(hcd);
1683 mehci->dev = &pdev->dev;
Hemant Kumar30d361c2012-08-20 14:44:40 -07001684 pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301685
Hemant Kumard343c012012-09-06 19:57:14 -07001686 spin_lock_init(&mehci->wakeup_lock);
1687
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001688 mehci->ehci.susp_sof_bug = 1;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001689 mehci->ehci.reset_sof_bug = 1;
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001690
Hemant Kumare4040492012-06-21 17:35:42 -07001691 mehci->ehci.resume_sof_bug = 1;
1692
Hemant Kumar30d361c2012-08-20 14:44:40 -07001693 if (pdata)
1694 mehci->ehci.log2_irq_thresh = pdata->log2_irq_thresh;
Hemant Kumar933e0402012-05-22 11:11:40 -07001695
Ofir Cohen4cc55372012-11-20 11:17:24 +02001696 ret = msm_hsic_init_gdsc(mehci, 1);
1697 if (ret) {
1698 dev_err(&pdev->dev, "unable to initialize GDSC\n");
1699 ret = -ENODEV;
1700 goto put_hcd;
1701 }
1702
Vamsi Krishna34f01582011-12-14 19:54:42 -08001703 res = platform_get_resource_byname(pdev,
1704 IORESOURCE_IRQ,
1705 "peripheral_status_irq");
1706 if (res)
1707 mehci->peripheral_status_irq = res->start;
1708
Jack Pham0cc75c42012-10-10 02:03:50 +02001709 res = platform_get_resource_byname(pdev, IORESOURCE_IRQ, "wakeup");
Hemant Kumar6fd65032012-05-23 13:02:24 -07001710 if (res) {
Jack Pham0cc75c42012-10-10 02:03:50 +02001711 mehci->wakeup_irq = res->start;
Hemant Kumar6fd65032012-05-23 13:02:24 -07001712 dev_dbg(mehci->dev, "wakeup_irq: %d\n", mehci->wakeup_irq);
1713 }
1714
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301715 ret = msm_hsic_init_clocks(mehci, 1);
1716 if (ret) {
1717 dev_err(&pdev->dev, "unable to initialize clocks\n");
1718 ret = -ENODEV;
1719 goto unmap;
1720 }
1721
1722 ret = msm_hsic_init_vddcx(mehci, 1);
1723 if (ret) {
1724 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
1725 ret = -ENODEV;
1726 goto deinit_clocks;
1727 }
1728
Vamsi Krishnafce1bd22012-08-17 17:12:26 -07001729 init_completion(&mehci->rt_completion);
1730 init_completion(&mehci->gpt0_completion);
Ofir Cohen5dddb152012-11-14 11:18:50 +02001731
1732 msm_hsic_phy_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301733
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001734 ehci_wq = create_singlethread_workqueue("ehci_wq");
1735 if (!ehci_wq) {
1736 dev_err(&pdev->dev, "unable to create workqueue\n");
1737 ret = -ENOMEM;
1738 goto deinit_vddcx;
1739 }
1740
1741 INIT_WORK(&mehci->bus_vote_w, ehci_hsic_bus_vote_w);
1742
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301743 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
1744 if (ret) {
1745 dev_err(&pdev->dev, "unable to register HCD\n");
Ofir Cohen5dddb152012-11-14 11:18:50 +02001746 goto destroy_wq;
1747 }
1748
1749 ret = msm_hsic_start(mehci);
1750 if (ret) {
1751 dev_err(&pdev->dev, "unable to initialize PHY\n");
1752 goto destroy_wq;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301753 }
1754
1755 device_init_wakeup(&pdev->dev, 1);
Sudhir Sharma1673e302012-08-27 17:37:24 -07001756 wake_lock_init(&mehci->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
1757 wake_lock(&mehci->wlock);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001758
1759 if (mehci->peripheral_status_irq) {
1760 ret = request_threaded_irq(mehci->peripheral_status_irq,
1761 NULL, hsic_peripheral_status_change,
1762 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1763 | IRQF_SHARED,
1764 "hsic_peripheral_status", mehci);
1765 if (ret)
1766 dev_err(&pdev->dev, "%s:request_irq:%d failed:%d",
1767 __func__, mehci->peripheral_status_irq, ret);
1768 }
1769
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001770 /* configure wakeup irq */
Hemant Kumar6fd65032012-05-23 13:02:24 -07001771 if (mehci->wakeup_irq) {
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07001772 /* In case if wakeup gpio is pulled high at this point
1773 * remote wakeup interrupt fires right after request_irq.
1774 * Remote wake up interrupt only needs to be enabled when
1775 * HSIC bus goes to suspend.
1776 */
1777 irq_set_status_flags(mehci->wakeup_irq, IRQ_NOAUTOEN);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001778 ret = request_irq(mehci->wakeup_irq, msm_hsic_wakeup_irq,
Hemant Kumar6fd65032012-05-23 13:02:24 -07001779 IRQF_TRIGGER_HIGH,
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001780 "msm_hsic_wakeup", mehci);
Hemant Kumarbbcdcbc2012-08-30 14:23:38 -07001781 if (ret) {
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001782 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
1783 mehci->wakeup_irq, ret);
1784 mehci->wakeup_irq = 0;
1785 }
1786 }
1787
Hemant Kumare6275972012-02-29 20:06:21 -08001788 ret = ehci_hsic_msm_debugfs_init(mehci);
1789 if (ret)
1790 dev_dbg(&pdev->dev, "mode debugfs file is"
1791 "not available\n");
1792
1793 if (pdata && pdata->bus_scale_table) {
1794 mehci->bus_perf_client =
1795 msm_bus_scale_register_client(pdata->bus_scale_table);
1796 /* Configure BUS performance parameters for MAX bandwidth */
1797 if (mehci->bus_perf_client) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001798 mehci->bus_vote = true;
1799 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -08001800 } else {
1801 dev_err(&pdev->dev, "%s: Failed to register BUS "
1802 "scaling client!!\n", __func__);
1803 }
1804 }
1805
Hemant Kumar105d07f2012-07-02 15:33:07 -07001806 __mehci = mehci;
1807
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001808 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301809 pm_qos_add_request(&mehci->pm_qos_req_dma,
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001810 PM_QOS_CPU_DMA_LATENCY, pdata->standalone_latency + 1);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301811
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301812 /*
1813 * This pdev->dev is assigned parent of root-hub by USB core,
1814 * hence, runtime framework automatically calls this driver's
1815 * runtime APIs based on root-hub's state.
1816 */
1817 pm_runtime_set_active(&pdev->dev);
1818 pm_runtime_enable(&pdev->dev);
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301819 /* Decrement the parent device's counter after probe.
1820 * As child is active, parent will not be put into
1821 * suspend mode.
1822 */
1823 if (pdev->dev.parent)
1824 pm_runtime_put_sync(pdev->dev.parent);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301825
1826 return 0;
1827
Ofir Cohen5dddb152012-11-14 11:18:50 +02001828destroy_wq:
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001829 destroy_workqueue(ehci_wq);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301830deinit_vddcx:
1831 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02001832 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301833deinit_clocks:
1834 msm_hsic_init_clocks(mehci, 0);
1835unmap:
1836 iounmap(hcd->regs);
1837put_hcd:
1838 usb_put_hcd(hcd);
1839
1840 return ret;
1841}
1842
1843static int __devexit ehci_hsic_msm_remove(struct platform_device *pdev)
1844{
1845 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1846 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301847 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1848
Hemant Kumarbc8bdf62012-10-17 12:29:51 -07001849 if (pdata && pdata->standalone_latency)
Pavankumar Kondeti4f5dc3b2012-09-07 15:33:09 +05301850 pm_qos_remove_request(&mehci->pm_qos_req_dma);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301851
Vamsi Krishna34f01582011-12-14 19:54:42 -08001852 if (mehci->peripheral_status_irq)
1853 free_irq(mehci->peripheral_status_irq, mehci);
Jack Phamfe441ea2012-03-23 17:03:15 -07001854
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001855 if (mehci->wakeup_irq) {
Jack Phamfe441ea2012-03-23 17:03:15 -07001856 if (mehci->wakeup_irq_enabled)
1857 disable_irq_wake(mehci->wakeup_irq);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001858 free_irq(mehci->wakeup_irq, mehci);
1859 }
Vamsi Krishna34f01582011-12-14 19:54:42 -08001860
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001861 /*
1862 * If the update request is called after unregister, the request will
1863 * fail. Results are undefined if unregister is called in the middle of
1864 * update request.
1865 */
1866 mehci->bus_vote = false;
1867 cancel_work_sync(&mehci->bus_vote_w);
1868
Hemant Kumare6275972012-02-29 20:06:21 -08001869 if (mehci->bus_perf_client)
1870 msm_bus_scale_unregister_client(mehci->bus_perf_client);
1871
1872 ehci_hsic_msm_debugfs_cleanup();
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301873 device_init_wakeup(&pdev->dev, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301874 pm_runtime_set_suspended(&pdev->dev);
1875
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001876 destroy_workqueue(ehci_wq);
1877
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301878 usb_remove_hcd(hcd);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301879 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301880 msm_hsic_init_vddcx(mehci, 0);
Ofir Cohen4cc55372012-11-20 11:17:24 +02001881 msm_hsic_init_gdsc(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301882
1883 msm_hsic_init_clocks(mehci, 0);
Sudhir Sharma1673e302012-08-27 17:37:24 -07001884 wake_lock_destroy(&mehci->wlock);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301885 iounmap(hcd->regs);
1886 usb_put_hcd(hcd);
1887
1888 return 0;
1889}
1890
1891#ifdef CONFIG_PM_SLEEP
1892static int msm_hsic_pm_suspend(struct device *dev)
1893{
Jack Phambe05fbb2012-05-16 10:56:26 -07001894 int ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301895 struct usb_hcd *hcd = dev_get_drvdata(dev);
1896 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1897
1898 dev_dbg(dev, "ehci-msm-hsic PM suspend\n");
1899
Hemant Kumar45d211b2012-05-31 17:58:43 -07001900 dbg_log_event(NULL, "PM Suspend", 0);
1901
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301902 if (device_may_wakeup(dev))
1903 enable_irq_wake(hcd->irq);
1904
Jack Phambe05fbb2012-05-16 10:56:26 -07001905 ret = msm_hsic_suspend(mehci);
1906
1907 if (ret && device_may_wakeup(dev))
1908 disable_irq_wake(hcd->irq);
1909
1910 return ret;
Jack Phamfe441ea2012-03-23 17:03:15 -07001911}
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301912
Jack Pham16b06f82012-08-14 20:03:59 -07001913static int msm_hsic_pm_suspend_noirq(struct device *dev)
1914{
1915 struct usb_hcd *hcd = dev_get_drvdata(dev);
1916 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1917
1918 if (mehci->async_int) {
1919 dev_dbg(dev, "suspend_noirq: Aborting due to pending interrupt\n");
1920 return -EBUSY;
1921 }
1922
1923 return 0;
1924}
1925
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301926static int msm_hsic_pm_resume(struct device *dev)
1927{
1928 int ret;
1929 struct usb_hcd *hcd = dev_get_drvdata(dev);
1930 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1931
Jack Pham16b06f82012-08-14 20:03:59 -07001932 dev_dbg(dev, "ehci-msm-hsic PM resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001933 dbg_log_event(NULL, "PM Resume", 0);
1934
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301935 if (device_may_wakeup(dev))
1936 disable_irq_wake(hcd->irq);
1937
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05301938 /*
1939 * Keep HSIC in Low Power Mode if system is resumed
1940 * by any other wakeup source. HSIC is resumed later
1941 * when remote wakeup is received or interface driver
1942 * start I/O.
1943 */
Pavankumar Kondeti41d004c2012-11-09 10:54:00 +05301944 if (!atomic_read(&mehci->pm_usage_cnt) &&
1945 pm_runtime_suspended(dev))
Pavankumar Kondeti6f199aa2012-09-18 17:52:51 +05301946 return 0;
1947
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301948 ret = msm_hsic_resume(mehci);
1949 if (ret)
1950 return ret;
1951
1952 /* Bring the device to full powered state upon system resume */
1953 pm_runtime_disable(dev);
1954 pm_runtime_set_active(dev);
1955 pm_runtime_enable(dev);
1956
1957 return 0;
1958}
1959#endif
1960
1961#ifdef CONFIG_PM_RUNTIME
1962static int msm_hsic_runtime_idle(struct device *dev)
1963{
1964 dev_dbg(dev, "EHCI runtime idle\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301965 return 0;
1966}
1967
1968static int msm_hsic_runtime_suspend(struct device *dev)
1969{
1970 struct usb_hcd *hcd = dev_get_drvdata(dev);
1971 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1972
1973 dev_dbg(dev, "EHCI runtime suspend\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001974
1975 dbg_log_event(NULL, "Run Time PM Suspend", 0);
1976
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301977 return msm_hsic_suspend(mehci);
1978}
1979
1980static int msm_hsic_runtime_resume(struct device *dev)
1981{
1982 struct usb_hcd *hcd = dev_get_drvdata(dev);
1983 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1984
1985 dev_dbg(dev, "EHCI runtime resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001986
1987 dbg_log_event(NULL, "Run Time PM Resume", 0);
1988
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301989 return msm_hsic_resume(mehci);
1990}
1991#endif
1992
1993#ifdef CONFIG_PM
1994static const struct dev_pm_ops msm_hsic_dev_pm_ops = {
1995 SET_SYSTEM_SLEEP_PM_OPS(msm_hsic_pm_suspend, msm_hsic_pm_resume)
Jack Pham16b06f82012-08-14 20:03:59 -07001996 .suspend_noirq = msm_hsic_pm_suspend_noirq,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301997 SET_RUNTIME_PM_OPS(msm_hsic_runtime_suspend, msm_hsic_runtime_resume,
1998 msm_hsic_runtime_idle)
1999};
2000#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002001static const struct of_device_id hsic_host_dt_match[] = {
2002 { .compatible = "qcom,hsic-host",
2003 },
2004 {}
2005};
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302006static struct platform_driver ehci_msm_hsic_driver = {
2007 .probe = ehci_hsic_msm_probe,
2008 .remove = __devexit_p(ehci_hsic_msm_remove),
2009 .driver = {
2010 .name = "msm_hsic_host",
2011#ifdef CONFIG_PM
2012 .pm = &msm_hsic_dev_pm_ops,
2013#endif
Ofir Cohen4cc55372012-11-20 11:17:24 +02002014 .of_match_table = hsic_host_dt_match,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302015 },
2016};