blob: 27e6f6d43cac9be4ef52e1c011e8bfa5752404c9 [file] [log] [blame]
Sten Wang7a47dd72007-11-12 21:31:11 -08001/*
2 * RDC R6040 Fast Ethernet MAC support
3 *
4 * Copyright (C) 2004 Sten Wang <sten.wang@rdc.com.tw>
5 * Copyright (C) 2007
Francois Romieu5ac5d612007-11-28 23:02:33 +01006 * Daniel Gimpelevich <daniel@gimpelevich.san-francisco.ca.us>
Sten Wang7a47dd72007-11-12 21:31:11 -08007 * 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>
Sten Wang7a47dd72007-11-12 21:31:11 -080027#include <linux/moduleparam.h>
28#include <linux/string.h>
29#include <linux/timer.h>
30#include <linux/errno.h>
31#include <linux/ioport.h>
Sten Wang7a47dd72007-11-12 21:31:11 -080032#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>
Jeff Garzik092427b2007-11-23 21:49:27 -050043#include <linux/bitops.h>
44#include <linux/io.h>
45#include <linux/irq.h>
46#include <linux/uaccess.h>
Florian Fainelli38318612010-05-31 09:18:57 +000047#include <linux/phy.h>
Sten Wang7a47dd72007-11-12 21:31:11 -080048
49#include <asm/processor.h>
Sten Wang7a47dd72007-11-12 21:31:11 -080050
51#define DRV_NAME "r6040"
Florian Fainelli92c4bbf2010-05-31 09:19:04 +000052#define DRV_VERSION "0.26"
53#define DRV_RELDATE "30May2010"
Sten Wang7a47dd72007-11-12 21:31:11 -080054
55/* PHY CHIP Address */
56#define PHY1_ADDR 1 /* For MAC1 */
Florian Fainelli2a30ca82009-03-24 23:34:35 +000057#define PHY2_ADDR 3 /* For MAC2 */
Sten Wang7a47dd72007-11-12 21:31:11 -080058#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. */
Francois Romieu5ac5d612007-11-28 23:02:33 +010062#define TX_TIMEOUT (6000 * HZ / 1000)
Sten Wang7a47dd72007-11-12 21:31:11 -080063
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 */
Florian Fainelli3d254342008-07-13 14:28:27 +020093#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 */
Sten Wang7a47dd72007-11-12 21:31:11 -0800101#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
Francois Romieu6c323102007-11-28 22:31:00 +0100135#define RX_DESC_SIZE (RX_DCNT * sizeof(struct r6040_descriptor))
136#define TX_DESC_SIZE (TX_DCNT * sizeof(struct r6040_descriptor))
Sten Wang7a47dd72007-11-12 21:31:11 -0800137#define MBCR_DEFAULT 0x012A /* MAC Bus Control Register */
Florian Fainelli3bcf8222010-04-07 16:50:58 -0700138#define MCAST_MAX 3 /* Max number multicast addresses to filter */
Sten Wang7a47dd72007-11-12 21:31:11 -0800139
Florian Fainelli32f565d2008-07-13 14:34:15 +0200140/* 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
Sten Wang7a47dd72007-11-12 21:31:11 -0800155/* 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");
Florian Fainellibc4de262009-04-08 15:50:43 -0700163MODULE_VERSION(DRV_VERSION " " DRV_RELDATE);
Sten Wang7a47dd72007-11-12 21:31:11 -0800164
Florian Fainelli3d254342008-07-13 14:28:27 +0200165/* RX and TX interrupts that we handle */
Florian Fainellie24ddf32008-07-13 14:35:32 +0200166#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)
Sten Wang7a47dd72007-11-12 21:31:11 -0800169
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 */
Sten Wang7a47dd72007-11-12 21:31:11 -0800183 struct pci_dev *pdev;
184 struct r6040_descriptor *rx_insert_ptr;
185 struct r6040_descriptor *rx_remove_ptr;
186 struct r6040_descriptor *tx_insert_ptr;
187 struct r6040_descriptor *tx_remove_ptr;
Francois Romieu6c323102007-11-28 22:31:00 +0100188 struct r6040_descriptor *rx_ring;
189 struct r6040_descriptor *tx_ring;
190 dma_addr_t rx_ring_dma;
191 dma_addr_t tx_ring_dma;
Florian Fainelli38318612010-05-31 09:18:57 +0000192 u16 tx_free_desc, phy_addr;
Sten Wang7a47dd72007-11-12 21:31:11 -0800193 u16 mcr0, mcr1;
Sten Wang7a47dd72007-11-12 21:31:11 -0800194 struct net_device *dev;
Florian Fainelli38318612010-05-31 09:18:57 +0000195 struct mii_bus *mii_bus;
Sten Wang7a47dd72007-11-12 21:31:11 -0800196 struct napi_struct napi;
Sten Wang7a47dd72007-11-12 21:31:11 -0800197 void __iomem *base;
Florian Fainelli38318612010-05-31 09:18:57 +0000198 struct phy_device *phydev;
199 int old_link;
200 int old_duplex;
Sten Wang7a47dd72007-11-12 21:31:11 -0800201};
202
Florian Fainelli2154c7042010-08-08 10:08:44 +0000203static char version[] __devinitdata = DRV_NAME
Sten Wang7a47dd72007-11-12 21:31:11 -0800204 ": RDC R6040 NAPI net driver,"
Florian Fainelli9a48ce82009-01-08 11:00:52 -0800205 "version "DRV_VERSION " (" DRV_RELDATE ")";
Sten Wang7a47dd72007-11-12 21:31:11 -0800206
Jeff Garzik092427b2007-11-23 21:49:27 -0500207static int phy_table[] = { PHY1_ADDR, PHY2_ADDR };
Sten Wang7a47dd72007-11-12 21:31:11 -0800208
209/* Read a word data from PHY Chip */
Florian Fainellic6e69bb2008-07-13 13:39:32 +0200210static int r6040_phy_read(void __iomem *ioaddr, int phy_addr, int reg)
Sten Wang7a47dd72007-11-12 21:31:11 -0800211{
212 int limit = 2048;
213 u16 cmd;
214
215 iowrite16(MDIO_READ + reg + (phy_addr << 8), ioaddr + MMDIO);
216 /* Wait for the read bit to be cleared */
217 while (limit--) {
218 cmd = ioread16(ioaddr + MMDIO);
Joe Chou11e5e8f2008-12-22 19:38:17 -0800219 if (!(cmd & MDIO_READ))
Sten Wang7a47dd72007-11-12 21:31:11 -0800220 break;
221 }
222
223 return ioread16(ioaddr + MMRD);
224}
225
226/* Write a word data from PHY Chip */
Florian Fainelli2154c7042010-08-08 10:08:44 +0000227static void r6040_phy_write(void __iomem *ioaddr,
228 int phy_addr, int reg, u16 val)
Sten Wang7a47dd72007-11-12 21:31:11 -0800229{
230 int limit = 2048;
231 u16 cmd;
232
233 iowrite16(val, ioaddr + MMWD);
234 /* Write the command to the MDIO bus */
235 iowrite16(MDIO_WRITE + reg + (phy_addr << 8), ioaddr + MMDIO);
236 /* Wait for the write bit to be cleared */
237 while (limit--) {
238 cmd = ioread16(ioaddr + MMDIO);
Joe Chou11e5e8f2008-12-22 19:38:17 -0800239 if (!(cmd & MDIO_WRITE))
Sten Wang7a47dd72007-11-12 21:31:11 -0800240 break;
241 }
242}
243
Florian Fainelli38318612010-05-31 09:18:57 +0000244static int r6040_mdiobus_read(struct mii_bus *bus, int phy_addr, int reg)
Sten Wang7a47dd72007-11-12 21:31:11 -0800245{
Florian Fainelli38318612010-05-31 09:18:57 +0000246 struct net_device *dev = bus->priv;
Sten Wang7a47dd72007-11-12 21:31:11 -0800247 struct r6040_private *lp = netdev_priv(dev);
248 void __iomem *ioaddr = lp->base;
249
Florian Fainelli38318612010-05-31 09:18:57 +0000250 return r6040_phy_read(ioaddr, phy_addr, reg);
Sten Wang7a47dd72007-11-12 21:31:11 -0800251}
252
Florian Fainelli38318612010-05-31 09:18:57 +0000253static int r6040_mdiobus_write(struct mii_bus *bus, int phy_addr,
254 int reg, u16 value)
Sten Wang7a47dd72007-11-12 21:31:11 -0800255{
Florian Fainelli38318612010-05-31 09:18:57 +0000256 struct net_device *dev = bus->priv;
Sten Wang7a47dd72007-11-12 21:31:11 -0800257 struct r6040_private *lp = netdev_priv(dev);
258 void __iomem *ioaddr = lp->base;
259
Florian Fainelli38318612010-05-31 09:18:57 +0000260 r6040_phy_write(ioaddr, phy_addr, reg, value);
261
262 return 0;
263}
264
265static int r6040_mdiobus_reset(struct mii_bus *bus)
266{
267 return 0;
Sten Wang7a47dd72007-11-12 21:31:11 -0800268}
269
Florian Fainellib4f12552007-12-12 22:55:34 +0100270static void r6040_free_txbufs(struct net_device *dev)
271{
272 struct r6040_private *lp = netdev_priv(dev);
273 int i;
274
275 for (i = 0; i < TX_DCNT; i++) {
276 if (lp->tx_insert_ptr->skb_ptr) {
Al Viroed773b42008-03-16 22:43:06 +0000277 pci_unmap_single(lp->pdev,
278 le32_to_cpu(lp->tx_insert_ptr->buf),
Florian Fainellib4f12552007-12-12 22:55:34 +0100279 MAX_BUF_SIZE, PCI_DMA_TODEVICE);
280 dev_kfree_skb(lp->tx_insert_ptr->skb_ptr);
Florian Fainelli3b060be2008-09-24 21:16:40 +0200281 lp->tx_insert_ptr->skb_ptr = NULL;
Florian Fainellib4f12552007-12-12 22:55:34 +0100282 }
283 lp->tx_insert_ptr = lp->tx_insert_ptr->vndescp;
284 }
285}
286
287static void r6040_free_rxbufs(struct net_device *dev)
288{
289 struct r6040_private *lp = netdev_priv(dev);
290 int i;
291
292 for (i = 0; i < RX_DCNT; i++) {
293 if (lp->rx_insert_ptr->skb_ptr) {
Al Viroed773b42008-03-16 22:43:06 +0000294 pci_unmap_single(lp->pdev,
295 le32_to_cpu(lp->rx_insert_ptr->buf),
Florian Fainellib4f12552007-12-12 22:55:34 +0100296 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
297 dev_kfree_skb(lp->rx_insert_ptr->skb_ptr);
298 lp->rx_insert_ptr->skb_ptr = NULL;
299 }
300 lp->rx_insert_ptr = lp->rx_insert_ptr->vndescp;
301 }
302}
303
Florian Fainellib4f12552007-12-12 22:55:34 +0100304static void r6040_init_ring_desc(struct r6040_descriptor *desc_ring,
305 dma_addr_t desc_dma, int size)
306{
307 struct r6040_descriptor *desc = desc_ring;
308 dma_addr_t mapping = desc_dma;
309
310 while (size-- > 0) {
Julia Lawall3f6602a2008-06-23 23:12:31 +0200311 mapping += sizeof(*desc);
Florian Fainellib4f12552007-12-12 22:55:34 +0100312 desc->ndesc = cpu_to_le32(mapping);
313 desc->vndescp = desc + 1;
314 desc++;
315 }
316 desc--;
317 desc->ndesc = cpu_to_le32(desc_dma);
318 desc->vndescp = desc_ring;
319}
320
Florian Fainelli3d463412008-07-13 14:32:18 +0200321static void r6040_init_txbufs(struct net_device *dev)
Florian Fainellib4f12552007-12-12 22:55:34 +0100322{
323 struct r6040_private *lp = netdev_priv(dev);
Florian Fainellib4f12552007-12-12 22:55:34 +0100324
325 lp->tx_free_desc = TX_DCNT;
326
327 lp->tx_remove_ptr = lp->tx_insert_ptr = lp->tx_ring;
328 r6040_init_ring_desc(lp->tx_ring, lp->tx_ring_dma, TX_DCNT);
Florian Fainellib4f12552007-12-12 22:55:34 +0100329}
330
Florian Fainelli3d463412008-07-13 14:32:18 +0200331static int r6040_alloc_rxbufs(struct net_device *dev)
Florian Fainellib4f12552007-12-12 22:55:34 +0100332{
333 struct r6040_private *lp = netdev_priv(dev);
Florian Fainelli3d463412008-07-13 14:32:18 +0200334 struct r6040_descriptor *desc;
335 struct sk_buff *skb;
336 int rc;
Florian Fainellib4f12552007-12-12 22:55:34 +0100337
338 lp->rx_remove_ptr = lp->rx_insert_ptr = lp->rx_ring;
339 r6040_init_ring_desc(lp->rx_ring, lp->rx_ring_dma, RX_DCNT);
340
Florian Fainelli3d463412008-07-13 14:32:18 +0200341 /* Allocate skbs for the rx descriptors */
342 desc = lp->rx_ring;
343 do {
344 skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
345 if (!skb) {
Florian Fainelli7d53b802010-04-07 21:39:27 +0000346 netdev_err(dev, "failed to alloc skb for rx\n");
Florian Fainelli3d463412008-07-13 14:32:18 +0200347 rc = -ENOMEM;
348 goto err_exit;
349 }
350 desc->skb_ptr = skb;
351 desc->buf = cpu_to_le32(pci_map_single(lp->pdev,
Florian Fainelli2154c7042010-08-08 10:08:44 +0000352 desc->skb_ptr->data,
353 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
Florian Fainelli32f565d2008-07-13 14:34:15 +0200354 desc->status = DSC_OWNER_MAC;
Florian Fainelli3d463412008-07-13 14:32:18 +0200355 desc = desc->vndescp;
356 } while (desc != lp->rx_ring);
357
358 return 0;
359
360err_exit:
361 /* Deallocate all previously allocated skbs */
362 r6040_free_rxbufs(dev);
363 return rc;
Florian Fainellifec3a232008-07-13 14:29:20 +0200364}
Florian Fainellib4f12552007-12-12 22:55:34 +0100365
Florian Fainellifec3a232008-07-13 14:29:20 +0200366static void r6040_init_mac_regs(struct net_device *dev)
367{
368 struct r6040_private *lp = netdev_priv(dev);
369 void __iomem *ioaddr = lp->base;
370 int limit = 2048;
371 u16 cmd;
372
373 /* Mask Off Interrupt */
374 iowrite16(MSK_INT, ioaddr + MIER);
375
376 /* Reset RDC MAC */
377 iowrite16(MAC_RST, ioaddr + MCR1);
378 while (limit--) {
379 cmd = ioread16(ioaddr + MCR1);
380 if (cmd & 0x1)
381 break;
382 }
383 /* Reset internal state machine */
384 iowrite16(2, ioaddr + MAC_SM);
385 iowrite16(0, ioaddr + MAC_SM);
Florian Fainellic1d69932008-09-03 16:50:03 +0200386 mdelay(5);
Florian Fainellifec3a232008-07-13 14:29:20 +0200387
388 /* MAC Bus Control Register */
389 iowrite16(MBCR_DEFAULT, ioaddr + MBCR);
390
391 /* Buffer Size Register */
392 iowrite16(MAX_BUF_SIZE, ioaddr + MR_BSR);
393
394 /* Write TX ring start address */
395 iowrite16(lp->tx_ring_dma, ioaddr + MTD_SA0);
396 iowrite16(lp->tx_ring_dma >> 16, ioaddr + MTD_SA1);
397
398 /* Write RX ring start address */
Florian Fainellib4f12552007-12-12 22:55:34 +0100399 iowrite16(lp->rx_ring_dma, ioaddr + MRD_SA0);
400 iowrite16(lp->rx_ring_dma >> 16, ioaddr + MRD_SA1);
Florian Fainellifec3a232008-07-13 14:29:20 +0200401
402 /* Set interrupt waiting time and packet numbers */
Florian Fainelli31718de2008-07-13 14:35:00 +0200403 iowrite16(0, ioaddr + MT_ICR);
404 iowrite16(0, ioaddr + MR_ICR);
Florian Fainellifec3a232008-07-13 14:29:20 +0200405
406 /* Enable interrupts */
407 iowrite16(INT_MASK, ioaddr + MIER);
408
409 /* Enable TX and RX */
410 iowrite16(lp->mcr0 | 0x0002, ioaddr);
411
412 /* Let TX poll the descriptors
413 * we may got called by r6040_tx_timeout which has left
414 * some unsent tx buffers */
415 iowrite16(0x01, ioaddr + MTPR);
Florian Fainellib4f12552007-12-12 22:55:34 +0100416}
Sten Wang7a47dd72007-11-12 21:31:11 -0800417
Florian Fainelli106adf32007-12-12 23:01:33 +0100418static void r6040_tx_timeout(struct net_device *dev)
419{
420 struct r6040_private *priv = netdev_priv(dev);
421 void __iomem *ioaddr = priv->base;
422
Florian Fainelli7d53b802010-04-07 21:39:27 +0000423 netdev_warn(dev, "transmit timed out, int enable %4.4x "
Florian Fainelli38318612010-05-31 09:18:57 +0000424 "status %4.4x\n",
Florian Fainelli7d53b802010-04-07 21:39:27 +0000425 ioread16(ioaddr + MIER),
Florian Fainelli38318612010-05-31 09:18:57 +0000426 ioread16(ioaddr + MISR));
Florian Fainelli106adf32007-12-12 23:01:33 +0100427
Florian Fainelli106adf32007-12-12 23:01:33 +0100428 dev->stats.tx_errors++;
Florian Fainellifec3a232008-07-13 14:29:20 +0200429
430 /* Reset MAC and re-init all registers */
431 r6040_init_mac_regs(dev);
Florian Fainelli106adf32007-12-12 23:01:33 +0100432}
433
Sten Wang7a47dd72007-11-12 21:31:11 -0800434static struct net_device_stats *r6040_get_stats(struct net_device *dev)
435{
436 struct r6040_private *priv = netdev_priv(dev);
437 void __iomem *ioaddr = priv->base;
438 unsigned long flags;
439
440 spin_lock_irqsave(&priv->lock, flags);
Florian Fainellid248fd72007-12-12 22:34:55 +0100441 dev->stats.rx_crc_errors += ioread8(ioaddr + ME_CNT1);
442 dev->stats.multicast += ioread8(ioaddr + ME_CNT0);
Sten Wang7a47dd72007-11-12 21:31:11 -0800443 spin_unlock_irqrestore(&priv->lock, flags);
444
Florian Fainellid248fd72007-12-12 22:34:55 +0100445 return &dev->stats;
Sten Wang7a47dd72007-11-12 21:31:11 -0800446}
447
448/* Stop RDC MAC and Free the allocated resource */
449static void r6040_down(struct net_device *dev)
450{
451 struct r6040_private *lp = netdev_priv(dev);
452 void __iomem *ioaddr = lp->base;
Sten Wang7a47dd72007-11-12 21:31:11 -0800453 int limit = 2048;
454 u16 *adrp;
455 u16 cmd;
456
457 /* Stop MAC */
458 iowrite16(MSK_INT, ioaddr + MIER); /* Mask Off Interrupt */
459 iowrite16(MAC_RST, ioaddr + MCR1); /* Reset RDC MAC */
460 while (limit--) {
461 cmd = ioread16(ioaddr + MCR1);
462 if (cmd & 0x1)
463 break;
464 }
465
466 /* Restore MAC Address to MIDx */
467 adrp = (u16 *) dev->dev_addr;
468 iowrite16(adrp[0], ioaddr + MID_0L);
469 iowrite16(adrp[1], ioaddr + MID_0M);
470 iowrite16(adrp[2], ioaddr + MID_0H);
Sten Wang7a47dd72007-11-12 21:31:11 -0800471}
472
Francois Romieu5ac5d612007-11-28 23:02:33 +0100473static int r6040_close(struct net_device *dev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800474{
475 struct r6040_private *lp = netdev_priv(dev);
Florian Fainelli58854c62009-01-09 23:19:26 -0800476 struct pci_dev *pdev = lp->pdev;
Sten Wang7a47dd72007-11-12 21:31:11 -0800477
Sten Wang7a47dd72007-11-12 21:31:11 -0800478 spin_lock_irq(&lp->lock);
Florian Fainelli129cf9a2008-07-13 14:32:45 +0200479 napi_disable(&lp->napi);
Sten Wang7a47dd72007-11-12 21:31:11 -0800480 netif_stop_queue(dev);
481 r6040_down(dev);
Florian Fainelli58854c62009-01-09 23:19:26 -0800482
483 free_irq(dev->irq, dev);
484
485 /* Free RX buffer */
486 r6040_free_rxbufs(dev);
487
488 /* Free TX buffer */
489 r6040_free_txbufs(dev);
490
Sten Wang7a47dd72007-11-12 21:31:11 -0800491 spin_unlock_irq(&lp->lock);
492
Florian Fainelli58854c62009-01-09 23:19:26 -0800493 /* Free Descriptor memory */
494 if (lp->rx_ring) {
Florian Fainelli2154c7042010-08-08 10:08:44 +0000495 pci_free_consistent(pdev,
496 RX_DESC_SIZE, lp->rx_ring, lp->rx_ring_dma);
Hannes Eder5b5103e2009-02-14 11:14:04 +0000497 lp->rx_ring = NULL;
Florian Fainelli58854c62009-01-09 23:19:26 -0800498 }
499
500 if (lp->tx_ring) {
Florian Fainelli2154c7042010-08-08 10:08:44 +0000501 pci_free_consistent(pdev,
502 TX_DESC_SIZE, lp->tx_ring, lp->tx_ring_dma);
Hannes Eder5b5103e2009-02-14 11:14:04 +0000503 lp->tx_ring = NULL;
Florian Fainelli58854c62009-01-09 23:19:26 -0800504 }
505
Sten Wang7a47dd72007-11-12 21:31:11 -0800506 return 0;
507}
508
Sten Wang7a47dd72007-11-12 21:31:11 -0800509static int r6040_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
510{
511 struct r6040_private *lp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800512
Florian Fainelli38318612010-05-31 09:18:57 +0000513 if (!lp->phydev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800514 return -EINVAL;
Florian Fainelli38318612010-05-31 09:18:57 +0000515
David S. Miller4cfa5802010-07-21 21:10:49 -0700516 return phy_mii_ioctl(lp->phydev, rq, cmd);
Sten Wang7a47dd72007-11-12 21:31:11 -0800517}
518
519static int r6040_rx(struct net_device *dev, int limit)
520{
521 struct r6040_private *priv = netdev_priv(dev);
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200522 struct r6040_descriptor *descptr = priv->rx_remove_ptr;
523 struct sk_buff *skb_ptr, *new_skb;
524 int count = 0;
Sten Wang7a47dd72007-11-12 21:31:11 -0800525 u16 err;
526
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200527 /* Limit not reached and the descriptor belongs to the CPU */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200528 while (count < limit && !(descptr->status & DSC_OWNER_MAC)) {
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200529 /* Read the descriptor status */
530 err = descptr->status;
531 /* Global error status set */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200532 if (err & DSC_RX_ERR) {
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200533 /* RX dribble */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200534 if (err & DSC_RX_ERR_DRI)
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200535 dev->stats.rx_frame_errors++;
536 /* Buffer lenght exceeded */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200537 if (err & DSC_RX_ERR_BUF)
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200538 dev->stats.rx_length_errors++;
539 /* Packet too long */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200540 if (err & DSC_RX_ERR_LONG)
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200541 dev->stats.rx_length_errors++;
542 /* Packet < 64 bytes */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200543 if (err & DSC_RX_ERR_RUNT)
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200544 dev->stats.rx_length_errors++;
545 /* CRC error */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200546 if (err & DSC_RX_ERR_CRC) {
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200547 spin_lock(&priv->lock);
548 dev->stats.rx_crc_errors++;
549 spin_unlock(&priv->lock);
Sten Wang7a47dd72007-11-12 21:31:11 -0800550 }
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200551 goto next_descr;
Sten Wang7a47dd72007-11-12 21:31:11 -0800552 }
Florian Fainelli2154c7042010-08-08 10:08:44 +0000553
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200554 /* Packet successfully received */
555 new_skb = netdev_alloc_skb(dev, MAX_BUF_SIZE);
556 if (!new_skb) {
557 dev->stats.rx_dropped++;
558 goto next_descr;
559 }
560 skb_ptr = descptr->skb_ptr;
561 skb_ptr->dev = priv->dev;
Florian Fainelli2154c7042010-08-08 10:08:44 +0000562
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200563 /* Do not count the CRC */
564 skb_put(skb_ptr, descptr->len - 4);
565 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
566 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE);
567 skb_ptr->protocol = eth_type_trans(skb_ptr, priv->dev);
Florian Fainelli2154c7042010-08-08 10:08:44 +0000568
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200569 /* Send to upper layer */
570 netif_receive_skb(skb_ptr);
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200571 dev->stats.rx_packets++;
572 dev->stats.rx_bytes += descptr->len - 4;
573
574 /* put new skb into descriptor */
575 descptr->skb_ptr = new_skb;
576 descptr->buf = cpu_to_le32(pci_map_single(priv->pdev,
577 descptr->skb_ptr->data,
578 MAX_BUF_SIZE, PCI_DMA_FROMDEVICE));
579
580next_descr:
581 /* put the descriptor back to the MAC */
Florian Fainelli32f565d2008-07-13 14:34:15 +0200582 descptr->status = DSC_OWNER_MAC;
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200583 descptr = descptr->vndescp;
584 count++;
Sten Wang7a47dd72007-11-12 21:31:11 -0800585 }
Florian Fainelli9ca28dc2008-07-13 14:33:36 +0200586 priv->rx_remove_ptr = descptr;
Sten Wang7a47dd72007-11-12 21:31:11 -0800587
588 return count;
589}
590
591static void r6040_tx(struct net_device *dev)
592{
593 struct r6040_private *priv = netdev_priv(dev);
594 struct r6040_descriptor *descptr;
595 void __iomem *ioaddr = priv->base;
596 struct sk_buff *skb_ptr;
597 u16 err;
598
599 spin_lock(&priv->lock);
600 descptr = priv->tx_remove_ptr;
601 while (priv->tx_free_desc < TX_DCNT) {
602 /* Check for errors */
603 err = ioread16(ioaddr + MLSR);
604
Florian Fainellid248fd72007-12-12 22:34:55 +0100605 if (err & 0x0200)
606 dev->stats.rx_fifo_errors++;
607 if (err & (0x2000 | 0x4000))
608 dev->stats.tx_carrier_errors++;
Sten Wang7a47dd72007-11-12 21:31:11 -0800609
Florian Fainelli32f565d2008-07-13 14:34:15 +0200610 if (descptr->status & DSC_OWNER_MAC)
Florian Fainelliec6d2d42007-12-12 23:13:15 +0100611 break; /* Not complete */
Sten Wang7a47dd72007-11-12 21:31:11 -0800612 skb_ptr = descptr->skb_ptr;
Al Viroed773b42008-03-16 22:43:06 +0000613 pci_unmap_single(priv->pdev, le32_to_cpu(descptr->buf),
Sten Wang7a47dd72007-11-12 21:31:11 -0800614 skb_ptr->len, PCI_DMA_TODEVICE);
615 /* Free buffer */
616 dev_kfree_skb_irq(skb_ptr);
617 descptr->skb_ptr = NULL;
618 /* To next descriptor */
619 descptr = descptr->vndescp;
620 priv->tx_free_desc++;
621 }
622 priv->tx_remove_ptr = descptr;
623
624 if (priv->tx_free_desc)
625 netif_wake_queue(dev);
626 spin_unlock(&priv->lock);
627}
628
629static int r6040_poll(struct napi_struct *napi, int budget)
630{
631 struct r6040_private *priv =
632 container_of(napi, struct r6040_private, napi);
633 struct net_device *dev = priv->dev;
634 void __iomem *ioaddr = priv->base;
635 int work_done;
636
637 work_done = r6040_rx(dev, budget);
638
639 if (work_done < budget) {
Ben Hutchings288379f2009-01-19 16:43:59 -0800640 napi_complete(napi);
Sten Wang7a47dd72007-11-12 21:31:11 -0800641 /* Enable RX interrupt */
Florian Fainellie24ddf32008-07-13 14:35:32 +0200642 iowrite16(ioread16(ioaddr + MIER) | RX_INTS, ioaddr + MIER);
Sten Wang7a47dd72007-11-12 21:31:11 -0800643 }
644 return work_done;
645}
646
647/* The RDC interrupt handler. */
648static irqreturn_t r6040_interrupt(int irq, void *dev_id)
649{
650 struct net_device *dev = dev_id;
651 struct r6040_private *lp = netdev_priv(dev);
652 void __iomem *ioaddr = lp->base;
Joe Chou3e7c4692008-12-22 19:40:02 -0800653 u16 misr, status;
Sten Wang7a47dd72007-11-12 21:31:11 -0800654
Joe Chou3e7c4692008-12-22 19:40:02 -0800655 /* Save MIER */
656 misr = ioread16(ioaddr + MIER);
Sten Wang7a47dd72007-11-12 21:31:11 -0800657 /* Mask off RDC MAC interrupt */
658 iowrite16(MSK_INT, ioaddr + MIER);
659 /* Read MISR status and clear */
660 status = ioread16(ioaddr + MISR);
661
Florian Fainelli35976d42009-07-08 03:05:14 +0000662 if (status == 0x0000 || status == 0xffff) {
663 /* Restore RDC MAC interrupt */
664 iowrite16(misr, ioaddr + MIER);
Sten Wang7a47dd72007-11-12 21:31:11 -0800665 return IRQ_NONE;
Florian Fainelli35976d42009-07-08 03:05:14 +0000666 }
Sten Wang7a47dd72007-11-12 21:31:11 -0800667
668 /* RX interrupt request */
Florian Fainellie24ddf32008-07-13 14:35:32 +0200669 if (status & RX_INTS) {
670 if (status & RX_NO_DESC) {
671 /* RX descriptor unavailable */
672 dev->stats.rx_dropped++;
673 dev->stats.rx_missed_errors++;
674 }
675 if (status & RX_FIFO_FULL)
676 dev->stats.rx_fifo_errors++;
677
Florian Fainelli3d254342008-07-13 14:28:27 +0200678 /* Mask off RX interrupt */
Joe Chou3e7c4692008-12-22 19:40:02 -0800679 misr &= ~RX_INTS;
Ben Hutchings288379f2009-01-19 16:43:59 -0800680 napi_schedule(&lp->napi);
Sten Wang7a47dd72007-11-12 21:31:11 -0800681 }
682
683 /* TX interrupt request */
Florian Fainellie24ddf32008-07-13 14:35:32 +0200684 if (status & TX_INTS)
Sten Wang7a47dd72007-11-12 21:31:11 -0800685 r6040_tx(dev);
686
Joe Chou3e7c4692008-12-22 19:40:02 -0800687 /* Restore RDC MAC interrupt */
688 iowrite16(misr, ioaddr + MIER);
689
Florian Fainelliec6d2d42007-12-12 23:13:15 +0100690 return IRQ_HANDLED;
Sten Wang7a47dd72007-11-12 21:31:11 -0800691}
692
693#ifdef CONFIG_NET_POLL_CONTROLLER
694static void r6040_poll_controller(struct net_device *dev)
695{
696 disable_irq(dev->irq);
Francois Romieu5ac5d612007-11-28 23:02:33 +0100697 r6040_interrupt(dev->irq, dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800698 enable_irq(dev->irq);
699}
700#endif
701
Sten Wang7a47dd72007-11-12 21:31:11 -0800702/* Init RDC MAC */
Florian Fainelli3d463412008-07-13 14:32:18 +0200703static int r6040_up(struct net_device *dev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800704{
705 struct r6040_private *lp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800706 void __iomem *ioaddr = lp->base;
Florian Fainelli3d463412008-07-13 14:32:18 +0200707 int ret;
Sten Wang7a47dd72007-11-12 21:31:11 -0800708
Florian Fainellib4f12552007-12-12 22:55:34 +0100709 /* Initialise and alloc RX/TX buffers */
Florian Fainelli3d463412008-07-13 14:32:18 +0200710 r6040_init_txbufs(dev);
711 ret = r6040_alloc_rxbufs(dev);
712 if (ret)
713 return ret;
Sten Wang7a47dd72007-11-12 21:31:11 -0800714
Sten Wang7a47dd72007-11-12 21:31:11 -0800715 /* improve performance (by RDC guys) */
Florian Fainelli2154c7042010-08-08 10:08:44 +0000716 r6040_phy_write(ioaddr, 30, 17,
717 (r6040_phy_read(ioaddr, 30, 17) | 0x4000));
718 r6040_phy_write(ioaddr, 30, 17,
719 ~((~r6040_phy_read(ioaddr, 30, 17)) | 0x2000));
Florian Fainellic6e69bb2008-07-13 13:39:32 +0200720 r6040_phy_write(ioaddr, 0, 19, 0x0000);
721 r6040_phy_write(ioaddr, 0, 30, 0x01F0);
Sten Wang7a47dd72007-11-12 21:31:11 -0800722
Florian Fainellifec3a232008-07-13 14:29:20 +0200723 /* Initialize all MAC registers */
724 r6040_init_mac_regs(dev);
Florian Fainelli3d463412008-07-13 14:32:18 +0200725
726 return 0;
Sten Wang7a47dd72007-11-12 21:31:11 -0800727}
728
Sten Wang7a47dd72007-11-12 21:31:11 -0800729
730/* Read/set MAC address routines */
731static void r6040_mac_address(struct net_device *dev)
732{
733 struct r6040_private *lp = netdev_priv(dev);
734 void __iomem *ioaddr = lp->base;
735 u16 *adrp;
736
737 /* MAC operation register */
738 iowrite16(0x01, ioaddr + MCR1); /* Reset MAC */
739 iowrite16(2, ioaddr + MAC_SM); /* Reset internal state machine */
740 iowrite16(0, ioaddr + MAC_SM);
Florian Fainellic1d69932008-09-03 16:50:03 +0200741 mdelay(5);
Sten Wang7a47dd72007-11-12 21:31:11 -0800742
743 /* Restore MAC Address */
744 adrp = (u16 *) dev->dev_addr;
745 iowrite16(adrp[0], ioaddr + MID_0L);
746 iowrite16(adrp[1], ioaddr + MID_0M);
747 iowrite16(adrp[2], ioaddr + MID_0H);
Otavio Salvador42099d72010-09-26 19:58:07 -0700748
749 /* Store MAC Address in perm_addr */
750 memcpy(dev->perm_addr, dev->dev_addr, ETH_ALEN);
Sten Wang7a47dd72007-11-12 21:31:11 -0800751}
752
Francois Romieu5ac5d612007-11-28 23:02:33 +0100753static int r6040_open(struct net_device *dev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800754{
Francois Romieu5ac5d612007-11-28 23:02:33 +0100755 struct r6040_private *lp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800756 int ret;
757
758 /* Request IRQ and Register interrupt handler */
Julia Lawall91dcbf32009-11-18 08:23:00 +0000759 ret = request_irq(dev->irq, r6040_interrupt,
Sten Wang7a47dd72007-11-12 21:31:11 -0800760 IRQF_SHARED, dev->name, dev);
761 if (ret)
Denis Kirjanovced1de42010-08-24 23:57:55 +0000762 goto out;
Sten Wang7a47dd72007-11-12 21:31:11 -0800763
764 /* Set MAC address */
765 r6040_mac_address(dev);
766
767 /* Allocate Descriptor memory */
Francois Romieu6c323102007-11-28 22:31:00 +0100768 lp->rx_ring =
769 pci_alloc_consistent(lp->pdev, RX_DESC_SIZE, &lp->rx_ring_dma);
Denis Kirjanovced1de42010-08-24 23:57:55 +0000770 if (!lp->rx_ring) {
771 ret = -ENOMEM;
772 goto err_free_irq;
773 }
Sten Wang7a47dd72007-11-12 21:31:11 -0800774
Francois Romieu6c323102007-11-28 22:31:00 +0100775 lp->tx_ring =
776 pci_alloc_consistent(lp->pdev, TX_DESC_SIZE, &lp->tx_ring_dma);
777 if (!lp->tx_ring) {
Denis Kirjanovced1de42010-08-24 23:57:55 +0000778 ret = -ENOMEM;
779 goto err_free_rx_ring;
Francois Romieu6c323102007-11-28 22:31:00 +0100780 }
781
Florian Fainelli3d463412008-07-13 14:32:18 +0200782 ret = r6040_up(dev);
Denis Kirjanovced1de42010-08-24 23:57:55 +0000783 if (ret)
784 goto err_free_tx_ring;
Sten Wang7a47dd72007-11-12 21:31:11 -0800785
786 napi_enable(&lp->napi);
787 netif_start_queue(dev);
788
Sten Wang7a47dd72007-11-12 21:31:11 -0800789 return 0;
Denis Kirjanovced1de42010-08-24 23:57:55 +0000790
791err_free_tx_ring:
792 pci_free_consistent(lp->pdev, TX_DESC_SIZE, lp->tx_ring,
793 lp->tx_ring_dma);
794err_free_rx_ring:
795 pci_free_consistent(lp->pdev, RX_DESC_SIZE, lp->rx_ring,
796 lp->rx_ring_dma);
797err_free_irq:
798 free_irq(dev->irq, dev);
799out:
800 return ret;
Sten Wang7a47dd72007-11-12 21:31:11 -0800801}
802
Stephen Hemminger613573252009-08-31 19:50:58 +0000803static netdev_tx_t r6040_start_xmit(struct sk_buff *skb,
804 struct net_device *dev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800805{
806 struct r6040_private *lp = netdev_priv(dev);
807 struct r6040_descriptor *descptr;
808 void __iomem *ioaddr = lp->base;
809 unsigned long flags;
Sten Wang7a47dd72007-11-12 21:31:11 -0800810
811 /* Critical Section */
812 spin_lock_irqsave(&lp->lock, flags);
813
814 /* TX resource check */
815 if (!lp->tx_free_desc) {
816 spin_unlock_irqrestore(&lp->lock, flags);
Jeff Garzik092427b2007-11-23 21:49:27 -0500817 netif_stop_queue(dev);
Florian Fainelli7d53b802010-04-07 21:39:27 +0000818 netdev_err(dev, ": no tx descriptor\n");
Stephen Hemminger613573252009-08-31 19:50:58 +0000819 return NETDEV_TX_BUSY;
Sten Wang7a47dd72007-11-12 21:31:11 -0800820 }
821
822 /* Statistic Counter */
823 dev->stats.tx_packets++;
824 dev->stats.tx_bytes += skb->len;
825 /* Set TX descriptor & Transmit it */
826 lp->tx_free_desc--;
827 descptr = lp->tx_insert_ptr;
828 if (skb->len < MISR)
829 descptr->len = MISR;
830 else
831 descptr->len = skb->len;
832
833 descptr->skb_ptr = skb;
834 descptr->buf = cpu_to_le32(pci_map_single(lp->pdev,
835 skb->data, skb->len, PCI_DMA_TODEVICE));
Florian Fainelli32f565d2008-07-13 14:34:15 +0200836 descptr->status = DSC_OWNER_MAC;
Sten Wang7a47dd72007-11-12 21:31:11 -0800837 /* Trigger the MAC to check the TX descriptor */
838 iowrite16(0x01, ioaddr + MTPR);
839 lp->tx_insert_ptr = descptr->vndescp;
840
841 /* If no tx resource, stop */
842 if (!lp->tx_free_desc)
843 netif_stop_queue(dev);
844
Sten Wang7a47dd72007-11-12 21:31:11 -0800845 spin_unlock_irqrestore(&lp->lock, flags);
Stephen Hemminger613573252009-08-31 19:50:58 +0000846
847 return NETDEV_TX_OK;
Sten Wang7a47dd72007-11-12 21:31:11 -0800848}
849
Francois Romieu5ac5d612007-11-28 23:02:33 +0100850static void r6040_multicast_list(struct net_device *dev)
Sten Wang7a47dd72007-11-12 21:31:11 -0800851{
852 struct r6040_private *lp = netdev_priv(dev);
853 void __iomem *ioaddr = lp->base;
854 u16 *adrp;
855 u16 reg;
856 unsigned long flags;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000857 struct netdev_hw_addr *ha;
Sten Wang7a47dd72007-11-12 21:31:11 -0800858 int i;
859
860 /* MAC Address */
861 adrp = (u16 *)dev->dev_addr;
862 iowrite16(adrp[0], ioaddr + MID_0L);
863 iowrite16(adrp[1], ioaddr + MID_0M);
864 iowrite16(adrp[2], ioaddr + MID_0H);
865
866 /* Promiscous Mode */
867 spin_lock_irqsave(&lp->lock, flags);
868
869 /* Clear AMCP & PROM bits */
870 reg = ioread16(ioaddr) & ~0x0120;
871 if (dev->flags & IFF_PROMISC) {
872 reg |= 0x0020;
873 lp->mcr0 |= 0x0020;
874 }
875 /* Too many multicast addresses
876 * accept all traffic */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000877 else if ((netdev_mc_count(dev) > MCAST_MAX) ||
878 (dev->flags & IFF_ALLMULTI))
Sten Wang7a47dd72007-11-12 21:31:11 -0800879 reg |= 0x0020;
880
881 iowrite16(reg, ioaddr);
882 spin_unlock_irqrestore(&lp->lock, flags);
883
884 /* Build the hash table */
Jiri Pirko4cd24ea2010-02-08 04:30:35 +0000885 if (netdev_mc_count(dev) > MCAST_MAX) {
Sten Wang7a47dd72007-11-12 21:31:11 -0800886 u16 hash_table[4];
887 u32 crc;
888
889 for (i = 0; i < 4; i++)
890 hash_table[i] = 0;
891
Jiri Pirko22bedad32010-04-01 21:22:57 +0000892 netdev_for_each_mc_addr(ha, dev) {
893 char *addrs = ha->addr;
Sten Wang7a47dd72007-11-12 21:31:11 -0800894
Sten Wang7a47dd72007-11-12 21:31:11 -0800895 if (!(*addrs & 1))
896 continue;
897
898 crc = ether_crc_le(6, addrs);
899 crc >>= 26;
900 hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
901 }
Sten Wang7a47dd72007-11-12 21:31:11 -0800902 /* Fill the MAC hash tables with their values */
903 iowrite16(hash_table[0], ioaddr + MAR0);
904 iowrite16(hash_table[1], ioaddr + MAR1);
905 iowrite16(hash_table[2], ioaddr + MAR2);
906 iowrite16(hash_table[3], ioaddr + MAR3);
907 }
908 /* Multicast Address 1~4 case */
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000909 i = 0;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000910 netdev_for_each_mc_addr(ha, dev) {
Ben Hutchingse2269302010-10-14 17:41:53 +0000911 if (i >= MCAST_MAX)
912 break;
913 adrp = (u16 *) ha->addr;
914 iowrite16(adrp[0], ioaddr + MID_1L + 8 * i);
915 iowrite16(adrp[1], ioaddr + MID_1M + 8 * i);
916 iowrite16(adrp[2], ioaddr + MID_1H + 8 * i);
917 i++;
918 }
919 while (i < MCAST_MAX) {
920 iowrite16(0xffff, ioaddr + MID_1L + 8 * i);
921 iowrite16(0xffff, ioaddr + MID_1M + 8 * i);
922 iowrite16(0xffff, ioaddr + MID_1H + 8 * i);
Jiri Pirkof9dcbcc2010-02-23 09:19:49 +0000923 i++;
Sten Wang7a47dd72007-11-12 21:31:11 -0800924 }
925}
926
927static void netdev_get_drvinfo(struct net_device *dev,
928 struct ethtool_drvinfo *info)
929{
930 struct r6040_private *rp = netdev_priv(dev);
931
932 strcpy(info->driver, DRV_NAME);
933 strcpy(info->version, DRV_VERSION);
934 strcpy(info->bus_info, pci_name(rp->pdev));
935}
936
937static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
938{
939 struct r6040_private *rp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800940
Florian Fainelli38318612010-05-31 09:18:57 +0000941 return phy_ethtool_gset(rp->phydev, cmd);
Sten Wang7a47dd72007-11-12 21:31:11 -0800942}
943
944static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
945{
946 struct r6040_private *rp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -0800947
Florian Fainelli38318612010-05-31 09:18:57 +0000948 return phy_ethtool_sset(rp->phydev, cmd);
Sten Wang7a47dd72007-11-12 21:31:11 -0800949}
950
Stephen Hemmingera7bd89c2008-11-21 17:34:56 -0800951static const struct ethtool_ops netdev_ethtool_ops = {
Sten Wang7a47dd72007-11-12 21:31:11 -0800952 .get_drvinfo = netdev_get_drvinfo,
953 .get_settings = netdev_get_settings,
954 .set_settings = netdev_set_settings,
Florian Fainelli38318612010-05-31 09:18:57 +0000955 .get_link = ethtool_op_get_link,
Sten Wang7a47dd72007-11-12 21:31:11 -0800956};
957
Stephen Hemmingera7bd89c2008-11-21 17:34:56 -0800958static const struct net_device_ops r6040_netdev_ops = {
959 .ndo_open = r6040_open,
960 .ndo_stop = r6040_close,
961 .ndo_start_xmit = r6040_start_xmit,
962 .ndo_get_stats = r6040_get_stats,
963 .ndo_set_multicast_list = r6040_multicast_list,
964 .ndo_change_mtu = eth_change_mtu,
965 .ndo_validate_addr = eth_validate_addr,
Florian Fainelli2154c7042010-08-08 10:08:44 +0000966 .ndo_set_mac_address = eth_mac_addr,
Stephen Hemmingera7bd89c2008-11-21 17:34:56 -0800967 .ndo_do_ioctl = r6040_ioctl,
968 .ndo_tx_timeout = r6040_tx_timeout,
969#ifdef CONFIG_NET_POLL_CONTROLLER
970 .ndo_poll_controller = r6040_poll_controller,
971#endif
972};
973
Florian Fainelli38318612010-05-31 09:18:57 +0000974static void r6040_adjust_link(struct net_device *dev)
975{
976 struct r6040_private *lp = netdev_priv(dev);
977 struct phy_device *phydev = lp->phydev;
978 int status_changed = 0;
979 void __iomem *ioaddr = lp->base;
980
981 BUG_ON(!phydev);
982
983 if (lp->old_link != phydev->link) {
984 status_changed = 1;
985 lp->old_link = phydev->link;
986 }
987
988 /* reflect duplex change */
989 if (phydev->link && (lp->old_duplex != phydev->duplex)) {
990 lp->mcr0 |= (phydev->duplex == DUPLEX_FULL ? 0x8000 : 0);
991 iowrite16(lp->mcr0, ioaddr);
992
993 status_changed = 1;
994 lp->old_duplex = phydev->duplex;
995 }
996
997 if (status_changed) {
998 pr_info("%s: link %s", dev->name, phydev->link ?
999 "UP" : "DOWN");
1000 if (phydev->link)
1001 pr_cont(" - %d/%s", phydev->speed,
1002 DUPLEX_FULL == phydev->duplex ? "full" : "half");
1003 pr_cont("\n");
1004 }
1005}
1006
1007static int r6040_mii_probe(struct net_device *dev)
1008{
1009 struct r6040_private *lp = netdev_priv(dev);
1010 struct phy_device *phydev = NULL;
1011
1012 phydev = phy_find_first(lp->mii_bus);
1013 if (!phydev) {
1014 dev_err(&lp->pdev->dev, "no PHY found\n");
1015 return -ENODEV;
1016 }
1017
1018 phydev = phy_connect(dev, dev_name(&phydev->dev), &r6040_adjust_link,
1019 0, PHY_INTERFACE_MODE_MII);
1020
1021 if (IS_ERR(phydev)) {
1022 dev_err(&lp->pdev->dev, "could not attach to PHY\n");
1023 return PTR_ERR(phydev);
1024 }
1025
1026 /* mask with MAC supported features */
1027 phydev->supported &= (SUPPORTED_10baseT_Half
1028 | SUPPORTED_10baseT_Full
1029 | SUPPORTED_100baseT_Half
1030 | SUPPORTED_100baseT_Full
1031 | SUPPORTED_Autoneg
1032 | SUPPORTED_MII
1033 | SUPPORTED_TP);
1034
1035 phydev->advertising = phydev->supported;
1036 lp->phydev = phydev;
1037 lp->old_link = 0;
1038 lp->old_duplex = -1;
1039
1040 dev_info(&lp->pdev->dev, "attached PHY driver [%s] "
1041 "(mii_bus:phy_addr=%s)\n",
1042 phydev->drv->name, dev_name(&phydev->dev));
1043
1044 return 0;
1045}
1046
Sten Wang7a47dd72007-11-12 21:31:11 -08001047static int __devinit r6040_init_one(struct pci_dev *pdev,
1048 const struct pci_device_id *ent)
1049{
1050 struct net_device *dev;
1051 struct r6040_private *lp;
1052 void __iomem *ioaddr;
1053 int err, io_size = R6040_IO_SIZE;
1054 static int card_idx = -1;
1055 int bar = 0;
Sten Wang7a47dd72007-11-12 21:31:11 -08001056 u16 *adrp;
Florian Fainelli38318612010-05-31 09:18:57 +00001057 int i;
Sten Wang7a47dd72007-11-12 21:31:11 -08001058
Florian Fainelli2154c7042010-08-08 10:08:44 +00001059 pr_info("%s\n", version);
Sten Wang7a47dd72007-11-12 21:31:11 -08001060
1061 err = pci_enable_device(pdev);
1062 if (err)
Florian Fainellib0e45392008-07-21 12:32:29 +02001063 goto err_out;
Sten Wang7a47dd72007-11-12 21:31:11 -08001064
1065 /* this should always be supported */
Yang Hongyang284901a2009-04-06 19:01:15 -07001066 err = pci_set_dma_mask(pdev, DMA_BIT_MASK(32));
Florian Fainellib0e45392008-07-21 12:32:29 +02001067 if (err) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001068 dev_err(&pdev->dev, "32-bit PCI DMA addresses"
Sten Wang7a47dd72007-11-12 21:31:11 -08001069 "not supported by the card\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001070 goto err_out;
Sten Wang7a47dd72007-11-12 21:31:11 -08001071 }
Yang Hongyang284901a2009-04-06 19:01:15 -07001072 err = pci_set_consistent_dma_mask(pdev, DMA_BIT_MASK(32));
Florian Fainellib0e45392008-07-21 12:32:29 +02001073 if (err) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001074 dev_err(&pdev->dev, "32-bit PCI DMA addresses"
Jeff Garzik092427b2007-11-23 21:49:27 -05001075 "not supported by the card\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001076 goto err_out;
Jeff Garzik092427b2007-11-23 21:49:27 -05001077 }
Sten Wang7a47dd72007-11-12 21:31:11 -08001078
1079 /* IO Size check */
Michael Opdenacker6f5bec12009-06-24 21:05:09 +00001080 if (pci_resource_len(pdev, bar) < io_size) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001081 dev_err(&pdev->dev, "Insufficient PCI resources, aborting\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001082 err = -EIO;
1083 goto err_out;
Sten Wang7a47dd72007-11-12 21:31:11 -08001084 }
1085
Sten Wang7a47dd72007-11-12 21:31:11 -08001086 pci_set_master(pdev);
1087
1088 dev = alloc_etherdev(sizeof(struct r6040_private));
1089 if (!dev) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001090 dev_err(&pdev->dev, "Failed to allocate etherdev\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001091 err = -ENOMEM;
1092 goto err_out;
Sten Wang7a47dd72007-11-12 21:31:11 -08001093 }
1094 SET_NETDEV_DEV(dev, &pdev->dev);
1095 lp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -08001096
Florian Fainellib0e45392008-07-21 12:32:29 +02001097 err = pci_request_regions(pdev, DRV_NAME);
1098
1099 if (err) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001100 dev_err(&pdev->dev, "Failed to request PCI regions\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001101 goto err_out_free_dev;
Sten Wang7a47dd72007-11-12 21:31:11 -08001102 }
1103
1104 ioaddr = pci_iomap(pdev, bar, io_size);
1105 if (!ioaddr) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001106 dev_err(&pdev->dev, "ioremap failed for device\n");
Florian Fainellib0e45392008-07-21 12:32:29 +02001107 err = -EIO;
1108 goto err_out_free_res;
Sten Wang7a47dd72007-11-12 21:31:11 -08001109 }
Florian Fainelli84314bf2009-01-08 11:01:58 -08001110 /* If PHY status change register is still set to zero it means the
1111 * bootloader didn't initialize it */
1112 if (ioread16(ioaddr + PHY_CC) == 0)
1113 iowrite16(0x9f07, ioaddr + PHY_CC);
Sten Wang7a47dd72007-11-12 21:31:11 -08001114
1115 /* Init system & device */
Sten Wang7a47dd72007-11-12 21:31:11 -08001116 lp->base = ioaddr;
1117 dev->irq = pdev->irq;
1118
1119 spin_lock_init(&lp->lock);
1120 pci_set_drvdata(pdev, dev);
1121
1122 /* Set MAC address */
1123 card_idx++;
1124
1125 adrp = (u16 *)dev->dev_addr;
1126 adrp[0] = ioread16(ioaddr + MID_0L);
1127 adrp[1] = ioread16(ioaddr + MID_0M);
1128 adrp[2] = ioread16(ioaddr + MID_0H);
1129
Florian Fainelli1d2b1a72009-01-08 11:02:30 -08001130 /* Some bootloader/BIOSes do not initialize
1131 * MAC address, warn about that */
Florian Fainelli9f113612009-01-08 15:04:45 +00001132 if (!(adrp[0] || adrp[1] || adrp[2])) {
Florian Fainelli2154c7042010-08-08 10:08:44 +00001133 netdev_warn(dev, "MAC address not initialized, "
1134 "generating random\n");
Florian Fainelli9f113612009-01-08 15:04:45 +00001135 random_ether_addr(dev->dev_addr);
1136 }
Florian Fainelli1d2b1a72009-01-08 11:02:30 -08001137
Sten Wang7a47dd72007-11-12 21:31:11 -08001138 /* Link new device into r6040_root_dev */
1139 lp->pdev = pdev;
Florian Fainelli129cf9a2008-07-13 14:32:45 +02001140 lp->dev = dev;
Sten Wang7a47dd72007-11-12 21:31:11 -08001141
1142 /* Init RDC private data */
1143 lp->mcr0 = 0x1002;
1144 lp->phy_addr = phy_table[card_idx];
Sten Wang7a47dd72007-11-12 21:31:11 -08001145
1146 /* The RDC-specific entries in the device structure. */
Stephen Hemmingera7bd89c2008-11-21 17:34:56 -08001147 dev->netdev_ops = &r6040_netdev_ops;
Sten Wang7a47dd72007-11-12 21:31:11 -08001148 dev->ethtool_ops = &netdev_ethtool_ops;
Sten Wang7a47dd72007-11-12 21:31:11 -08001149 dev->watchdog_timeo = TX_TIMEOUT;
Stephen Hemmingera7bd89c2008-11-21 17:34:56 -08001150
Sten Wang7a47dd72007-11-12 21:31:11 -08001151 netif_napi_add(dev, &lp->napi, r6040_poll, 64);
Sten Wang7a47dd72007-11-12 21:31:11 -08001152
Florian Fainelli38318612010-05-31 09:18:57 +00001153 lp->mii_bus = mdiobus_alloc();
1154 if (!lp->mii_bus) {
1155 dev_err(&pdev->dev, "mdiobus_alloc() failed\n");
Axel Lin9c86c0f2011-01-04 22:40:04 +00001156 err = -ENOMEM;
Mark Kellye03f6142009-08-20 01:26:20 +00001157 goto err_out_unmap;
1158 }
1159
Florian Fainelli38318612010-05-31 09:18:57 +00001160 lp->mii_bus->priv = dev;
1161 lp->mii_bus->read = r6040_mdiobus_read;
1162 lp->mii_bus->write = r6040_mdiobus_write;
1163 lp->mii_bus->reset = r6040_mdiobus_reset;
1164 lp->mii_bus->name = "r6040_eth_mii";
1165 snprintf(lp->mii_bus->id, MII_BUS_ID_SIZE, "%x", card_idx);
1166 lp->mii_bus->irq = kmalloc(sizeof(int)*PHY_MAX_ADDR, GFP_KERNEL);
1167 if (!lp->mii_bus->irq) {
1168 dev_err(&pdev->dev, "mii_bus irq allocation failed\n");
Axel Lin9c86c0f2011-01-04 22:40:04 +00001169 err = -ENOMEM;
Florian Fainelli38318612010-05-31 09:18:57 +00001170 goto err_out_mdio;
1171 }
1172
1173 for (i = 0; i < PHY_MAX_ADDR; i++)
1174 lp->mii_bus->irq[i] = PHY_POLL;
1175
1176 err = mdiobus_register(lp->mii_bus);
1177 if (err) {
1178 dev_err(&pdev->dev, "failed to register MII bus\n");
1179 goto err_out_mdio_irq;
1180 }
1181
1182 err = r6040_mii_probe(dev);
1183 if (err) {
1184 dev_err(&pdev->dev, "failed to probe MII bus\n");
1185 goto err_out_mdio_unregister;
1186 }
1187
Sten Wang7a47dd72007-11-12 21:31:11 -08001188 /* Register net device. After this dev->name assign */
1189 err = register_netdev(dev);
1190 if (err) {
Florian Fainelli7d53b802010-04-07 21:39:27 +00001191 dev_err(&pdev->dev, "Failed to register net device\n");
Florian Fainelli38318612010-05-31 09:18:57 +00001192 goto err_out_mdio_unregister;
Sten Wang7a47dd72007-11-12 21:31:11 -08001193 }
1194 return 0;
1195
Florian Fainelli38318612010-05-31 09:18:57 +00001196err_out_mdio_unregister:
1197 mdiobus_unregister(lp->mii_bus);
1198err_out_mdio_irq:
1199 kfree(lp->mii_bus->irq);
1200err_out_mdio:
1201 mdiobus_free(lp->mii_bus);
Florian Fainellib0e45392008-07-21 12:32:29 +02001202err_out_unmap:
1203 pci_iounmap(pdev, ioaddr);
1204err_out_free_res:
Sten Wang7a47dd72007-11-12 21:31:11 -08001205 pci_release_regions(pdev);
Florian Fainellib0e45392008-07-21 12:32:29 +02001206err_out_free_dev:
Sten Wang7a47dd72007-11-12 21:31:11 -08001207 free_netdev(dev);
Florian Fainellib0e45392008-07-21 12:32:29 +02001208err_out:
Sten Wang7a47dd72007-11-12 21:31:11 -08001209 return err;
1210}
1211
1212static void __devexit r6040_remove_one(struct pci_dev *pdev)
1213{
1214 struct net_device *dev = pci_get_drvdata(pdev);
Florian Fainelli38318612010-05-31 09:18:57 +00001215 struct r6040_private *lp = netdev_priv(dev);
Sten Wang7a47dd72007-11-12 21:31:11 -08001216
1217 unregister_netdev(dev);
Florian Fainelli38318612010-05-31 09:18:57 +00001218 mdiobus_unregister(lp->mii_bus);
1219 kfree(lp->mii_bus->irq);
1220 mdiobus_free(lp->mii_bus);
Sten Wang7a47dd72007-11-12 21:31:11 -08001221 pci_release_regions(pdev);
1222 free_netdev(dev);
1223 pci_disable_device(pdev);
1224 pci_set_drvdata(pdev, NULL);
1225}
1226
1227
Alexey Dobriyana3aa1882010-01-07 11:58:11 +00001228static DEFINE_PCI_DEVICE_TABLE(r6040_pci_tbl) = {
Francois Romieu5ac5d612007-11-28 23:02:33 +01001229 { PCI_DEVICE(PCI_VENDOR_ID_RDC, 0x6040) },
1230 { 0 }
Sten Wang7a47dd72007-11-12 21:31:11 -08001231};
1232MODULE_DEVICE_TABLE(pci, r6040_pci_tbl);
1233
1234static struct pci_driver r6040_driver = {
Francois Romieu5ac5d612007-11-28 23:02:33 +01001235 .name = DRV_NAME,
Sten Wang7a47dd72007-11-12 21:31:11 -08001236 .id_table = r6040_pci_tbl,
1237 .probe = r6040_init_one,
1238 .remove = __devexit_p(r6040_remove_one),
1239};
1240
1241
1242static int __init r6040_init(void)
1243{
1244 return pci_register_driver(&r6040_driver);
1245}
1246
1247
1248static void __exit r6040_cleanup(void)
1249{
1250 pci_unregister_driver(&r6040_driver);
1251}
1252
1253module_init(r6040_init);
1254module_exit(r6040_cleanup);