pci.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_root_bus(NULL, sysdata->busnr, &ixp2000_pci_ops,
  112. sysdata, &sysdata->resources);
  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. pci_set_flags(0);
  164. pcibios_min_io = 0;
  165. pcibios_min_mem = 0;
  166. #ifndef CONFIG_IXP2000_SUPPORT_BROKEN_PCI_IO
  167. /*
  168. * Configure the PCI unit to properly byteswap I/O transactions,
  169. * and verify that it worked.
  170. */
  171. ixp2000_reg_write(IXP2000_PCI_CONTROL,
  172. (*IXP2000_PCI_CONTROL | PCI_CONTROL_IEE));
  173. if ((*IXP2000_PCI_CONTROL & PCI_CONTROL_IEE) == 0)
  174. panic("IXP2000: PCI I/O is broken on this ixp model, and "
  175. "the needed workaround has not been configured in");
  176. #endif
  177. hook_fault_code(16+6, ixp2000_pci_abort_handler, SIGBUS, 0,
  178. "PCI config cycle to non-existent device");
  179. }
  180. /*
  181. * IXP2000 systems often have large resource requirements, so we just
  182. * use our own resource space.
  183. */
  184. static struct resource ixp2000_pci_mem_space = {
  185. .start = 0xe0000000,
  186. .end = 0xffffffff,
  187. .flags = IORESOURCE_MEM,
  188. .name = "PCI Mem Space"
  189. };
  190. static struct resource ixp2000_pci_io_space = {
  191. .start = 0x00010000,
  192. .end = 0x0001ffff,
  193. .flags = IORESOURCE_IO,
  194. .name = "PCI I/O Space"
  195. };
  196. int ixp2000_pci_setup(int nr, struct pci_sys_data *sys)
  197. {
  198. if (nr >= 1)
  199. return 0;
  200. pci_add_resource_offset(&sys->resources,
  201. &ixp2000_pci_io_space, sys->io_offset);
  202. pci_add_resource_offset(&sys->resources,
  203. &ixp2000_pci_mem_space, sys->mem_offset);
  204. return 1;
  205. }