pci.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. /*
  2. * arch/arm/mach-ixp2000/pci.c
  3. *
  4. * PCI routines for IXDP2400/IXDP2800 boards
  5. *
  6. * Original Author: Naeem Afzal <naeem.m.afzal@intel.com>
  7. * Maintained by: Deepak Saxena <dsaxena@plexity.net>
  8. *
  9. * Copyright 2002 Intel Corp.
  10. * Copyright (C) 2003-2004 MontaVista Software, Inc.
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/sched.h>
  18. #include <linux/kernel.h>
  19. #include <linux/pci.h>
  20. #include <linux/interrupt.h>
  21. #include <linux/mm.h>
  22. #include <linux/init.h>
  23. #include <linux/ioport.h>
  24. #include <linux/slab.h>
  25. #include <linux/delay.h>
  26. #include <asm/io.h>
  27. #include <asm/irq.h>
  28. #include <asm/system.h>
  29. #include <asm/mach-types.h>
  30. #include <asm/hardware.h>
  31. #include <asm/mach/pci.h>
  32. static int pci_master_aborts = 0;
  33. static int clear_master_aborts(void);
  34. u32 *
  35. ixp2000_pci_config_addr(unsigned int bus_nr, unsigned int devfn, int where)
  36. {
  37. u32 *paddress;
  38. if (PCI_SLOT(devfn) > 7)
  39. return 0;
  40. /* Must be dword aligned */
  41. where &= ~3;
  42. /*
  43. * For top bus, generate type 0, else type 1
  44. */
  45. if (!bus_nr) {
  46. /* only bits[23:16] are used for IDSEL */
  47. paddress = (u32 *) (IXP2000_PCI_CFG0_VIRT_BASE
  48. | (1 << (PCI_SLOT(devfn) + 16))
  49. | (PCI_FUNC(devfn) << 8) | where);
  50. } else {
  51. paddress = (u32 *) (IXP2000_PCI_CFG1_VIRT_BASE
  52. | (bus_nr << 16)
  53. | (PCI_SLOT(devfn) << 11)
  54. | (PCI_FUNC(devfn) << 8) | where);
  55. }
  56. return paddress;
  57. }
  58. /*
  59. * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
  60. * 0 and 3 are not valid indexes...
  61. */
  62. static u32 bytemask[] = {
  63. /*0*/ 0,
  64. /*1*/ 0xff,
  65. /*2*/ 0xffff,
  66. /*3*/ 0,
  67. /*4*/ 0xffffffff,
  68. };
  69. int ixp2000_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  70. int size, u32 *value)
  71. {
  72. u32 n;
  73. u32 *addr;
  74. n = where % 4;
  75. addr = ixp2000_pci_config_addr(bus->number, devfn, where);
  76. if (!addr)
  77. return PCIBIOS_DEVICE_NOT_FOUND;
  78. pci_master_aborts = 0;
  79. *value = (*addr >> (8*n)) & bytemask[size];
  80. if (pci_master_aborts) {
  81. pci_master_aborts = 0;
  82. *value = 0xffffffff;
  83. return PCIBIOS_DEVICE_NOT_FOUND;
  84. }
  85. return PCIBIOS_SUCCESSFUL;
  86. }
  87. /*
  88. * We don't do error checks by callling clear_master_aborts() b/c the
  89. * assumption is that the caller did a read first to make sure a device
  90. * exists.
  91. */
  92. int ixp2000_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  93. int size, u32 value)
  94. {
  95. u32 mask;
  96. u32 *addr;
  97. u32 temp;
  98. mask = ~(bytemask[size] << ((where % 0x4) * 8));
  99. addr = ixp2000_pci_config_addr(bus->number, devfn, where);
  100. if (!addr)
  101. return PCIBIOS_DEVICE_NOT_FOUND;
  102. temp = (u32) (value) << ((where % 0x4) * 8);
  103. *addr = (*addr & mask) | temp;
  104. clear_master_aborts();
  105. return PCIBIOS_SUCCESSFUL;
  106. }
  107. static struct pci_ops ixp2000_pci_ops = {
  108. .read = ixp2000_pci_read_config,
  109. .write = ixp2000_pci_write_config
  110. };
  111. struct pci_bus *ixp2000_pci_scan_bus(int nr, struct pci_sys_data *sysdata)
  112. {
  113. return pci_scan_bus(sysdata->busnr, &ixp2000_pci_ops, sysdata);
  114. }
  115. int ixp2000_pci_abort_handler(unsigned long addr, unsigned int fsr, struct pt_regs *regs)
  116. {
  117. volatile u32 temp;
  118. unsigned long flags;
  119. pci_master_aborts = 1;
  120. local_irq_save(flags);
  121. temp = *(IXP2000_PCI_CONTROL);
  122. if (temp & ((1 << 8) | (1 << 5))) {
  123. ixp2000_reg_write(IXP2000_PCI_CONTROL, temp);
  124. }
  125. temp = *(IXP2000_PCI_CMDSTAT);
  126. if (temp & (1 << 29)) {
  127. while (temp & (1 << 29)) {
  128. ixp2000_reg_write(IXP2000_PCI_CMDSTAT, temp);
  129. temp = *(IXP2000_PCI_CMDSTAT);
  130. }
  131. }
  132. local_irq_restore(flags);
  133. /*
  134. * If it was an imprecise abort, then we need to correct the
  135. * return address to be _after_ the instruction.
  136. */
  137. if (fsr & (1 << 10))
  138. regs->ARM_pc += 4;
  139. return 0;
  140. }
  141. int
  142. clear_master_aborts(void)
  143. {
  144. volatile u32 temp;
  145. unsigned long flags;
  146. local_irq_save(flags);
  147. temp = *(IXP2000_PCI_CONTROL);
  148. if (temp & ((1 << 8) | (1 << 5))) {
  149. ixp2000_reg_write(IXP2000_PCI_CONTROL, temp);
  150. }
  151. temp = *(IXP2000_PCI_CMDSTAT);
  152. if (temp & (1 << 29)) {
  153. while (temp & (1 << 29)) {
  154. ixp2000_reg_write(IXP2000_PCI_CMDSTAT, temp);
  155. temp = *(IXP2000_PCI_CMDSTAT);
  156. }
  157. }
  158. local_irq_restore(flags);
  159. return 0;
  160. }
  161. void __init
  162. ixp2000_pci_preinit(void)
  163. {
  164. #ifndef CONFIG_IXP2000_SUPPORT_BROKEN_PCI_IO
  165. /*
  166. * Configure the PCI unit to properly byteswap I/O transactions,
  167. * and verify that it worked.
  168. */
  169. ixp2000_reg_write(IXP2000_PCI_CONTROL,
  170. (*IXP2000_PCI_CONTROL | PCI_CONTROL_IEE));
  171. if ((*IXP2000_PCI_CONTROL & PCI_CONTROL_IEE) == 0)
  172. panic("IXP2000: PCI I/O is broken on this ixp model, and "
  173. "the needed workaround has not been configured in");
  174. #endif
  175. hook_fault_code(16+6, ixp2000_pci_abort_handler, SIGBUS,
  176. "PCI config cycle to non-existent device");
  177. }
  178. /*
  179. * IXP2000 systems often have large resource requirements, so we just
  180. * use our own resource space.
  181. */
  182. static struct resource ixp2000_pci_mem_space = {
  183. .start = 0xe0000000,
  184. .end = 0xffffffff,
  185. .flags = IORESOURCE_MEM,
  186. .name = "PCI Mem Space"
  187. };
  188. static struct resource ixp2000_pci_io_space = {
  189. .start = 0x00010000,
  190. .end = 0x0001ffff,
  191. .flags = IORESOURCE_IO,
  192. .name = "PCI I/O Space"
  193. };
  194. int ixp2000_pci_setup(int nr, struct pci_sys_data *sys)
  195. {
  196. if (nr >= 1)
  197. return 0;
  198. sys->resource[0] = &ixp2000_pci_io_space;
  199. sys->resource[1] = &ixp2000_pci_mem_space;
  200. sys->resource[2] = NULL;
  201. return 1;
  202. }