1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * NVIDIA Tegra xHCI host controller driver 4 * 5 * Copyright (C) 2014 NVIDIA Corporation 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_device.h> 18 #include <linux/phy/phy.h> 19 #include <linux/phy/tegra/xusb.h> 20 #include <linux/platform_device.h> 21 #include <linux/pm.h> 22 #include <linux/pm_domain.h> 23 #include <linux/pm_runtime.h> 24 #include <linux/regulator/consumer.h> 25 #include <linux/reset.h> 26 #include <linux/slab.h> 27 #include <linux/usb/otg.h> 28 #include <linux/usb/phy.h> 29 #include <linux/usb/role.h> 30 #include <soc/tegra/pmc.h> 31 32 #include "xhci.h" 33 34 #define TEGRA_XHCI_SS_HIGH_SPEED 120000000 35 #define TEGRA_XHCI_SS_LOW_SPEED 12000000 36 37 /* FPCI CFG registers */ 38 #define XUSB_CFG_1 0x004 39 #define XUSB_IO_SPACE_EN BIT(0) 40 #define XUSB_MEM_SPACE_EN BIT(1) 41 #define XUSB_BUS_MASTER_EN BIT(2) 42 #define XUSB_CFG_4 0x010 43 #define XUSB_BASE_ADDR_SHIFT 15 44 #define XUSB_BASE_ADDR_MASK 0x1ffff 45 #define XUSB_CFG_16 0x040 46 #define XUSB_CFG_24 0x060 47 #define XUSB_CFG_AXI_CFG 0x0f8 48 #define XUSB_CFG_ARU_C11_CSBRANGE 0x41c 49 #define XUSB_CFG_ARU_CONTEXT 0x43c 50 #define XUSB_CFG_ARU_CONTEXT_HS_PLS 0x478 51 #define XUSB_CFG_ARU_CONTEXT_FS_PLS 0x47c 52 #define XUSB_CFG_ARU_CONTEXT_HSFS_SPEED 0x480 53 #define XUSB_CFG_ARU_CONTEXT_HSFS_PP 0x484 54 #define XUSB_CFG_CSB_BASE_ADDR 0x800 55 56 /* FPCI mailbox registers */ 57 /* XUSB_CFG_ARU_MBOX_CMD */ 58 #define MBOX_DEST_FALC BIT(27) 59 #define MBOX_DEST_PME BIT(28) 60 #define MBOX_DEST_SMI BIT(29) 61 #define MBOX_DEST_XHCI BIT(30) 62 #define MBOX_INT_EN BIT(31) 63 /* XUSB_CFG_ARU_MBOX_DATA_IN and XUSB_CFG_ARU_MBOX_DATA_OUT */ 64 #define CMD_DATA_SHIFT 0 65 #define CMD_DATA_MASK 0xffffff 66 #define CMD_TYPE_SHIFT 24 67 #define CMD_TYPE_MASK 0xff 68 /* XUSB_CFG_ARU_MBOX_OWNER */ 69 #define MBOX_OWNER_NONE 0 70 #define MBOX_OWNER_FW 1 71 #define MBOX_OWNER_SW 2 72 #define XUSB_CFG_ARU_SMI_INTR 0x428 73 #define MBOX_SMI_INTR_FW_HANG BIT(1) 74 #define MBOX_SMI_INTR_EN BIT(3) 75 76 /* IPFS registers */ 77 #define IPFS_XUSB_HOST_MSI_BAR_SZ_0 0x0c0 78 #define IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0 0x0c4 79 #define IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0 0x0c8 80 #define IPFS_XUSB_HOST_MSI_VEC0_0 0x100 81 #define IPFS_XUSB_HOST_MSI_EN_VEC0_0 0x140 82 #define IPFS_XUSB_HOST_CONFIGURATION_0 0x180 83 #define IPFS_EN_FPCI BIT(0) 84 #define IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0 0x184 85 #define IPFS_XUSB_HOST_INTR_MASK_0 0x188 86 #define IPFS_IP_INT_MASK BIT(16) 87 #define IPFS_XUSB_HOST_INTR_ENABLE_0 0x198 88 #define IPFS_XUSB_HOST_UFPCI_CONFIG_0 0x19c 89 #define IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0 0x1bc 90 #define IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0 0x1dc 91 92 #define CSB_PAGE_SELECT_MASK 0x7fffff 93 #define CSB_PAGE_SELECT_SHIFT 9 94 #define CSB_PAGE_OFFSET_MASK 0x1ff 95 #define CSB_PAGE_SELECT(addr) ((addr) >> (CSB_PAGE_SELECT_SHIFT) & \ 96 CSB_PAGE_SELECT_MASK) 97 #define CSB_PAGE_OFFSET(addr) ((addr) & CSB_PAGE_OFFSET_MASK) 98 99 /* Falcon CSB registers */ 100 #define XUSB_FALC_CPUCTL 0x100 101 #define CPUCTL_STARTCPU BIT(1) 102 #define CPUCTL_STATE_HALTED BIT(4) 103 #define CPUCTL_STATE_STOPPED BIT(5) 104 #define XUSB_FALC_BOOTVEC 0x104 105 #define XUSB_FALC_DMACTL 0x10c 106 #define XUSB_FALC_IMFILLRNG1 0x154 107 #define IMFILLRNG1_TAG_MASK 0xffff 108 #define IMFILLRNG1_TAG_LO_SHIFT 0 109 #define IMFILLRNG1_TAG_HI_SHIFT 16 110 #define XUSB_FALC_IMFILLCTL 0x158 111 112 /* MP CSB registers */ 113 #define XUSB_CSB_MP_ILOAD_ATTR 0x101a00 114 #define XUSB_CSB_MP_ILOAD_BASE_LO 0x101a04 115 #define XUSB_CSB_MP_ILOAD_BASE_HI 0x101a08 116 #define XUSB_CSB_MP_L2IMEMOP_SIZE 0x101a10 117 #define L2IMEMOP_SIZE_SRC_OFFSET_SHIFT 8 118 #define L2IMEMOP_SIZE_SRC_OFFSET_MASK 0x3ff 119 #define L2IMEMOP_SIZE_SRC_COUNT_SHIFT 24 120 #define L2IMEMOP_SIZE_SRC_COUNT_MASK 0xff 121 #define XUSB_CSB_MP_L2IMEMOP_TRIG 0x101a14 122 #define L2IMEMOP_ACTION_SHIFT 24 123 #define L2IMEMOP_INVALIDATE_ALL (0x40 << L2IMEMOP_ACTION_SHIFT) 124 #define L2IMEMOP_LOAD_LOCKED_RESULT (0x11 << L2IMEMOP_ACTION_SHIFT) 125 #define XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT 0x101a18 126 #define L2IMEMOP_RESULT_VLD BIT(31) 127 #define XUSB_CSB_MP_APMAP 0x10181c 128 #define APMAP_BOOTPATH BIT(31) 129 130 #define IMEM_BLOCK_SIZE 256 131 132 struct tegra_xusb_fw_header { 133 __le32 boot_loadaddr_in_imem; 134 __le32 boot_codedfi_offset; 135 __le32 boot_codetag; 136 __le32 boot_codesize; 137 __le32 phys_memaddr; 138 __le16 reqphys_memsize; 139 __le16 alloc_phys_memsize; 140 __le32 rodata_img_offset; 141 __le32 rodata_section_start; 142 __le32 rodata_section_end; 143 __le32 main_fnaddr; 144 __le32 fwimg_cksum; 145 __le32 fwimg_created_time; 146 __le32 imem_resident_start; 147 __le32 imem_resident_end; 148 __le32 idirect_start; 149 __le32 idirect_end; 150 __le32 l2_imem_start; 151 __le32 l2_imem_end; 152 __le32 version_id; 153 u8 init_ddirect; 154 u8 reserved[3]; 155 __le32 phys_addr_log_buffer; 156 __le32 total_log_entries; 157 __le32 dequeue_ptr; 158 __le32 dummy_var[2]; 159 __le32 fwimg_len; 160 u8 magic[8]; 161 __le32 ss_low_power_entry_timeout; 162 u8 num_hsic_port; 163 u8 padding[139]; /* Pad to 256 bytes */ 164 }; 165 166 struct tegra_xusb_phy_type { 167 const char *name; 168 unsigned int num; 169 }; 170 171 struct tegra_xusb_mbox_regs { 172 u16 cmd; 173 u16 data_in; 174 u16 data_out; 175 u16 owner; 176 }; 177 178 struct tegra_xusb_context_soc { 179 struct { 180 const unsigned int *offsets; 181 unsigned int num_offsets; 182 } ipfs; 183 184 struct { 185 const unsigned int *offsets; 186 unsigned int num_offsets; 187 } fpci; 188 }; 189 190 struct tegra_xusb_soc { 191 const char *firmware; 192 const char * const *supply_names; 193 unsigned int num_supplies; 194 const struct tegra_xusb_phy_type *phy_types; 195 unsigned int num_types; 196 const struct tegra_xusb_context_soc *context; 197 198 struct { 199 struct { 200 unsigned int offset; 201 unsigned int count; 202 } usb2, ulpi, hsic, usb3; 203 } ports; 204 205 struct tegra_xusb_mbox_regs mbox; 206 207 bool scale_ss_clock; 208 bool has_ipfs; 209 bool lpm_support; 210 bool otg_reset_sspi; 211 }; 212 213 struct tegra_xusb_context { 214 u32 *ipfs; 215 u32 *fpci; 216 }; 217 218 struct tegra_xusb { 219 struct device *dev; 220 void __iomem *regs; 221 struct usb_hcd *hcd; 222 223 struct mutex lock; 224 225 int xhci_irq; 226 int mbox_irq; 227 228 void __iomem *ipfs_base; 229 void __iomem *fpci_base; 230 231 const struct tegra_xusb_soc *soc; 232 233 struct regulator_bulk_data *supplies; 234 235 struct tegra_xusb_padctl *padctl; 236 237 struct clk *host_clk; 238 struct clk *falcon_clk; 239 struct clk *ss_clk; 240 struct clk *ss_src_clk; 241 struct clk *hs_src_clk; 242 struct clk *fs_src_clk; 243 struct clk *pll_u_480m; 244 struct clk *clk_m; 245 struct clk *pll_e; 246 247 struct reset_control *host_rst; 248 struct reset_control *ss_rst; 249 250 struct device *genpd_dev_host; 251 struct device *genpd_dev_ss; 252 struct device_link *genpd_dl_host; 253 struct device_link *genpd_dl_ss; 254 255 struct phy **phys; 256 unsigned int num_phys; 257 258 struct usb_phy **usbphy; 259 unsigned int num_usb_phys; 260 int otg_usb2_port; 261 int otg_usb3_port; 262 bool host_mode; 263 struct notifier_block id_nb; 264 struct work_struct id_work; 265 266 /* Firmware loading related */ 267 struct { 268 size_t size; 269 void *virt; 270 dma_addr_t phys; 271 } fw; 272 273 struct tegra_xusb_context context; 274 }; 275 276 static struct hc_driver __read_mostly tegra_xhci_hc_driver; 277 278 static inline u32 fpci_readl(struct tegra_xusb *tegra, unsigned int offset) 279 { 280 return readl(tegra->fpci_base + offset); 281 } 282 283 static inline void fpci_writel(struct tegra_xusb *tegra, u32 value, 284 unsigned int offset) 285 { 286 writel(value, tegra->fpci_base + offset); 287 } 288 289 static inline u32 ipfs_readl(struct tegra_xusb *tegra, unsigned int offset) 290 { 291 return readl(tegra->ipfs_base + offset); 292 } 293 294 static inline void ipfs_writel(struct tegra_xusb *tegra, u32 value, 295 unsigned int offset) 296 { 297 writel(value, tegra->ipfs_base + offset); 298 } 299 300 static u32 csb_readl(struct tegra_xusb *tegra, unsigned int offset) 301 { 302 u32 page = CSB_PAGE_SELECT(offset); 303 u32 ofs = CSB_PAGE_OFFSET(offset); 304 305 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE); 306 307 return fpci_readl(tegra, XUSB_CFG_CSB_BASE_ADDR + ofs); 308 } 309 310 static void csb_writel(struct tegra_xusb *tegra, u32 value, 311 unsigned int offset) 312 { 313 u32 page = CSB_PAGE_SELECT(offset); 314 u32 ofs = CSB_PAGE_OFFSET(offset); 315 316 fpci_writel(tegra, page, XUSB_CFG_ARU_C11_CSBRANGE); 317 fpci_writel(tegra, value, XUSB_CFG_CSB_BASE_ADDR + ofs); 318 } 319 320 static int tegra_xusb_set_ss_clk(struct tegra_xusb *tegra, 321 unsigned long rate) 322 { 323 unsigned long new_parent_rate, old_parent_rate; 324 struct clk *clk = tegra->ss_src_clk; 325 unsigned int div; 326 int err; 327 328 if (clk_get_rate(clk) == rate) 329 return 0; 330 331 switch (rate) { 332 case TEGRA_XHCI_SS_HIGH_SPEED: 333 /* 334 * Reparent to PLLU_480M. Set divider first to avoid 335 * overclocking. 336 */ 337 old_parent_rate = clk_get_rate(clk_get_parent(clk)); 338 new_parent_rate = clk_get_rate(tegra->pll_u_480m); 339 div = new_parent_rate / rate; 340 341 err = clk_set_rate(clk, old_parent_rate / div); 342 if (err) 343 return err; 344 345 err = clk_set_parent(clk, tegra->pll_u_480m); 346 if (err) 347 return err; 348 349 /* 350 * The rate should already be correct, but set it again just 351 * to be sure. 352 */ 353 err = clk_set_rate(clk, rate); 354 if (err) 355 return err; 356 357 break; 358 359 case TEGRA_XHCI_SS_LOW_SPEED: 360 /* Reparent to CLK_M */ 361 err = clk_set_parent(clk, tegra->clk_m); 362 if (err) 363 return err; 364 365 err = clk_set_rate(clk, rate); 366 if (err) 367 return err; 368 369 break; 370 371 default: 372 dev_err(tegra->dev, "Invalid SS rate: %lu Hz\n", rate); 373 return -EINVAL; 374 } 375 376 if (clk_get_rate(clk) != rate) { 377 dev_err(tegra->dev, "SS clock doesn't match requested rate\n"); 378 return -EINVAL; 379 } 380 381 return 0; 382 } 383 384 static unsigned long extract_field(u32 value, unsigned int start, 385 unsigned int count) 386 { 387 return (value >> start) & ((1 << count) - 1); 388 } 389 390 /* Command requests from the firmware */ 391 enum tegra_xusb_mbox_cmd { 392 MBOX_CMD_MSG_ENABLED = 1, 393 MBOX_CMD_INC_FALC_CLOCK, 394 MBOX_CMD_DEC_FALC_CLOCK, 395 MBOX_CMD_INC_SSPI_CLOCK, 396 MBOX_CMD_DEC_SSPI_CLOCK, 397 MBOX_CMD_SET_BW, /* no ACK/NAK required */ 398 MBOX_CMD_SET_SS_PWR_GATING, 399 MBOX_CMD_SET_SS_PWR_UNGATING, 400 MBOX_CMD_SAVE_DFE_CTLE_CTX, 401 MBOX_CMD_AIRPLANE_MODE_ENABLED, /* unused */ 402 MBOX_CMD_AIRPLANE_MODE_DISABLED, /* unused */ 403 MBOX_CMD_START_HSIC_IDLE, 404 MBOX_CMD_STOP_HSIC_IDLE, 405 MBOX_CMD_DBC_WAKE_STACK, /* unused */ 406 MBOX_CMD_HSIC_PRETEND_CONNECT, 407 MBOX_CMD_RESET_SSPI, 408 MBOX_CMD_DISABLE_SS_LFPS_DETECTION, 409 MBOX_CMD_ENABLE_SS_LFPS_DETECTION, 410 411 MBOX_CMD_MAX, 412 413 /* Response message to above commands */ 414 MBOX_CMD_ACK = 128, 415 MBOX_CMD_NAK 416 }; 417 418 struct tegra_xusb_mbox_msg { 419 u32 cmd; 420 u32 data; 421 }; 422 423 static inline u32 tegra_xusb_mbox_pack(const struct tegra_xusb_mbox_msg *msg) 424 { 425 return (msg->cmd & CMD_TYPE_MASK) << CMD_TYPE_SHIFT | 426 (msg->data & CMD_DATA_MASK) << CMD_DATA_SHIFT; 427 } 428 static inline void tegra_xusb_mbox_unpack(struct tegra_xusb_mbox_msg *msg, 429 u32 value) 430 { 431 msg->cmd = (value >> CMD_TYPE_SHIFT) & CMD_TYPE_MASK; 432 msg->data = (value >> CMD_DATA_SHIFT) & CMD_DATA_MASK; 433 } 434 435 static bool tegra_xusb_mbox_cmd_requires_ack(enum tegra_xusb_mbox_cmd cmd) 436 { 437 switch (cmd) { 438 case MBOX_CMD_SET_BW: 439 case MBOX_CMD_ACK: 440 case MBOX_CMD_NAK: 441 return false; 442 443 default: 444 return true; 445 } 446 } 447 448 static int tegra_xusb_mbox_send(struct tegra_xusb *tegra, 449 const struct tegra_xusb_mbox_msg *msg) 450 { 451 bool wait_for_idle = false; 452 u32 value; 453 454 /* 455 * Acquire the mailbox. The firmware still owns the mailbox for 456 * ACK/NAK messages. 457 */ 458 if (!(msg->cmd == MBOX_CMD_ACK || msg->cmd == MBOX_CMD_NAK)) { 459 value = fpci_readl(tegra, tegra->soc->mbox.owner); 460 if (value != MBOX_OWNER_NONE) { 461 dev_err(tegra->dev, "mailbox is busy\n"); 462 return -EBUSY; 463 } 464 465 fpci_writel(tegra, MBOX_OWNER_SW, tegra->soc->mbox.owner); 466 467 value = fpci_readl(tegra, tegra->soc->mbox.owner); 468 if (value != MBOX_OWNER_SW) { 469 dev_err(tegra->dev, "failed to acquire mailbox\n"); 470 return -EBUSY; 471 } 472 473 wait_for_idle = true; 474 } 475 476 value = tegra_xusb_mbox_pack(msg); 477 fpci_writel(tegra, value, tegra->soc->mbox.data_in); 478 479 value = fpci_readl(tegra, tegra->soc->mbox.cmd); 480 value |= MBOX_INT_EN | MBOX_DEST_FALC; 481 fpci_writel(tegra, value, tegra->soc->mbox.cmd); 482 483 if (wait_for_idle) { 484 unsigned long timeout = jiffies + msecs_to_jiffies(250); 485 486 while (time_before(jiffies, timeout)) { 487 value = fpci_readl(tegra, tegra->soc->mbox.owner); 488 if (value == MBOX_OWNER_NONE) 489 break; 490 491 usleep_range(10, 20); 492 } 493 494 if (time_after(jiffies, timeout)) 495 value = fpci_readl(tegra, tegra->soc->mbox.owner); 496 497 if (value != MBOX_OWNER_NONE) 498 return -ETIMEDOUT; 499 } 500 501 return 0; 502 } 503 504 static irqreturn_t tegra_xusb_mbox_irq(int irq, void *data) 505 { 506 struct tegra_xusb *tegra = data; 507 u32 value; 508 509 /* clear mailbox interrupts */ 510 value = fpci_readl(tegra, XUSB_CFG_ARU_SMI_INTR); 511 fpci_writel(tegra, value, XUSB_CFG_ARU_SMI_INTR); 512 513 if (value & MBOX_SMI_INTR_FW_HANG) 514 dev_err(tegra->dev, "controller firmware hang\n"); 515 516 return IRQ_WAKE_THREAD; 517 } 518 519 static void tegra_xusb_mbox_handle(struct tegra_xusb *tegra, 520 const struct tegra_xusb_mbox_msg *msg) 521 { 522 struct tegra_xusb_padctl *padctl = tegra->padctl; 523 const struct tegra_xusb_soc *soc = tegra->soc; 524 struct device *dev = tegra->dev; 525 struct tegra_xusb_mbox_msg rsp; 526 unsigned long mask; 527 unsigned int port; 528 bool idle, enable; 529 int err = 0; 530 531 memset(&rsp, 0, sizeof(rsp)); 532 533 switch (msg->cmd) { 534 case MBOX_CMD_INC_FALC_CLOCK: 535 case MBOX_CMD_DEC_FALC_CLOCK: 536 rsp.data = clk_get_rate(tegra->falcon_clk) / 1000; 537 if (rsp.data != msg->data) 538 rsp.cmd = MBOX_CMD_NAK; 539 else 540 rsp.cmd = MBOX_CMD_ACK; 541 542 break; 543 544 case MBOX_CMD_INC_SSPI_CLOCK: 545 case MBOX_CMD_DEC_SSPI_CLOCK: 546 if (tegra->soc->scale_ss_clock) { 547 err = tegra_xusb_set_ss_clk(tegra, msg->data * 1000); 548 if (err < 0) 549 rsp.cmd = MBOX_CMD_NAK; 550 else 551 rsp.cmd = MBOX_CMD_ACK; 552 553 rsp.data = clk_get_rate(tegra->ss_src_clk) / 1000; 554 } else { 555 rsp.cmd = MBOX_CMD_ACK; 556 rsp.data = msg->data; 557 } 558 559 break; 560 561 case MBOX_CMD_SET_BW: 562 /* 563 * TODO: Request bandwidth once EMC scaling is supported. 564 * Ignore for now since ACK/NAK is not required for SET_BW 565 * messages. 566 */ 567 break; 568 569 case MBOX_CMD_SAVE_DFE_CTLE_CTX: 570 err = tegra_xusb_padctl_usb3_save_context(padctl, msg->data); 571 if (err < 0) { 572 dev_err(dev, "failed to save context for USB3#%u: %d\n", 573 msg->data, err); 574 rsp.cmd = MBOX_CMD_NAK; 575 } else { 576 rsp.cmd = MBOX_CMD_ACK; 577 } 578 579 rsp.data = msg->data; 580 break; 581 582 case MBOX_CMD_START_HSIC_IDLE: 583 case MBOX_CMD_STOP_HSIC_IDLE: 584 if (msg->cmd == MBOX_CMD_STOP_HSIC_IDLE) 585 idle = false; 586 else 587 idle = true; 588 589 mask = extract_field(msg->data, 1 + soc->ports.hsic.offset, 590 soc->ports.hsic.count); 591 592 for_each_set_bit(port, &mask, 32) { 593 err = tegra_xusb_padctl_hsic_set_idle(padctl, port, 594 idle); 595 if (err < 0) 596 break; 597 } 598 599 if (err < 0) { 600 dev_err(dev, "failed to set HSIC#%u %s: %d\n", port, 601 idle ? "idle" : "busy", err); 602 rsp.cmd = MBOX_CMD_NAK; 603 } else { 604 rsp.cmd = MBOX_CMD_ACK; 605 } 606 607 rsp.data = msg->data; 608 break; 609 610 case MBOX_CMD_DISABLE_SS_LFPS_DETECTION: 611 case MBOX_CMD_ENABLE_SS_LFPS_DETECTION: 612 if (msg->cmd == MBOX_CMD_DISABLE_SS_LFPS_DETECTION) 613 enable = false; 614 else 615 enable = true; 616 617 mask = extract_field(msg->data, 1 + soc->ports.usb3.offset, 618 soc->ports.usb3.count); 619 620 for_each_set_bit(port, &mask, soc->ports.usb3.count) { 621 err = tegra_xusb_padctl_usb3_set_lfps_detect(padctl, 622 port, 623 enable); 624 if (err < 0) 625 break; 626 627 /* 628 * wait 500us for LFPS detector to be disabled before 629 * sending ACK 630 */ 631 if (!enable) 632 usleep_range(500, 1000); 633 } 634 635 if (err < 0) { 636 dev_err(dev, 637 "failed to %s LFPS detection on USB3#%u: %d\n", 638 enable ? "enable" : "disable", port, err); 639 rsp.cmd = MBOX_CMD_NAK; 640 } else { 641 rsp.cmd = MBOX_CMD_ACK; 642 } 643 644 rsp.data = msg->data; 645 break; 646 647 default: 648 dev_warn(dev, "unknown message: %#x\n", msg->cmd); 649 break; 650 } 651 652 if (rsp.cmd) { 653 const char *cmd = (rsp.cmd == MBOX_CMD_ACK) ? "ACK" : "NAK"; 654 655 err = tegra_xusb_mbox_send(tegra, &rsp); 656 if (err < 0) 657 dev_err(dev, "failed to send %s: %d\n", cmd, err); 658 } 659 } 660 661 static irqreturn_t tegra_xusb_mbox_thread(int irq, void *data) 662 { 663 struct tegra_xusb *tegra = data; 664 struct tegra_xusb_mbox_msg msg; 665 u32 value; 666 667 mutex_lock(&tegra->lock); 668 669 value = fpci_readl(tegra, tegra->soc->mbox.data_out); 670 tegra_xusb_mbox_unpack(&msg, value); 671 672 value = fpci_readl(tegra, tegra->soc->mbox.cmd); 673 value &= ~MBOX_DEST_SMI; 674 fpci_writel(tegra, value, tegra->soc->mbox.cmd); 675 676 /* clear mailbox owner if no ACK/NAK is required */ 677 if (!tegra_xusb_mbox_cmd_requires_ack(msg.cmd)) 678 fpci_writel(tegra, MBOX_OWNER_NONE, tegra->soc->mbox.owner); 679 680 tegra_xusb_mbox_handle(tegra, &msg); 681 682 mutex_unlock(&tegra->lock); 683 return IRQ_HANDLED; 684 } 685 686 static void tegra_xusb_config(struct tegra_xusb *tegra) 687 { 688 u32 regs = tegra->hcd->rsrc_start; 689 u32 value; 690 691 if (tegra->soc->has_ipfs) { 692 value = ipfs_readl(tegra, IPFS_XUSB_HOST_CONFIGURATION_0); 693 value |= IPFS_EN_FPCI; 694 ipfs_writel(tegra, value, IPFS_XUSB_HOST_CONFIGURATION_0); 695 696 usleep_range(10, 20); 697 } 698 699 /* Program BAR0 space */ 700 value = fpci_readl(tegra, XUSB_CFG_4); 701 value &= ~(XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT); 702 value |= regs & (XUSB_BASE_ADDR_MASK << XUSB_BASE_ADDR_SHIFT); 703 fpci_writel(tegra, value, XUSB_CFG_4); 704 705 usleep_range(100, 200); 706 707 /* Enable bus master */ 708 value = fpci_readl(tegra, XUSB_CFG_1); 709 value |= XUSB_IO_SPACE_EN | XUSB_MEM_SPACE_EN | XUSB_BUS_MASTER_EN; 710 fpci_writel(tegra, value, XUSB_CFG_1); 711 712 if (tegra->soc->has_ipfs) { 713 /* Enable interrupt assertion */ 714 value = ipfs_readl(tegra, IPFS_XUSB_HOST_INTR_MASK_0); 715 value |= IPFS_IP_INT_MASK; 716 ipfs_writel(tegra, value, IPFS_XUSB_HOST_INTR_MASK_0); 717 718 /* Set hysteresis */ 719 ipfs_writel(tegra, 0x80, IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0); 720 } 721 } 722 723 static int tegra_xusb_clk_enable(struct tegra_xusb *tegra) 724 { 725 int err; 726 727 err = clk_prepare_enable(tegra->pll_e); 728 if (err < 0) 729 return err; 730 731 err = clk_prepare_enable(tegra->host_clk); 732 if (err < 0) 733 goto disable_plle; 734 735 err = clk_prepare_enable(tegra->ss_clk); 736 if (err < 0) 737 goto disable_host; 738 739 err = clk_prepare_enable(tegra->falcon_clk); 740 if (err < 0) 741 goto disable_ss; 742 743 err = clk_prepare_enable(tegra->fs_src_clk); 744 if (err < 0) 745 goto disable_falc; 746 747 err = clk_prepare_enable(tegra->hs_src_clk); 748 if (err < 0) 749 goto disable_fs_src; 750 751 if (tegra->soc->scale_ss_clock) { 752 err = tegra_xusb_set_ss_clk(tegra, TEGRA_XHCI_SS_HIGH_SPEED); 753 if (err < 0) 754 goto disable_hs_src; 755 } 756 757 return 0; 758 759 disable_hs_src: 760 clk_disable_unprepare(tegra->hs_src_clk); 761 disable_fs_src: 762 clk_disable_unprepare(tegra->fs_src_clk); 763 disable_falc: 764 clk_disable_unprepare(tegra->falcon_clk); 765 disable_ss: 766 clk_disable_unprepare(tegra->ss_clk); 767 disable_host: 768 clk_disable_unprepare(tegra->host_clk); 769 disable_plle: 770 clk_disable_unprepare(tegra->pll_e); 771 return err; 772 } 773 774 static void tegra_xusb_clk_disable(struct tegra_xusb *tegra) 775 { 776 clk_disable_unprepare(tegra->pll_e); 777 clk_disable_unprepare(tegra->host_clk); 778 clk_disable_unprepare(tegra->ss_clk); 779 clk_disable_unprepare(tegra->falcon_clk); 780 clk_disable_unprepare(tegra->fs_src_clk); 781 clk_disable_unprepare(tegra->hs_src_clk); 782 } 783 784 static int tegra_xusb_phy_enable(struct tegra_xusb *tegra) 785 { 786 unsigned int i; 787 int err; 788 789 for (i = 0; i < tegra->num_phys; i++) { 790 err = phy_init(tegra->phys[i]); 791 if (err) 792 goto disable_phy; 793 794 err = phy_power_on(tegra->phys[i]); 795 if (err) { 796 phy_exit(tegra->phys[i]); 797 goto disable_phy; 798 } 799 } 800 801 return 0; 802 803 disable_phy: 804 while (i--) { 805 phy_power_off(tegra->phys[i]); 806 phy_exit(tegra->phys[i]); 807 } 808 809 return err; 810 } 811 812 static void tegra_xusb_phy_disable(struct tegra_xusb *tegra) 813 { 814 unsigned int i; 815 816 for (i = 0; i < tegra->num_phys; i++) { 817 phy_power_off(tegra->phys[i]); 818 phy_exit(tegra->phys[i]); 819 } 820 } 821 822 static int tegra_xusb_runtime_suspend(struct device *dev) 823 { 824 struct tegra_xusb *tegra = dev_get_drvdata(dev); 825 826 regulator_bulk_disable(tegra->soc->num_supplies, tegra->supplies); 827 tegra_xusb_clk_disable(tegra); 828 829 return 0; 830 } 831 832 static int tegra_xusb_runtime_resume(struct device *dev) 833 { 834 struct tegra_xusb *tegra = dev_get_drvdata(dev); 835 int err; 836 837 err = tegra_xusb_clk_enable(tegra); 838 if (err) { 839 dev_err(dev, "failed to enable clocks: %d\n", err); 840 return err; 841 } 842 843 err = regulator_bulk_enable(tegra->soc->num_supplies, tegra->supplies); 844 if (err) { 845 dev_err(dev, "failed to enable regulators: %d\n", err); 846 goto disable_clk; 847 } 848 849 return 0; 850 851 disable_clk: 852 tegra_xusb_clk_disable(tegra); 853 return err; 854 } 855 856 #ifdef CONFIG_PM_SLEEP 857 static int tegra_xusb_init_context(struct tegra_xusb *tegra) 858 { 859 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 860 861 tegra->context.ipfs = devm_kcalloc(tegra->dev, soc->ipfs.num_offsets, 862 sizeof(u32), GFP_KERNEL); 863 if (!tegra->context.ipfs) 864 return -ENOMEM; 865 866 tegra->context.fpci = devm_kcalloc(tegra->dev, soc->fpci.num_offsets, 867 sizeof(u32), GFP_KERNEL); 868 if (!tegra->context.fpci) 869 return -ENOMEM; 870 871 return 0; 872 } 873 #else 874 static inline int tegra_xusb_init_context(struct tegra_xusb *tegra) 875 { 876 return 0; 877 } 878 #endif 879 880 static int tegra_xusb_request_firmware(struct tegra_xusb *tegra) 881 { 882 struct tegra_xusb_fw_header *header; 883 const struct firmware *fw; 884 int err; 885 886 err = request_firmware(&fw, tegra->soc->firmware, tegra->dev); 887 if (err < 0) { 888 dev_err(tegra->dev, "failed to request firmware: %d\n", err); 889 return err; 890 } 891 892 /* Load Falcon controller with its firmware. */ 893 header = (struct tegra_xusb_fw_header *)fw->data; 894 tegra->fw.size = le32_to_cpu(header->fwimg_len); 895 896 tegra->fw.virt = dma_alloc_coherent(tegra->dev, tegra->fw.size, 897 &tegra->fw.phys, GFP_KERNEL); 898 if (!tegra->fw.virt) { 899 dev_err(tegra->dev, "failed to allocate memory for firmware\n"); 900 release_firmware(fw); 901 return -ENOMEM; 902 } 903 904 header = (struct tegra_xusb_fw_header *)tegra->fw.virt; 905 memcpy(tegra->fw.virt, fw->data, tegra->fw.size); 906 release_firmware(fw); 907 908 return 0; 909 } 910 911 static int tegra_xusb_load_firmware(struct tegra_xusb *tegra) 912 { 913 unsigned int code_tag_blocks, code_size_blocks, code_blocks; 914 struct xhci_cap_regs __iomem *cap = tegra->regs; 915 struct tegra_xusb_fw_header *header; 916 struct device *dev = tegra->dev; 917 struct xhci_op_regs __iomem *op; 918 unsigned long timeout; 919 time64_t timestamp; 920 u64 address; 921 u32 value; 922 int err; 923 924 header = (struct tegra_xusb_fw_header *)tegra->fw.virt; 925 op = tegra->regs + HC_LENGTH(readl(&cap->hc_capbase)); 926 927 if (csb_readl(tegra, XUSB_CSB_MP_ILOAD_BASE_LO) != 0) { 928 dev_info(dev, "Firmware already loaded, Falcon state %#x\n", 929 csb_readl(tegra, XUSB_FALC_CPUCTL)); 930 return 0; 931 } 932 933 /* Program the size of DFI into ILOAD_ATTR. */ 934 csb_writel(tegra, tegra->fw.size, XUSB_CSB_MP_ILOAD_ATTR); 935 936 /* 937 * Boot code of the firmware reads the ILOAD_BASE registers 938 * to get to the start of the DFI in system memory. 939 */ 940 address = tegra->fw.phys + sizeof(*header); 941 csb_writel(tegra, address >> 32, XUSB_CSB_MP_ILOAD_BASE_HI); 942 csb_writel(tegra, address, XUSB_CSB_MP_ILOAD_BASE_LO); 943 944 /* Set BOOTPATH to 1 in APMAP. */ 945 csb_writel(tegra, APMAP_BOOTPATH, XUSB_CSB_MP_APMAP); 946 947 /* Invalidate L2IMEM. */ 948 csb_writel(tegra, L2IMEMOP_INVALIDATE_ALL, XUSB_CSB_MP_L2IMEMOP_TRIG); 949 950 /* 951 * Initiate fetch of bootcode from system memory into L2IMEM. 952 * Program bootcode location and size in system memory. 953 */ 954 code_tag_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codetag), 955 IMEM_BLOCK_SIZE); 956 code_size_blocks = DIV_ROUND_UP(le32_to_cpu(header->boot_codesize), 957 IMEM_BLOCK_SIZE); 958 code_blocks = code_tag_blocks + code_size_blocks; 959 960 value = ((code_tag_blocks & L2IMEMOP_SIZE_SRC_OFFSET_MASK) << 961 L2IMEMOP_SIZE_SRC_OFFSET_SHIFT) | 962 ((code_size_blocks & L2IMEMOP_SIZE_SRC_COUNT_MASK) << 963 L2IMEMOP_SIZE_SRC_COUNT_SHIFT); 964 csb_writel(tegra, value, XUSB_CSB_MP_L2IMEMOP_SIZE); 965 966 /* Trigger L2IMEM load operation. */ 967 csb_writel(tegra, L2IMEMOP_LOAD_LOCKED_RESULT, 968 XUSB_CSB_MP_L2IMEMOP_TRIG); 969 970 /* Setup Falcon auto-fill. */ 971 csb_writel(tegra, code_size_blocks, XUSB_FALC_IMFILLCTL); 972 973 value = ((code_tag_blocks & IMFILLRNG1_TAG_MASK) << 974 IMFILLRNG1_TAG_LO_SHIFT) | 975 ((code_blocks & IMFILLRNG1_TAG_MASK) << 976 IMFILLRNG1_TAG_HI_SHIFT); 977 csb_writel(tegra, value, XUSB_FALC_IMFILLRNG1); 978 979 csb_writel(tegra, 0, XUSB_FALC_DMACTL); 980 981 /* wait for RESULT_VLD to get set */ 982 #define tegra_csb_readl(offset) csb_readl(tegra, offset) 983 err = readx_poll_timeout(tegra_csb_readl, 984 XUSB_CSB_MEMPOOL_L2IMEMOP_RESULT, value, 985 value & L2IMEMOP_RESULT_VLD, 100, 10000); 986 if (err < 0) { 987 dev_err(dev, "DMA controller not ready %#010x\n", value); 988 return err; 989 } 990 #undef tegra_csb_readl 991 992 csb_writel(tegra, le32_to_cpu(header->boot_codetag), 993 XUSB_FALC_BOOTVEC); 994 995 /* Boot Falcon CPU and wait for USBSTS_CNR to get cleared. */ 996 csb_writel(tegra, CPUCTL_STARTCPU, XUSB_FALC_CPUCTL); 997 998 timeout = jiffies + msecs_to_jiffies(200); 999 1000 do { 1001 value = readl(&op->status); 1002 if ((value & STS_CNR) == 0) 1003 break; 1004 1005 usleep_range(1000, 2000); 1006 } while (time_is_after_jiffies(timeout)); 1007 1008 value = readl(&op->status); 1009 if (value & STS_CNR) { 1010 value = csb_readl(tegra, XUSB_FALC_CPUCTL); 1011 dev_err(dev, "XHCI controller not read: %#010x\n", value); 1012 return -EIO; 1013 } 1014 1015 timestamp = le32_to_cpu(header->fwimg_created_time); 1016 1017 dev_info(dev, "Firmware timestamp: %ptTs UTC\n", ×tamp); 1018 1019 return 0; 1020 } 1021 1022 static void tegra_xusb_powerdomain_remove(struct device *dev, 1023 struct tegra_xusb *tegra) 1024 { 1025 if (tegra->genpd_dl_ss) 1026 device_link_del(tegra->genpd_dl_ss); 1027 if (tegra->genpd_dl_host) 1028 device_link_del(tegra->genpd_dl_host); 1029 if (!IS_ERR_OR_NULL(tegra->genpd_dev_ss)) 1030 dev_pm_domain_detach(tegra->genpd_dev_ss, true); 1031 if (!IS_ERR_OR_NULL(tegra->genpd_dev_host)) 1032 dev_pm_domain_detach(tegra->genpd_dev_host, true); 1033 } 1034 1035 static int tegra_xusb_powerdomain_init(struct device *dev, 1036 struct tegra_xusb *tegra) 1037 { 1038 int err; 1039 1040 tegra->genpd_dev_host = dev_pm_domain_attach_by_name(dev, "xusb_host"); 1041 if (IS_ERR(tegra->genpd_dev_host)) { 1042 err = PTR_ERR(tegra->genpd_dev_host); 1043 dev_err(dev, "failed to get host pm-domain: %d\n", err); 1044 return err; 1045 } 1046 1047 tegra->genpd_dev_ss = dev_pm_domain_attach_by_name(dev, "xusb_ss"); 1048 if (IS_ERR(tegra->genpd_dev_ss)) { 1049 err = PTR_ERR(tegra->genpd_dev_ss); 1050 dev_err(dev, "failed to get superspeed pm-domain: %d\n", err); 1051 return err; 1052 } 1053 1054 tegra->genpd_dl_host = device_link_add(dev, tegra->genpd_dev_host, 1055 DL_FLAG_PM_RUNTIME | 1056 DL_FLAG_STATELESS); 1057 if (!tegra->genpd_dl_host) { 1058 dev_err(dev, "adding host device link failed!\n"); 1059 return -ENODEV; 1060 } 1061 1062 tegra->genpd_dl_ss = device_link_add(dev, tegra->genpd_dev_ss, 1063 DL_FLAG_PM_RUNTIME | 1064 DL_FLAG_STATELESS); 1065 if (!tegra->genpd_dl_ss) { 1066 dev_err(dev, "adding superspeed device link failed!\n"); 1067 return -ENODEV; 1068 } 1069 1070 return 0; 1071 } 1072 1073 static int __tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra) 1074 { 1075 struct tegra_xusb_mbox_msg msg; 1076 int err; 1077 1078 /* Enable firmware messages from controller. */ 1079 msg.cmd = MBOX_CMD_MSG_ENABLED; 1080 msg.data = 0; 1081 1082 err = tegra_xusb_mbox_send(tegra, &msg); 1083 if (err < 0) 1084 dev_err(tegra->dev, "failed to enable messages: %d\n", err); 1085 1086 return err; 1087 } 1088 1089 static int tegra_xusb_enable_firmware_messages(struct tegra_xusb *tegra) 1090 { 1091 int err; 1092 1093 mutex_lock(&tegra->lock); 1094 err = __tegra_xusb_enable_firmware_messages(tegra); 1095 mutex_unlock(&tegra->lock); 1096 1097 return err; 1098 } 1099 1100 static void tegra_xhci_set_port_power(struct tegra_xusb *tegra, bool main, 1101 bool set) 1102 { 1103 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1104 struct usb_hcd *hcd = main ? xhci->main_hcd : xhci->shared_hcd; 1105 unsigned int wait = (!main && !set) ? 1000 : 10; 1106 u16 typeReq = set ? SetPortFeature : ClearPortFeature; 1107 u16 wIndex = main ? tegra->otg_usb2_port + 1 : tegra->otg_usb3_port + 1; 1108 u32 status; 1109 u32 stat_power = main ? USB_PORT_STAT_POWER : USB_SS_PORT_STAT_POWER; 1110 u32 status_val = set ? stat_power : 0; 1111 1112 dev_dbg(tegra->dev, "%s():%s %s port power\n", __func__, 1113 set ? "set" : "clear", main ? "HS" : "SS"); 1114 1115 hcd->driver->hub_control(hcd, typeReq, USB_PORT_FEAT_POWER, wIndex, 1116 NULL, 0); 1117 1118 do { 1119 tegra_xhci_hc_driver.hub_control(hcd, GetPortStatus, 0, wIndex, 1120 (char *) &status, sizeof(status)); 1121 if (status_val == (status & stat_power)) 1122 break; 1123 1124 if (!main && !set) 1125 usleep_range(600, 700); 1126 else 1127 usleep_range(10, 20); 1128 } while (--wait > 0); 1129 1130 if (status_val != (status & stat_power)) 1131 dev_info(tegra->dev, "failed to %s %s PP %d\n", 1132 set ? "set" : "clear", 1133 main ? "HS" : "SS", status); 1134 } 1135 1136 static struct phy *tegra_xusb_get_phy(struct tegra_xusb *tegra, char *name, 1137 int port) 1138 { 1139 unsigned int i, phy_count = 0; 1140 1141 for (i = 0; i < tegra->soc->num_types; i++) { 1142 if (!strncmp(tegra->soc->phy_types[i].name, name, 1143 strlen(name))) 1144 return tegra->phys[phy_count+port]; 1145 1146 phy_count += tegra->soc->phy_types[i].num; 1147 } 1148 1149 return NULL; 1150 } 1151 1152 static void tegra_xhci_id_work(struct work_struct *work) 1153 { 1154 struct tegra_xusb *tegra = container_of(work, struct tegra_xusb, 1155 id_work); 1156 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1157 struct tegra_xusb_mbox_msg msg; 1158 struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", 1159 tegra->otg_usb2_port); 1160 u32 status; 1161 int ret; 1162 1163 dev_dbg(tegra->dev, "host mode %s\n", tegra->host_mode ? "on" : "off"); 1164 1165 mutex_lock(&tegra->lock); 1166 1167 if (tegra->host_mode) 1168 phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_HOST); 1169 else 1170 phy_set_mode_ext(phy, PHY_MODE_USB_OTG, USB_ROLE_NONE); 1171 1172 mutex_unlock(&tegra->lock); 1173 1174 if (tegra->host_mode) { 1175 /* switch to host mode */ 1176 if (tegra->otg_usb3_port >= 0) { 1177 if (tegra->soc->otg_reset_sspi) { 1178 /* set PP=0 */ 1179 tegra_xhci_hc_driver.hub_control( 1180 xhci->shared_hcd, GetPortStatus, 1181 0, tegra->otg_usb3_port+1, 1182 (char *) &status, sizeof(status)); 1183 if (status & USB_SS_PORT_STAT_POWER) 1184 tegra_xhci_set_port_power(tegra, false, 1185 false); 1186 1187 /* reset OTG port SSPI */ 1188 msg.cmd = MBOX_CMD_RESET_SSPI; 1189 msg.data = tegra->otg_usb3_port+1; 1190 1191 ret = tegra_xusb_mbox_send(tegra, &msg); 1192 if (ret < 0) { 1193 dev_info(tegra->dev, 1194 "failed to RESET_SSPI %d\n", 1195 ret); 1196 } 1197 } 1198 1199 tegra_xhci_set_port_power(tegra, false, true); 1200 } 1201 1202 tegra_xhci_set_port_power(tegra, true, true); 1203 1204 } else { 1205 if (tegra->otg_usb3_port >= 0) 1206 tegra_xhci_set_port_power(tegra, false, false); 1207 1208 tegra_xhci_set_port_power(tegra, true, false); 1209 } 1210 } 1211 1212 static int tegra_xusb_get_usb2_port(struct tegra_xusb *tegra, 1213 struct usb_phy *usbphy) 1214 { 1215 unsigned int i; 1216 1217 for (i = 0; i < tegra->num_usb_phys; i++) { 1218 if (tegra->usbphy[i] && usbphy == tegra->usbphy[i]) 1219 return i; 1220 } 1221 1222 return -1; 1223 } 1224 1225 static int tegra_xhci_id_notify(struct notifier_block *nb, 1226 unsigned long action, void *data) 1227 { 1228 struct tegra_xusb *tegra = container_of(nb, struct tegra_xusb, 1229 id_nb); 1230 struct usb_phy *usbphy = (struct usb_phy *)data; 1231 1232 dev_dbg(tegra->dev, "%s(): action is %d", __func__, usbphy->last_event); 1233 1234 if ((tegra->host_mode && usbphy->last_event == USB_EVENT_ID) || 1235 (!tegra->host_mode && usbphy->last_event != USB_EVENT_ID)) { 1236 dev_dbg(tegra->dev, "Same role(%d) received. Ignore", 1237 tegra->host_mode); 1238 return NOTIFY_OK; 1239 } 1240 1241 tegra->otg_usb2_port = tegra_xusb_get_usb2_port(tegra, usbphy); 1242 tegra->otg_usb3_port = tegra_xusb_padctl_get_usb3_companion( 1243 tegra->padctl, 1244 tegra->otg_usb2_port); 1245 1246 tegra->host_mode = (usbphy->last_event == USB_EVENT_ID) ? true : false; 1247 1248 schedule_work(&tegra->id_work); 1249 1250 return NOTIFY_OK; 1251 } 1252 1253 static int tegra_xusb_init_usb_phy(struct tegra_xusb *tegra) 1254 { 1255 unsigned int i; 1256 1257 tegra->usbphy = devm_kcalloc(tegra->dev, tegra->num_usb_phys, 1258 sizeof(*tegra->usbphy), GFP_KERNEL); 1259 if (!tegra->usbphy) 1260 return -ENOMEM; 1261 1262 INIT_WORK(&tegra->id_work, tegra_xhci_id_work); 1263 tegra->id_nb.notifier_call = tegra_xhci_id_notify; 1264 tegra->otg_usb2_port = -EINVAL; 1265 tegra->otg_usb3_port = -EINVAL; 1266 1267 for (i = 0; i < tegra->num_usb_phys; i++) { 1268 struct phy *phy = tegra_xusb_get_phy(tegra, "usb2", i); 1269 1270 if (!phy) 1271 continue; 1272 1273 tegra->usbphy[i] = devm_usb_get_phy_by_node(tegra->dev, 1274 phy->dev.of_node, 1275 &tegra->id_nb); 1276 if (!IS_ERR(tegra->usbphy[i])) { 1277 dev_dbg(tegra->dev, "usbphy-%d registered", i); 1278 otg_set_host(tegra->usbphy[i]->otg, &tegra->hcd->self); 1279 } else { 1280 /* 1281 * usb-phy is optional, continue if its not available. 1282 */ 1283 tegra->usbphy[i] = NULL; 1284 } 1285 } 1286 1287 return 0; 1288 } 1289 1290 static void tegra_xusb_deinit_usb_phy(struct tegra_xusb *tegra) 1291 { 1292 unsigned int i; 1293 1294 cancel_work_sync(&tegra->id_work); 1295 1296 for (i = 0; i < tegra->num_usb_phys; i++) 1297 if (tegra->usbphy[i]) 1298 otg_set_host(tegra->usbphy[i]->otg, NULL); 1299 } 1300 1301 static int tegra_xusb_probe(struct platform_device *pdev) 1302 { 1303 struct tegra_xusb *tegra; 1304 struct resource *regs; 1305 struct xhci_hcd *xhci; 1306 unsigned int i, j, k; 1307 struct phy *phy; 1308 int err; 1309 1310 BUILD_BUG_ON(sizeof(struct tegra_xusb_fw_header) != 256); 1311 1312 tegra = devm_kzalloc(&pdev->dev, sizeof(*tegra), GFP_KERNEL); 1313 if (!tegra) 1314 return -ENOMEM; 1315 1316 tegra->soc = of_device_get_match_data(&pdev->dev); 1317 mutex_init(&tegra->lock); 1318 tegra->dev = &pdev->dev; 1319 1320 err = tegra_xusb_init_context(tegra); 1321 if (err < 0) 1322 return err; 1323 1324 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0); 1325 tegra->regs = devm_ioremap_resource(&pdev->dev, regs); 1326 if (IS_ERR(tegra->regs)) 1327 return PTR_ERR(tegra->regs); 1328 1329 tegra->fpci_base = devm_platform_ioremap_resource(pdev, 1); 1330 if (IS_ERR(tegra->fpci_base)) 1331 return PTR_ERR(tegra->fpci_base); 1332 1333 if (tegra->soc->has_ipfs) { 1334 tegra->ipfs_base = devm_platform_ioremap_resource(pdev, 2); 1335 if (IS_ERR(tegra->ipfs_base)) 1336 return PTR_ERR(tegra->ipfs_base); 1337 } 1338 1339 tegra->xhci_irq = platform_get_irq(pdev, 0); 1340 if (tegra->xhci_irq < 0) 1341 return tegra->xhci_irq; 1342 1343 tegra->mbox_irq = platform_get_irq(pdev, 1); 1344 if (tegra->mbox_irq < 0) 1345 return tegra->mbox_irq; 1346 1347 tegra->padctl = tegra_xusb_padctl_get(&pdev->dev); 1348 if (IS_ERR(tegra->padctl)) 1349 return PTR_ERR(tegra->padctl); 1350 1351 tegra->host_clk = devm_clk_get(&pdev->dev, "xusb_host"); 1352 if (IS_ERR(tegra->host_clk)) { 1353 err = PTR_ERR(tegra->host_clk); 1354 dev_err(&pdev->dev, "failed to get xusb_host: %d\n", err); 1355 goto put_padctl; 1356 } 1357 1358 tegra->falcon_clk = devm_clk_get(&pdev->dev, "xusb_falcon_src"); 1359 if (IS_ERR(tegra->falcon_clk)) { 1360 err = PTR_ERR(tegra->falcon_clk); 1361 dev_err(&pdev->dev, "failed to get xusb_falcon_src: %d\n", err); 1362 goto put_padctl; 1363 } 1364 1365 tegra->ss_clk = devm_clk_get(&pdev->dev, "xusb_ss"); 1366 if (IS_ERR(tegra->ss_clk)) { 1367 err = PTR_ERR(tegra->ss_clk); 1368 dev_err(&pdev->dev, "failed to get xusb_ss: %d\n", err); 1369 goto put_padctl; 1370 } 1371 1372 tegra->ss_src_clk = devm_clk_get(&pdev->dev, "xusb_ss_src"); 1373 if (IS_ERR(tegra->ss_src_clk)) { 1374 err = PTR_ERR(tegra->ss_src_clk); 1375 dev_err(&pdev->dev, "failed to get xusb_ss_src: %d\n", err); 1376 goto put_padctl; 1377 } 1378 1379 tegra->hs_src_clk = devm_clk_get(&pdev->dev, "xusb_hs_src"); 1380 if (IS_ERR(tegra->hs_src_clk)) { 1381 err = PTR_ERR(tegra->hs_src_clk); 1382 dev_err(&pdev->dev, "failed to get xusb_hs_src: %d\n", err); 1383 goto put_padctl; 1384 } 1385 1386 tegra->fs_src_clk = devm_clk_get(&pdev->dev, "xusb_fs_src"); 1387 if (IS_ERR(tegra->fs_src_clk)) { 1388 err = PTR_ERR(tegra->fs_src_clk); 1389 dev_err(&pdev->dev, "failed to get xusb_fs_src: %d\n", err); 1390 goto put_padctl; 1391 } 1392 1393 tegra->pll_u_480m = devm_clk_get(&pdev->dev, "pll_u_480m"); 1394 if (IS_ERR(tegra->pll_u_480m)) { 1395 err = PTR_ERR(tegra->pll_u_480m); 1396 dev_err(&pdev->dev, "failed to get pll_u_480m: %d\n", err); 1397 goto put_padctl; 1398 } 1399 1400 tegra->clk_m = devm_clk_get(&pdev->dev, "clk_m"); 1401 if (IS_ERR(tegra->clk_m)) { 1402 err = PTR_ERR(tegra->clk_m); 1403 dev_err(&pdev->dev, "failed to get clk_m: %d\n", err); 1404 goto put_padctl; 1405 } 1406 1407 tegra->pll_e = devm_clk_get(&pdev->dev, "pll_e"); 1408 if (IS_ERR(tegra->pll_e)) { 1409 err = PTR_ERR(tegra->pll_e); 1410 dev_err(&pdev->dev, "failed to get pll_e: %d\n", err); 1411 goto put_padctl; 1412 } 1413 1414 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) { 1415 tegra->host_rst = devm_reset_control_get(&pdev->dev, 1416 "xusb_host"); 1417 if (IS_ERR(tegra->host_rst)) { 1418 err = PTR_ERR(tegra->host_rst); 1419 dev_err(&pdev->dev, 1420 "failed to get xusb_host reset: %d\n", err); 1421 goto put_padctl; 1422 } 1423 1424 tegra->ss_rst = devm_reset_control_get(&pdev->dev, "xusb_ss"); 1425 if (IS_ERR(tegra->ss_rst)) { 1426 err = PTR_ERR(tegra->ss_rst); 1427 dev_err(&pdev->dev, "failed to get xusb_ss reset: %d\n", 1428 err); 1429 goto put_padctl; 1430 } 1431 1432 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBA, 1433 tegra->ss_clk, 1434 tegra->ss_rst); 1435 if (err) { 1436 dev_err(&pdev->dev, 1437 "failed to enable XUSBA domain: %d\n", err); 1438 goto put_padctl; 1439 } 1440 1441 err = tegra_powergate_sequence_power_up(TEGRA_POWERGATE_XUSBC, 1442 tegra->host_clk, 1443 tegra->host_rst); 1444 if (err) { 1445 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA); 1446 dev_err(&pdev->dev, 1447 "failed to enable XUSBC domain: %d\n", err); 1448 goto put_padctl; 1449 } 1450 } else { 1451 err = tegra_xusb_powerdomain_init(&pdev->dev, tegra); 1452 if (err) 1453 goto put_powerdomains; 1454 } 1455 1456 tegra->supplies = devm_kcalloc(&pdev->dev, tegra->soc->num_supplies, 1457 sizeof(*tegra->supplies), GFP_KERNEL); 1458 if (!tegra->supplies) { 1459 err = -ENOMEM; 1460 goto put_powerdomains; 1461 } 1462 1463 regulator_bulk_set_supply_names(tegra->supplies, 1464 tegra->soc->supply_names, 1465 tegra->soc->num_supplies); 1466 1467 err = devm_regulator_bulk_get(&pdev->dev, tegra->soc->num_supplies, 1468 tegra->supplies); 1469 if (err) { 1470 dev_err(&pdev->dev, "failed to get regulators: %d\n", err); 1471 goto put_powerdomains; 1472 } 1473 1474 for (i = 0; i < tegra->soc->num_types; i++) { 1475 if (!strncmp(tegra->soc->phy_types[i].name, "usb2", 4)) 1476 tegra->num_usb_phys = tegra->soc->phy_types[i].num; 1477 tegra->num_phys += tegra->soc->phy_types[i].num; 1478 } 1479 1480 tegra->phys = devm_kcalloc(&pdev->dev, tegra->num_phys, 1481 sizeof(*tegra->phys), GFP_KERNEL); 1482 if (!tegra->phys) { 1483 err = -ENOMEM; 1484 goto put_powerdomains; 1485 } 1486 1487 for (i = 0, k = 0; i < tegra->soc->num_types; i++) { 1488 char prop[8]; 1489 1490 for (j = 0; j < tegra->soc->phy_types[i].num; j++) { 1491 snprintf(prop, sizeof(prop), "%s-%d", 1492 tegra->soc->phy_types[i].name, j); 1493 1494 phy = devm_phy_optional_get(&pdev->dev, prop); 1495 if (IS_ERR(phy)) { 1496 dev_err(&pdev->dev, 1497 "failed to get PHY %s: %ld\n", prop, 1498 PTR_ERR(phy)); 1499 err = PTR_ERR(phy); 1500 goto put_powerdomains; 1501 } 1502 1503 tegra->phys[k++] = phy; 1504 } 1505 } 1506 1507 tegra->hcd = usb_create_hcd(&tegra_xhci_hc_driver, &pdev->dev, 1508 dev_name(&pdev->dev)); 1509 if (!tegra->hcd) { 1510 err = -ENOMEM; 1511 goto put_powerdomains; 1512 } 1513 1514 tegra->hcd->regs = tegra->regs; 1515 tegra->hcd->rsrc_start = regs->start; 1516 tegra->hcd->rsrc_len = resource_size(regs); 1517 1518 /* 1519 * This must happen after usb_create_hcd(), because usb_create_hcd() 1520 * will overwrite the drvdata of the device with the hcd it creates. 1521 */ 1522 platform_set_drvdata(pdev, tegra); 1523 1524 err = tegra_xusb_phy_enable(tegra); 1525 if (err < 0) { 1526 dev_err(&pdev->dev, "failed to enable PHYs: %d\n", err); 1527 goto put_hcd; 1528 } 1529 1530 /* 1531 * The XUSB Falcon microcontroller can only address 40 bits, so set 1532 * the DMA mask accordingly. 1533 */ 1534 err = dma_set_mask_and_coherent(tegra->dev, DMA_BIT_MASK(40)); 1535 if (err < 0) { 1536 dev_err(&pdev->dev, "failed to set DMA mask: %d\n", err); 1537 goto disable_phy; 1538 } 1539 1540 err = tegra_xusb_request_firmware(tegra); 1541 if (err < 0) { 1542 dev_err(&pdev->dev, "failed to request firmware: %d\n", err); 1543 goto disable_phy; 1544 } 1545 1546 pm_runtime_enable(&pdev->dev); 1547 1548 if (!pm_runtime_enabled(&pdev->dev)) 1549 err = tegra_xusb_runtime_resume(&pdev->dev); 1550 else 1551 err = pm_runtime_get_sync(&pdev->dev); 1552 1553 if (err < 0) { 1554 dev_err(&pdev->dev, "failed to enable device: %d\n", err); 1555 goto free_firmware; 1556 } 1557 1558 tegra_xusb_config(tegra); 1559 1560 err = tegra_xusb_load_firmware(tegra); 1561 if (err < 0) { 1562 dev_err(&pdev->dev, "failed to load firmware: %d\n", err); 1563 goto put_rpm; 1564 } 1565 1566 err = usb_add_hcd(tegra->hcd, tegra->xhci_irq, IRQF_SHARED); 1567 if (err < 0) { 1568 dev_err(&pdev->dev, "failed to add USB HCD: %d\n", err); 1569 goto put_rpm; 1570 } 1571 1572 device_wakeup_enable(tegra->hcd->self.controller); 1573 1574 xhci = hcd_to_xhci(tegra->hcd); 1575 1576 xhci->shared_hcd = usb_create_shared_hcd(&tegra_xhci_hc_driver, 1577 &pdev->dev, 1578 dev_name(&pdev->dev), 1579 tegra->hcd); 1580 if (!xhci->shared_hcd) { 1581 dev_err(&pdev->dev, "failed to create shared HCD\n"); 1582 err = -ENOMEM; 1583 goto remove_usb2; 1584 } 1585 1586 err = usb_add_hcd(xhci->shared_hcd, tegra->xhci_irq, IRQF_SHARED); 1587 if (err < 0) { 1588 dev_err(&pdev->dev, "failed to add shared HCD: %d\n", err); 1589 goto put_usb3; 1590 } 1591 1592 err = tegra_xusb_enable_firmware_messages(tegra); 1593 if (err < 0) { 1594 dev_err(&pdev->dev, "failed to enable messages: %d\n", err); 1595 goto remove_usb3; 1596 } 1597 1598 err = devm_request_threaded_irq(&pdev->dev, tegra->mbox_irq, 1599 tegra_xusb_mbox_irq, 1600 tegra_xusb_mbox_thread, 0, 1601 dev_name(&pdev->dev), tegra); 1602 if (err < 0) { 1603 dev_err(&pdev->dev, "failed to request IRQ: %d\n", err); 1604 goto remove_usb3; 1605 } 1606 1607 err = tegra_xusb_init_usb_phy(tegra); 1608 if (err < 0) { 1609 dev_err(&pdev->dev, "failed to init USB PHY: %d\n", err); 1610 goto remove_usb3; 1611 } 1612 1613 return 0; 1614 1615 remove_usb3: 1616 usb_remove_hcd(xhci->shared_hcd); 1617 put_usb3: 1618 usb_put_hcd(xhci->shared_hcd); 1619 remove_usb2: 1620 usb_remove_hcd(tegra->hcd); 1621 put_rpm: 1622 if (!pm_runtime_status_suspended(&pdev->dev)) 1623 tegra_xusb_runtime_suspend(&pdev->dev); 1624 put_hcd: 1625 usb_put_hcd(tegra->hcd); 1626 free_firmware: 1627 dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt, 1628 tegra->fw.phys); 1629 disable_phy: 1630 tegra_xusb_phy_disable(tegra); 1631 pm_runtime_disable(&pdev->dev); 1632 put_powerdomains: 1633 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) { 1634 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC); 1635 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA); 1636 } else { 1637 tegra_xusb_powerdomain_remove(&pdev->dev, tegra); 1638 } 1639 put_padctl: 1640 tegra_xusb_padctl_put(tegra->padctl); 1641 return err; 1642 } 1643 1644 static int tegra_xusb_remove(struct platform_device *pdev) 1645 { 1646 struct tegra_xusb *tegra = platform_get_drvdata(pdev); 1647 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1648 1649 tegra_xusb_deinit_usb_phy(tegra); 1650 1651 usb_remove_hcd(xhci->shared_hcd); 1652 usb_put_hcd(xhci->shared_hcd); 1653 xhci->shared_hcd = NULL; 1654 usb_remove_hcd(tegra->hcd); 1655 usb_put_hcd(tegra->hcd); 1656 1657 dma_free_coherent(&pdev->dev, tegra->fw.size, tegra->fw.virt, 1658 tegra->fw.phys); 1659 1660 pm_runtime_put_sync(&pdev->dev); 1661 pm_runtime_disable(&pdev->dev); 1662 1663 if (!of_property_read_bool(pdev->dev.of_node, "power-domains")) { 1664 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBC); 1665 tegra_powergate_power_off(TEGRA_POWERGATE_XUSBA); 1666 } else { 1667 tegra_xusb_powerdomain_remove(&pdev->dev, tegra); 1668 } 1669 1670 tegra_xusb_phy_disable(tegra); 1671 1672 tegra_xusb_padctl_put(tegra->padctl); 1673 1674 return 0; 1675 } 1676 1677 #ifdef CONFIG_PM_SLEEP 1678 static bool xhci_hub_ports_suspended(struct xhci_hub *hub) 1679 { 1680 struct device *dev = hub->hcd->self.controller; 1681 bool status = true; 1682 unsigned int i; 1683 u32 value; 1684 1685 for (i = 0; i < hub->num_ports; i++) { 1686 value = readl(hub->ports[i]->addr); 1687 if ((value & PORT_PE) == 0) 1688 continue; 1689 1690 if ((value & PORT_PLS_MASK) != XDEV_U3) { 1691 dev_info(dev, "%u-%u isn't suspended: %#010x\n", 1692 hub->hcd->self.busnum, i + 1, value); 1693 status = false; 1694 } 1695 } 1696 1697 return status; 1698 } 1699 1700 static int tegra_xusb_check_ports(struct tegra_xusb *tegra) 1701 { 1702 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1703 unsigned long flags; 1704 int err = 0; 1705 1706 spin_lock_irqsave(&xhci->lock, flags); 1707 1708 if (!xhci_hub_ports_suspended(&xhci->usb2_rhub) || 1709 !xhci_hub_ports_suspended(&xhci->usb3_rhub)) 1710 err = -EBUSY; 1711 1712 spin_unlock_irqrestore(&xhci->lock, flags); 1713 1714 return err; 1715 } 1716 1717 static void tegra_xusb_save_context(struct tegra_xusb *tegra) 1718 { 1719 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 1720 struct tegra_xusb_context *ctx = &tegra->context; 1721 unsigned int i; 1722 1723 if (soc->ipfs.num_offsets > 0) { 1724 for (i = 0; i < soc->ipfs.num_offsets; i++) 1725 ctx->ipfs[i] = ipfs_readl(tegra, soc->ipfs.offsets[i]); 1726 } 1727 1728 if (soc->fpci.num_offsets > 0) { 1729 for (i = 0; i < soc->fpci.num_offsets; i++) 1730 ctx->fpci[i] = fpci_readl(tegra, soc->fpci.offsets[i]); 1731 } 1732 } 1733 1734 static void tegra_xusb_restore_context(struct tegra_xusb *tegra) 1735 { 1736 const struct tegra_xusb_context_soc *soc = tegra->soc->context; 1737 struct tegra_xusb_context *ctx = &tegra->context; 1738 unsigned int i; 1739 1740 if (soc->fpci.num_offsets > 0) { 1741 for (i = 0; i < soc->fpci.num_offsets; i++) 1742 fpci_writel(tegra, ctx->fpci[i], soc->fpci.offsets[i]); 1743 } 1744 1745 if (soc->ipfs.num_offsets > 0) { 1746 for (i = 0; i < soc->ipfs.num_offsets; i++) 1747 ipfs_writel(tegra, ctx->ipfs[i], soc->ipfs.offsets[i]); 1748 } 1749 } 1750 1751 static int tegra_xusb_enter_elpg(struct tegra_xusb *tegra, bool wakeup) 1752 { 1753 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1754 int err; 1755 1756 err = tegra_xusb_check_ports(tegra); 1757 if (err < 0) { 1758 dev_err(tegra->dev, "not all ports suspended: %d\n", err); 1759 return err; 1760 } 1761 1762 err = xhci_suspend(xhci, wakeup); 1763 if (err < 0) { 1764 dev_err(tegra->dev, "failed to suspend XHCI: %d\n", err); 1765 return err; 1766 } 1767 1768 tegra_xusb_save_context(tegra); 1769 tegra_xusb_phy_disable(tegra); 1770 tegra_xusb_clk_disable(tegra); 1771 1772 return 0; 1773 } 1774 1775 static int tegra_xusb_exit_elpg(struct tegra_xusb *tegra, bool wakeup) 1776 { 1777 struct xhci_hcd *xhci = hcd_to_xhci(tegra->hcd); 1778 int err; 1779 1780 err = tegra_xusb_clk_enable(tegra); 1781 if (err < 0) { 1782 dev_err(tegra->dev, "failed to enable clocks: %d\n", err); 1783 return err; 1784 } 1785 1786 err = tegra_xusb_phy_enable(tegra); 1787 if (err < 0) { 1788 dev_err(tegra->dev, "failed to enable PHYs: %d\n", err); 1789 goto disable_clk; 1790 } 1791 1792 tegra_xusb_config(tegra); 1793 tegra_xusb_restore_context(tegra); 1794 1795 err = tegra_xusb_load_firmware(tegra); 1796 if (err < 0) { 1797 dev_err(tegra->dev, "failed to load firmware: %d\n", err); 1798 goto disable_phy; 1799 } 1800 1801 err = __tegra_xusb_enable_firmware_messages(tegra); 1802 if (err < 0) { 1803 dev_err(tegra->dev, "failed to enable messages: %d\n", err); 1804 goto disable_phy; 1805 } 1806 1807 err = xhci_resume(xhci, true); 1808 if (err < 0) { 1809 dev_err(tegra->dev, "failed to resume XHCI: %d\n", err); 1810 goto disable_phy; 1811 } 1812 1813 return 0; 1814 1815 disable_phy: 1816 tegra_xusb_phy_disable(tegra); 1817 disable_clk: 1818 tegra_xusb_clk_disable(tegra); 1819 return err; 1820 } 1821 1822 static int tegra_xusb_suspend(struct device *dev) 1823 { 1824 struct tegra_xusb *tegra = dev_get_drvdata(dev); 1825 bool wakeup = device_may_wakeup(dev); 1826 int err; 1827 1828 synchronize_irq(tegra->mbox_irq); 1829 1830 mutex_lock(&tegra->lock); 1831 err = tegra_xusb_enter_elpg(tegra, wakeup); 1832 mutex_unlock(&tegra->lock); 1833 1834 return err; 1835 } 1836 1837 static int tegra_xusb_resume(struct device *dev) 1838 { 1839 struct tegra_xusb *tegra = dev_get_drvdata(dev); 1840 bool wakeup = device_may_wakeup(dev); 1841 int err; 1842 1843 mutex_lock(&tegra->lock); 1844 err = tegra_xusb_exit_elpg(tegra, wakeup); 1845 mutex_unlock(&tegra->lock); 1846 1847 return err; 1848 } 1849 #endif 1850 1851 static const struct dev_pm_ops tegra_xusb_pm_ops = { 1852 SET_RUNTIME_PM_OPS(tegra_xusb_runtime_suspend, 1853 tegra_xusb_runtime_resume, NULL) 1854 SET_SYSTEM_SLEEP_PM_OPS(tegra_xusb_suspend, tegra_xusb_resume) 1855 }; 1856 1857 static const char * const tegra124_supply_names[] = { 1858 "avddio-pex", 1859 "dvddio-pex", 1860 "avdd-usb", 1861 "hvdd-usb-ss", 1862 }; 1863 1864 static const struct tegra_xusb_phy_type tegra124_phy_types[] = { 1865 { .name = "usb3", .num = 2, }, 1866 { .name = "usb2", .num = 3, }, 1867 { .name = "hsic", .num = 2, }, 1868 }; 1869 1870 static const unsigned int tegra124_xusb_context_ipfs[] = { 1871 IPFS_XUSB_HOST_MSI_BAR_SZ_0, 1872 IPFS_XUSB_HOST_MSI_AXI_BAR_ST_0, 1873 IPFS_XUSB_HOST_MSI_FPCI_BAR_ST_0, 1874 IPFS_XUSB_HOST_MSI_VEC0_0, 1875 IPFS_XUSB_HOST_MSI_EN_VEC0_0, 1876 IPFS_XUSB_HOST_FPCI_ERROR_MASKS_0, 1877 IPFS_XUSB_HOST_INTR_MASK_0, 1878 IPFS_XUSB_HOST_INTR_ENABLE_0, 1879 IPFS_XUSB_HOST_UFPCI_CONFIG_0, 1880 IPFS_XUSB_HOST_CLKGATE_HYSTERESIS_0, 1881 IPFS_XUSB_HOST_MCCIF_FIFOCTRL_0, 1882 }; 1883 1884 static const unsigned int tegra124_xusb_context_fpci[] = { 1885 XUSB_CFG_ARU_CONTEXT_HS_PLS, 1886 XUSB_CFG_ARU_CONTEXT_FS_PLS, 1887 XUSB_CFG_ARU_CONTEXT_HSFS_SPEED, 1888 XUSB_CFG_ARU_CONTEXT_HSFS_PP, 1889 XUSB_CFG_ARU_CONTEXT, 1890 XUSB_CFG_AXI_CFG, 1891 XUSB_CFG_24, 1892 XUSB_CFG_16, 1893 }; 1894 1895 static const struct tegra_xusb_context_soc tegra124_xusb_context = { 1896 .ipfs = { 1897 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_ipfs), 1898 .offsets = tegra124_xusb_context_ipfs, 1899 }, 1900 .fpci = { 1901 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci), 1902 .offsets = tegra124_xusb_context_fpci, 1903 }, 1904 }; 1905 1906 static const struct tegra_xusb_soc tegra124_soc = { 1907 .firmware = "nvidia/tegra124/xusb.bin", 1908 .supply_names = tegra124_supply_names, 1909 .num_supplies = ARRAY_SIZE(tegra124_supply_names), 1910 .phy_types = tegra124_phy_types, 1911 .num_types = ARRAY_SIZE(tegra124_phy_types), 1912 .context = &tegra124_xusb_context, 1913 .ports = { 1914 .usb2 = { .offset = 4, .count = 4, }, 1915 .hsic = { .offset = 6, .count = 2, }, 1916 .usb3 = { .offset = 0, .count = 2, }, 1917 }, 1918 .scale_ss_clock = true, 1919 .has_ipfs = true, 1920 .otg_reset_sspi = false, 1921 .mbox = { 1922 .cmd = 0xe4, 1923 .data_in = 0xe8, 1924 .data_out = 0xec, 1925 .owner = 0xf0, 1926 }, 1927 }; 1928 MODULE_FIRMWARE("nvidia/tegra124/xusb.bin"); 1929 1930 static const char * const tegra210_supply_names[] = { 1931 "dvddio-pex", 1932 "hvddio-pex", 1933 "avdd-usb", 1934 }; 1935 1936 static const struct tegra_xusb_phy_type tegra210_phy_types[] = { 1937 { .name = "usb3", .num = 4, }, 1938 { .name = "usb2", .num = 4, }, 1939 { .name = "hsic", .num = 1, }, 1940 }; 1941 1942 static const struct tegra_xusb_soc tegra210_soc = { 1943 .firmware = "nvidia/tegra210/xusb.bin", 1944 .supply_names = tegra210_supply_names, 1945 .num_supplies = ARRAY_SIZE(tegra210_supply_names), 1946 .phy_types = tegra210_phy_types, 1947 .num_types = ARRAY_SIZE(tegra210_phy_types), 1948 .context = &tegra124_xusb_context, 1949 .ports = { 1950 .usb2 = { .offset = 4, .count = 4, }, 1951 .hsic = { .offset = 8, .count = 1, }, 1952 .usb3 = { .offset = 0, .count = 4, }, 1953 }, 1954 .scale_ss_clock = false, 1955 .has_ipfs = true, 1956 .otg_reset_sspi = true, 1957 .mbox = { 1958 .cmd = 0xe4, 1959 .data_in = 0xe8, 1960 .data_out = 0xec, 1961 .owner = 0xf0, 1962 }, 1963 }; 1964 MODULE_FIRMWARE("nvidia/tegra210/xusb.bin"); 1965 1966 static const char * const tegra186_supply_names[] = { 1967 }; 1968 MODULE_FIRMWARE("nvidia/tegra186/xusb.bin"); 1969 1970 static const struct tegra_xusb_phy_type tegra186_phy_types[] = { 1971 { .name = "usb3", .num = 3, }, 1972 { .name = "usb2", .num = 3, }, 1973 { .name = "hsic", .num = 1, }, 1974 }; 1975 1976 static const struct tegra_xusb_context_soc tegra186_xusb_context = { 1977 .fpci = { 1978 .num_offsets = ARRAY_SIZE(tegra124_xusb_context_fpci), 1979 .offsets = tegra124_xusb_context_fpci, 1980 }, 1981 }; 1982 1983 static const struct tegra_xusb_soc tegra186_soc = { 1984 .firmware = "nvidia/tegra186/xusb.bin", 1985 .supply_names = tegra186_supply_names, 1986 .num_supplies = ARRAY_SIZE(tegra186_supply_names), 1987 .phy_types = tegra186_phy_types, 1988 .num_types = ARRAY_SIZE(tegra186_phy_types), 1989 .context = &tegra186_xusb_context, 1990 .ports = { 1991 .usb3 = { .offset = 0, .count = 3, }, 1992 .usb2 = { .offset = 3, .count = 3, }, 1993 .hsic = { .offset = 6, .count = 1, }, 1994 }, 1995 .scale_ss_clock = false, 1996 .has_ipfs = false, 1997 .otg_reset_sspi = false, 1998 .mbox = { 1999 .cmd = 0xe4, 2000 .data_in = 0xe8, 2001 .data_out = 0xec, 2002 .owner = 0xf0, 2003 }, 2004 .lpm_support = true, 2005 }; 2006 2007 static const char * const tegra194_supply_names[] = { 2008 }; 2009 2010 static const struct tegra_xusb_phy_type tegra194_phy_types[] = { 2011 { .name = "usb3", .num = 4, }, 2012 { .name = "usb2", .num = 4, }, 2013 }; 2014 2015 static const struct tegra_xusb_soc tegra194_soc = { 2016 .firmware = "nvidia/tegra194/xusb.bin", 2017 .supply_names = tegra194_supply_names, 2018 .num_supplies = ARRAY_SIZE(tegra194_supply_names), 2019 .phy_types = tegra194_phy_types, 2020 .num_types = ARRAY_SIZE(tegra194_phy_types), 2021 .context = &tegra186_xusb_context, 2022 .ports = { 2023 .usb3 = { .offset = 0, .count = 4, }, 2024 .usb2 = { .offset = 4, .count = 4, }, 2025 }, 2026 .scale_ss_clock = false, 2027 .has_ipfs = false, 2028 .otg_reset_sspi = false, 2029 .mbox = { 2030 .cmd = 0x68, 2031 .data_in = 0x6c, 2032 .data_out = 0x70, 2033 .owner = 0x74, 2034 }, 2035 .lpm_support = true, 2036 }; 2037 MODULE_FIRMWARE("nvidia/tegra194/xusb.bin"); 2038 2039 static const struct of_device_id tegra_xusb_of_match[] = { 2040 { .compatible = "nvidia,tegra124-xusb", .data = &tegra124_soc }, 2041 { .compatible = "nvidia,tegra210-xusb", .data = &tegra210_soc }, 2042 { .compatible = "nvidia,tegra186-xusb", .data = &tegra186_soc }, 2043 { .compatible = "nvidia,tegra194-xusb", .data = &tegra194_soc }, 2044 { }, 2045 }; 2046 MODULE_DEVICE_TABLE(of, tegra_xusb_of_match); 2047 2048 static struct platform_driver tegra_xusb_driver = { 2049 .probe = tegra_xusb_probe, 2050 .remove = tegra_xusb_remove, 2051 .driver = { 2052 .name = "tegra-xusb", 2053 .pm = &tegra_xusb_pm_ops, 2054 .of_match_table = tegra_xusb_of_match, 2055 }, 2056 }; 2057 2058 static void tegra_xhci_quirks(struct device *dev, struct xhci_hcd *xhci) 2059 { 2060 struct tegra_xusb *tegra = dev_get_drvdata(dev); 2061 2062 xhci->quirks |= XHCI_PLAT; 2063 if (tegra && tegra->soc->lpm_support) 2064 xhci->quirks |= XHCI_LPM_SUPPORT; 2065 } 2066 2067 static int tegra_xhci_setup(struct usb_hcd *hcd) 2068 { 2069 return xhci_gen_setup(hcd, tegra_xhci_quirks); 2070 } 2071 2072 static const struct xhci_driver_overrides tegra_xhci_overrides __initconst = { 2073 .reset = tegra_xhci_setup, 2074 }; 2075 2076 static int __init tegra_xusb_init(void) 2077 { 2078 xhci_init_driver(&tegra_xhci_hc_driver, &tegra_xhci_overrides); 2079 2080 return platform_driver_register(&tegra_xusb_driver); 2081 } 2082 module_init(tegra_xusb_init); 2083 2084 static void __exit tegra_xusb_exit(void) 2085 { 2086 platform_driver_unregister(&tegra_xusb_driver); 2087 } 2088 module_exit(tegra_xusb_exit); 2089 2090 MODULE_AUTHOR("Andrew Bresticker <abrestic@chromium.org>"); 2091 MODULE_DESCRIPTION("NVIDIA Tegra XUSB xHCI host-controller driver"); 2092 MODULE_LICENSE("GPL v2"); 2093