18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0+
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PCIe host controller driver for NWL PCIe Bridge
48c2ecf20Sopenharmony_ci * Based on pcie-xilinx.c, pci-tegra.c
58c2ecf20Sopenharmony_ci *
68c2ecf20Sopenharmony_ci * (C) Copyright 2014 - 2015, Xilinx, Inc.
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/clk.h>
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
128c2ecf20Sopenharmony_ci#include <linux/irq.h>
138c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
148c2ecf20Sopenharmony_ci#include <linux/kernel.h>
158c2ecf20Sopenharmony_ci#include <linux/init.h>
168c2ecf20Sopenharmony_ci#include <linux/msi.h>
178c2ecf20Sopenharmony_ci#include <linux/of_address.h>
188c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
198c2ecf20Sopenharmony_ci#include <linux/of_platform.h>
208c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
218c2ecf20Sopenharmony_ci#include <linux/pci.h>
228c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
238c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h>
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include "../pci.h"
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci/* Bridge core config registers */
288c2ecf20Sopenharmony_ci#define BRCFG_PCIE_RX0			0x00000000
298c2ecf20Sopenharmony_ci#define BRCFG_INTERRUPT			0x00000010
308c2ecf20Sopenharmony_ci#define BRCFG_PCIE_RX_MSG_FILTER	0x00000020
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* Egress - Bridge translation registers */
338c2ecf20Sopenharmony_ci#define E_BREG_CAPABILITIES		0x00000200
348c2ecf20Sopenharmony_ci#define E_BREG_CONTROL			0x00000208
358c2ecf20Sopenharmony_ci#define E_BREG_BASE_LO			0x00000210
368c2ecf20Sopenharmony_ci#define E_BREG_BASE_HI			0x00000214
378c2ecf20Sopenharmony_ci#define E_ECAM_CAPABILITIES		0x00000220
388c2ecf20Sopenharmony_ci#define E_ECAM_CONTROL			0x00000228
398c2ecf20Sopenharmony_ci#define E_ECAM_BASE_LO			0x00000230
408c2ecf20Sopenharmony_ci#define E_ECAM_BASE_HI			0x00000234
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci/* Ingress - address translations */
438c2ecf20Sopenharmony_ci#define I_MSII_CAPABILITIES		0x00000300
448c2ecf20Sopenharmony_ci#define I_MSII_CONTROL			0x00000308
458c2ecf20Sopenharmony_ci#define I_MSII_BASE_LO			0x00000310
468c2ecf20Sopenharmony_ci#define I_MSII_BASE_HI			0x00000314
478c2ecf20Sopenharmony_ci
488c2ecf20Sopenharmony_ci#define I_ISUB_CONTROL			0x000003E8
498c2ecf20Sopenharmony_ci#define SET_ISUB_CONTROL		BIT(0)
508c2ecf20Sopenharmony_ci/* Rxed msg fifo  - Interrupt status registers */
518c2ecf20Sopenharmony_ci#define MSGF_MISC_STATUS		0x00000400
528c2ecf20Sopenharmony_ci#define MSGF_MISC_MASK			0x00000404
538c2ecf20Sopenharmony_ci#define MSGF_LEG_STATUS			0x00000420
548c2ecf20Sopenharmony_ci#define MSGF_LEG_MASK			0x00000424
558c2ecf20Sopenharmony_ci#define MSGF_MSI_STATUS_LO		0x00000440
568c2ecf20Sopenharmony_ci#define MSGF_MSI_STATUS_HI		0x00000444
578c2ecf20Sopenharmony_ci#define MSGF_MSI_MASK_LO		0x00000448
588c2ecf20Sopenharmony_ci#define MSGF_MSI_MASK_HI		0x0000044C
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci/* Msg filter mask bits */
618c2ecf20Sopenharmony_ci#define CFG_ENABLE_PM_MSG_FWD		BIT(1)
628c2ecf20Sopenharmony_ci#define CFG_ENABLE_INT_MSG_FWD		BIT(2)
638c2ecf20Sopenharmony_ci#define CFG_ENABLE_ERR_MSG_FWD		BIT(3)
648c2ecf20Sopenharmony_ci#define CFG_ENABLE_MSG_FILTER_MASK	(CFG_ENABLE_PM_MSG_FWD | \
658c2ecf20Sopenharmony_ci					CFG_ENABLE_INT_MSG_FWD | \
668c2ecf20Sopenharmony_ci					CFG_ENABLE_ERR_MSG_FWD)
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci/* Misc interrupt status mask bits */
698c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_RXMSG_AVAIL	BIT(0)
708c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_RXMSG_OVER		BIT(1)
718c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_SLAVE_ERR		BIT(4)
728c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_MASTER_ERR		BIT(5)
738c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_I_ADDR_ERR		BIT(6)
748c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_E_ADDR_ERR		BIT(7)
758c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_FATAL_AER		BIT(16)
768c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_NON_FATAL_AER	BIT(17)
778c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_CORR_AER		BIT(18)
788c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_UR_DETECT		BIT(20)
798c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_NON_FATAL_DEV	BIT(22)
808c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_FATAL_DEV		BIT(23)
818c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_LINK_DOWN		BIT(24)
828c2ecf20Sopenharmony_ci#define MSGF_MSIC_SR_LINK_AUTO_BWIDTH	BIT(25)
838c2ecf20Sopenharmony_ci#define MSGF_MSIC_SR_LINK_BWIDTH	BIT(26)
848c2ecf20Sopenharmony_ci
858c2ecf20Sopenharmony_ci#define MSGF_MISC_SR_MASKALL		(MSGF_MISC_SR_RXMSG_AVAIL | \
868c2ecf20Sopenharmony_ci					MSGF_MISC_SR_RXMSG_OVER | \
878c2ecf20Sopenharmony_ci					MSGF_MISC_SR_SLAVE_ERR | \
888c2ecf20Sopenharmony_ci					MSGF_MISC_SR_MASTER_ERR | \
898c2ecf20Sopenharmony_ci					MSGF_MISC_SR_I_ADDR_ERR | \
908c2ecf20Sopenharmony_ci					MSGF_MISC_SR_E_ADDR_ERR | \
918c2ecf20Sopenharmony_ci					MSGF_MISC_SR_FATAL_AER | \
928c2ecf20Sopenharmony_ci					MSGF_MISC_SR_NON_FATAL_AER | \
938c2ecf20Sopenharmony_ci					MSGF_MISC_SR_CORR_AER | \
948c2ecf20Sopenharmony_ci					MSGF_MISC_SR_UR_DETECT | \
958c2ecf20Sopenharmony_ci					MSGF_MISC_SR_NON_FATAL_DEV | \
968c2ecf20Sopenharmony_ci					MSGF_MISC_SR_FATAL_DEV | \
978c2ecf20Sopenharmony_ci					MSGF_MISC_SR_LINK_DOWN | \
988c2ecf20Sopenharmony_ci					MSGF_MSIC_SR_LINK_AUTO_BWIDTH | \
998c2ecf20Sopenharmony_ci					MSGF_MSIC_SR_LINK_BWIDTH)
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci/* Legacy interrupt status mask bits */
1028c2ecf20Sopenharmony_ci#define MSGF_LEG_SR_INTA		BIT(0)
1038c2ecf20Sopenharmony_ci#define MSGF_LEG_SR_INTB		BIT(1)
1048c2ecf20Sopenharmony_ci#define MSGF_LEG_SR_INTC		BIT(2)
1058c2ecf20Sopenharmony_ci#define MSGF_LEG_SR_INTD		BIT(3)
1068c2ecf20Sopenharmony_ci#define MSGF_LEG_SR_MASKALL		(MSGF_LEG_SR_INTA | MSGF_LEG_SR_INTB | \
1078c2ecf20Sopenharmony_ci					MSGF_LEG_SR_INTC | MSGF_LEG_SR_INTD)
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_ci/* MSI interrupt status mask bits */
1108c2ecf20Sopenharmony_ci#define MSGF_MSI_SR_LO_MASK		GENMASK(31, 0)
1118c2ecf20Sopenharmony_ci#define MSGF_MSI_SR_HI_MASK		GENMASK(31, 0)
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_ci#define MSII_PRESENT			BIT(0)
1148c2ecf20Sopenharmony_ci#define MSII_ENABLE			BIT(0)
1158c2ecf20Sopenharmony_ci#define MSII_STATUS_ENABLE		BIT(15)
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci/* Bridge config interrupt mask */
1188c2ecf20Sopenharmony_ci#define BRCFG_INTERRUPT_MASK		BIT(0)
1198c2ecf20Sopenharmony_ci#define BREG_PRESENT			BIT(0)
1208c2ecf20Sopenharmony_ci#define BREG_ENABLE			BIT(0)
1218c2ecf20Sopenharmony_ci#define BREG_ENABLE_FORCE		BIT(1)
1228c2ecf20Sopenharmony_ci
1238c2ecf20Sopenharmony_ci/* E_ECAM status mask bits */
1248c2ecf20Sopenharmony_ci#define E_ECAM_PRESENT			BIT(0)
1258c2ecf20Sopenharmony_ci#define E_ECAM_CR_ENABLE		BIT(0)
1268c2ecf20Sopenharmony_ci#define E_ECAM_SIZE_LOC			GENMASK(20, 16)
1278c2ecf20Sopenharmony_ci#define E_ECAM_SIZE_SHIFT		16
1288c2ecf20Sopenharmony_ci#define ECAM_BUS_LOC_SHIFT		20
1298c2ecf20Sopenharmony_ci#define ECAM_DEV_LOC_SHIFT		12
1308c2ecf20Sopenharmony_ci#define NWL_ECAM_VALUE_DEFAULT		12
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci#define CFG_DMA_REG_BAR			GENMASK(2, 0)
1338c2ecf20Sopenharmony_ci
1348c2ecf20Sopenharmony_ci#define INT_PCI_MSI_NR			(2 * 32)
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci/* Readin the PS_LINKUP */
1378c2ecf20Sopenharmony_ci#define PS_LINKUP_OFFSET		0x00000238
1388c2ecf20Sopenharmony_ci#define PCIE_PHY_LINKUP_BIT		BIT(0)
1398c2ecf20Sopenharmony_ci#define PHY_RDY_LINKUP_BIT		BIT(1)
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_ci/* Parameters for the waiting for link up routine */
1428c2ecf20Sopenharmony_ci#define LINK_WAIT_MAX_RETRIES          10
1438c2ecf20Sopenharmony_ci#define LINK_WAIT_USLEEP_MIN           90000
1448c2ecf20Sopenharmony_ci#define LINK_WAIT_USLEEP_MAX           100000
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_cistruct nwl_msi {			/* MSI information */
1478c2ecf20Sopenharmony_ci	struct irq_domain *msi_domain;
1488c2ecf20Sopenharmony_ci	unsigned long *bitmap;
1498c2ecf20Sopenharmony_ci	struct irq_domain *dev_domain;
1508c2ecf20Sopenharmony_ci	struct mutex lock;		/* protect bitmap variable */
1518c2ecf20Sopenharmony_ci	int irq_msi0;
1528c2ecf20Sopenharmony_ci	int irq_msi1;
1538c2ecf20Sopenharmony_ci};
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistruct nwl_pcie {
1568c2ecf20Sopenharmony_ci	struct device *dev;
1578c2ecf20Sopenharmony_ci	void __iomem *breg_base;
1588c2ecf20Sopenharmony_ci	void __iomem *pcireg_base;
1598c2ecf20Sopenharmony_ci	void __iomem *ecam_base;
1608c2ecf20Sopenharmony_ci	phys_addr_t phys_breg_base;	/* Physical Bridge Register Base */
1618c2ecf20Sopenharmony_ci	phys_addr_t phys_pcie_reg_base;	/* Physical PCIe Controller Base */
1628c2ecf20Sopenharmony_ci	phys_addr_t phys_ecam_base;	/* Physical Configuration Base */
1638c2ecf20Sopenharmony_ci	u32 breg_size;
1648c2ecf20Sopenharmony_ci	u32 pcie_reg_size;
1658c2ecf20Sopenharmony_ci	u32 ecam_size;
1668c2ecf20Sopenharmony_ci	int irq_intx;
1678c2ecf20Sopenharmony_ci	int irq_misc;
1688c2ecf20Sopenharmony_ci	u32 ecam_value;
1698c2ecf20Sopenharmony_ci	u8 last_busno;
1708c2ecf20Sopenharmony_ci	struct nwl_msi msi;
1718c2ecf20Sopenharmony_ci	struct irq_domain *legacy_irq_domain;
1728c2ecf20Sopenharmony_ci	struct clk *clk;
1738c2ecf20Sopenharmony_ci	raw_spinlock_t leg_mask_lock;
1748c2ecf20Sopenharmony_ci};
1758c2ecf20Sopenharmony_ci
1768c2ecf20Sopenharmony_cistatic inline u32 nwl_bridge_readl(struct nwl_pcie *pcie, u32 off)
1778c2ecf20Sopenharmony_ci{
1788c2ecf20Sopenharmony_ci	return readl(pcie->breg_base + off);
1798c2ecf20Sopenharmony_ci}
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_cistatic inline void nwl_bridge_writel(struct nwl_pcie *pcie, u32 val, u32 off)
1828c2ecf20Sopenharmony_ci{
1838c2ecf20Sopenharmony_ci	writel(val, pcie->breg_base + off);
1848c2ecf20Sopenharmony_ci}
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_cistatic bool nwl_pcie_link_up(struct nwl_pcie *pcie)
1878c2ecf20Sopenharmony_ci{
1888c2ecf20Sopenharmony_ci	if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PCIE_PHY_LINKUP_BIT)
1898c2ecf20Sopenharmony_ci		return true;
1908c2ecf20Sopenharmony_ci	return false;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic bool nwl_phy_link_up(struct nwl_pcie *pcie)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	if (readl(pcie->pcireg_base + PS_LINKUP_OFFSET) & PHY_RDY_LINKUP_BIT)
1968c2ecf20Sopenharmony_ci		return true;
1978c2ecf20Sopenharmony_ci	return false;
1988c2ecf20Sopenharmony_ci}
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_cistatic int nwl_wait_for_link(struct nwl_pcie *pcie)
2018c2ecf20Sopenharmony_ci{
2028c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
2038c2ecf20Sopenharmony_ci	int retries;
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ci	/* check if the link is up or not */
2068c2ecf20Sopenharmony_ci	for (retries = 0; retries < LINK_WAIT_MAX_RETRIES; retries++) {
2078c2ecf20Sopenharmony_ci		if (nwl_phy_link_up(pcie))
2088c2ecf20Sopenharmony_ci			return 0;
2098c2ecf20Sopenharmony_ci		usleep_range(LINK_WAIT_USLEEP_MIN, LINK_WAIT_USLEEP_MAX);
2108c2ecf20Sopenharmony_ci	}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_ci	dev_err(dev, "PHY link never came up\n");
2138c2ecf20Sopenharmony_ci	return -ETIMEDOUT;
2148c2ecf20Sopenharmony_ci}
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_cistatic bool nwl_pcie_valid_device(struct pci_bus *bus, unsigned int devfn)
2178c2ecf20Sopenharmony_ci{
2188c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = bus->sysdata;
2198c2ecf20Sopenharmony_ci
2208c2ecf20Sopenharmony_ci	/* Check link before accessing downstream ports */
2218c2ecf20Sopenharmony_ci	if (!pci_is_root_bus(bus)) {
2228c2ecf20Sopenharmony_ci		if (!nwl_pcie_link_up(pcie))
2238c2ecf20Sopenharmony_ci			return false;
2248c2ecf20Sopenharmony_ci	} else if (devfn > 0)
2258c2ecf20Sopenharmony_ci		/* Only one device down on each root port */
2268c2ecf20Sopenharmony_ci		return false;
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return true;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_ci/**
2328c2ecf20Sopenharmony_ci * nwl_pcie_map_bus - Get configuration base
2338c2ecf20Sopenharmony_ci *
2348c2ecf20Sopenharmony_ci * @bus: Bus structure of current bus
2358c2ecf20Sopenharmony_ci * @devfn: Device/function
2368c2ecf20Sopenharmony_ci * @where: Offset from base
2378c2ecf20Sopenharmony_ci *
2388c2ecf20Sopenharmony_ci * Return: Base address of the configuration space needed to be
2398c2ecf20Sopenharmony_ci *	   accessed.
2408c2ecf20Sopenharmony_ci */
2418c2ecf20Sopenharmony_cistatic void __iomem *nwl_pcie_map_bus(struct pci_bus *bus, unsigned int devfn,
2428c2ecf20Sopenharmony_ci				      int where)
2438c2ecf20Sopenharmony_ci{
2448c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = bus->sysdata;
2458c2ecf20Sopenharmony_ci	int relbus;
2468c2ecf20Sopenharmony_ci
2478c2ecf20Sopenharmony_ci	if (!nwl_pcie_valid_device(bus, devfn))
2488c2ecf20Sopenharmony_ci		return NULL;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	relbus = (bus->number << ECAM_BUS_LOC_SHIFT) |
2518c2ecf20Sopenharmony_ci			(devfn << ECAM_DEV_LOC_SHIFT);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	return pcie->ecam_base + relbus + where;
2548c2ecf20Sopenharmony_ci}
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_ci/* PCIe operations */
2578c2ecf20Sopenharmony_cistatic struct pci_ops nwl_pcie_ops = {
2588c2ecf20Sopenharmony_ci	.map_bus = nwl_pcie_map_bus,
2598c2ecf20Sopenharmony_ci	.read  = pci_generic_config_read,
2608c2ecf20Sopenharmony_ci	.write = pci_generic_config_write,
2618c2ecf20Sopenharmony_ci};
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_cistatic irqreturn_t nwl_pcie_misc_handler(int irq, void *data)
2648c2ecf20Sopenharmony_ci{
2658c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = data;
2668c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
2678c2ecf20Sopenharmony_ci	u32 misc_stat;
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	/* Checking for misc interrupts */
2708c2ecf20Sopenharmony_ci	misc_stat = nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
2718c2ecf20Sopenharmony_ci				     MSGF_MISC_SR_MASKALL;
2728c2ecf20Sopenharmony_ci	if (!misc_stat)
2738c2ecf20Sopenharmony_ci		return IRQ_NONE;
2748c2ecf20Sopenharmony_ci
2758c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_RXMSG_OVER)
2768c2ecf20Sopenharmony_ci		dev_err(dev, "Received Message FIFO Overflow\n");
2778c2ecf20Sopenharmony_ci
2788c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_SLAVE_ERR)
2798c2ecf20Sopenharmony_ci		dev_err(dev, "Slave error\n");
2808c2ecf20Sopenharmony_ci
2818c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_MASTER_ERR)
2828c2ecf20Sopenharmony_ci		dev_err(dev, "Master error\n");
2838c2ecf20Sopenharmony_ci
2848c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_I_ADDR_ERR)
2858c2ecf20Sopenharmony_ci		dev_err(dev, "In Misc Ingress address translation error\n");
2868c2ecf20Sopenharmony_ci
2878c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_E_ADDR_ERR)
2888c2ecf20Sopenharmony_ci		dev_err(dev, "In Misc Egress address translation error\n");
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_FATAL_AER)
2918c2ecf20Sopenharmony_ci		dev_err(dev, "Fatal Error in AER Capability\n");
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_NON_FATAL_AER)
2948c2ecf20Sopenharmony_ci		dev_err(dev, "Non-Fatal Error in AER Capability\n");
2958c2ecf20Sopenharmony_ci
2968c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_CORR_AER)
2978c2ecf20Sopenharmony_ci		dev_err(dev, "Correctable Error in AER Capability\n");
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_UR_DETECT)
3008c2ecf20Sopenharmony_ci		dev_err(dev, "Unsupported request Detected\n");
3018c2ecf20Sopenharmony_ci
3028c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_NON_FATAL_DEV)
3038c2ecf20Sopenharmony_ci		dev_err(dev, "Non-Fatal Error Detected\n");
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MISC_SR_FATAL_DEV)
3068c2ecf20Sopenharmony_ci		dev_err(dev, "Fatal Error Detected\n");
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MSIC_SR_LINK_AUTO_BWIDTH)
3098c2ecf20Sopenharmony_ci		dev_info(dev, "Link Autonomous Bandwidth Management Status bit set\n");
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (misc_stat & MSGF_MSIC_SR_LINK_BWIDTH)
3128c2ecf20Sopenharmony_ci		dev_info(dev, "Link Bandwidth Management Status bit set\n");
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Clear misc interrupt status */
3158c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, misc_stat, MSGF_MISC_STATUS);
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3188c2ecf20Sopenharmony_ci}
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_cistatic void nwl_pcie_leg_handler(struct irq_desc *desc)
3218c2ecf20Sopenharmony_ci{
3228c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
3238c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie;
3248c2ecf20Sopenharmony_ci	unsigned long status;
3258c2ecf20Sopenharmony_ci	u32 bit;
3268c2ecf20Sopenharmony_ci	u32 virq;
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
3298c2ecf20Sopenharmony_ci	pcie = irq_desc_get_handler_data(desc);
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	while ((status = nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
3328c2ecf20Sopenharmony_ci				MSGF_LEG_SR_MASKALL) != 0) {
3338c2ecf20Sopenharmony_ci		for_each_set_bit(bit, &status, PCI_NUM_INTX) {
3348c2ecf20Sopenharmony_ci			virq = irq_find_mapping(pcie->legacy_irq_domain, bit);
3358c2ecf20Sopenharmony_ci			if (virq)
3368c2ecf20Sopenharmony_ci				generic_handle_irq(virq);
3378c2ecf20Sopenharmony_ci		}
3388c2ecf20Sopenharmony_ci	}
3398c2ecf20Sopenharmony_ci
3408c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
3418c2ecf20Sopenharmony_ci}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_cistatic void nwl_pcie_handle_msi_irq(struct nwl_pcie *pcie, u32 status_reg)
3448c2ecf20Sopenharmony_ci{
3458c2ecf20Sopenharmony_ci	struct nwl_msi *msi;
3468c2ecf20Sopenharmony_ci	unsigned long status;
3478c2ecf20Sopenharmony_ci	u32 bit;
3488c2ecf20Sopenharmony_ci	u32 virq;
3498c2ecf20Sopenharmony_ci
3508c2ecf20Sopenharmony_ci	msi = &pcie->msi;
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_ci	while ((status = nwl_bridge_readl(pcie, status_reg)) != 0) {
3538c2ecf20Sopenharmony_ci		for_each_set_bit(bit, &status, 32) {
3548c2ecf20Sopenharmony_ci			nwl_bridge_writel(pcie, 1 << bit, status_reg);
3558c2ecf20Sopenharmony_ci			virq = irq_find_mapping(msi->dev_domain, bit);
3568c2ecf20Sopenharmony_ci			if (virq)
3578c2ecf20Sopenharmony_ci				generic_handle_irq(virq);
3588c2ecf20Sopenharmony_ci		}
3598c2ecf20Sopenharmony_ci	}
3608c2ecf20Sopenharmony_ci}
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_cistatic void nwl_pcie_msi_handler_high(struct irq_desc *desc)
3638c2ecf20Sopenharmony_ci{
3648c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
3658c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
3688c2ecf20Sopenharmony_ci	nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_HI);
3698c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
3708c2ecf20Sopenharmony_ci}
3718c2ecf20Sopenharmony_ci
3728c2ecf20Sopenharmony_cistatic void nwl_pcie_msi_handler_low(struct irq_desc *desc)
3738c2ecf20Sopenharmony_ci{
3748c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
3758c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = irq_desc_get_handler_data(desc);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
3788c2ecf20Sopenharmony_ci	nwl_pcie_handle_msi_irq(pcie, MSGF_MSI_STATUS_LO);
3798c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
3808c2ecf20Sopenharmony_ci}
3818c2ecf20Sopenharmony_ci
3828c2ecf20Sopenharmony_cistatic void nwl_mask_leg_irq(struct irq_data *data)
3838c2ecf20Sopenharmony_ci{
3848c2ecf20Sopenharmony_ci	struct irq_desc *desc = irq_to_desc(data->irq);
3858c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie;
3868c2ecf20Sopenharmony_ci	unsigned long flags;
3878c2ecf20Sopenharmony_ci	u32 mask;
3888c2ecf20Sopenharmony_ci	u32 val;
3898c2ecf20Sopenharmony_ci
3908c2ecf20Sopenharmony_ci	pcie = irq_desc_get_chip_data(desc);
3918c2ecf20Sopenharmony_ci	mask = 1 << (data->hwirq - 1);
3928c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
3938c2ecf20Sopenharmony_ci	val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
3948c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, (val & (~mask)), MSGF_LEG_MASK);
3958c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
3968c2ecf20Sopenharmony_ci}
3978c2ecf20Sopenharmony_ci
3988c2ecf20Sopenharmony_cistatic void nwl_unmask_leg_irq(struct irq_data *data)
3998c2ecf20Sopenharmony_ci{
4008c2ecf20Sopenharmony_ci	struct irq_desc *desc = irq_to_desc(data->irq);
4018c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie;
4028c2ecf20Sopenharmony_ci	unsigned long flags;
4038c2ecf20Sopenharmony_ci	u32 mask;
4048c2ecf20Sopenharmony_ci	u32 val;
4058c2ecf20Sopenharmony_ci
4068c2ecf20Sopenharmony_ci	pcie = irq_desc_get_chip_data(desc);
4078c2ecf20Sopenharmony_ci	mask = 1 << (data->hwirq - 1);
4088c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&pcie->leg_mask_lock, flags);
4098c2ecf20Sopenharmony_ci	val = nwl_bridge_readl(pcie, MSGF_LEG_MASK);
4108c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, (val | mask), MSGF_LEG_MASK);
4118c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&pcie->leg_mask_lock, flags);
4128c2ecf20Sopenharmony_ci}
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_cistatic struct irq_chip nwl_leg_irq_chip = {
4158c2ecf20Sopenharmony_ci	.name = "nwl_pcie:legacy",
4168c2ecf20Sopenharmony_ci	.irq_enable = nwl_unmask_leg_irq,
4178c2ecf20Sopenharmony_ci	.irq_disable = nwl_mask_leg_irq,
4188c2ecf20Sopenharmony_ci	.irq_mask = nwl_mask_leg_irq,
4198c2ecf20Sopenharmony_ci	.irq_unmask = nwl_unmask_leg_irq,
4208c2ecf20Sopenharmony_ci};
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_cistatic int nwl_legacy_map(struct irq_domain *domain, unsigned int irq,
4238c2ecf20Sopenharmony_ci			  irq_hw_number_t hwirq)
4248c2ecf20Sopenharmony_ci{
4258c2ecf20Sopenharmony_ci	irq_set_chip_and_handler(irq, &nwl_leg_irq_chip, handle_level_irq);
4268c2ecf20Sopenharmony_ci	irq_set_chip_data(irq, domain->host_data);
4278c2ecf20Sopenharmony_ci	irq_set_status_flags(irq, IRQ_LEVEL);
4288c2ecf20Sopenharmony_ci
4298c2ecf20Sopenharmony_ci	return 0;
4308c2ecf20Sopenharmony_ci}
4318c2ecf20Sopenharmony_ci
4328c2ecf20Sopenharmony_cistatic const struct irq_domain_ops legacy_domain_ops = {
4338c2ecf20Sopenharmony_ci	.map = nwl_legacy_map,
4348c2ecf20Sopenharmony_ci	.xlate = pci_irqd_intx_xlate,
4358c2ecf20Sopenharmony_ci};
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_MSI
4388c2ecf20Sopenharmony_cistatic struct irq_chip nwl_msi_irq_chip = {
4398c2ecf20Sopenharmony_ci	.name = "nwl_pcie:msi",
4408c2ecf20Sopenharmony_ci	.irq_enable = pci_msi_unmask_irq,
4418c2ecf20Sopenharmony_ci	.irq_disable = pci_msi_mask_irq,
4428c2ecf20Sopenharmony_ci	.irq_mask = pci_msi_mask_irq,
4438c2ecf20Sopenharmony_ci	.irq_unmask = pci_msi_unmask_irq,
4448c2ecf20Sopenharmony_ci};
4458c2ecf20Sopenharmony_ci
4468c2ecf20Sopenharmony_cistatic struct msi_domain_info nwl_msi_domain_info = {
4478c2ecf20Sopenharmony_ci	.flags = (MSI_FLAG_USE_DEF_DOM_OPS | MSI_FLAG_USE_DEF_CHIP_OPS |
4488c2ecf20Sopenharmony_ci		  MSI_FLAG_MULTI_PCI_MSI),
4498c2ecf20Sopenharmony_ci	.chip = &nwl_msi_irq_chip,
4508c2ecf20Sopenharmony_ci};
4518c2ecf20Sopenharmony_ci#endif
4528c2ecf20Sopenharmony_ci
4538c2ecf20Sopenharmony_cistatic void nwl_compose_msi_msg(struct irq_data *data, struct msi_msg *msg)
4548c2ecf20Sopenharmony_ci{
4558c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
4568c2ecf20Sopenharmony_ci	phys_addr_t msi_addr = pcie->phys_pcie_reg_base;
4578c2ecf20Sopenharmony_ci
4588c2ecf20Sopenharmony_ci	msg->address_lo = lower_32_bits(msi_addr);
4598c2ecf20Sopenharmony_ci	msg->address_hi = upper_32_bits(msi_addr);
4608c2ecf20Sopenharmony_ci	msg->data = data->hwirq;
4618c2ecf20Sopenharmony_ci}
4628c2ecf20Sopenharmony_ci
4638c2ecf20Sopenharmony_cistatic int nwl_msi_set_affinity(struct irq_data *irq_data,
4648c2ecf20Sopenharmony_ci				const struct cpumask *mask, bool force)
4658c2ecf20Sopenharmony_ci{
4668c2ecf20Sopenharmony_ci	return -EINVAL;
4678c2ecf20Sopenharmony_ci}
4688c2ecf20Sopenharmony_ci
4698c2ecf20Sopenharmony_cistatic struct irq_chip nwl_irq_chip = {
4708c2ecf20Sopenharmony_ci	.name = "Xilinx MSI",
4718c2ecf20Sopenharmony_ci	.irq_compose_msi_msg = nwl_compose_msi_msg,
4728c2ecf20Sopenharmony_ci	.irq_set_affinity = nwl_msi_set_affinity,
4738c2ecf20Sopenharmony_ci};
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_cistatic int nwl_irq_domain_alloc(struct irq_domain *domain, unsigned int virq,
4768c2ecf20Sopenharmony_ci				unsigned int nr_irqs, void *args)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = domain->host_data;
4798c2ecf20Sopenharmony_ci	struct nwl_msi *msi = &pcie->msi;
4808c2ecf20Sopenharmony_ci	int bit;
4818c2ecf20Sopenharmony_ci	int i;
4828c2ecf20Sopenharmony_ci
4838c2ecf20Sopenharmony_ci	mutex_lock(&msi->lock);
4848c2ecf20Sopenharmony_ci	bit = bitmap_find_free_region(msi->bitmap, INT_PCI_MSI_NR,
4858c2ecf20Sopenharmony_ci				      get_count_order(nr_irqs));
4868c2ecf20Sopenharmony_ci	if (bit < 0) {
4878c2ecf20Sopenharmony_ci		mutex_unlock(&msi->lock);
4888c2ecf20Sopenharmony_ci		return -ENOSPC;
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	for (i = 0; i < nr_irqs; i++) {
4928c2ecf20Sopenharmony_ci		irq_domain_set_info(domain, virq + i, bit + i, &nwl_irq_chip,
4938c2ecf20Sopenharmony_ci				domain->host_data, handle_simple_irq,
4948c2ecf20Sopenharmony_ci				NULL, NULL);
4958c2ecf20Sopenharmony_ci	}
4968c2ecf20Sopenharmony_ci	mutex_unlock(&msi->lock);
4978c2ecf20Sopenharmony_ci	return 0;
4988c2ecf20Sopenharmony_ci}
4998c2ecf20Sopenharmony_ci
5008c2ecf20Sopenharmony_cistatic void nwl_irq_domain_free(struct irq_domain *domain, unsigned int virq,
5018c2ecf20Sopenharmony_ci					unsigned int nr_irqs)
5028c2ecf20Sopenharmony_ci{
5038c2ecf20Sopenharmony_ci	struct irq_data *data = irq_domain_get_irq_data(domain, virq);
5048c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie = irq_data_get_irq_chip_data(data);
5058c2ecf20Sopenharmony_ci	struct nwl_msi *msi = &pcie->msi;
5068c2ecf20Sopenharmony_ci
5078c2ecf20Sopenharmony_ci	mutex_lock(&msi->lock);
5088c2ecf20Sopenharmony_ci	bitmap_release_region(msi->bitmap, data->hwirq,
5098c2ecf20Sopenharmony_ci			      get_count_order(nr_irqs));
5108c2ecf20Sopenharmony_ci	mutex_unlock(&msi->lock);
5118c2ecf20Sopenharmony_ci}
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_cistatic const struct irq_domain_ops dev_msi_domain_ops = {
5148c2ecf20Sopenharmony_ci	.alloc  = nwl_irq_domain_alloc,
5158c2ecf20Sopenharmony_ci	.free   = nwl_irq_domain_free,
5168c2ecf20Sopenharmony_ci};
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_cistatic int nwl_pcie_init_msi_irq_domain(struct nwl_pcie *pcie)
5198c2ecf20Sopenharmony_ci{
5208c2ecf20Sopenharmony_ci#ifdef CONFIG_PCI_MSI
5218c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
5228c2ecf20Sopenharmony_ci	struct fwnode_handle *fwnode = of_node_to_fwnode(dev->of_node);
5238c2ecf20Sopenharmony_ci	struct nwl_msi *msi = &pcie->msi;
5248c2ecf20Sopenharmony_ci
5258c2ecf20Sopenharmony_ci	msi->dev_domain = irq_domain_add_linear(NULL, INT_PCI_MSI_NR,
5268c2ecf20Sopenharmony_ci						&dev_msi_domain_ops, pcie);
5278c2ecf20Sopenharmony_ci	if (!msi->dev_domain) {
5288c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create dev IRQ domain\n");
5298c2ecf20Sopenharmony_ci		return -ENOMEM;
5308c2ecf20Sopenharmony_ci	}
5318c2ecf20Sopenharmony_ci	msi->msi_domain = pci_msi_create_irq_domain(fwnode,
5328c2ecf20Sopenharmony_ci						    &nwl_msi_domain_info,
5338c2ecf20Sopenharmony_ci						    msi->dev_domain);
5348c2ecf20Sopenharmony_ci	if (!msi->msi_domain) {
5358c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create msi IRQ domain\n");
5368c2ecf20Sopenharmony_ci		irq_domain_remove(msi->dev_domain);
5378c2ecf20Sopenharmony_ci		return -ENOMEM;
5388c2ecf20Sopenharmony_ci	}
5398c2ecf20Sopenharmony_ci#endif
5408c2ecf20Sopenharmony_ci	return 0;
5418c2ecf20Sopenharmony_ci}
5428c2ecf20Sopenharmony_ci
5438c2ecf20Sopenharmony_cistatic int nwl_pcie_init_irq_domain(struct nwl_pcie *pcie)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
5468c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
5478c2ecf20Sopenharmony_ci	struct device_node *legacy_intc_node;
5488c2ecf20Sopenharmony_ci
5498c2ecf20Sopenharmony_ci	legacy_intc_node = of_get_next_child(node, NULL);
5508c2ecf20Sopenharmony_ci	if (!legacy_intc_node) {
5518c2ecf20Sopenharmony_ci		dev_err(dev, "No legacy intc node found\n");
5528c2ecf20Sopenharmony_ci		return -EINVAL;
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	pcie->legacy_irq_domain = irq_domain_add_linear(legacy_intc_node,
5568c2ecf20Sopenharmony_ci							PCI_NUM_INTX,
5578c2ecf20Sopenharmony_ci							&legacy_domain_ops,
5588c2ecf20Sopenharmony_ci							pcie);
5598c2ecf20Sopenharmony_ci	of_node_put(legacy_intc_node);
5608c2ecf20Sopenharmony_ci	if (!pcie->legacy_irq_domain) {
5618c2ecf20Sopenharmony_ci		dev_err(dev, "failed to create IRQ domain\n");
5628c2ecf20Sopenharmony_ci		return -ENOMEM;
5638c2ecf20Sopenharmony_ci	}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_ci	raw_spin_lock_init(&pcie->leg_mask_lock);
5668c2ecf20Sopenharmony_ci	nwl_pcie_init_msi_irq_domain(pcie);
5678c2ecf20Sopenharmony_ci	return 0;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic int nwl_pcie_enable_msi(struct nwl_pcie *pcie)
5718c2ecf20Sopenharmony_ci{
5728c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
5738c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
5748c2ecf20Sopenharmony_ci	struct nwl_msi *msi = &pcie->msi;
5758c2ecf20Sopenharmony_ci	unsigned long base;
5768c2ecf20Sopenharmony_ci	int ret;
5778c2ecf20Sopenharmony_ci	int size = BITS_TO_LONGS(INT_PCI_MSI_NR) * sizeof(long);
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci	mutex_init(&msi->lock);
5808c2ecf20Sopenharmony_ci
5818c2ecf20Sopenharmony_ci	msi->bitmap = kzalloc(size, GFP_KERNEL);
5828c2ecf20Sopenharmony_ci	if (!msi->bitmap)
5838c2ecf20Sopenharmony_ci		return -ENOMEM;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	/* Get msi_1 IRQ number */
5868c2ecf20Sopenharmony_ci	msi->irq_msi1 = platform_get_irq_byname(pdev, "msi1");
5878c2ecf20Sopenharmony_ci	if (msi->irq_msi1 < 0) {
5888c2ecf20Sopenharmony_ci		ret = -EINVAL;
5898c2ecf20Sopenharmony_ci		goto err;
5908c2ecf20Sopenharmony_ci	}
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(msi->irq_msi1,
5938c2ecf20Sopenharmony_ci					 nwl_pcie_msi_handler_high, pcie);
5948c2ecf20Sopenharmony_ci
5958c2ecf20Sopenharmony_ci	/* Get msi_0 IRQ number */
5968c2ecf20Sopenharmony_ci	msi->irq_msi0 = platform_get_irq_byname(pdev, "msi0");
5978c2ecf20Sopenharmony_ci	if (msi->irq_msi0 < 0) {
5988c2ecf20Sopenharmony_ci		ret = -EINVAL;
5998c2ecf20Sopenharmony_ci		goto err;
6008c2ecf20Sopenharmony_ci	}
6018c2ecf20Sopenharmony_ci
6028c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(msi->irq_msi0,
6038c2ecf20Sopenharmony_ci					 nwl_pcie_msi_handler_low, pcie);
6048c2ecf20Sopenharmony_ci
6058c2ecf20Sopenharmony_ci	/* Check for msii_present bit */
6068c2ecf20Sopenharmony_ci	ret = nwl_bridge_readl(pcie, I_MSII_CAPABILITIES) & MSII_PRESENT;
6078c2ecf20Sopenharmony_ci	if (!ret) {
6088c2ecf20Sopenharmony_ci		dev_err(dev, "MSI not present\n");
6098c2ecf20Sopenharmony_ci		ret = -EIO;
6108c2ecf20Sopenharmony_ci		goto err;
6118c2ecf20Sopenharmony_ci	}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_ci	/* Enable MSII */
6148c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
6158c2ecf20Sopenharmony_ci			  MSII_ENABLE, I_MSII_CONTROL);
6168c2ecf20Sopenharmony_ci
6178c2ecf20Sopenharmony_ci	/* Enable MSII status */
6188c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, I_MSII_CONTROL) |
6198c2ecf20Sopenharmony_ci			  MSII_STATUS_ENABLE, I_MSII_CONTROL);
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	/* setup AFI/FPCI range */
6228c2ecf20Sopenharmony_ci	base = pcie->phys_pcie_reg_base;
6238c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, lower_32_bits(base), I_MSII_BASE_LO);
6248c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, upper_32_bits(base), I_MSII_BASE_HI);
6258c2ecf20Sopenharmony_ci
6268c2ecf20Sopenharmony_ci	/*
6278c2ecf20Sopenharmony_ci	 * For high range MSI interrupts: disable, clear any pending,
6288c2ecf20Sopenharmony_ci	 * and enable
6298c2ecf20Sopenharmony_ci	 */
6308c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_HI);
6318c2ecf20Sopenharmony_ci
6328c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie,  MSGF_MSI_STATUS_HI) &
6338c2ecf20Sopenharmony_ci			  MSGF_MSI_SR_HI_MASK, MSGF_MSI_STATUS_HI);
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, MSGF_MSI_SR_HI_MASK, MSGF_MSI_MASK_HI);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	/*
6388c2ecf20Sopenharmony_ci	 * For low range MSI interrupts: disable, clear any pending,
6398c2ecf20Sopenharmony_ci	 * and enable
6408c2ecf20Sopenharmony_ci	 */
6418c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, 0, MSGF_MSI_MASK_LO);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MSI_STATUS_LO) &
6448c2ecf20Sopenharmony_ci			  MSGF_MSI_SR_LO_MASK, MSGF_MSI_STATUS_LO);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, MSGF_MSI_SR_LO_MASK, MSGF_MSI_MASK_LO);
6478c2ecf20Sopenharmony_ci
6488c2ecf20Sopenharmony_ci	return 0;
6498c2ecf20Sopenharmony_cierr:
6508c2ecf20Sopenharmony_ci	kfree(msi->bitmap);
6518c2ecf20Sopenharmony_ci	msi->bitmap = NULL;
6528c2ecf20Sopenharmony_ci	return ret;
6538c2ecf20Sopenharmony_ci}
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_cistatic int nwl_pcie_bridge_init(struct nwl_pcie *pcie)
6568c2ecf20Sopenharmony_ci{
6578c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
6588c2ecf20Sopenharmony_ci	struct platform_device *pdev = to_platform_device(dev);
6598c2ecf20Sopenharmony_ci	u32 breg_val, ecam_val, first_busno = 0;
6608c2ecf20Sopenharmony_ci	int err;
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	breg_val = nwl_bridge_readl(pcie, E_BREG_CAPABILITIES) & BREG_PRESENT;
6638c2ecf20Sopenharmony_ci	if (!breg_val) {
6648c2ecf20Sopenharmony_ci		dev_err(dev, "BREG is not present\n");
6658c2ecf20Sopenharmony_ci		return breg_val;
6668c2ecf20Sopenharmony_ci	}
6678c2ecf20Sopenharmony_ci
6688c2ecf20Sopenharmony_ci	/* Write bridge_off to breg base */
6698c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_breg_base),
6708c2ecf20Sopenharmony_ci			  E_BREG_BASE_LO);
6718c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_breg_base),
6728c2ecf20Sopenharmony_ci			  E_BREG_BASE_HI);
6738c2ecf20Sopenharmony_ci
6748c2ecf20Sopenharmony_ci	/* Enable BREG */
6758c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, ~BREG_ENABLE_FORCE & BREG_ENABLE,
6768c2ecf20Sopenharmony_ci			  E_BREG_CONTROL);
6778c2ecf20Sopenharmony_ci
6788c2ecf20Sopenharmony_ci	/* Disable DMA channel registers */
6798c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_PCIE_RX0) |
6808c2ecf20Sopenharmony_ci			  CFG_DMA_REG_BAR, BRCFG_PCIE_RX0);
6818c2ecf20Sopenharmony_ci
6828c2ecf20Sopenharmony_ci	/* Enable Ingress subtractive decode translation */
6838c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, SET_ISUB_CONTROL, I_ISUB_CONTROL);
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	/* Enable msg filtering details */
6868c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, CFG_ENABLE_MSG_FILTER_MASK,
6878c2ecf20Sopenharmony_ci			  BRCFG_PCIE_RX_MSG_FILTER);
6888c2ecf20Sopenharmony_ci
6898c2ecf20Sopenharmony_ci	err = nwl_wait_for_link(pcie);
6908c2ecf20Sopenharmony_ci	if (err)
6918c2ecf20Sopenharmony_ci		return err;
6928c2ecf20Sopenharmony_ci
6938c2ecf20Sopenharmony_ci	ecam_val = nwl_bridge_readl(pcie, E_ECAM_CAPABILITIES) & E_ECAM_PRESENT;
6948c2ecf20Sopenharmony_ci	if (!ecam_val) {
6958c2ecf20Sopenharmony_ci		dev_err(dev, "ECAM is not present\n");
6968c2ecf20Sopenharmony_ci		return ecam_val;
6978c2ecf20Sopenharmony_ci	}
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	/* Enable ECAM */
7008c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
7018c2ecf20Sopenharmony_ci			  E_ECAM_CR_ENABLE, E_ECAM_CONTROL);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, E_ECAM_CONTROL) |
7048c2ecf20Sopenharmony_ci			  (pcie->ecam_value << E_ECAM_SIZE_SHIFT),
7058c2ecf20Sopenharmony_ci			  E_ECAM_CONTROL);
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, lower_32_bits(pcie->phys_ecam_base),
7088c2ecf20Sopenharmony_ci			  E_ECAM_BASE_LO);
7098c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, upper_32_bits(pcie->phys_ecam_base),
7108c2ecf20Sopenharmony_ci			  E_ECAM_BASE_HI);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/* Get bus range */
7138c2ecf20Sopenharmony_ci	ecam_val = nwl_bridge_readl(pcie, E_ECAM_CONTROL);
7148c2ecf20Sopenharmony_ci	pcie->last_busno = (ecam_val & E_ECAM_SIZE_LOC) >> E_ECAM_SIZE_SHIFT;
7158c2ecf20Sopenharmony_ci	/* Write primary, secondary and subordinate bus numbers */
7168c2ecf20Sopenharmony_ci	ecam_val = first_busno;
7178c2ecf20Sopenharmony_ci	ecam_val |= (first_busno + 1) << 8;
7188c2ecf20Sopenharmony_ci	ecam_val |= (pcie->last_busno << E_ECAM_SIZE_SHIFT);
7198c2ecf20Sopenharmony_ci	writel(ecam_val, (pcie->ecam_base + PCI_PRIMARY_BUS));
7208c2ecf20Sopenharmony_ci
7218c2ecf20Sopenharmony_ci	if (nwl_pcie_link_up(pcie))
7228c2ecf20Sopenharmony_ci		dev_info(dev, "Link is UP\n");
7238c2ecf20Sopenharmony_ci	else
7248c2ecf20Sopenharmony_ci		dev_info(dev, "Link is DOWN\n");
7258c2ecf20Sopenharmony_ci
7268c2ecf20Sopenharmony_ci	/* Get misc IRQ number */
7278c2ecf20Sopenharmony_ci	pcie->irq_misc = platform_get_irq_byname(pdev, "misc");
7288c2ecf20Sopenharmony_ci	if (pcie->irq_misc < 0)
7298c2ecf20Sopenharmony_ci		return -EINVAL;
7308c2ecf20Sopenharmony_ci
7318c2ecf20Sopenharmony_ci	err = devm_request_irq(dev, pcie->irq_misc,
7328c2ecf20Sopenharmony_ci			       nwl_pcie_misc_handler, IRQF_SHARED,
7338c2ecf20Sopenharmony_ci			       "nwl_pcie:misc", pcie);
7348c2ecf20Sopenharmony_ci	if (err) {
7358c2ecf20Sopenharmony_ci		dev_err(dev, "fail to register misc IRQ#%d\n",
7368c2ecf20Sopenharmony_ci			pcie->irq_misc);
7378c2ecf20Sopenharmony_ci		return err;
7388c2ecf20Sopenharmony_ci	}
7398c2ecf20Sopenharmony_ci
7408c2ecf20Sopenharmony_ci	/* Disable all misc interrupts */
7418c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, (u32)~MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
7428c2ecf20Sopenharmony_ci
7438c2ecf20Sopenharmony_ci	/* Clear pending misc interrupts */
7448c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_MISC_STATUS) &
7458c2ecf20Sopenharmony_ci			  MSGF_MISC_SR_MASKALL, MSGF_MISC_STATUS);
7468c2ecf20Sopenharmony_ci
7478c2ecf20Sopenharmony_ci	/* Enable all misc interrupts */
7488c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, MSGF_MISC_SR_MASKALL, MSGF_MISC_MASK);
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	/* Disable all legacy interrupts */
7528c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, (u32)~MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
7538c2ecf20Sopenharmony_ci
7548c2ecf20Sopenharmony_ci	/* Clear pending legacy interrupts */
7558c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, MSGF_LEG_STATUS) &
7568c2ecf20Sopenharmony_ci			  MSGF_LEG_SR_MASKALL, MSGF_LEG_STATUS);
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_ci	/* Enable all legacy interrupts */
7598c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, MSGF_LEG_SR_MASKALL, MSGF_LEG_MASK);
7608c2ecf20Sopenharmony_ci
7618c2ecf20Sopenharmony_ci	/* Enable the bridge config interrupt */
7628c2ecf20Sopenharmony_ci	nwl_bridge_writel(pcie, nwl_bridge_readl(pcie, BRCFG_INTERRUPT) |
7638c2ecf20Sopenharmony_ci			  BRCFG_INTERRUPT_MASK, BRCFG_INTERRUPT);
7648c2ecf20Sopenharmony_ci
7658c2ecf20Sopenharmony_ci	return 0;
7668c2ecf20Sopenharmony_ci}
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_cistatic int nwl_pcie_parse_dt(struct nwl_pcie *pcie,
7698c2ecf20Sopenharmony_ci			     struct platform_device *pdev)
7708c2ecf20Sopenharmony_ci{
7718c2ecf20Sopenharmony_ci	struct device *dev = pcie->dev;
7728c2ecf20Sopenharmony_ci	struct resource *res;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "breg");
7758c2ecf20Sopenharmony_ci	pcie->breg_base = devm_ioremap_resource(dev, res);
7768c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->breg_base))
7778c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->breg_base);
7788c2ecf20Sopenharmony_ci	pcie->phys_breg_base = res->start;
7798c2ecf20Sopenharmony_ci
7808c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "pcireg");
7818c2ecf20Sopenharmony_ci	pcie->pcireg_base = devm_ioremap_resource(dev, res);
7828c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->pcireg_base))
7838c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->pcireg_base);
7848c2ecf20Sopenharmony_ci	pcie->phys_pcie_reg_base = res->start;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "cfg");
7878c2ecf20Sopenharmony_ci	pcie->ecam_base = devm_pci_remap_cfg_resource(dev, res);
7888c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->ecam_base))
7898c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->ecam_base);
7908c2ecf20Sopenharmony_ci	pcie->phys_ecam_base = res->start;
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_ci	/* Get intx IRQ number */
7938c2ecf20Sopenharmony_ci	pcie->irq_intx = platform_get_irq_byname(pdev, "intx");
7948c2ecf20Sopenharmony_ci	if (pcie->irq_intx < 0)
7958c2ecf20Sopenharmony_ci		return pcie->irq_intx;
7968c2ecf20Sopenharmony_ci
7978c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(pcie->irq_intx,
7988c2ecf20Sopenharmony_ci					 nwl_pcie_leg_handler, pcie);
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	return 0;
8018c2ecf20Sopenharmony_ci}
8028c2ecf20Sopenharmony_ci
8038c2ecf20Sopenharmony_cistatic const struct of_device_id nwl_pcie_of_match[] = {
8048c2ecf20Sopenharmony_ci	{ .compatible = "xlnx,nwl-pcie-2.11", },
8058c2ecf20Sopenharmony_ci	{}
8068c2ecf20Sopenharmony_ci};
8078c2ecf20Sopenharmony_ci
8088c2ecf20Sopenharmony_cistatic int nwl_pcie_probe(struct platform_device *pdev)
8098c2ecf20Sopenharmony_ci{
8108c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
8118c2ecf20Sopenharmony_ci	struct nwl_pcie *pcie;
8128c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge;
8138c2ecf20Sopenharmony_ci	int err;
8148c2ecf20Sopenharmony_ci
8158c2ecf20Sopenharmony_ci	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
8168c2ecf20Sopenharmony_ci	if (!bridge)
8178c2ecf20Sopenharmony_ci		return -ENODEV;
8188c2ecf20Sopenharmony_ci
8198c2ecf20Sopenharmony_ci	pcie = pci_host_bridge_priv(bridge);
8208c2ecf20Sopenharmony_ci
8218c2ecf20Sopenharmony_ci	pcie->dev = dev;
8228c2ecf20Sopenharmony_ci	pcie->ecam_value = NWL_ECAM_VALUE_DEFAULT;
8238c2ecf20Sopenharmony_ci
8248c2ecf20Sopenharmony_ci	err = nwl_pcie_parse_dt(pcie, pdev);
8258c2ecf20Sopenharmony_ci	if (err) {
8268c2ecf20Sopenharmony_ci		dev_err(dev, "Parsing DT failed\n");
8278c2ecf20Sopenharmony_ci		return err;
8288c2ecf20Sopenharmony_ci	}
8298c2ecf20Sopenharmony_ci
8308c2ecf20Sopenharmony_ci	pcie->clk = devm_clk_get(dev, NULL);
8318c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->clk))
8328c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->clk);
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ci	err = clk_prepare_enable(pcie->clk);
8358c2ecf20Sopenharmony_ci	if (err) {
8368c2ecf20Sopenharmony_ci		dev_err(dev, "can't enable PCIe ref clock\n");
8378c2ecf20Sopenharmony_ci		return err;
8388c2ecf20Sopenharmony_ci	}
8398c2ecf20Sopenharmony_ci
8408c2ecf20Sopenharmony_ci	err = nwl_pcie_bridge_init(pcie);
8418c2ecf20Sopenharmony_ci	if (err) {
8428c2ecf20Sopenharmony_ci		dev_err(dev, "HW Initialization failed\n");
8438c2ecf20Sopenharmony_ci		return err;
8448c2ecf20Sopenharmony_ci	}
8458c2ecf20Sopenharmony_ci
8468c2ecf20Sopenharmony_ci	err = nwl_pcie_init_irq_domain(pcie);
8478c2ecf20Sopenharmony_ci	if (err) {
8488c2ecf20Sopenharmony_ci		dev_err(dev, "Failed creating IRQ Domain\n");
8498c2ecf20Sopenharmony_ci		return err;
8508c2ecf20Sopenharmony_ci	}
8518c2ecf20Sopenharmony_ci
8528c2ecf20Sopenharmony_ci	bridge->sysdata = pcie;
8538c2ecf20Sopenharmony_ci	bridge->ops = &nwl_pcie_ops;
8548c2ecf20Sopenharmony_ci
8558c2ecf20Sopenharmony_ci	if (IS_ENABLED(CONFIG_PCI_MSI)) {
8568c2ecf20Sopenharmony_ci		err = nwl_pcie_enable_msi(pcie);
8578c2ecf20Sopenharmony_ci		if (err < 0) {
8588c2ecf20Sopenharmony_ci			dev_err(dev, "failed to enable MSI support: %d\n", err);
8598c2ecf20Sopenharmony_ci			return err;
8608c2ecf20Sopenharmony_ci		}
8618c2ecf20Sopenharmony_ci	}
8628c2ecf20Sopenharmony_ci
8638c2ecf20Sopenharmony_ci	return pci_host_probe(bridge);
8648c2ecf20Sopenharmony_ci}
8658c2ecf20Sopenharmony_ci
8668c2ecf20Sopenharmony_cistatic struct platform_driver nwl_pcie_driver = {
8678c2ecf20Sopenharmony_ci	.driver = {
8688c2ecf20Sopenharmony_ci		.name = "nwl-pcie",
8698c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
8708c2ecf20Sopenharmony_ci		.of_match_table = nwl_pcie_of_match,
8718c2ecf20Sopenharmony_ci	},
8728c2ecf20Sopenharmony_ci	.probe = nwl_pcie_probe,
8738c2ecf20Sopenharmony_ci};
8748c2ecf20Sopenharmony_cibuiltin_platform_driver(nwl_pcie_driver);
875