18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * PCIe host controller driver for Marvell Armada-8K SoCs
48c2ecf20Sopenharmony_ci *
58c2ecf20Sopenharmony_ci * Armada-8K PCIe Glue Layer Source Code
68c2ecf20Sopenharmony_ci *
78c2ecf20Sopenharmony_ci * Copyright (C) 2016 Marvell Technology Group Ltd.
88c2ecf20Sopenharmony_ci *
98c2ecf20Sopenharmony_ci * Author: Yehuda Yitshak <yehuday@marvell.com>
108c2ecf20Sopenharmony_ci * Author: Shadi Ammouri <shadi@marvell.com>
118c2ecf20Sopenharmony_ci */
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci#include <linux/clk.h>
148c2ecf20Sopenharmony_ci#include <linux/delay.h>
158c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
168c2ecf20Sopenharmony_ci#include <linux/kernel.h>
178c2ecf20Sopenharmony_ci#include <linux/init.h>
188c2ecf20Sopenharmony_ci#include <linux/of.h>
198c2ecf20Sopenharmony_ci#include <linux/pci.h>
208c2ecf20Sopenharmony_ci#include <linux/phy/phy.h>
218c2ecf20Sopenharmony_ci#include <linux/platform_device.h>
228c2ecf20Sopenharmony_ci#include <linux/resource.h>
238c2ecf20Sopenharmony_ci#include <linux/of_pci.h>
248c2ecf20Sopenharmony_ci#include <linux/of_irq.h>
258c2ecf20Sopenharmony_ci
268c2ecf20Sopenharmony_ci#include "pcie-designware.h"
278c2ecf20Sopenharmony_ci
288c2ecf20Sopenharmony_ci#define ARMADA8K_PCIE_MAX_LANES PCIE_LNK_X4
298c2ecf20Sopenharmony_ci
308c2ecf20Sopenharmony_cistruct armada8k_pcie {
318c2ecf20Sopenharmony_ci	struct dw_pcie *pci;
328c2ecf20Sopenharmony_ci	struct clk *clk;
338c2ecf20Sopenharmony_ci	struct clk *clk_reg;
348c2ecf20Sopenharmony_ci	struct phy *phy[ARMADA8K_PCIE_MAX_LANES];
358c2ecf20Sopenharmony_ci	unsigned int phy_count;
368c2ecf20Sopenharmony_ci};
378c2ecf20Sopenharmony_ci
388c2ecf20Sopenharmony_ci#define PCIE_VENDOR_REGS_OFFSET		0x8000
398c2ecf20Sopenharmony_ci
408c2ecf20Sopenharmony_ci#define PCIE_GLOBAL_CONTROL_REG		(PCIE_VENDOR_REGS_OFFSET + 0x0)
418c2ecf20Sopenharmony_ci#define PCIE_APP_LTSSM_EN		BIT(2)
428c2ecf20Sopenharmony_ci#define PCIE_DEVICE_TYPE_SHIFT		4
438c2ecf20Sopenharmony_ci#define PCIE_DEVICE_TYPE_MASK		0xF
448c2ecf20Sopenharmony_ci#define PCIE_DEVICE_TYPE_RC		0x4 /* Root complex */
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_ci#define PCIE_GLOBAL_STATUS_REG		(PCIE_VENDOR_REGS_OFFSET + 0x8)
478c2ecf20Sopenharmony_ci#define PCIE_GLB_STS_RDLH_LINK_UP	BIT(1)
488c2ecf20Sopenharmony_ci#define PCIE_GLB_STS_PHY_LINK_UP	BIT(9)
498c2ecf20Sopenharmony_ci
508c2ecf20Sopenharmony_ci#define PCIE_GLOBAL_INT_CAUSE1_REG	(PCIE_VENDOR_REGS_OFFSET + 0x1C)
518c2ecf20Sopenharmony_ci#define PCIE_GLOBAL_INT_MASK1_REG	(PCIE_VENDOR_REGS_OFFSET + 0x20)
528c2ecf20Sopenharmony_ci#define PCIE_INT_A_ASSERT_MASK		BIT(9)
538c2ecf20Sopenharmony_ci#define PCIE_INT_B_ASSERT_MASK		BIT(10)
548c2ecf20Sopenharmony_ci#define PCIE_INT_C_ASSERT_MASK		BIT(11)
558c2ecf20Sopenharmony_ci#define PCIE_INT_D_ASSERT_MASK		BIT(12)
568c2ecf20Sopenharmony_ci
578c2ecf20Sopenharmony_ci#define PCIE_ARCACHE_TRC_REG		(PCIE_VENDOR_REGS_OFFSET + 0x50)
588c2ecf20Sopenharmony_ci#define PCIE_AWCACHE_TRC_REG		(PCIE_VENDOR_REGS_OFFSET + 0x54)
598c2ecf20Sopenharmony_ci#define PCIE_ARUSER_REG			(PCIE_VENDOR_REGS_OFFSET + 0x5C)
608c2ecf20Sopenharmony_ci#define PCIE_AWUSER_REG			(PCIE_VENDOR_REGS_OFFSET + 0x60)
618c2ecf20Sopenharmony_ci/*
628c2ecf20Sopenharmony_ci * AR/AW Cache defaults: Normal memory, Write-Back, Read / Write
638c2ecf20Sopenharmony_ci * allocate
648c2ecf20Sopenharmony_ci */
658c2ecf20Sopenharmony_ci#define ARCACHE_DEFAULT_VALUE		0x3511
668c2ecf20Sopenharmony_ci#define AWCACHE_DEFAULT_VALUE		0x5311
678c2ecf20Sopenharmony_ci
688c2ecf20Sopenharmony_ci#define DOMAIN_OUTER_SHAREABLE		0x2
698c2ecf20Sopenharmony_ci#define AX_USER_DOMAIN_MASK		0x3
708c2ecf20Sopenharmony_ci#define AX_USER_DOMAIN_SHIFT		4
718c2ecf20Sopenharmony_ci
728c2ecf20Sopenharmony_ci#define to_armada8k_pcie(x)	dev_get_drvdata((x)->dev)
738c2ecf20Sopenharmony_ci
748c2ecf20Sopenharmony_cistatic void armada8k_pcie_disable_phys(struct armada8k_pcie *pcie)
758c2ecf20Sopenharmony_ci{
768c2ecf20Sopenharmony_ci	int i;
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) {
798c2ecf20Sopenharmony_ci		phy_power_off(pcie->phy[i]);
808c2ecf20Sopenharmony_ci		phy_exit(pcie->phy[i]);
818c2ecf20Sopenharmony_ci	}
828c2ecf20Sopenharmony_ci}
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_cistatic int armada8k_pcie_enable_phys(struct armada8k_pcie *pcie)
858c2ecf20Sopenharmony_ci{
868c2ecf20Sopenharmony_ci	int ret;
878c2ecf20Sopenharmony_ci	int i;
888c2ecf20Sopenharmony_ci
898c2ecf20Sopenharmony_ci	for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) {
908c2ecf20Sopenharmony_ci		ret = phy_init(pcie->phy[i]);
918c2ecf20Sopenharmony_ci		if (ret)
928c2ecf20Sopenharmony_ci			return ret;
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci		ret = phy_set_mode_ext(pcie->phy[i], PHY_MODE_PCIE,
958c2ecf20Sopenharmony_ci				       pcie->phy_count);
968c2ecf20Sopenharmony_ci		if (ret) {
978c2ecf20Sopenharmony_ci			phy_exit(pcie->phy[i]);
988c2ecf20Sopenharmony_ci			return ret;
998c2ecf20Sopenharmony_ci		}
1008c2ecf20Sopenharmony_ci
1018c2ecf20Sopenharmony_ci		ret = phy_power_on(pcie->phy[i]);
1028c2ecf20Sopenharmony_ci		if (ret) {
1038c2ecf20Sopenharmony_ci			phy_exit(pcie->phy[i]);
1048c2ecf20Sopenharmony_ci			return ret;
1058c2ecf20Sopenharmony_ci		}
1068c2ecf20Sopenharmony_ci	}
1078c2ecf20Sopenharmony_ci
1088c2ecf20Sopenharmony_ci	return 0;
1098c2ecf20Sopenharmony_ci}
1108c2ecf20Sopenharmony_ci
1118c2ecf20Sopenharmony_cistatic int armada8k_pcie_setup_phys(struct armada8k_pcie *pcie)
1128c2ecf20Sopenharmony_ci{
1138c2ecf20Sopenharmony_ci	struct dw_pcie *pci = pcie->pci;
1148c2ecf20Sopenharmony_ci	struct device *dev = pci->dev;
1158c2ecf20Sopenharmony_ci	struct device_node *node = dev->of_node;
1168c2ecf20Sopenharmony_ci	int ret = 0;
1178c2ecf20Sopenharmony_ci	int i;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	for (i = 0; i < ARMADA8K_PCIE_MAX_LANES; i++) {
1208c2ecf20Sopenharmony_ci		pcie->phy[i] = devm_of_phy_get_by_index(dev, node, i);
1218c2ecf20Sopenharmony_ci		if (IS_ERR(pcie->phy[i])) {
1228c2ecf20Sopenharmony_ci			if (PTR_ERR(pcie->phy[i]) != -ENODEV)
1238c2ecf20Sopenharmony_ci				return PTR_ERR(pcie->phy[i]);
1248c2ecf20Sopenharmony_ci
1258c2ecf20Sopenharmony_ci			pcie->phy[i] = NULL;
1268c2ecf20Sopenharmony_ci			continue;
1278c2ecf20Sopenharmony_ci		}
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_ci		pcie->phy_count++;
1308c2ecf20Sopenharmony_ci	}
1318c2ecf20Sopenharmony_ci
1328c2ecf20Sopenharmony_ci	/* Old bindings miss the PHY handle, so just warn if there is no PHY */
1338c2ecf20Sopenharmony_ci	if (!pcie->phy_count)
1348c2ecf20Sopenharmony_ci		dev_warn(dev, "No available PHY\n");
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ci	ret = armada8k_pcie_enable_phys(pcie);
1378c2ecf20Sopenharmony_ci	if (ret)
1388c2ecf20Sopenharmony_ci		dev_err(dev, "Failed to initialize PHY(s) (%d)\n", ret);
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return ret;
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_cistatic int armada8k_pcie_link_up(struct dw_pcie *pci)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	u32 reg;
1468c2ecf20Sopenharmony_ci	u32 mask = PCIE_GLB_STS_RDLH_LINK_UP | PCIE_GLB_STS_PHY_LINK_UP;
1478c2ecf20Sopenharmony_ci
1488c2ecf20Sopenharmony_ci	reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_STATUS_REG);
1498c2ecf20Sopenharmony_ci
1508c2ecf20Sopenharmony_ci	if ((reg & mask) == mask)
1518c2ecf20Sopenharmony_ci		return 1;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	dev_dbg(pci->dev, "No link detected (Global-Status: 0x%08x).\n", reg);
1548c2ecf20Sopenharmony_ci	return 0;
1558c2ecf20Sopenharmony_ci}
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_cistatic void armada8k_pcie_establish_link(struct armada8k_pcie *pcie)
1588c2ecf20Sopenharmony_ci{
1598c2ecf20Sopenharmony_ci	struct dw_pcie *pci = pcie->pci;
1608c2ecf20Sopenharmony_ci	u32 reg;
1618c2ecf20Sopenharmony_ci
1628c2ecf20Sopenharmony_ci	if (!dw_pcie_link_up(pci)) {
1638c2ecf20Sopenharmony_ci		/* Disable LTSSM state machine to enable configuration */
1648c2ecf20Sopenharmony_ci		reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG);
1658c2ecf20Sopenharmony_ci		reg &= ~(PCIE_APP_LTSSM_EN);
1668c2ecf20Sopenharmony_ci		dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
1678c2ecf20Sopenharmony_ci	}
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	/* Set the device to root complex mode */
1708c2ecf20Sopenharmony_ci	reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG);
1718c2ecf20Sopenharmony_ci	reg &= ~(PCIE_DEVICE_TYPE_MASK << PCIE_DEVICE_TYPE_SHIFT);
1728c2ecf20Sopenharmony_ci	reg |= PCIE_DEVICE_TYPE_RC << PCIE_DEVICE_TYPE_SHIFT;
1738c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	/* Set the PCIe master AxCache attributes */
1768c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_ARCACHE_TRC_REG, ARCACHE_DEFAULT_VALUE);
1778c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_AWCACHE_TRC_REG, AWCACHE_DEFAULT_VALUE);
1788c2ecf20Sopenharmony_ci
1798c2ecf20Sopenharmony_ci	/* Set the PCIe master AxDomain attributes */
1808c2ecf20Sopenharmony_ci	reg = dw_pcie_readl_dbi(pci, PCIE_ARUSER_REG);
1818c2ecf20Sopenharmony_ci	reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
1828c2ecf20Sopenharmony_ci	reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
1838c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_ARUSER_REG, reg);
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci	reg = dw_pcie_readl_dbi(pci, PCIE_AWUSER_REG);
1868c2ecf20Sopenharmony_ci	reg &= ~(AX_USER_DOMAIN_MASK << AX_USER_DOMAIN_SHIFT);
1878c2ecf20Sopenharmony_ci	reg |= DOMAIN_OUTER_SHAREABLE << AX_USER_DOMAIN_SHIFT;
1888c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_AWUSER_REG, reg);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	/* Enable INT A-D interrupts */
1918c2ecf20Sopenharmony_ci	reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_INT_MASK1_REG);
1928c2ecf20Sopenharmony_ci	reg |= PCIE_INT_A_ASSERT_MASK | PCIE_INT_B_ASSERT_MASK |
1938c2ecf20Sopenharmony_ci	       PCIE_INT_C_ASSERT_MASK | PCIE_INT_D_ASSERT_MASK;
1948c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_MASK1_REG, reg);
1958c2ecf20Sopenharmony_ci
1968c2ecf20Sopenharmony_ci	if (!dw_pcie_link_up(pci)) {
1978c2ecf20Sopenharmony_ci		/* Configuration done. Start LTSSM */
1988c2ecf20Sopenharmony_ci		reg = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_CONTROL_REG);
1998c2ecf20Sopenharmony_ci		reg |= PCIE_APP_LTSSM_EN;
2008c2ecf20Sopenharmony_ci		dw_pcie_writel_dbi(pci, PCIE_GLOBAL_CONTROL_REG, reg);
2018c2ecf20Sopenharmony_ci	}
2028c2ecf20Sopenharmony_ci
2038c2ecf20Sopenharmony_ci	/* Wait until the link becomes active again */
2048c2ecf20Sopenharmony_ci	if (dw_pcie_wait_for_link(pci))
2058c2ecf20Sopenharmony_ci		dev_err(pci->dev, "Link not up after reconfiguration\n");
2068c2ecf20Sopenharmony_ci}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_cistatic int armada8k_pcie_host_init(struct pcie_port *pp)
2098c2ecf20Sopenharmony_ci{
2108c2ecf20Sopenharmony_ci	struct dw_pcie *pci = to_dw_pcie_from_pp(pp);
2118c2ecf20Sopenharmony_ci	struct armada8k_pcie *pcie = to_armada8k_pcie(pci);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ci	dw_pcie_setup_rc(pp);
2148c2ecf20Sopenharmony_ci	armada8k_pcie_establish_link(pcie);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ci	return 0;
2178c2ecf20Sopenharmony_ci}
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_cistatic irqreturn_t armada8k_pcie_irq_handler(int irq, void *arg)
2208c2ecf20Sopenharmony_ci{
2218c2ecf20Sopenharmony_ci	struct armada8k_pcie *pcie = arg;
2228c2ecf20Sopenharmony_ci	struct dw_pcie *pci = pcie->pci;
2238c2ecf20Sopenharmony_ci	u32 val;
2248c2ecf20Sopenharmony_ci
2258c2ecf20Sopenharmony_ci	/*
2268c2ecf20Sopenharmony_ci	 * Interrupts are directly handled by the device driver of the
2278c2ecf20Sopenharmony_ci	 * PCI device. However, they are also latched into the PCIe
2288c2ecf20Sopenharmony_ci	 * controller, so we simply discard them.
2298c2ecf20Sopenharmony_ci	 */
2308c2ecf20Sopenharmony_ci	val = dw_pcie_readl_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG);
2318c2ecf20Sopenharmony_ci	dw_pcie_writel_dbi(pci, PCIE_GLOBAL_INT_CAUSE1_REG, val);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
2348c2ecf20Sopenharmony_ci}
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_cistatic const struct dw_pcie_host_ops armada8k_pcie_host_ops = {
2378c2ecf20Sopenharmony_ci	.host_init = armada8k_pcie_host_init,
2388c2ecf20Sopenharmony_ci};
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_cistatic int armada8k_add_pcie_port(struct armada8k_pcie *pcie,
2418c2ecf20Sopenharmony_ci				  struct platform_device *pdev)
2428c2ecf20Sopenharmony_ci{
2438c2ecf20Sopenharmony_ci	struct dw_pcie *pci = pcie->pci;
2448c2ecf20Sopenharmony_ci	struct pcie_port *pp = &pci->pp;
2458c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2468c2ecf20Sopenharmony_ci	int ret;
2478c2ecf20Sopenharmony_ci
2488c2ecf20Sopenharmony_ci	pp->ops = &armada8k_pcie_host_ops;
2498c2ecf20Sopenharmony_ci
2508c2ecf20Sopenharmony_ci	pp->irq = platform_get_irq(pdev, 0);
2518c2ecf20Sopenharmony_ci	if (pp->irq < 0)
2528c2ecf20Sopenharmony_ci		return pp->irq;
2538c2ecf20Sopenharmony_ci
2548c2ecf20Sopenharmony_ci	ret = devm_request_irq(dev, pp->irq, armada8k_pcie_irq_handler,
2558c2ecf20Sopenharmony_ci			       IRQF_SHARED, "armada8k-pcie", pcie);
2568c2ecf20Sopenharmony_ci	if (ret) {
2578c2ecf20Sopenharmony_ci		dev_err(dev, "failed to request irq %d\n", pp->irq);
2588c2ecf20Sopenharmony_ci		return ret;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	ret = dw_pcie_host_init(pp);
2628c2ecf20Sopenharmony_ci	if (ret) {
2638c2ecf20Sopenharmony_ci		dev_err(dev, "failed to initialize host: %d\n", ret);
2648c2ecf20Sopenharmony_ci		return ret;
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci
2678c2ecf20Sopenharmony_ci	return 0;
2688c2ecf20Sopenharmony_ci}
2698c2ecf20Sopenharmony_ci
2708c2ecf20Sopenharmony_cistatic const struct dw_pcie_ops dw_pcie_ops = {
2718c2ecf20Sopenharmony_ci	.link_up = armada8k_pcie_link_up,
2728c2ecf20Sopenharmony_ci};
2738c2ecf20Sopenharmony_ci
2748c2ecf20Sopenharmony_cistatic int armada8k_pcie_probe(struct platform_device *pdev)
2758c2ecf20Sopenharmony_ci{
2768c2ecf20Sopenharmony_ci	struct dw_pcie *pci;
2778c2ecf20Sopenharmony_ci	struct armada8k_pcie *pcie;
2788c2ecf20Sopenharmony_ci	struct device *dev = &pdev->dev;
2798c2ecf20Sopenharmony_ci	struct resource *base;
2808c2ecf20Sopenharmony_ci	int ret;
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ci	pcie = devm_kzalloc(dev, sizeof(*pcie), GFP_KERNEL);
2838c2ecf20Sopenharmony_ci	if (!pcie)
2848c2ecf20Sopenharmony_ci		return -ENOMEM;
2858c2ecf20Sopenharmony_ci
2868c2ecf20Sopenharmony_ci	pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL);
2878c2ecf20Sopenharmony_ci	if (!pci)
2888c2ecf20Sopenharmony_ci		return -ENOMEM;
2898c2ecf20Sopenharmony_ci
2908c2ecf20Sopenharmony_ci	pci->dev = dev;
2918c2ecf20Sopenharmony_ci	pci->ops = &dw_pcie_ops;
2928c2ecf20Sopenharmony_ci
2938c2ecf20Sopenharmony_ci	pcie->pci = pci;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	pcie->clk = devm_clk_get(dev, NULL);
2968c2ecf20Sopenharmony_ci	if (IS_ERR(pcie->clk))
2978c2ecf20Sopenharmony_ci		return PTR_ERR(pcie->clk);
2988c2ecf20Sopenharmony_ci
2998c2ecf20Sopenharmony_ci	ret = clk_prepare_enable(pcie->clk);
3008c2ecf20Sopenharmony_ci	if (ret)
3018c2ecf20Sopenharmony_ci		return ret;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	pcie->clk_reg = devm_clk_get(dev, "reg");
3048c2ecf20Sopenharmony_ci	if (pcie->clk_reg == ERR_PTR(-EPROBE_DEFER)) {
3058c2ecf20Sopenharmony_ci		ret = -EPROBE_DEFER;
3068c2ecf20Sopenharmony_ci		goto fail;
3078c2ecf20Sopenharmony_ci	}
3088c2ecf20Sopenharmony_ci	if (!IS_ERR(pcie->clk_reg)) {
3098c2ecf20Sopenharmony_ci		ret = clk_prepare_enable(pcie->clk_reg);
3108c2ecf20Sopenharmony_ci		if (ret)
3118c2ecf20Sopenharmony_ci			goto fail_clkreg;
3128c2ecf20Sopenharmony_ci	}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_ci	/* Get the dw-pcie unit configuration/control registers base. */
3158c2ecf20Sopenharmony_ci	base = platform_get_resource_byname(pdev, IORESOURCE_MEM, "ctrl");
3168c2ecf20Sopenharmony_ci	pci->dbi_base = devm_pci_remap_cfg_resource(dev, base);
3178c2ecf20Sopenharmony_ci	if (IS_ERR(pci->dbi_base)) {
3188c2ecf20Sopenharmony_ci		ret = PTR_ERR(pci->dbi_base);
3198c2ecf20Sopenharmony_ci		goto fail_clkreg;
3208c2ecf20Sopenharmony_ci	}
3218c2ecf20Sopenharmony_ci
3228c2ecf20Sopenharmony_ci	ret = armada8k_pcie_setup_phys(pcie);
3238c2ecf20Sopenharmony_ci	if (ret)
3248c2ecf20Sopenharmony_ci		goto fail_clkreg;
3258c2ecf20Sopenharmony_ci
3268c2ecf20Sopenharmony_ci	platform_set_drvdata(pdev, pcie);
3278c2ecf20Sopenharmony_ci
3288c2ecf20Sopenharmony_ci	ret = armada8k_add_pcie_port(pcie, pdev);
3298c2ecf20Sopenharmony_ci	if (ret)
3308c2ecf20Sopenharmony_ci		goto disable_phy;
3318c2ecf20Sopenharmony_ci
3328c2ecf20Sopenharmony_ci	return 0;
3338c2ecf20Sopenharmony_ci
3348c2ecf20Sopenharmony_cidisable_phy:
3358c2ecf20Sopenharmony_ci	armada8k_pcie_disable_phys(pcie);
3368c2ecf20Sopenharmony_cifail_clkreg:
3378c2ecf20Sopenharmony_ci	clk_disable_unprepare(pcie->clk_reg);
3388c2ecf20Sopenharmony_cifail:
3398c2ecf20Sopenharmony_ci	clk_disable_unprepare(pcie->clk);
3408c2ecf20Sopenharmony_ci
3418c2ecf20Sopenharmony_ci	return ret;
3428c2ecf20Sopenharmony_ci}
3438c2ecf20Sopenharmony_ci
3448c2ecf20Sopenharmony_cistatic const struct of_device_id armada8k_pcie_of_match[] = {
3458c2ecf20Sopenharmony_ci	{ .compatible = "marvell,armada8k-pcie", },
3468c2ecf20Sopenharmony_ci	{},
3478c2ecf20Sopenharmony_ci};
3488c2ecf20Sopenharmony_ci
3498c2ecf20Sopenharmony_cistatic struct platform_driver armada8k_pcie_driver = {
3508c2ecf20Sopenharmony_ci	.probe		= armada8k_pcie_probe,
3518c2ecf20Sopenharmony_ci	.driver = {
3528c2ecf20Sopenharmony_ci		.name	= "armada8k-pcie",
3538c2ecf20Sopenharmony_ci		.of_match_table = of_match_ptr(armada8k_pcie_of_match),
3548c2ecf20Sopenharmony_ci		.suppress_bind_attrs = true,
3558c2ecf20Sopenharmony_ci	},
3568c2ecf20Sopenharmony_ci};
3578c2ecf20Sopenharmony_cibuiltin_platform_driver(armada8k_pcie_driver);
358