xref: /openbmc/linux/drivers/pci/controller/pcie-iproc-platform.c (revision 2612e3bbc0386368a850140a6c9b990cd496a5ec)
16e0832faSShawn Lin // SPDX-License-Identifier: GPL-2.0
26e0832faSShawn Lin /*
36e0832faSShawn Lin  * Copyright (C) 2015 Broadcom Corporation
46e0832faSShawn Lin  */
56e0832faSShawn Lin 
66e0832faSShawn Lin #include <linux/kernel.h>
76e0832faSShawn Lin #include <linux/pci.h>
86e0832faSShawn Lin #include <linux/clk.h>
96e0832faSShawn Lin #include <linux/module.h>
106e0832faSShawn Lin #include <linux/slab.h>
116e0832faSShawn Lin #include <linux/interrupt.h>
126e0832faSShawn Lin #include <linux/platform_device.h>
136e0832faSShawn Lin #include <linux/of_address.h>
146e0832faSShawn Lin #include <linux/of_pci.h>
156e0832faSShawn Lin #include <linux/of_platform.h>
166e0832faSShawn Lin #include <linux/phy/phy.h>
176e0832faSShawn Lin 
186e0832faSShawn Lin #include "../pci.h"
196e0832faSShawn Lin #include "pcie-iproc.h"
206e0832faSShawn Lin 
216e0832faSShawn Lin static const struct of_device_id iproc_pcie_of_match_table[] = {
226e0832faSShawn Lin 	{
236e0832faSShawn Lin 		.compatible = "brcm,iproc-pcie",
246e0832faSShawn Lin 		.data = (int *)IPROC_PCIE_PAXB,
256e0832faSShawn Lin 	}, {
266e0832faSShawn Lin 		.compatible = "brcm,iproc-pcie-paxb-v2",
276e0832faSShawn Lin 		.data = (int *)IPROC_PCIE_PAXB_V2,
286e0832faSShawn Lin 	}, {
296e0832faSShawn Lin 		.compatible = "brcm,iproc-pcie-paxc",
306e0832faSShawn Lin 		.data = (int *)IPROC_PCIE_PAXC,
316e0832faSShawn Lin 	}, {
326e0832faSShawn Lin 		.compatible = "brcm,iproc-pcie-paxc-v2",
336e0832faSShawn Lin 		.data = (int *)IPROC_PCIE_PAXC_V2,
346e0832faSShawn Lin 	},
356e0832faSShawn Lin 	{ /* sentinel */ }
366e0832faSShawn Lin };
376e0832faSShawn Lin MODULE_DEVICE_TABLE(of, iproc_pcie_of_match_table);
386e0832faSShawn Lin 
iproc_pltfm_pcie_probe(struct platform_device * pdev)3905463a76SBjorn Helgaas static int iproc_pltfm_pcie_probe(struct platform_device *pdev)
406e0832faSShawn Lin {
416e0832faSShawn Lin 	struct device *dev = &pdev->dev;
426e0832faSShawn Lin 	struct iproc_pcie *pcie;
436e0832faSShawn Lin 	struct device_node *np = dev->of_node;
446e0832faSShawn Lin 	struct resource reg;
456e0832faSShawn Lin 	struct pci_host_bridge *bridge;
466e0832faSShawn Lin 	int ret;
476e0832faSShawn Lin 
486e0832faSShawn Lin 	bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie));
496e0832faSShawn Lin 	if (!bridge)
506e0832faSShawn Lin 		return -ENOMEM;
516e0832faSShawn Lin 
526e0832faSShawn Lin 	pcie = pci_host_bridge_priv(bridge);
536e0832faSShawn Lin 
546e0832faSShawn Lin 	pcie->dev = dev;
556e0832faSShawn Lin 	pcie->type = (enum iproc_pcie_type) of_device_get_match_data(dev);
566e0832faSShawn Lin 
576e0832faSShawn Lin 	ret = of_address_to_resource(np, 0, &reg);
586e0832faSShawn Lin 	if (ret < 0) {
596e0832faSShawn Lin 		dev_err(dev, "unable to obtain controller resources\n");
606e0832faSShawn Lin 		return ret;
616e0832faSShawn Lin 	}
626e0832faSShawn Lin 
636e0832faSShawn Lin 	pcie->base = devm_pci_remap_cfgspace(dev, reg.start,
646e0832faSShawn Lin 					     resource_size(&reg));
656e0832faSShawn Lin 	if (!pcie->base) {
666e0832faSShawn Lin 		dev_err(dev, "unable to map controller registers\n");
676e0832faSShawn Lin 		return -ENOMEM;
686e0832faSShawn Lin 	}
696e0832faSShawn Lin 	pcie->base_addr = reg.start;
706e0832faSShawn Lin 
716e0832faSShawn Lin 	if (of_property_read_bool(np, "brcm,pcie-ob")) {
726e0832faSShawn Lin 		u32 val;
736e0832faSShawn Lin 
746e0832faSShawn Lin 		ret = of_property_read_u32(np, "brcm,pcie-ob-axi-offset",
756e0832faSShawn Lin 					   &val);
766e0832faSShawn Lin 		if (ret) {
776e0832faSShawn Lin 			dev_err(dev,
786e0832faSShawn Lin 				"missing brcm,pcie-ob-axi-offset property\n");
796e0832faSShawn Lin 			return ret;
806e0832faSShawn Lin 		}
816e0832faSShawn Lin 		pcie->ob.axi_offset = val;
826e0832faSShawn Lin 		pcie->need_ob_cfg = true;
836e0832faSShawn Lin 	}
846e0832faSShawn Lin 
856e0832faSShawn Lin 	/*
866e0832faSShawn Lin 	 * DT nodes are not used by all platforms that use the iProc PCIe
87f6b6aefeSBjorn Helgaas 	 * core driver. For platforms that require explicit inbound mapping
886e0832faSShawn Lin 	 * configuration, "dma-ranges" would have been present in DT
896e0832faSShawn Lin 	 */
906e0832faSShawn Lin 	pcie->need_ib_cfg = of_property_read_bool(np, "dma-ranges");
916e0832faSShawn Lin 
926e0832faSShawn Lin 	/* PHY use is optional */
93c1e33666SThierry Reding 	pcie->phy = devm_phy_optional_get(dev, "pcie-phy");
94c1e33666SThierry Reding 	if (IS_ERR(pcie->phy))
95c1e33666SThierry Reding 		return PTR_ERR(pcie->phy);
966e0832faSShawn Lin 
976e0832faSShawn Lin 	/* PAXC doesn't support legacy IRQs, skip mapping */
986e0832faSShawn Lin 	switch (pcie->type) {
996e0832faSShawn Lin 	case IPROC_PCIE_PAXC:
1006e0832faSShawn Lin 	case IPROC_PCIE_PAXC_V2:
10193645294SKrzysztof Wilczyński 		pcie->map_irq = NULL;
1026e0832faSShawn Lin 		break;
1036e0832faSShawn Lin 	default:
104b64aa11eSRob Herring 		break;
1056e0832faSShawn Lin 	}
1066e0832faSShawn Lin 
1077ef1c871SRob Herring 	ret = iproc_pcie_setup(pcie, &bridge->windows);
1086e0832faSShawn Lin 	if (ret) {
1096e0832faSShawn Lin 		dev_err(dev, "PCIe controller setup failed\n");
1106e0832faSShawn Lin 		return ret;
1116e0832faSShawn Lin 	}
1126e0832faSShawn Lin 
1136e0832faSShawn Lin 	platform_set_drvdata(pdev, pcie);
1146e0832faSShawn Lin 	return 0;
1156e0832faSShawn Lin }
1166e0832faSShawn Lin 
iproc_pltfm_pcie_remove(struct platform_device * pdev)117*6f1c0a04SUwe Kleine-König static void iproc_pltfm_pcie_remove(struct platform_device *pdev)
1186e0832faSShawn Lin {
1196e0832faSShawn Lin 	struct iproc_pcie *pcie = platform_get_drvdata(pdev);
1206e0832faSShawn Lin 
121*6f1c0a04SUwe Kleine-König 	iproc_pcie_remove(pcie);
1226e0832faSShawn Lin }
1236e0832faSShawn Lin 
iproc_pltfm_pcie_shutdown(struct platform_device * pdev)12405463a76SBjorn Helgaas static void iproc_pltfm_pcie_shutdown(struct platform_device *pdev)
1256e0832faSShawn Lin {
1266e0832faSShawn Lin 	struct iproc_pcie *pcie = platform_get_drvdata(pdev);
1276e0832faSShawn Lin 
1286e0832faSShawn Lin 	iproc_pcie_shutdown(pcie);
1296e0832faSShawn Lin }
1306e0832faSShawn Lin 
13105463a76SBjorn Helgaas static struct platform_driver iproc_pltfm_pcie_driver = {
1326e0832faSShawn Lin 	.driver = {
1336e0832faSShawn Lin 		.name = "iproc-pcie",
1346e0832faSShawn Lin 		.of_match_table = of_match_ptr(iproc_pcie_of_match_table),
1356e0832faSShawn Lin 	},
13605463a76SBjorn Helgaas 	.probe = iproc_pltfm_pcie_probe,
137*6f1c0a04SUwe Kleine-König 	.remove_new = iproc_pltfm_pcie_remove,
13805463a76SBjorn Helgaas 	.shutdown = iproc_pltfm_pcie_shutdown,
1396e0832faSShawn Lin };
14005463a76SBjorn Helgaas module_platform_driver(iproc_pltfm_pcie_driver);
1416e0832faSShawn Lin 
1426e0832faSShawn Lin MODULE_AUTHOR("Ray Jui <rjui@broadcom.com>");
1436e0832faSShawn Lin MODULE_DESCRIPTION("Broadcom iPROC PCIe platform driver");
1446e0832faSShawn Lin MODULE_LICENSE("GPL v2");
145