drivers/net/: use DEFINE_PCI_DEVICE_TABLE()
[deliverable/linux.git] / drivers / net / r6040.c
CommitLineData
7a47dd7a
SW
1/*
2 * RDC R6040 Fast Ethernet MAC support
3 *
4 * Copyright (C) 2004 Sten Wang <sten.wang@rdc.com.tw>
5 * Copyright (C) 2007
5ac5d616 6 * Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
7a47dd7a
SW
7 * Florian Fainelli <florian@openwrt.org>
8 *
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.
13 *
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.
18 *
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.
23*/
24
25#include <linux/kernel.h>
26#include <linux/module.h>
7a47dd7a
SW
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>
092427be
JG
44#include <linux/bitops.h>
45#include <linux/io.h>
46#include <linux/irq.h>
47#include <linux/uaccess.h>
7a47dd7a
SW
48
49#include <asm/processor.h>
7a47dd7a
SW
50
51#define DRV_NAME "r6040"
9818f660
FF
52#define DRV_VERSION "0.25"
53#define DRV_RELDATE "20Aug2009"
7a47dd7a
SW
54
55/* PHY CHIP Address */
56#define PHY1_ADDR 1 /* For MAC1 */
2a30ca8b 57#define PHY2_ADDR 3 /* For MAC2 */
7a47dd7a
SW
58#define PHY_MODE 0x3100 /* PHY CHIP Register 0 */
59#define PHY_CAP 0x01E1 /* PHY CHIP Register 4 */
60
61/* Time in jiffies before concluding the transmitter is hung. */
5ac5d616 62#define TX_TIMEOUT (6000 * HZ / 1000)
7a47dd7a
SW
63
64/* RDC MAC I/O Size */
65#define R6040_IO_SIZE 256
66
67/* MAX RDC MAC */
68#define MAX_MAC 2
69
70/* MAC registers */
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 */
3d254348
FF
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 */
7a47dd7a
SW
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 */
131
132#define TX_DCNT 0x80 /* TX descriptor count */
133#define RX_DCNT 0x80 /* RX descriptor count */
134#define MAX_BUF_SIZE 0x600
6c323103
FR
135#define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
136#define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
7a47dd7a
SW
137#define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
138#define MCAST_MAX 4 /* Max number multicast addresses to filter */
139
32f565df
FF
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 */
154
7a47dd7a
SW
155/* PHY settings */
156#define ICPLUS_PHY_ID 0x0243
157
158MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>,"
159 "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>,"
160 "Florian Fainelli <florian@openwrt.org>");
161MODULE_LICENSE("GPL");
162MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver");
bc4de260 163MODULE_VERSION(DRV_VERSION " " DRV_RELDATE);
7a47dd7a 164
3d254348 165/* RX and TX interrupts that we handle */
e24ddf3a
FF
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)
7a47dd7a
SW
169
170struct r6040_descriptor {
171 u16 status, len; /* 0-3 */
172 __le32 buf; /* 4-7 */
173 __le32 ndesc; /* 8-B */
174 u32 rev1; /* C-F */
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)));
180
181struct 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;
6c323103
FR
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;
9ca28dc4 193 u16 tx_free_desc, phy_addr, phy_mode;
7a47dd7a 194 u16 mcr0, mcr1;
7a47dd7a
SW
195 u16 switch_sig;
196 struct net_device *dev;
197 struct mii_if_info mii_if;
198 struct napi_struct napi;
7a47dd7a
SW
199 void __iomem *base;
200};
201
202static char version[] __devinitdata = KERN_INFO DRV_NAME
203 ": RDC R6040 NAPI net driver,"
9a48ce84 204 "version "DRV_VERSION " (" DRV_RELDATE ")";
7a47dd7a 205
092427be 206static int phy_table[] = { PHY1_ADDR, PHY2_ADDR };
7a47dd7a
SW
207
208/* Read a word data from PHY Chip */
c6e69bb9 209static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg)
7a47dd7a
SW
210{
211 int limit = 2048;
212 u16 cmd;
213
214 iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO);
215 /* Wait for the read bit to be cleared */
216 while (limit--) {
217 cmd = ioread16(ioaddr + MMDIO);
11e5e8f5 218 if (!(cmd & MDIO_READ))
7a47dd7a
SW
219 break;
220 }
221
222 return ioread16(ioaddr + MMRD);
223}
224
225/* Write a word data from PHY Chip */
c6e69bb9 226static void r6040_phy_write(void __iomem *ioaddr, int phy_addr, int reg, u16 val)
7a47dd7a
SW
227{
228 int limit = 2048;
229 u16 cmd;
230
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 */
235 while (limit--) {
236 cmd = ioread16(ioaddr + MMDIO);
11e5e8f5 237 if (!(cmd & MDIO_WRITE))
7a47dd7a
SW
238 break;
239 }
240}
241
c6e69bb9 242static int r6040_mdio_read(struct net_device *dev, int mii_id, int reg)
7a47dd7a
SW
243{
244 struct r6040_private *lp = netdev_priv(dev);
245 void __iomem *ioaddr = lp->base;
246
c6e69bb9 247 return (r6040_phy_read(ioaddr, lp->phy_addr, reg));
7a47dd7a
SW
248}
249
c6e69bb9 250static void r6040_mdio_write(struct net_device *dev, int mii_id, int reg, int val)
7a47dd7a
SW
251{
252 struct r6040_private *lp = netdev_priv(dev);
253 void __iomem *ioaddr = lp->base;
254
c6e69bb9 255 r6040_phy_write(ioaddr, lp->phy_addr, reg, val);
7a47dd7a
SW
256}
257
b4f1255d
FF
258static void r6040_free_txbufs(struct net_device *dev)
259{
260 struct r6040_private *lp = netdev_priv(dev);
261 int i;
262
263 for (i = 0; i < TX_DCNT; i++) {
264 if (lp->tx_insert_ptr->skb_ptr) {
ed773b4a
AV
265 pci_unmap_single(lp->pdev,
266 le32_to_cpu(lp->tx_insert_ptr->buf),
b4f1255d
FF
267 MAX_BUF_SIZE, PCI_DMA_TODEVICE);
268 dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
3b060be0 269 lp->tx_insert_ptr->skb_ptr = NULL;
b4f1255d
FF
270 }
271 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
272 }
273}
274
275static void r6040_free_rxbufs(struct net_device *dev)
276{
277 struct r6040_private *lp = netdev_priv(dev);
278 int i;
279
280 for (i = 0; i < RX_DCNT; i++) {
281 if (lp->rx_insert_ptr->skb_ptr) {
ed773b4a
AV
282 pci_unmap_single(lp->pdev,
283 le32_to_cpu(lp->rx_insert_ptr->buf),
b4f1255d
FF
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;
287 }
288 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
289 }
290}
291
b4f1255d
FF
292static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring,
293 dma_addr_t desc_dma, int size)
294{
295 struct r6040_descriptor *desc = desc_ring;
296 dma_addr_t mapping = desc_dma;
297
298 while (size-- > 0) {
3f6602ad 299 mapping += sizeof(*desc);
b4f1255d
FF
300 desc->ndesc = cpu_to_le32(mapping);
301 desc->vndescp = desc + 1;
302 desc++;
303 }
304 desc--;
305 desc->ndesc = cpu_to_le32(desc_dma);
306 desc->vndescp = desc_ring;
307}
308
3d463419 309static void r6040_init_txbufs(struct net_device *dev)
b4f1255d
FF
310{
311 struct r6040_private *lp = netdev_priv(dev);
b4f1255d
FF
312
313 lp->tx_free_desc = TX_DCNT;
314
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);
b4f1255d
FF
317}
318
3d463419 319static int r6040_alloc_rxbufs(struct net_device *dev)
b4f1255d
FF
320{
321 struct r6040_private *lp = netdev_priv(dev);
3d463419
FF
322 struct r6040_descriptor *desc;
323 struct sk_buff *skb;
324 int rc;
b4f1255d
FF
325
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);
328
3d463419
FF
329 /* Allocate skbs for the rx descriptors */
330 desc = lp->rx_ring;
331 do {
332 skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
333 if (!skb) {
9a48ce84 334 printk(KERN_ERR DRV_NAME "%s: failed to alloc skb for rx\n", dev->name);
3d463419
FF
335 rc = -ENOMEM;
336 goto err_exit;
337 }
338 desc->skb_ptr = skb;
339 desc->buf = cpu_to_le32(pci_map_single(lp->pdev,
340 desc->skb_ptr->data,
341 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
32f565df 342 desc->status = DSC_OWNER_MAC;
3d463419
FF
343 desc = desc->vndescp;
344 } while (desc != lp->rx_ring);
345
346 return 0;
347
348err_exit:
349 /* Deallocate all previously allocated skbs */
350 r6040_free_rxbufs(dev);
351 return rc;
fec3a23b
FF
352}
353
354static void r6040_init_mac_regs(struct net_device *dev)
355{
356 struct r6040_private *lp = netdev_priv(dev);
357 void __iomem *ioaddr = lp->base;
358 int limit = 2048;
359 u16 cmd;
360
361 /* Mask Off Interrupt */
362 iowrite16(MSK_INT, ioaddr + MIER);
363
364 /* Reset RDC MAC */
365 iowrite16(MAC_RST, ioaddr + MCR1);
366 while (limit--) {
367 cmd = ioread16(ioaddr + MCR1);
368 if (cmd & 0x1)
369 break;
370 }
371 /* Reset internal state machine */
372 iowrite16(2, ioaddr + MAC_SM);
373 iowrite16(0, ioaddr + MAC_SM);
c1d69937 374 mdelay(5);
fec3a23b
FF
375
376 /* MAC Bus Control Register */
377 iowrite16(MBCR_DEFAULT, ioaddr + MBCR);
378
379 /* Buffer Size Register */
380 iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR);
381
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);
b4f1255d 385
fec3a23b 386 /* Write RX ring start address */
b4f1255d
FF
387 iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0);
388 iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1);
fec3a23b
FF
389
390 /* Set interrupt waiting time and packet numbers */
31718ded
FF
391 iowrite16(0, ioaddr + MT_ICR);
392 iowrite16(0, ioaddr + MR_ICR);
fec3a23b
FF
393
394 /* Enable interrupts */
395 iowrite16(INT_MASK, ioaddr + MIER);
396
397 /* Enable TX and RX */
398 iowrite16(lp->mcr0 | 0x0002, ioaddr);
399
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);
824fb38e
FF
404
405 /* Check media */
406 mii_check_media(&lp->mii_if, 1, 1);
b4f1255d 407}
7a47dd7a 408
106adf3c
FF
409static void r6040_tx_timeout(struct net_device *dev)
410{
411 struct r6040_private *priv = netdev_priv(dev);
412 void __iomem *ioaddr = priv->base;
413
fec3a23b
FF
414 printk(KERN_WARNING "%s: transmit timed out, int enable %4.4x "
415 "status %4.4x, PHY status %4.4x\n",
106adf3c 416 dev->name, ioread16(ioaddr + MIER),
fec3a23b 417 ioread16(ioaddr + MISR),
c6e69bb9 418 r6040_mdio_read(dev, priv->mii_if.phy_id, MII_BMSR));
106adf3c 419
106adf3c 420 dev->stats.tx_errors++;
fec3a23b
FF
421
422 /* Reset MAC and re-init all registers */
423 r6040_init_mac_regs(dev);
106adf3c
FF
424}
425
7a47dd7a
SW
426static struct net_device_stats *r6040_get_stats(struct net_device *dev)
427{
428 struct r6040_private *priv = netdev_priv(dev);
429 void __iomem *ioaddr = priv->base;
430 unsigned long flags;
431
432 spin_lock_irqsave(&priv->lock, flags);
d248fd77
FF
433 dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1);
434 dev->stats.multicast += ioread8(ioaddr + ME_CNT0);
7a47dd7a
SW
435 spin_unlock_irqrestore(&priv->lock, flags);
436
d248fd77 437 return &dev->stats;
7a47dd7a
SW
438}
439
440/* Stop RDC MAC and Free the allocated resource */
441static void r6040_down(struct net_device *dev)
442{
443 struct r6040_private *lp = netdev_priv(dev);
444 void __iomem *ioaddr = lp->base;
7a47dd7a
SW
445 int limit = 2048;
446 u16 *adrp;
447 u16 cmd;
448
449 /* Stop MAC */
450 iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */
451 iowrite16(MAC_RST, ioaddr + MCR1); /* Reset RDC MAC */
452 while (limit--) {
453 cmd = ioread16(ioaddr + MCR1);
454 if (cmd & 0x1)
455 break;
456 }
457
458 /* Restore MAC Address to MIDx */
459 adrp = (u16 *) dev->dev_addr;
460 iowrite16(adrp[0], ioaddr + MID_0L);
461 iowrite16(adrp[1], ioaddr + MID_0M);
462 iowrite16(adrp[2], ioaddr + MID_0H);
7a47dd7a
SW
463}
464
5ac5d616 465static int r6040_close(struct net_device *dev)
7a47dd7a
SW
466{
467 struct r6040_private *lp = netdev_priv(dev);
58854c6b 468 struct pci_dev *pdev = lp->pdev;
7a47dd7a
SW
469
470 /* deleted timer */
471 del_timer_sync(&lp->timer);
472
473 spin_lock_irq(&lp->lock);
129cf9a7 474 napi_disable(&lp->napi);
7a47dd7a
SW
475 netif_stop_queue(dev);
476 r6040_down(dev);
58854c6b
FF
477
478 free_irq(dev->irq, dev);
479
480 /* Free RX buffer */
481 r6040_free_rxbufs(dev);
482
483 /* Free TX buffer */
484 r6040_free_txbufs(dev);
485
7a47dd7a
SW
486 spin_unlock_irq(&lp->lock);
487
58854c6b
FF
488 /* Free Descriptor memory */
489 if (lp->rx_ring) {
490 pci_free_consistent(pdev, RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma);
5b5103ec 491 lp->rx_ring = NULL;
58854c6b
FF
492 }
493
494 if (lp->tx_ring) {
495 pci_free_consistent(pdev, TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma);
5b5103ec 496 lp->tx_ring = NULL;
58854c6b
FF
497 }
498
7a47dd7a
SW
499 return 0;
500}
501
502/* Status of PHY CHIP */
c6e69bb9 503static int r6040_phy_mode_chk(struct net_device *dev)
7a47dd7a
SW
504{
505 struct r6040_private *lp = netdev_priv(dev);
506 void __iomem *ioaddr = lp->base;
507 int phy_dat;
508
509 /* PHY Link Status Check */
c6e69bb9 510 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
7a47dd7a
SW
511 if (!(phy_dat & 0x4))
512 phy_dat = 0x8000; /* Link Failed, full duplex */
513
514 /* PHY Chip Auto-Negotiation Status */
c6e69bb9 515 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 1);
7a47dd7a
SW
516 if (phy_dat & 0x0020) {
517 /* Auto Negotiation Mode */
c6e69bb9
FF
518 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 5);
519 phy_dat &= r6040_phy_read(ioaddr, lp->phy_addr, 4);
7a47dd7a
SW
520 if (phy_dat & 0x140)
521 /* Force full duplex */
522 phy_dat = 0x8000;
523 else
524 phy_dat = 0;
525 } else {
526 /* Force Mode */
c6e69bb9 527 phy_dat = r6040_phy_read(ioaddr, lp->phy_addr, 0);
7a47dd7a
SW
528 if (phy_dat & 0x100)
529 phy_dat = 0x8000;
530 else
531 phy_dat = 0x0000;
532 }
533
824fb38e
FF
534 mii_check_media(&lp->mii_if, 0, 1);
535
7a47dd7a
SW
536 return phy_dat;
537};
538
539static void r6040_set_carrier(struct mii_if_info *mii)
540{
c6e69bb9 541 if (r6040_phy_mode_chk(mii->dev)) {
7a47dd7a
SW
542 /* autoneg is off: Link is always assumed to be up */
543 if (!netif_carrier_ok(mii->dev))
544 netif_carrier_on(mii->dev);
545 } else
c6e69bb9 546 r6040_phy_mode_chk(mii->dev);
7a47dd7a
SW
547}
548
549static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
550{
551 struct r6040_private *lp = netdev_priv(dev);
5ac5d616 552 struct mii_ioctl_data *data = if_mii(rq);
7a47dd7a
SW
553 int rc;
554
555 if (!netif_running(dev))
556 return -EINVAL;
557 spin_lock_irq(&lp->lock);
558 rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
559 spin_unlock_irq(&lp->lock);
560 r6040_set_carrier(&lp->mii_if);
561 return rc;
562}
563
564static int r6040_rx(struct net_device *dev, int limit)
565{
566 struct r6040_private *priv = netdev_priv(dev);
9ca28dc4
FF
567 struct r6040_descriptor *descptr = priv->rx_remove_ptr;
568 struct sk_buff *skb_ptr, *new_skb;
569 int count = 0;
7a47dd7a
SW
570 u16 err;
571
9ca28dc4 572 /* Limit not reached and the descriptor belongs to the CPU */
32f565df 573 while (count < limit && !(descptr->status & DSC_OWNER_MAC)) {
9ca28dc4
FF
574 /* Read the descriptor status */
575 err = descptr->status;
576 /* Global error status set */
32f565df 577 if (err & DSC_RX_ERR) {
9ca28dc4 578 /* RX dribble */
32f565df 579 if (err & DSC_RX_ERR_DRI)
9ca28dc4
FF
580 dev->stats.rx_frame_errors++;
581 /* Buffer lenght exceeded */
32f565df 582 if (err & DSC_RX_ERR_BUF)
9ca28dc4
FF
583 dev->stats.rx_length_errors++;
584 /* Packet too long */
32f565df 585 if (err & DSC_RX_ERR_LONG)
9ca28dc4
FF
586 dev->stats.rx_length_errors++;
587 /* Packet < 64 bytes */
32f565df 588 if (err & DSC_RX_ERR_RUNT)
9ca28dc4
FF
589 dev->stats.rx_length_errors++;
590 /* CRC error */
32f565df 591 if (err & DSC_RX_ERR_CRC) {
9ca28dc4
FF
592 spin_lock(&priv->lock);
593 dev->stats.rx_crc_errors++;
594 spin_unlock(&priv->lock);
7a47dd7a 595 }
9ca28dc4
FF
596 goto next_descr;
597 }
598
599 /* Packet successfully received */
600 new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
601 if (!new_skb) {
602 dev->stats.rx_dropped++;
603 goto next_descr;
7a47dd7a 604 }
9ca28dc4
FF
605 skb_ptr = descptr->skb_ptr;
606 skb_ptr->dev = priv->dev;
607
608 /* Do not count the CRC */
609 skb_put(skb_ptr, descptr->len - 4);
610 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
611 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
612 skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev);
613
614 /* Send to upper layer */
615 netif_receive_skb(skb_ptr);
9ca28dc4
FF
616 dev->stats.rx_packets++;
617 dev->stats.rx_bytes += descptr->len - 4;
618
619 /* put new skb into descriptor */
620 descptr->skb_ptr = new_skb;
621 descptr->buf = cpu_to_le32(pci_map_single(priv->pdev,
622 descptr->skb_ptr->data,
623 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
624
625next_descr:
626 /* put the descriptor back to the MAC */
32f565df 627 descptr->status = DSC_OWNER_MAC;
9ca28dc4
FF
628 descptr = descptr->vndescp;
629 count++;
7a47dd7a 630 }
9ca28dc4 631 priv->rx_remove_ptr = descptr;
7a47dd7a
SW
632
633 return count;
634}
635
636static void r6040_tx(struct net_device *dev)
637{
638 struct r6040_private *priv = netdev_priv(dev);
639 struct r6040_descriptor *descptr;
640 void __iomem *ioaddr = priv->base;
641 struct sk_buff *skb_ptr;
642 u16 err;
643
644 spin_lock(&priv->lock);
645 descptr = priv->tx_remove_ptr;
646 while (priv->tx_free_desc < TX_DCNT) {
647 /* Check for errors */
648 err = ioread16(ioaddr + MLSR);
649
d248fd77
FF
650 if (err & 0x0200)
651 dev->stats.rx_fifo_errors++;
652 if (err & (0x2000 | 0x4000))
653 dev->stats.tx_carrier_errors++;
7a47dd7a 654
32f565df 655 if (descptr->status & DSC_OWNER_MAC)
ec6d2d45 656 break; /* Not complete */
7a47dd7a 657 skb_ptr = descptr->skb_ptr;
ed773b4a 658 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
7a47dd7a
SW
659 skb_ptr->len, PCI_DMA_TODEVICE);
660 /* Free buffer */
661 dev_kfree_skb_irq(skb_ptr);
662 descptr->skb_ptr = NULL;
663 /* To next descriptor */
664 descptr = descptr->vndescp;
665 priv->tx_free_desc++;
666 }
667 priv->tx_remove_ptr = descptr;
668
669 if (priv->tx_free_desc)
670 netif_wake_queue(dev);
671 spin_unlock(&priv->lock);
672}
673
674static int r6040_poll(struct napi_struct *napi, int budget)
675{
676 struct r6040_private *priv =
677 container_of(napi, struct r6040_private, napi);
678 struct net_device *dev = priv->dev;
679 void __iomem *ioaddr = priv->base;
680 int work_done;
681
682 work_done = r6040_rx(dev, budget);
683
684 if (work_done < budget) {
288379f0 685 napi_complete(napi);
7a47dd7a 686 /* Enable RX interrupt */
e24ddf3a 687 iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER);
7a47dd7a
SW
688 }
689 return work_done;
690}
691
692/* The RDC interrupt handler. */
693static irqreturn_t r6040_interrupt(int irq, void *dev_id)
694{
695 struct net_device *dev = dev_id;
696 struct r6040_private *lp = netdev_priv(dev);
697 void __iomem *ioaddr = lp->base;
3e7c469f 698 u16 misr, status;
7a47dd7a 699
3e7c469f
JC
700 /* Save MIER */
701 misr = ioread16(ioaddr + MIER);
7a47dd7a
SW
702 /* Mask off RDC MAC interrupt */
703 iowrite16(MSK_INT, ioaddr + MIER);
704 /* Read MISR status and clear */
705 status = ioread16(ioaddr + MISR);
706
35976d4d
FF
707 if (status == 0x0000 || status == 0xffff) {
708 /* Restore RDC MAC interrupt */
709 iowrite16(misr, ioaddr + MIER);
7a47dd7a 710 return IRQ_NONE;
35976d4d 711 }
7a47dd7a
SW
712
713 /* RX interrupt request */
e24ddf3a
FF
714 if (status & RX_INTS) {
715 if (status & RX_NO_DESC) {
716 /* RX descriptor unavailable */
717 dev->stats.rx_dropped++;
718 dev->stats.rx_missed_errors++;
719 }
720 if (status & RX_FIFO_FULL)
721 dev->stats.rx_fifo_errors++;
722
3d254348 723 /* Mask off RX interrupt */
3e7c469f 724 misr &= ~RX_INTS;
288379f0 725 napi_schedule(&lp->napi);
7a47dd7a
SW
726 }
727
728 /* TX interrupt request */
e24ddf3a 729 if (status & TX_INTS)
7a47dd7a
SW
730 r6040_tx(dev);
731
3e7c469f
JC
732 /* Restore RDC MAC interrupt */
733 iowrite16(misr, ioaddr + MIER);
734
ec6d2d45 735 return IRQ_HANDLED;
7a47dd7a
SW
736}
737
738#ifdef CONFIG_NET_POLL_CONTROLLER
739static void r6040_poll_controller(struct net_device *dev)
740{
741 disable_irq(dev->irq);
5ac5d616 742 r6040_interrupt(dev->irq, dev);
7a47dd7a
SW
743 enable_irq(dev->irq);
744}
745#endif
746
7a47dd7a 747/* Init RDC MAC */
3d463419 748static int r6040_up(struct net_device *dev)
7a47dd7a
SW
749{
750 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a 751 void __iomem *ioaddr = lp->base;
3d463419 752 int ret;
7a47dd7a 753
b4f1255d 754 /* Initialise and alloc RX/TX buffers */
3d463419
FF
755 r6040_init_txbufs(dev);
756 ret = r6040_alloc_rxbufs(dev);
757 if (ret)
758 return ret;
7a47dd7a 759
7a47dd7a 760 /* Read the PHY ID */
c6e69bb9 761 lp->switch_sig = r6040_phy_read(ioaddr, 0, 2);
7a47dd7a
SW
762
763 if (lp->switch_sig == ICPLUS_PHY_ID) {
c6e69bb9 764 r6040_phy_write(ioaddr, 29, 31, 0x175C); /* Enable registers */
7a47dd7a
SW
765 lp->phy_mode = 0x8000;
766 } else {
767 /* PHY Mode Check */
c6e69bb9
FF
768 r6040_phy_write(ioaddr, lp->phy_addr, 4, PHY_CAP);
769 r6040_phy_write(ioaddr, lp->phy_addr, 0, PHY_MODE);
7a47dd7a
SW
770
771 if (PHY_MODE == 0x3100)
c6e69bb9 772 lp->phy_mode = r6040_phy_mode_chk(dev);
7a47dd7a
SW
773 else
774 lp->phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
775 }
7a47dd7a 776
fec3a23b 777 /* Set duplex mode */
7a47dd7a 778 lp->mcr0 |= lp->phy_mode;
7a47dd7a
SW
779
780 /* improve performance (by RDC guys) */
c6e69bb9
FF
781 r6040_phy_write(ioaddr, 30, 17, (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
782 r6040_phy_write(ioaddr, 30, 17, ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
783 r6040_phy_write(ioaddr, 0, 19, 0x0000);
784 r6040_phy_write(ioaddr, 0, 30, 0x01F0);
7a47dd7a 785
fec3a23b
FF
786 /* Initialize all MAC registers */
787 r6040_init_mac_regs(dev);
3d463419
FF
788
789 return 0;
7a47dd7a
SW
790}
791
792/*
793 A periodic timer routine
794 Polling PHY Chip Link Status
795*/
796static void r6040_timer(unsigned long data)
797{
798 struct net_device *dev = (struct net_device *)data;
e6a9ea10 799 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a
SW
800 void __iomem *ioaddr = lp->base;
801 u16 phy_mode;
802
803 /* Polling PHY Chip Status */
804 if (PHY_MODE == 0x3100)
c6e69bb9 805 phy_mode = r6040_phy_mode_chk(dev);
7a47dd7a
SW
806 else
807 phy_mode = (PHY_MODE & 0x0100) ? 0x8000:0x0;
808
809 if (phy_mode != lp->phy_mode) {
810 lp->phy_mode = phy_mode;
811 lp->mcr0 = (lp->mcr0 & 0x7fff) | phy_mode;
812 iowrite16(lp->mcr0, ioaddr);
7a47dd7a
SW
813 }
814
815 /* Timer active again */
208aefa2 816 mod_timer(&lp->timer, round_jiffies(jiffies + HZ));
7a47dd7a
SW
817}
818
819/* Read/set MAC address routines */
820static void r6040_mac_address(struct net_device *dev)
821{
822 struct r6040_private *lp = netdev_priv(dev);
823 void __iomem *ioaddr = lp->base;
824 u16 *adrp;
825
826 /* MAC operation register */
827 iowrite16(0x01, ioaddr + MCR1); /* Reset MAC */
828 iowrite16(2, ioaddr + MAC_SM); /* Reset internal state machine */
829 iowrite16(0, ioaddr + MAC_SM);
c1d69937 830 mdelay(5);
7a47dd7a
SW
831
832 /* Restore MAC Address */
833 adrp = (u16 *) dev->dev_addr;
834 iowrite16(adrp[0], ioaddr + MID_0L);
835 iowrite16(adrp[1], ioaddr + MID_0M);
836 iowrite16(adrp[2], ioaddr + MID_0H);
837}
838
5ac5d616 839static int r6040_open(struct net_device *dev)
7a47dd7a 840{
5ac5d616 841 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a
SW
842 int ret;
843
844 /* Request IRQ and Register interrupt handler */
91dcbf36 845 ret = request_irq(dev->irq, r6040_interrupt,
7a47dd7a
SW
846 IRQF_SHARED, dev->name, dev);
847 if (ret)
848 return ret;
849
850 /* Set MAC address */
851 r6040_mac_address(dev);
852
853 /* Allocate Descriptor memory */
6c323103
FR
854 lp->rx_ring =
855 pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma);
856 if (!lp->rx_ring)
7a47dd7a
SW
857 return -ENOMEM;
858
6c323103
FR
859 lp->tx_ring =
860 pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma);
861 if (!lp->tx_ring) {
862 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
863 lp->rx_ring_dma);
864 return -ENOMEM;
865 }
866
3d463419
FF
867 ret = r6040_up(dev);
868 if (ret) {
869 pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring,
870 lp->tx_ring_dma);
871 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
872 lp->rx_ring_dma);
873 return ret;
874 }
7a47dd7a
SW
875
876 napi_enable(&lp->napi);
877 netif_start_queue(dev);
878
106adf3c
FF
879 /* set and active a timer process */
880 setup_timer(&lp->timer, r6040_timer, (unsigned long) dev);
881 if (lp->switch_sig != ICPLUS_PHY_ID)
882 mod_timer(&lp->timer, jiffies + HZ);
7a47dd7a
SW
883 return 0;
884}
885
61357325
SH
886static netdev_tx_t r6040_start_xmit(struct sk_buff *skb,
887 struct net_device *dev)
7a47dd7a
SW
888{
889 struct r6040_private *lp = netdev_priv(dev);
890 struct r6040_descriptor *descptr;
891 void __iomem *ioaddr = lp->base;
892 unsigned long flags;
7a47dd7a
SW
893
894 /* Critical Section */
895 spin_lock_irqsave(&lp->lock, flags);
896
897 /* TX resource check */
898 if (!lp->tx_free_desc) {
899 spin_unlock_irqrestore(&lp->lock, flags);
092427be 900 netif_stop_queue(dev);
7a47dd7a 901 printk(KERN_ERR DRV_NAME ": no tx descriptor\n");
61357325 902 return NETDEV_TX_BUSY;
7a47dd7a
SW
903 }
904
905 /* Statistic Counter */
906 dev->stats.tx_packets++;
907 dev->stats.tx_bytes += skb->len;
908 /* Set TX descriptor & Transmit it */
909 lp->tx_free_desc--;
910 descptr = lp->tx_insert_ptr;
911 if (skb->len < MISR)
912 descptr->len = MISR;
913 else
914 descptr->len = skb->len;
915
916 descptr->skb_ptr = skb;
917 descptr->buf = cpu_to_le32(pci_map_single(lp->pdev,
918 skb->data, skb->len, PCI_DMA_TODEVICE));
32f565df 919 descptr->status = DSC_OWNER_MAC;
7a47dd7a
SW
920 /* Trigger the MAC to check the TX descriptor */
921 iowrite16(0x01, ioaddr + MTPR);
922 lp->tx_insert_ptr = descptr->vndescp;
923
924 /* If no tx resource, stop */
925 if (!lp->tx_free_desc)
926 netif_stop_queue(dev);
927
928 dev->trans_start = jiffies;
929 spin_unlock_irqrestore(&lp->lock, flags);
61357325
SH
930
931 return NETDEV_TX_OK;
7a47dd7a
SW
932}
933
5ac5d616 934static void r6040_multicast_list(struct net_device *dev)
7a47dd7a
SW
935{
936 struct r6040_private *lp = netdev_priv(dev);
937 void __iomem *ioaddr = lp->base;
938 u16 *adrp;
939 u16 reg;
940 unsigned long flags;
941 struct dev_mc_list *dmi = dev->mc_list;
942 int i;
943
944 /* MAC Address */
945 adrp = (u16 *)dev->dev_addr;
946 iowrite16(adrp[0], ioaddr + MID_0L);
947 iowrite16(adrp[1], ioaddr + MID_0M);
948 iowrite16(adrp[2], ioaddr + MID_0H);
949
950 /* Promiscous Mode */
951 spin_lock_irqsave(&lp->lock, flags);
952
953 /* Clear AMCP & PROM bits */
954 reg = ioread16(ioaddr) & ~0x0120;
955 if (dev->flags & IFF_PROMISC) {
956 reg |= 0x0020;
957 lp->mcr0 |= 0x0020;
958 }
959 /* Too many multicast addresses
960 * accept all traffic */
8e95a202 961 else if ((dev->mc_count > MCAST_MAX) || (dev->flags & IFF_ALLMULTI))
7a47dd7a
SW
962 reg |= 0x0020;
963
964 iowrite16(reg, ioaddr);
965 spin_unlock_irqrestore(&lp->lock, flags);
966
967 /* Build the hash table */
968 if (dev->mc_count > MCAST_MAX) {
969 u16 hash_table[4];
970 u32 crc;
971
972 for (i = 0; i < 4; i++)
973 hash_table[i] = 0;
974
975 for (i = 0; i < dev->mc_count; i++) {
976 char *addrs = dmi->dmi_addr;
977
978 dmi = dmi->next;
979
980 if (!(*addrs & 1))
981 continue;
982
983 crc = ether_crc_le(6, addrs);
984 crc >>= 26;
985 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
986 }
987 /* Write the index of the hash table */
988 for (i = 0; i < 4; i++)
989 iowrite16(hash_table[i] << 14, ioaddr + MCR1);
990 /* Fill the MAC hash tables with their values */
991 iowrite16(hash_table[0], ioaddr + MAR0);
992 iowrite16(hash_table[1], ioaddr + MAR1);
993 iowrite16(hash_table[2], ioaddr + MAR2);
994 iowrite16(hash_table[3], ioaddr + MAR3);
995 }
996 /* Multicast Address 1~4 case */
997 for (i = 0, dmi; (i < dev->mc_count) && (i < MCAST_MAX); i++) {
998 adrp = (u16 *)dmi->dmi_addr;
999 iowrite16(adrp[0], ioaddr + MID_1L + 8*i);
1000 iowrite16(adrp[1], ioaddr + MID_1M + 8*i);
1001 iowrite16(adrp[2], ioaddr + MID_1H + 8*i);
1002 dmi = dmi->next;
1003 }
1004 for (i = dev->mc_count; i < MCAST_MAX; i++) {
1005 iowrite16(0xffff, ioaddr + MID_0L + 8*i);
1006 iowrite16(0xffff, ioaddr + MID_0M + 8*i);
1007 iowrite16(0xffff, ioaddr + MID_0H + 8*i);
1008 }
1009}
1010
1011static void netdev_get_drvinfo(struct net_device *dev,
1012 struct ethtool_drvinfo *info)
1013{
1014 struct r6040_private *rp = netdev_priv(dev);
1015
1016 strcpy(info->driver, DRV_NAME);
1017 strcpy(info->version, DRV_VERSION);
1018 strcpy(info->bus_info, pci_name(rp->pdev));
1019}
1020
1021static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1022{
1023 struct r6040_private *rp = netdev_priv(dev);
1024 int rc;
1025
1026 spin_lock_irq(&rp->lock);
1027 rc = mii_ethtool_gset(&rp->mii_if, cmd);
092427be 1028 spin_unlock_irq(&rp->lock);
7a47dd7a
SW
1029
1030 return rc;
1031}
1032
1033static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
1034{
1035 struct r6040_private *rp = netdev_priv(dev);
1036 int rc;
1037
1038 spin_lock_irq(&rp->lock);
1039 rc = mii_ethtool_sset(&rp->mii_if, cmd);
1040 spin_unlock_irq(&rp->lock);
1041 r6040_set_carrier(&rp->mii_if);
1042
1043 return rc;
1044}
1045
1046static u32 netdev_get_link(struct net_device *dev)
1047{
1048 struct r6040_private *rp = netdev_priv(dev);
1049
1050 return mii_link_ok(&rp->mii_if);
1051}
1052
a7bd89cb 1053static const struct ethtool_ops netdev_ethtool_ops = {
7a47dd7a
SW
1054 .get_drvinfo = netdev_get_drvinfo,
1055 .get_settings = netdev_get_settings,
1056 .set_settings = netdev_set_settings,
1057 .get_link = netdev_get_link,
1058};
1059
a7bd89cb
SH
1060static const struct net_device_ops r6040_netdev_ops = {
1061 .ndo_open = r6040_open,
1062 .ndo_stop = r6040_close,
1063 .ndo_start_xmit = r6040_start_xmit,
1064 .ndo_get_stats = r6040_get_stats,
1065 .ndo_set_multicast_list = r6040_multicast_list,
1066 .ndo_change_mtu = eth_change_mtu,
1067 .ndo_validate_addr = eth_validate_addr,
fe96aaa1 1068 .ndo_set_mac_address = eth_mac_addr,
a7bd89cb
SH
1069 .ndo_do_ioctl = r6040_ioctl,
1070 .ndo_tx_timeout = r6040_tx_timeout,
1071#ifdef CONFIG_NET_POLL_CONTROLLER
1072 .ndo_poll_controller = r6040_poll_controller,
1073#endif
1074};
1075
7a47dd7a
SW
1076static int __devinit r6040_init_one(struct pci_dev *pdev,
1077 const struct pci_device_id *ent)
1078{
1079 struct net_device *dev;
1080 struct r6040_private *lp;
1081 void __iomem *ioaddr;
1082 int err, io_size = R6040_IO_SIZE;
1083 static int card_idx = -1;
1084 int bar = 0;
7a47dd7a
SW
1085 u16 *adrp;
1086
3fa8486b 1087 printk("%s\n", version);
7a47dd7a
SW
1088
1089 err = pci_enable_device(pdev);
1090 if (err)
b0e45390 1091 goto err_out;
7a47dd7a
SW
1092
1093 /* this should always be supported */
284901a9 1094 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
b0e45390 1095 if (err) {
9a48ce84 1096 printk(KERN_ERR DRV_NAME ": 32-bit PCI DMA addresses"
7a47dd7a 1097 "not supported by the card\n");
b0e45390 1098 goto err_out;
7a47dd7a 1099 }
284901a9 1100 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
b0e45390 1101 if (err) {
9a48ce84 1102 printk(KERN_ERR DRV_NAME ": 32-bit PCI DMA addresses"
092427be 1103 "not supported by the card\n");
b0e45390 1104 goto err_out;
092427be 1105 }
7a47dd7a
SW
1106
1107 /* IO Size check */
6f5bec19 1108 if (pci_resource_len(pdev, bar) < io_size) {
9a48ce84 1109 printk(KERN_ERR DRV_NAME ": Insufficient PCI resources, aborting\n");
b0e45390
FF
1110 err = -EIO;
1111 goto err_out;
7a47dd7a
SW
1112 }
1113
7a47dd7a
SW
1114 pci_set_master(pdev);
1115
1116 dev = alloc_etherdev(sizeof(struct r6040_private));
1117 if (!dev) {
9a48ce84 1118 printk(KERN_ERR DRV_NAME ": Failed to allocate etherdev\n");
b0e45390
FF
1119 err = -ENOMEM;
1120 goto err_out;
7a47dd7a
SW
1121 }
1122 SET_NETDEV_DEV(dev, &pdev->dev);
1123 lp = netdev_priv(dev);
7a47dd7a 1124
b0e45390
FF
1125 err = pci_request_regions(pdev, DRV_NAME);
1126
1127 if (err) {
7a47dd7a 1128 printk(KERN_ERR DRV_NAME ": Failed to request PCI regions\n");
b0e45390 1129 goto err_out_free_dev;
7a47dd7a
SW
1130 }
1131
1132 ioaddr = pci_iomap(pdev, bar, io_size);
1133 if (!ioaddr) {
9a48ce84 1134 printk(KERN_ERR DRV_NAME ": ioremap failed for device %s\n",
7a47dd7a 1135 pci_name(pdev));
b0e45390
FF
1136 err = -EIO;
1137 goto err_out_free_res;
7a47dd7a 1138 }
84314bf9
FF
1139 /* If PHY status change register is still set to zero it means the
1140 * bootloader didn't initialize it */
1141 if (ioread16(ioaddr + PHY_CC) == 0)
1142 iowrite16(0x9f07, ioaddr + PHY_CC);
7a47dd7a
SW
1143
1144 /* Init system & device */
7a47dd7a
SW
1145 lp->base = ioaddr;
1146 dev->irq = pdev->irq;
1147
1148 spin_lock_init(&lp->lock);
1149 pci_set_drvdata(pdev, dev);
1150
1151 /* Set MAC address */
1152 card_idx++;
1153
1154 adrp = (u16 *)dev->dev_addr;
1155 adrp[0] = ioread16(ioaddr + MID_0L);
1156 adrp[1] = ioread16(ioaddr + MID_0M);
1157 adrp[2] = ioread16(ioaddr + MID_0H);
1158
1d2b1a76
FF
1159 /* Some bootloader/BIOSes do not initialize
1160 * MAC address, warn about that */
9f113618
FF
1161 if (!(adrp[0] || adrp[1] || adrp[2])) {
1162 printk(KERN_WARNING DRV_NAME ": MAC address not initialized, generating random\n");
1163 random_ether_addr(dev->dev_addr);
1164 }
1d2b1a76 1165
7a47dd7a
SW
1166 /* Link new device into r6040_root_dev */
1167 lp->pdev = pdev;
129cf9a7 1168 lp->dev = dev;
7a47dd7a
SW
1169
1170 /* Init RDC private data */
1171 lp->mcr0 = 0x1002;
1172 lp->phy_addr = phy_table[card_idx];
1173 lp->switch_sig = 0;
1174
1175 /* The RDC-specific entries in the device structure. */
a7bd89cb 1176 dev->netdev_ops = &r6040_netdev_ops;
7a47dd7a 1177 dev->ethtool_ops = &netdev_ethtool_ops;
7a47dd7a 1178 dev->watchdog_timeo = TX_TIMEOUT;
a7bd89cb 1179
7a47dd7a
SW
1180 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
1181 lp->mii_if.dev = dev;
c6e69bb9
FF
1182 lp->mii_if.mdio_read = r6040_mdio_read;
1183 lp->mii_if.mdio_write = r6040_mdio_write;
7a47dd7a
SW
1184 lp->mii_if.phy_id = lp->phy_addr;
1185 lp->mii_if.phy_id_mask = 0x1f;
1186 lp->mii_if.reg_num_mask = 0x1f;
1187
e03f614a
MK
1188 /* Check the vendor ID on the PHY, if 0xffff assume none attached */
1189 if (r6040_phy_read(ioaddr, lp->phy_addr, 2) == 0xffff) {
1190 printk(KERN_ERR DRV_NAME ": Failed to detect an attached PHY\n");
1191 err = -ENODEV;
1192 goto err_out_unmap;
1193 }
1194
7a47dd7a
SW
1195 /* Register net device. After this dev->name assign */
1196 err = register_netdev(dev);
1197 if (err) {
1198 printk(KERN_ERR DRV_NAME ": Failed to register net device\n");
b0e45390 1199 goto err_out_unmap;
7a47dd7a
SW
1200 }
1201 return 0;
1202
b0e45390
FF
1203err_out_unmap:
1204 pci_iounmap(pdev, ioaddr);
1205err_out_free_res:
7a47dd7a 1206 pci_release_regions(pdev);
b0e45390 1207err_out_free_dev:
7a47dd7a 1208 free_netdev(dev);
b0e45390 1209err_out:
7a47dd7a
SW
1210 return err;
1211}
1212
1213static void __devexit r6040_remove_one(struct pci_dev *pdev)
1214{
1215 struct net_device *dev = pci_get_drvdata(pdev);
1216
1217 unregister_netdev(dev);
1218 pci_release_regions(pdev);
1219 free_netdev(dev);
1220 pci_disable_device(pdev);
1221 pci_set_drvdata(pdev, NULL);
1222}
1223
1224
a3aa1884 1225static DEFINE_PCI_DEVICE_TABLE(r6040_pci_tbl) = {
5ac5d616
FR
1226 { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
1227 { 0 }
7a47dd7a
SW
1228};
1229MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1230
1231static struct pci_driver r6040_driver = {
5ac5d616 1232 .name = DRV_NAME,
7a47dd7a
SW
1233 .id_table = r6040_pci_tbl,
1234 .probe = r6040_init_one,
1235 .remove = __devexit_p(r6040_remove_one),
1236};
1237
1238
1239static int __init r6040_init(void)
1240{
1241 return pci_register_driver(&r6040_driver);
1242}
1243
1244
1245static void __exit r6040_cleanup(void)
1246{
1247 pci_unregister_driver(&r6040_driver);
1248}
1249
1250module_init(r6040_init);
1251module_exit(r6040_cleanup);
This page took 0.46885 seconds and 5 git commands to generate.