xref: /linux/drivers/net/ethernet/ibm/emac/core.c (revision 9410645520e9b820069761f3450ef6661418e279)
12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
29aa32835SJeff Kirsher /*
33396c782SPaul Gortmaker  * drivers/net/ethernet/ibm/emac/core.c
49aa32835SJeff Kirsher  *
59aa32835SJeff Kirsher  * Driver for PowerPC 4xx on-chip ethernet controller.
69aa32835SJeff Kirsher  *
79aa32835SJeff Kirsher  * Copyright 2007 Benjamin Herrenschmidt, IBM Corp.
89aa32835SJeff Kirsher  *                <benh@kernel.crashing.org>
99aa32835SJeff Kirsher  *
109aa32835SJeff Kirsher  * Based on the arch/ppc version of the driver:
119aa32835SJeff Kirsher  *
129aa32835SJeff Kirsher  * Copyright (c) 2004, 2005 Zultys Technologies.
139aa32835SJeff Kirsher  * Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>
149aa32835SJeff Kirsher  *
159aa32835SJeff Kirsher  * Based on original work by
169aa32835SJeff Kirsher  * 	Matt Porter <mporter@kernel.crashing.org>
179aa32835SJeff Kirsher  *	(c) 2003 Benjamin Herrenschmidt <benh@kernel.crashing.org>
189aa32835SJeff Kirsher  *      Armin Kuster <akuster@mvista.com>
199aa32835SJeff Kirsher  * 	Johnnie Peters <jpeters@mvista.com>
209aa32835SJeff Kirsher  */
219aa32835SJeff Kirsher 
229aa32835SJeff Kirsher #include <linux/module.h>
239aa32835SJeff Kirsher #include <linux/sched.h>
249aa32835SJeff Kirsher #include <linux/string.h>
259aa32835SJeff Kirsher #include <linux/errno.h>
269aa32835SJeff Kirsher #include <linux/delay.h>
279aa32835SJeff Kirsher #include <linux/types.h>
289aa32835SJeff Kirsher #include <linux/pci.h>
299aa32835SJeff Kirsher #include <linux/etherdevice.h>
309aa32835SJeff Kirsher #include <linux/skbuff.h>
319aa32835SJeff Kirsher #include <linux/crc32.h>
329aa32835SJeff Kirsher #include <linux/ethtool.h>
339aa32835SJeff Kirsher #include <linux/mii.h>
349aa32835SJeff Kirsher #include <linux/bitops.h>
359aa32835SJeff Kirsher #include <linux/of.h>
365af50730SRob Herring #include <linux/of_address.h>
375af50730SRob Herring #include <linux/of_irq.h>
389aa32835SJeff Kirsher #include <linux/of_net.h>
39a577ca6bSChristian Lamparter #include <linux/of_mdio.h>
403d40aed8SRob Herring #include <linux/of_platform.h>
4183c4a4eeSRob Herring #include <linux/platform_device.h>
429aa32835SJeff Kirsher #include <linux/slab.h>
439aa32835SJeff Kirsher 
449aa32835SJeff Kirsher #include <asm/processor.h>
459aa32835SJeff Kirsher #include <asm/io.h>
469aa32835SJeff Kirsher #include <asm/dma.h>
477c0f6ba6SLinus Torvalds #include <linux/uaccess.h>
489aa32835SJeff Kirsher #include <asm/dcr.h>
499aa32835SJeff Kirsher #include <asm/dcr-regs.h>
509aa32835SJeff Kirsher 
519aa32835SJeff Kirsher #include "core.h"
529aa32835SJeff Kirsher 
539aa32835SJeff Kirsher /*
549aa32835SJeff Kirsher  * Lack of dma_unmap_???? calls is intentional.
559aa32835SJeff Kirsher  *
569aa32835SJeff Kirsher  * API-correct usage requires additional support state information to be
579aa32835SJeff Kirsher  * maintained for every RX and TX buffer descriptor (BD). Unfortunately, due to
589aa32835SJeff Kirsher  * EMAC design (e.g. TX buffer passed from network stack can be split into
599aa32835SJeff Kirsher  * several BDs, dma_map_single/dma_map_page can be used to map particular BD),
609aa32835SJeff Kirsher  * maintaining such information will add additional overhead.
619aa32835SJeff Kirsher  * Current DMA API implementation for 4xx processors only ensures cache coherency
629aa32835SJeff Kirsher  * and dma_unmap_???? routines are empty and are likely to stay this way.
639aa32835SJeff Kirsher  * I decided to omit dma_unmap_??? calls because I don't want to add additional
649aa32835SJeff Kirsher  * complexity just for the sake of following some abstract API, when it doesn't
659aa32835SJeff Kirsher  * add any real benefit to the driver. I understand that this decision maybe
669aa32835SJeff Kirsher  * controversial, but I really tried to make code API-correct and efficient
679aa32835SJeff Kirsher  * at the same time and didn't come up with code I liked :(.                --ebs
689aa32835SJeff Kirsher  */
699aa32835SJeff Kirsher 
709aa32835SJeff Kirsher #define DRV_NAME        "emac"
719aa32835SJeff Kirsher #define DRV_VERSION     "3.54"
729aa32835SJeff Kirsher #define DRV_DESC        "PPC 4xx OCP EMAC driver"
739aa32835SJeff Kirsher 
749aa32835SJeff Kirsher MODULE_DESCRIPTION(DRV_DESC);
759aa32835SJeff Kirsher MODULE_AUTHOR
769aa32835SJeff Kirsher     ("Eugene Surovegin <eugene.surovegin@zultys.com> or <ebs@ebshome.net>");
779aa32835SJeff Kirsher MODULE_LICENSE("GPL");
789aa32835SJeff Kirsher 
799aa32835SJeff Kirsher /* minimum number of free TX descriptors required to wake up TX process */
809aa32835SJeff Kirsher #define EMAC_TX_WAKEUP_THRESH		(NUM_TX_BUFF / 4)
819aa32835SJeff Kirsher 
829aa32835SJeff Kirsher /* If packet size is less than this number, we allocate small skb and copy packet
839aa32835SJeff Kirsher  * contents into it instead of just sending original big skb up
849aa32835SJeff Kirsher  */
853b3bceefSTony Breeds #define EMAC_RX_COPY_THRESH		CONFIG_IBM_EMAC_RX_COPY_THRESHOLD
869aa32835SJeff Kirsher 
879aa32835SJeff Kirsher /* Since multiple EMACs share MDIO lines in various ways, we need
889aa32835SJeff Kirsher  * to avoid re-using the same PHY ID in cases where the arch didn't
899aa32835SJeff Kirsher  * setup precise phy_map entries
909aa32835SJeff Kirsher  *
919aa32835SJeff Kirsher  * XXX This is something that needs to be reworked as we can have multiple
929aa32835SJeff Kirsher  * EMAC "sets" (multiple ASICs containing several EMACs) though we can
939aa32835SJeff Kirsher  * probably require in that case to have explicit PHY IDs in the device-tree
949aa32835SJeff Kirsher  */
959aa32835SJeff Kirsher static u32 busy_phy_map;
969aa32835SJeff Kirsher static DEFINE_MUTEX(emac_phy_map_lock);
979aa32835SJeff Kirsher 
989aa32835SJeff Kirsher /* Having stable interface names is a doomed idea. However, it would be nice
999aa32835SJeff Kirsher  * if we didn't have completely random interface names at boot too :-) It's
1009aa32835SJeff Kirsher  * just a matter of making everybody's life easier. Since we are doing
1019aa32835SJeff Kirsher  * threaded probing, it's a bit harder though. The base idea here is that
1029aa32835SJeff Kirsher  * we make up a list of all emacs in the device-tree before we register the
1039aa32835SJeff Kirsher  * driver. Every emac will then wait for the previous one in the list to
1049aa32835SJeff Kirsher  * initialize before itself. We should also keep that list ordered by
1059aa32835SJeff Kirsher  * cell_index.
1069aa32835SJeff Kirsher  * That list is only 4 entries long, meaning that additional EMACs don't
1079aa32835SJeff Kirsher  * get ordering guarantees unless EMAC_BOOT_LIST_SIZE is increased.
1089aa32835SJeff Kirsher  */
1099aa32835SJeff Kirsher 
1109aa32835SJeff Kirsher #define EMAC_BOOT_LIST_SIZE	4
1119aa32835SJeff Kirsher static struct device_node *emac_boot_list[EMAC_BOOT_LIST_SIZE];
1129aa32835SJeff Kirsher 
1139aa32835SJeff Kirsher /* I don't want to litter system log with timeout errors
1149aa32835SJeff Kirsher  * when we have brain-damaged PHY.
1159aa32835SJeff Kirsher  */
emac_report_timeout_error(struct emac_instance * dev,const char * error)1169aa32835SJeff Kirsher static inline void emac_report_timeout_error(struct emac_instance *dev,
1179aa32835SJeff Kirsher 					     const char *error)
1189aa32835SJeff Kirsher {
1199aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX |
1209aa32835SJeff Kirsher 				  EMAC_FTR_460EX_PHY_CLK_FIX |
1219aa32835SJeff Kirsher 				  EMAC_FTR_440EP_PHY_CLK_FIX))
1229aa32835SJeff Kirsher 		DBG(dev, "%s" NL, error);
1239aa32835SJeff Kirsher 	else if (net_ratelimit())
124f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: %s\n", dev->ofdev->dev.of_node, error);
1259aa32835SJeff Kirsher }
1269aa32835SJeff Kirsher 
1279aa32835SJeff Kirsher /* EMAC PHY clock workaround:
1289aa32835SJeff Kirsher  * 440EP/440GR has more sane SDR0_MFR register implementation than 440GX,
1299aa32835SJeff Kirsher  * which allows controlling each EMAC clock
1309aa32835SJeff Kirsher  */
emac_rx_clk_tx(struct emac_instance * dev)1319aa32835SJeff Kirsher static inline void emac_rx_clk_tx(struct emac_instance *dev)
1329aa32835SJeff Kirsher {
1339aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
1349aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440EP_PHY_CLK_FIX))
1359aa32835SJeff Kirsher 		dcri_clrset(SDR0, SDR0_MFR,
1369aa32835SJeff Kirsher 			    0, SDR0_MFR_ECS >> dev->cell_index);
1379aa32835SJeff Kirsher #endif
1389aa32835SJeff Kirsher }
1399aa32835SJeff Kirsher 
emac_rx_clk_default(struct emac_instance * dev)1409aa32835SJeff Kirsher static inline void emac_rx_clk_default(struct emac_instance *dev)
1419aa32835SJeff Kirsher {
1429aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
1439aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440EP_PHY_CLK_FIX))
1449aa32835SJeff Kirsher 		dcri_clrset(SDR0, SDR0_MFR,
1459aa32835SJeff Kirsher 			    SDR0_MFR_ECS >> dev->cell_index, 0);
1469aa32835SJeff Kirsher #endif
1479aa32835SJeff Kirsher }
1489aa32835SJeff Kirsher 
1499aa32835SJeff Kirsher /* PHY polling intervals */
1509aa32835SJeff Kirsher #define PHY_POLL_LINK_ON	HZ
1519aa32835SJeff Kirsher #define PHY_POLL_LINK_OFF	(HZ / 5)
1529aa32835SJeff Kirsher 
1539aa32835SJeff Kirsher /* Graceful stop timeouts in us.
1549aa32835SJeff Kirsher  * We should allow up to 1 frame time (full-duplex, ignoring collisions)
1559aa32835SJeff Kirsher  */
1569aa32835SJeff Kirsher #define STOP_TIMEOUT_10		1230
1579aa32835SJeff Kirsher #define STOP_TIMEOUT_100	124
1589aa32835SJeff Kirsher #define STOP_TIMEOUT_1000	13
1599aa32835SJeff Kirsher #define STOP_TIMEOUT_1000_JUMBO	73
1609aa32835SJeff Kirsher 
1619aa32835SJeff Kirsher static unsigned char default_mcast_addr[] = {
1629aa32835SJeff Kirsher 	0x01, 0x80, 0xC2, 0x00, 0x00, 0x01
1639aa32835SJeff Kirsher };
1649aa32835SJeff Kirsher 
1659aa32835SJeff Kirsher /* Please, keep in sync with struct ibm_emac_stats/ibm_emac_error_stats */
1669aa32835SJeff Kirsher static const char emac_stats_keys[EMAC_ETHTOOL_STATS_COUNT][ETH_GSTRING_LEN] = {
1679aa32835SJeff Kirsher 	"rx_packets", "rx_bytes", "tx_packets", "tx_bytes", "rx_packets_csum",
1689aa32835SJeff Kirsher 	"tx_packets_csum", "tx_undo", "rx_dropped_stack", "rx_dropped_oom",
1699aa32835SJeff Kirsher 	"rx_dropped_error", "rx_dropped_resize", "rx_dropped_mtu",
1709aa32835SJeff Kirsher 	"rx_stopped", "rx_bd_errors", "rx_bd_overrun", "rx_bd_bad_packet",
1719aa32835SJeff Kirsher 	"rx_bd_runt_packet", "rx_bd_short_event", "rx_bd_alignment_error",
1729aa32835SJeff Kirsher 	"rx_bd_bad_fcs", "rx_bd_packet_too_long", "rx_bd_out_of_range",
1739aa32835SJeff Kirsher 	"rx_bd_in_range", "rx_parity", "rx_fifo_overrun", "rx_overrun",
1749aa32835SJeff Kirsher 	"rx_bad_packet", "rx_runt_packet", "rx_short_event",
1759aa32835SJeff Kirsher 	"rx_alignment_error", "rx_bad_fcs", "rx_packet_too_long",
1769aa32835SJeff Kirsher 	"rx_out_of_range", "rx_in_range", "tx_dropped", "tx_bd_errors",
1779aa32835SJeff Kirsher 	"tx_bd_bad_fcs", "tx_bd_carrier_loss", "tx_bd_excessive_deferral",
1789aa32835SJeff Kirsher 	"tx_bd_excessive_collisions", "tx_bd_late_collision",
1799aa32835SJeff Kirsher 	"tx_bd_multple_collisions", "tx_bd_single_collision",
1809aa32835SJeff Kirsher 	"tx_bd_underrun", "tx_bd_sqe", "tx_parity", "tx_underrun", "tx_sqe",
1819aa32835SJeff Kirsher 	"tx_errors"
1829aa32835SJeff Kirsher };
1839aa32835SJeff Kirsher 
1849aa32835SJeff Kirsher static irqreturn_t emac_irq(int irq, void *dev_instance);
1859aa32835SJeff Kirsher static void emac_clean_tx_ring(struct emac_instance *dev);
1869aa32835SJeff Kirsher static void __emac_set_multicast_list(struct emac_instance *dev);
1879aa32835SJeff Kirsher 
emac_phy_supports_gige(int phy_mode)1889aa32835SJeff Kirsher static inline int emac_phy_supports_gige(int phy_mode)
1899aa32835SJeff Kirsher {
19029635762SChristian Lamparter 	return  phy_interface_mode_is_rgmii(phy_mode) ||
19129635762SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_GMII ||
19278b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_SGMII ||
19378b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_TBI ||
19478b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_RTBI;
1959aa32835SJeff Kirsher }
1969aa32835SJeff Kirsher 
emac_phy_gpcs(int phy_mode)1979aa32835SJeff Kirsher static inline int emac_phy_gpcs(int phy_mode)
1989aa32835SJeff Kirsher {
19978b69921SChristian Lamparter 	return  phy_mode == PHY_INTERFACE_MODE_SGMII ||
20078b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_TBI ||
20178b69921SChristian Lamparter 		phy_mode == PHY_INTERFACE_MODE_RTBI;
2029aa32835SJeff Kirsher }
2039aa32835SJeff Kirsher 
emac_tx_enable(struct emac_instance * dev)2049aa32835SJeff Kirsher static inline void emac_tx_enable(struct emac_instance *dev)
2059aa32835SJeff Kirsher {
2069aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
2079aa32835SJeff Kirsher 	u32 r;
2089aa32835SJeff Kirsher 
2099aa32835SJeff Kirsher 	DBG(dev, "tx_enable" NL);
2109aa32835SJeff Kirsher 
2119aa32835SJeff Kirsher 	r = in_be32(&p->mr0);
2129aa32835SJeff Kirsher 	if (!(r & EMAC_MR0_TXE))
2139aa32835SJeff Kirsher 		out_be32(&p->mr0, r | EMAC_MR0_TXE);
2149aa32835SJeff Kirsher }
2159aa32835SJeff Kirsher 
emac_tx_disable(struct emac_instance * dev)2169aa32835SJeff Kirsher static void emac_tx_disable(struct emac_instance *dev)
2179aa32835SJeff Kirsher {
2189aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
2199aa32835SJeff Kirsher 	u32 r;
2209aa32835SJeff Kirsher 
2219aa32835SJeff Kirsher 	DBG(dev, "tx_disable" NL);
2229aa32835SJeff Kirsher 
2239aa32835SJeff Kirsher 	r = in_be32(&p->mr0);
2249aa32835SJeff Kirsher 	if (r & EMAC_MR0_TXE) {
2259aa32835SJeff Kirsher 		int n = dev->stop_timeout;
2269aa32835SJeff Kirsher 		out_be32(&p->mr0, r & ~EMAC_MR0_TXE);
2279aa32835SJeff Kirsher 		while (!(in_be32(&p->mr0) & EMAC_MR0_TXI) && n) {
2289aa32835SJeff Kirsher 			udelay(1);
2299aa32835SJeff Kirsher 			--n;
2309aa32835SJeff Kirsher 		}
2319aa32835SJeff Kirsher 		if (unlikely(!n))
2329aa32835SJeff Kirsher 			emac_report_timeout_error(dev, "TX disable timeout");
2339aa32835SJeff Kirsher 	}
2349aa32835SJeff Kirsher }
2359aa32835SJeff Kirsher 
emac_rx_enable(struct emac_instance * dev)2369aa32835SJeff Kirsher static void emac_rx_enable(struct emac_instance *dev)
2379aa32835SJeff Kirsher {
2389aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
2399aa32835SJeff Kirsher 	u32 r;
2409aa32835SJeff Kirsher 
2419aa32835SJeff Kirsher 	if (unlikely(test_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags)))
2429aa32835SJeff Kirsher 		goto out;
2439aa32835SJeff Kirsher 
2449aa32835SJeff Kirsher 	DBG(dev, "rx_enable" NL);
2459aa32835SJeff Kirsher 
2469aa32835SJeff Kirsher 	r = in_be32(&p->mr0);
2479aa32835SJeff Kirsher 	if (!(r & EMAC_MR0_RXE)) {
2489aa32835SJeff Kirsher 		if (unlikely(!(r & EMAC_MR0_RXI))) {
2499aa32835SJeff Kirsher 			/* Wait if previous async disable is still in progress */
2509aa32835SJeff Kirsher 			int n = dev->stop_timeout;
2519aa32835SJeff Kirsher 			while (!(r = in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
2529aa32835SJeff Kirsher 				udelay(1);
2539aa32835SJeff Kirsher 				--n;
2549aa32835SJeff Kirsher 			}
2559aa32835SJeff Kirsher 			if (unlikely(!n))
2569aa32835SJeff Kirsher 				emac_report_timeout_error(dev,
2579aa32835SJeff Kirsher 							  "RX disable timeout");
2589aa32835SJeff Kirsher 		}
2599aa32835SJeff Kirsher 		out_be32(&p->mr0, r | EMAC_MR0_RXE);
2609aa32835SJeff Kirsher 	}
2619aa32835SJeff Kirsher  out:
2629aa32835SJeff Kirsher 	;
2639aa32835SJeff Kirsher }
2649aa32835SJeff Kirsher 
emac_rx_disable(struct emac_instance * dev)2659aa32835SJeff Kirsher static void emac_rx_disable(struct emac_instance *dev)
2669aa32835SJeff Kirsher {
2679aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
2689aa32835SJeff Kirsher 	u32 r;
2699aa32835SJeff Kirsher 
2709aa32835SJeff Kirsher 	DBG(dev, "rx_disable" NL);
2719aa32835SJeff Kirsher 
2729aa32835SJeff Kirsher 	r = in_be32(&p->mr0);
2739aa32835SJeff Kirsher 	if (r & EMAC_MR0_RXE) {
2749aa32835SJeff Kirsher 		int n = dev->stop_timeout;
2759aa32835SJeff Kirsher 		out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
2769aa32835SJeff Kirsher 		while (!(in_be32(&p->mr0) & EMAC_MR0_RXI) && n) {
2779aa32835SJeff Kirsher 			udelay(1);
2789aa32835SJeff Kirsher 			--n;
2799aa32835SJeff Kirsher 		}
2809aa32835SJeff Kirsher 		if (unlikely(!n))
2819aa32835SJeff Kirsher 			emac_report_timeout_error(dev, "RX disable timeout");
2829aa32835SJeff Kirsher 	}
2839aa32835SJeff Kirsher }
2849aa32835SJeff Kirsher 
emac_netif_stop(struct emac_instance * dev)2859aa32835SJeff Kirsher static inline void emac_netif_stop(struct emac_instance *dev)
2869aa32835SJeff Kirsher {
2879aa32835SJeff Kirsher 	netif_tx_lock_bh(dev->ndev);
2889aa32835SJeff Kirsher 	netif_addr_lock(dev->ndev);
2899aa32835SJeff Kirsher 	dev->no_mcast = 1;
2909aa32835SJeff Kirsher 	netif_addr_unlock(dev->ndev);
2919aa32835SJeff Kirsher 	netif_tx_unlock_bh(dev->ndev);
292860e9538SFlorian Westphal 	netif_trans_update(dev->ndev);	/* prevent tx timeout */
2939aa32835SJeff Kirsher 	mal_poll_disable(dev->mal, &dev->commac);
2949aa32835SJeff Kirsher 	netif_tx_disable(dev->ndev);
2959aa32835SJeff Kirsher }
2969aa32835SJeff Kirsher 
emac_netif_start(struct emac_instance * dev)2979aa32835SJeff Kirsher static inline void emac_netif_start(struct emac_instance *dev)
2989aa32835SJeff Kirsher {
2999aa32835SJeff Kirsher 	netif_tx_lock_bh(dev->ndev);
3009aa32835SJeff Kirsher 	netif_addr_lock(dev->ndev);
3019aa32835SJeff Kirsher 	dev->no_mcast = 0;
3029aa32835SJeff Kirsher 	if (dev->mcast_pending && netif_running(dev->ndev))
3039aa32835SJeff Kirsher 		__emac_set_multicast_list(dev);
3049aa32835SJeff Kirsher 	netif_addr_unlock(dev->ndev);
3059aa32835SJeff Kirsher 	netif_tx_unlock_bh(dev->ndev);
3069aa32835SJeff Kirsher 
3079aa32835SJeff Kirsher 	netif_wake_queue(dev->ndev);
3089aa32835SJeff Kirsher 
3099aa32835SJeff Kirsher 	/* NOTE: unconditional netif_wake_queue is only appropriate
3109aa32835SJeff Kirsher 	 * so long as all callers are assured to have free tx slots
3119aa32835SJeff Kirsher 	 * (taken from tg3... though the case where that is wrong is
3129aa32835SJeff Kirsher 	 *  not terribly harmful)
3139aa32835SJeff Kirsher 	 */
3149aa32835SJeff Kirsher 	mal_poll_enable(dev->mal, &dev->commac);
3159aa32835SJeff Kirsher }
3169aa32835SJeff Kirsher 
emac_rx_disable_async(struct emac_instance * dev)3179aa32835SJeff Kirsher static inline void emac_rx_disable_async(struct emac_instance *dev)
3189aa32835SJeff Kirsher {
3199aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
3209aa32835SJeff Kirsher 	u32 r;
3219aa32835SJeff Kirsher 
3229aa32835SJeff Kirsher 	DBG(dev, "rx_disable_async" NL);
3239aa32835SJeff Kirsher 
3249aa32835SJeff Kirsher 	r = in_be32(&p->mr0);
3259aa32835SJeff Kirsher 	if (r & EMAC_MR0_RXE)
3269aa32835SJeff Kirsher 		out_be32(&p->mr0, r & ~EMAC_MR0_RXE);
3279aa32835SJeff Kirsher }
3289aa32835SJeff Kirsher 
emac_reset(struct emac_instance * dev)3299aa32835SJeff Kirsher static int emac_reset(struct emac_instance *dev)
3309aa32835SJeff Kirsher {
3319aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
3329aa32835SJeff Kirsher 	int n = 20;
33319d90eceSChristian Lamparter 	bool __maybe_unused try_internal_clock = false;
3349aa32835SJeff Kirsher 
3359aa32835SJeff Kirsher 	DBG(dev, "reset" NL);
3369aa32835SJeff Kirsher 
3379aa32835SJeff Kirsher 	if (!dev->reset_failed) {
3389aa32835SJeff Kirsher 		/* 40x erratum suggests stopping RX channel before reset,
3399aa32835SJeff Kirsher 		 * we stop TX as well
3409aa32835SJeff Kirsher 		 */
3419aa32835SJeff Kirsher 		emac_rx_disable(dev);
3429aa32835SJeff Kirsher 		emac_tx_disable(dev);
3439aa32835SJeff Kirsher 	}
3449aa32835SJeff Kirsher 
3459aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
34619d90eceSChristian Lamparter do_retry:
34723fbb5a8SPetri Gynther 	/*
34823fbb5a8SPetri Gynther 	 * PPC460EX/GT Embedded Processor Advanced User's Manual
34923fbb5a8SPetri Gynther 	 * section 28.10.1 Mode Register 0 (EMACx_MR0) states:
35023fbb5a8SPetri Gynther 	 * Note: The PHY must provide a TX Clk in order to perform a soft reset
35123fbb5a8SPetri Gynther 	 * of the EMAC. If none is present, select the internal clock
35223fbb5a8SPetri Gynther 	 * (SDR0_ETH_CFG[EMACx_PHY_CLK] = 1).
35323fbb5a8SPetri Gynther 	 * After a soft reset, select the external clock.
35419d90eceSChristian Lamparter 	 *
35519d90eceSChristian Lamparter 	 * The AR8035-A PHY Meraki MR24 does not provide a TX Clk if the
35619d90eceSChristian Lamparter 	 * ethernet cable is not attached. This causes the reset to timeout
35719d90eceSChristian Lamparter 	 * and the PHY detection code in emac_init_phy() is unable to
35819d90eceSChristian Lamparter 	 * communicate and detect the AR8035-A PHY. As a result, the emac
35919d90eceSChristian Lamparter 	 * driver bails out early and the user has no ethernet.
36019d90eceSChristian Lamparter 	 * In order to stay compatible with existing configurations, the
36119d90eceSChristian Lamparter 	 * driver will temporarily switch to the internal clock, after
36219d90eceSChristian Lamparter 	 * the first reset fails.
36323fbb5a8SPetri Gynther 	 */
36423fbb5a8SPetri Gynther 	if (emac_has_feature(dev, EMAC_FTR_460EX_PHY_CLK_FIX)) {
36519d90eceSChristian Lamparter 		if (try_internal_clock || (dev->phy_address == 0xffffffff &&
36619d90eceSChristian Lamparter 					   dev->phy_map == 0xffffffff)) {
36723fbb5a8SPetri Gynther 			/* No PHY: select internal loop clock before reset */
3689aa32835SJeff Kirsher 			dcri_clrset(SDR0, SDR0_ETH_CFG,
3699aa32835SJeff Kirsher 				    0, SDR0_ETH_CFG_ECS << dev->cell_index);
37023fbb5a8SPetri Gynther 		} else {
37123fbb5a8SPetri Gynther 			/* PHY present: select external clock before reset */
37223fbb5a8SPetri Gynther 			dcri_clrset(SDR0, SDR0_ETH_CFG,
37323fbb5a8SPetri Gynther 				    SDR0_ETH_CFG_ECS << dev->cell_index, 0);
37423fbb5a8SPetri Gynther 		}
37523fbb5a8SPetri Gynther 	}
3769aa32835SJeff Kirsher #endif
3779aa32835SJeff Kirsher 
3789aa32835SJeff Kirsher 	out_be32(&p->mr0, EMAC_MR0_SRST);
3799aa32835SJeff Kirsher 	while ((in_be32(&p->mr0) & EMAC_MR0_SRST) && n)
3809aa32835SJeff Kirsher 		--n;
3819aa32835SJeff Kirsher 
3829aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
38323fbb5a8SPetri Gynther 	if (emac_has_feature(dev, EMAC_FTR_460EX_PHY_CLK_FIX)) {
38419d90eceSChristian Lamparter 		if (!n && !try_internal_clock) {
38519d90eceSChristian Lamparter 			/* first attempt has timed out. */
38619d90eceSChristian Lamparter 			n = 20;
38719d90eceSChristian Lamparter 			try_internal_clock = true;
38819d90eceSChristian Lamparter 			goto do_retry;
38919d90eceSChristian Lamparter 		}
39019d90eceSChristian Lamparter 
39119d90eceSChristian Lamparter 		if (try_internal_clock || (dev->phy_address == 0xffffffff &&
39219d90eceSChristian Lamparter 					   dev->phy_map == 0xffffffff)) {
39323fbb5a8SPetri Gynther 			/* No PHY: restore external clock source after reset */
3949aa32835SJeff Kirsher 			dcri_clrset(SDR0, SDR0_ETH_CFG,
3959aa32835SJeff Kirsher 				    SDR0_ETH_CFG_ECS << dev->cell_index, 0);
39623fbb5a8SPetri Gynther 		}
39723fbb5a8SPetri Gynther 	}
3989aa32835SJeff Kirsher #endif
3999aa32835SJeff Kirsher 
4009aa32835SJeff Kirsher 	if (n) {
4019aa32835SJeff Kirsher 		dev->reset_failed = 0;
4029aa32835SJeff Kirsher 		return 0;
4039aa32835SJeff Kirsher 	} else {
4049aa32835SJeff Kirsher 		emac_report_timeout_error(dev, "reset timeout");
4059aa32835SJeff Kirsher 		dev->reset_failed = 1;
4069aa32835SJeff Kirsher 		return -ETIMEDOUT;
4079aa32835SJeff Kirsher 	}
4089aa32835SJeff Kirsher }
4099aa32835SJeff Kirsher 
emac_hash_mc(struct emac_instance * dev)4109aa32835SJeff Kirsher static void emac_hash_mc(struct emac_instance *dev)
4119aa32835SJeff Kirsher {
4125aa3b55bSSimon Horman 	u32 __iomem *gaht_base = emac_gaht_base(dev);
4139aa32835SJeff Kirsher 	const int regs = EMAC_XAHT_REGS(dev);
414ee4fccbeSKees Cook 	u32 gaht_temp[EMAC_XAHT_MAX_REGS];
4159aa32835SJeff Kirsher 	struct netdev_hw_addr *ha;
4169aa32835SJeff Kirsher 	int i;
4179aa32835SJeff Kirsher 
4189aa32835SJeff Kirsher 	DBG(dev, "hash_mc %d" NL, netdev_mc_count(dev->ndev));
4199aa32835SJeff Kirsher 
4209aa32835SJeff Kirsher 	memset(gaht_temp, 0, sizeof (gaht_temp));
4219aa32835SJeff Kirsher 
4229aa32835SJeff Kirsher 	netdev_for_each_mc_addr(ha, dev->ndev) {
4239aa32835SJeff Kirsher 		int slot, reg, mask;
4249aa32835SJeff Kirsher 		DBG2(dev, "mc %pM" NL, ha->addr);
4259aa32835SJeff Kirsher 
4269aa32835SJeff Kirsher 		slot = EMAC_XAHT_CRC_TO_SLOT(dev,
4279aa32835SJeff Kirsher 					     ether_crc(ETH_ALEN, ha->addr));
4289aa32835SJeff Kirsher 		reg = EMAC_XAHT_SLOT_TO_REG(dev, slot);
4299aa32835SJeff Kirsher 		mask = EMAC_XAHT_SLOT_TO_MASK(dev, slot);
4309aa32835SJeff Kirsher 
4319aa32835SJeff Kirsher 		gaht_temp[reg] |= mask;
4329aa32835SJeff Kirsher 	}
4339aa32835SJeff Kirsher 
4349aa32835SJeff Kirsher 	for (i = 0; i < regs; i++)
4359aa32835SJeff Kirsher 		out_be32(gaht_base + i, gaht_temp[i]);
4369aa32835SJeff Kirsher }
4379aa32835SJeff Kirsher 
emac_iff2rmr(struct net_device * ndev)4389aa32835SJeff Kirsher static inline u32 emac_iff2rmr(struct net_device *ndev)
4399aa32835SJeff Kirsher {
4409aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
4419aa32835SJeff Kirsher 	u32 r;
4429aa32835SJeff Kirsher 
4439aa32835SJeff Kirsher 	r = EMAC_RMR_SP | EMAC_RMR_SFCS | EMAC_RMR_IAE | EMAC_RMR_BAE;
4449aa32835SJeff Kirsher 
4459aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
4469aa32835SJeff Kirsher 	    r |= EMAC4_RMR_BASE;
4479aa32835SJeff Kirsher 	else
4489aa32835SJeff Kirsher 	    r |= EMAC_RMR_BASE;
4499aa32835SJeff Kirsher 
4509aa32835SJeff Kirsher 	if (ndev->flags & IFF_PROMISC)
4519aa32835SJeff Kirsher 		r |= EMAC_RMR_PME;
4529aa32835SJeff Kirsher 	else if (ndev->flags & IFF_ALLMULTI ||
4539aa32835SJeff Kirsher 			 (netdev_mc_count(ndev) > EMAC_XAHT_SLOTS(dev)))
4549aa32835SJeff Kirsher 		r |= EMAC_RMR_PMME;
4559aa32835SJeff Kirsher 	else if (!netdev_mc_empty(ndev))
4569aa32835SJeff Kirsher 		r |= EMAC_RMR_MAE;
4579aa32835SJeff Kirsher 
458ae5d3372SDuc Dang 	if (emac_has_feature(dev, EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE)) {
459ae5d3372SDuc Dang 		r &= ~EMAC4_RMR_MJS_MASK;
460ae5d3372SDuc Dang 		r |= EMAC4_RMR_MJS(ndev->mtu);
461ae5d3372SDuc Dang 	}
462ae5d3372SDuc Dang 
4639aa32835SJeff Kirsher 	return r;
4649aa32835SJeff Kirsher }
4659aa32835SJeff Kirsher 
__emac_calc_base_mr1(struct emac_instance * dev,int tx_size,int rx_size)4669aa32835SJeff Kirsher static u32 __emac_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
4679aa32835SJeff Kirsher {
4689aa32835SJeff Kirsher 	u32 ret = EMAC_MR1_VLE | EMAC_MR1_IST | EMAC_MR1_TR0_MULT;
4699aa32835SJeff Kirsher 
4709aa32835SJeff Kirsher 	DBG2(dev, "__emac_calc_base_mr1" NL);
4719aa32835SJeff Kirsher 
4729aa32835SJeff Kirsher 	switch(tx_size) {
4739aa32835SJeff Kirsher 	case 2048:
4749aa32835SJeff Kirsher 		ret |= EMAC_MR1_TFS_2K;
4759aa32835SJeff Kirsher 		break;
4769aa32835SJeff Kirsher 	default:
4779aa32835SJeff Kirsher 		printk(KERN_WARNING "%s: Unknown Tx FIFO size %d\n",
4789aa32835SJeff Kirsher 		       dev->ndev->name, tx_size);
4799aa32835SJeff Kirsher 	}
4809aa32835SJeff Kirsher 
4819aa32835SJeff Kirsher 	switch(rx_size) {
4829aa32835SJeff Kirsher 	case 16384:
4839aa32835SJeff Kirsher 		ret |= EMAC_MR1_RFS_16K;
4849aa32835SJeff Kirsher 		break;
4859aa32835SJeff Kirsher 	case 4096:
4869aa32835SJeff Kirsher 		ret |= EMAC_MR1_RFS_4K;
4879aa32835SJeff Kirsher 		break;
4889aa32835SJeff Kirsher 	default:
4899aa32835SJeff Kirsher 		printk(KERN_WARNING "%s: Unknown Rx FIFO size %d\n",
4909aa32835SJeff Kirsher 		       dev->ndev->name, rx_size);
4919aa32835SJeff Kirsher 	}
4929aa32835SJeff Kirsher 
4939aa32835SJeff Kirsher 	return ret;
4949aa32835SJeff Kirsher }
4959aa32835SJeff Kirsher 
__emac4_calc_base_mr1(struct emac_instance * dev,int tx_size,int rx_size)4969aa32835SJeff Kirsher static u32 __emac4_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
4979aa32835SJeff Kirsher {
4989aa32835SJeff Kirsher 	u32 ret = EMAC_MR1_VLE | EMAC_MR1_IST | EMAC4_MR1_TR |
4999aa32835SJeff Kirsher 		EMAC4_MR1_OBCI(dev->opb_bus_freq / 1000000);
5009aa32835SJeff Kirsher 
5019aa32835SJeff Kirsher 	DBG2(dev, "__emac4_calc_base_mr1" NL);
5029aa32835SJeff Kirsher 
5039aa32835SJeff Kirsher 	switch(tx_size) {
5049aa32835SJeff Kirsher 	case 16384:
5059aa32835SJeff Kirsher 		ret |= EMAC4_MR1_TFS_16K;
5069aa32835SJeff Kirsher 		break;
50745d6e545SIvan Mikhaylov 	case 8192:
50845d6e545SIvan Mikhaylov 		ret |= EMAC4_MR1_TFS_8K;
50945d6e545SIvan Mikhaylov 		break;
5109aa32835SJeff Kirsher 	case 4096:
5119aa32835SJeff Kirsher 		ret |= EMAC4_MR1_TFS_4K;
5129aa32835SJeff Kirsher 		break;
5139aa32835SJeff Kirsher 	case 2048:
5149aa32835SJeff Kirsher 		ret |= EMAC4_MR1_TFS_2K;
5159aa32835SJeff Kirsher 		break;
5169aa32835SJeff Kirsher 	default:
5179aa32835SJeff Kirsher 		printk(KERN_WARNING "%s: Unknown Tx FIFO size %d\n",
5189aa32835SJeff Kirsher 		       dev->ndev->name, tx_size);
5199aa32835SJeff Kirsher 	}
5209aa32835SJeff Kirsher 
5219aa32835SJeff Kirsher 	switch(rx_size) {
5229aa32835SJeff Kirsher 	case 16384:
5239aa32835SJeff Kirsher 		ret |= EMAC4_MR1_RFS_16K;
5249aa32835SJeff Kirsher 		break;
525bf68066fSIvan Mikhaylov 	case 8192:
526bf68066fSIvan Mikhaylov 		ret |= EMAC4_MR1_RFS_8K;
527bf68066fSIvan Mikhaylov 		break;
5289aa32835SJeff Kirsher 	case 4096:
5299aa32835SJeff Kirsher 		ret |= EMAC4_MR1_RFS_4K;
5309aa32835SJeff Kirsher 		break;
5319aa32835SJeff Kirsher 	case 2048:
5329aa32835SJeff Kirsher 		ret |= EMAC4_MR1_RFS_2K;
5339aa32835SJeff Kirsher 		break;
5349aa32835SJeff Kirsher 	default:
5359aa32835SJeff Kirsher 		printk(KERN_WARNING "%s: Unknown Rx FIFO size %d\n",
5369aa32835SJeff Kirsher 		       dev->ndev->name, rx_size);
5379aa32835SJeff Kirsher 	}
5389aa32835SJeff Kirsher 
5399aa32835SJeff Kirsher 	return ret;
5409aa32835SJeff Kirsher }
5419aa32835SJeff Kirsher 
emac_calc_base_mr1(struct emac_instance * dev,int tx_size,int rx_size)5429aa32835SJeff Kirsher static u32 emac_calc_base_mr1(struct emac_instance *dev, int tx_size, int rx_size)
5439aa32835SJeff Kirsher {
5449aa32835SJeff Kirsher 	return emac_has_feature(dev, EMAC_FTR_EMAC4) ?
5459aa32835SJeff Kirsher 		__emac4_calc_base_mr1(dev, tx_size, rx_size) :
5469aa32835SJeff Kirsher 		__emac_calc_base_mr1(dev, tx_size, rx_size);
5479aa32835SJeff Kirsher }
5489aa32835SJeff Kirsher 
emac_calc_trtr(struct emac_instance * dev,unsigned int size)5499aa32835SJeff Kirsher static inline u32 emac_calc_trtr(struct emac_instance *dev, unsigned int size)
5509aa32835SJeff Kirsher {
5519aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
5529aa32835SJeff Kirsher 		return ((size >> 6) - 1) << EMAC_TRTR_SHIFT_EMAC4;
5539aa32835SJeff Kirsher 	else
5549aa32835SJeff Kirsher 		return ((size >> 6) - 1) << EMAC_TRTR_SHIFT;
5559aa32835SJeff Kirsher }
5569aa32835SJeff Kirsher 
emac_calc_rwmr(struct emac_instance * dev,unsigned int low,unsigned int high)5579aa32835SJeff Kirsher static inline u32 emac_calc_rwmr(struct emac_instance *dev,
5589aa32835SJeff Kirsher 				 unsigned int low, unsigned int high)
5599aa32835SJeff Kirsher {
5609aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
5619aa32835SJeff Kirsher 		return (low << 22) | ( (high & 0x3ff) << 6);
5629aa32835SJeff Kirsher 	else
5639aa32835SJeff Kirsher 		return (low << 23) | ( (high & 0x1ff) << 7);
5649aa32835SJeff Kirsher }
5659aa32835SJeff Kirsher 
emac_configure(struct emac_instance * dev)5669aa32835SJeff Kirsher static int emac_configure(struct emac_instance *dev)
5679aa32835SJeff Kirsher {
5689aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
5699aa32835SJeff Kirsher 	struct net_device *ndev = dev->ndev;
5709aa32835SJeff Kirsher 	int tx_size, rx_size, link = netif_carrier_ok(dev->ndev);
5719aa32835SJeff Kirsher 	u32 r, mr1 = 0;
5729aa32835SJeff Kirsher 
5739aa32835SJeff Kirsher 	DBG(dev, "configure" NL);
5749aa32835SJeff Kirsher 
5759aa32835SJeff Kirsher 	if (!link) {
5769aa32835SJeff Kirsher 		out_be32(&p->mr1, in_be32(&p->mr1)
5779aa32835SJeff Kirsher 			 | EMAC_MR1_FDE | EMAC_MR1_ILE);
5789aa32835SJeff Kirsher 		udelay(100);
5799aa32835SJeff Kirsher 	} else if (emac_reset(dev) < 0)
5809aa32835SJeff Kirsher 		return -ETIMEDOUT;
5819aa32835SJeff Kirsher 
5829aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
5839aa32835SJeff Kirsher 		tah_reset(dev->tah_dev);
5849aa32835SJeff Kirsher 
5859aa32835SJeff Kirsher 	DBG(dev, " link = %d duplex = %d, pause = %d, asym_pause = %d\n",
5869aa32835SJeff Kirsher 	    link, dev->phy.duplex, dev->phy.pause, dev->phy.asym_pause);
5879aa32835SJeff Kirsher 
5889aa32835SJeff Kirsher 	/* Default fifo sizes */
5899aa32835SJeff Kirsher 	tx_size = dev->tx_fifo_size;
5909aa32835SJeff Kirsher 	rx_size = dev->rx_fifo_size;
5919aa32835SJeff Kirsher 
5929aa32835SJeff Kirsher 	/* No link, force loopback */
5939aa32835SJeff Kirsher 	if (!link)
5949aa32835SJeff Kirsher 		mr1 = EMAC_MR1_FDE | EMAC_MR1_ILE;
5959aa32835SJeff Kirsher 
5969aa32835SJeff Kirsher 	/* Check for full duplex */
5979aa32835SJeff Kirsher 	else if (dev->phy.duplex == DUPLEX_FULL)
5989aa32835SJeff Kirsher 		mr1 |= EMAC_MR1_FDE | EMAC_MR1_MWSW_001;
5999aa32835SJeff Kirsher 
6009aa32835SJeff Kirsher 	/* Adjust fifo sizes, mr1 and timeouts based on link speed */
6019aa32835SJeff Kirsher 	dev->stop_timeout = STOP_TIMEOUT_10;
6029aa32835SJeff Kirsher 	switch (dev->phy.speed) {
6039aa32835SJeff Kirsher 	case SPEED_1000:
6049aa32835SJeff Kirsher 		if (emac_phy_gpcs(dev->phy.mode)) {
6059aa32835SJeff Kirsher 			mr1 |= EMAC_MR1_MF_1000GPCS | EMAC_MR1_MF_IPPA(
6069aa32835SJeff Kirsher 				(dev->phy.gpcs_address != 0xffffffff) ?
6079aa32835SJeff Kirsher 				 dev->phy.gpcs_address : dev->phy.address);
6089aa32835SJeff Kirsher 
6099aa32835SJeff Kirsher 			/* Put some arbitrary OUI, Manuf & Rev IDs so we can
6109aa32835SJeff Kirsher 			 * identify this GPCS PHY later.
6119aa32835SJeff Kirsher 			 */
6129aa32835SJeff Kirsher 			out_be32(&p->u1.emac4.ipcr, 0xdeadbeef);
6139aa32835SJeff Kirsher 		} else
6149aa32835SJeff Kirsher 			mr1 |= EMAC_MR1_MF_1000;
6159aa32835SJeff Kirsher 
6169aa32835SJeff Kirsher 		/* Extended fifo sizes */
6179aa32835SJeff Kirsher 		tx_size = dev->tx_fifo_size_gige;
6189aa32835SJeff Kirsher 		rx_size = dev->rx_fifo_size_gige;
6199aa32835SJeff Kirsher 
6209aa32835SJeff Kirsher 		if (dev->ndev->mtu > ETH_DATA_LEN) {
6219aa32835SJeff Kirsher 			if (emac_has_feature(dev, EMAC_FTR_EMAC4))
6229aa32835SJeff Kirsher 				mr1 |= EMAC4_MR1_JPSM;
6239aa32835SJeff Kirsher 			else
6249aa32835SJeff Kirsher 				mr1 |= EMAC_MR1_JPSM;
6259aa32835SJeff Kirsher 			dev->stop_timeout = STOP_TIMEOUT_1000_JUMBO;
6269aa32835SJeff Kirsher 		} else
6279aa32835SJeff Kirsher 			dev->stop_timeout = STOP_TIMEOUT_1000;
6289aa32835SJeff Kirsher 		break;
6299aa32835SJeff Kirsher 	case SPEED_100:
6309aa32835SJeff Kirsher 		mr1 |= EMAC_MR1_MF_100;
6319aa32835SJeff Kirsher 		dev->stop_timeout = STOP_TIMEOUT_100;
6329aa32835SJeff Kirsher 		break;
6339aa32835SJeff Kirsher 	default: /* make gcc happy */
6349aa32835SJeff Kirsher 		break;
6359aa32835SJeff Kirsher 	}
6369aa32835SJeff Kirsher 
6379aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
6389aa32835SJeff Kirsher 		rgmii_set_speed(dev->rgmii_dev, dev->rgmii_port,
6399aa32835SJeff Kirsher 				dev->phy.speed);
6409aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
6419aa32835SJeff Kirsher 		zmii_set_speed(dev->zmii_dev, dev->zmii_port, dev->phy.speed);
6429aa32835SJeff Kirsher 
6439aa32835SJeff Kirsher 	/* on 40x erratum forces us to NOT use integrated flow control,
6449aa32835SJeff Kirsher 	 * let's hope it works on 44x ;)
6459aa32835SJeff Kirsher 	 */
6469aa32835SJeff Kirsher 	if (!emac_has_feature(dev, EMAC_FTR_NO_FLOW_CONTROL_40x) &&
6479aa32835SJeff Kirsher 	    dev->phy.duplex == DUPLEX_FULL) {
6489aa32835SJeff Kirsher 		if (dev->phy.pause)
6499aa32835SJeff Kirsher 			mr1 |= EMAC_MR1_EIFC | EMAC_MR1_APP;
6509aa32835SJeff Kirsher 		else if (dev->phy.asym_pause)
6519aa32835SJeff Kirsher 			mr1 |= EMAC_MR1_APP;
6529aa32835SJeff Kirsher 	}
6539aa32835SJeff Kirsher 
6549aa32835SJeff Kirsher 	/* Add base settings & fifo sizes & program MR1 */
6559aa32835SJeff Kirsher 	mr1 |= emac_calc_base_mr1(dev, tx_size, rx_size);
6569aa32835SJeff Kirsher 	out_be32(&p->mr1, mr1);
6579aa32835SJeff Kirsher 
6589aa32835SJeff Kirsher 	/* Set individual MAC address */
6599aa32835SJeff Kirsher 	out_be32(&p->iahr, (ndev->dev_addr[0] << 8) | ndev->dev_addr[1]);
6609aa32835SJeff Kirsher 	out_be32(&p->ialr, (ndev->dev_addr[2] << 24) |
6619aa32835SJeff Kirsher 		 (ndev->dev_addr[3] << 16) | (ndev->dev_addr[4] << 8) |
6629aa32835SJeff Kirsher 		 ndev->dev_addr[5]);
6639aa32835SJeff Kirsher 
6649aa32835SJeff Kirsher 	/* VLAN Tag Protocol ID */
6659aa32835SJeff Kirsher 	out_be32(&p->vtpid, 0x8100);
6669aa32835SJeff Kirsher 
6679aa32835SJeff Kirsher 	/* Receive mode register */
6689aa32835SJeff Kirsher 	r = emac_iff2rmr(ndev);
6699aa32835SJeff Kirsher 	if (r & EMAC_RMR_MAE)
6709aa32835SJeff Kirsher 		emac_hash_mc(dev);
6719aa32835SJeff Kirsher 	out_be32(&p->rmr, r);
6729aa32835SJeff Kirsher 
6739aa32835SJeff Kirsher 	/* FIFOs thresholds */
6749aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
6759aa32835SJeff Kirsher 		r = EMAC4_TMR1((dev->mal_burst_size / dev->fifo_entry_size) + 1,
6769aa32835SJeff Kirsher 			       tx_size / 2 / dev->fifo_entry_size);
6779aa32835SJeff Kirsher 	else
6789aa32835SJeff Kirsher 		r = EMAC_TMR1((dev->mal_burst_size / dev->fifo_entry_size) + 1,
6799aa32835SJeff Kirsher 			      tx_size / 2 / dev->fifo_entry_size);
6809aa32835SJeff Kirsher 	out_be32(&p->tmr1, r);
6819aa32835SJeff Kirsher 	out_be32(&p->trtr, emac_calc_trtr(dev, tx_size / 2));
6829aa32835SJeff Kirsher 
6839aa32835SJeff Kirsher 	/* PAUSE frame is sent when RX FIFO reaches its high-water mark,
6849aa32835SJeff Kirsher 	   there should be still enough space in FIFO to allow the our link
6859aa32835SJeff Kirsher 	   partner time to process this frame and also time to send PAUSE
6869aa32835SJeff Kirsher 	   frame itself.
6879aa32835SJeff Kirsher 
6889aa32835SJeff Kirsher 	   Here is the worst case scenario for the RX FIFO "headroom"
6899aa32835SJeff Kirsher 	   (from "The Switch Book") (100Mbps, without preamble, inter-frame gap):
6909aa32835SJeff Kirsher 
6919aa32835SJeff Kirsher 	   1) One maximum-length frame on TX                    1522 bytes
6929aa32835SJeff Kirsher 	   2) One PAUSE frame time                                64 bytes
6939aa32835SJeff Kirsher 	   3) PAUSE frame decode time allowance                   64 bytes
6949aa32835SJeff Kirsher 	   4) One maximum-length frame on RX                    1522 bytes
6959aa32835SJeff Kirsher 	   5) Round-trip propagation delay of the link (100Mb)    15 bytes
6969aa32835SJeff Kirsher 	   ----------
6979aa32835SJeff Kirsher 	   3187 bytes
6989aa32835SJeff Kirsher 
6999aa32835SJeff Kirsher 	   I chose to set high-water mark to RX_FIFO_SIZE / 4 (1024 bytes)
7009aa32835SJeff Kirsher 	   low-water mark  to RX_FIFO_SIZE / 8 (512 bytes)
7019aa32835SJeff Kirsher 	 */
7029aa32835SJeff Kirsher 	r = emac_calc_rwmr(dev, rx_size / 8 / dev->fifo_entry_size,
7039aa32835SJeff Kirsher 			   rx_size / 4 / dev->fifo_entry_size);
7049aa32835SJeff Kirsher 	out_be32(&p->rwmr, r);
7059aa32835SJeff Kirsher 
7069aa32835SJeff Kirsher 	/* Set PAUSE timer to the maximum */
7079aa32835SJeff Kirsher 	out_be32(&p->ptr, 0xffff);
7089aa32835SJeff Kirsher 
7099aa32835SJeff Kirsher 	/* IRQ sources */
7109aa32835SJeff Kirsher 	r = EMAC_ISR_OVR | EMAC_ISR_BP | EMAC_ISR_SE |
7119aa32835SJeff Kirsher 		EMAC_ISR_ALE | EMAC_ISR_BFCS | EMAC_ISR_PTLE | EMAC_ISR_ORE |
7129aa32835SJeff Kirsher 		EMAC_ISR_IRE | EMAC_ISR_TE;
7139aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
7149aa32835SJeff Kirsher 	    r |= EMAC4_ISR_TXPE | EMAC4_ISR_RXPE /* | EMAC4_ISR_TXUE |
7159aa32835SJeff Kirsher 						  EMAC4_ISR_RXOE | */;
7169aa32835SJeff Kirsher 	out_be32(&p->iser,  r);
7179aa32835SJeff Kirsher 
7189aa32835SJeff Kirsher 	/* We need to take GPCS PHY out of isolate mode after EMAC reset */
7199aa32835SJeff Kirsher 	if (emac_phy_gpcs(dev->phy.mode)) {
7209aa32835SJeff Kirsher 		if (dev->phy.gpcs_address != 0xffffffff)
7219aa32835SJeff Kirsher 			emac_mii_reset_gpcs(&dev->phy);
7229aa32835SJeff Kirsher 		else
7239aa32835SJeff Kirsher 			emac_mii_reset_phy(&dev->phy);
7249aa32835SJeff Kirsher 	}
7259aa32835SJeff Kirsher 
7269aa32835SJeff Kirsher 	return 0;
7279aa32835SJeff Kirsher }
7289aa32835SJeff Kirsher 
emac_reinitialize(struct emac_instance * dev)7299aa32835SJeff Kirsher static void emac_reinitialize(struct emac_instance *dev)
7309aa32835SJeff Kirsher {
7319aa32835SJeff Kirsher 	DBG(dev, "reinitialize" NL);
7329aa32835SJeff Kirsher 
7339aa32835SJeff Kirsher 	emac_netif_stop(dev);
7349aa32835SJeff Kirsher 	if (!emac_configure(dev)) {
7359aa32835SJeff Kirsher 		emac_tx_enable(dev);
7369aa32835SJeff Kirsher 		emac_rx_enable(dev);
7379aa32835SJeff Kirsher 	}
7389aa32835SJeff Kirsher 	emac_netif_start(dev);
7399aa32835SJeff Kirsher }
7409aa32835SJeff Kirsher 
emac_full_tx_reset(struct emac_instance * dev)7419aa32835SJeff Kirsher static void emac_full_tx_reset(struct emac_instance *dev)
7429aa32835SJeff Kirsher {
7439aa32835SJeff Kirsher 	DBG(dev, "full_tx_reset" NL);
7449aa32835SJeff Kirsher 
7459aa32835SJeff Kirsher 	emac_tx_disable(dev);
7469aa32835SJeff Kirsher 	mal_disable_tx_channel(dev->mal, dev->mal_tx_chan);
7479aa32835SJeff Kirsher 	emac_clean_tx_ring(dev);
7489aa32835SJeff Kirsher 	dev->tx_cnt = dev->tx_slot = dev->ack_slot = 0;
7499aa32835SJeff Kirsher 
7509aa32835SJeff Kirsher 	emac_configure(dev);
7519aa32835SJeff Kirsher 
7529aa32835SJeff Kirsher 	mal_enable_tx_channel(dev->mal, dev->mal_tx_chan);
7539aa32835SJeff Kirsher 	emac_tx_enable(dev);
7549aa32835SJeff Kirsher 	emac_rx_enable(dev);
7559aa32835SJeff Kirsher }
7569aa32835SJeff Kirsher 
emac_reset_work(struct work_struct * work)7579aa32835SJeff Kirsher static void emac_reset_work(struct work_struct *work)
7589aa32835SJeff Kirsher {
7599aa32835SJeff Kirsher 	struct emac_instance *dev = container_of(work, struct emac_instance, reset_work);
7609aa32835SJeff Kirsher 
7619aa32835SJeff Kirsher 	DBG(dev, "reset_work" NL);
7629aa32835SJeff Kirsher 
7639aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
7649aa32835SJeff Kirsher 	if (dev->opened) {
7659aa32835SJeff Kirsher 		emac_netif_stop(dev);
7669aa32835SJeff Kirsher 		emac_full_tx_reset(dev);
7679aa32835SJeff Kirsher 		emac_netif_start(dev);
7689aa32835SJeff Kirsher 	}
7699aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
7709aa32835SJeff Kirsher }
7719aa32835SJeff Kirsher 
emac_tx_timeout(struct net_device * ndev,unsigned int txqueue)7720290bd29SMichael S. Tsirkin static void emac_tx_timeout(struct net_device *ndev, unsigned int txqueue)
7739aa32835SJeff Kirsher {
7749aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
7759aa32835SJeff Kirsher 
7769aa32835SJeff Kirsher 	DBG(dev, "tx_timeout" NL);
7779aa32835SJeff Kirsher 
7789aa32835SJeff Kirsher 	schedule_work(&dev->reset_work);
7799aa32835SJeff Kirsher }
7809aa32835SJeff Kirsher 
7819aa32835SJeff Kirsher 
emac_phy_done(struct emac_instance * dev,u32 stacr)7829aa32835SJeff Kirsher static inline int emac_phy_done(struct emac_instance *dev, u32 stacr)
7839aa32835SJeff Kirsher {
7849aa32835SJeff Kirsher 	int done = !!(stacr & EMAC_STACR_OC);
7859aa32835SJeff Kirsher 
7869aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
7879aa32835SJeff Kirsher 		done = !done;
7889aa32835SJeff Kirsher 
7899aa32835SJeff Kirsher 	return done;
7909aa32835SJeff Kirsher };
7919aa32835SJeff Kirsher 
__emac_mdio_read(struct emac_instance * dev,u8 id,u8 reg)7929aa32835SJeff Kirsher static int __emac_mdio_read(struct emac_instance *dev, u8 id, u8 reg)
7939aa32835SJeff Kirsher {
7949aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
7959aa32835SJeff Kirsher 	u32 r = 0;
7969aa32835SJeff Kirsher 	int n, err = -ETIMEDOUT;
7979aa32835SJeff Kirsher 
7989aa32835SJeff Kirsher 	mutex_lock(&dev->mdio_lock);
7999aa32835SJeff Kirsher 
8009aa32835SJeff Kirsher 	DBG2(dev, "mdio_read(%02x,%02x)" NL, id, reg);
8019aa32835SJeff Kirsher 
8029aa32835SJeff Kirsher 	/* Enable proper MDIO port */
8039aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
8049aa32835SJeff Kirsher 		zmii_get_mdio(dev->zmii_dev, dev->zmii_port);
8059aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
8069aa32835SJeff Kirsher 		rgmii_get_mdio(dev->rgmii_dev, dev->rgmii_port);
8079aa32835SJeff Kirsher 
8089aa32835SJeff Kirsher 	/* Wait for management interface to become idle */
8099aa32835SJeff Kirsher 	n = 20;
8109aa32835SJeff Kirsher 	while (!emac_phy_done(dev, in_be32(&p->stacr))) {
8119aa32835SJeff Kirsher 		udelay(1);
8129aa32835SJeff Kirsher 		if (!--n) {
8139aa32835SJeff Kirsher 			DBG2(dev, " -> timeout wait idle\n");
8149aa32835SJeff Kirsher 			goto bail;
8159aa32835SJeff Kirsher 		}
8169aa32835SJeff Kirsher 	}
8179aa32835SJeff Kirsher 
8189aa32835SJeff Kirsher 	/* Issue read command */
8199aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
8209aa32835SJeff Kirsher 		r = EMAC4_STACR_BASE(dev->opb_bus_freq);
8219aa32835SJeff Kirsher 	else
8229aa32835SJeff Kirsher 		r = EMAC_STACR_BASE(dev->opb_bus_freq);
8239aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
8249aa32835SJeff Kirsher 		r |= EMAC_STACR_OC;
8259aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_NEW_STACR))
8269aa32835SJeff Kirsher 		r |= EMACX_STACR_STAC_READ;
8279aa32835SJeff Kirsher 	else
8289aa32835SJeff Kirsher 		r |= EMAC_STACR_STAC_READ;
8299aa32835SJeff Kirsher 	r |= (reg & EMAC_STACR_PRA_MASK)
8309aa32835SJeff Kirsher 		| ((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT);
8319aa32835SJeff Kirsher 	out_be32(&p->stacr, r);
8329aa32835SJeff Kirsher 
8339aa32835SJeff Kirsher 	/* Wait for read to complete */
8349aa32835SJeff Kirsher 	n = 200;
8359aa32835SJeff Kirsher 	while (!emac_phy_done(dev, (r = in_be32(&p->stacr)))) {
8369aa32835SJeff Kirsher 		udelay(1);
8379aa32835SJeff Kirsher 		if (!--n) {
8389aa32835SJeff Kirsher 			DBG2(dev, " -> timeout wait complete\n");
8399aa32835SJeff Kirsher 			goto bail;
8409aa32835SJeff Kirsher 		}
8419aa32835SJeff Kirsher 	}
8429aa32835SJeff Kirsher 
8439aa32835SJeff Kirsher 	if (unlikely(r & EMAC_STACR_PHYE)) {
8449aa32835SJeff Kirsher 		DBG(dev, "mdio_read(%02x, %02x) failed" NL, id, reg);
8459aa32835SJeff Kirsher 		err = -EREMOTEIO;
8469aa32835SJeff Kirsher 		goto bail;
8479aa32835SJeff Kirsher 	}
8489aa32835SJeff Kirsher 
8499aa32835SJeff Kirsher 	r = ((r >> EMAC_STACR_PHYD_SHIFT) & EMAC_STACR_PHYD_MASK);
8509aa32835SJeff Kirsher 
8519aa32835SJeff Kirsher 	DBG2(dev, "mdio_read -> %04x" NL, r);
8529aa32835SJeff Kirsher 	err = 0;
8539aa32835SJeff Kirsher  bail:
8549aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
8559aa32835SJeff Kirsher 		rgmii_put_mdio(dev->rgmii_dev, dev->rgmii_port);
8569aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
8579aa32835SJeff Kirsher 		zmii_put_mdio(dev->zmii_dev, dev->zmii_port);
8589aa32835SJeff Kirsher 	mutex_unlock(&dev->mdio_lock);
8599aa32835SJeff Kirsher 
8609aa32835SJeff Kirsher 	return err == 0 ? r : err;
8619aa32835SJeff Kirsher }
8629aa32835SJeff Kirsher 
__emac_mdio_write(struct emac_instance * dev,u8 id,u8 reg,u16 val)8639aa32835SJeff Kirsher static void __emac_mdio_write(struct emac_instance *dev, u8 id, u8 reg,
8649aa32835SJeff Kirsher 			      u16 val)
8659aa32835SJeff Kirsher {
8669aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
8679aa32835SJeff Kirsher 	u32 r = 0;
8689b96a3e6SChen Zhou 	int n;
8699aa32835SJeff Kirsher 
8709aa32835SJeff Kirsher 	mutex_lock(&dev->mdio_lock);
8719aa32835SJeff Kirsher 
8729aa32835SJeff Kirsher 	DBG2(dev, "mdio_write(%02x,%02x,%04x)" NL, id, reg, val);
8739aa32835SJeff Kirsher 
8749aa32835SJeff Kirsher 	/* Enable proper MDIO port */
8759aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
8769aa32835SJeff Kirsher 		zmii_get_mdio(dev->zmii_dev, dev->zmii_port);
8779aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
8789aa32835SJeff Kirsher 		rgmii_get_mdio(dev->rgmii_dev, dev->rgmii_port);
8799aa32835SJeff Kirsher 
8809aa32835SJeff Kirsher 	/* Wait for management interface to be idle */
8819aa32835SJeff Kirsher 	n = 20;
8829aa32835SJeff Kirsher 	while (!emac_phy_done(dev, in_be32(&p->stacr))) {
8839aa32835SJeff Kirsher 		udelay(1);
8849aa32835SJeff Kirsher 		if (!--n) {
8859aa32835SJeff Kirsher 			DBG2(dev, " -> timeout wait idle\n");
8869aa32835SJeff Kirsher 			goto bail;
8879aa32835SJeff Kirsher 		}
8889aa32835SJeff Kirsher 	}
8899aa32835SJeff Kirsher 
8909aa32835SJeff Kirsher 	/* Issue write command */
8919aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
8929aa32835SJeff Kirsher 		r = EMAC4_STACR_BASE(dev->opb_bus_freq);
8939aa32835SJeff Kirsher 	else
8949aa32835SJeff Kirsher 		r = EMAC_STACR_BASE(dev->opb_bus_freq);
8959aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_STACR_OC_INVERT))
8969aa32835SJeff Kirsher 		r |= EMAC_STACR_OC;
8979aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_NEW_STACR))
8989aa32835SJeff Kirsher 		r |= EMACX_STACR_STAC_WRITE;
8999aa32835SJeff Kirsher 	else
9009aa32835SJeff Kirsher 		r |= EMAC_STACR_STAC_WRITE;
9019aa32835SJeff Kirsher 	r |= (reg & EMAC_STACR_PRA_MASK) |
9029aa32835SJeff Kirsher 		((id & EMAC_STACR_PCDA_MASK) << EMAC_STACR_PCDA_SHIFT) |
9039aa32835SJeff Kirsher 		(val << EMAC_STACR_PHYD_SHIFT);
9049aa32835SJeff Kirsher 	out_be32(&p->stacr, r);
9059aa32835SJeff Kirsher 
9069aa32835SJeff Kirsher 	/* Wait for write to complete */
9079aa32835SJeff Kirsher 	n = 200;
9089aa32835SJeff Kirsher 	while (!emac_phy_done(dev, in_be32(&p->stacr))) {
9099aa32835SJeff Kirsher 		udelay(1);
9109aa32835SJeff Kirsher 		if (!--n) {
9119aa32835SJeff Kirsher 			DBG2(dev, " -> timeout wait complete\n");
9129aa32835SJeff Kirsher 			goto bail;
9139aa32835SJeff Kirsher 		}
9149aa32835SJeff Kirsher 	}
9159aa32835SJeff Kirsher  bail:
9169aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
9179aa32835SJeff Kirsher 		rgmii_put_mdio(dev->rgmii_dev, dev->rgmii_port);
9189aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
9199aa32835SJeff Kirsher 		zmii_put_mdio(dev->zmii_dev, dev->zmii_port);
9209aa32835SJeff Kirsher 	mutex_unlock(&dev->mdio_lock);
9219aa32835SJeff Kirsher }
9229aa32835SJeff Kirsher 
emac_mdio_read(struct net_device * ndev,int id,int reg)9239aa32835SJeff Kirsher static int emac_mdio_read(struct net_device *ndev, int id, int reg)
9249aa32835SJeff Kirsher {
9259aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
9269aa32835SJeff Kirsher 	int res;
9279aa32835SJeff Kirsher 
9289aa32835SJeff Kirsher 	res = __emac_mdio_read((dev->mdio_instance &&
9299aa32835SJeff Kirsher 				dev->phy.gpcs_address != id) ?
9309aa32835SJeff Kirsher 				dev->mdio_instance : dev,
9319aa32835SJeff Kirsher 			       (u8) id, (u8) reg);
9329aa32835SJeff Kirsher 	return res;
9339aa32835SJeff Kirsher }
9349aa32835SJeff Kirsher 
emac_mdio_write(struct net_device * ndev,int id,int reg,int val)9359aa32835SJeff Kirsher static void emac_mdio_write(struct net_device *ndev, int id, int reg, int val)
9369aa32835SJeff Kirsher {
9379aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
9389aa32835SJeff Kirsher 
9399aa32835SJeff Kirsher 	__emac_mdio_write((dev->mdio_instance &&
9409aa32835SJeff Kirsher 			   dev->phy.gpcs_address != id) ?
9419aa32835SJeff Kirsher 			   dev->mdio_instance : dev,
9429aa32835SJeff Kirsher 			  (u8) id, (u8) reg, (u16) val);
9439aa32835SJeff Kirsher }
9449aa32835SJeff Kirsher 
9459aa32835SJeff Kirsher /* Tx lock BH */
__emac_set_multicast_list(struct emac_instance * dev)9469aa32835SJeff Kirsher static void __emac_set_multicast_list(struct emac_instance *dev)
9479aa32835SJeff Kirsher {
9489aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
9499aa32835SJeff Kirsher 	u32 rmr = emac_iff2rmr(dev->ndev);
9509aa32835SJeff Kirsher 
9519aa32835SJeff Kirsher 	DBG(dev, "__multicast %08x" NL, rmr);
9529aa32835SJeff Kirsher 
9539aa32835SJeff Kirsher 	/* I decided to relax register access rules here to avoid
9549aa32835SJeff Kirsher 	 * full EMAC reset.
9559aa32835SJeff Kirsher 	 *
9569aa32835SJeff Kirsher 	 * There is a real problem with EMAC4 core if we use MWSW_001 bit
9579aa32835SJeff Kirsher 	 * in MR1 register and do a full EMAC reset.
9589aa32835SJeff Kirsher 	 * One TX BD status update is delayed and, after EMAC reset, it
9599aa32835SJeff Kirsher 	 * never happens, resulting in TX hung (it'll be recovered by TX
9609aa32835SJeff Kirsher 	 * timeout handler eventually, but this is just gross).
9619aa32835SJeff Kirsher 	 * So we either have to do full TX reset or try to cheat here :)
9629aa32835SJeff Kirsher 	 *
9639aa32835SJeff Kirsher 	 * The only required change is to RX mode register, so I *think* all
9649aa32835SJeff Kirsher 	 * we need is just to stop RX channel. This seems to work on all
9659aa32835SJeff Kirsher 	 * tested SoCs.                                                --ebs
9669aa32835SJeff Kirsher 	 *
9679aa32835SJeff Kirsher 	 */
9689aa32835SJeff Kirsher 	dev->mcast_pending = 0;
9699aa32835SJeff Kirsher 	emac_rx_disable(dev);
9709aa32835SJeff Kirsher 	if (rmr & EMAC_RMR_MAE)
9719aa32835SJeff Kirsher 		emac_hash_mc(dev);
9729aa32835SJeff Kirsher 	out_be32(&p->rmr, rmr);
9739aa32835SJeff Kirsher 	emac_rx_enable(dev);
9749aa32835SJeff Kirsher }
9759aa32835SJeff Kirsher 
9769aa32835SJeff Kirsher /* Tx lock BH */
emac_set_multicast_list(struct net_device * ndev)9779aa32835SJeff Kirsher static void emac_set_multicast_list(struct net_device *ndev)
9789aa32835SJeff Kirsher {
9799aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
9809aa32835SJeff Kirsher 
9819aa32835SJeff Kirsher 	DBG(dev, "multicast" NL);
9829aa32835SJeff Kirsher 
9839aa32835SJeff Kirsher 	BUG_ON(!netif_running(dev->ndev));
9849aa32835SJeff Kirsher 
9859aa32835SJeff Kirsher 	if (dev->no_mcast) {
9869aa32835SJeff Kirsher 		dev->mcast_pending = 1;
9879aa32835SJeff Kirsher 		return;
9889aa32835SJeff Kirsher 	}
9897106a069SIvan Mikhaylov 
9907106a069SIvan Mikhaylov 	mutex_lock(&dev->link_lock);
9919aa32835SJeff Kirsher 	__emac_set_multicast_list(dev);
9927106a069SIvan Mikhaylov 	mutex_unlock(&dev->link_lock);
9939aa32835SJeff Kirsher }
9949aa32835SJeff Kirsher 
emac_set_mac_address(struct net_device * ndev,void * sa)99501afd972SIvan Mikhaylov static int emac_set_mac_address(struct net_device *ndev, void *sa)
99601afd972SIvan Mikhaylov {
99701afd972SIvan Mikhaylov 	struct emac_instance *dev = netdev_priv(ndev);
99801afd972SIvan Mikhaylov 	struct sockaddr *addr = sa;
99901afd972SIvan Mikhaylov 	struct emac_regs __iomem *p = dev->emacp;
100001afd972SIvan Mikhaylov 
100101afd972SIvan Mikhaylov 	if (!is_valid_ether_addr(addr->sa_data))
100201afd972SIvan Mikhaylov 	       return -EADDRNOTAVAIL;
100301afd972SIvan Mikhaylov 
100401afd972SIvan Mikhaylov 	mutex_lock(&dev->link_lock);
100501afd972SIvan Mikhaylov 
1006a05e4c0aSJakub Kicinski 	eth_hw_addr_set(ndev, addr->sa_data);
100701afd972SIvan Mikhaylov 
100801afd972SIvan Mikhaylov 	emac_rx_disable(dev);
100901afd972SIvan Mikhaylov 	emac_tx_disable(dev);
101001afd972SIvan Mikhaylov 	out_be32(&p->iahr, (ndev->dev_addr[0] << 8) | ndev->dev_addr[1]);
101101afd972SIvan Mikhaylov 	out_be32(&p->ialr, (ndev->dev_addr[2] << 24) |
101201afd972SIvan Mikhaylov 		(ndev->dev_addr[3] << 16) | (ndev->dev_addr[4] << 8) |
101301afd972SIvan Mikhaylov 		ndev->dev_addr[5]);
101401afd972SIvan Mikhaylov 	emac_tx_enable(dev);
101501afd972SIvan Mikhaylov 	emac_rx_enable(dev);
101601afd972SIvan Mikhaylov 
101701afd972SIvan Mikhaylov 	mutex_unlock(&dev->link_lock);
101801afd972SIvan Mikhaylov 
101901afd972SIvan Mikhaylov 	return 0;
10209aa32835SJeff Kirsher }
10219aa32835SJeff Kirsher 
emac_resize_rx_ring(struct emac_instance * dev,int new_mtu)10229aa32835SJeff Kirsher static int emac_resize_rx_ring(struct emac_instance *dev, int new_mtu)
10239aa32835SJeff Kirsher {
10249aa32835SJeff Kirsher 	int rx_sync_size = emac_rx_sync_size(new_mtu);
10259aa32835SJeff Kirsher 	int rx_skb_size = emac_rx_skb_size(new_mtu);
10269aa32835SJeff Kirsher 	int i, ret = 0;
1027ae5d3372SDuc Dang 	int mr1_jumbo_bit_change = 0;
10289aa32835SJeff Kirsher 
10299aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
10309aa32835SJeff Kirsher 	emac_netif_stop(dev);
10319aa32835SJeff Kirsher 	emac_rx_disable(dev);
10329aa32835SJeff Kirsher 	mal_disable_rx_channel(dev->mal, dev->mal_rx_chan);
10339aa32835SJeff Kirsher 
10349aa32835SJeff Kirsher 	if (dev->rx_sg_skb) {
10359aa32835SJeff Kirsher 		++dev->estats.rx_dropped_resize;
10369aa32835SJeff Kirsher 		dev_kfree_skb(dev->rx_sg_skb);
10379aa32835SJeff Kirsher 		dev->rx_sg_skb = NULL;
10389aa32835SJeff Kirsher 	}
10399aa32835SJeff Kirsher 
10409aa32835SJeff Kirsher 	/* Make a first pass over RX ring and mark BDs ready, dropping
10419aa32835SJeff Kirsher 	 * non-processed packets on the way. We need this as a separate pass
10429aa32835SJeff Kirsher 	 * to simplify error recovery in the case of allocation failure later.
10439aa32835SJeff Kirsher 	 */
10449aa32835SJeff Kirsher 	for (i = 0; i < NUM_RX_BUFF; ++i) {
10459aa32835SJeff Kirsher 		if (dev->rx_desc[i].ctrl & MAL_RX_CTRL_FIRST)
10469aa32835SJeff Kirsher 			++dev->estats.rx_dropped_resize;
10479aa32835SJeff Kirsher 
10489aa32835SJeff Kirsher 		dev->rx_desc[i].data_len = 0;
10499aa32835SJeff Kirsher 		dev->rx_desc[i].ctrl = MAL_RX_CTRL_EMPTY |
10509aa32835SJeff Kirsher 		    (i == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
10519aa32835SJeff Kirsher 	}
10529aa32835SJeff Kirsher 
10539aa32835SJeff Kirsher 	/* Reallocate RX ring only if bigger skb buffers are required */
10549aa32835SJeff Kirsher 	if (rx_skb_size <= dev->rx_skb_size)
10559aa32835SJeff Kirsher 		goto skip;
10569aa32835SJeff Kirsher 
10579aa32835SJeff Kirsher 	/* Second pass, allocate new skbs */
10589aa32835SJeff Kirsher 	for (i = 0; i < NUM_RX_BUFF; ++i) {
105922087d65SChristian Lamparter 		struct sk_buff *skb;
106022087d65SChristian Lamparter 
106122087d65SChristian Lamparter 		skb = netdev_alloc_skb_ip_align(dev->ndev, rx_skb_size);
10629aa32835SJeff Kirsher 		if (!skb) {
10639aa32835SJeff Kirsher 			ret = -ENOMEM;
10649aa32835SJeff Kirsher 			goto oom;
10659aa32835SJeff Kirsher 		}
10669aa32835SJeff Kirsher 
10679aa32835SJeff Kirsher 		BUG_ON(!dev->rx_skb[i]);
10689aa32835SJeff Kirsher 		dev_kfree_skb(dev->rx_skb[i]);
10699aa32835SJeff Kirsher 
10709aa32835SJeff Kirsher 		dev->rx_desc[i].data_ptr =
107122087d65SChristian Lamparter 		    dma_map_single(&dev->ofdev->dev, skb->data - NET_IP_ALIGN,
107222087d65SChristian Lamparter 				   rx_sync_size, DMA_FROM_DEVICE)
107322087d65SChristian Lamparter 				   + NET_IP_ALIGN;
10749aa32835SJeff Kirsher 		dev->rx_skb[i] = skb;
10759aa32835SJeff Kirsher 	}
10769aa32835SJeff Kirsher  skip:
10779aa32835SJeff Kirsher 	/* Check if we need to change "Jumbo" bit in MR1 */
1078ae5d3372SDuc Dang 	if (emac_has_feature(dev, EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE)) {
1079ae5d3372SDuc Dang 		mr1_jumbo_bit_change = (new_mtu > ETH_DATA_LEN) ||
1080ae5d3372SDuc Dang 				(dev->ndev->mtu > ETH_DATA_LEN);
1081ae5d3372SDuc Dang 	} else {
1082ae5d3372SDuc Dang 		mr1_jumbo_bit_change = (new_mtu > ETH_DATA_LEN) ^
1083ae5d3372SDuc Dang 				(dev->ndev->mtu > ETH_DATA_LEN);
1084ae5d3372SDuc Dang 	}
1085ae5d3372SDuc Dang 
1086ae5d3372SDuc Dang 	if (mr1_jumbo_bit_change) {
10879aa32835SJeff Kirsher 		/* This is to prevent starting RX channel in emac_rx_enable() */
10889aa32835SJeff Kirsher 		set_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
10899aa32835SJeff Kirsher 
10901eb2cdedSEric Dumazet 		WRITE_ONCE(dev->ndev->mtu, new_mtu);
10919aa32835SJeff Kirsher 		emac_full_tx_reset(dev);
10929aa32835SJeff Kirsher 	}
10939aa32835SJeff Kirsher 
10949aa32835SJeff Kirsher 	mal_set_rcbs(dev->mal, dev->mal_rx_chan, emac_rx_size(new_mtu));
10959aa32835SJeff Kirsher  oom:
10969aa32835SJeff Kirsher 	/* Restart RX */
10979aa32835SJeff Kirsher 	clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
10989aa32835SJeff Kirsher 	dev->rx_slot = 0;
10999aa32835SJeff Kirsher 	mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
11009aa32835SJeff Kirsher 	emac_rx_enable(dev);
11019aa32835SJeff Kirsher 	emac_netif_start(dev);
11029aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
11039aa32835SJeff Kirsher 
11049aa32835SJeff Kirsher 	return ret;
11059aa32835SJeff Kirsher }
11069aa32835SJeff Kirsher 
11079aa32835SJeff Kirsher /* Process ctx, rtnl_lock semaphore */
emac_change_mtu(struct net_device * ndev,int new_mtu)11089aa32835SJeff Kirsher static int emac_change_mtu(struct net_device *ndev, int new_mtu)
11099aa32835SJeff Kirsher {
11109aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
11119aa32835SJeff Kirsher 	int ret = 0;
11129aa32835SJeff Kirsher 
11139aa32835SJeff Kirsher 	DBG(dev, "change_mtu(%d)" NL, new_mtu);
11149aa32835SJeff Kirsher 
11159aa32835SJeff Kirsher 	if (netif_running(ndev)) {
11169aa32835SJeff Kirsher 		/* Check if we really need to reinitialize RX ring */
11179aa32835SJeff Kirsher 		if (emac_rx_skb_size(ndev->mtu) != emac_rx_skb_size(new_mtu))
11189aa32835SJeff Kirsher 			ret = emac_resize_rx_ring(dev, new_mtu);
11199aa32835SJeff Kirsher 	}
11209aa32835SJeff Kirsher 
11219aa32835SJeff Kirsher 	if (!ret) {
11221eb2cdedSEric Dumazet 		WRITE_ONCE(ndev->mtu, new_mtu);
11239aa32835SJeff Kirsher 		dev->rx_skb_size = emac_rx_skb_size(new_mtu);
11249aa32835SJeff Kirsher 		dev->rx_sync_size = emac_rx_sync_size(new_mtu);
11259aa32835SJeff Kirsher 	}
11269aa32835SJeff Kirsher 
11279aa32835SJeff Kirsher 	return ret;
11289aa32835SJeff Kirsher }
11299aa32835SJeff Kirsher 
emac_clean_tx_ring(struct emac_instance * dev)11309aa32835SJeff Kirsher static void emac_clean_tx_ring(struct emac_instance *dev)
11319aa32835SJeff Kirsher {
11329aa32835SJeff Kirsher 	int i;
11339aa32835SJeff Kirsher 
11349aa32835SJeff Kirsher 	for (i = 0; i < NUM_TX_BUFF; ++i) {
11359aa32835SJeff Kirsher 		if (dev->tx_skb[i]) {
11369aa32835SJeff Kirsher 			dev_kfree_skb(dev->tx_skb[i]);
11379aa32835SJeff Kirsher 			dev->tx_skb[i] = NULL;
11389aa32835SJeff Kirsher 			if (dev->tx_desc[i].ctrl & MAL_TX_CTRL_READY)
11399aa32835SJeff Kirsher 				++dev->estats.tx_dropped;
11409aa32835SJeff Kirsher 		}
11419aa32835SJeff Kirsher 		dev->tx_desc[i].ctrl = 0;
11429aa32835SJeff Kirsher 		dev->tx_desc[i].data_ptr = 0;
11439aa32835SJeff Kirsher 	}
11449aa32835SJeff Kirsher }
11459aa32835SJeff Kirsher 
emac_clean_rx_ring(struct emac_instance * dev)11469aa32835SJeff Kirsher static void emac_clean_rx_ring(struct emac_instance *dev)
11479aa32835SJeff Kirsher {
11489aa32835SJeff Kirsher 	int i;
11499aa32835SJeff Kirsher 
11509aa32835SJeff Kirsher 	for (i = 0; i < NUM_RX_BUFF; ++i)
11519aa32835SJeff Kirsher 		if (dev->rx_skb[i]) {
11529aa32835SJeff Kirsher 			dev->rx_desc[i].ctrl = 0;
11539aa32835SJeff Kirsher 			dev_kfree_skb(dev->rx_skb[i]);
11549aa32835SJeff Kirsher 			dev->rx_skb[i] = NULL;
11559aa32835SJeff Kirsher 			dev->rx_desc[i].data_ptr = 0;
11569aa32835SJeff Kirsher 		}
11579aa32835SJeff Kirsher 
11589aa32835SJeff Kirsher 	if (dev->rx_sg_skb) {
11599aa32835SJeff Kirsher 		dev_kfree_skb(dev->rx_sg_skb);
11609aa32835SJeff Kirsher 		dev->rx_sg_skb = NULL;
11619aa32835SJeff Kirsher 	}
11629aa32835SJeff Kirsher }
11639aa32835SJeff Kirsher 
116422087d65SChristian Lamparter static int
__emac_prepare_rx_skb(struct sk_buff * skb,struct emac_instance * dev,int slot)116522087d65SChristian Lamparter __emac_prepare_rx_skb(struct sk_buff *skb, struct emac_instance *dev, int slot)
11669aa32835SJeff Kirsher {
11679aa32835SJeff Kirsher 	if (unlikely(!skb))
11689aa32835SJeff Kirsher 		return -ENOMEM;
11699aa32835SJeff Kirsher 
11709aa32835SJeff Kirsher 	dev->rx_skb[slot] = skb;
11719aa32835SJeff Kirsher 	dev->rx_desc[slot].data_len = 0;
11729aa32835SJeff Kirsher 
11739aa32835SJeff Kirsher 	dev->rx_desc[slot].data_ptr =
117422087d65SChristian Lamparter 	    dma_map_single(&dev->ofdev->dev, skb->data - NET_IP_ALIGN,
117522087d65SChristian Lamparter 			   dev->rx_sync_size, DMA_FROM_DEVICE) + NET_IP_ALIGN;
11769aa32835SJeff Kirsher 	wmb();
11779aa32835SJeff Kirsher 	dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
11789aa32835SJeff Kirsher 	    (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
11799aa32835SJeff Kirsher 
11809aa32835SJeff Kirsher 	return 0;
11819aa32835SJeff Kirsher }
11829aa32835SJeff Kirsher 
118322087d65SChristian Lamparter static int
emac_alloc_rx_skb(struct emac_instance * dev,int slot)118422087d65SChristian Lamparter emac_alloc_rx_skb(struct emac_instance *dev, int slot)
118522087d65SChristian Lamparter {
118622087d65SChristian Lamparter 	struct sk_buff *skb;
118722087d65SChristian Lamparter 
118822087d65SChristian Lamparter 	skb = __netdev_alloc_skb_ip_align(dev->ndev, dev->rx_skb_size,
118922087d65SChristian Lamparter 					  GFP_KERNEL);
119022087d65SChristian Lamparter 
119122087d65SChristian Lamparter 	return __emac_prepare_rx_skb(skb, dev, slot);
119222087d65SChristian Lamparter }
119322087d65SChristian Lamparter 
119422087d65SChristian Lamparter static int
emac_alloc_rx_skb_napi(struct emac_instance * dev,int slot)119522087d65SChristian Lamparter emac_alloc_rx_skb_napi(struct emac_instance *dev, int slot)
119622087d65SChristian Lamparter {
119722087d65SChristian Lamparter 	struct sk_buff *skb;
119822087d65SChristian Lamparter 
119922087d65SChristian Lamparter 	skb = napi_alloc_skb(&dev->mal->napi, dev->rx_skb_size);
120022087d65SChristian Lamparter 
120122087d65SChristian Lamparter 	return __emac_prepare_rx_skb(skb, dev, slot);
120222087d65SChristian Lamparter }
120322087d65SChristian Lamparter 
emac_print_link_status(struct emac_instance * dev)12049aa32835SJeff Kirsher static void emac_print_link_status(struct emac_instance *dev)
12059aa32835SJeff Kirsher {
12069aa32835SJeff Kirsher 	if (netif_carrier_ok(dev->ndev))
12079aa32835SJeff Kirsher 		printk(KERN_INFO "%s: link is up, %d %s%s\n",
12089aa32835SJeff Kirsher 		       dev->ndev->name, dev->phy.speed,
12099aa32835SJeff Kirsher 		       dev->phy.duplex == DUPLEX_FULL ? "FDX" : "HDX",
12109aa32835SJeff Kirsher 		       dev->phy.pause ? ", pause enabled" :
12119aa32835SJeff Kirsher 		       dev->phy.asym_pause ? ", asymmetric pause enabled" : "");
12129aa32835SJeff Kirsher 	else
12139aa32835SJeff Kirsher 		printk(KERN_INFO "%s: link is down\n", dev->ndev->name);
12149aa32835SJeff Kirsher }
12159aa32835SJeff Kirsher 
12169aa32835SJeff Kirsher /* Process ctx, rtnl_lock semaphore */
emac_open(struct net_device * ndev)12179aa32835SJeff Kirsher static int emac_open(struct net_device *ndev)
12189aa32835SJeff Kirsher {
12199aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
1220dcc34ef7SRosen Penev 	int i;
12219aa32835SJeff Kirsher 
12229aa32835SJeff Kirsher 	DBG(dev, "open" NL);
12239aa32835SJeff Kirsher 
12249aa32835SJeff Kirsher 	/* Allocate RX ring */
12259aa32835SJeff Kirsher 	for (i = 0; i < NUM_RX_BUFF; ++i)
122622087d65SChristian Lamparter 		if (emac_alloc_rx_skb(dev, i)) {
12279aa32835SJeff Kirsher 			printk(KERN_ERR "%s: failed to allocate RX ring\n",
12289aa32835SJeff Kirsher 			       ndev->name);
12299aa32835SJeff Kirsher 			goto oom;
12309aa32835SJeff Kirsher 		}
12319aa32835SJeff Kirsher 
12329aa32835SJeff Kirsher 	dev->tx_cnt = dev->tx_slot = dev->ack_slot = dev->rx_slot = 0;
12339aa32835SJeff Kirsher 	clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
12349aa32835SJeff Kirsher 	dev->rx_sg_skb = NULL;
12359aa32835SJeff Kirsher 
12369aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
12379aa32835SJeff Kirsher 	dev->opened = 1;
12389aa32835SJeff Kirsher 
12399aa32835SJeff Kirsher 	/* Start PHY polling now.
12409aa32835SJeff Kirsher 	 */
12419aa32835SJeff Kirsher 	if (dev->phy.address >= 0) {
12429aa32835SJeff Kirsher 		int link_poll_interval;
12439aa32835SJeff Kirsher 		if (dev->phy.def->ops->poll_link(&dev->phy)) {
12449aa32835SJeff Kirsher 			dev->phy.def->ops->read_link(&dev->phy);
12459aa32835SJeff Kirsher 			emac_rx_clk_default(dev);
12469aa32835SJeff Kirsher 			netif_carrier_on(dev->ndev);
12479aa32835SJeff Kirsher 			link_poll_interval = PHY_POLL_LINK_ON;
12489aa32835SJeff Kirsher 		} else {
12499aa32835SJeff Kirsher 			emac_rx_clk_tx(dev);
12509aa32835SJeff Kirsher 			netif_carrier_off(dev->ndev);
12519aa32835SJeff Kirsher 			link_poll_interval = PHY_POLL_LINK_OFF;
12529aa32835SJeff Kirsher 		}
12539aa32835SJeff Kirsher 		dev->link_polling = 1;
12549aa32835SJeff Kirsher 		wmb();
12559aa32835SJeff Kirsher 		schedule_delayed_work(&dev->link_work, link_poll_interval);
12569aa32835SJeff Kirsher 		emac_print_link_status(dev);
12579aa32835SJeff Kirsher 	} else
12589aa32835SJeff Kirsher 		netif_carrier_on(dev->ndev);
12599aa32835SJeff Kirsher 
12609aa32835SJeff Kirsher 	/* Required for Pause packet support in EMAC */
12619aa32835SJeff Kirsher 	dev_mc_add_global(ndev, default_mcast_addr);
12629aa32835SJeff Kirsher 
12639aa32835SJeff Kirsher 	emac_configure(dev);
12649aa32835SJeff Kirsher 	mal_poll_add(dev->mal, &dev->commac);
12659aa32835SJeff Kirsher 	mal_enable_tx_channel(dev->mal, dev->mal_tx_chan);
12669aa32835SJeff Kirsher 	mal_set_rcbs(dev->mal, dev->mal_rx_chan, emac_rx_size(ndev->mtu));
12679aa32835SJeff Kirsher 	mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
12689aa32835SJeff Kirsher 	emac_tx_enable(dev);
12699aa32835SJeff Kirsher 	emac_rx_enable(dev);
12709aa32835SJeff Kirsher 	emac_netif_start(dev);
12719aa32835SJeff Kirsher 
12729aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
12739aa32835SJeff Kirsher 
12749aa32835SJeff Kirsher 	return 0;
12759aa32835SJeff Kirsher  oom:
12769aa32835SJeff Kirsher 	emac_clean_rx_ring(dev);
12779aa32835SJeff Kirsher 	return -ENOMEM;
12789aa32835SJeff Kirsher }
12799aa32835SJeff Kirsher 
12809aa32835SJeff Kirsher /* BHs disabled */
12819aa32835SJeff Kirsher #if 0
12829aa32835SJeff Kirsher static int emac_link_differs(struct emac_instance *dev)
12839aa32835SJeff Kirsher {
12849aa32835SJeff Kirsher 	u32 r = in_be32(&dev->emacp->mr1);
12859aa32835SJeff Kirsher 
12869aa32835SJeff Kirsher 	int duplex = r & EMAC_MR1_FDE ? DUPLEX_FULL : DUPLEX_HALF;
12879aa32835SJeff Kirsher 	int speed, pause, asym_pause;
12889aa32835SJeff Kirsher 
12899aa32835SJeff Kirsher 	if (r & EMAC_MR1_MF_1000)
12909aa32835SJeff Kirsher 		speed = SPEED_1000;
12919aa32835SJeff Kirsher 	else if (r & EMAC_MR1_MF_100)
12929aa32835SJeff Kirsher 		speed = SPEED_100;
12939aa32835SJeff Kirsher 	else
12949aa32835SJeff Kirsher 		speed = SPEED_10;
12959aa32835SJeff Kirsher 
12969aa32835SJeff Kirsher 	switch (r & (EMAC_MR1_EIFC | EMAC_MR1_APP)) {
12979aa32835SJeff Kirsher 	case (EMAC_MR1_EIFC | EMAC_MR1_APP):
12989aa32835SJeff Kirsher 		pause = 1;
12999aa32835SJeff Kirsher 		asym_pause = 0;
13009aa32835SJeff Kirsher 		break;
13019aa32835SJeff Kirsher 	case EMAC_MR1_APP:
13029aa32835SJeff Kirsher 		pause = 0;
13039aa32835SJeff Kirsher 		asym_pause = 1;
13049aa32835SJeff Kirsher 		break;
13059aa32835SJeff Kirsher 	default:
13069aa32835SJeff Kirsher 		pause = asym_pause = 0;
13079aa32835SJeff Kirsher 	}
13089aa32835SJeff Kirsher 	return speed != dev->phy.speed || duplex != dev->phy.duplex ||
13099aa32835SJeff Kirsher 	    pause != dev->phy.pause || asym_pause != dev->phy.asym_pause;
13109aa32835SJeff Kirsher }
13119aa32835SJeff Kirsher #endif
13129aa32835SJeff Kirsher 
emac_link_timer(struct work_struct * work)13139aa32835SJeff Kirsher static void emac_link_timer(struct work_struct *work)
13149aa32835SJeff Kirsher {
13159aa32835SJeff Kirsher 	struct emac_instance *dev =
13169aa32835SJeff Kirsher 		container_of(to_delayed_work(work),
13179aa32835SJeff Kirsher 			     struct emac_instance, link_work);
13189aa32835SJeff Kirsher 	int link_poll_interval;
13199aa32835SJeff Kirsher 
13209aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
13219aa32835SJeff Kirsher 	DBG2(dev, "link timer" NL);
13229aa32835SJeff Kirsher 
13239aa32835SJeff Kirsher 	if (!dev->opened)
13249aa32835SJeff Kirsher 		goto bail;
13259aa32835SJeff Kirsher 
13269aa32835SJeff Kirsher 	if (dev->phy.def->ops->poll_link(&dev->phy)) {
13279aa32835SJeff Kirsher 		if (!netif_carrier_ok(dev->ndev)) {
13289aa32835SJeff Kirsher 			emac_rx_clk_default(dev);
13299aa32835SJeff Kirsher 			/* Get new link parameters */
13309aa32835SJeff Kirsher 			dev->phy.def->ops->read_link(&dev->phy);
13319aa32835SJeff Kirsher 
13329aa32835SJeff Kirsher 			netif_carrier_on(dev->ndev);
13339aa32835SJeff Kirsher 			emac_netif_stop(dev);
13349aa32835SJeff Kirsher 			emac_full_tx_reset(dev);
13359aa32835SJeff Kirsher 			emac_netif_start(dev);
13369aa32835SJeff Kirsher 			emac_print_link_status(dev);
13379aa32835SJeff Kirsher 		}
13389aa32835SJeff Kirsher 		link_poll_interval = PHY_POLL_LINK_ON;
13399aa32835SJeff Kirsher 	} else {
13409aa32835SJeff Kirsher 		if (netif_carrier_ok(dev->ndev)) {
13419aa32835SJeff Kirsher 			emac_rx_clk_tx(dev);
13429aa32835SJeff Kirsher 			netif_carrier_off(dev->ndev);
13439aa32835SJeff Kirsher 			netif_tx_disable(dev->ndev);
13449aa32835SJeff Kirsher 			emac_reinitialize(dev);
13459aa32835SJeff Kirsher 			emac_print_link_status(dev);
13469aa32835SJeff Kirsher 		}
13479aa32835SJeff Kirsher 		link_poll_interval = PHY_POLL_LINK_OFF;
13489aa32835SJeff Kirsher 	}
13499aa32835SJeff Kirsher 	schedule_delayed_work(&dev->link_work, link_poll_interval);
13509aa32835SJeff Kirsher  bail:
13519aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
13529aa32835SJeff Kirsher }
13539aa32835SJeff Kirsher 
emac_force_link_update(struct emac_instance * dev)13549aa32835SJeff Kirsher static void emac_force_link_update(struct emac_instance *dev)
13559aa32835SJeff Kirsher {
13569aa32835SJeff Kirsher 	netif_carrier_off(dev->ndev);
13579aa32835SJeff Kirsher 	smp_rmb();
13589aa32835SJeff Kirsher 	if (dev->link_polling) {
13599aa32835SJeff Kirsher 		cancel_delayed_work_sync(&dev->link_work);
13609aa32835SJeff Kirsher 		if (dev->link_polling)
13619aa32835SJeff Kirsher 			schedule_delayed_work(&dev->link_work,  PHY_POLL_LINK_OFF);
13629aa32835SJeff Kirsher 	}
13639aa32835SJeff Kirsher }
13649aa32835SJeff Kirsher 
13659aa32835SJeff Kirsher /* Process ctx, rtnl_lock semaphore */
emac_close(struct net_device * ndev)13669aa32835SJeff Kirsher static int emac_close(struct net_device *ndev)
13679aa32835SJeff Kirsher {
13689aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
13699aa32835SJeff Kirsher 
13709aa32835SJeff Kirsher 	DBG(dev, "close" NL);
13719aa32835SJeff Kirsher 
13729aa32835SJeff Kirsher 	if (dev->phy.address >= 0) {
13739aa32835SJeff Kirsher 		dev->link_polling = 0;
13749aa32835SJeff Kirsher 		cancel_delayed_work_sync(&dev->link_work);
13759aa32835SJeff Kirsher 	}
13769aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
13779aa32835SJeff Kirsher 	emac_netif_stop(dev);
13789aa32835SJeff Kirsher 	dev->opened = 0;
13799aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
13809aa32835SJeff Kirsher 
13819aa32835SJeff Kirsher 	emac_rx_disable(dev);
13829aa32835SJeff Kirsher 	emac_tx_disable(dev);
13839aa32835SJeff Kirsher 	mal_disable_rx_channel(dev->mal, dev->mal_rx_chan);
13849aa32835SJeff Kirsher 	mal_disable_tx_channel(dev->mal, dev->mal_tx_chan);
13859aa32835SJeff Kirsher 	mal_poll_del(dev->mal, &dev->commac);
13869aa32835SJeff Kirsher 
13879aa32835SJeff Kirsher 	emac_clean_tx_ring(dev);
13889aa32835SJeff Kirsher 	emac_clean_rx_ring(dev);
13899aa32835SJeff Kirsher 
13909aa32835SJeff Kirsher 	netif_carrier_off(ndev);
13919aa32835SJeff Kirsher 
13929aa32835SJeff Kirsher 	return 0;
13939aa32835SJeff Kirsher }
13949aa32835SJeff Kirsher 
emac_tx_csum(struct emac_instance * dev,struct sk_buff * skb)13959aa32835SJeff Kirsher static inline u16 emac_tx_csum(struct emac_instance *dev,
13969aa32835SJeff Kirsher 			       struct sk_buff *skb)
13979aa32835SJeff Kirsher {
13989aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH) &&
13999aa32835SJeff Kirsher 		(skb->ip_summed == CHECKSUM_PARTIAL)) {
14009aa32835SJeff Kirsher 		++dev->stats.tx_packets_csum;
14019aa32835SJeff Kirsher 		return EMAC_TX_CTRL_TAH_CSUM;
14029aa32835SJeff Kirsher 	}
14039aa32835SJeff Kirsher 	return 0;
14049aa32835SJeff Kirsher }
14059aa32835SJeff Kirsher 
emac_xmit_finish(struct emac_instance * dev,int len)140694b2bb28SYueHaibing static inline netdev_tx_t emac_xmit_finish(struct emac_instance *dev, int len)
14079aa32835SJeff Kirsher {
14089aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
14099aa32835SJeff Kirsher 	struct net_device *ndev = dev->ndev;
14109aa32835SJeff Kirsher 
14119aa32835SJeff Kirsher 	/* Send the packet out. If the if makes a significant perf
14129aa32835SJeff Kirsher 	 * difference, then we can store the TMR0 value in "dev"
14139aa32835SJeff Kirsher 	 * instead
14149aa32835SJeff Kirsher 	 */
14159aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4))
14169aa32835SJeff Kirsher 		out_be32(&p->tmr0, EMAC4_TMR0_XMIT);
14179aa32835SJeff Kirsher 	else
14189aa32835SJeff Kirsher 		out_be32(&p->tmr0, EMAC_TMR0_XMIT);
14199aa32835SJeff Kirsher 
14209aa32835SJeff Kirsher 	if (unlikely(++dev->tx_cnt == NUM_TX_BUFF)) {
14219aa32835SJeff Kirsher 		netif_stop_queue(ndev);
14229aa32835SJeff Kirsher 		DBG2(dev, "stopped TX queue" NL);
14239aa32835SJeff Kirsher 	}
14249aa32835SJeff Kirsher 
1425860e9538SFlorian Westphal 	netif_trans_update(ndev);
14269aa32835SJeff Kirsher 	++dev->stats.tx_packets;
14279aa32835SJeff Kirsher 	dev->stats.tx_bytes += len;
14289aa32835SJeff Kirsher 
14299aa32835SJeff Kirsher 	return NETDEV_TX_OK;
14309aa32835SJeff Kirsher }
14319aa32835SJeff Kirsher 
14329aa32835SJeff Kirsher /* Tx lock BH */
emac_start_xmit(struct sk_buff * skb,struct net_device * ndev)143394b2bb28SYueHaibing static netdev_tx_t emac_start_xmit(struct sk_buff *skb, struct net_device *ndev)
14349aa32835SJeff Kirsher {
14359aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
14369aa32835SJeff Kirsher 	unsigned int len = skb->len;
14379aa32835SJeff Kirsher 	int slot;
14389aa32835SJeff Kirsher 
14399aa32835SJeff Kirsher 	u16 ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
14409aa32835SJeff Kirsher 	    MAL_TX_CTRL_LAST | emac_tx_csum(dev, skb);
14419aa32835SJeff Kirsher 
14429aa32835SJeff Kirsher 	slot = dev->tx_slot++;
14439aa32835SJeff Kirsher 	if (dev->tx_slot == NUM_TX_BUFF) {
14449aa32835SJeff Kirsher 		dev->tx_slot = 0;
14459aa32835SJeff Kirsher 		ctrl |= MAL_TX_CTRL_WRAP;
14469aa32835SJeff Kirsher 	}
14479aa32835SJeff Kirsher 
14489aa32835SJeff Kirsher 	DBG2(dev, "xmit(%u) %d" NL, len, slot);
14499aa32835SJeff Kirsher 
14509aa32835SJeff Kirsher 	dev->tx_skb[slot] = skb;
14519aa32835SJeff Kirsher 	dev->tx_desc[slot].data_ptr = dma_map_single(&dev->ofdev->dev,
14529aa32835SJeff Kirsher 						     skb->data, len,
14539aa32835SJeff Kirsher 						     DMA_TO_DEVICE);
14549aa32835SJeff Kirsher 	dev->tx_desc[slot].data_len = (u16) len;
14559aa32835SJeff Kirsher 	wmb();
14569aa32835SJeff Kirsher 	dev->tx_desc[slot].ctrl = ctrl;
14579aa32835SJeff Kirsher 
14589aa32835SJeff Kirsher 	return emac_xmit_finish(dev, len);
14599aa32835SJeff Kirsher }
14609aa32835SJeff Kirsher 
emac_xmit_split(struct emac_instance * dev,int slot,u32 pd,int len,int last,u16 base_ctrl)14619aa32835SJeff Kirsher static inline int emac_xmit_split(struct emac_instance *dev, int slot,
14629aa32835SJeff Kirsher 				  u32 pd, int len, int last, u16 base_ctrl)
14639aa32835SJeff Kirsher {
14649aa32835SJeff Kirsher 	while (1) {
14659aa32835SJeff Kirsher 		u16 ctrl = base_ctrl;
14669aa32835SJeff Kirsher 		int chunk = min(len, MAL_MAX_TX_SIZE);
14679aa32835SJeff Kirsher 		len -= chunk;
14689aa32835SJeff Kirsher 
14699aa32835SJeff Kirsher 		slot = (slot + 1) % NUM_TX_BUFF;
14709aa32835SJeff Kirsher 
14719aa32835SJeff Kirsher 		if (last && !len)
14729aa32835SJeff Kirsher 			ctrl |= MAL_TX_CTRL_LAST;
14739aa32835SJeff Kirsher 		if (slot == NUM_TX_BUFF - 1)
14749aa32835SJeff Kirsher 			ctrl |= MAL_TX_CTRL_WRAP;
14759aa32835SJeff Kirsher 
14769aa32835SJeff Kirsher 		dev->tx_skb[slot] = NULL;
14779aa32835SJeff Kirsher 		dev->tx_desc[slot].data_ptr = pd;
14789aa32835SJeff Kirsher 		dev->tx_desc[slot].data_len = (u16) chunk;
14799aa32835SJeff Kirsher 		dev->tx_desc[slot].ctrl = ctrl;
14809aa32835SJeff Kirsher 		++dev->tx_cnt;
14819aa32835SJeff Kirsher 
14829aa32835SJeff Kirsher 		if (!len)
14839aa32835SJeff Kirsher 			break;
14849aa32835SJeff Kirsher 
14859aa32835SJeff Kirsher 		pd += chunk;
14869aa32835SJeff Kirsher 	}
14879aa32835SJeff Kirsher 	return slot;
14889aa32835SJeff Kirsher }
14899aa32835SJeff Kirsher 
14909aa32835SJeff Kirsher /* Tx lock BH disabled (SG version for TAH equipped EMACs) */
149194b2bb28SYueHaibing static netdev_tx_t
emac_start_xmit_sg(struct sk_buff * skb,struct net_device * ndev)149294b2bb28SYueHaibing emac_start_xmit_sg(struct sk_buff *skb, struct net_device *ndev)
14939aa32835SJeff Kirsher {
14949aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
14959aa32835SJeff Kirsher 	int nr_frags = skb_shinfo(skb)->nr_frags;
14969aa32835SJeff Kirsher 	int len = skb->len, chunk;
14979aa32835SJeff Kirsher 	int slot, i;
14989aa32835SJeff Kirsher 	u16 ctrl;
14999aa32835SJeff Kirsher 	u32 pd;
15009aa32835SJeff Kirsher 
15019aa32835SJeff Kirsher 	/* This is common "fast" path */
15029aa32835SJeff Kirsher 	if (likely(!nr_frags && len <= MAL_MAX_TX_SIZE))
15039aa32835SJeff Kirsher 		return emac_start_xmit(skb, ndev);
15049aa32835SJeff Kirsher 
15059aa32835SJeff Kirsher 	len -= skb->data_len;
15069aa32835SJeff Kirsher 
15079aa32835SJeff Kirsher 	/* Note, this is only an *estimation*, we can still run out of empty
15089aa32835SJeff Kirsher 	 * slots because of the additional fragmentation into
15099aa32835SJeff Kirsher 	 * MAL_MAX_TX_SIZE-sized chunks
15109aa32835SJeff Kirsher 	 */
15119aa32835SJeff Kirsher 	if (unlikely(dev->tx_cnt + nr_frags + mal_tx_chunks(len) > NUM_TX_BUFF))
15129aa32835SJeff Kirsher 		goto stop_queue;
15139aa32835SJeff Kirsher 
15149aa32835SJeff Kirsher 	ctrl = EMAC_TX_CTRL_GFCS | EMAC_TX_CTRL_GP | MAL_TX_CTRL_READY |
15159aa32835SJeff Kirsher 	    emac_tx_csum(dev, skb);
15169aa32835SJeff Kirsher 	slot = dev->tx_slot;
15179aa32835SJeff Kirsher 
15189aa32835SJeff Kirsher 	/* skb data */
15199aa32835SJeff Kirsher 	dev->tx_skb[slot] = NULL;
15209aa32835SJeff Kirsher 	chunk = min(len, MAL_MAX_TX_SIZE);
15219aa32835SJeff Kirsher 	dev->tx_desc[slot].data_ptr = pd =
15229aa32835SJeff Kirsher 	    dma_map_single(&dev->ofdev->dev, skb->data, len, DMA_TO_DEVICE);
15239aa32835SJeff Kirsher 	dev->tx_desc[slot].data_len = (u16) chunk;
15249aa32835SJeff Kirsher 	len -= chunk;
15259aa32835SJeff Kirsher 	if (unlikely(len))
15269aa32835SJeff Kirsher 		slot = emac_xmit_split(dev, slot, pd + chunk, len, !nr_frags,
15279aa32835SJeff Kirsher 				       ctrl);
15289aa32835SJeff Kirsher 	/* skb fragments */
15299aa32835SJeff Kirsher 	for (i = 0; i < nr_frags; ++i) {
1530d7840976SMatthew Wilcox (Oracle) 		skb_frag_t *frag = &skb_shinfo(skb)->frags[i];
15319e903e08SEric Dumazet 		len = skb_frag_size(frag);
15329aa32835SJeff Kirsher 
15339aa32835SJeff Kirsher 		if (unlikely(dev->tx_cnt + mal_tx_chunks(len) >= NUM_TX_BUFF))
15349aa32835SJeff Kirsher 			goto undo_frame;
15359aa32835SJeff Kirsher 
1536f8f114c2SIan Campbell 		pd = skb_frag_dma_map(&dev->ofdev->dev, frag, 0, len,
15379aa32835SJeff Kirsher 				      DMA_TO_DEVICE);
15389aa32835SJeff Kirsher 
15399aa32835SJeff Kirsher 		slot = emac_xmit_split(dev, slot, pd, len, i == nr_frags - 1,
15409aa32835SJeff Kirsher 				       ctrl);
15419aa32835SJeff Kirsher 	}
15429aa32835SJeff Kirsher 
15439aa32835SJeff Kirsher 	DBG2(dev, "xmit_sg(%u) %d - %d" NL, skb->len, dev->tx_slot, slot);
15449aa32835SJeff Kirsher 
15459aa32835SJeff Kirsher 	/* Attach skb to the last slot so we don't release it too early */
15469aa32835SJeff Kirsher 	dev->tx_skb[slot] = skb;
15479aa32835SJeff Kirsher 
15489aa32835SJeff Kirsher 	/* Send the packet out */
15499aa32835SJeff Kirsher 	if (dev->tx_slot == NUM_TX_BUFF - 1)
15509aa32835SJeff Kirsher 		ctrl |= MAL_TX_CTRL_WRAP;
15519aa32835SJeff Kirsher 	wmb();
15529aa32835SJeff Kirsher 	dev->tx_desc[dev->tx_slot].ctrl = ctrl;
15539aa32835SJeff Kirsher 	dev->tx_slot = (slot + 1) % NUM_TX_BUFF;
15549aa32835SJeff Kirsher 
15559aa32835SJeff Kirsher 	return emac_xmit_finish(dev, skb->len);
15569aa32835SJeff Kirsher 
15579aa32835SJeff Kirsher  undo_frame:
15589aa32835SJeff Kirsher 	/* Well, too bad. Our previous estimation was overly optimistic.
15599aa32835SJeff Kirsher 	 * Undo everything.
15609aa32835SJeff Kirsher 	 */
15619aa32835SJeff Kirsher 	while (slot != dev->tx_slot) {
15629aa32835SJeff Kirsher 		dev->tx_desc[slot].ctrl = 0;
15639aa32835SJeff Kirsher 		--dev->tx_cnt;
15649aa32835SJeff Kirsher 		if (--slot < 0)
15659aa32835SJeff Kirsher 			slot = NUM_TX_BUFF - 1;
15669aa32835SJeff Kirsher 	}
15679aa32835SJeff Kirsher 	++dev->estats.tx_undo;
15689aa32835SJeff Kirsher 
15699aa32835SJeff Kirsher  stop_queue:
15709aa32835SJeff Kirsher 	netif_stop_queue(ndev);
15719aa32835SJeff Kirsher 	DBG2(dev, "stopped TX queue" NL);
15729aa32835SJeff Kirsher 	return NETDEV_TX_BUSY;
15739aa32835SJeff Kirsher }
15749aa32835SJeff Kirsher 
15759aa32835SJeff Kirsher /* Tx lock BHs */
emac_parse_tx_error(struct emac_instance * dev,u16 ctrl)15769aa32835SJeff Kirsher static void emac_parse_tx_error(struct emac_instance *dev, u16 ctrl)
15779aa32835SJeff Kirsher {
15789aa32835SJeff Kirsher 	struct emac_error_stats *st = &dev->estats;
15799aa32835SJeff Kirsher 
15809aa32835SJeff Kirsher 	DBG(dev, "BD TX error %04x" NL, ctrl);
15819aa32835SJeff Kirsher 
15829aa32835SJeff Kirsher 	++st->tx_bd_errors;
15839aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_BFCS)
15849aa32835SJeff Kirsher 		++st->tx_bd_bad_fcs;
15859aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_LCS)
15869aa32835SJeff Kirsher 		++st->tx_bd_carrier_loss;
15879aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_ED)
15889aa32835SJeff Kirsher 		++st->tx_bd_excessive_deferral;
15899aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_EC)
15909aa32835SJeff Kirsher 		++st->tx_bd_excessive_collisions;
15919aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_LC)
15929aa32835SJeff Kirsher 		++st->tx_bd_late_collision;
15939aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_MC)
15949aa32835SJeff Kirsher 		++st->tx_bd_multple_collisions;
15959aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_SC)
15969aa32835SJeff Kirsher 		++st->tx_bd_single_collision;
15979aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_UR)
15989aa32835SJeff Kirsher 		++st->tx_bd_underrun;
15999aa32835SJeff Kirsher 	if (ctrl & EMAC_TX_ST_SQE)
16009aa32835SJeff Kirsher 		++st->tx_bd_sqe;
16019aa32835SJeff Kirsher }
16029aa32835SJeff Kirsher 
emac_poll_tx(void * param)16039aa32835SJeff Kirsher static void emac_poll_tx(void *param)
16049aa32835SJeff Kirsher {
16059aa32835SJeff Kirsher 	struct emac_instance *dev = param;
16069aa32835SJeff Kirsher 	u32 bad_mask;
16079aa32835SJeff Kirsher 
16089aa32835SJeff Kirsher 	DBG2(dev, "poll_tx, %d %d" NL, dev->tx_cnt, dev->ack_slot);
16099aa32835SJeff Kirsher 
16109aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
16119aa32835SJeff Kirsher 		bad_mask = EMAC_IS_BAD_TX_TAH;
16129aa32835SJeff Kirsher 	else
16139aa32835SJeff Kirsher 		bad_mask = EMAC_IS_BAD_TX;
16149aa32835SJeff Kirsher 
16159aa32835SJeff Kirsher 	netif_tx_lock_bh(dev->ndev);
16169aa32835SJeff Kirsher 	if (dev->tx_cnt) {
16179aa32835SJeff Kirsher 		u16 ctrl;
16189aa32835SJeff Kirsher 		int slot = dev->ack_slot, n = 0;
16199aa32835SJeff Kirsher 	again:
16209aa32835SJeff Kirsher 		ctrl = dev->tx_desc[slot].ctrl;
16219aa32835SJeff Kirsher 		if (!(ctrl & MAL_TX_CTRL_READY)) {
16229aa32835SJeff Kirsher 			struct sk_buff *skb = dev->tx_skb[slot];
16239aa32835SJeff Kirsher 			++n;
16249aa32835SJeff Kirsher 
16259aa32835SJeff Kirsher 			if (skb) {
16269aa32835SJeff Kirsher 				dev_kfree_skb(skb);
16279aa32835SJeff Kirsher 				dev->tx_skb[slot] = NULL;
16289aa32835SJeff Kirsher 			}
16299aa32835SJeff Kirsher 			slot = (slot + 1) % NUM_TX_BUFF;
16309aa32835SJeff Kirsher 
16319aa32835SJeff Kirsher 			if (unlikely(ctrl & bad_mask))
16329aa32835SJeff Kirsher 				emac_parse_tx_error(dev, ctrl);
16339aa32835SJeff Kirsher 
16349aa32835SJeff Kirsher 			if (--dev->tx_cnt)
16359aa32835SJeff Kirsher 				goto again;
16369aa32835SJeff Kirsher 		}
16379aa32835SJeff Kirsher 		if (n) {
16389aa32835SJeff Kirsher 			dev->ack_slot = slot;
16399aa32835SJeff Kirsher 			if (netif_queue_stopped(dev->ndev) &&
16409aa32835SJeff Kirsher 			    dev->tx_cnt < EMAC_TX_WAKEUP_THRESH)
16419aa32835SJeff Kirsher 				netif_wake_queue(dev->ndev);
16429aa32835SJeff Kirsher 
16439aa32835SJeff Kirsher 			DBG2(dev, "tx %d pkts" NL, n);
16449aa32835SJeff Kirsher 		}
16459aa32835SJeff Kirsher 	}
16469aa32835SJeff Kirsher 	netif_tx_unlock_bh(dev->ndev);
16479aa32835SJeff Kirsher }
16489aa32835SJeff Kirsher 
emac_recycle_rx_skb(struct emac_instance * dev,int slot,int len)16499aa32835SJeff Kirsher static inline void emac_recycle_rx_skb(struct emac_instance *dev, int slot,
16509aa32835SJeff Kirsher 				       int len)
16519aa32835SJeff Kirsher {
16529aa32835SJeff Kirsher 	struct sk_buff *skb = dev->rx_skb[slot];
16539aa32835SJeff Kirsher 
16549aa32835SJeff Kirsher 	DBG2(dev, "recycle %d %d" NL, slot, len);
16559aa32835SJeff Kirsher 
16569aa32835SJeff Kirsher 	if (len)
165722087d65SChristian Lamparter 		dma_map_single(&dev->ofdev->dev, skb->data - NET_IP_ALIGN,
165822087d65SChristian Lamparter 			       SKB_DATA_ALIGN(len + NET_IP_ALIGN),
165922087d65SChristian Lamparter 			       DMA_FROM_DEVICE);
16609aa32835SJeff Kirsher 
16619aa32835SJeff Kirsher 	dev->rx_desc[slot].data_len = 0;
16629aa32835SJeff Kirsher 	wmb();
16639aa32835SJeff Kirsher 	dev->rx_desc[slot].ctrl = MAL_RX_CTRL_EMPTY |
16649aa32835SJeff Kirsher 	    (slot == (NUM_RX_BUFF - 1) ? MAL_RX_CTRL_WRAP : 0);
16659aa32835SJeff Kirsher }
16669aa32835SJeff Kirsher 
emac_parse_rx_error(struct emac_instance * dev,u16 ctrl)16679aa32835SJeff Kirsher static void emac_parse_rx_error(struct emac_instance *dev, u16 ctrl)
16689aa32835SJeff Kirsher {
16699aa32835SJeff Kirsher 	struct emac_error_stats *st = &dev->estats;
16709aa32835SJeff Kirsher 
16719aa32835SJeff Kirsher 	DBG(dev, "BD RX error %04x" NL, ctrl);
16729aa32835SJeff Kirsher 
16739aa32835SJeff Kirsher 	++st->rx_bd_errors;
16749aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_OE)
16759aa32835SJeff Kirsher 		++st->rx_bd_overrun;
16769aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_BP)
16779aa32835SJeff Kirsher 		++st->rx_bd_bad_packet;
16789aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_RP)
16799aa32835SJeff Kirsher 		++st->rx_bd_runt_packet;
16809aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_SE)
16819aa32835SJeff Kirsher 		++st->rx_bd_short_event;
16829aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_AE)
16839aa32835SJeff Kirsher 		++st->rx_bd_alignment_error;
16849aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_BFCS)
16859aa32835SJeff Kirsher 		++st->rx_bd_bad_fcs;
16869aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_PTL)
16879aa32835SJeff Kirsher 		++st->rx_bd_packet_too_long;
16889aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_ORE)
16899aa32835SJeff Kirsher 		++st->rx_bd_out_of_range;
16909aa32835SJeff Kirsher 	if (ctrl & EMAC_RX_ST_IRE)
16919aa32835SJeff Kirsher 		++st->rx_bd_in_range;
16929aa32835SJeff Kirsher }
16939aa32835SJeff Kirsher 
emac_rx_csum(struct emac_instance * dev,struct sk_buff * skb,u16 ctrl)16949aa32835SJeff Kirsher static inline void emac_rx_csum(struct emac_instance *dev,
16959aa32835SJeff Kirsher 				struct sk_buff *skb, u16 ctrl)
16969aa32835SJeff Kirsher {
16973b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_TAH
16989aa32835SJeff Kirsher 	if (!ctrl && dev->tah_dev) {
16999aa32835SJeff Kirsher 		skb->ip_summed = CHECKSUM_UNNECESSARY;
17009aa32835SJeff Kirsher 		++dev->stats.rx_packets_csum;
17019aa32835SJeff Kirsher 	}
17029aa32835SJeff Kirsher #endif
17039aa32835SJeff Kirsher }
17049aa32835SJeff Kirsher 
emac_rx_sg_append(struct emac_instance * dev,int slot)17059aa32835SJeff Kirsher static inline int emac_rx_sg_append(struct emac_instance *dev, int slot)
17069aa32835SJeff Kirsher {
17079aa32835SJeff Kirsher 	if (likely(dev->rx_sg_skb != NULL)) {
17089aa32835SJeff Kirsher 		int len = dev->rx_desc[slot].data_len;
17099aa32835SJeff Kirsher 		int tot_len = dev->rx_sg_skb->len + len;
17109aa32835SJeff Kirsher 
171122087d65SChristian Lamparter 		if (unlikely(tot_len + NET_IP_ALIGN > dev->rx_skb_size)) {
17129aa32835SJeff Kirsher 			++dev->estats.rx_dropped_mtu;
17139aa32835SJeff Kirsher 			dev_kfree_skb(dev->rx_sg_skb);
17149aa32835SJeff Kirsher 			dev->rx_sg_skb = NULL;
17159aa32835SJeff Kirsher 		} else {
1716b05ae4eeSKyle Moffett 			memcpy(skb_tail_pointer(dev->rx_sg_skb),
17179aa32835SJeff Kirsher 					 dev->rx_skb[slot]->data, len);
17189aa32835SJeff Kirsher 			skb_put(dev->rx_sg_skb, len);
17199aa32835SJeff Kirsher 			emac_recycle_rx_skb(dev, slot, len);
17209aa32835SJeff Kirsher 			return 0;
17219aa32835SJeff Kirsher 		}
17229aa32835SJeff Kirsher 	}
17239aa32835SJeff Kirsher 	emac_recycle_rx_skb(dev, slot, 0);
17249aa32835SJeff Kirsher 	return -1;
17259aa32835SJeff Kirsher }
17269aa32835SJeff Kirsher 
17279aa32835SJeff Kirsher /* NAPI poll context */
emac_poll_rx(void * param,int budget)17289aa32835SJeff Kirsher static int emac_poll_rx(void *param, int budget)
17299aa32835SJeff Kirsher {
17309aa32835SJeff Kirsher 	struct emac_instance *dev = param;
17319aa32835SJeff Kirsher 	int slot = dev->rx_slot, received = 0;
17329aa32835SJeff Kirsher 
17339aa32835SJeff Kirsher 	DBG2(dev, "poll_rx(%d)" NL, budget);
17349aa32835SJeff Kirsher 
17359aa32835SJeff Kirsher  again:
17369aa32835SJeff Kirsher 	while (budget > 0) {
17379aa32835SJeff Kirsher 		int len;
17389aa32835SJeff Kirsher 		struct sk_buff *skb;
17399aa32835SJeff Kirsher 		u16 ctrl = dev->rx_desc[slot].ctrl;
17409aa32835SJeff Kirsher 
17419aa32835SJeff Kirsher 		if (ctrl & MAL_RX_CTRL_EMPTY)
17429aa32835SJeff Kirsher 			break;
17439aa32835SJeff Kirsher 
17449aa32835SJeff Kirsher 		skb = dev->rx_skb[slot];
17459aa32835SJeff Kirsher 		mb();
17469aa32835SJeff Kirsher 		len = dev->rx_desc[slot].data_len;
17479aa32835SJeff Kirsher 
17489aa32835SJeff Kirsher 		if (unlikely(!MAL_IS_SINGLE_RX(ctrl)))
17499aa32835SJeff Kirsher 			goto sg;
17509aa32835SJeff Kirsher 
17519aa32835SJeff Kirsher 		ctrl &= EMAC_BAD_RX_MASK;
17529aa32835SJeff Kirsher 		if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
17539aa32835SJeff Kirsher 			emac_parse_rx_error(dev, ctrl);
17549aa32835SJeff Kirsher 			++dev->estats.rx_dropped_error;
17559aa32835SJeff Kirsher 			emac_recycle_rx_skb(dev, slot, 0);
17569aa32835SJeff Kirsher 			len = 0;
17579aa32835SJeff Kirsher 			goto next;
17589aa32835SJeff Kirsher 		}
17599aa32835SJeff Kirsher 
17609aa32835SJeff Kirsher 		if (len < ETH_HLEN) {
17619aa32835SJeff Kirsher 			++dev->estats.rx_dropped_stack;
17629aa32835SJeff Kirsher 			emac_recycle_rx_skb(dev, slot, len);
17639aa32835SJeff Kirsher 			goto next;
17649aa32835SJeff Kirsher 		}
17659aa32835SJeff Kirsher 
17669aa32835SJeff Kirsher 		if (len && len < EMAC_RX_COPY_THRESH) {
176722087d65SChristian Lamparter 			struct sk_buff *copy_skb;
176822087d65SChristian Lamparter 
176922087d65SChristian Lamparter 			copy_skb = napi_alloc_skb(&dev->mal->napi, len);
17709aa32835SJeff Kirsher 			if (unlikely(!copy_skb))
17719aa32835SJeff Kirsher 				goto oom;
17729aa32835SJeff Kirsher 
177322087d65SChristian Lamparter 			memcpy(copy_skb->data - NET_IP_ALIGN,
177422087d65SChristian Lamparter 			       skb->data - NET_IP_ALIGN,
177522087d65SChristian Lamparter 			       len + NET_IP_ALIGN);
17769aa32835SJeff Kirsher 			emac_recycle_rx_skb(dev, slot, len);
17779aa32835SJeff Kirsher 			skb = copy_skb;
177822087d65SChristian Lamparter 		} else if (unlikely(emac_alloc_rx_skb_napi(dev, slot)))
17799aa32835SJeff Kirsher 			goto oom;
17809aa32835SJeff Kirsher 
17819aa32835SJeff Kirsher 		skb_put(skb, len);
17829aa32835SJeff Kirsher 	push_packet:
17839aa32835SJeff Kirsher 		skb->protocol = eth_type_trans(skb, dev->ndev);
17849aa32835SJeff Kirsher 		emac_rx_csum(dev, skb, ctrl);
17859aa32835SJeff Kirsher 
17869aa32835SJeff Kirsher 		if (unlikely(netif_receive_skb(skb) == NET_RX_DROP))
17879aa32835SJeff Kirsher 			++dev->estats.rx_dropped_stack;
17889aa32835SJeff Kirsher 	next:
17899aa32835SJeff Kirsher 		++dev->stats.rx_packets;
17909aa32835SJeff Kirsher 	skip:
17919aa32835SJeff Kirsher 		dev->stats.rx_bytes += len;
17929aa32835SJeff Kirsher 		slot = (slot + 1) % NUM_RX_BUFF;
17939aa32835SJeff Kirsher 		--budget;
17949aa32835SJeff Kirsher 		++received;
17959aa32835SJeff Kirsher 		continue;
17969aa32835SJeff Kirsher 	sg:
17979aa32835SJeff Kirsher 		if (ctrl & MAL_RX_CTRL_FIRST) {
17989aa32835SJeff Kirsher 			BUG_ON(dev->rx_sg_skb);
179922087d65SChristian Lamparter 			if (unlikely(emac_alloc_rx_skb_napi(dev, slot))) {
18009aa32835SJeff Kirsher 				DBG(dev, "rx OOM %d" NL, slot);
18019aa32835SJeff Kirsher 				++dev->estats.rx_dropped_oom;
18029aa32835SJeff Kirsher 				emac_recycle_rx_skb(dev, slot, 0);
18039aa32835SJeff Kirsher 			} else {
18049aa32835SJeff Kirsher 				dev->rx_sg_skb = skb;
18059aa32835SJeff Kirsher 				skb_put(skb, len);
18069aa32835SJeff Kirsher 			}
18079aa32835SJeff Kirsher 		} else if (!emac_rx_sg_append(dev, slot) &&
18089aa32835SJeff Kirsher 			   (ctrl & MAL_RX_CTRL_LAST)) {
18099aa32835SJeff Kirsher 
18109aa32835SJeff Kirsher 			skb = dev->rx_sg_skb;
18119aa32835SJeff Kirsher 			dev->rx_sg_skb = NULL;
18129aa32835SJeff Kirsher 
18139aa32835SJeff Kirsher 			ctrl &= EMAC_BAD_RX_MASK;
18149aa32835SJeff Kirsher 			if (unlikely(ctrl && ctrl != EMAC_RX_TAH_BAD_CSUM)) {
18159aa32835SJeff Kirsher 				emac_parse_rx_error(dev, ctrl);
18169aa32835SJeff Kirsher 				++dev->estats.rx_dropped_error;
18179aa32835SJeff Kirsher 				dev_kfree_skb(skb);
18189aa32835SJeff Kirsher 				len = 0;
18199aa32835SJeff Kirsher 			} else
18209aa32835SJeff Kirsher 				goto push_packet;
18219aa32835SJeff Kirsher 		}
18229aa32835SJeff Kirsher 		goto skip;
18239aa32835SJeff Kirsher 	oom:
18249aa32835SJeff Kirsher 		DBG(dev, "rx OOM %d" NL, slot);
18259aa32835SJeff Kirsher 		/* Drop the packet and recycle skb */
18269aa32835SJeff Kirsher 		++dev->estats.rx_dropped_oom;
18279aa32835SJeff Kirsher 		emac_recycle_rx_skb(dev, slot, 0);
18289aa32835SJeff Kirsher 		goto next;
18299aa32835SJeff Kirsher 	}
18309aa32835SJeff Kirsher 
18319aa32835SJeff Kirsher 	if (received) {
18329aa32835SJeff Kirsher 		DBG2(dev, "rx %d BDs" NL, received);
18339aa32835SJeff Kirsher 		dev->rx_slot = slot;
18349aa32835SJeff Kirsher 	}
18359aa32835SJeff Kirsher 
18369aa32835SJeff Kirsher 	if (unlikely(budget && test_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags))) {
18379aa32835SJeff Kirsher 		mb();
18389aa32835SJeff Kirsher 		if (!(dev->rx_desc[slot].ctrl & MAL_RX_CTRL_EMPTY)) {
18399aa32835SJeff Kirsher 			DBG2(dev, "rx restart" NL);
18409aa32835SJeff Kirsher 			received = 0;
18419aa32835SJeff Kirsher 			goto again;
18429aa32835SJeff Kirsher 		}
18439aa32835SJeff Kirsher 
18449aa32835SJeff Kirsher 		if (dev->rx_sg_skb) {
18459aa32835SJeff Kirsher 			DBG2(dev, "dropping partial rx packet" NL);
18469aa32835SJeff Kirsher 			++dev->estats.rx_dropped_error;
18479aa32835SJeff Kirsher 			dev_kfree_skb(dev->rx_sg_skb);
18489aa32835SJeff Kirsher 			dev->rx_sg_skb = NULL;
18499aa32835SJeff Kirsher 		}
18509aa32835SJeff Kirsher 
18519aa32835SJeff Kirsher 		clear_bit(MAL_COMMAC_RX_STOPPED, &dev->commac.flags);
18529aa32835SJeff Kirsher 		mal_enable_rx_channel(dev->mal, dev->mal_rx_chan);
18539aa32835SJeff Kirsher 		emac_rx_enable(dev);
18549aa32835SJeff Kirsher 		dev->rx_slot = 0;
18559aa32835SJeff Kirsher 	}
18569aa32835SJeff Kirsher 	return received;
18579aa32835SJeff Kirsher }
18589aa32835SJeff Kirsher 
18599aa32835SJeff Kirsher /* NAPI poll context */
emac_peek_rx(void * param)18609aa32835SJeff Kirsher static int emac_peek_rx(void *param)
18619aa32835SJeff Kirsher {
18629aa32835SJeff Kirsher 	struct emac_instance *dev = param;
18639aa32835SJeff Kirsher 
18649aa32835SJeff Kirsher 	return !(dev->rx_desc[dev->rx_slot].ctrl & MAL_RX_CTRL_EMPTY);
18659aa32835SJeff Kirsher }
18669aa32835SJeff Kirsher 
18679aa32835SJeff Kirsher /* NAPI poll context */
emac_peek_rx_sg(void * param)18689aa32835SJeff Kirsher static int emac_peek_rx_sg(void *param)
18699aa32835SJeff Kirsher {
18709aa32835SJeff Kirsher 	struct emac_instance *dev = param;
18719aa32835SJeff Kirsher 
18729aa32835SJeff Kirsher 	int slot = dev->rx_slot;
18739aa32835SJeff Kirsher 	while (1) {
18749aa32835SJeff Kirsher 		u16 ctrl = dev->rx_desc[slot].ctrl;
18759aa32835SJeff Kirsher 		if (ctrl & MAL_RX_CTRL_EMPTY)
18769aa32835SJeff Kirsher 			return 0;
18779aa32835SJeff Kirsher 		else if (ctrl & MAL_RX_CTRL_LAST)
18789aa32835SJeff Kirsher 			return 1;
18799aa32835SJeff Kirsher 
18809aa32835SJeff Kirsher 		slot = (slot + 1) % NUM_RX_BUFF;
18819aa32835SJeff Kirsher 
18829aa32835SJeff Kirsher 		/* I'm just being paranoid here :) */
18839aa32835SJeff Kirsher 		if (unlikely(slot == dev->rx_slot))
18849aa32835SJeff Kirsher 			return 0;
18859aa32835SJeff Kirsher 	}
18869aa32835SJeff Kirsher }
18879aa32835SJeff Kirsher 
18889aa32835SJeff Kirsher /* Hard IRQ */
emac_rxde(void * param)18899aa32835SJeff Kirsher static void emac_rxde(void *param)
18909aa32835SJeff Kirsher {
18919aa32835SJeff Kirsher 	struct emac_instance *dev = param;
18929aa32835SJeff Kirsher 
18939aa32835SJeff Kirsher 	++dev->estats.rx_stopped;
18949aa32835SJeff Kirsher 	emac_rx_disable_async(dev);
18959aa32835SJeff Kirsher }
18969aa32835SJeff Kirsher 
18979aa32835SJeff Kirsher /* Hard IRQ */
emac_irq(int irq,void * dev_instance)18989aa32835SJeff Kirsher static irqreturn_t emac_irq(int irq, void *dev_instance)
18999aa32835SJeff Kirsher {
19009aa32835SJeff Kirsher 	struct emac_instance *dev = dev_instance;
19019aa32835SJeff Kirsher 	struct emac_regs __iomem *p = dev->emacp;
19029aa32835SJeff Kirsher 	struct emac_error_stats *st = &dev->estats;
19039aa32835SJeff Kirsher 	u32 isr;
19049aa32835SJeff Kirsher 
19059aa32835SJeff Kirsher 	spin_lock(&dev->lock);
19069aa32835SJeff Kirsher 
19079aa32835SJeff Kirsher 	isr = in_be32(&p->isr);
19089aa32835SJeff Kirsher 	out_be32(&p->isr, isr);
19099aa32835SJeff Kirsher 
19109aa32835SJeff Kirsher 	DBG(dev, "isr = %08x" NL, isr);
19119aa32835SJeff Kirsher 
19129aa32835SJeff Kirsher 	if (isr & EMAC4_ISR_TXPE)
19139aa32835SJeff Kirsher 		++st->tx_parity;
19149aa32835SJeff Kirsher 	if (isr & EMAC4_ISR_RXPE)
19159aa32835SJeff Kirsher 		++st->rx_parity;
19169aa32835SJeff Kirsher 	if (isr & EMAC4_ISR_TXUE)
19179aa32835SJeff Kirsher 		++st->tx_underrun;
19189aa32835SJeff Kirsher 	if (isr & EMAC4_ISR_RXOE)
19199aa32835SJeff Kirsher 		++st->rx_fifo_overrun;
19209aa32835SJeff Kirsher 	if (isr & EMAC_ISR_OVR)
19219aa32835SJeff Kirsher 		++st->rx_overrun;
19229aa32835SJeff Kirsher 	if (isr & EMAC_ISR_BP)
19239aa32835SJeff Kirsher 		++st->rx_bad_packet;
19249aa32835SJeff Kirsher 	if (isr & EMAC_ISR_RP)
19259aa32835SJeff Kirsher 		++st->rx_runt_packet;
19269aa32835SJeff Kirsher 	if (isr & EMAC_ISR_SE)
19279aa32835SJeff Kirsher 		++st->rx_short_event;
19289aa32835SJeff Kirsher 	if (isr & EMAC_ISR_ALE)
19299aa32835SJeff Kirsher 		++st->rx_alignment_error;
19309aa32835SJeff Kirsher 	if (isr & EMAC_ISR_BFCS)
19319aa32835SJeff Kirsher 		++st->rx_bad_fcs;
19329aa32835SJeff Kirsher 	if (isr & EMAC_ISR_PTLE)
19339aa32835SJeff Kirsher 		++st->rx_packet_too_long;
19349aa32835SJeff Kirsher 	if (isr & EMAC_ISR_ORE)
19359aa32835SJeff Kirsher 		++st->rx_out_of_range;
19369aa32835SJeff Kirsher 	if (isr & EMAC_ISR_IRE)
19379aa32835SJeff Kirsher 		++st->rx_in_range;
19389aa32835SJeff Kirsher 	if (isr & EMAC_ISR_SQE)
19399aa32835SJeff Kirsher 		++st->tx_sqe;
19409aa32835SJeff Kirsher 	if (isr & EMAC_ISR_TE)
19419aa32835SJeff Kirsher 		++st->tx_errors;
19429aa32835SJeff Kirsher 
19439aa32835SJeff Kirsher 	spin_unlock(&dev->lock);
19449aa32835SJeff Kirsher 
19459aa32835SJeff Kirsher 	return IRQ_HANDLED;
19469aa32835SJeff Kirsher }
19479aa32835SJeff Kirsher 
emac_stats(struct net_device * ndev)19489aa32835SJeff Kirsher static struct net_device_stats *emac_stats(struct net_device *ndev)
19499aa32835SJeff Kirsher {
19509aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
19519aa32835SJeff Kirsher 	struct emac_stats *st = &dev->stats;
19529aa32835SJeff Kirsher 	struct emac_error_stats *est = &dev->estats;
1953065f4b69STobias Klauser 	struct net_device_stats *nst = &ndev->stats;
19549aa32835SJeff Kirsher 	unsigned long flags;
19559aa32835SJeff Kirsher 
19569aa32835SJeff Kirsher 	DBG2(dev, "stats" NL);
19579aa32835SJeff Kirsher 
19589aa32835SJeff Kirsher 	/* Compute "legacy" statistics */
19599aa32835SJeff Kirsher 	spin_lock_irqsave(&dev->lock, flags);
19609aa32835SJeff Kirsher 	nst->rx_packets = (unsigned long)st->rx_packets;
19619aa32835SJeff Kirsher 	nst->rx_bytes = (unsigned long)st->rx_bytes;
19629aa32835SJeff Kirsher 	nst->tx_packets = (unsigned long)st->tx_packets;
19639aa32835SJeff Kirsher 	nst->tx_bytes = (unsigned long)st->tx_bytes;
19649aa32835SJeff Kirsher 	nst->rx_dropped = (unsigned long)(est->rx_dropped_oom +
19659aa32835SJeff Kirsher 					  est->rx_dropped_error +
19669aa32835SJeff Kirsher 					  est->rx_dropped_resize +
19679aa32835SJeff Kirsher 					  est->rx_dropped_mtu);
19689aa32835SJeff Kirsher 	nst->tx_dropped = (unsigned long)est->tx_dropped;
19699aa32835SJeff Kirsher 
19709aa32835SJeff Kirsher 	nst->rx_errors = (unsigned long)est->rx_bd_errors;
19719aa32835SJeff Kirsher 	nst->rx_fifo_errors = (unsigned long)(est->rx_bd_overrun +
19729aa32835SJeff Kirsher 					      est->rx_fifo_overrun +
19739aa32835SJeff Kirsher 					      est->rx_overrun);
19749aa32835SJeff Kirsher 	nst->rx_frame_errors = (unsigned long)(est->rx_bd_alignment_error +
19759aa32835SJeff Kirsher 					       est->rx_alignment_error);
19769aa32835SJeff Kirsher 	nst->rx_crc_errors = (unsigned long)(est->rx_bd_bad_fcs +
19779aa32835SJeff Kirsher 					     est->rx_bad_fcs);
19789aa32835SJeff Kirsher 	nst->rx_length_errors = (unsigned long)(est->rx_bd_runt_packet +
19799aa32835SJeff Kirsher 						est->rx_bd_short_event +
19809aa32835SJeff Kirsher 						est->rx_bd_packet_too_long +
19819aa32835SJeff Kirsher 						est->rx_bd_out_of_range +
19829aa32835SJeff Kirsher 						est->rx_bd_in_range +
19839aa32835SJeff Kirsher 						est->rx_runt_packet +
19849aa32835SJeff Kirsher 						est->rx_short_event +
19859aa32835SJeff Kirsher 						est->rx_packet_too_long +
19869aa32835SJeff Kirsher 						est->rx_out_of_range +
19879aa32835SJeff Kirsher 						est->rx_in_range);
19889aa32835SJeff Kirsher 
19899aa32835SJeff Kirsher 	nst->tx_errors = (unsigned long)(est->tx_bd_errors + est->tx_errors);
19909aa32835SJeff Kirsher 	nst->tx_fifo_errors = (unsigned long)(est->tx_bd_underrun +
19919aa32835SJeff Kirsher 					      est->tx_underrun);
19929aa32835SJeff Kirsher 	nst->tx_carrier_errors = (unsigned long)est->tx_bd_carrier_loss;
19939aa32835SJeff Kirsher 	nst->collisions = (unsigned long)(est->tx_bd_excessive_deferral +
19949aa32835SJeff Kirsher 					  est->tx_bd_excessive_collisions +
19959aa32835SJeff Kirsher 					  est->tx_bd_late_collision +
19969aa32835SJeff Kirsher 					  est->tx_bd_multple_collisions);
19979aa32835SJeff Kirsher 	spin_unlock_irqrestore(&dev->lock, flags);
19989aa32835SJeff Kirsher 	return nst;
19999aa32835SJeff Kirsher }
20009aa32835SJeff Kirsher 
20019aa32835SJeff Kirsher static struct mal_commac_ops emac_commac_ops = {
20029aa32835SJeff Kirsher 	.poll_tx = &emac_poll_tx,
20039aa32835SJeff Kirsher 	.poll_rx = &emac_poll_rx,
20049aa32835SJeff Kirsher 	.peek_rx = &emac_peek_rx,
20059aa32835SJeff Kirsher 	.rxde = &emac_rxde,
20069aa32835SJeff Kirsher };
20079aa32835SJeff Kirsher 
20089aa32835SJeff Kirsher static struct mal_commac_ops emac_commac_sg_ops = {
20099aa32835SJeff Kirsher 	.poll_tx = &emac_poll_tx,
20109aa32835SJeff Kirsher 	.poll_rx = &emac_poll_rx,
20119aa32835SJeff Kirsher 	.peek_rx = &emac_peek_rx_sg,
20129aa32835SJeff Kirsher 	.rxde = &emac_rxde,
20139aa32835SJeff Kirsher };
20149aa32835SJeff Kirsher 
20159aa32835SJeff Kirsher /* Ethtool support */
emac_ethtool_get_link_ksettings(struct net_device * ndev,struct ethtool_link_ksettings * cmd)2016e4ccf764SPhilippe Reynes static int emac_ethtool_get_link_ksettings(struct net_device *ndev,
2017e4ccf764SPhilippe Reynes 					   struct ethtool_link_ksettings *cmd)
20189aa32835SJeff Kirsher {
20199aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
2020e4ccf764SPhilippe Reynes 	u32 supported, advertising;
20219aa32835SJeff Kirsher 
2022e4ccf764SPhilippe Reynes 	supported = dev->phy.features;
2023e4ccf764SPhilippe Reynes 	cmd->base.port = PORT_MII;
2024e4ccf764SPhilippe Reynes 	cmd->base.phy_address = dev->phy.address;
20259aa32835SJeff Kirsher 
20269aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
2027e4ccf764SPhilippe Reynes 	advertising = dev->phy.advertising;
2028e4ccf764SPhilippe Reynes 	cmd->base.autoneg = dev->phy.autoneg;
2029e4ccf764SPhilippe Reynes 	cmd->base.speed = dev->phy.speed;
2030e4ccf764SPhilippe Reynes 	cmd->base.duplex = dev->phy.duplex;
20319aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
20329aa32835SJeff Kirsher 
2033e4ccf764SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.supported,
2034e4ccf764SPhilippe Reynes 						supported);
2035e4ccf764SPhilippe Reynes 	ethtool_convert_legacy_u32_to_link_mode(cmd->link_modes.advertising,
2036e4ccf764SPhilippe Reynes 						advertising);
2037e4ccf764SPhilippe Reynes 
20389aa32835SJeff Kirsher 	return 0;
20399aa32835SJeff Kirsher }
20409aa32835SJeff Kirsher 
2041e4ccf764SPhilippe Reynes static int
emac_ethtool_set_link_ksettings(struct net_device * ndev,const struct ethtool_link_ksettings * cmd)2042e4ccf764SPhilippe Reynes emac_ethtool_set_link_ksettings(struct net_device *ndev,
2043e4ccf764SPhilippe Reynes 				const struct ethtool_link_ksettings *cmd)
20449aa32835SJeff Kirsher {
20459aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
20469aa32835SJeff Kirsher 	u32 f = dev->phy.features;
2047e4ccf764SPhilippe Reynes 	u32 advertising;
2048e4ccf764SPhilippe Reynes 
2049e4ccf764SPhilippe Reynes 	ethtool_convert_link_mode_to_legacy_u32(&advertising,
2050e4ccf764SPhilippe Reynes 						cmd->link_modes.advertising);
20519aa32835SJeff Kirsher 
20529aa32835SJeff Kirsher 	DBG(dev, "set_settings(%d, %d, %d, 0x%08x)" NL,
2053e4ccf764SPhilippe Reynes 	    cmd->base.autoneg, cmd->base.speed, cmd->base.duplex, advertising);
20549aa32835SJeff Kirsher 
20559aa32835SJeff Kirsher 	/* Basic sanity checks */
20569aa32835SJeff Kirsher 	if (dev->phy.address < 0)
20579aa32835SJeff Kirsher 		return -EOPNOTSUPP;
2058e4ccf764SPhilippe Reynes 	if (cmd->base.autoneg != AUTONEG_ENABLE &&
2059e4ccf764SPhilippe Reynes 	    cmd->base.autoneg != AUTONEG_DISABLE)
20609aa32835SJeff Kirsher 		return -EINVAL;
2061e4ccf764SPhilippe Reynes 	if (cmd->base.autoneg == AUTONEG_ENABLE && advertising == 0)
20629aa32835SJeff Kirsher 		return -EINVAL;
2063e4ccf764SPhilippe Reynes 	if (cmd->base.duplex != DUPLEX_HALF && cmd->base.duplex != DUPLEX_FULL)
20649aa32835SJeff Kirsher 		return -EINVAL;
20659aa32835SJeff Kirsher 
2066e4ccf764SPhilippe Reynes 	if (cmd->base.autoneg == AUTONEG_DISABLE) {
2067e4ccf764SPhilippe Reynes 		switch (cmd->base.speed) {
20689aa32835SJeff Kirsher 		case SPEED_10:
2069e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_HALF &&
20709aa32835SJeff Kirsher 			    !(f & SUPPORTED_10baseT_Half))
20719aa32835SJeff Kirsher 				return -EINVAL;
2072e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL &&
20739aa32835SJeff Kirsher 			    !(f & SUPPORTED_10baseT_Full))
20749aa32835SJeff Kirsher 				return -EINVAL;
20759aa32835SJeff Kirsher 			break;
20769aa32835SJeff Kirsher 		case SPEED_100:
2077e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_HALF &&
20789aa32835SJeff Kirsher 			    !(f & SUPPORTED_100baseT_Half))
20799aa32835SJeff Kirsher 				return -EINVAL;
2080e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL &&
20819aa32835SJeff Kirsher 			    !(f & SUPPORTED_100baseT_Full))
20829aa32835SJeff Kirsher 				return -EINVAL;
20839aa32835SJeff Kirsher 			break;
20849aa32835SJeff Kirsher 		case SPEED_1000:
2085e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_HALF &&
20869aa32835SJeff Kirsher 			    !(f & SUPPORTED_1000baseT_Half))
20879aa32835SJeff Kirsher 				return -EINVAL;
2088e4ccf764SPhilippe Reynes 			if (cmd->base.duplex == DUPLEX_FULL &&
20899aa32835SJeff Kirsher 			    !(f & SUPPORTED_1000baseT_Full))
20909aa32835SJeff Kirsher 				return -EINVAL;
20919aa32835SJeff Kirsher 			break;
20929aa32835SJeff Kirsher 		default:
20939aa32835SJeff Kirsher 			return -EINVAL;
20949aa32835SJeff Kirsher 		}
20959aa32835SJeff Kirsher 
20969aa32835SJeff Kirsher 		mutex_lock(&dev->link_lock);
2097e4ccf764SPhilippe Reynes 		dev->phy.def->ops->setup_forced(&dev->phy, cmd->base.speed,
2098e4ccf764SPhilippe Reynes 						cmd->base.duplex);
20999aa32835SJeff Kirsher 		mutex_unlock(&dev->link_lock);
21009aa32835SJeff Kirsher 
21019aa32835SJeff Kirsher 	} else {
21029aa32835SJeff Kirsher 		if (!(f & SUPPORTED_Autoneg))
21039aa32835SJeff Kirsher 			return -EINVAL;
21049aa32835SJeff Kirsher 
21059aa32835SJeff Kirsher 		mutex_lock(&dev->link_lock);
21069aa32835SJeff Kirsher 		dev->phy.def->ops->setup_aneg(&dev->phy,
2107e4ccf764SPhilippe Reynes 					      (advertising & f) |
21089aa32835SJeff Kirsher 					      (dev->phy.advertising &
21099aa32835SJeff Kirsher 					       (ADVERTISED_Pause |
21109aa32835SJeff Kirsher 						ADVERTISED_Asym_Pause)));
21119aa32835SJeff Kirsher 		mutex_unlock(&dev->link_lock);
21129aa32835SJeff Kirsher 	}
21139aa32835SJeff Kirsher 	emac_force_link_update(dev);
21149aa32835SJeff Kirsher 
21159aa32835SJeff Kirsher 	return 0;
21169aa32835SJeff Kirsher }
21179aa32835SJeff Kirsher 
211874624944SHao Chen static void
emac_ethtool_get_ringparam(struct net_device * ndev,struct ethtool_ringparam * rp,struct kernel_ethtool_ringparam * kernel_rp,struct netlink_ext_ack * extack)211974624944SHao Chen emac_ethtool_get_ringparam(struct net_device *ndev,
212074624944SHao Chen 			   struct ethtool_ringparam *rp,
212174624944SHao Chen 			   struct kernel_ethtool_ringparam *kernel_rp,
212274624944SHao Chen 			   struct netlink_ext_ack *extack)
21239aa32835SJeff Kirsher {
21249aa32835SJeff Kirsher 	rp->rx_max_pending = rp->rx_pending = NUM_RX_BUFF;
21259aa32835SJeff Kirsher 	rp->tx_max_pending = rp->tx_pending = NUM_TX_BUFF;
21269aa32835SJeff Kirsher }
21279aa32835SJeff Kirsher 
emac_ethtool_get_pauseparam(struct net_device * ndev,struct ethtool_pauseparam * pp)21289aa32835SJeff Kirsher static void emac_ethtool_get_pauseparam(struct net_device *ndev,
21299aa32835SJeff Kirsher 					struct ethtool_pauseparam *pp)
21309aa32835SJeff Kirsher {
21319aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
21329aa32835SJeff Kirsher 
21339aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
21349aa32835SJeff Kirsher 	if ((dev->phy.features & SUPPORTED_Autoneg) &&
21359aa32835SJeff Kirsher 	    (dev->phy.advertising & (ADVERTISED_Pause | ADVERTISED_Asym_Pause)))
21369aa32835SJeff Kirsher 		pp->autoneg = 1;
21379aa32835SJeff Kirsher 
21389aa32835SJeff Kirsher 	if (dev->phy.duplex == DUPLEX_FULL) {
21399aa32835SJeff Kirsher 		if (dev->phy.pause)
21409aa32835SJeff Kirsher 			pp->rx_pause = pp->tx_pause = 1;
21419aa32835SJeff Kirsher 		else if (dev->phy.asym_pause)
21429aa32835SJeff Kirsher 			pp->tx_pause = 1;
21439aa32835SJeff Kirsher 	}
21449aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
21459aa32835SJeff Kirsher }
21469aa32835SJeff Kirsher 
emac_get_regs_len(struct emac_instance * dev)21479aa32835SJeff Kirsher static int emac_get_regs_len(struct emac_instance *dev)
21489aa32835SJeff Kirsher {
21499aa32835SJeff Kirsher 		return sizeof(struct emac_ethtool_regs_subhdr) +
21505369c71fSIvan Mikhaylov 			sizeof(struct emac_regs);
21519aa32835SJeff Kirsher }
21529aa32835SJeff Kirsher 
emac_ethtool_get_regs_len(struct net_device * ndev)21539aa32835SJeff Kirsher static int emac_ethtool_get_regs_len(struct net_device *ndev)
21549aa32835SJeff Kirsher {
21559aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
21569aa32835SJeff Kirsher 	int size;
21579aa32835SJeff Kirsher 
21589aa32835SJeff Kirsher 	size = sizeof(struct emac_ethtool_regs_hdr) +
21599aa32835SJeff Kirsher 		emac_get_regs_len(dev) + mal_get_regs_len(dev->mal);
21609aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
21619aa32835SJeff Kirsher 		size += zmii_get_regs_len(dev->zmii_dev);
21629aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
21639aa32835SJeff Kirsher 		size += rgmii_get_regs_len(dev->rgmii_dev);
21649aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
21659aa32835SJeff Kirsher 		size += tah_get_regs_len(dev->tah_dev);
21669aa32835SJeff Kirsher 
21679aa32835SJeff Kirsher 	return size;
21689aa32835SJeff Kirsher }
21699aa32835SJeff Kirsher 
emac_dump_regs(struct emac_instance * dev,void * buf)21709aa32835SJeff Kirsher static void *emac_dump_regs(struct emac_instance *dev, void *buf)
21719aa32835SJeff Kirsher {
21729aa32835SJeff Kirsher 	struct emac_ethtool_regs_subhdr *hdr = buf;
21739aa32835SJeff Kirsher 
21749aa32835SJeff Kirsher 	hdr->index = dev->cell_index;
21755369c71fSIvan Mikhaylov 	if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
21765369c71fSIvan Mikhaylov 		hdr->version = EMAC4SYNC_ETHTOOL_REGS_VER;
21775369c71fSIvan Mikhaylov 	} else if (emac_has_feature(dev, EMAC_FTR_EMAC4)) {
21789aa32835SJeff Kirsher 		hdr->version = EMAC4_ETHTOOL_REGS_VER;
21799aa32835SJeff Kirsher 	} else {
21809aa32835SJeff Kirsher 		hdr->version = EMAC_ETHTOOL_REGS_VER;
21819aa32835SJeff Kirsher 	}
21825369c71fSIvan Mikhaylov 	memcpy_fromio(hdr + 1, dev->emacp, sizeof(struct emac_regs));
21835369c71fSIvan Mikhaylov 	return (void *)(hdr + 1) + sizeof(struct emac_regs);
21849aa32835SJeff Kirsher }
21859aa32835SJeff Kirsher 
emac_ethtool_get_regs(struct net_device * ndev,struct ethtool_regs * regs,void * buf)21869aa32835SJeff Kirsher static void emac_ethtool_get_regs(struct net_device *ndev,
21879aa32835SJeff Kirsher 				  struct ethtool_regs *regs, void *buf)
21889aa32835SJeff Kirsher {
21899aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
21909aa32835SJeff Kirsher 	struct emac_ethtool_regs_hdr *hdr = buf;
21919aa32835SJeff Kirsher 
21929aa32835SJeff Kirsher 	hdr->components = 0;
21939aa32835SJeff Kirsher 	buf = hdr + 1;
21949aa32835SJeff Kirsher 
21959aa32835SJeff Kirsher 	buf = mal_dump_regs(dev->mal, buf);
21969aa32835SJeff Kirsher 	buf = emac_dump_regs(dev, buf);
21979aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII)) {
21989aa32835SJeff Kirsher 		hdr->components |= EMAC_ETHTOOL_REGS_ZMII;
21999aa32835SJeff Kirsher 		buf = zmii_dump_regs(dev->zmii_dev, buf);
22009aa32835SJeff Kirsher 	}
22019aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
22029aa32835SJeff Kirsher 		hdr->components |= EMAC_ETHTOOL_REGS_RGMII;
22039aa32835SJeff Kirsher 		buf = rgmii_dump_regs(dev->rgmii_dev, buf);
22049aa32835SJeff Kirsher 	}
22059aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH)) {
22069aa32835SJeff Kirsher 		hdr->components |= EMAC_ETHTOOL_REGS_TAH;
22079aa32835SJeff Kirsher 		buf = tah_dump_regs(dev->tah_dev, buf);
22089aa32835SJeff Kirsher 	}
22099aa32835SJeff Kirsher }
22109aa32835SJeff Kirsher 
emac_ethtool_nway_reset(struct net_device * ndev)22119aa32835SJeff Kirsher static int emac_ethtool_nway_reset(struct net_device *ndev)
22129aa32835SJeff Kirsher {
22139aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
22149aa32835SJeff Kirsher 	int res = 0;
22159aa32835SJeff Kirsher 
22169aa32835SJeff Kirsher 	DBG(dev, "nway_reset" NL);
22179aa32835SJeff Kirsher 
22189aa32835SJeff Kirsher 	if (dev->phy.address < 0)
22199aa32835SJeff Kirsher 		return -EOPNOTSUPP;
22209aa32835SJeff Kirsher 
22219aa32835SJeff Kirsher 	mutex_lock(&dev->link_lock);
22229aa32835SJeff Kirsher 	if (!dev->phy.autoneg) {
22239aa32835SJeff Kirsher 		res = -EINVAL;
22249aa32835SJeff Kirsher 		goto out;
22259aa32835SJeff Kirsher 	}
22269aa32835SJeff Kirsher 
22279aa32835SJeff Kirsher 	dev->phy.def->ops->setup_aneg(&dev->phy, dev->phy.advertising);
22289aa32835SJeff Kirsher  out:
22299aa32835SJeff Kirsher 	mutex_unlock(&dev->link_lock);
22309aa32835SJeff Kirsher 	emac_force_link_update(dev);
22319aa32835SJeff Kirsher 	return res;
22329aa32835SJeff Kirsher }
22339aa32835SJeff Kirsher 
emac_ethtool_get_sset_count(struct net_device * ndev,int stringset)22349aa32835SJeff Kirsher static int emac_ethtool_get_sset_count(struct net_device *ndev, int stringset)
22359aa32835SJeff Kirsher {
22369aa32835SJeff Kirsher 	if (stringset == ETH_SS_STATS)
22379aa32835SJeff Kirsher 		return EMAC_ETHTOOL_STATS_COUNT;
22389aa32835SJeff Kirsher 	else
22399aa32835SJeff Kirsher 		return -EINVAL;
22409aa32835SJeff Kirsher }
22419aa32835SJeff Kirsher 
emac_ethtool_get_strings(struct net_device * ndev,u32 stringset,u8 * buf)22429aa32835SJeff Kirsher static void emac_ethtool_get_strings(struct net_device *ndev, u32 stringset,
22439aa32835SJeff Kirsher 				     u8 * buf)
22449aa32835SJeff Kirsher {
22459aa32835SJeff Kirsher 	if (stringset == ETH_SS_STATS)
22469aa32835SJeff Kirsher 		memcpy(buf, &emac_stats_keys, sizeof(emac_stats_keys));
22479aa32835SJeff Kirsher }
22489aa32835SJeff Kirsher 
emac_ethtool_get_ethtool_stats(struct net_device * ndev,struct ethtool_stats * estats,u64 * tmp_stats)22499aa32835SJeff Kirsher static void emac_ethtool_get_ethtool_stats(struct net_device *ndev,
22509aa32835SJeff Kirsher 					   struct ethtool_stats *estats,
22519aa32835SJeff Kirsher 					   u64 * tmp_stats)
22529aa32835SJeff Kirsher {
22539aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
22549aa32835SJeff Kirsher 
22559aa32835SJeff Kirsher 	memcpy(tmp_stats, &dev->stats, sizeof(dev->stats));
22569aa32835SJeff Kirsher 	tmp_stats += sizeof(dev->stats) / sizeof(u64);
22579aa32835SJeff Kirsher 	memcpy(tmp_stats, &dev->estats, sizeof(dev->estats));
22589aa32835SJeff Kirsher }
22599aa32835SJeff Kirsher 
emac_ethtool_get_drvinfo(struct net_device * ndev,struct ethtool_drvinfo * info)22609aa32835SJeff Kirsher static void emac_ethtool_get_drvinfo(struct net_device *ndev,
22619aa32835SJeff Kirsher 				     struct ethtool_drvinfo *info)
22629aa32835SJeff Kirsher {
22639aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
22649aa32835SJeff Kirsher 
2265f029c781SWolfram Sang 	strscpy(info->driver, "ibm_emac", sizeof(info->driver));
2266f029c781SWolfram Sang 	strscpy(info->version, DRV_VERSION, sizeof(info->version));
2267f7ce9103SRob Herring 	snprintf(info->bus_info, sizeof(info->bus_info), "PPC 4xx EMAC-%d %pOF",
2268f7ce9103SRob Herring 		 dev->cell_index, dev->ofdev->dev.of_node);
22699aa32835SJeff Kirsher }
22709aa32835SJeff Kirsher 
22719aa32835SJeff Kirsher static const struct ethtool_ops emac_ethtool_ops = {
22729aa32835SJeff Kirsher 	.get_drvinfo = emac_ethtool_get_drvinfo,
22739aa32835SJeff Kirsher 
22749aa32835SJeff Kirsher 	.get_regs_len = emac_ethtool_get_regs_len,
22759aa32835SJeff Kirsher 	.get_regs = emac_ethtool_get_regs,
22769aa32835SJeff Kirsher 
22779aa32835SJeff Kirsher 	.nway_reset = emac_ethtool_nway_reset,
22789aa32835SJeff Kirsher 
22799aa32835SJeff Kirsher 	.get_ringparam = emac_ethtool_get_ringparam,
22809aa32835SJeff Kirsher 	.get_pauseparam = emac_ethtool_get_pauseparam,
22819aa32835SJeff Kirsher 
22829aa32835SJeff Kirsher 	.get_strings = emac_ethtool_get_strings,
22839aa32835SJeff Kirsher 	.get_sset_count = emac_ethtool_get_sset_count,
22849aa32835SJeff Kirsher 	.get_ethtool_stats = emac_ethtool_get_ethtool_stats,
22859aa32835SJeff Kirsher 
22869aa32835SJeff Kirsher 	.get_link = ethtool_op_get_link,
2287e4ccf764SPhilippe Reynes 	.get_link_ksettings = emac_ethtool_get_link_ksettings,
2288e4ccf764SPhilippe Reynes 	.set_link_ksettings = emac_ethtool_set_link_ksettings,
22899aa32835SJeff Kirsher };
22909aa32835SJeff Kirsher 
emac_ioctl(struct net_device * ndev,struct ifreq * rq,int cmd)22919aa32835SJeff Kirsher static int emac_ioctl(struct net_device *ndev, struct ifreq *rq, int cmd)
22929aa32835SJeff Kirsher {
22939aa32835SJeff Kirsher 	struct emac_instance *dev = netdev_priv(ndev);
22949aa32835SJeff Kirsher 	struct mii_ioctl_data *data = if_mii(rq);
22959aa32835SJeff Kirsher 
22969aa32835SJeff Kirsher 	DBG(dev, "ioctl %08x" NL, cmd);
22979aa32835SJeff Kirsher 
22989aa32835SJeff Kirsher 	if (dev->phy.address < 0)
22999aa32835SJeff Kirsher 		return -EOPNOTSUPP;
23009aa32835SJeff Kirsher 
23019aa32835SJeff Kirsher 	switch (cmd) {
23029aa32835SJeff Kirsher 	case SIOCGMIIPHY:
23039aa32835SJeff Kirsher 		data->phy_id = dev->phy.address;
2304df561f66SGustavo A. R. Silva 		fallthrough;
23059aa32835SJeff Kirsher 	case SIOCGMIIREG:
23069aa32835SJeff Kirsher 		data->val_out = emac_mdio_read(ndev, dev->phy.address,
23079aa32835SJeff Kirsher 					       data->reg_num);
23089aa32835SJeff Kirsher 		return 0;
23099aa32835SJeff Kirsher 
23109aa32835SJeff Kirsher 	case SIOCSMIIREG:
23119aa32835SJeff Kirsher 		emac_mdio_write(ndev, dev->phy.address, data->reg_num,
23129aa32835SJeff Kirsher 				data->val_in);
23139aa32835SJeff Kirsher 		return 0;
23149aa32835SJeff Kirsher 	default:
23159aa32835SJeff Kirsher 		return -EOPNOTSUPP;
23169aa32835SJeff Kirsher 	}
23179aa32835SJeff Kirsher }
23189aa32835SJeff Kirsher 
23199aa32835SJeff Kirsher struct emac_depentry {
23209aa32835SJeff Kirsher 	u32			phandle;
23219aa32835SJeff Kirsher 	struct device_node	*node;
23229aa32835SJeff Kirsher 	struct platform_device	*ofdev;
23239aa32835SJeff Kirsher 	void			*drvdata;
23249aa32835SJeff Kirsher };
23259aa32835SJeff Kirsher 
23269aa32835SJeff Kirsher #define	EMAC_DEP_MAL_IDX	0
23279aa32835SJeff Kirsher #define	EMAC_DEP_ZMII_IDX	1
23289aa32835SJeff Kirsher #define	EMAC_DEP_RGMII_IDX	2
23299aa32835SJeff Kirsher #define	EMAC_DEP_TAH_IDX	3
23309aa32835SJeff Kirsher #define	EMAC_DEP_MDIO_IDX	4
23319aa32835SJeff Kirsher #define	EMAC_DEP_PREV_IDX	5
23329aa32835SJeff Kirsher #define	EMAC_DEP_COUNT		6
23339aa32835SJeff Kirsher 
emac_check_deps(struct emac_instance * dev,struct emac_depentry * deps)2334fe17dc1eSBill Pemberton static int emac_check_deps(struct emac_instance *dev,
23359aa32835SJeff Kirsher 			   struct emac_depentry *deps)
23369aa32835SJeff Kirsher {
23379aa32835SJeff Kirsher 	int i, there = 0;
23389aa32835SJeff Kirsher 	struct device_node *np;
23399aa32835SJeff Kirsher 
23409aa32835SJeff Kirsher 	for (i = 0; i < EMAC_DEP_COUNT; i++) {
23419aa32835SJeff Kirsher 		/* no dependency on that item, allright */
23429aa32835SJeff Kirsher 		if (deps[i].phandle == 0) {
23439aa32835SJeff Kirsher 			there++;
23449aa32835SJeff Kirsher 			continue;
23459aa32835SJeff Kirsher 		}
23469aa32835SJeff Kirsher 		/* special case for blist as the dependency might go away */
23479aa32835SJeff Kirsher 		if (i == EMAC_DEP_PREV_IDX) {
23489aa32835SJeff Kirsher 			np = *(dev->blist - 1);
23499aa32835SJeff Kirsher 			if (np == NULL) {
23509aa32835SJeff Kirsher 				deps[i].phandle = 0;
23519aa32835SJeff Kirsher 				there++;
23529aa32835SJeff Kirsher 				continue;
23539aa32835SJeff Kirsher 			}
23549aa32835SJeff Kirsher 			if (deps[i].node == NULL)
23559aa32835SJeff Kirsher 				deps[i].node = of_node_get(np);
23569aa32835SJeff Kirsher 		}
23579aa32835SJeff Kirsher 		if (deps[i].node == NULL)
23589aa32835SJeff Kirsher 			deps[i].node = of_find_node_by_phandle(deps[i].phandle);
23599aa32835SJeff Kirsher 		if (deps[i].node == NULL)
23609aa32835SJeff Kirsher 			continue;
23619aa32835SJeff Kirsher 		if (deps[i].ofdev == NULL)
23629aa32835SJeff Kirsher 			deps[i].ofdev = of_find_device_by_node(deps[i].node);
23639aa32835SJeff Kirsher 		if (deps[i].ofdev == NULL)
23649aa32835SJeff Kirsher 			continue;
23659aa32835SJeff Kirsher 		if (deps[i].drvdata == NULL)
2366bc353832SJingoo Han 			deps[i].drvdata = platform_get_drvdata(deps[i].ofdev);
23679aa32835SJeff Kirsher 		if (deps[i].drvdata != NULL)
23689aa32835SJeff Kirsher 			there++;
23699aa32835SJeff Kirsher 	}
2370c092d0beSRosen Penev 	if (there != EMAC_DEP_COUNT)
2371c092d0beSRosen Penev 		return -EPROBE_DEFER;
2372c092d0beSRosen Penev 	return 0;
23739aa32835SJeff Kirsher }
23749aa32835SJeff Kirsher 
emac_put_deps(struct emac_instance * dev)23759aa32835SJeff Kirsher static void emac_put_deps(struct emac_instance *dev)
23769aa32835SJeff Kirsher {
237783c4a4eeSRob Herring 	platform_device_put(dev->mal_dev);
237883c4a4eeSRob Herring 	platform_device_put(dev->zmii_dev);
237983c4a4eeSRob Herring 	platform_device_put(dev->rgmii_dev);
238083c4a4eeSRob Herring 	platform_device_put(dev->mdio_dev);
238183c4a4eeSRob Herring 	platform_device_put(dev->tah_dev);
23829aa32835SJeff Kirsher }
23839aa32835SJeff Kirsher 
emac_wait_deps(struct emac_instance * dev)2384fe17dc1eSBill Pemberton static int emac_wait_deps(struct emac_instance *dev)
23859aa32835SJeff Kirsher {
23869aa32835SJeff Kirsher 	struct emac_depentry deps[EMAC_DEP_COUNT];
23879aa32835SJeff Kirsher 	int i, err;
23889aa32835SJeff Kirsher 
23899aa32835SJeff Kirsher 	memset(&deps, 0, sizeof(deps));
23909aa32835SJeff Kirsher 
23919aa32835SJeff Kirsher 	deps[EMAC_DEP_MAL_IDX].phandle = dev->mal_ph;
23929aa32835SJeff Kirsher 	deps[EMAC_DEP_ZMII_IDX].phandle = dev->zmii_ph;
23939aa32835SJeff Kirsher 	deps[EMAC_DEP_RGMII_IDX].phandle = dev->rgmii_ph;
23949aa32835SJeff Kirsher 	if (dev->tah_ph)
23959aa32835SJeff Kirsher 		deps[EMAC_DEP_TAH_IDX].phandle = dev->tah_ph;
23969aa32835SJeff Kirsher 	if (dev->mdio_ph)
23979aa32835SJeff Kirsher 		deps[EMAC_DEP_MDIO_IDX].phandle = dev->mdio_ph;
23989aa32835SJeff Kirsher 	if (dev->blist && dev->blist > emac_boot_list)
23999aa32835SJeff Kirsher 		deps[EMAC_DEP_PREV_IDX].phandle = 0xffffffffu;
2400c092d0beSRosen Penev 	err = emac_check_deps(dev, deps);
24019aa32835SJeff Kirsher 	for (i = 0; i < EMAC_DEP_COUNT; i++) {
24029aa32835SJeff Kirsher 		of_node_put(deps[i].node);
2403f339664cSMarkus Elfring 		if (err)
240483c4a4eeSRob Herring 			platform_device_put(deps[i].ofdev);
24059aa32835SJeff Kirsher 	}
2406c092d0beSRosen Penev 	if (!err) {
24079aa32835SJeff Kirsher 		dev->mal_dev = deps[EMAC_DEP_MAL_IDX].ofdev;
24089aa32835SJeff Kirsher 		dev->zmii_dev = deps[EMAC_DEP_ZMII_IDX].ofdev;
24099aa32835SJeff Kirsher 		dev->rgmii_dev = deps[EMAC_DEP_RGMII_IDX].ofdev;
24109aa32835SJeff Kirsher 		dev->tah_dev = deps[EMAC_DEP_TAH_IDX].ofdev;
24119aa32835SJeff Kirsher 		dev->mdio_dev = deps[EMAC_DEP_MDIO_IDX].ofdev;
24129aa32835SJeff Kirsher 	}
241383c4a4eeSRob Herring 	platform_device_put(deps[EMAC_DEP_PREV_IDX].ofdev);
24149aa32835SJeff Kirsher 	return err;
24159aa32835SJeff Kirsher }
24169aa32835SJeff Kirsher 
emac_read_uint_prop(struct device_node * np,const char * name,u32 * val,int fatal)2417fe17dc1eSBill Pemberton static int emac_read_uint_prop(struct device_node *np, const char *name,
24189aa32835SJeff Kirsher 			       u32 *val, int fatal)
24199aa32835SJeff Kirsher {
2420cc0c92ffSRosen Penev 	int err;
2421cc0c92ffSRosen Penev 
2422cc0c92ffSRosen Penev 	err = of_property_read_u32(np, name, val);
2423cc0c92ffSRosen Penev 	if (err) {
24249aa32835SJeff Kirsher 		if (fatal)
2425cc0c92ffSRosen Penev 			pr_err("%pOF: missing %s property", np, name);
2426cc0c92ffSRosen Penev 		return err;
24279aa32835SJeff Kirsher 	}
24289aa32835SJeff Kirsher 	return 0;
24299aa32835SJeff Kirsher }
24309aa32835SJeff Kirsher 
emac_adjust_link(struct net_device * ndev)2431a577ca6bSChristian Lamparter static void emac_adjust_link(struct net_device *ndev)
2432a577ca6bSChristian Lamparter {
2433a577ca6bSChristian Lamparter 	struct emac_instance *dev = netdev_priv(ndev);
2434baab9de3SRosen Penev 	struct phy_device *phy = ndev->phydev;
2435a577ca6bSChristian Lamparter 
2436a577ca6bSChristian Lamparter 	dev->phy.autoneg = phy->autoneg;
2437a577ca6bSChristian Lamparter 	dev->phy.speed = phy->speed;
2438a577ca6bSChristian Lamparter 	dev->phy.duplex = phy->duplex;
2439a577ca6bSChristian Lamparter 	dev->phy.pause = phy->pause;
2440a577ca6bSChristian Lamparter 	dev->phy.asym_pause = phy->asym_pause;
24413c1bcc86SAndrew Lunn 	ethtool_convert_link_mode_to_legacy_u32(&dev->phy.advertising,
24423c1bcc86SAndrew Lunn 						phy->advertising);
2443a577ca6bSChristian Lamparter }
2444a577ca6bSChristian Lamparter 
emac_mii_bus_read(struct mii_bus * bus,int addr,int regnum)2445a577ca6bSChristian Lamparter static int emac_mii_bus_read(struct mii_bus *bus, int addr, int regnum)
2446a577ca6bSChristian Lamparter {
2447a577ca6bSChristian Lamparter 	int ret = emac_mdio_read(bus->priv, addr, regnum);
2448a577ca6bSChristian Lamparter 	/* This is a workaround for powered down ports/phys.
2449a577ca6bSChristian Lamparter 	 * In the wild, this was seen on the Cisco Meraki MX60(W).
2450a577ca6bSChristian Lamparter 	 * This hardware disables ports as part of the handoff
2451a577ca6bSChristian Lamparter 	 * procedure. Accessing the ports will lead to errors
2452a577ca6bSChristian Lamparter 	 * (-ETIMEDOUT, -EREMOTEIO) that do more harm than good.
2453a577ca6bSChristian Lamparter 	 */
2454a577ca6bSChristian Lamparter 	return ret < 0 ? 0xffff : ret;
2455a577ca6bSChristian Lamparter }
2456a577ca6bSChristian Lamparter 
emac_mii_bus_write(struct mii_bus * bus,int addr,int regnum,u16 val)2457a577ca6bSChristian Lamparter static int emac_mii_bus_write(struct mii_bus *bus, int addr,
2458a577ca6bSChristian Lamparter 			      int regnum, u16 val)
2459a577ca6bSChristian Lamparter {
2460a577ca6bSChristian Lamparter 	emac_mdio_write(bus->priv, addr, regnum, val);
2461a577ca6bSChristian Lamparter 	return 0;
2462a577ca6bSChristian Lamparter }
2463a577ca6bSChristian Lamparter 
emac_mii_bus_reset(struct mii_bus * bus)2464a577ca6bSChristian Lamparter static int emac_mii_bus_reset(struct mii_bus *bus)
2465a577ca6bSChristian Lamparter {
2466a577ca6bSChristian Lamparter 	struct emac_instance *dev = netdev_priv(bus->priv);
2467a577ca6bSChristian Lamparter 
2468a577ca6bSChristian Lamparter 	return emac_reset(dev);
2469a577ca6bSChristian Lamparter }
2470a577ca6bSChristian Lamparter 
emac_mdio_phy_start_aneg(struct mii_phy * phy,struct phy_device * phy_dev)24719065bc38SChristian Lamparter static int emac_mdio_phy_start_aneg(struct mii_phy *phy,
24729065bc38SChristian Lamparter 				    struct phy_device *phy_dev)
24739065bc38SChristian Lamparter {
24749065bc38SChristian Lamparter 	phy_dev->autoneg = phy->autoneg;
24759065bc38SChristian Lamparter 	phy_dev->speed = phy->speed;
24769065bc38SChristian Lamparter 	phy_dev->duplex = phy->duplex;
24773c1bcc86SAndrew Lunn 	ethtool_convert_legacy_u32_to_link_mode(phy_dev->advertising,
24783c1bcc86SAndrew Lunn 						phy->advertising);
24799065bc38SChristian Lamparter 	return phy_start_aneg(phy_dev);
24809065bc38SChristian Lamparter }
24819065bc38SChristian Lamparter 
emac_mdio_setup_aneg(struct mii_phy * phy,u32 advertise)2482a577ca6bSChristian Lamparter static int emac_mdio_setup_aneg(struct mii_phy *phy, u32 advertise)
2483a577ca6bSChristian Lamparter {
2484a577ca6bSChristian Lamparter 	struct net_device *ndev = phy->dev;
2485a577ca6bSChristian Lamparter 
2486a577ca6bSChristian Lamparter 	phy->autoneg = AUTONEG_ENABLE;
2487a577ca6bSChristian Lamparter 	phy->advertising = advertise;
2488baab9de3SRosen Penev 	return emac_mdio_phy_start_aneg(phy, ndev->phydev);
2489a577ca6bSChristian Lamparter }
2490a577ca6bSChristian Lamparter 
emac_mdio_setup_forced(struct mii_phy * phy,int speed,int fd)2491a577ca6bSChristian Lamparter static int emac_mdio_setup_forced(struct mii_phy *phy, int speed, int fd)
2492a577ca6bSChristian Lamparter {
2493a577ca6bSChristian Lamparter 	struct net_device *ndev = phy->dev;
2494a577ca6bSChristian Lamparter 
2495a577ca6bSChristian Lamparter 	phy->autoneg = AUTONEG_DISABLE;
2496a577ca6bSChristian Lamparter 	phy->speed = speed;
2497a577ca6bSChristian Lamparter 	phy->duplex = fd;
2498baab9de3SRosen Penev 	return emac_mdio_phy_start_aneg(phy, ndev->phydev);
2499a577ca6bSChristian Lamparter }
2500a577ca6bSChristian Lamparter 
emac_mdio_poll_link(struct mii_phy * phy)2501a577ca6bSChristian Lamparter static int emac_mdio_poll_link(struct mii_phy *phy)
2502a577ca6bSChristian Lamparter {
2503a577ca6bSChristian Lamparter 	struct net_device *ndev = phy->dev;
2504a577ca6bSChristian Lamparter 	struct emac_instance *dev = netdev_priv(ndev);
2505a577ca6bSChristian Lamparter 	int res;
2506a577ca6bSChristian Lamparter 
2507baab9de3SRosen Penev 	res = phy_read_status(ndev->phydev);
2508a577ca6bSChristian Lamparter 	if (res) {
2509a577ca6bSChristian Lamparter 		dev_err(&dev->ofdev->dev, "link update failed (%d).", res);
2510a577ca6bSChristian Lamparter 		return ethtool_op_get_link(ndev);
2511a577ca6bSChristian Lamparter 	}
2512a577ca6bSChristian Lamparter 
2513baab9de3SRosen Penev 	return ndev->phydev->link;
2514a577ca6bSChristian Lamparter }
2515a577ca6bSChristian Lamparter 
emac_mdio_read_link(struct mii_phy * phy)2516a577ca6bSChristian Lamparter static int emac_mdio_read_link(struct mii_phy *phy)
2517a577ca6bSChristian Lamparter {
2518a577ca6bSChristian Lamparter 	struct net_device *ndev = phy->dev;
2519baab9de3SRosen Penev 	struct phy_device *phy_dev = ndev->phydev;
2520a577ca6bSChristian Lamparter 	int res;
2521a577ca6bSChristian Lamparter 
25229065bc38SChristian Lamparter 	res = phy_read_status(phy_dev);
2523a577ca6bSChristian Lamparter 	if (res)
2524a577ca6bSChristian Lamparter 		return res;
2525a577ca6bSChristian Lamparter 
25269065bc38SChristian Lamparter 	phy->speed = phy_dev->speed;
25279065bc38SChristian Lamparter 	phy->duplex = phy_dev->duplex;
25289065bc38SChristian Lamparter 	phy->pause = phy_dev->pause;
25299065bc38SChristian Lamparter 	phy->asym_pause = phy_dev->asym_pause;
2530a577ca6bSChristian Lamparter 	return 0;
2531a577ca6bSChristian Lamparter }
2532a577ca6bSChristian Lamparter 
emac_mdio_init_phy(struct mii_phy * phy)2533a577ca6bSChristian Lamparter static int emac_mdio_init_phy(struct mii_phy *phy)
2534a577ca6bSChristian Lamparter {
2535a577ca6bSChristian Lamparter 	struct net_device *ndev = phy->dev;
2536a577ca6bSChristian Lamparter 
2537baab9de3SRosen Penev 	phy_start(ndev->phydev);
2538baab9de3SRosen Penev 	return phy_init_hw(ndev->phydev);
2539a577ca6bSChristian Lamparter }
2540a577ca6bSChristian Lamparter 
2541a577ca6bSChristian Lamparter static const struct mii_phy_ops emac_dt_mdio_phy_ops = {
2542a577ca6bSChristian Lamparter 	.init		= emac_mdio_init_phy,
2543a577ca6bSChristian Lamparter 	.setup_aneg	= emac_mdio_setup_aneg,
2544a577ca6bSChristian Lamparter 	.setup_forced	= emac_mdio_setup_forced,
2545a577ca6bSChristian Lamparter 	.poll_link	= emac_mdio_poll_link,
2546a577ca6bSChristian Lamparter 	.read_link	= emac_mdio_read_link,
2547a577ca6bSChristian Lamparter };
2548a577ca6bSChristian Lamparter 
emac_dt_mdio_probe(struct emac_instance * dev)2549a577ca6bSChristian Lamparter static int emac_dt_mdio_probe(struct emac_instance *dev)
2550a577ca6bSChristian Lamparter {
2551a577ca6bSChristian Lamparter 	struct device_node *mii_np;
255293a6d4e0SRosen Penev 	struct mii_bus *bus;
2553a577ca6bSChristian Lamparter 	int res;
2554a577ca6bSChristian Lamparter 
2555a577ca6bSChristian Lamparter 	mii_np = of_get_child_by_name(dev->ofdev->dev.of_node, "mdio");
2556a577ca6bSChristian Lamparter 	if (!mii_np) {
2557a577ca6bSChristian Lamparter 		dev_err(&dev->ofdev->dev, "no mdio definition found.");
2558a577ca6bSChristian Lamparter 		return -ENODEV;
2559a577ca6bSChristian Lamparter 	}
2560a577ca6bSChristian Lamparter 
2561a577ca6bSChristian Lamparter 	if (!of_device_is_available(mii_np)) {
2562a577ca6bSChristian Lamparter 		res = -ENODEV;
2563a577ca6bSChristian Lamparter 		goto put_node;
2564a577ca6bSChristian Lamparter 	}
2565a577ca6bSChristian Lamparter 
256693a6d4e0SRosen Penev 	bus = devm_mdiobus_alloc(&dev->ofdev->dev);
256793a6d4e0SRosen Penev 	if (!bus) {
2568a577ca6bSChristian Lamparter 		res = -ENOMEM;
2569a577ca6bSChristian Lamparter 		goto put_node;
2570a577ca6bSChristian Lamparter 	}
2571a577ca6bSChristian Lamparter 
257293a6d4e0SRosen Penev 	bus->priv = dev->ndev;
257393a6d4e0SRosen Penev 	bus->parent = dev->ndev->dev.parent;
257493a6d4e0SRosen Penev 	bus->name = "emac_mdio";
257593a6d4e0SRosen Penev 	bus->read = &emac_mii_bus_read;
257693a6d4e0SRosen Penev 	bus->write = &emac_mii_bus_write;
257793a6d4e0SRosen Penev 	bus->reset = &emac_mii_bus_reset;
257893a6d4e0SRosen Penev 	snprintf(bus->id, MII_BUS_ID_SIZE, "%s", dev->ofdev->name);
257993a6d4e0SRosen Penev 	res = devm_of_mdiobus_register(&dev->ofdev->dev, bus, mii_np);
2580a577ca6bSChristian Lamparter 	if (res) {
2581a577ca6bSChristian Lamparter 		dev_err(&dev->ofdev->dev, "cannot register MDIO bus %s (%d)",
258293a6d4e0SRosen Penev 			bus->name, res);
2583a577ca6bSChristian Lamparter 	}
2584a577ca6bSChristian Lamparter 
2585a577ca6bSChristian Lamparter  put_node:
2586a577ca6bSChristian Lamparter 	of_node_put(mii_np);
2587a577ca6bSChristian Lamparter 	return res;
2588a577ca6bSChristian Lamparter }
2589a577ca6bSChristian Lamparter 
emac_dt_phy_connect(struct emac_instance * dev,struct device_node * phy_handle)2590a577ca6bSChristian Lamparter static int emac_dt_phy_connect(struct emac_instance *dev,
2591a577ca6bSChristian Lamparter 			       struct device_node *phy_handle)
2592a577ca6bSChristian Lamparter {
2593baab9de3SRosen Penev 	struct phy_device *phy_dev;
2594baab9de3SRosen Penev 
2595a577ca6bSChristian Lamparter 	dev->phy.def = devm_kzalloc(&dev->ofdev->dev, sizeof(*dev->phy.def),
2596a577ca6bSChristian Lamparter 				    GFP_KERNEL);
2597a577ca6bSChristian Lamparter 	if (!dev->phy.def)
2598a577ca6bSChristian Lamparter 		return -ENOMEM;
2599a577ca6bSChristian Lamparter 
2600baab9de3SRosen Penev 	phy_dev = of_phy_connect(dev->ndev, phy_handle, &emac_adjust_link, 0,
2601baab9de3SRosen Penev 				 dev->phy_mode);
2602baab9de3SRosen Penev 	if (!phy_dev) {
2603a577ca6bSChristian Lamparter 		dev_err(&dev->ofdev->dev, "failed to connect to PHY.\n");
2604a577ca6bSChristian Lamparter 		return -ENODEV;
2605a577ca6bSChristian Lamparter 	}
2606a577ca6bSChristian Lamparter 
2607baab9de3SRosen Penev 	dev->phy.def->phy_id = phy_dev->drv->phy_id;
2608baab9de3SRosen Penev 	dev->phy.def->phy_id_mask = phy_dev->drv->phy_id_mask;
2609baab9de3SRosen Penev 	dev->phy.def->name = phy_dev->drv->name;
2610a577ca6bSChristian Lamparter 	dev->phy.def->ops = &emac_dt_mdio_phy_ops;
26113c1bcc86SAndrew Lunn 	ethtool_convert_link_mode_to_legacy_u32(&dev->phy.features,
2612baab9de3SRosen Penev 						phy_dev->supported);
2613baab9de3SRosen Penev 	dev->phy.address = phy_dev->mdio.addr;
2614baab9de3SRosen Penev 	dev->phy.mode = phy_dev->interface;
2615a577ca6bSChristian Lamparter 	return 0;
2616a577ca6bSChristian Lamparter }
2617a577ca6bSChristian Lamparter 
emac_dt_phy_probe(struct emac_instance * dev)2618a577ca6bSChristian Lamparter static int emac_dt_phy_probe(struct emac_instance *dev)
2619a577ca6bSChristian Lamparter {
2620a577ca6bSChristian Lamparter 	struct device_node *np = dev->ofdev->dev.of_node;
2621a577ca6bSChristian Lamparter 	struct device_node *phy_handle;
2622b793f081SChristian Lamparter 	int res = 1;
2623a577ca6bSChristian Lamparter 
2624a577ca6bSChristian Lamparter 	phy_handle = of_parse_phandle(np, "phy-handle", 0);
2625a577ca6bSChristian Lamparter 
2626a577ca6bSChristian Lamparter 	if (phy_handle) {
2627a577ca6bSChristian Lamparter 		res = emac_dt_mdio_probe(dev);
2628a577ca6bSChristian Lamparter 		if (!res) {
2629a577ca6bSChristian Lamparter 			res = emac_dt_phy_connect(dev, phy_handle);
2630a577ca6bSChristian Lamparter 		}
2631a577ca6bSChristian Lamparter 	}
2632a577ca6bSChristian Lamparter 
2633a577ca6bSChristian Lamparter 	of_node_put(phy_handle);
2634a577ca6bSChristian Lamparter 	return res;
2635a577ca6bSChristian Lamparter }
2636a577ca6bSChristian Lamparter 
emac_init_phy(struct emac_instance * dev)2637fe17dc1eSBill Pemberton static int emac_init_phy(struct emac_instance *dev)
26389aa32835SJeff Kirsher {
26399aa32835SJeff Kirsher 	struct device_node *np = dev->ofdev->dev.of_node;
26409aa32835SJeff Kirsher 	struct net_device *ndev = dev->ndev;
26419aa32835SJeff Kirsher 	u32 phy_map, adv;
26429aa32835SJeff Kirsher 	int i;
26439aa32835SJeff Kirsher 
26449aa32835SJeff Kirsher 	dev->phy.dev = ndev;
26459aa32835SJeff Kirsher 	dev->phy.mode = dev->phy_mode;
26469aa32835SJeff Kirsher 
2647a577ca6bSChristian Lamparter 	/* PHY-less configuration. */
2648a577ca6bSChristian Lamparter 	if ((dev->phy_address == 0xffffffff && dev->phy_map == 0xffffffff) ||
2649a577ca6bSChristian Lamparter 	    of_phy_is_fixed_link(np)) {
26509aa32835SJeff Kirsher 		emac_reset(dev);
26519aa32835SJeff Kirsher 
2652a577ca6bSChristian Lamparter 		/* PHY-less configuration. */
26539aa32835SJeff Kirsher 		dev->phy.address = -1;
26549aa32835SJeff Kirsher 		dev->phy.features = SUPPORTED_MII;
26559aa32835SJeff Kirsher 		if (emac_phy_supports_gige(dev->phy_mode))
26569aa32835SJeff Kirsher 			dev->phy.features |= SUPPORTED_1000baseT_Full;
26579aa32835SJeff Kirsher 		else
26589aa32835SJeff Kirsher 			dev->phy.features |= SUPPORTED_100baseT_Full;
26599aa32835SJeff Kirsher 		dev->phy.pause = 1;
26609aa32835SJeff Kirsher 
2661a577ca6bSChristian Lamparter 		if (of_phy_is_fixed_link(np)) {
2662a577ca6bSChristian Lamparter 			int res = emac_dt_mdio_probe(dev);
2663a577ca6bSChristian Lamparter 
2664a577ca6bSChristian Lamparter 			if (res)
2665a577ca6bSChristian Lamparter 				return res;
266608e39982SChristian Lamparter 
266708e39982SChristian Lamparter 			res = of_phy_register_fixed_link(np);
2668baab9de3SRosen Penev 			ndev->phydev = of_phy_find_device(np);
2669baab9de3SRosen Penev 			if (res || !ndev->phydev)
267008e39982SChristian Lamparter 				return res ? res : -EINVAL;
267108e39982SChristian Lamparter 			emac_adjust_link(dev->ndev);
2672baab9de3SRosen Penev 			put_device(&ndev->phydev->mdio.dev);
2673a577ca6bSChristian Lamparter 		}
26749aa32835SJeff Kirsher 		return 0;
26759aa32835SJeff Kirsher 	}
26769aa32835SJeff Kirsher 
26779aa32835SJeff Kirsher 	mutex_lock(&emac_phy_map_lock);
26789aa32835SJeff Kirsher 	phy_map = dev->phy_map | busy_phy_map;
26799aa32835SJeff Kirsher 
26809aa32835SJeff Kirsher 	DBG(dev, "PHY maps %08x %08x" NL, dev->phy_map, busy_phy_map);
26819aa32835SJeff Kirsher 
26829aa32835SJeff Kirsher 	dev->phy.mdio_read = emac_mdio_read;
26839aa32835SJeff Kirsher 	dev->phy.mdio_write = emac_mdio_write;
26849aa32835SJeff Kirsher 
26859aa32835SJeff Kirsher 	/* Enable internal clock source */
26869aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
26879aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
26889aa32835SJeff Kirsher 		dcri_clrset(SDR0, SDR0_MFR, 0, SDR0_MFR_ECS);
26899aa32835SJeff Kirsher #endif
26909aa32835SJeff Kirsher 	/* PHY clock workaround */
26919aa32835SJeff Kirsher 	emac_rx_clk_tx(dev);
26929aa32835SJeff Kirsher 
26939aa32835SJeff Kirsher 	/* Enable internal clock source on 440GX*/
26949aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
26959aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
26969aa32835SJeff Kirsher 		dcri_clrset(SDR0, SDR0_MFR, 0, SDR0_MFR_ECS);
26979aa32835SJeff Kirsher #endif
26989aa32835SJeff Kirsher 	/* Configure EMAC with defaults so we can at least use MDIO
26999aa32835SJeff Kirsher 	 * This is needed mostly for 440GX
27009aa32835SJeff Kirsher 	 */
27019aa32835SJeff Kirsher 	if (emac_phy_gpcs(dev->phy.mode)) {
27029aa32835SJeff Kirsher 		/* XXX
27039aa32835SJeff Kirsher 		 * Make GPCS PHY address equal to EMAC index.
27049aa32835SJeff Kirsher 		 * We probably should take into account busy_phy_map
27059aa32835SJeff Kirsher 		 * and/or phy_map here.
27069aa32835SJeff Kirsher 		 *
27079aa32835SJeff Kirsher 		 * Note that the busy_phy_map is currently global
27089aa32835SJeff Kirsher 		 * while it should probably be per-ASIC...
27099aa32835SJeff Kirsher 		 */
27109aa32835SJeff Kirsher 		dev->phy.gpcs_address = dev->gpcs_address;
27119aa32835SJeff Kirsher 		if (dev->phy.gpcs_address == 0xffffffff)
27129aa32835SJeff Kirsher 			dev->phy.address = dev->cell_index;
27139aa32835SJeff Kirsher 	}
27149aa32835SJeff Kirsher 
27159aa32835SJeff Kirsher 	emac_configure(dev);
27169aa32835SJeff Kirsher 
2717a577ca6bSChristian Lamparter 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII)) {
2718a577ca6bSChristian Lamparter 		int res = emac_dt_phy_probe(dev);
2719a577ca6bSChristian Lamparter 
2720b793f081SChristian Lamparter 		switch (res) {
2721b793f081SChristian Lamparter 		case 1:
2722b793f081SChristian Lamparter 			/* No phy-handle property configured.
2723b793f081SChristian Lamparter 			 * Continue with the existing phy probe
2724b793f081SChristian Lamparter 			 * and setup code.
2725b793f081SChristian Lamparter 			 */
2726b793f081SChristian Lamparter 			break;
2727b793f081SChristian Lamparter 
2728b793f081SChristian Lamparter 		case 0:
2729a577ca6bSChristian Lamparter 			mutex_unlock(&emac_phy_map_lock);
2730a577ca6bSChristian Lamparter 			goto init_phy;
2731a577ca6bSChristian Lamparter 
2732b793f081SChristian Lamparter 		default:
2733b793f081SChristian Lamparter 			mutex_unlock(&emac_phy_map_lock);
2734a577ca6bSChristian Lamparter 			dev_err(&dev->ofdev->dev, "failed to attach dt phy (%d).\n",
2735a577ca6bSChristian Lamparter 				res);
2736a577ca6bSChristian Lamparter 			return res;
2737a577ca6bSChristian Lamparter 		}
2738b793f081SChristian Lamparter 	}
2739a577ca6bSChristian Lamparter 
27409aa32835SJeff Kirsher 	if (dev->phy_address != 0xffffffff)
27419aa32835SJeff Kirsher 		phy_map = ~(1 << dev->phy_address);
27429aa32835SJeff Kirsher 
27439aa32835SJeff Kirsher 	for (i = 0; i < 0x20; phy_map >>= 1, ++i)
27449aa32835SJeff Kirsher 		if (!(phy_map & 1)) {
27459aa32835SJeff Kirsher 			int r;
27469aa32835SJeff Kirsher 			busy_phy_map |= 1 << i;
27479aa32835SJeff Kirsher 
27489aa32835SJeff Kirsher 			/* Quick check if there is a PHY at the address */
27499aa32835SJeff Kirsher 			r = emac_mdio_read(dev->ndev, i, MII_BMCR);
27509aa32835SJeff Kirsher 			if (r == 0xffff || r < 0)
27519aa32835SJeff Kirsher 				continue;
27529aa32835SJeff Kirsher 			if (!emac_mii_phy_probe(&dev->phy, i))
27539aa32835SJeff Kirsher 				break;
27549aa32835SJeff Kirsher 		}
27559aa32835SJeff Kirsher 
27569aa32835SJeff Kirsher 	/* Enable external clock source */
27579aa32835SJeff Kirsher #ifdef CONFIG_PPC_DCR_NATIVE
27589aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_440GX_PHY_CLK_FIX))
27599aa32835SJeff Kirsher 		dcri_clrset(SDR0, SDR0_MFR, SDR0_MFR_ECS, 0);
27609aa32835SJeff Kirsher #endif
27619aa32835SJeff Kirsher 	mutex_unlock(&emac_phy_map_lock);
27629aa32835SJeff Kirsher 	if (i == 0x20) {
2763f7ce9103SRob Herring 		printk(KERN_WARNING "%pOF: can't find PHY!\n", np);
27649aa32835SJeff Kirsher 		return -ENXIO;
27659aa32835SJeff Kirsher 	}
27669aa32835SJeff Kirsher 
2767a577ca6bSChristian Lamparter  init_phy:
27689aa32835SJeff Kirsher 	/* Init PHY */
27699aa32835SJeff Kirsher 	if (dev->phy.def->ops->init)
27709aa32835SJeff Kirsher 		dev->phy.def->ops->init(&dev->phy);
27719aa32835SJeff Kirsher 
27729aa32835SJeff Kirsher 	/* Disable any PHY features not supported by the platform */
27739aa32835SJeff Kirsher 	dev->phy.def->features &= ~dev->phy_feat_exc;
2774ae5d3372SDuc Dang 	dev->phy.features &= ~dev->phy_feat_exc;
27759aa32835SJeff Kirsher 
27769aa32835SJeff Kirsher 	/* Setup initial link parameters */
27779aa32835SJeff Kirsher 	if (dev->phy.features & SUPPORTED_Autoneg) {
27789aa32835SJeff Kirsher 		adv = dev->phy.features;
27799aa32835SJeff Kirsher 		if (!emac_has_feature(dev, EMAC_FTR_NO_FLOW_CONTROL_40x))
27809aa32835SJeff Kirsher 			adv |= ADVERTISED_Pause | ADVERTISED_Asym_Pause;
27819aa32835SJeff Kirsher 		/* Restart autonegotiation */
27829aa32835SJeff Kirsher 		dev->phy.def->ops->setup_aneg(&dev->phy, adv);
27839aa32835SJeff Kirsher 	} else {
27849aa32835SJeff Kirsher 		u32 f = dev->phy.def->features;
27859aa32835SJeff Kirsher 		int speed = SPEED_10, fd = DUPLEX_HALF;
27869aa32835SJeff Kirsher 
27879aa32835SJeff Kirsher 		/* Select highest supported speed/duplex */
27889aa32835SJeff Kirsher 		if (f & SUPPORTED_1000baseT_Full) {
27899aa32835SJeff Kirsher 			speed = SPEED_1000;
27909aa32835SJeff Kirsher 			fd = DUPLEX_FULL;
27919aa32835SJeff Kirsher 		} else if (f & SUPPORTED_1000baseT_Half)
27929aa32835SJeff Kirsher 			speed = SPEED_1000;
27939aa32835SJeff Kirsher 		else if (f & SUPPORTED_100baseT_Full) {
27949aa32835SJeff Kirsher 			speed = SPEED_100;
27959aa32835SJeff Kirsher 			fd = DUPLEX_FULL;
27969aa32835SJeff Kirsher 		} else if (f & SUPPORTED_100baseT_Half)
27979aa32835SJeff Kirsher 			speed = SPEED_100;
27989aa32835SJeff Kirsher 		else if (f & SUPPORTED_10baseT_Full)
27999aa32835SJeff Kirsher 			fd = DUPLEX_FULL;
28009aa32835SJeff Kirsher 
28019aa32835SJeff Kirsher 		/* Force link parameters */
28029aa32835SJeff Kirsher 		dev->phy.def->ops->setup_forced(&dev->phy, speed, fd);
28039aa32835SJeff Kirsher 	}
28049aa32835SJeff Kirsher 	return 0;
28059aa32835SJeff Kirsher }
28069aa32835SJeff Kirsher 
emac_init_config(struct emac_instance * dev)2807fe17dc1eSBill Pemberton static int emac_init_config(struct emac_instance *dev)
28089aa32835SJeff Kirsher {
28099aa32835SJeff Kirsher 	struct device_node *np = dev->ofdev->dev.of_node;
28100c65b2b9SAndrew Lunn 	int err;
28119aa32835SJeff Kirsher 
28129aa32835SJeff Kirsher 	/* Read config from device-tree */
28139aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "mal-device", &dev->mal_ph, 1))
28149aa32835SJeff Kirsher 		return -ENXIO;
28159aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "mal-tx-channel", &dev->mal_tx_chan, 1))
28169aa32835SJeff Kirsher 		return -ENXIO;
28179aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "mal-rx-channel", &dev->mal_rx_chan, 1))
28189aa32835SJeff Kirsher 		return -ENXIO;
28199aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "cell-index", &dev->cell_index, 1))
28209aa32835SJeff Kirsher 		return -ENXIO;
28219aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "max-frame-size", &dev->max_mtu, 0))
28223d5d96acSJarod Wilson 		dev->max_mtu = ETH_DATA_LEN;
28239aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "rx-fifo-size", &dev->rx_fifo_size, 0))
28249aa32835SJeff Kirsher 		dev->rx_fifo_size = 2048;
28259aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "tx-fifo-size", &dev->tx_fifo_size, 0))
28269aa32835SJeff Kirsher 		dev->tx_fifo_size = 2048;
28279aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "rx-fifo-size-gige", &dev->rx_fifo_size_gige, 0))
28289aa32835SJeff Kirsher 		dev->rx_fifo_size_gige = dev->rx_fifo_size;
28299aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "tx-fifo-size-gige", &dev->tx_fifo_size_gige, 0))
28309aa32835SJeff Kirsher 		dev->tx_fifo_size_gige = dev->tx_fifo_size;
28319aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "phy-address", &dev->phy_address, 0))
28329aa32835SJeff Kirsher 		dev->phy_address = 0xffffffff;
28339aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "phy-map", &dev->phy_map, 0))
28349aa32835SJeff Kirsher 		dev->phy_map = 0xffffffff;
28359aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "gpcs-address", &dev->gpcs_address, 0))
28369aa32835SJeff Kirsher 		dev->gpcs_address = 0xffffffff;
28379aa32835SJeff Kirsher 	if (emac_read_uint_prop(np->parent, "clock-frequency", &dev->opb_bus_freq, 1))
28389aa32835SJeff Kirsher 		return -ENXIO;
28399aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "tah-device", &dev->tah_ph, 0))
28409aa32835SJeff Kirsher 		dev->tah_ph = 0;
28419aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "tah-channel", &dev->tah_port, 0))
28429aa32835SJeff Kirsher 		dev->tah_port = 0;
28439aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "mdio-device", &dev->mdio_ph, 0))
28449aa32835SJeff Kirsher 		dev->mdio_ph = 0;
28459aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "zmii-device", &dev->zmii_ph, 0))
28469aa32835SJeff Kirsher 		dev->zmii_ph = 0;
28479aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "zmii-channel", &dev->zmii_port, 0))
28489aa32835SJeff Kirsher 		dev->zmii_port = 0xffffffff;
28499aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "rgmii-device", &dev->rgmii_ph, 0))
28509aa32835SJeff Kirsher 		dev->rgmii_ph = 0;
28519aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "rgmii-channel", &dev->rgmii_port, 0))
28529aa32835SJeff Kirsher 		dev->rgmii_port = 0xffffffff;
28539aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "fifo-entry-size", &dev->fifo_entry_size, 0))
28549aa32835SJeff Kirsher 		dev->fifo_entry_size = 16;
28559aa32835SJeff Kirsher 	if (emac_read_uint_prop(np, "mal-burst-size", &dev->mal_burst_size, 0))
28569aa32835SJeff Kirsher 		dev->mal_burst_size = 256;
28579aa32835SJeff Kirsher 
28589aa32835SJeff Kirsher 	/* PHY mode needs some decoding */
28590c65b2b9SAndrew Lunn 	err = of_get_phy_mode(np, &dev->phy_mode);
28600c65b2b9SAndrew Lunn 	if (err)
286178b69921SChristian Lamparter 		dev->phy_mode = PHY_INTERFACE_MODE_NA;
28629aa32835SJeff Kirsher 
28639aa32835SJeff Kirsher 	/* Check EMAC version */
28649aa32835SJeff Kirsher 	if (of_device_is_compatible(np, "ibm,emac4sync")) {
28659aa32835SJeff Kirsher 		dev->features |= (EMAC_FTR_EMAC4 | EMAC_FTR_EMAC4SYNC);
28669aa32835SJeff Kirsher 		if (of_device_is_compatible(np, "ibm,emac-460ex") ||
28679aa32835SJeff Kirsher 		    of_device_is_compatible(np, "ibm,emac-460gt"))
28689aa32835SJeff Kirsher 			dev->features |= EMAC_FTR_460EX_PHY_CLK_FIX;
28699aa32835SJeff Kirsher 		if (of_device_is_compatible(np, "ibm,emac-405ex") ||
28709aa32835SJeff Kirsher 		    of_device_is_compatible(np, "ibm,emac-405exr"))
28719aa32835SJeff Kirsher 			dev->features |= EMAC_FTR_440EP_PHY_CLK_FIX;
2872ae5d3372SDuc Dang 		if (of_device_is_compatible(np, "ibm,emac-apm821xx")) {
2873ae5d3372SDuc Dang 			dev->features |= (EMAC_APM821XX_REQ_JUMBO_FRAME_SIZE |
2874ae5d3372SDuc Dang 					  EMAC_FTR_APM821XX_NO_HALF_DUPLEX |
2875ae5d3372SDuc Dang 					  EMAC_FTR_460EX_PHY_CLK_FIX);
2876ae5d3372SDuc Dang 		}
28779aa32835SJeff Kirsher 	} else if (of_device_is_compatible(np, "ibm,emac4")) {
28789aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_EMAC4;
28799aa32835SJeff Kirsher 		if (of_device_is_compatible(np, "ibm,emac-440gx"))
28809aa32835SJeff Kirsher 			dev->features |= EMAC_FTR_440GX_PHY_CLK_FIX;
28819aa32835SJeff Kirsher 	} else {
28829aa32835SJeff Kirsher 		if (of_device_is_compatible(np, "ibm,emac-440ep") ||
28839aa32835SJeff Kirsher 		    of_device_is_compatible(np, "ibm,emac-440gr"))
28849aa32835SJeff Kirsher 			dev->features |= EMAC_FTR_440EP_PHY_CLK_FIX;
28859aa32835SJeff Kirsher 		if (of_device_is_compatible(np, "ibm,emac-405ez")) {
28863b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_NO_FLOW_CTRL
28879aa32835SJeff Kirsher 			dev->features |= EMAC_FTR_NO_FLOW_CONTROL_40x;
28889aa32835SJeff Kirsher #else
2889f7ce9103SRob Herring 			printk(KERN_ERR "%pOF: Flow control not disabled!\n",
2890f7ce9103SRob Herring 					np);
28919aa32835SJeff Kirsher 			return -ENXIO;
28929aa32835SJeff Kirsher #endif
28939aa32835SJeff Kirsher 		}
28949aa32835SJeff Kirsher 
28959aa32835SJeff Kirsher 	}
28969aa32835SJeff Kirsher 
28979aa32835SJeff Kirsher 	/* Fixup some feature bits based on the device tree */
28981a87e641SRob Herring 	if (of_property_read_bool(np, "has-inverted-stacr-oc"))
28999aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_STACR_OC_INVERT;
29001a87e641SRob Herring 	if (of_property_read_bool(np, "has-new-stacr-staopc"))
29019aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_HAS_NEW_STACR;
29029aa32835SJeff Kirsher 
29039aa32835SJeff Kirsher 	/* CAB lacks the appropriate properties */
29049aa32835SJeff Kirsher 	if (of_device_is_compatible(np, "ibm,emac-axon"))
29059aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_HAS_NEW_STACR |
29069aa32835SJeff Kirsher 			EMAC_FTR_STACR_OC_INVERT;
29079aa32835SJeff Kirsher 
29089aa32835SJeff Kirsher 	/* Enable TAH/ZMII/RGMII features as found */
29099aa32835SJeff Kirsher 	if (dev->tah_ph != 0) {
29103b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_TAH
29119aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_HAS_TAH;
29129aa32835SJeff Kirsher #else
2913f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: TAH support not enabled !\n", np);
29149aa32835SJeff Kirsher 		return -ENXIO;
29159aa32835SJeff Kirsher #endif
29169aa32835SJeff Kirsher 	}
29179aa32835SJeff Kirsher 
29189aa32835SJeff Kirsher 	if (dev->zmii_ph != 0) {
29193b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_ZMII
29209aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_HAS_ZMII;
29219aa32835SJeff Kirsher #else
2922f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: ZMII support not enabled !\n", np);
29239aa32835SJeff Kirsher 		return -ENXIO;
29249aa32835SJeff Kirsher #endif
29259aa32835SJeff Kirsher 	}
29269aa32835SJeff Kirsher 
29279aa32835SJeff Kirsher 	if (dev->rgmii_ph != 0) {
29283b3bceefSTony Breeds #ifdef CONFIG_IBM_EMAC_RGMII
29299aa32835SJeff Kirsher 		dev->features |= EMAC_FTR_HAS_RGMII;
29309aa32835SJeff Kirsher #else
2931f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: RGMII support not enabled !\n", np);
29329aa32835SJeff Kirsher 		return -ENXIO;
29339aa32835SJeff Kirsher #endif
29349aa32835SJeff Kirsher 	}
29359aa32835SJeff Kirsher 
29369aa32835SJeff Kirsher 	/* Read MAC-address */
293768a06402SJakub Kicinski 	err = of_get_ethdev_address(np, dev->ndev);
2938b6dc230fSYang Yingliang 	if (err)
2939b6dc230fSYang Yingliang 		return dev_err_probe(&dev->ofdev->dev, err,
2940b6dc230fSYang Yingliang 				     "Can't get valid [local-]mac-address from OF !\n");
29419aa32835SJeff Kirsher 
29429aa32835SJeff Kirsher 	/* IAHT and GAHT filter parameterization */
29439aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
29449aa32835SJeff Kirsher 		dev->xaht_slots_shift = EMAC4SYNC_XAHT_SLOTS_SHIFT;
29459aa32835SJeff Kirsher 		dev->xaht_width_shift = EMAC4SYNC_XAHT_WIDTH_SHIFT;
29469aa32835SJeff Kirsher 	} else {
29479aa32835SJeff Kirsher 		dev->xaht_slots_shift = EMAC4_XAHT_SLOTS_SHIFT;
29489aa32835SJeff Kirsher 		dev->xaht_width_shift = EMAC4_XAHT_WIDTH_SHIFT;
29499aa32835SJeff Kirsher 	}
29509aa32835SJeff Kirsher 
2951ee4fccbeSKees Cook 	/* This should never happen */
2952ee4fccbeSKees Cook 	if (WARN_ON(EMAC_XAHT_REGS(dev) > EMAC_XAHT_MAX_REGS))
2953ee4fccbeSKees Cook 		return -ENXIO;
2954ee4fccbeSKees Cook 
29559aa32835SJeff Kirsher 	DBG(dev, "features     : 0x%08x / 0x%08x\n", dev->features, EMAC_FTRS_POSSIBLE);
29569aa32835SJeff Kirsher 	DBG(dev, "tx_fifo_size : %d (%d gige)\n", dev->tx_fifo_size, dev->tx_fifo_size_gige);
29579aa32835SJeff Kirsher 	DBG(dev, "rx_fifo_size : %d (%d gige)\n", dev->rx_fifo_size, dev->rx_fifo_size_gige);
29589aa32835SJeff Kirsher 	DBG(dev, "max_mtu      : %d\n", dev->max_mtu);
29599aa32835SJeff Kirsher 	DBG(dev, "OPB freq     : %d\n", dev->opb_bus_freq);
29609aa32835SJeff Kirsher 
29619aa32835SJeff Kirsher 	return 0;
29629aa32835SJeff Kirsher }
29639aa32835SJeff Kirsher 
29649aa32835SJeff Kirsher static const struct net_device_ops emac_netdev_ops = {
29659aa32835SJeff Kirsher 	.ndo_open		= emac_open,
29669aa32835SJeff Kirsher 	.ndo_stop		= emac_close,
29679aa32835SJeff Kirsher 	.ndo_get_stats		= emac_stats,
2968afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= emac_set_multicast_list,
2969a7605370SArnd Bergmann 	.ndo_eth_ioctl		= emac_ioctl,
29709aa32835SJeff Kirsher 	.ndo_tx_timeout		= emac_tx_timeout,
29719aa32835SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
297201afd972SIvan Mikhaylov 	.ndo_set_mac_address	= emac_set_mac_address,
29739aa32835SJeff Kirsher 	.ndo_start_xmit		= emac_start_xmit,
29749aa32835SJeff Kirsher };
29759aa32835SJeff Kirsher 
29769aa32835SJeff Kirsher static const struct net_device_ops emac_gige_netdev_ops = {
29779aa32835SJeff Kirsher 	.ndo_open		= emac_open,
29789aa32835SJeff Kirsher 	.ndo_stop		= emac_close,
29799aa32835SJeff Kirsher 	.ndo_get_stats		= emac_stats,
2980afc4b13dSJiri Pirko 	.ndo_set_rx_mode	= emac_set_multicast_list,
2981a7605370SArnd Bergmann 	.ndo_eth_ioctl		= emac_ioctl,
29829aa32835SJeff Kirsher 	.ndo_tx_timeout		= emac_tx_timeout,
29839aa32835SJeff Kirsher 	.ndo_validate_addr	= eth_validate_addr,
298401afd972SIvan Mikhaylov 	.ndo_set_mac_address	= emac_set_mac_address,
29859aa32835SJeff Kirsher 	.ndo_start_xmit		= emac_start_xmit_sg,
29869aa32835SJeff Kirsher 	.ndo_change_mtu		= emac_change_mtu,
29879aa32835SJeff Kirsher };
29889aa32835SJeff Kirsher 
emac_probe(struct platform_device * ofdev)2989fe17dc1eSBill Pemberton static int emac_probe(struct platform_device *ofdev)
29909aa32835SJeff Kirsher {
29919aa32835SJeff Kirsher 	struct net_device *ndev;
29929aa32835SJeff Kirsher 	struct emac_instance *dev;
29939aa32835SJeff Kirsher 	struct device_node *np = ofdev->dev.of_node;
29949aa32835SJeff Kirsher 	struct device_node **blist = NULL;
29959aa32835SJeff Kirsher 	int err, i;
29969aa32835SJeff Kirsher 
29979aa32835SJeff Kirsher 	/* Skip unused/unwired EMACS.  We leave the check for an unused
29989aa32835SJeff Kirsher 	 * property here for now, but new flat device trees should set a
29999aa32835SJeff Kirsher 	 * status property to "disabled" instead.
30009aa32835SJeff Kirsher 	 */
30011a87e641SRob Herring 	if (of_property_read_bool(np, "unused") || !of_device_is_available(np))
30029aa32835SJeff Kirsher 		return -ENODEV;
30039aa32835SJeff Kirsher 
30049aa32835SJeff Kirsher 	/* Find ourselves in the bootlist if we are there */
30059aa32835SJeff Kirsher 	for (i = 0; i < EMAC_BOOT_LIST_SIZE; i++)
30069aa32835SJeff Kirsher 		if (emac_boot_list[i] == np)
30079aa32835SJeff Kirsher 			blist = &emac_boot_list[i];
30089aa32835SJeff Kirsher 
30099aa32835SJeff Kirsher 	/* Allocate our net_device structure */
30109aa32835SJeff Kirsher 	err = -ENOMEM;
3011b9758c43SRosen Penev 	ndev = devm_alloc_etherdev(&ofdev->dev, sizeof(struct emac_instance));
301241de8d4cSJoe Perches 	if (!ndev)
30139aa32835SJeff Kirsher 		goto err_gone;
301441de8d4cSJoe Perches 
30159aa32835SJeff Kirsher 	dev = netdev_priv(ndev);
30169aa32835SJeff Kirsher 	dev->ndev = ndev;
30179aa32835SJeff Kirsher 	dev->ofdev = ofdev;
30189aa32835SJeff Kirsher 	dev->blist = blist;
30199aa32835SJeff Kirsher 	SET_NETDEV_DEV(ndev, &ofdev->dev);
30209aa32835SJeff Kirsher 
30219aa32835SJeff Kirsher 	/* Initialize some embedded data structures */
30229aa32835SJeff Kirsher 	mutex_init(&dev->mdio_lock);
30239aa32835SJeff Kirsher 	mutex_init(&dev->link_lock);
30249aa32835SJeff Kirsher 	spin_lock_init(&dev->lock);
30259aa32835SJeff Kirsher 	INIT_WORK(&dev->reset_work, emac_reset_work);
30269aa32835SJeff Kirsher 
30279aa32835SJeff Kirsher 	/* Init various config data based on device-tree */
30289aa32835SJeff Kirsher 	err = emac_init_config(dev);
3029138b57f0SChristophe Jaillet 	if (err)
3030b9758c43SRosen Penev 		goto err_gone;
30319aa32835SJeff Kirsher 
3032*39b9b780SRosen Penev 	/* Get interrupts. EMAC irq is mandatory */
30339aa32835SJeff Kirsher 	dev->emac_irq = irq_of_parse_and_map(np, 0);
303499c1790eSMichael Ellerman 	if (!dev->emac_irq) {
3035f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: Can't map main interrupt\n", np);
3036138b57f0SChristophe Jaillet 		err = -ENODEV;
3037b9758c43SRosen Penev 		goto err_gone;
30389aa32835SJeff Kirsher 	}
3039dcc34ef7SRosen Penev 
3040dcc34ef7SRosen Penev 	/* Setup error IRQ handler */
3041dcc34ef7SRosen Penev 	err = devm_request_irq(&ofdev->dev, dev->emac_irq, emac_irq, 0, "EMAC",
3042dcc34ef7SRosen Penev 			       dev);
3043dcc34ef7SRosen Penev 	if (err) {
3044dcc34ef7SRosen Penev 		dev_err_probe(&ofdev->dev, err, "failed to request IRQ %d",
3045dcc34ef7SRosen Penev 			      dev->emac_irq);
3046dcc34ef7SRosen Penev 		goto err_gone;
3047dcc34ef7SRosen Penev 	}
3048dcc34ef7SRosen Penev 
30499aa32835SJeff Kirsher 	ndev->irq = dev->emac_irq;
30509aa32835SJeff Kirsher 
30519aa32835SJeff Kirsher 	/* Map EMAC regs */
3052138b57f0SChristophe Jaillet 	// TODO : platform_get_resource() and devm_ioremap_resource()
3053969b002dSRosen Penev 	dev->emacp = devm_of_iomap(&ofdev->dev, np, 0, NULL);
3054969b002dSRosen Penev 	if (!dev->emacp) {
3055969b002dSRosen Penev 		dev_err(&ofdev->dev, "can't map device registers");
30569aa32835SJeff Kirsher 		err = -ENOMEM;
3057*39b9b780SRosen Penev 		goto err_gone;
30589aa32835SJeff Kirsher 	}
30599aa32835SJeff Kirsher 
30609aa32835SJeff Kirsher 	/* Wait for dependent devices */
30619aa32835SJeff Kirsher 	err = emac_wait_deps(dev);
3062c092d0beSRosen Penev 	if (err)
3063*39b9b780SRosen Penev 		goto err_gone;
3064bc353832SJingoo Han 	dev->mal = platform_get_drvdata(dev->mal_dev);
30659aa32835SJeff Kirsher 	if (dev->mdio_dev != NULL)
3066bc353832SJingoo Han 		dev->mdio_instance = platform_get_drvdata(dev->mdio_dev);
30679aa32835SJeff Kirsher 
30689aa32835SJeff Kirsher 	/* Register with MAL */
30699aa32835SJeff Kirsher 	dev->commac.ops = &emac_commac_ops;
30709aa32835SJeff Kirsher 	dev->commac.dev = dev;
30719aa32835SJeff Kirsher 	dev->commac.tx_chan_mask = MAL_CHAN_MASK(dev->mal_tx_chan);
30729aa32835SJeff Kirsher 	dev->commac.rx_chan_mask = MAL_CHAN_MASK(dev->mal_rx_chan);
30739aa32835SJeff Kirsher 	err = mal_register_commac(dev->mal, &dev->commac);
30749aa32835SJeff Kirsher 	if (err) {
3075f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: failed to register with mal %pOF!\n",
3076f7ce9103SRob Herring 		       np, dev->mal_dev->dev.of_node);
30779aa32835SJeff Kirsher 		goto err_rel_deps;
30789aa32835SJeff Kirsher 	}
30799aa32835SJeff Kirsher 	dev->rx_skb_size = emac_rx_skb_size(ndev->mtu);
30809aa32835SJeff Kirsher 	dev->rx_sync_size = emac_rx_sync_size(ndev->mtu);
30819aa32835SJeff Kirsher 
30829aa32835SJeff Kirsher 	/* Get pointers to BD rings */
30839aa32835SJeff Kirsher 	dev->tx_desc =
30849aa32835SJeff Kirsher 	    dev->mal->bd_virt + mal_tx_bd_offset(dev->mal, dev->mal_tx_chan);
30859aa32835SJeff Kirsher 	dev->rx_desc =
30869aa32835SJeff Kirsher 	    dev->mal->bd_virt + mal_rx_bd_offset(dev->mal, dev->mal_rx_chan);
30879aa32835SJeff Kirsher 
30889aa32835SJeff Kirsher 	DBG(dev, "tx_desc %p" NL, dev->tx_desc);
30899aa32835SJeff Kirsher 	DBG(dev, "rx_desc %p" NL, dev->rx_desc);
30909aa32835SJeff Kirsher 
30919aa32835SJeff Kirsher 	/* Clean rings */
30929aa32835SJeff Kirsher 	memset(dev->tx_desc, 0, NUM_TX_BUFF * sizeof(struct mal_descriptor));
30939aa32835SJeff Kirsher 	memset(dev->rx_desc, 0, NUM_RX_BUFF * sizeof(struct mal_descriptor));
30949aa32835SJeff Kirsher 	memset(dev->tx_skb, 0, NUM_TX_BUFF * sizeof(struct sk_buff *));
30959aa32835SJeff Kirsher 	memset(dev->rx_skb, 0, NUM_RX_BUFF * sizeof(struct sk_buff *));
30969aa32835SJeff Kirsher 
30979aa32835SJeff Kirsher 	/* Attach to ZMII, if needed */
30989aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII) &&
30999aa32835SJeff Kirsher 	    (err = zmii_attach(dev->zmii_dev, dev->zmii_port, &dev->phy_mode)) != 0)
31009aa32835SJeff Kirsher 		goto err_unreg_commac;
31019aa32835SJeff Kirsher 
31029aa32835SJeff Kirsher 	/* Attach to RGMII, if needed */
31039aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII) &&
31049aa32835SJeff Kirsher 	    (err = rgmii_attach(dev->rgmii_dev, dev->rgmii_port, dev->phy_mode)) != 0)
31059aa32835SJeff Kirsher 		goto err_detach_zmii;
31069aa32835SJeff Kirsher 
31079aa32835SJeff Kirsher 	/* Attach to TAH, if needed */
31089aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH) &&
31099aa32835SJeff Kirsher 	    (err = tah_attach(dev->tah_dev, dev->tah_port)) != 0)
31109aa32835SJeff Kirsher 		goto err_detach_rgmii;
31119aa32835SJeff Kirsher 
31129aa32835SJeff Kirsher 	/* Set some link defaults before we can find out real parameters */
31139aa32835SJeff Kirsher 	dev->phy.speed = SPEED_100;
31149aa32835SJeff Kirsher 	dev->phy.duplex = DUPLEX_FULL;
31159aa32835SJeff Kirsher 	dev->phy.autoneg = AUTONEG_DISABLE;
31169aa32835SJeff Kirsher 	dev->phy.pause = dev->phy.asym_pause = 0;
31179aa32835SJeff Kirsher 	dev->stop_timeout = STOP_TIMEOUT_100;
31189aa32835SJeff Kirsher 	INIT_DELAYED_WORK(&dev->link_work, emac_link_timer);
31199aa32835SJeff Kirsher 
3120ae5d3372SDuc Dang 	/* Some SoCs like APM821xx does not support Half Duplex mode. */
3121ae5d3372SDuc Dang 	if (emac_has_feature(dev, EMAC_FTR_APM821XX_NO_HALF_DUPLEX)) {
3122ae5d3372SDuc Dang 		dev->phy_feat_exc = (SUPPORTED_1000baseT_Half |
3123ae5d3372SDuc Dang 				     SUPPORTED_100baseT_Half |
3124ae5d3372SDuc Dang 				     SUPPORTED_10baseT_Half);
3125ae5d3372SDuc Dang 	}
3126ae5d3372SDuc Dang 
31279aa32835SJeff Kirsher 	/* Find PHY if any */
31289aa32835SJeff Kirsher 	err = emac_init_phy(dev);
31299aa32835SJeff Kirsher 	if (err != 0)
31309aa32835SJeff Kirsher 		goto err_detach_tah;
31319aa32835SJeff Kirsher 
31329aa32835SJeff Kirsher 	if (dev->tah_dev) {
31339aa32835SJeff Kirsher 		ndev->hw_features = NETIF_F_IP_CSUM | NETIF_F_SG;
31349aa32835SJeff Kirsher 		ndev->features |= ndev->hw_features | NETIF_F_RXCSUM;
31359aa32835SJeff Kirsher 	}
31369aa32835SJeff Kirsher 	ndev->watchdog_timeo = 5 * HZ;
31379aa32835SJeff Kirsher 	if (emac_phy_supports_gige(dev->phy_mode)) {
31389aa32835SJeff Kirsher 		ndev->netdev_ops = &emac_gige_netdev_ops;
31399aa32835SJeff Kirsher 		dev->commac.ops = &emac_commac_sg_ops;
31409aa32835SJeff Kirsher 	} else
31419aa32835SJeff Kirsher 		ndev->netdev_ops = &emac_netdev_ops;
31427ad24ea4SWilfried Klaebe 	ndev->ethtool_ops = &emac_ethtool_ops;
31439aa32835SJeff Kirsher 
31443d5d96acSJarod Wilson 	/* MTU range: 46 - 1500 or whatever is in OF */
31453d5d96acSJarod Wilson 	ndev->min_mtu = EMAC_MIN_MTU;
31463d5d96acSJarod Wilson 	ndev->max_mtu = dev->max_mtu;
31473d5d96acSJarod Wilson 
31489aa32835SJeff Kirsher 	netif_carrier_off(ndev);
31499aa32835SJeff Kirsher 
3150a4dd8535SRosen Penev 	err = devm_register_netdev(&ofdev->dev, ndev);
31519aa32835SJeff Kirsher 	if (err) {
3152f7ce9103SRob Herring 		printk(KERN_ERR "%pOF: failed to register net device (%d)!\n",
3153f7ce9103SRob Herring 		       np, err);
31549aa32835SJeff Kirsher 		goto err_detach_tah;
31559aa32835SJeff Kirsher 	}
31569aa32835SJeff Kirsher 
31579aa32835SJeff Kirsher 	/* Set our drvdata last as we don't want them visible until we are
31589aa32835SJeff Kirsher 	 * fully initialized
31599aa32835SJeff Kirsher 	 */
31609aa32835SJeff Kirsher 	wmb();
3161bc353832SJingoo Han 	platform_set_drvdata(ofdev, dev);
31629aa32835SJeff Kirsher 
3163f7ce9103SRob Herring 	printk(KERN_INFO "%s: EMAC-%d %pOF, MAC %pM\n",
3164f7ce9103SRob Herring 	       ndev->name, dev->cell_index, np, ndev->dev_addr);
31659aa32835SJeff Kirsher 
316678b69921SChristian Lamparter 	if (dev->phy_mode == PHY_INTERFACE_MODE_SGMII)
31679aa32835SJeff Kirsher 		printk(KERN_NOTICE "%s: in SGMII mode\n", ndev->name);
31689aa32835SJeff Kirsher 
31699aa32835SJeff Kirsher 	if (dev->phy.address >= 0)
31709aa32835SJeff Kirsher 		printk("%s: found %s PHY (0x%02x)\n", ndev->name,
31719aa32835SJeff Kirsher 		       dev->phy.def->name, dev->phy.address);
31729aa32835SJeff Kirsher 
31739aa32835SJeff Kirsher 	/* Life is good */
31749aa32835SJeff Kirsher 	return 0;
31759aa32835SJeff Kirsher 
31769aa32835SJeff Kirsher 	/* I have a bad feeling about this ... */
31779aa32835SJeff Kirsher 
31789aa32835SJeff Kirsher  err_detach_tah:
31799aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
31809aa32835SJeff Kirsher 		tah_detach(dev->tah_dev, dev->tah_port);
31819aa32835SJeff Kirsher  err_detach_rgmii:
31829aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
31839aa32835SJeff Kirsher 		rgmii_detach(dev->rgmii_dev, dev->rgmii_port);
31849aa32835SJeff Kirsher  err_detach_zmii:
31859aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
31869aa32835SJeff Kirsher 		zmii_detach(dev->zmii_dev, dev->zmii_port);
31879aa32835SJeff Kirsher  err_unreg_commac:
31889aa32835SJeff Kirsher 	mal_unregister_commac(dev->mal, &dev->commac);
31899aa32835SJeff Kirsher  err_rel_deps:
31909aa32835SJeff Kirsher 	emac_put_deps(dev);
31919aa32835SJeff Kirsher  err_gone:
3192c092d0beSRosen Penev 	if (blist)
31939aa32835SJeff Kirsher 		*blist = NULL;
31949aa32835SJeff Kirsher 	return err;
31959aa32835SJeff Kirsher }
31969aa32835SJeff Kirsher 
emac_remove(struct platform_device * ofdev)3197ac35a3c4SUwe Kleine-König static void emac_remove(struct platform_device *ofdev)
31989aa32835SJeff Kirsher {
3199bc353832SJingoo Han 	struct emac_instance *dev = platform_get_drvdata(ofdev);
32009aa32835SJeff Kirsher 
32019aa32835SJeff Kirsher 	DBG(dev, "remove" NL);
32029aa32835SJeff Kirsher 
32039aa32835SJeff Kirsher 	cancel_work_sync(&dev->reset_work);
32049aa32835SJeff Kirsher 
32059aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_TAH))
32069aa32835SJeff Kirsher 		tah_detach(dev->tah_dev, dev->tah_port);
32079aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_RGMII))
32089aa32835SJeff Kirsher 		rgmii_detach(dev->rgmii_dev, dev->rgmii_port);
32099aa32835SJeff Kirsher 	if (emac_has_feature(dev, EMAC_FTR_HAS_ZMII))
32109aa32835SJeff Kirsher 		zmii_detach(dev->zmii_dev, dev->zmii_port);
32119aa32835SJeff Kirsher 
3212d6f14483SWolfgang Grandegger 	busy_phy_map &= ~(1 << dev->phy.address);
3213d6f14483SWolfgang Grandegger 	DBG(dev, "busy_phy_map now %#x" NL, busy_phy_map);
3214d6f14483SWolfgang Grandegger 
32159aa32835SJeff Kirsher 	mal_unregister_commac(dev->mal, &dev->commac);
32169aa32835SJeff Kirsher 	emac_put_deps(dev);
32179aa32835SJeff Kirsher }
32189aa32835SJeff Kirsher 
32199aa32835SJeff Kirsher /* XXX Features in here should be replaced by properties... */
322047b61667SFabian Frederick static const struct of_device_id emac_match[] =
32219aa32835SJeff Kirsher {
32229aa32835SJeff Kirsher 	{
32239aa32835SJeff Kirsher 		.type		= "network",
32249aa32835SJeff Kirsher 		.compatible	= "ibm,emac",
32259aa32835SJeff Kirsher 	},
32269aa32835SJeff Kirsher 	{
32279aa32835SJeff Kirsher 		.type		= "network",
32289aa32835SJeff Kirsher 		.compatible	= "ibm,emac4",
32299aa32835SJeff Kirsher 	},
32309aa32835SJeff Kirsher 	{
32319aa32835SJeff Kirsher 		.type		= "network",
32329aa32835SJeff Kirsher 		.compatible	= "ibm,emac4sync",
32339aa32835SJeff Kirsher 	},
32349aa32835SJeff Kirsher 	{},
32359aa32835SJeff Kirsher };
32369aa32835SJeff Kirsher MODULE_DEVICE_TABLE(of, emac_match);
32379aa32835SJeff Kirsher 
32389aa32835SJeff Kirsher static struct platform_driver emac_driver = {
32399aa32835SJeff Kirsher 	.driver = {
32409aa32835SJeff Kirsher 		.name = "emac",
32419aa32835SJeff Kirsher 		.of_match_table = emac_match,
32429aa32835SJeff Kirsher 	},
32439aa32835SJeff Kirsher 	.probe = emac_probe,
3244ac35a3c4SUwe Kleine-König 	.remove_new = emac_remove,
32459aa32835SJeff Kirsher };
32469aa32835SJeff Kirsher 
emac_make_bootlist(void)32479aa32835SJeff Kirsher static void __init emac_make_bootlist(void)
32489aa32835SJeff Kirsher {
32499aa32835SJeff Kirsher 	struct device_node *np = NULL;
3250c9003ec8SFabian Frederick 	int j, max, i = 0;
32519aa32835SJeff Kirsher 	int cell_indices[EMAC_BOOT_LIST_SIZE];
32529aa32835SJeff Kirsher 
32539aa32835SJeff Kirsher 	/* Collect EMACs */
32549aa32835SJeff Kirsher 	while((np = of_find_all_nodes(np)) != NULL) {
3255cc0c92ffSRosen Penev 		u32 idx;
32569aa32835SJeff Kirsher 
32579aa32835SJeff Kirsher 		if (of_match_node(emac_match, np) == NULL)
32589aa32835SJeff Kirsher 			continue;
32591a87e641SRob Herring 		if (of_property_read_bool(np, "unused"))
32609aa32835SJeff Kirsher 			continue;
3261cc0c92ffSRosen Penev 		if (of_property_read_u32(np, "cell-index", &idx))
32629aa32835SJeff Kirsher 			continue;
3263cc0c92ffSRosen Penev 		cell_indices[i] = idx;
32649aa32835SJeff Kirsher 		emac_boot_list[i++] = of_node_get(np);
32659aa32835SJeff Kirsher 		if (i >= EMAC_BOOT_LIST_SIZE) {
32669aa32835SJeff Kirsher 			of_node_put(np);
32679aa32835SJeff Kirsher 			break;
32689aa32835SJeff Kirsher 		}
32699aa32835SJeff Kirsher 	}
32709aa32835SJeff Kirsher 	max = i;
32719aa32835SJeff Kirsher 
32729aa32835SJeff Kirsher 	/* Bubble sort them (doh, what a creative algorithm :-) */
32739aa32835SJeff Kirsher 	for (i = 0; max > 1 && (i < (max - 1)); i++)
32749aa32835SJeff Kirsher 		for (j = i; j < max; j++) {
32759aa32835SJeff Kirsher 			if (cell_indices[i] > cell_indices[j]) {
3276c9003ec8SFabian Frederick 				swap(emac_boot_list[i], emac_boot_list[j]);
3277c9003ec8SFabian Frederick 				swap(cell_indices[i], cell_indices[j]);
32789aa32835SJeff Kirsher 			}
32799aa32835SJeff Kirsher 		}
32809aa32835SJeff Kirsher }
32819aa32835SJeff Kirsher 
emac_init(void)32829aa32835SJeff Kirsher static int __init emac_init(void)
32839aa32835SJeff Kirsher {
32849aa32835SJeff Kirsher 	int rc;
32859aa32835SJeff Kirsher 
32869aa32835SJeff Kirsher 	printk(KERN_INFO DRV_DESC ", version " DRV_VERSION "\n");
32879aa32835SJeff Kirsher 
32889aa32835SJeff Kirsher 	/* Build EMAC boot list */
32899aa32835SJeff Kirsher 	emac_make_bootlist();
32909aa32835SJeff Kirsher 
32919aa32835SJeff Kirsher 	/* Init submodules */
32929aa32835SJeff Kirsher 	rc = mal_init();
32939aa32835SJeff Kirsher 	if (rc)
32949aa32835SJeff Kirsher 		goto err;
32959aa32835SJeff Kirsher 	rc = zmii_init();
32969aa32835SJeff Kirsher 	if (rc)
32979aa32835SJeff Kirsher 		goto err_mal;
32989aa32835SJeff Kirsher 	rc = rgmii_init();
32999aa32835SJeff Kirsher 	if (rc)
33009aa32835SJeff Kirsher 		goto err_zmii;
33019aa32835SJeff Kirsher 	rc = tah_init();
33029aa32835SJeff Kirsher 	if (rc)
33039aa32835SJeff Kirsher 		goto err_rgmii;
33049aa32835SJeff Kirsher 	rc = platform_driver_register(&emac_driver);
33059aa32835SJeff Kirsher 	if (rc)
33069aa32835SJeff Kirsher 		goto err_tah;
33079aa32835SJeff Kirsher 
33089aa32835SJeff Kirsher 	return 0;
33099aa32835SJeff Kirsher 
33109aa32835SJeff Kirsher  err_tah:
33119aa32835SJeff Kirsher 	tah_exit();
33129aa32835SJeff Kirsher  err_rgmii:
33139aa32835SJeff Kirsher 	rgmii_exit();
33149aa32835SJeff Kirsher  err_zmii:
33159aa32835SJeff Kirsher 	zmii_exit();
33169aa32835SJeff Kirsher  err_mal:
33179aa32835SJeff Kirsher 	mal_exit();
33189aa32835SJeff Kirsher  err:
33199aa32835SJeff Kirsher 	return rc;
33209aa32835SJeff Kirsher }
33219aa32835SJeff Kirsher 
emac_exit(void)33229aa32835SJeff Kirsher static void __exit emac_exit(void)
33239aa32835SJeff Kirsher {
33249aa32835SJeff Kirsher 	int i;
33259aa32835SJeff Kirsher 
33269aa32835SJeff Kirsher 	platform_driver_unregister(&emac_driver);
33279aa32835SJeff Kirsher 
33289aa32835SJeff Kirsher 	tah_exit();
33299aa32835SJeff Kirsher 	rgmii_exit();
33309aa32835SJeff Kirsher 	zmii_exit();
33319aa32835SJeff Kirsher 	mal_exit();
33329aa32835SJeff Kirsher 
33339aa32835SJeff Kirsher 	/* Destroy EMAC boot list */
33349aa32835SJeff Kirsher 	for (i = 0; i < EMAC_BOOT_LIST_SIZE; i++)
33359aa32835SJeff Kirsher 		of_node_put(emac_boot_list[i]);
33369aa32835SJeff Kirsher }
33379aa32835SJeff Kirsher 
33389aa32835SJeff Kirsher module_init(emac_init);
33399aa32835SJeff Kirsher module_exit(emac_exit);
3340