|
| 1 | +// SPDX-License-Identifier: GPL-2.0 |
| 2 | +/* |
| 3 | + * Driver for the PCIe Controller in QiLai from Andes |
| 4 | + * |
| 5 | + * Copyright (C) 2026 Andes Technology Corporation |
| 6 | + */ |
| 7 | + |
| 8 | +#include <linux/bitfield.h> |
| 9 | +#include <linux/bits.h> |
| 10 | +#include <linux/kernel.h> |
| 11 | +#include <linux/module.h> |
| 12 | +#include <linux/pci.h> |
| 13 | +#include <linux/platform_device.h> |
| 14 | +#include <linux/pm_runtime.h> |
| 15 | +#include <linux/types.h> |
| 16 | + |
| 17 | +#include "pcie-designware.h" |
| 18 | + |
| 19 | +#define PCIE_INTR_CONTROL1 0x15c |
| 20 | +#define PCIE_MSI_CTRL_INT_EN BIT(28) |
| 21 | + |
| 22 | +#define PCIE_LOGIC_COHERENCY_CONTROL3 0x8e8 |
| 23 | + |
| 24 | +/* |
| 25 | + * Refer to Table A4-5 (Memory type encoding) in the |
| 26 | + * AMBA AXI and ACE Protocol Specification. |
| 27 | + * |
| 28 | + * The selected value corresponds to the Memory type field: |
| 29 | + * "Write-back, Read and Write-allocate". |
| 30 | + * |
| 31 | + * The last three rows in the table A4-5 in |
| 32 | + * AMBA AXI and ACE Protocol Specification: |
| 33 | + * ARCACHE AWCACHE Memory type |
| 34 | + * ------------------------------------------------------------------ |
| 35 | + * 1111 (0111) 0111 Write-back Read-allocate |
| 36 | + * 1011 1111 (1011) Write-back Write-allocate |
| 37 | + * 1111 1111 Write-back Read and Write-allocate (selected) |
| 38 | + */ |
| 39 | +#define IOCP_ARCACHE 0b1111 |
| 40 | +#define IOCP_AWCACHE 0b1111 |
| 41 | + |
| 42 | +#define PCIE_CFG_MSTR_ARCACHE_MODE GENMASK(6, 3) |
| 43 | +#define PCIE_CFG_MSTR_AWCACHE_MODE GENMASK(14, 11) |
| 44 | +#define PCIE_CFG_MSTR_ARCACHE_VALUE GENMASK(22, 19) |
| 45 | +#define PCIE_CFG_MSTR_AWCACHE_VALUE GENMASK(30, 27) |
| 46 | + |
| 47 | +#define PCIE_GEN_CONTROL2 0x54 |
| 48 | +#define PCIE_CFG_LTSSM_EN BIT(0) |
| 49 | + |
| 50 | +#define PCIE_REGS_PCIE_SII_PM_STATE 0xc0 |
| 51 | +#define SMLH_LINK_UP BIT(6) |
| 52 | +#define RDLH_LINK_UP BIT(7) |
| 53 | + |
| 54 | +struct qilai_pcie { |
| 55 | + struct dw_pcie pci; |
| 56 | + void __iomem *apb_base; |
| 57 | +}; |
| 58 | + |
| 59 | +#define to_qilai_pcie(_pci) container_of(_pci, struct qilai_pcie, pci) |
| 60 | + |
| 61 | +static bool qilai_pcie_link_up(struct dw_pcie *pci) |
| 62 | +{ |
| 63 | + struct qilai_pcie *pcie = to_qilai_pcie(pci); |
| 64 | + u32 val; |
| 65 | + |
| 66 | + val = readl(pcie->apb_base + PCIE_REGS_PCIE_SII_PM_STATE); |
| 67 | + |
| 68 | + return FIELD_GET(SMLH_LINK_UP, val) && FIELD_GET(RDLH_LINK_UP, val); |
| 69 | +} |
| 70 | + |
| 71 | +static int qilai_pcie_start_link(struct dw_pcie *pci) |
| 72 | +{ |
| 73 | + struct qilai_pcie *pcie = to_qilai_pcie(pci); |
| 74 | + u32 val; |
| 75 | + |
| 76 | + val = readl(pcie->apb_base + PCIE_GEN_CONTROL2); |
| 77 | + val |= PCIE_CFG_LTSSM_EN; |
| 78 | + writel(val, pcie->apb_base + PCIE_GEN_CONTROL2); |
| 79 | + |
| 80 | + return 0; |
| 81 | +} |
| 82 | + |
| 83 | +static const struct dw_pcie_ops qilai_pcie_ops = { |
| 84 | + .link_up = qilai_pcie_link_up, |
| 85 | + .start_link = qilai_pcie_start_link, |
| 86 | +}; |
| 87 | + |
| 88 | +/* |
| 89 | + * Set up the QiLai PCIe IOCP (IO Coherence Port) Read/Write Behaviors to the |
| 90 | + * Write-Back, Read and Write Allocate mode. |
| 91 | + * |
| 92 | + * The IOCP HW target is SoC last-level cache (L2 Cache), which serves as the |
| 93 | + * system cache. The IOCP HW helps maintain cache monitoring, ensuring that |
| 94 | + * the device can snoop data from/to the cache. |
| 95 | + */ |
| 96 | +static void qilai_pcie_iocp_cache_setup(struct dw_pcie_rp *pp) |
| 97 | +{ |
| 98 | + struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 99 | + u32 val; |
| 100 | + |
| 101 | + dw_pcie_dbi_ro_wr_en(pci); |
| 102 | + |
| 103 | + val = dw_pcie_readl_dbi(pci, PCIE_LOGIC_COHERENCY_CONTROL3); |
| 104 | + FIELD_MODIFY(PCIE_CFG_MSTR_ARCACHE_MODE, &val, IOCP_ARCACHE); |
| 105 | + FIELD_MODIFY(PCIE_CFG_MSTR_AWCACHE_MODE, &val, IOCP_AWCACHE); |
| 106 | + FIELD_MODIFY(PCIE_CFG_MSTR_ARCACHE_VALUE, &val, IOCP_ARCACHE); |
| 107 | + FIELD_MODIFY(PCIE_CFG_MSTR_AWCACHE_VALUE, &val, IOCP_AWCACHE); |
| 108 | + dw_pcie_writel_dbi(pci, PCIE_LOGIC_COHERENCY_CONTROL3, val); |
| 109 | + |
| 110 | + dw_pcie_dbi_ro_wr_dis(pci); |
| 111 | +} |
| 112 | + |
| 113 | +static void qilai_pcie_enable_msi(struct qilai_pcie *pcie) |
| 114 | +{ |
| 115 | + u32 val; |
| 116 | + |
| 117 | + val = readl(pcie->apb_base + PCIE_INTR_CONTROL1); |
| 118 | + val |= PCIE_MSI_CTRL_INT_EN; |
| 119 | + writel(val, pcie->apb_base + PCIE_INTR_CONTROL1); |
| 120 | +} |
| 121 | + |
| 122 | +static int qilai_pcie_host_init(struct dw_pcie_rp *pp) |
| 123 | +{ |
| 124 | + struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 125 | + struct qilai_pcie *pcie = to_qilai_pcie(pci); |
| 126 | + |
| 127 | + qilai_pcie_enable_msi(pcie); |
| 128 | + |
| 129 | + return 0; |
| 130 | +} |
| 131 | + |
| 132 | +static void qilai_pcie_host_post_init(struct dw_pcie_rp *pp) |
| 133 | +{ |
| 134 | + qilai_pcie_iocp_cache_setup(pp); |
| 135 | +} |
| 136 | + |
| 137 | +static const struct dw_pcie_host_ops qilai_pcie_host_ops = { |
| 138 | + .init = qilai_pcie_host_init, |
| 139 | + .post_init = qilai_pcie_host_post_init, |
| 140 | +}; |
| 141 | + |
| 142 | +static int qilai_pcie_probe(struct platform_device *pdev) |
| 143 | +{ |
| 144 | + struct qilai_pcie *pcie; |
| 145 | + struct dw_pcie *pci; |
| 146 | + struct device *dev = &pdev->dev; |
| 147 | + int ret; |
| 148 | + |
| 149 | + pcie = devm_kzalloc(&pdev->dev, sizeof(*pcie), GFP_KERNEL); |
| 150 | + if (!pcie) |
| 151 | + return -ENOMEM; |
| 152 | + |
| 153 | + platform_set_drvdata(pdev, pcie); |
| 154 | + |
| 155 | + pci = &pcie->pci; |
| 156 | + pcie->pci.dev = dev; |
| 157 | + pcie->pci.ops = &qilai_pcie_ops; |
| 158 | + pcie->pci.pp.ops = &qilai_pcie_host_ops; |
| 159 | + pci->use_parent_dt_ranges = true; |
| 160 | + |
| 161 | + dw_pcie_cap_set(&pcie->pci, REQ_RES); |
| 162 | + |
| 163 | + pcie->apb_base = devm_platform_ioremap_resource_byname(pdev, "apb"); |
| 164 | + if (IS_ERR(pcie->apb_base)) |
| 165 | + return PTR_ERR(pcie->apb_base); |
| 166 | + |
| 167 | + pm_runtime_set_active(dev); |
| 168 | + pm_runtime_no_callbacks(dev); |
| 169 | + devm_pm_runtime_enable(dev); |
| 170 | + |
| 171 | + ret = dw_pcie_host_init(&pcie->pci.pp); |
| 172 | + if (ret) |
| 173 | + return dev_err_probe(dev, ret, "Failed to initialize PCIe host\n"); |
| 174 | + |
| 175 | + return 0; |
| 176 | +} |
| 177 | + |
| 178 | +static const struct of_device_id qilai_pcie_of_match[] = { |
| 179 | + { .compatible = "andestech,qilai-pcie" }, |
| 180 | + {}, |
| 181 | +}; |
| 182 | +MODULE_DEVICE_TABLE(of, qilai_pcie_of_match); |
| 183 | + |
| 184 | +static struct platform_driver qilai_pcie_driver = { |
| 185 | + .probe = qilai_pcie_probe, |
| 186 | + .driver = { |
| 187 | + .name = "qilai-pcie", |
| 188 | + .of_match_table = qilai_pcie_of_match, |
| 189 | + .probe_type = PROBE_PREFER_ASYNCHRONOUS, |
| 190 | + }, |
| 191 | +}; |
| 192 | + |
| 193 | +builtin_platform_driver(qilai_pcie_driver); |
| 194 | + |
| 195 | +MODULE_AUTHOR("Randolph Lin <randolph@andestech.com>"); |
| 196 | +MODULE_DESCRIPTION("Andes QiLai PCIe driver"); |
| 197 | +MODULE_LICENSE("GPL"); |
0 commit comments