| 1 |
// SPDX-License-Identifier: GPL-2.0 |
| 2 |
/* |
| 3 |
* pcie-sg2042 - PCIe controller driver for Sophgo SG2042 SoC |
| 4 |
* |
| 5 |
* Copyright (C) 2025 Sophgo Technology Inc. |
| 6 |
* Copyright (C) 2025 Chen Wang <[email protected]> |
| 7 |
*/ |
| 8 |
|
| 9 |
#include <linux/mod_devicetable.h> |
| 10 |
#include <linux/pci.h> |
| 11 |
#include <linux/platform_device.h> |
| 12 |
#include <linux/pm_runtime.h> |
| 13 |
|
| 14 |
#include "pcie-cadence.h" |
| 15 |
|
| 16 |
/* |
| 17 |
* SG2042 only supports 4-byte aligned access, so for the rootbus (i.e. to |
| 18 |
* read/write the Root Port itself, read32/write32 is required. For |
| 19 |
* non-rootbus (i.e. to read/write the PCIe peripheral registers, supports |
| 20 |
* 1/2/4 byte aligned access, so directly using read/write should be fine. |
| 21 |
*/ |
| 22 |
|
| 23 |
static struct pci_ops sg2042_pcie_root_ops = { |
| 24 |
.map_bus = cdns_pci_map_bus, |
| 25 |
.read = pci_generic_config_read32, |
| 26 |
.write = pci_generic_config_write32, |
| 27 |
}; |
| 28 |
|
| 29 |
static struct pci_ops sg2042_pcie_child_ops = { |
| 30 |
.map_bus = cdns_pci_map_bus, |
| 31 |
.read = pci_generic_config_read, |
| 32 |
.write = pci_generic_config_write, |
| 33 |
}; |
| 34 |
|
| 35 |
static int sg2042_pcie_probe(struct platform_device *pdev) |
| 36 |
{ |
| 37 |
struct device *dev = &pdev->dev; |
| 38 |
struct pci_host_bridge *bridge; |
| 39 |
struct cdns_pcie *pcie; |
| 40 |
struct cdns_pcie_rc *rc; |
| 41 |
int ret; |
| 42 |
|
| 43 |
bridge = devm_pci_alloc_host_bridge(dev, sizeof(*rc)); |
| 44 |
if (!bridge) |
| 45 |
return dev_err_probe(dev, -ENOMEM, "Failed to alloc host bridge!\n"); |
| 46 |
|
| 47 |
bridge->ops = &sg2042_pcie_root_ops; |
| 48 |
bridge->child_ops = &sg2042_pcie_child_ops; |
| 49 |
|
| 50 |
rc = pci_host_bridge_priv(bridge); |
| 51 |
rc->quirk_broken_aspm_l0s = 1; |
| 52 |
rc->quirk_broken_aspm_l1 = 1; |
| 53 |
pcie = &rc->pcie; |
| 54 |
pcie->dev = dev; |
| 55 |
|
| 56 |
platform_set_drvdata(pdev, pcie); |
| 57 |
|
| 58 |
pm_runtime_set_active(dev); |
| 59 |
pm_runtime_no_callbacks(dev); |
| 60 |
devm_pm_runtime_enable(dev); |
| 61 |
|
| 62 |
ret = cdns_pcie_init_phy(dev, pcie); |
| 63 |
if (ret) |
| 64 |
return dev_err_probe(dev, ret, "Failed to init phy!\n"); |
| 65 |
|
| 66 |
ret = cdns_pcie_host_setup(rc); |
| 67 |
if (ret) { |
| 68 |
dev_err_probe(dev, ret, "Failed to setup host!\n"); |
| 69 |
cdns_pcie_disable_phy(pcie); |
| 70 |
return ret; |
| 71 |
} |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
static void sg2042_pcie_remove(struct platform_device *pdev) |
| 77 |
{ |
| 78 |
struct cdns_pcie *pcie = platform_get_drvdata(pdev); |
| 79 |
struct cdns_pcie_rc *rc; |
| 80 |
|
| 81 |
rc = container_of(pcie, struct cdns_pcie_rc, pcie); |
| 82 |
cdns_pcie_host_disable(rc); |
| 83 |
|
| 84 |
cdns_pcie_disable_phy(pcie); |
| 85 |
} |
| 86 |
|
| 87 |
static int sg2042_pcie_suspend_noirq(struct device *dev) |
| 88 |
{ |
| 89 |
struct cdns_pcie *pcie = dev_get_drvdata(dev); |
| 90 |
|
| 91 |
cdns_pcie_disable_phy(pcie); |
| 92 |
|
| 93 |
return 0; |
| 94 |
} |
| 95 |
|
| 96 |
static int sg2042_pcie_resume_noirq(struct device *dev) |
| 97 |
{ |
| 98 |
struct cdns_pcie *pcie = dev_get_drvdata(dev); |
| 99 |
int ret; |
| 100 |
|
| 101 |
ret = cdns_pcie_enable_phy(pcie); |
| 102 |
if (ret) { |
| 103 |
dev_err(dev, "failed to enable PHY\n"); |
| 104 |
return ret; |
| 105 |
} |
| 106 |
|
| 107 |
return 0; |
| 108 |
} |
| 109 |
|
| 110 |
static DEFINE_NOIRQ_DEV_PM_OPS(sg2042_pcie_pm_ops, |
| 111 |
sg2042_pcie_suspend_noirq, |
| 112 |
sg2042_pcie_resume_noirq); |
| 113 |
|
| 114 |
static const struct of_device_id sg2042_pcie_of_match[] = { |
| 115 |
{ .compatible = "sophgo,sg2042-pcie-host" }, |
| 116 |
{}, |
| 117 |
}; |
| 118 |
MODULE_DEVICE_TABLE(of, sg2042_pcie_of_match); |
| 119 |
|
| 120 |
static struct platform_driver sg2042_pcie_driver = { |
| 121 |
.driver = { |
| 122 |
.name = "sg2042-pcie", |
| 123 |
.of_match_table = sg2042_pcie_of_match, |
| 124 |
.pm = pm_sleep_ptr(&sg2042_pcie_pm_ops), |
| 125 |
}, |
| 126 |
.probe = sg2042_pcie_probe, |
| 127 |
.remove = sg2042_pcie_remove, |
| 128 |
}; |
| 129 |
module_platform_driver(sg2042_pcie_driver); |
| 130 |
|
| 131 |
MODULE_LICENSE("GPL"); |
| 132 |
MODULE_DESCRIPTION("PCIe controller driver for SG2042 SoCs"); |
| 133 |
MODULE_AUTHOR("Chen Wang <[email protected]>"); |