1 // SPDX-License-Identifier: GPL-2.0
3 * NVIDIA Tegra xHCI host controller driver
5 * Copyright (C) 2014 NVIDIA Corporation
6 * Copyright (C) 2014 Google, Inc.
10 #include <linux/delay.h>
11 #include <linux/dma-mapping.h>
12 #include <linux/firmware.h>
13 #include <linux/interrupt.h>
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/of_device.h>
17 #include <linux/phy/phy.h>
18 #include <linux/phy/tegra/xusb.h>
19 #include <linux/platform_device.h>
21 #include <linux/regulator/consumer.h>
22 #include <linux/reset.h>
23 #include <linux/slab.h>
27 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
28 #define TEGRA_XHCI_SS_LOW_SPEED 12000000
30 /* FPCI CFG registers */
31 #define XUSB_CFG_1 0x004
32 #define XUSB_IO_SPACE_EN BIT(0)
33 #define XUSB_MEM_SPACE_EN BIT(1)
34 #define XUSB_BUS_MASTER_EN BIT(2)
35 #define XUSB_CFG_4 0x010
36 #define XUSB_BASE_ADDR_SHIFT 15
37 #define XUSB_BASE_ADDR_MASK 0x1ffff
38 #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c
39 #define XUSB_CFG_CSB_BASE_ADDR 0x800
41 /* FPCI mailbox registers */
42 #define XUSB_CFG_ARU_MBOX_CMD 0x0e4
43 #define MBOX_DEST_FALC BIT(27)
44 #define MBOX_DEST_PME BIT(28)
45 #define MBOX_DEST_SMI BIT(29)
46 #define MBOX_DEST_XHCI BIT(30)
47 #define MBOX_INT_EN BIT(31)
48 #define XUSB_CFG_ARU_MBOX_DATA_IN 0x0e8
49 #define CMD_DATA_SHIFT 0
50 #define CMD_DATA_MASK 0xffffff
51 #define CMD_TYPE_SHIFT 24
52 #define CMD_TYPE_MASK 0xff
53 #define XUSB_CFG_ARU_MBOX_DATA_OUT 0x0ec
54 #define XUSB_CFG_ARU_MBOX_OWNER 0x0f0
55 #define MBOX_OWNER_NONE 0
56 #define MBOX_OWNER_FW 1
57 #define MBOX_OWNER_SW 2
58 #define XUSB_CFG_ARU_SMI_INTR 0x428
59 #define MBOX_SMI_INTR_FW_HANG BIT(1)
60 #define MBOX_SMI_INTR_EN BIT(3)
63 #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180
64 #define IPFS_EN_FPCI BIT(0)
65 #define IPFS_XUSB_HOST_INTR_MASK_0 0x188
66 #define IPFS_IP_INT_MASK BIT(16)
67 #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc
69 #define CSB_PAGE_SELECT_MASK 0x7fffff
70 #define CSB_PAGE_SELECT_SHIFT 9
71 #define CSB_PAGE_OFFSET_MASK 0x1ff
72 #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \
74 #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK)
76 /* Falcon CSB registers */
77 #define XUSB_FALC_CPUCTL 0x100
78 #define CPUCTL_STARTCPU BIT(1)
79 #define CPUCTL_STATE_HALTED BIT(4)
80 #define CPUCTL_STATE_STOPPED BIT(5)
81 #define XUSB_FALC_BOOTVEC 0x104
82 #define XUSB_FALC_DMACTL 0x10c
83 #define XUSB_FALC_IMFILLRNG1 0x154
84 #define IMFILLRNG1_TAG_MASK 0xffff
85 #define IMFILLRNG1_TAG_LO_SHIFT 0
86 #define IMFILLRNG1_TAG_HI_SHIFT 16
87 #define XUSB_FALC_IMFILLCTL 0x158
89 /* MP CSB registers */
90 #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00
91 #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04
92 #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08
93 #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10
94 #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8
95 #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff
96 #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24
97 #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff
98 #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14
99 #define L2IMEMOP_ACTION_SHIFT 24
100 #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT)
101 #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT)
102 #define XUSB_CSB_MP_APMAP 0x10181c
103 #define APMAP_BOOTPATH BIT(31)
105 #define IMEM_BLOCK_SIZE 256
107 struct tegra_xusb_fw_header
{
108 u32 boot_loadaddr_in_imem
;
109 u32 boot_codedfi_offset
;
114 u16 alloc_phys_memsize
;
115 u32 rodata_img_offset
;
116 u32 rodata_section_start
;
117 u32 rodata_section_end
;
120 u32 fwimg_created_time
;
121 u32 imem_resident_start
;
122 u32 imem_resident_end
;
130 u32 phys_addr_log_buffer
;
131 u32 total_log_entries
;
136 u32 ss_low_power_entry_timeout
;
138 u8 padding
[139]; /* Pad to 256 bytes */
141 struct tegra_xusb_phy_type
{
146 struct tegra_xusb_soc
{
147 const char *firmware
;
148 const char * const *supply_names
;
149 unsigned int num_supplies
;
150 const struct tegra_xusb_phy_type
*phy_types
;
151 unsigned int num_types
;
157 } usb2
, ulpi
, hsic
, usb3
;
173 void __iomem
*ipfs_base
;
174 void __iomem
*fpci_base
;
176 const struct tegra_xusb_soc
*soc
;
178 struct regulator_bulk_data
*supplies
;
180 struct tegra_xusb_padctl
*padctl
;
182 struct clk
*host_clk
;
183 struct clk
*falcon_clk
;
185 struct clk
*ss_src_clk
;
186 struct clk
*hs_src_clk
;
187 struct clk
*fs_src_clk
;
188 struct clk
*pll_u_480m
;
192 struct reset_control
*host_rst
;
193 struct reset_control
*ss_rst
;
196 unsigned int num_phys
;
198 /* Firmware loading related */
206 static struct hc_driver __read_mostly tegra_xhci_hc_driver
;
208 static inline u32
fpci_readl(struct tegra_xusb
*tegra
, unsigned int offset
)
210 return readl(tegra
->fpci_base
+ offset
);
213 static inline void fpci_writel(struct tegra_xusb
*tegra
, u32 value
,
216 writel(value
, tegra
->fpci_base
+ offset
);
219 static inline u32
ipfs_readl(struct tegra_xusb
*tegra
, unsigned int offset
)
221 return readl(tegra
->ipfs_base
+ offset
);
224 static inline void ipfs_writel(struct tegra_xusb
*tegra
, u32 value
,
227 writel(value
, tegra
->ipfs_base
+ offset
);
230 static u32
csb_readl(struct tegra_xusb
*tegra
, unsigned int offset
)
232 u32 page
= CSB_PAGE_SELECT(offset
);
233 u32 ofs
= CSB_PAGE_OFFSET(offset
);
235 fpci_writel(tegra
, page
, XUSB_CFG_ARU_C11_CSBRANGE
);
237 return fpci_readl(tegra
, XUSB_CFG_CSB_BASE_ADDR
+ ofs
);
240 static void csb_writel(struct tegra_xusb
*tegra
, u32 value
,
243 u32 page
= CSB_PAGE_SELECT(offset
);
244 u32 ofs
= CSB_PAGE_OFFSET(offset
);
246 fpci_writel(tegra
, page
, XUSB_CFG_ARU_C11_CSBRANGE
);
247 fpci_writel(tegra
, value
, XUSB_CFG_CSB_BASE_ADDR
+ ofs
);
250 static int tegra_xusb_set_ss_clk(struct tegra_xusb
*tegra
,
253 unsigned long new_parent_rate
, old_parent_rate
;
254 struct clk
*clk
= tegra
->ss_src_clk
;
258 if (clk_get_rate(clk
) == rate
)
262 case TEGRA_XHCI_SS_HIGH_SPEED
:
264 * Reparent to PLLU_480M. Set divider first to avoid
267 old_parent_rate
= clk_get_rate(clk_get_parent(clk
));
268 new_parent_rate
= clk_get_rate(tegra
->pll_u_480m
);
269 div
= new_parent_rate
/ rate
;
271 err
= clk_set_rate(clk
, old_parent_rate
/ div
);
275 err
= clk_set_parent(clk
, tegra
->pll_u_480m
);
280 * The rate should already be correct, but set it again just
283 err
= clk_set_rate(clk
, rate
);
289 case TEGRA_XHCI_SS_LOW_SPEED
:
290 /* Reparent to CLK_M */
291 err
= clk_set_parent(clk
, tegra
->clk_m
);
295 err
= clk_set_rate(clk
, rate
);
302 dev_err(tegra
->dev
, "Invalid SS rate: %lu Hz\n", rate
);
306 if (clk_get_rate(clk
) != rate
) {
307 dev_err(tegra
->dev
, "SS clock doesn't match requested rate\n");
314 static unsigned long extract_field(u32 value
, unsigned int start
,
317 return (value
>> start
) & ((1 << count
) - 1);
320 /* Command requests from the firmware */
321 enum tegra_xusb_mbox_cmd
{
322 MBOX_CMD_MSG_ENABLED
= 1,
323 MBOX_CMD_INC_FALC_CLOCK
,
324 MBOX_CMD_DEC_FALC_CLOCK
,
325 MBOX_CMD_INC_SSPI_CLOCK
,
326 MBOX_CMD_DEC_SSPI_CLOCK
,
327 MBOX_CMD_SET_BW
, /* no ACK/NAK required */
328 MBOX_CMD_SET_SS_PWR_GATING
,
329 MBOX_CMD_SET_SS_PWR_UNGATING
,
330 MBOX_CMD_SAVE_DFE_CTLE_CTX
,
331 MBOX_CMD_AIRPLANE_MODE_ENABLED
, /* unused */
332 MBOX_CMD_AIRPLANE_MODE_DISABLED
, /* unused */
333 MBOX_CMD_START_HSIC_IDLE
,
334 MBOX_CMD_STOP_HSIC_IDLE
,
335 MBOX_CMD_DBC_WAKE_STACK
, /* unused */
336 MBOX_CMD_HSIC_PRETEND_CONNECT
,
338 MBOX_CMD_DISABLE_SS_LFPS_DETECTION
,
339 MBOX_CMD_ENABLE_SS_LFPS_DETECTION
,
343 /* Response message to above commands */
348 static const char * const mbox_cmd_name
[] = {
350 [ 2] = "INC_FALCON_CLOCK",
351 [ 3] = "DEC_FALCON_CLOCK",
352 [ 4] = "INC_SSPI_CLOCK",
353 [ 5] = "DEC_SSPI_CLOCK",
355 [ 7] = "SET_SS_PWR_GATING",
356 [ 8] = "SET_SS_PWR_UNGATING",
357 [ 9] = "SAVE_DFE_CTLE_CTX",
358 [ 10] = "AIRPLANE_MODE_ENABLED",
359 [ 11] = "AIRPLANE_MODE_DISABLED",
360 [ 12] = "START_HSIC_IDLE",
361 [ 13] = "STOP_HSIC_IDLE",
362 [ 14] = "DBC_WAKE_STACK",
363 [ 15] = "HSIC_PRETEND_CONNECT",
364 [ 16] = "RESET_SSPI",
365 [ 17] = "DISABLE_SS_LFPS_DETECTION",
366 [ 18] = "ENABLE_SS_LFPS_DETECTION",
371 struct tegra_xusb_mbox_msg
{
376 static inline u32
tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg
*msg
)
378 return (msg
->cmd
& CMD_TYPE_MASK
) << CMD_TYPE_SHIFT
|
379 (msg
->data
& CMD_DATA_MASK
) << CMD_DATA_SHIFT
;
381 static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg
*msg
,
384 msg
->cmd
= (value
>> CMD_TYPE_SHIFT
) & CMD_TYPE_MASK
;
385 msg
->data
= (value
>> CMD_DATA_SHIFT
) & CMD_DATA_MASK
;
388 static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd
)
391 case MBOX_CMD_SET_BW
:
401 static int tegra_xusb_mbox_send(struct tegra_xusb
*tegra
,
402 const struct tegra_xusb_mbox_msg
*msg
)
404 bool wait_for_idle
= false;
408 * Acquire the mailbox. The firmware still owns the mailbox for
411 if (!(msg
->cmd
== MBOX_CMD_ACK
|| msg
->cmd
== MBOX_CMD_NAK
)) {
412 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_OWNER
);
413 if (value
!= MBOX_OWNER_NONE
) {
414 dev_err(tegra
->dev
, "mailbox is busy\n");
418 fpci_writel(tegra
, MBOX_OWNER_SW
, XUSB_CFG_ARU_MBOX_OWNER
);
420 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_OWNER
);
421 if (value
!= MBOX_OWNER_SW
) {
422 dev_err(tegra
->dev
, "failed to acquire mailbox\n");
426 wait_for_idle
= true;
429 value
= tegra_xusb_mbox_pack(msg
);
430 fpci_writel(tegra
, value
, XUSB_CFG_ARU_MBOX_DATA_IN
);
432 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_CMD
);
433 value
|= MBOX_INT_EN
| MBOX_DEST_FALC
;
434 fpci_writel(tegra
, value
, XUSB_CFG_ARU_MBOX_CMD
);
437 unsigned long timeout
= jiffies
+ msecs_to_jiffies(250);
439 while (time_before(jiffies
, timeout
)) {
440 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_OWNER
);
441 if (value
== MBOX_OWNER_NONE
)
444 usleep_range(10, 20);
447 if (time_after(jiffies
, timeout
))
448 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_OWNER
);
450 if (value
!= MBOX_OWNER_NONE
)
457 static irqreturn_t
tegra_xusb_mbox_irq(int irq
, void *data
)
459 struct tegra_xusb
*tegra
= data
;
462 /* clear mailbox interrupts */
463 value
= fpci_readl(tegra
, XUSB_CFG_ARU_SMI_INTR
);
464 fpci_writel(tegra
, value
, XUSB_CFG_ARU_SMI_INTR
);
466 if (value
& MBOX_SMI_INTR_FW_HANG
)
467 dev_err(tegra
->dev
, "controller firmware hang\n");
469 return IRQ_WAKE_THREAD
;
472 static void tegra_xusb_mbox_handle(struct tegra_xusb
*tegra
,
473 const struct tegra_xusb_mbox_msg
*msg
)
475 struct tegra_xusb_padctl
*padctl
= tegra
->padctl
;
476 const struct tegra_xusb_soc
*soc
= tegra
->soc
;
477 struct device
*dev
= tegra
->dev
;
478 struct tegra_xusb_mbox_msg rsp
;
484 memset(&rsp
, 0, sizeof(rsp
));
487 case MBOX_CMD_INC_FALC_CLOCK
:
488 case MBOX_CMD_DEC_FALC_CLOCK
:
489 rsp
.data
= clk_get_rate(tegra
->falcon_clk
) / 1000;
490 if (rsp
.data
!= msg
->data
)
491 rsp
.cmd
= MBOX_CMD_NAK
;
493 rsp
.cmd
= MBOX_CMD_ACK
;
497 case MBOX_CMD_INC_SSPI_CLOCK
:
498 case MBOX_CMD_DEC_SSPI_CLOCK
:
499 if (tegra
->soc
->scale_ss_clock
) {
500 err
= tegra_xusb_set_ss_clk(tegra
, msg
->data
* 1000);
502 rsp
.cmd
= MBOX_CMD_NAK
;
504 rsp
.cmd
= MBOX_CMD_ACK
;
506 rsp
.data
= clk_get_rate(tegra
->ss_src_clk
) / 1000;
508 rsp
.cmd
= MBOX_CMD_ACK
;
509 rsp
.data
= msg
->data
;
514 case MBOX_CMD_SET_BW
:
516 * TODO: Request bandwidth once EMC scaling is supported.
517 * Ignore for now since ACK/NAK is not required for SET_BW
522 case MBOX_CMD_SAVE_DFE_CTLE_CTX
:
523 err
= tegra_xusb_padctl_usb3_save_context(padctl
, msg
->data
);
525 dev_err(dev
, "failed to save context for USB3#%u: %d\n",
527 rsp
.cmd
= MBOX_CMD_NAK
;
529 rsp
.cmd
= MBOX_CMD_ACK
;
532 rsp
.data
= msg
->data
;
535 case MBOX_CMD_START_HSIC_IDLE
:
536 case MBOX_CMD_STOP_HSIC_IDLE
:
537 if (msg
->cmd
== MBOX_CMD_STOP_HSIC_IDLE
)
542 mask
= extract_field(msg
->data
, 1 + soc
->ports
.hsic
.offset
,
543 soc
->ports
.hsic
.count
);
545 for_each_set_bit(port
, &mask
, 32) {
546 err
= tegra_xusb_padctl_hsic_set_idle(padctl
, port
,
553 dev_err(dev
, "failed to set HSIC#%u %s: %d\n", port
,
554 idle
? "idle" : "busy", err
);
555 rsp
.cmd
= MBOX_CMD_NAK
;
557 rsp
.cmd
= MBOX_CMD_ACK
;
560 rsp
.data
= msg
->data
;
563 case MBOX_CMD_DISABLE_SS_LFPS_DETECTION
:
564 case MBOX_CMD_ENABLE_SS_LFPS_DETECTION
:
565 if (msg
->cmd
== MBOX_CMD_DISABLE_SS_LFPS_DETECTION
)
570 mask
= extract_field(msg
->data
, 1 + soc
->ports
.usb3
.offset
,
571 soc
->ports
.usb3
.count
);
573 for_each_set_bit(port
, &mask
, soc
->ports
.usb3
.count
) {
574 err
= tegra_xusb_padctl_usb3_set_lfps_detect(padctl
,
583 "failed to %s LFPS detection on USB3#%u: %d\n",
584 enable
? "enable" : "disable", port
, err
);
585 rsp
.cmd
= MBOX_CMD_NAK
;
587 rsp
.cmd
= MBOX_CMD_ACK
;
590 rsp
.data
= msg
->data
;
594 dev_warn(dev
, "unknown message: %#x\n", msg
->cmd
);
599 const char *cmd
= (rsp
.cmd
== MBOX_CMD_ACK
) ? "ACK" : "NAK";
601 err
= tegra_xusb_mbox_send(tegra
, &rsp
);
603 dev_err(dev
, "failed to send %s: %d\n", cmd
, err
);
607 static irqreturn_t
tegra_xusb_mbox_thread(int irq
, void *data
)
609 struct tegra_xusb
*tegra
= data
;
610 struct tegra_xusb_mbox_msg msg
;
613 mutex_lock(&tegra
->lock
);
615 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_DATA_OUT
);
616 tegra_xusb_mbox_unpack(&msg
, value
);
618 value
= fpci_readl(tegra
, XUSB_CFG_ARU_MBOX_CMD
);
619 value
&= ~MBOX_DEST_SMI
;
620 fpci_writel(tegra
, value
, XUSB_CFG_ARU_MBOX_CMD
);
622 /* clear mailbox owner if no ACK/NAK is required */
623 if (!tegra_xusb_mbox_cmd_requires_ack(msg
.cmd
))
624 fpci_writel(tegra
, MBOX_OWNER_NONE
, XUSB_CFG_ARU_MBOX_OWNER
);
626 tegra_xusb_mbox_handle(tegra
, &msg
);
628 mutex_unlock(&tegra
->lock
);
632 static void tegra_xusb_ipfs_config(struct tegra_xusb
*tegra
,
633 struct resource
*regs
)
637 value
= ipfs_readl(tegra
, IPFS_XUSB_HOST_CONFIGURATION_0
);
638 value
|= IPFS_EN_FPCI
;
639 ipfs_writel(tegra
, value
, IPFS_XUSB_HOST_CONFIGURATION_0
);
641 usleep_range(10, 20);
643 /* Program BAR0 space */
644 value
= fpci_readl(tegra
, XUSB_CFG_4
);
645 value
&= ~(XUSB_BASE_ADDR_MASK
<< XUSB_BASE_ADDR_SHIFT
);
646 value
|= regs
->start
& (XUSB_BASE_ADDR_MASK
<< XUSB_BASE_ADDR_SHIFT
);
647 fpci_writel(tegra
, value
, XUSB_CFG_4
);
649 usleep_range(100, 200);
651 /* Enable bus master */
652 value
= fpci_readl(tegra
, XUSB_CFG_1
);
653 value
|= XUSB_IO_SPACE_EN
| XUSB_MEM_SPACE_EN
| XUSB_BUS_MASTER_EN
;
654 fpci_writel(tegra
, value
, XUSB_CFG_1
);
656 /* Enable interrupt assertion */
657 value
= ipfs_readl(tegra
, IPFS_XUSB_HOST_INTR_MASK_0
);
658 value
|= IPFS_IP_INT_MASK
;
659 ipfs_writel(tegra
, value
, IPFS_XUSB_HOST_INTR_MASK_0
);
662 ipfs_writel(tegra
, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0
);
665 static int tegra_xusb_clk_enable(struct tegra_xusb
*tegra
)
669 err
= clk_prepare_enable(tegra
->pll_e
);
673 err
= clk_prepare_enable(tegra
->host_clk
);
677 err
= clk_prepare_enable(tegra
->ss_clk
);
681 err
= clk_prepare_enable(tegra
->falcon_clk
);
685 err
= clk_prepare_enable(tegra
->fs_src_clk
);
689 err
= clk_prepare_enable(tegra
->hs_src_clk
);
693 if (tegra
->soc
->scale_ss_clock
) {
694 err
= tegra_xusb_set_ss_clk(tegra
, TEGRA_XHCI_SS_HIGH_SPEED
);
702 clk_disable_unprepare(tegra
->hs_src_clk
);
704 clk_disable_unprepare(tegra
->fs_src_clk
);
706 clk_disable_unprepare(tegra
->falcon_clk
);
708 clk_disable_unprepare(tegra
->ss_clk
);
710 clk_disable_unprepare(tegra
->host_clk
);
712 clk_disable_unprepare(tegra
->pll_e
);
716 static void tegra_xusb_clk_disable(struct tegra_xusb
*tegra
)
718 clk_disable_unprepare(tegra
->pll_e
);
719 clk_disable_unprepare(tegra
->host_clk
);
720 clk_disable_unprepare(tegra
->ss_clk
);
721 clk_disable_unprepare(tegra
->falcon_clk
);
722 clk_disable_unprepare(tegra
->fs_src_clk
);
723 clk_disable_unprepare(tegra
->hs_src_clk
);
726 static int tegra_xusb_phy_enable(struct tegra_xusb
*tegra
)
731 for (i
= 0; i
< tegra
->num_phys
; i
++) {
732 err
= phy_init(tegra
->phys
[i
]);
736 err
= phy_power_on(tegra
->phys
[i
]);
738 phy_exit(tegra
->phys
[i
]);
747 phy_power_off(tegra
->phys
[i
]);
748 phy_exit(tegra
->phys
[i
]);
754 static void tegra_xusb_phy_disable(struct tegra_xusb
*tegra
)
758 for (i
= 0; i
< tegra
->num_phys
; i
++) {
759 phy_power_off(tegra
->phys
[i
]);
760 phy_exit(tegra
->phys
[i
]);
764 static int tegra_xusb_load_firmware(struct tegra_xusb
*tegra
)
766 unsigned int code_tag_blocks
, code_size_blocks
, code_blocks
;
767 struct tegra_xusb_fw_header
*header
;
768 struct device
*dev
= tegra
->dev
;
769 const struct firmware
*fw
;
770 unsigned long timeout
;
777 err
= request_firmware(&fw
, tegra
->soc
->firmware
, tegra
->dev
);
779 dev_err(tegra
->dev
, "failed to request firmware: %d\n", err
);
783 /* Load Falcon controller with its firmware. */
784 header
= (struct tegra_xusb_fw_header
*)fw
->data
;
785 tegra
->fw
.size
= le32_to_cpu(header
->fwimg_len
);
787 tegra
->fw
.virt
= dma_alloc_coherent(tegra
->dev
, tegra
->fw
.size
,
788 &tegra
->fw
.phys
, GFP_KERNEL
);
789 if (!tegra
->fw
.virt
) {
790 dev_err(tegra
->dev
, "failed to allocate memory for firmware\n");
791 release_firmware(fw
);
795 header
= (struct tegra_xusb_fw_header
*)tegra
->fw
.virt
;
796 memcpy(tegra
->fw
.virt
, fw
->data
, tegra
->fw
.size
);
797 release_firmware(fw
);
799 if (csb_readl(tegra
, XUSB_CSB_MP_ILOAD_BASE_LO
) != 0) {
800 dev_info(dev
, "Firmware already loaded, Falcon state %#x\n",
801 csb_readl(tegra
, XUSB_FALC_CPUCTL
));
805 /* Program the size of DFI into ILOAD_ATTR. */
806 csb_writel(tegra
, tegra
->fw
.size
, XUSB_CSB_MP_ILOAD_ATTR
);
809 * Boot code of the firmware reads the ILOAD_BASE registers
810 * to get to the start of the DFI in system memory.
812 address
= tegra
->fw
.phys
+ sizeof(*header
);
813 csb_writel(tegra
, address
>> 32, XUSB_CSB_MP_ILOAD_BASE_HI
);
814 csb_writel(tegra
, address
, XUSB_CSB_MP_ILOAD_BASE_LO
);
816 /* Set BOOTPATH to 1 in APMAP. */
817 csb_writel(tegra
, APMAP_BOOTPATH
, XUSB_CSB_MP_APMAP
);
819 /* Invalidate L2IMEM. */
820 csb_writel(tegra
, L2IMEMOP_INVALIDATE_ALL
, XUSB_CSB_MP_L2IMEMOP_TRIG
);
823 * Initiate fetch of bootcode from system memory into L2IMEM.
824 * Program bootcode location and size in system memory.
826 code_tag_blocks
= DIV_ROUND_UP(le32_to_cpu(header
->boot_codetag
),
828 code_size_blocks
= DIV_ROUND_UP(le32_to_cpu(header
->boot_codesize
),
830 code_blocks
= code_tag_blocks
+ code_size_blocks
;
832 value
= ((code_tag_blocks
& L2IMEMOP_SIZE_SRC_OFFSET_MASK
) <<
833 L2IMEMOP_SIZE_SRC_OFFSET_SHIFT
) |
834 ((code_size_blocks
& L2IMEMOP_SIZE_SRC_COUNT_MASK
) <<
835 L2IMEMOP_SIZE_SRC_COUNT_SHIFT
);
836 csb_writel(tegra
, value
, XUSB_CSB_MP_L2IMEMOP_SIZE
);
838 /* Trigger L2IMEM load operation. */
839 csb_writel(tegra
, L2IMEMOP_LOAD_LOCKED_RESULT
,
840 XUSB_CSB_MP_L2IMEMOP_TRIG
);
842 /* Setup Falcon auto-fill. */
843 csb_writel(tegra
, code_size_blocks
, XUSB_FALC_IMFILLCTL
);
845 value
= ((code_tag_blocks
& IMFILLRNG1_TAG_MASK
) <<
846 IMFILLRNG1_TAG_LO_SHIFT
) |
847 ((code_blocks
& IMFILLRNG1_TAG_MASK
) <<
848 IMFILLRNG1_TAG_HI_SHIFT
);
849 csb_writel(tegra
, value
, XUSB_FALC_IMFILLRNG1
);
851 csb_writel(tegra
, 0, XUSB_FALC_DMACTL
);
855 csb_writel(tegra
, le32_to_cpu(header
->boot_codetag
),
858 /* Boot Falcon CPU and wait for it to enter the STOPPED (idle) state. */
859 timeout
= jiffies
+ msecs_to_jiffies(5);
861 csb_writel(tegra
, CPUCTL_STARTCPU
, XUSB_FALC_CPUCTL
);
863 while (time_before(jiffies
, timeout
)) {
864 if (csb_readl(tegra
, XUSB_FALC_CPUCTL
) == CPUCTL_STATE_STOPPED
)
867 usleep_range(100, 200);
870 if (csb_readl(tegra
, XUSB_FALC_CPUCTL
) != CPUCTL_STATE_STOPPED
) {
871 dev_err(dev
, "Falcon failed to start, state: %#x\n",
872 csb_readl(tegra
, XUSB_FALC_CPUCTL
));
876 timestamp
= le32_to_cpu(header
->fwimg_created_time
);
877 time64_to_tm(timestamp
, 0, &time
);
879 dev_info(dev
, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
880 time
.tm_year
+ 1900, time
.tm_mon
+ 1, time
.tm_mday
,
881 time
.tm_hour
, time
.tm_min
, time
.tm_sec
);
886 static int tegra_xusb_probe(struct platform_device
*pdev
)
888 struct tegra_xusb_mbox_msg msg
;
889 struct resource
*res
, *regs
;
890 struct tegra_xusb
*tegra
;
891 struct xhci_hcd
*xhci
;
892 unsigned int i
, j
, k
;
896 BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header
) != 256);
898 tegra
= devm_kzalloc(&pdev
->dev
, sizeof(*tegra
), GFP_KERNEL
);
902 tegra
->soc
= of_device_get_match_data(&pdev
->dev
);
903 mutex_init(&tegra
->lock
);
904 tegra
->dev
= &pdev
->dev
;
906 regs
= platform_get_resource(pdev
, IORESOURCE_MEM
, 0);
907 tegra
->regs
= devm_ioremap_resource(&pdev
->dev
, regs
);
908 if (IS_ERR(tegra
->regs
))
909 return PTR_ERR(tegra
->regs
);
911 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 1);
912 tegra
->fpci_base
= devm_ioremap_resource(&pdev
->dev
, res
);
913 if (IS_ERR(tegra
->fpci_base
))
914 return PTR_ERR(tegra
->fpci_base
);
916 res
= platform_get_resource(pdev
, IORESOURCE_MEM
, 2);
917 tegra
->ipfs_base
= devm_ioremap_resource(&pdev
->dev
, res
);
918 if (IS_ERR(tegra
->ipfs_base
))
919 return PTR_ERR(tegra
->ipfs_base
);
921 tegra
->xhci_irq
= platform_get_irq(pdev
, 0);
922 if (tegra
->xhci_irq
< 0)
923 return tegra
->xhci_irq
;
925 tegra
->mbox_irq
= platform_get_irq(pdev
, 1);
926 if (tegra
->mbox_irq
< 0)
927 return tegra
->mbox_irq
;
929 tegra
->padctl
= tegra_xusb_padctl_get(&pdev
->dev
);
930 if (IS_ERR(tegra
->padctl
))
931 return PTR_ERR(tegra
->padctl
);
933 tegra
->host_rst
= devm_reset_control_get(&pdev
->dev
, "xusb_host");
934 if (IS_ERR(tegra
->host_rst
)) {
935 err
= PTR_ERR(tegra
->host_rst
);
936 dev_err(&pdev
->dev
, "failed to get xusb_host reset: %d\n", err
);
940 tegra
->ss_rst
= devm_reset_control_get(&pdev
->dev
, "xusb_ss");
941 if (IS_ERR(tegra
->ss_rst
)) {
942 err
= PTR_ERR(tegra
->ss_rst
);
943 dev_err(&pdev
->dev
, "failed to get xusb_ss reset: %d\n", err
);
947 tegra
->host_clk
= devm_clk_get(&pdev
->dev
, "xusb_host");
948 if (IS_ERR(tegra
->host_clk
)) {
949 err
= PTR_ERR(tegra
->host_clk
);
950 dev_err(&pdev
->dev
, "failed to get xusb_host: %d\n", err
);
954 tegra
->falcon_clk
= devm_clk_get(&pdev
->dev
, "xusb_falcon_src");
955 if (IS_ERR(tegra
->falcon_clk
)) {
956 err
= PTR_ERR(tegra
->falcon_clk
);
957 dev_err(&pdev
->dev
, "failed to get xusb_falcon_src: %d\n", err
);
961 tegra
->ss_clk
= devm_clk_get(&pdev
->dev
, "xusb_ss");
962 if (IS_ERR(tegra
->ss_clk
)) {
963 err
= PTR_ERR(tegra
->ss_clk
);
964 dev_err(&pdev
->dev
, "failed to get xusb_ss: %d\n", err
);
968 tegra
->ss_src_clk
= devm_clk_get(&pdev
->dev
, "xusb_ss_src");
969 if (IS_ERR(tegra
->ss_src_clk
)) {
970 err
= PTR_ERR(tegra
->ss_src_clk
);
971 dev_err(&pdev
->dev
, "failed to get xusb_ss_src: %d\n", err
);
975 tegra
->hs_src_clk
= devm_clk_get(&pdev
->dev
, "xusb_hs_src");
976 if (IS_ERR(tegra
->hs_src_clk
)) {
977 err
= PTR_ERR(tegra
->hs_src_clk
);
978 dev_err(&pdev
->dev
, "failed to get xusb_hs_src: %d\n", err
);
982 tegra
->fs_src_clk
= devm_clk_get(&pdev
->dev
, "xusb_fs_src");
983 if (IS_ERR(tegra
->fs_src_clk
)) {
984 err
= PTR_ERR(tegra
->fs_src_clk
);
985 dev_err(&pdev
->dev
, "failed to get xusb_fs_src: %d\n", err
);
989 tegra
->pll_u_480m
= devm_clk_get(&pdev
->dev
, "pll_u_480m");
990 if (IS_ERR(tegra
->pll_u_480m
)) {
991 err
= PTR_ERR(tegra
->pll_u_480m
);
992 dev_err(&pdev
->dev
, "failed to get pll_u_480m: %d\n", err
);
996 tegra
->clk_m
= devm_clk_get(&pdev
->dev
, "clk_m");
997 if (IS_ERR(tegra
->clk_m
)) {
998 err
= PTR_ERR(tegra
->clk_m
);
999 dev_err(&pdev
->dev
, "failed to get clk_m: %d\n", err
);
1003 tegra
->pll_e
= devm_clk_get(&pdev
->dev
, "pll_e");
1004 if (IS_ERR(tegra
->pll_e
)) {
1005 err
= PTR_ERR(tegra
->pll_e
);
1006 dev_err(&pdev
->dev
, "failed to get pll_e: %d\n", err
);
1010 tegra
->supplies
= devm_kcalloc(&pdev
->dev
, tegra
->soc
->num_supplies
,
1011 sizeof(*tegra
->supplies
), GFP_KERNEL
);
1012 if (!tegra
->supplies
) {
1017 for (i
= 0; i
< tegra
->soc
->num_supplies
; i
++)
1018 tegra
->supplies
[i
].supply
= tegra
->soc
->supply_names
[i
];
1020 err
= devm_regulator_bulk_get(&pdev
->dev
, tegra
->soc
->num_supplies
,
1023 dev_err(&pdev
->dev
, "failed to get regulators: %d\n", err
);
1027 for (i
= 0; i
< tegra
->soc
->num_types
; i
++)
1028 tegra
->num_phys
+= tegra
->soc
->phy_types
[i
].num
;
1030 tegra
->phys
= devm_kcalloc(&pdev
->dev
, tegra
->num_phys
,
1031 sizeof(*tegra
->phys
), GFP_KERNEL
);
1037 for (i
= 0, k
= 0; i
< tegra
->soc
->num_types
; i
++) {
1040 for (j
= 0; j
< tegra
->soc
->phy_types
[i
].num
; j
++) {
1041 snprintf(prop
, sizeof(prop
), "%s-%d",
1042 tegra
->soc
->phy_types
[i
].name
, j
);
1044 phy
= devm_phy_optional_get(&pdev
->dev
, prop
);
1047 "failed to get PHY %s: %ld\n", prop
,
1053 tegra
->phys
[k
++] = phy
;
1057 err
= tegra_xusb_clk_enable(tegra
);
1059 dev_err(&pdev
->dev
, "failed to enable clocks: %d\n", err
);
1063 err
= regulator_bulk_enable(tegra
->soc
->num_supplies
, tegra
->supplies
);
1065 dev_err(&pdev
->dev
, "failed to enable regulators: %d\n", err
);
1069 err
= tegra_xusb_phy_enable(tegra
);
1071 dev_err(&pdev
->dev
, "failed to enable PHYs: %d\n", err
);
1072 goto disable_regulator
;
1075 tegra_xusb_ipfs_config(tegra
, regs
);
1077 err
= tegra_xusb_load_firmware(tegra
);
1079 dev_err(&pdev
->dev
, "failed to load firmware: %d\n", err
);
1083 tegra
->hcd
= usb_create_hcd(&tegra_xhci_hc_driver
, &pdev
->dev
,
1084 dev_name(&pdev
->dev
));
1091 * This must happen after usb_create_hcd(), because usb_create_hcd()
1092 * will overwrite the drvdata of the device with the hcd it creates.
1094 platform_set_drvdata(pdev
, tegra
);
1096 tegra
->hcd
->regs
= tegra
->regs
;
1097 tegra
->hcd
->rsrc_start
= regs
->start
;
1098 tegra
->hcd
->rsrc_len
= resource_size(regs
);
1100 err
= usb_add_hcd(tegra
->hcd
, tegra
->xhci_irq
, IRQF_SHARED
);
1102 dev_err(&pdev
->dev
, "failed to add USB HCD: %d\n", err
);
1106 device_wakeup_enable(tegra
->hcd
->self
.controller
);
1108 xhci
= hcd_to_xhci(tegra
->hcd
);
1110 xhci
->shared_hcd
= usb_create_shared_hcd(&tegra_xhci_hc_driver
,
1112 dev_name(&pdev
->dev
),
1114 if (!xhci
->shared_hcd
) {
1115 dev_err(&pdev
->dev
, "failed to create shared HCD\n");
1120 err
= usb_add_hcd(xhci
->shared_hcd
, tegra
->xhci_irq
, IRQF_SHARED
);
1122 dev_err(&pdev
->dev
, "failed to add shared HCD: %d\n", err
);
1126 mutex_lock(&tegra
->lock
);
1128 /* Enable firmware messages from controller. */
1129 msg
.cmd
= MBOX_CMD_MSG_ENABLED
;
1132 err
= tegra_xusb_mbox_send(tegra
, &msg
);
1134 dev_err(&pdev
->dev
, "failed to enable messages: %d\n", err
);
1135 mutex_unlock(&tegra
->lock
);
1139 mutex_unlock(&tegra
->lock
);
1141 err
= devm_request_threaded_irq(&pdev
->dev
, tegra
->mbox_irq
,
1142 tegra_xusb_mbox_irq
,
1143 tegra_xusb_mbox_thread
, 0,
1144 dev_name(&pdev
->dev
), tegra
);
1146 dev_err(&pdev
->dev
, "failed to request IRQ: %d\n", err
);
1153 usb_remove_hcd(xhci
->shared_hcd
);
1155 usb_put_hcd(xhci
->shared_hcd
);
1157 usb_remove_hcd(tegra
->hcd
);
1159 usb_put_hcd(tegra
->hcd
);
1161 tegra_xusb_phy_disable(tegra
);
1163 regulator_bulk_disable(tegra
->soc
->num_supplies
, tegra
->supplies
);
1165 tegra_xusb_clk_disable(tegra
);
1167 tegra_xusb_padctl_put(tegra
->padctl
);
1171 static int tegra_xusb_remove(struct platform_device
*pdev
)
1173 struct tegra_xusb
*tegra
= platform_get_drvdata(pdev
);
1174 struct xhci_hcd
*xhci
= hcd_to_xhci(tegra
->hcd
);
1176 usb_remove_hcd(xhci
->shared_hcd
);
1177 usb_put_hcd(xhci
->shared_hcd
);
1178 usb_remove_hcd(tegra
->hcd
);
1179 usb_put_hcd(tegra
->hcd
);
1181 dma_free_coherent(&pdev
->dev
, tegra
->fw
.size
, tegra
->fw
.virt
,
1184 tegra_xusb_phy_disable(tegra
);
1185 regulator_bulk_disable(tegra
->soc
->num_supplies
, tegra
->supplies
);
1186 tegra_xusb_clk_disable(tegra
);
1188 tegra_xusb_padctl_put(tegra
->padctl
);
1193 #ifdef CONFIG_PM_SLEEP
1194 static int tegra_xusb_suspend(struct device
*dev
)
1196 struct tegra_xusb
*tegra
= dev_get_drvdata(dev
);
1197 struct xhci_hcd
*xhci
= hcd_to_xhci(tegra
->hcd
);
1198 bool wakeup
= device_may_wakeup(dev
);
1200 /* TODO: Powergate controller across suspend/resume. */
1201 return xhci_suspend(xhci
, wakeup
);
1204 static int tegra_xusb_resume(struct device
*dev
)
1206 struct tegra_xusb
*tegra
= dev_get_drvdata(dev
);
1207 struct xhci_hcd
*xhci
= hcd_to_xhci(tegra
->hcd
);
1209 return xhci_resume(xhci
, 0);
1213 static const struct dev_pm_ops tegra_xusb_pm_ops
= {
1214 SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend
, tegra_xusb_resume
)
1217 static const char * const tegra124_supply_names
[] = {
1225 "hvdd-usb-ss-pll-e",
1228 static const struct tegra_xusb_phy_type tegra124_phy_types
[] = {
1229 { .name
= "usb3", .num
= 2, },
1230 { .name
= "usb2", .num
= 3, },
1231 { .name
= "hsic", .num
= 2, },
1234 static const struct tegra_xusb_soc tegra124_soc
= {
1235 .firmware
= "nvidia/tegra124/xusb.bin",
1236 .supply_names
= tegra124_supply_names
,
1237 .num_supplies
= ARRAY_SIZE(tegra124_supply_names
),
1238 .phy_types
= tegra124_phy_types
,
1239 .num_types
= ARRAY_SIZE(tegra124_phy_types
),
1241 .usb2
= { .offset
= 4, .count
= 4, },
1242 .hsic
= { .offset
= 6, .count
= 2, },
1243 .usb3
= { .offset
= 0, .count
= 2, },
1245 .scale_ss_clock
= true,
1247 MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
1249 static const char * const tegra210_supply_names
[] = {
1259 static const struct tegra_xusb_phy_type tegra210_phy_types
[] = {
1260 { .name
= "usb3", .num
= 4, },
1261 { .name
= "usb2", .num
= 4, },
1262 { .name
= "hsic", .num
= 1, },
1265 static const struct tegra_xusb_soc tegra210_soc
= {
1266 .firmware
= "nvidia/tegra210/xusb.bin",
1267 .supply_names
= tegra210_supply_names
,
1268 .num_supplies
= ARRAY_SIZE(tegra210_supply_names
),
1269 .phy_types
= tegra210_phy_types
,
1270 .num_types
= ARRAY_SIZE(tegra210_phy_types
),
1272 .usb2
= { .offset
= 4, .count
= 4, },
1273 .hsic
= { .offset
= 8, .count
= 1, },
1274 .usb3
= { .offset
= 0, .count
= 4, },
1276 .scale_ss_clock
= false,
1278 MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
1280 static const struct of_device_id tegra_xusb_of_match
[] = {
1281 { .compatible
= "nvidia,tegra124-xusb", .data
= &tegra124_soc
},
1282 { .compatible
= "nvidia,tegra210-xusb", .data
= &tegra210_soc
},
1285 MODULE_DEVICE_TABLE(of
, tegra_xusb_of_match
);
1287 static struct platform_driver tegra_xusb_driver
= {
1288 .probe
= tegra_xusb_probe
,
1289 .remove
= tegra_xusb_remove
,
1291 .name
= "tegra-xusb",
1292 .pm
= &tegra_xusb_pm_ops
,
1293 .of_match_table
= tegra_xusb_of_match
,
1297 static void tegra_xhci_quirks(struct device
*dev
, struct xhci_hcd
*xhci
)
1299 xhci
->quirks
|= XHCI_PLAT
;
1302 static int tegra_xhci_setup(struct usb_hcd
*hcd
)
1304 return xhci_gen_setup(hcd
, tegra_xhci_quirks
);
1307 static const struct xhci_driver_overrides tegra_xhci_overrides __initconst
= {
1308 .reset
= tegra_xhci_setup
,
1311 static int __init
tegra_xusb_init(void)
1313 xhci_init_driver(&tegra_xhci_hc_driver
, &tegra_xhci_overrides
);
1315 return platform_driver_register(&tegra_xusb_driver
);
1317 module_init(tegra_xusb_init
);
1319 static void __exit
tegra_xusb_exit(void)
1321 platform_driver_unregister(&tegra_xusb_driver
);
1323 module_exit(tegra_xusb_exit
);
1325 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>");
1326 MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
1327 MODULE_LICENSE("GPL v2");