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