Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

isapnp.c 13KB

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