pci-auto.c 16 KB

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