pci.c 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * This program is free software; you can redistribute it and/or modify it
  3. * under the terms of the GNU General Public License as published by the
  4. * Free Software Foundation; either version 2 of the License, or (at your
  5. * option) any later version.
  6. *
  7. * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
  8. */
  9. #include <linux/config.h>
  10. #include <linux/kernel.h>
  11. #include <linux/mm.h>
  12. #include <linux/bootmem.h>
  13. #include <linux/init.h>
  14. #include <linux/types.h>
  15. #include <linux/pci.h>
  16. /*
  17. * Indicate whether we respect the PCI setup left by the firmware.
  18. *
  19. * Make this long-lived so that we know when shutting down
  20. * whether we probed only or not.
  21. */
  22. int pci_probe_only;
  23. #define PCI_ASSIGN_ALL_BUSSES 1
  24. unsigned int pci_probe = PCI_ASSIGN_ALL_BUSSES;
  25. /*
  26. * The PCI controller list.
  27. */
  28. struct pci_controller *hose_head, **hose_tail = &hose_head;
  29. struct pci_controller *pci_isa_hose;
  30. unsigned long PCIBIOS_MIN_IO = 0x0000;
  31. unsigned long PCIBIOS_MIN_MEM = 0;
  32. /*
  33. * We need to avoid collisions with `mirrored' VGA ports
  34. * and other strange ISA hardware, so we always want the
  35. * addresses to be allocated in the 0x000-0x0ff region
  36. * modulo 0x400.
  37. *
  38. * Why? Because some silly external IO cards only decode
  39. * the low 10 bits of the IO address. The 0x00-0xff region
  40. * is reserved for motherboard devices that decode all 16
  41. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  42. * but we want to try to avoid allocating at 0x2900-0x2bff
  43. * which might have be mirrored at 0x0100-0x03ff..
  44. */
  45. void
  46. pcibios_align_resource(void *data, struct resource *res,
  47. unsigned long size, unsigned long align)
  48. {
  49. struct pci_dev *dev = data;
  50. struct pci_controller *hose = dev->sysdata;
  51. unsigned long start = res->start;
  52. if (res->flags & IORESOURCE_IO) {
  53. /* Make sure we start at our min on all hoses */
  54. if (start < PCIBIOS_MIN_IO + hose->io_resource->start)
  55. start = PCIBIOS_MIN_IO + hose->io_resource->start;
  56. /*
  57. * Put everything into 0x00-0xff region modulo 0x400
  58. */
  59. if (start & 0x300)
  60. start = (start + 0x3ff) & ~0x3ff;
  61. } else if (res->flags & IORESOURCE_MEM) {
  62. /* Make sure we start at our min on all hoses */
  63. if (start < PCIBIOS_MIN_MEM + hose->mem_resource->start)
  64. start = PCIBIOS_MIN_MEM + hose->mem_resource->start;
  65. }
  66. res->start = start;
  67. }
  68. struct pci_controller * __init alloc_pci_controller(void)
  69. {
  70. return alloc_bootmem(sizeof(struct pci_controller));
  71. }
  72. void __init register_pci_controller(struct pci_controller *hose)
  73. {
  74. *hose_tail = hose;
  75. hose_tail = &hose->next;
  76. }
  77. /* Most MIPS systems have straight-forward swizzling needs. */
  78. static inline u8 bridge_swizzle(u8 pin, u8 slot)
  79. {
  80. return (((pin - 1) + slot) % 4) + 1;
  81. }
  82. static u8 __init common_swizzle(struct pci_dev *dev, u8 *pinp)
  83. {
  84. u8 pin = *pinp;
  85. while (dev->bus->parent) {
  86. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  87. /* Move up the chain of bridges. */
  88. dev = dev->bus->self;
  89. }
  90. *pinp = pin;
  91. /* The slot is the slot of the last bridge. */
  92. return PCI_SLOT(dev->devfn);
  93. }
  94. static int __init pcibios_init(void)
  95. {
  96. struct pci_controller *hose;
  97. struct pci_bus *bus;
  98. int next_busno;
  99. int need_domain_info = 0;
  100. /* Scan all of the recorded PCI controllers. */
  101. for (next_busno = 0, hose = hose_head; hose; hose = hose->next) {
  102. if (request_resource(&iomem_resource, hose->mem_resource) < 0)
  103. goto out;
  104. if (request_resource(&ioport_resource, hose->io_resource) < 0)
  105. goto out_free_mem_resource;
  106. if (!hose->iommu)
  107. PCI_DMA_BUS_IS_PHYS = 1;
  108. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  109. hose->bus = bus;
  110. hose->need_domain_info = need_domain_info;
  111. next_busno = bus->subordinate + 1;
  112. /* Don't allow 8-bit bus number overflow inside the hose -
  113. reserve some space for bridges. */
  114. if (next_busno > 224) {
  115. next_busno = 0;
  116. need_domain_info = 1;
  117. }
  118. continue;
  119. out_free_mem_resource:
  120. release_resource(hose->mem_resource);
  121. out:
  122. printk(KERN_WARNING
  123. "Skipping PCI bus scan due to resource conflict\n");
  124. }
  125. if (!pci_probe_only)
  126. pci_assign_unassigned_resources();
  127. pci_fixup_irqs(common_swizzle, pcibios_map_irq);
  128. return 0;
  129. }
  130. subsys_initcall(pcibios_init);
  131. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  132. {
  133. u16 cmd, old_cmd;
  134. int idx;
  135. struct resource *r;
  136. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  137. old_cmd = cmd;
  138. for(idx=0; idx<6; idx++) {
  139. /* Only set up the requested stuff */
  140. if (!(mask & (1<<idx)))
  141. continue;
  142. r = &dev->resource[idx];
  143. if (!r->start && r->end) {
  144. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  145. return -EINVAL;
  146. }
  147. if (r->flags & IORESOURCE_IO)
  148. cmd |= PCI_COMMAND_IO;
  149. if (r->flags & IORESOURCE_MEM)
  150. cmd |= PCI_COMMAND_MEMORY;
  151. }
  152. if (dev->resource[PCI_ROM_RESOURCE].start)
  153. cmd |= PCI_COMMAND_MEMORY;
  154. if (cmd != old_cmd) {
  155. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  156. pci_write_config_word(dev, PCI_COMMAND, cmd);
  157. }
  158. return 0;
  159. }
  160. /*
  161. * If we set up a device for bus mastering, we need to check the latency
  162. * timer as certain crappy BIOSes forget to set it properly.
  163. */
  164. unsigned int pcibios_max_latency = 255;
  165. void pcibios_set_master(struct pci_dev *dev)
  166. {
  167. u8 lat;
  168. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  169. if (lat < 16)
  170. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  171. else if (lat > pcibios_max_latency)
  172. lat = pcibios_max_latency;
  173. else
  174. return;
  175. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
  176. pci_name(dev), lat);
  177. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  178. }
  179. unsigned int pcibios_assign_all_busses(void)
  180. {
  181. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  182. }
  183. int pcibios_enable_device(struct pci_dev *dev, int mask)
  184. {
  185. int err;
  186. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  187. return err;
  188. return pcibios_plat_dev_init(dev);
  189. }
  190. static void __init pcibios_fixup_device_resources(struct pci_dev *dev,
  191. struct pci_bus *bus)
  192. {
  193. /* Update device resources. */
  194. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  195. unsigned long offset = 0;
  196. int i;
  197. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  198. if (!dev->resource[i].start)
  199. continue;
  200. if (dev->resource[i].flags & IORESOURCE_IO)
  201. offset = hose->io_offset;
  202. else if (dev->resource[i].flags & IORESOURCE_MEM)
  203. offset = hose->mem_offset;
  204. dev->resource[i].start += offset;
  205. dev->resource[i].end += offset;
  206. }
  207. }
  208. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  209. {
  210. /* Propagate hose info into the subordinate devices. */
  211. struct pci_controller *hose = bus->sysdata;
  212. struct list_head *ln;
  213. struct pci_dev *dev = bus->self;
  214. if (!dev) {
  215. bus->resource[0] = hose->io_resource;
  216. bus->resource[1] = hose->mem_resource;
  217. } else if (pci_probe_only &&
  218. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  219. pci_read_bridge_bases(bus);
  220. pcibios_fixup_device_resources(dev, bus);
  221. }
  222. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  223. struct pci_dev *dev = pci_dev_b(ln);
  224. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  225. pcibios_fixup_device_resources(dev, bus);
  226. }
  227. }
  228. void __init
  229. pcibios_update_irq(struct pci_dev *dev, int irq)
  230. {
  231. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  232. }
  233. void __devinit
  234. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  235. struct resource *res)
  236. {
  237. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  238. unsigned long offset = 0;
  239. if (res->flags & IORESOURCE_IO)
  240. offset = hose->io_offset;
  241. else if (res->flags & IORESOURCE_MEM)
  242. offset = hose->mem_offset;
  243. region->start = res->start - offset;
  244. region->end = res->end - offset;
  245. }
  246. void __devinit
  247. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  248. struct pci_bus_region *region)
  249. {
  250. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  251. unsigned long offset = 0;
  252. if (res->flags & IORESOURCE_IO)
  253. offset = hose->io_offset;
  254. else if (res->flags & IORESOURCE_MEM)
  255. offset = hose->mem_offset;
  256. res->start = region->start + offset;
  257. res->end = region->end + offset;
  258. }
  259. #ifdef CONFIG_HOTPLUG
  260. EXPORT_SYMBOL(pcibios_resource_to_bus);
  261. EXPORT_SYMBOL(pcibios_bus_to_resource);
  262. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  263. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  264. #endif
  265. char *pcibios_setup(char *str)
  266. {
  267. return str;
  268. }