xj | b04a402 | 2021-11-25 15:01:52 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * MediaTek PCIe host controller driver. |
| 4 | * |
| 5 | * Copyright (c) 2020 MediaTek Inc. |
| 6 | * Author: Jianjun Wang <jianjun.wang@mediatek.com> |
| 7 | */ |
| 8 | |
| 9 | #include <linux/arm-smccc.h> |
| 10 | #include <linux/clk.h> |
| 11 | #include <linux/delay.h> |
| 12 | #include <linux/iopoll.h> |
| 13 | #include <linux/irq.h> |
| 14 | #include <linux/irqchip/chained_irq.h> |
| 15 | #include <linux/irqdomain.h> |
| 16 | #include <linux/kernel.h> |
| 17 | #include <linux/msi.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of_address.h> |
| 20 | #include <linux/of_clk.h> |
| 21 | #include <linux/of_pci.h> |
| 22 | #include <linux/of_platform.h> |
| 23 | #include <linux/pci.h> |
| 24 | #include <linux/phy/phy.h> |
| 25 | #include <linux/platform_device.h> |
| 26 | #include <linux/pm_domain.h> |
| 27 | #include <linux/pm_runtime.h> |
| 28 | #include <linux/reset.h> |
| 29 | #include <linux/soc/mediatek/mtk_sip_svc.h> |
| 30 | |
| 31 | #include "../pci.h" |
| 32 | |
| 33 | /* PCIe per-port registers */ |
| 34 | #define PCIE_BASE_CONF_REG 0x14 |
| 35 | #define PCIE_SUPPORT_SPEED_MASK GENMASK(15, 8) |
| 36 | #define PCIE_SUPPORT_SPEED_SHIFT 8 |
| 37 | #define PCIE_SUPPORT_SPEED_2_5GT BIT(8) |
| 38 | #define PCIE_SUPPORT_SPEED_5_0GT BIT(9) |
| 39 | #define PCIE_SUPPORT_SPEED_8_0GT BIT(10) |
| 40 | #define PCIE_SUPPORT_SPEED_16_0GT BIT(11) |
| 41 | |
| 42 | #define PCIE_SETTING_REG 0x80 |
| 43 | #define PCIE_RC_MODE BIT(0) |
| 44 | #define PCIE_GEN_SUPPORT_MASK GENMASK(14, 12) |
| 45 | #define PCIE_GEN_SUPPORT_SHIFT 12 |
| 46 | #define PCIE_GEN2_SUPPORT BIT(12) |
| 47 | #define PCIE_GEN3_SUPPORT BIT(13) |
| 48 | #define PCIE_GEN4_SUPPORT BIT(14) |
| 49 | |
| 50 | #define PCIE_GEN_SUPPORT(max_lspd) \ |
| 51 | GENMASK((max_lspd) - 2 + PCIE_GEN_SUPPORT_SHIFT, PCIE_GEN_SUPPORT_SHIFT) |
| 52 | |
| 53 | #define PCIE_TARGET_SPEED_MASK GENMASK(3, 0) |
| 54 | |
| 55 | #define PCIE_VCORE_550_MILLIVOLT 0 |
| 56 | #define PCIE_VCORE_600_MILLIVOLT 1 |
| 57 | |
| 58 | #define PCIE_PCI_IDS_1 0x9c |
| 59 | #define PCI_CLASS(class) (class << 8) |
| 60 | |
| 61 | #define PCIE_PEX_LINK 0xc8 |
| 62 | #define ASPM_L1_TIMER_RECOUNT BIT(21) |
| 63 | |
| 64 | #define PCIE_CFGNUM_REG 0x140 |
| 65 | #define PCIE_CFG_DEVFN(devfn) ((devfn) & GENMASK(7, 0)) |
| 66 | #define PCIE_CFG_BUS(bus) (((bus) << 8) & GENMASK(15, 8)) |
| 67 | #define PCIE_CFG_BYTE_EN(bytes) (((bytes) << 16) & GENMASK(19, 16)) |
| 68 | #define PCIE_CFG_FORCE_BYTE_EN BIT(20) |
| 69 | #define PCIE_CFG_OFFSET_ADDR 0x1000 |
| 70 | #define PCIE_CFG_HEADER(devfn, bus) \ |
| 71 | (PCIE_CFG_DEVFN(devfn) | PCIE_CFG_BUS(bus)) |
| 72 | |
| 73 | #define PCIE_CFG_HEADER_FORCE_BE(devfn, bus, bytes) \ |
| 74 | (PCIE_CFG_HEADER(devfn, bus) | PCIE_CFG_BYTE_EN(bytes) \ |
| 75 | | PCIE_CFG_FORCE_BYTE_EN) |
| 76 | |
| 77 | #define PCIE_RST_CTRL_REG 0x148 |
| 78 | #define PCIE_MAC_RSTB BIT(0) |
| 79 | #define PCIE_PHY_RSTB BIT(1) |
| 80 | #define PCIE_BRG_RSTB BIT(2) |
| 81 | #define PCIE_PE_RSTB BIT(3) |
| 82 | |
| 83 | #define PCIE_MISC_STATUS_REG 0x14C |
| 84 | #define PCIE_LTR_MSG_RECEIVED BIT(0) |
| 85 | #define PCIE_PCIE_MSG_RECEIVED BIT(1) |
| 86 | |
| 87 | #define PCIE_LTSSM_STATUS_REG 0x150 |
| 88 | #define PCIE_LTSSM_STATE_MASK GENMASK(28, 24) |
| 89 | #define PCIE_LTSSM_STATE(val) ((val & PCIE_LTSSM_STATE_MASK) >> 24) |
| 90 | #define PCIE_LTSSM_STATE_L0 0x10 |
| 91 | #define PCIE_LTSSM_STATE_L1_IDLE 0x13 |
| 92 | #define PCIE_LTSSM_STATE_L2_IDLE 0x14 |
| 93 | |
| 94 | #define PCIE_LINK_STATUS_REG 0x154 |
| 95 | #define PCIE_PORT_LINKUP BIT(8) |
| 96 | |
| 97 | #define PCIE_MSI_SET_NUM 8 |
| 98 | #define PCIE_MSI_IRQS_PER_SET 32 |
| 99 | #define PCIE_MSI_IRQS_NUM \ |
| 100 | (PCIE_MSI_IRQS_PER_SET * (PCIE_MSI_SET_NUM)) |
| 101 | |
| 102 | #define PCIE_INT_ENABLE_REG 0x180 |
| 103 | #define PCIE_MSI_ENABLE GENMASK(PCIE_MSI_SET_NUM + 8 - 1, 8) |
| 104 | #define PCIE_INTX_SHIFT 24 |
| 105 | #define PCIE_MSI_SHIFT 8 |
| 106 | #define PCIE_INTX_MASK GENMASK(27, 24) |
| 107 | #define PCIE_MSG_MASK BIT(28) |
| 108 | #define PCIE_AER_MASK BIT(29) |
| 109 | #define PCIE_PM_MASK BIT(30) |
| 110 | |
| 111 | #define PCIE_INT_STATUS_REG 0x184 |
| 112 | #define PCIE_MSI_SET_ENABLE_REG 0x190 |
| 113 | #define PCIE_MSI_SET_ENABLE GENMASK(PCIE_MSI_SET_NUM - 1, 0) |
| 114 | |
| 115 | #define PCIE_LOW_POWER_CTRL_REG 0x194 |
| 116 | #define PCIE_DIS_LOWPWR_MASK GENMASK(3, 0) |
| 117 | #define PCIE_DIS_L0S_MASK BIT(0) |
| 118 | #define PCIE_DIS_L1_MASK BIT(1) |
| 119 | #define PCIE_DIS_L11_MASK BIT(2) |
| 120 | #define PCIE_DIS_L12_MASK BIT(3) |
| 121 | #define PCIE_FORCE_DIS_LOWPWR GENMASK(11, 8) |
| 122 | #define PCIE_FORCE_DIS_L0S BIT(8) |
| 123 | #define PCIE_FORCE_DIS_L1 BIT(9) |
| 124 | #define PCIE_FORCE_DIS_L11 BIT(10) |
| 125 | #define PCIE_FORCE_DIS_L12 BIT(11) |
| 126 | |
| 127 | #define PCIE_ICMD_PM_REG 0x198 |
| 128 | #define PCIE_TURN_OFF_LINK BIT(4) |
| 129 | |
| 130 | #define PCIE_AXI_IF_CTRL 0x1a8 |
| 131 | #define PCIE_AXI_TAG_EN BIT(1) |
| 132 | |
| 133 | #define PCIE_MSI_SET_BASE_REG 0xc00 |
| 134 | #define PCIE_MSI_SET_OFFSET 0x10 |
| 135 | #define PCIE_MSI_SET_STATUS_OFFSET 0x04 |
| 136 | #define PCIE_MSI_SET_ENABLE_OFFSET 0x08 |
| 137 | |
| 138 | #define PCIE_MSI_SET_ADDR_HI_BASE 0xc80 |
| 139 | #define PCIE_MSI_SET_ADDR_HI_OFFSET 0x04 |
| 140 | |
| 141 | #define PCIE_TRANS_TABLE_BASE_REG 0x800 |
| 142 | #define PCIE_ATR_SRC_ADDR_MSB_OFFSET 0x4 |
| 143 | #define PCIE_ATR_TRSL_ADDR_LSB_OFFSET 0x8 |
| 144 | #define PCIE_ATR_TRSL_ADDR_MSB_OFFSET 0xc |
| 145 | #define PCIE_ATR_TRSL_PARAM_OFFSET 0x10 |
| 146 | #define PCIE_ATR_TLB_SET_OFFSET 0x20 |
| 147 | |
| 148 | #define PCIE_MAX_TRANS_TABLES 8 |
| 149 | #define ATR_SIZE(size) (((size) << 1) & GENMASK(6, 1)) |
| 150 | #define ATR_ID(id) (id & GENMASK(3, 0)) |
| 151 | #define ATR_PARAM(param) (((param) << 16) & GENMASK(27, 16)) |
| 152 | |
| 153 | /* PCIe configuration registers */ |
| 154 | #define PCIE_CONF_EXP_LNKCTL2_REG 0x10B0 |
| 155 | |
| 156 | #define CHIP_VER_E1 0x00 |
| 157 | #define CHIP_VER_E2 0x01 |
| 158 | |
| 159 | struct tag_chipid { |
| 160 | u32 size; |
| 161 | u32 hw_code; |
| 162 | u32 hw_subcode; |
| 163 | u32 hw_ver; |
| 164 | u32 sw_ver; |
| 165 | }; |
| 166 | |
| 167 | /** |
| 168 | * struct mtk_msi_set - MSI information for each set |
| 169 | * @base: IO mapped register base |
| 170 | * @msg_addr: MSI message address |
| 171 | * @saved_irq_state: IRQ enable state saved at suspend time |
| 172 | */ |
| 173 | struct mtk_msi_set { |
| 174 | void __iomem *base; |
| 175 | phys_addr_t msg_addr; |
| 176 | u32 saved_irq_state; |
| 177 | }; |
| 178 | |
| 179 | /** |
| 180 | * struct mtk_pcie_port - PCIe port information |
| 181 | * @dev: PCIe device |
| 182 | * @base: IO mapped register base |
| 183 | * @reg_base: Physical register base |
| 184 | * @mac_reset: mac reset control |
| 185 | * @phy_reset: phy reset control |
| 186 | * @phy: PHY controller block |
| 187 | * @clks: PCIe clocks |
| 188 | * @num_clks: PCIe clocks count for this port |
| 189 | * @is_suspended: device suspend state |
| 190 | * @irq: PCIe controller interrupt number |
| 191 | * @intx_domain: legacy INTx IRQ domain |
| 192 | * @msi_domain: MSI IRQ domain |
| 193 | * @msi_top_domain: MSI IRQ top domain |
| 194 | * @msi_info: MSI sets information |
| 195 | * @lock: lock protecting IRQ bit map |
| 196 | * @msi_irq_in_use: bit map for assigned MSI IRQ |
| 197 | */ |
| 198 | struct mtk_pcie_port { |
| 199 | struct device *dev; |
| 200 | void __iomem *base; |
| 201 | phys_addr_t reg_base; |
| 202 | struct reset_control *mac_reset; |
| 203 | struct reset_control *phy_reset; |
| 204 | struct phy *phy; |
| 205 | struct clk **clks; |
| 206 | int num_clks; |
| 207 | int port_num; |
| 208 | unsigned int busnr; |
| 209 | int max_link_speed; |
| 210 | enum pci_bus_speed link_speed; |
| 211 | bool is_suspended; |
| 212 | u32 sw_ver; |
| 213 | |
| 214 | int irq; |
| 215 | u32 saved_irq_state; |
| 216 | raw_spinlock_t irq_lock; |
| 217 | struct irq_domain *intx_domain; |
| 218 | struct irq_domain *msi_domain; |
| 219 | struct irq_domain *msi_bottom_domain; |
| 220 | struct mtk_msi_set msi_sets[PCIE_MSI_SET_NUM]; |
| 221 | struct mutex lock; |
| 222 | DECLARE_BITMAP(msi_irq_in_use, PCIE_MSI_IRQS_NUM); |
| 223 | }; |
| 224 | |
| 225 | static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn, |
| 226 | int where, int size, u32 *val) |
| 227 | { |
| 228 | struct mtk_pcie_port *port = bus->sysdata; |
| 229 | int bytes; |
| 230 | |
| 231 | bytes = ((1 << size) - 1) << (where & 0x3); |
| 232 | writel(PCIE_CFG_HEADER_FORCE_BE(devfn, bus->number, bytes), |
| 233 | port->base + PCIE_CFGNUM_REG); |
| 234 | |
| 235 | *val = readl(port->base + PCIE_CFG_OFFSET_ADDR + (where & ~0x3)); |
| 236 | |
| 237 | if (size <= 2) |
| 238 | *val = (*val >> (8 * (where & 0x3))) & ((1 << (size * 8)) - 1); |
| 239 | |
| 240 | return PCIBIOS_SUCCESSFUL; |
| 241 | } |
| 242 | |
| 243 | static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn, |
| 244 | int where, int size, u32 val) |
| 245 | { |
| 246 | struct mtk_pcie_port *port = bus->sysdata; |
| 247 | int bytes; |
| 248 | |
| 249 | bytes = ((1 << size) - 1) << (where & 0x3); |
| 250 | writel(PCIE_CFG_HEADER_FORCE_BE(devfn, bus->number, bytes), |
| 251 | port->base + PCIE_CFGNUM_REG); |
| 252 | |
| 253 | if (size <= 2) |
| 254 | val = (val & ((1 << (size * 8)) - 1)) << ((where & 0x3) * 8); |
| 255 | |
| 256 | writel(val, port->base + PCIE_CFG_OFFSET_ADDR + (where & ~0x3)); |
| 257 | |
| 258 | return PCIBIOS_SUCCESSFUL; |
| 259 | } |
| 260 | |
| 261 | static struct pci_ops mtk_pcie_ops = { |
| 262 | .read = mtk_pcie_config_read, |
| 263 | .write = mtk_pcie_config_write, |
| 264 | }; |
| 265 | |
| 266 | static void mtk_pcie_set_trans_window(void __iomem *reg, |
| 267 | resource_size_t cpu_addr, |
| 268 | resource_size_t pci_addr, size_t size) |
| 269 | { |
| 270 | writel(lower_32_bits(cpu_addr) | ATR_SIZE(fls(size) - 1) | 1, reg); |
| 271 | writel(upper_32_bits(cpu_addr), reg + PCIE_ATR_SRC_ADDR_MSB_OFFSET); |
| 272 | writel(lower_32_bits(pci_addr), reg + PCIE_ATR_TRSL_ADDR_LSB_OFFSET); |
| 273 | writel(upper_32_bits(pci_addr), reg + PCIE_ATR_TRSL_ADDR_MSB_OFFSET); |
| 274 | writel(ATR_ID(0) | ATR_PARAM(0), reg + PCIE_ATR_TRSL_PARAM_OFFSET); |
| 275 | } |
| 276 | |
| 277 | static int mtk_pcie_set_trans_table(void __iomem *reg, |
| 278 | resource_size_t cpu_addr, |
| 279 | resource_size_t pci_addr, size_t size, |
| 280 | unsigned int num) |
| 281 | { |
| 282 | void __iomem *table_base; |
| 283 | |
| 284 | if (num > PCIE_MAX_TRANS_TABLES) |
| 285 | return -ENODEV; |
| 286 | |
| 287 | table_base = reg + num * PCIE_ATR_TLB_SET_OFFSET; |
| 288 | mtk_pcie_set_trans_window(table_base, cpu_addr, pci_addr, size); |
| 289 | |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static void mtk_pcie_pre_init_mt6890(struct mtk_pcie_port *port) |
| 294 | { |
| 295 | u32 val = 0; |
| 296 | void __iomem *phy_mode; |
| 297 | |
| 298 | phy_mode = ioremap(0x10005000, 0x1000); |
| 299 | |
| 300 | /* Set phy mode to RC for port 0 */ |
| 301 | val = readl(phy_mode + 0x600); |
| 302 | val |= 1 << 14; |
| 303 | writel(val, phy_mode + 0x600); |
| 304 | |
| 305 | iounmap(phy_mode); |
| 306 | |
| 307 | /* enable low power */ |
| 308 | val = readl(port->base + PCIE_LOW_POWER_CTRL_REG); |
| 309 | val |= PCIE_DIS_LOWPWR_MASK; |
| 310 | val &= ~PCIE_FORCE_DIS_LOWPWR; |
| 311 | writel(val, port->base + PCIE_LOW_POWER_CTRL_REG); |
| 312 | } |
| 313 | |
| 314 | static unsigned long mtk_pcie_vcore_smc(unsigned long id, |
| 315 | unsigned long arg0, unsigned long arg1) |
| 316 | { |
| 317 | struct arm_smccc_res res; |
| 318 | |
| 319 | arm_smccc_smc(id, arg0, arg1, 0, 0, 0, 0, 0, &res); |
| 320 | return res.a0; |
| 321 | } |
| 322 | |
| 323 | static unsigned long mtk_pcie_vcore_550_millivolt(int port_num) |
| 324 | { |
| 325 | return mtk_pcie_vcore_smc(MTK_SIP_PCIE_CONTROL, |
| 326 | port_num, PCIE_VCORE_550_MILLIVOLT); |
| 327 | } |
| 328 | |
| 329 | static unsigned long mtk_pcie_vcore_600_millivolt(int port_num) |
| 330 | { |
| 331 | return mtk_pcie_vcore_smc(MTK_SIP_PCIE_CONTROL, |
| 332 | port_num, PCIE_VCORE_600_MILLIVOLT); |
| 333 | } |
| 334 | |
| 335 | static int mtk_pcie_set_link_speed(struct mtk_pcie_port *port) |
| 336 | { |
| 337 | u32 val; |
| 338 | int err; |
| 339 | |
| 340 | if ((port->max_link_speed < 1) || (port->port_num < 0)) |
| 341 | return -EINVAL; |
| 342 | |
| 343 | val = readl(port->base + PCIE_BASE_CONF_REG); |
| 344 | val = (val & PCIE_SUPPORT_SPEED_MASK) >> PCIE_SUPPORT_SPEED_SHIFT; |
| 345 | if (val & (1 << (port->max_link_speed - 1))) { |
| 346 | val = readl(port->base + PCIE_SETTING_REG); |
| 347 | val &= ~PCIE_GEN_SUPPORT_MASK; |
| 348 | |
| 349 | if (port->max_link_speed > 1) |
| 350 | val |= PCIE_GEN_SUPPORT(port->max_link_speed); |
| 351 | |
| 352 | writel(val, port->base + PCIE_SETTING_REG); |
| 353 | |
| 354 | /* Set target speed */ |
| 355 | val = readl(port->base + PCIE_CONF_EXP_LNKCTL2_REG); |
| 356 | val &= ~PCIE_TARGET_SPEED_MASK; |
| 357 | writel(val | port->max_link_speed, |
| 358 | port->base + PCIE_CONF_EXP_LNKCTL2_REG); |
| 359 | |
| 360 | /* set vcore 550mV for GEN2, set vcore 600mV for above GEN3 */ |
| 361 | if (port->max_link_speed <= 2) { |
| 362 | err = mtk_pcie_vcore_550_millivolt(port->port_num); |
| 363 | if (err) |
| 364 | dev_info(port->dev, "vcore adjust 550mV fail\n"); |
| 365 | } else { |
| 366 | err = mtk_pcie_vcore_600_millivolt(port->port_num); |
| 367 | if (err) |
| 368 | dev_info(port->dev, "vcore adjust 600mV fail\n"); |
| 369 | } |
| 370 | |
| 371 | return 0; |
| 372 | } |
| 373 | |
| 374 | return -EINVAL; |
| 375 | } |
| 376 | |
| 377 | static int mtk_pcie_get_chipid(struct mtk_pcie_port *port) |
| 378 | { |
| 379 | struct device_node *node; |
| 380 | struct tag_chipid *chip_id; |
| 381 | int len; |
| 382 | |
| 383 | node = of_find_node_by_path("/chosen"); |
| 384 | if (!node) |
| 385 | node = of_find_node_by_path("/chosen@0"); |
| 386 | |
| 387 | if (node) { |
| 388 | chip_id = (struct tag_chipid *)of_get_property(node, |
| 389 | "atag,chipid", |
| 390 | &len); |
| 391 | if (!chip_id) { |
| 392 | pr_info("could not found atag,chipid in chosen\n"); |
| 393 | return -ENODEV; |
| 394 | } |
| 395 | } else { |
| 396 | pr_info("chosen node not found in device tree\n"); |
| 397 | return -ENODEV; |
| 398 | } |
| 399 | |
| 400 | port->sw_ver = chip_id->sw_ver; |
| 401 | dev_info(port->dev, "current sw version: %s\n", |
| 402 | port->sw_ver == CHIP_VER_E1 ? "E1" : "E2"); |
| 403 | |
| 404 | return 0; |
| 405 | } |
| 406 | |
| 407 | static void mtk_pcie_enable_msi(struct mtk_pcie_port *port) |
| 408 | { |
| 409 | int i; |
| 410 | u32 val; |
| 411 | |
| 412 | for (i = 0; i < PCIE_MSI_SET_NUM; i++) { |
| 413 | struct mtk_msi_set *msi_set = &port->msi_sets[i]; |
| 414 | |
| 415 | msi_set->base = port->base + PCIE_MSI_SET_BASE_REG + |
| 416 | i * PCIE_MSI_SET_OFFSET; |
| 417 | msi_set->msg_addr = port->reg_base + PCIE_MSI_SET_BASE_REG + |
| 418 | i * PCIE_MSI_SET_OFFSET; |
| 419 | |
| 420 | /* Configure the MSI capture address */ |
| 421 | writel(lower_32_bits(msi_set->msg_addr), msi_set->base); |
| 422 | writel(upper_32_bits(msi_set->msg_addr), |
| 423 | port->base + PCIE_MSI_SET_ADDR_HI_BASE + |
| 424 | i * PCIE_MSI_SET_ADDR_HI_OFFSET); |
| 425 | } |
| 426 | |
| 427 | val = readl(port->base + PCIE_MSI_SET_ENABLE_REG); |
| 428 | val |= PCIE_MSI_SET_ENABLE; |
| 429 | writel(val, port->base + PCIE_MSI_SET_ENABLE_REG); |
| 430 | |
| 431 | val = readl(port->base + PCIE_INT_ENABLE_REG); |
| 432 | val |= PCIE_MSI_ENABLE; |
| 433 | writel(val, port->base + PCIE_INT_ENABLE_REG); |
| 434 | } |
| 435 | |
| 436 | static int mtk_pcie_startup_port(struct mtk_pcie_port *port) |
| 437 | { |
| 438 | struct resource_entry *entry; |
| 439 | struct pci_host_bridge *host = pci_host_bridge_from_priv(port); |
| 440 | u32 val; |
| 441 | int err = 0; |
| 442 | unsigned int table_index = 0; |
| 443 | |
| 444 | /* high speed ethernet hook point */ |
| 445 | |
| 446 | /* set as RC mode */ |
| 447 | val = readl(port->base + PCIE_SETTING_REG); |
| 448 | val |= PCIE_RC_MODE; |
| 449 | writel(val, port->base + PCIE_SETTING_REG); |
| 450 | |
| 451 | /* set class code */ |
| 452 | val = readl(port->base + PCIE_PCI_IDS_1); |
| 453 | val &= ~GENMASK(31, 8); |
| 454 | val |= PCI_CLASS(PCI_CLASS_BRIDGE_PCI << 8); |
| 455 | writel(val, port->base + PCIE_PCI_IDS_1); |
| 456 | |
| 457 | mtk_pcie_pre_init_mt6890(port); |
| 458 | |
| 459 | err = mtk_pcie_set_link_speed(port); |
| 460 | if (err) |
| 461 | dev_info(port->dev, "unsupported speed: GEN%d\n", |
| 462 | port->max_link_speed); |
| 463 | |
| 464 | /* Assert all reset signals */ |
| 465 | val = readl(port->base + PCIE_RST_CTRL_REG); |
| 466 | val |= PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB | PCIE_PE_RSTB; |
| 467 | writel(val, port->base + PCIE_RST_CTRL_REG); |
| 468 | |
| 469 | /* De-assert reset signals*/ |
| 470 | val &= ~(PCIE_MAC_RSTB | PCIE_PHY_RSTB | PCIE_BRG_RSTB); |
| 471 | writel(val, port->base + PCIE_RST_CTRL_REG); |
| 472 | |
| 473 | /* Delay 100ms to wait the reference clocks become stable */ |
| 474 | usleep_range(100 * 1000, 120 * 1000); |
| 475 | |
| 476 | /* De-assert pe reset*/ |
| 477 | val &= ~PCIE_PE_RSTB; |
| 478 | writel(val, port->base + PCIE_RST_CTRL_REG); |
| 479 | |
| 480 | /* Check if the link is up or not */ |
| 481 | err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_REG, val, |
| 482 | !!(val & PCIE_PORT_LINKUP), 20, |
| 483 | 50 * USEC_PER_MSEC); |
| 484 | if (err) { |
| 485 | val = readl(port->base + PCIE_LTSSM_STATUS_REG); |
| 486 | dev_err(port->dev, "PCIe link down, ltssm reg val: %#x\n", val); |
| 487 | return err; |
| 488 | } |
| 489 | |
| 490 | mtk_pcie_enable_msi(port); |
| 491 | |
| 492 | /* Set PCIe translation windows */ |
| 493 | resource_list_for_each_entry(entry, &host->windows) { |
| 494 | unsigned long type = resource_type(entry->res); |
| 495 | struct resource *res = NULL; |
| 496 | resource_size_t cpu_addr; |
| 497 | resource_size_t pci_addr; |
| 498 | |
| 499 | if (!(type & (IORESOURCE_MEM | IORESOURCE_IO))) |
| 500 | continue; |
| 501 | |
| 502 | res = entry->res; |
| 503 | cpu_addr = res->start; |
| 504 | pci_addr = res->start - entry->offset; |
| 505 | mtk_pcie_set_trans_table(port->base + PCIE_TRANS_TABLE_BASE_REG, |
| 506 | cpu_addr, pci_addr, resource_size(res), |
| 507 | table_index); |
| 508 | |
| 509 | dev_dbg(port->dev, "Set %s trans window[%d]: cpu_addr = %#llx, pci_addr = %#llx, size = %#llx\n", |
| 510 | (!!(type & IORESOURCE_MEM) ? "MEM" : "IO"), table_index, |
| 511 | cpu_addr, pci_addr, resource_size(res)); |
| 512 | table_index++; |
| 513 | } |
| 514 | |
| 515 | return err; |
| 516 | } |
| 517 | |
| 518 | static int mtk_pcie_set_affinity(struct irq_data *data, |
| 519 | const struct cpumask *mask, bool force) |
| 520 | { |
| 521 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 522 | int ret; |
| 523 | |
| 524 | ret = irq_set_affinity_hint(port->irq, mask); |
| 525 | if (ret) |
| 526 | return ret; |
| 527 | |
| 528 | irq_data_update_effective_affinity(data, mask); |
| 529 | |
| 530 | return 0; |
| 531 | } |
| 532 | |
| 533 | static void mtk_pcie_msi_irq_mask(struct irq_data *data) |
| 534 | { |
| 535 | pci_msi_mask_irq(data); |
| 536 | irq_chip_mask_parent(data); |
| 537 | } |
| 538 | |
| 539 | static void mtk_pcie_msi_irq_unmask(struct irq_data *data) |
| 540 | { |
| 541 | pci_msi_unmask_irq(data); |
| 542 | irq_chip_unmask_parent(data); |
| 543 | } |
| 544 | |
| 545 | static struct irq_chip mtk_msi_irq_chip = { |
| 546 | .irq_ack = irq_chip_ack_parent, |
| 547 | .irq_mask = mtk_pcie_msi_irq_mask, |
| 548 | .irq_unmask = mtk_pcie_msi_irq_unmask, |
| 549 | .name = "MSI", |
| 550 | }; |
| 551 | |
| 552 | static struct msi_domain_info mtk_msi_domain_info = { |
| 553 | .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | |
| 554 | MSI_FLAG_PCI_MSIX | MSI_FLAG_MULTI_PCI_MSI), |
| 555 | .chip = &mtk_msi_irq_chip, |
| 556 | }; |
| 557 | |
| 558 | static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 559 | { |
| 560 | struct mtk_msi_set *msi_set = irq_data_get_irq_chip_data(data); |
| 561 | struct mtk_pcie_port *port = data->domain->host_data; |
| 562 | unsigned long hwirq; |
| 563 | |
| 564 | hwirq = data->hwirq % PCIE_MSI_IRQS_PER_SET; |
| 565 | |
| 566 | msg->address_hi = upper_32_bits(msi_set->msg_addr); |
| 567 | msg->address_lo = lower_32_bits(msi_set->msg_addr); |
| 568 | msg->data = hwirq; |
| 569 | dev_dbg(port->dev, "msi#%#lx address_hi %#x address_lo %#x data %d\n", |
| 570 | hwirq, msg->address_hi, msg->address_lo, msg->data); |
| 571 | } |
| 572 | |
| 573 | static void mtk_msi_bottom_irq_ack(struct irq_data *data) |
| 574 | { |
| 575 | struct mtk_msi_set *msi_set = irq_data_get_irq_chip_data(data); |
| 576 | unsigned long hwirq; |
| 577 | |
| 578 | hwirq = data->hwirq % PCIE_MSI_IRQS_PER_SET; |
| 579 | |
| 580 | writel(BIT(hwirq), msi_set->base + PCIE_MSI_SET_STATUS_OFFSET); |
| 581 | } |
| 582 | |
| 583 | static void mtk_msi_bottom_irq_mask(struct irq_data *data) |
| 584 | { |
| 585 | struct mtk_msi_set *msi_set = irq_data_get_irq_chip_data(data); |
| 586 | struct mtk_pcie_port *port = data->domain->host_data; |
| 587 | unsigned long hwirq, flags; |
| 588 | u32 val; |
| 589 | |
| 590 | hwirq = data->hwirq % PCIE_MSI_IRQS_PER_SET; |
| 591 | |
| 592 | raw_spin_lock_irqsave(&port->irq_lock, flags); |
| 593 | val = readl(msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 594 | val &= ~BIT(hwirq); |
| 595 | writel(val, msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 596 | raw_spin_unlock_irqrestore(&port->irq_lock, flags); |
| 597 | } |
| 598 | |
| 599 | static void mtk_msi_bottom_irq_unmask(struct irq_data *data) |
| 600 | { |
| 601 | struct mtk_msi_set *msi_set = irq_data_get_irq_chip_data(data); |
| 602 | struct mtk_pcie_port *port = data->domain->host_data; |
| 603 | unsigned long hwirq, flags; |
| 604 | u32 val; |
| 605 | |
| 606 | hwirq = data->hwirq % PCIE_MSI_IRQS_PER_SET; |
| 607 | |
| 608 | raw_spin_lock_irqsave(&port->irq_lock, flags); |
| 609 | val = readl(msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 610 | val |= BIT(hwirq); |
| 611 | writel(val, msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 612 | raw_spin_unlock_irqrestore(&port->irq_lock, flags); |
| 613 | } |
| 614 | |
| 615 | static struct irq_chip mtk_msi_bottom_irq_chip = { |
| 616 | .irq_ack = mtk_msi_bottom_irq_ack, |
| 617 | .irq_mask = mtk_msi_bottom_irq_mask, |
| 618 | .irq_unmask = mtk_msi_bottom_irq_unmask, |
| 619 | .irq_compose_msi_msg = mtk_compose_msi_msg, |
| 620 | .irq_set_affinity = mtk_pcie_set_affinity, |
| 621 | .name = "MSI", |
| 622 | }; |
| 623 | |
| 624 | static int mtk_msi_bottom_domain_alloc(struct irq_domain *domain, |
| 625 | unsigned int virq, unsigned int nr_irqs, |
| 626 | void *arg) |
| 627 | { |
| 628 | struct mtk_pcie_port *port = domain->host_data; |
| 629 | struct mtk_msi_set *msi_set; |
| 630 | int i, hwirq, set_idx; |
| 631 | |
| 632 | mutex_lock(&port->lock); |
| 633 | |
| 634 | hwirq = bitmap_find_free_region(port->msi_irq_in_use, PCIE_MSI_IRQS_NUM, |
| 635 | order_base_2(nr_irqs)); |
| 636 | |
| 637 | mutex_unlock(&port->lock); |
| 638 | |
| 639 | if (hwirq < 0) |
| 640 | return -ENOSPC; |
| 641 | |
| 642 | set_idx = hwirq / PCIE_MSI_IRQS_PER_SET; |
| 643 | msi_set = &port->msi_sets[set_idx]; |
| 644 | |
| 645 | for (i = 0; i < nr_irqs; i++) |
| 646 | irq_domain_set_info(domain, virq + i, hwirq + i, |
| 647 | &mtk_msi_bottom_irq_chip, msi_set, |
| 648 | handle_edge_irq, NULL, NULL); |
| 649 | |
| 650 | return 0; |
| 651 | } |
| 652 | |
| 653 | static void mtk_msi_bottom_domain_free(struct irq_domain *domain, |
| 654 | unsigned int virq, unsigned int nr_irqs) |
| 655 | { |
| 656 | struct mtk_pcie_port *port = domain->host_data; |
| 657 | struct irq_data *data = irq_domain_get_irq_data(domain, virq); |
| 658 | |
| 659 | mutex_lock(&port->lock); |
| 660 | |
| 661 | bitmap_release_region(port->msi_irq_in_use, data->hwirq, |
| 662 | order_base_2(nr_irqs)); |
| 663 | |
| 664 | mutex_unlock(&port->lock); |
| 665 | |
| 666 | irq_domain_free_irqs_common(domain, virq, nr_irqs); |
| 667 | } |
| 668 | |
| 669 | static const struct irq_domain_ops mtk_msi_bottom_domain_ops = { |
| 670 | .alloc = mtk_msi_bottom_domain_alloc, |
| 671 | .free = mtk_msi_bottom_domain_free, |
| 672 | }; |
| 673 | |
| 674 | static void mtk_intx_mask(struct irq_data *data) |
| 675 | { |
| 676 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 677 | u32 val; |
| 678 | |
| 679 | val = readl(port->base + PCIE_INT_ENABLE_REG); |
| 680 | val &= ~(1 << (data->hwirq + PCIE_INTX_SHIFT)); |
| 681 | writel(val, port->base + PCIE_INT_ENABLE_REG); |
| 682 | } |
| 683 | |
| 684 | static void mtk_intx_unmask(struct irq_data *data) |
| 685 | { |
| 686 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 687 | u32 val; |
| 688 | |
| 689 | val = readl(port->base + PCIE_INT_ENABLE_REG); |
| 690 | val |= 1 << (data->hwirq + PCIE_INTX_SHIFT); |
| 691 | writel(val, port->base + PCIE_INT_ENABLE_REG); |
| 692 | } |
| 693 | |
| 694 | static void mtk_intx_eoi(struct irq_data *data) |
| 695 | { |
| 696 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 697 | unsigned long hwirq; |
| 698 | |
| 699 | /** |
| 700 | * As an emulated level irq, its interrupt status will be remained |
| 701 | * until receive the corresponding message of de-assert, hence that |
| 702 | * the status can only be cleared when the interrupt has been serviced. |
| 703 | */ |
| 704 | hwirq = data->hwirq + PCIE_INTX_SHIFT; |
| 705 | writel(1 << hwirq, port->base + PCIE_INT_STATUS_REG); |
| 706 | } |
| 707 | |
| 708 | static struct irq_chip mtk_intx_irq_chip = { |
| 709 | .irq_mask = mtk_intx_mask, |
| 710 | .irq_unmask = mtk_intx_unmask, |
| 711 | .irq_eoi = mtk_intx_eoi, |
| 712 | .irq_set_affinity = mtk_pcie_set_affinity, |
| 713 | .name = "PCIe", |
| 714 | }; |
| 715 | |
| 716 | static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 717 | irq_hw_number_t hwirq) |
| 718 | { |
| 719 | irq_set_chip_and_handler_name(irq, &mtk_intx_irq_chip, |
| 720 | handle_fasteoi_irq, "INTx"); |
| 721 | irq_set_chip_data(irq, domain->host_data); |
| 722 | |
| 723 | return 0; |
| 724 | } |
| 725 | |
| 726 | static const struct irq_domain_ops intx_domain_ops = { |
| 727 | .map = mtk_pcie_intx_map, |
| 728 | }; |
| 729 | |
| 730 | static int mtk_pcie_init_irq_domains(struct mtk_pcie_port *port, |
| 731 | struct device_node *node) |
| 732 | { |
| 733 | struct device *dev = port->dev; |
| 734 | struct device_node *intc_node; |
| 735 | struct fwnode_handle *fwnode = of_node_to_fwnode(node); |
| 736 | int ret; |
| 737 | |
| 738 | raw_spin_lock_init(&port->irq_lock); |
| 739 | |
| 740 | /* Setup INTx */ |
| 741 | intc_node = of_get_child_by_name(node, "legacy-interrupt-controller"); |
| 742 | if (!intc_node) { |
| 743 | dev_notice(dev, "Missing PCIe Intc node\n"); |
| 744 | return -ENODEV; |
| 745 | } |
| 746 | |
| 747 | port->intx_domain = irq_domain_add_linear(intc_node, PCI_NUM_INTX, |
| 748 | &intx_domain_ops, port); |
| 749 | if (!port->intx_domain) { |
| 750 | dev_notice(dev, "failed to get INTx IRQ domain\n"); |
| 751 | return -ENODEV; |
| 752 | } |
| 753 | |
| 754 | /* Setup MSI */ |
| 755 | mutex_init(&port->lock); |
| 756 | |
| 757 | port->msi_bottom_domain = irq_domain_add_linear(node, PCIE_MSI_IRQS_NUM, |
| 758 | &mtk_msi_bottom_domain_ops, port); |
| 759 | if (!port->msi_bottom_domain) { |
| 760 | dev_err(dev, "failed to create MSI bottom domain\n"); |
| 761 | ret = -ENODEV; |
| 762 | goto err_msi_bottom_domain; |
| 763 | } |
| 764 | |
| 765 | port->msi_domain = pci_msi_create_irq_domain(fwnode, |
| 766 | &mtk_msi_domain_info, |
| 767 | port->msi_bottom_domain); |
| 768 | if (!port->msi_domain) { |
| 769 | dev_err(dev, "failed to create MSI domain\n"); |
| 770 | ret = -ENODEV; |
| 771 | goto err_msi_domain; |
| 772 | } |
| 773 | |
| 774 | return 0; |
| 775 | |
| 776 | err_msi_domain: |
| 777 | irq_domain_remove(port->msi_bottom_domain); |
| 778 | err_msi_bottom_domain: |
| 779 | irq_domain_remove(port->intx_domain); |
| 780 | |
| 781 | return ret; |
| 782 | } |
| 783 | |
| 784 | static void mtk_pcie_irq_teardown(struct mtk_pcie_port *port) |
| 785 | { |
| 786 | irq_set_chained_handler_and_data(port->irq, NULL, NULL); |
| 787 | |
| 788 | if (port->intx_domain) |
| 789 | irq_domain_remove(port->intx_domain); |
| 790 | |
| 791 | if (port->msi_domain) |
| 792 | irq_domain_remove(port->msi_domain); |
| 793 | |
| 794 | if (port->msi_bottom_domain) |
| 795 | irq_domain_remove(port->msi_bottom_domain); |
| 796 | |
| 797 | irq_dispose_mapping(port->irq); |
| 798 | } |
| 799 | |
| 800 | static void mtk_pcie_msi_handler(struct mtk_pcie_port *port, int set_idx) |
| 801 | { |
| 802 | struct mtk_msi_set *msi_set = &port->msi_sets[set_idx]; |
| 803 | unsigned long msi_enable, msi_status; |
| 804 | unsigned int virq; |
| 805 | irq_hw_number_t bit, hwirq; |
| 806 | |
| 807 | msi_enable = readl(msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 808 | |
| 809 | do { |
| 810 | msi_status = readl(msi_set->base + |
| 811 | PCIE_MSI_SET_STATUS_OFFSET); |
| 812 | msi_status &= msi_enable; |
| 813 | if (!msi_status) |
| 814 | break; |
| 815 | |
| 816 | for_each_set_bit(bit, &msi_status, PCIE_MSI_IRQS_PER_SET) { |
| 817 | hwirq = bit + set_idx * PCIE_MSI_IRQS_PER_SET; |
| 818 | virq = irq_find_mapping(port->msi_bottom_domain, hwirq); |
| 819 | generic_handle_irq(virq); |
| 820 | } |
| 821 | } while (true); |
| 822 | } |
| 823 | |
| 824 | static void mtk_pcie_irq_handler(struct irq_desc *desc) |
| 825 | { |
| 826 | struct mtk_pcie_port *port = irq_desc_get_handler_data(desc); |
| 827 | struct irq_chip *irqchip = irq_desc_get_chip(desc); |
| 828 | unsigned long status; |
| 829 | unsigned int virq; |
| 830 | irq_hw_number_t irq_bit = PCIE_INTX_SHIFT; |
| 831 | |
| 832 | chained_irq_enter(irqchip, desc); |
| 833 | |
| 834 | status = readl(port->base + PCIE_INT_STATUS_REG); |
| 835 | for_each_set_bit_from(irq_bit, &status, PCI_NUM_INTX + |
| 836 | PCIE_INTX_SHIFT) { |
| 837 | virq = irq_find_mapping(port->intx_domain, |
| 838 | irq_bit - PCIE_INTX_SHIFT); |
| 839 | generic_handle_irq(virq); |
| 840 | } |
| 841 | |
| 842 | irq_bit = PCIE_MSI_SHIFT; |
| 843 | for_each_set_bit_from(irq_bit, &status, PCIE_MSI_SET_NUM + |
| 844 | PCIE_MSI_SHIFT) { |
| 845 | mtk_pcie_msi_handler(port, irq_bit - PCIE_MSI_SHIFT); |
| 846 | |
| 847 | writel(BIT(irq_bit), port->base + PCIE_INT_STATUS_REG); |
| 848 | } |
| 849 | |
| 850 | chained_irq_exit(irqchip, desc); |
| 851 | } |
| 852 | |
| 853 | static int mtk_pcie_setup_irq(struct mtk_pcie_port *port, |
| 854 | struct device_node *node) |
| 855 | { |
| 856 | struct device *dev = port->dev; |
| 857 | struct platform_device *pdev = to_platform_device(dev); |
| 858 | int err; |
| 859 | |
| 860 | err = mtk_pcie_init_irq_domains(port, node); |
| 861 | if (err) { |
| 862 | dev_err(dev, "failed to init PCIe IRQ domain\n"); |
| 863 | return err; |
| 864 | } |
| 865 | |
| 866 | port->irq = platform_get_irq(pdev, 0); |
| 867 | if (port->irq < 0) |
| 868 | return port->irq; |
| 869 | |
| 870 | irq_set_chained_handler_and_data(port->irq, mtk_pcie_irq_handler, port); |
| 871 | |
| 872 | return 0; |
| 873 | } |
| 874 | |
| 875 | static int mtk_pcie_clk_init(struct mtk_pcie_port *port) |
| 876 | { |
| 877 | struct device *dev = port->dev; |
| 878 | struct device_node *np = dev->of_node; |
| 879 | int i; |
| 880 | |
| 881 | port->num_clks = of_clk_get_parent_count(np); |
| 882 | if (port->num_clks == 0) { |
| 883 | dev_warn(dev, "pcie clock is not found\n"); |
| 884 | return 0; |
| 885 | } |
| 886 | |
| 887 | port->clks = devm_kzalloc(dev, port->num_clks, GFP_KERNEL); |
| 888 | if (!port->clks) |
| 889 | return -ENOMEM; |
| 890 | |
| 891 | for (i = 0; i < port->num_clks; i++) { |
| 892 | struct clk *clk; |
| 893 | int ret; |
| 894 | |
| 895 | clk = of_clk_get(np, i); |
| 896 | if (IS_ERR(clk)) { |
| 897 | while (--i >= 0) |
| 898 | clk_put(port->clks[i]); |
| 899 | return PTR_ERR(clk); |
| 900 | } |
| 901 | |
| 902 | ret = clk_prepare_enable(clk); |
| 903 | if (ret < 0) { |
| 904 | while (--i >= 0) { |
| 905 | clk_disable_unprepare(port->clks[i]); |
| 906 | clk_put(port->clks[i]); |
| 907 | } |
| 908 | clk_put(clk); |
| 909 | |
| 910 | return ret; |
| 911 | } |
| 912 | |
| 913 | port->clks[i] = clk; |
| 914 | } |
| 915 | |
| 916 | return 0; |
| 917 | } |
| 918 | |
| 919 | static int mtk_pcie_disable_clk(struct mtk_pcie_port *port) |
| 920 | { |
| 921 | int i; |
| 922 | |
| 923 | if (port->num_clks == 0) |
| 924 | return 0; |
| 925 | |
| 926 | for (i = 0; i < port->num_clks; i++) { |
| 927 | clk_disable_unprepare(port->clks[i]); |
| 928 | clk_put(port->clks[i]); |
| 929 | } |
| 930 | port->num_clks = 0; |
| 931 | |
| 932 | return 0; |
| 933 | } |
| 934 | |
| 935 | static int mtk_pcie_power_up(struct mtk_pcie_port *port) |
| 936 | { |
| 937 | struct device *dev = port->dev; |
| 938 | int err = 0; |
| 939 | |
| 940 | port->phy_reset = devm_reset_control_get_optional_exclusive(dev, |
| 941 | "phy-rst"); |
| 942 | if (IS_ERR(port->phy_reset)) |
| 943 | return PTR_ERR(port->phy_reset); |
| 944 | |
| 945 | reset_control_deassert(port->phy_reset); |
| 946 | |
| 947 | /* phy power on and enable pipe clock */ |
| 948 | port->phy = devm_phy_optional_get(dev, "pcie-phy"); |
| 949 | if (IS_ERR(port->phy)) |
| 950 | return PTR_ERR(port->phy); |
| 951 | |
| 952 | if (port->phy != NULL) { |
| 953 | if (port->port_num >= 0) |
| 954 | port->phy->id = port->port_num; |
| 955 | |
| 956 | err = phy_power_on(port->phy); |
| 957 | if (err) { |
| 958 | dev_notice(dev, "failed to power on pcie phy\n"); |
| 959 | goto err_phy_on; |
| 960 | } |
| 961 | |
| 962 | err = phy_init(port->phy); |
| 963 | if (err) |
| 964 | dev_notice(dev, "failed to initialize phy impedance select, follow the default\n"); |
| 965 | } |
| 966 | |
| 967 | port->mac_reset = devm_reset_control_get_optional_exclusive(dev, |
| 968 | "mac-rst"); |
| 969 | if (IS_ERR(port->mac_reset)) |
| 970 | return PTR_ERR(port->mac_reset); |
| 971 | |
| 972 | reset_control_deassert(port->mac_reset); |
| 973 | |
| 974 | /* mac power on and enable transaction layer clocks */ |
| 975 | pm_runtime_enable(dev); |
| 976 | pm_runtime_get_sync(dev); |
| 977 | |
| 978 | err = mtk_pcie_clk_init(port); |
| 979 | if (err) { |
| 980 | dev_notice(dev, "clock init failed\n"); |
| 981 | goto err_clk_init; |
| 982 | } |
| 983 | |
| 984 | return err; |
| 985 | |
| 986 | err_clk_init: |
| 987 | pm_runtime_put_sync(dev); |
| 988 | pm_runtime_disable(dev); |
| 989 | reset_control_assert(port->mac_reset); |
| 990 | phy_power_off(port->phy); |
| 991 | err_phy_on: |
| 992 | phy_exit(port->phy); |
| 993 | reset_control_assert(port->phy_reset); |
| 994 | |
| 995 | return -EBUSY; |
| 996 | } |
| 997 | |
| 998 | static void mtk_pcie_power_down(struct mtk_pcie_port *port) |
| 999 | { |
| 1000 | phy_power_off(port->phy); |
| 1001 | phy_exit(port->phy); |
| 1002 | |
| 1003 | mtk_pcie_disable_clk(port); |
| 1004 | |
| 1005 | pm_runtime_put_sync(port->dev); |
| 1006 | pm_runtime_disable(port->dev); |
| 1007 | } |
| 1008 | |
| 1009 | static int mtk_pcie_setup(struct mtk_pcie_port *port) |
| 1010 | { |
| 1011 | struct device *dev = port->dev; |
| 1012 | struct pci_host_bridge *host = pci_host_bridge_from_priv(port); |
| 1013 | struct platform_device *pdev = to_platform_device(dev); |
| 1014 | struct list_head *windows = &host->windows; |
| 1015 | struct resource *regs, *bus; |
| 1016 | int err; |
| 1017 | |
| 1018 | err = pci_parse_request_of_pci_ranges(dev, windows, &bus); |
| 1019 | if (err) |
| 1020 | return err; |
| 1021 | |
| 1022 | port->busnr = bus->start; |
| 1023 | |
| 1024 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcie-mac"); |
| 1025 | port->base = devm_ioremap_resource(dev, regs); |
| 1026 | if (IS_ERR(port->base)) { |
| 1027 | dev_err(dev, "failed to map register base\n"); |
| 1028 | return PTR_ERR(port->base); |
| 1029 | } |
| 1030 | |
| 1031 | port->reg_base = regs->start; |
| 1032 | |
| 1033 | port->max_link_speed = of_pci_get_max_link_speed(dev->of_node); |
| 1034 | if (port->max_link_speed > 0) |
| 1035 | dev_info(dev, "max speed to GEN%d\n", port->max_link_speed); |
| 1036 | |
| 1037 | port->port_num = of_get_pci_domain_nr(dev->of_node); |
| 1038 | if (port->port_num >= 0) |
| 1039 | dev_info(dev, "host bridge domain number %d\n", port->port_num); |
| 1040 | |
| 1041 | err = mtk_pcie_get_chipid(port); |
| 1042 | if (err) { |
| 1043 | dev_info(port->dev, "unknown chip version\n"); |
| 1044 | port->sw_ver = CHIP_VER_E1; |
| 1045 | } |
| 1046 | |
| 1047 | /* Don't touch the hardware registers before power up */ |
| 1048 | err = mtk_pcie_power_up(port); |
| 1049 | if (err) |
| 1050 | return err; |
| 1051 | |
| 1052 | /* Try link up */ |
| 1053 | err = mtk_pcie_startup_port(port); |
| 1054 | if (err) { |
| 1055 | dev_notice(dev, "PCIe link down\n"); |
| 1056 | goto err_setup; |
| 1057 | } |
| 1058 | |
| 1059 | err = mtk_pcie_setup_irq(port, dev->of_node); |
| 1060 | if (err) |
| 1061 | goto err_setup; |
| 1062 | |
| 1063 | dev_info(dev, "PCIe link up success!\n"); |
| 1064 | |
| 1065 | return 0; |
| 1066 | |
| 1067 | err_setup: |
| 1068 | mtk_pcie_power_down(port); |
| 1069 | |
| 1070 | return err; |
| 1071 | } |
| 1072 | |
| 1073 | static int mtk_pcie_probe(struct platform_device *pdev) |
| 1074 | { |
| 1075 | struct device *dev = &pdev->dev; |
| 1076 | struct mtk_pcie_port *port; |
| 1077 | struct pci_host_bridge *host; |
| 1078 | int err; |
| 1079 | |
| 1080 | host = devm_pci_alloc_host_bridge(dev, sizeof(*port)); |
| 1081 | if (!host) |
| 1082 | return -ENOMEM; |
| 1083 | |
| 1084 | port = pci_host_bridge_priv(host); |
| 1085 | |
| 1086 | port->dev = dev; |
| 1087 | platform_set_drvdata(pdev, port); |
| 1088 | |
| 1089 | err = mtk_pcie_setup(port); |
| 1090 | if (err) |
| 1091 | goto release_resource; |
| 1092 | |
| 1093 | host->busnr = port->busnr; |
| 1094 | host->dev.parent = port->dev; |
| 1095 | host->ops = &mtk_pcie_ops; |
| 1096 | host->map_irq = of_irq_parse_and_map_pci; |
| 1097 | host->swizzle_irq = pci_common_swizzle; |
| 1098 | host->sysdata = port; |
| 1099 | |
| 1100 | err = pci_host_probe(host); |
| 1101 | if (err) |
| 1102 | goto power_down; |
| 1103 | |
| 1104 | return 0; |
| 1105 | |
| 1106 | power_down: |
| 1107 | mtk_pcie_power_down(port); |
| 1108 | release_resource: |
| 1109 | pci_free_resource_list(&host->windows); |
| 1110 | |
| 1111 | return err; |
| 1112 | } |
| 1113 | |
| 1114 | static int mtk_pcie_remove(struct platform_device *pdev) |
| 1115 | { |
| 1116 | struct mtk_pcie_port *port = platform_get_drvdata(pdev); |
| 1117 | struct pci_host_bridge *host = pci_host_bridge_from_priv(port); |
| 1118 | |
| 1119 | pci_lock_rescan_remove(); |
| 1120 | pci_stop_root_bus(host->bus); |
| 1121 | pci_remove_root_bus(host->bus); |
| 1122 | pci_unlock_rescan_remove(); |
| 1123 | |
| 1124 | mtk_pcie_irq_teardown(port); |
| 1125 | mtk_pcie_power_down(port); |
| 1126 | |
| 1127 | return 0; |
| 1128 | } |
| 1129 | |
| 1130 | static void mtk_pcie_suspend_noirq_fixup_mt6890(struct mtk_pcie_port *port) |
| 1131 | { |
| 1132 | void __iomem *mtcmos; |
| 1133 | u32 val, offset; |
| 1134 | |
| 1135 | if (port->sw_ver != CHIP_VER_E1) |
| 1136 | return; |
| 1137 | |
| 1138 | dev_info(port->dev, "%s\n", __func__); |
| 1139 | |
| 1140 | if ((port->port_num < 0) || (port->port_num > 3)) { |
| 1141 | dev_notice(port->dev, "unknown port_num, workaround abort\n"); |
| 1142 | return; |
| 1143 | } |
| 1144 | |
| 1145 | if (port->port_num < 3) |
| 1146 | offset = 4 * port->port_num; |
| 1147 | else |
| 1148 | offset = 4 * 2; |
| 1149 | |
| 1150 | mtcmos = ioremap(0x10006000, 0x1000); |
| 1151 | |
| 1152 | /* Reset MAC */ |
| 1153 | val = readl(mtcmos + 0x330 + offset); |
| 1154 | val &= ~BIT(0); |
| 1155 | writel(val, mtcmos + 0x330 + offset); |
| 1156 | val = readl(mtcmos + 0x330 + offset); |
| 1157 | dev_info(port->dev, "MAC MTCMOS val = %#x\n", val); |
| 1158 | |
| 1159 | /* PHY power down */ |
| 1160 | val = readl(mtcmos + 0x30c + offset); |
| 1161 | val |= BIT(1); |
| 1162 | writel(val, mtcmos + 0x30c + offset); |
| 1163 | val |= BIT(4); |
| 1164 | writel(val, mtcmos + 0x30c + offset); |
| 1165 | val &= ~BIT(0); |
| 1166 | writel(val, mtcmos + 0x30c + offset); |
| 1167 | val &= ~BIT(2); |
| 1168 | writel(val, mtcmos + 0x30c + offset); |
| 1169 | val &= ~BIT(3); |
| 1170 | writel(val, mtcmos + 0x30c + offset); |
| 1171 | |
| 1172 | val = readl(mtcmos + 0x30c + offset); |
| 1173 | dev_info(port->dev, "PHY MTCMOS val = %#x\n", val); |
| 1174 | |
| 1175 | iounmap(mtcmos); |
| 1176 | } |
| 1177 | |
| 1178 | static void mtk_pcie_resume_noirq_fixup_mt6890(struct mtk_pcie_port *port) |
| 1179 | { |
| 1180 | void __iomem *mtcmos; |
| 1181 | u32 val, offset; |
| 1182 | |
| 1183 | if (port->sw_ver != CHIP_VER_E1) |
| 1184 | return; |
| 1185 | |
| 1186 | dev_info(port->dev, "%s\n", __func__); |
| 1187 | |
| 1188 | if ((port->port_num < 0) || (port->port_num > 3)) { |
| 1189 | dev_notice(port->dev, "unknown port_num, workaround abort\n"); |
| 1190 | return; |
| 1191 | } |
| 1192 | |
| 1193 | if (port->port_num < 3) |
| 1194 | offset = 4 * port->port_num; |
| 1195 | else |
| 1196 | offset = 4 * 2; |
| 1197 | |
| 1198 | mtcmos = ioremap(0x10006000, 0x1000); |
| 1199 | |
| 1200 | /* PHY power up */ |
| 1201 | val = readl(mtcmos + 0x30c + offset); |
| 1202 | val |= BIT(2); |
| 1203 | writel(val, mtcmos + 0x30c + offset); |
| 1204 | val |= BIT(3); |
| 1205 | writel(val, mtcmos + 0x30c + offset); |
| 1206 | val &= ~BIT(4); |
| 1207 | writel(val, mtcmos + 0x30c + offset); |
| 1208 | val &= ~BIT(1); |
| 1209 | writel(val, mtcmos + 0x30c + offset); |
| 1210 | val |= BIT(0); |
| 1211 | writel(val, mtcmos + 0x30c + offset); |
| 1212 | |
| 1213 | val = readl(mtcmos + 0x30c + offset); |
| 1214 | dev_info(port->dev, "PHY MTCMOS val = %#x\n", val); |
| 1215 | |
| 1216 | /* Release MAC */ |
| 1217 | val = readl(mtcmos + 0x330 + offset); |
| 1218 | val |= BIT(0); |
| 1219 | writel(val, mtcmos + 0x330 + offset); |
| 1220 | dev_info(port->dev, "MAC MTCMOS val = %#x\n", val); |
| 1221 | |
| 1222 | iounmap(mtcmos); |
| 1223 | } |
| 1224 | |
| 1225 | static void __maybe_unused mtk_pcie_irq_save(struct mtk_pcie_port *port) |
| 1226 | { |
| 1227 | int i; |
| 1228 | |
| 1229 | raw_spin_lock(&port->irq_lock); |
| 1230 | |
| 1231 | port->saved_irq_state = readl(port->base + PCIE_INT_ENABLE_REG); |
| 1232 | |
| 1233 | for (i = 0; i < PCIE_MSI_SET_NUM; i++) { |
| 1234 | struct mtk_msi_set *msi_set = &port->msi_sets[i]; |
| 1235 | |
| 1236 | msi_set->saved_irq_state = readl(msi_set->base + |
| 1237 | PCIE_MSI_SET_ENABLE_OFFSET); |
| 1238 | } |
| 1239 | |
| 1240 | raw_spin_unlock(&port->irq_lock); |
| 1241 | } |
| 1242 | |
| 1243 | static void __maybe_unused mtk_pcie_irq_restore(struct mtk_pcie_port *port) |
| 1244 | { |
| 1245 | int i; |
| 1246 | |
| 1247 | raw_spin_lock(&port->irq_lock); |
| 1248 | |
| 1249 | writel(port->saved_irq_state, port->base + PCIE_INT_ENABLE_REG); |
| 1250 | |
| 1251 | for (i = 0; i < PCIE_MSI_SET_NUM; i++) { |
| 1252 | struct mtk_msi_set *msi_set = &port->msi_sets[i]; |
| 1253 | |
| 1254 | writel(msi_set->saved_irq_state, |
| 1255 | msi_set->base + PCIE_MSI_SET_ENABLE_OFFSET); |
| 1256 | } |
| 1257 | |
| 1258 | raw_spin_unlock(&port->irq_lock); |
| 1259 | } |
| 1260 | |
| 1261 | static int __maybe_unused mtk_pcie_turn_off_link(struct mtk_pcie_port *port) |
| 1262 | { |
| 1263 | u32 val; |
| 1264 | |
| 1265 | val = readl(port->base + PCIE_ICMD_PM_REG); |
| 1266 | val |= PCIE_TURN_OFF_LINK; |
| 1267 | writel(val, port->base + PCIE_ICMD_PM_REG); |
| 1268 | |
| 1269 | /* Check the link is L2 */ |
| 1270 | return readl_poll_timeout(port->base + PCIE_LTSSM_STATUS_REG, val, |
| 1271 | (PCIE_LTSSM_STATE(val) == |
| 1272 | PCIE_LTSSM_STATE_L2_IDLE), 20, |
| 1273 | 50 * USEC_PER_MSEC); |
| 1274 | } |
| 1275 | |
| 1276 | static int __maybe_unused mtk_pcie_suspend_noirq(struct device *dev) |
| 1277 | { |
| 1278 | struct mtk_pcie_port *port = dev_get_drvdata(dev); |
| 1279 | int i, err; |
| 1280 | |
| 1281 | if (port->is_suspended) |
| 1282 | return 0; |
| 1283 | |
| 1284 | mtk_pcie_irq_save(port); |
| 1285 | |
| 1286 | /* Trigger link to L2 state */ |
| 1287 | err = mtk_pcie_turn_off_link(port); |
| 1288 | if (err) { |
| 1289 | dev_notice(port->dev, "can not enter L2 state\n"); |
| 1290 | goto power_off; |
| 1291 | } |
| 1292 | |
| 1293 | /* Wait Harrier enter L2 state */ |
| 1294 | usleep_range(10 * 1000, 20 * 1000); |
| 1295 | |
| 1296 | dev_info(port->dev, "enter L2 state success"); |
| 1297 | |
| 1298 | power_off: |
| 1299 | phy_power_off(port->phy); |
| 1300 | |
| 1301 | for (i = 0; i < port->num_clks; i++) |
| 1302 | clk_disable_unprepare(port->clks[i]); |
| 1303 | |
| 1304 | mtk_pcie_suspend_noirq_fixup_mt6890(port); |
| 1305 | |
| 1306 | port->is_suspended = true; |
| 1307 | |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | static int __maybe_unused mtk_pcie_resume_noirq(struct device *dev) |
| 1312 | { |
| 1313 | struct mtk_pcie_port *port = dev_get_drvdata(dev); |
| 1314 | int i, err; |
| 1315 | |
| 1316 | if (!port->is_suspended) |
| 1317 | return 0; |
| 1318 | |
| 1319 | mtk_pcie_resume_noirq_fixup_mt6890(port); |
| 1320 | |
| 1321 | phy_power_on(port->phy); |
| 1322 | |
| 1323 | for (i = 0; i < port->num_clks; i++) { |
| 1324 | err = clk_prepare_enable(port->clks[i]); |
| 1325 | if (err < 0) { |
| 1326 | while (--i >= 0) |
| 1327 | clk_disable_unprepare(port->clks[i]); |
| 1328 | return err; |
| 1329 | } |
| 1330 | } |
| 1331 | |
| 1332 | err = mtk_pcie_startup_port(port); |
| 1333 | if (err) { |
| 1334 | dev_notice(port->dev, "resume failed\n"); |
| 1335 | return err; |
| 1336 | } |
| 1337 | |
| 1338 | port->is_suspended = false; |
| 1339 | |
| 1340 | mtk_pcie_irq_restore(port); |
| 1341 | |
| 1342 | dev_info(port->dev, "resume done\n"); |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static const struct dev_pm_ops mtk_pcie_pm_ops = { |
| 1348 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq, |
| 1349 | mtk_pcie_resume_noirq) |
| 1350 | }; |
| 1351 | |
| 1352 | static void mtk_pcie_mtcmos_fixup_mt6890(struct pci_dev *pdev) |
| 1353 | { |
| 1354 | struct mtk_pcie_port *port = pdev->bus->sysdata; |
| 1355 | struct device *dev = port->dev; |
| 1356 | struct generic_pm_domain *pcie_pd; |
| 1357 | |
| 1358 | if (port->sw_ver != CHIP_VER_E1) |
| 1359 | return; |
| 1360 | |
| 1361 | dev_info(dev, "%s\n", __func__); |
| 1362 | |
| 1363 | if (dev->pm_domain) { |
| 1364 | /* Configure the power domain as always on */ |
| 1365 | pcie_pd = pd_to_genpd(dev->pm_domain); |
| 1366 | pcie_pd->flags |= GENPD_FLAG_ALWAYS_ON; |
| 1367 | } |
| 1368 | } |
| 1369 | |
| 1370 | DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_MEDIATEK, 0x4d75, |
| 1371 | mtk_pcie_mtcmos_fixup_mt6890); |
| 1372 | |
| 1373 | /* SW workaround, for those 2735 ICs has SEC_MSC efuse setting */ |
| 1374 | static void mtk_pcie_disable_smpu_fixup_mt2735(struct pci_dev *pdev) |
| 1375 | { |
| 1376 | struct device *dev = &pdev->dev; |
| 1377 | struct arm_smccc_res res; |
| 1378 | |
| 1379 | arm_smccc_smc(MTK_SIP_PCIE_DISABLE_SMPU, 0, 0, 0, 0, 0, 0, 0, &res); |
| 1380 | if (res.a0) |
| 1381 | dev_info(dev, "can't disable SMPU through SMC call\n"); |
| 1382 | } |
| 1383 | |
| 1384 | DECLARE_PCI_FIXUP_FINAL(PCI_VENDOR_ID_MEDIATEK, 0x4d75, |
| 1385 | mtk_pcie_disable_smpu_fixup_mt2735); |
| 1386 | |
| 1387 | static const struct of_device_id mtk_pcie_of_match[] = { |
| 1388 | { .compatible = "mediatek,gen3-pcie" }, |
| 1389 | { .compatible = "mediatek,mt6880-pcie" }, |
| 1390 | { .compatible = "mediatek,mt6890-pcie" }, |
| 1391 | { .compatible = "mediatek,mt2735-pcie" }, |
| 1392 | {}, |
| 1393 | }; |
| 1394 | |
| 1395 | static struct platform_driver mtk_pcie_driver = { |
| 1396 | .probe = mtk_pcie_probe, |
| 1397 | .remove = mtk_pcie_remove, |
| 1398 | .driver = { |
| 1399 | .name = "mtk-pcie", |
| 1400 | .of_match_table = mtk_pcie_of_match, |
| 1401 | .pm = &mtk_pcie_pm_ops, |
| 1402 | }, |
| 1403 | }; |
| 1404 | |
| 1405 | module_platform_driver(mtk_pcie_driver); |
| 1406 | MODULE_LICENSE("GPL v2"); |