1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVIDIA Tegra xHCI host controller driver 4 * 5 * Copyright (c) 2014-2020, NVIDIA CORPORATION. All rights reserved. 6 * Copyright (C) 2014 Google, Inc. 7 */ 8 9 #include <linux/clk.h> 10 #include <linux/delay.h> 11 #include <linux/dma-mapping.h> 12 #include <linux/firmware.h> 13 #include <linux/interrupt.h> 14 #include <linux/iopoll.h> 15 #include <linux/kernel.h> 16 #include <linux/module.h> 17 #include <linux/of.h> 18 #include <linux/of_irq.h> 19 #include <linux/phy/phy.h> 20 #include <linux/phy/tegra/xusb.h> 21 #include <linux/platform_device.h> 22 #include <linux/usb/ch9.h> 23 #include <linux/pm.h> 24 #include <linux/pm_domain.h> 25 #include <linux/pm_runtime.h> 26 #include <linux/regulator/consumer.h> 27 #include <linux/reset.h> 28 #include <linux/slab.h> 29 #include <linux/string_choices.h> 30 #include <linux/usb/otg.h> 31 #include <linux/usb/phy.h> 32 #include <linux/usb/role.h> 33 #include <soc/tegra/pmc.h> 34 35 #include "xhci.h" 36 37 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000 38 #define TEGRA_XHCI_SS_LOW_SPEED 12000000 39 40 /* FPCI CFG registers */ 41 #define XUSB_CFG_1 0x004 42 #define XUSB_IO_SPACE_EN BIT(0) 43 #define XUSB_MEM_SPACE_EN BIT(1) 44 #define XUSB_BUS_MASTER_EN BIT(2) 45 #define XUSB_CFG_4 0x010 46 #define XUSB_BASE_ADDR_SHIFT 15 47 #define XUSB_BASE_ADDR_MASK 0x1ffff 48 #define XUSB_CFG_7 0x01c 49 #define XUSB_BASE2_ADDR_SHIFT 16 50 #define XUSB_BASE2_ADDR_MASK 0xffff 51 #define XUSB_CFG_16 0x040 52 #define XUSB_CFG_24 0x060 53 #define XUSB_CFG_AXI_CFG 0x0f8 54 #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c 55 #define XUSB_CFG_ARU_CONTEXT 0x43c 56 #define XUSB_CFG_ARU_CONTEXT_HS_PLS 0x478 57 #define XUSB_CFG_ARU_CONTEXT_FS_PLS 0x47c 58 #define XUSB_CFG_ARU_CONTEXT_HSFS_SPEED 0x480 59 #define XUSB_CFG_ARU_CONTEXT_HSFS_PP 0x484 60 #define XUSB_CFG_CSB_BASE_ADDR 0x800 61 62 /* FPCI mailbox registers */ 63 /* XUSB_CFG_ARU_MBOX_CMD */ 64 #define MBOX_DEST_FALC BIT(27) 65 #define MBOX_DEST_PME BIT(28) 66 #define MBOX_DEST_SMI BIT(29) 67 #define MBOX_DEST_XHCI BIT(30) 68 #define MBOX_INT_EN BIT(31) 69 /* XUSB_CFG_ARU_MBOX_DATA_IN and XUSB_CFG_ARU_MBOX_DATA_OUT */ 70 #define CMD_DATA_SHIFT 0 71 #define CMD_DATA_MASK 0xffffff 72 #define CMD_TYPE_SHIFT 24 73 #define CMD_TYPE_MASK 0xff 74 /* XUSB_CFG_ARU_MBOX_OWNER */ 75 #define MBOX_OWNER_NONE 0 76 #define MBOX_OWNER_FW 1 77 #define MBOX_OWNER_SW 2 78 #define XUSB_CFG_ARU_SMI_INTR 0x428 79 #define MBOX_SMI_INTR_FW_HANG BIT(1) 80 #define MBOX_SMI_INTR_EN BIT(3) 81 82 /* BAR2 registers */ 83 #define XUSB_BAR2_ARU_MBOX_CMD 0x004 84 #define XUSB_BAR2_ARU_MBOX_DATA_IN 0x008 85 #define XUSB_BAR2_ARU_MBOX_DATA_OUT 0x00c 86 #define XUSB_BAR2_ARU_MBOX_OWNER 0x010 87 #define XUSB_BAR2_ARU_SMI_INTR 0x014 88 #define XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0 0x01c 89 #define XUSB_BAR2_ARU_IFRDMA_CFG0 0x0e0 90 #define XUSB_BAR2_ARU_IFRDMA_CFG1 0x0e4 91 #define XUSB_BAR2_ARU_IFRDMA_STREAMID_FIELD 0x0e8 92 #define XUSB_BAR2_ARU_C11_CSBRANGE 0x9c 93 #define XUSB_BAR2_ARU_FW_SCRATCH 0x1000 94 #define XUSB_BAR2_CSB_BASE_ADDR 0x2000 95 96 /* IPFS registers */ 97 #define IPFS_XUSB_HOST_MSI_BAR_SZ_0 0x0c0 98 #define IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0 0x0c4 99 #define IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0 0x0c8 100 #define IPFS_XUSB_HOST_MSI_VEC0_0 0x100 101 #define IPFS_XUSB_HOST_MSI_EN_VEC0_0 0x140 102 #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180 103 #define IPFS_EN_FPCI BIT(0) 104 #define IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0 0x184 105 #define IPFS_XUSB_HOST_INTR_MASK_0 0x188 106 #define IPFS_IP_INT_MASK BIT(16) 107 #define IPFS_XUSB_HOST_INTR_ENABLE_0 0x198 108 #define IPFS_XUSB_HOST_UFPCI_CONFIG_0 0x19c 109 #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc 110 #define IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0 0x1dc 111 112 #define CSB_PAGE_SELECT_MASK 0x7fffff 113 #define CSB_PAGE_SELECT_SHIFT 9 114 #define CSB_PAGE_OFFSET_MASK 0x1ff 115 #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \ 116 CSB_PAGE_SELECT_MASK) 117 #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK) 118 119 /* Falcon CSB registers */ 120 #define XUSB_FALC_CPUCTL 0x100 121 #define CPUCTL_STARTCPU BIT(1) 122 #define CPUCTL_STATE_HALTED BIT(4) 123 #define CPUCTL_STATE_STOPPED BIT(5) 124 #define XUSB_FALC_BOOTVEC 0x104 125 #define XUSB_FALC_DMACTL 0x10c 126 #define XUSB_FALC_IMFILLRNG1 0x154 127 #define IMFILLRNG1_TAG_MASK 0xffff 128 #define IMFILLRNG1_TAG_LO_SHIFT 0 129 #define IMFILLRNG1_TAG_HI_SHIFT 16 130 #define XUSB_FALC_IMFILLCTL 0x158 131 132 /* CSB ARU registers */ 133 #define XUSB_CSB_ARU_SCRATCH0 0x100100 134 135 /* MP CSB registers */ 136 #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00 137 #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04 138 #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08 139 #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10 140 #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8 141 #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff 142 #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24 143 #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff 144 #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14 145 #define L2IMEMOP_ACTION_SHIFT 24 146 #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT) 147 #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT) 148 #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT 0x101a18 149 #define L2IMEMOP_RESULT_VLD BIT(31) 150 #define XUSB_CSB_MP_APMAP 0x10181c 151 #define APMAP_BOOTPATH BIT(31) 152 153 #define IMEM_BLOCK_SIZE 256 154 155 #define FW_IOCTL_TYPE_SHIFT 24 156 #define FW_IOCTL_CFGTBL_READ 17 157 158 #define WAKE_IRQ_START_INDEX 2 159 160 struct tegra_xusb_fw_header { 161 __le32 boot_loadaddr_in_imem; 162 __le32 boot_codedfi_offset; 163 __le32 boot_codetag; 164 __le32 boot_codesize; 165 __le32 phys_memaddr; 166 __le16 reqphys_memsize; 167 __le16 alloc_phys_memsize; 168 __le32 rodata_img_offset; 169 __le32 rodata_section_start; 170 __le32 rodata_section_end; 171 __le32 main_fnaddr; 172 __le32 fwimg_cksum; 173 __le32 fwimg_created_time; 174 __le32 imem_resident_start; 175 __le32 imem_resident_end; 176 __le32 idirect_start; 177 __le32 idirect_end; 178 __le32 l2_imem_start; 179 __le32 l2_imem_end; 180 __le32 version_id; 181 u8 init_ddirect; 182 u8 reserved[3]; 183 __le32 phys_addr_log_buffer; 184 __le32 total_log_entries; 185 __le32 dequeue_ptr; 186 __le32 dummy_var[2]; 187 __le32 fwimg_len; 188 u8 magic[8]; 189 __le32 ss_low_power_entry_timeout; 190 u8 num_hsic_port; 191 u8 padding[139]; /* Pad to 256 bytes */ 192 }; 193 194 struct tegra_xusb_phy_type { 195 const char *name; 196 unsigned int num; 197 }; 198 199 struct tegra_xusb_mbox_regs { 200 u16 cmd; 201 u16 data_in; 202 u16 data_out; 203 u16 owner; 204 u16 smi_intr; 205 }; 206 207 struct tegra_xusb_context_soc { 208 struct { 209 const unsigned int *offsets; 210 unsigned int num_offsets; 211 } ipfs; 212 213 struct { 214 const unsigned int *offsets; 215 unsigned int num_offsets; 216 } fpci; 217 }; 218 219 struct tegra_xusb; 220 struct tegra_xusb_soc_ops { 221 u32 (*mbox_reg_readl)(struct tegra_xusb *tegra, unsigned int offset); 222 void (*mbox_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset); 223 u32 (*csb_reg_readl)(struct tegra_xusb *tegra, unsigned int offset); 224 void (*csb_reg_writel)(struct tegra_xusb *tegra, u32 value, unsigned int offset); 225 }; 226 227 struct tegra_xusb_soc { 228 const char *firmware; 229 const char * const *supply_names; 230 unsigned int num_supplies; 231 const struct tegra_xusb_phy_type *phy_types; 232 unsigned int num_types; 233 unsigned int max_num_wakes; 234 const struct tegra_xusb_context_soc *context; 235 236 struct { 237 struct { 238 unsigned int offset; 239 unsigned int count; 240 } usb2, ulpi, hsic, usb3; 241 } ports; 242 243 struct tegra_xusb_mbox_regs mbox; 244 const struct tegra_xusb_soc_ops *ops; 245 246 bool scale_ss_clock; 247 bool has_ipfs; 248 bool lpm_support; 249 bool otg_reset_sspi; 250 bool otg_set_port_power; 251 252 bool has_bar2; 253 }; 254 255 struct tegra_xusb_context { 256 u32 *ipfs; 257 u32 *fpci; 258 }; 259 260 struct tegra_xusb { 261 struct device *dev; 262 void __iomem *regs; 263 struct usb_hcd *hcd; 264 265 struct mutex lock; 266 267 int xhci_irq; 268 int mbox_irq; 269 int padctl_irq; 270 int *wake_irqs; 271 272 void __iomem *ipfs_base; 273 void __iomem *fpci_base; 274 void __iomem *bar2_base; 275 struct resource *bar2; 276 277 const struct tegra_xusb_soc *soc; 278 279 struct regulator_bulk_data *supplies; 280 281 struct tegra_xusb_padctl *padctl; 282 283 struct clk *host_clk; 284 struct clk *falcon_clk; 285 struct clk *ss_clk; 286 struct clk *ss_src_clk; 287 struct clk *hs_src_clk; 288 struct clk *fs_src_clk; 289 struct clk *pll_u_480m; 290 struct clk *clk_m; 291 struct clk *pll_e; 292 293 struct reset_control *host_rst; 294 struct reset_control *ss_rst; 295 296 struct tegra_pmc *pmc; 297 struct device *genpd_dev_host; 298 struct device *genpd_dev_ss; 299 bool use_genpd; 300 301 struct phy **phys; 302 unsigned int num_phys; 303 304 struct usb_phy **usbphy; 305 unsigned int num_usb_phys; 306 int otg_usb2_port; 307 int otg_usb3_port; 308 bool host_mode; 309 struct notifier_block id_nb; 310 struct work_struct id_work; 311 312 /* Firmware loading related */ 313 struct { 314 size_t size; 315 void *virt; 316 dma_addr_t phys; 317 } fw; 318 319 bool suspended; 320 struct tegra_xusb_context context; 321 u8 lp0_utmi_pad_mask; 322 int num_wakes; 323 }; 324 325 static struct hc_driver __read_mostly tegra_xhci_hc_driver; 326 327 static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset) 328 { 329 return readl(tegra->fpci_base + offset); 330 } 331 332 static inline void fpci_writel(struct tegra_xusb *tegra, u32 value, 333 unsigned int offset) 334 { 335 writel(value, tegra->fpci_base + offset); 336 } 337 338 static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset) 339 { 340 return readl(tegra->ipfs_base + offset); 341 } 342 343 static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value, 344 unsigned int offset) 345 { 346 writel(value, tegra->ipfs_base + offset); 347 } 348 349 static inline u32 bar2_readl(struct tegra_xusb *tegra, unsigned int offset) 350 { 351 return readl(tegra->bar2_base + offset); 352 } 353 354 static inline void bar2_writel(struct tegra_xusb *tegra, u32 value, 355 unsigned int offset) 356 { 357 writel(value, tegra->bar2_base + offset); 358 } 359 360 static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset) 361 { 362 const struct tegra_xusb_soc_ops *ops = tegra->soc->ops; 363 364 return ops->csb_reg_readl(tegra, offset); 365 } 366 367 static void csb_writel(struct tegra_xusb *tegra, u32 value, 368 unsigned int offset) 369 { 370 const struct tegra_xusb_soc_ops *ops = tegra->soc->ops; 371 372 ops->csb_reg_writel(tegra, value, offset); 373 } 374 375 static u32 fpci_csb_readl(struct tegra_xusb *tegra, unsigned int offset) 376 { 377 u32 page = CSB_PAGE_SELECT(offset); 378 u32 ofs = CSB_PAGE_OFFSET(offset); 379 380 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE); 381 382 return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs); 383 } 384 385 static void fpci_csb_writel(struct tegra_xusb *tegra, u32 value, 386 unsigned int offset) 387 { 388 u32 page = CSB_PAGE_SELECT(offset); 389 u32 ofs = CSB_PAGE_OFFSET(offset); 390 391 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE); 392 fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs); 393 } 394 395 static u32 bar2_csb_readl(struct tegra_xusb *tegra, unsigned int offset) 396 { 397 u32 page = CSB_PAGE_SELECT(offset); 398 u32 ofs = CSB_PAGE_OFFSET(offset); 399 400 bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE); 401 402 return bar2_readl(tegra, XUSB_BAR2_CSB_BASE_ADDR + ofs); 403 } 404 405 static void bar2_csb_writel(struct tegra_xusb *tegra, u32 value, 406 unsigned int offset) 407 { 408 u32 page = CSB_PAGE_SELECT(offset); 409 u32 ofs = CSB_PAGE_OFFSET(offset); 410 411 bar2_writel(tegra, page, XUSB_BAR2_ARU_C11_CSBRANGE); 412 bar2_writel(tegra, value, XUSB_BAR2_CSB_BASE_ADDR + ofs); 413 } 414 415 static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra, 416 unsigned long rate) 417 { 418 unsigned long new_parent_rate, old_parent_rate; 419 struct clk *clk = tegra->ss_src_clk; 420 unsigned int div; 421 int err; 422 423 if (clk_get_rate(clk) == rate) 424 return 0; 425 426 switch (rate) { 427 case TEGRA_XHCI_SS_HIGH_SPEED: 428 /* 429 * Reparent to PLLU_480M. Set divider first to avoid 430 * overclocking. 431 */ 432 old_parent_rate = clk_get_rate(clk_get_parent(clk)); 433 new_parent_rate = clk_get_rate(tegra->pll_u_480m); 434 div = new_parent_rate / rate; 435 436 err = clk_set_rate(clk, old_parent_rate / div); 437 if (err) 438 return err; 439 440 err = clk_set_parent(clk, tegra->pll_u_480m); 441 if (err) 442 return err; 443 444 /* 445 * The rate should already be correct, but set it again just 446 * to be sure. 447 */ 448 err = clk_set_rate(clk, rate); 449 if (err) 450 return err; 451 452 break; 453 454 case TEGRA_XHCI_SS_LOW_SPEED: 455 /* Reparent to CLK_M */ 456 err = clk_set_parent(clk, tegra->clk_m); 457 if (err) 458 return err; 459 460 err = clk_set_rate(clk, rate); 461 if (err) 462 return err; 463 464 break; 465 466 default: 467 dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate); 468 return -EINVAL; 469 } 470 471 if (clk_get_rate(clk) != rate) { 472 dev_err(tegra->dev, "SS clock doesn't match requested rate\n"); 473 return -EINVAL; 474 } 475 476 return 0; 477 } 478 479 static unsigned long extract_field(u32 value, unsigned int start, 480 unsigned int count) 481 { 482 return (value >> start) & ((1 << count) - 1); 483 } 484 485 /* Command requests from the firmware */ 486 enum tegra_xusb_mbox_cmd { 487 MBOX_CMD_MSG_ENABLED = 1, 488 MBOX_CMD_INC_FALC_CLOCK, 489 MBOX_CMD_DEC_FALC_CLOCK, 490 MBOX_CMD_INC_SSPI_CLOCK, 491 MBOX_CMD_DEC_SSPI_CLOCK, 492 MBOX_CMD_SET_BW, /* no ACK/NAK required */ 493 MBOX_CMD_SET_SS_PWR_GATING, 494 MBOX_CMD_SET_SS_PWR_UNGATING, 495 MBOX_CMD_SAVE_DFE_CTLE_CTX, 496 MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */ 497 MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */ 498 MBOX_CMD_START_HSIC_IDLE, 499 MBOX_CMD_STOP_HSIC_IDLE, 500 MBOX_CMD_DBC_WAKE_STACK, /* unused */ 501 MBOX_CMD_HSIC_PRETEND_CONNECT, 502 MBOX_CMD_RESET_SSPI, 503 MBOX_CMD_DISABLE_SS_LFPS_DETECTION, 504 MBOX_CMD_ENABLE_SS_LFPS_DETECTION, 505 506 MBOX_CMD_MAX, 507 508 /* Response message to above commands */ 509 MBOX_CMD_ACK = 128, 510 MBOX_CMD_NAK 511 }; 512 513 struct tegra_xusb_mbox_msg { 514 u32 cmd; 515 u32 data; 516 }; 517 518 static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg) 519 { 520 return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT | 521 (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT; 522 } 523 static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg, 524 u32 value) 525 { 526 msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK; 527 msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK; 528 } 529 530 static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd) 531 { 532 switch (cmd) { 533 case MBOX_CMD_SET_BW: 534 case MBOX_CMD_ACK: 535 case MBOX_CMD_NAK: 536 return false; 537 538 default: 539 return true; 540 } 541 } 542 543 static int tegra_xusb_mbox_send(struct tegra_xusb *tegra, 544 const struct tegra_xusb_mbox_msg *msg) 545 { 546 const struct tegra_xusb_soc_ops *ops = tegra->soc->ops; 547 bool wait_for_idle = false; 548 u32 value; 549 550 /* 551 * Acquire the mailbox. The firmware still owns the mailbox for 552 * ACK/NAK messages. 553 */ 554 if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) { 555 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner); 556 if (value != MBOX_OWNER_NONE) { 557 dev_err(tegra->dev, "mailbox is busy\n"); 558 return -EBUSY; 559 } 560 561 ops->mbox_reg_writel(tegra, MBOX_OWNER_SW, tegra->soc->mbox.owner); 562 563 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner); 564 if (value != MBOX_OWNER_SW) { 565 dev_err(tegra->dev, "failed to acquire mailbox\n"); 566 return -EBUSY; 567 } 568 569 wait_for_idle = true; 570 } 571 572 value = tegra_xusb_mbox_pack(msg); 573 ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.data_in); 574 575 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd); 576 value |= MBOX_INT_EN | MBOX_DEST_FALC; 577 ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd); 578 579 if (wait_for_idle) { 580 unsigned long timeout = jiffies + msecs_to_jiffies(250); 581 582 while (time_before(jiffies, timeout)) { 583 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner); 584 if (value == MBOX_OWNER_NONE) 585 break; 586 587 usleep_range(10, 20); 588 } 589 590 if (time_after(jiffies, timeout)) 591 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.owner); 592 593 if (value != MBOX_OWNER_NONE) 594 return -ETIMEDOUT; 595 } 596 597 return 0; 598 } 599 600 static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data) 601 { 602 struct tegra_xusb *tegra = data; 603 const struct tegra_xusb_soc_ops *ops = tegra->soc->ops; 604 u32 value; 605 606 /* clear mailbox interrupts */ 607 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.smi_intr); 608 ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.smi_intr); 609 610 if (value & MBOX_SMI_INTR_FW_HANG) 611 dev_err(tegra->dev, "controller firmware hang\n"); 612 613 return IRQ_WAKE_THREAD; 614 } 615 616 static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra, 617 const struct tegra_xusb_mbox_msg *msg) 618 { 619 struct tegra_xusb_padctl *padctl = tegra->padctl; 620 const struct tegra_xusb_soc *soc = tegra->soc; 621 struct device *dev = tegra->dev; 622 struct tegra_xusb_mbox_msg rsp; 623 unsigned long mask; 624 unsigned int port; 625 bool idle, enable; 626 int err = 0; 627 628 memset(&rsp, 0, sizeof(rsp)); 629 630 switch (msg->cmd) { 631 case MBOX_CMD_INC_FALC_CLOCK: 632 case MBOX_CMD_DEC_FALC_CLOCK: 633 rsp.data = clk_get_rate(tegra->falcon_clk) / 1000; 634 if (rsp.data != msg->data) 635 rsp.cmd = MBOX_CMD_NAK; 636 else 637 rsp.cmd = MBOX_CMD_ACK; 638 639 break; 640 641 case MBOX_CMD_INC_SSPI_CLOCK: 642 case MBOX_CMD_DEC_SSPI_CLOCK: 643 if (tegra->soc->scale_ss_clock) { 644 err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000); 645 if (err < 0) 646 rsp.cmd = MBOX_CMD_NAK; 647 else 648 rsp.cmd = MBOX_CMD_ACK; 649 650 rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000; 651 } else { 652 rsp.cmd = MBOX_CMD_ACK; 653 rsp.data = msg->data; 654 } 655 656 break; 657 658 case MBOX_CMD_SET_BW: 659 /* 660 * TODO: Request bandwidth once EMC scaling is supported. 661 * Ignore for now since ACK/NAK is not required for SET_BW 662 * messages. 663 */ 664 break; 665 666 case MBOX_CMD_SAVE_DFE_CTLE_CTX: 667 err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data); 668 if (err < 0) { 669 dev_err(dev, "failed to save context for USB3#%u: %d\n", 670 msg->data, err); 671 rsp.cmd = MBOX_CMD_NAK; 672 } else { 673 rsp.cmd = MBOX_CMD_ACK; 674 } 675 676 rsp.data = msg->data; 677 break; 678 679 case MBOX_CMD_START_HSIC_IDLE: 680 case MBOX_CMD_STOP_HSIC_IDLE: 681 if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE) 682 idle = false; 683 else 684 idle = true; 685 686 mask = extract_field(msg->data, 1 + soc->ports.hsic.offset, 687 soc->ports.hsic.count); 688 689 for_each_set_bit(port, &mask, 32) { 690 err = tegra_xusb_padctl_hsic_set_idle(padctl, port, 691 idle); 692 if (err < 0) 693 break; 694 } 695 696 if (err < 0) { 697 dev_err(dev, "failed to set HSIC#%u %s: %d\n", port, 698 idle ? "idle" : "busy", err); 699 rsp.cmd = MBOX_CMD_NAK; 700 } else { 701 rsp.cmd = MBOX_CMD_ACK; 702 } 703 704 rsp.data = msg->data; 705 break; 706 707 case MBOX_CMD_DISABLE_SS_LFPS_DETECTION: 708 case MBOX_CMD_ENABLE_SS_LFPS_DETECTION: 709 if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION) 710 enable = false; 711 else 712 enable = true; 713 714 mask = extract_field(msg->data, 1 + soc->ports.usb3.offset, 715 soc->ports.usb3.count); 716 717 for_each_set_bit(port, &mask, soc->ports.usb3.count) { 718 err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl, 719 port, 720 enable); 721 if (err < 0) 722 break; 723 724 /* 725 * wait 500us for LFPS detector to be disabled before 726 * sending ACK 727 */ 728 if (!enable) 729 usleep_range(500, 1000); 730 } 731 732 if (err < 0) { 733 dev_err(dev, 734 "failed to %s LFPS detection on USB3#%u: %d\n", 735 str_enable_disable(enable), port, err); 736 rsp.cmd = MBOX_CMD_NAK; 737 } else { 738 rsp.cmd = MBOX_CMD_ACK; 739 } 740 741 rsp.data = msg->data; 742 break; 743 744 default: 745 dev_warn(dev, "unknown message: %#x\n", msg->cmd); 746 break; 747 } 748 749 if (rsp.cmd) { 750 const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK"; 751 752 err = tegra_xusb_mbox_send(tegra, &rsp); 753 if (err < 0) 754 dev_err(dev, "failed to send %s: %d\n", cmd, err); 755 } 756 } 757 758 static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data) 759 { 760 struct tegra_xusb *tegra = data; 761 const struct tegra_xusb_soc_ops *ops = tegra->soc->ops; 762 struct tegra_xusb_mbox_msg msg; 763 u32 value; 764 765 mutex_lock(&tegra->lock); 766 767 if (pm_runtime_suspended(tegra->dev) || tegra->suspended) 768 goto out; 769 770 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.data_out); 771 tegra_xusb_mbox_unpack(&msg, value); 772 773 value = ops->mbox_reg_readl(tegra, tegra->soc->mbox.cmd); 774 value &= ~MBOX_DEST_SMI; 775 ops->mbox_reg_writel(tegra, value, tegra->soc->mbox.cmd); 776 777 /* clear mailbox owner if no ACK/NAK is required */ 778 if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd)) 779 ops->mbox_reg_writel(tegra, MBOX_OWNER_NONE, tegra->soc->mbox.owner); 780 781 tegra_xusb_mbox_handle(tegra, &msg); 782 783 out: 784 mutex_unlock(&tegra->lock); 785 return IRQ_HANDLED; 786 } 787 788 static void tegra_xusb_config(struct tegra_xusb *tegra) 789 { 790 u32 regs = tegra->hcd->rsrc_start; 791 u32 value; 792 793 if (tegra->soc->has_ipfs) { 794 value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0); 795 value |= IPFS_EN_FPCI; 796 ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0); 797 798 usleep_range(10, 20); 799 } 800 801 /* Program BAR0 space */ 802 value = fpci_readl(tegra, XUSB_CFG_4); 803 value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT); 804 value |= regs & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT); 805 fpci_writel(tegra, value, XUSB_CFG_4); 806 807 /* Program BAR2 space */ 808 if (tegra->bar2) { 809 value = fpci_readl(tegra, XUSB_CFG_7); 810 value &= ~(XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT); 811 value |= tegra->bar2->start & 812 (XUSB_BASE2_ADDR_MASK << XUSB_BASE2_ADDR_SHIFT); 813 fpci_writel(tegra, value, XUSB_CFG_7); 814 } 815 816 usleep_range(100, 200); 817 818 /* Enable bus master */ 819 value = fpci_readl(tegra, XUSB_CFG_1); 820 value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN; 821 fpci_writel(tegra, value, XUSB_CFG_1); 822 823 if (tegra->soc->has_ipfs) { 824 /* Enable interrupt assertion */ 825 value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0); 826 value |= IPFS_IP_INT_MASK; 827 ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0); 828 829 /* Set hysteresis */ 830 ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0); 831 } 832 } 833 834 static int tegra_xusb_clk_enable(struct tegra_xusb *tegra) 835 { 836 int err; 837 838 err = clk_prepare_enable(tegra->pll_e); 839 if (err < 0) 840 return err; 841 842 err = clk_prepare_enable(tegra->host_clk); 843 if (err < 0) 844 goto disable_plle; 845 846 err = clk_prepare_enable(tegra->ss_clk); 847 if (err < 0) 848 goto disable_host; 849 850 err = clk_prepare_enable(tegra->falcon_clk); 851 if (err < 0) 852 goto disable_ss; 853 854 err = clk_prepare_enable(tegra->fs_src_clk); 855 if (err < 0) 856 goto disable_falc; 857 858 err = clk_prepare_enable(tegra->hs_src_clk); 859 if (err < 0) 860 goto disable_fs_src; 861 862 if (tegra->soc->scale_ss_clock) { 863 err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED); 864 if (err < 0) 865 goto disable_hs_src; 866 } 867 868 return 0; 869 870 disable_hs_src: 871 clk_disable_unprepare(tegra->hs_src_clk); 872 disable_fs_src: 873 clk_disable_unprepare(tegra->fs_src_clk); 874 disable_falc: 875 clk_disable_unprepare(tegra->falcon_clk); 876 disable_ss: 877 clk_disable_unprepare(tegra->ss_clk); 878 disable_host: 879 clk_disable_unprepare(tegra->host_clk); 880 disable_plle: 881 clk_disable_unprepare(tegra->pll_e); 882 return err; 883 } 884 885 static void tegra_xusb_clk_disable(struct tegra_xusb *tegra) 886 { 887 clk_disable_unprepare(tegra->pll_e); 888 clk_disable_unprepare(tegra->host_clk); 889 clk_disable_unprepare(tegra->ss_clk); 890 clk_disable_unprepare(tegra->falcon_clk); 891 clk_disable_unprepare(tegra->fs_src_clk); 892 clk_disable_unprepare(tegra->hs_src_clk); 893 } 894 895 static int tegra_xusb_phy_enable(struct tegra_xusb *tegra) 896 { 897 unsigned int i; 898 int err; 899 900 for (i = 0; i < tegra->num_phys; i++) { 901 err = phy_init(tegra->phys[i]); 902 if (err) 903 goto disable_phy; 904 905 err = phy_power_on(tegra->phys[i]); 906 if (err) { 907 phy_exit(tegra->phys[i]); 908 goto disable_phy; 909 } 910 } 911 912 return 0; 913 914 disable_phy: 915 while (i--) { 916 phy_power_off(tegra->phys[i]); 917 phy_exit(tegra->phys[i]); 918 } 919 920 return err; 921 } 922 923 static void tegra_xusb_phy_disable(struct tegra_xusb *tegra) 924 { 925 unsigned int i; 926 927 for (i = 0; i < tegra->num_phys; i++) { 928 phy_power_off(tegra->phys[i]); 929 phy_exit(tegra->phys[i]); 930 } 931 } 932 933 #ifdef CONFIG_PM_SLEEP 934 static int tegra_xusb_init_context(struct tegra_xusb *tegra) 935 { 936 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 937 938 tegra->context.ipfs = devm_kcalloc(tegra->dev, soc->ipfs.num_offsets, 939 sizeof(u32), GFP_KERNEL); 940 if (!tegra->context.ipfs) 941 return -ENOMEM; 942 943 tegra->context.fpci = devm_kcalloc(tegra->dev, soc->fpci.num_offsets, 944 sizeof(u32), GFP_KERNEL); 945 if (!tegra->context.fpci) 946 return -ENOMEM; 947 948 return 0; 949 } 950 #else 951 static inline int tegra_xusb_init_context(struct tegra_xusb *tegra) 952 { 953 return 0; 954 } 955 #endif 956 957 static int tegra_xusb_request_firmware(struct tegra_xusb *tegra) 958 { 959 struct tegra_xusb_fw_header *header; 960 const struct firmware *fw; 961 int err; 962 963 err = request_firmware(&fw, tegra->soc->firmware, tegra->dev); 964 if (err < 0) { 965 dev_err(tegra->dev, "failed to request firmware: %d\n", err); 966 return err; 967 } 968 969 /* Load Falcon controller with its firmware. */ 970 header = (struct tegra_xusb_fw_header *)fw->data; 971 tegra->fw.size = le32_to_cpu(header->fwimg_len); 972 973 tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size, 974 &tegra->fw.phys, GFP_KERNEL); 975 if (!tegra->fw.virt) { 976 dev_err(tegra->dev, "failed to allocate memory for firmware\n"); 977 release_firmware(fw); 978 return -ENOMEM; 979 } 980 981 header = (struct tegra_xusb_fw_header *)tegra->fw.virt; 982 memcpy(tegra->fw.virt, fw->data, tegra->fw.size); 983 release_firmware(fw); 984 985 return 0; 986 } 987 988 static int tegra_xusb_wait_for_falcon(struct tegra_xusb *tegra) 989 { 990 struct xhci_cap_regs __iomem *cap_regs; 991 struct xhci_op_regs __iomem *op_regs; 992 int ret; 993 u32 value; 994 995 cap_regs = tegra->regs; 996 op_regs = tegra->regs + HC_LENGTH(readl(&cap_regs->hc_capbase)); 997 998 ret = readl_poll_timeout(&op_regs->status, value, !(value & STS_CNR), 1000, 200000); 999 1000 if (ret) 1001 dev_err(tegra->dev, "XHCI Controller not ready. Falcon state: 0x%x\n", 1002 csb_readl(tegra, XUSB_FALC_CPUCTL)); 1003 1004 return ret; 1005 } 1006 1007 static int tegra_xusb_load_firmware_rom(struct tegra_xusb *tegra) 1008 { 1009 unsigned int code_tag_blocks, code_size_blocks, code_blocks; 1010 struct tegra_xusb_fw_header *header; 1011 struct device *dev = tegra->dev; 1012 time64_t timestamp; 1013 u64 address; 1014 u32 value; 1015 int err; 1016 1017 header = (struct tegra_xusb_fw_header *)tegra->fw.virt; 1018 1019 if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) { 1020 dev_info(dev, "Firmware already loaded, Falcon state %#x\n", 1021 csb_readl(tegra, XUSB_FALC_CPUCTL)); 1022 return 0; 1023 } 1024 1025 /* Program the size of DFI into ILOAD_ATTR. */ 1026 csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR); 1027 1028 /* 1029 * Boot code of the firmware reads the ILOAD_BASE registers 1030 * to get to the start of the DFI in system memory. 1031 */ 1032 address = tegra->fw.phys + sizeof(*header); 1033 csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI); 1034 csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO); 1035 1036 /* Set BOOTPATH to 1 in APMAP. */ 1037 csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP); 1038 1039 /* Invalidate L2IMEM. */ 1040 csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG); 1041 1042 /* 1043 * Initiate fetch of bootcode from system memory into L2IMEM. 1044 * Program bootcode location and size in system memory. 1045 */ 1046 code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag), 1047 IMEM_BLOCK_SIZE); 1048 code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize), 1049 IMEM_BLOCK_SIZE); 1050 code_blocks = code_tag_blocks + code_size_blocks; 1051 1052 value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) << 1053 L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) | 1054 ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) << 1055 L2IMEMOP_SIZE_SRC_COUNT_SHIFT); 1056 csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE); 1057 1058 /* Trigger L2IMEM load operation. */ 1059 csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT, 1060 XUSB_CSB_MP_L2IMEMOP_TRIG); 1061 1062 /* Setup Falcon auto-fill. */ 1063 csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL); 1064 1065 value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) << 1066 IMFILLRNG1_TAG_LO_SHIFT) | 1067 ((code_blocks & IMFILLRNG1_TAG_MASK) << 1068 IMFILLRNG1_TAG_HI_SHIFT); 1069 csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1); 1070 1071 csb_writel(tegra, 0, XUSB_FALC_DMACTL); 1072 1073 /* wait for RESULT_VLD to get set */ 1074 #define tegra_csb_readl(offset) csb_readl(tegra, offset) 1075 err = readx_poll_timeout(tegra_csb_readl, 1076 XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT, value, 1077 value & L2IMEMOP_RESULT_VLD, 100, 10000); 1078 if (err < 0) { 1079 dev_err(dev, "DMA controller not ready %#010x\n", value); 1080 return err; 1081 } 1082 #undef tegra_csb_readl 1083 1084 csb_writel(tegra, le32_to_cpu(header->boot_codetag), 1085 XUSB_FALC_BOOTVEC); 1086 1087 /* Boot Falcon CPU and wait for USBSTS_CNR to get cleared. */ 1088 csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL); 1089 1090 if (tegra_xusb_wait_for_falcon(tegra)) 1091 return -EIO; 1092 1093 timestamp = le32_to_cpu(header->fwimg_created_time); 1094 1095 dev_info(dev, "Firmware timestamp: %ptTs UTC\n", ×tamp); 1096 1097 return 0; 1098 } 1099 1100 static u32 tegra_xusb_read_firmware_header(struct tegra_xusb *tegra, u32 offset) 1101 { 1102 /* 1103 * We only accept reading the firmware config table 1104 * The offset should not exceed the fw header structure 1105 */ 1106 if (offset >= sizeof(struct tegra_xusb_fw_header)) 1107 return 0; 1108 1109 bar2_writel(tegra, (FW_IOCTL_CFGTBL_READ << FW_IOCTL_TYPE_SHIFT) | offset, 1110 XUSB_BAR2_ARU_FW_SCRATCH); 1111 return bar2_readl(tegra, XUSB_BAR2_ARU_SMI_ARU_FW_SCRATCH_DATA0); 1112 } 1113 1114 static int tegra_xusb_init_ifr_firmware(struct tegra_xusb *tegra) 1115 { 1116 time64_t timestamp; 1117 1118 if (tegra_xusb_wait_for_falcon(tegra)) 1119 return -EIO; 1120 1121 #define offsetof_32(X, Y) ((u8)(offsetof(X, Y) / sizeof(__le32))) 1122 timestamp = tegra_xusb_read_firmware_header(tegra, offsetof_32(struct tegra_xusb_fw_header, 1123 fwimg_created_time) << 2); 1124 1125 dev_info(tegra->dev, "Firmware timestamp: %ptTs UTC\n", ×tamp); 1126 1127 return 0; 1128 } 1129 1130 static int tegra_xusb_load_firmware(struct tegra_xusb *tegra) 1131 { 1132 if (!tegra->soc->firmware) 1133 return tegra_xusb_init_ifr_firmware(tegra); 1134 else 1135 return tegra_xusb_load_firmware_rom(tegra); 1136 } 1137 1138 static void tegra_xusb_powerdomain_remove(struct device *dev, 1139 struct tegra_xusb *tegra) 1140 { 1141 if (!tegra->use_genpd) 1142 return; 1143 1144 if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss)) 1145 dev_pm_domain_detach(tegra->genpd_dev_ss, true); 1146 if (!IS_ERR_OR_NULL(tegra->genpd_dev_host)) 1147 dev_pm_domain_detach(tegra->genpd_dev_host, true); 1148 } 1149 1150 static int tegra_xusb_powerdomain_init(struct device *dev, 1151 struct tegra_xusb *tegra) 1152 { 1153 int err; 1154 1155 tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host"); 1156 if (IS_ERR(tegra->genpd_dev_host)) { 1157 err = PTR_ERR(tegra->genpd_dev_host); 1158 dev_err(dev, "failed to get host pm-domain: %d\n", err); 1159 return err; 1160 } 1161 1162 tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss"); 1163 if (IS_ERR(tegra->genpd_dev_ss)) { 1164 err = PTR_ERR(tegra->genpd_dev_ss); 1165 dev_err(dev, "failed to get superspeed pm-domain: %d\n", err); 1166 return err; 1167 } 1168 1169 tegra->use_genpd = true; 1170 1171 return 0; 1172 } 1173 1174 static int tegra_xusb_unpowergate_partitions(struct tegra_xusb *tegra) 1175 { 1176 struct device *dev = tegra->dev; 1177 int rc; 1178 1179 if (tegra->use_genpd) { 1180 rc = pm_runtime_resume_and_get(tegra->genpd_dev_ss); 1181 if (rc < 0) { 1182 dev_err(dev, "failed to enable XUSB SS partition\n"); 1183 return rc; 1184 } 1185 1186 rc = pm_runtime_resume_and_get(tegra->genpd_dev_host); 1187 if (rc < 0) { 1188 dev_err(dev, "failed to enable XUSB Host partition\n"); 1189 pm_runtime_put_sync(tegra->genpd_dev_ss); 1190 return rc; 1191 } 1192 } else { 1193 rc = tegra_pmc_powergate_sequence_power_up(tegra->pmc, 1194 TEGRA_POWERGATE_XUSBA, 1195 tegra->ss_clk, 1196 tegra->ss_rst); 1197 if (rc < 0) { 1198 dev_err(dev, "failed to enable XUSB SS partition\n"); 1199 return rc; 1200 } 1201 1202 rc = tegra_pmc_powergate_sequence_power_up(tegra->pmc, 1203 TEGRA_POWERGATE_XUSBC, 1204 tegra->host_clk, 1205 tegra->host_rst); 1206 if (rc < 0) { 1207 dev_err(dev, "failed to enable XUSB Host partition\n"); 1208 tegra_pmc_powergate_power_off(tegra->pmc, 1209 TEGRA_POWERGATE_XUSBA); 1210 return rc; 1211 } 1212 } 1213 1214 return 0; 1215 } 1216 1217 static int tegra_xusb_powergate_partitions(struct tegra_xusb *tegra) 1218 { 1219 struct device *dev = tegra->dev; 1220 int rc; 1221 1222 if (tegra->use_genpd) { 1223 rc = pm_runtime_put_sync(tegra->genpd_dev_host); 1224 if (rc < 0) { 1225 dev_err(dev, "failed to disable XUSB Host partition\n"); 1226 return rc; 1227 } 1228 1229 rc = pm_runtime_put_sync(tegra->genpd_dev_ss); 1230 if (rc < 0) { 1231 dev_err(dev, "failed to disable XUSB SS partition\n"); 1232 pm_runtime_get_sync(tegra->genpd_dev_host); 1233 return rc; 1234 } 1235 } else { 1236 rc = tegra_pmc_powergate_power_off(tegra->pmc, 1237 TEGRA_POWERGATE_XUSBC); 1238 if (rc < 0) { 1239 dev_err(dev, "failed to disable XUSB Host partition\n"); 1240 return rc; 1241 } 1242 1243 rc = tegra_pmc_powergate_power_off(tegra->pmc, 1244 TEGRA_POWERGATE_XUSBA); 1245 if (rc < 0) { 1246 dev_err(dev, "failed to disable XUSB SS partition\n"); 1247 tegra_pmc_powergate_sequence_power_up(tegra->pmc, 1248 TEGRA_POWERGATE_XUSBC, 1249 tegra->host_clk, 1250 tegra->host_rst); 1251 return rc; 1252 } 1253 } 1254 1255 return 0; 1256 } 1257 1258 static int __tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra) 1259 { 1260 struct tegra_xusb_mbox_msg msg; 1261 int err; 1262 1263 /* Enable firmware messages from controller. */ 1264 msg.cmd = MBOX_CMD_MSG_ENABLED; 1265 msg.data = 0; 1266 1267 err = tegra_xusb_mbox_send(tegra, &msg); 1268 if (err < 0) 1269 dev_err(tegra->dev, "failed to enable messages: %d\n", err); 1270 1271 return err; 1272 } 1273 1274 static irqreturn_t tegra_xusb_padctl_irq(int irq, void *data) 1275 { 1276 struct tegra_xusb *tegra = data; 1277 1278 mutex_lock(&tegra->lock); 1279 1280 if (tegra->suspended) { 1281 mutex_unlock(&tegra->lock); 1282 return IRQ_HANDLED; 1283 } 1284 1285 mutex_unlock(&tegra->lock); 1286 1287 pm_runtime_resume(tegra->dev); 1288 1289 return IRQ_HANDLED; 1290 } 1291 1292 static int tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra) 1293 { 1294 int err; 1295 1296 mutex_lock(&tegra->lock); 1297 err = __tegra_xusb_enable_firmware_messages(tegra); 1298 mutex_unlock(&tegra->lock); 1299 1300 return err; 1301 } 1302 1303 static void tegra_xhci_set_port_power(struct tegra_xusb *tegra, bool main, 1304 bool set) 1305 { 1306 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1307 struct usb_hcd *hcd = main ? xhci->main_hcd : xhci->shared_hcd; 1308 unsigned int wait = (!main && !set) ? 1000 : 10; 1309 u16 typeReq = set ? SetPortFeature : ClearPortFeature; 1310 u16 wIndex = main ? tegra->otg_usb2_port + 1 : tegra->otg_usb3_port + 1; 1311 u32 status; 1312 u32 stat_power = main ? USB_PORT_STAT_POWER : USB_SS_PORT_STAT_POWER; 1313 u32 status_val = set ? stat_power : 0; 1314 1315 dev_dbg(tegra->dev, "%s():%s %s port power\n", __func__, 1316 set ? "set" : "clear", main ? "HS" : "SS"); 1317 1318 hcd->driver->hub_control(hcd, typeReq, USB_PORT_FEAT_POWER, wIndex, 1319 NULL, 0); 1320 1321 do { 1322 tegra_xhci_hc_driver.hub_control(hcd, GetPortStatus, 0, wIndex, 1323 (char *) &status, sizeof(status)); 1324 if (status_val == (status & stat_power)) 1325 break; 1326 1327 if (!main && !set) 1328 usleep_range(600, 700); 1329 else 1330 usleep_range(10, 20); 1331 } while (--wait > 0); 1332 1333 if (status_val != (status & stat_power)) 1334 dev_info(tegra->dev, "failed to %s %s PP %d\n", 1335 set ? "set" : "clear", 1336 main ? "HS" : "SS", status); 1337 } 1338 1339 static struct phy *tegra_xusb_get_phy(struct tegra_xusb *tegra, char *name, 1340 int port) 1341 { 1342 unsigned int i, phy_count = 0; 1343 1344 for (i = 0; i < tegra->soc->num_types; i++) { 1345 if (!strncmp(tegra->soc->phy_types[i].name, name, 1346 strlen(name))) 1347 return tegra->phys[phy_count+port]; 1348 1349 phy_count += tegra->soc->phy_types[i].num; 1350 } 1351 1352 return NULL; 1353 } 1354 1355 static void tegra_xhci_id_work(struct work_struct *work) 1356 { 1357 struct tegra_xusb *tegra = container_of(work, struct tegra_xusb, 1358 id_work); 1359 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1360 struct tegra_xusb_mbox_msg msg; 1361 struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", 1362 tegra->otg_usb2_port); 1363 bool host_mode = tegra->host_mode; 1364 u32 status; 1365 int ret; 1366 1367 dev_dbg(tegra->dev, "host mode %s\n", str_on_off(host_mode)); 1368 1369 if (host_mode) 1370 phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_HOST); 1371 else 1372 phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_NONE); 1373 1374 tegra->otg_usb3_port = tegra_xusb_padctl_get_usb3_companion(tegra->padctl, 1375 tegra->otg_usb2_port); 1376 1377 pm_runtime_get_sync(tegra->dev); 1378 if (tegra->soc->otg_set_port_power) { 1379 if (host_mode) { 1380 /* switch to host mode */ 1381 if (tegra->otg_usb3_port >= 0) { 1382 if (tegra->soc->otg_reset_sspi) { 1383 /* set PP=0 */ 1384 tegra_xhci_hc_driver.hub_control( 1385 xhci->shared_hcd, GetPortStatus, 1386 0, tegra->otg_usb3_port+1, 1387 (char *) &status, sizeof(status)); 1388 if (status & USB_SS_PORT_STAT_POWER) 1389 tegra_xhci_set_port_power(tegra, false, 1390 false); 1391 1392 /* reset OTG port SSPI */ 1393 msg.cmd = MBOX_CMD_RESET_SSPI; 1394 msg.data = tegra->otg_usb3_port+1; 1395 1396 ret = tegra_xusb_mbox_send(tegra, &msg); 1397 if (ret < 0) { 1398 dev_info(tegra->dev, 1399 "failed to RESET_SSPI %d\n", 1400 ret); 1401 } 1402 } 1403 1404 tegra_xhci_set_port_power(tegra, false, true); 1405 } 1406 1407 tegra_xhci_set_port_power(tegra, true, true); 1408 1409 } else { 1410 if (tegra->otg_usb3_port >= 0) 1411 tegra_xhci_set_port_power(tegra, false, false); 1412 1413 tegra_xhci_set_port_power(tegra, true, false); 1414 } 1415 } 1416 pm_runtime_put_autosuspend(tegra->dev); 1417 } 1418 1419 #if IS_ENABLED(CONFIG_PM) || IS_ENABLED(CONFIG_PM_SLEEP) 1420 static bool is_usb2_otg_phy(struct tegra_xusb *tegra, unsigned int index) 1421 { 1422 return (tegra->usbphy[index] != NULL); 1423 } 1424 1425 static bool is_usb3_otg_phy(struct tegra_xusb *tegra, unsigned int index) 1426 { 1427 struct tegra_xusb_padctl *padctl = tegra->padctl; 1428 unsigned int i; 1429 int port; 1430 1431 for (i = 0; i < tegra->num_usb_phys; i++) { 1432 if (is_usb2_otg_phy(tegra, i)) { 1433 port = tegra_xusb_padctl_get_usb3_companion(padctl, i); 1434 if ((port >= 0) && (index == (unsigned int)port)) 1435 return true; 1436 } 1437 } 1438 1439 return false; 1440 } 1441 1442 static bool is_host_mode_phy(struct tegra_xusb *tegra, unsigned int phy_type, unsigned int index) 1443 { 1444 if (strcmp(tegra->soc->phy_types[phy_type].name, "hsic") == 0) 1445 return true; 1446 1447 if (strcmp(tegra->soc->phy_types[phy_type].name, "usb2") == 0) { 1448 if (is_usb2_otg_phy(tegra, index)) 1449 return ((index == tegra->otg_usb2_port) && tegra->host_mode); 1450 else 1451 return true; 1452 } 1453 1454 if (strcmp(tegra->soc->phy_types[phy_type].name, "usb3") == 0) { 1455 if (is_usb3_otg_phy(tegra, index)) 1456 return ((index == tegra->otg_usb3_port) && tegra->host_mode); 1457 else 1458 return true; 1459 } 1460 1461 return false; 1462 } 1463 #endif 1464 1465 static int tegra_xusb_get_usb2_port(struct tegra_xusb *tegra, 1466 struct usb_phy *usbphy) 1467 { 1468 unsigned int i; 1469 1470 for (i = 0; i < tegra->num_usb_phys; i++) { 1471 if (tegra->usbphy[i] && usbphy == tegra->usbphy[i]) 1472 return i; 1473 } 1474 1475 return -1; 1476 } 1477 1478 static int tegra_xhci_id_notify(struct notifier_block *nb, 1479 unsigned long action, void *data) 1480 { 1481 struct tegra_xusb *tegra = container_of(nb, struct tegra_xusb, 1482 id_nb); 1483 struct usb_phy *usbphy = (struct usb_phy *)data; 1484 1485 dev_dbg(tegra->dev, "%s(): action is %d", __func__, usbphy->last_event); 1486 1487 if ((tegra->host_mode && usbphy->last_event == USB_EVENT_ID) || 1488 (!tegra->host_mode && usbphy->last_event != USB_EVENT_ID)) { 1489 dev_dbg(tegra->dev, "Same role(%d) received. Ignore", 1490 tegra->host_mode); 1491 return NOTIFY_OK; 1492 } 1493 1494 tegra->otg_usb2_port = tegra_xusb_get_usb2_port(tegra, usbphy); 1495 1496 tegra->host_mode = usbphy->last_event == USB_EVENT_ID; 1497 1498 schedule_work(&tegra->id_work); 1499 1500 return NOTIFY_OK; 1501 } 1502 1503 static int tegra_xusb_init_usb_phy(struct tegra_xusb *tegra) 1504 { 1505 unsigned int i; 1506 1507 tegra->usbphy = devm_kcalloc(tegra->dev, tegra->num_usb_phys, 1508 sizeof(*tegra->usbphy), GFP_KERNEL); 1509 if (!tegra->usbphy) 1510 return -ENOMEM; 1511 1512 INIT_WORK(&tegra->id_work, tegra_xhci_id_work); 1513 tegra->id_nb.notifier_call = tegra_xhci_id_notify; 1514 tegra->otg_usb2_port = -EINVAL; 1515 tegra->otg_usb3_port = -EINVAL; 1516 1517 for (i = 0; i < tegra->num_usb_phys; i++) { 1518 struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i); 1519 1520 if (!phy) 1521 continue; 1522 1523 tegra->usbphy[i] = devm_usb_get_phy_by_node(tegra->dev, 1524 phy->dev.of_node, 1525 &tegra->id_nb); 1526 if (!IS_ERR(tegra->usbphy[i])) { 1527 dev_dbg(tegra->dev, "usbphy-%d registered", i); 1528 otg_set_host(tegra->usbphy[i]->otg, &tegra->hcd->self); 1529 } else { 1530 /* 1531 * usb-phy is optional, continue if its not available. 1532 */ 1533 tegra->usbphy[i] = NULL; 1534 } 1535 } 1536 1537 return 0; 1538 } 1539 1540 static void tegra_xusb_deinit_usb_phy(struct tegra_xusb *tegra) 1541 { 1542 unsigned int i; 1543 1544 cancel_work_sync(&tegra->id_work); 1545 1546 for (i = 0; i < tegra->num_usb_phys; i++) 1547 if (tegra->usbphy[i]) 1548 otg_set_host(tegra->usbphy[i]->otg, NULL); 1549 } 1550 1551 static int tegra_xusb_setup_wakeup(struct platform_device *pdev, struct tegra_xusb *tegra) 1552 { 1553 unsigned int i; 1554 1555 if (tegra->soc->max_num_wakes == 0) 1556 return 0; 1557 1558 tegra->wake_irqs = devm_kcalloc(tegra->dev, 1559 tegra->soc->max_num_wakes, 1560 sizeof(*tegra->wake_irqs), GFP_KERNEL); 1561 if (!tegra->wake_irqs) 1562 return -ENOMEM; 1563 1564 /* 1565 * USB wake events are independent of each other, so it is not necessary for a platform 1566 * to utilize all wake-up events supported for a given device. The USB host can operate 1567 * even if wake-up events are not defined or fail to be configured. Therefore, we only 1568 * return critical errors, such as -ENOMEM. 1569 */ 1570 for (i = 0; i < tegra->soc->max_num_wakes; i++) { 1571 struct irq_data *data; 1572 1573 tegra->wake_irqs[i] = platform_get_irq_optional(pdev, i + WAKE_IRQ_START_INDEX); 1574 if (tegra->wake_irqs[i] < 0) 1575 break; 1576 1577 data = irq_get_irq_data(tegra->wake_irqs[i]); 1578 if (!data) { 1579 dev_warn(tegra->dev, "get wake event %d irq data fail\n", i); 1580 break; 1581 } 1582 1583 irq_set_irq_type(tegra->wake_irqs[i], irqd_get_trigger_type(data)); 1584 } 1585 1586 tegra->num_wakes = i; 1587 dev_dbg(tegra->dev, "setup %d wake events\n", tegra->num_wakes); 1588 1589 return 0; 1590 } 1591 1592 static int tegra_xusb_probe(struct platform_device *pdev) 1593 { 1594 struct tegra_xusb *tegra; 1595 struct device_node *np; 1596 struct resource *regs; 1597 struct xhci_hcd *xhci; 1598 unsigned int i, j, k; 1599 struct phy *phy; 1600 int err; 1601 1602 BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256); 1603 1604 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); 1605 if (!tegra) 1606 return -ENOMEM; 1607 1608 tegra->soc = of_device_get_match_data(&pdev->dev); 1609 mutex_init(&tegra->lock); 1610 tegra->dev = &pdev->dev; 1611 1612 err = tegra_xusb_init_context(tegra); 1613 if (err < 0) 1614 return err; 1615 1616 tegra->regs = devm_platform_get_and_ioremap_resource(pdev, 0, ®s); 1617 if (IS_ERR(tegra->regs)) 1618 return PTR_ERR(tegra->regs); 1619 1620 tegra->fpci_base = devm_platform_ioremap_resource(pdev, 1); 1621 if (IS_ERR(tegra->fpci_base)) 1622 return PTR_ERR(tegra->fpci_base); 1623 1624 if (tegra->soc->has_ipfs) { 1625 tegra->ipfs_base = devm_platform_ioremap_resource(pdev, 2); 1626 if (IS_ERR(tegra->ipfs_base)) 1627 return PTR_ERR(tegra->ipfs_base); 1628 } else if (tegra->soc->has_bar2) { 1629 tegra->bar2_base = devm_platform_get_and_ioremap_resource(pdev, 2, &tegra->bar2); 1630 if (IS_ERR(tegra->bar2_base)) 1631 return PTR_ERR(tegra->bar2_base); 1632 } 1633 1634 tegra->xhci_irq = platform_get_irq(pdev, 0); 1635 if (tegra->xhci_irq < 0) 1636 return tegra->xhci_irq; 1637 1638 tegra->mbox_irq = platform_get_irq(pdev, 1); 1639 if (tegra->mbox_irq < 0) 1640 return tegra->mbox_irq; 1641 1642 err = tegra_xusb_setup_wakeup(pdev, tegra); 1643 if (err) 1644 return err; 1645 1646 tegra->padctl = tegra_xusb_padctl_get(&pdev->dev); 1647 if (IS_ERR(tegra->padctl)) 1648 return PTR_ERR(tegra->padctl); 1649 1650 np = of_parse_phandle(pdev->dev.of_node, "nvidia,xusb-padctl", 0); 1651 if (!np) { 1652 err = -ENODEV; 1653 goto put_padctl; 1654 } 1655 1656 tegra->padctl_irq = of_irq_get(np, 0); 1657 if (tegra->padctl_irq == -EPROBE_DEFER) { 1658 err = tegra->padctl_irq; 1659 goto put_padctl; 1660 } else if (tegra->padctl_irq <= 0) { 1661 /* Older device-trees don't have padctrl interrupt */ 1662 tegra->padctl_irq = 0; 1663 dev_dbg(&pdev->dev, 1664 "%pOF is missing an interrupt, disabling PM support\n", np); 1665 } 1666 1667 tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host"); 1668 if (IS_ERR(tegra->host_clk)) { 1669 err = PTR_ERR(tegra->host_clk); 1670 dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err); 1671 goto put_padctl; 1672 } 1673 1674 tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src"); 1675 if (IS_ERR(tegra->falcon_clk)) { 1676 err = PTR_ERR(tegra->falcon_clk); 1677 dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err); 1678 goto put_padctl; 1679 } 1680 1681 tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss"); 1682 if (IS_ERR(tegra->ss_clk)) { 1683 err = PTR_ERR(tegra->ss_clk); 1684 dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err); 1685 goto put_padctl; 1686 } 1687 1688 tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src"); 1689 if (IS_ERR(tegra->ss_src_clk)) { 1690 err = PTR_ERR(tegra->ss_src_clk); 1691 dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err); 1692 goto put_padctl; 1693 } 1694 1695 tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src"); 1696 if (IS_ERR(tegra->hs_src_clk)) { 1697 err = PTR_ERR(tegra->hs_src_clk); 1698 dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err); 1699 goto put_padctl; 1700 } 1701 1702 tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src"); 1703 if (IS_ERR(tegra->fs_src_clk)) { 1704 err = PTR_ERR(tegra->fs_src_clk); 1705 dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err); 1706 goto put_padctl; 1707 } 1708 1709 tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m"); 1710 if (IS_ERR(tegra->pll_u_480m)) { 1711 err = PTR_ERR(tegra->pll_u_480m); 1712 dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err); 1713 goto put_padctl; 1714 } 1715 1716 tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m"); 1717 if (IS_ERR(tegra->clk_m)) { 1718 err = PTR_ERR(tegra->clk_m); 1719 dev_err(&pdev->dev, "failed to get clk_m: %d\n", err); 1720 goto put_padctl; 1721 } 1722 1723 tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e"); 1724 if (IS_ERR(tegra->pll_e)) { 1725 err = PTR_ERR(tegra->pll_e); 1726 dev_err(&pdev->dev, "failed to get pll_e: %d\n", err); 1727 goto put_padctl; 1728 } 1729 1730 if (!of_property_present(pdev->dev.of_node, "power-domains")) { 1731 tegra->host_rst = devm_reset_control_get(&pdev->dev, 1732 "xusb_host"); 1733 if (IS_ERR(tegra->host_rst)) { 1734 err = PTR_ERR(tegra->host_rst); 1735 dev_err(&pdev->dev, 1736 "failed to get xusb_host reset: %d\n", err); 1737 goto put_padctl; 1738 } 1739 1740 tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss"); 1741 if (IS_ERR(tegra->ss_rst)) { 1742 err = PTR_ERR(tegra->ss_rst); 1743 dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n", 1744 err); 1745 goto put_padctl; 1746 } 1747 1748 tegra->pmc = devm_tegra_pmc_get(&pdev->dev); 1749 if (IS_ERR(tegra->pmc)) { 1750 err = dev_err_probe(&pdev->dev, PTR_ERR(tegra->pmc), 1751 "failed to get PMC\n"); 1752 goto put_padctl; 1753 } 1754 } else { 1755 err = tegra_xusb_powerdomain_init(&pdev->dev, tegra); 1756 if (err) 1757 goto put_powerdomains; 1758 } 1759 1760 tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies, 1761 sizeof(*tegra->supplies), GFP_KERNEL); 1762 if (!tegra->supplies) { 1763 err = -ENOMEM; 1764 goto put_powerdomains; 1765 } 1766 1767 regulator_bulk_set_supply_names(tegra->supplies, 1768 tegra->soc->supply_names, 1769 tegra->soc->num_supplies); 1770 1771 err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies, 1772 tegra->supplies); 1773 if (err) { 1774 dev_err(&pdev->dev, "failed to get regulators: %d\n", err); 1775 goto put_powerdomains; 1776 } 1777 1778 for (i = 0; i < tegra->soc->num_types; i++) { 1779 if (!strncmp(tegra->soc->phy_types[i].name, "usb2", 4)) 1780 tegra->num_usb_phys = tegra->soc->phy_types[i].num; 1781 tegra->num_phys += tegra->soc->phy_types[i].num; 1782 } 1783 1784 tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys, 1785 sizeof(*tegra->phys), GFP_KERNEL); 1786 if (!tegra->phys) { 1787 err = -ENOMEM; 1788 goto put_powerdomains; 1789 } 1790 1791 for (i = 0, k = 0; i < tegra->soc->num_types; i++) { 1792 char prop[8]; 1793 1794 for (j = 0; j < tegra->soc->phy_types[i].num; j++) { 1795 snprintf(prop, sizeof(prop), "%s-%d", 1796 tegra->soc->phy_types[i].name, j); 1797 1798 phy = devm_phy_optional_get(&pdev->dev, prop); 1799 if (IS_ERR(phy)) { 1800 dev_err(&pdev->dev, 1801 "failed to get PHY %s: %ld\n", prop, 1802 PTR_ERR(phy)); 1803 err = PTR_ERR(phy); 1804 goto put_powerdomains; 1805 } 1806 1807 tegra->phys[k++] = phy; 1808 } 1809 } 1810 1811 tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev, 1812 dev_name(&pdev->dev)); 1813 if (!tegra->hcd) { 1814 err = -ENOMEM; 1815 goto put_powerdomains; 1816 } 1817 1818 tegra->hcd->skip_phy_initialization = 1; 1819 tegra->hcd->regs = tegra->regs; 1820 tegra->hcd->rsrc_start = regs->start; 1821 tegra->hcd->rsrc_len = resource_size(regs); 1822 1823 /* 1824 * This must happen after usb_create_hcd(), because usb_create_hcd() 1825 * will overwrite the drvdata of the device with the hcd it creates. 1826 */ 1827 platform_set_drvdata(pdev, tegra); 1828 1829 err = tegra_xusb_clk_enable(tegra); 1830 if (err) { 1831 dev_err(tegra->dev, "failed to enable clocks: %d\n", err); 1832 goto put_hcd; 1833 } 1834 1835 err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies); 1836 if (err) { 1837 dev_err(tegra->dev, "failed to enable regulators: %d\n", err); 1838 goto disable_clk; 1839 } 1840 1841 err = tegra_xusb_phy_enable(tegra); 1842 if (err < 0) { 1843 dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err); 1844 goto disable_regulator; 1845 } 1846 1847 /* 1848 * The XUSB Falcon microcontroller can only address 40 bits, so set 1849 * the DMA mask accordingly. 1850 */ 1851 err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40)); 1852 if (err < 0) { 1853 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err); 1854 goto disable_phy; 1855 } 1856 1857 if (tegra->soc->firmware) { 1858 err = tegra_xusb_request_firmware(tegra); 1859 if (err < 0) { 1860 dev_err(&pdev->dev, 1861 "failed to request firmware: %d\n", err); 1862 goto disable_phy; 1863 } 1864 } 1865 1866 err = tegra_xusb_unpowergate_partitions(tegra); 1867 if (err) 1868 goto free_firmware; 1869 1870 tegra_xusb_config(tegra); 1871 1872 err = tegra_xusb_load_firmware(tegra); 1873 if (err < 0) { 1874 dev_err(&pdev->dev, "failed to load firmware: %d\n", err); 1875 goto powergate; 1876 } 1877 1878 err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED); 1879 if (err < 0) { 1880 dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err); 1881 goto powergate; 1882 } 1883 1884 device_wakeup_enable(tegra->hcd->self.controller); 1885 1886 xhci = hcd_to_xhci(tegra->hcd); 1887 1888 xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver, 1889 &pdev->dev, 1890 dev_name(&pdev->dev), 1891 tegra->hcd); 1892 if (!xhci->shared_hcd) { 1893 dev_err(&pdev->dev, "failed to create shared HCD\n"); 1894 err = -ENOMEM; 1895 goto remove_usb2; 1896 } 1897 1898 if (HCC_MAX_PSA(xhci->hcc_params) >= 4) 1899 xhci->shared_hcd->can_do_streams = 1; 1900 1901 err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED); 1902 if (err < 0) { 1903 dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err); 1904 goto put_usb3; 1905 } 1906 1907 err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq, 1908 tegra_xusb_mbox_irq, 1909 tegra_xusb_mbox_thread, 0, 1910 dev_name(&pdev->dev), tegra); 1911 if (err < 0) { 1912 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err); 1913 goto remove_usb3; 1914 } 1915 1916 if (tegra->padctl_irq) { 1917 err = devm_request_threaded_irq(&pdev->dev, tegra->padctl_irq, 1918 NULL, tegra_xusb_padctl_irq, 1919 IRQF_ONESHOT, dev_name(&pdev->dev), 1920 tegra); 1921 if (err < 0) { 1922 dev_err(&pdev->dev, "failed to request padctl IRQ: %d\n", err); 1923 goto remove_usb3; 1924 } 1925 } 1926 1927 err = tegra_xusb_enable_firmware_messages(tegra); 1928 if (err < 0) { 1929 dev_err(&pdev->dev, "failed to enable messages: %d\n", err); 1930 goto remove_usb3; 1931 } 1932 1933 err = tegra_xusb_init_usb_phy(tegra); 1934 if (err < 0) { 1935 dev_err(&pdev->dev, "failed to init USB PHY: %d\n", err); 1936 goto remove_usb3; 1937 } 1938 1939 /* Enable wake for both USB 2.0 and USB 3.0 roothubs */ 1940 device_init_wakeup(&tegra->hcd->self.root_hub->dev, true); 1941 device_init_wakeup(&xhci->shared_hcd->self.root_hub->dev, true); 1942 1943 pm_runtime_use_autosuspend(tegra->dev); 1944 pm_runtime_set_autosuspend_delay(tegra->dev, 2000); 1945 pm_runtime_mark_last_busy(tegra->dev); 1946 pm_runtime_set_active(tegra->dev); 1947 1948 if (tegra->padctl_irq) { 1949 device_init_wakeup(tegra->dev, true); 1950 pm_runtime_enable(tegra->dev); 1951 } 1952 1953 return 0; 1954 1955 remove_usb3: 1956 usb_remove_hcd(xhci->shared_hcd); 1957 put_usb3: 1958 usb_put_hcd(xhci->shared_hcd); 1959 remove_usb2: 1960 usb_remove_hcd(tegra->hcd); 1961 powergate: 1962 tegra_xusb_powergate_partitions(tegra); 1963 free_firmware: 1964 dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt, 1965 tegra->fw.phys); 1966 disable_phy: 1967 tegra_xusb_phy_disable(tegra); 1968 disable_regulator: 1969 regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies); 1970 disable_clk: 1971 tegra_xusb_clk_disable(tegra); 1972 put_hcd: 1973 usb_put_hcd(tegra->hcd); 1974 put_powerdomains: 1975 tegra_xusb_powerdomain_remove(&pdev->dev, tegra); 1976 put_padctl: 1977 of_node_put(np); 1978 tegra_xusb_padctl_put(tegra->padctl); 1979 return err; 1980 } 1981 1982 static void tegra_xusb_disable(struct tegra_xusb *tegra) 1983 { 1984 tegra_xusb_powergate_partitions(tegra); 1985 tegra_xusb_powerdomain_remove(tegra->dev, tegra); 1986 tegra_xusb_phy_disable(tegra); 1987 tegra_xusb_clk_disable(tegra); 1988 regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies); 1989 } 1990 1991 static void tegra_xusb_remove(struct platform_device *pdev) 1992 { 1993 struct tegra_xusb *tegra = platform_get_drvdata(pdev); 1994 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1995 1996 tegra_xusb_deinit_usb_phy(tegra); 1997 1998 pm_runtime_get_sync(&pdev->dev); 1999 usb_remove_hcd(xhci->shared_hcd); 2000 usb_put_hcd(xhci->shared_hcd); 2001 xhci->shared_hcd = NULL; 2002 usb_remove_hcd(tegra->hcd); 2003 usb_put_hcd(tegra->hcd); 2004 2005 dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt, 2006 tegra->fw.phys); 2007 2008 if (tegra->padctl_irq) 2009 pm_runtime_disable(&pdev->dev); 2010 2011 pm_runtime_put(&pdev->dev); 2012 2013 tegra_xusb_disable(tegra); 2014 tegra_xusb_padctl_put(tegra->padctl); 2015 } 2016 2017 static void tegra_xusb_shutdown(struct platform_device *pdev) 2018 { 2019 struct tegra_xusb *tegra = platform_get_drvdata(pdev); 2020 2021 pm_runtime_get_sync(&pdev->dev); 2022 disable_irq(tegra->xhci_irq); 2023 xhci_shutdown(tegra->hcd); 2024 tegra_xusb_disable(tegra); 2025 } 2026 2027 static bool xhci_hub_ports_suspended(struct xhci_hub *hub) 2028 { 2029 struct device *dev = hub->hcd->self.controller; 2030 bool status = true; 2031 unsigned int i; 2032 u32 value; 2033 2034 for (i = 0; i < hub->num_ports; i++) { 2035 value = xhci_portsc_readl(hub->ports[i]); 2036 if ((value & PORT_PE) == 0) 2037 continue; 2038 2039 if ((value & PORT_PLS_MASK) != XDEV_U3) { 2040 dev_info(dev, "%u-%u isn't suspended: %#010x\n", 2041 hub->hcd->self.busnum, i + 1, value); 2042 status = false; 2043 } 2044 } 2045 2046 return status; 2047 } 2048 2049 static int tegra_xusb_check_ports(struct tegra_xusb *tegra) 2050 { 2051 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 2052 struct xhci_bus_state *bus_state = &xhci->usb2_rhub.bus_state; 2053 unsigned long flags; 2054 int err = 0; 2055 2056 if (bus_state->bus_suspended) { 2057 /* xusb_hub_suspend() has just directed one or more USB2 port(s) 2058 * to U3 state, it takes 3ms to enter U3. 2059 */ 2060 usleep_range(3000, 4000); 2061 } 2062 2063 spin_lock_irqsave(&xhci->lock, flags); 2064 2065 if (!xhci_hub_ports_suspended(&xhci->usb2_rhub) || 2066 !xhci_hub_ports_suspended(&xhci->usb3_rhub)) 2067 err = -EBUSY; 2068 2069 spin_unlock_irqrestore(&xhci->lock, flags); 2070 2071 return err; 2072 } 2073 2074 static void tegra_xusb_save_context(struct tegra_xusb *tegra) 2075 { 2076 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 2077 struct tegra_xusb_context *ctx = &tegra->context; 2078 unsigned int i; 2079 2080 if (soc->ipfs.num_offsets > 0) { 2081 for (i = 0; i < soc->ipfs.num_offsets; i++) 2082 ctx->ipfs[i] = ipfs_readl(tegra, soc->ipfs.offsets[i]); 2083 } 2084 2085 if (soc->fpci.num_offsets > 0) { 2086 for (i = 0; i < soc->fpci.num_offsets; i++) 2087 ctx->fpci[i] = fpci_readl(tegra, soc->fpci.offsets[i]); 2088 } 2089 } 2090 2091 static void tegra_xusb_restore_context(struct tegra_xusb *tegra) 2092 { 2093 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 2094 struct tegra_xusb_context *ctx = &tegra->context; 2095 unsigned int i; 2096 2097 if (soc->fpci.num_offsets > 0) { 2098 for (i = 0; i < soc->fpci.num_offsets; i++) 2099 fpci_writel(tegra, ctx->fpci[i], soc->fpci.offsets[i]); 2100 } 2101 2102 if (soc->ipfs.num_offsets > 0) { 2103 for (i = 0; i < soc->ipfs.num_offsets; i++) 2104 ipfs_writel(tegra, ctx->ipfs[i], soc->ipfs.offsets[i]); 2105 } 2106 } 2107 2108 static enum usb_device_speed tegra_xhci_portsc_to_speed(struct tegra_xusb *tegra, u32 portsc) 2109 { 2110 if (DEV_LOWSPEED(portsc)) 2111 return USB_SPEED_LOW; 2112 2113 if (DEV_HIGHSPEED(portsc)) 2114 return USB_SPEED_HIGH; 2115 2116 if (DEV_FULLSPEED(portsc)) 2117 return USB_SPEED_FULL; 2118 2119 if (DEV_SUPERSPEED_ANY(portsc)) 2120 return USB_SPEED_SUPER; 2121 2122 return USB_SPEED_UNKNOWN; 2123 } 2124 2125 static void tegra_xhci_enable_phy_sleepwalk_wake(struct tegra_xusb *tegra) 2126 { 2127 struct tegra_xusb_padctl *padctl = tegra->padctl; 2128 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 2129 enum usb_device_speed speed; 2130 struct phy *phy; 2131 unsigned int index, offset; 2132 unsigned int i, j, k; 2133 struct xhci_hub *rhub; 2134 u32 portsc; 2135 2136 for (i = 0, k = 0; i < tegra->soc->num_types; i++) { 2137 if (strcmp(tegra->soc->phy_types[i].name, "usb3") == 0) 2138 rhub = &xhci->usb3_rhub; 2139 else 2140 rhub = &xhci->usb2_rhub; 2141 2142 if (strcmp(tegra->soc->phy_types[i].name, "hsic") == 0) 2143 offset = tegra->soc->ports.usb2.count; 2144 else 2145 offset = 0; 2146 2147 for (j = 0; j < tegra->soc->phy_types[i].num; j++) { 2148 phy = tegra->phys[k++]; 2149 2150 if (!phy) 2151 continue; 2152 2153 index = j + offset; 2154 2155 if (index >= rhub->num_ports) 2156 continue; 2157 2158 if (!is_host_mode_phy(tegra, i, j)) 2159 continue; 2160 2161 portsc = xhci_portsc_readl(rhub->ports[index]); 2162 speed = tegra_xhci_portsc_to_speed(tegra, portsc); 2163 tegra_xusb_padctl_enable_phy_sleepwalk(padctl, phy, speed); 2164 tegra_xusb_padctl_enable_phy_wake(padctl, phy); 2165 } 2166 } 2167 } 2168 2169 static void tegra_xhci_disable_phy_wake(struct tegra_xusb *tegra) 2170 { 2171 struct tegra_xusb_padctl *padctl = tegra->padctl; 2172 unsigned int i; 2173 2174 for (i = 0; i < tegra->num_usb_phys; i++) { 2175 struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i); 2176 2177 if (!phy) 2178 continue; 2179 2180 if (tegra_xusb_padctl_remote_wake_detected(padctl, phy)) 2181 tegra_phy_xusb_utmi_pad_power_on(phy); 2182 } 2183 2184 for (i = 0; i < tegra->num_phys; i++) { 2185 if (!tegra->phys[i]) 2186 continue; 2187 2188 if (tegra_xusb_padctl_remote_wake_detected(padctl, tegra->phys[i])) 2189 dev_dbg(tegra->dev, "%pOF remote wake detected\n", 2190 tegra->phys[i]->dev.of_node); 2191 2192 tegra_xusb_padctl_disable_phy_wake(padctl, tegra->phys[i]); 2193 } 2194 } 2195 2196 static void tegra_xhci_disable_phy_sleepwalk(struct tegra_xusb *tegra) 2197 { 2198 struct tegra_xusb_padctl *padctl = tegra->padctl; 2199 unsigned int i; 2200 2201 for (i = 0; i < tegra->num_phys; i++) { 2202 if (!tegra->phys[i]) 2203 continue; 2204 2205 tegra_xusb_padctl_disable_phy_sleepwalk(padctl, tegra->phys[i]); 2206 } 2207 } 2208 2209 static void tegra_xhci_program_utmi_power_lp0_exit(struct tegra_xusb *tegra) 2210 { 2211 unsigned int i, index_to_usb2; 2212 struct phy *phy; 2213 2214 for (i = 0; i < tegra->soc->num_types; i++) { 2215 if (strcmp(tegra->soc->phy_types[i].name, "usb2") == 0) 2216 index_to_usb2 = i; 2217 } 2218 2219 for (i = 0; i < tegra->num_usb_phys; i++) { 2220 if (!is_host_mode_phy(tegra, index_to_usb2, i)) 2221 continue; 2222 2223 phy = tegra_xusb_get_phy(tegra, "usb2", i); 2224 if (tegra->lp0_utmi_pad_mask & BIT(i)) 2225 tegra_phy_xusb_utmi_pad_power_on(phy); 2226 else 2227 tegra_phy_xusb_utmi_pad_power_down(phy); 2228 } 2229 } 2230 2231 static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool is_auto_resume) 2232 { 2233 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 2234 struct device *dev = tegra->dev; 2235 bool wakeup = is_auto_resume ? true : device_may_wakeup(dev); 2236 unsigned int i; 2237 int err; 2238 u32 usbcmd; 2239 u32 portsc; 2240 2241 dev_dbg(dev, "entering ELPG\n"); 2242 2243 usbcmd = readl(&xhci->op_regs->command); 2244 usbcmd &= ~CMD_EIE; 2245 writel(usbcmd, &xhci->op_regs->command); 2246 2247 err = tegra_xusb_check_ports(tegra); 2248 if (err < 0) { 2249 dev_err(tegra->dev, "not all ports suspended: %d\n", err); 2250 goto out; 2251 } 2252 2253 for (i = 0; i < xhci->usb2_rhub.num_ports; i++) { 2254 if (!xhci->usb2_rhub.ports[i]) 2255 continue; 2256 portsc = xhci_portsc_readl(xhci->usb2_rhub.ports[i]); 2257 tegra->lp0_utmi_pad_mask &= ~BIT(i); 2258 if (((portsc & PORT_PLS_MASK) == XDEV_U3) || ((portsc & DEV_SPEED_MASK) == XDEV_FS)) 2259 tegra->lp0_utmi_pad_mask |= BIT(i); 2260 } 2261 2262 err = xhci_suspend(xhci, wakeup); 2263 if (err < 0) { 2264 dev_err(tegra->dev, "failed to suspend XHCI: %d\n", err); 2265 goto out; 2266 } 2267 2268 tegra_xusb_save_context(tegra); 2269 2270 if (wakeup) 2271 tegra_xhci_enable_phy_sleepwalk_wake(tegra); 2272 2273 tegra_xusb_powergate_partitions(tegra); 2274 2275 for (i = 0; i < tegra->num_phys; i++) { 2276 if (!tegra->phys[i]) 2277 continue; 2278 2279 phy_power_off(tegra->phys[i]); 2280 if (!wakeup) 2281 phy_exit(tegra->phys[i]); 2282 } 2283 2284 tegra_xusb_clk_disable(tegra); 2285 2286 out: 2287 if (!err) 2288 dev_dbg(tegra->dev, "entering ELPG done\n"); 2289 else { 2290 usbcmd = readl(&xhci->op_regs->command); 2291 usbcmd |= CMD_EIE; 2292 writel(usbcmd, &xhci->op_regs->command); 2293 2294 dev_dbg(tegra->dev, "entering ELPG failed\n"); 2295 pm_runtime_mark_last_busy(tegra->dev); 2296 } 2297 2298 return err; 2299 } 2300 2301 static int tegra_xusb_exit_elpg(struct tegra_xusb *tegra, bool is_auto_resume) 2302 { 2303 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 2304 struct device *dev = tegra->dev; 2305 bool wakeup = is_auto_resume ? true : device_may_wakeup(dev); 2306 unsigned int i; 2307 u32 usbcmd; 2308 int err; 2309 2310 dev_dbg(dev, "exiting ELPG\n"); 2311 pm_runtime_mark_last_busy(tegra->dev); 2312 2313 err = tegra_xusb_clk_enable(tegra); 2314 if (err < 0) { 2315 dev_err(tegra->dev, "failed to enable clocks: %d\n", err); 2316 goto out; 2317 } 2318 2319 err = tegra_xusb_unpowergate_partitions(tegra); 2320 if (err) 2321 goto disable_clks; 2322 2323 if (wakeup) 2324 tegra_xhci_disable_phy_wake(tegra); 2325 2326 for (i = 0; i < tegra->num_phys; i++) { 2327 if (!tegra->phys[i]) 2328 continue; 2329 2330 if (!wakeup) 2331 phy_init(tegra->phys[i]); 2332 2333 phy_power_on(tegra->phys[i]); 2334 } 2335 if (tegra->suspended) 2336 tegra_xhci_program_utmi_power_lp0_exit(tegra); 2337 2338 tegra_xusb_config(tegra); 2339 tegra_xusb_restore_context(tegra); 2340 2341 err = tegra_xusb_load_firmware(tegra); 2342 if (err < 0) { 2343 dev_err(tegra->dev, "failed to load firmware: %d\n", err); 2344 goto disable_phy; 2345 } 2346 2347 err = __tegra_xusb_enable_firmware_messages(tegra); 2348 if (err < 0) { 2349 dev_err(tegra->dev, "failed to enable messages: %d\n", err); 2350 goto disable_phy; 2351 } 2352 2353 if (wakeup) 2354 tegra_xhci_disable_phy_sleepwalk(tegra); 2355 2356 err = xhci_resume(xhci, false, is_auto_resume); 2357 if (err < 0) { 2358 dev_err(tegra->dev, "failed to resume XHCI: %d\n", err); 2359 goto disable_phy; 2360 } 2361 2362 usbcmd = readl(&xhci->op_regs->command); 2363 usbcmd |= CMD_EIE; 2364 writel(usbcmd, &xhci->op_regs->command); 2365 2366 goto out; 2367 2368 disable_phy: 2369 for (i = 0; i < tegra->num_phys; i++) { 2370 if (!tegra->phys[i]) 2371 continue; 2372 2373 phy_power_off(tegra->phys[i]); 2374 if (!wakeup) 2375 phy_exit(tegra->phys[i]); 2376 } 2377 tegra_xusb_powergate_partitions(tegra); 2378 disable_clks: 2379 tegra_xusb_clk_disable(tegra); 2380 out: 2381 if (!err) 2382 dev_dbg(dev, "exiting ELPG done\n"); 2383 else 2384 dev_dbg(dev, "exiting ELPG failed\n"); 2385 2386 return err; 2387 } 2388 2389 static __maybe_unused int tegra_xusb_suspend(struct device *dev) 2390 { 2391 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2392 int err; 2393 2394 synchronize_irq(tegra->mbox_irq); 2395 2396 mutex_lock(&tegra->lock); 2397 2398 if (pm_runtime_suspended(dev)) { 2399 err = tegra_xusb_exit_elpg(tegra, true); 2400 if (err < 0) 2401 goto out; 2402 } 2403 2404 err = tegra_xusb_enter_elpg(tegra, false); 2405 if (err < 0) { 2406 if (pm_runtime_suspended(dev)) { 2407 pm_runtime_disable(dev); 2408 pm_runtime_set_active(dev); 2409 pm_runtime_enable(dev); 2410 } 2411 2412 goto out; 2413 } 2414 2415 out: 2416 if (!err) { 2417 tegra->suspended = true; 2418 pm_runtime_disable(dev); 2419 2420 if (device_may_wakeup(dev)) { 2421 unsigned int i; 2422 2423 if (enable_irq_wake(tegra->padctl_irq)) 2424 dev_err(dev, "failed to enable padctl wakes\n"); 2425 2426 for (i = 0; i < tegra->num_wakes; i++) 2427 enable_irq_wake(tegra->wake_irqs[i]); 2428 } 2429 } 2430 2431 mutex_unlock(&tegra->lock); 2432 2433 return err; 2434 } 2435 2436 static __maybe_unused int tegra_xusb_resume(struct device *dev) 2437 { 2438 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2439 int err; 2440 2441 mutex_lock(&tegra->lock); 2442 2443 if (!tegra->suspended) { 2444 mutex_unlock(&tegra->lock); 2445 return 0; 2446 } 2447 2448 err = tegra_xusb_exit_elpg(tegra, false); 2449 if (err < 0) { 2450 mutex_unlock(&tegra->lock); 2451 return err; 2452 } 2453 2454 if (device_may_wakeup(dev)) { 2455 unsigned int i; 2456 2457 if (disable_irq_wake(tegra->padctl_irq)) 2458 dev_err(dev, "failed to disable padctl wakes\n"); 2459 2460 for (i = 0; i < tegra->num_wakes; i++) 2461 disable_irq_wake(tegra->wake_irqs[i]); 2462 } 2463 tegra->suspended = false; 2464 mutex_unlock(&tegra->lock); 2465 2466 pm_runtime_set_active(dev); 2467 pm_runtime_enable(dev); 2468 2469 return 0; 2470 } 2471 2472 static __maybe_unused int tegra_xusb_runtime_suspend(struct device *dev) 2473 { 2474 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2475 int ret; 2476 2477 synchronize_irq(tegra->mbox_irq); 2478 mutex_lock(&tegra->lock); 2479 ret = tegra_xusb_enter_elpg(tegra, true); 2480 mutex_unlock(&tegra->lock); 2481 2482 return ret; 2483 } 2484 2485 static __maybe_unused int tegra_xusb_runtime_resume(struct device *dev) 2486 { 2487 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2488 int err; 2489 2490 mutex_lock(&tegra->lock); 2491 err = tegra_xusb_exit_elpg(tegra, true); 2492 mutex_unlock(&tegra->lock); 2493 2494 return err; 2495 } 2496 2497 static const struct dev_pm_ops tegra_xusb_pm_ops = { 2498 SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend, 2499 tegra_xusb_runtime_resume, NULL) 2500 SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume) 2501 }; 2502 2503 static const char * const tegra124_supply_names[] = { 2504 "avddio-pex", 2505 "dvddio-pex", 2506 "avdd-usb", 2507 "hvdd-usb-ss", 2508 }; 2509 2510 static const struct tegra_xusb_phy_type tegra124_phy_types[] = { 2511 { .name = "usb3", .num = 2, }, 2512 { .name = "usb2", .num = 3, }, 2513 { .name = "hsic", .num = 2, }, 2514 }; 2515 2516 static const unsigned int tegra124_xusb_context_ipfs[] = { 2517 IPFS_XUSB_HOST_MSI_BAR_SZ_0, 2518 IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0, 2519 IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0, 2520 IPFS_XUSB_HOST_MSI_VEC0_0, 2521 IPFS_XUSB_HOST_MSI_EN_VEC0_0, 2522 IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0, 2523 IPFS_XUSB_HOST_INTR_MASK_0, 2524 IPFS_XUSB_HOST_INTR_ENABLE_0, 2525 IPFS_XUSB_HOST_UFPCI_CONFIG_0, 2526 IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0, 2527 IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0, 2528 }; 2529 2530 static const unsigned int tegra124_xusb_context_fpci[] = { 2531 XUSB_CFG_ARU_CONTEXT_HS_PLS, 2532 XUSB_CFG_ARU_CONTEXT_FS_PLS, 2533 XUSB_CFG_ARU_CONTEXT_HSFS_SPEED, 2534 XUSB_CFG_ARU_CONTEXT_HSFS_PP, 2535 XUSB_CFG_ARU_CONTEXT, 2536 XUSB_CFG_AXI_CFG, 2537 XUSB_CFG_24, 2538 XUSB_CFG_16, 2539 }; 2540 2541 static const struct tegra_xusb_context_soc tegra124_xusb_context = { 2542 .ipfs = { 2543 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_ipfs), 2544 .offsets = tegra124_xusb_context_ipfs, 2545 }, 2546 .fpci = { 2547 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci), 2548 .offsets = tegra124_xusb_context_fpci, 2549 }, 2550 }; 2551 2552 static const struct tegra_xusb_soc_ops tegra124_ops = { 2553 .mbox_reg_readl = &fpci_readl, 2554 .mbox_reg_writel = &fpci_writel, 2555 .csb_reg_readl = &fpci_csb_readl, 2556 .csb_reg_writel = &fpci_csb_writel, 2557 }; 2558 2559 static const struct tegra_xusb_soc tegra124_soc = { 2560 .firmware = "nvidia/tegra124/xusb.bin", 2561 .supply_names = tegra124_supply_names, 2562 .num_supplies = ARRAY_SIZE(tegra124_supply_names), 2563 .phy_types = tegra124_phy_types, 2564 .num_types = ARRAY_SIZE(tegra124_phy_types), 2565 .context = &tegra124_xusb_context, 2566 .ports = { 2567 .usb2 = { .offset = 4, .count = 4, }, 2568 .hsic = { .offset = 6, .count = 2, }, 2569 .usb3 = { .offset = 0, .count = 2, }, 2570 }, 2571 .scale_ss_clock = true, 2572 .has_ipfs = true, 2573 .otg_reset_sspi = false, 2574 .otg_set_port_power = true, 2575 .ops = &tegra124_ops, 2576 .mbox = { 2577 .cmd = 0xe4, 2578 .data_in = 0xe8, 2579 .data_out = 0xec, 2580 .owner = 0xf0, 2581 .smi_intr = XUSB_CFG_ARU_SMI_INTR, 2582 }, 2583 }; 2584 #if IS_ENABLED(CONFIG_ARCH_TEGRA_124_SOC) || IS_ENABLED(CONFIG_ARCH_TEGRA_132_SOC) 2585 MODULE_FIRMWARE("nvidia/tegra124/xusb.bin"); 2586 #endif 2587 2588 static const char * const tegra210_supply_names[] = { 2589 "dvddio-pex", 2590 "hvddio-pex", 2591 "avdd-usb", 2592 }; 2593 2594 static const struct tegra_xusb_phy_type tegra210_phy_types[] = { 2595 { .name = "usb3", .num = 4, }, 2596 { .name = "usb2", .num = 4, }, 2597 { .name = "hsic", .num = 1, }, 2598 }; 2599 2600 static const struct tegra_xusb_soc tegra210_soc = { 2601 .firmware = "nvidia/tegra210/xusb.bin", 2602 .supply_names = tegra210_supply_names, 2603 .num_supplies = ARRAY_SIZE(tegra210_supply_names), 2604 .phy_types = tegra210_phy_types, 2605 .num_types = ARRAY_SIZE(tegra210_phy_types), 2606 .context = &tegra124_xusb_context, 2607 .ports = { 2608 .usb2 = { .offset = 4, .count = 4, }, 2609 .hsic = { .offset = 8, .count = 1, }, 2610 .usb3 = { .offset = 0, .count = 4, }, 2611 }, 2612 .scale_ss_clock = false, 2613 .has_ipfs = true, 2614 .otg_reset_sspi = true, 2615 .otg_set_port_power = true, 2616 .ops = &tegra124_ops, 2617 .mbox = { 2618 .cmd = 0xe4, 2619 .data_in = 0xe8, 2620 .data_out = 0xec, 2621 .owner = 0xf0, 2622 .smi_intr = XUSB_CFG_ARU_SMI_INTR, 2623 }, 2624 }; 2625 #if IS_ENABLED(CONFIG_ARCH_TEGRA_210_SOC) 2626 MODULE_FIRMWARE("nvidia/tegra210/xusb.bin"); 2627 #endif 2628 2629 static const char * const tegra186_supply_names[] = { 2630 }; 2631 #if IS_ENABLED(CONFIG_ARCH_TEGRA_186_SOC) 2632 MODULE_FIRMWARE("nvidia/tegra186/xusb.bin"); 2633 #endif 2634 2635 static const struct tegra_xusb_phy_type tegra186_phy_types[] = { 2636 { .name = "usb3", .num = 3, }, 2637 { .name = "usb2", .num = 3, }, 2638 { .name = "hsic", .num = 1, }, 2639 }; 2640 2641 static const struct tegra_xusb_context_soc tegra186_xusb_context = { 2642 .fpci = { 2643 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci), 2644 .offsets = tegra124_xusb_context_fpci, 2645 }, 2646 }; 2647 2648 static const struct tegra_xusb_soc tegra186_soc = { 2649 .firmware = "nvidia/tegra186/xusb.bin", 2650 .supply_names = tegra186_supply_names, 2651 .num_supplies = ARRAY_SIZE(tegra186_supply_names), 2652 .phy_types = tegra186_phy_types, 2653 .num_types = ARRAY_SIZE(tegra186_phy_types), 2654 .context = &tegra186_xusb_context, 2655 .ports = { 2656 .usb3 = { .offset = 0, .count = 3, }, 2657 .usb2 = { .offset = 3, .count = 3, }, 2658 .hsic = { .offset = 6, .count = 1, }, 2659 }, 2660 .scale_ss_clock = false, 2661 .has_ipfs = false, 2662 .otg_reset_sspi = false, 2663 .otg_set_port_power = true, 2664 .ops = &tegra124_ops, 2665 .mbox = { 2666 .cmd = 0xe4, 2667 .data_in = 0xe8, 2668 .data_out = 0xec, 2669 .owner = 0xf0, 2670 .smi_intr = XUSB_CFG_ARU_SMI_INTR, 2671 }, 2672 .lpm_support = true, 2673 }; 2674 2675 static const char * const tegra194_supply_names[] = { 2676 }; 2677 2678 static const struct tegra_xusb_phy_type tegra194_phy_types[] = { 2679 { .name = "usb3", .num = 4, }, 2680 { .name = "usb2", .num = 4, }, 2681 }; 2682 2683 static const struct tegra_xusb_soc tegra194_soc = { 2684 .firmware = "nvidia/tegra194/xusb.bin", 2685 .supply_names = tegra194_supply_names, 2686 .num_supplies = ARRAY_SIZE(tegra194_supply_names), 2687 .phy_types = tegra194_phy_types, 2688 .num_types = ARRAY_SIZE(tegra194_phy_types), 2689 .context = &tegra186_xusb_context, 2690 .ports = { 2691 .usb3 = { .offset = 0, .count = 4, }, 2692 .usb2 = { .offset = 4, .count = 4, }, 2693 }, 2694 .scale_ss_clock = false, 2695 .has_ipfs = false, 2696 .otg_reset_sspi = false, 2697 .otg_set_port_power = false, 2698 .ops = &tegra124_ops, 2699 .mbox = { 2700 .cmd = 0x68, 2701 .data_in = 0x6c, 2702 .data_out = 0x70, 2703 .owner = 0x74, 2704 .smi_intr = XUSB_CFG_ARU_SMI_INTR, 2705 }, 2706 .lpm_support = true, 2707 }; 2708 #if IS_ENABLED(CONFIG_ARCH_TEGRA_194_SOC) 2709 MODULE_FIRMWARE("nvidia/tegra194/xusb.bin"); 2710 #endif 2711 2712 static const struct tegra_xusb_soc_ops tegra234_ops = { 2713 .mbox_reg_readl = &bar2_readl, 2714 .mbox_reg_writel = &bar2_writel, 2715 .csb_reg_readl = &bar2_csb_readl, 2716 .csb_reg_writel = &bar2_csb_writel, 2717 }; 2718 2719 static const struct tegra_xusb_soc tegra234_soc = { 2720 .supply_names = tegra194_supply_names, 2721 .num_supplies = ARRAY_SIZE(tegra194_supply_names), 2722 .phy_types = tegra194_phy_types, 2723 .num_types = ARRAY_SIZE(tegra194_phy_types), 2724 .max_num_wakes = 7, 2725 .context = &tegra186_xusb_context, 2726 .ports = { 2727 .usb3 = { .offset = 0, .count = 4, }, 2728 .usb2 = { .offset = 4, .count = 4, }, 2729 }, 2730 .scale_ss_clock = false, 2731 .has_ipfs = false, 2732 .otg_reset_sspi = false, 2733 .otg_set_port_power = false, 2734 .ops = &tegra234_ops, 2735 .mbox = { 2736 .cmd = XUSB_BAR2_ARU_MBOX_CMD, 2737 .data_in = XUSB_BAR2_ARU_MBOX_DATA_IN, 2738 .data_out = XUSB_BAR2_ARU_MBOX_DATA_OUT, 2739 .owner = XUSB_BAR2_ARU_MBOX_OWNER, 2740 .smi_intr = XUSB_BAR2_ARU_SMI_INTR, 2741 }, 2742 .lpm_support = true, 2743 .has_bar2 = true, 2744 }; 2745 2746 static const struct of_device_id tegra_xusb_of_match[] = { 2747 { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc }, 2748 { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc }, 2749 { .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc }, 2750 { .compatible = "nvidia,tegra194-xusb", .data = &tegra194_soc }, 2751 { .compatible = "nvidia,tegra234-xusb", .data = &tegra234_soc }, 2752 { }, 2753 }; 2754 MODULE_DEVICE_TABLE(of, tegra_xusb_of_match); 2755 2756 static struct platform_driver tegra_xusb_driver = { 2757 .probe = tegra_xusb_probe, 2758 .remove = tegra_xusb_remove, 2759 .shutdown = tegra_xusb_shutdown, 2760 .driver = { 2761 .name = "tegra-xusb", 2762 .pm = &tegra_xusb_pm_ops, 2763 .of_match_table = tegra_xusb_of_match, 2764 }, 2765 }; 2766 2767 static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci) 2768 { 2769 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2770 2771 if (tegra && tegra->soc->lpm_support) 2772 xhci->quirks |= XHCI_LPM_SUPPORT; 2773 } 2774 2775 static int tegra_xhci_setup(struct usb_hcd *hcd) 2776 { 2777 return xhci_gen_setup(hcd, tegra_xhci_quirks); 2778 } 2779 2780 static int tegra_xhci_hub_control(struct usb_hcd *hcd, u16 type_req, u16 value, u16 index, 2781 char *buf, u16 length) 2782 { 2783 struct tegra_xusb *tegra = dev_get_drvdata(hcd->self.controller); 2784 struct xhci_hcd *xhci = hcd_to_xhci(hcd); 2785 struct xhci_hub *rhub; 2786 struct xhci_bus_state *bus_state; 2787 int port = (index & 0xff) - 1; 2788 unsigned int i; 2789 struct xhci_port **ports; 2790 u32 portsc; 2791 int ret; 2792 struct phy *phy; 2793 2794 rhub = &xhci->usb2_rhub; 2795 bus_state = &rhub->bus_state; 2796 if (bus_state->resuming_ports && hcd->speed == HCD_USB2) { 2797 ports = rhub->ports; 2798 i = rhub->num_ports; 2799 while (i--) { 2800 if (!test_bit(i, &bus_state->resuming_ports)) 2801 continue; 2802 portsc = xhci_portsc_readl(ports[i]); 2803 if ((portsc & PORT_PLS_MASK) == XDEV_RESUME) 2804 tegra_phy_xusb_utmi_pad_power_on( 2805 tegra_xusb_get_phy(tegra, "usb2", (int) i)); 2806 } 2807 } 2808 2809 if (hcd->speed == HCD_USB2) { 2810 phy = tegra_xusb_get_phy(tegra, "usb2", port); 2811 if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_SUSPEND)) { 2812 if (!index || index > rhub->num_ports) 2813 return -EPIPE; 2814 tegra_phy_xusb_utmi_pad_power_on(phy); 2815 } 2816 if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_RESET)) { 2817 if (!index || index > rhub->num_ports) 2818 return -EPIPE; 2819 ports = rhub->ports; 2820 portsc = xhci_portsc_readl(ports[port]); 2821 if (portsc & PORT_CONNECT) 2822 tegra_phy_xusb_utmi_pad_power_on(phy); 2823 } 2824 } 2825 2826 ret = xhci_hub_control(hcd, type_req, value, index, buf, length); 2827 if (ret < 0) 2828 return ret; 2829 2830 if (hcd->speed == HCD_USB2) { 2831 /* Use phy where we set previously */ 2832 if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_SUSPEND)) 2833 /* We don't suspend the PAD while HNP role swap happens on the OTG port */ 2834 if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable)) 2835 tegra_phy_xusb_utmi_pad_power_down(phy); 2836 2837 if ((type_req == ClearPortFeature) && (value == USB_PORT_FEAT_C_CONNECTION)) { 2838 ports = rhub->ports; 2839 portsc = xhci_portsc_readl(ports[port]); 2840 if (!(portsc & PORT_CONNECT)) { 2841 /* We don't suspend the PAD while HNP role swap happens on the OTG 2842 * port 2843 */ 2844 if (!((hcd->self.otg_port == (port + 1)) && hcd->self.b_hnp_enable)) 2845 tegra_phy_xusb_utmi_pad_power_down(phy); 2846 } 2847 } 2848 if ((type_req == SetPortFeature) && (value == USB_PORT_FEAT_TEST)) 2849 tegra_phy_xusb_utmi_pad_power_on(phy); 2850 } 2851 2852 return ret; 2853 } 2854 2855 static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = { 2856 .reset = tegra_xhci_setup, 2857 .hub_control = tegra_xhci_hub_control, 2858 }; 2859 2860 static int __init tegra_xusb_init(void) 2861 { 2862 xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides); 2863 2864 return platform_driver_register(&tegra_xusb_driver); 2865 } 2866 module_init(tegra_xusb_init); 2867 2868 static void __exit tegra_xusb_exit(void) 2869 { 2870 platform_driver_unregister(&tegra_xusb_driver); 2871 } 2872 module_exit(tegra_xusb_exit); 2873 2874 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>"); 2875 MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver"); 2876 MODULE_LICENSE("GPL v2"); 2877