1 /**************************************************************************
3 * isapnp.c -- Etherboot isapnp support for the 3Com 3c515
4 * Written 2002-2003 by Timothy Legge <tlegge@rogers.com>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston, MA
21 * Portions of this code:
22 * Copyright (C) 2001 P.J.H.Fox (fox@roestock.demon.co.uk)
27 * Version 0.1 April 26, 2002 TJL
28 * Version 0.2 01/08/2003 TJL Moved outside the 3c515.c driver file
29 * Version 0.3 Sept 23, 2003 timlegge Change delay to currticks
32 * Generalised into an ISAPnP bus that can be used by more than just
33 * the 3c515 by Michael Brown <mbrown@fensystems.co.uk>
35 ***************************************************************************/
41 * Etherboot orignally gained ISAPnP support in a very limited way for
42 * the 3c515 NIC. The current implementation is almost a complete
43 * rewrite based on the ISAPnP specification, with passing reference
44 * to the Linux ISAPnP code.
46 * There can be only one ISAPnP bus in a system. Once the read port
47 * is known and all cards have been allocated CSNs, there's nothing to
48 * be gained by re-scanning for cards.
50 * External code (e.g. the ISAPnP ROM prefix) may already know the
51 * read port address, in which case it can store it in
52 * #isapnp_read_port. Note that setting the read port address in this
53 * way will prevent further isolation from taking place; you should
54 * set the read port address only if you know that devices have
55 * already been allocated CSNs.
59 FILE_LICENCE ( GPL2_OR_LATER );
68 #include <ipxe/isapnp.h>
71 * ISAPnP Read Port address.
73 * ROM prefix may be able to set this address, which is why this is
76 uint16_t isapnp_read_port;
78 static void isapnpbus_remove ( struct root_device *rootdev );
81 * ISAPnP utility functions
85 #define ISAPNP_CARD_ID_FMT "ID %04x:%04x (\"%s\") serial %x"
86 #define ISAPNP_CARD_ID_DATA(identifier) \
87 (identifier)->vendor_id, (identifier)->prod_id, \
88 isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
90 #define ISAPNP_DEV_ID_FMT "ID %04x:%04x (\"%s\")"
91 #define ISAPNP_DEV_ID_DATA(isapnp) \
92 (isapnp)->vendor_id, (isapnp)->prod_id, \
93 isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )
95 static inline void isapnp_write_address ( unsigned int address ) {
96 outb ( address, ISAPNP_ADDRESS );
99 static inline void isapnp_write_data ( unsigned int data ) {
100 outb ( data, ISAPNP_WRITE_DATA );
103 static inline unsigned int isapnp_read_data ( void ) {
104 return inb ( isapnp_read_port );
107 static inline void isapnp_write_byte ( unsigned int address,
108 unsigned int value ) {
109 isapnp_write_address ( address );
110 isapnp_write_data ( value );
113 static inline unsigned int isapnp_read_byte ( unsigned int address ) {
114 isapnp_write_address ( address );
115 return isapnp_read_data ();
118 static inline unsigned int isapnp_read_word ( unsigned int address ) {
119 /* Yes, they're in big-endian order */
120 return ( ( isapnp_read_byte ( address ) << 8 )
121 | isapnp_read_byte ( address + 1 ) );
124 /** Inform cards of a new read port address */
125 static inline void isapnp_set_read_port ( void ) {
126 isapnp_write_byte ( ISAPNP_READPORT, ( isapnp_read_port >> 2 ) );
130 * Enter the Isolation state.
132 * Only cards currently in the Sleep state will respond to this
135 static inline void isapnp_serialisolation ( void ) {
136 isapnp_write_address ( ISAPNP_SERIALISOLATION );
140 * Enter the Wait for Key state.
142 * All cards will respond to this command, regardless of their current
145 static inline void isapnp_wait_for_key ( void ) {
146 isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
150 * Reset (i.e. remove) Card Select Number.
152 * Only cards currently in the Sleep state will respond to this
155 static inline void isapnp_reset_csn ( void ) {
156 isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
160 * Place a specified card into the Config state.
162 * @v csn Card Select Number
166 * Only cards currently in the Sleep, Isolation, or Config states will
167 * respond to this command. The card that has the specified CSN will
168 * enter the Config state, all other cards will enter the Sleep state.
170 static inline void isapnp_wake ( uint8_t csn ) {
171 isapnp_write_byte ( ISAPNP_WAKE, csn );
174 static inline unsigned int isapnp_read_resourcedata ( void ) {
175 return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
178 static inline unsigned int isapnp_read_status ( void ) {
179 return isapnp_read_byte ( ISAPNP_STATUS );
183 * Assign a Card Select Number to a card, and enter the Config state.
185 * @v csn Card Select Number
187 * Only cards in the Isolation state will respond to this command.
188 * The isolation protocol is designed so that only one card will
189 * remain in the Isolation state by the time the isolation protocol
192 static inline void isapnp_write_csn ( unsigned int csn ) {
193 isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
196 static inline void isapnp_logicaldevice ( unsigned int logdev ) {
197 isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
200 static inline void isapnp_activate ( unsigned int logdev ) {
201 isapnp_logicaldevice ( logdev );
202 isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
205 static inline void isapnp_deactivate ( unsigned int logdev ) {
206 isapnp_logicaldevice ( logdev );
207 isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
210 static inline unsigned int isapnp_read_iobase ( unsigned int index ) {
211 return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
214 static inline unsigned int isapnp_read_irqno ( unsigned int index ) {
215 return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
218 static void isapnp_delay ( void ) {
223 * Linear feedback shift register.
225 * @v lfsr Current value of the LFSR
226 * @v input_bit Current input bit to the LFSR
227 * @ret lfsr Next value of the LFSR
229 * This routine implements the linear feedback shift register as
230 * described in Appendix B of the PnP ISA spec. The hardware
231 * implementation uses eight D-type latches and two XOR gates. I
232 * think this is probably the smallest possible implementation in
233 * software. Six instructions when input_bit is a constant 0 (for
234 * isapnp_send_key). :)
236 static inline unsigned int isapnp_lfsr_next ( unsigned int lfsr,
237 unsigned int input_bit ) {
238 register uint8_t lfsr_next;
240 lfsr_next = lfsr >> 1;
241 lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
246 * Send the ISAPnP initiation key.
248 * Sending the key causes all ISAPnP cards that are currently in the
249 * Wait for Key state to transition into the Sleep state.
251 static void isapnp_send_key ( void ) {
256 isapnp_write_address ( 0x00 );
257 isapnp_write_address ( 0x00 );
259 lfsr = ISAPNP_LFSR_SEED;
260 for ( i = 0 ; i < 32 ; i++ ) {
261 isapnp_write_address ( lfsr );
262 lfsr = isapnp_lfsr_next ( lfsr, 0 );
267 * Compute ISAPnP identifier checksum
269 * @v identifier ISAPnP identifier
270 * @ret checksum Expected checksum value
272 static unsigned int isapnp_checksum ( struct isapnp_identifier *identifier ) {
277 lfsr = ISAPNP_LFSR_SEED;
278 for ( i = 0 ; i < 8 ; i++ ) {
279 byte = * ( ( ( uint8_t * ) identifier ) + i );
280 for ( j = 0 ; j < 8 ; j++ ) {
281 lfsr = isapnp_lfsr_next ( lfsr, byte );
289 * Read a byte of resource data from the current location
291 * @ret byte Byte of resource data
293 static inline unsigned int isapnp_peek_byte ( void ) {
296 /* Wait for data to be ready */
297 for ( i = 0 ; i < 20 ; i++ ) {
298 if ( isapnp_read_status() & 0x01 ) {
299 /* Byte ready - read it */
300 return isapnp_read_resourcedata();
304 /* Data never became ready - return 0xff */
309 * Read resource data.
311 * @v buf Buffer in which to store data, or NULL
312 * @v bytes Number of bytes to read
314 * Resource data is read from the current location. If #buf is NULL,
315 * the data is discarded.
317 static void isapnp_peek ( void *buf, size_t len ) {
321 for ( i = 0 ; i < len ; i++) {
322 byte = isapnp_peek_byte();
324 * ( ( uint8_t * ) buf + i ) = byte;
329 * Find a tag within the resource data.
331 * @v wanted_tag The tag that we're looking for
332 * @v buf Buffer in which to store the tag's contents
333 * @v len Length of buffer
334 * @ret rc Return status code
336 * Scan through the resource data until we find a particular tag, and
337 * read its contents into a buffer.
339 static int isapnp_find_tag ( unsigned int wanted_tag, void *buf, size_t len ) {
341 unsigned int tag_len;
343 DBG2 ( "ISAPnP read tag" );
345 tag = isapnp_peek_byte();
346 if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
347 tag_len = ISAPNP_SMALL_TAG_LEN ( tag );
348 tag = ISAPNP_SMALL_TAG_NAME ( tag );
350 tag_len = ( isapnp_peek_byte() +
351 ( isapnp_peek_byte() << 8 ) );
352 tag = ISAPNP_LARGE_TAG_NAME ( tag );
354 DBG2 ( " %02x (%02x)", tag, tag_len );
355 if ( tag == wanted_tag ) {
358 isapnp_peek ( buf, len );
362 isapnp_peek ( NULL, tag_len );
364 } while ( tag != ISAPNP_TAG_END );
370 * Find specified Logical Device ID tag
372 * @v logdev Logical device ID
373 * @v logdevid Logical device ID structure to fill in
374 * @ret rc Return status code
376 static int isapnp_find_logdevid ( unsigned int logdev,
377 struct isapnp_logdevid *logdevid ) {
381 for ( i = 0 ; i <= logdev ; i++ ) {
382 if ( ( rc = isapnp_find_tag ( ISAPNP_TAG_LOGDEVID, logdevid,
383 sizeof ( *logdevid ) ) ) != 0 )
390 * Try isolating ISAPnP cards at the current read port.
392 * @ret \>0 Number of ISAPnP cards found
393 * @ret 0 There are no ISAPnP cards in the system
394 * @ret \<0 A conflict was detected; try a new read port
397 * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
398 * gives the best overview of what happens here.
400 static int isapnp_try_isolate ( void ) {
401 struct isapnp_identifier identifier;
403 unsigned int seen_55aa, seen_life;
404 unsigned int csn = 0;
408 DBG ( "ISAPnP attempting isolation at read port %04x\n",
411 /* Place all cards into the Sleep state, whatever state
412 * they're currently in.
414 isapnp_wait_for_key();
417 /* Reset all assigned CSNs */
422 /* Place all cards into the Isolation state */
423 isapnp_wait_for_key ();
425 isapnp_wake ( 0x00 );
427 /* Set the read port */
428 isapnp_set_read_port();
433 /* All cards that do not have assigned CSNs are
434 * currently in the Isolation state, each time we go
438 /* Initiate serial isolation */
439 isapnp_serialisolation();
442 /* Read identifier serially via the ISAPnP read port. */
443 memset ( &identifier, 0, sizeof ( identifier ) );
444 seen_55aa = seen_life = 0;
445 for ( i = 0 ; i < 9 ; i++ ) {
447 for ( j = 0 ; j < 8 ; j++ ) {
448 data = isapnp_read_data();
450 data = ( ( data << 8 ) | isapnp_read_data() );
453 if ( data != 0xffff ) {
455 if ( data == 0x55aa ) {
461 *( ( ( uint8_t * ) &identifier ) + i ) = byte;
464 /* If we didn't see any 55aa patterns, stop here */
467 DBG ( "ISAPnP found no more cards\n" );
470 DBG ( "ISAPnP saw life but no cards, "
471 "trying new read port\n" );
474 DBG ( "ISAPnP saw no signs of life, "
475 "abandoning isolation\n" );
481 /* If the checksum was invalid stop here */
482 if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
483 DBG ( "ISAPnP found malformed card "
484 ISAPNP_CARD_ID_FMT "\n with checksum %02x "
485 "(should be %02x), trying new read port\n",
486 ISAPNP_CARD_ID_DATA ( &identifier ),
488 isapnp_checksum ( &identifier) );
493 /* Give the device a CSN */
495 DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
496 ", assigning CSN %02x\n",
497 ISAPNP_CARD_ID_DATA ( &identifier ), csn );
499 isapnp_write_csn ( csn );
502 /* Send this card back to Sleep and force all cards
503 * without a CSN into Isolation state
505 isapnp_wake ( 0x00 );
509 /* Place all cards in Wait for Key state */
510 isapnp_wait_for_key();
512 /* Return number of cards found */
514 DBG ( "ISAPnP found %d cards at read port %04x\n",
515 csn, isapnp_read_port );
521 * Find a valid read port and isolate all ISAPnP cards.
524 static void isapnp_isolate ( void ) {
525 for ( isapnp_read_port = ISAPNP_READ_PORT_START ;
526 isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
527 isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
528 /* Avoid problematic locations such as the NE2000
531 if ( ( isapnp_read_port >= 0x280 ) &&
532 ( isapnp_read_port <= 0x380 ) )
535 /* If we detect any ISAPnP cards at this location, stop */
536 if ( isapnp_try_isolate() >= 0 )
542 * Activate or deactivate an ISAPnP device.
544 * @v isapnp ISAPnP device
545 * @v activation True to enable, False to disable the device
549 * This routine simply activates the device in its current
550 * configuration, or deactivates the device. It does not attempt any
551 * kind of resource arbitration.
554 void isapnp_device_activation ( struct isapnp_device *isapnp,
556 /* Wake the card and select the logical device */
557 isapnp_wait_for_key ();
559 isapnp_wake ( isapnp->csn );
560 isapnp_logicaldevice ( isapnp->logdev );
562 /* Activate/deactivate the logical device */
563 isapnp_activate ( activation );
566 /* Return all cards to Wait for Key state */
567 isapnp_wait_for_key ();
569 DBG ( "ISAPnP %s device %02x:%02x\n",
570 ( activation ? "activated" : "deactivated" ),
571 isapnp->csn, isapnp->logdev );
575 * Probe an ISAPnP device
577 * @v isapnp ISAPnP device
578 * @ret rc Return status code
580 * Searches for a driver for the ISAPnP device. If a driver is found,
581 * its probe() routine is called.
583 static int isapnp_probe ( struct isapnp_device *isapnp ) {
584 struct isapnp_driver *driver;
585 struct isapnp_device_id *id;
589 DBG ( "Adding ISAPnP device %02x:%02x (%04x:%04x (\"%s\") "
590 "io %x irq %d)\n", isapnp->csn, isapnp->logdev,
591 isapnp->vendor_id, isapnp->prod_id,
592 isa_id_string ( isapnp->vendor_id, isapnp->prod_id ),
593 isapnp->ioaddr, isapnp->irqno );
595 for_each_table_entry ( driver, ISAPNP_DRIVERS ) {
596 for ( i = 0 ; i < driver->id_count ; i++ ) {
597 id = &driver->ids[i];
598 if ( id->vendor_id != isapnp->vendor_id )
600 if ( ISA_PROD_ID ( id->prod_id ) !=
601 ISA_PROD_ID ( isapnp->prod_id ) )
603 isapnp->driver = driver;
604 isapnp->dev.driver_name = id->name;
605 DBG ( "...using driver %s\n", isapnp->dev.driver_name );
606 if ( ( rc = driver->probe ( isapnp, id ) ) != 0 ) {
607 DBG ( "......probe failed\n" );
614 DBG ( "...no driver found\n" );
619 * Remove an ISAPnP device
621 * @v isapnp ISAPnP device
623 static void isapnp_remove ( struct isapnp_device *isapnp ) {
624 isapnp->driver->remove ( isapnp );
625 DBG ( "Removed ISAPnP device %02x:%02x\n",
626 isapnp->csn, isapnp->logdev );
630 * Probe ISAPnP root bus
632 * @v rootdev ISAPnP bus root device
634 * Scans the ISAPnP bus for devices and registers all devices it can
637 static int isapnpbus_probe ( struct root_device *rootdev ) {
638 struct isapnp_device *isapnp = NULL;
639 struct isapnp_identifier identifier;
640 struct isapnp_logdevid logdevid;
645 /* Perform isolation if it hasn't yet been done */
646 if ( ! isapnp_read_port )
649 for ( csn = 1 ; csn <= 0xff ; csn++ ) {
650 for ( logdev = 0 ; logdev <= 0xff ; logdev++ ) {
652 /* Allocate struct isapnp_device */
654 isapnp = malloc ( sizeof ( *isapnp ) );
659 memset ( isapnp, 0, sizeof ( *isapnp ) );
661 isapnp->logdev = logdev;
664 isapnp_wait_for_key();
668 /* Read the card identifier */
669 isapnp_peek ( &identifier, sizeof ( identifier ) );
671 /* No card with this CSN; stop here */
672 if ( identifier.vendor_id & 0x80 )
675 /* Find the Logical Device ID tag */
676 if ( ( rc = isapnp_find_logdevid ( logdev,
678 /* No more logical devices; go to next CSN */
682 /* Select the logical device */
683 isapnp_logicaldevice ( logdev );
685 /* Populate struct isapnp_device */
686 isapnp->vendor_id = logdevid.vendor_id;
687 isapnp->prod_id = logdevid.prod_id;
688 isapnp->ioaddr = isapnp_read_iobase ( 0 );
689 isapnp->irqno = isapnp_read_irqno ( 0 );
691 /* Return all cards to Wait for Key state */
692 isapnp_wait_for_key();
694 /* Add to device hierarchy */
695 snprintf ( isapnp->dev.name,
696 sizeof ( isapnp->dev.name ),
697 "ISAPnP%02x:%02x", csn, logdev );
698 isapnp->dev.desc.bus_type = BUS_TYPE_ISAPNP;
699 isapnp->dev.desc.vendor = isapnp->vendor_id;
700 isapnp->dev.desc.device = isapnp->prod_id;
701 isapnp->dev.desc.ioaddr = isapnp->ioaddr;
702 isapnp->dev.desc.irq = isapnp->irqno;
703 isapnp->dev.parent = &rootdev->dev;
704 list_add ( &isapnp->dev.siblings,
705 &rootdev->dev.children );
706 INIT_LIST_HEAD ( &isapnp->dev.children );
708 /* Look for a driver */
709 if ( isapnp_probe ( isapnp ) == 0 ) {
710 /* isapnpdev registered, we can drop our ref */
713 /* Not registered; re-use struct */
714 list_del ( &isapnp->dev.siblings );
725 isapnpbus_remove ( rootdev );
730 * Remove ISAPnP root bus
732 * @v rootdev ISAPnP bus root device
734 static void isapnpbus_remove ( struct root_device *rootdev ) {
735 struct isapnp_device *isapnp;
736 struct isapnp_device *tmp;
738 list_for_each_entry_safe ( isapnp, tmp, &rootdev->dev.children,
740 isapnp_remove ( isapnp );
741 list_del ( &isapnp->dev.siblings );
746 /** ISAPnP bus root device driver */
747 static struct root_driver isapnp_root_driver = {
748 .probe = isapnpbus_probe,
749 .remove = isapnpbus_remove,
752 /** ISAPnP bus root device */
753 struct root_device isapnp_root_device __root_device = {
754 .dev = { .name = "ISAPnP" },
755 .driver = &isapnp_root_driver,