b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * PCIe host controller driver for Texas Instruments Keystone SoCs |
| 4 | * |
| 5 | * Copyright (C) 2013-2014 Texas Instruments., Ltd. |
| 6 | * http://www.ti.com |
| 7 | * |
| 8 | * Author: Murali Karicheri <m-karicheri2@ti.com> |
| 9 | * Implementation based on pci-exynos.c and pcie-designware.c |
| 10 | */ |
| 11 | |
| 12 | #include <linux/clk.h> |
| 13 | #include <linux/delay.h> |
| 14 | #include <linux/gpio/consumer.h> |
| 15 | #include <linux/init.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/irqchip/chained_irq.h> |
| 18 | #include <linux/irqdomain.h> |
| 19 | #include <linux/mfd/syscon.h> |
| 20 | #include <linux/msi.h> |
| 21 | #include <linux/of.h> |
| 22 | #include <linux/of_device.h> |
| 23 | #include <linux/of_irq.h> |
| 24 | #include <linux/of_pci.h> |
| 25 | #include <linux/phy/phy.h> |
| 26 | #include <linux/platform_device.h> |
| 27 | #include <linux/regmap.h> |
| 28 | #include <linux/resource.h> |
| 29 | #include <linux/signal.h> |
| 30 | |
| 31 | #include "../../pci.h" |
| 32 | #include "pcie-designware.h" |
| 33 | |
| 34 | #define PCIE_VENDORID_MASK 0xffff |
| 35 | #define PCIE_DEVICEID_SHIFT 16 |
| 36 | |
| 37 | /* Application registers */ |
| 38 | #define PID 0x000 |
| 39 | #define RTL GENMASK(15, 11) |
| 40 | #define RTL_SHIFT 11 |
| 41 | #define AM6_PCI_PG1_RTL_VER 0x15 |
| 42 | |
| 43 | #define CMD_STATUS 0x004 |
| 44 | #define LTSSM_EN_VAL BIT(0) |
| 45 | #define OB_XLAT_EN_VAL BIT(1) |
| 46 | #define DBI_CS2 BIT(5) |
| 47 | |
| 48 | #define CFG_SETUP 0x008 |
| 49 | #define CFG_BUS(x) (((x) & 0xff) << 16) |
| 50 | #define CFG_DEVICE(x) (((x) & 0x1f) << 8) |
| 51 | #define CFG_FUNC(x) ((x) & 0x7) |
| 52 | #define CFG_TYPE1 BIT(24) |
| 53 | |
| 54 | #define OB_SIZE 0x030 |
| 55 | #define OB_OFFSET_INDEX(n) (0x200 + (8 * (n))) |
| 56 | #define OB_OFFSET_HI(n) (0x204 + (8 * (n))) |
| 57 | #define OB_ENABLEN BIT(0) |
| 58 | #define OB_WIN_SIZE 8 /* 8MB */ |
| 59 | |
| 60 | #define PCIE_LEGACY_IRQ_ENABLE_SET(n) (0x188 + (0x10 * ((n) - 1))) |
| 61 | #define PCIE_LEGACY_IRQ_ENABLE_CLR(n) (0x18c + (0x10 * ((n) - 1))) |
| 62 | #define PCIE_EP_IRQ_SET 0x64 |
| 63 | #define PCIE_EP_IRQ_CLR 0x68 |
| 64 | #define INT_ENABLE BIT(0) |
| 65 | |
| 66 | /* IRQ register defines */ |
| 67 | #define IRQ_EOI 0x050 |
| 68 | |
| 69 | #define MSI_IRQ 0x054 |
| 70 | #define MSI_IRQ_STATUS(n) (0x104 + ((n) << 4)) |
| 71 | #define MSI_IRQ_ENABLE_SET(n) (0x108 + ((n) << 4)) |
| 72 | #define MSI_IRQ_ENABLE_CLR(n) (0x10c + ((n) << 4)) |
| 73 | #define MSI_IRQ_OFFSET 4 |
| 74 | |
| 75 | #define IRQ_STATUS(n) (0x184 + ((n) << 4)) |
| 76 | #define IRQ_ENABLE_SET(n) (0x188 + ((n) << 4)) |
| 77 | #define INTx_EN BIT(0) |
| 78 | |
| 79 | #define ERR_IRQ_STATUS 0x1c4 |
| 80 | #define ERR_IRQ_ENABLE_SET 0x1c8 |
| 81 | #define ERR_AER BIT(5) /* ECRC error */ |
| 82 | #define AM6_ERR_AER BIT(4) /* AM6 ECRC error */ |
| 83 | #define ERR_AXI BIT(4) /* AXI tag lookup fatal error */ |
| 84 | #define ERR_CORR BIT(3) /* Correctable error */ |
| 85 | #define ERR_NONFATAL BIT(2) /* Non-fatal error */ |
| 86 | #define ERR_FATAL BIT(1) /* Fatal error */ |
| 87 | #define ERR_SYS BIT(0) /* System error */ |
| 88 | #define ERR_IRQ_ALL (ERR_AER | ERR_AXI | ERR_CORR | \ |
| 89 | ERR_NONFATAL | ERR_FATAL | ERR_SYS) |
| 90 | |
| 91 | /* PCIE controller device IDs */ |
| 92 | #define PCIE_RC_K2HK 0xb008 |
| 93 | #define PCIE_RC_K2E 0xb009 |
| 94 | #define PCIE_RC_K2L 0xb00a |
| 95 | #define PCIE_RC_K2G 0xb00b |
| 96 | |
| 97 | #define KS_PCIE_DEV_TYPE_MASK (0x3 << 1) |
| 98 | #define KS_PCIE_DEV_TYPE(mode) ((mode) << 1) |
| 99 | |
| 100 | #define EP 0x0 |
| 101 | #define LEG_EP 0x1 |
| 102 | #define RC 0x2 |
| 103 | |
| 104 | #define EXP_CAP_ID_OFFSET 0x70 |
| 105 | |
| 106 | #define KS_PCIE_SYSCLOCKOUTEN BIT(0) |
| 107 | |
| 108 | #define AM654_PCIE_DEV_TYPE_MASK 0x3 |
| 109 | #define AM654_WIN_SIZE SZ_64K |
| 110 | |
| 111 | #define APP_ADDR_SPACE_0 (16 * SZ_1K) |
| 112 | |
| 113 | #define to_keystone_pcie(x) dev_get_drvdata((x)->dev) |
| 114 | |
| 115 | #define PCI_DEVICE_ID_TI_AM654X 0xb00c |
| 116 | |
| 117 | struct ks_pcie_of_data { |
| 118 | enum dw_pcie_device_mode mode; |
| 119 | const struct dw_pcie_host_ops *host_ops; |
| 120 | const struct dw_pcie_ep_ops *ep_ops; |
| 121 | unsigned int version; |
| 122 | }; |
| 123 | |
| 124 | struct keystone_pcie { |
| 125 | struct dw_pcie *pci; |
| 126 | /* PCI Device ID */ |
| 127 | u32 device_id; |
| 128 | int legacy_host_irqs[PCI_NUM_INTX]; |
| 129 | struct device_node *legacy_intc_np; |
| 130 | |
| 131 | int msi_host_irq; |
| 132 | int num_lanes; |
| 133 | u32 num_viewport; |
| 134 | struct phy **phy; |
| 135 | struct device_link **link; |
| 136 | struct device_node *msi_intc_np; |
| 137 | struct irq_domain *legacy_irq_domain; |
| 138 | struct device_node *np; |
| 139 | |
| 140 | /* Application register space */ |
| 141 | void __iomem *va_app_base; /* DT 1st resource */ |
| 142 | struct resource app; |
| 143 | bool is_am6; |
| 144 | }; |
| 145 | |
| 146 | static u32 ks_pcie_app_readl(struct keystone_pcie *ks_pcie, u32 offset) |
| 147 | { |
| 148 | return readl(ks_pcie->va_app_base + offset); |
| 149 | } |
| 150 | |
| 151 | static void ks_pcie_app_writel(struct keystone_pcie *ks_pcie, u32 offset, |
| 152 | u32 val) |
| 153 | { |
| 154 | writel(val, ks_pcie->va_app_base + offset); |
| 155 | } |
| 156 | |
| 157 | static void ks_pcie_msi_irq_ack(struct irq_data *data) |
| 158 | { |
| 159 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 160 | struct keystone_pcie *ks_pcie; |
| 161 | u32 irq = data->hwirq; |
| 162 | struct dw_pcie *pci; |
| 163 | u32 reg_offset; |
| 164 | u32 bit_pos; |
| 165 | |
| 166 | pci = to_dw_pcie_from_pp(pp); |
| 167 | ks_pcie = to_keystone_pcie(pci); |
| 168 | |
| 169 | reg_offset = irq % 8; |
| 170 | bit_pos = irq >> 3; |
| 171 | |
| 172 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_STATUS(reg_offset), |
| 173 | BIT(bit_pos)); |
| 174 | ks_pcie_app_writel(ks_pcie, IRQ_EOI, reg_offset + MSI_IRQ_OFFSET); |
| 175 | } |
| 176 | |
| 177 | static void ks_pcie_compose_msi_msg(struct irq_data *data, struct msi_msg *msg) |
| 178 | { |
| 179 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 180 | struct keystone_pcie *ks_pcie; |
| 181 | struct dw_pcie *pci; |
| 182 | u64 msi_target; |
| 183 | |
| 184 | pci = to_dw_pcie_from_pp(pp); |
| 185 | ks_pcie = to_keystone_pcie(pci); |
| 186 | |
| 187 | msi_target = ks_pcie->app.start + MSI_IRQ; |
| 188 | msg->address_lo = lower_32_bits(msi_target); |
| 189 | msg->address_hi = upper_32_bits(msi_target); |
| 190 | msg->data = data->hwirq; |
| 191 | |
| 192 | dev_dbg(pci->dev, "msi#%d address_hi %#x address_lo %#x\n", |
| 193 | (int)data->hwirq, msg->address_hi, msg->address_lo); |
| 194 | } |
| 195 | |
| 196 | static int ks_pcie_msi_set_affinity(struct irq_data *irq_data, |
| 197 | const struct cpumask *mask, bool force) |
| 198 | { |
| 199 | return -EINVAL; |
| 200 | } |
| 201 | |
| 202 | static void ks_pcie_msi_mask(struct irq_data *data) |
| 203 | { |
| 204 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 205 | struct keystone_pcie *ks_pcie; |
| 206 | u32 irq = data->hwirq; |
| 207 | struct dw_pcie *pci; |
| 208 | unsigned long flags; |
| 209 | u32 reg_offset; |
| 210 | u32 bit_pos; |
| 211 | |
| 212 | raw_spin_lock_irqsave(&pp->lock, flags); |
| 213 | |
| 214 | pci = to_dw_pcie_from_pp(pp); |
| 215 | ks_pcie = to_keystone_pcie(pci); |
| 216 | |
| 217 | reg_offset = irq % 8; |
| 218 | bit_pos = irq >> 3; |
| 219 | |
| 220 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_CLR(reg_offset), |
| 221 | BIT(bit_pos)); |
| 222 | |
| 223 | raw_spin_unlock_irqrestore(&pp->lock, flags); |
| 224 | } |
| 225 | |
| 226 | static void ks_pcie_msi_unmask(struct irq_data *data) |
| 227 | { |
| 228 | struct pcie_port *pp = irq_data_get_irq_chip_data(data); |
| 229 | struct keystone_pcie *ks_pcie; |
| 230 | u32 irq = data->hwirq; |
| 231 | struct dw_pcie *pci; |
| 232 | unsigned long flags; |
| 233 | u32 reg_offset; |
| 234 | u32 bit_pos; |
| 235 | |
| 236 | raw_spin_lock_irqsave(&pp->lock, flags); |
| 237 | |
| 238 | pci = to_dw_pcie_from_pp(pp); |
| 239 | ks_pcie = to_keystone_pcie(pci); |
| 240 | |
| 241 | reg_offset = irq % 8; |
| 242 | bit_pos = irq >> 3; |
| 243 | |
| 244 | ks_pcie_app_writel(ks_pcie, MSI_IRQ_ENABLE_SET(reg_offset), |
| 245 | BIT(bit_pos)); |
| 246 | |
| 247 | raw_spin_unlock_irqrestore(&pp->lock, flags); |
| 248 | } |
| 249 | |
| 250 | static struct irq_chip ks_pcie_msi_irq_chip = { |
| 251 | .name = "KEYSTONE-PCI-MSI", |
| 252 | .irq_ack = ks_pcie_msi_irq_ack, |
| 253 | .irq_compose_msi_msg = ks_pcie_compose_msi_msg, |
| 254 | .irq_set_affinity = ks_pcie_msi_set_affinity, |
| 255 | .irq_mask = ks_pcie_msi_mask, |
| 256 | .irq_unmask = ks_pcie_msi_unmask, |
| 257 | }; |
| 258 | |
| 259 | static int ks_pcie_msi_host_init(struct pcie_port *pp) |
| 260 | { |
| 261 | pp->msi_irq_chip = &ks_pcie_msi_irq_chip; |
| 262 | return dw_pcie_allocate_domains(pp); |
| 263 | } |
| 264 | |
| 265 | static void ks_pcie_handle_legacy_irq(struct keystone_pcie *ks_pcie, |
| 266 | int offset) |
| 267 | { |
| 268 | struct dw_pcie *pci = ks_pcie->pci; |
| 269 | struct device *dev = pci->dev; |
| 270 | u32 pending; |
| 271 | int virq; |
| 272 | |
| 273 | pending = ks_pcie_app_readl(ks_pcie, IRQ_STATUS(offset)); |
| 274 | |
| 275 | if (BIT(0) & pending) { |
| 276 | virq = irq_linear_revmap(ks_pcie->legacy_irq_domain, offset); |
| 277 | dev_dbg(dev, ": irq: irq_offset %d, virq %d\n", offset, virq); |
| 278 | generic_handle_irq(virq); |
| 279 | } |
| 280 | |
| 281 | /* EOI the INTx interrupt */ |
| 282 | ks_pcie_app_writel(ks_pcie, IRQ_EOI, offset); |
| 283 | } |
| 284 | |
| 285 | /* |
| 286 | * Dummy function so that DW core doesn't configure MSI |
| 287 | */ |
| 288 | static int ks_pcie_am654_msi_host_init(struct pcie_port *pp) |
| 289 | { |
| 290 | return 0; |
| 291 | } |
| 292 | |
| 293 | static void ks_pcie_enable_error_irq(struct keystone_pcie *ks_pcie) |
| 294 | { |
| 295 | ks_pcie_app_writel(ks_pcie, ERR_IRQ_ENABLE_SET, ERR_IRQ_ALL); |
| 296 | } |
| 297 | |
| 298 | static irqreturn_t ks_pcie_handle_error_irq(struct keystone_pcie *ks_pcie) |
| 299 | { |
| 300 | u32 reg; |
| 301 | struct device *dev = ks_pcie->pci->dev; |
| 302 | |
| 303 | reg = ks_pcie_app_readl(ks_pcie, ERR_IRQ_STATUS); |
| 304 | if (!reg) |
| 305 | return IRQ_NONE; |
| 306 | |
| 307 | if (reg & ERR_SYS) |
| 308 | dev_err(dev, "System Error\n"); |
| 309 | |
| 310 | if (reg & ERR_FATAL) |
| 311 | dev_err(dev, "Fatal Error\n"); |
| 312 | |
| 313 | if (reg & ERR_NONFATAL) |
| 314 | dev_dbg(dev, "Non Fatal Error\n"); |
| 315 | |
| 316 | if (reg & ERR_CORR) |
| 317 | dev_dbg(dev, "Correctable Error\n"); |
| 318 | |
| 319 | if (!ks_pcie->is_am6 && (reg & ERR_AXI)) |
| 320 | dev_err(dev, "AXI tag lookup fatal Error\n"); |
| 321 | |
| 322 | if (reg & ERR_AER || (ks_pcie->is_am6 && (reg & AM6_ERR_AER))) |
| 323 | dev_err(dev, "ECRC Error\n"); |
| 324 | |
| 325 | ks_pcie_app_writel(ks_pcie, ERR_IRQ_STATUS, reg); |
| 326 | |
| 327 | return IRQ_HANDLED; |
| 328 | } |
| 329 | |
| 330 | static void ks_pcie_ack_legacy_irq(struct irq_data *d) |
| 331 | { |
| 332 | } |
| 333 | |
| 334 | static void ks_pcie_mask_legacy_irq(struct irq_data *d) |
| 335 | { |
| 336 | } |
| 337 | |
| 338 | static void ks_pcie_unmask_legacy_irq(struct irq_data *d) |
| 339 | { |
| 340 | } |
| 341 | |
| 342 | static struct irq_chip ks_pcie_legacy_irq_chip = { |
| 343 | .name = "Keystone-PCI-Legacy-IRQ", |
| 344 | .irq_ack = ks_pcie_ack_legacy_irq, |
| 345 | .irq_mask = ks_pcie_mask_legacy_irq, |
| 346 | .irq_unmask = ks_pcie_unmask_legacy_irq, |
| 347 | }; |
| 348 | |
| 349 | static int ks_pcie_init_legacy_irq_map(struct irq_domain *d, |
| 350 | unsigned int irq, |
| 351 | irq_hw_number_t hw_irq) |
| 352 | { |
| 353 | irq_set_chip_and_handler(irq, &ks_pcie_legacy_irq_chip, |
| 354 | handle_level_irq); |
| 355 | irq_set_chip_data(irq, d->host_data); |
| 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | static const struct irq_domain_ops ks_pcie_legacy_irq_domain_ops = { |
| 361 | .map = ks_pcie_init_legacy_irq_map, |
| 362 | .xlate = irq_domain_xlate_onetwocell, |
| 363 | }; |
| 364 | |
| 365 | /** |
| 366 | * ks_pcie_set_dbi_mode() - Set DBI mode to access overlaid BAR mask |
| 367 | * registers |
| 368 | * |
| 369 | * Since modification of dbi_cs2 involves different clock domain, read the |
| 370 | * status back to ensure the transition is complete. |
| 371 | */ |
| 372 | static void ks_pcie_set_dbi_mode(struct keystone_pcie *ks_pcie) |
| 373 | { |
| 374 | u32 val; |
| 375 | |
| 376 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 377 | val |= DBI_CS2; |
| 378 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
| 379 | |
| 380 | do { |
| 381 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 382 | } while (!(val & DBI_CS2)); |
| 383 | } |
| 384 | |
| 385 | /** |
| 386 | * ks_pcie_clear_dbi_mode() - Disable DBI mode |
| 387 | * |
| 388 | * Since modification of dbi_cs2 involves different clock domain, read the |
| 389 | * status back to ensure the transition is complete. |
| 390 | */ |
| 391 | static void ks_pcie_clear_dbi_mode(struct keystone_pcie *ks_pcie) |
| 392 | { |
| 393 | u32 val; |
| 394 | |
| 395 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 396 | val &= ~DBI_CS2; |
| 397 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
| 398 | |
| 399 | do { |
| 400 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 401 | } while (val & DBI_CS2); |
| 402 | } |
| 403 | |
| 404 | static void ks_pcie_setup_rc_app_regs(struct keystone_pcie *ks_pcie) |
| 405 | { |
| 406 | u32 val; |
| 407 | u32 num_viewport = ks_pcie->num_viewport; |
| 408 | struct dw_pcie *pci = ks_pcie->pci; |
| 409 | struct pcie_port *pp = &pci->pp; |
| 410 | u64 start = pp->mem->start; |
| 411 | u64 end = pp->mem->end; |
| 412 | int i; |
| 413 | |
| 414 | /* Disable BARs for inbound access */ |
| 415 | ks_pcie_set_dbi_mode(ks_pcie); |
| 416 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 0); |
| 417 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_1, 0); |
| 418 | ks_pcie_clear_dbi_mode(ks_pcie); |
| 419 | |
| 420 | if (ks_pcie->is_am6) |
| 421 | return; |
| 422 | |
| 423 | val = ilog2(OB_WIN_SIZE); |
| 424 | ks_pcie_app_writel(ks_pcie, OB_SIZE, val); |
| 425 | |
| 426 | /* Using Direct 1:1 mapping of RC <-> PCI memory space */ |
| 427 | for (i = 0; i < num_viewport && (start < end); i++) { |
| 428 | ks_pcie_app_writel(ks_pcie, OB_OFFSET_INDEX(i), |
| 429 | lower_32_bits(start) | OB_ENABLEN); |
| 430 | ks_pcie_app_writel(ks_pcie, OB_OFFSET_HI(i), |
| 431 | upper_32_bits(start)); |
| 432 | start += OB_WIN_SIZE * SZ_1M; |
| 433 | } |
| 434 | |
| 435 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 436 | val |= OB_XLAT_EN_VAL; |
| 437 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
| 438 | } |
| 439 | |
| 440 | static int ks_pcie_rd_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 441 | unsigned int devfn, int where, int size, |
| 442 | u32 *val) |
| 443 | { |
| 444 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 445 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 446 | u32 reg; |
| 447 | |
| 448 | reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) | |
| 449 | CFG_FUNC(PCI_FUNC(devfn)); |
| 450 | if (bus->parent->number != pp->root_bus_nr) |
| 451 | reg |= CFG_TYPE1; |
| 452 | ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg); |
| 453 | |
| 454 | return dw_pcie_read(pp->va_cfg0_base + where, size, val); |
| 455 | } |
| 456 | |
| 457 | static int ks_pcie_wr_other_conf(struct pcie_port *pp, struct pci_bus *bus, |
| 458 | unsigned int devfn, int where, int size, |
| 459 | u32 val) |
| 460 | { |
| 461 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 462 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 463 | u32 reg; |
| 464 | |
| 465 | reg = CFG_BUS(bus->number) | CFG_DEVICE(PCI_SLOT(devfn)) | |
| 466 | CFG_FUNC(PCI_FUNC(devfn)); |
| 467 | if (bus->parent->number != pp->root_bus_nr) |
| 468 | reg |= CFG_TYPE1; |
| 469 | ks_pcie_app_writel(ks_pcie, CFG_SETUP, reg); |
| 470 | |
| 471 | return dw_pcie_write(pp->va_cfg0_base + where, size, val); |
| 472 | } |
| 473 | |
| 474 | /** |
| 475 | * ks_pcie_v3_65_scan_bus() - keystone scan_bus post initialization |
| 476 | * |
| 477 | * This sets BAR0 to enable inbound access for MSI_IRQ register |
| 478 | */ |
| 479 | static void ks_pcie_v3_65_scan_bus(struct pcie_port *pp) |
| 480 | { |
| 481 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 482 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 483 | |
| 484 | /* Configure and set up BAR0 */ |
| 485 | ks_pcie_set_dbi_mode(ks_pcie); |
| 486 | |
| 487 | /* Enable BAR0 */ |
| 488 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, 1); |
| 489 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, SZ_4K - 1); |
| 490 | |
| 491 | ks_pcie_clear_dbi_mode(ks_pcie); |
| 492 | |
| 493 | /* |
| 494 | * For BAR0, just setting bus address for inbound writes (MSI) should |
| 495 | * be sufficient. Use physical address to avoid any conflicts. |
| 496 | */ |
| 497 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, ks_pcie->app.start); |
| 498 | } |
| 499 | |
| 500 | /** |
| 501 | * ks_pcie_link_up() - Check if link up |
| 502 | */ |
| 503 | static int ks_pcie_link_up(struct dw_pcie *pci) |
| 504 | { |
| 505 | u32 val; |
| 506 | |
| 507 | val = dw_pcie_readl_dbi(pci, PCIE_PORT_DEBUG0); |
| 508 | val &= PORT_LOGIC_LTSSM_STATE_MASK; |
| 509 | return (val == PORT_LOGIC_LTSSM_STATE_L0); |
| 510 | } |
| 511 | |
| 512 | static void ks_pcie_stop_link(struct dw_pcie *pci) |
| 513 | { |
| 514 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 515 | u32 val; |
| 516 | |
| 517 | /* Disable Link training */ |
| 518 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 519 | val &= ~LTSSM_EN_VAL; |
| 520 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, val); |
| 521 | } |
| 522 | |
| 523 | static int ks_pcie_start_link(struct dw_pcie *pci) |
| 524 | { |
| 525 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 526 | struct device *dev = pci->dev; |
| 527 | u32 val; |
| 528 | |
| 529 | if (dw_pcie_link_up(pci)) { |
| 530 | dev_dbg(dev, "link is already up\n"); |
| 531 | return 0; |
| 532 | } |
| 533 | |
| 534 | /* Initiate Link Training */ |
| 535 | val = ks_pcie_app_readl(ks_pcie, CMD_STATUS); |
| 536 | ks_pcie_app_writel(ks_pcie, CMD_STATUS, LTSSM_EN_VAL | val); |
| 537 | |
| 538 | return 0; |
| 539 | } |
| 540 | |
| 541 | static void ks_pcie_quirk(struct pci_dev *dev) |
| 542 | { |
| 543 | struct pci_bus *bus = dev->bus; |
| 544 | struct keystone_pcie *ks_pcie; |
| 545 | struct device *bridge_dev; |
| 546 | struct pci_dev *bridge; |
| 547 | u32 val; |
| 548 | |
| 549 | static const struct pci_device_id rc_pci_devids[] = { |
| 550 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2HK), |
| 551 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 552 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2E), |
| 553 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 554 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2L), |
| 555 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 556 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCIE_RC_K2G), |
| 557 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 558 | { 0, }, |
| 559 | }; |
| 560 | static const struct pci_device_id am6_pci_devids[] = { |
| 561 | { PCI_DEVICE(PCI_VENDOR_ID_TI, PCI_DEVICE_ID_TI_AM654X), |
| 562 | .class = PCI_CLASS_BRIDGE_PCI << 8, .class_mask = ~0, }, |
| 563 | { 0, }, |
| 564 | }; |
| 565 | |
| 566 | if (pci_is_root_bus(bus)) |
| 567 | bridge = dev; |
| 568 | |
| 569 | /* look for the host bridge */ |
| 570 | while (!pci_is_root_bus(bus)) { |
| 571 | bridge = bus->self; |
| 572 | bus = bus->parent; |
| 573 | } |
| 574 | |
| 575 | if (!bridge) |
| 576 | return; |
| 577 | |
| 578 | /* |
| 579 | * Keystone PCI controller has a h/w limitation of |
| 580 | * 256 bytes maximum read request size. It can't handle |
| 581 | * anything higher than this. So force this limit on |
| 582 | * all downstream devices. |
| 583 | */ |
| 584 | if (pci_match_id(rc_pci_devids, bridge)) { |
| 585 | if (pcie_get_readrq(dev) > 256) { |
| 586 | dev_info(&dev->dev, "limiting MRRS to 256 bytes\n"); |
| 587 | pcie_set_readrq(dev, 256); |
| 588 | } |
| 589 | } |
| 590 | |
| 591 | /* |
| 592 | * Memory transactions fail with PCI controller in AM654 PG1.0 |
| 593 | * when MRRS is set to more than 128 bytes. Force the MRRS to |
| 594 | * 128 bytes in all downstream devices. |
| 595 | */ |
| 596 | if (pci_match_id(am6_pci_devids, bridge)) { |
| 597 | bridge_dev = pci_get_host_bridge_device(dev); |
| 598 | if (!bridge_dev || !bridge_dev->parent) |
| 599 | return; |
| 600 | |
| 601 | ks_pcie = dev_get_drvdata(bridge_dev->parent); |
| 602 | if (!ks_pcie) |
| 603 | return; |
| 604 | |
| 605 | val = ks_pcie_app_readl(ks_pcie, PID); |
| 606 | val &= RTL; |
| 607 | val >>= RTL_SHIFT; |
| 608 | if (val != AM6_PCI_PG1_RTL_VER) |
| 609 | return; |
| 610 | |
| 611 | if (pcie_get_readrq(dev) > 128) { |
| 612 | dev_info(&dev->dev, "limiting MRRS to 128 bytes\n"); |
| 613 | pcie_set_readrq(dev, 128); |
| 614 | } |
| 615 | } |
| 616 | } |
| 617 | DECLARE_PCI_FIXUP_ENABLE(PCI_ANY_ID, PCI_ANY_ID, ks_pcie_quirk); |
| 618 | |
| 619 | static void ks_pcie_msi_irq_handler(struct irq_desc *desc) |
| 620 | { |
| 621 | unsigned int irq = desc->irq_data.hwirq; |
| 622 | struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc); |
| 623 | u32 offset = irq - ks_pcie->msi_host_irq; |
| 624 | struct dw_pcie *pci = ks_pcie->pci; |
| 625 | struct pcie_port *pp = &pci->pp; |
| 626 | struct device *dev = pci->dev; |
| 627 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 628 | u32 vector, virq, reg, pos; |
| 629 | |
| 630 | dev_dbg(dev, "%s, irq %d\n", __func__, irq); |
| 631 | |
| 632 | /* |
| 633 | * The chained irq handler installation would have replaced normal |
| 634 | * interrupt driver handler so we need to take care of mask/unmask and |
| 635 | * ack operation. |
| 636 | */ |
| 637 | chained_irq_enter(chip, desc); |
| 638 | |
| 639 | reg = ks_pcie_app_readl(ks_pcie, MSI_IRQ_STATUS(offset)); |
| 640 | /* |
| 641 | * MSI0 status bit 0-3 shows vectors 0, 8, 16, 24, MSI1 status bit |
| 642 | * shows 1, 9, 17, 25 and so forth |
| 643 | */ |
| 644 | for (pos = 0; pos < 4; pos++) { |
| 645 | if (!(reg & BIT(pos))) |
| 646 | continue; |
| 647 | |
| 648 | vector = offset + (pos << 3); |
| 649 | virq = irq_linear_revmap(pp->irq_domain, vector); |
| 650 | dev_dbg(dev, "irq: bit %d, vector %d, virq %d\n", pos, vector, |
| 651 | virq); |
| 652 | generic_handle_irq(virq); |
| 653 | } |
| 654 | |
| 655 | chained_irq_exit(chip, desc); |
| 656 | } |
| 657 | |
| 658 | /** |
| 659 | * ks_pcie_legacy_irq_handler() - Handle legacy interrupt |
| 660 | * @irq: IRQ line for legacy interrupts |
| 661 | * @desc: Pointer to irq descriptor |
| 662 | * |
| 663 | * Traverse through pending legacy interrupts and invoke handler for each. Also |
| 664 | * takes care of interrupt controller level mask/ack operation. |
| 665 | */ |
| 666 | static void ks_pcie_legacy_irq_handler(struct irq_desc *desc) |
| 667 | { |
| 668 | unsigned int irq = irq_desc_get_irq(desc); |
| 669 | struct keystone_pcie *ks_pcie = irq_desc_get_handler_data(desc); |
| 670 | struct dw_pcie *pci = ks_pcie->pci; |
| 671 | struct device *dev = pci->dev; |
| 672 | u32 irq_offset = irq - ks_pcie->legacy_host_irqs[0]; |
| 673 | struct irq_chip *chip = irq_desc_get_chip(desc); |
| 674 | |
| 675 | dev_dbg(dev, ": Handling legacy irq %d\n", irq); |
| 676 | |
| 677 | /* |
| 678 | * The chained irq handler installation would have replaced normal |
| 679 | * interrupt driver handler so we need to take care of mask/unmask and |
| 680 | * ack operation. |
| 681 | */ |
| 682 | chained_irq_enter(chip, desc); |
| 683 | ks_pcie_handle_legacy_irq(ks_pcie, irq_offset); |
| 684 | chained_irq_exit(chip, desc); |
| 685 | } |
| 686 | |
| 687 | static int ks_pcie_config_msi_irq(struct keystone_pcie *ks_pcie) |
| 688 | { |
| 689 | struct device *dev = ks_pcie->pci->dev; |
| 690 | struct device_node *np = ks_pcie->np; |
| 691 | struct device_node *intc_np; |
| 692 | struct irq_data *irq_data; |
| 693 | int irq_count, irq, ret, i; |
| 694 | |
| 695 | if (!IS_ENABLED(CONFIG_PCI_MSI)) |
| 696 | return 0; |
| 697 | |
| 698 | intc_np = of_get_child_by_name(np, "msi-interrupt-controller"); |
| 699 | if (!intc_np) { |
| 700 | if (ks_pcie->is_am6) |
| 701 | return 0; |
| 702 | dev_warn(dev, "msi-interrupt-controller node is absent\n"); |
| 703 | return -EINVAL; |
| 704 | } |
| 705 | |
| 706 | irq_count = of_irq_count(intc_np); |
| 707 | if (!irq_count) { |
| 708 | dev_err(dev, "No IRQ entries in msi-interrupt-controller\n"); |
| 709 | ret = -EINVAL; |
| 710 | goto err; |
| 711 | } |
| 712 | |
| 713 | for (i = 0; i < irq_count; i++) { |
| 714 | irq = irq_of_parse_and_map(intc_np, i); |
| 715 | if (!irq) { |
| 716 | ret = -EINVAL; |
| 717 | goto err; |
| 718 | } |
| 719 | |
| 720 | if (!ks_pcie->msi_host_irq) { |
| 721 | irq_data = irq_get_irq_data(irq); |
| 722 | if (!irq_data) { |
| 723 | ret = -EINVAL; |
| 724 | goto err; |
| 725 | } |
| 726 | ks_pcie->msi_host_irq = irq_data->hwirq; |
| 727 | } |
| 728 | |
| 729 | irq_set_chained_handler_and_data(irq, ks_pcie_msi_irq_handler, |
| 730 | ks_pcie); |
| 731 | } |
| 732 | |
| 733 | of_node_put(intc_np); |
| 734 | return 0; |
| 735 | |
| 736 | err: |
| 737 | of_node_put(intc_np); |
| 738 | return ret; |
| 739 | } |
| 740 | |
| 741 | static int ks_pcie_config_legacy_irq(struct keystone_pcie *ks_pcie) |
| 742 | { |
| 743 | struct device *dev = ks_pcie->pci->dev; |
| 744 | struct irq_domain *legacy_irq_domain; |
| 745 | struct device_node *np = ks_pcie->np; |
| 746 | struct device_node *intc_np; |
| 747 | int irq_count, irq, ret = 0, i; |
| 748 | |
| 749 | intc_np = of_get_child_by_name(np, "legacy-interrupt-controller"); |
| 750 | if (!intc_np) { |
| 751 | /* |
| 752 | * Since legacy interrupts are modeled as edge-interrupts in |
| 753 | * AM6, keep it disabled for now. |
| 754 | */ |
| 755 | if (ks_pcie->is_am6) |
| 756 | return 0; |
| 757 | dev_warn(dev, "legacy-interrupt-controller node is absent\n"); |
| 758 | return -EINVAL; |
| 759 | } |
| 760 | |
| 761 | irq_count = of_irq_count(intc_np); |
| 762 | if (!irq_count) { |
| 763 | dev_err(dev, "No IRQ entries in legacy-interrupt-controller\n"); |
| 764 | ret = -EINVAL; |
| 765 | goto err; |
| 766 | } |
| 767 | |
| 768 | for (i = 0; i < irq_count; i++) { |
| 769 | irq = irq_of_parse_and_map(intc_np, i); |
| 770 | if (!irq) { |
| 771 | ret = -EINVAL; |
| 772 | goto err; |
| 773 | } |
| 774 | ks_pcie->legacy_host_irqs[i] = irq; |
| 775 | |
| 776 | irq_set_chained_handler_and_data(irq, |
| 777 | ks_pcie_legacy_irq_handler, |
| 778 | ks_pcie); |
| 779 | } |
| 780 | |
| 781 | legacy_irq_domain = |
| 782 | irq_domain_add_linear(intc_np, PCI_NUM_INTX, |
| 783 | &ks_pcie_legacy_irq_domain_ops, NULL); |
| 784 | if (!legacy_irq_domain) { |
| 785 | dev_err(dev, "Failed to add irq domain for legacy irqs\n"); |
| 786 | ret = -EINVAL; |
| 787 | goto err; |
| 788 | } |
| 789 | ks_pcie->legacy_irq_domain = legacy_irq_domain; |
| 790 | |
| 791 | for (i = 0; i < PCI_NUM_INTX; i++) |
| 792 | ks_pcie_app_writel(ks_pcie, IRQ_ENABLE_SET(i), INTx_EN); |
| 793 | |
| 794 | err: |
| 795 | of_node_put(intc_np); |
| 796 | return ret; |
| 797 | } |
| 798 | |
| 799 | #ifdef CONFIG_ARM |
| 800 | /* |
| 801 | * When a PCI device does not exist during config cycles, keystone host gets a |
| 802 | * bus error instead of returning 0xffffffff. This handler always returns 0 |
| 803 | * for this kind of faults. |
| 804 | */ |
| 805 | static int ks_pcie_fault(unsigned long addr, unsigned int fsr, |
| 806 | struct pt_regs *regs) |
| 807 | { |
| 808 | unsigned long instr = *(unsigned long *) instruction_pointer(regs); |
| 809 | |
| 810 | if ((instr & 0x0e100090) == 0x00100090) { |
| 811 | int reg = (instr >> 12) & 15; |
| 812 | |
| 813 | regs->uregs[reg] = -1; |
| 814 | regs->ARM_pc += 4; |
| 815 | } |
| 816 | |
| 817 | return 0; |
| 818 | } |
| 819 | #endif |
| 820 | |
| 821 | static int __init ks_pcie_init_id(struct keystone_pcie *ks_pcie) |
| 822 | { |
| 823 | int ret; |
| 824 | unsigned int id; |
| 825 | struct regmap *devctrl_regs; |
| 826 | struct dw_pcie *pci = ks_pcie->pci; |
| 827 | struct device *dev = pci->dev; |
| 828 | struct device_node *np = dev->of_node; |
| 829 | |
| 830 | devctrl_regs = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-id"); |
| 831 | if (IS_ERR(devctrl_regs)) |
| 832 | return PTR_ERR(devctrl_regs); |
| 833 | |
| 834 | ret = regmap_read(devctrl_regs, 0, &id); |
| 835 | if (ret) |
| 836 | return ret; |
| 837 | |
| 838 | dw_pcie_dbi_ro_wr_en(pci); |
| 839 | dw_pcie_writew_dbi(pci, PCI_VENDOR_ID, id & PCIE_VENDORID_MASK); |
| 840 | dw_pcie_writew_dbi(pci, PCI_DEVICE_ID, id >> PCIE_DEVICEID_SHIFT); |
| 841 | dw_pcie_dbi_ro_wr_dis(pci); |
| 842 | |
| 843 | return 0; |
| 844 | } |
| 845 | |
| 846 | static int __init ks_pcie_host_init(struct pcie_port *pp) |
| 847 | { |
| 848 | struct dw_pcie *pci = to_dw_pcie_from_pp(pp); |
| 849 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 850 | int ret; |
| 851 | |
| 852 | ret = ks_pcie_config_legacy_irq(ks_pcie); |
| 853 | if (ret) |
| 854 | return ret; |
| 855 | |
| 856 | ret = ks_pcie_config_msi_irq(ks_pcie); |
| 857 | if (ret) |
| 858 | return ret; |
| 859 | |
| 860 | dw_pcie_setup_rc(pp); |
| 861 | |
| 862 | ks_pcie_stop_link(pci); |
| 863 | ks_pcie_setup_rc_app_regs(ks_pcie); |
| 864 | writew(PCI_IO_RANGE_TYPE_32 | (PCI_IO_RANGE_TYPE_32 << 8), |
| 865 | pci->dbi_base + PCI_IO_BASE); |
| 866 | |
| 867 | ret = ks_pcie_init_id(ks_pcie); |
| 868 | if (ret < 0) |
| 869 | return ret; |
| 870 | |
| 871 | #ifdef CONFIG_ARM |
| 872 | /* |
| 873 | * PCIe access errors that result into OCP errors are caught by ARM as |
| 874 | * "External aborts" |
| 875 | */ |
| 876 | hook_fault_code(17, ks_pcie_fault, SIGBUS, 0, |
| 877 | "Asynchronous external abort"); |
| 878 | #endif |
| 879 | |
| 880 | ks_pcie_start_link(pci); |
| 881 | dw_pcie_wait_for_link(pci); |
| 882 | |
| 883 | return 0; |
| 884 | } |
| 885 | |
| 886 | static const struct dw_pcie_host_ops ks_pcie_host_ops = { |
| 887 | .rd_other_conf = ks_pcie_rd_other_conf, |
| 888 | .wr_other_conf = ks_pcie_wr_other_conf, |
| 889 | .host_init = ks_pcie_host_init, |
| 890 | .msi_host_init = ks_pcie_msi_host_init, |
| 891 | .scan_bus = ks_pcie_v3_65_scan_bus, |
| 892 | }; |
| 893 | |
| 894 | static const struct dw_pcie_host_ops ks_pcie_am654_host_ops = { |
| 895 | .host_init = ks_pcie_host_init, |
| 896 | .msi_host_init = ks_pcie_am654_msi_host_init, |
| 897 | }; |
| 898 | |
| 899 | static irqreturn_t ks_pcie_err_irq_handler(int irq, void *priv) |
| 900 | { |
| 901 | struct keystone_pcie *ks_pcie = priv; |
| 902 | |
| 903 | return ks_pcie_handle_error_irq(ks_pcie); |
| 904 | } |
| 905 | |
| 906 | static int ks_pcie_add_pcie_port(struct keystone_pcie *ks_pcie, |
| 907 | struct platform_device *pdev) |
| 908 | { |
| 909 | struct dw_pcie *pci = ks_pcie->pci; |
| 910 | struct pcie_port *pp = &pci->pp; |
| 911 | struct device *dev = &pdev->dev; |
| 912 | struct resource *res; |
| 913 | int ret; |
| 914 | |
| 915 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "config"); |
| 916 | pp->va_cfg0_base = devm_pci_remap_cfg_resource(dev, res); |
| 917 | if (IS_ERR(pp->va_cfg0_base)) |
| 918 | return PTR_ERR(pp->va_cfg0_base); |
| 919 | |
| 920 | pp->va_cfg1_base = pp->va_cfg0_base; |
| 921 | |
| 922 | ret = dw_pcie_host_init(pp); |
| 923 | if (ret) { |
| 924 | dev_err(dev, "failed to initialize host\n"); |
| 925 | return ret; |
| 926 | } |
| 927 | |
| 928 | return 0; |
| 929 | } |
| 930 | |
| 931 | static u32 ks_pcie_am654_read_dbi2(struct dw_pcie *pci, void __iomem *base, |
| 932 | u32 reg, size_t size) |
| 933 | { |
| 934 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 935 | u32 val; |
| 936 | |
| 937 | ks_pcie_set_dbi_mode(ks_pcie); |
| 938 | dw_pcie_read(base + reg, size, &val); |
| 939 | ks_pcie_clear_dbi_mode(ks_pcie); |
| 940 | return val; |
| 941 | } |
| 942 | |
| 943 | static void ks_pcie_am654_write_dbi2(struct dw_pcie *pci, void __iomem *base, |
| 944 | u32 reg, size_t size, u32 val) |
| 945 | { |
| 946 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 947 | |
| 948 | ks_pcie_set_dbi_mode(ks_pcie); |
| 949 | dw_pcie_write(base + reg, size, val); |
| 950 | ks_pcie_clear_dbi_mode(ks_pcie); |
| 951 | } |
| 952 | |
| 953 | static const struct dw_pcie_ops ks_pcie_dw_pcie_ops = { |
| 954 | .start_link = ks_pcie_start_link, |
| 955 | .stop_link = ks_pcie_stop_link, |
| 956 | .link_up = ks_pcie_link_up, |
| 957 | .read_dbi2 = ks_pcie_am654_read_dbi2, |
| 958 | .write_dbi2 = ks_pcie_am654_write_dbi2, |
| 959 | }; |
| 960 | |
| 961 | static void ks_pcie_am654_ep_init(struct dw_pcie_ep *ep) |
| 962 | { |
| 963 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 964 | int flags; |
| 965 | |
| 966 | ep->page_size = AM654_WIN_SIZE; |
| 967 | flags = PCI_BASE_ADDRESS_SPACE_MEMORY | PCI_BASE_ADDRESS_MEM_TYPE_32; |
| 968 | dw_pcie_writel_dbi2(pci, PCI_BASE_ADDRESS_0, APP_ADDR_SPACE_0 - 1); |
| 969 | dw_pcie_writel_dbi(pci, PCI_BASE_ADDRESS_0, flags); |
| 970 | } |
| 971 | |
| 972 | static void ks_pcie_am654_raise_legacy_irq(struct keystone_pcie *ks_pcie) |
| 973 | { |
| 974 | struct dw_pcie *pci = ks_pcie->pci; |
| 975 | u8 int_pin; |
| 976 | |
| 977 | int_pin = dw_pcie_readb_dbi(pci, PCI_INTERRUPT_PIN); |
| 978 | if (int_pin == 0 || int_pin > 4) |
| 979 | return; |
| 980 | |
| 981 | ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_SET(int_pin), |
| 982 | INT_ENABLE); |
| 983 | ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_SET, INT_ENABLE); |
| 984 | mdelay(1); |
| 985 | ks_pcie_app_writel(ks_pcie, PCIE_EP_IRQ_CLR, INT_ENABLE); |
| 986 | ks_pcie_app_writel(ks_pcie, PCIE_LEGACY_IRQ_ENABLE_CLR(int_pin), |
| 987 | INT_ENABLE); |
| 988 | } |
| 989 | |
| 990 | static int ks_pcie_am654_raise_irq(struct dw_pcie_ep *ep, u8 func_no, |
| 991 | enum pci_epc_irq_type type, |
| 992 | u16 interrupt_num) |
| 993 | { |
| 994 | struct dw_pcie *pci = to_dw_pcie_from_ep(ep); |
| 995 | struct keystone_pcie *ks_pcie = to_keystone_pcie(pci); |
| 996 | |
| 997 | switch (type) { |
| 998 | case PCI_EPC_IRQ_LEGACY: |
| 999 | ks_pcie_am654_raise_legacy_irq(ks_pcie); |
| 1000 | break; |
| 1001 | case PCI_EPC_IRQ_MSI: |
| 1002 | dw_pcie_ep_raise_msi_irq(ep, func_no, interrupt_num); |
| 1003 | break; |
| 1004 | default: |
| 1005 | dev_err(pci->dev, "UNKNOWN IRQ type\n"); |
| 1006 | return -EINVAL; |
| 1007 | } |
| 1008 | |
| 1009 | return 0; |
| 1010 | } |
| 1011 | |
| 1012 | static const struct pci_epc_features ks_pcie_am654_epc_features = { |
| 1013 | .linkup_notifier = false, |
| 1014 | .msi_capable = true, |
| 1015 | .msix_capable = false, |
| 1016 | .reserved_bar = 1 << BAR_0 | 1 << BAR_1, |
| 1017 | .bar_fixed_64bit = 1 << BAR_0, |
| 1018 | .bar_fixed_size[2] = SZ_1M, |
| 1019 | .bar_fixed_size[3] = SZ_64K, |
| 1020 | .bar_fixed_size[4] = 256, |
| 1021 | .bar_fixed_size[5] = SZ_1M, |
| 1022 | .align = SZ_1M, |
| 1023 | }; |
| 1024 | |
| 1025 | static const struct pci_epc_features* |
| 1026 | ks_pcie_am654_get_features(struct dw_pcie_ep *ep) |
| 1027 | { |
| 1028 | return &ks_pcie_am654_epc_features; |
| 1029 | } |
| 1030 | |
| 1031 | static const struct dw_pcie_ep_ops ks_pcie_am654_ep_ops = { |
| 1032 | .ep_init = ks_pcie_am654_ep_init, |
| 1033 | .raise_irq = ks_pcie_am654_raise_irq, |
| 1034 | .get_features = &ks_pcie_am654_get_features, |
| 1035 | }; |
| 1036 | |
| 1037 | static int ks_pcie_add_pcie_ep(struct keystone_pcie *ks_pcie, |
| 1038 | struct platform_device *pdev) |
| 1039 | { |
| 1040 | int ret; |
| 1041 | struct dw_pcie_ep *ep; |
| 1042 | struct resource *res; |
| 1043 | struct device *dev = &pdev->dev; |
| 1044 | struct dw_pcie *pci = ks_pcie->pci; |
| 1045 | |
| 1046 | ep = &pci->ep; |
| 1047 | |
| 1048 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "addr_space"); |
| 1049 | if (!res) |
| 1050 | return -EINVAL; |
| 1051 | |
| 1052 | ep->phys_base = res->start; |
| 1053 | ep->addr_size = resource_size(res); |
| 1054 | |
| 1055 | ret = dw_pcie_ep_init(ep); |
| 1056 | if (ret) { |
| 1057 | dev_err(dev, "failed to initialize endpoint\n"); |
| 1058 | return ret; |
| 1059 | } |
| 1060 | |
| 1061 | return 0; |
| 1062 | } |
| 1063 | |
| 1064 | static void ks_pcie_disable_phy(struct keystone_pcie *ks_pcie) |
| 1065 | { |
| 1066 | int num_lanes = ks_pcie->num_lanes; |
| 1067 | |
| 1068 | while (num_lanes--) { |
| 1069 | phy_power_off(ks_pcie->phy[num_lanes]); |
| 1070 | phy_exit(ks_pcie->phy[num_lanes]); |
| 1071 | } |
| 1072 | } |
| 1073 | |
| 1074 | static int ks_pcie_enable_phy(struct keystone_pcie *ks_pcie) |
| 1075 | { |
| 1076 | int i; |
| 1077 | int ret; |
| 1078 | int num_lanes = ks_pcie->num_lanes; |
| 1079 | |
| 1080 | for (i = 0; i < num_lanes; i++) { |
| 1081 | ret = phy_reset(ks_pcie->phy[i]); |
| 1082 | if (ret < 0) |
| 1083 | goto err_phy; |
| 1084 | |
| 1085 | ret = phy_init(ks_pcie->phy[i]); |
| 1086 | if (ret < 0) |
| 1087 | goto err_phy; |
| 1088 | |
| 1089 | ret = phy_power_on(ks_pcie->phy[i]); |
| 1090 | if (ret < 0) { |
| 1091 | phy_exit(ks_pcie->phy[i]); |
| 1092 | goto err_phy; |
| 1093 | } |
| 1094 | } |
| 1095 | |
| 1096 | return 0; |
| 1097 | |
| 1098 | err_phy: |
| 1099 | while (--i >= 0) { |
| 1100 | phy_power_off(ks_pcie->phy[i]); |
| 1101 | phy_exit(ks_pcie->phy[i]); |
| 1102 | } |
| 1103 | |
| 1104 | return ret; |
| 1105 | } |
| 1106 | |
| 1107 | static int ks_pcie_set_mode(struct device *dev) |
| 1108 | { |
| 1109 | struct device_node *np = dev->of_node; |
| 1110 | struct regmap *syscon; |
| 1111 | u32 val; |
| 1112 | u32 mask; |
| 1113 | int ret = 0; |
| 1114 | |
| 1115 | syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode"); |
| 1116 | if (IS_ERR(syscon)) |
| 1117 | return 0; |
| 1118 | |
| 1119 | mask = KS_PCIE_DEV_TYPE_MASK | KS_PCIE_SYSCLOCKOUTEN; |
| 1120 | val = KS_PCIE_DEV_TYPE(RC) | KS_PCIE_SYSCLOCKOUTEN; |
| 1121 | |
| 1122 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 1123 | if (ret) { |
| 1124 | dev_err(dev, "failed to set pcie mode\n"); |
| 1125 | return ret; |
| 1126 | } |
| 1127 | |
| 1128 | return 0; |
| 1129 | } |
| 1130 | |
| 1131 | static int ks_pcie_am654_set_mode(struct device *dev, |
| 1132 | enum dw_pcie_device_mode mode) |
| 1133 | { |
| 1134 | struct device_node *np = dev->of_node; |
| 1135 | struct regmap *syscon; |
| 1136 | u32 val; |
| 1137 | u32 mask; |
| 1138 | int ret = 0; |
| 1139 | |
| 1140 | syscon = syscon_regmap_lookup_by_phandle(np, "ti,syscon-pcie-mode"); |
| 1141 | if (IS_ERR(syscon)) |
| 1142 | return 0; |
| 1143 | |
| 1144 | mask = AM654_PCIE_DEV_TYPE_MASK; |
| 1145 | |
| 1146 | switch (mode) { |
| 1147 | case DW_PCIE_RC_TYPE: |
| 1148 | val = RC; |
| 1149 | break; |
| 1150 | case DW_PCIE_EP_TYPE: |
| 1151 | val = EP; |
| 1152 | break; |
| 1153 | default: |
| 1154 | dev_err(dev, "INVALID device type %d\n", mode); |
| 1155 | return -EINVAL; |
| 1156 | } |
| 1157 | |
| 1158 | ret = regmap_update_bits(syscon, 0, mask, val); |
| 1159 | if (ret) { |
| 1160 | dev_err(dev, "failed to set pcie mode\n"); |
| 1161 | return ret; |
| 1162 | } |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | static void ks_pcie_set_link_speed(struct dw_pcie *pci, int link_speed) |
| 1168 | { |
| 1169 | u32 val; |
| 1170 | |
| 1171 | dw_pcie_dbi_ro_wr_en(pci); |
| 1172 | |
| 1173 | val = dw_pcie_readl_dbi(pci, EXP_CAP_ID_OFFSET + PCI_EXP_LNKCAP); |
| 1174 | if ((val & PCI_EXP_LNKCAP_SLS) != link_speed) { |
| 1175 | val &= ~((u32)PCI_EXP_LNKCAP_SLS); |
| 1176 | val |= link_speed; |
| 1177 | dw_pcie_writel_dbi(pci, EXP_CAP_ID_OFFSET + PCI_EXP_LNKCAP, |
| 1178 | val); |
| 1179 | } |
| 1180 | |
| 1181 | val = dw_pcie_readl_dbi(pci, EXP_CAP_ID_OFFSET + PCI_EXP_LNKCTL2); |
| 1182 | if ((val & PCI_EXP_LNKCAP_SLS) != link_speed) { |
| 1183 | val &= ~((u32)PCI_EXP_LNKCAP_SLS); |
| 1184 | val |= link_speed; |
| 1185 | dw_pcie_writel_dbi(pci, EXP_CAP_ID_OFFSET + PCI_EXP_LNKCTL2, |
| 1186 | val); |
| 1187 | } |
| 1188 | |
| 1189 | dw_pcie_dbi_ro_wr_dis(pci); |
| 1190 | } |
| 1191 | |
| 1192 | static const struct ks_pcie_of_data ks_pcie_rc_of_data = { |
| 1193 | .host_ops = &ks_pcie_host_ops, |
| 1194 | .version = 0x365A, |
| 1195 | }; |
| 1196 | |
| 1197 | static const struct ks_pcie_of_data ks_pcie_am654_rc_of_data = { |
| 1198 | .host_ops = &ks_pcie_am654_host_ops, |
| 1199 | .mode = DW_PCIE_RC_TYPE, |
| 1200 | .version = 0x490A, |
| 1201 | }; |
| 1202 | |
| 1203 | static const struct ks_pcie_of_data ks_pcie_am654_ep_of_data = { |
| 1204 | .ep_ops = &ks_pcie_am654_ep_ops, |
| 1205 | .mode = DW_PCIE_EP_TYPE, |
| 1206 | .version = 0x490A, |
| 1207 | }; |
| 1208 | |
| 1209 | static const struct of_device_id ks_pcie_of_match[] = { |
| 1210 | { |
| 1211 | .type = "pci", |
| 1212 | .data = &ks_pcie_rc_of_data, |
| 1213 | .compatible = "ti,keystone-pcie", |
| 1214 | }, |
| 1215 | { |
| 1216 | .data = &ks_pcie_am654_rc_of_data, |
| 1217 | .compatible = "ti,am654-pcie-rc", |
| 1218 | }, |
| 1219 | { |
| 1220 | .data = &ks_pcie_am654_ep_of_data, |
| 1221 | .compatible = "ti,am654-pcie-ep", |
| 1222 | }, |
| 1223 | { }, |
| 1224 | }; |
| 1225 | |
| 1226 | static int ks_pcie_probe(struct platform_device *pdev) |
| 1227 | { |
| 1228 | const struct dw_pcie_host_ops *host_ops; |
| 1229 | const struct dw_pcie_ep_ops *ep_ops; |
| 1230 | struct device *dev = &pdev->dev; |
| 1231 | struct device_node *np = dev->of_node; |
| 1232 | const struct ks_pcie_of_data *data; |
| 1233 | const struct of_device_id *match; |
| 1234 | enum dw_pcie_device_mode mode; |
| 1235 | struct dw_pcie *pci; |
| 1236 | struct keystone_pcie *ks_pcie; |
| 1237 | struct device_link **link; |
| 1238 | struct gpio_desc *gpiod; |
| 1239 | void __iomem *atu_base; |
| 1240 | struct resource *res; |
| 1241 | unsigned int version; |
| 1242 | void __iomem *base; |
| 1243 | u32 num_viewport; |
| 1244 | struct phy **phy; |
| 1245 | int link_speed; |
| 1246 | u32 num_lanes; |
| 1247 | char name[10]; |
| 1248 | int ret; |
| 1249 | int irq; |
| 1250 | int i; |
| 1251 | |
| 1252 | match = of_match_device(of_match_ptr(ks_pcie_of_match), dev); |
| 1253 | data = (struct ks_pcie_of_data *)match->data; |
| 1254 | if (!data) |
| 1255 | return -EINVAL; |
| 1256 | |
| 1257 | version = data->version; |
| 1258 | host_ops = data->host_ops; |
| 1259 | ep_ops = data->ep_ops; |
| 1260 | mode = data->mode; |
| 1261 | |
| 1262 | ks_pcie = devm_kzalloc(dev, sizeof(*ks_pcie), GFP_KERNEL); |
| 1263 | if (!ks_pcie) |
| 1264 | return -ENOMEM; |
| 1265 | |
| 1266 | pci = devm_kzalloc(dev, sizeof(*pci), GFP_KERNEL); |
| 1267 | if (!pci) |
| 1268 | return -ENOMEM; |
| 1269 | |
| 1270 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "app"); |
| 1271 | ks_pcie->va_app_base = devm_ioremap_resource(dev, res); |
| 1272 | if (IS_ERR(ks_pcie->va_app_base)) |
| 1273 | return PTR_ERR(ks_pcie->va_app_base); |
| 1274 | |
| 1275 | ks_pcie->app = *res; |
| 1276 | |
| 1277 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dbics"); |
| 1278 | base = devm_pci_remap_cfg_resource(dev, res); |
| 1279 | if (IS_ERR(base)) |
| 1280 | return PTR_ERR(base); |
| 1281 | |
| 1282 | if (of_device_is_compatible(np, "ti,am654-pcie-rc")) |
| 1283 | ks_pcie->is_am6 = true; |
| 1284 | |
| 1285 | pci->dbi_base = base; |
| 1286 | pci->dbi_base2 = base; |
| 1287 | pci->dev = dev; |
| 1288 | pci->ops = &ks_pcie_dw_pcie_ops; |
| 1289 | pci->version = version; |
| 1290 | |
| 1291 | irq = platform_get_irq(pdev, 0); |
| 1292 | if (irq < 0) { |
| 1293 | dev_err(dev, "missing IRQ resource: %d\n", irq); |
| 1294 | return irq; |
| 1295 | } |
| 1296 | |
| 1297 | ret = request_irq(irq, ks_pcie_err_irq_handler, IRQF_SHARED, |
| 1298 | "ks-pcie-error-irq", ks_pcie); |
| 1299 | if (ret < 0) { |
| 1300 | dev_err(dev, "failed to request error IRQ %d\n", |
| 1301 | irq); |
| 1302 | return ret; |
| 1303 | } |
| 1304 | |
| 1305 | ret = of_property_read_u32(np, "num-lanes", &num_lanes); |
| 1306 | if (ret) |
| 1307 | num_lanes = 1; |
| 1308 | |
| 1309 | phy = devm_kzalloc(dev, sizeof(*phy) * num_lanes, GFP_KERNEL); |
| 1310 | if (!phy) |
| 1311 | return -ENOMEM; |
| 1312 | |
| 1313 | link = devm_kzalloc(dev, sizeof(*link) * num_lanes, GFP_KERNEL); |
| 1314 | if (!link) |
| 1315 | return -ENOMEM; |
| 1316 | |
| 1317 | for (i = 0; i < num_lanes; i++) { |
| 1318 | snprintf(name, sizeof(name), "pcie-phy%d", i); |
| 1319 | phy[i] = devm_phy_optional_get(dev, name); |
| 1320 | if (IS_ERR(phy[i])) { |
| 1321 | ret = PTR_ERR(phy[i]); |
| 1322 | goto err_link; |
| 1323 | } |
| 1324 | |
| 1325 | if (!phy[i]) |
| 1326 | continue; |
| 1327 | |
| 1328 | link[i] = device_link_add(dev, &phy[i]->dev, DL_FLAG_STATELESS); |
| 1329 | if (!link[i]) { |
| 1330 | ret = -EINVAL; |
| 1331 | goto err_link; |
| 1332 | } |
| 1333 | } |
| 1334 | |
| 1335 | ks_pcie->np = np; |
| 1336 | ks_pcie->pci = pci; |
| 1337 | ks_pcie->link = link; |
| 1338 | ks_pcie->num_lanes = num_lanes; |
| 1339 | ks_pcie->phy = phy; |
| 1340 | |
| 1341 | gpiod = devm_gpiod_get_optional(dev, "reset", |
| 1342 | GPIOD_OUT_LOW); |
| 1343 | if (IS_ERR(gpiod)) { |
| 1344 | ret = PTR_ERR(gpiod); |
| 1345 | if (ret != -EPROBE_DEFER) |
| 1346 | dev_err(dev, "Failed to get reset GPIO\n"); |
| 1347 | goto err_link; |
| 1348 | } |
| 1349 | |
| 1350 | /* Obtain references to the PHYs */ |
| 1351 | for (i = 0; i < num_lanes; i++) |
| 1352 | phy_pm_runtime_get_sync(ks_pcie->phy[i]); |
| 1353 | |
| 1354 | ret = ks_pcie_enable_phy(ks_pcie); |
| 1355 | |
| 1356 | /* Release references to the PHYs */ |
| 1357 | for (i = 0; i < num_lanes; i++) |
| 1358 | phy_pm_runtime_put_sync(ks_pcie->phy[i]); |
| 1359 | |
| 1360 | if (ret) { |
| 1361 | dev_err(dev, "failed to enable phy\n"); |
| 1362 | goto err_link; |
| 1363 | } |
| 1364 | |
| 1365 | platform_set_drvdata(pdev, ks_pcie); |
| 1366 | pm_runtime_enable(dev); |
| 1367 | ret = pm_runtime_get_sync(dev); |
| 1368 | if (ret < 0) { |
| 1369 | dev_err(dev, "pm_runtime_get_sync failed\n"); |
| 1370 | goto err_get_sync; |
| 1371 | } |
| 1372 | |
| 1373 | if (pci->version >= 0x480A) { |
| 1374 | res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "atu"); |
| 1375 | atu_base = devm_ioremap_resource(dev, res); |
| 1376 | if (IS_ERR(atu_base)) { |
| 1377 | ret = PTR_ERR(atu_base); |
| 1378 | goto err_get_sync; |
| 1379 | } |
| 1380 | |
| 1381 | pci->atu_base = atu_base; |
| 1382 | |
| 1383 | ret = ks_pcie_am654_set_mode(dev, mode); |
| 1384 | if (ret < 0) |
| 1385 | goto err_get_sync; |
| 1386 | } else { |
| 1387 | ret = ks_pcie_set_mode(dev); |
| 1388 | if (ret < 0) |
| 1389 | goto err_get_sync; |
| 1390 | } |
| 1391 | |
| 1392 | link_speed = of_pci_get_max_link_speed(np); |
| 1393 | if (link_speed < 0) |
| 1394 | link_speed = 2; |
| 1395 | |
| 1396 | ks_pcie_set_link_speed(pci, link_speed); |
| 1397 | |
| 1398 | switch (mode) { |
| 1399 | case DW_PCIE_RC_TYPE: |
| 1400 | if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_HOST)) { |
| 1401 | ret = -ENODEV; |
| 1402 | goto err_get_sync; |
| 1403 | } |
| 1404 | |
| 1405 | ret = of_property_read_u32(np, "num-viewport", &num_viewport); |
| 1406 | if (ret < 0) { |
| 1407 | dev_err(dev, "unable to read *num-viewport* property\n"); |
| 1408 | goto err_get_sync; |
| 1409 | } |
| 1410 | |
| 1411 | /* |
| 1412 | * "Power Sequencing and Reset Signal Timings" table in |
| 1413 | * PCI EXPRESS CARD ELECTROMECHANICAL SPECIFICATION, REV. 2.0 |
| 1414 | * indicates PERST# should be deasserted after minimum of 100us |
| 1415 | * once REFCLK is stable. The REFCLK to the connector in RC |
| 1416 | * mode is selected while enabling the PHY. So deassert PERST# |
| 1417 | * after 100 us. |
| 1418 | */ |
| 1419 | if (gpiod) { |
| 1420 | usleep_range(100, 200); |
| 1421 | gpiod_set_value_cansleep(gpiod, 1); |
| 1422 | } |
| 1423 | |
| 1424 | ks_pcie->num_viewport = num_viewport; |
| 1425 | pci->pp.ops = host_ops; |
| 1426 | ret = ks_pcie_add_pcie_port(ks_pcie, pdev); |
| 1427 | if (ret < 0) |
| 1428 | goto err_get_sync; |
| 1429 | break; |
| 1430 | case DW_PCIE_EP_TYPE: |
| 1431 | if (!IS_ENABLED(CONFIG_PCI_KEYSTONE_EP)) { |
| 1432 | ret = -ENODEV; |
| 1433 | goto err_get_sync; |
| 1434 | } |
| 1435 | |
| 1436 | pci->ep.ops = ep_ops; |
| 1437 | ret = ks_pcie_add_pcie_ep(ks_pcie, pdev); |
| 1438 | if (ret < 0) |
| 1439 | goto err_get_sync; |
| 1440 | break; |
| 1441 | default: |
| 1442 | dev_err(dev, "INVALID device type %d\n", mode); |
| 1443 | } |
| 1444 | |
| 1445 | ks_pcie_enable_error_irq(ks_pcie); |
| 1446 | |
| 1447 | return 0; |
| 1448 | |
| 1449 | err_get_sync: |
| 1450 | pm_runtime_put(dev); |
| 1451 | pm_runtime_disable(dev); |
| 1452 | ks_pcie_disable_phy(ks_pcie); |
| 1453 | |
| 1454 | err_link: |
| 1455 | while (--i >= 0 && link[i]) |
| 1456 | device_link_del(link[i]); |
| 1457 | |
| 1458 | return ret; |
| 1459 | } |
| 1460 | |
| 1461 | static int ks_pcie_remove(struct platform_device *pdev) |
| 1462 | { |
| 1463 | struct keystone_pcie *ks_pcie = platform_get_drvdata(pdev); |
| 1464 | struct device_link **link = ks_pcie->link; |
| 1465 | int num_lanes = ks_pcie->num_lanes; |
| 1466 | struct device *dev = &pdev->dev; |
| 1467 | |
| 1468 | pm_runtime_put(dev); |
| 1469 | pm_runtime_disable(dev); |
| 1470 | ks_pcie_disable_phy(ks_pcie); |
| 1471 | while (num_lanes--) |
| 1472 | device_link_del(link[num_lanes]); |
| 1473 | |
| 1474 | return 0; |
| 1475 | } |
| 1476 | |
| 1477 | static struct platform_driver ks_pcie_driver = { |
| 1478 | .probe = ks_pcie_probe, |
| 1479 | .remove = ks_pcie_remove, |
| 1480 | .driver = { |
| 1481 | .name = "keystone-pcie", |
| 1482 | .of_match_table = of_match_ptr(ks_pcie_of_match), |
| 1483 | }, |
| 1484 | }; |
| 1485 | builtin_platform_driver(ks_pcie_driver); |