2 * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
4 * Copyright (C) 2010 Google, Inc.
5 * Copyright (C) 2009 - 2011 NVIDIA Corporation
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.
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
19 #include <linux/clk.h>
20 #include <linux/err.h>
21 #include <linux/platform_device.h>
22 #include <linux/platform_data/tegra_usb.h>
23 #include <linux/irq.h>
24 #include <linux/usb/otg.h>
25 #include <linux/gpio.h>
27 #include <linux/of_gpio.h>
28 #include <linux/pm_runtime.h>
30 #include <linux/usb/tegra_usb_phy.h>
32 #define TEGRA_USB_BASE 0xC5000000
33 #define TEGRA_USB2_BASE 0xC5004000
34 #define TEGRA_USB3_BASE 0xC5008000
36 #define TEGRA_USB_PORTSC_PHCD (1 << 23)
38 #define TEGRA_USB_SUSP_CTRL_OFFSET 0x400
39 #define TEGRA_USB_SUSP_CLR (1 << 5)
40 #define TEGRA_USB_PHY_CLK_VALID (1 << 7)
41 #define TEGRA_USB_SRT (1 << 25)
42 #define TEGRA_USB_PHY_CLK_VALID_INT_ENB (1 << 9)
43 #define TEGRA_USB_PHY_CLK_VALID_INT_STS (1 << 8)
45 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
46 #define TEGRA_USB_PORTSC1_OFFSET 0x184
48 #define TEGRA_USB_PORTSC1_OFFSET 0x174
50 #define TEGRA_USB_PORTSC1_WKCN (1 << 20)
52 #define TEGRA_LVL2_CLK_GATE_OVRB 0xfc
53 #define TEGRA_USB2_CLK_OVR_ON (1 << 10)
55 #define TEGRA_USB_DMA_ALIGN 32
57 #define STS_SRI (1<<7) /* SOF Recieved */
59 #define TEGRA_HSIC_CONNECTION_MAX_RETRIES 5
60 #define HOSTPC_REG_OFFSET 0x1b4
62 #define HOSTPC1_DEVLC_STS (1 << 28)
63 #define HOSTPC1_DEVLC_PTS(x) (((x) & 0x7) << 29)
65 struct tegra_ehci_hcd {
66 struct ehci_hcd *ehci;
67 struct tegra_usb_phy *phy;
71 struct usb_phy *transceiver;
75 struct delayed_work work;
76 enum tegra_usb_phy_port_speed port_speed;
77 struct work_struct clk_timer_work;
78 struct timer_list clk_timer;
81 int hsic_connect_retries;
85 static void tegra_ehci_power_up(struct usb_hcd *hcd)
87 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
89 if (!tegra->default_enable)
90 clk_enable(tegra->clk);
91 usb_phy_set_suspend(&tegra->phy->u_phy, 0);
92 tegra->host_resumed = 1;
95 static void tegra_ehci_power_down(struct usb_hcd *hcd)
97 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
99 tegra->host_resumed = 0;
100 usb_phy_set_suspend(&tegra->phy->u_phy, 1);
101 if (!tegra->default_enable)
102 clk_disable(tegra->clk);
105 static int tegra_ehci_internal_port_reset(
106 struct ehci_hcd *ehci,
107 u32 __iomem *portsc_reg
116 spin_lock_irqsave(&ehci->lock, flags);
117 saved_usbintr = ehci_readl(ehci, &ehci->regs->intr_enable);
118 /* disable USB interrupt */
119 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
120 spin_unlock_irqrestore(&ehci->lock, flags);
123 * Here we have to do Port Reset at most twice for
124 * Port Enable bit to be set.
126 for (i = 0; i < 2; i++) {
127 temp = ehci_readl(ehci, portsc_reg);
129 ehci_writel(ehci, temp, portsc_reg);
132 ehci_writel(ehci, temp, portsc_reg);
138 * Up to this point, Port Enable bit is
139 * expected to be set after 2 ms waiting.
140 * USB1 usually takes extra 45 ms, for safety,
141 * we take 100 ms as timeout.
143 temp = ehci_readl(ehci, portsc_reg);
144 } while (!(temp & PORT_PE) && tries--);
152 * Clear Connect Status Change bit if it's set.
153 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
156 ehci_writel(ehci, PORT_CSC, portsc_reg);
159 * Write to clear any interrupt status bits that might be set
162 temp = ehci_readl(ehci, &ehci->regs->status);
163 ehci_writel(ehci, temp, &ehci->regs->status);
165 /* restore original interrupt enable bits */
166 ehci_writel(ehci, saved_usbintr, &ehci->regs->intr_enable);
170 static irqreturn_t tegra_ehci_irq (struct usb_hcd *hcd)
172 struct ehci_hcd *ehci = hcd_to_ehci (hcd);
173 struct ehci_regs __iomem *hw = ehci->regs;
174 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
176 irqreturn_t irq_status;
177 bool pmc_remote_wakeup = false;
179 if ((tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) &&
180 (tegra->ehci->has_hostpc)) {
181 /* check if there is any remote wake event */
182 if (tegra_usb_phy_is_remotewake_detected(tegra->phy)) {
183 pmc_remote_wakeup = true;
184 spin_lock (&ehci->lock);
185 usb_hcd_resume_root_hub(hcd);
186 spin_unlock (&ehci->lock);
189 if (tegra->phy->hotplug) {
190 spin_lock(&ehci->lock);
191 val = readl(hcd->regs + TEGRA_USB_SUSP_CTRL_OFFSET);
192 if ((val & TEGRA_USB_PHY_CLK_VALID_INT_STS)) {
193 val &= ~TEGRA_USB_PHY_CLK_VALID_INT_ENB |
194 TEGRA_USB_PHY_CLK_VALID_INT_STS;
195 writel(val , (hcd->regs + TEGRA_USB_SUSP_CTRL_OFFSET));
197 val = readl(&hw->status);
198 if (!(val & STS_PCD)) {
199 spin_unlock(&ehci->lock);
202 val = readl(hcd->regs + TEGRA_USB_PORTSC1_OFFSET);
203 val &= ~(TEGRA_USB_PORTSC1_WKCN | PORT_RWC_BITS);
204 writel(val , (hcd->regs + TEGRA_USB_PORTSC1_OFFSET));
206 spin_unlock(&ehci->lock);
209 irq_status = ehci_irq(hcd);
211 if (pmc_remote_wakeup) {
212 ehci->controller_remote_wakeup = false;
215 if (ehci->controller_remote_wakeup) {
216 ehci->controller_remote_wakeup = false;
217 /* disable interrupts */
218 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
219 tegra_usb_phy_preresume(tegra->phy, true);
220 tegra->port_resuming = 1;
225 static int tegra_ehci_hub_control(
234 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
235 int ports = HCS_N_PORTS(ehci->hcs_params);
237 u32 __iomem *status_reg;
243 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
245 bool do_post_resume = false;
247 if (!tegra->host_resumed) {
249 memset (buf, 0, wLength);
253 hsic = (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_HSIC);
255 status_reg = &ehci->regs->port_status[(wIndex & 0xff) - 1];
257 spin_lock_irqsave(&ehci->lock, flags);
259 if (typeReq == GetPortStatus) {
260 temp = ehci_readl(ehci, status_reg);
261 if (tegra->port_resuming && !(temp & PORT_SUSPEND) &&
262 time_after_eq(jiffies, ehci->reset_done[wIndex-1])) {
263 clear_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
264 ehci->reset_done[wIndex-1] = 0;
265 do_post_resume = true;
266 } else if (tegra->port_resuming && (temp & PORT_RESUME) &&
267 time_after_eq(jiffies, ehci->reset_done[wIndex-1]) ) {
268 do_post_resume = true;
271 if (do_post_resume) {
272 clear_bit(wIndex-1, &ehci->resuming_ports);
273 tegra->port_resuming = 0;
274 tegra_usb_phy_postresume(tegra->phy, false);
275 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) {
276 ehci->command |= CMD_RUN;
278 * ehci run bit is disabled to avoid SOF.
279 * 2LS WAR is executed by now enable the run bit.
281 ehci_writel(ehci, ehci->command,
282 &ehci->regs->command);
283 /* Now we can safely re-enable irqs */
284 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
288 } else if (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_SUSPEND) {
289 temp = ehci_readl(ehci, status_reg);
290 if ((temp & PORT_PE) == 0 || (temp & PORT_RESET) != 0) {
295 temp &= ~(PORT_RWC_BITS | PORT_WKCONN_E);
296 temp |= PORT_WKDISC_E | PORT_WKOC_E;
297 ehci_writel(ehci, temp | PORT_SUSPEND, status_reg);
299 /* Need a 4ms delay before the controller goes to suspend */
303 * If a transaction is in progress, there may be a delay in
304 * suspending the port. Poll until the port is suspended.
306 if (handshake(ehci, status_reg, PORT_SUSPEND,
308 pr_err("%s: timeout waiting for SUSPEND\n", __func__);
310 set_bit((wIndex & 0xff) - 1, &ehci->suspended_ports);
312 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) {
313 /* Disable RUN bit. */
314 ehci->command &= ~CMD_RUN;
315 ehci_writel(ehci, ehci->command,
316 &ehci->regs->command);
319 tegra_usb_phy_postsuspend(tegra->phy, false);
324 /* For USB1 port we need to issue Port Reset twice internally */
325 if (tegra->phy->instance == 0 &&
326 (typeReq == SetPortFeature && wValue == USB_PORT_FEAT_RESET)) {
327 spin_unlock_irqrestore(&ehci->lock, flags);
328 return tegra_ehci_internal_port_reset(ehci, status_reg);
332 * Tegra host controller will time the resume operation to clear the bit
333 * when the port control state switches to HS or FS Idle. This behavior
334 * is different from EHCI where the host controller driver is required
335 * to set this bit to a zero after the resume duration is timed in the
338 else if (typeReq == ClearPortFeature &&
339 wValue == USB_PORT_FEAT_SUSPEND) {
340 temp = ehci_readl(ehci, status_reg);
341 if ((temp & PORT_RESET) || !(temp & PORT_PE)) {
346 if (!(temp & PORT_SUSPEND))
349 tegra->port_resuming = 1;
351 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) {
352 /* disable interrupts */
353 ehci_writel(ehci, 0, &ehci->regs->intr_enable);
354 /* Disable RUN bit. */
355 ehci->command &= ~CMD_RUN;
356 ehci_writel(ehci, ehci->command,
357 &ehci->regs->command);
360 /* Disable disconnect detection during port resume */
361 tegra_usb_phy_preresume(tegra->phy, false);
362 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
363 if (tegra->phy->usb_phy_type != TEGRA_USB_PHY_TYPE_UTMIP) {
365 ehci_dbg(ehci, "%s:USBSTS = 0x%x", __func__,
366 ehci_readl(ehci, &ehci->regs->status));
367 usbsts_reg = ehci_readl(ehci, &ehci->regs->status);
368 ehci_writel(ehci, usbsts_reg, &ehci->regs->status);
369 usbsts_reg = ehci_readl(ehci, &ehci->regs->status);
372 if (handshake(ehci, &ehci->regs->status, STS_SRI, STS_SRI, 2000))
373 pr_err("%s: timeout set for STS_SRI\n", __func__);
375 usbsts_reg = ehci_readl(ehci, &ehci->regs->status);
376 ehci_writel(ehci, usbsts_reg, &ehci->regs->status);
378 if (handshake(ehci, &ehci->regs->status, STS_SRI, 0, 2000))
379 pr_err("%s: timeout clear STS_SRI\n", __func__);
381 if (handshake(ehci, &ehci->regs->status, STS_SRI, STS_SRI, 2000))
382 pr_err("%s: timeout set STS_SRI\n", __func__);
385 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
388 temp &= ~(PORT_RWC_BITS | PORT_WAKE_BITS);
389 /* start resume signaling */
390 ehci_writel(ehci, temp | PORT_RESUME, status_reg);
391 set_bit(wIndex-1, &ehci->resuming_ports);
393 ehci->reset_done[wIndex-1] = jiffies + msecs_to_jiffies(25);
394 /* whoever resumes must GetPortStatus to complete it!! */
398 /* Handle port reset here */
399 if ((hsic) && (typeReq == SetPortFeature) &&
400 ((wValue == USB_PORT_FEAT_RESET) || (wValue == USB_PORT_FEAT_POWER))) {
401 selector = wIndex >> 8;
403 if (!wIndex || wIndex > ports) {
409 temp = ehci_readl(ehci, status_reg);
410 if (temp & PORT_OWNER)
412 temp &= ~PORT_RWC_BITS;
415 case USB_PORT_FEAT_RESET:
417 if (temp & PORT_RESUME) {
421 /* line status bits may report this as low speed,
422 * which can be fine if this root hub has a
423 * transaction translator built in.
425 if ((temp & (PORT_PE|PORT_CONNECT)) == PORT_CONNECT
426 && !ehci_is_TDI(ehci) && PORT_USB11 (temp)) {
427 ehci_dbg (ehci, "port %d low speed --> companion\n", wIndex + 1);
429 ehci_writel(ehci, temp, status_reg);
431 ehci_vdbg(ehci, "port %d reset\n", wIndex + 1);
434 * caller must wait, then call GetPortStatus
435 * usb 2.0 spec says 50 ms resets on root
437 ehci->reset_done[wIndex] = jiffies + msecs_to_jiffies(50);
438 ehci_writel(ehci, temp, status_reg);
439 if (hsic && (wIndex == 0))
440 tegra_usb_phy_bus_reset(tegra->phy);
445 case USB_PORT_FEAT_POWER:
447 if (HCS_PPC(ehci->hcs_params))
448 ehci_writel(ehci, temp | PORT_POWER, status_reg);
449 if (hsic && (wIndex == 0))
450 tegra_usb_phy_bus_connect(tegra->phy);
457 spin_unlock_irqrestore(&ehci->lock, flags);
459 /* Handle the hub control events here */
460 return ehci_hub_control(hcd, typeReq, wValue, wIndex, buf, wLength);
462 spin_unlock_irqrestore(&ehci->lock, flags);
467 static void tegra_ehci_restart(struct usb_hcd *hcd, bool is_dpd)
469 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
470 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
473 ehci->controller_resets_phy = 0;
474 tegra_ehci_pre_reset(tegra->phy, false);
476 tegra_ehci_post_reset(tegra->phy, false);
478 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_NULL_ULPI)
479 ehci->controller_resets_phy = 1;
481 /* setup the frame list and Async q heads */
482 ehci_writel(ehci, ehci->periodic_dma, &ehci->regs->frame_list);
483 ehci_writel(ehci, (u32)ehci->async->qh_dma, &ehci->regs->async_next);
484 /* setup the command register and set the controller in RUN mode */
485 ehci->command &= ~(CMD_LRESET|CMD_IAAD|CMD_PSE|CMD_ASE|CMD_RESET);
486 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
487 /* dont start RS here for HSIC, it will be set by bus_reset */
488 if (tegra->phy->usb_phy_type != TEGRA_USB_PHY_TYPE_HSIC)
490 ehci->command |= CMD_RUN;
491 ehci_writel(ehci, ehci->command, &ehci->regs->command);
493 /* Enable the root Port Power */
494 if (HCS_PPC(ehci->hcs_params)) {
495 temp = ehci_readl(ehci, &ehci->regs->port_status[0]);
496 ehci_writel(ehci, temp | PORT_POWER, &ehci->regs->port_status[0]);
499 down_write(&ehci_cf_port_reset_rwsem);
501 hcd->state = HC_STATE_SUSPENDED;
503 hcd->state = HC_STATE_RUNNING;
504 ehci_writel(ehci, FLAG_CF, &ehci->regs->configured_flag);
505 /* flush posted writes */
506 ehci_readl(ehci, &ehci->regs->command);
507 up_write(&ehci_cf_port_reset_rwsem);
509 /* Turn On Interrupts */
510 ehci_writel(ehci, INTR_MASK, &ehci->regs->intr_enable);
514 static void tegra_ehci_shutdown(struct usb_hcd *hcd)
516 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
518 /* ehci_shutdown touches the USB controller registers, make sure
519 * controller has clocks to it */
520 if (!tegra->host_resumed)
521 tegra_ehci_power_up(hcd);
525 /* we are ready to shut down, powerdown the phy */
526 tegra_ehci_power_down(hcd);
529 static int tegra_ehci_setup(struct usb_hcd *hcd)
531 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
532 struct tegra_ehci_hcd *tegra = dev_get_drvdata(hcd->self.controller);
535 /* EHCI registers start at offset 0x100 */
536 ehci->caps = hcd->regs + 0x100;
538 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
539 ehci->has_hostpc = 1;
542 /* switch to host mode */
545 tegra_ehci_pre_reset(tegra->phy, false);
547 retval = ehci_setup(hcd);
551 ehci->controller_remote_wakeup = false;
552 tegra_ehci_post_reset(tegra->phy, false);
554 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_NULL_ULPI) {
556 * Resetting the controller has the side effect of resetting the PHY.
557 * So, never reset the controller after the calling
558 * tegra_ehci_reinit API.
560 ehci->controller_resets_phy = 1;
566 struct dma_aligned_buffer {
568 void *old_xfer_buffer;
572 static void free_dma_aligned_buffer(struct urb *urb)
574 struct dma_aligned_buffer *temp;
576 if (!(urb->transfer_flags & URB_ALIGNED_TEMP_BUFFER))
579 temp = container_of(urb->transfer_buffer,
580 struct dma_aligned_buffer, data);
582 if (usb_urb_dir_in(urb))
583 memcpy(temp->old_xfer_buffer, temp->data,
584 urb->transfer_buffer_length);
585 urb->transfer_buffer = temp->old_xfer_buffer;
586 kfree(temp->kmalloc_ptr);
588 urb->transfer_flags &= ~URB_ALIGNED_TEMP_BUFFER;
591 static int alloc_dma_aligned_buffer(struct urb *urb, gfp_t mem_flags)
593 struct dma_aligned_buffer *temp, *kmalloc_ptr;
596 if (urb->num_sgs || urb->sg ||
597 urb->transfer_buffer_length == 0 ||
598 !((uintptr_t)urb->transfer_buffer & (TEGRA_USB_DMA_ALIGN - 1)))
601 /* Allocate a buffer with enough padding for alignment */
602 kmalloc_size = urb->transfer_buffer_length +
603 sizeof(struct dma_aligned_buffer) + TEGRA_USB_DMA_ALIGN - 1;
605 kmalloc_ptr = kmalloc(kmalloc_size, mem_flags);
609 /* Position our struct dma_aligned_buffer such that data is aligned */
610 temp = PTR_ALIGN(kmalloc_ptr + 1, TEGRA_USB_DMA_ALIGN) - 1;
611 temp->kmalloc_ptr = kmalloc_ptr;
612 temp->old_xfer_buffer = urb->transfer_buffer;
613 if (usb_urb_dir_out(urb))
614 memcpy(temp->data, urb->transfer_buffer,
615 urb->transfer_buffer_length);
616 urb->transfer_buffer = temp->data;
618 urb->transfer_flags |= URB_ALIGNED_TEMP_BUFFER;
623 static int tegra_ehci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
628 ret = alloc_dma_aligned_buffer(urb, mem_flags);
632 ret = usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
634 free_dma_aligned_buffer(urb);
639 static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd *hcd, struct urb *urb)
641 usb_hcd_unmap_urb_for_dma(hcd, urb);
642 free_dma_aligned_buffer(urb);
645 static void tegra_hsic_connection_work(struct work_struct *work)
647 struct tegra_ehci_hcd *tegra =
648 container_of(work, struct tegra_ehci_hcd, work.work);
649 if (tegra_usb_phy_is_device_connected(tegra->phy)) {
650 cancel_delayed_work(&tegra->work);
653 /* Few cases HSIC device may not be connected, so *
654 ** skip this check after configured max re-tries. */
655 if (tegra->hsic_connect_retries++ > TEGRA_HSIC_CONNECTION_MAX_RETRIES)
658 schedule_delayed_work(&tegra->work, jiffies + msecs_to_jiffies(50));
662 void clk_timer_callback(unsigned long data)
664 struct tegra_ehci_hcd *tegra = (struct tegra_ehci_hcd*) data;
667 if (!timer_pending(&tegra->clk_timer)) {
668 spin_lock_irqsave(&tegra->ehci->lock, flags);
669 tegra->timer_event = 1;
670 spin_unlock_irqrestore(&tegra->ehci->lock, flags);
671 schedule_work(&tegra->clk_timer_work);
675 static void clk_timer_work_handler(struct work_struct* clk_timer_work) {
676 struct tegra_ehci_hcd *tegra = container_of(clk_timer_work,
677 struct tegra_ehci_hcd, clk_timer_work);
680 bool clock_enabled, timer_event;
682 spin_lock_irqsave(&tegra->ehci->lock, flags);
683 clock_enabled = tegra->clock_enabled;
684 timer_event = tegra->timer_event;
685 spin_unlock_irqrestore(&tegra->ehci->lock, flags);
688 spin_lock_irqsave(&tegra->ehci->lock, flags);
689 tegra->clock_enabled = 0;
690 tegra->timer_event = 0;
691 spin_unlock_irqrestore(&tegra->ehci->lock, flags);
692 clk_disable_unprepare(tegra->emc_clk);
693 clk_disable_unprepare(tegra->sclk_clk);
697 if ((!clock_enabled)) {
698 ret = mod_timer(&tegra->clk_timer, jiffies + msecs_to_jiffies(2000));
700 pr_err("tegra_ehci_urb_enqueue timer modify failed \n");
701 clk_prepare_enable(tegra->emc_clk);
702 clk_prepare_enable(tegra->sclk_clk);
703 spin_lock_irqsave(&tegra->ehci->lock, flags);
704 tegra->clock_enabled = 1;
705 spin_unlock_irqrestore(&tegra->ehci->lock, flags);
707 if (timer_pending(&tegra->clk_timer)) {
708 mod_timer_pending (&tegra->clk_timer, jiffies
709 + msecs_to_jiffies(2000));
714 static int tegra_ehci_urb_enqueue (
719 struct tegra_ehci_hcd *pdata;
721 int transfer_buffer_length;
722 struct ehci_hcd *ehci = hcd_to_ehci(hcd);
724 pdata = dev_get_drvdata(hcd->self.controller);
726 xfertype = usb_endpoint_type(&urb->ep->desc);
727 transfer_buffer_length = urb->transfer_buffer_length;
728 spin_lock_irqsave(&ehci->lock,flags);
729 /* Turn on the USB busy hints */
731 case USB_ENDPOINT_XFER_INT:
732 if (transfer_buffer_length < 255) {
733 /* Do nothing for interrupt buffers < 255 */
735 /* signal to set the busy hints */
736 schedule_work(&pdata->clk_timer_work);
739 case USB_ENDPOINT_XFER_ISOC:
740 case USB_ENDPOINT_XFER_BULK:
741 /* signal to set the busy hints */
742 schedule_work(&pdata->clk_timer_work);
744 case USB_ENDPOINT_XFER_CONTROL:
746 /* Do nothing special here */
749 spin_unlock_irqrestore(&ehci->lock,flags);
750 return ehci_urb_enqueue(hcd, urb, mem_flags);
753 static const struct hc_driver tegra_ehci_hc_driver = {
754 .description = hcd_name,
755 .product_desc = "Tegra EHCI Host Controller",
756 .hcd_priv_size = sizeof(struct ehci_hcd),
757 .flags = HCD_USB2 | HCD_MEMORY,
759 /* standard ehci functions */
760 .irq = tegra_ehci_irq,
763 .urb_enqueue = tegra_ehci_urb_enqueue,
764 .urb_dequeue = ehci_urb_dequeue,
765 .endpoint_disable = ehci_endpoint_disable,
766 .endpoint_reset = ehci_endpoint_reset,
767 .get_frame_number = ehci_get_frame,
768 .hub_status_data = ehci_hub_status_data,
769 .clear_tt_buffer_complete = ehci_clear_tt_buffer_complete,
770 .relinquish_port = ehci_relinquish_port,
771 .port_handed_over = ehci_port_handed_over,
773 /* modified ehci functions for tegra */
774 .reset = tegra_ehci_setup,
775 .shutdown = tegra_ehci_shutdown,
776 .map_urb_for_dma = tegra_ehci_map_urb_for_dma,
777 .unmap_urb_for_dma = tegra_ehci_unmap_urb_for_dma,
778 .hub_control = tegra_ehci_hub_control,
780 .bus_suspend = ehci_bus_suspend,
781 .bus_resume = ehci_bus_resume,
785 static int setup_vbus_gpio(struct platform_device *pdev,
786 struct tegra_ehci_platform_data *pdata)
791 gpio = pdata->vbus_gpio;
792 if (!gpio_is_valid(gpio))
793 gpio = of_get_named_gpio(pdev->dev.of_node,
794 "nvidia,vbus-gpio", 0);
795 if (!gpio_is_valid(gpio))
798 err = gpio_request(gpio, "vbus_gpio");
800 dev_err(&pdev->dev, "can't request vbus gpio %d", gpio);
803 err = gpio_direction_output(gpio, 1);
805 dev_err(&pdev->dev, "can't enable vbus\n");
814 static int controller_suspend(struct device *dev)
816 struct tegra_ehci_hcd *tegra =
817 platform_get_drvdata(to_platform_device(dev));
818 struct ehci_hcd *ehci = tegra->ehci;
819 struct usb_hcd *hcd = ehci_to_hcd(ehci);
820 struct ehci_regs __iomem *hw = ehci->regs;
824 hsic = (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_HSIC);
826 if (time_before(jiffies, ehci->next_statechange))
830 #ifndef CONFIG_ARCH_TEGRA_2x_SOC
831 if (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) {
833 * Ehci run bit is disabled by now read this into command variable
834 * so that bus resume will not enable run bit immedialty.
835 * this is required for 2LS WAR on UTMIP interface.
837 tegra->ehci->command = ehci_readl(tegra->ehci,
838 &tegra->ehci->regs->command);
842 spin_lock_irqsave(&ehci->lock, flags);
843 if (tegra->ehci->has_hostpc)
844 tegra->port_speed = (readl(hcd->regs + HOSTPC_REG_OFFSET) >> 25) & 0x3;
846 tegra->port_speed = (readl(&hw->port_status[0]) >> 26) & 0x3;
847 spin_unlock_irqrestore(&ehci->lock, flags);
849 tegra_ehci_power_down(hcd);
854 static int controller_resume(struct device *dev)
856 struct tegra_ehci_hcd *tegra =
857 platform_get_drvdata(to_platform_device(dev));
858 struct usb_device *udev = hcd->self.root_hub;
859 struct ehci_hcd *ehci = tegra->ehci;
860 struct usb_hcd *hcd = ehci_to_hcd(ehci);
861 struct ehci_regs __iomem *hw = ehci->regs;
865 bool utmip_remote_wakeup = false;
867 null_ulpi = (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_NULL_ULPI);
868 hsic = (tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_HSIC);
870 tegra_ehci_power_up(hcd);
871 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
873 if ((tegra->port_speed > TEGRA_USB_PHY_PORT_SPEED_HIGH) || (hsic) ||
877 /* Force the phy to keep data lines in suspend state */
878 tegra_ehci_phy_restore_start(tegra->phy, tegra->port_speed);
880 if ((tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) &&
881 (tegra->ehci->has_hostpc)) {
885 /* Enable host mode */
888 if ((tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) &&
889 (tegra->ehci->has_hostpc)) {
890 val = readl(hcd->regs + HOSTPC_REG_OFFSET);
891 val &= ~HOSTPC1_DEVLC_PTS(~0);
892 val |= HOSTPC1_DEVLC_STS;
893 writel(val, hcd->regs + HOSTPC_REG_OFFSET);
896 /* Enable Port Power */
897 val = readl(&hw->port_status[0]);
899 writel(val, &hw->port_status[0]);
902 if ((tegra->phy->usb_phy_type == TEGRA_USB_PHY_TYPE_UTMIP) &&
903 (tegra->ehci->has_hostpc) && (tegra->phy->remote_wakeup)) {
904 utmip_remote_wakeup = true;
907 /* Check if the phy resume from LP0. When the phy resume from LP0
908 * USB register will be reset. */
909 if (!readl(&hw->async_next)) {
910 /* Program the field PTC based on the saved speed mode */
911 val = readl(&hw->port_status[0]);
912 val &= ~PORT_TEST(~0);
913 if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_HIGH)
914 val |= PORT_TEST_FORCE;
915 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_FULL)
917 else if (tegra->port_speed == TEGRA_USB_PHY_PORT_SPEED_LOW)
919 writel(val, &hw->port_status[0]);
922 /* Disable test mode by setting PTC field to NORMAL_OP */
923 val = readl(&hw->port_status[0]);
924 val &= ~PORT_TEST(~0);
925 writel(val, &hw->port_status[0]);
929 /* Poll until CCS is enabled */
930 if (handshake(ehci, &hw->port_status[0], PORT_CONNECT,
931 PORT_CONNECT, 2000)) {
932 pr_err("%s: timeout waiting for PORT_CONNECT\n", __func__);
936 /* Poll until PE is enabled */
937 if (handshake(ehci, &hw->port_status[0], PORT_PE,
939 pr_err("%s: timeout waiting for USB_PORTSC1_PE\n", __func__);
943 /* Clear the PCI status, to avoid an interrupt taken upon resume */
944 val = readl(&hw->status);
946 writel(val, &hw->status);
948 /* Put controller in suspend mode by writing 1 to SUSP bit of PORTSC */
949 val = readl(&hw->port_status[0]);
950 if ((val & PORT_POWER) && (val & PORT_PE)) {
952 writel(val, &hw->port_status[0]);
954 /* Need a 4ms delay before the controller goes to suspend */
957 /* Wait until port suspend completes */
958 if (handshake(ehci, &hw->port_status[0], PORT_SUSPEND,
959 PORT_SUSPEND, 1000)) {
960 pr_err("%s: timeout waiting for PORT_SUSPEND\n",
966 tegra_ehci_phy_restore_end(tegra->phy);
967 if (utmip_remote_wakeup) {
968 ehci->command |= CMD_RUN;
969 ehci_writel(ehci, ehci->command, &ehci->regs->command);
975 bool LP0 = !readl(&hw->async_next);
980 pr_info("LP0 restart %d\n", cnt++);
981 tegra_ehci_phy_restore_start(tegra->phy,
985 val = readl(&hw->port_status[0]);
986 if (!((val & PORT_POWER) && (val & PORT_PE))) {
987 tegra_ehci_restart(hcd, is_dpd);
991 tegra_ehci_phy_restore_end(tegra->phy);
996 if ((tegra->port_speed <= TEGRA_USB_PHY_PORT_SPEED_HIGH) && (!hsic))
997 tegra_ehci_phy_restore_end(tegra->phy);
999 val = readl(&hw->port_status[0]);
1000 if (!((val & PORT_POWER) && (val & PORT_PE)))
1001 tegra_ehci_restart(hcd, false);
1003 tegra_usb_phy_bus_idle(tegra->phy);
1004 tegra->hsic_connect_retries = 0;
1005 if (!tegra_usb_phy_is_device_connected(tegra->phy))
1006 schedule_delayed_work(&tegra->work, 50);
1008 tegra_ehci_restart(hcd, false);
1015 static int tegra_ehci_suspend(struct device *dev)
1017 struct tegra_ehci_hcd *tegra =
1018 platform_get_drvdata(to_platform_device(dev));
1019 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
1023 pm_runtime_resume();
1025 if (tegra->phy->hotplug) {
1026 /* Disable PHY clock valid interrupts while going into suspend*/
1027 val = readl(hcd->regs + TEGRA_USB_SUSP_CTRL_OFFSET);
1028 val &= ~TEGRA_USB_PHY_CLK_VALID_INT_ENB;
1029 writel(val , (hcd->regs + TEGRA_USB_SUSP_CTRL_OFFSET));
1033 * When system sleep is supported and USB controller wakeup is
1034 * implemented: If the controller is runtime-suspended and the
1035 * wakeup setting needs to be changed, call pm_runtime_resume().
1037 if (HCD_HW_ACCESSIBLE(hcd))
1038 rc = controller_suspend(dev);
1039 if (tegra->default_enable)
1040 clk_disable(tegra->clk);
1044 static int tegra_ehci_resume(struct device *dev)
1046 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
1049 if (tegra->default_enable)
1050 clk_enable(tegra->clk);
1052 rc = controller_resume(dev);
1054 pm_runtime_disable(dev);
1055 pm_runtime_set_active(dev);
1056 pm_runtime_enable(dev);
1061 static int tegra_ehci_runtime_suspend(struct device *dev)
1063 return controller_suspend(dev);
1066 static int tegra_ehci_runtime_resume(struct device *dev)
1068 return controller_resume(dev);
1071 static const struct dev_pm_ops tegra_ehci_pm_ops = {
1072 .suspend = tegra_ehci_suspend,
1073 .resume = tegra_ehci_resume,
1074 .runtime_suspend = tegra_ehci_runtime_suspend,
1075 .runtime_resume = tegra_ehci_runtime_resume,
1080 static u64 tegra_ehci_dma_mask = DMA_BIT_MASK(32);
1082 static int tegra_ehci_probe(struct platform_device *pdev)
1084 struct resource *res;
1085 struct usb_hcd *hcd;
1086 struct tegra_ehci_hcd *tegra;
1087 struct tegra_ehci_platform_data *pdata;
1090 int instance = pdev->id;
1092 pdata = pdev->dev.platform_data;
1094 dev_err(&pdev->dev, "Platform data missing\n");
1098 /* Right now device-tree probed devices don't get dma_mask set.
1099 * Since shared usb code relies on it, set it here for now.
1100 * Once we have dma capability bindings this can go away.
1102 if (!pdev->dev.dma_mask)
1103 pdev->dev.dma_mask = &tegra_ehci_dma_mask;
1105 setup_vbus_gpio(pdev, pdata);
1107 tegra = devm_kzalloc(&pdev->dev, sizeof(struct tegra_ehci_hcd),
1112 hcd = usb_create_hcd(&tegra_ehci_hc_driver, &pdev->dev,
1113 dev_name(&pdev->dev));
1115 dev_err(&pdev->dev, "Unable to create HCD\n");
1119 platform_set_drvdata(pdev, tegra);
1120 tegra->default_enable = pdata->default_enable;
1122 tegra->clk = devm_clk_get(&pdev->dev, NULL);
1123 if (IS_ERR(tegra->clk)) {
1124 dev_err(&pdev->dev, "Can't get ehci clock\n");
1125 err = PTR_ERR(tegra->clk);
1129 err = clk_prepare_enable(tegra->clk);
1133 tegra->sclk_clk = devm_clk_get(&pdev->dev, "sclk");
1134 if (IS_ERR(tegra->sclk_clk)) {
1135 dev_err(&pdev->dev, "Can't get sclk clock\n");
1136 err = PTR_ERR(tegra->sclk_clk);
1140 clk_prepare_enable(tegra->sclk_clk);
1141 clk_set_rate(tegra->sclk_clk, 80000000);
1143 tegra->emc_clk = devm_clk_get(&pdev->dev, "emc");
1144 if (IS_ERR(tegra->emc_clk)) {
1145 dev_err(&pdev->dev, "Can't get emc clock\n");
1146 err = PTR_ERR(tegra->emc_clk);
1149 init_timer(&tegra->clk_timer);
1150 tegra->clk_timer.function = clk_timer_callback;
1151 tegra->clk_timer.data = (unsigned long) tegra;
1153 #ifdef CONFIG_ARCH_TEGRA_2x_SOC
1154 /* Set DDR busy hints to 150MHz. For Tegra 2x SOC, DDR rate is half of EMC rate */
1155 clk_set_rate(tegra->emc_clk, 300000000);
1157 /* Set DDR busy hints to 100MHz. For Tegra 3x SOC DDR rate equals to EMC rate */
1158 clk_set_rate(tegra->emc_clk, 100000000);
1161 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1163 dev_err(&pdev->dev, "Failed to get I/O memory\n");
1167 hcd->rsrc_start = res->start;
1168 hcd->rsrc_len = resource_size(res);
1169 hcd->regs = devm_ioremap(&pdev->dev, res->start, resource_size(res));
1171 dev_err(&pdev->dev, "Failed to remap I/O memory\n");
1176 /* This is pretty ugly and needs to be fixed when we do only
1177 * device-tree probing. Old code relies on the platform_device
1178 * numbering that we lack for device-tree-instantiated devices.
1181 switch (res->start) {
1182 case TEGRA_USB_BASE:
1185 case TEGRA_USB2_BASE:
1188 case TEGRA_USB3_BASE:
1193 dev_err(&pdev->dev, "unknown usb instance\n");
1198 INIT_DELAYED_WORK(&tegra->work, tegra_hsic_connection_work);
1200 INIT_WORK(&tegra->clk_timer_work, clk_timer_work_handler);
1202 tegra->phy = tegra_usb_phy_open(&pdev->dev, instance, hcd->regs,
1204 TEGRA_USB_PHY_MODE_HOST, pdata->phy_type);
1205 if (IS_ERR(tegra->phy)) {
1206 dev_err(&pdev->dev, "Failed to open USB phy\n");
1210 tegra->phy->hotplug = pdata->hotplug;
1212 usb_phy_init(&tegra->phy->u_phy);
1214 err = usb_phy_set_suspend(&tegra->phy->u_phy, 0);
1216 dev_err(&pdev->dev, "Failed to power on the phy\n");
1220 tegra->host_resumed = 1;
1221 tegra->ehci = hcd_to_ehci(hcd);
1223 irq = platform_get_irq(pdev, 0);
1225 dev_err(&pdev->dev, "Failed to get IRQ\n");
1231 if (pdata->operating_mode == TEGRA_USB_OTG) {
1232 tegra->transceiver =
1233 devm_usb_get_phy(&pdev->dev, USB_PHY_TYPE_USB2);
1234 if (!IS_ERR(tegra->transceiver))
1235 otg_set_host(tegra->transceiver->otg, &hcd->self);
1238 err = usb_add_hcd(hcd, irq, IRQF_SHARED);
1240 dev_err(&pdev->dev, "Failed to add USB HCD error = %d\n", err);
1244 err = enable_irq_wake(tegra->irq);
1246 dev_warn(&pdev->dev,
1247 "Couldn't enable USB host mode wakeup, irq=%d, "
1248 "error=%d\n", tegra->irq, err);
1253 pm_runtime_set_active(&pdev->dev);
1254 pm_runtime_get_noresume(&pdev->dev);
1256 /* Don't skip the pm_runtime_forbid call if wakeup isn't working */
1257 /* if (!pdata->power_down_on_bus_suspend) */
1258 pm_runtime_forbid(&pdev->dev);
1259 pm_runtime_enable(&pdev->dev);
1260 pm_runtime_put_sync(&pdev->dev);
1264 if (!IS_ERR(tegra->transceiver))
1265 otg_set_host(tegra->transceiver->otg, NULL);
1266 usb_phy_shutdown(&tegra->phy->u_phy);
1268 clk_disable_unprepare(tegra->emc_clk);
1270 clk_disable_unprepare(tegra->sclk_clk);
1272 clk_disable_unprepare(tegra->clk);
1278 static int tegra_ehci_remove(struct platform_device *pdev)
1280 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
1281 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
1283 /* make sure controller is on as we will touch its registers */
1284 if (!tegra->host_resumed)
1285 tegra_ehci_power_up(hcd, true);
1287 pm_runtime_get_sync(&pdev->dev);
1288 pm_runtime_disable(&pdev->dev);
1289 pm_runtime_put_noidle(&pdev->dev);
1291 if (!IS_ERR(tegra->transceiver))
1292 otg_set_host(tegra->transceiver->otg, NULL);
1294 /* Turn Off Interrupts */
1295 ehci_writel(tegra->ehci, 0, &tegra->ehci->regs->intr_enable);
1296 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1298 disable_irq_wake(tegra->irq);
1299 usb_remove_hcd(hcd);
1301 cancel_delayed_work(&tegra->work);
1302 tegra_usb_phy_power_off(tegra->phy);
1303 usb_phy_shutdown(&tegra->phy->u_phy);
1305 del_timer_sync(&tegra->clk_timer);
1307 clk_disable_unprepare(tegra->clk);
1309 if (tegra->clock_enabled) {
1310 clk_disable_unprepare(tegra->sclk_clk);
1311 clk_disable_unprepare(tegra->emc_clk);
1317 static void tegra_ehci_hcd_shutdown(struct platform_device *pdev)
1319 struct tegra_ehci_hcd *tegra = platform_get_drvdata(pdev);
1320 struct usb_hcd *hcd = ehci_to_hcd(tegra->ehci);
1322 if (hcd->driver->shutdown)
1323 hcd->driver->shutdown(hcd);
1326 static struct of_device_id tegra_ehci_of_match[] = {
1327 { .compatible = "nvidia,tegra20-ehci", },
1331 static struct platform_driver tegra_ehci_driver = {
1332 .probe = tegra_ehci_probe,
1333 .remove = tegra_ehci_remove,
1334 .shutdown = tegra_ehci_hcd_shutdown,
1336 .name = "tegra-ehci",
1337 .of_match_table = tegra_ehci_of_match,
1339 .pm = &tegra_ehci_pm_ops,