pci.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 <mach/hardware.h>
  28. #include <asm/mach/pci.h>
  29. static volatile int pci_master_aborts = 0;
  30. static int clear_master_aborts(void);
  31. u32 *
  32. ixp2000_pci_config_addr(unsigned int bus_nr, unsigned int devfn, int where)
  33. {
  34. u32 *paddress;
  35. if (PCI_SLOT(devfn) > 7)
  36. return 0;
  37. /* Must be dword aligned */
  38. where &= ~3;
  39. /*
  40. * For top bus, generate type 0, else type 1
  41. */
  42. if (!bus_nr) {
  43. /* only bits[23:16] are used for IDSEL */
  44. paddress = (u32 *) (IXP2000_PCI_CFG0_VIRT_BASE
  45. | (1 << (PCI_SLOT(devfn) + 16))
  46. | (PCI_FUNC(devfn) << 8) | where);
  47. } else {
  48. paddress = (u32 *) (IXP2000_PCI_CFG1_VIRT_BASE
  49. | (bus_nr << 16)
  50. | (PCI_SLOT(devfn) << 11)
  51. | (PCI_FUNC(devfn) << 8) | where);
  52. }
  53. return paddress;
  54. }
  55. /*
  56. * Mask table, bits to mask for quantity of size 1, 2 or 4 bytes.
  57. * 0 and 3 are not valid indexes...
  58. */
  59. static u32 bytemask[] = {
  60. /*0*/ 0,
  61. /*1*/ 0xff,
  62. /*2*/ 0xffff,
  63. /*3*/ 0,
  64. /*4*/ 0xffffffff,
  65. };
  66. int ixp2000_pci_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  67. int size, u32 *value)
  68. {
  69. u32 n;
  70. u32 *addr;
  71. n = where % 4;
  72. addr = ixp2000_pci_config_addr(bus->number, devfn, where);
  73. if (!addr)
  74. return PCIBIOS_DEVICE_NOT_FOUND;
  75. pci_master_aborts = 0;
  76. *value = (*addr >> (8*n)) & bytemask[size];
  77. if (pci_master_aborts) {
  78. pci_master_aborts = 0;
  79. *value = 0xffffffff;
  80. return PCIBIOS_DEVICE_NOT_FOUND;
  81. }
  82. return PCIBIOS_SUCCESSFUL;
  83. }
  84. /*
  85. * We don't do error checks by calling clear_master_aborts() b/c the
  86. * assumption is that the caller did a read first to make sure a device
  87. * exists.
  88. */
  89. int ixp2000_pci_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  90. int size, u32 value)
  91. {
  92. u32 mask;
  93. u32 *addr;
  94. u32 temp;
  95. mask = ~(bytemask[size] << ((where % 0x4) * 8));
  96. addr = ixp2000_pci_config_addr(bus->number, devfn, where);
  97. if (!addr)
  98. return PCIBIOS_DEVICE_NOT_FOUND;
  99. temp = (u32) (value) << ((where % 0x4) * 8);
  100. *addr = (*addr & mask) | temp;
  101. clear_master_aborts();
  102. return PCIBIOS_SUCCESSFUL;
  103. }
  104. static struct pci_ops ixp2000_pci_ops = {
  105. .read = ixp2000_pci_read_config,
  106. .write = ixp2000_pci_write_config
  107. };
  108. struct pci_bus *ixp2000_pci_scan_bus(int nr, struct pci_sys_data *sysdata)
  109. {
  110. return pci_scan_root_bus(NULL, sysdata->busnr, &ixp2000_pci_ops,
  111. sysdata, &sysdata->resources);
  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. pci_set_flags(0);
  163. pcibios_min_io = 0;
  164. pcibios_min_mem = 0;
  165. #ifndef CONFIG_IXP2000_SUPPORT_BROKEN_PCI_IO
  166. /*
  167. * Configure the PCI unit to properly byteswap I/O transactions,
  168. * and verify that it worked.
  169. */
  170. ixp2000_reg_write(IXP2000_PCI_CONTROL,
  171. (*IXP2000_PCI_CONTROL | PCI_CONTROL_IEE));
  172. if ((*IXP2000_PCI_CONTROL & PCI_CONTROL_IEE) == 0)
  173. panic("IXP2000: PCI I/O is broken on this ixp model, and "
  174. "the needed workaround has not been configured in");
  175. #endif
  176. hook_fault_code(16+6, ixp2000_pci_abort_handler, SIGBUS, 0,
  177. "PCI config cycle to non-existent device");
  178. }
  179. /*
  180. * IXP2000 systems often have large resource requirements, so we just
  181. * use our own resource space.
  182. */
  183. static struct resource ixp2000_pci_mem_space = {
  184. .start = 0xe0000000,
  185. .end = 0xffffffff,
  186. .flags = IORESOURCE_MEM,
  187. .name = "PCI Mem Space"
  188. };
  189. static struct resource ixp2000_pci_io_space = {
  190. .start = 0x00010000,
  191. .end = 0x0001ffff,
  192. .flags = IORESOURCE_IO,
  193. .name = "PCI I/O Space"
  194. };
  195. int ixp2000_pci_setup(int nr, struct pci_sys_data *sys)
  196. {
  197. if (nr >= 1)
  198. return 0;
  199. pci_add_resource_offset(&sys->resources,
  200. &ixp2000_pci_io_space, sys->io_offset);
  201. pci_add_resource_offset(&sys->resources,
  202. &ixp2000_pci_mem_space, sys->mem_offset);
  203. return 1;
  204. }