pci_auto.c 11 KB

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