2 * RDC R6040 Fast Ethernet MAC support
4 * Copyright (C) 2004 Sten Wang <sten.wang@rdc.com.tw>
6 * Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
7 * Florian Fainelli <florian@openwrt.org>
9 * This program is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU General Public License
11 * as published by the Free Software Foundation; either version 2
12 * of the License, or (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the
21 * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA.
25 #include <linux/kernel.h>
26 #include <linux/module.h>
27 #include <linux/moduleparam.h>
28 #include <linux/string.h>
29 #include <linux/timer.h>
30 #include <linux/errno.h>
31 #include <linux/ioport.h>
32 #include <linux/slab.h>
33 #include <linux/interrupt.h>
34 #include <linux/pci.h>
35 #include <linux/netdevice.h>
36 #include <linux/etherdevice.h>
37 #include <linux/skbuff.h>
38 #include <linux/init.h>
39 #include <linux/delay.h>
40 #include <linux/mii.h>
41 #include <linux/ethtool.h>
42 #include <linux/crc32.h>
43 #include <linux/spinlock.h>
44 #include <linux/bitops.h>
46 #include <linux/irq.h>
47 #include <linux/uaccess.h>
49 #include <asm/processor.h>
51 #define DRV_NAME "r6040"
52 #define DRV_VERSION "0.22"
53 #define DRV_RELDATE "25Mar2009"
55 /* PHY CHIP Address */
56 #define PHY1_ADDR 1 /* For MAC1 */
57 #define PHY2_ADDR 3 /* For MAC2 */
58 #define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
59 #define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
61 /* Time in jiffies before concluding the transmitter is hung. */
62 #define TX_TIMEOUT (6000 * HZ / 1000)
64 /* RDC MAC I/O Size */
65 #define R6040_IO_SIZE 256
71 #define MCR0 0x00 /* Control register 0 */
72 #define MCR1 0x04 /* Control register 1 */
73 #define MAC_RST 0x0001 /* Reset the MAC */
74 #define MBCR 0x08 /* Bus control */
75 #define MT_ICR 0x0C /* TX interrupt control */
76 #define MR_ICR 0x10 /* RX interrupt control */
77 #define MTPR 0x14 /* TX poll command register */
78 #define MR_BSR 0x18 /* RX buffer size */
79 #define MR_DCR 0x1A /* RX descriptor control */
80 #define MLSR 0x1C /* Last status */
81 #define MMDIO 0x20 /* MDIO control register */
82 #define MDIO_WRITE 0x4000 /* MDIO write */
83 #define MDIO_READ 0x2000 /* MDIO read */
84 #define MMRD 0x24 /* MDIO read data register */
85 #define MMWD 0x28 /* MDIO write data register */
86 #define MTD_SA0 0x2C /* TX descriptor start address 0 */
87 #define MTD_SA1 0x30 /* TX descriptor start address 1 */
88 #define MRD_SA0 0x34 /* RX descriptor start address 0 */
89 #define MRD_SA1 0x38 /* RX descriptor start address 1 */
90 #define MISR 0x3C /* Status register */
91 #define MIER 0x40 /* INT enable register */
92 #define MSK_INT 0x0000 /* Mask off interrupts */
93 #define RX_FINISH 0x0001 /* RX finished */
94 #define RX_NO_DESC 0x0002 /* No RX descriptor available */
95 #define RX_FIFO_FULL 0x0004 /* RX FIFO full */
96 #define RX_EARLY 0x0008 /* RX early */
97 #define TX_FINISH 0x0010 /* TX finished */
98 #define TX_EARLY 0x0080 /* TX early */
99 #define EVENT_OVRFL 0x0100 /* Event counter overflow */
100 #define LINK_CHANGED 0x0200 /* PHY link changed */
101 #define ME_CISR 0x44 /* Event counter INT status */
102 #define ME_CIER 0x48 /* Event counter INT enable */
103 #define MR_CNT 0x50 /* Successfully received packet counter */
104 #define ME_CNT0 0x52 /* Event counter 0 */
105 #define ME_CNT1 0x54 /* Event counter 1 */
106 #define ME_CNT2 0x56 /* Event counter 2 */
107 #define ME_CNT3 0x58 /* Event counter 3 */
108 #define MT_CNT 0x5A /* Successfully transmit packet counter */
109 #define ME_CNT4 0x5C /* Event counter 4 */
110 #define MP_CNT 0x5E /* Pause frame counter register */
111 #define MAR0 0x60 /* Hash table 0 */
112 #define MAR1 0x62 /* Hash table 1 */
113 #define MAR2 0x64 /* Hash table 2 */
114 #define MAR3 0x66 /* Hash table 3 */
115 #define MID_0L 0x68 /* Multicast address MID0 Low */
116 #define MID_0M 0x6A /* Multicast address MID0 Medium */
117 #define MID_0H 0x6C /* Multicast address MID0 High */
118 #define MID_1L 0x70 /* MID1 Low */
119 #define MID_1M 0x72 /* MID1 Medium */
120 #define MID_1H 0x74 /* MID1 High */
121 #define MID_2L 0x78 /* MID2 Low */
122 #define MID_2M 0x7A /* MID2 Medium */
123 #define MID_2H 0x7C /* MID2 High */
124 #define MID_3L 0x80 /* MID3 Low */
125 #define MID_3M 0x82 /* MID3 Medium */
126 #define MID_3H 0x84 /* MID3 High */
127 #define PHY_CC 0x88 /* PHY status change configuration register */
128 #define PHY_ST 0x8A /* PHY status register */
129 #define MAC_SM 0xAC /* MAC status machine */
130 #define MAC_ID 0xBE /* Identifier register */
132 #define TX_DCNT 0x80 /* TX descriptor count */
133 #define RX_DCNT 0x80 /* RX descriptor count */
134 #define MAX_BUF_SIZE 0x600
135 #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
136 #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
137 #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
138 #define MCAST_MAX 4 /* Max number multicast addresses to filter */
140 /* Descriptor status */
141 #define DSC_OWNER_MAC 0x8000 /* MAC is the owner of this descriptor */
142 #define DSC_RX_OK 0x4000 /* RX was successful */
143 #define DSC_RX_ERR 0x0800 /* RX PHY error */
144 #define DSC_RX_ERR_DRI 0x0400 /* RX dribble packet */
145 #define DSC_RX_ERR_BUF 0x0200 /* RX length exceeds buffer size */
146 #define DSC_RX_ERR_LONG 0x0100 /* RX length > maximum packet length */
147 #define DSC_RX_ERR_RUNT 0x0080 /* RX packet length < 64 byte */
148 #define DSC_RX_ERR_CRC 0x0040 /* RX CRC error */
149 #define DSC_RX_BCAST 0x0020 /* RX broadcast (no error) */
150 #define DSC_RX_MCAST 0x0010 /* RX multicast (no error) */
151 #define DSC_RX_MCH_HIT 0x0008 /* RX multicast hit in hash table (no error) */
152 #define DSC_RX_MIDH_HIT 0x0004 /* RX MID table hit (no error) */
153 #define DSC_RX_IDX_MID_MASK 3 /* RX mask for the index of matched MIDx */
156 #define ICPLUS_PHY_ID 0x0243
158 MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>,"
159 "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>,"
160 "Florian Fainelli <florian@openwrt.org>");
161 MODULE_LICENSE("GPL");
162 MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver");
163 MODULE_VERSION(DRV_VERSION " " DRV_RELDATE);
165 /* RX and TX interrupts that we handle */
166 #define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH)
167 #define TX_INTS (TX_FINISH)
168 #define INT_MASK (RX_INTS | TX_INTS)
170 struct r6040_descriptor {
171 u16 status, len; /* 0-3 */
172 __le32 buf; /* 4-7 */
173 __le32 ndesc; /* 8-B */
175 char *vbufp; /* 10-13 */
176 struct r6040_descriptor *vndescp; /* 14-17 */
177 struct sk_buff *skb_ptr; /* 18-1B */
178 u32 rev2; /* 1C-1F */
179 } __attribute__((aligned(32)));
181 struct r6040_private {
182 spinlock_t lock; /* driver lock */
183 struct timer_list timer;
184 struct pci_dev *pdev;
185 struct r6040_descriptor *rx_insert_ptr;
186 struct r6040_descriptor *rx_remove_ptr;
187 struct r6040_descriptor *tx_insert_ptr;
188 struct r6040_descriptor *tx_remove_ptr;
189 struct r6040_descriptor *rx_ring;
190 struct r6040_descriptor *tx_ring;
191 dma_addr_t rx_ring_dma;
192 dma_addr_t tx_ring_dma;
193 u16 tx_free_desc, phy_addr, phy_mode;
196 struct net_device *dev;
197 struct mii_if_info mii_if;
198 struct napi_struct napi;
202 static char version[] __devinitdata = KERN_INFO DRV_NAME
203 ": RDC R6040 NAPI net driver,"
204 "version "DRV_VERSION " (" DRV_RELDATE ")";
206 static int phy_table[] = { PHY1_ADDR, PHY2_ADDR };
208 /* Read a word data from PHY Chip */
209 static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg)
214 iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO);
215 /* Wait for the read bit to be cleared */
217 cmd = ioread16(ioaddr + MMDIO);
218 if (!(cmd & MDIO_READ))
222 return ioread16(ioaddr + MMRD);
225 /* Write a word data from PHY Chip */
226 static void r6040_phy_write(void __iomem *ioaddr, int phy_addr, int reg, u16 val)
231 iowrite16(val, ioaddr + MMWD);
232 /* Write the command to the MDIO bus */
233 iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO);
234 /* Wait for the write bit to be cleared */
236 cmd = ioread16(ioaddr + MMDIO);
237 if (!(cmd & MDIO_WRITE))
242 static int r6040_mdio_read(struct net_device *dev, int mii_id, int reg)
244 struct r6040_private *lp = netdev_priv(dev);
245 void __iomem *ioaddr = lp->base;
247 return (r6040_phy_read(ioaddr, lp->phy_addr, reg));
250 static void r6040_mdio_write(struct net_device *dev, int mii_id, int reg, int val)
252 struct r6040_private *lp = netdev_priv(dev);
253 void __iomem *ioaddr = lp->base;
255 r6040_phy_write(ioaddr, lp->phy_addr, reg, val);
258 static void r6040_free_txbufs(struct net_device *dev)
260 struct r6040_private *lp = netdev_priv(dev);
263 for (i = 0; i < TX_DCNT; i++) {
264 if (lp->tx_insert_ptr->skb_ptr) {
265 pci_unmap_single(lp->pdev,
266 le32_to_cpu(lp->tx_insert_ptr->buf),
267 MAX_BUF_SIZE, PCI_DMA_TODEVICE);
268 dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
269 lp->tx_insert_ptr->skb_ptr = NULL;
271 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
275 static void r6040_free_rxbufs(struct net_device *dev)
277 struct r6040_private *lp = netdev_priv(dev);
280 for (i = 0; i < RX_DCNT; i++) {
281 if (lp->rx_insert_ptr->skb_ptr) {
282 pci_unmap_single(lp->pdev,
283 le32_to_cpu(lp->rx_insert_ptr->buf),
284 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
285 dev_kfree_skb(lp->rx_insert_ptr->skb_ptr);
286 lp->rx_insert_ptr->skb_ptr = NULL;
288 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
292 static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring,
293 dma_addr_t desc_dma, int size)
295 struct r6040_descriptor *desc = desc_ring;
296 dma_addr_t mapping = desc_dma;
299 mapping += sizeof(*desc);
300 desc->ndesc = cpu_to_le32(mapping);
301 desc->vndescp = desc + 1;
305 desc->ndesc = cpu_to_le32(desc_dma);
306 desc->vndescp = desc_ring;
309 static void r6040_init_txbufs(struct net_device *dev)
311 struct r6040_private *lp = netdev_priv(dev);
313 lp->tx_free_desc = TX_DCNT;
315 lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring;
316 r6040_init_ring_desc(lp->tx_ring, lp->tx_ring_dma, TX_DCNT);
319 static int r6040_alloc_rxbufs(struct net_device *dev)
321 struct r6040_private *lp = netdev_priv(dev);
322 struct r6040_descriptor *desc;
326 lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring;
327 r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT);
329 /* Allocate skbs for the rx descriptors */
332 skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
334 printk(KERN_ERR DRV_NAME "%s: failed to alloc skb for rx\n", dev->name);
339 desc->buf = cpu_to_le32(pci_map_single(lp->pdev,
341 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
342 desc->status = DSC_OWNER_MAC;
343 desc = desc->vndescp;
344 } while (desc != lp->rx_ring);
349 /* Deallocate all previously allocated skbs */
350 r6040_free_rxbufs(dev);
354 static void r6040_init_mac_regs(struct net_device *dev)
356 struct r6040_private *lp = netdev_priv(dev);
357 void __iomem *ioaddr = lp->base;
361 /* Mask Off Interrupt */
362 iowrite16(MSK_INT, ioaddr + MIER);
365 iowrite16(MAC_RST, ioaddr + MCR1);
367 cmd = ioread16(ioaddr + MCR1);
371 /* Reset internal state machine */
372 iowrite16(2, ioaddr + MAC_SM);
373 iowrite16(0, ioaddr + MAC_SM);
376 /* MAC Bus Control Register */
377 iowrite16(MBCR_DEFAULT, ioaddr + MBCR);
379 /* Buffer Size Register */
380 iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR);
382 /* Write TX ring start address */
383 iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0);
384 iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1);
386 /* Write RX ring start address */
387 iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0);
388 iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1);
390 /* Set interrupt waiting time and packet numbers */
391 iowrite16(0, ioaddr + MT_ICR);
392 iowrite16(0, ioaddr + MR_ICR);
394 /* Enable interrupts */
395 iowrite16(INT_MASK, ioaddr + MIER);
397 /* Enable TX and RX */
398 iowrite16(lp->mcr0 | 0x0002, ioaddr);
400 /* Let TX poll the descriptors
401 * we may got called by r6040_tx_timeout which has left
402 * some unsent tx buffers */
403 iowrite16(0x01, ioaddr + MTPR);
406 static void r6040_tx_timeout(struct net_device *dev)
408 struct r6040_private *priv = netdev_priv(dev);
409 void __iomem *ioaddr = priv->base;
411 printk(KERN_WARNING "%s: transmit timed out, int enable %4.4x "
412 "status %4.4x, PHY status %4.4x\n",
413 dev->name, ioread16(ioaddr + MIER),
414 ioread16(ioaddr + MISR),
415 r6040_mdio_read(dev, priv->mii_if.phy_id, MII_BMSR));
417 dev->stats.tx_errors++;
419 /* Reset MAC and re-init all registers */
420 r6040_init_mac_regs(dev);
423 static struct net_device_stats *r6040_get_stats(struct net_device *dev)
425 struct r6040_private *priv = netdev_priv(dev);
426 void __iomem *ioaddr = priv->base;
429 spin_lock_irqsave(&priv->lock, flags);
430 dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1);
431 dev->stats.multicast += ioread8(ioaddr + ME_CNT0);
432 spin_unlock_irqrestore(&priv->lock, flags);
437 /* Stop RDC MAC and Free the allocated resource */
438 static void r6040_down(struct net_device *dev)
440 struct r6040_private *lp = netdev_priv(dev);
441 void __iomem *ioaddr = lp->base;
447 iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */
448 iowrite16(MAC_RST, ioaddr + MCR1); /* Reset RDC MAC */
450 cmd = ioread16(ioaddr + MCR1);
455 /* Restore MAC Address to MIDx */
456 adrp = (u16 *) dev->dev_addr;
457 iowrite16(adrp[0], ioaddr + MID_0L);
458 iowrite16(adrp[1], ioaddr + MID_0M);
459 iowrite16(adrp[2], ioaddr + MID_0H);
462 static int r6040_close(struct net_device *dev)
464 struct r6040_private *lp = netdev_priv(dev);
465 struct pci_dev *pdev = lp->pdev;
468 del_timer_sync(&lp->timer);
470 spin_lock_irq(&lp->lock);
471 napi_disable(&lp->napi);
472 netif_stop_queue(dev);
475 free_irq(dev->irq, dev);
478 r6040_free_rxbufs(dev);
481 r6040_free_txbufs(dev);
483 spin_unlock_irq(&lp->lock);
485 /* Free Descriptor memory */
487 pci_free_consistent(pdev, RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma);
492 pci_free_consistent(pdev, TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma);
499 /* Status of PHY CHIP */
500 static int r6040_phy_mode_chk(struct net_device *dev)
502 struct r6040_private *lp = netdev_priv(dev);
503 void __iomem *ioaddr = lp->base;
506 /* PHY Link Status Check */
507 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
508 if (!(phy_dat & 0x4))
509 phy_dat = 0x8000; /* Link Failed, full duplex */
511 /* PHY Chip Auto-Negotiation Status */
512 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
513 if (phy_dat & 0x0020) {
514 /* Auto Negotiation Mode */
515 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 5);
516 phy_dat &= r6040_phy_read(ioaddr, lp->phy_addr, 4);
518 /* Force full duplex */
524 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 0);
534 static void r6040_set_carrier(struct mii_if_info *mii)
536 if (r6040_phy_mode_chk(mii->dev)) {
537 /* autoneg is off: Link is always assumed to be up */
538 if (!netif_carrier_ok(mii->dev))
539 netif_carrier_on(mii->dev);
541 r6040_phy_mode_chk(mii->dev);
544 static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
546 struct r6040_private *lp = netdev_priv(dev);
547 struct mii_ioctl_data *data = if_mii(rq);
550 if (!netif_running(dev))
552 spin_lock_irq(&lp->lock);
553 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
554 spin_unlock_irq(&lp->lock);
555 r6040_set_carrier(&lp->mii_if);
559 static int r6040_rx(struct net_device *dev, int limit)
561 struct r6040_private *priv = netdev_priv(dev);
562 struct r6040_descriptor *descptr = priv->rx_remove_ptr;
563 struct sk_buff *skb_ptr, *new_skb;
567 /* Limit not reached and the descriptor belongs to the CPU */
568 while (count < limit && !(descptr->status & DSC_OWNER_MAC)) {
569 /* Read the descriptor status */
570 err = descptr->status;
571 /* Global error status set */
572 if (err & DSC_RX_ERR) {
574 if (err & DSC_RX_ERR_DRI)
575 dev->stats.rx_frame_errors++;
576 /* Buffer lenght exceeded */
577 if (err & DSC_RX_ERR_BUF)
578 dev->stats.rx_length_errors++;
579 /* Packet too long */
580 if (err & DSC_RX_ERR_LONG)
581 dev->stats.rx_length_errors++;
582 /* Packet < 64 bytes */
583 if (err & DSC_RX_ERR_RUNT)
584 dev->stats.rx_length_errors++;
586 if (err & DSC_RX_ERR_CRC) {
587 spin_lock(&priv->lock);
588 dev->stats.rx_crc_errors++;
589 spin_unlock(&priv->lock);
594 /* Packet successfully received */
595 new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
597 dev->stats.rx_dropped++;
600 skb_ptr = descptr->skb_ptr;
601 skb_ptr->dev = priv->dev;
603 /* Do not count the CRC */
604 skb_put(skb_ptr, descptr->len - 4);
605 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
606 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
607 skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev);
609 /* Send to upper layer */
610 netif_receive_skb(skb_ptr);
611 dev->stats.rx_packets++;
612 dev->stats.rx_bytes += descptr->len - 4;
614 /* put new skb into descriptor */
615 descptr->skb_ptr = new_skb;
616 descptr->buf = cpu_to_le32(pci_map_single(priv->pdev,
617 descptr->skb_ptr->data,
618 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
621 /* put the descriptor back to the MAC */
622 descptr->status = DSC_OWNER_MAC;
623 descptr = descptr->vndescp;
626 priv->rx_remove_ptr = descptr;
631 static void r6040_tx(struct net_device *dev)
633 struct r6040_private *priv = netdev_priv(dev);
634 struct r6040_descriptor *descptr;
635 void __iomem *ioaddr = priv->base;
636 struct sk_buff *skb_ptr;
639 spin_lock(&priv->lock);
640 descptr = priv->tx_remove_ptr;
641 while (priv->tx_free_desc < TX_DCNT) {
642 /* Check for errors */
643 err = ioread16(ioaddr + MLSR);
646 dev->stats.rx_fifo_errors++;
647 if (err & (0x2000 | 0x4000))
648 dev->stats.tx_carrier_errors++;
650 if (descptr->status & DSC_OWNER_MAC)
651 break; /* Not complete */
652 skb_ptr = descptr->skb_ptr;
653 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
654 skb_ptr->len, PCI_DMA_TODEVICE);
656 dev_kfree_skb_irq(skb_ptr);
657 descptr->skb_ptr = NULL;
658 /* To next descriptor */
659 descptr = descptr->vndescp;
660 priv->tx_free_desc++;
662 priv->tx_remove_ptr = descptr;
664 if (priv->tx_free_desc)
665 netif_wake_queue(dev);
666 spin_unlock(&priv->lock);
669 static int r6040_poll(struct napi_struct *napi, int budget)
671 struct r6040_private *priv =
672 container_of(napi, struct r6040_private, napi);
673 struct net_device *dev = priv->dev;
674 void __iomem *ioaddr = priv->base;
677 work_done = r6040_rx(dev, budget);
679 if (work_done < budget) {
681 /* Enable RX interrupt */
682 iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER);
687 /* The RDC interrupt handler. */
688 static irqreturn_t r6040_interrupt(int irq, void *dev_id)
690 struct net_device *dev = dev_id;
691 struct r6040_private *lp = netdev_priv(dev);
692 void __iomem *ioaddr = lp->base;
696 misr = ioread16(ioaddr + MIER);
697 /* Mask off RDC MAC interrupt */
698 iowrite16(MSK_INT, ioaddr + MIER);
699 /* Read MISR status and clear */
700 status = ioread16(ioaddr + MISR);
702 if (status == 0x0000 || status == 0xffff)
705 /* RX interrupt request */
706 if (status & RX_INTS) {
707 if (status & RX_NO_DESC) {
708 /* RX descriptor unavailable */
709 dev->stats.rx_dropped++;
710 dev->stats.rx_missed_errors++;
712 if (status & RX_FIFO_FULL)
713 dev->stats.rx_fifo_errors++;
715 /* Mask off RX interrupt */
717 napi_schedule(&lp->napi);
720 /* TX interrupt request */
721 if (status & TX_INTS)
724 /* Restore RDC MAC interrupt */
725 iowrite16(misr, ioaddr + MIER);
730 #ifdef CONFIG_NET_POLL_CONTROLLER
731 static void r6040_poll_controller(struct net_device *dev)
733 disable_irq(dev->irq);
734 r6040_interrupt(dev->irq, dev);
735 enable_irq(dev->irq);
740 static int r6040_up(struct net_device *dev)
742 struct r6040_private *lp = netdev_priv(dev);
743 void __iomem *ioaddr = lp->base;
746 /* Initialise and alloc RX/TX buffers */
747 r6040_init_txbufs(dev);
748 ret = r6040_alloc_rxbufs(dev);
752 /* Read the PHY ID */
753 lp->switch_sig = r6040_phy_read(ioaddr, 0, 2);
755 if (lp->switch_sig == ICPLUS_PHY_ID) {
756 r6040_phy_write(ioaddr, 29, 31, 0x175C); /* Enable registers */
757 lp->phy_mode = 0x8000;
760 r6040_phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
761 r6040_phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
763 if (PHY_MODE == 0x3100)
764 lp->phy_mode = r6040_phy_mode_chk(dev);
766 lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
769 /* Set duplex mode */
770 lp->mcr0 |= lp->phy_mode;
772 /* improve performance (by RDC guys) */
773 r6040_phy_write(ioaddr, 30, 17, (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
774 r6040_phy_write(ioaddr, 30, 17, ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
775 r6040_phy_write(ioaddr, 0, 19, 0x0000);
776 r6040_phy_write(ioaddr, 0, 30, 0x01F0);
778 /* Initialize all MAC registers */
779 r6040_init_mac_regs(dev);
785 A periodic timer routine
786 Polling PHY Chip Link Status
788 static void r6040_timer(unsigned long data)
790 struct net_device *dev = (struct net_device *)data;
791 struct r6040_private *lp = netdev_priv(dev);
792 void __iomem *ioaddr = lp->base;
795 /* Polling PHY Chip Status */
796 if (PHY_MODE == 0x3100)
797 phy_mode = r6040_phy_mode_chk(dev);
799 phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
801 if (phy_mode != lp->phy_mode) {
802 lp->phy_mode = phy_mode;
803 lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode;
804 iowrite16(lp->mcr0, ioaddr);
805 printk(KERN_INFO "Link Change %x \n", ioread16(ioaddr));
808 /* Timer active again */
809 mod_timer(&lp->timer, round_jiffies(jiffies + HZ));
812 /* Read/set MAC address routines */
813 static void r6040_mac_address(struct net_device *dev)
815 struct r6040_private *lp = netdev_priv(dev);
816 void __iomem *ioaddr = lp->base;
819 /* MAC operation register */
820 iowrite16(0x01, ioaddr + MCR1); /* Reset MAC */
821 iowrite16(2, ioaddr + MAC_SM); /* Reset internal state machine */
822 iowrite16(0, ioaddr + MAC_SM);
825 /* Restore MAC Address */
826 adrp = (u16 *) dev->dev_addr;
827 iowrite16(adrp[0], ioaddr + MID_0L);
828 iowrite16(adrp[1], ioaddr + MID_0M);
829 iowrite16(adrp[2], ioaddr + MID_0H);
832 static int r6040_open(struct net_device *dev)
834 struct r6040_private *lp = netdev_priv(dev);
837 /* Request IRQ and Register interrupt handler */
838 ret = request_irq(dev->irq, &r6040_interrupt,
839 IRQF_SHARED, dev->name, dev);
843 /* Set MAC address */
844 r6040_mac_address(dev);
846 /* Allocate Descriptor memory */
848 pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma);
853 pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma);
855 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
862 pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring,
864 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
869 napi_enable(&lp->napi);
870 netif_start_queue(dev);
872 /* set and active a timer process */
873 setup_timer(&lp->timer, r6040_timer, (unsigned long) dev);
874 if (lp->switch_sig != ICPLUS_PHY_ID)
875 mod_timer(&lp->timer, jiffies + HZ);
879 static int r6040_start_xmit(struct sk_buff *skb, struct net_device *dev)
881 struct r6040_private *lp = netdev_priv(dev);
882 struct r6040_descriptor *descptr;
883 void __iomem *ioaddr = lp->base;
885 int ret = NETDEV_TX_OK;
887 /* Critical Section */
888 spin_lock_irqsave(&lp->lock, flags);
890 /* TX resource check */
891 if (!lp->tx_free_desc) {
892 spin_unlock_irqrestore(&lp->lock, flags);
893 netif_stop_queue(dev);
894 printk(KERN_ERR DRV_NAME ": no tx descriptor\n");
895 ret = NETDEV_TX_BUSY;
899 /* Statistic Counter */
900 dev->stats.tx_packets++;
901 dev->stats.tx_bytes += skb->len;
902 /* Set TX descriptor & Transmit it */
904 descptr = lp->tx_insert_ptr;
908 descptr->len = skb->len;
910 descptr->skb_ptr = skb;
911 descptr->buf = cpu_to_le32(pci_map_single(lp->pdev,
912 skb->data, skb->len, PCI_DMA_TODEVICE));
913 descptr->status = DSC_OWNER_MAC;
914 /* Trigger the MAC to check the TX descriptor */
915 iowrite16(0x01, ioaddr + MTPR);
916 lp->tx_insert_ptr = descptr->vndescp;
918 /* If no tx resource, stop */
919 if (!lp->tx_free_desc)
920 netif_stop_queue(dev);
922 dev->trans_start = jiffies;
923 spin_unlock_irqrestore(&lp->lock, flags);
927 static void r6040_multicast_list(struct net_device *dev)
929 struct r6040_private *lp = netdev_priv(dev);
930 void __iomem *ioaddr = lp->base;
934 struct dev_mc_list *dmi = dev->mc_list;
938 adrp = (u16 *)dev->dev_addr;
939 iowrite16(adrp[0], ioaddr + MID_0L);
940 iowrite16(adrp[1], ioaddr + MID_0M);
941 iowrite16(adrp[2], ioaddr + MID_0H);
943 /* Promiscous Mode */
944 spin_lock_irqsave(&lp->lock, flags);
946 /* Clear AMCP & PROM bits */
947 reg = ioread16(ioaddr) & ~0x0120;
948 if (dev->flags & IFF_PROMISC) {
952 /* Too many multicast addresses
953 * accept all traffic */
954 else if ((dev->mc_count > MCAST_MAX)
955 || (dev->flags & IFF_ALLMULTI))
958 iowrite16(reg, ioaddr);
959 spin_unlock_irqrestore(&lp->lock, flags);
961 /* Build the hash table */
962 if (dev->mc_count > MCAST_MAX) {
966 for (i = 0; i < 4; i++)
969 for (i = 0; i < dev->mc_count; i++) {
970 char *addrs = dmi->dmi_addr;
977 crc = ether_crc_le(6, addrs);
979 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
981 /* Write the index of the hash table */
982 for (i = 0; i < 4; i++)
983 iowrite16(hash_table[i] << 14, ioaddr + MCR1);
984 /* Fill the MAC hash tables with their values */
985 iowrite16(hash_table[0], ioaddr + MAR0);
986 iowrite16(hash_table[1], ioaddr + MAR1);
987 iowrite16(hash_table[2], ioaddr + MAR2);
988 iowrite16(hash_table[3], ioaddr + MAR3);
990 /* Multicast Address 1~4 case */
991 for (i = 0, dmi; (i < dev->mc_count) && (i < MCAST_MAX); i++) {
992 adrp = (u16 *)dmi->dmi_addr;
993 iowrite16(adrp[0], ioaddr + MID_1L + 8*i);
994 iowrite16(adrp[1], ioaddr + MID_1M + 8*i);
995 iowrite16(adrp[2], ioaddr + MID_1H + 8*i);
998 for (i = dev->mc_count; i < MCAST_MAX; i++) {
999 iowrite16(0xffff, ioaddr + MID_0L + 8*i);
1000 iowrite16(0xffff, ioaddr + MID_0M + 8*i);
1001 iowrite16(0xffff, ioaddr + MID_0H + 8*i);
1005 static void netdev_get_drvinfo(struct net_device *dev,
1006 struct ethtool_drvinfo *info)
1008 struct r6040_private *rp = netdev_priv(dev);
1010 strcpy(info->driver, DRV_NAME);
1011 strcpy(info->version, DRV_VERSION);
1012 strcpy(info->bus_info, pci_name(rp->pdev));
1015 static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1017 struct r6040_private *rp = netdev_priv(dev);
1020 spin_lock_irq(&rp->lock);
1021 rc = mii_ethtool_gset(&rp->mii_if, cmd);
1022 spin_unlock_irq(&rp->lock);
1027 static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1029 struct r6040_private *rp = netdev_priv(dev);
1032 spin_lock_irq(&rp->lock);
1033 rc = mii_ethtool_sset(&rp->mii_if, cmd);
1034 spin_unlock_irq(&rp->lock);
1035 r6040_set_carrier(&rp->mii_if);
1040 static u32 netdev_get_link(struct net_device *dev)
1042 struct r6040_private *rp = netdev_priv(dev);
1044 return mii_link_ok(&rp->mii_if);
1047 static const struct ethtool_ops netdev_ethtool_ops = {
1048 .get_drvinfo = netdev_get_drvinfo,
1049 .get_settings = netdev_get_settings,
1050 .set_settings = netdev_set_settings,
1051 .get_link = netdev_get_link,
1054 static const struct net_device_ops r6040_netdev_ops = {
1055 .ndo_open = r6040_open,
1056 .ndo_stop = r6040_close,
1057 .ndo_start_xmit = r6040_start_xmit,
1058 .ndo_get_stats = r6040_get_stats,
1059 .ndo_set_multicast_list = r6040_multicast_list,
1060 .ndo_change_mtu = eth_change_mtu,
1061 .ndo_validate_addr = eth_validate_addr,
1062 .ndo_set_mac_address = eth_mac_addr,
1063 .ndo_do_ioctl = r6040_ioctl,
1064 .ndo_tx_timeout = r6040_tx_timeout,
1065 #ifdef CONFIG_NET_POLL_CONTROLLER
1066 .ndo_poll_controller = r6040_poll_controller,
1070 static int __devinit r6040_init_one(struct pci_dev *pdev,
1071 const struct pci_device_id *ent)
1073 struct net_device *dev;
1074 struct r6040_private *lp;
1075 void __iomem *ioaddr;
1076 int err, io_size = R6040_IO_SIZE;
1077 static int card_idx = -1;
1082 printk(KERN_INFO "%s\n", version);
1084 err = pci_enable_device(pdev);
1088 /* this should always be supported */
1089 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
1091 printk(KERN_ERR DRV_NAME ": 32-bit PCI DMA addresses"
1092 "not supported by the card\n");
1095 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
1097 printk(KERN_ERR DRV_NAME ": 32-bit PCI DMA addresses"
1098 "not supported by the card\n");
1103 if (pci_resource_len(pdev, 0) < io_size) {
1104 printk(KERN_ERR DRV_NAME ": Insufficient PCI resources, aborting\n");
1109 pioaddr = pci_resource_start(pdev, 0); /* IO map base address */
1110 pci_set_master(pdev);
1112 dev = alloc_etherdev(sizeof(struct r6040_private));
1114 printk(KERN_ERR DRV_NAME ": Failed to allocate etherdev\n");
1118 SET_NETDEV_DEV(dev, &pdev->dev);
1119 lp = netdev_priv(dev);
1121 err = pci_request_regions(pdev, DRV_NAME);
1124 printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
1125 goto err_out_free_dev;
1128 ioaddr = pci_iomap(pdev, bar, io_size);
1130 printk(KERN_ERR DRV_NAME ": ioremap failed for device %s\n",
1133 goto err_out_free_res;
1135 /* If PHY status change register is still set to zero it means the
1136 * bootloader didn't initialize it */
1137 if (ioread16(ioaddr + PHY_CC) == 0)
1138 iowrite16(0x9f07, ioaddr + PHY_CC);
1140 /* Init system & device */
1142 dev->irq = pdev->irq;
1144 spin_lock_init(&lp->lock);
1145 pci_set_drvdata(pdev, dev);
1147 /* Set MAC address */
1150 adrp = (u16 *)dev->dev_addr;
1151 adrp[0] = ioread16(ioaddr + MID_0L);
1152 adrp[1] = ioread16(ioaddr + MID_0M);
1153 adrp[2] = ioread16(ioaddr + MID_0H);
1155 /* Some bootloader/BIOSes do not initialize
1156 * MAC address, warn about that */
1157 if (!(adrp[0] || adrp[1] || adrp[2])) {
1158 printk(KERN_WARNING DRV_NAME ": MAC address not initialized, generating random\n");
1159 random_ether_addr(dev->dev_addr);
1162 /* Link new device into r6040_root_dev */
1166 /* Init RDC private data */
1168 lp->phy_addr = phy_table[card_idx];
1171 /* The RDC-specific entries in the device structure. */
1172 dev->netdev_ops = &r6040_netdev_ops;
1173 dev->ethtool_ops = &netdev_ethtool_ops;
1174 dev->watchdog_timeo = TX_TIMEOUT;
1176 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
1177 lp->mii_if.dev = dev;
1178 lp->mii_if.mdio_read = r6040_mdio_read;
1179 lp->mii_if.mdio_write = r6040_mdio_write;
1180 lp->mii_if.phy_id = lp->phy_addr;
1181 lp->mii_if.phy_id_mask = 0x1f;
1182 lp->mii_if.reg_num_mask = 0x1f;
1184 /* Register net device. After this dev->name assign */
1185 err = register_netdev(dev);
1187 printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
1193 pci_iounmap(pdev, ioaddr);
1195 pci_release_regions(pdev);
1202 static void __devexit r6040_remove_one(struct pci_dev *pdev)
1204 struct net_device *dev = pci_get_drvdata(pdev);
1206 unregister_netdev(dev);
1207 pci_release_regions(pdev);
1209 pci_disable_device(pdev);
1210 pci_set_drvdata(pdev, NULL);
1214 static struct pci_device_id r6040_pci_tbl[] = {
1215 { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
1218 MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1220 static struct pci_driver r6040_driver = {
1222 .id_table = r6040_pci_tbl,
1223 .probe = r6040_init_one,
1224 .remove = __devexit_p(r6040_remove_one),
1228 static int __init r6040_init(void)
1230 return pci_register_driver(&r6040_driver);
1234 static void __exit r6040_cleanup(void)
1236 pci_unregister_driver(&r6040_driver);
1239 module_init(r6040_init);
1240 module_exit(r6040_cleanup);