1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Intel IXP4xx PCI host controller
7 * Based on the IXP4xx arch/arm/mach-ixp4xx/common-pci.c driver
8 * Copyright (C) 2002 Intel Corporation
10 * Copyright (C) 2003-2004 MontaVista Software, Inc.
15 * - Test IO-space access
19 #include <linux/init.h>
21 #include <linux/kernel.h>
22 #include <linux/of_address.h>
23 #include <linux/of_device.h>
24 #include <linux/of_pci.h>
25 #include <linux/pci.h>
26 #include <linux/platform_device.h>
27 #include <linux/slab.h>
28 #include <linux/bits.h>
30 /* Register offsets */
31 #define IXP4XX_PCI_NP_AD 0x00
32 #define IXP4XX_PCI_NP_CBE 0x04
33 #define IXP4XX_PCI_NP_WDATA 0x08
34 #define IXP4XX_PCI_NP_RDATA 0x0c
35 #define IXP4XX_PCI_CRP_AD_CBE 0x10
36 #define IXP4XX_PCI_CRP_WDATA 0x14
37 #define IXP4XX_PCI_CRP_RDATA 0x18
38 #define IXP4XX_PCI_CSR 0x1c
39 #define IXP4XX_PCI_ISR 0x20
40 #define IXP4XX_PCI_INTEN 0x24
41 #define IXP4XX_PCI_DMACTRL 0x28
42 #define IXP4XX_PCI_AHBMEMBASE 0x2c
43 #define IXP4XX_PCI_AHBIOBASE 0x30
44 #define IXP4XX_PCI_PCIMEMBASE 0x34
45 #define IXP4XX_PCI_AHBDOORBELL 0x38
46 #define IXP4XX_PCI_PCIDOORBELL 0x3c
47 #define IXP4XX_PCI_ATPDMA0_AHBADDR 0x40
48 #define IXP4XX_PCI_ATPDMA0_PCIADDR 0x44
49 #define IXP4XX_PCI_ATPDMA0_LENADDR 0x48
50 #define IXP4XX_PCI_ATPDMA1_AHBADDR 0x4c
51 #define IXP4XX_PCI_ATPDMA1_PCIADDR 0x50
52 #define IXP4XX_PCI_ATPDMA1_LENADDR 0x54
54 /* CSR bit definitions */
55 #define IXP4XX_PCI_CSR_HOST BIT(0)
56 #define IXP4XX_PCI_CSR_ARBEN BIT(1)
57 #define IXP4XX_PCI_CSR_ADS BIT(2)
58 #define IXP4XX_PCI_CSR_PDS BIT(3)
59 #define IXP4XX_PCI_CSR_ABE BIT(4)
60 #define IXP4XX_PCI_CSR_DBT BIT(5)
61 #define IXP4XX_PCI_CSR_ASE BIT(8)
62 #define IXP4XX_PCI_CSR_IC BIT(15)
63 #define IXP4XX_PCI_CSR_PRST BIT(16)
65 /* ISR (Interrupt status) Register bit definitions */
66 #define IXP4XX_PCI_ISR_PSE BIT(0)
67 #define IXP4XX_PCI_ISR_PFE BIT(1)
68 #define IXP4XX_PCI_ISR_PPE BIT(2)
69 #define IXP4XX_PCI_ISR_AHBE BIT(3)
70 #define IXP4XX_PCI_ISR_APDC BIT(4)
71 #define IXP4XX_PCI_ISR_PADC BIT(5)
72 #define IXP4XX_PCI_ISR_ADB BIT(6)
73 #define IXP4XX_PCI_ISR_PDB BIT(7)
75 /* INTEN (Interrupt Enable) Register bit definitions */
76 #define IXP4XX_PCI_INTEN_PSE BIT(0)
77 #define IXP4XX_PCI_INTEN_PFE BIT(1)
78 #define IXP4XX_PCI_INTEN_PPE BIT(2)
79 #define IXP4XX_PCI_INTEN_AHBE BIT(3)
80 #define IXP4XX_PCI_INTEN_APDC BIT(4)
81 #define IXP4XX_PCI_INTEN_PADC BIT(5)
82 #define IXP4XX_PCI_INTEN_ADB BIT(6)
83 #define IXP4XX_PCI_INTEN_PDB BIT(7)
85 /* Shift value for byte enable on NP cmd/byte enable register */
86 #define IXP4XX_PCI_NP_CBE_BESL 4
88 /* PCI commands supported by NP access unit */
89 #define NP_CMD_IOREAD 0x2
90 #define NP_CMD_IOWRITE 0x3
91 #define NP_CMD_CONFIGREAD 0xa
92 #define NP_CMD_CONFIGWRITE 0xb
93 #define NP_CMD_MEMREAD 0x6
94 #define NP_CMD_MEMWRITE 0x7
96 /* Constants for CRP access into local config space */
97 #define CRP_AD_CBE_BESL 20
98 #define CRP_AD_CBE_WRITE 0x00010000
100 /* Special PCI configuration space registers for this controller */
101 #define IXP4XX_PCI_RTOTTO 0x40
111 * The IXP4xx has a peculiar address bus that will change the
112 * byte order on SoC peripherals depending on whether the device
113 * operates in big-endian or little-endian mode. That means that
114 * readl() and writel() that always use little-endian access
115 * will not work for SoC peripherals such as the PCI controller
116 * when used in big-endian mode. The accesses to the individual
117 * PCI devices on the other hand, are always little-endian and
118 * can use readl() and writel().
120 * For local AHB bus access we need to use __raw_[readl|writel]()
121 * to make sure that we access the SoC devices in the CPU native
124 static inline u32 ixp4xx_readl(struct ixp4xx_pci *p, u32 reg)
126 return __raw_readl(p->base + reg);
129 static inline void ixp4xx_writel(struct ixp4xx_pci *p, u32 reg, u32 val)
131 __raw_writel(val, p->base + reg);
134 static int ixp4xx_pci_check_master_abort(struct ixp4xx_pci *p)
136 u32 isr = ixp4xx_readl(p, IXP4XX_PCI_ISR);
138 if (isr & IXP4XX_PCI_ISR_PFE) {
139 /* Make sure the master abort bit is reset */
140 ixp4xx_writel(p, IXP4XX_PCI_ISR, IXP4XX_PCI_ISR_PFE);
141 dev_dbg(p->dev, "master abort detected\n");
148 static int ixp4xx_pci_read_indirect(struct ixp4xx_pci *p, u32 addr, u32 cmd, u32 *data)
150 ixp4xx_writel(p, IXP4XX_PCI_NP_AD, addr);
152 if (p->errata_hammer) {
156 * PCI workaround - only works if NP PCI space reads have
157 * no side effects. Hammer the register and read twice 8
158 * times. last one will be good.
160 for (i = 0; i < 8; i++) {
161 ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
162 *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
163 *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
166 ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
167 *data = ixp4xx_readl(p, IXP4XX_PCI_NP_RDATA);
170 return ixp4xx_pci_check_master_abort(p);
173 static int ixp4xx_pci_write_indirect(struct ixp4xx_pci *p, u32 addr, u32 cmd, u32 data)
175 ixp4xx_writel(p, IXP4XX_PCI_NP_AD, addr);
177 /* Set up the write */
178 ixp4xx_writel(p, IXP4XX_PCI_NP_CBE, cmd);
180 /* Execute the write by writing to NP_WDATA */
181 ixp4xx_writel(p, IXP4XX_PCI_NP_WDATA, data);
183 return ixp4xx_pci_check_master_abort(p);
186 static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)
188 /* Root bus is always 0 in this hardware */
191 return BIT(32-PCI_SLOT(devfn)) | ((PCI_FUNC(devfn)) << 8) |
195 return (bus_num << 16) | ((PCI_SLOT(devfn)) << 11) |
196 ((PCI_FUNC(devfn)) << 8) | (where & ~3) | 1;
201 * CRP functions are "Controller Configuration Port" accesses
202 * initiated from within this driver itself to read/write PCI
203 * control information in the config space.
205 static u32 ixp4xx_crp_byte_lane_enable_bits(u32 n, int size)
208 return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;
210 return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;
216 static int ixp4xx_crp_read_config(struct ixp4xx_pci *p, int where, int size,
224 dev_dbg(p->dev, "%s from %d size %d cmd %08x\n",
225 __func__, where, size, cmd);
227 ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
228 val = ixp4xx_readl(p, IXP4XX_PCI_CRP_RDATA);
234 dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
238 dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
242 dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
245 /* Should not happen */
246 dev_err(p->dev, "%s illegal size\n", __func__);
247 return PCIBIOS_DEVICE_NOT_FOUND;
251 return PCIBIOS_SUCCESSFUL;
254 static int ixp4xx_crp_write_config(struct ixp4xx_pci *p, int where, int size,
260 cmd = ixp4xx_crp_byte_lane_enable_bits(n, size);
261 if (cmd == 0xffffffff)
262 return PCIBIOS_BAD_REGISTER_NUMBER;
264 cmd |= CRP_AD_CBE_WRITE;
266 val = value << (8*n);
268 dev_dbg(p->dev, "%s to %d size %d cmd %08x val %08x\n",
269 __func__, where, size, cmd, val);
271 ixp4xx_writel(p, IXP4XX_PCI_CRP_AD_CBE, cmd);
272 ixp4xx_writel(p, IXP4XX_PCI_CRP_WDATA, val);
274 return PCIBIOS_SUCCESSFUL;
278 * Then follows the functions that read and write from the common PCI
279 * configuration space.
281 static u32 ixp4xx_byte_lane_enable_bits(u32 n, int size)
284 return (0xf & ~BIT(n)) << 4;
286 return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
292 static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn,
293 int where, int size, u32 *value)
295 struct ixp4xx_pci *p = bus->sysdata;
296 u32 n, addr, val, cmd;
297 u8 bus_num = bus->number;
302 cmd = ixp4xx_byte_lane_enable_bits(n, size);
303 if (cmd == 0xffffffff)
304 return PCIBIOS_BAD_REGISTER_NUMBER;
306 addr = ixp4xx_config_addr(bus_num, devfn, where);
307 cmd |= NP_CMD_CONFIGREAD;
308 dev_dbg(p->dev, "read_config from %d size %d dev %d:%d:%d address: %08x cmd: %08x\n",
309 where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
311 ret = ixp4xx_pci_read_indirect(p, addr, cmd, &val);
313 return PCIBIOS_DEVICE_NOT_FOUND;
319 dev_dbg(p->dev, "%s read byte %02x\n", __func__, val);
323 dev_dbg(p->dev, "%s read word %04x\n", __func__, val);
327 dev_dbg(p->dev, "%s read long %08x\n", __func__, val);
330 /* Should not happen */
331 dev_err(p->dev, "%s illegal size\n", __func__);
332 return PCIBIOS_DEVICE_NOT_FOUND;
336 return PCIBIOS_SUCCESSFUL;
339 static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn,
340 int where, int size, u32 value)
342 struct ixp4xx_pci *p = bus->sysdata;
343 u32 n, addr, val, cmd;
344 u8 bus_num = bus->number;
348 cmd = ixp4xx_byte_lane_enable_bits(n, size);
349 if (cmd == 0xffffffff)
350 return PCIBIOS_BAD_REGISTER_NUMBER;
352 addr = ixp4xx_config_addr(bus_num, devfn, where);
353 cmd |= NP_CMD_CONFIGWRITE;
354 val = value << (8*n);
356 dev_dbg(p->dev, "write_config_byte %#x to %d size %d dev %d:%d:%d addr: %08x cmd %08x\n",
357 value, where, size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn), addr, cmd);
359 ret = ixp4xx_pci_write_indirect(p, addr, cmd, val);
361 return PCIBIOS_DEVICE_NOT_FOUND;
363 return PCIBIOS_SUCCESSFUL;
366 static struct pci_ops ixp4xx_pci_ops = {
367 .read = ixp4xx_pci_read_config,
368 .write = ixp4xx_pci_write_config,
371 static u32 ixp4xx_pci_addr_to_64mconf(phys_addr_t addr)
375 base = ((addr & 0xff000000) >> 24);
376 return (base << 24) | ((base + 1) << 16)
377 | ((base + 2) << 8) | (base + 3);
380 static int ixp4xx_pci_parse_map_ranges(struct ixp4xx_pci *p)
382 struct device *dev = p->dev;
383 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
384 struct resource_entry *win;
385 struct resource *res;
388 win = resource_list_first_type(&bridge->windows, IORESOURCE_MEM);
393 addr = res->start - win->offset;
395 if (res->flags & IORESOURCE_PREFETCH)
396 res->name = "IXP4xx PCI PRE-MEM";
398 res->name = "IXP4xx PCI NON-PRE-MEM";
400 dev_dbg(dev, "%s window %pR, bus addr %pa\n",
401 res->name, res, &addr);
402 if (resource_size(res) != SZ_64M) {
403 dev_err(dev, "memory range is not 64MB\n");
407 pcimembase = ixp4xx_pci_addr_to_64mconf(addr);
408 /* Commit configuration */
409 ixp4xx_writel(p, IXP4XX_PCI_PCIMEMBASE, pcimembase);
411 dev_err(dev, "no AHB memory mapping defined\n");
414 win = resource_list_first_type(&bridge->windows, IORESOURCE_IO);
418 addr = pci_pio_to_address(res->start);
420 dev_err(dev, "IO mem at uneven address: %pa\n", &addr);
424 res->name = "IXP4xx PCI IO MEM";
426 * Setup I/O space location for PCI->AHB access, the
427 * upper 24 bits of the address goes into the lower
428 * 24 bits of this register.
430 ixp4xx_writel(p, IXP4XX_PCI_AHBIOBASE, (addr >> 8));
432 dev_info(dev, "no IO space AHB memory mapping defined\n");
438 static int ixp4xx_pci_parse_map_dma_ranges(struct ixp4xx_pci *p)
440 struct device *dev = p->dev;
441 struct pci_host_bridge *bridge = pci_host_bridge_from_priv(p);
442 struct resource_entry *win;
443 struct resource *res;
447 win = resource_list_first_type(&bridge->dma_ranges, IORESOURCE_MEM);
450 addr = res->start - win->offset;
452 if (resource_size(res) != SZ_64M) {
453 dev_err(dev, "DMA memory range is not 64MB\n");
457 dev_dbg(dev, "DMA MEM BASE: %pa\n", &addr);
459 * 4 PCI-to-AHB windows of 16 MB each, write the 8 high bits
460 * into each byte of the PCI_AHBMEMBASE register.
462 ahbmembase = ixp4xx_pci_addr_to_64mconf(addr);
463 /* Commit AHB membase */
464 ixp4xx_writel(p, IXP4XX_PCI_AHBMEMBASE, ahbmembase);
466 dev_err(dev, "no DMA memory range defined\n");
472 /* Only used to get context for abort handling */
473 static struct ixp4xx_pci *ixp4xx_pci_abort_singleton;
475 static int ixp4xx_pci_abort_handler(unsigned long addr, unsigned int fsr,
476 struct pt_regs *regs)
478 struct ixp4xx_pci *p = ixp4xx_pci_abort_singleton;
482 isr = ixp4xx_readl(p, IXP4XX_PCI_ISR);
483 ret = ixp4xx_crp_read_config(p, PCI_STATUS, 2, &status);
485 dev_err(p->dev, "unable to read abort status\n");
490 "PCI: abort_handler addr = %#lx, isr = %#x, status = %#x\n",
493 /* Make sure the Master Abort bit is reset */
494 ixp4xx_writel(p, IXP4XX_PCI_ISR, IXP4XX_PCI_ISR_PFE);
495 status |= PCI_STATUS_REC_MASTER_ABORT;
496 ret = ixp4xx_crp_write_config(p, PCI_STATUS, 2, status);
498 dev_err(p->dev, "unable to clear abort status bit\n");
501 * If it was an imprecise abort, then we need to correct the
502 * return address to be _after_ the instruction.
504 if (fsr & (1 << 10)) {
505 dev_err(p->dev, "imprecise abort\n");
512 static int __init ixp4xx_pci_probe(struct platform_device *pdev)
514 struct device *dev = &pdev->dev;
515 struct device_node *np = dev->of_node;
516 struct ixp4xx_pci *p;
517 struct pci_host_bridge *host;
529 host = devm_pci_alloc_host_bridge(dev, sizeof(*p));
533 host->ops = &ixp4xx_pci_ops;
534 p = pci_host_bridge_priv(host);
537 dev_set_drvdata(dev, p);
540 * Set up quirk for erratic behaviour in the 42x variant
541 * when accessing config space.
543 if (of_device_is_compatible(np, "intel,ixp42x-pci")) {
544 p->errata_hammer = true;
545 dev_info(dev, "activate hammering errata\n");
548 p->base = devm_platform_ioremap_resource(pdev, 0);
550 return PTR_ERR(p->base);
552 val = ixp4xx_readl(p, IXP4XX_PCI_CSR);
553 p->host_mode = !!(val & IXP4XX_PCI_CSR_HOST);
554 dev_info(dev, "controller is in %s mode\n",
555 p->host_mode ? "host" : "option");
557 /* Hook in our fault handler for PCI errors */
558 ixp4xx_pci_abort_singleton = p;
559 hook_fault_code(16+6, ixp4xx_pci_abort_handler, SIGBUS, 0,
560 "imprecise external abort");
562 ret = ixp4xx_pci_parse_map_ranges(p);
566 ret = ixp4xx_pci_parse_map_dma_ranges(p);
570 /* This is only configured in host mode */
572 addr = __pa(PAGE_OFFSET);
573 /* This is a noop (0x00) but explains what is going on */
574 addr |= PCI_BASE_ADDRESS_SPACE_MEMORY;
576 for (i = 0; i < 4; i++) {
577 /* Write this directly into the config space */
578 ret = ixp4xx_crp_write_config(p, basereg[i], 4, addr);
580 dev_err(dev, "failed to set up PCI_BASE_ADDRESS_%d\n", i);
582 dev_info(dev, "set PCI_BASE_ADDR_%d to %pa\n", i, &addr);
587 * Enable CSR window at 64 MiB to allow PCI masters to continue
588 * prefetching past the 64 MiB boundary, if all AHB to PCI
589 * windows are consecutive.
591 ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_4, 4, addr);
593 dev_err(dev, "failed to set up PCI_BASE_ADDRESS_4\n");
595 dev_info(dev, "set PCI_BASE_ADDR_4 to %pa\n", &addr);
598 * Put the IO memory window at the very end of physical memory
599 * at 0xfffffc00. This is when the system is trying to access IO
603 addr |= PCI_BASE_ADDRESS_SPACE_IO;
604 ret = ixp4xx_crp_write_config(p, PCI_BASE_ADDRESS_5, 4, addr);
606 dev_err(dev, "failed to set up PCI_BASE_ADDRESS_5\n");
608 dev_info(dev, "set PCI_BASE_ADDR_5 to %pa\n", &addr);
611 * Retry timeout to 0x80
612 * Transfer ready timeout to 0xff
614 ret = ixp4xx_crp_write_config(p, IXP4XX_PCI_RTOTTO, 4,
617 dev_err(dev, "failed to set up TRDY limit\n");
619 dev_info(dev, "set TRDY limit to 0x80ff\n");
622 /* Clear interrupts */
623 val = IXP4XX_PCI_ISR_PSE | IXP4XX_PCI_ISR_PFE | IXP4XX_PCI_ISR_PPE | IXP4XX_PCI_ISR_AHBE;
624 ixp4xx_writel(p, IXP4XX_PCI_ISR, val);
627 * Set Initialize Complete in PCI Control Register: allow IXP4XX to
628 * generate PCI configuration cycles. Specify that the AHB bus is
629 * operating in big-endian mode. Set up byte lane swapping between
630 * little-endian PCI and the big-endian AHB bus.
632 val = IXP4XX_PCI_CSR_IC | IXP4XX_PCI_CSR_ABE;
633 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN))
634 val |= (IXP4XX_PCI_CSR_PDS | IXP4XX_PCI_CSR_ADS);
635 ixp4xx_writel(p, IXP4XX_PCI_CSR, val);
637 ret = ixp4xx_crp_write_config(p, PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
639 dev_err(dev, "unable to initialize master and command memory\n");
641 dev_info(dev, "initialized as master\n");
643 pci_host_probe(host);
648 static const struct of_device_id ixp4xx_pci_of_match[] = {
650 .compatible = "intel,ixp42x-pci",
653 .compatible = "intel,ixp43x-pci",
659 * This driver needs to be a builtin module with suppressed bind
660 * attributes since the probe() is initializing a hard exception
661 * handler and this can only be done from __init-tagged code
662 * sections. This module cannot be removed and inserted at all.
664 static struct platform_driver ixp4xx_pci_driver = {
666 .name = "ixp4xx-pci",
667 .suppress_bind_attrs = true,
668 .of_match_table = ixp4xx_pci_of_match,
671 builtin_platform_driver_probe(ixp4xx_pci_driver, ixp4xx_pci_probe);