pci-auto.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * PCI autoconfiguration library
  3. *
  4. * Author: Matt Porter <mporter@mvista.com>
  5. *
  6. * Copyright 2000, 2001 MontaVista Software Inc.
  7. * Copyright 2001 Bradley D. LaRonde <brad@ltc.com>
  8. * Copyright 2003 Paul Mundt <lethal@linux-sh.org>
  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. /*
  16. * Modified for MIPS by Jun Sun, jsun@mvista.com
  17. *
  18. * . Simplify the interface between pci_auto and the rest: a single function.
  19. * . Assign resources from low address to upper address.
  20. * . change most int to u32.
  21. *
  22. * Further modified to include it as mips generic code, ppopov@mvista.com.
  23. *
  24. * 2001-10-26 Bradley D. LaRonde <brad@ltc.com>
  25. * - Add a top_bus argument to the "early config" functions so that
  26. * they can set a fake parent bus pointer to convince the underlying
  27. * pci ops to use type 1 configuration for sub busses.
  28. * - Set bridge base and limit registers correctly.
  29. * - Align io and memory base properly before and after bridge setup.
  30. * - Don't fall through to pci_setup_bars for bridge.
  31. * - Reformat the debug output to look more like lspci's output.
  32. *
  33. * Cloned for SuperH by M. R. Brown, mrbrown@0xd6.org
  34. *
  35. * 2003-08-05 Paul Mundt <lethal@linux-sh.org>
  36. * - Don't update the BAR values on systems that already have valid addresses
  37. * and don't want these updated for whatever reason, by way of a new config
  38. * option check. However, we still read in the old BAR values so that they
  39. * can still be reported through the debug output.
  40. */
  41. #include <linux/kernel.h>
  42. #include <linux/init.h>
  43. #include <linux/types.h>
  44. #include <linux/pci.h>
  45. #define DEBUG
  46. #ifdef DEBUG
  47. #define DBG(x...) printk(x)
  48. #else
  49. #define DBG(x...)
  50. #endif
  51. /*
  52. * These functions are used early on before PCI scanning is done
  53. * and all of the pci_dev and pci_bus structures have been created.
  54. */
  55. static struct pci_dev *fake_pci_dev(struct pci_channel *hose,
  56. int top_bus, int busnr, int devfn)
  57. {
  58. static struct pci_dev dev;
  59. static struct pci_bus bus;
  60. dev.bus = &bus;
  61. dev.sysdata = hose;
  62. dev.devfn = devfn;
  63. bus.number = busnr;
  64. bus.ops = hose->pci_ops;
  65. bus.sysdata = hose;
  66. if(busnr != top_bus)
  67. /* Fake a parent bus structure. */
  68. bus.parent = &bus;
  69. else
  70. bus.parent = NULL;
  71. return &dev;
  72. }
  73. #define EARLY_PCI_OP(rw, size, type) \
  74. static int early_##rw##_config_##size(struct pci_channel *hose, \
  75. int top_bus, int bus, int devfn, int offset, type value) \
  76. { \
  77. return pci_##rw##_config_##size( \
  78. fake_pci_dev(hose, top_bus, bus, devfn), \
  79. offset, value); \
  80. }
  81. EARLY_PCI_OP(read, byte, u8 *)
  82. EARLY_PCI_OP(read, word, u16 *)
  83. EARLY_PCI_OP(read, dword, u32 *)
  84. EARLY_PCI_OP(write, byte, u8)
  85. EARLY_PCI_OP(write, word, u16)
  86. EARLY_PCI_OP(write, dword, u32)
  87. static struct resource *io_resource_inuse;
  88. static struct resource *mem_resource_inuse;
  89. static u32 pciauto_lower_iospc;
  90. static u32 pciauto_upper_iospc;
  91. static u32 pciauto_lower_memspc;
  92. static u32 pciauto_upper_memspc;
  93. static void __init
  94. pciauto_setup_bars(struct pci_channel *hose,
  95. int top_bus,
  96. int current_bus,
  97. int pci_devfn,
  98. int bar_limit)
  99. {
  100. u32 bar_response, bar_size, bar_value;
  101. u32 bar, addr_mask, bar_nr = 0;
  102. u32 * upper_limit;
  103. u32 * lower_limit;
  104. int found_mem64 = 0;
  105. for (bar = PCI_BASE_ADDRESS_0; bar <= bar_limit; bar+=4) {
  106. u32 bar_addr;
  107. /* Read the old BAR value */
  108. early_read_config_dword(hose, top_bus,
  109. current_bus,
  110. pci_devfn,
  111. bar,
  112. &bar_addr);
  113. /* Tickle the BAR and get the response */
  114. early_write_config_dword(hose, top_bus,
  115. current_bus,
  116. pci_devfn,
  117. bar,
  118. 0xffffffff);
  119. early_read_config_dword(hose, top_bus,
  120. current_bus,
  121. pci_devfn,
  122. bar,
  123. &bar_response);
  124. /*
  125. * Write the old BAR value back out, only update the BAR
  126. * if we implicitly want resources to be updated, which
  127. * is done by the generic code further down. -- PFM.
  128. */
  129. early_write_config_dword(hose, top_bus,
  130. current_bus,
  131. pci_devfn,
  132. bar,
  133. bar_addr);
  134. /* If BAR is not implemented go to the next BAR */
  135. if (!bar_response)
  136. continue;
  137. /*
  138. * Workaround for a BAR that doesn't use its upper word,
  139. * like the ALi 1535D+ PCI DC-97 Controller Modem (M5457).
  140. * bdl <brad@ltc.com>
  141. */
  142. if (!(bar_response & 0xffff0000))
  143. bar_response |= 0xffff0000;
  144. retry:
  145. /* Check the BAR type and set our address mask */
  146. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  147. addr_mask = PCI_BASE_ADDRESS_IO_MASK;
  148. upper_limit = &pciauto_upper_iospc;
  149. lower_limit = &pciauto_lower_iospc;
  150. DBG(" I/O");
  151. } else {
  152. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  153. PCI_BASE_ADDRESS_MEM_TYPE_64)
  154. found_mem64 = 1;
  155. addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
  156. upper_limit = &pciauto_upper_memspc;
  157. lower_limit = &pciauto_lower_memspc;
  158. DBG(" Mem");
  159. }
  160. /* Calculate requested size */
  161. bar_size = ~(bar_response & addr_mask) + 1;
  162. /* Allocate a base address */
  163. bar_value = ((*lower_limit - 1) & ~(bar_size - 1)) + bar_size;
  164. if ((bar_value + bar_size) > *upper_limit) {
  165. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  166. if (io_resource_inuse->child) {
  167. io_resource_inuse =
  168. io_resource_inuse->child;
  169. pciauto_lower_iospc =
  170. io_resource_inuse->start;
  171. pciauto_upper_iospc =
  172. io_resource_inuse->end + 1;
  173. goto retry;
  174. }
  175. } else {
  176. if (mem_resource_inuse->child) {
  177. mem_resource_inuse =
  178. mem_resource_inuse->child;
  179. pciauto_lower_memspc =
  180. mem_resource_inuse->start;
  181. pciauto_upper_memspc =
  182. mem_resource_inuse->end + 1;
  183. goto retry;
  184. }
  185. }
  186. DBG(" unavailable -- skipping, value %x size %x\n",
  187. bar_value, bar_size);
  188. continue;
  189. }
  190. if (bar_value < *lower_limit || (bar_value + bar_size) >= *upper_limit) {
  191. DBG(" unavailable -- skipping, value %x size %x\n",
  192. bar_value, bar_size);
  193. continue;
  194. }
  195. #ifdef CONFIG_PCI_AUTO_UPDATE_RESOURCES
  196. /* Write it out and update our limit */
  197. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  198. bar, bar_value);
  199. #endif
  200. *lower_limit = bar_value + bar_size;
  201. /*
  202. * If we are a 64-bit decoder then increment to the
  203. * upper 32 bits of the bar and force it to locate
  204. * in the lower 4GB of memory.
  205. */
  206. if (found_mem64) {
  207. bar += 4;
  208. early_write_config_dword(hose, top_bus,
  209. current_bus,
  210. pci_devfn,
  211. bar,
  212. 0x00000000);
  213. }
  214. DBG(" at 0x%.8x [size=0x%x]\n", bar_value, bar_size);
  215. bar_nr++;
  216. }
  217. }
  218. static void __init
  219. pciauto_prescan_setup_bridge(struct pci_channel *hose,
  220. int top_bus,
  221. int current_bus,
  222. int pci_devfn,
  223. int sub_bus)
  224. {
  225. /* Configure bus number registers */
  226. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  227. PCI_PRIMARY_BUS, current_bus);
  228. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  229. PCI_SECONDARY_BUS, sub_bus + 1);
  230. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  231. PCI_SUBORDINATE_BUS, 0xff);
  232. /* Align memory and I/O to 1MB and 4KB boundaries. */
  233. pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
  234. & ~(0x100000 - 1);
  235. pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
  236. & ~(0x1000 - 1);
  237. /* Set base (lower limit) of address range behind bridge. */
  238. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  239. PCI_MEMORY_BASE, pciauto_lower_memspc >> 16);
  240. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  241. PCI_IO_BASE, (pciauto_lower_iospc & 0x0000f000) >> 8);
  242. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  243. PCI_IO_BASE_UPPER16, pciauto_lower_iospc >> 16);
  244. /* We don't support prefetchable memory for now, so disable */
  245. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  246. PCI_PREF_MEMORY_BASE, 0);
  247. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  248. PCI_PREF_MEMORY_LIMIT, 0);
  249. }
  250. static void __init
  251. pciauto_postscan_setup_bridge(struct pci_channel *hose,
  252. int top_bus,
  253. int current_bus,
  254. int pci_devfn,
  255. int sub_bus)
  256. {
  257. u32 temp;
  258. /*
  259. * [jsun] we always bump up baselines a little, so that if there
  260. * nothing behind P2P bridge, we don't wind up overlapping IO/MEM
  261. * spaces.
  262. */
  263. pciauto_lower_memspc += 1;
  264. pciauto_lower_iospc += 1;
  265. /* Configure bus number registers */
  266. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  267. PCI_SUBORDINATE_BUS, sub_bus);
  268. /* Set upper limit of address range behind bridge. */
  269. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  270. PCI_MEMORY_LIMIT, pciauto_lower_memspc >> 16);
  271. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  272. PCI_IO_LIMIT, (pciauto_lower_iospc & 0x0000f000) >> 8);
  273. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  274. PCI_IO_LIMIT_UPPER16, pciauto_lower_iospc >> 16);
  275. /* Align memory and I/O to 1MB and 4KB boundaries. */
  276. pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
  277. & ~(0x100000 - 1);
  278. pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
  279. & ~(0x1000 - 1);
  280. /* Enable memory and I/O accesses, enable bus master */
  281. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  282. PCI_COMMAND, &temp);
  283. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  284. PCI_COMMAND, temp | PCI_COMMAND_IO | PCI_COMMAND_MEMORY
  285. | PCI_COMMAND_MASTER);
  286. }
  287. static void __init
  288. pciauto_prescan_setup_cardbus_bridge(struct pci_channel *hose,
  289. int top_bus,
  290. int current_bus,
  291. int pci_devfn,
  292. int sub_bus)
  293. {
  294. /* Configure bus number registers */
  295. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  296. PCI_PRIMARY_BUS, current_bus);
  297. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  298. PCI_SECONDARY_BUS, sub_bus + 1);
  299. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  300. PCI_SUBORDINATE_BUS, 0xff);
  301. /* Align memory and I/O to 4KB and 4 byte boundaries. */
  302. pciauto_lower_memspc = (pciauto_lower_memspc + (0x1000 - 1))
  303. & ~(0x1000 - 1);
  304. pciauto_lower_iospc = (pciauto_lower_iospc + (0x4 - 1))
  305. & ~(0x4 - 1);
  306. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  307. PCI_CB_MEMORY_BASE_0, pciauto_lower_memspc);
  308. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  309. PCI_CB_IO_BASE_0, pciauto_lower_iospc);
  310. }
  311. static void __init
  312. pciauto_postscan_setup_cardbus_bridge(struct pci_channel *hose,
  313. int top_bus,
  314. int current_bus,
  315. int pci_devfn,
  316. int sub_bus)
  317. {
  318. u32 temp;
  319. /*
  320. * [jsun] we always bump up baselines a little, so that if there
  321. * nothing behind P2P bridge, we don't wind up overlapping IO/MEM
  322. * spaces.
  323. */
  324. pciauto_lower_memspc += 1;
  325. pciauto_lower_iospc += 1;
  326. /*
  327. * Configure subordinate bus number. The PCI subsystem
  328. * bus scan will renumber buses (reserving three additional
  329. * for this PCI<->CardBus bridge for the case where a CardBus
  330. * adapter contains a P2P or CB2CB bridge.
  331. */
  332. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  333. PCI_SUBORDINATE_BUS, sub_bus);
  334. /*
  335. * Reserve an additional 4MB for mem space and 16KB for
  336. * I/O space. This should cover any additional space
  337. * requirement of unusual CardBus devices with
  338. * additional bridges that can consume more address space.
  339. *
  340. * Although pcmcia-cs currently will reprogram bridge
  341. * windows, the goal is to add an option to leave them
  342. * alone and use the bridge window ranges as the regions
  343. * that are searched for free resources upon hot-insertion
  344. * of a device. This will allow a PCI<->CardBus bridge
  345. * configured by this routine to happily live behind a
  346. * P2P bridge in a system.
  347. */
  348. /* Align memory and I/O to 4KB and 4 byte boundaries. */
  349. pciauto_lower_memspc = (pciauto_lower_memspc + (0x1000 - 1))
  350. & ~(0x1000 - 1);
  351. pciauto_lower_iospc = (pciauto_lower_iospc + (0x4 - 1))
  352. & ~(0x4 - 1);
  353. /* Set up memory and I/O filter limits, assume 32-bit I/O space */
  354. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  355. PCI_CB_MEMORY_LIMIT_0, pciauto_lower_memspc - 1);
  356. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  357. PCI_CB_IO_LIMIT_0, pciauto_lower_iospc - 1);
  358. /* Enable memory and I/O accesses, enable bus master */
  359. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  360. PCI_COMMAND, &temp);
  361. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  362. PCI_COMMAND, temp | PCI_COMMAND_IO | PCI_COMMAND_MEMORY |
  363. PCI_COMMAND_MASTER);
  364. }
  365. #define PCIAUTO_IDE_MODE_MASK 0x05
  366. static int __init
  367. pciauto_bus_scan(struct pci_channel *hose, int top_bus, int current_bus)
  368. {
  369. int sub_bus;
  370. u32 pci_devfn, pci_class, cmdstat, found_multi=0;
  371. unsigned short vid, did;
  372. unsigned char header_type;
  373. int devfn_start = 0;
  374. int devfn_stop = 0xff;
  375. sub_bus = current_bus;
  376. if (hose->first_devfn)
  377. devfn_start = hose->first_devfn;
  378. if (hose->last_devfn)
  379. devfn_stop = hose->last_devfn;
  380. for (pci_devfn=devfn_start; pci_devfn<devfn_stop; pci_devfn++) {
  381. if (PCI_FUNC(pci_devfn) && !found_multi)
  382. continue;
  383. early_read_config_word(hose, top_bus, current_bus, pci_devfn,
  384. PCI_VENDOR_ID, &vid);
  385. if (vid == 0xffff) continue;
  386. early_read_config_byte(hose, top_bus, current_bus, pci_devfn,
  387. PCI_HEADER_TYPE, &header_type);
  388. if (!PCI_FUNC(pci_devfn))
  389. found_multi = header_type & 0x80;
  390. early_read_config_word(hose, top_bus, current_bus, pci_devfn,
  391. PCI_DEVICE_ID, &did);
  392. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  393. PCI_CLASS_REVISION, &pci_class);
  394. DBG("%.2x:%.2x.%x Class %.4x: %.4x:%.4x",
  395. current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn),
  396. pci_class >> 16, vid, did);
  397. if (pci_class & 0xff)
  398. DBG(" (rev %.2x)", pci_class & 0xff);
  399. DBG("\n");
  400. if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
  401. DBG(" Bridge: primary=%.2x, secondary=%.2x\n",
  402. current_bus, sub_bus + 1);
  403. pciauto_prescan_setup_bridge(hose, top_bus, current_bus,
  404. pci_devfn, sub_bus);
  405. DBG("Scanning sub bus %.2x, I/O 0x%.8x, Mem 0x%.8x\n",
  406. sub_bus + 1,
  407. pciauto_lower_iospc, pciauto_lower_memspc);
  408. sub_bus = pciauto_bus_scan(hose, top_bus, sub_bus+1);
  409. DBG("Back to bus %.2x\n", current_bus);
  410. pciauto_postscan_setup_bridge(hose, top_bus, current_bus,
  411. pci_devfn, sub_bus);
  412. continue;
  413. } else if ((pci_class >> 16) == PCI_CLASS_BRIDGE_CARDBUS) {
  414. DBG(" CARDBUS Bridge: primary=%.2x, secondary=%.2x\n",
  415. current_bus, sub_bus + 1);
  416. DBG("PCI Autoconfig: Found CardBus bridge, device %d function %d\n", PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn));
  417. /* Place CardBus Socket/ExCA registers */
  418. pciauto_setup_bars(hose, top_bus, current_bus, pci_devfn, PCI_BASE_ADDRESS_0);
  419. pciauto_prescan_setup_cardbus_bridge(hose, top_bus,
  420. current_bus, pci_devfn, sub_bus);
  421. DBG("Scanning sub bus %.2x, I/O 0x%.8x, Mem 0x%.8x\n",
  422. sub_bus + 1,
  423. pciauto_lower_iospc, pciauto_lower_memspc);
  424. sub_bus = pciauto_bus_scan(hose, top_bus, sub_bus+1);
  425. DBG("Back to bus %.2x, sub_bus is %x\n", current_bus, sub_bus);
  426. pciauto_postscan_setup_cardbus_bridge(hose, top_bus,
  427. current_bus, pci_devfn, sub_bus);
  428. continue;
  429. } else if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
  430. unsigned char prg_iface;
  431. early_read_config_byte(hose, top_bus, current_bus,
  432. pci_devfn, PCI_CLASS_PROG, &prg_iface);
  433. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
  434. DBG("Skipping legacy mode IDE controller\n");
  435. continue;
  436. }
  437. }
  438. /*
  439. * Found a peripheral, enable some standard
  440. * settings
  441. */
  442. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  443. PCI_COMMAND, &cmdstat);
  444. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  445. PCI_COMMAND, cmdstat | PCI_COMMAND_IO |
  446. PCI_COMMAND_MEMORY |
  447. PCI_COMMAND_MASTER);
  448. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  449. PCI_LATENCY_TIMER, 0x80);
  450. /* Allocate PCI I/O and/or memory space */
  451. pciauto_setup_bars(hose, top_bus, current_bus, pci_devfn, PCI_BASE_ADDRESS_5);
  452. }
  453. return sub_bus;
  454. }
  455. int __init
  456. pciauto_assign_resources(int busno, struct pci_channel *hose)
  457. {
  458. /* setup resource limits */
  459. io_resource_inuse = hose->io_resource;
  460. mem_resource_inuse = hose->mem_resource;
  461. pciauto_lower_iospc = io_resource_inuse->start;
  462. pciauto_upper_iospc = io_resource_inuse->end + 1;
  463. pciauto_lower_memspc = mem_resource_inuse->start;
  464. pciauto_upper_memspc = mem_resource_inuse->end + 1;
  465. DBG("Autoconfig PCI channel 0x%p\n", hose);
  466. DBG("Scanning bus %.2x, I/O 0x%.8x:0x%.8x, Mem 0x%.8x:0x%.8x\n",
  467. busno, pciauto_lower_iospc, pciauto_upper_iospc,
  468. pciauto_lower_memspc, pciauto_upper_memspc);
  469. return pciauto_bus_scan(hose, busno, busno);
  470. }