pci.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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. if (hose->get_busno && pci_probe_only)
  109. next_busno = (*hose->get_busno)();
  110. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  111. hose->bus = bus;
  112. hose->need_domain_info = need_domain_info;
  113. if (bus) {
  114. next_busno = bus->subordinate + 1;
  115. /* Don't allow 8-bit bus number overflow inside the hose -
  116. reserve some space for bridges. */
  117. if (next_busno > 224) {
  118. next_busno = 0;
  119. need_domain_info = 1;
  120. }
  121. }
  122. continue;
  123. out_free_mem_resource:
  124. release_resource(hose->mem_resource);
  125. out:
  126. printk(KERN_WARNING
  127. "Skipping PCI bus scan due to resource conflict\n");
  128. }
  129. if (!pci_probe_only)
  130. pci_assign_unassigned_resources();
  131. pci_fixup_irqs(common_swizzle, pcibios_map_irq);
  132. return 0;
  133. }
  134. subsys_initcall(pcibios_init);
  135. static int pcibios_enable_resources(struct pci_dev *dev, int mask)
  136. {
  137. u16 cmd, old_cmd;
  138. int idx;
  139. struct resource *r;
  140. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  141. old_cmd = cmd;
  142. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  143. /* Only set up the requested stuff */
  144. if (!(mask & (1<<idx)))
  145. continue;
  146. r = &dev->resource[idx];
  147. if (!r->start && r->end) {
  148. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  149. return -EINVAL;
  150. }
  151. if (r->flags & IORESOURCE_IO)
  152. cmd |= PCI_COMMAND_IO;
  153. if (r->flags & IORESOURCE_MEM)
  154. cmd |= PCI_COMMAND_MEMORY;
  155. }
  156. if (dev->resource[PCI_ROM_RESOURCE].start)
  157. cmd |= PCI_COMMAND_MEMORY;
  158. if (cmd != old_cmd) {
  159. printk("PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  160. pci_write_config_word(dev, PCI_COMMAND, cmd);
  161. }
  162. return 0;
  163. }
  164. /*
  165. * If we set up a device for bus mastering, we need to check the latency
  166. * timer as certain crappy BIOSes forget to set it properly.
  167. */
  168. unsigned int pcibios_max_latency = 255;
  169. void pcibios_set_master(struct pci_dev *dev)
  170. {
  171. u8 lat;
  172. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  173. if (lat < 16)
  174. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  175. else if (lat > pcibios_max_latency)
  176. lat = pcibios_max_latency;
  177. else
  178. return;
  179. printk(KERN_DEBUG "PCI: Setting latency timer of device %s to %d\n",
  180. pci_name(dev), lat);
  181. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  182. }
  183. unsigned int pcibios_assign_all_busses(void)
  184. {
  185. return (pci_probe & PCI_ASSIGN_ALL_BUSSES) ? 1 : 0;
  186. }
  187. int pcibios_enable_device(struct pci_dev *dev, int mask)
  188. {
  189. int err;
  190. if ((err = pcibios_enable_resources(dev, mask)) < 0)
  191. return err;
  192. return pcibios_plat_dev_init(dev);
  193. }
  194. static void __init pcibios_fixup_device_resources(struct pci_dev *dev,
  195. struct pci_bus *bus)
  196. {
  197. /* Update device resources. */
  198. struct pci_controller *hose = (struct pci_controller *)bus->sysdata;
  199. unsigned long offset = 0;
  200. int i;
  201. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  202. if (!dev->resource[i].start)
  203. continue;
  204. if (dev->resource[i].flags & IORESOURCE_IO)
  205. offset = hose->io_offset;
  206. else if (dev->resource[i].flags & IORESOURCE_MEM)
  207. offset = hose->mem_offset;
  208. dev->resource[i].start += offset;
  209. dev->resource[i].end += offset;
  210. }
  211. }
  212. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  213. {
  214. /* Propagate hose info into the subordinate devices. */
  215. struct pci_controller *hose = bus->sysdata;
  216. struct list_head *ln;
  217. struct pci_dev *dev = bus->self;
  218. if (!dev) {
  219. bus->resource[0] = hose->io_resource;
  220. bus->resource[1] = hose->mem_resource;
  221. } else if (pci_probe_only &&
  222. (dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  223. pci_read_bridge_bases(bus);
  224. pcibios_fixup_device_resources(dev, bus);
  225. }
  226. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  227. struct pci_dev *dev = pci_dev_b(ln);
  228. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  229. pcibios_fixup_device_resources(dev, bus);
  230. }
  231. }
  232. void __init
  233. pcibios_update_irq(struct pci_dev *dev, int irq)
  234. {
  235. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  236. }
  237. void __devinit
  238. pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  239. struct resource *res)
  240. {
  241. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  242. unsigned long offset = 0;
  243. if (res->flags & IORESOURCE_IO)
  244. offset = hose->io_offset;
  245. else if (res->flags & IORESOURCE_MEM)
  246. offset = hose->mem_offset;
  247. region->start = res->start - offset;
  248. region->end = res->end - offset;
  249. }
  250. void __devinit
  251. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  252. struct pci_bus_region *region)
  253. {
  254. struct pci_controller *hose = (struct pci_controller *)dev->sysdata;
  255. unsigned long offset = 0;
  256. if (res->flags & IORESOURCE_IO)
  257. offset = hose->io_offset;
  258. else if (res->flags & IORESOURCE_MEM)
  259. offset = hose->mem_offset;
  260. res->start = region->start + offset;
  261. res->end = region->end + offset;
  262. }
  263. #ifdef CONFIG_HOTPLUG
  264. EXPORT_SYMBOL(pcibios_resource_to_bus);
  265. EXPORT_SYMBOL(pcibios_bus_to_resource);
  266. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  267. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  268. #endif
  269. char *pcibios_setup(char *str)
  270. {
  271. return str;
  272. }