xref: /openbmc/linux/drivers/net/ethernet/korina.c (revision afc4b13df143122f99a0eb10bfefb216c2806de0)
119c72cacSJeff Kirsher /*
219c72cacSJeff Kirsher  *  Driver for the IDT RC32434 (Korina) on-chip ethernet controller.
319c72cacSJeff Kirsher  *
419c72cacSJeff Kirsher  *  Copyright 2004 IDT Inc. (rischelp@idt.com)
519c72cacSJeff Kirsher  *  Copyright 2006 Felix Fietkau <nbd@openwrt.org>
619c72cacSJeff Kirsher  *  Copyright 2008 Florian Fainelli <florian@openwrt.org>
719c72cacSJeff Kirsher  *
819c72cacSJeff Kirsher  *  This program is free software; you can redistribute  it and/or modify it
919c72cacSJeff Kirsher  *  under  the terms of  the GNU General  Public License as published by the
1019c72cacSJeff Kirsher  *  Free Software Foundation;  either version 2 of the  License, or (at your
1119c72cacSJeff Kirsher  *  option) any later version.
1219c72cacSJeff Kirsher  *
1319c72cacSJeff Kirsher  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
1419c72cacSJeff Kirsher  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
1519c72cacSJeff Kirsher  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1619c72cacSJeff Kirsher  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
1719c72cacSJeff Kirsher  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1819c72cacSJeff Kirsher  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
1919c72cacSJeff Kirsher  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2019c72cacSJeff Kirsher  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
2119c72cacSJeff Kirsher  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2219c72cacSJeff Kirsher  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2319c72cacSJeff Kirsher  *
2419c72cacSJeff Kirsher  *  You should have received a copy of the  GNU General Public License along
2519c72cacSJeff Kirsher  *  with this program; if not, write  to the Free Software Foundation, Inc.,
2619c72cacSJeff Kirsher  *  675 Mass Ave, Cambridge, MA 02139, USA.
2719c72cacSJeff Kirsher  *
2819c72cacSJeff Kirsher  *  Writing to a DMA status register:
2919c72cacSJeff Kirsher  *
3019c72cacSJeff Kirsher  *  When writing to the status register, you should mask the bit you have
3119c72cacSJeff Kirsher  *  been testing the status register with. Both Tx and Rx DMA registers
3219c72cacSJeff Kirsher  *  should stick to this procedure.
3319c72cacSJeff Kirsher  */
3419c72cacSJeff Kirsher 
3519c72cacSJeff Kirsher #include <linux/module.h>
3619c72cacSJeff Kirsher #include <linux/kernel.h>
3719c72cacSJeff Kirsher #include <linux/moduleparam.h>
3819c72cacSJeff Kirsher #include <linux/sched.h>
3919c72cacSJeff Kirsher #include <linux/ctype.h>
4019c72cacSJeff Kirsher #include <linux/types.h>
4119c72cacSJeff Kirsher #include <linux/interrupt.h>
4219c72cacSJeff Kirsher #include <linux/init.h>
4319c72cacSJeff Kirsher #include <linux/ioport.h>
4419c72cacSJeff Kirsher #include <linux/in.h>
4519c72cacSJeff Kirsher #include <linux/slab.h>
4619c72cacSJeff Kirsher #include <linux/string.h>
4719c72cacSJeff Kirsher #include <linux/delay.h>
4819c72cacSJeff Kirsher #include <linux/netdevice.h>
4919c72cacSJeff Kirsher #include <linux/etherdevice.h>
5019c72cacSJeff Kirsher #include <linux/skbuff.h>
5119c72cacSJeff Kirsher #include <linux/errno.h>
5219c72cacSJeff Kirsher #include <linux/platform_device.h>
5319c72cacSJeff Kirsher #include <linux/mii.h>
5419c72cacSJeff Kirsher #include <linux/ethtool.h>
5519c72cacSJeff Kirsher #include <linux/crc32.h>
5619c72cacSJeff Kirsher 
5719c72cacSJeff Kirsher #include <asm/bootinfo.h>
5819c72cacSJeff Kirsher #include <asm/system.h>
5919c72cacSJeff Kirsher #include <asm/bitops.h>
6019c72cacSJeff Kirsher #include <asm/pgtable.h>
6119c72cacSJeff Kirsher #include <asm/segment.h>
6219c72cacSJeff Kirsher #include <asm/io.h>
6319c72cacSJeff Kirsher #include <asm/dma.h>
6419c72cacSJeff Kirsher 
6519c72cacSJeff Kirsher #include <asm/mach-rc32434/rb.h>
6619c72cacSJeff Kirsher #include <asm/mach-rc32434/rc32434.h>
6719c72cacSJeff Kirsher #include <asm/mach-rc32434/eth.h>
6819c72cacSJeff Kirsher #include <asm/mach-rc32434/dma_v.h>
6919c72cacSJeff Kirsher 
7019c72cacSJeff Kirsher #define DRV_NAME        "korina"
7119c72cacSJeff Kirsher #define DRV_VERSION     "0.10"
7219c72cacSJeff Kirsher #define DRV_RELDATE     "04Mar2008"
7319c72cacSJeff Kirsher 
7419c72cacSJeff Kirsher #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \
7519c72cacSJeff Kirsher 				   ((dev)->dev_addr[1]))
7619c72cacSJeff Kirsher #define STATION_ADDRESS_LOW(dev)  (((dev)->dev_addr[2] << 24) | \
7719c72cacSJeff Kirsher 				   ((dev)->dev_addr[3] << 16) | \
7819c72cacSJeff Kirsher 				   ((dev)->dev_addr[4] << 8)  | \
7919c72cacSJeff Kirsher 				   ((dev)->dev_addr[5]))
8019c72cacSJeff Kirsher 
8119c72cacSJeff Kirsher #define MII_CLOCK 1250000 	/* no more than 2.5MHz */
8219c72cacSJeff Kirsher 
8319c72cacSJeff Kirsher /* the following must be powers of two */
8419c72cacSJeff Kirsher #define KORINA_NUM_RDS	64  /* number of receive descriptors */
8519c72cacSJeff Kirsher #define KORINA_NUM_TDS	64  /* number of transmit descriptors */
8619c72cacSJeff Kirsher 
8719c72cacSJeff Kirsher /* KORINA_RBSIZE is the hardware's default maximum receive
8819c72cacSJeff Kirsher  * frame size in bytes. Having this hardcoded means that there
8919c72cacSJeff Kirsher  * is no support for MTU sizes greater than 1500. */
9019c72cacSJeff Kirsher #define KORINA_RBSIZE	1536 /* size of one resource buffer = Ether MTU */
9119c72cacSJeff Kirsher #define KORINA_RDS_MASK	(KORINA_NUM_RDS - 1)
9219c72cacSJeff Kirsher #define KORINA_TDS_MASK	(KORINA_NUM_TDS - 1)
9319c72cacSJeff Kirsher #define RD_RING_SIZE 	(KORINA_NUM_RDS * sizeof(struct dma_desc))
9419c72cacSJeff Kirsher #define TD_RING_SIZE	(KORINA_NUM_TDS * sizeof(struct dma_desc))
9519c72cacSJeff Kirsher 
9619c72cacSJeff Kirsher #define TX_TIMEOUT 	(6000 * HZ / 1000)
9719c72cacSJeff Kirsher 
9819c72cacSJeff Kirsher enum chain_status { desc_filled, desc_empty };
9919c72cacSJeff Kirsher #define IS_DMA_FINISHED(X)   (((X) & (DMA_DESC_FINI)) != 0)
10019c72cacSJeff Kirsher #define IS_DMA_DONE(X)   (((X) & (DMA_DESC_DONE)) != 0)
10119c72cacSJeff Kirsher #define RCVPKT_LENGTH(X)     (((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
10219c72cacSJeff Kirsher 
10319c72cacSJeff Kirsher /* Information that need to be kept for each board. */
10419c72cacSJeff Kirsher struct korina_private {
10519c72cacSJeff Kirsher 	struct eth_regs *eth_regs;
10619c72cacSJeff Kirsher 	struct dma_reg *rx_dma_regs;
10719c72cacSJeff Kirsher 	struct dma_reg *tx_dma_regs;
10819c72cacSJeff Kirsher 	struct dma_desc *td_ring; /* transmit descriptor ring */
10919c72cacSJeff Kirsher 	struct dma_desc *rd_ring; /* receive descriptor ring  */
11019c72cacSJeff Kirsher 
11119c72cacSJeff Kirsher 	struct sk_buff *tx_skb[KORINA_NUM_TDS];
11219c72cacSJeff Kirsher 	struct sk_buff *rx_skb[KORINA_NUM_RDS];
11319c72cacSJeff Kirsher 
11419c72cacSJeff Kirsher 	int rx_next_done;
11519c72cacSJeff Kirsher 	int rx_chain_head;
11619c72cacSJeff Kirsher 	int rx_chain_tail;
11719c72cacSJeff Kirsher 	enum chain_status rx_chain_status;
11819c72cacSJeff Kirsher 
11919c72cacSJeff Kirsher 	int tx_next_done;
12019c72cacSJeff Kirsher 	int tx_chain_head;
12119c72cacSJeff Kirsher 	int tx_chain_tail;
12219c72cacSJeff Kirsher 	enum chain_status tx_chain_status;
12319c72cacSJeff Kirsher 	int tx_count;
12419c72cacSJeff Kirsher 	int tx_full;
12519c72cacSJeff Kirsher 
12619c72cacSJeff Kirsher 	int rx_irq;
12719c72cacSJeff Kirsher 	int tx_irq;
12819c72cacSJeff Kirsher 	int ovr_irq;
12919c72cacSJeff Kirsher 	int und_irq;
13019c72cacSJeff Kirsher 
13119c72cacSJeff Kirsher 	spinlock_t lock;        /* NIC xmit lock */
13219c72cacSJeff Kirsher 
13319c72cacSJeff Kirsher 	int dma_halt_cnt;
13419c72cacSJeff Kirsher 	int dma_run_cnt;
13519c72cacSJeff Kirsher 	struct napi_struct napi;
13619c72cacSJeff Kirsher 	struct timer_list media_check_timer;
13719c72cacSJeff Kirsher 	struct mii_if_info mii_if;
13819c72cacSJeff Kirsher 	struct work_struct restart_task;
13919c72cacSJeff Kirsher 	struct net_device *dev;
14019c72cacSJeff Kirsher 	int phy_addr;
14119c72cacSJeff Kirsher };
14219c72cacSJeff Kirsher 
14319c72cacSJeff Kirsher extern unsigned int idt_cpu_freq;
14419c72cacSJeff Kirsher 
14519c72cacSJeff Kirsher static inline void korina_start_dma(struct dma_reg *ch, u32 dma_addr)
14619c72cacSJeff Kirsher {
14719c72cacSJeff Kirsher 	writel(0, &ch->dmandptr);
14819c72cacSJeff Kirsher 	writel(dma_addr, &ch->dmadptr);
14919c72cacSJeff Kirsher }
15019c72cacSJeff Kirsher 
15119c72cacSJeff Kirsher static inline void korina_abort_dma(struct net_device *dev,
15219c72cacSJeff Kirsher 					struct dma_reg *ch)
15319c72cacSJeff Kirsher {
15419c72cacSJeff Kirsher        if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
15519c72cacSJeff Kirsher 	       writel(0x10, &ch->dmac);
15619c72cacSJeff Kirsher 
15719c72cacSJeff Kirsher 	       while (!(readl(&ch->dmas) & DMA_STAT_HALT))
15819c72cacSJeff Kirsher 		       dev->trans_start = jiffies;
15919c72cacSJeff Kirsher 
16019c72cacSJeff Kirsher 	       writel(0, &ch->dmas);
16119c72cacSJeff Kirsher        }
16219c72cacSJeff Kirsher 
16319c72cacSJeff Kirsher        writel(0, &ch->dmadptr);
16419c72cacSJeff Kirsher        writel(0, &ch->dmandptr);
16519c72cacSJeff Kirsher }
16619c72cacSJeff Kirsher 
16719c72cacSJeff Kirsher static inline void korina_chain_dma(struct dma_reg *ch, u32 dma_addr)
16819c72cacSJeff Kirsher {
16919c72cacSJeff Kirsher 	writel(dma_addr, &ch->dmandptr);
17019c72cacSJeff Kirsher }
17119c72cacSJeff Kirsher 
17219c72cacSJeff Kirsher static void korina_abort_tx(struct net_device *dev)
17319c72cacSJeff Kirsher {
17419c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
17519c72cacSJeff Kirsher 
17619c72cacSJeff Kirsher 	korina_abort_dma(dev, lp->tx_dma_regs);
17719c72cacSJeff Kirsher }
17819c72cacSJeff Kirsher 
17919c72cacSJeff Kirsher static void korina_abort_rx(struct net_device *dev)
18019c72cacSJeff Kirsher {
18119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
18219c72cacSJeff Kirsher 
18319c72cacSJeff Kirsher 	korina_abort_dma(dev, lp->rx_dma_regs);
18419c72cacSJeff Kirsher }
18519c72cacSJeff Kirsher 
18619c72cacSJeff Kirsher static void korina_start_rx(struct korina_private *lp,
18719c72cacSJeff Kirsher 					struct dma_desc *rd)
18819c72cacSJeff Kirsher {
18919c72cacSJeff Kirsher 	korina_start_dma(lp->rx_dma_regs, CPHYSADDR(rd));
19019c72cacSJeff Kirsher }
19119c72cacSJeff Kirsher 
19219c72cacSJeff Kirsher static void korina_chain_rx(struct korina_private *lp,
19319c72cacSJeff Kirsher 					struct dma_desc *rd)
19419c72cacSJeff Kirsher {
19519c72cacSJeff Kirsher 	korina_chain_dma(lp->rx_dma_regs, CPHYSADDR(rd));
19619c72cacSJeff Kirsher }
19719c72cacSJeff Kirsher 
19819c72cacSJeff Kirsher /* transmit packet */
19919c72cacSJeff Kirsher static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
20019c72cacSJeff Kirsher {
20119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
20219c72cacSJeff Kirsher 	unsigned long flags;
20319c72cacSJeff Kirsher 	u32 length;
20419c72cacSJeff Kirsher 	u32 chain_prev, chain_next;
20519c72cacSJeff Kirsher 	struct dma_desc *td;
20619c72cacSJeff Kirsher 
20719c72cacSJeff Kirsher 	spin_lock_irqsave(&lp->lock, flags);
20819c72cacSJeff Kirsher 
20919c72cacSJeff Kirsher 	td = &lp->td_ring[lp->tx_chain_tail];
21019c72cacSJeff Kirsher 
21119c72cacSJeff Kirsher 	/* stop queue when full, drop pkts if queue already full */
21219c72cacSJeff Kirsher 	if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
21319c72cacSJeff Kirsher 		lp->tx_full = 1;
21419c72cacSJeff Kirsher 
21519c72cacSJeff Kirsher 		if (lp->tx_count == (KORINA_NUM_TDS - 2))
21619c72cacSJeff Kirsher 			netif_stop_queue(dev);
21719c72cacSJeff Kirsher 		else {
21819c72cacSJeff Kirsher 			dev->stats.tx_dropped++;
21919c72cacSJeff Kirsher 			dev_kfree_skb_any(skb);
22019c72cacSJeff Kirsher 			spin_unlock_irqrestore(&lp->lock, flags);
22119c72cacSJeff Kirsher 
22219c72cacSJeff Kirsher 			return NETDEV_TX_BUSY;
22319c72cacSJeff Kirsher 		}
22419c72cacSJeff Kirsher 	}
22519c72cacSJeff Kirsher 
22619c72cacSJeff Kirsher 	lp->tx_count++;
22719c72cacSJeff Kirsher 
22819c72cacSJeff Kirsher 	lp->tx_skb[lp->tx_chain_tail] = skb;
22919c72cacSJeff Kirsher 
23019c72cacSJeff Kirsher 	length = skb->len;
23119c72cacSJeff Kirsher 	dma_cache_wback((u32)skb->data, skb->len);
23219c72cacSJeff Kirsher 
23319c72cacSJeff Kirsher 	/* Setup the transmit descriptor. */
23419c72cacSJeff Kirsher 	dma_cache_inv((u32) td, sizeof(*td));
23519c72cacSJeff Kirsher 	td->ca = CPHYSADDR(skb->data);
23619c72cacSJeff Kirsher 	chain_prev = (lp->tx_chain_tail - 1) & KORINA_TDS_MASK;
23719c72cacSJeff Kirsher 	chain_next = (lp->tx_chain_tail + 1) & KORINA_TDS_MASK;
23819c72cacSJeff Kirsher 
23919c72cacSJeff Kirsher 	if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
24019c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_empty) {
24119c72cacSJeff Kirsher 			/* Update tail */
24219c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
24319c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
24419c72cacSJeff Kirsher 			/* Move tail */
24519c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
24619c72cacSJeff Kirsher 			/* Write to NDPTR */
24719c72cacSJeff Kirsher 			writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
24819c72cacSJeff Kirsher 					&lp->tx_dma_regs->dmandptr);
24919c72cacSJeff Kirsher 			/* Move head to tail */
25019c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
25119c72cacSJeff Kirsher 		} else {
25219c72cacSJeff Kirsher 			/* Update tail */
25319c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
25419c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
25519c72cacSJeff Kirsher 			/* Link to prev */
25619c72cacSJeff Kirsher 			lp->td_ring[chain_prev].control &=
25719c72cacSJeff Kirsher 					~DMA_DESC_COF;
25819c72cacSJeff Kirsher 			/* Link to prev */
25919c72cacSJeff Kirsher 			lp->td_ring[chain_prev].link =  CPHYSADDR(td);
26019c72cacSJeff Kirsher 			/* Move tail */
26119c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
26219c72cacSJeff Kirsher 			/* Write to NDPTR */
26319c72cacSJeff Kirsher 			writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
26419c72cacSJeff Kirsher 					&(lp->tx_dma_regs->dmandptr));
26519c72cacSJeff Kirsher 			/* Move head to tail */
26619c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
26719c72cacSJeff Kirsher 			lp->tx_chain_status = desc_empty;
26819c72cacSJeff Kirsher 		}
26919c72cacSJeff Kirsher 	} else {
27019c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_empty) {
27119c72cacSJeff Kirsher 			/* Update tail */
27219c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
27319c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
27419c72cacSJeff Kirsher 			/* Move tail */
27519c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
27619c72cacSJeff Kirsher 			lp->tx_chain_status = desc_filled;
27719c72cacSJeff Kirsher 		} else {
27819c72cacSJeff Kirsher 			/* Update tail */
27919c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
28019c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
28119c72cacSJeff Kirsher 			lp->td_ring[chain_prev].control &=
28219c72cacSJeff Kirsher 					~DMA_DESC_COF;
28319c72cacSJeff Kirsher 			lp->td_ring[chain_prev].link =  CPHYSADDR(td);
28419c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
28519c72cacSJeff Kirsher 		}
28619c72cacSJeff Kirsher 	}
28719c72cacSJeff Kirsher 	dma_cache_wback((u32) td, sizeof(*td));
28819c72cacSJeff Kirsher 
28919c72cacSJeff Kirsher 	dev->trans_start = jiffies;
29019c72cacSJeff Kirsher 	spin_unlock_irqrestore(&lp->lock, flags);
29119c72cacSJeff Kirsher 
29219c72cacSJeff Kirsher 	return NETDEV_TX_OK;
29319c72cacSJeff Kirsher }
29419c72cacSJeff Kirsher 
29519c72cacSJeff Kirsher static int mdio_read(struct net_device *dev, int mii_id, int reg)
29619c72cacSJeff Kirsher {
29719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
29819c72cacSJeff Kirsher 	int ret;
29919c72cacSJeff Kirsher 
30019c72cacSJeff Kirsher 	mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
30119c72cacSJeff Kirsher 
30219c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->miimcfg);
30319c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->miimcmd);
30419c72cacSJeff Kirsher 	writel(mii_id | reg, &lp->eth_regs->miimaddr);
30519c72cacSJeff Kirsher 	writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
30619c72cacSJeff Kirsher 
30719c72cacSJeff Kirsher 	ret = (int)(readl(&lp->eth_regs->miimrdd));
30819c72cacSJeff Kirsher 	return ret;
30919c72cacSJeff Kirsher }
31019c72cacSJeff Kirsher 
31119c72cacSJeff Kirsher static void mdio_write(struct net_device *dev, int mii_id, int reg, int val)
31219c72cacSJeff Kirsher {
31319c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
31419c72cacSJeff Kirsher 
31519c72cacSJeff Kirsher 	mii_id = ((lp->rx_irq == 0x2c ? 1 : 0) << 8);
31619c72cacSJeff Kirsher 
31719c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->miimcfg);
31819c72cacSJeff Kirsher 	writel(1, &lp->eth_regs->miimcmd);
31919c72cacSJeff Kirsher 	writel(mii_id | reg, &lp->eth_regs->miimaddr);
32019c72cacSJeff Kirsher 	writel(ETH_MII_CMD_SCN, &lp->eth_regs->miimcmd);
32119c72cacSJeff Kirsher 	writel(val, &lp->eth_regs->miimwtd);
32219c72cacSJeff Kirsher }
32319c72cacSJeff Kirsher 
32419c72cacSJeff Kirsher /* Ethernet Rx DMA interrupt */
32519c72cacSJeff Kirsher static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
32619c72cacSJeff Kirsher {
32719c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
32819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
32919c72cacSJeff Kirsher 	u32 dmas, dmasm;
33019c72cacSJeff Kirsher 	irqreturn_t retval;
33119c72cacSJeff Kirsher 
33219c72cacSJeff Kirsher 	dmas = readl(&lp->rx_dma_regs->dmas);
33319c72cacSJeff Kirsher 	if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
33419c72cacSJeff Kirsher 		dmasm = readl(&lp->rx_dma_regs->dmasm);
33519c72cacSJeff Kirsher 		writel(dmasm | (DMA_STAT_DONE |
33619c72cacSJeff Kirsher 				DMA_STAT_HALT | DMA_STAT_ERR),
33719c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmasm);
33819c72cacSJeff Kirsher 
33919c72cacSJeff Kirsher 		napi_schedule(&lp->napi);
34019c72cacSJeff Kirsher 
34119c72cacSJeff Kirsher 		if (dmas & DMA_STAT_ERR)
34219c72cacSJeff Kirsher 			printk(KERN_ERR "%s: DMA error\n", dev->name);
34319c72cacSJeff Kirsher 
34419c72cacSJeff Kirsher 		retval = IRQ_HANDLED;
34519c72cacSJeff Kirsher 	} else
34619c72cacSJeff Kirsher 		retval = IRQ_NONE;
34719c72cacSJeff Kirsher 
34819c72cacSJeff Kirsher 	return retval;
34919c72cacSJeff Kirsher }
35019c72cacSJeff Kirsher 
35119c72cacSJeff Kirsher static int korina_rx(struct net_device *dev, int limit)
35219c72cacSJeff Kirsher {
35319c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
35419c72cacSJeff Kirsher 	struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
35519c72cacSJeff Kirsher 	struct sk_buff *skb, *skb_new;
35619c72cacSJeff Kirsher 	u8 *pkt_buf;
35719c72cacSJeff Kirsher 	u32 devcs, pkt_len, dmas;
35819c72cacSJeff Kirsher 	int count;
35919c72cacSJeff Kirsher 
36019c72cacSJeff Kirsher 	dma_cache_inv((u32)rd, sizeof(*rd));
36119c72cacSJeff Kirsher 
36219c72cacSJeff Kirsher 	for (count = 0; count < limit; count++) {
36319c72cacSJeff Kirsher 		skb = lp->rx_skb[lp->rx_next_done];
36419c72cacSJeff Kirsher 		skb_new = NULL;
36519c72cacSJeff Kirsher 
36619c72cacSJeff Kirsher 		devcs = rd->devcs;
36719c72cacSJeff Kirsher 
36819c72cacSJeff Kirsher 		if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0)
36919c72cacSJeff Kirsher 			break;
37019c72cacSJeff Kirsher 
37119c72cacSJeff Kirsher 		/* Update statistics counters */
37219c72cacSJeff Kirsher 		if (devcs & ETH_RX_CRC)
37319c72cacSJeff Kirsher 			dev->stats.rx_crc_errors++;
37419c72cacSJeff Kirsher 		if (devcs & ETH_RX_LOR)
37519c72cacSJeff Kirsher 			dev->stats.rx_length_errors++;
37619c72cacSJeff Kirsher 		if (devcs & ETH_RX_LE)
37719c72cacSJeff Kirsher 			dev->stats.rx_length_errors++;
37819c72cacSJeff Kirsher 		if (devcs & ETH_RX_OVR)
37919c72cacSJeff Kirsher 			dev->stats.rx_fifo_errors++;
38019c72cacSJeff Kirsher 		if (devcs & ETH_RX_CV)
38119c72cacSJeff Kirsher 			dev->stats.rx_frame_errors++;
38219c72cacSJeff Kirsher 		if (devcs & ETH_RX_CES)
38319c72cacSJeff Kirsher 			dev->stats.rx_length_errors++;
38419c72cacSJeff Kirsher 		if (devcs & ETH_RX_MP)
38519c72cacSJeff Kirsher 			dev->stats.multicast++;
38619c72cacSJeff Kirsher 
38719c72cacSJeff Kirsher 		if ((devcs & ETH_RX_LD) != ETH_RX_LD) {
38819c72cacSJeff Kirsher 			/* check that this is a whole packet
38919c72cacSJeff Kirsher 			 * WARNING: DMA_FD bit incorrectly set
39019c72cacSJeff Kirsher 			 * in Rc32434 (errata ref #077) */
39119c72cacSJeff Kirsher 			dev->stats.rx_errors++;
39219c72cacSJeff Kirsher 			dev->stats.rx_dropped++;
39319c72cacSJeff Kirsher 		} else if ((devcs & ETH_RX_ROK)) {
39419c72cacSJeff Kirsher 			pkt_len = RCVPKT_LENGTH(devcs);
39519c72cacSJeff Kirsher 
39619c72cacSJeff Kirsher 			/* must be the (first and) last
39719c72cacSJeff Kirsher 			 * descriptor then */
39819c72cacSJeff Kirsher 			pkt_buf = (u8 *)lp->rx_skb[lp->rx_next_done]->data;
39919c72cacSJeff Kirsher 
40019c72cacSJeff Kirsher 			/* invalidate the cache */
40119c72cacSJeff Kirsher 			dma_cache_inv((unsigned long)pkt_buf, pkt_len - 4);
40219c72cacSJeff Kirsher 
40319c72cacSJeff Kirsher 			/* Malloc up new buffer. */
40419c72cacSJeff Kirsher 			skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
40519c72cacSJeff Kirsher 
40619c72cacSJeff Kirsher 			if (!skb_new)
40719c72cacSJeff Kirsher 				break;
40819c72cacSJeff Kirsher 			/* Do not count the CRC */
40919c72cacSJeff Kirsher 			skb_put(skb, pkt_len - 4);
41019c72cacSJeff Kirsher 			skb->protocol = eth_type_trans(skb, dev);
41119c72cacSJeff Kirsher 
41219c72cacSJeff Kirsher 			/* Pass the packet to upper layers */
41319c72cacSJeff Kirsher 			netif_receive_skb(skb);
41419c72cacSJeff Kirsher 			dev->stats.rx_packets++;
41519c72cacSJeff Kirsher 			dev->stats.rx_bytes += pkt_len;
41619c72cacSJeff Kirsher 
41719c72cacSJeff Kirsher 			/* Update the mcast stats */
41819c72cacSJeff Kirsher 			if (devcs & ETH_RX_MP)
41919c72cacSJeff Kirsher 				dev->stats.multicast++;
42019c72cacSJeff Kirsher 
42119c72cacSJeff Kirsher 			lp->rx_skb[lp->rx_next_done] = skb_new;
42219c72cacSJeff Kirsher 		}
42319c72cacSJeff Kirsher 
42419c72cacSJeff Kirsher 		rd->devcs = 0;
42519c72cacSJeff Kirsher 
42619c72cacSJeff Kirsher 		/* Restore descriptor's curr_addr */
42719c72cacSJeff Kirsher 		if (skb_new)
42819c72cacSJeff Kirsher 			rd->ca = CPHYSADDR(skb_new->data);
42919c72cacSJeff Kirsher 		else
43019c72cacSJeff Kirsher 			rd->ca = CPHYSADDR(skb->data);
43119c72cacSJeff Kirsher 
43219c72cacSJeff Kirsher 		rd->control = DMA_COUNT(KORINA_RBSIZE) |
43319c72cacSJeff Kirsher 			DMA_DESC_COD | DMA_DESC_IOD;
43419c72cacSJeff Kirsher 		lp->rd_ring[(lp->rx_next_done - 1) &
43519c72cacSJeff Kirsher 			KORINA_RDS_MASK].control &=
43619c72cacSJeff Kirsher 			~DMA_DESC_COD;
43719c72cacSJeff Kirsher 
43819c72cacSJeff Kirsher 		lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
43919c72cacSJeff Kirsher 		dma_cache_wback((u32)rd, sizeof(*rd));
44019c72cacSJeff Kirsher 		rd = &lp->rd_ring[lp->rx_next_done];
44119c72cacSJeff Kirsher 		writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
44219c72cacSJeff Kirsher 	}
44319c72cacSJeff Kirsher 
44419c72cacSJeff Kirsher 	dmas = readl(&lp->rx_dma_regs->dmas);
44519c72cacSJeff Kirsher 
44619c72cacSJeff Kirsher 	if (dmas & DMA_STAT_HALT) {
44719c72cacSJeff Kirsher 		writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
44819c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmas);
44919c72cacSJeff Kirsher 
45019c72cacSJeff Kirsher 		lp->dma_halt_cnt++;
45119c72cacSJeff Kirsher 		rd->devcs = 0;
45219c72cacSJeff Kirsher 		skb = lp->rx_skb[lp->rx_next_done];
45319c72cacSJeff Kirsher 		rd->ca = CPHYSADDR(skb->data);
45419c72cacSJeff Kirsher 		dma_cache_wback((u32)rd, sizeof(*rd));
45519c72cacSJeff Kirsher 		korina_chain_rx(lp, rd);
45619c72cacSJeff Kirsher 	}
45719c72cacSJeff Kirsher 
45819c72cacSJeff Kirsher 	return count;
45919c72cacSJeff Kirsher }
46019c72cacSJeff Kirsher 
46119c72cacSJeff Kirsher static int korina_poll(struct napi_struct *napi, int budget)
46219c72cacSJeff Kirsher {
46319c72cacSJeff Kirsher 	struct korina_private *lp =
46419c72cacSJeff Kirsher 		container_of(napi, struct korina_private, napi);
46519c72cacSJeff Kirsher 	struct net_device *dev = lp->dev;
46619c72cacSJeff Kirsher 	int work_done;
46719c72cacSJeff Kirsher 
46819c72cacSJeff Kirsher 	work_done = korina_rx(dev, budget);
46919c72cacSJeff Kirsher 	if (work_done < budget) {
47019c72cacSJeff Kirsher 		napi_complete(napi);
47119c72cacSJeff Kirsher 
47219c72cacSJeff Kirsher 		writel(readl(&lp->rx_dma_regs->dmasm) &
47319c72cacSJeff Kirsher 			~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
47419c72cacSJeff Kirsher 			&lp->rx_dma_regs->dmasm);
47519c72cacSJeff Kirsher 	}
47619c72cacSJeff Kirsher 	return work_done;
47719c72cacSJeff Kirsher }
47819c72cacSJeff Kirsher 
47919c72cacSJeff Kirsher /*
48019c72cacSJeff Kirsher  * Set or clear the multicast filter for this adaptor.
48119c72cacSJeff Kirsher  */
48219c72cacSJeff Kirsher static void korina_multicast_list(struct net_device *dev)
48319c72cacSJeff Kirsher {
48419c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
48519c72cacSJeff Kirsher 	unsigned long flags;
48619c72cacSJeff Kirsher 	struct netdev_hw_addr *ha;
48719c72cacSJeff Kirsher 	u32 recognise = ETH_ARC_AB;	/* always accept broadcasts */
48819c72cacSJeff Kirsher 	int i;
48919c72cacSJeff Kirsher 
49019c72cacSJeff Kirsher 	/* Set promiscuous mode */
49119c72cacSJeff Kirsher 	if (dev->flags & IFF_PROMISC)
49219c72cacSJeff Kirsher 		recognise |= ETH_ARC_PRO;
49319c72cacSJeff Kirsher 
49419c72cacSJeff Kirsher 	else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
49519c72cacSJeff Kirsher 		/* All multicast and broadcast */
49619c72cacSJeff Kirsher 		recognise |= ETH_ARC_AM;
49719c72cacSJeff Kirsher 
49819c72cacSJeff Kirsher 	/* Build the hash table */
49919c72cacSJeff Kirsher 	if (netdev_mc_count(dev) > 4) {
50019c72cacSJeff Kirsher 		u16 hash_table[4];
50119c72cacSJeff Kirsher 		u32 crc;
50219c72cacSJeff Kirsher 
50319c72cacSJeff Kirsher 		for (i = 0; i < 4; i++)
50419c72cacSJeff Kirsher 			hash_table[i] = 0;
50519c72cacSJeff Kirsher 
50619c72cacSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
50719c72cacSJeff Kirsher 			crc = ether_crc_le(6, ha->addr);
50819c72cacSJeff Kirsher 			crc >>= 26;
50919c72cacSJeff Kirsher 			hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
51019c72cacSJeff Kirsher 		}
51119c72cacSJeff Kirsher 		/* Accept filtered multicast */
51219c72cacSJeff Kirsher 		recognise |= ETH_ARC_AFM;
51319c72cacSJeff Kirsher 
51419c72cacSJeff Kirsher 		/* Fill the MAC hash tables with their values */
51519c72cacSJeff Kirsher 		writel((u32)(hash_table[1] << 16 | hash_table[0]),
51619c72cacSJeff Kirsher 					&lp->eth_regs->ethhash0);
51719c72cacSJeff Kirsher 		writel((u32)(hash_table[3] << 16 | hash_table[2]),
51819c72cacSJeff Kirsher 					&lp->eth_regs->ethhash1);
51919c72cacSJeff Kirsher 	}
52019c72cacSJeff Kirsher 
52119c72cacSJeff Kirsher 	spin_lock_irqsave(&lp->lock, flags);
52219c72cacSJeff Kirsher 	writel(recognise, &lp->eth_regs->etharc);
52319c72cacSJeff Kirsher 	spin_unlock_irqrestore(&lp->lock, flags);
52419c72cacSJeff Kirsher }
52519c72cacSJeff Kirsher 
52619c72cacSJeff Kirsher static void korina_tx(struct net_device *dev)
52719c72cacSJeff Kirsher {
52819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
52919c72cacSJeff Kirsher 	struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
53019c72cacSJeff Kirsher 	u32 devcs;
53119c72cacSJeff Kirsher 	u32 dmas;
53219c72cacSJeff Kirsher 
53319c72cacSJeff Kirsher 	spin_lock(&lp->lock);
53419c72cacSJeff Kirsher 
53519c72cacSJeff Kirsher 	/* Process all desc that are done */
53619c72cacSJeff Kirsher 	while (IS_DMA_FINISHED(td->control)) {
53719c72cacSJeff Kirsher 		if (lp->tx_full == 1) {
53819c72cacSJeff Kirsher 			netif_wake_queue(dev);
53919c72cacSJeff Kirsher 			lp->tx_full = 0;
54019c72cacSJeff Kirsher 		}
54119c72cacSJeff Kirsher 
54219c72cacSJeff Kirsher 		devcs = lp->td_ring[lp->tx_next_done].devcs;
54319c72cacSJeff Kirsher 		if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
54419c72cacSJeff Kirsher 				(ETH_TX_FD | ETH_TX_LD)) {
54519c72cacSJeff Kirsher 			dev->stats.tx_errors++;
54619c72cacSJeff Kirsher 			dev->stats.tx_dropped++;
54719c72cacSJeff Kirsher 
54819c72cacSJeff Kirsher 			/* Should never happen */
54919c72cacSJeff Kirsher 			printk(KERN_ERR "%s: split tx ignored\n",
55019c72cacSJeff Kirsher 							dev->name);
55119c72cacSJeff Kirsher 		} else if (devcs & ETH_TX_TOK) {
55219c72cacSJeff Kirsher 			dev->stats.tx_packets++;
55319c72cacSJeff Kirsher 			dev->stats.tx_bytes +=
55419c72cacSJeff Kirsher 					lp->tx_skb[lp->tx_next_done]->len;
55519c72cacSJeff Kirsher 		} else {
55619c72cacSJeff Kirsher 			dev->stats.tx_errors++;
55719c72cacSJeff Kirsher 			dev->stats.tx_dropped++;
55819c72cacSJeff Kirsher 
55919c72cacSJeff Kirsher 			/* Underflow */
56019c72cacSJeff Kirsher 			if (devcs & ETH_TX_UND)
56119c72cacSJeff Kirsher 				dev->stats.tx_fifo_errors++;
56219c72cacSJeff Kirsher 
56319c72cacSJeff Kirsher 			/* Oversized frame */
56419c72cacSJeff Kirsher 			if (devcs & ETH_TX_OF)
56519c72cacSJeff Kirsher 				dev->stats.tx_aborted_errors++;
56619c72cacSJeff Kirsher 
56719c72cacSJeff Kirsher 			/* Excessive deferrals */
56819c72cacSJeff Kirsher 			if (devcs & ETH_TX_ED)
56919c72cacSJeff Kirsher 				dev->stats.tx_carrier_errors++;
57019c72cacSJeff Kirsher 
57119c72cacSJeff Kirsher 			/* Collisions: medium busy */
57219c72cacSJeff Kirsher 			if (devcs & ETH_TX_EC)
57319c72cacSJeff Kirsher 				dev->stats.collisions++;
57419c72cacSJeff Kirsher 
57519c72cacSJeff Kirsher 			/* Late collision */
57619c72cacSJeff Kirsher 			if (devcs & ETH_TX_LC)
57719c72cacSJeff Kirsher 				dev->stats.tx_window_errors++;
57819c72cacSJeff Kirsher 		}
57919c72cacSJeff Kirsher 
58019c72cacSJeff Kirsher 		/* We must always free the original skb */
58119c72cacSJeff Kirsher 		if (lp->tx_skb[lp->tx_next_done]) {
58219c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
58319c72cacSJeff Kirsher 			lp->tx_skb[lp->tx_next_done] = NULL;
58419c72cacSJeff Kirsher 		}
58519c72cacSJeff Kirsher 
58619c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
58719c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
58819c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].link = 0;
58919c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].ca = 0;
59019c72cacSJeff Kirsher 		lp->tx_count--;
59119c72cacSJeff Kirsher 
59219c72cacSJeff Kirsher 		/* Go on to next transmission */
59319c72cacSJeff Kirsher 		lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
59419c72cacSJeff Kirsher 		td = &lp->td_ring[lp->tx_next_done];
59519c72cacSJeff Kirsher 
59619c72cacSJeff Kirsher 	}
59719c72cacSJeff Kirsher 
59819c72cacSJeff Kirsher 	/* Clear the DMA status register */
59919c72cacSJeff Kirsher 	dmas = readl(&lp->tx_dma_regs->dmas);
60019c72cacSJeff Kirsher 	writel(~dmas, &lp->tx_dma_regs->dmas);
60119c72cacSJeff Kirsher 
60219c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) &
60319c72cacSJeff Kirsher 			~(DMA_STAT_FINI | DMA_STAT_ERR),
60419c72cacSJeff Kirsher 			&lp->tx_dma_regs->dmasm);
60519c72cacSJeff Kirsher 
60619c72cacSJeff Kirsher 	spin_unlock(&lp->lock);
60719c72cacSJeff Kirsher }
60819c72cacSJeff Kirsher 
60919c72cacSJeff Kirsher static irqreturn_t
61019c72cacSJeff Kirsher korina_tx_dma_interrupt(int irq, void *dev_id)
61119c72cacSJeff Kirsher {
61219c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
61319c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
61419c72cacSJeff Kirsher 	u32 dmas, dmasm;
61519c72cacSJeff Kirsher 	irqreturn_t retval;
61619c72cacSJeff Kirsher 
61719c72cacSJeff Kirsher 	dmas = readl(&lp->tx_dma_regs->dmas);
61819c72cacSJeff Kirsher 
61919c72cacSJeff Kirsher 	if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
62019c72cacSJeff Kirsher 		dmasm = readl(&lp->tx_dma_regs->dmasm);
62119c72cacSJeff Kirsher 		writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
62219c72cacSJeff Kirsher 				&lp->tx_dma_regs->dmasm);
62319c72cacSJeff Kirsher 
62419c72cacSJeff Kirsher 		korina_tx(dev);
62519c72cacSJeff Kirsher 
62619c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_filled &&
62719c72cacSJeff Kirsher 			(readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
62819c72cacSJeff Kirsher 			writel(CPHYSADDR(&lp->td_ring[lp->tx_chain_head]),
62919c72cacSJeff Kirsher 				&(lp->tx_dma_regs->dmandptr));
63019c72cacSJeff Kirsher 			lp->tx_chain_status = desc_empty;
63119c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
63219c72cacSJeff Kirsher 			dev->trans_start = jiffies;
63319c72cacSJeff Kirsher 		}
63419c72cacSJeff Kirsher 		if (dmas & DMA_STAT_ERR)
63519c72cacSJeff Kirsher 			printk(KERN_ERR "%s: DMA error\n", dev->name);
63619c72cacSJeff Kirsher 
63719c72cacSJeff Kirsher 		retval = IRQ_HANDLED;
63819c72cacSJeff Kirsher 	} else
63919c72cacSJeff Kirsher 		retval = IRQ_NONE;
64019c72cacSJeff Kirsher 
64119c72cacSJeff Kirsher 	return retval;
64219c72cacSJeff Kirsher }
64319c72cacSJeff Kirsher 
64419c72cacSJeff Kirsher 
64519c72cacSJeff Kirsher static void korina_check_media(struct net_device *dev, unsigned int init_media)
64619c72cacSJeff Kirsher {
64719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
64819c72cacSJeff Kirsher 
64919c72cacSJeff Kirsher 	mii_check_media(&lp->mii_if, 0, init_media);
65019c72cacSJeff Kirsher 
65119c72cacSJeff Kirsher 	if (lp->mii_if.full_duplex)
65219c72cacSJeff Kirsher 		writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
65319c72cacSJeff Kirsher 						&lp->eth_regs->ethmac2);
65419c72cacSJeff Kirsher 	else
65519c72cacSJeff Kirsher 		writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
65619c72cacSJeff Kirsher 						&lp->eth_regs->ethmac2);
65719c72cacSJeff Kirsher }
65819c72cacSJeff Kirsher 
65919c72cacSJeff Kirsher static void korina_poll_media(unsigned long data)
66019c72cacSJeff Kirsher {
66119c72cacSJeff Kirsher 	struct net_device *dev = (struct net_device *) data;
66219c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
66319c72cacSJeff Kirsher 
66419c72cacSJeff Kirsher 	korina_check_media(dev, 0);
66519c72cacSJeff Kirsher 	mod_timer(&lp->media_check_timer, jiffies + HZ);
66619c72cacSJeff Kirsher }
66719c72cacSJeff Kirsher 
66819c72cacSJeff Kirsher static void korina_set_carrier(struct mii_if_info *mii)
66919c72cacSJeff Kirsher {
67019c72cacSJeff Kirsher 	if (mii->force_media) {
67119c72cacSJeff Kirsher 		/* autoneg is off: Link is always assumed to be up */
67219c72cacSJeff Kirsher 		if (!netif_carrier_ok(mii->dev))
67319c72cacSJeff Kirsher 			netif_carrier_on(mii->dev);
67419c72cacSJeff Kirsher 	} else  /* Let MMI library update carrier status */
67519c72cacSJeff Kirsher 		korina_check_media(mii->dev, 0);
67619c72cacSJeff Kirsher }
67719c72cacSJeff Kirsher 
67819c72cacSJeff Kirsher static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
67919c72cacSJeff Kirsher {
68019c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
68119c72cacSJeff Kirsher 	struct mii_ioctl_data *data = if_mii(rq);
68219c72cacSJeff Kirsher 	int rc;
68319c72cacSJeff Kirsher 
68419c72cacSJeff Kirsher 	if (!netif_running(dev))
68519c72cacSJeff Kirsher 		return -EINVAL;
68619c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
68719c72cacSJeff Kirsher 	rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
68819c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
68919c72cacSJeff Kirsher 	korina_set_carrier(&lp->mii_if);
69019c72cacSJeff Kirsher 
69119c72cacSJeff Kirsher 	return rc;
69219c72cacSJeff Kirsher }
69319c72cacSJeff Kirsher 
69419c72cacSJeff Kirsher /* ethtool helpers */
69519c72cacSJeff Kirsher static void netdev_get_drvinfo(struct net_device *dev,
69619c72cacSJeff Kirsher 			struct ethtool_drvinfo *info)
69719c72cacSJeff Kirsher {
69819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
69919c72cacSJeff Kirsher 
70019c72cacSJeff Kirsher 	strcpy(info->driver, DRV_NAME);
70119c72cacSJeff Kirsher 	strcpy(info->version, DRV_VERSION);
70219c72cacSJeff Kirsher 	strcpy(info->bus_info, lp->dev->name);
70319c72cacSJeff Kirsher }
70419c72cacSJeff Kirsher 
70519c72cacSJeff Kirsher static int netdev_get_settings(struct net_device *dev, struct ethtool_cmd *cmd)
70619c72cacSJeff Kirsher {
70719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
70819c72cacSJeff Kirsher 	int rc;
70919c72cacSJeff Kirsher 
71019c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
71119c72cacSJeff Kirsher 	rc = mii_ethtool_gset(&lp->mii_if, cmd);
71219c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
71319c72cacSJeff Kirsher 
71419c72cacSJeff Kirsher 	return rc;
71519c72cacSJeff Kirsher }
71619c72cacSJeff Kirsher 
71719c72cacSJeff Kirsher static int netdev_set_settings(struct net_device *dev, struct ethtool_cmd *cmd)
71819c72cacSJeff Kirsher {
71919c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
72019c72cacSJeff Kirsher 	int rc;
72119c72cacSJeff Kirsher 
72219c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
72319c72cacSJeff Kirsher 	rc = mii_ethtool_sset(&lp->mii_if, cmd);
72419c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
72519c72cacSJeff Kirsher 	korina_set_carrier(&lp->mii_if);
72619c72cacSJeff Kirsher 
72719c72cacSJeff Kirsher 	return rc;
72819c72cacSJeff Kirsher }
72919c72cacSJeff Kirsher 
73019c72cacSJeff Kirsher static u32 netdev_get_link(struct net_device *dev)
73119c72cacSJeff Kirsher {
73219c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
73319c72cacSJeff Kirsher 
73419c72cacSJeff Kirsher 	return mii_link_ok(&lp->mii_if);
73519c72cacSJeff Kirsher }
73619c72cacSJeff Kirsher 
73719c72cacSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = {
73819c72cacSJeff Kirsher 	.get_drvinfo            = netdev_get_drvinfo,
73919c72cacSJeff Kirsher 	.get_settings           = netdev_get_settings,
74019c72cacSJeff Kirsher 	.set_settings           = netdev_set_settings,
74119c72cacSJeff Kirsher 	.get_link               = netdev_get_link,
74219c72cacSJeff Kirsher };
74319c72cacSJeff Kirsher 
74419c72cacSJeff Kirsher static int korina_alloc_ring(struct net_device *dev)
74519c72cacSJeff Kirsher {
74619c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
74719c72cacSJeff Kirsher 	struct sk_buff *skb;
74819c72cacSJeff Kirsher 	int i;
74919c72cacSJeff Kirsher 
75019c72cacSJeff Kirsher 	/* Initialize the transmit descriptors */
75119c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_TDS; i++) {
75219c72cacSJeff Kirsher 		lp->td_ring[i].control = DMA_DESC_IOF;
75319c72cacSJeff Kirsher 		lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
75419c72cacSJeff Kirsher 		lp->td_ring[i].ca = 0;
75519c72cacSJeff Kirsher 		lp->td_ring[i].link = 0;
75619c72cacSJeff Kirsher 	}
75719c72cacSJeff Kirsher 	lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
75819c72cacSJeff Kirsher 			lp->tx_full = lp->tx_count = 0;
75919c72cacSJeff Kirsher 	lp->tx_chain_status = desc_empty;
76019c72cacSJeff Kirsher 
76119c72cacSJeff Kirsher 	/* Initialize the receive descriptors */
76219c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_RDS; i++) {
76319c72cacSJeff Kirsher 		skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
76419c72cacSJeff Kirsher 		if (!skb)
76519c72cacSJeff Kirsher 			return -ENOMEM;
76619c72cacSJeff Kirsher 		lp->rx_skb[i] = skb;
76719c72cacSJeff Kirsher 		lp->rd_ring[i].control = DMA_DESC_IOD |
76819c72cacSJeff Kirsher 				DMA_COUNT(KORINA_RBSIZE);
76919c72cacSJeff Kirsher 		lp->rd_ring[i].devcs = 0;
77019c72cacSJeff Kirsher 		lp->rd_ring[i].ca = CPHYSADDR(skb->data);
77119c72cacSJeff Kirsher 		lp->rd_ring[i].link = CPHYSADDR(&lp->rd_ring[i+1]);
77219c72cacSJeff Kirsher 	}
77319c72cacSJeff Kirsher 
77419c72cacSJeff Kirsher 	/* loop back receive descriptors, so the last
77519c72cacSJeff Kirsher 	 * descriptor points to the first one */
77619c72cacSJeff Kirsher 	lp->rd_ring[i - 1].link = CPHYSADDR(&lp->rd_ring[0]);
77719c72cacSJeff Kirsher 	lp->rd_ring[i - 1].control |= DMA_DESC_COD;
77819c72cacSJeff Kirsher 
77919c72cacSJeff Kirsher 	lp->rx_next_done  = 0;
78019c72cacSJeff Kirsher 	lp->rx_chain_head = 0;
78119c72cacSJeff Kirsher 	lp->rx_chain_tail = 0;
78219c72cacSJeff Kirsher 	lp->rx_chain_status = desc_empty;
78319c72cacSJeff Kirsher 
78419c72cacSJeff Kirsher 	return 0;
78519c72cacSJeff Kirsher }
78619c72cacSJeff Kirsher 
78719c72cacSJeff Kirsher static void korina_free_ring(struct net_device *dev)
78819c72cacSJeff Kirsher {
78919c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
79019c72cacSJeff Kirsher 	int i;
79119c72cacSJeff Kirsher 
79219c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_RDS; i++) {
79319c72cacSJeff Kirsher 		lp->rd_ring[i].control = 0;
79419c72cacSJeff Kirsher 		if (lp->rx_skb[i])
79519c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->rx_skb[i]);
79619c72cacSJeff Kirsher 		lp->rx_skb[i] = NULL;
79719c72cacSJeff Kirsher 	}
79819c72cacSJeff Kirsher 
79919c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_TDS; i++) {
80019c72cacSJeff Kirsher 		lp->td_ring[i].control = 0;
80119c72cacSJeff Kirsher 		if (lp->tx_skb[i])
80219c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->tx_skb[i]);
80319c72cacSJeff Kirsher 		lp->tx_skb[i] = NULL;
80419c72cacSJeff Kirsher 	}
80519c72cacSJeff Kirsher }
80619c72cacSJeff Kirsher 
80719c72cacSJeff Kirsher /*
80819c72cacSJeff Kirsher  * Initialize the RC32434 ethernet controller.
80919c72cacSJeff Kirsher  */
81019c72cacSJeff Kirsher static int korina_init(struct net_device *dev)
81119c72cacSJeff Kirsher {
81219c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
81319c72cacSJeff Kirsher 
81419c72cacSJeff Kirsher 	/* Disable DMA */
81519c72cacSJeff Kirsher 	korina_abort_tx(dev);
81619c72cacSJeff Kirsher 	korina_abort_rx(dev);
81719c72cacSJeff Kirsher 
81819c72cacSJeff Kirsher 	/* reset ethernet logic */
81919c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->ethintfc);
82019c72cacSJeff Kirsher 	while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
82119c72cacSJeff Kirsher 		dev->trans_start = jiffies;
82219c72cacSJeff Kirsher 
82319c72cacSJeff Kirsher 	/* Enable Ethernet Interface */
82419c72cacSJeff Kirsher 	writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
82519c72cacSJeff Kirsher 
82619c72cacSJeff Kirsher 	/* Allocate rings */
82719c72cacSJeff Kirsher 	if (korina_alloc_ring(dev)) {
82819c72cacSJeff Kirsher 		printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
82919c72cacSJeff Kirsher 		korina_free_ring(dev);
83019c72cacSJeff Kirsher 		return -ENOMEM;
83119c72cacSJeff Kirsher 	}
83219c72cacSJeff Kirsher 
83319c72cacSJeff Kirsher 	writel(0, &lp->rx_dma_regs->dmas);
83419c72cacSJeff Kirsher 	/* Start Rx DMA */
83519c72cacSJeff Kirsher 	korina_start_rx(lp, &lp->rd_ring[0]);
83619c72cacSJeff Kirsher 
83719c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) &
83819c72cacSJeff Kirsher 			~(DMA_STAT_FINI | DMA_STAT_ERR),
83919c72cacSJeff Kirsher 			&lp->tx_dma_regs->dmasm);
84019c72cacSJeff Kirsher 	writel(readl(&lp->rx_dma_regs->dmasm) &
84119c72cacSJeff Kirsher 			~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
84219c72cacSJeff Kirsher 			&lp->rx_dma_regs->dmasm);
84319c72cacSJeff Kirsher 
84419c72cacSJeff Kirsher 	/* Accept only packets destined for this Ethernet device address */
84519c72cacSJeff Kirsher 	writel(ETH_ARC_AB, &lp->eth_regs->etharc);
84619c72cacSJeff Kirsher 
84719c72cacSJeff Kirsher 	/* Set all Ether station address registers to their initial values */
84819c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
84919c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
85019c72cacSJeff Kirsher 
85119c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
85219c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
85319c72cacSJeff Kirsher 
85419c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
85519c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
85619c72cacSJeff Kirsher 
85719c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
85819c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
85919c72cacSJeff Kirsher 
86019c72cacSJeff Kirsher 
86119c72cacSJeff Kirsher 	/* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
86219c72cacSJeff Kirsher 	writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
86319c72cacSJeff Kirsher 			&lp->eth_regs->ethmac2);
86419c72cacSJeff Kirsher 
86519c72cacSJeff Kirsher 	/* Back to back inter-packet-gap */
86619c72cacSJeff Kirsher 	writel(0x15, &lp->eth_regs->ethipgt);
86719c72cacSJeff Kirsher 	/* Non - Back to back inter-packet-gap */
86819c72cacSJeff Kirsher 	writel(0x12, &lp->eth_regs->ethipgr);
86919c72cacSJeff Kirsher 
87019c72cacSJeff Kirsher 	/* Management Clock Prescaler Divisor
87119c72cacSJeff Kirsher 	 * Clock independent setting */
87219c72cacSJeff Kirsher 	writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
87319c72cacSJeff Kirsher 		       &lp->eth_regs->ethmcp);
87419c72cacSJeff Kirsher 
87519c72cacSJeff Kirsher 	/* don't transmit until fifo contains 48b */
87619c72cacSJeff Kirsher 	writel(48, &lp->eth_regs->ethfifott);
87719c72cacSJeff Kirsher 
87819c72cacSJeff Kirsher 	writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
87919c72cacSJeff Kirsher 
88019c72cacSJeff Kirsher 	napi_enable(&lp->napi);
88119c72cacSJeff Kirsher 	netif_start_queue(dev);
88219c72cacSJeff Kirsher 
88319c72cacSJeff Kirsher 	return 0;
88419c72cacSJeff Kirsher }
88519c72cacSJeff Kirsher 
88619c72cacSJeff Kirsher /*
88719c72cacSJeff Kirsher  * Restart the RC32434 ethernet controller.
88819c72cacSJeff Kirsher  */
88919c72cacSJeff Kirsher static void korina_restart_task(struct work_struct *work)
89019c72cacSJeff Kirsher {
89119c72cacSJeff Kirsher 	struct korina_private *lp = container_of(work,
89219c72cacSJeff Kirsher 			struct korina_private, restart_task);
89319c72cacSJeff Kirsher 	struct net_device *dev = lp->dev;
89419c72cacSJeff Kirsher 
89519c72cacSJeff Kirsher 	/*
89619c72cacSJeff Kirsher 	 * Disable interrupts
89719c72cacSJeff Kirsher 	 */
89819c72cacSJeff Kirsher 	disable_irq(lp->rx_irq);
89919c72cacSJeff Kirsher 	disable_irq(lp->tx_irq);
90019c72cacSJeff Kirsher 	disable_irq(lp->ovr_irq);
90119c72cacSJeff Kirsher 	disable_irq(lp->und_irq);
90219c72cacSJeff Kirsher 
90319c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) |
90419c72cacSJeff Kirsher 				DMA_STAT_FINI | DMA_STAT_ERR,
90519c72cacSJeff Kirsher 				&lp->tx_dma_regs->dmasm);
90619c72cacSJeff Kirsher 	writel(readl(&lp->rx_dma_regs->dmasm) |
90719c72cacSJeff Kirsher 				DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
90819c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmasm);
90919c72cacSJeff Kirsher 
91019c72cacSJeff Kirsher 	korina_free_ring(dev);
91119c72cacSJeff Kirsher 
91219c72cacSJeff Kirsher 	napi_disable(&lp->napi);
91319c72cacSJeff Kirsher 
91419c72cacSJeff Kirsher 	if (korina_init(dev) < 0) {
91519c72cacSJeff Kirsher 		printk(KERN_ERR "%s: cannot restart device\n", dev->name);
91619c72cacSJeff Kirsher 		return;
91719c72cacSJeff Kirsher 	}
91819c72cacSJeff Kirsher 	korina_multicast_list(dev);
91919c72cacSJeff Kirsher 
92019c72cacSJeff Kirsher 	enable_irq(lp->und_irq);
92119c72cacSJeff Kirsher 	enable_irq(lp->ovr_irq);
92219c72cacSJeff Kirsher 	enable_irq(lp->tx_irq);
92319c72cacSJeff Kirsher 	enable_irq(lp->rx_irq);
92419c72cacSJeff Kirsher }
92519c72cacSJeff Kirsher 
92619c72cacSJeff Kirsher static void korina_clear_and_restart(struct net_device *dev, u32 value)
92719c72cacSJeff Kirsher {
92819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
92919c72cacSJeff Kirsher 
93019c72cacSJeff Kirsher 	netif_stop_queue(dev);
93119c72cacSJeff Kirsher 	writel(value, &lp->eth_regs->ethintfc);
93219c72cacSJeff Kirsher 	schedule_work(&lp->restart_task);
93319c72cacSJeff Kirsher }
93419c72cacSJeff Kirsher 
93519c72cacSJeff Kirsher /* Ethernet Tx Underflow interrupt */
93619c72cacSJeff Kirsher static irqreturn_t korina_und_interrupt(int irq, void *dev_id)
93719c72cacSJeff Kirsher {
93819c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
93919c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
94019c72cacSJeff Kirsher 	unsigned int und;
94119c72cacSJeff Kirsher 
94219c72cacSJeff Kirsher 	spin_lock(&lp->lock);
94319c72cacSJeff Kirsher 
94419c72cacSJeff Kirsher 	und = readl(&lp->eth_regs->ethintfc);
94519c72cacSJeff Kirsher 
94619c72cacSJeff Kirsher 	if (und & ETH_INT_FC_UND)
94719c72cacSJeff Kirsher 		korina_clear_and_restart(dev, und & ~ETH_INT_FC_UND);
94819c72cacSJeff Kirsher 
94919c72cacSJeff Kirsher 	spin_unlock(&lp->lock);
95019c72cacSJeff Kirsher 
95119c72cacSJeff Kirsher 	return IRQ_HANDLED;
95219c72cacSJeff Kirsher }
95319c72cacSJeff Kirsher 
95419c72cacSJeff Kirsher static void korina_tx_timeout(struct net_device *dev)
95519c72cacSJeff Kirsher {
95619c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
95719c72cacSJeff Kirsher 
95819c72cacSJeff Kirsher 	schedule_work(&lp->restart_task);
95919c72cacSJeff Kirsher }
96019c72cacSJeff Kirsher 
96119c72cacSJeff Kirsher /* Ethernet Rx Overflow interrupt */
96219c72cacSJeff Kirsher static irqreturn_t
96319c72cacSJeff Kirsher korina_ovr_interrupt(int irq, void *dev_id)
96419c72cacSJeff Kirsher {
96519c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
96619c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
96719c72cacSJeff Kirsher 	unsigned int ovr;
96819c72cacSJeff Kirsher 
96919c72cacSJeff Kirsher 	spin_lock(&lp->lock);
97019c72cacSJeff Kirsher 	ovr = readl(&lp->eth_regs->ethintfc);
97119c72cacSJeff Kirsher 
97219c72cacSJeff Kirsher 	if (ovr & ETH_INT_FC_OVR)
97319c72cacSJeff Kirsher 		korina_clear_and_restart(dev, ovr & ~ETH_INT_FC_OVR);
97419c72cacSJeff Kirsher 
97519c72cacSJeff Kirsher 	spin_unlock(&lp->lock);
97619c72cacSJeff Kirsher 
97719c72cacSJeff Kirsher 	return IRQ_HANDLED;
97819c72cacSJeff Kirsher }
97919c72cacSJeff Kirsher 
98019c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
98119c72cacSJeff Kirsher static void korina_poll_controller(struct net_device *dev)
98219c72cacSJeff Kirsher {
98319c72cacSJeff Kirsher 	disable_irq(dev->irq);
98419c72cacSJeff Kirsher 	korina_tx_dma_interrupt(dev->irq, dev);
98519c72cacSJeff Kirsher 	enable_irq(dev->irq);
98619c72cacSJeff Kirsher }
98719c72cacSJeff Kirsher #endif
98819c72cacSJeff Kirsher 
98919c72cacSJeff Kirsher static int korina_open(struct net_device *dev)
99019c72cacSJeff Kirsher {
99119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
99219c72cacSJeff Kirsher 	int ret;
99319c72cacSJeff Kirsher 
99419c72cacSJeff Kirsher 	/* Initialize */
99519c72cacSJeff Kirsher 	ret = korina_init(dev);
99619c72cacSJeff Kirsher 	if (ret < 0) {
99719c72cacSJeff Kirsher 		printk(KERN_ERR "%s: cannot open device\n", dev->name);
99819c72cacSJeff Kirsher 		goto out;
99919c72cacSJeff Kirsher 	}
100019c72cacSJeff Kirsher 
100119c72cacSJeff Kirsher 	/* Install the interrupt handler
100219c72cacSJeff Kirsher 	 * that handles the Done Finished
100319c72cacSJeff Kirsher 	 * Ovr and Und Events */
100419c72cacSJeff Kirsher 	ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
100519c72cacSJeff Kirsher 			IRQF_DISABLED, "Korina ethernet Rx", dev);
100619c72cacSJeff Kirsher 	if (ret < 0) {
100719c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
100819c72cacSJeff Kirsher 		    dev->name, lp->rx_irq);
100919c72cacSJeff Kirsher 		goto err_release;
101019c72cacSJeff Kirsher 	}
101119c72cacSJeff Kirsher 	ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
101219c72cacSJeff Kirsher 			IRQF_DISABLED, "Korina ethernet Tx", dev);
101319c72cacSJeff Kirsher 	if (ret < 0) {
101419c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
101519c72cacSJeff Kirsher 		    dev->name, lp->tx_irq);
101619c72cacSJeff Kirsher 		goto err_free_rx_irq;
101719c72cacSJeff Kirsher 	}
101819c72cacSJeff Kirsher 
101919c72cacSJeff Kirsher 	/* Install handler for overrun error. */
102019c72cacSJeff Kirsher 	ret = request_irq(lp->ovr_irq, korina_ovr_interrupt,
102119c72cacSJeff Kirsher 			IRQF_DISABLED, "Ethernet Overflow", dev);
102219c72cacSJeff Kirsher 	if (ret < 0) {
102319c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get OVR IRQ %d\n",
102419c72cacSJeff Kirsher 		    dev->name, lp->ovr_irq);
102519c72cacSJeff Kirsher 		goto err_free_tx_irq;
102619c72cacSJeff Kirsher 	}
102719c72cacSJeff Kirsher 
102819c72cacSJeff Kirsher 	/* Install handler for underflow error. */
102919c72cacSJeff Kirsher 	ret = request_irq(lp->und_irq, korina_und_interrupt,
103019c72cacSJeff Kirsher 			IRQF_DISABLED, "Ethernet Underflow", dev);
103119c72cacSJeff Kirsher 	if (ret < 0) {
103219c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get UND IRQ %d\n",
103319c72cacSJeff Kirsher 		    dev->name, lp->und_irq);
103419c72cacSJeff Kirsher 		goto err_free_ovr_irq;
103519c72cacSJeff Kirsher 	}
103619c72cacSJeff Kirsher 	mod_timer(&lp->media_check_timer, jiffies + 1);
103719c72cacSJeff Kirsher out:
103819c72cacSJeff Kirsher 	return ret;
103919c72cacSJeff Kirsher 
104019c72cacSJeff Kirsher err_free_ovr_irq:
104119c72cacSJeff Kirsher 	free_irq(lp->ovr_irq, dev);
104219c72cacSJeff Kirsher err_free_tx_irq:
104319c72cacSJeff Kirsher 	free_irq(lp->tx_irq, dev);
104419c72cacSJeff Kirsher err_free_rx_irq:
104519c72cacSJeff Kirsher 	free_irq(lp->rx_irq, dev);
104619c72cacSJeff Kirsher err_release:
104719c72cacSJeff Kirsher 	korina_free_ring(dev);
104819c72cacSJeff Kirsher 	goto out;
104919c72cacSJeff Kirsher }
105019c72cacSJeff Kirsher 
105119c72cacSJeff Kirsher static int korina_close(struct net_device *dev)
105219c72cacSJeff Kirsher {
105319c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
105419c72cacSJeff Kirsher 	u32 tmp;
105519c72cacSJeff Kirsher 
105619c72cacSJeff Kirsher 	del_timer(&lp->media_check_timer);
105719c72cacSJeff Kirsher 
105819c72cacSJeff Kirsher 	/* Disable interrupts */
105919c72cacSJeff Kirsher 	disable_irq(lp->rx_irq);
106019c72cacSJeff Kirsher 	disable_irq(lp->tx_irq);
106119c72cacSJeff Kirsher 	disable_irq(lp->ovr_irq);
106219c72cacSJeff Kirsher 	disable_irq(lp->und_irq);
106319c72cacSJeff Kirsher 
106419c72cacSJeff Kirsher 	korina_abort_tx(dev);
106519c72cacSJeff Kirsher 	tmp = readl(&lp->tx_dma_regs->dmasm);
106619c72cacSJeff Kirsher 	tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
106719c72cacSJeff Kirsher 	writel(tmp, &lp->tx_dma_regs->dmasm);
106819c72cacSJeff Kirsher 
106919c72cacSJeff Kirsher 	korina_abort_rx(dev);
107019c72cacSJeff Kirsher 	tmp = readl(&lp->rx_dma_regs->dmasm);
107119c72cacSJeff Kirsher 	tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
107219c72cacSJeff Kirsher 	writel(tmp, &lp->rx_dma_regs->dmasm);
107319c72cacSJeff Kirsher 
107419c72cacSJeff Kirsher 	korina_free_ring(dev);
107519c72cacSJeff Kirsher 
107619c72cacSJeff Kirsher 	napi_disable(&lp->napi);
107719c72cacSJeff Kirsher 
107819c72cacSJeff Kirsher 	cancel_work_sync(&lp->restart_task);
107919c72cacSJeff Kirsher 
108019c72cacSJeff Kirsher 	free_irq(lp->rx_irq, dev);
108119c72cacSJeff Kirsher 	free_irq(lp->tx_irq, dev);
108219c72cacSJeff Kirsher 	free_irq(lp->ovr_irq, dev);
108319c72cacSJeff Kirsher 	free_irq(lp->und_irq, dev);
108419c72cacSJeff Kirsher 
108519c72cacSJeff Kirsher 	return 0;
108619c72cacSJeff Kirsher }
108719c72cacSJeff Kirsher 
108819c72cacSJeff Kirsher static const struct net_device_ops korina_netdev_ops = {
108919c72cacSJeff Kirsher 	.ndo_open		= korina_open,
109019c72cacSJeff Kirsher 	.ndo_stop		= korina_close,
109119c72cacSJeff Kirsher 	.ndo_start_xmit		= korina_send_packet,
1092*afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= korina_multicast_list,
109319c72cacSJeff Kirsher 	.ndo_tx_timeout		= korina_tx_timeout,
109419c72cacSJeff Kirsher 	.ndo_do_ioctl		= korina_ioctl,
109519c72cacSJeff Kirsher 	.ndo_change_mtu		= eth_change_mtu,
109619c72cacSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
109719c72cacSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
109819c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
109919c72cacSJeff Kirsher 	.ndo_poll_controller	= korina_poll_controller,
110019c72cacSJeff Kirsher #endif
110119c72cacSJeff Kirsher };
110219c72cacSJeff Kirsher 
110319c72cacSJeff Kirsher static int korina_probe(struct platform_device *pdev)
110419c72cacSJeff Kirsher {
110519c72cacSJeff Kirsher 	struct korina_device *bif = platform_get_drvdata(pdev);
110619c72cacSJeff Kirsher 	struct korina_private *lp;
110719c72cacSJeff Kirsher 	struct net_device *dev;
110819c72cacSJeff Kirsher 	struct resource *r;
110919c72cacSJeff Kirsher 	int rc;
111019c72cacSJeff Kirsher 
111119c72cacSJeff Kirsher 	dev = alloc_etherdev(sizeof(struct korina_private));
111219c72cacSJeff Kirsher 	if (!dev) {
111319c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": alloc_etherdev failed\n");
111419c72cacSJeff Kirsher 		return -ENOMEM;
111519c72cacSJeff Kirsher 	}
111619c72cacSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
111719c72cacSJeff Kirsher 	lp = netdev_priv(dev);
111819c72cacSJeff Kirsher 
111919c72cacSJeff Kirsher 	bif->dev = dev;
112019c72cacSJeff Kirsher 	memcpy(dev->dev_addr, bif->mac, 6);
112119c72cacSJeff Kirsher 
112219c72cacSJeff Kirsher 	lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
112319c72cacSJeff Kirsher 	lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
112419c72cacSJeff Kirsher 	lp->ovr_irq = platform_get_irq_byname(pdev, "korina_ovr");
112519c72cacSJeff Kirsher 	lp->und_irq = platform_get_irq_byname(pdev, "korina_und");
112619c72cacSJeff Kirsher 
112719c72cacSJeff Kirsher 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_regs");
112819c72cacSJeff Kirsher 	dev->base_addr = r->start;
112919c72cacSJeff Kirsher 	lp->eth_regs = ioremap_nocache(r->start, resource_size(r));
113019c72cacSJeff Kirsher 	if (!lp->eth_regs) {
113119c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
113219c72cacSJeff Kirsher 		rc = -ENXIO;
113319c72cacSJeff Kirsher 		goto probe_err_out;
113419c72cacSJeff Kirsher 	}
113519c72cacSJeff Kirsher 
113619c72cacSJeff Kirsher 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_rx");
113719c72cacSJeff Kirsher 	lp->rx_dma_regs = ioremap_nocache(r->start, resource_size(r));
113819c72cacSJeff Kirsher 	if (!lp->rx_dma_regs) {
113919c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
114019c72cacSJeff Kirsher 		rc = -ENXIO;
114119c72cacSJeff Kirsher 		goto probe_err_dma_rx;
114219c72cacSJeff Kirsher 	}
114319c72cacSJeff Kirsher 
114419c72cacSJeff Kirsher 	r = platform_get_resource_byname(pdev, IORESOURCE_MEM, "korina_dma_tx");
114519c72cacSJeff Kirsher 	lp->tx_dma_regs = ioremap_nocache(r->start, resource_size(r));
114619c72cacSJeff Kirsher 	if (!lp->tx_dma_regs) {
114719c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
114819c72cacSJeff Kirsher 		rc = -ENXIO;
114919c72cacSJeff Kirsher 		goto probe_err_dma_tx;
115019c72cacSJeff Kirsher 	}
115119c72cacSJeff Kirsher 
115219c72cacSJeff Kirsher 	lp->td_ring = kmalloc(TD_RING_SIZE + RD_RING_SIZE, GFP_KERNEL);
115319c72cacSJeff Kirsher 	if (!lp->td_ring) {
115419c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot allocate descriptors\n");
115519c72cacSJeff Kirsher 		rc = -ENXIO;
115619c72cacSJeff Kirsher 		goto probe_err_td_ring;
115719c72cacSJeff Kirsher 	}
115819c72cacSJeff Kirsher 
115919c72cacSJeff Kirsher 	dma_cache_inv((unsigned long)(lp->td_ring),
116019c72cacSJeff Kirsher 			TD_RING_SIZE + RD_RING_SIZE);
116119c72cacSJeff Kirsher 
116219c72cacSJeff Kirsher 	/* now convert TD_RING pointer to KSEG1 */
116319c72cacSJeff Kirsher 	lp->td_ring = (struct dma_desc *)KSEG1ADDR(lp->td_ring);
116419c72cacSJeff Kirsher 	lp->rd_ring = &lp->td_ring[KORINA_NUM_TDS];
116519c72cacSJeff Kirsher 
116619c72cacSJeff Kirsher 	spin_lock_init(&lp->lock);
116719c72cacSJeff Kirsher 	/* just use the rx dma irq */
116819c72cacSJeff Kirsher 	dev->irq = lp->rx_irq;
116919c72cacSJeff Kirsher 	lp->dev = dev;
117019c72cacSJeff Kirsher 
117119c72cacSJeff Kirsher 	dev->netdev_ops = &korina_netdev_ops;
117219c72cacSJeff Kirsher 	dev->ethtool_ops = &netdev_ethtool_ops;
117319c72cacSJeff Kirsher 	dev->watchdog_timeo = TX_TIMEOUT;
117419c72cacSJeff Kirsher 	netif_napi_add(dev, &lp->napi, korina_poll, 64);
117519c72cacSJeff Kirsher 
117619c72cacSJeff Kirsher 	lp->phy_addr = (((lp->rx_irq == 0x2c? 1:0) << 8) | 0x05);
117719c72cacSJeff Kirsher 	lp->mii_if.dev = dev;
117819c72cacSJeff Kirsher 	lp->mii_if.mdio_read = mdio_read;
117919c72cacSJeff Kirsher 	lp->mii_if.mdio_write = mdio_write;
118019c72cacSJeff Kirsher 	lp->mii_if.phy_id = lp->phy_addr;
118119c72cacSJeff Kirsher 	lp->mii_if.phy_id_mask = 0x1f;
118219c72cacSJeff Kirsher 	lp->mii_if.reg_num_mask = 0x1f;
118319c72cacSJeff Kirsher 
118419c72cacSJeff Kirsher 	rc = register_netdev(dev);
118519c72cacSJeff Kirsher 	if (rc < 0) {
118619c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME
118719c72cacSJeff Kirsher 			": cannot register net device: %d\n", rc);
118819c72cacSJeff Kirsher 		goto probe_err_register;
118919c72cacSJeff Kirsher 	}
119019c72cacSJeff Kirsher 	setup_timer(&lp->media_check_timer, korina_poll_media, (unsigned long) dev);
119119c72cacSJeff Kirsher 
119219c72cacSJeff Kirsher 	INIT_WORK(&lp->restart_task, korina_restart_task);
119319c72cacSJeff Kirsher 
119419c72cacSJeff Kirsher 	printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
119519c72cacSJeff Kirsher 			dev->name);
119619c72cacSJeff Kirsher out:
119719c72cacSJeff Kirsher 	return rc;
119819c72cacSJeff Kirsher 
119919c72cacSJeff Kirsher probe_err_register:
120019c72cacSJeff Kirsher 	kfree(lp->td_ring);
120119c72cacSJeff Kirsher probe_err_td_ring:
120219c72cacSJeff Kirsher 	iounmap(lp->tx_dma_regs);
120319c72cacSJeff Kirsher probe_err_dma_tx:
120419c72cacSJeff Kirsher 	iounmap(lp->rx_dma_regs);
120519c72cacSJeff Kirsher probe_err_dma_rx:
120619c72cacSJeff Kirsher 	iounmap(lp->eth_regs);
120719c72cacSJeff Kirsher probe_err_out:
120819c72cacSJeff Kirsher 	free_netdev(dev);
120919c72cacSJeff Kirsher 	goto out;
121019c72cacSJeff Kirsher }
121119c72cacSJeff Kirsher 
121219c72cacSJeff Kirsher static int korina_remove(struct platform_device *pdev)
121319c72cacSJeff Kirsher {
121419c72cacSJeff Kirsher 	struct korina_device *bif = platform_get_drvdata(pdev);
121519c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(bif->dev);
121619c72cacSJeff Kirsher 
121719c72cacSJeff Kirsher 	iounmap(lp->eth_regs);
121819c72cacSJeff Kirsher 	iounmap(lp->rx_dma_regs);
121919c72cacSJeff Kirsher 	iounmap(lp->tx_dma_regs);
122019c72cacSJeff Kirsher 
122119c72cacSJeff Kirsher 	platform_set_drvdata(pdev, NULL);
122219c72cacSJeff Kirsher 	unregister_netdev(bif->dev);
122319c72cacSJeff Kirsher 	free_netdev(bif->dev);
122419c72cacSJeff Kirsher 
122519c72cacSJeff Kirsher 	return 0;
122619c72cacSJeff Kirsher }
122719c72cacSJeff Kirsher 
122819c72cacSJeff Kirsher static struct platform_driver korina_driver = {
122919c72cacSJeff Kirsher 	.driver.name = "korina",
123019c72cacSJeff Kirsher 	.probe = korina_probe,
123119c72cacSJeff Kirsher 	.remove = korina_remove,
123219c72cacSJeff Kirsher };
123319c72cacSJeff Kirsher 
123419c72cacSJeff Kirsher static int __init korina_init_module(void)
123519c72cacSJeff Kirsher {
123619c72cacSJeff Kirsher 	return platform_driver_register(&korina_driver);
123719c72cacSJeff Kirsher }
123819c72cacSJeff Kirsher 
123919c72cacSJeff Kirsher static void korina_cleanup_module(void)
124019c72cacSJeff Kirsher {
124119c72cacSJeff Kirsher 	return platform_driver_unregister(&korina_driver);
124219c72cacSJeff Kirsher }
124319c72cacSJeff Kirsher 
124419c72cacSJeff Kirsher module_init(korina_init_module);
124519c72cacSJeff Kirsher module_exit(korina_cleanup_module);
124619c72cacSJeff Kirsher 
124719c72cacSJeff Kirsher MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
124819c72cacSJeff Kirsher MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
124919c72cacSJeff Kirsher MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
125019c72cacSJeff Kirsher MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
125119c72cacSJeff Kirsher MODULE_LICENSE("GPL");
1252