pci-auto.c 8.4 KB

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