pci_auto.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  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. #undef DEBUG
  16. #ifdef DEBUG
  17. #define DEBUGF(x...) printf(x)
  18. #else
  19. #define DEBUGF(x...)
  20. #endif /* DEBUG */
  21. #define PCIAUTO_IDE_MODE_MASK 0x05
  22. /* the user can define CONFIG_SYS_PCI_CACHE_LINE_SIZE to avoid problems */
  23. #ifndef CONFIG_SYS_PCI_CACHE_LINE_SIZE
  24. #define CONFIG_SYS_PCI_CACHE_LINE_SIZE 8
  25. #endif
  26. /*
  27. *
  28. */
  29. void pciauto_region_init(struct pci_region *res)
  30. {
  31. /*
  32. * Avoid allocating PCI resources from address 0 -- this is illegal
  33. * according to PCI 2.1 and moreover, this is known to cause Linux IDE
  34. * drivers to fail. Use a reasonable starting value of 0x1000 instead.
  35. */
  36. res->bus_lower = res->bus_start ? res->bus_start : 0x1000;
  37. }
  38. void pciauto_region_align(struct pci_region *res, pci_size_t size)
  39. {
  40. res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
  41. }
  42. int pciauto_region_allocate(struct pci_region *res, pci_size_t size,
  43. pci_addr_t *bar)
  44. {
  45. pci_addr_t addr;
  46. if (!res) {
  47. DEBUGF("No resource");
  48. goto error;
  49. }
  50. addr = ((res->bus_lower - 1) | (size - 1)) + 1;
  51. if (addr - res->bus_start + size > res->size) {
  52. DEBUGF("No room in resource");
  53. goto error;
  54. }
  55. res->bus_lower = addr + size;
  56. DEBUGF("address=0x%llx bus_lower=0x%llx", (u64)addr, (u64)res->bus_lower);
  57. *bar = addr;
  58. return 0;
  59. error:
  60. *bar = (pci_addr_t)-1;
  61. return -1;
  62. }
  63. /*
  64. *
  65. */
  66. void pciauto_setup_device(struct pci_controller *hose,
  67. pci_dev_t dev, int bars_num,
  68. struct pci_region *mem,
  69. struct pci_region *prefetch,
  70. struct pci_region *io)
  71. {
  72. u32 bar_response;
  73. pci_size_t bar_size;
  74. u16 cmdstat = 0;
  75. int bar, bar_nr = 0;
  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. pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
  156. pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE,
  157. CONFIG_SYS_PCI_CACHE_LINE_SIZE);
  158. pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
  159. }
  160. int pciauto_setup_rom(struct pci_controller *hose, pci_dev_t dev)
  161. {
  162. pci_addr_t bar_value;
  163. pci_size_t bar_size;
  164. u32 bar_response;
  165. u16 cmdstat = 0;
  166. pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS, 0xfffffffe);
  167. pci_hose_read_config_dword(hose, dev, PCI_ROM_ADDRESS, &bar_response);
  168. if (!bar_response)
  169. return -ENOENT;
  170. bar_size = -(bar_response & ~1);
  171. DEBUGF("PCI Autoconfig: ROM, size=%#x, ", bar_size);
  172. if (pciauto_region_allocate(hose->pci_mem, bar_size, &bar_value) == 0) {
  173. pci_hose_write_config_dword(hose, dev, PCI_ROM_ADDRESS,
  174. bar_value);
  175. }
  176. DEBUGF("\n");
  177. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
  178. cmdstat |= PCI_COMMAND_IO | PCI_COMMAND_MEMORY | PCI_COMMAND_MASTER;
  179. pci_hose_write_config_word(hose, dev, PCI_COMMAND, cmdstat);
  180. return 0;
  181. }
  182. void pciauto_prescan_setup_bridge(struct pci_controller *hose,
  183. pci_dev_t dev, int sub_bus)
  184. {
  185. struct pci_region *pci_mem = hose->pci_mem;
  186. struct pci_region *pci_prefetch = hose->pci_prefetch;
  187. struct pci_region *pci_io = hose->pci_io;
  188. u16 cmdstat, prefechable_64;
  189. pci_hose_read_config_word(hose, dev, PCI_COMMAND, &cmdstat);
  190. pci_hose_read_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
  191. &prefechable_64);
  192. prefechable_64 &= PCI_PREF_RANGE_TYPE_MASK;
  193. /* Configure bus number registers */
  194. pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS,
  195. PCI_BUS(dev) - hose->first_busno);
  196. pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS,
  197. sub_bus - hose->first_busno);
  198. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
  199. if (pci_mem) {
  200. /* Round memory allocator to 1MB boundary */
  201. pciauto_region_align(pci_mem, 0x100000);
  202. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  203. pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
  204. (pci_mem->bus_lower & 0xfff00000) >> 16);
  205. cmdstat |= PCI_COMMAND_MEMORY;
  206. }
  207. if (pci_prefetch) {
  208. /* Round memory allocator to 1MB boundary */
  209. pciauto_region_align(pci_prefetch, 0x100000);
  210. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  211. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE,
  212. (pci_prefetch->bus_lower & 0xfff00000) >> 16);
  213. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64)
  214. #ifdef CONFIG_SYS_PCI_64BIT
  215. pci_hose_write_config_dword(hose, dev,
  216. PCI_PREF_BASE_UPPER32,
  217. pci_prefetch->bus_lower >> 32);
  218. #else
  219. pci_hose_write_config_dword(hose, dev,
  220. PCI_PREF_BASE_UPPER32,
  221. 0x0);
  222. #endif
  223. cmdstat |= PCI_COMMAND_MEMORY;
  224. } else {
  225. /* We don't support prefetchable memory for now, so disable */
  226. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
  227. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x0);
  228. if (prefechable_64 == PCI_PREF_RANGE_TYPE_64) {
  229. pci_hose_write_config_word(hose, dev, PCI_PREF_BASE_UPPER32, 0x0);
  230. pci_hose_write_config_word(hose, dev, PCI_PREF_LIMIT_UPPER32, 0x0);
  231. }
  232. }
  233. if (pci_io) {
  234. /* Round I/O allocator to 4KB boundary */
  235. pciauto_region_align(pci_io, 0x1000);
  236. pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
  237. (pci_io->bus_lower & 0x0000f000) >> 8);
  238. pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
  239. (pci_io->bus_lower & 0xffff0000) >> 16);
  240. cmdstat |= PCI_COMMAND_IO;
  241. }
  242. /* Enable memory and I/O accesses, enable bus master */
  243. pci_hose_write_config_word(hose, dev, PCI_COMMAND,
  244. cmdstat | PCI_COMMAND_MASTER);
  245. }
  246. void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  247. pci_dev_t dev, int sub_bus)
  248. {
  249. struct pci_region *pci_mem = hose->pci_mem;
  250. struct pci_region *pci_prefetch = hose->pci_prefetch;
  251. struct pci_region *pci_io = hose->pci_io;
  252. /* Configure bus number registers */
  253. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS,
  254. sub_bus - hose->first_busno);
  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. *
  293. */
  294. void pciauto_config_init(struct pci_controller *hose)
  295. {
  296. int i;
  297. hose->pci_io = hose->pci_mem = hose->pci_prefetch = NULL;
  298. for (i = 0; i < hose->region_count; i++) {
  299. switch(hose->regions[i].flags) {
  300. case PCI_REGION_IO:
  301. if (!hose->pci_io ||
  302. hose->pci_io->size < hose->regions[i].size)
  303. hose->pci_io = hose->regions + i;
  304. break;
  305. case PCI_REGION_MEM:
  306. if (!hose->pci_mem ||
  307. hose->pci_mem->size < hose->regions[i].size)
  308. hose->pci_mem = hose->regions + i;
  309. break;
  310. case (PCI_REGION_MEM | PCI_REGION_PREFETCH):
  311. if (!hose->pci_prefetch ||
  312. hose->pci_prefetch->size < hose->regions[i].size)
  313. hose->pci_prefetch = hose->regions + i;
  314. break;
  315. }
  316. }
  317. if (hose->pci_mem) {
  318. pciauto_region_init(hose->pci_mem);
  319. DEBUGF("PCI Autoconfig: Bus Memory region: [0x%llx-0x%llx],\n"
  320. "\t\tPhysical Memory [%llx-%llxx]\n",
  321. (u64)hose->pci_mem->bus_start,
  322. (u64)(hose->pci_mem->bus_start + hose->pci_mem->size - 1),
  323. (u64)hose->pci_mem->phys_start,
  324. (u64)(hose->pci_mem->phys_start + hose->pci_mem->size - 1));
  325. }
  326. if (hose->pci_prefetch) {
  327. pciauto_region_init(hose->pci_prefetch);
  328. DEBUGF("PCI Autoconfig: Bus Prefetchable Mem: [0x%llx-0x%llx],\n"
  329. "\t\tPhysical Memory [%llx-%llx]\n",
  330. (u64)hose->pci_prefetch->bus_start,
  331. (u64)(hose->pci_prefetch->bus_start +
  332. hose->pci_prefetch->size - 1),
  333. (u64)hose->pci_prefetch->phys_start,
  334. (u64)(hose->pci_prefetch->phys_start +
  335. hose->pci_prefetch->size - 1));
  336. }
  337. if (hose->pci_io) {
  338. pciauto_region_init(hose->pci_io);
  339. DEBUGF("PCI Autoconfig: Bus I/O region: [0x%llx-0x%llx],\n"
  340. "\t\tPhysical Memory: [%llx-%llx]\n",
  341. (u64)hose->pci_io->bus_start,
  342. (u64)(hose->pci_io->bus_start + hose->pci_io->size - 1),
  343. (u64)hose->pci_io->phys_start,
  344. (u64)(hose->pci_io->phys_start + hose->pci_io->size - 1));
  345. }
  346. }
  347. /*
  348. * HJF: Changed this to return int. I think this is required
  349. * to get the correct result when scanning bridges
  350. */
  351. int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
  352. {
  353. unsigned int sub_bus = PCI_BUS(dev);
  354. unsigned short class;
  355. unsigned char prg_iface;
  356. int n;
  357. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  358. switch (class) {
  359. case PCI_CLASS_BRIDGE_PCI:
  360. DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n",
  361. PCI_DEV(dev));
  362. pciauto_setup_device(hose, dev, 2, hose->pci_mem,
  363. hose->pci_prefetch, hose->pci_io);
  364. #ifdef CONFIG_DM_PCI
  365. n = dm_pci_hose_probe_bus(hose, dev);
  366. if (n < 0)
  367. return n;
  368. sub_bus = (unsigned int)n;
  369. #else
  370. /* Passing in current_busno allows for sibling P2P bridges */
  371. hose->current_busno++;
  372. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  373. /*
  374. * need to figure out if this is a subordinate bridge on the bus
  375. * to be able to properly set the pri/sec/sub bridge registers.
  376. */
  377. n = pci_hose_scan_bus(hose, hose->current_busno);
  378. /* figure out the deepest we've gone for this leg */
  379. sub_bus = max((unsigned int)n, sub_bus);
  380. pciauto_postscan_setup_bridge(hose, dev, sub_bus);
  381. sub_bus = hose->current_busno;
  382. #endif
  383. break;
  384. case PCI_CLASS_STORAGE_IDE:
  385. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
  386. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
  387. DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
  388. return sub_bus;
  389. }
  390. pciauto_setup_device(hose, dev, 6, hose->pci_mem,
  391. hose->pci_prefetch, hose->pci_io);
  392. break;
  393. case PCI_CLASS_BRIDGE_CARDBUS:
  394. /*
  395. * just do a minimal setup of the bridge,
  396. * let the OS take care of the rest
  397. */
  398. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  399. hose->pci_prefetch, hose->pci_io);
  400. DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
  401. PCI_DEV(dev));
  402. #ifndef CONFIG_DM_PCI
  403. hose->current_busno++;
  404. #endif
  405. break;
  406. #if defined(CONFIG_PCIAUTO_SKIP_HOST_BRIDGE)
  407. case PCI_CLASS_BRIDGE_OTHER:
  408. DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
  409. PCI_DEV(dev));
  410. break;
  411. #endif
  412. #if defined(CONFIG_MPC834x) && !defined(CONFIG_VME8349)
  413. case PCI_CLASS_BRIDGE_OTHER:
  414. /*
  415. * The host/PCI bridge 1 seems broken in 8349 - it presents
  416. * itself as 'PCI_CLASS_BRIDGE_OTHER' and appears as an _agent_
  417. * device claiming resources io/mem/irq.. we only allow for
  418. * the PIMMR window to be allocated (BAR0 - 1MB size)
  419. */
  420. DEBUGF("PCI Autoconfig: Broken bridge found, only minimal config\n");
  421. pciauto_setup_device(hose, dev, 0, hose->pci_mem,
  422. hose->pci_prefetch, hose->pci_io);
  423. break;
  424. #endif
  425. case PCI_CLASS_PROCESSOR_POWERPC: /* an agent or end-point */
  426. DEBUGF("PCI AutoConfig: Found PowerPC device\n");
  427. default:
  428. pciauto_setup_device(hose, dev, 6, hose->pci_mem,
  429. hose->pci_prefetch, hose->pci_io);
  430. break;
  431. }
  432. return sub_bus;
  433. }