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/bitfield.h> 12 #include <linux/delay.h> 13 #include <linux/gpio/consumer.h> 14 #include <linux/interrupt.h> 15 #include <linux/irq.h> 16 #include <linux/irqchip/irq-msi-lib.h> 17 #include <linux/irqdomain.h> 18 #include <linux/kernel.h> 19 #include <linux/module.h> 20 #include <linux/pci.h> 21 #include <linux/pci-ecam.h> 22 #include <linux/init.h> 23 #include <linux/phy/phy.h> 24 #include <linux/platform_device.h> 25 #include <linux/msi.h> 26 #include <linux/of_address.h> 27 #include <linux/of_pci.h> 28 29 #include "pci-host-common.h" 30 #include "../pci.h" 31 #include "../pci-bridge-emul.h" 32 33 /* PCIe core registers */ 34 #define PCIE_CORE_DEV_ID_REG 0x0 35 #define PCIE_CORE_CMD_STATUS_REG 0x4 36 #define PCIE_CORE_DEV_REV_REG 0x8 37 #define PCIE_CORE_SSDEV_ID_REG 0x2c 38 #define PCIE_CORE_PCIEXP_CAP 0xc0 39 #define PCIE_CORE_PCIERR_CAP 0x100 40 #define PCIE_CORE_ERR_CAPCTL_REG 0x118 41 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX BIT(5) 42 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN BIT(6) 43 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK BIT(7) 44 #define PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV BIT(8) 45 /* PIO registers base address and register offsets */ 46 #define PIO_BASE_ADDR 0x4000 47 #define PIO_CTRL (PIO_BASE_ADDR + 0x0) 48 #define PIO_CTRL_TYPE_MASK GENMASK(3, 0) 49 #define PIO_CTRL_ADDR_WIN_DISABLE BIT(24) 50 #define PIO_STAT (PIO_BASE_ADDR + 0x4) 51 #define PIO_COMPLETION_STATUS_SHIFT 7 52 #define PIO_COMPLETION_STATUS_MASK GENMASK(9, 7) 53 #define PIO_COMPLETION_STATUS_OK 0 54 #define PIO_COMPLETION_STATUS_UR 1 55 #define PIO_COMPLETION_STATUS_RRS 2 56 #define PIO_COMPLETION_STATUS_CA 4 57 #define PIO_NON_POSTED_REQ BIT(10) 58 #define PIO_ERR_STATUS BIT(11) 59 #define PIO_ADDR_LS (PIO_BASE_ADDR + 0x8) 60 #define PIO_ADDR_MS (PIO_BASE_ADDR + 0xc) 61 #define PIO_WR_DATA (PIO_BASE_ADDR + 0x10) 62 #define PIO_WR_DATA_STRB (PIO_BASE_ADDR + 0x14) 63 #define PIO_RD_DATA (PIO_BASE_ADDR + 0x18) 64 #define PIO_START (PIO_BASE_ADDR + 0x1c) 65 #define PIO_ISR (PIO_BASE_ADDR + 0x20) 66 #define PIO_ISRM (PIO_BASE_ADDR + 0x24) 67 68 /* Aardvark Control registers */ 69 #define CONTROL_BASE_ADDR 0x4800 70 #define PCIE_CORE_CTRL0_REG (CONTROL_BASE_ADDR + 0x0) 71 #define PCIE_GEN_SEL_MSK 0x3 72 #define PCIE_GEN_SEL_SHIFT 0x0 73 #define SPEED_GEN_1 0 74 #define SPEED_GEN_2 1 75 #define SPEED_GEN_3 2 76 #define IS_RC_MSK 1 77 #define IS_RC_SHIFT 2 78 #define LANE_CNT_MSK 0x18 79 #define LANE_CNT_SHIFT 0x3 80 #define LANE_COUNT_1 (0 << LANE_CNT_SHIFT) 81 #define LANE_COUNT_2 (1 << LANE_CNT_SHIFT) 82 #define LANE_COUNT_4 (2 << LANE_CNT_SHIFT) 83 #define LANE_COUNT_8 (3 << LANE_CNT_SHIFT) 84 #define LINK_TRAINING_EN BIT(6) 85 #define LEGACY_INTA BIT(28) 86 #define LEGACY_INTB BIT(29) 87 #define LEGACY_INTC BIT(30) 88 #define LEGACY_INTD BIT(31) 89 #define PCIE_CORE_CTRL1_REG (CONTROL_BASE_ADDR + 0x4) 90 #define HOT_RESET_GEN BIT(0) 91 #define PCIE_CORE_CTRL2_REG (CONTROL_BASE_ADDR + 0x8) 92 #define PCIE_CORE_CTRL2_RESERVED 0x7 93 #define PCIE_CORE_CTRL2_TD_ENABLE BIT(4) 94 #define PCIE_CORE_CTRL2_STRICT_ORDER_ENABLE BIT(5) 95 #define PCIE_CORE_CTRL2_OB_WIN_ENABLE BIT(6) 96 #define PCIE_CORE_CTRL2_MSI_ENABLE BIT(10) 97 #define PCIE_CORE_REF_CLK_REG (CONTROL_BASE_ADDR + 0x14) 98 #define PCIE_CORE_REF_CLK_TX_ENABLE BIT(1) 99 #define PCIE_CORE_REF_CLK_RX_ENABLE BIT(2) 100 #define PCIE_MSG_LOG_REG (CONTROL_BASE_ADDR + 0x30) 101 #define PCIE_ISR0_REG (CONTROL_BASE_ADDR + 0x40) 102 #define PCIE_MSG_PM_PME_MASK BIT(7) 103 #define PCIE_ISR0_MASK_REG (CONTROL_BASE_ADDR + 0x44) 104 #define PCIE_ISR0_MSI_INT_PENDING BIT(24) 105 #define PCIE_ISR0_CORR_ERR BIT(11) 106 #define PCIE_ISR0_NFAT_ERR BIT(12) 107 #define PCIE_ISR0_FAT_ERR BIT(13) 108 #define PCIE_ISR0_ERR_MASK GENMASK(13, 11) 109 #define PCIE_ISR0_INTX_ASSERT(val) BIT(16 + (val)) 110 #define PCIE_ISR0_INTX_DEASSERT(val) BIT(20 + (val)) 111 #define PCIE_ISR0_ALL_MASK GENMASK(31, 0) 112 #define PCIE_ISR1_REG (CONTROL_BASE_ADDR + 0x48) 113 #define PCIE_ISR1_MASK_REG (CONTROL_BASE_ADDR + 0x4C) 114 #define PCIE_ISR1_POWER_STATE_CHANGE BIT(4) 115 #define PCIE_ISR1_FLUSH BIT(5) 116 #define PCIE_ISR1_INTX_ASSERT(val) BIT(8 + (val)) 117 #define PCIE_ISR1_ALL_MASK GENMASK(31, 0) 118 #define PCIE_MSI_ADDR_LOW_REG (CONTROL_BASE_ADDR + 0x50) 119 #define PCIE_MSI_ADDR_HIGH_REG (CONTROL_BASE_ADDR + 0x54) 120 #define PCIE_MSI_STATUS_REG (CONTROL_BASE_ADDR + 0x58) 121 #define PCIE_MSI_MASK_REG (CONTROL_BASE_ADDR + 0x5C) 122 #define PCIE_MSI_ALL_MASK GENMASK(31, 0) 123 #define PCIE_MSI_PAYLOAD_REG (CONTROL_BASE_ADDR + 0x9C) 124 #define PCIE_MSI_DATA_MASK GENMASK(15, 0) 125 126 /* PCIe window configuration */ 127 #define OB_WIN_BASE_ADDR 0x4c00 128 #define OB_WIN_BLOCK_SIZE 0x20 129 #define OB_WIN_COUNT 8 130 #define OB_WIN_REG_ADDR(win, offset) (OB_WIN_BASE_ADDR + \ 131 OB_WIN_BLOCK_SIZE * (win) + \ 132 (offset)) 133 #define OB_WIN_MATCH_LS(win) OB_WIN_REG_ADDR(win, 0x00) 134 #define OB_WIN_ENABLE BIT(0) 135 #define OB_WIN_MATCH_MS(win) OB_WIN_REG_ADDR(win, 0x04) 136 #define OB_WIN_REMAP_LS(win) OB_WIN_REG_ADDR(win, 0x08) 137 #define OB_WIN_REMAP_MS(win) OB_WIN_REG_ADDR(win, 0x0c) 138 #define OB_WIN_MASK_LS(win) OB_WIN_REG_ADDR(win, 0x10) 139 #define OB_WIN_MASK_MS(win) OB_WIN_REG_ADDR(win, 0x14) 140 #define OB_WIN_ACTIONS(win) OB_WIN_REG_ADDR(win, 0x18) 141 #define OB_WIN_DEFAULT_ACTIONS (OB_WIN_ACTIONS(OB_WIN_COUNT-1) + 0x4) 142 #define OB_WIN_FUNC_NUM_MASK GENMASK(31, 24) 143 #define OB_WIN_FUNC_NUM_SHIFT 24 144 #define OB_WIN_FUNC_NUM_ENABLE BIT(23) 145 #define OB_WIN_BUS_NUM_BITS_MASK GENMASK(22, 20) 146 #define OB_WIN_BUS_NUM_BITS_SHIFT 20 147 #define OB_WIN_MSG_CODE_ENABLE BIT(22) 148 #define OB_WIN_MSG_CODE_MASK GENMASK(21, 14) 149 #define OB_WIN_MSG_CODE_SHIFT 14 150 #define OB_WIN_MSG_PAYLOAD_LEN BIT(12) 151 #define OB_WIN_ATTR_ENABLE BIT(11) 152 #define OB_WIN_ATTR_TC_MASK GENMASK(10, 8) 153 #define OB_WIN_ATTR_TC_SHIFT 8 154 #define OB_WIN_ATTR_RELAXED BIT(7) 155 #define OB_WIN_ATTR_NOSNOOP BIT(6) 156 #define OB_WIN_ATTR_POISON BIT(5) 157 #define OB_WIN_ATTR_IDO BIT(4) 158 #define OB_WIN_TYPE_MASK GENMASK(3, 0) 159 #define OB_WIN_TYPE_SHIFT 0 160 #define OB_WIN_TYPE_MEM 0x0 161 #define OB_WIN_TYPE_IO 0x4 162 #define OB_WIN_TYPE_CONFIG_TYPE0 0x8 163 #define OB_WIN_TYPE_CONFIG_TYPE1 0x9 164 #define OB_WIN_TYPE_MSG 0xc 165 166 /* LMI registers base address and register offsets */ 167 #define LMI_BASE_ADDR 0x6000 168 #define CFG_REG (LMI_BASE_ADDR + 0x0) 169 #define LTSSM_SHIFT 24 170 #define LTSSM_MASK 0x3f 171 #define RC_BAR_CONFIG 0x300 172 173 /* LTSSM values in CFG_REG */ 174 enum { 175 LTSSM_DETECT_QUIET = 0x0, 176 LTSSM_DETECT_ACTIVE = 0x1, 177 LTSSM_POLLING_ACTIVE = 0x2, 178 LTSSM_POLLING_COMPLIANCE = 0x3, 179 LTSSM_POLLING_CONFIGURATION = 0x4, 180 LTSSM_CONFIG_LINKWIDTH_START = 0x5, 181 LTSSM_CONFIG_LINKWIDTH_ACCEPT = 0x6, 182 LTSSM_CONFIG_LANENUM_ACCEPT = 0x7, 183 LTSSM_CONFIG_LANENUM_WAIT = 0x8, 184 LTSSM_CONFIG_COMPLETE = 0x9, 185 LTSSM_CONFIG_IDLE = 0xa, 186 LTSSM_RECOVERY_RCVR_LOCK = 0xb, 187 LTSSM_RECOVERY_SPEED = 0xc, 188 LTSSM_RECOVERY_RCVR_CFG = 0xd, 189 LTSSM_RECOVERY_IDLE = 0xe, 190 LTSSM_L0 = 0x10, 191 LTSSM_RX_L0S_ENTRY = 0x11, 192 LTSSM_RX_L0S_IDLE = 0x12, 193 LTSSM_RX_L0S_FTS = 0x13, 194 LTSSM_TX_L0S_ENTRY = 0x14, 195 LTSSM_TX_L0S_IDLE = 0x15, 196 LTSSM_TX_L0S_FTS = 0x16, 197 LTSSM_L1_ENTRY = 0x17, 198 LTSSM_L1_IDLE = 0x18, 199 LTSSM_L2_IDLE = 0x19, 200 LTSSM_L2_TRANSMIT_WAKE = 0x1a, 201 LTSSM_DISABLED = 0x20, 202 LTSSM_LOOPBACK_ENTRY_MASTER = 0x21, 203 LTSSM_LOOPBACK_ACTIVE_MASTER = 0x22, 204 LTSSM_LOOPBACK_EXIT_MASTER = 0x23, 205 LTSSM_LOOPBACK_ENTRY_SLAVE = 0x24, 206 LTSSM_LOOPBACK_ACTIVE_SLAVE = 0x25, 207 LTSSM_LOOPBACK_EXIT_SLAVE = 0x26, 208 LTSSM_HOT_RESET = 0x27, 209 LTSSM_RECOVERY_EQUALIZATION_PHASE0 = 0x28, 210 LTSSM_RECOVERY_EQUALIZATION_PHASE1 = 0x29, 211 LTSSM_RECOVERY_EQUALIZATION_PHASE2 = 0x2a, 212 LTSSM_RECOVERY_EQUALIZATION_PHASE3 = 0x2b, 213 }; 214 215 #define VENDOR_ID_REG (LMI_BASE_ADDR + 0x44) 216 217 /* PCIe core controller registers */ 218 #define CTRL_CORE_BASE_ADDR 0x18000 219 #define CTRL_CONFIG_REG (CTRL_CORE_BASE_ADDR + 0x0) 220 #define CTRL_MODE_SHIFT 0x0 221 #define CTRL_MODE_MASK 0x1 222 #define PCIE_CORE_MODE_DIRECT 0x0 223 #define PCIE_CORE_MODE_COMMAND 0x1 224 225 /* PCIe Central Interrupts Registers */ 226 #define CENTRAL_INT_BASE_ADDR 0x1b000 227 #define HOST_CTRL_INT_STATUS_REG (CENTRAL_INT_BASE_ADDR + 0x0) 228 #define HOST_CTRL_INT_MASK_REG (CENTRAL_INT_BASE_ADDR + 0x4) 229 #define PCIE_IRQ_CMDQ_INT BIT(0) 230 #define PCIE_IRQ_MSI_STATUS_INT BIT(1) 231 #define PCIE_IRQ_CMD_SENT_DONE BIT(3) 232 #define PCIE_IRQ_DMA_INT BIT(4) 233 #define PCIE_IRQ_IB_DXFERDONE BIT(5) 234 #define PCIE_IRQ_OB_DXFERDONE BIT(6) 235 #define PCIE_IRQ_OB_RXFERDONE BIT(7) 236 #define PCIE_IRQ_COMPQ_INT BIT(12) 237 #define PCIE_IRQ_DIR_RD_DDR_DET BIT(13) 238 #define PCIE_IRQ_DIR_WR_DDR_DET BIT(14) 239 #define PCIE_IRQ_CORE_INT BIT(16) 240 #define PCIE_IRQ_CORE_INT_PIO BIT(17) 241 #define PCIE_IRQ_DPMU_INT BIT(18) 242 #define PCIE_IRQ_PCIE_MIS_INT BIT(19) 243 #define PCIE_IRQ_MSI_INT1_DET BIT(20) 244 #define PCIE_IRQ_MSI_INT2_DET BIT(21) 245 #define PCIE_IRQ_RC_DBELL_DET BIT(22) 246 #define PCIE_IRQ_EP_STATUS BIT(23) 247 #define PCIE_IRQ_ALL_MASK GENMASK(31, 0) 248 #define PCIE_IRQ_ENABLE_INTS_MASK PCIE_IRQ_CORE_INT 249 250 /* Transaction types */ 251 #define PCIE_CONFIG_RD_TYPE0 0x8 252 #define PCIE_CONFIG_RD_TYPE1 0x9 253 #define PCIE_CONFIG_WR_TYPE0 0xa 254 #define PCIE_CONFIG_WR_TYPE1 0xb 255 256 #define PIO_RETRY_CNT 750000 /* 1.5 s */ 257 #define PIO_RETRY_DELAY 2 /* 2 us*/ 258 259 #define LINK_WAIT_MAX_RETRIES 10 260 #define LINK_WAIT_USLEEP_MIN 90000 261 #define LINK_WAIT_USLEEP_MAX 100000 262 #define RETRAIN_WAIT_MAX_RETRIES 10 263 #define RETRAIN_WAIT_USLEEP_US 2000 264 265 #define MSI_IRQ_NUM 32 266 267 #define CFG_RD_RRS_VAL 0xffff0001 268 269 struct advk_pcie { 270 struct platform_device *pdev; 271 void __iomem *base; 272 struct { 273 phys_addr_t match; 274 phys_addr_t remap; 275 phys_addr_t mask; 276 u32 actions; 277 } wins[OB_WIN_COUNT]; 278 u8 wins_count; 279 struct irq_domain *rp_irq_domain; 280 struct irq_domain *irq_domain; 281 struct irq_chip irq_chip; 282 raw_spinlock_t irq_lock; 283 struct irq_domain *msi_inner_domain; 284 raw_spinlock_t msi_irq_lock; 285 DECLARE_BITMAP(msi_used, MSI_IRQ_NUM); 286 struct mutex msi_used_lock; 287 int link_gen; 288 struct pci_bridge_emul bridge; 289 struct gpio_desc *reset_gpio; 290 struct phy *phy; 291 }; 292 293 static inline void advk_writel(struct advk_pcie *pcie, u32 val, u64 reg) 294 { 295 writel(val, pcie->base + reg); 296 } 297 298 static inline u32 advk_readl(struct advk_pcie *pcie, u64 reg) 299 { 300 return readl(pcie->base + reg); 301 } 302 303 static u8 advk_pcie_ltssm_state(struct advk_pcie *pcie) 304 { 305 u32 val; 306 u8 ltssm_state; 307 308 val = advk_readl(pcie, CFG_REG); 309 ltssm_state = (val >> LTSSM_SHIFT) & LTSSM_MASK; 310 return ltssm_state; 311 } 312 313 static inline bool advk_pcie_link_up(struct advk_pcie *pcie) 314 { 315 /* check if LTSSM is in normal operation - some L* state */ 316 u8 ltssm_state = advk_pcie_ltssm_state(pcie); 317 return ltssm_state >= LTSSM_L0 && ltssm_state < LTSSM_DISABLED; 318 } 319 320 static inline bool advk_pcie_link_active(struct advk_pcie *pcie) 321 { 322 /* 323 * According to PCIe Base specification 3.0, Table 4-14: Link 324 * Status Mapped to the LTSSM, and 4.2.6.3.6 Configuration.Idle 325 * is Link Up mapped to LTSSM Configuration.Idle, Recovery, L0, 326 * L0s, L1 and L2 states. And according to 3.2.1. Data Link 327 * Control and Management State Machine Rules is DL Up status 328 * reported in DL Active state. 329 */ 330 u8 ltssm_state = advk_pcie_ltssm_state(pcie); 331 return ltssm_state >= LTSSM_CONFIG_IDLE && ltssm_state < LTSSM_DISABLED; 332 } 333 334 static inline bool advk_pcie_link_training(struct advk_pcie *pcie) 335 { 336 /* 337 * According to PCIe Base specification 3.0, Table 4-14: Link 338 * Status Mapped to the LTSSM is Link Training mapped to LTSSM 339 * Configuration and Recovery states. 340 */ 341 u8 ltssm_state = advk_pcie_ltssm_state(pcie); 342 return ((ltssm_state >= LTSSM_CONFIG_LINKWIDTH_START && 343 ltssm_state < LTSSM_L0) || 344 (ltssm_state >= LTSSM_RECOVERY_EQUALIZATION_PHASE0 && 345 ltssm_state <= LTSSM_RECOVERY_EQUALIZATION_PHASE3)); 346 } 347 348 static int advk_pcie_wait_for_link(struct advk_pcie *pcie) 349 { 350 int retries; 351 352 /* check if the link is up or not */ 353 for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) { 354 if (advk_pcie_link_up(pcie)) { 355 pci_host_common_link_train_delay(pcie->link_gen); 356 return 0; 357 } 358 359 usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX); 360 } 361 362 return -ETIMEDOUT; 363 } 364 365 static void advk_pcie_wait_for_retrain(struct advk_pcie *pcie) 366 { 367 size_t retries; 368 369 for (retries = 0; retries < RETRAIN_WAIT_MAX_RETRIES; ++retries) { 370 if (advk_pcie_link_training(pcie)) 371 break; 372 udelay(RETRAIN_WAIT_USLEEP_US); 373 } 374 } 375 376 static void advk_pcie_issue_perst(struct advk_pcie *pcie) 377 { 378 if (!pcie->reset_gpio) 379 return; 380 381 /* 10ms delay is needed for some cards */ 382 dev_info(&pcie->pdev->dev, "issuing PERST via reset GPIO for 10ms\n"); 383 gpiod_set_value_cansleep(pcie->reset_gpio, 1); 384 usleep_range(10000, 11000); 385 gpiod_set_value_cansleep(pcie->reset_gpio, 0); 386 } 387 388 static void advk_pcie_train_link(struct advk_pcie *pcie) 389 { 390 struct device *dev = &pcie->pdev->dev; 391 u32 reg; 392 int ret; 393 394 /* 395 * Setup PCIe rev / gen compliance based on device tree property 396 * 'max-link-speed' which also forces maximal link speed. 397 */ 398 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 399 reg &= ~PCIE_GEN_SEL_MSK; 400 if (pcie->link_gen == 3) 401 reg |= SPEED_GEN_3; 402 else if (pcie->link_gen == 2) 403 reg |= SPEED_GEN_2; 404 else 405 reg |= SPEED_GEN_1; 406 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 407 408 /* 409 * Set maximal link speed value also into PCIe Link Control 2 register. 410 * Armada 3700 Functional Specification says that default value is based 411 * on SPEED_GEN but tests showed that default value is always 8.0 GT/s. 412 */ 413 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 414 reg &= ~PCI_EXP_LNKCTL2_TLS; 415 if (pcie->link_gen == 3) 416 reg |= PCI_EXP_LNKCTL2_TLS_8_0GT; 417 else if (pcie->link_gen == 2) 418 reg |= PCI_EXP_LNKCTL2_TLS_5_0GT; 419 else 420 reg |= PCI_EXP_LNKCTL2_TLS_2_5GT; 421 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_LNKCTL2); 422 423 /* Enable link training after selecting PCIe generation */ 424 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 425 reg |= LINK_TRAINING_EN; 426 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 427 428 /* 429 * Reset PCIe card via PERST# signal. Some cards are not detected 430 * during link training when they are in some non-initial state. 431 */ 432 advk_pcie_issue_perst(pcie); 433 434 /* 435 * PERST# signal could have been asserted by pinctrl subsystem before 436 * probe() callback has been called or issued explicitly by reset gpio 437 * function advk_pcie_issue_perst(), making the endpoint going into 438 * fundamental reset. As required by PCI Express spec (PCI Express 439 * Base Specification, REV. 4.0 PCI Express, February 19 2014, 6.6.1 440 * Conventional Reset) a delay for at least 100ms after such a reset 441 * before sending a Configuration Request to the device is needed. 442 * So wait until PCIe link is up. Function advk_pcie_wait_for_link() 443 * waits for link at least 900ms. 444 */ 445 ret = advk_pcie_wait_for_link(pcie); 446 if (ret < 0) 447 dev_err(dev, "link never came up\n"); 448 else 449 dev_info(dev, "link up\n"); 450 } 451 452 /* 453 * Set PCIe address window register which could be used for memory 454 * mapping. 455 */ 456 static void advk_pcie_set_ob_win(struct advk_pcie *pcie, u8 win_num, 457 phys_addr_t match, phys_addr_t remap, 458 phys_addr_t mask, u32 actions) 459 { 460 advk_writel(pcie, OB_WIN_ENABLE | 461 lower_32_bits(match), OB_WIN_MATCH_LS(win_num)); 462 advk_writel(pcie, upper_32_bits(match), OB_WIN_MATCH_MS(win_num)); 463 advk_writel(pcie, lower_32_bits(remap), OB_WIN_REMAP_LS(win_num)); 464 advk_writel(pcie, upper_32_bits(remap), OB_WIN_REMAP_MS(win_num)); 465 advk_writel(pcie, lower_32_bits(mask), OB_WIN_MASK_LS(win_num)); 466 advk_writel(pcie, upper_32_bits(mask), OB_WIN_MASK_MS(win_num)); 467 advk_writel(pcie, actions, OB_WIN_ACTIONS(win_num)); 468 } 469 470 static void advk_pcie_disable_ob_win(struct advk_pcie *pcie, u8 win_num) 471 { 472 advk_writel(pcie, 0, OB_WIN_MATCH_LS(win_num)); 473 advk_writel(pcie, 0, OB_WIN_MATCH_MS(win_num)); 474 advk_writel(pcie, 0, OB_WIN_REMAP_LS(win_num)); 475 advk_writel(pcie, 0, OB_WIN_REMAP_MS(win_num)); 476 advk_writel(pcie, 0, OB_WIN_MASK_LS(win_num)); 477 advk_writel(pcie, 0, OB_WIN_MASK_MS(win_num)); 478 advk_writel(pcie, 0, OB_WIN_ACTIONS(win_num)); 479 } 480 481 static void advk_pcie_setup_hw(struct advk_pcie *pcie) 482 { 483 phys_addr_t msi_addr; 484 u32 reg; 485 int i; 486 487 /* 488 * Configure PCIe Reference clock. Direction is from the PCIe 489 * controller to the endpoint card, so enable transmitting of 490 * Reference clock differential signal off-chip and disable 491 * receiving off-chip differential signal. 492 */ 493 reg = advk_readl(pcie, PCIE_CORE_REF_CLK_REG); 494 reg |= PCIE_CORE_REF_CLK_TX_ENABLE; 495 reg &= ~PCIE_CORE_REF_CLK_RX_ENABLE; 496 advk_writel(pcie, reg, PCIE_CORE_REF_CLK_REG); 497 498 /* Set to Direct mode */ 499 reg = advk_readl(pcie, CTRL_CONFIG_REG); 500 reg &= ~(CTRL_MODE_MASK << CTRL_MODE_SHIFT); 501 reg |= ((PCIE_CORE_MODE_DIRECT & CTRL_MODE_MASK) << CTRL_MODE_SHIFT); 502 advk_writel(pcie, reg, CTRL_CONFIG_REG); 503 504 /* Set PCI global control register to RC mode */ 505 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 506 reg |= (IS_RC_MSK << IS_RC_SHIFT); 507 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 508 509 /* 510 * Replace incorrect PCI vendor id value 0x1b4b by correct value 0x11ab. 511 * VENDOR_ID_REG contains vendor id in low 16 bits and subsystem vendor 512 * id in high 16 bits. Updating this register changes readback value of 513 * read-only vendor id bits in PCIE_CORE_DEV_ID_REG register. Workaround 514 * for erratum 4.1: "The value of device and vendor ID is incorrect". 515 */ 516 reg = (PCI_VENDOR_ID_MARVELL << 16) | PCI_VENDOR_ID_MARVELL; 517 advk_writel(pcie, reg, VENDOR_ID_REG); 518 519 /* 520 * Change Class Code of PCI Bridge device to PCI Bridge (0x600400), 521 * because the default value is Mass storage controller (0x010400). 522 * 523 * Note that this Aardvark PCI Bridge does not have compliant Type 1 524 * Configuration Space and it even cannot be accessed via Aardvark's 525 * PCI config space access method. Something like config space is 526 * available in internal Aardvark registers starting at offset 0x0 527 * and is reported as Type 0. In range 0x10 - 0x34 it has totally 528 * different registers. 529 * 530 * Therefore driver uses emulation of PCI Bridge which emulates 531 * access to configuration space via internal Aardvark registers or 532 * emulated configuration buffer. 533 */ 534 reg = advk_readl(pcie, PCIE_CORE_DEV_REV_REG); 535 reg &= ~0xffffff00; 536 reg |= PCI_CLASS_BRIDGE_PCI_NORMAL << 8; 537 advk_writel(pcie, reg, PCIE_CORE_DEV_REV_REG); 538 539 /* Disable Root Bridge I/O space, memory space and bus mastering */ 540 reg = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 541 reg &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 542 advk_writel(pcie, reg, PCIE_CORE_CMD_STATUS_REG); 543 544 /* Set Advanced Error Capabilities and Control PF0 register */ 545 reg = PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX | 546 PCIE_CORE_ERR_CAPCTL_ECRC_CHK_TX_EN | 547 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK | 548 PCIE_CORE_ERR_CAPCTL_ECRC_CHCK_RCV; 549 advk_writel(pcie, reg, PCIE_CORE_ERR_CAPCTL_REG); 550 551 /* Set PCIe Device Control register */ 552 reg = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 553 reg &= ~PCI_EXP_DEVCTL_RELAX_EN; 554 reg &= ~PCI_EXP_DEVCTL_NOSNOOP_EN; 555 reg &= ~PCI_EXP_DEVCTL_PAYLOAD; 556 reg &= ~PCI_EXP_DEVCTL_READRQ; 557 reg |= PCI_EXP_DEVCTL_PAYLOAD_512B; 558 reg |= PCI_EXP_DEVCTL_READRQ_512B; 559 advk_writel(pcie, reg, PCIE_CORE_PCIEXP_CAP + PCI_EXP_DEVCTL); 560 561 /* Program PCIe Control 2 to disable strict ordering */ 562 reg = PCIE_CORE_CTRL2_RESERVED | 563 PCIE_CORE_CTRL2_TD_ENABLE; 564 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 565 566 /* Set lane X1 */ 567 reg = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 568 reg &= ~LANE_CNT_MSK; 569 reg |= LANE_COUNT_1; 570 advk_writel(pcie, reg, PCIE_CORE_CTRL0_REG); 571 572 /* Set MSI address */ 573 msi_addr = virt_to_phys(pcie); 574 advk_writel(pcie, lower_32_bits(msi_addr), PCIE_MSI_ADDR_LOW_REG); 575 advk_writel(pcie, upper_32_bits(msi_addr), PCIE_MSI_ADDR_HIGH_REG); 576 577 /* Enable MSI */ 578 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 579 reg |= PCIE_CORE_CTRL2_MSI_ENABLE; 580 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 581 582 /* Clear all interrupts */ 583 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG); 584 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 585 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 586 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 587 588 /* Disable All ISR0/1 and MSI Sources */ 589 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG); 590 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 591 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG); 592 593 /* Unmask summary MSI interrupt */ 594 reg = advk_readl(pcie, PCIE_ISR0_MASK_REG); 595 reg &= ~PCIE_ISR0_MSI_INT_PENDING; 596 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 597 598 /* Unmask PME interrupt for processing of PME requester */ 599 reg = advk_readl(pcie, PCIE_ISR0_MASK_REG); 600 reg &= ~PCIE_MSG_PM_PME_MASK; 601 advk_writel(pcie, reg, PCIE_ISR0_MASK_REG); 602 603 /* Enable summary interrupt for GIC SPI source */ 604 reg = PCIE_IRQ_ALL_MASK & (~PCIE_IRQ_ENABLE_INTS_MASK); 605 advk_writel(pcie, reg, HOST_CTRL_INT_MASK_REG); 606 607 /* 608 * Enable AXI address window location generation: 609 * When it is enabled, the default outbound window 610 * configurations (Default User Field: 0xD0074CFC) 611 * are used to transparent address translation for 612 * the outbound transactions. Thus, PCIe address 613 * windows are not required for transparent memory 614 * access when default outbound window configuration 615 * is set for memory access. 616 */ 617 reg = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 618 reg |= PCIE_CORE_CTRL2_OB_WIN_ENABLE; 619 advk_writel(pcie, reg, PCIE_CORE_CTRL2_REG); 620 621 /* 622 * Set memory access in Default User Field so it 623 * is not required to configure PCIe address for 624 * transparent memory access. 625 */ 626 advk_writel(pcie, OB_WIN_TYPE_MEM, OB_WIN_DEFAULT_ACTIONS); 627 628 /* 629 * Bypass the address window mapping for PIO: 630 * Since PIO access already contains all required 631 * info over AXI interface by PIO registers, the 632 * address window is not required. 633 */ 634 reg = advk_readl(pcie, PIO_CTRL); 635 reg |= PIO_CTRL_ADDR_WIN_DISABLE; 636 advk_writel(pcie, reg, PIO_CTRL); 637 638 /* 639 * Configure PCIe address windows for non-memory or 640 * non-transparent access as by default PCIe uses 641 * transparent memory access. 642 */ 643 for (i = 0; i < pcie->wins_count; i++) 644 advk_pcie_set_ob_win(pcie, i, 645 pcie->wins[i].match, pcie->wins[i].remap, 646 pcie->wins[i].mask, pcie->wins[i].actions); 647 648 /* Disable remaining PCIe outbound windows */ 649 for (i = pcie->wins_count; i < OB_WIN_COUNT; i++) 650 advk_pcie_disable_ob_win(pcie, i); 651 652 advk_pcie_train_link(pcie); 653 } 654 655 static int advk_pcie_check_pio_status(struct advk_pcie *pcie, bool allow_rrs, u32 *val) 656 { 657 struct device *dev = &pcie->pdev->dev; 658 u32 reg; 659 unsigned int status; 660 char *strcomp_status, *str_posted; 661 int ret; 662 663 reg = advk_readl(pcie, PIO_STAT); 664 status = (reg & PIO_COMPLETION_STATUS_MASK) >> 665 PIO_COMPLETION_STATUS_SHIFT; 666 667 /* 668 * According to HW spec, the PIO status check sequence as below: 669 * 1) even if COMPLETION_STATUS(bit9:7) indicates successful, 670 * it still needs to check Error Status(bit11), only when this bit 671 * indicates no error happen, the operation is successful. 672 * 2) value Unsupported Request(1) of COMPLETION_STATUS(bit9:7) only 673 * means a PIO write error, and for PIO read it is successful with 674 * a read value of 0xFFFFFFFF. 675 * 3) value Config Request Retry Status(RRS) of COMPLETION_STATUS(bit9:7) 676 * only means a PIO write error, and for PIO read it is successful 677 * with a read value of 0xFFFF0001. 678 * 4) value Completer Abort (CA) of COMPLETION_STATUS(bit9:7) means 679 * error for both PIO read and PIO write operation. 680 * 5) other errors are indicated as 'unknown'. 681 */ 682 switch (status) { 683 case PIO_COMPLETION_STATUS_OK: 684 if (reg & PIO_ERR_STATUS) { 685 strcomp_status = "COMP_ERR"; 686 ret = -EFAULT; 687 break; 688 } 689 /* Get the read result */ 690 if (val) 691 *val = advk_readl(pcie, PIO_RD_DATA); 692 /* No error */ 693 strcomp_status = NULL; 694 ret = 0; 695 break; 696 case PIO_COMPLETION_STATUS_UR: 697 strcomp_status = "UR"; 698 ret = -EOPNOTSUPP; 699 break; 700 case PIO_COMPLETION_STATUS_RRS: 701 if (allow_rrs && val) { 702 /* PCIe r6.0, sec 2.3.2, says: 703 * If Configuration RRS Software Visibility is enabled: 704 * For a Configuration Read Request that includes both 705 * bytes of the Vendor ID field of a device Function's 706 * Configuration Space Header, the Root Complex must 707 * complete the Request to the host by returning a 708 * read-data value of 0001h for the Vendor ID field and 709 * all '1's for any additional bytes included in the 710 * request. 711 * 712 * So RRS in this case is not an error status. 713 */ 714 *val = CFG_RD_RRS_VAL; 715 strcomp_status = NULL; 716 ret = 0; 717 break; 718 } 719 /* PCIe r6.0, sec 2.3.2, says: 720 * If RRS Software Visibility is not enabled, the Root Complex 721 * must re-issue the Configuration Request as a new Request. 722 * If RRS Software Visibility is enabled: For a Configuration 723 * Write Request or for any other Configuration Read Request, 724 * the Root Complex must re-issue the Configuration Request as 725 * a new Request. 726 * A Root Complex implementation may choose to limit the number 727 * of Configuration Request/RRS Completion Status loops before 728 * determining that something is wrong with the target of the 729 * Request and taking appropriate action, e.g., complete the 730 * Request to the host as a failed transaction. 731 * 732 * So return -EAGAIN and caller (pci-aardvark.c driver) will 733 * re-issue request again up to the PIO_RETRY_CNT retries. 734 */ 735 strcomp_status = "RRS"; 736 ret = -EAGAIN; 737 break; 738 case PIO_COMPLETION_STATUS_CA: 739 strcomp_status = "CA"; 740 ret = -ECANCELED; 741 break; 742 default: 743 strcomp_status = "Unknown"; 744 ret = -EINVAL; 745 break; 746 } 747 748 if (!strcomp_status) 749 return ret; 750 751 if (reg & PIO_NON_POSTED_REQ) 752 str_posted = "Non-posted"; 753 else 754 str_posted = "Posted"; 755 756 dev_dbg(dev, "%s PIO Response Status: %s, %#x @ %#x\n", 757 str_posted, strcomp_status, reg, advk_readl(pcie, PIO_ADDR_LS)); 758 759 return ret; 760 } 761 762 static int advk_pcie_wait_pio(struct advk_pcie *pcie) 763 { 764 struct device *dev = &pcie->pdev->dev; 765 int i; 766 767 for (i = 1; i <= PIO_RETRY_CNT; i++) { 768 u32 start, isr; 769 770 start = advk_readl(pcie, PIO_START); 771 isr = advk_readl(pcie, PIO_ISR); 772 if (!start && isr) 773 return i; 774 udelay(PIO_RETRY_DELAY); 775 } 776 777 dev_err(dev, "PIO read/write transfer time out\n"); 778 return -ETIMEDOUT; 779 } 780 781 static pci_bridge_emul_read_status_t 782 advk_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge, 783 int reg, u32 *value) 784 { 785 struct advk_pcie *pcie = bridge->data; 786 787 switch (reg) { 788 case PCI_COMMAND: 789 *value = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 790 return PCI_BRIDGE_EMUL_HANDLED; 791 792 case PCI_INTERRUPT_LINE: { 793 /* 794 * From the whole 32bit register we support reading from HW only 795 * two bits: PCI_BRIDGE_CTL_BUS_RESET and PCI_BRIDGE_CTL_SERR. 796 * Other bits are retrieved only from emulated config buffer. 797 */ 798 __le32 *cfgspace = (__le32 *)&bridge->conf; 799 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]); 800 if (advk_readl(pcie, PCIE_ISR0_MASK_REG) & PCIE_ISR0_ERR_MASK) 801 val &= ~(PCI_BRIDGE_CTL_SERR << 16); 802 else 803 val |= PCI_BRIDGE_CTL_SERR << 16; 804 if (advk_readl(pcie, PCIE_CORE_CTRL1_REG) & HOT_RESET_GEN) 805 val |= PCI_BRIDGE_CTL_BUS_RESET << 16; 806 else 807 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16); 808 *value = val; 809 return PCI_BRIDGE_EMUL_HANDLED; 810 } 811 812 default: 813 return PCI_BRIDGE_EMUL_NOT_HANDLED; 814 } 815 } 816 817 static void 818 advk_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge, 819 int reg, u32 old, u32 new, u32 mask) 820 { 821 struct advk_pcie *pcie = bridge->data; 822 823 switch (reg) { 824 case PCI_COMMAND: 825 advk_writel(pcie, new, PCIE_CORE_CMD_STATUS_REG); 826 break; 827 828 case PCI_INTERRUPT_LINE: 829 /* 830 * According to Figure 6-3: Pseudo Logic Diagram for Error 831 * Message Controls in PCIe base specification, SERR# Enable bit 832 * in Bridge Control register enable receiving of ERR_* messages 833 */ 834 if (mask & (PCI_BRIDGE_CTL_SERR << 16)) { 835 u32 val = advk_readl(pcie, PCIE_ISR0_MASK_REG); 836 if (new & (PCI_BRIDGE_CTL_SERR << 16)) 837 val &= ~PCIE_ISR0_ERR_MASK; 838 else 839 val |= PCIE_ISR0_ERR_MASK; 840 advk_writel(pcie, val, PCIE_ISR0_MASK_REG); 841 } 842 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) { 843 u32 val = advk_readl(pcie, PCIE_CORE_CTRL1_REG); 844 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16)) 845 val |= HOT_RESET_GEN; 846 else 847 val &= ~HOT_RESET_GEN; 848 advk_writel(pcie, val, PCIE_CORE_CTRL1_REG); 849 } 850 break; 851 852 default: 853 break; 854 } 855 } 856 857 static pci_bridge_emul_read_status_t 858 advk_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge, 859 int reg, u32 *value) 860 { 861 struct advk_pcie *pcie = bridge->data; 862 863 864 switch (reg) { 865 /* 866 * PCI_EXP_SLTCAP, PCI_EXP_SLTCTL, PCI_EXP_RTCTL and PCI_EXP_RTSTA are 867 * also supported, but do not need to be handled here, because their 868 * values are stored in emulated config space buffer, and we read them 869 * from there when needed. 870 */ 871 872 case PCI_EXP_LNKCAP: { 873 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 874 /* 875 * PCI_EXP_LNKCAP_DLLLARC bit is hardwired in aardvark HW to 0. 876 * But support for PCI_EXP_LNKSTA_DLLLA is emulated via ltssm 877 * state so explicitly enable PCI_EXP_LNKCAP_DLLLARC flag. 878 */ 879 val |= PCI_EXP_LNKCAP_DLLLARC; 880 *value = val; 881 return PCI_BRIDGE_EMUL_HANDLED; 882 } 883 884 case PCI_EXP_LNKCTL: { 885 /* u32 contains both PCI_EXP_LNKCTL and PCI_EXP_LNKSTA */ 886 u32 val = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg) & 887 ~(PCI_EXP_LNKSTA_LT << 16); 888 if (advk_pcie_link_training(pcie)) 889 val |= (PCI_EXP_LNKSTA_LT << 16); 890 if (advk_pcie_link_active(pcie)) 891 val |= (PCI_EXP_LNKSTA_DLLLA << 16); 892 *value = val; 893 return PCI_BRIDGE_EMUL_HANDLED; 894 } 895 896 case PCI_EXP_DEVCAP: 897 case PCI_EXP_DEVCTL: 898 case PCI_EXP_DEVCAP2: 899 case PCI_EXP_DEVCTL2: 900 case PCI_EXP_LNKCAP2: 901 case PCI_EXP_LNKCTL2: 902 *value = advk_readl(pcie, PCIE_CORE_PCIEXP_CAP + reg); 903 return PCI_BRIDGE_EMUL_HANDLED; 904 905 default: 906 return PCI_BRIDGE_EMUL_NOT_HANDLED; 907 } 908 909 } 910 911 static void 912 advk_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge, 913 int reg, u32 old, u32 new, u32 mask) 914 { 915 struct advk_pcie *pcie = bridge->data; 916 917 switch (reg) { 918 case PCI_EXP_LNKCTL: 919 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 920 if (new & PCI_EXP_LNKCTL_RL) 921 advk_pcie_wait_for_retrain(pcie); 922 break; 923 924 case PCI_EXP_RTCTL: { 925 u16 rootctl = le16_to_cpu(bridge->pcie_conf.rootctl); 926 /* Only emulation of PMEIE and RRS_SVE bits is provided */ 927 rootctl &= PCI_EXP_RTCTL_PMEIE | PCI_EXP_RTCTL_RRS_SVE; 928 bridge->pcie_conf.rootctl = cpu_to_le16(rootctl); 929 break; 930 } 931 932 /* 933 * PCI_EXP_RTSTA is also supported, but does not need to be handled 934 * here, because its value is stored in emulated config space buffer, 935 * and we write it there when needed. 936 */ 937 938 case PCI_EXP_DEVCTL: 939 case PCI_EXP_DEVCTL2: 940 case PCI_EXP_LNKCTL2: 941 advk_writel(pcie, new, PCIE_CORE_PCIEXP_CAP + reg); 942 break; 943 944 default: 945 break; 946 } 947 } 948 949 static pci_bridge_emul_read_status_t 950 advk_pci_bridge_emul_ext_conf_read(struct pci_bridge_emul *bridge, 951 int reg, u32 *value) 952 { 953 struct advk_pcie *pcie = bridge->data; 954 955 switch (reg) { 956 case 0: 957 *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg); 958 959 /* 960 * PCI_EXT_CAP_NEXT bits are set to offset 0x150, but Armada 961 * 3700 Functional Specification does not document registers 962 * at those addresses. 963 * 964 * Thus we clear PCI_EXT_CAP_NEXT bits to make Advanced Error 965 * Reporting Capability header the last Extended Capability. 966 * If we obtain documentation for those registers in the 967 * future, this can be changed. 968 */ 969 *value &= 0x000fffff; 970 return PCI_BRIDGE_EMUL_HANDLED; 971 972 case PCI_ERR_UNCOR_STATUS: 973 case PCI_ERR_UNCOR_MASK: 974 case PCI_ERR_UNCOR_SEVER: 975 case PCI_ERR_COR_STATUS: 976 case PCI_ERR_COR_MASK: 977 case PCI_ERR_CAP: 978 case PCI_ERR_HEADER_LOG + 0: 979 case PCI_ERR_HEADER_LOG + 4: 980 case PCI_ERR_HEADER_LOG + 8: 981 case PCI_ERR_HEADER_LOG + 12: 982 case PCI_ERR_ROOT_COMMAND: 983 case PCI_ERR_ROOT_STATUS: 984 case PCI_ERR_ROOT_ERR_SRC: 985 *value = advk_readl(pcie, PCIE_CORE_PCIERR_CAP + reg); 986 return PCI_BRIDGE_EMUL_HANDLED; 987 988 default: 989 return PCI_BRIDGE_EMUL_NOT_HANDLED; 990 } 991 } 992 993 static void 994 advk_pci_bridge_emul_ext_conf_write(struct pci_bridge_emul *bridge, 995 int reg, u32 old, u32 new, u32 mask) 996 { 997 struct advk_pcie *pcie = bridge->data; 998 999 switch (reg) { 1000 /* These are W1C registers, so clear other bits */ 1001 case PCI_ERR_UNCOR_STATUS: 1002 case PCI_ERR_COR_STATUS: 1003 case PCI_ERR_ROOT_STATUS: 1004 new &= mask; 1005 fallthrough; 1006 1007 case PCI_ERR_UNCOR_MASK: 1008 case PCI_ERR_UNCOR_SEVER: 1009 case PCI_ERR_COR_MASK: 1010 case PCI_ERR_CAP: 1011 case PCI_ERR_HEADER_LOG + 0: 1012 case PCI_ERR_HEADER_LOG + 4: 1013 case PCI_ERR_HEADER_LOG + 8: 1014 case PCI_ERR_HEADER_LOG + 12: 1015 case PCI_ERR_ROOT_COMMAND: 1016 case PCI_ERR_ROOT_ERR_SRC: 1017 advk_writel(pcie, new, PCIE_CORE_PCIERR_CAP + reg); 1018 break; 1019 1020 default: 1021 break; 1022 } 1023 } 1024 1025 static const struct pci_bridge_emul_ops advk_pci_bridge_emul_ops = { 1026 .read_base = advk_pci_bridge_emul_base_conf_read, 1027 .write_base = advk_pci_bridge_emul_base_conf_write, 1028 .read_pcie = advk_pci_bridge_emul_pcie_conf_read, 1029 .write_pcie = advk_pci_bridge_emul_pcie_conf_write, 1030 .read_ext = advk_pci_bridge_emul_ext_conf_read, 1031 .write_ext = advk_pci_bridge_emul_ext_conf_write, 1032 }; 1033 1034 /* 1035 * Initialize the configuration space of the PCI-to-PCI bridge 1036 * associated with the given PCIe interface. 1037 */ 1038 static int advk_sw_pci_bridge_init(struct advk_pcie *pcie) 1039 { 1040 struct pci_bridge_emul *bridge = &pcie->bridge; 1041 1042 bridge->conf.vendor = 1043 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) & 0xffff); 1044 bridge->conf.device = 1045 cpu_to_le16(advk_readl(pcie, PCIE_CORE_DEV_ID_REG) >> 16); 1046 bridge->conf.class_revision = 1047 cpu_to_le32(advk_readl(pcie, PCIE_CORE_DEV_REV_REG) & 0xff); 1048 1049 /* Support 32 bits I/O addressing */ 1050 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32; 1051 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32; 1052 1053 /* Support 64 bits memory pref */ 1054 bridge->conf.pref_mem_base = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 1055 bridge->conf.pref_mem_limit = cpu_to_le16(PCI_PREF_RANGE_TYPE_64); 1056 1057 /* Support interrupt A for MSI feature */ 1058 bridge->conf.intpin = PCI_INTERRUPT_INTA; 1059 1060 /* 1061 * Aardvark HW provides PCIe Capability structure in version 2 and 1062 * indicate slot support, which is emulated. 1063 */ 1064 bridge->pcie_conf.cap = cpu_to_le16(2 | PCI_EXP_FLAGS_SLOT); 1065 1066 /* 1067 * Set Presence Detect State bit permanently since there is no support 1068 * for unplugging the card nor detecting whether it is plugged. (If a 1069 * platform exists in the future that supports it, via a GPIO for 1070 * example, it should be implemented via this bit.) 1071 * 1072 * Set physical slot number to 1 since there is only one port and zero 1073 * value is reserved for ports within the same silicon as Root Port 1074 * which is not our case. 1075 */ 1076 bridge->pcie_conf.slotcap = cpu_to_le32(FIELD_PREP(PCI_EXP_SLTCAP_PSN, 1077 1)); 1078 bridge->pcie_conf.slotsta = cpu_to_le16(PCI_EXP_SLTSTA_PDS); 1079 1080 /* Indicates supports for Completion Retry Status */ 1081 bridge->pcie_conf.rootcap = cpu_to_le16(PCI_EXP_RTCAP_RRS_SV); 1082 1083 bridge->subsystem_vendor_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) & 0xffff; 1084 bridge->subsystem_id = advk_readl(pcie, PCIE_CORE_SSDEV_ID_REG) >> 16; 1085 bridge->has_pcie = true; 1086 bridge->pcie_start = PCIE_CORE_PCIEXP_CAP; 1087 bridge->data = pcie; 1088 bridge->ops = &advk_pci_bridge_emul_ops; 1089 1090 return pci_bridge_emul_init(bridge, 0); 1091 } 1092 1093 static bool advk_pcie_valid_device(struct advk_pcie *pcie, struct pci_bus *bus, 1094 int devfn) 1095 { 1096 if (pci_is_root_bus(bus) && PCI_SLOT(devfn) != 0) 1097 return false; 1098 1099 /* 1100 * If the link goes down after we check for link-up, we have a problem: 1101 * if a PIO request is executed while link-down, the whole controller 1102 * gets stuck in a non-functional state, and even after link comes up 1103 * again, PIO requests won't work anymore, and a reset of the whole PCIe 1104 * controller is needed. Therefore we need to prevent sending PIO 1105 * requests while the link is down. 1106 */ 1107 if (!pci_is_root_bus(bus) && !advk_pcie_link_up(pcie)) 1108 return false; 1109 1110 return true; 1111 } 1112 1113 static bool advk_pcie_pio_is_running(struct advk_pcie *pcie) 1114 { 1115 struct device *dev = &pcie->pdev->dev; 1116 1117 /* 1118 * Trying to start a new PIO transfer when previous has not completed 1119 * cause External Abort on CPU which results in kernel panic: 1120 * 1121 * SError Interrupt on CPU0, code 0xbf000002 -- SError 1122 * Kernel panic - not syncing: Asynchronous SError Interrupt 1123 * 1124 * Functions advk_pcie_rd_conf() and advk_pcie_wr_conf() are protected 1125 * by raw_spin_lock_irqsave() at pci_lock_config() level to prevent 1126 * concurrent calls at the same time. But because PIO transfer may take 1127 * about 1.5s when link is down or card is disconnected, it means that 1128 * advk_pcie_wait_pio() does not always have to wait for completion. 1129 * 1130 * Some versions of ARM Trusted Firmware handles this External Abort at 1131 * EL3 level and mask it to prevent kernel panic. Relevant TF-A commit: 1132 * https://git.trustedfirmware.org/TF-A/trusted-firmware-a.git/commit/?id=3c7dcdac5c50 1133 */ 1134 if (advk_readl(pcie, PIO_START)) { 1135 dev_err(dev, "Previous PIO read/write transfer is still running\n"); 1136 return true; 1137 } 1138 1139 return false; 1140 } 1141 1142 static int advk_pcie_rd_conf(struct pci_bus *bus, u32 devfn, 1143 int where, int size, u32 *val) 1144 { 1145 struct advk_pcie *pcie = bus->sysdata; 1146 int retry_count; 1147 bool allow_rrs; 1148 u32 reg; 1149 int ret; 1150 1151 if (!advk_pcie_valid_device(pcie, bus, devfn)) 1152 return PCIBIOS_DEVICE_NOT_FOUND; 1153 1154 if (pci_is_root_bus(bus)) 1155 return pci_bridge_emul_conf_read(&pcie->bridge, where, 1156 size, val); 1157 1158 /* 1159 * Configuration Request Retry Status (RRS) is possible to return 1160 * only when reading both bytes from PCI_VENDOR_ID at once and 1161 * RRS_SVE flag on Root Port is enabled. 1162 */ 1163 allow_rrs = (where == PCI_VENDOR_ID) && (size >= 2) && 1164 (le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & 1165 PCI_EXP_RTCTL_RRS_SVE); 1166 1167 if (advk_pcie_pio_is_running(pcie)) 1168 goto try_rrs; 1169 1170 /* Program the control register */ 1171 reg = advk_readl(pcie, PIO_CTRL); 1172 reg &= ~PIO_CTRL_TYPE_MASK; 1173 if (pci_is_root_bus(bus->parent)) 1174 reg |= PCIE_CONFIG_RD_TYPE0; 1175 else 1176 reg |= PCIE_CONFIG_RD_TYPE1; 1177 advk_writel(pcie, reg, PIO_CTRL); 1178 1179 /* Program the address registers */ 1180 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 1181 advk_writel(pcie, reg, PIO_ADDR_LS); 1182 advk_writel(pcie, 0, PIO_ADDR_MS); 1183 1184 /* Program the data strobe */ 1185 advk_writel(pcie, 0xf, PIO_WR_DATA_STRB); 1186 1187 retry_count = 0; 1188 do { 1189 /* Clear PIO DONE ISR and start the transfer */ 1190 advk_writel(pcie, 1, PIO_ISR); 1191 advk_writel(pcie, 1, PIO_START); 1192 1193 ret = advk_pcie_wait_pio(pcie); 1194 if (ret < 0) 1195 goto try_rrs; 1196 1197 retry_count += ret; 1198 1199 /* Check PIO status and get the read result */ 1200 ret = advk_pcie_check_pio_status(pcie, allow_rrs, val); 1201 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 1202 1203 if (ret < 0) 1204 goto fail; 1205 1206 if (size == 1) 1207 *val = (*val >> (8 * (where & 3))) & 0xff; 1208 else if (size == 2) 1209 *val = (*val >> (8 * (where & 3))) & 0xffff; 1210 1211 return PCIBIOS_SUCCESSFUL; 1212 1213 try_rrs: 1214 /* 1215 * If it is possible, return Configuration Request Retry Status so 1216 * that caller tries to issue the request again instead of failing. 1217 */ 1218 if (allow_rrs) { 1219 *val = CFG_RD_RRS_VAL; 1220 return PCIBIOS_SUCCESSFUL; 1221 } 1222 1223 fail: 1224 *val = 0xffffffff; 1225 return PCIBIOS_SET_FAILED; 1226 } 1227 1228 static int advk_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 1229 int where, int size, u32 val) 1230 { 1231 struct advk_pcie *pcie = bus->sysdata; 1232 u32 reg; 1233 u32 data_strobe = 0x0; 1234 int retry_count; 1235 int offset; 1236 int ret; 1237 1238 if (!advk_pcie_valid_device(pcie, bus, devfn)) 1239 return PCIBIOS_DEVICE_NOT_FOUND; 1240 1241 if (pci_is_root_bus(bus)) 1242 return pci_bridge_emul_conf_write(&pcie->bridge, where, 1243 size, val); 1244 1245 if (where % size) 1246 return PCIBIOS_SET_FAILED; 1247 1248 if (advk_pcie_pio_is_running(pcie)) 1249 return PCIBIOS_SET_FAILED; 1250 1251 /* Program the control register */ 1252 reg = advk_readl(pcie, PIO_CTRL); 1253 reg &= ~PIO_CTRL_TYPE_MASK; 1254 if (pci_is_root_bus(bus->parent)) 1255 reg |= PCIE_CONFIG_WR_TYPE0; 1256 else 1257 reg |= PCIE_CONFIG_WR_TYPE1; 1258 advk_writel(pcie, reg, PIO_CTRL); 1259 1260 /* Program the address registers */ 1261 reg = ALIGN_DOWN(PCIE_ECAM_OFFSET(bus->number, devfn, where), 4); 1262 advk_writel(pcie, reg, PIO_ADDR_LS); 1263 advk_writel(pcie, 0, PIO_ADDR_MS); 1264 1265 /* Calculate the write strobe */ 1266 offset = where & 0x3; 1267 reg = val << (8 * offset); 1268 data_strobe = GENMASK(size - 1, 0) << offset; 1269 1270 /* Program the data register */ 1271 advk_writel(pcie, reg, PIO_WR_DATA); 1272 1273 /* Program the data strobe */ 1274 advk_writel(pcie, data_strobe, PIO_WR_DATA_STRB); 1275 1276 retry_count = 0; 1277 do { 1278 /* Clear PIO DONE ISR and start the transfer */ 1279 advk_writel(pcie, 1, PIO_ISR); 1280 advk_writel(pcie, 1, PIO_START); 1281 1282 ret = advk_pcie_wait_pio(pcie); 1283 if (ret < 0) 1284 return PCIBIOS_SET_FAILED; 1285 1286 retry_count += ret; 1287 1288 ret = advk_pcie_check_pio_status(pcie, false, NULL); 1289 } while (ret == -EAGAIN && retry_count < PIO_RETRY_CNT); 1290 1291 return ret < 0 ? PCIBIOS_SET_FAILED : PCIBIOS_SUCCESSFUL; 1292 } 1293 1294 static struct pci_ops advk_pcie_ops = { 1295 .read = advk_pcie_rd_conf, 1296 .write = advk_pcie_wr_conf, 1297 }; 1298 1299 static void advk_msi_irq_compose_msi_msg(struct irq_data *data, 1300 struct msi_msg *msg) 1301 { 1302 struct advk_pcie *pcie = irq_data_get_irq_chip_data(data); 1303 phys_addr_t msi_addr = virt_to_phys(pcie); 1304 1305 msg->address_lo = lower_32_bits(msi_addr); 1306 msg->address_hi = upper_32_bits(msi_addr); 1307 msg->data = data->hwirq; 1308 } 1309 1310 static void advk_msi_irq_mask(struct irq_data *d) 1311 { 1312 struct advk_pcie *pcie = d->domain->host_data; 1313 irq_hw_number_t hwirq = irqd_to_hwirq(d); 1314 unsigned long flags; 1315 u32 mask; 1316 1317 raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags); 1318 mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 1319 mask |= BIT(hwirq); 1320 advk_writel(pcie, mask, PCIE_MSI_MASK_REG); 1321 raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags); 1322 } 1323 1324 static void advk_msi_irq_unmask(struct irq_data *d) 1325 { 1326 struct advk_pcie *pcie = d->domain->host_data; 1327 irq_hw_number_t hwirq = irqd_to_hwirq(d); 1328 unsigned long flags; 1329 u32 mask; 1330 1331 raw_spin_lock_irqsave(&pcie->msi_irq_lock, flags); 1332 mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 1333 mask &= ~BIT(hwirq); 1334 advk_writel(pcie, mask, PCIE_MSI_MASK_REG); 1335 raw_spin_unlock_irqrestore(&pcie->msi_irq_lock, flags); 1336 } 1337 1338 static struct irq_chip advk_msi_bottom_irq_chip = { 1339 .name = "MSI", 1340 .irq_compose_msi_msg = advk_msi_irq_compose_msi_msg, 1341 .irq_mask = advk_msi_irq_mask, 1342 .irq_unmask = advk_msi_irq_unmask, 1343 }; 1344 1345 static int advk_msi_irq_domain_alloc(struct irq_domain *domain, 1346 unsigned int virq, 1347 unsigned int nr_irqs, void *args) 1348 { 1349 struct advk_pcie *pcie = domain->host_data; 1350 int hwirq, i; 1351 1352 mutex_lock(&pcie->msi_used_lock); 1353 hwirq = bitmap_find_free_region(pcie->msi_used, MSI_IRQ_NUM, 1354 order_base_2(nr_irqs)); 1355 mutex_unlock(&pcie->msi_used_lock); 1356 if (hwirq < 0) 1357 return -ENOSPC; 1358 1359 for (i = 0; i < nr_irqs; i++) 1360 irq_domain_set_info(domain, virq + i, hwirq + i, 1361 &advk_msi_bottom_irq_chip, 1362 domain->host_data, handle_simple_irq, 1363 NULL, NULL); 1364 1365 return 0; 1366 } 1367 1368 static void advk_msi_irq_domain_free(struct irq_domain *domain, 1369 unsigned int virq, unsigned int nr_irqs) 1370 { 1371 struct irq_data *d = irq_domain_get_irq_data(domain, virq); 1372 struct advk_pcie *pcie = domain->host_data; 1373 1374 mutex_lock(&pcie->msi_used_lock); 1375 bitmap_release_region(pcie->msi_used, d->hwirq, order_base_2(nr_irqs)); 1376 mutex_unlock(&pcie->msi_used_lock); 1377 } 1378 1379 static const struct irq_domain_ops advk_msi_domain_ops = { 1380 .alloc = advk_msi_irq_domain_alloc, 1381 .free = advk_msi_irq_domain_free, 1382 }; 1383 1384 static void advk_pcie_irq_mask(struct irq_data *d) 1385 { 1386 struct advk_pcie *pcie = d->domain->host_data; 1387 irq_hw_number_t hwirq = irqd_to_hwirq(d); 1388 unsigned long flags; 1389 u32 mask; 1390 1391 raw_spin_lock_irqsave(&pcie->irq_lock, flags); 1392 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 1393 mask |= PCIE_ISR1_INTX_ASSERT(hwirq); 1394 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 1395 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 1396 } 1397 1398 static void advk_pcie_irq_unmask(struct irq_data *d) 1399 { 1400 struct advk_pcie *pcie = d->domain->host_data; 1401 irq_hw_number_t hwirq = irqd_to_hwirq(d); 1402 unsigned long flags; 1403 u32 mask; 1404 1405 raw_spin_lock_irqsave(&pcie->irq_lock, flags); 1406 mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 1407 mask &= ~PCIE_ISR1_INTX_ASSERT(hwirq); 1408 advk_writel(pcie, mask, PCIE_ISR1_MASK_REG); 1409 raw_spin_unlock_irqrestore(&pcie->irq_lock, flags); 1410 } 1411 1412 static int advk_pcie_irq_map(struct irq_domain *h, 1413 unsigned int virq, irq_hw_number_t hwirq) 1414 { 1415 struct advk_pcie *pcie = h->host_data; 1416 1417 irq_set_status_flags(virq, IRQ_LEVEL); 1418 irq_set_chip_and_handler(virq, &pcie->irq_chip, 1419 handle_level_irq); 1420 irq_set_chip_data(virq, pcie); 1421 1422 return 0; 1423 } 1424 1425 static const struct irq_domain_ops advk_pcie_irq_domain_ops = { 1426 .map = advk_pcie_irq_map, 1427 .xlate = irq_domain_xlate_onecell, 1428 }; 1429 1430 #define ADVK_MSI_FLAGS_REQUIRED (MSI_FLAG_USE_DEF_DOM_OPS | \ 1431 MSI_FLAG_USE_DEF_CHIP_OPS | \ 1432 MSI_FLAG_PCI_MSI_MASK_PARENT | \ 1433 MSI_FLAG_NO_AFFINITY) 1434 #define ADVK_MSI_FLAGS_SUPPORTED (MSI_GENERIC_FLAGS_MASK | \ 1435 MSI_FLAG_PCI_MSIX | \ 1436 MSI_FLAG_MULTI_PCI_MSI) 1437 1438 static const struct msi_parent_ops advk_msi_parent_ops = { 1439 .required_flags = ADVK_MSI_FLAGS_REQUIRED, 1440 .supported_flags = ADVK_MSI_FLAGS_SUPPORTED, 1441 .bus_select_token = DOMAIN_BUS_PCI_MSI, 1442 .prefix = "advk-", 1443 .init_dev_msi_info = msi_lib_init_dev_msi_info, 1444 }; 1445 1446 static int advk_pcie_init_msi_irq_domain(struct advk_pcie *pcie) 1447 { 1448 struct device *dev = &pcie->pdev->dev; 1449 1450 raw_spin_lock_init(&pcie->msi_irq_lock); 1451 mutex_init(&pcie->msi_used_lock); 1452 1453 struct irq_domain_info info = { 1454 .fwnode = dev_fwnode(dev), 1455 .ops = &advk_msi_domain_ops, 1456 .host_data = pcie, 1457 .size = MSI_IRQ_NUM, 1458 }; 1459 1460 pcie->msi_inner_domain = msi_create_parent_irq_domain(&info, &advk_msi_parent_ops); 1461 if (!pcie->msi_inner_domain) 1462 return -ENOMEM; 1463 1464 return 0; 1465 } 1466 1467 static void advk_pcie_remove_msi_irq_domain(struct advk_pcie *pcie) 1468 { 1469 irq_domain_remove(pcie->msi_inner_domain); 1470 } 1471 1472 static int advk_pcie_init_irq_domain(struct advk_pcie *pcie) 1473 { 1474 struct device *dev = &pcie->pdev->dev; 1475 struct device_node *node = dev->of_node; 1476 struct device_node *pcie_intc_node; 1477 struct irq_chip *irq_chip; 1478 int ret = 0; 1479 1480 raw_spin_lock_init(&pcie->irq_lock); 1481 1482 pcie_intc_node = of_get_next_child(node, NULL); 1483 if (!pcie_intc_node) { 1484 dev_err(dev, "No PCIe Intc node found\n"); 1485 return -ENODEV; 1486 } 1487 1488 irq_chip = &pcie->irq_chip; 1489 1490 irq_chip->name = devm_kasprintf(dev, GFP_KERNEL, "%s-irq", 1491 dev_name(dev)); 1492 if (!irq_chip->name) { 1493 ret = -ENOMEM; 1494 goto out_put_node; 1495 } 1496 1497 irq_chip->irq_mask = advk_pcie_irq_mask; 1498 irq_chip->irq_unmask = advk_pcie_irq_unmask; 1499 1500 pcie->irq_domain = irq_domain_create_linear(of_fwnode_handle(pcie_intc_node), PCI_NUM_INTX, 1501 &advk_pcie_irq_domain_ops, pcie); 1502 if (!pcie->irq_domain) { 1503 dev_err(dev, "Failed to get a INTx IRQ domain\n"); 1504 ret = -ENOMEM; 1505 goto out_put_node; 1506 } 1507 1508 out_put_node: 1509 of_node_put(pcie_intc_node); 1510 return ret; 1511 } 1512 1513 static void advk_pcie_remove_irq_domain(struct advk_pcie *pcie) 1514 { 1515 irq_domain_remove(pcie->irq_domain); 1516 } 1517 1518 static struct irq_chip advk_rp_irq_chip = { 1519 .name = "advk-RP", 1520 }; 1521 1522 static int advk_pcie_rp_irq_map(struct irq_domain *h, 1523 unsigned int virq, irq_hw_number_t hwirq) 1524 { 1525 struct advk_pcie *pcie = h->host_data; 1526 1527 irq_set_chip_and_handler(virq, &advk_rp_irq_chip, handle_simple_irq); 1528 irq_set_chip_data(virq, pcie); 1529 1530 return 0; 1531 } 1532 1533 static const struct irq_domain_ops advk_pcie_rp_irq_domain_ops = { 1534 .map = advk_pcie_rp_irq_map, 1535 .xlate = irq_domain_xlate_onecell, 1536 }; 1537 1538 static int advk_pcie_init_rp_irq_domain(struct advk_pcie *pcie) 1539 { 1540 pcie->rp_irq_domain = irq_domain_create_linear(NULL, 1, &advk_pcie_rp_irq_domain_ops, pcie); 1541 if (!pcie->rp_irq_domain) { 1542 dev_err(&pcie->pdev->dev, "Failed to add Root Port IRQ domain\n"); 1543 return -ENOMEM; 1544 } 1545 1546 return 0; 1547 } 1548 1549 static void advk_pcie_remove_rp_irq_domain(struct advk_pcie *pcie) 1550 { 1551 irq_domain_remove(pcie->rp_irq_domain); 1552 } 1553 1554 static void advk_pcie_handle_pme(struct advk_pcie *pcie) 1555 { 1556 u32 requester = advk_readl(pcie, PCIE_MSG_LOG_REG) >> 16; 1557 1558 advk_writel(pcie, PCIE_MSG_PM_PME_MASK, PCIE_ISR0_REG); 1559 1560 /* 1561 * PCIE_MSG_LOG_REG contains the last inbound message, so store 1562 * the requester ID only when PME was not asserted yet. 1563 * Also do not trigger PME interrupt when PME is still asserted. 1564 */ 1565 if (!(le32_to_cpu(pcie->bridge.pcie_conf.rootsta) & PCI_EXP_RTSTA_PME)) { 1566 pcie->bridge.pcie_conf.rootsta = cpu_to_le32(requester | PCI_EXP_RTSTA_PME); 1567 1568 /* 1569 * Trigger PME interrupt only if PMEIE bit in Root Control is set. 1570 * Aardvark HW returns zero for PCI_EXP_FLAGS_IRQ, so use PCIe interrupt 0. 1571 */ 1572 if (!(le16_to_cpu(pcie->bridge.pcie_conf.rootctl) & PCI_EXP_RTCTL_PMEIE)) 1573 return; 1574 1575 if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL) 1576 dev_err_ratelimited(&pcie->pdev->dev, "unhandled PME IRQ\n"); 1577 } 1578 } 1579 1580 static void advk_pcie_handle_msi(struct advk_pcie *pcie) 1581 { 1582 u32 msi_val, msi_mask, msi_status, msi_idx; 1583 1584 msi_mask = advk_readl(pcie, PCIE_MSI_MASK_REG); 1585 msi_val = advk_readl(pcie, PCIE_MSI_STATUS_REG); 1586 msi_status = msi_val & ((~msi_mask) & PCIE_MSI_ALL_MASK); 1587 1588 for (msi_idx = 0; msi_idx < MSI_IRQ_NUM; msi_idx++) { 1589 if (!(BIT(msi_idx) & msi_status)) 1590 continue; 1591 1592 advk_writel(pcie, BIT(msi_idx), PCIE_MSI_STATUS_REG); 1593 if (generic_handle_domain_irq(pcie->msi_inner_domain, msi_idx) == -EINVAL) 1594 dev_err_ratelimited(&pcie->pdev->dev, "unexpected MSI 0x%02x\n", msi_idx); 1595 } 1596 1597 advk_writel(pcie, PCIE_ISR0_MSI_INT_PENDING, 1598 PCIE_ISR0_REG); 1599 } 1600 1601 static void advk_pcie_handle_int(struct advk_pcie *pcie) 1602 { 1603 u32 isr0_val, isr0_mask, isr0_status; 1604 u32 isr1_val, isr1_mask, isr1_status; 1605 int i; 1606 1607 isr0_val = advk_readl(pcie, PCIE_ISR0_REG); 1608 isr0_mask = advk_readl(pcie, PCIE_ISR0_MASK_REG); 1609 isr0_status = isr0_val & ((~isr0_mask) & PCIE_ISR0_ALL_MASK); 1610 1611 isr1_val = advk_readl(pcie, PCIE_ISR1_REG); 1612 isr1_mask = advk_readl(pcie, PCIE_ISR1_MASK_REG); 1613 isr1_status = isr1_val & ((~isr1_mask) & PCIE_ISR1_ALL_MASK); 1614 1615 /* Process PME interrupt as the first one to do not miss PME requester id */ 1616 if (isr0_status & PCIE_MSG_PM_PME_MASK) 1617 advk_pcie_handle_pme(pcie); 1618 1619 /* Process ERR interrupt */ 1620 if (isr0_status & PCIE_ISR0_ERR_MASK) { 1621 advk_writel(pcie, PCIE_ISR0_ERR_MASK, PCIE_ISR0_REG); 1622 1623 /* 1624 * Aardvark HW returns zero for PCI_ERR_ROOT_AER_IRQ, so use 1625 * PCIe interrupt 0 1626 */ 1627 if (generic_handle_domain_irq(pcie->rp_irq_domain, 0) == -EINVAL) 1628 dev_err_ratelimited(&pcie->pdev->dev, "unhandled ERR IRQ\n"); 1629 } 1630 1631 /* Process MSI interrupts */ 1632 if (isr0_status & PCIE_ISR0_MSI_INT_PENDING) 1633 advk_pcie_handle_msi(pcie); 1634 1635 /* Process legacy interrupts */ 1636 for (i = 0; i < PCI_NUM_INTX; i++) { 1637 if (!(isr1_status & PCIE_ISR1_INTX_ASSERT(i))) 1638 continue; 1639 1640 advk_writel(pcie, PCIE_ISR1_INTX_ASSERT(i), 1641 PCIE_ISR1_REG); 1642 1643 if (generic_handle_domain_irq(pcie->irq_domain, i) == -EINVAL) 1644 dev_err_ratelimited(&pcie->pdev->dev, "unexpected INT%c IRQ\n", 1645 (char)i + 'A'); 1646 } 1647 } 1648 1649 static irqreturn_t advk_pcie_irq_handler(int irq, void *arg) 1650 { 1651 struct advk_pcie *pcie = arg; 1652 u32 status; 1653 1654 status = advk_readl(pcie, HOST_CTRL_INT_STATUS_REG); 1655 if (!(status & PCIE_IRQ_CORE_INT)) 1656 return IRQ_NONE; 1657 1658 advk_pcie_handle_int(pcie); 1659 1660 /* Clear interrupt */ 1661 advk_writel(pcie, PCIE_IRQ_CORE_INT, HOST_CTRL_INT_STATUS_REG); 1662 1663 return IRQ_HANDLED; 1664 } 1665 1666 static int advk_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 1667 { 1668 struct advk_pcie *pcie = dev->bus->sysdata; 1669 1670 /* 1671 * Emulated root bridge has its own emulated irq chip and irq domain. 1672 * Argument pin is the INTx pin (1=INTA, 2=INTB, 3=INTC, 4=INTD) and 1673 * hwirq for irq_create_mapping() is indexed from zero. 1674 */ 1675 if (pci_is_root_bus(dev->bus)) 1676 return irq_create_mapping(pcie->rp_irq_domain, pin - 1); 1677 else 1678 return of_irq_parse_and_map_pci(dev, slot, pin); 1679 } 1680 1681 static void advk_pcie_disable_phy(struct advk_pcie *pcie) 1682 { 1683 phy_power_off(pcie->phy); 1684 phy_exit(pcie->phy); 1685 } 1686 1687 static int advk_pcie_enable_phy(struct advk_pcie *pcie) 1688 { 1689 int ret; 1690 1691 if (!pcie->phy) 1692 return 0; 1693 1694 ret = phy_init(pcie->phy); 1695 if (ret) 1696 return ret; 1697 1698 ret = phy_set_mode(pcie->phy, PHY_MODE_PCIE); 1699 if (ret) { 1700 phy_exit(pcie->phy); 1701 return ret; 1702 } 1703 1704 ret = phy_power_on(pcie->phy); 1705 if (ret) { 1706 phy_exit(pcie->phy); 1707 return ret; 1708 } 1709 1710 return 0; 1711 } 1712 1713 static int advk_pcie_setup_phy(struct advk_pcie *pcie) 1714 { 1715 struct device *dev = &pcie->pdev->dev; 1716 struct device_node *node = dev->of_node; 1717 int ret = 0; 1718 1719 pcie->phy = devm_of_phy_get(dev, node, NULL); 1720 if (IS_ERR(pcie->phy) && (PTR_ERR(pcie->phy) == -EPROBE_DEFER)) 1721 return PTR_ERR(pcie->phy); 1722 1723 /* Old bindings miss the PHY handle */ 1724 if (IS_ERR(pcie->phy)) { 1725 dev_warn(dev, "PHY unavailable (%ld)\n", PTR_ERR(pcie->phy)); 1726 pcie->phy = NULL; 1727 return 0; 1728 } 1729 1730 ret = advk_pcie_enable_phy(pcie); 1731 if (ret) 1732 dev_err(dev, "Failed to initialize PHY (%d)\n", ret); 1733 1734 return ret; 1735 } 1736 1737 static int advk_pcie_probe(struct platform_device *pdev) 1738 { 1739 struct device *dev = &pdev->dev; 1740 struct advk_pcie *pcie; 1741 struct pci_host_bridge *bridge; 1742 struct resource_entry *entry; 1743 int ret, irq; 1744 1745 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct advk_pcie)); 1746 if (!bridge) 1747 return -ENOMEM; 1748 1749 pcie = pci_host_bridge_priv(bridge); 1750 pcie->pdev = pdev; 1751 platform_set_drvdata(pdev, pcie); 1752 1753 resource_list_for_each_entry(entry, &bridge->windows) { 1754 resource_size_t start = entry->res->start; 1755 resource_size_t size = resource_size(entry->res); 1756 unsigned long type = resource_type(entry->res); 1757 u64 win_size; 1758 1759 /* 1760 * Aardvark hardware allows to configure also PCIe window 1761 * for config type 0 and type 1 mapping, but driver uses 1762 * only PIO for issuing configuration transfers which does 1763 * not use PCIe window configuration. 1764 */ 1765 if (type != IORESOURCE_MEM && type != IORESOURCE_IO) 1766 continue; 1767 1768 /* 1769 * Skip transparent memory resources. Default outbound access 1770 * configuration is set to transparent memory access so it 1771 * does not need window configuration. 1772 */ 1773 if (type == IORESOURCE_MEM && entry->offset == 0) 1774 continue; 1775 1776 /* 1777 * The n-th PCIe window is configured by tuple (match, remap, mask) 1778 * and an access to address A uses this window if A matches the 1779 * match with given mask. 1780 * So every PCIe window size must be a power of two and every start 1781 * address must be aligned to window size. Minimal size is 64 KiB 1782 * because lower 16 bits of mask must be zero. Remapped address 1783 * may have set only bits from the mask. 1784 */ 1785 while (pcie->wins_count < OB_WIN_COUNT && size > 0) { 1786 /* Calculate the largest aligned window size */ 1787 win_size = (1ULL << (fls64(size)-1)) | 1788 (start ? (1ULL << __ffs64(start)) : 0); 1789 win_size = 1ULL << __ffs64(win_size); 1790 if (win_size < 0x10000) 1791 break; 1792 1793 dev_dbg(dev, 1794 "Configuring PCIe window %d: [0x%llx-0x%llx] as %lu\n", 1795 pcie->wins_count, (unsigned long long)start, 1796 (unsigned long long)start + win_size, type); 1797 1798 if (type == IORESOURCE_IO) { 1799 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_IO; 1800 pcie->wins[pcie->wins_count].match = pci_pio_to_address(start); 1801 } else { 1802 pcie->wins[pcie->wins_count].actions = OB_WIN_TYPE_MEM; 1803 pcie->wins[pcie->wins_count].match = start; 1804 } 1805 pcie->wins[pcie->wins_count].remap = start - entry->offset; 1806 pcie->wins[pcie->wins_count].mask = ~(win_size - 1); 1807 1808 if (pcie->wins[pcie->wins_count].remap & (win_size - 1)) 1809 break; 1810 1811 start += win_size; 1812 size -= win_size; 1813 pcie->wins_count++; 1814 } 1815 1816 if (size > 0) { 1817 dev_err(&pcie->pdev->dev, 1818 "Invalid PCIe region [0x%llx-0x%llx]\n", 1819 (unsigned long long)entry->res->start, 1820 (unsigned long long)entry->res->end + 1); 1821 return -EINVAL; 1822 } 1823 } 1824 1825 pcie->base = devm_platform_ioremap_resource(pdev, 0); 1826 if (IS_ERR(pcie->base)) 1827 return PTR_ERR(pcie->base); 1828 1829 irq = platform_get_irq(pdev, 0); 1830 if (irq < 0) 1831 return irq; 1832 1833 ret = devm_request_irq(dev, irq, advk_pcie_irq_handler, 1834 IRQF_SHARED | IRQF_NO_THREAD, "advk-pcie", 1835 pcie); 1836 if (ret) { 1837 dev_err(dev, "Failed to register interrupt\n"); 1838 return ret; 1839 } 1840 1841 pcie->reset_gpio = devm_gpiod_get_optional(dev, "reset", GPIOD_OUT_LOW); 1842 ret = PTR_ERR_OR_ZERO(pcie->reset_gpio); 1843 if (ret) { 1844 if (ret != -EPROBE_DEFER) 1845 dev_err(dev, "Failed to get reset-gpio: %i\n", ret); 1846 return ret; 1847 } 1848 1849 ret = gpiod_set_consumer_name(pcie->reset_gpio, "pcie1-reset"); 1850 if (ret) { 1851 dev_err(dev, "Failed to set reset gpio name: %d\n", ret); 1852 return ret; 1853 } 1854 1855 ret = of_pci_get_max_link_speed(dev->of_node); 1856 if (ret <= 0 || ret > 3) 1857 pcie->link_gen = 3; 1858 else 1859 pcie->link_gen = ret; 1860 1861 ret = advk_pcie_setup_phy(pcie); 1862 if (ret) 1863 return ret; 1864 1865 advk_pcie_setup_hw(pcie); 1866 1867 ret = advk_sw_pci_bridge_init(pcie); 1868 if (ret) { 1869 dev_err(dev, "Failed to register emulated root PCI bridge\n"); 1870 return ret; 1871 } 1872 1873 ret = advk_pcie_init_irq_domain(pcie); 1874 if (ret) { 1875 dev_err(dev, "Failed to initialize irq\n"); 1876 return ret; 1877 } 1878 1879 ret = advk_pcie_init_msi_irq_domain(pcie); 1880 if (ret) { 1881 dev_err(dev, "Failed to initialize irq\n"); 1882 advk_pcie_remove_irq_domain(pcie); 1883 return ret; 1884 } 1885 1886 ret = advk_pcie_init_rp_irq_domain(pcie); 1887 if (ret) { 1888 dev_err(dev, "Failed to initialize irq\n"); 1889 advk_pcie_remove_msi_irq_domain(pcie); 1890 advk_pcie_remove_irq_domain(pcie); 1891 return ret; 1892 } 1893 1894 bridge->sysdata = pcie; 1895 bridge->ops = &advk_pcie_ops; 1896 bridge->map_irq = advk_pcie_map_irq; 1897 1898 ret = pci_host_probe(bridge); 1899 if (ret < 0) { 1900 advk_pcie_remove_rp_irq_domain(pcie); 1901 advk_pcie_remove_msi_irq_domain(pcie); 1902 advk_pcie_remove_irq_domain(pcie); 1903 return ret; 1904 } 1905 1906 return 0; 1907 } 1908 1909 static void advk_pcie_remove(struct platform_device *pdev) 1910 { 1911 struct advk_pcie *pcie = platform_get_drvdata(pdev); 1912 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 1913 u32 val; 1914 int i; 1915 1916 /* Remove PCI bus with all devices */ 1917 pci_lock_rescan_remove(); 1918 pci_stop_root_bus(bridge->bus); 1919 pci_remove_root_bus(bridge->bus); 1920 pci_unlock_rescan_remove(); 1921 1922 /* Disable Root Bridge I/O space, memory space and bus mastering */ 1923 val = advk_readl(pcie, PCIE_CORE_CMD_STATUS_REG); 1924 val &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 1925 advk_writel(pcie, val, PCIE_CORE_CMD_STATUS_REG); 1926 1927 /* Disable MSI */ 1928 val = advk_readl(pcie, PCIE_CORE_CTRL2_REG); 1929 val &= ~PCIE_CORE_CTRL2_MSI_ENABLE; 1930 advk_writel(pcie, val, PCIE_CORE_CTRL2_REG); 1931 1932 /* Clear MSI address */ 1933 advk_writel(pcie, 0, PCIE_MSI_ADDR_LOW_REG); 1934 advk_writel(pcie, 0, PCIE_MSI_ADDR_HIGH_REG); 1935 1936 /* Mask all interrupts */ 1937 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_MASK_REG); 1938 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_MASK_REG); 1939 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_MASK_REG); 1940 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_MASK_REG); 1941 1942 /* Clear all interrupts */ 1943 advk_writel(pcie, PCIE_MSI_ALL_MASK, PCIE_MSI_STATUS_REG); 1944 advk_writel(pcie, PCIE_ISR0_ALL_MASK, PCIE_ISR0_REG); 1945 advk_writel(pcie, PCIE_ISR1_ALL_MASK, PCIE_ISR1_REG); 1946 advk_writel(pcie, PCIE_IRQ_ALL_MASK, HOST_CTRL_INT_STATUS_REG); 1947 1948 /* Remove IRQ domains */ 1949 advk_pcie_remove_rp_irq_domain(pcie); 1950 advk_pcie_remove_msi_irq_domain(pcie); 1951 advk_pcie_remove_irq_domain(pcie); 1952 1953 /* Free config space for emulated root bridge */ 1954 pci_bridge_emul_cleanup(&pcie->bridge); 1955 1956 /* Assert PERST# signal which prepares PCIe card for power down */ 1957 if (pcie->reset_gpio) 1958 gpiod_set_value_cansleep(pcie->reset_gpio, 1); 1959 1960 /* Disable link training */ 1961 val = advk_readl(pcie, PCIE_CORE_CTRL0_REG); 1962 val &= ~LINK_TRAINING_EN; 1963 advk_writel(pcie, val, PCIE_CORE_CTRL0_REG); 1964 1965 /* Disable outbound address windows mapping */ 1966 for (i = 0; i < OB_WIN_COUNT; i++) 1967 advk_pcie_disable_ob_win(pcie, i); 1968 1969 /* Disable phy */ 1970 advk_pcie_disable_phy(pcie); 1971 } 1972 1973 static const struct of_device_id advk_pcie_of_match_table[] = { 1974 { .compatible = "marvell,armada-3700-pcie", }, 1975 {}, 1976 }; 1977 MODULE_DEVICE_TABLE(of, advk_pcie_of_match_table); 1978 1979 static struct platform_driver advk_pcie_driver = { 1980 .driver = { 1981 .name = "advk-pcie", 1982 .of_match_table = advk_pcie_of_match_table, 1983 }, 1984 .probe = advk_pcie_probe, 1985 .remove = advk_pcie_remove, 1986 }; 1987 module_platform_driver(advk_pcie_driver); 1988 1989 MODULE_DESCRIPTION("Aardvark PCIe controller"); 1990 MODULE_LICENSE("GPL v2"); 1991