blob: bab330cd7f1e0759b7ddce47d418358a9a6e0db4 [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* ehci-msm.c - HSUSB Host Controller Driver Implementation
2 *
Duy Truong790f06d2013-02-13 16:38:12 -08003 * Copyright (c) 2008-2012, The Linux Foundation. All rights reserved.
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07004 *
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/interrupt.h>
27#include <linux/workqueue.h>
28#include <linux/clk.h>
29#include <linux/spinlock.h>
30
31#include <mach/board.h>
32#include <mach/rpc_hsusb.h>
33#include <mach/msm_hsusb.h>
34#include <mach/msm_hsusb_hw.h>
35#include <mach/msm_otg.h>
36#include <mach/clk.h>
37#include <linux/wakelock.h>
38#include <linux/pm_runtime.h>
39
40#include <mach/msm72k_otg.h>
41
42#define MSM_USB_BASE (hcd->regs)
43
44struct msmusb_hcd {
45 struct ehci_hcd ehci;
Manu Gautam5143b252012-01-05 19:25:23 -080046 struct clk *alt_core_clk;
47 struct clk *iface_clk;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070048 unsigned in_lpm;
49 struct work_struct lpm_exit_work;
50 spinlock_t lock;
51 struct wake_lock wlock;
52 unsigned int clk_enabled;
53 struct msm_usb_host_platform_data *pdata;
54 unsigned running;
Steve Mucklef132c6c2012-06-06 18:30:57 -070055 struct usb_phy *xceiv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070056 struct work_struct otg_work;
57 unsigned flags;
58 struct msm_otg_ops otg_ops;
59};
60
61static inline struct msmusb_hcd *hcd_to_mhcd(struct usb_hcd *hcd)
62{
63 return (struct msmusb_hcd *) (hcd->hcd_priv);
64}
65
66static inline struct usb_hcd *mhcd_to_hcd(struct msmusb_hcd *mhcd)
67{
68 return container_of((void *) mhcd, struct usb_hcd, hcd_priv);
69}
70
71static void msm_xusb_pm_qos_update(struct msmusb_hcd *mhcd, int vote)
72{
73 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
74
75 /* if otg driver is available, it would take
76 * care of voting for appropriate pclk source
77 */
78 if (mhcd->xceiv)
79 return;
80
81 if (vote)
Manu Gautam28b1bac2012-01-30 16:43:06 +053082 clk_prepare_enable(pdata->ebi1_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070083 else
Manu Gautam28b1bac2012-01-30 16:43:06 +053084 clk_disable_unprepare(pdata->ebi1_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -070085}
86
87static void msm_xusb_enable_clks(struct msmusb_hcd *mhcd)
88{
89 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
90
91 if (mhcd->clk_enabled)
92 return;
93
94 switch (PHY_TYPE(pdata->phy_info)) {
95 case USB_PHY_INTEGRATED:
96 /* OTG driver takes care of clock management */
97 break;
98 case USB_PHY_SERIAL_PMIC:
Manu Gautam28b1bac2012-01-30 16:43:06 +053099 clk_prepare_enable(mhcd->alt_core_clk);
100 clk_prepare_enable(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700101 break;
102 default:
Manu Gautam5143b252012-01-05 19:25:23 -0800103 pr_err("%s: undefined phy type ( %X )\n", __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700104 pdata->phy_info);
105 return;
106 }
107 mhcd->clk_enabled = 1;
108}
109
110static void msm_xusb_disable_clks(struct msmusb_hcd *mhcd)
111{
112 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
113
114 if (!mhcd->clk_enabled)
115 return;
116
117 switch (PHY_TYPE(pdata->phy_info)) {
118 case USB_PHY_INTEGRATED:
119 /* OTG driver takes care of clock management */
120 break;
121 case USB_PHY_SERIAL_PMIC:
Manu Gautam28b1bac2012-01-30 16:43:06 +0530122 clk_disable_unprepare(mhcd->alt_core_clk);
123 clk_disable_unprepare(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700124 break;
125 default:
Manu Gautam5143b252012-01-05 19:25:23 -0800126 pr_err("%s: undefined phy type ( %X )\n", __func__,
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700127 pdata->phy_info);
128 return;
129 }
130 mhcd->clk_enabled = 0;
131
132}
133
134static int usb_wakeup_phy(struct usb_hcd *hcd)
135{
136 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
137 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
138 int ret = -ENODEV;
139
140 switch (PHY_TYPE(pdata->phy_info)) {
141 case USB_PHY_INTEGRATED:
142 break;
143 case USB_PHY_SERIAL_PMIC:
144 ret = msm_fsusb_resume_phy();
145 break;
146 default:
147 pr_err("%s: undefined phy type ( %X ) \n", __func__,
148 pdata->phy_info);
149 }
150
151 return ret;
152}
153
154#ifdef CONFIG_PM
155static int usb_suspend_phy(struct usb_hcd *hcd)
156{
157 int ret = 0;
158 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
159 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
160
161 switch (PHY_TYPE(pdata->phy_info)) {
162 case USB_PHY_INTEGRATED:
163 break;
164 case USB_PHY_SERIAL_PMIC:
165 ret = msm_fsusb_set_remote_wakeup();
166 ret = msm_fsusb_suspend_phy();
167 break;
168 default:
169 pr_err("%s: undefined phy type ( %X ) \n", __func__,
170 pdata->phy_info);
171 ret = -ENODEV;
172 break;
173 }
174
175 return ret;
176}
177
178static int usb_lpm_enter(struct usb_hcd *hcd)
179{
180 struct device *dev = container_of((void *)hcd, struct device,
181 platform_data);
182 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
183
184 disable_irq(hcd->irq);
185 if (mhcd->in_lpm) {
186 pr_info("%s: already in lpm. nothing to do\n", __func__);
187 enable_irq(hcd->irq);
188 return 0;
189 }
190
191 if (HC_IS_RUNNING(hcd->state)) {
192 pr_info("%s: can't enter into lpm. controller is runnning\n",
193 __func__);
194 enable_irq(hcd->irq);
195 return -1;
196 }
197
198 pr_info("%s: lpm enter procedure started\n", __func__);
199
200 mhcd->in_lpm = 1;
201
202 if (usb_suspend_phy(hcd)) {
203 mhcd->in_lpm = 0;
204 enable_irq(hcd->irq);
205 pr_info("phy suspend failed\n");
206 pr_info("%s: lpm enter procedure end\n", __func__);
207 return -1;
208 }
209
210 msm_xusb_disable_clks(mhcd);
211
212 if (mhcd->xceiv && mhcd->xceiv->set_suspend)
213 mhcd->xceiv->set_suspend(mhcd->xceiv, 1);
214
215 if (device_may_wakeup(dev))
216 enable_irq_wake(hcd->irq);
217 enable_irq(hcd->irq);
218 pr_info("%s: lpm enter procedure end\n", __func__);
219 return 0;
220}
221#endif
222
223void usb_lpm_exit_w(struct work_struct *work)
224{
225 struct msmusb_hcd *mhcd = container_of((void *) work,
226 struct msmusb_hcd, lpm_exit_work);
227
228 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
229
230 struct device *dev = container_of((void *)hcd, struct device,
231 platform_data);
232 msm_xusb_enable_clks(mhcd);
233
234
235 if (usb_wakeup_phy(hcd)) {
236 pr_err("fatal error: cannot bring phy out of lpm\n");
237 return;
238 }
239
240 /* If resume signalling finishes before lpm exit, PCD is not set in
241 * USBSTS register. Drive resume signal to the downstream device now
242 * so that EHCI can process the upcoming port change interrupt.*/
243
244 writel(readl(USB_PORTSC) | PORTSC_FPR, USB_PORTSC);
245
246 if (mhcd->xceiv && mhcd->xceiv->set_suspend)
247 mhcd->xceiv->set_suspend(mhcd->xceiv, 0);
248
249 if (device_may_wakeup(dev))
250 disable_irq_wake(hcd->irq);
251 enable_irq(hcd->irq);
252}
253
254static void usb_lpm_exit(struct usb_hcd *hcd)
255{
256 unsigned long flags;
257 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
258
259 spin_lock_irqsave(&mhcd->lock, flags);
260 if (!mhcd->in_lpm) {
261 spin_unlock_irqrestore(&mhcd->lock, flags);
262 return;
263 }
264 mhcd->in_lpm = 0;
265 disable_irq_nosync(hcd->irq);
266 schedule_work(&mhcd->lpm_exit_work);
267 spin_unlock_irqrestore(&mhcd->lock, flags);
268}
269
270static irqreturn_t ehci_msm_irq(struct usb_hcd *hcd)
271{
272 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700273 struct msm_otg *otg = container_of(mhcd->xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700274
275 /*
276 * OTG scheduled a work to get Integrated PHY out of LPM,
277 * WAIT till then */
278 if (PHY_TYPE(mhcd->pdata->phy_info) == USB_PHY_INTEGRATED)
279 if (atomic_read(&otg->in_lpm))
280 return IRQ_HANDLED;
281
282 return ehci_irq(hcd);
283}
284
285#ifdef CONFIG_PM
286
287static int ehci_msm_bus_suspend(struct usb_hcd *hcd)
288{
289 int ret;
290 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
291 struct device *dev = hcd->self.controller;
292
293 ret = ehci_bus_suspend(hcd);
294 if (ret) {
295 pr_err("ehci_bus suspend faield\n");
296 return ret;
297 }
298 if (PHY_TYPE(mhcd->pdata->phy_info) == USB_PHY_INTEGRATED)
Steve Mucklef132c6c2012-06-06 18:30:57 -0700299 ret = usb_phy_set_suspend(mhcd->xceiv, 1);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700300 else
301 ret = usb_lpm_enter(hcd);
302
303 pm_runtime_put_noidle(dev);
304 pm_runtime_suspend(dev);
305 wake_unlock(&mhcd->wlock);
306 return ret;
307}
308
309static int ehci_msm_bus_resume(struct usb_hcd *hcd)
310{
311 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
312 struct device *dev = hcd->self.controller;
313
314 wake_lock(&mhcd->wlock);
315 pm_runtime_get_noresume(dev);
316 pm_runtime_resume(dev);
317
318 if (PHY_TYPE(mhcd->pdata->phy_info) == USB_PHY_INTEGRATED) {
Steve Mucklef132c6c2012-06-06 18:30:57 -0700319 usb_phy_set_suspend(mhcd->xceiv, 0);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700320 } else { /* PMIC serial phy */
321 usb_lpm_exit(hcd);
322 if (cancel_work_sync(&(mhcd->lpm_exit_work)))
323 usb_lpm_exit_w(&mhcd->lpm_exit_work);
324 }
325
326 return ehci_bus_resume(hcd);
327
328}
329
330#else
331
332#define ehci_msm_bus_suspend NULL
333#define ehci_msm_bus_resume NULL
334
335#endif /* CONFIG_PM */
336
337static int ehci_msm_reset(struct usb_hcd *hcd)
338{
339 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
340 int retval;
341
342 ehci->caps = USB_CAPLENGTH;
343 ehci->regs = USB_CAPLENGTH +
344 HC_LENGTH(ehci, ehci_readl(ehci, &ehci->caps->hc_capbase));
345
346 /* cache the data to minimize the chip reads*/
347 ehci->hcs_params = ehci_readl(ehci, &ehci->caps->hcs_params);
348
349 retval = ehci_init(hcd);
350 if (retval)
351 return retval;
352
353 hcd->has_tt = 1;
354 ehci->sbrn = HCD_USB2;
355
356 retval = ehci_reset(ehci);
357
358 /* SW workaround for USB stability issues*/
359 writel(0x0, USB_AHB_MODE);
360 writel(0x0, USB_AHB_BURST);
361
362 return retval;
363}
364
365#define PTS_VAL(x) (PHY_TYPE(x) == USB_PHY_SERIAL_PMIC) ? PORTSC_PTS_SERIAL : \
366 PORTSC_PTS_ULPI
367
368static int ehci_msm_run(struct usb_hcd *hcd)
369{
370 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
371 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
372 int retval = 0;
373 int port = HCS_N_PORTS(ehci->hcs_params);
374 u32 __iomem *reg_ptr;
375 u32 hcc_params;
376 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
377
378 hcd->uses_new_polling = 1;
379 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
380
381 /* set hostmode */
382 reg_ptr = (u32 __iomem *)(((u8 __iomem *)ehci->regs) + USBMODE);
383 ehci_writel(ehci, (USBMODE_VBUS | USBMODE_SDIS), reg_ptr);
384
385 /* port configuration - phy, port speed, port power, port enable */
386 while (port--)
387 ehci_writel(ehci, (PTS_VAL(pdata->phy_info) | PORT_POWER |
388 PORT_PE), &ehci->regs->port_status[port]);
389
390 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
391 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
392
393 hcc_params = ehci_readl(ehci, &ehci->caps->hcc_params);
394 if (HCC_64BIT_ADDR(hcc_params))
395 ehci_writel(ehci, 0, &ehci->regs->segment);
396
397 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
398 ehci->command |= CMD_RUN;
399 ehci_writel(ehci, ehci->command, &ehci->regs->command);
400 ehci_readl(ehci, &ehci->regs->command); /* unblock posted writes */
401
Vijayavardhan Vennapusa78aad592012-08-31 15:55:29 +0530402 ehci->rh_state = EHCI_RH_RUNNING;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700403
404 /*Enable appropriate Interrupts*/
405 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
406
407 return retval;
408}
409
410static struct hc_driver msm_hc_driver = {
411 .description = hcd_name,
412 .product_desc = "Qualcomm On-Chip EHCI Host Controller",
413 .hcd_priv_size = sizeof(struct msmusb_hcd),
414
415 /*
416 * generic hardware linkage
417 */
418 .irq = ehci_msm_irq,
419 .flags = HCD_USB2,
420
421 .reset = ehci_msm_reset,
422 .start = ehci_msm_run,
423
424 .stop = ehci_stop,
425 .shutdown = ehci_shutdown,
426
427 /*
428 * managing i/o requests and associated device resources
429 */
430 .urb_enqueue = ehci_urb_enqueue,
431 .urb_dequeue = ehci_urb_dequeue,
432 .endpoint_disable = ehci_endpoint_disable,
433
434 /*
435 * scheduling support
436 */
437 .get_frame_number = ehci_get_frame,
438
439 /*
440 * root hub support
441 */
442 .hub_status_data = ehci_hub_status_data,
443 .hub_control = ehci_hub_control,
444 .bus_suspend = ehci_msm_bus_suspend,
445 .bus_resume = ehci_msm_bus_resume,
446 .relinquish_port = ehci_relinquish_port,
447
448 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
449};
450
451static void msm_hsusb_request_host(void *handle, int request)
452{
453 struct msmusb_hcd *mhcd = handle;
454 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
455 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700456 struct msm_otg *otg = container_of(mhcd->xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700457#ifdef CONFIG_USB_OTG
458 struct usb_device *udev = hcd->self.root_hub;
459#endif
460 struct device *dev = hcd->self.controller;
461
462 switch (request) {
463#ifdef CONFIG_USB_OTG
464 case REQUEST_HNP_SUSPEND:
465 /* disable Root hub auto suspend. As hardware is configured
466 * for peripheral mode, mark hardware is not available.
467 */
468 if (PHY_TYPE(pdata->phy_info) == USB_PHY_INTEGRATED) {
469 pm_runtime_disable(&udev->dev);
470 /* Mark root hub as disconnected. This would
471 * protect suspend/resume via sysfs.
472 */
473 udev->state = USB_STATE_NOTATTACHED;
474 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
475 hcd->state = HC_STATE_HALT;
476 pm_runtime_put_noidle(dev);
477 pm_runtime_suspend(dev);
478 }
479 break;
480 case REQUEST_HNP_RESUME:
481 if (PHY_TYPE(pdata->phy_info) == USB_PHY_INTEGRATED) {
482 pm_runtime_get_noresume(dev);
483 pm_runtime_resume(dev);
484 disable_irq(hcd->irq);
485 ehci_msm_reset(hcd);
486 ehci_msm_run(hcd);
487 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
488 pm_runtime_enable(&udev->dev);
489 udev->state = USB_STATE_CONFIGURED;
490 enable_irq(hcd->irq);
491 }
492 break;
493#endif
494 case REQUEST_RESUME:
495 usb_hcd_resume_root_hub(hcd);
496 break;
497 case REQUEST_START:
498 if (mhcd->running)
499 break;
500 pm_runtime_get_noresume(dev);
501 pm_runtime_resume(dev);
502 wake_lock(&mhcd->wlock);
503 msm_xusb_pm_qos_update(mhcd, 1);
504 msm_xusb_enable_clks(mhcd);
505 if (PHY_TYPE(pdata->phy_info) == USB_PHY_INTEGRATED)
506 if (otg->set_clk)
507 otg->set_clk(mhcd->xceiv, 1);
508 if (pdata->vbus_power)
509 pdata->vbus_power(pdata->phy_info, 1);
510 if (pdata->config_gpio)
511 pdata->config_gpio(1);
512 usb_add_hcd(hcd, hcd->irq, IRQF_SHARED);
513 mhcd->running = 1;
514 if (PHY_TYPE(pdata->phy_info) == USB_PHY_INTEGRATED)
515 if (otg->set_clk)
516 otg->set_clk(mhcd->xceiv, 0);
517 break;
518 case REQUEST_STOP:
519 if (!mhcd->running)
520 break;
521 mhcd->running = 0;
522 /* come out of lpm before deregistration */
523 if (PHY_TYPE(pdata->phy_info) == USB_PHY_SERIAL_PMIC) {
524 usb_lpm_exit(hcd);
525 if (cancel_work_sync(&(mhcd->lpm_exit_work)))
526 usb_lpm_exit_w(&mhcd->lpm_exit_work);
527 }
528 usb_remove_hcd(hcd);
529 if (pdata->config_gpio)
530 pdata->config_gpio(0);
531 if (pdata->vbus_power)
532 pdata->vbus_power(pdata->phy_info, 0);
533 msm_xusb_disable_clks(mhcd);
534 wake_lock_timeout(&mhcd->wlock, HZ/2);
535 msm_xusb_pm_qos_update(mhcd, 0);
536 pm_runtime_put_noidle(dev);
537 pm_runtime_suspend(dev);
538 break;
539 }
540}
541
542static void msm_hsusb_otg_work(struct work_struct *work)
543{
544 struct msmusb_hcd *mhcd;
545
546 mhcd = container_of(work, struct msmusb_hcd, otg_work);
547 msm_hsusb_request_host((void *)mhcd, mhcd->flags);
548}
549static void msm_hsusb_start_host(struct usb_bus *bus, int start)
550{
551 struct usb_hcd *hcd = bus_to_hcd(bus);
552 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
553
554 mhcd->flags = start;
555 if (in_interrupt())
556 schedule_work(&mhcd->otg_work);
557 else
558 msm_hsusb_request_host((void *)mhcd, mhcd->flags);
559
560}
561
562static int msm_xusb_init_phy(struct msmusb_hcd *mhcd)
563{
564 int ret = -ENODEV;
565 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
566 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
567
568 switch (PHY_TYPE(pdata->phy_info)) {
569 case USB_PHY_INTEGRATED:
570 ret = 0;
571 case USB_PHY_SERIAL_PMIC:
572 msm_xusb_enable_clks(mhcd);
573 writel(0, USB_USBINTR);
574 ret = msm_fsusb_rpc_init(&mhcd->otg_ops);
575 if (!ret)
576 msm_fsusb_init_phy();
577 msm_xusb_disable_clks(mhcd);
578 break;
579 default:
580 pr_err("%s: undefined phy type ( %X ) \n", __func__,
581 pdata->phy_info);
582 }
583
584 return ret;
585}
586
587static int msm_xusb_rpc_close(struct msmusb_hcd *mhcd)
588{
589 int retval = -ENODEV;
590 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
591
592 switch (PHY_TYPE(pdata->phy_info)) {
593 case USB_PHY_INTEGRATED:
594 if (!mhcd->xceiv)
595 retval = msm_hsusb_rpc_close();
596 break;
597 case USB_PHY_SERIAL_PMIC:
598 retval = msm_fsusb_reset_phy();
599 msm_fsusb_rpc_deinit();
600 break;
601 default:
602 pr_err("%s: undefined phy type ( %X ) \n", __func__,
603 pdata->phy_info);
604 }
605 return retval;
606}
607
Manu Gautam5143b252012-01-05 19:25:23 -0800608static int msm_xusb_init_host(struct platform_device *pdev,
609 struct msmusb_hcd *mhcd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700610{
611 int ret = 0;
612 struct msm_otg *otg;
613 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
614 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
615 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
616
617 switch (PHY_TYPE(pdata->phy_info)) {
618 case USB_PHY_INTEGRATED:
619 msm_hsusb_rpc_connect();
620
621 if (pdata->vbus_init)
622 pdata->vbus_init(1);
623
624 /* VBUS might be present. Turn off vbus */
625 if (pdata->vbus_power)
626 pdata->vbus_power(pdata->phy_info, 0);
627
628 INIT_WORK(&mhcd->otg_work, msm_hsusb_otg_work);
Steve Mucklef132c6c2012-06-06 18:30:57 -0700629 mhcd->xceiv = usb_get_transceiver();
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700630 if (!mhcd->xceiv)
631 return -ENODEV;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700632 otg = container_of(mhcd->xceiv, struct msm_otg, phy);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700633 hcd->regs = otg->regs;
634 otg->start_host = msm_hsusb_start_host;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700635
Steve Mucklef132c6c2012-06-06 18:30:57 -0700636 ret = otg_set_host(mhcd->xceiv->otg, &hcd->self);
Vijayavardhan Vennapusaa36dc2c2012-03-05 16:09:40 +0530637 ehci->transceiver = mhcd->xceiv;
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700638 break;
639 case USB_PHY_SERIAL_PMIC:
640 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
641
642 if (!hcd->regs)
643 return -EFAULT;
644 /* get usb clocks */
Manu Gautam5143b252012-01-05 19:25:23 -0800645 mhcd->alt_core_clk = clk_get(&pdev->dev, "alt_core_clk");
646 if (IS_ERR(mhcd->alt_core_clk)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700647 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800648 return PTR_ERR(mhcd->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700649 }
650
Manu Gautam5143b252012-01-05 19:25:23 -0800651 mhcd->iface_clk = clk_get(&pdev->dev, "iface_clk");
652 if (IS_ERR(mhcd->iface_clk)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700653 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800654 clk_put(mhcd->alt_core_clk);
655 return PTR_ERR(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700656 }
657 mhcd->otg_ops.request = msm_hsusb_request_host;
658 mhcd->otg_ops.handle = (void *) mhcd;
659 ret = msm_xusb_init_phy(mhcd);
660 if (ret < 0) {
661 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800662 clk_put(mhcd->alt_core_clk);
663 clk_put(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700664 }
665 break;
666 default:
667 pr_err("phy type is bad\n");
668 }
669 return ret;
670}
671
672static int __devinit ehci_msm_probe(struct platform_device *pdev)
673{
674 struct usb_hcd *hcd;
675 struct resource *res;
676 struct msm_usb_host_platform_data *pdata;
677 int retval;
678 struct msmusb_hcd *mhcd;
679
680 hcd = usb_create_hcd(&msm_hc_driver, &pdev->dev, dev_name(&pdev->dev));
681 if (!hcd)
682 return -ENOMEM;
683
Manu Gautam578656b2013-03-19 15:30:08 +0530684 hcd_to_bus(hcd)->skip_resume = true;
685
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700686 hcd->irq = platform_get_irq(pdev, 0);
687 if (hcd->irq < 0) {
688 usb_put_hcd(hcd);
689 return hcd->irq;
690 }
691
692 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
693 if (!res) {
694 usb_put_hcd(hcd);
695 return -ENODEV;
696 }
697
698 hcd->rsrc_start = res->start;
699 hcd->rsrc_len = resource_size(res);
700
701 mhcd = hcd_to_mhcd(hcd);
702 spin_lock_init(&mhcd->lock);
703 mhcd->in_lpm = 0;
704 mhcd->running = 0;
705 device_init_wakeup(&pdev->dev, 1);
706
707 pdata = pdev->dev.platform_data;
708 if (PHY_TYPE(pdata->phy_info) == USB_PHY_UNDEFINED) {
709 usb_put_hcd(hcd);
710 return -ENODEV;
711 }
712 hcd->power_budget = pdata->power_budget;
713 mhcd->pdata = pdata;
714 INIT_WORK(&mhcd->lpm_exit_work, usb_lpm_exit_w);
715
716 wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
Manu Gautam5143b252012-01-05 19:25:23 -0800717 pdata->ebi1_clk = clk_get(&pdev->dev, "core_clk");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700718 if (IS_ERR(pdata->ebi1_clk))
719 pdata->ebi1_clk = NULL;
720 else
721 clk_set_rate(pdata->ebi1_clk, INT_MAX);
722
Manu Gautam5143b252012-01-05 19:25:23 -0800723 retval = msm_xusb_init_host(pdev, mhcd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700724
725 if (retval < 0) {
726 wake_lock_destroy(&mhcd->wlock);
727 usb_put_hcd(hcd);
728 clk_put(pdata->ebi1_clk);
729 }
730
731 pm_runtime_enable(&pdev->dev);
732
733 return retval;
734}
735
736static void msm_xusb_uninit_host(struct msmusb_hcd *mhcd)
737{
738 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
739 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
740
741 switch (PHY_TYPE(pdata->phy_info)) {
742 case USB_PHY_INTEGRATED:
743 if (pdata->vbus_init)
744 pdata->vbus_init(0);
Vijayavardhan Vennapusaa36dc2c2012-03-05 16:09:40 +0530745 hcd_to_ehci(hcd)->transceiver = NULL;
Steve Mucklef132c6c2012-06-06 18:30:57 -0700746 otg_set_host(mhcd->xceiv->otg, NULL);
747 usb_put_transceiver(mhcd->xceiv);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700748 cancel_work_sync(&mhcd->otg_work);
749 break;
750 case USB_PHY_SERIAL_PMIC:
751 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800752 clk_put(mhcd->alt_core_clk);
753 clk_put(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700754 msm_fsusb_reset_phy();
755 msm_fsusb_rpc_deinit();
756 break;
757 default:
758 pr_err("phy type is bad\n");
759 }
760}
761static int __exit ehci_msm_remove(struct platform_device *pdev)
762{
763 struct usb_hcd *hcd = platform_get_drvdata(pdev);
764 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
765 struct msm_usb_host_platform_data *pdata;
766 int retval = 0;
767
768 pdata = pdev->dev.platform_data;
769 device_init_wakeup(&pdev->dev, 0);
770
771 msm_hsusb_request_host((void *)mhcd, REQUEST_STOP);
772 msm_xusb_uninit_host(mhcd);
773 retval = msm_xusb_rpc_close(mhcd);
774
775 wake_lock_destroy(&mhcd->wlock);
776 usb_put_hcd(hcd);
777 clk_put(pdata->ebi1_clk);
778
779 pm_runtime_disable(&pdev->dev);
780 pm_runtime_set_suspended(&pdev->dev);
781
782 return retval;
783}
784
785static int ehci_msm_runtime_suspend(struct device *dev)
786{
787 dev_dbg(dev, "pm_runtime: suspending...\n");
788 return 0;
789}
790
791static int ehci_msm_runtime_resume(struct device *dev)
792{
793 dev_dbg(dev, "pm_runtime: resuming...\n");
794 return 0;
795}
796
797static int ehci_msm_runtime_idle(struct device *dev)
798{
799 dev_dbg(dev, "pm_runtime: idling...\n");
800 return 0;
801}
802
803static const struct dev_pm_ops ehci_msm_dev_pm_ops = {
804 .runtime_suspend = ehci_msm_runtime_suspend,
805 .runtime_resume = ehci_msm_runtime_resume,
806 .runtime_idle = ehci_msm_runtime_idle
807};
808
809static struct platform_driver ehci_msm_driver = {
810 .probe = ehci_msm_probe,
811 .remove = __exit_p(ehci_msm_remove),
812 .driver = {.name = "msm_hsusb_host",
813 .pm = &ehci_msm_dev_pm_ops, },
814};