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

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. #undef DBG
  21. #ifdef DEBUG_PCI_IO
  22. #define DBG(...) printf ( __VA_ARGS__ )
  23. #else
  24. #define DBG(...)
  25. #endif
  26. /* Macros for direct PCI access */
  27. #define CONFIG_ADDRESS 0xcf8
  28. #define CONFIG_DATA 0xcfc
  29. #define CONFIG_CMD( pci, where ) \
  30. ( 0x80000000 | (pci->busdevfn << 8) | (where & ~3) )
  31. /* Signatures for PCI BIOS */
  32. #define BIOS_SIG(a,b,c,d) ( ( a<<0 ) + ( b<<8 ) + ( c<<16 ) + ( d<<24 ) )
  33. #define PRINT_BIOS_SIG(x) ( (x) & 0xff ), ( ( (x)>>8 ) & 0xff ), \
  34. ( ( (x)>>16 ) & 0xff ),( ( (x)>>24 ) & 0xff )
  35. #define BIOS32_SIGNATURE BIOS_SIG ( '_', '3', '2', '_' )
  36. #define PCI_SIGNATURE BIOS_SIG ( 'P', 'C', 'I', ' ' )
  37. #define PCI_SERVICE BIOS_SIG ( '$', 'P', 'C', 'I' )
  38. /* BIOS32 structure as found in PCI BIOS ROM */
  39. struct bios32 {
  40. unsigned long signature; /* _32_ */
  41. unsigned long entry; /* 32 bit physical address */
  42. unsigned char revision; /* Revision level, 0 */
  43. unsigned char length; /* Length in paragraphs */
  44. unsigned char checksum; /* Should byte sum to zero */
  45. unsigned char reserved[5]; /* Must be zero */
  46. };
  47. /* Values returned by BIOS32 service directory */
  48. #define BIOS32_SERVICE_PRESENT 0x00
  49. #define BIOS32_SERVICE_NOT_PRESENT 0x80
  50. #define CF ( 1 << 0 )
  51. /* PCI BIOS entry point */
  52. #ifndef KEEP_IT_REAL
  53. static unsigned long pcibios32_entry;
  54. #endif
  55. static int have_pcibios;
  56. /* Macro for calling a 32-bit entry point with flat physical
  57. * addresses. Use in a statement such as
  58. * __asm__ ( FLAT_FAR_CALL_ESI,
  59. * : <output registers>
  60. * : "S" ( entry_point ), <other input registers> );
  61. */
  62. #define FLAT_FAR_CALL_ESI "call _virt_to_phys\n\t" \
  63. "pushl %%cs\n\t" \
  64. "call *%%esi\n\t" \
  65. "cli\n\t" \
  66. "cld\n\t" \
  67. "call _phys_to_virt\n\t"
  68. /*
  69. * Functions for accessing PCI configuration space directly with type
  70. * 1 accesses.
  71. *
  72. */
  73. static inline int pcidirect_read_config_byte ( struct pci_device *pci,
  74. unsigned int where,
  75. uint8_t *value ) {
  76. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  77. *value = inb ( CONFIG_DATA + ( where & 3 ) );
  78. return 0;
  79. }
  80. static inline int pcidirect_read_config_word ( struct pci_device *pci,
  81. unsigned int where,
  82. uint16_t *value ) {
  83. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  84. *value = inw ( CONFIG_DATA + ( where & 2 ) );
  85. return 0;
  86. }
  87. static inline int pcidirect_read_config_dword ( struct pci_device *pci,
  88. unsigned int where,
  89. uint32_t *value ) {
  90. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  91. *value = inl ( CONFIG_DATA );
  92. return 0;
  93. }
  94. static inline int pcidirect_write_config_byte ( struct pci_device *pci,
  95. unsigned int where,
  96. uint8_t value ) {
  97. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  98. outb ( value, CONFIG_DATA + ( where & 3 ) );
  99. return 0;
  100. }
  101. static inline int pcidirect_write_config_word ( struct pci_device *pci,
  102. unsigned int where,
  103. uint16_t value ) {
  104. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  105. outw ( value, CONFIG_DATA + ( where & 2 ) );
  106. return 0;
  107. }
  108. static inline int pcidirect_write_config_dword ( struct pci_device *pci,
  109. unsigned int where,
  110. uint32_t value ) {
  111. outl ( CONFIG_CMD ( pci, where ), CONFIG_ADDRESS );
  112. outl ( value, CONFIG_DATA );
  113. return 0;
  114. }
  115. /*
  116. * Functions for accessing PCI configuration space directly via the
  117. * PCI BIOS.
  118. *
  119. * Under -DKEEP_IT_REAL, we use INT 1A, otherwise we use the BIOS32
  120. * interface.
  121. */
  122. #ifdef KEEP_IT_REAL
  123. static void find_pcibios16 ( void ) {
  124. uint16_t present;
  125. uint32_t signature;
  126. uint16_t flags;
  127. uint16_t revision;
  128. /* PCI BIOS installation check */
  129. REAL_EXEC ( rm_pcibios_check,
  130. "int $0x1a\n\t"
  131. "pushfw\n\t"
  132. "popw %%cx\n\t",
  133. 4,
  134. OUT_CONSTRAINTS ( "=a" ( present ), "=b" ( revision ),
  135. "=c" ( flags ), "=d" ( signature ) ),
  136. IN_CONSTRAINTS ( "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) +
  137. PCIBIOS_PCI_BIOS_PRESENT ) ),
  138. CLOBBER ( "esi", "edi", "ebp" ) );
  139. if ( ( flags & CF ) ||
  140. ( ( present >> 8 ) != 0 ) ||
  141. ( signature != PCI_SIGNATURE ) ) {
  142. DBG ( "PCI BIOS installation check failed\n" );
  143. return;
  144. }
  145. /* We have a PCI BIOS */
  146. DBG ( "Found 16-bit PCI BIOS interface\n" );
  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. DBG ( "BIOS32 service %c%c%c%c present at %#x\n",
  232. PRINT_BIOS_SIG ( service ), ( address + entry ) );
  233. return ( address + entry );
  234. case BIOS32_SERVICE_NOT_PRESENT:
  235. DBG ( "BIOS32 service %c%c%c%c : not present\n",
  236. PRINT_BIOS_SIG ( service ) );
  237. return 0;
  238. default: /* Shouldn't happen */
  239. DBG ( "BIOS32 returned %#x for service %c%c%c%c!\n",
  240. return_code, PRINT_BIOS_SIG ( service ) );
  241. return 0;
  242. }
  243. }
  244. /*
  245. * Find the 32-bit PCI BIOS interface, if present.
  246. *
  247. */
  248. static void find_pcibios32 ( void ) {
  249. struct bios32 *bios32;
  250. uint32_t signature;
  251. uint16_t present;
  252. uint32_t flags;
  253. uint16_t revision;
  254. uint32_t discard;
  255. /* Locate BIOS32 service directory */
  256. bios32 = find_bios32 ();
  257. if ( ! bios32 ) {
  258. DBG ( "No BIOS32\n" );
  259. return;
  260. }
  261. /* Locate PCI BIOS service */
  262. pcibios32_entry = find_bios32_service ( bios32, PCI_SERVICE );
  263. if ( ! pcibios32_entry ) {
  264. DBG ( "No PCI BIOS\n" );
  265. return;
  266. }
  267. /* PCI BIOS installation check */
  268. __asm__ ( FLAT_FAR_CALL_ESI
  269. "pushfl\n\t"
  270. "popl %%ecx\n\t"
  271. : "=a" ( present ), "=b" ( revision ), "=c" ( flags ),
  272. "=d" ( signature ), "=S" ( discard )
  273. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 )
  274. + PCIBIOS_PCI_BIOS_PRESENT ),
  275. "S" ( pcibios32_entry )
  276. : "edi", "ebp" );
  277. if ( ( flags & CF ) ||
  278. ( ( present >> 8 ) != 0 ) ||
  279. ( signature != PCI_SIGNATURE ) ) {
  280. DBG ( "PCI BIOS installation check failed\n" );
  281. return;
  282. }
  283. /* We have a PCI BIOS */
  284. DBG ( "Found 32-bit PCI BIOS interface at %#x\n", pcibios32_entry );
  285. have_pcibios = 1;
  286. return;
  287. }
  288. INIT_FN ( INIT_PCIBIOS, find_pcibios32, NULL, NULL );
  289. #define pcibios32_read_write( command, pci, where, value ) \
  290. ( { \
  291. uint32_t discard_b, discard_D, discard_S; \
  292. uint16_t ret; \
  293. \
  294. __asm__ ( FLAT_FAR_CALL_ESI \
  295. "jc 1f\n\t" \
  296. "xorl %%eax, %%eax\n\t" \
  297. "\n1:\n\t" \
  298. : "=a" ( ret ), "=b" ( discard_b ), \
  299. "=c" ( value ), \
  300. "=S" ( discard_S ), "=D" ( discard_D ) \
  301. : "a" ( ( PCIBIOS_PCI_FUNCTION_ID << 8 ) \
  302. + command ), \
  303. "b" ( pci->busdevfn ), "c" ( value ), \
  304. "D" ( where ), "S" ( pcibios32_entry ) \
  305. : "edx", "ebp" ); \
  306. \
  307. ( ret >> 8 ); \
  308. } )
  309. #define pcibios_read_write pcibios32_read_write
  310. #endif /* KEEP_IT_REAL */
  311. static inline int pcibios_read_config_byte ( struct pci_device *pci,
  312. unsigned int where,
  313. uint8_t *value ) {
  314. return pcibios_read_write ( PCIBIOS_READ_CONFIG_BYTE,
  315. pci, where, *value );
  316. }
  317. static inline int pcibios_read_config_word ( struct pci_device *pci,
  318. unsigned int where,
  319. uint16_t *value ) {
  320. return pcibios_read_write ( PCIBIOS_READ_CONFIG_WORD,
  321. pci, where, *value );
  322. }
  323. static inline int pcibios_read_config_dword ( struct pci_device *pci,
  324. unsigned int where,
  325. uint32_t *value ) {
  326. return pcibios_read_write ( PCIBIOS_READ_CONFIG_DWORD,
  327. pci, where, *value );
  328. }
  329. static inline int pcibios_write_config_byte ( struct pci_device *pci,
  330. unsigned int where,
  331. uint8_t value ) {
  332. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_BYTE,
  333. pci, where, value );
  334. }
  335. static inline int pcibios_write_config_word ( struct pci_device *pci,
  336. unsigned int where,
  337. uint16_t value ) {
  338. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_WORD,
  339. pci, where, value );
  340. }
  341. static inline int pcibios_write_config_dword ( struct pci_device *pci,
  342. unsigned int where,
  343. uint32_t value ) {
  344. return pcibios_read_write ( PCIBIOS_WRITE_CONFIG_DWORD,
  345. pci, where, value );
  346. }
  347. /*
  348. * Functions for accessing PCI configuration space via the PCI BIOS if
  349. * present, otherwise directly via type 1 accesses.
  350. *
  351. */
  352. int pci_read_config_byte ( struct pci_device *pci, unsigned int where,
  353. uint8_t *value ) {
  354. return have_pcibios ?
  355. pcibios_read_config_byte ( pci, where, value ) :
  356. pcidirect_read_config_byte ( pci, where, value );
  357. }
  358. int pci_read_config_word ( struct pci_device *pci, unsigned int where,
  359. uint16_t *value ) {
  360. return have_pcibios ?
  361. pcibios_read_config_word ( pci, where, value ) :
  362. pcidirect_read_config_word ( pci, where, value );
  363. }
  364. int pci_read_config_dword ( struct pci_device *pci, unsigned int where,
  365. uint32_t *value ) {
  366. return have_pcibios ?
  367. pcibios_read_config_dword ( pci, where, value ) :
  368. pcidirect_read_config_dword ( pci, where, value );
  369. }
  370. int pci_write_config_byte ( struct pci_device *pci, unsigned int where,
  371. uint8_t value ) {
  372. return have_pcibios ?
  373. pcibios_write_config_byte ( pci, where, value ) :
  374. pcidirect_write_config_byte ( pci, where, value );
  375. }
  376. int pci_write_config_word ( struct pci_device *pci, unsigned int where,
  377. uint16_t value ) {
  378. return have_pcibios ?
  379. pcibios_write_config_word ( pci, where, value ) :
  380. pcidirect_write_config_word ( pci, where, value );
  381. }
  382. int pci_write_config_dword ( struct pci_device *pci, unsigned int where,
  383. uint32_t value ) {
  384. return have_pcibios ?
  385. pcibios_write_config_dword ( pci, where, value ) :
  386. pcidirect_write_config_dword ( pci, where, value );
  387. }
  388. unsigned long pci_bus_base ( struct pci_device *pci __unused ) {
  389. /* architecturally this must be 0 */
  390. return 0;
  391. }