pci_auto.c 12 KB

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