pci_auto.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  1. /*
  2. * PCI autoconfiguration library
  3. *
  4. * Author: Matt Porter <mporter@mvista.com>
  5. *
  6. * 2001 (c) MontaVista, Software, Inc. This file is licensed under
  7. * the terms of the GNU General Public License version 2. This program
  8. * is licensed "as is" without any warranty of any kind, whether express
  9. * or implied.
  10. */
  11. /*
  12. * The CardBus support is very preliminary. Preallocating space is
  13. * the way to go but will require some change in card services to
  14. * make it useful. Eventually this will ensure that we can put
  15. * multiple CB bridges behind multiple P2P bridges. For now, at
  16. * least it ensures that we place the CB bridge BAR and assigned
  17. * initial bus numbers. I definitely need to do something about
  18. * the lack of 16-bit I/O support. -MDP
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/init.h>
  22. #include <linux/pci.h>
  23. #include <asm/pci-bridge.h>
  24. #define PCIAUTO_IDE_MODE_MASK 0x05
  25. #undef DEBUG
  26. #ifdef DEBUG
  27. #define DBG(x...) printk(x)
  28. #else
  29. #define DBG(x...)
  30. #endif /* DEBUG */
  31. static int pciauto_upper_iospc;
  32. static int pciauto_upper_memspc;
  33. void __init pciauto_setup_bars(struct pci_controller *hose,
  34. int current_bus,
  35. int pci_devfn,
  36. int bar_limit)
  37. {
  38. int bar_response, bar_size, bar_value;
  39. int bar, addr_mask;
  40. int * upper_limit;
  41. int found_mem64 = 0;
  42. DBG("PCI Autoconfig: Found Bus %d, Device %d, Function %d\n",
  43. current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn) );
  44. for (bar = PCI_BASE_ADDRESS_0; bar <= bar_limit; bar+=4) {
  45. /* Tickle the BAR and get the response */
  46. early_write_config_dword(hose,
  47. current_bus,
  48. pci_devfn,
  49. bar,
  50. 0xffffffff);
  51. early_read_config_dword(hose,
  52. current_bus,
  53. pci_devfn,
  54. bar,
  55. &bar_response);
  56. /* If BAR is not implemented go to the next BAR */
  57. if (!bar_response)
  58. continue;
  59. /* Check the BAR type and set our address mask */
  60. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  61. addr_mask = PCI_BASE_ADDRESS_IO_MASK;
  62. upper_limit = &pciauto_upper_iospc;
  63. DBG("PCI Autoconfig: BAR 0x%x, I/O, ", bar);
  64. } else {
  65. if ( (bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  66. PCI_BASE_ADDRESS_MEM_TYPE_64)
  67. found_mem64 = 1;
  68. addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
  69. upper_limit = &pciauto_upper_memspc;
  70. DBG("PCI Autoconfig: BAR 0x%x, Mem ", bar);
  71. }
  72. /* Calculate requested size */
  73. bar_size = ~(bar_response & addr_mask) + 1;
  74. /* Allocate a base address */
  75. bar_value = (*upper_limit - bar_size) & ~(bar_size - 1);
  76. /* Write it out and update our limit */
  77. early_write_config_dword(hose,
  78. current_bus,
  79. pci_devfn,
  80. bar,
  81. bar_value);
  82. *upper_limit = bar_value;
  83. /*
  84. * If we are a 64-bit decoder then increment to the
  85. * upper 32 bits of the bar and force it to locate
  86. * in the lower 4GB of memory.
  87. */
  88. if (found_mem64) {
  89. bar += 4;
  90. early_write_config_dword(hose,
  91. current_bus,
  92. pci_devfn,
  93. bar,
  94. 0x00000000);
  95. found_mem64 = 0;
  96. }
  97. DBG("size=0x%x, address=0x%x\n",
  98. bar_size, bar_value);
  99. }
  100. }
  101. void __init pciauto_prescan_setup_bridge(struct pci_controller *hose,
  102. int current_bus,
  103. int pci_devfn,
  104. int sub_bus,
  105. int *iosave,
  106. int *memsave)
  107. {
  108. /* Configure bus number registers */
  109. early_write_config_byte(hose,
  110. current_bus,
  111. pci_devfn,
  112. PCI_PRIMARY_BUS,
  113. current_bus);
  114. early_write_config_byte(hose,
  115. current_bus,
  116. pci_devfn,
  117. PCI_SECONDARY_BUS,
  118. sub_bus + 1);
  119. early_write_config_byte(hose,
  120. current_bus,
  121. pci_devfn,
  122. PCI_SUBORDINATE_BUS,
  123. 0xff);
  124. /* Round memory allocator to 1MB boundary */
  125. pciauto_upper_memspc &= ~(0x100000 - 1);
  126. *memsave = pciauto_upper_memspc;
  127. /* Round I/O allocator to 4KB boundary */
  128. pciauto_upper_iospc &= ~(0x1000 - 1);
  129. *iosave = pciauto_upper_iospc;
  130. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  131. early_write_config_word(hose,
  132. current_bus,
  133. pci_devfn,
  134. PCI_MEMORY_LIMIT,
  135. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  136. early_write_config_byte(hose,
  137. current_bus,
  138. pci_devfn,
  139. PCI_IO_LIMIT,
  140. ((pciauto_upper_iospc - 1) & 0x0000f000) >> 8);
  141. early_write_config_word(hose,
  142. current_bus,
  143. pci_devfn,
  144. PCI_IO_LIMIT_UPPER16,
  145. ((pciauto_upper_iospc - 1) & 0xffff0000) >> 16);
  146. /* Zero upper 32 bits of prefetchable base/limit */
  147. early_write_config_dword(hose,
  148. current_bus,
  149. pci_devfn,
  150. PCI_PREF_BASE_UPPER32,
  151. 0);
  152. early_write_config_dword(hose,
  153. current_bus,
  154. pci_devfn,
  155. PCI_PREF_LIMIT_UPPER32,
  156. 0);
  157. }
  158. void __init pciauto_postscan_setup_bridge(struct pci_controller *hose,
  159. int current_bus,
  160. int pci_devfn,
  161. int sub_bus,
  162. int *iosave,
  163. int *memsave)
  164. {
  165. int cmdstat;
  166. /* Configure bus number registers */
  167. early_write_config_byte(hose,
  168. current_bus,
  169. pci_devfn,
  170. PCI_SUBORDINATE_BUS,
  171. sub_bus);
  172. /*
  173. * Round memory allocator to 1MB boundary.
  174. * If no space used, allocate minimum.
  175. */
  176. pciauto_upper_memspc &= ~(0x100000 - 1);
  177. if (*memsave == pciauto_upper_memspc)
  178. pciauto_upper_memspc -= 0x00100000;
  179. early_write_config_word(hose,
  180. current_bus,
  181. pci_devfn,
  182. PCI_MEMORY_BASE,
  183. pciauto_upper_memspc >> 16);
  184. /* Allocate 1MB for pre-fretch */
  185. early_write_config_word(hose,
  186. current_bus,
  187. pci_devfn,
  188. PCI_PREF_MEMORY_LIMIT,
  189. ((pciauto_upper_memspc - 1) & 0xfff00000) >> 16);
  190. pciauto_upper_memspc -= 0x100000;
  191. early_write_config_word(hose,
  192. current_bus,
  193. pci_devfn,
  194. PCI_PREF_MEMORY_BASE,
  195. pciauto_upper_memspc >> 16);
  196. /* Round I/O allocator to 4KB boundary */
  197. pciauto_upper_iospc &= ~(0x1000 - 1);
  198. if (*iosave == pciauto_upper_iospc)
  199. pciauto_upper_iospc -= 0x1000;
  200. early_write_config_byte(hose,
  201. current_bus,
  202. pci_devfn,
  203. PCI_IO_BASE,
  204. (pciauto_upper_iospc & 0x0000f000) >> 8);
  205. early_write_config_word(hose,
  206. current_bus,
  207. pci_devfn,
  208. PCI_IO_BASE_UPPER16,
  209. pciauto_upper_iospc >> 16);
  210. /* Enable memory and I/O accesses, enable bus master */
  211. early_read_config_dword(hose,
  212. current_bus,
  213. pci_devfn,
  214. PCI_COMMAND,
  215. &cmdstat);
  216. early_write_config_dword(hose,
  217. current_bus,
  218. pci_devfn,
  219. PCI_COMMAND,
  220. cmdstat |
  221. PCI_COMMAND_IO |
  222. PCI_COMMAND_MEMORY |
  223. PCI_COMMAND_MASTER);
  224. }
  225. void __init pciauto_prescan_setup_cardbus_bridge(struct pci_controller *hose,
  226. int current_bus,
  227. int pci_devfn,
  228. int sub_bus,
  229. int *iosave,
  230. int *memsave)
  231. {
  232. /* Configure bus number registers */
  233. early_write_config_byte(hose,
  234. current_bus,
  235. pci_devfn,
  236. PCI_PRIMARY_BUS,
  237. current_bus);
  238. early_write_config_byte(hose,
  239. current_bus,
  240. pci_devfn,
  241. PCI_SECONDARY_BUS,
  242. sub_bus + 1);
  243. early_write_config_byte(hose,
  244. current_bus,
  245. pci_devfn,
  246. PCI_SUBORDINATE_BUS,
  247. 0xff);
  248. /* Round memory allocator to 4KB boundary */
  249. pciauto_upper_memspc &= ~(0x1000 - 1);
  250. *memsave = pciauto_upper_memspc;
  251. /* Round I/O allocator to 4 byte boundary */
  252. pciauto_upper_iospc &= ~(0x4 - 1);
  253. *iosave = pciauto_upper_iospc;
  254. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  255. early_write_config_dword(hose,
  256. current_bus,
  257. pci_devfn,
  258. 0x20,
  259. pciauto_upper_memspc - 1);
  260. early_write_config_dword(hose,
  261. current_bus,
  262. pci_devfn,
  263. 0x30,
  264. pciauto_upper_iospc - 1);
  265. }
  266. void __init pciauto_postscan_setup_cardbus_bridge(struct pci_controller *hose,
  267. int current_bus,
  268. int pci_devfn,
  269. int sub_bus,
  270. int *iosave,
  271. int *memsave)
  272. {
  273. int cmdstat;
  274. /*
  275. * Configure subordinate bus number. The PCI subsystem
  276. * bus scan will renumber buses (reserving three additional
  277. * for this PCI<->CardBus bridge for the case where a CardBus
  278. * adapter contains a P2P or CB2CB bridge.
  279. */
  280. early_write_config_byte(hose,
  281. current_bus,
  282. pci_devfn,
  283. PCI_SUBORDINATE_BUS,
  284. sub_bus);
  285. /*
  286. * Reserve an additional 4MB for mem space and 16KB for
  287. * I/O space. This should cover any additional space
  288. * requirement of unusual CardBus devices with
  289. * additional bridges that can consume more address space.
  290. *
  291. * Although pcmcia-cs currently will reprogram bridge
  292. * windows, the goal is to add an option to leave them
  293. * alone and use the bridge window ranges as the regions
  294. * that are searched for free resources upon hot-insertion
  295. * of a device. This will allow a PCI<->CardBus bridge
  296. * configured by this routine to happily live behind a
  297. * P2P bridge in a system.
  298. */
  299. pciauto_upper_memspc -= 0x00400000;
  300. pciauto_upper_iospc -= 0x00004000;
  301. /* Round memory allocator to 4KB boundary */
  302. pciauto_upper_memspc &= ~(0x1000 - 1);
  303. early_write_config_dword(hose,
  304. current_bus,
  305. pci_devfn,
  306. 0x1c,
  307. pciauto_upper_memspc);
  308. /* Round I/O allocator to 4 byte boundary */
  309. pciauto_upper_iospc &= ~(0x4 - 1);
  310. early_write_config_dword(hose,
  311. current_bus,
  312. pci_devfn,
  313. 0x2c,
  314. pciauto_upper_iospc);
  315. /* Enable memory and I/O accesses, enable bus master */
  316. early_read_config_dword(hose,
  317. current_bus,
  318. pci_devfn,
  319. PCI_COMMAND,
  320. &cmdstat);
  321. early_write_config_dword(hose,
  322. current_bus,
  323. pci_devfn,
  324. PCI_COMMAND,
  325. cmdstat |
  326. PCI_COMMAND_IO |
  327. PCI_COMMAND_MEMORY |
  328. PCI_COMMAND_MASTER);
  329. }
  330. int __init pciauto_bus_scan(struct pci_controller *hose, int current_bus)
  331. {
  332. int sub_bus, pci_devfn, pci_class, cmdstat, found_multi = 0;
  333. unsigned short vid;
  334. unsigned char header_type;
  335. /*
  336. * Fetch our I/O and memory space upper boundaries used
  337. * to allocated base addresses on this hose.
  338. */
  339. if (current_bus == hose->first_busno) {
  340. pciauto_upper_iospc = hose->io_space.end + 1;
  341. pciauto_upper_memspc = hose->mem_space.end + 1;
  342. }
  343. sub_bus = current_bus;
  344. for (pci_devfn = 0; pci_devfn < 0xff; pci_devfn++) {
  345. /* Skip our host bridge */
  346. if ( (current_bus == hose->first_busno) && (pci_devfn == 0) )
  347. continue;
  348. if (PCI_FUNC(pci_devfn) && !found_multi)
  349. continue;
  350. /* If config space read fails from this device, move on */
  351. if (early_read_config_byte(hose,
  352. current_bus,
  353. pci_devfn,
  354. PCI_HEADER_TYPE,
  355. &header_type))
  356. continue;
  357. if (!PCI_FUNC(pci_devfn))
  358. found_multi = header_type & 0x80;
  359. early_read_config_word(hose,
  360. current_bus,
  361. pci_devfn,
  362. PCI_VENDOR_ID,
  363. &vid);
  364. if (vid != 0xffff) {
  365. early_read_config_dword(hose,
  366. current_bus,
  367. pci_devfn,
  368. PCI_CLASS_REVISION, &pci_class);
  369. if ( (pci_class >> 16) == PCI_CLASS_BRIDGE_PCI ) {
  370. int iosave, memsave;
  371. DBG("PCI Autoconfig: Found P2P bridge, device %d\n", PCI_SLOT(pci_devfn));
  372. /* Allocate PCI I/O and/or memory space */
  373. pciauto_setup_bars(hose,
  374. current_bus,
  375. pci_devfn,
  376. PCI_BASE_ADDRESS_1);
  377. pciauto_prescan_setup_bridge(hose,
  378. current_bus,
  379. pci_devfn,
  380. sub_bus,
  381. &iosave,
  382. &memsave);
  383. sub_bus = pciauto_bus_scan(hose, sub_bus+1);
  384. pciauto_postscan_setup_bridge(hose,
  385. current_bus,
  386. pci_devfn,
  387. sub_bus,
  388. &iosave,
  389. &memsave);
  390. } else if ((pci_class >> 16) == PCI_CLASS_BRIDGE_CARDBUS) {
  391. int iosave, memsave;
  392. DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
  393. /* Place CardBus Socket/ExCA registers */
  394. pciauto_setup_bars(hose,
  395. current_bus,
  396. pci_devfn,
  397. PCI_BASE_ADDRESS_0);
  398. pciauto_prescan_setup_cardbus_bridge(hose,
  399. current_bus,
  400. pci_devfn,
  401. sub_bus,
  402. &iosave,
  403. &memsave);
  404. sub_bus = pciauto_bus_scan(hose, sub_bus+1);
  405. pciauto_postscan_setup_cardbus_bridge(hose,
  406. current_bus,
  407. pci_devfn,
  408. sub_bus,
  409. &iosave,
  410. &memsave);
  411. } else {
  412. if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
  413. unsigned char prg_iface;
  414. early_read_config_byte(hose,
  415. current_bus,
  416. pci_devfn,
  417. PCI_CLASS_PROG,
  418. &prg_iface);
  419. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
  420. DBG("PCI Autoconfig: Skipping legacy mode IDE controller\n");
  421. continue;
  422. }
  423. }
  424. /* Allocate PCI I/O and/or memory space */
  425. pciauto_setup_bars(hose,
  426. current_bus,
  427. pci_devfn,
  428. PCI_BASE_ADDRESS_5);
  429. /*
  430. * Enable some standard settings
  431. */
  432. early_read_config_dword(hose,
  433. current_bus,
  434. pci_devfn,
  435. PCI_COMMAND,
  436. &cmdstat);
  437. early_write_config_dword(hose,
  438. current_bus,
  439. pci_devfn,
  440. PCI_COMMAND,
  441. cmdstat |
  442. PCI_COMMAND_IO |
  443. PCI_COMMAND_MEMORY |
  444. PCI_COMMAND_MASTER);
  445. early_write_config_byte(hose,
  446. current_bus,
  447. pci_devfn,
  448. PCI_LATENCY_TIMER,
  449. 0x80);
  450. }
  451. }
  452. }
  453. return sub_bus;
  454. }