Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449
  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. * : "=S" ( discard, or real output ), <other output registers>
  54. * : "S" ( entry_point ), <other input registers> );
  55. * "=S" *must* be specified as an output, otherwise the compiler will
  56. * assume that it remains unaltered.
  57. */
  58. #define FLAT_FAR_CALL_ESI "call _virt_to_phys\n\t" \
  59. "pushl %%cs\n\t" \
  60. "call *%%esi\n\t" \
  61. "cli\n\t" \
  62. "cld\n\t" \
  63. "call _phys_to_virt\n\t"
  64. /*
  65. * Functions for accessing PCI configuration space directly with type
  66. * 1 accesses.
  67. *
  68. */
  69. static inline int pcidirect_read_config_byte ( struct pci_device *pci,
  70. unsigned int where,
  71. uint8_t *value ) {
  72. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  73. *value = inb ( CONFIG_DATA + ( where & 3 ) );
  74. return 0;
  75. }
  76. static inline int pcidirect_read_config_word ( struct pci_device *pci,
  77. unsigned int where,
  78. uint16_t *value ) {
  79. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  80. *value = inw ( CONFIG_DATA + ( where & 2 ) );
  81. return 0;
  82. }
  83. static inline int pcidirect_read_config_dword ( struct pci_device *pci,
  84. unsigned int where,
  85. uint32_t *value ) {
  86. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  87. *value = inl ( CONFIG_DATA );
  88. return 0;
  89. }
  90. static inline int pcidirect_write_config_byte ( struct pci_device *pci,
  91. unsigned int where,
  92. uint8_t value ) {
  93. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  94. outb ( value, CONFIG_DATA + ( where & 3 ) );
  95. return 0;
  96. }
  97. static inline int pcidirect_write_config_word ( struct pci_device *pci,
  98. unsigned int where,
  99. uint16_t value ) {
  100. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  101. outw ( value, CONFIG_DATA + ( where & 2 ) );
  102. return 0;
  103. }
  104. static inline int pcidirect_write_config_dword ( struct pci_device *pci,
  105. unsigned int where,
  106. uint32_t value ) {
  107. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  108. outl ( value, CONFIG_DATA );
  109. return 0;
  110. }
  111. /*
  112. * Functions for accessing PCI configuration space directly via the
  113. * PCI BIOS.
  114. *
  115. * Under -DKEEP_IT_REAL, we use INT 1A, otherwise we use the BIOS32
  116. * interface.
  117. */
  118. #ifdef KEEP_IT_REAL
  119. static void find_pcibios16 ( void ) {
  120. uint16_t present;
  121. uint32_t signature;
  122. uint16_t flags;
  123. uint16_t revision;
  124. uint8_t max_bus;
  125. /* PCI BIOS installation check */
  126. REAL_EXEC ( rm_pcibios_check,
  127. "int $0x1a\n\t"
  128. "pushfw\n\t"
  129. "popw %%si\n\t",
  130. 5,
  131. OUT_CONSTRAINTS ( "=a" ( present ), "=b" ( revision ),
  132. "=c" ( max_bus ), "=d" ( signature ),
  133. "=S" ( flags ) ),
  134. IN_CONSTRAINTS ( "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) +
  135. PCIBIOS_PCI_BIOS_PRESENT ) ),
  136. CLOBBER ( "edi", "ebp" ) );
  137. if ( ( flags & CF ) ||
  138. ( ( present >> 8 ) != 0 ) ||
  139. ( signature != PCI_SIGNATURE ) ) {
  140. DBG ( "PCI BIOS installation check failed\n" );
  141. return;
  142. }
  143. /* We have a PCI BIOS */
  144. DBG ( "Found 16-bit PCI BIOS interface with %d buses\n", max_bus + 1 );
  145. have_pcibios = 1;
  146. pci_max_bus = max_bus;
  147. return;
  148. }
  149. INIT_FN ( INIT_PCIBIOS, find_pcibios16, NULL, NULL );
  150. #define pcibios16_read_write( command, pci, where, value ) \
  151. ( { \
  152. uint32_t discard_b, discard_D; \
  153. uint16_t ret; \
  154. \
  155. REAL_EXEC ( 999, /* need a local label */ \
  156. "int $0x1a\n\t" \
  157. "jc 1f\n\t" \
  158. "xorw %%ax, %%ax\n\t" \
  159. "\n1:\n\t", \
  160. 5, \
  161. OUT_CONSTRAINTS ( "=a" ( ret ), \
  162. "=b" ( discard_b ), \
  163. "=c" ( value ), \
  164. "=D" ( discard_D ) ), \
  165. IN_CONSTRAINTS ( "a" ( command + \
  166. ( PCIBIOS_PCI_FUNCTION_ID << 8 ) ), \
  167. "b" ( pci->busdevfn ), \
  168. "c" ( value ), \
  169. "D" ( where ) ), \
  170. CLOBBER ( "edx", "esi", "ebp" ) ); \
  171. \
  172. ( ret >> 8 ); \
  173. } )
  174. #define pcibios_read_write pcibios16_read_write
  175. #else /* KEEP_IT_REAL */
  176. /*
  177. * Locate the BIOS32 service directory by scanning for a valid BIOS32
  178. * structure
  179. *
  180. */
  181. static struct bios32 * find_bios32 ( void ) {
  182. uint32_t address;
  183. /*
  184. * Follow the standard procedure for locating the BIOS32 Service
  185. * directory by scanning the permissible address range from
  186. * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  187. *
  188. */
  189. for ( address = 0xe0000 ; address < 0xffff0 ; address += 16 ) {
  190. struct bios32 * candidate = phys_to_virt ( address );
  191. unsigned int length, i;
  192. unsigned char sum;
  193. if ( candidate->signature != BIOS32_SIGNATURE )
  194. continue;
  195. length = candidate->length * 16;
  196. if ( ! length )
  197. continue;
  198. for ( sum = 0, i = 0 ; i < length ; i++ )
  199. sum += ( ( char * ) candidate ) [i];
  200. if ( sum != 0 )
  201. continue;
  202. if ( candidate->revision != 0 ) {
  203. DBG ( "unsupported BIOS32 revision %d at %#x\n",
  204. candidate->revision, address );
  205. continue;
  206. }
  207. DBG ( "BIOS32 Service Directory structure at %#x\n", address );
  208. return candidate;
  209. }
  210. return NULL;
  211. }
  212. /*
  213. * Look up a service in the BIOS32 service directory
  214. *
  215. */
  216. static unsigned long find_bios32_service ( struct bios32 * bios32,
  217. unsigned long service ) {
  218. uint8_t return_code;
  219. uint32_t address;
  220. uint32_t length;
  221. uint32_t entry;
  222. uint32_t discard;
  223. __asm__ ( FLAT_FAR_CALL_ESI
  224. : "=a" ( return_code ), "=b" ( address ),
  225. "=c" ( length ), "=d" ( entry ), "=S" ( discard )
  226. : "a" ( service ), "b" ( 0 ), "S" ( bios32->entry )
  227. : "edi", "ebp" );
  228. switch ( return_code ) {
  229. case BIOS32_SERVICE_PRESENT:
  230. DBG ( "BIOS32 service %c%c%c%c present at %#x\n",
  231. PRINT_BIOS_SIG ( service ), ( address + entry ) );
  232. return ( address + entry );
  233. case BIOS32_SERVICE_NOT_PRESENT:
  234. DBG ( "BIOS32 service %c%c%c%c : not present\n",
  235. PRINT_BIOS_SIG ( service ) );
  236. return 0;
  237. default: /* Shouldn't happen */
  238. DBG ( "BIOS32 returned %#x for service %c%c%c%c!\n",
  239. return_code, PRINT_BIOS_SIG ( service ) );
  240. return 0;
  241. }
  242. }
  243. /*
  244. * Find the 32-bit PCI BIOS interface, if present.
  245. *
  246. */
  247. static void find_pcibios32 ( void ) {
  248. struct bios32 *bios32;
  249. uint32_t signature;
  250. uint16_t present;
  251. uint32_t flags;
  252. uint16_t revision;
  253. uint8_t max_bus;
  254. /* Locate BIOS32 service directory */
  255. bios32 = find_bios32 ();
  256. if ( ! bios32 ) {
  257. DBG ( "No BIOS32\n" );
  258. return;
  259. }
  260. /* Locate PCI BIOS service */
  261. pcibios32_entry = find_bios32_service ( bios32, PCI_SERVICE );
  262. if ( ! pcibios32_entry ) {
  263. DBG ( "No PCI BIOS\n" );
  264. return;
  265. }
  266. /* PCI BIOS installation check */
  267. __asm__ ( FLAT_FAR_CALL_ESI
  268. "pushfl\n\t"
  269. "popl %%esi\n\t"
  270. : "=a" ( present ), "=b" ( revision ), "=c" ( max_bus ),
  271. "=d" ( signature ), "=S" ( flags )
  272. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 )
  273. + PCIBIOS_PCI_BIOS_PRESENT ),
  274. "S" ( pcibios32_entry )
  275. : "edi", "ebp" );
  276. if ( ( flags & CF ) ||
  277. ( ( present >> 8 ) != 0 ) ||
  278. ( signature != PCI_SIGNATURE ) ) {
  279. DBG ( "PCI BIOS installation check failed\n" );
  280. return;
  281. }
  282. /* We have a PCI BIOS */
  283. DBG ( "Found 32-bit PCI BIOS interface at %#x with %d bus(es)\n",
  284. pcibios32_entry, max_bus + 1 );
  285. have_pcibios = 1;
  286. pci_max_bus = max_bus;
  287. return;
  288. }
  289. INIT_FN ( INIT_PCIBIOS, find_pcibios32, NULL, NULL );
  290. #define pcibios32_read_write( command, pci, where, value ) \
  291. ( { \
  292. uint32_t discard_b, discard_D, discard_S; \
  293. uint16_t ret; \
  294. \
  295. __asm__ ( FLAT_FAR_CALL_ESI \
  296. "jc 1f\n\t" \
  297. "xorl %%eax, %%eax\n\t" \
  298. "\n1:\n\t" \
  299. : "=a" ( ret ), "=b" ( discard_b ), \
  300. "=c" ( value ), \
  301. "=S" ( discard_S ), "=D" ( discard_D ) \
  302. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) \
  303. + command ), \
  304. "b" ( pci->busdevfn ), "c" ( value ), \
  305. "D" ( where ), "S" ( pcibios32_entry ) \
  306. : "edx", "ebp" ); \
  307. \
  308. ( ret >> 8 ); \
  309. } )
  310. #define pcibios_read_write pcibios32_read_write
  311. #endif /* KEEP_IT_REAL */
  312. static inline int pcibios_read_config_byte ( struct pci_device *pci,
  313. unsigned int where,
  314. uint8_t *value ) {
  315. return pcibios_read_write ( PCIBIOS_READ_CONFIG_BYTE,
  316. pci, where, *value );
  317. }
  318. static inline int pcibios_read_config_word ( struct pci_device *pci,
  319. unsigned int where,
  320. uint16_t *value ) {
  321. return pcibios_read_write ( PCIBIOS_READ_CONFIG_WORD,
  322. pci, where, *value );
  323. }
  324. static inline int pcibios_read_config_dword ( struct pci_device *pci,
  325. unsigned int where,
  326. uint32_t *value ) {
  327. return pcibios_read_write ( PCIBIOS_READ_CONFIG_DWORD,
  328. pci, where, *value );
  329. }
  330. static inline int pcibios_write_config_byte ( struct pci_device *pci,
  331. unsigned int where,
  332. uint8_t value ) {
  333. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_BYTE,
  334. pci, where, value );
  335. }
  336. static inline int pcibios_write_config_word ( struct pci_device *pci,
  337. unsigned int where,
  338. uint16_t value ) {
  339. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_WORD,
  340. pci, where, value );
  341. }
  342. static inline int pcibios_write_config_dword ( struct pci_device *pci,
  343. unsigned int where,
  344. uint32_t value ) {
  345. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_DWORD,
  346. pci, where, value );
  347. }
  348. /*
  349. * Functions for accessing PCI configuration space via the PCI BIOS if
  350. * present, otherwise directly via type 1 accesses.
  351. *
  352. */
  353. int pci_read_config_byte ( struct pci_device *pci, unsigned int where,
  354. uint8_t *value ) {
  355. return have_pcibios ?
  356. pcibios_read_config_byte ( pci, where, value ) :
  357. pcidirect_read_config_byte ( pci, where, value );
  358. }
  359. int pci_read_config_word ( struct pci_device *pci, unsigned int where,
  360. uint16_t *value ) {
  361. return have_pcibios ?
  362. pcibios_read_config_word ( pci, where, value ) :
  363. pcidirect_read_config_word ( pci, where, value );
  364. }
  365. int pci_read_config_dword ( struct pci_device *pci, unsigned int where,
  366. uint32_t *value ) {
  367. return have_pcibios ?
  368. pcibios_read_config_dword ( pci, where, value ) :
  369. pcidirect_read_config_dword ( pci, where, value );
  370. }
  371. int pci_write_config_byte ( struct pci_device *pci, unsigned int where,
  372. uint8_t value ) {
  373. return have_pcibios ?
  374. pcibios_write_config_byte ( pci, where, value ) :
  375. pcidirect_write_config_byte ( pci, where, value );
  376. }
  377. int pci_write_config_word ( struct pci_device *pci, unsigned int where,
  378. uint16_t value ) {
  379. return have_pcibios ?
  380. pcibios_write_config_word ( pci, where, value ) :
  381. pcidirect_write_config_word ( pci, where, value );
  382. }
  383. int pci_write_config_dword ( struct pci_device *pci, unsigned int where,
  384. uint32_t value ) {
  385. return have_pcibios ?
  386. pcibios_write_config_dword ( pci, where, value ) :
  387. pcidirect_write_config_dword ( pci, where, value );
  388. }
  389. unsigned long pci_bus_base ( struct pci_device *pci __unused ) {
  390. /* architecturally this must be 0 */
  391. return 0;
  392. }