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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443
  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. have_pcibios = 1;
  148. return;
  149. }
  150. INIT_FN ( INIT_PCIBIOS, find_pcibios16, NULL, NULL );
  151. #define pcibios16_read_write( command, pci, where, value ) \
  152. ( { \
  153. uint32_t discard_b, discard_D; \
  154. uint16_t ret; \
  155. \
  156. REAL_EXEC ( 999, /* need a local label */ \
  157. "int $0x1a\n\t" \
  158. "jc 1f\n\t" \
  159. "xorw %%ax, %%ax\n\t" \
  160. "\n1:\n\t", \
  161. 5, \
  162. OUT_CONSTRAINTS ( "=a" ( ret ), \
  163. "=b" ( discard_b ), \
  164. "=c" ( value ), \
  165. "=D" ( discard_D ) ), \
  166. IN_CONSTRAINTS ( "a" ( command + \
  167. ( PCIBIOS_PCI_FUNCTION_ID << 8 ) ), \
  168. "b" ( pci->busdevfn ), \
  169. "c" ( value ), \
  170. "D" ( where ) ), \
  171. CLOBBER ( "edx", "esi", "ebp" ) ); \
  172. \
  173. ( ret >> 8 ); \
  174. } )
  175. #define pcibios_read_write pcibios16_read_write
  176. #else /* KEEP_IT_REAL */
  177. /*
  178. * Locate the BIOS32 service directory by scanning for a valid BIOS32
  179. * structure
  180. *
  181. */
  182. static struct bios32 * find_bios32 ( void ) {
  183. uint32_t address;
  184. /*
  185. * Follow the standard procedure for locating the BIOS32 Service
  186. * directory by scanning the permissible address range from
  187. * 0xe0000 through 0xfffff for a valid BIOS32 structure.
  188. *
  189. */
  190. for ( address = 0xe0000 ; address < 0xffff0 ; address += 16 ) {
  191. struct bios32 * candidate = phys_to_virt ( address );
  192. unsigned int length, i;
  193. unsigned char sum;
  194. if ( candidate->signature != BIOS32_SIGNATURE )
  195. continue;
  196. length = candidate->length * 16;
  197. if ( ! length )
  198. continue;
  199. for ( sum = 0, i = 0 ; i < length ; i++ )
  200. sum += ( ( char * ) candidate ) [i];
  201. if ( sum != 0 )
  202. continue;
  203. if ( candidate->revision != 0 ) {
  204. DBG ( "unsupported BIOS32 revision %d at %#x\n",
  205. candidate->revision, address );
  206. continue;
  207. }
  208. DBG ( "BIOS32 Service Directory structure at %#x\n", address );
  209. return candidate;
  210. }
  211. return NULL;
  212. }
  213. /*
  214. * Look up a service in the BIOS32 service directory
  215. *
  216. */
  217. static unsigned long find_bios32_service ( struct bios32 * bios32,
  218. unsigned long service ) {
  219. uint8_t return_code;
  220. uint32_t address;
  221. uint32_t length;
  222. uint32_t entry;
  223. uint32_t discard;
  224. __asm__ ( FLAT_FAR_CALL_ESI
  225. : "=a" ( return_code ), "=b" ( address ),
  226. "=c" ( length ), "=d" ( entry ), "=S" ( discard )
  227. : "a" ( service ), "b" ( 0 ), "S" ( bios32->entry )
  228. : "edi", "ebp" );
  229. switch ( return_code ) {
  230. case BIOS32_SERVICE_PRESENT:
  231. return ( address + entry );
  232. case BIOS32_SERVICE_NOT_PRESENT:
  233. DBG ( "BIOS32 service %c%c%c%c : not present\n",
  234. PRINT_BIOS_SIG ( service ) );
  235. return 0;
  236. default: /* Shouldn't happen */
  237. DBG ( "BIOS32 returned %#x for service %c%c%c%c!\n",
  238. return_code, PRINT_BIOS_SIG ( service ) );
  239. return 0;
  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. have_pcibios = 1;
  279. return;
  280. }
  281. INIT_FN ( INIT_PCIBIOS, find_pcibios32, NULL, NULL );
  282. #define pcibios32_read_write( command, pci, where, value ) \
  283. ( { \
  284. uint32_t discard_b, discard_D, discard_S; \
  285. uint16_t ret; \
  286. \
  287. __asm__ ( FLAT_FAR_CALL_ESI \
  288. "jc 1f\n\t" \
  289. "xorl %%eax, %%eax\n\t" \
  290. "\n1:\n\t" \
  291. : "=a" ( ret ), "=b" ( discard_b ), \
  292. "=c" ( value ), \
  293. "=S" ( discard_S ), "=D" ( discard_D ) \
  294. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) \
  295. + command ), \
  296. "b" ( pci->busdevfn ), "c" ( value ), \
  297. "D" ( where ), "S" ( pcibios32_entry ) \
  298. : "edx", "ebp" ); \
  299. \
  300. ( ret >> 8 ); \
  301. } )
  302. #define pcibios_read_write pcibios32_read_write
  303. #endif /* KEEP_IT_REAL */
  304. static inline int pcibios_read_config_byte ( struct pci_device *pci,
  305. unsigned int where,
  306. uint8_t *value ) {
  307. return pcibios_read_write ( PCIBIOS_READ_CONFIG_BYTE,
  308. pci, where, *value );
  309. }
  310. static inline int pcibios_read_config_word ( struct pci_device *pci,
  311. unsigned int where,
  312. uint16_t *value ) {
  313. return pcibios_read_write ( PCIBIOS_READ_CONFIG_WORD,
  314. pci, where, *value );
  315. }
  316. static inline int pcibios_read_config_dword ( struct pci_device *pci,
  317. unsigned int where,
  318. uint32_t *value ) {
  319. return pcibios_read_write ( PCIBIOS_READ_CONFIG_DWORD,
  320. pci, where, *value );
  321. }
  322. static inline int pcibios_write_config_byte ( struct pci_device *pci,
  323. unsigned int where,
  324. uint8_t value ) {
  325. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_BYTE,
  326. pci, where, value );
  327. }
  328. static inline int pcibios_write_config_word ( struct pci_device *pci,
  329. unsigned int where,
  330. uint16_t value ) {
  331. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_WORD,
  332. pci, where, value );
  333. }
  334. static inline int pcibios_write_config_dword ( struct pci_device *pci,
  335. unsigned int where,
  336. uint32_t value ) {
  337. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_DWORD,
  338. pci, where, value );
  339. }
  340. /*
  341. * Functions for accessing PCI configuration space via the PCI BIOS if
  342. * present, otherwise directly via type 1 accesses.
  343. *
  344. */
  345. int pci_read_config_byte ( struct pci_device *pci, unsigned int where,
  346. uint8_t *value ) {
  347. return have_pcibios ?
  348. pcibios_read_config_byte ( pci, where, value ) :
  349. pcidirect_read_config_byte ( pci, where, value );
  350. }
  351. int pci_read_config_word ( struct pci_device *pci, unsigned int where,
  352. uint16_t *value ) {
  353. return have_pcibios ?
  354. pcibios_read_config_word ( pci, where, value ) :
  355. pcidirect_read_config_word ( pci, where, value );
  356. }
  357. int pci_read_config_dword ( struct pci_device *pci, unsigned int where,
  358. uint32_t *value ) {
  359. return have_pcibios ?
  360. pcibios_read_config_dword ( pci, where, value ) :
  361. pcidirect_read_config_dword ( pci, where, value );
  362. }
  363. int pci_write_config_byte ( struct pci_device *pci, unsigned int where,
  364. uint8_t value ) {
  365. return have_pcibios ?
  366. pcibios_write_config_byte ( pci, where, value ) :
  367. pcidirect_write_config_byte ( pci, where, value );
  368. }
  369. int pci_write_config_word ( struct pci_device *pci, unsigned int where,
  370. uint16_t value ) {
  371. return have_pcibios ?
  372. pcibios_write_config_word ( pci, where, value ) :
  373. pcidirect_write_config_word ( pci, where, value );
  374. }
  375. int pci_write_config_dword ( struct pci_device *pci, unsigned int where,
  376. uint32_t value ) {
  377. return have_pcibios ?
  378. pcibios_write_config_dword ( pci, where, value ) :
  379. pcidirect_write_config_dword ( pci, where, value );
  380. }
  381. unsigned long pci_bus_base ( struct pci_device *pci __unused ) {
  382. /* architecturally this must be 0 */
  383. return 0;
  384. }