pci.c 5.3 KB

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