00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 #include <linux/module.h>
00038 #include <linux/version.h>
00039 #include <linux/kernel.h>
00040 #include <linux/sched.h>
00041 #include <linux/string.h>
00042 #include <linux/timer.h>
00043 #include <linux/errno.h>
00044 #include <linux/ioport.h>
00045 #include <linux/malloc.h>
00046 #include <linux/interrupt.h>
00047 #include <linux/pci.h>
00048 #include <linux/netdevice.h>
00049
00050 #include <linux/etherdevice.h>
00051 #include <linux/skbuff.h>
00052 #include <asm/processor.h>
00053 #include <asm/bitops.h>
00054 #include <asm/io.h>
00055 #include <linux/delay.h>
00056
00057 #include "sis900.h"
00058
00059 static const char *version =
00060 "sis900.c: v1.06.11 4/30/2002\n";
00061
00062 static int max_interrupt_work = 20;
00063 static int multicast_filter_limit = 128;
00064
00065 #define sis900_debug debug
00066 static int sis900_debug = 0;
00067
00068
00069 #define TX_TIMEOUT (4*HZ)
00070
00071 struct mac_chip_info {
00072 const char *name;
00073 u16 vendor_id, device_id, flags;
00074 int io_size;
00075 struct device *(*probe) (struct mac_chip_info *mac, struct pci_dev * pci_dev,
00076 struct device * net_dev);
00077 };
00078 static struct device * sis900_mac_probe (struct mac_chip_info * mac, struct pci_dev * pci_dev,
00079 struct device * net_dev);
00080
00081 static struct mac_chip_info mac_chip_table[] = {
00082 { "SiS 900 PCI Fast Ethernet", PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
00083 PCI_COMMAND_IO|PCI_COMMAND_MASTER, SIS900_TOTAL_SIZE, sis900_mac_probe},
00084 { "SiS 7016 PCI Fast Ethernet",PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
00085 PCI_COMMAND_IO|PCI_COMMAND_MASTER, SIS900_TOTAL_SIZE, sis900_mac_probe},
00086 {0,},
00087 };
00088
00089 static void sis900_read_mode(struct device *net_dev, int *speed, int *duplex);
00090
00091 static struct mii_chip_info {
00092 const char * name;
00093 u16 phy_id0;
00094 u16 phy_id1;
00095 u8 phy_types;
00096 #define HOME 0x0001
00097 #define LAN 0x0002
00098 #define MIX 0x0003
00099 } mii_chip_table[] = {
00100 { "SiS 900 Internal MII PHY", 0x001d, 0x8000, LAN },
00101 { "SiS 7014 Physical Layer Solution", 0x0016, 0xf830, LAN },
00102 { "AMD 79C901 10BASE-T PHY", 0x0000, 0x6B70, LAN },
00103 { "AMD 79C901 HomePNA PHY", 0x0000, 0x6B90, HOME},
00104 { "ICS LAN PHY", 0x0015, 0xF440, LAN },
00105 { "NS 83851 PHY", 0x2000, 0x5C20, MIX },
00106 { "Realtek RTL8201 PHY", 0x0000, 0x8200, LAN },
00107 {0,},
00108 };
00109
00110 struct mii_phy {
00111 struct mii_phy * next;
00112 int phy_addr;
00113 u16 phy_id0;
00114 u16 phy_id1;
00115 u16 status;
00116 u8 phy_types;
00117 };
00118
00119 typedef struct _BufferDesc {
00120 u32 link;
00121 u32 cmdsts;
00122 u32 bufptr;
00123 } BufferDesc;
00124
00125 struct sis900_private {
00126 struct device *next_module;
00127 struct net_device_stats stats;
00128 struct pci_dev * pci_dev;
00129
00130 struct mac_chip_info * mac;
00131 struct mii_phy * mii;
00132 struct mii_phy * first_mii;
00133 unsigned int cur_phy;
00134
00135 struct timer_list timer;
00136 u8 autong_complete;
00137
00138 unsigned int cur_rx, dirty_rx;
00139 unsigned int cur_tx, dirty_tx;
00140
00141
00142 struct sk_buff *tx_skbuff[NUM_TX_DESC];
00143 struct sk_buff *rx_skbuff[NUM_RX_DESC];
00144 BufferDesc tx_ring[NUM_TX_DESC];
00145 BufferDesc rx_ring[NUM_RX_DESC];
00146
00147 unsigned int tx_full;
00148 int LinkOn;
00149 };
00150
00151 #ifdef MODULE
00152 #if LINUX_VERSION_CODE > 0x20115
00153 MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
00154 MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
00155 MODULE_PARM(multicast_filter_limit, "i");
00156 MODULE_PARM(max_interrupt_work, "i");
00157 MODULE_PARM(debug, "i");
00158 #endif
00159 #endif
00160
00161 static int sis900_open(struct device *net_dev);
00162 static int sis900_mii_probe (struct device * net_dev);
00163 static void sis900_init_rxfilter (struct device * net_dev);
00164 static u16 read_eeprom(long ioaddr, int location);
00165 static u16 mdio_read(struct device *net_dev, int phy_id, int location);
00166 static void mdio_write(struct device *net_dev, int phy_id, int location, int val);
00167 static void sis900_timer(unsigned long data);
00168 static void sis900_check_mode (struct device *net_dev, struct mii_phy *mii_phy);
00169 static void sis900_tx_timeout(struct device *net_dev);
00170 static void sis900_init_tx_ring(struct device *net_dev);
00171 static void sis900_init_rx_ring(struct device *net_dev);
00172 static int sis900_start_xmit(struct sk_buff *skb, struct device *net_dev);
00173 static int sis900_rx(struct device *net_dev);
00174 static void sis900_finish_xmit (struct device *net_dev);
00175 static void sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
00176 static int sis900_close(struct device *net_dev);
00177 static int mii_ioctl(struct device *net_dev, struct ifreq *rq, int cmd);
00178 static struct enet_statistics *sis900_get_stats(struct device *net_dev);
00179 static u16 sis900_compute_hashtable_index(u8 *addr, u8 revision);
00180 static void set_rx_mode(struct device *net_dev);
00181 static void sis900_reset(struct device *net_dev);
00182 static void sis630_set_eq(struct device *net_dev, u8 revision);
00183 static u16 sis900_default_phy(struct device * net_dev);
00184 static void sis900_set_capability( struct device *net_dev ,struct mii_phy *phy);
00185 static u16 sis900_reset_phy(struct device *net_dev, int phy_addr);
00186 static void sis900_auto_negotiate(struct device *net_dev, int phy_addr);
00187 static void sis900_set_mode (long ioaddr, int speed, int duplex);
00188
00189
00190 static struct device *root_sis900_dev = NULL;
00191
00192
00193 int sis900_probe (struct device * net_dev)
00194 {
00195 int found = 0;
00196 struct pci_dev * pci_dev = NULL;
00197
00198 if (!pci_present())
00199 return -ENODEV;
00200
00201 while ((pci_dev = pci_find_class (PCI_CLASS_NETWORK_ETHERNET << 8, pci_dev)) != NULL) {
00202
00203 u32 pci_io_base;
00204 struct mac_chip_info * mac;
00205
00206 for (mac = mac_chip_table; mac->vendor_id; mac++) {
00207
00208 if (pci_dev->vendor == mac->vendor_id &&
00209 pci_dev->device == mac->device_id)
00210 break;
00211 }
00212
00213 if (mac->vendor_id == 0)
00214
00215 continue;
00216
00217
00218 pci_io_base = pci_dev->base_address[0] & PCI_BASE_ADDRESS_IO_MASK;
00219 if ((mac->flags & PCI_COMMAND_IO ) &&
00220 check_region(pci_io_base, mac->io_size))
00221 continue;
00222
00223
00224 pci_set_master(pci_dev);
00225
00226
00227 net_dev = mac->probe(mac, pci_dev, net_dev);
00228
00229 if (net_dev != NULL) {
00230 found++;
00231 }
00232 net_dev = NULL;
00233 }
00234 return found ? 0 : -ENODEV;
00235 }
00236
00237
00238 static int sis900_get_mac_addr(struct pci_dev * pci_dev, struct device *net_dev)
00239 {
00240 long ioaddr = pci_dev->base_address[0] & ~3;
00241 u16 signature;
00242 int i;
00243
00244
00245 signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
00246 if (signature == 0xffff || signature == 0x0000) {
00247 printk (KERN_INFO "%s: Error EERPOM read %x\n",
00248 net_dev->name, signature);
00249 return 0;
00250 }
00251
00252
00253 for (i = 0; i < 3; i++)
00254 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
00255
00256 return 1;
00257 }
00258
00259
00260 static int sis630e_get_mac_addr(struct pci_dev * pci_dev, struct device *net_dev)
00261 {
00262 struct pci_dev *isa_bridge = NULL;
00263 u8 reg;
00264 int i;
00265
00266 if ((isa_bridge = pci_find_device(0x1039, 0x0008, isa_bridge)) == NULL) {
00267 printk("%s: Can not find ISA bridge\n", net_dev->name);
00268 return 0;
00269 }
00270 pci_read_config_byte(isa_bridge, 0x48, ®);
00271 pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
00272
00273 for (i = 0; i < 6; i++) {
00274 outb(0x09 + i, 0x70);
00275 ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
00276 }
00277 pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
00278
00279 return 1;
00280 }
00281
00282
00283 static int sis635_get_mac_addr(struct pci_dev * pci_dev, struct device *net_dev)
00284 {
00285 long ioaddr = net_dev->base_addr;
00286 u32 rfcrSave;
00287 u32 i;
00288
00289 rfcrSave = inl(rfcr + ioaddr);
00290
00291 outl(rfcrSave | RELOAD, ioaddr + cr);
00292 outl(0, ioaddr + cr);
00293
00294
00295 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
00296
00297
00298 for (i = 0 ; i < 3 ; i++) {
00299 outl((i << RFADDR_shift), ioaddr + rfcr);
00300 *( ((u16 *)net_dev->dev_addr) + i) = inw(ioaddr + rfdr);
00301 }
00302
00303
00304 outl(rfcrSave | RFEN, rfcr + ioaddr);
00305
00306 return 1;
00307 }
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323 static int sis962_get_mac_addr(struct pci_dev * pci_dev, struct device *net_dev)
00324 {
00325
00326 long ioaddr = net_dev->base_addr;
00327 long ee_addr = ioaddr + mear;
00328 u32 waittime = 0;
00329 int ret = 0;
00330
00331 outl(EEREQ, ee_addr);
00332 while(waittime < 2000) {
00333 if(inl(ee_addr) & EEGNT) {
00334 ret = sis900_get_mac_addr(pci_dev, net_dev);
00335 outl(EEDONE, ee_addr);
00336 return(ret);
00337 } else {
00338 udelay(1);
00339 waittime ++;
00340 }
00341 }
00342 outl(EEDONE, ee_addr);
00343 return 0;
00344 }
00345
00346 static struct device * sis900_mac_probe (struct mac_chip_info * mac, struct pci_dev * pci_dev,
00347 struct device * net_dev)
00348 {
00349 struct sis900_private *sis_priv;
00350 long ioaddr = pci_dev->base_address[0] & ~3;
00351 int irq = pci_dev->irq;
00352 static int did_version = 0;
00353 u8 revision;
00354 int i, ret = 0;
00355
00356 if (did_version++ == 0)
00357 printk(KERN_INFO "%s", version);
00358
00359 if ((net_dev = init_etherdev(net_dev, 0)) == NULL)
00360 return NULL;
00361
00362 if ((net_dev->priv = kmalloc(sizeof(struct sis900_private), GFP_KERNEL)) == NULL) {
00363 unregister_netdev(net_dev);
00364 return NULL;
00365 }
00366
00367 sis_priv = net_dev->priv;
00368 memset(sis_priv, 0, sizeof(struct sis900_private));
00369
00370
00371 request_region(ioaddr, mac->io_size, net_dev->name);
00372 net_dev->base_addr = ioaddr;
00373 net_dev->irq = irq;
00374 sis_priv->pci_dev = pci_dev;
00375 sis_priv->mac = mac;
00376
00377 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
00378 if ( revision == SIS630E_900_REV )
00379 ret = sis630e_get_mac_addr(pci_dev, net_dev);
00380 else if ((revision > 0x81) && (revision <= 0x90))
00381 ret = sis635_get_mac_addr(pci_dev, net_dev);
00382 else if (revision == SIS962_900_REV)
00383 ret = sis962_get_mac_addr(pci_dev, net_dev);
00384 else
00385 ret = sis900_get_mac_addr(pci_dev, net_dev);
00386
00387 if (ret == 0) {
00388 unregister_netdev(net_dev);
00389 return NULL;
00390 }
00391
00392
00393 printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name, mac->name,
00394 ioaddr, irq);
00395 for (i = 0; i < 5; i++)
00396 printk("%2.2x:", (u8)net_dev->dev_addr[i]);
00397 printk("%2.2x.\n", net_dev->dev_addr[i]);
00398
00399
00400 if (revision == SIS630ET_900_REV)
00401 outl(ACCESSMODE | inl(ioaddr + cr), ioaddr + cr);
00402
00403
00404 if (sis900_mii_probe(net_dev) == 0) {
00405 unregister_netdev(net_dev);
00406 kfree(sis_priv);
00407 release_region(ioaddr, mac->io_size);
00408 return NULL;
00409 }
00410
00411 sis_priv->next_module = root_sis900_dev;
00412 root_sis900_dev = net_dev;
00413
00414
00415 net_dev->open = &sis900_open;
00416 net_dev->hard_start_xmit = &sis900_start_xmit;
00417 net_dev->stop = &sis900_close;
00418 net_dev->get_stats = &sis900_get_stats;
00419 net_dev->set_multicast_list = &set_rx_mode;
00420 net_dev->do_ioctl = &mii_ioctl;
00421
00422 return net_dev;
00423 }
00424
00425
00426 static int sis900_mii_probe (struct device * net_dev)
00427 {
00428 struct sis900_private * sis_priv = (struct sis900_private *)net_dev->priv;
00429 u16 poll_bit = MII_STAT_LINK, status = 0;
00430 unsigned int timeout = jiffies + 5 * HZ;
00431 int phy_addr;
00432 u8 revision;
00433
00434 sis_priv->mii = NULL;
00435
00436
00437 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
00438 struct mii_phy * mii_phy = NULL;
00439 u16 mii_status;
00440 int i;
00441
00442 for(i=0; i<2; i++)
00443 mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
00444
00445 if (mii_status == 0xffff || mii_status == 0x0000)
00446
00447 continue;
00448
00449 if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) == NULL) {
00450 printk(KERN_INFO "Cannot allocate mem for struct mii_phy\n");
00451 return 0;
00452 }
00453
00454 mii_phy->phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
00455 mii_phy->phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
00456 mii_phy->phy_addr = phy_addr;
00457 mii_phy->status = mii_status;
00458 mii_phy->next = sis_priv->mii;
00459 sis_priv->mii = mii_phy;
00460 sis_priv->first_mii = mii_phy;
00461
00462 for (i=0; mii_chip_table[i].phy_id1; i++)
00463 if ( ( mii_phy->phy_id0 == mii_chip_table[i].phy_id0 ) &&
00464 ( (mii_phy->phy_id1 & 0xFFF0) == mii_chip_table[i].phy_id1 )){
00465
00466 mii_phy->phy_types = mii_chip_table[i].phy_types;
00467 if(mii_chip_table[i].phy_types == MIX)
00468 mii_phy->phy_types =
00469 (mii_status & (MII_STAT_CAN_TX_FDX | MII_STAT_CAN_TX))?LAN:HOME;
00470 printk(KERN_INFO "%s: %s transceiver found at address %d.\n",
00471 net_dev->name, mii_chip_table[i].name, phy_addr);
00472 break;
00473 }
00474
00475 if( !mii_chip_table[i].phy_id1 )
00476 printk(KERN_INFO "%s: Unknown PHY transceiver found at address %d.\n",
00477 net_dev->name, phy_addr);
00478 }
00479
00480 if (sis_priv->mii == NULL) {
00481 printk(KERN_INFO "%s: No MII transceivers found!\n",
00482 net_dev->name);
00483 return 0;
00484 }
00485
00486
00487 sis_priv->mii = NULL;
00488 sis900_default_phy( net_dev );
00489
00490
00491 if( (sis_priv->mii->phy_id0 == 0x001D) &&
00492 ( (sis_priv->mii->phy_id1&0xFFF0) == 0x8000) )
00493 status = sis900_reset_phy( net_dev, sis_priv->cur_phy );
00494
00495
00496 if ((sis_priv->mii->phy_id0 == 0x0015) &&
00497 ((sis_priv->mii->phy_id1&0xFFF0) == 0xF440))
00498 mdio_write(net_dev, sis_priv->cur_phy, 0x0018, 0xD200);
00499
00500 if( status & MII_STAT_LINK ){
00501 while (poll_bit) {
00502 current->state = TASK_INTERRUPTIBLE;
00503 schedule_timeout(0);
00504 poll_bit ^= (mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS) & poll_bit);
00505 if (jiffies >= timeout) {
00506 printk(KERN_WARNING "%s: reset phy and link down\n", net_dev->name);
00507 return -ETIME;
00508 }
00509 }
00510 }
00511
00512 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
00513 if (revision == SIS630E_900_REV) {
00514
00515 mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
00516 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
00517 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
00518 mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
00519
00520 }
00521
00522 if (sis_priv->mii->status & MII_STAT_LINK)
00523 sis_priv->LinkOn = TRUE;
00524 else
00525 sis_priv->LinkOn = FALSE;
00526
00527 return 1;
00528 }
00529
00530
00531
00532 static u16 sis900_default_phy(struct device * net_dev)
00533 {
00534 struct sis900_private * sis_priv = (struct sis900_private *)net_dev->priv;
00535 struct mii_phy *phy = NULL, *phy_home = NULL, *default_phy = NULL;
00536 u16 status;
00537
00538 for( phy=sis_priv->first_mii; phy; phy=phy->next ){
00539 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
00540 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
00541
00542
00543 if ( (status & MII_STAT_LINK) && !(default_phy) )
00544 default_phy = phy;
00545 else{
00546 status = mdio_read(net_dev, phy->phy_addr, MII_CONTROL);
00547 mdio_write(net_dev, phy->phy_addr, MII_CONTROL,
00548 status | MII_CNTL_AUTO | MII_CNTL_ISOLATE);
00549 if( phy->phy_types == HOME )
00550 phy_home = phy;
00551 }
00552 }
00553
00554 if( (!default_phy) && phy_home )
00555 default_phy = phy_home;
00556 else if(!default_phy)
00557 default_phy = sis_priv->first_mii;
00558
00559 if( sis_priv->mii != default_phy ){
00560 sis_priv->mii = default_phy;
00561 sis_priv->cur_phy = default_phy->phy_addr;
00562 printk(KERN_INFO "%s: Using transceiver found at address %d as default\n", net_dev->name,sis_priv->cur_phy);
00563 }
00564
00565 status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
00566 status &= (~MII_CNTL_ISOLATE);
00567
00568 mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, status);
00569 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
00570 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
00571
00572 return status;
00573 }
00574
00575
00576
00577 static void sis900_set_capability( struct device *net_dev , struct mii_phy *phy )
00578 {
00579 u16 cap;
00580 u16 status;
00581
00582 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
00583 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
00584
00585 cap = MII_NWAY_CSMA_CD |
00586 ((phy->status & MII_STAT_CAN_TX_FDX)? MII_NWAY_TX_FDX:0) |
00587 ((phy->status & MII_STAT_CAN_TX) ? MII_NWAY_TX:0) |
00588 ((phy->status & MII_STAT_CAN_T_FDX) ? MII_NWAY_T_FDX:0)|
00589 ((phy->status & MII_STAT_CAN_T) ? MII_NWAY_T:0);
00590
00591 mdio_write( net_dev, phy->phy_addr, MII_ANADV, cap );
00592 }
00593
00594
00595
00596 #define eeprom_delay() inl(ee_addr)
00597
00598
00599
00600 static u16 read_eeprom(long ioaddr, int location)
00601 {
00602 int i;
00603 u16 retval = 0;
00604 long ee_addr = ioaddr + mear;
00605 u32 read_cmd = location | EEread;
00606
00607 outl(0, ee_addr);
00608 eeprom_delay();
00609 outl(EECLK, ee_addr);
00610 eeprom_delay();
00611
00612
00613 for (i = 8; i >= 0; i--) {
00614 u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
00615 outl(dataval, ee_addr);
00616 eeprom_delay();
00617 outl(dataval | EECLK, ee_addr);
00618 eeprom_delay();
00619 }
00620 outb(EECS, ee_addr);
00621 eeprom_delay();
00622
00623
00624 for (i = 16; i > 0; i--) {
00625 outl(EECS, ee_addr);
00626 eeprom_delay();
00627 outl(EECS | EECLK, ee_addr);
00628 eeprom_delay();
00629 retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
00630 eeprom_delay();
00631 }
00632
00633
00634 outl(0, ee_addr);
00635 eeprom_delay();
00636 outl(EECLK, ee_addr);
00637
00638 return (retval);
00639 }
00640
00641
00642
00643
00644 #define mdio_delay() inl(mdio_addr)
00645
00646 static void mdio_idle(long mdio_addr)
00647 {
00648 outl(MDIO | MDDIR, mdio_addr);
00649 mdio_delay();
00650 outl(MDIO | MDDIR | MDC, mdio_addr);
00651 }
00652
00653
00654 static void mdio_reset(long mdio_addr)
00655 {
00656 int i;
00657
00658 for (i = 31; i >= 0; i--) {
00659 outl(MDDIR | MDIO, mdio_addr);
00660 mdio_delay();
00661 outl(MDDIR | MDIO | MDC, mdio_addr);
00662 mdio_delay();
00663 }
00664 return;
00665 }
00666
00667 static u16 mdio_read(struct device *net_dev, int phy_id, int location)
00668 {
00669 long mdio_addr = net_dev->base_addr + mear;
00670 int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
00671 u16 retval = 0;
00672 int i;
00673
00674 mdio_reset(mdio_addr);
00675 mdio_idle(mdio_addr);
00676
00677 for (i = 15; i >= 0; i--) {
00678 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
00679 outl(dataval, mdio_addr);
00680 mdio_delay();
00681 outl(dataval | MDC, mdio_addr);
00682 mdio_delay();
00683 }
00684
00685
00686 for (i = 16; i > 0; i--) {
00687 outl(0, mdio_addr);
00688 mdio_delay();
00689 retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
00690 outl(MDC, mdio_addr);
00691 mdio_delay();
00692 }
00693 outl(0x00, mdio_addr);
00694
00695 return retval;
00696 }
00697
00698 static void mdio_write(struct device *net_dev, int phy_id, int location, int value)
00699 {
00700 long mdio_addr = net_dev->base_addr + mear;
00701 int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
00702 int i;
00703
00704 mdio_reset(mdio_addr);
00705 mdio_idle(mdio_addr);
00706
00707
00708 for (i = 15; i >= 0; i--) {
00709 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
00710 outb(dataval, mdio_addr);
00711 mdio_delay();
00712 outb(dataval | MDC, mdio_addr);
00713 mdio_delay();
00714 }
00715 mdio_delay();
00716
00717
00718 for (i = 15; i >= 0; i--) {
00719 int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
00720 outl(dataval, mdio_addr);
00721 mdio_delay();
00722 outl(dataval | MDC, mdio_addr);
00723 mdio_delay();
00724 }
00725 mdio_delay();
00726
00727
00728 for (i = 2; i > 0; i--) {
00729 outb(0, mdio_addr);
00730 mdio_delay();
00731 outb(MDC, mdio_addr);
00732 mdio_delay();
00733 }
00734 outl(0x00, mdio_addr);
00735
00736 return;
00737 }
00738
00739 static u16 sis900_reset_phy(struct device *net_dev, int phy_addr)
00740 {
00741 int i = 0;
00742 u16 status;
00743
00744 while (i++ < 2)
00745 status = mdio_read(net_dev, phy_addr, MII_STATUS);
00746
00747 mdio_write( net_dev, phy_addr, MII_CONTROL, MII_CNTL_RESET );
00748
00749 return status;
00750 }
00751
00752 static int
00753 sis900_open(struct device *net_dev)
00754 {
00755 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
00756 long ioaddr = net_dev->base_addr;
00757 u8 revision;
00758
00759
00760 sis900_reset(net_dev);
00761
00762
00763 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
00764 sis630_set_eq(net_dev, revision);
00765
00766 if (request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev)) {
00767 return -EAGAIN;
00768 }
00769
00770 MOD_INC_USE_COUNT;
00771
00772 sis900_init_rxfilter(net_dev);
00773
00774 sis900_init_tx_ring(net_dev);
00775 sis900_init_rx_ring(net_dev);
00776
00777 set_rx_mode(net_dev);
00778
00779 net_dev->tbusy = 0;
00780 net_dev->interrupt = 0;
00781 net_dev->start = 1;
00782
00783
00784 sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
00785
00786
00787 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
00788 outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
00789 outl(IE, ioaddr + ier);
00790
00791 sis900_check_mode(net_dev, sis_priv->mii);
00792
00793
00794
00795 init_timer(&sis_priv->timer);
00796 sis_priv->timer.expires = jiffies + HZ;
00797 sis_priv->timer.data = (unsigned long)net_dev;
00798 sis_priv->timer.function = &sis900_timer;
00799 add_timer(&sis_priv->timer);
00800
00801 return 0;
00802 }
00803
00804
00805 static void
00806 sis900_init_rxfilter (struct device * net_dev)
00807 {
00808 long ioaddr = net_dev->base_addr;
00809 u32 rfcrSave;
00810 u32 i;
00811
00812 rfcrSave = inl(rfcr + ioaddr);
00813
00814
00815 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
00816
00817
00818 for (i = 0 ; i < 3 ; i++) {
00819 u32 w;
00820
00821 w = (u32) *((u16 *)(net_dev->dev_addr)+i);
00822 outl((i << RFADDR_shift), ioaddr + rfcr);
00823 outl(w, ioaddr + rfdr);
00824
00825 if (sis900_debug > 2) {
00826 printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
00827 net_dev->name, i, inl(ioaddr + rfdr));
00828 }
00829 }
00830
00831
00832 outl(rfcrSave | RFEN, rfcr + ioaddr);
00833 }
00834
00835
00836 static void
00837 sis900_init_tx_ring(struct device *net_dev)
00838 {
00839 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
00840 long ioaddr = net_dev->base_addr;
00841 int i;
00842
00843 sis_priv->tx_full = 0;
00844 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
00845
00846 for (i = 0; i < NUM_TX_DESC; i++) {
00847 sis_priv->tx_skbuff[i] = NULL;
00848
00849 sis_priv->tx_ring[i].link = (u32) virt_to_bus(&sis_priv->tx_ring[i+1]);
00850 sis_priv->tx_ring[i].cmdsts = 0;
00851 sis_priv->tx_ring[i].bufptr = 0;
00852 }
00853 sis_priv->tx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->tx_ring[0]);
00854
00855
00856 outl(virt_to_bus(&sis_priv->tx_ring[0]), ioaddr + txdp);
00857 if (sis900_debug > 2)
00858 printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
00859 net_dev->name, inl(ioaddr + txdp));
00860 }
00861
00862
00863 static void
00864 sis900_init_rx_ring(struct device *net_dev)
00865 {
00866 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
00867 long ioaddr = net_dev->base_addr;
00868 int i;
00869
00870 sis_priv->cur_rx = 0;
00871 sis_priv->dirty_rx = 0;
00872
00873
00874 for (i = 0; i < NUM_RX_DESC; i++) {
00875 sis_priv->rx_skbuff[i] = NULL;
00876
00877 sis_priv->rx_ring[i].link = (u32) virt_to_bus(&sis_priv->rx_ring[i+1]);
00878 sis_priv->rx_ring[i].cmdsts = 0;
00879 sis_priv->rx_ring[i].bufptr = 0;
00880 }
00881 sis_priv->rx_ring[i-1].link = (u32) virt_to_bus(&sis_priv->rx_ring[0]);
00882
00883
00884 for (i = 0; i < NUM_RX_DESC; i++) {
00885 struct sk_buff *skb;
00886
00887 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
00888
00889
00890
00891
00892 break;
00893 }
00894 skb->dev = net_dev;
00895 sis_priv->rx_skbuff[i] = skb;
00896 sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
00897 sis_priv->rx_ring[i].bufptr = virt_to_bus(skb->tail);
00898 }
00899 sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
00900
00901
00902 outl(virt_to_bus(&sis_priv->rx_ring[0]), ioaddr + rxdp);
00903 if (sis900_debug > 2)
00904 printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
00905 net_dev->name, inl(ioaddr + rxdp));
00906 }
00907
00908
00909
00910
00911
00912
00913
00914
00915
00916
00917
00918
00919
00920
00921
00922
00923
00924
00925
00926
00927
00928
00929
00930
00931
00932
00933
00934
00935 static void sis630_set_eq(struct device *net_dev, u8 revision)
00936 {
00937 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
00938 u16 reg14h, eq_value, max_value=0, min_value=0;
00939 u8 host_bridge_rev;
00940 int i, maxcount=10;
00941 struct pci_dev *dev=NULL;
00942
00943 if ( !(revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
00944 revision == SIS630A_900_REV || revision == SIS630ET_900_REV) )
00945 return;
00946
00947 if ((dev = pci_find_device(SIS630_VENDOR_ID, SIS630_DEVICE_ID, dev)))
00948 pci_read_config_byte(dev, PCI_CLASS_REVISION, &host_bridge_rev);
00949
00950 if (sis_priv->LinkOn) {
00951 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
00952 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (0x2200 | reg14h) & 0xBFFF);
00953 for (i=0; i < maxcount; i++) {
00954 eq_value=(0x00F8 & mdio_read(net_dev, sis_priv->cur_phy, MII_RESV)) >> 3;
00955 if (i == 0)
00956 max_value=min_value=eq_value;
00957 max_value=(eq_value > max_value) ? eq_value : max_value;
00958 min_value=(eq_value < min_value) ? eq_value : min_value;
00959 }
00960
00961 if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
00962 revision == SIS630ET_900_REV) {
00963 if (max_value < 5)
00964 eq_value=max_value;
00965 else if (max_value >= 5 && max_value < 15)
00966 eq_value=(max_value == min_value) ? max_value+2 : max_value+1;
00967 else if (max_value >= 15)
00968 eq_value=(max_value == min_value) ? max_value+6 : max_value+5;
00969 }
00970
00971 if (revision == SIS630A_900_REV &&
00972 (host_bridge_rev == SIS630B0 || host_bridge_rev == SIS630B1)) {
00973 if (max_value == 0)
00974 eq_value=3;
00975 else
00976 eq_value=(max_value+min_value+1)/2;
00977 }
00978
00979 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
00980 reg14h=(reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
00981 reg14h=(reg14h | 0x6000) & 0xFDFF;
00982 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
00983 }
00984 else {
00985 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
00986 if (revision == SIS630A_900_REV &&
00987 (host_bridge_rev == SIS630B0 || host_bridge_rev == SIS630B1))
00988 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2200) & 0xBFFF);
00989 else
00990 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2000) & 0xBFFF);
00991 }
00992 return;
00993 }
00994
00995
00996
00997
00998
00999 static void sis900_timer(unsigned long data)
01000 {
01001 struct device *net_dev = (struct device *)data;
01002 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
01003 struct mii_phy *mii_phy = sis_priv->mii;
01004 static int next_tick = 5*HZ;
01005 u16 status;
01006 u8 revision;
01007
01008 if(!sis_priv->autong_complete){
01009 int speed, duplex = 0;
01010
01011 sis900_read_mode(net_dev, &speed, &duplex);
01012 if(duplex){
01013 sis900_set_mode(net_dev->base_addr, speed, duplex);
01014 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
01015 sis630_set_eq(net_dev, revision);
01016 }
01017
01018 sis_priv->timer.expires = jiffies + HZ;
01019 add_timer(&sis_priv->timer);
01020 return;
01021 }
01022
01023 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
01024 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
01025
01026
01027 if ( !sis_priv->LinkOn ) {
01028 LookForLink:
01029
01030 status = sis900_default_phy( net_dev );
01031 mii_phy = sis_priv->mii;
01032
01033 if( status & MII_STAT_LINK ){
01034 sis900_check_mode(net_dev, mii_phy);
01035 sis_priv->LinkOn = TRUE;
01036 }
01037 }
01038
01039 else{
01040 if( !(status & MII_STAT_LINK) ){
01041 sis_priv->LinkOn = FALSE;
01042 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
01043
01044
01045 if( (mii_phy->phy_id0 == 0x001D) &&
01046 ( (mii_phy->phy_id1 & 0xFFF0) == 0x8000 ))
01047 sis900_reset_phy( net_dev, sis_priv->cur_phy );
01048
01049 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
01050 sis630_set_eq(net_dev, revision);
01051
01052 goto LookForLink;
01053 }
01054 }
01055
01056 sis_priv->timer.expires = jiffies + next_tick;
01057 add_timer(&sis_priv->timer);
01058 }
01059
01060 static void sis900_check_mode (struct device *net_dev, struct mii_phy *mii_phy)
01061 {
01062 struct sis900_private *sis_priv = (struct sis900_private *)net_dev->priv;
01063 long ioaddr = net_dev->base_addr;
01064 int speed, duplex;
01065
01066 if( mii_phy->phy_types == LAN ){
01067 outl( ~EXD & inl( ioaddr + cfg ), ioaddr + cfg);
01068 sis900_set_capability(net_dev , mii_phy);
01069 sis900_auto_negotiate(net_dev, sis_priv->cur_phy);
01070 }else{
01071 outl(EXD | inl( ioaddr + cfg ), ioaddr + cfg);
01072 speed = HW_SPEED_HOME;
01073 duplex = FDX_CAPABLE_HALF_SELECTED;
01074 sis900_set_mode(net_dev->base_addr, speed, duplex);
01075 sis_priv->autong_complete = 1;
01076 }
01077 }
01078
01079 static void sis900_set_mode (long ioaddr, int speed, int duplex)
01080 {
01081 u32 tx_flags = 0, rx_flags = 0;
01082
01083 if( inl(ioaddr + cfg) & EDB_MASTER_EN ){
01084 tx_flags = TxATP | (DMA_BURST_64 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
01085 rx_flags = DMA_BURST_64 << RxMXDMA_shift;
01086 }
01087 else{
01088 tx_flags = TxATP | (DMA_BURST_512 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
01089 rx_flags = DMA_BURST_512 << RxMXDMA_shift;
01090 }
01091
01092 if (speed == HW_SPEED_HOME || speed == HW_S