pci.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  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 inline u8 bridge_swizzle(u8 pin, u8 slot)
  24. {
  25. return (((pin - 1) + slot) % 4) + 1;
  26. }
  27. static u8 __init simple_swizzle(struct pci_dev *dev, u8 *pinp)
  28. {
  29. u8 pin = *pinp;
  30. while (dev->bus->parent) {
  31. pin = bridge_swizzle(pin, PCI_SLOT(dev->devfn));
  32. /* Move up the chain of bridges. */
  33. dev = dev->bus->self;
  34. }
  35. *pinp = pin;
  36. /* The slot is the slot of the last bridge. */
  37. return PCI_SLOT(dev->devfn);
  38. }
  39. static int __init pcibios_init(void)
  40. {
  41. struct pci_channel *p;
  42. struct pci_bus *bus;
  43. int busno;
  44. #ifdef CONFIG_PCI_AUTO
  45. /* assign resources */
  46. busno = 0;
  47. for (p = board_pci_channels; p->pci_ops != NULL; p++)
  48. busno = pciauto_assign_resources(busno, p) + 1;
  49. #endif
  50. /* scan the buses */
  51. busno = 0;
  52. for (p = board_pci_channels; p->pci_ops != NULL; p++) {
  53. bus = pci_scan_bus(busno, p->pci_ops, p);
  54. busno = bus->subordinate + 1;
  55. }
  56. pci_fixup_irqs(simple_swizzle, pcibios_map_platform_irq);
  57. return 0;
  58. }
  59. subsys_initcall(pcibios_init);
  60. /*
  61. * Called after each bus is probed, but before its children
  62. * are examined.
  63. */
  64. void __init pcibios_fixup_bus(struct pci_bus *bus)
  65. {
  66. pci_read_bridge_bases(bus);
  67. }
  68. void
  69. pcibios_update_resource(struct pci_dev *dev, struct resource *root,
  70. struct resource *res, int resource)
  71. {
  72. u32 new, check;
  73. int reg;
  74. new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
  75. if (resource < 6) {
  76. reg = PCI_BASE_ADDRESS_0 + 4*resource;
  77. } else if (resource == PCI_ROM_RESOURCE) {
  78. res->flags |= IORESOURCE_ROM_ENABLE;
  79. new |= PCI_ROM_ADDRESS_ENABLE;
  80. reg = dev->rom_base_reg;
  81. } else {
  82. /*
  83. * Somebody might have asked allocation of a non-standard
  84. * resource
  85. */
  86. return;
  87. }
  88. pci_write_config_dword(dev, reg, new);
  89. pci_read_config_dword(dev, reg, &check);
  90. if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ?
  91. PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
  92. printk(KERN_ERR "PCI: Error while updating region "
  93. "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
  94. new, check);
  95. }
  96. }
  97. void pcibios_align_resource(void *data, struct resource *res,
  98. resource_size_t size, resource_size_t align)
  99. __attribute__ ((weak));
  100. /*
  101. * We need to avoid collisions with `mirrored' VGA ports
  102. * and other strange ISA hardware, so we always want the
  103. * addresses to be allocated in the 0x000-0x0ff region
  104. * modulo 0x400.
  105. */
  106. void pcibios_align_resource(void *data, struct resource *res,
  107. resource_size_t size, resource_size_t align)
  108. {
  109. if (res->flags & IORESOURCE_IO) {
  110. resource_size_t start = res->start;
  111. if (start & 0x300) {
  112. start = (start + 0x3ff) & ~0x3ff;
  113. res->start = start;
  114. }
  115. }
  116. }
  117. int pcibios_enable_device(struct pci_dev *dev, int mask)
  118. {
  119. u16 cmd, old_cmd;
  120. int idx;
  121. struct resource *r;
  122. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  123. old_cmd = cmd;
  124. for(idx=0; idx<6; idx++) {
  125. if (!(mask & (1 << idx)))
  126. continue;
  127. r = &dev->resource[idx];
  128. if (!r->start && r->end) {
  129. printk(KERN_ERR "PCI: Device %s not available because "
  130. "of resource collisions\n", pci_name(dev));
  131. return -EINVAL;
  132. }
  133. if (r->flags & IORESOURCE_IO)
  134. cmd |= PCI_COMMAND_IO;
  135. if (r->flags & IORESOURCE_MEM)
  136. cmd |= PCI_COMMAND_MEMORY;
  137. }
  138. if (dev->resource[PCI_ROM_RESOURCE].start)
  139. cmd |= PCI_COMMAND_MEMORY;
  140. if (cmd != old_cmd) {
  141. printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n",
  142. pci_name(dev), old_cmd, cmd);
  143. pci_write_config_word(dev, PCI_COMMAND, cmd);
  144. }
  145. return 0;
  146. }
  147. /*
  148. * If we set up a device for bus mastering, we need to check and set
  149. * the latency timer as it may not be properly set.
  150. */
  151. unsigned int pcibios_max_latency = 255;
  152. void pcibios_set_master(struct pci_dev *dev)
  153. {
  154. u8 lat;
  155. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  156. if (lat < 16)
  157. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  158. else if (lat > pcibios_max_latency)
  159. lat = pcibios_max_latency;
  160. else
  161. return;
  162. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n",
  163. pci_name(dev), lat);
  164. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  165. }
  166. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  167. {
  168. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  169. }
  170. void __iomem *pci_iomap(struct pci_dev *dev, int bar, unsigned long maxlen)
  171. {
  172. unsigned long start = pci_resource_start(dev, bar);
  173. unsigned long len = pci_resource_len(dev, bar);
  174. unsigned long flags = pci_resource_flags(dev, bar);
  175. if (unlikely(!len || !start))
  176. return NULL;
  177. if (maxlen && len > maxlen)
  178. len = maxlen;
  179. /*
  180. * Presently the IORESOURCE_MEM case is a bit special, most
  181. * SH7751 style PCI controllers have PCI memory at a fixed
  182. * location in the address space where no remapping is desired
  183. * (typically at 0xfd000000, but is_pci_memaddr() will know
  184. * best). With the IORESOURCE_MEM case more care has to be taken
  185. * to inhibit page table mapping for legacy cores, but this is
  186. * punted off to __ioremap().
  187. * -- PFM.
  188. */
  189. if (flags & IORESOURCE_IO)
  190. return ioport_map(start, len);
  191. if (flags & IORESOURCE_MEM)
  192. return ioremap(start, len);
  193. return NULL;
  194. }
  195. EXPORT_SYMBOL(pci_iomap);
  196. void pci_iounmap(struct pci_dev *dev, void __iomem *addr)
  197. {
  198. iounmap(addr);
  199. }
  200. EXPORT_SYMBOL(pci_iounmap);