pci-asb2305.c 6.8 KB

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