b43legacy: drop ssb-duplicated workaround for dangling cores
[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>
7a47dd7a
SW
32#include <linux/interrupt.h>
33#include <linux/pci.h>
34#include <linux/netdevice.h>
35#include <linux/etherdevice.h>
36#include <linux/skbuff.h>
37#include <linux/init.h>
38#include <linux/delay.h>
39#include <linux/mii.h>
40#include <linux/ethtool.h>
41#include <linux/crc32.h>
42#include <linux/spinlock.h>
092427be
JG
43#include <linux/bitops.h>
44#include <linux/io.h>
45#include <linux/irq.h>
46#include <linux/uaccess.h>
3831861b 47#include <linux/phy.h>
7a47dd7a
SW
48
49#include <asm/processor.h>
7a47dd7a
SW
50
51#define DRV_NAME "r6040"
86f99fff
FF
52#define DRV_VERSION "0.27"
53#define DRV_RELDATE "23Feb2011"
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 */
c60c9c71
SL
72#define MCR0_PROMISC 0x0020 /* Promiscuous mode */
73#define MCR0_HASH_EN 0x0100 /* Enable multicast hash table function */
7a47dd7a
SW
74#define MCR1 0x04 /* Control register 1 */
75#define MAC_RST 0x0001 /* Reset the MAC */
76#define MBCR 0x08 /* Bus control */
77#define MT_ICR 0x0C /* TX interrupt control */
78#define MR_ICR 0x10 /* RX interrupt control */
79#define MTPR 0x14 /* TX poll command register */
80#define MR_BSR 0x18 /* RX buffer size */
81#define MR_DCR 0x1A /* RX descriptor control */
82#define MLSR 0x1C /* Last status */
83#define MMDIO 0x20 /* MDIO control register */
84#define MDIO_WRITE 0x4000 /* MDIO write */
85#define MDIO_READ 0x2000 /* MDIO read */
86#define MMRD 0x24 /* MDIO read data register */
87#define MMWD 0x28 /* MDIO write data register */
88#define MTD_SA0 0x2C /* TX descriptor start address 0 */
89#define MTD_SA1 0x30 /* TX descriptor start address 1 */
90#define MRD_SA0 0x34 /* RX descriptor start address 0 */
91#define MRD_SA1 0x38 /* RX descriptor start address 1 */
92#define MISR 0x3C /* Status register */
93#define MIER 0x40 /* INT enable register */
94#define MSK_INT 0x0000 /* Mask off interrupts */
3d254348
FF
95#define RX_FINISH 0x0001 /* RX finished */
96#define RX_NO_DESC 0x0002 /* No RX descriptor available */
97#define RX_FIFO_FULL 0x0004 /* RX FIFO full */
98#define RX_EARLY 0x0008 /* RX early */
99#define TX_FINISH 0x0010 /* TX finished */
100#define TX_EARLY 0x0080 /* TX early */
101#define EVENT_OVRFL 0x0100 /* Event counter overflow */
102#define LINK_CHANGED 0x0200 /* PHY link changed */
7a47dd7a
SW
103#define ME_CISR 0x44 /* Event counter INT status */
104#define ME_CIER 0x48 /* Event counter INT enable */
105#define MR_CNT 0x50 /* Successfully received packet counter */
106#define ME_CNT0 0x52 /* Event counter 0 */
107#define ME_CNT1 0x54 /* Event counter 1 */
108#define ME_CNT2 0x56 /* Event counter 2 */
109#define ME_CNT3 0x58 /* Event counter 3 */
110#define MT_CNT 0x5A /* Successfully transmit packet counter */
111#define ME_CNT4 0x5C /* Event counter 4 */
112#define MP_CNT 0x5E /* Pause frame counter register */
113#define MAR0 0x60 /* Hash table 0 */
114#define MAR1 0x62 /* Hash table 1 */
115#define MAR2 0x64 /* Hash table 2 */
116#define MAR3 0x66 /* Hash table 3 */
117#define MID_0L 0x68 /* Multicast address MID0 Low */
118#define MID_0M 0x6A /* Multicast address MID0 Medium */
119#define MID_0H 0x6C /* Multicast address MID0 High */
120#define MID_1L 0x70 /* MID1 Low */
121#define MID_1M 0x72 /* MID1 Medium */
122#define MID_1H 0x74 /* MID1 High */
123#define MID_2L 0x78 /* MID2 Low */
124#define MID_2M 0x7A /* MID2 Medium */
125#define MID_2H 0x7C /* MID2 High */
126#define MID_3L 0x80 /* MID3 Low */
127#define MID_3M 0x82 /* MID3 Medium */
128#define MID_3H 0x84 /* MID3 High */
129#define PHY_CC 0x88 /* PHY status change configuration register */
130#define PHY_ST 0x8A /* PHY status register */
131#define MAC_SM 0xAC /* MAC status machine */
132#define MAC_ID 0xBE /* Identifier register */
133
134#define TX_DCNT 0x80 /* TX descriptor count */
135#define RX_DCNT 0x80 /* RX descriptor count */
136#define MAX_BUF_SIZE 0x600
6c323103
FR
137#define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
138#define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
7a47dd7a 139#define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
3bcf8229 140#define MCAST_MAX 3 /* Max number multicast addresses to filter */
7a47dd7a 141
32f565df
FF
142/* Descriptor status */
143#define DSC_OWNER_MAC 0x8000 /* MAC is the owner of this descriptor */
144#define DSC_RX_OK 0x4000 /* RX was successful */
145#define DSC_RX_ERR 0x0800 /* RX PHY error */
146#define DSC_RX_ERR_DRI 0x0400 /* RX dribble packet */
147#define DSC_RX_ERR_BUF 0x0200 /* RX length exceeds buffer size */
148#define DSC_RX_ERR_LONG 0x0100 /* RX length > maximum packet length */
149#define DSC_RX_ERR_RUNT 0x0080 /* RX packet length < 64 byte */
150#define DSC_RX_ERR_CRC 0x0040 /* RX CRC error */
151#define DSC_RX_BCAST 0x0020 /* RX broadcast (no error) */
152#define DSC_RX_MCAST 0x0010 /* RX multicast (no error) */
153#define DSC_RX_MCH_HIT 0x0008 /* RX multicast hit in hash table (no error) */
154#define DSC_RX_MIDH_HIT 0x0004 /* RX MID table hit (no error) */
155#define DSC_RX_IDX_MID_MASK 3 /* RX mask for the index of matched MIDx */
156
7a47dd7a
SW
157/* PHY settings */
158#define ICPLUS_PHY_ID 0x0243
159
160MODULE_AUTHOR("Sten Wang <sten.wang@rdc.com.tw>,"
161 "Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>,"
162 "Florian Fainelli <florian@openwrt.org>");
163MODULE_LICENSE("GPL");
164MODULE_DESCRIPTION("RDC R6040 NAPI PCI FastEthernet driver");
bc4de260 165MODULE_VERSION(DRV_VERSION " " DRV_RELDATE);
7a47dd7a 166
3d254348 167/* RX and TX interrupts that we handle */
e24ddf3a
FF
168#define RX_INTS (RX_FIFO_FULL | RX_NO_DESC | RX_FINISH)
169#define TX_INTS (TX_FINISH)
170#define INT_MASK (RX_INTS | TX_INTS)
7a47dd7a
SW
171
172struct r6040_descriptor {
173 u16 status, len; /* 0-3 */
174 __le32 buf; /* 4-7 */
175 __le32 ndesc; /* 8-B */
176 u32 rev1; /* C-F */
177 char *vbufp; /* 10-13 */
178 struct r6040_descriptor *vndescp; /* 14-17 */
179 struct sk_buff *skb_ptr; /* 18-1B */
180 u32 rev2; /* 1C-1F */
181} __attribute__((aligned(32)));
182
183struct r6040_private {
184 spinlock_t lock; /* driver lock */
7a47dd7a
SW
185 struct pci_dev *pdev;
186 struct r6040_descriptor *rx_insert_ptr;
187 struct r6040_descriptor *rx_remove_ptr;
188 struct r6040_descriptor *tx_insert_ptr;
189 struct r6040_descriptor *tx_remove_ptr;
6c323103
FR
190 struct r6040_descriptor *rx_ring;
191 struct r6040_descriptor *tx_ring;
192 dma_addr_t rx_ring_dma;
193 dma_addr_t tx_ring_dma;
3831861b 194 u16 tx_free_desc, phy_addr;
7a47dd7a 195 u16 mcr0, mcr1;
7a47dd7a 196 struct net_device *dev;
3831861b 197 struct mii_bus *mii_bus;
7a47dd7a 198 struct napi_struct napi;
7a47dd7a 199 void __iomem *base;
3831861b
FF
200 struct phy_device *phydev;
201 int old_link;
202 int old_duplex;
7a47dd7a
SW
203};
204
2154c704 205static char version[] __devinitdata = DRV_NAME
7a47dd7a 206 ": RDC R6040 NAPI net driver,"
9a48ce84 207 "version "DRV_VERSION " (" DRV_RELDATE ")";
7a47dd7a 208
092427be 209static int phy_table[] = { PHY1_ADDR, PHY2_ADDR };
7a47dd7a
SW
210
211/* Read a word data from PHY Chip */
c6e69bb9 212static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg)
7a47dd7a
SW
213{
214 int limit = 2048;
215 u16 cmd;
216
217 iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO);
218 /* Wait for the read bit to be cleared */
219 while (limit--) {
220 cmd = ioread16(ioaddr + MMDIO);
11e5e8f5 221 if (!(cmd & MDIO_READ))
7a47dd7a
SW
222 break;
223 }
224
225 return ioread16(ioaddr + MMRD);
226}
227
228/* Write a word data from PHY Chip */
2154c704
FF
229static void r6040_phy_write(void __iomem *ioaddr,
230 int phy_addr, int reg, u16 val)
7a47dd7a
SW
231{
232 int limit = 2048;
233 u16 cmd;
234
235 iowrite16(val, ioaddr + MMWD);
236 /* Write the command to the MDIO bus */
237 iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO);
238 /* Wait for the write bit to be cleared */
239 while (limit--) {
240 cmd = ioread16(ioaddr + MMDIO);
11e5e8f5 241 if (!(cmd & MDIO_WRITE))
7a47dd7a
SW
242 break;
243 }
244}
245
3831861b 246static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg)
7a47dd7a 247{
3831861b 248 struct net_device *dev = bus->priv;
7a47dd7a
SW
249 struct r6040_private *lp = netdev_priv(dev);
250 void __iomem *ioaddr = lp->base;
251
3831861b 252 return r6040_phy_read(ioaddr, phy_addr, reg);
7a47dd7a
SW
253}
254
3831861b
FF
255static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr,
256 int reg, u16 value)
7a47dd7a 257{
3831861b 258 struct net_device *dev = bus->priv;
7a47dd7a
SW
259 struct r6040_private *lp = netdev_priv(dev);
260 void __iomem *ioaddr = lp->base;
261
3831861b
FF
262 r6040_phy_write(ioaddr, phy_addr, reg, value);
263
264 return 0;
265}
266
267static int r6040_mdiobus_reset(struct mii_bus *bus)
268{
269 return 0;
7a47dd7a
SW
270}
271
b4f1255d
FF
272static void r6040_free_txbufs(struct net_device *dev)
273{
274 struct r6040_private *lp = netdev_priv(dev);
275 int i;
276
277 for (i = 0; i < TX_DCNT; i++) {
278 if (lp->tx_insert_ptr->skb_ptr) {
ed773b4a
AV
279 pci_unmap_single(lp->pdev,
280 le32_to_cpu(lp->tx_insert_ptr->buf),
b4f1255d
FF
281 MAX_BUF_SIZE, PCI_DMA_TODEVICE);
282 dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
3b060be0 283 lp->tx_insert_ptr->skb_ptr = NULL;
b4f1255d
FF
284 }
285 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
286 }
287}
288
289static void r6040_free_rxbufs(struct net_device *dev)
290{
291 struct r6040_private *lp = netdev_priv(dev);
292 int i;
293
294 for (i = 0; i < RX_DCNT; i++) {
295 if (lp->rx_insert_ptr->skb_ptr) {
ed773b4a
AV
296 pci_unmap_single(lp->pdev,
297 le32_to_cpu(lp->rx_insert_ptr->buf),
b4f1255d
FF
298 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
299 dev_kfree_skb(lp->rx_insert_ptr->skb_ptr);
300 lp->rx_insert_ptr->skb_ptr = NULL;
301 }
302 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
303 }
304}
305
b4f1255d
FF
306static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring,
307 dma_addr_t desc_dma, int size)
308{
309 struct r6040_descriptor *desc = desc_ring;
310 dma_addr_t mapping = desc_dma;
311
312 while (size-- > 0) {
3f6602ad 313 mapping += sizeof(*desc);
b4f1255d
FF
314 desc->ndesc = cpu_to_le32(mapping);
315 desc->vndescp = desc + 1;
316 desc++;
317 }
318 desc--;
319 desc->ndesc = cpu_to_le32(desc_dma);
320 desc->vndescp = desc_ring;
321}
322
3d463419 323static void r6040_init_txbufs(struct net_device *dev)
b4f1255d
FF
324{
325 struct r6040_private *lp = netdev_priv(dev);
b4f1255d
FF
326
327 lp->tx_free_desc = TX_DCNT;
328
329 lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring;
330 r6040_init_ring_desc(lp->tx_ring, lp->tx_ring_dma, TX_DCNT);
b4f1255d
FF
331}
332
3d463419 333static int r6040_alloc_rxbufs(struct net_device *dev)
b4f1255d
FF
334{
335 struct r6040_private *lp = netdev_priv(dev);
3d463419
FF
336 struct r6040_descriptor *desc;
337 struct sk_buff *skb;
338 int rc;
b4f1255d
FF
339
340 lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring;
341 r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT);
342
3d463419
FF
343 /* Allocate skbs for the rx descriptors */
344 desc = lp->rx_ring;
345 do {
346 skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
347 if (!skb) {
7d53b809 348 netdev_err(dev, "failed to alloc skb for rx\n");
3d463419
FF
349 rc = -ENOMEM;
350 goto err_exit;
351 }
352 desc->skb_ptr = skb;
353 desc->buf = cpu_to_le32(pci_map_single(lp->pdev,
2154c704
FF
354 desc->skb_ptr->data,
355 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
32f565df 356 desc->status = DSC_OWNER_MAC;
3d463419
FF
357 desc = desc->vndescp;
358 } while (desc != lp->rx_ring);
359
360 return 0;
361
362err_exit:
363 /* Deallocate all previously allocated skbs */
364 r6040_free_rxbufs(dev);
365 return rc;
fec3a23b
FF
366}
367
368static void r6040_init_mac_regs(struct net_device *dev)
369{
370 struct r6040_private *lp = netdev_priv(dev);
371 void __iomem *ioaddr = lp->base;
372 int limit = 2048;
373 u16 cmd;
374
375 /* Mask Off Interrupt */
376 iowrite16(MSK_INT, ioaddr + MIER);
377
378 /* Reset RDC MAC */
379 iowrite16(MAC_RST, ioaddr + MCR1);
380 while (limit--) {
381 cmd = ioread16(ioaddr + MCR1);
382 if (cmd & 0x1)
383 break;
384 }
385 /* Reset internal state machine */
386 iowrite16(2, ioaddr + MAC_SM);
387 iowrite16(0, ioaddr + MAC_SM);
c1d69937 388 mdelay(5);
fec3a23b
FF
389
390 /* MAC Bus Control Register */
391 iowrite16(MBCR_DEFAULT, ioaddr + MBCR);
392
393 /* Buffer Size Register */
394 iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR);
395
396 /* Write TX ring start address */
397 iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0);
398 iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1);
b4f1255d 399
fec3a23b 400 /* Write RX ring start address */
b4f1255d
FF
401 iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0);
402 iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1);
fec3a23b
FF
403
404 /* Set interrupt waiting time and packet numbers */
31718ded
FF
405 iowrite16(0, ioaddr + MT_ICR);
406 iowrite16(0, ioaddr + MR_ICR);
fec3a23b
FF
407
408 /* Enable interrupts */
409 iowrite16(INT_MASK, ioaddr + MIER);
410
411 /* Enable TX and RX */
412 iowrite16(lp->mcr0 | 0x0002, ioaddr);
413
414 /* Let TX poll the descriptors
415 * we may got called by r6040_tx_timeout which has left
416 * some unsent tx buffers */
417 iowrite16(0x01, ioaddr + MTPR);
b4f1255d 418}
7a47dd7a 419
106adf3c
FF
420static void r6040_tx_timeout(struct net_device *dev)
421{
422 struct r6040_private *priv = netdev_priv(dev);
423 void __iomem *ioaddr = priv->base;
424
7d53b809 425 netdev_warn(dev, "transmit timed out, int enable %4.4x "
3831861b 426 "status %4.4x\n",
7d53b809 427 ioread16(ioaddr + MIER),
3831861b 428 ioread16(ioaddr + MISR));
106adf3c 429
106adf3c 430 dev->stats.tx_errors++;
fec3a23b
FF
431
432 /* Reset MAC and re-init all registers */
433 r6040_init_mac_regs(dev);
106adf3c
FF
434}
435
7a47dd7a
SW
436static struct net_device_stats *r6040_get_stats(struct net_device *dev)
437{
438 struct r6040_private *priv = netdev_priv(dev);
439 void __iomem *ioaddr = priv->base;
440 unsigned long flags;
441
442 spin_lock_irqsave(&priv->lock, flags);
d248fd77
FF
443 dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1);
444 dev->stats.multicast += ioread8(ioaddr + ME_CNT0);
7a47dd7a
SW
445 spin_unlock_irqrestore(&priv->lock, flags);
446
d248fd77 447 return &dev->stats;
7a47dd7a
SW
448}
449
450/* Stop RDC MAC and Free the allocated resource */
451static void r6040_down(struct net_device *dev)
452{
453 struct r6040_private *lp = netdev_priv(dev);
454 void __iomem *ioaddr = lp->base;
7a47dd7a
SW
455 int limit = 2048;
456 u16 *adrp;
457 u16 cmd;
458
459 /* Stop MAC */
460 iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */
461 iowrite16(MAC_RST, ioaddr + MCR1); /* Reset RDC MAC */
462 while (limit--) {
463 cmd = ioread16(ioaddr + MCR1);
464 if (cmd & 0x1)
465 break;
466 }
467
468 /* Restore MAC Address to MIDx */
469 adrp = (u16 *) dev->dev_addr;
470 iowrite16(adrp[0], ioaddr + MID_0L);
471 iowrite16(adrp[1], ioaddr + MID_0M);
472 iowrite16(adrp[2], ioaddr + MID_0H);
7a47dd7a
SW
473}
474
5ac5d616 475static int r6040_close(struct net_device *dev)
7a47dd7a
SW
476{
477 struct r6040_private *lp = netdev_priv(dev);
58854c6b 478 struct pci_dev *pdev = lp->pdev;
7a47dd7a 479
7a47dd7a 480 spin_lock_irq(&lp->lock);
129cf9a7 481 napi_disable(&lp->napi);
7a47dd7a
SW
482 netif_stop_queue(dev);
483 r6040_down(dev);
58854c6b
FF
484
485 free_irq(dev->irq, dev);
486
487 /* Free RX buffer */
488 r6040_free_rxbufs(dev);
489
490 /* Free TX buffer */
491 r6040_free_txbufs(dev);
492
7a47dd7a
SW
493 spin_unlock_irq(&lp->lock);
494
58854c6b
FF
495 /* Free Descriptor memory */
496 if (lp->rx_ring) {
2154c704
FF
497 pci_free_consistent(pdev,
498 RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma);
5b5103ec 499 lp->rx_ring = NULL;
58854c6b
FF
500 }
501
502 if (lp->tx_ring) {
2154c704
FF
503 pci_free_consistent(pdev,
504 TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma);
5b5103ec 505 lp->tx_ring = NULL;
58854c6b
FF
506 }
507
7a47dd7a
SW
508 return 0;
509}
510
7a47dd7a
SW
511static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
512{
513 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a 514
3831861b 515 if (!lp->phydev)
7a47dd7a 516 return -EINVAL;
3831861b 517
4cfa580e 518 return phy_mii_ioctl(lp->phydev, rq, cmd);
7a47dd7a
SW
519}
520
521static int r6040_rx(struct net_device *dev, int limit)
522{
523 struct r6040_private *priv = netdev_priv(dev);
9ca28dc4
FF
524 struct r6040_descriptor *descptr = priv->rx_remove_ptr;
525 struct sk_buff *skb_ptr, *new_skb;
526 int count = 0;
7a47dd7a
SW
527 u16 err;
528
9ca28dc4 529 /* Limit not reached and the descriptor belongs to the CPU */
32f565df 530 while (count < limit && !(descptr->status & DSC_OWNER_MAC)) {
9ca28dc4
FF
531 /* Read the descriptor status */
532 err = descptr->status;
533 /* Global error status set */
32f565df 534 if (err & DSC_RX_ERR) {
9ca28dc4 535 /* RX dribble */
32f565df 536 if (err & DSC_RX_ERR_DRI)
9ca28dc4
FF
537 dev->stats.rx_frame_errors++;
538 /* Buffer lenght exceeded */
32f565df 539 if (err & DSC_RX_ERR_BUF)
9ca28dc4
FF
540 dev->stats.rx_length_errors++;
541 /* Packet too long */
32f565df 542 if (err & DSC_RX_ERR_LONG)
9ca28dc4
FF
543 dev->stats.rx_length_errors++;
544 /* Packet < 64 bytes */
32f565df 545 if (err & DSC_RX_ERR_RUNT)
9ca28dc4
FF
546 dev->stats.rx_length_errors++;
547 /* CRC error */
32f565df 548 if (err & DSC_RX_ERR_CRC) {
9ca28dc4
FF
549 spin_lock(&priv->lock);
550 dev->stats.rx_crc_errors++;
551 spin_unlock(&priv->lock);
7a47dd7a 552 }
9ca28dc4
FF
553 goto next_descr;
554 }
2154c704 555
9ca28dc4
FF
556 /* Packet successfully received */
557 new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
558 if (!new_skb) {
559 dev->stats.rx_dropped++;
560 goto next_descr;
7a47dd7a 561 }
9ca28dc4
FF
562 skb_ptr = descptr->skb_ptr;
563 skb_ptr->dev = priv->dev;
2154c704 564
9ca28dc4
FF
565 /* Do not count the CRC */
566 skb_put(skb_ptr, descptr->len - 4);
567 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
568 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
569 skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev);
2154c704 570
9ca28dc4
FF
571 /* Send to upper layer */
572 netif_receive_skb(skb_ptr);
9ca28dc4
FF
573 dev->stats.rx_packets++;
574 dev->stats.rx_bytes += descptr->len - 4;
575
576 /* put new skb into descriptor */
577 descptr->skb_ptr = new_skb;
578 descptr->buf = cpu_to_le32(pci_map_single(priv->pdev,
579 descptr->skb_ptr->data,
580 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
581
582next_descr:
583 /* put the descriptor back to the MAC */
32f565df 584 descptr->status = DSC_OWNER_MAC;
9ca28dc4
FF
585 descptr = descptr->vndescp;
586 count++;
7a47dd7a 587 }
9ca28dc4 588 priv->rx_remove_ptr = descptr;
7a47dd7a
SW
589
590 return count;
591}
592
593static void r6040_tx(struct net_device *dev)
594{
595 struct r6040_private *priv = netdev_priv(dev);
596 struct r6040_descriptor *descptr;
597 void __iomem *ioaddr = priv->base;
598 struct sk_buff *skb_ptr;
599 u16 err;
600
601 spin_lock(&priv->lock);
602 descptr = priv->tx_remove_ptr;
603 while (priv->tx_free_desc < TX_DCNT) {
604 /* Check for errors */
605 err = ioread16(ioaddr + MLSR);
606
d248fd77
FF
607 if (err & 0x0200)
608 dev->stats.rx_fifo_errors++;
609 if (err & (0x2000 | 0x4000))
610 dev->stats.tx_carrier_errors++;
7a47dd7a 611
32f565df 612 if (descptr->status & DSC_OWNER_MAC)
ec6d2d45 613 break; /* Not complete */
7a47dd7a 614 skb_ptr = descptr->skb_ptr;
ed773b4a 615 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
7a47dd7a
SW
616 skb_ptr->len, PCI_DMA_TODEVICE);
617 /* Free buffer */
618 dev_kfree_skb_irq(skb_ptr);
619 descptr->skb_ptr = NULL;
620 /* To next descriptor */
621 descptr = descptr->vndescp;
622 priv->tx_free_desc++;
623 }
624 priv->tx_remove_ptr = descptr;
625
626 if (priv->tx_free_desc)
627 netif_wake_queue(dev);
628 spin_unlock(&priv->lock);
629}
630
631static int r6040_poll(struct napi_struct *napi, int budget)
632{
633 struct r6040_private *priv =
634 container_of(napi, struct r6040_private, napi);
635 struct net_device *dev = priv->dev;
636 void __iomem *ioaddr = priv->base;
637 int work_done;
638
639 work_done = r6040_rx(dev, budget);
640
641 if (work_done < budget) {
288379f0 642 napi_complete(napi);
7a47dd7a 643 /* Enable RX interrupt */
e24ddf3a 644 iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER);
7a47dd7a
SW
645 }
646 return work_done;
647}
648
649/* The RDC interrupt handler. */
650static irqreturn_t r6040_interrupt(int irq, void *dev_id)
651{
652 struct net_device *dev = dev_id;
653 struct r6040_private *lp = netdev_priv(dev);
654 void __iomem *ioaddr = lp->base;
3e7c469f 655 u16 misr, status;
7a47dd7a 656
3e7c469f
JC
657 /* Save MIER */
658 misr = ioread16(ioaddr + MIER);
7a47dd7a
SW
659 /* Mask off RDC MAC interrupt */
660 iowrite16(MSK_INT, ioaddr + MIER);
661 /* Read MISR status and clear */
662 status = ioread16(ioaddr + MISR);
663
35976d4d
FF
664 if (status == 0x0000 || status == 0xffff) {
665 /* Restore RDC MAC interrupt */
666 iowrite16(misr, ioaddr + MIER);
7a47dd7a 667 return IRQ_NONE;
35976d4d 668 }
7a47dd7a
SW
669
670 /* RX interrupt request */
e24ddf3a
FF
671 if (status & RX_INTS) {
672 if (status & RX_NO_DESC) {
673 /* RX descriptor unavailable */
674 dev->stats.rx_dropped++;
675 dev->stats.rx_missed_errors++;
676 }
677 if (status & RX_FIFO_FULL)
678 dev->stats.rx_fifo_errors++;
679
3d254348 680 /* Mask off RX interrupt */
3e7c469f 681 misr &= ~RX_INTS;
288379f0 682 napi_schedule(&lp->napi);
7a47dd7a
SW
683 }
684
685 /* TX interrupt request */
e24ddf3a 686 if (status & TX_INTS)
7a47dd7a
SW
687 r6040_tx(dev);
688
3e7c469f
JC
689 /* Restore RDC MAC interrupt */
690 iowrite16(misr, ioaddr + MIER);
691
ec6d2d45 692 return IRQ_HANDLED;
7a47dd7a
SW
693}
694
695#ifdef CONFIG_NET_POLL_CONTROLLER
696static void r6040_poll_controller(struct net_device *dev)
697{
698 disable_irq(dev->irq);
5ac5d616 699 r6040_interrupt(dev->irq, dev);
7a47dd7a
SW
700 enable_irq(dev->irq);
701}
702#endif
703
7a47dd7a 704/* Init RDC MAC */
3d463419 705static int r6040_up(struct net_device *dev)
7a47dd7a
SW
706{
707 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a 708 void __iomem *ioaddr = lp->base;
3d463419 709 int ret;
7a47dd7a 710
b4f1255d 711 /* Initialise and alloc RX/TX buffers */
3d463419
FF
712 r6040_init_txbufs(dev);
713 ret = r6040_alloc_rxbufs(dev);
714 if (ret)
715 return ret;
7a47dd7a 716
7a47dd7a 717 /* improve performance (by RDC guys) */
2154c704
FF
718 r6040_phy_write(ioaddr, 30, 17,
719 (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
720 r6040_phy_write(ioaddr, 30, 17,
721 ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
c6e69bb9
FF
722 r6040_phy_write(ioaddr, 0, 19, 0x0000);
723 r6040_phy_write(ioaddr, 0, 30, 0x01F0);
7a47dd7a 724
fec3a23b
FF
725 /* Initialize all MAC registers */
726 r6040_init_mac_regs(dev);
3d463419
FF
727
728 return 0;
7a47dd7a
SW
729}
730
7a47dd7a
SW
731
732/* Read/set MAC address routines */
733static void r6040_mac_address(struct net_device *dev)
734{
735 struct r6040_private *lp = netdev_priv(dev);
736 void __iomem *ioaddr = lp->base;
737 u16 *adrp;
738
739 /* MAC operation register */
740 iowrite16(0x01, ioaddr + MCR1); /* Reset MAC */
741 iowrite16(2, ioaddr + MAC_SM); /* Reset internal state machine */
742 iowrite16(0, ioaddr + MAC_SM);
c1d69937 743 mdelay(5);
7a47dd7a
SW
744
745 /* Restore MAC Address */
746 adrp = (u16 *) dev->dev_addr;
747 iowrite16(adrp[0], ioaddr + MID_0L);
748 iowrite16(adrp[1], ioaddr + MID_0M);
749 iowrite16(adrp[2], ioaddr + MID_0H);
42099d7a
OS
750
751 /* Store MAC Address in perm_addr */
752 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
7a47dd7a
SW
753}
754
5ac5d616 755static int r6040_open(struct net_device *dev)
7a47dd7a 756{
5ac5d616 757 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a
SW
758 int ret;
759
760 /* Request IRQ and Register interrupt handler */
91dcbf36 761 ret = request_irq(dev->irq, r6040_interrupt,
7a47dd7a
SW
762 IRQF_SHARED, dev->name, dev);
763 if (ret)
ced1de4c 764 goto out;
7a47dd7a
SW
765
766 /* Set MAC address */
767 r6040_mac_address(dev);
768
769 /* Allocate Descriptor memory */
6c323103
FR
770 lp->rx_ring =
771 pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma);
ced1de4c
DK
772 if (!lp->rx_ring) {
773 ret = -ENOMEM;
774 goto err_free_irq;
775 }
7a47dd7a 776
6c323103
FR
777 lp->tx_ring =
778 pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma);
779 if (!lp->tx_ring) {
ced1de4c
DK
780 ret = -ENOMEM;
781 goto err_free_rx_ring;
6c323103
FR
782 }
783
3d463419 784 ret = r6040_up(dev);
ced1de4c
DK
785 if (ret)
786 goto err_free_tx_ring;
7a47dd7a
SW
787
788 napi_enable(&lp->napi);
789 netif_start_queue(dev);
790
7a47dd7a 791 return 0;
ced1de4c
DK
792
793err_free_tx_ring:
794 pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring,
795 lp->tx_ring_dma);
796err_free_rx_ring:
797 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
798 lp->rx_ring_dma);
799err_free_irq:
800 free_irq(dev->irq, dev);
801out:
802 return ret;
7a47dd7a
SW
803}
804
61357325
SH
805static netdev_tx_t r6040_start_xmit(struct sk_buff *skb,
806 struct net_device *dev)
7a47dd7a
SW
807{
808 struct r6040_private *lp = netdev_priv(dev);
809 struct r6040_descriptor *descptr;
810 void __iomem *ioaddr = lp->base;
811 unsigned long flags;
7a47dd7a
SW
812
813 /* Critical Section */
814 spin_lock_irqsave(&lp->lock, flags);
815
816 /* TX resource check */
817 if (!lp->tx_free_desc) {
818 spin_unlock_irqrestore(&lp->lock, flags);
092427be 819 netif_stop_queue(dev);
7d53b809 820 netdev_err(dev, ": no tx descriptor\n");
61357325 821 return NETDEV_TX_BUSY;
7a47dd7a
SW
822 }
823
824 /* Statistic Counter */
825 dev->stats.tx_packets++;
826 dev->stats.tx_bytes += skb->len;
827 /* Set TX descriptor & Transmit it */
828 lp->tx_free_desc--;
829 descptr = lp->tx_insert_ptr;
830 if (skb->len < MISR)
831 descptr->len = MISR;
832 else
833 descptr->len = skb->len;
834
835 descptr->skb_ptr = skb;
836 descptr->buf = cpu_to_le32(pci_map_single(lp->pdev,
837 skb->data, skb->len, PCI_DMA_TODEVICE));
32f565df 838 descptr->status = DSC_OWNER_MAC;
7a47dd7a
SW
839 /* Trigger the MAC to check the TX descriptor */
840 iowrite16(0x01, ioaddr + MTPR);
841 lp->tx_insert_ptr = descptr->vndescp;
842
843 /* If no tx resource, stop */
844 if (!lp->tx_free_desc)
845 netif_stop_queue(dev);
846
7a47dd7a 847 spin_unlock_irqrestore(&lp->lock, flags);
61357325
SH
848
849 return NETDEV_TX_OK;
7a47dd7a
SW
850}
851
5ac5d616 852static void r6040_multicast_list(struct net_device *dev)
7a47dd7a
SW
853{
854 struct r6040_private *lp = netdev_priv(dev);
855 void __iomem *ioaddr = lp->base;
7a47dd7a 856 unsigned long flags;
22bedad3 857 struct netdev_hw_addr *ha;
7a47dd7a 858 int i;
c60c9c71
SL
859 u16 *adrp;
860 u16 hash_table[4] = { 0 };
861
862 spin_lock_irqsave(&lp->lock, flags);
7a47dd7a 863
c60c9c71 864 /* Keep our MAC Address */
7a47dd7a
SW
865 adrp = (u16 *)dev->dev_addr;
866 iowrite16(adrp[0], ioaddr + MID_0L);
867 iowrite16(adrp[1], ioaddr + MID_0M);
868 iowrite16(adrp[2], ioaddr + MID_0H);
869
7a47dd7a 870 /* Clear AMCP & PROM bits */
c60c9c71 871 lp->mcr0 = ioread16(ioaddr + MCR0) & ~(MCR0_PROMISC | MCR0_HASH_EN);
7a47dd7a 872
c60c9c71
SL
873 /* Promiscuous mode */
874 if (dev->flags & IFF_PROMISC)
875 lp->mcr0 |= MCR0_PROMISC;
7a47dd7a 876
c60c9c71
SL
877 /* Enable multicast hash table function to
878 * receive all multicast packets. */
879 else if (dev->flags & IFF_ALLMULTI) {
880 lp->mcr0 |= MCR0_HASH_EN;
7a47dd7a 881
c60c9c71
SL
882 for (i = 0; i < MCAST_MAX ; i++) {
883 iowrite16(0, ioaddr + MID_1L + 8 * i);
884 iowrite16(0, ioaddr + MID_1M + 8 * i);
885 iowrite16(0, ioaddr + MID_1H + 8 * i);
886 }
7a47dd7a 887
c60c9c71
SL
888 for (i = 0; i < 4; i++)
889 hash_table[i] = 0xffff;
890 }
891 /* Use internal multicast address registers if the number of
892 * multicast addresses is not greater than MCAST_MAX. */
893 else if (netdev_mc_count(dev) <= MCAST_MAX) {
894 i = 0;
22bedad3 895 netdev_for_each_mc_addr(ha, dev) {
c60c9c71
SL
896 u16 *adrp = (u16 *) ha->addr;
897 iowrite16(adrp[0], ioaddr + MID_1L + 8 * i);
898 iowrite16(adrp[1], ioaddr + MID_1M + 8 * i);
899 iowrite16(adrp[2], ioaddr + MID_1H + 8 * i);
900 i++;
901 }
902 while (i < MCAST_MAX) {
903 iowrite16(0, ioaddr + MID_1L + 8 * i);
904 iowrite16(0, ioaddr + MID_1M + 8 * i);
905 iowrite16(0, ioaddr + MID_1H + 8 * i);
906 i++;
907 }
908 }
909 /* Otherwise, Enable multicast hash table function. */
910 else {
911 u32 crc;
7a47dd7a 912
c60c9c71
SL
913 lp->mcr0 |= MCR0_HASH_EN;
914
915 for (i = 0; i < MCAST_MAX ; i++) {
916 iowrite16(0, ioaddr + MID_1L + 8 * i);
917 iowrite16(0, ioaddr + MID_1M + 8 * i);
918 iowrite16(0, ioaddr + MID_1H + 8 * i);
919 }
7a47dd7a 920
c60c9c71
SL
921 /* Build multicast hash table */
922 netdev_for_each_mc_addr(ha, dev) {
923 u8 *addrs = ha->addr;
924
925 crc = ether_crc(ETH_ALEN, addrs);
7a47dd7a 926 crc >>= 26;
c60c9c71 927 hash_table[crc >> 4] |= 1 << (crc & 0xf);
7a47dd7a 928 }
c60c9c71
SL
929 }
930
931 iowrite16(lp->mcr0, ioaddr + MCR0);
932
933 /* Fill the MAC hash tables with their values */
934 if (lp->mcr0 && MCR0_HASH_EN) {
7a47dd7a
SW
935 iowrite16(hash_table[0], ioaddr + MAR0);
936 iowrite16(hash_table[1], ioaddr + MAR1);
937 iowrite16(hash_table[2], ioaddr + MAR2);
938 iowrite16(hash_table[3], ioaddr + MAR3);
939 }
c60c9c71
SL
940
941 spin_unlock_irqrestore(&lp->lock, flags);
7a47dd7a
SW
942}
943
944static void netdev_get_drvinfo(struct net_device *dev,
945 struct ethtool_drvinfo *info)
946{
947 struct r6040_private *rp = netdev_priv(dev);
948
949 strcpy(info->driver, DRV_NAME);
950 strcpy(info->version, DRV_VERSION);
951 strcpy(info->bus_info, pci_name(rp->pdev));
952}
953
954static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
955{
956 struct r6040_private *rp = netdev_priv(dev);
7a47dd7a 957
3831861b 958 return phy_ethtool_gset(rp->phydev, cmd);
7a47dd7a
SW
959}
960
961static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
7a47dd7a
SW
962{
963 struct r6040_private *rp = netdev_priv(dev);
964
3831861b 965 return phy_ethtool_sset(rp->phydev, cmd);
7a47dd7a
SW
966}
967
a7bd89cb 968static const struct ethtool_ops netdev_ethtool_ops = {
7a47dd7a
SW
969 .get_drvinfo = netdev_get_drvinfo,
970 .get_settings = netdev_get_settings,
971 .set_settings = netdev_set_settings,
3831861b 972 .get_link = ethtool_op_get_link,
7a47dd7a
SW
973};
974
a7bd89cb
SH
975static const struct net_device_ops r6040_netdev_ops = {
976 .ndo_open = r6040_open,
977 .ndo_stop = r6040_close,
978 .ndo_start_xmit = r6040_start_xmit,
979 .ndo_get_stats = r6040_get_stats,
980 .ndo_set_multicast_list = r6040_multicast_list,
981 .ndo_change_mtu = eth_change_mtu,
982 .ndo_validate_addr = eth_validate_addr,
2154c704 983 .ndo_set_mac_address = eth_mac_addr,
a7bd89cb
SH
984 .ndo_do_ioctl = r6040_ioctl,
985 .ndo_tx_timeout = r6040_tx_timeout,
986#ifdef CONFIG_NET_POLL_CONTROLLER
987 .ndo_poll_controller = r6040_poll_controller,
988#endif
989};
990
3831861b
FF
991static void r6040_adjust_link(struct net_device *dev)
992{
993 struct r6040_private *lp = netdev_priv(dev);
994 struct phy_device *phydev = lp->phydev;
995 int status_changed = 0;
996 void __iomem *ioaddr = lp->base;
997
998 BUG_ON(!phydev);
999
1000 if (lp->old_link != phydev->link) {
1001 status_changed = 1;
1002 lp->old_link = phydev->link;
1003 }
1004
1005 /* reflect duplex change */
1006 if (phydev->link && (lp->old_duplex != phydev->duplex)) {
1007 lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? 0x8000 : 0);
1008 iowrite16(lp->mcr0, ioaddr);
1009
1010 status_changed = 1;
1011 lp->old_duplex = phydev->duplex;
1012 }
1013
1014 if (status_changed) {
1015 pr_info("%s: link %s", dev->name, phydev->link ?
1016 "UP" : "DOWN");
1017 if (phydev->link)
1018 pr_cont(" - %d/%s", phydev->speed,
1019 DUPLEX_FULL == phydev->duplex ? "full" : "half");
1020 pr_cont("\n");
1021 }
1022}
1023
1024static int r6040_mii_probe(struct net_device *dev)
1025{
1026 struct r6040_private *lp = netdev_priv(dev);
1027 struct phy_device *phydev = NULL;
1028
1029 phydev = phy_find_first(lp->mii_bus);
1030 if (!phydev) {
1031 dev_err(&lp->pdev->dev, "no PHY found\n");
1032 return -ENODEV;
1033 }
1034
1035 phydev = phy_connect(dev, dev_name(&phydev->dev), &r6040_adjust_link,
1036 0, PHY_INTERFACE_MODE_MII);
1037
1038 if (IS_ERR(phydev)) {
1039 dev_err(&lp->pdev->dev, "could not attach to PHY\n");
1040 return PTR_ERR(phydev);
1041 }
1042
1043 /* mask with MAC supported features */
1044 phydev->supported &= (SUPPORTED_10baseT_Half
1045 | SUPPORTED_10baseT_Full
1046 | SUPPORTED_100baseT_Half
1047 | SUPPORTED_100baseT_Full
1048 | SUPPORTED_Autoneg
1049 | SUPPORTED_MII
1050 | SUPPORTED_TP);
1051
1052 phydev->advertising = phydev->supported;
1053 lp->phydev = phydev;
1054 lp->old_link = 0;
1055 lp->old_duplex = -1;
1056
1057 dev_info(&lp->pdev->dev, "attached PHY driver [%s] "
1058 "(mii_bus:phy_addr=%s)\n",
1059 phydev->drv->name, dev_name(&phydev->dev));
1060
1061 return 0;
1062}
1063
7a47dd7a
SW
1064static int __devinit r6040_init_one(struct pci_dev *pdev,
1065 const struct pci_device_id *ent)
1066{
1067 struct net_device *dev;
1068 struct r6040_private *lp;
1069 void __iomem *ioaddr;
1070 int err, io_size = R6040_IO_SIZE;
1071 static int card_idx = -1;
1072 int bar = 0;
7a47dd7a 1073 u16 *adrp;
3831861b 1074 int i;
7a47dd7a 1075
2154c704 1076 pr_info("%s\n", version);
7a47dd7a
SW
1077
1078 err = pci_enable_device(pdev);
1079 if (err)
b0e45390 1080 goto err_out;
7a47dd7a
SW
1081
1082 /* this should always be supported */
284901a9 1083 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
b0e45390 1084 if (err) {
7d53b809 1085 dev_err(&pdev->dev, "32-bit PCI DMA addresses"
7a47dd7a 1086 "not supported by the card\n");
b0e45390 1087 goto err_out;
7a47dd7a 1088 }
284901a9 1089 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
b0e45390 1090 if (err) {
7d53b809 1091 dev_err(&pdev->dev, "32-bit PCI DMA addresses"
092427be 1092 "not supported by the card\n");
b0e45390 1093 goto err_out;
092427be 1094 }
7a47dd7a
SW
1095
1096 /* IO Size check */
6f5bec19 1097 if (pci_resource_len(pdev, bar) < io_size) {
7d53b809 1098 dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
b0e45390
FF
1099 err = -EIO;
1100 goto err_out;
7a47dd7a
SW
1101 }
1102
7a47dd7a
SW
1103 pci_set_master(pdev);
1104
1105 dev = alloc_etherdev(sizeof(struct r6040_private));
1106 if (!dev) {
7d53b809 1107 dev_err(&pdev->dev, "Failed to allocate etherdev\n");
b0e45390
FF
1108 err = -ENOMEM;
1109 goto err_out;
7a47dd7a
SW
1110 }
1111 SET_NETDEV_DEV(dev, &pdev->dev);
1112 lp = netdev_priv(dev);
7a47dd7a 1113
b0e45390
FF
1114 err = pci_request_regions(pdev, DRV_NAME);
1115
1116 if (err) {
7d53b809 1117 dev_err(&pdev->dev, "Failed to request PCI regions\n");
b0e45390 1118 goto err_out_free_dev;
7a47dd7a
SW
1119 }
1120
1121 ioaddr = pci_iomap(pdev, bar, io_size);
1122 if (!ioaddr) {
7d53b809 1123 dev_err(&pdev->dev, "ioremap failed for device\n");
b0e45390
FF
1124 err = -EIO;
1125 goto err_out_free_res;
7a47dd7a 1126 }
84314bf9
FF
1127 /* If PHY status change register is still set to zero it means the
1128 * bootloader didn't initialize it */
1129 if (ioread16(ioaddr + PHY_CC) == 0)
1130 iowrite16(0x9f07, ioaddr + PHY_CC);
7a47dd7a
SW
1131
1132 /* Init system & device */
7a47dd7a
SW
1133 lp->base = ioaddr;
1134 dev->irq = pdev->irq;
1135
1136 spin_lock_init(&lp->lock);
1137 pci_set_drvdata(pdev, dev);
1138
1139 /* Set MAC address */
1140 card_idx++;
1141
1142 adrp = (u16 *)dev->dev_addr;
1143 adrp[0] = ioread16(ioaddr + MID_0L);
1144 adrp[1] = ioread16(ioaddr + MID_0M);
1145 adrp[2] = ioread16(ioaddr + MID_0H);
1146
1d2b1a76
FF
1147 /* Some bootloader/BIOSes do not initialize
1148 * MAC address, warn about that */
9f113618 1149 if (!(adrp[0] || adrp[1] || adrp[2])) {
2154c704
FF
1150 netdev_warn(dev, "MAC address not initialized, "
1151 "generating random\n");
9f113618
FF
1152 random_ether_addr(dev->dev_addr);
1153 }
1d2b1a76 1154
7a47dd7a
SW
1155 /* Link new device into r6040_root_dev */
1156 lp->pdev = pdev;
129cf9a7 1157 lp->dev = dev;
7a47dd7a
SW
1158
1159 /* Init RDC private data */
1160 lp->mcr0 = 0x1002;
1161 lp->phy_addr = phy_table[card_idx];
7a47dd7a
SW
1162
1163 /* The RDC-specific entries in the device structure. */
a7bd89cb 1164 dev->netdev_ops = &r6040_netdev_ops;
7a47dd7a 1165 dev->ethtool_ops = &netdev_ethtool_ops;
7a47dd7a 1166 dev->watchdog_timeo = TX_TIMEOUT;
a7bd89cb 1167
7a47dd7a 1168 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
3831861b
FF
1169
1170 lp->mii_bus = mdiobus_alloc();
1171 if (!lp->mii_bus) {
1172 dev_err(&pdev->dev, "mdiobus_alloc() failed\n");
9c86c0f4 1173 err = -ENOMEM;
e03f614a
MK
1174 goto err_out_unmap;
1175 }
1176
3831861b
FF
1177 lp->mii_bus->priv = dev;
1178 lp->mii_bus->read = r6040_mdiobus_read;
1179 lp->mii_bus->write = r6040_mdiobus_write;
1180 lp->mii_bus->reset = r6040_mdiobus_reset;
1181 lp->mii_bus->name = "r6040_eth_mii";
1182 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x", card_idx);
1183 lp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1184 if (!lp->mii_bus->irq) {
1185 dev_err(&pdev->dev, "mii_bus irq allocation failed\n");
9c86c0f4 1186 err = -ENOMEM;
3831861b
FF
1187 goto err_out_mdio;
1188 }
1189
1190 for (i = 0; i < PHY_MAX_ADDR; i++)
1191 lp->mii_bus->irq[i] = PHY_POLL;
1192
1193 err = mdiobus_register(lp->mii_bus);
1194 if (err) {
1195 dev_err(&pdev->dev, "failed to register MII bus\n");
1196 goto err_out_mdio_irq;
1197 }
1198
1199 err = r6040_mii_probe(dev);
1200 if (err) {
1201 dev_err(&pdev->dev, "failed to probe MII bus\n");
1202 goto err_out_mdio_unregister;
1203 }
1204
7a47dd7a
SW
1205 /* Register net device. After this dev->name assign */
1206 err = register_netdev(dev);
1207 if (err) {
7d53b809 1208 dev_err(&pdev->dev, "Failed to register net device\n");
3831861b 1209 goto err_out_mdio_unregister;
7a47dd7a
SW
1210 }
1211 return 0;
1212
3831861b
FF
1213err_out_mdio_unregister:
1214 mdiobus_unregister(lp->mii_bus);
1215err_out_mdio_irq:
1216 kfree(lp->mii_bus->irq);
1217err_out_mdio:
1218 mdiobus_free(lp->mii_bus);
b0e45390
FF
1219err_out_unmap:
1220 pci_iounmap(pdev, ioaddr);
1221err_out_free_res:
7a47dd7a 1222 pci_release_regions(pdev);
b0e45390 1223err_out_free_dev:
7a47dd7a 1224 free_netdev(dev);
b0e45390 1225err_out:
7a47dd7a
SW
1226 return err;
1227}
1228
1229static void __devexit r6040_remove_one(struct pci_dev *pdev)
1230{
1231 struct net_device *dev = pci_get_drvdata(pdev);
3831861b 1232 struct r6040_private *lp = netdev_priv(dev);
7a47dd7a
SW
1233
1234 unregister_netdev(dev);
3831861b
FF
1235 mdiobus_unregister(lp->mii_bus);
1236 kfree(lp->mii_bus->irq);
1237 mdiobus_free(lp->mii_bus);
7a47dd7a
SW
1238 pci_release_regions(pdev);
1239 free_netdev(dev);
1240 pci_disable_device(pdev);
1241 pci_set_drvdata(pdev, NULL);
1242}
1243
1244
a3aa1884 1245static DEFINE_PCI_DEVICE_TABLE(r6040_pci_tbl) = {
5ac5d616
FR
1246 { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
1247 { 0 }
7a47dd7a
SW
1248};
1249MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1250
1251static struct pci_driver r6040_driver = {
5ac5d616 1252 .name = DRV_NAME,
7a47dd7a
SW
1253 .id_table = r6040_pci_tbl,
1254 .probe = r6040_init_one,
1255 .remove = __devexit_p(r6040_remove_one),
1256};
1257
1258
1259static int __init r6040_init(void)
1260{
1261 return pci_register_driver(&r6040_driver);
1262}
1263
1264
1265static void __exit r6040_cleanup(void)
1266{
1267 pci_unregister_driver(&r6040_driver);
1268}
1269
1270module_init(r6040_init);
1271module_exit(r6040_cleanup);
This page took 0.512126 seconds and 5 git commands to generate.