EHCI: update PM methods in ehci-tegra.c
[pandora-kernel.git] / drivers / usb / host / ehci-tegra.c
1 /*
2  * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
3  *
4  * Copyright (C) 2010 Google, Inc.
5  * Copyright (C) 2009 NVIDIA Corporation
6  *
7  * This program is free software; you can redistribute it and/or modify it
8  * under the terms of the GNU General Public License as published by the
9  * Free Software Foundation; either version 2 of the License, or (at your
10  * option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful, but WITHOUT
13  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for
15  * more details.
16  *
17  */
18
19 #include <linux/clk.h>
20 #include <linux/platform_device.h>
21 #include <linux/platform_data/tegra_usb.h>
22 #include <linux/irq.h>
23 #include <linux/usb/otg.h>
24 #include <linux/gpio.h>
25 #include <linux/of.h>
26 #include <linux/of_gpio.h>
27 #include <linux/pm_runtime.h>
28
29 #include <mach/usb_phy.h>
30 #include <mach/iomap.h>
31
32 #define TEGRA_USB_DMA_ALIGN 32
33
34 struct tegra_ehci_hcd {
35         struct ehci_hcd *ehci;
36         struct tegra_usb_phy *phy;
37         struct clk *clk;
38         struct clk *emc_clk;
39         struct usb_phy *transceiver;
40         int host_resumed;
41         int port_resuming;
42         enum tegra_usb_phy_port_speed port_speed;
43 };
44
45 static void tegra_ehci_power_up(struct usb_hcd *hcd)
46 {
47         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
48
49         clk_enable(tegra->emc_clk);
50         clk_enable(tegra->clk);
51         tegra_usb_phy_power_on(tegra->phy);
52         tegra->host_resumed = 1;
53 }
54
55 static void tegra_ehci_power_down(struct usb_hcd *hcd)
56 {
57         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
58
59         tegra->host_resumed = 0;
60         tegra_usb_phy_power_off(tegra->phy);
61         clk_disable(tegra->clk);
62         clk_disable(tegra->emc_clk);
63 }
64
65 static int tegra_ehci_internal_port_reset(
66         struct ehci_hcd *ehci,
67         u32 __iomem     *portsc_reg
68 )
69 {
70         u32             temp;
71         unsigned long   flags;
72         int             retval = 0;
73         int             i, tries;
74         u32             saved_usbintr;
75
76         spin_lock_irqsave(&ehci->lock, flags);
77         saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
78         /* disable USB interrupt */
79         ehci_writel(ehci, 0, &ehci->regs->intr_enable);
80         spin_unlock_irqrestore(&ehci->lock, flags);
81
82         /*
83          * Here we have to do Port Reset at most twice for
84          * Port Enable bit to be set.
85          */
86         for (i = 0; i < 2; i++) {
87                 temp = ehci_readl(ehci, portsc_reg);
88                 temp |= PORT_RESET;
89                 ehci_writel(ehci, temp, portsc_reg);
90                 mdelay(10);
91                 temp &= ~PORT_RESET;
92                 ehci_writel(ehci, temp, portsc_reg);
93                 mdelay(1);
94                 tries = 100;
95                 do {
96                         mdelay(1);
97                         /*
98                          * Up to this point, Port Enable bit is
99                          * expected to be set after 2 ms waiting.
100                          * USB1 usually takes extra 45 ms, for safety,
101                          * we take 100 ms as timeout.
102                          */
103                         temp = ehci_readl(ehci, portsc_reg);
104                 } while (!(temp & PORT_PE) && tries--);
105                 if (temp & PORT_PE)
106                         break;
107         }
108         if (i == 2)
109                 retval = -ETIMEDOUT;
110
111         /*
112          * Clear Connect Status Change bit if it's set.
113          * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
114          */
115         if (temp & PORT_CSC)
116                 ehci_writel(ehci, PORT_CSC, portsc_reg);
117
118         /*
119          * Write to clear any interrupt status bits that might be set
120          * during port reset.
121          */
122         temp = ehci_readl(ehci, &ehci->regs->status);
123         ehci_writel(ehci, temp, &ehci->regs->status);
124
125         /* restore original interrupt enable bits */
126         ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
127         return retval;
128 }
129
130 static int tegra_ehci_hub_control(
131         struct usb_hcd  *hcd,
132         u16             typeReq,
133         u16             wValue,
134         u16             wIndex,
135         char            *buf,
136         u16             wLength
137 )
138 {
139         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
140         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
141         u32 __iomem     *status_reg;
142         u32             temp;
143         unsigned long   flags;
144         int             retval = 0;
145
146         status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
147
148         spin_lock_irqsave(&ehci->lock, flags);
149
150         /*
151          * In ehci_hub_control() for USB_PORT_FEAT_ENABLE clears the other bits
152          * that are write on clear, by writing back the register read value, so
153          * USB_PORT_FEAT_ENABLE is handled by masking the set on clear bits
154          */
155         if (typeReq == ClearPortFeature && wValue == USB_PORT_FEAT_ENABLE) {
156                 temp = ehci_readl(ehci, status_reg) & ~PORT_RWC_BITS;
157                 ehci_writel(ehci, temp & ~PORT_PE, status_reg);
158                 goto done;
159         }
160
161         else if (typeReq == GetPortStatus) {
162                 temp = ehci_readl(ehci, status_reg);
163                 if (tegra->port_resuming && !(temp & PORT_SUSPEND)) {
164                         /* Resume completed, re-enable disconnect detection */
165                         tegra->port_resuming = 0;
166                         tegra_usb_phy_postresume(tegra->phy);
167                 }
168         }
169
170         else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
171                 temp = ehci_readl(ehci, status_reg);
172                 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
173                         retval = -EPIPE;
174                         goto done;
175                 }
176
177                 temp &= ~PORT_WKCONN_E;
178                 temp |= PORT_WKDISC_E | PORT_WKOC_E;
179                 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
180
181                 /*
182                  * If a transaction is in progress, there may be a delay in
183                  * suspending the port. Poll until the port is suspended.
184                  */
185                 if (handshake(ehci, status_reg, PORT_SUSPEND,
186                                                 PORT_SUSPEND, 5000))
187                         pr_err("%s: timeout waiting for SUSPEND\n", __func__);
188
189                 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
190                 goto done;
191         }
192
193         /* For USB1 port we need to issue Port Reset twice internally */
194         if (tegra->phy->instance == 0 &&
195            (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
196                 spin_unlock_irqrestore(&ehci->lock, flags);
197                 return tegra_ehci_internal_port_reset(ehci, status_reg);
198         }
199
200         /*
201          * Tegra host controller will time the resume operation to clear the bit
202          * when the port control state switches to HS or FS Idle. This behavior
203          * is different from EHCI where the host controller driver is required
204          * to set this bit to a zero after the resume duration is timed in the
205          * driver.
206          */
207         else if (typeReq == ClearPortFeature &&
208                                         wValue == USB_PORT_FEAT_SUSPEND) {
209                 temp = ehci_readl(ehci, status_reg);
210                 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
211                         retval = -EPIPE;
212                         goto done;
213                 }
214
215                 if (!(temp & PORT_SUSPEND))
216                         goto done;
217
218                 /* Disable disconnect detection during port resume */
219                 tegra_usb_phy_preresume(tegra->phy);
220
221                 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
222
223                 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
224                 /* start resume signalling */
225                 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
226                 set_bit(wIndex-1, &ehci->resuming_ports);
227
228                 spin_unlock_irqrestore(&ehci->lock, flags);
229                 msleep(20);
230                 spin_lock_irqsave(&ehci->lock, flags);
231
232                 /* Poll until the controller clears RESUME and SUSPEND */
233                 if (handshake(ehci, status_reg, PORT_RESUME, 0, 2000))
234                         pr_err("%s: timeout waiting for RESUME\n", __func__);
235                 if (handshake(ehci, status_reg, PORT_SUSPEND, 0, 2000))
236                         pr_err("%s: timeout waiting for SUSPEND\n", __func__);
237
238                 ehci->reset_done[wIndex-1] = 0;
239                 clear_bit(wIndex-1, &ehci->resuming_ports);
240
241                 tegra->port_resuming = 1;
242                 goto done;
243         }
244
245         spin_unlock_irqrestore(&ehci->lock, flags);
246
247         /* Handle the hub control events here */
248         return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
249 done:
250         spin_unlock_irqrestore(&ehci->lock, flags);
251         return retval;
252 }
253
254 static void tegra_ehci_restart(struct usb_hcd *hcd)
255 {
256         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
257
258         ehci_reset(ehci);
259
260         /* setup the frame list and Async q heads */
261         ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
262         ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
263         /* setup the command register and set the controller in RUN mode */
264         ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
265         ehci->command |= CMD_RUN;
266         ehci_writel(ehci, ehci->command, &ehci->regs->command);
267
268         down_write(&ehci_cf_port_reset_rwsem);
269         ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
270         /* flush posted writes */
271         ehci_readl(ehci, &ehci->regs->command);
272         up_write(&ehci_cf_port_reset_rwsem);
273 }
274
275 static void tegra_ehci_shutdown(struct usb_hcd *hcd)
276 {
277         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
278
279         /* ehci_shutdown touches the USB controller registers, make sure
280          * controller has clocks to it */
281         if (!tegra->host_resumed)
282                 tegra_ehci_power_up(hcd);
283
284         ehci_shutdown(hcd);
285 }
286
287 static int tegra_ehci_setup(struct usb_hcd *hcd)
288 {
289         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
290         int retval;
291
292         /* EHCI registers start at offset 0x100 */
293         ehci->caps = hcd->regs + 0x100;
294         ehci->regs = hcd->regs + 0x100 +
295                 HC_LENGTH(ehci, readl(&ehci->caps->hc_capbase));
296
297         dbg_hcs_params(ehci, "reset");
298         dbg_hcc_params(ehci, "reset");
299
300         /* cache this readonly data; minimize chip reads */
301         ehci->hcs_params = readl(&ehci->caps->hcs_params);
302
303         /* switch to host mode */
304         hcd->has_tt = 1;
305         ehci_reset(ehci);
306
307         retval = ehci_halt(ehci);
308         if (retval)
309                 return retval;
310
311         /* data structure init */
312         retval = ehci_init(hcd);
313         if (retval)
314                 return retval;
315
316         ehci->sbrn = 0x20;
317
318         ehci_port_power(ehci, 1);
319         return retval;
320 }
321
322 struct temp_buffer {
323         void *kmalloc_ptr;
324         void *old_xfer_buffer;
325         u8 data[0];
326 };
327
328 static void free_temp_buffer(struct urb *urb)
329 {
330         enum dma_data_direction dir;
331         struct temp_buffer *temp;
332
333         if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
334                 return;
335
336         dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
337
338         temp = container_of(urb->transfer_buffer, struct temp_buffer,
339                             data);
340
341         if (dir == DMA_FROM_DEVICE)
342                 memcpy(temp->old_xfer_buffer, temp->data,
343                        urb->transfer_buffer_length);
344         urb->transfer_buffer = temp->old_xfer_buffer;
345         kfree(temp->kmalloc_ptr);
346
347         urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
348 }
349
350 static int alloc_temp_buffer(struct urb *urb, gfp_t mem_flags)
351 {
352         enum dma_data_direction dir;
353         struct temp_buffer *temp, *kmalloc_ptr;
354         size_t kmalloc_size;
355
356         if (urb->num_sgs || urb->sg ||
357             urb->transfer_buffer_length == 0 ||
358             !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
359                 return 0;
360
361         dir = usb_urb_dir_in(urb) ? DMA_FROM_DEVICE : DMA_TO_DEVICE;
362
363         /* Allocate a buffer with enough padding for alignment */
364         kmalloc_size = urb->transfer_buffer_length +
365                 sizeof(struct temp_buffer) + TEGRA_USB_DMA_ALIGN - 1;
366
367         kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
368         if (!kmalloc_ptr)
369                 return -ENOMEM;
370
371         /* Position our struct temp_buffer such that data is aligned */
372         temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
373
374         temp->kmalloc_ptr = kmalloc_ptr;
375         temp->old_xfer_buffer = urb->transfer_buffer;
376         if (dir == DMA_TO_DEVICE)
377                 memcpy(temp->data, urb->transfer_buffer,
378                        urb->transfer_buffer_length);
379         urb->transfer_buffer = temp->data;
380
381         urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
382
383         return 0;
384 }
385
386 static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
387                                       gfp_t mem_flags)
388 {
389         int ret;
390
391         ret = alloc_temp_buffer(urb, mem_flags);
392         if (ret)
393                 return ret;
394
395         ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
396         if (ret)
397                 free_temp_buffer(urb);
398
399         return ret;
400 }
401
402 static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
403 {
404         usb_hcd_unmap_urb_for_dma(hcd, urb);
405         free_temp_buffer(urb);
406 }
407
408 static const struct hc_driver tegra_ehci_hc_driver = {
409         .description            = hcd_name,
410         .product_desc           = "Tegra EHCI Host Controller",
411         .hcd_priv_size          = sizeof(struct ehci_hcd),
412
413         .flags                  = HCD_USB2 | HCD_MEMORY,
414
415         .reset                  = tegra_ehci_setup,
416         .irq                    = ehci_irq,
417
418         .start                  = ehci_run,
419         .stop                   = ehci_stop,
420         .shutdown               = tegra_ehci_shutdown,
421         .urb_enqueue            = ehci_urb_enqueue,
422         .urb_dequeue            = ehci_urb_dequeue,
423         .map_urb_for_dma        = tegra_ehci_map_urb_for_dma,
424         .unmap_urb_for_dma      = tegra_ehci_unmap_urb_for_dma,
425         .endpoint_disable       = ehci_endpoint_disable,
426         .endpoint_reset         = ehci_endpoint_reset,
427         .get_frame_number       = ehci_get_frame,
428         .hub_status_data        = ehci_hub_status_data,
429         .hub_control            = tegra_ehci_hub_control,
430         .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
431 #ifdef CONFIG_PM
432         .bus_suspend            = ehci_bus_suspend,
433         .bus_resume             = ehci_bus_resume,
434 #endif
435         .relinquish_port        = ehci_relinquish_port,
436         .port_handed_over       = ehci_port_handed_over,
437 };
438
439 static int setup_vbus_gpio(struct platform_device *pdev)
440 {
441         int err = 0;
442         int gpio;
443
444         if (!pdev->dev.of_node)
445                 return 0;
446
447         gpio = of_get_named_gpio(pdev->dev.of_node, "nvidia,vbus-gpio", 0);
448         if (!gpio_is_valid(gpio))
449                 return 0;
450
451         err = gpio_request(gpio, "vbus_gpio");
452         if (err) {
453                 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
454                 return err;
455         }
456         err = gpio_direction_output(gpio, 1);
457         if (err) {
458                 dev_err(&pdev->dev, "can't enable vbus\n");
459                 return err;
460         }
461         gpio_set_value(gpio, 1);
462
463         return err;
464 }
465
466 #ifdef CONFIG_PM
467
468 static int controller_suspend(struct device *dev)
469 {
470         struct tegra_ehci_hcd *tegra =
471                         platform_get_drvdata(to_platform_device(dev));
472         struct ehci_hcd *ehci = tegra->ehci;
473         struct usb_hcd *hcd = ehci_to_hcd(ehci);
474         struct ehci_regs __iomem *hw = ehci->regs;
475         unsigned long flags;
476
477         if (time_before(jiffies, ehci->next_statechange))
478                 msleep(10);
479
480         spin_lock_irqsave(&ehci->lock, flags);
481
482         tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
483         ehci_halt(ehci);
484         clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
485
486         spin_unlock_irqrestore(&ehci->lock, flags);
487
488         tegra_ehci_power_down(hcd);
489         return 0;
490 }
491
492 static int controller_resume(struct device *dev)
493 {
494         struct tegra_ehci_hcd *tegra =
495                         platform_get_drvdata(to_platform_device(dev));
496         struct ehci_hcd *ehci = tegra->ehci;
497         struct usb_hcd *hcd = ehci_to_hcd(ehci);
498         struct ehci_regs __iomem *hw = ehci->regs;
499         unsigned long val;
500
501         set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
502         tegra_ehci_power_up(hcd);
503
504         if (tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) {
505                 /* Wait for the phy to detect new devices
506                  * before we restart the controller */
507                 msleep(10);
508                 goto restart;
509         }
510
511         /* Force the phy to keep data lines in suspend state */
512         tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
513
514         /* Enable host mode */
515         tdi_reset(ehci);
516
517         /* Enable Port Power */
518         val = readl(&hw->port_status[0]);
519         val |= PORT_POWER;
520         writel(val, &hw->port_status[0]);
521         udelay(10);
522
523         /* Check if the phy resume from LP0. When the phy resume from LP0
524          * USB register will be reset. */
525         if (!readl(&hw->async_next)) {
526                 /* Program the field PTC based on the saved speed mode */
527                 val = readl(&hw->port_status[0]);
528                 val &= ~PORT_TEST(~0);
529                 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
530                         val |= PORT_TEST_FORCE;
531                 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
532                         val |= PORT_TEST(6);
533                 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
534                         val |= PORT_TEST(7);
535                 writel(val, &hw->port_status[0]);
536                 udelay(10);
537
538                 /* Disable test mode by setting PTC field to NORMAL_OP */
539                 val = readl(&hw->port_status[0]);
540                 val &= ~PORT_TEST(~0);
541                 writel(val, &hw->port_status[0]);
542                 udelay(10);
543         }
544
545         /* Poll until CCS is enabled */
546         if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
547                                                  PORT_CONNECT, 2000)) {
548                 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
549                 goto restart;
550         }
551
552         /* Poll until PE is enabled */
553         if (handshake(ehci, &hw->port_status[0], PORT_PE,
554                                                  PORT_PE, 2000)) {
555                 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
556                 goto restart;
557         }
558
559         /* Clear the PCI status, to avoid an interrupt taken upon resume */
560         val = readl(&hw->status);
561         val |= STS_PCD;
562         writel(val, &hw->status);
563
564         /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
565         val = readl(&hw->port_status[0]);
566         if ((val & PORT_POWER) && (val & PORT_PE)) {
567                 val |= PORT_SUSPEND;
568                 writel(val, &hw->port_status[0]);
569
570                 /* Wait until port suspend completes */
571                 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
572                                                          PORT_SUSPEND, 1000)) {
573                         pr_err("%s: timeout waiting for PORT_SUSPEND\n",
574                                                                 __func__);
575                         goto restart;
576                 }
577         }
578
579         tegra_ehci_phy_restore_end(tegra->phy);
580         goto done;
581
582  restart:
583         if (tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH)
584                 tegra_ehci_phy_restore_end(tegra->phy);
585
586         tegra_ehci_restart(hcd);
587
588  done:
589         tegra_usb_phy_preresume(tegra->phy);
590         tegra->port_resuming = 1;
591         return 0;
592 }
593
594 static int tegra_ehci_suspend(struct device *dev)
595 {
596         struct tegra_ehci_hcd *tegra =
597                         platform_get_drvdata(to_platform_device(dev));
598         struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
599         int rc = 0;
600
601         /*
602          * When system sleep is supported and USB controller wakeup is
603          * implemented: If the controller is runtime-suspended and the
604          * wakeup setting needs to be changed, call pm_runtime_resume().
605          */
606         if (HCD_HW_ACCESSIBLE(hcd))
607                 rc = controller_suspend(dev);
608         return rc;
609 }
610
611 static int tegra_ehci_resume(struct device *dev)
612 {
613         int rc;
614
615         rc = controller_resume(dev);
616         if (rc == 0) {
617                 pm_runtime_disable(dev);
618                 pm_runtime_set_active(dev);
619                 pm_runtime_enable(dev);
620         }
621         return rc;
622 }
623
624 static int tegra_ehci_runtime_suspend(struct device *dev)
625 {
626         return controller_suspend(dev);
627 }
628
629 static int tegra_ehci_runtime_resume(struct device *dev)
630 {
631         return controller_resume(dev);
632 }
633
634 static const struct dev_pm_ops tegra_ehci_pm_ops = {
635         .suspend        = tegra_ehci_suspend,
636         .resume         = tegra_ehci_resume,
637         .runtime_suspend = tegra_ehci_runtime_suspend,
638         .runtime_resume = tegra_ehci_runtime_resume,
639 };
640
641 #endif
642
643 static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
644
645 static int tegra_ehci_probe(struct platform_device *pdev)
646 {
647         struct resource *res;
648         struct usb_hcd *hcd;
649         struct tegra_ehci_hcd *tegra;
650         struct tegra_ehci_platform_data *pdata;
651         int err = 0;
652         int irq;
653         int instance = pdev->id;
654
655         pdata = pdev->dev.platform_data;
656         if (!pdata) {
657                 dev_err(&pdev->dev, "Platform data missing\n");
658                 return -EINVAL;
659         }
660
661         /* Right now device-tree probed devices don't get dma_mask set.
662          * Since shared usb code relies on it, set it here for now.
663          * Once we have dma capability bindings this can go away.
664          */
665         if (!pdev->dev.dma_mask)
666                 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
667
668         setup_vbus_gpio(pdev);
669
670         tegra = kzalloc(sizeof(struct tegra_ehci_hcd), GFP_KERNEL);
671         if (!tegra)
672                 return -ENOMEM;
673
674         hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
675                                         dev_name(&pdev->dev));
676         if (!hcd) {
677                 dev_err(&pdev->dev, "Unable to create HCD\n");
678                 err = -ENOMEM;
679                 goto fail_hcd;
680         }
681
682         platform_set_drvdata(pdev, tegra);
683
684         tegra->clk = clk_get(&pdev->dev, NULL);
685         if (IS_ERR(tegra->clk)) {
686                 dev_err(&pdev->dev, "Can't get ehci clock\n");
687                 err = PTR_ERR(tegra->clk);
688                 goto fail_clk;
689         }
690
691         err = clk_enable(tegra->clk);
692         if (err)
693                 goto fail_clken;
694
695         tegra->emc_clk = clk_get(&pdev->dev, "emc");
696         if (IS_ERR(tegra->emc_clk)) {
697                 dev_err(&pdev->dev, "Can't get emc clock\n");
698                 err = PTR_ERR(tegra->emc_clk);
699                 goto fail_emc_clk;
700         }
701
702         clk_enable(tegra->emc_clk);
703         clk_set_rate(tegra->emc_clk, 400000000);
704
705         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
706         if (!res) {
707                 dev_err(&pdev->dev, "Failed to get I/O memory\n");
708                 err = -ENXIO;
709                 goto fail_io;
710         }
711         hcd->rsrc_start = res->start;
712         hcd->rsrc_len = resource_size(res);
713         hcd->regs = ioremap(res->start, resource_size(res));
714         if (!hcd->regs) {
715                 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
716                 err = -ENOMEM;
717                 goto fail_io;
718         }
719
720         /* This is pretty ugly and needs to be fixed when we do only
721          * device-tree probing. Old code relies on the platform_device
722          * numbering that we lack for device-tree-instantiated devices.
723          */
724         if (instance < 0) {
725                 switch (res->start) {
726                 case TEGRA_USB_BASE:
727                         instance = 0;
728                         break;
729                 case TEGRA_USB2_BASE:
730                         instance = 1;
731                         break;
732                 case TEGRA_USB3_BASE:
733                         instance = 2;
734                         break;
735                 default:
736                         err = -ENODEV;
737                         dev_err(&pdev->dev, "unknown usb instance\n");
738                         goto fail_phy;
739                 }
740         }
741
742         tegra->phy = tegra_usb_phy_open(instance, hcd->regs, pdata->phy_config,
743                                                 TEGRA_USB_PHY_MODE_HOST);
744         if (IS_ERR(tegra->phy)) {
745                 dev_err(&pdev->dev, "Failed to open USB phy\n");
746                 err = -ENXIO;
747                 goto fail_phy;
748         }
749
750         err = tegra_usb_phy_power_on(tegra->phy);
751         if (err) {
752                 dev_err(&pdev->dev, "Failed to power on the phy\n");
753                 goto fail;
754         }
755
756         tegra->host_resumed = 1;
757         tegra->ehci = hcd_to_ehci(hcd);
758
759         irq = platform_get_irq(pdev, 0);
760         if (!irq) {
761                 dev_err(&pdev->dev, "Failed to get IRQ\n");
762                 err = -ENODEV;
763                 goto fail;
764         }
765
766 #ifdef CONFIG_USB_OTG_UTILS
767         if (pdata->operating_mode == TEGRA_USB_OTG) {
768                 tegra->transceiver = usb_get_transceiver();
769                 if (tegra->transceiver)
770                         otg_set_host(tegra->transceiver->otg, &hcd->self);
771         }
772 #endif
773
774         err = usb_add_hcd(hcd, irq, IRQF_SHARED);
775         if (err) {
776                 dev_err(&pdev->dev, "Failed to add USB HCD\n");
777                 goto fail;
778         }
779
780         pm_runtime_set_active(&pdev->dev);
781         pm_runtime_get_noresume(&pdev->dev);
782
783         /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
784         /* if (!pdata->power_down_on_bus_suspend) */
785                 pm_runtime_forbid(&pdev->dev);
786         pm_runtime_enable(&pdev->dev);
787         pm_runtime_put_sync(&pdev->dev);
788         return err;
789
790 fail:
791 #ifdef CONFIG_USB_OTG_UTILS
792         if (tegra->transceiver) {
793                 otg_set_host(tegra->transceiver->otg, NULL);
794                 usb_put_transceiver(tegra->transceiver);
795         }
796 #endif
797         tegra_usb_phy_close(tegra->phy);
798 fail_phy:
799         iounmap(hcd->regs);
800 fail_io:
801         clk_disable(tegra->emc_clk);
802         clk_put(tegra->emc_clk);
803 fail_emc_clk:
804         clk_disable(tegra->clk);
805 fail_clken:
806         clk_put(tegra->clk);
807 fail_clk:
808         usb_put_hcd(hcd);
809 fail_hcd:
810         kfree(tegra);
811         return err;
812 }
813
814 static int tegra_ehci_remove(struct platform_device *pdev)
815 {
816         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
817         struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
818
819         if (tegra == NULL || hcd == NULL)
820                 return -EINVAL;
821
822         pm_runtime_get_sync(&pdev->dev);
823         pm_runtime_disable(&pdev->dev);
824         pm_runtime_put_noidle(&pdev->dev);
825
826 #ifdef CONFIG_USB_OTG_UTILS
827         if (tegra->transceiver) {
828                 otg_set_host(tegra->transceiver->otg, NULL);
829                 usb_put_transceiver(tegra->transceiver);
830         }
831 #endif
832
833         usb_remove_hcd(hcd);
834         usb_put_hcd(hcd);
835
836         tegra_usb_phy_close(tegra->phy);
837         iounmap(hcd->regs);
838
839         clk_disable(tegra->clk);
840         clk_put(tegra->clk);
841
842         clk_disable(tegra->emc_clk);
843         clk_put(tegra->emc_clk);
844
845         kfree(tegra);
846         return 0;
847 }
848
849 static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
850 {
851         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
852         struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
853
854         if (hcd->driver->shutdown)
855                 hcd->driver->shutdown(hcd);
856 }
857
858 static struct of_device_id tegra_ehci_of_match[] __devinitdata = {
859         { .compatible = "nvidia,tegra20-ehci", },
860         { },
861 };
862
863 static struct platform_driver tegra_ehci_driver = {
864         .probe          = tegra_ehci_probe,
865         .remove         = tegra_ehci_remove,
866         .shutdown       = tegra_ehci_hcd_shutdown,
867         .driver         = {
868                 .name   = "tegra-ehci",
869                 .of_match_table = tegra_ehci_of_match,
870 #ifdef CONFIG_PM
871                 .pm     = &tegra_ehci_pm_ops,
872 #endif
873         }
874 };