iPXE
3c509.c
Go to the documentation of this file.
00001 /*
00002  * Split out into 3c509.c and 3c5x9.c, to make it possible to build a
00003  * 3c529 module without including ISA, ISAPnP and EISA code.
00004  *
00005  */
00006 
00007 FILE_LICENCE ( BSD2 );
00008 
00009 #include <stdint.h>
00010 #include <stdlib.h>
00011 #include <string.h>
00012 #include <errno.h>
00013 #include <ipxe/io.h>
00014 #include <unistd.h>
00015 #include <ipxe/device.h>
00016 #include <ipxe/isa.h>
00017 #include "3c509.h"
00018 
00019 /*
00020  * 3c509 cards have their own method of contention resolution; this
00021  * effectively defines another bus type similar to ISAPnP.  Even the
00022  * original ISA cards can be programatically mapped to any I/O address
00023  * in the range 0x200-0x3e0.
00024  * 
00025  * However, there is a small problem: once you've activated a card,
00026  * the only ways to deactivate it will also wipe its tag, meaning that
00027  * you won't be able to subsequently reactivate it without going
00028  * through the whole ID sequence again.  The solution we adopt is to
00029  * isolate and tag all cards at the start, and to immediately
00030  * re-isolate and re-tag a card after disabling it.
00031  *
00032  */
00033 
00034 static void t509bus_remove ( struct root_device *rootdev );
00035 
00036 static unsigned int t509_id_port = 0;
00037 static unsigned int t509_max_tag = 0;
00038 
00039 /** A 3c509 device */
00040 struct t509_device {
00041         /** Generic device */
00042         struct device dev;
00043         /** Tag */
00044         unsigned int tag;
00045         /** I/O address */
00046         uint16_t ioaddr;
00047         /** Driver-private data
00048          *
00049          * Use t509_set_drvdata() and t509_get_drvdata() to access
00050          * this field.
00051          */
00052         void *priv;
00053 };
00054 
00055 /**
00056  * Set 3c509 driver-private data
00057  *
00058  * @v t509              3c509 device
00059  * @v priv              Private data
00060  */
00061 static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
00062         t509->priv = priv;
00063 }
00064 
00065 /**
00066  * Get 3c509 driver-private data
00067  *
00068  * @v t509              3c509 device
00069  * @ret priv            Private data
00070  */
00071 static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
00072         return t509->priv;
00073 }
00074 
00075 /*
00076  * t509 utility functions
00077  *
00078  */
00079 
00080 static inline void t509_set_id_port ( void ) {
00081         outb ( 0x00, t509_id_port );
00082 }
00083 
00084 static inline void t509_wait_for_id_sequence ( void ) {
00085         outb ( 0x00, t509_id_port );
00086 }
00087 
00088 static inline void t509_global_reset ( void ) {
00089         outb ( 0xc0, t509_id_port );
00090 }
00091 
00092 static inline void t509_reset_tag ( void ) {
00093         outb ( 0xd0, t509_id_port );
00094 }
00095 
00096 static inline void t509_set_tag ( uint8_t tag ) {
00097         outb ( 0xd0 | tag, t509_id_port );
00098 }
00099 
00100 static inline void t509_select_tag ( uint8_t tag ) {
00101         outb ( 0xd8 | tag, t509_id_port );
00102 }
00103 
00104 static inline void t509_activate ( uint16_t ioaddr ) {
00105         outb ( 0xe0 | ( ioaddr >> 4 ), t509_id_port );
00106 }
00107 
00108 static inline void t509_deactivate_and_reset_tag ( uint16_t ioaddr ) {
00109         outb ( GLOBAL_RESET, ioaddr + EP_COMMAND );
00110 }
00111 
00112 static inline void t509_load_eeprom_word ( uint8_t offset ) {
00113         outb ( 0x80 | offset, t509_id_port );
00114 }
00115 
00116 /*
00117  * Find a suitable ID port
00118  *
00119  */
00120 static inline int t509_find_id_port ( void ) {
00121 
00122         for ( t509_id_port = EP_ID_PORT_START ;
00123               t509_id_port < EP_ID_PORT_END ;
00124               t509_id_port += EP_ID_PORT_INC ) {
00125                 t509_set_id_port ();
00126                 /* See if anything's listening */
00127                 outb ( 0xff, t509_id_port );
00128                 if ( inb ( t509_id_port ) & 0x01 ) {
00129                         /* Found a suitable port */
00130                         DBG ( "T509 using ID port at %04x\n", t509_id_port );
00131                         return 0;
00132                 }
00133         }
00134         /* No id port available */
00135         DBG ( "T509 found no available ID port\n" );
00136         return -ENOENT;
00137 }
00138 
00139 /*
00140  * Send ID sequence to the ID port
00141  *
00142  */
00143 static void t509_send_id_sequence ( void ) {
00144         unsigned short lrs_state, i;
00145 
00146         t509_set_id_port ();
00147         /* Reset IDS on cards */
00148         t509_wait_for_id_sequence ();
00149         lrs_state = 0xff;
00150         for ( i = 0; i < 255; i++ ) {
00151                 outb ( lrs_state, t509_id_port );
00152                 lrs_state <<= 1;
00153                 lrs_state = lrs_state & 0x100 ? lrs_state ^ 0xcf : lrs_state;
00154         }
00155 }
00156 
00157 /*
00158  * We get eeprom data from the id_port given an offset into the eeprom.
00159  * Basically; after the ID_sequence is sent to all of the cards; they enter
00160  * the ID_CMD state where they will accept command requests. 0x80-0xbf loads
00161  * the eeprom data.  We then read the port 16 times and with every read; the
00162  * cards check for contention (ie: if one card writes a 0 bit and another
00163  * writes a 1 bit then the host sees a 0. At the end of the cycle; each card
00164  * compares the data on the bus; if there is a difference then that card goes
00165  * into ID_WAIT state again). In the meantime; one bit of data is returned in
00166  * the AX register which is conveniently returned to us by inb().  Hence; we
00167  * read 16 times getting one bit of data with each read.
00168  */
00169 static uint16_t t509_id_read_eeprom ( int offset ) {
00170         int i, data = 0;
00171 
00172         t509_load_eeprom_word ( offset );
00173         /* Do we really need this wait? Won't be noticeable anyway */
00174         udelay(10000);
00175 
00176         for ( i = 0; i < 16; i++ ) {
00177                 data = ( data << 1 ) | ( inw ( t509_id_port ) & 1 );
00178         }
00179         return data;
00180 }
00181 
00182 /*
00183  * Isolate and tag all t509 cards
00184  *
00185  */
00186 static int t509_isolate ( void ) {
00187         unsigned int i;
00188         uint16_t contend[3];
00189         int rc;
00190 
00191         /* Find a suitable ID port */
00192         if ( ( rc = t509_find_id_port() ) != 0 )
00193                 return rc;
00194 
00195         while ( 1 ) {
00196 
00197                 /* All cards are in ID_WAIT state each time we go
00198                  * through this loop.
00199                  */
00200 
00201                 /* Send the ID sequence */
00202                 t509_send_id_sequence();
00203 
00204                 /* First time through, reset all tags.  On subsequent
00205                  * iterations, kill off any already-tagged cards
00206                  */
00207                 if ( t509_max_tag == 0 ) {
00208                         t509_reset_tag();
00209                 } else {
00210                         t509_select_tag ( 0 );
00211                 }
00212         
00213                 /* Read the manufacturer ID, to see if there are any
00214                  * more cards
00215                  */
00216                 if ( t509_id_read_eeprom ( EEPROM_MFG_ID ) != MFG_ID ) {
00217                         DBG ( "T509 saw %s signs of life\n",
00218                               t509_max_tag ? "no further" : "no" );
00219                         break;
00220                 }
00221 
00222                 /* Perform contention selection on the MAC address */
00223                 for ( i = 0 ; i < 3 ; i++ ) {
00224                         contend[i] = t509_id_read_eeprom ( i );
00225                 }
00226 
00227                 /* Only one device will still be left alive.  Tag it. */
00228                 ++t509_max_tag;
00229                 DBG ( "T509 found card %04x%04x%04x, assigning tag %02x\n",
00230                       contend[0], contend[1], contend[2], t509_max_tag );
00231                 t509_set_tag ( t509_max_tag );
00232 
00233                 /* Return all cards back to ID_WAIT state */
00234                 t509_wait_for_id_sequence();
00235         }
00236 
00237         DBG ( "T509 found %d cards using ID port %04x\n",
00238               t509_max_tag, t509_id_port );
00239         return 0;
00240 }
00241 
00242 /*
00243  * Activate a T509 device
00244  *
00245  * The device will be enabled at whatever ioaddr is specified in the
00246  * struct t509_device; there is no need to stick with the default
00247  * ioaddr read from the EEPROM.
00248  *
00249  */
00250 static inline void activate_t509_device ( struct t509_device *t509 ) {
00251         t509_send_id_sequence ();
00252         t509_select_tag ( t509->tag );
00253         t509_activate ( t509->ioaddr );
00254         DBG ( "T509 activated device %02x at ioaddr %04x\n",
00255               t509->tag, t509->ioaddr );
00256 }
00257 
00258 /*
00259  * Deactivate a T509 device
00260  *
00261  * Disabling also clears the tag, so we immediately isolate and re-tag
00262  * this card.
00263  *
00264  */
00265 static inline void deactivate_t509_device ( struct t509_device *t509 ) {
00266         t509_deactivate_and_reset_tag ( t509->ioaddr );
00267         udelay ( 1000 );
00268         t509_send_id_sequence ();
00269         t509_select_tag ( 0 );
00270         t509_set_tag ( t509->tag );
00271         t509_wait_for_id_sequence ();
00272         DBG ( "T509 deactivated device at %04x and re-tagged as %02x\n",
00273               t509->ioaddr, t509->tag );
00274 }
00275 
00276 /*
00277  * The ISA probe function
00278  *
00279  */
00280 static int legacy_t509_probe ( struct nic *nic, void *hwdev ) {
00281         struct t509_device *t509 = hwdev;
00282 
00283         /* We could change t509->ioaddr if we wanted to */
00284         activate_t509_device ( t509 );
00285         nic->ioaddr = t509->ioaddr;
00286 
00287         /* Hand off to generic t5x9 probe routine */
00288         return t5x9_probe ( nic, ISA_PROD_ID ( PROD_ID ), ISA_PROD_ID_MASK );
00289 }
00290 
00291 static void legacy_t509_disable ( struct nic *nic, void *hwdev ) {
00292         struct t509_device *t509 = hwdev;
00293 
00294         t5x9_disable ( nic );
00295         deactivate_t509_device ( t509 );
00296 }
00297 
00298 static inline void legacy_t509_set_drvdata ( void *hwdev, void *priv ) {
00299         t509_set_drvdata ( hwdev, priv );
00300 }
00301 
00302 static inline void * legacy_t509_get_drvdata ( void *hwdev ) {
00303         return t509_get_drvdata ( hwdev );
00304 }
00305 
00306 /**
00307  * Probe a 3c509 device
00308  *
00309  * @v t509              3c509 device
00310  * @ret rc              Return status code
00311  *
00312  * Searches for a driver for the 3c509 device.  If a driver is found,
00313  * its probe() routine is called.
00314  */
00315 static int t509_probe ( struct t509_device *t509 ) {
00316         DBG ( "Adding 3c509 device %02x (I/O %04x)\n",
00317               t509->tag, t509->ioaddr );
00318         return legacy_probe ( t509, legacy_t509_set_drvdata, &t509->dev,
00319                               legacy_t509_probe, legacy_t509_disable );
00320 }
00321 
00322 /**
00323  * Remove a 3c509 device
00324  *
00325  * @v t509              3c509 device
00326  */
00327 static void t509_remove ( struct t509_device *t509 ) {
00328         legacy_remove ( t509, legacy_t509_get_drvdata, legacy_t509_disable );
00329         DBG ( "Removed 3c509 device %02x\n", t509->tag );
00330 }
00331 
00332 /**
00333  * Probe 3c509 root bus
00334  *
00335  * @v rootdev           3c509 bus root device
00336  *
00337  * Scans the 3c509 bus for devices and registers all devices it can
00338  * find.
00339  */
00340 static int t509bus_probe ( struct root_device *rootdev ) {
00341         struct t509_device *t509 = NULL;
00342         unsigned int tag;
00343         unsigned int iobase;
00344         int rc;
00345 
00346         /* Perform isolation and tagging */
00347         if ( ( rc = t509_isolate() ) != 0 )
00348                 return rc;
00349 
00350         for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
00351                 /* Allocate struct t509_device */
00352                 if ( ! t509 )
00353                         t509 = malloc ( sizeof ( *t509 ) );
00354                 if ( ! t509 ) {
00355                         rc = -ENOMEM;
00356                         goto err;
00357                 }
00358                 memset ( t509, 0, sizeof ( *t509 ) );
00359                 t509->tag = tag;
00360 
00361                 /* Send the ID sequence */
00362                 t509_send_id_sequence ();
00363 
00364                 /* Select the specified tag */
00365                 t509_select_tag ( t509->tag );
00366 
00367                 /* Read the default I/O address */
00368                 iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
00369                 t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
00370 
00371                 /* Send card back to ID_WAIT */
00372                 t509_wait_for_id_sequence();
00373 
00374                 /* Add to device hierarchy */
00375                 snprintf ( t509->dev.name, sizeof ( t509->dev.name ),
00376                            "t509%02x", tag );
00377                 t509->dev.desc.bus_type = BUS_TYPE_ISA;
00378                 t509->dev.desc.vendor = MFG_ID;
00379                 t509->dev.desc.device = PROD_ID;
00380                 t509->dev.parent = &rootdev->dev;
00381                 list_add ( &t509->dev.siblings, &rootdev->dev.children );
00382                 INIT_LIST_HEAD ( &t509->dev.children );
00383                         
00384                 /* Look for a driver */
00385                 if ( t509_probe ( t509 ) == 0 ) {
00386                         /* t509dev registered, we can drop our ref */
00387                         t509 = NULL;
00388                 } else {
00389                         /* Not registered; re-use struct */
00390                         list_del ( &t509->dev.siblings );
00391                 }
00392         }
00393 
00394         free ( t509 );
00395         return 0;
00396 
00397  err:
00398         free ( t509 );
00399         t509bus_remove ( rootdev );
00400         return rc;
00401 }
00402 
00403 /**
00404  * Remove 3c509 root bus
00405  *
00406  * @v rootdev           3c509 bus root device
00407  */
00408 static void t509bus_remove ( struct root_device *rootdev ) {
00409         struct t509_device *t509;
00410         struct t509_device *tmp;
00411 
00412         list_for_each_entry_safe ( t509, tmp, &rootdev->dev.children,
00413                                    dev.siblings ) {
00414                 t509_remove ( t509 );
00415                 list_del ( &t509->dev.siblings );
00416                 free ( t509 );
00417         }
00418 }
00419 
00420 /** 3c509 bus root device driver */
00421 static struct root_driver t509_root_driver = {
00422         .probe = t509bus_probe,
00423         .remove = t509bus_remove,
00424 };
00425 
00426 /** 3c509 bus root device */
00427 struct root_device t509_root_device __root_device = {
00428         .dev = { .name = "3c509" },
00429         .driver = &t509_root_driver,
00430 };
00431 
00432 ISA_ROM ( "3c509", "3c509" );