162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
262306a36Sopenharmony_ci/* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
362306a36Sopenharmony_ci *
462306a36Sopenharmony_ci * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller (davem@davemloft.net)
562306a36Sopenharmony_ci */
662306a36Sopenharmony_ci
762306a36Sopenharmony_ci#include <linux/module.h>
862306a36Sopenharmony_ci#include <linux/pgtable.h>
962306a36Sopenharmony_ci
1062306a36Sopenharmony_ci#include <linux/kernel.h>
1162306a36Sopenharmony_ci#include <linux/types.h>
1262306a36Sopenharmony_ci#include <linux/fcntl.h>
1362306a36Sopenharmony_ci#include <linux/interrupt.h>
1462306a36Sopenharmony_ci#include <linux/ioport.h>
1562306a36Sopenharmony_ci#include <linux/in.h>
1662306a36Sopenharmony_ci#include <linux/string.h>
1762306a36Sopenharmony_ci#include <linux/delay.h>
1862306a36Sopenharmony_ci#include <linux/crc32.h>
1962306a36Sopenharmony_ci#include <linux/errno.h>
2062306a36Sopenharmony_ci#include <linux/ethtool.h>
2162306a36Sopenharmony_ci#include <linux/mii.h>
2262306a36Sopenharmony_ci#include <linux/netdevice.h>
2362306a36Sopenharmony_ci#include <linux/etherdevice.h>
2462306a36Sopenharmony_ci#include <linux/skbuff.h>
2562306a36Sopenharmony_ci#include <linux/bitops.h>
2662306a36Sopenharmony_ci#include <linux/dma-mapping.h>
2762306a36Sopenharmony_ci#include <linux/of.h>
2862306a36Sopenharmony_ci#include <linux/platform_device.h>
2962306a36Sopenharmony_ci#include <linux/gfp.h>
3062306a36Sopenharmony_ci
3162306a36Sopenharmony_ci#include <asm/auxio.h>
3262306a36Sopenharmony_ci#include <asm/byteorder.h>
3362306a36Sopenharmony_ci#include <asm/dma.h>
3462306a36Sopenharmony_ci#include <asm/idprom.h>
3562306a36Sopenharmony_ci#include <asm/io.h>
3662306a36Sopenharmony_ci#include <asm/openprom.h>
3762306a36Sopenharmony_ci#include <asm/oplib.h>
3862306a36Sopenharmony_ci
3962306a36Sopenharmony_ci#include "sunbmac.h"
4062306a36Sopenharmony_ci
4162306a36Sopenharmony_ci#define DRV_NAME	"sunbmac"
4262306a36Sopenharmony_ci#define DRV_VERSION	"2.1"
4362306a36Sopenharmony_ci#define DRV_RELDATE	"August 26, 2008"
4462306a36Sopenharmony_ci#define DRV_AUTHOR	"David S. Miller (davem@davemloft.net)"
4562306a36Sopenharmony_ci
4662306a36Sopenharmony_cistatic char version[] =
4762306a36Sopenharmony_ci	DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
4862306a36Sopenharmony_ci
4962306a36Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
5062306a36Sopenharmony_ciMODULE_AUTHOR(DRV_AUTHOR);
5162306a36Sopenharmony_ciMODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
5262306a36Sopenharmony_ciMODULE_LICENSE("GPL");
5362306a36Sopenharmony_ci
5462306a36Sopenharmony_ci#undef DEBUG_PROBE
5562306a36Sopenharmony_ci#undef DEBUG_TX
5662306a36Sopenharmony_ci#undef DEBUG_IRQ
5762306a36Sopenharmony_ci
5862306a36Sopenharmony_ci#ifdef DEBUG_PROBE
5962306a36Sopenharmony_ci#define DP(x)  printk x
6062306a36Sopenharmony_ci#else
6162306a36Sopenharmony_ci#define DP(x)
6262306a36Sopenharmony_ci#endif
6362306a36Sopenharmony_ci
6462306a36Sopenharmony_ci#ifdef DEBUG_TX
6562306a36Sopenharmony_ci#define DTX(x)  printk x
6662306a36Sopenharmony_ci#else
6762306a36Sopenharmony_ci#define DTX(x)
6862306a36Sopenharmony_ci#endif
6962306a36Sopenharmony_ci
7062306a36Sopenharmony_ci#ifdef DEBUG_IRQ
7162306a36Sopenharmony_ci#define DIRQ(x)  printk x
7262306a36Sopenharmony_ci#else
7362306a36Sopenharmony_ci#define DIRQ(x)
7462306a36Sopenharmony_ci#endif
7562306a36Sopenharmony_ci
7662306a36Sopenharmony_ci#define DEFAULT_JAMSIZE    4 /* Toe jam */
7762306a36Sopenharmony_ci
7862306a36Sopenharmony_ci#define QEC_RESET_TRIES 200
7962306a36Sopenharmony_ci
8062306a36Sopenharmony_cistatic int qec_global_reset(void __iomem *gregs)
8162306a36Sopenharmony_ci{
8262306a36Sopenharmony_ci	int tries = QEC_RESET_TRIES;
8362306a36Sopenharmony_ci
8462306a36Sopenharmony_ci	sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
8562306a36Sopenharmony_ci	while (--tries) {
8662306a36Sopenharmony_ci		if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
8762306a36Sopenharmony_ci			udelay(20);
8862306a36Sopenharmony_ci			continue;
8962306a36Sopenharmony_ci		}
9062306a36Sopenharmony_ci		break;
9162306a36Sopenharmony_ci	}
9262306a36Sopenharmony_ci	if (tries)
9362306a36Sopenharmony_ci		return 0;
9462306a36Sopenharmony_ci	printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
9562306a36Sopenharmony_ci	return -1;
9662306a36Sopenharmony_ci}
9762306a36Sopenharmony_ci
9862306a36Sopenharmony_cistatic void qec_init(struct bigmac *bp)
9962306a36Sopenharmony_ci{
10062306a36Sopenharmony_ci	struct platform_device *qec_op = bp->qec_op;
10162306a36Sopenharmony_ci	void __iomem *gregs = bp->gregs;
10262306a36Sopenharmony_ci	u8 bsizes = bp->bigmac_bursts;
10362306a36Sopenharmony_ci	u32 regval;
10462306a36Sopenharmony_ci
10562306a36Sopenharmony_ci	/* 64byte bursts do not work at the moment, do
10662306a36Sopenharmony_ci	 * not even try to enable them.  -DaveM
10762306a36Sopenharmony_ci	 */
10862306a36Sopenharmony_ci	if (bsizes & DMA_BURST32)
10962306a36Sopenharmony_ci		regval = GLOB_CTRL_B32;
11062306a36Sopenharmony_ci	else
11162306a36Sopenharmony_ci		regval = GLOB_CTRL_B16;
11262306a36Sopenharmony_ci	sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
11362306a36Sopenharmony_ci	sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
11462306a36Sopenharmony_ci
11562306a36Sopenharmony_ci	/* All of memsize is given to bigmac. */
11662306a36Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]),
11762306a36Sopenharmony_ci		    gregs + GLOB_MSIZE);
11862306a36Sopenharmony_ci
11962306a36Sopenharmony_ci	/* Half to the transmitter, half to the receiver. */
12062306a36Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
12162306a36Sopenharmony_ci		    gregs + GLOB_TSIZE);
12262306a36Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
12362306a36Sopenharmony_ci		    gregs + GLOB_RSIZE);
12462306a36Sopenharmony_ci}
12562306a36Sopenharmony_ci
12662306a36Sopenharmony_ci#define TX_RESET_TRIES     32
12762306a36Sopenharmony_ci#define RX_RESET_TRIES     32
12862306a36Sopenharmony_ci
12962306a36Sopenharmony_cistatic void bigmac_tx_reset(void __iomem *bregs)
13062306a36Sopenharmony_ci{
13162306a36Sopenharmony_ci	int tries = TX_RESET_TRIES;
13262306a36Sopenharmony_ci
13362306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_TXCFG);
13462306a36Sopenharmony_ci
13562306a36Sopenharmony_ci	/* The fifo threshold bit is read-only and does
13662306a36Sopenharmony_ci	 * not clear.  -DaveM
13762306a36Sopenharmony_ci	 */
13862306a36Sopenharmony_ci	while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
13962306a36Sopenharmony_ci	       --tries != 0)
14062306a36Sopenharmony_ci		udelay(20);
14162306a36Sopenharmony_ci
14262306a36Sopenharmony_ci	if (!tries) {
14362306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
14462306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
14562306a36Sopenharmony_ci		       sbus_readl(bregs + BMAC_TXCFG));
14662306a36Sopenharmony_ci	}
14762306a36Sopenharmony_ci}
14862306a36Sopenharmony_ci
14962306a36Sopenharmony_cistatic void bigmac_rx_reset(void __iomem *bregs)
15062306a36Sopenharmony_ci{
15162306a36Sopenharmony_ci	int tries = RX_RESET_TRIES;
15262306a36Sopenharmony_ci
15362306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_RXCFG);
15462306a36Sopenharmony_ci	while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
15562306a36Sopenharmony_ci		udelay(20);
15662306a36Sopenharmony_ci
15762306a36Sopenharmony_ci	if (!tries) {
15862306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
15962306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
16062306a36Sopenharmony_ci		       sbus_readl(bregs + BMAC_RXCFG));
16162306a36Sopenharmony_ci	}
16262306a36Sopenharmony_ci}
16362306a36Sopenharmony_ci
16462306a36Sopenharmony_ci/* Reset the transmitter and receiver. */
16562306a36Sopenharmony_cistatic void bigmac_stop(struct bigmac *bp)
16662306a36Sopenharmony_ci{
16762306a36Sopenharmony_ci	bigmac_tx_reset(bp->bregs);
16862306a36Sopenharmony_ci	bigmac_rx_reset(bp->bregs);
16962306a36Sopenharmony_ci}
17062306a36Sopenharmony_ci
17162306a36Sopenharmony_cistatic void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
17262306a36Sopenharmony_ci{
17362306a36Sopenharmony_ci	struct net_device_stats *stats = &bp->dev->stats;
17462306a36Sopenharmony_ci
17562306a36Sopenharmony_ci	stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
17662306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_RCRCECTR);
17762306a36Sopenharmony_ci
17862306a36Sopenharmony_ci	stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
17962306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_UNALECTR);
18062306a36Sopenharmony_ci
18162306a36Sopenharmony_ci	stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
18262306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_GLECTR);
18362306a36Sopenharmony_ci
18462306a36Sopenharmony_ci	stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
18562306a36Sopenharmony_ci
18662306a36Sopenharmony_ci	stats->collisions +=
18762306a36Sopenharmony_ci		(sbus_readl(bregs + BMAC_EXCTR) +
18862306a36Sopenharmony_ci		 sbus_readl(bregs + BMAC_LTCTR));
18962306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_EXCTR);
19062306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_LTCTR);
19162306a36Sopenharmony_ci}
19262306a36Sopenharmony_ci
19362306a36Sopenharmony_cistatic void bigmac_clean_rings(struct bigmac *bp)
19462306a36Sopenharmony_ci{
19562306a36Sopenharmony_ci	int i;
19662306a36Sopenharmony_ci
19762306a36Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
19862306a36Sopenharmony_ci		if (bp->rx_skbs[i] != NULL) {
19962306a36Sopenharmony_ci			dev_kfree_skb_any(bp->rx_skbs[i]);
20062306a36Sopenharmony_ci			bp->rx_skbs[i] = NULL;
20162306a36Sopenharmony_ci		}
20262306a36Sopenharmony_ci	}
20362306a36Sopenharmony_ci
20462306a36Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++) {
20562306a36Sopenharmony_ci		if (bp->tx_skbs[i] != NULL) {
20662306a36Sopenharmony_ci			dev_kfree_skb_any(bp->tx_skbs[i]);
20762306a36Sopenharmony_ci			bp->tx_skbs[i] = NULL;
20862306a36Sopenharmony_ci		}
20962306a36Sopenharmony_ci	}
21062306a36Sopenharmony_ci}
21162306a36Sopenharmony_ci
21262306a36Sopenharmony_cistatic void bigmac_init_rings(struct bigmac *bp, bool non_blocking)
21362306a36Sopenharmony_ci{
21462306a36Sopenharmony_ci	struct bmac_init_block *bb = bp->bmac_block;
21562306a36Sopenharmony_ci	int i;
21662306a36Sopenharmony_ci	gfp_t gfp_flags = GFP_KERNEL;
21762306a36Sopenharmony_ci
21862306a36Sopenharmony_ci	if (non_blocking)
21962306a36Sopenharmony_ci		gfp_flags = GFP_ATOMIC;
22062306a36Sopenharmony_ci
22162306a36Sopenharmony_ci	bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
22262306a36Sopenharmony_ci
22362306a36Sopenharmony_ci	/* Free any skippy bufs left around in the rings. */
22462306a36Sopenharmony_ci	bigmac_clean_rings(bp);
22562306a36Sopenharmony_ci
22662306a36Sopenharmony_ci	/* Now get new skbufs for the receive ring. */
22762306a36Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
22862306a36Sopenharmony_ci		struct sk_buff *skb;
22962306a36Sopenharmony_ci
23062306a36Sopenharmony_ci		skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
23162306a36Sopenharmony_ci		if (!skb)
23262306a36Sopenharmony_ci			continue;
23362306a36Sopenharmony_ci
23462306a36Sopenharmony_ci		bp->rx_skbs[i] = skb;
23562306a36Sopenharmony_ci
23662306a36Sopenharmony_ci		/* Because we reserve afterwards. */
23762306a36Sopenharmony_ci		skb_put(skb, ETH_FRAME_LEN);
23862306a36Sopenharmony_ci		skb_reserve(skb, 34);
23962306a36Sopenharmony_ci
24062306a36Sopenharmony_ci		bb->be_rxd[i].rx_addr =
24162306a36Sopenharmony_ci			dma_map_single(&bp->bigmac_op->dev,
24262306a36Sopenharmony_ci				       skb->data,
24362306a36Sopenharmony_ci				       RX_BUF_ALLOC_SIZE - 34,
24462306a36Sopenharmony_ci				       DMA_FROM_DEVICE);
24562306a36Sopenharmony_ci		bb->be_rxd[i].rx_flags =
24662306a36Sopenharmony_ci			(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
24762306a36Sopenharmony_ci	}
24862306a36Sopenharmony_ci
24962306a36Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++)
25062306a36Sopenharmony_ci		bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
25162306a36Sopenharmony_ci}
25262306a36Sopenharmony_ci
25362306a36Sopenharmony_ci#define MGMT_CLKON  (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
25462306a36Sopenharmony_ci#define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
25562306a36Sopenharmony_ci
25662306a36Sopenharmony_cistatic void idle_transceiver(void __iomem *tregs)
25762306a36Sopenharmony_ci{
25862306a36Sopenharmony_ci	int i = 20;
25962306a36Sopenharmony_ci
26062306a36Sopenharmony_ci	while (i--) {
26162306a36Sopenharmony_ci		sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
26262306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
26362306a36Sopenharmony_ci		sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
26462306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
26562306a36Sopenharmony_ci	}
26662306a36Sopenharmony_ci}
26762306a36Sopenharmony_ci
26862306a36Sopenharmony_cistatic void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
26962306a36Sopenharmony_ci{
27062306a36Sopenharmony_ci	if (bp->tcvr_type == internal) {
27162306a36Sopenharmony_ci		bit = (bit & 1) << 3;
27262306a36Sopenharmony_ci		sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
27362306a36Sopenharmony_ci			    tregs + TCVR_MPAL);
27462306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
27562306a36Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
27662306a36Sopenharmony_ci			    tregs + TCVR_MPAL);
27762306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
27862306a36Sopenharmony_ci	} else if (bp->tcvr_type == external) {
27962306a36Sopenharmony_ci		bit = (bit & 1) << 2;
28062306a36Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
28162306a36Sopenharmony_ci			    tregs + TCVR_MPAL);
28262306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
28362306a36Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
28462306a36Sopenharmony_ci			    tregs + TCVR_MPAL);
28562306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
28662306a36Sopenharmony_ci	} else {
28762306a36Sopenharmony_ci		printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
28862306a36Sopenharmony_ci	}
28962306a36Sopenharmony_ci}
29062306a36Sopenharmony_ci
29162306a36Sopenharmony_cistatic int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
29262306a36Sopenharmony_ci{
29362306a36Sopenharmony_ci	int retval = 0;
29462306a36Sopenharmony_ci
29562306a36Sopenharmony_ci	if (bp->tcvr_type == internal) {
29662306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
29762306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
29862306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
29962306a36Sopenharmony_ci			    tregs + TCVR_MPAL);
30062306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
30162306a36Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
30262306a36Sopenharmony_ci	} else if (bp->tcvr_type == external) {
30362306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
30462306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
30562306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
30662306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
30762306a36Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
30862306a36Sopenharmony_ci	} else {
30962306a36Sopenharmony_ci		printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
31062306a36Sopenharmony_ci	}
31162306a36Sopenharmony_ci	return retval;
31262306a36Sopenharmony_ci}
31362306a36Sopenharmony_ci
31462306a36Sopenharmony_cistatic int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
31562306a36Sopenharmony_ci{
31662306a36Sopenharmony_ci	int retval = 0;
31762306a36Sopenharmony_ci
31862306a36Sopenharmony_ci	if (bp->tcvr_type == internal) {
31962306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
32062306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
32162306a36Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
32262306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
32362306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
32462306a36Sopenharmony_ci	} else if (bp->tcvr_type == external) {
32562306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
32662306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
32762306a36Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
32862306a36Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
32962306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
33062306a36Sopenharmony_ci	} else {
33162306a36Sopenharmony_ci		printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
33262306a36Sopenharmony_ci	}
33362306a36Sopenharmony_ci	return retval;
33462306a36Sopenharmony_ci}
33562306a36Sopenharmony_ci
33662306a36Sopenharmony_cistatic void put_tcvr_byte(struct bigmac *bp,
33762306a36Sopenharmony_ci			  void __iomem *tregs,
33862306a36Sopenharmony_ci			  unsigned int byte)
33962306a36Sopenharmony_ci{
34062306a36Sopenharmony_ci	int shift = 4;
34162306a36Sopenharmony_ci
34262306a36Sopenharmony_ci	do {
34362306a36Sopenharmony_ci		write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
34462306a36Sopenharmony_ci		shift -= 1;
34562306a36Sopenharmony_ci	} while (shift >= 0);
34662306a36Sopenharmony_ci}
34762306a36Sopenharmony_ci
34862306a36Sopenharmony_cistatic void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
34962306a36Sopenharmony_ci			      int reg, unsigned short val)
35062306a36Sopenharmony_ci{
35162306a36Sopenharmony_ci	int shift;
35262306a36Sopenharmony_ci
35362306a36Sopenharmony_ci	reg &= 0xff;
35462306a36Sopenharmony_ci	val &= 0xffff;
35562306a36Sopenharmony_ci	switch(bp->tcvr_type) {
35662306a36Sopenharmony_ci	case internal:
35762306a36Sopenharmony_ci	case external:
35862306a36Sopenharmony_ci		break;
35962306a36Sopenharmony_ci
36062306a36Sopenharmony_ci	default:
36162306a36Sopenharmony_ci		printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
36262306a36Sopenharmony_ci		return;
36362306a36Sopenharmony_ci	}
36462306a36Sopenharmony_ci
36562306a36Sopenharmony_ci	idle_transceiver(tregs);
36662306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
36762306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
36862306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
36962306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
37062306a36Sopenharmony_ci
37162306a36Sopenharmony_ci	put_tcvr_byte(bp, tregs,
37262306a36Sopenharmony_ci		      ((bp->tcvr_type == internal) ?
37362306a36Sopenharmony_ci		       BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
37462306a36Sopenharmony_ci
37562306a36Sopenharmony_ci	put_tcvr_byte(bp, tregs, reg);
37662306a36Sopenharmony_ci
37762306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
37862306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
37962306a36Sopenharmony_ci
38062306a36Sopenharmony_ci	shift = 15;
38162306a36Sopenharmony_ci	do {
38262306a36Sopenharmony_ci		write_tcvr_bit(bp, tregs, (val >> shift) & 1);
38362306a36Sopenharmony_ci		shift -= 1;
38462306a36Sopenharmony_ci	} while (shift >= 0);
38562306a36Sopenharmony_ci}
38662306a36Sopenharmony_ci
38762306a36Sopenharmony_cistatic unsigned short bigmac_tcvr_read(struct bigmac *bp,
38862306a36Sopenharmony_ci				       void __iomem *tregs,
38962306a36Sopenharmony_ci				       int reg)
39062306a36Sopenharmony_ci{
39162306a36Sopenharmony_ci	unsigned short retval = 0;
39262306a36Sopenharmony_ci
39362306a36Sopenharmony_ci	reg &= 0xff;
39462306a36Sopenharmony_ci	switch(bp->tcvr_type) {
39562306a36Sopenharmony_ci	case internal:
39662306a36Sopenharmony_ci	case external:
39762306a36Sopenharmony_ci		break;
39862306a36Sopenharmony_ci
39962306a36Sopenharmony_ci	default:
40062306a36Sopenharmony_ci		printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
40162306a36Sopenharmony_ci		return 0xffff;
40262306a36Sopenharmony_ci	}
40362306a36Sopenharmony_ci
40462306a36Sopenharmony_ci	idle_transceiver(tregs);
40562306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
40662306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
40762306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
40862306a36Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
40962306a36Sopenharmony_ci
41062306a36Sopenharmony_ci	put_tcvr_byte(bp, tregs,
41162306a36Sopenharmony_ci		      ((bp->tcvr_type == internal) ?
41262306a36Sopenharmony_ci		       BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
41362306a36Sopenharmony_ci
41462306a36Sopenharmony_ci	put_tcvr_byte(bp, tregs, reg);
41562306a36Sopenharmony_ci
41662306a36Sopenharmony_ci	if (bp->tcvr_type == external) {
41762306a36Sopenharmony_ci		int shift = 15;
41862306a36Sopenharmony_ci
41962306a36Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
42062306a36Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
42162306a36Sopenharmony_ci
42262306a36Sopenharmony_ci		do {
42362306a36Sopenharmony_ci			int tmp;
42462306a36Sopenharmony_ci
42562306a36Sopenharmony_ci			tmp = read_tcvr_bit2(bp, tregs);
42662306a36Sopenharmony_ci			retval |= ((tmp & 1) << shift);
42762306a36Sopenharmony_ci			shift -= 1;
42862306a36Sopenharmony_ci		} while (shift >= 0);
42962306a36Sopenharmony_ci
43062306a36Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
43162306a36Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
43262306a36Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
43362306a36Sopenharmony_ci	} else {
43462306a36Sopenharmony_ci		int shift = 15;
43562306a36Sopenharmony_ci
43662306a36Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
43762306a36Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
43862306a36Sopenharmony_ci
43962306a36Sopenharmony_ci		do {
44062306a36Sopenharmony_ci			int tmp;
44162306a36Sopenharmony_ci
44262306a36Sopenharmony_ci			tmp = read_tcvr_bit(bp, tregs);
44362306a36Sopenharmony_ci			retval |= ((tmp & 1) << shift);
44462306a36Sopenharmony_ci			shift -= 1;
44562306a36Sopenharmony_ci		} while (shift >= 0);
44662306a36Sopenharmony_ci
44762306a36Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
44862306a36Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
44962306a36Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
45062306a36Sopenharmony_ci	}
45162306a36Sopenharmony_ci	return retval;
45262306a36Sopenharmony_ci}
45362306a36Sopenharmony_ci
45462306a36Sopenharmony_cistatic void bigmac_tcvr_init(struct bigmac *bp)
45562306a36Sopenharmony_ci{
45662306a36Sopenharmony_ci	void __iomem *tregs = bp->tregs;
45762306a36Sopenharmony_ci	u32 mpal;
45862306a36Sopenharmony_ci
45962306a36Sopenharmony_ci	idle_transceiver(tregs);
46062306a36Sopenharmony_ci	sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
46162306a36Sopenharmony_ci		    tregs + TCVR_MPAL);
46262306a36Sopenharmony_ci	sbus_readl(tregs + TCVR_MPAL);
46362306a36Sopenharmony_ci
46462306a36Sopenharmony_ci	/* Only the bit for the present transceiver (internal or
46562306a36Sopenharmony_ci	 * external) will stick, set them both and see what stays.
46662306a36Sopenharmony_ci	 */
46762306a36Sopenharmony_ci	sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
46862306a36Sopenharmony_ci	sbus_readl(tregs + TCVR_MPAL);
46962306a36Sopenharmony_ci	udelay(20);
47062306a36Sopenharmony_ci
47162306a36Sopenharmony_ci	mpal = sbus_readl(tregs + TCVR_MPAL);
47262306a36Sopenharmony_ci	if (mpal & MGMT_PAL_EXT_MDIO) {
47362306a36Sopenharmony_ci		bp->tcvr_type = external;
47462306a36Sopenharmony_ci		sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
47562306a36Sopenharmony_ci			    tregs + TCVR_TPAL);
47662306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_TPAL);
47762306a36Sopenharmony_ci	} else if (mpal & MGMT_PAL_INT_MDIO) {
47862306a36Sopenharmony_ci		bp->tcvr_type = internal;
47962306a36Sopenharmony_ci		sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
48062306a36Sopenharmony_ci			      TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
48162306a36Sopenharmony_ci			    tregs + TCVR_TPAL);
48262306a36Sopenharmony_ci		sbus_readl(tregs + TCVR_TPAL);
48362306a36Sopenharmony_ci	} else {
48462306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
48562306a36Sopenharmony_ci		       "external MDIO available!\n");
48662306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
48762306a36Sopenharmony_ci		       sbus_readl(tregs + TCVR_MPAL),
48862306a36Sopenharmony_ci		       sbus_readl(tregs + TCVR_TPAL));
48962306a36Sopenharmony_ci	}
49062306a36Sopenharmony_ci}
49162306a36Sopenharmony_ci
49262306a36Sopenharmony_cistatic int bigmac_init_hw(struct bigmac *, bool);
49362306a36Sopenharmony_ci
49462306a36Sopenharmony_cistatic int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
49562306a36Sopenharmony_ci{
49662306a36Sopenharmony_ci	if (bp->sw_bmcr & BMCR_SPEED100) {
49762306a36Sopenharmony_ci		int timeout;
49862306a36Sopenharmony_ci
49962306a36Sopenharmony_ci		/* Reset the PHY. */
50062306a36Sopenharmony_ci		bp->sw_bmcr	= (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
50162306a36Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
50262306a36Sopenharmony_ci		bp->sw_bmcr	= (BMCR_RESET);
50362306a36Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
50462306a36Sopenharmony_ci
50562306a36Sopenharmony_ci		timeout = 64;
50662306a36Sopenharmony_ci		while (--timeout) {
50762306a36Sopenharmony_ci			bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
50862306a36Sopenharmony_ci			if ((bp->sw_bmcr & BMCR_RESET) == 0)
50962306a36Sopenharmony_ci				break;
51062306a36Sopenharmony_ci			udelay(20);
51162306a36Sopenharmony_ci		}
51262306a36Sopenharmony_ci		if (timeout == 0)
51362306a36Sopenharmony_ci			printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
51462306a36Sopenharmony_ci
51562306a36Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
51662306a36Sopenharmony_ci
51762306a36Sopenharmony_ci		/* Now we try 10baseT. */
51862306a36Sopenharmony_ci		bp->sw_bmcr &= ~(BMCR_SPEED100);
51962306a36Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
52062306a36Sopenharmony_ci		return 0;
52162306a36Sopenharmony_ci	}
52262306a36Sopenharmony_ci
52362306a36Sopenharmony_ci	/* We've tried them all. */
52462306a36Sopenharmony_ci	return -1;
52562306a36Sopenharmony_ci}
52662306a36Sopenharmony_ci
52762306a36Sopenharmony_cistatic void bigmac_timer(struct timer_list *t)
52862306a36Sopenharmony_ci{
52962306a36Sopenharmony_ci	struct bigmac *bp = from_timer(bp, t, bigmac_timer);
53062306a36Sopenharmony_ci	void __iomem *tregs = bp->tregs;
53162306a36Sopenharmony_ci	int restart_timer = 0;
53262306a36Sopenharmony_ci
53362306a36Sopenharmony_ci	bp->timer_ticks++;
53462306a36Sopenharmony_ci	if (bp->timer_state == ltrywait) {
53562306a36Sopenharmony_ci		bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
53662306a36Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
53762306a36Sopenharmony_ci		if (bp->sw_bmsr & BMSR_LSTATUS) {
53862306a36Sopenharmony_ci			printk(KERN_INFO "%s: Link is now up at %s.\n",
53962306a36Sopenharmony_ci			       bp->dev->name,
54062306a36Sopenharmony_ci			       (bp->sw_bmcr & BMCR_SPEED100) ?
54162306a36Sopenharmony_ci			       "100baseT" : "10baseT");
54262306a36Sopenharmony_ci			bp->timer_state = asleep;
54362306a36Sopenharmony_ci			restart_timer = 0;
54462306a36Sopenharmony_ci		} else {
54562306a36Sopenharmony_ci			if (bp->timer_ticks >= 4) {
54662306a36Sopenharmony_ci				int ret;
54762306a36Sopenharmony_ci
54862306a36Sopenharmony_ci				ret = try_next_permutation(bp, tregs);
54962306a36Sopenharmony_ci				if (ret == -1) {
55062306a36Sopenharmony_ci					printk(KERN_ERR "%s: Link down, cable problem?\n",
55162306a36Sopenharmony_ci					       bp->dev->name);
55262306a36Sopenharmony_ci					ret = bigmac_init_hw(bp, true);
55362306a36Sopenharmony_ci					if (ret) {
55462306a36Sopenharmony_ci						printk(KERN_ERR "%s: Error, cannot re-init the "
55562306a36Sopenharmony_ci						       "BigMAC.\n", bp->dev->name);
55662306a36Sopenharmony_ci					}
55762306a36Sopenharmony_ci					return;
55862306a36Sopenharmony_ci				}
55962306a36Sopenharmony_ci				bp->timer_ticks = 0;
56062306a36Sopenharmony_ci				restart_timer = 1;
56162306a36Sopenharmony_ci			} else {
56262306a36Sopenharmony_ci				restart_timer = 1;
56362306a36Sopenharmony_ci			}
56462306a36Sopenharmony_ci		}
56562306a36Sopenharmony_ci	} else {
56662306a36Sopenharmony_ci		/* Can't happens.... */
56762306a36Sopenharmony_ci		printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
56862306a36Sopenharmony_ci		       bp->dev->name);
56962306a36Sopenharmony_ci		restart_timer = 0;
57062306a36Sopenharmony_ci		bp->timer_ticks = 0;
57162306a36Sopenharmony_ci		bp->timer_state = asleep; /* foo on you */
57262306a36Sopenharmony_ci	}
57362306a36Sopenharmony_ci
57462306a36Sopenharmony_ci	if (restart_timer != 0) {
57562306a36Sopenharmony_ci		bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
57662306a36Sopenharmony_ci		add_timer(&bp->bigmac_timer);
57762306a36Sopenharmony_ci	}
57862306a36Sopenharmony_ci}
57962306a36Sopenharmony_ci
58062306a36Sopenharmony_ci/* Well, really we just force the chip into 100baseT then
58162306a36Sopenharmony_ci * 10baseT, each time checking for a link status.
58262306a36Sopenharmony_ci */
58362306a36Sopenharmony_cistatic void bigmac_begin_auto_negotiation(struct bigmac *bp)
58462306a36Sopenharmony_ci{
58562306a36Sopenharmony_ci	void __iomem *tregs = bp->tregs;
58662306a36Sopenharmony_ci	int timeout;
58762306a36Sopenharmony_ci
58862306a36Sopenharmony_ci	/* Grab new software copies of PHY registers. */
58962306a36Sopenharmony_ci	bp->sw_bmsr	= bigmac_tcvr_read(bp, tregs, MII_BMSR);
59062306a36Sopenharmony_ci	bp->sw_bmcr	= bigmac_tcvr_read(bp, tregs, MII_BMCR);
59162306a36Sopenharmony_ci
59262306a36Sopenharmony_ci	/* Reset the PHY. */
59362306a36Sopenharmony_ci	bp->sw_bmcr	= (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
59462306a36Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
59562306a36Sopenharmony_ci	bp->sw_bmcr	= (BMCR_RESET);
59662306a36Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
59762306a36Sopenharmony_ci
59862306a36Sopenharmony_ci	timeout = 64;
59962306a36Sopenharmony_ci	while (--timeout) {
60062306a36Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
60162306a36Sopenharmony_ci		if ((bp->sw_bmcr & BMCR_RESET) == 0)
60262306a36Sopenharmony_ci			break;
60362306a36Sopenharmony_ci		udelay(20);
60462306a36Sopenharmony_ci	}
60562306a36Sopenharmony_ci	if (timeout == 0)
60662306a36Sopenharmony_ci		printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
60762306a36Sopenharmony_ci
60862306a36Sopenharmony_ci	bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
60962306a36Sopenharmony_ci
61062306a36Sopenharmony_ci	/* First we try 100baseT. */
61162306a36Sopenharmony_ci	bp->sw_bmcr |= BMCR_SPEED100;
61262306a36Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
61362306a36Sopenharmony_ci
61462306a36Sopenharmony_ci	bp->timer_state = ltrywait;
61562306a36Sopenharmony_ci	bp->timer_ticks = 0;
61662306a36Sopenharmony_ci	bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
61762306a36Sopenharmony_ci	add_timer(&bp->bigmac_timer);
61862306a36Sopenharmony_ci}
61962306a36Sopenharmony_ci
62062306a36Sopenharmony_cistatic int bigmac_init_hw(struct bigmac *bp, bool non_blocking)
62162306a36Sopenharmony_ci{
62262306a36Sopenharmony_ci	void __iomem *gregs        = bp->gregs;
62362306a36Sopenharmony_ci	void __iomem *cregs        = bp->creg;
62462306a36Sopenharmony_ci	void __iomem *bregs        = bp->bregs;
62562306a36Sopenharmony_ci	__u32 bblk_dvma = (__u32)bp->bblock_dvma;
62662306a36Sopenharmony_ci	const unsigned char *e = &bp->dev->dev_addr[0];
62762306a36Sopenharmony_ci
62862306a36Sopenharmony_ci	/* Latch current counters into statistics. */
62962306a36Sopenharmony_ci	bigmac_get_counters(bp, bregs);
63062306a36Sopenharmony_ci
63162306a36Sopenharmony_ci	/* Reset QEC. */
63262306a36Sopenharmony_ci	qec_global_reset(gregs);
63362306a36Sopenharmony_ci
63462306a36Sopenharmony_ci	/* Init QEC. */
63562306a36Sopenharmony_ci	qec_init(bp);
63662306a36Sopenharmony_ci
63762306a36Sopenharmony_ci	/* Alloc and reset the tx/rx descriptor chains. */
63862306a36Sopenharmony_ci	bigmac_init_rings(bp, non_blocking);
63962306a36Sopenharmony_ci
64062306a36Sopenharmony_ci	/* Initialize the PHY. */
64162306a36Sopenharmony_ci	bigmac_tcvr_init(bp);
64262306a36Sopenharmony_ci
64362306a36Sopenharmony_ci	/* Stop transmitter and receiver. */
64462306a36Sopenharmony_ci	bigmac_stop(bp);
64562306a36Sopenharmony_ci
64662306a36Sopenharmony_ci	/* Set hardware ethernet address. */
64762306a36Sopenharmony_ci	sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
64862306a36Sopenharmony_ci	sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
64962306a36Sopenharmony_ci	sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
65062306a36Sopenharmony_ci
65162306a36Sopenharmony_ci	/* Clear the hash table until mc upload occurs. */
65262306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE3);
65362306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE2);
65462306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE1);
65562306a36Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE0);
65662306a36Sopenharmony_ci
65762306a36Sopenharmony_ci	/* Enable Big Mac hash table filter. */
65862306a36Sopenharmony_ci	sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
65962306a36Sopenharmony_ci		    bregs + BMAC_RXCFG);
66062306a36Sopenharmony_ci	udelay(20);
66162306a36Sopenharmony_ci
66262306a36Sopenharmony_ci	/* Ok, configure the Big Mac transmitter. */
66362306a36Sopenharmony_ci	sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
66462306a36Sopenharmony_ci
66562306a36Sopenharmony_ci	/* The HME docs recommend to use the 10LSB of our MAC here. */
66662306a36Sopenharmony_ci	sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
66762306a36Sopenharmony_ci		    bregs + BMAC_RSEED);
66862306a36Sopenharmony_ci
66962306a36Sopenharmony_ci	/* Enable the output drivers no matter what. */
67062306a36Sopenharmony_ci	sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
67162306a36Sopenharmony_ci		    bregs + BMAC_XIFCFG);
67262306a36Sopenharmony_ci
67362306a36Sopenharmony_ci	/* Tell the QEC where the ring descriptors are. */
67462306a36Sopenharmony_ci	sbus_writel(bblk_dvma + bib_offset(be_rxd, 0),
67562306a36Sopenharmony_ci		    cregs + CREG_RXDS);
67662306a36Sopenharmony_ci	sbus_writel(bblk_dvma + bib_offset(be_txd, 0),
67762306a36Sopenharmony_ci		    cregs + CREG_TXDS);
67862306a36Sopenharmony_ci
67962306a36Sopenharmony_ci	/* Setup the FIFO pointers into QEC local memory. */
68062306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_RXRBUFPTR);
68162306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_RXWBUFPTR);
68262306a36Sopenharmony_ci	sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
68362306a36Sopenharmony_ci		    cregs + CREG_TXRBUFPTR);
68462306a36Sopenharmony_ci	sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
68562306a36Sopenharmony_ci		    cregs + CREG_TXWBUFPTR);
68662306a36Sopenharmony_ci
68762306a36Sopenharmony_ci	/* Tell bigmac what interrupts we don't want to hear about. */
68862306a36Sopenharmony_ci	sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
68962306a36Sopenharmony_ci		    bregs + BMAC_IMASK);
69062306a36Sopenharmony_ci
69162306a36Sopenharmony_ci	/* Enable the various other irq's. */
69262306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_RIMASK);
69362306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_TIMASK);
69462306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_QMASK);
69562306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_BMASK);
69662306a36Sopenharmony_ci
69762306a36Sopenharmony_ci	/* Set jam size to a reasonable default. */
69862306a36Sopenharmony_ci	sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
69962306a36Sopenharmony_ci
70062306a36Sopenharmony_ci	/* Clear collision counter. */
70162306a36Sopenharmony_ci	sbus_writel(0, cregs + CREG_CCNT);
70262306a36Sopenharmony_ci
70362306a36Sopenharmony_ci	/* Enable transmitter and receiver. */
70462306a36Sopenharmony_ci	sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
70562306a36Sopenharmony_ci		    bregs + BMAC_TXCFG);
70662306a36Sopenharmony_ci	sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
70762306a36Sopenharmony_ci		    bregs + BMAC_RXCFG);
70862306a36Sopenharmony_ci
70962306a36Sopenharmony_ci	/* Ok, start detecting link speed/duplex. */
71062306a36Sopenharmony_ci	bigmac_begin_auto_negotiation(bp);
71162306a36Sopenharmony_ci
71262306a36Sopenharmony_ci	/* Success. */
71362306a36Sopenharmony_ci	return 0;
71462306a36Sopenharmony_ci}
71562306a36Sopenharmony_ci
71662306a36Sopenharmony_ci/* Error interrupts get sent here. */
71762306a36Sopenharmony_cistatic void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
71862306a36Sopenharmony_ci{
71962306a36Sopenharmony_ci	printk(KERN_ERR "bigmac_is_medium_rare: ");
72062306a36Sopenharmony_ci	if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
72162306a36Sopenharmony_ci		if (qec_status & GLOB_STAT_ER)
72262306a36Sopenharmony_ci			printk("QEC_ERROR, ");
72362306a36Sopenharmony_ci		if (qec_status & GLOB_STAT_BM)
72462306a36Sopenharmony_ci			printk("QEC_BMAC_ERROR, ");
72562306a36Sopenharmony_ci	}
72662306a36Sopenharmony_ci	if (bmac_status & CREG_STAT_ERRORS) {
72762306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_BERROR)
72862306a36Sopenharmony_ci			printk("BMAC_ERROR, ");
72962306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_TXDERROR)
73062306a36Sopenharmony_ci			printk("TXD_ERROR, ");
73162306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_TXLERR)
73262306a36Sopenharmony_ci			printk("TX_LATE_ERROR, ");
73362306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_TXPERR)
73462306a36Sopenharmony_ci			printk("TX_PARITY_ERROR, ");
73562306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_TXSERR)
73662306a36Sopenharmony_ci			printk("TX_SBUS_ERROR, ");
73762306a36Sopenharmony_ci
73862306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_RXDROP)
73962306a36Sopenharmony_ci			printk("RX_DROP_ERROR, ");
74062306a36Sopenharmony_ci
74162306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_RXSMALL)
74262306a36Sopenharmony_ci			printk("RX_SMALL_ERROR, ");
74362306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_RXLERR)
74462306a36Sopenharmony_ci			printk("RX_LATE_ERROR, ");
74562306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_RXPERR)
74662306a36Sopenharmony_ci			printk("RX_PARITY_ERROR, ");
74762306a36Sopenharmony_ci		if (bmac_status & CREG_STAT_RXSERR)
74862306a36Sopenharmony_ci			printk("RX_SBUS_ERROR, ");
74962306a36Sopenharmony_ci	}
75062306a36Sopenharmony_ci
75162306a36Sopenharmony_ci	printk(" RESET\n");
75262306a36Sopenharmony_ci	bigmac_init_hw(bp, true);
75362306a36Sopenharmony_ci}
75462306a36Sopenharmony_ci
75562306a36Sopenharmony_ci/* BigMAC transmit complete service routines. */
75662306a36Sopenharmony_cistatic void bigmac_tx(struct bigmac *bp)
75762306a36Sopenharmony_ci{
75862306a36Sopenharmony_ci	struct be_txd *txbase = &bp->bmac_block->be_txd[0];
75962306a36Sopenharmony_ci	struct net_device *dev = bp->dev;
76062306a36Sopenharmony_ci	int elem;
76162306a36Sopenharmony_ci
76262306a36Sopenharmony_ci	spin_lock(&bp->lock);
76362306a36Sopenharmony_ci
76462306a36Sopenharmony_ci	elem = bp->tx_old;
76562306a36Sopenharmony_ci	DTX(("bigmac_tx: tx_old[%d] ", elem));
76662306a36Sopenharmony_ci	while (elem != bp->tx_new) {
76762306a36Sopenharmony_ci		struct sk_buff *skb;
76862306a36Sopenharmony_ci		struct be_txd *this = &txbase[elem];
76962306a36Sopenharmony_ci
77062306a36Sopenharmony_ci		DTX(("this(%p) [flags(%08x)addr(%08x)]",
77162306a36Sopenharmony_ci		     this, this->tx_flags, this->tx_addr));
77262306a36Sopenharmony_ci
77362306a36Sopenharmony_ci		if (this->tx_flags & TXD_OWN)
77462306a36Sopenharmony_ci			break;
77562306a36Sopenharmony_ci		skb = bp->tx_skbs[elem];
77662306a36Sopenharmony_ci		dev->stats.tx_packets++;
77762306a36Sopenharmony_ci		dev->stats.tx_bytes += skb->len;
77862306a36Sopenharmony_ci		dma_unmap_single(&bp->bigmac_op->dev,
77962306a36Sopenharmony_ci				 this->tx_addr, skb->len,
78062306a36Sopenharmony_ci				 DMA_TO_DEVICE);
78162306a36Sopenharmony_ci
78262306a36Sopenharmony_ci		DTX(("skb(%p) ", skb));
78362306a36Sopenharmony_ci		bp->tx_skbs[elem] = NULL;
78462306a36Sopenharmony_ci		dev_consume_skb_irq(skb);
78562306a36Sopenharmony_ci
78662306a36Sopenharmony_ci		elem = NEXT_TX(elem);
78762306a36Sopenharmony_ci	}
78862306a36Sopenharmony_ci	DTX((" DONE, tx_old=%d\n", elem));
78962306a36Sopenharmony_ci	bp->tx_old = elem;
79062306a36Sopenharmony_ci
79162306a36Sopenharmony_ci	if (netif_queue_stopped(dev) &&
79262306a36Sopenharmony_ci	    TX_BUFFS_AVAIL(bp) > 0)
79362306a36Sopenharmony_ci		netif_wake_queue(bp->dev);
79462306a36Sopenharmony_ci
79562306a36Sopenharmony_ci	spin_unlock(&bp->lock);
79662306a36Sopenharmony_ci}
79762306a36Sopenharmony_ci
79862306a36Sopenharmony_ci/* BigMAC receive complete service routines. */
79962306a36Sopenharmony_cistatic void bigmac_rx(struct bigmac *bp)
80062306a36Sopenharmony_ci{
80162306a36Sopenharmony_ci	struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
80262306a36Sopenharmony_ci	struct be_rxd *this;
80362306a36Sopenharmony_ci	int elem = bp->rx_new, drops = 0;
80462306a36Sopenharmony_ci	u32 flags;
80562306a36Sopenharmony_ci
80662306a36Sopenharmony_ci	this = &rxbase[elem];
80762306a36Sopenharmony_ci	while (!((flags = this->rx_flags) & RXD_OWN)) {
80862306a36Sopenharmony_ci		struct sk_buff *skb;
80962306a36Sopenharmony_ci		int len = (flags & RXD_LENGTH); /* FCS not included */
81062306a36Sopenharmony_ci
81162306a36Sopenharmony_ci		/* Check for errors. */
81262306a36Sopenharmony_ci		if (len < ETH_ZLEN) {
81362306a36Sopenharmony_ci			bp->dev->stats.rx_errors++;
81462306a36Sopenharmony_ci			bp->dev->stats.rx_length_errors++;
81562306a36Sopenharmony_ci
81662306a36Sopenharmony_ci	drop_it:
81762306a36Sopenharmony_ci			/* Return it to the BigMAC. */
81862306a36Sopenharmony_ci			bp->dev->stats.rx_dropped++;
81962306a36Sopenharmony_ci			this->rx_flags =
82062306a36Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
82162306a36Sopenharmony_ci			goto next;
82262306a36Sopenharmony_ci		}
82362306a36Sopenharmony_ci		skb = bp->rx_skbs[elem];
82462306a36Sopenharmony_ci		if (len > RX_COPY_THRESHOLD) {
82562306a36Sopenharmony_ci			struct sk_buff *new_skb;
82662306a36Sopenharmony_ci
82762306a36Sopenharmony_ci			/* Now refill the entry, if we can. */
82862306a36Sopenharmony_ci			new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
82962306a36Sopenharmony_ci			if (new_skb == NULL) {
83062306a36Sopenharmony_ci				drops++;
83162306a36Sopenharmony_ci				goto drop_it;
83262306a36Sopenharmony_ci			}
83362306a36Sopenharmony_ci			dma_unmap_single(&bp->bigmac_op->dev,
83462306a36Sopenharmony_ci					 this->rx_addr,
83562306a36Sopenharmony_ci					 RX_BUF_ALLOC_SIZE - 34,
83662306a36Sopenharmony_ci					 DMA_FROM_DEVICE);
83762306a36Sopenharmony_ci			bp->rx_skbs[elem] = new_skb;
83862306a36Sopenharmony_ci			skb_put(new_skb, ETH_FRAME_LEN);
83962306a36Sopenharmony_ci			skb_reserve(new_skb, 34);
84062306a36Sopenharmony_ci			this->rx_addr =
84162306a36Sopenharmony_ci				dma_map_single(&bp->bigmac_op->dev,
84262306a36Sopenharmony_ci					       new_skb->data,
84362306a36Sopenharmony_ci					       RX_BUF_ALLOC_SIZE - 34,
84462306a36Sopenharmony_ci					       DMA_FROM_DEVICE);
84562306a36Sopenharmony_ci			this->rx_flags =
84662306a36Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
84762306a36Sopenharmony_ci
84862306a36Sopenharmony_ci			/* Trim the original skb for the netif. */
84962306a36Sopenharmony_ci			skb_trim(skb, len);
85062306a36Sopenharmony_ci		} else {
85162306a36Sopenharmony_ci			struct sk_buff *copy_skb = netdev_alloc_skb(bp->dev, len + 2);
85262306a36Sopenharmony_ci
85362306a36Sopenharmony_ci			if (copy_skb == NULL) {
85462306a36Sopenharmony_ci				drops++;
85562306a36Sopenharmony_ci				goto drop_it;
85662306a36Sopenharmony_ci			}
85762306a36Sopenharmony_ci			skb_reserve(copy_skb, 2);
85862306a36Sopenharmony_ci			skb_put(copy_skb, len);
85962306a36Sopenharmony_ci			dma_sync_single_for_cpu(&bp->bigmac_op->dev,
86062306a36Sopenharmony_ci						this->rx_addr, len,
86162306a36Sopenharmony_ci						DMA_FROM_DEVICE);
86262306a36Sopenharmony_ci			skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
86362306a36Sopenharmony_ci			dma_sync_single_for_device(&bp->bigmac_op->dev,
86462306a36Sopenharmony_ci						   this->rx_addr, len,
86562306a36Sopenharmony_ci						   DMA_FROM_DEVICE);
86662306a36Sopenharmony_ci
86762306a36Sopenharmony_ci			/* Reuse original ring buffer. */
86862306a36Sopenharmony_ci			this->rx_flags =
86962306a36Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
87062306a36Sopenharmony_ci
87162306a36Sopenharmony_ci			skb = copy_skb;
87262306a36Sopenharmony_ci		}
87362306a36Sopenharmony_ci
87462306a36Sopenharmony_ci		/* No checksums done by the BigMAC ;-( */
87562306a36Sopenharmony_ci		skb->protocol = eth_type_trans(skb, bp->dev);
87662306a36Sopenharmony_ci		netif_rx(skb);
87762306a36Sopenharmony_ci		bp->dev->stats.rx_packets++;
87862306a36Sopenharmony_ci		bp->dev->stats.rx_bytes += len;
87962306a36Sopenharmony_ci	next:
88062306a36Sopenharmony_ci		elem = NEXT_RX(elem);
88162306a36Sopenharmony_ci		this = &rxbase[elem];
88262306a36Sopenharmony_ci	}
88362306a36Sopenharmony_ci	bp->rx_new = elem;
88462306a36Sopenharmony_ci	if (drops)
88562306a36Sopenharmony_ci		printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
88662306a36Sopenharmony_ci}
88762306a36Sopenharmony_ci
88862306a36Sopenharmony_cistatic irqreturn_t bigmac_interrupt(int irq, void *dev_id)
88962306a36Sopenharmony_ci{
89062306a36Sopenharmony_ci	struct bigmac *bp = (struct bigmac *) dev_id;
89162306a36Sopenharmony_ci	u32 qec_status, bmac_status;
89262306a36Sopenharmony_ci
89362306a36Sopenharmony_ci	DIRQ(("bigmac_interrupt: "));
89462306a36Sopenharmony_ci
89562306a36Sopenharmony_ci	/* Latch status registers now. */
89662306a36Sopenharmony_ci	bmac_status = sbus_readl(bp->creg + CREG_STAT);
89762306a36Sopenharmony_ci	qec_status = sbus_readl(bp->gregs + GLOB_STAT);
89862306a36Sopenharmony_ci
89962306a36Sopenharmony_ci	DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
90062306a36Sopenharmony_ci	if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
90162306a36Sopenharmony_ci	   (bmac_status & CREG_STAT_ERRORS))
90262306a36Sopenharmony_ci		bigmac_is_medium_rare(bp, qec_status, bmac_status);
90362306a36Sopenharmony_ci
90462306a36Sopenharmony_ci	if (bmac_status & CREG_STAT_TXIRQ)
90562306a36Sopenharmony_ci		bigmac_tx(bp);
90662306a36Sopenharmony_ci
90762306a36Sopenharmony_ci	if (bmac_status & CREG_STAT_RXIRQ)
90862306a36Sopenharmony_ci		bigmac_rx(bp);
90962306a36Sopenharmony_ci
91062306a36Sopenharmony_ci	return IRQ_HANDLED;
91162306a36Sopenharmony_ci}
91262306a36Sopenharmony_ci
91362306a36Sopenharmony_cistatic int bigmac_open(struct net_device *dev)
91462306a36Sopenharmony_ci{
91562306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
91662306a36Sopenharmony_ci	int ret;
91762306a36Sopenharmony_ci
91862306a36Sopenharmony_ci	ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
91962306a36Sopenharmony_ci	if (ret) {
92062306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
92162306a36Sopenharmony_ci		return ret;
92262306a36Sopenharmony_ci	}
92362306a36Sopenharmony_ci	timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
92462306a36Sopenharmony_ci	ret = bigmac_init_hw(bp, false);
92562306a36Sopenharmony_ci	if (ret)
92662306a36Sopenharmony_ci		free_irq(dev->irq, bp);
92762306a36Sopenharmony_ci	return ret;
92862306a36Sopenharmony_ci}
92962306a36Sopenharmony_ci
93062306a36Sopenharmony_cistatic int bigmac_close(struct net_device *dev)
93162306a36Sopenharmony_ci{
93262306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
93362306a36Sopenharmony_ci
93462306a36Sopenharmony_ci	del_timer(&bp->bigmac_timer);
93562306a36Sopenharmony_ci	bp->timer_state = asleep;
93662306a36Sopenharmony_ci	bp->timer_ticks = 0;
93762306a36Sopenharmony_ci
93862306a36Sopenharmony_ci	bigmac_stop(bp);
93962306a36Sopenharmony_ci	bigmac_clean_rings(bp);
94062306a36Sopenharmony_ci	free_irq(dev->irq, bp);
94162306a36Sopenharmony_ci	return 0;
94262306a36Sopenharmony_ci}
94362306a36Sopenharmony_ci
94462306a36Sopenharmony_cistatic void bigmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
94562306a36Sopenharmony_ci{
94662306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
94762306a36Sopenharmony_ci
94862306a36Sopenharmony_ci	bigmac_init_hw(bp, true);
94962306a36Sopenharmony_ci	netif_wake_queue(dev);
95062306a36Sopenharmony_ci}
95162306a36Sopenharmony_ci
95262306a36Sopenharmony_ci/* Put a packet on the wire. */
95362306a36Sopenharmony_cistatic netdev_tx_t
95462306a36Sopenharmony_cibigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
95562306a36Sopenharmony_ci{
95662306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
95762306a36Sopenharmony_ci	int len, entry;
95862306a36Sopenharmony_ci	u32 mapping;
95962306a36Sopenharmony_ci
96062306a36Sopenharmony_ci	len = skb->len;
96162306a36Sopenharmony_ci	mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
96262306a36Sopenharmony_ci				 len, DMA_TO_DEVICE);
96362306a36Sopenharmony_ci
96462306a36Sopenharmony_ci	/* Avoid a race... */
96562306a36Sopenharmony_ci	spin_lock_irq(&bp->lock);
96662306a36Sopenharmony_ci	entry = bp->tx_new;
96762306a36Sopenharmony_ci	DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
96862306a36Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
96962306a36Sopenharmony_ci	bp->tx_skbs[entry] = skb;
97062306a36Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_addr = mapping;
97162306a36Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_flags =
97262306a36Sopenharmony_ci		(TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
97362306a36Sopenharmony_ci	bp->tx_new = NEXT_TX(entry);
97462306a36Sopenharmony_ci	if (TX_BUFFS_AVAIL(bp) <= 0)
97562306a36Sopenharmony_ci		netif_stop_queue(dev);
97662306a36Sopenharmony_ci	spin_unlock_irq(&bp->lock);
97762306a36Sopenharmony_ci
97862306a36Sopenharmony_ci	/* Get it going. */
97962306a36Sopenharmony_ci	sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
98062306a36Sopenharmony_ci
98162306a36Sopenharmony_ci
98262306a36Sopenharmony_ci	return NETDEV_TX_OK;
98362306a36Sopenharmony_ci}
98462306a36Sopenharmony_ci
98562306a36Sopenharmony_cistatic struct net_device_stats *bigmac_get_stats(struct net_device *dev)
98662306a36Sopenharmony_ci{
98762306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
98862306a36Sopenharmony_ci
98962306a36Sopenharmony_ci	bigmac_get_counters(bp, bp->bregs);
99062306a36Sopenharmony_ci	return &dev->stats;
99162306a36Sopenharmony_ci}
99262306a36Sopenharmony_ci
99362306a36Sopenharmony_cistatic void bigmac_set_multicast(struct net_device *dev)
99462306a36Sopenharmony_ci{
99562306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
99662306a36Sopenharmony_ci	void __iomem *bregs = bp->bregs;
99762306a36Sopenharmony_ci	struct netdev_hw_addr *ha;
99862306a36Sopenharmony_ci	u32 tmp, crc;
99962306a36Sopenharmony_ci
100062306a36Sopenharmony_ci	/* Disable the receiver.  The bit self-clears when
100162306a36Sopenharmony_ci	 * the operation is complete.
100262306a36Sopenharmony_ci	 */
100362306a36Sopenharmony_ci	tmp = sbus_readl(bregs + BMAC_RXCFG);
100462306a36Sopenharmony_ci	tmp &= ~(BIGMAC_RXCFG_ENABLE);
100562306a36Sopenharmony_ci	sbus_writel(tmp, bregs + BMAC_RXCFG);
100662306a36Sopenharmony_ci	while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
100762306a36Sopenharmony_ci		udelay(20);
100862306a36Sopenharmony_ci
100962306a36Sopenharmony_ci	if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
101062306a36Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE0);
101162306a36Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE1);
101262306a36Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE2);
101362306a36Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE3);
101462306a36Sopenharmony_ci	} else if (dev->flags & IFF_PROMISC) {
101562306a36Sopenharmony_ci		tmp = sbus_readl(bregs + BMAC_RXCFG);
101662306a36Sopenharmony_ci		tmp |= BIGMAC_RXCFG_PMISC;
101762306a36Sopenharmony_ci		sbus_writel(tmp, bregs + BMAC_RXCFG);
101862306a36Sopenharmony_ci	} else {
101962306a36Sopenharmony_ci		u16 hash_table[4] = { 0 };
102062306a36Sopenharmony_ci
102162306a36Sopenharmony_ci		netdev_for_each_mc_addr(ha, dev) {
102262306a36Sopenharmony_ci			crc = ether_crc_le(6, ha->addr);
102362306a36Sopenharmony_ci			crc >>= 26;
102462306a36Sopenharmony_ci			hash_table[crc >> 4] |= 1 << (crc & 0xf);
102562306a36Sopenharmony_ci		}
102662306a36Sopenharmony_ci		sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
102762306a36Sopenharmony_ci		sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
102862306a36Sopenharmony_ci		sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
102962306a36Sopenharmony_ci		sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
103062306a36Sopenharmony_ci	}
103162306a36Sopenharmony_ci
103262306a36Sopenharmony_ci	/* Re-enable the receiver. */
103362306a36Sopenharmony_ci	tmp = sbus_readl(bregs + BMAC_RXCFG);
103462306a36Sopenharmony_ci	tmp |= BIGMAC_RXCFG_ENABLE;
103562306a36Sopenharmony_ci	sbus_writel(tmp, bregs + BMAC_RXCFG);
103662306a36Sopenharmony_ci}
103762306a36Sopenharmony_ci
103862306a36Sopenharmony_ci/* Ethtool support... */
103962306a36Sopenharmony_cistatic void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
104062306a36Sopenharmony_ci{
104162306a36Sopenharmony_ci	strscpy(info->driver, "sunbmac", sizeof(info->driver));
104262306a36Sopenharmony_ci	strscpy(info->version, "2.0", sizeof(info->version));
104362306a36Sopenharmony_ci}
104462306a36Sopenharmony_ci
104562306a36Sopenharmony_cistatic u32 bigmac_get_link(struct net_device *dev)
104662306a36Sopenharmony_ci{
104762306a36Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
104862306a36Sopenharmony_ci
104962306a36Sopenharmony_ci	spin_lock_irq(&bp->lock);
105062306a36Sopenharmony_ci	bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
105162306a36Sopenharmony_ci	spin_unlock_irq(&bp->lock);
105262306a36Sopenharmony_ci
105362306a36Sopenharmony_ci	return (bp->sw_bmsr & BMSR_LSTATUS);
105462306a36Sopenharmony_ci}
105562306a36Sopenharmony_ci
105662306a36Sopenharmony_cistatic const struct ethtool_ops bigmac_ethtool_ops = {
105762306a36Sopenharmony_ci	.get_drvinfo		= bigmac_get_drvinfo,
105862306a36Sopenharmony_ci	.get_link		= bigmac_get_link,
105962306a36Sopenharmony_ci};
106062306a36Sopenharmony_ci
106162306a36Sopenharmony_cistatic const struct net_device_ops bigmac_ops = {
106262306a36Sopenharmony_ci	.ndo_open		= bigmac_open,
106362306a36Sopenharmony_ci	.ndo_stop		= bigmac_close,
106462306a36Sopenharmony_ci	.ndo_start_xmit		= bigmac_start_xmit,
106562306a36Sopenharmony_ci	.ndo_get_stats		= bigmac_get_stats,
106662306a36Sopenharmony_ci	.ndo_set_rx_mode	= bigmac_set_multicast,
106762306a36Sopenharmony_ci	.ndo_tx_timeout		= bigmac_tx_timeout,
106862306a36Sopenharmony_ci	.ndo_set_mac_address	= eth_mac_addr,
106962306a36Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
107062306a36Sopenharmony_ci};
107162306a36Sopenharmony_ci
107262306a36Sopenharmony_cistatic int bigmac_ether_init(struct platform_device *op,
107362306a36Sopenharmony_ci			     struct platform_device *qec_op)
107462306a36Sopenharmony_ci{
107562306a36Sopenharmony_ci	static int version_printed;
107662306a36Sopenharmony_ci	struct net_device *dev;
107762306a36Sopenharmony_ci	u8 bsizes, bsizes_more;
107862306a36Sopenharmony_ci	struct bigmac *bp;
107962306a36Sopenharmony_ci
108062306a36Sopenharmony_ci	/* Get a new device struct for this interface. */
108162306a36Sopenharmony_ci	dev = alloc_etherdev(sizeof(struct bigmac));
108262306a36Sopenharmony_ci	if (!dev)
108362306a36Sopenharmony_ci		return -ENOMEM;
108462306a36Sopenharmony_ci
108562306a36Sopenharmony_ci	if (version_printed++ == 0)
108662306a36Sopenharmony_ci		printk(KERN_INFO "%s", version);
108762306a36Sopenharmony_ci
108862306a36Sopenharmony_ci	eth_hw_addr_set(dev, idprom->id_ethaddr);
108962306a36Sopenharmony_ci
109062306a36Sopenharmony_ci	/* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
109162306a36Sopenharmony_ci	bp = netdev_priv(dev);
109262306a36Sopenharmony_ci	bp->qec_op = qec_op;
109362306a36Sopenharmony_ci	bp->bigmac_op = op;
109462306a36Sopenharmony_ci
109562306a36Sopenharmony_ci	SET_NETDEV_DEV(dev, &op->dev);
109662306a36Sopenharmony_ci
109762306a36Sopenharmony_ci	spin_lock_init(&bp->lock);
109862306a36Sopenharmony_ci
109962306a36Sopenharmony_ci	/* Map in QEC global control registers. */
110062306a36Sopenharmony_ci	bp->gregs = of_ioremap(&qec_op->resource[0], 0,
110162306a36Sopenharmony_ci			       GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
110262306a36Sopenharmony_ci	if (!bp->gregs) {
110362306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
110462306a36Sopenharmony_ci		goto fail_and_cleanup;
110562306a36Sopenharmony_ci	}
110662306a36Sopenharmony_ci
110762306a36Sopenharmony_ci	/* Make sure QEC is in BigMAC mode. */
110862306a36Sopenharmony_ci	if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
110962306a36Sopenharmony_ci		printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
111062306a36Sopenharmony_ci		goto fail_and_cleanup;
111162306a36Sopenharmony_ci	}
111262306a36Sopenharmony_ci
111362306a36Sopenharmony_ci	/* Reset the QEC. */
111462306a36Sopenharmony_ci	if (qec_global_reset(bp->gregs))
111562306a36Sopenharmony_ci		goto fail_and_cleanup;
111662306a36Sopenharmony_ci
111762306a36Sopenharmony_ci	/* Get supported SBUS burst sizes. */
111862306a36Sopenharmony_ci	bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
111962306a36Sopenharmony_ci	bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
112062306a36Sopenharmony_ci
112162306a36Sopenharmony_ci	bsizes &= 0xff;
112262306a36Sopenharmony_ci	if (bsizes_more != 0xff)
112362306a36Sopenharmony_ci		bsizes &= bsizes_more;
112462306a36Sopenharmony_ci	if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
112562306a36Sopenharmony_ci	    (bsizes & DMA_BURST32) == 0)
112662306a36Sopenharmony_ci		bsizes = (DMA_BURST32 - 1);
112762306a36Sopenharmony_ci	bp->bigmac_bursts = bsizes;
112862306a36Sopenharmony_ci
112962306a36Sopenharmony_ci	/* Perform QEC initialization. */
113062306a36Sopenharmony_ci	qec_init(bp);
113162306a36Sopenharmony_ci
113262306a36Sopenharmony_ci	/* Map in the BigMAC channel registers. */
113362306a36Sopenharmony_ci	bp->creg = of_ioremap(&op->resource[0], 0,
113462306a36Sopenharmony_ci			      CREG_REG_SIZE, "BigMAC QEC Channel Regs");
113562306a36Sopenharmony_ci	if (!bp->creg) {
113662306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
113762306a36Sopenharmony_ci		goto fail_and_cleanup;
113862306a36Sopenharmony_ci	}
113962306a36Sopenharmony_ci
114062306a36Sopenharmony_ci	/* Map in the BigMAC control registers. */
114162306a36Sopenharmony_ci	bp->bregs = of_ioremap(&op->resource[1], 0,
114262306a36Sopenharmony_ci			       BMAC_REG_SIZE, "BigMAC Primary Regs");
114362306a36Sopenharmony_ci	if (!bp->bregs) {
114462306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
114562306a36Sopenharmony_ci		goto fail_and_cleanup;
114662306a36Sopenharmony_ci	}
114762306a36Sopenharmony_ci
114862306a36Sopenharmony_ci	/* Map in the BigMAC transceiver registers, this is how you poke at
114962306a36Sopenharmony_ci	 * the BigMAC's PHY.
115062306a36Sopenharmony_ci	 */
115162306a36Sopenharmony_ci	bp->tregs = of_ioremap(&op->resource[2], 0,
115262306a36Sopenharmony_ci			       TCVR_REG_SIZE, "BigMAC Transceiver Regs");
115362306a36Sopenharmony_ci	if (!bp->tregs) {
115462306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
115562306a36Sopenharmony_ci		goto fail_and_cleanup;
115662306a36Sopenharmony_ci	}
115762306a36Sopenharmony_ci
115862306a36Sopenharmony_ci	/* Stop the BigMAC. */
115962306a36Sopenharmony_ci	bigmac_stop(bp);
116062306a36Sopenharmony_ci
116162306a36Sopenharmony_ci	/* Allocate transmit/receive descriptor DVMA block. */
116262306a36Sopenharmony_ci	bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
116362306a36Sopenharmony_ci					    PAGE_SIZE,
116462306a36Sopenharmony_ci					    &bp->bblock_dvma, GFP_ATOMIC);
116562306a36Sopenharmony_ci	if (bp->bmac_block == NULL || bp->bblock_dvma == 0)
116662306a36Sopenharmony_ci		goto fail_and_cleanup;
116762306a36Sopenharmony_ci
116862306a36Sopenharmony_ci	/* Get the board revision of this BigMAC. */
116962306a36Sopenharmony_ci	bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
117062306a36Sopenharmony_ci					      "board-version", 1);
117162306a36Sopenharmony_ci
117262306a36Sopenharmony_ci	/* Init auto-negotiation timer state. */
117362306a36Sopenharmony_ci	timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
117462306a36Sopenharmony_ci	bp->timer_state = asleep;
117562306a36Sopenharmony_ci	bp->timer_ticks = 0;
117662306a36Sopenharmony_ci
117762306a36Sopenharmony_ci	/* Backlink to generic net device struct. */
117862306a36Sopenharmony_ci	bp->dev = dev;
117962306a36Sopenharmony_ci
118062306a36Sopenharmony_ci	/* Set links to our BigMAC open and close routines. */
118162306a36Sopenharmony_ci	dev->ethtool_ops = &bigmac_ethtool_ops;
118262306a36Sopenharmony_ci	dev->netdev_ops = &bigmac_ops;
118362306a36Sopenharmony_ci	dev->watchdog_timeo = 5*HZ;
118462306a36Sopenharmony_ci
118562306a36Sopenharmony_ci	/* Finish net device registration. */
118662306a36Sopenharmony_ci	dev->irq = bp->bigmac_op->archdata.irqs[0];
118762306a36Sopenharmony_ci	dev->dma = 0;
118862306a36Sopenharmony_ci
118962306a36Sopenharmony_ci	if (register_netdev(dev)) {
119062306a36Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot register device.\n");
119162306a36Sopenharmony_ci		goto fail_and_cleanup;
119262306a36Sopenharmony_ci	}
119362306a36Sopenharmony_ci
119462306a36Sopenharmony_ci	dev_set_drvdata(&bp->bigmac_op->dev, bp);
119562306a36Sopenharmony_ci
119662306a36Sopenharmony_ci	printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
119762306a36Sopenharmony_ci	       dev->name, dev->dev_addr);
119862306a36Sopenharmony_ci
119962306a36Sopenharmony_ci	return 0;
120062306a36Sopenharmony_ci
120162306a36Sopenharmony_cifail_and_cleanup:
120262306a36Sopenharmony_ci	/* Something went wrong, undo whatever we did so far. */
120362306a36Sopenharmony_ci	/* Free register mappings if any. */
120462306a36Sopenharmony_ci	if (bp->gregs)
120562306a36Sopenharmony_ci		of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
120662306a36Sopenharmony_ci	if (bp->creg)
120762306a36Sopenharmony_ci		of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
120862306a36Sopenharmony_ci	if (bp->bregs)
120962306a36Sopenharmony_ci		of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
121062306a36Sopenharmony_ci	if (bp->tregs)
121162306a36Sopenharmony_ci		of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
121262306a36Sopenharmony_ci
121362306a36Sopenharmony_ci	if (bp->bmac_block)
121462306a36Sopenharmony_ci		dma_free_coherent(&bp->bigmac_op->dev,
121562306a36Sopenharmony_ci				  PAGE_SIZE,
121662306a36Sopenharmony_ci				  bp->bmac_block,
121762306a36Sopenharmony_ci				  bp->bblock_dvma);
121862306a36Sopenharmony_ci
121962306a36Sopenharmony_ci	/* This also frees the co-located private data */
122062306a36Sopenharmony_ci	free_netdev(dev);
122162306a36Sopenharmony_ci	return -ENODEV;
122262306a36Sopenharmony_ci}
122362306a36Sopenharmony_ci
122462306a36Sopenharmony_ci/* QEC can be the parent of either QuadEthernet or a BigMAC.  We want
122562306a36Sopenharmony_ci * the latter.
122662306a36Sopenharmony_ci */
122762306a36Sopenharmony_cistatic int bigmac_sbus_probe(struct platform_device *op)
122862306a36Sopenharmony_ci{
122962306a36Sopenharmony_ci	struct device *parent = op->dev.parent;
123062306a36Sopenharmony_ci	struct platform_device *qec_op;
123162306a36Sopenharmony_ci
123262306a36Sopenharmony_ci	qec_op = to_platform_device(parent);
123362306a36Sopenharmony_ci
123462306a36Sopenharmony_ci	return bigmac_ether_init(op, qec_op);
123562306a36Sopenharmony_ci}
123662306a36Sopenharmony_ci
123762306a36Sopenharmony_cistatic int bigmac_sbus_remove(struct platform_device *op)
123862306a36Sopenharmony_ci{
123962306a36Sopenharmony_ci	struct bigmac *bp = platform_get_drvdata(op);
124062306a36Sopenharmony_ci	struct device *parent = op->dev.parent;
124162306a36Sopenharmony_ci	struct net_device *net_dev = bp->dev;
124262306a36Sopenharmony_ci	struct platform_device *qec_op;
124362306a36Sopenharmony_ci
124462306a36Sopenharmony_ci	qec_op = to_platform_device(parent);
124562306a36Sopenharmony_ci
124662306a36Sopenharmony_ci	unregister_netdev(net_dev);
124762306a36Sopenharmony_ci
124862306a36Sopenharmony_ci	of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
124962306a36Sopenharmony_ci	of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
125062306a36Sopenharmony_ci	of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
125162306a36Sopenharmony_ci	of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
125262306a36Sopenharmony_ci	dma_free_coherent(&op->dev,
125362306a36Sopenharmony_ci			  PAGE_SIZE,
125462306a36Sopenharmony_ci			  bp->bmac_block,
125562306a36Sopenharmony_ci			  bp->bblock_dvma);
125662306a36Sopenharmony_ci
125762306a36Sopenharmony_ci	free_netdev(net_dev);
125862306a36Sopenharmony_ci
125962306a36Sopenharmony_ci	return 0;
126062306a36Sopenharmony_ci}
126162306a36Sopenharmony_ci
126262306a36Sopenharmony_cistatic const struct of_device_id bigmac_sbus_match[] = {
126362306a36Sopenharmony_ci	{
126462306a36Sopenharmony_ci		.name = "be",
126562306a36Sopenharmony_ci	},
126662306a36Sopenharmony_ci	{},
126762306a36Sopenharmony_ci};
126862306a36Sopenharmony_ci
126962306a36Sopenharmony_ciMODULE_DEVICE_TABLE(of, bigmac_sbus_match);
127062306a36Sopenharmony_ci
127162306a36Sopenharmony_cistatic struct platform_driver bigmac_sbus_driver = {
127262306a36Sopenharmony_ci	.driver = {
127362306a36Sopenharmony_ci		.name = "sunbmac",
127462306a36Sopenharmony_ci		.of_match_table = bigmac_sbus_match,
127562306a36Sopenharmony_ci	},
127662306a36Sopenharmony_ci	.probe		= bigmac_sbus_probe,
127762306a36Sopenharmony_ci	.remove		= bigmac_sbus_remove,
127862306a36Sopenharmony_ci};
127962306a36Sopenharmony_ci
128062306a36Sopenharmony_cimodule_platform_driver(bigmac_sbus_driver);
1281