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., 675 Mass Ave, Cambridge, MA 02139, USA.
20 * Portions of this code:
21 * Copyright (C) 2001 P.J.H.Fox (fox@roestock.demon.co.uk)
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
31 * Generalised into an ISAPnP bus that can be used by more than just
32 * the 3c515 by Michael Brown <mbrown@fensystems.co.uk>
34 ***************************************************************************/
40 * Etherboot orignally gained ISAPnP support in a very limited way for
41 * the 3c515 NIC. The current implementation is almost a complete
42 * rewrite based on the ISAPnP specification, with passing reference
43 * to the Linux ISAPnP code.
45 * There can be only one ISAPnP bus in a system. Once the read port
46 * is known and all cards have been allocated CSNs, there's nothing to
47 * be gained by re-scanning for cards.
49 * However, we shouldn't make scanning the ISAPnP bus an INIT_FN(),
50 * because even ISAPnP probing can still screw up other devices on the
51 * ISA bus. We therefore probe only when we are first asked to find
54 * External code (e.g. the ISAPnP ROM prefix) may already know the
55 * read port address, in which case it can store it in
56 * #isapnp_read_port. Note that setting the read port address in this
57 * way will prevent further isolation from taking place; you should
58 * set the read port address only if you know that devices have
59 * already been allocated CSNs.
71 * ISAPnP Read Port address.
74 uint16_t isapnp_read_port;
77 * Highest assigned CSN.
79 * Note that @b we do not necessarily assign CSNs; it could be done by
80 * the PnP BIOS instead. We therefore set this only when we first try
81 * to Wake[CSN] a device and find that there's nothing there. Page 16
82 * (PDF page 22) of the ISAPnP spec states that "Valid Card Select
83 * Numbers for identified ISA cards range from 1 to 255 and must be
84 * assigned sequentially starting from 1", so we are (theoretically,
85 * at least) safe to assume that there are no ISAPnP cards at CSNs
86 * higher than the first unused CSN.
89 static uint8_t isapnp_max_csn = 0xff;
92 * ISAPnP utility functions
96 #define ISAPNP_CARD_ID_FMT "ID %hx:%hx (\"%s\") serial %x"
97 #define ISAPNP_CARD_ID_DATA(identifier) \
98 (identifier)->vendor_id, (identifier)->prod_id, \
99 isa_id_string ( (identifier)->vendor_id, (identifier)->prod_id ), \
101 #define ISAPNP_DEV_ID_FMT "ID %hx:%hx (\"%s\")"
102 #define ISAPNP_DEV_ID_DATA(isapnp) \
103 (isapnp)->vendor_id, (isapnp)->prod_id, \
104 isa_id_string ( (isapnp)->vendor_id, (isapnp)->prod_id )
106 static inline void isapnp_write_address ( uint8_t address ) {
107 outb ( address, ISAPNP_ADDRESS );
110 static inline void isapnp_write_data ( uint8_t data ) {
111 outb ( data, ISAPNP_WRITE_DATA );
114 static inline uint8_t isapnp_read_data ( void ) {
115 return inb ( isapnp_read_port );
118 static inline void isapnp_write_byte ( uint8_t address, uint8_t value ) {
119 isapnp_write_address ( address );
120 isapnp_write_data ( value );
123 static inline uint8_t isapnp_read_byte ( uint8_t address ) {
124 isapnp_write_address ( address );
125 return isapnp_read_data ();
128 static inline uint16_t isapnp_read_word ( uint8_t address ) {
129 /* Yes, they're in big-endian order */
130 return ( ( isapnp_read_byte ( address ) << 8 )
131 + isapnp_read_byte ( address + 1 ) );
134 /** Inform cards of a new read port address */
135 static inline void isapnp_set_read_port ( void ) {
136 isapnp_write_byte ( ISAPNP_READPORT, isapnp_read_port >> 2 );
140 * Enter the Isolation state.
142 * Only cards currently in the Sleep state will respond to this
146 static inline void isapnp_serialisolation ( void ) {
147 isapnp_write_address ( ISAPNP_SERIALISOLATION );
151 * Enter the Wait for Key state.
153 * All cards will respond to this command, regardless of their current
157 static inline void isapnp_wait_for_key ( void ) {
158 isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_WAIT_FOR_KEY );
162 * Reset (i.e. remove) Card Select Number.
164 * Only cards currently in the Sleep state will respond to this
168 static inline void isapnp_reset_csn ( void ) {
169 isapnp_write_byte ( ISAPNP_CONFIGCONTROL, ISAPNP_CONFIG_RESET_CSN );
173 * Place a specified card into the Config state.
175 * @v csn Card Select Number
179 * Only cards currently in the Sleep, Isolation, or Config states will
180 * respond to this command. The card that has the specified CSN will
181 * enter the Config state, all other cards will enter the Sleep state.
184 static inline void isapnp_wake ( uint8_t csn ) {
185 isapnp_write_byte ( ISAPNP_WAKE, csn );
188 static inline uint8_t isapnp_read_resourcedata ( void ) {
189 return isapnp_read_byte ( ISAPNP_RESOURCEDATA );
192 static inline uint8_t isapnp_read_status ( void ) {
193 return isapnp_read_byte ( ISAPNP_STATUS );
197 * Assign a Card Select Number to a card, and enter the Config state.
199 * @v csn Card Select Number
203 * Only cards in the Isolation state will respond to this command.
204 * The isolation protocol is designed so that only one card will
205 * remain in the Isolation state by the time the isolation protocol
209 static inline void isapnp_write_csn ( uint8_t csn ) {
210 isapnp_write_byte ( ISAPNP_CARDSELECTNUMBER, csn );
213 static inline void isapnp_logicaldevice ( uint8_t logdev ) {
214 isapnp_write_byte ( ISAPNP_LOGICALDEVICENUMBER, logdev );
217 static inline void isapnp_activate ( uint8_t logdev ) {
218 isapnp_logicaldevice ( logdev );
219 isapnp_write_byte ( ISAPNP_ACTIVATE, 1 );
222 static inline void isapnp_deactivate ( uint8_t logdev ) {
223 isapnp_logicaldevice ( logdev );
224 isapnp_write_byte ( ISAPNP_ACTIVATE, 0 );
227 static inline uint16_t isapnp_read_iobase ( unsigned int index ) {
228 return isapnp_read_word ( ISAPNP_IOBASE ( index ) );
231 static inline uint8_t isapnp_read_irqno ( unsigned int index ) {
232 return isapnp_read_byte ( ISAPNP_IRQNO ( index ) );
235 static void isapnp_delay ( void ) {
240 * Linear feedback shift register.
242 * @v lfsr Current value of the LFSR
243 * @v input_bit Current input bit to the LFSR
244 * @ret lfsr Next value of the LFSR
247 * This routine implements the linear feedback shift register as
248 * described in Appendix B of the PnP ISA spec. The hardware
249 * implementation uses eight D-type latches and two XOR gates. I
250 * think this is probably the smallest possible implementation in
251 * software. Six instructions when input_bit is a constant 0 (for
252 * isapnp_send_key). :)
255 static inline uint8_t isapnp_lfsr_next ( uint8_t lfsr, int input_bit ) {
256 register uint8_t lfsr_next;
258 lfsr_next = lfsr >> 1;
259 lfsr_next |= ( ( ( lfsr ^ lfsr_next ) ^ input_bit ) ) << 7;
264 * Send the ISAPnP initiation key.
266 * Sending the key causes all ISAPnP cards that are currently in the
267 * Wait for Key state to transition into the Sleep state.
270 static void isapnp_send_key ( void ) {
275 isapnp_write_address ( 0x00 );
276 isapnp_write_address ( 0x00 );
278 lfsr = ISAPNP_LFSR_SEED;
279 for ( i = 0 ; i < 32 ; i++ ) {
280 isapnp_write_address ( lfsr );
281 lfsr = isapnp_lfsr_next ( lfsr, 0 );
286 * Compute ISAPnP identifier checksum
288 * @v identifier ISAPnP identifier
289 * @ret checksum Expected checksum value
293 static uint8_t isapnp_checksum ( struct isapnp_identifier *identifier ) {
298 lfsr = ISAPNP_LFSR_SEED;
299 for ( i = 0 ; i < 8 ; i++ ) {
300 byte = ( (char *) identifier )[i];
301 for ( j = 0 ; j < 8 ; j++ ) {
302 lfsr = isapnp_lfsr_next ( lfsr, byte );
310 * Read a byte of resource data from the current location
313 static inline uint8_t isapnp_peek_byte ( void ) {
316 /* Wait for data to be ready */
317 for ( i = 0 ; i < 20 ; i ++ ) {
318 if ( isapnp_read_status() & 0x01 ) {
319 /* Byte ready - read it */
320 return isapnp_read_resourcedata();
324 /* Data never became ready - return 0xff */
329 * Read resource data.
331 * @v buf Buffer in which to store data, or NULL
332 * @v bytes Number of bytes to read
336 * Resource data is read from the current location. If #buf is NULL,
337 * the data is discarded.
340 static void isapnp_peek ( uint8_t *buf, size_t bytes ) {
344 for ( i = 0 ; i < bytes ; i++) {
345 byte = isapnp_peek_byte();
353 * Find a tag within the resource data.
355 * @v wanted_tag The tag that we're looking for
356 * @v buf Buffer in which to store the tag's contents
357 * @ret True Tag was found
358 * @ret False Tag was not found
361 * Scan through the resource data until we find a particular tag, and
362 * read its contents into a buffer. It is the caller's responsibility
363 * to ensure that #buf is large enough to contain a tag of the
367 static int isapnp_find_tag ( uint8_t wanted_tag, uint8_t *buf ) {
371 DBG2 ( "ISAPnP read tag" );
373 tag = isapnp_peek_byte();
374 if ( ISAPNP_IS_SMALL_TAG ( tag ) ) {
375 len = ISAPNP_SMALL_TAG_LEN ( tag );
376 tag = ISAPNP_SMALL_TAG_NAME ( tag );
378 len = isapnp_peek_byte() + ( isapnp_peek_byte() << 8 );
379 tag = ISAPNP_LARGE_TAG_NAME ( tag );
381 DBG2 ( " %hhx (%hhx)", tag, len );
382 if ( tag == wanted_tag ) {
383 isapnp_peek ( buf, len );
387 isapnp_peek ( NULL, len );
389 } while ( tag != ISAPNP_TAG_END );
395 * Try isolating ISAPnP cards at the current read port.
397 * @ret \>0 Number of ISAPnP cards found
398 * @ret 0 There are no ISAPnP cards in the system
399 * @ret \<0 A conflict was detected; try a new read port
402 * The state diagram on page 18 (PDF page 24) of the PnP ISA spec
403 * gives the best overview of what happens here.
406 static int isapnp_try_isolate ( void ) {
407 struct isapnp_identifier identifier;
409 unsigned int seen_55aa, seen_life;
410 unsigned int csn = 0;
414 DBG ( "ISAPnP attempting isolation at read port %hx\n",
417 /* Place all cards into the Sleep state, whatever state
418 * they're currently in.
420 isapnp_wait_for_key ();
423 /* Reset all assigned CSNs */
428 /* Place all cards into the Isolation state */
429 isapnp_wait_for_key ();
431 isapnp_wake ( 0x00 );
433 /* Set the read port */
434 isapnp_set_read_port ();
439 /* All cards that do not have assigned CSNs are
440 * currently in the Isolation state, each time we go
444 /* Initiate serial isolation */
445 isapnp_serialisolation ();
448 /* Read identifier serially via the ISAPnP read port. */
449 memset ( &identifier, 0, sizeof ( identifier ) );
450 seen_55aa = seen_life = 0;
451 for ( i = 0 ; i < 9 ; i++ ) {
453 for ( j = 0 ; j < 8 ; j++ ) {
454 data = isapnp_read_data ();
456 data = ( data << 8 ) | isapnp_read_data ();
459 if ( data != 0xffff ) {
461 if ( data == 0x55aa ) {
467 ( (char *) &identifier )[i] = byte;
470 /* If we didn't see any 55aa patterns, stop here */
473 DBG ( "ISAPnP found no more cards\n" );
476 DBG ( "ISAPnP saw life but no cards, "
477 "trying new read port\n" );
480 DBG ( "ISAPnP saw no signs of life, "
481 "abandoning isolation\n" );
487 /* If the checksum was invalid stop here */
488 if ( identifier.checksum != isapnp_checksum ( &identifier) ) {
489 DBG ( "ISAPnP found malformed card "
490 ISAPNP_CARD_ID_FMT "\n with checksum %hhx "
491 "(should be %hhx), trying new read port\n",
492 ISAPNP_CARD_ID_DATA ( &identifier ),
494 isapnp_checksum ( &identifier) );
499 /* Give the device a CSN */
501 DBG ( "ISAPnP found card " ISAPNP_CARD_ID_FMT
502 ", assigning CSN %hhx\n",
503 ISAPNP_CARD_ID_DATA ( &identifier ), csn );
505 isapnp_write_csn ( csn );
508 /* Send this card back to Sleep and force all cards
509 * without a CSN into Isolation state
511 isapnp_wake ( 0x00 );
515 /* Place all cards in Wait for Key state */
516 isapnp_wait_for_key ();
518 /* Return number of cards found */
520 DBG ( "ISAPnP found %d cards at read port %hx\n",
521 csn, isapnp_read_port );
527 * Find a valid read port and isolate all ISAPnP cards.
530 static void isapnp_isolate ( void ) {
531 for ( isapnp_read_port = ISAPNP_READ_PORT_MIN ;
532 isapnp_read_port <= ISAPNP_READ_PORT_MAX ;
533 isapnp_read_port += ISAPNP_READ_PORT_STEP ) {
534 /* Avoid problematic locations such as the NE2000
537 if ( ( isapnp_read_port >= 0x280 ) &&
538 ( isapnp_read_port <= 0x380 ) )
541 /* If we detect any ISAPnP cards at this location, stop */
542 if ( isapnp_try_isolate () >= 0 )
548 * Increment a #bus_loc structure to the next possible ISAPnP
551 * @v bus_loc Bus location
552 * @ret True #bus_loc contains a valid ISAPnP location
553 * @ret False There are no more valid ISAPnP locations
556 * If there are no more valid locations, the #bus_loc structure will
560 static int isapnp_next_location ( struct bus_loc *bus_loc ) {
561 struct isapnp_loc *isapnp_loc = ( struct isapnp_loc * ) bus_loc;
564 * Ensure that there is sufficient space in the shared bus
565 * structures for a struct isapnp_loc and a struct isapnp_dev,
566 * as mandated by bus.h.
569 BUS_LOC_CHECK ( struct isapnp_loc );
570 BUS_DEV_CHECK ( struct isapnp_device );
572 return ( ++isapnp_loc->logdev ? 1 : ++isapnp_loc->csn );
576 * Fill in parameters for an ISAPnP device based on CSN.
578 * @v bus_dev Bus device to be filled in
579 * @v bus_loc Bus location as filled in by isapnp_next_location()
580 * @ret True A device is present at this location
581 * @ret False No device is present at this location
585 static int isapnp_fill_device ( struct bus_dev *bus_dev,
586 struct bus_loc *bus_loc ) {
587 struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
588 struct isapnp_loc *isapnp_loc = ( struct isapnp_loc * ) bus_loc;
590 struct isapnp_identifier identifier;
591 struct isapnp_logdevid logdevid;
594 uint8_t first_nonexistent_logdev;
597 /* Copy CSN and logdev to isapnp_device, set default values */
598 isapnp->csn = isapnp_loc->csn;
599 isapnp->logdev = isapnp_loc->logdev;
602 /* CSN 0 is never valid, but may be passed in */
606 /* Check to see if we are already past the maximum CSN */
607 if ( isapnp->csn > isapnp_max_csn )
610 /* Check cache to see if we are already past the highest
611 * logical device of this CSN
613 if ( ( isapnp->csn == cache.csn ) &&
614 ( isapnp->logdev >= cache.first_nonexistent_logdev ) )
617 /* Perform isolation if it hasn't yet been done */
618 if ( ! isapnp_read_port )
622 isapnp_wait_for_key ();
624 isapnp_wake ( isapnp->csn );
626 /* Read the card identifier */
627 isapnp_peek ( ( char * ) &identifier, sizeof ( identifier ) );
629 /* Need to return 0 if no device exists at this CSN */
630 if ( identifier.vendor_id & 0x80 ) {
631 isapnp_max_csn = isapnp->csn - 1;
635 /* Find the Logical Device ID tag corresponding to this device */
636 for ( i = 0 ; i <= isapnp->logdev ; i++ ) {
637 if ( ! isapnp_find_tag ( ISAPNP_TAG_LOGDEVID,
638 ( char * ) &logdevid ) ) {
639 /* No tag for this device */
640 if ( isapnp->logdev == 0 ) {
641 DBG ( "ISAPnP found no device %hhx.0 on card "
642 ISAPNP_CARD_ID_FMT "\n", isapnp->csn,
643 ISAPNP_CARD_ID_DATA ( &identifier ) );
645 cache.csn = isapnp->csn;
646 cache.first_nonexistent_logdev = isapnp->logdev;
651 /* Read information from logdevid structure */
652 isapnp->vendor_id = logdevid.vendor_id;
653 isapnp->prod_id = logdevid.prod_id;
655 /* Select the logical device */
656 isapnp_logicaldevice ( isapnp->logdev );
658 /* Read the current ioaddr and irqno */
659 isapnp->ioaddr = isapnp_read_iobase ( 0 );
660 isapnp->irqno = isapnp_read_irqno ( 0 );
662 /* Return all cards to Wait for Key state */
663 isapnp_wait_for_key ();
665 DBG ( "ISAPnP found device %hhx.%hhx " ISAPNP_DEV_ID_FMT
666 ", base %hx irq %d\n", isapnp->csn, isapnp->logdev,
667 ISAPNP_DEV_ID_DATA ( isapnp ), isapnp->ioaddr, isapnp->irqno );
668 DBG ( " on card " ISAPNP_CARD_ID_FMT "\n",
669 ISAPNP_CARD_ID_DATA ( &identifier ) );
675 * Test whether or not a driver is capable of driving the device.
677 * @v bus_dev Bus device as filled in by isapnp_fill_device()
678 * @v device_driver Device driver
679 * @ret True Driver is capable of driving this device
680 * @ret False Driver is not capable of driving this device
684 static int isapnp_check_driver ( struct bus_dev *bus_dev,
685 struct device_driver *device_driver ) {
686 struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
687 struct isapnp_driver *driver
688 = ( struct isapnp_driver * ) device_driver->bus_driver_info;
691 /* Compare against driver's ID list */
692 for ( i = 0 ; i < driver->id_count ; i++ ) {
693 struct isapnp_id *id = &driver->ids[i];
695 if ( ( isapnp->vendor_id == id->vendor_id ) &&
696 ( ISA_PROD_ID ( isapnp->prod_id ) ==
697 ISA_PROD_ID ( id->prod_id ) ) ) {
698 DBG ( "ISAPnP found ID %hx:%hx (\"%s\") (device %s) "
699 "matching driver %s\n",
700 isapnp->vendor_id, isapnp->prod_id,
701 isa_id_string( isapnp->vendor_id,
703 id->name, device_driver->name );
704 isapnp->name = id->name;
713 * Describe an ISAPnP device.
715 * @v bus_dev Bus device as filled in by isapnp_fill_device()
716 * @ret string Printable string describing the device
719 * The string returned by isapnp_describe_device() is valid only until
720 * the next call to isapnp_describe_device().
723 static char * isapnp_describe_device ( struct bus_dev *bus_dev ) {
724 struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
725 static char isapnp_description[] = "ISAPnP 00:00";
727 sprintf ( isapnp_description + 7, "%hhx:%hhx",
728 isapnp->csn, isapnp->logdev );
729 return isapnp_description;
733 * Name an ISAPnP device.
735 * @v bus_dev Bus device as filled in by isapnp_fill_device()
736 * @ret string Printable string naming the device
739 * The string returned by isapnp_name_device() is valid only until the
740 * next call to isapnp_name_device().
743 static const char * isapnp_name_device ( struct bus_dev *bus_dev ) {
744 struct isapnp_device *isapnp = ( struct isapnp_device * ) bus_dev;
750 * ISAPnP bus operations table
753 struct bus_driver isapnp_driver __bus_driver = {
755 .next_location = isapnp_next_location,
756 .fill_device = isapnp_fill_device,
757 .check_driver = isapnp_check_driver,
758 .describe_device = isapnp_describe_device,
759 .name_device = isapnp_name_device,
763 * Activate or deactivate an ISAPnP device.
765 * @v isapnp ISAPnP device
766 * @v activation True to enable, False to disable the device
770 * This routine simply activates the device in its current
771 * configuration, or deactivates the device. It does not attempt any
772 * kind of resource arbitration.
775 void isapnp_device_activation ( struct isapnp_device *isapnp,
777 /* Wake the card and select the logical device */
778 isapnp_wait_for_key ();
780 isapnp_wake ( isapnp->csn );
781 isapnp_logicaldevice ( isapnp->logdev );
783 /* Activate/deactivate the logical device */
784 isapnp_activate ( activation );
787 /* Return all cards to Wait for Key state */
788 isapnp_wait_for_key ();
790 DBG ( "ISAPnP %s device %hhx.%hhx\n",
791 ( activation ? "activated" : "deactivated" ),
792 isapnp->csn, isapnp->logdev );
796 * Fill in a nic structure.
798 * @v nic NIC structure to be filled in
799 * @v isapnp ISAPnP device
803 * This fills in generic NIC parameters (e.g. I/O address and IRQ
804 * number) that can be determined directly from the ISAPnP device,
805 * without any driver-specific knowledge.
808 void isapnp_fill_nic ( struct nic *nic, struct isapnp_device *isapnp ) {
810 /* Fill in ioaddr and irqno */
811 nic->ioaddr = isapnp->ioaddr;
812 nic->irqno = isapnp->irqno;
814 /* Fill in DHCP device ID structure */
815 nic->dhcp_dev_id.bus_type = ISA_BUS_TYPE;
816 nic->dhcp_dev_id.vendor_id = htons ( isapnp->vendor_id );
817 nic->dhcp_dev_id.device_id = htons ( isapnp->prod_id );