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

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