pci_auto.c 12 KB

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