pci_auto_old.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  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. #endif
  91. debug("PCI Autoconfig: BAR %d, %s, size=0x%llx, ",
  92. bar_nr, bar_res == prefetch ? "Prf" : "Mem",
  93. (unsigned long long)bar_size);
  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. #ifdef CONFIG_DM_PCI
  159. /* The root controller has the region information */
  160. struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
  161. pci_mem = ctlr_hose->pci_mem;
  162. pci_prefetch = ctlr_hose->pci_prefetch;
  163. pci_io = ctlr_hose->pci_io;
  164. #else
  165. pci_mem = hose->pci_mem;
  166. pci_prefetch = hose->pci_prefetch;
  167. pci_io = hose->pci_io;
  168. #endif
  169. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
  170. pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
  171. &prefechable_64);
  172. prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
  173. /* Configure bus number registers */
  174. #ifdef CONFIG_DM_PCI
  175. pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev));
  176. pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus);
  177. #else
  178. pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
  179. PCI_BUS(dev) - hose->first_busno);
  180. pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
  181. sub_bus - hose->first_busno);
  182. #endif
  183. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
  184. if (pci_mem) {
  185. /* Round memory allocator to 1MB boundary */
  186. pciauto_region_align(pci_mem, 0x100000);
  187. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  188. pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
  189. (pci_mem->bus_lower & 0xfff00000) >> 16);
  190. cmdstat |= PCI_COMMAND_MEMORY;
  191. }
  192. if (pci_prefetch) {
  193. /* Round memory allocator to 1MB boundary */
  194. pciauto_region_align(pci_prefetch, 0x100000);
  195. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  196. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
  197. (pci_prefetch->bus_lower & 0xfff00000) >> 16);
  198. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
  199. #ifdef CONFIG_SYS_PCI_64BIT
  200. pci_hose_write_config_dword(hose, dev,
  201. PCI_PREF_BASE_UPPER32,
  202. pci_prefetch->bus_lower >> 32);
  203. #else
  204. pci_hose_write_config_dword(hose, dev,
  205. PCI_PREF_BASE_UPPER32,
  206. 0x0);
  207. #endif
  208. cmdstat |= PCI_COMMAND_MEMORY;
  209. } else {
  210. /* We don't support prefetchable memory for now, so disable */
  211. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
  212. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
  213. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
  214. pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
  215. pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
  216. }
  217. }
  218. if (pci_io) {
  219. /* Round I/O allocator to 4KB boundary */
  220. pciauto_region_align(pci_io, 0x1000);
  221. pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
  222. (pci_io->bus_lower & 0x0000f000) >> 8);
  223. pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
  224. (pci_io->bus_lower & 0xffff0000) >> 16);
  225. cmdstat |= PCI_COMMAND_IO;
  226. }
  227. /* Enable memory and I/O accesses, enable bus master */
  228. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  229. cmdstat | PCI_COMMAND_MASTER);
  230. }
  231. void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  232. pci_dev_t dev, int sub_bus)
  233. {
  234. struct pci_region *pci_mem;
  235. struct pci_region *pci_prefetch;
  236. struct pci_region *pci_io;
  237. #ifdef CONFIG_DM_PCI
  238. /* The root controller has the region information */
  239. struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
  240. pci_mem = ctlr_hose->pci_mem;
  241. pci_prefetch = ctlr_hose->pci_prefetch;
  242. pci_io = ctlr_hose->pci_io;
  243. #else
  244. pci_mem = hose->pci_mem;
  245. pci_prefetch = hose->pci_prefetch;
  246. pci_io = hose->pci_io;
  247. #endif
  248. /* Configure bus number registers */
  249. #ifdef CONFIG_DM_PCI
  250. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus);
  251. #else
  252. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
  253. sub_bus - hose->first_busno);
  254. #endif
  255. if (pci_mem) {
  256. /* Round memory allocator to 1MB boundary */
  257. pciauto_region_align(pci_mem, 0x100000);
  258. pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
  259. (pci_mem->bus_lower - 1) >> 16);
  260. }
  261. if (pci_prefetch) {
  262. u16 prefechable_64;
  263. pci_hose_read_config_word(hose, dev,
  264. PCI_PREF_MEMORY_LIMIT,
  265. &prefechable_64);
  266. prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
  267. /* Round memory allocator to 1MB boundary */
  268. pciauto_region_align(pci_prefetch, 0x100000);
  269. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT,
  270. (pci_prefetch->bus_lower - 1) >> 16);
  271. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
  272. #ifdef CONFIG_SYS_PCI_64BIT
  273. pci_hose_write_config_dword(hose, dev,
  274. PCI_PREF_LIMIT_UPPER32,
  275. (pci_prefetch->bus_lower - 1) >> 32);
  276. #else
  277. pci_hose_write_config_dword(hose, dev,
  278. PCI_PREF_LIMIT_UPPER32,
  279. 0x0);
  280. #endif
  281. }
  282. if (pci_io) {
  283. /* Round I/O allocator to 4KB boundary */
  284. pciauto_region_align(pci_io, 0x1000);
  285. pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
  286. ((pci_io->bus_lower - 1) & 0x0000f000) >> 8);
  287. pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
  288. ((pci_io->bus_lower - 1) & 0xffff0000) >> 16);
  289. }
  290. }
  291. /*
  292. * HJF: Changed this to return int. I think this is required
  293. * to get the correct result when scanning bridges
  294. */
  295. int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
  296. {
  297. struct pci_region *pci_mem;
  298. struct pci_region *pci_prefetch;
  299. struct pci_region *pci_io;
  300. unsigned int sub_bus = PCI_BUS(dev);
  301. unsigned short class;
  302. int n;
  303. #ifdef CONFIG_DM_PCI
  304. /* The root controller has the region information */
  305. struct pci_controller *ctlr_hose = pci_bus_to_hose(0);
  306. pci_mem = ctlr_hose->pci_mem;
  307. pci_prefetch = ctlr_hose->pci_prefetch;
  308. pci_io = ctlr_hose->pci_io;
  309. #else
  310. pci_mem = hose->pci_mem;
  311. pci_prefetch = hose->pci_prefetch;
  312. pci_io = hose->pci_io;
  313. #endif
  314. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  315. switch (class) {
  316. case PCI_CLASS_BRIDGE_PCI:
  317. debug("PCI Autoconfig: Found P2P bridge, device %d\n",
  318. PCI_DEV(dev));
  319. pciauto_setup_device(hose, dev, 2, pci_mem,
  320. pci_prefetch, pci_io);
  321. #ifdef CONFIG_DM_PCI
  322. n = dm_pci_hose_probe_bus(hose, dev);
  323. if (n < 0)
  324. return n;
  325. sub_bus = (unsigned int)n;
  326. #else
  327. /* Passing in current_busno allows for sibling P2P bridges */
  328. hose->current_busno++;
  329. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  330. /*
  331. * need to figure out if this is a subordinate bridge on the bus
  332. * to be able to properly set the pri/sec/sub bridge registers.
  333. */
  334. n = pci_hose_scan_bus(hose, hose->current_busno);
  335. /* figure out the deepest we've gone for this leg */
  336. sub_bus = max((unsigned int)n, sub_bus);
  337. pciauto_postscan_setup_bridge(hose, dev, sub_bus);
  338. sub_bus = hose->current_busno;
  339. #endif
  340. break;
  341. case PCI_CLASS_BRIDGE_CARDBUS:
  342. /*
  343. * just do a minimal setup of the bridge,
  344. * let the OS take care of the rest
  345. */
  346. pciauto_setup_device(hose, dev, 0, pci_mem,
  347. pci_prefetch, pci_io);
  348. debug("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
  349. PCI_DEV(dev));
  350. #ifndef CONFIG_DM_PCI
  351. hose->current_busno++;
  352. #endif
  353. break;
  354. #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
  355. case PCI_CLASS_BRIDGE_OTHER:
  356. debug("PCI Autoconfig: Skipping bridge device %d\n",
  357. PCI_DEV(dev));
  358. break;
  359. #endif
  360. #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
  361. case PCI_CLASS_BRIDGE_OTHER:
  362. /*
  363. * The host/PCI bridge 1 seems broken in 8349 - it presents
  364. * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
  365. * device claiming resources io/mem/irq.. we only allow for
  366. * the PIMMR window to be allocated (BAR0 - 1MB size)
  367. */
  368. debug("PCI Autoconfig: Broken bridge found, only minimal config\n");
  369. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  370. hose->pci_prefetch, hose->pci_io);
  371. break;
  372. #endif
  373. case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
  374. debug("PCI AutoConfig: Found PowerPC device\n");
  375. default:
  376. pciauto_setup_device(hose, dev, 6, pci_mem,
  377. pci_prefetch, pci_io);
  378. break;
  379. }
  380. return sub_bus;
  381. }