18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * MediaTek PCIe host controller driver. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2017 MediaTek Inc. 68c2ecf20Sopenharmony_ci * Author: Ryder Lee <ryder.lee@mediatek.com> 78c2ecf20Sopenharmony_ci * Honghui Zhang <honghui.zhang@mediatek.com> 88c2ecf20Sopenharmony_ci */ 98c2ecf20Sopenharmony_ci 108c2ecf20Sopenharmony_ci#include <linux/clk.h> 118c2ecf20Sopenharmony_ci#include <linux/delay.h> 128c2ecf20Sopenharmony_ci#include <linux/iopoll.h> 138c2ecf20Sopenharmony_ci#include <linux/irq.h> 148c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h> 158c2ecf20Sopenharmony_ci#include <linux/irqdomain.h> 168c2ecf20Sopenharmony_ci#include <linux/kernel.h> 178c2ecf20Sopenharmony_ci#include <linux/msi.h> 188c2ecf20Sopenharmony_ci#include <linux/module.h> 198c2ecf20Sopenharmony_ci#include <linux/of_address.h> 208c2ecf20Sopenharmony_ci#include <linux/of_pci.h> 218c2ecf20Sopenharmony_ci#include <linux/of_platform.h> 228c2ecf20Sopenharmony_ci#include <linux/pci.h> 238c2ecf20Sopenharmony_ci#include <linux/phy/phy.h> 248c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 258c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 268c2ecf20Sopenharmony_ci#include <linux/reset.h> 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci#include "../pci.h" 298c2ecf20Sopenharmony_ci 308c2ecf20Sopenharmony_ci/* PCIe shared registers */ 318c2ecf20Sopenharmony_ci#define PCIE_SYS_CFG 0x00 328c2ecf20Sopenharmony_ci#define PCIE_INT_ENABLE 0x0c 338c2ecf20Sopenharmony_ci#define PCIE_CFG_ADDR 0x20 348c2ecf20Sopenharmony_ci#define PCIE_CFG_DATA 0x24 358c2ecf20Sopenharmony_ci 368c2ecf20Sopenharmony_ci/* PCIe per port registers */ 378c2ecf20Sopenharmony_ci#define PCIE_BAR0_SETUP 0x10 388c2ecf20Sopenharmony_ci#define PCIE_CLASS 0x34 398c2ecf20Sopenharmony_ci#define PCIE_LINK_STATUS 0x50 408c2ecf20Sopenharmony_ci 418c2ecf20Sopenharmony_ci#define PCIE_PORT_INT_EN(x) BIT(20 + (x)) 428c2ecf20Sopenharmony_ci#define PCIE_PORT_PERST(x) BIT(1 + (x)) 438c2ecf20Sopenharmony_ci#define PCIE_PORT_LINKUP BIT(0) 448c2ecf20Sopenharmony_ci#define PCIE_BAR_MAP_MAX GENMASK(31, 16) 458c2ecf20Sopenharmony_ci 468c2ecf20Sopenharmony_ci#define PCIE_BAR_ENABLE BIT(0) 478c2ecf20Sopenharmony_ci#define PCIE_REVISION_ID BIT(0) 488c2ecf20Sopenharmony_ci#define PCIE_CLASS_CODE (0x60400 << 8) 498c2ecf20Sopenharmony_ci#define PCIE_CONF_REG(regn) (((regn) & GENMASK(7, 2)) | \ 508c2ecf20Sopenharmony_ci ((((regn) >> 8) & GENMASK(3, 0)) << 24)) 518c2ecf20Sopenharmony_ci#define PCIE_CONF_FUN(fun) (((fun) << 8) & GENMASK(10, 8)) 528c2ecf20Sopenharmony_ci#define PCIE_CONF_DEV(dev) (((dev) << 11) & GENMASK(15, 11)) 538c2ecf20Sopenharmony_ci#define PCIE_CONF_BUS(bus) (((bus) << 16) & GENMASK(23, 16)) 548c2ecf20Sopenharmony_ci#define PCIE_CONF_ADDR(regn, fun, dev, bus) \ 558c2ecf20Sopenharmony_ci (PCIE_CONF_REG(regn) | PCIE_CONF_FUN(fun) | \ 568c2ecf20Sopenharmony_ci PCIE_CONF_DEV(dev) | PCIE_CONF_BUS(bus)) 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci/* MediaTek specific configuration registers */ 598c2ecf20Sopenharmony_ci#define PCIE_FTS_NUM 0x70c 608c2ecf20Sopenharmony_ci#define PCIE_FTS_NUM_MASK GENMASK(15, 8) 618c2ecf20Sopenharmony_ci#define PCIE_FTS_NUM_L0(x) ((x) & 0xff << 8) 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci#define PCIE_FC_CREDIT 0x73c 648c2ecf20Sopenharmony_ci#define PCIE_FC_CREDIT_MASK (GENMASK(31, 31) | GENMASK(28, 16)) 658c2ecf20Sopenharmony_ci#define PCIE_FC_CREDIT_VAL(x) ((x) << 16) 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci/* PCIe V2 share registers */ 688c2ecf20Sopenharmony_ci#define PCIE_SYS_CFG_V2 0x0 698c2ecf20Sopenharmony_ci#define PCIE_CSR_LTSSM_EN(x) BIT(0 + (x) * 8) 708c2ecf20Sopenharmony_ci#define PCIE_CSR_ASPM_L1_EN(x) BIT(1 + (x) * 8) 718c2ecf20Sopenharmony_ci 728c2ecf20Sopenharmony_ci/* PCIe V2 per-port registers */ 738c2ecf20Sopenharmony_ci#define PCIE_MSI_VECTOR 0x0c0 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci#define PCIE_CONF_VEND_ID 0x100 768c2ecf20Sopenharmony_ci#define PCIE_CONF_DEVICE_ID 0x102 778c2ecf20Sopenharmony_ci#define PCIE_CONF_CLASS_ID 0x106 788c2ecf20Sopenharmony_ci 798c2ecf20Sopenharmony_ci#define PCIE_INT_MASK 0x420 808c2ecf20Sopenharmony_ci#define INTX_MASK GENMASK(19, 16) 818c2ecf20Sopenharmony_ci#define INTX_SHIFT 16 828c2ecf20Sopenharmony_ci#define PCIE_INT_STATUS 0x424 838c2ecf20Sopenharmony_ci#define MSI_STATUS BIT(23) 848c2ecf20Sopenharmony_ci#define PCIE_IMSI_STATUS 0x42c 858c2ecf20Sopenharmony_ci#define PCIE_IMSI_ADDR 0x430 868c2ecf20Sopenharmony_ci#define MSI_MASK BIT(23) 878c2ecf20Sopenharmony_ci#define MTK_MSI_IRQS_NUM 32 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_ci#define PCIE_AHB_TRANS_BASE0_L 0x438 908c2ecf20Sopenharmony_ci#define PCIE_AHB_TRANS_BASE0_H 0x43c 918c2ecf20Sopenharmony_ci#define AHB2PCIE_SIZE(x) ((x) & GENMASK(4, 0)) 928c2ecf20Sopenharmony_ci#define PCIE_AXI_WINDOW0 0x448 938c2ecf20Sopenharmony_ci#define WIN_ENABLE BIT(7) 948c2ecf20Sopenharmony_ci/* 958c2ecf20Sopenharmony_ci * Define PCIe to AHB window size as 2^33 to support max 8GB address space 968c2ecf20Sopenharmony_ci * translate, support least 4GB DRAM size access from EP DMA(physical DRAM 978c2ecf20Sopenharmony_ci * start from 0x40000000). 988c2ecf20Sopenharmony_ci */ 998c2ecf20Sopenharmony_ci#define PCIE2AHB_SIZE 0x21 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci/* PCIe V2 configuration transaction header */ 1028c2ecf20Sopenharmony_ci#define PCIE_CFG_HEADER0 0x460 1038c2ecf20Sopenharmony_ci#define PCIE_CFG_HEADER1 0x464 1048c2ecf20Sopenharmony_ci#define PCIE_CFG_HEADER2 0x468 1058c2ecf20Sopenharmony_ci#define PCIE_CFG_WDATA 0x470 1068c2ecf20Sopenharmony_ci#define PCIE_APP_TLP_REQ 0x488 1078c2ecf20Sopenharmony_ci#define PCIE_CFG_RDATA 0x48c 1088c2ecf20Sopenharmony_ci#define APP_CFG_REQ BIT(0) 1098c2ecf20Sopenharmony_ci#define APP_CPL_STATUS GENMASK(7, 5) 1108c2ecf20Sopenharmony_ci 1118c2ecf20Sopenharmony_ci#define CFG_WRRD_TYPE_0 4 1128c2ecf20Sopenharmony_ci#define CFG_WR_FMT 2 1138c2ecf20Sopenharmony_ci#define CFG_RD_FMT 0 1148c2ecf20Sopenharmony_ci 1158c2ecf20Sopenharmony_ci#define CFG_DW0_LENGTH(length) ((length) & GENMASK(9, 0)) 1168c2ecf20Sopenharmony_ci#define CFG_DW0_TYPE(type) (((type) << 24) & GENMASK(28, 24)) 1178c2ecf20Sopenharmony_ci#define CFG_DW0_FMT(fmt) (((fmt) << 29) & GENMASK(31, 29)) 1188c2ecf20Sopenharmony_ci#define CFG_DW2_REGN(regn) ((regn) & GENMASK(11, 2)) 1198c2ecf20Sopenharmony_ci#define CFG_DW2_FUN(fun) (((fun) << 16) & GENMASK(18, 16)) 1208c2ecf20Sopenharmony_ci#define CFG_DW2_DEV(dev) (((dev) << 19) & GENMASK(23, 19)) 1218c2ecf20Sopenharmony_ci#define CFG_DW2_BUS(bus) (((bus) << 24) & GENMASK(31, 24)) 1228c2ecf20Sopenharmony_ci#define CFG_HEADER_DW0(type, fmt) \ 1238c2ecf20Sopenharmony_ci (CFG_DW0_LENGTH(1) | CFG_DW0_TYPE(type) | CFG_DW0_FMT(fmt)) 1248c2ecf20Sopenharmony_ci#define CFG_HEADER_DW1(where, size) \ 1258c2ecf20Sopenharmony_ci (GENMASK(((size) - 1), 0) << ((where) & 0x3)) 1268c2ecf20Sopenharmony_ci#define CFG_HEADER_DW2(regn, fun, dev, bus) \ 1278c2ecf20Sopenharmony_ci (CFG_DW2_REGN(regn) | CFG_DW2_FUN(fun) | \ 1288c2ecf20Sopenharmony_ci CFG_DW2_DEV(dev) | CFG_DW2_BUS(bus)) 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci#define PCIE_RST_CTRL 0x510 1318c2ecf20Sopenharmony_ci#define PCIE_PHY_RSTB BIT(0) 1328c2ecf20Sopenharmony_ci#define PCIE_PIPE_SRSTB BIT(1) 1338c2ecf20Sopenharmony_ci#define PCIE_MAC_SRSTB BIT(2) 1348c2ecf20Sopenharmony_ci#define PCIE_CRSTB BIT(3) 1358c2ecf20Sopenharmony_ci#define PCIE_PERSTB BIT(8) 1368c2ecf20Sopenharmony_ci#define PCIE_LINKDOWN_RST_EN GENMASK(15, 13) 1378c2ecf20Sopenharmony_ci#define PCIE_LINK_STATUS_V2 0x804 1388c2ecf20Sopenharmony_ci#define PCIE_PORT_LINKUP_V2 BIT(10) 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_cistruct mtk_pcie_port; 1418c2ecf20Sopenharmony_ci 1428c2ecf20Sopenharmony_ci/** 1438c2ecf20Sopenharmony_ci * struct mtk_pcie_soc - differentiate between host generations 1448c2ecf20Sopenharmony_ci * @need_fix_class_id: whether this host's class ID needed to be fixed or not 1458c2ecf20Sopenharmony_ci * @need_fix_device_id: whether this host's device ID needed to be fixed or not 1468c2ecf20Sopenharmony_ci * @device_id: device ID which this host need to be fixed 1478c2ecf20Sopenharmony_ci * @ops: pointer to configuration access functions 1488c2ecf20Sopenharmony_ci * @startup: pointer to controller setting functions 1498c2ecf20Sopenharmony_ci * @setup_irq: pointer to initialize IRQ functions 1508c2ecf20Sopenharmony_ci */ 1518c2ecf20Sopenharmony_cistruct mtk_pcie_soc { 1528c2ecf20Sopenharmony_ci bool need_fix_class_id; 1538c2ecf20Sopenharmony_ci bool need_fix_device_id; 1548c2ecf20Sopenharmony_ci unsigned int device_id; 1558c2ecf20Sopenharmony_ci struct pci_ops *ops; 1568c2ecf20Sopenharmony_ci int (*startup)(struct mtk_pcie_port *port); 1578c2ecf20Sopenharmony_ci int (*setup_irq)(struct mtk_pcie_port *port, struct device_node *node); 1588c2ecf20Sopenharmony_ci}; 1598c2ecf20Sopenharmony_ci 1608c2ecf20Sopenharmony_ci/** 1618c2ecf20Sopenharmony_ci * struct mtk_pcie_port - PCIe port information 1628c2ecf20Sopenharmony_ci * @base: IO mapped register base 1638c2ecf20Sopenharmony_ci * @list: port list 1648c2ecf20Sopenharmony_ci * @pcie: pointer to PCIe host info 1658c2ecf20Sopenharmony_ci * @reset: pointer to port reset control 1668c2ecf20Sopenharmony_ci * @sys_ck: pointer to transaction/data link layer clock 1678c2ecf20Sopenharmony_ci * @ahb_ck: pointer to AHB slave interface operating clock for CSR access 1688c2ecf20Sopenharmony_ci * and RC initiated MMIO access 1698c2ecf20Sopenharmony_ci * @axi_ck: pointer to application layer MMIO channel operating clock 1708c2ecf20Sopenharmony_ci * @aux_ck: pointer to pe2_mac_bridge and pe2_mac_core operating clock 1718c2ecf20Sopenharmony_ci * when pcie_mac_ck/pcie_pipe_ck is turned off 1728c2ecf20Sopenharmony_ci * @obff_ck: pointer to OBFF functional block operating clock 1738c2ecf20Sopenharmony_ci * @pipe_ck: pointer to LTSSM and PHY/MAC layer operating clock 1748c2ecf20Sopenharmony_ci * @phy: pointer to PHY control block 1758c2ecf20Sopenharmony_ci * @slot: port slot 1768c2ecf20Sopenharmony_ci * @irq: GIC irq 1778c2ecf20Sopenharmony_ci * @irq_domain: legacy INTx IRQ domain 1788c2ecf20Sopenharmony_ci * @inner_domain: inner IRQ domain 1798c2ecf20Sopenharmony_ci * @msi_domain: MSI IRQ domain 1808c2ecf20Sopenharmony_ci * @lock: protect the msi_irq_in_use bitmap 1818c2ecf20Sopenharmony_ci * @msi_irq_in_use: bit map for assigned MSI IRQ 1828c2ecf20Sopenharmony_ci */ 1838c2ecf20Sopenharmony_cistruct mtk_pcie_port { 1848c2ecf20Sopenharmony_ci void __iomem *base; 1858c2ecf20Sopenharmony_ci struct list_head list; 1868c2ecf20Sopenharmony_ci struct mtk_pcie *pcie; 1878c2ecf20Sopenharmony_ci struct reset_control *reset; 1888c2ecf20Sopenharmony_ci struct clk *sys_ck; 1898c2ecf20Sopenharmony_ci struct clk *ahb_ck; 1908c2ecf20Sopenharmony_ci struct clk *axi_ck; 1918c2ecf20Sopenharmony_ci struct clk *aux_ck; 1928c2ecf20Sopenharmony_ci struct clk *obff_ck; 1938c2ecf20Sopenharmony_ci struct clk *pipe_ck; 1948c2ecf20Sopenharmony_ci struct phy *phy; 1958c2ecf20Sopenharmony_ci u32 slot; 1968c2ecf20Sopenharmony_ci int irq; 1978c2ecf20Sopenharmony_ci struct irq_domain *irq_domain; 1988c2ecf20Sopenharmony_ci struct irq_domain *inner_domain; 1998c2ecf20Sopenharmony_ci struct irq_domain *msi_domain; 2008c2ecf20Sopenharmony_ci struct mutex lock; 2018c2ecf20Sopenharmony_ci DECLARE_BITMAP(msi_irq_in_use, MTK_MSI_IRQS_NUM); 2028c2ecf20Sopenharmony_ci}; 2038c2ecf20Sopenharmony_ci 2048c2ecf20Sopenharmony_ci/** 2058c2ecf20Sopenharmony_ci * struct mtk_pcie - PCIe host information 2068c2ecf20Sopenharmony_ci * @dev: pointer to PCIe device 2078c2ecf20Sopenharmony_ci * @base: IO mapped register base 2088c2ecf20Sopenharmony_ci * @free_ck: free-run reference clock 2098c2ecf20Sopenharmony_ci * @mem: non-prefetchable memory resource 2108c2ecf20Sopenharmony_ci * @ports: pointer to PCIe port information 2118c2ecf20Sopenharmony_ci * @soc: pointer to SoC-dependent operations 2128c2ecf20Sopenharmony_ci */ 2138c2ecf20Sopenharmony_cistruct mtk_pcie { 2148c2ecf20Sopenharmony_ci struct device *dev; 2158c2ecf20Sopenharmony_ci void __iomem *base; 2168c2ecf20Sopenharmony_ci struct clk *free_ck; 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci struct list_head ports; 2198c2ecf20Sopenharmony_ci const struct mtk_pcie_soc *soc; 2208c2ecf20Sopenharmony_ci}; 2218c2ecf20Sopenharmony_ci 2228c2ecf20Sopenharmony_cistatic void mtk_pcie_subsys_powerdown(struct mtk_pcie *pcie) 2238c2ecf20Sopenharmony_ci{ 2248c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci clk_disable_unprepare(pcie->free_ck); 2278c2ecf20Sopenharmony_ci 2288c2ecf20Sopenharmony_ci pm_runtime_put_sync(dev); 2298c2ecf20Sopenharmony_ci pm_runtime_disable(dev); 2308c2ecf20Sopenharmony_ci} 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_cistatic void mtk_pcie_port_free(struct mtk_pcie_port *port) 2338c2ecf20Sopenharmony_ci{ 2348c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = port->pcie; 2358c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 2368c2ecf20Sopenharmony_ci 2378c2ecf20Sopenharmony_ci devm_iounmap(dev, port->base); 2388c2ecf20Sopenharmony_ci list_del(&port->list); 2398c2ecf20Sopenharmony_ci devm_kfree(dev, port); 2408c2ecf20Sopenharmony_ci} 2418c2ecf20Sopenharmony_ci 2428c2ecf20Sopenharmony_cistatic void mtk_pcie_put_resources(struct mtk_pcie *pcie) 2438c2ecf20Sopenharmony_ci{ 2448c2ecf20Sopenharmony_ci struct mtk_pcie_port *port, *tmp; 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci list_for_each_entry_safe(port, tmp, &pcie->ports, list) { 2478c2ecf20Sopenharmony_ci phy_power_off(port->phy); 2488c2ecf20Sopenharmony_ci phy_exit(port->phy); 2498c2ecf20Sopenharmony_ci clk_disable_unprepare(port->pipe_ck); 2508c2ecf20Sopenharmony_ci clk_disable_unprepare(port->obff_ck); 2518c2ecf20Sopenharmony_ci clk_disable_unprepare(port->axi_ck); 2528c2ecf20Sopenharmony_ci clk_disable_unprepare(port->aux_ck); 2538c2ecf20Sopenharmony_ci clk_disable_unprepare(port->ahb_ck); 2548c2ecf20Sopenharmony_ci clk_disable_unprepare(port->sys_ck); 2558c2ecf20Sopenharmony_ci mtk_pcie_port_free(port); 2568c2ecf20Sopenharmony_ci } 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci mtk_pcie_subsys_powerdown(pcie); 2598c2ecf20Sopenharmony_ci} 2608c2ecf20Sopenharmony_ci 2618c2ecf20Sopenharmony_cistatic int mtk_pcie_check_cfg_cpld(struct mtk_pcie_port *port) 2628c2ecf20Sopenharmony_ci{ 2638c2ecf20Sopenharmony_ci u32 val; 2648c2ecf20Sopenharmony_ci int err; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci err = readl_poll_timeout_atomic(port->base + PCIE_APP_TLP_REQ, val, 2678c2ecf20Sopenharmony_ci !(val & APP_CFG_REQ), 10, 2688c2ecf20Sopenharmony_ci 100 * USEC_PER_MSEC); 2698c2ecf20Sopenharmony_ci if (err) 2708c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 2718c2ecf20Sopenharmony_ci 2728c2ecf20Sopenharmony_ci if (readl(port->base + PCIE_APP_TLP_REQ) & APP_CPL_STATUS) 2738c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 2748c2ecf20Sopenharmony_ci 2758c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 2768c2ecf20Sopenharmony_ci} 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_cistatic int mtk_pcie_hw_rd_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn, 2798c2ecf20Sopenharmony_ci int where, int size, u32 *val) 2808c2ecf20Sopenharmony_ci{ 2818c2ecf20Sopenharmony_ci u32 tmp; 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_ci /* Write PCIe configuration transaction header for Cfgrd */ 2848c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_RD_FMT), 2858c2ecf20Sopenharmony_ci port->base + PCIE_CFG_HEADER0); 2868c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); 2878c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus), 2888c2ecf20Sopenharmony_ci port->base + PCIE_CFG_HEADER2); 2898c2ecf20Sopenharmony_ci 2908c2ecf20Sopenharmony_ci /* Trigger h/w to transmit Cfgrd TLP */ 2918c2ecf20Sopenharmony_ci tmp = readl(port->base + PCIE_APP_TLP_REQ); 2928c2ecf20Sopenharmony_ci tmp |= APP_CFG_REQ; 2938c2ecf20Sopenharmony_ci writel(tmp, port->base + PCIE_APP_TLP_REQ); 2948c2ecf20Sopenharmony_ci 2958c2ecf20Sopenharmony_ci /* Check completion status */ 2968c2ecf20Sopenharmony_ci if (mtk_pcie_check_cfg_cpld(port)) 2978c2ecf20Sopenharmony_ci return PCIBIOS_SET_FAILED; 2988c2ecf20Sopenharmony_ci 2998c2ecf20Sopenharmony_ci /* Read cpld payload of Cfgrd */ 3008c2ecf20Sopenharmony_ci *val = readl(port->base + PCIE_CFG_RDATA); 3018c2ecf20Sopenharmony_ci 3028c2ecf20Sopenharmony_ci if (size == 1) 3038c2ecf20Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xff; 3048c2ecf20Sopenharmony_ci else if (size == 2) 3058c2ecf20Sopenharmony_ci *val = (*val >> (8 * (where & 3))) & 0xffff; 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ci return PCIBIOS_SUCCESSFUL; 3088c2ecf20Sopenharmony_ci} 3098c2ecf20Sopenharmony_ci 3108c2ecf20Sopenharmony_cistatic int mtk_pcie_hw_wr_cfg(struct mtk_pcie_port *port, u32 bus, u32 devfn, 3118c2ecf20Sopenharmony_ci int where, int size, u32 val) 3128c2ecf20Sopenharmony_ci{ 3138c2ecf20Sopenharmony_ci /* Write PCIe configuration transaction header for Cfgwr */ 3148c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW0(CFG_WRRD_TYPE_0, CFG_WR_FMT), 3158c2ecf20Sopenharmony_ci port->base + PCIE_CFG_HEADER0); 3168c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW1(where, size), port->base + PCIE_CFG_HEADER1); 3178c2ecf20Sopenharmony_ci writel(CFG_HEADER_DW2(where, PCI_FUNC(devfn), PCI_SLOT(devfn), bus), 3188c2ecf20Sopenharmony_ci port->base + PCIE_CFG_HEADER2); 3198c2ecf20Sopenharmony_ci 3208c2ecf20Sopenharmony_ci /* Write Cfgwr data */ 3218c2ecf20Sopenharmony_ci val = val << 8 * (where & 3); 3228c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_CFG_WDATA); 3238c2ecf20Sopenharmony_ci 3248c2ecf20Sopenharmony_ci /* Trigger h/w to transmit Cfgwr TLP */ 3258c2ecf20Sopenharmony_ci val = readl(port->base + PCIE_APP_TLP_REQ); 3268c2ecf20Sopenharmony_ci val |= APP_CFG_REQ; 3278c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_APP_TLP_REQ); 3288c2ecf20Sopenharmony_ci 3298c2ecf20Sopenharmony_ci /* Check completion status */ 3308c2ecf20Sopenharmony_ci return mtk_pcie_check_cfg_cpld(port); 3318c2ecf20Sopenharmony_ci} 3328c2ecf20Sopenharmony_ci 3338c2ecf20Sopenharmony_cistatic struct mtk_pcie_port *mtk_pcie_find_port(struct pci_bus *bus, 3348c2ecf20Sopenharmony_ci unsigned int devfn) 3358c2ecf20Sopenharmony_ci{ 3368c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = bus->sysdata; 3378c2ecf20Sopenharmony_ci struct mtk_pcie_port *port; 3388c2ecf20Sopenharmony_ci struct pci_dev *dev = NULL; 3398c2ecf20Sopenharmony_ci 3408c2ecf20Sopenharmony_ci /* 3418c2ecf20Sopenharmony_ci * Walk the bus hierarchy to get the devfn value 3428c2ecf20Sopenharmony_ci * of the port in the root bus. 3438c2ecf20Sopenharmony_ci */ 3448c2ecf20Sopenharmony_ci while (bus && bus->number) { 3458c2ecf20Sopenharmony_ci dev = bus->self; 3468c2ecf20Sopenharmony_ci bus = dev->bus; 3478c2ecf20Sopenharmony_ci devfn = dev->devfn; 3488c2ecf20Sopenharmony_ci } 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ci list_for_each_entry(port, &pcie->ports, list) 3518c2ecf20Sopenharmony_ci if (port->slot == PCI_SLOT(devfn)) 3528c2ecf20Sopenharmony_ci return port; 3538c2ecf20Sopenharmony_ci 3548c2ecf20Sopenharmony_ci return NULL; 3558c2ecf20Sopenharmony_ci} 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_cistatic int mtk_pcie_config_read(struct pci_bus *bus, unsigned int devfn, 3588c2ecf20Sopenharmony_ci int where, int size, u32 *val) 3598c2ecf20Sopenharmony_ci{ 3608c2ecf20Sopenharmony_ci struct mtk_pcie_port *port; 3618c2ecf20Sopenharmony_ci u32 bn = bus->number; 3628c2ecf20Sopenharmony_ci int ret; 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ci port = mtk_pcie_find_port(bus, devfn); 3658c2ecf20Sopenharmony_ci if (!port) { 3668c2ecf20Sopenharmony_ci *val = ~0; 3678c2ecf20Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 3688c2ecf20Sopenharmony_ci } 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci ret = mtk_pcie_hw_rd_cfg(port, bn, devfn, where, size, val); 3718c2ecf20Sopenharmony_ci if (ret) 3728c2ecf20Sopenharmony_ci *val = ~0; 3738c2ecf20Sopenharmony_ci 3748c2ecf20Sopenharmony_ci return ret; 3758c2ecf20Sopenharmony_ci} 3768c2ecf20Sopenharmony_ci 3778c2ecf20Sopenharmony_cistatic int mtk_pcie_config_write(struct pci_bus *bus, unsigned int devfn, 3788c2ecf20Sopenharmony_ci int where, int size, u32 val) 3798c2ecf20Sopenharmony_ci{ 3808c2ecf20Sopenharmony_ci struct mtk_pcie_port *port; 3818c2ecf20Sopenharmony_ci u32 bn = bus->number; 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ci port = mtk_pcie_find_port(bus, devfn); 3848c2ecf20Sopenharmony_ci if (!port) 3858c2ecf20Sopenharmony_ci return PCIBIOS_DEVICE_NOT_FOUND; 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci return mtk_pcie_hw_wr_cfg(port, bn, devfn, where, size, val); 3888c2ecf20Sopenharmony_ci} 3898c2ecf20Sopenharmony_ci 3908c2ecf20Sopenharmony_cistatic struct pci_ops mtk_pcie_ops_v2 = { 3918c2ecf20Sopenharmony_ci .read = mtk_pcie_config_read, 3928c2ecf20Sopenharmony_ci .write = mtk_pcie_config_write, 3938c2ecf20Sopenharmony_ci}; 3948c2ecf20Sopenharmony_ci 3958c2ecf20Sopenharmony_cistatic void mtk_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) 3968c2ecf20Sopenharmony_ci{ 3978c2ecf20Sopenharmony_ci struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); 3988c2ecf20Sopenharmony_ci phys_addr_t addr; 3998c2ecf20Sopenharmony_ci 4008c2ecf20Sopenharmony_ci /* MT2712/MT7622 only support 32-bit MSI addresses */ 4018c2ecf20Sopenharmony_ci addr = virt_to_phys(port->base + PCIE_MSI_VECTOR); 4028c2ecf20Sopenharmony_ci msg->address_hi = 0; 4038c2ecf20Sopenharmony_ci msg->address_lo = lower_32_bits(addr); 4048c2ecf20Sopenharmony_ci 4058c2ecf20Sopenharmony_ci msg->data = data->hwirq; 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_ci dev_dbg(port->pcie->dev, "msi#%d address_hi %#x address_lo %#x\n", 4088c2ecf20Sopenharmony_ci (int)data->hwirq, msg->address_hi, msg->address_lo); 4098c2ecf20Sopenharmony_ci} 4108c2ecf20Sopenharmony_ci 4118c2ecf20Sopenharmony_cistatic int mtk_msi_set_affinity(struct irq_data *irq_data, 4128c2ecf20Sopenharmony_ci const struct cpumask *mask, bool force) 4138c2ecf20Sopenharmony_ci{ 4148c2ecf20Sopenharmony_ci return -EINVAL; 4158c2ecf20Sopenharmony_ci} 4168c2ecf20Sopenharmony_ci 4178c2ecf20Sopenharmony_cistatic void mtk_msi_ack_irq(struct irq_data *data) 4188c2ecf20Sopenharmony_ci{ 4198c2ecf20Sopenharmony_ci struct mtk_pcie_port *port = irq_data_get_irq_chip_data(data); 4208c2ecf20Sopenharmony_ci u32 hwirq = data->hwirq; 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ci writel(1 << hwirq, port->base + PCIE_IMSI_STATUS); 4238c2ecf20Sopenharmony_ci} 4248c2ecf20Sopenharmony_ci 4258c2ecf20Sopenharmony_cistatic struct irq_chip mtk_msi_bottom_irq_chip = { 4268c2ecf20Sopenharmony_ci .name = "MTK MSI", 4278c2ecf20Sopenharmony_ci .irq_compose_msi_msg = mtk_compose_msi_msg, 4288c2ecf20Sopenharmony_ci .irq_set_affinity = mtk_msi_set_affinity, 4298c2ecf20Sopenharmony_ci .irq_ack = mtk_msi_ack_irq, 4308c2ecf20Sopenharmony_ci}; 4318c2ecf20Sopenharmony_ci 4328c2ecf20Sopenharmony_cistatic int mtk_pcie_irq_domain_alloc(struct irq_domain *domain, unsigned int virq, 4338c2ecf20Sopenharmony_ci unsigned int nr_irqs, void *args) 4348c2ecf20Sopenharmony_ci{ 4358c2ecf20Sopenharmony_ci struct mtk_pcie_port *port = domain->host_data; 4368c2ecf20Sopenharmony_ci unsigned long bit; 4378c2ecf20Sopenharmony_ci 4388c2ecf20Sopenharmony_ci WARN_ON(nr_irqs != 1); 4398c2ecf20Sopenharmony_ci mutex_lock(&port->lock); 4408c2ecf20Sopenharmony_ci 4418c2ecf20Sopenharmony_ci bit = find_first_zero_bit(port->msi_irq_in_use, MTK_MSI_IRQS_NUM); 4428c2ecf20Sopenharmony_ci if (bit >= MTK_MSI_IRQS_NUM) { 4438c2ecf20Sopenharmony_ci mutex_unlock(&port->lock); 4448c2ecf20Sopenharmony_ci return -ENOSPC; 4458c2ecf20Sopenharmony_ci } 4468c2ecf20Sopenharmony_ci 4478c2ecf20Sopenharmony_ci __set_bit(bit, port->msi_irq_in_use); 4488c2ecf20Sopenharmony_ci 4498c2ecf20Sopenharmony_ci mutex_unlock(&port->lock); 4508c2ecf20Sopenharmony_ci 4518c2ecf20Sopenharmony_ci irq_domain_set_info(domain, virq, bit, &mtk_msi_bottom_irq_chip, 4528c2ecf20Sopenharmony_ci domain->host_data, handle_edge_irq, 4538c2ecf20Sopenharmony_ci NULL, NULL); 4548c2ecf20Sopenharmony_ci 4558c2ecf20Sopenharmony_ci return 0; 4568c2ecf20Sopenharmony_ci} 4578c2ecf20Sopenharmony_ci 4588c2ecf20Sopenharmony_cistatic void mtk_pcie_irq_domain_free(struct irq_domain *domain, 4598c2ecf20Sopenharmony_ci unsigned int virq, unsigned int nr_irqs) 4608c2ecf20Sopenharmony_ci{ 4618c2ecf20Sopenharmony_ci struct irq_data *d = irq_domain_get_irq_data(domain, virq); 4628c2ecf20Sopenharmony_ci struct mtk_pcie_port *port = irq_data_get_irq_chip_data(d); 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci mutex_lock(&port->lock); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ci if (!test_bit(d->hwirq, port->msi_irq_in_use)) 4678c2ecf20Sopenharmony_ci dev_err(port->pcie->dev, "trying to free unused MSI#%lu\n", 4688c2ecf20Sopenharmony_ci d->hwirq); 4698c2ecf20Sopenharmony_ci else 4708c2ecf20Sopenharmony_ci __clear_bit(d->hwirq, port->msi_irq_in_use); 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ci mutex_unlock(&port->lock); 4738c2ecf20Sopenharmony_ci 4748c2ecf20Sopenharmony_ci irq_domain_free_irqs_parent(domain, virq, nr_irqs); 4758c2ecf20Sopenharmony_ci} 4768c2ecf20Sopenharmony_ci 4778c2ecf20Sopenharmony_cistatic const struct irq_domain_ops msi_domain_ops = { 4788c2ecf20Sopenharmony_ci .alloc = mtk_pcie_irq_domain_alloc, 4798c2ecf20Sopenharmony_ci .free = mtk_pcie_irq_domain_free, 4808c2ecf20Sopenharmony_ci}; 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_cistatic struct irq_chip mtk_msi_irq_chip = { 4838c2ecf20Sopenharmony_ci .name = "MTK PCIe MSI", 4848c2ecf20Sopenharmony_ci .irq_ack = irq_chip_ack_parent, 4858c2ecf20Sopenharmony_ci .irq_mask = pci_msi_mask_irq, 4868c2ecf20Sopenharmony_ci .irq_unmask = pci_msi_unmask_irq, 4878c2ecf20Sopenharmony_ci}; 4888c2ecf20Sopenharmony_ci 4898c2ecf20Sopenharmony_cistatic struct msi_domain_info mtk_msi_domain_info = { 4908c2ecf20Sopenharmony_ci .flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS | 4918c2ecf20Sopenharmony_ci MSI_FLAG_PCI_MSIX), 4928c2ecf20Sopenharmony_ci .chip = &mtk_msi_irq_chip, 4938c2ecf20Sopenharmony_ci}; 4948c2ecf20Sopenharmony_ci 4958c2ecf20Sopenharmony_cistatic int mtk_pcie_allocate_msi_domains(struct mtk_pcie_port *port) 4968c2ecf20Sopenharmony_ci{ 4978c2ecf20Sopenharmony_ci struct fwnode_handle *fwnode = of_node_to_fwnode(port->pcie->dev->of_node); 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci mutex_init(&port->lock); 5008c2ecf20Sopenharmony_ci 5018c2ecf20Sopenharmony_ci port->inner_domain = irq_domain_create_linear(fwnode, MTK_MSI_IRQS_NUM, 5028c2ecf20Sopenharmony_ci &msi_domain_ops, port); 5038c2ecf20Sopenharmony_ci if (!port->inner_domain) { 5048c2ecf20Sopenharmony_ci dev_err(port->pcie->dev, "failed to create IRQ domain\n"); 5058c2ecf20Sopenharmony_ci return -ENOMEM; 5068c2ecf20Sopenharmony_ci } 5078c2ecf20Sopenharmony_ci 5088c2ecf20Sopenharmony_ci port->msi_domain = pci_msi_create_irq_domain(fwnode, &mtk_msi_domain_info, 5098c2ecf20Sopenharmony_ci port->inner_domain); 5108c2ecf20Sopenharmony_ci if (!port->msi_domain) { 5118c2ecf20Sopenharmony_ci dev_err(port->pcie->dev, "failed to create MSI domain\n"); 5128c2ecf20Sopenharmony_ci irq_domain_remove(port->inner_domain); 5138c2ecf20Sopenharmony_ci return -ENOMEM; 5148c2ecf20Sopenharmony_ci } 5158c2ecf20Sopenharmony_ci 5168c2ecf20Sopenharmony_ci return 0; 5178c2ecf20Sopenharmony_ci} 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_cistatic void mtk_pcie_enable_msi(struct mtk_pcie_port *port) 5208c2ecf20Sopenharmony_ci{ 5218c2ecf20Sopenharmony_ci u32 val; 5228c2ecf20Sopenharmony_ci phys_addr_t msg_addr; 5238c2ecf20Sopenharmony_ci 5248c2ecf20Sopenharmony_ci msg_addr = virt_to_phys(port->base + PCIE_MSI_VECTOR); 5258c2ecf20Sopenharmony_ci val = lower_32_bits(msg_addr); 5268c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_IMSI_ADDR); 5278c2ecf20Sopenharmony_ci 5288c2ecf20Sopenharmony_ci val = readl(port->base + PCIE_INT_MASK); 5298c2ecf20Sopenharmony_ci val &= ~MSI_MASK; 5308c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_INT_MASK); 5318c2ecf20Sopenharmony_ci} 5328c2ecf20Sopenharmony_ci 5338c2ecf20Sopenharmony_cistatic void mtk_pcie_irq_teardown(struct mtk_pcie *pcie) 5348c2ecf20Sopenharmony_ci{ 5358c2ecf20Sopenharmony_ci struct mtk_pcie_port *port, *tmp; 5368c2ecf20Sopenharmony_ci 5378c2ecf20Sopenharmony_ci list_for_each_entry_safe(port, tmp, &pcie->ports, list) { 5388c2ecf20Sopenharmony_ci irq_set_chained_handler_and_data(port->irq, NULL, NULL); 5398c2ecf20Sopenharmony_ci 5408c2ecf20Sopenharmony_ci if (port->irq_domain) 5418c2ecf20Sopenharmony_ci irq_domain_remove(port->irq_domain); 5428c2ecf20Sopenharmony_ci 5438c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PCI_MSI)) { 5448c2ecf20Sopenharmony_ci if (port->msi_domain) 5458c2ecf20Sopenharmony_ci irq_domain_remove(port->msi_domain); 5468c2ecf20Sopenharmony_ci if (port->inner_domain) 5478c2ecf20Sopenharmony_ci irq_domain_remove(port->inner_domain); 5488c2ecf20Sopenharmony_ci } 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ci irq_dispose_mapping(port->irq); 5518c2ecf20Sopenharmony_ci } 5528c2ecf20Sopenharmony_ci} 5538c2ecf20Sopenharmony_ci 5548c2ecf20Sopenharmony_cistatic int mtk_pcie_intx_map(struct irq_domain *domain, unsigned int irq, 5558c2ecf20Sopenharmony_ci irq_hw_number_t hwirq) 5568c2ecf20Sopenharmony_ci{ 5578c2ecf20Sopenharmony_ci irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq); 5588c2ecf20Sopenharmony_ci irq_set_chip_data(irq, domain->host_data); 5598c2ecf20Sopenharmony_ci 5608c2ecf20Sopenharmony_ci return 0; 5618c2ecf20Sopenharmony_ci} 5628c2ecf20Sopenharmony_ci 5638c2ecf20Sopenharmony_cistatic const struct irq_domain_ops intx_domain_ops = { 5648c2ecf20Sopenharmony_ci .map = mtk_pcie_intx_map, 5658c2ecf20Sopenharmony_ci}; 5668c2ecf20Sopenharmony_ci 5678c2ecf20Sopenharmony_cistatic int mtk_pcie_init_irq_domain(struct mtk_pcie_port *port, 5688c2ecf20Sopenharmony_ci struct device_node *node) 5698c2ecf20Sopenharmony_ci{ 5708c2ecf20Sopenharmony_ci struct device *dev = port->pcie->dev; 5718c2ecf20Sopenharmony_ci struct device_node *pcie_intc_node; 5728c2ecf20Sopenharmony_ci int ret; 5738c2ecf20Sopenharmony_ci 5748c2ecf20Sopenharmony_ci /* Setup INTx */ 5758c2ecf20Sopenharmony_ci pcie_intc_node = of_get_next_child(node, NULL); 5768c2ecf20Sopenharmony_ci if (!pcie_intc_node) { 5778c2ecf20Sopenharmony_ci dev_err(dev, "no PCIe Intc node found\n"); 5788c2ecf20Sopenharmony_ci return -ENODEV; 5798c2ecf20Sopenharmony_ci } 5808c2ecf20Sopenharmony_ci 5818c2ecf20Sopenharmony_ci port->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX, 5828c2ecf20Sopenharmony_ci &intx_domain_ops, port); 5838c2ecf20Sopenharmony_ci of_node_put(pcie_intc_node); 5848c2ecf20Sopenharmony_ci if (!port->irq_domain) { 5858c2ecf20Sopenharmony_ci dev_err(dev, "failed to get INTx IRQ domain\n"); 5868c2ecf20Sopenharmony_ci return -ENODEV; 5878c2ecf20Sopenharmony_ci } 5888c2ecf20Sopenharmony_ci 5898c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PCI_MSI)) { 5908c2ecf20Sopenharmony_ci ret = mtk_pcie_allocate_msi_domains(port); 5918c2ecf20Sopenharmony_ci if (ret) 5928c2ecf20Sopenharmony_ci return ret; 5938c2ecf20Sopenharmony_ci } 5948c2ecf20Sopenharmony_ci 5958c2ecf20Sopenharmony_ci return 0; 5968c2ecf20Sopenharmony_ci} 5978c2ecf20Sopenharmony_ci 5988c2ecf20Sopenharmony_cistatic void mtk_pcie_intr_handler(struct irq_desc *desc) 5998c2ecf20Sopenharmony_ci{ 6008c2ecf20Sopenharmony_ci struct mtk_pcie_port *port = irq_desc_get_handler_data(desc); 6018c2ecf20Sopenharmony_ci struct irq_chip *irqchip = irq_desc_get_chip(desc); 6028c2ecf20Sopenharmony_ci unsigned long status; 6038c2ecf20Sopenharmony_ci u32 virq; 6048c2ecf20Sopenharmony_ci u32 bit = INTX_SHIFT; 6058c2ecf20Sopenharmony_ci 6068c2ecf20Sopenharmony_ci chained_irq_enter(irqchip, desc); 6078c2ecf20Sopenharmony_ci 6088c2ecf20Sopenharmony_ci status = readl(port->base + PCIE_INT_STATUS); 6098c2ecf20Sopenharmony_ci if (status & INTX_MASK) { 6108c2ecf20Sopenharmony_ci for_each_set_bit_from(bit, &status, PCI_NUM_INTX + INTX_SHIFT) { 6118c2ecf20Sopenharmony_ci /* Clear the INTx */ 6128c2ecf20Sopenharmony_ci writel(1 << bit, port->base + PCIE_INT_STATUS); 6138c2ecf20Sopenharmony_ci virq = irq_find_mapping(port->irq_domain, 6148c2ecf20Sopenharmony_ci bit - INTX_SHIFT); 6158c2ecf20Sopenharmony_ci generic_handle_irq(virq); 6168c2ecf20Sopenharmony_ci } 6178c2ecf20Sopenharmony_ci } 6188c2ecf20Sopenharmony_ci 6198c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PCI_MSI)) { 6208c2ecf20Sopenharmony_ci if (status & MSI_STATUS){ 6218c2ecf20Sopenharmony_ci unsigned long imsi_status; 6228c2ecf20Sopenharmony_ci 6238c2ecf20Sopenharmony_ci /* 6248c2ecf20Sopenharmony_ci * The interrupt status can be cleared even if the 6258c2ecf20Sopenharmony_ci * MSI status remains pending. As such, given the 6268c2ecf20Sopenharmony_ci * edge-triggered interrupt type, its status should 6278c2ecf20Sopenharmony_ci * be cleared before being dispatched to the 6288c2ecf20Sopenharmony_ci * handler of the underlying device. 6298c2ecf20Sopenharmony_ci */ 6308c2ecf20Sopenharmony_ci writel(MSI_STATUS, port->base + PCIE_INT_STATUS); 6318c2ecf20Sopenharmony_ci while ((imsi_status = readl(port->base + PCIE_IMSI_STATUS))) { 6328c2ecf20Sopenharmony_ci for_each_set_bit(bit, &imsi_status, MTK_MSI_IRQS_NUM) { 6338c2ecf20Sopenharmony_ci virq = irq_find_mapping(port->inner_domain, bit); 6348c2ecf20Sopenharmony_ci generic_handle_irq(virq); 6358c2ecf20Sopenharmony_ci } 6368c2ecf20Sopenharmony_ci } 6378c2ecf20Sopenharmony_ci } 6388c2ecf20Sopenharmony_ci } 6398c2ecf20Sopenharmony_ci 6408c2ecf20Sopenharmony_ci chained_irq_exit(irqchip, desc); 6418c2ecf20Sopenharmony_ci} 6428c2ecf20Sopenharmony_ci 6438c2ecf20Sopenharmony_cistatic int mtk_pcie_setup_irq(struct mtk_pcie_port *port, 6448c2ecf20Sopenharmony_ci struct device_node *node) 6458c2ecf20Sopenharmony_ci{ 6468c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = port->pcie; 6478c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 6488c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 6498c2ecf20Sopenharmony_ci int err; 6508c2ecf20Sopenharmony_ci 6518c2ecf20Sopenharmony_ci err = mtk_pcie_init_irq_domain(port, node); 6528c2ecf20Sopenharmony_ci if (err) { 6538c2ecf20Sopenharmony_ci dev_err(dev, "failed to init PCIe IRQ domain\n"); 6548c2ecf20Sopenharmony_ci return err; 6558c2ecf20Sopenharmony_ci } 6568c2ecf20Sopenharmony_ci 6578c2ecf20Sopenharmony_ci port->irq = platform_get_irq(pdev, port->slot); 6588c2ecf20Sopenharmony_ci if (port->irq < 0) 6598c2ecf20Sopenharmony_ci return port->irq; 6608c2ecf20Sopenharmony_ci 6618c2ecf20Sopenharmony_ci irq_set_chained_handler_and_data(port->irq, 6628c2ecf20Sopenharmony_ci mtk_pcie_intr_handler, port); 6638c2ecf20Sopenharmony_ci 6648c2ecf20Sopenharmony_ci return 0; 6658c2ecf20Sopenharmony_ci} 6668c2ecf20Sopenharmony_ci 6678c2ecf20Sopenharmony_cistatic int mtk_pcie_startup_port_v2(struct mtk_pcie_port *port) 6688c2ecf20Sopenharmony_ci{ 6698c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = port->pcie; 6708c2ecf20Sopenharmony_ci struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); 6718c2ecf20Sopenharmony_ci struct resource *mem = NULL; 6728c2ecf20Sopenharmony_ci struct resource_entry *entry; 6738c2ecf20Sopenharmony_ci const struct mtk_pcie_soc *soc = port->pcie->soc; 6748c2ecf20Sopenharmony_ci u32 val; 6758c2ecf20Sopenharmony_ci int err; 6768c2ecf20Sopenharmony_ci 6778c2ecf20Sopenharmony_ci entry = resource_list_first_type(&host->windows, IORESOURCE_MEM); 6788c2ecf20Sopenharmony_ci if (entry) 6798c2ecf20Sopenharmony_ci mem = entry->res; 6808c2ecf20Sopenharmony_ci if (!mem) 6818c2ecf20Sopenharmony_ci return -EINVAL; 6828c2ecf20Sopenharmony_ci 6838c2ecf20Sopenharmony_ci /* MT7622 platforms need to enable LTSSM and ASPM from PCIe subsys */ 6848c2ecf20Sopenharmony_ci if (pcie->base) { 6858c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_SYS_CFG_V2); 6868c2ecf20Sopenharmony_ci val |= PCIE_CSR_LTSSM_EN(port->slot) | 6878c2ecf20Sopenharmony_ci PCIE_CSR_ASPM_L1_EN(port->slot); 6888c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_SYS_CFG_V2); 6898c2ecf20Sopenharmony_ci } 6908c2ecf20Sopenharmony_ci 6918c2ecf20Sopenharmony_ci /* Assert all reset signals */ 6928c2ecf20Sopenharmony_ci writel(0, port->base + PCIE_RST_CTRL); 6938c2ecf20Sopenharmony_ci 6948c2ecf20Sopenharmony_ci /* 6958c2ecf20Sopenharmony_ci * Enable PCIe link down reset, if link status changed from link up to 6968c2ecf20Sopenharmony_ci * link down, this will reset MAC control registers and configuration 6978c2ecf20Sopenharmony_ci * space. 6988c2ecf20Sopenharmony_ci */ 6998c2ecf20Sopenharmony_ci writel(PCIE_LINKDOWN_RST_EN, port->base + PCIE_RST_CTRL); 7008c2ecf20Sopenharmony_ci 7018c2ecf20Sopenharmony_ci /* De-assert PHY, PE, PIPE, MAC and configuration reset */ 7028c2ecf20Sopenharmony_ci val = readl(port->base + PCIE_RST_CTRL); 7038c2ecf20Sopenharmony_ci val |= PCIE_PHY_RSTB | PCIE_PERSTB | PCIE_PIPE_SRSTB | 7048c2ecf20Sopenharmony_ci PCIE_MAC_SRSTB | PCIE_CRSTB; 7058c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_RST_CTRL); 7068c2ecf20Sopenharmony_ci 7078c2ecf20Sopenharmony_ci /* Set up vendor ID and class code */ 7088c2ecf20Sopenharmony_ci if (soc->need_fix_class_id) { 7098c2ecf20Sopenharmony_ci val = PCI_VENDOR_ID_MEDIATEK; 7108c2ecf20Sopenharmony_ci writew(val, port->base + PCIE_CONF_VEND_ID); 7118c2ecf20Sopenharmony_ci 7128c2ecf20Sopenharmony_ci val = PCI_CLASS_BRIDGE_PCI; 7138c2ecf20Sopenharmony_ci writew(val, port->base + PCIE_CONF_CLASS_ID); 7148c2ecf20Sopenharmony_ci } 7158c2ecf20Sopenharmony_ci 7168c2ecf20Sopenharmony_ci if (soc->need_fix_device_id) 7178c2ecf20Sopenharmony_ci writew(soc->device_id, port->base + PCIE_CONF_DEVICE_ID); 7188c2ecf20Sopenharmony_ci 7198c2ecf20Sopenharmony_ci /* 100ms timeout value should be enough for Gen1/2 training */ 7208c2ecf20Sopenharmony_ci err = readl_poll_timeout(port->base + PCIE_LINK_STATUS_V2, val, 7218c2ecf20Sopenharmony_ci !!(val & PCIE_PORT_LINKUP_V2), 20, 7228c2ecf20Sopenharmony_ci 100 * USEC_PER_MSEC); 7238c2ecf20Sopenharmony_ci if (err) 7248c2ecf20Sopenharmony_ci return -ETIMEDOUT; 7258c2ecf20Sopenharmony_ci 7268c2ecf20Sopenharmony_ci /* Set INTx mask */ 7278c2ecf20Sopenharmony_ci val = readl(port->base + PCIE_INT_MASK); 7288c2ecf20Sopenharmony_ci val &= ~INTX_MASK; 7298c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_INT_MASK); 7308c2ecf20Sopenharmony_ci 7318c2ecf20Sopenharmony_ci if (IS_ENABLED(CONFIG_PCI_MSI)) 7328c2ecf20Sopenharmony_ci mtk_pcie_enable_msi(port); 7338c2ecf20Sopenharmony_ci 7348c2ecf20Sopenharmony_ci /* Set AHB to PCIe translation windows */ 7358c2ecf20Sopenharmony_ci val = lower_32_bits(mem->start) | 7368c2ecf20Sopenharmony_ci AHB2PCIE_SIZE(fls(resource_size(mem))); 7378c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_AHB_TRANS_BASE0_L); 7388c2ecf20Sopenharmony_ci 7398c2ecf20Sopenharmony_ci val = upper_32_bits(mem->start); 7408c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_AHB_TRANS_BASE0_H); 7418c2ecf20Sopenharmony_ci 7428c2ecf20Sopenharmony_ci /* Set PCIe to AXI translation memory space.*/ 7438c2ecf20Sopenharmony_ci val = PCIE2AHB_SIZE | WIN_ENABLE; 7448c2ecf20Sopenharmony_ci writel(val, port->base + PCIE_AXI_WINDOW0); 7458c2ecf20Sopenharmony_ci 7468c2ecf20Sopenharmony_ci return 0; 7478c2ecf20Sopenharmony_ci} 7488c2ecf20Sopenharmony_ci 7498c2ecf20Sopenharmony_cistatic void __iomem *mtk_pcie_map_bus(struct pci_bus *bus, 7508c2ecf20Sopenharmony_ci unsigned int devfn, int where) 7518c2ecf20Sopenharmony_ci{ 7528c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = bus->sysdata; 7538c2ecf20Sopenharmony_ci 7548c2ecf20Sopenharmony_ci writel(PCIE_CONF_ADDR(where, PCI_FUNC(devfn), PCI_SLOT(devfn), 7558c2ecf20Sopenharmony_ci bus->number), pcie->base + PCIE_CFG_ADDR); 7568c2ecf20Sopenharmony_ci 7578c2ecf20Sopenharmony_ci return pcie->base + PCIE_CFG_DATA + (where & 3); 7588c2ecf20Sopenharmony_ci} 7598c2ecf20Sopenharmony_ci 7608c2ecf20Sopenharmony_cistatic struct pci_ops mtk_pcie_ops = { 7618c2ecf20Sopenharmony_ci .map_bus = mtk_pcie_map_bus, 7628c2ecf20Sopenharmony_ci .read = pci_generic_config_read, 7638c2ecf20Sopenharmony_ci .write = pci_generic_config_write, 7648c2ecf20Sopenharmony_ci}; 7658c2ecf20Sopenharmony_ci 7668c2ecf20Sopenharmony_cistatic int mtk_pcie_startup_port(struct mtk_pcie_port *port) 7678c2ecf20Sopenharmony_ci{ 7688c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = port->pcie; 7698c2ecf20Sopenharmony_ci u32 func = PCI_FUNC(port->slot << 3); 7708c2ecf20Sopenharmony_ci u32 slot = PCI_SLOT(port->slot << 3); 7718c2ecf20Sopenharmony_ci u32 val; 7728c2ecf20Sopenharmony_ci int err; 7738c2ecf20Sopenharmony_ci 7748c2ecf20Sopenharmony_ci /* assert port PERST_N */ 7758c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_SYS_CFG); 7768c2ecf20Sopenharmony_ci val |= PCIE_PORT_PERST(port->slot); 7778c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_SYS_CFG); 7788c2ecf20Sopenharmony_ci 7798c2ecf20Sopenharmony_ci /* de-assert port PERST_N */ 7808c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_SYS_CFG); 7818c2ecf20Sopenharmony_ci val &= ~PCIE_PORT_PERST(port->slot); 7828c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_SYS_CFG); 7838c2ecf20Sopenharmony_ci 7848c2ecf20Sopenharmony_ci /* 100ms timeout value should be enough for Gen1/2 training */ 7858c2ecf20Sopenharmony_ci err = readl_poll_timeout(port->base + PCIE_LINK_STATUS, val, 7868c2ecf20Sopenharmony_ci !!(val & PCIE_PORT_LINKUP), 20, 7878c2ecf20Sopenharmony_ci 100 * USEC_PER_MSEC); 7888c2ecf20Sopenharmony_ci if (err) 7898c2ecf20Sopenharmony_ci return -ETIMEDOUT; 7908c2ecf20Sopenharmony_ci 7918c2ecf20Sopenharmony_ci /* enable interrupt */ 7928c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_INT_ENABLE); 7938c2ecf20Sopenharmony_ci val |= PCIE_PORT_INT_EN(port->slot); 7948c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_INT_ENABLE); 7958c2ecf20Sopenharmony_ci 7968c2ecf20Sopenharmony_ci /* map to all DDR region. We need to set it before cfg operation. */ 7978c2ecf20Sopenharmony_ci writel(PCIE_BAR_MAP_MAX | PCIE_BAR_ENABLE, 7988c2ecf20Sopenharmony_ci port->base + PCIE_BAR0_SETUP); 7998c2ecf20Sopenharmony_ci 8008c2ecf20Sopenharmony_ci /* configure class code and revision ID */ 8018c2ecf20Sopenharmony_ci writel(PCIE_CLASS_CODE | PCIE_REVISION_ID, port->base + PCIE_CLASS); 8028c2ecf20Sopenharmony_ci 8038c2ecf20Sopenharmony_ci /* configure FC credit */ 8048c2ecf20Sopenharmony_ci writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0), 8058c2ecf20Sopenharmony_ci pcie->base + PCIE_CFG_ADDR); 8068c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_CFG_DATA); 8078c2ecf20Sopenharmony_ci val &= ~PCIE_FC_CREDIT_MASK; 8088c2ecf20Sopenharmony_ci val |= PCIE_FC_CREDIT_VAL(0x806c); 8098c2ecf20Sopenharmony_ci writel(PCIE_CONF_ADDR(PCIE_FC_CREDIT, func, slot, 0), 8108c2ecf20Sopenharmony_ci pcie->base + PCIE_CFG_ADDR); 8118c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_CFG_DATA); 8128c2ecf20Sopenharmony_ci 8138c2ecf20Sopenharmony_ci /* configure RC FTS number to 250 when it leaves L0s */ 8148c2ecf20Sopenharmony_ci writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0), 8158c2ecf20Sopenharmony_ci pcie->base + PCIE_CFG_ADDR); 8168c2ecf20Sopenharmony_ci val = readl(pcie->base + PCIE_CFG_DATA); 8178c2ecf20Sopenharmony_ci val &= ~PCIE_FTS_NUM_MASK; 8188c2ecf20Sopenharmony_ci val |= PCIE_FTS_NUM_L0(0x50); 8198c2ecf20Sopenharmony_ci writel(PCIE_CONF_ADDR(PCIE_FTS_NUM, func, slot, 0), 8208c2ecf20Sopenharmony_ci pcie->base + PCIE_CFG_ADDR); 8218c2ecf20Sopenharmony_ci writel(val, pcie->base + PCIE_CFG_DATA); 8228c2ecf20Sopenharmony_ci 8238c2ecf20Sopenharmony_ci return 0; 8248c2ecf20Sopenharmony_ci} 8258c2ecf20Sopenharmony_ci 8268c2ecf20Sopenharmony_cistatic void mtk_pcie_enable_port(struct mtk_pcie_port *port) 8278c2ecf20Sopenharmony_ci{ 8288c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = port->pcie; 8298c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 8308c2ecf20Sopenharmony_ci int err; 8318c2ecf20Sopenharmony_ci 8328c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->sys_ck); 8338c2ecf20Sopenharmony_ci if (err) { 8348c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable sys_ck%d clock\n", port->slot); 8358c2ecf20Sopenharmony_ci goto err_sys_clk; 8368c2ecf20Sopenharmony_ci } 8378c2ecf20Sopenharmony_ci 8388c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->ahb_ck); 8398c2ecf20Sopenharmony_ci if (err) { 8408c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable ahb_ck%d\n", port->slot); 8418c2ecf20Sopenharmony_ci goto err_ahb_clk; 8428c2ecf20Sopenharmony_ci } 8438c2ecf20Sopenharmony_ci 8448c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->aux_ck); 8458c2ecf20Sopenharmony_ci if (err) { 8468c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable aux_ck%d\n", port->slot); 8478c2ecf20Sopenharmony_ci goto err_aux_clk; 8488c2ecf20Sopenharmony_ci } 8498c2ecf20Sopenharmony_ci 8508c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->axi_ck); 8518c2ecf20Sopenharmony_ci if (err) { 8528c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable axi_ck%d\n", port->slot); 8538c2ecf20Sopenharmony_ci goto err_axi_clk; 8548c2ecf20Sopenharmony_ci } 8558c2ecf20Sopenharmony_ci 8568c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->obff_ck); 8578c2ecf20Sopenharmony_ci if (err) { 8588c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable obff_ck%d\n", port->slot); 8598c2ecf20Sopenharmony_ci goto err_obff_clk; 8608c2ecf20Sopenharmony_ci } 8618c2ecf20Sopenharmony_ci 8628c2ecf20Sopenharmony_ci err = clk_prepare_enable(port->pipe_ck); 8638c2ecf20Sopenharmony_ci if (err) { 8648c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable pipe_ck%d\n", port->slot); 8658c2ecf20Sopenharmony_ci goto err_pipe_clk; 8668c2ecf20Sopenharmony_ci } 8678c2ecf20Sopenharmony_ci 8688c2ecf20Sopenharmony_ci reset_control_assert(port->reset); 8698c2ecf20Sopenharmony_ci reset_control_deassert(port->reset); 8708c2ecf20Sopenharmony_ci 8718c2ecf20Sopenharmony_ci err = phy_init(port->phy); 8728c2ecf20Sopenharmony_ci if (err) { 8738c2ecf20Sopenharmony_ci dev_err(dev, "failed to initialize port%d phy\n", port->slot); 8748c2ecf20Sopenharmony_ci goto err_phy_init; 8758c2ecf20Sopenharmony_ci } 8768c2ecf20Sopenharmony_ci 8778c2ecf20Sopenharmony_ci err = phy_power_on(port->phy); 8788c2ecf20Sopenharmony_ci if (err) { 8798c2ecf20Sopenharmony_ci dev_err(dev, "failed to power on port%d phy\n", port->slot); 8808c2ecf20Sopenharmony_ci goto err_phy_on; 8818c2ecf20Sopenharmony_ci } 8828c2ecf20Sopenharmony_ci 8838c2ecf20Sopenharmony_ci if (!pcie->soc->startup(port)) 8848c2ecf20Sopenharmony_ci return; 8858c2ecf20Sopenharmony_ci 8868c2ecf20Sopenharmony_ci dev_info(dev, "Port%d link down\n", port->slot); 8878c2ecf20Sopenharmony_ci 8888c2ecf20Sopenharmony_ci phy_power_off(port->phy); 8898c2ecf20Sopenharmony_cierr_phy_on: 8908c2ecf20Sopenharmony_ci phy_exit(port->phy); 8918c2ecf20Sopenharmony_cierr_phy_init: 8928c2ecf20Sopenharmony_ci clk_disable_unprepare(port->pipe_ck); 8938c2ecf20Sopenharmony_cierr_pipe_clk: 8948c2ecf20Sopenharmony_ci clk_disable_unprepare(port->obff_ck); 8958c2ecf20Sopenharmony_cierr_obff_clk: 8968c2ecf20Sopenharmony_ci clk_disable_unprepare(port->axi_ck); 8978c2ecf20Sopenharmony_cierr_axi_clk: 8988c2ecf20Sopenharmony_ci clk_disable_unprepare(port->aux_ck); 8998c2ecf20Sopenharmony_cierr_aux_clk: 9008c2ecf20Sopenharmony_ci clk_disable_unprepare(port->ahb_ck); 9018c2ecf20Sopenharmony_cierr_ahb_clk: 9028c2ecf20Sopenharmony_ci clk_disable_unprepare(port->sys_ck); 9038c2ecf20Sopenharmony_cierr_sys_clk: 9048c2ecf20Sopenharmony_ci mtk_pcie_port_free(port); 9058c2ecf20Sopenharmony_ci} 9068c2ecf20Sopenharmony_ci 9078c2ecf20Sopenharmony_cistatic int mtk_pcie_parse_port(struct mtk_pcie *pcie, 9088c2ecf20Sopenharmony_ci struct device_node *node, 9098c2ecf20Sopenharmony_ci int slot) 9108c2ecf20Sopenharmony_ci{ 9118c2ecf20Sopenharmony_ci struct mtk_pcie_port *port; 9128c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 9138c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 9148c2ecf20Sopenharmony_ci char name[10]; 9158c2ecf20Sopenharmony_ci int err; 9168c2ecf20Sopenharmony_ci 9178c2ecf20Sopenharmony_ci port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL); 9188c2ecf20Sopenharmony_ci if (!port) 9198c2ecf20Sopenharmony_ci return -ENOMEM; 9208c2ecf20Sopenharmony_ci 9218c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "port%d", slot); 9228c2ecf20Sopenharmony_ci port->base = devm_platform_ioremap_resource_byname(pdev, name); 9238c2ecf20Sopenharmony_ci if (IS_ERR(port->base)) { 9248c2ecf20Sopenharmony_ci dev_err(dev, "failed to map port%d base\n", slot); 9258c2ecf20Sopenharmony_ci return PTR_ERR(port->base); 9268c2ecf20Sopenharmony_ci } 9278c2ecf20Sopenharmony_ci 9288c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "sys_ck%d", slot); 9298c2ecf20Sopenharmony_ci port->sys_ck = devm_clk_get(dev, name); 9308c2ecf20Sopenharmony_ci if (IS_ERR(port->sys_ck)) { 9318c2ecf20Sopenharmony_ci dev_err(dev, "failed to get sys_ck%d clock\n", slot); 9328c2ecf20Sopenharmony_ci return PTR_ERR(port->sys_ck); 9338c2ecf20Sopenharmony_ci } 9348c2ecf20Sopenharmony_ci 9358c2ecf20Sopenharmony_ci /* sys_ck might be divided into the following parts in some chips */ 9368c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "ahb_ck%d", slot); 9378c2ecf20Sopenharmony_ci port->ahb_ck = devm_clk_get_optional(dev, name); 9388c2ecf20Sopenharmony_ci if (IS_ERR(port->ahb_ck)) 9398c2ecf20Sopenharmony_ci return PTR_ERR(port->ahb_ck); 9408c2ecf20Sopenharmony_ci 9418c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "axi_ck%d", slot); 9428c2ecf20Sopenharmony_ci port->axi_ck = devm_clk_get_optional(dev, name); 9438c2ecf20Sopenharmony_ci if (IS_ERR(port->axi_ck)) 9448c2ecf20Sopenharmony_ci return PTR_ERR(port->axi_ck); 9458c2ecf20Sopenharmony_ci 9468c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "aux_ck%d", slot); 9478c2ecf20Sopenharmony_ci port->aux_ck = devm_clk_get_optional(dev, name); 9488c2ecf20Sopenharmony_ci if (IS_ERR(port->aux_ck)) 9498c2ecf20Sopenharmony_ci return PTR_ERR(port->aux_ck); 9508c2ecf20Sopenharmony_ci 9518c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "obff_ck%d", slot); 9528c2ecf20Sopenharmony_ci port->obff_ck = devm_clk_get_optional(dev, name); 9538c2ecf20Sopenharmony_ci if (IS_ERR(port->obff_ck)) 9548c2ecf20Sopenharmony_ci return PTR_ERR(port->obff_ck); 9558c2ecf20Sopenharmony_ci 9568c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "pipe_ck%d", slot); 9578c2ecf20Sopenharmony_ci port->pipe_ck = devm_clk_get_optional(dev, name); 9588c2ecf20Sopenharmony_ci if (IS_ERR(port->pipe_ck)) 9598c2ecf20Sopenharmony_ci return PTR_ERR(port->pipe_ck); 9608c2ecf20Sopenharmony_ci 9618c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "pcie-rst%d", slot); 9628c2ecf20Sopenharmony_ci port->reset = devm_reset_control_get_optional_exclusive(dev, name); 9638c2ecf20Sopenharmony_ci if (PTR_ERR(port->reset) == -EPROBE_DEFER) 9648c2ecf20Sopenharmony_ci return PTR_ERR(port->reset); 9658c2ecf20Sopenharmony_ci 9668c2ecf20Sopenharmony_ci /* some platforms may use default PHY setting */ 9678c2ecf20Sopenharmony_ci snprintf(name, sizeof(name), "pcie-phy%d", slot); 9688c2ecf20Sopenharmony_ci port->phy = devm_phy_optional_get(dev, name); 9698c2ecf20Sopenharmony_ci if (IS_ERR(port->phy)) 9708c2ecf20Sopenharmony_ci return PTR_ERR(port->phy); 9718c2ecf20Sopenharmony_ci 9728c2ecf20Sopenharmony_ci port->slot = slot; 9738c2ecf20Sopenharmony_ci port->pcie = pcie; 9748c2ecf20Sopenharmony_ci 9758c2ecf20Sopenharmony_ci if (pcie->soc->setup_irq) { 9768c2ecf20Sopenharmony_ci err = pcie->soc->setup_irq(port, node); 9778c2ecf20Sopenharmony_ci if (err) 9788c2ecf20Sopenharmony_ci return err; 9798c2ecf20Sopenharmony_ci } 9808c2ecf20Sopenharmony_ci 9818c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&port->list); 9828c2ecf20Sopenharmony_ci list_add_tail(&port->list, &pcie->ports); 9838c2ecf20Sopenharmony_ci 9848c2ecf20Sopenharmony_ci return 0; 9858c2ecf20Sopenharmony_ci} 9868c2ecf20Sopenharmony_ci 9878c2ecf20Sopenharmony_cistatic int mtk_pcie_subsys_powerup(struct mtk_pcie *pcie) 9888c2ecf20Sopenharmony_ci{ 9898c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 9908c2ecf20Sopenharmony_ci struct platform_device *pdev = to_platform_device(dev); 9918c2ecf20Sopenharmony_ci struct resource *regs; 9928c2ecf20Sopenharmony_ci int err; 9938c2ecf20Sopenharmony_ci 9948c2ecf20Sopenharmony_ci /* get shared registers, which are optional */ 9958c2ecf20Sopenharmony_ci regs = platform_get_resource_byname(pdev, IORESOURCE_MEM, "subsys"); 9968c2ecf20Sopenharmony_ci if (regs) { 9978c2ecf20Sopenharmony_ci pcie->base = devm_ioremap_resource(dev, regs); 9988c2ecf20Sopenharmony_ci if (IS_ERR(pcie->base)) { 9998c2ecf20Sopenharmony_ci dev_err(dev, "failed to map shared register\n"); 10008c2ecf20Sopenharmony_ci return PTR_ERR(pcie->base); 10018c2ecf20Sopenharmony_ci } 10028c2ecf20Sopenharmony_ci } 10038c2ecf20Sopenharmony_ci 10048c2ecf20Sopenharmony_ci pcie->free_ck = devm_clk_get(dev, "free_ck"); 10058c2ecf20Sopenharmony_ci if (IS_ERR(pcie->free_ck)) { 10068c2ecf20Sopenharmony_ci if (PTR_ERR(pcie->free_ck) == -EPROBE_DEFER) 10078c2ecf20Sopenharmony_ci return -EPROBE_DEFER; 10088c2ecf20Sopenharmony_ci 10098c2ecf20Sopenharmony_ci pcie->free_ck = NULL; 10108c2ecf20Sopenharmony_ci } 10118c2ecf20Sopenharmony_ci 10128c2ecf20Sopenharmony_ci pm_runtime_enable(dev); 10138c2ecf20Sopenharmony_ci pm_runtime_get_sync(dev); 10148c2ecf20Sopenharmony_ci 10158c2ecf20Sopenharmony_ci /* enable top level clock */ 10168c2ecf20Sopenharmony_ci err = clk_prepare_enable(pcie->free_ck); 10178c2ecf20Sopenharmony_ci if (err) { 10188c2ecf20Sopenharmony_ci dev_err(dev, "failed to enable free_ck\n"); 10198c2ecf20Sopenharmony_ci goto err_free_ck; 10208c2ecf20Sopenharmony_ci } 10218c2ecf20Sopenharmony_ci 10228c2ecf20Sopenharmony_ci return 0; 10238c2ecf20Sopenharmony_ci 10248c2ecf20Sopenharmony_cierr_free_ck: 10258c2ecf20Sopenharmony_ci pm_runtime_put_sync(dev); 10268c2ecf20Sopenharmony_ci pm_runtime_disable(dev); 10278c2ecf20Sopenharmony_ci 10288c2ecf20Sopenharmony_ci return err; 10298c2ecf20Sopenharmony_ci} 10308c2ecf20Sopenharmony_ci 10318c2ecf20Sopenharmony_cistatic int mtk_pcie_setup(struct mtk_pcie *pcie) 10328c2ecf20Sopenharmony_ci{ 10338c2ecf20Sopenharmony_ci struct device *dev = pcie->dev; 10348c2ecf20Sopenharmony_ci struct device_node *node = dev->of_node, *child; 10358c2ecf20Sopenharmony_ci struct mtk_pcie_port *port, *tmp; 10368c2ecf20Sopenharmony_ci int err; 10378c2ecf20Sopenharmony_ci 10388c2ecf20Sopenharmony_ci for_each_available_child_of_node(node, child) { 10398c2ecf20Sopenharmony_ci int slot; 10408c2ecf20Sopenharmony_ci 10418c2ecf20Sopenharmony_ci err = of_pci_get_devfn(child); 10428c2ecf20Sopenharmony_ci if (err < 0) { 10438c2ecf20Sopenharmony_ci dev_err(dev, "failed to parse devfn: %d\n", err); 10448c2ecf20Sopenharmony_ci goto error_put_node; 10458c2ecf20Sopenharmony_ci } 10468c2ecf20Sopenharmony_ci 10478c2ecf20Sopenharmony_ci slot = PCI_SLOT(err); 10488c2ecf20Sopenharmony_ci 10498c2ecf20Sopenharmony_ci err = mtk_pcie_parse_port(pcie, child, slot); 10508c2ecf20Sopenharmony_ci if (err) 10518c2ecf20Sopenharmony_ci goto error_put_node; 10528c2ecf20Sopenharmony_ci } 10538c2ecf20Sopenharmony_ci 10548c2ecf20Sopenharmony_ci err = mtk_pcie_subsys_powerup(pcie); 10558c2ecf20Sopenharmony_ci if (err) 10568c2ecf20Sopenharmony_ci return err; 10578c2ecf20Sopenharmony_ci 10588c2ecf20Sopenharmony_ci /* enable each port, and then check link status */ 10598c2ecf20Sopenharmony_ci list_for_each_entry_safe(port, tmp, &pcie->ports, list) 10608c2ecf20Sopenharmony_ci mtk_pcie_enable_port(port); 10618c2ecf20Sopenharmony_ci 10628c2ecf20Sopenharmony_ci /* power down PCIe subsys if slots are all empty (link down) */ 10638c2ecf20Sopenharmony_ci if (list_empty(&pcie->ports)) 10648c2ecf20Sopenharmony_ci mtk_pcie_subsys_powerdown(pcie); 10658c2ecf20Sopenharmony_ci 10668c2ecf20Sopenharmony_ci return 0; 10678c2ecf20Sopenharmony_cierror_put_node: 10688c2ecf20Sopenharmony_ci of_node_put(child); 10698c2ecf20Sopenharmony_ci return err; 10708c2ecf20Sopenharmony_ci} 10718c2ecf20Sopenharmony_ci 10728c2ecf20Sopenharmony_cistatic int mtk_pcie_probe(struct platform_device *pdev) 10738c2ecf20Sopenharmony_ci{ 10748c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 10758c2ecf20Sopenharmony_ci struct mtk_pcie *pcie; 10768c2ecf20Sopenharmony_ci struct pci_host_bridge *host; 10778c2ecf20Sopenharmony_ci int err; 10788c2ecf20Sopenharmony_ci 10798c2ecf20Sopenharmony_ci host = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); 10808c2ecf20Sopenharmony_ci if (!host) 10818c2ecf20Sopenharmony_ci return -ENOMEM; 10828c2ecf20Sopenharmony_ci 10838c2ecf20Sopenharmony_ci pcie = pci_host_bridge_priv(host); 10848c2ecf20Sopenharmony_ci 10858c2ecf20Sopenharmony_ci pcie->dev = dev; 10868c2ecf20Sopenharmony_ci pcie->soc = of_device_get_match_data(dev); 10878c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, pcie); 10888c2ecf20Sopenharmony_ci INIT_LIST_HEAD(&pcie->ports); 10898c2ecf20Sopenharmony_ci 10908c2ecf20Sopenharmony_ci err = mtk_pcie_setup(pcie); 10918c2ecf20Sopenharmony_ci if (err) 10928c2ecf20Sopenharmony_ci return err; 10938c2ecf20Sopenharmony_ci 10948c2ecf20Sopenharmony_ci host->ops = pcie->soc->ops; 10958c2ecf20Sopenharmony_ci host->sysdata = pcie; 10968c2ecf20Sopenharmony_ci 10978c2ecf20Sopenharmony_ci err = pci_host_probe(host); 10988c2ecf20Sopenharmony_ci if (err) 10998c2ecf20Sopenharmony_ci goto put_resources; 11008c2ecf20Sopenharmony_ci 11018c2ecf20Sopenharmony_ci return 0; 11028c2ecf20Sopenharmony_ci 11038c2ecf20Sopenharmony_ciput_resources: 11048c2ecf20Sopenharmony_ci if (!list_empty(&pcie->ports)) 11058c2ecf20Sopenharmony_ci mtk_pcie_put_resources(pcie); 11068c2ecf20Sopenharmony_ci 11078c2ecf20Sopenharmony_ci return err; 11088c2ecf20Sopenharmony_ci} 11098c2ecf20Sopenharmony_ci 11108c2ecf20Sopenharmony_ci 11118c2ecf20Sopenharmony_cistatic void mtk_pcie_free_resources(struct mtk_pcie *pcie) 11128c2ecf20Sopenharmony_ci{ 11138c2ecf20Sopenharmony_ci struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); 11148c2ecf20Sopenharmony_ci struct list_head *windows = &host->windows; 11158c2ecf20Sopenharmony_ci 11168c2ecf20Sopenharmony_ci pci_free_resource_list(windows); 11178c2ecf20Sopenharmony_ci} 11188c2ecf20Sopenharmony_ci 11198c2ecf20Sopenharmony_cistatic int mtk_pcie_remove(struct platform_device *pdev) 11208c2ecf20Sopenharmony_ci{ 11218c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = platform_get_drvdata(pdev); 11228c2ecf20Sopenharmony_ci struct pci_host_bridge *host = pci_host_bridge_from_priv(pcie); 11238c2ecf20Sopenharmony_ci 11248c2ecf20Sopenharmony_ci pci_stop_root_bus(host->bus); 11258c2ecf20Sopenharmony_ci pci_remove_root_bus(host->bus); 11268c2ecf20Sopenharmony_ci mtk_pcie_free_resources(pcie); 11278c2ecf20Sopenharmony_ci 11288c2ecf20Sopenharmony_ci mtk_pcie_irq_teardown(pcie); 11298c2ecf20Sopenharmony_ci 11308c2ecf20Sopenharmony_ci mtk_pcie_put_resources(pcie); 11318c2ecf20Sopenharmony_ci 11328c2ecf20Sopenharmony_ci return 0; 11338c2ecf20Sopenharmony_ci} 11348c2ecf20Sopenharmony_ci 11358c2ecf20Sopenharmony_cistatic int __maybe_unused mtk_pcie_suspend_noirq(struct device *dev) 11368c2ecf20Sopenharmony_ci{ 11378c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = dev_get_drvdata(dev); 11388c2ecf20Sopenharmony_ci struct mtk_pcie_port *port; 11398c2ecf20Sopenharmony_ci 11408c2ecf20Sopenharmony_ci if (list_empty(&pcie->ports)) 11418c2ecf20Sopenharmony_ci return 0; 11428c2ecf20Sopenharmony_ci 11438c2ecf20Sopenharmony_ci list_for_each_entry(port, &pcie->ports, list) { 11448c2ecf20Sopenharmony_ci clk_disable_unprepare(port->pipe_ck); 11458c2ecf20Sopenharmony_ci clk_disable_unprepare(port->obff_ck); 11468c2ecf20Sopenharmony_ci clk_disable_unprepare(port->axi_ck); 11478c2ecf20Sopenharmony_ci clk_disable_unprepare(port->aux_ck); 11488c2ecf20Sopenharmony_ci clk_disable_unprepare(port->ahb_ck); 11498c2ecf20Sopenharmony_ci clk_disable_unprepare(port->sys_ck); 11508c2ecf20Sopenharmony_ci phy_power_off(port->phy); 11518c2ecf20Sopenharmony_ci phy_exit(port->phy); 11528c2ecf20Sopenharmony_ci } 11538c2ecf20Sopenharmony_ci 11548c2ecf20Sopenharmony_ci clk_disable_unprepare(pcie->free_ck); 11558c2ecf20Sopenharmony_ci 11568c2ecf20Sopenharmony_ci return 0; 11578c2ecf20Sopenharmony_ci} 11588c2ecf20Sopenharmony_ci 11598c2ecf20Sopenharmony_cistatic int __maybe_unused mtk_pcie_resume_noirq(struct device *dev) 11608c2ecf20Sopenharmony_ci{ 11618c2ecf20Sopenharmony_ci struct mtk_pcie *pcie = dev_get_drvdata(dev); 11628c2ecf20Sopenharmony_ci struct mtk_pcie_port *port, *tmp; 11638c2ecf20Sopenharmony_ci 11648c2ecf20Sopenharmony_ci if (list_empty(&pcie->ports)) 11658c2ecf20Sopenharmony_ci return 0; 11668c2ecf20Sopenharmony_ci 11678c2ecf20Sopenharmony_ci clk_prepare_enable(pcie->free_ck); 11688c2ecf20Sopenharmony_ci 11698c2ecf20Sopenharmony_ci list_for_each_entry_safe(port, tmp, &pcie->ports, list) 11708c2ecf20Sopenharmony_ci mtk_pcie_enable_port(port); 11718c2ecf20Sopenharmony_ci 11728c2ecf20Sopenharmony_ci /* In case of EP was removed while system suspend. */ 11738c2ecf20Sopenharmony_ci if (list_empty(&pcie->ports)) 11748c2ecf20Sopenharmony_ci clk_disable_unprepare(pcie->free_ck); 11758c2ecf20Sopenharmony_ci 11768c2ecf20Sopenharmony_ci return 0; 11778c2ecf20Sopenharmony_ci} 11788c2ecf20Sopenharmony_ci 11798c2ecf20Sopenharmony_cistatic const struct dev_pm_ops mtk_pcie_pm_ops = { 11808c2ecf20Sopenharmony_ci SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mtk_pcie_suspend_noirq, 11818c2ecf20Sopenharmony_ci mtk_pcie_resume_noirq) 11828c2ecf20Sopenharmony_ci}; 11838c2ecf20Sopenharmony_ci 11848c2ecf20Sopenharmony_cistatic const struct mtk_pcie_soc mtk_pcie_soc_v1 = { 11858c2ecf20Sopenharmony_ci .ops = &mtk_pcie_ops, 11868c2ecf20Sopenharmony_ci .startup = mtk_pcie_startup_port, 11878c2ecf20Sopenharmony_ci}; 11888c2ecf20Sopenharmony_ci 11898c2ecf20Sopenharmony_cistatic const struct mtk_pcie_soc mtk_pcie_soc_mt2712 = { 11908c2ecf20Sopenharmony_ci .ops = &mtk_pcie_ops_v2, 11918c2ecf20Sopenharmony_ci .startup = mtk_pcie_startup_port_v2, 11928c2ecf20Sopenharmony_ci .setup_irq = mtk_pcie_setup_irq, 11938c2ecf20Sopenharmony_ci}; 11948c2ecf20Sopenharmony_ci 11958c2ecf20Sopenharmony_cistatic const struct mtk_pcie_soc mtk_pcie_soc_mt7622 = { 11968c2ecf20Sopenharmony_ci .need_fix_class_id = true, 11978c2ecf20Sopenharmony_ci .ops = &mtk_pcie_ops_v2, 11988c2ecf20Sopenharmony_ci .startup = mtk_pcie_startup_port_v2, 11998c2ecf20Sopenharmony_ci .setup_irq = mtk_pcie_setup_irq, 12008c2ecf20Sopenharmony_ci}; 12018c2ecf20Sopenharmony_ci 12028c2ecf20Sopenharmony_cistatic const struct mtk_pcie_soc mtk_pcie_soc_mt7629 = { 12038c2ecf20Sopenharmony_ci .need_fix_class_id = true, 12048c2ecf20Sopenharmony_ci .need_fix_device_id = true, 12058c2ecf20Sopenharmony_ci .device_id = PCI_DEVICE_ID_MEDIATEK_7629, 12068c2ecf20Sopenharmony_ci .ops = &mtk_pcie_ops_v2, 12078c2ecf20Sopenharmony_ci .startup = mtk_pcie_startup_port_v2, 12088c2ecf20Sopenharmony_ci .setup_irq = mtk_pcie_setup_irq, 12098c2ecf20Sopenharmony_ci}; 12108c2ecf20Sopenharmony_ci 12118c2ecf20Sopenharmony_cistatic const struct of_device_id mtk_pcie_ids[] = { 12128c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt2701-pcie", .data = &mtk_pcie_soc_v1 }, 12138c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt7623-pcie", .data = &mtk_pcie_soc_v1 }, 12148c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt2712-pcie", .data = &mtk_pcie_soc_mt2712 }, 12158c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt7622-pcie", .data = &mtk_pcie_soc_mt7622 }, 12168c2ecf20Sopenharmony_ci { .compatible = "mediatek,mt7629-pcie", .data = &mtk_pcie_soc_mt7629 }, 12178c2ecf20Sopenharmony_ci {}, 12188c2ecf20Sopenharmony_ci}; 12198c2ecf20Sopenharmony_ci 12208c2ecf20Sopenharmony_cistatic struct platform_driver mtk_pcie_driver = { 12218c2ecf20Sopenharmony_ci .probe = mtk_pcie_probe, 12228c2ecf20Sopenharmony_ci .remove = mtk_pcie_remove, 12238c2ecf20Sopenharmony_ci .driver = { 12248c2ecf20Sopenharmony_ci .name = "mtk-pcie", 12258c2ecf20Sopenharmony_ci .of_match_table = mtk_pcie_ids, 12268c2ecf20Sopenharmony_ci .suppress_bind_attrs = true, 12278c2ecf20Sopenharmony_ci .pm = &mtk_pcie_pm_ops, 12288c2ecf20Sopenharmony_ci }, 12298c2ecf20Sopenharmony_ci}; 12308c2ecf20Sopenharmony_cimodule_platform_driver(mtk_pcie_driver); 12318c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2"); 1232