blob: 368888521d86da85dec0a24cfb96f63679a0a6f5 [file] [log] [blame]
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301/* ehci-msm-hsic.c - HSUSB Host Controller Driver Implementation
2 *
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05303 * Copyright (c) 2011-2013, Linux Foundation. All rights reserved.
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05304 *
5 * Partly derived from ehci-fsl.c and ehci-hcd.c
6 * Copyright (c) 2000-2004 by David Brownell
7 * Copyright (c) 2005 MontaVista Software
8 *
9 * All source code in this file is licensed under the following license except
10 * where indicated.
11 *
12 * This program is free software; you can redistribute it and/or modify it
13 * under the terms of the GNU General Public License version 2 as published
14 * by the Free Software Foundation.
15 *
16 * This program is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
19 *
20 * See the GNU General Public License for more details.
21 * You should have received a copy of the GNU General Public License
22 * along with this program; if not, you can find it at http://www.fsf.org
23 */
24
25#include <linux/platform_device.h>
26#include <linux/clk.h>
27#include <linux/err.h>
Hemant Kumare6275972012-02-29 20:06:21 -080028#include <linux/debugfs.h>
29#include <linux/seq_file.h>
Ajay Dudaniab3bf192012-08-28 09:58:04 -070030#include <linux/wakelock.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053031#include <linux/pm_runtime.h>
32#include <linux/regulator/consumer.h>
33
34#include <linux/usb/msm_hsusb_hw.h>
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +053035#include <linux/usb/msm_hsusb.h>
36#include <linux/gpio.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030037#include <linux/spinlock.h>
Ajay Dudani11ab1d52012-08-17 17:12:26 -070038#include <linux/kthread.h>
39#include <linux/wait.h>
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +053040#include <linux/pm_qos.h>
Hemant Kumar5e386632012-08-30 14:23:38 -070041#include <linux/irq.h>
Pavankumar Kondeti95933142013-10-10 22:39:15 +053042#include <linux/ktime.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030043
44#include <mach/msm_bus.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053045#include <mach/clk.h>
46#include <mach/msm_iomap.h>
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +053047#include <mach/msm_xo.h>
Vamsi Krishna34f01582011-12-14 19:54:42 -080048#include <linux/spinlock.h>
Hemant Kumar45d211b2012-05-31 17:58:43 -070049#include <linux/cpu.h>
Amit Blayd6ea6102012-06-07 16:26:24 +030050#include <mach/rpm-regulator.h>
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053051
52#define MSM_USB_BASE (hcd->regs)
Hemant Kumar105d07f2012-07-02 15:33:07 -070053#define USB_REG_START_OFFSET 0x90
54#define USB_REG_END_OFFSET 0x250
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053055
Pavankumar Kondeti95933142013-10-10 22:39:15 +053056#define RESUME_RETRY_LIMIT 3
57#define RESUME_SIGNAL_TIME_USEC (21 * 1000)
58#define RESUME_SIGNAL_TIME_SOF_USEC (23 * 1000)
59
Hemant Kumar2309eaa2012-08-14 16:46:42 -070060static struct workqueue_struct *ehci_wq;
Ajay Dudani11ab1d52012-08-17 17:12:26 -070061struct ehci_timer {
62#define GPT_LD(p) ((p) & 0x00FFFFFF)
63 u32 gptimer0_ld;
64#define GPT_RUN BIT(31)
65#define GPT_RESET BIT(30)
66#define GPT_MODE BIT(24)
67#define GPT_CNT(p) ((p) & 0x00FFFFFF)
68 u32 gptimer0_ctrl;
69
70 u32 gptimer1_ld;
71 u32 gptimer1_ctrl;
72};
Hemant Kumar2309eaa2012-08-14 16:46:42 -070073
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053074struct msm_hsic_hcd {
75 struct ehci_hcd ehci;
Hemant Kumar4cd49e12012-09-06 19:57:14 -070076 spinlock_t wakeup_lock;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053077 struct device *dev;
78 struct clk *ahb_clk;
Manu Gautam5143b252012-01-05 19:25:23 -080079 struct clk *core_clk;
80 struct clk *alt_core_clk;
81 struct clk *phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053082 struct clk *cal_clk;
83 struct regulator *hsic_vddcx;
Pavankumar Kondetibbd05822013-01-28 17:04:39 +053084 atomic_t async_int;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +053085 atomic_t in_lpm;
Ajay Dudaniab3bf192012-08-28 09:58:04 -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;
Hemant Kumar6fd65032012-05-23 13:02:24 -070089 int wakeup_gpio;
Jack Phamfe441ea2012-03-23 17:03:15 -070090 bool wakeup_irq_enabled;
Ajay Dudaniab3bf192012-08-28 09:58:04 -070091 atomic_t pm_usage_cnt;
Hemant Kumare6275972012-02-29 20:06:21 -080092 uint32_t bus_perf_client;
Hemant Kumar6fd65032012-05-23 13:02:24 -070093 uint32_t wakeup_int_cnt;
Amit Blayd6ea6102012-06-07 16:26:24 +030094 enum usb_vdd_type vdd_type;
Hemant Kumar2309eaa2012-08-14 16:46:42 -070095
96 struct work_struct bus_vote_w;
97 bool bus_vote;
Ajay Dudani11ab1d52012-08-17 17:12:26 -070098
99 /* gp timer */
100 struct ehci_timer __iomem *timer;
101 struct completion gpt0_completion;
102 struct completion rt_completion;
103 int resume_status;
104 int resume_again;
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530105 int bus_reset;
106 int reset_again;
Pavankumar Kondeti95933142013-10-10 22:39:15 +0530107 ktime_t resume_start_t;
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +0530108
109 struct pm_qos_request pm_qos_req_dma;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530110};
111
Hemant Kumar105d07f2012-07-02 15:33:07 -0700112struct msm_hsic_hcd *__mehci;
113
Hemant Kumare6275972012-02-29 20:06:21 -0800114static bool debug_bus_voting_enabled = true;
Hemant Kumar45d211b2012-05-31 17:58:43 -0700115
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700116static unsigned int enable_payload_log = 1;
117module_param(enable_payload_log, uint, S_IRUGO | S_IWUSR);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700118static unsigned int enable_dbg_log = 1;
119module_param(enable_dbg_log, uint, S_IRUGO | S_IWUSR);
120/*by default log ep0 and efs sync ep*/
121static unsigned int ep_addr_rxdbg_mask = 9;
122module_param(ep_addr_rxdbg_mask, uint, S_IRUGO | S_IWUSR);
123static unsigned int ep_addr_txdbg_mask = 9;
124module_param(ep_addr_txdbg_mask, uint, S_IRUGO | S_IWUSR);
125
126/* Maximum debug message length */
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700127#define DBG_MSG_LEN 128UL
Hemant Kumar45d211b2012-05-31 17:58:43 -0700128
129/* Maximum number of messages */
130#define DBG_MAX_MSG 256UL
131
132#define TIME_BUF_LEN 20
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700133#define HEX_DUMP_LEN 72
Hemant Kumar45d211b2012-05-31 17:58:43 -0700134
135enum event_type {
136 EVENT_UNDEF = -1,
137 URB_SUBMIT,
138 URB_COMPLETE,
139 EVENT_NONE,
140};
141
142#define EVENT_STR_LEN 5
143
Hemant Kumar45d211b2012-05-31 17:58:43 -0700144static enum event_type str_to_event(const char *name)
145{
146 if (!strncasecmp("S", name, EVENT_STR_LEN))
147 return URB_SUBMIT;
148 if (!strncasecmp("C", name, EVENT_STR_LEN))
149 return URB_COMPLETE;
150 if (!strncasecmp("", name, EVENT_STR_LEN))
151 return EVENT_NONE;
152
153 return EVENT_UNDEF;
154}
155
156/*log ep0 activity*/
157static struct {
158 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
159 unsigned idx; /* index */
160 rwlock_t lck; /* lock */
161} dbg_hsic_ctrl = {
162 .idx = 0,
163 .lck = __RW_LOCK_UNLOCKED(lck)
164};
165
166static struct {
167 char (buf[DBG_MAX_MSG])[DBG_MSG_LEN]; /* buffer */
168 unsigned idx; /* index */
169 rwlock_t lck; /* lock */
170} dbg_hsic_data = {
171 .idx = 0,
172 .lck = __RW_LOCK_UNLOCKED(lck)
173};
174
175/**
176 * dbg_inc: increments debug event index
177 * @idx: buffer index
178 */
179static void dbg_inc(unsigned *idx)
180{
181 *idx = (*idx + 1) & (DBG_MAX_MSG-1);
182}
183
184/*get_timestamp - returns time of day in us */
185static char *get_timestamp(char *tbuf)
186{
187 unsigned long long t;
188 unsigned long nanosec_rem;
189
190 t = cpu_clock(smp_processor_id());
191 nanosec_rem = do_div(t, 1000000000)/1000;
192 scnprintf(tbuf, TIME_BUF_LEN, "[%5lu.%06lu] ", (unsigned long)t,
193 nanosec_rem);
194 return tbuf;
195}
196
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530197static int allow_dbg_log(struct urb *urb, int ep_addr)
Hemant Kumar45d211b2012-05-31 17:58:43 -0700198{
199 int dir, num;
200
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530201 dir = usb_urb_dir_in(urb) ? USB_DIR_IN : USB_DIR_OUT;
Hemant Kumar45d211b2012-05-31 17:58:43 -0700202 num = ep_addr & ~USB_DIR_IN;
203 num = 1 << num;
204
205 if ((dir == USB_DIR_IN) && (num & ep_addr_rxdbg_mask))
206 return 1;
207 if ((dir == USB_DIR_OUT) && (num & ep_addr_txdbg_mask))
208 return 1;
209
210 return 0;
211}
212
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530213static char *
214get_hex_data(char *dbuf, struct urb *urb, int event, int status, size_t max_len)
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700215{
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700216 char *ubuf = urb->transfer_buffer;
217 size_t len = event ? \
218 urb->actual_length : urb->transfer_buffer_length;
219
220 if (status == -EINPROGRESS)
221 status = 0;
222
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530223 /*Only dump ep in successful completions and epout submissions*/
224 if (len && !status && ((usb_urb_dir_in(urb) && event) ||
225 (usb_urb_dir_out(urb) && !event))) {
226 if (len >= max_len)
227 len = max_len;
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700228 hex_dump_to_buffer(ubuf, len, 32, 4, dbuf, HEX_DUMP_LEN, 0);
229 } else {
230 dbuf = "";
231 }
232
233 return dbuf;
234}
235
Hemant Kumar45d211b2012-05-31 17:58:43 -0700236static void dbg_log_event(struct urb *urb, char * event, unsigned extra)
237{
238 unsigned long flags;
239 int ep_addr;
240 char tbuf[TIME_BUF_LEN];
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700241 char dbuf[HEX_DUMP_LEN];
Hemant Kumar45d211b2012-05-31 17:58:43 -0700242
243 if (!enable_dbg_log)
244 return;
245
246 if (!urb) {
247 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
248 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx], DBG_MSG_LEN,
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700249 "%s: %s : %u", get_timestamp(tbuf), event, extra);
Hemant Kumar45d211b2012-05-31 17:58:43 -0700250 dbg_inc(&dbg_hsic_ctrl.idx);
251 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
252 return;
253 }
254
255 ep_addr = urb->ep->desc.bEndpointAddress;
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530256 if (!allow_dbg_log(urb, ep_addr))
Hemant Kumar45d211b2012-05-31 17:58:43 -0700257 return;
258
259 if ((ep_addr & 0x0f) == 0x0) {
260 /*submit event*/
261 if (!str_to_event(event)) {
262 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
263 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
264 DBG_MSG_LEN, "%s: [%s : %p]:[%s] "
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530265 "%02x %02x %04x %04x %04x %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700266 get_timestamp(tbuf), event, urb,
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530267 usb_urb_dir_in(urb) ? "in" : "out",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700268 urb->setup_packet[0], urb->setup_packet[1],
269 (urb->setup_packet[3] << 8) |
270 urb->setup_packet[2],
271 (urb->setup_packet[5] << 8) |
272 urb->setup_packet[4],
273 (urb->setup_packet[7] << 8) |
274 urb->setup_packet[6],
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530275 urb->transfer_buffer_length, extra,
276 enable_payload_log ? get_hex_data(dbuf, urb,
277 str_to_event(event), extra, 16) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700278
279 dbg_inc(&dbg_hsic_ctrl.idx);
280 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
281 } else {
282 write_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
283 scnprintf(dbg_hsic_ctrl.buf[dbg_hsic_ctrl.idx],
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530284 DBG_MSG_LEN, "%s: [%s : %p]:[%s] %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700285 get_timestamp(tbuf), event, urb,
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530286 usb_urb_dir_in(urb) ? "in" : "out",
287 urb->actual_length, extra,
288 enable_payload_log ? get_hex_data(dbuf, urb,
289 str_to_event(event), extra, 16) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700290
291 dbg_inc(&dbg_hsic_ctrl.idx);
292 write_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
293 }
294 } else {
295 write_lock_irqsave(&dbg_hsic_data.lck, flags);
296 scnprintf(dbg_hsic_data.buf[dbg_hsic_data.idx], DBG_MSG_LEN,
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700297 "%s: [%s : %p]:ep%d[%s] %u %d %s",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700298 get_timestamp(tbuf), event, urb, ep_addr & 0x0f,
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530299 usb_urb_dir_in(urb) ? "in" : "out",
Hemant Kumar45d211b2012-05-31 17:58:43 -0700300 str_to_event(event) ? urb->actual_length :
Hemant Kumardf2d84d2012-08-15 09:06:35 -0700301 urb->transfer_buffer_length, extra,
302 enable_payload_log ? get_hex_data(dbuf, urb,
Pavankumar Kondetia2953ed2013-03-22 15:26:50 +0530303 str_to_event(event), extra, 32) : "");
Hemant Kumar45d211b2012-05-31 17:58:43 -0700304
305 dbg_inc(&dbg_hsic_data.idx);
306 write_unlock_irqrestore(&dbg_hsic_data.lck, flags);
307 }
308}
309
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530310static inline struct msm_hsic_hcd *hcd_to_hsic(struct usb_hcd *hcd)
311{
312 return (struct msm_hsic_hcd *) (hcd->hcd_priv);
313}
314
315static inline struct usb_hcd *hsic_to_hcd(struct msm_hsic_hcd *mehci)
316{
317 return container_of((void *) mehci, struct usb_hcd, hcd_priv);
318}
319
Hemant Kumar105d07f2012-07-02 15:33:07 -0700320static void dump_hsic_regs(struct usb_hcd *hcd)
321{
322 int i;
323 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
324
325 if (atomic_read(&mehci->in_lpm))
326 return;
327
328 for (i = USB_REG_START_OFFSET; i <= USB_REG_END_OFFSET; i += 0x10)
329 pr_info("%p: %08x\t%08x\t%08x\t%08x\n", hcd->regs + i,
330 readl_relaxed(hcd->regs + i),
331 readl_relaxed(hcd->regs + i + 4),
332 readl_relaxed(hcd->regs + i + 8),
333 readl_relaxed(hcd->regs + i + 0xc));
334}
335
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530336#define ULPI_IO_TIMEOUT_USEC (10 * 1000)
337
Amit Blayd6ea6102012-06-07 16:26:24 +0300338#define USB_PHY_VDD_DIG_VOL_NONE 0 /*uV */
Hemant Kumar266d9d52012-10-17 13:48:10 -0700339#define USB_PHY_VDD_DIG_VOL_MIN 945000 /* uV */
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700340#define USB_PHY_VDD_DIG_VOL_MAX 1320000 /* uV */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530341
Lena Salman8c8ba382012-02-14 15:59:31 +0200342#define HSIC_DBG1_REG 0x38
343
Amit Blayd6ea6102012-06-07 16:26:24 +0300344static const int vdd_val[VDD_TYPE_MAX][VDD_VAL_MAX] = {
345 { /* VDD_CX CORNER Voting */
346 [VDD_NONE] = RPM_VREG_CORNER_NONE,
347 [VDD_MIN] = RPM_VREG_CORNER_NOMINAL,
348 [VDD_MAX] = RPM_VREG_CORNER_HIGH,
349 },
350 { /* VDD_CX Voltage Voting */
351 [VDD_NONE] = USB_PHY_VDD_DIG_VOL_NONE,
352 [VDD_MIN] = USB_PHY_VDD_DIG_VOL_MIN,
353 [VDD_MAX] = USB_PHY_VDD_DIG_VOL_MAX,
354 },
355};
356
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530357static int msm_hsic_init_vddcx(struct msm_hsic_hcd *mehci, int init)
358{
359 int ret = 0;
Amit Blayd6ea6102012-06-07 16:26:24 +0300360 int none_vol, min_vol, max_vol;
361
362 if (!mehci->hsic_vddcx) {
363 mehci->vdd_type = VDDCX_CORNER;
364 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
365 "hsic_vdd_dig");
366 if (IS_ERR(mehci->hsic_vddcx)) {
367 mehci->hsic_vddcx = devm_regulator_get(mehci->dev,
368 "HSIC_VDDCX");
369 if (IS_ERR(mehci->hsic_vddcx)) {
370 dev_err(mehci->dev, "unable to get hsic vddcx\n");
371 return PTR_ERR(mehci->hsic_vddcx);
372 }
373 mehci->vdd_type = VDDCX;
374 }
375 }
376
377 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
378 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
379 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530380
381 if (!init)
382 goto disable_reg;
383
Amit Blayd6ea6102012-06-07 16:26:24 +0300384 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530385 if (ret) {
386 dev_err(mehci->dev, "unable to set the voltage"
387 "for hsic vddcx\n");
Mayank Rana189ac052012-03-24 04:35:02 +0530388 return ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530389 }
390
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530391 ret = regulator_enable(mehci->hsic_vddcx);
392 if (ret) {
393 dev_err(mehci->dev, "unable to enable hsic vddcx\n");
394 goto reg_enable_err;
395 }
396
397 return 0;
398
399disable_reg:
400 regulator_disable(mehci->hsic_vddcx);
401reg_enable_err:
Amit Blayd6ea6102012-06-07 16:26:24 +0300402 regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
403
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530404 return ret;
405
406}
407
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530408static int __maybe_unused ulpi_read(struct msm_hsic_hcd *mehci, u32 reg)
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700409{
410 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Devin Kim01e5a5b2012-08-30 02:52:45 -0700411 int cnt = 0;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700412
413 /* initiate read operation */
414 writel_relaxed(ULPI_RUN | ULPI_READ | ULPI_ADDR(reg),
415 USB_ULPI_VIEWPORT);
416
417 /* wait for completion */
Devin Kim01e5a5b2012-08-30 02:52:45 -0700418 while (cnt < ULPI_IO_TIMEOUT_USEC) {
419 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
420 break;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700421 udelay(1);
Devin Kim01e5a5b2012-08-30 02:52:45 -0700422 cnt++;
423 }
424
425 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
426 dev_err(mehci->dev, "ulpi_read: timeout ULPI_VIEWPORT: %08x\n",
427 readl_relaxed(USB_ULPI_VIEWPORT));
428 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
429 readl_relaxed(USB_PORTSC),
430 readl_relaxed(USB_USBCMD),
431 readl_relaxed(USB_FRINDEX));
432
433 /*frame counter increments afte 125us*/
434 udelay(130);
435 dev_err(mehci->dev, "ulpi_read: FRINDEX: %08x\n",
436 readl_relaxed(USB_FRINDEX));
437 return -ETIMEDOUT;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -0700438 }
439
440 return ULPI_DATA_READ(readl_relaxed(USB_ULPI_VIEWPORT));
441}
442
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530443static int ulpi_write(struct msm_hsic_hcd *mehci, u32 val, u32 reg)
444{
445 struct usb_hcd *hcd = hsic_to_hcd(mehci);
446 int cnt = 0;
447
448 /* initiate write operation */
449 writel_relaxed(ULPI_RUN | ULPI_WRITE |
450 ULPI_ADDR(reg) | ULPI_DATA(val),
451 USB_ULPI_VIEWPORT);
452
453 /* wait for completion */
454 while (cnt < ULPI_IO_TIMEOUT_USEC) {
455 if (!(readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_RUN))
456 break;
457 udelay(1);
458 cnt++;
459 }
460
461 if (cnt >= ULPI_IO_TIMEOUT_USEC) {
Devin Kim01e5a5b2012-08-30 02:52:45 -0700462 dev_err(mehci->dev, "ulpi_write: timeout ULPI_VIEWPORT: %08x\n",
463 readl_relaxed(USB_ULPI_VIEWPORT));
464 dev_err(mehci->dev, "PORTSC: %08x USBCMD: %08x FRINDEX: %08x\n",
465 readl_relaxed(USB_PORTSC),
466 readl_relaxed(USB_USBCMD),
467 readl_relaxed(USB_FRINDEX));
468
469 /*frame counter increments afte 125us*/
470 udelay(130);
471 dev_err(mehci->dev, "ulpi_write: FRINDEX: %08x\n",
472 readl_relaxed(USB_FRINDEX));
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530473 return -ETIMEDOUT;
474 }
475
476 return 0;
477}
478
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530479static int msm_hsic_config_gpios(struct msm_hsic_hcd *mehci, int gpio_en)
480{
481 int rc = 0;
482 struct msm_hsic_host_platform_data *pdata;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800483 static int gpio_status;
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530484
485 pdata = mehci->dev->platform_data;
Vamsi Krishna34f01582011-12-14 19:54:42 -0800486
Lena Salman8c8ba382012-02-14 15:59:31 +0200487 if (!pdata || !pdata->strobe || !pdata->data)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530488 return rc;
489
Vamsi Krishna34f01582011-12-14 19:54:42 -0800490 if (gpio_status == gpio_en)
491 return 0;
492
493 gpio_status = gpio_en;
494
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530495 if (!gpio_en)
496 goto free_gpio;
497
498 rc = gpio_request(pdata->strobe, "HSIC_STROBE_GPIO");
499 if (rc < 0) {
500 dev_err(mehci->dev, "gpio request failed for HSIC STROBE\n");
501 return rc;
502 }
503
504 rc = gpio_request(pdata->data, "HSIC_DATA_GPIO");
505 if (rc < 0) {
506 dev_err(mehci->dev, "gpio request failed for HSIC DATA\n");
507 goto free_strobe;
508 }
509
Hemant Kumar6fd65032012-05-23 13:02:24 -0700510 if (mehci->wakeup_gpio) {
511 rc = gpio_request(mehci->wakeup_gpio, "HSIC_WAKEUP_GPIO");
512 if (rc < 0) {
513 dev_err(mehci->dev, "gpio request failed for HSIC WAKEUP\n");
514 goto free_data;
515 }
516 }
517
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530518 return 0;
519
520free_gpio:
Hemant Kumar6fd65032012-05-23 13:02:24 -0700521 if (mehci->wakeup_gpio)
522 gpio_free(mehci->wakeup_gpio);
523free_data:
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530524 gpio_free(pdata->data);
525free_strobe:
526 gpio_free(pdata->strobe);
527
528 return rc;
529}
530
Vamsi Krishna64b48612012-06-14 16:08:11 -0700531static void msm_hsic_clk_reset(struct msm_hsic_hcd *mehci)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530532{
533 int ret;
534
Manu Gautam5143b252012-01-05 19:25:23 -0800535 ret = clk_reset(mehci->core_clk, CLK_RESET_ASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530536 if (ret) {
Vamsi Krishna64b48612012-06-14 16:08:11 -0700537 dev_err(mehci->dev, "hsic clk assert failed:%d\n", ret);
538 return;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530539 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700540 clk_disable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530541
Manu Gautam5143b252012-01-05 19:25:23 -0800542 ret = clk_reset(mehci->core_clk, CLK_RESET_DEASSERT);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530543 if (ret)
Vamsi Krishna64b48612012-06-14 16:08:11 -0700544 dev_err(mehci->dev, "hsic clk deassert failed:%d\n", ret);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530545
Vamsi Krishna64b48612012-06-14 16:08:11 -0700546 usleep_range(10000, 12000);
547
548 clk_enable(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530549}
550
Vamsi Krishna64b48612012-06-14 16:08:11 -0700551#define HSIC_STROBE_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C0)
552#define HSIC_DATA_GPIO_PAD_CTL (MSM_TLMM_BASE+0x20C4)
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530553#define HSIC_CAL_PAD_CTL (MSM_TLMM_BASE+0x20C8)
554#define HSIC_LV_MODE 0x04
555#define HSIC_PAD_CALIBRATION 0xA8
556#define HSIC_GPIO_PAD_VAL 0x0A0AAA10
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530557#define LINK_RESET_TIMEOUT_USEC (250 * 1000)
558static int msm_hsic_reset(struct msm_hsic_hcd *mehci)
559{
560 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530561 int ret;
Lena Salman8c8ba382012-02-14 15:59:31 +0200562 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530563
Vamsi Krishna64b48612012-06-14 16:08:11 -0700564 msm_hsic_clk_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530565
Vamsi Krishna64b48612012-06-14 16:08:11 -0700566 /* select ulpi phy */
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530567 writel_relaxed(0x80000000, USB_PORTSC);
568
Vamsi Krishna64b48612012-06-14 16:08:11 -0700569 mb();
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530570
Lena Salman8c8ba382012-02-14 15:59:31 +0200571 /* HSIC init sequence when HSIC signals (Strobe/Data) are
572 routed via GPIOs */
573 if (pdata && pdata->strobe && pdata->data) {
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530574
Lena Salman8c8ba382012-02-14 15:59:31 +0200575 /* Enable LV_MODE in HSIC_CAL_PAD_CTL register */
576 writel_relaxed(HSIC_LV_MODE, HSIC_CAL_PAD_CTL);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530577
Vamsi Krishna64b48612012-06-14 16:08:11 -0700578 mb();
579
Lena Salman8c8ba382012-02-14 15:59:31 +0200580 /*set periodic calibration interval to ~2.048sec in
581 HSIC_IO_CAL_REG */
582 ulpi_write(mehci, 0xFF, 0x33);
583
584 /* Enable periodic IO calibration in HSIC_CFG register */
585 ulpi_write(mehci, HSIC_PAD_CALIBRATION, 0x30);
586
Vamsi Krishna64b48612012-06-14 16:08:11 -0700587 /* Configure GPIO pins for HSIC functionality mode */
Lena Salman8c8ba382012-02-14 15:59:31 +0200588 ret = msm_hsic_config_gpios(mehci, 1);
589 if (ret) {
590 dev_err(mehci->dev, " gpio configuarion failed\n");
591 return ret;
592 }
Vamsi Krishna64b48612012-06-14 16:08:11 -0700593 /* Set LV_MODE=0x1 and DCC=0x2 in HSIC_GPIO PAD_CTL register */
594 writel_relaxed(HSIC_GPIO_PAD_VAL, HSIC_STROBE_GPIO_PAD_CTL);
595 writel_relaxed(HSIC_GPIO_PAD_VAL, HSIC_DATA_GPIO_PAD_CTL);
596
597 mb();
598
Lena Salman8c8ba382012-02-14 15:59:31 +0200599 /* Enable HSIC mode in HSIC_CFG register */
600 ulpi_write(mehci, 0x01, 0x31);
601 } else {
602 /* HSIC init sequence when HSIC signals (Strobe/Data) are routed
603 via dedicated I/O */
604
605 /* programmable length of connect signaling (33.2ns) */
606 ret = ulpi_write(mehci, 3, HSIC_DBG1_REG);
607 if (ret) {
608 pr_err("%s: Unable to program length of connect "
609 "signaling\n", __func__);
610 }
611
612 /*set periodic calibration interval to ~2.048sec in
613 HSIC_IO_CAL_REG */
614 ulpi_write(mehci, 0xFF, 0x33);
615
616 /* Enable HSIC mode in HSIC_CFG register */
617 ulpi_write(mehci, 0xA9, 0x30);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530618 }
619
Hemant Kumar6fd65032012-05-23 13:02:24 -0700620 /*disable auto resume*/
621 ulpi_write(mehci, ULPI_IFC_CTRL_AUTORESUME, ULPI_CLR(ULPI_IFC_CTRL));
622
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530623 return 0;
624}
625
626#define PHY_SUSPEND_TIMEOUT_USEC (500 * 1000)
627#define PHY_RESUME_TIMEOUT_USEC (100 * 1000)
628
629#ifdef CONFIG_PM_SLEEP
630static int msm_hsic_suspend(struct msm_hsic_hcd *mehci)
631{
632 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530633 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530634 u32 val;
Amit Blayd6ea6102012-06-07 16:26:24 +0300635 int none_vol, max_vol;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530636
637 if (atomic_read(&mehci->in_lpm)) {
638 dev_dbg(mehci->dev, "%s called in lpm\n", __func__);
639 return 0;
640 }
641
642 disable_irq(hcd->irq);
Jack Phambe05fbb2012-05-16 10:56:26 -0700643
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700644 /* make sure we don't race against a remote wakeup */
645 if (test_bit(HCD_FLAG_WAKEUP_PENDING, &hcd->flags) ||
Jack Phambe05fbb2012-05-16 10:56:26 -0700646 readl_relaxed(USB_PORTSC) & PORT_RESUME) {
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700647 dev_dbg(mehci->dev, "wakeup pending, aborting suspend\n");
Jack Phambe05fbb2012-05-16 10:56:26 -0700648 enable_irq(hcd->irq);
649 return -EBUSY;
650 }
651
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530652 /*
653 * PHY may take some time or even fail to enter into low power
654 * mode (LPM). Hence poll for 500 msec and reset the PHY and link
655 * in failure case.
656 */
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700657 val = readl_relaxed(USB_PORTSC);
658 val &= ~PORT_RWC_BITS;
659 val |= PORTSC_PHCD;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530660 writel_relaxed(val, USB_PORTSC);
661 while (cnt < PHY_SUSPEND_TIMEOUT_USEC) {
662 if (readl_relaxed(USB_PORTSC) & PORTSC_PHCD)
663 break;
664 udelay(1);
665 cnt++;
666 }
667
668 if (cnt >= PHY_SUSPEND_TIMEOUT_USEC) {
669 dev_err(mehci->dev, "Unable to suspend PHY\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530670 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530671 msm_hsic_reset(mehci);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530672 }
673
674 /*
675 * PHY has capability to generate interrupt asynchronously in low
676 * power mode (LPM). This interrupt is level triggered. So USB IRQ
677 * line must be disabled till async interrupt enable bit is cleared
678 * in USBCMD register. Assert STP (ULPI interface STOP signal) to
Pavankumar Kondeti1c1d82b2013-01-28 21:37:59 +0530679 * block data communication from PHY. Enable asynchronous interrupt
680 * only when wakeup gpio IRQ is not present.
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530681 */
Pavankumar Kondeti1c1d82b2013-01-28 21:37:59 +0530682 if (mehci->wakeup_irq)
683 writel_relaxed(readl_relaxed(USB_USBCMD) |
684 ULPI_STP_CTRL, USB_USBCMD);
685 else
686 writel_relaxed(readl_relaxed(USB_USBCMD) | ASYNC_INTR_CTRL |
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530687 ULPI_STP_CTRL, USB_USBCMD);
688
689 /*
690 * Ensure that hardware is put in low power mode before
691 * clocks are turned OFF and VDD is allowed to minimize.
692 */
693 mb();
694
Manu Gautam28b1bac2012-01-30 16:43:06 +0530695 clk_disable_unprepare(mehci->core_clk);
696 clk_disable_unprepare(mehci->phy_clk);
697 clk_disable_unprepare(mehci->cal_clk);
698 clk_disable_unprepare(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530699
Amit Blayd6ea6102012-06-07 16:26:24 +0300700 none_vol = vdd_val[mehci->vdd_type][VDD_NONE];
701 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
702
703 ret = regulator_set_voltage(mehci->hsic_vddcx, none_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700704 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300705 dev_err(mehci->dev, "unable to set vddcx voltage for VDD MIN\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700706
Hemant Kumare6275972012-02-29 20:06:21 -0800707 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700708 mehci->bus_vote = false;
709 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800710 }
711
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530712 atomic_set(&mehci->in_lpm, 1);
713 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700714
715 mehci->wakeup_irq_enabled = 1;
716 enable_irq_wake(mehci->wakeup_irq);
717 enable_irq(mehci->wakeup_irq);
718
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700719 wake_unlock(&mehci->wlock);
720
Devin Kim476bbd72012-07-19 18:11:11 -0700721 dev_dbg(mehci->dev, "HSIC-USB in low power mode\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530722
723 return 0;
724}
725
726static int msm_hsic_resume(struct msm_hsic_hcd *mehci)
727{
728 struct usb_hcd *hcd = hsic_to_hcd(mehci);
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +0530729 int cnt = 0, ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530730 unsigned temp;
Amit Blayd6ea6102012-06-07 16:26:24 +0300731 int min_vol, max_vol;
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700732 unsigned long flags;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530733
734 if (!atomic_read(&mehci->in_lpm)) {
735 dev_dbg(mehci->dev, "%s called in !in_lpm\n", __func__);
736 return 0;
737 }
738
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530739 /* Handles race with Async interrupt */
740 disable_irq(hcd->irq);
741
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700742 spin_lock_irqsave(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700743 if (mehci->wakeup_irq_enabled) {
744 disable_irq_wake(mehci->wakeup_irq);
745 disable_irq_nosync(mehci->wakeup_irq);
746 mehci->wakeup_irq_enabled = 0;
747 }
Hemant Kumar4cd49e12012-09-06 19:57:14 -0700748 spin_unlock_irqrestore(&mehci->wakeup_lock, flags);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700749
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700750 wake_lock(&mehci->wlock);
751
Hemant Kumare6275972012-02-29 20:06:21 -0800752 if (mehci->bus_perf_client && debug_bus_voting_enabled) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700753 mehci->bus_vote = true;
754 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -0800755 }
756
Amit Blayd6ea6102012-06-07 16:26:24 +0300757 min_vol = vdd_val[mehci->vdd_type][VDD_MIN];
758 max_vol = vdd_val[mehci->vdd_type][VDD_MAX];
759
760 ret = regulator_set_voltage(mehci->hsic_vddcx, min_vol, max_vol);
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700761 if (ret < 0)
Amit Blayd6ea6102012-06-07 16:26:24 +0300762 dev_err(mehci->dev, "unable to set nominal vddcx voltage (no VDD MIN)\n");
Vamsi Krishna45d88fa2011-11-02 13:28:42 -0700763
Manu Gautam28b1bac2012-01-30 16:43:06 +0530764 clk_prepare_enable(mehci->core_clk);
765 clk_prepare_enable(mehci->phy_clk);
766 clk_prepare_enable(mehci->cal_clk);
767 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530768
769 temp = readl_relaxed(USB_USBCMD);
770 temp &= ~ASYNC_INTR_CTRL;
771 temp &= ~ULPI_STP_CTRL;
772 writel_relaxed(temp, USB_USBCMD);
773
774 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD))
775 goto skip_phy_resume;
776
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700777 temp = readl_relaxed(USB_PORTSC);
778 temp &= ~(PORT_RWC_BITS | PORTSC_PHCD);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530779 writel_relaxed(temp, USB_PORTSC);
780 while (cnt < PHY_RESUME_TIMEOUT_USEC) {
781 if (!(readl_relaxed(USB_PORTSC) & PORTSC_PHCD) &&
782 (readl_relaxed(USB_ULPI_VIEWPORT) & ULPI_SYNC_STATE))
783 break;
784 udelay(1);
785 cnt++;
786 }
787
788 if (cnt >= PHY_RESUME_TIMEOUT_USEC) {
789 /*
790 * This is a fatal error. Reset the link and
791 * PHY to make hsic working.
792 */
793 dev_err(mehci->dev, "Unable to resume USB. Reset the hsic\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +0530794 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530795 msm_hsic_reset(mehci);
796 }
797
798skip_phy_resume:
799
Hemant Kumar6fd65032012-05-23 13:02:24 -0700800 usb_hcd_resume_root_hub(hcd);
801
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530802 atomic_set(&mehci->in_lpm, 0);
803
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530804 if (atomic_read(&mehci->async_int)) {
805 atomic_set(&mehci->async_int, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530806 pm_runtime_put_noidle(mehci->dev);
Jack Phamdd5ad792012-07-26 10:31:03 -0700807 enable_irq(hcd->irq);
Hemant Kumar6fd65032012-05-23 13:02:24 -0700808 }
809
Ajay Dudaniab3bf192012-08-28 09:58:04 -0700810 if (atomic_read(&mehci->pm_usage_cnt)) {
811 atomic_set(&mehci->pm_usage_cnt, 0);
812 pm_runtime_put_noidle(mehci->dev);
813 }
Jack Phamdd5ad792012-07-26 10:31:03 -0700814
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530815 enable_irq(hcd->irq);
Devin Kim476bbd72012-07-19 18:11:11 -0700816 dev_dbg(mehci->dev, "HSIC-USB exited from low power mode\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530817
818 return 0;
819}
820#endif
821
Hemant Kumar2309eaa2012-08-14 16:46:42 -0700822static void ehci_hsic_bus_vote_w(struct work_struct *w)
823{
824 struct msm_hsic_hcd *mehci =
825 container_of(w, struct msm_hsic_hcd, bus_vote_w);
826 int ret;
827
828 ret = msm_bus_scale_client_update_request(mehci->bus_perf_client,
829 mehci->bus_vote);
830 if (ret)
831 dev_err(mehci->dev, "%s: Failed to vote for bus bandwidth %d\n",
832 __func__, ret);
833}
834
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530835static int msm_hsic_reset_done(struct usb_hcd *hcd)
836{
837 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
838 u32 __iomem *status_reg = &ehci->regs->port_status[0];
839 int ret;
840
841 ehci_writel(ehci, ehci_readl(ehci, status_reg) & ~(PORT_RWC_BITS |
842 PORT_RESET), status_reg);
843
844 ret = handshake(ehci, status_reg, PORT_RESET, 0, 1 * 1000);
845
846 if (ret)
847 pr_err("reset handshake failed in %s\n", __func__);
848 else
849 ehci_writel(ehci, ehci_readl(ehci, &ehci->regs->command) |
850 CMD_RUN, &ehci->regs->command);
851
852 return ret;
853}
854
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700855#define STS_GPTIMER0_INTERRUPT BIT(24)
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530856static irqreturn_t msm_hsic_irq(struct usb_hcd *hcd)
857{
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700858 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530859 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700860 u32 status;
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530861 int ret;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530862
863 if (atomic_read(&mehci->in_lpm)) {
Hemant Kumar3dbc5b32012-05-09 15:36:11 -0700864 dev_dbg(mehci->dev, "phy async intr\n");
Pavankumar Kondetibbd05822013-01-28 17:04:39 +0530865 dbg_log_event(NULL, "Async IRQ", 0);
866 ret = pm_runtime_get(mehci->dev);
867 if ((ret == 1) || (ret == -EINPROGRESS)) {
868 pm_runtime_put_noidle(mehci->dev);
869 } else {
870 disable_irq_nosync(hcd->irq);
871 atomic_set(&mehci->async_int, 1);
872 }
873
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530874 return IRQ_HANDLED;
875 }
876
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700877 status = ehci_readl(ehci, &ehci->regs->status);
878
879 if (status & STS_GPTIMER0_INTERRUPT) {
880 int timeleft;
881
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530882 dbg_log_event(NULL, "FPR: gpt0_isr", mehci->bus_reset);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700883
884 timeleft = GPT_CNT(ehci_readl(ehci,
885 &mehci->timer->gptimer1_ctrl));
Pavankumar Kondeti95933142013-10-10 22:39:15 +0530886 if (!mehci->bus_reset) {
887 if (ktime_us_delta(ktime_get(), mehci->resume_start_t) >
888 RESUME_SIGNAL_TIME_SOF_USEC) {
889 dbg_log_event(NULL, "FPR: GPT prog invalid",
890 timeleft);
891 pr_err("HSIC GPT timer prog invalid\n");
892 timeleft = 0;
893 }
894 }
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700895 if (timeleft) {
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530896 if (mehci->bus_reset) {
897 ret = msm_hsic_reset_done(hcd);
898 if (ret) {
899 mehci->reset_again = 1;
900 dbg_log_event(NULL, "RESET: fail", 0);
901 }
902 } else {
Pavankumar Kondeti95933142013-10-10 22:39:15 +0530903 writel_relaxed(readl_relaxed(
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530904 &ehci->regs->command) | CMD_RUN,
905 &ehci->regs->command);
Pavankumar Kondeti95933142013-10-10 22:39:15 +0530906 if (ktime_us_delta(ktime_get(),
907 mehci->resume_start_t) >
908 RESUME_SIGNAL_TIME_SOF_USEC) {
909 dbg_log_event(NULL,
910 "FPR: resume prog invalid",
911 timeleft);
912 pr_err("HSIC resume fail. retrying\n");
913 mehci->resume_again = 1;
914 }
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530915 }
916 } else {
917 if (mehci->bus_reset)
918 mehci->reset_again = 1;
919 else
920 mehci->resume_again = 1;
921 }
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700922
923 dbg_log_event(NULL, "FPR: timeleft", timeleft);
924
925 complete(&mehci->gpt0_completion);
926 ehci_writel(ehci, STS_GPTIMER0_INTERRUPT, &ehci->regs->status);
927 }
928
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530929 return ehci_irq(hcd);
930}
931
932static int ehci_hsic_reset(struct usb_hcd *hcd)
933{
934 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700935 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530936 int retval;
937
Ajay Dudani11ab1d52012-08-17 17:12:26 -0700938 mehci->timer = USB_HS_GPTIMER_BASE;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530939 ehci->caps = USB_CAPLENGTH;
940 ehci->regs = USB_CAPLENGTH +
941 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
942 dbg_hcs_params(ehci, "reset");
943 dbg_hcc_params(ehci, "reset");
944
945 /* cache the data to minimize the chip reads*/
946 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
947
948 hcd->has_tt = 1;
949 ehci->sbrn = HCD_USB2;
950
951 retval = ehci_halt(ehci);
952 if (retval)
953 return retval;
954
955 /* data structure init */
956 retval = ehci_init(hcd);
957 if (retval)
958 return retval;
959
960 retval = ehci_reset(ehci);
961 if (retval)
962 return retval;
963
964 /* bursts of unspecified length. */
965 writel_relaxed(0, USB_AHBBURST);
966 /* Use the AHB transactor */
Vijayavardhan Vennapusa5f32d7a2012-03-14 16:30:26 +0530967 writel_relaxed(0x08, USB_AHBMODE);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +0530968 /* Disable streaming mode and select host mode */
969 writel_relaxed(0x13, USB_USBMODE);
970
971 ehci_port_power(ehci, 1);
972 return 0;
973}
974
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530975#define RESET_RETRY_LIMIT 3
976#define RESET_SIGNAL_TIME_SOF_USEC (50 * 1000)
977#define RESET_SIGNAL_TIME_USEC (20 * 1000)
978static void ehci_hsic_reset_sof_bug_handler(struct usb_hcd *hcd, u32 val)
979{
980 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
981 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
982 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
983 u32 __iomem *status_reg = &ehci->regs->port_status[0];
Pavankumar Kondeti37493632013-03-05 09:43:22 +0530984 u32 cmd;
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530985 unsigned long flags;
986 int retries = 0, ret, cnt = RESET_SIGNAL_TIME_USEC;
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +0530987 s32 next_latency = 0;
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530988
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +0530989 if (pdata && pdata->swfi_latency) {
990 next_latency = pdata->swfi_latency + 1;
991 pm_qos_update_request(&mehci->pm_qos_req_dma, next_latency);
992 next_latency = PM_QOS_DEFAULT_VALUE;
993 }
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +0530994
995 mehci->bus_reset = 1;
Pavankumar Kondeti37493632013-03-05 09:43:22 +0530996
997 /* Halt the controller */
998 cmd = ehci_readl(ehci, &ehci->regs->command);
999 cmd &= ~CMD_RUN;
1000 ehci_writel(ehci, cmd, &ehci->regs->command);
1001 ret = handshake(ehci, &ehci->regs->status, STS_HALT,
1002 STS_HALT, 16 * 125);
1003 if (ret) {
1004 pr_err("halt handshake fatal error\n");
1005 dbg_log_event(NULL, "HALT: fatal", 0);
1006 goto fail;
1007 }
1008
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +05301009retry:
1010 retries++;
1011 dbg_log_event(NULL, "RESET: start", retries);
1012 pr_debug("reset begin %d\n", retries);
1013 mehci->reset_again = 0;
1014 spin_lock_irqsave(&ehci->lock, flags);
1015 ehci_writel(ehci, val, status_reg);
1016 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_USEC - 1),
1017 &mehci->timer->gptimer0_ld);
1018 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1019 &mehci->timer->gptimer0_ctrl);
1020 ehci_writel(ehci, INTR_MASK | STS_GPTIMER0_INTERRUPT,
1021 &ehci->regs->intr_enable);
1022
1023 ehci_writel(ehci, GPT_LD(RESET_SIGNAL_TIME_SOF_USEC - 1),
1024 &mehci->timer->gptimer1_ld);
1025 ehci_writel(ehci, GPT_RESET | GPT_RUN,
1026 &mehci->timer->gptimer1_ctrl);
1027
1028 spin_unlock_irqrestore(&ehci->lock, flags);
1029 wait_for_completion(&mehci->gpt0_completion);
1030
1031 if (!mehci->reset_again)
1032 goto done;
1033
1034 if (handshake(ehci, status_reg, PORT_RESET, 0, 10 * 1000)) {
1035 pr_err("reset handshake fatal error\n");
1036 dbg_log_event(NULL, "RESET: fatal", retries);
1037 goto fail;
1038 }
1039
1040 if (retries < RESET_RETRY_LIMIT)
1041 goto retry;
1042
1043 /* complete reset in tight loop */
1044 pr_info("RESET in tight loop\n");
1045 dbg_log_event(NULL, "RESET: tight", 0);
1046
1047 spin_lock_irqsave(&ehci->lock, flags);
1048 ehci_writel(ehci, val, status_reg);
1049 while (cnt--)
1050 udelay(1);
1051 ret = msm_hsic_reset_done(hcd);
1052 spin_unlock_irqrestore(&ehci->lock, flags);
1053 if (ret) {
1054 pr_err("RESET in tight loop failed\n");
1055 dbg_log_event(NULL, "RESET: tight failed", 0);
1056 goto fail;
1057 }
1058
1059done:
1060 dbg_log_event(NULL, "RESET: done", retries);
1061 pr_debug("reset completed\n");
1062fail:
1063 mehci->bus_reset = 0;
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301064 if (next_latency)
1065 pm_qos_update_request(&mehci->pm_qos_req_dma, next_latency);
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +05301066}
1067
Hemant Kumar45d211b2012-05-31 17:58:43 -07001068static int ehci_hsic_bus_suspend(struct usb_hcd *hcd)
1069{
Pavankumar Kondeti3c137392012-09-14 14:02:36 +05301070 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1071
1072 if (!(readl_relaxed(USB_PORTSC) & PORT_PE)) {
1073 dbg_log_event(NULL, "RH suspend attempt failed", 0);
1074 dev_dbg(mehci->dev, "%s:port is not enabled skip suspend\n",
1075 __func__);
1076 return -EAGAIN;
1077 }
1078
Hemant Kumar45d211b2012-05-31 17:58:43 -07001079 dbg_log_event(NULL, "Suspend RH", 0);
1080 return ehci_bus_suspend(hcd);
1081}
1082
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001083static int msm_hsic_resume_thread(void *data)
1084{
1085 struct msm_hsic_hcd *mehci = data;
1086 struct usb_hcd *hcd = hsic_to_hcd(mehci);
1087 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1088 u32 temp;
1089 unsigned long resume_needed = 0;
1090 int retry_cnt = 0;
1091 int tight_resume = 0;
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301092 int tight_count = 0;
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301093 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301094 s32 next_latency = 0;
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001095
1096 dbg_log_event(NULL, "Resume RH", 0);
1097
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301098 if (pdata && pdata->swfi_latency) {
1099 next_latency = pdata->swfi_latency + 1;
1100 pm_qos_update_request(&mehci->pm_qos_req_dma, next_latency);
1101 next_latency = PM_QOS_DEFAULT_VALUE;
1102 }
1103
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001104 /* keep delay between bus states */
Pavankumar Kondetibed9ef72013-08-01 11:30:32 +05301105 if (time_before_eq(jiffies, ehci->next_statechange))
1106 usleep_range(10000, 10000);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001107
1108 spin_lock_irq(&ehci->lock);
1109 if (!HCD_HW_ACCESSIBLE(hcd)) {
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001110 mehci->resume_status = -ESHUTDOWN;
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301111 goto exit;
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001112 }
1113
1114 if (unlikely(ehci->debug)) {
1115 if (!dbgp_reset_prep())
1116 ehci->debug = NULL;
1117 else
1118 dbgp_external_startup();
1119 }
1120
1121 /* at least some APM implementations will try to deliver
1122 * IRQs right away, so delay them until we're ready.
1123 */
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301124 writel_relaxed(0, &ehci->regs->intr_enable);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001125
1126 /* re-init operational registers */
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301127 writel_relaxed(0, &ehci->regs->segment);
1128 writel_relaxed(ehci->periodic_dma, &ehci->regs->frame_list);
1129 writel_relaxed((u32) ehci->async->qh_dma, &ehci->regs->async_next);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001130
1131 /*CMD_RUN will be set after, PORT_RESUME gets cleared*/
1132 if (ehci->resume_sof_bug)
1133 ehci->command &= ~CMD_RUN;
1134
1135 /* restore CMD_RUN, framelist size, and irq threshold */
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301136 writel_relaxed(ehci->command, &ehci->regs->command);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001137
1138 /* manually resume the ports we suspended during bus_suspend() */
1139resume_again:
1140 if (retry_cnt >= RESUME_RETRY_LIMIT) {
1141 pr_info("retry count(%d) reached max, resume in tight loop\n",
1142 retry_cnt);
1143 tight_resume = 1;
1144 }
1145
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301146 temp = readl_relaxed(&ehci->regs->port_status[0]);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001147 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
1148 if (test_bit(0, &ehci->bus_suspended) && (temp & PORT_SUSPEND)) {
1149 temp |= PORT_RESUME;
1150 set_bit(0, &resume_needed);
1151 }
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301152 mehci->resume_start_t = ktime_get();
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001153 dbg_log_event(NULL, "FPR: Set", temp);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301154 writel_relaxed(temp, &ehci->regs->port_status[0]);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001155
1156 /* HSIC controller has a h/w bug due to which it can try to send SOFs
1157 * (start of frames) during port resume resulting in phy lockup. HSIC hw
1158 * controller in MSM clears FPR bit after driving the resume signal for
1159 * 20ms. Workaround is to stop SOFs before driving resume and then start
1160 * sending SOFs immediately. Need to send SOFs within 3ms of resume
1161 * completion otherwise peripheral may enter undefined state. As
1162 * usleep_range does not gurantee exact sleep time, GPTimer is used to
1163 * to time the resume sequence. If driver exceeds allowable time SOFs,
1164 * repeat the resume process.
1165 */
1166 if (ehci->resume_sof_bug && resume_needed) {
1167 if (!tight_resume) {
1168 mehci->resume_again = 0;
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301169 writel_relaxed(GPT_LD(RESUME_SIGNAL_TIME_USEC - 1),
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001170 &mehci->timer->gptimer0_ld);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301171 writel_relaxed(GPT_RESET | GPT_RUN,
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001172 &mehci->timer->gptimer0_ctrl);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301173 writel_relaxed(INTR_MASK | STS_GPTIMER0_INTERRUPT,
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001174 &ehci->regs->intr_enable);
1175
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301176 writel_relaxed(GPT_LD(RESUME_SIGNAL_TIME_SOF_USEC - 1),
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001177 &mehci->timer->gptimer1_ld);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301178 writel_relaxed(GPT_RESET | GPT_RUN,
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001179 &mehci->timer->gptimer1_ctrl);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301180 dbg_log_event(NULL, "GPT timer prog done", 0);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001181
1182 spin_unlock_irq(&ehci->lock);
1183 wait_for_completion(&mehci->gpt0_completion);
1184 spin_lock_irq(&ehci->lock);
1185 } else {
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301186 dbg_log_event(NULL, "FPR: Tightloop", tight_count);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001187 /* do the resume in a tight loop */
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301188 mdelay(22);
1189 writel_relaxed(readl_relaxed(&ehci->regs->command) |
1190 CMD_RUN, &ehci->regs->command);
1191 if (ktime_us_delta(ktime_get(), mehci->resume_start_t) >
1192 RESUME_SIGNAL_TIME_SOF_USEC) {
1193 dbg_log_event(NULL, "FPR: Tightloop fail", 0);
1194 if (++tight_count > 3) {
1195 pr_err("HSIC resume failed\n");
1196 mehci->resume_status = -ENODEV;
1197 goto exit;
1198 }
1199 pr_err("FPR tight loop fail %d\n", tight_count);
1200 mehci->resume_again = 1;
1201 } else {
1202 dbg_log_event(NULL, "FPR: Tightloop done", 0);
1203 }
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001204 }
1205
1206 if (mehci->resume_again) {
1207 int temp;
1208
1209 dbg_log_event(NULL, "FPR: Re-Resume", retry_cnt);
1210 pr_info("FPR: retry count: %d\n", retry_cnt);
1211 spin_unlock_irq(&ehci->lock);
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301212 temp = readl_relaxed(&ehci->regs->command);
1213 if (temp & CMD_RUN) {
1214 temp &= ~CMD_RUN;
1215 writel_relaxed(temp, &ehci->regs->command);
1216 dbg_log_event(NULL, "FPR: R/S cleared", 0);
1217 }
1218 temp = readl_relaxed(&ehci->regs->port_status[0]);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001219 temp &= ~PORT_RWC_BITS;
1220 temp |= PORT_SUSPEND;
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301221 writel_relaxed(temp, &ehci->regs->port_status[0]);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001222 /* Keep the bus idle for 5ms so that peripheral
1223 * can detect and initiate suspend
1224 */
1225 usleep_range(5000, 5000);
1226 dbg_log_event(NULL,
1227 "FPR: RResume",
1228 ehci_readl(ehci, &ehci->regs->port_status[0]));
1229 spin_lock_irq(&ehci->lock);
1230 mehci->resume_again = 0;
1231 retry_cnt++;
1232 goto resume_again;
1233 }
1234 }
1235
Pavankumar Kondeti95933142013-10-10 22:39:15 +05301236 ehci->command |= CMD_RUN;
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001237 dbg_log_event(NULL, "FPR: RT-Done", 0);
1238 mehci->resume_status = 1;
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301239exit:
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001240 spin_unlock_irq(&ehci->lock);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001241 complete(&mehci->rt_completion);
Pavankumar Kondeti8ea50d52013-07-22 15:32:17 +05301242 if (next_latency)
1243 pm_qos_update_request(&mehci->pm_qos_req_dma, next_latency);
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001244
1245 return 0;
1246}
1247
Hemant Kumar45d211b2012-05-31 17:58:43 -07001248static int ehci_hsic_bus_resume(struct usb_hcd *hcd)
1249{
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001250 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1251 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
1252 u32 temp;
1253 struct task_struct *resume_thread = NULL;
1254
1255 mehci->resume_status = 0;
1256 resume_thread = kthread_run(msm_hsic_resume_thread,
1257 mehci, "hsic_resume_thread");
1258 if (IS_ERR(resume_thread)) {
1259 pr_err("Error creating resume thread:%lu\n",
1260 PTR_ERR(resume_thread));
1261 return PTR_ERR(resume_thread);
1262 }
1263
1264 wait_for_completion(&mehci->rt_completion);
1265
1266 if (mehci->resume_status < 0)
1267 return mehci->resume_status;
1268
1269 dbg_log_event(NULL, "FPR: Wokeup", 0);
1270 spin_lock_irq(&ehci->lock);
1271 (void) ehci_readl(ehci, &ehci->regs->command);
1272
1273 temp = 0;
1274 if (ehci->async->qh_next.qh)
1275 temp |= CMD_ASE;
1276 if (ehci->periodic_sched)
1277 temp |= CMD_PSE;
1278 if (temp) {
1279 ehci->command |= temp;
1280 ehci_writel(ehci, ehci->command, &ehci->regs->command);
1281 }
1282
1283 ehci->next_statechange = jiffies + msecs_to_jiffies(5);
1284 hcd->state = HC_STATE_RUNNING;
1285 ehci->rh_state = EHCI_RH_RUNNING;
1286
1287 /* Now we can safely re-enable irqs */
1288 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
1289
1290 spin_unlock_irq(&ehci->lock);
1291
1292 return 0;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001293}
1294
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301295static struct hc_driver msm_hsic_driver = {
1296 .description = hcd_name,
1297 .product_desc = "Qualcomm EHCI Host Controller using HSIC",
1298 .hcd_priv_size = sizeof(struct msm_hsic_hcd),
1299
1300 /*
1301 * generic hardware linkage
1302 */
1303 .irq = msm_hsic_irq,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001304 .flags = HCD_USB2 | HCD_MEMORY | HCD_OLD_ENUM,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301305
1306 .reset = ehci_hsic_reset,
1307 .start = ehci_run,
1308
1309 .stop = ehci_stop,
1310 .shutdown = ehci_shutdown,
1311
1312 /*
1313 * managing i/o requests and associated device resources
1314 */
Hemant Kumardf2d84d2012-08-15 09:06:35 -07001315 .urb_enqueue = ehci_urb_enqueue,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301316 .urb_dequeue = ehci_urb_dequeue,
1317 .endpoint_disable = ehci_endpoint_disable,
1318 .endpoint_reset = ehci_endpoint_reset,
1319 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
1320
1321 /*
1322 * scheduling support
1323 */
1324 .get_frame_number = ehci_get_frame,
1325
1326 /*
1327 * root hub support
1328 */
1329 .hub_status_data = ehci_hub_status_data,
1330 .hub_control = ehci_hub_control,
1331 .relinquish_port = ehci_relinquish_port,
1332 .port_handed_over = ehci_port_handed_over,
1333
1334 /*
1335 * PM support
1336 */
Hemant Kumar45d211b2012-05-31 17:58:43 -07001337 .bus_suspend = ehci_hsic_bus_suspend,
1338 .bus_resume = ehci_hsic_bus_resume,
1339
Hemant Kumardf2d84d2012-08-15 09:06:35 -07001340 .log_urb = dbg_log_event,
Hemant Kumar105d07f2012-07-02 15:33:07 -07001341 .dump_regs = dump_hsic_regs,
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001342
Pavankumar Kondeti9ecc39c2013-01-29 14:41:58 +05301343 .reset_sof_bug_handler = ehci_hsic_reset_sof_bug_handler,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301344};
1345
1346static int msm_hsic_init_clocks(struct msm_hsic_hcd *mehci, u32 init)
1347{
1348 int ret = 0;
1349
1350 if (!init)
1351 goto put_clocks;
1352
Lena Salman8c8ba382012-02-14 15:59:31 +02001353 /*core_clk is required for LINK protocol engine
1354 *clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001355 mehci->core_clk = clk_get(mehci->dev, "core_clk");
1356 if (IS_ERR(mehci->core_clk)) {
1357 dev_err(mehci->dev, "failed to get core_clk\n");
1358 ret = PTR_ERR(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301359 return ret;
1360 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301361
Lena Salman8c8ba382012-02-14 15:59:31 +02001362 /* alt_core_clk is for LINK to be used during PHY RESET
1363 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001364 mehci->alt_core_clk = clk_get(mehci->dev, "alt_core_clk");
1365 if (IS_ERR(mehci->alt_core_clk)) {
1366 dev_err(mehci->dev, "failed to core_clk\n");
1367 ret = PTR_ERR(mehci->alt_core_clk);
1368 goto put_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301369 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301370
Lena Salman8c8ba382012-02-14 15:59:31 +02001371 /* phy_clk is required for HSIC PHY operation
1372 * clock rate appropriately set by target specific clock driver */
Manu Gautam5143b252012-01-05 19:25:23 -08001373 mehci->phy_clk = clk_get(mehci->dev, "phy_clk");
1374 if (IS_ERR(mehci->phy_clk)) {
1375 dev_err(mehci->dev, "failed to get phy_clk\n");
1376 ret = PTR_ERR(mehci->phy_clk);
1377 goto put_alt_core_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301378 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301379
1380 /* 10MHz cal_clk is required for calibration of I/O pads */
Manu Gautam5143b252012-01-05 19:25:23 -08001381 mehci->cal_clk = clk_get(mehci->dev, "cal_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301382 if (IS_ERR(mehci->cal_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001383 dev_err(mehci->dev, "failed to get cal_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301384 ret = PTR_ERR(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001385 goto put_phy_clk;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301386 }
1387 clk_set_rate(mehci->cal_clk, 10000000);
1388
1389 /* ahb_clk is required for data transfers */
Manu Gautam5143b252012-01-05 19:25:23 -08001390 mehci->ahb_clk = clk_get(mehci->dev, "iface_clk");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301391 if (IS_ERR(mehci->ahb_clk)) {
Manu Gautam5143b252012-01-05 19:25:23 -08001392 dev_err(mehci->dev, "failed to get iface_clk\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301393 ret = PTR_ERR(mehci->ahb_clk);
1394 goto put_cal_clk;
1395 }
1396
Manu Gautam28b1bac2012-01-30 16:43:06 +05301397 clk_prepare_enable(mehci->core_clk);
1398 clk_prepare_enable(mehci->phy_clk);
1399 clk_prepare_enable(mehci->cal_clk);
1400 clk_prepare_enable(mehci->ahb_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301401
1402 return 0;
1403
1404put_clocks:
Jack Phamfd193eb2012-02-22 15:38:08 -08001405 if (!atomic_read(&mehci->in_lpm)) {
1406 clk_disable_unprepare(mehci->core_clk);
1407 clk_disable_unprepare(mehci->phy_clk);
1408 clk_disable_unprepare(mehci->cal_clk);
1409 clk_disable_unprepare(mehci->ahb_clk);
1410 }
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301411 clk_put(mehci->ahb_clk);
1412put_cal_clk:
1413 clk_put(mehci->cal_clk);
Manu Gautam5143b252012-01-05 19:25:23 -08001414put_phy_clk:
1415 clk_put(mehci->phy_clk);
1416put_alt_core_clk:
1417 clk_put(mehci->alt_core_clk);
1418put_core_clk:
1419 clk_put(mehci->core_clk);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301420
1421 return ret;
1422}
Vamsi Krishna34f01582011-12-14 19:54:42 -08001423static irqreturn_t hsic_peripheral_status_change(int irq, void *dev_id)
1424{
1425 struct msm_hsic_hcd *mehci = dev_id;
1426
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001427 pr_debug("%s: mehci:%p dev_id:%p\n", __func__, mehci, dev_id);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001428
1429 if (mehci)
1430 msm_hsic_config_gpios(mehci, 0);
1431
1432 return IRQ_HANDLED;
1433}
1434
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001435static irqreturn_t msm_hsic_wakeup_irq(int irq, void *data)
1436{
1437 struct msm_hsic_hcd *mehci = data;
Ajay Dudanid666daf2012-09-27 12:04:12 +05301438 int ret;
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001439
Hemant Kumar6fd65032012-05-23 13:02:24 -07001440 mehci->wakeup_int_cnt++;
Hemant Kumar45d211b2012-05-31 17:58:43 -07001441 dbg_log_event(NULL, "Remote Wakeup IRQ", mehci->wakeup_int_cnt);
Hemant Kumar6fd65032012-05-23 13:02:24 -07001442 dev_dbg(mehci->dev, "%s: hsic remote wakeup interrupt cnt: %u\n",
1443 __func__, mehci->wakeup_int_cnt);
1444
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001445 wake_lock(&mehci->wlock);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001446
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001447 spin_lock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001448 if (mehci->wakeup_irq_enabled) {
1449 mehci->wakeup_irq_enabled = 0;
1450 disable_irq_wake(irq);
1451 disable_irq_nosync(irq);
1452 }
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001453 spin_unlock(&mehci->wakeup_lock);
Jack Phamfe441ea2012-03-23 17:03:15 -07001454
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001455 if (!atomic_read(&mehci->pm_usage_cnt)) {
Ajay Dudanid666daf2012-09-27 12:04:12 +05301456 ret = pm_runtime_get(mehci->dev);
1457 /*
1458 * HSIC runtime resume can race with us.
1459 * if we are active (ret == 1) or resuming
1460 * (ret == -EINPROGRESS), decrement the
1461 * PM usage counter before returning.
1462 */
1463 if ((ret == 1) || (ret == -EINPROGRESS))
1464 pm_runtime_put_noidle(mehci->dev);
1465 else
1466 atomic_set(&mehci->pm_usage_cnt, 1);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001467 }
1468
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001469 return IRQ_HANDLED;
1470}
1471
Hemant Kumare6275972012-02-29 20:06:21 -08001472static int ehci_hsic_msm_bus_show(struct seq_file *s, void *unused)
1473{
1474 if (debug_bus_voting_enabled)
1475 seq_printf(s, "enabled\n");
1476 else
1477 seq_printf(s, "disabled\n");
1478
1479 return 0;
1480}
1481
1482static int ehci_hsic_msm_bus_open(struct inode *inode, struct file *file)
1483{
1484 return single_open(file, ehci_hsic_msm_bus_show, inode->i_private);
1485}
1486
1487static ssize_t ehci_hsic_msm_bus_write(struct file *file,
1488 const char __user *ubuf, size_t count, loff_t *ppos)
1489{
1490 char buf[8];
1491 int ret;
1492 struct seq_file *s = file->private_data;
1493 struct msm_hsic_hcd *mehci = s->private;
1494
1495 memset(buf, 0x00, sizeof(buf));
1496
1497 if (copy_from_user(&buf, ubuf, min_t(size_t, sizeof(buf) - 1, count)))
1498 return -EFAULT;
1499
1500 if (!strncmp(buf, "enable", 6)) {
1501 /* Do not vote here. Let hsic driver decide when to vote */
1502 debug_bus_voting_enabled = true;
1503 } else {
1504 debug_bus_voting_enabled = false;
1505 if (mehci->bus_perf_client) {
1506 ret = msm_bus_scale_client_update_request(
1507 mehci->bus_perf_client, 0);
1508 if (ret)
1509 dev_err(mehci->dev, "%s: Failed to devote "
1510 "for bus bw %d\n", __func__, ret);
1511 }
1512 }
1513
1514 return count;
1515}
1516
1517const struct file_operations ehci_hsic_msm_bus_fops = {
1518 .open = ehci_hsic_msm_bus_open,
1519 .read = seq_read,
1520 .write = ehci_hsic_msm_bus_write,
1521 .llseek = seq_lseek,
1522 .release = single_release,
1523};
1524
Hemant Kumar6fd65032012-05-23 13:02:24 -07001525static int ehci_hsic_msm_wakeup_cnt_show(struct seq_file *s, void *unused)
1526{
1527 struct msm_hsic_hcd *mehci = s->private;
1528
1529 seq_printf(s, "%u\n", mehci->wakeup_int_cnt);
1530
1531 return 0;
1532}
1533
1534static int ehci_hsic_msm_wakeup_cnt_open(struct inode *inode, struct file *f)
1535{
1536 return single_open(f, ehci_hsic_msm_wakeup_cnt_show, inode->i_private);
1537}
1538
1539const struct file_operations ehci_hsic_msm_wakeup_cnt_fops = {
1540 .open = ehci_hsic_msm_wakeup_cnt_open,
1541 .read = seq_read,
1542 .llseek = seq_lseek,
1543 .release = single_release,
1544};
1545
Hemant Kumar45d211b2012-05-31 17:58:43 -07001546static int ehci_hsic_msm_data_events_show(struct seq_file *s, void *unused)
1547{
1548 unsigned long flags;
1549 unsigned i;
1550
1551 read_lock_irqsave(&dbg_hsic_data.lck, flags);
1552
1553 i = dbg_hsic_data.idx;
1554 for (dbg_inc(&i); i != dbg_hsic_data.idx; dbg_inc(&i)) {
1555 if (!strnlen(dbg_hsic_data.buf[i], DBG_MSG_LEN))
1556 continue;
1557 seq_printf(s, "%s\n", dbg_hsic_data.buf[i]);
1558 }
1559
1560 read_unlock_irqrestore(&dbg_hsic_data.lck, flags);
1561
1562 return 0;
1563}
1564
1565static int ehci_hsic_msm_data_events_open(struct inode *inode, struct file *f)
1566{
1567 return single_open(f, ehci_hsic_msm_data_events_show, inode->i_private);
1568}
1569
1570const struct file_operations ehci_hsic_msm_dbg_data_fops = {
1571 .open = ehci_hsic_msm_data_events_open,
1572 .read = seq_read,
1573 .llseek = seq_lseek,
1574 .release = single_release,
1575};
1576
1577static int ehci_hsic_msm_ctrl_events_show(struct seq_file *s, void *unused)
1578{
1579 unsigned long flags;
1580 unsigned i;
1581
1582 read_lock_irqsave(&dbg_hsic_ctrl.lck, flags);
1583
1584 i = dbg_hsic_ctrl.idx;
1585 for (dbg_inc(&i); i != dbg_hsic_ctrl.idx; dbg_inc(&i)) {
1586 if (!strnlen(dbg_hsic_ctrl.buf[i], DBG_MSG_LEN))
1587 continue;
1588 seq_printf(s, "%s\n", dbg_hsic_ctrl.buf[i]);
1589 }
1590
1591 read_unlock_irqrestore(&dbg_hsic_ctrl.lck, flags);
1592
1593 return 0;
1594}
1595
1596static int ehci_hsic_msm_ctrl_events_open(struct inode *inode, struct file *f)
1597{
1598 return single_open(f, ehci_hsic_msm_ctrl_events_show, inode->i_private);
1599}
1600
1601const struct file_operations ehci_hsic_msm_dbg_ctrl_fops = {
1602 .open = ehci_hsic_msm_ctrl_events_open,
1603 .read = seq_read,
1604 .llseek = seq_lseek,
1605 .release = single_release,
1606};
1607
Hemant Kumare6275972012-02-29 20:06:21 -08001608static struct dentry *ehci_hsic_msm_dbg_root;
1609static int ehci_hsic_msm_debugfs_init(struct msm_hsic_hcd *mehci)
1610{
1611 struct dentry *ehci_hsic_msm_dentry;
1612
1613 ehci_hsic_msm_dbg_root = debugfs_create_dir("ehci_hsic_msm_dbg", NULL);
1614
1615 if (!ehci_hsic_msm_dbg_root || IS_ERR(ehci_hsic_msm_dbg_root))
1616 return -ENODEV;
1617
1618 ehci_hsic_msm_dentry = debugfs_create_file("bus_voting",
1619 S_IRUGO | S_IWUSR,
1620 ehci_hsic_msm_dbg_root, mehci,
1621 &ehci_hsic_msm_bus_fops);
1622
1623 if (!ehci_hsic_msm_dentry) {
1624 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1625 return -ENODEV;
1626 }
1627
Hemant Kumar6fd65032012-05-23 13:02:24 -07001628 ehci_hsic_msm_dentry = debugfs_create_file("wakeup_cnt",
1629 S_IRUGO,
1630 ehci_hsic_msm_dbg_root, mehci,
1631 &ehci_hsic_msm_wakeup_cnt_fops);
1632
1633 if (!ehci_hsic_msm_dentry) {
1634 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1635 return -ENODEV;
1636 }
1637
Hemant Kumar45d211b2012-05-31 17:58:43 -07001638 ehci_hsic_msm_dentry = debugfs_create_file("show_ctrl_events",
1639 S_IRUGO,
1640 ehci_hsic_msm_dbg_root, mehci,
1641 &ehci_hsic_msm_dbg_ctrl_fops);
1642
1643 if (!ehci_hsic_msm_dentry) {
1644 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1645 return -ENODEV;
1646 }
1647
1648 ehci_hsic_msm_dentry = debugfs_create_file("show_data_events",
1649 S_IRUGO,
1650 ehci_hsic_msm_dbg_root, mehci,
1651 &ehci_hsic_msm_dbg_data_fops);
1652
1653 if (!ehci_hsic_msm_dentry) {
1654 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1655 return -ENODEV;
1656 }
1657
Hemant Kumare6275972012-02-29 20:06:21 -08001658 return 0;
1659}
1660
1661static void ehci_hsic_msm_debugfs_cleanup(void)
1662{
1663 debugfs_remove_recursive(ehci_hsic_msm_dbg_root);
1664}
1665
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301666static int __devinit ehci_hsic_msm_probe(struct platform_device *pdev)
1667{
1668 struct usb_hcd *hcd;
1669 struct resource *res;
1670 struct msm_hsic_hcd *mehci;
Vijayavardhan Vennapusa2b592824f2011-11-02 19:51:32 +05301671 struct msm_hsic_host_platform_data *pdata;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301672 int ret;
1673
1674 dev_dbg(&pdev->dev, "ehci_msm-hsic probe\n");
1675
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301676 /* After parent device's probe is executed, it will be put in suspend
1677 * mode. When child device's probe is called, driver core is not
1678 * resuming parent device due to which parent will be in suspend even
1679 * though child is active. Hence resume the parent device explicitly.
1680 */
1681 if (pdev->dev.parent)
1682 pm_runtime_get_sync(pdev->dev.parent);
1683
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301684 hcd = usb_create_hcd(&msm_hsic_driver, &pdev->dev,
1685 dev_name(&pdev->dev));
1686 if (!hcd) {
1687 dev_err(&pdev->dev, "Unable to create HCD\n");
1688 return -ENOMEM;
1689 }
1690
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301691 hcd_to_bus(hcd)->skip_resume = true;
1692
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301693 hcd->irq = platform_get_irq(pdev, 0);
1694 if (hcd->irq < 0) {
1695 dev_err(&pdev->dev, "Unable to get IRQ resource\n");
1696 ret = hcd->irq;
1697 goto put_hcd;
1698 }
1699
1700 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1701 if (!res) {
1702 dev_err(&pdev->dev, "Unable to get memory resource\n");
1703 ret = -ENODEV;
1704 goto put_hcd;
1705 }
1706
1707 hcd->rsrc_start = res->start;
1708 hcd->rsrc_len = resource_size(res);
1709 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
1710 if (!hcd->regs) {
1711 dev_err(&pdev->dev, "ioremap failed\n");
1712 ret = -ENOMEM;
1713 goto put_hcd;
1714 }
1715
1716 mehci = hcd_to_hsic(hcd);
1717 mehci->dev = &pdev->dev;
Ajay Dudanic4e40db2012-08-20 14:44:40 -07001718 pdata = mehci->dev->platform_data;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301719
Hemant Kumar4cd49e12012-09-06 19:57:14 -07001720 spin_lock_init(&mehci->wakeup_lock);
1721
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001722 mehci->ehci.susp_sof_bug = 1;
Vamsi Krishna8e6edcb2012-06-20 18:08:50 -07001723 mehci->ehci.reset_sof_bug = 1;
Hemant Kumar38ce5d82012-05-29 13:00:58 -07001724
Hemant Kumare4040492012-06-21 17:35:42 -07001725 mehci->ehci.resume_sof_bug = 1;
1726
Ajay Dudanic4e40db2012-08-20 14:44:40 -07001727 if (pdata)
1728 mehci->ehci.log2_irq_thresh = pdata->log2_irq_thresh;
Hemant Kumar933e0402012-05-22 11:11:40 -07001729
Vamsi Krishna34f01582011-12-14 19:54:42 -08001730 res = platform_get_resource_byname(pdev,
1731 IORESOURCE_IRQ,
1732 "peripheral_status_irq");
1733 if (res)
1734 mehci->peripheral_status_irq = res->start;
1735
Hemant Kumar6fd65032012-05-23 13:02:24 -07001736 res = platform_get_resource_byname(pdev, IORESOURCE_IO, "wakeup");
1737 if (res) {
1738 mehci->wakeup_gpio = res->start;
1739 mehci->wakeup_irq = MSM_GPIO_TO_INT(res->start);
1740 dev_dbg(mehci->dev, "wakeup_irq: %d\n", mehci->wakeup_irq);
1741 }
1742
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301743 ret = msm_hsic_init_clocks(mehci, 1);
1744 if (ret) {
1745 dev_err(&pdev->dev, "unable to initialize clocks\n");
1746 ret = -ENODEV;
1747 goto unmap;
1748 }
1749
1750 ret = msm_hsic_init_vddcx(mehci, 1);
1751 if (ret) {
1752 dev_err(&pdev->dev, "unable to initialize VDDCX\n");
1753 ret = -ENODEV;
1754 goto deinit_clocks;
1755 }
1756
Ajay Dudani11ab1d52012-08-17 17:12:26 -07001757 init_completion(&mehci->rt_completion);
1758 init_completion(&mehci->gpt0_completion);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301759 ret = msm_hsic_reset(mehci);
1760 if (ret) {
1761 dev_err(&pdev->dev, "unable to initialize PHY\n");
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301762 goto deinit_vddcx;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301763 }
1764
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001765 ehci_wq = create_singlethread_workqueue("ehci_wq");
1766 if (!ehci_wq) {
1767 dev_err(&pdev->dev, "unable to create workqueue\n");
1768 ret = -ENOMEM;
1769 goto deinit_vddcx;
1770 }
1771
1772 INIT_WORK(&mehci->bus_vote_w, ehci_hsic_bus_vote_w);
1773
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301774 ret = usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
1775 if (ret) {
1776 dev_err(&pdev->dev, "unable to register HCD\n");
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301777 goto unconfig_gpio;
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301778 }
1779
1780 device_init_wakeup(&pdev->dev, 1);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001781 wake_lock_init(&mehci->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
1782 wake_lock(&mehci->wlock);
Vamsi Krishna34f01582011-12-14 19:54:42 -08001783
1784 if (mehci->peripheral_status_irq) {
1785 ret = request_threaded_irq(mehci->peripheral_status_irq,
1786 NULL, hsic_peripheral_status_change,
1787 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1788 | IRQF_SHARED,
1789 "hsic_peripheral_status", mehci);
1790 if (ret)
1791 dev_err(&pdev->dev, "%s:request_irq:%d failed:%d",
1792 __func__, mehci->peripheral_status_irq, ret);
1793 }
1794
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001795 /* configure wakeup irq */
Hemant Kumar6fd65032012-05-23 13:02:24 -07001796 if (mehci->wakeup_irq) {
Hemant Kumar5e386632012-08-30 14:23:38 -07001797 /* In case if wakeup gpio is pulled high at this point
1798 * remote wakeup interrupt fires right after request_irq.
1799 * Remote wake up interrupt only needs to be enabled when
1800 * HSIC bus goes to suspend.
1801 */
1802 irq_set_status_flags(mehci->wakeup_irq, IRQ_NOAUTOEN);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001803 ret = request_irq(mehci->wakeup_irq, msm_hsic_wakeup_irq,
Hemant Kumar6fd65032012-05-23 13:02:24 -07001804 IRQF_TRIGGER_HIGH,
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001805 "msm_hsic_wakeup", mehci);
Hemant Kumar5e386632012-08-30 14:23:38 -07001806 if (ret) {
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001807 dev_err(&pdev->dev, "request_irq(%d) failed: %d\n",
1808 mehci->wakeup_irq, ret);
1809 mehci->wakeup_irq = 0;
1810 }
1811 }
1812
Hemant Kumare6275972012-02-29 20:06:21 -08001813 ret = ehci_hsic_msm_debugfs_init(mehci);
1814 if (ret)
1815 dev_dbg(&pdev->dev, "mode debugfs file is"
1816 "not available\n");
1817
1818 if (pdata && pdata->bus_scale_table) {
1819 mehci->bus_perf_client =
1820 msm_bus_scale_register_client(pdata->bus_scale_table);
1821 /* Configure BUS performance parameters for MAX bandwidth */
1822 if (mehci->bus_perf_client) {
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001823 mehci->bus_vote = true;
1824 queue_work(ehci_wq, &mehci->bus_vote_w);
Hemant Kumare6275972012-02-29 20:06:21 -08001825 } else {
1826 dev_err(&pdev->dev, "%s: Failed to register BUS "
1827 "scaling client!!\n", __func__);
1828 }
1829 }
1830
Hemant Kumar105d07f2012-07-02 15:33:07 -07001831 __mehci = mehci;
1832
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301833 if (pdata && pdata->swfi_latency)
1834 pm_qos_add_request(&mehci->pm_qos_req_dma,
1835 PM_QOS_CPU_DMA_LATENCY, PM_QOS_DEFAULT_VALUE);
1836
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301837 /*
1838 * This pdev->dev is assigned parent of root-hub by USB core,
1839 * hence, runtime framework automatically calls this driver's
1840 * runtime APIs based on root-hub's state.
1841 */
1842 pm_runtime_set_active(&pdev->dev);
1843 pm_runtime_enable(&pdev->dev);
Vijayavardhan Vennapusaafbbb8f2012-04-13 16:28:45 +05301844 /* Decrement the parent device's counter after probe.
1845 * As child is active, parent will not be put into
1846 * suspend mode.
1847 */
1848 if (pdev->dev.parent)
1849 pm_runtime_put_sync(pdev->dev.parent);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301850
1851 return 0;
1852
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301853unconfig_gpio:
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001854 destroy_workqueue(ehci_wq);
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301855 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301856deinit_vddcx:
1857 msm_hsic_init_vddcx(mehci, 0);
1858deinit_clocks:
1859 msm_hsic_init_clocks(mehci, 0);
1860unmap:
1861 iounmap(hcd->regs);
1862put_hcd:
1863 usb_put_hcd(hcd);
1864
1865 return ret;
1866}
1867
1868static int __devexit ehci_hsic_msm_remove(struct platform_device *pdev)
1869{
1870 struct usb_hcd *hcd = platform_get_drvdata(pdev);
1871 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301872 struct msm_hsic_host_platform_data *pdata = mehci->dev->platform_data;
1873
Pavankumar Kondetifff3f2e2013-02-12 21:43:56 +05301874 /* Remove the HCD prior to releasing our resources. */
1875 usb_remove_hcd(hcd);
1876
Pavankumar Kondetife2d4d32012-09-07 15:33:09 +05301877 if (pdata && pdata->swfi_latency)
1878 pm_qos_remove_request(&mehci->pm_qos_req_dma);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301879
Vamsi Krishna34f01582011-12-14 19:54:42 -08001880 if (mehci->peripheral_status_irq)
1881 free_irq(mehci->peripheral_status_irq, mehci);
Jack Phamfe441ea2012-03-23 17:03:15 -07001882
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001883 if (mehci->wakeup_irq) {
Jack Phamfe441ea2012-03-23 17:03:15 -07001884 if (mehci->wakeup_irq_enabled)
1885 disable_irq_wake(mehci->wakeup_irq);
Vamsi Krishna6921cbe2012-02-21 18:34:43 -08001886 free_irq(mehci->wakeup_irq, mehci);
1887 }
Vamsi Krishna34f01582011-12-14 19:54:42 -08001888
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001889 /*
1890 * If the update request is called after unregister, the request will
1891 * fail. Results are undefined if unregister is called in the middle of
1892 * update request.
1893 */
1894 mehci->bus_vote = false;
1895 cancel_work_sync(&mehci->bus_vote_w);
1896
Hemant Kumare6275972012-02-29 20:06:21 -08001897 if (mehci->bus_perf_client)
1898 msm_bus_scale_unregister_client(mehci->bus_perf_client);
1899
1900 ehci_hsic_msm_debugfs_cleanup();
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301901 device_init_wakeup(&pdev->dev, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301902 pm_runtime_set_suspended(&pdev->dev);
1903
Hemant Kumar2309eaa2012-08-14 16:46:42 -07001904 destroy_workqueue(ehci_wq);
1905
Vijayavardhan Vennapusae3316a12011-10-15 06:05:17 +05301906 msm_hsic_config_gpios(mehci, 0);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301907 msm_hsic_init_vddcx(mehci, 0);
1908
1909 msm_hsic_init_clocks(mehci, 0);
Ajay Dudaniab3bf192012-08-28 09:58:04 -07001910 wake_lock_destroy(&mehci->wlock);
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301911 iounmap(hcd->regs);
1912 usb_put_hcd(hcd);
1913
1914 return 0;
1915}
1916
1917#ifdef CONFIG_PM_SLEEP
1918static int msm_hsic_pm_suspend(struct device *dev)
1919{
1920 struct usb_hcd *hcd = dev_get_drvdata(dev);
1921 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1922
1923 dev_dbg(dev, "ehci-msm-hsic PM suspend\n");
1924
Hemant Kumar45d211b2012-05-31 17:58:43 -07001925 dbg_log_event(NULL, "PM Suspend", 0);
1926
Pavankumar Kondetife319632013-04-25 13:51:24 +05301927 if (!atomic_read(&mehci->in_lpm)) {
1928 dev_info(dev, "abort suspend\n");
1929 dbg_log_event(NULL, "PM Suspend abort", 0);
1930 return -EBUSY;
1931 }
1932
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301933 if (device_may_wakeup(dev))
1934 enable_irq_wake(hcd->irq);
1935
Pavankumar Kondetife319632013-04-25 13:51:24 +05301936 return 0;
Jack Phamfe441ea2012-03-23 17:03:15 -07001937}
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301938
Jack Pham16b06f82012-08-14 20:03:59 -07001939static int msm_hsic_pm_suspend_noirq(struct device *dev)
1940{
1941 struct usb_hcd *hcd = dev_get_drvdata(dev);
1942 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1943
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05301944 if (atomic_read(&mehci->async_int)) {
Jack Pham16b06f82012-08-14 20:03:59 -07001945 dev_dbg(dev, "suspend_noirq: Aborting due to pending interrupt\n");
1946 return -EBUSY;
1947 }
1948
1949 return 0;
1950}
1951
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301952static int msm_hsic_pm_resume(struct device *dev)
1953{
1954 int ret;
1955 struct usb_hcd *hcd = dev_get_drvdata(dev);
1956 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1957
Jack Pham16b06f82012-08-14 20:03:59 -07001958 dev_dbg(dev, "ehci-msm-hsic PM resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07001959 dbg_log_event(NULL, "PM Resume", 0);
1960
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301961 if (device_may_wakeup(dev))
1962 disable_irq_wake(hcd->irq);
1963
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301964 /*
1965 * Keep HSIC in Low Power Mode if system is resumed
1966 * by any other wakeup source. HSIC is resumed later
1967 * when remote wakeup is received or interface driver
1968 * start I/O.
1969 */
Pavankumar Kondeti98089c02012-11-09 10:54:00 +05301970 if (!atomic_read(&mehci->pm_usage_cnt) &&
Pavankumar Kondetibbd05822013-01-28 17:04:39 +05301971 !atomic_read(&mehci->async_int) &&
Pavankumar Kondeti98089c02012-11-09 10:54:00 +05301972 pm_runtime_suspended(dev))
Pavankumar Kondeti1c851692012-09-18 17:52:51 +05301973 return 0;
1974
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301975 ret = msm_hsic_resume(mehci);
1976 if (ret)
1977 return ret;
1978
1979 /* Bring the device to full powered state upon system resume */
1980 pm_runtime_disable(dev);
1981 pm_runtime_set_active(dev);
1982 pm_runtime_enable(dev);
1983
1984 return 0;
1985}
1986#endif
1987
1988#ifdef CONFIG_PM_RUNTIME
1989static int msm_hsic_runtime_idle(struct device *dev)
1990{
1991 dev_dbg(dev, "EHCI runtime idle\n");
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05301992 return 0;
1993}
1994
1995static int msm_hsic_runtime_suspend(struct device *dev)
1996{
1997 struct usb_hcd *hcd = dev_get_drvdata(dev);
1998 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
1999
2000 dev_dbg(dev, "EHCI runtime suspend\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002001
2002 dbg_log_event(NULL, "Run Time PM Suspend", 0);
2003
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302004 return msm_hsic_suspend(mehci);
2005}
2006
2007static int msm_hsic_runtime_resume(struct device *dev)
2008{
2009 struct usb_hcd *hcd = dev_get_drvdata(dev);
2010 struct msm_hsic_hcd *mehci = hcd_to_hsic(hcd);
2011
2012 dev_dbg(dev, "EHCI runtime resume\n");
Hemant Kumar45d211b2012-05-31 17:58:43 -07002013
2014 dbg_log_event(NULL, "Run Time PM Resume", 0);
2015
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302016 return msm_hsic_resume(mehci);
2017}
2018#endif
2019
2020#ifdef CONFIG_PM
2021static const struct dev_pm_ops msm_hsic_dev_pm_ops = {
2022 SET_SYSTEM_SLEEP_PM_OPS(msm_hsic_pm_suspend, msm_hsic_pm_resume)
Jack Pham16b06f82012-08-14 20:03:59 -07002023 .suspend_noirq = msm_hsic_pm_suspend_noirq,
Vijayavardhan Vennapusa39025fe2011-10-15 05:55:10 +05302024 SET_RUNTIME_PM_OPS(msm_hsic_runtime_suspend, msm_hsic_runtime_resume,
2025 msm_hsic_runtime_idle)
2026};
2027#endif
2028
2029static struct platform_driver ehci_msm_hsic_driver = {
2030 .probe = ehci_hsic_msm_probe,
2031 .remove = __devexit_p(ehci_hsic_msm_remove),
2032 .driver = {
2033 .name = "msm_hsic_host",
2034#ifdef CONFIG_PM
2035 .pm = &msm_hsic_dev_pm_ops,
2036#endif
2037 },
2038};