pci_auto.c 12 KB

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