pci-auto.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. /* define DEBUG to print some debugging messages. */
  48. #undef DEBUG
  49. #ifdef DEBUG
  50. # define DBG(x...) printk(x)
  51. #else
  52. # define DBG(x...)
  53. #endif
  54. static int pciauto_upper_iospc;
  55. static int pciauto_upper_memspc;
  56. static struct pci_dev pciauto_dev;
  57. static struct pci_bus pciauto_bus;
  58. /*
  59. * Helper functions
  60. */
  61. /* Initialize the bars of a PCI device. */
  62. static void __init
  63. pciauto_setup_bars(struct pci_dev *dev, int bar_limit)
  64. {
  65. int bar_size;
  66. int bar, bar_nr;
  67. int *upper_limit;
  68. int found_mem64 = 0;
  69. for (bar = PCI_BASE_ADDRESS_0, bar_nr = 0;
  70. bar <= bar_limit;
  71. bar+=4, bar_nr++)
  72. {
  73. /* Tickle the BAR and get the size */
  74. pci_write_config_dword(dev, bar, 0xffffffff);
  75. pci_read_config_dword(dev, bar, &bar_size);
  76. /* If BAR is not implemented go to the next BAR */
  77. if (!bar_size)
  78. continue;
  79. /* Check the BAR type and set our address mask */
  80. if (bar_size & PCI_BASE_ADDRESS_SPACE_IO)
  81. {
  82. bar_size &= PCI_BASE_ADDRESS_IO_MASK;
  83. upper_limit = &pciauto_upper_iospc;
  84. DBG("PCI Autoconfig: BAR %d, I/O, ", bar_nr);
  85. }
  86. else
  87. {
  88. if ((bar_size & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  89. PCI_BASE_ADDRESS_MEM_TYPE_64)
  90. found_mem64 = 1;
  91. bar_size &= PCI_BASE_ADDRESS_MEM_MASK;
  92. upper_limit = &pciauto_upper_memspc;
  93. DBG("PCI Autoconfig: BAR %d, Mem, ", bar_nr);
  94. }
  95. /* Allocate a base address (bar_size is negative!) */
  96. *upper_limit = (*upper_limit + bar_size) & bar_size;
  97. /* Write it out and update our limit */
  98. pci_write_config_dword(dev, bar, *upper_limit);
  99. /*
  100. * If we are a 64-bit decoder then increment to the
  101. * upper 32 bits of the bar and force it to locate
  102. * in the lower 4GB of memory.
  103. */
  104. if (found_mem64)
  105. pci_write_config_dword(dev, (bar+=4), 0x00000000);
  106. DBG("size=0x%x, address=0x%x\n", ~bar_size + 1, *upper_limit);
  107. }
  108. }
  109. /* Initialize the interrupt number. */
  110. static void __init
  111. pciauto_setup_irq(struct pci_controller* pci_ctrl,struct pci_dev *dev,int devfn)
  112. {
  113. u8 pin;
  114. int irq = 0;
  115. pci_read_config_byte(dev, PCI_INTERRUPT_PIN, &pin);
  116. /* Fix illegal pin numbers. */
  117. if (pin == 0 || pin > 4)
  118. pin = 1;
  119. if (pci_ctrl->map_irq)
  120. irq = pci_ctrl->map_irq(dev, PCI_SLOT(devfn), pin);
  121. if (irq == -1)
  122. irq = 0;
  123. DBG("PCI Autoconfig: Interrupt %d, pin %d\n", irq, pin);
  124. pci_write_config_byte(dev, PCI_INTERRUPT_LINE, irq);
  125. }
  126. static void __init
  127. pciauto_prescan_setup_bridge(struct pci_dev *dev, int current_bus,
  128. int sub_bus, int *iosave, int *memsave)
  129. {
  130. /* Configure bus number registers */
  131. pci_write_config_byte(dev, PCI_PRIMARY_BUS, current_bus);
  132. pci_write_config_byte(dev, PCI_SECONDARY_BUS, sub_bus + 1);
  133. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, 0xff);
  134. /* Round memory allocator to 1MB boundary */
  135. pciauto_upper_memspc &= ~(0x100000 - 1);
  136. *memsave = pciauto_upper_memspc;
  137. /* Round I/O allocator to 4KB boundary */
  138. pciauto_upper_iospc &= ~(0x1000 - 1);
  139. *iosave = pciauto_upper_iospc;
  140. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  141. pci_write_config_word(dev, PCI_MEMORY_LIMIT,
  142. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  143. pci_write_config_byte(dev, PCI_IO_LIMIT,
  144. ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
  145. pci_write_config_word(dev, PCI_IO_LIMIT_UPPER16,
  146. ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
  147. }
  148. static void __init
  149. pciauto_postscan_setup_bridge(struct pci_dev *dev, int current_bus, int sub_bus,
  150. int *iosave, int *memsave)
  151. {
  152. int cmdstat;
  153. /* Configure bus number registers */
  154. pci_write_config_byte(dev, PCI_SUBORDINATE_BUS, sub_bus);
  155. /*
  156. * Round memory allocator to 1MB boundary.
  157. * If no space used, allocate minimum.
  158. */
  159. pciauto_upper_memspc &= ~(0x100000 - 1);
  160. if (*memsave == pciauto_upper_memspc)
  161. pciauto_upper_memspc -= 0x00100000;
  162. pci_write_config_word(dev, PCI_MEMORY_BASE, pciauto_upper_memspc >> 16);
  163. /* Allocate 1MB for pre-fretch */
  164. pci_write_config_word(dev, PCI_PREF_MEMORY_LIMIT,
  165. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  166. pciauto_upper_memspc -= 0x100000;
  167. pci_write_config_word(dev, PCI_PREF_MEMORY_BASE,
  168. pciauto_upper_memspc >> 16);
  169. /* Round I/O allocator to 4KB boundary */
  170. pciauto_upper_iospc &= ~(0x1000 - 1);
  171. if (*iosave == pciauto_upper_iospc)
  172. pciauto_upper_iospc -= 0x1000;
  173. pci_write_config_byte(dev, PCI_IO_BASE,
  174. (pciauto_upper_iospc & 0x0000f000) >> 8);
  175. pci_write_config_word(dev, PCI_IO_BASE_UPPER16,
  176. pciauto_upper_iospc >> 16);
  177. /* Enable memory and I/O accesses, enable bus master */
  178. pci_read_config_dword(dev, PCI_COMMAND, &cmdstat);
  179. pci_write_config_dword(dev, PCI_COMMAND,
  180. cmdstat |
  181. PCI_COMMAND_IO |
  182. PCI_COMMAND_MEMORY |
  183. PCI_COMMAND_MASTER);
  184. }
  185. /*
  186. * Scan the current PCI bus.
  187. */
  188. int __init pciauto_bus_scan(struct pci_controller *pci_ctrl, int current_bus)
  189. {
  190. int sub_bus, pci_devfn, pci_class, cmdstat, found_multi=0;
  191. unsigned short vid;
  192. unsigned char header_type;
  193. struct pci_dev *dev = &pciauto_dev;
  194. pciauto_dev.bus = &pciauto_bus;
  195. pciauto_dev.sysdata = pci_ctrl;
  196. pciauto_bus.ops = pci_ctrl->ops;
  197. /*
  198. * Fetch our I/O and memory space upper boundaries used
  199. * to allocated base addresses on this pci_controller.
  200. */
  201. if (current_bus == pci_ctrl->first_busno)
  202. {
  203. pciauto_upper_iospc = pci_ctrl->io_resource.end + 1;
  204. pciauto_upper_memspc = pci_ctrl->mem_resources[0].end + 1;
  205. }
  206. sub_bus = current_bus;
  207. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++)
  208. {
  209. /* Skip our host bridge */
  210. if ((current_bus == pci_ctrl->first_busno) && (pci_devfn == 0))
  211. continue;
  212. if (PCI_FUNC(pci_devfn) && !found_multi)
  213. continue;
  214. pciauto_bus.number = current_bus;
  215. pciauto_dev.devfn = pci_devfn;
  216. /* If config space read fails from this device, move on */
  217. if (pci_read_config_byte(dev, PCI_HEADER_TYPE, &header_type))
  218. continue;
  219. if (!PCI_FUNC(pci_devfn))
  220. found_multi = header_type & 0x80;
  221. pci_read_config_word(dev, PCI_VENDOR_ID, &vid);
  222. if (vid == 0xffff || vid == 0x0000) {
  223. found_multi = 0;
  224. continue;
  225. }
  226. pci_read_config_dword(dev, PCI_CLASS_REVISION, &pci_class);
  227. if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
  228. int iosave, memsave;
  229. DBG("PCI Autoconfig: Found P2P bridge, device %d\n",
  230. PCI_SLOT(pci_devfn));
  231. /* Allocate PCI I/O and/or memory space */
  232. pciauto_setup_bars(dev, PCI_BASE_ADDRESS_1);
  233. pciauto_prescan_setup_bridge(dev, current_bus, sub_bus,
  234. &iosave, &memsave);
  235. sub_bus = pciauto_bus_scan(pci_ctrl, sub_bus+1);
  236. pciauto_postscan_setup_bridge(dev, current_bus, sub_bus,
  237. &iosave, &memsave);
  238. pciauto_bus.number = current_bus;
  239. continue;
  240. }
  241. #if 0
  242. /* Skip legacy mode IDE controller */
  243. if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
  244. unsigned char prg_iface;
  245. pci_read_config_byte(dev, PCI_CLASS_PROG, &prg_iface);
  246. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
  247. DBG("PCI Autoconfig: Skipping legacy mode "
  248. "IDE controller\n");
  249. continue;
  250. }
  251. }
  252. #endif
  253. /*
  254. * Found a peripheral, enable some standard
  255. * settings
  256. */
  257. pci_read_config_dword(dev, PCI_COMMAND, &cmdstat);
  258. pci_write_config_dword(dev, PCI_COMMAND,
  259. cmdstat |
  260. PCI_COMMAND_IO |
  261. PCI_COMMAND_MEMORY |
  262. PCI_COMMAND_MASTER);
  263. pci_write_config_byte(dev, PCI_LATENCY_TIMER, 0x80);
  264. /* Allocate PCI I/O and/or memory space */
  265. DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
  266. current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn) );
  267. pciauto_setup_bars(dev, PCI_BASE_ADDRESS_5);
  268. pciauto_setup_irq(pci_ctrl, dev, pci_devfn);
  269. }
  270. return sub_bus;
  271. }