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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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 ( union 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 = identifier->bytes[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. * Try isolating ISAPnP cards at the current read port. Return the
  211. * number of ISAPnP cards found.
  212. *
  213. * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
  214. * gives the best overview of what happens here.
  215. *
  216. */
  217. static int isapnp_try_isolate ( void ) {
  218. union isapnp_identifier identifier;
  219. int i, j, seen55aa;
  220. uint16_t data;
  221. uint8_t byte;
  222. DBG ( "ISAPnP attempting isolation at read port %hx\n",
  223. isapnp_read_port );
  224. /* Place all cards into the Sleep state, whatever state
  225. * they're currently in.
  226. */
  227. isapnp_wait_for_key ();
  228. isapnp_send_key ();
  229. /* Reset all assigned CSNs */
  230. isapnp_reset_csn ();
  231. isapnp_max_csn = 0;
  232. isapnp_delay();
  233. isapnp_delay();
  234. /* Place all cards into the Isolation state */
  235. isapnp_wait_for_key ();
  236. isapnp_send_key ();
  237. isapnp_wake ( 0x00 );
  238. /* Set the read port */
  239. isapnp_set_read_port ();
  240. isapnp_delay();
  241. while ( 1 ) {
  242. /* All cards that do not have assigned CSNs are
  243. * currently in the Isolation state, each time we go
  244. * through this loop.
  245. */
  246. /* Initiate serial isolation */
  247. isapnp_serialisolation ();
  248. isapnp_delay();
  249. /* Read identifier serially via the ISAPnP read port. */
  250. memset ( &identifier, 0, sizeof ( identifier ) );
  251. seen55aa = 0;
  252. for ( i = 0 ; i < 9 ; i++ ) {
  253. byte = 0;
  254. for ( j = 0 ; j < 8 ; j++ ) {
  255. data = isapnp_read_data ();
  256. isapnp_delay();
  257. data = ( data << 8 ) | isapnp_read_data ();
  258. isapnp_delay();
  259. if ( data == 0x55aa ) {
  260. byte |= 1;
  261. }
  262. byte <<= 1;
  263. }
  264. identifier.bytes[i] = byte;
  265. if ( byte ) {
  266. seen55aa = 1;
  267. }
  268. }
  269. /* If we didn't see a valid ISAPnP device, stop here */
  270. if ( ( ! seen55aa ) ||
  271. ( identifier.checksum != isapnp_checksum (&identifier) ) )
  272. break;
  273. /* Give the device a CSN */
  274. isapnp_max_csn++;
  275. DBG ( "ISAPnP isolation found device "
  276. "%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx:%hhx "
  277. "(checksum %hhx), assigning CSN %hhx\n",
  278. identifier.bytes[0], identifier.bytes[1],
  279. identifier.bytes[2], identifier.bytes[3],
  280. identifier.bytes[4], identifier.bytes[5],
  281. identifier.bytes[6], identifier.bytes[7],
  282. identifier.checksum, isapnp_max_csn );
  283. isapnp_write_csn ( isapnp_max_csn );
  284. isapnp_delay();
  285. /* Send this card back to Sleep and force all cards
  286. * without a CSN into Isolation state
  287. */
  288. isapnp_wake ( 0x00 );
  289. isapnp_delay();
  290. }
  291. /* Place all cards in Wait for Key state */
  292. isapnp_wait_for_key ();
  293. /* Return number of cards found */
  294. DBG ( "ISAPnP found %d devices at read port %hx\n", isapnp_read_port );
  295. return isapnp_max_csn;
  296. }
  297. /*
  298. * Isolate all ISAPnP cards, locating a valid read port in the process.
  299. *
  300. */
  301. static void isapnp_isolate ( void ) {
  302. for ( isapnp_read_port = ISAPNP_READ_PORT_MIN ;
  303. isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
  304. isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
  305. /* Avoid problematic locations such as the NE2000
  306. * probe space
  307. */
  308. if ( ( isapnp_read_port >= 0x280 ) &&
  309. ( isapnp_read_port <= 0x380 ) )
  310. continue;
  311. /* If we detect any ISAPnP cards at this location, stop */
  312. if ( isapnp_try_isolate () )
  313. return;
  314. }
  315. }
  316. /*
  317. * Fill in parameters for an ISAPnP device based on CSN
  318. *
  319. * Return 1 if device present, 0 otherwise
  320. *
  321. */
  322. static int fill_isapnp_device ( struct isapnp_device *isapnp ) {
  323. union isapnp_identifier identifier;
  324. /* Ensure that all ISAPnP cards have CSNs allocated to them,
  325. * if we haven't already done so.
  326. */
  327. if ( ! isapnp_read_port ) {
  328. isapnp_isolate();
  329. }
  330. /* Wake the specified CSN */
  331. isapnp_wait_for_key ();
  332. isapnp_send_key ();
  333. isapnp_wake ( isapnp->csn );
  334. /* Read the identifier. Do *not* verify the checksum, because
  335. * the PnP ISA spec explicitly states in section 4.5 that the
  336. * checksum is invalid except when read via the serial
  337. * isolation protocol. (This is presumably to allow for lazy
  338. * card designers who implement the checksum using the LFSR
  339. * only and can't be bothered to write the same value into the
  340. * EPROM).
  341. */
  342. isapnp_peek ( identifier.bytes, sizeof ( identifier ) );
  343. /* Read information from identifier structure */
  344. isapnp->vendor_id = identifier.vendor_id;
  345. isapnp->prod_id = identifier.prod_id;
  346. /* I/O addresses and IRQs are associated with logical devices,
  347. * not with cards. They will be assigned when
  348. * activate_isapnp_device() is called.
  349. */
  350. isapnp->ioaddr = 0;
  351. isapnp->irqno = 0;
  352. /* Return all cards to Wait for Key state */
  353. isapnp_wait_for_key ();
  354. DBG ( "ISAPnP found CSN %hhx ID %hx:%hx (\"%s\")\n",
  355. isapnp->csn, isapnp->vendor_id, isapnp->prod_id,
  356. isa_id_string ( isapnp->vendor_id, isapnp->prod_id ) );
  357. return 1;
  358. }
  359. /*
  360. * Find an ISAPnP device matching the specified driver
  361. *
  362. */
  363. int find_isapnp_device ( struct isapnp_device *isapnp,
  364. struct isapnp_driver *driver ) {
  365. unsigned int i;
  366. /* Initialise struct isapnp if it's the first time it's been used. */
  367. if ( isapnp->magic != isapnp_magic ) {
  368. memset ( isapnp, 0, sizeof ( *isapnp ) );
  369. isapnp->magic = isapnp_magic;
  370. isapnp->csn = 1;
  371. }
  372. /* Iterate through all possible ISAPNP CSNs, starting where we
  373. * left off.
  374. */
  375. for ( ; isapnp->csn <= isapnp_max_csn ; isapnp->csn++ ) {
  376. /* If we've already used this device, skip it */
  377. if ( isapnp->already_tried ) {
  378. isapnp->already_tried = 0;
  379. continue;
  380. }
  381. /* Fill in device parameters */
  382. if ( ! fill_isapnp_device ( isapnp ) ) {
  383. continue;
  384. }
  385. /* Compare against driver's ID list */
  386. for ( i = 0 ; i < driver->id_count ; i++ ) {
  387. struct isapnp_id *id = &driver->ids[i];
  388. if ( ( isapnp->vendor_id == id->vendor_id ) &&
  389. ( ISA_PROD_ID ( isapnp->prod_id ) ==
  390. ISA_PROD_ID ( id->prod_id ) ) ) {
  391. DBG ( "Device %s (driver %s) matches ID %s\n",
  392. id->name, driver->name,
  393. isa_id_string ( isapnp->vendor_id,
  394. isapnp->prod_id ) );
  395. isapnp->name = id->name;
  396. isapnp->already_tried = 1;
  397. return 1;
  398. }
  399. }
  400. }
  401. /* No device found */
  402. isapnp->csn = 1;
  403. return 0;
  404. }
  405. /*
  406. * Find the next ISAPNP device that can be used to boot using the
  407. * specified driver.
  408. *
  409. */
  410. int find_isapnp_boot_device ( struct dev *dev, struct isapnp_driver *driver ) {
  411. struct isapnp_device *isapnp = ( struct isapnp_device * ) dev->bus;
  412. if ( ! find_isapnp_device ( isapnp, driver ) )
  413. return 0;
  414. dev->name = isapnp->name;
  415. dev->devid.bus_type = ISA_BUS_TYPE;
  416. dev->devid.vendor_id = isapnp->vendor_id;
  417. dev->devid.device_id = isapnp->prod_id;
  418. return 1;
  419. }
  420. /*
  421. * Activate a logical function on an ISAPnP device, and fill in the
  422. * ioaddr and irqno fields in the isapnp struct.
  423. *
  424. * This routine simply activates the device in its current
  425. * configuration. It does not attempt any kind of resource
  426. * arbitration.
  427. *
  428. */
  429. void activate_isapnp_device ( struct isapnp_device *isapnp,
  430. uint8_t logdev ) {
  431. /* Wake the device */
  432. isapnp_wait_for_key ();
  433. isapnp_send_key ();
  434. isapnp_wake ( isapnp->csn );
  435. /* Select the specified logical device */
  436. isapnp_activate ( logdev );
  437. isapnp_delay();
  438. /* Read the current ioaddr and irqno */
  439. isapnp->ioaddr = isapnp_read_iobase ( 0 );
  440. isapnp->irqno = isapnp_read_irqno ( 0 );
  441. /* Return all cards to Wait for Key state */
  442. isapnp_wait_for_key ();
  443. DBG ( "ISAPnP activated logical device %hhx on CSN %hhx "
  444. "with ioaddr %hx and IRQ %d\n",
  445. logdev, isapnp->csn, isapnp->ioaddr, isapnp->irqno );
  446. }
  447. /*
  448. * Deactivate a logical function on an ISAPnP device
  449. *
  450. */
  451. void deactivate_isapnp_device ( struct isapnp_device *isapnp,
  452. uint8_t logdev ) {
  453. /* Wake the device */
  454. isapnp_wait_for_key ();
  455. isapnp_send_key ();
  456. isapnp_wake ( isapnp->csn );
  457. /* Select the specified logical device */
  458. isapnp_deactivate ( logdev );
  459. isapnp_delay();
  460. /* Return all cards to Wait for Key state */
  461. isapnp_wait_for_key ();
  462. DBG ( "ISAPnP deactivated logical device %hhx on CSN %hhx\n",
  463. logdev, isapnp->csn );
  464. }