Merge git://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux-2.6
[wrt350n-kernel.git] / arch / arm / mach-ixp4xx / common-pci.c
blob9bd45fe928c2ada9ab0b7a53cd93b313990151d0
1 /*
2 * arch/arm/mach-ixp4xx/common-pci.c
4 * IXP4XX PCI routines for all platforms
6 * Maintainer: Deepak Saxena <dsaxena@plexity.net>
8 * Copyright (C) 2002 Intel Corporation.
9 * Copyright (C) 2003 Greg Ungerer <gerg@snapgear.com>
10 * Copyright (C) 2003-2004 MontaVista Software, Inc.
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License version 2 as
14 * published by the Free Software Foundation.
18 #include <linux/sched.h>
19 #include <linux/kernel.h>
20 #include <linux/pci.h>
21 #include <linux/interrupt.h>
22 #include <linux/mm.h>
23 #include <linux/init.h>
24 #include <linux/ioport.h>
25 #include <linux/slab.h>
26 #include <linux/delay.h>
27 #include <linux/device.h>
28 #include <asm/dma-mapping.h>
30 #include <asm/io.h>
31 #include <asm/irq.h>
32 #include <asm/sizes.h>
33 #include <asm/system.h>
34 #include <asm/mach/pci.h>
35 #include <asm/hardware.h>
39 * IXP4xx PCI read function is dependent on whether we are
40 * running A0 or B0 (AppleGate) silicon.
42 int (*ixp4xx_pci_read)(u32 addr, u32 cmd, u32* data);
45 * Base address for PCI regsiter region
47 unsigned long ixp4xx_pci_reg_base = 0;
50 * PCI cfg an I/O routines are done by programming a
51 * command/byte enable register, and then read/writing
52 * the data from a data regsiter. We need to ensure
53 * these transactions are atomic or we will end up
54 * with corrupt data on the bus or in a driver.
56 static DEFINE_SPINLOCK(ixp4xx_pci_lock);
59 * Read from PCI config space
61 static void crp_read(u32 ad_cbe, u32 *data)
63 unsigned long flags;
64 spin_lock_irqsave(&ixp4xx_pci_lock, flags);
65 *PCI_CRP_AD_CBE = ad_cbe;
66 *data = *PCI_CRP_RDATA;
67 spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
71 * Write to PCI config space
73 static void crp_write(u32 ad_cbe, u32 data)
75 unsigned long flags;
76 spin_lock_irqsave(&ixp4xx_pci_lock, flags);
77 *PCI_CRP_AD_CBE = CRP_AD_CBE_WRITE | ad_cbe;
78 *PCI_CRP_WDATA = data;
79 spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
82 static inline int check_master_abort(void)
84 /* check Master Abort bit after access */
85 unsigned long isr = *PCI_ISR;
87 if (isr & PCI_ISR_PFE) {
88 /* make sure the Master Abort bit is reset */
89 *PCI_ISR = PCI_ISR_PFE;
90 <<<<<<< HEAD:arch/arm/mach-ixp4xx/common-pci.c
91 pr_debug("%s failed\n", __FUNCTION__);
92 =======
93 pr_debug("%s failed\n", __func__);
94 >>>>>>> 264e3e889d86e552b4191d69bb60f4f3b383135a:arch/arm/mach-ixp4xx/common-pci.c
95 return 1;
98 return 0;
101 int ixp4xx_pci_read_errata(u32 addr, u32 cmd, u32* data)
103 unsigned long flags;
104 int retval = 0;
105 int i;
107 spin_lock_irqsave(&ixp4xx_pci_lock, flags);
109 *PCI_NP_AD = addr;
112 * PCI workaround - only works if NP PCI space reads have
113 * no side effects!!! Read 8 times. last one will be good.
115 for (i = 0; i < 8; i++) {
116 *PCI_NP_CBE = cmd;
117 *data = *PCI_NP_RDATA;
118 *data = *PCI_NP_RDATA;
121 if(check_master_abort())
122 retval = 1;
124 spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
125 return retval;
128 int ixp4xx_pci_read_no_errata(u32 addr, u32 cmd, u32* data)
130 unsigned long flags;
131 int retval = 0;
133 spin_lock_irqsave(&ixp4xx_pci_lock, flags);
135 *PCI_NP_AD = addr;
137 /* set up and execute the read */
138 *PCI_NP_CBE = cmd;
140 /* the result of the read is now in NP_RDATA */
141 *data = *PCI_NP_RDATA;
143 if(check_master_abort())
144 retval = 1;
146 spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
147 return retval;
150 int ixp4xx_pci_write(u32 addr, u32 cmd, u32 data)
152 unsigned long flags;
153 int retval = 0;
155 spin_lock_irqsave(&ixp4xx_pci_lock, flags);
157 *PCI_NP_AD = addr;
159 /* set up the write */
160 *PCI_NP_CBE = cmd;
162 /* execute the write by writing to NP_WDATA */
163 *PCI_NP_WDATA = data;
165 if(check_master_abort())
166 retval = 1;
168 spin_unlock_irqrestore(&ixp4xx_pci_lock, flags);
169 return retval;
172 static u32 ixp4xx_config_addr(u8 bus_num, u16 devfn, int where)
174 u32 addr;
175 if (!bus_num) {
176 /* type 0 */
177 addr = BIT(32-PCI_SLOT(devfn)) | ((PCI_FUNC(devfn)) << 8) |
178 (where & ~3);
179 } else {
180 /* type 1 */
181 addr = (bus_num << 16) | ((PCI_SLOT(devfn)) << 11) |
182 ((PCI_FUNC(devfn)) << 8) | (where & ~3) | 1;
184 return addr;
188 * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
189 * 0 and 3 are not valid indexes...
191 static u32 bytemask[] = {
192 /*0*/ 0,
193 /*1*/ 0xff,
194 /*2*/ 0xffff,
195 /*3*/ 0,
196 /*4*/ 0xffffffff,
199 static u32 local_byte_lane_enable_bits(u32 n, int size)
201 if (size == 1)
202 return (0xf & ~BIT(n)) << CRP_AD_CBE_BESL;
203 if (size == 2)
204 return (0xf & ~(BIT(n) | BIT(n+1))) << CRP_AD_CBE_BESL;
205 if (size == 4)
206 return 0;
207 return 0xffffffff;
210 static int local_read_config(int where, int size, u32 *value)
212 u32 n, data;
213 pr_debug("local_read_config from %d size %d\n", where, size);
214 n = where % 4;
215 crp_read(where & ~3, &data);
216 *value = (data >> (8*n)) & bytemask[size];
217 pr_debug("local_read_config read %#x\n", *value);
218 return PCIBIOS_SUCCESSFUL;
221 static int local_write_config(int where, int size, u32 value)
223 u32 n, byte_enables, data;
224 pr_debug("local_write_config %#x to %d size %d\n", value, where, size);
225 n = where % 4;
226 byte_enables = local_byte_lane_enable_bits(n, size);
227 if (byte_enables == 0xffffffff)
228 return PCIBIOS_BAD_REGISTER_NUMBER;
229 data = value << (8*n);
230 crp_write((where & ~3) | byte_enables, data);
231 return PCIBIOS_SUCCESSFUL;
234 static u32 byte_lane_enable_bits(u32 n, int size)
236 if (size == 1)
237 return (0xf & ~BIT(n)) << 4;
238 if (size == 2)
239 return (0xf & ~(BIT(n) | BIT(n+1))) << 4;
240 if (size == 4)
241 return 0;
242 return 0xffffffff;
245 static int ixp4xx_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 *value)
247 u32 n, byte_enables, addr, data;
248 u8 bus_num = bus->number;
250 pr_debug("read_config from %d size %d dev %d:%d:%d\n", where, size,
251 bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
253 *value = 0xffffffff;
254 n = where % 4;
255 byte_enables = byte_lane_enable_bits(n, size);
256 if (byte_enables == 0xffffffff)
257 return PCIBIOS_BAD_REGISTER_NUMBER;
259 addr = ixp4xx_config_addr(bus_num, devfn, where);
260 if (ixp4xx_pci_read(addr, byte_enables | NP_CMD_CONFIGREAD, &data))
261 return PCIBIOS_DEVICE_NOT_FOUND;
263 *value = (data >> (8*n)) & bytemask[size];
264 pr_debug("read_config_byte read %#x\n", *value);
265 return PCIBIOS_SUCCESSFUL;
268 static int ixp4xx_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where, int size, u32 value)
270 u32 n, byte_enables, addr, data;
271 u8 bus_num = bus->number;
273 pr_debug("write_config_byte %#x to %d size %d dev %d:%d:%d\n", value, where,
274 size, bus_num, PCI_SLOT(devfn), PCI_FUNC(devfn));
276 n = where % 4;
277 byte_enables = byte_lane_enable_bits(n, size);
278 if (byte_enables == 0xffffffff)
279 return PCIBIOS_BAD_REGISTER_NUMBER;
281 addr = ixp4xx_config_addr(bus_num, devfn, where);
282 data = value << (8*n);
283 if (ixp4xx_pci_write(addr, byte_enables | NP_CMD_CONFIGWRITE, data))
284 return PCIBIOS_DEVICE_NOT_FOUND;
286 return PCIBIOS_SUCCESSFUL;
289 struct pci_ops ixp4xx_ops = {
290 .read = ixp4xx_pci_read_config,
291 .write = ixp4xx_pci_write_config,
295 * PCI abort handler
297 static int abort_handler(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
299 u32 isr, status;
301 isr = *PCI_ISR;
302 local_read_config(PCI_STATUS, 2, &status);
303 pr_debug("PCI: abort_handler addr = %#lx, isr = %#x, "
304 "status = %#x\n", addr, isr, status);
306 /* make sure the Master Abort bit is reset */
307 *PCI_ISR = PCI_ISR_PFE;
308 status |= PCI_STATUS_REC_MASTER_ABORT;
309 local_write_config(PCI_STATUS, 2, status);
312 * If it was an imprecise abort, then we need to correct the
313 * return address to be _after_ the instruction.
315 if (fsr & (1 << 10))
316 regs->ARM_pc += 4;
318 return 0;
323 * Setup DMA mask to 64MB on PCI devices. Ignore all other devices.
325 static int ixp4xx_pci_platform_notify(struct device *dev)
327 if(dev->bus == &pci_bus_type) {
328 *dev->dma_mask = SZ_64M - 1;
329 dev->coherent_dma_mask = SZ_64M - 1;
330 dmabounce_register_dev(dev, 2048, 4096);
332 return 0;
335 static int ixp4xx_pci_platform_notify_remove(struct device *dev)
337 if(dev->bus == &pci_bus_type) {
338 dmabounce_unregister_dev(dev);
340 return 0;
343 int dma_needs_bounce(struct device *dev, dma_addr_t dma_addr, size_t size)
345 return (dev->bus == &pci_bus_type ) && ((dma_addr + size) >= SZ_64M);
349 * Only first 64MB of memory can be accessed via PCI.
350 * We use GFP_DMA to allocate safe buffers to do map/unmap.
351 * This is really ugly and we need a better way of specifying
352 * DMA-capable regions of memory.
354 void __init ixp4xx_adjust_zones(int node, unsigned long *zone_size,
355 unsigned long *zhole_size)
357 unsigned int sz = SZ_64M >> PAGE_SHIFT;
360 * Only adjust if > 64M on current system
362 if (node || (zone_size[0] <= sz))
363 return;
365 zone_size[1] = zone_size[0] - sz;
366 zone_size[0] = sz;
367 zhole_size[1] = zhole_size[0];
368 zhole_size[0] = 0;
371 void __init ixp4xx_pci_preinit(void)
373 unsigned long processor_id;
375 asm("mrc p15, 0, %0, cr0, cr0, 0;" : "=r"(processor_id) :);
378 * Determine which PCI read method to use.
379 * Rev 0 IXP425 requires workaround.
381 if (!(processor_id & 0xf) && cpu_is_ixp42x()) {
382 printk("PCI: IXP42x A0 silicon detected - "
383 "PCI Non-Prefetch Workaround Enabled\n");
384 ixp4xx_pci_read = ixp4xx_pci_read_errata;
385 } else
386 ixp4xx_pci_read = ixp4xx_pci_read_no_errata;
389 /* hook in our fault handler for PCI errors */
390 hook_fault_code(16+6, abort_handler, SIGBUS, "imprecise external abort");
392 pr_debug("setup PCI-AHB(inbound) and AHB-PCI(outbound) address mappings\n");
395 * We use identity AHB->PCI address translation
396 * in the 0x48000000 to 0x4bffffff address space
398 *PCI_PCIMEMBASE = 0x48494A4B;
401 * We also use identity PCI->AHB address translation
402 * in 4 16MB BARs that begin at the physical memory start
404 *PCI_AHBMEMBASE = (PHYS_OFFSET & 0xFF000000) +
405 ((PHYS_OFFSET & 0xFF000000) >> 8) +
406 ((PHYS_OFFSET & 0xFF000000) >> 16) +
407 ((PHYS_OFFSET & 0xFF000000) >> 24) +
408 0x00010203;
410 if (*PCI_CSR & PCI_CSR_HOST) {
411 printk("PCI: IXP4xx is host\n");
413 pr_debug("setup BARs in controller\n");
416 * We configure the PCI inbound memory windows to be
417 * 1:1 mapped to SDRAM
419 local_write_config(PCI_BASE_ADDRESS_0, 4, PHYS_OFFSET + 0x00000000);
420 local_write_config(PCI_BASE_ADDRESS_1, 4, PHYS_OFFSET + 0x01000000);
421 local_write_config(PCI_BASE_ADDRESS_2, 4, PHYS_OFFSET + 0x02000000);
422 local_write_config(PCI_BASE_ADDRESS_3, 4, PHYS_OFFSET + 0x03000000);
425 * Enable CSR window at 0xff000000.
427 local_write_config(PCI_BASE_ADDRESS_4, 4, 0xff000008);
430 * Enable the IO window to be way up high, at 0xfffffc00
432 local_write_config(PCI_BASE_ADDRESS_5, 4, 0xfffffc01);
433 } else {
434 printk("PCI: IXP4xx is target - No bus scan performed\n");
437 printk("PCI: IXP4xx Using %s access for memory space\n",
438 #ifndef CONFIG_IXP4XX_INDIRECT_PCI
439 "direct"
440 #else
441 "indirect"
442 #endif
445 pr_debug("clear error bits in ISR\n");
446 *PCI_ISR = PCI_ISR_PSE | PCI_ISR_PFE | PCI_ISR_PPE | PCI_ISR_AHBE;
449 * Set Initialize Complete in PCI Control Register: allow IXP4XX to
450 * respond to PCI configuration cycles. Specify that the AHB bus is
451 * operating in big endian mode. Set up byte lane swapping between
452 * little-endian PCI and the big-endian AHB bus
454 #ifdef __ARMEB__
455 *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE | PCI_CSR_PDS | PCI_CSR_ADS;
456 #else
457 *PCI_CSR = PCI_CSR_IC | PCI_CSR_ABE;
458 #endif
460 pr_debug("DONE\n");
463 int ixp4xx_setup(int nr, struct pci_sys_data *sys)
465 struct resource *res;
467 if (nr >= 1)
468 return 0;
470 res = kzalloc(sizeof(*res) * 2, GFP_KERNEL);
471 if (res == NULL) {
473 * If we're out of memory this early, something is wrong,
474 * so we might as well catch it here.
476 panic("PCI: unable to allocate resources?\n");
479 local_write_config(PCI_COMMAND, 2, PCI_COMMAND_MASTER | PCI_COMMAND_MEMORY);
481 res[0].name = "PCI I/O Space";
482 res[0].start = 0x00000000;
483 res[0].end = 0x0000ffff;
484 res[0].flags = IORESOURCE_IO;
486 res[1].name = "PCI Memory Space";
487 res[1].start = PCIBIOS_MIN_MEM;
488 #ifndef CONFIG_IXP4XX_INDIRECT_PCI
489 res[1].end = 0x4bffffff;
490 #else
491 res[1].end = 0x4fffffff;
492 #endif
493 res[1].flags = IORESOURCE_MEM;
495 request_resource(&ioport_resource, &res[0]);
496 request_resource(&iomem_resource, &res[1]);
498 sys->resource[0] = &res[0];
499 sys->resource[1] = &res[1];
500 sys->resource[2] = NULL;
502 platform_notify = ixp4xx_pci_platform_notify;
503 platform_notify_remove = ixp4xx_pci_platform_notify_remove;
505 return 1;
508 struct pci_bus *ixp4xx_scan_bus(int nr, struct pci_sys_data *sys)
510 return pci_scan_bus(sys->busnr, &ixp4xx_ops, sys);
514 * We override these so we properly do dmabounce otherwise drivers
515 * are able to set the dma_mask to 0xffffffff and we can no longer
516 * trap bounces. :(
518 * We just return true on everyhing except for < 64MB in which case
519 * we will fail miseralby and die since we can't handle that case.
522 pci_set_dma_mask(struct pci_dev *dev, u64 mask)
524 if (mask >= SZ_64M - 1 )
525 return 0;
527 return -EIO;
531 pci_set_consistent_dma_mask(struct pci_dev *dev, u64 mask)
533 if (mask >= SZ_64M - 1 )
534 return 0;
536 return -EIO;
539 EXPORT_SYMBOL(ixp4xx_pci_read);
540 EXPORT_SYMBOL(ixp4xx_pci_write);