You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

pci_io.c 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. #define DEBUG_PCI_IO
  21. #undef DBG
  22. #ifdef DEBUG_PCI_IO
  23. #define DBG(...) printf ( __VA_ARGS__ )
  24. #else
  25. #define DBG(...)
  26. #endif
  27. /* Macros for direct PCI access */
  28. #define CONFIG_ADDRESS 0xcf8
  29. #define CONFIG_DATA 0xcfc
  30. #define CONFIG_CMD( pci, where ) \
  31. ( 0x80000000 | (pci->busdevfn << 8) | (where & ~3) )
  32. /* Signatures for PCI BIOS */
  33. #define BIOS_SIG(a,b,c,d) ( ( a<<0 ) + ( b<<8 ) + ( c<<16 ) + ( d<<24 ) )
  34. #define PRINT_BIOS_SIG(x) ( (x) & 0xff ), ( ( (x)>>8 ) & 0xff ), \
  35. ( ( (x)>>16 ) & 0xff ),( ( (x)>>24 ) & 0xff )
  36. #define BIOS32_SIGNATURE BIOS_SIG ( '_', '3', '2', '_' )
  37. #define PCI_SIGNATURE BIOS_SIG ( 'P', 'C', 'I', ' ' )
  38. #define PCI_SERVICE BIOS_SIG ( '$', 'P', 'C', 'I' )
  39. /* BIOS32 structure as found in PCI BIOS ROM */
  40. struct bios32 {
  41. unsigned long signature; /* _32_ */
  42. unsigned long entry; /* 32 bit physical address */
  43. unsigned char revision; /* Revision level, 0 */
  44. unsigned char length; /* Length in paragraphs */
  45. unsigned char checksum; /* Should byte sum to zero */
  46. unsigned char reserved[5]; /* Must be zero */
  47. };
  48. /* Values returned by BIOS32 service directory */
  49. #define BIOS32_SERVICE_PRESENT 0x00
  50. #define BIOS32_SERVICE_NOT_PRESENT 0x80
  51. #define CF ( 1 << 0 )
  52. /* PCI BIOS entry point */
  53. #ifndef KEEP_IT_REAL
  54. static unsigned long pcibios32_entry;
  55. #endif
  56. static int have_pcibios;
  57. /* Macro for calling a 32-bit entry point with flat physical
  58. * addresses. Use in a statement such as
  59. * __asm__ ( FLAT_FAR_CALL_ESI,
  60. * : <output registers>
  61. * : "S" ( entry_point ), <other input registers> );
  62. */
  63. #define FLAT_FAR_CALL_ESI "call _virt_to_phys\n\t" \
  64. "pushl %%cs\n\t" \
  65. "call *%%esi\n\t" \
  66. "cli\n\t" \
  67. "cld\n\t" \
  68. "call _phys_to_virt\n\t"
  69. /*
  70. * Functions for accessing PCI configuration space directly with type
  71. * 1 accesses.
  72. *
  73. */
  74. static inline int pcidirect_read_config_byte ( struct pci_device *pci,
  75. unsigned int where,
  76. uint8_t *value ) {
  77. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  78. *value = inb ( CONFIG_DATA + ( where & 3 ) );
  79. return 0;
  80. }
  81. static inline int pcidirect_read_config_word ( struct pci_device *pci,
  82. unsigned int where,
  83. uint16_t *value ) {
  84. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  85. *value = inw ( CONFIG_DATA + ( where & 2 ) );
  86. return 0;
  87. }
  88. static inline int pcidirect_read_config_dword ( struct pci_device *pci,
  89. unsigned int where,
  90. uint32_t *value ) {
  91. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  92. *value = inl ( CONFIG_DATA );
  93. return 0;
  94. }
  95. static inline int pcidirect_write_config_byte ( struct pci_device *pci,
  96. unsigned int where,
  97. uint8_t value ) {
  98. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  99. outb ( value, CONFIG_DATA + ( where & 3 ) );
  100. return 0;
  101. }
  102. static inline int pcidirect_write_config_word ( struct pci_device *pci,
  103. unsigned int where,
  104. uint16_t value ) {
  105. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  106. outw ( value, CONFIG_DATA + ( where & 2 ) );
  107. return 0;
  108. }
  109. static inline int pcidirect_write_config_dword ( struct pci_device *pci,
  110. unsigned int where,
  111. uint32_t value ) {
  112. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  113. outl ( value, CONFIG_DATA );
  114. return 0;
  115. }
  116. /*
  117. * Functions for accessing PCI configuration space directly via the
  118. * PCI BIOS.
  119. *
  120. * Under -DKEEP_IT_REAL, we use INT 1A, otherwise we use the BIOS32
  121. * interface.
  122. */
  123. #ifdef KEEP_IT_REAL
  124. static void find_pcibios16 ( void ) {
  125. uint16_t present;
  126. uint32_t signature;
  127. uint16_t flags;
  128. uint16_t revision;
  129. /* PCI BIOS installation check */
  130. REAL_EXEC ( rm_pcibios_check,
  131. "int $0x1a\n\t"
  132. "pushfw\n\t"
  133. "popw %%cx\n\t",
  134. 4,
  135. OUT_CONSTRAINTS ( "=a" ( present ), "=b" ( revision ),
  136. "=c" ( flags ), "=d" ( signature ) ),
  137. IN_CONSTRAINTS ( "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) +
  138. PCIBIOS_PCI_BIOS_PRESENT ) ),
  139. CLOBBER ( "esi", "edi", "ebp" ) );
  140. if ( ( flags & CF ) ||
  141. ( ( present >> 8 ) != 0 ) ||
  142. ( signature != PCI_SIGNATURE ) ) {
  143. DBG ( "PCI BIOS installation check failed\n" );
  144. return;
  145. }
  146. /* We have a PCI BIOS */
  147. DBG ( "Found 16-bit PCI BIOS interface\n" );
  148. have_pcibios = 1;
  149. return;
  150. }
  151. INIT_FN ( INIT_PCIBIOS, find_pcibios16, NULL, NULL );
  152. #define pcibios16_read_write( command, pci, where, value ) \
  153. ( { \
  154. uint32_t discard_b, discard_D; \
  155. uint16_t ret; \
  156. \
  157. REAL_EXEC ( 999, /* need a local label */ \
  158. "int $0x1a\n\t" \
  159. "jc 1f\n\t" \
  160. "xorw %%ax, %%ax\n\t" \
  161. "\n1:\n\t", \
  162. 5, \
  163. OUT_CONSTRAINTS ( "=a" ( ret ), \
  164. "=b" ( discard_b ), \
  165. "=c" ( value ), \
  166. "=D" ( discard_D ) ), \
  167. IN_CONSTRAINTS ( "a" ( command + \
  168. ( PCIBIOS_PCI_FUNCTION_ID << 8 ) ), \
  169. "b" ( pci->busdevfn ), \
  170. "c" ( value ), \
  171. "D" ( where ) ), \
  172. CLOBBER ( "edx", "esi", "ebp" ) ); \
  173. \
  174. ( ret >> 8 ); \
  175. } )
  176. #define pcibios_read_write pcibios16_read_write
  177. #else /* KEEP_IT_REAL */
  178. /*
  179. * Locate the BIOS32 service directory by scanning for a valid BIOS32
  180. * structure
  181. *
  182. */
  183. static struct bios32 * find_bios32 ( void ) {
  184. uint32_t address;
  185. /*
  186. * Follow the standard procedure for locating the BIOS32 Service
  187. * directory by scanning the permissible address range from
  188. * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  189. *
  190. */
  191. for ( address = 0xe0000 ; address < 0xffff0 ; address += 16 ) {
  192. struct bios32 * candidate = phys_to_virt ( address );
  193. unsigned int length, i;
  194. unsigned char sum;
  195. if ( candidate->signature != BIOS32_SIGNATURE )
  196. continue;
  197. length = candidate->length * 16;
  198. if ( ! length )
  199. continue;
  200. for ( sum = 0, i = 0 ; i < length ; i++ )
  201. sum += ( ( char * ) candidate ) [i];
  202. if ( sum != 0 )
  203. continue;
  204. if ( candidate->revision != 0 ) {
  205. DBG ( "unsupported BIOS32 revision %d at %#x\n",
  206. candidate->revision, address );
  207. continue;
  208. }
  209. DBG ( "BIOS32 Service Directory structure at %#x\n", address );
  210. return candidate;
  211. }
  212. return NULL;
  213. }
  214. /*
  215. * Look up a service in the BIOS32 service directory
  216. *
  217. */
  218. static unsigned long find_bios32_service ( struct bios32 * bios32,
  219. unsigned long service ) {
  220. uint8_t return_code;
  221. uint32_t address;
  222. uint32_t length;
  223. uint32_t entry;
  224. uint32_t discard;
  225. __asm__ ( FLAT_FAR_CALL_ESI
  226. : "=a" ( return_code ), "=b" ( address ),
  227. "=c" ( length ), "=d" ( entry ), "=S" ( discard )
  228. : "a" ( service ), "b" ( 0 ), "S" ( bios32->entry )
  229. : "edi", "ebp" );
  230. switch ( return_code ) {
  231. case BIOS32_SERVICE_PRESENT:
  232. DBG ( "BIOS32 service %c%c%c%c present at %#x\n",
  233. PRINT_BIOS_SIG ( service ), ( address + entry ) );
  234. return ( address + entry );
  235. case BIOS32_SERVICE_NOT_PRESENT:
  236. DBG ( "BIOS32 service %c%c%c%c : not present\n",
  237. PRINT_BIOS_SIG ( service ) );
  238. return 0;
  239. default: /* Shouldn't happen */
  240. DBG ( "BIOS32 returned %#x for service %c%c%c%c!\n",
  241. return_code, PRINT_BIOS_SIG ( service ) );
  242. return 0;
  243. }
  244. }
  245. /*
  246. * Find the 32-bit PCI BIOS interface, if present.
  247. *
  248. */
  249. static void find_pcibios32 ( void ) {
  250. struct bios32 *bios32;
  251. uint32_t signature;
  252. uint16_t present;
  253. uint32_t flags;
  254. uint16_t revision;
  255. uint32_t discard;
  256. /* Locate BIOS32 service directory */
  257. bios32 = find_bios32 ();
  258. if ( ! bios32 ) {
  259. DBG ( "No BIOS32\n" );
  260. return;
  261. }
  262. /* Locate PCI BIOS service */
  263. pcibios32_entry = find_bios32_service ( bios32, PCI_SERVICE );
  264. if ( ! pcibios32_entry ) {
  265. DBG ( "No PCI BIOS\n" );
  266. return;
  267. }
  268. /* PCI BIOS installation check */
  269. __asm__ ( FLAT_FAR_CALL_ESI
  270. "pushfl\n\t"
  271. "popl %%ecx\n\t"
  272. : "=a" ( present ), "=b" ( revision ), "=c" ( flags ),
  273. "=d" ( signature ), "=S" ( discard )
  274. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 )
  275. + PCIBIOS_PCI_BIOS_PRESENT ),
  276. "S" ( pcibios32_entry )
  277. : "edi", "ebp" );
  278. if ( ( flags & CF ) ||
  279. ( ( present >> 8 ) != 0 ) ||
  280. ( signature != PCI_SIGNATURE ) ) {
  281. DBG ( "PCI BIOS installation check failed\n" );
  282. return;
  283. }
  284. /* We have a PCI BIOS */
  285. DBG ( "Found 32-bit PCI BIOS interface at %#x\n", pcibios32_entry );
  286. have_pcibios = 1;
  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. }