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/version.h>
28 #include <linux/moduleparam.h>
29 #include <linux/string.h>
30 #include <linux/timer.h>
31 #include <linux/errno.h>
32 #include <linux/ioport.h>
33 #include <linux/slab.h>
34 #include <linux/interrupt.h>
35 #include <linux/pci.h>
36 #include <linux/netdevice.h>
37 #include <linux/etherdevice.h>
38 #include <linux/skbuff.h>
39 #include <linux/init.h>
40 #include <linux/delay.h>
41 #include <linux/mii.h>
42 #include <linux/ethtool.h>
43 #include <linux/crc32.h>
44 #include <linux/spinlock.h>
45 #include <linux/bitops.h>
47 #include <linux/irq.h>
48 #include <linux/uaccess.h>
50 #include <asm/processor.h>
52 #define DRV_NAME "r6040"
53 #define DRV_VERSION "0.16"
54 #define DRV_RELDATE "10Nov2007"
56 /* PHY CHIP Address */
57 #define PHY1_ADDR 1 /* For MAC1 */
58 #define PHY2_ADDR 2 /* For MAC2 */
59 #define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
60 #define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
62 /* Time in jiffies before concluding the transmitter is hung. */
63 #define TX_TIMEOUT (6000 * HZ / 1000)
65 /* RDC MAC I/O Size */
66 #define R6040_IO_SIZE 256
72 #define MCR0 0x00 /* Control register 0 */
73 #define MCR1 0x04 /* Control register 1 */
74 #define MAC_RST 0x0001 /* Reset the MAC */
75 #define MBCR 0x08 /* Bus control */
76 #define MT_ICR 0x0C /* TX interrupt control */
77 #define MR_ICR 0x10 /* RX interrupt control */
78 #define MTPR 0x14 /* TX poll command register */
79 #define MR_BSR 0x18 /* RX buffer size */
80 #define MR_DCR 0x1A /* RX descriptor control */
81 #define MLSR 0x1C /* Last status */
82 #define MMDIO 0x20 /* MDIO control register */
83 #define MDIO_WRITE 0x4000 /* MDIO write */
84 #define MDIO_READ 0x2000 /* MDIO read */
85 #define MMRD 0x24 /* MDIO read data register */
86 #define MMWD 0x28 /* MDIO write data register */
87 #define MTD_SA0 0x2C /* TX descriptor start address 0 */
88 #define MTD_SA1 0x30 /* TX descriptor start address 1 */
89 #define MRD_SA0 0x34 /* RX descriptor start address 0 */
90 #define MRD_SA1 0x38 /* RX descriptor start address 1 */
91 #define MISR 0x3C /* Status register */
92 #define MIER 0x40 /* INT enable register */
93 #define MSK_INT 0x0000 /* Mask off interrupts */
94 #define ME_CISR 0x44 /* Event counter INT status */
95 #define ME_CIER 0x48 /* Event counter INT enable */
96 #define MR_CNT 0x50 /* Successfully received packet counter */
97 #define ME_CNT0 0x52 /* Event counter 0 */
98 #define ME_CNT1 0x54 /* Event counter 1 */
99 #define ME_CNT2 0x56 /* Event counter 2 */
100 #define ME_CNT3 0x58 /* Event counter 3 */
101 #define MT_CNT 0x5A /* Successfully transmit packet counter */
102 #define ME_CNT4 0x5C /* Event counter 4 */
103 #define MP_CNT 0x5E /* Pause frame counter register */
104 #define MAR0 0x60 /* Hash table 0 */
105 #define MAR1 0x62 /* Hash table 1 */
106 #define MAR2 0x64 /* Hash table 2 */
107 #define MAR3 0x66 /* Hash table 3 */
108 #define MID_0L 0x68 /* Multicast address MID0 Low */
109 #define MID_0M 0x6A /* Multicast address MID0 Medium */
110 #define MID_0H 0x6C /* Multicast address MID0 High */
111 #define MID_1L 0x70 /* MID1 Low */
112 #define MID_1M 0x72 /* MID1 Medium */
113 #define MID_1H 0x74 /* MID1 High */
114 #define MID_2L 0x78 /* MID2 Low */
115 #define MID_2M 0x7A /* MID2 Medium */
116 #define MID_2H 0x7C /* MID2 High */
117 #define MID_3L 0x80 /* MID3 Low */
118 #define MID_3M 0x82 /* MID3 Medium */
119 #define MID_3H 0x84 /* MID3 High */
120 #define PHY_CC 0x88 /* PHY status change configuration register */
121 #define PHY_ST 0x8A /* PHY status register */
122 #define MAC_SM 0xAC /* MAC status machine */
123 #define MAC_ID 0xBE /* Identifier register */
125 #define TX_DCNT 0x80 /* TX descriptor count */
126 #define RX_DCNT 0x80 /* RX descriptor count */
127 #define MAX_BUF_SIZE 0x600
128 #define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
129 #define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
130 #define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
131 #define MCAST_MAX 4 /* Max number multicast addresses to filter */
134 #define ICPLUS_PHY_ID 0x0243
136 MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>,"
137 "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>,"
138 "Florian Fainelli <florian@openwrt.org>");
139 MODULE_LICENSE("GPL");
140 MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver");
142 #define RX_INT 0x0001
143 #define TX_INT 0x0010
144 #define RX_NO_DESC_INT 0x0002
145 #define INT_MASK (RX_INT | TX_INT)
147 struct r6040_descriptor
{
148 u16 status
, len
; /* 0-3 */
149 __le32 buf
; /* 4-7 */
150 __le32 ndesc
; /* 8-B */
152 char *vbufp
; /* 10-13 */
153 struct r6040_descriptor
*vndescp
; /* 14-17 */
154 struct sk_buff
*skb_ptr
; /* 18-1B */
155 u32 rev2
; /* 1C-1F */
156 } __attribute__((aligned(32)));
158 struct r6040_private
{
159 spinlock_t lock
; /* driver lock */
160 struct timer_list timer
;
161 struct pci_dev
*pdev
;
162 struct r6040_descriptor
*rx_insert_ptr
;
163 struct r6040_descriptor
*rx_remove_ptr
;
164 struct r6040_descriptor
*tx_insert_ptr
;
165 struct r6040_descriptor
*tx_remove_ptr
;
166 struct r6040_descriptor
*rx_ring
;
167 struct r6040_descriptor
*tx_ring
;
168 dma_addr_t rx_ring_dma
;
169 dma_addr_t tx_ring_dma
;
170 u16 tx_free_desc
, rx_free_desc
, phy_addr
, phy_mode
;
173 struct net_device
*dev
;
174 struct mii_if_info mii_if
;
175 struct napi_struct napi
;
179 static char version
[] __devinitdata
= KERN_INFO DRV_NAME
180 ": RDC R6040 NAPI net driver,"
181 "version "DRV_VERSION
" (" DRV_RELDATE
")\n";
183 static int phy_table
[] = { PHY1_ADDR
, PHY2_ADDR
};
185 /* Read a word data from PHY Chip */
186 static int phy_read(void __iomem
*ioaddr
, int phy_addr
, int reg
)
191 iowrite16(MDIO_READ
+ reg
+ (phy_addr
<< 8), ioaddr
+ MMDIO
);
192 /* Wait for the read bit to be cleared */
194 cmd
= ioread16(ioaddr
+ MMDIO
);
199 return ioread16(ioaddr
+ MMRD
);
202 /* Write a word data from PHY Chip */
203 static void phy_write(void __iomem
*ioaddr
, int phy_addr
, int reg
, u16 val
)
208 iowrite16(val
, ioaddr
+ MMWD
);
209 /* Write the command to the MDIO bus */
210 iowrite16(MDIO_WRITE
+ reg
+ (phy_addr
<< 8), ioaddr
+ MMDIO
);
211 /* Wait for the write bit to be cleared */
213 cmd
= ioread16(ioaddr
+ MMDIO
);
214 if (cmd
& MDIO_WRITE
)
219 static int mdio_read(struct net_device
*dev
, int mii_id
, int reg
)
221 struct r6040_private
*lp
= netdev_priv(dev
);
222 void __iomem
*ioaddr
= lp
->base
;
224 return (phy_read(ioaddr
, lp
->phy_addr
, reg
));
227 static void mdio_write(struct net_device
*dev
, int mii_id
, int reg
, int val
)
229 struct r6040_private
*lp
= netdev_priv(dev
);
230 void __iomem
*ioaddr
= lp
->base
;
232 phy_write(ioaddr
, lp
->phy_addr
, reg
, val
);
235 static void r6040_free_txbufs(struct net_device
*dev
)
237 struct r6040_private
*lp
= netdev_priv(dev
);
240 for (i
= 0; i
< TX_DCNT
; i
++) {
241 if (lp
->tx_insert_ptr
->skb_ptr
) {
242 pci_unmap_single(lp
->pdev
,
243 le32_to_cpu(lp
->tx_insert_ptr
->buf
),
244 MAX_BUF_SIZE
, PCI_DMA_TODEVICE
);
245 dev_kfree_skb(lp
->tx_insert_ptr
->skb_ptr
);
246 lp
->rx_insert_ptr
->skb_ptr
= NULL
;
248 lp
->tx_insert_ptr
= lp
->tx_insert_ptr
->vndescp
;
252 static void r6040_free_rxbufs(struct net_device
*dev
)
254 struct r6040_private
*lp
= netdev_priv(dev
);
257 for (i
= 0; i
< RX_DCNT
; i
++) {
258 if (lp
->rx_insert_ptr
->skb_ptr
) {
259 pci_unmap_single(lp
->pdev
,
260 le32_to_cpu(lp
->rx_insert_ptr
->buf
),
261 MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
262 dev_kfree_skb(lp
->rx_insert_ptr
->skb_ptr
);
263 lp
->rx_insert_ptr
->skb_ptr
= NULL
;
265 lp
->rx_insert_ptr
= lp
->rx_insert_ptr
->vndescp
;
269 static void r6040_init_ring_desc(struct r6040_descriptor
*desc_ring
,
270 dma_addr_t desc_dma
, int size
)
272 struct r6040_descriptor
*desc
= desc_ring
;
273 dma_addr_t mapping
= desc_dma
;
276 mapping
+= sizeof(*desc
);
277 desc
->ndesc
= cpu_to_le32(mapping
);
278 desc
->vndescp
= desc
+ 1;
282 desc
->ndesc
= cpu_to_le32(desc_dma
);
283 desc
->vndescp
= desc_ring
;
286 /* Allocate skb buffer for rx descriptor */
287 static void rx_buf_alloc(struct r6040_private
*lp
, struct net_device
*dev
)
289 struct r6040_descriptor
*descptr
;
290 void __iomem
*ioaddr
= lp
->base
;
292 descptr
= lp
->rx_insert_ptr
;
293 while (lp
->rx_free_desc
< RX_DCNT
) {
294 descptr
->skb_ptr
= netdev_alloc_skb(dev
, MAX_BUF_SIZE
);
296 if (!descptr
->skb_ptr
)
298 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
,
299 descptr
->skb_ptr
->data
,
300 MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
));
301 descptr
->status
= 0x8000;
302 descptr
= descptr
->vndescp
;
305 iowrite16(lp
->mcr0
| 0x0002, ioaddr
);
307 lp
->rx_insert_ptr
= descptr
;
310 static void r6040_alloc_txbufs(struct net_device
*dev
)
312 struct r6040_private
*lp
= netdev_priv(dev
);
313 void __iomem
*ioaddr
= lp
->base
;
315 lp
->tx_free_desc
= TX_DCNT
;
317 lp
->tx_remove_ptr
= lp
->tx_insert_ptr
= lp
->tx_ring
;
318 r6040_init_ring_desc(lp
->tx_ring
, lp
->tx_ring_dma
, TX_DCNT
);
320 iowrite16(lp
->tx_ring_dma
, ioaddr
+ MTD_SA0
);
321 iowrite16(lp
->tx_ring_dma
>> 16, ioaddr
+ MTD_SA1
);
324 static void r6040_alloc_rxbufs(struct net_device
*dev
)
326 struct r6040_private
*lp
= netdev_priv(dev
);
327 void __iomem
*ioaddr
= lp
->base
;
329 lp
->rx_free_desc
= 0;
331 lp
->rx_remove_ptr
= lp
->rx_insert_ptr
= lp
->rx_ring
;
332 r6040_init_ring_desc(lp
->rx_ring
, lp
->rx_ring_dma
, RX_DCNT
);
334 rx_buf_alloc(lp
, dev
);
336 iowrite16(lp
->rx_ring_dma
, ioaddr
+ MRD_SA0
);
337 iowrite16(lp
->rx_ring_dma
>> 16, ioaddr
+ MRD_SA1
);
340 static void r6040_tx_timeout(struct net_device
*dev
)
342 struct r6040_private
*priv
= netdev_priv(dev
);
343 void __iomem
*ioaddr
= priv
->base
;
345 printk(KERN_WARNING
"%s: transmit timed out, status %4.4x, PHY status "
347 dev
->name
, ioread16(ioaddr
+ MIER
),
348 mdio_read(dev
, priv
->mii_if
.phy_id
, MII_BMSR
));
350 disable_irq(dev
->irq
);
351 napi_disable(&priv
->napi
);
352 spin_lock(&priv
->lock
);
353 /* Clear all descriptors */
354 r6040_free_txbufs(dev
);
355 r6040_free_rxbufs(dev
);
356 r6040_alloc_txbufs(dev
);
357 r6040_alloc_rxbufs(dev
);
360 iowrite16(MAC_RST
, ioaddr
+ MCR1
);
361 spin_unlock(&priv
->lock
);
362 enable_irq(dev
->irq
);
364 dev
->stats
.tx_errors
++;
365 netif_wake_queue(dev
);
368 static struct net_device_stats
*r6040_get_stats(struct net_device
*dev
)
370 struct r6040_private
*priv
= netdev_priv(dev
);
371 void __iomem
*ioaddr
= priv
->base
;
374 spin_lock_irqsave(&priv
->lock
, flags
);
375 dev
->stats
.rx_crc_errors
+= ioread8(ioaddr
+ ME_CNT1
);
376 dev
->stats
.multicast
+= ioread8(ioaddr
+ ME_CNT0
);
377 spin_unlock_irqrestore(&priv
->lock
, flags
);
382 /* Stop RDC MAC and Free the allocated resource */
383 static void r6040_down(struct net_device
*dev
)
385 struct r6040_private
*lp
= netdev_priv(dev
);
386 void __iomem
*ioaddr
= lp
->base
;
387 struct pci_dev
*pdev
= lp
->pdev
;
393 iowrite16(MSK_INT
, ioaddr
+ MIER
); /* Mask Off Interrupt */
394 iowrite16(MAC_RST
, ioaddr
+ MCR1
); /* Reset RDC MAC */
396 cmd
= ioread16(ioaddr
+ MCR1
);
401 /* Restore MAC Address to MIDx */
402 adrp
= (u16
*) dev
->dev_addr
;
403 iowrite16(adrp
[0], ioaddr
+ MID_0L
);
404 iowrite16(adrp
[1], ioaddr
+ MID_0M
);
405 iowrite16(adrp
[2], ioaddr
+ MID_0H
);
406 free_irq(dev
->irq
, dev
);
409 r6040_free_rxbufs(dev
);
412 r6040_free_txbufs(dev
);
414 /* Free Descriptor memory */
415 pci_free_consistent(pdev
, RX_DESC_SIZE
, lp
->rx_ring
, lp
->rx_ring_dma
);
416 pci_free_consistent(pdev
, TX_DESC_SIZE
, lp
->tx_ring
, lp
->tx_ring_dma
);
419 static int r6040_close(struct net_device
*dev
)
421 struct r6040_private
*lp
= netdev_priv(dev
);
424 del_timer_sync(&lp
->timer
);
426 spin_lock_irq(&lp
->lock
);
427 netif_stop_queue(dev
);
429 spin_unlock_irq(&lp
->lock
);
434 /* Status of PHY CHIP */
435 static int phy_mode_chk(struct net_device
*dev
)
437 struct r6040_private
*lp
= netdev_priv(dev
);
438 void __iomem
*ioaddr
= lp
->base
;
441 /* PHY Link Status Check */
442 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
443 if (!(phy_dat
& 0x4))
444 phy_dat
= 0x8000; /* Link Failed, full duplex */
446 /* PHY Chip Auto-Negotiation Status */
447 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 1);
448 if (phy_dat
& 0x0020) {
449 /* Auto Negotiation Mode */
450 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 5);
451 phy_dat
&= phy_read(ioaddr
, lp
->phy_addr
, 4);
453 /* Force full duplex */
459 phy_dat
= phy_read(ioaddr
, lp
->phy_addr
, 0);
469 static void r6040_set_carrier(struct mii_if_info
*mii
)
471 if (phy_mode_chk(mii
->dev
)) {
472 /* autoneg is off: Link is always assumed to be up */
473 if (!netif_carrier_ok(mii
->dev
))
474 netif_carrier_on(mii
->dev
);
476 phy_mode_chk(mii
->dev
);
479 static int r6040_ioctl(struct net_device
*dev
, struct ifreq
*rq
, int cmd
)
481 struct r6040_private
*lp
= netdev_priv(dev
);
482 struct mii_ioctl_data
*data
= if_mii(rq
);
485 if (!netif_running(dev
))
487 spin_lock_irq(&lp
->lock
);
488 rc
= generic_mii_ioctl(&lp
->mii_if
, data
, cmd
, NULL
);
489 spin_unlock_irq(&lp
->lock
);
490 r6040_set_carrier(&lp
->mii_if
);
494 static int r6040_rx(struct net_device
*dev
, int limit
)
496 struct r6040_private
*priv
= netdev_priv(dev
);
498 void __iomem
*ioaddr
= priv
->base
;
501 for (count
= 0; count
< limit
; ++count
) {
502 struct r6040_descriptor
*descptr
= priv
->rx_remove_ptr
;
503 struct sk_buff
*skb_ptr
;
505 /* Disable RX interrupt */
506 iowrite16(ioread16(ioaddr
+ MIER
) & (~RX_INT
), ioaddr
+ MIER
);
507 descptr
= priv
->rx_remove_ptr
;
509 /* Check for errors */
510 err
= ioread16(ioaddr
+ MLSR
);
512 dev
->stats
.rx_errors
++;
513 /* RX FIFO over-run */
515 dev
->stats
.rx_fifo_errors
++;
516 /* RX descriptor unavailable */
518 dev
->stats
.rx_frame_errors
++;
519 /* Received packet with length over buffer lenght */
521 dev
->stats
.rx_over_errors
++;
522 /* Received packet with too long or short */
523 if (err
& (0x0010 | 0x0008))
524 dev
->stats
.rx_length_errors
++;
525 /* Received packet with CRC errors */
527 spin_lock(&priv
->lock
);
528 dev
->stats
.rx_crc_errors
++;
529 spin_unlock(&priv
->lock
);
532 while (priv
->rx_free_desc
) {
534 if (descptr
->status
& 0x8000)
536 skb_ptr
= descptr
->skb_ptr
;
538 printk(KERN_ERR
"%s: Inconsistent RX"
539 "descriptor chain\n",
543 descptr
->skb_ptr
= NULL
;
544 skb_ptr
->dev
= priv
->dev
;
545 /* Do not count the CRC */
546 skb_put(skb_ptr
, descptr
->len
- 4);
547 pci_unmap_single(priv
->pdev
, le32_to_cpu(descptr
->buf
),
548 MAX_BUF_SIZE
, PCI_DMA_FROMDEVICE
);
549 skb_ptr
->protocol
= eth_type_trans(skb_ptr
, priv
->dev
);
550 /* Send to upper layer */
551 netif_receive_skb(skb_ptr
);
552 dev
->last_rx
= jiffies
;
553 dev
->stats
.rx_packets
++;
554 dev
->stats
.rx_bytes
+= descptr
->len
;
555 /* To next descriptor */
556 descptr
= descptr
->vndescp
;
557 priv
->rx_free_desc
--;
559 priv
->rx_remove_ptr
= descptr
;
561 /* Allocate new RX buffer */
562 if (priv
->rx_free_desc
< RX_DCNT
)
563 rx_buf_alloc(priv
, priv
->dev
);
568 static void r6040_tx(struct net_device
*dev
)
570 struct r6040_private
*priv
= netdev_priv(dev
);
571 struct r6040_descriptor
*descptr
;
572 void __iomem
*ioaddr
= priv
->base
;
573 struct sk_buff
*skb_ptr
;
576 spin_lock(&priv
->lock
);
577 descptr
= priv
->tx_remove_ptr
;
578 while (priv
->tx_free_desc
< TX_DCNT
) {
579 /* Check for errors */
580 err
= ioread16(ioaddr
+ MLSR
);
583 dev
->stats
.rx_fifo_errors
++;
584 if (err
& (0x2000 | 0x4000))
585 dev
->stats
.tx_carrier_errors
++;
587 if (descptr
->status
& 0x8000)
588 break; /* Not complete */
589 skb_ptr
= descptr
->skb_ptr
;
590 pci_unmap_single(priv
->pdev
, le32_to_cpu(descptr
->buf
),
591 skb_ptr
->len
, PCI_DMA_TODEVICE
);
593 dev_kfree_skb_irq(skb_ptr
);
594 descptr
->skb_ptr
= NULL
;
595 /* To next descriptor */
596 descptr
= descptr
->vndescp
;
597 priv
->tx_free_desc
++;
599 priv
->tx_remove_ptr
= descptr
;
601 if (priv
->tx_free_desc
)
602 netif_wake_queue(dev
);
603 spin_unlock(&priv
->lock
);
606 static int r6040_poll(struct napi_struct
*napi
, int budget
)
608 struct r6040_private
*priv
=
609 container_of(napi
, struct r6040_private
, napi
);
610 struct net_device
*dev
= priv
->dev
;
611 void __iomem
*ioaddr
= priv
->base
;
614 work_done
= r6040_rx(dev
, budget
);
616 if (work_done
< budget
) {
617 netif_rx_complete(dev
, napi
);
618 /* Enable RX interrupt */
619 iowrite16(ioread16(ioaddr
+ MIER
) | RX_INT
, ioaddr
+ MIER
);
624 /* The RDC interrupt handler. */
625 static irqreturn_t
r6040_interrupt(int irq
, void *dev_id
)
627 struct net_device
*dev
= dev_id
;
628 struct r6040_private
*lp
= netdev_priv(dev
);
629 void __iomem
*ioaddr
= lp
->base
;
632 /* Mask off RDC MAC interrupt */
633 iowrite16(MSK_INT
, ioaddr
+ MIER
);
634 /* Read MISR status and clear */
635 status
= ioread16(ioaddr
+ MISR
);
637 if (status
== 0x0000 || status
== 0xffff)
640 /* RX interrupt request */
642 netif_rx_schedule(dev
, &lp
->napi
);
643 iowrite16(TX_INT
, ioaddr
+ MIER
);
646 /* TX interrupt request */
653 #ifdef CONFIG_NET_POLL_CONTROLLER
654 static void r6040_poll_controller(struct net_device
*dev
)
656 disable_irq(dev
->irq
);
657 r6040_interrupt(dev
->irq
, dev
);
658 enable_irq(dev
->irq
);
663 static void r6040_up(struct net_device
*dev
)
665 struct r6040_private
*lp
= netdev_priv(dev
);
666 void __iomem
*ioaddr
= lp
->base
;
668 /* Initialise and alloc RX/TX buffers */
669 r6040_alloc_txbufs(dev
);
670 r6040_alloc_rxbufs(dev
);
672 /* Buffer Size Register */
673 iowrite16(MAX_BUF_SIZE
, ioaddr
+ MR_BSR
);
674 /* Read the PHY ID */
675 lp
->switch_sig
= phy_read(ioaddr
, 0, 2);
677 if (lp
->switch_sig
== ICPLUS_PHY_ID
) {
678 phy_write(ioaddr
, 29, 31, 0x175C); /* Enable registers */
679 lp
->phy_mode
= 0x8000;
682 phy_write(ioaddr
, lp
->phy_addr
, 4, PHY_CAP
);
683 phy_write(ioaddr
, lp
->phy_addr
, 0, PHY_MODE
);
685 if (PHY_MODE
== 0x3100)
686 lp
->phy_mode
= phy_mode_chk(dev
);
688 lp
->phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
690 /* MAC Bus Control Register */
691 iowrite16(MBCR_DEFAULT
, ioaddr
+ MBCR
);
693 /* MAC TX/RX Enable */
694 lp
->mcr0
|= lp
->phy_mode
;
695 iowrite16(lp
->mcr0
, ioaddr
);
697 /* set interrupt waiting time and packet numbers */
698 iowrite16(0x0F06, ioaddr
+ MT_ICR
);
699 iowrite16(0x0F06, ioaddr
+ MR_ICR
);
701 /* improve performance (by RDC guys) */
702 phy_write(ioaddr
, 30, 17, (phy_read(ioaddr
, 30, 17) | 0x4000));
703 phy_write(ioaddr
, 30, 17, ~((~phy_read(ioaddr
, 30, 17)) | 0x2000));
704 phy_write(ioaddr
, 0, 19, 0x0000);
705 phy_write(ioaddr
, 0, 30, 0x01F0);
707 /* Interrupt Mask Register */
708 iowrite16(INT_MASK
, ioaddr
+ MIER
);
712 A periodic timer routine
713 Polling PHY Chip Link Status
715 static void r6040_timer(unsigned long data
)
717 struct net_device
*dev
= (struct net_device
*)data
;
718 struct r6040_private
*lp
= netdev_priv(dev
);
719 void __iomem
*ioaddr
= lp
->base
;
722 /* Polling PHY Chip Status */
723 if (PHY_MODE
== 0x3100)
724 phy_mode
= phy_mode_chk(dev
);
726 phy_mode
= (PHY_MODE
& 0x0100) ? 0x8000:0x0;
728 if (phy_mode
!= lp
->phy_mode
) {
729 lp
->phy_mode
= phy_mode
;
730 lp
->mcr0
= (lp
->mcr0
& 0x7fff) | phy_mode
;
731 iowrite16(lp
->mcr0
, ioaddr
);
732 printk(KERN_INFO
"Link Change %x \n", ioread16(ioaddr
));
735 /* Timer active again */
736 mod_timer(&lp
->timer
, round_jiffies(jiffies
+ HZ
));
739 /* Read/set MAC address routines */
740 static void r6040_mac_address(struct net_device
*dev
)
742 struct r6040_private
*lp
= netdev_priv(dev
);
743 void __iomem
*ioaddr
= lp
->base
;
746 /* MAC operation register */
747 iowrite16(0x01, ioaddr
+ MCR1
); /* Reset MAC */
748 iowrite16(2, ioaddr
+ MAC_SM
); /* Reset internal state machine */
749 iowrite16(0, ioaddr
+ MAC_SM
);
752 /* Restore MAC Address */
753 adrp
= (u16
*) dev
->dev_addr
;
754 iowrite16(adrp
[0], ioaddr
+ MID_0L
);
755 iowrite16(adrp
[1], ioaddr
+ MID_0M
);
756 iowrite16(adrp
[2], ioaddr
+ MID_0H
);
759 static int r6040_open(struct net_device
*dev
)
761 struct r6040_private
*lp
= netdev_priv(dev
);
764 /* Request IRQ and Register interrupt handler */
765 ret
= request_irq(dev
->irq
, &r6040_interrupt
,
766 IRQF_SHARED
, dev
->name
, dev
);
770 /* Set MAC address */
771 r6040_mac_address(dev
);
773 /* Allocate Descriptor memory */
775 pci_alloc_consistent(lp
->pdev
, RX_DESC_SIZE
, &lp
->rx_ring_dma
);
780 pci_alloc_consistent(lp
->pdev
, TX_DESC_SIZE
, &lp
->tx_ring_dma
);
782 pci_free_consistent(lp
->pdev
, RX_DESC_SIZE
, lp
->rx_ring
,
789 napi_enable(&lp
->napi
);
790 netif_start_queue(dev
);
792 /* set and active a timer process */
793 setup_timer(&lp
->timer
, r6040_timer
, (unsigned long) dev
);
794 if (lp
->switch_sig
!= ICPLUS_PHY_ID
)
795 mod_timer(&lp
->timer
, jiffies
+ HZ
);
799 static int r6040_start_xmit(struct sk_buff
*skb
, struct net_device
*dev
)
801 struct r6040_private
*lp
= netdev_priv(dev
);
802 struct r6040_descriptor
*descptr
;
803 void __iomem
*ioaddr
= lp
->base
;
805 int ret
= NETDEV_TX_OK
;
807 /* Critical Section */
808 spin_lock_irqsave(&lp
->lock
, flags
);
810 /* TX resource check */
811 if (!lp
->tx_free_desc
) {
812 spin_unlock_irqrestore(&lp
->lock
, flags
);
813 netif_stop_queue(dev
);
814 printk(KERN_ERR DRV_NAME
": no tx descriptor\n");
815 ret
= NETDEV_TX_BUSY
;
819 /* Statistic Counter */
820 dev
->stats
.tx_packets
++;
821 dev
->stats
.tx_bytes
+= skb
->len
;
822 /* Set TX descriptor & Transmit it */
824 descptr
= lp
->tx_insert_ptr
;
828 descptr
->len
= skb
->len
;
830 descptr
->skb_ptr
= skb
;
831 descptr
->buf
= cpu_to_le32(pci_map_single(lp
->pdev
,
832 skb
->data
, skb
->len
, PCI_DMA_TODEVICE
));
833 descptr
->status
= 0x8000;
834 /* Trigger the MAC to check the TX descriptor */
835 iowrite16(0x01, ioaddr
+ MTPR
);
836 lp
->tx_insert_ptr
= descptr
->vndescp
;
838 /* If no tx resource, stop */
839 if (!lp
->tx_free_desc
)
840 netif_stop_queue(dev
);
842 dev
->trans_start
= jiffies
;
843 spin_unlock_irqrestore(&lp
->lock
, flags
);
847 static void r6040_multicast_list(struct net_device
*dev
)
849 struct r6040_private
*lp
= netdev_priv(dev
);
850 void __iomem
*ioaddr
= lp
->base
;
854 struct dev_mc_list
*dmi
= dev
->mc_list
;
858 adrp
= (u16
*)dev
->dev_addr
;
859 iowrite16(adrp
[0], ioaddr
+ MID_0L
);
860 iowrite16(adrp
[1], ioaddr
+ MID_0M
);
861 iowrite16(adrp
[2], ioaddr
+ MID_0H
);
863 /* Promiscous Mode */
864 spin_lock_irqsave(&lp
->lock
, flags
);
866 /* Clear AMCP & PROM bits */
867 reg
= ioread16(ioaddr
) & ~0x0120;
868 if (dev
->flags
& IFF_PROMISC
) {
872 /* Too many multicast addresses
873 * accept all traffic */
874 else if ((dev
->mc_count
> MCAST_MAX
)
875 || (dev
->flags
& IFF_ALLMULTI
))
878 iowrite16(reg
, ioaddr
);
879 spin_unlock_irqrestore(&lp
->lock
, flags
);
881 /* Build the hash table */
882 if (dev
->mc_count
> MCAST_MAX
) {
886 for (i
= 0; i
< 4; i
++)
889 for (i
= 0; i
< dev
->mc_count
; i
++) {
890 char *addrs
= dmi
->dmi_addr
;
897 crc
= ether_crc_le(6, addrs
);
899 hash_table
[crc
>> 4] |= 1 << (15 - (crc
& 0xf));
901 /* Write the index of the hash table */
902 for (i
= 0; i
< 4; i
++)
903 iowrite16(hash_table
[i
] << 14, ioaddr
+ MCR1
);
904 /* Fill the MAC hash tables with their values */
905 iowrite16(hash_table
[0], ioaddr
+ MAR0
);
906 iowrite16(hash_table
[1], ioaddr
+ MAR1
);
907 iowrite16(hash_table
[2], ioaddr
+ MAR2
);
908 iowrite16(hash_table
[3], ioaddr
+ MAR3
);
910 /* Multicast Address 1~4 case */
911 for (i
= 0, dmi
; (i
< dev
->mc_count
) && (i
< MCAST_MAX
); i
++) {
912 adrp
= (u16
*)dmi
->dmi_addr
;
913 iowrite16(adrp
[0], ioaddr
+ MID_1L
+ 8*i
);
914 iowrite16(adrp
[1], ioaddr
+ MID_1M
+ 8*i
);
915 iowrite16(adrp
[2], ioaddr
+ MID_1H
+ 8*i
);
918 for (i
= dev
->mc_count
; i
< MCAST_MAX
; i
++) {
919 iowrite16(0xffff, ioaddr
+ MID_0L
+ 8*i
);
920 iowrite16(0xffff, ioaddr
+ MID_0M
+ 8*i
);
921 iowrite16(0xffff, ioaddr
+ MID_0H
+ 8*i
);
925 static void netdev_get_drvinfo(struct net_device
*dev
,
926 struct ethtool_drvinfo
*info
)
928 struct r6040_private
*rp
= netdev_priv(dev
);
930 strcpy(info
->driver
, DRV_NAME
);
931 strcpy(info
->version
, DRV_VERSION
);
932 strcpy(info
->bus_info
, pci_name(rp
->pdev
));
935 static int netdev_get_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
937 struct r6040_private
*rp
= netdev_priv(dev
);
940 spin_lock_irq(&rp
->lock
);
941 rc
= mii_ethtool_gset(&rp
->mii_if
, cmd
);
942 spin_unlock_irq(&rp
->lock
);
947 static int netdev_set_settings(struct net_device
*dev
, struct ethtool_cmd
*cmd
)
949 struct r6040_private
*rp
= netdev_priv(dev
);
952 spin_lock_irq(&rp
->lock
);
953 rc
= mii_ethtool_sset(&rp
->mii_if
, cmd
);
954 spin_unlock_irq(&rp
->lock
);
955 r6040_set_carrier(&rp
->mii_if
);
960 static u32
netdev_get_link(struct net_device
*dev
)
962 struct r6040_private
*rp
= netdev_priv(dev
);
964 return mii_link_ok(&rp
->mii_if
);
967 static struct ethtool_ops netdev_ethtool_ops
= {
968 .get_drvinfo
= netdev_get_drvinfo
,
969 .get_settings
= netdev_get_settings
,
970 .set_settings
= netdev_set_settings
,
971 .get_link
= netdev_get_link
,
974 static int __devinit
r6040_init_one(struct pci_dev
*pdev
,
975 const struct pci_device_id
*ent
)
977 struct net_device
*dev
;
978 struct r6040_private
*lp
;
979 void __iomem
*ioaddr
;
980 int err
, io_size
= R6040_IO_SIZE
;
981 static int card_idx
= -1;
986 printk(KERN_INFO
"%s\n", version
);
988 err
= pci_enable_device(pdev
);
992 /* this should always be supported */
993 if (pci_set_dma_mask(pdev
, DMA_32BIT_MASK
)) {
994 printk(KERN_ERR DRV_NAME
"32-bit PCI DMA addresses"
995 "not supported by the card\n");
998 if (pci_set_consistent_dma_mask(pdev
, DMA_32BIT_MASK
)) {
999 printk(KERN_ERR DRV_NAME
"32-bit PCI DMA addresses"
1000 "not supported by the card\n");
1005 if (pci_resource_len(pdev
, 0) < io_size
) {
1006 printk(KERN_ERR
"Insufficient PCI resources, aborting\n");
1010 pioaddr
= pci_resource_start(pdev
, 0); /* IO map base address */
1011 pci_set_master(pdev
);
1013 dev
= alloc_etherdev(sizeof(struct r6040_private
));
1015 printk(KERN_ERR
"Failed to allocate etherdev\n");
1018 SET_NETDEV_DEV(dev
, &pdev
->dev
);
1019 lp
= netdev_priv(dev
);
1022 if (pci_request_regions(pdev
, DRV_NAME
)) {
1023 printk(KERN_ERR DRV_NAME
": Failed to request PCI regions\n");
1025 goto err_out_disable
;
1028 ioaddr
= pci_iomap(pdev
, bar
, io_size
);
1030 printk(KERN_ERR
"ioremap failed for device %s\n",
1035 /* Init system & device */
1037 dev
->irq
= pdev
->irq
;
1039 spin_lock_init(&lp
->lock
);
1040 pci_set_drvdata(pdev
, dev
);
1042 /* Set MAC address */
1045 adrp
= (u16
*)dev
->dev_addr
;
1046 adrp
[0] = ioread16(ioaddr
+ MID_0L
);
1047 adrp
[1] = ioread16(ioaddr
+ MID_0M
);
1048 adrp
[2] = ioread16(ioaddr
+ MID_0H
);
1050 /* Link new device into r6040_root_dev */
1053 /* Init RDC private data */
1055 lp
->phy_addr
= phy_table
[card_idx
];
1058 /* The RDC-specific entries in the device structure. */
1059 dev
->open
= &r6040_open
;
1060 dev
->hard_start_xmit
= &r6040_start_xmit
;
1061 dev
->stop
= &r6040_close
;
1062 dev
->get_stats
= r6040_get_stats
;
1063 dev
->set_multicast_list
= &r6040_multicast_list
;
1064 dev
->do_ioctl
= &r6040_ioctl
;
1065 dev
->ethtool_ops
= &netdev_ethtool_ops
;
1066 dev
->tx_timeout
= &r6040_tx_timeout
;
1067 dev
->watchdog_timeo
= TX_TIMEOUT
;
1068 #ifdef CONFIG_NET_POLL_CONTROLLER
1069 dev
->poll_controller
= r6040_poll_controller
;
1071 netif_napi_add(dev
, &lp
->napi
, r6040_poll
, 64);
1072 lp
->mii_if
.dev
= dev
;
1073 lp
->mii_if
.mdio_read
= mdio_read
;
1074 lp
->mii_if
.mdio_write
= mdio_write
;
1075 lp
->mii_if
.phy_id
= lp
->phy_addr
;
1076 lp
->mii_if
.phy_id_mask
= 0x1f;
1077 lp
->mii_if
.reg_num_mask
= 0x1f;
1079 /* Register net device. After this dev->name assign */
1080 err
= register_netdev(dev
);
1082 printk(KERN_ERR DRV_NAME
": Failed to register net device\n");
1088 pci_release_regions(pdev
);
1090 pci_disable_device(pdev
);
1091 pci_set_drvdata(pdev
, NULL
);
1097 static void __devexit
r6040_remove_one(struct pci_dev
*pdev
)
1099 struct net_device
*dev
= pci_get_drvdata(pdev
);
1101 unregister_netdev(dev
);
1102 pci_release_regions(pdev
);
1104 pci_disable_device(pdev
);
1105 pci_set_drvdata(pdev
, NULL
);
1109 static struct pci_device_id r6040_pci_tbl
[] = {
1110 { PCI_DEVICE(PCI_VENDOR_ID_RDC
, 0x6040) },
1113 MODULE_DEVICE_TABLE(pci
, r6040_pci_tbl
);
1115 static struct pci_driver r6040_driver
= {
1117 .id_table
= r6040_pci_tbl
,
1118 .probe
= r6040_init_one
,
1119 .remove
= __devexit_p(r6040_remove_one
),
1123 static int __init
r6040_init(void)
1125 return pci_register_driver(&r6040_driver
);
1129 static void __exit
r6040_cleanup(void)
1131 pci_unregister_driver(&r6040_driver
);
1134 module_init(r6040_init
);
1135 module_exit(r6040_cleanup
);