00001
00002
00003
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 <gpxe/io.h>
00014 #include <unistd.h>
00015 #include <gpxe/device.h>
00016 #include <gpxe/isa.h>
00017 #include "3c509.h"
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00040 struct t509_device {
00041
00042 struct device dev;
00043
00044 unsigned int tag;
00045
00046 uint16_t ioaddr;
00047
00048
00049
00050
00051
00052 void *priv;
00053 };
00054
00055
00056
00057
00058
00059
00060
00061 static inline void t509_set_drvdata ( struct t509_device *t509, void *priv ) {
00062 t509->priv = priv;
00063 }
00064
00065
00066
00067
00068
00069
00070
00071 static inline void * t509_get_drvdata ( struct t509_device *t509 ) {
00072 return t509->priv;
00073 }
00074
00075
00076
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
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
00127 outb ( 0xff, t509_id_port );
00128 if ( inb ( t509_id_port ) & 0x01 ) {
00129
00130 DBG ( "T509 using ID port at %04x\n", t509_id_port );
00131 return 0;
00132 }
00133 }
00134
00135 DBG ( "T509 found no available ID port\n" );
00136 return -ENOENT;
00137 }
00138
00139
00140
00141
00142
00143 static void t509_send_id_sequence ( void ) {
00144 unsigned short lrs_state, i;
00145
00146 t509_set_id_port ();
00147
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
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169 static uint16_t t509_id_read_eeprom ( int offset ) {
00170 int i, data = 0;
00171
00172 t509_load_eeprom_word ( offset );
00173
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
00184
00185
00186 static int t509_isolate ( void ) {
00187 unsigned int i;
00188 uint16_t contend[3];
00189 int rc;
00190
00191
00192 if ( ( rc = t509_find_id_port() ) != 0 )
00193 return rc;
00194
00195 while ( 1 ) {
00196
00197
00198
00199
00200
00201
00202 t509_send_id_sequence();
00203
00204
00205
00206
00207 if ( t509_max_tag == 0 ) {
00208 t509_reset_tag();
00209 } else {
00210 t509_select_tag ( 0 );
00211 }
00212
00213
00214
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
00223 for ( i = 0 ; i < 3 ; i++ ) {
00224 contend[i] = t509_id_read_eeprom ( i );
00225 }
00226
00227
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
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
00244
00245
00246
00247
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
00260
00261
00262
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
00278
00279
00280 static int legacy_t509_probe ( struct nic *nic, void *hwdev ) {
00281 struct t509_device *t509 = hwdev;
00282
00283
00284 activate_t509_device ( t509 );
00285 nic->ioaddr = t509->ioaddr;
00286
00287
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
00308
00309
00310
00311
00312
00313
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
00324
00325
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
00334
00335
00336
00337
00338
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
00347 if ( ( rc = t509_isolate() ) != 0 )
00348 return rc;
00349
00350 for ( tag = 1 ; tag <= t509_max_tag ; tag++ ) {
00351
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
00362 t509_send_id_sequence ();
00363
00364
00365 t509_select_tag ( t509->tag );
00366
00367
00368 iobase = t509_id_read_eeprom ( EEPROM_ADDR_CFG );
00369 t509->ioaddr = 0x200 + ( ( iobase & 0x1f ) << 4 );
00370
00371
00372 t509_wait_for_id_sequence();
00373
00374
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
00385 if ( t509_probe ( t509 ) == 0 ) {
00386
00387 t509 = NULL;
00388 } else {
00389
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
00405
00406
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
00421 static struct root_driver t509_root_driver = {
00422 .probe = t509bus_probe,
00423 .remove = t509bus_remove,
00424 };
00425
00426
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" );