pci_auto_old.c 11 KB

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