blob: 87c75e762877143f1d7968e1e1eb4b51ee7eb94b [file] [log] [blame]
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -07001/* ehci-msm.c - HSUSB Host Controller Driver Implementation
2 *
Manu Gautam5143b252012-01-05 19:25:23 -08003 * Copyright (c) 2008-2012, Code Aurora Forum. 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;
55 struct otg_transceiver *xceiv;
56 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)
82 clk_enable(pdata->ebi1_clk);
83 else
84 clk_disable(pdata->ebi1_clk);
85}
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 Gautam5143b252012-01-05 19:25:23 -080099 clk_enable(mhcd->alt_core_clk);
100 clk_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 Gautam5143b252012-01-05 19:25:23 -0800122 clk_disable(mhcd->alt_core_clk);
123 clk_disable(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);
273 struct msm_otg *otg = container_of(mhcd->xceiv, struct msm_otg, otg);
274
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)
299 ret = otg_set_suspend(mhcd->xceiv, 1);
300 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) {
319 otg_set_suspend(mhcd->xceiv, 0);
320 } 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
402 hcd->state = HC_STATE_RUNNING;
403
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;
456 struct msm_otg *otg = container_of(mhcd->xceiv, struct msm_otg, otg);
457#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
608#ifdef CONFIG_USB_OTG
609static void ehci_msm_start_hnp(struct ehci_hcd *ehci)
610{
611 struct usb_hcd *hcd = ehci_to_hcd(ehci);
612 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
613
614 /* OTG driver handles HNP */
615 otg_start_hnp(mhcd->xceiv);
616}
617#else
618#define ehci_msm_start_hnp NULL
619#endif
620
Manu Gautam5143b252012-01-05 19:25:23 -0800621static int msm_xusb_init_host(struct platform_device *pdev,
622 struct msmusb_hcd *mhcd)
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700623{
624 int ret = 0;
625 struct msm_otg *otg;
626 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
627 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
628 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
629
630 switch (PHY_TYPE(pdata->phy_info)) {
631 case USB_PHY_INTEGRATED:
632 msm_hsusb_rpc_connect();
633
634 if (pdata->vbus_init)
635 pdata->vbus_init(1);
636
637 /* VBUS might be present. Turn off vbus */
638 if (pdata->vbus_power)
639 pdata->vbus_power(pdata->phy_info, 0);
640
641 INIT_WORK(&mhcd->otg_work, msm_hsusb_otg_work);
642 mhcd->xceiv = otg_get_transceiver();
643 if (!mhcd->xceiv)
644 return -ENODEV;
645 otg = container_of(mhcd->xceiv, struct msm_otg, otg);
646 hcd->regs = otg->regs;
647 otg->start_host = msm_hsusb_start_host;
648 ehci->start_hnp = ehci_msm_start_hnp;
649
650 ret = otg_set_host(mhcd->xceiv, &hcd->self);
651 break;
652 case USB_PHY_SERIAL_PMIC:
653 hcd->regs = ioremap(hcd->rsrc_start, hcd->rsrc_len);
654
655 if (!hcd->regs)
656 return -EFAULT;
657 /* get usb clocks */
Manu Gautam5143b252012-01-05 19:25:23 -0800658 mhcd->alt_core_clk = clk_get(&pdev->dev, "alt_core_clk");
659 if (IS_ERR(mhcd->alt_core_clk)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700660 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800661 return PTR_ERR(mhcd->alt_core_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700662 }
663
Manu Gautam5143b252012-01-05 19:25:23 -0800664 mhcd->iface_clk = clk_get(&pdev->dev, "iface_clk");
665 if (IS_ERR(mhcd->iface_clk)) {
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700666 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800667 clk_put(mhcd->alt_core_clk);
668 return PTR_ERR(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700669 }
670 mhcd->otg_ops.request = msm_hsusb_request_host;
671 mhcd->otg_ops.handle = (void *) mhcd;
672 ret = msm_xusb_init_phy(mhcd);
673 if (ret < 0) {
674 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800675 clk_put(mhcd->alt_core_clk);
676 clk_put(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700677 }
678 break;
679 default:
680 pr_err("phy type is bad\n");
681 }
682 return ret;
683}
684
685static int __devinit ehci_msm_probe(struct platform_device *pdev)
686{
687 struct usb_hcd *hcd;
688 struct resource *res;
689 struct msm_usb_host_platform_data *pdata;
690 int retval;
691 struct msmusb_hcd *mhcd;
692
693 hcd = usb_create_hcd(&msm_hc_driver, &pdev->dev, dev_name(&pdev->dev));
694 if (!hcd)
695 return -ENOMEM;
696
697 hcd->irq = platform_get_irq(pdev, 0);
698 if (hcd->irq < 0) {
699 usb_put_hcd(hcd);
700 return hcd->irq;
701 }
702
703 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
704 if (!res) {
705 usb_put_hcd(hcd);
706 return -ENODEV;
707 }
708
709 hcd->rsrc_start = res->start;
710 hcd->rsrc_len = resource_size(res);
711
712 mhcd = hcd_to_mhcd(hcd);
713 spin_lock_init(&mhcd->lock);
714 mhcd->in_lpm = 0;
715 mhcd->running = 0;
716 device_init_wakeup(&pdev->dev, 1);
717
718 pdata = pdev->dev.platform_data;
719 if (PHY_TYPE(pdata->phy_info) == USB_PHY_UNDEFINED) {
720 usb_put_hcd(hcd);
721 return -ENODEV;
722 }
723 hcd->power_budget = pdata->power_budget;
724 mhcd->pdata = pdata;
725 INIT_WORK(&mhcd->lpm_exit_work, usb_lpm_exit_w);
726
727 wake_lock_init(&mhcd->wlock, WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
Manu Gautam5143b252012-01-05 19:25:23 -0800728 pdata->ebi1_clk = clk_get(&pdev->dev, "core_clk");
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700729 if (IS_ERR(pdata->ebi1_clk))
730 pdata->ebi1_clk = NULL;
731 else
732 clk_set_rate(pdata->ebi1_clk, INT_MAX);
733
Manu Gautam5143b252012-01-05 19:25:23 -0800734 retval = msm_xusb_init_host(pdev, mhcd);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700735
736 if (retval < 0) {
737 wake_lock_destroy(&mhcd->wlock);
738 usb_put_hcd(hcd);
739 clk_put(pdata->ebi1_clk);
740 }
741
742 pm_runtime_enable(&pdev->dev);
743
744 return retval;
745}
746
747static void msm_xusb_uninit_host(struct msmusb_hcd *mhcd)
748{
749 struct usb_hcd *hcd = mhcd_to_hcd(mhcd);
750 struct msm_usb_host_platform_data *pdata = mhcd->pdata;
751
752 switch (PHY_TYPE(pdata->phy_info)) {
753 case USB_PHY_INTEGRATED:
754 if (pdata->vbus_init)
755 pdata->vbus_init(0);
756 otg_set_host(mhcd->xceiv, NULL);
757 otg_put_transceiver(mhcd->xceiv);
758 cancel_work_sync(&mhcd->otg_work);
759 break;
760 case USB_PHY_SERIAL_PMIC:
761 iounmap(hcd->regs);
Manu Gautam5143b252012-01-05 19:25:23 -0800762 clk_put(mhcd->alt_core_clk);
763 clk_put(mhcd->iface_clk);
Bryan Huntsman3f2bc4d2011-08-16 17:27:22 -0700764 msm_fsusb_reset_phy();
765 msm_fsusb_rpc_deinit();
766 break;
767 default:
768 pr_err("phy type is bad\n");
769 }
770}
771static int __exit ehci_msm_remove(struct platform_device *pdev)
772{
773 struct usb_hcd *hcd = platform_get_drvdata(pdev);
774 struct msmusb_hcd *mhcd = hcd_to_mhcd(hcd);
775 struct msm_usb_host_platform_data *pdata;
776 int retval = 0;
777
778 pdata = pdev->dev.platform_data;
779 device_init_wakeup(&pdev->dev, 0);
780
781 msm_hsusb_request_host((void *)mhcd, REQUEST_STOP);
782 msm_xusb_uninit_host(mhcd);
783 retval = msm_xusb_rpc_close(mhcd);
784
785 wake_lock_destroy(&mhcd->wlock);
786 usb_put_hcd(hcd);
787 clk_put(pdata->ebi1_clk);
788
789 pm_runtime_disable(&pdev->dev);
790 pm_runtime_set_suspended(&pdev->dev);
791
792 return retval;
793}
794
795static int ehci_msm_runtime_suspend(struct device *dev)
796{
797 dev_dbg(dev, "pm_runtime: suspending...\n");
798 return 0;
799}
800
801static int ehci_msm_runtime_resume(struct device *dev)
802{
803 dev_dbg(dev, "pm_runtime: resuming...\n");
804 return 0;
805}
806
807static int ehci_msm_runtime_idle(struct device *dev)
808{
809 dev_dbg(dev, "pm_runtime: idling...\n");
810 return 0;
811}
812
813static const struct dev_pm_ops ehci_msm_dev_pm_ops = {
814 .runtime_suspend = ehci_msm_runtime_suspend,
815 .runtime_resume = ehci_msm_runtime_resume,
816 .runtime_idle = ehci_msm_runtime_idle
817};
818
819static struct platform_driver ehci_msm_driver = {
820 .probe = ehci_msm_probe,
821 .remove = __exit_p(ehci_msm_remove),
822 .driver = {.name = "msm_hsusb_host",
823 .pm = &ehci_msm_dev_pm_ops, },
824};