您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

pci_io.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  1. /*
  2. ** Support for NE2000 PCI clones added David Monro June 1997
  3. ** Generalised to other NICs by Ken Yap July 1997
  4. **
  5. ** Most of this is taken from:
  6. **
  7. ** /usr/src/linux/drivers/pci/pci.c
  8. ** /usr/src/linux/include/linux/pci.h
  9. ** /usr/src/linux/arch/i386/bios32.c
  10. ** /usr/src/linux/include/linux/bios32.h
  11. ** /usr/src/linux/drivers/net/ne.c
  12. */
  13. #include "etherboot.h"
  14. #include "init.h"
  15. #include "pci.h"
  16. #include "pci_io.h"
  17. #ifdef KEEP_IT_REAL
  18. #include "realmode.h"
  19. #endif
  20. /* Macros for direct PCI access */
  21. #define CONFIG_ADDRESS 0xcf8
  22. #define CONFIG_DATA 0xcfc
  23. #define CONFIG_CMD( pci, where ) \
  24. ( 0x80000000 | (pci->busdevfn << 8) | (where & ~3) )
  25. /* Signatures for PCI BIOS */
  26. #define BIOS_SIG(a,b,c,d) ( ( a<<0 ) + ( b<<8 ) + ( c<<16 ) + ( d<<24 ) )
  27. #define PRINT_BIOS_SIG(x) ( (x) & 0xff ), ( ( (x)>>8 ) & 0xff ), \
  28. ( ( (x)>>16 ) & 0xff ),( ( (x)>>24 ) & 0xff )
  29. #define BIOS32_SIGNATURE BIOS_SIG ( '_', '3', '2', '_' )
  30. #define PCI_SIGNATURE BIOS_SIG ( 'P', 'C', 'I', ' ' )
  31. #define PCI_SERVICE BIOS_SIG ( '$', 'P', 'C', 'I' )
  32. /* BIOS32 structure as found in PCI BIOS ROM */
  33. struct bios32 {
  34. unsigned long signature; /* _32_ */
  35. unsigned long entry; /* 32 bit physical address */
  36. unsigned char revision; /* Revision level, 0 */
  37. unsigned char length; /* Length in paragraphs */
  38. unsigned char checksum; /* Should byte sum to zero */
  39. unsigned char reserved[5]; /* Must be zero */
  40. };
  41. /* Values returned by BIOS32 service directory */
  42. #define BIOS32_SERVICE_PRESENT 0x00
  43. #define BIOS32_SERVICE_NOT_PRESENT 0x80
  44. #define CF ( 1 << 0 )
  45. /* PCI BIOS entry point */
  46. #ifndef KEEP_IT_REAL
  47. static unsigned long pcibios32_entry;
  48. #endif
  49. static int have_pcibios;
  50. /* Macro for calling a 32-bit entry point with flat physical
  51. * addresses. Use in a statement such as
  52. * __asm__ ( FLAT_FAR_CALL_ESI,
  53. * : <output registers>
  54. * : "S" ( entry_point ), <other input registers> );
  55. */
  56. #define FLAT_FAR_CALL_ESI "call _virt_to_phys\n\t" \
  57. "pushl %%cs\n\t" \
  58. "call *%%esi\n\t" \
  59. "cli\n\t" \
  60. "cld\n\t" \
  61. "call _phys_to_virt\n\t"
  62. /*
  63. * Functions for accessing PCI configuration space directly with type
  64. * 1 accesses.
  65. *
  66. */
  67. static inline int pcidirect_read_config_byte ( struct pci_device *pci,
  68. unsigned int where,
  69. uint8_t *value ) {
  70. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  71. *value = inb ( CONFIG_DATA + ( where & 3 ) );
  72. return 0;
  73. }
  74. static inline int pcidirect_read_config_word ( struct pci_device *pci,
  75. unsigned int where,
  76. uint16_t *value ) {
  77. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  78. *value = inw ( CONFIG_DATA + ( where & 2 ) );
  79. return 0;
  80. }
  81. static inline int pcidirect_read_config_dword ( struct pci_device *pci,
  82. unsigned int where,
  83. uint32_t *value ) {
  84. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  85. *value = inl ( CONFIG_DATA );
  86. return 0;
  87. }
  88. static inline int pcidirect_write_config_byte ( struct pci_device *pci,
  89. unsigned int where,
  90. uint8_t value ) {
  91. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  92. outb ( value, CONFIG_DATA + ( where & 3 ) );
  93. return 0;
  94. }
  95. static inline int pcidirect_write_config_word ( struct pci_device *pci,
  96. unsigned int where,
  97. uint16_t value ) {
  98. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  99. outw ( value, CONFIG_DATA + ( where & 2 ) );
  100. return 0;
  101. }
  102. static inline int pcidirect_write_config_dword ( struct pci_device *pci,
  103. unsigned int where,
  104. uint32_t value ) {
  105. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  106. outl ( value, CONFIG_DATA );
  107. return 0;
  108. }
  109. /*
  110. * Functions for accessing PCI configuration space directly via the
  111. * PCI BIOS.
  112. *
  113. * Under -DKEEP_IT_REAL, we use INT 1A, otherwise we use the BIOS32
  114. * interface.
  115. */
  116. #ifdef KEEP_IT_REAL
  117. static void find_pcibios16 ( void ) {
  118. uint16_t present;
  119. uint32_t signature;
  120. uint16_t flags;
  121. uint16_t revision;
  122. /* PCI BIOS installation check */
  123. REAL_EXEC ( rm_pcibios_check,
  124. "int $0x1a\n\t"
  125. "pushfw\n\t"
  126. "popw %%cx\n\t",
  127. 4,
  128. OUT_CONSTRAINTS ( "=a" ( present ), "=b" ( revision ),
  129. "=c" ( flags ), "=d" ( signature ) ),
  130. IN_CONSTRAINTS ( "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) +
  131. PCIBIOS_PCI_BIOS_PRESENT ) ),
  132. CLOBBER ( "esi", "edi", "ebp" ) );
  133. if ( ( flags & CF ) ||
  134. ( ( present >> 8 ) != 0 ) ||
  135. ( signature != PCI_SIGNATURE ) ) {
  136. DBG ( "PCI BIOS installation check failed\n" );
  137. return;
  138. }
  139. /* We have a PCI BIOS */
  140. DBG ( "Found 16-bit PCI BIOS interface\n" );
  141. have_pcibios = 1;
  142. return;
  143. }
  144. INIT_FN ( INIT_PCIBIOS, find_pcibios16, NULL, NULL );
  145. #define pcibios16_read_write( command, pci, where, value ) \
  146. ( { \
  147. uint32_t discard_b, discard_D; \
  148. uint16_t ret; \
  149. \
  150. REAL_EXEC ( 999, /* need a local label */ \
  151. "int $0x1a\n\t" \
  152. "jc 1f\n\t" \
  153. "xorw %%ax, %%ax\n\t" \
  154. "\n1:\n\t", \
  155. 5, \
  156. OUT_CONSTRAINTS ( "=a" ( ret ), \
  157. "=b" ( discard_b ), \
  158. "=c" ( value ), \
  159. "=D" ( discard_D ) ), \
  160. IN_CONSTRAINTS ( "a" ( command + \
  161. ( PCIBIOS_PCI_FUNCTION_ID << 8 ) ), \
  162. "b" ( pci->busdevfn ), \
  163. "c" ( value ), \
  164. "D" ( where ) ), \
  165. CLOBBER ( "edx", "esi", "ebp" ) ); \
  166. \
  167. ( ret >> 8 ); \
  168. } )
  169. #define pcibios_read_write pcibios16_read_write
  170. #else /* KEEP_IT_REAL */
  171. /*
  172. * Locate the BIOS32 service directory by scanning for a valid BIOS32
  173. * structure
  174. *
  175. */
  176. static struct bios32 * find_bios32 ( void ) {
  177. uint32_t address;
  178. /*
  179. * Follow the standard procedure for locating the BIOS32 Service
  180. * directory by scanning the permissible address range from
  181. * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  182. *
  183. */
  184. for ( address = 0xe0000 ; address < 0xffff0 ; address += 16 ) {
  185. struct bios32 * candidate = phys_to_virt ( address );
  186. unsigned int length, i;
  187. unsigned char sum;
  188. if ( candidate->signature != BIOS32_SIGNATURE )
  189. continue;
  190. length = candidate->length * 16;
  191. if ( ! length )
  192. continue;
  193. for ( sum = 0, i = 0 ; i < length ; i++ )
  194. sum += ( ( char * ) candidate ) [i];
  195. if ( sum != 0 )
  196. continue;
  197. if ( candidate->revision != 0 ) {
  198. DBG ( "unsupported BIOS32 revision %d at %#x\n",
  199. candidate->revision, address );
  200. continue;
  201. }
  202. DBG ( "BIOS32 Service Directory structure at %#x\n", address );
  203. return candidate;
  204. }
  205. return NULL;
  206. }
  207. /*
  208. * Look up a service in the BIOS32 service directory
  209. *
  210. */
  211. static unsigned long find_bios32_service ( struct bios32 * bios32,
  212. unsigned long service ) {
  213. uint8_t return_code;
  214. uint32_t address;
  215. uint32_t length;
  216. uint32_t entry;
  217. uint32_t discard;
  218. __asm__ ( FLAT_FAR_CALL_ESI
  219. : "=a" ( return_code ), "=b" ( address ),
  220. "=c" ( length ), "=d" ( entry ), "=S" ( discard )
  221. : "a" ( service ), "b" ( 0 ), "S" ( bios32->entry )
  222. : "edi", "ebp" );
  223. switch ( return_code ) {
  224. case BIOS32_SERVICE_PRESENT:
  225. DBG ( "BIOS32 service %c%c%c%c present at %#x\n",
  226. PRINT_BIOS_SIG ( service ), ( address + entry ) );
  227. return ( address + entry );
  228. case BIOS32_SERVICE_NOT_PRESENT:
  229. DBG ( "BIOS32 service %c%c%c%c : not present\n",
  230. PRINT_BIOS_SIG ( service ) );
  231. return 0;
  232. default: /* Shouldn't happen */
  233. DBG ( "BIOS32 returned %#x for service %c%c%c%c!\n",
  234. return_code, PRINT_BIOS_SIG ( service ) );
  235. return 0;
  236. }
  237. }
  238. /*
  239. * Find the 32-bit PCI BIOS interface, if present.
  240. *
  241. */
  242. static void find_pcibios32 ( void ) {
  243. struct bios32 *bios32;
  244. uint32_t signature;
  245. uint16_t present;
  246. uint32_t flags;
  247. uint16_t revision;
  248. uint32_t discard;
  249. /* Locate BIOS32 service directory */
  250. bios32 = find_bios32 ();
  251. if ( ! bios32 ) {
  252. DBG ( "No BIOS32\n" );
  253. return;
  254. }
  255. /* Locate PCI BIOS service */
  256. pcibios32_entry = find_bios32_service ( bios32, PCI_SERVICE );
  257. if ( ! pcibios32_entry ) {
  258. DBG ( "No PCI BIOS\n" );
  259. return;
  260. }
  261. /* PCI BIOS installation check */
  262. __asm__ ( FLAT_FAR_CALL_ESI
  263. "pushfl\n\t"
  264. "popl %%ecx\n\t"
  265. : "=a" ( present ), "=b" ( revision ), "=c" ( flags ),
  266. "=d" ( signature ), "=S" ( discard )
  267. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 )
  268. + PCIBIOS_PCI_BIOS_PRESENT ),
  269. "S" ( pcibios32_entry )
  270. : "edi", "ebp" );
  271. if ( ( flags & CF ) ||
  272. ( ( present >> 8 ) != 0 ) ||
  273. ( signature != PCI_SIGNATURE ) ) {
  274. DBG ( "PCI BIOS installation check failed\n" );
  275. return;
  276. }
  277. /* We have a PCI BIOS */
  278. DBG ( "Found 32-bit PCI BIOS interface at %#x\n", pcibios32_entry );
  279. have_pcibios = 1;
  280. return;
  281. }
  282. INIT_FN ( INIT_PCIBIOS, find_pcibios32, NULL, NULL );
  283. #define pcibios32_read_write( command, pci, where, value ) \
  284. ( { \
  285. uint32_t discard_b, discard_D, discard_S; \
  286. uint16_t ret; \
  287. \
  288. __asm__ ( FLAT_FAR_CALL_ESI \
  289. "jc 1f\n\t" \
  290. "xorl %%eax, %%eax\n\t" \
  291. "\n1:\n\t" \
  292. : "=a" ( ret ), "=b" ( discard_b ), \
  293. "=c" ( value ), \
  294. "=S" ( discard_S ), "=D" ( discard_D ) \
  295. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) \
  296. + command ), \
  297. "b" ( pci->busdevfn ), "c" ( value ), \
  298. "D" ( where ), "S" ( pcibios32_entry ) \
  299. : "edx", "ebp" ); \
  300. \
  301. ( ret >> 8 ); \
  302. } )
  303. #define pcibios_read_write pcibios32_read_write
  304. #endif /* KEEP_IT_REAL */
  305. static inline int pcibios_read_config_byte ( struct pci_device *pci,
  306. unsigned int where,
  307. uint8_t *value ) {
  308. return pcibios_read_write ( PCIBIOS_READ_CONFIG_BYTE,
  309. pci, where, *value );
  310. }
  311. static inline int pcibios_read_config_word ( struct pci_device *pci,
  312. unsigned int where,
  313. uint16_t *value ) {
  314. return pcibios_read_write ( PCIBIOS_READ_CONFIG_WORD,
  315. pci, where, *value );
  316. }
  317. static inline int pcibios_read_config_dword ( struct pci_device *pci,
  318. unsigned int where,
  319. uint32_t *value ) {
  320. return pcibios_read_write ( PCIBIOS_READ_CONFIG_DWORD,
  321. pci, where, *value );
  322. }
  323. static inline int pcibios_write_config_byte ( struct pci_device *pci,
  324. unsigned int where,
  325. uint8_t value ) {
  326. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_BYTE,
  327. pci, where, value );
  328. }
  329. static inline int pcibios_write_config_word ( struct pci_device *pci,
  330. unsigned int where,
  331. uint16_t value ) {
  332. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_WORD,
  333. pci, where, value );
  334. }
  335. static inline int pcibios_write_config_dword ( struct pci_device *pci,
  336. unsigned int where,
  337. uint32_t value ) {
  338. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_DWORD,
  339. pci, where, value );
  340. }
  341. /*
  342. * Functions for accessing PCI configuration space via the PCI BIOS if
  343. * present, otherwise directly via type 1 accesses.
  344. *
  345. */
  346. int pci_read_config_byte ( struct pci_device *pci, unsigned int where,
  347. uint8_t *value ) {
  348. return have_pcibios ?
  349. pcibios_read_config_byte ( pci, where, value ) :
  350. pcidirect_read_config_byte ( pci, where, value );
  351. }
  352. int pci_read_config_word ( struct pci_device *pci, unsigned int where,
  353. uint16_t *value ) {
  354. return have_pcibios ?
  355. pcibios_read_config_word ( pci, where, value ) :
  356. pcidirect_read_config_word ( pci, where, value );
  357. }
  358. int pci_read_config_dword ( struct pci_device *pci, unsigned int where,
  359. uint32_t *value ) {
  360. return have_pcibios ?
  361. pcibios_read_config_dword ( pci, where, value ) :
  362. pcidirect_read_config_dword ( pci, where, value );
  363. }
  364. int pci_write_config_byte ( struct pci_device *pci, unsigned int where,
  365. uint8_t value ) {
  366. return have_pcibios ?
  367. pcibios_write_config_byte ( pci, where, value ) :
  368. pcidirect_write_config_byte ( pci, where, value );
  369. }
  370. int pci_write_config_word ( struct pci_device *pci, unsigned int where,
  371. uint16_t value ) {
  372. return have_pcibios ?
  373. pcibios_write_config_word ( pci, where, value ) :
  374. pcidirect_write_config_word ( pci, where, value );
  375. }
  376. int pci_write_config_dword ( struct pci_device *pci, unsigned int where,
  377. uint32_t value ) {
  378. return have_pcibios ?
  379. pcibios_write_config_dword ( pci, where, value ) :
  380. pcidirect_write_config_dword ( pci, where, value );
  381. }
  382. unsigned long pci_bus_base ( struct pci_device *pci __unused ) {
  383. /* architecturally this must be 0 */
  384. return 0;
  385. }