pci-auto.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * arch/xtensa/lib/pci-auto.c
  3. *
  4. * PCI autoconfiguration library
  5. *
  6. * Copyright (C) 2001 - 2005 Tensilica Inc.
  7. *
  8. * Chris Zankel <zankel@tensilica.com, cez@zankel.net>
  9. *
  10. * Based on work from Matt Porter <mporter@mvista.com>
  11. *
  12. * This program is free software; you can redistribute it and/or modify it
  13. * under the terms of the GNU General Public License as published by the
  14. * Free Software Foundation; either version 2 of the License, or (at your
  15. * option) any later version.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/init.h>
  19. #include <linux/pci.h>
  20. #include <asm/pci-bridge.h>
  21. /*
  22. *
  23. * Setting up a PCI
  24. *
  25. * pci_ctrl->first_busno = <first bus number (0)>
  26. * pci_ctrl->last_busno = <last bus number (0xff)>
  27. * pci_ctrl->ops = <PCI config operations>
  28. * pci_ctrl->map_irq = <function to return the interrupt number for a device>
  29. *
  30. * pci_ctrl->io_space.start = <IO space start address (PCI view)>
  31. * pci_ctrl->io_space.end = <IO space end address (PCI view)>
  32. * pci_ctrl->io_space.base = <IO space offset: address 0 from CPU space>
  33. * pci_ctrl->mem_space.start = <MEM space start address (PCI view)>
  34. * pci_ctrl->mem_space.end = <MEM space end address (PCI view)>
  35. * pci_ctrl->mem_space.base = <MEM space offset: address 0 from CPU space>
  36. *
  37. * pcibios_init_resource(&pci_ctrl->io_resource, <IO space start>,
  38. * <IO space end>, IORESOURCE_IO, "PCI host bridge");
  39. * pcibios_init_resource(&pci_ctrl->mem_resources[0], <MEM space start>,
  40. * <MEM space end>, IORESOURCE_MEM, "PCI host bridge");
  41. *
  42. * pci_ctrl->last_busno = pciauto_bus_scan(pci_ctrl,pci_ctrl->first_busno);
  43. *
  44. * int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
  45. *
  46. */
  47. static int pciauto_upper_iospc;
  48. static int pciauto_upper_memspc;
  49. static struct pci_dev pciauto_dev;
  50. static struct pci_bus pciauto_bus;
  51. /*
  52. * Helper functions
  53. */
  54. /* Initialize the bars of a PCI device. */
  55. static void __init
  56. pciauto_setup_bars(struct pci_dev *dev, int bar_limit)
  57. {
  58. int bar_size;
  59. int bar, bar_nr;
  60. int *upper_limit;
  61. int found_mem64 = 0;
  62. for (bar = PCI_BASE_ADDRESS_0, bar_nr = 0;
  63. bar <= bar_limit;
  64. bar+=4, bar_nr++)
  65. {
  66. /* Tickle the BAR and get the size */
  67. pci_write_config_dword(dev, bar, 0xffffffff);
  68. pci_read_config_dword(dev, bar, &bar_size);
  69. /* If BAR is not implemented go to the next BAR */
  70. if (!bar_size)
  71. continue;
  72. /* Check the BAR type and set our address mask */
  73. if (bar_size & PCI_BASE_ADDRESS_SPACE_IO)
  74. {
  75. bar_size &= PCI_BASE_ADDRESS_IO_MASK;
  76. upper_limit = &pciauto_upper_iospc;
  77. pr_debug("PCI Autoconfig: BAR %d, I/O, ", bar_nr);
  78. }
  79. else
  80. {
  81. if ((bar_size & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  82. PCI_BASE_ADDRESS_MEM_TYPE_64)
  83. found_mem64 = 1;
  84. bar_size &= PCI_BASE_ADDRESS_MEM_MASK;
  85. upper_limit = &pciauto_upper_memspc;
  86. pr_debug("PCI Autoconfig: BAR %d, Mem, ", bar_nr);
  87. }
  88. /* Allocate a base address (bar_size is negative!) */
  89. *upper_limit = (*upper_limit + bar_size) & bar_size;
  90. /* Write it out and update our limit */
  91. pci_write_config_dword(dev, bar, *upper_limit);
  92. /*
  93. * If we are a 64-bit decoder then increment to the
  94. * upper 32 bits of the bar and force it to locate
  95. * in the lower 4GB of memory.
  96. */
  97. if (found_mem64)
  98. pci_write_config_dword(dev, (bar+=4), 0x00000000);
  99. pr_debug("size=0x%x, address=0x%x\n",
  100. ~bar_size + 1, *upper_limit);
  101. }
  102. }
  103. /* Initialize the interrupt number. */
  104. static void __init
  105. pciauto_setup_irq(struct pci_controller* pci_ctrl,struct pci_dev *dev,int devfn)
  106. {
  107. u8 pin;
  108. int irq = 0;
  109. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  110. /* Fix illegal pin numbers. */
  111. if (pin == 0 || pin > 4)
  112. pin = 1;
  113. if (pci_ctrl->map_irq)
  114. irq = pci_ctrl->map_irq(dev, PCI_SLOT(devfn), pin);
  115. if (irq == -1)
  116. irq = 0;
  117. pr_debug("PCI Autoconfig: Interrupt %d, pin %d\n", irq, pin);
  118. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  119. }
  120. static void __init
  121. pciauto_prescan_setup_bridge(struct pci_dev *dev, int current_bus,
  122. int sub_bus, int *iosave, int *memsave)
  123. {
  124. /* Configure bus number registers */
  125. pci_write_config_byte(dev, PCI_PRIMARY_BUS, current_bus);
  126. pci_write_config_byte(dev, PCI_SECONDARY_BUS, sub_bus + 1);
  127. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, 0xff);
  128. /* Round memory allocator to 1MB boundary */
  129. pciauto_upper_memspc &= ~(0x100000 - 1);
  130. *memsave = pciauto_upper_memspc;
  131. /* Round I/O allocator to 4KB boundary */
  132. pciauto_upper_iospc &= ~(0x1000 - 1);
  133. *iosave = pciauto_upper_iospc;
  134. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  135. pci_write_config_word(dev, PCI_MEMORY_LIMIT,
  136. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  137. pci_write_config_byte(dev, PCI_IO_LIMIT,
  138. ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
  139. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  140. ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
  141. }
  142. static void __init
  143. pciauto_postscan_setup_bridge(struct pci_dev *dev, int current_bus, int sub_bus,
  144. int *iosave, int *memsave)
  145. {
  146. int cmdstat;
  147. /* Configure bus number registers */
  148. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, sub_bus);
  149. /*
  150. * Round memory allocator to 1MB boundary.
  151. * If no space used, allocate minimum.
  152. */
  153. pciauto_upper_memspc &= ~(0x100000 - 1);
  154. if (*memsave == pciauto_upper_memspc)
  155. pciauto_upper_memspc -= 0x00100000;
  156. pci_write_config_word(dev, PCI_MEMORY_BASE, pciauto_upper_memspc >> 16);
  157. /* Allocate 1MB for pre-fretch */
  158. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT,
  159. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  160. pciauto_upper_memspc -= 0x100000;
  161. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE,
  162. pciauto_upper_memspc >> 16);
  163. /* Round I/O allocator to 4KB boundary */
  164. pciauto_upper_iospc &= ~(0x1000 - 1);
  165. if (*iosave == pciauto_upper_iospc)
  166. pciauto_upper_iospc -= 0x1000;
  167. pci_write_config_byte(dev, PCI_IO_BASE,
  168. (pciauto_upper_iospc & 0x0000f000) >> 8);
  169. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  170. pciauto_upper_iospc >> 16);
  171. /* Enable memory and I/O accesses, enable bus master */
  172. pci_read_config_dword(dev, PCI_COMMAND, &cmdstat);
  173. pci_write_config_dword(dev, PCI_COMMAND,
  174. cmdstat |
  175. PCI_COMMAND_IO |
  176. PCI_COMMAND_MEMORY |
  177. PCI_COMMAND_MASTER);
  178. }
  179. /*
  180. * Scan the current PCI bus.
  181. */
  182. int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
  183. {
  184. int sub_bus, pci_devfn, pci_class, cmdstat, found_multi=0;
  185. unsigned short vid;
  186. unsigned char header_type;
  187. struct pci_dev *dev = &pciauto_dev;
  188. pciauto_dev.bus = &pciauto_bus;
  189. pciauto_dev.sysdata = pci_ctrl;
  190. pciauto_bus.ops = pci_ctrl->ops;
  191. /*
  192. * Fetch our I/O and memory space upper boundaries used
  193. * to allocated base addresses on this pci_controller.
  194. */
  195. if (current_bus == pci_ctrl->first_busno)
  196. {
  197. pciauto_upper_iospc = pci_ctrl->io_resource.end + 1;
  198. pciauto_upper_memspc = pci_ctrl->mem_resources[0].end + 1;
  199. }
  200. sub_bus = current_bus;
  201. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++)
  202. {
  203. /* Skip our host bridge */
  204. if ((current_bus == pci_ctrl->first_busno) && (pci_devfn == 0))
  205. continue;
  206. if (PCI_FUNC(pci_devfn) && !found_multi)
  207. continue;
  208. pciauto_bus.number = current_bus;
  209. pciauto_dev.devfn = pci_devfn;
  210. /* If config space read fails from this device, move on */
  211. if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type))
  212. continue;
  213. if (!PCI_FUNC(pci_devfn))
  214. found_multi = header_type & 0x80;
  215. pci_read_config_word(dev, PCI_VENDOR_ID, &vid);
  216. if (vid == 0xffff || vid == 0x0000) {
  217. found_multi = 0;
  218. continue;
  219. }
  220. pci_read_config_dword(dev, PCI_CLASS_REVISION, &pci_class);
  221. if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
  222. int iosave, memsave;
  223. pr_debug("PCI Autoconfig: Found P2P bridge, device %d\n",
  224. PCI_SLOT(pci_devfn));
  225. /* Allocate PCI I/O and/or memory space */
  226. pciauto_setup_bars(dev, PCI_BASE_ADDRESS_1);
  227. pciauto_prescan_setup_bridge(dev, current_bus, sub_bus,
  228. &iosave, &memsave);
  229. sub_bus = pciauto_bus_scan(pci_ctrl, sub_bus+1);
  230. pciauto_postscan_setup_bridge(dev, current_bus, sub_bus,
  231. &iosave, &memsave);
  232. pciauto_bus.number = current_bus;
  233. continue;
  234. }
  235. /*
  236. * Found a peripheral, enable some standard
  237. * settings
  238. */
  239. pci_read_config_dword(dev, PCI_COMMAND, &cmdstat);
  240. pci_write_config_dword(dev, PCI_COMMAND,
  241. cmdstat |
  242. PCI_COMMAND_IO |
  243. PCI_COMMAND_MEMORY |
  244. PCI_COMMAND_MASTER);
  245. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x80);
  246. /* Allocate PCI I/O and/or memory space */
  247. pr_debug("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
  248. current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
  249. pciauto_setup_bars(dev, PCI_BASE_ADDRESS_5);
  250. pciauto_setup_irq(pci_ctrl, dev, pci_devfn);
  251. }
  252. return sub_bus;
  253. }