1de80f95cSTom Joseph // SPDX-License-Identifier: GPL-2.0 2de80f95cSTom Joseph /* 3de80f95cSTom Joseph * Cadence PCIe platform driver. 4de80f95cSTom Joseph * 5de80f95cSTom Joseph * Copyright (c) 2019, Cadence Design Systems 6de80f95cSTom Joseph * Author: Tom Joseph <tjoseph@cadence.com> 7de80f95cSTom Joseph */ 8de80f95cSTom Joseph #include <linux/kernel.h> 9de80f95cSTom Joseph #include <linux/of_address.h> 10de80f95cSTom Joseph #include <linux/of_pci.h> 11de80f95cSTom Joseph #include <linux/platform_device.h> 12de80f95cSTom Joseph #include <linux/pm_runtime.h> 13de80f95cSTom Joseph #include <linux/of_device.h> 14de80f95cSTom Joseph #include "pcie-cadence.h" 15de80f95cSTom Joseph 16d07701a1SKishon Vijay Abraham I #define CDNS_PLAT_CPU_TO_BUS_ADDR 0x0FFFFFFF 17d07701a1SKishon Vijay Abraham I 18de80f95cSTom Joseph /** 19de80f95cSTom Joseph * struct cdns_plat_pcie - private data for this PCIe platform driver 20de80f95cSTom Joseph * @pcie: Cadence PCIe controller 21de80f95cSTom Joseph * @is_rc: Set to 1 indicates the PCIe controller mode is Root Complex, 22de80f95cSTom Joseph * if 0 it is in Endpoint mode. 23de80f95cSTom Joseph */ 24de80f95cSTom Joseph struct cdns_plat_pcie { 25de80f95cSTom Joseph struct cdns_pcie *pcie; 26de80f95cSTom Joseph bool is_rc; 27de80f95cSTom Joseph }; 28de80f95cSTom Joseph 29de80f95cSTom Joseph struct cdns_plat_pcie_of_data { 30de80f95cSTom Joseph bool is_rc; 31de80f95cSTom Joseph }; 32de80f95cSTom Joseph 33de80f95cSTom Joseph static const struct of_device_id cdns_plat_pcie_of_match[]; 34de80f95cSTom Joseph 35d07701a1SKishon Vijay Abraham I static u64 cdns_plat_cpu_addr_fixup(struct cdns_pcie *pcie, u64 cpu_addr) 36d07701a1SKishon Vijay Abraham I { 37d07701a1SKishon Vijay Abraham I return cpu_addr & CDNS_PLAT_CPU_TO_BUS_ADDR; 38d07701a1SKishon Vijay Abraham I } 39d07701a1SKishon Vijay Abraham I 40d07701a1SKishon Vijay Abraham I static const struct cdns_pcie_ops cdns_plat_ops = { 41d07701a1SKishon Vijay Abraham I .cpu_addr_fixup = cdns_plat_cpu_addr_fixup, 42d07701a1SKishon Vijay Abraham I }; 43d07701a1SKishon Vijay Abraham I 44de80f95cSTom Joseph static int cdns_plat_pcie_probe(struct platform_device *pdev) 45de80f95cSTom Joseph { 46de80f95cSTom Joseph const struct cdns_plat_pcie_of_data *data; 47de80f95cSTom Joseph struct cdns_plat_pcie *cdns_plat_pcie; 48de80f95cSTom Joseph const struct of_device_id *match; 49de80f95cSTom Joseph struct device *dev = &pdev->dev; 50de80f95cSTom Joseph struct pci_host_bridge *bridge; 51de80f95cSTom Joseph struct cdns_pcie_ep *ep; 52de80f95cSTom Joseph struct cdns_pcie_rc *rc; 53de80f95cSTom Joseph int phy_count; 54de80f95cSTom Joseph bool is_rc; 55de80f95cSTom Joseph int ret; 56de80f95cSTom Joseph 57de80f95cSTom Joseph match = of_match_device(cdns_plat_pcie_of_match, dev); 58de80f95cSTom Joseph if (!match) 59de80f95cSTom Joseph return -EINVAL; 60de80f95cSTom Joseph 61de80f95cSTom Joseph data = (struct cdns_plat_pcie_of_data *)match->data; 62de80f95cSTom Joseph is_rc = data->is_rc; 63de80f95cSTom Joseph 64de80f95cSTom Joseph pr_debug(" Started %s with is_rc: %d\n", __func__, is_rc); 65de80f95cSTom Joseph cdns_plat_pcie = devm_kzalloc(dev, sizeof(*cdns_plat_pcie), GFP_KERNEL); 66de80f95cSTom Joseph if (!cdns_plat_pcie) 67de80f95cSTom Joseph return -ENOMEM; 68de80f95cSTom Joseph 69de80f95cSTom Joseph platform_set_drvdata(pdev, cdns_plat_pcie); 70de80f95cSTom Joseph if (is_rc) { 71de80f95cSTom Joseph if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_HOST)) 72de80f95cSTom Joseph return -ENODEV; 73de80f95cSTom Joseph 74de80f95cSTom Joseph bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc)); 75de80f95cSTom Joseph if (!bridge) 76de80f95cSTom Joseph return -ENOMEM; 77de80f95cSTom Joseph 78de80f95cSTom Joseph rc = pci_host_bridge_priv(bridge); 79de80f95cSTom Joseph rc->pcie.dev = dev; 80d07701a1SKishon Vijay Abraham I rc->pcie.ops = &cdns_plat_ops; 81de80f95cSTom Joseph cdns_plat_pcie->pcie = &rc->pcie; 82de80f95cSTom Joseph cdns_plat_pcie->is_rc = is_rc; 83de80f95cSTom Joseph 84de80f95cSTom Joseph ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie); 85de80f95cSTom Joseph if (ret) { 86de80f95cSTom Joseph dev_err(dev, "failed to init phy\n"); 87de80f95cSTom Joseph return ret; 88de80f95cSTom Joseph } 89de80f95cSTom Joseph pm_runtime_enable(dev); 90de80f95cSTom Joseph ret = pm_runtime_get_sync(dev); 91de80f95cSTom Joseph if (ret < 0) { 92de80f95cSTom Joseph dev_err(dev, "pm_runtime_get_sync() failed\n"); 93de80f95cSTom Joseph goto err_get_sync; 94de80f95cSTom Joseph } 95de80f95cSTom Joseph 96de80f95cSTom Joseph ret = cdns_pcie_host_setup(rc); 97de80f95cSTom Joseph if (ret) 98de80f95cSTom Joseph goto err_init; 99de80f95cSTom Joseph } else { 100de80f95cSTom Joseph if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_EP)) 101de80f95cSTom Joseph return -ENODEV; 102de80f95cSTom Joseph 103de80f95cSTom Joseph ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL); 104de80f95cSTom Joseph if (!ep) 105de80f95cSTom Joseph return -ENOMEM; 106de80f95cSTom Joseph 107de80f95cSTom Joseph ep->pcie.dev = dev; 108d07701a1SKishon Vijay Abraham I ep->pcie.ops = &cdns_plat_ops; 109de80f95cSTom Joseph cdns_plat_pcie->pcie = &ep->pcie; 110de80f95cSTom Joseph cdns_plat_pcie->is_rc = is_rc; 111de80f95cSTom Joseph 112de80f95cSTom Joseph ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie); 113de80f95cSTom Joseph if (ret) { 114de80f95cSTom Joseph dev_err(dev, "failed to init phy\n"); 115de80f95cSTom Joseph return ret; 116de80f95cSTom Joseph } 117de80f95cSTom Joseph 118de80f95cSTom Joseph pm_runtime_enable(dev); 119de80f95cSTom Joseph ret = pm_runtime_get_sync(dev); 120de80f95cSTom Joseph if (ret < 0) { 121de80f95cSTom Joseph dev_err(dev, "pm_runtime_get_sync() failed\n"); 122de80f95cSTom Joseph goto err_get_sync; 123de80f95cSTom Joseph } 124de80f95cSTom Joseph 125de80f95cSTom Joseph ret = cdns_pcie_ep_setup(ep); 126de80f95cSTom Joseph if (ret) 127de80f95cSTom Joseph goto err_init; 128de80f95cSTom Joseph } 129de80f95cSTom Joseph 130*27cd7e3cSLi Chen return 0; 131*27cd7e3cSLi Chen 132de80f95cSTom Joseph err_init: 133de80f95cSTom Joseph err_get_sync: 1347a790087SDinghao Liu pm_runtime_put_sync(dev); 135de80f95cSTom Joseph pm_runtime_disable(dev); 136de80f95cSTom Joseph cdns_pcie_disable_phy(cdns_plat_pcie->pcie); 137de80f95cSTom Joseph phy_count = cdns_plat_pcie->pcie->phy_count; 138de80f95cSTom Joseph while (phy_count--) 139de80f95cSTom Joseph device_link_del(cdns_plat_pcie->pcie->link[phy_count]); 140de80f95cSTom Joseph 141de80f95cSTom Joseph return 0; 142de80f95cSTom Joseph } 143de80f95cSTom Joseph 144de80f95cSTom Joseph static void cdns_plat_pcie_shutdown(struct platform_device *pdev) 145de80f95cSTom Joseph { 146de80f95cSTom Joseph struct device *dev = &pdev->dev; 147de80f95cSTom Joseph struct cdns_pcie *pcie = dev_get_drvdata(dev); 148de80f95cSTom Joseph int ret; 149de80f95cSTom Joseph 150de80f95cSTom Joseph ret = pm_runtime_put_sync(dev); 151de80f95cSTom Joseph if (ret < 0) 152de80f95cSTom Joseph dev_dbg(dev, "pm_runtime_put_sync failed\n"); 153de80f95cSTom Joseph 154de80f95cSTom Joseph pm_runtime_disable(dev); 155de80f95cSTom Joseph 156de80f95cSTom Joseph cdns_pcie_disable_phy(pcie); 157de80f95cSTom Joseph } 158de80f95cSTom Joseph 159de80f95cSTom Joseph static const struct cdns_plat_pcie_of_data cdns_plat_pcie_host_of_data = { 160de80f95cSTom Joseph .is_rc = true, 161de80f95cSTom Joseph }; 162de80f95cSTom Joseph 163de80f95cSTom Joseph static const struct cdns_plat_pcie_of_data cdns_plat_pcie_ep_of_data = { 164de80f95cSTom Joseph .is_rc = false, 165de80f95cSTom Joseph }; 166de80f95cSTom Joseph 167de80f95cSTom Joseph static const struct of_device_id cdns_plat_pcie_of_match[] = { 168de80f95cSTom Joseph { 169de80f95cSTom Joseph .compatible = "cdns,cdns-pcie-host", 170de80f95cSTom Joseph .data = &cdns_plat_pcie_host_of_data, 171de80f95cSTom Joseph }, 172de80f95cSTom Joseph { 173de80f95cSTom Joseph .compatible = "cdns,cdns-pcie-ep", 174de80f95cSTom Joseph .data = &cdns_plat_pcie_ep_of_data, 175de80f95cSTom Joseph }, 176de80f95cSTom Joseph {}, 177de80f95cSTom Joseph }; 178de80f95cSTom Joseph 179de80f95cSTom Joseph static struct platform_driver cdns_plat_pcie_driver = { 180de80f95cSTom Joseph .driver = { 181de80f95cSTom Joseph .name = "cdns-pcie", 182de80f95cSTom Joseph .of_match_table = cdns_plat_pcie_of_match, 183de80f95cSTom Joseph .pm = &cdns_pcie_pm_ops, 184de80f95cSTom Joseph }, 185de80f95cSTom Joseph .probe = cdns_plat_pcie_probe, 186de80f95cSTom Joseph .shutdown = cdns_plat_pcie_shutdown, 187de80f95cSTom Joseph }; 188de80f95cSTom Joseph builtin_platform_driver(cdns_plat_pcie_driver); 189