18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * Copyright Altera Corporation (C) 2013-2015. All rights reserved
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Author: Ley Foon Tan <lftan@altera.com>
68c2ecf20Sopenharmony_ci * Description: Altera PCIe host controller driver
78c2ecf20Sopenharmony_ci */
88c2ecf20Sopenharmony_ci
98c2ecf20Sopenharmony_ci#include <linux/delay.h>
108c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
118c2ecf20Sopenharmony_ci#include <linux/irqchip/chained_irq.h>
128c2ecf20Sopenharmony_ci#include <linux/init.h>
138c2ecf20Sopenharmony_ci#include <linux/module.h>
148c2ecf20Sopenharmony_ci#include <linux/of_address.h>
158c2ecf20Sopenharmony_ci#include <linux/of_device.h>
168c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
178c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
188c2ecf20Sopenharmony_ci#include <linux/pci.h>
198c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
208c2ecf20Sopenharmony_ci#include <linux/slab.h>
218c2ecf20Sopenharmony_ci
228c2ecf20Sopenharmony_ci#include "../pci.h"
238c2ecf20Sopenharmony_ci
248c2ecf20Sopenharmony_ci#define RP_TX_REG0			0x2000
258c2ecf20Sopenharmony_ci#define RP_TX_REG1			0x2004
268c2ecf20Sopenharmony_ci#define RP_TX_CNTRL			0x2008
278c2ecf20Sopenharmony_ci#define RP_TX_EOP			0x2
288c2ecf20Sopenharmony_ci#define RP_TX_SOP			0x1
298c2ecf20Sopenharmony_ci#define RP_RXCPL_STATUS			0x2010
308c2ecf20Sopenharmony_ci#define RP_RXCPL_EOP			0x2
318c2ecf20Sopenharmony_ci#define RP_RXCPL_SOP			0x1
328c2ecf20Sopenharmony_ci#define RP_RXCPL_REG0			0x2014
338c2ecf20Sopenharmony_ci#define RP_RXCPL_REG1			0x2018
348c2ecf20Sopenharmony_ci#define P2A_INT_STATUS			0x3060
358c2ecf20Sopenharmony_ci#define P2A_INT_STS_ALL			0xf
368c2ecf20Sopenharmony_ci#define P2A_INT_ENABLE			0x3070
378c2ecf20Sopenharmony_ci#define P2A_INT_ENA_ALL			0xf
388c2ecf20Sopenharmony_ci#define RP_LTSSM			0x3c64
398c2ecf20Sopenharmony_ci#define RP_LTSSM_MASK			0x1f
408c2ecf20Sopenharmony_ci#define LTSSM_L0			0xf
418c2ecf20Sopenharmony_ci
428c2ecf20Sopenharmony_ci#define S10_RP_TX_CNTRL			0x2004
438c2ecf20Sopenharmony_ci#define S10_RP_RXCPL_REG		0x2008
448c2ecf20Sopenharmony_ci#define S10_RP_RXCPL_STATUS		0x200C
458c2ecf20Sopenharmony_ci#define S10_RP_CFG_ADDR(pcie, reg)	\
468c2ecf20Sopenharmony_ci	(((pcie)->hip_base) + (reg) + (1 << 20))
478c2ecf20Sopenharmony_ci#define S10_RP_SECONDARY(pcie)		\
488c2ecf20Sopenharmony_ci	readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS))
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci/* TLP configuration type 0 and 1 */
518c2ecf20Sopenharmony_ci#define TLP_FMTTYPE_CFGRD0		0x04	/* Configuration Read Type 0 */
528c2ecf20Sopenharmony_ci#define TLP_FMTTYPE_CFGWR0		0x44	/* Configuration Write Type 0 */
538c2ecf20Sopenharmony_ci#define TLP_FMTTYPE_CFGRD1		0x05	/* Configuration Read Type 1 */
548c2ecf20Sopenharmony_ci#define TLP_FMTTYPE_CFGWR1		0x45	/* Configuration Write Type 1 */
558c2ecf20Sopenharmony_ci#define TLP_PAYLOAD_SIZE		0x01
568c2ecf20Sopenharmony_ci#define TLP_READ_TAG			0x1d
578c2ecf20Sopenharmony_ci#define TLP_WRITE_TAG			0x10
588c2ecf20Sopenharmony_ci#define RP_DEVFN			0
598c2ecf20Sopenharmony_ci#define TLP_REQ_ID(bus, devfn)		(((bus) << 8) | (devfn))
608c2ecf20Sopenharmony_ci#define TLP_CFG_DW0(pcie, cfg)		\
618c2ecf20Sopenharmony_ci		(((cfg) << 24) |	\
628c2ecf20Sopenharmony_ci		  TLP_PAYLOAD_SIZE)
638c2ecf20Sopenharmony_ci#define TLP_CFG_DW1(pcie, tag, be)	\
648c2ecf20Sopenharmony_ci	(((TLP_REQ_ID(pcie->root_bus_nr,  RP_DEVFN)) << 16) | (tag << 8) | (be))
658c2ecf20Sopenharmony_ci#define TLP_CFG_DW2(bus, devfn, offset)	\
668c2ecf20Sopenharmony_ci				(((bus) << 24) | ((devfn) << 16) | (offset))
678c2ecf20Sopenharmony_ci#define TLP_COMP_STATUS(s)		(((s) >> 13) & 7)
688c2ecf20Sopenharmony_ci#define TLP_BYTE_COUNT(s)		(((s) >> 0) & 0xfff)
698c2ecf20Sopenharmony_ci#define TLP_HDR_SIZE			3
708c2ecf20Sopenharmony_ci#define TLP_LOOP			500
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define LINK_UP_TIMEOUT			HZ
738c2ecf20Sopenharmony_ci#define LINK_RETRAIN_TIMEOUT		HZ
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define DWORD_MASK			3
768c2ecf20Sopenharmony_ci
778c2ecf20Sopenharmony_ci#define S10_TLP_FMTTYPE_CFGRD0		0x05
788c2ecf20Sopenharmony_ci#define S10_TLP_FMTTYPE_CFGRD1		0x04
798c2ecf20Sopenharmony_ci#define S10_TLP_FMTTYPE_CFGWR0		0x45
808c2ecf20Sopenharmony_ci#define S10_TLP_FMTTYPE_CFGWR1		0x44
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_cienum altera_pcie_version {
838c2ecf20Sopenharmony_ci	ALTERA_PCIE_V1 = 0,
848c2ecf20Sopenharmony_ci	ALTERA_PCIE_V2,
858c2ecf20Sopenharmony_ci};
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct altera_pcie {
888c2ecf20Sopenharmony_ci	struct platform_device	*pdev;
898c2ecf20Sopenharmony_ci	void __iomem		*cra_base;
908c2ecf20Sopenharmony_ci	void __iomem		*hip_base;
918c2ecf20Sopenharmony_ci	int			irq;
928c2ecf20Sopenharmony_ci	u8			root_bus_nr;
938c2ecf20Sopenharmony_ci	struct irq_domain	*irq_domain;
948c2ecf20Sopenharmony_ci	struct resource		bus_range;
958c2ecf20Sopenharmony_ci	const struct altera_pcie_data	*pcie_data;
968c2ecf20Sopenharmony_ci};
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistruct altera_pcie_ops {
998c2ecf20Sopenharmony_ci	int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value);
1008c2ecf20Sopenharmony_ci	void (*tlp_write_pkt)(struct altera_pcie *pcie, u32 *headers,
1018c2ecf20Sopenharmony_ci			      u32 data, bool align);
1028c2ecf20Sopenharmony_ci	bool (*get_link_status)(struct altera_pcie *pcie);
1038c2ecf20Sopenharmony_ci	int (*rp_read_cfg)(struct altera_pcie *pcie, int where,
1048c2ecf20Sopenharmony_ci			   int size, u32 *value);
1058c2ecf20Sopenharmony_ci	int (*rp_write_cfg)(struct altera_pcie *pcie, u8 busno,
1068c2ecf20Sopenharmony_ci			    int where, int size, u32 value);
1078c2ecf20Sopenharmony_ci};
1088c2ecf20Sopenharmony_ci
1098c2ecf20Sopenharmony_cistruct altera_pcie_data {
1108c2ecf20Sopenharmony_ci	const struct altera_pcie_ops *ops;
1118c2ecf20Sopenharmony_ci	enum altera_pcie_version version;
1128c2ecf20Sopenharmony_ci	u32 cap_offset;		/* PCIe capability structure register offset */
1138c2ecf20Sopenharmony_ci	u32 cfgrd0;
1148c2ecf20Sopenharmony_ci	u32 cfgrd1;
1158c2ecf20Sopenharmony_ci	u32 cfgwr0;
1168c2ecf20Sopenharmony_ci	u32 cfgwr1;
1178c2ecf20Sopenharmony_ci};
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistruct tlp_rp_regpair_t {
1208c2ecf20Sopenharmony_ci	u32 ctrl;
1218c2ecf20Sopenharmony_ci	u32 reg0;
1228c2ecf20Sopenharmony_ci	u32 reg1;
1238c2ecf20Sopenharmony_ci};
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_cistatic inline void cra_writel(struct altera_pcie *pcie, const u32 value,
1268c2ecf20Sopenharmony_ci			      const u32 reg)
1278c2ecf20Sopenharmony_ci{
1288c2ecf20Sopenharmony_ci	writel_relaxed(value, pcie->cra_base + reg);
1298c2ecf20Sopenharmony_ci}
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_cistatic inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg)
1328c2ecf20Sopenharmony_ci{
1338c2ecf20Sopenharmony_ci	return readl_relaxed(pcie->cra_base + reg);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_cistatic bool altera_pcie_link_up(struct altera_pcie *pcie)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0);
1398c2ecf20Sopenharmony_ci}
1408c2ecf20Sopenharmony_ci
1418c2ecf20Sopenharmony_cistatic bool s10_altera_pcie_link_up(struct altera_pcie *pcie)
1428c2ecf20Sopenharmony_ci{
1438c2ecf20Sopenharmony_ci	void __iomem *addr = S10_RP_CFG_ADDR(pcie,
1448c2ecf20Sopenharmony_ci				   pcie->pcie_data->cap_offset +
1458c2ecf20Sopenharmony_ci				   PCI_EXP_LNKSTA);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	return !!(readw(addr) & PCI_EXP_LNKSTA_DLLLA);
1488c2ecf20Sopenharmony_ci}
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci/*
1518c2ecf20Sopenharmony_ci * Altera PCIe port uses BAR0 of RC's configuration space as the translation
1528c2ecf20Sopenharmony_ci * from PCI bus to native BUS.  Entire DDR region is mapped into PCIe space
1538c2ecf20Sopenharmony_ci * using these registers, so it can be reached by DMA from EP devices.
1548c2ecf20Sopenharmony_ci * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt
1558c2ecf20Sopenharmony_ci * from EP devices, eventually trigger interrupt to GIC.  The BAR0 of bridge
1568c2ecf20Sopenharmony_ci * should be hidden during enumeration to avoid the sizing and resource
1578c2ecf20Sopenharmony_ci * allocation by PCIe core.
1588c2ecf20Sopenharmony_ci */
1598c2ecf20Sopenharmony_cistatic bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int  devfn,
1608c2ecf20Sopenharmony_ci				    int offset)
1618c2ecf20Sopenharmony_ci{
1628c2ecf20Sopenharmony_ci	if (pci_is_root_bus(bus) && (devfn == 0) &&
1638c2ecf20Sopenharmony_ci	    (offset == PCI_BASE_ADDRESS_0))
1648c2ecf20Sopenharmony_ci		return true;
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	return false;
1678c2ecf20Sopenharmony_ci}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_cistatic void tlp_write_tx(struct altera_pcie *pcie,
1708c2ecf20Sopenharmony_ci			 struct tlp_rp_regpair_t *tlp_rp_regdata)
1718c2ecf20Sopenharmony_ci{
1728c2ecf20Sopenharmony_ci	cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0);
1738c2ecf20Sopenharmony_ci	cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1);
1748c2ecf20Sopenharmony_ci	cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL);
1758c2ecf20Sopenharmony_ci}
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_cistatic void s10_tlp_write_tx(struct altera_pcie *pcie, u32 reg0, u32 ctrl)
1788c2ecf20Sopenharmony_ci{
1798c2ecf20Sopenharmony_ci	cra_writel(pcie, reg0, RP_TX_REG0);
1808c2ecf20Sopenharmony_ci	cra_writel(pcie, ctrl, S10_RP_TX_CNTRL);
1818c2ecf20Sopenharmony_ci}
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_cistatic bool altera_pcie_valid_device(struct altera_pcie *pcie,
1848c2ecf20Sopenharmony_ci				     struct pci_bus *bus, int dev)
1858c2ecf20Sopenharmony_ci{
1868c2ecf20Sopenharmony_ci	/* If there is no link, then there is no device */
1878c2ecf20Sopenharmony_ci	if (bus->number != pcie->root_bus_nr) {
1888c2ecf20Sopenharmony_ci		if (!pcie->pcie_data->ops->get_link_status(pcie))
1898c2ecf20Sopenharmony_ci			return false;
1908c2ecf20Sopenharmony_ci	}
1918c2ecf20Sopenharmony_ci
1928c2ecf20Sopenharmony_ci	/* access only one slot on each root port */
1938c2ecf20Sopenharmony_ci	if (bus->number == pcie->root_bus_nr && dev > 0)
1948c2ecf20Sopenharmony_ci		return false;
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	return true;
1978c2ecf20Sopenharmony_ci}
1988c2ecf20Sopenharmony_ci
1998c2ecf20Sopenharmony_cistatic int tlp_read_packet(struct altera_pcie *pcie, u32 *value)
2008c2ecf20Sopenharmony_ci{
2018c2ecf20Sopenharmony_ci	int i;
2028c2ecf20Sopenharmony_ci	bool sop = false;
2038c2ecf20Sopenharmony_ci	u32 ctrl;
2048c2ecf20Sopenharmony_ci	u32 reg0, reg1;
2058c2ecf20Sopenharmony_ci	u32 comp_status = 1;
2068c2ecf20Sopenharmony_ci
2078c2ecf20Sopenharmony_ci	/*
2088c2ecf20Sopenharmony_ci	 * Minimum 2 loops to read TLP headers and 1 loop to read data
2098c2ecf20Sopenharmony_ci	 * payload.
2108c2ecf20Sopenharmony_ci	 */
2118c2ecf20Sopenharmony_ci	for (i = 0; i < TLP_LOOP; i++) {
2128c2ecf20Sopenharmony_ci		ctrl = cra_readl(pcie, RP_RXCPL_STATUS);
2138c2ecf20Sopenharmony_ci		if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) {
2148c2ecf20Sopenharmony_ci			reg0 = cra_readl(pcie, RP_RXCPL_REG0);
2158c2ecf20Sopenharmony_ci			reg1 = cra_readl(pcie, RP_RXCPL_REG1);
2168c2ecf20Sopenharmony_ci
2178c2ecf20Sopenharmony_ci			if (ctrl & RP_RXCPL_SOP) {
2188c2ecf20Sopenharmony_ci				sop = true;
2198c2ecf20Sopenharmony_ci				comp_status = TLP_COMP_STATUS(reg1);
2208c2ecf20Sopenharmony_ci			}
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci			if (ctrl & RP_RXCPL_EOP) {
2238c2ecf20Sopenharmony_ci				if (comp_status)
2248c2ecf20Sopenharmony_ci					return PCIBIOS_DEVICE_NOT_FOUND;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci				if (value)
2278c2ecf20Sopenharmony_ci					*value = reg0;
2288c2ecf20Sopenharmony_ci
2298c2ecf20Sopenharmony_ci				return PCIBIOS_SUCCESSFUL;
2308c2ecf20Sopenharmony_ci			}
2318c2ecf20Sopenharmony_ci		}
2328c2ecf20Sopenharmony_ci		udelay(5);
2338c2ecf20Sopenharmony_ci	}
2348c2ecf20Sopenharmony_ci
2358c2ecf20Sopenharmony_ci	return PCIBIOS_DEVICE_NOT_FOUND;
2368c2ecf20Sopenharmony_ci}
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_cistatic int s10_tlp_read_packet(struct altera_pcie *pcie, u32 *value)
2398c2ecf20Sopenharmony_ci{
2408c2ecf20Sopenharmony_ci	u32 ctrl;
2418c2ecf20Sopenharmony_ci	u32 comp_status;
2428c2ecf20Sopenharmony_ci	u32 dw[4];
2438c2ecf20Sopenharmony_ci	u32 count;
2448c2ecf20Sopenharmony_ci	struct device *dev = &pcie->pdev->dev;
2458c2ecf20Sopenharmony_ci
2468c2ecf20Sopenharmony_ci	for (count = 0; count < TLP_LOOP; count++) {
2478c2ecf20Sopenharmony_ci		ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
2488c2ecf20Sopenharmony_ci		if (ctrl & RP_RXCPL_SOP) {
2498c2ecf20Sopenharmony_ci			/* Read first DW */
2508c2ecf20Sopenharmony_ci			dw[0] = cra_readl(pcie, S10_RP_RXCPL_REG);
2518c2ecf20Sopenharmony_ci			break;
2528c2ecf20Sopenharmony_ci		}
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci		udelay(5);
2558c2ecf20Sopenharmony_ci	}
2568c2ecf20Sopenharmony_ci
2578c2ecf20Sopenharmony_ci	/* SOP detection failed, return error */
2588c2ecf20Sopenharmony_ci	if (count == TLP_LOOP)
2598c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	count = 1;
2628c2ecf20Sopenharmony_ci
2638c2ecf20Sopenharmony_ci	/* Poll for EOP */
2648c2ecf20Sopenharmony_ci	while (count < ARRAY_SIZE(dw)) {
2658c2ecf20Sopenharmony_ci		ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS);
2668c2ecf20Sopenharmony_ci		dw[count++] = cra_readl(pcie, S10_RP_RXCPL_REG);
2678c2ecf20Sopenharmony_ci		if (ctrl & RP_RXCPL_EOP) {
2688c2ecf20Sopenharmony_ci			comp_status = TLP_COMP_STATUS(dw[1]);
2698c2ecf20Sopenharmony_ci			if (comp_status)
2708c2ecf20Sopenharmony_ci				return PCIBIOS_DEVICE_NOT_FOUND;
2718c2ecf20Sopenharmony_ci
2728c2ecf20Sopenharmony_ci			if (value && TLP_BYTE_COUNT(dw[1]) == sizeof(u32) &&
2738c2ecf20Sopenharmony_ci			    count == 4)
2748c2ecf20Sopenharmony_ci				*value = dw[3];
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci			return PCIBIOS_SUCCESSFUL;
2778c2ecf20Sopenharmony_ci		}
2788c2ecf20Sopenharmony_ci	}
2798c2ecf20Sopenharmony_ci
2808c2ecf20Sopenharmony_ci	dev_warn(dev, "Malformed TLP packet\n");
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	return PCIBIOS_DEVICE_NOT_FOUND;
2838c2ecf20Sopenharmony_ci}
2848c2ecf20Sopenharmony_ci
2858c2ecf20Sopenharmony_cistatic void tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
2868c2ecf20Sopenharmony_ci			     u32 data, bool align)
2878c2ecf20Sopenharmony_ci{
2888c2ecf20Sopenharmony_ci	struct tlp_rp_regpair_t tlp_rp_regdata;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	tlp_rp_regdata.reg0 = headers[0];
2918c2ecf20Sopenharmony_ci	tlp_rp_regdata.reg1 = headers[1];
2928c2ecf20Sopenharmony_ci	tlp_rp_regdata.ctrl = RP_TX_SOP;
2938c2ecf20Sopenharmony_ci	tlp_write_tx(pcie, &tlp_rp_regdata);
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (align) {
2968c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg0 = headers[2];
2978c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg1 = 0;
2988c2ecf20Sopenharmony_ci		tlp_rp_regdata.ctrl = 0;
2998c2ecf20Sopenharmony_ci		tlp_write_tx(pcie, &tlp_rp_regdata);
3008c2ecf20Sopenharmony_ci
3018c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg0 = data;
3028c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg1 = 0;
3038c2ecf20Sopenharmony_ci	} else {
3048c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg0 = headers[2];
3058c2ecf20Sopenharmony_ci		tlp_rp_regdata.reg1 = data;
3068c2ecf20Sopenharmony_ci	}
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	tlp_rp_regdata.ctrl = RP_TX_EOP;
3098c2ecf20Sopenharmony_ci	tlp_write_tx(pcie, &tlp_rp_regdata);
3108c2ecf20Sopenharmony_ci}
3118c2ecf20Sopenharmony_ci
3128c2ecf20Sopenharmony_cistatic void s10_tlp_write_packet(struct altera_pcie *pcie, u32 *headers,
3138c2ecf20Sopenharmony_ci				 u32 data, bool dummy)
3148c2ecf20Sopenharmony_ci{
3158c2ecf20Sopenharmony_ci	s10_tlp_write_tx(pcie, headers[0], RP_TX_SOP);
3168c2ecf20Sopenharmony_ci	s10_tlp_write_tx(pcie, headers[1], 0);
3178c2ecf20Sopenharmony_ci	s10_tlp_write_tx(pcie, headers[2], 0);
3188c2ecf20Sopenharmony_ci	s10_tlp_write_tx(pcie, data, RP_TX_EOP);
3198c2ecf20Sopenharmony_ci}
3208c2ecf20Sopenharmony_ci
3218c2ecf20Sopenharmony_cistatic void get_tlp_header(struct altera_pcie *pcie, u8 bus, u32 devfn,
3228c2ecf20Sopenharmony_ci			   int where, u8 byte_en, bool read, u32 *headers)
3238c2ecf20Sopenharmony_ci{
3248c2ecf20Sopenharmony_ci	u8 cfg;
3258c2ecf20Sopenharmony_ci	u8 cfg0 = read ? pcie->pcie_data->cfgrd0 : pcie->pcie_data->cfgwr0;
3268c2ecf20Sopenharmony_ci	u8 cfg1 = read ? pcie->pcie_data->cfgrd1 : pcie->pcie_data->cfgwr1;
3278c2ecf20Sopenharmony_ci	u8 tag = read ? TLP_READ_TAG : TLP_WRITE_TAG;
3288c2ecf20Sopenharmony_ci
3298c2ecf20Sopenharmony_ci	if (pcie->pcie_data->version == ALTERA_PCIE_V1)
3308c2ecf20Sopenharmony_ci		cfg = (bus == pcie->root_bus_nr) ? cfg0 : cfg1;
3318c2ecf20Sopenharmony_ci	else
3328c2ecf20Sopenharmony_ci		cfg = (bus > S10_RP_SECONDARY(pcie)) ? cfg0 : cfg1;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	headers[0] = TLP_CFG_DW0(pcie, cfg);
3358c2ecf20Sopenharmony_ci	headers[1] = TLP_CFG_DW1(pcie, tag, byte_en);
3368c2ecf20Sopenharmony_ci	headers[2] = TLP_CFG_DW2(bus, devfn, where);
3378c2ecf20Sopenharmony_ci}
3388c2ecf20Sopenharmony_ci
3398c2ecf20Sopenharmony_cistatic int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn,
3408c2ecf20Sopenharmony_ci			      int where, u8 byte_en, u32 *value)
3418c2ecf20Sopenharmony_ci{
3428c2ecf20Sopenharmony_ci	u32 headers[TLP_HDR_SIZE];
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_ci	get_tlp_header(pcie, bus, devfn, where, byte_en, true,
3458c2ecf20Sopenharmony_ci		       headers);
3468c2ecf20Sopenharmony_ci
3478c2ecf20Sopenharmony_ci	pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, 0, false);
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	return pcie->pcie_data->ops->tlp_read_pkt(pcie, value);
3508c2ecf20Sopenharmony_ci}
3518c2ecf20Sopenharmony_ci
3528c2ecf20Sopenharmony_cistatic int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn,
3538c2ecf20Sopenharmony_ci			       int where, u8 byte_en, u32 value)
3548c2ecf20Sopenharmony_ci{
3558c2ecf20Sopenharmony_ci	u32 headers[TLP_HDR_SIZE];
3568c2ecf20Sopenharmony_ci	int ret;
3578c2ecf20Sopenharmony_ci
3588c2ecf20Sopenharmony_ci	get_tlp_header(pcie, bus, devfn, where, byte_en, false,
3598c2ecf20Sopenharmony_ci		       headers);
3608c2ecf20Sopenharmony_ci
3618c2ecf20Sopenharmony_ci	/* check alignment to Qword */
3628c2ecf20Sopenharmony_ci	if ((where & 0x7) == 0)
3638c2ecf20Sopenharmony_ci		pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
3648c2ecf20Sopenharmony_ci						    value, true);
3658c2ecf20Sopenharmony_ci	else
3668c2ecf20Sopenharmony_ci		pcie->pcie_data->ops->tlp_write_pkt(pcie, headers,
3678c2ecf20Sopenharmony_ci						    value, false);
3688c2ecf20Sopenharmony_ci
3698c2ecf20Sopenharmony_ci	ret = pcie->pcie_data->ops->tlp_read_pkt(pcie, NULL);
3708c2ecf20Sopenharmony_ci	if (ret != PCIBIOS_SUCCESSFUL)
3718c2ecf20Sopenharmony_ci		return ret;
3728c2ecf20Sopenharmony_ci
3738c2ecf20Sopenharmony_ci	/*
3748c2ecf20Sopenharmony_ci	 * Monitor changes to PCI_PRIMARY_BUS register on root port
3758c2ecf20Sopenharmony_ci	 * and update local copy of root bus number accordingly.
3768c2ecf20Sopenharmony_ci	 */
3778c2ecf20Sopenharmony_ci	if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS))
3788c2ecf20Sopenharmony_ci		pcie->root_bus_nr = (u8)(value);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
3818c2ecf20Sopenharmony_ci}
3828c2ecf20Sopenharmony_ci
3838c2ecf20Sopenharmony_cistatic int s10_rp_read_cfg(struct altera_pcie *pcie, int where,
3848c2ecf20Sopenharmony_ci			   int size, u32 *value)
3858c2ecf20Sopenharmony_ci{
3868c2ecf20Sopenharmony_ci	void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	switch (size) {
3898c2ecf20Sopenharmony_ci	case 1:
3908c2ecf20Sopenharmony_ci		*value = readb(addr);
3918c2ecf20Sopenharmony_ci		break;
3928c2ecf20Sopenharmony_ci	case 2:
3938c2ecf20Sopenharmony_ci		*value = readw(addr);
3948c2ecf20Sopenharmony_ci		break;
3958c2ecf20Sopenharmony_ci	default:
3968c2ecf20Sopenharmony_ci		*value = readl(addr);
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci	}
3998c2ecf20Sopenharmony_ci
4008c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
4018c2ecf20Sopenharmony_ci}
4028c2ecf20Sopenharmony_ci
4038c2ecf20Sopenharmony_cistatic int s10_rp_write_cfg(struct altera_pcie *pcie, u8 busno,
4048c2ecf20Sopenharmony_ci			    int where, int size, u32 value)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	void __iomem *addr = S10_RP_CFG_ADDR(pcie, where);
4078c2ecf20Sopenharmony_ci
4088c2ecf20Sopenharmony_ci	switch (size) {
4098c2ecf20Sopenharmony_ci	case 1:
4108c2ecf20Sopenharmony_ci		writeb(value, addr);
4118c2ecf20Sopenharmony_ci		break;
4128c2ecf20Sopenharmony_ci	case 2:
4138c2ecf20Sopenharmony_ci		writew(value, addr);
4148c2ecf20Sopenharmony_ci		break;
4158c2ecf20Sopenharmony_ci	default:
4168c2ecf20Sopenharmony_ci		writel(value, addr);
4178c2ecf20Sopenharmony_ci		break;
4188c2ecf20Sopenharmony_ci	}
4198c2ecf20Sopenharmony_ci
4208c2ecf20Sopenharmony_ci	/*
4218c2ecf20Sopenharmony_ci	 * Monitor changes to PCI_PRIMARY_BUS register on root port
4228c2ecf20Sopenharmony_ci	 * and update local copy of root bus number accordingly.
4238c2ecf20Sopenharmony_ci	 */
4248c2ecf20Sopenharmony_ci	if (busno == pcie->root_bus_nr && where == PCI_PRIMARY_BUS)
4258c2ecf20Sopenharmony_ci		pcie->root_bus_nr = value & 0xff;
4268c2ecf20Sopenharmony_ci
4278c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
4288c2ecf20Sopenharmony_ci}
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_cistatic int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno,
4318c2ecf20Sopenharmony_ci				 unsigned int devfn, int where, int size,
4328c2ecf20Sopenharmony_ci				 u32 *value)
4338c2ecf20Sopenharmony_ci{
4348c2ecf20Sopenharmony_ci	int ret;
4358c2ecf20Sopenharmony_ci	u32 data;
4368c2ecf20Sopenharmony_ci	u8 byte_en;
4378c2ecf20Sopenharmony_ci
4388c2ecf20Sopenharmony_ci	if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_read_cfg)
4398c2ecf20Sopenharmony_ci		return pcie->pcie_data->ops->rp_read_cfg(pcie, where,
4408c2ecf20Sopenharmony_ci							 size, value);
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci	switch (size) {
4438c2ecf20Sopenharmony_ci	case 1:
4448c2ecf20Sopenharmony_ci		byte_en = 1 << (where & 3);
4458c2ecf20Sopenharmony_ci		break;
4468c2ecf20Sopenharmony_ci	case 2:
4478c2ecf20Sopenharmony_ci		byte_en = 3 << (where & 3);
4488c2ecf20Sopenharmony_ci		break;
4498c2ecf20Sopenharmony_ci	default:
4508c2ecf20Sopenharmony_ci		byte_en = 0xf;
4518c2ecf20Sopenharmony_ci		break;
4528c2ecf20Sopenharmony_ci	}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	ret = tlp_cfg_dword_read(pcie, busno, devfn,
4558c2ecf20Sopenharmony_ci				 (where & ~DWORD_MASK), byte_en, &data);
4568c2ecf20Sopenharmony_ci	if (ret != PCIBIOS_SUCCESSFUL)
4578c2ecf20Sopenharmony_ci		return ret;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	switch (size) {
4608c2ecf20Sopenharmony_ci	case 1:
4618c2ecf20Sopenharmony_ci		*value = (data >> (8 * (where & 0x3))) & 0xff;
4628c2ecf20Sopenharmony_ci		break;
4638c2ecf20Sopenharmony_ci	case 2:
4648c2ecf20Sopenharmony_ci		*value = (data >> (8 * (where & 0x2))) & 0xffff;
4658c2ecf20Sopenharmony_ci		break;
4668c2ecf20Sopenharmony_ci	default:
4678c2ecf20Sopenharmony_ci		*value = data;
4688c2ecf20Sopenharmony_ci		break;
4698c2ecf20Sopenharmony_ci	}
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	return PCIBIOS_SUCCESSFUL;
4728c2ecf20Sopenharmony_ci}
4738c2ecf20Sopenharmony_ci
4748c2ecf20Sopenharmony_cistatic int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno,
4758c2ecf20Sopenharmony_ci				  unsigned int devfn, int where, int size,
4768c2ecf20Sopenharmony_ci				  u32 value)
4778c2ecf20Sopenharmony_ci{
4788c2ecf20Sopenharmony_ci	u32 data32;
4798c2ecf20Sopenharmony_ci	u32 shift = 8 * (where & 3);
4808c2ecf20Sopenharmony_ci	u8 byte_en;
4818c2ecf20Sopenharmony_ci
4828c2ecf20Sopenharmony_ci	if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_write_cfg)
4838c2ecf20Sopenharmony_ci		return pcie->pcie_data->ops->rp_write_cfg(pcie, busno,
4848c2ecf20Sopenharmony_ci						     where, size, value);
4858c2ecf20Sopenharmony_ci
4868c2ecf20Sopenharmony_ci	switch (size) {
4878c2ecf20Sopenharmony_ci	case 1:
4888c2ecf20Sopenharmony_ci		data32 = (value & 0xff) << shift;
4898c2ecf20Sopenharmony_ci		byte_en = 1 << (where & 3);
4908c2ecf20Sopenharmony_ci		break;
4918c2ecf20Sopenharmony_ci	case 2:
4928c2ecf20Sopenharmony_ci		data32 = (value & 0xffff) << shift;
4938c2ecf20Sopenharmony_ci		byte_en = 3 << (where & 3);
4948c2ecf20Sopenharmony_ci		break;
4958c2ecf20Sopenharmony_ci	default:
4968c2ecf20Sopenharmony_ci		data32 = value;
4978c2ecf20Sopenharmony_ci		byte_en = 0xf;
4988c2ecf20Sopenharmony_ci		break;
4998c2ecf20Sopenharmony_ci	}
5008c2ecf20Sopenharmony_ci
5018c2ecf20Sopenharmony_ci	return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK),
5028c2ecf20Sopenharmony_ci				   byte_en, data32);
5038c2ecf20Sopenharmony_ci}
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_cistatic int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn,
5068c2ecf20Sopenharmony_ci				int where, int size, u32 *value)
5078c2ecf20Sopenharmony_ci{
5088c2ecf20Sopenharmony_ci	struct altera_pcie *pcie = bus->sysdata;
5098c2ecf20Sopenharmony_ci
5108c2ecf20Sopenharmony_ci	if (altera_pcie_hide_rc_bar(bus, devfn, where))
5118c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
5128c2ecf20Sopenharmony_ci
5138c2ecf20Sopenharmony_ci	if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) {
5148c2ecf20Sopenharmony_ci		*value = 0xffffffff;
5158c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
5168c2ecf20Sopenharmony_ci	}
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size,
5198c2ecf20Sopenharmony_ci				     value);
5208c2ecf20Sopenharmony_ci}
5218c2ecf20Sopenharmony_ci
5228c2ecf20Sopenharmony_cistatic int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn,
5238c2ecf20Sopenharmony_ci				 int where, int size, u32 value)
5248c2ecf20Sopenharmony_ci{
5258c2ecf20Sopenharmony_ci	struct altera_pcie *pcie = bus->sysdata;
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_ci	if (altera_pcie_hide_rc_bar(bus, devfn, where))
5288c2ecf20Sopenharmony_ci		return PCIBIOS_BAD_REGISTER_NUMBER;
5298c2ecf20Sopenharmony_ci
5308c2ecf20Sopenharmony_ci	if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn)))
5318c2ecf20Sopenharmony_ci		return PCIBIOS_DEVICE_NOT_FOUND;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size,
5348c2ecf20Sopenharmony_ci				     value);
5358c2ecf20Sopenharmony_ci}
5368c2ecf20Sopenharmony_ci
5378c2ecf20Sopenharmony_cistatic struct pci_ops altera_pcie_ops = {
5388c2ecf20Sopenharmony_ci	.read = altera_pcie_cfg_read,
5398c2ecf20Sopenharmony_ci	.write = altera_pcie_cfg_write,
5408c2ecf20Sopenharmony_ci};
5418c2ecf20Sopenharmony_ci
5428c2ecf20Sopenharmony_cistatic int altera_read_cap_word(struct altera_pcie *pcie, u8 busno,
5438c2ecf20Sopenharmony_ci				unsigned int devfn, int offset, u16 *value)
5448c2ecf20Sopenharmony_ci{
5458c2ecf20Sopenharmony_ci	u32 data;
5468c2ecf20Sopenharmony_ci	int ret;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci	ret = _altera_pcie_cfg_read(pcie, busno, devfn,
5498c2ecf20Sopenharmony_ci				    pcie->pcie_data->cap_offset + offset,
5508c2ecf20Sopenharmony_ci				    sizeof(*value),
5518c2ecf20Sopenharmony_ci				    &data);
5528c2ecf20Sopenharmony_ci	*value = data;
5538c2ecf20Sopenharmony_ci	return ret;
5548c2ecf20Sopenharmony_ci}
5558c2ecf20Sopenharmony_ci
5568c2ecf20Sopenharmony_cistatic int altera_write_cap_word(struct altera_pcie *pcie, u8 busno,
5578c2ecf20Sopenharmony_ci				 unsigned int devfn, int offset, u16 value)
5588c2ecf20Sopenharmony_ci{
5598c2ecf20Sopenharmony_ci	return _altera_pcie_cfg_write(pcie, busno, devfn,
5608c2ecf20Sopenharmony_ci				      pcie->pcie_data->cap_offset + offset,
5618c2ecf20Sopenharmony_ci				      sizeof(value),
5628c2ecf20Sopenharmony_ci				      value);
5638c2ecf20Sopenharmony_ci}
5648c2ecf20Sopenharmony_ci
5658c2ecf20Sopenharmony_cistatic void altera_wait_link_retrain(struct altera_pcie *pcie)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	struct device *dev = &pcie->pdev->dev;
5688c2ecf20Sopenharmony_ci	u16 reg16;
5698c2ecf20Sopenharmony_ci	unsigned long start_jiffies;
5708c2ecf20Sopenharmony_ci
5718c2ecf20Sopenharmony_ci	/* Wait for link training end. */
5728c2ecf20Sopenharmony_ci	start_jiffies = jiffies;
5738c2ecf20Sopenharmony_ci	for (;;) {
5748c2ecf20Sopenharmony_ci		altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
5758c2ecf20Sopenharmony_ci				     PCI_EXP_LNKSTA, &reg16);
5768c2ecf20Sopenharmony_ci		if (!(reg16 & PCI_EXP_LNKSTA_LT))
5778c2ecf20Sopenharmony_ci			break;
5788c2ecf20Sopenharmony_ci
5798c2ecf20Sopenharmony_ci		if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) {
5808c2ecf20Sopenharmony_ci			dev_err(dev, "link retrain timeout\n");
5818c2ecf20Sopenharmony_ci			break;
5828c2ecf20Sopenharmony_ci		}
5838c2ecf20Sopenharmony_ci		udelay(100);
5848c2ecf20Sopenharmony_ci	}
5858c2ecf20Sopenharmony_ci
5868c2ecf20Sopenharmony_ci	/* Wait for link is up */
5878c2ecf20Sopenharmony_ci	start_jiffies = jiffies;
5888c2ecf20Sopenharmony_ci	for (;;) {
5898c2ecf20Sopenharmony_ci		if (pcie->pcie_data->ops->get_link_status(pcie))
5908c2ecf20Sopenharmony_ci			break;
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci		if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) {
5938c2ecf20Sopenharmony_ci			dev_err(dev, "link up timeout\n");
5948c2ecf20Sopenharmony_ci			break;
5958c2ecf20Sopenharmony_ci		}
5968c2ecf20Sopenharmony_ci		udelay(100);
5978c2ecf20Sopenharmony_ci	}
5988c2ecf20Sopenharmony_ci}
5998c2ecf20Sopenharmony_ci
6008c2ecf20Sopenharmony_cistatic void altera_pcie_retrain(struct altera_pcie *pcie)
6018c2ecf20Sopenharmony_ci{
6028c2ecf20Sopenharmony_ci	u16 linkcap, linkstat, linkctl;
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	if (!pcie->pcie_data->ops->get_link_status(pcie))
6058c2ecf20Sopenharmony_ci		return;
6068c2ecf20Sopenharmony_ci
6078c2ecf20Sopenharmony_ci	/*
6088c2ecf20Sopenharmony_ci	 * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but
6098c2ecf20Sopenharmony_ci	 * current speed is 2.5 GB/s.
6108c2ecf20Sopenharmony_ci	 */
6118c2ecf20Sopenharmony_ci	altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP,
6128c2ecf20Sopenharmony_ci			     &linkcap);
6138c2ecf20Sopenharmony_ci	if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB)
6148c2ecf20Sopenharmony_ci		return;
6158c2ecf20Sopenharmony_ci
6168c2ecf20Sopenharmony_ci	altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA,
6178c2ecf20Sopenharmony_ci			     &linkstat);
6188c2ecf20Sopenharmony_ci	if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) {
6198c2ecf20Sopenharmony_ci		altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
6208c2ecf20Sopenharmony_ci				     PCI_EXP_LNKCTL, &linkctl);
6218c2ecf20Sopenharmony_ci		linkctl |= PCI_EXP_LNKCTL_RL;
6228c2ecf20Sopenharmony_ci		altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN,
6238c2ecf20Sopenharmony_ci				      PCI_EXP_LNKCTL, linkctl);
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci		altera_wait_link_retrain(pcie);
6268c2ecf20Sopenharmony_ci	}
6278c2ecf20Sopenharmony_ci}
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_cistatic int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
6308c2ecf20Sopenharmony_ci				irq_hw_number_t hwirq)
6318c2ecf20Sopenharmony_ci{
6328c2ecf20Sopenharmony_ci	irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
6338c2ecf20Sopenharmony_ci	irq_set_chip_data(irq, domain->host_data);
6348c2ecf20Sopenharmony_ci	return 0;
6358c2ecf20Sopenharmony_ci}
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_cistatic const struct irq_domain_ops intx_domain_ops = {
6388c2ecf20Sopenharmony_ci	.map = altera_pcie_intx_map,
6398c2ecf20Sopenharmony_ci	.xlate = pci_irqd_intx_xlate,
6408c2ecf20Sopenharmony_ci};
6418c2ecf20Sopenharmony_ci
6428c2ecf20Sopenharmony_cistatic void altera_pcie_isr(struct irq_desc *desc)
6438c2ecf20Sopenharmony_ci{
6448c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
6458c2ecf20Sopenharmony_ci	struct altera_pcie *pcie;
6468c2ecf20Sopenharmony_ci	struct device *dev;
6478c2ecf20Sopenharmony_ci	unsigned long status;
6488c2ecf20Sopenharmony_ci	u32 bit;
6498c2ecf20Sopenharmony_ci	u32 virq;
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
6528c2ecf20Sopenharmony_ci	pcie = irq_desc_get_handler_data(desc);
6538c2ecf20Sopenharmony_ci	dev = &pcie->pdev->dev;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	while ((status = cra_readl(pcie, P2A_INT_STATUS)
6568c2ecf20Sopenharmony_ci		& P2A_INT_STS_ALL) != 0) {
6578c2ecf20Sopenharmony_ci		for_each_set_bit(bit, &status, PCI_NUM_INTX) {
6588c2ecf20Sopenharmony_ci			/* clear interrupts */
6598c2ecf20Sopenharmony_ci			cra_writel(pcie, 1 << bit, P2A_INT_STATUS);
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_ci			virq = irq_find_mapping(pcie->irq_domain, bit);
6628c2ecf20Sopenharmony_ci			if (virq)
6638c2ecf20Sopenharmony_ci				generic_handle_irq(virq);
6648c2ecf20Sopenharmony_ci			else
6658c2ecf20Sopenharmony_ci				dev_err(dev, "unexpected IRQ, INT%d\n", bit);
6668c2ecf20Sopenharmony_ci		}
6678c2ecf20Sopenharmony_ci	}
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
6708c2ecf20Sopenharmony_ci}
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_cistatic int altera_pcie_init_irq_domain(struct altera_pcie *pcie)
6738c2ecf20Sopenharmony_ci{
6748c2ecf20Sopenharmony_ci	struct device *dev = &pcie->pdev->dev;
6758c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
6768c2ecf20Sopenharmony_ci
6778c2ecf20Sopenharmony_ci	/* Setup INTx */
6788c2ecf20Sopenharmony_ci	pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX,
6798c2ecf20Sopenharmony_ci					&intx_domain_ops, pcie);
6808c2ecf20Sopenharmony_ci	if (!pcie->irq_domain) {
6818c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get a INTx IRQ domain\n");
6828c2ecf20Sopenharmony_ci		return -ENOMEM;
6838c2ecf20Sopenharmony_ci	}
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_ci	return 0;
6868c2ecf20Sopenharmony_ci}
6878c2ecf20Sopenharmony_ci
6888c2ecf20Sopenharmony_cistatic void altera_pcie_irq_teardown(struct altera_pcie *pcie)
6898c2ecf20Sopenharmony_ci{
6908c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(pcie->irq, NULL, NULL);
6918c2ecf20Sopenharmony_ci	irq_domain_remove(pcie->irq_domain);
6928c2ecf20Sopenharmony_ci	irq_dispose_mapping(pcie->irq);
6938c2ecf20Sopenharmony_ci}
6948c2ecf20Sopenharmony_ci
6958c2ecf20Sopenharmony_cistatic int altera_pcie_parse_dt(struct altera_pcie *pcie)
6968c2ecf20Sopenharmony_ci{
6978c2ecf20Sopenharmony_ci	struct platform_device *pdev = pcie->pdev;
6988c2ecf20Sopenharmony_ci
6998c2ecf20Sopenharmony_ci	pcie->cra_base = devm_platform_ioremap_resource_byname(pdev, "Cra");
7008c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->cra_base))
7018c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->cra_base);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	if (pcie->pcie_data->version == ALTERA_PCIE_V2) {
7048c2ecf20Sopenharmony_ci		pcie->hip_base =
7058c2ecf20Sopenharmony_ci			devm_platform_ioremap_resource_byname(pdev, "Hip");
7068c2ecf20Sopenharmony_ci		if (IS_ERR(pcie->hip_base))
7078c2ecf20Sopenharmony_ci			return PTR_ERR(pcie->hip_base);
7088c2ecf20Sopenharmony_ci	}
7098c2ecf20Sopenharmony_ci
7108c2ecf20Sopenharmony_ci	/* setup IRQ */
7118c2ecf20Sopenharmony_ci	pcie->irq = platform_get_irq(pdev, 0);
7128c2ecf20Sopenharmony_ci	if (pcie->irq < 0)
7138c2ecf20Sopenharmony_ci		return pcie->irq;
7148c2ecf20Sopenharmony_ci
7158c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie);
7168c2ecf20Sopenharmony_ci	return 0;
7178c2ecf20Sopenharmony_ci}
7188c2ecf20Sopenharmony_ci
7198c2ecf20Sopenharmony_cistatic void altera_pcie_host_init(struct altera_pcie *pcie)
7208c2ecf20Sopenharmony_ci{
7218c2ecf20Sopenharmony_ci	altera_pcie_retrain(pcie);
7228c2ecf20Sopenharmony_ci}
7238c2ecf20Sopenharmony_ci
7248c2ecf20Sopenharmony_cistatic const struct altera_pcie_ops altera_pcie_ops_1_0 = {
7258c2ecf20Sopenharmony_ci	.tlp_read_pkt = tlp_read_packet,
7268c2ecf20Sopenharmony_ci	.tlp_write_pkt = tlp_write_packet,
7278c2ecf20Sopenharmony_ci	.get_link_status = altera_pcie_link_up,
7288c2ecf20Sopenharmony_ci};
7298c2ecf20Sopenharmony_ci
7308c2ecf20Sopenharmony_cistatic const struct altera_pcie_ops altera_pcie_ops_2_0 = {
7318c2ecf20Sopenharmony_ci	.tlp_read_pkt = s10_tlp_read_packet,
7328c2ecf20Sopenharmony_ci	.tlp_write_pkt = s10_tlp_write_packet,
7338c2ecf20Sopenharmony_ci	.get_link_status = s10_altera_pcie_link_up,
7348c2ecf20Sopenharmony_ci	.rp_read_cfg = s10_rp_read_cfg,
7358c2ecf20Sopenharmony_ci	.rp_write_cfg = s10_rp_write_cfg,
7368c2ecf20Sopenharmony_ci};
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_cistatic const struct altera_pcie_data altera_pcie_1_0_data = {
7398c2ecf20Sopenharmony_ci	.ops = &altera_pcie_ops_1_0,
7408c2ecf20Sopenharmony_ci	.cap_offset = 0x80,
7418c2ecf20Sopenharmony_ci	.version = ALTERA_PCIE_V1,
7428c2ecf20Sopenharmony_ci	.cfgrd0 = TLP_FMTTYPE_CFGRD0,
7438c2ecf20Sopenharmony_ci	.cfgrd1 = TLP_FMTTYPE_CFGRD1,
7448c2ecf20Sopenharmony_ci	.cfgwr0 = TLP_FMTTYPE_CFGWR0,
7458c2ecf20Sopenharmony_ci	.cfgwr1 = TLP_FMTTYPE_CFGWR1,
7468c2ecf20Sopenharmony_ci};
7478c2ecf20Sopenharmony_ci
7488c2ecf20Sopenharmony_cistatic const struct altera_pcie_data altera_pcie_2_0_data = {
7498c2ecf20Sopenharmony_ci	.ops = &altera_pcie_ops_2_0,
7508c2ecf20Sopenharmony_ci	.version = ALTERA_PCIE_V2,
7518c2ecf20Sopenharmony_ci	.cap_offset = 0x70,
7528c2ecf20Sopenharmony_ci	.cfgrd0 = S10_TLP_FMTTYPE_CFGRD0,
7538c2ecf20Sopenharmony_ci	.cfgrd1 = S10_TLP_FMTTYPE_CFGRD1,
7548c2ecf20Sopenharmony_ci	.cfgwr0 = S10_TLP_FMTTYPE_CFGWR0,
7558c2ecf20Sopenharmony_ci	.cfgwr1 = S10_TLP_FMTTYPE_CFGWR1,
7568c2ecf20Sopenharmony_ci};
7578c2ecf20Sopenharmony_ci
7588c2ecf20Sopenharmony_cistatic const struct of_device_id altera_pcie_of_match[] = {
7598c2ecf20Sopenharmony_ci	{.compatible = "altr,pcie-root-port-1.0",
7608c2ecf20Sopenharmony_ci	 .data = &altera_pcie_1_0_data },
7618c2ecf20Sopenharmony_ci	{.compatible = "altr,pcie-root-port-2.0",
7628c2ecf20Sopenharmony_ci	 .data = &altera_pcie_2_0_data },
7638c2ecf20Sopenharmony_ci	{},
7648c2ecf20Sopenharmony_ci};
7658c2ecf20Sopenharmony_ci
7668c2ecf20Sopenharmony_cistatic int altera_pcie_probe(struct platform_device *pdev)
7678c2ecf20Sopenharmony_ci{
7688c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
7698c2ecf20Sopenharmony_ci	struct altera_pcie *pcie;
7708c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge;
7718c2ecf20Sopenharmony_ci	int ret;
7728c2ecf20Sopenharmony_ci	const struct of_device_id *match;
7738c2ecf20Sopenharmony_ci
7748c2ecf20Sopenharmony_ci	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
7758c2ecf20Sopenharmony_ci	if (!bridge)
7768c2ecf20Sopenharmony_ci		return -ENOMEM;
7778c2ecf20Sopenharmony_ci
7788c2ecf20Sopenharmony_ci	pcie = pci_host_bridge_priv(bridge);
7798c2ecf20Sopenharmony_ci	pcie->pdev = pdev;
7808c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pcie);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	match = of_match_device(altera_pcie_of_match, &pdev->dev);
7838c2ecf20Sopenharmony_ci	if (!match)
7848c2ecf20Sopenharmony_ci		return -ENODEV;
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci	pcie->pcie_data = match->data;
7878c2ecf20Sopenharmony_ci
7888c2ecf20Sopenharmony_ci	ret = altera_pcie_parse_dt(pcie);
7898c2ecf20Sopenharmony_ci	if (ret) {
7908c2ecf20Sopenharmony_ci		dev_err(dev, "Parsing DT failed\n");
7918c2ecf20Sopenharmony_ci		return ret;
7928c2ecf20Sopenharmony_ci	}
7938c2ecf20Sopenharmony_ci
7948c2ecf20Sopenharmony_ci	ret = altera_pcie_init_irq_domain(pcie);
7958c2ecf20Sopenharmony_ci	if (ret) {
7968c2ecf20Sopenharmony_ci		dev_err(dev, "Failed creating IRQ Domain\n");
7978c2ecf20Sopenharmony_ci		return ret;
7988c2ecf20Sopenharmony_ci	}
7998c2ecf20Sopenharmony_ci
8008c2ecf20Sopenharmony_ci	/* clear all interrupts */
8018c2ecf20Sopenharmony_ci	cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS);
8028c2ecf20Sopenharmony_ci	/* enable all interrupts */
8038c2ecf20Sopenharmony_ci	cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE);
8048c2ecf20Sopenharmony_ci	altera_pcie_host_init(pcie);
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	bridge->sysdata = pcie;
8078c2ecf20Sopenharmony_ci	bridge->busnr = pcie->root_bus_nr;
8088c2ecf20Sopenharmony_ci	bridge->ops = &altera_pcie_ops;
8098c2ecf20Sopenharmony_ci
8108c2ecf20Sopenharmony_ci	return pci_host_probe(bridge);
8118c2ecf20Sopenharmony_ci}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_cistatic int altera_pcie_remove(struct platform_device *pdev)
8148c2ecf20Sopenharmony_ci{
8158c2ecf20Sopenharmony_ci	struct altera_pcie *pcie = platform_get_drvdata(pdev);
8168c2ecf20Sopenharmony_ci	struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie);
8178c2ecf20Sopenharmony_ci
8188c2ecf20Sopenharmony_ci	pci_stop_root_bus(bridge->bus);
8198c2ecf20Sopenharmony_ci	pci_remove_root_bus(bridge->bus);
8208c2ecf20Sopenharmony_ci	altera_pcie_irq_teardown(pcie);
8218c2ecf20Sopenharmony_ci
8228c2ecf20Sopenharmony_ci	return 0;
8238c2ecf20Sopenharmony_ci}
8248c2ecf20Sopenharmony_ci
8258c2ecf20Sopenharmony_cistatic struct platform_driver altera_pcie_driver = {
8268c2ecf20Sopenharmony_ci	.probe		= altera_pcie_probe,
8278c2ecf20Sopenharmony_ci	.remove		= altera_pcie_remove,
8288c2ecf20Sopenharmony_ci	.driver = {
8298c2ecf20Sopenharmony_ci		.name	= "altera-pcie",
8308c2ecf20Sopenharmony_ci		.of_match_table = altera_pcie_of_match,
8318c2ecf20Sopenharmony_ci	},
8328c2ecf20Sopenharmony_ci};
8338c2ecf20Sopenharmony_ci
8348c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, altera_pcie_of_match);
8358c2ecf20Sopenharmony_cimodule_platform_driver(altera_pcie_driver);
8368c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL v2");
837