1 // SPDX-License-Identifier: GPL-2.0 2 /* 3 * PCIe driver for Marvell Armada 370 and Armada XP SoCs 4 * 5 * Author: Thomas Petazzoni <thomas.petazzoni@free-electrons.com> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/pci.h> 11 #include <linux/clk.h> 12 #include <linux/delay.h> 13 #include <linux/gpio.h> 14 #include <linux/init.h> 15 #include <linux/mbus.h> 16 #include <linux/slab.h> 17 #include <linux/platform_device.h> 18 #include <linux/of_address.h> 19 #include <linux/of_irq.h> 20 #include <linux/of_gpio.h> 21 #include <linux/of_pci.h> 22 #include <linux/of_platform.h> 23 24 #include "../pci.h" 25 #include "../pci-bridge-emul.h" 26 27 /* 28 * PCIe unit register offsets. 29 */ 30 #define PCIE_DEV_ID_OFF 0x0000 31 #define PCIE_CMD_OFF 0x0004 32 #define PCIE_DEV_REV_OFF 0x0008 33 #define PCIE_BAR_LO_OFF(n) (0x0010 + ((n) << 3)) 34 #define PCIE_BAR_HI_OFF(n) (0x0014 + ((n) << 3)) 35 #define PCIE_CAP_PCIEXP 0x0060 36 #define PCIE_HEADER_LOG_4_OFF 0x0128 37 #define PCIE_BAR_CTRL_OFF(n) (0x1804 + (((n) - 1) * 4)) 38 #define PCIE_WIN04_CTRL_OFF(n) (0x1820 + ((n) << 4)) 39 #define PCIE_WIN04_BASE_OFF(n) (0x1824 + ((n) << 4)) 40 #define PCIE_WIN04_REMAP_OFF(n) (0x182c + ((n) << 4)) 41 #define PCIE_WIN5_CTRL_OFF 0x1880 42 #define PCIE_WIN5_BASE_OFF 0x1884 43 #define PCIE_WIN5_REMAP_OFF 0x188c 44 #define PCIE_CONF_ADDR_OFF 0x18f8 45 #define PCIE_CONF_ADDR_EN 0x80000000 46 #define PCIE_CONF_REG(r) ((((r) & 0xf00) << 16) | ((r) & 0xfc)) 47 #define PCIE_CONF_BUS(b) (((b) & 0xff) << 16) 48 #define PCIE_CONF_DEV(d) (((d) & 0x1f) << 11) 49 #define PCIE_CONF_FUNC(f) (((f) & 0x7) << 8) 50 #define PCIE_CONF_ADDR(bus, devfn, where) \ 51 (PCIE_CONF_BUS(bus) | PCIE_CONF_DEV(PCI_SLOT(devfn)) | \ 52 PCIE_CONF_FUNC(PCI_FUNC(devfn)) | PCIE_CONF_REG(where) | \ 53 PCIE_CONF_ADDR_EN) 54 #define PCIE_CONF_DATA_OFF 0x18fc 55 #define PCIE_INT_CAUSE_OFF 0x1900 56 #define PCIE_INT_PM_PME BIT(28) 57 #define PCIE_MASK_OFF 0x1910 58 #define PCIE_MASK_ENABLE_INTS 0x0f000000 59 #define PCIE_CTRL_OFF 0x1a00 60 #define PCIE_CTRL_X1_MODE 0x0001 61 #define PCIE_CTRL_RC_MODE BIT(1) 62 #define PCIE_CTRL_MASTER_HOT_RESET BIT(24) 63 #define PCIE_STAT_OFF 0x1a04 64 #define PCIE_STAT_BUS 0xff00 65 #define PCIE_STAT_DEV 0x1f0000 66 #define PCIE_STAT_LINK_DOWN BIT(0) 67 #define PCIE_RC_RTSTA 0x1a14 68 #define PCIE_DEBUG_CTRL 0x1a60 69 #define PCIE_DEBUG_SOFT_RESET BIT(20) 70 71 struct mvebu_pcie_port; 72 73 /* Structure representing all PCIe interfaces */ 74 struct mvebu_pcie { 75 struct platform_device *pdev; 76 struct mvebu_pcie_port *ports; 77 struct resource io; 78 struct resource realio; 79 struct resource mem; 80 struct resource busn; 81 int nports; 82 }; 83 84 struct mvebu_pcie_window { 85 phys_addr_t base; 86 phys_addr_t remap; 87 size_t size; 88 }; 89 90 /* Structure representing one PCIe interface */ 91 struct mvebu_pcie_port { 92 char *name; 93 void __iomem *base; 94 u32 port; 95 u32 lane; 96 int devfn; 97 unsigned int mem_target; 98 unsigned int mem_attr; 99 unsigned int io_target; 100 unsigned int io_attr; 101 struct clk *clk; 102 struct gpio_desc *reset_gpio; 103 char *reset_name; 104 struct pci_bridge_emul bridge; 105 struct device_node *dn; 106 struct mvebu_pcie *pcie; 107 struct mvebu_pcie_window memwin; 108 struct mvebu_pcie_window iowin; 109 u32 saved_pcie_stat; 110 struct resource regs; 111 }; 112 113 static inline void mvebu_writel(struct mvebu_pcie_port *port, u32 val, u32 reg) 114 { 115 writel(val, port->base + reg); 116 } 117 118 static inline u32 mvebu_readl(struct mvebu_pcie_port *port, u32 reg) 119 { 120 return readl(port->base + reg); 121 } 122 123 static inline bool mvebu_has_ioport(struct mvebu_pcie_port *port) 124 { 125 return port->io_target != -1 && port->io_attr != -1; 126 } 127 128 static bool mvebu_pcie_link_up(struct mvebu_pcie_port *port) 129 { 130 return !(mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_LINK_DOWN); 131 } 132 133 static u8 mvebu_pcie_get_local_bus_nr(struct mvebu_pcie_port *port) 134 { 135 return (mvebu_readl(port, PCIE_STAT_OFF) & PCIE_STAT_BUS) >> 8; 136 } 137 138 static void mvebu_pcie_set_local_bus_nr(struct mvebu_pcie_port *port, int nr) 139 { 140 u32 stat; 141 142 stat = mvebu_readl(port, PCIE_STAT_OFF); 143 stat &= ~PCIE_STAT_BUS; 144 stat |= nr << 8; 145 mvebu_writel(port, stat, PCIE_STAT_OFF); 146 } 147 148 static void mvebu_pcie_set_local_dev_nr(struct mvebu_pcie_port *port, int nr) 149 { 150 u32 stat; 151 152 stat = mvebu_readl(port, PCIE_STAT_OFF); 153 stat &= ~PCIE_STAT_DEV; 154 stat |= nr << 16; 155 mvebu_writel(port, stat, PCIE_STAT_OFF); 156 } 157 158 static void mvebu_pcie_disable_wins(struct mvebu_pcie_port *port) 159 { 160 int i; 161 162 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(0)); 163 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0)); 164 165 for (i = 1; i < 3; i++) { 166 mvebu_writel(port, 0, PCIE_BAR_CTRL_OFF(i)); 167 mvebu_writel(port, 0, PCIE_BAR_LO_OFF(i)); 168 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(i)); 169 } 170 171 for (i = 0; i < 5; i++) { 172 mvebu_writel(port, 0, PCIE_WIN04_CTRL_OFF(i)); 173 mvebu_writel(port, 0, PCIE_WIN04_BASE_OFF(i)); 174 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i)); 175 } 176 177 mvebu_writel(port, 0, PCIE_WIN5_CTRL_OFF); 178 mvebu_writel(port, 0, PCIE_WIN5_BASE_OFF); 179 mvebu_writel(port, 0, PCIE_WIN5_REMAP_OFF); 180 } 181 182 /* 183 * Setup PCIE BARs and Address Decode Wins: 184 * BAR[0] -> internal registers (needed for MSI) 185 * BAR[1] -> covers all DRAM banks 186 * BAR[2] -> Disabled 187 * WIN[0-3] -> DRAM bank[0-3] 188 */ 189 static void mvebu_pcie_setup_wins(struct mvebu_pcie_port *port) 190 { 191 const struct mbus_dram_target_info *dram; 192 u32 size; 193 int i; 194 195 dram = mv_mbus_dram_info(); 196 197 /* First, disable and clear BARs and windows. */ 198 mvebu_pcie_disable_wins(port); 199 200 /* Setup windows for DDR banks. Count total DDR size on the fly. */ 201 size = 0; 202 for (i = 0; i < dram->num_cs; i++) { 203 const struct mbus_dram_window *cs = dram->cs + i; 204 205 mvebu_writel(port, cs->base & 0xffff0000, 206 PCIE_WIN04_BASE_OFF(i)); 207 mvebu_writel(port, 0, PCIE_WIN04_REMAP_OFF(i)); 208 mvebu_writel(port, 209 ((cs->size - 1) & 0xffff0000) | 210 (cs->mbus_attr << 8) | 211 (dram->mbus_dram_target_id << 4) | 1, 212 PCIE_WIN04_CTRL_OFF(i)); 213 214 size += cs->size; 215 } 216 217 /* Round up 'size' to the nearest power of two. */ 218 if ((size & (size - 1)) != 0) 219 size = 1 << fls(size); 220 221 /* Setup BAR[1] to all DRAM banks. */ 222 mvebu_writel(port, dram->cs[0].base, PCIE_BAR_LO_OFF(1)); 223 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(1)); 224 mvebu_writel(port, ((size - 1) & 0xffff0000) | 1, 225 PCIE_BAR_CTRL_OFF(1)); 226 227 /* 228 * Point BAR[0] to the device's internal registers. 229 */ 230 mvebu_writel(port, round_down(port->regs.start, SZ_1M), PCIE_BAR_LO_OFF(0)); 231 mvebu_writel(port, 0, PCIE_BAR_HI_OFF(0)); 232 } 233 234 static void mvebu_pcie_setup_hw(struct mvebu_pcie_port *port) 235 { 236 u32 ctrl, cmd, dev_rev, mask; 237 238 /* Setup PCIe controller to Root Complex mode. */ 239 ctrl = mvebu_readl(port, PCIE_CTRL_OFF); 240 ctrl |= PCIE_CTRL_RC_MODE; 241 mvebu_writel(port, ctrl, PCIE_CTRL_OFF); 242 243 /* Disable Root Bridge I/O space, memory space and bus mastering. */ 244 cmd = mvebu_readl(port, PCIE_CMD_OFF); 245 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 246 mvebu_writel(port, cmd, PCIE_CMD_OFF); 247 248 /* 249 * Change Class Code of PCI Bridge device to PCI Bridge (0x6004) 250 * because default value is Memory controller (0x5080). 251 * 252 * Note that this mvebu PCI Bridge does not have compliant Type 1 253 * Configuration Space. Header Type is reported as Type 0 and it 254 * has format of Type 0 config space. 255 * 256 * Moreover Type 0 BAR registers (ranges 0x10 - 0x28 and 0x30 - 0x34) 257 * have the same format in Marvell's specification as in PCIe 258 * specification, but their meaning is totally different and they do 259 * different things: they are aliased into internal mvebu registers 260 * (e.g. PCIE_BAR_LO_OFF) and these should not be changed or 261 * reconfigured by pci device drivers. 262 * 263 * Therefore driver uses emulation of PCI Bridge which emulates 264 * access to configuration space via internal mvebu registers or 265 * emulated configuration buffer. Driver access these PCI Bridge 266 * directly for simplification, but these registers can be accessed 267 * also via standard mvebu way for accessing PCI config space. 268 */ 269 dev_rev = mvebu_readl(port, PCIE_DEV_REV_OFF); 270 dev_rev &= ~0xffffff00; 271 dev_rev |= (PCI_CLASS_BRIDGE_PCI << 8) << 8; 272 mvebu_writel(port, dev_rev, PCIE_DEV_REV_OFF); 273 274 /* Point PCIe unit MBUS decode windows to DRAM space. */ 275 mvebu_pcie_setup_wins(port); 276 277 /* Enable interrupt lines A-D. */ 278 mask = mvebu_readl(port, PCIE_MASK_OFF); 279 mask |= PCIE_MASK_ENABLE_INTS; 280 mvebu_writel(port, mask, PCIE_MASK_OFF); 281 } 282 283 static int mvebu_pcie_hw_rd_conf(struct mvebu_pcie_port *port, 284 struct pci_bus *bus, 285 u32 devfn, int where, int size, u32 *val) 286 { 287 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF; 288 289 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where), 290 PCIE_CONF_ADDR_OFF); 291 292 switch (size) { 293 case 1: 294 *val = readb_relaxed(conf_data + (where & 3)); 295 break; 296 case 2: 297 *val = readw_relaxed(conf_data + (where & 2)); 298 break; 299 case 4: 300 *val = readl_relaxed(conf_data); 301 break; 302 default: 303 *val = 0xffffffff; 304 return PCIBIOS_BAD_REGISTER_NUMBER; 305 } 306 307 return PCIBIOS_SUCCESSFUL; 308 } 309 310 static int mvebu_pcie_hw_wr_conf(struct mvebu_pcie_port *port, 311 struct pci_bus *bus, 312 u32 devfn, int where, int size, u32 val) 313 { 314 void __iomem *conf_data = port->base + PCIE_CONF_DATA_OFF; 315 316 mvebu_writel(port, PCIE_CONF_ADDR(bus->number, devfn, where), 317 PCIE_CONF_ADDR_OFF); 318 319 switch (size) { 320 case 1: 321 writeb(val, conf_data + (where & 3)); 322 break; 323 case 2: 324 writew(val, conf_data + (where & 2)); 325 break; 326 case 4: 327 writel(val, conf_data); 328 break; 329 default: 330 return PCIBIOS_BAD_REGISTER_NUMBER; 331 } 332 333 return PCIBIOS_SUCCESSFUL; 334 } 335 336 /* 337 * Remove windows, starting from the largest ones to the smallest 338 * ones. 339 */ 340 static void mvebu_pcie_del_windows(struct mvebu_pcie_port *port, 341 phys_addr_t base, size_t size) 342 { 343 while (size) { 344 size_t sz = 1 << (fls(size) - 1); 345 346 mvebu_mbus_del_window(base, sz); 347 base += sz; 348 size -= sz; 349 } 350 } 351 352 /* 353 * MBus windows can only have a power of two size, but PCI BARs do not 354 * have this constraint. Therefore, we have to split the PCI BAR into 355 * areas each having a power of two size. We start from the largest 356 * one (i.e highest order bit set in the size). 357 */ 358 static int mvebu_pcie_add_windows(struct mvebu_pcie_port *port, 359 unsigned int target, unsigned int attribute, 360 phys_addr_t base, size_t size, 361 phys_addr_t remap) 362 { 363 size_t size_mapped = 0; 364 365 while (size) { 366 size_t sz = 1 << (fls(size) - 1); 367 int ret; 368 369 ret = mvebu_mbus_add_window_remap_by_id(target, attribute, base, 370 sz, remap); 371 if (ret) { 372 phys_addr_t end = base + sz - 1; 373 374 dev_err(&port->pcie->pdev->dev, 375 "Could not create MBus window at [mem %pa-%pa]: %d\n", 376 &base, &end, ret); 377 mvebu_pcie_del_windows(port, base - size_mapped, 378 size_mapped); 379 return ret; 380 } 381 382 size -= sz; 383 size_mapped += sz; 384 base += sz; 385 if (remap != MVEBU_MBUS_NO_REMAP) 386 remap += sz; 387 } 388 389 return 0; 390 } 391 392 static int mvebu_pcie_set_window(struct mvebu_pcie_port *port, 393 unsigned int target, unsigned int attribute, 394 const struct mvebu_pcie_window *desired, 395 struct mvebu_pcie_window *cur) 396 { 397 int ret; 398 399 if (desired->base == cur->base && desired->remap == cur->remap && 400 desired->size == cur->size) 401 return 0; 402 403 if (cur->size != 0) { 404 mvebu_pcie_del_windows(port, cur->base, cur->size); 405 cur->size = 0; 406 cur->base = 0; 407 408 /* 409 * If something tries to change the window while it is enabled 410 * the change will not be done atomically. That would be 411 * difficult to do in the general case. 412 */ 413 } 414 415 if (desired->size == 0) 416 return 0; 417 418 ret = mvebu_pcie_add_windows(port, target, attribute, desired->base, 419 desired->size, desired->remap); 420 if (ret) { 421 cur->size = 0; 422 cur->base = 0; 423 return ret; 424 } 425 426 *cur = *desired; 427 return 0; 428 } 429 430 static int mvebu_pcie_handle_iobase_change(struct mvebu_pcie_port *port) 431 { 432 struct mvebu_pcie_window desired = {}; 433 struct pci_bridge_emul_conf *conf = &port->bridge.conf; 434 435 /* Are the new iobase/iolimit values invalid? */ 436 if (conf->iolimit < conf->iobase || 437 conf->iolimitupper < conf->iobaseupper) 438 return mvebu_pcie_set_window(port, port->io_target, port->io_attr, 439 &desired, &port->iowin); 440 441 if (!mvebu_has_ioport(port)) { 442 dev_WARN(&port->pcie->pdev->dev, 443 "Attempt to set IO when IO is disabled\n"); 444 return -EOPNOTSUPP; 445 } 446 447 /* 448 * We read the PCI-to-PCI bridge emulated registers, and 449 * calculate the base address and size of the address decoding 450 * window to setup, according to the PCI-to-PCI bridge 451 * specifications. iobase is the bus address, port->iowin_base 452 * is the CPU address. 453 */ 454 desired.remap = ((conf->iobase & 0xF0) << 8) | 455 (conf->iobaseupper << 16); 456 desired.base = port->pcie->io.start + desired.remap; 457 desired.size = ((0xFFF | ((conf->iolimit & 0xF0) << 8) | 458 (conf->iolimitupper << 16)) - 459 desired.remap) + 460 1; 461 462 return mvebu_pcie_set_window(port, port->io_target, port->io_attr, &desired, 463 &port->iowin); 464 } 465 466 static int mvebu_pcie_handle_membase_change(struct mvebu_pcie_port *port) 467 { 468 struct mvebu_pcie_window desired = {.remap = MVEBU_MBUS_NO_REMAP}; 469 struct pci_bridge_emul_conf *conf = &port->bridge.conf; 470 471 /* Are the new membase/memlimit values invalid? */ 472 if (conf->memlimit < conf->membase) 473 return mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, 474 &desired, &port->memwin); 475 476 /* 477 * We read the PCI-to-PCI bridge emulated registers, and 478 * calculate the base address and size of the address decoding 479 * window to setup, according to the PCI-to-PCI bridge 480 * specifications. 481 */ 482 desired.base = ((conf->membase & 0xFFF0) << 16); 483 desired.size = (((conf->memlimit & 0xFFF0) << 16) | 0xFFFFF) - 484 desired.base + 1; 485 486 return mvebu_pcie_set_window(port, port->mem_target, port->mem_attr, &desired, 487 &port->memwin); 488 } 489 490 static pci_bridge_emul_read_status_t 491 mvebu_pci_bridge_emul_base_conf_read(struct pci_bridge_emul *bridge, 492 int reg, u32 *value) 493 { 494 struct mvebu_pcie_port *port = bridge->data; 495 496 switch (reg) { 497 case PCI_COMMAND: 498 *value = mvebu_readl(port, PCIE_CMD_OFF); 499 break; 500 501 case PCI_PRIMARY_BUS: { 502 /* 503 * From the whole 32bit register we support reading from HW only 504 * secondary bus number which is mvebu local bus number. 505 * Other bits are retrieved only from emulated config buffer. 506 */ 507 __le32 *cfgspace = (__le32 *)&bridge->conf; 508 u32 val = le32_to_cpu(cfgspace[PCI_PRIMARY_BUS / 4]); 509 val &= ~0xff00; 510 val |= mvebu_pcie_get_local_bus_nr(port) << 8; 511 *value = val; 512 break; 513 } 514 515 case PCI_INTERRUPT_LINE: { 516 /* 517 * From the whole 32bit register we support reading from HW only 518 * one bit: PCI_BRIDGE_CTL_BUS_RESET. 519 * Other bits are retrieved only from emulated config buffer. 520 */ 521 __le32 *cfgspace = (__le32 *)&bridge->conf; 522 u32 val = le32_to_cpu(cfgspace[PCI_INTERRUPT_LINE / 4]); 523 if (mvebu_readl(port, PCIE_CTRL_OFF) & PCIE_CTRL_MASTER_HOT_RESET) 524 val |= PCI_BRIDGE_CTL_BUS_RESET << 16; 525 else 526 val &= ~(PCI_BRIDGE_CTL_BUS_RESET << 16); 527 *value = val; 528 break; 529 } 530 531 default: 532 return PCI_BRIDGE_EMUL_NOT_HANDLED; 533 } 534 535 return PCI_BRIDGE_EMUL_HANDLED; 536 } 537 538 static pci_bridge_emul_read_status_t 539 mvebu_pci_bridge_emul_pcie_conf_read(struct pci_bridge_emul *bridge, 540 int reg, u32 *value) 541 { 542 struct mvebu_pcie_port *port = bridge->data; 543 544 switch (reg) { 545 case PCI_EXP_DEVCAP: 546 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP); 547 break; 548 549 case PCI_EXP_DEVCTL: 550 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL); 551 break; 552 553 case PCI_EXP_LNKCAP: 554 /* 555 * PCIe requires the clock power management capability to be 556 * hard-wired to zero for downstream ports 557 */ 558 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCAP) & 559 ~PCI_EXP_LNKCAP_CLKPM; 560 break; 561 562 case PCI_EXP_LNKCTL: 563 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL); 564 break; 565 566 case PCI_EXP_SLTCTL: 567 *value = PCI_EXP_SLTSTA_PDS << 16; 568 break; 569 570 case PCI_EXP_RTSTA: 571 *value = mvebu_readl(port, PCIE_RC_RTSTA); 572 break; 573 574 case PCI_EXP_DEVCAP2: 575 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCAP2); 576 break; 577 578 case PCI_EXP_DEVCTL2: 579 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL2); 580 break; 581 582 case PCI_EXP_LNKCTL2: 583 *value = mvebu_readl(port, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL2); 584 break; 585 586 default: 587 return PCI_BRIDGE_EMUL_NOT_HANDLED; 588 } 589 590 return PCI_BRIDGE_EMUL_HANDLED; 591 } 592 593 static void 594 mvebu_pci_bridge_emul_base_conf_write(struct pci_bridge_emul *bridge, 595 int reg, u32 old, u32 new, u32 mask) 596 { 597 struct mvebu_pcie_port *port = bridge->data; 598 struct pci_bridge_emul_conf *conf = &bridge->conf; 599 600 switch (reg) { 601 case PCI_COMMAND: 602 if (!mvebu_has_ioport(port)) { 603 conf->command = cpu_to_le16( 604 le16_to_cpu(conf->command) & ~PCI_COMMAND_IO); 605 new &= ~PCI_COMMAND_IO; 606 } 607 608 mvebu_writel(port, new, PCIE_CMD_OFF); 609 break; 610 611 case PCI_IO_BASE: 612 if ((mask & 0xffff) && mvebu_pcie_handle_iobase_change(port)) { 613 /* On error disable IO range */ 614 conf->iobase &= ~0xf0; 615 conf->iolimit &= ~0xf0; 616 conf->iobaseupper = cpu_to_le16(0x0000); 617 conf->iolimitupper = cpu_to_le16(0x0000); 618 if (mvebu_has_ioport(port)) 619 conf->iobase |= 0xf0; 620 } 621 break; 622 623 case PCI_MEMORY_BASE: 624 if (mvebu_pcie_handle_membase_change(port)) { 625 /* On error disable mem range */ 626 conf->membase = cpu_to_le16(le16_to_cpu(conf->membase) & ~0xfff0); 627 conf->memlimit = cpu_to_le16(le16_to_cpu(conf->memlimit) & ~0xfff0); 628 conf->membase = cpu_to_le16(le16_to_cpu(conf->membase) | 0xfff0); 629 } 630 break; 631 632 case PCI_IO_BASE_UPPER16: 633 if (mvebu_pcie_handle_iobase_change(port)) { 634 /* On error disable IO range */ 635 conf->iobase &= ~0xf0; 636 conf->iolimit &= ~0xf0; 637 conf->iobaseupper = cpu_to_le16(0x0000); 638 conf->iolimitupper = cpu_to_le16(0x0000); 639 if (mvebu_has_ioport(port)) 640 conf->iobase |= 0xf0; 641 } 642 break; 643 644 case PCI_PRIMARY_BUS: 645 if (mask & 0xff00) 646 mvebu_pcie_set_local_bus_nr(port, conf->secondary_bus); 647 break; 648 649 case PCI_INTERRUPT_LINE: 650 if (mask & (PCI_BRIDGE_CTL_BUS_RESET << 16)) { 651 u32 ctrl = mvebu_readl(port, PCIE_CTRL_OFF); 652 if (new & (PCI_BRIDGE_CTL_BUS_RESET << 16)) 653 ctrl |= PCIE_CTRL_MASTER_HOT_RESET; 654 else 655 ctrl &= ~PCIE_CTRL_MASTER_HOT_RESET; 656 mvebu_writel(port, ctrl, PCIE_CTRL_OFF); 657 } 658 break; 659 660 default: 661 break; 662 } 663 } 664 665 static void 666 mvebu_pci_bridge_emul_pcie_conf_write(struct pci_bridge_emul *bridge, 667 int reg, u32 old, u32 new, u32 mask) 668 { 669 struct mvebu_pcie_port *port = bridge->data; 670 671 switch (reg) { 672 case PCI_EXP_DEVCTL: 673 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL); 674 break; 675 676 case PCI_EXP_LNKCTL: 677 /* 678 * If we don't support CLKREQ, we must ensure that the 679 * CLKREQ enable bit always reads zero. Since we haven't 680 * had this capability, and it's dependent on board wiring, 681 * disable it for the time being. 682 */ 683 new &= ~PCI_EXP_LNKCTL_CLKREQ_EN; 684 685 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL); 686 break; 687 688 case PCI_EXP_RTSTA: 689 /* 690 * PME Status bit in Root Status Register (PCIE_RC_RTSTA) 691 * is read-only and can be cleared only by writing 0b to the 692 * Interrupt Cause RW0C register (PCIE_INT_CAUSE_OFF). So 693 * clear PME via Interrupt Cause. 694 */ 695 if (new & PCI_EXP_RTSTA_PME) 696 mvebu_writel(port, ~PCIE_INT_PM_PME, PCIE_INT_CAUSE_OFF); 697 break; 698 699 case PCI_EXP_DEVCTL2: 700 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_DEVCTL2); 701 break; 702 703 case PCI_EXP_LNKCTL2: 704 mvebu_writel(port, new, PCIE_CAP_PCIEXP + PCI_EXP_LNKCTL2); 705 break; 706 707 default: 708 break; 709 } 710 } 711 712 static struct pci_bridge_emul_ops mvebu_pci_bridge_emul_ops = { 713 .read_base = mvebu_pci_bridge_emul_base_conf_read, 714 .write_base = mvebu_pci_bridge_emul_base_conf_write, 715 .read_pcie = mvebu_pci_bridge_emul_pcie_conf_read, 716 .write_pcie = mvebu_pci_bridge_emul_pcie_conf_write, 717 }; 718 719 /* 720 * Initialize the configuration space of the PCI-to-PCI bridge 721 * associated with the given PCIe interface. 722 */ 723 static int mvebu_pci_bridge_emul_init(struct mvebu_pcie_port *port) 724 { 725 struct pci_bridge_emul *bridge = &port->bridge; 726 u32 pcie_cap = mvebu_readl(port, PCIE_CAP_PCIEXP); 727 u8 pcie_cap_ver = ((pcie_cap >> 16) & PCI_EXP_FLAGS_VERS); 728 729 bridge->conf.vendor = PCI_VENDOR_ID_MARVELL; 730 bridge->conf.device = mvebu_readl(port, PCIE_DEV_ID_OFF) >> 16; 731 bridge->conf.class_revision = 732 mvebu_readl(port, PCIE_DEV_REV_OFF) & 0xff; 733 734 if (mvebu_has_ioport(port)) { 735 /* We support 32 bits I/O addressing */ 736 bridge->conf.iobase = PCI_IO_RANGE_TYPE_32; 737 bridge->conf.iolimit = PCI_IO_RANGE_TYPE_32; 738 } 739 740 /* 741 * Older mvebu hardware provides PCIe Capability structure only in 742 * version 1. New hardware provides it in version 2. 743 */ 744 bridge->pcie_conf.cap = cpu_to_le16(pcie_cap_ver); 745 746 bridge->has_pcie = true; 747 bridge->data = port; 748 bridge->ops = &mvebu_pci_bridge_emul_ops; 749 750 return pci_bridge_emul_init(bridge, PCI_BRIDGE_EMUL_NO_PREFETCHABLE_BAR); 751 } 752 753 static inline struct mvebu_pcie *sys_to_pcie(struct pci_sys_data *sys) 754 { 755 return sys->private_data; 756 } 757 758 static struct mvebu_pcie_port *mvebu_pcie_find_port(struct mvebu_pcie *pcie, 759 struct pci_bus *bus, 760 int devfn) 761 { 762 int i; 763 764 for (i = 0; i < pcie->nports; i++) { 765 struct mvebu_pcie_port *port = &pcie->ports[i]; 766 767 if (!port->base) 768 continue; 769 770 if (bus->number == 0 && port->devfn == devfn) 771 return port; 772 if (bus->number != 0 && 773 bus->number >= port->bridge.conf.secondary_bus && 774 bus->number <= port->bridge.conf.subordinate_bus) 775 return port; 776 } 777 778 return NULL; 779 } 780 781 /* PCI configuration space write function */ 782 static int mvebu_pcie_wr_conf(struct pci_bus *bus, u32 devfn, 783 int where, int size, u32 val) 784 { 785 struct mvebu_pcie *pcie = bus->sysdata; 786 struct mvebu_pcie_port *port; 787 int ret; 788 789 port = mvebu_pcie_find_port(pcie, bus, devfn); 790 if (!port) 791 return PCIBIOS_DEVICE_NOT_FOUND; 792 793 /* Access the emulated PCI-to-PCI bridge */ 794 if (bus->number == 0) 795 return pci_bridge_emul_conf_write(&port->bridge, where, 796 size, val); 797 798 if (!mvebu_pcie_link_up(port)) 799 return PCIBIOS_DEVICE_NOT_FOUND; 800 801 /* Access the real PCIe interface */ 802 ret = mvebu_pcie_hw_wr_conf(port, bus, devfn, 803 where, size, val); 804 805 return ret; 806 } 807 808 /* PCI configuration space read function */ 809 static int mvebu_pcie_rd_conf(struct pci_bus *bus, u32 devfn, int where, 810 int size, u32 *val) 811 { 812 struct mvebu_pcie *pcie = bus->sysdata; 813 struct mvebu_pcie_port *port; 814 int ret; 815 816 port = mvebu_pcie_find_port(pcie, bus, devfn); 817 if (!port) 818 return PCIBIOS_DEVICE_NOT_FOUND; 819 820 /* Access the emulated PCI-to-PCI bridge */ 821 if (bus->number == 0) 822 return pci_bridge_emul_conf_read(&port->bridge, where, 823 size, val); 824 825 if (!mvebu_pcie_link_up(port)) 826 return PCIBIOS_DEVICE_NOT_FOUND; 827 828 /* Access the real PCIe interface */ 829 ret = mvebu_pcie_hw_rd_conf(port, bus, devfn, 830 where, size, val); 831 832 return ret; 833 } 834 835 static struct pci_ops mvebu_pcie_ops = { 836 .read = mvebu_pcie_rd_conf, 837 .write = mvebu_pcie_wr_conf, 838 }; 839 840 static int mvebu_pcie_map_irq(const struct pci_dev *dev, u8 slot, u8 pin) 841 { 842 /* Interrupt support on mvebu emulated bridges is not implemented yet */ 843 if (dev->bus->number == 0) 844 return 0; /* Proper return code 0 == NO_IRQ */ 845 846 return of_irq_parse_and_map_pci(dev, slot, pin); 847 } 848 849 static resource_size_t mvebu_pcie_align_resource(struct pci_dev *dev, 850 const struct resource *res, 851 resource_size_t start, 852 resource_size_t size, 853 resource_size_t align) 854 { 855 if (dev->bus->number != 0) 856 return start; 857 858 /* 859 * On the PCI-to-PCI bridge side, the I/O windows must have at 860 * least a 64 KB size and the memory windows must have at 861 * least a 1 MB size. Moreover, MBus windows need to have a 862 * base address aligned on their size, and their size must be 863 * a power of two. This means that if the BAR doesn't have a 864 * power of two size, several MBus windows will actually be 865 * created. We need to ensure that the biggest MBus window 866 * (which will be the first one) is aligned on its size, which 867 * explains the rounddown_pow_of_two() being done here. 868 */ 869 if (res->flags & IORESOURCE_IO) 870 return round_up(start, max_t(resource_size_t, SZ_64K, 871 rounddown_pow_of_two(size))); 872 else if (res->flags & IORESOURCE_MEM) 873 return round_up(start, max_t(resource_size_t, SZ_1M, 874 rounddown_pow_of_two(size))); 875 else 876 return start; 877 } 878 879 static void __iomem *mvebu_pcie_map_registers(struct platform_device *pdev, 880 struct device_node *np, 881 struct mvebu_pcie_port *port) 882 { 883 int ret = 0; 884 885 ret = of_address_to_resource(np, 0, &port->regs); 886 if (ret) 887 return (void __iomem *)ERR_PTR(ret); 888 889 return devm_ioremap_resource(&pdev->dev, &port->regs); 890 } 891 892 #define DT_FLAGS_TO_TYPE(flags) (((flags) >> 24) & 0x03) 893 #define DT_TYPE_IO 0x1 894 #define DT_TYPE_MEM32 0x2 895 #define DT_CPUADDR_TO_TARGET(cpuaddr) (((cpuaddr) >> 56) & 0xFF) 896 #define DT_CPUADDR_TO_ATTR(cpuaddr) (((cpuaddr) >> 48) & 0xFF) 897 898 static int mvebu_get_tgt_attr(struct device_node *np, int devfn, 899 unsigned long type, 900 unsigned int *tgt, 901 unsigned int *attr) 902 { 903 const int na = 3, ns = 2; 904 const __be32 *range; 905 int rlen, nranges, rangesz, pna, i; 906 907 *tgt = -1; 908 *attr = -1; 909 910 range = of_get_property(np, "ranges", &rlen); 911 if (!range) 912 return -EINVAL; 913 914 pna = of_n_addr_cells(np); 915 rangesz = pna + na + ns; 916 nranges = rlen / sizeof(__be32) / rangesz; 917 918 for (i = 0; i < nranges; i++, range += rangesz) { 919 u32 flags = of_read_number(range, 1); 920 u32 slot = of_read_number(range + 1, 1); 921 u64 cpuaddr = of_read_number(range + na, pna); 922 unsigned long rtype; 923 924 if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_IO) 925 rtype = IORESOURCE_IO; 926 else if (DT_FLAGS_TO_TYPE(flags) == DT_TYPE_MEM32) 927 rtype = IORESOURCE_MEM; 928 else 929 continue; 930 931 if (slot == PCI_SLOT(devfn) && type == rtype) { 932 *tgt = DT_CPUADDR_TO_TARGET(cpuaddr); 933 *attr = DT_CPUADDR_TO_ATTR(cpuaddr); 934 return 0; 935 } 936 } 937 938 return -ENOENT; 939 } 940 941 #ifdef CONFIG_PM_SLEEP 942 static int mvebu_pcie_suspend(struct device *dev) 943 { 944 struct mvebu_pcie *pcie; 945 int i; 946 947 pcie = dev_get_drvdata(dev); 948 for (i = 0; i < pcie->nports; i++) { 949 struct mvebu_pcie_port *port = pcie->ports + i; 950 if (!port->base) 951 continue; 952 port->saved_pcie_stat = mvebu_readl(port, PCIE_STAT_OFF); 953 } 954 955 return 0; 956 } 957 958 static int mvebu_pcie_resume(struct device *dev) 959 { 960 struct mvebu_pcie *pcie; 961 int i; 962 963 pcie = dev_get_drvdata(dev); 964 for (i = 0; i < pcie->nports; i++) { 965 struct mvebu_pcie_port *port = pcie->ports + i; 966 if (!port->base) 967 continue; 968 mvebu_writel(port, port->saved_pcie_stat, PCIE_STAT_OFF); 969 mvebu_pcie_setup_hw(port); 970 } 971 972 return 0; 973 } 974 #endif 975 976 static void mvebu_pcie_port_clk_put(void *data) 977 { 978 struct mvebu_pcie_port *port = data; 979 980 clk_put(port->clk); 981 } 982 983 static int mvebu_pcie_parse_port(struct mvebu_pcie *pcie, 984 struct mvebu_pcie_port *port, struct device_node *child) 985 { 986 struct device *dev = &pcie->pdev->dev; 987 enum of_gpio_flags flags; 988 int reset_gpio, ret; 989 990 port->pcie = pcie; 991 992 if (of_property_read_u32(child, "marvell,pcie-port", &port->port)) { 993 dev_warn(dev, "ignoring %pOF, missing pcie-port property\n", 994 child); 995 goto skip; 996 } 997 998 if (of_property_read_u32(child, "marvell,pcie-lane", &port->lane)) 999 port->lane = 0; 1000 1001 port->name = devm_kasprintf(dev, GFP_KERNEL, "pcie%d.%d", port->port, 1002 port->lane); 1003 if (!port->name) { 1004 ret = -ENOMEM; 1005 goto err; 1006 } 1007 1008 port->devfn = of_pci_get_devfn(child); 1009 if (port->devfn < 0) 1010 goto skip; 1011 if (PCI_FUNC(port->devfn) != 0) { 1012 dev_err(dev, "%s: invalid function number, must be zero\n", 1013 port->name); 1014 goto skip; 1015 } 1016 1017 ret = mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_MEM, 1018 &port->mem_target, &port->mem_attr); 1019 if (ret < 0) { 1020 dev_err(dev, "%s: cannot get tgt/attr for mem window\n", 1021 port->name); 1022 goto skip; 1023 } 1024 1025 if (resource_size(&pcie->io) != 0) { 1026 mvebu_get_tgt_attr(dev->of_node, port->devfn, IORESOURCE_IO, 1027 &port->io_target, &port->io_attr); 1028 } else { 1029 port->io_target = -1; 1030 port->io_attr = -1; 1031 } 1032 1033 reset_gpio = of_get_named_gpio_flags(child, "reset-gpios", 0, &flags); 1034 if (reset_gpio == -EPROBE_DEFER) { 1035 ret = reset_gpio; 1036 goto err; 1037 } 1038 1039 if (gpio_is_valid(reset_gpio)) { 1040 unsigned long gpio_flags; 1041 1042 port->reset_name = devm_kasprintf(dev, GFP_KERNEL, "%s-reset", 1043 port->name); 1044 if (!port->reset_name) { 1045 ret = -ENOMEM; 1046 goto err; 1047 } 1048 1049 if (flags & OF_GPIO_ACTIVE_LOW) { 1050 dev_info(dev, "%pOF: reset gpio is active low\n", 1051 child); 1052 gpio_flags = GPIOF_ACTIVE_LOW | 1053 GPIOF_OUT_INIT_LOW; 1054 } else { 1055 gpio_flags = GPIOF_OUT_INIT_HIGH; 1056 } 1057 1058 ret = devm_gpio_request_one(dev, reset_gpio, gpio_flags, 1059 port->reset_name); 1060 if (ret) { 1061 if (ret == -EPROBE_DEFER) 1062 goto err; 1063 goto skip; 1064 } 1065 1066 port->reset_gpio = gpio_to_desc(reset_gpio); 1067 } 1068 1069 port->clk = of_clk_get_by_name(child, NULL); 1070 if (IS_ERR(port->clk)) { 1071 dev_err(dev, "%s: cannot get clock\n", port->name); 1072 goto skip; 1073 } 1074 1075 ret = devm_add_action(dev, mvebu_pcie_port_clk_put, port); 1076 if (ret < 0) { 1077 clk_put(port->clk); 1078 goto err; 1079 } 1080 1081 return 1; 1082 1083 skip: 1084 ret = 0; 1085 1086 /* In the case of skipping, we need to free these */ 1087 devm_kfree(dev, port->reset_name); 1088 port->reset_name = NULL; 1089 devm_kfree(dev, port->name); 1090 port->name = NULL; 1091 1092 err: 1093 return ret; 1094 } 1095 1096 /* 1097 * Power up a PCIe port. PCIe requires the refclk to be stable for 100µs 1098 * prior to releasing PERST. See table 2-4 in section 2.6.2 AC Specifications 1099 * of the PCI Express Card Electromechanical Specification, 1.1. 1100 */ 1101 static int mvebu_pcie_powerup(struct mvebu_pcie_port *port) 1102 { 1103 int ret; 1104 1105 ret = clk_prepare_enable(port->clk); 1106 if (ret < 0) 1107 return ret; 1108 1109 if (port->reset_gpio) { 1110 u32 reset_udelay = PCI_PM_D3COLD_WAIT * 1000; 1111 1112 of_property_read_u32(port->dn, "reset-delay-us", 1113 &reset_udelay); 1114 1115 udelay(100); 1116 1117 gpiod_set_value_cansleep(port->reset_gpio, 0); 1118 msleep(reset_udelay / 1000); 1119 } 1120 1121 return 0; 1122 } 1123 1124 /* 1125 * Power down a PCIe port. Strictly, PCIe requires us to place the card 1126 * in D3hot state before asserting PERST#. 1127 */ 1128 static void mvebu_pcie_powerdown(struct mvebu_pcie_port *port) 1129 { 1130 gpiod_set_value_cansleep(port->reset_gpio, 1); 1131 1132 clk_disable_unprepare(port->clk); 1133 } 1134 1135 /* 1136 * devm_of_pci_get_host_bridge_resources() only sets up translateable resources, 1137 * so we need extra resource setup parsing our special DT properties encoding 1138 * the MEM and IO apertures. 1139 */ 1140 static int mvebu_pcie_parse_request_resources(struct mvebu_pcie *pcie) 1141 { 1142 struct device *dev = &pcie->pdev->dev; 1143 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 1144 int ret; 1145 1146 /* Get the PCIe memory aperture */ 1147 mvebu_mbus_get_pcie_mem_aperture(&pcie->mem); 1148 if (resource_size(&pcie->mem) == 0) { 1149 dev_err(dev, "invalid memory aperture size\n"); 1150 return -EINVAL; 1151 } 1152 1153 pcie->mem.name = "PCI MEM"; 1154 pci_add_resource(&bridge->windows, &pcie->mem); 1155 ret = devm_request_resource(dev, &iomem_resource, &pcie->mem); 1156 if (ret) 1157 return ret; 1158 1159 /* Get the PCIe IO aperture */ 1160 mvebu_mbus_get_pcie_io_aperture(&pcie->io); 1161 1162 if (resource_size(&pcie->io) != 0) { 1163 pcie->realio.flags = pcie->io.flags; 1164 pcie->realio.start = PCIBIOS_MIN_IO; 1165 pcie->realio.end = min_t(resource_size_t, 1166 IO_SPACE_LIMIT - SZ_64K, 1167 resource_size(&pcie->io) - 1); 1168 pcie->realio.name = "PCI I/O"; 1169 1170 ret = devm_pci_remap_iospace(dev, &pcie->realio, pcie->io.start); 1171 if (ret) 1172 return ret; 1173 1174 pci_add_resource(&bridge->windows, &pcie->realio); 1175 ret = devm_request_resource(dev, &ioport_resource, &pcie->realio); 1176 if (ret) 1177 return ret; 1178 } 1179 1180 return 0; 1181 } 1182 1183 static int mvebu_pcie_probe(struct platform_device *pdev) 1184 { 1185 struct device *dev = &pdev->dev; 1186 struct mvebu_pcie *pcie; 1187 struct pci_host_bridge *bridge; 1188 struct device_node *np = dev->of_node; 1189 struct device_node *child; 1190 int num, i, ret; 1191 1192 bridge = devm_pci_alloc_host_bridge(dev, sizeof(struct mvebu_pcie)); 1193 if (!bridge) 1194 return -ENOMEM; 1195 1196 pcie = pci_host_bridge_priv(bridge); 1197 pcie->pdev = pdev; 1198 platform_set_drvdata(pdev, pcie); 1199 1200 ret = mvebu_pcie_parse_request_resources(pcie); 1201 if (ret) 1202 return ret; 1203 1204 num = of_get_available_child_count(np); 1205 1206 pcie->ports = devm_kcalloc(dev, num, sizeof(*pcie->ports), GFP_KERNEL); 1207 if (!pcie->ports) 1208 return -ENOMEM; 1209 1210 i = 0; 1211 for_each_available_child_of_node(np, child) { 1212 struct mvebu_pcie_port *port = &pcie->ports[i]; 1213 1214 ret = mvebu_pcie_parse_port(pcie, port, child); 1215 if (ret < 0) { 1216 of_node_put(child); 1217 return ret; 1218 } else if (ret == 0) { 1219 continue; 1220 } 1221 1222 port->dn = child; 1223 i++; 1224 } 1225 pcie->nports = i; 1226 1227 for (i = 0; i < pcie->nports; i++) { 1228 struct mvebu_pcie_port *port = &pcie->ports[i]; 1229 1230 child = port->dn; 1231 if (!child) 1232 continue; 1233 1234 ret = mvebu_pcie_powerup(port); 1235 if (ret < 0) 1236 continue; 1237 1238 port->base = mvebu_pcie_map_registers(pdev, child, port); 1239 if (IS_ERR(port->base)) { 1240 dev_err(dev, "%s: cannot map registers\n", port->name); 1241 port->base = NULL; 1242 mvebu_pcie_powerdown(port); 1243 continue; 1244 } 1245 1246 ret = mvebu_pci_bridge_emul_init(port); 1247 if (ret < 0) { 1248 dev_err(dev, "%s: cannot init emulated bridge\n", 1249 port->name); 1250 devm_iounmap(dev, port->base); 1251 port->base = NULL; 1252 mvebu_pcie_powerdown(port); 1253 continue; 1254 } 1255 1256 /* 1257 * PCIe topology exported by mvebu hw is quite complicated. In 1258 * reality has something like N fully independent host bridges 1259 * where each host bridge has one PCIe Root Port (which acts as 1260 * PCI Bridge device). Each host bridge has its own independent 1261 * internal registers, independent access to PCI config space, 1262 * independent interrupt lines, independent window and memory 1263 * access configuration. But additionally there is some kind of 1264 * peer-to-peer support between PCIe devices behind different 1265 * host bridges limited just to forwarding of memory and I/O 1266 * transactions (forwarding of error messages and config cycles 1267 * is not supported). So we could say there are N independent 1268 * PCIe Root Complexes. 1269 * 1270 * For this kind of setup DT should have been structured into 1271 * N independent PCIe controllers / host bridges. But instead 1272 * structure in past was defined to put PCIe Root Ports of all 1273 * host bridges into one bus zero, like in classic multi-port 1274 * Root Complex setup with just one host bridge. 1275 * 1276 * This means that pci-mvebu.c driver provides "virtual" bus 0 1277 * on which registers all PCIe Root Ports (PCI Bridge devices) 1278 * specified in DT by their BDF addresses and virtually routes 1279 * PCI config access of each PCI bridge device to specific PCIe 1280 * host bridge. 1281 * 1282 * Normally PCI Bridge should choose between Type 0 and Type 1 1283 * config requests based on primary and secondary bus numbers 1284 * configured on the bridge itself. But because mvebu PCI Bridge 1285 * does not have registers for primary and secondary bus numbers 1286 * in its config space, it determinates type of config requests 1287 * via its own custom way. 1288 * 1289 * There are two options how mvebu determinate type of config 1290 * request. 1291 * 1292 * 1. If Secondary Bus Number Enable bit is not set or is not 1293 * available (applies for pre-XP PCIe controllers) then Type 0 1294 * is used if target bus number equals Local Bus Number (bits 1295 * [15:8] in register 0x1a04) and target device number differs 1296 * from Local Device Number (bits [20:16] in register 0x1a04). 1297 * Type 1 is used if target bus number differs from Local Bus 1298 * Number. And when target bus number equals Local Bus Number 1299 * and target device equals Local Device Number then request is 1300 * routed to Local PCI Bridge (PCIe Root Port). 1301 * 1302 * 2. If Secondary Bus Number Enable bit is set (bit 7 in 1303 * register 0x1a2c) then mvebu hw determinate type of config 1304 * request like compliant PCI Bridge based on primary bus number 1305 * which is configured via Local Bus Number (bits [15:8] in 1306 * register 0x1a04) and secondary bus number which is configured 1307 * via Secondary Bus Number (bits [7:0] in register 0x1a2c). 1308 * Local PCI Bridge (PCIe Root Port) is available on primary bus 1309 * as device with Local Device Number (bits [20:16] in register 1310 * 0x1a04). 1311 * 1312 * Secondary Bus Number Enable bit is disabled by default and 1313 * option 2. is not available on pre-XP PCIe controllers. Hence 1314 * this driver always use option 1. 1315 * 1316 * Basically it means that primary and secondary buses shares 1317 * one virtual number configured via Local Bus Number bits and 1318 * Local Device Number bits determinates if accessing primary 1319 * or secondary bus. Set Local Device Number to 1 and redirect 1320 * all writes of PCI Bridge Secondary Bus Number register to 1321 * Local Bus Number (bits [15:8] in register 0x1a04). 1322 * 1323 * So when accessing devices on buses behind secondary bus 1324 * number it would work correctly. And also when accessing 1325 * device 0 at secondary bus number via config space would be 1326 * correctly routed to secondary bus. Due to issues described 1327 * in mvebu_pcie_setup_hw(), PCI Bridges at primary bus (zero) 1328 * are not accessed directly via PCI config space but rarher 1329 * indirectly via kernel emulated PCI bridge driver. 1330 */ 1331 mvebu_pcie_setup_hw(port); 1332 mvebu_pcie_set_local_dev_nr(port, 0); 1333 } 1334 1335 pcie->nports = i; 1336 1337 bridge->sysdata = pcie; 1338 bridge->ops = &mvebu_pcie_ops; 1339 bridge->align_resource = mvebu_pcie_align_resource; 1340 bridge->map_irq = mvebu_pcie_map_irq; 1341 1342 return pci_host_probe(bridge); 1343 } 1344 1345 static int mvebu_pcie_remove(struct platform_device *pdev) 1346 { 1347 struct mvebu_pcie *pcie = platform_get_drvdata(pdev); 1348 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(pcie); 1349 u32 cmd; 1350 int i; 1351 1352 /* Remove PCI bus with all devices. */ 1353 pci_lock_rescan_remove(); 1354 pci_stop_root_bus(bridge->bus); 1355 pci_remove_root_bus(bridge->bus); 1356 pci_unlock_rescan_remove(); 1357 1358 for (i = 0; i < pcie->nports; i++) { 1359 struct mvebu_pcie_port *port = &pcie->ports[i]; 1360 1361 if (!port->base) 1362 continue; 1363 1364 /* Disable Root Bridge I/O space, memory space and bus mastering. */ 1365 cmd = mvebu_readl(port, PCIE_CMD_OFF); 1366 cmd &= ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER); 1367 mvebu_writel(port, cmd, PCIE_CMD_OFF); 1368 1369 /* Mask all interrupt sources. */ 1370 mvebu_writel(port, 0, PCIE_MASK_OFF); 1371 1372 /* Free config space for emulated root bridge. */ 1373 pci_bridge_emul_cleanup(&port->bridge); 1374 1375 /* Disable and clear BARs and windows. */ 1376 mvebu_pcie_disable_wins(port); 1377 1378 /* Delete PCIe IO and MEM windows. */ 1379 if (port->iowin.size) 1380 mvebu_pcie_del_windows(port, port->iowin.base, port->iowin.size); 1381 if (port->memwin.size) 1382 mvebu_pcie_del_windows(port, port->memwin.base, port->memwin.size); 1383 1384 /* Power down card and disable clocks. Must be the last step. */ 1385 mvebu_pcie_powerdown(port); 1386 } 1387 1388 return 0; 1389 } 1390 1391 static const struct of_device_id mvebu_pcie_of_match_table[] = { 1392 { .compatible = "marvell,armada-xp-pcie", }, 1393 { .compatible = "marvell,armada-370-pcie", }, 1394 { .compatible = "marvell,dove-pcie", }, 1395 { .compatible = "marvell,kirkwood-pcie", }, 1396 {}, 1397 }; 1398 1399 static const struct dev_pm_ops mvebu_pcie_pm_ops = { 1400 SET_NOIRQ_SYSTEM_SLEEP_PM_OPS(mvebu_pcie_suspend, mvebu_pcie_resume) 1401 }; 1402 1403 static struct platform_driver mvebu_pcie_driver = { 1404 .driver = { 1405 .name = "mvebu-pcie", 1406 .of_match_table = mvebu_pcie_of_match_table, 1407 .pm = &mvebu_pcie_pm_ops, 1408 }, 1409 .probe = mvebu_pcie_probe, 1410 .remove = mvebu_pcie_remove, 1411 }; 1412 module_platform_driver(mvebu_pcie_driver); 1413 1414 MODULE_AUTHOR("Thomas Petazzoni <thomas.petazzoni@bootlin.com>"); 1415 MODULE_AUTHOR("Pali Rohár <pali@kernel.org>"); 1416 MODULE_DESCRIPTION("Marvell EBU PCIe controller"); 1417 MODULE_LICENSE("GPL v2"); 1418