pci_auto.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /*
  26. *
  27. */
  28. void pciauto_region_init(struct pci_region* res)
  29. {
  30. res->bus_lower = res->bus_start;
  31. }
  32. void pciauto_region_align(struct pci_region *res, unsigned long size)
  33. {
  34. res->bus_lower = ((res->bus_lower - 1) | (size - 1)) + 1;
  35. }
  36. int pciauto_region_allocate(struct pci_region* res, unsigned int size, unsigned int *bar)
  37. {
  38. unsigned long addr;
  39. if (!res)
  40. {
  41. DEBUGF("No resource");
  42. goto error;
  43. }
  44. addr = ((res->bus_lower - 1) | (size - 1)) + 1;
  45. if (addr - res->bus_start + size > res->size)
  46. {
  47. DEBUGF("No room in resource");
  48. goto error;
  49. }
  50. res->bus_lower = addr + size;
  51. DEBUGF("address=0x%lx", addr);
  52. *bar = addr;
  53. return 0;
  54. error:
  55. *bar = 0xffffffff;
  56. return -1;
  57. }
  58. /*
  59. *
  60. */
  61. void pciauto_setup_device(struct pci_controller *hose,
  62. pci_dev_t dev, int bars_num,
  63. struct pci_region *mem,
  64. struct pci_region *io)
  65. {
  66. unsigned int bar_value, bar_response, bar_size;
  67. unsigned int cmdstat = 0;
  68. struct pci_region *bar_res;
  69. int bar, bar_nr = 0;
  70. int found_mem64 = 0;
  71. pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
  72. cmdstat = (cmdstat & ~(PCI_COMMAND_IO | PCI_COMMAND_MEMORY)) | PCI_COMMAND_MASTER;
  73. for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_0 + (bars_num*4); bar += 4)
  74. {
  75. /* Tickle the BAR and get the response */
  76. pci_hose_write_config_dword(hose, dev, bar, 0xffffffff);
  77. pci_hose_read_config_dword(hose, dev, bar, &bar_response);
  78. /* If BAR is not implemented go to the next BAR */
  79. if (!bar_response)
  80. continue;
  81. found_mem64 = 0;
  82. /* Check the BAR type and set our address mask */
  83. if (bar_response & PCI_BASE_ADDRESS_SPACE)
  84. {
  85. bar_size = ~(bar_response & PCI_BASE_ADDRESS_IO_MASK) + 1;
  86. bar_res = io;
  87. DEBUGF("PCI Autoconfig: BAR %d, I/O, size=0x%x, ", bar_nr, bar_size);
  88. }
  89. else
  90. {
  91. if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  92. PCI_BASE_ADDRESS_MEM_TYPE_64)
  93. found_mem64 = 1;
  94. bar_size = ~(bar_response & PCI_BASE_ADDRESS_MEM_MASK) + 1;
  95. bar_res = mem;
  96. DEBUGF("PCI Autoconfig: BAR %d, Mem, size=0x%x, ", bar_nr, bar_size);
  97. }
  98. if (pciauto_region_allocate(bar_res, bar_size, &bar_value) == 0)
  99. {
  100. /* Write it out and update our limit */
  101. pci_hose_write_config_dword(hose, dev, bar, bar_value);
  102. /*
  103. * If we are a 64-bit decoder then increment to the
  104. * upper 32 bits of the bar and force it to locate
  105. * in the lower 4GB of memory.
  106. */
  107. if (found_mem64)
  108. {
  109. bar += 4;
  110. pci_hose_write_config_dword(hose, dev, bar, 0x00000000);
  111. }
  112. cmdstat |= (bar_response & PCI_BASE_ADDRESS_SPACE) ?
  113. PCI_COMMAND_IO : PCI_COMMAND_MEMORY;
  114. }
  115. DEBUGF("\n");
  116. bar_nr++;
  117. }
  118. pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat);
  119. pci_hose_write_config_byte(hose, dev, PCI_CACHE_LINE_SIZE, 0x08);
  120. pci_hose_write_config_byte(hose, dev, PCI_LATENCY_TIMER, 0x80);
  121. }
  122. static void pciauto_prescan_setup_bridge(struct pci_controller *hose,
  123. pci_dev_t dev, int sub_bus)
  124. {
  125. struct pci_region *pci_mem = hose->pci_mem;
  126. struct pci_region *pci_io = hose->pci_io;
  127. unsigned int cmdstat;
  128. pci_hose_read_config_dword(hose, dev, PCI_COMMAND, &cmdstat);
  129. /* Configure bus number registers */
  130. pci_hose_write_config_byte(hose, dev, PCI_PRIMARY_BUS, PCI_BUS(dev));
  131. /* TBS: passed in sub_bus is correct, removed the +1 */
  132. pci_hose_write_config_byte(hose, dev, PCI_SECONDARY_BUS, sub_bus);
  133. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, 0xff);
  134. if (pci_mem)
  135. {
  136. /* Round memory allocator to 1MB boundary */
  137. pciauto_region_align(pci_mem, 0x100000);
  138. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  139. pci_hose_write_config_word(hose, dev, PCI_MEMORY_BASE,
  140. (pci_mem->bus_lower & 0xfff00000) >> 16);
  141. cmdstat |= PCI_COMMAND_MEMORY;
  142. }
  143. if (pci_io)
  144. {
  145. /* Round I/O allocator to 4KB boundary */
  146. pciauto_region_align(pci_io, 0x1000);
  147. pci_hose_write_config_byte(hose, dev, PCI_IO_BASE,
  148. (pci_io->bus_lower & 0x0000f000) >> 8);
  149. pci_hose_write_config_word(hose, dev, PCI_IO_BASE_UPPER16,
  150. (pci_io->bus_lower & 0xffff0000) >> 16);
  151. cmdstat |= PCI_COMMAND_IO;
  152. }
  153. /* We don't support prefetchable memory for now, so disable */
  154. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_BASE, 0x1000);
  155. pci_hose_write_config_word(hose, dev, PCI_PREF_MEMORY_LIMIT, 0x1000);
  156. /* Enable memory and I/O accesses, enable bus master */
  157. pci_hose_write_config_dword(hose, dev, PCI_COMMAND, cmdstat | PCI_COMMAND_MASTER);
  158. }
  159. static void pciauto_postscan_setup_bridge(struct pci_controller *hose,
  160. pci_dev_t dev, int sub_bus)
  161. {
  162. struct pci_region *pci_mem = hose->pci_mem;
  163. struct pci_region *pci_io = hose->pci_io;
  164. /* Configure bus number registers */
  165. pci_hose_write_config_byte(hose, dev, PCI_SUBORDINATE_BUS, sub_bus);
  166. if (pci_mem)
  167. {
  168. /* Round memory allocator to 1MB boundary */
  169. pciauto_region_align(pci_mem, 0x100000);
  170. pci_hose_write_config_word(hose, dev, PCI_MEMORY_LIMIT,
  171. (pci_mem->bus_lower-1) >> 16);
  172. }
  173. if (pci_io)
  174. {
  175. /* Round I/O allocator to 4KB boundary */
  176. pciauto_region_align(pci_io, 0x1000);
  177. pci_hose_write_config_byte(hose, dev, PCI_IO_LIMIT,
  178. ((pci_io->bus_lower-1) & 0x0000f000) >> 8);
  179. pci_hose_write_config_word(hose, dev, PCI_IO_LIMIT_UPPER16,
  180. ((pci_io->bus_lower-1) & 0xffff0000) >> 16);
  181. }
  182. }
  183. /*
  184. *
  185. */
  186. void pciauto_config_init(struct pci_controller *hose)
  187. {
  188. int i;
  189. hose->pci_io = hose->pci_mem = NULL;
  190. for (i=0; i<hose->region_count; i++)
  191. {
  192. switch(hose->regions[i].flags)
  193. {
  194. case PCI_REGION_IO:
  195. if (!hose->pci_io ||
  196. hose->pci_io->size < hose->regions[i].size)
  197. hose->pci_io = hose->regions + i;
  198. break;
  199. case PCI_REGION_MEM:
  200. if (!hose->pci_mem ||
  201. hose->pci_mem->size < hose->regions[i].size)
  202. hose->pci_mem = hose->regions + i;
  203. break;
  204. }
  205. }
  206. if (hose->pci_mem)
  207. {
  208. pciauto_region_init(hose->pci_mem);
  209. DEBUGF("PCI Autoconfig: Memory region: [%lx-%lx]\n",
  210. hose->pci_mem->bus_start,
  211. hose->pci_mem->bus_start + hose->pci_mem->size - 1);
  212. }
  213. if (hose->pci_io)
  214. {
  215. pciauto_region_init(hose->pci_io);
  216. DEBUGF("PCI Autoconfig: I/O region: [%lx-%lx]\n",
  217. hose->pci_io->bus_start,
  218. hose->pci_io->bus_start + hose->pci_io->size - 1);
  219. }
  220. }
  221. /* HJF: Changed this to return int. I think this is required
  222. * to get the correct result when scanning bridges
  223. */
  224. int pciauto_config_device(struct pci_controller *hose, pci_dev_t dev)
  225. {
  226. unsigned int sub_bus = PCI_BUS(dev);
  227. unsigned short class;
  228. unsigned char prg_iface;
  229. int n;
  230. pci_hose_read_config_word(hose, dev, PCI_CLASS_DEVICE, &class);
  231. switch(class)
  232. {
  233. case PCI_CLASS_BRIDGE_PCI:
  234. hose->current_busno++;
  235. pciauto_setup_device(hose, dev, 2, hose->pci_mem, hose->pci_io);
  236. DEBUGF("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_DEV(dev));
  237. /* TBS: Passing in current_busno allows for sibling P2P bridges */
  238. pciauto_prescan_setup_bridge(hose, dev, hose->current_busno);
  239. /*
  240. * TBS: need to figure out if this is a subordinate bridge on the bus
  241. * to be able to properly set the pri/sec/sub bridge registers.
  242. */
  243. n = pci_hose_scan_bus(hose, hose->current_busno);
  244. /* TBS: figure out the deepest we've gone for this leg */
  245. sub_bus = max(n, sub_bus);
  246. pciauto_postscan_setup_bridge(hose, dev, sub_bus);
  247. sub_bus = hose->current_busno;
  248. break;
  249. case PCI_CLASS_STORAGE_IDE:
  250. pci_hose_read_config_byte(hose, dev, PCI_CLASS_PROG, &prg_iface);
  251. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK))
  252. {
  253. DEBUGF("PCI Autoconfig: Skipping legacy mode IDE controller\n");
  254. return sub_bus;
  255. }
  256. pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_io);
  257. break;
  258. case PCI_CLASS_BRIDGE_CARDBUS:
  259. /* just do a minimal setup of the bridge, let the OS take care of the rest */
  260. pciauto_setup_device(hose, dev, 0, hose->pci_mem, hose->pci_io);
  261. DEBUGF("PCI Autoconfig: Found P2CardBus bridge, device %d\n",
  262. PCI_DEV(dev));
  263. hose->current_busno++;
  264. break;
  265. #ifdef CONFIG_MPC5200
  266. case PCI_CLASS_BRIDGE_OTHER:
  267. DEBUGF("PCI Autoconfig: Skipping bridge device %d\n",
  268. PCI_DEV(dev));
  269. break;
  270. #endif
  271. default:
  272. pciauto_setup_device(hose, dev, 6, hose->pci_mem, hose->pci_io);
  273. break;
  274. }
  275. return sub_bus;
  276. }
  277. #endif /* CONFIG_PCI */