Home | History | Annotate | Download | only in netboot
      1 /* -*- Mode:C; c-basic-offset:4; -*- */
      2 
      3 /*
      4   Tulip and clone Etherboot Driver
      5 
      6   By Marty Connor (mdc (at) thinguin.org)
      7   Copyright (C) 2001 Entity Cyber, Inc.
      8 
      9   This software may be used and distributed according to the terms
     10   of the GNU Public License, incorporated herein by reference.
     11 
     12   As of April 2001 this driver should support most tulip cards that
     13   the Linux tulip driver supports because Donald Becker's Linux media
     14   detection code is now included.
     15 
     16   Based on Ken Yap's Tulip Etherboot Driver and Donald Becker's
     17   Linux Tulip Driver. Supports N-Way speed auto-configuration on
     18   MX98715, MX98715A and MX98725. Support inexpensive PCI 10/100 cards
     19   based on the Macronix MX987x5 chip, such as the SOHOware Fast
     20   model SFA110A, and the LinkSYS model LNE100TX. The NetGear
     21   model FA310X, based on the LC82C168 chip is supported.
     22   The TRENDnet TE100-PCIA NIC which uses a genuine Intel 21143-PD
     23   chipset is supported. Also, Davicom DM9102's.
     24 
     25   Documentation and source code used:
     26   Source for Etherboot driver at
     27   http://etherboot.sourceforge.net/
     28   MX98715A Data Sheet and MX98715A Application Note
     29   on http://www.macronix.com/  (PDF format files)
     30   Source for Linux tulip driver at
     31   http://cesdis.gsfc.nasa.gov/linux/drivers/tulip.html
     32 
     33   Adapted by Ken Yap from
     34   FreeBSD netboot DEC 21143 driver
     35   Author: David Sharp
     36   date: Nov/98
     37 
     38   Some code fragments were taken from verious places, Ken Yap's
     39   etherboot, FreeBSD's if_de.c, and various Linux related files.
     40   DEC's manuals for the 21143 and SROM format were very helpful.
     41   The Linux de driver development page has a number of links to
     42   useful related information.  Have a look at:
     43   ftp://cesdis.gsfc.nasa.gov/pub/linux/drivers/tulip-devel.html
     44 */
     45 
     46 /*********************************************************************/
     47 /* Revision History                                                  */
     48 /*********************************************************************/
     49 
     50 /*
     51   11 Apr 2001  mdc     [patch to etherboot 4.7.24]
     52      Major rewrite to include Linux tulip driver media detection
     53      code.  This driver should support a lot more cards now.
     54   16 Jul 2000  mdc     0.75b11
     55      Added support for ADMtek 0985 Centaur-P, a "Comet" tulip clone
     56      which is used on the LinkSYS LNE100TX v4.x cards.  We already
     57      support LNE100TX v2.0 cards, which use a different controller.
     58   04 Jul 2000   jam     ?
     59      Added test of status after receiving a packet from the card.
     60      Also uncommented the tulip_disable routine.  Stray packets
     61      seemed to be causing problems.
     62   27 Apr 2000   njl     ?
     63   29 Feb 2000   mdc     0.75b7
     64      Increased reset delay to 3 seconds because Macronix cards seem to
     65      need more reset time before card comes back to a usable state.
     66   26 Feb 2000   mdc     0.75b6
     67      Added a 1 second delay after initializing the transmitter because
     68      some cards seem to need the time or they drop the first packet
     69      transmitted.
     70   23 Feb 2000   mdc     0.75b5
     71      removed udelay code and used currticks() for more reliable delay
     72      code in reset pause and sanity timeouts.  Added function prototypes
     73      and TX debugging code.
     74   21 Feb 2000   mdc     patch to Etherboot 4.4.3
     75      Incorporated patches from Bob Edwards and Paul Mackerras of
     76      Linuxcare's OZLabs to deal with inefficiencies in tulip_transmit
     77      and udelay.  We now wait for packet transmission to complete
     78      (or sanity timeout).
     79   04 Feb 2000   Robert.Edwards (at) anu.edu.au patch to Etherboot 4.4.2
     80      patch to tulip.c that implements the automatic selection of the MII
     81      interface on cards using the Intel/DEC 21143 reference design, in
     82      particular, the TRENDnet TE100-PCIA NIC which uses a genuine Intel
     83      21143-PD chipset.
     84   11 Jan 2000   mdc     0.75b4
     85      Added support for NetGear FA310TX card based on the LC82C168
     86      chip.  This should also support Lite-On LC82C168 boards.
     87      Added simple MII support. Re-arranged code to better modularize
     88      initializations.
     89   04 Dec 1999   mdc     0.75b3
     90      Added preliminary support for LNE100TX PCI cards.  Should work for
     91      PNIC2 cards. No MII support, but single interface (RJ45) tulip
     92      cards seem to not care.
     93   03 Dec 1999   mdc     0.75b2
     94      Renamed from mx987x5 to tulip, merged in original tulip init code
     95      from tulip.c to support other tulip compatible cards.
     96   02 Dec 1999   mdc     0.75b1
     97      Released Beta MX987x5 Driver for code review and testing to netboot
     98      and thinguin mailing lists.
     99 */
    100 
    101 
    102 /*********************************************************************/
    104 /* Declarations                                                      */
    105 /*********************************************************************/
    106 
    107 #include "etherboot.h"
    108 #include "nic.h"
    109 #include "pci.h"
    110 #include "cards.h"
    111 
    112 /* User settable parameters */
    113 
    114 #undef   TULIP_DEBUG
    115 #undef   TULIP_DEBUG_WHERE
    116 static int tulip_debug = 2;             /* 1 normal messages, 0 quiet .. 7 verbose. */
    117 
    118 #define TX_TIME_OUT       2*TICKS_PER_SEC
    119 
    120 typedef unsigned char  u8;
    121 typedef   signed char  s8;
    122 typedef unsigned short u16;
    123 typedef   signed short s16;
    124 typedef unsigned int   u32;
    125 typedef   signed int   s32;
    126 
    127 /* helpful macros if on a big_endian machine for changing byte order.
    128    not strictly needed on Intel */
    129 #define le16_to_cpu(val) (val)
    130 #define cpu_to_le32(val) (val)
    131 #define get_unaligned(ptr) (*(ptr))
    132 #define put_unaligned(val, ptr) ((void)( *(ptr) = (val) ))
    133 #define get_u16(ptr) (*(u16 *)(ptr))
    134 #define virt_to_bus(x) ((unsigned long)x)
    135 #define virt_to_le32desc(addr)  virt_to_bus(addr)
    136 
    137 #define TULIP_IOTYPE  PCI_USES_MASTER | PCI_USES_IO | PCI_ADDR0
    138 #define TULIP_SIZE 0x80
    139 
    140 /* This is a mysterious value that can be written to CSR11 in the 21040 (only)
    141    to support a pre-NWay full-duplex signaling mechanism using short frames.
    142    No one knows what it should be, but if left at its default value some
    143    10base2(!) packets trigger a full-duplex-request interrupt. */
    144 #define FULL_DUPLEX_MAGIC       0x6969
    145 
    146 static const int csr0 = 0x01A00000 | 0x8000;
    147 
    148 /*  The possible media types that can be set in options[] are: */
    149 #define MEDIA_MASK 31
    150 static const char * const medianame[32] = {
    151     "10baseT", "10base2", "AUI", "100baseTx",
    152     "10baseT-FDX", "100baseTx-FDX", "100baseT4", "100baseFx",
    153     "100baseFx-FDX", "MII 10baseT", "MII 10baseT-FDX", "MII",
    154     "10baseT(forced)", "MII 100baseTx", "MII 100baseTx-FDX", "MII 100baseT4",
    155     "MII 100baseFx-HDX", "MII 100baseFx-FDX", "Home-PNA 1Mbps", "Invalid-19",
    156 };
    157 
    158 /* This much match tulip_tbl[]!  Note 21142 == 21143. */
    159 enum tulip_chips {
    160     DC21040=0, DC21041=1, DC21140=2, DC21142=3, DC21143=3,
    161     LC82C168, MX98713, MX98715, MX98725, AX88141, AX88140, PNIC2, COMET,
    162     COMPEX9881, I21145, XIRCOM
    163 };
    164 
    165 enum pci_id_flags_bits {
    166     /* Set PCI command register bits before calling probe1(). */
    167     PCI_USES_IO=1, PCI_USES_MEM=2, PCI_USES_MASTER=4,
    168     /* Read and map the single following PCI BAR. */
    169     PCI_ADDR0=0<<4, PCI_ADDR1=1<<4, PCI_ADDR2=2<<4, PCI_ADDR3=3<<4,
    170     PCI_ADDR_64BITS=0x100, PCI_NO_ACPI_WAKE=0x200, PCI_NO_MIN_LATENCY=0x400,
    171     PCI_UNUSED_IRQ=0x800,
    172 };
    173 
    174 struct pci_id_info {
    175     char *name;
    176     struct match_info {
    177         u32 pci, pci_mask, subsystem, subsystem_mask;
    178         u32 revision, revision_mask;                            /* Only 8 bits. */
    179     } id;
    180     enum pci_id_flags_bits pci_flags;
    181     int io_size;                                /* Needed for I/O region check or ioremap(). */
    182     int drv_flags;                              /* Driver use, intended as capability flags. */
    183 };
    184 
    185 static struct pci_id_info pci_id_tbl[] = {
    186     { "Digital DC21040 Tulip", { 0x00021011, 0xffffffff, 0, 0, 0, 0 },
    187       TULIP_IOTYPE, 0x80, DC21040 },
    188     { "Digital DC21041 Tulip", { 0x00141011, 0xffffffff, 0, 0, 0, 0 },
    189       TULIP_IOTYPE, 0x80, DC21041 },
    190     { "Digital DS21140A Tulip", { 0x00091011, 0xffffffff, 0,0, 0x20,0xf0 },
    191       TULIP_IOTYPE, 0x80, DC21140 },
    192     { "Digital DS21140 Tulip", { 0x00091011, 0xffffffff, 0, 0, 0, 0 },
    193       TULIP_IOTYPE, 0x80, DC21140 },
    194     { "Digital DS21143 Tulip", { 0x00191011, 0xffffffff, 0,0, 65,0xff },
    195       TULIP_IOTYPE, TULIP_SIZE, DC21142 },
    196     { "Digital DS21142 Tulip", { 0x00191011, 0xffffffff, 0, 0, 0, 0 },
    197       TULIP_IOTYPE, TULIP_SIZE, DC21142 },
    198     { "Kingston KNE110tx (PNIC)", { 0x000211AD, 0xffffffff, 0xf0022646, 0xffffffff, 0, 0 },
    199       TULIP_IOTYPE, 256, LC82C168 },
    200     { "Lite-On 82c168 PNIC", { 0x000211AD, 0xffffffff, 0, 0, 0, 0 },
    201       TULIP_IOTYPE, 256, LC82C168 },
    202     { "Macronix 98713 PMAC", { 0x051210d9, 0xffffffff, 0, 0, 0, 0 },
    203       TULIP_IOTYPE, 256, MX98713 },
    204     { "Macronix 98715 PMAC", { 0x053110d9, 0xffffffff, 0, 0, 0, 0 },
    205       TULIP_IOTYPE, 256, MX98715 },
    206     { "Macronix 98725 PMAC", { 0x053110d9, 0xffffffff, 0, 0, 0, 0 },
    207       TULIP_IOTYPE, 256, MX98725 },
    208     { "ASIX AX88141", { 0x1400125B, 0xffffffff, 0,0, 0x10, 0xf0 },
    209       TULIP_IOTYPE, 128, AX88141 },
    210     { "ASIX AX88140", { 0x1400125B, 0xffffffff, 0, 0, 0, 0 },
    211       TULIP_IOTYPE, 128, AX88140 },
    212     { "Lite-On LC82C115 PNIC-II", { 0xc11511AD, 0xffffffff, 0, 0, 0, 0 },
    213       TULIP_IOTYPE, 256, PNIC2 },
    214     { "ADMtek AN981 Comet", { 0x09811317, 0xffffffff, 0, 0, 0, 0 },
    215       TULIP_IOTYPE, 256, COMET },
    216     { "ADMtek Centaur-P", { 0x09851317, 0xffffffff, 0, 0, 0, 0 },
    217       TULIP_IOTYPE, 256, COMET },
    218     { "ADMtek Centaur-C", { 0x19851317, 0xffffffff, 0, 0, 0, 0 },
    219       TULIP_IOTYPE, 256, COMET },
    220     { "Compex RL100-TX", { 0x988111F6, 0xffffffff, 0, 0, 0, 0 },
    221       TULIP_IOTYPE, 128, COMPEX9881 },
    222     { "Intel 21145 Tulip", { 0x00398086, 0xffffffff, 0, 0, 0, 0 },
    223       TULIP_IOTYPE, 128, I21145 },
    224     { "Xircom Tulip clone", { 0x0003115d, 0xffffffff, 0, 0, 0, 0 },
    225       TULIP_IOTYPE, 128, XIRCOM },
    226     { "Davicom DM9102", { 0x91021282, 0xffffffff, 0, 0, 0, 0 },
    227       TULIP_IOTYPE, 0x80, DC21140 },
    228     { "Davicom DM9100", { 0x91001282, 0xffffffff, 0, 0, 0, 0 },
    229       TULIP_IOTYPE, 0x80, DC21140 },
    230     { "Macronix mxic-98715 (EN1217)", { 0x12171113, 0xffffffff, 0, 0, 0, 0 },
    231       TULIP_IOTYPE, 256, MX98715 },
    232     { 0, { 0, 0, 0, 0, 0, 0 }, 0, 0, 0 },
    233 };
    234 
    235 enum tbl_flag {
    236     HAS_MII=1, HAS_MEDIA_TABLE=2, CSR12_IN_SROM=4, ALWAYS_CHECK_MII=8,
    237     HAS_PWRDWN=0x10, MC_HASH_ONLY=0x20, /* Hash-only multicast filter. */
    238     HAS_PNICNWAY=0x80, HAS_NWAY=0x40,   /* Uses internal NWay xcvr. */
    239     HAS_INTR_MITIGATION=0x100, IS_ASIX=0x200, HAS_8023X=0x400,
    240 };
    241 
    242 /* Note: this table must match  enum tulip_chips  above. */
    243 static struct tulip_chip_table {
    244     char *chip_name;
    245     int flags;
    246 } tulip_tbl[] = {
    247     { "Digital DC21040 Tulip", 0},
    248     { "Digital DC21041 Tulip", HAS_MEDIA_TABLE | HAS_NWAY },
    249     { "Digital DS21140 Tulip", HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM },
    250     { "Digital DS21143 Tulip", HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII
    251       | HAS_PWRDWN | HAS_NWAY   | HAS_INTR_MITIGATION },
    252     { "Lite-On 82c168 PNIC", HAS_MII | HAS_PNICNWAY },
    253     { "Macronix 98713 PMAC", HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM },
    254     { "Macronix 98715 PMAC", HAS_MEDIA_TABLE },
    255     { "Macronix 98725 PMAC", HAS_MEDIA_TABLE },
    256     { "ASIX AX88140", HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM
    257       | MC_HASH_ONLY | IS_ASIX },
    258     { "ASIX AX88141", HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM | MC_HASH_ONLY
    259       | IS_ASIX },
    260     { "Lite-On PNIC-II", HAS_MII | HAS_NWAY | HAS_8023X },
    261     { "ADMtek Comet", MC_HASH_ONLY },
    262     { "Compex 9881 PMAC",       HAS_MII | HAS_MEDIA_TABLE | CSR12_IN_SROM },
    263     { "Intel DS21145 Tulip", HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII
    264       | HAS_PWRDWN | HAS_NWAY },
    265     { "Xircom tulip work-alike", HAS_MII | HAS_MEDIA_TABLE | ALWAYS_CHECK_MII
    266       | HAS_PWRDWN | HAS_NWAY },
    267     { 0, 0 },
    268 };
    269 
    270 /* A full-duplex map for media types. */
    271 enum MediaIs {
    272     MediaIsFD = 1, MediaAlwaysFD=2, MediaIsMII=4, MediaIsFx=8,
    273     MediaIs100=16};
    274 
    275 static const char media_cap[32] =
    276 {0,0,0,16,  3,19,16,24,  27,4,7,5, 0,20,23,20, 20,31,0,0, };
    277 static u8 t21040_csr13[] = {2,0x0C,8,4,  4,0,0,0, 0,0,0,0, 4,0,0,0};
    278 
    279 /* 21041 transceiver register settings: 10-T, 10-2, AUI, 10-T, 10T-FD */
    280 static u16 t21041_csr13[] = { 0xEF01, 0xEF09, 0xEF09, 0xEF01, 0xEF09, };
    281 static u16 t21041_csr14[] = { 0xFFFF, 0xF7FD, 0xF7FD, 0x7F3F, 0x7F3D, };
    282 static u16 t21041_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
    283 
    284 static u16 t21142_csr13[] = { 0x0001, 0x0009, 0x0009, 0x0000, 0x0001, };
    285 static u16 t21142_csr14[] = { 0xFFFF, 0x0705, 0x0705, 0x0000, 0x7F3D, };
    286 static u16 t21142_csr15[] = { 0x0008, 0x0006, 0x000E, 0x0008, 0x0008, };
    287 
    288 /* Offsets to the Command and Status Registers, "CSRs".  All accesses
    289    must be longword instructions and quadword aligned. */
    290 enum tulip_offsets {
    291     CSR0=0,     CSR1=0x08,  CSR2=0x10,  CSR3=0x18,  CSR4=0x20,  CSR5=0x28,
    292     CSR6=0x30,  CSR7=0x38,  CSR8=0x40,  CSR9=0x48, CSR10=0x50, CSR11=0x58,
    293     CSR12=0x60, CSR13=0x68, CSR14=0x70, CSR15=0x78, CSR16=0x80, CSR20=0xA0
    294 };
    295 
    296 /* The bits in the CSR5 status registers, mostly interrupt sources. */
    297 enum status_bits {
    298     TimerInt=0x800, TPLnkFail=0x1000, TPLnkPass=0x10,
    299     NormalIntr=0x10000, AbnormalIntr=0x8000,
    300     RxJabber=0x200, RxDied=0x100, RxNoBuf=0x80, RxIntr=0x40,
    301     TxFIFOUnderflow=0x20, TxJabber=0x08, TxNoBuf=0x04, TxDied=0x02, TxIntr=0x01,
    302 };
    303 
    304 enum desc_status_bits {
    305     DescOwnded=0x80000000, RxDescFatalErr=0x8000, RxWholePkt=0x0300,
    306 };
    307 
    308 struct medialeaf {
    309     u8 type;
    310     u8 media;
    311     unsigned char *leafdata;
    312 };
    313 
    314 struct mediatable {
    315     u16 defaultmedia;
    316     u8 leafcount, csr12dir;                             /* General purpose pin directions. */
    317     unsigned has_mii:1, has_nonmii:1, has_reset:6;
    318     u32 csr15dir, csr15val;                             /* 21143 NWay setting. */
    319     struct medialeaf mleaf[0];
    320 };
    321 
    322 struct mediainfo {
    323     struct mediainfo *next;
    324     int info_type;
    325     int index;
    326     unsigned char *info;
    327 };
    328 
    329 /* EEPROM Address width definitions */
    330 #define EEPROM_ADDRLEN 6
    331 #define EEPROM_SIZE    128              /* 2 << EEPROM_ADDRLEN */
    332 
    333 /* The EEPROM commands include the alway-set leading bit. */
    334 #define EE_WRITE_CMD    (5 << addr_len)
    335 #define EE_READ_CMD     (6 << addr_len)
    336 #define EE_ERASE_CMD    (7 << addr_len)
    337 
    338 /* EEPROM_Ctrl bits. */
    339 #define EE_SHIFT_CLK    0x02    /* EEPROM shift clock. */
    340 #define EE_CS           0x01    /* EEPROM chip select. */
    341 #define EE_DATA_WRITE   0x04    /* EEPROM chip data in. */
    342 #define EE_WRITE_0      0x01
    343 #define EE_WRITE_1      0x05
    344 #define EE_DATA_READ    0x08    /* EEPROM chip data out. */
    345 #define EE_ENB          (0x4800 | EE_CS)
    346 
    347 /* Delay between EEPROM clock transitions.  Even at 33Mhz current PCI
    348    implementations don't overrun the EEPROM clock.  We add a bus
    349    turn-around to insure that this remains true.  */
    350 #define eeprom_delay()  inl(ee_addr)
    351 
    352 /* Size of transmit and receive buffers */
    353 #define BUFLEN 1536
    354 
    355 /* Ring-wrap flag in length field, use for last ring entry.
    356    0x01000000 means chain on buffer2 address,
    357    0x02000000 means use the ring start address in CSR2/3.
    358    Note: Some work-alike chips do not function correctly in chained mode.
    359    The ASIX chip works only in chained mode.
    360    Thus we indicate ring mode, but always write the 'next' field for
    361    chained mode as well. */
    362 #define DESC_RING_WRAP 0x02000000
    363 
    364 /* transmit and receive descriptor format */
    365 struct tulip_rx_desc {
    366     volatile u32 status;
    367     u32 length;
    368     u32 buffer1, buffer2;
    369 };
    370 
    371 struct tulip_tx_desc {
    372     volatile u32 status;
    373     u32 length;
    374     u32 buffer1, buffer2;
    375 };
    376 
    377 /*********************************************************************/
    378 /* Global Storage                                                    */
    379 /*********************************************************************/
    380 
    381 static u32 ioaddr;
    382 
    383 /* Note: transmit and receive buffers must be longword aligned and
    384    longword divisable */
    385 
    386 #define TX_RING_SIZE	2
    387 static struct tulip_tx_desc tx_ring[TX_RING_SIZE] __attribute__ ((aligned(4)));
    388 
    389 #ifdef USE_LOWMEM_BUFFER
    390 #define txb ((char *)0x10000 - BUFLEN)
    391 #else
    392 static unsigned char txb[BUFLEN] __attribute__ ((aligned(4)));
    393 #endif
    394 
    395 #define RX_RING_SIZE	4
    396 static struct tulip_rx_desc rx_ring[RX_RING_SIZE] __attribute__ ((aligned(4)));
    397 
    398 #ifdef USE_LOWMEM_BUFFER
    399 #define rxb ((char *)0x10000 - RX_RING_SIZE * BUFLEN - BUFLEN)
    400 #else
    401 static unsigned char rxb[RX_RING_SIZE * BUFLEN] __attribute__ ((aligned(4)));
    402 #endif
    403 
    404 static struct tulip_private {
    405     int cur_rx;
    406     int chip_id;                        /* index into tulip_tbl[]  */
    407     int pci_id_idx;                     /* index into pci_id_tbl[] */
    408     int revision;
    409     int flags;
    410     unsigned short vendor_id;           /* PCI card vendor code */
    411     unsigned short dev_id;              /* PCI card device code */
    412     unsigned char ehdr[ETH_HLEN];       /* buffer for ethernet header */
    413     const char *nic_name;
    414     unsigned int csr0, csr6;            /* Current CSR0, CSR6 settings. */
    415     unsigned int if_port;
    416     unsigned int full_duplex;         /* Full-duplex operation requested. */
    417     unsigned int full_duplex_lock;
    418     unsigned int medialock;           /* Do not sense media type. */
    419     unsigned int mediasense;          /* Media sensing in progress. */
    420     unsigned int nway, nwayset;     /* 21143 internal NWay. */
    421     unsigned int default_port;
    422     unsigned char eeprom[EEPROM_SIZE];  /* Serial EEPROM contents. */
    423     u8 media_table_storage[(sizeof(struct mediatable) + 32*sizeof(struct medialeaf))];
    424     u16 sym_advertise, mii_advertise;   /* NWay to-advertise. */
    425     struct mediatable *mtable;
    426     u16 lpar;                           /* 21143 Link partner ability. */
    427     u16 advertising[4];                 /* MII advertise, from SROM table. */
    428     signed char phys[4], mii_cnt;       /* MII device addresses. */
    429     int cur_index;                      /* Current media index. */
    430     int saved_if_port;
    431 } tpx;
    432 
    433 static struct tulip_private *tp;
    434 
    435 /* Known cards that have old-style EEPROMs.
    436    Writing this table is described at
    437    http://cesdis.gsfc.nasa.gov/linux/drivers/tulip-drivers/tulip-media.html */
    438 static struct fixups {
    439     char *name;
    440     unsigned char addr0, addr1, addr2;
    441     u16 newtable[32];                           /* Max length below. */
    442 } eeprom_fixups[] = {
    443     {"Asante", 0, 0, 0x94, {0x1e00, 0x0000, 0x0800, 0x0100, 0x018c,
    444                             0x0000, 0x0000, 0xe078, 0x0001, 0x0050, 0x0018 }},
    445     {"SMC9332DST", 0, 0, 0xC0, { 0x1e00, 0x0000, 0x0800, 0x041f,
    446                                  0x0000, 0x009E, /* 10baseT */
    447                                  0x0004, 0x009E, /* 10baseT-FD */
    448                                  0x0903, 0x006D, /* 100baseTx */
    449                                  0x0905, 0x006D, /* 100baseTx-FD */ }},
    450     {"Cogent EM100", 0, 0, 0x92, { 0x1e00, 0x0000, 0x0800, 0x063f,
    451                                    0x0107, 0x8021, /* 100baseFx */
    452                                    0x0108, 0x8021, /* 100baseFx-FD */
    453                                    0x0100, 0x009E, /* 10baseT */
    454                                    0x0104, 0x009E, /* 10baseT-FD */
    455                                    0x0103, 0x006D, /* 100baseTx */
    456                                    0x0105, 0x006D, /* 100baseTx-FD */ }},
    457     {"Maxtech NX-110", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x0513,
    458                                      0x1001, 0x009E, /* 10base2, CSR12 0x10*/
    459                                      0x0000, 0x009E, /* 10baseT */
    460                                      0x0004, 0x009E, /* 10baseT-FD */
    461                                      0x0303, 0x006D, /* 100baseTx, CSR12 0x03 */
    462                                      0x0305, 0x006D, /* 100baseTx-FD CSR12 0x03 */}},
    463     {"Accton EN1207", 0, 0, 0xE8, { 0x1e00, 0x0000, 0x0800, 0x051F,
    464                                     0x1B01, 0x0000, /* 10base2,   CSR12 0x1B */
    465                                     0x0B00, 0x009E, /* 10baseT,   CSR12 0x0B */
    466                                     0x0B04, 0x009E, /* 10baseT-FD,CSR12 0x0B */
    467                                     0x1B03, 0x006D, /* 100baseTx, CSR12 0x1B */
    468                                     0x1B05, 0x006D, /* 100baseTx-FD CSR12 0x1B */
    469     }},
    470     {0, 0, 0, 0, {}}};
    471 
    472 static const char * block_name[] = {"21140 non-MII", "21140 MII PHY",
    473                                     "21142 Serial PHY", "21142 MII PHY", "21143 SYM PHY", "21143 reset method"};
    474 
    475 
    476 /*********************************************************************/
    478 /* Function Prototypes                                               */
    479 /*********************************************************************/
    480 static int mdio_read(struct nic *nic, int phy_id, int location);
    481 static void mdio_write(struct nic *nic, int phy_id, int location, int value);
    482 static int read_eeprom(unsigned long ioaddr, int location, int addr_len);
    483 static void parse_eeprom(struct nic *nic);
    484 struct nic *tulip_probe(struct nic *nic, unsigned short *io_addrs,
    485                         struct pci_device *pci);
    486 static void tulip_init_ring(struct nic *nic);
    487 static void tulip_reset(struct nic *nic);
    488 static void tulip_transmit(struct nic *nic, const char *d, unsigned int t,
    489                            unsigned int s, const char *p);
    490 static int tulip_poll(struct nic *nic);
    491 static void tulip_disable(struct nic *nic);
    492 static void nway_start(struct nic *nic);
    493 static void pnic_do_nway(struct nic *nic);
    494 static void select_media(struct nic *nic, int startup);
    495 static void init_media(struct nic *nic);
    496 static void start_link(struct nic *nic);
    497 static int tulip_check_duplex(struct nic *nic);
    498 
    499 static void tulip_wait(unsigned int nticks);
    500 
    501 #ifdef TULIP_DEBUG_WHERE
    502 static void whereami(const char *str);
    503 #endif
    504 
    505 #ifdef TULIP_DEBUG
    506 static void tulip_more(void);
    507 #endif
    508 
    509 
    510 /*********************************************************************/
    512 /* Utility Routines                                                  */
    513 /*********************************************************************/
    514 
    515 #ifdef TULIP_DEBUG_WHERE
    516 static void whereami (const char *str)
    517 {
    518     printf("%s: %s\n", tp->nic_name, str);
    519     /* sleep(2); */
    520 }
    521 #endif
    522 
    523 #ifdef  TULIP_DEBUG
    524 static void tulip_more(void)
    525 {
    526     printf("\n\n-- more --");
    527     while (!iskey())
    528         /* wait */;
    529     getchar();
    530     printf("\n\n");
    531 }
    532 #endif /* TULIP_DEBUG */
    533 
    534 static void tulip_wait(unsigned int nticks)
    535 {
    536     unsigned int to = currticks() + nticks;
    537     while (currticks() < to)
    538         /* wait */ ;
    539 }
    540 
    541 
    542 /*********************************************************************/
    544 /* Media Descriptor Code                                             */
    545 /*********************************************************************/
    546 
    547 /* MII transceiver control section.
    548    Read and write the MII registers using software-generated serial
    549    MDIO protocol.  See the MII specifications or DP83840A data sheet
    550    for details. */
    551 
    552 /* The maximum data clock rate is 2.5 Mhz.  The minimum timing is usually
    553    met by back-to-back PCI I/O cycles, but we insert a delay to avoid
    554    "overclocking" issues or future 66Mhz PCI. */
    555 #define mdio_delay() inl(mdio_addr)
    556 
    557 /* Read and write the MII registers using software-generated serial
    558    MDIO protocol.  It is just different enough from the EEPROM protocol
    559    to not share code.  The maxium data clock rate is 2.5 Mhz. */
    560 #define MDIO_SHIFT_CLK  0x10000
    561 #define MDIO_DATA_WRITE0 0x00000
    562 #define MDIO_DATA_WRITE1 0x20000
    563 #define MDIO_ENB                0x00000         /* Ignore the 0x02000 databook setting. */
    564 #define MDIO_ENB_IN             0x40000
    565 #define MDIO_DATA_READ  0x80000
    566 
    567 /* MII transceiver control section.
    568    Read and write the MII registers using software-generated serial
    569    MDIO protocol.  See the MII specifications or DP83840A data sheet
    570    for details. */
    571 
    572 int mdio_read(struct nic *nic, int phy_id, int location)
    573 {
    574     int i;
    575     int read_cmd = (0xf6 << 10) | (phy_id << 5) | location;
    576     int retval = 0;
    577     long mdio_addr = ioaddr + CSR9;
    578 
    579 #ifdef TULIP_DEBUG_WHERE
    580     whereami("mdio_read\n");
    581 #endif
    582 
    583     if (tp->chip_id == LC82C168) {
    584 	int i = 1000;
    585 	outl(0x60020000 + (phy_id<<23) + (location<<18), ioaddr + 0xA0);
    586 	inl(ioaddr + 0xA0);
    587 	inl(ioaddr + 0xA0);
    588 	while (--i > 0)
    589 	    if ( ! ((retval = inl(ioaddr + 0xA0)) & 0x80000000))
    590 		return retval & 0xffff;
    591 	return 0xffff;
    592     }
    593 
    594     if (tp->chip_id == COMET) {
    595 	if (phy_id == 1) {
    596 	    if (location < 7)
    597 		return inl(ioaddr + 0xB4 + (location<<2));
    598 	    else if (location == 17)
    599 		return inl(ioaddr + 0xD0);
    600 	    else if (location >= 29 && location <= 31)
    601 		return inl(ioaddr + 0xD4 + ((location-29)<<2));
    602 	}
    603 	return 0xffff;
    604     }
    605 
    606     /* Establish sync by sending at least 32 logic ones. */
    607     for (i = 32; i >= 0; i--) {
    608 	outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
    609 	mdio_delay();
    610 	outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
    611 	mdio_delay();
    612     }
    613     /* Shift the read command bits out. */
    614     for (i = 15; i >= 0; i--) {
    615 	int dataval = (read_cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
    616 
    617 	outl(MDIO_ENB | dataval, mdio_addr);
    618 	mdio_delay();
    619 	outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
    620 	mdio_delay();
    621     }
    622     /* Read the two transition, 16 data, and wire-idle bits. */
    623     for (i = 19; i > 0; i--) {
    624 	outl(MDIO_ENB_IN, mdio_addr);
    625 	mdio_delay();
    626 	retval = (retval << 1) | ((inl(mdio_addr) & MDIO_DATA_READ) ? 1 : 0);
    627 	outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
    628 	mdio_delay();
    629     }
    630     return (retval>>1) & 0xffff;
    631 }
    632 
    633 void mdio_write(struct nic *nic, int phy_id, int location, int value)
    634 {
    635     int i;
    636     int cmd = (0x5002 << 16) | (phy_id << 23) | (location<<18) | value;
    637     long mdio_addr = ioaddr + CSR9;
    638 
    639 #ifdef TULIP_DEBUG_WHERE
    640     whereami("mdio_write\n");
    641 #endif
    642 
    643     if (tp->chip_id == LC82C168) {
    644 	int i = 1000;
    645 	outl(cmd, ioaddr + 0xA0);
    646 	do
    647 	    if ( ! (inl(ioaddr + 0xA0) & 0x80000000))
    648 		break;
    649 	while (--i > 0);
    650 	return;
    651     }
    652 
    653     if (tp->chip_id == COMET) {
    654 	if (phy_id != 1)
    655 	    return;
    656 	if (location < 7)
    657 	    outl(value, ioaddr + 0xB4 + (location<<2));
    658 	else if (location == 17)
    659 	    outl(value, ioaddr + 0xD0);
    660 	else if (location >= 29 && location <= 31)
    661 	    outl(value, ioaddr + 0xD4 + ((location-29)<<2));
    662 	return;
    663     }
    664 
    665     /* Establish sync by sending 32 logic ones. */
    666     for (i = 32; i >= 0; i--) {
    667 	outl(MDIO_ENB | MDIO_DATA_WRITE1, mdio_addr);
    668 	mdio_delay();
    669 	outl(MDIO_ENB | MDIO_DATA_WRITE1 | MDIO_SHIFT_CLK, mdio_addr);
    670 	mdio_delay();
    671     }
    672     /* Shift the command bits out. */
    673     for (i = 31; i >= 0; i--) {
    674 	int dataval = (cmd & (1 << i)) ? MDIO_DATA_WRITE1 : 0;
    675 	outl(MDIO_ENB | dataval, mdio_addr);
    676 	mdio_delay();
    677 	outl(MDIO_ENB | dataval | MDIO_SHIFT_CLK, mdio_addr);
    678 	mdio_delay();
    679     }
    680     /* Clear out extra bits. */
    681     for (i = 2; i > 0; i--) {
    682 	outl(MDIO_ENB_IN, mdio_addr);
    683 	mdio_delay();
    684 	outl(MDIO_ENB_IN | MDIO_SHIFT_CLK, mdio_addr);
    685 	mdio_delay();
    686     }
    687 }
    688 
    689 
    690 /*********************************************************************/
    692 /* EEPROM Reading Code                                               */
    693 /*********************************************************************/
    694 /* EEPROM routines adapted from the Linux Tulip Code */
    695 /* Reading a serial EEPROM is a "bit" grungy, but we work our way
    696    through:->.
    697 */
    698 static int read_eeprom(unsigned long ioaddr, int location, int addr_len)
    699 {
    700     int i;
    701     unsigned short retval = 0;
    702     long ee_addr = ioaddr + CSR9;
    703     int read_cmd = location | EE_READ_CMD;
    704 
    705 #ifdef TULIP_DEBUG_WHERE
    706     whereami("read_eeprom\n");
    707 #endif
    708 
    709     outl(EE_ENB & ~EE_CS, ee_addr);
    710     outl(EE_ENB, ee_addr);
    711 
    712     /* Shift the read command bits out. */
    713     for (i = 4 + addr_len; i >= 0; i--) {
    714         short dataval = (read_cmd & (1 << i)) ? EE_DATA_WRITE : 0;
    715         outl(EE_ENB | dataval, ee_addr);
    716         eeprom_delay();
    717         outl(EE_ENB | dataval | EE_SHIFT_CLK, ee_addr);
    718         eeprom_delay();
    719     }
    720     outl(EE_ENB, ee_addr);
    721 
    722     for (i = 16; i > 0; i--) {
    723         outl(EE_ENB | EE_SHIFT_CLK, ee_addr);
    724         eeprom_delay();
    725         retval = (retval << 1) | ((inl(ee_addr) & EE_DATA_READ) ? 1 : 0);
    726         outl(EE_ENB, ee_addr);
    727         eeprom_delay();
    728     }
    729 
    730     /* Terminate the EEPROM access. */
    731     outl(EE_ENB & ~EE_CS, ee_addr);
    732     return retval;
    733 }
    734 
    735 
    736 /*********************************************************************/
    738 /* EEPROM Parsing Code                                               */
    739 /*********************************************************************/
    740 static void parse_eeprom(struct nic *nic)
    741 {
    742     unsigned char *p, *ee_data = tp->eeprom;
    743     int new_advertise = 0;
    744     int i;
    745 
    746 #ifdef TULIP_DEBUG_WHERE
    747     whereami("parse_eeprom\n");
    748 #endif
    749 
    750     tp->mtable = 0;
    751     /* Detect an old-style (SA only) EEPROM layout:
    752        memcmp(ee_data, ee_data+16, 8). */
    753     for (i = 0; i < 8; i ++)
    754         if (ee_data[i] != ee_data[16+i])
    755             break;
    756     if (i >= 8) {
    757         /* Do a fix-up based on the vendor half of the station address. */
    758         for (i = 0; eeprom_fixups[i].name; i++) {
    759             if (nic->node_addr[0] == eeprom_fixups[i].addr0
    760                 &&  nic->node_addr[1] == eeprom_fixups[i].addr1
    761                 &&  nic->node_addr[2] == eeprom_fixups[i].addr2) {
    762                 if (nic->node_addr[2] == 0xE8  &&  ee_data[0x1a] == 0x55)
    763                     i++;                /* An Accton EN1207, not an outlaw Maxtech. */
    764                 memcpy(ee_data + 26, eeprom_fixups[i].newtable,
    765                        sizeof(eeprom_fixups[i].newtable));
    766 #ifdef TULIP_DEBUG
    767                 printf("%s: Old format EEPROM on '%s' board.\n%s: Using substitute media control info.\n",
    768                        tp->nic_name, eeprom_fixups[i].name, tp->nic_name);
    769 #endif
    770                 break;
    771             }
    772         }
    773         if (eeprom_fixups[i].name == NULL) { /* No fixup found. */
    774 #ifdef TULIP_DEBUG
    775             printf("%s: Old style EEPROM with no media selection information.\n",
    776                    tp->nic_name);
    777 #endif
    778             return;
    779         }
    780     }
    781 
    782     if (ee_data[19] > 1) {
    783 #ifdef TULIP_DEBUG
    784         printf("%s:  Multiport cards (%d ports) may not work correctly.\n",
    785                tp->nic_name, ee_data[19]);
    786 #endif
    787     }
    788 
    789     p = (void *)ee_data + ee_data[27];
    790 
    791     if (ee_data[27] == 0) {             /* No valid media table. */
    792 #ifdef TULIP_DEBUG
    793         if (tulip_debug > 1) {
    794             printf("%s:  No Valid Media Table. ee_data[27] = %hhX\n",
    795                    tp->nic_name, ee_data[27]);
    796         }
    797 #endif
    798     } else if (tp->chip_id == DC21041) {
    799         int media = get_u16(p);
    800         int count = p[2];
    801         p += 3;
    802 
    803         printf("%s: 21041 Media table, default media %hX (%s).\n",
    804                tp->nic_name, media,
    805                media & 0x0800 ? "Autosense" : medianame[media & 15]);
    806         for (i = 0; i < count; i++) {
    807             unsigned char media_block = *p++;
    808             int media_code = media_block & MEDIA_MASK;
    809             if (media_block & 0x40)
    810                 p += 6;
    811             switch(media_code) {
    812             case 0: new_advertise |= 0x0020; break;
    813             case 4: new_advertise |= 0x0040; break;
    814             }
    815             printf("%s:  21041 media #%d, %s.\n",
    816                    tp->nic_name, media_code, medianame[media_code]);
    817         }
    818     } else {
    819         unsigned char csr12dir = 0;
    820         int count;
    821         struct mediatable *mtable;
    822         u16 media = get_u16(p);
    823 
    824         p += 2;
    825         if (tp->flags & CSR12_IN_SROM)
    826             csr12dir = *p++;
    827         count = *p++;
    828 
    829         tp->mtable = mtable = (struct mediatable *)&tp->media_table_storage[0];
    830 
    831         mtable->defaultmedia = media;
    832         mtable->leafcount = count;
    833         mtable->csr12dir = csr12dir;
    834         mtable->has_nonmii = mtable->has_mii = mtable->has_reset = 0;
    835         mtable->csr15dir = mtable->csr15val = 0;
    836 
    837         printf("%s:  EEPROM default media type %s.\n", tp->nic_name,
    838                media & 0x0800 ? "Autosense" : medianame[media & MEDIA_MASK]);
    839 
    840         for (i = 0; i < count; i++) {
    841             struct medialeaf *leaf = &mtable->mleaf[i];
    842 
    843             if ((p[0] & 0x80) == 0) { /* 21140 Compact block. */
    844                 leaf->type = 0;
    845                 leaf->media = p[0] & 0x3f;
    846                 leaf->leafdata = p;
    847                 if ((p[2] & 0x61) == 0x01)      /* Bogus, but Znyx boards do it. */
    848                     mtable->has_mii = 1;
    849                 p += 4;
    850             } else {
    851                 switch(leaf->type = p[1]) {
    852                 case 5:
    853                     mtable->has_reset = i;
    854                     leaf->media = p[2] & 0x0f;
    855                     break;
    856                 case 1: case 3:
    857                     mtable->has_mii = 1;
    858                     leaf->media = 11;
    859                     break;
    860                 case 2:
    861                     if ((p[2] & 0x3f) == 0) {
    862                         u32 base15 = (p[2] & 0x40) ? get_u16(p + 7) : 0x0008;
    863                         u16 *p1 = (u16 *)(p + (p[2] & 0x40 ? 9 : 3));
    864                         mtable->csr15dir = (get_unaligned(p1 + 0)<<16) + base15;
    865                         mtable->csr15val = (get_unaligned(p1 + 1)<<16) + base15;
    866                     }
    867                     /* Fall through. */
    868                 case 0: case 4:
    869                     mtable->has_nonmii = 1;
    870                     leaf->media = p[2] & MEDIA_MASK;
    871                     switch (leaf->media) {
    872                     case 0: new_advertise |= 0x0020; break;
    873                     case 4: new_advertise |= 0x0040; break;
    874                     case 3: new_advertise |= 0x0080; break;
    875                     case 5: new_advertise |= 0x0100; break;
    876                     case 6: new_advertise |= 0x0200; break;
    877                     }
    878                     break;
    879                 default:
    880                     leaf->media = 19;
    881                 }
    882                 leaf->leafdata = p + 2;
    883                 p += (p[0] & 0x3f) + 1;
    884             }
    885 #ifdef TULIP_DEBUG
    886             if (tulip_debug > 1  &&  leaf->media == 11) {
    887                 unsigned char *bp = leaf->leafdata;
    888                 printf("%s:  MII interface PHY %d, setup/reset sequences %d/%d long, capabilities %hhX %hhX.\n",
    889                        tp->nic_name, bp[0], bp[1], bp[2 + bp[1]*2],
    890                        bp[5 + bp[2 + bp[1]*2]*2], bp[4 + bp[2 + bp[1]*2]*2]);
    891             }
    892 #endif
    893             printf("%s:  Index #%d - Media %s (#%d) described "
    894                    "by a %s (%d) block.\n",
    895                    tp->nic_name, i, medianame[leaf->media], leaf->media,
    896                    leaf->type < 6 ? block_name[leaf->type] : "UNKNOWN",
    897                    leaf->type);
    898         }
    899         if (new_advertise)
    900             tp->sym_advertise = new_advertise;
    901     }
    902 }
    903 
    904 
    905 /*********************************************************************/
    907 /* tulip_init_ring - setup the tx and rx descriptors                */
    908 /*********************************************************************/
    909 static void tulip_init_ring(struct nic *nic)
    910 {
    911     int i;
    912 
    913 #ifdef TULIP_DEBUG_WHERE
    914     whereami("tulip_init_ring\n");
    915 #endif
    916 
    917     tp->cur_rx = 0;
    918 
    919     for (i = 0; i < RX_RING_SIZE; i++) {
    920 	rx_ring[i].status  = cpu_to_le32(0x80000000);
    921 	rx_ring[i].length  = cpu_to_le32(BUFLEN);
    922 	rx_ring[i].buffer1 = virt_to_le32desc(&rxb[i * BUFLEN]);
    923 	rx_ring[i].buffer2 = virt_to_le32desc(&rx_ring[i+1]);
    924     }
    925     /* Mark the last entry as wrapping the ring. */
    926     rx_ring[i-1].length    = cpu_to_le32(DESC_RING_WRAP | BUFLEN);
    927     rx_ring[i-1].buffer2   = virt_to_le32desc(&rx_ring[0]);
    928 
    929     /* We only use 1 transmit buffer, but we use 2 descriptors so
    930        transmit engines have somewhere to point to if they feel the need */
    931 
    932     tx_ring[0].status  = 0x00000000;
    933     tx_ring[0].buffer1 = virt_to_le32desc(&txb[0]);
    934     tx_ring[0].buffer2 = virt_to_le32desc(&tx_ring[1]);
    935 
    936     /* this descriptor should never get used, since it will never be owned
    937        by the machine (status will always == 0) */
    938     tx_ring[1].status  = 0x00000000;
    939     tx_ring[1].buffer1 = virt_to_le32desc(&txb[0]);
    940     tx_ring[1].buffer2 = virt_to_le32desc(&tx_ring[0]);
    941 
    942     /* Mark the last entry as wrapping the ring, though this should never happen */
    943     tx_ring[1].length  = cpu_to_le32(DESC_RING_WRAP | BUFLEN);
    944 }
    945 
    946 /*********************************************************************/
    948 /* eth_reset - Reset adapter                                         */
    949 /*********************************************************************/
    950 static void tulip_reset(struct nic *nic)
    951 {
    952     int i;
    953     unsigned long to;
    954     u32 addr_low, addr_high;
    955 
    956 #ifdef TULIP_DEBUG_WHERE
    957     whereami("tulip_reset\n");
    958 #endif
    959 
    960     /* Stop Tx and RX */
    961     outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
    962 
    963     /* On some chip revs we must set the MII/SYM port before the reset!? */
    964     if (tp->mii_cnt  ||  (tp->mtable  &&  tp->mtable->has_mii)) {
    965 	outl(0x814C0000, ioaddr + CSR6);
    966     }
    967 
    968     /* Reset the chip, holding bit 0 set at least 50 PCI cycles. */
    969     outl(0x00000001, ioaddr + CSR0);
    970     tulip_wait(1);
    971 
    972     /* turn off reset and set cache align=16lword, burst=unlimit */
    973     outl(tp->csr0, ioaddr + CSR0);
    974 
    975     /*  Wait the specified 50 PCI cycles after a reset */
    976     tulip_wait(1);
    977 
    978     /* set up transmit and receive descriptors */
    979     tulip_init_ring(nic);
    980 
    981     if (tp->chip_id == PNIC2) {
    982         u32 addr_high = (nic->node_addr[1]<<8) + (nic->node_addr[0]<<0);
    983         /* This address setting does not appear to impact chip operation?? */
    984         outl((nic->node_addr[5]<<8) + nic->node_addr[4] +
    985              (nic->node_addr[3]<<24) + (nic->node_addr[2]<<16),
    986              ioaddr + 0xB0);
    987         outl(addr_high + (addr_high<<16), ioaddr + 0xB8);
    988     }
    989 
    990     /* MC_HASH_ONLY boards don't support setup packets */
    991     if (tp->flags & MC_HASH_ONLY) {
    992         u32 addr_low = cpu_to_le32(get_unaligned((u32 *)nic->node_addr));
    993         u32 addr_high = cpu_to_le32(get_unaligned((u16 *)(nic->node_addr+4)));
    994 
    995 	/* clear multicast hash filters and setup MAC address filters */
    996 	if (tp->flags & IS_ASIX) {
    997             outl(0, ioaddr + CSR13);
    998             outl(addr_low,  ioaddr + CSR14);
    999             outl(1, ioaddr + CSR13);
   1000             outl(addr_high, ioaddr + CSR14);
   1001 	    outl(2, ioaddr + CSR13);
   1002 	    outl(0, ioaddr + CSR14);
   1003 	    outl(3, ioaddr + CSR13);
   1004 	    outl(0, ioaddr + CSR14);
   1005 	} else if (tp->chip_id == COMET) {
   1006             outl(addr_low,  ioaddr + 0xA4);
   1007             outl(addr_high, ioaddr + 0xA8);
   1008             outl(0, ioaddr + 0xAC);
   1009             outl(0, ioaddr + 0xB0);
   1010 	}
   1011     } else {
   1012 	/* for other boards we send a setup packet to initialize
   1013 	   the filters */
   1014 	u32 tx_flags = 0x08000000 | 192;
   1015 
   1016 	/* construct perfect filter frame with mac address as first match
   1017 	   and broadcast address for all others */
   1018 	for (i=0; i<192; i++)
   1019 	    txb[i] = 0xFF;
   1020 	txb[0] = nic->node_addr[0];
   1021 	txb[1] = nic->node_addr[1];
   1022 	txb[4] = nic->node_addr[2];
   1023 	txb[5] = nic->node_addr[3];
   1024 	txb[8] = nic->node_addr[4];
   1025 	txb[9] = nic->node_addr[5];
   1026 
   1027 	tx_ring[0].length  = cpu_to_le32(tx_flags);
   1028 	tx_ring[0].buffer1 = virt_to_le32desc(&txb[0]);
   1029 	tx_ring[0].status  = cpu_to_le32(0x80000000);
   1030     }
   1031 
   1032     /* Point to rx and tx descriptors */
   1033     outl((unsigned long)&rx_ring[0], ioaddr + CSR3);
   1034     outl((unsigned long)&tx_ring[0], ioaddr + CSR4);
   1035 
   1036     init_media(nic);
   1037 
   1038     /* set the chip's operating mode (but don't turn on xmit and recv yet) */
   1039     outl((tp->csr6 & ~0x00002002), ioaddr + CSR6);
   1040 
   1041     /* send setup packet for cards that support it */
   1042     if (!(tp->flags & MC_HASH_ONLY)) {
   1043 	/* enable transmit  wait for completion */
   1044 	outl(tp->csr6 | 0x00002000, ioaddr + CSR6);
   1045 	/* immediate transmit demand */
   1046 	outl(0, ioaddr + CSR1);
   1047 
   1048 	to = currticks() + TX_TIME_OUT;
   1049 	while ((tx_ring[0].status & 0x80000000) && (currticks() < to))
   1050 	    /* wait */ ;
   1051 
   1052 	if (currticks() >= to) {
   1053 	    printf ("%s: TX Setup Timeout.\n", tp->nic_name);
   1054 	}
   1055     }
   1056 
   1057     if (tp->chip_id == LC82C168)
   1058 	tulip_check_duplex(nic);
   1059 
   1060     /* enable transmit and receive */
   1061     outl(tp->csr6 | 0x00002002, ioaddr + CSR6);
   1062 }
   1063 
   1064 
   1065 /*********************************************************************/
   1067 /* eth_transmit - Transmit a frame                                   */
   1068 /*********************************************************************/
   1069 static void tulip_transmit(struct nic *nic, const char *d, unsigned int t,
   1070                            unsigned int s, const char *p)
   1071 {
   1072     u16 nstype;
   1073     u32 to;
   1074     u32 csr6 = inl(ioaddr + CSR6);
   1075 
   1076 #ifdef TULIP_DEBUG_WHERE
   1077     whereami("tulip_transmit\n");
   1078 #endif
   1079 
   1080     /* Disable Tx */
   1081     outl(csr6 & ~0x00002000, ioaddr + CSR6);
   1082 
   1083     memcpy(txb, d, ETH_ALEN);
   1084     memcpy(txb + ETH_ALEN, nic->node_addr, ETH_ALEN);
   1085     nstype = htons((u16) t);
   1086     memcpy(txb + 2 * ETH_ALEN, (u8 *)&nstype, 2);
   1087     memcpy(txb + ETH_HLEN, p, s);
   1088 
   1089     s += ETH_HLEN;
   1090     s &= 0x0FFF;
   1091 
   1092     /* pad to minimum packet size */
   1093     while (s < ETH_ZLEN)
   1094         txb[s++] = '\0';
   1095 
   1096 #ifdef TULIP_DEBUG
   1097     if (tulip_debug > 1)
   1098 	printf("%s: sending %d bytes ethtype %hX\n", tp->nic_name, s, t);
   1099 #endif
   1100 
   1101     /* setup the transmit descriptor */
   1102     /* 0x60000000 = no interrupt on completion */
   1103     tx_ring[0].length = cpu_to_le32(0x60000000 | s);
   1104     tx_ring[0].status = cpu_to_le32(0x80000000);
   1105 
   1106     /* Point to transmit descriptor */
   1107     outl((u32)&tx_ring[0], ioaddr + CSR4);
   1108 
   1109     /* Enable Tx */
   1110     outl(csr6 | 0x00002000, ioaddr + CSR6);
   1111     /* immediate transmit demand */
   1112     outl(0, ioaddr + CSR1);
   1113 
   1114     to = currticks() + TX_TIME_OUT;
   1115     while ((tx_ring[0].status & 0x80000000) && (currticks() < to))
   1116         /* wait */ ;
   1117 
   1118     if (currticks() >= to) {
   1119         printf ("TX Timeout!\n");
   1120     }
   1121 
   1122     /* Disable Tx */
   1123     outl(csr6 & ~0x00002000, ioaddr + CSR6);
   1124 }
   1125 
   1126 /*********************************************************************/
   1128 /* eth_poll - Wait for a frame                                       */
   1129 /*********************************************************************/
   1130 static int tulip_poll(struct nic *nic)
   1131 {
   1132 
   1133 #ifdef TULIP_DEBUG_WHERE
   1134     whereami("tulip_poll\n");
   1135 #endif
   1136 
   1137     /* no packet waiting. packet still owned by NIC */
   1138     if (rx_ring[tp->cur_rx].status & 0x80000000)
   1139         return 0;
   1140 
   1141 #ifdef TULIP_DEBUG_WHERE
   1142     whereami("tulip_poll got one\n");
   1143 #endif
   1144 
   1145     nic->packetlen = (rx_ring[tp->cur_rx].status & 0x3FFF0000) >> 16;
   1146 
   1147     /* if we get a corrupted packet. throw it away and move on */
   1148     if (rx_ring[tp->cur_rx].status & 0x00008000) {
   1149 	/* return the descriptor and buffer to receive ring */
   1150         rx_ring[tp->cur_rx].status = 0x80000000;
   1151 	tp->cur_rx = (++tp->cur_rx) % RX_RING_SIZE;
   1152         return 0;
   1153     }
   1154 
   1155     /* copy packet to working buffer */
   1156     memcpy(nic->packet, rxb + tp->cur_rx * BUFLEN, nic->packetlen);
   1157 
   1158     /* return the descriptor and buffer to receive ring */
   1159     rx_ring[tp->cur_rx].status = 0x80000000;
   1160     tp->cur_rx = (++tp->cur_rx) % RX_RING_SIZE;
   1161 
   1162     return 1;
   1163 }
   1164 
   1165 /*********************************************************************/
   1167 /* eth_disable - Disable the interface                               */
   1168 /*********************************************************************/
   1169 static void tulip_disable(struct nic *nic)
   1170 {
   1171 
   1172 #ifdef TULIP_DEBUG_WHERE
   1173     whereami("tulip_disable\n");
   1174 #endif
   1175 
   1176     /* disable interrupts */
   1177     outl(0x00000000, ioaddr + CSR7);
   1178 
   1179     /* Stop the chip's Tx and Rx processes. */
   1180     outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
   1181 
   1182     /* Clear the missed-packet counter. */
   1183     (volatile unsigned long)inl(ioaddr + CSR8);
   1184 }
   1185 
   1186 /*********************************************************************/
   1188 /* eth_probe - Look for an adapter                                   */
   1189 /*********************************************************************/
   1190 struct nic *tulip_probe(struct nic *nic, unsigned short *io_addrs,
   1191                         struct pci_device *pci)
   1192 {
   1193     u32 i, l1, l2;
   1194     u8  chip_rev;
   1195     u8 ee_data[EEPROM_SIZE];
   1196     unsigned short sum;
   1197     int chip_idx;
   1198     static unsigned char last_phys_addr[ETH_ALEN] = {0x00, 'L', 'i', 'n', 'u', 'x'};
   1199 
   1200     if (io_addrs == 0 || *io_addrs == 0)
   1201         return 0;
   1202 
   1203     ioaddr         = *io_addrs;
   1204 
   1205     /* point to private storage */
   1206     tp = &tpx;
   1207 
   1208     tp->vendor_id  = pci->vendor;
   1209     tp->dev_id     = pci->dev_id;
   1210     tp->nic_name   = pci->name;
   1211 
   1212     tp->if_port = 0;
   1213     tp->default_port = 0;
   1214 
   1215     adjust_pci_device(pci);
   1216 
   1217     /* disable interrupts */
   1218     outl(0x00000000, ioaddr + CSR7);
   1219 
   1220     /* Stop the chip's Tx and Rx processes. */
   1221     outl(inl(ioaddr + CSR6) & ~0x00002002, ioaddr + CSR6);
   1222 
   1223     /* Clear the missed-packet counter. */
   1224     (volatile unsigned long)inl(ioaddr + CSR8);
   1225 
   1226     printf("\n");                /* so we start on a fresh line */
   1227 #ifdef TULIP_DEBUG_WHERE
   1228     whereami("tulip_probe\n");
   1229 #endif
   1230 
   1231 #ifdef TULIP_DEBUG
   1232     if (tulip_debug > 1)
   1233 	printf ("%s: Looking for Tulip Chip: Vendor=%hX  Device=%hX\n", tp->nic_name,
   1234 		tp->vendor_id, tp->dev_id);
   1235 #endif
   1236 
   1237     /* Figure out which chip we're dealing with */
   1238     i = 0;
   1239     chip_idx = -1;
   1240 
   1241     while (pci_id_tbl[i].name) {
   1242         if ( (((u32) tp->dev_id << 16) | tp->vendor_id) ==
   1243              (pci_id_tbl[i].id.pci & pci_id_tbl[i].id.pci_mask) ) {
   1244             chip_idx = pci_id_tbl[i].drv_flags;
   1245             break;
   1246         }
   1247         i++;
   1248     }
   1249 
   1250     if (chip_idx == -1) {
   1251         printf ("%s: Unknown Tulip Chip: Vendor=%hX  Device=%hX\n", tp->nic_name,
   1252                 tp->vendor_id, tp->dev_id);
   1253         return 0;
   1254     }
   1255 
   1256     tp->pci_id_idx = i;
   1257     tp->flags = tulip_tbl[chip_idx].flags;
   1258 
   1259 #ifdef TULIP_DEBUG
   1260     if (tulip_debug > 1) {
   1261 	printf ("%s: tp->pci_id_idx == %d,  name == %s\n", tp->nic_name,
   1262 		tp->pci_id_idx, pci_id_tbl[tp->pci_id_idx].name);
   1263 	printf ("%s: chip_idx == %d, name == %s\n", tp->nic_name, chip_idx,
   1264 		tulip_tbl[chip_idx].chip_name);
   1265     }
   1266 #endif
   1267 
   1268     /* Bring the 21041/21143 out of sleep mode.
   1269        Caution: Snooze mode does not work with some boards! */
   1270     if (tp->flags & HAS_PWRDWN)
   1271         pcibios_write_config_dword(pci->bus, pci->devfn, 0x40, 0x00000000);
   1272 
   1273     if (inl(ioaddr + CSR5) == 0xFFFFFFFF) {
   1274         printf("%s: The Tulip chip at %X is not functioning.\n",
   1275                tp->nic_name, ioaddr);
   1276         return 0;
   1277     }
   1278 
   1279     pcibios_read_config_byte(pci->bus, pci->devfn, PCI_REVISION, &chip_rev);
   1280 
   1281     printf("%s: [chip: %s] rev %d at %hX\n", tp->nic_name,
   1282            tulip_tbl[chip_idx].chip_name, chip_rev, ioaddr);
   1283     printf("%s: Vendor=%hX  Device=%hX", tp->nic_name, tp->vendor_id, tp->dev_id);
   1284 
   1285     if (chip_idx == DC21041  &&  inl(ioaddr + CSR9) & 0x8000) {
   1286         printf(" 21040 compatible mode.");
   1287         chip_idx = DC21040;
   1288     }
   1289 
   1290     printf("\n");
   1291 
   1292     /* The SROM/EEPROM interface varies dramatically. */
   1293     sum = 0;
   1294     if (chip_idx == DC21040) {
   1295         outl(0, ioaddr + CSR9);         /* Reset the pointer with a dummy write. */
   1296         for (i = 0; i < ETH_ALEN; i++) {
   1297             int value, boguscnt = 100000;
   1298             do
   1299                 value = inl(ioaddr + CSR9);
   1300             while (value < 0  && --boguscnt > 0);
   1301             nic->node_addr[i] = value;
   1302             sum += value & 0xff;
   1303         }
   1304     } else if (chip_idx == LC82C168) {
   1305         for (i = 0; i < 3; i++) {
   1306             int value, boguscnt = 100000;
   1307             outl(0x600 | i, ioaddr + 0x98);
   1308             do
   1309                 value = inl(ioaddr + CSR9);
   1310             while (value < 0  && --boguscnt > 0);
   1311             put_unaligned(le16_to_cpu(value), ((u16*)nic->node_addr) + i);
   1312             sum += value & 0xffff;
   1313         }
   1314     } else if (chip_idx == COMET) {
   1315         /* No need to read the EEPROM. */
   1316         put_unaligned(inl(ioaddr + 0xA4), (u32 *)nic->node_addr);
   1317         put_unaligned(inl(ioaddr + 0xA8), (u16 *)(nic->node_addr + 4));
   1318         for (i = 0; i < ETH_ALEN; i ++)
   1319             sum += nic->node_addr[i];
   1320     } else {
   1321         /* A serial EEPROM interface, we read now and sort it out later. */
   1322         int sa_offset = 0;
   1323         int ee_addr_size = read_eeprom(ioaddr, 0xff, 8) & 0x40000 ? 8 : 6;
   1324 
   1325         for (i = 0; i < sizeof(ee_data)/2; i++)
   1326             ((u16 *)ee_data)[i] =
   1327                 le16_to_cpu(read_eeprom(ioaddr, i, ee_addr_size));
   1328 
   1329         /* DEC now has a specification (see Notes) but early board makers
   1330            just put the address in the first EEPROM locations. */
   1331         /* This does  memcmp(eedata, eedata+16, 8) */
   1332         for (i = 0; i < 8; i ++)
   1333             if (ee_data[i] != ee_data[16+i])
   1334                 sa_offset = 20;
   1335         if (ee_data[0] == 0xff  &&  ee_data[1] == 0xff &&  ee_data[2] == 0) {
   1336             sa_offset = 2;              /* Grrr, damn Matrox boards. */
   1337         }
   1338         for (i = 0; i < ETH_ALEN; i ++) {
   1339             nic->node_addr[i] = ee_data[i + sa_offset];
   1340             sum += ee_data[i + sa_offset];
   1341         }
   1342     }
   1343     /* Lite-On boards have the address byte-swapped. */
   1344     if ((nic->node_addr[0] == 0xA0  ||  nic->node_addr[0] == 0xC0)
   1345         &&  nic->node_addr[1] == 0x00)
   1346         for (i = 0; i < ETH_ALEN; i+=2) {
   1347             char tmp = nic->node_addr[i];
   1348             nic->node_addr[i] = nic->node_addr[i+1];
   1349             nic->node_addr[i+1] = tmp;
   1350         }
   1351 
   1352     if (sum == 0  || sum == ETH_ALEN*0xff) {
   1353         printf("%s: EEPROM not present!\n", tp->nic_name);
   1354         for (i = 0; i < ETH_ALEN-1; i++)
   1355             nic->node_addr[i] = last_phys_addr[i];
   1356         nic->node_addr[i] = last_phys_addr[i] + 1;
   1357     }
   1358 
   1359     for (i = 0; i < ETH_ALEN; i++)
   1360         last_phys_addr[i] = nic->node_addr[i];
   1361 
   1362     printf("%s: %! at ioaddr %hX\n", tp->nic_name, nic->node_addr, ioaddr);
   1363 
   1364     tp->chip_id = chip_idx;
   1365     tp->revision = chip_rev;
   1366     tp->csr0 = csr0;
   1367 
   1368     /* BugFixes: The 21143-TD hangs with PCI Write-and-Invalidate cycles.
   1369        And the ASIX must have a burst limit or horrible things happen. */
   1370     if (chip_idx == DC21143  &&  chip_rev == 65)
   1371         tp->csr0 &= ~0x01000000;
   1372     else if (tp->flags & IS_ASIX)
   1373         tp->csr0 |= 0x2000;
   1374 
   1375     if (media_cap[tp->default_port] & MediaIsMII) {
   1376         u16 media2advert[] = { 0x20, 0x40, 0x03e0, 0x60, 0x80, 0x100, 0x200 };
   1377         tp->mii_advertise = media2advert[tp->default_port - 9];
   1378         tp->mii_advertise |= (tp->flags & HAS_8023X); /* Matching bits! */
   1379     }
   1380 
   1381     /* This is logically part of the probe routine, but too complex
   1382        to write inline. */
   1383     if (tp->flags & HAS_MEDIA_TABLE) {
   1384         memcpy(tp->eeprom, ee_data, sizeof(tp->eeprom));
   1385         parse_eeprom(nic);
   1386     }
   1387 
   1388     start_link(nic);
   1389 
   1390     /* reset the device and make ready for tx and rx of packets */
   1391     tulip_reset(nic);
   1392 
   1393     nic->reset    = tulip_reset;
   1394     nic->poll     = tulip_poll;
   1395     nic->transmit = tulip_transmit;
   1396     nic->disable  = tulip_disable;
   1397 
   1398     /* give the board a chance to reset before returning */
   1399     tulip_wait(4*TICKS_PER_SEC);
   1400 
   1401     return nic;
   1402 }
   1403 
   1404 static void start_link(struct nic *nic)
   1405 {
   1406     int i;
   1407 
   1408 #ifdef TULIP_DEBUG_WHERE
   1409     whereami("start_link\n");
   1410 #endif
   1411 
   1412     if ((tp->flags & ALWAYS_CHECK_MII) ||
   1413         (tp->mtable  &&  tp->mtable->has_mii) ||
   1414         ( ! tp->mtable  &&  (tp->flags & HAS_MII))) {
   1415         unsigned int phy, phy_idx;
   1416         if (tp->mtable  &&  tp->mtable->has_mii) {
   1417             for (i = 0; i < tp->mtable->leafcount; i++)
   1418                 if (tp->mtable->mleaf[i].media == 11) {
   1419                     tp->cur_index = i;
   1420                     tp->saved_if_port = tp->if_port;
   1421                     select_media(nic, 2);
   1422                     tp->if_port = tp->saved_if_port;
   1423                     break;
   1424                 }
   1425         }
   1426 
   1427         /* Find the connected MII xcvrs. */
   1428         for (phy = 0, phy_idx = 0; phy < 32 && phy_idx < sizeof(tp->phys);
   1429              phy++) {
   1430             int mii_status = mdio_read(nic, phy, 1);
   1431             if ((mii_status & 0x8301) == 0x8001 ||
   1432                 ((mii_status & 0x8000) == 0  && (mii_status & 0x7800) != 0)) {
   1433                 int mii_reg0 = mdio_read(nic, phy, 0);
   1434                 int mii_advert = mdio_read(nic, phy, 4);
   1435                 int to_advert;
   1436 
   1437                 if (tp->mii_advertise)
   1438                     to_advert = tp->mii_advertise;
   1439                 else if (tp->advertising[phy_idx])
   1440                     to_advert = tp->advertising[phy_idx];
   1441                 else                    /* Leave unchanged. */
   1442                     tp->mii_advertise = to_advert = mii_advert;
   1443 
   1444                 tp->phys[phy_idx++] = phy;
   1445                 printf("%s:  MII transceiver %d config %hX status %hX advertising %hX.\n",
   1446                        tp->nic_name, phy, mii_reg0, mii_status, mii_advert);
   1447                                 /* Fixup for DLink with miswired PHY. */
   1448                 if (mii_advert != to_advert) {
   1449                     printf("%s:  Advertising %hX on PHY %d previously advertising %hX.\n",
   1450                            tp->nic_name, to_advert, phy, mii_advert);
   1451                     mdio_write(nic, phy, 4, to_advert);
   1452                 }
   1453                                 /* Enable autonegotiation: some boards default to off. */
   1454                 mdio_write(nic, phy, 0, mii_reg0 |
   1455                            (tp->full_duplex ? 0x1100 : 0x1000) |
   1456                            (media_cap[tp->default_port]&MediaIs100 ? 0x2000:0));
   1457             }
   1458         }
   1459         tp->mii_cnt = phy_idx;
   1460         if (tp->mtable  &&  tp->mtable->has_mii  &&  phy_idx == 0) {
   1461             printf("%s: ***WARNING***: No MII transceiver found!\n",
   1462                    tp->nic_name);
   1463             tp->phys[0] = 1;
   1464         }
   1465     }
   1466 
   1467     /* Reset the xcvr interface and turn on heartbeat. */
   1468     switch (tp->chip_id) {
   1469     case DC21040:
   1470         outl(0x00000000, ioaddr + CSR13);
   1471         outl(0x00000004, ioaddr + CSR13);
   1472         break;
   1473     case DC21041:
   1474         /* This is nway_start(). */
   1475         if (tp->sym_advertise == 0)
   1476             tp->sym_advertise = 0x0061;
   1477         outl(0x00000000, ioaddr + CSR13);
   1478         outl(0xFFFFFFFF, ioaddr + CSR14);
   1479         outl(0x00000008, ioaddr + CSR15); /* Listen on AUI also. */
   1480         outl(inl(ioaddr + CSR6) | 0x0200, ioaddr + CSR6);
   1481         outl(0x0000EF01, ioaddr + CSR13);
   1482         break;
   1483     case DC21140: default:
   1484         if (tp->mtable)
   1485             outl(tp->mtable->csr12dir | 0x100, ioaddr + CSR12);
   1486         break;
   1487     case DC21142:
   1488     case PNIC2:
   1489         if (tp->mii_cnt  ||  media_cap[tp->if_port] & MediaIsMII) {
   1490             outl(0x82020000, ioaddr + CSR6);
   1491             outl(0x0000, ioaddr + CSR13);
   1492             outl(0x0000, ioaddr + CSR14);
   1493             outl(0x820E0000, ioaddr + CSR6);
   1494         } else
   1495             nway_start(nic);
   1496         break;
   1497     case LC82C168:
   1498         if ( ! tp->mii_cnt) {
   1499             tp->nway = 1;
   1500             tp->nwayset = 0;
   1501             outl(0x00420000, ioaddr + CSR6);
   1502             outl(0x30, ioaddr + CSR12);
   1503             outl(0x0001F078, ioaddr + 0xB8);
   1504             outl(0x0201F078, ioaddr + 0xB8); /* Turn on autonegotiation. */
   1505         }
   1506         break;
   1507     case MX98713: case COMPEX9881:
   1508         outl(0x00000000, ioaddr + CSR6);
   1509         outl(0x000711C0, ioaddr + CSR14); /* Turn on NWay. */
   1510         outl(0x00000001, ioaddr + CSR13);
   1511         break;
   1512     case MX98715: case MX98725:
   1513         outl(0x01a80000, ioaddr + CSR6);
   1514         outl(0xFFFFFFFF, ioaddr + CSR14);
   1515         outl(0x00001000, ioaddr + CSR12);
   1516         break;
   1517     case COMET:
   1518         /* No initialization necessary. */
   1519         break;
   1520     }
   1521 }
   1522 
   1523 static void nway_start(struct nic *nic)
   1524 {
   1525     int csr14 = ((tp->sym_advertise & 0x0780) << 9)  |
   1526         ((tp->sym_advertise&0x0020)<<1) | 0xffbf;
   1527 
   1528 #ifdef TULIP_DEBUG_WHERE
   1529     whereami("nway_start\n");
   1530 #endif
   1531 
   1532     tp->if_port = 0;
   1533     tp->nway = tp->mediasense = 1;
   1534     tp->nwayset = tp->lpar = 0;
   1535     if (tp->chip_id == PNIC2) {
   1536         tp->csr6 = 0x01000000 | (tp->sym_advertise & 0x0040 ? 0x0200 : 0);
   1537         return;
   1538     }
   1539 #ifdef TULIP_DEBUG
   1540     if (tulip_debug > 1)
   1541         printf("%s: Restarting internal NWay autonegotiation, %X.\n",
   1542                tp->nic_name, csr14);
   1543 #endif
   1544     outl(0x0001, ioaddr + CSR13);
   1545     outl(csr14, ioaddr + CSR14);
   1546     tp->csr6 = 0x82420000 | (tp->sym_advertise & 0x0040 ? 0x0200 : 0);
   1547     outl(tp->csr6, ioaddr + CSR6);
   1548     if (tp->mtable  &&  tp->mtable->csr15dir) {
   1549         outl(tp->mtable->csr15dir, ioaddr + CSR15);
   1550         outl(tp->mtable->csr15val, ioaddr + CSR15);
   1551     } else if (tp->chip_id != PNIC2)
   1552         outw(0x0008, ioaddr + CSR15);
   1553     if (tp->chip_id == DC21041)                 /* Trigger NWAY. */
   1554         outl(0xEF01, ioaddr + CSR12);
   1555     else
   1556         outl(0x1301, ioaddr + CSR12);
   1557 }
   1558 
   1559 static void init_media(struct nic *nic)
   1560 {
   1561     int i;
   1562 
   1563 #ifdef TULIP_DEBUG_WHERE
   1564     whereami("init_media\n");
   1565 #endif
   1566 
   1567     tp->saved_if_port = tp->if_port;
   1568     if (tp->if_port == 0)
   1569         tp->if_port = tp->default_port;
   1570 
   1571     /* Allow selecting a default media. */
   1572     i = 0;
   1573     if (tp->mtable == NULL)
   1574         goto media_picked;
   1575     if (tp->if_port) {
   1576         int looking_for = media_cap[tp->if_port] & MediaIsMII ? 11 :
   1577             (tp->if_port == 12 ? 0 : tp->if_port);
   1578         for (i = 0; i < tp->mtable->leafcount; i++)
   1579             if (tp->mtable->mleaf[i].media == looking_for) {
   1580                 printf("%s: Using user-specified media %s.\n",
   1581                        tp->nic_name, medianame[tp->if_port]);
   1582                 goto media_picked;
   1583             }
   1584     }
   1585     if ((tp->mtable->defaultmedia & 0x0800) == 0) {
   1586         int looking_for = tp->mtable->defaultmedia & 15;
   1587         for (i = 0; i < tp->mtable->leafcount; i++)
   1588             if (tp->mtable->mleaf[i].media == looking_for) {
   1589                 printf("%s: Using EEPROM-set media %s.\n",
   1590                        tp->nic_name, medianame[looking_for]);
   1591                 goto media_picked;
   1592             }
   1593     }
   1594     /* Start sensing first non-full-duplex media. */
   1595     for (i = tp->mtable->leafcount - 1;
   1596          (media_cap[tp->mtable->mleaf[i].media] & MediaAlwaysFD) && i > 0; i--)
   1597         ;
   1598  media_picked:
   1599 
   1600     tp->csr6 = 0;
   1601     tp->cur_index = i;
   1602     tp->nwayset = 0;
   1603 
   1604     if (tp->if_port) {
   1605         if (tp->chip_id == DC21143  &&  media_cap[tp->if_port] & MediaIsMII) {
   1606             /* We must reset the media CSRs when we force-select MII mode. */
   1607             outl(0x0000, ioaddr + CSR13);
   1608             outl(0x0000, ioaddr + CSR14);
   1609             outl(0x0008, ioaddr + CSR15);
   1610         }
   1611         select_media(nic, 1);
   1612         return;
   1613     }
   1614     switch(tp->chip_id) {
   1615     case DC21041:
   1616         /* tp->nway = 1;*/
   1617         nway_start(nic);
   1618         break;
   1619     case DC21142:
   1620         if (tp->mii_cnt) {
   1621             select_media(nic, 1);
   1622 #ifdef TULIP_DEBUG
   1623             if (tulip_debug > 1)
   1624                 printf("%s: Using MII transceiver %d, status %hX.\n",
   1625                        tp->nic_name, tp->phys[0], mdio_read(nic, tp->phys[0], 1));
   1626 #endif
   1627             outl(0x82020000, ioaddr + CSR6);
   1628             tp->csr6 = 0x820E0000;
   1629             tp->if_port = 11;
   1630             outl(0x0000, ioaddr + CSR13);
   1631             outl(0x0000, ioaddr + CSR14);
   1632         } else
   1633             nway_start(nic);
   1634         break;
   1635     case PNIC2:
   1636         nway_start(nic);
   1637         break;
   1638     case LC82C168:
   1639         if (tp->mii_cnt) {
   1640             tp->if_port = 11;
   1641             tp->csr6 = 0x814C0000 | (tp->full_duplex ? 0x0200 : 0);
   1642             outl(0x0001, ioaddr + CSR15);
   1643         } else if (inl(ioaddr + CSR5) & TPLnkPass)
   1644             pnic_do_nway(nic);
   1645         else {
   1646             /* Start with 10mbps to do autonegotiation. */
   1647             outl(0x32, ioaddr + CSR12);
   1648             tp->csr6 = 0x00420000;
   1649             outl(0x0001B078, ioaddr + 0xB8);
   1650             outl(0x0201B078, ioaddr + 0xB8);
   1651         }
   1652         break;
   1653     case MX98713: case COMPEX9881:
   1654         tp->if_port = 0;
   1655         tp->csr6 = 0x01880000 | (tp->full_duplex ? 0x0200 : 0);
   1656         outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
   1657         break;
   1658     case MX98715: case MX98725:
   1659         /* Provided by BOLO, Macronix - 12/10/1998. */
   1660         tp->if_port = 0;
   1661         tp->csr6 = 0x01a80200;
   1662         outl(0x0f370000 | inw(ioaddr + 0x80), ioaddr + 0x80);
   1663         outl(0x11000 | inw(ioaddr + 0xa0), ioaddr + 0xa0);
   1664         break;
   1665     case COMET:
   1666         tp->if_port = 0;
   1667 	tp->csr6 = 0x00040000;
   1668         break;
   1669     case AX88140: case AX88141:
   1670         tp->csr6 = tp->mii_cnt ? 0x00040100 : 0x00000100;
   1671         break;
   1672     default:
   1673         select_media(nic, 1);
   1674     }
   1675 }
   1676 
   1677 static void pnic_do_nway(struct nic *nic)
   1678 {
   1679     u32 phy_reg = inl(ioaddr + 0xB8);
   1680     u32 new_csr6 = tp->csr6 & ~0x40C40200;
   1681 
   1682 #ifdef TULIP_DEBUG_WHERE
   1683     whereami("pnic_do_nway\n");
   1684 #endif
   1685 
   1686     if (phy_reg & 0x78000000) { /* Ignore baseT4 */
   1687         if (phy_reg & 0x20000000)               tp->if_port = 5;
   1688         else if (phy_reg & 0x40000000)  tp->if_port = 3;
   1689         else if (phy_reg & 0x10000000)  tp->if_port = 4;
   1690         else if (phy_reg & 0x08000000)  tp->if_port = 0;
   1691         tp->nwayset = 1;
   1692         new_csr6 = (tp->if_port & 1) ? 0x01860000 : 0x00420000;
   1693         outl(0x32 | (tp->if_port & 1), ioaddr + CSR12);
   1694         if (tp->if_port & 1)
   1695             outl(0x1F868, ioaddr + 0xB8);
   1696         if (phy_reg & 0x30000000) {
   1697             tp->full_duplex = 1;
   1698             new_csr6 |= 0x00000200;
   1699         }
   1700 #ifdef TULIP_DEBUG
   1701         if (tulip_debug > 1)
   1702             printf("%s: PNIC autonegotiated status %X, %s.\n",
   1703                    tp->nic_name, phy_reg, medianame[tp->if_port]);
   1704 #endif
   1705         if (tp->csr6 != new_csr6) {
   1706             tp->csr6 = new_csr6;
   1707             outl(tp->csr6 | 0x0002, ioaddr + CSR6);     /* Restart Tx */
   1708             outl(tp->csr6 | 0x2002, ioaddr + CSR6);
   1709         }
   1710     }
   1711 }
   1712 
   1713 /* Set up the transceiver control registers for the selected media type. */
   1714 static void select_media(struct nic *nic, int startup)
   1715 {
   1716     struct mediatable *mtable = tp->mtable;
   1717     u32 new_csr6;
   1718     int i;
   1719 
   1720 #ifdef TULIP_DEBUG_WHERE
   1721     whereami("select_media\n");
   1722 #endif
   1723 
   1724     if (mtable) {
   1725         struct medialeaf *mleaf = &mtable->mleaf[tp->cur_index];
   1726         unsigned char *p = mleaf->leafdata;
   1727         switch (mleaf->type) {
   1728         case 0:                                 /* 21140 non-MII xcvr. */
   1729 #ifdef TULIP_DEBUG
   1730             if (tulip_debug > 1)
   1731                 printf("%s: Using a 21140 non-MII transceiver"
   1732                        " with control setting %hhX.\n",
   1733                        tp->nic_name, p[1]);
   1734 #endif
   1735             tp->if_port = p[0];
   1736             if (startup)
   1737                 outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
   1738             outl(p[1], ioaddr + CSR12);
   1739             new_csr6 = 0x02000000 | ((p[2] & 0x71) << 18);
   1740             break;
   1741         case 2: case 4: {
   1742             u16 setup[5];
   1743             u32 csr13val, csr14val, csr15dir, csr15val;
   1744             for (i = 0; i < 5; i++)
   1745                 setup[i] = get_u16(&p[i*2 + 1]);
   1746 
   1747             tp->if_port = p[0] & 15;
   1748             if (media_cap[tp->if_port] & MediaAlwaysFD)
   1749                 tp->full_duplex = 1;
   1750 
   1751             if (startup && mtable->has_reset) {
   1752                 struct medialeaf *rleaf = &mtable->mleaf[mtable->has_reset];
   1753                 unsigned char *rst = rleaf->leafdata;
   1754 #ifdef TULIP_DEBUG
   1755                 if (tulip_debug > 1)
   1756                     printf("%s: Resetting the transceiver.\n",
   1757                            tp->nic_name);
   1758 #endif
   1759                 for (i = 0; i < rst[0]; i++)
   1760                     outl(get_u16(rst + 1 + (i<<1)) << 16, ioaddr + CSR15);
   1761             }
   1762 #ifdef TULIP_DEBUG
   1763             if (tulip_debug > 1)
   1764                 printf("%s: 21143 non-MII %s transceiver control "
   1765                        "%hX/%hX.\n",
   1766                        tp->nic_name, medianame[tp->if_port], setup[0], setup[1]);
   1767 #endif
   1768             if (p[0] & 0x40) {  /* SIA (CSR13-15) setup values are provided. */
   1769                 csr13val = setup[0];
   1770                 csr14val = setup[1];
   1771                 csr15dir = (setup[3]<<16) | setup[2];
   1772                 csr15val = (setup[4]<<16) | setup[2];
   1773                 outl(0, ioaddr + CSR13);
   1774                 outl(csr14val, ioaddr + CSR14);
   1775                 outl(csr15dir, ioaddr + CSR15); /* Direction */
   1776                 outl(csr15val, ioaddr + CSR15); /* Data */
   1777                 outl(csr13val, ioaddr + CSR13);
   1778             } else {
   1779                 csr13val = 1;
   1780                 csr14val = 0x0003FF7F;
   1781                 csr15dir = (setup[0]<<16) | 0x0008;
   1782                 csr15val = (setup[1]<<16) | 0x0008;
   1783                 if (tp->if_port <= 4)
   1784                     csr14val = t21142_csr14[tp->if_port];
   1785                 if (startup) {
   1786                     outl(0, ioaddr + CSR13);
   1787                     outl(csr14val, ioaddr + CSR14);
   1788                 }
   1789                 outl(csr15dir, ioaddr + CSR15); /* Direction */
   1790                 outl(csr15val, ioaddr + CSR15); /* Data */
   1791                 if (startup) outl(csr13val, ioaddr + CSR13);
   1792             }
   1793 #ifdef TULIP_DEBUG
   1794             if (tulip_debug > 1)
   1795                 printf("%s:  Setting CSR15 to %X/%X.\n",
   1796                        tp->nic_name, csr15dir, csr15val);
   1797 #endif
   1798             if (mleaf->type == 4)
   1799                 new_csr6 = 0x82020000 | ((setup[2] & 0x71) << 18);
   1800             else
   1801                 new_csr6 = 0x82420000;
   1802             break;
   1803         }
   1804         case 1: case 3: {
   1805             int phy_num = p[0];
   1806             int init_length = p[1];
   1807             u16 *misc_info;
   1808 
   1809             tp->if_port = 11;
   1810             new_csr6 = 0x020E0000;
   1811             if (mleaf->type == 3) {     /* 21142 */
   1812                 u16 *init_sequence = (u16*)(p+2);
   1813                 u16 *reset_sequence = &((u16*)(p+3))[init_length];
   1814                 int reset_length = p[2 + init_length*2];
   1815                 misc_info = reset_sequence + reset_length;
   1816                 if (startup)
   1817                     for (i = 0; i < reset_length; i++)
   1818                         outl(get_u16(&reset_sequence[i]) << 16, ioaddr + CSR15);
   1819                 for (i = 0; i < init_length; i++)
   1820                     outl(get_u16(&init_sequence[i]) << 16, ioaddr + CSR15);
   1821             } else {
   1822                 u8 *init_sequence = p + 2;
   1823                 u8 *reset_sequence = p + 3 + init_length;
   1824                 int reset_length = p[2 + init_length];
   1825                 misc_info = (u16*)(reset_sequence + reset_length);
   1826                 if (startup) {
   1827                     outl(mtable->csr12dir | 0x100, ioaddr + CSR12);
   1828                     for (i = 0; i < reset_length; i++)
   1829                         outl(reset_sequence[i], ioaddr + CSR12);
   1830                 }
   1831                 for (i = 0; i < init_length; i++)
   1832                     outl(init_sequence[i], ioaddr + CSR12);
   1833             }
   1834             tp->advertising[phy_num] = get_u16(&misc_info[1]) | 1;
   1835             if (startup < 2) {
   1836                 if (tp->mii_advertise == 0)
   1837                     tp->mii_advertise = tp->advertising[phy_num];
   1838 #ifdef TULIP_DEBUG
   1839                 if (tulip_debug > 1)
   1840                     printf("%s:  Advertising %hX on MII %d.\n",
   1841                            tp->nic_name, tp->mii_advertise, tp->phys[phy_num]);
   1842 #endif
   1843                 mdio_write(nic, tp->phys[phy_num], 4, tp->mii_advertise);
   1844             }
   1845             break;
   1846         }
   1847         default:
   1848             printf("%s:  Invalid media table selection %d.\n",
   1849                    tp->nic_name, mleaf->type);
   1850             new_csr6 = 0x020E0000;
   1851         }
   1852 #ifdef TULIP_DEBUG
   1853         if (tulip_debug > 1)
   1854             printf("%s: Using media type %s, CSR12 is %hhX.\n",
   1855                    tp->nic_name, medianame[tp->if_port],
   1856                    inl(ioaddr + CSR12) & 0xff);
   1857 #endif
   1858     } else if (tp->chip_id == DC21041) {
   1859         int port = tp->if_port <= 4 ? tp->if_port : 0;
   1860 #ifdef TULIP_DEBUG
   1861         if (tulip_debug > 1)
   1862             printf("%s: 21041 using media %s, CSR12 is %hX.\n",
   1863                    tp->nic_name, medianame[port == 3 ? 12: port],
   1864                    inl(ioaddr + CSR12));
   1865 #endif
   1866         outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
   1867         outl(t21041_csr14[port], ioaddr + CSR14);
   1868         outl(t21041_csr15[port], ioaddr + CSR15);
   1869         outl(t21041_csr13[port], ioaddr + CSR13);
   1870         new_csr6 = 0x80020000;
   1871     } else if (tp->chip_id == LC82C168) {
   1872         if (startup && ! tp->medialock)
   1873             tp->if_port = tp->mii_cnt ? 11 : 0;
   1874 #ifdef TULIP_DEBUG
   1875         if (tulip_debug > 1)
   1876 	    printf("%s: PNIC PHY status is %hX, media %s.\n",
   1877                    tp->nic_name, inl(ioaddr + 0xB8), medianame[tp->if_port]);
   1878 #endif
   1879         if (tp->mii_cnt) {
   1880             new_csr6 = 0x810C0000;
   1881             outl(0x0001, ioaddr + CSR15);
   1882             outl(0x0201B07A, ioaddr + 0xB8);
   1883         } else if (startup) {
   1884             /* Start with 10mbps to do autonegotiation. */
   1885             outl(0x32, ioaddr + CSR12);
   1886             new_csr6 = 0x00420000;
   1887             outl(0x0001B078, ioaddr + 0xB8);
   1888             outl(0x0201B078, ioaddr + 0xB8);
   1889         } else if (tp->if_port == 3  ||  tp->if_port == 5) {
   1890             outl(0x33, ioaddr + CSR12);
   1891             new_csr6 = 0x01860000;
   1892             /* Trigger autonegotiation. */
   1893             outl(startup ? 0x0201F868 : 0x0001F868, ioaddr + 0xB8);
   1894         } else {
   1895             outl(0x32, ioaddr + CSR12);
   1896             new_csr6 = 0x00420000;
   1897             outl(0x1F078, ioaddr + 0xB8);
   1898         }
   1899     } else if (tp->chip_id == DC21040) {                                        /* 21040 */
   1900         /* Turn on the xcvr interface. */
   1901         int csr12 = inl(ioaddr + CSR12);
   1902 #ifdef TULIP_DEBUG
   1903         if (tulip_debug > 1)
   1904             printf("%s: 21040 media type is %s, CSR12 is %hhX.\n",
   1905                    tp->nic_name, medianame[tp->if_port], csr12);
   1906 #endif
   1907         if (media_cap[tp->if_port] & MediaAlwaysFD)
   1908             tp->full_duplex = 1;
   1909         new_csr6 = 0x20000;
   1910         /* Set the full duplux match frame. */
   1911         outl(FULL_DUPLEX_MAGIC, ioaddr + CSR11);
   1912         outl(0x00000000, ioaddr + CSR13); /* Reset the serial interface */
   1913         if (t21040_csr13[tp->if_port] & 8) {
   1914             outl(0x0705, ioaddr + CSR14);
   1915             outl(0x0006, ioaddr + CSR15);
   1916         } else {
   1917             outl(0xffff, ioaddr + CSR14);
   1918             outl(0x0000, ioaddr + CSR15);
   1919         }
   1920         outl(0x8f01 | t21040_csr13[tp->if_port], ioaddr + CSR13);
   1921     } else {                                    /* Unknown chip type with no media table. */
   1922         if (tp->default_port == 0)
   1923             tp->if_port = tp->mii_cnt ? 11 : 3;
   1924         if (media_cap[tp->if_port] & MediaIsMII) {
   1925             new_csr6 = 0x020E0000;
   1926         } else if (media_cap[tp->if_port] & MediaIsFx) {
   1927             new_csr6 = 0x028600000;
   1928         } else
   1929             new_csr6 = 0x038600000;
   1930 #ifdef TULIP_DEBUG
   1931         if (tulip_debug > 1)
   1932             printf("%s: No media description table, assuming "
   1933                    "%s transceiver, CSR12 %hhX.\n",
   1934                    tp->nic_name, medianame[tp->if_port],
   1935                    inl(ioaddr + CSR12));
   1936 #endif
   1937     }
   1938 
   1939     tp->csr6 = new_csr6 | (tp->csr6 & 0xfdff) | (tp->full_duplex ? 0x0200 : 0);
   1940     return;
   1941 }
   1942 
   1943 /*
   1944   Check the MII negotiated duplex and change the CSR6 setting if
   1945   required.
   1946   Return 0 if everything is OK.
   1947   Return < 0 if the transceiver is missing or has no link beat.
   1948 */
   1949 static int tulip_check_duplex(struct nic *nic)
   1950 {
   1951         unsigned int bmsr, lpa, negotiated, new_csr6;
   1952 
   1953         bmsr = mdio_read(nic, tp->phys[0], 1);
   1954         lpa = mdio_read(nic, tp->phys[0], 5);
   1955 
   1956 #ifdef TULIP_DEBUG
   1957         if (tulip_debug > 1)
   1958                 printf("%s: MII status %#x, Link partner report "
   1959                            "%#x.\n", tp->nic_name, bmsr, lpa);
   1960 #endif
   1961 
   1962         if (bmsr == 0xffff)
   1963                 return -2;
   1964         if ((bmsr & 4) == 0) {
   1965                 int new_bmsr = mdio_read(nic, tp->phys[0], 1);
   1966                 if ((new_bmsr & 4) == 0) {
   1967 #ifdef TULIP_DEBUG
   1968                         if (tulip_debug  > 1)
   1969                                 printf("%s: No link beat on the MII interface,"
   1970                                            " status %#x.\n", tp->nic_name,
   1971                                            new_bmsr);
   1972 #endif
   1973                         return -1;
   1974                 }
   1975         }
   1976         tp->full_duplex = lpa & 0x140;
   1977 
   1978         new_csr6 = tp->csr6;
   1979         negotiated = lpa & tp->advertising[0];
   1980 
   1981         if(negotiated & 0x380) new_csr6 &= ~0x400000;
   1982         else                   new_csr6 |= 0x400000;
   1983         if (tp->full_duplex)   new_csr6 |= 0x200;
   1984         else                   new_csr6 &= ~0x200;
   1985 
   1986         if (new_csr6 != tp->csr6) {
   1987                 tp->csr6 = new_csr6;
   1988 
   1989 #ifdef TULIP_DEBUG
   1990                 if (tulip_debug > 0)
   1991                         printf("%s: Setting %s-duplex based on MII"
   1992                                    "#%d link partner capability of %#x.\n",
   1993                                    tp->nic_name,
   1994                                    tp->full_duplex ? "full" : "half",
   1995                                    tp->phys[0], lpa);
   1996 #endif
   1997                 return 1;
   1998         }
   1999 
   2000         return 0;
   2001 }
   2002