18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * pcie-dra7xx - PCIe controller driver for TI DRA7xx SoCs
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Copyright (C) 2013-2014 Texas Instruments Incorporated - https://www.ti.com
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Authors: Kishon Vijay Abraham I <kishon@ti.com>
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/delay.h>
118c2ecf20Sopenharmony_ci#include <linux/device.h>
128c2ecf20Sopenharmony_ci#include <linux/err.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/irq.h>
158c2ecf20Sopenharmony_ci#include <linux/irqdomain.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/of_device.h>
198c2ecf20Sopenharmony_ci#include <linux/of_gpio.h>
208c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
218c2ecf20Sopenharmony_ci#include <linux/pci.h>
228c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
238c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
248c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h>
258c2ecf20Sopenharmony_ci#include <linux/resource.h>
268c2ecf20Sopenharmony_ci#include <linux/types.h>
278c2ecf20Sopenharmony_ci#include <linux/mfd/syscon.h>
288c2ecf20Sopenharmony_ci#include <linux/regmap.h>
298c2ecf20Sopenharmony_ci#include <linux/gpio/consumer.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include "../../pci.h"
328c2ecf20Sopenharmony_ci#include "pcie-designware.h"
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci/* PCIe controller wrapper DRA7XX configuration registers */
358c2ecf20Sopenharmony_ci
368c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN		0x0024
378c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN		0x0028
388c2ecf20Sopenharmony_ci#define	ERR_SYS						BIT(0)
398c2ecf20Sopenharmony_ci#define	ERR_FATAL					BIT(1)
408c2ecf20Sopenharmony_ci#define	ERR_NONFATAL					BIT(2)
418c2ecf20Sopenharmony_ci#define	ERR_COR						BIT(3)
428c2ecf20Sopenharmony_ci#define	ERR_AXI						BIT(4)
438c2ecf20Sopenharmony_ci#define	ERR_ECRC					BIT(5)
448c2ecf20Sopenharmony_ci#define	PME_TURN_OFF					BIT(8)
458c2ecf20Sopenharmony_ci#define	PME_TO_ACK					BIT(9)
468c2ecf20Sopenharmony_ci#define	PM_PME						BIT(10)
478c2ecf20Sopenharmony_ci#define	LINK_REQ_RST					BIT(11)
488c2ecf20Sopenharmony_ci#define	LINK_UP_EVT					BIT(12)
498c2ecf20Sopenharmony_ci#define	CFG_BME_EVT					BIT(13)
508c2ecf20Sopenharmony_ci#define	CFG_MSE_EVT					BIT(14)
518c2ecf20Sopenharmony_ci#define	INTERRUPTS (ERR_SYS | ERR_FATAL | ERR_NONFATAL | ERR_COR | ERR_AXI | \
528c2ecf20Sopenharmony_ci			ERR_ECRC | PME_TURN_OFF | PME_TO_ACK | PM_PME | \
538c2ecf20Sopenharmony_ci			LINK_REQ_RST | LINK_UP_EVT | CFG_BME_EVT | CFG_MSE_EVT)
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI		0x0034
568c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI		0x0038
578c2ecf20Sopenharmony_ci#define	INTA						BIT(0)
588c2ecf20Sopenharmony_ci#define	INTB						BIT(1)
598c2ecf20Sopenharmony_ci#define	INTC						BIT(2)
608c2ecf20Sopenharmony_ci#define	INTD						BIT(3)
618c2ecf20Sopenharmony_ci#define	MSI						BIT(4)
628c2ecf20Sopenharmony_ci#define	LEG_EP_INTERRUPTS (INTA | INTB | INTC | INTD)
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#define	PCIECTRL_TI_CONF_DEVICE_TYPE			0x0100
658c2ecf20Sopenharmony_ci#define	DEVICE_TYPE_EP					0x0
668c2ecf20Sopenharmony_ci#define	DEVICE_TYPE_LEG_EP				0x1
678c2ecf20Sopenharmony_ci#define	DEVICE_TYPE_RC					0x4
688c2ecf20Sopenharmony_ci
698c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_DEVICE_CMD			0x0104
708c2ecf20Sopenharmony_ci#define	LTSSM_EN					0x1
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define	PCIECTRL_DRA7XX_CONF_PHY_CS			0x010C
738c2ecf20Sopenharmony_ci#define	LINK_UP						BIT(16)
748c2ecf20Sopenharmony_ci#define	DRA7XX_CPU_TO_BUS_ADDR				0x0FFFFFFF
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define	PCIECTRL_TI_CONF_INTX_ASSERT			0x0124
778c2ecf20Sopenharmony_ci#define	PCIECTRL_TI_CONF_INTX_DEASSERT			0x0128
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define	PCIECTRL_TI_CONF_MSI_XMT			0x012c
808c2ecf20Sopenharmony_ci#define MSI_REQ_GRANT					BIT(0)
818c2ecf20Sopenharmony_ci#define MSI_VECTOR_SHIFT				7
828c2ecf20Sopenharmony_ci
838c2ecf20Sopenharmony_ci#define PCIE_1LANE_2LANE_SELECTION			BIT(13)
848c2ecf20Sopenharmony_ci#define PCIE_B1C0_MODE_SEL				BIT(2)
858c2ecf20Sopenharmony_ci#define PCIE_B0_B1_TSYNCEN				BIT(0)
868c2ecf20Sopenharmony_ci
878c2ecf20Sopenharmony_cistruct dra7xx_pcie {
888c2ecf20Sopenharmony_ci	struct dw_pcie		*pci;
898c2ecf20Sopenharmony_ci	void __iomem		*base;		/* DT ti_conf */
908c2ecf20Sopenharmony_ci	int			phy_count;	/* DT phy-names count */
918c2ecf20Sopenharmony_ci	struct phy		**phy;
928c2ecf20Sopenharmony_ci	struct irq_domain	*irq_domain;
938c2ecf20Sopenharmony_ci	enum dw_pcie_device_mode mode;
948c2ecf20Sopenharmony_ci};
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_cistruct dra7xx_pcie_of_data {
978c2ecf20Sopenharmony_ci	enum dw_pcie_device_mode mode;
988c2ecf20Sopenharmony_ci	u32 b1co_mode_sel_mask;
998c2ecf20Sopenharmony_ci};
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci#define to_dra7xx_pcie(x)	dev_get_drvdata((x)->dev)
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_cistatic inline u32 dra7xx_pcie_readl(struct dra7xx_pcie *pcie, u32 offset)
1048c2ecf20Sopenharmony_ci{
1058c2ecf20Sopenharmony_ci	return readl(pcie->base + offset);
1068c2ecf20Sopenharmony_ci}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_cistatic inline void dra7xx_pcie_writel(struct dra7xx_pcie *pcie, u32 offset,
1098c2ecf20Sopenharmony_ci				      u32 value)
1108c2ecf20Sopenharmony_ci{
1118c2ecf20Sopenharmony_ci	writel(value, pcie->base + offset);
1128c2ecf20Sopenharmony_ci}
1138c2ecf20Sopenharmony_ci
1148c2ecf20Sopenharmony_cistatic u64 dra7xx_pcie_cpu_addr_fixup(struct dw_pcie *pci, u64 pci_addr)
1158c2ecf20Sopenharmony_ci{
1168c2ecf20Sopenharmony_ci	return pci_addr & DRA7XX_CPU_TO_BUS_ADDR;
1178c2ecf20Sopenharmony_ci}
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_cistatic int dra7xx_pcie_link_up(struct dw_pcie *pci)
1208c2ecf20Sopenharmony_ci{
1218c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
1228c2ecf20Sopenharmony_ci	u32 reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_PHY_CS);
1238c2ecf20Sopenharmony_ci
1248c2ecf20Sopenharmony_ci	return !!(reg & LINK_UP);
1258c2ecf20Sopenharmony_ci}
1268c2ecf20Sopenharmony_ci
1278c2ecf20Sopenharmony_cistatic void dra7xx_pcie_stop_link(struct dw_pcie *pci)
1288c2ecf20Sopenharmony_ci{
1298c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
1308c2ecf20Sopenharmony_ci	u32 reg;
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
1338c2ecf20Sopenharmony_ci	reg &= ~LTSSM_EN;
1348c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
1358c2ecf20Sopenharmony_ci}
1368c2ecf20Sopenharmony_ci
1378c2ecf20Sopenharmony_cistatic int dra7xx_pcie_establish_link(struct dw_pcie *pci)
1388c2ecf20Sopenharmony_ci{
1398c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
1408c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
1418c2ecf20Sopenharmony_ci	u32 reg;
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_ci	if (dw_pcie_link_up(pci)) {
1448c2ecf20Sopenharmony_ci		dev_err(dev, "link is already up\n");
1458c2ecf20Sopenharmony_ci		return 0;
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
1498c2ecf20Sopenharmony_ci	reg |= LTSSM_EN;
1508c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_ci	return 0;
1538c2ecf20Sopenharmony_ci}
1548c2ecf20Sopenharmony_ci
1558c2ecf20Sopenharmony_cistatic void dra7xx_pcie_enable_msi_interrupts(struct dra7xx_pcie *dra7xx)
1568c2ecf20Sopenharmony_ci{
1578c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI,
1588c2ecf20Sopenharmony_ci			   LEG_EP_INTERRUPTS | MSI);
1598c2ecf20Sopenharmony_ci
1608c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx,
1618c2ecf20Sopenharmony_ci			   PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MSI,
1628c2ecf20Sopenharmony_ci			   MSI | LEG_EP_INTERRUPTS);
1638c2ecf20Sopenharmony_ci}
1648c2ecf20Sopenharmony_ci
1658c2ecf20Sopenharmony_cistatic void dra7xx_pcie_enable_wrapper_interrupts(struct dra7xx_pcie *dra7xx)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN,
1688c2ecf20Sopenharmony_ci			   INTERRUPTS);
1698c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQENABLE_SET_MAIN,
1708c2ecf20Sopenharmony_ci			   INTERRUPTS);
1718c2ecf20Sopenharmony_ci}
1728c2ecf20Sopenharmony_ci
1738c2ecf20Sopenharmony_cistatic void dra7xx_pcie_enable_interrupts(struct dra7xx_pcie *dra7xx)
1748c2ecf20Sopenharmony_ci{
1758c2ecf20Sopenharmony_ci	dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
1768c2ecf20Sopenharmony_ci	dra7xx_pcie_enable_msi_interrupts(dra7xx);
1778c2ecf20Sopenharmony_ci}
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_cistatic int dra7xx_pcie_host_init(struct pcie_port *pp)
1808c2ecf20Sopenharmony_ci{
1818c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
1828c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	dw_pcie_setup_rc(pp);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	dra7xx_pcie_establish_link(pci);
1878c2ecf20Sopenharmony_ci	dw_pcie_wait_for_link(pci);
1888c2ecf20Sopenharmony_ci	dw_pcie_msi_init(pp);
1898c2ecf20Sopenharmony_ci	dra7xx_pcie_enable_interrupts(dra7xx);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ci	return 0;
1928c2ecf20Sopenharmony_ci}
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_cistatic int dra7xx_pcie_intx_map(struct irq_domain *domain, unsigned int irq,
1958c2ecf20Sopenharmony_ci				irq_hw_number_t hwirq)
1968c2ecf20Sopenharmony_ci{
1978c2ecf20Sopenharmony_ci	irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq);
1988c2ecf20Sopenharmony_ci	irq_set_chip_data(irq, domain->host_data);
1998c2ecf20Sopenharmony_ci
2008c2ecf20Sopenharmony_ci	return 0;
2018c2ecf20Sopenharmony_ci}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_cistatic const struct irq_domain_ops intx_domain_ops = {
2048c2ecf20Sopenharmony_ci	.map = dra7xx_pcie_intx_map,
2058c2ecf20Sopenharmony_ci	.xlate = pci_irqd_intx_xlate,
2068c2ecf20Sopenharmony_ci};
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int dra7xx_pcie_handle_msi(struct pcie_port *pp, int index)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
2118c2ecf20Sopenharmony_ci	unsigned long val;
2128c2ecf20Sopenharmony_ci	int pos, irq;
2138c2ecf20Sopenharmony_ci
2148c2ecf20Sopenharmony_ci	val = dw_pcie_readl_dbi(pci, PCIE_MSI_INTR0_STATUS +
2158c2ecf20Sopenharmony_ci				   (index * MSI_REG_CTRL_BLOCK_SIZE));
2168c2ecf20Sopenharmony_ci	if (!val)
2178c2ecf20Sopenharmony_ci		return 0;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL, 0);
2208c2ecf20Sopenharmony_ci	while (pos != MAX_MSI_IRQS_PER_CTRL) {
2218c2ecf20Sopenharmony_ci		irq = irq_find_mapping(pp->irq_domain,
2228c2ecf20Sopenharmony_ci				       (index * MAX_MSI_IRQS_PER_CTRL) + pos);
2238c2ecf20Sopenharmony_ci		generic_handle_irq(irq);
2248c2ecf20Sopenharmony_ci		pos++;
2258c2ecf20Sopenharmony_ci		pos = find_next_bit(&val, MAX_MSI_IRQS_PER_CTRL, pos);
2268c2ecf20Sopenharmony_ci	}
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci	return 1;
2298c2ecf20Sopenharmony_ci}
2308c2ecf20Sopenharmony_ci
2318c2ecf20Sopenharmony_cistatic void dra7xx_pcie_handle_msi_irq(struct pcie_port *pp)
2328c2ecf20Sopenharmony_ci{
2338c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
2348c2ecf20Sopenharmony_ci	int ret, i, count, num_ctrls;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
2378c2ecf20Sopenharmony_ci
2388c2ecf20Sopenharmony_ci	/**
2398c2ecf20Sopenharmony_ci	 * Need to make sure all MSI status bits read 0 before exiting.
2408c2ecf20Sopenharmony_ci	 * Else, new MSI IRQs are not registered by the wrapper. Have an
2418c2ecf20Sopenharmony_ci	 * upperbound for the loop and exit the IRQ in case of IRQ flood
2428c2ecf20Sopenharmony_ci	 * to avoid locking up system in interrupt context.
2438c2ecf20Sopenharmony_ci	 */
2448c2ecf20Sopenharmony_ci	count = 0;
2458c2ecf20Sopenharmony_ci	do {
2468c2ecf20Sopenharmony_ci		ret = 0;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci		for (i = 0; i < num_ctrls; i++)
2498c2ecf20Sopenharmony_ci			ret |= dra7xx_pcie_handle_msi(pp, i);
2508c2ecf20Sopenharmony_ci		count++;
2518c2ecf20Sopenharmony_ci	} while (ret && count <= 1000);
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci	if (count > 1000)
2548c2ecf20Sopenharmony_ci		dev_warn_ratelimited(pci->dev,
2558c2ecf20Sopenharmony_ci				     "Too many MSI IRQs to handle\n");
2568c2ecf20Sopenharmony_ci}
2578c2ecf20Sopenharmony_ci
2588c2ecf20Sopenharmony_cistatic void dra7xx_pcie_msi_irq_handler(struct irq_desc *desc)
2598c2ecf20Sopenharmony_ci{
2608c2ecf20Sopenharmony_ci	struct irq_chip *chip = irq_desc_get_chip(desc);
2618c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx;
2628c2ecf20Sopenharmony_ci	struct dw_pcie *pci;
2638c2ecf20Sopenharmony_ci	struct pcie_port *pp;
2648c2ecf20Sopenharmony_ci	unsigned long reg;
2658c2ecf20Sopenharmony_ci	u32 virq, bit;
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	chained_irq_enter(chip, desc);
2688c2ecf20Sopenharmony_ci
2698c2ecf20Sopenharmony_ci	pp = irq_desc_get_handler_data(desc);
2708c2ecf20Sopenharmony_ci	pci = to_dw_pcie_from_pp(pp);
2718c2ecf20Sopenharmony_ci	dra7xx = to_dra7xx_pcie(pci);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci	reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI);
2748c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MSI, reg);
2758c2ecf20Sopenharmony_ci
2768c2ecf20Sopenharmony_ci	switch (reg) {
2778c2ecf20Sopenharmony_ci	case MSI:
2788c2ecf20Sopenharmony_ci		dra7xx_pcie_handle_msi_irq(pp);
2798c2ecf20Sopenharmony_ci		break;
2808c2ecf20Sopenharmony_ci	case INTA:
2818c2ecf20Sopenharmony_ci	case INTB:
2828c2ecf20Sopenharmony_ci	case INTC:
2838c2ecf20Sopenharmony_ci	case INTD:
2848c2ecf20Sopenharmony_ci		for_each_set_bit(bit, &reg, PCI_NUM_INTX) {
2858c2ecf20Sopenharmony_ci			virq = irq_find_mapping(dra7xx->irq_domain, bit);
2868c2ecf20Sopenharmony_ci			if (virq)
2878c2ecf20Sopenharmony_ci				generic_handle_irq(virq);
2888c2ecf20Sopenharmony_ci		}
2898c2ecf20Sopenharmony_ci		break;
2908c2ecf20Sopenharmony_ci	}
2918c2ecf20Sopenharmony_ci
2928c2ecf20Sopenharmony_ci	chained_irq_exit(chip, desc);
2938c2ecf20Sopenharmony_ci}
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_cistatic irqreturn_t dra7xx_pcie_irq_handler(int irq, void *arg)
2968c2ecf20Sopenharmony_ci{
2978c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = arg;
2988c2ecf20Sopenharmony_ci	struct dw_pcie *pci = dra7xx->pci;
2998c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
3008c2ecf20Sopenharmony_ci	struct dw_pcie_ep *ep = &pci->ep;
3018c2ecf20Sopenharmony_ci	u32 reg;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN);
3048c2ecf20Sopenharmony_ci
3058c2ecf20Sopenharmony_ci	if (reg & ERR_SYS)
3068c2ecf20Sopenharmony_ci		dev_dbg(dev, "System Error\n");
3078c2ecf20Sopenharmony_ci
3088c2ecf20Sopenharmony_ci	if (reg & ERR_FATAL)
3098c2ecf20Sopenharmony_ci		dev_dbg(dev, "Fatal Error\n");
3108c2ecf20Sopenharmony_ci
3118c2ecf20Sopenharmony_ci	if (reg & ERR_NONFATAL)
3128c2ecf20Sopenharmony_ci		dev_dbg(dev, "Non Fatal Error\n");
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	if (reg & ERR_COR)
3158c2ecf20Sopenharmony_ci		dev_dbg(dev, "Correctable Error\n");
3168c2ecf20Sopenharmony_ci
3178c2ecf20Sopenharmony_ci	if (reg & ERR_AXI)
3188c2ecf20Sopenharmony_ci		dev_dbg(dev, "AXI tag lookup fatal Error\n");
3198c2ecf20Sopenharmony_ci
3208c2ecf20Sopenharmony_ci	if (reg & ERR_ECRC)
3218c2ecf20Sopenharmony_ci		dev_dbg(dev, "ECRC Error\n");
3228c2ecf20Sopenharmony_ci
3238c2ecf20Sopenharmony_ci	if (reg & PME_TURN_OFF)
3248c2ecf20Sopenharmony_ci		dev_dbg(dev,
3258c2ecf20Sopenharmony_ci			"Power Management Event Turn-Off message received\n");
3268c2ecf20Sopenharmony_ci
3278c2ecf20Sopenharmony_ci	if (reg & PME_TO_ACK)
3288c2ecf20Sopenharmony_ci		dev_dbg(dev,
3298c2ecf20Sopenharmony_ci			"Power Management Turn-Off Ack message received\n");
3308c2ecf20Sopenharmony_ci
3318c2ecf20Sopenharmony_ci	if (reg & PM_PME)
3328c2ecf20Sopenharmony_ci		dev_dbg(dev, "PM Power Management Event message received\n");
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_ci	if (reg & LINK_REQ_RST)
3358c2ecf20Sopenharmony_ci		dev_dbg(dev, "Link Request Reset\n");
3368c2ecf20Sopenharmony_ci
3378c2ecf20Sopenharmony_ci	if (reg & LINK_UP_EVT) {
3388c2ecf20Sopenharmony_ci		if (dra7xx->mode == DW_PCIE_EP_TYPE)
3398c2ecf20Sopenharmony_ci			dw_pcie_ep_linkup(ep);
3408c2ecf20Sopenharmony_ci		dev_dbg(dev, "Link-up state change\n");
3418c2ecf20Sopenharmony_ci	}
3428c2ecf20Sopenharmony_ci
3438c2ecf20Sopenharmony_ci	if (reg & CFG_BME_EVT)
3448c2ecf20Sopenharmony_ci		dev_dbg(dev, "CFG 'Bus Master Enable' change\n");
3458c2ecf20Sopenharmony_ci
3468c2ecf20Sopenharmony_ci	if (reg & CFG_MSE_EVT)
3478c2ecf20Sopenharmony_ci		dev_dbg(dev, "CFG 'Memory Space Enable' change\n");
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_IRQSTATUS_MAIN, reg);
3508c2ecf20Sopenharmony_ci
3518c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
3528c2ecf20Sopenharmony_ci}
3538c2ecf20Sopenharmony_ci
3548c2ecf20Sopenharmony_cistatic int dra7xx_pcie_init_irq_domain(struct pcie_port *pp)
3558c2ecf20Sopenharmony_ci{
3568c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
3578c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
3588c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
3598c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
3608c2ecf20Sopenharmony_ci	struct device_node *pcie_intc_node =  of_get_next_child(node, NULL);
3618c2ecf20Sopenharmony_ci
3628c2ecf20Sopenharmony_ci	if (!pcie_intc_node) {
3638c2ecf20Sopenharmony_ci		dev_err(dev, "No PCIe Intc node found\n");
3648c2ecf20Sopenharmony_ci		return -ENODEV;
3658c2ecf20Sopenharmony_ci	}
3668c2ecf20Sopenharmony_ci
3678c2ecf20Sopenharmony_ci	irq_set_chained_handler_and_data(pp->irq, dra7xx_pcie_msi_irq_handler,
3688c2ecf20Sopenharmony_ci					 pp);
3698c2ecf20Sopenharmony_ci	dra7xx->irq_domain = irq_domain_add_linear(pcie_intc_node, PCI_NUM_INTX,
3708c2ecf20Sopenharmony_ci						   &intx_domain_ops, pp);
3718c2ecf20Sopenharmony_ci	of_node_put(pcie_intc_node);
3728c2ecf20Sopenharmony_ci	if (!dra7xx->irq_domain) {
3738c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to get a INTx IRQ domain\n");
3748c2ecf20Sopenharmony_ci		return -ENODEV;
3758c2ecf20Sopenharmony_ci	}
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	return 0;
3788c2ecf20Sopenharmony_ci}
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_cistatic void dra7xx_pcie_setup_msi_msg(struct irq_data *d, struct msi_msg *msg)
3818c2ecf20Sopenharmony_ci{
3828c2ecf20Sopenharmony_ci	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
3838c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
3848c2ecf20Sopenharmony_ci	u64 msi_target;
3858c2ecf20Sopenharmony_ci
3868c2ecf20Sopenharmony_ci	msi_target = (u64)pp->msi_data;
3878c2ecf20Sopenharmony_ci
3888c2ecf20Sopenharmony_ci	msg->address_lo = lower_32_bits(msi_target);
3898c2ecf20Sopenharmony_ci	msg->address_hi = upper_32_bits(msi_target);
3908c2ecf20Sopenharmony_ci
3918c2ecf20Sopenharmony_ci	msg->data = d->hwirq;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n",
3948c2ecf20Sopenharmony_ci		(int)d->hwirq, msg->address_hi, msg->address_lo);
3958c2ecf20Sopenharmony_ci}
3968c2ecf20Sopenharmony_ci
3978c2ecf20Sopenharmony_cistatic int dra7xx_pcie_msi_set_affinity(struct irq_data *d,
3988c2ecf20Sopenharmony_ci					const struct cpumask *mask,
3998c2ecf20Sopenharmony_ci					bool force)
4008c2ecf20Sopenharmony_ci{
4018c2ecf20Sopenharmony_ci	return -EINVAL;
4028c2ecf20Sopenharmony_ci}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_cistatic void dra7xx_pcie_bottom_mask(struct irq_data *d)
4058c2ecf20Sopenharmony_ci{
4068c2ecf20Sopenharmony_ci	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
4078c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4088c2ecf20Sopenharmony_ci	unsigned int res, bit, ctrl;
4098c2ecf20Sopenharmony_ci	unsigned long flags;
4108c2ecf20Sopenharmony_ci
4118c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&pp->lock, flags);
4128c2ecf20Sopenharmony_ci
4138c2ecf20Sopenharmony_ci	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
4148c2ecf20Sopenharmony_ci	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
4158c2ecf20Sopenharmony_ci	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
4168c2ecf20Sopenharmony_ci
4178c2ecf20Sopenharmony_ci	pp->irq_mask[ctrl] |= BIT(bit);
4188c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res,
4198c2ecf20Sopenharmony_ci			   pp->irq_mask[ctrl]);
4208c2ecf20Sopenharmony_ci
4218c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&pp->lock, flags);
4228c2ecf20Sopenharmony_ci}
4238c2ecf20Sopenharmony_ci
4248c2ecf20Sopenharmony_cistatic void dra7xx_pcie_bottom_unmask(struct irq_data *d)
4258c2ecf20Sopenharmony_ci{
4268c2ecf20Sopenharmony_ci	struct pcie_port *pp = irq_data_get_irq_chip_data(d);
4278c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4288c2ecf20Sopenharmony_ci	unsigned int res, bit, ctrl;
4298c2ecf20Sopenharmony_ci	unsigned long flags;
4308c2ecf20Sopenharmony_ci
4318c2ecf20Sopenharmony_ci	raw_spin_lock_irqsave(&pp->lock, flags);
4328c2ecf20Sopenharmony_ci
4338c2ecf20Sopenharmony_ci	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
4348c2ecf20Sopenharmony_ci	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
4358c2ecf20Sopenharmony_ci	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
4368c2ecf20Sopenharmony_ci
4378c2ecf20Sopenharmony_ci	pp->irq_mask[ctrl] &= ~BIT(bit);
4388c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK + res,
4398c2ecf20Sopenharmony_ci			   pp->irq_mask[ctrl]);
4408c2ecf20Sopenharmony_ci
4418c2ecf20Sopenharmony_ci	raw_spin_unlock_irqrestore(&pp->lock, flags);
4428c2ecf20Sopenharmony_ci}
4438c2ecf20Sopenharmony_ci
4448c2ecf20Sopenharmony_cistatic void dra7xx_pcie_bottom_ack(struct irq_data *d)
4458c2ecf20Sopenharmony_ci{
4468c2ecf20Sopenharmony_ci	struct pcie_port *pp  = irq_data_get_irq_chip_data(d);
4478c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4488c2ecf20Sopenharmony_ci	unsigned int res, bit, ctrl;
4498c2ecf20Sopenharmony_ci
4508c2ecf20Sopenharmony_ci	ctrl = d->hwirq / MAX_MSI_IRQS_PER_CTRL;
4518c2ecf20Sopenharmony_ci	res = ctrl * MSI_REG_CTRL_BLOCK_SIZE;
4528c2ecf20Sopenharmony_ci	bit = d->hwirq % MAX_MSI_IRQS_PER_CTRL;
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_STATUS + res, BIT(bit));
4558c2ecf20Sopenharmony_ci}
4568c2ecf20Sopenharmony_ci
4578c2ecf20Sopenharmony_cistatic struct irq_chip dra7xx_pci_msi_bottom_irq_chip = {
4588c2ecf20Sopenharmony_ci	.name = "DRA7XX-PCI-MSI",
4598c2ecf20Sopenharmony_ci	.irq_ack = dra7xx_pcie_bottom_ack,
4608c2ecf20Sopenharmony_ci	.irq_compose_msi_msg = dra7xx_pcie_setup_msi_msg,
4618c2ecf20Sopenharmony_ci	.irq_set_affinity = dra7xx_pcie_msi_set_affinity,
4628c2ecf20Sopenharmony_ci	.irq_mask = dra7xx_pcie_bottom_mask,
4638c2ecf20Sopenharmony_ci	.irq_unmask = dra7xx_pcie_bottom_unmask,
4648c2ecf20Sopenharmony_ci};
4658c2ecf20Sopenharmony_ci
4668c2ecf20Sopenharmony_cistatic int dra7xx_pcie_msi_host_init(struct pcie_port *pp)
4678c2ecf20Sopenharmony_ci{
4688c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
4698c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
4708c2ecf20Sopenharmony_ci	u32 ctrl, num_ctrls;
4718c2ecf20Sopenharmony_ci	int ret;
4728c2ecf20Sopenharmony_ci
4738c2ecf20Sopenharmony_ci	pp->msi_irq_chip = &dra7xx_pci_msi_bottom_irq_chip;
4748c2ecf20Sopenharmony_ci
4758c2ecf20Sopenharmony_ci	num_ctrls = pp->num_vectors / MAX_MSI_IRQS_PER_CTRL;
4768c2ecf20Sopenharmony_ci	/* Initialize IRQ Status array */
4778c2ecf20Sopenharmony_ci	for (ctrl = 0; ctrl < num_ctrls; ctrl++) {
4788c2ecf20Sopenharmony_ci		pp->irq_mask[ctrl] = ~0;
4798c2ecf20Sopenharmony_ci		dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_MASK +
4808c2ecf20Sopenharmony_ci				    (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
4818c2ecf20Sopenharmony_ci				    pp->irq_mask[ctrl]);
4828c2ecf20Sopenharmony_ci		dw_pcie_writel_dbi(pci, PCIE_MSI_INTR0_ENABLE +
4838c2ecf20Sopenharmony_ci				    (ctrl * MSI_REG_CTRL_BLOCK_SIZE),
4848c2ecf20Sopenharmony_ci				    ~0);
4858c2ecf20Sopenharmony_ci	}
4868c2ecf20Sopenharmony_ci
4878c2ecf20Sopenharmony_ci	ret = dw_pcie_allocate_domains(pp);
4888c2ecf20Sopenharmony_ci	if (ret)
4898c2ecf20Sopenharmony_ci		return ret;
4908c2ecf20Sopenharmony_ci
4918c2ecf20Sopenharmony_ci	pp->msi_data = dma_map_single_attrs(dev, &pp->msi_msg,
4928c2ecf20Sopenharmony_ci					   sizeof(pp->msi_msg),
4938c2ecf20Sopenharmony_ci					   DMA_FROM_DEVICE,
4948c2ecf20Sopenharmony_ci					   DMA_ATTR_SKIP_CPU_SYNC);
4958c2ecf20Sopenharmony_ci	ret = dma_mapping_error(dev, pp->msi_data);
4968c2ecf20Sopenharmony_ci	if (ret) {
4978c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to map MSI data\n");
4988c2ecf20Sopenharmony_ci		pp->msi_data = 0;
4998c2ecf20Sopenharmony_ci		dw_pcie_free_msi(pp);
5008c2ecf20Sopenharmony_ci	}
5018c2ecf20Sopenharmony_ci	return ret;
5028c2ecf20Sopenharmony_ci}
5038c2ecf20Sopenharmony_ci
5048c2ecf20Sopenharmony_cistatic const struct dw_pcie_host_ops dra7xx_pcie_host_ops = {
5058c2ecf20Sopenharmony_ci	.host_init = dra7xx_pcie_host_init,
5068c2ecf20Sopenharmony_ci	.msi_host_init = dra7xx_pcie_msi_host_init,
5078c2ecf20Sopenharmony_ci};
5088c2ecf20Sopenharmony_ci
5098c2ecf20Sopenharmony_cistatic void dra7xx_pcie_ep_init(struct dw_pcie_ep *ep)
5108c2ecf20Sopenharmony_ci{
5118c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
5128c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
5138c2ecf20Sopenharmony_ci	enum pci_barno bar;
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci	for (bar = 0; bar < PCI_STD_NUM_BARS; bar++)
5168c2ecf20Sopenharmony_ci		dw_pcie_ep_reset_bar(pci, bar);
5178c2ecf20Sopenharmony_ci
5188c2ecf20Sopenharmony_ci	dra7xx_pcie_enable_wrapper_interrupts(dra7xx);
5198c2ecf20Sopenharmony_ci}
5208c2ecf20Sopenharmony_ci
5218c2ecf20Sopenharmony_cistatic void dra7xx_pcie_raise_legacy_irq(struct dra7xx_pcie *dra7xx)
5228c2ecf20Sopenharmony_ci{
5238c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_ASSERT, 0x1);
5248c2ecf20Sopenharmony_ci	mdelay(1);
5258c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_INTX_DEASSERT, 0x1);
5268c2ecf20Sopenharmony_ci}
5278c2ecf20Sopenharmony_ci
5288c2ecf20Sopenharmony_cistatic void dra7xx_pcie_raise_msi_irq(struct dra7xx_pcie *dra7xx,
5298c2ecf20Sopenharmony_ci				      u8 interrupt_num)
5308c2ecf20Sopenharmony_ci{
5318c2ecf20Sopenharmony_ci	u32 reg;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	reg = (interrupt_num - 1) << MSI_VECTOR_SHIFT;
5348c2ecf20Sopenharmony_ci	reg |= MSI_REQ_GRANT;
5358c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_MSI_XMT, reg);
5368c2ecf20Sopenharmony_ci}
5378c2ecf20Sopenharmony_ci
5388c2ecf20Sopenharmony_cistatic int dra7xx_pcie_raise_irq(struct dw_pcie_ep *ep, u8 func_no,
5398c2ecf20Sopenharmony_ci				 enum pci_epc_irq_type type, u16 interrupt_num)
5408c2ecf20Sopenharmony_ci{
5418c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_ep(ep);
5428c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = to_dra7xx_pcie(pci);
5438c2ecf20Sopenharmony_ci
5448c2ecf20Sopenharmony_ci	switch (type) {
5458c2ecf20Sopenharmony_ci	case PCI_EPC_IRQ_LEGACY:
5468c2ecf20Sopenharmony_ci		dra7xx_pcie_raise_legacy_irq(dra7xx);
5478c2ecf20Sopenharmony_ci		break;
5488c2ecf20Sopenharmony_ci	case PCI_EPC_IRQ_MSI:
5498c2ecf20Sopenharmony_ci		dra7xx_pcie_raise_msi_irq(dra7xx, interrupt_num);
5508c2ecf20Sopenharmony_ci		break;
5518c2ecf20Sopenharmony_ci	default:
5528c2ecf20Sopenharmony_ci		dev_err(pci->dev, "UNKNOWN IRQ type\n");
5538c2ecf20Sopenharmony_ci	}
5548c2ecf20Sopenharmony_ci
5558c2ecf20Sopenharmony_ci	return 0;
5568c2ecf20Sopenharmony_ci}
5578c2ecf20Sopenharmony_ci
5588c2ecf20Sopenharmony_cistatic const struct pci_epc_features dra7xx_pcie_epc_features = {
5598c2ecf20Sopenharmony_ci	.linkup_notifier = true,
5608c2ecf20Sopenharmony_ci	.msi_capable = true,
5618c2ecf20Sopenharmony_ci	.msix_capable = false,
5628c2ecf20Sopenharmony_ci};
5638c2ecf20Sopenharmony_ci
5648c2ecf20Sopenharmony_cistatic const struct pci_epc_features*
5658c2ecf20Sopenharmony_cidra7xx_pcie_get_features(struct dw_pcie_ep *ep)
5668c2ecf20Sopenharmony_ci{
5678c2ecf20Sopenharmony_ci	return &dra7xx_pcie_epc_features;
5688c2ecf20Sopenharmony_ci}
5698c2ecf20Sopenharmony_ci
5708c2ecf20Sopenharmony_cistatic const struct dw_pcie_ep_ops pcie_ep_ops = {
5718c2ecf20Sopenharmony_ci	.ep_init = dra7xx_pcie_ep_init,
5728c2ecf20Sopenharmony_ci	.raise_irq = dra7xx_pcie_raise_irq,
5738c2ecf20Sopenharmony_ci	.get_features = dra7xx_pcie_get_features,
5748c2ecf20Sopenharmony_ci};
5758c2ecf20Sopenharmony_ci
5768c2ecf20Sopenharmony_cistatic int __init dra7xx_add_pcie_ep(struct dra7xx_pcie *dra7xx,
5778c2ecf20Sopenharmony_ci				     struct platform_device *pdev)
5788c2ecf20Sopenharmony_ci{
5798c2ecf20Sopenharmony_ci	int ret;
5808c2ecf20Sopenharmony_ci	struct dw_pcie_ep *ep;
5818c2ecf20Sopenharmony_ci	struct resource *res;
5828c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
5838c2ecf20Sopenharmony_ci	struct dw_pcie *pci = dra7xx->pci;
5848c2ecf20Sopenharmony_ci
5858c2ecf20Sopenharmony_ci	ep = &pci->ep;
5868c2ecf20Sopenharmony_ci	ep->ops = &pcie_ep_ops;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "ep_dbics");
5898c2ecf20Sopenharmony_ci	if (IS_ERR(pci->dbi_base))
5908c2ecf20Sopenharmony_ci		return PTR_ERR(pci->dbi_base);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	pci->dbi_base2 =
5938c2ecf20Sopenharmony_ci		devm_platform_ioremap_resource_byname(pdev, "ep_dbics2");
5948c2ecf20Sopenharmony_ci	if (IS_ERR(pci->dbi_base2))
5958c2ecf20Sopenharmony_ci		return PTR_ERR(pci->dbi_base2);
5968c2ecf20Sopenharmony_ci
5978c2ecf20Sopenharmony_ci	res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space");
5988c2ecf20Sopenharmony_ci	if (!res)
5998c2ecf20Sopenharmony_ci		return -EINVAL;
6008c2ecf20Sopenharmony_ci
6018c2ecf20Sopenharmony_ci	ep->phys_base = res->start;
6028c2ecf20Sopenharmony_ci	ep->addr_size = resource_size(res);
6038c2ecf20Sopenharmony_ci
6048c2ecf20Sopenharmony_ci	ret = dw_pcie_ep_init(ep);
6058c2ecf20Sopenharmony_ci	if (ret) {
6068c2ecf20Sopenharmony_ci		dev_err(dev, "failed to initialize endpoint\n");
6078c2ecf20Sopenharmony_ci		return ret;
6088c2ecf20Sopenharmony_ci	}
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	return 0;
6118c2ecf20Sopenharmony_ci}
6128c2ecf20Sopenharmony_ci
6138c2ecf20Sopenharmony_cistatic int __init dra7xx_add_pcie_port(struct dra7xx_pcie *dra7xx,
6148c2ecf20Sopenharmony_ci				       struct platform_device *pdev)
6158c2ecf20Sopenharmony_ci{
6168c2ecf20Sopenharmony_ci	int ret;
6178c2ecf20Sopenharmony_ci	struct dw_pcie *pci = dra7xx->pci;
6188c2ecf20Sopenharmony_ci	struct pcie_port *pp = &pci->pp;
6198c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
6208c2ecf20Sopenharmony_ci
6218c2ecf20Sopenharmony_ci	pp->irq = platform_get_irq(pdev, 1);
6228c2ecf20Sopenharmony_ci	if (pp->irq < 0)
6238c2ecf20Sopenharmony_ci		return pp->irq;
6248c2ecf20Sopenharmony_ci
6258c2ecf20Sopenharmony_ci	ret = dra7xx_pcie_init_irq_domain(pp);
6268c2ecf20Sopenharmony_ci	if (ret < 0)
6278c2ecf20Sopenharmony_ci		return ret;
6288c2ecf20Sopenharmony_ci
6298c2ecf20Sopenharmony_ci	pci->dbi_base = devm_platform_ioremap_resource_byname(pdev, "rc_dbics");
6308c2ecf20Sopenharmony_ci	if (IS_ERR(pci->dbi_base))
6318c2ecf20Sopenharmony_ci		return PTR_ERR(pci->dbi_base);
6328c2ecf20Sopenharmony_ci
6338c2ecf20Sopenharmony_ci	pp->ops = &dra7xx_pcie_host_ops;
6348c2ecf20Sopenharmony_ci
6358c2ecf20Sopenharmony_ci	ret = dw_pcie_host_init(pp);
6368c2ecf20Sopenharmony_ci	if (ret) {
6378c2ecf20Sopenharmony_ci		dev_err(dev, "failed to initialize host\n");
6388c2ecf20Sopenharmony_ci		return ret;
6398c2ecf20Sopenharmony_ci	}
6408c2ecf20Sopenharmony_ci
6418c2ecf20Sopenharmony_ci	return 0;
6428c2ecf20Sopenharmony_ci}
6438c2ecf20Sopenharmony_ci
6448c2ecf20Sopenharmony_cistatic const struct dw_pcie_ops dw_pcie_ops = {
6458c2ecf20Sopenharmony_ci	.cpu_addr_fixup = dra7xx_pcie_cpu_addr_fixup,
6468c2ecf20Sopenharmony_ci	.start_link = dra7xx_pcie_establish_link,
6478c2ecf20Sopenharmony_ci	.stop_link = dra7xx_pcie_stop_link,
6488c2ecf20Sopenharmony_ci	.link_up = dra7xx_pcie_link_up,
6498c2ecf20Sopenharmony_ci};
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_cistatic void dra7xx_pcie_disable_phy(struct dra7xx_pcie *dra7xx)
6528c2ecf20Sopenharmony_ci{
6538c2ecf20Sopenharmony_ci	int phy_count = dra7xx->phy_count;
6548c2ecf20Sopenharmony_ci
6558c2ecf20Sopenharmony_ci	while (phy_count--) {
6568c2ecf20Sopenharmony_ci		phy_power_off(dra7xx->phy[phy_count]);
6578c2ecf20Sopenharmony_ci		phy_exit(dra7xx->phy[phy_count]);
6588c2ecf20Sopenharmony_ci	}
6598c2ecf20Sopenharmony_ci}
6608c2ecf20Sopenharmony_ci
6618c2ecf20Sopenharmony_cistatic int dra7xx_pcie_enable_phy(struct dra7xx_pcie *dra7xx)
6628c2ecf20Sopenharmony_ci{
6638c2ecf20Sopenharmony_ci	int phy_count = dra7xx->phy_count;
6648c2ecf20Sopenharmony_ci	int ret;
6658c2ecf20Sopenharmony_ci	int i;
6668c2ecf20Sopenharmony_ci
6678c2ecf20Sopenharmony_ci	for (i = 0; i < phy_count; i++) {
6688c2ecf20Sopenharmony_ci		ret = phy_set_mode(dra7xx->phy[i], PHY_MODE_PCIE);
6698c2ecf20Sopenharmony_ci		if (ret < 0)
6708c2ecf20Sopenharmony_ci			goto err_phy;
6718c2ecf20Sopenharmony_ci
6728c2ecf20Sopenharmony_ci		ret = phy_init(dra7xx->phy[i]);
6738c2ecf20Sopenharmony_ci		if (ret < 0)
6748c2ecf20Sopenharmony_ci			goto err_phy;
6758c2ecf20Sopenharmony_ci
6768c2ecf20Sopenharmony_ci		ret = phy_power_on(dra7xx->phy[i]);
6778c2ecf20Sopenharmony_ci		if (ret < 0) {
6788c2ecf20Sopenharmony_ci			phy_exit(dra7xx->phy[i]);
6798c2ecf20Sopenharmony_ci			goto err_phy;
6808c2ecf20Sopenharmony_ci		}
6818c2ecf20Sopenharmony_ci	}
6828c2ecf20Sopenharmony_ci
6838c2ecf20Sopenharmony_ci	return 0;
6848c2ecf20Sopenharmony_ci
6858c2ecf20Sopenharmony_cierr_phy:
6868c2ecf20Sopenharmony_ci	while (--i >= 0) {
6878c2ecf20Sopenharmony_ci		phy_power_off(dra7xx->phy[i]);
6888c2ecf20Sopenharmony_ci		phy_exit(dra7xx->phy[i]);
6898c2ecf20Sopenharmony_ci	}
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	return ret;
6928c2ecf20Sopenharmony_ci}
6938c2ecf20Sopenharmony_ci
6948c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra7xx_pcie_rc_of_data = {
6958c2ecf20Sopenharmony_ci	.mode = DW_PCIE_RC_TYPE,
6968c2ecf20Sopenharmony_ci};
6978c2ecf20Sopenharmony_ci
6988c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra7xx_pcie_ep_of_data = {
6998c2ecf20Sopenharmony_ci	.mode = DW_PCIE_EP_TYPE,
7008c2ecf20Sopenharmony_ci};
7018c2ecf20Sopenharmony_ci
7028c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra746_pcie_rc_of_data = {
7038c2ecf20Sopenharmony_ci	.b1co_mode_sel_mask = BIT(2),
7048c2ecf20Sopenharmony_ci	.mode = DW_PCIE_RC_TYPE,
7058c2ecf20Sopenharmony_ci};
7068c2ecf20Sopenharmony_ci
7078c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra726_pcie_rc_of_data = {
7088c2ecf20Sopenharmony_ci	.b1co_mode_sel_mask = GENMASK(3, 2),
7098c2ecf20Sopenharmony_ci	.mode = DW_PCIE_RC_TYPE,
7108c2ecf20Sopenharmony_ci};
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra746_pcie_ep_of_data = {
7138c2ecf20Sopenharmony_ci	.b1co_mode_sel_mask = BIT(2),
7148c2ecf20Sopenharmony_ci	.mode = DW_PCIE_EP_TYPE,
7158c2ecf20Sopenharmony_ci};
7168c2ecf20Sopenharmony_ci
7178c2ecf20Sopenharmony_cistatic const struct dra7xx_pcie_of_data dra726_pcie_ep_of_data = {
7188c2ecf20Sopenharmony_ci	.b1co_mode_sel_mask = GENMASK(3, 2),
7198c2ecf20Sopenharmony_ci	.mode = DW_PCIE_EP_TYPE,
7208c2ecf20Sopenharmony_ci};
7218c2ecf20Sopenharmony_ci
7228c2ecf20Sopenharmony_cistatic const struct of_device_id of_dra7xx_pcie_match[] = {
7238c2ecf20Sopenharmony_ci	{
7248c2ecf20Sopenharmony_ci		.compatible = "ti,dra7-pcie",
7258c2ecf20Sopenharmony_ci		.data = &dra7xx_pcie_rc_of_data,
7268c2ecf20Sopenharmony_ci	},
7278c2ecf20Sopenharmony_ci	{
7288c2ecf20Sopenharmony_ci		.compatible = "ti,dra7-pcie-ep",
7298c2ecf20Sopenharmony_ci		.data = &dra7xx_pcie_ep_of_data,
7308c2ecf20Sopenharmony_ci	},
7318c2ecf20Sopenharmony_ci	{
7328c2ecf20Sopenharmony_ci		.compatible = "ti,dra746-pcie-rc",
7338c2ecf20Sopenharmony_ci		.data = &dra746_pcie_rc_of_data,
7348c2ecf20Sopenharmony_ci	},
7358c2ecf20Sopenharmony_ci	{
7368c2ecf20Sopenharmony_ci		.compatible = "ti,dra726-pcie-rc",
7378c2ecf20Sopenharmony_ci		.data = &dra726_pcie_rc_of_data,
7388c2ecf20Sopenharmony_ci	},
7398c2ecf20Sopenharmony_ci	{
7408c2ecf20Sopenharmony_ci		.compatible = "ti,dra746-pcie-ep",
7418c2ecf20Sopenharmony_ci		.data = &dra746_pcie_ep_of_data,
7428c2ecf20Sopenharmony_ci	},
7438c2ecf20Sopenharmony_ci	{
7448c2ecf20Sopenharmony_ci		.compatible = "ti,dra726-pcie-ep",
7458c2ecf20Sopenharmony_ci		.data = &dra726_pcie_ep_of_data,
7468c2ecf20Sopenharmony_ci	},
7478c2ecf20Sopenharmony_ci	{},
7488c2ecf20Sopenharmony_ci};
7498c2ecf20Sopenharmony_ci
7508c2ecf20Sopenharmony_ci/*
7518c2ecf20Sopenharmony_ci * dra7xx_pcie_unaligned_memaccess: workaround for AM572x/AM571x Errata i870
7528c2ecf20Sopenharmony_ci * @dra7xx: the dra7xx device where the workaround should be applied
7538c2ecf20Sopenharmony_ci *
7548c2ecf20Sopenharmony_ci * Access to the PCIe slave port that are not 32-bit aligned will result
7558c2ecf20Sopenharmony_ci * in incorrect mapping to TLP Address and Byte enable fields. Therefore,
7568c2ecf20Sopenharmony_ci * byte and half-word accesses are not possible to byte offset 0x1, 0x2, or
7578c2ecf20Sopenharmony_ci * 0x3.
7588c2ecf20Sopenharmony_ci *
7598c2ecf20Sopenharmony_ci * To avoid this issue set PCIE_SS1_AXI2OCP_LEGACY_MODE_ENABLE to 1.
7608c2ecf20Sopenharmony_ci */
7618c2ecf20Sopenharmony_cistatic int dra7xx_pcie_unaligned_memaccess(struct device *dev)
7628c2ecf20Sopenharmony_ci{
7638c2ecf20Sopenharmony_ci	int ret;
7648c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
7658c2ecf20Sopenharmony_ci	struct of_phandle_args args;
7668c2ecf20Sopenharmony_ci	struct regmap *regmap;
7678c2ecf20Sopenharmony_ci
7688c2ecf20Sopenharmony_ci	regmap = syscon_regmap_lookup_by_phandle(np,
7698c2ecf20Sopenharmony_ci						 "ti,syscon-unaligned-access");
7708c2ecf20Sopenharmony_ci	if (IS_ERR(regmap)) {
7718c2ecf20Sopenharmony_ci		dev_dbg(dev, "can't get ti,syscon-unaligned-access\n");
7728c2ecf20Sopenharmony_ci		return -EINVAL;
7738c2ecf20Sopenharmony_ci	}
7748c2ecf20Sopenharmony_ci
7758c2ecf20Sopenharmony_ci	ret = of_parse_phandle_with_fixed_args(np, "ti,syscon-unaligned-access",
7768c2ecf20Sopenharmony_ci					       2, 0, &args);
7778c2ecf20Sopenharmony_ci	if (ret) {
7788c2ecf20Sopenharmony_ci		dev_err(dev, "failed to parse ti,syscon-unaligned-access\n");
7798c2ecf20Sopenharmony_ci		return ret;
7808c2ecf20Sopenharmony_ci	}
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci	ret = regmap_update_bits(regmap, args.args[0], args.args[1],
7838c2ecf20Sopenharmony_ci				 args.args[1]);
7848c2ecf20Sopenharmony_ci	if (ret)
7858c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable unaligned access\n");
7868c2ecf20Sopenharmony_ci
7878c2ecf20Sopenharmony_ci	of_node_put(args.np);
7888c2ecf20Sopenharmony_ci
7898c2ecf20Sopenharmony_ci	return ret;
7908c2ecf20Sopenharmony_ci}
7918c2ecf20Sopenharmony_ci
7928c2ecf20Sopenharmony_cistatic int dra7xx_pcie_configure_two_lane(struct device *dev,
7938c2ecf20Sopenharmony_ci					  u32 b1co_mode_sel_mask)
7948c2ecf20Sopenharmony_ci{
7958c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
7968c2ecf20Sopenharmony_ci	struct regmap *pcie_syscon;
7978c2ecf20Sopenharmony_ci	unsigned int pcie_reg;
7988c2ecf20Sopenharmony_ci	u32 mask;
7998c2ecf20Sopenharmony_ci	u32 val;
8008c2ecf20Sopenharmony_ci
8018c2ecf20Sopenharmony_ci	pcie_syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-lane-sel");
8028c2ecf20Sopenharmony_ci	if (IS_ERR(pcie_syscon)) {
8038c2ecf20Sopenharmony_ci		dev_err(dev, "unable to get ti,syscon-lane-sel\n");
8048c2ecf20Sopenharmony_ci		return -EINVAL;
8058c2ecf20Sopenharmony_ci	}
8068c2ecf20Sopenharmony_ci
8078c2ecf20Sopenharmony_ci	if (of_property_read_u32_index(np, "ti,syscon-lane-sel", 1,
8088c2ecf20Sopenharmony_ci				       &pcie_reg)) {
8098c2ecf20Sopenharmony_ci		dev_err(dev, "couldn't get lane selection reg offset\n");
8108c2ecf20Sopenharmony_ci		return -EINVAL;
8118c2ecf20Sopenharmony_ci	}
8128c2ecf20Sopenharmony_ci
8138c2ecf20Sopenharmony_ci	mask = b1co_mode_sel_mask | PCIE_B0_B1_TSYNCEN;
8148c2ecf20Sopenharmony_ci	val = PCIE_B1C0_MODE_SEL | PCIE_B0_B1_TSYNCEN;
8158c2ecf20Sopenharmony_ci	regmap_update_bits(pcie_syscon, pcie_reg, mask, val);
8168c2ecf20Sopenharmony_ci
8178c2ecf20Sopenharmony_ci	return 0;
8188c2ecf20Sopenharmony_ci}
8198c2ecf20Sopenharmony_ci
8208c2ecf20Sopenharmony_cistatic int __init dra7xx_pcie_probe(struct platform_device *pdev)
8218c2ecf20Sopenharmony_ci{
8228c2ecf20Sopenharmony_ci	u32 reg;
8238c2ecf20Sopenharmony_ci	int ret;
8248c2ecf20Sopenharmony_ci	int irq;
8258c2ecf20Sopenharmony_ci	int i;
8268c2ecf20Sopenharmony_ci	int phy_count;
8278c2ecf20Sopenharmony_ci	struct phy **phy;
8288c2ecf20Sopenharmony_ci	struct device_link **link;
8298c2ecf20Sopenharmony_ci	void __iomem *base;
8308c2ecf20Sopenharmony_ci	struct dw_pcie *pci;
8318c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx;
8328c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
8338c2ecf20Sopenharmony_ci	struct device_node *np = dev->of_node;
8348c2ecf20Sopenharmony_ci	char name[10];
8358c2ecf20Sopenharmony_ci	struct gpio_desc *reset;
8368c2ecf20Sopenharmony_ci	const struct of_device_id *match;
8378c2ecf20Sopenharmony_ci	const struct dra7xx_pcie_of_data *data;
8388c2ecf20Sopenharmony_ci	enum dw_pcie_device_mode mode;
8398c2ecf20Sopenharmony_ci	u32 b1co_mode_sel_mask;
8408c2ecf20Sopenharmony_ci
8418c2ecf20Sopenharmony_ci	match = of_match_device(of_match_ptr(of_dra7xx_pcie_match), dev);
8428c2ecf20Sopenharmony_ci	if (!match)
8438c2ecf20Sopenharmony_ci		return -EINVAL;
8448c2ecf20Sopenharmony_ci
8458c2ecf20Sopenharmony_ci	data = (struct dra7xx_pcie_of_data *)match->data;
8468c2ecf20Sopenharmony_ci	mode = (enum dw_pcie_device_mode)data->mode;
8478c2ecf20Sopenharmony_ci	b1co_mode_sel_mask = data->b1co_mode_sel_mask;
8488c2ecf20Sopenharmony_ci
8498c2ecf20Sopenharmony_ci	dra7xx = devm_kzalloc(dev, sizeof(*dra7xx), GFP_KERNEL);
8508c2ecf20Sopenharmony_ci	if (!dra7xx)
8518c2ecf20Sopenharmony_ci		return -ENOMEM;
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
8548c2ecf20Sopenharmony_ci	if (!pci)
8558c2ecf20Sopenharmony_ci		return -ENOMEM;
8568c2ecf20Sopenharmony_ci
8578c2ecf20Sopenharmony_ci	pci->dev = dev;
8588c2ecf20Sopenharmony_ci	pci->ops = &dw_pcie_ops;
8598c2ecf20Sopenharmony_ci
8608c2ecf20Sopenharmony_ci	irq = platform_get_irq(pdev, 0);
8618c2ecf20Sopenharmony_ci	if (irq < 0)
8628c2ecf20Sopenharmony_ci		return irq;
8638c2ecf20Sopenharmony_ci
8648c2ecf20Sopenharmony_ci	base = devm_platform_ioremap_resource_byname(pdev, "ti_conf");
8658c2ecf20Sopenharmony_ci	if (IS_ERR(base))
8668c2ecf20Sopenharmony_ci		return PTR_ERR(base);
8678c2ecf20Sopenharmony_ci
8688c2ecf20Sopenharmony_ci	phy_count = of_property_count_strings(np, "phy-names");
8698c2ecf20Sopenharmony_ci	if (phy_count < 0) {
8708c2ecf20Sopenharmony_ci		dev_err(dev, "unable to find the strings\n");
8718c2ecf20Sopenharmony_ci		return phy_count;
8728c2ecf20Sopenharmony_ci	}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci	phy = devm_kcalloc(dev, phy_count, sizeof(*phy), GFP_KERNEL);
8758c2ecf20Sopenharmony_ci	if (!phy)
8768c2ecf20Sopenharmony_ci		return -ENOMEM;
8778c2ecf20Sopenharmony_ci
8788c2ecf20Sopenharmony_ci	link = devm_kcalloc(dev, phy_count, sizeof(*link), GFP_KERNEL);
8798c2ecf20Sopenharmony_ci	if (!link)
8808c2ecf20Sopenharmony_ci		return -ENOMEM;
8818c2ecf20Sopenharmony_ci
8828c2ecf20Sopenharmony_ci	for (i = 0; i < phy_count; i++) {
8838c2ecf20Sopenharmony_ci		snprintf(name, sizeof(name), "pcie-phy%d", i);
8848c2ecf20Sopenharmony_ci		phy[i] = devm_phy_get(dev, name);
8858c2ecf20Sopenharmony_ci		if (IS_ERR(phy[i]))
8868c2ecf20Sopenharmony_ci			return PTR_ERR(phy[i]);
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_ci		link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS);
8898c2ecf20Sopenharmony_ci		if (!link[i]) {
8908c2ecf20Sopenharmony_ci			ret = -EINVAL;
8918c2ecf20Sopenharmony_ci			goto err_link;
8928c2ecf20Sopenharmony_ci		}
8938c2ecf20Sopenharmony_ci	}
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	dra7xx->base = base;
8968c2ecf20Sopenharmony_ci	dra7xx->phy = phy;
8978c2ecf20Sopenharmony_ci	dra7xx->pci = pci;
8988c2ecf20Sopenharmony_ci	dra7xx->phy_count = phy_count;
8998c2ecf20Sopenharmony_ci
9008c2ecf20Sopenharmony_ci	if (phy_count == 2) {
9018c2ecf20Sopenharmony_ci		ret = dra7xx_pcie_configure_two_lane(dev, b1co_mode_sel_mask);
9028c2ecf20Sopenharmony_ci		if (ret < 0)
9038c2ecf20Sopenharmony_ci			dra7xx->phy_count = 1; /* Fallback to x1 lane mode */
9048c2ecf20Sopenharmony_ci	}
9058c2ecf20Sopenharmony_ci
9068c2ecf20Sopenharmony_ci	ret = dra7xx_pcie_enable_phy(dra7xx);
9078c2ecf20Sopenharmony_ci	if (ret) {
9088c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable phy\n");
9098c2ecf20Sopenharmony_ci		return ret;
9108c2ecf20Sopenharmony_ci	}
9118c2ecf20Sopenharmony_ci
9128c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, dra7xx);
9138c2ecf20Sopenharmony_ci
9148c2ecf20Sopenharmony_ci	pm_runtime_enable(dev);
9158c2ecf20Sopenharmony_ci	ret = pm_runtime_get_sync(dev);
9168c2ecf20Sopenharmony_ci	if (ret < 0) {
9178c2ecf20Sopenharmony_ci		dev_err(dev, "pm_runtime_get_sync failed\n");
9188c2ecf20Sopenharmony_ci		goto err_get_sync;
9198c2ecf20Sopenharmony_ci	}
9208c2ecf20Sopenharmony_ci
9218c2ecf20Sopenharmony_ci	reset = devm_gpiod_get_optional(dev, NULL, GPIOD_OUT_HIGH);
9228c2ecf20Sopenharmony_ci	if (IS_ERR(reset)) {
9238c2ecf20Sopenharmony_ci		ret = PTR_ERR(reset);
9248c2ecf20Sopenharmony_ci		dev_err(&pdev->dev, "gpio request failed, ret %d\n", ret);
9258c2ecf20Sopenharmony_ci		goto err_gpio;
9268c2ecf20Sopenharmony_ci	}
9278c2ecf20Sopenharmony_ci
9288c2ecf20Sopenharmony_ci	reg = dra7xx_pcie_readl(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD);
9298c2ecf20Sopenharmony_ci	reg &= ~LTSSM_EN;
9308c2ecf20Sopenharmony_ci	dra7xx_pcie_writel(dra7xx, PCIECTRL_DRA7XX_CONF_DEVICE_CMD, reg);
9318c2ecf20Sopenharmony_ci
9328c2ecf20Sopenharmony_ci	switch (mode) {
9338c2ecf20Sopenharmony_ci	case DW_PCIE_RC_TYPE:
9348c2ecf20Sopenharmony_ci		if (!IS_ENABLED(CONFIG_PCI_DRA7XX_HOST)) {
9358c2ecf20Sopenharmony_ci			ret = -ENODEV;
9368c2ecf20Sopenharmony_ci			goto err_gpio;
9378c2ecf20Sopenharmony_ci		}
9388c2ecf20Sopenharmony_ci
9398c2ecf20Sopenharmony_ci		dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
9408c2ecf20Sopenharmony_ci				   DEVICE_TYPE_RC);
9418c2ecf20Sopenharmony_ci
9428c2ecf20Sopenharmony_ci		ret = dra7xx_pcie_unaligned_memaccess(dev);
9438c2ecf20Sopenharmony_ci		if (ret)
9448c2ecf20Sopenharmony_ci			dev_err(dev, "WA for Errata i870 not applied\n");
9458c2ecf20Sopenharmony_ci
9468c2ecf20Sopenharmony_ci		ret = dra7xx_add_pcie_port(dra7xx, pdev);
9478c2ecf20Sopenharmony_ci		if (ret < 0)
9488c2ecf20Sopenharmony_ci			goto err_gpio;
9498c2ecf20Sopenharmony_ci		break;
9508c2ecf20Sopenharmony_ci	case DW_PCIE_EP_TYPE:
9518c2ecf20Sopenharmony_ci		if (!IS_ENABLED(CONFIG_PCI_DRA7XX_EP)) {
9528c2ecf20Sopenharmony_ci			ret = -ENODEV;
9538c2ecf20Sopenharmony_ci			goto err_gpio;
9548c2ecf20Sopenharmony_ci		}
9558c2ecf20Sopenharmony_ci
9568c2ecf20Sopenharmony_ci		dra7xx_pcie_writel(dra7xx, PCIECTRL_TI_CONF_DEVICE_TYPE,
9578c2ecf20Sopenharmony_ci				   DEVICE_TYPE_EP);
9588c2ecf20Sopenharmony_ci
9598c2ecf20Sopenharmony_ci		ret = dra7xx_pcie_unaligned_memaccess(dev);
9608c2ecf20Sopenharmony_ci		if (ret)
9618c2ecf20Sopenharmony_ci			goto err_gpio;
9628c2ecf20Sopenharmony_ci
9638c2ecf20Sopenharmony_ci		ret = dra7xx_add_pcie_ep(dra7xx, pdev);
9648c2ecf20Sopenharmony_ci		if (ret < 0)
9658c2ecf20Sopenharmony_ci			goto err_gpio;
9668c2ecf20Sopenharmony_ci		break;
9678c2ecf20Sopenharmony_ci	default:
9688c2ecf20Sopenharmony_ci		dev_err(dev, "INVALID device type %d\n", mode);
9698c2ecf20Sopenharmony_ci	}
9708c2ecf20Sopenharmony_ci	dra7xx->mode = mode;
9718c2ecf20Sopenharmony_ci
9728c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, irq, dra7xx_pcie_irq_handler,
9738c2ecf20Sopenharmony_ci			       IRQF_SHARED, "dra7xx-pcie-main", dra7xx);
9748c2ecf20Sopenharmony_ci	if (ret) {
9758c2ecf20Sopenharmony_ci		dev_err(dev, "failed to request irq\n");
9768c2ecf20Sopenharmony_ci		goto err_gpio;
9778c2ecf20Sopenharmony_ci	}
9788c2ecf20Sopenharmony_ci
9798c2ecf20Sopenharmony_ci	return 0;
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_cierr_gpio:
9828c2ecf20Sopenharmony_cierr_get_sync:
9838c2ecf20Sopenharmony_ci	pm_runtime_put(dev);
9848c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
9858c2ecf20Sopenharmony_ci	dra7xx_pcie_disable_phy(dra7xx);
9868c2ecf20Sopenharmony_ci
9878c2ecf20Sopenharmony_cierr_link:
9888c2ecf20Sopenharmony_ci	while (--i >= 0)
9898c2ecf20Sopenharmony_ci		device_link_del(link[i]);
9908c2ecf20Sopenharmony_ci
9918c2ecf20Sopenharmony_ci	return ret;
9928c2ecf20Sopenharmony_ci}
9938c2ecf20Sopenharmony_ci
9948c2ecf20Sopenharmony_ci#ifdef CONFIG_PM_SLEEP
9958c2ecf20Sopenharmony_cistatic int dra7xx_pcie_suspend(struct device *dev)
9968c2ecf20Sopenharmony_ci{
9978c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
9988c2ecf20Sopenharmony_ci	struct dw_pcie *pci = dra7xx->pci;
9998c2ecf20Sopenharmony_ci	u32 val;
10008c2ecf20Sopenharmony_ci
10018c2ecf20Sopenharmony_ci	if (dra7xx->mode != DW_PCIE_RC_TYPE)
10028c2ecf20Sopenharmony_ci		return 0;
10038c2ecf20Sopenharmony_ci
10048c2ecf20Sopenharmony_ci	/* clear MSE */
10058c2ecf20Sopenharmony_ci	val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
10068c2ecf20Sopenharmony_ci	val &= ~PCI_COMMAND_MEMORY;
10078c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	return 0;
10108c2ecf20Sopenharmony_ci}
10118c2ecf20Sopenharmony_ci
10128c2ecf20Sopenharmony_cistatic int dra7xx_pcie_resume(struct device *dev)
10138c2ecf20Sopenharmony_ci{
10148c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
10158c2ecf20Sopenharmony_ci	struct dw_pcie *pci = dra7xx->pci;
10168c2ecf20Sopenharmony_ci	u32 val;
10178c2ecf20Sopenharmony_ci
10188c2ecf20Sopenharmony_ci	if (dra7xx->mode != DW_PCIE_RC_TYPE)
10198c2ecf20Sopenharmony_ci		return 0;
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci	/* set MSE */
10228c2ecf20Sopenharmony_ci	val = dw_pcie_readl_dbi(pci, PCI_COMMAND);
10238c2ecf20Sopenharmony_ci	val |= PCI_COMMAND_MEMORY;
10248c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCI_COMMAND, val);
10258c2ecf20Sopenharmony_ci
10268c2ecf20Sopenharmony_ci	return 0;
10278c2ecf20Sopenharmony_ci}
10288c2ecf20Sopenharmony_ci
10298c2ecf20Sopenharmony_cistatic int dra7xx_pcie_suspend_noirq(struct device *dev)
10308c2ecf20Sopenharmony_ci{
10318c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
10328c2ecf20Sopenharmony_ci
10338c2ecf20Sopenharmony_ci	dra7xx_pcie_disable_phy(dra7xx);
10348c2ecf20Sopenharmony_ci
10358c2ecf20Sopenharmony_ci	return 0;
10368c2ecf20Sopenharmony_ci}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_cistatic int dra7xx_pcie_resume_noirq(struct device *dev)
10398c2ecf20Sopenharmony_ci{
10408c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
10418c2ecf20Sopenharmony_ci	int ret;
10428c2ecf20Sopenharmony_ci
10438c2ecf20Sopenharmony_ci	ret = dra7xx_pcie_enable_phy(dra7xx);
10448c2ecf20Sopenharmony_ci	if (ret) {
10458c2ecf20Sopenharmony_ci		dev_err(dev, "failed to enable phy\n");
10468c2ecf20Sopenharmony_ci		return ret;
10478c2ecf20Sopenharmony_ci	}
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	return 0;
10508c2ecf20Sopenharmony_ci}
10518c2ecf20Sopenharmony_ci#endif
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_cistatic void dra7xx_pcie_shutdown(struct platform_device *pdev)
10548c2ecf20Sopenharmony_ci{
10558c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
10568c2ecf20Sopenharmony_ci	struct dra7xx_pcie *dra7xx = dev_get_drvdata(dev);
10578c2ecf20Sopenharmony_ci	int ret;
10588c2ecf20Sopenharmony_ci
10598c2ecf20Sopenharmony_ci	dra7xx_pcie_stop_link(dra7xx->pci);
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_ci	ret = pm_runtime_put_sync(dev);
10628c2ecf20Sopenharmony_ci	if (ret < 0)
10638c2ecf20Sopenharmony_ci		dev_dbg(dev, "pm_runtime_put_sync failed\n");
10648c2ecf20Sopenharmony_ci
10658c2ecf20Sopenharmony_ci	pm_runtime_disable(dev);
10668c2ecf20Sopenharmony_ci	dra7xx_pcie_disable_phy(dra7xx);
10678c2ecf20Sopenharmony_ci}
10688c2ecf20Sopenharmony_ci
10698c2ecf20Sopenharmony_cistatic const struct dev_pm_ops dra7xx_pcie_pm_ops = {
10708c2ecf20Sopenharmony_ci	SET_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend, dra7xx_pcie_resume)
10718c2ecf20Sopenharmony_ci	SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(dra7xx_pcie_suspend_noirq,
10728c2ecf20Sopenharmony_ci				      dra7xx_pcie_resume_noirq)
10738c2ecf20Sopenharmony_ci};
10748c2ecf20Sopenharmony_ci
10758c2ecf20Sopenharmony_cistatic struct platform_driver dra7xx_pcie_driver = {
10768c2ecf20Sopenharmony_ci	.driver = {
10778c2ecf20Sopenharmony_ci		.name	= "dra7-pcie",
10788c2ecf20Sopenharmony_ci		.of_match_table = of_dra7xx_pcie_match,
10798c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
10808c2ecf20Sopenharmony_ci		.pm	= &dra7xx_pcie_pm_ops,
10818c2ecf20Sopenharmony_ci	},
10828c2ecf20Sopenharmony_ci	.shutdown = dra7xx_pcie_shutdown,
10838c2ecf20Sopenharmony_ci};
10848c2ecf20Sopenharmony_cibuiltin_platform_driver_probe(dra7xx_pcie_driver, dra7xx_pcie_probe);
1085