pci-new.c 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. * New-style PCI core.
  3. *
  4. * Copyright (c) 2002 M. R. Brown
  5. * Copyright (c) 2004 - 2009 Paul Mundt
  6. *
  7. * This file is subject to the terms and conditions of the GNU General Public
  8. * License. See the file "COPYING" in the main directory of this archive
  9. * for more details.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/pci.h>
  13. #include <linux/init.h>
  14. #include <linux/dma-debug.h>
  15. #include <linux/io.h>
  16. #include <linux/mutex.h>
  17. /*
  18. * The PCI controller list.
  19. */
  20. static struct pci_channel *hose_head, **hose_tail = &hose_head;
  21. static int pci_initialized;
  22. static void __devinit pcibios_scanbus(struct pci_channel *hose)
  23. {
  24. static int next_busno;
  25. struct pci_bus *bus;
  26. /* Catch botched conversion attempts */
  27. BUG_ON(hose->init);
  28. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  29. if (bus) {
  30. next_busno = bus->subordinate + 1;
  31. /* Don't allow 8-bit bus number overflow inside the hose -
  32. reserve some space for bridges. */
  33. if (next_busno > 224)
  34. next_busno = 0;
  35. pci_bus_size_bridges(bus);
  36. pci_bus_assign_resources(bus);
  37. pci_enable_bridges(bus);
  38. }
  39. }
  40. static DEFINE_MUTEX(pci_scan_mutex);
  41. void __devinit register_pci_controller(struct pci_channel *hose)
  42. {
  43. if (request_resource(&iomem_resource, hose->mem_resource) < 0)
  44. goto out;
  45. if (request_resource(&ioport_resource, hose->io_resource) < 0) {
  46. release_resource(hose->mem_resource);
  47. goto out;
  48. }
  49. *hose_tail = hose;
  50. hose_tail = &hose->next;
  51. /*
  52. * Do not panic here but later - this might hapen before console init.
  53. */
  54. if (!hose->io_map_base) {
  55. printk(KERN_WARNING
  56. "registering PCI controller with io_map_base unset\n");
  57. }
  58. /*
  59. * Scan the bus if it is register after the PCI subsystem
  60. * initialization.
  61. */
  62. if (pci_initialized) {
  63. mutex_lock(&pci_scan_mutex);
  64. pcibios_scanbus(hose);
  65. mutex_unlock(&pci_scan_mutex);
  66. }
  67. return;
  68. out:
  69. printk(KERN_WARNING
  70. "Skipping PCI bus scan due to resource conflict\n");
  71. }
  72. static int __init pcibios_init(void)
  73. {
  74. struct pci_channel *hose;
  75. /* Scan all of the recorded PCI controllers. */
  76. for (hose = hose_head; hose; hose = hose->next)
  77. pcibios_scanbus(hose);
  78. pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
  79. dma_debug_add_bus(&pci_bus_type);
  80. pci_initialized = 1;
  81. return 0;
  82. }
  83. subsys_initcall(pcibios_init);
  84. static void pcibios_fixup_device_resources(struct pci_dev *dev,
  85. struct pci_bus *bus)
  86. {
  87. /* Update device resources. */
  88. struct pci_channel *hose = bus->sysdata;
  89. unsigned long offset = 0;
  90. int i;
  91. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  92. if (!dev->resource[i].start)
  93. continue;
  94. if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
  95. continue;
  96. if (dev->resource[i].flags & IORESOURCE_IO)
  97. offset = hose->io_offset;
  98. else if (dev->resource[i].flags & IORESOURCE_MEM)
  99. offset = hose->mem_offset;
  100. dev->resource[i].start += offset;
  101. dev->resource[i].end += offset;
  102. }
  103. }
  104. /*
  105. * Called after each bus is probed, but before its children
  106. * are examined.
  107. */
  108. void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
  109. {
  110. struct pci_dev *dev = bus->self;
  111. struct list_head *ln;
  112. struct pci_channel *chan = bus->sysdata;
  113. if (!dev) {
  114. bus->resource[0] = chan->io_resource;
  115. bus->resource[1] = chan->mem_resource;
  116. }
  117. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  118. dev = pci_dev_b(ln);
  119. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  120. pcibios_fixup_device_resources(dev, bus);
  121. }
  122. }
  123. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  124. struct resource *res)
  125. {
  126. struct pci_channel *hose = dev->sysdata;
  127. unsigned long offset = 0;
  128. if (res->flags & IORESOURCE_IO)
  129. offset = hose->io_offset;
  130. else if (res->flags & IORESOURCE_MEM)
  131. offset = hose->mem_offset;
  132. region->start = res->start - offset;
  133. region->end = res->end - offset;
  134. }
  135. void __devinit
  136. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  137. struct pci_bus_region *region)
  138. {
  139. struct pci_channel *hose = dev->sysdata;
  140. unsigned long offset = 0;
  141. if (res->flags & IORESOURCE_IO)
  142. offset = hose->io_offset;
  143. else if (res->flags & IORESOURCE_MEM)
  144. offset = hose->mem_offset;
  145. res->start = region->start + offset;
  146. res->end = region->end + offset;
  147. }
  148. int pcibios_enable_device(struct pci_dev *dev, int mask)
  149. {
  150. u16 cmd, old_cmd;
  151. int idx;
  152. struct resource *r;
  153. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  154. old_cmd = cmd;
  155. for(idx=0; idx<6; idx++) {
  156. if (!(mask & (1 << idx)))
  157. continue;
  158. r = &dev->resource[idx];
  159. if (!r->start && r->end) {
  160. printk(KERN_ERR "PCI: Device %s not available because "
  161. "of resource collisions\n", pci_name(dev));
  162. return -EINVAL;
  163. }
  164. if (r->flags & IORESOURCE_IO)
  165. cmd |= PCI_COMMAND_IO;
  166. if (r->flags & IORESOURCE_MEM)
  167. cmd |= PCI_COMMAND_MEMORY;
  168. }
  169. if (dev->resource[PCI_ROM_RESOURCE].start)
  170. cmd |= PCI_COMMAND_MEMORY;
  171. if (cmd != old_cmd) {
  172. printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
  173. pci_name(dev), old_cmd, cmd);
  174. pci_write_config_word(dev, PCI_COMMAND, cmd);
  175. }
  176. return 0;
  177. }
  178. /*
  179. * If we set up a device for bus mastering, we need to check and set
  180. * the latency timer as it may not be properly set.
  181. */
  182. static unsigned int pcibios_max_latency = 255;
  183. void pcibios_set_master(struct pci_dev *dev)
  184. {
  185. u8 lat;
  186. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  187. if (lat < 16)
  188. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  189. else if (lat > pcibios_max_latency)
  190. lat = pcibios_max_latency;
  191. else
  192. return;
  193. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  194. pci_name(dev), lat);
  195. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  196. }
  197. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  198. {
  199. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  200. }