rjw | 1f88458 | 2022-01-06 17:20:42 +0800 | [diff] [blame^] | 1 | /* |
| 2 | * MediaTek PCIe host controller driver. |
| 3 | * |
| 4 | * Copyright (c) 2017 MediaTek Inc. |
| 5 | * Author: Ryder Lee <ryder.lee@mediatek.com> |
| 6 | * Honghui Zhang <honghui.zhang@mediatek.com> |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | * |
| 12 | * This program is distributed in the hope that it will be useful, |
| 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | * GNU General Public License for more details. |
| 16 | */ |
| 17 | |
| 18 | #include <linux/clk.h> |
| 19 | #include <linux/delay.h> |
| 20 | #include <linux/iopoll.h> |
| 21 | #include <linux/irq.h> |
| 22 | #include <linux/irqchip/chained_irq.h> |
| 23 | #include <linux/irqdomain.h> |
| 24 | #include <linux/kernel.h> |
| 25 | #include <linux/msi.h> |
| 26 | #include <linux/module.h> |
| 27 | #include <linux/of_address.h> |
| 28 | #include <linux/of_pci.h> |
| 29 | #include <linux/of_platform.h> |
| 30 | #include <linux/pci.h> |
| 31 | #include <linux/phy/phy.h> |
| 32 | #include <linux/platform_device.h> |
| 33 | #include <linux/pm_runtime.h> |
| 34 | #include <linux/reset.h> |
| 35 | |
| 36 | /* PCIe shared registers */ |
| 37 | #define PCIE_SYS_CFG 0x00 |
| 38 | #define PCIE_INT_ENABLE 0x0c |
| 39 | #define PCIE_CFG_ADDR 0x20 |
| 40 | #define PCIE_CFG_DATA 0x24 |
| 41 | |
| 42 | /* PCIe per port registers */ |
| 43 | #define PCIE_BAR0_SETUP 0x10 |
| 44 | #define PCIE_CLASS 0x34 |
| 45 | #define PCIE_LINK_STATUS 0x50 |
| 46 | |
| 47 | #define PCIE_PORT_INT_EN(x) BIT(20 + (x)) |
| 48 | #define PCIE_PORT_PERST(x) BIT(1 + (x)) |
| 49 | #define PCIE_PORT_LINKUP BIT(0) |
| 50 | #define PCIE_BAR_MAP_MAX GENMASK(31, 16) |
| 51 | |
| 52 | #define PCIE_BAR_ENABLE BIT(0) |
| 53 | #define PCIE_REVISION_ID BIT(0) |
| 54 | #define PCIE_CLASS_CODE (0x60400 << 8) |
| 55 | #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \ |
| 56 | ((((regn) >> 8) & GENMASK(3, 0)) << 24)) |
| 57 | #define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8)) |
| 58 | #define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11)) |
| 59 | #define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16)) |
| 60 | #define PCIE_CONF_ADDR(regn, fun, dev, bus) \ |
| 61 | (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \ |
| 62 | PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus)) |
| 63 | |
| 64 | /* MediaTek specific configuration registers */ |
| 65 | #define PCIE_FTS_NUM 0x70c |
| 66 | #define PCIE_FTS_NUM_MASK GENMASK(15, 8) |
| 67 | #define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8) |
| 68 | |
| 69 | #define PCIE_FC_CREDIT 0x73c |
| 70 | #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16)) |
| 71 | #define PCIE_FC_CREDIT_VAL(x) ((x) << 16) |
| 72 | |
| 73 | /* PCIe V2 share registers */ |
| 74 | #define PCIE_SYS_CFG_V2 0x0 |
| 75 | #define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8) |
| 76 | #define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8) |
| 77 | |
| 78 | /* PCIe V2 per-port registers */ |
| 79 | #define PCIE_MSI_VECTOR 0x0c0 |
| 80 | |
| 81 | #define PCIE_CONF_VEND_ID 0x100 |
| 82 | #define PCIE_CONF_CLASS_ID 0x106 |
| 83 | |
| 84 | #define PCIE_INT_MASK 0x420 |
| 85 | #define INTX_MASK GENMASK(19, 16) |
| 86 | #define INTX_SHIFT 16 |
| 87 | #define PCIE_INT_STATUS 0x424 |
| 88 | #define MSI_STATUS BIT(23) |
| 89 | #define PCIE_IMSI_STATUS 0x42c |
| 90 | #define PCIE_IMSI_ADDR 0x430 |
| 91 | #define MSI_MASK BIT(23) |
| 92 | #define MTK_MSI_IRQS_NUM 32 |
| 93 | |
| 94 | #define PCIE_AHB_TRANS_BASE0_L 0x438 |
| 95 | #define PCIE_AHB_TRANS_BASE0_H 0x43c |
| 96 | #define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0)) |
| 97 | #define PCIE_AXI_WINDOW0 0x448 |
| 98 | #define WIN_ENABLE BIT(7) |
| 99 | |
| 100 | /* PCIe V2 configuration transaction header */ |
| 101 | #define PCIE_CFG_HEADER0 0x460 |
| 102 | #define PCIE_CFG_HEADER1 0x464 |
| 103 | #define PCIE_CFG_HEADER2 0x468 |
| 104 | #define PCIE_CFG_WDATA 0x470 |
| 105 | #define PCIE_APP_TLP_REQ 0x488 |
| 106 | #define PCIE_CFG_RDATA 0x48c |
| 107 | #define APP_CFG_REQ BIT(0) |
| 108 | #define APP_CPL_STATUS GENMASK(7, 5) |
| 109 | |
| 110 | #define CFG_WRRD_TYPE_0 4 |
| 111 | #define CFG_WR_FMT 2 |
| 112 | #define CFG_RD_FMT 0 |
| 113 | |
| 114 | #define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0)) |
| 115 | #define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24)) |
| 116 | #define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29)) |
| 117 | #define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2)) |
| 118 | #define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16)) |
| 119 | #define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19)) |
| 120 | #define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24)) |
| 121 | #define CFG_HEADER_DW0(type, fmt) \ |
| 122 | (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt)) |
| 123 | #define CFG_HEADER_DW1(where, size) \ |
| 124 | (GENMASK(((size) - 1), 0) << ((where) & 0x3)) |
| 125 | #define CFG_HEADER_DW2(regn, fun, dev, bus) \ |
| 126 | (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \ |
| 127 | CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus)) |
| 128 | |
| 129 | #define PCIE_RST_CTRL 0x510 |
| 130 | #define PCIE_PHY_RSTB BIT(0) |
| 131 | #define PCIE_PIPE_SRSTB BIT(1) |
| 132 | #define PCIE_MAC_SRSTB BIT(2) |
| 133 | #define PCIE_CRSTB BIT(3) |
| 134 | #define PCIE_PERSTB BIT(8) |
| 135 | #define PCIE_LINKDOWN_RST_EN GENMASK(15, 13) |
| 136 | #define PCIE_LINK_STATUS_V2 0x804 |
| 137 | #define PCIE_PORT_LINKUP_V2 BIT(10) |
| 138 | |
| 139 | struct mtk_pcie_port; |
| 140 | |
| 141 | /** |
| 142 | * struct mtk_pcie_soc - differentiate between host generations |
| 143 | * @need_fix_class_id: whether this host's class ID needed to be fixed or not |
| 144 | * @ops: pointer to configuration access functions |
| 145 | * @startup: pointer to controller setting functions |
| 146 | * @setup_irq: pointer to initialize IRQ functions |
| 147 | */ |
| 148 | struct mtk_pcie_soc { |
| 149 | bool need_fix_class_id; |
| 150 | struct pci_ops *ops; |
| 151 | int (*startup)(struct mtk_pcie_port *port); |
| 152 | int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node); |
| 153 | }; |
| 154 | |
| 155 | /** |
| 156 | * struct mtk_pcie_port - PCIe port information |
| 157 | * @base: IO mapped register base |
| 158 | * @list: port list |
| 159 | * @pcie: pointer to PCIe host info |
| 160 | * @reset: pointer to port reset control |
| 161 | * @sys_ck: pointer to transaction/data link layer clock |
| 162 | * @ahb_ck: pointer to AHB slave interface operating clock for CSR access |
| 163 | * and RC initiated MMIO access |
| 164 | * @axi_ck: pointer to application layer MMIO channel operating clock |
| 165 | * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock |
| 166 | * when pcie_mac_ck/pcie_pipe_ck is turned off |
| 167 | * @obff_ck: pointer to OBFF functional block operating clock |
| 168 | * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock |
| 169 | * @phy: pointer to PHY control block |
| 170 | * @slot: port slot |
| 171 | * @irq: GIC irq |
| 172 | * @irq_domain: legacy INTx IRQ domain |
| 173 | * @inner_domain: inner IRQ domain |
| 174 | * @msi_domain: MSI IRQ domain |
| 175 | * @lock: protect the msi_irq_in_use bitmap |
| 176 | * @msi_irq_in_use: bit map for assigned MSI IRQ |
| 177 | */ |
| 178 | struct mtk_pcie_port { |
| 179 | void __iomem *base; |
| 180 | struct list_head list; |
| 181 | struct mtk_pcie *pcie; |
| 182 | struct reset_control *reset; |
| 183 | struct clk *sys_ck; |
| 184 | struct clk *ahb_ck; |
| 185 | struct clk *axi_ck; |
| 186 | struct clk *aux_ck; |
| 187 | struct clk *obff_ck; |
| 188 | struct clk *pipe_ck; |
| 189 | struct phy *phy; |
| 190 | u32 slot; |
| 191 | int irq; |
| 192 | struct irq_domain *irq_domain; |
| 193 | struct irq_domain *inner_domain; |
| 194 | struct irq_domain *msi_domain; |
| 195 | struct mutex lock; |
| 196 | DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM); |
| 197 | }; |
| 198 | |
| 199 | /** |
| 200 | * struct mtk_pcie - PCIe host information |
| 201 | * @dev: pointer to PCIe device |
| 202 | * @base: IO mapped register base |
| 203 | * @free_ck: free-run reference clock |
| 204 | * @mem: non-prefetchable memory resource |
| 205 | * @ports: pointer to PCIe port information |
| 206 | * @soc: pointer to SoC-dependent operations |
| 207 | * @busnr: root bus number |
| 208 | */ |
| 209 | struct mtk_pcie { |
| 210 | struct device *dev; |
| 211 | void __iomem *base; |
| 212 | struct clk *free_ck; |
| 213 | |
| 214 | struct resource mem; |
| 215 | struct list_head ports; |
| 216 | const struct mtk_pcie_soc *soc; |
| 217 | unsigned int busnr; |
| 218 | }; |
| 219 | |
| 220 | static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie) |
| 221 | { |
| 222 | struct device *dev = pcie->dev; |
| 223 | |
| 224 | clk_disable_unprepare(pcie->free_ck); |
| 225 | |
| 226 | pm_runtime_put_sync(dev); |
| 227 | pm_runtime_disable(dev); |
| 228 | } |
| 229 | |
| 230 | static void mtk_pcie_port_free(struct mtk_pcie_port *port) |
| 231 | { |
| 232 | struct mtk_pcie *pcie = port->pcie; |
| 233 | struct device *dev = pcie->dev; |
| 234 | |
| 235 | devm_iounmap(dev, port->base); |
| 236 | list_del(&port->list); |
| 237 | devm_kfree(dev, port); |
| 238 | } |
| 239 | |
| 240 | static void mtk_pcie_put_resources(struct mtk_pcie *pcie) |
| 241 | { |
| 242 | struct mtk_pcie_port *port, *tmp; |
| 243 | |
| 244 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) { |
| 245 | phy_power_off(port->phy); |
| 246 | phy_exit(port->phy); |
| 247 | clk_disable_unprepare(port->pipe_ck); |
| 248 | clk_disable_unprepare(port->obff_ck); |
| 249 | clk_disable_unprepare(port->axi_ck); |
| 250 | clk_disable_unprepare(port->aux_ck); |
| 251 | clk_disable_unprepare(port->ahb_ck); |
| 252 | clk_disable_unprepare(port->sys_ck); |
| 253 | mtk_pcie_port_free(port); |
| 254 | } |
| 255 | |
| 256 | mtk_pcie_subsys_powerdown(pcie); |
| 257 | } |
| 258 | |
| 259 | static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port) |
| 260 | { |
| 261 | u32 val; |
| 262 | int err; |
| 263 | |
| 264 | err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val, |
| 265 | !(val & APP_CFG_REQ), 10, |
| 266 | 100 * USEC_PER_MSEC); |
| 267 | if (err) |
| 268 | return PCIBIOS_SET_FAILED; |
| 269 | |
| 270 | if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS) |
| 271 | return PCIBIOS_SET_FAILED; |
| 272 | |
| 273 | return PCIBIOS_SUCCESSFUL; |
| 274 | } |
| 275 | |
| 276 | static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn, |
| 277 | int where, int size, u32 *val) |
| 278 | { |
| 279 | u32 tmp; |
| 280 | |
| 281 | /* Write PCIe configuration transaction header for Cfgrd */ |
| 282 | writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT), |
| 283 | port->base + PCIE_CFG_HEADER0); |
| 284 | writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); |
| 285 | writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus), |
| 286 | port->base + PCIE_CFG_HEADER2); |
| 287 | |
| 288 | /* Trigger h/w to transmit Cfgrd TLP */ |
| 289 | tmp = readl(port->base + PCIE_APP_TLP_REQ); |
| 290 | tmp |= APP_CFG_REQ; |
| 291 | writel(tmp, port->base + PCIE_APP_TLP_REQ); |
| 292 | |
| 293 | /* Check completion status */ |
| 294 | if (mtk_pcie_check_cfg_cpld(port)) |
| 295 | return PCIBIOS_SET_FAILED; |
| 296 | |
| 297 | /* Read cpld payload of Cfgrd */ |
| 298 | *val = readl(port->base + PCIE_CFG_RDATA); |
| 299 | |
| 300 | if (size == 1) |
| 301 | *val = (*val >> (8 * (where & 3))) & 0xff; |
| 302 | else if (size == 2) |
| 303 | *val = (*val >> (8 * (where & 3))) & 0xffff; |
| 304 | |
| 305 | return PCIBIOS_SUCCESSFUL; |
| 306 | } |
| 307 | |
| 308 | static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn, |
| 309 | int where, int size, u32 val) |
| 310 | { |
| 311 | /* Write PCIe configuration transaction header for Cfgwr */ |
| 312 | writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT), |
| 313 | port->base + PCIE_CFG_HEADER0); |
| 314 | writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); |
| 315 | writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus), |
| 316 | port->base + PCIE_CFG_HEADER2); |
| 317 | |
| 318 | /* Write Cfgwr data */ |
| 319 | val = val << 8 * (where & 3); |
| 320 | writel(val, port->base + PCIE_CFG_WDATA); |
| 321 | |
| 322 | /* Trigger h/w to transmit Cfgwr TLP */ |
| 323 | val = readl(port->base + PCIE_APP_TLP_REQ); |
| 324 | val |= APP_CFG_REQ; |
| 325 | writel(val, port->base + PCIE_APP_TLP_REQ); |
| 326 | |
| 327 | /* Check completion status */ |
| 328 | return mtk_pcie_check_cfg_cpld(port); |
| 329 | } |
| 330 | |
| 331 | static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus, |
| 332 | unsigned int devfn) |
| 333 | { |
| 334 | struct mtk_pcie *pcie = bus->sysdata; |
| 335 | struct mtk_pcie_port *port; |
| 336 | struct pci_dev *dev = NULL; |
| 337 | |
| 338 | /* |
| 339 | * Walk the bus hierarchy to get the devfn value |
| 340 | * of the port in the root bus. |
| 341 | */ |
| 342 | while (bus && bus->number) { |
| 343 | dev = bus->self; |
| 344 | bus = dev->bus; |
| 345 | devfn = dev->devfn; |
| 346 | } |
| 347 | |
| 348 | list_for_each_entry(port, &pcie->ports, list) |
| 349 | if (port->slot == PCI_SLOT(devfn)) |
| 350 | return port; |
| 351 | |
| 352 | return NULL; |
| 353 | } |
| 354 | |
| 355 | static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn, |
| 356 | int where, int size, u32 *val) |
| 357 | { |
| 358 | struct mtk_pcie_port *port; |
| 359 | u32 bn = bus->number; |
| 360 | int ret; |
| 361 | |
| 362 | port = mtk_pcie_find_port(bus, devfn); |
| 363 | if (!port) { |
| 364 | *val = ~0; |
| 365 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 366 | } |
| 367 | |
| 368 | ret = mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val); |
| 369 | if (ret) |
| 370 | *val = ~0; |
| 371 | |
| 372 | return ret; |
| 373 | } |
| 374 | |
| 375 | static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn, |
| 376 | int where, int size, u32 val) |
| 377 | { |
| 378 | struct mtk_pcie_port *port; |
| 379 | u32 bn = bus->number; |
| 380 | |
| 381 | port = mtk_pcie_find_port(bus, devfn); |
| 382 | if (!port) |
| 383 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 384 | |
| 385 | return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val); |
| 386 | } |
| 387 | |
| 388 | static struct pci_ops mtk_pcie_ops_v2 = { |
| 389 | .read = mtk_pcie_config_read, |
| 390 | .write = mtk_pcie_config_write, |
| 391 | }; |
| 392 | |
| 393 | static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 394 | { |
| 395 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 396 | phys_addr_t addr; |
| 397 | |
| 398 | /* MT2712/MT7622 only support 32-bit MSI addresses */ |
| 399 | addr = virt_to_phys(port->base + PCIE_MSI_VECTOR); |
| 400 | msg->address_hi = 0; |
| 401 | msg->address_lo = lower_32_bits(addr); |
| 402 | |
| 403 | msg->data = data->hwirq; |
| 404 | |
| 405 | dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n", |
| 406 | (int)data->hwirq, msg->address_hi, msg->address_lo); |
| 407 | } |
| 408 | |
| 409 | static int mtk_msi_set_affinity(struct irq_data *irq_data, |
| 410 | const struct cpumask *mask, bool force) |
| 411 | { |
| 412 | return -EINVAL; |
| 413 | } |
| 414 | |
| 415 | static void mtk_msi_ack_irq(struct irq_data *data) |
| 416 | { |
| 417 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); |
| 418 | u32 hwirq = data->hwirq; |
| 419 | |
| 420 | writel(1 << hwirq, port->base + PCIE_IMSI_STATUS); |
| 421 | } |
| 422 | |
| 423 | static struct irq_chip mtk_msi_bottom_irq_chip = { |
| 424 | .name = "MTK MSI", |
| 425 | .irq_compose_msi_msg = mtk_compose_msi_msg, |
| 426 | .irq_set_affinity = mtk_msi_set_affinity, |
| 427 | .irq_ack = mtk_msi_ack_irq, |
| 428 | }; |
| 429 | |
| 430 | static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, |
| 431 | unsigned int nr_irqs, void *args) |
| 432 | { |
| 433 | struct mtk_pcie_port *port = domain->host_data; |
| 434 | unsigned long bit; |
| 435 | |
| 436 | WARN_ON(nr_irqs != 1); |
| 437 | mutex_lock(&port->lock); |
| 438 | |
| 439 | bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM); |
| 440 | if (bit >= MTK_MSI_IRQS_NUM) { |
| 441 | mutex_unlock(&port->lock); |
| 442 | return -ENOSPC; |
| 443 | } |
| 444 | |
| 445 | __set_bit(bit, port->msi_irq_in_use); |
| 446 | |
| 447 | mutex_unlock(&port->lock); |
| 448 | |
| 449 | irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip, |
| 450 | domain->host_data, handle_edge_irq, |
| 451 | NULL, NULL); |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | static void mtk_pcie_irq_domain_free(struct irq_domain *domain, |
| 457 | unsigned int virq, unsigned int nr_irqs) |
| 458 | { |
| 459 | struct irq_data *d = irq_domain_get_irq_data(domain, virq); |
| 460 | struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d); |
| 461 | |
| 462 | mutex_lock(&port->lock); |
| 463 | |
| 464 | if (!test_bit(d->hwirq, port->msi_irq_in_use)) |
| 465 | dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n", |
| 466 | d->hwirq); |
| 467 | else |
| 468 | __clear_bit(d->hwirq, port->msi_irq_in_use); |
| 469 | |
| 470 | mutex_unlock(&port->lock); |
| 471 | |
| 472 | irq_domain_free_irqs_parent(domain, virq, nr_irqs); |
| 473 | } |
| 474 | |
| 475 | static const struct irq_domain_ops msi_domain_ops = { |
| 476 | .alloc = mtk_pcie_irq_domain_alloc, |
| 477 | .free = mtk_pcie_irq_domain_free, |
| 478 | }; |
| 479 | |
| 480 | static struct irq_chip mtk_msi_irq_chip = { |
| 481 | .name = "MTK PCIe MSI", |
| 482 | .irq_ack = irq_chip_ack_parent, |
| 483 | .irq_mask = pci_msi_mask_irq, |
| 484 | .irq_unmask = pci_msi_unmask_irq, |
| 485 | }; |
| 486 | |
| 487 | static struct msi_domain_info mtk_msi_domain_info = { |
| 488 | .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | |
| 489 | MSI_FLAG_PCI_MSIX), |
| 490 | .chip = &mtk_msi_irq_chip, |
| 491 | }; |
| 492 | |
| 493 | static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port) |
| 494 | { |
| 495 | struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node); |
| 496 | |
| 497 | mutex_init(&port->lock); |
| 498 | |
| 499 | port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM, |
| 500 | &msi_domain_ops, port); |
| 501 | if (!port->inner_domain) { |
| 502 | dev_err(port->pcie->dev, "failed to create IRQ domain\n"); |
| 503 | return -ENOMEM; |
| 504 | } |
| 505 | |
| 506 | port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info, |
| 507 | port->inner_domain); |
| 508 | if (!port->msi_domain) { |
| 509 | dev_err(port->pcie->dev, "failed to create MSI domain\n"); |
| 510 | irq_domain_remove(port->inner_domain); |
| 511 | return -ENOMEM; |
| 512 | } |
| 513 | |
| 514 | return 0; |
| 515 | } |
| 516 | |
| 517 | static void mtk_pcie_enable_msi(struct mtk_pcie_port *port) |
| 518 | { |
| 519 | u32 val; |
| 520 | phys_addr_t msg_addr; |
| 521 | |
| 522 | msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR); |
| 523 | val = lower_32_bits(msg_addr); |
| 524 | writel(val, port->base + PCIE_IMSI_ADDR); |
| 525 | |
| 526 | val = readl(port->base + PCIE_INT_MASK); |
| 527 | val &= ~MSI_MASK; |
| 528 | writel(val, port->base + PCIE_INT_MASK); |
| 529 | } |
| 530 | |
| 531 | static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie) |
| 532 | { |
| 533 | struct mtk_pcie_port *port, *tmp; |
| 534 | |
| 535 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) { |
| 536 | irq_set_chained_handler_and_data(port->irq, NULL, NULL); |
| 537 | |
| 538 | if (port->irq_domain) |
| 539 | irq_domain_remove(port->irq_domain); |
| 540 | |
| 541 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 542 | if (port->msi_domain) |
| 543 | irq_domain_remove(port->msi_domain); |
| 544 | if (port->inner_domain) |
| 545 | irq_domain_remove(port->inner_domain); |
| 546 | } |
| 547 | |
| 548 | irq_dispose_mapping(port->irq); |
| 549 | } |
| 550 | } |
| 551 | |
| 552 | static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 553 | irq_hw_number_t hwirq) |
| 554 | { |
| 555 | irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq); |
| 556 | irq_set_chip_data(irq, domain->host_data); |
| 557 | |
| 558 | return 0; |
| 559 | } |
| 560 | |
| 561 | static const struct irq_domain_ops intx_domain_ops = { |
| 562 | .map = mtk_pcie_intx_map, |
| 563 | }; |
| 564 | |
| 565 | static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port, |
| 566 | struct device_node *node) |
| 567 | { |
| 568 | struct device *dev = port->pcie->dev; |
| 569 | struct device_node *pcie_intc_node; |
| 570 | int ret; |
| 571 | |
| 572 | /* Setup INTx */ |
| 573 | pcie_intc_node = of_get_next_child(node, NULL); |
| 574 | if (!pcie_intc_node) { |
| 575 | dev_err(dev, "no PCIe Intc node found\n"); |
| 576 | return -ENODEV; |
| 577 | } |
| 578 | |
| 579 | port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, |
| 580 | &intx_domain_ops, port); |
| 581 | if (!port->irq_domain) { |
| 582 | dev_err(dev, "failed to get INTx IRQ domain\n"); |
| 583 | return -ENODEV; |
| 584 | } |
| 585 | |
| 586 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 587 | ret = mtk_pcie_allocate_msi_domains(port); |
| 588 | if (ret) |
| 589 | return ret; |
| 590 | } |
| 591 | |
| 592 | return 0; |
| 593 | } |
| 594 | |
| 595 | static void mtk_pcie_intr_handler(struct irq_desc *desc) |
| 596 | { |
| 597 | struct mtk_pcie_port *port = irq_desc_get_handler_data(desc); |
| 598 | struct irq_chip *irqchip = irq_desc_get_chip(desc); |
| 599 | unsigned long status; |
| 600 | u32 virq; |
| 601 | u32 bit = INTX_SHIFT; |
| 602 | |
| 603 | chained_irq_enter(irqchip, desc); |
| 604 | |
| 605 | status = readl(port->base + PCIE_INT_STATUS); |
| 606 | if (status & INTX_MASK) { |
| 607 | for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) { |
| 608 | /* Clear the INTx */ |
| 609 | writel(1 << bit, port->base + PCIE_INT_STATUS); |
| 610 | virq = irq_find_mapping(port->irq_domain, |
| 611 | bit - INTX_SHIFT); |
| 612 | generic_handle_irq(virq); |
| 613 | } |
| 614 | } |
| 615 | |
| 616 | if (IS_ENABLED(CONFIG_PCI_MSI)) { |
| 617 | if (status & MSI_STATUS){ |
| 618 | unsigned long imsi_status; |
| 619 | |
| 620 | while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) { |
| 621 | for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) { |
| 622 | virq = irq_find_mapping(port->inner_domain, bit); |
| 623 | generic_handle_irq(virq); |
| 624 | } |
| 625 | } |
| 626 | /* Clear MSI interrupt status */ |
| 627 | writel(MSI_STATUS, port->base + PCIE_INT_STATUS); |
| 628 | } |
| 629 | } |
| 630 | |
| 631 | chained_irq_exit(irqchip, desc); |
| 632 | |
| 633 | return; |
| 634 | } |
| 635 | |
| 636 | static int mtk_pcie_setup_irq(struct mtk_pcie_port *port, |
| 637 | struct device_node *node) |
| 638 | { |
| 639 | struct mtk_pcie *pcie = port->pcie; |
| 640 | struct device *dev = pcie->dev; |
| 641 | struct platform_device *pdev = to_platform_device(dev); |
| 642 | int err; |
| 643 | |
| 644 | err = mtk_pcie_init_irq_domain(port, node); |
| 645 | if (err) { |
| 646 | dev_err(dev, "failed to init PCIe IRQ domain\n"); |
| 647 | return err; |
| 648 | } |
| 649 | |
| 650 | port->irq = platform_get_irq(pdev, port->slot); |
| 651 | irq_set_chained_handler_and_data(port->irq, |
| 652 | mtk_pcie_intr_handler, port); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) |
| 658 | { |
| 659 | struct mtk_pcie *pcie = port->pcie; |
| 660 | struct resource *mem = &pcie->mem; |
| 661 | const struct mtk_pcie_soc *soc = port->pcie->soc; |
| 662 | u32 val; |
| 663 | size_t size; |
| 664 | int err; |
| 665 | |
| 666 | /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */ |
| 667 | if (pcie->base) { |
| 668 | val = readl(pcie->base + PCIE_SYS_CFG_V2); |
| 669 | val |= PCIE_CSR_LTSSM_EN(port->slot) | |
| 670 | PCIE_CSR_ASPM_L1_EN(port->slot); |
| 671 | writel(val, pcie->base + PCIE_SYS_CFG_V2); |
| 672 | } |
| 673 | |
| 674 | /* Assert all reset signals */ |
| 675 | writel(0, port->base + PCIE_RST_CTRL); |
| 676 | |
| 677 | /* |
| 678 | * Enable PCIe link down reset, if link status changed from link up to |
| 679 | * link down, this will reset MAC control registers and configuration |
| 680 | * space. |
| 681 | */ |
| 682 | writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL); |
| 683 | |
| 684 | /* De-assert PHY, PE, PIPE, MAC and configuration reset */ |
| 685 | val = readl(port->base + PCIE_RST_CTRL); |
| 686 | val |= PCIE_PHY_RSTB | PCIE_PIPE_SRSTB | |
| 687 | PCIE_MAC_SRSTB | PCIE_CRSTB; |
| 688 | writel(val, port->base + PCIE_RST_CTRL); |
| 689 | |
| 690 | usleep_range(100 * 1000, 120 * 1000); |
| 691 | val |= PCIE_PERSTB; |
| 692 | writel(val, port->base + PCIE_RST_CTRL); |
| 693 | |
| 694 | /* Set up vendor ID and class code */ |
| 695 | if (soc->need_fix_class_id) { |
| 696 | val = PCI_VENDOR_ID_MEDIATEK; |
| 697 | writew(val, port->base + PCIE_CONF_VEND_ID); |
| 698 | |
| 699 | val = PCI_CLASS_BRIDGE_PCI; |
| 700 | writew(val, port->base + PCIE_CONF_CLASS_ID); |
| 701 | } |
| 702 | |
| 703 | /* 100ms timeout value should be enough for Gen1/2 training */ |
| 704 | err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val, |
| 705 | !!(val & PCIE_PORT_LINKUP_V2), 20, |
| 706 | 100 * USEC_PER_MSEC); |
| 707 | if (err) |
| 708 | return -ETIMEDOUT; |
| 709 | |
| 710 | /* Set INTx mask */ |
| 711 | val = readl(port->base + PCIE_INT_MASK); |
| 712 | val &= ~INTX_MASK; |
| 713 | writel(val, port->base + PCIE_INT_MASK); |
| 714 | |
| 715 | if (IS_ENABLED(CONFIG_PCI_MSI)) |
| 716 | mtk_pcie_enable_msi(port); |
| 717 | |
| 718 | /* Set AHB to PCIe translation windows */ |
| 719 | size = mem->end - mem->start; |
| 720 | val = lower_32_bits(mem->start) | AHB2PCIE_SIZE(fls(size)); |
| 721 | writel(val, port->base + PCIE_AHB_TRANS_BASE0_L); |
| 722 | |
| 723 | val = upper_32_bits(mem->start); |
| 724 | writel(val, port->base + PCIE_AHB_TRANS_BASE0_H); |
| 725 | |
| 726 | /* Set PCIe to AXI translation memory space.*/ |
| 727 | val = fls(0xffffffff) | WIN_ENABLE; |
| 728 | writel(val, port->base + PCIE_AXI_WINDOW0); |
| 729 | |
| 730 | return 0; |
| 731 | } |
| 732 | |
| 733 | static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus, |
| 734 | unsigned int devfn, int where) |
| 735 | { |
| 736 | struct mtk_pcie *pcie = bus->sysdata; |
| 737 | |
| 738 | writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn), |
| 739 | bus->number), pcie->base + PCIE_CFG_ADDR); |
| 740 | |
| 741 | return pcie->base + PCIE_CFG_DATA + (where & 3); |
| 742 | } |
| 743 | |
| 744 | static struct pci_ops mtk_pcie_ops = { |
| 745 | .map_bus = mtk_pcie_map_bus, |
| 746 | .read = pci_generic_config_read, |
| 747 | .write = pci_generic_config_write, |
| 748 | }; |
| 749 | |
| 750 | static int mtk_pcie_startup_port(struct mtk_pcie_port *port) |
| 751 | { |
| 752 | struct mtk_pcie *pcie = port->pcie; |
| 753 | u32 func = PCI_FUNC(port->slot << 3); |
| 754 | u32 slot = PCI_SLOT(port->slot << 3); |
| 755 | u32 val; |
| 756 | int err; |
| 757 | |
| 758 | /* assert port PERST_N */ |
| 759 | val = readl(pcie->base + PCIE_SYS_CFG); |
| 760 | val |= PCIE_PORT_PERST(port->slot); |
| 761 | writel(val, pcie->base + PCIE_SYS_CFG); |
| 762 | |
| 763 | /* de-assert port PERST_N */ |
| 764 | val = readl(pcie->base + PCIE_SYS_CFG); |
| 765 | val &= ~PCIE_PORT_PERST(port->slot); |
| 766 | writel(val, pcie->base + PCIE_SYS_CFG); |
| 767 | |
| 768 | /* 100ms timeout value should be enough for Gen1/2 training */ |
| 769 | err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val, |
| 770 | !!(val & PCIE_PORT_LINKUP), 20, |
| 771 | 100 * USEC_PER_MSEC); |
| 772 | if (err) |
| 773 | return -ETIMEDOUT; |
| 774 | |
| 775 | /* enable interrupt */ |
| 776 | val = readl(pcie->base + PCIE_INT_ENABLE); |
| 777 | val |= PCIE_PORT_INT_EN(port->slot); |
| 778 | writel(val, pcie->base + PCIE_INT_ENABLE); |
| 779 | |
| 780 | /* map to all DDR region. We need to set it before cfg operation. */ |
| 781 | writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE, |
| 782 | port->base + PCIE_BAR0_SETUP); |
| 783 | |
| 784 | /* configure class code and revision ID */ |
| 785 | writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS); |
| 786 | |
| 787 | /* configure FC credit */ |
| 788 | writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0), |
| 789 | pcie->base + PCIE_CFG_ADDR); |
| 790 | val = readl(pcie->base + PCIE_CFG_DATA); |
| 791 | val &= ~PCIE_FC_CREDIT_MASK; |
| 792 | val |= PCIE_FC_CREDIT_VAL(0x806c); |
| 793 | writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0), |
| 794 | pcie->base + PCIE_CFG_ADDR); |
| 795 | writel(val, pcie->base + PCIE_CFG_DATA); |
| 796 | |
| 797 | /* configure RC FTS number to 250 when it leaves L0s */ |
| 798 | writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0), |
| 799 | pcie->base + PCIE_CFG_ADDR); |
| 800 | val = readl(pcie->base + PCIE_CFG_DATA); |
| 801 | val &= ~PCIE_FTS_NUM_MASK; |
| 802 | val |= PCIE_FTS_NUM_L0(0x50); |
| 803 | writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0), |
| 804 | pcie->base + PCIE_CFG_ADDR); |
| 805 | writel(val, pcie->base + PCIE_CFG_DATA); |
| 806 | |
| 807 | return 0; |
| 808 | } |
| 809 | |
| 810 | static void mtk_pcie_enable_port(struct mtk_pcie_port *port) |
| 811 | { |
| 812 | struct mtk_pcie *pcie = port->pcie; |
| 813 | struct device *dev = pcie->dev; |
| 814 | int err; |
| 815 | |
| 816 | err = clk_prepare_enable(port->sys_ck); |
| 817 | if (err) { |
| 818 | dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot); |
| 819 | goto err_sys_clk; |
| 820 | } |
| 821 | |
| 822 | err = clk_prepare_enable(port->ahb_ck); |
| 823 | if (err) { |
| 824 | dev_err(dev, "failed to enable ahb_ck%d\n", port->slot); |
| 825 | goto err_ahb_clk; |
| 826 | } |
| 827 | |
| 828 | err = clk_prepare_enable(port->aux_ck); |
| 829 | if (err) { |
| 830 | dev_err(dev, "failed to enable aux_ck%d\n", port->slot); |
| 831 | goto err_aux_clk; |
| 832 | } |
| 833 | |
| 834 | err = clk_prepare_enable(port->axi_ck); |
| 835 | if (err) { |
| 836 | dev_err(dev, "failed to enable axi_ck%d\n", port->slot); |
| 837 | goto err_axi_clk; |
| 838 | } |
| 839 | |
| 840 | err = clk_prepare_enable(port->obff_ck); |
| 841 | if (err) { |
| 842 | dev_err(dev, "failed to enable obff_ck%d\n", port->slot); |
| 843 | goto err_obff_clk; |
| 844 | } |
| 845 | |
| 846 | err = clk_prepare_enable(port->pipe_ck); |
| 847 | if (err) { |
| 848 | dev_err(dev, "failed to enable pipe_ck%d\n", port->slot); |
| 849 | goto err_pipe_clk; |
| 850 | } |
| 851 | |
| 852 | reset_control_assert(port->reset); |
| 853 | reset_control_deassert(port->reset); |
| 854 | |
| 855 | err = phy_init(port->phy); |
| 856 | if (err) { |
| 857 | dev_err(dev, "failed to initialize port%d phy\n", port->slot); |
| 858 | goto err_phy_init; |
| 859 | } |
| 860 | |
| 861 | err = phy_power_on(port->phy); |
| 862 | if (err) { |
| 863 | dev_err(dev, "failed to power on port%d phy\n", port->slot); |
| 864 | goto err_phy_on; |
| 865 | } |
| 866 | |
| 867 | err = phy_set_mode(port->phy, PHY_MODE_PCIE); |
| 868 | if (err) { |
| 869 | dev_err(dev, "failed to set mode on port%d phy\n", port->slot); |
| 870 | goto err_phy_on; |
| 871 | } |
| 872 | |
| 873 | if (!pcie->soc->startup(port)) |
| 874 | return; |
| 875 | |
| 876 | dev_info(dev, "Port%d link down\n", port->slot); |
| 877 | |
| 878 | phy_power_off(port->phy); |
| 879 | err_phy_on: |
| 880 | phy_exit(port->phy); |
| 881 | err_phy_init: |
| 882 | clk_disable_unprepare(port->pipe_ck); |
| 883 | err_pipe_clk: |
| 884 | clk_disable_unprepare(port->obff_ck); |
| 885 | err_obff_clk: |
| 886 | clk_disable_unprepare(port->axi_ck); |
| 887 | err_axi_clk: |
| 888 | clk_disable_unprepare(port->aux_ck); |
| 889 | err_aux_clk: |
| 890 | clk_disable_unprepare(port->ahb_ck); |
| 891 | err_ahb_clk: |
| 892 | clk_disable_unprepare(port->sys_ck); |
| 893 | err_sys_clk: |
| 894 | mtk_pcie_port_free(port); |
| 895 | } |
| 896 | |
| 897 | static int mtk_pcie_parse_port(struct mtk_pcie *pcie, |
| 898 | struct device_node *node, |
| 899 | int slot) |
| 900 | { |
| 901 | struct mtk_pcie_port *port; |
| 902 | struct resource *regs; |
| 903 | struct device *dev = pcie->dev; |
| 904 | struct platform_device *pdev = to_platform_device(dev); |
| 905 | char name[10]; |
| 906 | int err; |
| 907 | |
| 908 | port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); |
| 909 | if (!port) |
| 910 | return -ENOMEM; |
| 911 | |
| 912 | snprintf(name, sizeof(name), "port%d", slot); |
| 913 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, name); |
| 914 | port->base = devm_ioremap_resource(dev, regs); |
| 915 | if (IS_ERR(port->base)) { |
| 916 | dev_err(dev, "failed to map port%d base\n", slot); |
| 917 | return PTR_ERR(port->base); |
| 918 | } |
| 919 | |
| 920 | snprintf(name, sizeof(name), "sys_ck%d", slot); |
| 921 | port->sys_ck = devm_clk_get(dev, name); |
| 922 | if (IS_ERR(port->sys_ck)) { |
| 923 | dev_err(dev, "failed to get sys_ck%d clock\n", slot); |
| 924 | return PTR_ERR(port->sys_ck); |
| 925 | } |
| 926 | |
| 927 | /* sys_ck might be divided into the following parts in some chips */ |
| 928 | snprintf(name, sizeof(name), "ahb_ck%d", slot); |
| 929 | port->ahb_ck = devm_clk_get(dev, name); |
| 930 | if (IS_ERR(port->ahb_ck)) { |
| 931 | if (PTR_ERR(port->ahb_ck) == -EPROBE_DEFER) |
| 932 | return -EPROBE_DEFER; |
| 933 | |
| 934 | port->ahb_ck = NULL; |
| 935 | } |
| 936 | |
| 937 | snprintf(name, sizeof(name), "axi_ck%d", slot); |
| 938 | port->axi_ck = devm_clk_get(dev, name); |
| 939 | if (IS_ERR(port->axi_ck)) { |
| 940 | if (PTR_ERR(port->axi_ck) == -EPROBE_DEFER) |
| 941 | return -EPROBE_DEFER; |
| 942 | |
| 943 | port->axi_ck = NULL; |
| 944 | } |
| 945 | |
| 946 | snprintf(name, sizeof(name), "aux_ck%d", slot); |
| 947 | port->aux_ck = devm_clk_get(dev, name); |
| 948 | if (IS_ERR(port->aux_ck)) { |
| 949 | if (PTR_ERR(port->aux_ck) == -EPROBE_DEFER) |
| 950 | return -EPROBE_DEFER; |
| 951 | |
| 952 | port->aux_ck = NULL; |
| 953 | } |
| 954 | |
| 955 | snprintf(name, sizeof(name), "obff_ck%d", slot); |
| 956 | port->obff_ck = devm_clk_get(dev, name); |
| 957 | if (IS_ERR(port->obff_ck)) { |
| 958 | if (PTR_ERR(port->obff_ck) == -EPROBE_DEFER) |
| 959 | return -EPROBE_DEFER; |
| 960 | |
| 961 | port->obff_ck = NULL; |
| 962 | } |
| 963 | |
| 964 | snprintf(name, sizeof(name), "pipe_ck%d", slot); |
| 965 | port->pipe_ck = devm_clk_get(dev, name); |
| 966 | if (IS_ERR(port->pipe_ck)) { |
| 967 | if (PTR_ERR(port->pipe_ck) == -EPROBE_DEFER) |
| 968 | return -EPROBE_DEFER; |
| 969 | |
| 970 | port->pipe_ck = NULL; |
| 971 | } |
| 972 | |
| 973 | snprintf(name, sizeof(name), "pcie-rst%d", slot); |
| 974 | port->reset = devm_reset_control_get_optional_exclusive(dev, name); |
| 975 | if (PTR_ERR(port->reset) == -EPROBE_DEFER) |
| 976 | return PTR_ERR(port->reset); |
| 977 | |
| 978 | /* some platforms may use default PHY setting */ |
| 979 | snprintf(name, sizeof(name), "pcie-phy%d", slot); |
| 980 | port->phy = devm_phy_optional_get(dev, name); |
| 981 | if (IS_ERR(port->phy)) |
| 982 | return PTR_ERR(port->phy); |
| 983 | |
| 984 | port->slot = slot; |
| 985 | port->pcie = pcie; |
| 986 | |
| 987 | if (pcie->soc->setup_irq) { |
| 988 | err = pcie->soc->setup_irq(port, node); |
| 989 | if (err) |
| 990 | return err; |
| 991 | } |
| 992 | |
| 993 | INIT_LIST_HEAD(&port->list); |
| 994 | list_add_tail(&port->list, &pcie->ports); |
| 995 | |
| 996 | return 0; |
| 997 | } |
| 998 | |
| 999 | static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie) |
| 1000 | { |
| 1001 | struct device *dev = pcie->dev; |
| 1002 | struct platform_device *pdev = to_platform_device(dev); |
| 1003 | struct resource *regs; |
| 1004 | int err; |
| 1005 | |
| 1006 | /* get shared registers, which are optional */ |
| 1007 | regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys"); |
| 1008 | if (regs) { |
| 1009 | pcie->base = devm_ioremap_resource(dev, regs); |
| 1010 | if (IS_ERR(pcie->base)) { |
| 1011 | dev_err(dev, "failed to map shared register\n"); |
| 1012 | return PTR_ERR(pcie->base); |
| 1013 | } |
| 1014 | } |
| 1015 | |
| 1016 | pcie->free_ck = devm_clk_get(dev, "free_ck"); |
| 1017 | if (IS_ERR(pcie->free_ck)) { |
| 1018 | if (PTR_ERR(pcie->free_ck) == -EPROBE_DEFER) |
| 1019 | return -EPROBE_DEFER; |
| 1020 | |
| 1021 | pcie->free_ck = NULL; |
| 1022 | } |
| 1023 | |
| 1024 | pm_runtime_enable(dev); |
| 1025 | pm_runtime_get_sync(dev); |
| 1026 | |
| 1027 | /* enable top level clock */ |
| 1028 | err = clk_prepare_enable(pcie->free_ck); |
| 1029 | if (err) { |
| 1030 | dev_err(dev, "failed to enable free_ck\n"); |
| 1031 | goto err_free_ck; |
| 1032 | } |
| 1033 | |
| 1034 | return 0; |
| 1035 | |
| 1036 | err_free_ck: |
| 1037 | pm_runtime_put_sync(dev); |
| 1038 | pm_runtime_disable(dev); |
| 1039 | |
| 1040 | return err; |
| 1041 | } |
| 1042 | |
| 1043 | static int mtk_pcie_setup(struct mtk_pcie *pcie) |
| 1044 | { |
| 1045 | struct device *dev = pcie->dev; |
| 1046 | struct device_node *node = dev->of_node, *child; |
| 1047 | struct mtk_pcie_port *port, *tmp; |
| 1048 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
| 1049 | struct list_head *windows = &host->windows; |
| 1050 | struct resource_entry *win, *tmp_win; |
| 1051 | resource_size_t io_base; |
| 1052 | int err; |
| 1053 | |
| 1054 | err = of_pci_get_host_bridge_resources(node, 0, 0xff, |
| 1055 | windows, &io_base); |
| 1056 | if (err) |
| 1057 | return err; |
| 1058 | |
| 1059 | err = devm_request_pci_bus_resources(dev, windows); |
| 1060 | if (err < 0) |
| 1061 | return err; |
| 1062 | |
| 1063 | /* Get the I/O and memory ranges from DT */ |
| 1064 | resource_list_for_each_entry_safe(win, tmp_win, windows) { |
| 1065 | switch (resource_type(win->res)) { |
| 1066 | case IORESOURCE_IO: |
| 1067 | err = devm_pci_remap_iospace(dev, win->res, io_base); |
| 1068 | if (err) { |
| 1069 | dev_warn(dev, "error %d: failed to map resource %pR\n", |
| 1070 | err, win->res); |
| 1071 | resource_list_destroy_entry(win); |
| 1072 | } |
| 1073 | break; |
| 1074 | case IORESOURCE_MEM: |
| 1075 | memcpy(&pcie->mem, win->res, sizeof(*win->res)); |
| 1076 | pcie->mem.name = "non-prefetchable"; |
| 1077 | break; |
| 1078 | case IORESOURCE_BUS: |
| 1079 | pcie->busnr = win->res->start; |
| 1080 | break; |
| 1081 | } |
| 1082 | } |
| 1083 | |
| 1084 | for_each_available_child_of_node(node, child) { |
| 1085 | int slot; |
| 1086 | |
| 1087 | err = of_pci_get_devfn(child); |
| 1088 | if (err < 0) { |
| 1089 | dev_err(dev, "failed to parse devfn: %d\n", err); |
| 1090 | return err; |
| 1091 | } |
| 1092 | |
| 1093 | slot = PCI_SLOT(err); |
| 1094 | |
| 1095 | err = mtk_pcie_parse_port(pcie, child, slot); |
| 1096 | if (err) |
| 1097 | return err; |
| 1098 | } |
| 1099 | |
| 1100 | err = mtk_pcie_subsys_powerup(pcie); |
| 1101 | if (err) |
| 1102 | return err; |
| 1103 | |
| 1104 | /* enable each port, and then check link status */ |
| 1105 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) |
| 1106 | mtk_pcie_enable_port(port); |
| 1107 | |
| 1108 | /* power down PCIe subsys if slots are all empty (link down) */ |
| 1109 | if (list_empty(&pcie->ports)) |
| 1110 | mtk_pcie_subsys_powerdown(pcie); |
| 1111 | |
| 1112 | return 0; |
| 1113 | } |
| 1114 | |
| 1115 | static int mtk_pcie_probe(struct platform_device *pdev) |
| 1116 | { |
| 1117 | struct device *dev = &pdev->dev; |
| 1118 | struct mtk_pcie *pcie; |
| 1119 | struct pci_host_bridge *host; |
| 1120 | int err; |
| 1121 | |
| 1122 | host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); |
| 1123 | if (!host) |
| 1124 | return -ENOMEM; |
| 1125 | |
| 1126 | pcie = pci_host_bridge_priv(host); |
| 1127 | |
| 1128 | pcie->dev = dev; |
| 1129 | pcie->soc = of_device_get_match_data(dev); |
| 1130 | platform_set_drvdata(pdev, pcie); |
| 1131 | INIT_LIST_HEAD(&pcie->ports); |
| 1132 | |
| 1133 | err = mtk_pcie_setup(pcie); |
| 1134 | if (err) |
| 1135 | return err; |
| 1136 | |
| 1137 | host->busnr = pcie->busnr; |
| 1138 | host->dev.parent = pcie->dev; |
| 1139 | host->ops = pcie->soc->ops; |
| 1140 | host->map_irq = of_irq_parse_and_map_pci; |
| 1141 | host->swizzle_irq = pci_common_swizzle; |
| 1142 | host->sysdata = pcie; |
| 1143 | |
| 1144 | err = pci_host_probe(host); |
| 1145 | if (err) |
| 1146 | goto put_resources; |
| 1147 | |
| 1148 | return 0; |
| 1149 | |
| 1150 | put_resources: |
| 1151 | if (!list_empty(&pcie->ports)) |
| 1152 | mtk_pcie_put_resources(pcie); |
| 1153 | |
| 1154 | return err; |
| 1155 | } |
| 1156 | |
| 1157 | static void mtk_pcie_free_resources(struct mtk_pcie *pcie) |
| 1158 | { |
| 1159 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
| 1160 | struct list_head *windows = &host->windows; |
| 1161 | |
| 1162 | pci_free_resource_list(windows); |
| 1163 | } |
| 1164 | |
| 1165 | static int mtk_pcie_remove(struct platform_device *pdev) |
| 1166 | { |
| 1167 | struct mtk_pcie *pcie = platform_get_drvdata(pdev); |
| 1168 | struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); |
| 1169 | |
| 1170 | pci_stop_root_bus(host->bus); |
| 1171 | pci_remove_root_bus(host->bus); |
| 1172 | mtk_pcie_free_resources(pcie); |
| 1173 | |
| 1174 | mtk_pcie_irq_teardown(pcie); |
| 1175 | |
| 1176 | mtk_pcie_put_resources(pcie); |
| 1177 | |
| 1178 | return 0; |
| 1179 | } |
| 1180 | |
| 1181 | static int __maybe_unused mtk_pcie_suspend_noirq(struct device *dev) |
| 1182 | { |
| 1183 | struct mtk_pcie *pcie = dev_get_drvdata(dev); |
| 1184 | struct mtk_pcie_port *port; |
| 1185 | int ret; |
| 1186 | |
| 1187 | if (list_empty(&pcie->ports)) |
| 1188 | return 0; |
| 1189 | |
| 1190 | list_for_each_entry(port, &pcie->ports, list) { |
| 1191 | clk_disable_unprepare(port->pipe_ck); |
| 1192 | clk_disable_unprepare(port->obff_ck); |
| 1193 | clk_disable_unprepare(port->axi_ck); |
| 1194 | clk_disable_unprepare(port->aux_ck); |
| 1195 | clk_disable_unprepare(port->ahb_ck); |
| 1196 | clk_disable_unprepare(port->sys_ck); |
| 1197 | phy_power_off(port->phy); |
| 1198 | phy_exit(port->phy); |
| 1199 | } |
| 1200 | |
| 1201 | clk_disable_unprepare(pcie->free_ck); |
| 1202 | |
| 1203 | ret = pinctrl_pm_select_sleep_state(dev); |
| 1204 | if (ret) |
| 1205 | dev_info(dev, "Failed to set pin sleep state (%d)\n", ret); |
| 1206 | |
| 1207 | return 0; |
| 1208 | } |
| 1209 | |
| 1210 | static int __maybe_unused mtk_pcie_resume_noirq(struct device *dev) |
| 1211 | { |
| 1212 | struct mtk_pcie *pcie = dev_get_drvdata(dev); |
| 1213 | struct mtk_pcie_port *port, *tmp; |
| 1214 | int ret; |
| 1215 | |
| 1216 | if (list_empty(&pcie->ports)) |
| 1217 | return 0; |
| 1218 | |
| 1219 | ret = pinctrl_pm_select_default_state(dev); |
| 1220 | if (ret) { |
| 1221 | dev_info(dev, "Failed to set pin default state (%d)\n", ret); |
| 1222 | return ret; |
| 1223 | } |
| 1224 | |
| 1225 | clk_prepare_enable(pcie->free_ck); |
| 1226 | |
| 1227 | list_for_each_entry_safe(port, tmp, &pcie->ports, list) |
| 1228 | mtk_pcie_enable_port(port); |
| 1229 | |
| 1230 | /* In case of EP was removed while system suspend. */ |
| 1231 | if (list_empty(&pcie->ports)) |
| 1232 | clk_disable_unprepare(pcie->free_ck); |
| 1233 | |
| 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static const struct dev_pm_ops mtk_pcie_pm_ops = { |
| 1238 | SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq, |
| 1239 | mtk_pcie_resume_noirq) |
| 1240 | }; |
| 1241 | |
| 1242 | static const struct mtk_pcie_soc mtk_pcie_soc_v1 = { |
| 1243 | .ops = &mtk_pcie_ops, |
| 1244 | .startup = mtk_pcie_startup_port, |
| 1245 | }; |
| 1246 | |
| 1247 | static const struct mtk_pcie_soc mtk_pcie_soc_v2_common = { |
| 1248 | .ops = &mtk_pcie_ops_v2, |
| 1249 | .startup = mtk_pcie_startup_port_v2, |
| 1250 | .setup_irq = mtk_pcie_setup_irq, |
| 1251 | }; |
| 1252 | |
| 1253 | static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = { |
| 1254 | .need_fix_class_id = true, |
| 1255 | .ops = &mtk_pcie_ops_v2, |
| 1256 | .startup = mtk_pcie_startup_port_v2, |
| 1257 | .setup_irq = mtk_pcie_setup_irq, |
| 1258 | }; |
| 1259 | |
| 1260 | static const struct of_device_id mtk_pcie_ids[] = { |
| 1261 | { |
| 1262 | .compatible = "mediatek,mt2701-pcie", |
| 1263 | .data = &mtk_pcie_soc_v1 |
| 1264 | }, |
| 1265 | { |
| 1266 | .compatible = "mediatek,mt7623-pcie", |
| 1267 | .data = &mtk_pcie_soc_v1 |
| 1268 | }, |
| 1269 | { |
| 1270 | .compatible = "mediatek,mt2712-pcie", |
| 1271 | .data = &mtk_pcie_soc_v2_common |
| 1272 | }, |
| 1273 | { |
| 1274 | .compatible = "mediatek,mt2731-pcie", |
| 1275 | .data = &mtk_pcie_soc_v2_common |
| 1276 | }, |
| 1277 | { |
| 1278 | .compatible = "mediatek,mt7622-pcie", |
| 1279 | .data = &mtk_pcie_soc_mt7622 |
| 1280 | }, |
| 1281 | {}, |
| 1282 | }; |
| 1283 | |
| 1284 | static struct platform_driver mtk_pcie_driver = { |
| 1285 | .probe = mtk_pcie_probe, |
| 1286 | .remove = mtk_pcie_remove, |
| 1287 | .driver = { |
| 1288 | .name = "mtk-pcie", |
| 1289 | .of_match_table = mtk_pcie_ids, |
| 1290 | .suppress_bind_attrs = true, |
| 1291 | .pm = &mtk_pcie_pm_ops, |
| 1292 | }, |
| 1293 | }; |
| 1294 | module_platform_driver(mtk_pcie_driver); |
| 1295 | MODULE_LICENSE("GPL v2"); |
| 1296 | |
| 1297 | static void mtk_fixup_bar0(struct pci_dev *dev) |
| 1298 | { |
| 1299 | struct resource *dev_res = &dev->resource[0]; |
| 1300 | /* 32bit resource length will calculate size to 0, set it smaller */ |
| 1301 | if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) |
| 1302 | dev_res->end = 0xfffffffe; |
| 1303 | } |
| 1304 | DECLARE_PCI_FIXUP_HEADER(0x14c3, 0x2731, mtk_fixup_bar0); |