1 /* sis900.c: A SiS 900/7016 PCI Fast Ethernet driver for Linux.
2 Copyright 1999 Silicon Integrated System Corporation
3 Revision: 1.08.06 Sep. 24 2002
5 Modified from the driver which is originally written by Donald Becker.
7 This software may be used and distributed according to the terms
8 of the GNU General Public License (GPL), incorporated herein by reference.
9 Drivers based on this skeleton fall under the GPL and must retain
10 the authorship (implicit copyright) notice.
13 SiS 7016 Fast Ethernet PCI Bus 10/100 Mbps LAN Controller with OnNow Support,
14 preliminary Rev. 1.0 Jan. 14, 1998
15 SiS 900 Fast Ethernet PCI Bus 10/100 Mbps LAN Single Chip with OnNow Support,
16 preliminary Rev. 1.0 Nov. 10, 1998
17 SiS 7014 Single Chip 100BASE-TX/10BASE-T Physical Layer Solution,
18 preliminary Rev. 1.0 Jan. 18, 1998
19 http://www.sis.com.tw/support/databook.htm
21 Rev 1.08.07 Nov. 2 2003 Daniele Venzano <webvenza@libero.it> add suspend/resume support
22 Rev 1.08.06 Sep. 24 2002 Mufasa Yang bug fix for Tx timeout & add SiS963 support
23 Rev 1.08.05 Jun. 6 2002 Mufasa Yang bug fix for read_eeprom & Tx descriptor over-boundary
24 Rev 1.08.04 Apr. 25 2002 Mufasa Yang <mufasa@sis.com.tw> added SiS962 support
25 Rev 1.08.03 Feb. 1 2002 Matt Domsch <Matt_Domsch@dell.com> update to use library crc32 function
26 Rev 1.08.02 Nov. 30 2001 Hui-Fen Hsu workaround for EDB & bug fix for dhcp problem
27 Rev 1.08.01 Aug. 25 2001 Hui-Fen Hsu update for 630ET & workaround for ICS1893 PHY
28 Rev 1.08.00 Jun. 11 2001 Hui-Fen Hsu workaround for RTL8201 PHY and some bug fix
29 Rev 1.07.11 Apr. 2 2001 Hui-Fen Hsu updates PCI drivers to use the new pci_set_dma_mask for kernel 2.4.3
30 Rev 1.07.10 Mar. 1 2001 Hui-Fen Hsu <hfhsu@sis.com.tw> some bug fix & 635M/B support
31 Rev 1.07.09 Feb. 9 2001 Dave Jones <davej@suse.de> PCI enable cleanup
32 Rev 1.07.08 Jan. 8 2001 Lei-Chun Chang added RTL8201 PHY support
33 Rev 1.07.07 Nov. 29 2000 Lei-Chun Chang added kernel-doc extractable documentation and 630 workaround fix
34 Rev 1.07.06 Nov. 7 2000 Jeff Garzik <jgarzik@pobox.com> some bug fix and cleaning
35 Rev 1.07.05 Nov. 6 2000 metapirat<metapirat@gmx.de> contribute media type select by ifconfig
36 Rev 1.07.04 Sep. 6 2000 Lei-Chun Chang added ICS1893 PHY support
37 Rev 1.07.03 Aug. 24 2000 Lei-Chun Chang (lcchang@sis.com.tw) modified 630E eqaulizer workaround rule
38 Rev 1.07.01 Aug. 08 2000 Ollie Lho minor update for SiS 630E and SiS 630E A1
39 Rev 1.07 Mar. 07 2000 Ollie Lho bug fix in Rx buffer ring
40 Rev 1.06.04 Feb. 11 2000 Jeff Garzik <jgarzik@pobox.com> softnet and init for kernel 2.4
41 Rev 1.06.03 Dec. 23 1999 Ollie Lho Third release
42 Rev 1.06.02 Nov. 23 1999 Ollie Lho bug in mac probing fixed
43 Rev 1.06.01 Nov. 16 1999 Ollie Lho CRC calculation provide by Joseph Zbiciak (im14u2c@primenet.com)
44 Rev 1.06 Nov. 4 1999 Ollie Lho (ollie@sis.com.tw) Second release
45 Rev 1.05.05 Oct. 29 1999 Ollie Lho (ollie@sis.com.tw) Single buffer Tx/Rx
46 Chin-Shan Li (lcs@sis.com.tw) Added AMD Am79c901 HomePNA PHY support
47 Rev 1.05 Aug. 7 1999 Jim Huang (cmhuang@sis.com.tw) Initial release
50 #include <linux/module.h>
51 #include <linux/kernel.h>
52 #include <linux/string.h>
53 #include <linux/timer.h>
54 #include <linux/errno.h>
55 #include <linux/ioport.h>
56 #include <linux/slab.h>
57 #include <linux/interrupt.h>
58 #include <linux/pci.h>
59 #include <linux/netdevice.h>
60 #include <linux/init.h>
61 #include <linux/mii.h>
62 #include <linux/etherdevice.h>
63 #include <linux/skbuff.h>
64 #include <linux/delay.h>
65 #include <linux/ethtool.h>
66 #include <linux/crc32.h>
68 #include <asm/processor.h> /* Processor type for cache alignment. */
69 #include <asm/bitops.h>
71 #include <asm/uaccess.h> /* User space memory access functions */
75 #define SIS900_MODULE_NAME "sis900"
76 #define SIS900_DRV_VERSION "v1.08.07 11/02/2003"
78 static char version[] __devinitdata =
79 KERN_INFO "sis900.c: " SIS900_DRV_VERSION "\n";
81 static int max_interrupt_work = 40;
82 static int multicast_filter_limit = 128;
84 #define sis900_debug debug
85 static int sis900_debug;
87 /* Time in jiffies before concluding the transmitter is hung. */
88 #define TX_TIMEOUT (4*HZ)
89 /* SiS 900 is capable of 32 bits BM DMA */
90 #define SIS900_DMA_MASK 0xffffffff
96 static char * card_names[] = {
97 "SiS 900 PCI Fast Ethernet",
98 "SiS 7016 PCI Fast Ethernet"
100 static struct pci_device_id sis900_pci_tbl [] = {
101 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_900,
102 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_900},
103 {PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_7016,
104 PCI_ANY_ID, PCI_ANY_ID, 0, 0, SIS_7016},
107 MODULE_DEVICE_TABLE (pci, sis900_pci_tbl);
109 static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex);
111 static struct mii_chip_info {
119 } mii_chip_table[] = {
120 { "SiS 900 Internal MII PHY", 0x001d, 0x8000, LAN },
121 { "SiS 7014 Physical Layer Solution", 0x0016, 0xf830, LAN },
122 { "AMD 79C901 10BASE-T PHY", 0x0000, 0x6B70, LAN },
123 { "AMD 79C901 HomePNA PHY", 0x0000, 0x6B90, HOME},
124 { "ICS LAN PHY", 0x0015, 0xF440, LAN },
125 { "NS 83851 PHY", 0x2000, 0x5C20, MIX },
126 { "Realtek RTL8201 PHY", 0x0000, 0x8200, LAN },
127 { "VIA 6103 PHY", 0x0101, 0x8f20, LAN },
132 struct mii_phy * next;
140 typedef struct _BufferDesc {
146 struct sis900_private {
147 struct net_device_stats stats;
148 struct pci_dev * pci_dev;
152 struct mii_phy * mii;
153 struct mii_phy * first_mii; /* record the first mii structure */
154 unsigned int cur_phy;
156 struct timer_list timer; /* Link status detection timer. */
157 u8 autong_complete; /* 1: auto-negotiate complete */
159 unsigned int cur_rx, dirty_rx; /* producer/comsumer pointers for Tx/Rx ring */
160 unsigned int cur_tx, dirty_tx;
162 /* The saved address of a sent/receive-in-place packet buffer */
163 struct sk_buff *tx_skbuff[NUM_TX_DESC];
164 struct sk_buff *rx_skbuff[NUM_RX_DESC];
168 dma_addr_t tx_ring_dma;
169 dma_addr_t rx_ring_dma;
171 unsigned int tx_full; /* The Tx queue is full. */
176 MODULE_AUTHOR("Jim Huang <cmhuang@sis.com.tw>, Ollie Lho <ollie@sis.com.tw>");
177 MODULE_DESCRIPTION("SiS 900 PCI Fast Ethernet driver");
178 MODULE_LICENSE("GPL");
180 MODULE_PARM(multicast_filter_limit, "i");
181 MODULE_PARM(max_interrupt_work, "i");
182 MODULE_PARM(debug, "i");
183 MODULE_PARM_DESC(multicast_filter_limit, "SiS 900/7016 maximum number of filtered multicast addresses");
184 MODULE_PARM_DESC(max_interrupt_work, "SiS 900/7016 maximum events handled per interrupt");
185 MODULE_PARM_DESC(debug, "SiS 900/7016 debug level (2-4)");
187 static int sis900_open(struct net_device *net_dev);
188 static int sis900_mii_probe (struct net_device * net_dev);
189 static void sis900_init_rxfilter (struct net_device * net_dev);
190 static u16 read_eeprom(long ioaddr, int location);
191 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location);
192 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int val);
193 static void sis900_timer(unsigned long data);
194 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy);
195 static void sis900_tx_timeout(struct net_device *net_dev);
196 static void sis900_init_tx_ring(struct net_device *net_dev);
197 static void sis900_init_rx_ring(struct net_device *net_dev);
198 static int sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev);
199 static int sis900_rx(struct net_device *net_dev);
200 static void sis900_finish_xmit (struct net_device *net_dev);
201 static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs);
202 static int sis900_close(struct net_device *net_dev);
203 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd);
204 static struct net_device_stats *sis900_get_stats(struct net_device *net_dev);
205 static u16 sis900_mcast_bitnr(u8 *addr, u8 revision);
206 static void set_rx_mode(struct net_device *net_dev);
207 static void sis900_reset(struct net_device *net_dev);
208 static void sis630_set_eq(struct net_device *net_dev, u8 revision);
209 static int sis900_set_config(struct net_device *dev, struct ifmap *map);
210 static u16 sis900_default_phy(struct net_device * net_dev);
211 static void sis900_set_capability( struct net_device *net_dev ,struct mii_phy *phy);
212 static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr);
213 static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr);
214 static void sis900_set_mode (long ioaddr, int speed, int duplex);
215 static struct ethtool_ops sis900_ethtool_ops;
218 * sis900_get_mac_addr - Get MAC address for stand alone SiS900 model
219 * @pci_dev: the sis900 pci device
220 * @net_dev: the net device to get address for
222 * Older SiS900 and friends, use EEPROM to store MAC address.
223 * MAC address is read from read_eeprom() into @net_dev->dev_addr.
226 static int __devinit sis900_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
228 long ioaddr = pci_resource_start(pci_dev, 0);
232 /* check to see if we have sane EEPROM */
233 signature = (u16) read_eeprom(ioaddr, EEPROMSignature);
234 if (signature == 0xffff || signature == 0x0000) {
235 printk (KERN_INFO "%s: Error EERPOM read %x\n",
236 net_dev->name, signature);
240 /* get MAC address from EEPROM */
241 for (i = 0; i < 3; i++)
242 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
248 * sis630e_get_mac_addr - Get MAC address for SiS630E model
249 * @pci_dev: the sis900 pci device
250 * @net_dev: the net device to get address for
252 * SiS630E model, use APC CMOS RAM to store MAC address.
253 * APC CMOS RAM is accessed through ISA bridge.
254 * MAC address is read into @net_dev->dev_addr.
257 static int __devinit sis630e_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
259 struct pci_dev *isa_bridge = NULL;
263 if ((isa_bridge = pci_find_device(0x1039, 0x0008, isa_bridge)) == NULL) {
264 printk("%s: Can not find ISA bridge\n", net_dev->name);
267 pci_read_config_byte(isa_bridge, 0x48, ®);
268 pci_write_config_byte(isa_bridge, 0x48, reg | 0x40);
270 for (i = 0; i < 6; i++) {
271 outb(0x09 + i, 0x70);
272 ((u8 *)(net_dev->dev_addr))[i] = inb(0x71);
274 pci_write_config_byte(isa_bridge, 0x48, reg & ~0x40);
281 * sis635_get_mac_addr - Get MAC address for SIS635 model
282 * @pci_dev: the sis900 pci device
283 * @net_dev: the net device to get address for
285 * SiS635 model, set MAC Reload Bit to load Mac address from APC
286 * to rfdr. rfdr is accessed through rfcr. MAC address is read into
287 * @net_dev->dev_addr.
290 static int __devinit sis635_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
292 long ioaddr = net_dev->base_addr;
296 rfcrSave = inl(rfcr + ioaddr);
298 outl(rfcrSave | RELOAD, ioaddr + cr);
299 outl(0, ioaddr + cr);
301 /* disable packet filtering before setting filter */
302 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
304 /* load MAC addr to filter data register */
305 for (i = 0 ; i < 3 ; i++) {
306 outl((i << RFADDR_shift), ioaddr + rfcr);
307 *( ((u16 *)net_dev->dev_addr) + i) = inw(ioaddr + rfdr);
310 /* enable packet filtering */
311 outl(rfcrSave | RFEN, rfcr + ioaddr);
317 * sis96x_get_mac_addr - Get MAC address for SiS962 or SiS963 model
318 * @pci_dev: the sis900 pci device
319 * @net_dev: the net device to get address for
321 * SiS962 or SiS963 model, use EEPROM to store MAC address. And EEPROM
323 * LAN and 1394. When access EEPROM, send EEREQ signal to hardware first
324 * and wait for EEGNT. If EEGNT is ON, EEPROM is permitted to be access
325 * by LAN, otherwise is not. After MAC address is read from EEPROM, send
326 * EEDONE signal to refuse EEPROM access by LAN.
327 * The EEPROM map of SiS962 or SiS963 is different to SiS900.
328 * The signature field in SiS962 or SiS963 spec is meaningless.
329 * MAC address is read into @net_dev->dev_addr.
332 static int __devinit sis96x_get_mac_addr(struct pci_dev * pci_dev, struct net_device *net_dev)
334 long ioaddr = net_dev->base_addr;
335 long ee_addr = ioaddr + mear;
339 outl(EEREQ, ee_addr);
340 while(waittime < 2000) {
341 if(inl(ee_addr) & EEGNT) {
343 /* get MAC address from EEPROM */
344 for (i = 0; i < 3; i++)
345 ((u16 *)(net_dev->dev_addr))[i] = read_eeprom(ioaddr, i+EEPROMMACAddr);
347 outl(EEDONE, ee_addr);
354 outl(EEDONE, ee_addr);
359 * sis900_probe - Probe for sis900 device
360 * @pci_dev: the sis900 pci device
361 * @pci_id: the pci device ID
363 * Check and probe sis900 net device for @pci_dev.
364 * Get mac address according to the chip revision,
365 * and assign SiS900-specific entries in the device structure.
366 * ie: sis900_open(), sis900_start_xmit(), sis900_close(), etc.
369 static int __devinit sis900_probe (struct pci_dev *pci_dev, const struct pci_device_id *pci_id)
371 struct sis900_private *sis_priv;
372 struct net_device *net_dev;
379 char *card_name = card_names[pci_id->driver_data];
381 /* when built into the kernel, we only print version if device is found */
383 static int printed_version;
384 if (!printed_version++)
388 /* setup various bits in PCI command register */
389 ret = pci_enable_device(pci_dev);
392 i = pci_set_dma_mask(pci_dev, SIS900_DMA_MASK);
394 printk(KERN_ERR "sis900.c: architecture does not support"
395 "32bit PCI busmaster DMA\n");
399 pci_set_master(pci_dev);
401 net_dev = alloc_etherdev(sizeof(struct sis900_private));
404 SET_MODULE_OWNER(net_dev);
405 SET_NETDEV_DEV(net_dev, &pci_dev->dev);
407 /* We do a request_region() to register /proc/ioports info. */
408 ioaddr = pci_resource_start(pci_dev, 0);
409 ret = pci_request_regions(pci_dev, "sis900");
413 sis_priv = net_dev->priv;
414 net_dev->base_addr = ioaddr;
415 net_dev->irq = pci_dev->irq;
416 sis_priv->pci_dev = pci_dev;
417 spin_lock_init(&sis_priv->lock);
419 pci_set_drvdata(pci_dev, net_dev);
421 ring_space = pci_alloc_consistent(pci_dev, TX_TOTAL_SIZE, &ring_dma);
424 goto err_out_cleardev;
426 sis_priv->tx_ring = (BufferDesc *)ring_space;
427 sis_priv->tx_ring_dma = ring_dma;
429 ring_space = pci_alloc_consistent(pci_dev, RX_TOTAL_SIZE, &ring_dma);
434 sis_priv->rx_ring = (BufferDesc *)ring_space;
435 sis_priv->rx_ring_dma = ring_dma;
437 /* The SiS900-specific entries in the device structure. */
438 net_dev->open = &sis900_open;
439 net_dev->hard_start_xmit = &sis900_start_xmit;
440 net_dev->stop = &sis900_close;
441 net_dev->get_stats = &sis900_get_stats;
442 net_dev->set_config = &sis900_set_config;
443 net_dev->set_multicast_list = &set_rx_mode;
444 net_dev->do_ioctl = &mii_ioctl;
445 net_dev->tx_timeout = sis900_tx_timeout;
446 net_dev->watchdog_timeo = TX_TIMEOUT;
447 net_dev->ethtool_ops = &sis900_ethtool_ops;
449 ret = register_netdev(net_dev);
453 /* Get Mac address according to the chip revision */
454 pci_read_config_byte(pci_dev, PCI_CLASS_REVISION, &revision);
457 if (revision == SIS630E_900_REV)
458 ret = sis630e_get_mac_addr(pci_dev, net_dev);
459 else if ((revision > 0x81) && (revision <= 0x90) )
460 ret = sis635_get_mac_addr(pci_dev, net_dev);
461 else if (revision == SIS96x_900_REV)
462 ret = sis96x_get_mac_addr(pci_dev, net_dev);
464 ret = sis900_get_mac_addr(pci_dev, net_dev);
468 goto err_out_unregister;
471 /* 630ET : set the mii access mode as software-mode */
472 if (revision == SIS630ET_900_REV)
473 outl(ACCESSMODE | inl(ioaddr + cr), ioaddr + cr);
475 /* probe for mii transceiver */
476 if (sis900_mii_probe(net_dev) == 0) {
478 goto err_out_unregister;
481 /* save our host bridge revision */
482 dev = pci_find_device(PCI_VENDOR_ID_SI, PCI_DEVICE_ID_SI_630, NULL);
484 pci_read_config_byte(dev, PCI_CLASS_REVISION, &sis_priv->host_bridge_rev);
486 /* print some information about our NIC */
487 printk(KERN_INFO "%s: %s at %#lx, IRQ %d, ", net_dev->name,
488 card_name, ioaddr, net_dev->irq);
489 for (i = 0; i < 5; i++)
490 printk("%2.2x:", (u8)net_dev->dev_addr[i]);
491 printk("%2.2x.\n", net_dev->dev_addr[i]);
496 unregister_netdev(net_dev);
498 pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
499 sis_priv->rx_ring_dma);
501 pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
502 sis_priv->tx_ring_dma);
504 pci_set_drvdata(pci_dev, NULL);
505 pci_release_regions(pci_dev);
507 free_netdev(net_dev);
512 * sis900_mii_probe - Probe MII PHY for sis900
513 * @net_dev: the net device to probe for
515 * Search for total of 32 possible mii phy addresses.
516 * Identify and set current phy if found one,
517 * return error if it failed to found.
520 static int __init sis900_mii_probe (struct net_device * net_dev)
522 struct sis900_private * sis_priv = net_dev->priv;
523 u16 poll_bit = MII_STAT_LINK, status = 0;
524 unsigned long timeout = jiffies + 5 * HZ;
528 sis_priv->mii = NULL;
530 /* search for total of 32 possible mii phy addresses */
531 for (phy_addr = 0; phy_addr < 32; phy_addr++) {
532 struct mii_phy * mii_phy = NULL;
537 for(i = 0; i < 2; i++)
538 mii_status = mdio_read(net_dev, phy_addr, MII_STATUS);
540 if (mii_status == 0xffff || mii_status == 0x0000)
541 /* the mii is not accessible, try next one */
544 if ((mii_phy = kmalloc(sizeof(struct mii_phy), GFP_KERNEL)) == NULL) {
545 printk(KERN_INFO "Cannot allocate mem for struct mii_phy\n");
546 mii_phy = sis_priv->first_mii;
550 mii_phy = mii_phy->next;
556 mii_phy->phy_id0 = mdio_read(net_dev, phy_addr, MII_PHY_ID0);
557 mii_phy->phy_id1 = mdio_read(net_dev, phy_addr, MII_PHY_ID1);
558 mii_phy->phy_addr = phy_addr;
559 mii_phy->status = mii_status;
560 mii_phy->next = sis_priv->mii;
561 sis_priv->mii = mii_phy;
562 sis_priv->first_mii = mii_phy;
564 for (i = 0; mii_chip_table[i].phy_id1; i++)
565 if ((mii_phy->phy_id0 == mii_chip_table[i].phy_id0 ) &&
566 ((mii_phy->phy_id1 & 0xFFF0) == mii_chip_table[i].phy_id1)){
567 mii_phy->phy_types = mii_chip_table[i].phy_types;
568 if (mii_chip_table[i].phy_types == MIX)
570 (mii_status & (MII_STAT_CAN_TX_FDX | MII_STAT_CAN_TX)) ? LAN : HOME;
571 printk(KERN_INFO "%s: %s transceiver found at address %d.\n",
572 net_dev->name, mii_chip_table[i].name, phy_addr);
576 if( !mii_chip_table[i].phy_id1 )
577 printk(KERN_INFO "%s: Unknown PHY transceiver found at address %d.\n",
578 net_dev->name, phy_addr);
581 if (sis_priv->mii == NULL) {
582 printk(KERN_INFO "%s: No MII transceivers found!\n",
587 /* select default PHY for mac */
588 sis_priv->mii = NULL;
589 sis900_default_phy( net_dev );
591 /* Reset phy if default phy is internal sis900 */
592 if ((sis_priv->mii->phy_id0 == 0x001D) &&
593 ((sis_priv->mii->phy_id1&0xFFF0) == 0x8000))
594 status = sis900_reset_phy(net_dev, sis_priv->cur_phy);
596 /* workaround for ICS1893 PHY */
597 if ((sis_priv->mii->phy_id0 == 0x0015) &&
598 ((sis_priv->mii->phy_id1&0xFFF0) == 0xF440))
599 mdio_write(net_dev, sis_priv->cur_phy, 0x0018, 0xD200);
601 if(status & MII_STAT_LINK){
605 poll_bit ^= (mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS) & poll_bit);
606 if (time_after_eq(jiffies, timeout)) {
607 printk(KERN_WARNING "%s: reset phy and link down now\n", net_dev->name);
613 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
614 if (revision == SIS630E_900_REV) {
615 /* SiS 630E has some bugs on default value of PHY registers */
616 mdio_write(net_dev, sis_priv->cur_phy, MII_ANADV, 0x05e1);
617 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG1, 0x22);
618 mdio_write(net_dev, sis_priv->cur_phy, MII_CONFIG2, 0xff00);
619 mdio_write(net_dev, sis_priv->cur_phy, MII_MASK, 0xffc0);
620 //mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, 0x1000);
623 if (sis_priv->mii->status & MII_STAT_LINK)
624 netif_carrier_on(net_dev);
626 netif_carrier_off(net_dev);
632 * sis900_default_phy - Select default PHY for sis900 mac.
633 * @net_dev: the net device to probe for
635 * Select first detected PHY with link as default.
636 * If no one is link on, select PHY whose types is HOME as default.
637 * If HOME doesn't exist, select LAN.
640 static u16 sis900_default_phy(struct net_device * net_dev)
642 struct sis900_private * sis_priv = net_dev->priv;
643 struct mii_phy *phy = NULL, *phy_home = NULL, *default_phy = NULL;
646 for( phy=sis_priv->first_mii; phy; phy=phy->next ){
647 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
648 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
650 /* Link ON & Not select deafalut PHY */
651 if ( (status & MII_STAT_LINK) && !(default_phy) )
654 status = mdio_read(net_dev, phy->phy_addr, MII_CONTROL);
655 mdio_write(net_dev, phy->phy_addr, MII_CONTROL,
656 status | MII_CNTL_AUTO | MII_CNTL_ISOLATE);
657 if( phy->phy_types == HOME )
662 if( (!default_phy) && phy_home )
663 default_phy = phy_home;
664 else if(!default_phy)
665 default_phy = sis_priv->first_mii;
667 if( sis_priv->mii != default_phy ){
668 sis_priv->mii = default_phy;
669 sis_priv->cur_phy = default_phy->phy_addr;
670 printk(KERN_INFO "%s: Using transceiver found at address %d as default\n", net_dev->name,sis_priv->cur_phy);
673 status = mdio_read(net_dev, sis_priv->cur_phy, MII_CONTROL);
674 status &= (~MII_CNTL_ISOLATE);
676 mdio_write(net_dev, sis_priv->cur_phy, MII_CONTROL, status);
677 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
678 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
685 * sis900_set_capability - set the media capability of network adapter.
686 * @net_dev : the net device to probe for
689 * Set the media capability of network adapter according to
690 * mii status register. It's necessary before auto-negotiate.
693 static void sis900_set_capability( struct net_device *net_dev , struct mii_phy *phy )
698 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
699 status = mdio_read(net_dev, phy->phy_addr, MII_STATUS);
701 cap = MII_NWAY_CSMA_CD |
702 ((phy->status & MII_STAT_CAN_TX_FDX)? MII_NWAY_TX_FDX:0) |
703 ((phy->status & MII_STAT_CAN_TX) ? MII_NWAY_TX:0) |
704 ((phy->status & MII_STAT_CAN_T_FDX) ? MII_NWAY_T_FDX:0)|
705 ((phy->status & MII_STAT_CAN_T) ? MII_NWAY_T:0);
707 mdio_write(net_dev, phy->phy_addr, MII_ANADV, cap);
711 /* Delay between EEPROM clock transitions. */
712 #define eeprom_delay() inl(ee_addr)
715 * read_eeprom - Read Serial EEPROM
716 * @ioaddr: base i/o address
717 * @location: the EEPROM location to read
719 * Read Serial EEPROM through EEPROM Access Register.
720 * Note that location is in word (16 bits) unit
723 static u16 __devinit read_eeprom(long ioaddr, int location)
727 long ee_addr = ioaddr + mear;
728 u32 read_cmd = location | EEread;
735 /* Shift the read command (9) bits out. */
736 for (i = 8; i >= 0; i--) {
737 u32 dataval = (read_cmd & (1 << i)) ? EEDI | EECS : EECS;
738 outl(dataval, ee_addr);
740 outl(dataval | EECLK, ee_addr);
746 /* read the 16-bits data in */
747 for (i = 16; i > 0; i--) {
750 outl(EECS | EECLK, ee_addr);
752 retval = (retval << 1) | ((inl(ee_addr) & EEDO) ? 1 : 0);
756 /* Terminate the EEPROM access. */
763 /* Read and write the MII management registers using software-generated
764 serial MDIO protocol. Note that the command bits and data bits are
765 send out separately */
766 #define mdio_delay() inl(mdio_addr)
768 static void mdio_idle(long mdio_addr)
770 outl(MDIO | MDDIR, mdio_addr);
772 outl(MDIO | MDDIR | MDC, mdio_addr);
775 /* Syncronize the MII management interface by shifting 32 one bits out. */
776 static void mdio_reset(long mdio_addr)
780 for (i = 31; i >= 0; i--) {
781 outl(MDDIR | MDIO, mdio_addr);
783 outl(MDDIR | MDIO | MDC, mdio_addr);
790 * mdio_read - read MII PHY register
791 * @net_dev: the net device to read
792 * @phy_id: the phy address to read
793 * @location: the phy regiester id to read
795 * Read MII registers through MDIO and MDC
796 * using MDIO management frame structure and protocol(defined by ISO/IEC).
797 * Please see SiS7014 or ICS spec
800 static u16 mdio_read(struct net_device *net_dev, int phy_id, int location)
802 long mdio_addr = net_dev->base_addr + mear;
803 int mii_cmd = MIIread|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
807 mdio_reset(mdio_addr);
808 mdio_idle(mdio_addr);
810 for (i = 15; i >= 0; i--) {
811 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
812 outl(dataval, mdio_addr);
814 outl(dataval | MDC, mdio_addr);
818 /* Read the 16 data bits. */
819 for (i = 16; i > 0; i--) {
822 retval = (retval << 1) | ((inl(mdio_addr) & MDIO) ? 1 : 0);
823 outl(MDC, mdio_addr);
826 outl(0x00, mdio_addr);
832 * mdio_write - write MII PHY register
833 * @net_dev: the net device to write
834 * @phy_id: the phy address to write
835 * @location: the phy regiester id to write
836 * @value: the register value to write with
838 * Write MII registers with @value through MDIO and MDC
839 * using MDIO management frame structure and protocol(defined by ISO/IEC)
840 * please see SiS7014 or ICS spec
843 static void mdio_write(struct net_device *net_dev, int phy_id, int location, int value)
845 long mdio_addr = net_dev->base_addr + mear;
846 int mii_cmd = MIIwrite|(phy_id<<MIIpmdShift)|(location<<MIIregShift);
849 mdio_reset(mdio_addr);
850 mdio_idle(mdio_addr);
852 /* Shift the command bits out. */
853 for (i = 15; i >= 0; i--) {
854 int dataval = (mii_cmd & (1 << i)) ? MDDIR | MDIO : MDDIR;
855 outb(dataval, mdio_addr);
857 outb(dataval | MDC, mdio_addr);
862 /* Shift the value bits out. */
863 for (i = 15; i >= 0; i--) {
864 int dataval = (value & (1 << i)) ? MDDIR | MDIO : MDDIR;
865 outl(dataval, mdio_addr);
867 outl(dataval | MDC, mdio_addr);
872 /* Clear out extra bits. */
873 for (i = 2; i > 0; i--) {
876 outb(MDC, mdio_addr);
879 outl(0x00, mdio_addr);
886 * sis900_reset_phy - reset sis900 mii phy.
887 * @net_dev: the net device to write
888 * @phy_addr: default phy address
890 * Some specific phy can't work properly without reset.
891 * This function will be called during initialization and
892 * link status change from ON to DOWN.
895 static u16 sis900_reset_phy(struct net_device *net_dev, int phy_addr)
901 status = mdio_read(net_dev, phy_addr, MII_STATUS);
903 mdio_write( net_dev, phy_addr, MII_CONTROL, MII_CNTL_RESET );
909 * sis900_open - open sis900 device
910 * @net_dev: the net device to open
912 * Do some initialization and start net interface.
913 * enable interrupts and set sis900 timer.
917 sis900_open(struct net_device *net_dev)
919 struct sis900_private *sis_priv = net_dev->priv;
920 long ioaddr = net_dev->base_addr;
924 /* Soft reset the chip. */
925 sis900_reset(net_dev);
927 /* Equalizer workaround Rule */
928 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
929 sis630_set_eq(net_dev, revision);
931 ret = request_irq(net_dev->irq, &sis900_interrupt, SA_SHIRQ, net_dev->name, net_dev);
935 sis900_init_rxfilter(net_dev);
937 sis900_init_tx_ring(net_dev);
938 sis900_init_rx_ring(net_dev);
940 set_rx_mode(net_dev);
942 netif_start_queue(net_dev);
944 /* Workaround for EDB */
945 sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
947 /* Enable all known interrupts by setting the interrupt mask. */
948 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
949 outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
950 outl(IE, ioaddr + ier);
952 sis900_check_mode(net_dev, sis_priv->mii);
954 /* Set the timer to switch to check for link beat and perhaps switch
955 to an alternate media type. */
956 init_timer(&sis_priv->timer);
957 sis_priv->timer.expires = jiffies + HZ;
958 sis_priv->timer.data = (unsigned long)net_dev;
959 sis_priv->timer.function = &sis900_timer;
960 add_timer(&sis_priv->timer);
966 * sis900_init_rxfilter - Initialize the Rx filter
967 * @net_dev: the net device to initialize for
969 * Set receive filter address to our MAC address
970 * and enable packet filtering.
974 sis900_init_rxfilter (struct net_device * net_dev)
976 long ioaddr = net_dev->base_addr;
980 rfcrSave = inl(rfcr + ioaddr);
982 /* disable packet filtering before setting filter */
983 outl(rfcrSave & ~RFEN, rfcr + ioaddr);
985 /* load MAC addr to filter data register */
986 for (i = 0 ; i < 3 ; i++) {
989 w = (u32) *((u16 *)(net_dev->dev_addr)+i);
990 outl((i << RFADDR_shift), ioaddr + rfcr);
991 outl(w, ioaddr + rfdr);
993 if (sis900_debug > 2) {
994 printk(KERN_INFO "%s: Receive Filter Addrss[%d]=%x\n",
995 net_dev->name, i, inl(ioaddr + rfdr));
999 /* enable packet filtering */
1000 outl(rfcrSave | RFEN, rfcr + ioaddr);
1004 * sis900_init_tx_ring - Initialize the Tx descriptor ring
1005 * @net_dev: the net device to initialize for
1007 * Initialize the Tx descriptor ring,
1011 sis900_init_tx_ring(struct net_device *net_dev)
1013 struct sis900_private *sis_priv = net_dev->priv;
1014 long ioaddr = net_dev->base_addr;
1017 sis_priv->tx_full = 0;
1018 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1020 for (i = 0; i < NUM_TX_DESC; i++) {
1021 sis_priv->tx_skbuff[i] = NULL;
1023 sis_priv->tx_ring[i].link = sis_priv->tx_ring_dma +
1024 ((i+1)%NUM_TX_DESC)*sizeof(BufferDesc);
1025 sis_priv->tx_ring[i].cmdsts = 0;
1026 sis_priv->tx_ring[i].bufptr = 0;
1029 /* load Transmit Descriptor Register */
1030 outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1031 if (sis900_debug > 2)
1032 printk(KERN_INFO "%s: TX descriptor register loaded with: %8.8x\n",
1033 net_dev->name, inl(ioaddr + txdp));
1037 * sis900_init_rx_ring - Initialize the Rx descriptor ring
1038 * @net_dev: the net device to initialize for
1040 * Initialize the Rx descriptor ring,
1041 * and pre-allocate recevie buffers (socket buffer)
1045 sis900_init_rx_ring(struct net_device *net_dev)
1047 struct sis900_private *sis_priv = net_dev->priv;
1048 long ioaddr = net_dev->base_addr;
1051 sis_priv->cur_rx = 0;
1052 sis_priv->dirty_rx = 0;
1054 /* init RX descriptor */
1055 for (i = 0; i < NUM_RX_DESC; i++) {
1056 sis_priv->rx_skbuff[i] = NULL;
1058 sis_priv->rx_ring[i].link = sis_priv->rx_ring_dma +
1059 ((i+1)%NUM_RX_DESC)*sizeof(BufferDesc);
1060 sis_priv->rx_ring[i].cmdsts = 0;
1061 sis_priv->rx_ring[i].bufptr = 0;
1064 /* allocate sock buffers */
1065 for (i = 0; i < NUM_RX_DESC; i++) {
1066 struct sk_buff *skb;
1068 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1069 /* not enough memory for skbuff, this makes a "hole"
1070 on the buffer ring, it is not clear how the
1071 hardware will react to this kind of degenerated
1076 sis_priv->rx_skbuff[i] = skb;
1077 sis_priv->rx_ring[i].cmdsts = RX_BUF_SIZE;
1078 sis_priv->rx_ring[i].bufptr = pci_map_single(sis_priv->pci_dev,
1079 skb->tail, RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1081 sis_priv->dirty_rx = (unsigned int) (i - NUM_RX_DESC);
1083 /* load Receive Descriptor Register */
1084 outl(sis_priv->rx_ring_dma, ioaddr + rxdp);
1085 if (sis900_debug > 2)
1086 printk(KERN_INFO "%s: RX descriptor register loaded with: %8.8x\n",
1087 net_dev->name, inl(ioaddr + rxdp));
1091 * sis630_set_eq - set phy equalizer value for 630 LAN
1092 * @net_dev: the net device to set equalizer value
1093 * @revision: 630 LAN revision number
1095 * 630E equalizer workaround rule(Cyrus Huang 08/15)
1096 * PHY register 14h(Test)
1097 * Bit 14: 0 -- Automatically dectect (default)
1098 * 1 -- Manually set Equalizer filter
1099 * Bit 13: 0 -- (Default)
1100 * 1 -- Speed up convergence of equalizer setting
1101 * Bit 9 : 0 -- (Default)
1102 * 1 -- Disable Baseline Wander
1103 * Bit 3~7 -- Equalizer filter setting
1104 * Link ON: Set Bit 9, 13 to 1, Bit 14 to 0
1105 * Then calculate equalizer value
1106 * Then set equalizer value, and set Bit 14 to 1, Bit 9 to 0
1107 * Link Off:Set Bit 13 to 1, Bit 14 to 0
1108 * Calculate Equalizer value:
1109 * When Link is ON and Bit 14 is 0, SIS900PHY will auto-dectect proper equalizer value.
1110 * When the equalizer is stable, this value is not a fixed value. It will be within
1111 * a small range(eg. 7~9). Then we get a minimum and a maximum value(eg. min=7, max=9)
1112 * 0 <= max <= 4 --> set equalizer to max
1113 * 5 <= max <= 14 --> set equalizer to max+1 or set equalizer to max+2 if max == min
1114 * max >= 15 --> set equalizer to max+5 or set equalizer to max+6 if max == min
1117 static void sis630_set_eq(struct net_device *net_dev, u8 revision)
1119 struct sis900_private *sis_priv = net_dev->priv;
1120 u16 reg14h, eq_value=0, max_value=0, min_value=0;
1123 if ( !(revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1124 revision == SIS630A_900_REV || revision == SIS630ET_900_REV) )
1127 if (netif_carrier_ok(net_dev)) {
1128 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1129 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (0x2200 | reg14h) & 0xBFFF);
1130 for (i=0; i < maxcount; i++) {
1131 eq_value=(0x00F8 & mdio_read(net_dev, sis_priv->cur_phy, MII_RESV)) >> 3;
1133 max_value=min_value=eq_value;
1134 max_value=(eq_value > max_value) ? eq_value : max_value;
1135 min_value=(eq_value < min_value) ? eq_value : min_value;
1137 /* 630E rule to determine the equalizer value */
1138 if (revision == SIS630E_900_REV || revision == SIS630EA1_900_REV ||
1139 revision == SIS630ET_900_REV) {
1142 else if (max_value >= 5 && max_value < 15)
1143 eq_value=(max_value == min_value) ? max_value+2 : max_value+1;
1144 else if (max_value >= 15)
1145 eq_value=(max_value == min_value) ? max_value+6 : max_value+5;
1147 /* 630B0&B1 rule to determine the equalizer value */
1148 if (revision == SIS630A_900_REV &&
1149 (sis_priv->host_bridge_rev == SIS630B0 ||
1150 sis_priv->host_bridge_rev == SIS630B1)) {
1154 eq_value=(max_value+min_value+1)/2;
1156 /* write equalizer value and setting */
1157 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1158 reg14h=(reg14h & 0xFF07) | ((eq_value << 3) & 0x00F8);
1159 reg14h=(reg14h | 0x6000) & 0xFDFF;
1160 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, reg14h);
1163 reg14h=mdio_read(net_dev, sis_priv->cur_phy, MII_RESV);
1164 if (revision == SIS630A_900_REV &&
1165 (sis_priv->host_bridge_rev == SIS630B0 ||
1166 sis_priv->host_bridge_rev == SIS630B1))
1167 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2200) & 0xBFFF);
1169 mdio_write(net_dev, sis_priv->cur_phy, MII_RESV, (reg14h | 0x2000) & 0xBFFF);
1175 * sis900_timer - sis900 timer routine
1176 * @data: pointer to sis900 net device
1178 * On each timer ticks we check two things,
1179 * link status (ON/OFF) and link mode (10/100/Full/Half)
1182 static void sis900_timer(unsigned long data)
1184 struct net_device *net_dev = (struct net_device *)data;
1185 struct sis900_private *sis_priv = net_dev->priv;
1186 struct mii_phy *mii_phy = sis_priv->mii;
1187 static int next_tick = 5*HZ;
1191 if (!sis_priv->autong_complete){
1192 int speed, duplex = 0;
1194 sis900_read_mode(net_dev, &speed, &duplex);
1196 sis900_set_mode(net_dev->base_addr, speed, duplex);
1197 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1198 sis630_set_eq(net_dev, revision);
1199 netif_start_queue(net_dev);
1202 sis_priv->timer.expires = jiffies + HZ;
1203 add_timer(&sis_priv->timer);
1207 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1208 status = mdio_read(net_dev, sis_priv->cur_phy, MII_STATUS);
1210 /* Link OFF -> ON */
1211 if (!netif_carrier_ok(net_dev)) {
1213 /* Search for new PHY */
1214 status = sis900_default_phy(net_dev);
1215 mii_phy = sis_priv->mii;
1217 if (status & MII_STAT_LINK){
1218 sis900_check_mode(net_dev, mii_phy);
1219 netif_carrier_on(net_dev);
1222 /* Link ON -> OFF */
1224 if (!(status & MII_STAT_LINK)){
1225 netif_carrier_off(net_dev);
1226 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1228 /* Change mode issue */
1229 if ((mii_phy->phy_id0 == 0x001D) &&
1230 ((mii_phy->phy_id1 & 0xFFF0) == 0x8000))
1231 sis900_reset_phy(net_dev, sis_priv->cur_phy);
1233 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
1234 sis630_set_eq(net_dev, revision);
1240 sis_priv->timer.expires = jiffies + next_tick;
1241 add_timer(&sis_priv->timer);
1245 * sis900_check_mode - check the media mode for sis900
1246 * @net_dev: the net device to be checked
1247 * @mii_phy: the mii phy
1249 * Older driver gets the media mode from mii status output
1250 * register. Now we set our media capability and auto-negotiate
1251 * to get the upper bound of speed and duplex between two ends.
1252 * If the types of mii phy is HOME, it doesn't need to auto-negotiate
1253 * and autong_complete should be set to 1.
1256 static void sis900_check_mode (struct net_device *net_dev, struct mii_phy *mii_phy)
1258 struct sis900_private *sis_priv = net_dev->priv;
1259 long ioaddr = net_dev->base_addr;
1262 if( mii_phy->phy_types == LAN ){
1263 outl( ~EXD & inl( ioaddr + cfg ), ioaddr + cfg);
1264 sis900_set_capability(net_dev , mii_phy);
1265 sis900_auto_negotiate(net_dev, sis_priv->cur_phy);
1267 outl(EXD | inl( ioaddr + cfg ), ioaddr + cfg);
1268 speed = HW_SPEED_HOME;
1269 duplex = FDX_CAPABLE_HALF_SELECTED;
1270 sis900_set_mode(ioaddr, speed, duplex);
1271 sis_priv->autong_complete = 1;
1276 * sis900_set_mode - Set the media mode of mac register.
1277 * @ioaddr: the address of the device
1278 * @speed : the transmit speed to be determined
1279 * @duplex: the duplex mode to be determined
1281 * Set the media mode of mac register txcfg/rxcfg according to
1282 * speed and duplex of phy. Bit EDB_MASTER_EN indicates the EDB
1283 * bus is used instead of PCI bus. When this bit is set 1, the
1284 * Max DMA Burst Size for TX/RX DMA should be no larger than 16
1288 static void sis900_set_mode (long ioaddr, int speed, int duplex)
1290 u32 tx_flags = 0, rx_flags = 0;
1292 if( inl(ioaddr + cfg) & EDB_MASTER_EN ){
1293 tx_flags = TxATP | (DMA_BURST_64 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1294 rx_flags = DMA_BURST_64 << RxMXDMA_shift;
1297 tx_flags = TxATP | (DMA_BURST_512 << TxMXDMA_shift) | (TX_FILL_THRESH << TxFILLT_shift);
1298 rx_flags = DMA_BURST_512 << RxMXDMA_shift;
1301 if (speed == HW_SPEED_HOME || speed == HW_SPEED_10_MBPS ) {
1302 rx_flags |= (RxDRNT_10 << RxDRNT_shift);
1303 tx_flags |= (TxDRNT_10 << TxDRNT_shift);
1306 rx_flags |= (RxDRNT_100 << RxDRNT_shift);
1307 tx_flags |= (TxDRNT_100 << TxDRNT_shift);
1310 if (duplex == FDX_CAPABLE_FULL_SELECTED) {
1311 tx_flags |= (TxCSI | TxHBI);
1315 outl (tx_flags, ioaddr + txcfg);
1316 outl (rx_flags, ioaddr + rxcfg);
1320 * sis900_auto_negotiate - Set the Auto-Negotiation Enable/Reset bit.
1321 * @net_dev: the net device to read mode for
1322 * @phy_addr: mii phy address
1324 * If the adapter is link-on, set the auto-negotiate enable/reset bit.
1325 * autong_complete should be set to 0 when starting auto-negotiation.
1326 * autong_complete should be set to 1 if we didn't start auto-negotiation.
1327 * sis900_timer will wait for link on again if autong_complete = 0.
1330 static void sis900_auto_negotiate(struct net_device *net_dev, int phy_addr)
1332 struct sis900_private *sis_priv = net_dev->priv;
1337 status = mdio_read(net_dev, phy_addr, MII_STATUS);
1339 if (!(status & MII_STAT_LINK)){
1340 printk(KERN_INFO "%s: Media Link Off\n", net_dev->name);
1341 sis_priv->autong_complete = 1;
1342 netif_carrier_off(net_dev);
1346 /* (Re)start AutoNegotiate */
1347 mdio_write(net_dev, phy_addr, MII_CONTROL,
1348 MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1349 sis_priv->autong_complete = 0;
1354 * sis900_read_mode - read media mode for sis900 internal phy
1355 * @net_dev: the net device to read mode for
1356 * @speed : the transmit speed to be determined
1357 * @duplex : the duplex mode to be determined
1359 * The capability of remote end will be put in mii register autorec
1360 * after auto-negotiation. Use AND operation to get the upper bound
1361 * of speed and duplex between two ends.
1364 static void sis900_read_mode(struct net_device *net_dev, int *speed, int *duplex)
1366 struct sis900_private *sis_priv = net_dev->priv;
1367 struct mii_phy *phy = sis_priv->mii;
1368 int phy_addr = sis_priv->cur_phy;
1370 u16 autoadv, autorec;
1374 status = mdio_read(net_dev, phy_addr, MII_STATUS);
1376 if (!(status & MII_STAT_LINK))
1379 /* AutoNegotiate completed */
1380 autoadv = mdio_read(net_dev, phy_addr, MII_ANADV);
1381 autorec = mdio_read(net_dev, phy_addr, MII_ANLPAR);
1382 status = autoadv & autorec;
1384 *speed = HW_SPEED_10_MBPS;
1385 *duplex = FDX_CAPABLE_HALF_SELECTED;
1387 if (status & (MII_NWAY_TX | MII_NWAY_TX_FDX))
1388 *speed = HW_SPEED_100_MBPS;
1389 if (status & ( MII_NWAY_TX_FDX | MII_NWAY_T_FDX))
1390 *duplex = FDX_CAPABLE_FULL_SELECTED;
1392 sis_priv->autong_complete = 1;
1394 /* Workaround for Realtek RTL8201 PHY issue */
1395 if((phy->phy_id0 == 0x0000) && ((phy->phy_id1 & 0xFFF0) == 0x8200)){
1396 if(mdio_read(net_dev, phy_addr, MII_CONTROL) & MII_CNTL_FDX)
1397 *duplex = FDX_CAPABLE_FULL_SELECTED;
1398 if(mdio_read(net_dev, phy_addr, 0x0019) & 0x01)
1399 *speed = HW_SPEED_100_MBPS;
1402 printk(KERN_INFO "%s: Media Link On %s %s-duplex \n",
1404 *speed == HW_SPEED_100_MBPS ?
1405 "100mbps" : "10mbps",
1406 *duplex == FDX_CAPABLE_FULL_SELECTED ?
1411 * sis900_tx_timeout - sis900 transmit timeout routine
1412 * @net_dev: the net device to transmit
1414 * print transmit timeout status
1415 * disable interrupts and do some tasks
1418 static void sis900_tx_timeout(struct net_device *net_dev)
1420 struct sis900_private *sis_priv = net_dev->priv;
1421 long ioaddr = net_dev->base_addr;
1422 unsigned long flags;
1425 printk(KERN_INFO "%s: Transmit timeout, status %8.8x %8.8x \n",
1426 net_dev->name, inl(ioaddr + cr), inl(ioaddr + isr));
1428 /* Disable interrupts by clearing the interrupt mask. */
1429 outl(0x0000, ioaddr + imr);
1431 /* use spinlock to prevent interrupt handler accessing buffer ring */
1432 spin_lock_irqsave(&sis_priv->lock, flags);
1434 /* discard unsent packets */
1435 sis_priv->dirty_tx = sis_priv->cur_tx = 0;
1436 for (i = 0; i < NUM_TX_DESC; i++) {
1437 struct sk_buff *skb = sis_priv->tx_skbuff[i];
1440 pci_unmap_single(sis_priv->pci_dev,
1441 sis_priv->tx_ring[i].bufptr, skb->len,
1443 dev_kfree_skb_irq(skb);
1444 sis_priv->tx_skbuff[i] = 0;
1445 sis_priv->tx_ring[i].cmdsts = 0;
1446 sis_priv->tx_ring[i].bufptr = 0;
1447 sis_priv->stats.tx_dropped++;
1450 sis_priv->tx_full = 0;
1451 netif_wake_queue(net_dev);
1453 spin_unlock_irqrestore(&sis_priv->lock, flags);
1455 net_dev->trans_start = jiffies;
1457 /* load Transmit Descriptor Register */
1458 outl(sis_priv->tx_ring_dma, ioaddr + txdp);
1460 /* Enable all known interrupts by setting the interrupt mask. */
1461 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
1466 * sis900_start_xmit - sis900 start transmit routine
1467 * @skb: socket buffer pointer to put the data being transmitted
1468 * @net_dev: the net device to transmit with
1470 * Set the transmit buffer descriptor,
1471 * and write TxENA to enable transmit state machine.
1472 * tell upper layer if the buffer is full
1476 sis900_start_xmit(struct sk_buff *skb, struct net_device *net_dev)
1478 struct sis900_private *sis_priv = net_dev->priv;
1479 long ioaddr = net_dev->base_addr;
1481 unsigned long flags;
1482 unsigned int index_cur_tx, index_dirty_tx;
1483 unsigned int count_dirty_tx;
1485 /* Don't transmit data before the complete of auto-negotiation */
1486 if(!sis_priv->autong_complete){
1487 netif_stop_queue(net_dev);
1491 spin_lock_irqsave(&sis_priv->lock, flags);
1493 /* Calculate the next Tx descriptor entry. */
1494 entry = sis_priv->cur_tx % NUM_TX_DESC;
1495 sis_priv->tx_skbuff[entry] = skb;
1497 /* set the transmit buffer descriptor and enable Transmit State Machine */
1498 sis_priv->tx_ring[entry].bufptr = pci_map_single(sis_priv->pci_dev,
1499 skb->data, skb->len, PCI_DMA_TODEVICE);
1500 sis_priv->tx_ring[entry].cmdsts = (OWN | skb->len);
1501 outl(TxENA | inl(ioaddr + cr), ioaddr + cr);
1503 sis_priv->cur_tx ++;
1504 index_cur_tx = sis_priv->cur_tx;
1505 index_dirty_tx = sis_priv->dirty_tx;
1507 for (count_dirty_tx = 0; index_cur_tx != index_dirty_tx; index_dirty_tx++)
1510 if (index_cur_tx == index_dirty_tx) {
1511 /* dirty_tx is met in the cycle of cur_tx, buffer full */
1512 sis_priv->tx_full = 1;
1513 netif_stop_queue(net_dev);
1514 } else if (count_dirty_tx < NUM_TX_DESC) {
1515 /* Typical path, tell upper layer that more transmission is possible */
1516 netif_start_queue(net_dev);
1518 /* buffer full, tell upper layer no more transmission */
1519 sis_priv->tx_full = 1;
1520 netif_stop_queue(net_dev);
1523 spin_unlock_irqrestore(&sis_priv->lock, flags);
1525 net_dev->trans_start = jiffies;
1527 if (sis900_debug > 3)
1528 printk(KERN_INFO "%s: Queued Tx packet at %p size %d "
1530 net_dev->name, skb->data, (int)skb->len, entry);
1536 * sis900_interrupt - sis900 interrupt handler
1537 * @irq: the irq number
1538 * @dev_instance: the client data object
1539 * @regs: snapshot of processor context
1541 * The interrupt handler does all of the Rx thread work,
1542 * and cleans up after the Tx thread
1545 static irqreturn_t sis900_interrupt(int irq, void *dev_instance, struct pt_regs *regs)
1547 struct net_device *net_dev = dev_instance;
1548 struct sis900_private *sis_priv = net_dev->priv;
1549 int boguscnt = max_interrupt_work;
1550 long ioaddr = net_dev->base_addr;
1552 unsigned int handled = 0;
1554 spin_lock (&sis_priv->lock);
1557 status = inl(ioaddr + isr);
1559 if ((status & (HIBERR|TxURN|TxERR|TxIDLE|RxORN|RxERR|RxOK)) == 0)
1560 /* nothing intresting happened */
1564 /* why dow't we break after Tx/Rx case ?? keyword: full-duplex */
1565 if (status & (RxORN | RxERR | RxOK))
1569 if (status & (TxURN | TxERR | TxIDLE))
1571 sis900_finish_xmit(net_dev);
1573 /* something strange happened !!! */
1574 if (status & HIBERR) {
1575 printk(KERN_INFO "%s: Abnormal interrupt,"
1576 "status %#8.8x.\n", net_dev->name, status);
1579 if (--boguscnt < 0) {
1580 printk(KERN_INFO "%s: Too much work at interrupt, "
1581 "interrupt status = %#8.8x.\n",
1582 net_dev->name, status);
1587 if (sis900_debug > 3)
1588 printk(KERN_INFO "%s: exiting interrupt, "
1589 "interrupt status = 0x%#8.8x.\n",
1590 net_dev->name, inl(ioaddr + isr));
1592 spin_unlock (&sis_priv->lock);
1593 return IRQ_RETVAL(handled);
1597 * sis900_rx - sis900 receive routine
1598 * @net_dev: the net device which receives data
1600 * Process receive interrupt events,
1601 * put buffer to higher layer and refill buffer pool
1602 * Note: This fucntion is called by interrupt handler,
1603 * don't do "too much" work here
1606 static int sis900_rx(struct net_device *net_dev)
1608 struct sis900_private *sis_priv = net_dev->priv;
1609 long ioaddr = net_dev->base_addr;
1610 unsigned int entry = sis_priv->cur_rx % NUM_RX_DESC;
1611 u32 rx_status = sis_priv->rx_ring[entry].cmdsts;
1613 if (sis900_debug > 3)
1614 printk(KERN_INFO "sis900_rx, cur_rx:%4.4d, dirty_rx:%4.4d "
1616 sis_priv->cur_rx, sis_priv->dirty_rx, rx_status);
1618 while (rx_status & OWN) {
1619 unsigned int rx_size;
1621 rx_size = (rx_status & DSIZE) - CRC_SIZE;
1623 if (rx_status & (ABORT|OVERRUN|TOOLONG|RUNT|RXISERR|CRCERR|FAERR)) {
1624 /* corrupted packet received */
1625 if (sis900_debug > 3)
1626 printk(KERN_INFO "%s: Corrupted packet "
1627 "received, buffer status = 0x%8.8x.\n",
1628 net_dev->name, rx_status);
1629 sis_priv->stats.rx_errors++;
1630 if (rx_status & OVERRUN)
1631 sis_priv->stats.rx_over_errors++;
1632 if (rx_status & (TOOLONG|RUNT))
1633 sis_priv->stats.rx_length_errors++;
1634 if (rx_status & (RXISERR | FAERR))
1635 sis_priv->stats.rx_frame_errors++;
1636 if (rx_status & CRCERR)
1637 sis_priv->stats.rx_crc_errors++;
1638 /* reset buffer descriptor state */
1639 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1641 struct sk_buff * skb;
1643 /* This situation should never happen, but due to
1644 some unknow bugs, it is possible that
1645 we are working on NULL sk_buff :-( */
1646 if (sis_priv->rx_skbuff[entry] == NULL) {
1647 printk(KERN_INFO "%s: NULL pointer "
1648 "encountered in Rx ring, skipping\n",
1653 pci_unmap_single(sis_priv->pci_dev,
1654 sis_priv->rx_ring[entry].bufptr, RX_BUF_SIZE,
1655 PCI_DMA_FROMDEVICE);
1656 /* give the socket buffer to upper layers */
1657 skb = sis_priv->rx_skbuff[entry];
1658 skb_put(skb, rx_size);
1659 skb->protocol = eth_type_trans(skb, net_dev);
1662 /* some network statistics */
1663 if ((rx_status & BCAST) == MCAST)
1664 sis_priv->stats.multicast++;
1665 net_dev->last_rx = jiffies;
1666 sis_priv->stats.rx_bytes += rx_size;
1667 sis_priv->stats.rx_packets++;
1669 /* refill the Rx buffer, what if there is not enought memory for
1670 new socket buffer ?? */
1671 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1672 /* not enough memory for skbuff, this makes a "hole"
1673 on the buffer ring, it is not clear how the
1674 hardware will react to this kind of degenerated
1676 printk(KERN_INFO "%s: Memory squeeze,"
1677 "deferring packet.\n",
1679 sis_priv->rx_skbuff[entry] = NULL;
1680 /* reset buffer descriptor state */
1681 sis_priv->rx_ring[entry].cmdsts = 0;
1682 sis_priv->rx_ring[entry].bufptr = 0;
1683 sis_priv->stats.rx_dropped++;
1687 sis_priv->rx_skbuff[entry] = skb;
1688 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1689 sis_priv->rx_ring[entry].bufptr =
1690 pci_map_single(sis_priv->pci_dev, skb->tail,
1691 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1692 sis_priv->dirty_rx++;
1695 entry = sis_priv->cur_rx % NUM_RX_DESC;
1696 rx_status = sis_priv->rx_ring[entry].cmdsts;
1699 /* refill the Rx buffer, what if the rate of refilling is slower than
1701 for (;sis_priv->cur_rx - sis_priv->dirty_rx > 0; sis_priv->dirty_rx++) {
1702 struct sk_buff *skb;
1704 entry = sis_priv->dirty_rx % NUM_RX_DESC;
1706 if (sis_priv->rx_skbuff[entry] == NULL) {
1707 if ((skb = dev_alloc_skb(RX_BUF_SIZE)) == NULL) {
1708 /* not enough memory for skbuff, this makes a "hole"
1709 on the buffer ring, it is not clear how the
1710 hardware will react to this kind of degenerated
1712 printk(KERN_INFO "%s: Memory squeeze,"
1713 "deferring packet.\n",
1715 sis_priv->stats.rx_dropped++;
1719 sis_priv->rx_skbuff[entry] = skb;
1720 sis_priv->rx_ring[entry].cmdsts = RX_BUF_SIZE;
1721 sis_priv->rx_ring[entry].bufptr =
1722 pci_map_single(sis_priv->pci_dev, skb->tail,
1723 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1726 /* re-enable the potentially idle receive state matchine */
1727 outl(RxENA | inl(ioaddr + cr), ioaddr + cr );
1733 * sis900_finish_xmit - finish up transmission of packets
1734 * @net_dev: the net device to be transmitted on
1736 * Check for error condition and free socket buffer etc
1737 * schedule for more transmission as needed
1738 * Note: This fucntion is called by interrupt handler,
1739 * don't do "too much" work here
1742 static void sis900_finish_xmit (struct net_device *net_dev)
1744 struct sis900_private *sis_priv = net_dev->priv;
1746 for (; sis_priv->dirty_tx != sis_priv->cur_tx; sis_priv->dirty_tx++) {
1747 struct sk_buff *skb;
1751 entry = sis_priv->dirty_tx % NUM_TX_DESC;
1752 tx_status = sis_priv->tx_ring[entry].cmdsts;
1754 if (tx_status & OWN) {
1755 /* The packet is not transmitted yet (owned by hardware) !
1756 Note: the interrupt is generated only when Tx Machine
1757 is idle, so this is an almost impossible case */
1761 if (tx_status & (ABORT | UNDERRUN | OWCOLL)) {
1762 /* packet unsuccessfully transmitted */
1763 if (sis900_debug > 3)
1764 printk(KERN_INFO "%s: Transmit "
1765 "error, Tx status %8.8x.\n",
1766 net_dev->name, tx_status);
1767 sis_priv->stats.tx_errors++;
1768 if (tx_status & UNDERRUN)
1769 sis_priv->stats.tx_fifo_errors++;
1770 if (tx_status & ABORT)
1771 sis_priv->stats.tx_aborted_errors++;
1772 if (tx_status & NOCARRIER)
1773 sis_priv->stats.tx_carrier_errors++;
1774 if (tx_status & OWCOLL)
1775 sis_priv->stats.tx_window_errors++;
1777 /* packet successfully transmitted */
1778 sis_priv->stats.collisions += (tx_status & COLCNT) >> 16;
1779 sis_priv->stats.tx_bytes += tx_status & DSIZE;
1780 sis_priv->stats.tx_packets++;
1782 /* Free the original skb. */
1783 skb = sis_priv->tx_skbuff[entry];
1784 pci_unmap_single(sis_priv->pci_dev,
1785 sis_priv->tx_ring[entry].bufptr, skb->len,
1787 dev_kfree_skb_irq(skb);
1788 sis_priv->tx_skbuff[entry] = NULL;
1789 sis_priv->tx_ring[entry].bufptr = 0;
1790 sis_priv->tx_ring[entry].cmdsts = 0;
1793 if (sis_priv->tx_full && netif_queue_stopped(net_dev) &&
1794 sis_priv->cur_tx - sis_priv->dirty_tx < NUM_TX_DESC - 4) {
1795 /* The ring is no longer full, clear tx_full and schedule more transmission
1796 by netif_wake_queue(net_dev) */
1797 sis_priv->tx_full = 0;
1798 netif_wake_queue (net_dev);
1803 * sis900_close - close sis900 device
1804 * @net_dev: the net device to be closed
1806 * Disable interrupts, stop the Tx and Rx Status Machine
1807 * free Tx and RX socket buffer
1811 sis900_close(struct net_device *net_dev)
1813 long ioaddr = net_dev->base_addr;
1814 struct sis900_private *sis_priv = net_dev->priv;
1815 struct sk_buff *skb;
1818 netif_stop_queue(net_dev);
1820 /* Disable interrupts by clearing the interrupt mask. */
1821 outl(0x0000, ioaddr + imr);
1822 outl(0x0000, ioaddr + ier);
1824 /* Stop the chip's Tx and Rx Status Machine */
1825 outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
1827 del_timer(&sis_priv->timer);
1829 free_irq(net_dev->irq, net_dev);
1831 /* Free Tx and RX skbuff */
1832 for (i = 0; i < NUM_RX_DESC; i++) {
1833 skb = sis_priv->rx_skbuff[i];
1835 pci_unmap_single(sis_priv->pci_dev,
1836 sis_priv->rx_ring[i].bufptr,
1837 RX_BUF_SIZE, PCI_DMA_FROMDEVICE);
1839 sis_priv->rx_skbuff[i] = 0;
1842 for (i = 0; i < NUM_TX_DESC; i++) {
1843 skb = sis_priv->tx_skbuff[i];
1845 pci_unmap_single(sis_priv->pci_dev,
1846 sis_priv->tx_ring[i].bufptr, skb->len,
1849 sis_priv->tx_skbuff[i] = 0;
1853 /* Green! Put the chip in low-power mode. */
1859 * sis900_get_drvinfo - Return information about driver
1860 * @net_dev: the net device to probe
1861 * @info: container for info returned
1863 * Process ethtool command such as "ehtool -i" to show information
1866 static void sis900_get_drvinfo(struct net_device *net_dev,
1867 struct ethtool_drvinfo *info)
1869 struct sis900_private *sis_priv = net_dev->priv;
1871 strcpy (info->driver, SIS900_MODULE_NAME);
1872 strcpy (info->version, SIS900_DRV_VERSION);
1873 strcpy (info->bus_info, pci_name(sis_priv->pci_dev));
1876 static struct ethtool_ops sis900_ethtool_ops = {
1877 .get_drvinfo = sis900_get_drvinfo,
1881 * mii_ioctl - process MII i/o control command
1882 * @net_dev: the net device to command for
1883 * @rq: parameter for command
1884 * @cmd: the i/o command
1886 * Process MII command like read/write MII register
1889 static int mii_ioctl(struct net_device *net_dev, struct ifreq *rq, int cmd)
1891 struct sis900_private *sis_priv = net_dev->priv;
1892 struct mii_ioctl_data *data = (struct mii_ioctl_data *)&rq->ifr_data;
1895 case SIOCGMIIPHY: /* Get address of MII PHY in use. */
1896 data->phy_id = sis_priv->mii->phy_addr;
1899 case SIOCGMIIREG: /* Read MII PHY register. */
1900 data->val_out = mdio_read(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f);
1903 case SIOCSMIIREG: /* Write MII PHY register. */
1904 if (!capable(CAP_NET_ADMIN))
1906 mdio_write(net_dev, data->phy_id & 0x1f, data->reg_num & 0x1f, data->val_in);
1914 * sis900_get_stats - Get sis900 read/write statistics
1915 * @net_dev: the net device to get statistics for
1917 * get tx/rx statistics for sis900
1920 static struct net_device_stats *
1921 sis900_get_stats(struct net_device *net_dev)
1923 struct sis900_private *sis_priv = net_dev->priv;
1925 return &sis_priv->stats;
1929 * sis900_set_config - Set media type by net_device.set_config
1930 * @dev: the net device for media type change
1931 * @map: ifmap passed by ifconfig
1933 * Set media type to 10baseT, 100baseT or 0(for auto) by ifconfig
1934 * we support only port changes. All other runtime configuration
1935 * changes will be ignored
1938 static int sis900_set_config(struct net_device *dev, struct ifmap *map)
1940 struct sis900_private *sis_priv = dev->priv;
1941 struct mii_phy *mii_phy = sis_priv->mii;
1945 if ((map->port != (u_char)(-1)) && (map->port != dev->if_port)) {
1946 /* we switch on the ifmap->port field. I couldn't find anything
1947 like a definition or standard for the values of that field.
1948 I think the meaning of those values is device specific. But
1949 since I would like to change the media type via the ifconfig
1950 command I use the definition from linux/netdevice.h
1951 (which seems to be different from the ifport(pcmcia) definition)
1954 case IF_PORT_UNKNOWN: /* use auto here */
1955 dev->if_port = map->port;
1956 /* we are going to change the media type, so the Link will
1957 be temporary down and we need to reflect that here. When
1958 the Link comes up again, it will be sensed by the sis_timer
1959 procedure, which also does all the rest for us */
1960 netif_carrier_off(dev);
1962 /* read current state */
1963 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1965 /* enable auto negotiation and reset the negotioation
1966 (I don't really know what the auto negatiotiation reset
1967 really means, but it sounds for me right to do one here)*/
1968 mdio_write(dev, mii_phy->phy_addr,
1969 MII_CONTROL, status | MII_CNTL_AUTO | MII_CNTL_RST_AUTO);
1973 case IF_PORT_10BASET: /* 10BaseT */
1974 dev->if_port = map->port;
1976 /* we are going to change the media type, so the Link will
1977 be temporary down and we need to reflect that here. When
1978 the Link comes up again, it will be sensed by the sis_timer
1979 procedure, which also does all the rest for us */
1980 netif_carrier_off(dev);
1982 /* set Speed to 10Mbps */
1983 /* read current state */
1984 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
1986 /* disable auto negotiation and force 10MBit mode*/
1987 mdio_write(dev, mii_phy->phy_addr,
1988 MII_CONTROL, status & ~(MII_CNTL_SPEED | MII_CNTL_AUTO));
1991 case IF_PORT_100BASET: /* 100BaseT */
1992 case IF_PORT_100BASETX: /* 100BaseTx */
1993 dev->if_port = map->port;
1995 /* we are going to change the media type, so the Link will
1996 be temporary down and we need to reflect that here. When
1997 the Link comes up again, it will be sensed by the sis_timer
1998 procedure, which also does all the rest for us */
1999 netif_carrier_off(dev);
2001 /* set Speed to 100Mbps */
2002 /* disable auto negotiation and enable 100MBit Mode */
2003 status = mdio_read(dev, mii_phy->phy_addr, MII_CONTROL);
2004 mdio_write(dev, mii_phy->phy_addr,
2005 MII_CONTROL, (status & ~MII_CNTL_SPEED) | MII_CNTL_SPEED);
2009 case IF_PORT_10BASE2: /* 10Base2 */
2010 case IF_PORT_AUI: /* AUI */
2011 case IF_PORT_100BASEFX: /* 100BaseFx */
2012 /* These Modes are not supported (are they?)*/
2013 printk(KERN_INFO "Not supported");
2018 printk(KERN_INFO "Invalid");
2026 * sis900_mcast_bitnr - compute hashtable index
2027 * @addr: multicast address
2028 * @revision: revision id of chip
2030 * SiS 900 uses the most sigificant 7 bits to index a 128 bits multicast
2031 * hash table, which makes this function a little bit different from other drivers
2032 * SiS 900 B0 & 635 M/B uses the most significat 8 bits to index 256 bits
2033 * multicast hash table.
2036 static inline u16 sis900_mcast_bitnr(u8 *addr, u8 revision)
2039 u32 crc = ether_crc(6, addr);
2041 /* leave 8 or 7 most siginifant bits */
2042 if ((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2043 return ((int)(crc >> 24));
2045 return ((int)(crc >> 25));
2049 * set_rx_mode - Set SiS900 receive mode
2050 * @net_dev: the net device to be set
2052 * Set SiS900 receive mode for promiscuous, multicast, or broadcast mode.
2053 * And set the appropriate multicast filter.
2054 * Multicast hash table changes from 128 to 256 bits for 635M/B & 900B0.
2057 static void set_rx_mode(struct net_device *net_dev)
2059 long ioaddr = net_dev->base_addr;
2060 struct sis900_private * sis_priv = net_dev->priv;
2061 u16 mc_filter[16] = {0}; /* 256/128 bits multicast hash table */
2062 int i, table_entries;
2066 /* 635 Hash Table entires = 256(2^16) */
2067 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2068 if((revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV))
2073 if (net_dev->flags & IFF_PROMISC) {
2074 /* Accept any kinds of packets */
2075 rx_mode = RFPromiscuous;
2076 for (i = 0; i < table_entries; i++)
2077 mc_filter[i] = 0xffff;
2078 } else if ((net_dev->mc_count > multicast_filter_limit) ||
2079 (net_dev->flags & IFF_ALLMULTI)) {
2080 /* too many multicast addresses or accept all multicast packet */
2081 rx_mode = RFAAB | RFAAM;
2082 for (i = 0; i < table_entries; i++)
2083 mc_filter[i] = 0xffff;
2085 /* Accept Broadcast packet, destination address matchs our MAC address,
2086 use Receive Filter to reject unwanted MCAST packet */
2087 struct dev_mc_list *mclist;
2089 for (i = 0, mclist = net_dev->mc_list; mclist && i < net_dev->mc_count;
2090 i++, mclist = mclist->next) {
2091 unsigned int bit_nr =
2092 sis900_mcast_bitnr(mclist->dmi_addr, revision);
2093 mc_filter[bit_nr >> 4] |= (1 << (bit_nr & 0xf));
2097 /* update Multicast Hash Table in Receive Filter */
2098 for (i = 0; i < table_entries; i++) {
2099 /* why plus 0x04 ??, That makes the correct value for hash table. */
2100 outl((u32)(0x00000004+i) << RFADDR_shift, ioaddr + rfcr);
2101 outl(mc_filter[i], ioaddr + rfdr);
2104 outl(RFEN | rx_mode, ioaddr + rfcr);
2106 /* sis900 is capatable of looping back packet at MAC level for debugging purpose */
2107 if (net_dev->flags & IFF_LOOPBACK) {
2109 /* We must disable Tx/Rx before setting loopback mode */
2110 cr_saved = inl(ioaddr + cr);
2111 outl(cr_saved | TxDIS | RxDIS, ioaddr + cr);
2112 /* enable loopback */
2113 outl(inl(ioaddr + txcfg) | TxMLB, ioaddr + txcfg);
2114 outl(inl(ioaddr + rxcfg) | RxATX, ioaddr + rxcfg);
2116 outl(cr_saved, ioaddr + cr);
2123 * sis900_reset - Reset sis900 MAC
2124 * @net_dev: the net device to reset
2126 * reset sis900 MAC and wait until finished
2127 * reset through command register
2128 * change backoff algorithm for 900B0 & 635 M/B
2131 static void sis900_reset(struct net_device *net_dev)
2133 struct sis900_private * sis_priv = net_dev->priv;
2134 long ioaddr = net_dev->base_addr;
2136 u32 status = TxRCMP | RxRCMP;
2139 outl(0, ioaddr + ier);
2140 outl(0, ioaddr + imr);
2141 outl(0, ioaddr + rfcr);
2143 outl(RxRESET | TxRESET | RESET | inl(ioaddr + cr), ioaddr + cr);
2145 /* Check that the chip has finished the reset. */
2146 while (status && (i++ < 1000)) {
2147 status ^= (inl(isr + ioaddr) & status);
2150 pci_read_config_byte(sis_priv->pci_dev, PCI_CLASS_REVISION, &revision);
2151 if( (revision >= SIS635A_900_REV) || (revision == SIS900B_900_REV) )
2152 outl(PESEL | RND_CNT, ioaddr + cfg);
2154 outl(PESEL, ioaddr + cfg);
2158 * sis900_remove - Remove sis900 device
2159 * @pci_dev: the pci device to be removed
2161 * remove and release SiS900 net device
2164 static void __devexit sis900_remove(struct pci_dev *pci_dev)
2166 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2167 struct sis900_private * sis_priv = net_dev->priv;
2168 struct mii_phy *phy = NULL;
2170 while (sis_priv->first_mii) {
2171 phy = sis_priv->first_mii;
2172 sis_priv->first_mii = phy->next;
2176 pci_free_consistent(pci_dev, RX_TOTAL_SIZE, sis_priv->rx_ring,
2177 sis_priv->rx_ring_dma);
2178 pci_free_consistent(pci_dev, TX_TOTAL_SIZE, sis_priv->tx_ring,
2179 sis_priv->tx_ring_dma);
2180 unregister_netdev(net_dev);
2181 free_netdev(net_dev);
2182 pci_release_regions(pci_dev);
2183 pci_set_drvdata(pci_dev, NULL);
2188 static int sis900_suspend(struct pci_dev *pci_dev, u32 state)
2190 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2191 struct sis900_private *sis_priv = net_dev->priv;
2192 long ioaddr = net_dev->base_addr;
2194 if(!netif_running(net_dev))
2197 netif_stop_queue(net_dev);
2199 /* Stop the chip's Tx and Rx Status Machine */
2200 outl(RxDIS | TxDIS | inl(ioaddr + cr), ioaddr + cr);
2202 pci_set_power_state(pci_dev, 3);
2203 pci_save_state(pci_dev, sis_priv->pci_state);
2208 static int sis900_resume(struct pci_dev *pci_dev)
2210 struct net_device *net_dev = pci_get_drvdata(pci_dev);
2211 struct sis900_private *sis_priv = net_dev->priv;
2212 long ioaddr = net_dev->base_addr;
2214 if(!netif_running(net_dev))
2216 pci_restore_state(pci_dev, sis_priv->pci_state);
2217 pci_set_power_state(pci_dev, 0);
2219 sis900_init_rxfilter(net_dev);
2221 sis900_init_tx_ring(net_dev);
2222 sis900_init_rx_ring(net_dev);
2224 set_rx_mode(net_dev);
2226 netif_device_attach(net_dev);
2227 netif_start_queue(net_dev);
2229 /* Workaround for EDB */
2230 sis900_set_mode(ioaddr, HW_SPEED_10_MBPS, FDX_CAPABLE_HALF_SELECTED);
2232 /* Enable all known interrupts by setting the interrupt mask. */
2233 outl((RxSOVR|RxORN|RxERR|RxOK|TxURN|TxERR|TxIDLE), ioaddr + imr);
2234 outl(RxENA | inl(ioaddr + cr), ioaddr + cr);
2235 outl(IE, ioaddr + ier);
2237 sis900_check_mode(net_dev, sis_priv->mii);
2241 #endif /* CONFIG_PM */
2243 static struct pci_driver sis900_pci_driver = {
2244 .name = SIS900_MODULE_NAME,
2245 .id_table = sis900_pci_tbl,
2246 .probe = sis900_probe,
2247 .remove = __devexit_p(sis900_remove),
2249 .suspend = sis900_suspend,
2250 .resume = sis900_resume,
2251 #endif /* CONFIG_PM */
2254 static int __init sis900_init_module(void)
2256 /* when a module, this is printed whether or not devices are found in probe */
2261 return pci_module_init(&sis900_pci_driver);
2264 static void __exit sis900_cleanup_module(void)
2266 pci_unregister_driver(&sis900_pci_driver);
2269 module_init(sis900_init_module);
2270 module_exit(sis900_cleanup_module);