pci-frv.c 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254
  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, *pr;
  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. pr = pci_find_parent_resource(dev, r);
  92. if (!pr || request_resource(pr, r) < 0)
  93. printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev));
  94. }
  95. }
  96. pcibios_allocate_bus_resources(&bus->children);
  97. }
  98. }
  99. static void __init pcibios_allocate_resources(int pass)
  100. {
  101. struct pci_dev *dev = NULL;
  102. int idx, disabled;
  103. u16 command;
  104. struct resource *r, *pr;
  105. for_each_pci_dev(dev) {
  106. pci_read_config_word(dev, PCI_COMMAND, &command);
  107. for(idx = 0; idx < 6; idx++) {
  108. r = &dev->resource[idx];
  109. if (r->parent) /* Already allocated */
  110. continue;
  111. if (!r->start) /* Address not assigned at all */
  112. continue;
  113. if (r->flags & IORESOURCE_IO)
  114. disabled = !(command & PCI_COMMAND_IO);
  115. else
  116. disabled = !(command & PCI_COMMAND_MEMORY);
  117. if (pass == disabled) {
  118. DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
  119. r->start, r->end, r->flags, disabled, pass);
  120. pr = pci_find_parent_resource(dev, r);
  121. if (!pr || request_resource(pr, r) < 0) {
  122. printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev));
  123. /* We'll assign a new address later */
  124. r->end -= r->start;
  125. r->start = 0;
  126. }
  127. }
  128. }
  129. if (!pass) {
  130. r = &dev->resource[PCI_ROM_RESOURCE];
  131. if (r->flags & IORESOURCE_ROM_ENABLE) {
  132. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  133. u32 reg;
  134. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  135. r->flags &= ~IORESOURCE_ROM_ENABLE;
  136. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  137. pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
  138. }
  139. }
  140. }
  141. }
  142. static void __init pcibios_assign_resources(void)
  143. {
  144. struct pci_dev *dev = NULL;
  145. int idx;
  146. struct resource *r;
  147. for_each_pci_dev(dev) {
  148. int class = dev->class >> 8;
  149. /* Don't touch classless devices and host bridges */
  150. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  151. continue;
  152. for(idx=0; idx<6; idx++) {
  153. r = &dev->resource[idx];
  154. /*
  155. * Don't touch IDE controllers and I/O ports of video cards!
  156. */
  157. if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
  158. (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
  159. continue;
  160. /*
  161. * We shall assign a new address to this resource, either because
  162. * the BIOS forgot to do so or because we have decided the old
  163. * address was unusable for some reason.
  164. */
  165. if (!r->start && r->end)
  166. pci_assign_resource(dev, idx);
  167. }
  168. if (pci_probe & PCI_ASSIGN_ROMS) {
  169. r = &dev->resource[PCI_ROM_RESOURCE];
  170. r->end -= r->start;
  171. r->start = 0;
  172. if (r->end)
  173. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  174. }
  175. }
  176. }
  177. void __init pcibios_resource_survey(void)
  178. {
  179. DBG("PCI: Allocating resources\n");
  180. pcibios_allocate_bus_resources(&pci_root_buses);
  181. pcibios_allocate_resources(0);
  182. pcibios_allocate_resources(1);
  183. pcibios_assign_resources();
  184. }
  185. int pcibios_enable_resources(struct pci_dev *dev, int mask)
  186. {
  187. u16 cmd, old_cmd;
  188. int idx;
  189. struct resource *r;
  190. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  191. old_cmd = cmd;
  192. for(idx=0; idx<6; idx++) {
  193. /* Only set up the requested stuff */
  194. if (!(mask & (1<<idx)))
  195. continue;
  196. r = &dev->resource[idx];
  197. if (!r->start && r->end) {
  198. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  199. return -EINVAL;
  200. }
  201. if (r->flags & IORESOURCE_IO)
  202. cmd |= PCI_COMMAND_IO;
  203. if (r->flags & IORESOURCE_MEM)
  204. cmd |= PCI_COMMAND_MEMORY;
  205. }
  206. if (dev->resource[PCI_ROM_RESOURCE].start)
  207. cmd |= PCI_COMMAND_MEMORY;
  208. if (cmd != old_cmd) {
  209. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  210. pci_write_config_word(dev, PCI_COMMAND, cmd);
  211. }
  212. return 0;
  213. }
  214. /*
  215. * If we set up a device for bus mastering, we need to check the latency
  216. * timer as certain crappy BIOSes forget to set it properly.
  217. */
  218. unsigned int pcibios_max_latency = 255;
  219. void pcibios_set_master(struct pci_dev *dev)
  220. {
  221. u8 lat;
  222. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  223. if (lat < 16)
  224. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  225. else if (lat > pcibios_max_latency)
  226. lat = pcibios_max_latency;
  227. else
  228. return;
  229. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
  230. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  231. }