pcibios.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. /*
  2. * $Id: pcibios.c,v 1.1 2001/08/24 12:38:19 dwmw2 Exp $
  3. *
  4. * arch/sh/kernel/pcibios.c
  5. *
  6. * Copyright (C) 2002 STMicroelectronics Limited
  7. * Author : David J. McKay
  8. *
  9. * Copyright (C) 2004 Richard Curnow, SuperH UK Limited
  10. *
  11. * This file is subject to the terms and conditions of the GNU General Public
  12. * License. See the file "COPYING" in the main directory of this archive
  13. * for more details.
  14. * This is GPL'd.
  15. *
  16. * Provided here are generic versions of:
  17. * pcibios_update_resource()
  18. * pcibios_align_resource()
  19. * pcibios_enable_device()
  20. * pcibios_set_master()
  21. * pcibios_update_irq()
  22. *
  23. * These functions are collected here to reduce duplication of common
  24. * code amongst the many platform-specific PCI support code files.
  25. *
  26. * Platform-specific files are expected to provide:
  27. * pcibios_fixup_bus()
  28. * pcibios_init()
  29. * pcibios_setup()
  30. * pcibios_fixup_pbus_ranges()
  31. */
  32. #include <linux/kernel.h>
  33. #include <linux/pci.h>
  34. #include <linux/init.h>
  35. void
  36. pcibios_update_resource(struct pci_dev *dev, struct resource *root,
  37. struct resource *res, int resource)
  38. {
  39. u32 new, check;
  40. int reg;
  41. new = res->start | (res->flags & PCI_REGION_FLAG_MASK);
  42. if (resource < 6) {
  43. reg = PCI_BASE_ADDRESS_0 + 4*resource;
  44. } else if (resource == PCI_ROM_RESOURCE) {
  45. res->flags |= IORESOURCE_ROM_ENABLE;
  46. new |= PCI_ROM_ADDRESS_ENABLE;
  47. reg = dev->rom_base_reg;
  48. } else {
  49. /* Somebody might have asked allocation of a non-standard resource */
  50. return;
  51. }
  52. pci_write_config_dword(dev, reg, new);
  53. pci_read_config_dword(dev, reg, &check);
  54. if ((new ^ check) & ((new & PCI_BASE_ADDRESS_SPACE_IO) ? PCI_BASE_ADDRESS_IO_MASK : PCI_BASE_ADDRESS_MEM_MASK)) {
  55. printk(KERN_ERR "PCI: Error while updating region "
  56. "%s/%d (%08x != %08x)\n", pci_name(dev), resource,
  57. new, check);
  58. }
  59. }
  60. /*
  61. * We need to avoid collisions with `mirrored' VGA ports
  62. * and other strange ISA hardware, so we always want the
  63. * addresses to be allocated in the 0x000-0x0ff region
  64. * modulo 0x400.
  65. */
  66. void pcibios_align_resource(void *data, struct resource *res,
  67. unsigned long size, unsigned long align)
  68. {
  69. if (res->flags & IORESOURCE_IO) {
  70. unsigned long start = res->start;
  71. if (start & 0x300) {
  72. start = (start + 0x3ff) & ~0x3ff;
  73. res->start = start;
  74. }
  75. }
  76. }
  77. static void pcibios_enable_bridge(struct pci_dev *dev)
  78. {
  79. struct pci_bus *bus = dev->subordinate;
  80. u16 cmd, old_cmd;
  81. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  82. old_cmd = cmd;
  83. if (bus->resource[0]->flags & IORESOURCE_IO) {
  84. cmd |= PCI_COMMAND_IO;
  85. }
  86. if ((bus->resource[1]->flags & IORESOURCE_MEM) ||
  87. (bus->resource[2]->flags & IORESOURCE_PREFETCH)) {
  88. cmd |= PCI_COMMAND_MEMORY;
  89. }
  90. if (cmd != old_cmd) {
  91. pci_write_config_word(dev, PCI_COMMAND, cmd);
  92. }
  93. printk("PCI bridge %s, command register -> %04x\n",
  94. pci_name(dev), cmd);
  95. }
  96. int pcibios_enable_device(struct pci_dev *dev, int mask)
  97. {
  98. u16 cmd, old_cmd;
  99. int idx;
  100. struct resource *r;
  101. if ((dev->class >> 8) == PCI_CLASS_BRIDGE_PCI) {
  102. pcibios_enable_bridge(dev);
  103. }
  104. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  105. old_cmd = cmd;
  106. for(idx=0; idx<6; idx++) {
  107. if (!(mask & (1 << idx)))
  108. continue;
  109. r = &dev->resource[idx];
  110. if (!r->start && r->end) {
  111. printk(KERN_ERR "PCI: Device %s not available because of resource collisions\n", pci_name(dev));
  112. return -EINVAL;
  113. }
  114. if (r->flags & IORESOURCE_IO)
  115. cmd |= PCI_COMMAND_IO;
  116. if (r->flags & IORESOURCE_MEM)
  117. cmd |= PCI_COMMAND_MEMORY;
  118. }
  119. if (dev->resource[PCI_ROM_RESOURCE].start)
  120. cmd |= PCI_COMMAND_MEMORY;
  121. if (cmd != old_cmd) {
  122. printk(KERN_INFO "PCI: Enabling device %s (%04x -> %04x)\n", pci_name(dev), old_cmd, cmd);
  123. pci_write_config_word(dev, PCI_COMMAND, cmd);
  124. }
  125. return 0;
  126. }
  127. /*
  128. * If we set up a device for bus mastering, we need to check and set
  129. * the latency timer as it may not be properly set.
  130. */
  131. unsigned int pcibios_max_latency = 255;
  132. void pcibios_set_master(struct pci_dev *dev)
  133. {
  134. u8 lat;
  135. pci_read_config_byte(dev, PCI_LATENCY_TIMER, &lat);
  136. if (lat < 16)
  137. lat = (64 <= pcibios_max_latency) ? 64 : pcibios_max_latency;
  138. else if (lat > pcibios_max_latency)
  139. lat = pcibios_max_latency;
  140. else
  141. return;
  142. printk(KERN_INFO "PCI: Setting latency timer of device %s to %d\n", pci_name(dev), lat);
  143. pci_write_config_byte(dev, PCI_LATENCY_TIMER, lat);
  144. }
  145. void __init pcibios_update_irq(struct pci_dev *dev, int irq)
  146. {
  147. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  148. }