pci.c 5.3 KB

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