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 14KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. static inline void isapnp_write_address ( uint8_t address ) {
  64. outb ( address, ISAPNP_ADDRESS );
  65. }
  66. static inline void isapnp_write_data ( uint8_t data ) {
  67. outb ( data, ISAPNP_WRITE_DATA );
  68. }
  69. static inline uint8_t isapnp_read_data ( void ) {
  70. return inb ( isapnp_read_port );
  71. }
  72. static inline void isapnp_write_byte ( uint8_t address, uint8_t value ) {
  73. isapnp_write_address ( address );
  74. isapnp_write_data ( value );
  75. }
  76. static inline uint8_t isapnp_read_byte ( uint8_t address ) {
  77. isapnp_write_address ( address );
  78. return isapnp_read_data ();
  79. }
  80. static inline uint16_t isapnp_read_word ( uint8_t address ) {
  81. /* Yes, they're in big-endian order */
  82. return ( ( isapnp_read_byte ( address ) << 8 )
  83. + isapnp_read_byte ( address + 1 ) );
  84. }
  85. static inline void isapnp_set_read_port ( void ) {
  86. isapnp_write_byte ( ISAPNP_READPORT, isapnp_read_port >> 2 );
  87. }
  88. static inline void isapnp_serialisolation ( void ) {
  89. isapnp_write_address ( ISAPNP_SERIALISOLATION );
  90. }
  91. static inline void isapnp_wait_for_key ( void ) {
  92. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
  93. }
  94. static inline void isapnp_reset_csn ( void ) {
  95. isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
  96. }
  97. static inline void isapnp_wake ( uint8_t csn ) {
  98. isapnp_write_byte ( ISAPNP_WAKE, csn );
  99. }
  100. static inline uint8_t isapnp_read_resourcedata ( void ) {
  101. return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
  102. }
  103. static inline uint8_t isapnp_read_status ( void ) {
  104. return isapnp_read_byte ( ISAPNP_STATUS );
  105. }
  106. static inline void isapnp_write_csn ( uint8_t csn ) {
  107. isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
  108. }
  109. static inline void isapnp_logicaldevice ( uint8_t logdev ) {
  110. isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
  111. }
  112. static inline void isapnp_activate ( uint8_t logdev ) {
  113. isapnp_logicaldevice ( logdev );
  114. isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
  115. }
  116. static inline void isapnp_deactivate ( uint8_t logdev ) {
  117. isapnp_logicaldevice ( logdev );
  118. isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
  119. }
  120. static inline uint16_t isapnp_read_iobase ( unsigned int index ) {
  121. return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
  122. }
  123. static inline uint8_t isapnp_read_irqno ( unsigned int index ) {
  124. return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
  125. }
  126. static void isapnp_delay ( void ) {
  127. udelay ( 1000 );
  128. }
  129. /*
  130. * The linear feedback shift register as described in Appendix B of
  131. * the PnP ISA spec. The hardware implementation uses eight D-type
  132. * latches and two XOR gates. I think this is probably the smallest
  133. * possible implementation in software. Six instructions when input_bit
  134. * is a constant 0 (for isapnp_send_key). :)
  135. *
  136. */
  137. static inline uint8_t isapnp_lfsr_next ( uint8_t lfsr, int input_bit ) {
  138. register uint8_t lfsr_next;
  139. lfsr_next = lfsr >> 1;
  140. lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
  141. return lfsr_next;
  142. }
  143. /*
  144. * Send the ISAPnP initiation key
  145. *
  146. */
  147. static void isapnp_send_key ( void ) {
  148. unsigned int i;
  149. uint8_t lfsr;
  150. isapnp_delay();
  151. isapnp_write_address ( 0x00 );
  152. isapnp_write_address ( 0x00 );
  153. lfsr = ISAPNP_LFSR_SEED;
  154. for ( i = 0 ; i < 32 ; i++ ) {
  155. isapnp_write_address ( lfsr );
  156. lfsr = isapnp_lfsr_next ( lfsr, 0 );
  157. }
  158. }
  159. /*
  160. * Compute ISAPnP identifier checksum
  161. *
  162. */
  163. static uint8_t isapnp_checksum ( struct isapnp_identifier *identifier ) {
  164. int i, j;
  165. uint8_t lfsr;
  166. uint8_t byte;
  167. lfsr = ISAPNP_LFSR_SEED;
  168. for ( i = 0 ; i < 8 ; i++ ) {
  169. byte = ( (char *) identifier )[i];
  170. for ( j = 0 ; j < 8 ; j++ ) {
  171. lfsr = isapnp_lfsr_next ( lfsr, byte );
  172. byte >>= 1;
  173. }
  174. }
  175. return lfsr;
  176. }
  177. /*
  178. * Read a byte of resource data from the current location
  179. *
  180. */
  181. static inline uint8_t isapnp_peek_byte ( void ) {
  182. int i;
  183. /* Wait for data to be ready */
  184. for ( i = 0 ; i < 20 ; i ++ ) {
  185. if ( isapnp_read_status() & 0x01 ) {
  186. /* Byte ready - read it */
  187. return isapnp_read_resourcedata();
  188. }
  189. isapnp_delay ();
  190. }
  191. /* Data never became ready - return 0xff */
  192. return 0xff;
  193. }
  194. /*
  195. * Read n bytes of resource data from the current location. If buf is
  196. * NULL, discard data.
  197. *
  198. */
  199. static void isapnp_peek ( uint8_t *buf, size_t bytes ) {
  200. unsigned int i;
  201. uint8_t byte;
  202. for ( i = 0 ; i < bytes ; i++) {
  203. byte = isapnp_peek_byte();
  204. if ( buf ) {
  205. buf[i] = byte;
  206. }
  207. }
  208. }
  209. /*
  210. * Scan through the resource data until we find a particular tag, and
  211. * read its contents into a buffer.
  212. *
  213. * It is the caller's responsibility to ensure that buf is large
  214. * enough to contain a tag of the requested size.
  215. *
  216. */
  217. static int isapnp_find_tag ( uint8_t wanted_tag, uint8_t *buf ) {
  218. uint8_t tag;
  219. uint16_t len;
  220. do {
  221. tag = isapnp_peek_byte();
  222. if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
  223. len = ISAPNP_SMALL_TAG_LEN ( tag );
  224. tag = ISAPNP_SMALL_TAG_NAME ( tag );
  225. } else {
  226. len = isapnp_peek_byte() + ( isapnp_peek_byte() << 8 );
  227. tag = ISAPNP_LARGE_TAG_NAME ( tag );
  228. }
  229. DBG ( "ISAPnP read tag %hhx len %hhx\n", tag, len );
  230. if ( tag == wanted_tag ) {
  231. isapnp_peek ( buf, len );
  232. return 1;
  233. } else {
  234. isapnp_peek ( NULL, len );
  235. }
  236. } while ( tag != ISAPNP_TAG_END );
  237. return 0;
  238. }
  239. /*
  240. * Try isolating ISAPnP cards at the current read port. Return the
  241. * number of ISAPnP cards found.
  242. *
  243. * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
  244. * gives the best overview of what happens here.
  245. *
  246. */
  247. static int isapnp_try_isolate ( void ) {
  248. struct isapnp_identifier identifier;
  249. int i, j, seen55aa;
  250. uint16_t data;
  251. uint8_t byte;
  252. DBG ( "ISAPnP attempting isolation at read port %hx\n",
  253. isapnp_read_port );
  254. /* Place all cards into the Sleep state, whatever state
  255. * they're currently in.
  256. */
  257. isapnp_wait_for_key ();
  258. isapnp_send_key ();
  259. /* Reset all assigned CSNs */
  260. isapnp_reset_csn ();
  261. isapnp_max_csn = 0;
  262. isapnp_delay();
  263. isapnp_delay();
  264. /* Place all cards into the Isolation state */
  265. isapnp_wait_for_key ();
  266. isapnp_send_key ();
  267. isapnp_wake ( 0x00 );
  268. /* Set the read port */
  269. isapnp_set_read_port ();
  270. isapnp_delay();
  271. while ( 1 ) {
  272. /* All cards that do not have assigned CSNs are
  273. * currently in the Isolation state, each time we go
  274. * through this loop.
  275. */
  276. /* Initiate serial isolation */
  277. isapnp_serialisolation ();
  278. isapnp_delay();
  279. /* Read identifier serially via the ISAPnP read port. */
  280. memset ( &identifier, 0, sizeof ( identifier ) );
  281. seen55aa = 0;
  282. for ( i = 0 ; i < 9 ; i++ ) {
  283. byte = 0;
  284. for ( j = 0 ; j < 8 ; j++ ) {
  285. data = isapnp_read_data ();
  286. isapnp_delay();
  287. data = ( data << 8 ) | isapnp_read_data ();
  288. isapnp_delay();
  289. if ( data == 0x55aa ) {
  290. byte |= 1;
  291. }
  292. byte <<= 1;
  293. }
  294. ( (char *) &identifier )[i] = byte;
  295. if ( byte ) {
  296. seen55aa = 1;
  297. }
  298. }
  299. /* If we didn't see a valid ISAPnP device, stop here */
  300. if ( ( ! seen55aa ) ||
  301. ( identifier.checksum != isapnp_checksum (&identifier) ) )
  302. break;
  303. /* Give the device a CSN */
  304. isapnp_max_csn++;
  305. DBG ( "ISAPnP isolation found card %hhx ID %hx:%hx (\"%s\") "
  306. "serial %x checksum %hhx, assigning CSN %hhx\n",
  307. identifier.vendor_id, identifier.prod_id,
  308. isa_id_string ( identifier.vendor_id,
  309. identifier.prod_id ),
  310. identifier.serial, identifier.checksum, isapnp_max_csn );
  311. isapnp_write_csn ( isapnp_max_csn );
  312. isapnp_delay();
  313. /* Send this card back to Sleep and force all cards
  314. * without a CSN into Isolation state
  315. */
  316. isapnp_wake ( 0x00 );
  317. isapnp_delay();
  318. }
  319. /* Place all cards in Wait for Key state */
  320. isapnp_wait_for_key ();
  321. /* Return number of cards found */
  322. DBG ( "ISAPnP found %d cards at read port %hx\n",
  323. isapnp_max_csn, isapnp_read_port );
  324. return isapnp_max_csn;
  325. }
  326. /*
  327. * Isolate all ISAPnP cards, locating a valid read port in the process.
  328. *
  329. */
  330. static void isapnp_isolate ( void ) {
  331. for ( isapnp_read_port = ISAPNP_READ_PORT_MIN ;
  332. isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
  333. isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
  334. /* Avoid problematic locations such as the NE2000
  335. * probe space
  336. */
  337. if ( ( isapnp_read_port >= 0x280 ) &&
  338. ( isapnp_read_port <= 0x380 ) )
  339. continue;
  340. /* If we detect any ISAPnP cards at this location, stop */
  341. if ( isapnp_try_isolate () )
  342. return;
  343. }
  344. }
  345. /*
  346. * Fill in parameters for an ISAPnP device based on CSN
  347. *
  348. * Return 1 if device present, 0 otherwise
  349. *
  350. */
  351. static int fill_isapnp_device ( struct isapnp_device *isapnp ) {
  352. unsigned int i;
  353. struct isapnp_logdevid logdevid;
  354. /* Wake the card */
  355. isapnp_wait_for_key ();
  356. isapnp_send_key ();
  357. isapnp_wake ( isapnp->csn );
  358. /* Skip past the card identifier */
  359. isapnp_peek ( NULL, sizeof ( struct isapnp_identifier ) );
  360. /* Find the Logical Device ID tag corresponding to this device */
  361. for ( i = 0 ; i <= isapnp->logdev ; i++ ) {
  362. if ( ! isapnp_find_tag ( ISAPNP_TAG_LOGDEVID,
  363. ( char * ) &logdevid ) ) {
  364. /* No tag for this device */
  365. return 0;
  366. }
  367. }
  368. /* Read information from identifier structure */
  369. isapnp->vendor_id = logdevid.vendor_id;
  370. isapnp->prod_id = logdevid.prod_id;
  371. /* Select the logical device */
  372. isapnp_logicaldevice ( isapnp->logdev );
  373. /* Read the current ioaddr and irqno */
  374. isapnp->ioaddr = isapnp_read_iobase ( 0 );
  375. isapnp->irqno = isapnp_read_irqno ( 0 );
  376. /* Return all cards to Wait for Key state */
  377. isapnp_wait_for_key ();
  378. DBG ( "ISAPnP found device %hhx.%hhx ID %hx:%hx (\"%s\"), "
  379. "base %hx irq %d\n",
  380. isapnp->csn, isapnp->logdev, isapnp->vendor_id, isapnp->prod_id,
  381. isa_id_string ( isapnp->vendor_id, isapnp->prod_id ),
  382. isapnp->ioaddr, isapnp->irqno );
  383. return 1;
  384. }
  385. /*
  386. * Find an ISAPnP device matching the specified driver
  387. *
  388. */
  389. int find_isapnp_device ( struct isapnp_device *isapnp,
  390. struct isapnp_driver *driver ) {
  391. unsigned int i;
  392. /* Initialise struct isapnp if it's the first time it's been used. */
  393. if ( isapnp->magic != isapnp_magic ) {
  394. memset ( isapnp, 0, sizeof ( *isapnp ) );
  395. isapnp->magic = isapnp_magic;
  396. isapnp->csn = 1;
  397. }
  398. /* Ensure that all ISAPnP cards have CSNs allocated to them,
  399. * if we haven't already done so.
  400. */
  401. if ( ! isapnp_read_port ) {
  402. isapnp_isolate();
  403. }
  404. /* Iterate through all possible ISAPNP CSNs, starting where we
  405. * left off.
  406. */
  407. for ( ; isapnp->csn <= isapnp_max_csn ; isapnp->csn++ ) {
  408. for ( ; isapnp->logdev <= 0xff ; isapnp->logdev++ ) {
  409. /* If we've already used this device, skip it */
  410. if ( isapnp->already_tried ) {
  411. isapnp->already_tried = 0;
  412. continue;
  413. }
  414. /* Fill in device parameters */
  415. if ( ! fill_isapnp_device ( isapnp ) ) {
  416. /* If fill_isapnp_device fails, assume
  417. * that we've reached the last logical
  418. * device on this card, and proceed to
  419. * the next card.
  420. */
  421. isapnp->logdev = 0;
  422. break;
  423. }
  424. /* Compare against driver's ID list */
  425. for ( i = 0 ; i < driver->id_count ; i++ ) {
  426. struct isapnp_id *id = &driver->ids[i];
  427. if ( ( isapnp->vendor_id == id->vendor_id ) &&
  428. ( ISA_PROD_ID ( isapnp->prod_id ) ==
  429. ISA_PROD_ID ( id->prod_id ) ) ) {
  430. DBG ( "Device %s (driver %s) "
  431. "matches ID %s\n",
  432. id->name, driver->name,
  433. isa_id_string( isapnp->vendor_id,
  434. isapnp->prod_id ) );
  435. isapnp->name = id->name;
  436. isapnp->already_tried = 1;
  437. return 1;
  438. }
  439. }
  440. }
  441. }
  442. /* No device found */
  443. isapnp->csn = 1;
  444. return 0;
  445. }
  446. /*
  447. * Find the next ISAPNP device that can be used to boot using the
  448. * specified driver.
  449. *
  450. */
  451. int find_isapnp_boot_device ( struct dev *dev, struct isapnp_driver *driver ) {
  452. struct isapnp_device *isapnp = ( struct isapnp_device * ) dev->bus;
  453. if ( ! find_isapnp_device ( isapnp, driver ) )
  454. return 0;
  455. dev->name = isapnp->name;
  456. dev->devid.bus_type = ISA_BUS_TYPE;
  457. dev->devid.vendor_id = isapnp->vendor_id;
  458. dev->devid.device_id = isapnp->prod_id;
  459. return 1;
  460. }
  461. /*
  462. * Activate or deactivate an ISAPnP device
  463. *
  464. * This routine simply activates the device in its current
  465. * configuration. It does not attempt any kind of resource
  466. * arbitration.
  467. *
  468. */
  469. void activate_isapnp_device ( struct isapnp_device *isapnp,
  470. int activate ) {
  471. /* Wake the card and select the logical device */
  472. isapnp_wait_for_key ();
  473. isapnp_send_key ();
  474. isapnp_wake ( isapnp->csn );
  475. isapnp_logicaldevice ( isapnp->logdev );
  476. /* Activate/deactivate the logical device */
  477. isapnp_activate ( activate );
  478. isapnp_delay();
  479. /* Return all cards to Wait for Key state */
  480. isapnp_wait_for_key ();
  481. DBG ( "ISAPnP activated device %hhx.%hhx\n",
  482. isapnp->csn, isapnp->logdev );
  483. }