pci.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. /*
  2. * arch/sh/drivers/pci/pci.c
  3. *
  4. * Copyright (c) 2002 M. R. Brown <mrbrown@linux-sh.org>
  5. * Copyright (c) 2004 - 2006 Paul Mundt <lethal@linux-sh.org>
  6. *
  7. * These functions are collected here to reduce duplication of common
  8. * code amongst the many platform-specific PCI support code files.
  9. *
  10. * These routines require the following board-specific routines:
  11. * void pcibios_fixup_irqs();
  12. *
  13. * See include/asm-sh/pci.h for more information.
  14. *
  15. * This file is subject to the terms and conditions of the GNU General Public
  16. * License. See the file "COPYING" in the main directory of this archive
  17. * for more details.
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/init.h>
  22. #include <asm/io.h>
  23. static u8 __init simple_swizzle(struct pci_dev *dev, u8 *pinp)
  24. {
  25. u8 pin = *pinp;
  26. while (dev->bus->parent) {
  27. pin = pci_swizzle_interrupt_pin(dev, pin);
  28. /* Move up the chain of bridges. */
  29. dev = dev->bus->self;
  30. }
  31. *pinp = pin;
  32. /* The slot is the slot of the last bridge. */
  33. return PCI_SLOT(dev->devfn);
  34. }
  35. static int __init pcibios_init(void)
  36. {
  37. struct pci_channel *p;
  38. struct pci_bus *bus;
  39. int busno;
  40. #ifdef CONFIG_PCI_AUTO
  41. /* assign resources */
  42. busno = 0;
  43. for (p = board_pci_channels; p->pci_ops != NULL; p++)
  44. busno = pciauto_assign_resources(busno, p) + 1;
  45. #endif
  46. /* scan the buses */
  47. busno = 0;
  48. for (p = board_pci_channels; p->pci_ops != NULL; p++) {
  49. bus = pci_scan_bus(busno, p->pci_ops, p);
  50. busno = bus->subordinate + 1;
  51. }
  52. pci_fixup_irqs(simple_swizzle, pcibios_map_platform_irq);
  53. return 0;
  54. }
  55. subsys_initcall(pcibios_init);
  56. /*
  57. * Called after each bus is probed, but before its children
  58. * are examined.
  59. */
  60. void __devinit __weak pcibios_fixup_bus(struct pci_bus *bus)
  61. {
  62. pci_read_bridge_bases(bus);
  63. }
  64. void pcibios_align_resource(void *data, struct resource *res,
  65. resource_size_t size, resource_size_t align)
  66. __attribute__ ((weak));
  67. /*
  68. * We need to avoid collisions with `mirrored' VGA ports
  69. * and other strange ISA hardware, so we always want the
  70. * addresses to be allocated in the 0x000-0x0ff region
  71. * modulo 0x400.
  72. */
  73. void pcibios_align_resource(void *data, struct resource *res,
  74. resource_size_t size, resource_size_t align)
  75. {
  76. if (res->flags & IORESOURCE_IO) {
  77. resource_size_t start = res->start;
  78. if (start & 0x300) {
  79. start = (start + 0x3ff) & ~0x3ff;
  80. res->start = start;
  81. }
  82. }
  83. }
  84. int pcibios_enable_device(struct pci_dev *dev, int mask)
  85. {
  86. u16 cmd, old_cmd;
  87. int idx;
  88. struct resource *r;
  89. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  90. old_cmd = cmd;
  91. for(idx=0; idx<6; idx++) {
  92. if (!(mask & (1 << idx)))
  93. continue;
  94. r = &dev->resource[idx];
  95. if (!r->start && r->end) {
  96. printk(KERN_ERR "PCI: Device %s not available because "
  97. "of resource collisions\n", pci_name(dev));
  98. return -EINVAL;
  99. }
  100. if (r->flags & IORESOURCE_IO)
  101. cmd |= PCI_COMMAND_IO;
  102. if (r->flags & IORESOURCE_MEM)
  103. cmd |= PCI_COMMAND_MEMORY;
  104. }
  105. if (dev->resource[PCI_ROM_RESOURCE].start)
  106. cmd |= PCI_COMMAND_MEMORY;
  107. if (cmd != old_cmd) {
  108. printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
  109. pci_name(dev), old_cmd, cmd);
  110. pci_write_config_word(dev, PCI_COMMAND, cmd);
  111. }
  112. return 0;
  113. }
  114. /*
  115. * If we set up a device for bus mastering, we need to check and set
  116. * the latency timer as it may not be properly set.
  117. */
  118. static unsigned int pcibios_max_latency = 255;
  119. void pcibios_set_master(struct pci_dev *dev)
  120. {
  121. u8 lat;
  122. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  123. if (lat < 16)
  124. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  125. else if (lat > pcibios_max_latency)
  126. lat = pcibios_max_latency;
  127. else
  128. return;
  129. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  130. pci_name(dev), lat);
  131. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  132. }
  133. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  134. {
  135. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  136. }
  137. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  138. {
  139. resource_size_t start = pci_resource_start(dev, bar);
  140. resource_size_t len = pci_resource_len(dev, bar);
  141. unsigned long flags = pci_resource_flags(dev, bar);
  142. if (unlikely(!len || !start))
  143. return NULL;
  144. if (maxlen && len > maxlen)
  145. len = maxlen;
  146. /*
  147. * Presently the IORESOURCE_MEM case is a bit special, most
  148. * SH7751 style PCI controllers have PCI memory at a fixed
  149. * location in the address space where no remapping is desired
  150. * (typically at 0xfd000000, but is_pci_memaddr() will know
  151. * best). With the IORESOURCE_MEM case more care has to be taken
  152. * to inhibit page table mapping for legacy cores, but this is
  153. * punted off to __ioremap().
  154. * -- PFM.
  155. */
  156. if (flags & IORESOURCE_IO)
  157. return ioport_map(start, len);
  158. if (flags & IORESOURCE_MEM)
  159. return ioremap(start, len);
  160. return NULL;
  161. }
  162. EXPORT_SYMBOL(pci_iomap);
  163. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  164. {
  165. iounmap(addr);
  166. }
  167. EXPORT_SYMBOL(pci_iounmap);