1 // SPDX-License-Identifier: GPL-2.0
3 * Support for Intel IXP4xx PCI host controller
5 * Copyright (C) 2017 Linus Walleij <linus.walleij@linaro.org>
7 * Based on the IXP4xx arch/arm/mach-ixp4xx/common-pci.c driver
8 * Copyright (C) 2002 Intel Corporation
9 * Copyright (C) 2003 Greg Ungerer <gerg@linux-m68k.org>
10 * Copyright (C) 2003-2004 MontaVista Software, Inc.
11 * Copyright (C) 2005 Deepak Saxena <dsaxena@plexity.net>
12 * Copyright (C) 2005 Alessandro Zummo <a.zummo@towertech.it>
15 * - Test IO-space access
19 #include <linux/init.h>
21 #include <linux/kernel.h>
23 #include <linux/of_pci.h>
24 #include <linux/pci.h>
25 #include <linux/platform_device.h>
26 #include <linux/slab.h>
27 #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 (PCI_CONF1_ADDRESS(0, 0, PCI_FUNC(devfn
), where
) &
192 ~PCI_CONF1_ENABLE
) | BIT(32-PCI_SLOT(devfn
));
195 return (PCI_CONF1_ADDRESS(bus_num
, PCI_SLOT(devfn
),
196 PCI_FUNC(devfn
), where
) &
197 ~PCI_CONF1_ENABLE
) | 1;
202 * CRP functions are "Controller Configuration Port" accesses
203 * initiated from within this driver itself to read/write PCI
204 * control information in the config space.
206 static u32
ixp4xx_crp_byte_lane_enable_bits(u32 n
, int size
)
209 return (0xf & ~BIT(n
)) << CRP_AD_CBE_BESL
;
211 return (0xf & ~(BIT(n
) | BIT(n
+1))) << CRP_AD_CBE_BESL
;
217 static int ixp4xx_crp_read_config(struct ixp4xx_pci
*p
, int where
, int size
,
225 dev_dbg(p
->dev
, "%s from %d size %d cmd %08x\n",
226 __func__
, where
, size
, cmd
);
228 ixp4xx_writel(p
, IXP4XX_PCI_CRP_AD_CBE
, cmd
);
229 val
= ixp4xx_readl(p
, IXP4XX_PCI_CRP_RDATA
);
235 dev_dbg(p
->dev
, "%s read byte %02x\n", __func__
, val
);
239 dev_dbg(p
->dev
, "%s read word %04x\n", __func__
, val
);
243 dev_dbg(p
->dev
, "%s read long %08x\n", __func__
, val
);
246 /* Should not happen */
247 dev_err(p
->dev
, "%s illegal size\n", __func__
);
248 return PCIBIOS_DEVICE_NOT_FOUND
;
252 return PCIBIOS_SUCCESSFUL
;
255 static int ixp4xx_crp_write_config(struct ixp4xx_pci
*p
, int where
, int size
,
261 cmd
= ixp4xx_crp_byte_lane_enable_bits(n
, size
);
262 if (cmd
== 0xffffffff)
263 return PCIBIOS_BAD_REGISTER_NUMBER
;
265 cmd
|= CRP_AD_CBE_WRITE
;
267 val
= value
<< (8*n
);
269 dev_dbg(p
->dev
, "%s to %d size %d cmd %08x val %08x\n",
270 __func__
, where
, size
, cmd
, val
);
272 ixp4xx_writel(p
, IXP4XX_PCI_CRP_AD_CBE
, cmd
);
273 ixp4xx_writel(p
, IXP4XX_PCI_CRP_WDATA
, val
);
275 return PCIBIOS_SUCCESSFUL
;
279 * Then follows the functions that read and write from the common PCI
280 * configuration space.
282 static u32
ixp4xx_byte_lane_enable_bits(u32 n
, int size
)
285 return (0xf & ~BIT(n
)) << 4;
287 return (0xf & ~(BIT(n
) | BIT(n
+1))) << 4;
293 static int ixp4xx_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
,
294 int where
, int size
, u32
*value
)
296 struct ixp4xx_pci
*p
= bus
->sysdata
;
297 u32 n
, addr
, val
, cmd
;
298 u8 bus_num
= bus
->number
;
303 cmd
= ixp4xx_byte_lane_enable_bits(n
, size
);
304 if (cmd
== 0xffffffff)
305 return PCIBIOS_BAD_REGISTER_NUMBER
;
307 addr
= ixp4xx_config_addr(bus_num
, devfn
, where
);
308 cmd
|= NP_CMD_CONFIGREAD
;
309 dev_dbg(p
->dev
, "read_config from %d size %d dev %d:%d:%d address: %08x cmd: %08x\n",
310 where
, size
, bus_num
, PCI_SLOT(devfn
), PCI_FUNC(devfn
), addr
, cmd
);
312 ret
= ixp4xx_pci_read_indirect(p
, addr
, cmd
, &val
);
314 return PCIBIOS_DEVICE_NOT_FOUND
;
320 dev_dbg(p
->dev
, "%s read byte %02x\n", __func__
, val
);
324 dev_dbg(p
->dev
, "%s read word %04x\n", __func__
, val
);
328 dev_dbg(p
->dev
, "%s read long %08x\n", __func__
, val
);
331 /* Should not happen */
332 dev_err(p
->dev
, "%s illegal size\n", __func__
);
333 return PCIBIOS_DEVICE_NOT_FOUND
;
337 return PCIBIOS_SUCCESSFUL
;
340 static int ixp4xx_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
,
341 int where
, int size
, u32 value
)
343 struct ixp4xx_pci
*p
= bus
->sysdata
;
344 u32 n
, addr
, val
, cmd
;
345 u8 bus_num
= bus
->number
;
349 cmd
= ixp4xx_byte_lane_enable_bits(n
, size
);
350 if (cmd
== 0xffffffff)
351 return PCIBIOS_BAD_REGISTER_NUMBER
;
353 addr
= ixp4xx_config_addr(bus_num
, devfn
, where
);
354 cmd
|= NP_CMD_CONFIGWRITE
;
355 val
= value
<< (8*n
);
357 dev_dbg(p
->dev
, "write_config_byte %#x to %d size %d dev %d:%d:%d addr: %08x cmd %08x\n",
358 value
, where
, size
, bus_num
, PCI_SLOT(devfn
), PCI_FUNC(devfn
), addr
, cmd
);
360 ret
= ixp4xx_pci_write_indirect(p
, addr
, cmd
, val
);
362 return PCIBIOS_DEVICE_NOT_FOUND
;
364 return PCIBIOS_SUCCESSFUL
;
367 static struct pci_ops ixp4xx_pci_ops
= {
368 .read
= ixp4xx_pci_read_config
,
369 .write
= ixp4xx_pci_write_config
,
372 static u32
ixp4xx_pci_addr_to_64mconf(phys_addr_t addr
)
376 base
= ((addr
& 0xff000000) >> 24);
377 return (base
<< 24) | ((base
+ 1) << 16)
378 | ((base
+ 2) << 8) | (base
+ 3);
381 static int ixp4xx_pci_parse_map_ranges(struct ixp4xx_pci
*p
)
383 struct device
*dev
= p
->dev
;
384 struct pci_host_bridge
*bridge
= pci_host_bridge_from_priv(p
);
385 struct resource_entry
*win
;
386 struct resource
*res
;
389 win
= resource_list_first_type(&bridge
->windows
, IORESOURCE_MEM
);
394 addr
= res
->start
- win
->offset
;
396 if (res
->flags
& IORESOURCE_PREFETCH
)
397 res
->name
= "IXP4xx PCI PRE-MEM";
399 res
->name
= "IXP4xx PCI NON-PRE-MEM";
401 dev_dbg(dev
, "%s window %pR, bus addr %pa\n",
402 res
->name
, res
, &addr
);
403 if (resource_size(res
) != SZ_64M
) {
404 dev_err(dev
, "memory range is not 64MB\n");
408 pcimembase
= ixp4xx_pci_addr_to_64mconf(addr
);
409 /* Commit configuration */
410 ixp4xx_writel(p
, IXP4XX_PCI_PCIMEMBASE
, pcimembase
);
412 dev_err(dev
, "no AHB memory mapping defined\n");
415 win
= resource_list_first_type(&bridge
->windows
, IORESOURCE_IO
);
419 addr
= pci_pio_to_address(res
->start
);
421 dev_err(dev
, "IO mem at uneven address: %pa\n", &addr
);
425 res
->name
= "IXP4xx PCI IO MEM";
427 * Setup I/O space location for PCI->AHB access, the
428 * upper 24 bits of the address goes into the lower
429 * 24 bits of this register.
431 ixp4xx_writel(p
, IXP4XX_PCI_AHBIOBASE
, (addr
>> 8));
433 dev_info(dev
, "no IO space AHB memory mapping defined\n");
439 static int ixp4xx_pci_parse_map_dma_ranges(struct ixp4xx_pci
*p
)
441 struct device
*dev
= p
->dev
;
442 struct pci_host_bridge
*bridge
= pci_host_bridge_from_priv(p
);
443 struct resource_entry
*win
;
444 struct resource
*res
;
448 win
= resource_list_first_type(&bridge
->dma_ranges
, IORESOURCE_MEM
);
451 addr
= res
->start
- win
->offset
;
453 if (resource_size(res
) != SZ_64M
) {
454 dev_err(dev
, "DMA memory range is not 64MB\n");
458 dev_dbg(dev
, "DMA MEM BASE: %pa\n", &addr
);
460 * 4 PCI-to-AHB windows of 16 MB each, write the 8 high bits
461 * into each byte of the PCI_AHBMEMBASE register.
463 ahbmembase
= ixp4xx_pci_addr_to_64mconf(addr
);
464 /* Commit AHB membase */
465 ixp4xx_writel(p
, IXP4XX_PCI_AHBMEMBASE
, ahbmembase
);
467 dev_err(dev
, "no DMA memory range defined\n");
473 /* Only used to get context for abort handling */
474 static struct ixp4xx_pci
*ixp4xx_pci_abort_singleton
;
476 static int ixp4xx_pci_abort_handler(unsigned long addr
, unsigned int fsr
,
477 struct pt_regs
*regs
)
479 struct ixp4xx_pci
*p
= ixp4xx_pci_abort_singleton
;
483 isr
= ixp4xx_readl(p
, IXP4XX_PCI_ISR
);
484 ret
= ixp4xx_crp_read_config(p
, PCI_STATUS
, 2, &status
);
486 dev_err(p
->dev
, "unable to read abort status\n");
491 "PCI: abort_handler addr = %#lx, isr = %#x, status = %#x\n",
494 /* Make sure the Master Abort bit is reset */
495 ixp4xx_writel(p
, IXP4XX_PCI_ISR
, IXP4XX_PCI_ISR_PFE
);
496 status
|= PCI_STATUS_REC_MASTER_ABORT
;
497 ret
= ixp4xx_crp_write_config(p
, PCI_STATUS
, 2, status
);
499 dev_err(p
->dev
, "unable to clear abort status bit\n");
502 * If it was an imprecise abort, then we need to correct the
503 * return address to be _after_ the instruction.
505 if (fsr
& (1 << 10)) {
506 dev_err(p
->dev
, "imprecise abort\n");
513 static int __init
ixp4xx_pci_probe(struct platform_device
*pdev
)
515 struct device
*dev
= &pdev
->dev
;
516 struct device_node
*np
= dev
->of_node
;
517 struct ixp4xx_pci
*p
;
518 struct pci_host_bridge
*host
;
530 host
= devm_pci_alloc_host_bridge(dev
, sizeof(*p
));
534 host
->ops
= &ixp4xx_pci_ops
;
535 p
= pci_host_bridge_priv(host
);
538 dev_set_drvdata(dev
, p
);
541 * Set up quirk for erratic behaviour in the 42x variant
542 * when accessing config space.
544 if (of_device_is_compatible(np
, "intel,ixp42x-pci")) {
545 p
->errata_hammer
= true;
546 dev_info(dev
, "activate hammering errata\n");
549 p
->base
= devm_platform_ioremap_resource(pdev
, 0);
551 return PTR_ERR(p
->base
);
553 val
= ixp4xx_readl(p
, IXP4XX_PCI_CSR
);
554 p
->host_mode
= !!(val
& IXP4XX_PCI_CSR_HOST
);
555 dev_info(dev
, "controller is in %s mode\n",
556 p
->host_mode
? "host" : "option");
558 /* Hook in our fault handler for PCI errors */
559 ixp4xx_pci_abort_singleton
= p
;
560 hook_fault_code(16+6, ixp4xx_pci_abort_handler
, SIGBUS
, 0,
561 "imprecise external abort");
563 ret
= ixp4xx_pci_parse_map_ranges(p
);
567 ret
= ixp4xx_pci_parse_map_dma_ranges(p
);
571 /* This is only configured in host mode */
573 addr
= __pa(PAGE_OFFSET
);
574 /* This is a noop (0x00) but explains what is going on */
575 addr
|= PCI_BASE_ADDRESS_SPACE_MEMORY
;
577 for (i
= 0; i
< 4; i
++) {
578 /* Write this directly into the config space */
579 ret
= ixp4xx_crp_write_config(p
, basereg
[i
], 4, addr
);
581 dev_err(dev
, "failed to set up PCI_BASE_ADDRESS_%d\n", i
);
583 dev_info(dev
, "set PCI_BASE_ADDR_%d to %pa\n", i
, &addr
);
588 * Enable CSR window at 64 MiB to allow PCI masters to continue
589 * prefetching past the 64 MiB boundary, if all AHB to PCI
590 * windows are consecutive.
592 ret
= ixp4xx_crp_write_config(p
, PCI_BASE_ADDRESS_4
, 4, addr
);
594 dev_err(dev
, "failed to set up PCI_BASE_ADDRESS_4\n");
596 dev_info(dev
, "set PCI_BASE_ADDR_4 to %pa\n", &addr
);
599 * Put the IO memory window at the very end of physical memory
600 * at 0xfffffc00. This is when the system is trying to access IO
604 addr
|= PCI_BASE_ADDRESS_SPACE_IO
;
605 ret
= ixp4xx_crp_write_config(p
, PCI_BASE_ADDRESS_5
, 4, addr
);
607 dev_err(dev
, "failed to set up PCI_BASE_ADDRESS_5\n");
609 dev_info(dev
, "set PCI_BASE_ADDR_5 to %pa\n", &addr
);
612 * Retry timeout to 0x80
613 * Transfer ready timeout to 0xff
615 ret
= ixp4xx_crp_write_config(p
, IXP4XX_PCI_RTOTTO
, 4,
618 dev_err(dev
, "failed to set up TRDY limit\n");
620 dev_info(dev
, "set TRDY limit to 0x80ff\n");
623 /* Clear interrupts */
624 val
= IXP4XX_PCI_ISR_PSE
| IXP4XX_PCI_ISR_PFE
| IXP4XX_PCI_ISR_PPE
| IXP4XX_PCI_ISR_AHBE
;
625 ixp4xx_writel(p
, IXP4XX_PCI_ISR
, val
);
628 * Set Initialize Complete in PCI Control Register: allow IXP4XX to
629 * generate PCI configuration cycles. Specify that the AHB bus is
630 * operating in big-endian mode. Set up byte lane swapping between
631 * little-endian PCI and the big-endian AHB bus.
633 val
= IXP4XX_PCI_CSR_IC
| IXP4XX_PCI_CSR_ABE
;
634 if (IS_ENABLED(CONFIG_CPU_BIG_ENDIAN
))
635 val
|= (IXP4XX_PCI_CSR_PDS
| IXP4XX_PCI_CSR_ADS
);
636 ixp4xx_writel(p
, IXP4XX_PCI_CSR
, val
);
638 ret
= ixp4xx_crp_write_config(p
, PCI_COMMAND
, 2, PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
);
640 dev_err(dev
, "unable to initialize master and command memory\n");
642 dev_info(dev
, "initialized as master\n");
644 pci_host_probe(host
);
649 static const struct of_device_id ixp4xx_pci_of_match
[] = {
651 .compatible
= "intel,ixp42x-pci",
654 .compatible
= "intel,ixp43x-pci",
660 * This driver needs to be a builtin module with suppressed bind
661 * attributes since the probe() is initializing a hard exception
662 * handler and this can only be done from __init-tagged code
663 * sections. This module cannot be removed and inserted at all.
665 static struct platform_driver ixp4xx_pci_driver
= {
667 .name
= "ixp4xx-pci",
668 .suppress_bind_attrs
= true,
669 .of_match_table
= ixp4xx_pci_of_match
,
672 builtin_platform_driver_probe(ixp4xx_pci_driver
, ixp4xx_pci_probe
);