pci_auto.c 11 KB

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