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