18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * Cadence PCIe platform driver. 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2019, Cadence Design Systems 68c2ecf20Sopenharmony_ci * Author: Tom Joseph <tjoseph@cadence.com> 78c2ecf20Sopenharmony_ci */ 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/of_address.h> 108c2ecf20Sopenharmony_ci#include <linux/of_pci.h> 118c2ecf20Sopenharmony_ci#include <linux/platform_device.h> 128c2ecf20Sopenharmony_ci#include <linux/pm_runtime.h> 138c2ecf20Sopenharmony_ci#include <linux/of_device.h> 148c2ecf20Sopenharmony_ci#include "pcie-cadence.h" 158c2ecf20Sopenharmony_ci 168c2ecf20Sopenharmony_ci#define CDNS_PLAT_CPU_TO_BUS_ADDR 0x0FFFFFFF 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci/** 198c2ecf20Sopenharmony_ci * struct cdns_plat_pcie - private data for this PCIe platform driver 208c2ecf20Sopenharmony_ci * @pcie: Cadence PCIe controller 218c2ecf20Sopenharmony_ci * @is_rc: Set to 1 indicates the PCIe controller mode is Root Complex, 228c2ecf20Sopenharmony_ci * if 0 it is in Endpoint mode. 238c2ecf20Sopenharmony_ci */ 248c2ecf20Sopenharmony_cistruct cdns_plat_pcie { 258c2ecf20Sopenharmony_ci struct cdns_pcie *pcie; 268c2ecf20Sopenharmony_ci bool is_rc; 278c2ecf20Sopenharmony_ci}; 288c2ecf20Sopenharmony_ci 298c2ecf20Sopenharmony_cistruct cdns_plat_pcie_of_data { 308c2ecf20Sopenharmony_ci bool is_rc; 318c2ecf20Sopenharmony_ci}; 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_cistatic const struct of_device_id cdns_plat_pcie_of_match[]; 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_cistatic u64 cdns_plat_cpu_addr_fixup(struct cdns_pcie *pcie, u64 cpu_addr) 368c2ecf20Sopenharmony_ci{ 378c2ecf20Sopenharmony_ci return cpu_addr & CDNS_PLAT_CPU_TO_BUS_ADDR; 388c2ecf20Sopenharmony_ci} 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistatic const struct cdns_pcie_ops cdns_plat_ops = { 418c2ecf20Sopenharmony_ci .cpu_addr_fixup = cdns_plat_cpu_addr_fixup, 428c2ecf20Sopenharmony_ci}; 438c2ecf20Sopenharmony_ci 448c2ecf20Sopenharmony_cistatic int cdns_plat_pcie_probe(struct platform_device *pdev) 458c2ecf20Sopenharmony_ci{ 468c2ecf20Sopenharmony_ci const struct cdns_plat_pcie_of_data *data; 478c2ecf20Sopenharmony_ci struct cdns_plat_pcie *cdns_plat_pcie; 488c2ecf20Sopenharmony_ci const struct of_device_id *match; 498c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 508c2ecf20Sopenharmony_ci struct pci_host_bridge *bridge; 518c2ecf20Sopenharmony_ci struct cdns_pcie_ep *ep; 528c2ecf20Sopenharmony_ci struct cdns_pcie_rc *rc; 538c2ecf20Sopenharmony_ci int phy_count; 548c2ecf20Sopenharmony_ci bool is_rc; 558c2ecf20Sopenharmony_ci int ret; 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci match = of_match_device(cdns_plat_pcie_of_match, dev); 588c2ecf20Sopenharmony_ci if (!match) 598c2ecf20Sopenharmony_ci return -EINVAL; 608c2ecf20Sopenharmony_ci 618c2ecf20Sopenharmony_ci data = (struct cdns_plat_pcie_of_data *)match->data; 628c2ecf20Sopenharmony_ci is_rc = data->is_rc; 638c2ecf20Sopenharmony_ci 648c2ecf20Sopenharmony_ci pr_debug(" Started %s with is_rc: %d\n", __func__, is_rc); 658c2ecf20Sopenharmony_ci cdns_plat_pcie = devm_kzalloc(dev, sizeof(*cdns_plat_pcie), GFP_KERNEL); 668c2ecf20Sopenharmony_ci if (!cdns_plat_pcie) 678c2ecf20Sopenharmony_ci return -ENOMEM; 688c2ecf20Sopenharmony_ci 698c2ecf20Sopenharmony_ci platform_set_drvdata(pdev, cdns_plat_pcie); 708c2ecf20Sopenharmony_ci if (is_rc) { 718c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_HOST)) 728c2ecf20Sopenharmony_ci return -ENODEV; 738c2ecf20Sopenharmony_ci 748c2ecf20Sopenharmony_ci bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc)); 758c2ecf20Sopenharmony_ci if (!bridge) 768c2ecf20Sopenharmony_ci return -ENOMEM; 778c2ecf20Sopenharmony_ci 788c2ecf20Sopenharmony_ci rc = pci_host_bridge_priv(bridge); 798c2ecf20Sopenharmony_ci rc->pcie.dev = dev; 808c2ecf20Sopenharmony_ci rc->pcie.ops = &cdns_plat_ops; 818c2ecf20Sopenharmony_ci cdns_plat_pcie->pcie = &rc->pcie; 828c2ecf20Sopenharmony_ci cdns_plat_pcie->is_rc = is_rc; 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie); 858c2ecf20Sopenharmony_ci if (ret) { 868c2ecf20Sopenharmony_ci dev_err(dev, "failed to init phy\n"); 878c2ecf20Sopenharmony_ci return ret; 888c2ecf20Sopenharmony_ci } 898c2ecf20Sopenharmony_ci pm_runtime_enable(dev); 908c2ecf20Sopenharmony_ci ret = pm_runtime_get_sync(dev); 918c2ecf20Sopenharmony_ci if (ret < 0) { 928c2ecf20Sopenharmony_ci dev_err(dev, "pm_runtime_get_sync() failed\n"); 938c2ecf20Sopenharmony_ci goto err_get_sync; 948c2ecf20Sopenharmony_ci } 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci ret = cdns_pcie_host_setup(rc); 978c2ecf20Sopenharmony_ci if (ret) 988c2ecf20Sopenharmony_ci goto err_init; 998c2ecf20Sopenharmony_ci } else { 1008c2ecf20Sopenharmony_ci if (!IS_ENABLED(CONFIG_PCIE_CADENCE_PLAT_EP)) 1018c2ecf20Sopenharmony_ci return -ENODEV; 1028c2ecf20Sopenharmony_ci 1038c2ecf20Sopenharmony_ci ep = devm_kzalloc(dev, sizeof(*ep), GFP_KERNEL); 1048c2ecf20Sopenharmony_ci if (!ep) 1058c2ecf20Sopenharmony_ci return -ENOMEM; 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci ep->pcie.dev = dev; 1088c2ecf20Sopenharmony_ci ep->pcie.ops = &cdns_plat_ops; 1098c2ecf20Sopenharmony_ci cdns_plat_pcie->pcie = &ep->pcie; 1108c2ecf20Sopenharmony_ci cdns_plat_pcie->is_rc = is_rc; 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci ret = cdns_pcie_init_phy(dev, cdns_plat_pcie->pcie); 1138c2ecf20Sopenharmony_ci if (ret) { 1148c2ecf20Sopenharmony_ci dev_err(dev, "failed to init phy\n"); 1158c2ecf20Sopenharmony_ci return ret; 1168c2ecf20Sopenharmony_ci } 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci pm_runtime_enable(dev); 1198c2ecf20Sopenharmony_ci ret = pm_runtime_get_sync(dev); 1208c2ecf20Sopenharmony_ci if (ret < 0) { 1218c2ecf20Sopenharmony_ci dev_err(dev, "pm_runtime_get_sync() failed\n"); 1228c2ecf20Sopenharmony_ci goto err_get_sync; 1238c2ecf20Sopenharmony_ci } 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ci ret = cdns_pcie_ep_setup(ep); 1268c2ecf20Sopenharmony_ci if (ret) 1278c2ecf20Sopenharmony_ci goto err_init; 1288c2ecf20Sopenharmony_ci } 1298c2ecf20Sopenharmony_ci 1308c2ecf20Sopenharmony_ci return 0; 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci err_init: 1338c2ecf20Sopenharmony_ci err_get_sync: 1348c2ecf20Sopenharmony_ci pm_runtime_put_sync(dev); 1358c2ecf20Sopenharmony_ci pm_runtime_disable(dev); 1368c2ecf20Sopenharmony_ci cdns_pcie_disable_phy(cdns_plat_pcie->pcie); 1378c2ecf20Sopenharmony_ci phy_count = cdns_plat_pcie->pcie->phy_count; 1388c2ecf20Sopenharmony_ci while (phy_count--) 1398c2ecf20Sopenharmony_ci device_link_del(cdns_plat_pcie->pcie->link[phy_count]); 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci return 0; 1428c2ecf20Sopenharmony_ci} 1438c2ecf20Sopenharmony_ci 1448c2ecf20Sopenharmony_cistatic void cdns_plat_pcie_shutdown(struct platform_device *pdev) 1458c2ecf20Sopenharmony_ci{ 1468c2ecf20Sopenharmony_ci struct device *dev = &pdev->dev; 1478c2ecf20Sopenharmony_ci struct cdns_pcie *pcie = dev_get_drvdata(dev); 1488c2ecf20Sopenharmony_ci int ret; 1498c2ecf20Sopenharmony_ci 1508c2ecf20Sopenharmony_ci ret = pm_runtime_put_sync(dev); 1518c2ecf20Sopenharmony_ci if (ret < 0) 1528c2ecf20Sopenharmony_ci dev_dbg(dev, "pm_runtime_put_sync failed\n"); 1538c2ecf20Sopenharmony_ci 1548c2ecf20Sopenharmony_ci pm_runtime_disable(dev); 1558c2ecf20Sopenharmony_ci 1568c2ecf20Sopenharmony_ci cdns_pcie_disable_phy(pcie); 1578c2ecf20Sopenharmony_ci} 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_cistatic const struct cdns_plat_pcie_of_data cdns_plat_pcie_host_of_data = { 1608c2ecf20Sopenharmony_ci .is_rc = true, 1618c2ecf20Sopenharmony_ci}; 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_cistatic const struct cdns_plat_pcie_of_data cdns_plat_pcie_ep_of_data = { 1648c2ecf20Sopenharmony_ci .is_rc = false, 1658c2ecf20Sopenharmony_ci}; 1668c2ecf20Sopenharmony_ci 1678c2ecf20Sopenharmony_cistatic const struct of_device_id cdns_plat_pcie_of_match[] = { 1688c2ecf20Sopenharmony_ci { 1698c2ecf20Sopenharmony_ci .compatible = "cdns,cdns-pcie-host", 1708c2ecf20Sopenharmony_ci .data = &cdns_plat_pcie_host_of_data, 1718c2ecf20Sopenharmony_ci }, 1728c2ecf20Sopenharmony_ci { 1738c2ecf20Sopenharmony_ci .compatible = "cdns,cdns-pcie-ep", 1748c2ecf20Sopenharmony_ci .data = &cdns_plat_pcie_ep_of_data, 1758c2ecf20Sopenharmony_ci }, 1768c2ecf20Sopenharmony_ci {}, 1778c2ecf20Sopenharmony_ci}; 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_cistatic struct platform_driver cdns_plat_pcie_driver = { 1808c2ecf20Sopenharmony_ci .driver = { 1818c2ecf20Sopenharmony_ci .name = "cdns-pcie", 1828c2ecf20Sopenharmony_ci .of_match_table = cdns_plat_pcie_of_match, 1838c2ecf20Sopenharmony_ci .pm = &cdns_pcie_pm_ops, 1848c2ecf20Sopenharmony_ci }, 1858c2ecf20Sopenharmony_ci .probe = cdns_plat_pcie_probe, 1868c2ecf20Sopenharmony_ci .shutdown = cdns_plat_pcie_shutdown, 1878c2ecf20Sopenharmony_ci}; 1888c2ecf20Sopenharmony_cibuiltin_platform_driver(cdns_plat_pcie_driver); 189