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