pci.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  1. /*
  2. * arch/xtensa/kernel/pci.c
  3. *
  4. * PCI bios-type initialisation for PCI machines
  5. *
  6. * This program is free software; you can redistribute it and/or modify it
  7. * under the terms of the GNU General Public License as published by the
  8. * Free Software Foundation; either version 2 of the License, or (at your
  9. * option) any later version.
  10. *
  11. * Copyright (C) 2001-2005 Tensilica Inc.
  12. *
  13. * Based largely on work from Cort (ppc/kernel/pci.c)
  14. * IO functions copied from sparc.
  15. *
  16. * Chris Zankel <chris@zankel.net>
  17. *
  18. */
  19. #include <linux/kernel.h>
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include <linux/string.h>
  23. #include <linux/init.h>
  24. #include <linux/sched.h>
  25. #include <linux/errno.h>
  26. #include <linux/memblock.h>
  27. #include <asm/pci-bridge.h>
  28. #include <asm/platform.h>
  29. /* PCI Controller */
  30. /*
  31. * pcibios_alloc_controller
  32. * pcibios_enable_device
  33. * pcibios_fixups
  34. * pcibios_align_resource
  35. * pcibios_fixup_bus
  36. * pci_bus_add_device
  37. */
  38. static struct pci_controller *pci_ctrl_head;
  39. static struct pci_controller **pci_ctrl_tail = &pci_ctrl_head;
  40. static int pci_bus_count;
  41. /*
  42. * We need to avoid collisions with `mirrored' VGA ports
  43. * and other strange ISA hardware, so we always want the
  44. * addresses to be allocated in the 0x000-0x0ff region
  45. * modulo 0x400.
  46. *
  47. * Why? Because some silly external IO cards only decode
  48. * the low 10 bits of the IO address. The 0x00-0xff region
  49. * is reserved for motherboard devices that decode all 16
  50. * bits, so it's ok to allocate at, say, 0x2800-0x28ff,
  51. * but we want to try to avoid allocating at 0x2900-0x2bff
  52. * which might have be mirrored at 0x0100-0x03ff..
  53. */
  54. resource_size_t
  55. pcibios_align_resource(void *data, const struct resource *res,
  56. resource_size_t size, resource_size_t align)
  57. {
  58. struct pci_dev *dev = data;
  59. resource_size_t start = res->start;
  60. if (res->flags & IORESOURCE_IO) {
  61. if (size > 0x100) {
  62. pr_err("PCI: I/O Region %s/%d too large (%u bytes)\n",
  63. pci_name(dev), dev->resource - res,
  64. size);
  65. }
  66. if (start & 0x300)
  67. start = (start + 0x3ff) & ~0x3ff;
  68. }
  69. return start;
  70. }
  71. static void __init pci_controller_apertures(struct pci_controller *pci_ctrl,
  72. struct list_head *resources)
  73. {
  74. struct resource *res;
  75. unsigned long io_offset;
  76. int i;
  77. io_offset = (unsigned long)pci_ctrl->io_space.base;
  78. res = &pci_ctrl->io_resource;
  79. if (!res->flags) {
  80. if (io_offset)
  81. pr_err("I/O resource not set for host bridge %d\n",
  82. pci_ctrl->index);
  83. res->start = 0;
  84. res->end = IO_SPACE_LIMIT;
  85. res->flags = IORESOURCE_IO;
  86. }
  87. res->start += io_offset;
  88. res->end += io_offset;
  89. pci_add_resource_offset(resources, res, io_offset);
  90. for (i = 0; i < 3; i++) {
  91. res = &pci_ctrl->mem_resources[i];
  92. if (!res->flags) {
  93. if (i > 0)
  94. continue;
  95. pr_err("Memory resource not set for host bridge %d\n",
  96. pci_ctrl->index);
  97. res->start = 0;
  98. res->end = ~0U;
  99. res->flags = IORESOURCE_MEM;
  100. }
  101. pci_add_resource(resources, res);
  102. }
  103. }
  104. static int __init pcibios_init(void)
  105. {
  106. struct pci_controller *pci_ctrl;
  107. struct list_head resources;
  108. struct pci_bus *bus;
  109. int next_busno = 0, ret;
  110. pr_info("PCI: Probing PCI hardware\n");
  111. /* Scan all of the recorded PCI controllers. */
  112. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  113. pci_ctrl->last_busno = 0xff;
  114. INIT_LIST_HEAD(&resources);
  115. pci_controller_apertures(pci_ctrl, &resources);
  116. bus = pci_scan_root_bus(NULL, pci_ctrl->first_busno,
  117. pci_ctrl->ops, pci_ctrl, &resources);
  118. if (!bus)
  119. continue;
  120. pci_ctrl->bus = bus;
  121. pci_ctrl->last_busno = bus->busn_res.end;
  122. if (next_busno <= pci_ctrl->last_busno)
  123. next_busno = pci_ctrl->last_busno+1;
  124. }
  125. pci_bus_count = next_busno;
  126. ret = platform_pcibios_fixup();
  127. if (ret)
  128. return ret;
  129. for (pci_ctrl = pci_ctrl_head; pci_ctrl; pci_ctrl = pci_ctrl->next) {
  130. if (pci_ctrl->bus)
  131. pci_bus_add_devices(pci_ctrl->bus);
  132. }
  133. return 0;
  134. }
  135. subsys_initcall(pcibios_init);
  136. void pcibios_fixup_bus(struct pci_bus *bus)
  137. {
  138. if (bus->parent) {
  139. /* This is a subordinate bridge */
  140. pci_read_bridge_bases(bus);
  141. }
  142. }
  143. void pcibios_set_master(struct pci_dev *dev)
  144. {
  145. /* No special bus mastering setup handling */
  146. }
  147. int pcibios_enable_device(struct pci_dev *dev, int mask)
  148. {
  149. u16 cmd, old_cmd;
  150. int idx;
  151. struct resource *r;
  152. pci_read_config_word(dev, PCI_COMMAND, &cmd);
  153. old_cmd = cmd;
  154. for (idx=0; idx<6; idx++) {
  155. r = &dev->resource[idx];
  156. if (!r->start && r->end) {
  157. pci_err(dev, "can't enable device: resource collisions\n");
  158. return -EINVAL;
  159. }
  160. if (r->flags & IORESOURCE_IO)
  161. cmd |= PCI_COMMAND_IO;
  162. if (r->flags & IORESOURCE_MEM)
  163. cmd |= PCI_COMMAND_MEMORY;
  164. }
  165. if (cmd != old_cmd) {
  166. pci_info(dev, "enabling device (%04x -> %04x)\n", old_cmd, cmd);
  167. pci_write_config_word(dev, PCI_COMMAND, cmd);
  168. }
  169. return 0;
  170. }
  171. /*
  172. * Platform support for /proc/bus/pci/X/Y mmap()s.
  173. * -- paulus.
  174. */
  175. int pci_iobar_pfn(struct pci_dev *pdev, int bar, struct vm_area_struct *vma)
  176. {
  177. struct pci_controller *pci_ctrl = (struct pci_controller*) pdev->sysdata;
  178. resource_size_t ioaddr = pci_resource_start(pdev, bar);
  179. if (pci_ctrl == 0)
  180. return -EINVAL; /* should never happen */
  181. /* Convert to an offset within this PCI controller */
  182. ioaddr -= (unsigned long)pci_ctrl->io_space.base;
  183. vma->vm_pgoff += (ioaddr + pci_ctrl->io_space.start) >> PAGE_SHIFT;
  184. return 0;
  185. }