Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * MediaTek PCIe host controller driver.
0004  *
0005  * Copyright (c) 2017 MediaTek Inc.
0006  * Author: Ryder Lee <ryder.lee@mediatek.com>
0007  *     Honghui Zhang <honghui.zhang@mediatek.com>
0008  */
0009 
0010 #include <linux/clk.h>
0011 #include <linux/delay.h>
0012 #include <linux/iopoll.h>
0013 #include <linux/irq.h>
0014 #include <linux/irqchip/chained_irq.h>
0015 #include <linux/irqdomain.h>
0016 #include <linux/kernel.h>
0017 #include <linux/mfd/syscon.h>
0018 #include <linux/msi.h>
0019 #include <linux/module.h>
0020 #include <linux/of_address.h>
0021 #include <linux/of_pci.h>
0022 #include <linux/of_platform.h>
0023 #include <linux/pci.h>
0024 #include <linux/phy/phy.h>
0025 #include <linux/platform_device.h>
0026 #include <linux/pm_runtime.h>
0027 #include <linux/regmap.h>
0028 #include <linux/reset.h>
0029 
0030 #include "../pci.h"
0031 
0032 /* PCIe shared registers */
0033 #define PCIE_SYS_CFG        0x00
0034 #define PCIE_INT_ENABLE     0x0c
0035 #define PCIE_CFG_ADDR       0x20
0036 #define PCIE_CFG_DATA       0x24
0037 
0038 /* PCIe per port registers */
0039 #define PCIE_BAR0_SETUP     0x10
0040 #define PCIE_CLASS      0x34
0041 #define PCIE_LINK_STATUS    0x50
0042 
0043 #define PCIE_PORT_INT_EN(x) BIT(20 + (x))
0044 #define PCIE_PORT_PERST(x)  BIT(1 + (x))
0045 #define PCIE_PORT_LINKUP    BIT(0)
0046 #define PCIE_BAR_MAP_MAX    GENMASK(31, 16)
0047 
0048 #define PCIE_BAR_ENABLE     BIT(0)
0049 #define PCIE_REVISION_ID    BIT(0)
0050 #define PCIE_CLASS_CODE     (0x60400 << 8)
0051 #define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \
0052                 ((((regn) >> 8) & GENMASK(3, 0)) << 24))
0053 #define PCIE_CONF_FUN(fun)  (((fun) << 8) & GENMASK(10, 8))
0054 #define PCIE_CONF_DEV(dev)  (((dev) << 11) & GENMASK(15, 11))
0055 #define PCIE_CONF_BUS(bus)  (((bus) << 16) & GENMASK(23, 16))
0056 #define PCIE_CONF_ADDR(regn, fun, dev, bus) \
0057     (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \
0058      PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus))
0059 
0060 /* MediaTek specific configuration registers */
0061 #define PCIE_FTS_NUM        0x70c
0062 #define PCIE_FTS_NUM_MASK   GENMASK(15, 8)
0063 #define PCIE_FTS_NUM_L0(x)  ((x) & 0xff << 8)
0064 
0065 #define PCIE_FC_CREDIT      0x73c
0066 #define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16))
0067 #define PCIE_FC_CREDIT_VAL(x)   ((x) << 16)
0068 
0069 /* PCIe V2 share registers */
0070 #define PCIE_SYS_CFG_V2     0x0
0071 #define PCIE_CSR_LTSSM_EN(x)    BIT(0 + (x) * 8)
0072 #define PCIE_CSR_ASPM_L1_EN(x)  BIT(1 + (x) * 8)
0073 
0074 /* PCIe V2 per-port registers */
0075 #define PCIE_MSI_VECTOR     0x0c0
0076 
0077 #define PCIE_CONF_VEND_ID   0x100
0078 #define PCIE_CONF_DEVICE_ID 0x102
0079 #define PCIE_CONF_CLASS_ID  0x106
0080 
0081 #define PCIE_INT_MASK       0x420
0082 #define INTX_MASK       GENMASK(19, 16)
0083 #define INTX_SHIFT      16
0084 #define PCIE_INT_STATUS     0x424
0085 #define MSI_STATUS      BIT(23)
0086 #define PCIE_IMSI_STATUS    0x42c
0087 #define PCIE_IMSI_ADDR      0x430
0088 #define MSI_MASK        BIT(23)
0089 #define MTK_MSI_IRQS_NUM    32
0090 
0091 #define PCIE_AHB_TRANS_BASE0_L  0x438
0092 #define PCIE_AHB_TRANS_BASE0_H  0x43c
0093 #define AHB2PCIE_SIZE(x)    ((x) & GENMASK(4, 0))
0094 #define PCIE_AXI_WINDOW0    0x448
0095 #define WIN_ENABLE      BIT(7)
0096 /*
0097  * Define PCIe to AHB window size as 2^33 to support max 8GB address space
0098  * translate, support least 4GB DRAM size access from EP DMA(physical DRAM
0099  * start from 0x40000000).
0100  */
0101 #define PCIE2AHB_SIZE   0x21
0102 
0103 /* PCIe V2 configuration transaction header */
0104 #define PCIE_CFG_HEADER0    0x460
0105 #define PCIE_CFG_HEADER1    0x464
0106 #define PCIE_CFG_HEADER2    0x468
0107 #define PCIE_CFG_WDATA      0x470
0108 #define PCIE_APP_TLP_REQ    0x488
0109 #define PCIE_CFG_RDATA      0x48c
0110 #define APP_CFG_REQ     BIT(0)
0111 #define APP_CPL_STATUS      GENMASK(7, 5)
0112 
0113 #define CFG_WRRD_TYPE_0     4
0114 #define CFG_WR_FMT      2
0115 #define CFG_RD_FMT      0
0116 
0117 #define CFG_DW0_LENGTH(length)  ((length) & GENMASK(9, 0))
0118 #define CFG_DW0_TYPE(type)  (((type) << 24) & GENMASK(28, 24))
0119 #define CFG_DW0_FMT(fmt)    (((fmt) << 29) & GENMASK(31, 29))
0120 #define CFG_DW2_REGN(regn)  ((regn) & GENMASK(11, 2))
0121 #define CFG_DW2_FUN(fun)    (((fun) << 16) & GENMASK(18, 16))
0122 #define CFG_DW2_DEV(dev)    (((dev) << 19) & GENMASK(23, 19))
0123 #define CFG_DW2_BUS(bus)    (((bus) << 24) & GENMASK(31, 24))
0124 #define CFG_HEADER_DW0(type, fmt) \
0125     (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt))
0126 #define CFG_HEADER_DW1(where, size) \
0127     (GENMASK(((size) - 1), 0) << ((where) & 0x3))
0128 #define CFG_HEADER_DW2(regn, fun, dev, bus) \
0129     (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \
0130     CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus))
0131 
0132 #define PCIE_RST_CTRL       0x510
0133 #define PCIE_PHY_RSTB       BIT(0)
0134 #define PCIE_PIPE_SRSTB     BIT(1)
0135 #define PCIE_MAC_SRSTB      BIT(2)
0136 #define PCIE_CRSTB      BIT(3)
0137 #define PCIE_PERSTB     BIT(8)
0138 #define PCIE_LINKDOWN_RST_EN    GENMASK(15, 13)
0139 #define PCIE_LINK_STATUS_V2 0x804
0140 #define PCIE_PORT_LINKUP_V2 BIT(10)
0141 
0142 struct mtk_pcie_port;
0143 
0144 /**
0145  * struct mtk_pcie_soc - differentiate between host generations
0146  * @need_fix_class_id: whether this host's class ID needed to be fixed or not
0147  * @need_fix_device_id: whether this host's device ID needed to be fixed or not
0148  * @no_msi: Bridge has no MSI support, and relies on an external block
0149  * @device_id: device ID which this host need to be fixed
0150  * @ops: pointer to configuration access functions
0151  * @startup: pointer to controller setting functions
0152  * @setup_irq: pointer to initialize IRQ functions
0153  */
0154 struct mtk_pcie_soc {
0155     bool need_fix_class_id;
0156     bool need_fix_device_id;
0157     bool no_msi;
0158     unsigned int device_id;
0159     struct pci_ops *ops;
0160     int (*startup)(struct mtk_pcie_port *port);
0161     int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node);
0162 };
0163 
0164 /**
0165  * struct mtk_pcie_port - PCIe port information
0166  * @base: IO mapped register base
0167  * @list: port list
0168  * @pcie: pointer to PCIe host info
0169  * @reset: pointer to port reset control
0170  * @sys_ck: pointer to transaction/data link layer clock
0171  * @ahb_ck: pointer to AHB slave interface operating clock for CSR access
0172  *          and RC initiated MMIO access
0173  * @axi_ck: pointer to application layer MMIO channel operating clock
0174  * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock
0175  *          when pcie_mac_ck/pcie_pipe_ck is turned off
0176  * @obff_ck: pointer to OBFF functional block operating clock
0177  * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock
0178  * @phy: pointer to PHY control block
0179  * @slot: port slot
0180  * @irq: GIC irq
0181  * @irq_domain: legacy INTx IRQ domain
0182  * @inner_domain: inner IRQ domain
0183  * @msi_domain: MSI IRQ domain
0184  * @lock: protect the msi_irq_in_use bitmap
0185  * @msi_irq_in_use: bit map for assigned MSI IRQ
0186  */
0187 struct mtk_pcie_port {
0188     void __iomem *base;
0189     struct list_head list;
0190     struct mtk_pcie *pcie;
0191     struct reset_control *reset;
0192     struct clk *sys_ck;
0193     struct clk *ahb_ck;
0194     struct clk *axi_ck;
0195     struct clk *aux_ck;
0196     struct clk *obff_ck;
0197     struct clk *pipe_ck;
0198     struct phy *phy;
0199     u32 slot;
0200     int irq;
0201     struct irq_domain *irq_domain;
0202     struct irq_domain *inner_domain;
0203     struct irq_domain *msi_domain;
0204     struct mutex lock;
0205     DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM);
0206 };
0207 
0208 /**
0209  * struct mtk_pcie - PCIe host information
0210  * @dev: pointer to PCIe device
0211  * @base: IO mapped register base
0212  * @cfg: IO mapped register map for PCIe config
0213  * @free_ck: free-run reference clock
0214  * @mem: non-prefetchable memory resource
0215  * @ports: pointer to PCIe port information
0216  * @soc: pointer to SoC-dependent operations
0217  */
0218 struct mtk_pcie {
0219     struct device *dev;
0220     void __iomem *base;
0221     struct regmap *cfg;
0222     struct clk *free_ck;
0223 
0224     struct list_head ports;
0225     const struct mtk_pcie_soc *soc;
0226 };
0227 
0228 static void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie)
0229 {
0230     struct device *dev = pcie->dev;
0231 
0232     clk_disable_unprepare(pcie->free_ck);
0233 
0234     pm_runtime_put_sync(dev);
0235     pm_runtime_disable(dev);
0236 }
0237 
0238 static void mtk_pcie_port_free(struct mtk_pcie_port *port)
0239 {
0240     struct mtk_pcie *pcie = port->pcie;
0241     struct device *dev = pcie->dev;
0242 
0243     devm_iounmap(dev, port->base);
0244     list_del(&port->list);
0245     devm_kfree(dev, port);
0246 }
0247 
0248 static void mtk_pcie_put_resources(struct mtk_pcie *pcie)
0249 {
0250     struct mtk_pcie_port *port, *tmp;
0251 
0252     list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
0253         phy_power_off(port->phy);
0254         phy_exit(port->phy);
0255         clk_disable_unprepare(port->pipe_ck);
0256         clk_disable_unprepare(port->obff_ck);
0257         clk_disable_unprepare(port->axi_ck);
0258         clk_disable_unprepare(port->aux_ck);
0259         clk_disable_unprepare(port->ahb_ck);
0260         clk_disable_unprepare(port->sys_ck);
0261         mtk_pcie_port_free(port);
0262     }
0263 
0264     mtk_pcie_subsys_powerdown(pcie);
0265 }
0266 
0267 static int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port)
0268 {
0269     u32 val;
0270     int err;
0271 
0272     err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val,
0273                     !(val & APP_CFG_REQ), 10,
0274                     100 * USEC_PER_MSEC);
0275     if (err)
0276         return PCIBIOS_SET_FAILED;
0277 
0278     if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS)
0279         return PCIBIOS_SET_FAILED;
0280 
0281     return PCIBIOS_SUCCESSFUL;
0282 }
0283 
0284 static int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
0285                   int where, int size, u32 *val)
0286 {
0287     u32 tmp;
0288 
0289     /* Write PCIe configuration transaction header for Cfgrd */
0290     writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT),
0291            port->base + PCIE_CFG_HEADER0);
0292     writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
0293     writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
0294            port->base + PCIE_CFG_HEADER2);
0295 
0296     /* Trigger h/w to transmit Cfgrd TLP */
0297     tmp = readl(port->base + PCIE_APP_TLP_REQ);
0298     tmp |= APP_CFG_REQ;
0299     writel(tmp, port->base + PCIE_APP_TLP_REQ);
0300 
0301     /* Check completion status */
0302     if (mtk_pcie_check_cfg_cpld(port))
0303         return PCIBIOS_SET_FAILED;
0304 
0305     /* Read cpld payload of Cfgrd */
0306     *val = readl(port->base + PCIE_CFG_RDATA);
0307 
0308     if (size == 1)
0309         *val = (*val >> (8 * (where & 3))) & 0xff;
0310     else if (size == 2)
0311         *val = (*val >> (8 * (where & 3))) & 0xffff;
0312 
0313     return PCIBIOS_SUCCESSFUL;
0314 }
0315 
0316 static int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn,
0317                   int where, int size, u32 val)
0318 {
0319     /* Write PCIe configuration transaction header for Cfgwr */
0320     writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT),
0321            port->base + PCIE_CFG_HEADER0);
0322     writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1);
0323     writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus),
0324            port->base + PCIE_CFG_HEADER2);
0325 
0326     /* Write Cfgwr data */
0327     val = val << 8 * (where & 3);
0328     writel(val, port->base + PCIE_CFG_WDATA);
0329 
0330     /* Trigger h/w to transmit Cfgwr TLP */
0331     val = readl(port->base + PCIE_APP_TLP_REQ);
0332     val |= APP_CFG_REQ;
0333     writel(val, port->base + PCIE_APP_TLP_REQ);
0334 
0335     /* Check completion status */
0336     return mtk_pcie_check_cfg_cpld(port);
0337 }
0338 
0339 static struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus,
0340                         unsigned int devfn)
0341 {
0342     struct mtk_pcie *pcie = bus->sysdata;
0343     struct mtk_pcie_port *port;
0344     struct pci_dev *dev = NULL;
0345 
0346     /*
0347      * Walk the bus hierarchy to get the devfn value
0348      * of the port in the root bus.
0349      */
0350     while (bus && bus->number) {
0351         dev = bus->self;
0352         bus = dev->bus;
0353         devfn = dev->devfn;
0354     }
0355 
0356     list_for_each_entry(port, &pcie->ports, list)
0357         if (port->slot == PCI_SLOT(devfn))
0358             return port;
0359 
0360     return NULL;
0361 }
0362 
0363 static int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn,
0364                 int where, int size, u32 *val)
0365 {
0366     struct mtk_pcie_port *port;
0367     u32 bn = bus->number;
0368 
0369     port = mtk_pcie_find_port(bus, devfn);
0370     if (!port)
0371         return PCIBIOS_DEVICE_NOT_FOUND;
0372 
0373     return mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val);
0374 }
0375 
0376 static int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn,
0377                  int where, int size, u32 val)
0378 {
0379     struct mtk_pcie_port *port;
0380     u32 bn = bus->number;
0381 
0382     port = mtk_pcie_find_port(bus, devfn);
0383     if (!port)
0384         return PCIBIOS_DEVICE_NOT_FOUND;
0385 
0386     return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val);
0387 }
0388 
0389 static struct pci_ops mtk_pcie_ops_v2 = {
0390     .read  = mtk_pcie_config_read,
0391     .write = mtk_pcie_config_write,
0392 };
0393 
0394 static void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
0395 {
0396     struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
0397     phys_addr_t addr;
0398 
0399     /* MT2712/MT7622 only support 32-bit MSI addresses */
0400     addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
0401     msg->address_hi = 0;
0402     msg->address_lo = lower_32_bits(addr);
0403 
0404     msg->data = data->hwirq;
0405 
0406     dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n",
0407         (int)data->hwirq, msg->address_hi, msg->address_lo);
0408 }
0409 
0410 static int mtk_msi_set_affinity(struct irq_data *irq_data,
0411                 const struct cpumask *mask, bool force)
0412 {
0413      return -EINVAL;
0414 }
0415 
0416 static void mtk_msi_ack_irq(struct irq_data *data)
0417 {
0418     struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data);
0419     u32 hwirq = data->hwirq;
0420 
0421     writel(1 << hwirq, port->base + PCIE_IMSI_STATUS);
0422 }
0423 
0424 static struct irq_chip mtk_msi_bottom_irq_chip = {
0425     .name           = "MTK MSI",
0426     .irq_compose_msi_msg    = mtk_compose_msi_msg,
0427     .irq_set_affinity   = mtk_msi_set_affinity,
0428     .irq_ack        = mtk_msi_ack_irq,
0429 };
0430 
0431 static int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
0432                      unsigned int nr_irqs, void *args)
0433 {
0434     struct mtk_pcie_port *port = domain->host_data;
0435     unsigned long bit;
0436 
0437     WARN_ON(nr_irqs != 1);
0438     mutex_lock(&port->lock);
0439 
0440     bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM);
0441     if (bit >= MTK_MSI_IRQS_NUM) {
0442         mutex_unlock(&port->lock);
0443         return -ENOSPC;
0444     }
0445 
0446     __set_bit(bit, port->msi_irq_in_use);
0447 
0448     mutex_unlock(&port->lock);
0449 
0450     irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip,
0451                 domain->host_data, handle_edge_irq,
0452                 NULL, NULL);
0453 
0454     return 0;
0455 }
0456 
0457 static void mtk_pcie_irq_domain_free(struct irq_domain *domain,
0458                      unsigned int virq, unsigned int nr_irqs)
0459 {
0460     struct irq_data *d = irq_domain_get_irq_data(domain, virq);
0461     struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d);
0462 
0463     mutex_lock(&port->lock);
0464 
0465     if (!test_bit(d->hwirq, port->msi_irq_in_use))
0466         dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n",
0467             d->hwirq);
0468     else
0469         __clear_bit(d->hwirq, port->msi_irq_in_use);
0470 
0471     mutex_unlock(&port->lock);
0472 
0473     irq_domain_free_irqs_parent(domain, virq, nr_irqs);
0474 }
0475 
0476 static const struct irq_domain_ops msi_domain_ops = {
0477     .alloc  = mtk_pcie_irq_domain_alloc,
0478     .free   = mtk_pcie_irq_domain_free,
0479 };
0480 
0481 static struct irq_chip mtk_msi_irq_chip = {
0482     .name       = "MTK PCIe MSI",
0483     .irq_ack    = irq_chip_ack_parent,
0484     .irq_mask   = pci_msi_mask_irq,
0485     .irq_unmask = pci_msi_unmask_irq,
0486 };
0487 
0488 static struct msi_domain_info mtk_msi_domain_info = {
0489     .flags  = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
0490            MSI_FLAG_PCI_MSIX),
0491     .chip   = &mtk_msi_irq_chip,
0492 };
0493 
0494 static int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port)
0495 {
0496     struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node);
0497 
0498     mutex_init(&port->lock);
0499 
0500     port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM,
0501                               &msi_domain_ops, port);
0502     if (!port->inner_domain) {
0503         dev_err(port->pcie->dev, "failed to create IRQ domain\n");
0504         return -ENOMEM;
0505     }
0506 
0507     port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info,
0508                              port->inner_domain);
0509     if (!port->msi_domain) {
0510         dev_err(port->pcie->dev, "failed to create MSI domain\n");
0511         irq_domain_remove(port->inner_domain);
0512         return -ENOMEM;
0513     }
0514 
0515     return 0;
0516 }
0517 
0518 static void mtk_pcie_enable_msi(struct mtk_pcie_port *port)
0519 {
0520     u32 val;
0521     phys_addr_t msg_addr;
0522 
0523     msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR);
0524     val = lower_32_bits(msg_addr);
0525     writel(val, port->base + PCIE_IMSI_ADDR);
0526 
0527     val = readl(port->base + PCIE_INT_MASK);
0528     val &= ~MSI_MASK;
0529     writel(val, port->base + PCIE_INT_MASK);
0530 }
0531 
0532 static void mtk_pcie_irq_teardown(struct mtk_pcie *pcie)
0533 {
0534     struct mtk_pcie_port *port, *tmp;
0535 
0536     list_for_each_entry_safe(port, tmp, &pcie->ports, list) {
0537         irq_set_chained_handler_and_data(port->irq, NULL, NULL);
0538 
0539         if (port->irq_domain)
0540             irq_domain_remove(port->irq_domain);
0541 
0542         if (IS_ENABLED(CONFIG_PCI_MSI)) {
0543             if (port->msi_domain)
0544                 irq_domain_remove(port->msi_domain);
0545             if (port->inner_domain)
0546                 irq_domain_remove(port->inner_domain);
0547         }
0548 
0549         irq_dispose_mapping(port->irq);
0550     }
0551 }
0552 
0553 static int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
0554                  irq_hw_number_t hwirq)
0555 {
0556     irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
0557     irq_set_chip_data(irq, domain->host_data);
0558 
0559     return 0;
0560 }
0561 
0562 static const struct irq_domain_ops intx_domain_ops = {
0563     .map = mtk_pcie_intx_map,
0564 };
0565 
0566 static int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port,
0567                     struct device_node *node)
0568 {
0569     struct device *dev = port->pcie->dev;
0570     struct device_node *pcie_intc_node;
0571     int ret;
0572 
0573     /* Setup INTx */
0574     pcie_intc_node = of_get_next_child(node, NULL);
0575     if (!pcie_intc_node) {
0576         dev_err(dev, "no PCIe Intc node found\n");
0577         return -ENODEV;
0578     }
0579 
0580     port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
0581                          &intx_domain_ops, port);
0582     of_node_put(pcie_intc_node);
0583     if (!port->irq_domain) {
0584         dev_err(dev, "failed to get INTx IRQ domain\n");
0585         return -ENODEV;
0586     }
0587 
0588     if (IS_ENABLED(CONFIG_PCI_MSI)) {
0589         ret = mtk_pcie_allocate_msi_domains(port);
0590         if (ret)
0591             return ret;
0592     }
0593 
0594     return 0;
0595 }
0596 
0597 static void mtk_pcie_intr_handler(struct irq_desc *desc)
0598 {
0599     struct mtk_pcie_port *port = irq_desc_get_handler_data(desc);
0600     struct irq_chip *irqchip = irq_desc_get_chip(desc);
0601     unsigned long status;
0602     u32 bit = INTX_SHIFT;
0603 
0604     chained_irq_enter(irqchip, desc);
0605 
0606     status = readl(port->base + PCIE_INT_STATUS);
0607     if (status & INTX_MASK) {
0608         for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) {
0609             /* Clear the INTx */
0610             writel(1 << bit, port->base + PCIE_INT_STATUS);
0611             generic_handle_domain_irq(port->irq_domain,
0612                           bit - INTX_SHIFT);
0613         }
0614     }
0615 
0616     if (IS_ENABLED(CONFIG_PCI_MSI)) {
0617         if (status & MSI_STATUS){
0618             unsigned long imsi_status;
0619 
0620             while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) {
0621                 for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM)
0622                     generic_handle_domain_irq(port->inner_domain, bit);
0623             }
0624             /* Clear MSI interrupt status */
0625             writel(MSI_STATUS, port->base + PCIE_INT_STATUS);
0626         }
0627     }
0628 
0629     chained_irq_exit(irqchip, desc);
0630 }
0631 
0632 static int mtk_pcie_setup_irq(struct mtk_pcie_port *port,
0633                   struct device_node *node)
0634 {
0635     struct mtk_pcie *pcie = port->pcie;
0636     struct device *dev = pcie->dev;
0637     struct platform_device *pdev = to_platform_device(dev);
0638     int err;
0639 
0640     err = mtk_pcie_init_irq_domain(port, node);
0641     if (err) {
0642         dev_err(dev, "failed to init PCIe IRQ domain\n");
0643         return err;
0644     }
0645 
0646     if (of_find_property(dev->of_node, "interrupt-names", NULL))
0647         port->irq = platform_get_irq_byname(pdev, "pcie_irq");
0648     else
0649         port->irq = platform_get_irq(pdev, port->slot);
0650 
0651     if (port->irq < 0)
0652         return port->irq;
0653 
0654     irq_set_chained_handler_and_data(port->irq,
0655                      mtk_pcie_intr_handler, port);
0656 
0657     return 0;
0658 }
0659 
0660 static int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port)
0661 {
0662     struct mtk_pcie *pcie = port->pcie;
0663     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
0664     struct resource *mem = NULL;
0665     struct resource_entry *entry;
0666     const struct mtk_pcie_soc *soc = port->pcie->soc;
0667     u32 val;
0668     int err;
0669 
0670     entry = resource_list_first_type(&host->windows, IORESOURCE_MEM);
0671     if (entry)
0672         mem = entry->res;
0673     if (!mem)
0674         return -EINVAL;
0675 
0676     /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */
0677     if (pcie->base) {
0678         val = readl(pcie->base + PCIE_SYS_CFG_V2);
0679         val |= PCIE_CSR_LTSSM_EN(port->slot) |
0680                PCIE_CSR_ASPM_L1_EN(port->slot);
0681         writel(val, pcie->base + PCIE_SYS_CFG_V2);
0682     } else if (pcie->cfg) {
0683         val = PCIE_CSR_LTSSM_EN(port->slot) |
0684               PCIE_CSR_ASPM_L1_EN(port->slot);
0685         regmap_update_bits(pcie->cfg, PCIE_SYS_CFG_V2, val, val);
0686     }
0687 
0688     /* Assert all reset signals */
0689     writel(0, port->base + PCIE_RST_CTRL);
0690 
0691     /*
0692      * Enable PCIe link down reset, if link status changed from link up to
0693      * link down, this will reset MAC control registers and configuration
0694      * space.
0695      */
0696     writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL);
0697 
0698     /*
0699      * Described in PCIe CEM specification sections 2.2 (PERST# Signal) and
0700      * 2.2.1 (Initial Power-Up (G3 to S0)). The deassertion of PERST# should
0701      * be delayed 100ms (TPVPERL) for the power and clock to become stable.
0702      */
0703     msleep(100);
0704 
0705     /* De-assert PHY, PE, PIPE, MAC and configuration reset */
0706     val = readl(port->base + PCIE_RST_CTRL);
0707     val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB |
0708            PCIE_MAC_SRSTB | PCIE_CRSTB;
0709     writel(val, port->base + PCIE_RST_CTRL);
0710 
0711     /* Set up vendor ID and class code */
0712     if (soc->need_fix_class_id) {
0713         val = PCI_VENDOR_ID_MEDIATEK;
0714         writew(val, port->base + PCIE_CONF_VEND_ID);
0715 
0716         val = PCI_CLASS_BRIDGE_PCI;
0717         writew(val, port->base + PCIE_CONF_CLASS_ID);
0718     }
0719 
0720     if (soc->need_fix_device_id)
0721         writew(soc->device_id, port->base + PCIE_CONF_DEVICE_ID);
0722 
0723     /* 100ms timeout value should be enough for Gen1/2 training */
0724     err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val,
0725                  !!(val & PCIE_PORT_LINKUP_V2), 20,
0726                  100 * USEC_PER_MSEC);
0727     if (err)
0728         return -ETIMEDOUT;
0729 
0730     /* Set INTx mask */
0731     val = readl(port->base + PCIE_INT_MASK);
0732     val &= ~INTX_MASK;
0733     writel(val, port->base + PCIE_INT_MASK);
0734 
0735     if (IS_ENABLED(CONFIG_PCI_MSI))
0736         mtk_pcie_enable_msi(port);
0737 
0738     /* Set AHB to PCIe translation windows */
0739     val = lower_32_bits(mem->start) |
0740           AHB2PCIE_SIZE(fls(resource_size(mem)));
0741     writel(val, port->base + PCIE_AHB_TRANS_BASE0_L);
0742 
0743     val = upper_32_bits(mem->start);
0744     writel(val, port->base + PCIE_AHB_TRANS_BASE0_H);
0745 
0746     /* Set PCIe to AXI translation memory space.*/
0747     val = PCIE2AHB_SIZE | WIN_ENABLE;
0748     writel(val, port->base + PCIE_AXI_WINDOW0);
0749 
0750     return 0;
0751 }
0752 
0753 static void __iomem *mtk_pcie_map_bus(struct pci_bus *bus,
0754                       unsigned int devfn, int where)
0755 {
0756     struct mtk_pcie *pcie = bus->sysdata;
0757 
0758     writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn),
0759                   bus->number), pcie->base + PCIE_CFG_ADDR);
0760 
0761     return pcie->base + PCIE_CFG_DATA + (where & 3);
0762 }
0763 
0764 static struct pci_ops mtk_pcie_ops = {
0765     .map_bus = mtk_pcie_map_bus,
0766     .read  = pci_generic_config_read,
0767     .write = pci_generic_config_write,
0768 };
0769 
0770 static int mtk_pcie_startup_port(struct mtk_pcie_port *port)
0771 {
0772     struct mtk_pcie *pcie = port->pcie;
0773     u32 func = PCI_FUNC(port->slot);
0774     u32 slot = PCI_SLOT(port->slot << 3);
0775     u32 val;
0776     int err;
0777 
0778     /* assert port PERST_N */
0779     val = readl(pcie->base + PCIE_SYS_CFG);
0780     val |= PCIE_PORT_PERST(port->slot);
0781     writel(val, pcie->base + PCIE_SYS_CFG);
0782 
0783     /* de-assert port PERST_N */
0784     val = readl(pcie->base + PCIE_SYS_CFG);
0785     val &= ~PCIE_PORT_PERST(port->slot);
0786     writel(val, pcie->base + PCIE_SYS_CFG);
0787 
0788     /* 100ms timeout value should be enough for Gen1/2 training */
0789     err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val,
0790                  !!(val & PCIE_PORT_LINKUP), 20,
0791                  100 * USEC_PER_MSEC);
0792     if (err)
0793         return -ETIMEDOUT;
0794 
0795     /* enable interrupt */
0796     val = readl(pcie->base + PCIE_INT_ENABLE);
0797     val |= PCIE_PORT_INT_EN(port->slot);
0798     writel(val, pcie->base + PCIE_INT_ENABLE);
0799 
0800     /* map to all DDR region. We need to set it before cfg operation. */
0801     writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE,
0802            port->base + PCIE_BAR0_SETUP);
0803 
0804     /* configure class code and revision ID */
0805     writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS);
0806 
0807     /* configure FC credit */
0808     writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
0809            pcie->base + PCIE_CFG_ADDR);
0810     val = readl(pcie->base + PCIE_CFG_DATA);
0811     val &= ~PCIE_FC_CREDIT_MASK;
0812     val |= PCIE_FC_CREDIT_VAL(0x806c);
0813     writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0),
0814            pcie->base + PCIE_CFG_ADDR);
0815     writel(val, pcie->base + PCIE_CFG_DATA);
0816 
0817     /* configure RC FTS number to 250 when it leaves L0s */
0818     writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
0819            pcie->base + PCIE_CFG_ADDR);
0820     val = readl(pcie->base + PCIE_CFG_DATA);
0821     val &= ~PCIE_FTS_NUM_MASK;
0822     val |= PCIE_FTS_NUM_L0(0x50);
0823     writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0),
0824            pcie->base + PCIE_CFG_ADDR);
0825     writel(val, pcie->base + PCIE_CFG_DATA);
0826 
0827     return 0;
0828 }
0829 
0830 static void mtk_pcie_enable_port(struct mtk_pcie_port *port)
0831 {
0832     struct mtk_pcie *pcie = port->pcie;
0833     struct device *dev = pcie->dev;
0834     int err;
0835 
0836     err = clk_prepare_enable(port->sys_ck);
0837     if (err) {
0838         dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot);
0839         goto err_sys_clk;
0840     }
0841 
0842     err = clk_prepare_enable(port->ahb_ck);
0843     if (err) {
0844         dev_err(dev, "failed to enable ahb_ck%d\n", port->slot);
0845         goto err_ahb_clk;
0846     }
0847 
0848     err = clk_prepare_enable(port->aux_ck);
0849     if (err) {
0850         dev_err(dev, "failed to enable aux_ck%d\n", port->slot);
0851         goto err_aux_clk;
0852     }
0853 
0854     err = clk_prepare_enable(port->axi_ck);
0855     if (err) {
0856         dev_err(dev, "failed to enable axi_ck%d\n", port->slot);
0857         goto err_axi_clk;
0858     }
0859 
0860     err = clk_prepare_enable(port->obff_ck);
0861     if (err) {
0862         dev_err(dev, "failed to enable obff_ck%d\n", port->slot);
0863         goto err_obff_clk;
0864     }
0865 
0866     err = clk_prepare_enable(port->pipe_ck);
0867     if (err) {
0868         dev_err(dev, "failed to enable pipe_ck%d\n", port->slot);
0869         goto err_pipe_clk;
0870     }
0871 
0872     reset_control_assert(port->reset);
0873     reset_control_deassert(port->reset);
0874 
0875     err = phy_init(port->phy);
0876     if (err) {
0877         dev_err(dev, "failed to initialize port%d phy\n", port->slot);
0878         goto err_phy_init;
0879     }
0880 
0881     err = phy_power_on(port->phy);
0882     if (err) {
0883         dev_err(dev, "failed to power on port%d phy\n", port->slot);
0884         goto err_phy_on;
0885     }
0886 
0887     if (!pcie->soc->startup(port))
0888         return;
0889 
0890     dev_info(dev, "Port%d link down\n", port->slot);
0891 
0892     phy_power_off(port->phy);
0893 err_phy_on:
0894     phy_exit(port->phy);
0895 err_phy_init:
0896     clk_disable_unprepare(port->pipe_ck);
0897 err_pipe_clk:
0898     clk_disable_unprepare(port->obff_ck);
0899 err_obff_clk:
0900     clk_disable_unprepare(port->axi_ck);
0901 err_axi_clk:
0902     clk_disable_unprepare(port->aux_ck);
0903 err_aux_clk:
0904     clk_disable_unprepare(port->ahb_ck);
0905 err_ahb_clk:
0906     clk_disable_unprepare(port->sys_ck);
0907 err_sys_clk:
0908     mtk_pcie_port_free(port);
0909 }
0910 
0911 static int mtk_pcie_parse_port(struct mtk_pcie *pcie,
0912                    struct device_node *node,
0913                    int slot)
0914 {
0915     struct mtk_pcie_port *port;
0916     struct device *dev = pcie->dev;
0917     struct platform_device *pdev = to_platform_device(dev);
0918     char name[10];
0919     int err;
0920 
0921     port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
0922     if (!port)
0923         return -ENOMEM;
0924 
0925     snprintf(name, sizeof(name), "port%d", slot);
0926     port->base = devm_platform_ioremap_resource_byname(pdev, name);
0927     if (IS_ERR(port->base)) {
0928         dev_err(dev, "failed to map port%d base\n", slot);
0929         return PTR_ERR(port->base);
0930     }
0931 
0932     snprintf(name, sizeof(name), "sys_ck%d", slot);
0933     port->sys_ck = devm_clk_get(dev, name);
0934     if (IS_ERR(port->sys_ck)) {
0935         dev_err(dev, "failed to get sys_ck%d clock\n", slot);
0936         return PTR_ERR(port->sys_ck);
0937     }
0938 
0939     /* sys_ck might be divided into the following parts in some chips */
0940     snprintf(name, sizeof(name), "ahb_ck%d", slot);
0941     port->ahb_ck = devm_clk_get_optional(dev, name);
0942     if (IS_ERR(port->ahb_ck))
0943         return PTR_ERR(port->ahb_ck);
0944 
0945     snprintf(name, sizeof(name), "axi_ck%d", slot);
0946     port->axi_ck = devm_clk_get_optional(dev, name);
0947     if (IS_ERR(port->axi_ck))
0948         return PTR_ERR(port->axi_ck);
0949 
0950     snprintf(name, sizeof(name), "aux_ck%d", slot);
0951     port->aux_ck = devm_clk_get_optional(dev, name);
0952     if (IS_ERR(port->aux_ck))
0953         return PTR_ERR(port->aux_ck);
0954 
0955     snprintf(name, sizeof(name), "obff_ck%d", slot);
0956     port->obff_ck = devm_clk_get_optional(dev, name);
0957     if (IS_ERR(port->obff_ck))
0958         return PTR_ERR(port->obff_ck);
0959 
0960     snprintf(name, sizeof(name), "pipe_ck%d", slot);
0961     port->pipe_ck = devm_clk_get_optional(dev, name);
0962     if (IS_ERR(port->pipe_ck))
0963         return PTR_ERR(port->pipe_ck);
0964 
0965     snprintf(name, sizeof(name), "pcie-rst%d", slot);
0966     port->reset = devm_reset_control_get_optional_exclusive(dev, name);
0967     if (PTR_ERR(port->reset) == -EPROBE_DEFER)
0968         return PTR_ERR(port->reset);
0969 
0970     /* some platforms may use default PHY setting */
0971     snprintf(name, sizeof(name), "pcie-phy%d", slot);
0972     port->phy = devm_phy_optional_get(dev, name);
0973     if (IS_ERR(port->phy))
0974         return PTR_ERR(port->phy);
0975 
0976     port->slot = slot;
0977     port->pcie = pcie;
0978 
0979     if (pcie->soc->setup_irq) {
0980         err = pcie->soc->setup_irq(port, node);
0981         if (err)
0982             return err;
0983     }
0984 
0985     INIT_LIST_HEAD(&port->list);
0986     list_add_tail(&port->list, &pcie->ports);
0987 
0988     return 0;
0989 }
0990 
0991 static int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie)
0992 {
0993     struct device *dev = pcie->dev;
0994     struct platform_device *pdev = to_platform_device(dev);
0995     struct resource *regs;
0996     struct device_node *cfg_node;
0997     int err;
0998 
0999     /* get shared registers, which are optional */
1000     regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys");
1001     if (regs) {
1002         pcie->base = devm_ioremap_resource(dev, regs);
1003         if (IS_ERR(pcie->base))
1004             return PTR_ERR(pcie->base);
1005     }
1006 
1007     cfg_node = of_find_compatible_node(NULL, NULL,
1008                        "mediatek,generic-pciecfg");
1009     if (cfg_node) {
1010         pcie->cfg = syscon_node_to_regmap(cfg_node);
1011         of_node_put(cfg_node);
1012         if (IS_ERR(pcie->cfg))
1013             return PTR_ERR(pcie->cfg);
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     int err, slot;
1049 
1050     slot = of_get_pci_domain_nr(dev->of_node);
1051     if (slot < 0) {
1052         for_each_available_child_of_node(node, child) {
1053             err = of_pci_get_devfn(child);
1054             if (err < 0) {
1055                 dev_err(dev, "failed to get devfn: %d\n", err);
1056                 goto error_put_node;
1057             }
1058 
1059             slot = PCI_SLOT(err);
1060 
1061             err = mtk_pcie_parse_port(pcie, child, slot);
1062             if (err)
1063                 goto error_put_node;
1064         }
1065     } else {
1066         err = mtk_pcie_parse_port(pcie, node, slot);
1067         if (err)
1068             return err;
1069     }
1070 
1071     err = mtk_pcie_subsys_powerup(pcie);
1072     if (err)
1073         return err;
1074 
1075     /* enable each port, and then check link status */
1076     list_for_each_entry_safe(port, tmp, &pcie->ports, list)
1077         mtk_pcie_enable_port(port);
1078 
1079     /* power down PCIe subsys if slots are all empty (link down) */
1080     if (list_empty(&pcie->ports))
1081         mtk_pcie_subsys_powerdown(pcie);
1082 
1083     return 0;
1084 error_put_node:
1085     of_node_put(child);
1086     return err;
1087 }
1088 
1089 static int mtk_pcie_probe(struct platform_device *pdev)
1090 {
1091     struct device *dev = &pdev->dev;
1092     struct mtk_pcie *pcie;
1093     struct pci_host_bridge *host;
1094     int err;
1095 
1096     host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
1097     if (!host)
1098         return -ENOMEM;
1099 
1100     pcie = pci_host_bridge_priv(host);
1101 
1102     pcie->dev = dev;
1103     pcie->soc = of_device_get_match_data(dev);
1104     platform_set_drvdata(pdev, pcie);
1105     INIT_LIST_HEAD(&pcie->ports);
1106 
1107     err = mtk_pcie_setup(pcie);
1108     if (err)
1109         return err;
1110 
1111     host->ops = pcie->soc->ops;
1112     host->sysdata = pcie;
1113     host->msi_domain = pcie->soc->no_msi;
1114 
1115     err = pci_host_probe(host);
1116     if (err)
1117         goto put_resources;
1118 
1119     return 0;
1120 
1121 put_resources:
1122     if (!list_empty(&pcie->ports))
1123         mtk_pcie_put_resources(pcie);
1124 
1125     return err;
1126 }
1127 
1128 
1129 static void mtk_pcie_free_resources(struct mtk_pcie *pcie)
1130 {
1131     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1132     struct list_head *windows = &host->windows;
1133 
1134     pci_free_resource_list(windows);
1135 }
1136 
1137 static int mtk_pcie_remove(struct platform_device *pdev)
1138 {
1139     struct mtk_pcie *pcie = platform_get_drvdata(pdev);
1140     struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie);
1141 
1142     pci_stop_root_bus(host->bus);
1143     pci_remove_root_bus(host->bus);
1144     mtk_pcie_free_resources(pcie);
1145 
1146     mtk_pcie_irq_teardown(pcie);
1147 
1148     mtk_pcie_put_resources(pcie);
1149 
1150     return 0;
1151 }
1152 
1153 static int mtk_pcie_suspend_noirq(struct device *dev)
1154 {
1155     struct mtk_pcie *pcie = dev_get_drvdata(dev);
1156     struct mtk_pcie_port *port;
1157 
1158     if (list_empty(&pcie->ports))
1159         return 0;
1160 
1161     list_for_each_entry(port, &pcie->ports, list) {
1162         clk_disable_unprepare(port->pipe_ck);
1163         clk_disable_unprepare(port->obff_ck);
1164         clk_disable_unprepare(port->axi_ck);
1165         clk_disable_unprepare(port->aux_ck);
1166         clk_disable_unprepare(port->ahb_ck);
1167         clk_disable_unprepare(port->sys_ck);
1168         phy_power_off(port->phy);
1169         phy_exit(port->phy);
1170     }
1171 
1172     clk_disable_unprepare(pcie->free_ck);
1173 
1174     return 0;
1175 }
1176 
1177 static int mtk_pcie_resume_noirq(struct device *dev)
1178 {
1179     struct mtk_pcie *pcie = dev_get_drvdata(dev);
1180     struct mtk_pcie_port *port, *tmp;
1181 
1182     if (list_empty(&pcie->ports))
1183         return 0;
1184 
1185     clk_prepare_enable(pcie->free_ck);
1186 
1187     list_for_each_entry_safe(port, tmp, &pcie->ports, list)
1188         mtk_pcie_enable_port(port);
1189 
1190     /* In case of EP was removed while system suspend. */
1191     if (list_empty(&pcie->ports))
1192         clk_disable_unprepare(pcie->free_ck);
1193 
1194     return 0;
1195 }
1196 
1197 static const struct dev_pm_ops mtk_pcie_pm_ops = {
1198     NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq,
1199                   mtk_pcie_resume_noirq)
1200 };
1201 
1202 static const struct mtk_pcie_soc mtk_pcie_soc_v1 = {
1203     .no_msi = true,
1204     .ops = &mtk_pcie_ops,
1205     .startup = mtk_pcie_startup_port,
1206 };
1207 
1208 static const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = {
1209     .ops = &mtk_pcie_ops_v2,
1210     .startup = mtk_pcie_startup_port_v2,
1211     .setup_irq = mtk_pcie_setup_irq,
1212 };
1213 
1214 static const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = {
1215     .need_fix_class_id = true,
1216     .ops = &mtk_pcie_ops_v2,
1217     .startup = mtk_pcie_startup_port_v2,
1218     .setup_irq = mtk_pcie_setup_irq,
1219 };
1220 
1221 static const struct mtk_pcie_soc mtk_pcie_soc_mt7629 = {
1222     .need_fix_class_id = true,
1223     .need_fix_device_id = true,
1224     .device_id = PCI_DEVICE_ID_MEDIATEK_7629,
1225     .ops = &mtk_pcie_ops_v2,
1226     .startup = mtk_pcie_startup_port_v2,
1227     .setup_irq = mtk_pcie_setup_irq,
1228 };
1229 
1230 static const struct of_device_id mtk_pcie_ids[] = {
1231     { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 },
1232     { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 },
1233     { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_mt2712 },
1234     { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_mt7622 },
1235     { .compatible = "mediatek,mt7629-pcie", .data = &mtk_pcie_soc_mt7629 },
1236     {},
1237 };
1238 MODULE_DEVICE_TABLE(of, mtk_pcie_ids);
1239 
1240 static struct platform_driver mtk_pcie_driver = {
1241     .probe = mtk_pcie_probe,
1242     .remove = mtk_pcie_remove,
1243     .driver = {
1244         .name = "mtk-pcie",
1245         .of_match_table = mtk_pcie_ids,
1246         .suppress_bind_attrs = true,
1247         .pm = &mtk_pcie_pm_ops,
1248     },
1249 };
1250 module_platform_driver(mtk_pcie_driver);
1251 MODULE_LICENSE("GPL v2");