18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0
28c2ecf20Sopenharmony_ci/* sunbmac.c: Driver for Sparc BigMAC 100baseT ethernet adapters.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Copyright (C) 1997, 1998, 1999, 2003, 2008 David S. Miller (davem@davemloft.net)
58c2ecf20Sopenharmony_ci */
68c2ecf20Sopenharmony_ci
78c2ecf20Sopenharmony_ci#include <linux/module.h>
88c2ecf20Sopenharmony_ci#include <linux/pgtable.h>
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#include <linux/kernel.h>
118c2ecf20Sopenharmony_ci#include <linux/types.h>
128c2ecf20Sopenharmony_ci#include <linux/fcntl.h>
138c2ecf20Sopenharmony_ci#include <linux/interrupt.h>
148c2ecf20Sopenharmony_ci#include <linux/ioport.h>
158c2ecf20Sopenharmony_ci#include <linux/in.h>
168c2ecf20Sopenharmony_ci#include <linux/string.h>
178c2ecf20Sopenharmony_ci#include <linux/delay.h>
188c2ecf20Sopenharmony_ci#include <linux/crc32.h>
198c2ecf20Sopenharmony_ci#include <linux/errno.h>
208c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
218c2ecf20Sopenharmony_ci#include <linux/mii.h>
228c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
238c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
248c2ecf20Sopenharmony_ci#include <linux/skbuff.h>
258c2ecf20Sopenharmony_ci#include <linux/bitops.h>
268c2ecf20Sopenharmony_ci#include <linux/dma-mapping.h>
278c2ecf20Sopenharmony_ci#include <linux/of.h>
288c2ecf20Sopenharmony_ci#include <linux/of_device.h>
298c2ecf20Sopenharmony_ci#include <linux/gfp.h>
308c2ecf20Sopenharmony_ci
318c2ecf20Sopenharmony_ci#include <asm/auxio.h>
328c2ecf20Sopenharmony_ci#include <asm/byteorder.h>
338c2ecf20Sopenharmony_ci#include <asm/dma.h>
348c2ecf20Sopenharmony_ci#include <asm/idprom.h>
358c2ecf20Sopenharmony_ci#include <asm/io.h>
368c2ecf20Sopenharmony_ci#include <asm/openprom.h>
378c2ecf20Sopenharmony_ci#include <asm/oplib.h>
388c2ecf20Sopenharmony_ci
398c2ecf20Sopenharmony_ci#include "sunbmac.h"
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci#define DRV_NAME	"sunbmac"
428c2ecf20Sopenharmony_ci#define DRV_VERSION	"2.1"
438c2ecf20Sopenharmony_ci#define DRV_RELDATE	"August 26, 2008"
448c2ecf20Sopenharmony_ci#define DRV_AUTHOR	"David S. Miller (davem@davemloft.net)"
458c2ecf20Sopenharmony_ci
468c2ecf20Sopenharmony_cistatic char version[] =
478c2ecf20Sopenharmony_ci	DRV_NAME ".c:v" DRV_VERSION " " DRV_RELDATE " " DRV_AUTHOR "\n";
488c2ecf20Sopenharmony_ci
498c2ecf20Sopenharmony_ciMODULE_VERSION(DRV_VERSION);
508c2ecf20Sopenharmony_ciMODULE_AUTHOR(DRV_AUTHOR);
518c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("Sun BigMAC 100baseT ethernet driver");
528c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL");
538c2ecf20Sopenharmony_ci
548c2ecf20Sopenharmony_ci#undef DEBUG_PROBE
558c2ecf20Sopenharmony_ci#undef DEBUG_TX
568c2ecf20Sopenharmony_ci#undef DEBUG_IRQ
578c2ecf20Sopenharmony_ci
588c2ecf20Sopenharmony_ci#ifdef DEBUG_PROBE
598c2ecf20Sopenharmony_ci#define DP(x)  printk x
608c2ecf20Sopenharmony_ci#else
618c2ecf20Sopenharmony_ci#define DP(x)
628c2ecf20Sopenharmony_ci#endif
638c2ecf20Sopenharmony_ci
648c2ecf20Sopenharmony_ci#ifdef DEBUG_TX
658c2ecf20Sopenharmony_ci#define DTX(x)  printk x
668c2ecf20Sopenharmony_ci#else
678c2ecf20Sopenharmony_ci#define DTX(x)
688c2ecf20Sopenharmony_ci#endif
698c2ecf20Sopenharmony_ci
708c2ecf20Sopenharmony_ci#ifdef DEBUG_IRQ
718c2ecf20Sopenharmony_ci#define DIRQ(x)  printk x
728c2ecf20Sopenharmony_ci#else
738c2ecf20Sopenharmony_ci#define DIRQ(x)
748c2ecf20Sopenharmony_ci#endif
758c2ecf20Sopenharmony_ci
768c2ecf20Sopenharmony_ci#define DEFAULT_JAMSIZE    4 /* Toe jam */
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci#define QEC_RESET_TRIES 200
798c2ecf20Sopenharmony_ci
808c2ecf20Sopenharmony_cistatic int qec_global_reset(void __iomem *gregs)
818c2ecf20Sopenharmony_ci{
828c2ecf20Sopenharmony_ci	int tries = QEC_RESET_TRIES;
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci	sbus_writel(GLOB_CTRL_RESET, gregs + GLOB_CTRL);
858c2ecf20Sopenharmony_ci	while (--tries) {
868c2ecf20Sopenharmony_ci		if (sbus_readl(gregs + GLOB_CTRL) & GLOB_CTRL_RESET) {
878c2ecf20Sopenharmony_ci			udelay(20);
888c2ecf20Sopenharmony_ci			continue;
898c2ecf20Sopenharmony_ci		}
908c2ecf20Sopenharmony_ci		break;
918c2ecf20Sopenharmony_ci	}
928c2ecf20Sopenharmony_ci	if (tries)
938c2ecf20Sopenharmony_ci		return 0;
948c2ecf20Sopenharmony_ci	printk(KERN_ERR "BigMAC: Cannot reset the QEC.\n");
958c2ecf20Sopenharmony_ci	return -1;
968c2ecf20Sopenharmony_ci}
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_cistatic void qec_init(struct bigmac *bp)
998c2ecf20Sopenharmony_ci{
1008c2ecf20Sopenharmony_ci	struct platform_device *qec_op = bp->qec_op;
1018c2ecf20Sopenharmony_ci	void __iomem *gregs = bp->gregs;
1028c2ecf20Sopenharmony_ci	u8 bsizes = bp->bigmac_bursts;
1038c2ecf20Sopenharmony_ci	u32 regval;
1048c2ecf20Sopenharmony_ci
1058c2ecf20Sopenharmony_ci	/* 64byte bursts do not work at the moment, do
1068c2ecf20Sopenharmony_ci	 * not even try to enable them.  -DaveM
1078c2ecf20Sopenharmony_ci	 */
1088c2ecf20Sopenharmony_ci	if (bsizes & DMA_BURST32)
1098c2ecf20Sopenharmony_ci		regval = GLOB_CTRL_B32;
1108c2ecf20Sopenharmony_ci	else
1118c2ecf20Sopenharmony_ci		regval = GLOB_CTRL_B16;
1128c2ecf20Sopenharmony_ci	sbus_writel(regval | GLOB_CTRL_BMODE, gregs + GLOB_CTRL);
1138c2ecf20Sopenharmony_ci	sbus_writel(GLOB_PSIZE_2048, gregs + GLOB_PSIZE);
1148c2ecf20Sopenharmony_ci
1158c2ecf20Sopenharmony_ci	/* All of memsize is given to bigmac. */
1168c2ecf20Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]),
1178c2ecf20Sopenharmony_ci		    gregs + GLOB_MSIZE);
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	/* Half to the transmitter, half to the receiver. */
1208c2ecf20Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
1218c2ecf20Sopenharmony_ci		    gregs + GLOB_TSIZE);
1228c2ecf20Sopenharmony_ci	sbus_writel(resource_size(&qec_op->resource[1]) >> 1,
1238c2ecf20Sopenharmony_ci		    gregs + GLOB_RSIZE);
1248c2ecf20Sopenharmony_ci}
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define TX_RESET_TRIES     32
1278c2ecf20Sopenharmony_ci#define RX_RESET_TRIES     32
1288c2ecf20Sopenharmony_ci
1298c2ecf20Sopenharmony_cistatic void bigmac_tx_reset(void __iomem *bregs)
1308c2ecf20Sopenharmony_ci{
1318c2ecf20Sopenharmony_ci	int tries = TX_RESET_TRIES;
1328c2ecf20Sopenharmony_ci
1338c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_TXCFG);
1348c2ecf20Sopenharmony_ci
1358c2ecf20Sopenharmony_ci	/* The fifo threshold bit is read-only and does
1368c2ecf20Sopenharmony_ci	 * not clear.  -DaveM
1378c2ecf20Sopenharmony_ci	 */
1388c2ecf20Sopenharmony_ci	while ((sbus_readl(bregs + BMAC_TXCFG) & ~(BIGMAC_TXCFG_FIFO)) != 0 &&
1398c2ecf20Sopenharmony_ci	       --tries != 0)
1408c2ecf20Sopenharmony_ci		udelay(20);
1418c2ecf20Sopenharmony_ci
1428c2ecf20Sopenharmony_ci	if (!tries) {
1438c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Transmitter will not reset.\n");
1448c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: tx_cfg is %08x\n",
1458c2ecf20Sopenharmony_ci		       sbus_readl(bregs + BMAC_TXCFG));
1468c2ecf20Sopenharmony_ci	}
1478c2ecf20Sopenharmony_ci}
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_cistatic void bigmac_rx_reset(void __iomem *bregs)
1508c2ecf20Sopenharmony_ci{
1518c2ecf20Sopenharmony_ci	int tries = RX_RESET_TRIES;
1528c2ecf20Sopenharmony_ci
1538c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_RXCFG);
1548c2ecf20Sopenharmony_ci	while (sbus_readl(bregs + BMAC_RXCFG) && --tries)
1558c2ecf20Sopenharmony_ci		udelay(20);
1568c2ecf20Sopenharmony_ci
1578c2ecf20Sopenharmony_ci	if (!tries) {
1588c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Receiver will not reset.\n");
1598c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: rx_cfg is %08x\n",
1608c2ecf20Sopenharmony_ci		       sbus_readl(bregs + BMAC_RXCFG));
1618c2ecf20Sopenharmony_ci	}
1628c2ecf20Sopenharmony_ci}
1638c2ecf20Sopenharmony_ci
1648c2ecf20Sopenharmony_ci/* Reset the transmitter and receiver. */
1658c2ecf20Sopenharmony_cistatic void bigmac_stop(struct bigmac *bp)
1668c2ecf20Sopenharmony_ci{
1678c2ecf20Sopenharmony_ci	bigmac_tx_reset(bp->bregs);
1688c2ecf20Sopenharmony_ci	bigmac_rx_reset(bp->bregs);
1698c2ecf20Sopenharmony_ci}
1708c2ecf20Sopenharmony_ci
1718c2ecf20Sopenharmony_cistatic void bigmac_get_counters(struct bigmac *bp, void __iomem *bregs)
1728c2ecf20Sopenharmony_ci{
1738c2ecf20Sopenharmony_ci	struct net_device_stats *stats = &bp->dev->stats;
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_ci	stats->rx_crc_errors += sbus_readl(bregs + BMAC_RCRCECTR);
1768c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_RCRCECTR);
1778c2ecf20Sopenharmony_ci
1788c2ecf20Sopenharmony_ci	stats->rx_frame_errors += sbus_readl(bregs + BMAC_UNALECTR);
1798c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_UNALECTR);
1808c2ecf20Sopenharmony_ci
1818c2ecf20Sopenharmony_ci	stats->rx_length_errors += sbus_readl(bregs + BMAC_GLECTR);
1828c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_GLECTR);
1838c2ecf20Sopenharmony_ci
1848c2ecf20Sopenharmony_ci	stats->tx_aborted_errors += sbus_readl(bregs + BMAC_EXCTR);
1858c2ecf20Sopenharmony_ci
1868c2ecf20Sopenharmony_ci	stats->collisions +=
1878c2ecf20Sopenharmony_ci		(sbus_readl(bregs + BMAC_EXCTR) +
1888c2ecf20Sopenharmony_ci		 sbus_readl(bregs + BMAC_LTCTR));
1898c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_EXCTR);
1908c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_LTCTR);
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_cistatic void bigmac_clean_rings(struct bigmac *bp)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	int i;
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
1988c2ecf20Sopenharmony_ci		if (bp->rx_skbs[i] != NULL) {
1998c2ecf20Sopenharmony_ci			dev_kfree_skb_any(bp->rx_skbs[i]);
2008c2ecf20Sopenharmony_ci			bp->rx_skbs[i] = NULL;
2018c2ecf20Sopenharmony_ci		}
2028c2ecf20Sopenharmony_ci	}
2038c2ecf20Sopenharmony_ci
2048c2ecf20Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++) {
2058c2ecf20Sopenharmony_ci		if (bp->tx_skbs[i] != NULL) {
2068c2ecf20Sopenharmony_ci			dev_kfree_skb_any(bp->tx_skbs[i]);
2078c2ecf20Sopenharmony_ci			bp->tx_skbs[i] = NULL;
2088c2ecf20Sopenharmony_ci		}
2098c2ecf20Sopenharmony_ci	}
2108c2ecf20Sopenharmony_ci}
2118c2ecf20Sopenharmony_ci
2128c2ecf20Sopenharmony_cistatic void bigmac_init_rings(struct bigmac *bp, bool non_blocking)
2138c2ecf20Sopenharmony_ci{
2148c2ecf20Sopenharmony_ci	struct bmac_init_block *bb = bp->bmac_block;
2158c2ecf20Sopenharmony_ci	int i;
2168c2ecf20Sopenharmony_ci	gfp_t gfp_flags = GFP_KERNEL;
2178c2ecf20Sopenharmony_ci
2188c2ecf20Sopenharmony_ci	if (non_blocking)
2198c2ecf20Sopenharmony_ci		gfp_flags = GFP_ATOMIC;
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_ci	bp->rx_new = bp->rx_old = bp->tx_new = bp->tx_old = 0;
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ci	/* Free any skippy bufs left around in the rings. */
2248c2ecf20Sopenharmony_ci	bigmac_clean_rings(bp);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci	/* Now get new skbufs for the receive ring. */
2278c2ecf20Sopenharmony_ci	for (i = 0; i < RX_RING_SIZE; i++) {
2288c2ecf20Sopenharmony_ci		struct sk_buff *skb;
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_ci		skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, gfp_flags);
2318c2ecf20Sopenharmony_ci		if (!skb)
2328c2ecf20Sopenharmony_ci			continue;
2338c2ecf20Sopenharmony_ci
2348c2ecf20Sopenharmony_ci		bp->rx_skbs[i] = skb;
2358c2ecf20Sopenharmony_ci
2368c2ecf20Sopenharmony_ci		/* Because we reserve afterwards. */
2378c2ecf20Sopenharmony_ci		skb_put(skb, ETH_FRAME_LEN);
2388c2ecf20Sopenharmony_ci		skb_reserve(skb, 34);
2398c2ecf20Sopenharmony_ci
2408c2ecf20Sopenharmony_ci		bb->be_rxd[i].rx_addr =
2418c2ecf20Sopenharmony_ci			dma_map_single(&bp->bigmac_op->dev,
2428c2ecf20Sopenharmony_ci				       skb->data,
2438c2ecf20Sopenharmony_ci				       RX_BUF_ALLOC_SIZE - 34,
2448c2ecf20Sopenharmony_ci				       DMA_FROM_DEVICE);
2458c2ecf20Sopenharmony_ci		bb->be_rxd[i].rx_flags =
2468c2ecf20Sopenharmony_ci			(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
2478c2ecf20Sopenharmony_ci	}
2488c2ecf20Sopenharmony_ci
2498c2ecf20Sopenharmony_ci	for (i = 0; i < TX_RING_SIZE; i++)
2508c2ecf20Sopenharmony_ci		bb->be_txd[i].tx_flags = bb->be_txd[i].tx_addr = 0;
2518c2ecf20Sopenharmony_ci}
2528c2ecf20Sopenharmony_ci
2538c2ecf20Sopenharmony_ci#define MGMT_CLKON  (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB|MGMT_PAL_DCLOCK)
2548c2ecf20Sopenharmony_ci#define MGMT_CLKOFF (MGMT_PAL_INT_MDIO|MGMT_PAL_EXT_MDIO|MGMT_PAL_OENAB)
2558c2ecf20Sopenharmony_ci
2568c2ecf20Sopenharmony_cistatic void idle_transceiver(void __iomem *tregs)
2578c2ecf20Sopenharmony_ci{
2588c2ecf20Sopenharmony_ci	int i = 20;
2598c2ecf20Sopenharmony_ci
2608c2ecf20Sopenharmony_ci	while (i--) {
2618c2ecf20Sopenharmony_ci		sbus_writel(MGMT_CLKOFF, tregs + TCVR_MPAL);
2628c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2638c2ecf20Sopenharmony_ci		sbus_writel(MGMT_CLKON, tregs + TCVR_MPAL);
2648c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2658c2ecf20Sopenharmony_ci	}
2668c2ecf20Sopenharmony_ci}
2678c2ecf20Sopenharmony_ci
2688c2ecf20Sopenharmony_cistatic void write_tcvr_bit(struct bigmac *bp, void __iomem *tregs, int bit)
2698c2ecf20Sopenharmony_ci{
2708c2ecf20Sopenharmony_ci	if (bp->tcvr_type == internal) {
2718c2ecf20Sopenharmony_ci		bit = (bit & 1) << 3;
2728c2ecf20Sopenharmony_ci		sbus_writel(bit | (MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO),
2738c2ecf20Sopenharmony_ci			    tregs + TCVR_MPAL);
2748c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2758c2ecf20Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_OENAB | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
2768c2ecf20Sopenharmony_ci			    tregs + TCVR_MPAL);
2778c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2788c2ecf20Sopenharmony_ci	} else if (bp->tcvr_type == external) {
2798c2ecf20Sopenharmony_ci		bit = (bit & 1) << 2;
2808c2ecf20Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB,
2818c2ecf20Sopenharmony_ci			    tregs + TCVR_MPAL);
2828c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2838c2ecf20Sopenharmony_ci		sbus_writel(bit | MGMT_PAL_INT_MDIO | MGMT_PAL_OENAB | MGMT_PAL_DCLOCK,
2848c2ecf20Sopenharmony_ci			    tregs + TCVR_MPAL);
2858c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2868c2ecf20Sopenharmony_ci	} else {
2878c2ecf20Sopenharmony_ci		printk(KERN_ERR "write_tcvr_bit: No transceiver type known!\n");
2888c2ecf20Sopenharmony_ci	}
2898c2ecf20Sopenharmony_ci}
2908c2ecf20Sopenharmony_ci
2918c2ecf20Sopenharmony_cistatic int read_tcvr_bit(struct bigmac *bp, void __iomem *tregs)
2928c2ecf20Sopenharmony_ci{
2938c2ecf20Sopenharmony_ci	int retval = 0;
2948c2ecf20Sopenharmony_ci
2958c2ecf20Sopenharmony_ci	if (bp->tcvr_type == internal) {
2968c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
2978c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
2988c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
2998c2ecf20Sopenharmony_ci			    tregs + TCVR_MPAL);
3008c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3018c2ecf20Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
3028c2ecf20Sopenharmony_ci	} else if (bp->tcvr_type == external) {
3038c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
3048c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3058c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
3068c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3078c2ecf20Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
3088c2ecf20Sopenharmony_ci	} else {
3098c2ecf20Sopenharmony_ci		printk(KERN_ERR "read_tcvr_bit: No transceiver type known!\n");
3108c2ecf20Sopenharmony_ci	}
3118c2ecf20Sopenharmony_ci	return retval;
3128c2ecf20Sopenharmony_ci}
3138c2ecf20Sopenharmony_ci
3148c2ecf20Sopenharmony_cistatic int read_tcvr_bit2(struct bigmac *bp, void __iomem *tregs)
3158c2ecf20Sopenharmony_ci{
3168c2ecf20Sopenharmony_ci	int retval = 0;
3178c2ecf20Sopenharmony_ci
3188c2ecf20Sopenharmony_ci	if (bp->tcvr_type == internal) {
3198c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
3208c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3218c2ecf20Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_INT_MDIO) >> 3;
3228c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
3238c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3248c2ecf20Sopenharmony_ci	} else if (bp->tcvr_type == external) {
3258c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO, tregs + TCVR_MPAL);
3268c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3278c2ecf20Sopenharmony_ci		retval = (sbus_readl(tregs + TCVR_MPAL) & MGMT_PAL_EXT_MDIO) >> 2;
3288c2ecf20Sopenharmony_ci		sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_DCLOCK, tregs + TCVR_MPAL);
3298c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_MPAL);
3308c2ecf20Sopenharmony_ci	} else {
3318c2ecf20Sopenharmony_ci		printk(KERN_ERR "read_tcvr_bit2: No transceiver type known!\n");
3328c2ecf20Sopenharmony_ci	}
3338c2ecf20Sopenharmony_ci	return retval;
3348c2ecf20Sopenharmony_ci}
3358c2ecf20Sopenharmony_ci
3368c2ecf20Sopenharmony_cistatic void put_tcvr_byte(struct bigmac *bp,
3378c2ecf20Sopenharmony_ci			  void __iomem *tregs,
3388c2ecf20Sopenharmony_ci			  unsigned int byte)
3398c2ecf20Sopenharmony_ci{
3408c2ecf20Sopenharmony_ci	int shift = 4;
3418c2ecf20Sopenharmony_ci
3428c2ecf20Sopenharmony_ci	do {
3438c2ecf20Sopenharmony_ci		write_tcvr_bit(bp, tregs, ((byte >> shift) & 1));
3448c2ecf20Sopenharmony_ci		shift -= 1;
3458c2ecf20Sopenharmony_ci	} while (shift >= 0);
3468c2ecf20Sopenharmony_ci}
3478c2ecf20Sopenharmony_ci
3488c2ecf20Sopenharmony_cistatic void bigmac_tcvr_write(struct bigmac *bp, void __iomem *tregs,
3498c2ecf20Sopenharmony_ci			      int reg, unsigned short val)
3508c2ecf20Sopenharmony_ci{
3518c2ecf20Sopenharmony_ci	int shift;
3528c2ecf20Sopenharmony_ci
3538c2ecf20Sopenharmony_ci	reg &= 0xff;
3548c2ecf20Sopenharmony_ci	val &= 0xffff;
3558c2ecf20Sopenharmony_ci	switch(bp->tcvr_type) {
3568c2ecf20Sopenharmony_ci	case internal:
3578c2ecf20Sopenharmony_ci	case external:
3588c2ecf20Sopenharmony_ci		break;
3598c2ecf20Sopenharmony_ci
3608c2ecf20Sopenharmony_ci	default:
3618c2ecf20Sopenharmony_ci		printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
3628c2ecf20Sopenharmony_ci		return;
3638c2ecf20Sopenharmony_ci	}
3648c2ecf20Sopenharmony_ci
3658c2ecf20Sopenharmony_ci	idle_transceiver(tregs);
3668c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
3678c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
3688c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
3698c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
3708c2ecf20Sopenharmony_ci
3718c2ecf20Sopenharmony_ci	put_tcvr_byte(bp, tregs,
3728c2ecf20Sopenharmony_ci		      ((bp->tcvr_type == internal) ?
3738c2ecf20Sopenharmony_ci		       BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
3748c2ecf20Sopenharmony_ci
3758c2ecf20Sopenharmony_ci	put_tcvr_byte(bp, tregs, reg);
3768c2ecf20Sopenharmony_ci
3778c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
3788c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
3798c2ecf20Sopenharmony_ci
3808c2ecf20Sopenharmony_ci	shift = 15;
3818c2ecf20Sopenharmony_ci	do {
3828c2ecf20Sopenharmony_ci		write_tcvr_bit(bp, tregs, (val >> shift) & 1);
3838c2ecf20Sopenharmony_ci		shift -= 1;
3848c2ecf20Sopenharmony_ci	} while (shift >= 0);
3858c2ecf20Sopenharmony_ci}
3868c2ecf20Sopenharmony_ci
3878c2ecf20Sopenharmony_cistatic unsigned short bigmac_tcvr_read(struct bigmac *bp,
3888c2ecf20Sopenharmony_ci				       void __iomem *tregs,
3898c2ecf20Sopenharmony_ci				       int reg)
3908c2ecf20Sopenharmony_ci{
3918c2ecf20Sopenharmony_ci	unsigned short retval = 0;
3928c2ecf20Sopenharmony_ci
3938c2ecf20Sopenharmony_ci	reg &= 0xff;
3948c2ecf20Sopenharmony_ci	switch(bp->tcvr_type) {
3958c2ecf20Sopenharmony_ci	case internal:
3968c2ecf20Sopenharmony_ci	case external:
3978c2ecf20Sopenharmony_ci		break;
3988c2ecf20Sopenharmony_ci
3998c2ecf20Sopenharmony_ci	default:
4008c2ecf20Sopenharmony_ci		printk(KERN_ERR "bigmac_tcvr_read: Whoops, no known transceiver type.\n");
4018c2ecf20Sopenharmony_ci		return 0xffff;
4028c2ecf20Sopenharmony_ci	}
4038c2ecf20Sopenharmony_ci
4048c2ecf20Sopenharmony_ci	idle_transceiver(tregs);
4058c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
4068c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
4078c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 1);
4088c2ecf20Sopenharmony_ci	write_tcvr_bit(bp, tregs, 0);
4098c2ecf20Sopenharmony_ci
4108c2ecf20Sopenharmony_ci	put_tcvr_byte(bp, tregs,
4118c2ecf20Sopenharmony_ci		      ((bp->tcvr_type == internal) ?
4128c2ecf20Sopenharmony_ci		       BIGMAC_PHY_INTERNAL : BIGMAC_PHY_EXTERNAL));
4138c2ecf20Sopenharmony_ci
4148c2ecf20Sopenharmony_ci	put_tcvr_byte(bp, tregs, reg);
4158c2ecf20Sopenharmony_ci
4168c2ecf20Sopenharmony_ci	if (bp->tcvr_type == external) {
4178c2ecf20Sopenharmony_ci		int shift = 15;
4188c2ecf20Sopenharmony_ci
4198c2ecf20Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
4208c2ecf20Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
4218c2ecf20Sopenharmony_ci
4228c2ecf20Sopenharmony_ci		do {
4238c2ecf20Sopenharmony_ci			int tmp;
4248c2ecf20Sopenharmony_ci
4258c2ecf20Sopenharmony_ci			tmp = read_tcvr_bit2(bp, tregs);
4268c2ecf20Sopenharmony_ci			retval |= ((tmp & 1) << shift);
4278c2ecf20Sopenharmony_ci			shift -= 1;
4288c2ecf20Sopenharmony_ci		} while (shift >= 0);
4298c2ecf20Sopenharmony_ci
4308c2ecf20Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
4318c2ecf20Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
4328c2ecf20Sopenharmony_ci		(void) read_tcvr_bit2(bp, tregs);
4338c2ecf20Sopenharmony_ci	} else {
4348c2ecf20Sopenharmony_ci		int shift = 15;
4358c2ecf20Sopenharmony_ci
4368c2ecf20Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
4378c2ecf20Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
4388c2ecf20Sopenharmony_ci
4398c2ecf20Sopenharmony_ci		do {
4408c2ecf20Sopenharmony_ci			int tmp;
4418c2ecf20Sopenharmony_ci
4428c2ecf20Sopenharmony_ci			tmp = read_tcvr_bit(bp, tregs);
4438c2ecf20Sopenharmony_ci			retval |= ((tmp & 1) << shift);
4448c2ecf20Sopenharmony_ci			shift -= 1;
4458c2ecf20Sopenharmony_ci		} while (shift >= 0);
4468c2ecf20Sopenharmony_ci
4478c2ecf20Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
4488c2ecf20Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
4498c2ecf20Sopenharmony_ci		(void) read_tcvr_bit(bp, tregs);
4508c2ecf20Sopenharmony_ci	}
4518c2ecf20Sopenharmony_ci	return retval;
4528c2ecf20Sopenharmony_ci}
4538c2ecf20Sopenharmony_ci
4548c2ecf20Sopenharmony_cistatic void bigmac_tcvr_init(struct bigmac *bp)
4558c2ecf20Sopenharmony_ci{
4568c2ecf20Sopenharmony_ci	void __iomem *tregs = bp->tregs;
4578c2ecf20Sopenharmony_ci	u32 mpal;
4588c2ecf20Sopenharmony_ci
4598c2ecf20Sopenharmony_ci	idle_transceiver(tregs);
4608c2ecf20Sopenharmony_ci	sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO | MGMT_PAL_DCLOCK,
4618c2ecf20Sopenharmony_ci		    tregs + TCVR_MPAL);
4628c2ecf20Sopenharmony_ci	sbus_readl(tregs + TCVR_MPAL);
4638c2ecf20Sopenharmony_ci
4648c2ecf20Sopenharmony_ci	/* Only the bit for the present transceiver (internal or
4658c2ecf20Sopenharmony_ci	 * external) will stick, set them both and see what stays.
4668c2ecf20Sopenharmony_ci	 */
4678c2ecf20Sopenharmony_ci	sbus_writel(MGMT_PAL_INT_MDIO | MGMT_PAL_EXT_MDIO, tregs + TCVR_MPAL);
4688c2ecf20Sopenharmony_ci	sbus_readl(tregs + TCVR_MPAL);
4698c2ecf20Sopenharmony_ci	udelay(20);
4708c2ecf20Sopenharmony_ci
4718c2ecf20Sopenharmony_ci	mpal = sbus_readl(tregs + TCVR_MPAL);
4728c2ecf20Sopenharmony_ci	if (mpal & MGMT_PAL_EXT_MDIO) {
4738c2ecf20Sopenharmony_ci		bp->tcvr_type = external;
4748c2ecf20Sopenharmony_ci		sbus_writel(~(TCVR_PAL_EXTLBACK | TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
4758c2ecf20Sopenharmony_ci			    tregs + TCVR_TPAL);
4768c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_TPAL);
4778c2ecf20Sopenharmony_ci	} else if (mpal & MGMT_PAL_INT_MDIO) {
4788c2ecf20Sopenharmony_ci		bp->tcvr_type = internal;
4798c2ecf20Sopenharmony_ci		sbus_writel(~(TCVR_PAL_SERIAL | TCVR_PAL_EXTLBACK |
4808c2ecf20Sopenharmony_ci			      TCVR_PAL_MSENSE | TCVR_PAL_LTENABLE),
4818c2ecf20Sopenharmony_ci			    tregs + TCVR_TPAL);
4828c2ecf20Sopenharmony_ci		sbus_readl(tregs + TCVR_TPAL);
4838c2ecf20Sopenharmony_ci	} else {
4848c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: AIEEE, neither internal nor "
4858c2ecf20Sopenharmony_ci		       "external MDIO available!\n");
4868c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: mgmt_pal[%08x] tcvr_pal[%08x]\n",
4878c2ecf20Sopenharmony_ci		       sbus_readl(tregs + TCVR_MPAL),
4888c2ecf20Sopenharmony_ci		       sbus_readl(tregs + TCVR_TPAL));
4898c2ecf20Sopenharmony_ci	}
4908c2ecf20Sopenharmony_ci}
4918c2ecf20Sopenharmony_ci
4928c2ecf20Sopenharmony_cistatic int bigmac_init_hw(struct bigmac *, bool);
4938c2ecf20Sopenharmony_ci
4948c2ecf20Sopenharmony_cistatic int try_next_permutation(struct bigmac *bp, void __iomem *tregs)
4958c2ecf20Sopenharmony_ci{
4968c2ecf20Sopenharmony_ci	if (bp->sw_bmcr & BMCR_SPEED100) {
4978c2ecf20Sopenharmony_ci		int timeout;
4988c2ecf20Sopenharmony_ci
4998c2ecf20Sopenharmony_ci		/* Reset the PHY. */
5008c2ecf20Sopenharmony_ci		bp->sw_bmcr	= (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
5018c2ecf20Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
5028c2ecf20Sopenharmony_ci		bp->sw_bmcr	= (BMCR_RESET);
5038c2ecf20Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
5048c2ecf20Sopenharmony_ci
5058c2ecf20Sopenharmony_ci		timeout = 64;
5068c2ecf20Sopenharmony_ci		while (--timeout) {
5078c2ecf20Sopenharmony_ci			bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
5088c2ecf20Sopenharmony_ci			if ((bp->sw_bmcr & BMCR_RESET) == 0)
5098c2ecf20Sopenharmony_ci				break;
5108c2ecf20Sopenharmony_ci			udelay(20);
5118c2ecf20Sopenharmony_ci		}
5128c2ecf20Sopenharmony_ci		if (timeout == 0)
5138c2ecf20Sopenharmony_ci			printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
5148c2ecf20Sopenharmony_ci
5158c2ecf20Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
5168c2ecf20Sopenharmony_ci
5178c2ecf20Sopenharmony_ci		/* Now we try 10baseT. */
5188c2ecf20Sopenharmony_ci		bp->sw_bmcr &= ~(BMCR_SPEED100);
5198c2ecf20Sopenharmony_ci		bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
5208c2ecf20Sopenharmony_ci		return 0;
5218c2ecf20Sopenharmony_ci	}
5228c2ecf20Sopenharmony_ci
5238c2ecf20Sopenharmony_ci	/* We've tried them all. */
5248c2ecf20Sopenharmony_ci	return -1;
5258c2ecf20Sopenharmony_ci}
5268c2ecf20Sopenharmony_ci
5278c2ecf20Sopenharmony_cistatic void bigmac_timer(struct timer_list *t)
5288c2ecf20Sopenharmony_ci{
5298c2ecf20Sopenharmony_ci	struct bigmac *bp = from_timer(bp, t, bigmac_timer);
5308c2ecf20Sopenharmony_ci	void __iomem *tregs = bp->tregs;
5318c2ecf20Sopenharmony_ci	int restart_timer = 0;
5328c2ecf20Sopenharmony_ci
5338c2ecf20Sopenharmony_ci	bp->timer_ticks++;
5348c2ecf20Sopenharmony_ci	if (bp->timer_state == ltrywait) {
5358c2ecf20Sopenharmony_ci		bp->sw_bmsr = bigmac_tcvr_read(bp, tregs, MII_BMSR);
5368c2ecf20Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
5378c2ecf20Sopenharmony_ci		if (bp->sw_bmsr & BMSR_LSTATUS) {
5388c2ecf20Sopenharmony_ci			printk(KERN_INFO "%s: Link is now up at %s.\n",
5398c2ecf20Sopenharmony_ci			       bp->dev->name,
5408c2ecf20Sopenharmony_ci			       (bp->sw_bmcr & BMCR_SPEED100) ?
5418c2ecf20Sopenharmony_ci			       "100baseT" : "10baseT");
5428c2ecf20Sopenharmony_ci			bp->timer_state = asleep;
5438c2ecf20Sopenharmony_ci			restart_timer = 0;
5448c2ecf20Sopenharmony_ci		} else {
5458c2ecf20Sopenharmony_ci			if (bp->timer_ticks >= 4) {
5468c2ecf20Sopenharmony_ci				int ret;
5478c2ecf20Sopenharmony_ci
5488c2ecf20Sopenharmony_ci				ret = try_next_permutation(bp, tregs);
5498c2ecf20Sopenharmony_ci				if (ret == -1) {
5508c2ecf20Sopenharmony_ci					printk(KERN_ERR "%s: Link down, cable problem?\n",
5518c2ecf20Sopenharmony_ci					       bp->dev->name);
5528c2ecf20Sopenharmony_ci					ret = bigmac_init_hw(bp, true);
5538c2ecf20Sopenharmony_ci					if (ret) {
5548c2ecf20Sopenharmony_ci						printk(KERN_ERR "%s: Error, cannot re-init the "
5558c2ecf20Sopenharmony_ci						       "BigMAC.\n", bp->dev->name);
5568c2ecf20Sopenharmony_ci					}
5578c2ecf20Sopenharmony_ci					return;
5588c2ecf20Sopenharmony_ci				}
5598c2ecf20Sopenharmony_ci				bp->timer_ticks = 0;
5608c2ecf20Sopenharmony_ci				restart_timer = 1;
5618c2ecf20Sopenharmony_ci			} else {
5628c2ecf20Sopenharmony_ci				restart_timer = 1;
5638c2ecf20Sopenharmony_ci			}
5648c2ecf20Sopenharmony_ci		}
5658c2ecf20Sopenharmony_ci	} else {
5668c2ecf20Sopenharmony_ci		/* Can't happens.... */
5678c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: Aieee, link timer is asleep but we got one anyways!\n",
5688c2ecf20Sopenharmony_ci		       bp->dev->name);
5698c2ecf20Sopenharmony_ci		restart_timer = 0;
5708c2ecf20Sopenharmony_ci		bp->timer_ticks = 0;
5718c2ecf20Sopenharmony_ci		bp->timer_state = asleep; /* foo on you */
5728c2ecf20Sopenharmony_ci	}
5738c2ecf20Sopenharmony_ci
5748c2ecf20Sopenharmony_ci	if (restart_timer != 0) {
5758c2ecf20Sopenharmony_ci		bp->bigmac_timer.expires = jiffies + ((12 * HZ)/10); /* 1.2 sec. */
5768c2ecf20Sopenharmony_ci		add_timer(&bp->bigmac_timer);
5778c2ecf20Sopenharmony_ci	}
5788c2ecf20Sopenharmony_ci}
5798c2ecf20Sopenharmony_ci
5808c2ecf20Sopenharmony_ci/* Well, really we just force the chip into 100baseT then
5818c2ecf20Sopenharmony_ci * 10baseT, each time checking for a link status.
5828c2ecf20Sopenharmony_ci */
5838c2ecf20Sopenharmony_cistatic void bigmac_begin_auto_negotiation(struct bigmac *bp)
5848c2ecf20Sopenharmony_ci{
5858c2ecf20Sopenharmony_ci	void __iomem *tregs = bp->tregs;
5868c2ecf20Sopenharmony_ci	int timeout;
5878c2ecf20Sopenharmony_ci
5888c2ecf20Sopenharmony_ci	/* Grab new software copies of PHY registers. */
5898c2ecf20Sopenharmony_ci	bp->sw_bmsr	= bigmac_tcvr_read(bp, tregs, MII_BMSR);
5908c2ecf20Sopenharmony_ci	bp->sw_bmcr	= bigmac_tcvr_read(bp, tregs, MII_BMCR);
5918c2ecf20Sopenharmony_ci
5928c2ecf20Sopenharmony_ci	/* Reset the PHY. */
5938c2ecf20Sopenharmony_ci	bp->sw_bmcr	= (BMCR_ISOLATE | BMCR_PDOWN | BMCR_LOOPBACK);
5948c2ecf20Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
5958c2ecf20Sopenharmony_ci	bp->sw_bmcr	= (BMCR_RESET);
5968c2ecf20Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
5978c2ecf20Sopenharmony_ci
5988c2ecf20Sopenharmony_ci	timeout = 64;
5998c2ecf20Sopenharmony_ci	while (--timeout) {
6008c2ecf20Sopenharmony_ci		bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
6018c2ecf20Sopenharmony_ci		if ((bp->sw_bmcr & BMCR_RESET) == 0)
6028c2ecf20Sopenharmony_ci			break;
6038c2ecf20Sopenharmony_ci		udelay(20);
6048c2ecf20Sopenharmony_ci	}
6058c2ecf20Sopenharmony_ci	if (timeout == 0)
6068c2ecf20Sopenharmony_ci		printk(KERN_ERR "%s: PHY reset failed.\n", bp->dev->name);
6078c2ecf20Sopenharmony_ci
6088c2ecf20Sopenharmony_ci	bp->sw_bmcr = bigmac_tcvr_read(bp, tregs, MII_BMCR);
6098c2ecf20Sopenharmony_ci
6108c2ecf20Sopenharmony_ci	/* First we try 100baseT. */
6118c2ecf20Sopenharmony_ci	bp->sw_bmcr |= BMCR_SPEED100;
6128c2ecf20Sopenharmony_ci	bigmac_tcvr_write(bp, tregs, MII_BMCR, bp->sw_bmcr);
6138c2ecf20Sopenharmony_ci
6148c2ecf20Sopenharmony_ci	bp->timer_state = ltrywait;
6158c2ecf20Sopenharmony_ci	bp->timer_ticks = 0;
6168c2ecf20Sopenharmony_ci	bp->bigmac_timer.expires = jiffies + (12 * HZ) / 10;
6178c2ecf20Sopenharmony_ci	add_timer(&bp->bigmac_timer);
6188c2ecf20Sopenharmony_ci}
6198c2ecf20Sopenharmony_ci
6208c2ecf20Sopenharmony_cistatic int bigmac_init_hw(struct bigmac *bp, bool non_blocking)
6218c2ecf20Sopenharmony_ci{
6228c2ecf20Sopenharmony_ci	void __iomem *gregs        = bp->gregs;
6238c2ecf20Sopenharmony_ci	void __iomem *cregs        = bp->creg;
6248c2ecf20Sopenharmony_ci	void __iomem *bregs        = bp->bregs;
6258c2ecf20Sopenharmony_ci	__u32 bblk_dvma = (__u32)bp->bblock_dvma;
6268c2ecf20Sopenharmony_ci	unsigned char *e = &bp->dev->dev_addr[0];
6278c2ecf20Sopenharmony_ci
6288c2ecf20Sopenharmony_ci	/* Latch current counters into statistics. */
6298c2ecf20Sopenharmony_ci	bigmac_get_counters(bp, bregs);
6308c2ecf20Sopenharmony_ci
6318c2ecf20Sopenharmony_ci	/* Reset QEC. */
6328c2ecf20Sopenharmony_ci	qec_global_reset(gregs);
6338c2ecf20Sopenharmony_ci
6348c2ecf20Sopenharmony_ci	/* Init QEC. */
6358c2ecf20Sopenharmony_ci	qec_init(bp);
6368c2ecf20Sopenharmony_ci
6378c2ecf20Sopenharmony_ci	/* Alloc and reset the tx/rx descriptor chains. */
6388c2ecf20Sopenharmony_ci	bigmac_init_rings(bp, non_blocking);
6398c2ecf20Sopenharmony_ci
6408c2ecf20Sopenharmony_ci	/* Initialize the PHY. */
6418c2ecf20Sopenharmony_ci	bigmac_tcvr_init(bp);
6428c2ecf20Sopenharmony_ci
6438c2ecf20Sopenharmony_ci	/* Stop transmitter and receiver. */
6448c2ecf20Sopenharmony_ci	bigmac_stop(bp);
6458c2ecf20Sopenharmony_ci
6468c2ecf20Sopenharmony_ci	/* Set hardware ethernet address. */
6478c2ecf20Sopenharmony_ci	sbus_writel(((e[4] << 8) | e[5]), bregs + BMAC_MACADDR2);
6488c2ecf20Sopenharmony_ci	sbus_writel(((e[2] << 8) | e[3]), bregs + BMAC_MACADDR1);
6498c2ecf20Sopenharmony_ci	sbus_writel(((e[0] << 8) | e[1]), bregs + BMAC_MACADDR0);
6508c2ecf20Sopenharmony_ci
6518c2ecf20Sopenharmony_ci	/* Clear the hash table until mc upload occurs. */
6528c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE3);
6538c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE2);
6548c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE1);
6558c2ecf20Sopenharmony_ci	sbus_writel(0, bregs + BMAC_HTABLE0);
6568c2ecf20Sopenharmony_ci
6578c2ecf20Sopenharmony_ci	/* Enable Big Mac hash table filter. */
6588c2ecf20Sopenharmony_ci	sbus_writel(BIGMAC_RXCFG_HENABLE | BIGMAC_RXCFG_FIFO,
6598c2ecf20Sopenharmony_ci		    bregs + BMAC_RXCFG);
6608c2ecf20Sopenharmony_ci	udelay(20);
6618c2ecf20Sopenharmony_ci
6628c2ecf20Sopenharmony_ci	/* Ok, configure the Big Mac transmitter. */
6638c2ecf20Sopenharmony_ci	sbus_writel(BIGMAC_TXCFG_FIFO, bregs + BMAC_TXCFG);
6648c2ecf20Sopenharmony_ci
6658c2ecf20Sopenharmony_ci	/* The HME docs recommend to use the 10LSB of our MAC here. */
6668c2ecf20Sopenharmony_ci	sbus_writel(((e[5] | e[4] << 8) & 0x3ff),
6678c2ecf20Sopenharmony_ci		    bregs + BMAC_RSEED);
6688c2ecf20Sopenharmony_ci
6698c2ecf20Sopenharmony_ci	/* Enable the output drivers no matter what. */
6708c2ecf20Sopenharmony_ci	sbus_writel(BIGMAC_XCFG_ODENABLE | BIGMAC_XCFG_RESV,
6718c2ecf20Sopenharmony_ci		    bregs + BMAC_XIFCFG);
6728c2ecf20Sopenharmony_ci
6738c2ecf20Sopenharmony_ci	/* Tell the QEC where the ring descriptors are. */
6748c2ecf20Sopenharmony_ci	sbus_writel(bblk_dvma + bib_offset(be_rxd, 0),
6758c2ecf20Sopenharmony_ci		    cregs + CREG_RXDS);
6768c2ecf20Sopenharmony_ci	sbus_writel(bblk_dvma + bib_offset(be_txd, 0),
6778c2ecf20Sopenharmony_ci		    cregs + CREG_TXDS);
6788c2ecf20Sopenharmony_ci
6798c2ecf20Sopenharmony_ci	/* Setup the FIFO pointers into QEC local memory. */
6808c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_RXRBUFPTR);
6818c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_RXWBUFPTR);
6828c2ecf20Sopenharmony_ci	sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
6838c2ecf20Sopenharmony_ci		    cregs + CREG_TXRBUFPTR);
6848c2ecf20Sopenharmony_ci	sbus_writel(sbus_readl(gregs + GLOB_RSIZE),
6858c2ecf20Sopenharmony_ci		    cregs + CREG_TXWBUFPTR);
6868c2ecf20Sopenharmony_ci
6878c2ecf20Sopenharmony_ci	/* Tell bigmac what interrupts we don't want to hear about. */
6888c2ecf20Sopenharmony_ci	sbus_writel(BIGMAC_IMASK_GOTFRAME | BIGMAC_IMASK_SENTFRAME,
6898c2ecf20Sopenharmony_ci		    bregs + BMAC_IMASK);
6908c2ecf20Sopenharmony_ci
6918c2ecf20Sopenharmony_ci	/* Enable the various other irq's. */
6928c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_RIMASK);
6938c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_TIMASK);
6948c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_QMASK);
6958c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_BMASK);
6968c2ecf20Sopenharmony_ci
6978c2ecf20Sopenharmony_ci	/* Set jam size to a reasonable default. */
6988c2ecf20Sopenharmony_ci	sbus_writel(DEFAULT_JAMSIZE, bregs + BMAC_JSIZE);
6998c2ecf20Sopenharmony_ci
7008c2ecf20Sopenharmony_ci	/* Clear collision counter. */
7018c2ecf20Sopenharmony_ci	sbus_writel(0, cregs + CREG_CCNT);
7028c2ecf20Sopenharmony_ci
7038c2ecf20Sopenharmony_ci	/* Enable transmitter and receiver. */
7048c2ecf20Sopenharmony_ci	sbus_writel(sbus_readl(bregs + BMAC_TXCFG) | BIGMAC_TXCFG_ENABLE,
7058c2ecf20Sopenharmony_ci		    bregs + BMAC_TXCFG);
7068c2ecf20Sopenharmony_ci	sbus_writel(sbus_readl(bregs + BMAC_RXCFG) | BIGMAC_RXCFG_ENABLE,
7078c2ecf20Sopenharmony_ci		    bregs + BMAC_RXCFG);
7088c2ecf20Sopenharmony_ci
7098c2ecf20Sopenharmony_ci	/* Ok, start detecting link speed/duplex. */
7108c2ecf20Sopenharmony_ci	bigmac_begin_auto_negotiation(bp);
7118c2ecf20Sopenharmony_ci
7128c2ecf20Sopenharmony_ci	/* Success. */
7138c2ecf20Sopenharmony_ci	return 0;
7148c2ecf20Sopenharmony_ci}
7158c2ecf20Sopenharmony_ci
7168c2ecf20Sopenharmony_ci/* Error interrupts get sent here. */
7178c2ecf20Sopenharmony_cistatic void bigmac_is_medium_rare(struct bigmac *bp, u32 qec_status, u32 bmac_status)
7188c2ecf20Sopenharmony_ci{
7198c2ecf20Sopenharmony_ci	printk(KERN_ERR "bigmac_is_medium_rare: ");
7208c2ecf20Sopenharmony_ci	if (qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) {
7218c2ecf20Sopenharmony_ci		if (qec_status & GLOB_STAT_ER)
7228c2ecf20Sopenharmony_ci			printk("QEC_ERROR, ");
7238c2ecf20Sopenharmony_ci		if (qec_status & GLOB_STAT_BM)
7248c2ecf20Sopenharmony_ci			printk("QEC_BMAC_ERROR, ");
7258c2ecf20Sopenharmony_ci	}
7268c2ecf20Sopenharmony_ci	if (bmac_status & CREG_STAT_ERRORS) {
7278c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_BERROR)
7288c2ecf20Sopenharmony_ci			printk("BMAC_ERROR, ");
7298c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_TXDERROR)
7308c2ecf20Sopenharmony_ci			printk("TXD_ERROR, ");
7318c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_TXLERR)
7328c2ecf20Sopenharmony_ci			printk("TX_LATE_ERROR, ");
7338c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_TXPERR)
7348c2ecf20Sopenharmony_ci			printk("TX_PARITY_ERROR, ");
7358c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_TXSERR)
7368c2ecf20Sopenharmony_ci			printk("TX_SBUS_ERROR, ");
7378c2ecf20Sopenharmony_ci
7388c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_RXDROP)
7398c2ecf20Sopenharmony_ci			printk("RX_DROP_ERROR, ");
7408c2ecf20Sopenharmony_ci
7418c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_RXSMALL)
7428c2ecf20Sopenharmony_ci			printk("RX_SMALL_ERROR, ");
7438c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_RXLERR)
7448c2ecf20Sopenharmony_ci			printk("RX_LATE_ERROR, ");
7458c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_RXPERR)
7468c2ecf20Sopenharmony_ci			printk("RX_PARITY_ERROR, ");
7478c2ecf20Sopenharmony_ci		if (bmac_status & CREG_STAT_RXSERR)
7488c2ecf20Sopenharmony_ci			printk("RX_SBUS_ERROR, ");
7498c2ecf20Sopenharmony_ci	}
7508c2ecf20Sopenharmony_ci
7518c2ecf20Sopenharmony_ci	printk(" RESET\n");
7528c2ecf20Sopenharmony_ci	bigmac_init_hw(bp, true);
7538c2ecf20Sopenharmony_ci}
7548c2ecf20Sopenharmony_ci
7558c2ecf20Sopenharmony_ci/* BigMAC transmit complete service routines. */
7568c2ecf20Sopenharmony_cistatic void bigmac_tx(struct bigmac *bp)
7578c2ecf20Sopenharmony_ci{
7588c2ecf20Sopenharmony_ci	struct be_txd *txbase = &bp->bmac_block->be_txd[0];
7598c2ecf20Sopenharmony_ci	struct net_device *dev = bp->dev;
7608c2ecf20Sopenharmony_ci	int elem;
7618c2ecf20Sopenharmony_ci
7628c2ecf20Sopenharmony_ci	spin_lock(&bp->lock);
7638c2ecf20Sopenharmony_ci
7648c2ecf20Sopenharmony_ci	elem = bp->tx_old;
7658c2ecf20Sopenharmony_ci	DTX(("bigmac_tx: tx_old[%d] ", elem));
7668c2ecf20Sopenharmony_ci	while (elem != bp->tx_new) {
7678c2ecf20Sopenharmony_ci		struct sk_buff *skb;
7688c2ecf20Sopenharmony_ci		struct be_txd *this = &txbase[elem];
7698c2ecf20Sopenharmony_ci
7708c2ecf20Sopenharmony_ci		DTX(("this(%p) [flags(%08x)addr(%08x)]",
7718c2ecf20Sopenharmony_ci		     this, this->tx_flags, this->tx_addr));
7728c2ecf20Sopenharmony_ci
7738c2ecf20Sopenharmony_ci		if (this->tx_flags & TXD_OWN)
7748c2ecf20Sopenharmony_ci			break;
7758c2ecf20Sopenharmony_ci		skb = bp->tx_skbs[elem];
7768c2ecf20Sopenharmony_ci		dev->stats.tx_packets++;
7778c2ecf20Sopenharmony_ci		dev->stats.tx_bytes += skb->len;
7788c2ecf20Sopenharmony_ci		dma_unmap_single(&bp->bigmac_op->dev,
7798c2ecf20Sopenharmony_ci				 this->tx_addr, skb->len,
7808c2ecf20Sopenharmony_ci				 DMA_TO_DEVICE);
7818c2ecf20Sopenharmony_ci
7828c2ecf20Sopenharmony_ci		DTX(("skb(%p) ", skb));
7838c2ecf20Sopenharmony_ci		bp->tx_skbs[elem] = NULL;
7848c2ecf20Sopenharmony_ci		dev_consume_skb_irq(skb);
7858c2ecf20Sopenharmony_ci
7868c2ecf20Sopenharmony_ci		elem = NEXT_TX(elem);
7878c2ecf20Sopenharmony_ci	}
7888c2ecf20Sopenharmony_ci	DTX((" DONE, tx_old=%d\n", elem));
7898c2ecf20Sopenharmony_ci	bp->tx_old = elem;
7908c2ecf20Sopenharmony_ci
7918c2ecf20Sopenharmony_ci	if (netif_queue_stopped(dev) &&
7928c2ecf20Sopenharmony_ci	    TX_BUFFS_AVAIL(bp) > 0)
7938c2ecf20Sopenharmony_ci		netif_wake_queue(bp->dev);
7948c2ecf20Sopenharmony_ci
7958c2ecf20Sopenharmony_ci	spin_unlock(&bp->lock);
7968c2ecf20Sopenharmony_ci}
7978c2ecf20Sopenharmony_ci
7988c2ecf20Sopenharmony_ci/* BigMAC receive complete service routines. */
7998c2ecf20Sopenharmony_cistatic void bigmac_rx(struct bigmac *bp)
8008c2ecf20Sopenharmony_ci{
8018c2ecf20Sopenharmony_ci	struct be_rxd *rxbase = &bp->bmac_block->be_rxd[0];
8028c2ecf20Sopenharmony_ci	struct be_rxd *this;
8038c2ecf20Sopenharmony_ci	int elem = bp->rx_new, drops = 0;
8048c2ecf20Sopenharmony_ci	u32 flags;
8058c2ecf20Sopenharmony_ci
8068c2ecf20Sopenharmony_ci	this = &rxbase[elem];
8078c2ecf20Sopenharmony_ci	while (!((flags = this->rx_flags) & RXD_OWN)) {
8088c2ecf20Sopenharmony_ci		struct sk_buff *skb;
8098c2ecf20Sopenharmony_ci		int len = (flags & RXD_LENGTH); /* FCS not included */
8108c2ecf20Sopenharmony_ci
8118c2ecf20Sopenharmony_ci		/* Check for errors. */
8128c2ecf20Sopenharmony_ci		if (len < ETH_ZLEN) {
8138c2ecf20Sopenharmony_ci			bp->dev->stats.rx_errors++;
8148c2ecf20Sopenharmony_ci			bp->dev->stats.rx_length_errors++;
8158c2ecf20Sopenharmony_ci
8168c2ecf20Sopenharmony_ci	drop_it:
8178c2ecf20Sopenharmony_ci			/* Return it to the BigMAC. */
8188c2ecf20Sopenharmony_ci			bp->dev->stats.rx_dropped++;
8198c2ecf20Sopenharmony_ci			this->rx_flags =
8208c2ecf20Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
8218c2ecf20Sopenharmony_ci			goto next;
8228c2ecf20Sopenharmony_ci		}
8238c2ecf20Sopenharmony_ci		skb = bp->rx_skbs[elem];
8248c2ecf20Sopenharmony_ci		if (len > RX_COPY_THRESHOLD) {
8258c2ecf20Sopenharmony_ci			struct sk_buff *new_skb;
8268c2ecf20Sopenharmony_ci
8278c2ecf20Sopenharmony_ci			/* Now refill the entry, if we can. */
8288c2ecf20Sopenharmony_ci			new_skb = big_mac_alloc_skb(RX_BUF_ALLOC_SIZE, GFP_ATOMIC);
8298c2ecf20Sopenharmony_ci			if (new_skb == NULL) {
8308c2ecf20Sopenharmony_ci				drops++;
8318c2ecf20Sopenharmony_ci				goto drop_it;
8328c2ecf20Sopenharmony_ci			}
8338c2ecf20Sopenharmony_ci			dma_unmap_single(&bp->bigmac_op->dev,
8348c2ecf20Sopenharmony_ci					 this->rx_addr,
8358c2ecf20Sopenharmony_ci					 RX_BUF_ALLOC_SIZE - 34,
8368c2ecf20Sopenharmony_ci					 DMA_FROM_DEVICE);
8378c2ecf20Sopenharmony_ci			bp->rx_skbs[elem] = new_skb;
8388c2ecf20Sopenharmony_ci			skb_put(new_skb, ETH_FRAME_LEN);
8398c2ecf20Sopenharmony_ci			skb_reserve(new_skb, 34);
8408c2ecf20Sopenharmony_ci			this->rx_addr =
8418c2ecf20Sopenharmony_ci				dma_map_single(&bp->bigmac_op->dev,
8428c2ecf20Sopenharmony_ci					       new_skb->data,
8438c2ecf20Sopenharmony_ci					       RX_BUF_ALLOC_SIZE - 34,
8448c2ecf20Sopenharmony_ci					       DMA_FROM_DEVICE);
8458c2ecf20Sopenharmony_ci			this->rx_flags =
8468c2ecf20Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
8478c2ecf20Sopenharmony_ci
8488c2ecf20Sopenharmony_ci			/* Trim the original skb for the netif. */
8498c2ecf20Sopenharmony_ci			skb_trim(skb, len);
8508c2ecf20Sopenharmony_ci		} else {
8518c2ecf20Sopenharmony_ci			struct sk_buff *copy_skb = netdev_alloc_skb(bp->dev, len + 2);
8528c2ecf20Sopenharmony_ci
8538c2ecf20Sopenharmony_ci			if (copy_skb == NULL) {
8548c2ecf20Sopenharmony_ci				drops++;
8558c2ecf20Sopenharmony_ci				goto drop_it;
8568c2ecf20Sopenharmony_ci			}
8578c2ecf20Sopenharmony_ci			skb_reserve(copy_skb, 2);
8588c2ecf20Sopenharmony_ci			skb_put(copy_skb, len);
8598c2ecf20Sopenharmony_ci			dma_sync_single_for_cpu(&bp->bigmac_op->dev,
8608c2ecf20Sopenharmony_ci						this->rx_addr, len,
8618c2ecf20Sopenharmony_ci						DMA_FROM_DEVICE);
8628c2ecf20Sopenharmony_ci			skb_copy_to_linear_data(copy_skb, (unsigned char *)skb->data, len);
8638c2ecf20Sopenharmony_ci			dma_sync_single_for_device(&bp->bigmac_op->dev,
8648c2ecf20Sopenharmony_ci						   this->rx_addr, len,
8658c2ecf20Sopenharmony_ci						   DMA_FROM_DEVICE);
8668c2ecf20Sopenharmony_ci
8678c2ecf20Sopenharmony_ci			/* Reuse original ring buffer. */
8688c2ecf20Sopenharmony_ci			this->rx_flags =
8698c2ecf20Sopenharmony_ci				(RXD_OWN | ((RX_BUF_ALLOC_SIZE - 34) & RXD_LENGTH));
8708c2ecf20Sopenharmony_ci
8718c2ecf20Sopenharmony_ci			skb = copy_skb;
8728c2ecf20Sopenharmony_ci		}
8738c2ecf20Sopenharmony_ci
8748c2ecf20Sopenharmony_ci		/* No checksums done by the BigMAC ;-( */
8758c2ecf20Sopenharmony_ci		skb->protocol = eth_type_trans(skb, bp->dev);
8768c2ecf20Sopenharmony_ci		netif_rx(skb);
8778c2ecf20Sopenharmony_ci		bp->dev->stats.rx_packets++;
8788c2ecf20Sopenharmony_ci		bp->dev->stats.rx_bytes += len;
8798c2ecf20Sopenharmony_ci	next:
8808c2ecf20Sopenharmony_ci		elem = NEXT_RX(elem);
8818c2ecf20Sopenharmony_ci		this = &rxbase[elem];
8828c2ecf20Sopenharmony_ci	}
8838c2ecf20Sopenharmony_ci	bp->rx_new = elem;
8848c2ecf20Sopenharmony_ci	if (drops)
8858c2ecf20Sopenharmony_ci		printk(KERN_NOTICE "%s: Memory squeeze, deferring packet.\n", bp->dev->name);
8868c2ecf20Sopenharmony_ci}
8878c2ecf20Sopenharmony_ci
8888c2ecf20Sopenharmony_cistatic irqreturn_t bigmac_interrupt(int irq, void *dev_id)
8898c2ecf20Sopenharmony_ci{
8908c2ecf20Sopenharmony_ci	struct bigmac *bp = (struct bigmac *) dev_id;
8918c2ecf20Sopenharmony_ci	u32 qec_status, bmac_status;
8928c2ecf20Sopenharmony_ci
8938c2ecf20Sopenharmony_ci	DIRQ(("bigmac_interrupt: "));
8948c2ecf20Sopenharmony_ci
8958c2ecf20Sopenharmony_ci	/* Latch status registers now. */
8968c2ecf20Sopenharmony_ci	bmac_status = sbus_readl(bp->creg + CREG_STAT);
8978c2ecf20Sopenharmony_ci	qec_status = sbus_readl(bp->gregs + GLOB_STAT);
8988c2ecf20Sopenharmony_ci
8998c2ecf20Sopenharmony_ci	DIRQ(("qec_status=%08x bmac_status=%08x\n", qec_status, bmac_status));
9008c2ecf20Sopenharmony_ci	if ((qec_status & (GLOB_STAT_ER | GLOB_STAT_BM)) ||
9018c2ecf20Sopenharmony_ci	   (bmac_status & CREG_STAT_ERRORS))
9028c2ecf20Sopenharmony_ci		bigmac_is_medium_rare(bp, qec_status, bmac_status);
9038c2ecf20Sopenharmony_ci
9048c2ecf20Sopenharmony_ci	if (bmac_status & CREG_STAT_TXIRQ)
9058c2ecf20Sopenharmony_ci		bigmac_tx(bp);
9068c2ecf20Sopenharmony_ci
9078c2ecf20Sopenharmony_ci	if (bmac_status & CREG_STAT_RXIRQ)
9088c2ecf20Sopenharmony_ci		bigmac_rx(bp);
9098c2ecf20Sopenharmony_ci
9108c2ecf20Sopenharmony_ci	return IRQ_HANDLED;
9118c2ecf20Sopenharmony_ci}
9128c2ecf20Sopenharmony_ci
9138c2ecf20Sopenharmony_cistatic int bigmac_open(struct net_device *dev)
9148c2ecf20Sopenharmony_ci{
9158c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9168c2ecf20Sopenharmony_ci	int ret;
9178c2ecf20Sopenharmony_ci
9188c2ecf20Sopenharmony_ci	ret = request_irq(dev->irq, bigmac_interrupt, IRQF_SHARED, dev->name, bp);
9198c2ecf20Sopenharmony_ci	if (ret) {
9208c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Can't order irq %d to go.\n", dev->irq);
9218c2ecf20Sopenharmony_ci		return ret;
9228c2ecf20Sopenharmony_ci	}
9238c2ecf20Sopenharmony_ci	timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
9248c2ecf20Sopenharmony_ci	ret = bigmac_init_hw(bp, false);
9258c2ecf20Sopenharmony_ci	if (ret)
9268c2ecf20Sopenharmony_ci		free_irq(dev->irq, bp);
9278c2ecf20Sopenharmony_ci	return ret;
9288c2ecf20Sopenharmony_ci}
9298c2ecf20Sopenharmony_ci
9308c2ecf20Sopenharmony_cistatic int bigmac_close(struct net_device *dev)
9318c2ecf20Sopenharmony_ci{
9328c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9338c2ecf20Sopenharmony_ci
9348c2ecf20Sopenharmony_ci	del_timer(&bp->bigmac_timer);
9358c2ecf20Sopenharmony_ci	bp->timer_state = asleep;
9368c2ecf20Sopenharmony_ci	bp->timer_ticks = 0;
9378c2ecf20Sopenharmony_ci
9388c2ecf20Sopenharmony_ci	bigmac_stop(bp);
9398c2ecf20Sopenharmony_ci	bigmac_clean_rings(bp);
9408c2ecf20Sopenharmony_ci	free_irq(dev->irq, bp);
9418c2ecf20Sopenharmony_ci	return 0;
9428c2ecf20Sopenharmony_ci}
9438c2ecf20Sopenharmony_ci
9448c2ecf20Sopenharmony_cistatic void bigmac_tx_timeout(struct net_device *dev, unsigned int txqueue)
9458c2ecf20Sopenharmony_ci{
9468c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9478c2ecf20Sopenharmony_ci
9488c2ecf20Sopenharmony_ci	bigmac_init_hw(bp, true);
9498c2ecf20Sopenharmony_ci	netif_wake_queue(dev);
9508c2ecf20Sopenharmony_ci}
9518c2ecf20Sopenharmony_ci
9528c2ecf20Sopenharmony_ci/* Put a packet on the wire. */
9538c2ecf20Sopenharmony_cistatic netdev_tx_t
9548c2ecf20Sopenharmony_cibigmac_start_xmit(struct sk_buff *skb, struct net_device *dev)
9558c2ecf20Sopenharmony_ci{
9568c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9578c2ecf20Sopenharmony_ci	int len, entry;
9588c2ecf20Sopenharmony_ci	u32 mapping;
9598c2ecf20Sopenharmony_ci
9608c2ecf20Sopenharmony_ci	len = skb->len;
9618c2ecf20Sopenharmony_ci	mapping = dma_map_single(&bp->bigmac_op->dev, skb->data,
9628c2ecf20Sopenharmony_ci				 len, DMA_TO_DEVICE);
9638c2ecf20Sopenharmony_ci
9648c2ecf20Sopenharmony_ci	/* Avoid a race... */
9658c2ecf20Sopenharmony_ci	spin_lock_irq(&bp->lock);
9668c2ecf20Sopenharmony_ci	entry = bp->tx_new;
9678c2ecf20Sopenharmony_ci	DTX(("bigmac_start_xmit: len(%d) entry(%d)\n", len, entry));
9688c2ecf20Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_flags = TXD_UPDATE;
9698c2ecf20Sopenharmony_ci	bp->tx_skbs[entry] = skb;
9708c2ecf20Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_addr = mapping;
9718c2ecf20Sopenharmony_ci	bp->bmac_block->be_txd[entry].tx_flags =
9728c2ecf20Sopenharmony_ci		(TXD_OWN | TXD_SOP | TXD_EOP | (len & TXD_LENGTH));
9738c2ecf20Sopenharmony_ci	bp->tx_new = NEXT_TX(entry);
9748c2ecf20Sopenharmony_ci	if (TX_BUFFS_AVAIL(bp) <= 0)
9758c2ecf20Sopenharmony_ci		netif_stop_queue(dev);
9768c2ecf20Sopenharmony_ci	spin_unlock_irq(&bp->lock);
9778c2ecf20Sopenharmony_ci
9788c2ecf20Sopenharmony_ci	/* Get it going. */
9798c2ecf20Sopenharmony_ci	sbus_writel(CREG_CTRL_TWAKEUP, bp->creg + CREG_CTRL);
9808c2ecf20Sopenharmony_ci
9818c2ecf20Sopenharmony_ci
9828c2ecf20Sopenharmony_ci	return NETDEV_TX_OK;
9838c2ecf20Sopenharmony_ci}
9848c2ecf20Sopenharmony_ci
9858c2ecf20Sopenharmony_cistatic struct net_device_stats *bigmac_get_stats(struct net_device *dev)
9868c2ecf20Sopenharmony_ci{
9878c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9888c2ecf20Sopenharmony_ci
9898c2ecf20Sopenharmony_ci	bigmac_get_counters(bp, bp->bregs);
9908c2ecf20Sopenharmony_ci	return &dev->stats;
9918c2ecf20Sopenharmony_ci}
9928c2ecf20Sopenharmony_ci
9938c2ecf20Sopenharmony_cistatic void bigmac_set_multicast(struct net_device *dev)
9948c2ecf20Sopenharmony_ci{
9958c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
9968c2ecf20Sopenharmony_ci	void __iomem *bregs = bp->bregs;
9978c2ecf20Sopenharmony_ci	struct netdev_hw_addr *ha;
9988c2ecf20Sopenharmony_ci	u32 tmp, crc;
9998c2ecf20Sopenharmony_ci
10008c2ecf20Sopenharmony_ci	/* Disable the receiver.  The bit self-clears when
10018c2ecf20Sopenharmony_ci	 * the operation is complete.
10028c2ecf20Sopenharmony_ci	 */
10038c2ecf20Sopenharmony_ci	tmp = sbus_readl(bregs + BMAC_RXCFG);
10048c2ecf20Sopenharmony_ci	tmp &= ~(BIGMAC_RXCFG_ENABLE);
10058c2ecf20Sopenharmony_ci	sbus_writel(tmp, bregs + BMAC_RXCFG);
10068c2ecf20Sopenharmony_ci	while ((sbus_readl(bregs + BMAC_RXCFG) & BIGMAC_RXCFG_ENABLE) != 0)
10078c2ecf20Sopenharmony_ci		udelay(20);
10088c2ecf20Sopenharmony_ci
10098c2ecf20Sopenharmony_ci	if ((dev->flags & IFF_ALLMULTI) || (netdev_mc_count(dev) > 64)) {
10108c2ecf20Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE0);
10118c2ecf20Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE1);
10128c2ecf20Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE2);
10138c2ecf20Sopenharmony_ci		sbus_writel(0xffff, bregs + BMAC_HTABLE3);
10148c2ecf20Sopenharmony_ci	} else if (dev->flags & IFF_PROMISC) {
10158c2ecf20Sopenharmony_ci		tmp = sbus_readl(bregs + BMAC_RXCFG);
10168c2ecf20Sopenharmony_ci		tmp |= BIGMAC_RXCFG_PMISC;
10178c2ecf20Sopenharmony_ci		sbus_writel(tmp, bregs + BMAC_RXCFG);
10188c2ecf20Sopenharmony_ci	} else {
10198c2ecf20Sopenharmony_ci		u16 hash_table[4] = { 0 };
10208c2ecf20Sopenharmony_ci
10218c2ecf20Sopenharmony_ci		netdev_for_each_mc_addr(ha, dev) {
10228c2ecf20Sopenharmony_ci			crc = ether_crc_le(6, ha->addr);
10238c2ecf20Sopenharmony_ci			crc >>= 26;
10248c2ecf20Sopenharmony_ci			hash_table[crc >> 4] |= 1 << (crc & 0xf);
10258c2ecf20Sopenharmony_ci		}
10268c2ecf20Sopenharmony_ci		sbus_writel(hash_table[0], bregs + BMAC_HTABLE0);
10278c2ecf20Sopenharmony_ci		sbus_writel(hash_table[1], bregs + BMAC_HTABLE1);
10288c2ecf20Sopenharmony_ci		sbus_writel(hash_table[2], bregs + BMAC_HTABLE2);
10298c2ecf20Sopenharmony_ci		sbus_writel(hash_table[3], bregs + BMAC_HTABLE3);
10308c2ecf20Sopenharmony_ci	}
10318c2ecf20Sopenharmony_ci
10328c2ecf20Sopenharmony_ci	/* Re-enable the receiver. */
10338c2ecf20Sopenharmony_ci	tmp = sbus_readl(bregs + BMAC_RXCFG);
10348c2ecf20Sopenharmony_ci	tmp |= BIGMAC_RXCFG_ENABLE;
10358c2ecf20Sopenharmony_ci	sbus_writel(tmp, bregs + BMAC_RXCFG);
10368c2ecf20Sopenharmony_ci}
10378c2ecf20Sopenharmony_ci
10388c2ecf20Sopenharmony_ci/* Ethtool support... */
10398c2ecf20Sopenharmony_cistatic void bigmac_get_drvinfo(struct net_device *dev, struct ethtool_drvinfo *info)
10408c2ecf20Sopenharmony_ci{
10418c2ecf20Sopenharmony_ci	strlcpy(info->driver, "sunbmac", sizeof(info->driver));
10428c2ecf20Sopenharmony_ci	strlcpy(info->version, "2.0", sizeof(info->version));
10438c2ecf20Sopenharmony_ci}
10448c2ecf20Sopenharmony_ci
10458c2ecf20Sopenharmony_cistatic u32 bigmac_get_link(struct net_device *dev)
10468c2ecf20Sopenharmony_ci{
10478c2ecf20Sopenharmony_ci	struct bigmac *bp = netdev_priv(dev);
10488c2ecf20Sopenharmony_ci
10498c2ecf20Sopenharmony_ci	spin_lock_irq(&bp->lock);
10508c2ecf20Sopenharmony_ci	bp->sw_bmsr = bigmac_tcvr_read(bp, bp->tregs, MII_BMSR);
10518c2ecf20Sopenharmony_ci	spin_unlock_irq(&bp->lock);
10528c2ecf20Sopenharmony_ci
10538c2ecf20Sopenharmony_ci	return (bp->sw_bmsr & BMSR_LSTATUS);
10548c2ecf20Sopenharmony_ci}
10558c2ecf20Sopenharmony_ci
10568c2ecf20Sopenharmony_cistatic const struct ethtool_ops bigmac_ethtool_ops = {
10578c2ecf20Sopenharmony_ci	.get_drvinfo		= bigmac_get_drvinfo,
10588c2ecf20Sopenharmony_ci	.get_link		= bigmac_get_link,
10598c2ecf20Sopenharmony_ci};
10608c2ecf20Sopenharmony_ci
10618c2ecf20Sopenharmony_cistatic const struct net_device_ops bigmac_ops = {
10628c2ecf20Sopenharmony_ci	.ndo_open		= bigmac_open,
10638c2ecf20Sopenharmony_ci	.ndo_stop		= bigmac_close,
10648c2ecf20Sopenharmony_ci	.ndo_start_xmit		= bigmac_start_xmit,
10658c2ecf20Sopenharmony_ci	.ndo_get_stats		= bigmac_get_stats,
10668c2ecf20Sopenharmony_ci	.ndo_set_rx_mode	= bigmac_set_multicast,
10678c2ecf20Sopenharmony_ci	.ndo_tx_timeout		= bigmac_tx_timeout,
10688c2ecf20Sopenharmony_ci	.ndo_set_mac_address	= eth_mac_addr,
10698c2ecf20Sopenharmony_ci	.ndo_validate_addr	= eth_validate_addr,
10708c2ecf20Sopenharmony_ci};
10718c2ecf20Sopenharmony_ci
10728c2ecf20Sopenharmony_cistatic int bigmac_ether_init(struct platform_device *op,
10738c2ecf20Sopenharmony_ci			     struct platform_device *qec_op)
10748c2ecf20Sopenharmony_ci{
10758c2ecf20Sopenharmony_ci	static int version_printed;
10768c2ecf20Sopenharmony_ci	struct net_device *dev;
10778c2ecf20Sopenharmony_ci	u8 bsizes, bsizes_more;
10788c2ecf20Sopenharmony_ci	struct bigmac *bp;
10798c2ecf20Sopenharmony_ci	int i;
10808c2ecf20Sopenharmony_ci
10818c2ecf20Sopenharmony_ci	/* Get a new device struct for this interface. */
10828c2ecf20Sopenharmony_ci	dev = alloc_etherdev(sizeof(struct bigmac));
10838c2ecf20Sopenharmony_ci	if (!dev)
10848c2ecf20Sopenharmony_ci		return -ENOMEM;
10858c2ecf20Sopenharmony_ci
10868c2ecf20Sopenharmony_ci	if (version_printed++ == 0)
10878c2ecf20Sopenharmony_ci		printk(KERN_INFO "%s", version);
10888c2ecf20Sopenharmony_ci
10898c2ecf20Sopenharmony_ci	for (i = 0; i < 6; i++)
10908c2ecf20Sopenharmony_ci		dev->dev_addr[i] = idprom->id_ethaddr[i];
10918c2ecf20Sopenharmony_ci
10928c2ecf20Sopenharmony_ci	/* Setup softc, with backpointers to QEC and BigMAC SBUS device structs. */
10938c2ecf20Sopenharmony_ci	bp = netdev_priv(dev);
10948c2ecf20Sopenharmony_ci	bp->qec_op = qec_op;
10958c2ecf20Sopenharmony_ci	bp->bigmac_op = op;
10968c2ecf20Sopenharmony_ci
10978c2ecf20Sopenharmony_ci	SET_NETDEV_DEV(dev, &op->dev);
10988c2ecf20Sopenharmony_ci
10998c2ecf20Sopenharmony_ci	spin_lock_init(&bp->lock);
11008c2ecf20Sopenharmony_ci
11018c2ecf20Sopenharmony_ci	/* Map in QEC global control registers. */
11028c2ecf20Sopenharmony_ci	bp->gregs = of_ioremap(&qec_op->resource[0], 0,
11038c2ecf20Sopenharmony_ci			       GLOB_REG_SIZE, "BigMAC QEC GLobal Regs");
11048c2ecf20Sopenharmony_ci	if (!bp->gregs) {
11058c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map QEC global registers.\n");
11068c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11078c2ecf20Sopenharmony_ci	}
11088c2ecf20Sopenharmony_ci
11098c2ecf20Sopenharmony_ci	/* Make sure QEC is in BigMAC mode. */
11108c2ecf20Sopenharmony_ci	if ((sbus_readl(bp->gregs + GLOB_CTRL) & 0xf0000000) != GLOB_CTRL_BMODE) {
11118c2ecf20Sopenharmony_ci		printk(KERN_ERR "BigMAC: AIEEE, QEC is not in BigMAC mode!\n");
11128c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11138c2ecf20Sopenharmony_ci	}
11148c2ecf20Sopenharmony_ci
11158c2ecf20Sopenharmony_ci	/* Reset the QEC. */
11168c2ecf20Sopenharmony_ci	if (qec_global_reset(bp->gregs))
11178c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11188c2ecf20Sopenharmony_ci
11198c2ecf20Sopenharmony_ci	/* Get supported SBUS burst sizes. */
11208c2ecf20Sopenharmony_ci	bsizes = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
11218c2ecf20Sopenharmony_ci	bsizes_more = of_getintprop_default(qec_op->dev.of_node, "burst-sizes", 0xff);
11228c2ecf20Sopenharmony_ci
11238c2ecf20Sopenharmony_ci	bsizes &= 0xff;
11248c2ecf20Sopenharmony_ci	if (bsizes_more != 0xff)
11258c2ecf20Sopenharmony_ci		bsizes &= bsizes_more;
11268c2ecf20Sopenharmony_ci	if (bsizes == 0xff || (bsizes & DMA_BURST16) == 0 ||
11278c2ecf20Sopenharmony_ci	    (bsizes & DMA_BURST32) == 0)
11288c2ecf20Sopenharmony_ci		bsizes = (DMA_BURST32 - 1);
11298c2ecf20Sopenharmony_ci	bp->bigmac_bursts = bsizes;
11308c2ecf20Sopenharmony_ci
11318c2ecf20Sopenharmony_ci	/* Perform QEC initialization. */
11328c2ecf20Sopenharmony_ci	qec_init(bp);
11338c2ecf20Sopenharmony_ci
11348c2ecf20Sopenharmony_ci	/* Map in the BigMAC channel registers. */
11358c2ecf20Sopenharmony_ci	bp->creg = of_ioremap(&op->resource[0], 0,
11368c2ecf20Sopenharmony_ci			      CREG_REG_SIZE, "BigMAC QEC Channel Regs");
11378c2ecf20Sopenharmony_ci	if (!bp->creg) {
11388c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map QEC channel registers.\n");
11398c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11408c2ecf20Sopenharmony_ci	}
11418c2ecf20Sopenharmony_ci
11428c2ecf20Sopenharmony_ci	/* Map in the BigMAC control registers. */
11438c2ecf20Sopenharmony_ci	bp->bregs = of_ioremap(&op->resource[1], 0,
11448c2ecf20Sopenharmony_ci			       BMAC_REG_SIZE, "BigMAC Primary Regs");
11458c2ecf20Sopenharmony_ci	if (!bp->bregs) {
11468c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map BigMAC primary registers.\n");
11478c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11488c2ecf20Sopenharmony_ci	}
11498c2ecf20Sopenharmony_ci
11508c2ecf20Sopenharmony_ci	/* Map in the BigMAC transceiver registers, this is how you poke at
11518c2ecf20Sopenharmony_ci	 * the BigMAC's PHY.
11528c2ecf20Sopenharmony_ci	 */
11538c2ecf20Sopenharmony_ci	bp->tregs = of_ioremap(&op->resource[2], 0,
11548c2ecf20Sopenharmony_ci			       TCVR_REG_SIZE, "BigMAC Transceiver Regs");
11558c2ecf20Sopenharmony_ci	if (!bp->tregs) {
11568c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot map BigMAC transceiver registers.\n");
11578c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11588c2ecf20Sopenharmony_ci	}
11598c2ecf20Sopenharmony_ci
11608c2ecf20Sopenharmony_ci	/* Stop the BigMAC. */
11618c2ecf20Sopenharmony_ci	bigmac_stop(bp);
11628c2ecf20Sopenharmony_ci
11638c2ecf20Sopenharmony_ci	/* Allocate transmit/receive descriptor DVMA block. */
11648c2ecf20Sopenharmony_ci	bp->bmac_block = dma_alloc_coherent(&bp->bigmac_op->dev,
11658c2ecf20Sopenharmony_ci					    PAGE_SIZE,
11668c2ecf20Sopenharmony_ci					    &bp->bblock_dvma, GFP_ATOMIC);
11678c2ecf20Sopenharmony_ci	if (bp->bmac_block == NULL || bp->bblock_dvma == 0)
11688c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11698c2ecf20Sopenharmony_ci
11708c2ecf20Sopenharmony_ci	/* Get the board revision of this BigMAC. */
11718c2ecf20Sopenharmony_ci	bp->board_rev = of_getintprop_default(bp->bigmac_op->dev.of_node,
11728c2ecf20Sopenharmony_ci					      "board-version", 1);
11738c2ecf20Sopenharmony_ci
11748c2ecf20Sopenharmony_ci	/* Init auto-negotiation timer state. */
11758c2ecf20Sopenharmony_ci	timer_setup(&bp->bigmac_timer, bigmac_timer, 0);
11768c2ecf20Sopenharmony_ci	bp->timer_state = asleep;
11778c2ecf20Sopenharmony_ci	bp->timer_ticks = 0;
11788c2ecf20Sopenharmony_ci
11798c2ecf20Sopenharmony_ci	/* Backlink to generic net device struct. */
11808c2ecf20Sopenharmony_ci	bp->dev = dev;
11818c2ecf20Sopenharmony_ci
11828c2ecf20Sopenharmony_ci	/* Set links to our BigMAC open and close routines. */
11838c2ecf20Sopenharmony_ci	dev->ethtool_ops = &bigmac_ethtool_ops;
11848c2ecf20Sopenharmony_ci	dev->netdev_ops = &bigmac_ops;
11858c2ecf20Sopenharmony_ci	dev->watchdog_timeo = 5*HZ;
11868c2ecf20Sopenharmony_ci
11878c2ecf20Sopenharmony_ci	/* Finish net device registration. */
11888c2ecf20Sopenharmony_ci	dev->irq = bp->bigmac_op->archdata.irqs[0];
11898c2ecf20Sopenharmony_ci	dev->dma = 0;
11908c2ecf20Sopenharmony_ci
11918c2ecf20Sopenharmony_ci	if (register_netdev(dev)) {
11928c2ecf20Sopenharmony_ci		printk(KERN_ERR "BIGMAC: Cannot register device.\n");
11938c2ecf20Sopenharmony_ci		goto fail_and_cleanup;
11948c2ecf20Sopenharmony_ci	}
11958c2ecf20Sopenharmony_ci
11968c2ecf20Sopenharmony_ci	dev_set_drvdata(&bp->bigmac_op->dev, bp);
11978c2ecf20Sopenharmony_ci
11988c2ecf20Sopenharmony_ci	printk(KERN_INFO "%s: BigMAC 100baseT Ethernet %pM\n",
11998c2ecf20Sopenharmony_ci	       dev->name, dev->dev_addr);
12008c2ecf20Sopenharmony_ci
12018c2ecf20Sopenharmony_ci	return 0;
12028c2ecf20Sopenharmony_ci
12038c2ecf20Sopenharmony_cifail_and_cleanup:
12048c2ecf20Sopenharmony_ci	/* Something went wrong, undo whatever we did so far. */
12058c2ecf20Sopenharmony_ci	/* Free register mappings if any. */
12068c2ecf20Sopenharmony_ci	if (bp->gregs)
12078c2ecf20Sopenharmony_ci		of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
12088c2ecf20Sopenharmony_ci	if (bp->creg)
12098c2ecf20Sopenharmony_ci		of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
12108c2ecf20Sopenharmony_ci	if (bp->bregs)
12118c2ecf20Sopenharmony_ci		of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
12128c2ecf20Sopenharmony_ci	if (bp->tregs)
12138c2ecf20Sopenharmony_ci		of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
12148c2ecf20Sopenharmony_ci
12158c2ecf20Sopenharmony_ci	if (bp->bmac_block)
12168c2ecf20Sopenharmony_ci		dma_free_coherent(&bp->bigmac_op->dev,
12178c2ecf20Sopenharmony_ci				  PAGE_SIZE,
12188c2ecf20Sopenharmony_ci				  bp->bmac_block,
12198c2ecf20Sopenharmony_ci				  bp->bblock_dvma);
12208c2ecf20Sopenharmony_ci
12218c2ecf20Sopenharmony_ci	/* This also frees the co-located private data */
12228c2ecf20Sopenharmony_ci	free_netdev(dev);
12238c2ecf20Sopenharmony_ci	return -ENODEV;
12248c2ecf20Sopenharmony_ci}
12258c2ecf20Sopenharmony_ci
12268c2ecf20Sopenharmony_ci/* QEC can be the parent of either QuadEthernet or a BigMAC.  We want
12278c2ecf20Sopenharmony_ci * the latter.
12288c2ecf20Sopenharmony_ci */
12298c2ecf20Sopenharmony_cistatic int bigmac_sbus_probe(struct platform_device *op)
12308c2ecf20Sopenharmony_ci{
12318c2ecf20Sopenharmony_ci	struct device *parent = op->dev.parent;
12328c2ecf20Sopenharmony_ci	struct platform_device *qec_op;
12338c2ecf20Sopenharmony_ci
12348c2ecf20Sopenharmony_ci	qec_op = to_platform_device(parent);
12358c2ecf20Sopenharmony_ci
12368c2ecf20Sopenharmony_ci	return bigmac_ether_init(op, qec_op);
12378c2ecf20Sopenharmony_ci}
12388c2ecf20Sopenharmony_ci
12398c2ecf20Sopenharmony_cistatic int bigmac_sbus_remove(struct platform_device *op)
12408c2ecf20Sopenharmony_ci{
12418c2ecf20Sopenharmony_ci	struct bigmac *bp = platform_get_drvdata(op);
12428c2ecf20Sopenharmony_ci	struct device *parent = op->dev.parent;
12438c2ecf20Sopenharmony_ci	struct net_device *net_dev = bp->dev;
12448c2ecf20Sopenharmony_ci	struct platform_device *qec_op;
12458c2ecf20Sopenharmony_ci
12468c2ecf20Sopenharmony_ci	qec_op = to_platform_device(parent);
12478c2ecf20Sopenharmony_ci
12488c2ecf20Sopenharmony_ci	unregister_netdev(net_dev);
12498c2ecf20Sopenharmony_ci
12508c2ecf20Sopenharmony_ci	of_iounmap(&qec_op->resource[0], bp->gregs, GLOB_REG_SIZE);
12518c2ecf20Sopenharmony_ci	of_iounmap(&op->resource[0], bp->creg, CREG_REG_SIZE);
12528c2ecf20Sopenharmony_ci	of_iounmap(&op->resource[1], bp->bregs, BMAC_REG_SIZE);
12538c2ecf20Sopenharmony_ci	of_iounmap(&op->resource[2], bp->tregs, TCVR_REG_SIZE);
12548c2ecf20Sopenharmony_ci	dma_free_coherent(&op->dev,
12558c2ecf20Sopenharmony_ci			  PAGE_SIZE,
12568c2ecf20Sopenharmony_ci			  bp->bmac_block,
12578c2ecf20Sopenharmony_ci			  bp->bblock_dvma);
12588c2ecf20Sopenharmony_ci
12598c2ecf20Sopenharmony_ci	free_netdev(net_dev);
12608c2ecf20Sopenharmony_ci
12618c2ecf20Sopenharmony_ci	return 0;
12628c2ecf20Sopenharmony_ci}
12638c2ecf20Sopenharmony_ci
12648c2ecf20Sopenharmony_cistatic const struct of_device_id bigmac_sbus_match[] = {
12658c2ecf20Sopenharmony_ci	{
12668c2ecf20Sopenharmony_ci		.name = "be",
12678c2ecf20Sopenharmony_ci	},
12688c2ecf20Sopenharmony_ci	{},
12698c2ecf20Sopenharmony_ci};
12708c2ecf20Sopenharmony_ci
12718c2ecf20Sopenharmony_ciMODULE_DEVICE_TABLE(of, bigmac_sbus_match);
12728c2ecf20Sopenharmony_ci
12738c2ecf20Sopenharmony_cistatic struct platform_driver bigmac_sbus_driver = {
12748c2ecf20Sopenharmony_ci	.driver = {
12758c2ecf20Sopenharmony_ci		.name = "sunbmac",
12768c2ecf20Sopenharmony_ci		.of_match_table = bigmac_sbus_match,
12778c2ecf20Sopenharmony_ci	},
12788c2ecf20Sopenharmony_ci	.probe		= bigmac_sbus_probe,
12798c2ecf20Sopenharmony_ci	.remove		= bigmac_sbus_remove,
12808c2ecf20Sopenharmony_ci};
12818c2ecf20Sopenharmony_ci
12828c2ecf20Sopenharmony_cimodule_platform_driver(bigmac_sbus_driver);
1283