1 // SPDX-License-Identifier: GPL-2.0+
3 * EHCI-compliant USB host controller driver for NVIDIA Tegra SoCs
5 * Copyright (C) 2010 Google, Inc.
6 * Copyright (C) 2009 - 2013 NVIDIA Corporation
10 #include <linux/dma-mapping.h>
11 #include <linux/err.h>
12 #include <linux/gpio.h>
14 #include <linux/irq.h>
15 #include <linux/module.h>
17 #include <linux/of_device.h>
18 #include <linux/of_gpio.h>
19 #include <linux/platform_device.h>
20 #include <linux/pm_runtime.h>
21 #include <linux/reset.h>
22 #include <linux/slab.h>
23 #include <linux/usb/ehci_def.h>
24 #include <linux/usb/tegra_usb_phy.h>
25 #include <linux/usb.h>
26 #include <linux/usb/hcd.h>
27 #include <linux/usb/otg.h>
31 #define PORT_WAKE_BITS (PORT_WKOC_E|PORT_WKDISC_E|PORT_WKCONN_E)
33 #define TEGRA_USB_DMA_ALIGN 32
35 #define DRIVER_DESC "Tegra EHCI driver"
36 #define DRV_NAME "tegra-ehci"
38 static struct hc_driver __read_mostly tegra_ehci_hc_driver
;
40 struct tegra_ehci_soc_config
{
44 struct tegra_ehci_hcd
{
46 struct reset_control
*rst
;
48 bool needs_double_reset
;
51 static int tegra_reset_usb_controller(struct platform_device
*pdev
)
53 struct device_node
*phy_np
;
54 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
55 struct tegra_ehci_hcd
*tegra
=
56 (struct tegra_ehci_hcd
*)hcd_to_ehci(hcd
)->priv
;
57 struct reset_control
*rst
;
60 phy_np
= of_parse_phandle(pdev
->dev
.of_node
, "nvidia,phy", 0);
65 * The 1st USB controller contains some UTMI pad registers that are
66 * global for all the controllers on the chip. Those registers are
67 * also cleared when reset is asserted to the 1st controller.
69 rst
= of_reset_control_get_shared(phy_np
, "utmi-pads");
72 "can't get utmi-pads reset from the PHY\n");
74 "continuing, but please update your DT\n");
77 * PHY driver performs UTMI-pads reset in a case of
80 reset_control_put(rst
);
85 /* reset control is shared, hence initialize it first */
86 err
= reset_control_deassert(tegra
->rst
);
90 err
= reset_control_assert(tegra
->rst
);
96 err
= reset_control_deassert(tegra
->rst
);
103 static int tegra_ehci_internal_port_reset(
104 struct ehci_hcd
*ehci
,
105 u32 __iomem
*portsc_reg
114 spin_lock_irqsave(&ehci
->lock
, flags
);
115 saved_usbintr
= ehci_readl(ehci
, &ehci
->regs
->intr_enable
);
116 /* disable USB interrupt */
117 ehci_writel(ehci
, 0, &ehci
->regs
->intr_enable
);
118 spin_unlock_irqrestore(&ehci
->lock
, flags
);
121 * Here we have to do Port Reset at most twice for
122 * Port Enable bit to be set.
124 for (i
= 0; i
< 2; i
++) {
125 temp
= ehci_readl(ehci
, portsc_reg
);
127 ehci_writel(ehci
, temp
, portsc_reg
);
130 ehci_writel(ehci
, temp
, portsc_reg
);
136 * Up to this point, Port Enable bit is
137 * expected to be set after 2 ms waiting.
138 * USB1 usually takes extra 45 ms, for safety,
139 * we take 100 ms as timeout.
141 temp
= ehci_readl(ehci
, portsc_reg
);
142 } while (!(temp
& PORT_PE
) && tries
--);
150 * Clear Connect Status Change bit if it's set.
151 * We can't clear PORT_PEC. It will also cause PORT_PE to be cleared.
154 ehci_writel(ehci
, PORT_CSC
, portsc_reg
);
157 * Write to clear any interrupt status bits that might be set
160 temp
= ehci_readl(ehci
, &ehci
->regs
->status
);
161 ehci_writel(ehci
, temp
, &ehci
->regs
->status
);
163 /* restore original interrupt enable bits */
164 ehci_writel(ehci
, saved_usbintr
, &ehci
->regs
->intr_enable
);
168 static int tegra_ehci_hub_control(
177 struct ehci_hcd
*ehci
= hcd_to_ehci(hcd
);
178 struct tegra_ehci_hcd
*tegra
= (struct tegra_ehci_hcd
*)ehci
->priv
;
179 u32 __iomem
*status_reg
;
184 status_reg
= &ehci
->regs
->port_status
[(wIndex
& 0xff) - 1];
186 spin_lock_irqsave(&ehci
->lock
, flags
);
188 if (typeReq
== GetPortStatus
) {
189 temp
= ehci_readl(ehci
, status_reg
);
190 if (tegra
->port_resuming
&& !(temp
& PORT_SUSPEND
)) {
191 /* Resume completed, re-enable disconnect detection */
192 tegra
->port_resuming
= 0;
193 tegra_usb_phy_postresume(hcd
->usb_phy
);
197 else if (typeReq
== SetPortFeature
&& wValue
== USB_PORT_FEAT_SUSPEND
) {
198 temp
= ehci_readl(ehci
, status_reg
);
199 if ((temp
& PORT_PE
) == 0 || (temp
& PORT_RESET
) != 0) {
204 temp
&= ~(PORT_RWC_BITS
| PORT_WKCONN_E
);
205 temp
|= PORT_WKDISC_E
| PORT_WKOC_E
;
206 ehci_writel(ehci
, temp
| PORT_SUSPEND
, status_reg
);
209 * If a transaction is in progress, there may be a delay in
210 * suspending the port. Poll until the port is suspended.
212 if (ehci_handshake(ehci
, status_reg
, PORT_SUSPEND
,
214 pr_err("%s: timeout waiting for SUSPEND\n", __func__
);
216 set_bit((wIndex
& 0xff) - 1, &ehci
->suspended_ports
);
220 /* For USB1 port we need to issue Port Reset twice internally */
221 if (tegra
->needs_double_reset
&&
222 (typeReq
== SetPortFeature
&& wValue
== USB_PORT_FEAT_RESET
)) {
223 spin_unlock_irqrestore(&ehci
->lock
, flags
);
224 return tegra_ehci_internal_port_reset(ehci
, status_reg
);
228 * Tegra host controller will time the resume operation to clear the bit
229 * when the port control state switches to HS or FS Idle. This behavior
230 * is different from EHCI where the host controller driver is required
231 * to set this bit to a zero after the resume duration is timed in the
234 else if (typeReq
== ClearPortFeature
&&
235 wValue
== USB_PORT_FEAT_SUSPEND
) {
236 temp
= ehci_readl(ehci
, status_reg
);
237 if ((temp
& PORT_RESET
) || !(temp
& PORT_PE
)) {
242 if (!(temp
& PORT_SUSPEND
))
245 /* Disable disconnect detection during port resume */
246 tegra_usb_phy_preresume(hcd
->usb_phy
);
248 ehci
->reset_done
[wIndex
-1] = jiffies
+ msecs_to_jiffies(25);
250 temp
&= ~(PORT_RWC_BITS
| PORT_WAKE_BITS
);
251 /* start resume signalling */
252 ehci_writel(ehci
, temp
| PORT_RESUME
, status_reg
);
253 set_bit(wIndex
-1, &ehci
->resuming_ports
);
255 spin_unlock_irqrestore(&ehci
->lock
, flags
);
257 spin_lock_irqsave(&ehci
->lock
, flags
);
259 /* Poll until the controller clears RESUME and SUSPEND */
260 if (ehci_handshake(ehci
, status_reg
, PORT_RESUME
, 0, 2000))
261 pr_err("%s: timeout waiting for RESUME\n", __func__
);
262 if (ehci_handshake(ehci
, status_reg
, PORT_SUSPEND
, 0, 2000))
263 pr_err("%s: timeout waiting for SUSPEND\n", __func__
);
265 ehci
->reset_done
[wIndex
-1] = 0;
266 clear_bit(wIndex
-1, &ehci
->resuming_ports
);
268 tegra
->port_resuming
= 1;
272 spin_unlock_irqrestore(&ehci
->lock
, flags
);
274 /* Handle the hub control events here */
275 return ehci_hub_control(hcd
, typeReq
, wValue
, wIndex
, buf
, wLength
);
278 spin_unlock_irqrestore(&ehci
->lock
, flags
);
282 struct dma_aligned_buffer
{
284 void *old_xfer_buffer
;
288 static void free_dma_aligned_buffer(struct urb
*urb
)
290 struct dma_aligned_buffer
*temp
;
293 if (!(urb
->transfer_flags
& URB_ALIGNED_TEMP_BUFFER
))
296 temp
= container_of(urb
->transfer_buffer
,
297 struct dma_aligned_buffer
, data
);
299 if (usb_urb_dir_in(urb
)) {
300 if (usb_pipeisoc(urb
->pipe
))
301 length
= urb
->transfer_buffer_length
;
303 length
= urb
->actual_length
;
305 memcpy(temp
->old_xfer_buffer
, temp
->data
, length
);
307 urb
->transfer_buffer
= temp
->old_xfer_buffer
;
308 kfree(temp
->kmalloc_ptr
);
310 urb
->transfer_flags
&= ~URB_ALIGNED_TEMP_BUFFER
;
313 static int alloc_dma_aligned_buffer(struct urb
*urb
, gfp_t mem_flags
)
315 struct dma_aligned_buffer
*temp
, *kmalloc_ptr
;
318 if (urb
->num_sgs
|| urb
->sg
||
319 urb
->transfer_buffer_length
== 0 ||
320 !((uintptr_t)urb
->transfer_buffer
& (TEGRA_USB_DMA_ALIGN
- 1)))
323 /* Allocate a buffer with enough padding for alignment */
324 kmalloc_size
= urb
->transfer_buffer_length
+
325 sizeof(struct dma_aligned_buffer
) + TEGRA_USB_DMA_ALIGN
- 1;
327 kmalloc_ptr
= kmalloc(kmalloc_size
, mem_flags
);
331 /* Position our struct dma_aligned_buffer such that data is aligned */
332 temp
= PTR_ALIGN(kmalloc_ptr
+ 1, TEGRA_USB_DMA_ALIGN
) - 1;
333 temp
->kmalloc_ptr
= kmalloc_ptr
;
334 temp
->old_xfer_buffer
= urb
->transfer_buffer
;
335 if (usb_urb_dir_out(urb
))
336 memcpy(temp
->data
, urb
->transfer_buffer
,
337 urb
->transfer_buffer_length
);
338 urb
->transfer_buffer
= temp
->data
;
340 urb
->transfer_flags
|= URB_ALIGNED_TEMP_BUFFER
;
345 static int tegra_ehci_map_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
,
350 ret
= alloc_dma_aligned_buffer(urb
, mem_flags
);
354 ret
= usb_hcd_map_urb_for_dma(hcd
, urb
, mem_flags
);
356 free_dma_aligned_buffer(urb
);
361 static void tegra_ehci_unmap_urb_for_dma(struct usb_hcd
*hcd
, struct urb
*urb
)
363 usb_hcd_unmap_urb_for_dma(hcd
, urb
);
364 free_dma_aligned_buffer(urb
);
367 static const struct tegra_ehci_soc_config tegra30_soc_config
= {
371 static const struct tegra_ehci_soc_config tegra20_soc_config
= {
375 static const struct of_device_id tegra_ehci_of_match
[] = {
376 { .compatible
= "nvidia,tegra30-ehci", .data
= &tegra30_soc_config
},
377 { .compatible
= "nvidia,tegra20-ehci", .data
= &tegra20_soc_config
},
381 static int tegra_ehci_probe(struct platform_device
*pdev
)
383 const struct of_device_id
*match
;
384 const struct tegra_ehci_soc_config
*soc_config
;
385 struct resource
*res
;
387 struct ehci_hcd
*ehci
;
388 struct tegra_ehci_hcd
*tegra
;
391 struct usb_phy
*u_phy
;
393 match
= of_match_device(tegra_ehci_of_match
, &pdev
->dev
);
395 dev_err(&pdev
->dev
, "Error: No device match found\n");
398 soc_config
= match
->data
;
400 /* Right now device-tree probed devices don't get dma_mask set.
401 * Since shared usb code relies on it, set it here for now.
402 * Once we have dma capability bindings this can go away.
404 err
= dma_coerce_mask_and_coherent(&pdev
->dev
, DMA_BIT_MASK(32));
408 hcd
= usb_create_hcd(&tegra_ehci_hc_driver
, &pdev
->dev
,
409 dev_name(&pdev
->dev
));
411 dev_err(&pdev
->dev
, "Unable to create HCD\n");
414 platform_set_drvdata(pdev
, hcd
);
415 ehci
= hcd_to_ehci(hcd
);
416 tegra
= (struct tegra_ehci_hcd
*)ehci
->priv
;
420 tegra
->clk
= devm_clk_get(&pdev
->dev
, NULL
);
421 if (IS_ERR(tegra
->clk
)) {
422 dev_err(&pdev
->dev
, "Can't get ehci clock\n");
423 err
= PTR_ERR(tegra
->clk
);
424 goto cleanup_hcd_create
;
427 tegra
->rst
= devm_reset_control_get_shared(&pdev
->dev
, "usb");
428 if (IS_ERR(tegra
->rst
)) {
429 dev_err(&pdev
->dev
, "Can't get ehci reset\n");
430 err
= PTR_ERR(tegra
->rst
);
431 goto cleanup_hcd_create
;
434 err
= clk_prepare_enable(tegra
->clk
);
436 goto cleanup_hcd_create
;
438 err
= tegra_reset_usb_controller(pdev
);
440 dev_err(&pdev
->dev
, "Failed to reset controller\n");
444 u_phy
= devm_usb_get_phy_by_phandle(&pdev
->dev
, "nvidia,phy", 0);
449 hcd
->usb_phy
= u_phy
;
450 hcd
->skip_phy_initialization
= 1;
452 tegra
->needs_double_reset
= of_property_read_bool(pdev
->dev
.of_node
,
453 "nvidia,needs-double-reset");
455 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
456 hcd
->regs
= devm_ioremap_resource(&pdev
->dev
, res
);
457 if (IS_ERR(hcd
->regs
)) {
458 err
= PTR_ERR(hcd
->regs
);
461 hcd
->rsrc_start
= res
->start
;
462 hcd
->rsrc_len
= resource_size(res
);
464 ehci
->caps
= hcd
->regs
+ 0x100;
465 ehci
->has_hostpc
= soc_config
->has_hostpc
;
467 err
= usb_phy_init(hcd
->usb_phy
);
469 dev_err(&pdev
->dev
, "Failed to initialize phy\n");
473 u_phy
->otg
= devm_kzalloc(&pdev
->dev
, sizeof(struct usb_otg
),
479 u_phy
->otg
->host
= hcd_to_bus(hcd
);
481 irq
= platform_get_irq(pdev
, 0);
487 otg_set_host(u_phy
->otg
, &hcd
->self
);
489 err
= usb_add_hcd(hcd
, irq
, IRQF_SHARED
);
491 dev_err(&pdev
->dev
, "Failed to add USB HCD\n");
492 goto cleanup_otg_set_host
;
494 device_wakeup_enable(hcd
->self
.controller
);
498 cleanup_otg_set_host
:
499 otg_set_host(u_phy
->otg
, NULL
);
501 usb_phy_shutdown(hcd
->usb_phy
);
503 clk_disable_unprepare(tegra
->clk
);
509 static int tegra_ehci_remove(struct platform_device
*pdev
)
511 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
512 struct tegra_ehci_hcd
*tegra
=
513 (struct tegra_ehci_hcd
*)hcd_to_ehci(hcd
)->priv
;
516 otg_set_host(hcd
->usb_phy
->otg
, NULL
);
517 usb_phy_shutdown(hcd
->usb_phy
);
518 clk_disable_unprepare(tegra
->clk
);
524 static void tegra_ehci_hcd_shutdown(struct platform_device
*pdev
)
526 struct usb_hcd
*hcd
= platform_get_drvdata(pdev
);
528 if (hcd
->driver
->shutdown
)
529 hcd
->driver
->shutdown(hcd
);
532 static struct platform_driver tegra_ehci_driver
= {
533 .probe
= tegra_ehci_probe
,
534 .remove
= tegra_ehci_remove
,
535 .shutdown
= tegra_ehci_hcd_shutdown
,
538 .of_match_table
= tegra_ehci_of_match
,
542 static int tegra_ehci_reset(struct usb_hcd
*hcd
)
544 struct ehci_hcd
*ehci
= hcd_to_ehci(hcd
);
548 retval
= ehci_setup(hcd
);
553 * We should really pull this value out of tegra_ehci_soc_config, but
554 * to avoid needing access to it, make use of the fact that Tegra20 is
555 * the only one so far that needs a value of 10, and Tegra20 is the
556 * only one which doesn't set has_hostpc.
558 txfifothresh
= ehci
->has_hostpc
? 0x10 : 10;
559 ehci_writel(ehci
, txfifothresh
<< 16, &ehci
->regs
->txfill_tuning
);
564 static const struct ehci_driver_overrides tegra_overrides __initconst
= {
565 .extra_priv_size
= sizeof(struct tegra_ehci_hcd
),
566 .reset
= tegra_ehci_reset
,
569 static int __init
ehci_tegra_init(void)
574 pr_info(DRV_NAME
": " DRIVER_DESC
"\n");
576 ehci_init_driver(&tegra_ehci_hc_driver
, &tegra_overrides
);
579 * The Tegra HW has some unusual quirks, which require Tegra-specific
580 * workarounds. We override certain hc_driver functions here to
581 * achieve that. We explicitly do not enhance ehci_driver_overrides to
582 * allow this more easily, since this is an unusual case, and we don't
583 * want to encourage others to override these functions by making it
587 tegra_ehci_hc_driver
.map_urb_for_dma
= tegra_ehci_map_urb_for_dma
;
588 tegra_ehci_hc_driver
.unmap_urb_for_dma
= tegra_ehci_unmap_urb_for_dma
;
589 tegra_ehci_hc_driver
.hub_control
= tegra_ehci_hub_control
;
591 return platform_driver_register(&tegra_ehci_driver
);
593 module_init(ehci_tegra_init
);
595 static void __exit
ehci_tegra_cleanup(void)
597 platform_driver_unregister(&tegra_ehci_driver
);
599 module_exit(ehci_tegra_cleanup
);
601 MODULE_DESCRIPTION(DRIVER_DESC
);
602 MODULE_LICENSE("GPL");
603 MODULE_ALIAS("platform:" DRV_NAME
);
604 MODULE_DEVICE_TABLE(of
, tegra_ehci_of_match
);