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.

isapnp.c 16KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**************************************************************************
  2. *
  3. * isapnp.c -- Etherboot isapnp support for the 3Com 3c515
  4. * Written 2002-2003 by Timothy Legge <tlegge@rogers.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  19. *
  20. * Portions of this code:
  21. * Copyright (C) 2001 P.J.H.Fox (fox@roestock.demon.co.uk)
  22. *
  23. *
  24. * REVISION HISTORY:
  25. * ================
  26. * Version 0.1 April 26, 2002 TJL
  27. * Version 0.2 01/08/2003 TJL Moved outside the 3c515.c driver file
  28. * Version 0.3 Sept 23, 2003 timlegge Change delay to currticks
  29. *
  30. *
  31. * Generalised into an ISAPnP bus that can be used by more than just
  32. * the 3c515 by Michael Brown <mbrown@fensystems.co.uk>
  33. *
  34. ***************************************************************************/
  35. #include "etherboot.h"
  36. #include "timer.h"
  37. #include "io.h"
  38. #include "isapnp.h"
  39. /*
  40. * Ensure that there is sufficient space in the shared dev_bus
  41. * structure for a struct isapnp_device.
  42. *
  43. */
  44. DEV_BUS( struct isapnp_device, isapnp_dev );
  45. static char isapnp_magic[0]; /* guaranteed unique symbol */
  46. /*
  47. * We can have only one ISAPnP bus in a system. Once the read port is
  48. * known and all cards have been allocated CSNs, there's nothing to be
  49. * gained by re-scanning for cards.
  50. *
  51. * However, we shouldn't make scanning the ISAPnP bus an INIT_FN(),
  52. * because even ISAPnP probing can still screw up other devices on the
  53. * ISA bus. We therefore probe only when we are first asked to find
  54. * an ISAPnP device.
  55. *
  56. */
  57. static uint16_t isapnp_read_port;
  58. static uint16_t isapnp_max_csn;
  59. /*
  60. * ISAPnP utility functions
  61. *
  62. */
  63. #define ISAPNP_CARD_ID_FMT "ID %hx:%hx (\"%s\") serial %x"
  64. #define ISAPNP_CARD_ID_DATA(identifier) \
  65. (identifier)->vendor_id, (identifier)->prod_id, \
  66. isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
  67. (identifier)->serial
  68. #define ISAPNP_DEV_ID_FMT "ID %hx:%hx (\"%s\")"
  69. #define ISAPNP_DEV_ID_DATA(isapnp) \
  70. (isapnp)->vendor_id, (isapnp)->prod_id, \
  71. isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )
  72. static inline void isapnp_write_address ( uint8_t address ) {
  73. outb ( address, ISAPNP_ADDRESS );
  74. }
  75. static inline void isapnp_write_data ( uint8_t data ) {
  76. outb ( data, ISAPNP_WRITE_DATA );
  77. }
  78. static inline uint8_t isapnp_read_data ( void ) {
  79. return inb ( isapnp_read_port );
  80. }
  81. static inline void isapnp_write_byte ( uint8_t address, uint8_t value ) {
  82. isapnp_write_address ( address );
  83. isapnp_write_data ( value );
  84. }
  85. static inline uint8_t isapnp_read_byte ( uint8_t address ) {
  86. isapnp_write_address ( address );
  87. return isapnp_read_data ();
  88. }
  89. static inline uint16_t isapnp_read_word ( uint8_t address ) {
  90. /* Yes, they're in big-endian order */
  91. return ( ( isapnp_read_byte ( address ) << 8 )
  92. + isapnp_read_byte ( address + 1 ) );
  93. }
  94. static inline void isapnp_set_read_port ( void ) {
  95. isapnp_write_byte ( ISAPNP_READPORT, isapnp_read_port >> 2 );
  96. }
  97. static inline void isapnp_serialisolation ( void ) {
  98. isapnp_write_address ( ISAPNP_SERIALISOLATION );
  99. }
  100. static inline void isapnp_wait_for_key ( void ) {
  101. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
  102. }
  103. static inline void isapnp_reset_csn ( void ) {
  104. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
  105. }
  106. static inline void isapnp_wake ( uint8_t csn ) {
  107. isapnp_write_byte ( ISAPNP_WAKE, csn );
  108. }
  109. static inline uint8_t isapnp_read_resourcedata ( void ) {
  110. return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
  111. }
  112. static inline uint8_t isapnp_read_status ( void ) {
  113. return isapnp_read_byte ( ISAPNP_STATUS );
  114. }
  115. static inline void isapnp_write_csn ( uint8_t csn ) {
  116. isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
  117. }
  118. static inline void isapnp_logicaldevice ( uint8_t logdev ) {
  119. isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
  120. }
  121. static inline void isapnp_activate ( uint8_t logdev ) {
  122. isapnp_logicaldevice ( logdev );
  123. isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
  124. }
  125. static inline void isapnp_deactivate ( uint8_t logdev ) {
  126. isapnp_logicaldevice ( logdev );
  127. isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
  128. }
  129. static inline uint16_t isapnp_read_iobase ( unsigned int index ) {
  130. return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
  131. }
  132. static inline uint8_t isapnp_read_irqno ( unsigned int index ) {
  133. return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
  134. }
  135. static void isapnp_delay ( void ) {
  136. udelay ( 1000 );
  137. }
  138. /*
  139. * The linear feedback shift register as described in Appendix B of
  140. * the PnP ISA spec. The hardware implementation uses eight D-type
  141. * latches and two XOR gates. I think this is probably the smallest
  142. * possible implementation in software. Six instructions when input_bit
  143. * is a constant 0 (for isapnp_send_key). :)
  144. *
  145. */
  146. static inline uint8_t isapnp_lfsr_next ( uint8_t lfsr, int input_bit ) {
  147. register uint8_t lfsr_next;
  148. lfsr_next = lfsr >> 1;
  149. lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
  150. return lfsr_next;
  151. }
  152. /*
  153. * Send the ISAPnP initiation key
  154. *
  155. */
  156. static void isapnp_send_key ( void ) {
  157. unsigned int i;
  158. uint8_t lfsr;
  159. isapnp_delay();
  160. isapnp_write_address ( 0x00 );
  161. isapnp_write_address ( 0x00 );
  162. lfsr = ISAPNP_LFSR_SEED;
  163. for ( i = 0 ; i < 32 ; i++ ) {
  164. isapnp_write_address ( lfsr );
  165. lfsr = isapnp_lfsr_next ( lfsr, 0 );
  166. }
  167. }
  168. /*
  169. * Compute ISAPnP identifier checksum
  170. *
  171. */
  172. static uint8_t isapnp_checksum ( struct isapnp_identifier *identifier ) {
  173. int i, j;
  174. uint8_t lfsr;
  175. uint8_t byte;
  176. lfsr = ISAPNP_LFSR_SEED;
  177. for ( i = 0 ; i < 8 ; i++ ) {
  178. byte = ( (char *) identifier )[i];
  179. for ( j = 0 ; j < 8 ; j++ ) {
  180. lfsr = isapnp_lfsr_next ( lfsr, byte );
  181. byte >>= 1;
  182. }
  183. }
  184. return lfsr;
  185. }
  186. /*
  187. * Read a byte of resource data from the current location
  188. *
  189. */
  190. static inline uint8_t isapnp_peek_byte ( void ) {
  191. int i;
  192. /* Wait for data to be ready */
  193. for ( i = 0 ; i < 20 ; i ++ ) {
  194. if ( isapnp_read_status() & 0x01 ) {
  195. /* Byte ready - read it */
  196. return isapnp_read_resourcedata();
  197. }
  198. isapnp_delay ();
  199. }
  200. /* Data never became ready - return 0xff */
  201. return 0xff;
  202. }
  203. /*
  204. * Read n bytes of resource data from the current location. If buf is
  205. * NULL, discard data.
  206. *
  207. */
  208. static void isapnp_peek ( uint8_t *buf, size_t bytes ) {
  209. unsigned int i;
  210. uint8_t byte;
  211. for ( i = 0 ; i < bytes ; i++) {
  212. byte = isapnp_peek_byte();
  213. if ( buf ) {
  214. buf[i] = byte;
  215. }
  216. }
  217. }
  218. /*
  219. * Scan through the resource data until we find a particular tag, and
  220. * read its contents into a buffer.
  221. *
  222. * It is the caller's responsibility to ensure that buf is large
  223. * enough to contain a tag of the requested size.
  224. *
  225. */
  226. static int isapnp_find_tag ( uint8_t wanted_tag, uint8_t *buf ) {
  227. uint8_t tag;
  228. uint16_t len;
  229. DBG ( "ISAPnP read tag" );
  230. do {
  231. tag = isapnp_peek_byte();
  232. if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
  233. len = ISAPNP_SMALL_TAG_LEN ( tag );
  234. tag = ISAPNP_SMALL_TAG_NAME ( tag );
  235. } else {
  236. len = isapnp_peek_byte() + ( isapnp_peek_byte() << 8 );
  237. tag = ISAPNP_LARGE_TAG_NAME ( tag );
  238. }
  239. DBG ( " %hhx (%hhx)", tag, len );
  240. if ( tag == wanted_tag ) {
  241. isapnp_peek ( buf, len );
  242. DBG ( "\n" );
  243. return 1;
  244. } else {
  245. isapnp_peek ( NULL, len );
  246. }
  247. } while ( tag != ISAPNP_TAG_END );
  248. DBG ( "\n" );
  249. return 0;
  250. }
  251. /*
  252. * Try isolating ISAPnP cards at the current read port. Return the
  253. * number of ISAPnP cards found.
  254. *
  255. * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
  256. * gives the best overview of what happens here.
  257. *
  258. */
  259. static int isapnp_try_isolate ( void ) {
  260. struct isapnp_identifier identifier;
  261. int i, j, seen55aa;
  262. uint16_t data;
  263. uint8_t byte;
  264. DBG ( "ISAPnP attempting isolation at read port %hx\n",
  265. isapnp_read_port );
  266. /* Place all cards into the Sleep state, whatever state
  267. * they're currently in.
  268. */
  269. isapnp_wait_for_key ();
  270. isapnp_send_key ();
  271. /* Reset all assigned CSNs */
  272. isapnp_reset_csn ();
  273. isapnp_max_csn = 0;
  274. isapnp_delay();
  275. isapnp_delay();
  276. /* Place all cards into the Isolation state */
  277. isapnp_wait_for_key ();
  278. isapnp_send_key ();
  279. isapnp_wake ( 0x00 );
  280. /* Set the read port */
  281. isapnp_set_read_port ();
  282. isapnp_delay();
  283. while ( 1 ) {
  284. /* All cards that do not have assigned CSNs are
  285. * currently in the Isolation state, each time we go
  286. * through this loop.
  287. */
  288. /* Initiate serial isolation */
  289. isapnp_serialisolation ();
  290. isapnp_delay();
  291. /* Read identifier serially via the ISAPnP read port. */
  292. memset ( &identifier, 0, sizeof ( identifier ) );
  293. seen55aa = 0;
  294. for ( i = 0 ; i < 9 ; i++ ) {
  295. byte = 0;
  296. for ( j = 0 ; j < 8 ; j++ ) {
  297. data = isapnp_read_data ();
  298. isapnp_delay();
  299. data = ( data << 8 ) | isapnp_read_data ();
  300. isapnp_delay();
  301. byte >>= 1;
  302. if ( data == 0x55aa ) {
  303. byte |= 0x80;
  304. seen55aa = 1;
  305. }
  306. }
  307. ( (char *) &identifier )[i] = byte;
  308. }
  309. /* If we didn't see any 55aa patterns, stop here */
  310. if ( ! seen55aa ) {
  311. DBG ( "ISAPnP saw %s signs of life\n",
  312. isapnp_max_csn ? "no futher" : "no" );
  313. break;
  314. }
  315. /* If the checksum was invalid stop here */
  316. if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
  317. DBG ( "ISAPnP found malformed card "
  318. ISAPNP_CARD_ID_FMT "\n with checksum %hhx "
  319. "(should be %hhx), abandoning isolation\n",
  320. ISAPNP_CARD_ID_DATA ( &identifier ),
  321. identifier.checksum,
  322. isapnp_checksum ( &identifier) );
  323. /* break; */
  324. }
  325. /* Give the device a CSN */
  326. isapnp_max_csn++;
  327. DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
  328. ", assigning CSN %hhx\n",
  329. ISAPNP_CARD_ID_DATA ( &identifier ), isapnp_max_csn );
  330. isapnp_write_csn ( isapnp_max_csn );
  331. isapnp_delay();
  332. /* Send this card back to Sleep and force all cards
  333. * without a CSN into Isolation state
  334. */
  335. isapnp_wake ( 0x00 );
  336. isapnp_delay();
  337. }
  338. /* Place all cards in Wait for Key state */
  339. isapnp_wait_for_key ();
  340. /* Return number of cards found */
  341. DBG ( "ISAPnP found %d cards at read port %hx\n",
  342. isapnp_max_csn, isapnp_read_port );
  343. return isapnp_max_csn;
  344. }
  345. /*
  346. * Isolate all ISAPnP cards, locating a valid read port in the process.
  347. *
  348. */
  349. static void isapnp_isolate ( void ) {
  350. for ( isapnp_read_port = ISAPNP_READ_PORT_MIN ;
  351. isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
  352. isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
  353. /* Avoid problematic locations such as the NE2000
  354. * probe space
  355. */
  356. if ( ( isapnp_read_port >= 0x280 ) &&
  357. ( isapnp_read_port <= 0x380 ) )
  358. continue;
  359. /* If we detect any ISAPnP cards at this location, stop */
  360. if ( isapnp_try_isolate () )
  361. return;
  362. }
  363. }
  364. /*
  365. * Fill in parameters for an ISAPnP device based on CSN
  366. *
  367. * Return 1 if device present, 0 otherwise
  368. *
  369. */
  370. static int fill_isapnp_device ( struct isapnp_device *isapnp ) {
  371. unsigned int i;
  372. struct isapnp_identifier identifier;
  373. struct isapnp_logdevid logdevid;
  374. /* Wake the card */
  375. isapnp_wait_for_key ();
  376. isapnp_send_key ();
  377. isapnp_wake ( isapnp->csn );
  378. /* Read the card identifier */
  379. isapnp_peek ( ( char * ) &identifier, sizeof ( identifier ) );
  380. /* Find the Logical Device ID tag corresponding to this device */
  381. for ( i = 0 ; i <= isapnp->logdev ; i++ ) {
  382. if ( ! isapnp_find_tag ( ISAPNP_TAG_LOGDEVID,
  383. ( char * ) &logdevid ) ) {
  384. /* No tag for this device */
  385. DBG ( "ISAPnP found no device %hhx.%hhx on "
  386. "card " ISAPNP_CARD_ID_FMT "\n",
  387. isapnp->csn, isapnp->logdev,
  388. ISAPNP_CARD_ID_DATA ( &identifier ) );
  389. return 0;
  390. }
  391. }
  392. /* Read information from logdevid structure */
  393. isapnp->vendor_id = logdevid.vendor_id;
  394. isapnp->prod_id = logdevid.prod_id;
  395. /* Select the logical device */
  396. isapnp_logicaldevice ( isapnp->logdev );
  397. /* Read the current ioaddr and irqno */
  398. isapnp->ioaddr = isapnp_read_iobase ( 0 );
  399. isapnp->irqno = isapnp_read_irqno ( 0 );
  400. /* Return all cards to Wait for Key state */
  401. isapnp_wait_for_key ();
  402. DBG ( "ISAPnP found device %hhx.%hhx " ISAPNP_DEV_ID_FMT
  403. ", base %hx irq %d\n", isapnp->csn, isapnp->logdev,
  404. ISAPNP_DEV_ID_DATA ( isapnp ), isapnp->ioaddr, isapnp->irqno );
  405. DBG ( " on card " ISAPNP_CARD_ID_FMT "\n",
  406. ISAPNP_CARD_ID_DATA ( &identifier ) );
  407. return 1;
  408. }
  409. /*
  410. * Find an ISAPnP device matching the specified driver
  411. *
  412. */
  413. int find_isapnp_device ( struct isapnp_device *isapnp,
  414. struct isapnp_driver *driver ) {
  415. unsigned int i;
  416. /* Initialise struct isapnp if it's the first time it's been used. */
  417. if ( isapnp->magic != isapnp_magic ) {
  418. memset ( isapnp, 0, sizeof ( *isapnp ) );
  419. isapnp->magic = isapnp_magic;
  420. isapnp->csn = 1;
  421. }
  422. /* Ensure that all ISAPnP cards have CSNs allocated to them,
  423. * if we haven't already done so.
  424. */
  425. if ( ! isapnp_read_port ) {
  426. isapnp_isolate();
  427. }
  428. /* Iterate through all possible ISAPNP CSNs, starting where we
  429. * left off.
  430. */
  431. DBG ( "ISAPnP searching for device matching driver %s\n",
  432. driver->name );
  433. for ( ; isapnp->csn <= isapnp_max_csn ; isapnp->csn++ ) {
  434. for ( ; isapnp->logdev <= 0xff ; isapnp->logdev++ ) {
  435. /* If we've already used this device, skip it */
  436. if ( isapnp->already_tried ) {
  437. isapnp->already_tried = 0;
  438. continue;
  439. }
  440. /* Fill in device parameters */
  441. if ( ! fill_isapnp_device ( isapnp ) ) {
  442. /* If fill_isapnp_device fails, assume
  443. * that we've reached the last logical
  444. * device on this card, and proceed to
  445. * the next card.
  446. */
  447. isapnp->logdev = 0;
  448. break;
  449. }
  450. /* Compare against driver's ID list */
  451. for ( i = 0 ; i < driver->id_count ; i++ ) {
  452. struct isapnp_id *id = &driver->ids[i];
  453. if ( ( isapnp->vendor_id == id->vendor_id ) &&
  454. ( ISA_PROD_ID ( isapnp->prod_id ) ==
  455. ISA_PROD_ID ( id->prod_id ) ) ) {
  456. DBG ( "ISAPnP found ID %hx:%hx "
  457. "(\"%s\") (device %s) "
  458. "matching driver %s\n",
  459. isapnp->vendor_id,
  460. isapnp->prod_id,
  461. isa_id_string( isapnp->vendor_id,
  462. isapnp->prod_id ),
  463. id->name, driver->name );
  464. isapnp->name = id->name;
  465. isapnp->already_tried = 1;
  466. return 1;
  467. }
  468. }
  469. }
  470. }
  471. /* No device found */
  472. DBG ( "ISAPnP found no device matching driver %s\n", driver->name );
  473. isapnp->csn = 1;
  474. return 0;
  475. }
  476. /*
  477. * Find the next ISAPNP device that can be used to boot using the
  478. * specified driver.
  479. *
  480. */
  481. int find_isapnp_boot_device ( struct dev *dev, struct isapnp_driver *driver ) {
  482. struct isapnp_device *isapnp = ( struct isapnp_device * ) dev->bus;
  483. if ( ! find_isapnp_device ( isapnp, driver ) )
  484. return 0;
  485. dev->name = isapnp->name;
  486. dev->devid.bus_type = ISA_BUS_TYPE;
  487. dev->devid.vendor_id = isapnp->vendor_id;
  488. dev->devid.device_id = isapnp->prod_id;
  489. return 1;
  490. }
  491. /*
  492. * Activate or deactivate an ISAPnP device
  493. *
  494. * This routine simply activates the device in its current
  495. * configuration. It does not attempt any kind of resource
  496. * arbitration.
  497. *
  498. */
  499. void activate_isapnp_device ( struct isapnp_device *isapnp,
  500. int activate ) {
  501. /* Wake the card and select the logical device */
  502. isapnp_wait_for_key ();
  503. isapnp_send_key ();
  504. isapnp_wake ( isapnp->csn );
  505. isapnp_logicaldevice ( isapnp->logdev );
  506. /* Activate/deactivate the logical device */
  507. isapnp_activate ( activate );
  508. isapnp_delay();
  509. /* Return all cards to Wait for Key state */
  510. isapnp_wait_for_key ();
  511. DBG ( "ISAPnP activated device %hhx.%hhx\n",
  512. isapnp->csn, isapnp->logdev );
  513. }