xref: /openbmc/linux/drivers/net/ethernet/korina.c (revision 0fc96939a97ffd4929b19a3e2d1b3858ab9efa8b)
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>
787736fc6SRoman Yeryomin  *  Copyright 2017 Roman Yeryomin <roman@advem.lv>
819c72cacSJeff Kirsher  *
919c72cacSJeff Kirsher  *  This program is free software; you can redistribute  it and/or modify it
1019c72cacSJeff Kirsher  *  under  the terms of  the GNU General  Public License as published by the
1119c72cacSJeff Kirsher  *  Free Software Foundation;  either version 2 of the  License, or (at your
1219c72cacSJeff Kirsher  *  option) any later version.
1319c72cacSJeff Kirsher  *
1419c72cacSJeff Kirsher  *  THIS  SOFTWARE  IS PROVIDED   ``AS  IS'' AND   ANY  EXPRESS OR IMPLIED
1519c72cacSJeff Kirsher  *  WARRANTIES,   INCLUDING, BUT NOT  LIMITED  TO, THE IMPLIED WARRANTIES OF
1619c72cacSJeff Kirsher  *  MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN
1719c72cacSJeff Kirsher  *  NO  EVENT  SHALL   THE AUTHOR  BE    LIABLE FOR ANY   DIRECT, INDIRECT,
1819c72cacSJeff Kirsher  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
1919c72cacSJeff Kirsher  *  NOT LIMITED   TO, PROCUREMENT OF  SUBSTITUTE GOODS  OR SERVICES; LOSS OF
2019c72cacSJeff Kirsher  *  USE, DATA,  OR PROFITS; OR  BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
2119c72cacSJeff Kirsher  *  ANY THEORY OF LIABILITY, WHETHER IN  CONTRACT, STRICT LIABILITY, OR TORT
2219c72cacSJeff Kirsher  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
2319c72cacSJeff Kirsher  *  THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
2419c72cacSJeff Kirsher  *
2519c72cacSJeff Kirsher  *  You should have received a copy of the  GNU General Public License along
2619c72cacSJeff Kirsher  *  with this program; if not, write  to the Free Software Foundation, Inc.,
2719c72cacSJeff Kirsher  *  675 Mass Ave, Cambridge, MA 02139, USA.
2819c72cacSJeff Kirsher  *
2919c72cacSJeff Kirsher  *  Writing to a DMA status register:
3019c72cacSJeff Kirsher  *
3119c72cacSJeff Kirsher  *  When writing to the status register, you should mask the bit you have
3219c72cacSJeff Kirsher  *  been testing the status register with. Both Tx and Rx DMA registers
3319c72cacSJeff Kirsher  *  should stick to this procedure.
3419c72cacSJeff Kirsher  */
3519c72cacSJeff Kirsher 
3619c72cacSJeff Kirsher #include <linux/module.h>
3719c72cacSJeff Kirsher #include <linux/kernel.h>
3819c72cacSJeff Kirsher #include <linux/moduleparam.h>
3919c72cacSJeff Kirsher #include <linux/sched.h>
4019c72cacSJeff Kirsher #include <linux/ctype.h>
4119c72cacSJeff Kirsher #include <linux/types.h>
4219c72cacSJeff Kirsher #include <linux/interrupt.h>
4319c72cacSJeff Kirsher #include <linux/ioport.h>
4489f9d540SThomas Bogendoerfer #include <linux/iopoll.h>
4519c72cacSJeff Kirsher #include <linux/in.h>
4619c72cacSJeff Kirsher #include <linux/slab.h>
4719c72cacSJeff Kirsher #include <linux/string.h>
4819c72cacSJeff Kirsher #include <linux/delay.h>
4919c72cacSJeff Kirsher #include <linux/netdevice.h>
5019c72cacSJeff Kirsher #include <linux/etherdevice.h>
5119c72cacSJeff Kirsher #include <linux/skbuff.h>
5219c72cacSJeff Kirsher #include <linux/errno.h>
5319c72cacSJeff Kirsher #include <linux/platform_device.h>
5419c72cacSJeff Kirsher #include <linux/mii.h>
5519c72cacSJeff Kirsher #include <linux/ethtool.h>
5619c72cacSJeff Kirsher #include <linux/crc32.h>
5765fddcfcSMike Rapoport #include <linux/pgtable.h>
5819c72cacSJeff Kirsher 
5919c72cacSJeff Kirsher #include <asm/bootinfo.h>
6019c72cacSJeff Kirsher #include <asm/bitops.h>
6119c72cacSJeff Kirsher #include <asm/io.h>
6219c72cacSJeff Kirsher #include <asm/dma.h>
6319c72cacSJeff Kirsher 
6419c72cacSJeff Kirsher #include <asm/mach-rc32434/rb.h>
6519c72cacSJeff Kirsher #include <asm/mach-rc32434/rc32434.h>
6619c72cacSJeff Kirsher #include <asm/mach-rc32434/eth.h>
6719c72cacSJeff Kirsher #include <asm/mach-rc32434/dma_v.h>
6819c72cacSJeff Kirsher 
6919c72cacSJeff Kirsher #define DRV_NAME	"korina"
70da1d2defSRoman Yeryomin #define DRV_VERSION	"0.20"
71da1d2defSRoman Yeryomin #define DRV_RELDATE	"15Sep2017"
7219c72cacSJeff Kirsher 
7319c72cacSJeff Kirsher #define STATION_ADDRESS_HIGH(dev) (((dev)->dev_addr[0] << 8) | \
7419c72cacSJeff Kirsher 				   ((dev)->dev_addr[1]))
7519c72cacSJeff Kirsher #define STATION_ADDRESS_LOW(dev)  (((dev)->dev_addr[2] << 24) | \
7619c72cacSJeff Kirsher 				   ((dev)->dev_addr[3] << 16) | \
7719c72cacSJeff Kirsher 				   ((dev)->dev_addr[4] << 8)  | \
7819c72cacSJeff Kirsher 				   ((dev)->dev_addr[5]))
7919c72cacSJeff Kirsher 
8019c72cacSJeff Kirsher #define MII_CLOCK	1250000 /* no more than 2.5MHz */
8119c72cacSJeff Kirsher 
8219c72cacSJeff Kirsher /* the following must be powers of two */
8319c72cacSJeff Kirsher #define KORINA_NUM_RDS	64  /* number of receive descriptors */
8419c72cacSJeff Kirsher #define KORINA_NUM_TDS	64  /* number of transmit descriptors */
8519c72cacSJeff Kirsher 
8619c72cacSJeff Kirsher /* KORINA_RBSIZE is the hardware's default maximum receive
8719c72cacSJeff Kirsher  * frame size in bytes. Having this hardcoded means that there
8819c72cacSJeff Kirsher  * is no support for MTU sizes greater than 1500. */
8919c72cacSJeff Kirsher #define KORINA_RBSIZE	1536 /* size of one resource buffer = Ether MTU */
9019c72cacSJeff Kirsher #define KORINA_RDS_MASK	(KORINA_NUM_RDS - 1)
9119c72cacSJeff Kirsher #define KORINA_TDS_MASK	(KORINA_NUM_TDS - 1)
9219c72cacSJeff Kirsher #define RD_RING_SIZE	(KORINA_NUM_RDS * sizeof(struct dma_desc))
9319c72cacSJeff Kirsher #define TD_RING_SIZE	(KORINA_NUM_TDS * sizeof(struct dma_desc))
9419c72cacSJeff Kirsher 
9519c72cacSJeff Kirsher #define TX_TIMEOUT	(6000 * HZ / 1000)
9619c72cacSJeff Kirsher 
972e5396b1SRoman Yeryomin enum chain_status {
982e5396b1SRoman Yeryomin 	desc_filled,
992e5396b1SRoman Yeryomin 	desc_empty
1002e5396b1SRoman Yeryomin };
1012e5396b1SRoman Yeryomin 
10219c72cacSJeff Kirsher #define IS_DMA_FINISHED(X)	(((X) & (DMA_DESC_FINI)) != 0)
10319c72cacSJeff Kirsher #define IS_DMA_DONE(X)		(((X) & (DMA_DESC_DONE)) != 0)
10419c72cacSJeff Kirsher #define RCVPKT_LENGTH(X)	(((X) & ETH_RX_LEN) >> ETH_RX_LEN_BIT)
10519c72cacSJeff Kirsher 
10619c72cacSJeff Kirsher /* Information that need to be kept for each board. */
10719c72cacSJeff Kirsher struct korina_private {
108b4cd249aSThomas Bogendoerfer 	struct eth_regs __iomem *eth_regs;
109b4cd249aSThomas Bogendoerfer 	struct dma_reg __iomem *rx_dma_regs;
110b4cd249aSThomas Bogendoerfer 	struct dma_reg __iomem *tx_dma_regs;
11119c72cacSJeff Kirsher 	struct dma_desc *td_ring; /* transmit descriptor ring */
11219c72cacSJeff Kirsher 	struct dma_desc *rd_ring; /* receive descriptor ring  */
113*0fc96939SThomas Bogendoerfer 	dma_addr_t td_dma;
114*0fc96939SThomas Bogendoerfer 	dma_addr_t rd_dma;
11519c72cacSJeff Kirsher 
11619c72cacSJeff Kirsher 	struct sk_buff *tx_skb[KORINA_NUM_TDS];
11719c72cacSJeff Kirsher 	struct sk_buff *rx_skb[KORINA_NUM_RDS];
11819c72cacSJeff Kirsher 
119*0fc96939SThomas Bogendoerfer 	dma_addr_t rx_skb_dma[KORINA_NUM_RDS];
120*0fc96939SThomas Bogendoerfer 	dma_addr_t tx_skb_dma[KORINA_NUM_TDS];
121*0fc96939SThomas Bogendoerfer 
12219c72cacSJeff Kirsher 	int rx_next_done;
12319c72cacSJeff Kirsher 	int rx_chain_head;
12419c72cacSJeff Kirsher 	int rx_chain_tail;
12519c72cacSJeff Kirsher 	enum chain_status rx_chain_status;
12619c72cacSJeff Kirsher 
12719c72cacSJeff Kirsher 	int tx_next_done;
12819c72cacSJeff Kirsher 	int tx_chain_head;
12919c72cacSJeff Kirsher 	int tx_chain_tail;
13019c72cacSJeff Kirsher 	enum chain_status tx_chain_status;
13119c72cacSJeff Kirsher 	int tx_count;
13219c72cacSJeff Kirsher 	int tx_full;
13319c72cacSJeff Kirsher 
13419c72cacSJeff Kirsher 	int rx_irq;
13519c72cacSJeff Kirsher 	int tx_irq;
13619c72cacSJeff Kirsher 
13719c72cacSJeff Kirsher 	spinlock_t lock;	/* NIC xmit lock */
13819c72cacSJeff Kirsher 
13919c72cacSJeff Kirsher 	int dma_halt_cnt;
14019c72cacSJeff Kirsher 	int dma_run_cnt;
14119c72cacSJeff Kirsher 	struct napi_struct napi;
14219c72cacSJeff Kirsher 	struct timer_list media_check_timer;
14319c72cacSJeff Kirsher 	struct mii_if_info mii_if;
14419c72cacSJeff Kirsher 	struct work_struct restart_task;
14519c72cacSJeff Kirsher 	struct net_device *dev;
146*0fc96939SThomas Bogendoerfer 	struct device *dmadev;
14719c72cacSJeff Kirsher };
14819c72cacSJeff Kirsher 
14919c72cacSJeff Kirsher extern unsigned int idt_cpu_freq;
15019c72cacSJeff Kirsher 
151*0fc96939SThomas Bogendoerfer static dma_addr_t korina_tx_dma(struct korina_private *lp, int idx)
152*0fc96939SThomas Bogendoerfer {
153*0fc96939SThomas Bogendoerfer 	return lp->td_dma + (idx * sizeof(struct dma_desc));
154*0fc96939SThomas Bogendoerfer }
155*0fc96939SThomas Bogendoerfer 
156*0fc96939SThomas Bogendoerfer static dma_addr_t korina_rx_dma(struct korina_private *lp, int idx)
157*0fc96939SThomas Bogendoerfer {
158*0fc96939SThomas Bogendoerfer 	return lp->rd_dma + (idx * sizeof(struct dma_desc));
159*0fc96939SThomas Bogendoerfer }
160*0fc96939SThomas Bogendoerfer 
16119c72cacSJeff Kirsher static inline void korina_abort_dma(struct net_device *dev,
16219c72cacSJeff Kirsher 					struct dma_reg *ch)
16319c72cacSJeff Kirsher {
16419c72cacSJeff Kirsher 	if (readl(&ch->dmac) & DMA_CHAN_RUN_BIT) {
16519c72cacSJeff Kirsher 		writel(0x10, &ch->dmac);
16619c72cacSJeff Kirsher 
16719c72cacSJeff Kirsher 		while (!(readl(&ch->dmas) & DMA_STAT_HALT))
168860e9538SFlorian Westphal 			netif_trans_update(dev);
16919c72cacSJeff Kirsher 
17019c72cacSJeff Kirsher 		writel(0, &ch->dmas);
17119c72cacSJeff Kirsher 	}
17219c72cacSJeff Kirsher 
17319c72cacSJeff Kirsher 	writel(0, &ch->dmadptr);
17419c72cacSJeff Kirsher 	writel(0, &ch->dmandptr);
17519c72cacSJeff Kirsher }
17619c72cacSJeff Kirsher 
17719c72cacSJeff Kirsher static void korina_abort_tx(struct net_device *dev)
17819c72cacSJeff Kirsher {
17919c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
18019c72cacSJeff Kirsher 
18119c72cacSJeff Kirsher 	korina_abort_dma(dev, lp->tx_dma_regs);
18219c72cacSJeff Kirsher }
18319c72cacSJeff Kirsher 
18419c72cacSJeff Kirsher static void korina_abort_rx(struct net_device *dev)
18519c72cacSJeff Kirsher {
18619c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
18719c72cacSJeff Kirsher 
18819c72cacSJeff Kirsher 	korina_abort_dma(dev, lp->rx_dma_regs);
18919c72cacSJeff Kirsher }
19019c72cacSJeff Kirsher 
19119c72cacSJeff Kirsher /* transmit packet */
19219c72cacSJeff Kirsher static int korina_send_packet(struct sk_buff *skb, struct net_device *dev)
19319c72cacSJeff Kirsher {
19419c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
19519c72cacSJeff Kirsher 	u32 chain_prev, chain_next;
196*0fc96939SThomas Bogendoerfer 	unsigned long flags;
19719c72cacSJeff Kirsher 	struct dma_desc *td;
198*0fc96939SThomas Bogendoerfer 	dma_addr_t ca;
199*0fc96939SThomas Bogendoerfer 	u32 length;
200*0fc96939SThomas Bogendoerfer 	int idx;
20119c72cacSJeff Kirsher 
20219c72cacSJeff Kirsher 	spin_lock_irqsave(&lp->lock, flags);
20319c72cacSJeff Kirsher 
204*0fc96939SThomas Bogendoerfer 	idx = lp->tx_chain_tail;
205*0fc96939SThomas Bogendoerfer 	td = &lp->td_ring[idx];
20619c72cacSJeff Kirsher 
20719c72cacSJeff Kirsher 	/* stop queue when full, drop pkts if queue already full */
20819c72cacSJeff Kirsher 	if (lp->tx_count >= (KORINA_NUM_TDS - 2)) {
20919c72cacSJeff Kirsher 		lp->tx_full = 1;
21019c72cacSJeff Kirsher 
21119c72cacSJeff Kirsher 		if (lp->tx_count == (KORINA_NUM_TDS - 2))
21219c72cacSJeff Kirsher 			netif_stop_queue(dev);
213*0fc96939SThomas Bogendoerfer 		else
214*0fc96939SThomas Bogendoerfer 			goto drop_packet;
21519c72cacSJeff Kirsher 	}
21619c72cacSJeff Kirsher 
21719c72cacSJeff Kirsher 	lp->tx_count++;
21819c72cacSJeff Kirsher 
219*0fc96939SThomas Bogendoerfer 	lp->tx_skb[idx] = skb;
22019c72cacSJeff Kirsher 
22119c72cacSJeff Kirsher 	length = skb->len;
22219c72cacSJeff Kirsher 
22319c72cacSJeff Kirsher 	/* Setup the transmit descriptor. */
224*0fc96939SThomas Bogendoerfer 	ca = dma_map_single(lp->dmadev, skb->data, length, DMA_TO_DEVICE);
225*0fc96939SThomas Bogendoerfer 	if (dma_mapping_error(lp->dmadev, ca))
226*0fc96939SThomas Bogendoerfer 		goto drop_packet;
227*0fc96939SThomas Bogendoerfer 
228*0fc96939SThomas Bogendoerfer 	lp->tx_skb_dma[idx] = ca;
229*0fc96939SThomas Bogendoerfer 	td->ca = ca;
230*0fc96939SThomas Bogendoerfer 
231*0fc96939SThomas Bogendoerfer 	chain_prev = (idx - 1) & KORINA_TDS_MASK;
232*0fc96939SThomas Bogendoerfer 	chain_next = (idx + 1) & KORINA_TDS_MASK;
23319c72cacSJeff Kirsher 
23419c72cacSJeff Kirsher 	if (readl(&(lp->tx_dma_regs->dmandptr)) == 0) {
23519c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_empty) {
23619c72cacSJeff Kirsher 			/* Update tail */
23719c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
23819c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
23919c72cacSJeff Kirsher 			/* Move tail */
24019c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
24119c72cacSJeff Kirsher 			/* Write to NDPTR */
242*0fc96939SThomas Bogendoerfer 			writel(korina_tx_dma(lp, lp->tx_chain_head),
24319c72cacSJeff Kirsher 			       &lp->tx_dma_regs->dmandptr);
24419c72cacSJeff Kirsher 			/* Move head to tail */
24519c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
24619c72cacSJeff Kirsher 		} else {
24719c72cacSJeff Kirsher 			/* Update tail */
24819c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
24919c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
25019c72cacSJeff Kirsher 			/* Link to prev */
25119c72cacSJeff Kirsher 			lp->td_ring[chain_prev].control &=
25219c72cacSJeff Kirsher 					~DMA_DESC_COF;
25319c72cacSJeff Kirsher 			/* Link to prev */
254*0fc96939SThomas Bogendoerfer 			lp->td_ring[chain_prev].link = korina_tx_dma(lp, idx);
25519c72cacSJeff Kirsher 			/* Move tail */
25619c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
25719c72cacSJeff Kirsher 			/* Write to NDPTR */
258*0fc96939SThomas Bogendoerfer 			writel(korina_tx_dma(lp, lp->tx_chain_head),
259*0fc96939SThomas Bogendoerfer 			       &lp->tx_dma_regs->dmandptr);
26019c72cacSJeff Kirsher 			/* Move head to tail */
26119c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
26219c72cacSJeff Kirsher 			lp->tx_chain_status = desc_empty;
26319c72cacSJeff Kirsher 		}
26419c72cacSJeff Kirsher 	} else {
26519c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_empty) {
26619c72cacSJeff Kirsher 			/* Update tail */
26719c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
26819c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
26919c72cacSJeff Kirsher 			/* Move tail */
27019c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
27119c72cacSJeff Kirsher 			lp->tx_chain_status = desc_filled;
27219c72cacSJeff Kirsher 		} else {
27319c72cacSJeff Kirsher 			/* Update tail */
27419c72cacSJeff Kirsher 			td->control = DMA_COUNT(length) |
27519c72cacSJeff Kirsher 					DMA_DESC_COF | DMA_DESC_IOF;
27619c72cacSJeff Kirsher 			lp->td_ring[chain_prev].control &=
27719c72cacSJeff Kirsher 					~DMA_DESC_COF;
278*0fc96939SThomas Bogendoerfer 			lp->td_ring[chain_prev].link = korina_tx_dma(lp, idx);
27919c72cacSJeff Kirsher 			lp->tx_chain_tail = chain_next;
28019c72cacSJeff Kirsher 		}
28119c72cacSJeff Kirsher 	}
28219c72cacSJeff Kirsher 
283860e9538SFlorian Westphal 	netif_trans_update(dev);
28419c72cacSJeff Kirsher 	spin_unlock_irqrestore(&lp->lock, flags);
28519c72cacSJeff Kirsher 
28619c72cacSJeff Kirsher 	return NETDEV_TX_OK;
287*0fc96939SThomas Bogendoerfer 
288*0fc96939SThomas Bogendoerfer drop_packet:
289*0fc96939SThomas Bogendoerfer 	dev->stats.tx_dropped++;
290*0fc96939SThomas Bogendoerfer 	dev_kfree_skb_any(skb);
291*0fc96939SThomas Bogendoerfer 	spin_unlock_irqrestore(&lp->lock, flags);
292*0fc96939SThomas Bogendoerfer 
293*0fc96939SThomas Bogendoerfer 	return NETDEV_TX_OK;
29419c72cacSJeff Kirsher }
29519c72cacSJeff Kirsher 
29689f9d540SThomas Bogendoerfer static int korina_mdio_wait(struct korina_private *lp)
29789f9d540SThomas Bogendoerfer {
29889f9d540SThomas Bogendoerfer 	u32 value;
29989f9d540SThomas Bogendoerfer 
30089f9d540SThomas Bogendoerfer 	return readl_poll_timeout_atomic(&lp->eth_regs->miimind,
30189f9d540SThomas Bogendoerfer 					 value, value & ETH_MII_IND_BSY,
30289f9d540SThomas Bogendoerfer 					 1, 1000);
30389f9d540SThomas Bogendoerfer }
30489f9d540SThomas Bogendoerfer 
30589f9d540SThomas Bogendoerfer static int korina_mdio_read(struct net_device *dev, int phy, int reg)
30619c72cacSJeff Kirsher {
30719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
30819c72cacSJeff Kirsher 	int ret;
30919c72cacSJeff Kirsher 
31089f9d540SThomas Bogendoerfer 	ret = korina_mdio_wait(lp);
31189f9d540SThomas Bogendoerfer 	if (ret < 0)
31289f9d540SThomas Bogendoerfer 		return ret;
31319c72cacSJeff Kirsher 
31489f9d540SThomas Bogendoerfer 	writel(phy << 8 | reg, &lp->eth_regs->miimaddr);
31589f9d540SThomas Bogendoerfer 	writel(1, &lp->eth_regs->miimcmd);
31689f9d540SThomas Bogendoerfer 
31789f9d540SThomas Bogendoerfer 	ret = korina_mdio_wait(lp);
31889f9d540SThomas Bogendoerfer 	if (ret < 0)
31989f9d540SThomas Bogendoerfer 		return ret;
32089f9d540SThomas Bogendoerfer 
32189f9d540SThomas Bogendoerfer 	if (readl(&lp->eth_regs->miimind) & ETH_MII_IND_NV)
32289f9d540SThomas Bogendoerfer 		return -EINVAL;
32389f9d540SThomas Bogendoerfer 
32489f9d540SThomas Bogendoerfer 	ret = readl(&lp->eth_regs->miimrdd);
32519c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->miimcmd);
32619c72cacSJeff Kirsher 	return ret;
32719c72cacSJeff Kirsher }
32819c72cacSJeff Kirsher 
32989f9d540SThomas Bogendoerfer static void korina_mdio_write(struct net_device *dev, int phy, int reg, int val)
33019c72cacSJeff Kirsher {
33119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
33219c72cacSJeff Kirsher 
33389f9d540SThomas Bogendoerfer 	if (korina_mdio_wait(lp))
33489f9d540SThomas Bogendoerfer 		return;
33519c72cacSJeff Kirsher 
33689f9d540SThomas Bogendoerfer 	writel(0, &lp->eth_regs->miimcmd);
33789f9d540SThomas Bogendoerfer 	writel(phy << 8 | reg, &lp->eth_regs->miimaddr);
33819c72cacSJeff Kirsher 	writel(val, &lp->eth_regs->miimwtd);
33919c72cacSJeff Kirsher }
34019c72cacSJeff Kirsher 
34119c72cacSJeff Kirsher /* Ethernet Rx DMA interrupt */
34219c72cacSJeff Kirsher static irqreturn_t korina_rx_dma_interrupt(int irq, void *dev_id)
34319c72cacSJeff Kirsher {
34419c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
34519c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
34619c72cacSJeff Kirsher 	u32 dmas, dmasm;
34719c72cacSJeff Kirsher 	irqreturn_t retval;
34819c72cacSJeff Kirsher 
34919c72cacSJeff Kirsher 	dmas = readl(&lp->rx_dma_regs->dmas);
35019c72cacSJeff Kirsher 	if (dmas & (DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR)) {
35119c72cacSJeff Kirsher 		dmasm = readl(&lp->rx_dma_regs->dmasm);
35219c72cacSJeff Kirsher 		writel(dmasm | (DMA_STAT_DONE |
35319c72cacSJeff Kirsher 				DMA_STAT_HALT | DMA_STAT_ERR),
35419c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmasm);
35519c72cacSJeff Kirsher 
35619c72cacSJeff Kirsher 		napi_schedule(&lp->napi);
35719c72cacSJeff Kirsher 
35819c72cacSJeff Kirsher 		if (dmas & DMA_STAT_ERR)
35919c72cacSJeff Kirsher 			printk(KERN_ERR "%s: DMA error\n", dev->name);
36019c72cacSJeff Kirsher 
36119c72cacSJeff Kirsher 		retval = IRQ_HANDLED;
36219c72cacSJeff Kirsher 	} else
36319c72cacSJeff Kirsher 		retval = IRQ_NONE;
36419c72cacSJeff Kirsher 
36519c72cacSJeff Kirsher 	return retval;
36619c72cacSJeff Kirsher }
36719c72cacSJeff Kirsher 
36819c72cacSJeff Kirsher static int korina_rx(struct net_device *dev, int limit)
36919c72cacSJeff Kirsher {
37019c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
37119c72cacSJeff Kirsher 	struct dma_desc *rd = &lp->rd_ring[lp->rx_next_done];
37219c72cacSJeff Kirsher 	struct sk_buff *skb, *skb_new;
37319c72cacSJeff Kirsher 	u32 devcs, pkt_len, dmas;
374*0fc96939SThomas Bogendoerfer 	dma_addr_t ca;
37519c72cacSJeff Kirsher 	int count;
37619c72cacSJeff Kirsher 
37719c72cacSJeff Kirsher 	for (count = 0; count < limit; count++) {
37819c72cacSJeff Kirsher 		skb = lp->rx_skb[lp->rx_next_done];
37919c72cacSJeff Kirsher 		skb_new = NULL;
38019c72cacSJeff Kirsher 
38119c72cacSJeff Kirsher 		devcs = rd->devcs;
38219c72cacSJeff Kirsher 
38319c72cacSJeff Kirsher 		if ((KORINA_RBSIZE - (u32)DMA_COUNT(rd->control)) == 0)
38419c72cacSJeff Kirsher 			break;
38519c72cacSJeff Kirsher 
386364a97f5SRoman Yeryomin 		/* check that this is a whole packet
387364a97f5SRoman Yeryomin 		 * WARNING: DMA_FD bit incorrectly set
388364a97f5SRoman Yeryomin 		 * in Rc32434 (errata ref #077) */
389364a97f5SRoman Yeryomin 		if (!(devcs & ETH_RX_LD))
390364a97f5SRoman Yeryomin 			goto next;
391364a97f5SRoman Yeryomin 
392364a97f5SRoman Yeryomin 		if (!(devcs & ETH_RX_ROK)) {
39319c72cacSJeff Kirsher 			/* Update statistics counters */
394364a97f5SRoman Yeryomin 			dev->stats.rx_errors++;
395364a97f5SRoman Yeryomin 			dev->stats.rx_dropped++;
39619c72cacSJeff Kirsher 			if (devcs & ETH_RX_CRC)
39719c72cacSJeff Kirsher 				dev->stats.rx_crc_errors++;
39819c72cacSJeff Kirsher 			if (devcs & ETH_RX_LE)
39919c72cacSJeff Kirsher 				dev->stats.rx_length_errors++;
40019c72cacSJeff Kirsher 			if (devcs & ETH_RX_OVR)
40119c72cacSJeff Kirsher 				dev->stats.rx_fifo_errors++;
40219c72cacSJeff Kirsher 			if (devcs & ETH_RX_CV)
40319c72cacSJeff Kirsher 				dev->stats.rx_frame_errors++;
40419c72cacSJeff Kirsher 			if (devcs & ETH_RX_CES)
405364a97f5SRoman Yeryomin 				dev->stats.rx_frame_errors++;
40619c72cacSJeff Kirsher 
407364a97f5SRoman Yeryomin 			goto next;
408364a97f5SRoman Yeryomin 		}
409364a97f5SRoman Yeryomin 
41019c72cacSJeff Kirsher 		/* Malloc up new buffer. */
41119c72cacSJeff Kirsher 		skb_new = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
41219c72cacSJeff Kirsher 		if (!skb_new)
41319c72cacSJeff Kirsher 			break;
414*0fc96939SThomas Bogendoerfer 
415*0fc96939SThomas Bogendoerfer 		ca = dma_map_single(lp->dmadev, skb_new->data, KORINA_RBSIZE,
416*0fc96939SThomas Bogendoerfer 				    DMA_FROM_DEVICE);
417*0fc96939SThomas Bogendoerfer 		if (dma_mapping_error(lp->dmadev, ca)) {
418*0fc96939SThomas Bogendoerfer 			dev_kfree_skb_any(skb_new);
419*0fc96939SThomas Bogendoerfer 			break;
420*0fc96939SThomas Bogendoerfer 		}
421*0fc96939SThomas Bogendoerfer 
422*0fc96939SThomas Bogendoerfer 		pkt_len = RCVPKT_LENGTH(devcs);
423*0fc96939SThomas Bogendoerfer 		dma_unmap_single(lp->dmadev, lp->rx_skb_dma[lp->rx_next_done],
424*0fc96939SThomas Bogendoerfer 				 pkt_len, DMA_FROM_DEVICE);
425*0fc96939SThomas Bogendoerfer 
42619c72cacSJeff Kirsher 		/* Do not count the CRC */
42719c72cacSJeff Kirsher 		skb_put(skb, pkt_len - 4);
42819c72cacSJeff Kirsher 		skb->protocol = eth_type_trans(skb, dev);
42919c72cacSJeff Kirsher 
43019c72cacSJeff Kirsher 		/* Pass the packet to upper layers */
431247c78f2SRoman Yeryomin 		napi_gro_receive(&lp->napi, skb);
43219c72cacSJeff Kirsher 		dev->stats.rx_packets++;
43319c72cacSJeff Kirsher 		dev->stats.rx_bytes += pkt_len;
43419c72cacSJeff Kirsher 
43519c72cacSJeff Kirsher 		/* Update the mcast stats */
43619c72cacSJeff Kirsher 		if (devcs & ETH_RX_MP)
43719c72cacSJeff Kirsher 			dev->stats.multicast++;
43819c72cacSJeff Kirsher 
43919c72cacSJeff Kirsher 		lp->rx_skb[lp->rx_next_done] = skb_new;
440*0fc96939SThomas Bogendoerfer 		lp->rx_skb_dma[lp->rx_next_done] = ca;
44119c72cacSJeff Kirsher 
442364a97f5SRoman Yeryomin next:
44319c72cacSJeff Kirsher 		rd->devcs = 0;
44419c72cacSJeff Kirsher 
44519c72cacSJeff Kirsher 		/* Restore descriptor's curr_addr */
446*0fc96939SThomas Bogendoerfer 		rd->ca = lp->rx_skb_dma[lp->rx_next_done];
44719c72cacSJeff Kirsher 
44819c72cacSJeff Kirsher 		rd->control = DMA_COUNT(KORINA_RBSIZE) |
44919c72cacSJeff Kirsher 			DMA_DESC_COD | DMA_DESC_IOD;
45019c72cacSJeff Kirsher 		lp->rd_ring[(lp->rx_next_done - 1) &
45119c72cacSJeff Kirsher 			KORINA_RDS_MASK].control &=
45219c72cacSJeff Kirsher 			~DMA_DESC_COD;
45319c72cacSJeff Kirsher 
45419c72cacSJeff Kirsher 		lp->rx_next_done = (lp->rx_next_done + 1) & KORINA_RDS_MASK;
45519c72cacSJeff Kirsher 		rd = &lp->rd_ring[lp->rx_next_done];
45619c72cacSJeff Kirsher 		writel(~DMA_STAT_DONE, &lp->rx_dma_regs->dmas);
45719c72cacSJeff Kirsher 	}
45819c72cacSJeff Kirsher 
45919c72cacSJeff Kirsher 	dmas = readl(&lp->rx_dma_regs->dmas);
46019c72cacSJeff Kirsher 
46119c72cacSJeff Kirsher 	if (dmas & DMA_STAT_HALT) {
46219c72cacSJeff Kirsher 		writel(~(DMA_STAT_HALT | DMA_STAT_ERR),
46319c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmas);
46419c72cacSJeff Kirsher 
46519c72cacSJeff Kirsher 		lp->dma_halt_cnt++;
46619c72cacSJeff Kirsher 		rd->devcs = 0;
467*0fc96939SThomas Bogendoerfer 		rd->ca = lp->rx_skb_dma[lp->rx_next_done];
468*0fc96939SThomas Bogendoerfer 		writel(korina_rx_dma(lp, rd - lp->rd_ring),
469*0fc96939SThomas Bogendoerfer 		       &lp->rx_dma_regs->dmandptr);
47019c72cacSJeff Kirsher 	}
47119c72cacSJeff Kirsher 
47219c72cacSJeff Kirsher 	return count;
47319c72cacSJeff Kirsher }
47419c72cacSJeff Kirsher 
47519c72cacSJeff Kirsher static int korina_poll(struct napi_struct *napi, int budget)
47619c72cacSJeff Kirsher {
47719c72cacSJeff Kirsher 	struct korina_private *lp =
47819c72cacSJeff Kirsher 		container_of(napi, struct korina_private, napi);
47919c72cacSJeff Kirsher 	struct net_device *dev = lp->dev;
48019c72cacSJeff Kirsher 	int work_done;
48119c72cacSJeff Kirsher 
48219c72cacSJeff Kirsher 	work_done = korina_rx(dev, budget);
48319c72cacSJeff Kirsher 	if (work_done < budget) {
4846ad20165SEric Dumazet 		napi_complete_done(napi, work_done);
48519c72cacSJeff Kirsher 
48619c72cacSJeff Kirsher 		writel(readl(&lp->rx_dma_regs->dmasm) &
48719c72cacSJeff Kirsher 			~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
48819c72cacSJeff Kirsher 			&lp->rx_dma_regs->dmasm);
48919c72cacSJeff Kirsher 	}
49019c72cacSJeff Kirsher 	return work_done;
49119c72cacSJeff Kirsher }
49219c72cacSJeff Kirsher 
49319c72cacSJeff Kirsher /*
49419c72cacSJeff Kirsher  * Set or clear the multicast filter for this adaptor.
49519c72cacSJeff Kirsher  */
49619c72cacSJeff Kirsher static void korina_multicast_list(struct net_device *dev)
49719c72cacSJeff Kirsher {
49819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
49919c72cacSJeff Kirsher 	unsigned long flags;
50019c72cacSJeff Kirsher 	struct netdev_hw_addr *ha;
50119c72cacSJeff Kirsher 	u32 recognise = ETH_ARC_AB;	/* always accept broadcasts */
50219c72cacSJeff Kirsher 
50319c72cacSJeff Kirsher 	/* Set promiscuous mode */
50419c72cacSJeff Kirsher 	if (dev->flags & IFF_PROMISC)
50519c72cacSJeff Kirsher 		recognise |= ETH_ARC_PRO;
50619c72cacSJeff Kirsher 
50719c72cacSJeff Kirsher 	else if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 4))
50819c72cacSJeff Kirsher 		/* All multicast and broadcast */
50919c72cacSJeff Kirsher 		recognise |= ETH_ARC_AM;
51019c72cacSJeff Kirsher 
51119c72cacSJeff Kirsher 	/* Build the hash table */
51219c72cacSJeff Kirsher 	if (netdev_mc_count(dev) > 4) {
513e998fd41SEmilio López 		u16 hash_table[4] = { 0 };
51419c72cacSJeff Kirsher 		u32 crc;
51519c72cacSJeff Kirsher 
51619c72cacSJeff Kirsher 		netdev_for_each_mc_addr(ha, dev) {
51719c72cacSJeff Kirsher 			crc = ether_crc_le(6, ha->addr);
51819c72cacSJeff Kirsher 			crc >>= 26;
51919c72cacSJeff Kirsher 			hash_table[crc >> 4] |= 1 << (15 - (crc & 0xf));
52019c72cacSJeff Kirsher 		}
52119c72cacSJeff Kirsher 		/* Accept filtered multicast */
52219c72cacSJeff Kirsher 		recognise |= ETH_ARC_AFM;
52319c72cacSJeff Kirsher 
52419c72cacSJeff Kirsher 		/* Fill the MAC hash tables with their values */
52519c72cacSJeff Kirsher 		writel((u32)(hash_table[1] << 16 | hash_table[0]),
52619c72cacSJeff Kirsher 					&lp->eth_regs->ethhash0);
52719c72cacSJeff Kirsher 		writel((u32)(hash_table[3] << 16 | hash_table[2]),
52819c72cacSJeff Kirsher 					&lp->eth_regs->ethhash1);
52919c72cacSJeff Kirsher 	}
53019c72cacSJeff Kirsher 
53119c72cacSJeff Kirsher 	spin_lock_irqsave(&lp->lock, flags);
53219c72cacSJeff Kirsher 	writel(recognise, &lp->eth_regs->etharc);
53319c72cacSJeff Kirsher 	spin_unlock_irqrestore(&lp->lock, flags);
53419c72cacSJeff Kirsher }
53519c72cacSJeff Kirsher 
53619c72cacSJeff Kirsher static void korina_tx(struct net_device *dev)
53719c72cacSJeff Kirsher {
53819c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
53919c72cacSJeff Kirsher 	struct dma_desc *td = &lp->td_ring[lp->tx_next_done];
54019c72cacSJeff Kirsher 	u32 devcs;
54119c72cacSJeff Kirsher 	u32 dmas;
54219c72cacSJeff Kirsher 
54319c72cacSJeff Kirsher 	spin_lock(&lp->lock);
54419c72cacSJeff Kirsher 
54519c72cacSJeff Kirsher 	/* Process all desc that are done */
54619c72cacSJeff Kirsher 	while (IS_DMA_FINISHED(td->control)) {
54719c72cacSJeff Kirsher 		if (lp->tx_full == 1) {
54819c72cacSJeff Kirsher 			netif_wake_queue(dev);
54919c72cacSJeff Kirsher 			lp->tx_full = 0;
55019c72cacSJeff Kirsher 		}
55119c72cacSJeff Kirsher 
55219c72cacSJeff Kirsher 		devcs = lp->td_ring[lp->tx_next_done].devcs;
55319c72cacSJeff Kirsher 		if ((devcs & (ETH_TX_FD | ETH_TX_LD)) !=
55419c72cacSJeff Kirsher 				(ETH_TX_FD | ETH_TX_LD)) {
55519c72cacSJeff Kirsher 			dev->stats.tx_errors++;
55619c72cacSJeff Kirsher 			dev->stats.tx_dropped++;
55719c72cacSJeff Kirsher 
55819c72cacSJeff Kirsher 			/* Should never happen */
55919c72cacSJeff Kirsher 			printk(KERN_ERR "%s: split tx ignored\n",
56019c72cacSJeff Kirsher 							dev->name);
56119c72cacSJeff Kirsher 		} else if (devcs & ETH_TX_TOK) {
56219c72cacSJeff Kirsher 			dev->stats.tx_packets++;
56319c72cacSJeff Kirsher 			dev->stats.tx_bytes +=
56419c72cacSJeff Kirsher 					lp->tx_skb[lp->tx_next_done]->len;
56519c72cacSJeff Kirsher 		} else {
56619c72cacSJeff Kirsher 			dev->stats.tx_errors++;
56719c72cacSJeff Kirsher 			dev->stats.tx_dropped++;
56819c72cacSJeff Kirsher 
56919c72cacSJeff Kirsher 			/* Underflow */
57019c72cacSJeff Kirsher 			if (devcs & ETH_TX_UND)
57119c72cacSJeff Kirsher 				dev->stats.tx_fifo_errors++;
57219c72cacSJeff Kirsher 
57319c72cacSJeff Kirsher 			/* Oversized frame */
57419c72cacSJeff Kirsher 			if (devcs & ETH_TX_OF)
57519c72cacSJeff Kirsher 				dev->stats.tx_aborted_errors++;
57619c72cacSJeff Kirsher 
57719c72cacSJeff Kirsher 			/* Excessive deferrals */
57819c72cacSJeff Kirsher 			if (devcs & ETH_TX_ED)
57919c72cacSJeff Kirsher 				dev->stats.tx_carrier_errors++;
58019c72cacSJeff Kirsher 
58119c72cacSJeff Kirsher 			/* Collisions: medium busy */
58219c72cacSJeff Kirsher 			if (devcs & ETH_TX_EC)
58319c72cacSJeff Kirsher 				dev->stats.collisions++;
58419c72cacSJeff Kirsher 
58519c72cacSJeff Kirsher 			/* Late collision */
58619c72cacSJeff Kirsher 			if (devcs & ETH_TX_LC)
58719c72cacSJeff Kirsher 				dev->stats.tx_window_errors++;
58819c72cacSJeff Kirsher 		}
58919c72cacSJeff Kirsher 
59019c72cacSJeff Kirsher 		/* We must always free the original skb */
59119c72cacSJeff Kirsher 		if (lp->tx_skb[lp->tx_next_done]) {
592*0fc96939SThomas Bogendoerfer 			dma_unmap_single(lp->dmadev,
593*0fc96939SThomas Bogendoerfer 					 lp->tx_skb_dma[lp->tx_next_done],
594*0fc96939SThomas Bogendoerfer 					 lp->tx_skb[lp->tx_next_done]->len,
595*0fc96939SThomas Bogendoerfer 					 DMA_TO_DEVICE);
59619c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->tx_skb[lp->tx_next_done]);
59719c72cacSJeff Kirsher 			lp->tx_skb[lp->tx_next_done] = NULL;
59819c72cacSJeff Kirsher 		}
59919c72cacSJeff Kirsher 
60019c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].control = DMA_DESC_IOF;
60119c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].devcs = ETH_TX_FD | ETH_TX_LD;
60219c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].link = 0;
60319c72cacSJeff Kirsher 		lp->td_ring[lp->tx_next_done].ca = 0;
60419c72cacSJeff Kirsher 		lp->tx_count--;
60519c72cacSJeff Kirsher 
60619c72cacSJeff Kirsher 		/* Go on to next transmission */
60719c72cacSJeff Kirsher 		lp->tx_next_done = (lp->tx_next_done + 1) & KORINA_TDS_MASK;
60819c72cacSJeff Kirsher 		td = &lp->td_ring[lp->tx_next_done];
60919c72cacSJeff Kirsher 
61019c72cacSJeff Kirsher 	}
61119c72cacSJeff Kirsher 
61219c72cacSJeff Kirsher 	/* Clear the DMA status register */
61319c72cacSJeff Kirsher 	dmas = readl(&lp->tx_dma_regs->dmas);
61419c72cacSJeff Kirsher 	writel(~dmas, &lp->tx_dma_regs->dmas);
61519c72cacSJeff Kirsher 
61619c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) &
61719c72cacSJeff Kirsher 			~(DMA_STAT_FINI | DMA_STAT_ERR),
61819c72cacSJeff Kirsher 			&lp->tx_dma_regs->dmasm);
61919c72cacSJeff Kirsher 
62019c72cacSJeff Kirsher 	spin_unlock(&lp->lock);
62119c72cacSJeff Kirsher }
62219c72cacSJeff Kirsher 
62319c72cacSJeff Kirsher static irqreturn_t
62419c72cacSJeff Kirsher korina_tx_dma_interrupt(int irq, void *dev_id)
62519c72cacSJeff Kirsher {
62619c72cacSJeff Kirsher 	struct net_device *dev = dev_id;
62719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
62819c72cacSJeff Kirsher 	u32 dmas, dmasm;
62919c72cacSJeff Kirsher 	irqreturn_t retval;
63019c72cacSJeff Kirsher 
63119c72cacSJeff Kirsher 	dmas = readl(&lp->tx_dma_regs->dmas);
63219c72cacSJeff Kirsher 
63319c72cacSJeff Kirsher 	if (dmas & (DMA_STAT_FINI | DMA_STAT_ERR)) {
63419c72cacSJeff Kirsher 		dmasm = readl(&lp->tx_dma_regs->dmasm);
63519c72cacSJeff Kirsher 		writel(dmasm | (DMA_STAT_FINI | DMA_STAT_ERR),
63619c72cacSJeff Kirsher 				&lp->tx_dma_regs->dmasm);
63719c72cacSJeff Kirsher 
63819c72cacSJeff Kirsher 		korina_tx(dev);
63919c72cacSJeff Kirsher 
64019c72cacSJeff Kirsher 		if (lp->tx_chain_status == desc_filled &&
64119c72cacSJeff Kirsher 			(readl(&(lp->tx_dma_regs->dmandptr)) == 0)) {
642*0fc96939SThomas Bogendoerfer 			writel(korina_tx_dma(lp, lp->tx_chain_head),
643*0fc96939SThomas Bogendoerfer 			       &lp->tx_dma_regs->dmandptr);
64419c72cacSJeff Kirsher 			lp->tx_chain_status = desc_empty;
64519c72cacSJeff Kirsher 			lp->tx_chain_head = lp->tx_chain_tail;
646860e9538SFlorian Westphal 			netif_trans_update(dev);
64719c72cacSJeff Kirsher 		}
64819c72cacSJeff Kirsher 		if (dmas & DMA_STAT_ERR)
64919c72cacSJeff Kirsher 			printk(KERN_ERR "%s: DMA error\n", dev->name);
65019c72cacSJeff Kirsher 
65119c72cacSJeff Kirsher 		retval = IRQ_HANDLED;
65219c72cacSJeff Kirsher 	} else
65319c72cacSJeff Kirsher 		retval = IRQ_NONE;
65419c72cacSJeff Kirsher 
65519c72cacSJeff Kirsher 	return retval;
65619c72cacSJeff Kirsher }
65719c72cacSJeff Kirsher 
65819c72cacSJeff Kirsher 
65919c72cacSJeff Kirsher static void korina_check_media(struct net_device *dev, unsigned int init_media)
66019c72cacSJeff Kirsher {
66119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
66219c72cacSJeff Kirsher 
66389f9d540SThomas Bogendoerfer 	mii_check_media(&lp->mii_if, 1, init_media);
66419c72cacSJeff Kirsher 
66519c72cacSJeff Kirsher 	if (lp->mii_if.full_duplex)
66619c72cacSJeff Kirsher 		writel(readl(&lp->eth_regs->ethmac2) | ETH_MAC2_FD,
66719c72cacSJeff Kirsher 						&lp->eth_regs->ethmac2);
66819c72cacSJeff Kirsher 	else
66919c72cacSJeff Kirsher 		writel(readl(&lp->eth_regs->ethmac2) & ~ETH_MAC2_FD,
67019c72cacSJeff Kirsher 						&lp->eth_regs->ethmac2);
67119c72cacSJeff Kirsher }
67219c72cacSJeff Kirsher 
67334309b36SKees Cook static void korina_poll_media(struct timer_list *t)
67419c72cacSJeff Kirsher {
67534309b36SKees Cook 	struct korina_private *lp = from_timer(lp, t, media_check_timer);
67634309b36SKees Cook 	struct net_device *dev = lp->dev;
67719c72cacSJeff Kirsher 
67819c72cacSJeff Kirsher 	korina_check_media(dev, 0);
67919c72cacSJeff Kirsher 	mod_timer(&lp->media_check_timer, jiffies + HZ);
68019c72cacSJeff Kirsher }
68119c72cacSJeff Kirsher 
68219c72cacSJeff Kirsher static void korina_set_carrier(struct mii_if_info *mii)
68319c72cacSJeff Kirsher {
68419c72cacSJeff Kirsher 	if (mii->force_media) {
68519c72cacSJeff Kirsher 		/* autoneg is off: Link is always assumed to be up */
68619c72cacSJeff Kirsher 		if (!netif_carrier_ok(mii->dev))
68719c72cacSJeff Kirsher 			netif_carrier_on(mii->dev);
68819c72cacSJeff Kirsher 	} else  /* Let MMI library update carrier status */
68919c72cacSJeff Kirsher 		korina_check_media(mii->dev, 0);
69019c72cacSJeff Kirsher }
69119c72cacSJeff Kirsher 
69219c72cacSJeff Kirsher static int korina_ioctl(struct net_device *dev, struct ifreq *rq, int cmd)
69319c72cacSJeff Kirsher {
69419c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
69519c72cacSJeff Kirsher 	struct mii_ioctl_data *data = if_mii(rq);
69619c72cacSJeff Kirsher 	int rc;
69719c72cacSJeff Kirsher 
69819c72cacSJeff Kirsher 	if (!netif_running(dev))
69919c72cacSJeff Kirsher 		return -EINVAL;
70019c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
70119c72cacSJeff Kirsher 	rc = generic_mii_ioctl(&lp->mii_if, data, cmd, NULL);
70219c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
70319c72cacSJeff Kirsher 	korina_set_carrier(&lp->mii_if);
70419c72cacSJeff Kirsher 
70519c72cacSJeff Kirsher 	return rc;
70619c72cacSJeff Kirsher }
70719c72cacSJeff Kirsher 
70819c72cacSJeff Kirsher /* ethtool helpers */
70919c72cacSJeff Kirsher static void netdev_get_drvinfo(struct net_device *dev,
71019c72cacSJeff Kirsher 				struct ethtool_drvinfo *info)
71119c72cacSJeff Kirsher {
71219c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
71319c72cacSJeff Kirsher 
7147826d43fSJiri Pirko 	strlcpy(info->driver, DRV_NAME, sizeof(info->driver));
7157826d43fSJiri Pirko 	strlcpy(info->version, DRV_VERSION, sizeof(info->version));
7167826d43fSJiri Pirko 	strlcpy(info->bus_info, lp->dev->name, sizeof(info->bus_info));
71719c72cacSJeff Kirsher }
71819c72cacSJeff Kirsher 
719af473688SPhilippe Reynes static int netdev_get_link_ksettings(struct net_device *dev,
720af473688SPhilippe Reynes 				     struct ethtool_link_ksettings *cmd)
72119c72cacSJeff Kirsher {
72219c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
72319c72cacSJeff Kirsher 
72419c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
72582c01a84Syuval.shaia@oracle.com 	mii_ethtool_get_link_ksettings(&lp->mii_if, cmd);
72619c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
72719c72cacSJeff Kirsher 
72882c01a84Syuval.shaia@oracle.com 	return 0;
72919c72cacSJeff Kirsher }
73019c72cacSJeff Kirsher 
731af473688SPhilippe Reynes static int netdev_set_link_ksettings(struct net_device *dev,
732af473688SPhilippe Reynes 				     const struct ethtool_link_ksettings *cmd)
73319c72cacSJeff Kirsher {
73419c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
73519c72cacSJeff Kirsher 	int rc;
73619c72cacSJeff Kirsher 
73719c72cacSJeff Kirsher 	spin_lock_irq(&lp->lock);
738af473688SPhilippe Reynes 	rc = mii_ethtool_set_link_ksettings(&lp->mii_if, cmd);
73919c72cacSJeff Kirsher 	spin_unlock_irq(&lp->lock);
74019c72cacSJeff Kirsher 	korina_set_carrier(&lp->mii_if);
74119c72cacSJeff Kirsher 
74219c72cacSJeff Kirsher 	return rc;
74319c72cacSJeff Kirsher }
74419c72cacSJeff Kirsher 
74519c72cacSJeff Kirsher static u32 netdev_get_link(struct net_device *dev)
74619c72cacSJeff Kirsher {
74719c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
74819c72cacSJeff Kirsher 
74919c72cacSJeff Kirsher 	return mii_link_ok(&lp->mii_if);
75019c72cacSJeff Kirsher }
75119c72cacSJeff Kirsher 
75219c72cacSJeff Kirsher static const struct ethtool_ops netdev_ethtool_ops = {
75319c72cacSJeff Kirsher 	.get_drvinfo		= netdev_get_drvinfo,
75419c72cacSJeff Kirsher 	.get_link		= netdev_get_link,
755af473688SPhilippe Reynes 	.get_link_ksettings	= netdev_get_link_ksettings,
756af473688SPhilippe Reynes 	.set_link_ksettings	= netdev_set_link_ksettings,
75719c72cacSJeff Kirsher };
75819c72cacSJeff Kirsher 
75919c72cacSJeff Kirsher static int korina_alloc_ring(struct net_device *dev)
76019c72cacSJeff Kirsher {
76119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
76219c72cacSJeff Kirsher 	struct sk_buff *skb;
763*0fc96939SThomas Bogendoerfer 	dma_addr_t ca;
76419c72cacSJeff Kirsher 	int i;
76519c72cacSJeff Kirsher 
76619c72cacSJeff Kirsher 	/* Initialize the transmit descriptors */
76719c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_TDS; i++) {
76819c72cacSJeff Kirsher 		lp->td_ring[i].control = DMA_DESC_IOF;
76919c72cacSJeff Kirsher 		lp->td_ring[i].devcs = ETH_TX_FD | ETH_TX_LD;
77019c72cacSJeff Kirsher 		lp->td_ring[i].ca = 0;
77119c72cacSJeff Kirsher 		lp->td_ring[i].link = 0;
77219c72cacSJeff Kirsher 	}
77319c72cacSJeff Kirsher 	lp->tx_next_done = lp->tx_chain_head = lp->tx_chain_tail =
77419c72cacSJeff Kirsher 			lp->tx_full = lp->tx_count = 0;
77519c72cacSJeff Kirsher 	lp->tx_chain_status = desc_empty;
77619c72cacSJeff Kirsher 
77719c72cacSJeff Kirsher 	/* Initialize the receive descriptors */
77819c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_RDS; i++) {
77919c72cacSJeff Kirsher 		skb = netdev_alloc_skb_ip_align(dev, KORINA_RBSIZE);
78019c72cacSJeff Kirsher 		if (!skb)
78119c72cacSJeff Kirsher 			return -ENOMEM;
78219c72cacSJeff Kirsher 		lp->rx_skb[i] = skb;
78319c72cacSJeff Kirsher 		lp->rd_ring[i].control = DMA_DESC_IOD |
78419c72cacSJeff Kirsher 				DMA_COUNT(KORINA_RBSIZE);
78519c72cacSJeff Kirsher 		lp->rd_ring[i].devcs = 0;
786*0fc96939SThomas Bogendoerfer 		ca = dma_map_single(lp->dmadev, skb->data, KORINA_RBSIZE,
787*0fc96939SThomas Bogendoerfer 				    DMA_FROM_DEVICE);
788*0fc96939SThomas Bogendoerfer 		if (dma_mapping_error(lp->dmadev, ca))
789*0fc96939SThomas Bogendoerfer 			return -ENOMEM;
790*0fc96939SThomas Bogendoerfer 		lp->rd_ring[i].ca = ca;
791*0fc96939SThomas Bogendoerfer 		lp->rx_skb_dma[i] = ca;
792*0fc96939SThomas Bogendoerfer 		lp->rd_ring[i].link = korina_rx_dma(lp, i + 1);
79319c72cacSJeff Kirsher 	}
79419c72cacSJeff Kirsher 
79519c72cacSJeff Kirsher 	/* loop back receive descriptors, so the last
79619c72cacSJeff Kirsher 	 * descriptor points to the first one */
797*0fc96939SThomas Bogendoerfer 	lp->rd_ring[i - 1].link = lp->rd_dma;
79819c72cacSJeff Kirsher 	lp->rd_ring[i - 1].control |= DMA_DESC_COD;
79919c72cacSJeff Kirsher 
80019c72cacSJeff Kirsher 	lp->rx_next_done  = 0;
80119c72cacSJeff Kirsher 	lp->rx_chain_head = 0;
80219c72cacSJeff Kirsher 	lp->rx_chain_tail = 0;
80319c72cacSJeff Kirsher 	lp->rx_chain_status = desc_empty;
80419c72cacSJeff Kirsher 
80519c72cacSJeff Kirsher 	return 0;
80619c72cacSJeff Kirsher }
80719c72cacSJeff Kirsher 
80819c72cacSJeff Kirsher static void korina_free_ring(struct net_device *dev)
80919c72cacSJeff Kirsher {
81019c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
81119c72cacSJeff Kirsher 	int i;
81219c72cacSJeff Kirsher 
81319c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_RDS; i++) {
81419c72cacSJeff Kirsher 		lp->rd_ring[i].control = 0;
815*0fc96939SThomas Bogendoerfer 		if (lp->rx_skb[i]) {
816*0fc96939SThomas Bogendoerfer 			dma_unmap_single(lp->dmadev, lp->rx_skb_dma[i],
817*0fc96939SThomas Bogendoerfer 					 KORINA_RBSIZE, DMA_FROM_DEVICE);
81819c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->rx_skb[i]);
81919c72cacSJeff Kirsher 			lp->rx_skb[i] = NULL;
82019c72cacSJeff Kirsher 		}
821*0fc96939SThomas Bogendoerfer 	}
82219c72cacSJeff Kirsher 
82319c72cacSJeff Kirsher 	for (i = 0; i < KORINA_NUM_TDS; i++) {
82419c72cacSJeff Kirsher 		lp->td_ring[i].control = 0;
825*0fc96939SThomas Bogendoerfer 		if (lp->tx_skb[i]) {
826*0fc96939SThomas Bogendoerfer 			dma_unmap_single(lp->dmadev, lp->tx_skb_dma[i],
827*0fc96939SThomas Bogendoerfer 					 lp->tx_skb[i]->len, DMA_TO_DEVICE);
82819c72cacSJeff Kirsher 			dev_kfree_skb_any(lp->tx_skb[i]);
82919c72cacSJeff Kirsher 			lp->tx_skb[i] = NULL;
83019c72cacSJeff Kirsher 		}
83119c72cacSJeff Kirsher 	}
832*0fc96939SThomas Bogendoerfer }
83319c72cacSJeff Kirsher 
83419c72cacSJeff Kirsher /*
83519c72cacSJeff Kirsher  * Initialize the RC32434 ethernet controller.
83619c72cacSJeff Kirsher  */
83719c72cacSJeff Kirsher static int korina_init(struct net_device *dev)
83819c72cacSJeff Kirsher {
83919c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
84019c72cacSJeff Kirsher 
84119c72cacSJeff Kirsher 	/* Disable DMA */
84219c72cacSJeff Kirsher 	korina_abort_tx(dev);
84319c72cacSJeff Kirsher 	korina_abort_rx(dev);
84419c72cacSJeff Kirsher 
84519c72cacSJeff Kirsher 	/* reset ethernet logic */
84619c72cacSJeff Kirsher 	writel(0, &lp->eth_regs->ethintfc);
84719c72cacSJeff Kirsher 	while ((readl(&lp->eth_regs->ethintfc) & ETH_INT_FC_RIP))
848860e9538SFlorian Westphal 		netif_trans_update(dev);
84919c72cacSJeff Kirsher 
85019c72cacSJeff Kirsher 	/* Enable Ethernet Interface */
85119c72cacSJeff Kirsher 	writel(ETH_INT_FC_EN, &lp->eth_regs->ethintfc);
85219c72cacSJeff Kirsher 
85319c72cacSJeff Kirsher 	/* Allocate rings */
85419c72cacSJeff Kirsher 	if (korina_alloc_ring(dev)) {
85519c72cacSJeff Kirsher 		printk(KERN_ERR "%s: descriptor allocation failed\n", dev->name);
85619c72cacSJeff Kirsher 		korina_free_ring(dev);
85719c72cacSJeff Kirsher 		return -ENOMEM;
85819c72cacSJeff Kirsher 	}
85919c72cacSJeff Kirsher 
86019c72cacSJeff Kirsher 	writel(0, &lp->rx_dma_regs->dmas);
86119c72cacSJeff Kirsher 	/* Start Rx DMA */
8620fe63247SThomas Bogendoerfer 	writel(0, &lp->rx_dma_regs->dmandptr);
863*0fc96939SThomas Bogendoerfer 	writel(korina_rx_dma(lp, 0), &lp->rx_dma_regs->dmadptr);
86419c72cacSJeff Kirsher 
86519c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) &
86619c72cacSJeff Kirsher 			~(DMA_STAT_FINI | DMA_STAT_ERR),
86719c72cacSJeff Kirsher 			&lp->tx_dma_regs->dmasm);
86819c72cacSJeff Kirsher 	writel(readl(&lp->rx_dma_regs->dmasm) &
86919c72cacSJeff Kirsher 			~(DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR),
87019c72cacSJeff Kirsher 			&lp->rx_dma_regs->dmasm);
87119c72cacSJeff Kirsher 
87219c72cacSJeff Kirsher 	/* Accept only packets destined for this Ethernet device address */
87319c72cacSJeff Kirsher 	writel(ETH_ARC_AB, &lp->eth_regs->etharc);
87419c72cacSJeff Kirsher 
87519c72cacSJeff Kirsher 	/* Set all Ether station address registers to their initial values */
87619c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal0);
87719c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah0);
87819c72cacSJeff Kirsher 
87919c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal1);
88019c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah1);
88119c72cacSJeff Kirsher 
88219c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal2);
88319c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah2);
88419c72cacSJeff Kirsher 
88519c72cacSJeff Kirsher 	writel(STATION_ADDRESS_LOW(dev), &lp->eth_regs->ethsal3);
88619c72cacSJeff Kirsher 	writel(STATION_ADDRESS_HIGH(dev), &lp->eth_regs->ethsah3);
88719c72cacSJeff Kirsher 
88819c72cacSJeff Kirsher 
88919c72cacSJeff Kirsher 	/* Frame Length Checking, Pad Enable, CRC Enable, Full Duplex set */
89019c72cacSJeff Kirsher 	writel(ETH_MAC2_PE | ETH_MAC2_CEN | ETH_MAC2_FD,
89119c72cacSJeff Kirsher 			&lp->eth_regs->ethmac2);
89219c72cacSJeff Kirsher 
89319c72cacSJeff Kirsher 	/* Back to back inter-packet-gap */
89419c72cacSJeff Kirsher 	writel(0x15, &lp->eth_regs->ethipgt);
89519c72cacSJeff Kirsher 	/* Non - Back to back inter-packet-gap */
89619c72cacSJeff Kirsher 	writel(0x12, &lp->eth_regs->ethipgr);
89719c72cacSJeff Kirsher 
89819c72cacSJeff Kirsher 	/* Management Clock Prescaler Divisor
89919c72cacSJeff Kirsher 	 * Clock independent setting */
90019c72cacSJeff Kirsher 	writel(((idt_cpu_freq) / MII_CLOCK + 1) & ~1,
90119c72cacSJeff Kirsher 			&lp->eth_regs->ethmcp);
90289f9d540SThomas Bogendoerfer 	writel(0, &lp->eth_regs->miimcfg);
90319c72cacSJeff Kirsher 
90419c72cacSJeff Kirsher 	/* don't transmit until fifo contains 48b */
90519c72cacSJeff Kirsher 	writel(48, &lp->eth_regs->ethfifott);
90619c72cacSJeff Kirsher 
90719c72cacSJeff Kirsher 	writel(ETH_MAC1_RE, &lp->eth_regs->ethmac1);
90819c72cacSJeff Kirsher 
90989f9d540SThomas Bogendoerfer 	korina_check_media(dev, 1);
91089f9d540SThomas Bogendoerfer 
91119c72cacSJeff Kirsher 	napi_enable(&lp->napi);
91219c72cacSJeff Kirsher 	netif_start_queue(dev);
91319c72cacSJeff Kirsher 
91419c72cacSJeff Kirsher 	return 0;
91519c72cacSJeff Kirsher }
91619c72cacSJeff Kirsher 
91719c72cacSJeff Kirsher /*
91819c72cacSJeff Kirsher  * Restart the RC32434 ethernet controller.
91919c72cacSJeff Kirsher  */
92019c72cacSJeff Kirsher static void korina_restart_task(struct work_struct *work)
92119c72cacSJeff Kirsher {
92219c72cacSJeff Kirsher 	struct korina_private *lp = container_of(work,
92319c72cacSJeff Kirsher 			struct korina_private, restart_task);
92419c72cacSJeff Kirsher 	struct net_device *dev = lp->dev;
92519c72cacSJeff Kirsher 
92619c72cacSJeff Kirsher 	/*
92719c72cacSJeff Kirsher 	 * Disable interrupts
92819c72cacSJeff Kirsher 	 */
92919c72cacSJeff Kirsher 	disable_irq(lp->rx_irq);
93019c72cacSJeff Kirsher 	disable_irq(lp->tx_irq);
93119c72cacSJeff Kirsher 
93219c72cacSJeff Kirsher 	writel(readl(&lp->tx_dma_regs->dmasm) |
93319c72cacSJeff Kirsher 				DMA_STAT_FINI | DMA_STAT_ERR,
93419c72cacSJeff Kirsher 				&lp->tx_dma_regs->dmasm);
93519c72cacSJeff Kirsher 	writel(readl(&lp->rx_dma_regs->dmasm) |
93619c72cacSJeff Kirsher 				DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR,
93719c72cacSJeff Kirsher 				&lp->rx_dma_regs->dmasm);
93819c72cacSJeff Kirsher 
93919c72cacSJeff Kirsher 	napi_disable(&lp->napi);
94019c72cacSJeff Kirsher 
941e6afb1adSFlorian Fainelli 	korina_free_ring(dev);
942e6afb1adSFlorian Fainelli 
94319c72cacSJeff Kirsher 	if (korina_init(dev) < 0) {
94419c72cacSJeff Kirsher 		printk(KERN_ERR "%s: cannot restart device\n", dev->name);
94519c72cacSJeff Kirsher 		return;
94619c72cacSJeff Kirsher 	}
94719c72cacSJeff Kirsher 	korina_multicast_list(dev);
94819c72cacSJeff Kirsher 
94919c72cacSJeff Kirsher 	enable_irq(lp->tx_irq);
95019c72cacSJeff Kirsher 	enable_irq(lp->rx_irq);
95119c72cacSJeff Kirsher }
95219c72cacSJeff Kirsher 
9530290bd29SMichael S. Tsirkin static void korina_tx_timeout(struct net_device *dev, unsigned int txqueue)
95419c72cacSJeff Kirsher {
95519c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
95619c72cacSJeff Kirsher 
95719c72cacSJeff Kirsher 	schedule_work(&lp->restart_task);
95819c72cacSJeff Kirsher }
95919c72cacSJeff Kirsher 
96019c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
96119c72cacSJeff Kirsher static void korina_poll_controller(struct net_device *dev)
96219c72cacSJeff Kirsher {
96319c72cacSJeff Kirsher 	disable_irq(dev->irq);
96419c72cacSJeff Kirsher 	korina_tx_dma_interrupt(dev->irq, dev);
96519c72cacSJeff Kirsher 	enable_irq(dev->irq);
96619c72cacSJeff Kirsher }
96719c72cacSJeff Kirsher #endif
96819c72cacSJeff Kirsher 
96919c72cacSJeff Kirsher static int korina_open(struct net_device *dev)
97019c72cacSJeff Kirsher {
97119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
97219c72cacSJeff Kirsher 	int ret;
97319c72cacSJeff Kirsher 
97419c72cacSJeff Kirsher 	/* Initialize */
97519c72cacSJeff Kirsher 	ret = korina_init(dev);
97619c72cacSJeff Kirsher 	if (ret < 0) {
97719c72cacSJeff Kirsher 		printk(KERN_ERR "%s: cannot open device\n", dev->name);
97819c72cacSJeff Kirsher 		goto out;
97919c72cacSJeff Kirsher 	}
98019c72cacSJeff Kirsher 
98119c72cacSJeff Kirsher 	/* Install the interrupt handler
9827ce103b4SRoman Yeryomin 	 * that handles the Done Finished */
98319c72cacSJeff Kirsher 	ret = request_irq(lp->rx_irq, korina_rx_dma_interrupt,
9842414fe16SMichael Opdenacker 			0, "Korina ethernet Rx", dev);
98519c72cacSJeff Kirsher 	if (ret < 0) {
98619c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get Rx DMA IRQ %d\n",
98719c72cacSJeff Kirsher 			dev->name, lp->rx_irq);
98819c72cacSJeff Kirsher 		goto err_release;
98919c72cacSJeff Kirsher 	}
99019c72cacSJeff Kirsher 	ret = request_irq(lp->tx_irq, korina_tx_dma_interrupt,
9912414fe16SMichael Opdenacker 			0, "Korina ethernet Tx", dev);
99219c72cacSJeff Kirsher 	if (ret < 0) {
99319c72cacSJeff Kirsher 		printk(KERN_ERR "%s: unable to get Tx DMA IRQ %d\n",
99419c72cacSJeff Kirsher 			dev->name, lp->tx_irq);
99519c72cacSJeff Kirsher 		goto err_free_rx_irq;
99619c72cacSJeff Kirsher 	}
99719c72cacSJeff Kirsher 
99819c72cacSJeff Kirsher 	mod_timer(&lp->media_check_timer, jiffies + 1);
99919c72cacSJeff Kirsher out:
100019c72cacSJeff Kirsher 	return ret;
100119c72cacSJeff Kirsher 
100219c72cacSJeff Kirsher err_free_rx_irq:
100319c72cacSJeff Kirsher 	free_irq(lp->rx_irq, dev);
100419c72cacSJeff Kirsher err_release:
100519c72cacSJeff Kirsher 	korina_free_ring(dev);
100619c72cacSJeff Kirsher 	goto out;
100719c72cacSJeff Kirsher }
100819c72cacSJeff Kirsher 
100919c72cacSJeff Kirsher static int korina_close(struct net_device *dev)
101019c72cacSJeff Kirsher {
101119c72cacSJeff Kirsher 	struct korina_private *lp = netdev_priv(dev);
101219c72cacSJeff Kirsher 	u32 tmp;
101319c72cacSJeff Kirsher 
101419c72cacSJeff Kirsher 	del_timer(&lp->media_check_timer);
101519c72cacSJeff Kirsher 
101619c72cacSJeff Kirsher 	/* Disable interrupts */
101719c72cacSJeff Kirsher 	disable_irq(lp->rx_irq);
101819c72cacSJeff Kirsher 	disable_irq(lp->tx_irq);
101919c72cacSJeff Kirsher 
102019c72cacSJeff Kirsher 	korina_abort_tx(dev);
102119c72cacSJeff Kirsher 	tmp = readl(&lp->tx_dma_regs->dmasm);
102219c72cacSJeff Kirsher 	tmp = tmp | DMA_STAT_FINI | DMA_STAT_ERR;
102319c72cacSJeff Kirsher 	writel(tmp, &lp->tx_dma_regs->dmasm);
102419c72cacSJeff Kirsher 
102519c72cacSJeff Kirsher 	korina_abort_rx(dev);
102619c72cacSJeff Kirsher 	tmp = readl(&lp->rx_dma_regs->dmasm);
102719c72cacSJeff Kirsher 	tmp = tmp | DMA_STAT_DONE | DMA_STAT_HALT | DMA_STAT_ERR;
102819c72cacSJeff Kirsher 	writel(tmp, &lp->rx_dma_regs->dmasm);
102919c72cacSJeff Kirsher 
103019c72cacSJeff Kirsher 	napi_disable(&lp->napi);
103119c72cacSJeff Kirsher 
103219c72cacSJeff Kirsher 	cancel_work_sync(&lp->restart_task);
103319c72cacSJeff Kirsher 
1034e6afb1adSFlorian Fainelli 	korina_free_ring(dev);
1035e6afb1adSFlorian Fainelli 
103619c72cacSJeff Kirsher 	free_irq(lp->rx_irq, dev);
103719c72cacSJeff Kirsher 	free_irq(lp->tx_irq, dev);
103819c72cacSJeff Kirsher 
103919c72cacSJeff Kirsher 	return 0;
104019c72cacSJeff Kirsher }
104119c72cacSJeff Kirsher 
104219c72cacSJeff Kirsher static const struct net_device_ops korina_netdev_ops = {
104319c72cacSJeff Kirsher 	.ndo_open		= korina_open,
104419c72cacSJeff Kirsher 	.ndo_stop		= korina_close,
104519c72cacSJeff Kirsher 	.ndo_start_xmit		= korina_send_packet,
1046afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= korina_multicast_list,
104719c72cacSJeff Kirsher 	.ndo_tx_timeout		= korina_tx_timeout,
104819c72cacSJeff Kirsher 	.ndo_do_ioctl		= korina_ioctl,
104919c72cacSJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
105019c72cacSJeff Kirsher 	.ndo_set_mac_address	= eth_mac_addr,
105119c72cacSJeff Kirsher #ifdef CONFIG_NET_POLL_CONTROLLER
105219c72cacSJeff Kirsher 	.ndo_poll_controller	= korina_poll_controller,
105319c72cacSJeff Kirsher #endif
105419c72cacSJeff Kirsher };
105519c72cacSJeff Kirsher 
105619c72cacSJeff Kirsher static int korina_probe(struct platform_device *pdev)
105719c72cacSJeff Kirsher {
105819c72cacSJeff Kirsher 	struct korina_device *bif = platform_get_drvdata(pdev);
105919c72cacSJeff Kirsher 	struct korina_private *lp;
106019c72cacSJeff Kirsher 	struct net_device *dev;
1061b4cd249aSThomas Bogendoerfer 	void __iomem *p;
106219c72cacSJeff Kirsher 	int rc;
106319c72cacSJeff Kirsher 
1064b4cd249aSThomas Bogendoerfer 	dev = devm_alloc_etherdev(&pdev->dev, sizeof(struct korina_private));
106541de8d4cSJoe Perches 	if (!dev)
106619c72cacSJeff Kirsher 		return -ENOMEM;
106741de8d4cSJoe Perches 
106819c72cacSJeff Kirsher 	SET_NETDEV_DEV(dev, &pdev->dev);
106919c72cacSJeff Kirsher 	lp = netdev_priv(dev);
107019c72cacSJeff Kirsher 
107119c72cacSJeff Kirsher 	bif->dev = dev;
1072d458cdf7SJoe Perches 	memcpy(dev->dev_addr, bif->mac, ETH_ALEN);
107319c72cacSJeff Kirsher 
107419c72cacSJeff Kirsher 	lp->rx_irq = platform_get_irq_byname(pdev, "korina_rx");
107519c72cacSJeff Kirsher 	lp->tx_irq = platform_get_irq_byname(pdev, "korina_tx");
107619c72cacSJeff Kirsher 
1077b4cd249aSThomas Bogendoerfer 	p = devm_platform_ioremap_resource_byname(pdev, "korina_regs");
1078b4cd249aSThomas Bogendoerfer 	if (!p) {
107919c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap registers\n");
1080b4cd249aSThomas Bogendoerfer 		return -ENOMEM;
108119c72cacSJeff Kirsher 	}
1082b4cd249aSThomas Bogendoerfer 	lp->eth_regs = p;
108319c72cacSJeff Kirsher 
1084b4cd249aSThomas Bogendoerfer 	p = devm_platform_ioremap_resource_byname(pdev, "korina_dma_rx");
1085b4cd249aSThomas Bogendoerfer 	if (!p) {
108619c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap Rx DMA registers\n");
1087b4cd249aSThomas Bogendoerfer 		return -ENOMEM;
108819c72cacSJeff Kirsher 	}
1089b4cd249aSThomas Bogendoerfer 	lp->rx_dma_regs = p;
109019c72cacSJeff Kirsher 
1091b4cd249aSThomas Bogendoerfer 	p = devm_platform_ioremap_resource_byname(pdev, "korina_dma_tx");
1092b4cd249aSThomas Bogendoerfer 	if (!p) {
109319c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME ": cannot remap Tx DMA registers\n");
1094b4cd249aSThomas Bogendoerfer 		return -ENOMEM;
109519c72cacSJeff Kirsher 	}
1096b4cd249aSThomas Bogendoerfer 	lp->tx_dma_regs = p;
109719c72cacSJeff Kirsher 
1098*0fc96939SThomas Bogendoerfer 	lp->td_ring = dmam_alloc_coherent(&pdev->dev, TD_RING_SIZE,
1099*0fc96939SThomas Bogendoerfer 					  &lp->td_dma, GFP_KERNEL);
1100b4cd249aSThomas Bogendoerfer 	if (!lp->td_ring)
1101b4cd249aSThomas Bogendoerfer 		return -ENOMEM;
110219c72cacSJeff Kirsher 
1103*0fc96939SThomas Bogendoerfer 	lp->rd_ring = dmam_alloc_coherent(&pdev->dev, RD_RING_SIZE,
1104*0fc96939SThomas Bogendoerfer 					  &lp->rd_dma, GFP_KERNEL);
1105*0fc96939SThomas Bogendoerfer 	if (!lp->rd_ring)
1106*0fc96939SThomas Bogendoerfer 		return -ENOMEM;
110719c72cacSJeff Kirsher 
110819c72cacSJeff Kirsher 	spin_lock_init(&lp->lock);
110919c72cacSJeff Kirsher 	/* just use the rx dma irq */
111019c72cacSJeff Kirsher 	dev->irq = lp->rx_irq;
111119c72cacSJeff Kirsher 	lp->dev = dev;
1112*0fc96939SThomas Bogendoerfer 	lp->dmadev = &pdev->dev;
111319c72cacSJeff Kirsher 
111419c72cacSJeff Kirsher 	dev->netdev_ops = &korina_netdev_ops;
111519c72cacSJeff Kirsher 	dev->ethtool_ops = &netdev_ethtool_ops;
111619c72cacSJeff Kirsher 	dev->watchdog_timeo = TX_TIMEOUT;
1117d609d289SRoman Yeryomin 	netif_napi_add(dev, &lp->napi, korina_poll, NAPI_POLL_WEIGHT);
111819c72cacSJeff Kirsher 
111919c72cacSJeff Kirsher 	lp->mii_if.dev = dev;
112089f9d540SThomas Bogendoerfer 	lp->mii_if.mdio_read = korina_mdio_read;
112189f9d540SThomas Bogendoerfer 	lp->mii_if.mdio_write = korina_mdio_write;
112289f9d540SThomas Bogendoerfer 	lp->mii_if.phy_id = 1;
112319c72cacSJeff Kirsher 	lp->mii_if.phy_id_mask = 0x1f;
112419c72cacSJeff Kirsher 	lp->mii_if.reg_num_mask = 0x1f;
112519c72cacSJeff Kirsher 
112619c72cacSJeff Kirsher 	rc = register_netdev(dev);
112719c72cacSJeff Kirsher 	if (rc < 0) {
112819c72cacSJeff Kirsher 		printk(KERN_ERR DRV_NAME
112919c72cacSJeff Kirsher 			": cannot register net device: %d\n", rc);
1130b4cd249aSThomas Bogendoerfer 		return rc;
113119c72cacSJeff Kirsher 	}
113234309b36SKees Cook 	timer_setup(&lp->media_check_timer, korina_poll_media, 0);
113319c72cacSJeff Kirsher 
113419c72cacSJeff Kirsher 	INIT_WORK(&lp->restart_task, korina_restart_task);
113519c72cacSJeff Kirsher 
113619c72cacSJeff Kirsher 	printk(KERN_INFO "%s: " DRV_NAME "-" DRV_VERSION " " DRV_RELDATE "\n",
113719c72cacSJeff Kirsher 			dev->name);
113819c72cacSJeff Kirsher 	return rc;
113919c72cacSJeff Kirsher }
114019c72cacSJeff Kirsher 
114119c72cacSJeff Kirsher static int korina_remove(struct platform_device *pdev)
114219c72cacSJeff Kirsher {
114319c72cacSJeff Kirsher 	struct korina_device *bif = platform_get_drvdata(pdev);
114419c72cacSJeff Kirsher 
114519c72cacSJeff Kirsher 	unregister_netdev(bif->dev);
114619c72cacSJeff Kirsher 
114719c72cacSJeff Kirsher 	return 0;
114819c72cacSJeff Kirsher }
114919c72cacSJeff Kirsher 
115019c72cacSJeff Kirsher static struct platform_driver korina_driver = {
115119c72cacSJeff Kirsher 	.driver.name = "korina",
115219c72cacSJeff Kirsher 	.probe = korina_probe,
115319c72cacSJeff Kirsher 	.remove = korina_remove,
115419c72cacSJeff Kirsher };
115519c72cacSJeff Kirsher 
1156db62f684SAxel Lin module_platform_driver(korina_driver);
115719c72cacSJeff Kirsher 
115819c72cacSJeff Kirsher MODULE_AUTHOR("Philip Rischel <rischelp@idt.com>");
115919c72cacSJeff Kirsher MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>");
116019c72cacSJeff Kirsher MODULE_AUTHOR("Florian Fainelli <florian@openwrt.org>");
116187736fc6SRoman Yeryomin MODULE_AUTHOR("Roman Yeryomin <roman@advem.lv>");
116219c72cacSJeff Kirsher MODULE_DESCRIPTION("IDT RC32434 (Korina) Ethernet driver");
116319c72cacSJeff Kirsher MODULE_LICENSE("GPL");
1164