pci.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. /*
  2. * New-style PCI core.
  3. *
  4. * Copyright (c) 2004 - 2009 Paul Mundt
  5. * Copyright (c) 2002 M. R. Brown
  6. *
  7. * Modelled after arch/mips/pci/pci.c:
  8. * Copyright (C) 2003, 04 Ralf Baechle (ralf@linux-mips.org)
  9. *
  10. * This file is subject to the terms and conditions of the GNU General Public
  11. * License. See the file "COPYING" in the main directory of this archive
  12. * for more details.
  13. */
  14. #include <linux/kernel.h>
  15. #include <linux/mm.h>
  16. #include <linux/pci.h>
  17. #include <linux/init.h>
  18. #include <linux/types.h>
  19. #include <linux/dma-debug.h>
  20. #include <linux/io.h>
  21. #include <linux/mutex.h>
  22. unsigned long PCIBIOS_MIN_IO = 0x0000;
  23. unsigned long PCIBIOS_MIN_MEM = 0;
  24. /*
  25. * The PCI controller list.
  26. */
  27. static struct pci_channel *hose_head, **hose_tail = &hose_head;
  28. static int pci_initialized;
  29. static void __devinit pcibios_scanbus(struct pci_channel *hose)
  30. {
  31. static int next_busno;
  32. struct pci_bus *bus;
  33. bus = pci_scan_bus(next_busno, hose->pci_ops, hose);
  34. if (bus) {
  35. next_busno = bus->subordinate + 1;
  36. /* Don't allow 8-bit bus number overflow inside the hose -
  37. reserve some space for bridges. */
  38. if (next_busno > 224)
  39. next_busno = 0;
  40. pci_bus_size_bridges(bus);
  41. pci_bus_assign_resources(bus);
  42. pci_enable_bridges(bus);
  43. }
  44. }
  45. static DEFINE_MUTEX(pci_scan_mutex);
  46. void __devinit register_pci_controller(struct pci_channel *hose)
  47. {
  48. request_resource(&iomem_resource, hose->mem_resource);
  49. request_resource(&ioport_resource, hose->io_resource);
  50. *hose_tail = hose;
  51. hose_tail = &hose->next;
  52. /*
  53. * Do not panic here but later - this might hapen before console init.
  54. */
  55. if (!hose->io_map_base) {
  56. printk(KERN_WARNING
  57. "registering PCI controller with io_map_base unset\n");
  58. }
  59. /*
  60. * Scan the bus if it is register after the PCI subsystem
  61. * initialization.
  62. */
  63. if (pci_initialized) {
  64. mutex_lock(&pci_scan_mutex);
  65. pcibios_scanbus(hose);
  66. mutex_unlock(&pci_scan_mutex);
  67. }
  68. }
  69. static int __init pcibios_init(void)
  70. {
  71. struct pci_channel *hose;
  72. /* Scan all of the recorded PCI controllers. */
  73. for (hose = hose_head; hose; hose = hose->next)
  74. pcibios_scanbus(hose);
  75. pci_fixup_irqs(pci_common_swizzle, pcibios_map_platform_irq);
  76. dma_debug_add_bus(&pci_bus_type);
  77. pci_initialized = 1;
  78. return 0;
  79. }
  80. subsys_initcall(pcibios_init);
  81. static void pcibios_fixup_device_resources(struct pci_dev *dev,
  82. struct pci_bus *bus)
  83. {
  84. /* Update device resources. */
  85. struct pci_channel *hose = bus->sysdata;
  86. unsigned long offset = 0;
  87. int i;
  88. for (i = 0; i < PCI_NUM_RESOURCES; i++) {
  89. if (!dev->resource[i].start)
  90. continue;
  91. if (dev->resource[i].flags & IORESOURCE_PCI_FIXED)
  92. continue;
  93. if (dev->resource[i].flags & IORESOURCE_IO)
  94. offset = hose->io_offset;
  95. else if (dev->resource[i].flags & IORESOURCE_MEM)
  96. offset = hose->mem_offset;
  97. dev->resource[i].start += offset;
  98. dev->resource[i].end += offset;
  99. }
  100. }
  101. /*
  102. * Called after each bus is probed, but before its children
  103. * are examined.
  104. */
  105. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  106. {
  107. struct pci_dev *dev = bus->self;
  108. struct list_head *ln;
  109. struct pci_channel *chan = bus->sysdata;
  110. if (!dev) {
  111. bus->resource[0] = chan->io_resource;
  112. bus->resource[1] = chan->mem_resource;
  113. }
  114. for (ln = bus->devices.next; ln != &bus->devices; ln = ln->next) {
  115. dev = pci_dev_b(ln);
  116. if ((dev->class >> 8) != PCI_CLASS_BRIDGE_PCI)
  117. pcibios_fixup_device_resources(dev, bus);
  118. }
  119. }
  120. /*
  121. * We need to avoid collisions with `mirrored' VGA ports
  122. * and other strange ISA hardware, so we always want the
  123. * addresses to be allocated in the 0x000-0x0ff region
  124. * modulo 0x400.
  125. */
  126. void pcibios_align_resource(void *data, struct resource *res,
  127. resource_size_t size, resource_size_t align)
  128. {
  129. struct pci_dev *dev = data;
  130. struct pci_channel *chan = dev->sysdata;
  131. resource_size_t start = res->start;
  132. if (res->flags & IORESOURCE_IO) {
  133. if (start < PCIBIOS_MIN_IO + chan->io_resource->start)
  134. start = PCIBIOS_MIN_IO + chan->io_resource->start;
  135. /*
  136. * Put everything into 0x00-0xff region modulo 0x400.
  137. */
  138. if (start & 0x300) {
  139. start = (start + 0x3ff) & ~0x3ff;
  140. res->start = start;
  141. }
  142. } else if (res->flags & IORESOURCE_MEM) {
  143. if (start < PCIBIOS_MIN_MEM + chan->mem_resource->start)
  144. start = PCIBIOS_MIN_MEM + chan->mem_resource->start;
  145. }
  146. res->start = start;
  147. }
  148. void pcibios_resource_to_bus(struct pci_dev *dev, struct pci_bus_region *region,
  149. struct resource *res)
  150. {
  151. struct pci_channel *hose = dev->sysdata;
  152. unsigned long offset = 0;
  153. if (res->flags & IORESOURCE_IO)
  154. offset = hose->io_offset;
  155. else if (res->flags & IORESOURCE_MEM)
  156. offset = hose->mem_offset;
  157. region->start = res->start - offset;
  158. region->end = res->end - offset;
  159. }
  160. void __devinit
  161. pcibios_bus_to_resource(struct pci_dev *dev, struct resource *res,
  162. struct pci_bus_region *region)
  163. {
  164. struct pci_channel *hose = dev->sysdata;
  165. unsigned long offset = 0;
  166. if (res->flags & IORESOURCE_IO)
  167. offset = hose->io_offset;
  168. else if (res->flags & IORESOURCE_MEM)
  169. offset = hose->mem_offset;
  170. res->start = region->start + offset;
  171. res->end = region->end + offset;
  172. }
  173. int pcibios_enable_device(struct pci_dev *dev, int mask)
  174. {
  175. u16 cmd, old_cmd;
  176. int idx;
  177. struct resource *r;
  178. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  179. old_cmd = cmd;
  180. for (idx=0; idx < PCI_NUM_RESOURCES; idx++) {
  181. /* Only set up the requested stuff */
  182. if (!(mask & (1<<idx)))
  183. continue;
  184. r = &dev->resource[idx];
  185. if (!(r->flags & (IORESOURCE_IO | IORESOURCE_MEM)))
  186. continue;
  187. if ((idx == PCI_ROM_RESOURCE) &&
  188. (!(r->flags & IORESOURCE_ROM_ENABLE)))
  189. continue;
  190. if (!r->start && r->end) {
  191. printk(KERN_ERR "PCI: Device %s not available "
  192. "because of resource collisions\n",
  193. pci_name(dev));
  194. return -EINVAL;
  195. }
  196. if (r->flags & IORESOURCE_IO)
  197. cmd |= PCI_COMMAND_IO;
  198. if (r->flags & IORESOURCE_MEM)
  199. cmd |= PCI_COMMAND_MEMORY;
  200. }
  201. if (cmd != old_cmd) {
  202. printk("PCI: Enabling device %s (%04x -> %04x)\n",
  203. pci_name(dev), old_cmd, cmd);
  204. pci_write_config_word(dev, PCI_COMMAND, cmd);
  205. }
  206. return 0;
  207. }
  208. /*
  209. * If we set up a device for bus mastering, we need to check and set
  210. * the latency timer as it may not be properly set.
  211. */
  212. static unsigned int pcibios_max_latency = 255;
  213. void pcibios_set_master(struct pci_dev *dev)
  214. {
  215. u8 lat;
  216. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  217. if (lat < 16)
  218. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  219. else if (lat > pcibios_max_latency)
  220. lat = pcibios_max_latency;
  221. else
  222. return;
  223. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  224. pci_name(dev), lat);
  225. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  226. }
  227. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  228. {
  229. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  230. }
  231. char * __devinit pcibios_setup(char *str)
  232. {
  233. return str;
  234. }
  235. int pci_mmap_page_range(struct pci_dev *dev, struct vm_area_struct *vma,
  236. enum pci_mmap_state mmap_state, int write_combine)
  237. {
  238. /*
  239. * I/O space can be accessed via normal processor loads and stores on
  240. * this platform but for now we elect not to do this and portable
  241. * drivers should not do this anyway.
  242. */
  243. if (mmap_state == pci_mmap_io)
  244. return -EINVAL;
  245. /*
  246. * Ignore write-combine; for now only return uncached mappings.
  247. */
  248. vma->vm_page_prot = pgprot_noncached(vma->vm_page_prot);
  249. return remap_pfn_range(vma, vma->vm_start, vma->vm_pgoff,
  250. vma->vm_end - vma->vm_start,
  251. vma->vm_page_prot);
  252. }
  253. #ifndef CONFIG_GENERIC_IOMAP
  254. static void __iomem *ioport_map_pci(struct pci_dev *dev,
  255. unsigned long port, unsigned int nr)
  256. {
  257. struct pci_channel *chan = dev->sysdata;
  258. if (!chan->io_map_base)
  259. chan->io_map_base = generic_io_base;
  260. return (void __iomem *)(chan->io_map_base + port);
  261. }
  262. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  263. {
  264. resource_size_t start = pci_resource_start(dev, bar);
  265. resource_size_t len = pci_resource_len(dev, bar);
  266. unsigned long flags = pci_resource_flags(dev, bar);
  267. if (unlikely(!len || !start))
  268. return NULL;
  269. if (maxlen && len > maxlen)
  270. len = maxlen;
  271. if (flags & IORESOURCE_IO)
  272. return ioport_map_pci(dev, start, len);
  273. /*
  274. * Presently the IORESOURCE_MEM case is a bit special, most
  275. * SH7751 style PCI controllers have PCI memory at a fixed
  276. * location in the address space where no remapping is desired.
  277. * With the IORESOURCE_MEM case more care has to be taken
  278. * to inhibit page table mapping for legacy cores, but this is
  279. * punted off to __ioremap().
  280. * -- PFM.
  281. */
  282. if (flags & IORESOURCE_MEM) {
  283. if (flags & IORESOURCE_CACHEABLE)
  284. return ioremap(start, len);
  285. return ioremap_nocache(start, len);
  286. }
  287. return NULL;
  288. }
  289. EXPORT_SYMBOL(pci_iomap);
  290. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  291. {
  292. iounmap(addr);
  293. }
  294. EXPORT_SYMBOL(pci_iounmap);
  295. #endif /* CONFIG_GENERIC_IOMAP */
  296. #ifdef CONFIG_HOTPLUG
  297. EXPORT_SYMBOL(pcibios_resource_to_bus);
  298. EXPORT_SYMBOL(pcibios_bus_to_resource);
  299. EXPORT_SYMBOL(PCIBIOS_MIN_IO);
  300. EXPORT_SYMBOL(PCIBIOS_MIN_MEM);
  301. #endif