pci_auto.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * PCI autoconfiguration library
  3. *
  4. * Author: Matt Porter <mporter@mvista.com>
  5. *
  6. * Copyright 2000 MontaVista Software Inc.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <dm.h>
  12. #include <errno.h>
  13. #include <pci.h>
  14. /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
  15. #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
  16. #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
  17. #endif
  18. void dm_pciauto_setup_device(struct udevice *dev, int bars_num,
  19. struct pci_region *mem,
  20. struct pci_region *prefetch, struct pci_region *io,
  21. bool enum_only)
  22. {
  23. u32 bar_response;
  24. pci_size_t bar_size;
  25. u16 cmdstat = 0;
  26. int bar, bar_nr = 0;
  27. u8 header_type;
  28. int rom_addr;
  29. pci_addr_t bar_value;
  30. struct pci_region *bar_res = NULL;
  31. int found_mem64 = 0;
  32. u16 class;
  33. dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
  34. cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) |
  35. PCI_COMMAND_MASTER;
  36. for (bar = PCI_BASE_ADDRESS_0;
  37. bar < PCI_BASE_ADDRESS_0 + (bars_num * 4); bar += 4) {
  38. /* Tickle the BAR and get the response */
  39. if (!enum_only)
  40. dm_pci_write_config32(dev, bar, 0xffffffff);
  41. dm_pci_read_config32(dev, bar, &bar_response);
  42. /* If BAR is not implemented go to the next BAR */
  43. if (!bar_response)
  44. continue;
  45. found_mem64 = 0;
  46. /* Check the BAR type and set our address mask */
  47. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  48. bar_size = ((~(bar_response & PCI_BASE_ADDRESS_IO_MASK))
  49. & 0xffff) + 1;
  50. if (!enum_only)
  51. bar_res = io;
  52. debug("PCI Autoconfig: BAR %d, I/O, size=0x%llx, ",
  53. bar_nr, (unsigned long long)bar_size);
  54. } else {
  55. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  56. PCI_BASE_ADDRESS_MEM_TYPE_64) {
  57. u32 bar_response_upper;
  58. u64 bar64;
  59. if (!enum_only) {
  60. dm_pci_write_config32(dev, bar + 4,
  61. 0xffffffff);
  62. }
  63. dm_pci_read_config32(dev, bar + 4,
  64. &bar_response_upper);
  65. bar64 = ((u64)bar_response_upper << 32) |
  66. bar_response;
  67. bar_size = ~(bar64 & PCI_BASE_ADDRESS_MEM_MASK)
  68. + 1;
  69. if (!enum_only)
  70. found_mem64 = 1;
  71. } else {
  72. bar_size = (u32)(~(bar_response &
  73. PCI_BASE_ADDRESS_MEM_MASK) + 1);
  74. }
  75. if (!enum_only) {
  76. if (prefetch && (bar_response &
  77. PCI_BASE_ADDRESS_MEM_PREFETCH)) {
  78. bar_res = prefetch;
  79. } else {
  80. bar_res = mem;
  81. }
  82. }
  83. debug("PCI Autoconfig: BAR %d, %s, size=0x%llx, ",
  84. bar_nr, bar_res == prefetch ? "Prf" : "Mem",
  85. (unsigned long long)bar_size);
  86. }
  87. if (!enum_only && pciauto_region_allocate(bar_res, bar_size,
  88. &bar_value) == 0) {
  89. /* Write it out and update our limit */
  90. dm_pci_write_config32(dev, bar, (u32)bar_value);
  91. if (found_mem64) {
  92. bar += 4;
  93. #ifdef CONFIG_SYS_PCI_64BIT
  94. dm_pci_write_config32(dev, bar,
  95. (u32)(bar_value >> 32));
  96. #else
  97. /*
  98. * If we are a 64-bit decoder then increment to
  99. * the upper 32 bits of the bar and force it to
  100. * locate in the lower 4GB of memory.
  101. */
  102. dm_pci_write_config32(dev, bar, 0x00000000);
  103. #endif
  104. }
  105. }
  106. cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
  107. PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
  108. debug("\n");
  109. bar_nr++;
  110. }
  111. if (!enum_only) {
  112. /* Configure the expansion ROM address */
  113. dm_pci_read_config8(dev, PCI_HEADER_TYPE, &header_type);
  114. header_type &= 0x7f;
  115. if (header_type != PCI_HEADER_TYPE_CARDBUS) {
  116. rom_addr = (header_type == PCI_HEADER_TYPE_NORMAL) ?
  117. PCI_ROM_ADDRESS : PCI_ROM_ADDRESS1;
  118. dm_pci_write_config32(dev, rom_addr, 0xfffffffe);
  119. dm_pci_read_config32(dev, rom_addr, &bar_response);
  120. if (bar_response) {
  121. bar_size = -(bar_response & ~1);
  122. debug("PCI Autoconfig: ROM, size=%#x, ",
  123. (unsigned int)bar_size);
  124. if (pciauto_region_allocate(mem, bar_size,
  125. &bar_value) == 0) {
  126. dm_pci_write_config32(dev, rom_addr,
  127. bar_value);
  128. }
  129. cmdstat |= PCI_COMMAND_MEMORY;
  130. debug("\n");
  131. }
  132. }
  133. }
  134. /* PCI_COMMAND_IO must be set for VGA device */
  135. dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
  136. if (class == PCI_CLASS_DISPLAY_VGA)
  137. cmdstat |= PCI_COMMAND_IO;
  138. dm_pci_write_config16(dev, PCI_COMMAND, cmdstat);
  139. dm_pci_write_config8(dev, PCI_CACHE_LINE_SIZE,
  140. CONFIG_SYS_PCI_CACHE_LINE_SIZE);
  141. dm_pci_write_config8(dev, PCI_LATENCY_TIMER, 0x80);
  142. }
  143. void dm_pciauto_prescan_setup_bridge(struct udevice *dev, int sub_bus)
  144. {
  145. struct pci_region *pci_mem;
  146. struct pci_region *pci_prefetch;
  147. struct pci_region *pci_io;
  148. u16 cmdstat, prefechable_64;
  149. struct udevice *ctlr = pci_get_controller(dev);
  150. struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
  151. pci_mem = ctlr_hose->pci_mem;
  152. pci_prefetch = ctlr_hose->pci_prefetch;
  153. pci_io = ctlr_hose->pci_io;
  154. dm_pci_read_config16(dev, PCI_COMMAND, &cmdstat);
  155. dm_pci_read_config16(dev, PCI_PREF_MEMORY_BASE, &prefechable_64);
  156. prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
  157. /* Configure bus number registers */
  158. dm_pci_write_config8(dev, PCI_PRIMARY_BUS,
  159. PCI_BUS(dm_pci_get_bdf(dev)));
  160. dm_pci_write_config8(dev, PCI_SECONDARY_BUS, sub_bus);
  161. dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, 0xff);
  162. if (pci_mem) {
  163. /* Round memory allocator to 1MB boundary */
  164. pciauto_region_align(pci_mem, 0x100000);
  165. /*
  166. * Set up memory and I/O filter limits, assume 32-bit
  167. * I/O space
  168. */
  169. dm_pci_write_config16(dev, PCI_MEMORY_BASE,
  170. (pci_mem->bus_lower & 0xfff00000) >> 16);
  171. cmdstat |= PCI_COMMAND_MEMORY;
  172. }
  173. if (pci_prefetch) {
  174. /* Round memory allocator to 1MB boundary */
  175. pciauto_region_align(pci_prefetch, 0x100000);
  176. /*
  177. * Set up memory and I/O filter limits, assume 32-bit
  178. * I/O space
  179. */
  180. dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE,
  181. (pci_prefetch->bus_lower & 0xfff00000) >> 16);
  182. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
  183. #ifdef CONFIG_SYS_PCI_64BIT
  184. dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32,
  185. pci_prefetch->bus_lower >> 32);
  186. #else
  187. dm_pci_write_config32(dev, PCI_PREF_BASE_UPPER32, 0x0);
  188. #endif
  189. cmdstat |= PCI_COMMAND_MEMORY;
  190. } else {
  191. /* We don't support prefetchable memory for now, so disable */
  192. dm_pci_write_config16(dev, PCI_PREF_MEMORY_BASE, 0x1000);
  193. dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT, 0x0);
  194. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
  195. dm_pci_write_config16(dev, PCI_PREF_BASE_UPPER32, 0x0);
  196. dm_pci_write_config16(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
  197. }
  198. }
  199. if (pci_io) {
  200. /* Round I/O allocator to 4KB boundary */
  201. pciauto_region_align(pci_io, 0x1000);
  202. dm_pci_write_config8(dev, PCI_IO_BASE,
  203. (pci_io->bus_lower & 0x0000f000) >> 8);
  204. dm_pci_write_config16(dev, PCI_IO_BASE_UPPER16,
  205. (pci_io->bus_lower & 0xffff0000) >> 16);
  206. cmdstat |= PCI_COMMAND_IO;
  207. }
  208. /* Enable memory and I/O accesses, enable bus master */
  209. dm_pci_write_config16(dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
  210. }
  211. void dm_pciauto_postscan_setup_bridge(struct udevice *dev, int sub_bus)
  212. {
  213. struct pci_region *pci_mem;
  214. struct pci_region *pci_prefetch;
  215. struct pci_region *pci_io;
  216. struct udevice *ctlr = pci_get_controller(dev);
  217. struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
  218. pci_mem = ctlr_hose->pci_mem;
  219. pci_prefetch = ctlr_hose->pci_prefetch;
  220. pci_io = ctlr_hose->pci_io;
  221. /* Configure bus number registers */
  222. dm_pci_write_config8(dev, PCI_SUBORDINATE_BUS, sub_bus);
  223. if (pci_mem) {
  224. /* Round memory allocator to 1MB boundary */
  225. pciauto_region_align(pci_mem, 0x100000);
  226. dm_pci_write_config16(dev, PCI_MEMORY_LIMIT,
  227. (pci_mem->bus_lower - 1) >> 16);
  228. }
  229. if (pci_prefetch) {
  230. u16 prefechable_64;
  231. dm_pci_read_config16(dev, PCI_PREF_MEMORY_LIMIT,
  232. &prefechable_64);
  233. prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
  234. /* Round memory allocator to 1MB boundary */
  235. pciauto_region_align(pci_prefetch, 0x100000);
  236. dm_pci_write_config16(dev, PCI_PREF_MEMORY_LIMIT,
  237. (pci_prefetch->bus_lower - 1) >> 16);
  238. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
  239. #ifdef CONFIG_SYS_PCI_64BIT
  240. dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32,
  241. (pci_prefetch->bus_lower - 1) >> 32);
  242. #else
  243. dm_pci_write_config32(dev, PCI_PREF_LIMIT_UPPER32, 0x0);
  244. #endif
  245. }
  246. if (pci_io) {
  247. /* Round I/O allocator to 4KB boundary */
  248. pciauto_region_align(pci_io, 0x1000);
  249. dm_pci_write_config8(dev, PCI_IO_LIMIT,
  250. ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
  251. dm_pci_write_config16(dev, PCI_IO_LIMIT_UPPER16,
  252. ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
  253. }
  254. }
  255. /*
  256. * HJF: Changed this to return int. I think this is required
  257. * to get the correct result when scanning bridges
  258. */
  259. int dm_pciauto_config_device(struct udevice *dev)
  260. {
  261. struct pci_region *pci_mem;
  262. struct pci_region *pci_prefetch;
  263. struct pci_region *pci_io;
  264. unsigned int sub_bus = PCI_BUS(dm_pci_get_bdf(dev));
  265. unsigned short class;
  266. bool enum_only = false;
  267. struct udevice *ctlr = pci_get_controller(dev);
  268. struct pci_controller *ctlr_hose = dev_get_uclass_priv(ctlr);
  269. int n;
  270. #ifdef CONFIG_PCI_ENUM_ONLY
  271. enum_only = true;
  272. #endif
  273. pci_mem = ctlr_hose->pci_mem;
  274. pci_prefetch = ctlr_hose->pci_prefetch;
  275. pci_io = ctlr_hose->pci_io;
  276. dm_pci_read_config16(dev, PCI_CLASS_DEVICE, &class);
  277. switch (class) {
  278. case PCI_CLASS_BRIDGE_PCI:
  279. debug("PCI Autoconfig: Found P2P bridge, device %d\n",
  280. PCI_DEV(dm_pci_get_bdf(dev)));
  281. dm_pciauto_setup_device(dev, 2, pci_mem, pci_prefetch, pci_io,
  282. enum_only);
  283. n = dm_pci_hose_probe_bus(dev);
  284. if (n < 0)
  285. return n;
  286. sub_bus = (unsigned int)n;
  287. break;
  288. case PCI_CLASS_BRIDGE_CARDBUS:
  289. /*
  290. * just do a minimal setup of the bridge,
  291. * let the OS take care of the rest
  292. */
  293. dm_pciauto_setup_device(dev, 0, pci_mem, pci_prefetch, pci_io,
  294. enum_only);
  295. debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
  296. PCI_DEV(dm_pci_get_bdf(dev)));
  297. break;
  298. #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
  299. case PCI_CLASS_BRIDGE_OTHER:
  300. debug("PCI Autoconfig: Skipping bridge device %d\n",
  301. PCI_DEV(dm_pci_get_bdf(dev)));
  302. break;
  303. #endif
  304. #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
  305. case PCI_CLASS_BRIDGE_OTHER:
  306. /*
  307. * The host/PCI bridge 1 seems broken in 8349 - it presents
  308. * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
  309. * device claiming resources io/mem/irq.. we only allow for
  310. * the PIMMR window to be allocated (BAR0 - 1MB size)
  311. */
  312. debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
  313. dm_pciauto_setup_device(dev, 0, hose->pci_mem,
  314. hose->pci_prefetch, hose->pci_io,
  315. enum_only);
  316. break;
  317. #endif
  318. case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
  319. debug("PCI AutoConfig: Found PowerPC device\n");
  320. /* fall through */
  321. default:
  322. dm_pciauto_setup_device(dev, 6, pci_mem, pci_prefetch, pci_io,
  323. enum_only);
  324. break;
  325. }
  326. return sub_bus;
  327. }