pci_auto.c 14 KB

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