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/pm_domain.h>
22 #include <linux/pm_runtime.h>
23 #include <linux/regulator/consumer.h>
24 #include <linux/reset.h>
25 #include <linux/slab.h>
26 #include <soc/tegra/pmc.h>
30 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000
31 #define TEGRA_XHCI_SS_LOW_SPEED 12000000
33 /* FPCI CFG registers */
34 #define XUSB_CFG_1 0x004
35 #define XUSB_IO_SPACE_EN BIT(0)
36 #define XUSB_MEM_SPACE_EN BIT(1)
37 #define XUSB_BUS_MASTER_EN BIT(2)
38 #define XUSB_CFG_4 0x010
39 #define XUSB_BASE_ADDR_SHIFT 15
40 #define XUSB_BASE_ADDR_MASK 0x1ffff
41 #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c
42 #define XUSB_CFG_CSB_BASE_ADDR 0x800
44 /* FPCI mailbox registers */
45 #define XUSB_CFG_ARU_MBOX_CMD 0x0e4
46 #define MBOX_DEST_FALC BIT(27)
47 #define MBOX_DEST_PME BIT(28)
48 #define MBOX_DEST_SMI BIT(29)
49 #define MBOX_DEST_XHCI BIT(30)
50 #define MBOX_INT_EN BIT(31)
51 #define XUSB_CFG_ARU_MBOX_DATA_IN 0x0e8
52 #define CMD_DATA_SHIFT 0
53 #define CMD_DATA_MASK 0xffffff
54 #define CMD_TYPE_SHIFT 24
55 #define CMD_TYPE_MASK 0xff
56 #define XUSB_CFG_ARU_MBOX_DATA_OUT 0x0ec
57 #define XUSB_CFG_ARU_MBOX_OWNER 0x0f0
58 #define MBOX_OWNER_NONE 0
59 #define MBOX_OWNER_FW 1
60 #define MBOX_OWNER_SW 2
61 #define XUSB_CFG_ARU_SMI_INTR 0x428
62 #define MBOX_SMI_INTR_FW_HANG BIT(1)
63 #define MBOX_SMI_INTR_EN BIT(3)
66 #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180
67 #define IPFS_EN_FPCI BIT(0)
68 #define IPFS_XUSB_HOST_INTR_MASK_0 0x188
69 #define IPFS_IP_INT_MASK BIT(16)
70 #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc
72 #define CSB_PAGE_SELECT_MASK 0x7fffff
73 #define CSB_PAGE_SELECT_SHIFT 9
74 #define CSB_PAGE_OFFSET_MASK 0x1ff
75 #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \
77 #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK)
79 /* Falcon CSB registers */
80 #define XUSB_FALC_CPUCTL 0x100
81 #define CPUCTL_STARTCPU BIT(1)
82 #define CPUCTL_STATE_HALTED BIT(4)
83 #define CPUCTL_STATE_STOPPED BIT(5)
84 #define XUSB_FALC_BOOTVEC 0x104
85 #define XUSB_FALC_DMACTL 0x10c
86 #define XUSB_FALC_IMFILLRNG1 0x154
87 #define IMFILLRNG1_TAG_MASK 0xffff
88 #define IMFILLRNG1_TAG_LO_SHIFT 0
89 #define IMFILLRNG1_TAG_HI_SHIFT 16
90 #define XUSB_FALC_IMFILLCTL 0x158
92 /* MP CSB registers */
93 #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00
94 #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04
95 #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08
96 #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10
97 #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8
98 #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff
99 #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24
100 #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff
101 #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14
102 #define L2IMEMOP_ACTION_SHIFT 24
103 #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT)
104 #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT)
105 #define XUSB_CSB_MP_APMAP 0x10181c
106 #define APMAP_BOOTPATH BIT(31)
108 #define IMEM_BLOCK_SIZE 256
110 struct tegra_xusb_fw_header {
111 __le32 boot_loadaddr_in_imem;
112 __le32 boot_codedfi_offset;
114 __le32 boot_codesize;
116 __le16 reqphys_memsize;
117 __le16 alloc_phys_memsize;
118 __le32 rodata_img_offset;
119 __le32 rodata_section_start;
120 __le32 rodata_section_end;
123 __le32 fwimg_created_time;
124 __le32 imem_resident_start;
125 __le32 imem_resident_end;
126 __le32 idirect_start;
128 __le32 l2_imem_start;
133 __le32 phys_addr_log_buffer;
134 __le32 total_log_entries;
139 __le32 ss_low_power_entry_timeout;
141 u8 padding[139]; /* Pad to 256 bytes */
144 struct tegra_xusb_phy_type {
149 struct tegra_xusb_soc {
150 const char *firmware;
151 const char * const *supply_names;
152 unsigned int num_supplies;
153 const struct tegra_xusb_phy_type *phy_types;
154 unsigned int num_types;
160 } usb2, ulpi, hsic, usb3;
176 void __iomem *ipfs_base;
177 void __iomem *fpci_base;
179 const struct tegra_xusb_soc *soc;
181 struct regulator_bulk_data *supplies;
183 struct tegra_xusb_padctl *padctl;
185 struct clk *host_clk;
186 struct clk *falcon_clk;
188 struct clk *ss_src_clk;
189 struct clk *hs_src_clk;
190 struct clk *fs_src_clk;
191 struct clk *pll_u_480m;
195 struct reset_control *host_rst;
196 struct reset_control *ss_rst;
198 struct device *genpd_dev_host;
199 struct device *genpd_dev_ss;
200 struct device_link *genpd_dl_host;
201 struct device_link *genpd_dl_ss;
204 unsigned int num_phys;
206 /* Firmware loading related */
214 static struct hc_driver __read_mostly tegra_xhci_hc_driver;
216 static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset)
218 return readl(tegra->fpci_base + offset);
221 static inline void fpci_writel(struct tegra_xusb *tegra, u32 value,
224 writel(value, tegra->fpci_base + offset);
227 static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset)
229 return readl(tegra->ipfs_base + offset);
232 static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value,
235 writel(value, tegra->ipfs_base + offset);
238 static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset)
240 u32 page = CSB_PAGE_SELECT(offset);
241 u32 ofs = CSB_PAGE_OFFSET(offset);
243 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
245 return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs);
248 static void csb_writel(struct tegra_xusb *tegra, u32 value,
251 u32 page = CSB_PAGE_SELECT(offset);
252 u32 ofs = CSB_PAGE_OFFSET(offset);
254 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE);
255 fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs);
258 static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra,
261 unsigned long new_parent_rate, old_parent_rate;
262 struct clk *clk = tegra->ss_src_clk;
266 if (clk_get_rate(clk) == rate)
270 case TEGRA_XHCI_SS_HIGH_SPEED:
272 * Reparent to PLLU_480M. Set divider first to avoid
275 old_parent_rate = clk_get_rate(clk_get_parent(clk));
276 new_parent_rate = clk_get_rate(tegra->pll_u_480m);
277 div = new_parent_rate / rate;
279 err = clk_set_rate(clk, old_parent_rate / div);
283 err = clk_set_parent(clk, tegra->pll_u_480m);
288 * The rate should already be correct, but set it again just
291 err = clk_set_rate(clk, rate);
297 case TEGRA_XHCI_SS_LOW_SPEED:
298 /* Reparent to CLK_M */
299 err = clk_set_parent(clk, tegra->clk_m);
303 err = clk_set_rate(clk, rate);
310 dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate);
314 if (clk_get_rate(clk) != rate) {
315 dev_err(tegra->dev, "SS clock doesn't match requested rate\n");
322 static unsigned long extract_field(u32 value, unsigned int start,
325 return (value >> start) & ((1 << count) - 1);
328 /* Command requests from the firmware */
329 enum tegra_xusb_mbox_cmd {
330 MBOX_CMD_MSG_ENABLED = 1,
331 MBOX_CMD_INC_FALC_CLOCK,
332 MBOX_CMD_DEC_FALC_CLOCK,
333 MBOX_CMD_INC_SSPI_CLOCK,
334 MBOX_CMD_DEC_SSPI_CLOCK,
335 MBOX_CMD_SET_BW, /* no ACK/NAK required */
336 MBOX_CMD_SET_SS_PWR_GATING,
337 MBOX_CMD_SET_SS_PWR_UNGATING,
338 MBOX_CMD_SAVE_DFE_CTLE_CTX,
339 MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */
340 MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */
341 MBOX_CMD_START_HSIC_IDLE,
342 MBOX_CMD_STOP_HSIC_IDLE,
343 MBOX_CMD_DBC_WAKE_STACK, /* unused */
344 MBOX_CMD_HSIC_PRETEND_CONNECT,
346 MBOX_CMD_DISABLE_SS_LFPS_DETECTION,
347 MBOX_CMD_ENABLE_SS_LFPS_DETECTION,
351 /* Response message to above commands */
356 static const char * const mbox_cmd_name[] = {
358 [ 2] = "INC_FALCON_CLOCK",
359 [ 3] = "DEC_FALCON_CLOCK",
360 [ 4] = "INC_SSPI_CLOCK",
361 [ 5] = "DEC_SSPI_CLOCK",
363 [ 7] = "SET_SS_PWR_GATING",
364 [ 8] = "SET_SS_PWR_UNGATING",
365 [ 9] = "SAVE_DFE_CTLE_CTX",
366 [ 10] = "AIRPLANE_MODE_ENABLED",
367 [ 11] = "AIRPLANE_MODE_DISABLED",
368 [ 12] = "START_HSIC_IDLE",
369 [ 13] = "STOP_HSIC_IDLE",
370 [ 14] = "DBC_WAKE_STACK",
371 [ 15] = "HSIC_PRETEND_CONNECT",
372 [ 16] = "RESET_SSPI",
373 [ 17] = "DISABLE_SS_LFPS_DETECTION",
374 [ 18] = "ENABLE_SS_LFPS_DETECTION",
379 struct tegra_xusb_mbox_msg {
384 static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg)
386 return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT |
387 (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT;
389 static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg,
392 msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK;
393 msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK;
396 static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd)
399 case MBOX_CMD_SET_BW:
409 static int tegra_xusb_mbox_send(struct tegra_xusb *tegra,
410 const struct tegra_xusb_mbox_msg *msg)
412 bool wait_for_idle = false;
416 * Acquire the mailbox. The firmware still owns the mailbox for
419 if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) {
420 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
421 if (value != MBOX_OWNER_NONE) {
422 dev_err(tegra->dev, "mailbox is busy\n");
426 fpci_writel(tegra, MBOX_OWNER_SW, XUSB_CFG_ARU_MBOX_OWNER);
428 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
429 if (value != MBOX_OWNER_SW) {
430 dev_err(tegra->dev, "failed to acquire mailbox\n");
434 wait_for_idle = true;
437 value = tegra_xusb_mbox_pack(msg);
438 fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_DATA_IN);
440 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_CMD);
441 value |= MBOX_INT_EN | MBOX_DEST_FALC;
442 fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_CMD);
445 unsigned long timeout = jiffies + msecs_to_jiffies(250);
447 while (time_before(jiffies, timeout)) {
448 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
449 if (value == MBOX_OWNER_NONE)
452 usleep_range(10, 20);
455 if (time_after(jiffies, timeout))
456 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_OWNER);
458 if (value != MBOX_OWNER_NONE)
465 static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data)
467 struct tegra_xusb *tegra = data;
470 /* clear mailbox interrupts */
471 value = fpci_readl(tegra, XUSB_CFG_ARU_SMI_INTR);
472 fpci_writel(tegra, value, XUSB_CFG_ARU_SMI_INTR);
474 if (value & MBOX_SMI_INTR_FW_HANG)
475 dev_err(tegra->dev, "controller firmware hang\n");
477 return IRQ_WAKE_THREAD;
480 static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra,
481 const struct tegra_xusb_mbox_msg *msg)
483 struct tegra_xusb_padctl *padctl = tegra->padctl;
484 const struct tegra_xusb_soc *soc = tegra->soc;
485 struct device *dev = tegra->dev;
486 struct tegra_xusb_mbox_msg rsp;
492 memset(&rsp, 0, sizeof(rsp));
495 case MBOX_CMD_INC_FALC_CLOCK:
496 case MBOX_CMD_DEC_FALC_CLOCK:
497 rsp.data = clk_get_rate(tegra->falcon_clk) / 1000;
498 if (rsp.data != msg->data)
499 rsp.cmd = MBOX_CMD_NAK;
501 rsp.cmd = MBOX_CMD_ACK;
505 case MBOX_CMD_INC_SSPI_CLOCK:
506 case MBOX_CMD_DEC_SSPI_CLOCK:
507 if (tegra->soc->scale_ss_clock) {
508 err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000);
510 rsp.cmd = MBOX_CMD_NAK;
512 rsp.cmd = MBOX_CMD_ACK;
514 rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000;
516 rsp.cmd = MBOX_CMD_ACK;
517 rsp.data = msg->data;
522 case MBOX_CMD_SET_BW:
524 * TODO: Request bandwidth once EMC scaling is supported.
525 * Ignore for now since ACK/NAK is not required for SET_BW
530 case MBOX_CMD_SAVE_DFE_CTLE_CTX:
531 err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data);
533 dev_err(dev, "failed to save context for USB3#%u: %d\n",
535 rsp.cmd = MBOX_CMD_NAK;
537 rsp.cmd = MBOX_CMD_ACK;
540 rsp.data = msg->data;
543 case MBOX_CMD_START_HSIC_IDLE:
544 case MBOX_CMD_STOP_HSIC_IDLE:
545 if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE)
550 mask = extract_field(msg->data, 1 + soc->ports.hsic.offset,
551 soc->ports.hsic.count);
553 for_each_set_bit(port, &mask, 32) {
554 err = tegra_xusb_padctl_hsic_set_idle(padctl, port,
561 dev_err(dev, "failed to set HSIC#%u %s: %d\n", port,
562 idle ? "idle" : "busy", err);
563 rsp.cmd = MBOX_CMD_NAK;
565 rsp.cmd = MBOX_CMD_ACK;
568 rsp.data = msg->data;
571 case MBOX_CMD_DISABLE_SS_LFPS_DETECTION:
572 case MBOX_CMD_ENABLE_SS_LFPS_DETECTION:
573 if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION)
578 mask = extract_field(msg->data, 1 + soc->ports.usb3.offset,
579 soc->ports.usb3.count);
581 for_each_set_bit(port, &mask, soc->ports.usb3.count) {
582 err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl,
591 "failed to %s LFPS detection on USB3#%u: %d\n",
592 enable ? "enable" : "disable", port, err);
593 rsp.cmd = MBOX_CMD_NAK;
595 rsp.cmd = MBOX_CMD_ACK;
598 rsp.data = msg->data;
602 dev_warn(dev, "unknown message: %#x\n", msg->cmd);
607 const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK";
609 err = tegra_xusb_mbox_send(tegra, &rsp);
611 dev_err(dev, "failed to send %s: %d\n", cmd, err);
615 static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data)
617 struct tegra_xusb *tegra = data;
618 struct tegra_xusb_mbox_msg msg;
621 mutex_lock(&tegra->lock);
623 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_DATA_OUT);
624 tegra_xusb_mbox_unpack(&msg, value);
626 value = fpci_readl(tegra, XUSB_CFG_ARU_MBOX_CMD);
627 value &= ~MBOX_DEST_SMI;
628 fpci_writel(tegra, value, XUSB_CFG_ARU_MBOX_CMD);
630 /* clear mailbox owner if no ACK/NAK is required */
631 if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd))
632 fpci_writel(tegra, MBOX_OWNER_NONE, XUSB_CFG_ARU_MBOX_OWNER);
634 tegra_xusb_mbox_handle(tegra, &msg);
636 mutex_unlock(&tegra->lock);
640 static void tegra_xusb_ipfs_config(struct tegra_xusb *tegra,
641 struct resource *regs)
645 value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0);
646 value |= IPFS_EN_FPCI;
647 ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0);
649 usleep_range(10, 20);
651 /* Program BAR0 space */
652 value = fpci_readl(tegra, XUSB_CFG_4);
653 value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
654 value |= regs->start & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT);
655 fpci_writel(tegra, value, XUSB_CFG_4);
657 usleep_range(100, 200);
659 /* Enable bus master */
660 value = fpci_readl(tegra, XUSB_CFG_1);
661 value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN;
662 fpci_writel(tegra, value, XUSB_CFG_1);
664 /* Enable interrupt assertion */
665 value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0);
666 value |= IPFS_IP_INT_MASK;
667 ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0);
670 ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0);
673 static int tegra_xusb_clk_enable(struct tegra_xusb *tegra)
677 err = clk_prepare_enable(tegra->pll_e);
681 err = clk_prepare_enable(tegra->host_clk);
685 err = clk_prepare_enable(tegra->ss_clk);
689 err = clk_prepare_enable(tegra->falcon_clk);
693 err = clk_prepare_enable(tegra->fs_src_clk);
697 err = clk_prepare_enable(tegra->hs_src_clk);
701 if (tegra->soc->scale_ss_clock) {
702 err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED);
710 clk_disable_unprepare(tegra->hs_src_clk);
712 clk_disable_unprepare(tegra->fs_src_clk);
714 clk_disable_unprepare(tegra->falcon_clk);
716 clk_disable_unprepare(tegra->ss_clk);
718 clk_disable_unprepare(tegra->host_clk);
720 clk_disable_unprepare(tegra->pll_e);
724 static void tegra_xusb_clk_disable(struct tegra_xusb *tegra)
726 clk_disable_unprepare(tegra->pll_e);
727 clk_disable_unprepare(tegra->host_clk);
728 clk_disable_unprepare(tegra->ss_clk);
729 clk_disable_unprepare(tegra->falcon_clk);
730 clk_disable_unprepare(tegra->fs_src_clk);
731 clk_disable_unprepare(tegra->hs_src_clk);
734 static int tegra_xusb_phy_enable(struct tegra_xusb *tegra)
739 for (i = 0; i < tegra->num_phys; i++) {
740 err = phy_init(tegra->phys[i]);
744 err = phy_power_on(tegra->phys[i]);
746 phy_exit(tegra->phys[i]);
755 phy_power_off(tegra->phys[i]);
756 phy_exit(tegra->phys[i]);
762 static void tegra_xusb_phy_disable(struct tegra_xusb *tegra)
766 for (i = 0; i < tegra->num_phys; i++) {
767 phy_power_off(tegra->phys[i]);
768 phy_exit(tegra->phys[i]);
772 static int tegra_xusb_runtime_suspend(struct device *dev)
774 struct tegra_xusb *tegra = dev_get_drvdata(dev);
776 tegra_xusb_phy_disable(tegra);
777 regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
778 tegra_xusb_clk_disable(tegra);
783 static int tegra_xusb_runtime_resume(struct device *dev)
785 struct tegra_xusb *tegra = dev_get_drvdata(dev);
788 err = tegra_xusb_clk_enable(tegra);
790 dev_err(dev, "failed to enable clocks: %d\n", err);
794 err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies);
796 dev_err(dev, "failed to enable regulators: %d\n", err);
800 err = tegra_xusb_phy_enable(tegra);
802 dev_err(dev, "failed to enable PHYs: %d\n", err);
803 goto disable_regulator;
809 regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies);
811 tegra_xusb_clk_disable(tegra);
815 static int tegra_xusb_load_firmware(struct tegra_xusb *tegra)
817 unsigned int code_tag_blocks, code_size_blocks, code_blocks;
818 struct tegra_xusb_fw_header *header;
819 struct device *dev = tegra->dev;
820 const struct firmware *fw;
821 unsigned long timeout;
828 err = request_firmware(&fw, tegra->soc->firmware, tegra->dev);
830 dev_err(tegra->dev, "failed to request firmware: %d\n", err);
834 /* Load Falcon controller with its firmware. */
835 header = (struct tegra_xusb_fw_header *)fw->data;
836 tegra->fw.size = le32_to_cpu(header->fwimg_len);
838 tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size,
839 &tegra->fw.phys, GFP_KERNEL);
840 if (!tegra->fw.virt) {
841 dev_err(tegra->dev, "failed to allocate memory for firmware\n");
842 release_firmware(fw);
846 header = (struct tegra_xusb_fw_header *)tegra->fw.virt;
847 memcpy(tegra->fw.virt, fw->data, tegra->fw.size);
848 release_firmware(fw);
850 if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) {
851 dev_info(dev, "Firmware already loaded, Falcon state %#x\n",
852 csb_readl(tegra, XUSB_FALC_CPUCTL));
856 /* Program the size of DFI into ILOAD_ATTR. */
857 csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR);
860 * Boot code of the firmware reads the ILOAD_BASE registers
861 * to get to the start of the DFI in system memory.
863 address = tegra->fw.phys + sizeof(*header);
864 csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI);
865 csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO);
867 /* Set BOOTPATH to 1 in APMAP. */
868 csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP);
870 /* Invalidate L2IMEM. */
871 csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG);
874 * Initiate fetch of bootcode from system memory into L2IMEM.
875 * Program bootcode location and size in system memory.
877 code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag),
879 code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize),
881 code_blocks = code_tag_blocks + code_size_blocks;
883 value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) <<
884 L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) |
885 ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) <<
886 L2IMEMOP_SIZE_SRC_COUNT_SHIFT);
887 csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE);
889 /* Trigger L2IMEM load operation. */
890 csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT,
891 XUSB_CSB_MP_L2IMEMOP_TRIG);
893 /* Setup Falcon auto-fill. */
894 csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL);
896 value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) <<
897 IMFILLRNG1_TAG_LO_SHIFT) |
898 ((code_blocks & IMFILLRNG1_TAG_MASK) <<
899 IMFILLRNG1_TAG_HI_SHIFT);
900 csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1);
902 csb_writel(tegra, 0, XUSB_FALC_DMACTL);
906 csb_writel(tegra, le32_to_cpu(header->boot_codetag),
909 /* Boot Falcon CPU and wait for it to enter the STOPPED (idle) state. */
910 timeout = jiffies + msecs_to_jiffies(5);
912 csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL);
914 while (time_before(jiffies, timeout)) {
915 if (csb_readl(tegra, XUSB_FALC_CPUCTL) == CPUCTL_STATE_STOPPED)
918 usleep_range(100, 200);
921 if (csb_readl(tegra, XUSB_FALC_CPUCTL) != CPUCTL_STATE_STOPPED) {
922 dev_err(dev, "Falcon failed to start, state: %#x\n",
923 csb_readl(tegra, XUSB_FALC_CPUCTL));
927 timestamp = le32_to_cpu(header->fwimg_created_time);
928 time64_to_tm(timestamp, 0, &time);
930 dev_info(dev, "Firmware timestamp: %ld-%02d-%02d %02d:%02d:%02d UTC\n",
931 time.tm_year + 1900, time.tm_mon + 1, time.tm_mday,
932 time.tm_hour, time.tm_min, time.tm_sec);
937 static void tegra_xusb_powerdomain_remove(struct device *dev,
938 struct tegra_xusb *tegra)
940 if (tegra->genpd_dl_ss)
941 device_link_del(tegra->genpd_dl_ss);
942 if (tegra->genpd_dl_host)
943 device_link_del(tegra->genpd_dl_host);
944 if (tegra->genpd_dev_ss)
945 dev_pm_domain_detach(tegra->genpd_dev_ss, true);
946 if (tegra->genpd_dev_host)
947 dev_pm_domain_detach(tegra->genpd_dev_host, true);
950 static int tegra_xusb_powerdomain_init(struct device *dev,
951 struct tegra_xusb *tegra)
955 tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host");
956 if (IS_ERR(tegra->genpd_dev_host)) {
957 err = PTR_ERR(tegra->genpd_dev_host);
958 dev_err(dev, "failed to get host pm-domain: %d\n", err);
962 tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss");
963 if (IS_ERR(tegra->genpd_dev_ss)) {
964 err = PTR_ERR(tegra->genpd_dev_ss);
965 dev_err(dev, "failed to get superspeed pm-domain: %d\n", err);
969 tegra->genpd_dl_host = device_link_add(dev, tegra->genpd_dev_host,
972 if (!tegra->genpd_dl_host) {
973 dev_err(dev, "adding host device link failed!\n");
977 tegra->genpd_dl_ss = device_link_add(dev, tegra->genpd_dev_ss,
980 if (!tegra->genpd_dl_ss) {
981 dev_err(dev, "adding superspeed device link failed!\n");
988 static int tegra_xusb_probe(struct platform_device *pdev)
990 struct tegra_xusb_mbox_msg msg;
991 struct resource *res, *regs;
992 struct tegra_xusb *tegra;
993 struct xhci_hcd *xhci;
994 unsigned int i, j, k;
998 BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256);
1000 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL);
1004 tegra->soc = of_device_get_match_data(&pdev->dev);
1005 mutex_init(&tegra->lock);
1006 tegra->dev = &pdev->dev;
1008 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1009 tegra->regs = devm_ioremap_resource(&pdev->dev, regs);
1010 if (IS_ERR(tegra->regs))
1011 return PTR_ERR(tegra->regs);
1013 res = platform_get_resource(pdev, IORESOURCE_MEM, 1);
1014 tegra->fpci_base = devm_ioremap_resource(&pdev->dev, res);
1015 if (IS_ERR(tegra->fpci_base))
1016 return PTR_ERR(tegra->fpci_base);
1018 res = platform_get_resource(pdev, IORESOURCE_MEM, 2);
1019 tegra->ipfs_base = devm_ioremap_resource(&pdev->dev, res);
1020 if (IS_ERR(tegra->ipfs_base))
1021 return PTR_ERR(tegra->ipfs_base);
1023 tegra->xhci_irq = platform_get_irq(pdev, 0);
1024 if (tegra->xhci_irq < 0)
1025 return tegra->xhci_irq;
1027 tegra->mbox_irq = platform_get_irq(pdev, 1);
1028 if (tegra->mbox_irq < 0)
1029 return tegra->mbox_irq;
1031 tegra->padctl = tegra_xusb_padctl_get(&pdev->dev);
1032 if (IS_ERR(tegra->padctl))
1033 return PTR_ERR(tegra->padctl);
1035 tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host");
1036 if (IS_ERR(tegra->host_clk)) {
1037 err = PTR_ERR(tegra->host_clk);
1038 dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err);
1042 tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src");
1043 if (IS_ERR(tegra->falcon_clk)) {
1044 err = PTR_ERR(tegra->falcon_clk);
1045 dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err);
1049 tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss");
1050 if (IS_ERR(tegra->ss_clk)) {
1051 err = PTR_ERR(tegra->ss_clk);
1052 dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err);
1056 tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src");
1057 if (IS_ERR(tegra->ss_src_clk)) {
1058 err = PTR_ERR(tegra->ss_src_clk);
1059 dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err);
1063 tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src");
1064 if (IS_ERR(tegra->hs_src_clk)) {
1065 err = PTR_ERR(tegra->hs_src_clk);
1066 dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err);
1070 tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src");
1071 if (IS_ERR(tegra->fs_src_clk)) {
1072 err = PTR_ERR(tegra->fs_src_clk);
1073 dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err);
1077 tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m");
1078 if (IS_ERR(tegra->pll_u_480m)) {
1079 err = PTR_ERR(tegra->pll_u_480m);
1080 dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err);
1084 tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m");
1085 if (IS_ERR(tegra->clk_m)) {
1086 err = PTR_ERR(tegra->clk_m);
1087 dev_err(&pdev->dev, "failed to get clk_m: %d\n", err);
1091 tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e");
1092 if (IS_ERR(tegra->pll_e)) {
1093 err = PTR_ERR(tegra->pll_e);
1094 dev_err(&pdev->dev, "failed to get pll_e: %d\n", err);
1098 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
1099 tegra->host_rst = devm_reset_control_get(&pdev->dev,
1101 if (IS_ERR(tegra->host_rst)) {
1102 err = PTR_ERR(tegra->host_rst);
1104 "failed to get xusb_host reset: %d\n", err);
1108 tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss");
1109 if (IS_ERR(tegra->ss_rst)) {
1110 err = PTR_ERR(tegra->ss_rst);
1111 dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n",
1116 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA,
1121 "failed to enable XUSBA domain: %d\n", err);
1125 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC,
1129 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
1131 "failed to enable XUSBC domain: %d\n", err);
1135 err = tegra_xusb_powerdomain_init(&pdev->dev, tegra);
1137 goto put_powerdomains;
1140 tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies,
1141 sizeof(*tegra->supplies), GFP_KERNEL);
1142 if (!tegra->supplies) {
1144 goto put_powerdomains;
1147 for (i = 0; i < tegra->soc->num_supplies; i++)
1148 tegra->supplies[i].supply = tegra->soc->supply_names[i];
1150 err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies,
1153 dev_err(&pdev->dev, "failed to get regulators: %d\n", err);
1154 goto put_powerdomains;
1157 for (i = 0; i < tegra->soc->num_types; i++)
1158 tegra->num_phys += tegra->soc->phy_types[i].num;
1160 tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys,
1161 sizeof(*tegra->phys), GFP_KERNEL);
1164 goto put_powerdomains;
1167 for (i = 0, k = 0; i < tegra->soc->num_types; i++) {
1170 for (j = 0; j < tegra->soc->phy_types[i].num; j++) {
1171 snprintf(prop, sizeof(prop), "%s-%d",
1172 tegra->soc->phy_types[i].name, j);
1174 phy = devm_phy_optional_get(&pdev->dev, prop);
1177 "failed to get PHY %s: %ld\n", prop,
1180 goto put_powerdomains;
1183 tegra->phys[k++] = phy;
1187 tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev,
1188 dev_name(&pdev->dev));
1191 goto put_powerdomains;
1195 * This must happen after usb_create_hcd(), because usb_create_hcd()
1196 * will overwrite the drvdata of the device with the hcd it creates.
1198 platform_set_drvdata(pdev, tegra);
1200 pm_runtime_enable(&pdev->dev);
1201 if (pm_runtime_enabled(&pdev->dev))
1202 err = pm_runtime_get_sync(&pdev->dev);
1204 err = tegra_xusb_runtime_resume(&pdev->dev);
1207 dev_err(&pdev->dev, "failed to enable device: %d\n", err);
1211 tegra_xusb_ipfs_config(tegra, regs);
1213 err = tegra_xusb_load_firmware(tegra);
1215 dev_err(&pdev->dev, "failed to load firmware: %d\n", err);
1219 tegra->hcd->regs = tegra->regs;
1220 tegra->hcd->rsrc_start = regs->start;
1221 tegra->hcd->rsrc_len = resource_size(regs);
1223 err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED);
1225 dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err);
1229 device_wakeup_enable(tegra->hcd->self.controller);
1231 xhci = hcd_to_xhci(tegra->hcd);
1233 xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver,
1235 dev_name(&pdev->dev),
1237 if (!xhci->shared_hcd) {
1238 dev_err(&pdev->dev, "failed to create shared HCD\n");
1243 err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED);
1245 dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err);
1249 mutex_lock(&tegra->lock);
1251 /* Enable firmware messages from controller. */
1252 msg.cmd = MBOX_CMD_MSG_ENABLED;
1255 err = tegra_xusb_mbox_send(tegra, &msg);
1257 dev_err(&pdev->dev, "failed to enable messages: %d\n", err);
1258 mutex_unlock(&tegra->lock);
1262 mutex_unlock(&tegra->lock);
1264 err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq,
1265 tegra_xusb_mbox_irq,
1266 tegra_xusb_mbox_thread, 0,
1267 dev_name(&pdev->dev), tegra);
1269 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err);
1276 usb_remove_hcd(xhci->shared_hcd);
1278 usb_put_hcd(xhci->shared_hcd);
1280 usb_remove_hcd(tegra->hcd);
1282 if (!pm_runtime_status_suspended(&pdev->dev))
1283 tegra_xusb_runtime_suspend(&pdev->dev);
1285 pm_runtime_disable(&pdev->dev);
1286 usb_put_hcd(tegra->hcd);
1288 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
1289 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
1290 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
1292 tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
1295 tegra_xusb_padctl_put(tegra->padctl);
1299 static int tegra_xusb_remove(struct platform_device *pdev)
1301 struct tegra_xusb *tegra = platform_get_drvdata(pdev);
1302 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1304 usb_remove_hcd(xhci->shared_hcd);
1305 usb_put_hcd(xhci->shared_hcd);
1306 xhci->shared_hcd = NULL;
1307 usb_remove_hcd(tegra->hcd);
1308 usb_put_hcd(tegra->hcd);
1310 dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt,
1313 pm_runtime_put_sync(&pdev->dev);
1314 pm_runtime_disable(&pdev->dev);
1316 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) {
1317 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC);
1318 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA);
1320 tegra_xusb_powerdomain_remove(&pdev->dev, tegra);
1323 tegra_xusb_padctl_put(tegra->padctl);
1328 #ifdef CONFIG_PM_SLEEP
1329 static int tegra_xusb_suspend(struct device *dev)
1331 struct tegra_xusb *tegra = dev_get_drvdata(dev);
1332 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1333 bool wakeup = device_may_wakeup(dev);
1335 /* TODO: Powergate controller across suspend/resume. */
1336 return xhci_suspend(xhci, wakeup);
1339 static int tegra_xusb_resume(struct device *dev)
1341 struct tegra_xusb *tegra = dev_get_drvdata(dev);
1342 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd);
1344 return xhci_resume(xhci, 0);
1348 static const struct dev_pm_ops tegra_xusb_pm_ops = {
1349 SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend,
1350 tegra_xusb_runtime_resume, NULL)
1351 SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume)
1354 static const char * const tegra124_supply_names[] = {
1362 "hvdd-usb-ss-pll-e",
1365 static const struct tegra_xusb_phy_type tegra124_phy_types[] = {
1366 { .name = "usb3", .num = 2, },
1367 { .name = "usb2", .num = 3, },
1368 { .name = "hsic", .num = 2, },
1371 static const struct tegra_xusb_soc tegra124_soc = {
1372 .firmware = "nvidia/tegra124/xusb.bin",
1373 .supply_names = tegra124_supply_names,
1374 .num_supplies = ARRAY_SIZE(tegra124_supply_names),
1375 .phy_types = tegra124_phy_types,
1376 .num_types = ARRAY_SIZE(tegra124_phy_types),
1378 .usb2 = { .offset = 4, .count = 4, },
1379 .hsic = { .offset = 6, .count = 2, },
1380 .usb3 = { .offset = 0, .count = 2, },
1382 .scale_ss_clock = true,
1384 MODULE_FIRMWARE("nvidia/tegra124/xusb.bin");
1386 static const char * const tegra210_supply_names[] = {
1396 static const struct tegra_xusb_phy_type tegra210_phy_types[] = {
1397 { .name = "usb3", .num = 4, },
1398 { .name = "usb2", .num = 4, },
1399 { .name = "hsic", .num = 1, },
1402 static const struct tegra_xusb_soc tegra210_soc = {
1403 .firmware = "nvidia/tegra210/xusb.bin",
1404 .supply_names = tegra210_supply_names,
1405 .num_supplies = ARRAY_SIZE(tegra210_supply_names),
1406 .phy_types = tegra210_phy_types,
1407 .num_types = ARRAY_SIZE(tegra210_phy_types),
1409 .usb2 = { .offset = 4, .count = 4, },
1410 .hsic = { .offset = 8, .count = 1, },
1411 .usb3 = { .offset = 0, .count = 4, },
1413 .scale_ss_clock = false,
1415 MODULE_FIRMWARE("nvidia/tegra210/xusb.bin");
1417 static const struct of_device_id tegra_xusb_of_match[] = {
1418 { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc },
1419 { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc },
1422 MODULE_DEVICE_TABLE(of, tegra_xusb_of_match);
1424 static struct platform_driver tegra_xusb_driver = {
1425 .probe = tegra_xusb_probe,
1426 .remove = tegra_xusb_remove,
1428 .name = "tegra-xusb",
1429 .pm = &tegra_xusb_pm_ops,
1430 .of_match_table = tegra_xusb_of_match,
1434 static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci)
1436 xhci->quirks |= XHCI_PLAT;
1439 static int tegra_xhci_setup(struct usb_hcd *hcd)
1441 return xhci_gen_setup(hcd, tegra_xhci_quirks);
1444 static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = {
1445 .reset = tegra_xhci_setup,
1448 static int __init tegra_xusb_init(void)
1450 xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides);
1452 return platform_driver_register(&tegra_xusb_driver);
1454 module_init(tegra_xusb_init);
1456 static void __exit tegra_xusb_exit(void)
1458 platform_driver_unregister(&tegra_xusb_driver);
1460 module_exit(tegra_xusb_exit);
1463 MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver");
1464 MODULE_LICENSE("GPL v2");