Bjorn Helgaas | 8cfab3c | 2018-01-26 12:50:27 -0600 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 2 | /* |
| 3 | * Copyright Altera Corporation (C) 2013-2015. All rights reserved |
| 4 | * |
Paul Gortmaker | bb9b54c | 2016-08-22 17:59:42 -0400 | [diff] [blame] | 5 | * Author: Ley Foon Tan <lftan@altera.com> |
| 6 | * Description: Altera PCIe host controller driver |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include <linux/delay.h> |
| 10 | #include <linux/interrupt.h> |
| 11 | #include <linux/irqchip/chained_irq.h> |
Paul Gortmaker | bb9b54c | 2016-08-22 17:59:42 -0400 | [diff] [blame] | 12 | #include <linux/init.h> |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 14 | #include <linux/of_address.h> |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 15 | #include <linux/of_device.h> |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 16 | #include <linux/of_irq.h> |
| 17 | #include <linux/of_pci.h> |
| 18 | #include <linux/pci.h> |
| 19 | #include <linux/platform_device.h> |
| 20 | #include <linux/slab.h> |
| 21 | |
Rob Herring | 9e2aee8 | 2018-05-11 12:15:30 -0500 | [diff] [blame] | 22 | #include "../pci.h" |
| 23 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 24 | #define RP_TX_REG0 0x2000 |
| 25 | #define RP_TX_REG1 0x2004 |
| 26 | #define RP_TX_CNTRL 0x2008 |
| 27 | #define RP_TX_EOP 0x2 |
| 28 | #define RP_TX_SOP 0x1 |
| 29 | #define RP_RXCPL_STATUS 0x2010 |
| 30 | #define RP_RXCPL_EOP 0x2 |
| 31 | #define RP_RXCPL_SOP 0x1 |
| 32 | #define RP_RXCPL_REG0 0x2014 |
| 33 | #define RP_RXCPL_REG1 0x2018 |
| 34 | #define P2A_INT_STATUS 0x3060 |
| 35 | #define P2A_INT_STS_ALL 0xf |
| 36 | #define P2A_INT_ENABLE 0x3070 |
| 37 | #define P2A_INT_ENA_ALL 0xf |
| 38 | #define RP_LTSSM 0x3c64 |
Ley Foon Tan | eff31f4 | 2016-03-02 17:43:07 +0800 | [diff] [blame] | 39 | #define RP_LTSSM_MASK 0x1f |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 40 | #define LTSSM_L0 0xf |
| 41 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 42 | #define S10_RP_TX_CNTRL 0x2004 |
| 43 | #define S10_RP_RXCPL_REG 0x2008 |
| 44 | #define S10_RP_RXCPL_STATUS 0x200C |
| 45 | #define S10_RP_CFG_ADDR(pcie, reg) \ |
| 46 | (((pcie)->hip_base) + (reg) + (1 << 20)) |
Ley Foon Tan | 7a28db0 | 2019-06-12 14:42:00 +0800 | [diff] [blame] | 47 | #define S10_RP_SECONDARY(pcie) \ |
| 48 | readb(S10_RP_CFG_ADDR(pcie, PCI_SECONDARY_BUS)) |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 49 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 50 | /* TLP configuration type 0 and 1 */ |
| 51 | #define TLP_FMTTYPE_CFGRD0 0x04 /* Configuration Read Type 0 */ |
| 52 | #define TLP_FMTTYPE_CFGWR0 0x44 /* Configuration Write Type 0 */ |
| 53 | #define TLP_FMTTYPE_CFGRD1 0x05 /* Configuration Read Type 1 */ |
| 54 | #define TLP_FMTTYPE_CFGWR1 0x45 /* Configuration Write Type 1 */ |
| 55 | #define TLP_PAYLOAD_SIZE 0x01 |
| 56 | #define TLP_READ_TAG 0x1d |
| 57 | #define TLP_WRITE_TAG 0x10 |
Bjorn Helgaas | 4f27628 | 2016-10-06 13:29:02 -0500 | [diff] [blame] | 58 | #define RP_DEVFN 0 |
| 59 | #define TLP_REQ_ID(bus, devfn) (((bus) << 8) | (devfn)) |
Ley Foon Tan | 7a28db0 | 2019-06-12 14:42:00 +0800 | [diff] [blame] | 60 | #define TLP_CFG_DW0(pcie, cfg) \ |
| 61 | (((cfg) << 24) | \ |
| 62 | TLP_PAYLOAD_SIZE) |
Bjorn Helgaas | 4f27628 | 2016-10-06 13:29:02 -0500 | [diff] [blame] | 63 | #define TLP_CFG_DW1(pcie, tag, be) \ |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 64 | (((TLP_REQ_ID(pcie->root_bus_nr, RP_DEVFN)) << 16) | (tag << 8) | (be)) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 65 | #define TLP_CFG_DW2(bus, devfn, offset) \ |
| 66 | (((bus) << 24) | ((devfn) << 16) | (offset)) |
Yadi Hu | 8ca6e0a | 2017-02-17 14:20:26 -0600 | [diff] [blame] | 67 | #define TLP_COMP_STATUS(s) (((s) >> 13) & 7) |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 68 | #define TLP_BYTE_COUNT(s) (((s) >> 0) & 0xfff) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 69 | #define TLP_HDR_SIZE 3 |
| 70 | #define TLP_LOOP 500 |
| 71 | |
Ley Foon Tan | 411dc32 | 2016-08-15 14:06:02 +0800 | [diff] [blame] | 72 | #define LINK_UP_TIMEOUT HZ |
| 73 | #define LINK_RETRAIN_TIMEOUT HZ |
Ley Foon Tan | 3a928e9 | 2016-06-21 16:53:13 +0800 | [diff] [blame] | 74 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 75 | #define DWORD_MASK 3 |
| 76 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 77 | #define S10_TLP_FMTTYPE_CFGRD0 0x05 |
| 78 | #define S10_TLP_FMTTYPE_CFGRD1 0x04 |
| 79 | #define S10_TLP_FMTTYPE_CFGWR0 0x45 |
| 80 | #define S10_TLP_FMTTYPE_CFGWR1 0x44 |
| 81 | |
| 82 | enum altera_pcie_version { |
| 83 | ALTERA_PCIE_V1 = 0, |
| 84 | ALTERA_PCIE_V2, |
| 85 | }; |
| 86 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 87 | struct altera_pcie { |
| 88 | struct platform_device *pdev; |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 89 | void __iomem *cra_base; |
| 90 | void __iomem *hip_base; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 91 | int irq; |
| 92 | u8 root_bus_nr; |
| 93 | struct irq_domain *irq_domain; |
| 94 | struct resource bus_range; |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 95 | const struct altera_pcie_data *pcie_data; |
| 96 | }; |
| 97 | |
| 98 | struct altera_pcie_ops { |
| 99 | int (*tlp_read_pkt)(struct altera_pcie *pcie, u32 *value); |
| 100 | void (*tlp_write_pkt)(struct altera_pcie *pcie, u32 *headers, |
| 101 | u32 data, bool align); |
| 102 | bool (*get_link_status)(struct altera_pcie *pcie); |
| 103 | int (*rp_read_cfg)(struct altera_pcie *pcie, int where, |
| 104 | int size, u32 *value); |
| 105 | int (*rp_write_cfg)(struct altera_pcie *pcie, u8 busno, |
| 106 | int where, int size, u32 value); |
| 107 | }; |
| 108 | |
| 109 | struct altera_pcie_data { |
| 110 | const struct altera_pcie_ops *ops; |
| 111 | enum altera_pcie_version version; |
| 112 | u32 cap_offset; /* PCIe capability structure register offset */ |
| 113 | u32 cfgrd0; |
| 114 | u32 cfgrd1; |
| 115 | u32 cfgwr0; |
| 116 | u32 cfgwr1; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 117 | }; |
| 118 | |
| 119 | struct tlp_rp_regpair_t { |
| 120 | u32 ctrl; |
| 121 | u32 reg0; |
| 122 | u32 reg1; |
| 123 | }; |
| 124 | |
Bjorn Helgaas | f8be11ae | 2016-07-22 15:54:41 -0500 | [diff] [blame] | 125 | static inline void cra_writel(struct altera_pcie *pcie, const u32 value, |
| 126 | const u32 reg) |
| 127 | { |
| 128 | writel_relaxed(value, pcie->cra_base + reg); |
| 129 | } |
| 130 | |
| 131 | static inline u32 cra_readl(struct altera_pcie *pcie, const u32 reg) |
| 132 | { |
| 133 | return readl_relaxed(pcie->cra_base + reg); |
| 134 | } |
| 135 | |
Bjorn Helgaas | 499c010 | 2017-11-09 16:17:39 -0600 | [diff] [blame] | 136 | static bool altera_pcie_link_up(struct altera_pcie *pcie) |
Bjorn Helgaas | f8be11ae | 2016-07-22 15:54:41 -0500 | [diff] [blame] | 137 | { |
| 138 | return !!((cra_readl(pcie, RP_LTSSM) & RP_LTSSM_MASK) == LTSSM_L0); |
| 139 | } |
| 140 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 141 | static bool s10_altera_pcie_link_up(struct altera_pcie *pcie) |
| 142 | { |
| 143 | void __iomem *addr = S10_RP_CFG_ADDR(pcie, |
| 144 | pcie->pcie_data->cap_offset + |
| 145 | PCI_EXP_LNKSTA); |
| 146 | |
| 147 | return !!(readw(addr) & PCI_EXP_LNKSTA_DLLLA); |
| 148 | } |
| 149 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 150 | /* |
| 151 | * Altera PCIe port uses BAR0 of RC's configuration space as the translation |
| 152 | * from PCI bus to native BUS. Entire DDR region is mapped into PCIe space |
| 153 | * using these registers, so it can be reached by DMA from EP devices. |
| 154 | * This BAR0 will also access to MSI vector when receiving MSI/MSIX interrupt |
| 155 | * from EP devices, eventually trigger interrupt to GIC. The BAR0 of bridge |
| 156 | * should be hidden during enumeration to avoid the sizing and resource |
| 157 | * allocation by PCIe core. |
| 158 | */ |
| 159 | static bool altera_pcie_hide_rc_bar(struct pci_bus *bus, unsigned int devfn, |
| 160 | int offset) |
| 161 | { |
| 162 | if (pci_is_root_bus(bus) && (devfn == 0) && |
| 163 | (offset == PCI_BASE_ADDRESS_0)) |
| 164 | return true; |
| 165 | |
| 166 | return false; |
| 167 | } |
| 168 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 169 | static void tlp_write_tx(struct altera_pcie *pcie, |
| 170 | struct tlp_rp_regpair_t *tlp_rp_regdata) |
| 171 | { |
| 172 | cra_writel(pcie, tlp_rp_regdata->reg0, RP_TX_REG0); |
| 173 | cra_writel(pcie, tlp_rp_regdata->reg1, RP_TX_REG1); |
| 174 | cra_writel(pcie, tlp_rp_regdata->ctrl, RP_TX_CNTRL); |
| 175 | } |
| 176 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 177 | static void s10_tlp_write_tx(struct altera_pcie *pcie, u32 reg0, u32 ctrl) |
| 178 | { |
| 179 | cra_writel(pcie, reg0, RP_TX_REG0); |
| 180 | cra_writel(pcie, ctrl, S10_RP_TX_CNTRL); |
| 181 | } |
| 182 | |
Bjorn Helgaas | 14c7b95 | 2016-10-06 13:29:03 -0500 | [diff] [blame] | 183 | static bool altera_pcie_valid_device(struct altera_pcie *pcie, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 184 | struct pci_bus *bus, int dev) |
| 185 | { |
| 186 | /* If there is no link, then there is no device */ |
| 187 | if (bus->number != pcie->root_bus_nr) { |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 188 | if (!pcie->pcie_data->ops->get_link_status(pcie)) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 189 | return false; |
| 190 | } |
| 191 | |
| 192 | /* access only one slot on each root port */ |
| 193 | if (bus->number == pcie->root_bus_nr && dev > 0) |
| 194 | return false; |
| 195 | |
Colin Ian King | c96efe2 | 2020-03-27 13:45:56 +0000 | [diff] [blame] | 196 | return true; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | static int tlp_read_packet(struct altera_pcie *pcie, u32 *value) |
| 200 | { |
Dan Carpenter | 7f52f31 | 2015-12-04 16:21:08 -0600 | [diff] [blame] | 201 | int i; |
Gustavo A. R. Silva | db89ed1 | 2018-01-19 21:26:51 -0600 | [diff] [blame] | 202 | bool sop = false; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 203 | u32 ctrl; |
| 204 | u32 reg0, reg1; |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 205 | u32 comp_status = 1; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 206 | |
| 207 | /* |
| 208 | * Minimum 2 loops to read TLP headers and 1 loop to read data |
| 209 | * payload. |
| 210 | */ |
Dan Carpenter | 7f52f31 | 2015-12-04 16:21:08 -0600 | [diff] [blame] | 211 | for (i = 0; i < TLP_LOOP; i++) { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 212 | ctrl = cra_readl(pcie, RP_RXCPL_STATUS); |
| 213 | if ((ctrl & RP_RXCPL_SOP) || (ctrl & RP_RXCPL_EOP) || sop) { |
| 214 | reg0 = cra_readl(pcie, RP_RXCPL_REG0); |
| 215 | reg1 = cra_readl(pcie, RP_RXCPL_REG1); |
| 216 | |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 217 | if (ctrl & RP_RXCPL_SOP) { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 218 | sop = true; |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 219 | comp_status = TLP_COMP_STATUS(reg1); |
| 220 | } |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 221 | |
| 222 | if (ctrl & RP_RXCPL_EOP) { |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 223 | if (comp_status) |
| 224 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 225 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 226 | if (value) |
| 227 | *value = reg0; |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 228 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 229 | return PCIBIOS_SUCCESSFUL; |
| 230 | } |
| 231 | } |
| 232 | udelay(5); |
| 233 | } |
| 234 | |
Ley Foon Tan | ea1d379 | 2015-12-04 16:21:16 -0600 | [diff] [blame] | 235 | return PCIBIOS_DEVICE_NOT_FOUND; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 236 | } |
| 237 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 238 | static int s10_tlp_read_packet(struct altera_pcie *pcie, u32 *value) |
| 239 | { |
| 240 | u32 ctrl; |
| 241 | u32 comp_status; |
| 242 | u32 dw[4]; |
| 243 | u32 count; |
| 244 | struct device *dev = &pcie->pdev->dev; |
| 245 | |
| 246 | for (count = 0; count < TLP_LOOP; count++) { |
| 247 | ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS); |
| 248 | if (ctrl & RP_RXCPL_SOP) { |
| 249 | /* Read first DW */ |
| 250 | dw[0] = cra_readl(pcie, S10_RP_RXCPL_REG); |
| 251 | break; |
| 252 | } |
| 253 | |
| 254 | udelay(5); |
| 255 | } |
| 256 | |
| 257 | /* SOP detection failed, return error */ |
| 258 | if (count == TLP_LOOP) |
| 259 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 260 | |
| 261 | count = 1; |
| 262 | |
| 263 | /* Poll for EOP */ |
| 264 | while (count < ARRAY_SIZE(dw)) { |
| 265 | ctrl = cra_readl(pcie, S10_RP_RXCPL_STATUS); |
| 266 | dw[count++] = cra_readl(pcie, S10_RP_RXCPL_REG); |
| 267 | if (ctrl & RP_RXCPL_EOP) { |
| 268 | comp_status = TLP_COMP_STATUS(dw[1]); |
| 269 | if (comp_status) |
| 270 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 271 | |
| 272 | if (value && TLP_BYTE_COUNT(dw[1]) == sizeof(u32) && |
| 273 | count == 4) |
| 274 | *value = dw[3]; |
| 275 | |
| 276 | return PCIBIOS_SUCCESSFUL; |
| 277 | } |
| 278 | } |
| 279 | |
| 280 | dev_warn(dev, "Malformed TLP packet\n"); |
| 281 | |
| 282 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 283 | } |
| 284 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 285 | static void tlp_write_packet(struct altera_pcie *pcie, u32 *headers, |
| 286 | u32 data, bool align) |
| 287 | { |
| 288 | struct tlp_rp_regpair_t tlp_rp_regdata; |
| 289 | |
| 290 | tlp_rp_regdata.reg0 = headers[0]; |
| 291 | tlp_rp_regdata.reg1 = headers[1]; |
| 292 | tlp_rp_regdata.ctrl = RP_TX_SOP; |
| 293 | tlp_write_tx(pcie, &tlp_rp_regdata); |
| 294 | |
| 295 | if (align) { |
| 296 | tlp_rp_regdata.reg0 = headers[2]; |
| 297 | tlp_rp_regdata.reg1 = 0; |
| 298 | tlp_rp_regdata.ctrl = 0; |
| 299 | tlp_write_tx(pcie, &tlp_rp_regdata); |
| 300 | |
| 301 | tlp_rp_regdata.reg0 = data; |
| 302 | tlp_rp_regdata.reg1 = 0; |
| 303 | } else { |
| 304 | tlp_rp_regdata.reg0 = headers[2]; |
| 305 | tlp_rp_regdata.reg1 = data; |
| 306 | } |
| 307 | |
| 308 | tlp_rp_regdata.ctrl = RP_TX_EOP; |
| 309 | tlp_write_tx(pcie, &tlp_rp_regdata); |
| 310 | } |
| 311 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 312 | static void s10_tlp_write_packet(struct altera_pcie *pcie, u32 *headers, |
| 313 | u32 data, bool dummy) |
| 314 | { |
| 315 | s10_tlp_write_tx(pcie, headers[0], RP_TX_SOP); |
| 316 | s10_tlp_write_tx(pcie, headers[1], 0); |
| 317 | s10_tlp_write_tx(pcie, headers[2], 0); |
| 318 | s10_tlp_write_tx(pcie, data, RP_TX_EOP); |
| 319 | } |
| 320 | |
Ley Foon Tan | 7a28db0 | 2019-06-12 14:42:00 +0800 | [diff] [blame] | 321 | static void get_tlp_header(struct altera_pcie *pcie, u8 bus, u32 devfn, |
| 322 | int where, u8 byte_en, bool read, u32 *headers) |
| 323 | { |
| 324 | u8 cfg; |
| 325 | u8 cfg0 = read ? pcie->pcie_data->cfgrd0 : pcie->pcie_data->cfgwr0; |
| 326 | u8 cfg1 = read ? pcie->pcie_data->cfgrd1 : pcie->pcie_data->cfgwr1; |
| 327 | u8 tag = read ? TLP_READ_TAG : TLP_WRITE_TAG; |
| 328 | |
| 329 | if (pcie->pcie_data->version == ALTERA_PCIE_V1) |
| 330 | cfg = (bus == pcie->root_bus_nr) ? cfg0 : cfg1; |
| 331 | else |
| 332 | cfg = (bus > S10_RP_SECONDARY(pcie)) ? cfg0 : cfg1; |
| 333 | |
| 334 | headers[0] = TLP_CFG_DW0(pcie, cfg); |
| 335 | headers[1] = TLP_CFG_DW1(pcie, tag, byte_en); |
| 336 | headers[2] = TLP_CFG_DW2(bus, devfn, where); |
| 337 | } |
| 338 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 339 | static int tlp_cfg_dword_read(struct altera_pcie *pcie, u8 bus, u32 devfn, |
| 340 | int where, u8 byte_en, u32 *value) |
| 341 | { |
| 342 | u32 headers[TLP_HDR_SIZE]; |
| 343 | |
Ley Foon Tan | 7a28db0 | 2019-06-12 14:42:00 +0800 | [diff] [blame] | 344 | get_tlp_header(pcie, bus, devfn, where, byte_en, true, |
| 345 | headers); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 346 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 347 | pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, 0, false); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 348 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 349 | return pcie->pcie_data->ops->tlp_read_pkt(pcie, value); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static int tlp_cfg_dword_write(struct altera_pcie *pcie, u8 bus, u32 devfn, |
| 353 | int where, u8 byte_en, u32 value) |
| 354 | { |
| 355 | u32 headers[TLP_HDR_SIZE]; |
| 356 | int ret; |
| 357 | |
Ley Foon Tan | 7a28db0 | 2019-06-12 14:42:00 +0800 | [diff] [blame] | 358 | get_tlp_header(pcie, bus, devfn, where, byte_en, false, |
| 359 | headers); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 360 | |
| 361 | /* check alignment to Qword */ |
| 362 | if ((where & 0x7) == 0) |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 363 | pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, |
| 364 | value, true); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 365 | else |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 366 | pcie->pcie_data->ops->tlp_write_pkt(pcie, headers, |
| 367 | value, false); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 368 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 369 | ret = pcie->pcie_data->ops->tlp_read_pkt(pcie, NULL); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 370 | if (ret != PCIBIOS_SUCCESSFUL) |
| 371 | return ret; |
| 372 | |
| 373 | /* |
| 374 | * Monitor changes to PCI_PRIMARY_BUS register on root port |
| 375 | * and update local copy of root bus number accordingly. |
| 376 | */ |
| 377 | if ((bus == pcie->root_bus_nr) && (where == PCI_PRIMARY_BUS)) |
| 378 | pcie->root_bus_nr = (u8)(value); |
| 379 | |
| 380 | return PCIBIOS_SUCCESSFUL; |
| 381 | } |
| 382 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 383 | static int s10_rp_read_cfg(struct altera_pcie *pcie, int where, |
| 384 | int size, u32 *value) |
| 385 | { |
| 386 | void __iomem *addr = S10_RP_CFG_ADDR(pcie, where); |
| 387 | |
| 388 | switch (size) { |
| 389 | case 1: |
| 390 | *value = readb(addr); |
| 391 | break; |
| 392 | case 2: |
| 393 | *value = readw(addr); |
| 394 | break; |
| 395 | default: |
| 396 | *value = readl(addr); |
| 397 | break; |
| 398 | } |
| 399 | |
| 400 | return PCIBIOS_SUCCESSFUL; |
| 401 | } |
| 402 | |
| 403 | static int s10_rp_write_cfg(struct altera_pcie *pcie, u8 busno, |
| 404 | int where, int size, u32 value) |
| 405 | { |
| 406 | void __iomem *addr = S10_RP_CFG_ADDR(pcie, where); |
| 407 | |
| 408 | switch (size) { |
| 409 | case 1: |
| 410 | writeb(value, addr); |
| 411 | break; |
| 412 | case 2: |
| 413 | writew(value, addr); |
| 414 | break; |
| 415 | default: |
| 416 | writel(value, addr); |
| 417 | break; |
| 418 | } |
| 419 | |
| 420 | /* |
| 421 | * Monitor changes to PCI_PRIMARY_BUS register on root port |
| 422 | * and update local copy of root bus number accordingly. |
| 423 | */ |
| 424 | if (busno == pcie->root_bus_nr && where == PCI_PRIMARY_BUS) |
| 425 | pcie->root_bus_nr = value & 0xff; |
| 426 | |
| 427 | return PCIBIOS_SUCCESSFUL; |
| 428 | } |
| 429 | |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 430 | static int _altera_pcie_cfg_read(struct altera_pcie *pcie, u8 busno, |
| 431 | unsigned int devfn, int where, int size, |
| 432 | u32 *value) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 433 | { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 434 | int ret; |
| 435 | u32 data; |
| 436 | u8 byte_en; |
| 437 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 438 | if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_read_cfg) |
| 439 | return pcie->pcie_data->ops->rp_read_cfg(pcie, where, |
| 440 | size, value); |
| 441 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 442 | switch (size) { |
| 443 | case 1: |
| 444 | byte_en = 1 << (where & 3); |
| 445 | break; |
| 446 | case 2: |
| 447 | byte_en = 3 << (where & 3); |
| 448 | break; |
| 449 | default: |
| 450 | byte_en = 0xf; |
| 451 | break; |
| 452 | } |
| 453 | |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 454 | ret = tlp_cfg_dword_read(pcie, busno, devfn, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 455 | (where & ~DWORD_MASK), byte_en, &data); |
| 456 | if (ret != PCIBIOS_SUCCESSFUL) |
| 457 | return ret; |
| 458 | |
| 459 | switch (size) { |
| 460 | case 1: |
| 461 | *value = (data >> (8 * (where & 0x3))) & 0xff; |
| 462 | break; |
| 463 | case 2: |
| 464 | *value = (data >> (8 * (where & 0x2))) & 0xffff; |
| 465 | break; |
| 466 | default: |
| 467 | *value = data; |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | return PCIBIOS_SUCCESSFUL; |
| 472 | } |
| 473 | |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 474 | static int _altera_pcie_cfg_write(struct altera_pcie *pcie, u8 busno, |
| 475 | unsigned int devfn, int where, int size, |
| 476 | u32 value) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 477 | { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 478 | u32 data32; |
| 479 | u32 shift = 8 * (where & 3); |
| 480 | u8 byte_en; |
| 481 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 482 | if (busno == pcie->root_bus_nr && pcie->pcie_data->ops->rp_write_cfg) |
| 483 | return pcie->pcie_data->ops->rp_write_cfg(pcie, busno, |
| 484 | where, size, value); |
| 485 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 486 | switch (size) { |
| 487 | case 1: |
| 488 | data32 = (value & 0xff) << shift; |
| 489 | byte_en = 1 << (where & 3); |
| 490 | break; |
| 491 | case 2: |
| 492 | data32 = (value & 0xffff) << shift; |
| 493 | byte_en = 3 << (where & 3); |
| 494 | break; |
| 495 | default: |
| 496 | data32 = value; |
| 497 | byte_en = 0xf; |
| 498 | break; |
| 499 | } |
| 500 | |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 501 | return tlp_cfg_dword_write(pcie, busno, devfn, (where & ~DWORD_MASK), |
| 502 | byte_en, data32); |
| 503 | } |
| 504 | |
| 505 | static int altera_pcie_cfg_read(struct pci_bus *bus, unsigned int devfn, |
| 506 | int where, int size, u32 *value) |
| 507 | { |
| 508 | struct altera_pcie *pcie = bus->sysdata; |
| 509 | |
| 510 | if (altera_pcie_hide_rc_bar(bus, devfn, where)) |
| 511 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 512 | |
Naveen Naidu | 5f09342 | 2021-11-18 19:33:23 +0530 | [diff] [blame] | 513 | if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 514 | return PCIBIOS_DEVICE_NOT_FOUND; |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 515 | |
| 516 | return _altera_pcie_cfg_read(pcie, bus->number, devfn, where, size, |
| 517 | value); |
| 518 | } |
| 519 | |
| 520 | static int altera_pcie_cfg_write(struct pci_bus *bus, unsigned int devfn, |
| 521 | int where, int size, u32 value) |
| 522 | { |
| 523 | struct altera_pcie *pcie = bus->sysdata; |
| 524 | |
| 525 | if (altera_pcie_hide_rc_bar(bus, devfn, where)) |
| 526 | return PCIBIOS_BAD_REGISTER_NUMBER; |
| 527 | |
Bjorn Helgaas | 14c7b95 | 2016-10-06 13:29:03 -0500 | [diff] [blame] | 528 | if (!altera_pcie_valid_device(pcie, bus, PCI_SLOT(devfn))) |
Ley Foon Tan | 31fc0ad | 2016-08-26 09:47:24 +0800 | [diff] [blame] | 529 | return PCIBIOS_DEVICE_NOT_FOUND; |
| 530 | |
| 531 | return _altera_pcie_cfg_write(pcie, bus->number, devfn, where, size, |
| 532 | value); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 533 | } |
| 534 | |
| 535 | static struct pci_ops altera_pcie_ops = { |
| 536 | .read = altera_pcie_cfg_read, |
| 537 | .write = altera_pcie_cfg_write, |
| 538 | }; |
| 539 | |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 540 | static int altera_read_cap_word(struct altera_pcie *pcie, u8 busno, |
| 541 | unsigned int devfn, int offset, u16 *value) |
| 542 | { |
| 543 | u32 data; |
| 544 | int ret; |
| 545 | |
| 546 | ret = _altera_pcie_cfg_read(pcie, busno, devfn, |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 547 | pcie->pcie_data->cap_offset + offset, |
| 548 | sizeof(*value), |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 549 | &data); |
| 550 | *value = data; |
| 551 | return ret; |
| 552 | } |
| 553 | |
| 554 | static int altera_write_cap_word(struct altera_pcie *pcie, u8 busno, |
| 555 | unsigned int devfn, int offset, u16 value) |
| 556 | { |
| 557 | return _altera_pcie_cfg_write(pcie, busno, devfn, |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 558 | pcie->pcie_data->cap_offset + offset, |
| 559 | sizeof(value), |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 560 | value); |
| 561 | } |
| 562 | |
| 563 | static void altera_wait_link_retrain(struct altera_pcie *pcie) |
| 564 | { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 565 | struct device *dev = &pcie->pdev->dev; |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 566 | u16 reg16; |
| 567 | unsigned long start_jiffies; |
| 568 | |
| 569 | /* Wait for link training end. */ |
| 570 | start_jiffies = jiffies; |
| 571 | for (;;) { |
| 572 | altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, |
| 573 | PCI_EXP_LNKSTA, ®16); |
| 574 | if (!(reg16 & PCI_EXP_LNKSTA_LT)) |
| 575 | break; |
| 576 | |
| 577 | if (time_after(jiffies, start_jiffies + LINK_RETRAIN_TIMEOUT)) { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 578 | dev_err(dev, "link retrain timeout\n"); |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 579 | break; |
| 580 | } |
| 581 | udelay(100); |
| 582 | } |
| 583 | |
| 584 | /* Wait for link is up */ |
| 585 | start_jiffies = jiffies; |
| 586 | for (;;) { |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 587 | if (pcie->pcie_data->ops->get_link_status(pcie)) |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 588 | break; |
| 589 | |
| 590 | if (time_after(jiffies, start_jiffies + LINK_UP_TIMEOUT)) { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 591 | dev_err(dev, "link up timeout\n"); |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 592 | break; |
| 593 | } |
| 594 | udelay(100); |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | static void altera_pcie_retrain(struct altera_pcie *pcie) |
| 599 | { |
| 600 | u16 linkcap, linkstat, linkctl; |
| 601 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 602 | if (!pcie->pcie_data->ops->get_link_status(pcie)) |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 603 | return; |
| 604 | |
| 605 | /* |
| 606 | * Set the retrain bit if the PCIe rootport support > 2.5GB/s, but |
| 607 | * current speed is 2.5 GB/s. |
| 608 | */ |
| 609 | altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKCAP, |
| 610 | &linkcap); |
| 611 | if ((linkcap & PCI_EXP_LNKCAP_SLS) <= PCI_EXP_LNKCAP_SLS_2_5GB) |
| 612 | return; |
| 613 | |
| 614 | altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, PCI_EXP_LNKSTA, |
| 615 | &linkstat); |
| 616 | if ((linkstat & PCI_EXP_LNKSTA_CLS) == PCI_EXP_LNKSTA_CLS_2_5GB) { |
| 617 | altera_read_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, |
| 618 | PCI_EXP_LNKCTL, &linkctl); |
| 619 | linkctl |= PCI_EXP_LNKCTL_RL; |
| 620 | altera_write_cap_word(pcie, pcie->root_bus_nr, RP_DEVFN, |
| 621 | PCI_EXP_LNKCTL, linkctl); |
| 622 | |
| 623 | altera_wait_link_retrain(pcie); |
| 624 | } |
| 625 | } |
| 626 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 627 | static int altera_pcie_intx_map(struct irq_domain *domain, unsigned int irq, |
| 628 | irq_hw_number_t hwirq) |
| 629 | { |
| 630 | irq_set_chip_and_handler(irq, &dummy_irq_chip, handle_simple_irq); |
| 631 | irq_set_chip_data(irq, domain->host_data); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 632 | return 0; |
| 633 | } |
| 634 | |
| 635 | static const struct irq_domain_ops intx_domain_ops = { |
| 636 | .map = altera_pcie_intx_map, |
Paul Burton | bfdbbf0 | 2017-08-15 16:24:38 -0500 | [diff] [blame] | 637 | .xlate = pci_irqd_intx_xlate, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 638 | }; |
| 639 | |
| 640 | static void altera_pcie_isr(struct irq_desc *desc) |
| 641 | { |
| 642 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 643 | struct altera_pcie *pcie; |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 644 | struct device *dev; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 645 | unsigned long status; |
| 646 | u32 bit; |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 647 | int ret; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 648 | |
| 649 | chained_irq_enter(chip, desc); |
| 650 | pcie = irq_desc_get_handler_data(desc); |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 651 | dev = &pcie->pdev->dev; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 652 | |
| 653 | while ((status = cra_readl(pcie, P2A_INT_STATUS) |
| 654 | & P2A_INT_STS_ALL) != 0) { |
Paul Burton | bfdbbf0 | 2017-08-15 16:24:38 -0500 | [diff] [blame] | 655 | for_each_set_bit(bit, &status, PCI_NUM_INTX) { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 656 | /* clear interrupts */ |
| 657 | cra_writel(pcie, 1 << bit, P2A_INT_STATUS); |
| 658 | |
Marc Zyngier | d21faba1 | 2021-08-02 17:26:19 +0100 | [diff] [blame] | 659 | ret = generic_handle_domain_irq(pcie->irq_domain, bit); |
| 660 | if (ret) |
| 661 | dev_err_ratelimited(dev, "unexpected IRQ, INT%d\n", bit); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 662 | } |
| 663 | } |
| 664 | |
| 665 | chained_irq_exit(chip, desc); |
| 666 | } |
| 667 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 668 | static int altera_pcie_init_irq_domain(struct altera_pcie *pcie) |
| 669 | { |
| 670 | struct device *dev = &pcie->pdev->dev; |
| 671 | struct device_node *node = dev->of_node; |
| 672 | |
| 673 | /* Setup INTx */ |
Paul Burton | bfdbbf0 | 2017-08-15 16:24:38 -0500 | [diff] [blame] | 674 | pcie->irq_domain = irq_domain_add_linear(node, PCI_NUM_INTX, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 675 | &intx_domain_ops, pcie); |
| 676 | if (!pcie->irq_domain) { |
| 677 | dev_err(dev, "Failed to get a INTx IRQ domain\n"); |
| 678 | return -ENOMEM; |
| 679 | } |
| 680 | |
| 681 | return 0; |
| 682 | } |
| 683 | |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 684 | static void altera_pcie_irq_teardown(struct altera_pcie *pcie) |
| 685 | { |
| 686 | irq_set_chained_handler_and_data(pcie->irq, NULL, NULL); |
| 687 | irq_domain_remove(pcie->irq_domain); |
| 688 | irq_dispose_mapping(pcie->irq); |
| 689 | } |
| 690 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 691 | static int altera_pcie_parse_dt(struct altera_pcie *pcie) |
| 692 | { |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 693 | struct platform_device *pdev = pcie->pdev; |
| 694 | |
Dejin Zheng | e2dcd20 | 2020-06-03 01:16:01 +0800 | [diff] [blame] | 695 | pcie->cra_base = devm_platform_ioremap_resource_byname(pdev, "Cra"); |
Wei Yongjun | c19699a | 2016-10-17 14:56:13 +0000 | [diff] [blame] | 696 | if (IS_ERR(pcie->cra_base)) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 697 | return PTR_ERR(pcie->cra_base); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 698 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 699 | if (pcie->pcie_data->version == ALTERA_PCIE_V2) { |
Dejin Zheng | e2dcd20 | 2020-06-03 01:16:01 +0800 | [diff] [blame] | 700 | pcie->hip_base = |
| 701 | devm_platform_ioremap_resource_byname(pdev, "Hip"); |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 702 | if (IS_ERR(pcie->hip_base)) |
| 703 | return PTR_ERR(pcie->hip_base); |
| 704 | } |
| 705 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 706 | /* setup IRQ */ |
| 707 | pcie->irq = platform_get_irq(pdev, 0); |
Krzysztof Wilczyński | caecb05 | 2020-08-02 14:25:53 +0000 | [diff] [blame] | 708 | if (pcie->irq < 0) |
Fabio Estevam | ef75369 | 2017-08-31 14:52:07 -0300 | [diff] [blame] | 709 | return pcie->irq; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 710 | |
| 711 | irq_set_chained_handler_and_data(pcie->irq, altera_pcie_isr, pcie); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 712 | return 0; |
| 713 | } |
| 714 | |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 715 | static void altera_pcie_host_init(struct altera_pcie *pcie) |
| 716 | { |
| 717 | altera_pcie_retrain(pcie); |
| 718 | } |
| 719 | |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 720 | static const struct altera_pcie_ops altera_pcie_ops_1_0 = { |
| 721 | .tlp_read_pkt = tlp_read_packet, |
| 722 | .tlp_write_pkt = tlp_write_packet, |
| 723 | .get_link_status = altera_pcie_link_up, |
| 724 | }; |
| 725 | |
| 726 | static const struct altera_pcie_ops altera_pcie_ops_2_0 = { |
| 727 | .tlp_read_pkt = s10_tlp_read_packet, |
| 728 | .tlp_write_pkt = s10_tlp_write_packet, |
| 729 | .get_link_status = s10_altera_pcie_link_up, |
| 730 | .rp_read_cfg = s10_rp_read_cfg, |
| 731 | .rp_write_cfg = s10_rp_write_cfg, |
| 732 | }; |
| 733 | |
| 734 | static const struct altera_pcie_data altera_pcie_1_0_data = { |
| 735 | .ops = &altera_pcie_ops_1_0, |
| 736 | .cap_offset = 0x80, |
| 737 | .version = ALTERA_PCIE_V1, |
| 738 | .cfgrd0 = TLP_FMTTYPE_CFGRD0, |
| 739 | .cfgrd1 = TLP_FMTTYPE_CFGRD1, |
| 740 | .cfgwr0 = TLP_FMTTYPE_CFGWR0, |
| 741 | .cfgwr1 = TLP_FMTTYPE_CFGWR1, |
| 742 | }; |
| 743 | |
| 744 | static const struct altera_pcie_data altera_pcie_2_0_data = { |
| 745 | .ops = &altera_pcie_ops_2_0, |
| 746 | .version = ALTERA_PCIE_V2, |
| 747 | .cap_offset = 0x70, |
| 748 | .cfgrd0 = S10_TLP_FMTTYPE_CFGRD0, |
| 749 | .cfgrd1 = S10_TLP_FMTTYPE_CFGRD1, |
| 750 | .cfgwr0 = S10_TLP_FMTTYPE_CFGWR0, |
| 751 | .cfgwr1 = S10_TLP_FMTTYPE_CFGWR1, |
| 752 | }; |
| 753 | |
| 754 | static const struct of_device_id altera_pcie_of_match[] = { |
| 755 | {.compatible = "altr,pcie-root-port-1.0", |
| 756 | .data = &altera_pcie_1_0_data }, |
| 757 | {.compatible = "altr,pcie-root-port-2.0", |
| 758 | .data = &altera_pcie_2_0_data }, |
| 759 | {}, |
| 760 | }; |
| 761 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 762 | static int altera_pcie_probe(struct platform_device *pdev) |
| 763 | { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 764 | struct device *dev = &pdev->dev; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 765 | struct altera_pcie *pcie; |
Lorenzo Pieralisi | 9815791 | 2017-06-28 15:13:58 -0500 | [diff] [blame] | 766 | struct pci_host_bridge *bridge; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 767 | int ret; |
Fan Fei | c31990d | 2021-12-22 19:10:32 -0600 | [diff] [blame] | 768 | const struct altera_pcie_data *data; |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 769 | |
Lorenzo Pieralisi | 9815791 | 2017-06-28 15:13:58 -0500 | [diff] [blame] | 770 | bridge = devm_pci_alloc_host_bridge(dev, sizeof(*pcie)); |
| 771 | if (!bridge) |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 772 | return -ENOMEM; |
| 773 | |
Lorenzo Pieralisi | 9815791 | 2017-06-28 15:13:58 -0500 | [diff] [blame] | 774 | pcie = pci_host_bridge_priv(bridge); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 775 | pcie->pdev = pdev; |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 776 | platform_set_drvdata(pdev, pcie); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 777 | |
Fan Fei | c31990d | 2021-12-22 19:10:32 -0600 | [diff] [blame] | 778 | data = of_device_get_match_data(&pdev->dev); |
| 779 | if (!data) |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 780 | return -ENODEV; |
| 781 | |
Fan Fei | c31990d | 2021-12-22 19:10:32 -0600 | [diff] [blame] | 782 | pcie->pcie_data = data; |
Ley Foon Tan | 49fdbd7 | 2019-02-28 18:52:50 +0800 | [diff] [blame] | 783 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 784 | ret = altera_pcie_parse_dt(pcie); |
| 785 | if (ret) { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 786 | dev_err(dev, "Parsing DT failed\n"); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 787 | return ret; |
| 788 | } |
| 789 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 790 | ret = altera_pcie_init_irq_domain(pcie); |
| 791 | if (ret) { |
Bjorn Helgaas | fe49067 | 2016-10-11 19:50:52 -0500 | [diff] [blame] | 792 | dev_err(dev, "Failed creating IRQ Domain\n"); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 793 | return ret; |
| 794 | } |
| 795 | |
| 796 | /* clear all interrupts */ |
| 797 | cra_writel(pcie, P2A_INT_STS_ALL, P2A_INT_STATUS); |
| 798 | /* enable all interrupts */ |
| 799 | cra_writel(pcie, P2A_INT_ENA_ALL, P2A_INT_ENABLE); |
Ley Foon Tan | ce4f1c7 | 2016-08-26 09:47:25 +0800 | [diff] [blame] | 800 | altera_pcie_host_init(pcie); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 801 | |
Lorenzo Pieralisi | 9815791 | 2017-06-28 15:13:58 -0500 | [diff] [blame] | 802 | bridge->sysdata = pcie; |
| 803 | bridge->busnr = pcie->root_bus_nr; |
| 804 | bridge->ops = &altera_pcie_ops; |
| 805 | |
Rob Herring | c63aed7 | 2020-05-22 17:48:27 -0600 | [diff] [blame] | 806 | return pci_host_probe(bridge); |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 807 | } |
| 808 | |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 809 | static int altera_pcie_remove(struct platform_device *pdev) |
| 810 | { |
| 811 | struct altera_pcie *pcie = platform_get_drvdata(pdev); |
| 812 | struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); |
| 813 | |
| 814 | pci_stop_root_bus(bridge->bus); |
| 815 | pci_remove_root_bus(bridge->bus); |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 816 | altera_pcie_irq_teardown(pcie); |
| 817 | |
| 818 | return 0; |
| 819 | } |
| 820 | |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 821 | static struct platform_driver altera_pcie_driver = { |
| 822 | .probe = altera_pcie_probe, |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 823 | .remove = altera_pcie_remove, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 824 | .driver = { |
| 825 | .name = "altera-pcie", |
| 826 | .of_match_table = altera_pcie_of_match, |
Ley Foon Tan | eaa6111 | 2015-10-23 18:27:12 +0800 | [diff] [blame] | 827 | }, |
| 828 | }; |
| 829 | |
Ley Foon Tan | ec15c4d | 2019-04-24 12:57:14 +0800 | [diff] [blame] | 830 | MODULE_DEVICE_TABLE(of, altera_pcie_of_match); |
| 831 | module_platform_driver(altera_pcie_driver); |
| 832 | MODULE_LICENSE("GPL v2"); |