]> rtime.felk.cvut.cz Git - sojka/nv-tegra/linux-3.10.git/blob - drivers/usb/host/ehci-tegra.c
usb: host: tegra: no delay for boost frequency
[sojka/nv-tegra/linux-3.10.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-2014 NVIDIA CORPORATION. All rights reserved.
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/err.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/delay.h>
27 #include <linux/of_gpio.h>
28 #include <linux/pm_runtime.h>
29 #include <linux/tegra-soc.h>
30
31 #include <linux/usb/tegra_usb_phy.h>
32
33 #include <mach/pm_domains.h>
34 #include <linux/pm_qos.h>
35 #include <linux/wakelock.h>
36
37 /* HACK! This needs to come from DT */
38 #include "../../../arch/arm/mach-tegra/iomap.h"
39
40 #if 0
41 #define EHCI_DBG(stuff...)      pr_info("ehci-tegra: " stuff)
42 #else
43 #define EHCI_DBG(stuff...)      do {} while (0)
44 #endif
45
46 static const char driver_name[] = "tegra-ehci";
47
48 #define TEGRA_USB_DMA_ALIGN 32
49
50 #define HOSTPC_REG_OFFSET               0x1b4
51
52 #define HOSTPC1_DEVLC_STS               (1 << 28)
53 #define HOSTPC1_DEVLC_NYT_ASUS          1
54 #define TEGRA_STREAM_DISABLE    0x1f8
55 #define TEGRA_STREAM_DISABLE_OFFSET (1 << 4)
56
57 struct tegra_ehci_hcd {
58         struct ehci_hcd *ehci;
59         struct tegra_usb_phy *phy;
60         struct usb_phy *transceiver;
61         struct mutex sync_lock;
62         struct wake_lock ehci_wake_lock;
63         bool port_resuming;
64         unsigned int irq;
65         bool bus_suspended_fail;
66         bool unaligned_dma_buf_supported;
67         bool has_hostpc;
68 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
69         bool boost_enable;
70         bool boost_requested;
71         bool cpu_boost_in_work;
72         struct delayed_work boost_cpu_freq_work;
73         struct pm_qos_request boost_cpu_freq_req;
74 #endif
75 };
76
77 struct dma_align_buffer {
78         void *kmalloc_ptr;
79         void *old_xfer_buffer;
80         u8 data[0];
81 };
82
83 static struct usb_phy *get_usb_phy(struct tegra_usb_phy *x)
84 {
85         return (struct usb_phy *)x;
86 }
87
88 static int tegra_ehci_port_speed(struct ehci_hcd *ehci)
89 {
90         u32 hostpc = ehci_readl(ehci, &ehci->regs->hostpc);
91         enum usb_device_speed port_speed;
92
93         switch ((hostpc >> (ehci->has_hostpc ? 25 : 26)) & 3) {
94         case 0:
95                 port_speed = USB_SPEED_LOW;
96                 break;
97         case 1:
98                 port_speed = USB_SPEED_FULL;
99                 break;
100         case 2:
101                 port_speed = USB_SPEED_HIGH;
102                 break;
103         default:
104                 port_speed = USB_SPEED_UNKNOWN;
105         }
106         return port_speed;
107 }
108
109 static void tegra_ehci_notify_event(struct tegra_ehci_hcd *tegra, int event)
110 {
111         tegra->transceiver->last_event = event;
112         atomic_notifier_call_chain(&tegra->transceiver->notifier, event,
113                                          tegra->transceiver->otg->gadget);
114 }
115
116 static void free_align_buffer(struct urb *urb, struct usb_hcd *hcd)
117 {
118         struct dma_align_buffer *temp;
119         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
120         
121         if (tegra->unaligned_dma_buf_supported)
122                 return;
123
124         if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
125                 return;
126
127         temp = container_of(urb->transfer_buffer,
128                 struct dma_align_buffer, data);
129
130         /* In transaction, DMA from Device */
131         if (usb_urb_dir_in(urb))
132                 memcpy(temp->old_xfer_buffer, temp->data,
133                                         urb->transfer_buffer_length);
134
135         urb->transfer_buffer = temp->old_xfer_buffer;
136         urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
137         kfree(temp->kmalloc_ptr);
138 }
139
140 static int alloc_align_buffer(struct urb *urb, gfp_t mem_flags,
141         struct usb_hcd *hcd)
142 {
143         struct dma_align_buffer *temp, *kmalloc_ptr;
144         size_t kmalloc_size;
145         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
146
147         if (tegra->unaligned_dma_buf_supported)
148                 return 0;
149
150         if (urb->num_sgs || urb->sg ||
151                 urb->transfer_buffer_length == 0 ||
152                 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
153                 return 0;
154
155         /* Allocate a buffer with enough padding for alignment */
156         kmalloc_size = urb->transfer_buffer_length +
157                 sizeof(struct dma_align_buffer) + TEGRA_USB_DMA_ALIGN - 1;
158         kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
159
160         if (!kmalloc_ptr)
161                 return -ENOMEM;
162
163         /* Position our struct dma_align_buffer such that data is aligned */
164         temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
165         temp->kmalloc_ptr = kmalloc_ptr;
166         temp->old_xfer_buffer = urb->transfer_buffer;
167         /* OUT transaction, DMA to Device */
168         if (!usb_urb_dir_in(urb))
169                 memcpy(temp->data, urb->transfer_buffer,
170                                 urb->transfer_buffer_length);
171
172         urb->transfer_buffer = temp->data;
173         urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
174
175         return 0;
176 }
177
178 static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd,
179         struct urb *urb, gfp_t mem_flags)
180 {
181         int ret;
182
183         ret = alloc_align_buffer(urb, mem_flags, hcd);
184         if (ret)
185                 return ret;
186
187         ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
188
189         if (ret)
190                 free_align_buffer(urb, hcd);
191
192         return ret;
193 }
194
195 static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd,
196         struct urb *urb)
197 {
198         usb_hcd_unmap_urb_for_dma(hcd, urb);
199         free_align_buffer(urb, hcd);
200 }
201
202 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
203 static void tegra_ehci_boost_cpu_frequency_work(struct work_struct *work)
204 {
205         struct tegra_ehci_hcd *tegra = container_of(work,
206                 struct tegra_ehci_hcd, boost_cpu_freq_work.work);
207         if (tegra->cpu_boost_in_work) {
208                 tegra->boost_requested = true;
209                 if (tegra->boost_enable)
210                         pm_qos_update_request(
211                                 &tegra->boost_cpu_freq_req,
212                                 (s32)CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ * 1000);
213         }
214 }
215 #endif
216
217 static irqreturn_t tegra_ehci_irq(struct usb_hcd *hcd)
218 {
219         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
220         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
221         irqreturn_t irq_status;
222
223         spin_lock(&ehci->lock);
224         irq_status = tegra_usb_phy_irq(tegra->phy);
225         if (irq_status == IRQ_NONE) {
226                 spin_unlock(&ehci->lock);
227                 return irq_status;
228         }
229         if (tegra_usb_phy_pmc_wakeup(tegra->phy)) {
230                 ehci_dbg(ehci, "pmc interrupt detected\n");
231                 wake_lock_timeout(&tegra->ehci_wake_lock, HZ);
232                 usb_hcd_resume_root_hub(hcd);
233                 spin_unlock(&ehci->lock);
234                 return irq_status;
235         }
236         spin_unlock(&ehci->lock);
237
238         EHCI_DBG("%s() cmd = 0x%x, int_sts = 0x%x, portsc = 0x%x\n", __func__,
239                 ehci_readl(ehci, &ehci->regs->command),
240                 ehci_readl(ehci, &ehci->regs->status),
241                 ehci_readl(ehci, &ehci->regs->port_status[0]));
242
243         irq_status = ehci_irq(hcd);
244
245         if (ehci->controller_remote_wakeup) {
246                 ehci->controller_remote_wakeup = false;
247                 tegra_usb_phy_pre_resume(tegra->phy, true);
248                 tegra->port_resuming = 1;
249         }
250         return irq_status;
251 }
252
253
254 static int tegra_ehci_hub_control(
255         struct usb_hcd  *hcd,
256         u16     typeReq,
257         u16     wValue,
258         u16     wIndex,
259         char    *buf,
260         u16     wLength
261 )
262 {
263         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
264         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
265         int     retval = 0;
266         u32 __iomem     *status_reg;
267
268         if (!tegra_usb_phy_hw_accessible(tegra->phy)) {
269                 if (buf)
270                         memset(buf, 0, wLength);
271                 return retval;
272         }
273
274         /* Do tegra phy specific actions based on the type request */
275         switch (typeReq) {
276         case GetPortStatus:
277                 if (tegra->port_resuming) {
278                         u32 cmd;
279                         int delay = ehci->reset_done[wIndex-1] - jiffies;
280                         /* Sometimes it seems we get called too soon... In that case, wait.*/
281                         if (delay > 0) {
282                                 ehci_dbg(ehci, "GetPortStatus called too soon, waiting %dms...\n", delay);
283                                 mdelay(jiffies_to_msecs(delay));
284                         }
285                         status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
286                         /* Ensure the port PORT_SUSPEND and PORT_RESUME has cleared */
287                         if (handshake(ehci, status_reg, (PORT_SUSPEND | PORT_RESUME), 0, 25000)) {
288                                 EHCI_DBG("%s: timeout waiting for SUSPEND to clear\n", __func__);
289                         }
290                         tegra_usb_phy_post_resume(tegra->phy);
291                         tegra->port_resuming = 0;
292                         /* If run bit is not set by now enable it */
293                         cmd = ehci_readl(ehci, &ehci->regs->command);
294                         if (!(cmd & CMD_RUN)) {
295                                 cmd |= CMD_RUN;
296                                 ehci->command |= CMD_RUN;
297                                 ehci_writel(ehci, cmd, &ehci->regs->command);
298                         }
299                         /* Now we can safely re-enable irqs */
300                         ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
301                 }
302                 break;
303         case ClearPortFeature:
304                 if (wValue == USB_PORT_FEAT_SUSPEND) {
305                         tegra_usb_phy_pre_resume(tegra->phy, false);
306                         tegra->port_resuming = 1;
307                 } else if (wValue == USB_PORT_FEAT_ENABLE) {
308                         u32 temp;
309                         temp = ehci_readl(ehci, &ehci->regs->port_status[0]) & ~PORT_RWC_BITS;
310                         ehci_writel(ehci, temp & ~PORT_PE, &ehci->regs->port_status[0]);
311                         return retval;
312                 }
313                 break;
314         case SetPortFeature:
315                 if (wValue == USB_PORT_FEAT_SUSPEND)
316                         tegra_usb_phy_pre_suspend(tegra->phy);
317                 break;
318         }
319
320         /* handle ehci hub control request */
321         retval = ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
322
323         /* do tegra phy specific actions based on the type request */
324         if (!retval) {
325                 switch (typeReq) {
326                 case SetPortFeature:
327                         if (wValue == USB_PORT_FEAT_SUSPEND) {
328                                 tegra_usb_phy_post_suspend(tegra->phy);
329                         } else if (wValue == USB_PORT_FEAT_RESET) {
330                                 if (wIndex == 1)
331                                         tegra_usb_phy_bus_reset(tegra->phy);
332                         } else if (wValue == USB_PORT_FEAT_POWER) {
333                                 if (wIndex == 1)
334                                         tegra_usb_phy_port_power(tegra->phy);
335                         }
336                         break;
337                 case ClearPortFeature:
338                         if (wValue == USB_PORT_FEAT_SUSPEND) {
339                                 /* tegra USB controller needs 25 ms to resume the port */
340                                 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
341                         }
342                         break;
343                 }
344         }
345
346         return retval;
347 }
348
349 static void tegra_ehci_shutdown(struct usb_hcd *hcd)
350 {
351         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
352         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
353         struct platform_device *pdev = to_platform_device(hcd->self.controller);
354         struct tegra_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
355         mutex_lock(&tegra->sync_lock);
356         if (tegra_usb_phy_hw_accessible(tegra->phy)) {
357                 ehci_silence_controller(ehci);
358         }
359         if (pdata->port_otg)
360                 tegra_usb_enable_vbus(tegra->phy, false);
361         mutex_unlock(&tegra->sync_lock);
362 }
363
364 static int tegra_ehci_setup(struct usb_hcd *hcd)
365 {
366         struct ehci_hcd *ehci = hcd_to_ehci(hcd);
367         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
368         int retval;
369 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
370         u32 val;
371 #endif
372
373         /* EHCI registers start at offset 0x100 */
374         ehci->caps = hcd->regs + 0x100;
375
376         ehci->has_hostpc = tegra->has_hostpc;
377         ehci->broken_hostpc_phcd = true;
378
379 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
380         ehci->has_hostpc = 1;
381
382         val = readl(hcd->regs + HOSTPC_REG_OFFSET);
383         val &= ~HOSTPC1_DEVLC_STS;
384         val &= ~HOSTPC1_DEVLC_NYT_ASUS;
385         writel(val, hcd->regs + HOSTPC_REG_OFFSET);
386 #endif
387         /* switch to host mode */
388         hcd->has_tt = 1;
389
390         retval = ehci_setup(hcd);
391         if (retval)
392                 return retval;
393
394         ehci->controller_remote_wakeup = false;
395         tegra_usb_phy_reset(tegra->phy);
396
397 #if !defined(CONFIG_ARCH_TEGRA_2x_SOC)
398         if (tegra_platform_is_fpga()) {
399                 val =  readl(hcd->regs + TEGRA_STREAM_DISABLE);
400                 val |= TEGRA_STREAM_DISABLE_OFFSET;
401                 writel(val , hcd->regs + TEGRA_STREAM_DISABLE);
402         }
403 #endif
404
405         return 0;
406 }
407
408
409 #ifdef CONFIG_PM
410 static int tegra_ehci_bus_suspend(struct usb_hcd *hcd)
411 {
412         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
413         int err = 0;
414         EHCI_DBG("%s() BEGIN\n", __func__);
415
416 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
417         tegra->boost_requested = false;
418         if (tegra->boost_enable
419             && pm_qos_request_active(&tegra->boost_cpu_freq_req))
420                 pm_qos_update_request(&tegra->boost_cpu_freq_req,
421                         PM_QOS_DEFAULT_VALUE);
422         tegra->cpu_boost_in_work = false;
423 #endif
424
425         mutex_lock(&tegra->sync_lock);
426         tegra->bus_suspended_fail = false;
427         err = ehci_bus_suspend(hcd);
428         if (err)
429                 tegra->bus_suspended_fail = true;
430         else
431                 usb_phy_set_suspend(get_usb_phy(tegra->phy), 1);
432         mutex_unlock(&tegra->sync_lock);
433         EHCI_DBG("%s() END\n", __func__);
434
435         return err;
436 }
437
438 static int tegra_ehci_bus_resume(struct usb_hcd *hcd)
439 {
440         struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
441         int err = 0;
442         EHCI_DBG("%s() BEGIN\n", __func__);
443
444         mutex_lock(&tegra->sync_lock);
445         usb_phy_set_suspend(get_usb_phy(tegra->phy), 0);
446         err = ehci_bus_resume(hcd);
447         mutex_unlock(&tegra->sync_lock);
448
449 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
450         tegra->boost_requested = true;
451         if (pm_qos_request_active(&tegra->boost_cpu_freq_req)
452             && tegra->boost_enable
453             && (tegra_ehci_port_speed(tegra->ehci) == USB_SPEED_HIGH))
454                 pm_qos_update_request(&tegra->boost_cpu_freq_req,
455                         (s32)CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ * 1000);
456         tegra->cpu_boost_in_work = false;
457 #endif
458         EHCI_DBG("%s() END\n", __func__);
459         return err;
460 }
461 #endif
462
463 static const struct hc_driver tegra_ehci_hc_driver = {
464         .description            = hcd_name,
465         .product_desc           = "Tegra EHCI Host Controller",
466         .hcd_priv_size          = sizeof(struct ehci_hcd),
467         .flags                  = HCD_USB2 | HCD_MEMORY,
468
469         /* standard ehci functions */
470         .start                  = ehci_run,
471         .stop                   = ehci_stop,
472         .urb_enqueue            = ehci_urb_enqueue,
473         .urb_dequeue            = ehci_urb_dequeue,
474         .endpoint_disable       = ehci_endpoint_disable,
475         .endpoint_reset = ehci_endpoint_reset,
476         .get_frame_number       = ehci_get_frame,
477         .hub_status_data        = ehci_hub_status_data,
478         .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
479         .relinquish_port        = ehci_relinquish_port,
480         .port_handed_over       = ehci_port_handed_over,
481
482         /* modified ehci functions for tegra */
483         .reset                  = tegra_ehci_setup,
484         .irq                    = tegra_ehci_irq,
485         .shutdown               = tegra_ehci_shutdown,
486         .map_urb_for_dma        = tegra_ehci_map_urb_for_dma,
487         .unmap_urb_for_dma      = tegra_ehci_unmap_urb_for_dma,
488         .hub_control            = tegra_ehci_hub_control,
489 #ifdef CONFIG_PM
490         .bus_suspend    = tegra_ehci_bus_suspend,
491         .bus_resume     = tegra_ehci_bus_resume,
492 #endif
493 };
494
495 static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
496
497 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
498 static ssize_t show_boost_enable(struct device *dev,
499                                  struct device_attribute *attr,
500                                  char *buf)
501 {
502         struct tegra_ehci_hcd *tegra = dev_get_drvdata(dev);
503         return scnprintf(buf, PAGE_SIZE, "%s\n",
504                          tegra->boost_enable ? "Y" : "N");
505 }
506
507
508 static ssize_t store_boost_enable(struct device *dev,
509                                   struct device_attribute *attr,
510                                   const char *buf, size_t count)
511 {
512         struct tegra_ehci_hcd *tegra = dev_get_drvdata(dev);
513         bool new_boost;
514         bool old_boost = tegra->boost_enable;
515
516         if (strtobool(buf, &new_boost) == 0) {
517                 tegra->boost_enable = new_boost;
518                 if (!old_boost && new_boost
519                     && tegra->boost_requested
520                     && pm_qos_request_active(&tegra->boost_cpu_freq_req))
521                         pm_qos_update_request(
522                                 &tegra->boost_cpu_freq_req,
523                                 (s32)CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ * 1000);
524                 else if (old_boost && !new_boost
525                          && pm_qos_request_active(&tegra->boost_cpu_freq_req))
526                         pm_qos_update_request(&tegra->boost_cpu_freq_req,
527                                               PM_QOS_DEFAULT_VALUE);
528         }
529         return count;
530 }
531 static DEVICE_ATTR(boost_enable, 0644,
532                    show_boost_enable, store_boost_enable);
533 #endif
534
535 static int tegra_ehci_probe(struct platform_device *pdev)
536 {
537         struct resource *res;
538         struct usb_hcd *hcd;
539         struct tegra_ehci_hcd *tegra;
540         struct tegra_usb_platform_data *pdata;
541         int err = 0;
542         int irq;
543         int instance = pdev->id;
544
545         /* Right now device-tree probed devices don't get dma_mask set.
546          * Since shared usb code relies on it, set it here for now.
547          * Once we have dma capability bindings this can go away.
548          */
549         if (!pdev->dev.dma_mask)
550                 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
551
552         pdata = dev_get_platdata(&pdev->dev);
553
554         tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
555                              GFP_KERNEL);
556         if (!tegra) {
557                 dev_err(&pdev->dev, "memory alloc failed\n");
558                 return -ENOMEM;
559         }
560
561         mutex_init(&tegra->sync_lock);
562
563         hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
564                                         dev_name(&pdev->dev));
565         if (!hcd) {
566                 dev_err(&pdev->dev, "unable to create HCD\n");
567                 return -ENOMEM;
568         }
569
570         platform_set_drvdata(pdev, tegra);
571
572 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
573         tegra->boost_requested = false;
574         /* Add boost enable/disable knob */
575         tegra->boost_enable = true;
576         err = device_create_file(hcd->self.controller, &dev_attr_boost_enable);
577         if (err < 0)
578                 goto fail_sysfs;
579 #endif
580
581         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
582         if (!res) {
583                 dev_err(&pdev->dev, "failed to get I/O memory\n");
584                 err = -ENXIO;
585                 goto fail_io;
586         }
587         hcd->rsrc_start = res->start;
588         hcd->rsrc_len = resource_size(res);
589         hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
590         if (!hcd->regs) {
591                 dev_err(&pdev->dev, "failed to remap I/O memory\n");
592                 err = -ENOMEM;
593                 goto fail_io;
594         }
595
596         /* This is pretty ugly and needs to be fixed when we do only
597          * device-tree probing. Old code relies on the platform_device
598          * numbering that we lack for device-tree-instantiated devices.
599          */
600         if (instance < 0) {
601                 switch (res->start) {
602                 case TEGRA_USB_BASE:
603                         instance = 0;
604                         break;
605                 case TEGRA_USB2_BASE:
606                         instance = 1;
607                         break;
608                 case TEGRA_USB3_BASE:
609                         instance = 2;
610                         break;
611                 default:
612                         err = -ENODEV;
613                         dev_err(&pdev->dev, "unknown usb instance\n");
614                         goto fail_io;
615                 }
616         }
617
618         irq = platform_get_irq(pdev, 0);
619         if (irq < 0) {
620                 dev_err(&pdev->dev, "failed to get IRQ\n");
621                 err = -ENODEV;
622                 goto fail_io;
623         }
624         tegra->irq = irq;
625
626         tegra->unaligned_dma_buf_supported = pdata->unaligned_dma_buf_supported;
627         tegra->has_hostpc = pdata->has_hostpc;
628
629         tegra->phy = tegra_usb_phy_open(pdev);
630         hcd->phy = get_usb_phy(tegra->phy);
631         if (IS_ERR(tegra->phy)) {
632                 dev_err(&pdev->dev, "failed to open USB phy\n");
633                 err = -ENXIO;
634                 goto fail_io;
635         }
636
637         err = tegra_usb_phy_power_on(tegra->phy);
638         if (err) {
639                 dev_err(&pdev->dev, "failed to power on the phy\n");
640                 goto fail_phy;
641         }
642
643         err = usb_phy_init(get_usb_phy(tegra->phy));
644         if (err) {
645                 dev_err(&pdev->dev, "failed to init the phy\n");
646                 goto fail_phy;
647         }
648
649         err = usb_add_hcd(hcd, irq, IRQF_SHARED | IRQF_TRIGGER_HIGH);
650         if (err) {
651                 dev_err(&pdev->dev, "Failed to add USB HCD, error=%d\n", err);
652                 goto fail_phy;
653         }
654
655         tegra->ehci = hcd_to_ehci(hcd);
656
657         if (pdata->port_otg) {
658                 tegra->transceiver =
659                         devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
660                 if (!IS_ERR_OR_NULL(tegra->transceiver))
661                         otg_set_host(tegra->transceiver->otg, &hcd->self);
662         }
663
664         tegra_pd_add_device(&pdev->dev);
665         pm_runtime_enable(&pdev->dev);
666
667 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
668         INIT_DELAYED_WORK(&tegra->boost_cpu_freq_work,
669                                         tegra_ehci_boost_cpu_frequency_work);
670         pm_qos_add_request(&tegra->boost_cpu_freq_req, PM_QOS_CPU_FREQ_MIN,
671                                         PM_QOS_DEFAULT_VALUE);
672         schedule_delayed_work(&tegra->boost_cpu_freq_work, 12000);
673         tegra->cpu_boost_in_work = true;
674 #endif
675
676         wake_lock_init(&tegra->ehci_wake_lock,
677                        WAKE_LOCK_SUSPEND, dev_name(&pdev->dev));
678
679         return err;
680
681 fail_phy:
682         usb_phy_shutdown(get_usb_phy(tegra->phy));
683 fail_io:
684 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
685         device_remove_file(hcd->self.controller, &dev_attr_boost_enable);
686 fail_sysfs:
687 #endif
688         usb_put_hcd(hcd);
689
690         return err;
691 }
692
693
694 #ifdef CONFIG_PM
695 static int tegra_ehci_resume(struct platform_device *pdev)
696 {
697         int err = 0;
698         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
699         struct tegra_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
700         if (tegra->irq) {
701                 err = disable_irq_wake(tegra->irq);
702                 if (err < 0)
703                         dev_err(&pdev->dev,
704                                 "Couldn't disable USB host mode wakeup, irq=%d, "
705                                 "error=%d\n", tegra->irq, err);
706         }
707         if (pdata->u_data.host.turn_off_vbus_on_lp0) {
708                 tegra_usb_enable_vbus(tegra->phy, true);
709                 tegra_ehci_notify_event(tegra, USB_EVENT_ID);
710         }
711         return tegra_usb_phy_power_on(tegra->phy);
712 }
713
714 static int tegra_ehci_suspend(struct platform_device *pdev, pm_message_t state)
715 {
716         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
717         struct tegra_usb_platform_data *pdata = dev_get_platdata(&pdev->dev);
718         int err;
719
720         /* bus suspend could have failed because of remote wakeup resume */
721         if (tegra->bus_suspended_fail)
722                 return -EBUSY;
723         else {
724                 err = tegra_usb_phy_power_off(tegra->phy);
725                 if (err < 0)
726                         return err;
727                 if (pdata->u_data.host.turn_off_vbus_on_lp0) {
728                         tegra_usb_enable_vbus(tegra->phy, false);
729                         tegra_usb_phy_pmc_disable(tegra->phy);
730                 }
731                 if (tegra->irq) {
732                         err = enable_irq_wake(tegra->irq);
733                         if (err < 0)
734                                 dev_err(&pdev->dev,
735                                         "Couldn't enable USB host mode wakeup, irq=%d, "
736                                         "error=%d\n", tegra->irq, err);
737                 }
738                 return err;
739         }
740 }
741 #endif
742
743 static int tegra_ehci_remove(struct platform_device *pdev)
744 {
745         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
746         struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
747         struct usb_device *rhdev = NULL;
748         struct tegra_usb_platform_data *pdata;
749         unsigned long timeout = 0;
750
751         wake_lock_destroy(&tegra->ehci_wake_lock);
752
753 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
754         cancel_delayed_work_sync(&tegra->boost_cpu_freq_work);
755         tegra->cpu_boost_in_work = false;
756         pm_qos_remove_request(&tegra->boost_cpu_freq_req);
757 #endif
758         rhdev = hcd->self.root_hub;
759         pdata = dev_get_platdata(&pdev->dev);
760
761         if (!IS_ERR_OR_NULL(tegra->transceiver))
762                 otg_set_host(tegra->transceiver->otg, NULL);
763
764         /* Make sure phy is powered ON to access USB register */
765         if(!tegra_usb_phy_hw_accessible(tegra->phy))
766                 tegra_usb_phy_power_on(tegra->phy);
767
768         if (pdata->port_otg) {
769                 timeout = jiffies + 5 * HZ;
770                 /* wait for devices connected to root hub to disconnect*/
771                 while (rhdev && usb_hub_find_child(rhdev, 1)) {
772                         /* wait for any control packets
773                         sent to root hub to complete */
774                         if (time_after(jiffies, timeout))
775                                 break;
776                         msleep(20);
777                         cpu_relax();
778                 }
779         }
780
781 #ifdef CONFIG_TEGRA_EHCI_BOOST_CPU_FREQ
782         device_remove_file(hcd->self.controller, &dev_attr_boost_enable);
783 #endif
784         usb_remove_hcd(hcd);
785         usb_put_hcd(hcd);
786         tegra_usb_phy_power_off(tegra->phy);
787         usb_phy_shutdown(get_usb_phy(tegra->phy));
788
789         mutex_destroy(&tegra->sync_lock);
790         tegra_pd_remove_device(&pdev->dev);
791
792         return 0;
793 }
794
795 static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
796 {
797         struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
798         struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
799
800         if (hcd->driver->shutdown)
801                 hcd->driver->shutdown(hcd);
802 }
803
804 static struct of_device_id tegra_ehci_of_match[] = {
805         { .compatible = "nvidia,tegra20-ehci", },
806         { },
807 };
808
809 static struct platform_driver tegra_ehci_driver = {
810         .probe          = tegra_ehci_probe,
811         .remove         = tegra_ehci_remove,
812         .shutdown       = tegra_ehci_hcd_shutdown,
813 #ifdef CONFIG_PM
814         .suspend = tegra_ehci_suspend,
815         .resume  = tegra_ehci_resume,
816 #endif
817         .driver = {
818                 .name   = driver_name,
819                 .of_match_table = tegra_ehci_of_match,
820         }
821 };