pci_auto.c
上传用户:jlfgdled
上传日期:2013-04-10
资源大小:33168k
文件大小:11k
源码类别:

Linux/Unix编程

开发平台:

Unix_Linux

  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.  *
  9.  * This program is free software; you can redistribute  it and/or modify it
  10.  * under  the terms of  the GNU General  Public License as published by the
  11.  * Free Software Foundation;  either version 2 of the  License, or (at your
  12.  * option) any later version.
  13.  */
  14. /*
  15.  * Modified for MIPS by Jun Sun, jsun@mvista.com
  16.  *
  17.  * . Simplify the interface between pci_auto and the rest: a single function.
  18.  * . Assign resources from low address to upper address.
  19.  * . change most int to u32.
  20.  *
  21.  * Further modified to include it as mips generic code, ppopov@mvista.com.
  22.  *
  23.  * 2001-10-26  Bradley D. LaRonde <brad@ltc.com>
  24.  * - Add a top_bus argument to the "early config" functions so that
  25.  *   they can set a fake parent bus pointer to convince the underlying
  26.  *   pci ops to use type 1 configuration for sub busses.
  27.  * - Set bridge base and limit registers correctly.
  28.  * - Align io and memory base properly before and after bridge setup.
  29.  * - Don't fall through to pci_setup_bars for bridge.
  30.  * - Reformat the debug output to look more like lspci's output.
  31.  */
  32. #include <linux/kernel.h>
  33. #include <linux/init.h>
  34. #include <linux/types.h>
  35. #include <linux/pci.h>
  36. #include <asm/pci_channel.h>
  37. #define DEBUG
  38. #ifdef  DEBUG
  39. #define DBG(x...) printk(x)
  40. #else
  41. #define DBG(x...)
  42. #endif
  43. /*
  44.  * These functions are used early on before PCI scanning is done
  45.  * and all of the pci_dev and pci_bus structures have been created.
  46.  */
  47. static struct pci_dev *fake_pci_dev(struct pci_channel *hose,
  48. int top_bus, int busnr, int devfn)
  49. {
  50. static struct pci_dev dev;
  51. static struct pci_bus bus;
  52. dev.bus = &bus;
  53. dev.sysdata = hose;
  54. dev.devfn = devfn;
  55. bus.number = busnr;
  56. bus.ops = hose->pci_ops;
  57. if(busnr != top_bus)
  58. /* Fake a parent bus structure. */
  59. bus.parent = &bus;
  60. else
  61. bus.parent = NULL;
  62. return &dev;
  63. }
  64. #define EARLY_PCI_OP(rw, size, type)
  65. int early_##rw##_config_##size(struct pci_channel *hose,
  66. int top_bus, int bus, int devfn, int offset, type value)
  67. {
  68. return pci_##rw##_config_##size(
  69. fake_pci_dev(hose, top_bus, bus, devfn),
  70. offset, value);
  71. }
  72. EARLY_PCI_OP(read, byte, u8 *)
  73. EARLY_PCI_OP(read, word, u16 *)
  74. EARLY_PCI_OP(read, dword, u32 *)
  75. EARLY_PCI_OP(write, byte, u8)
  76. EARLY_PCI_OP(write, word, u16)
  77. EARLY_PCI_OP(write, dword, u32)
  78. static struct resource *io_resource_inuse;
  79. static struct resource *mem_resource_inuse;
  80. static u32 pciauto_lower_iospc;
  81. static u32 pciauto_upper_iospc;
  82. static u32 pciauto_lower_memspc;
  83. static u32 pciauto_upper_memspc;
  84. void __init
  85. pciauto_setup_bars(struct pci_channel *hose,
  86.    int top_bus,
  87.    int current_bus,
  88.    int pci_devfn)
  89. {
  90. u32 bar_response, bar_size, bar_value;
  91. u32 bar, addr_mask, bar_nr = 0;
  92. u32 * upper_limit;
  93. u32 * lower_limit;
  94. int found_mem64 = 0;
  95. for (bar = PCI_BASE_ADDRESS_0; bar <= PCI_BASE_ADDRESS_5; bar+=4) {
  96. /* Tickle the BAR and get the response */
  97. early_write_config_dword(hose, top_bus,
  98.  current_bus,
  99.  pci_devfn,
  100.  bar,
  101.  0xffffffff);
  102. early_read_config_dword(hose, top_bus,
  103. current_bus,
  104. pci_devfn,
  105. bar,
  106. &bar_response);
  107. /* If BAR is not implemented go to the next BAR */
  108. if (!bar_response)
  109. continue;
  110. /*
  111.  * Workaround for a BAR that doesn't use its upper word,
  112.  * like the ALi 1535D+ PCI DC-97 Controller Modem (M5457).
  113.  * bdl <brad@ltc.com>
  114.  */
  115. if (!(bar_response & 0xffff0000))
  116. bar_response |= 0xffff0000;
  117. retry:
  118. /* Check the BAR type and set our address mask */
  119. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  120. addr_mask = PCI_BASE_ADDRESS_IO_MASK;
  121. upper_limit = &pciauto_upper_iospc;
  122. lower_limit = &pciauto_lower_iospc;
  123. DBG("        I/O");
  124. } else {
  125. if ((bar_response & PCI_BASE_ADDRESS_MEM_TYPE_MASK) ==
  126.     PCI_BASE_ADDRESS_MEM_TYPE_64)
  127. found_mem64 = 1;
  128. addr_mask = PCI_BASE_ADDRESS_MEM_MASK;
  129. upper_limit = &pciauto_upper_memspc;
  130. lower_limit = &pciauto_lower_memspc;
  131. DBG("        Mem");
  132. }
  133. /* Calculate requested size */
  134. bar_size = ~(bar_response & addr_mask) + 1;
  135. /* Allocate a base address */
  136. bar_value = ((*lower_limit - 1) & ~(bar_size - 1)) + bar_size;
  137. if ((bar_value + bar_size) > *upper_limit) {
  138. if (bar_response & PCI_BASE_ADDRESS_SPACE) {
  139. if (io_resource_inuse->child) {
  140. io_resource_inuse =
  141. io_resource_inuse->child;
  142. pciauto_lower_iospc =
  143. io_resource_inuse->start;
  144. pciauto_upper_iospc =
  145. io_resource_inuse->end + 1;
  146. goto retry;
  147. }
  148. } else {
  149. if (mem_resource_inuse->child) {
  150. mem_resource_inuse =
  151. mem_resource_inuse->child;
  152. pciauto_lower_memspc =
  153. mem_resource_inuse->start;
  154. pciauto_upper_memspc =
  155. mem_resource_inuse->end + 1;
  156. goto retry;
  157. }
  158. }
  159. DBG(" unavailable -- skippingn");
  160. continue;
  161. }
  162. /* Write it out and update our limit */
  163. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  164.  bar, bar_value);
  165. *lower_limit = bar_value + bar_size;
  166. /*
  167.  * If we are a 64-bit decoder then increment to the
  168.  * upper 32 bits of the bar and force it to locate
  169.  * in the lower 4GB of memory.
  170.  */
  171. if (found_mem64) {
  172. bar += 4;
  173. early_write_config_dword(hose, top_bus,
  174.  current_bus,
  175.  pci_devfn,
  176.  bar,
  177.  0x00000000);
  178. }
  179. DBG(" at 0x%.8x [size=0x%x]n", bar_value, bar_size);
  180. bar_nr++;
  181. }
  182. }
  183. void __init
  184. pciauto_prescan_setup_bridge(struct pci_channel *hose,
  185.      int top_bus,
  186.      int current_bus,
  187.      int pci_devfn,
  188.      int sub_bus)
  189. {
  190. /* Configure bus number registers */
  191. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  192.                         PCI_PRIMARY_BUS, current_bus);
  193. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  194. PCI_SECONDARY_BUS, sub_bus + 1);
  195. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  196. PCI_SUBORDINATE_BUS, 0xff);
  197. /* Align memory and I/O to 1MB and 4KB boundaries. */
  198. pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
  199. & ~(0x100000 - 1);
  200. pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
  201. & ~(0x1000 - 1);
  202. /* Set base (lower limit) of address range behind bridge. */
  203. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  204. PCI_MEMORY_BASE, pciauto_lower_memspc >> 16);
  205. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  206. PCI_IO_BASE, (pciauto_lower_iospc & 0x0000f000) >> 8);
  207. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  208. PCI_IO_BASE_UPPER16, pciauto_lower_iospc >> 16);
  209. /* We don't support prefetchable memory for now, so disable */
  210. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  211. PCI_PREF_MEMORY_BASE, 0);
  212. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  213. PCI_PREF_MEMORY_LIMIT, 0);
  214. }
  215. void __init
  216. pciauto_postscan_setup_bridge(struct pci_channel *hose,
  217.       int top_bus,
  218.       int current_bus,
  219.       int pci_devfn,
  220.       int sub_bus)
  221. {
  222. u32 temp;
  223. /* Configure bus number registers */
  224. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  225. PCI_SUBORDINATE_BUS, sub_bus);
  226. /* Set upper limit of address range behind bridge. */
  227. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  228. PCI_MEMORY_LIMIT, pciauto_lower_memspc >> 16);
  229. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  230. PCI_IO_LIMIT, (pciauto_lower_iospc & 0x0000f000) >> 8);
  231. early_write_config_word(hose, top_bus, current_bus, pci_devfn,
  232. PCI_IO_LIMIT_UPPER16, pciauto_lower_iospc >> 16);
  233. /* Align memory and I/O to 1MB and 4KB boundaries. */
  234. pciauto_lower_memspc = (pciauto_lower_memspc + (0x100000 - 1))
  235. & ~(0x100000 - 1);
  236. pciauto_lower_iospc = (pciauto_lower_iospc + (0x1000 - 1))
  237. & ~(0x1000 - 1);
  238. /* Enable memory and I/O accesses, enable bus master */
  239. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  240. PCI_COMMAND, &temp);
  241. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  242. PCI_COMMAND, temp | PCI_COMMAND_IO | PCI_COMMAND_MEMORY
  243. | PCI_COMMAND_MASTER);
  244. }
  245. #define      PCIAUTO_IDE_MODE_MASK           0x05
  246. int __init
  247. pciauto_bus_scan(struct pci_channel *hose, int top_bus, int current_bus)
  248. {
  249. int sub_bus;
  250. u32 pci_devfn, pci_class, cmdstat, found_multi=0;
  251. unsigned short vid, did;
  252. unsigned char header_type;
  253. int devfn_start = 0;
  254. int devfn_stop = 0xff;
  255. sub_bus = current_bus;
  256. if (hose->first_devfn)
  257. devfn_start = hose->first_devfn;
  258. if (hose->last_devfn)
  259. devfn_stop = hose->last_devfn;
  260. for (pci_devfn=devfn_start; pci_devfn<devfn_stop; pci_devfn++) {
  261. if (PCI_FUNC(pci_devfn) && !found_multi)
  262. continue;
  263. early_read_config_word(hose, top_bus, current_bus, pci_devfn,
  264.        PCI_VENDOR_ID, &vid);
  265. if (vid == 0xffff) continue;
  266. early_read_config_byte(hose, top_bus, current_bus, pci_devfn,
  267.        PCI_HEADER_TYPE, &header_type);
  268. if (!PCI_FUNC(pci_devfn))
  269. found_multi = header_type & 0x80;
  270. early_read_config_word(hose, top_bus, current_bus, pci_devfn,
  271.        PCI_DEVICE_ID, &did);
  272. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  273. PCI_CLASS_REVISION, &pci_class);
  274. DBG("%.2x:%.2x.%x Class %.4x: %.4x:%.4x",
  275. current_bus, PCI_SLOT(pci_devfn), PCI_FUNC(pci_devfn),
  276. pci_class >> 16, vid, did);
  277. if (pci_class & 0xff)
  278. DBG(" (rev %.2x)", pci_class & 0xff);
  279. DBG("n");
  280. if ((pci_class >> 16) == PCI_CLASS_BRIDGE_PCI) {
  281. DBG("        Bridge: primary=%.2x, secondary=%.2xn",
  282. current_bus, sub_bus + 1);
  283. pciauto_prescan_setup_bridge(hose, top_bus, current_bus,
  284.      pci_devfn, sub_bus);
  285. DBG("Scanning sub bus %.2x, I/O 0x%.8x, Mem 0x%.8xn",
  286. sub_bus + 1,
  287. pciauto_lower_iospc, pciauto_lower_memspc);
  288. sub_bus = pciauto_bus_scan(hose, top_bus, sub_bus+1);
  289. DBG("Back to bus %.2xn", current_bus);
  290. pciauto_postscan_setup_bridge(hose, top_bus, current_bus,
  291.       pci_devfn, sub_bus);
  292. continue;
  293. } else if ((pci_class >> 16) == PCI_CLASS_STORAGE_IDE) {
  294. unsigned char prg_iface;
  295. early_read_config_byte(hose, top_bus, current_bus,
  296. pci_devfn, PCI_CLASS_PROG, &prg_iface);
  297. if (!(prg_iface & PCIAUTO_IDE_MODE_MASK)) {
  298. DBG("Skipping legacy mode IDE controllern");
  299. continue;
  300. }
  301. }
  302.   /*
  303.  * Found a peripheral, enable some standard
  304.  * settings
  305.  */
  306. early_read_config_dword(hose, top_bus, current_bus, pci_devfn,
  307. PCI_COMMAND, &cmdstat);
  308. early_write_config_dword(hose, top_bus, current_bus, pci_devfn,
  309.  PCI_COMMAND, cmdstat | PCI_COMMAND_IO |
  310.  PCI_COMMAND_MEMORY |
  311.  PCI_COMMAND_MASTER);
  312. early_write_config_byte(hose, top_bus, current_bus, pci_devfn,
  313. PCI_LATENCY_TIMER, 0x80);
  314. /* Allocate PCI I/O and/or memory space */
  315. pciauto_setup_bars(hose, top_bus, current_bus, pci_devfn);
  316. }
  317. return sub_bus;
  318. }
  319. int __init
  320. pciauto_assign_resources(int busno, struct pci_channel *hose)
  321. {
  322. /* setup resource limits */
  323. io_resource_inuse = hose->io_resource;
  324. mem_resource_inuse = hose->mem_resource;
  325. pciauto_lower_iospc = io_resource_inuse->start;
  326. pciauto_upper_iospc = io_resource_inuse->end + 1;
  327. pciauto_lower_memspc = mem_resource_inuse->start;
  328. pciauto_upper_memspc = mem_resource_inuse->end + 1;
  329. DBG("Autoconfig PCI channel 0x%pn", hose);
  330. DBG("Scanning bus %.2x, I/O 0x%.8x:0x%.8x, Mem 0x%.8x:0x%.8xn",
  331. busno, pciauto_lower_iospc, pciauto_upper_iospc,
  332. pciauto_lower_memspc, pciauto_upper_memspc);
  333. return pciauto_bus_scan(hose, busno, busno);
  334. }