pci_auto.c 12 KB

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