pci-frv.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. /* pci-frv.c: low-level PCI access routines
  2. *
  3. * Copyright (C) 2003-5 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. * - Derived from the i386 equivalent stuff
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version
  10. * 2 of the License, or (at your option) any later version.
  11. */
  12. #include <linux/types.h>
  13. #include <linux/kernel.h>
  14. #include <linux/pci.h>
  15. #include <linux/init.h>
  16. #include <linux/ioport.h>
  17. #include <linux/errno.h>
  18. #include "pci-frv.h"
  19. /*
  20. * We need to avoid collisions with `mirrored' VGA ports
  21. * and other strange ISA hardware, so we always want the
  22. * addresses to be allocated in the 0x000-0x0ff region
  23. * modulo 0x400.
  24. *
  25. * Why? Because some silly external IO cards only decode
  26. * the low 10 bits of the IO address. The 0x00-0xff region
  27. * is reserved for motherboard devices that decode all 16
  28. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  29. * but we want to try to avoid allocating at 0x2900-0x2bff
  30. * which might have be mirrored at 0x0100-0x03ff..
  31. */
  32. void
  33. pcibios_align_resource(void *data, struct resource *res,
  34. resource_size_t size, resource_size_t align)
  35. {
  36. if (res->flags & IORESOURCE_IO) {
  37. resource_size_t start = res->start;
  38. if (start & 0x300) {
  39. start = (start + 0x3ff) & ~0x3ff;
  40. res->start = start;
  41. }
  42. }
  43. }
  44. /*
  45. * Handle resources of PCI devices. If the world were perfect, we could
  46. * just allocate all the resource regions and do nothing more. It isn't.
  47. * On the other hand, we cannot just re-allocate all devices, as it would
  48. * require us to know lots of host bridge internals. So we attempt to
  49. * keep as much of the original configuration as possible, but tweak it
  50. * when it's found to be wrong.
  51. *
  52. * Known BIOS problems we have to work around:
  53. * - I/O or memory regions not configured
  54. * - regions configured, but not enabled in the command register
  55. * - bogus I/O addresses above 64K used
  56. * - expansion ROMs left enabled (this may sound harmless, but given
  57. * the fact the PCI specs explicitly allow address decoders to be
  58. * shared between expansion ROMs and other resource regions, it's
  59. * at least dangerous)
  60. *
  61. * Our solution:
  62. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  63. * This gives us fixed barriers on where we can allocate.
  64. * (2) Allocate resources for all enabled devices. If there is
  65. * a collision, just mark the resource as unallocated. Also
  66. * disable expansion ROMs during this step.
  67. * (3) Try to allocate resources for disabled devices. If the
  68. * resources were assigned correctly, everything goes well,
  69. * if they weren't, they won't disturb allocation of other
  70. * resources.
  71. * (4) Assign new addresses to resources which were either
  72. * not configured at all or misconfigured. If explicitly
  73. * requested by the user, configure expansion ROM address
  74. * as well.
  75. */
  76. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  77. {
  78. struct list_head *ln;
  79. struct pci_bus *bus;
  80. struct pci_dev *dev;
  81. int idx;
  82. struct resource *r;
  83. /* Depth-First Search on bus tree */
  84. for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
  85. bus = pci_bus_b(ln);
  86. if ((dev = bus->self)) {
  87. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  88. r = &dev->resource[idx];
  89. if (!r->start)
  90. continue;
  91. if (pci_claim_resource(dev, idx) < 0)
  92. printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev));
  93. }
  94. }
  95. pcibios_allocate_bus_resources(&bus->children);
  96. }
  97. }
  98. static void __init pcibios_allocate_resources(int pass)
  99. {
  100. struct pci_dev *dev = NULL;
  101. int idx, disabled;
  102. u16 command;
  103. struct resource *r;
  104. for_each_pci_dev(dev) {
  105. pci_read_config_word(dev, PCI_COMMAND, &command);
  106. for(idx = 0; idx < 6; idx++) {
  107. r = &dev->resource[idx];
  108. if (r->parent) /* Already allocated */
  109. continue;
  110. if (!r->start) /* Address not assigned at all */
  111. continue;
  112. if (r->flags & IORESOURCE_IO)
  113. disabled = !(command & PCI_COMMAND_IO);
  114. else
  115. disabled = !(command & PCI_COMMAND_MEMORY);
  116. if (pass == disabled) {
  117. DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
  118. r->start, r->end, r->flags, disabled, pass);
  119. if (pci_claim_resource(dev, idx) < 0) {
  120. printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev));
  121. /* We'll assign a new address later */
  122. r->end -= r->start;
  123. r->start = 0;
  124. }
  125. }
  126. }
  127. if (!pass) {
  128. r = &dev->resource[PCI_ROM_RESOURCE];
  129. if (r->flags & IORESOURCE_ROM_ENABLE) {
  130. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  131. u32 reg;
  132. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  133. r->flags &= ~IORESOURCE_ROM_ENABLE;
  134. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  135. pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
  136. }
  137. }
  138. }
  139. }
  140. static void __init pcibios_assign_resources(void)
  141. {
  142. struct pci_dev *dev = NULL;
  143. int idx;
  144. struct resource *r;
  145. for_each_pci_dev(dev) {
  146. int class = dev->class >> 8;
  147. /* Don't touch classless devices and host bridges */
  148. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  149. continue;
  150. for(idx=0; idx<6; idx++) {
  151. r = &dev->resource[idx];
  152. /*
  153. * Don't touch IDE controllers and I/O ports of video cards!
  154. */
  155. if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
  156. (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
  157. continue;
  158. /*
  159. * We shall assign a new address to this resource, either because
  160. * the BIOS forgot to do so or because we have decided the old
  161. * address was unusable for some reason.
  162. */
  163. if (!r->start && r->end)
  164. pci_assign_resource(dev, idx);
  165. }
  166. if (pci_probe & PCI_ASSIGN_ROMS) {
  167. r = &dev->resource[PCI_ROM_RESOURCE];
  168. r->end -= r->start;
  169. r->start = 0;
  170. if (r->end)
  171. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  172. }
  173. }
  174. }
  175. void __init pcibios_resource_survey(void)
  176. {
  177. DBG("PCI: Allocating resources\n");
  178. pcibios_allocate_bus_resources(&pci_root_buses);
  179. pcibios_allocate_resources(0);
  180. pcibios_allocate_resources(1);
  181. pcibios_assign_resources();
  182. }
  183. /*
  184. * If we set up a device for bus mastering, we need to check the latency
  185. * timer as certain crappy BIOSes forget to set it properly.
  186. */
  187. unsigned int pcibios_max_latency = 255;
  188. void pcibios_set_master(struct pci_dev *dev)
  189. {
  190. u8 lat;
  191. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  192. if (lat < 16)
  193. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  194. else if (lat > pcibios_max_latency)
  195. lat = pcibios_max_latency;
  196. else
  197. return;
  198. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
  199. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  200. }