pci-frv.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  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. #if 0
  20. void
  21. pcibios_update_resource(struct pci_dev *dev, struct resource *root,
  22. struct resource *res, int resource)
  23. {
  24. u32 new, check;
  25. int reg;
  26. new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
  27. if (resource < 6) {
  28. reg = PCI_BASE_ADDRESS_0 + 4*resource;
  29. } else if (resource == PCI_ROM_RESOURCE) {
  30. res->flags |= IORESOURCE_ROM_ENABLE;
  31. new |= PCI_ROM_ADDRESS_ENABLE;
  32. reg = dev->rom_base_reg;
  33. } else {
  34. /* Somebody might have asked allocation of a non-standard resource */
  35. return;
  36. }
  37. pci_write_config_dword(dev, reg, new);
  38. pci_read_config_dword(dev, reg, &check);
  39. if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
  40. printk(KERN_ERR "PCI: Error while updating region "
  41. "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
  42. new, check);
  43. }
  44. }
  45. #endif
  46. /*
  47. * We need to avoid collisions with `mirrored' VGA ports
  48. * and other strange ISA hardware, so we always want the
  49. * addresses to be allocated in the 0x000-0x0ff region
  50. * modulo 0x400.
  51. *
  52. * Why? Because some silly external IO cards only decode
  53. * the low 10 bits of the IO address. The 0x00-0xff region
  54. * is reserved for motherboard devices that decode all 16
  55. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  56. * but we want to try to avoid allocating at 0x2900-0x2bff
  57. * which might have be mirrored at 0x0100-0x03ff..
  58. */
  59. void
  60. pcibios_align_resource(void *data, struct resource *res,
  61. resource_size_t size, resource_size_t align)
  62. {
  63. if (res->flags & IORESOURCE_IO) {
  64. resource_size_t start = res->start;
  65. if (start & 0x300) {
  66. start = (start + 0x3ff) & ~0x3ff;
  67. res->start = start;
  68. }
  69. }
  70. }
  71. /*
  72. * Handle resources of PCI devices. If the world were perfect, we could
  73. * just allocate all the resource regions and do nothing more. It isn't.
  74. * On the other hand, we cannot just re-allocate all devices, as it would
  75. * require us to know lots of host bridge internals. So we attempt to
  76. * keep as much of the original configuration as possible, but tweak it
  77. * when it's found to be wrong.
  78. *
  79. * Known BIOS problems we have to work around:
  80. * - I/O or memory regions not configured
  81. * - regions configured, but not enabled in the command register
  82. * - bogus I/O addresses above 64K used
  83. * - expansion ROMs left enabled (this may sound harmless, but given
  84. * the fact the PCI specs explicitly allow address decoders to be
  85. * shared between expansion ROMs and other resource regions, it's
  86. * at least dangerous)
  87. *
  88. * Our solution:
  89. * (1) Allocate resources for all buses behind PCI-to-PCI bridges.
  90. * This gives us fixed barriers on where we can allocate.
  91. * (2) Allocate resources for all enabled devices. If there is
  92. * a collision, just mark the resource as unallocated. Also
  93. * disable expansion ROMs during this step.
  94. * (3) Try to allocate resources for disabled devices. If the
  95. * resources were assigned correctly, everything goes well,
  96. * if they weren't, they won't disturb allocation of other
  97. * resources.
  98. * (4) Assign new addresses to resources which were either
  99. * not configured at all or misconfigured. If explicitly
  100. * requested by the user, configure expansion ROM address
  101. * as well.
  102. */
  103. static void __init pcibios_allocate_bus_resources(struct list_head *bus_list)
  104. {
  105. struct list_head *ln;
  106. struct pci_bus *bus;
  107. struct pci_dev *dev;
  108. int idx;
  109. struct resource *r, *pr;
  110. /* Depth-First Search on bus tree */
  111. for (ln=bus_list->next; ln != bus_list; ln=ln->next) {
  112. bus = pci_bus_b(ln);
  113. if ((dev = bus->self)) {
  114. for (idx = PCI_BRIDGE_RESOURCES; idx < PCI_NUM_RESOURCES; idx++) {
  115. r = &dev->resource[idx];
  116. if (!r->start)
  117. continue;
  118. pr = pci_find_parent_resource(dev, r);
  119. if (!pr || request_resource(pr, r) < 0)
  120. printk(KERN_ERR "PCI: Cannot allocate resource region %d of bridge %s\n", idx, pci_name(dev));
  121. }
  122. }
  123. pcibios_allocate_bus_resources(&bus->children);
  124. }
  125. }
  126. static void __init pcibios_allocate_resources(int pass)
  127. {
  128. struct pci_dev *dev = NULL;
  129. int idx, disabled;
  130. u16 command;
  131. struct resource *r, *pr;
  132. for_each_pci_dev(dev) {
  133. pci_read_config_word(dev, PCI_COMMAND, &command);
  134. for(idx = 0; idx < 6; idx++) {
  135. r = &dev->resource[idx];
  136. if (r->parent) /* Already allocated */
  137. continue;
  138. if (!r->start) /* Address not assigned at all */
  139. continue;
  140. if (r->flags & IORESOURCE_IO)
  141. disabled = !(command & PCI_COMMAND_IO);
  142. else
  143. disabled = !(command & PCI_COMMAND_MEMORY);
  144. if (pass == disabled) {
  145. DBG("PCI: Resource %08lx-%08lx (f=%lx, d=%d, p=%d)\n",
  146. r->start, r->end, r->flags, disabled, pass);
  147. pr = pci_find_parent_resource(dev, r);
  148. if (!pr || request_resource(pr, r) < 0) {
  149. printk(KERN_ERR "PCI: Cannot allocate resource region %d of device %s\n", idx, pci_name(dev));
  150. /* We'll assign a new address later */
  151. r->end -= r->start;
  152. r->start = 0;
  153. }
  154. }
  155. }
  156. if (!pass) {
  157. r = &dev->resource[PCI_ROM_RESOURCE];
  158. if (r->flags & IORESOURCE_ROM_ENABLE) {
  159. /* Turn the ROM off, leave the resource region, but keep it unregistered. */
  160. u32 reg;
  161. DBG("PCI: Switching off ROM of %s\n", pci_name(dev));
  162. r->flags &= ~IORESOURCE_ROM_ENABLE;
  163. pci_read_config_dword(dev, dev->rom_base_reg, &reg);
  164. pci_write_config_dword(dev, dev->rom_base_reg, reg & ~PCI_ROM_ADDRESS_ENABLE);
  165. }
  166. }
  167. }
  168. }
  169. static void __init pcibios_assign_resources(void)
  170. {
  171. struct pci_dev *dev = NULL;
  172. int idx;
  173. struct resource *r;
  174. for_each_pci_dev(dev) {
  175. int class = dev->class >> 8;
  176. /* Don't touch classless devices and host bridges */
  177. if (!class || class == PCI_CLASS_BRIDGE_HOST)
  178. continue;
  179. for(idx=0; idx<6; idx++) {
  180. r = &dev->resource[idx];
  181. /*
  182. * Don't touch IDE controllers and I/O ports of video cards!
  183. */
  184. if ((class == PCI_CLASS_STORAGE_IDE && idx < 4) ||
  185. (class == PCI_CLASS_DISPLAY_VGA && (r->flags & IORESOURCE_IO)))
  186. continue;
  187. /*
  188. * We shall assign a new address to this resource, either because
  189. * the BIOS forgot to do so or because we have decided the old
  190. * address was unusable for some reason.
  191. */
  192. if (!r->start && r->end)
  193. pci_assign_resource(dev, idx);
  194. }
  195. if (pci_probe & PCI_ASSIGN_ROMS) {
  196. r = &dev->resource[PCI_ROM_RESOURCE];
  197. r->end -= r->start;
  198. r->start = 0;
  199. if (r->end)
  200. pci_assign_resource(dev, PCI_ROM_RESOURCE);
  201. }
  202. }
  203. }
  204. void __init pcibios_resource_survey(void)
  205. {
  206. DBG("PCI: Allocating resources\n");
  207. pcibios_allocate_bus_resources(&pci_root_buses);
  208. pcibios_allocate_resources(0);
  209. pcibios_allocate_resources(1);
  210. pcibios_assign_resources();
  211. }
  212. int pcibios_enable_resources(struct pci_dev *dev, int mask)
  213. {
  214. u16 cmd, old_cmd;
  215. int idx;
  216. struct resource *r;
  217. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  218. old_cmd = cmd;
  219. for(idx=0; idx<6; idx++) {
  220. /* Only set up the requested stuff */
  221. if (!(mask & (1<<idx)))
  222. continue;
  223. r = &dev->resource[idx];
  224. if (!r->start && r->end) {
  225. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  226. return -EINVAL;
  227. }
  228. if (r->flags & IORESOURCE_IO)
  229. cmd |= PCI_COMMAND_IO;
  230. if (r->flags & IORESOURCE_MEM)
  231. cmd |= PCI_COMMAND_MEMORY;
  232. }
  233. if (dev->resource[PCI_ROM_RESOURCE].start)
  234. cmd |= PCI_COMMAND_MEMORY;
  235. if (cmd != old_cmd) {
  236. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  237. pci_write_config_word(dev, PCI_COMMAND, cmd);
  238. }
  239. return 0;
  240. }
  241. /*
  242. * If we set up a device for bus mastering, we need to check the latency
  243. * timer as certain crappy BIOSes forget to set it properly.
  244. */
  245. unsigned int pcibios_max_latency = 255;
  246. void pcibios_set_master(struct pci_dev *dev)
  247. {
  248. u8 lat;
  249. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  250. if (lat < 16)
  251. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  252. else if (lat > pcibios_max_latency)
  253. lat = pcibios_max_latency;
  254. else
  255. return;
  256. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
  257. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  258. }