1 // SPDX-License-Identifier: GPL-2.0-only
3 * arch/arm/mach-ixp4xx/common-pci.c
5 * IXP4XX PCI routines for all platforms
7 * Maintainer: Deepak Saxena <dsaxena@plexity.net>
9 * Copyright (C) 2002 Intel Corporation.
10 * Copyright (C) 2003 Greg Ungerer <gerg@snapgear.com>
11 * Copyright (C) 2003-2004 MontaVista Software, Inc.
14 #include <linux/sched.h>
15 #include <linux/kernel.h>
16 #include <linux/pci.h>
17 #include <linux/interrupt.h>
19 #include <linux/init.h>
20 #include <linux/ioport.h>
21 #include <linux/slab.h>
22 #include <linux/delay.h>
23 #include <linux/device.h>
25 #include <linux/export.h>
26 #include <asm/dma-mapping.h>
28 #include <asm/cputype.h>
30 #include <linux/sizes.h>
31 #include <asm/mach/pci.h>
32 #include <mach/hardware.h>
36 * IXP4xx PCI read function is dependent on whether we are
37 * running A0 or B0 (AppleGate) silicon.
39 int (*ixp4xx_pci_read
)(u32 addr
, u32 cmd
, u32
* data
);
42 * Base address for PCI register region
44 unsigned long ixp4xx_pci_reg_base
= 0;
47 * PCI cfg an I/O routines are done by programming a
48 * command/byte enable register, and then read/writing
49 * the data from a data register. We need to ensure
50 * these transactions are atomic or we will end up
51 * with corrupt data on the bus or in a driver.
53 static DEFINE_RAW_SPINLOCK(ixp4xx_pci_lock
);
56 * Read from PCI config space
58 static void crp_read(u32 ad_cbe
, u32
*data
)
61 raw_spin_lock_irqsave(&ixp4xx_pci_lock
, flags
);
62 *PCI_CRP_AD_CBE
= ad_cbe
;
63 *data
= *PCI_CRP_RDATA
;
64 raw_spin_unlock_irqrestore(&ixp4xx_pci_lock
, flags
);
68 * Write to PCI config space
70 static void crp_write(u32 ad_cbe
, u32 data
)
73 raw_spin_lock_irqsave(&ixp4xx_pci_lock
, flags
);
74 *PCI_CRP_AD_CBE
= CRP_AD_CBE_WRITE
| ad_cbe
;
75 *PCI_CRP_WDATA
= data
;
76 raw_spin_unlock_irqrestore(&ixp4xx_pci_lock
, flags
);
79 static inline int check_master_abort(void)
81 /* check Master Abort bit after access */
82 unsigned long isr
= *PCI_ISR
;
84 if (isr
& PCI_ISR_PFE
) {
85 /* make sure the Master Abort bit is reset */
86 *PCI_ISR
= PCI_ISR_PFE
;
87 pr_debug("%s failed\n", __func__
);
94 int ixp4xx_pci_read_errata(u32 addr
, u32 cmd
, u32
* data
)
100 raw_spin_lock_irqsave(&ixp4xx_pci_lock
, flags
);
105 * PCI workaround - only works if NP PCI space reads have
106 * no side effects!!! Read 8 times. last one will be good.
108 for (i
= 0; i
< 8; i
++) {
110 *data
= *PCI_NP_RDATA
;
111 *data
= *PCI_NP_RDATA
;
114 if(check_master_abort())
117 raw_spin_unlock_irqrestore(&ixp4xx_pci_lock
, flags
);
121 int ixp4xx_pci_read_no_errata(u32 addr
, u32 cmd
, u32
* data
)
126 raw_spin_lock_irqsave(&ixp4xx_pci_lock
, flags
);
130 /* set up and execute the read */
133 /* the result of the read is now in NP_RDATA */
134 *data
= *PCI_NP_RDATA
;
136 if(check_master_abort())
139 raw_spin_unlock_irqrestore(&ixp4xx_pci_lock
, flags
);
143 int ixp4xx_pci_write(u32 addr
, u32 cmd
, u32 data
)
148 raw_spin_lock_irqsave(&ixp4xx_pci_lock
, flags
);
152 /* set up the write */
155 /* execute the write by writing to NP_WDATA */
156 *PCI_NP_WDATA
= data
;
158 if(check_master_abort())
161 raw_spin_unlock_irqrestore(&ixp4xx_pci_lock
, flags
);
165 static u32
ixp4xx_config_addr(u8 bus_num
, u16 devfn
, int where
)
170 addr
= BIT(32-PCI_SLOT(devfn
)) | ((PCI_FUNC(devfn
)) << 8) |
174 addr
= (bus_num
<< 16) | ((PCI_SLOT(devfn
)) << 11) |
175 ((PCI_FUNC(devfn
)) << 8) | (where
& ~3) | 1;
181 * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
182 * 0 and 3 are not valid indexes...
184 static u32 bytemask
[] = {
192 static u32
local_byte_lane_enable_bits(u32 n
, int size
)
195 return (0xf & ~BIT(n
)) << CRP_AD_CBE_BESL
;
197 return (0xf & ~(BIT(n
) | BIT(n
+1))) << CRP_AD_CBE_BESL
;
203 static int local_read_config(int where
, int size
, u32
*value
)
206 pr_debug("local_read_config from %d size %d\n", where
, size
);
208 crp_read(where
& ~3, &data
);
209 *value
= (data
>> (8*n
)) & bytemask
[size
];
210 pr_debug("local_read_config read %#x\n", *value
);
211 return PCIBIOS_SUCCESSFUL
;
214 static int local_write_config(int where
, int size
, u32 value
)
216 u32 n
, byte_enables
, data
;
217 pr_debug("local_write_config %#x to %d size %d\n", value
, where
, size
);
219 byte_enables
= local_byte_lane_enable_bits(n
, size
);
220 if (byte_enables
== 0xffffffff)
221 return PCIBIOS_BAD_REGISTER_NUMBER
;
222 data
= value
<< (8*n
);
223 crp_write((where
& ~3) | byte_enables
, data
);
224 return PCIBIOS_SUCCESSFUL
;
227 static u32
byte_lane_enable_bits(u32 n
, int size
)
230 return (0xf & ~BIT(n
)) << 4;
232 return (0xf & ~(BIT(n
) | BIT(n
+1))) << 4;
238 static int ixp4xx_pci_read_config(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32
*value
)
240 u32 n
, byte_enables
, addr
, data
;
241 u8 bus_num
= bus
->number
;
243 pr_debug("read_config from %d size %d dev %d:%d:%d\n", where
, size
,
244 bus_num
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
248 byte_enables
= byte_lane_enable_bits(n
, size
);
249 if (byte_enables
== 0xffffffff)
250 return PCIBIOS_BAD_REGISTER_NUMBER
;
252 addr
= ixp4xx_config_addr(bus_num
, devfn
, where
);
253 if (ixp4xx_pci_read(addr
, byte_enables
| NP_CMD_CONFIGREAD
, &data
))
254 return PCIBIOS_DEVICE_NOT_FOUND
;
256 *value
= (data
>> (8*n
)) & bytemask
[size
];
257 pr_debug("read_config_byte read %#x\n", *value
);
258 return PCIBIOS_SUCCESSFUL
;
261 static int ixp4xx_pci_write_config(struct pci_bus
*bus
, unsigned int devfn
, int where
, int size
, u32 value
)
263 u32 n
, byte_enables
, addr
, data
;
264 u8 bus_num
= bus
->number
;
266 pr_debug("write_config_byte %#x to %d size %d dev %d:%d:%d\n", value
, where
,
267 size
, bus_num
, PCI_SLOT(devfn
), PCI_FUNC(devfn
));
270 byte_enables
= byte_lane_enable_bits(n
, size
);
271 if (byte_enables
== 0xffffffff)
272 return PCIBIOS_BAD_REGISTER_NUMBER
;
274 addr
= ixp4xx_config_addr(bus_num
, devfn
, where
);
275 data
= value
<< (8*n
);
276 if (ixp4xx_pci_write(addr
, byte_enables
| NP_CMD_CONFIGWRITE
, data
))
277 return PCIBIOS_DEVICE_NOT_FOUND
;
279 return PCIBIOS_SUCCESSFUL
;
282 struct pci_ops ixp4xx_ops
= {
283 .read
= ixp4xx_pci_read_config
,
284 .write
= ixp4xx_pci_write_config
,
290 static int abort_handler(unsigned long addr
, unsigned int fsr
, struct pt_regs
*regs
)
295 local_read_config(PCI_STATUS
, 2, &status
);
296 pr_debug("PCI: abort_handler addr = %#lx, isr = %#x, "
297 "status = %#x\n", addr
, isr
, status
);
299 /* make sure the Master Abort bit is reset */
300 *PCI_ISR
= PCI_ISR_PFE
;
301 status
|= PCI_STATUS_REC_MASTER_ABORT
;
302 local_write_config(PCI_STATUS
, 2, status
);
305 * If it was an imprecise abort, then we need to correct the
306 * return address to be _after_ the instruction.
314 void __init
ixp4xx_pci_preinit(void)
316 unsigned long cpuid
= read_cpuid_id();
318 #ifdef CONFIG_IXP4XX_INDIRECT_PCI
319 pcibios_min_mem
= 0x10000000; /* 1 GB of indirect PCI MMIO space */
321 pcibios_min_mem
= 0x48000000; /* 64 MB of PCI MMIO space */
324 * Determine which PCI read method to use.
325 * Rev 0 IXP425 requires workaround.
327 if (!(cpuid
& 0xf) && cpu_is_ixp42x()) {
328 printk("PCI: IXP42x A0 silicon detected - "
329 "PCI Non-Prefetch Workaround Enabled\n");
330 ixp4xx_pci_read
= ixp4xx_pci_read_errata
;
332 ixp4xx_pci_read
= ixp4xx_pci_read_no_errata
;
335 /* hook in our fault handler for PCI errors */
336 hook_fault_code(16+6, abort_handler
, SIGBUS
, 0,
337 "imprecise external abort");
339 pr_debug("setup PCI-AHB(inbound) and AHB-PCI(outbound) address mappings\n");
342 * We use identity AHB->PCI address translation
343 * in the 0x48000000 to 0x4bffffff address space
345 *PCI_PCIMEMBASE
= 0x48494A4B;
348 * We also use identity PCI->AHB address translation
349 * in 4 16MB BARs that begin at the physical memory start
351 *PCI_AHBMEMBASE
= (PHYS_OFFSET
& 0xFF000000) +
352 ((PHYS_OFFSET
& 0xFF000000) >> 8) +
353 ((PHYS_OFFSET
& 0xFF000000) >> 16) +
354 ((PHYS_OFFSET
& 0xFF000000) >> 24) +
357 if (*PCI_CSR
& PCI_CSR_HOST
) {
358 printk("PCI: IXP4xx is host\n");
360 pr_debug("setup BARs in controller\n");
363 * We configure the PCI inbound memory windows to be
364 * 1:1 mapped to SDRAM
366 local_write_config(PCI_BASE_ADDRESS_0
, 4, PHYS_OFFSET
);
367 local_write_config(PCI_BASE_ADDRESS_1
, 4, PHYS_OFFSET
+ SZ_16M
);
368 local_write_config(PCI_BASE_ADDRESS_2
, 4, PHYS_OFFSET
+ SZ_32M
);
369 local_write_config(PCI_BASE_ADDRESS_3
, 4,
370 PHYS_OFFSET
+ SZ_32M
+ SZ_16M
);
373 * Enable CSR window at 64 MiB to allow PCI masters
374 * to continue prefetching past 64 MiB boundary.
376 local_write_config(PCI_BASE_ADDRESS_4
, 4, PHYS_OFFSET
+ SZ_64M
);
379 * Enable the IO window to be way up high, at 0xfffffc00
381 local_write_config(PCI_BASE_ADDRESS_5
, 4, 0xfffffc01);
382 local_write_config(0x40, 4, 0x000080FF); /* No TRDY time limit */
384 printk("PCI: IXP4xx is target - No bus scan performed\n");
387 printk("PCI: IXP4xx Using %s access for memory space\n",
388 #ifndef CONFIG_IXP4XX_INDIRECT_PCI
395 pr_debug("clear error bits in ISR\n");
396 *PCI_ISR
= PCI_ISR_PSE
| PCI_ISR_PFE
| PCI_ISR_PPE
| PCI_ISR_AHBE
;
399 * Set Initialize Complete in PCI Control Register: allow IXP4XX to
400 * respond to PCI configuration cycles. Specify that the AHB bus is
401 * operating in big endian mode. Set up byte lane swapping between
402 * little-endian PCI and the big-endian AHB bus
405 *PCI_CSR
= PCI_CSR_IC
| PCI_CSR_ABE
| PCI_CSR_PDS
| PCI_CSR_ADS
;
407 *PCI_CSR
= PCI_CSR_IC
| PCI_CSR_ABE
;
413 int ixp4xx_setup(int nr
, struct pci_sys_data
*sys
)
415 struct resource
*res
;
420 res
= kcalloc(2, sizeof(*res
), GFP_KERNEL
);
423 * If we're out of memory this early, something is wrong,
424 * so we might as well catch it here.
426 panic("PCI: unable to allocate resources?\n");
429 local_write_config(PCI_COMMAND
, 2, PCI_COMMAND_MASTER
| PCI_COMMAND_MEMORY
);
431 res
[0].name
= "PCI I/O Space";
432 res
[0].start
= 0x00000000;
433 res
[0].end
= 0x0000ffff;
434 res
[0].flags
= IORESOURCE_IO
;
436 res
[1].name
= "PCI Memory Space";
437 res
[1].start
= PCIBIOS_MIN_MEM
;
438 res
[1].end
= PCIBIOS_MAX_MEM
;
439 res
[1].flags
= IORESOURCE_MEM
;
441 request_resource(&ioport_resource
, &res
[0]);
442 request_resource(&iomem_resource
, &res
[1]);
444 pci_add_resource_offset(&sys
->resources
, &res
[0], sys
->io_offset
);
445 pci_add_resource_offset(&sys
->resources
, &res
[1], sys
->mem_offset
);
450 EXPORT_SYMBOL(ixp4xx_pci_read
);
451 EXPORT_SYMBOL(ixp4xx_pci_write
);