1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * Driver for the Aardvark PCIe controller, used on Marvell Armada 4 * 3700. 5 * 6 * Copyright (C) 2016 Marvell 7 * 8 * Author: Hezi Shahmoon <hezi.shahmoon@marvell.com> 9 */ 10 11 #include <linux/delay.h> 12 #include <linux/gpio/consumer.h> 13 #include <linux/interrupt.h> 14 #include <linux/irq.h> 15 #include <linux/irqdomain.h> 16 #include <linux/kernel.h> 17 #include <linux/module.h> 18 #include <linux/pci.h> 19 #include <linux/pci-ecam.h> 20 #include <linux/init.h> 21 #include <linux/phy/phy.h> 22 #include <linux/platform_device.h> 23 #include <linux/msi.h> 24 #include <linux/of_address.h> 25 #include <linux/of_gpio.h> 26 #include <linux/of_pci.h> 27 28 #include "../pci.h" 29 #include "../pci-bridge-emul.h" 30 31 /* PCIe core registers */ 32 #define PCIE_CORE_DEV_ID_REG 0x0 33 #define PCIE_CORE_CMD_STATUS_REG 0x4 34 #define PCIE_CORE_CMD_IO_ACCESS_EN BIT(0) 35 #define PCIE_CORE_CMD_MEM_ACCESS_EN BIT(1) 36 #define PCIE_CORE_CMD_MEM_IO_REQ_EN BIT(2) 37 #define PCIE_CORE_DEV_REV_REG 0x8 38 #define PCIE_CORE_PCIEXP_CAP 0xc0 39 #define PCIE_CORE_ERR_CAPCTL_REG 0x118 40 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) 41 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) 42 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7) 43 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8) 44 #define PCIE_CORE_INT_A_ASSERT_ENABLE 1 45 #define PCIE_CORE_INT_B_ASSERT_ENABLE 2 46 #define PCIE_CORE_INT_C_ASSERT_ENABLE 3 47 #define PCIE_CORE_INT_D_ASSERT_ENABLE 4 48 /* PIO registers base address and register offsets */ 49 #define PIO_BASE_ADDR 0x4000 50 #define PIO_CTRL (PIO_BASE_ADDR + 0x0) 51 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0) 52 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) 53 #define PIO_STAT (PIO_BASE_ADDR + 0x4) 54 #define PIO_COMPLETION_STATUS_SHIFT 7 55 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) 56 #define PIO_COMPLETION_STATUS_OK 0 57 #define PIO_COMPLETION_STATUS_UR 1 58 #define PIO_COMPLETION_STATUS_CRS 2 59 #define PIO_COMPLETION_STATUS_CA 4 60 #define PIO_NON_POSTED_REQ BIT(0) 61 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) 62 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) 63 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) 64 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) 65 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) 66 #define PIO_START (PIO_BASE_ADDR + 0x1c) 67 #define PIO_ISR (PIO_BASE_ADDR + 0x20) 68 #define PIO_ISRM (PIO_BASE_ADDR + 0x24) 69 70 /* Aardvark Control registers */ 71 #define CONTROL_BASE_ADDR 0x4800 72 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) 73 #define PCIE_GEN_SEL_MSK 0x3 74 #define PCIE_GEN_SEL_SHIFT 0x0 75 #define SPEED_GEN_1 0 76 #define SPEED_GEN_2 1 77 #define SPEED_GEN_3 2 78 #define IS_RC_MSK 1 79 #define IS_RC_SHIFT 2 80 #define LANE_CNT_MSK 0x18 81 #define LANE_CNT_SHIFT 0x3 82 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) 83 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) 84 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) 85 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) 86 #define LINK_TRAINING_EN BIT(6) 87 #define LEGACY_INTA BIT(28) 88 #define LEGACY_INTB BIT(29) 89 #define LEGACY_INTC BIT(30) 90 #define LEGACY_INTD BIT(31) 91 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4) 92 #define HOT_RESET_GEN BIT(0) 93 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) 94 #define PCIE_CORE_CTRL2_RESERVED 0x7 95 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) 96 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) 97 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6) 98 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10) 99 #define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14) 100 #define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1) 101 #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30) 102 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40) 103 #define PCIE_MSG_PM_PME_MASK BIT(7) 104 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44) 105 #define PCIE_ISR0_MSI_INT_PENDING BIT(24) 106 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val)) 107 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val)) 108 #define PCIE_ISR0_ALL_MASK GENMASK(26, 0) 109 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48) 110 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C) 111 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4) 112 #define PCIE_ISR1_FLUSH BIT(5) 113 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val)) 114 #define PCIE_ISR1_ALL_MASK GENMASK(11, 4) 115 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50) 116 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54) 117 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58) 118 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C) 119 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C) 120 121 /* LMI registers base address and register offsets */ 122 #define LMI_BASE_ADDR 0x6000 123 #define CFG_REG (LMI_BASE_ADDR + 0x0) 124 #define LTSSM_SHIFT 24 125 #define LTSSM_MASK 0x3f 126 #define LTSSM_L0 0x10 127 #define RC_BAR_CONFIG 0x300 128 129 /* PCIe core controller registers */ 130 #define CTRL_CORE_BASE_ADDR 0x18000 131 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) 132 #define CTRL_MODE_SHIFT 0x0 133 #define CTRL_MODE_MASK 0x1 134 #define PCIE_CORE_MODE_DIRECT 0x0 135 #define PCIE_CORE_MODE_COMMAND 0x1 136 137 /* PCIe Central Interrupts Registers */ 138 #define CENTRAL_INT_BASE_ADDR 0x1b000 139 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0) 140 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4) 141 #define PCIE_IRQ_CMDQ_INT BIT(0) 142 #define PCIE_IRQ_MSI_STATUS_INT BIT(1) 143 #define PCIE_IRQ_CMD_SENT_DONE BIT(3) 144 #define PCIE_IRQ_DMA_INT BIT(4) 145 #define PCIE_IRQ_IB_DXFERDONE BIT(5) 146 #define PCIE_IRQ_OB_DXFERDONE BIT(6) 147 #define PCIE_IRQ_OB_RXFERDONE BIT(7) 148 #define PCIE_IRQ_COMPQ_INT BIT(12) 149 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13) 150 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14) 151 #define PCIE_IRQ_CORE_INT BIT(16) 152 #define PCIE_IRQ_CORE_INT_PIO BIT(17) 153 #define PCIE_IRQ_DPMU_INT BIT(18) 154 #define PCIE_IRQ_PCIE_MIS_INT BIT(19) 155 #define PCIE_IRQ_MSI_INT1_DET BIT(20) 156 #define PCIE_IRQ_MSI_INT2_DET BIT(21) 157 #define PCIE_IRQ_RC_DBELL_DET BIT(22) 158 #define PCIE_IRQ_EP_STATUS BIT(23) 159 #define PCIE_IRQ_ALL_MASK 0xfff0fb 160 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT 161 162 /* Transaction types */ 163 #define PCIE_CONFIG_RD_TYPE0 0x8 164 #define PCIE_CONFIG_RD_TYPE1 0x9 165 #define PCIE_CONFIG_WR_TYPE0 0xa 166 #define PCIE_CONFIG_WR_TYPE1 0xb 167 168 #define PIO_RETRY_CNT 500 169 #define PIO_RETRY_DELAY 2 /* 2 us*/ 170 171 #define LINK_WAIT_MAX_RETRIES 10 172 #define LINK_WAIT_USLEEP_MIN 90000 173 #define LINK_WAIT_USLEEP_MAX 100000 174 #define RETRAIN_WAIT_MAX_RETRIES 10 175 #define RETRAIN_WAIT_USLEEP_US 2000 176 177 #define MSI_IRQ_NUM 32 178 179 struct advk_pcie { 180 struct platform_device *pdev; 181 void __iomem *base; 182 struct irq_domain *irq_domain; 183 struct irq_chip irq_chip; 184 struct irq_domain *msi_domain; 185 struct irq_domain *msi_inner_domain; 186 struct irq_chip msi_bottom_irq_chip; 187 struct irq_chip msi_irq_chip; 188 struct msi_domain_info msi_domain_info; 189 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM); 190 struct mutex msi_used_lock; 191 u16 msi_msg; 192 int link_gen; 193 struct pci_bridge_emul bridge; 194 struct gpio_desc *reset_gpio; 195 struct phy *phy; 196 }; 197 198 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg) 199 { 200 writel(val, pcie->base + reg); 201 } 202 203 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg) 204 { 205 return readl(pcie->base + reg); 206 } 207 208 static inline u16 advk_read16(struct advk_pcie *pcie, u64 reg) 209 { 210 return advk_readl(pcie, (reg & ~0x3)) >> ((reg & 0x3) * 8); 211 } 212 213 static int advk_pcie_link_up(struct advk_pcie *pcie) 214 { 215 u32 val, ltssm_state; 216 217 val = advk_readl(pcie, CFG_REG); 218 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; 219 return ltssm_state >= LTSSM_L0; 220 } 221 222 static int advk_pcie_wait_for_link(struct advk_pcie *pcie) 223 { 224 int retries; 225 226 /* check if the link is up or not */ 227 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) { 228 if (advk_pcie_link_up(pcie)) 229 return 0; 230 231 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX); 232 } 233 234 return -ETIMEDOUT; 235 } 236 237 static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie) 238 { 239 size_t retries; 240 241 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) { 242 if (!advk_pcie_link_up(pcie)) 243 break; 244 udelay(RETRAIN_WAIT_USLEEP_US); 245 } 246 } 247 248 static void advk_pcie_issue_perst(struct advk_pcie *pcie) 249 { 250 u32 reg; 251 252 if (!pcie->reset_gpio) 253 return; 254 255 /* 256 * As required by PCI Express spec (PCI Express Base Specification, REV. 257 * 4.0 PCI Express, February 19 2014, 6.6.1 Conventional Reset) a delay 258 * for at least 100ms after de-asserting PERST# signal is needed before 259 * link training is enabled. So ensure that link training is disabled 260 * prior de-asserting PERST# signal to fulfill that PCI Express spec 261 * requirement. 262 */ 263 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 264 reg &= ~LINK_TRAINING_EN; 265 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 266 267 /* 10ms delay is needed for some cards */ 268 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n"); 269 gpiod_set_value_cansleep(pcie->reset_gpio, 1); 270 usleep_range(10000, 11000); 271 gpiod_set_value_cansleep(pcie->reset_gpio, 0); 272 } 273 274 static int advk_pcie_train_at_gen(struct advk_pcie *pcie, int gen) 275 { 276 int ret, neg_gen; 277 u32 reg; 278 279 /* Setup link speed */ 280 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 281 reg &= ~PCIE_GEN_SEL_MSK; 282 if (gen == 3) 283 reg |= SPEED_GEN_3; 284 else if (gen == 2) 285 reg |= SPEED_GEN_2; 286 else 287 reg |= SPEED_GEN_1; 288 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 289 290 /* 291 * Enable link training. This is not needed in every call to this 292 * function, just once suffices, but it does not break anything either. 293 */ 294 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 295 reg |= LINK_TRAINING_EN; 296 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 297 298 /* 299 * Start link training immediately after enabling it. 300 * This solves problems for some buggy cards. 301 */ 302 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL); 303 reg |= PCI_EXP_LNKCTL_RL; 304 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL); 305 306 ret = advk_pcie_wait_for_link(pcie); 307 if (ret) 308 return ret; 309 310 reg = advk_read16(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKSTA); 311 neg_gen = reg & PCI_EXP_LNKSTA_CLS; 312 313 return neg_gen; 314 } 315 316 static void advk_pcie_train_link(struct advk_pcie *pcie) 317 { 318 struct device *dev = &pcie->pdev->dev; 319 int neg_gen = -1, gen; 320 321 /* 322 * Reset PCIe card via PERST# signal. Some cards are not detected 323 * during link training when they are in some non-initial state. 324 */ 325 advk_pcie_issue_perst(pcie); 326 327 /* 328 * PERST# signal could have been asserted by pinctrl subsystem before 329 * probe() callback has been called or issued explicitly by reset gpio 330 * function advk_pcie_issue_perst(), making the endpoint going into 331 * fundamental reset. As required by PCI Express spec a delay for at 332 * least 100ms after such a reset before link training is needed. 333 */ 334 msleep(PCI_PM_D3COLD_WAIT); 335 336 /* 337 * Try link training at link gen specified by device tree property 338 * 'max-link-speed'. If this fails, iteratively train at lower gen. 339 */ 340 for (gen = pcie->link_gen; gen > 0; --gen) { 341 neg_gen = advk_pcie_train_at_gen(pcie, gen); 342 if (neg_gen > 0) 343 break; 344 } 345 346 if (neg_gen < 0) 347 goto err; 348 349 /* 350 * After successful training if negotiated gen is lower than requested, 351 * train again on negotiated gen. This solves some stability issues for 352 * some buggy gen1 cards. 353 */ 354 if (neg_gen < gen) { 355 gen = neg_gen; 356 neg_gen = advk_pcie_train_at_gen(pcie, gen); 357 } 358 359 if (neg_gen == gen) { 360 dev_info(dev, "link up at gen %i\n", gen); 361 return; 362 } 363 364 err: 365 dev_err(dev, "link never came up\n"); 366 } 367 368 static void advk_pcie_setup_hw(struct advk_pcie *pcie) 369 { 370 u32 reg; 371 372 /* Enable TX */ 373 reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG); 374 reg |= PCIE_CORE_REF_CLK_TX_ENABLE; 375 advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG); 376 377 /* Set to Direct mode */ 378 reg = advk_readl(pcie, CTRL_CONFIG_REG); 379 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); 380 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); 381 advk_writel(pcie, reg, CTRL_CONFIG_REG); 382 383 /* Set PCI global control register to RC mode */ 384 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 385 reg |= (IS_RC_MSK << IS_RC_SHIFT); 386 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 387 388 /* Set Advanced Error Capabilities and Control PF0 register */ 389 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | 390 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | 391 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK | 392 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV; 393 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); 394 395 /* Set PCIe Device Control register */ 396 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 397 reg &= ~PCI_EXP_DEVCTL_RELAX_EN; 398 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN; 399 reg &= ~PCI_EXP_DEVCTL_READRQ; 400 reg |= PCI_EXP_DEVCTL_PAYLOAD; /* Set max payload size */ 401 reg |= PCI_EXP_DEVCTL_READRQ_512B; 402 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 403 404 /* Program PCIe Control 2 to disable strict ordering */ 405 reg = PCIE_CORE_CTRL2_RESERVED | 406 PCIE_CORE_CTRL2_TD_ENABLE; 407 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 408 409 /* Set lane X1 */ 410 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 411 reg &= ~LANE_CNT_MSK; 412 reg |= LANE_COUNT_1; 413 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 414 415 /* Enable MSI */ 416 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 417 reg |= PCIE_CORE_CTRL2_MSI_ENABLE; 418 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 419 420 /* Clear all interrupts */ 421 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 422 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 423 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 424 425 /* Disable All ISR0/1 Sources */ 426 reg = PCIE_ISR0_ALL_MASK; 427 reg &= ~PCIE_ISR0_MSI_INT_PENDING; 428 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 429 430 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 431 432 /* Unmask all MSIs */ 433 advk_writel(pcie, 0, PCIE_MSI_MASK_REG); 434 435 /* Enable summary interrupt for GIC SPI source */ 436 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK); 437 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG); 438 439 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 440 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE; 441 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 442 443 /* Bypass the address window mapping for PIO */ 444 reg = advk_readl(pcie, PIO_CTRL); 445 reg |= PIO_CTRL_ADDR_WIN_DISABLE; 446 advk_writel(pcie, reg, PIO_CTRL); 447 448 advk_pcie_train_link(pcie); 449 450 /* 451 * FIXME: The following register update is suspicious. This register is 452 * applicable only when the PCI controller is configured for Endpoint 453 * mode, not as a Root Complex. But apparently when this code is 454 * removed, some cards stop working. This should be investigated and 455 * a comment explaining this should be put here. 456 */ 457 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 458 reg |= PCIE_CORE_CMD_MEM_ACCESS_EN | 459 PCIE_CORE_CMD_IO_ACCESS_EN | 460 PCIE_CORE_CMD_MEM_IO_REQ_EN; 461 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); 462 } 463 464 static void advk_pcie_check_pio_status(struct advk_pcie *pcie) 465 { 466 struct device *dev = &pcie->pdev->dev; 467 u32 reg; 468 unsigned int status; 469 char *strcomp_status, *str_posted; 470 471 reg = advk_readl(pcie, PIO_STAT); 472 status = (reg & PIO_COMPLETION_STATUS_MASK) >> 473 PIO_COMPLETION_STATUS_SHIFT; 474 475 if (!status) 476 return; 477 478 switch (status) { 479 case PIO_COMPLETION_STATUS_UR: 480 strcomp_status = "UR"; 481 break; 482 case PIO_COMPLETION_STATUS_CRS: 483 strcomp_status = "CRS"; 484 break; 485 case PIO_COMPLETION_STATUS_CA: 486 strcomp_status = "CA"; 487 break; 488 default: 489 strcomp_status = "Unknown"; 490 break; 491 } 492 493 if (reg & PIO_NON_POSTED_REQ) 494 str_posted = "Non-posted"; 495 else 496 str_posted = "Posted"; 497 498 dev_err(dev, "%s PIO Response Status: %s, %#x @ %#x\n", 499 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS)); 500 } 501 502 static int advk_pcie_wait_pio(struct advk_pcie *pcie) 503 { 504 struct device *dev = &pcie->pdev->dev; 505 int i; 506 507 for (i = 0; i < PIO_RETRY_CNT; i++) { 508 u32 start, isr; 509 510 start = advk_readl(pcie, PIO_START); 511 isr = advk_readl(pcie, PIO_ISR); 512 if (!start && isr) 513 return 0; 514 udelay(PIO_RETRY_DELAY); 515 } 516 517 dev_err(dev, "PIO read/write transfer time out\n"); 518 return -ETIMEDOUT; 519 } 520 521 522 static pci_bridge_emul_read_status_t 523 advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge, 524 int reg, u32 *value) 525 { 526 struct advk_pcie *pcie = bridge->data; 527 528 529 switch (reg) { 530 case PCI_EXP_SLTCTL: 531 *value = PCI_EXP_SLTSTA_PDS << 16; 532 return PCI_BRIDGE_EMUL_HANDLED; 533 534 case PCI_EXP_RTCTL: { 535 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG); 536 *value = (val & PCIE_MSG_PM_PME_MASK) ? 0 : PCI_EXP_RTCTL_PMEIE; 537 return PCI_BRIDGE_EMUL_HANDLED; 538 } 539 540 case PCI_EXP_RTSTA: { 541 u32 isr0 = advk_readl(pcie, PCIE_ISR0_REG); 542 u32 msglog = advk_readl(pcie, PCIE_MSG_LOG_REG); 543 *value = (isr0 & PCIE_MSG_PM_PME_MASK) << 16 | (msglog >> 16); 544 return PCI_BRIDGE_EMUL_HANDLED; 545 } 546 547 case PCI_EXP_LNKCTL: { 548 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */ 549 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) & 550 ~(PCI_EXP_LNKSTA_LT << 16); 551 if (!advk_pcie_link_up(pcie)) 552 val |= (PCI_EXP_LNKSTA_LT << 16); 553 *value = val; 554 return PCI_BRIDGE_EMUL_HANDLED; 555 } 556 557 case PCI_CAP_LIST_ID: 558 case PCI_EXP_DEVCAP: 559 case PCI_EXP_DEVCTL: 560 case PCI_EXP_LNKCAP: 561 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 562 return PCI_BRIDGE_EMUL_HANDLED; 563 default: 564 return PCI_BRIDGE_EMUL_NOT_HANDLED; 565 } 566 567 } 568 569 static void 570 advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge, 571 int reg, u32 old, u32 new, u32 mask) 572 { 573 struct advk_pcie *pcie = bridge->data; 574 575 switch (reg) { 576 case PCI_EXP_DEVCTL: 577 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 578 break; 579 580 case PCI_EXP_LNKCTL: 581 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 582 if (new & PCI_EXP_LNKCTL_RL) 583 advk_pcie_wait_for_retrain(pcie); 584 break; 585 586 case PCI_EXP_RTCTL: { 587 /* Only mask/unmask PME interrupt */ 588 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG) & 589 ~PCIE_MSG_PM_PME_MASK; 590 if ((new & PCI_EXP_RTCTL_PMEIE) == 0) 591 val |= PCIE_MSG_PM_PME_MASK; 592 advk_writel(pcie, val, PCIE_ISR0_MASK_REG); 593 break; 594 } 595 596 case PCI_EXP_RTSTA: 597 new = (new & PCI_EXP_RTSTA_PME) >> 9; 598 advk_writel(pcie, new, PCIE_ISR0_REG); 599 break; 600 601 default: 602 break; 603 } 604 } 605 606 static struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = { 607 .read_pcie = advk_pci_bridge_emul_pcie_conf_read, 608 .write_pcie = advk_pci_bridge_emul_pcie_conf_write, 609 }; 610 611 /* 612 * Initialize the configuration space of the PCI-to-PCI bridge 613 * associated with the given PCIe interface. 614 */ 615 static int advk_sw_pci_bridge_init(struct advk_pcie *pcie) 616 { 617 struct pci_bridge_emul *bridge = &pcie->bridge; 618 619 bridge->conf.vendor = 620 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff); 621 bridge->conf.device = 622 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16); 623 bridge->conf.class_revision = 624 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff); 625 626 /* Support 32 bits I/O addressing */ 627 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32; 628 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32; 629 630 /* Support 64 bits memory pref */ 631 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 632 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 633 634 /* Support interrupt A for MSI feature */ 635 bridge->conf.intpin = PCIE_CORE_INT_A_ASSERT_ENABLE; 636 637 bridge->has_pcie = true; 638 bridge->data = pcie; 639 bridge->ops = &advk_pci_bridge_emul_ops; 640 641 return pci_bridge_emul_init(bridge, 0); 642 } 643 644 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus, 645 int devfn) 646 { 647 if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0) 648 return false; 649 650 /* 651 * If the link goes down after we check for link-up, nothing bad 652 * happens but the config access times out. 653 */ 654 if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie)) 655 return false; 656 657 return true; 658 } 659 660 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie) 661 { 662 struct device *dev = &pcie->pdev->dev; 663 664 /* 665 * Trying to start a new PIO transfer when previous has not completed 666 * cause External Abort on CPU which results in kernel panic: 667 * 668 * SError Interrupt on CPU0, code 0xbf000002 -- SError 669 * Kernel panic - not syncing: Asynchronous SError Interrupt 670 * 671 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected 672 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent 673 * concurrent calls at the same time. But because PIO transfer may take 674 * about 1.5s when link is down or card is disconnected, it means that 675 * advk_pcie_wait_pio() does not always have to wait for completion. 676 * 677 * Some versions of ARM Trusted Firmware handles this External Abort at 678 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit: 679 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50 680 */ 681 if (advk_readl(pcie, PIO_START)) { 682 dev_err(dev, "Previous PIO read/write transfer is still running\n"); 683 return true; 684 } 685 686 return false; 687 } 688 689 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn, 690 int where, int size, u32 *val) 691 { 692 struct advk_pcie *pcie = bus->sysdata; 693 u32 reg; 694 int ret; 695 696 if (!advk_pcie_valid_device(pcie, bus, devfn)) { 697 *val = 0xffffffff; 698 return PCIBIOS_DEVICE_NOT_FOUND; 699 } 700 701 if (pci_is_root_bus(bus)) 702 return pci_bridge_emul_conf_read(&pcie->bridge, where, 703 size, val); 704 705 if (advk_pcie_pio_is_running(pcie)) { 706 *val = 0xffffffff; 707 return PCIBIOS_SET_FAILED; 708 } 709 710 /* Program the control register */ 711 reg = advk_readl(pcie, PIO_CTRL); 712 reg &= ~PIO_CTRL_TYPE_MASK; 713 if (pci_is_root_bus(bus->parent)) 714 reg |= PCIE_CONFIG_RD_TYPE0; 715 else 716 reg |= PCIE_CONFIG_RD_TYPE1; 717 advk_writel(pcie, reg, PIO_CTRL); 718 719 /* Program the address registers */ 720 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 721 advk_writel(pcie, reg, PIO_ADDR_LS); 722 advk_writel(pcie, 0, PIO_ADDR_MS); 723 724 /* Program the data strobe */ 725 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB); 726 727 /* Clear PIO DONE ISR and start the transfer */ 728 advk_writel(pcie, 1, PIO_ISR); 729 advk_writel(pcie, 1, PIO_START); 730 731 ret = advk_pcie_wait_pio(pcie); 732 if (ret < 0) { 733 *val = 0xffffffff; 734 return PCIBIOS_SET_FAILED; 735 } 736 737 advk_pcie_check_pio_status(pcie); 738 739 /* Get the read result */ 740 *val = advk_readl(pcie, PIO_RD_DATA); 741 if (size == 1) 742 *val = (*val >> (8 * (where & 3))) & 0xff; 743 else if (size == 2) 744 *val = (*val >> (8 * (where & 3))) & 0xffff; 745 746 return PCIBIOS_SUCCESSFUL; 747 } 748 749 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 750 int where, int size, u32 val) 751 { 752 struct advk_pcie *pcie = bus->sysdata; 753 u32 reg; 754 u32 data_strobe = 0x0; 755 int offset; 756 int ret; 757 758 if (!advk_pcie_valid_device(pcie, bus, devfn)) 759 return PCIBIOS_DEVICE_NOT_FOUND; 760 761 if (pci_is_root_bus(bus)) 762 return pci_bridge_emul_conf_write(&pcie->bridge, where, 763 size, val); 764 765 if (where % size) 766 return PCIBIOS_SET_FAILED; 767 768 if (advk_pcie_pio_is_running(pcie)) 769 return PCIBIOS_SET_FAILED; 770 771 /* Program the control register */ 772 reg = advk_readl(pcie, PIO_CTRL); 773 reg &= ~PIO_CTRL_TYPE_MASK; 774 if (pci_is_root_bus(bus->parent)) 775 reg |= PCIE_CONFIG_WR_TYPE0; 776 else 777 reg |= PCIE_CONFIG_WR_TYPE1; 778 advk_writel(pcie, reg, PIO_CTRL); 779 780 /* Program the address registers */ 781 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 782 advk_writel(pcie, reg, PIO_ADDR_LS); 783 advk_writel(pcie, 0, PIO_ADDR_MS); 784 785 /* Calculate the write strobe */ 786 offset = where & 0x3; 787 reg = val << (8 * offset); 788 data_strobe = GENMASK(size - 1, 0) << offset; 789 790 /* Program the data register */ 791 advk_writel(pcie, reg, PIO_WR_DATA); 792 793 /* Program the data strobe */ 794 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB); 795 796 /* Clear PIO DONE ISR and start the transfer */ 797 advk_writel(pcie, 1, PIO_ISR); 798 advk_writel(pcie, 1, PIO_START); 799 800 ret = advk_pcie_wait_pio(pcie); 801 if (ret < 0) 802 return PCIBIOS_SET_FAILED; 803 804 advk_pcie_check_pio_status(pcie); 805 806 return PCIBIOS_SUCCESSFUL; 807 } 808 809 static struct pci_ops advk_pcie_ops = { 810 .read = advk_pcie_rd_conf, 811 .write = advk_pcie_wr_conf, 812 }; 813 814 static void advk_msi_irq_compose_msi_msg(struct irq_data *data, 815 struct msi_msg *msg) 816 { 817 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data); 818 phys_addr_t msi_msg = virt_to_phys(&pcie->msi_msg); 819 820 msg->address_lo = lower_32_bits(msi_msg); 821 msg->address_hi = upper_32_bits(msi_msg); 822 msg->data = data->irq; 823 } 824 825 static int advk_msi_set_affinity(struct irq_data *irq_data, 826 const struct cpumask *mask, bool force) 827 { 828 return -EINVAL; 829 } 830 831 static int advk_msi_irq_domain_alloc(struct irq_domain *domain, 832 unsigned int virq, 833 unsigned int nr_irqs, void *args) 834 { 835 struct advk_pcie *pcie = domain->host_data; 836 int hwirq, i; 837 838 mutex_lock(&pcie->msi_used_lock); 839 hwirq = bitmap_find_next_zero_area(pcie->msi_used, MSI_IRQ_NUM, 840 0, nr_irqs, 0); 841 if (hwirq >= MSI_IRQ_NUM) { 842 mutex_unlock(&pcie->msi_used_lock); 843 return -ENOSPC; 844 } 845 846 bitmap_set(pcie->msi_used, hwirq, nr_irqs); 847 mutex_unlock(&pcie->msi_used_lock); 848 849 for (i = 0; i < nr_irqs; i++) 850 irq_domain_set_info(domain, virq + i, hwirq + i, 851 &pcie->msi_bottom_irq_chip, 852 domain->host_data, handle_simple_irq, 853 NULL, NULL); 854 855 return hwirq; 856 } 857 858 static void advk_msi_irq_domain_free(struct irq_domain *domain, 859 unsigned int virq, unsigned int nr_irqs) 860 { 861 struct irq_data *d = irq_domain_get_irq_data(domain, virq); 862 struct advk_pcie *pcie = domain->host_data; 863 864 mutex_lock(&pcie->msi_used_lock); 865 bitmap_clear(pcie->msi_used, d->hwirq, nr_irqs); 866 mutex_unlock(&pcie->msi_used_lock); 867 } 868 869 static const struct irq_domain_ops advk_msi_domain_ops = { 870 .alloc = advk_msi_irq_domain_alloc, 871 .free = advk_msi_irq_domain_free, 872 }; 873 874 static void advk_pcie_irq_mask(struct irq_data *d) 875 { 876 struct advk_pcie *pcie = d->domain->host_data; 877 irq_hw_number_t hwirq = irqd_to_hwirq(d); 878 u32 mask; 879 880 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 881 mask |= PCIE_ISR1_INTX_ASSERT(hwirq); 882 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 883 } 884 885 static void advk_pcie_irq_unmask(struct irq_data *d) 886 { 887 struct advk_pcie *pcie = d->domain->host_data; 888 irq_hw_number_t hwirq = irqd_to_hwirq(d); 889 u32 mask; 890 891 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 892 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq); 893 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 894 } 895 896 static int advk_pcie_irq_map(struct irq_domain *h, 897 unsigned int virq, irq_hw_number_t hwirq) 898 { 899 struct advk_pcie *pcie = h->host_data; 900 901 advk_pcie_irq_mask(irq_get_irq_data(virq)); 902 irq_set_status_flags(virq, IRQ_LEVEL); 903 irq_set_chip_and_handler(virq, &pcie->irq_chip, 904 handle_level_irq); 905 irq_set_chip_data(virq, pcie); 906 907 return 0; 908 } 909 910 static const struct irq_domain_ops advk_pcie_irq_domain_ops = { 911 .map = advk_pcie_irq_map, 912 .xlate = irq_domain_xlate_onecell, 913 }; 914 915 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie) 916 { 917 struct device *dev = &pcie->pdev->dev; 918 struct device_node *node = dev->of_node; 919 struct irq_chip *bottom_ic, *msi_ic; 920 struct msi_domain_info *msi_di; 921 phys_addr_t msi_msg_phys; 922 923 mutex_init(&pcie->msi_used_lock); 924 925 bottom_ic = &pcie->msi_bottom_irq_chip; 926 927 bottom_ic->name = "MSI"; 928 bottom_ic->irq_compose_msi_msg = advk_msi_irq_compose_msi_msg; 929 bottom_ic->irq_set_affinity = advk_msi_set_affinity; 930 931 msi_ic = &pcie->msi_irq_chip; 932 msi_ic->name = "advk-MSI"; 933 934 msi_di = &pcie->msi_domain_info; 935 msi_di->flags = MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 936 MSI_FLAG_MULTI_PCI_MSI; 937 msi_di->chip = msi_ic; 938 939 msi_msg_phys = virt_to_phys(&pcie->msi_msg); 940 941 advk_writel(pcie, lower_32_bits(msi_msg_phys), 942 PCIE_MSI_ADDR_LOW_REG); 943 advk_writel(pcie, upper_32_bits(msi_msg_phys), 944 PCIE_MSI_ADDR_HIGH_REG); 945 946 pcie->msi_inner_domain = 947 irq_domain_add_linear(NULL, MSI_IRQ_NUM, 948 &advk_msi_domain_ops, pcie); 949 if (!pcie->msi_inner_domain) 950 return -ENOMEM; 951 952 pcie->msi_domain = 953 pci_msi_create_irq_domain(of_node_to_fwnode(node), 954 msi_di, pcie->msi_inner_domain); 955 if (!pcie->msi_domain) { 956 irq_domain_remove(pcie->msi_inner_domain); 957 return -ENOMEM; 958 } 959 960 return 0; 961 } 962 963 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie) 964 { 965 irq_domain_remove(pcie->msi_domain); 966 irq_domain_remove(pcie->msi_inner_domain); 967 } 968 969 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie) 970 { 971 struct device *dev = &pcie->pdev->dev; 972 struct device_node *node = dev->of_node; 973 struct device_node *pcie_intc_node; 974 struct irq_chip *irq_chip; 975 int ret = 0; 976 977 pcie_intc_node = of_get_next_child(node, NULL); 978 if (!pcie_intc_node) { 979 dev_err(dev, "No PCIe Intc node found\n"); 980 return -ENODEV; 981 } 982 983 irq_chip = &pcie->irq_chip; 984 985 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq", 986 dev_name(dev)); 987 if (!irq_chip->name) { 988 ret = -ENOMEM; 989 goto out_put_node; 990 } 991 992 irq_chip->irq_mask = advk_pcie_irq_mask; 993 irq_chip->irq_mask_ack = advk_pcie_irq_mask; 994 irq_chip->irq_unmask = advk_pcie_irq_unmask; 995 996 pcie->irq_domain = 997 irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, 998 &advk_pcie_irq_domain_ops, pcie); 999 if (!pcie->irq_domain) { 1000 dev_err(dev, "Failed to get a INTx IRQ domain\n"); 1001 ret = -ENOMEM; 1002 goto out_put_node; 1003 } 1004 1005 out_put_node: 1006 of_node_put(pcie_intc_node); 1007 return ret; 1008 } 1009 1010 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie) 1011 { 1012 irq_domain_remove(pcie->irq_domain); 1013 } 1014 1015 static void advk_pcie_handle_msi(struct advk_pcie *pcie) 1016 { 1017 u32 msi_val, msi_mask, msi_status, msi_idx; 1018 u16 msi_data; 1019 1020 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 1021 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG); 1022 msi_status = msi_val & ~msi_mask; 1023 1024 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) { 1025 if (!(BIT(msi_idx) & msi_status)) 1026 continue; 1027 1028 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG); 1029 msi_data = advk_readl(pcie, PCIE_MSI_PAYLOAD_REG) & 0xFF; 1030 generic_handle_irq(msi_data); 1031 } 1032 1033 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING, 1034 PCIE_ISR0_REG); 1035 } 1036 1037 static void advk_pcie_handle_int(struct advk_pcie *pcie) 1038 { 1039 u32 isr0_val, isr0_mask, isr0_status; 1040 u32 isr1_val, isr1_mask, isr1_status; 1041 int i, virq; 1042 1043 isr0_val = advk_readl(pcie, PCIE_ISR0_REG); 1044 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG); 1045 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK); 1046 1047 isr1_val = advk_readl(pcie, PCIE_ISR1_REG); 1048 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 1049 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK); 1050 1051 if (!isr0_status && !isr1_status) { 1052 advk_writel(pcie, isr0_val, PCIE_ISR0_REG); 1053 advk_writel(pcie, isr1_val, PCIE_ISR1_REG); 1054 return; 1055 } 1056 1057 /* Process MSI interrupts */ 1058 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING) 1059 advk_pcie_handle_msi(pcie); 1060 1061 /* Process legacy interrupts */ 1062 for (i = 0; i < PCI_NUM_INTX; i++) { 1063 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i))) 1064 continue; 1065 1066 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i), 1067 PCIE_ISR1_REG); 1068 1069 virq = irq_find_mapping(pcie->irq_domain, i); 1070 generic_handle_irq(virq); 1071 } 1072 } 1073 1074 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg) 1075 { 1076 struct advk_pcie *pcie = arg; 1077 u32 status; 1078 1079 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG); 1080 if (!(status & PCIE_IRQ_CORE_INT)) 1081 return IRQ_NONE; 1082 1083 advk_pcie_handle_int(pcie); 1084 1085 /* Clear interrupt */ 1086 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG); 1087 1088 return IRQ_HANDLED; 1089 } 1090 1091 static void __maybe_unused advk_pcie_disable_phy(struct advk_pcie *pcie) 1092 { 1093 phy_power_off(pcie->phy); 1094 phy_exit(pcie->phy); 1095 } 1096 1097 static int advk_pcie_enable_phy(struct advk_pcie *pcie) 1098 { 1099 int ret; 1100 1101 if (!pcie->phy) 1102 return 0; 1103 1104 ret = phy_init(pcie->phy); 1105 if (ret) 1106 return ret; 1107 1108 ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE); 1109 if (ret) { 1110 phy_exit(pcie->phy); 1111 return ret; 1112 } 1113 1114 ret = phy_power_on(pcie->phy); 1115 if (ret == -EOPNOTSUPP) { 1116 dev_warn(&pcie->pdev->dev, "PHY unsupported by firmware\n"); 1117 } else if (ret) { 1118 phy_exit(pcie->phy); 1119 return ret; 1120 } 1121 1122 return 0; 1123 } 1124 1125 static int advk_pcie_setup_phy(struct advk_pcie *pcie) 1126 { 1127 struct device *dev = &pcie->pdev->dev; 1128 struct device_node *node = dev->of_node; 1129 int ret = 0; 1130 1131 pcie->phy = devm_of_phy_get(dev, node, NULL); 1132 if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER)) 1133 return PTR_ERR(pcie->phy); 1134 1135 /* Old bindings miss the PHY handle */ 1136 if (IS_ERR(pcie->phy)) { 1137 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy)); 1138 pcie->phy = NULL; 1139 return 0; 1140 } 1141 1142 ret = advk_pcie_enable_phy(pcie); 1143 if (ret) 1144 dev_err(dev, "Failed to initialize PHY (%d)\n", ret); 1145 1146 return ret; 1147 } 1148 1149 static int advk_pcie_probe(struct platform_device *pdev) 1150 { 1151 struct device *dev = &pdev->dev; 1152 struct advk_pcie *pcie; 1153 struct pci_host_bridge *bridge; 1154 int ret, irq; 1155 1156 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie)); 1157 if (!bridge) 1158 return -ENOMEM; 1159 1160 pcie = pci_host_bridge_priv(bridge); 1161 pcie->pdev = pdev; 1162 platform_set_drvdata(pdev, pcie); 1163 1164 pcie->base = devm_platform_ioremap_resource(pdev, 0); 1165 if (IS_ERR(pcie->base)) 1166 return PTR_ERR(pcie->base); 1167 1168 irq = platform_get_irq(pdev, 0); 1169 if (irq < 0) 1170 return irq; 1171 1172 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler, 1173 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie", 1174 pcie); 1175 if (ret) { 1176 dev_err(dev, "Failed to register interrupt\n"); 1177 return ret; 1178 } 1179 1180 pcie->reset_gpio = devm_gpiod_get_from_of_node(dev, dev->of_node, 1181 "reset-gpios", 0, 1182 GPIOD_OUT_LOW, 1183 "pcie1-reset"); 1184 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio); 1185 if (ret) { 1186 if (ret == -ENOENT) { 1187 pcie->reset_gpio = NULL; 1188 } else { 1189 if (ret != -EPROBE_DEFER) 1190 dev_err(dev, "Failed to get reset-gpio: %i\n", 1191 ret); 1192 return ret; 1193 } 1194 } 1195 1196 ret = of_pci_get_max_link_speed(dev->of_node); 1197 if (ret <= 0 || ret > 3) 1198 pcie->link_gen = 3; 1199 else 1200 pcie->link_gen = ret; 1201 1202 ret = advk_pcie_setup_phy(pcie); 1203 if (ret) 1204 return ret; 1205 1206 advk_pcie_setup_hw(pcie); 1207 1208 ret = advk_sw_pci_bridge_init(pcie); 1209 if (ret) { 1210 dev_err(dev, "Failed to register emulated root PCI bridge\n"); 1211 return ret; 1212 } 1213 1214 ret = advk_pcie_init_irq_domain(pcie); 1215 if (ret) { 1216 dev_err(dev, "Failed to initialize irq\n"); 1217 return ret; 1218 } 1219 1220 ret = advk_pcie_init_msi_irq_domain(pcie); 1221 if (ret) { 1222 dev_err(dev, "Failed to initialize irq\n"); 1223 advk_pcie_remove_irq_domain(pcie); 1224 return ret; 1225 } 1226 1227 bridge->sysdata = pcie; 1228 bridge->ops = &advk_pcie_ops; 1229 1230 ret = pci_host_probe(bridge); 1231 if (ret < 0) { 1232 advk_pcie_remove_msi_irq_domain(pcie); 1233 advk_pcie_remove_irq_domain(pcie); 1234 return ret; 1235 } 1236 1237 return 0; 1238 } 1239 1240 static int advk_pcie_remove(struct platform_device *pdev) 1241 { 1242 struct advk_pcie *pcie = platform_get_drvdata(pdev); 1243 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 1244 1245 pci_lock_rescan_remove(); 1246 pci_stop_root_bus(bridge->bus); 1247 pci_remove_root_bus(bridge->bus); 1248 pci_unlock_rescan_remove(); 1249 1250 advk_pcie_remove_msi_irq_domain(pcie); 1251 advk_pcie_remove_irq_domain(pcie); 1252 1253 return 0; 1254 } 1255 1256 static const struct of_device_id advk_pcie_of_match_table[] = { 1257 { .compatible = "marvell,armada-3700-pcie", }, 1258 {}, 1259 }; 1260 MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table); 1261 1262 static struct platform_driver advk_pcie_driver = { 1263 .driver = { 1264 .name = "advk-pcie", 1265 .of_match_table = advk_pcie_of_match_table, 1266 }, 1267 .probe = advk_pcie_probe, 1268 .remove = advk_pcie_remove, 1269 }; 1270 module_platform_driver(advk_pcie_driver); 1271 1272 MODULE_DESCRIPTION("Aardvark PCIe controller"); 1273 MODULE_LICENSE("GPL v2"); 1274