pci.c 4.5 KB

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