xref: /openbmc/linux/drivers/net/dsa/microchip/ksz8795.c (revision 2c709e0bdad4d996ec8925b9ee6d5b97458708f1)
1e66f840cSTristram Ha // SPDX-License-Identifier: GPL-2.0
2e66f840cSTristram Ha /*
3e66f840cSTristram Ha  * Microchip KSZ8795 switch driver
4e66f840cSTristram Ha  *
5e66f840cSTristram Ha  * Copyright (C) 2017 Microchip Technology Inc.
6e66f840cSTristram Ha  *	Tristram Ha <Tristram.Ha@microchip.com>
7e66f840cSTristram Ha  */
8e66f840cSTristram Ha 
9e66f840cSTristram Ha #include <linux/delay.h>
10e66f840cSTristram Ha #include <linux/export.h>
11e66f840cSTristram Ha #include <linux/gpio.h>
12e66f840cSTristram Ha #include <linux/kernel.h>
13e66f840cSTristram Ha #include <linux/module.h>
14e66f840cSTristram Ha #include <linux/platform_data/microchip-ksz.h>
15e66f840cSTristram Ha #include <linux/phy.h>
16e66f840cSTristram Ha #include <linux/etherdevice.h>
17e66f840cSTristram Ha #include <linux/if_bridge.h>
18ec4b94f9SMichael Grzeschik #include <linux/micrel_phy.h>
19e66f840cSTristram Ha #include <net/dsa.h>
20e66f840cSTristram Ha #include <net/switchdev.h>
21*2c709e0bSMichael Grzeschik #include <linux/phylink.h>
22e66f840cSTristram Ha 
23e66f840cSTristram Ha #include "ksz_common.h"
24e66f840cSTristram Ha #include "ksz8795_reg.h"
259f73e112SMichael Grzeschik #include "ksz8.h"
269f73e112SMichael Grzeschik 
279f73e112SMichael Grzeschik static const u8 ksz8795_regs[] = {
289f73e112SMichael Grzeschik 	[REG_IND_CTRL_0]		= 0x6E,
299f73e112SMichael Grzeschik 	[REG_IND_DATA_8]		= 0x70,
309f73e112SMichael Grzeschik 	[REG_IND_DATA_CHECK]		= 0x72,
319f73e112SMichael Grzeschik 	[REG_IND_DATA_HI]		= 0x71,
329f73e112SMichael Grzeschik 	[REG_IND_DATA_LO]		= 0x75,
339f73e112SMichael Grzeschik 	[REG_IND_MIB_CHECK]		= 0x74,
349f73e112SMichael Grzeschik 	[P_FORCE_CTRL]			= 0x0C,
359f73e112SMichael Grzeschik 	[P_LINK_STATUS]			= 0x0E,
369f73e112SMichael Grzeschik 	[P_LOCAL_CTRL]			= 0x07,
379f73e112SMichael Grzeschik 	[P_NEG_RESTART_CTRL]		= 0x0D,
389f73e112SMichael Grzeschik 	[P_REMOTE_STATUS]		= 0x08,
399f73e112SMichael Grzeschik 	[P_SPEED_STATUS]		= 0x09,
409f73e112SMichael Grzeschik 	[S_TAIL_TAG_CTRL]		= 0x0C,
419f73e112SMichael Grzeschik };
429f73e112SMichael Grzeschik 
439f73e112SMichael Grzeschik static const u32 ksz8795_masks[] = {
449f73e112SMichael Grzeschik 	[PORT_802_1P_REMAPPING]		= BIT(7),
459f73e112SMichael Grzeschik 	[SW_TAIL_TAG_ENABLE]		= BIT(1),
469f73e112SMichael Grzeschik 	[MIB_COUNTER_OVERFLOW]		= BIT(6),
479f73e112SMichael Grzeschik 	[MIB_COUNTER_VALID]		= BIT(5),
489f73e112SMichael Grzeschik 	[VLAN_TABLE_FID]		= GENMASK(6, 0),
499f73e112SMichael Grzeschik 	[VLAN_TABLE_MEMBERSHIP]		= GENMASK(11, 7),
509f73e112SMichael Grzeschik 	[VLAN_TABLE_VALID]		= BIT(12),
519f73e112SMichael Grzeschik 	[STATIC_MAC_TABLE_VALID]	= BIT(21),
529f73e112SMichael Grzeschik 	[STATIC_MAC_TABLE_USE_FID]	= BIT(23),
539f73e112SMichael Grzeschik 	[STATIC_MAC_TABLE_FID]		= GENMASK(30, 24),
549f73e112SMichael Grzeschik 	[STATIC_MAC_TABLE_OVERRIDE]	= BIT(26),
559f73e112SMichael Grzeschik 	[STATIC_MAC_TABLE_FWD_PORTS]	= GENMASK(24, 20),
569f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_ENTRIES_H]	= GENMASK(6, 0),
579f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_MAC_EMPTY]	= BIT(8),
589f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_NOT_READY]	= BIT(7),
599f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_ENTRIES]	= GENMASK(31, 29),
609f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_FID]		= GENMASK(26, 20),
619f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_SRC_PORT]	= GENMASK(26, 24),
629f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TABLE_TIMESTAMP]	= GENMASK(28, 27),
639f73e112SMichael Grzeschik };
649f73e112SMichael Grzeschik 
659f73e112SMichael Grzeschik static const u8 ksz8795_shifts[] = {
669f73e112SMichael Grzeschik 	[VLAN_TABLE_MEMBERSHIP_S]	= 7,
679f73e112SMichael Grzeschik 	[VLAN_TABLE]			= 16,
689f73e112SMichael Grzeschik 	[STATIC_MAC_FWD_PORTS]		= 16,
699f73e112SMichael Grzeschik 	[STATIC_MAC_FID]		= 24,
709f73e112SMichael Grzeschik 	[DYNAMIC_MAC_ENTRIES_H]		= 3,
719f73e112SMichael Grzeschik 	[DYNAMIC_MAC_ENTRIES]		= 29,
729f73e112SMichael Grzeschik 	[DYNAMIC_MAC_FID]		= 16,
739f73e112SMichael Grzeschik 	[DYNAMIC_MAC_TIMESTAMP]		= 27,
749f73e112SMichael Grzeschik 	[DYNAMIC_MAC_SRC_PORT]		= 24,
759f73e112SMichael Grzeschik };
76e66f840cSTristram Ha 
774b20a07eSOleksij Rempel static const u8 ksz8863_regs[] = {
784b20a07eSOleksij Rempel 	[REG_IND_CTRL_0]		= 0x79,
794b20a07eSOleksij Rempel 	[REG_IND_DATA_8]		= 0x7B,
804b20a07eSOleksij Rempel 	[REG_IND_DATA_CHECK]		= 0x7B,
814b20a07eSOleksij Rempel 	[REG_IND_DATA_HI]		= 0x7C,
824b20a07eSOleksij Rempel 	[REG_IND_DATA_LO]		= 0x80,
834b20a07eSOleksij Rempel 	[REG_IND_MIB_CHECK]		= 0x80,
844b20a07eSOleksij Rempel 	[P_FORCE_CTRL]			= 0x0C,
854b20a07eSOleksij Rempel 	[P_LINK_STATUS]			= 0x0E,
864b20a07eSOleksij Rempel 	[P_LOCAL_CTRL]			= 0x0C,
874b20a07eSOleksij Rempel 	[P_NEG_RESTART_CTRL]		= 0x0D,
884b20a07eSOleksij Rempel 	[P_REMOTE_STATUS]		= 0x0E,
894b20a07eSOleksij Rempel 	[P_SPEED_STATUS]		= 0x0F,
904b20a07eSOleksij Rempel 	[S_TAIL_TAG_CTRL]		= 0x03,
914b20a07eSOleksij Rempel };
924b20a07eSOleksij Rempel 
934b20a07eSOleksij Rempel static const u32 ksz8863_masks[] = {
944b20a07eSOleksij Rempel 	[PORT_802_1P_REMAPPING]		= BIT(3),
954b20a07eSOleksij Rempel 	[SW_TAIL_TAG_ENABLE]		= BIT(6),
964b20a07eSOleksij Rempel 	[MIB_COUNTER_OVERFLOW]		= BIT(7),
974b20a07eSOleksij Rempel 	[MIB_COUNTER_VALID]		= BIT(6),
984b20a07eSOleksij Rempel 	[VLAN_TABLE_FID]		= GENMASK(15, 12),
994b20a07eSOleksij Rempel 	[VLAN_TABLE_MEMBERSHIP]		= GENMASK(18, 16),
1004b20a07eSOleksij Rempel 	[VLAN_TABLE_VALID]		= BIT(19),
1014b20a07eSOleksij Rempel 	[STATIC_MAC_TABLE_VALID]	= BIT(19),
1024b20a07eSOleksij Rempel 	[STATIC_MAC_TABLE_USE_FID]	= BIT(21),
1034b20a07eSOleksij Rempel 	[STATIC_MAC_TABLE_FID]		= GENMASK(29, 26),
1044b20a07eSOleksij Rempel 	[STATIC_MAC_TABLE_OVERRIDE]	= BIT(20),
1054b20a07eSOleksij Rempel 	[STATIC_MAC_TABLE_FWD_PORTS]	= GENMASK(18, 16),
1064b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_ENTRIES_H]	= GENMASK(5, 0),
1074b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_MAC_EMPTY]	= BIT(7),
1084b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_NOT_READY]	= BIT(7),
1094b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_ENTRIES]	= GENMASK(31, 28),
1104b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_FID]		= GENMASK(19, 16),
1114b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_SRC_PORT]	= GENMASK(21, 20),
1124b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TABLE_TIMESTAMP]	= GENMASK(23, 22),
1134b20a07eSOleksij Rempel };
1144b20a07eSOleksij Rempel 
1154b20a07eSOleksij Rempel static u8 ksz8863_shifts[] = {
1164b20a07eSOleksij Rempel 	[VLAN_TABLE_MEMBERSHIP_S]	= 16,
1174b20a07eSOleksij Rempel 	[STATIC_MAC_FWD_PORTS]		= 16,
1184b20a07eSOleksij Rempel 	[STATIC_MAC_FID]		= 22,
1194b20a07eSOleksij Rempel 	[DYNAMIC_MAC_ENTRIES_H]		= 3,
1204b20a07eSOleksij Rempel 	[DYNAMIC_MAC_ENTRIES]		= 24,
1214b20a07eSOleksij Rempel 	[DYNAMIC_MAC_FID]		= 16,
1224b20a07eSOleksij Rempel 	[DYNAMIC_MAC_TIMESTAMP]		= 24,
1234b20a07eSOleksij Rempel 	[DYNAMIC_MAC_SRC_PORT]		= 20,
1244b20a07eSOleksij Rempel };
1254b20a07eSOleksij Rempel 
1264b20a07eSOleksij Rempel struct mib_names {
127e66f840cSTristram Ha 	char string[ETH_GSTRING_LEN];
1284b20a07eSOleksij Rempel };
1294b20a07eSOleksij Rempel 
1304b20a07eSOleksij Rempel static const struct mib_names ksz87xx_mib_names[] = {
131e66f840cSTristram Ha 	{ "rx_hi" },
132e66f840cSTristram Ha 	{ "rx_undersize" },
133e66f840cSTristram Ha 	{ "rx_fragments" },
134e66f840cSTristram Ha 	{ "rx_oversize" },
135e66f840cSTristram Ha 	{ "rx_jabbers" },
136e66f840cSTristram Ha 	{ "rx_symbol_err" },
137e66f840cSTristram Ha 	{ "rx_crc_err" },
138e66f840cSTristram Ha 	{ "rx_align_err" },
139e66f840cSTristram Ha 	{ "rx_mac_ctrl" },
140e66f840cSTristram Ha 	{ "rx_pause" },
141e66f840cSTristram Ha 	{ "rx_bcast" },
142e66f840cSTristram Ha 	{ "rx_mcast" },
143e66f840cSTristram Ha 	{ "rx_ucast" },
144e66f840cSTristram Ha 	{ "rx_64_or_less" },
145e66f840cSTristram Ha 	{ "rx_65_127" },
146e66f840cSTristram Ha 	{ "rx_128_255" },
147e66f840cSTristram Ha 	{ "rx_256_511" },
148e66f840cSTristram Ha 	{ "rx_512_1023" },
149e66f840cSTristram Ha 	{ "rx_1024_1522" },
150e66f840cSTristram Ha 	{ "rx_1523_2000" },
151e66f840cSTristram Ha 	{ "rx_2001" },
152e66f840cSTristram Ha 	{ "tx_hi" },
153e66f840cSTristram Ha 	{ "tx_late_col" },
154e66f840cSTristram Ha 	{ "tx_pause" },
155e66f840cSTristram Ha 	{ "tx_bcast" },
156e66f840cSTristram Ha 	{ "tx_mcast" },
157e66f840cSTristram Ha 	{ "tx_ucast" },
158e66f840cSTristram Ha 	{ "tx_deferred" },
159e66f840cSTristram Ha 	{ "tx_total_col" },
160e66f840cSTristram Ha 	{ "tx_exc_col" },
161e66f840cSTristram Ha 	{ "tx_single_col" },
162e66f840cSTristram Ha 	{ "tx_mult_col" },
163e66f840cSTristram Ha 	{ "rx_total" },
164e66f840cSTristram Ha 	{ "tx_total" },
165e66f840cSTristram Ha 	{ "rx_discards" },
166e66f840cSTristram Ha 	{ "tx_discards" },
167e66f840cSTristram Ha };
168e66f840cSTristram Ha 
1694b20a07eSOleksij Rempel static const struct mib_names ksz88xx_mib_names[] = {
1704b20a07eSOleksij Rempel 	{ "rx" },
1714b20a07eSOleksij Rempel 	{ "rx_hi" },
1724b20a07eSOleksij Rempel 	{ "rx_undersize" },
1734b20a07eSOleksij Rempel 	{ "rx_fragments" },
1744b20a07eSOleksij Rempel 	{ "rx_oversize" },
1754b20a07eSOleksij Rempel 	{ "rx_jabbers" },
1764b20a07eSOleksij Rempel 	{ "rx_symbol_err" },
1774b20a07eSOleksij Rempel 	{ "rx_crc_err" },
1784b20a07eSOleksij Rempel 	{ "rx_align_err" },
1794b20a07eSOleksij Rempel 	{ "rx_mac_ctrl" },
1804b20a07eSOleksij Rempel 	{ "rx_pause" },
1814b20a07eSOleksij Rempel 	{ "rx_bcast" },
1824b20a07eSOleksij Rempel 	{ "rx_mcast" },
1834b20a07eSOleksij Rempel 	{ "rx_ucast" },
1844b20a07eSOleksij Rempel 	{ "rx_64_or_less" },
1854b20a07eSOleksij Rempel 	{ "rx_65_127" },
1864b20a07eSOleksij Rempel 	{ "rx_128_255" },
1874b20a07eSOleksij Rempel 	{ "rx_256_511" },
1884b20a07eSOleksij Rempel 	{ "rx_512_1023" },
1894b20a07eSOleksij Rempel 	{ "rx_1024_1522" },
1904b20a07eSOleksij Rempel 	{ "tx" },
1914b20a07eSOleksij Rempel 	{ "tx_hi" },
1924b20a07eSOleksij Rempel 	{ "tx_late_col" },
1934b20a07eSOleksij Rempel 	{ "tx_pause" },
1944b20a07eSOleksij Rempel 	{ "tx_bcast" },
1954b20a07eSOleksij Rempel 	{ "tx_mcast" },
1964b20a07eSOleksij Rempel 	{ "tx_ucast" },
1974b20a07eSOleksij Rempel 	{ "tx_deferred" },
1984b20a07eSOleksij Rempel 	{ "tx_total_col" },
1994b20a07eSOleksij Rempel 	{ "tx_exc_col" },
2004b20a07eSOleksij Rempel 	{ "tx_single_col" },
2014b20a07eSOleksij Rempel 	{ "tx_mult_col" },
2024b20a07eSOleksij Rempel 	{ "rx_discards" },
2034b20a07eSOleksij Rempel 	{ "tx_discards" },
2044b20a07eSOleksij Rempel };
2054b20a07eSOleksij Rempel 
2064b20a07eSOleksij Rempel static bool ksz_is_ksz88x3(struct ksz_device *dev)
2074b20a07eSOleksij Rempel {
2084b20a07eSOleksij Rempel 	return dev->chip_id == 0x8830;
2094b20a07eSOleksij Rempel }
2104b20a07eSOleksij Rempel 
211e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
212e66f840cSTristram Ha {
213e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
214e66f840cSTristram Ha }
215e66f840cSTristram Ha 
216e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
217e66f840cSTristram Ha 			 bool set)
218e66f840cSTristram Ha {
219e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
220e66f840cSTristram Ha 			   bits, set ? bits : 0);
221e66f840cSTristram Ha }
222e66f840cSTristram Ha 
2234b5baca0SMichael Grzeschik static int ksz8_reset_switch(struct ksz_device *dev)
224e66f840cSTristram Ha {
2254b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev)) {
2264b20a07eSOleksij Rempel 		/* reset switch */
2274b20a07eSOleksij Rempel 		ksz_cfg(dev, KSZ8863_REG_SW_RESET,
2284b20a07eSOleksij Rempel 			KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true);
2294b20a07eSOleksij Rempel 		ksz_cfg(dev, KSZ8863_REG_SW_RESET,
2304b20a07eSOleksij Rempel 			KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false);
2314b20a07eSOleksij Rempel 	} else {
232e66f840cSTristram Ha 		/* reset switch */
233e66f840cSTristram Ha 		ksz_write8(dev, REG_POWER_MANAGEMENT_1,
234e66f840cSTristram Ha 			   SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
235e66f840cSTristram Ha 		ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
2364b20a07eSOleksij Rempel 	}
237e66f840cSTristram Ha 
238e66f840cSTristram Ha 	return 0;
239e66f840cSTristram Ha }
240e66f840cSTristram Ha 
241e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue)
242e66f840cSTristram Ha {
243e66f840cSTristram Ha 	u8 hi, lo;
244e66f840cSTristram Ha 
245e66f840cSTristram Ha 	/* Number of queues can only be 1, 2, or 4. */
246e66f840cSTristram Ha 	switch (queue) {
247e66f840cSTristram Ha 	case 4:
248e66f840cSTristram Ha 	case 3:
249e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_4;
250e66f840cSTristram Ha 		break;
251e66f840cSTristram Ha 	case 2:
252e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_2;
253e66f840cSTristram Ha 		break;
254e66f840cSTristram Ha 	default:
255e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_1;
256e66f840cSTristram Ha 	}
257e66f840cSTristram Ha 	ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo);
258e66f840cSTristram Ha 	ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi);
259e66f840cSTristram Ha 	lo &= ~PORT_QUEUE_SPLIT_L;
260e66f840cSTristram Ha 	if (queue & PORT_QUEUE_SPLIT_2)
261e66f840cSTristram Ha 		lo |= PORT_QUEUE_SPLIT_L;
262e66f840cSTristram Ha 	hi &= ~PORT_QUEUE_SPLIT_H;
263e66f840cSTristram Ha 	if (queue & PORT_QUEUE_SPLIT_4)
264e66f840cSTristram Ha 		hi |= PORT_QUEUE_SPLIT_H;
265e66f840cSTristram Ha 	ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo);
266e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi);
267e66f840cSTristram Ha 
268e66f840cSTristram Ha 	/* Default is port based for egress rate limit. */
269e66f840cSTristram Ha 	if (queue != PORT_QUEUE_SPLIT_1)
270e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED,
271e66f840cSTristram Ha 			true);
272e66f840cSTristram Ha }
273e66f840cSTristram Ha 
2744b5baca0SMichael Grzeschik static void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
275e66f840cSTristram Ha {
2769f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
2779f73e112SMichael Grzeschik 	const u32 *masks;
2789f73e112SMichael Grzeschik 	const u8 *regs;
279e66f840cSTristram Ha 	u16 ctrl_addr;
280e66f840cSTristram Ha 	u32 data;
281e66f840cSTristram Ha 	u8 check;
282e66f840cSTristram Ha 	int loop;
283e66f840cSTristram Ha 
2849f73e112SMichael Grzeschik 	masks = ksz8->masks;
2859f73e112SMichael Grzeschik 	regs = ksz8->regs;
2869f73e112SMichael Grzeschik 
28731b62c78SMichael Grzeschik 	ctrl_addr = addr + dev->reg_mib_cnt * port;
288e66f840cSTristram Ha 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
289e66f840cSTristram Ha 
290e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
2919f73e112SMichael Grzeschik 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
292e66f840cSTristram Ha 
293e66f840cSTristram Ha 	/* It is almost guaranteed to always read the valid bit because of
294e66f840cSTristram Ha 	 * slow SPI speed.
295e66f840cSTristram Ha 	 */
296e66f840cSTristram Ha 	for (loop = 2; loop > 0; loop--) {
2979f73e112SMichael Grzeschik 		ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
298e66f840cSTristram Ha 
2999f73e112SMichael Grzeschik 		if (check & masks[MIB_COUNTER_VALID]) {
3009f73e112SMichael Grzeschik 			ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
3019f73e112SMichael Grzeschik 			if (check & masks[MIB_COUNTER_OVERFLOW])
302e66f840cSTristram Ha 				*cnt += MIB_COUNTER_VALUE + 1;
303e66f840cSTristram Ha 			*cnt += data & MIB_COUNTER_VALUE;
304e66f840cSTristram Ha 			break;
305e66f840cSTristram Ha 		}
306e66f840cSTristram Ha 	}
307e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
308e66f840cSTristram Ha }
309e66f840cSTristram Ha 
3104b20a07eSOleksij Rempel static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
311e66f840cSTristram Ha 			      u64 *dropped, u64 *cnt)
312e66f840cSTristram Ha {
3139f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
3149f73e112SMichael Grzeschik 	const u32 *masks;
3159f73e112SMichael Grzeschik 	const u8 *regs;
316e66f840cSTristram Ha 	u16 ctrl_addr;
317e66f840cSTristram Ha 	u32 data;
318e66f840cSTristram Ha 	u8 check;
319e66f840cSTristram Ha 	int loop;
320e66f840cSTristram Ha 
3219f73e112SMichael Grzeschik 	masks = ksz8->masks;
3229f73e112SMichael Grzeschik 	regs = ksz8->regs;
3239f73e112SMichael Grzeschik 
32431b62c78SMichael Grzeschik 	addr -= dev->reg_mib_cnt;
3254b20a07eSOleksij Rempel 	ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
3264b20a07eSOleksij Rempel 	ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
327e66f840cSTristram Ha 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
328e66f840cSTristram Ha 
329e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
3309f73e112SMichael Grzeschik 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
331e66f840cSTristram Ha 
332e66f840cSTristram Ha 	/* It is almost guaranteed to always read the valid bit because of
333e66f840cSTristram Ha 	 * slow SPI speed.
334e66f840cSTristram Ha 	 */
335e66f840cSTristram Ha 	for (loop = 2; loop > 0; loop--) {
3369f73e112SMichael Grzeschik 		ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
337e66f840cSTristram Ha 
3389f73e112SMichael Grzeschik 		if (check & masks[MIB_COUNTER_VALID]) {
3399f73e112SMichael Grzeschik 			ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
340e66f840cSTristram Ha 			if (addr < 2) {
341e66f840cSTristram Ha 				u64 total;
342e66f840cSTristram Ha 
343e66f840cSTristram Ha 				total = check & MIB_TOTAL_BYTES_H;
344e66f840cSTristram Ha 				total <<= 32;
345e66f840cSTristram Ha 				*cnt += total;
346e66f840cSTristram Ha 				*cnt += data;
3479f73e112SMichael Grzeschik 				if (check & masks[MIB_COUNTER_OVERFLOW]) {
348e66f840cSTristram Ha 					total = MIB_TOTAL_BYTES_H + 1;
349e66f840cSTristram Ha 					total <<= 32;
350e66f840cSTristram Ha 					*cnt += total;
351e66f840cSTristram Ha 				}
352e66f840cSTristram Ha 			} else {
3539f73e112SMichael Grzeschik 				if (check & masks[MIB_COUNTER_OVERFLOW])
354e66f840cSTristram Ha 					*cnt += MIB_PACKET_DROPPED + 1;
355e66f840cSTristram Ha 				*cnt += data & MIB_PACKET_DROPPED;
356e66f840cSTristram Ha 			}
357e66f840cSTristram Ha 			break;
358e66f840cSTristram Ha 		}
359e66f840cSTristram Ha 	}
360e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
361e66f840cSTristram Ha }
362e66f840cSTristram Ha 
3634b20a07eSOleksij Rempel static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
3644b20a07eSOleksij Rempel 			      u64 *dropped, u64 *cnt)
3654b20a07eSOleksij Rempel {
3664b20a07eSOleksij Rempel 	struct ksz8 *ksz8 = dev->priv;
3674b20a07eSOleksij Rempel 	const u8 *regs = ksz8->regs;
3684b20a07eSOleksij Rempel 	u32 *last = (u32 *)dropped;
3694b20a07eSOleksij Rempel 	u16 ctrl_addr;
3704b20a07eSOleksij Rempel 	u32 data;
3714b20a07eSOleksij Rempel 	u32 cur;
3724b20a07eSOleksij Rempel 
3734b20a07eSOleksij Rempel 	addr -= dev->reg_mib_cnt;
3744b20a07eSOleksij Rempel 	ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
3754b20a07eSOleksij Rempel 			   KSZ8863_MIB_PACKET_DROPPED_RX_0;
3764b20a07eSOleksij Rempel 	ctrl_addr += port;
3774b20a07eSOleksij Rempel 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
3784b20a07eSOleksij Rempel 
3794b20a07eSOleksij Rempel 	mutex_lock(&dev->alu_mutex);
3804b20a07eSOleksij Rempel 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
3814b20a07eSOleksij Rempel 	ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
3824b20a07eSOleksij Rempel 	mutex_unlock(&dev->alu_mutex);
3834b20a07eSOleksij Rempel 
3844b20a07eSOleksij Rempel 	data &= MIB_PACKET_DROPPED;
3854b20a07eSOleksij Rempel 	cur = last[addr];
3864b20a07eSOleksij Rempel 	if (data != cur) {
3874b20a07eSOleksij Rempel 		last[addr] = data;
3884b20a07eSOleksij Rempel 		if (data < cur)
3894b20a07eSOleksij Rempel 			data += MIB_PACKET_DROPPED + 1;
3904b20a07eSOleksij Rempel 		data -= cur;
3914b20a07eSOleksij Rempel 		*cnt += data;
3924b20a07eSOleksij Rempel 	}
3934b20a07eSOleksij Rempel }
3944b20a07eSOleksij Rempel 
3954b20a07eSOleksij Rempel static void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
3964b20a07eSOleksij Rempel 			   u64 *dropped, u64 *cnt)
3974b20a07eSOleksij Rempel {
3984b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev))
3994b20a07eSOleksij Rempel 		ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
4004b20a07eSOleksij Rempel 	else
4014b20a07eSOleksij Rempel 		ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
4024b20a07eSOleksij Rempel }
4034b20a07eSOleksij Rempel 
4044b5baca0SMichael Grzeschik static void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
405e66f840cSTristram Ha {
4064b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev))
4074b20a07eSOleksij Rempel 		return;
4084b20a07eSOleksij Rempel 
409e66f840cSTristram Ha 	/* enable the port for flush/freeze function */
410e66f840cSTristram Ha 	if (freeze)
411e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
412e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
413e66f840cSTristram Ha 
414e66f840cSTristram Ha 	/* disable the port after freeze is done */
415e66f840cSTristram Ha 	if (!freeze)
416e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
417e66f840cSTristram Ha }
418e66f840cSTristram Ha 
4194b5baca0SMichael Grzeschik static void ksz8_port_init_cnt(struct ksz_device *dev, int port)
420e66f840cSTristram Ha {
421e66f840cSTristram Ha 	struct ksz_port_mib *mib = &dev->ports[port].mib;
4224b20a07eSOleksij Rempel 	u64 *dropped;
423e66f840cSTristram Ha 
4244b20a07eSOleksij Rempel 	if (!ksz_is_ksz88x3(dev)) {
425e66f840cSTristram Ha 		/* flush all enabled port MIB counters */
426e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
427e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
428e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
4294b20a07eSOleksij Rempel 	}
430e66f840cSTristram Ha 
431e66f840cSTristram Ha 	mib->cnt_ptr = 0;
432e66f840cSTristram Ha 
433e66f840cSTristram Ha 	/* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
434e66f840cSTristram Ha 	while (mib->cnt_ptr < dev->reg_mib_cnt) {
435e66f840cSTristram Ha 		dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
436e66f840cSTristram Ha 					&mib->counters[mib->cnt_ptr]);
437e66f840cSTristram Ha 		++mib->cnt_ptr;
438e66f840cSTristram Ha 	}
439e66f840cSTristram Ha 
4404b20a07eSOleksij Rempel 	/* last one in storage */
4414b20a07eSOleksij Rempel 	dropped = &mib->counters[dev->mib_cnt];
4424b20a07eSOleksij Rempel 
443e66f840cSTristram Ha 	/* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
444e66f840cSTristram Ha 	while (mib->cnt_ptr < dev->mib_cnt) {
445e66f840cSTristram Ha 		dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
4464b20a07eSOleksij Rempel 					dropped, &mib->counters[mib->cnt_ptr]);
447e66f840cSTristram Ha 		++mib->cnt_ptr;
448e66f840cSTristram Ha 	}
449e66f840cSTristram Ha 	mib->cnt_ptr = 0;
450e66f840cSTristram Ha 	memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
451e66f840cSTristram Ha }
452e66f840cSTristram Ha 
4534b5baca0SMichael Grzeschik static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
454e66f840cSTristram Ha {
4559f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
4569f73e112SMichael Grzeschik 	const u8 *regs = ksz8->regs;
457e66f840cSTristram Ha 	u16 ctrl_addr;
458e66f840cSTristram Ha 
459e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
460e66f840cSTristram Ha 
461e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
4629f73e112SMichael Grzeschik 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
4639f73e112SMichael Grzeschik 	ksz_read64(dev, regs[REG_IND_DATA_HI], data);
464e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
465e66f840cSTristram Ha }
466e66f840cSTristram Ha 
4674b5baca0SMichael Grzeschik static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
468e66f840cSTristram Ha {
4699f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
4709f73e112SMichael Grzeschik 	const u8 *regs = ksz8->regs;
471e66f840cSTristram Ha 	u16 ctrl_addr;
472e66f840cSTristram Ha 
473e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(table) | addr;
474e66f840cSTristram Ha 
475e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
4769f73e112SMichael Grzeschik 	ksz_write64(dev, regs[REG_IND_DATA_HI], data);
4779f73e112SMichael Grzeschik 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
478e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
479e66f840cSTristram Ha }
480e66f840cSTristram Ha 
4814b5baca0SMichael Grzeschik static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
482e66f840cSTristram Ha {
4839f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
484e66f840cSTristram Ha 	int timeout = 100;
4859f73e112SMichael Grzeschik 	const u32 *masks;
4869f73e112SMichael Grzeschik 	const u8 *regs;
4879f73e112SMichael Grzeschik 
4889f73e112SMichael Grzeschik 	masks = ksz8->masks;
4899f73e112SMichael Grzeschik 	regs = ksz8->regs;
490e66f840cSTristram Ha 
491e66f840cSTristram Ha 	do {
4929f73e112SMichael Grzeschik 		ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
493e66f840cSTristram Ha 		timeout--;
4949f73e112SMichael Grzeschik 	} while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
495e66f840cSTristram Ha 
496e66f840cSTristram Ha 	/* Entry is not ready for accessing. */
4979f73e112SMichael Grzeschik 	if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) {
498e66f840cSTristram Ha 		return -EAGAIN;
499e66f840cSTristram Ha 	/* Entry is ready for accessing. */
500e66f840cSTristram Ha 	} else {
5019f73e112SMichael Grzeschik 		ksz_read8(dev, regs[REG_IND_DATA_8], data);
502e66f840cSTristram Ha 
503e66f840cSTristram Ha 		/* There is no valid entry in the table. */
5049f73e112SMichael Grzeschik 		if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY])
505e66f840cSTristram Ha 			return -ENXIO;
506e66f840cSTristram Ha 	}
507e66f840cSTristram Ha 	return 0;
508e66f840cSTristram Ha }
509e66f840cSTristram Ha 
5104b5baca0SMichael Grzeschik static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr,
511e66f840cSTristram Ha 				u8 *mac_addr, u8 *fid, u8 *src_port,
512e66f840cSTristram Ha 				u8 *timestamp, u16 *entries)
513e66f840cSTristram Ha {
5149f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
515e66f840cSTristram Ha 	u32 data_hi, data_lo;
5169f73e112SMichael Grzeschik 	const u8 *shifts;
5179f73e112SMichael Grzeschik 	const u32 *masks;
5189f73e112SMichael Grzeschik 	const u8 *regs;
519e66f840cSTristram Ha 	u16 ctrl_addr;
520e66f840cSTristram Ha 	u8 data;
521e66f840cSTristram Ha 	int rc;
522e66f840cSTristram Ha 
5239f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
5249f73e112SMichael Grzeschik 	masks = ksz8->masks;
5259f73e112SMichael Grzeschik 	regs = ksz8->regs;
5269f73e112SMichael Grzeschik 
527e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
528e66f840cSTristram Ha 
529e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
5309f73e112SMichael Grzeschik 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
531e66f840cSTristram Ha 
5324b5baca0SMichael Grzeschik 	rc = ksz8_valid_dyn_entry(dev, &data);
533e66f840cSTristram Ha 	if (rc == -EAGAIN) {
534e66f840cSTristram Ha 		if (addr == 0)
535e66f840cSTristram Ha 			*entries = 0;
536e66f840cSTristram Ha 	} else if (rc == -ENXIO) {
537e66f840cSTristram Ha 		*entries = 0;
538e66f840cSTristram Ha 	/* At least one valid entry in the table. */
539e66f840cSTristram Ha 	} else {
540e66f840cSTristram Ha 		u64 buf = 0;
541e66f840cSTristram Ha 		int cnt;
542e66f840cSTristram Ha 
5439f73e112SMichael Grzeschik 		ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
544e66f840cSTristram Ha 		data_hi = (u32)(buf >> 32);
545e66f840cSTristram Ha 		data_lo = (u32)buf;
546e66f840cSTristram Ha 
547e66f840cSTristram Ha 		/* Check out how many valid entry in the table. */
5489f73e112SMichael Grzeschik 		cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
5499f73e112SMichael Grzeschik 		cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
5509f73e112SMichael Grzeschik 		cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
5519f73e112SMichael Grzeschik 			shifts[DYNAMIC_MAC_ENTRIES];
552e66f840cSTristram Ha 		*entries = cnt + 1;
553e66f840cSTristram Ha 
5549f73e112SMichael Grzeschik 		*fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
5559f73e112SMichael Grzeschik 			shifts[DYNAMIC_MAC_FID];
5569f73e112SMichael Grzeschik 		*src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
5579f73e112SMichael Grzeschik 			shifts[DYNAMIC_MAC_SRC_PORT];
5589f73e112SMichael Grzeschik 		*timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >>
5599f73e112SMichael Grzeschik 			shifts[DYNAMIC_MAC_TIMESTAMP];
560e66f840cSTristram Ha 
561e66f840cSTristram Ha 		mac_addr[5] = (u8)data_lo;
562e66f840cSTristram Ha 		mac_addr[4] = (u8)(data_lo >> 8);
563e66f840cSTristram Ha 		mac_addr[3] = (u8)(data_lo >> 16);
564e66f840cSTristram Ha 		mac_addr[2] = (u8)(data_lo >> 24);
565e66f840cSTristram Ha 
566e66f840cSTristram Ha 		mac_addr[1] = (u8)data_hi;
567e66f840cSTristram Ha 		mac_addr[0] = (u8)(data_hi >> 8);
568e66f840cSTristram Ha 		rc = 0;
569e66f840cSTristram Ha 	}
570e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
571e66f840cSTristram Ha 
572e66f840cSTristram Ha 	return rc;
573e66f840cSTristram Ha }
574e66f840cSTristram Ha 
5754b5baca0SMichael Grzeschik static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
576e66f840cSTristram Ha 				struct alu_struct *alu)
577e66f840cSTristram Ha {
5789f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
579e66f840cSTristram Ha 	u32 data_hi, data_lo;
5809f73e112SMichael Grzeschik 	const u8 *shifts;
5819f73e112SMichael Grzeschik 	const u32 *masks;
582e66f840cSTristram Ha 	u64 data;
583e66f840cSTristram Ha 
5849f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
5859f73e112SMichael Grzeschik 	masks = ksz8->masks;
5869f73e112SMichael Grzeschik 
5874b5baca0SMichael Grzeschik 	ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
588e66f840cSTristram Ha 	data_hi = data >> 32;
589e66f840cSTristram Ha 	data_lo = (u32)data;
5909f73e112SMichael Grzeschik 	if (data_hi & (masks[STATIC_MAC_TABLE_VALID] |
5919f73e112SMichael Grzeschik 			masks[STATIC_MAC_TABLE_OVERRIDE])) {
592e66f840cSTristram Ha 		alu->mac[5] = (u8)data_lo;
593e66f840cSTristram Ha 		alu->mac[4] = (u8)(data_lo >> 8);
594e66f840cSTristram Ha 		alu->mac[3] = (u8)(data_lo >> 16);
595e66f840cSTristram Ha 		alu->mac[2] = (u8)(data_lo >> 24);
596e66f840cSTristram Ha 		alu->mac[1] = (u8)data_hi;
597e66f840cSTristram Ha 		alu->mac[0] = (u8)(data_hi >> 8);
5989f73e112SMichael Grzeschik 		alu->port_forward =
5999f73e112SMichael Grzeschik 			(data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
6009f73e112SMichael Grzeschik 				shifts[STATIC_MAC_FWD_PORTS];
601e66f840cSTristram Ha 		alu->is_override =
6029f73e112SMichael Grzeschik 			(data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
603e66f840cSTristram Ha 		data_hi >>= 1;
6049f73e112SMichael Grzeschik 		alu->is_static = true;
6059f73e112SMichael Grzeschik 		alu->is_use_fid =
6069f73e112SMichael Grzeschik 			(data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
6079f73e112SMichael Grzeschik 		alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
6089f73e112SMichael Grzeschik 				shifts[STATIC_MAC_FID];
609e66f840cSTristram Ha 		return 0;
610e66f840cSTristram Ha 	}
611e66f840cSTristram Ha 	return -ENXIO;
612e66f840cSTristram Ha }
613e66f840cSTristram Ha 
6144b5baca0SMichael Grzeschik static void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
615e66f840cSTristram Ha 				 struct alu_struct *alu)
616e66f840cSTristram Ha {
6179f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
618e66f840cSTristram Ha 	u32 data_hi, data_lo;
6199f73e112SMichael Grzeschik 	const u8 *shifts;
6209f73e112SMichael Grzeschik 	const u32 *masks;
621e66f840cSTristram Ha 	u64 data;
622e66f840cSTristram Ha 
6239f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
6249f73e112SMichael Grzeschik 	masks = ksz8->masks;
6259f73e112SMichael Grzeschik 
626e66f840cSTristram Ha 	data_lo = ((u32)alu->mac[2] << 24) |
627e66f840cSTristram Ha 		((u32)alu->mac[3] << 16) |
628e66f840cSTristram Ha 		((u32)alu->mac[4] << 8) | alu->mac[5];
629e66f840cSTristram Ha 	data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
6309f73e112SMichael Grzeschik 	data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
631e66f840cSTristram Ha 
632e66f840cSTristram Ha 	if (alu->is_override)
6339f73e112SMichael Grzeschik 		data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
634e66f840cSTristram Ha 	if (alu->is_use_fid) {
6359f73e112SMichael Grzeschik 		data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
6369f73e112SMichael Grzeschik 		data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
637e66f840cSTristram Ha 	}
638e66f840cSTristram Ha 	if (alu->is_static)
6399f73e112SMichael Grzeschik 		data_hi |= masks[STATIC_MAC_TABLE_VALID];
640e66f840cSTristram Ha 	else
6419f73e112SMichael Grzeschik 		data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
642e66f840cSTristram Ha 
643e66f840cSTristram Ha 	data = (u64)data_hi << 32 | data_lo;
6444b5baca0SMichael Grzeschik 	ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
645e66f840cSTristram Ha }
646e66f840cSTristram Ha 
6479f73e112SMichael Grzeschik static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
6489f73e112SMichael Grzeschik 			   u8 *member, u8 *valid)
649e66f840cSTristram Ha {
6509f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
6519f73e112SMichael Grzeschik 	const u8 *shifts;
6529f73e112SMichael Grzeschik 	const u32 *masks;
6539f73e112SMichael Grzeschik 
6549f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
6559f73e112SMichael Grzeschik 	masks = ksz8->masks;
6569f73e112SMichael Grzeschik 
6579f73e112SMichael Grzeschik 	*fid = vlan & masks[VLAN_TABLE_FID];
6589f73e112SMichael Grzeschik 	*member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
6599f73e112SMichael Grzeschik 			shifts[VLAN_TABLE_MEMBERSHIP_S];
6609f73e112SMichael Grzeschik 	*valid = !!(vlan & masks[VLAN_TABLE_VALID]);
661e66f840cSTristram Ha }
662e66f840cSTristram Ha 
6639f73e112SMichael Grzeschik static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
6649f73e112SMichael Grzeschik 			 u16 *vlan)
665e66f840cSTristram Ha {
6669f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
6679f73e112SMichael Grzeschik 	const u8 *shifts;
6689f73e112SMichael Grzeschik 	const u32 *masks;
6699f73e112SMichael Grzeschik 
6709f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
6719f73e112SMichael Grzeschik 	masks = ksz8->masks;
6729f73e112SMichael Grzeschik 
673e66f840cSTristram Ha 	*vlan = fid;
6749f73e112SMichael Grzeschik 	*vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
675e66f840cSTristram Ha 	if (valid)
6769f73e112SMichael Grzeschik 		*vlan |= masks[VLAN_TABLE_VALID];
677e66f840cSTristram Ha }
678e66f840cSTristram Ha 
6794b5baca0SMichael Grzeschik static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
680e66f840cSTristram Ha {
6819f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
6829f73e112SMichael Grzeschik 	const u8 *shifts;
683e66f840cSTristram Ha 	u64 data;
684e66f840cSTristram Ha 	int i;
685e66f840cSTristram Ha 
6869f73e112SMichael Grzeschik 	shifts = ksz8->shifts;
6879f73e112SMichael Grzeschik 
6884b5baca0SMichael Grzeschik 	ksz8_r_table(dev, TABLE_VLAN, addr, &data);
6894ce2a984SMichael Grzeschik 	addr *= dev->phy_port_cnt;
6904ce2a984SMichael Grzeschik 	for (i = 0; i < dev->phy_port_cnt; i++) {
691e66f840cSTristram Ha 		dev->vlan_cache[addr + i].table[0] = (u16)data;
6929f73e112SMichael Grzeschik 		data >>= shifts[VLAN_TABLE];
693e66f840cSTristram Ha 	}
694e66f840cSTristram Ha }
695e66f840cSTristram Ha 
6964b5baca0SMichael Grzeschik static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
697e66f840cSTristram Ha {
698e66f840cSTristram Ha 	int index;
699e66f840cSTristram Ha 	u16 *data;
700e66f840cSTristram Ha 	u16 addr;
701e66f840cSTristram Ha 	u64 buf;
702e66f840cSTristram Ha 
703e66f840cSTristram Ha 	data = (u16 *)&buf;
7044ce2a984SMichael Grzeschik 	addr = vid / dev->phy_port_cnt;
705e66f840cSTristram Ha 	index = vid & 3;
7064b5baca0SMichael Grzeschik 	ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
707e66f840cSTristram Ha 	*vlan = data[index];
708e66f840cSTristram Ha }
709e66f840cSTristram Ha 
7104b5baca0SMichael Grzeschik static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
711e66f840cSTristram Ha {
712e66f840cSTristram Ha 	int index;
713e66f840cSTristram Ha 	u16 *data;
714e66f840cSTristram Ha 	u16 addr;
715e66f840cSTristram Ha 	u64 buf;
716e66f840cSTristram Ha 
717e66f840cSTristram Ha 	data = (u16 *)&buf;
7184ce2a984SMichael Grzeschik 	addr = vid / dev->phy_port_cnt;
719e66f840cSTristram Ha 	index = vid & 3;
7204b5baca0SMichael Grzeschik 	ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
721e66f840cSTristram Ha 	data[index] = vlan;
722e66f840cSTristram Ha 	dev->vlan_cache[vid].table[0] = vlan;
7234b5baca0SMichael Grzeschik 	ksz8_w_table(dev, TABLE_VLAN, addr, buf);
724e66f840cSTristram Ha }
725e66f840cSTristram Ha 
7264b5baca0SMichael Grzeschik static void ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
727e66f840cSTristram Ha {
7289f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
729e66f840cSTristram Ha 	u8 restart, speed, ctrl, link;
7309f73e112SMichael Grzeschik 	const u8 *regs = ksz8->regs;
731e66f840cSTristram Ha 	int processed = true;
732e66f840cSTristram Ha 	u16 data = 0;
733e66f840cSTristram Ha 	u8 p = phy;
734e66f840cSTristram Ha 
735e66f840cSTristram Ha 	switch (reg) {
736ec4b94f9SMichael Grzeschik 	case MII_BMCR:
7379f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
7389f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
7399f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
740e66f840cSTristram Ha 		if (restart & PORT_PHY_LOOPBACK)
741ec4b94f9SMichael Grzeschik 			data |= BMCR_LOOPBACK;
742e66f840cSTristram Ha 		if (ctrl & PORT_FORCE_100_MBIT)
743ec4b94f9SMichael Grzeschik 			data |= BMCR_SPEED100;
7444b20a07eSOleksij Rempel 		if (ksz_is_ksz88x3(dev)) {
7454b20a07eSOleksij Rempel 			if ((ctrl & PORT_AUTO_NEG_ENABLE))
746ec4b94f9SMichael Grzeschik 				data |= BMCR_ANENABLE;
7474b20a07eSOleksij Rempel 		} else {
748e66f840cSTristram Ha 			if (!(ctrl & PORT_AUTO_NEG_DISABLE))
749ec4b94f9SMichael Grzeschik 				data |= BMCR_ANENABLE;
7504b20a07eSOleksij Rempel 		}
751e66f840cSTristram Ha 		if (restart & PORT_POWER_DOWN)
752ec4b94f9SMichael Grzeschik 			data |= BMCR_PDOWN;
753e66f840cSTristram Ha 		if (restart & PORT_AUTO_NEG_RESTART)
754ec4b94f9SMichael Grzeschik 			data |= BMCR_ANRESTART;
755e66f840cSTristram Ha 		if (ctrl & PORT_FORCE_FULL_DUPLEX)
756ec4b94f9SMichael Grzeschik 			data |= BMCR_FULLDPLX;
757e66f840cSTristram Ha 		if (speed & PORT_HP_MDIX)
758ec4b94f9SMichael Grzeschik 			data |= KSZ886X_BMCR_HP_MDIX;
759e66f840cSTristram Ha 		if (restart & PORT_FORCE_MDIX)
760ec4b94f9SMichael Grzeschik 			data |= KSZ886X_BMCR_FORCE_MDI;
761e66f840cSTristram Ha 		if (restart & PORT_AUTO_MDIX_DISABLE)
762ec4b94f9SMichael Grzeschik 			data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
763e66f840cSTristram Ha 		if (restart & PORT_TX_DISABLE)
764ec4b94f9SMichael Grzeschik 			data |= KSZ886X_BMCR_DISABLE_TRANSMIT;
765e66f840cSTristram Ha 		if (restart & PORT_LED_OFF)
766ec4b94f9SMichael Grzeschik 			data |= KSZ886X_BMCR_DISABLE_LED;
767e66f840cSTristram Ha 		break;
768ec4b94f9SMichael Grzeschik 	case MII_BMSR:
7699f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
770ec4b94f9SMichael Grzeschik 		data = BMSR_100FULL |
771ec4b94f9SMichael Grzeschik 		       BMSR_100HALF |
772ec4b94f9SMichael Grzeschik 		       BMSR_10FULL |
773ec4b94f9SMichael Grzeschik 		       BMSR_10HALF |
774ec4b94f9SMichael Grzeschik 		       BMSR_ANEGCAPABLE;
775e66f840cSTristram Ha 		if (link & PORT_AUTO_NEG_COMPLETE)
776ec4b94f9SMichael Grzeschik 			data |= BMSR_ANEGCOMPLETE;
777e66f840cSTristram Ha 		if (link & PORT_STAT_LINK_GOOD)
778ec4b94f9SMichael Grzeschik 			data |= BMSR_LSTATUS;
779e66f840cSTristram Ha 		break;
780ec4b94f9SMichael Grzeschik 	case MII_PHYSID1:
781e66f840cSTristram Ha 		data = KSZ8795_ID_HI;
782e66f840cSTristram Ha 		break;
783ec4b94f9SMichael Grzeschik 	case MII_PHYSID2:
7844b20a07eSOleksij Rempel 		if (ksz_is_ksz88x3(dev))
7854b20a07eSOleksij Rempel 			data = KSZ8863_ID_LO;
7864b20a07eSOleksij Rempel 		else
787e66f840cSTristram Ha 			data = KSZ8795_ID_LO;
788e66f840cSTristram Ha 		break;
789ec4b94f9SMichael Grzeschik 	case MII_ADVERTISE:
7909f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
791ec4b94f9SMichael Grzeschik 		data = ADVERTISE_CSMA;
792e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
793ec4b94f9SMichael Grzeschik 			data |= ADVERTISE_PAUSE_CAP;
794e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_100BTX_FD)
795ec4b94f9SMichael Grzeschik 			data |= ADVERTISE_100FULL;
796e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_100BTX)
797ec4b94f9SMichael Grzeschik 			data |= ADVERTISE_100HALF;
798e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_10BT_FD)
799ec4b94f9SMichael Grzeschik 			data |= ADVERTISE_10FULL;
800e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_10BT)
801ec4b94f9SMichael Grzeschik 			data |= ADVERTISE_10HALF;
802e66f840cSTristram Ha 		break;
803ec4b94f9SMichael Grzeschik 	case MII_LPA:
8049f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
805ec4b94f9SMichael Grzeschik 		data = LPA_SLCT;
806e66f840cSTristram Ha 		if (link & PORT_REMOTE_SYM_PAUSE)
807ec4b94f9SMichael Grzeschik 			data |= LPA_PAUSE_CAP;
808e66f840cSTristram Ha 		if (link & PORT_REMOTE_100BTX_FD)
809ec4b94f9SMichael Grzeschik 			data |= LPA_100FULL;
810e66f840cSTristram Ha 		if (link & PORT_REMOTE_100BTX)
811ec4b94f9SMichael Grzeschik 			data |= LPA_100HALF;
812e66f840cSTristram Ha 		if (link & PORT_REMOTE_10BT_FD)
813ec4b94f9SMichael Grzeschik 			data |= LPA_10FULL;
814e66f840cSTristram Ha 		if (link & PORT_REMOTE_10BT)
815ec4b94f9SMichael Grzeschik 			data |= LPA_10HALF;
816ec4b94f9SMichael Grzeschik 		if (data & ~LPA_SLCT)
817ec4b94f9SMichael Grzeschik 			data |= LPA_LPACK;
818e66f840cSTristram Ha 		break;
819e66f840cSTristram Ha 	default:
820e66f840cSTristram Ha 		processed = false;
821e66f840cSTristram Ha 		break;
822e66f840cSTristram Ha 	}
823e66f840cSTristram Ha 	if (processed)
824e66f840cSTristram Ha 		*val = data;
825e66f840cSTristram Ha }
826e66f840cSTristram Ha 
8274b5baca0SMichael Grzeschik static void ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
828e66f840cSTristram Ha {
8299f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
830e66f840cSTristram Ha 	u8 restart, speed, ctrl, data;
8319f73e112SMichael Grzeschik 	const u8 *regs = ksz8->regs;
8329f73e112SMichael Grzeschik 	u8 p = phy;
833e66f840cSTristram Ha 
834e66f840cSTristram Ha 	switch (reg) {
835ec4b94f9SMichael Grzeschik 	case MII_BMCR:
836e66f840cSTristram Ha 
837e66f840cSTristram Ha 		/* Do not support PHY reset function. */
838ec4b94f9SMichael Grzeschik 		if (val & BMCR_RESET)
839e66f840cSTristram Ha 			break;
8409f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
841e66f840cSTristram Ha 		data = speed;
842ec4b94f9SMichael Grzeschik 		if (val & KSZ886X_BMCR_HP_MDIX)
843e66f840cSTristram Ha 			data |= PORT_HP_MDIX;
844e66f840cSTristram Ha 		else
845e66f840cSTristram Ha 			data &= ~PORT_HP_MDIX;
846e66f840cSTristram Ha 		if (data != speed)
8479f73e112SMichael Grzeschik 			ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data);
8489f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
849e66f840cSTristram Ha 		data = ctrl;
8504b20a07eSOleksij Rempel 		if (ksz_is_ksz88x3(dev)) {
851ec4b94f9SMichael Grzeschik 			if ((val & BMCR_ANENABLE))
8524b20a07eSOleksij Rempel 				data |= PORT_AUTO_NEG_ENABLE;
8534b20a07eSOleksij Rempel 			else
8544b20a07eSOleksij Rempel 				data &= ~PORT_AUTO_NEG_ENABLE;
8554b20a07eSOleksij Rempel 		} else {
856ec4b94f9SMichael Grzeschik 			if (!(val & BMCR_ANENABLE))
857e66f840cSTristram Ha 				data |= PORT_AUTO_NEG_DISABLE;
858e66f840cSTristram Ha 			else
859e66f840cSTristram Ha 				data &= ~PORT_AUTO_NEG_DISABLE;
860e66f840cSTristram Ha 
861e66f840cSTristram Ha 			/* Fiber port does not support auto-negotiation. */
862e66f840cSTristram Ha 			if (dev->ports[p].fiber)
863e66f840cSTristram Ha 				data |= PORT_AUTO_NEG_DISABLE;
8644b20a07eSOleksij Rempel 		}
8654b20a07eSOleksij Rempel 
866ec4b94f9SMichael Grzeschik 		if (val & BMCR_SPEED100)
867e66f840cSTristram Ha 			data |= PORT_FORCE_100_MBIT;
868e66f840cSTristram Ha 		else
869e66f840cSTristram Ha 			data &= ~PORT_FORCE_100_MBIT;
870ec4b94f9SMichael Grzeschik 		if (val & BMCR_FULLDPLX)
871e66f840cSTristram Ha 			data |= PORT_FORCE_FULL_DUPLEX;
872e66f840cSTristram Ha 		else
873e66f840cSTristram Ha 			data &= ~PORT_FORCE_FULL_DUPLEX;
874e66f840cSTristram Ha 		if (data != ctrl)
8759f73e112SMichael Grzeschik 			ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data);
8769f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
877e66f840cSTristram Ha 		data = restart;
878ec4b94f9SMichael Grzeschik 		if (val & KSZ886X_BMCR_DISABLE_LED)
879e66f840cSTristram Ha 			data |= PORT_LED_OFF;
880e66f840cSTristram Ha 		else
881e66f840cSTristram Ha 			data &= ~PORT_LED_OFF;
882ec4b94f9SMichael Grzeschik 		if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
883e66f840cSTristram Ha 			data |= PORT_TX_DISABLE;
884e66f840cSTristram Ha 		else
885e66f840cSTristram Ha 			data &= ~PORT_TX_DISABLE;
886ec4b94f9SMichael Grzeschik 		if (val & BMCR_ANRESTART)
887e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_RESTART;
888e66f840cSTristram Ha 		else
889e66f840cSTristram Ha 			data &= ~(PORT_AUTO_NEG_RESTART);
890ec4b94f9SMichael Grzeschik 		if (val & BMCR_PDOWN)
891e66f840cSTristram Ha 			data |= PORT_POWER_DOWN;
892e66f840cSTristram Ha 		else
893e66f840cSTristram Ha 			data &= ~PORT_POWER_DOWN;
894ec4b94f9SMichael Grzeschik 		if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
895e66f840cSTristram Ha 			data |= PORT_AUTO_MDIX_DISABLE;
896e66f840cSTristram Ha 		else
897e66f840cSTristram Ha 			data &= ~PORT_AUTO_MDIX_DISABLE;
898ec4b94f9SMichael Grzeschik 		if (val & KSZ886X_BMCR_FORCE_MDI)
899e66f840cSTristram Ha 			data |= PORT_FORCE_MDIX;
900e66f840cSTristram Ha 		else
901e66f840cSTristram Ha 			data &= ~PORT_FORCE_MDIX;
902ec4b94f9SMichael Grzeschik 		if (val & BMCR_LOOPBACK)
903e66f840cSTristram Ha 			data |= PORT_PHY_LOOPBACK;
904e66f840cSTristram Ha 		else
905e66f840cSTristram Ha 			data &= ~PORT_PHY_LOOPBACK;
906e66f840cSTristram Ha 		if (data != restart)
9079f73e112SMichael Grzeschik 			ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL], data);
908e66f840cSTristram Ha 		break;
909ec4b94f9SMichael Grzeschik 	case MII_ADVERTISE:
9109f73e112SMichael Grzeschik 		ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
911e66f840cSTristram Ha 		data = ctrl;
912e66f840cSTristram Ha 		data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
913e66f840cSTristram Ha 			  PORT_AUTO_NEG_100BTX_FD |
914e66f840cSTristram Ha 			  PORT_AUTO_NEG_100BTX |
915e66f840cSTristram Ha 			  PORT_AUTO_NEG_10BT_FD |
916e66f840cSTristram Ha 			  PORT_AUTO_NEG_10BT);
917ec4b94f9SMichael Grzeschik 		if (val & ADVERTISE_PAUSE_CAP)
918e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_SYM_PAUSE;
919ec4b94f9SMichael Grzeschik 		if (val & ADVERTISE_100FULL)
920e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_100BTX_FD;
921ec4b94f9SMichael Grzeschik 		if (val & ADVERTISE_100HALF)
922e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_100BTX;
923ec4b94f9SMichael Grzeschik 		if (val & ADVERTISE_10FULL)
924e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_10BT_FD;
925ec4b94f9SMichael Grzeschik 		if (val & ADVERTISE_10HALF)
926e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_10BT;
927e66f840cSTristram Ha 		if (data != ctrl)
9289f73e112SMichael Grzeschik 			ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
929e66f840cSTristram Ha 		break;
930e66f840cSTristram Ha 	default:
931e66f840cSTristram Ha 		break;
932e66f840cSTristram Ha 	}
933e66f840cSTristram Ha }
934e66f840cSTristram Ha 
9354b5baca0SMichael Grzeschik static enum dsa_tag_protocol ksz8_get_tag_protocol(struct dsa_switch *ds,
9364d776482SFlorian Fainelli 						   int port,
9374d776482SFlorian Fainelli 						   enum dsa_tag_protocol mp)
938e66f840cSTristram Ha {
9394b20a07eSOleksij Rempel 	struct ksz_device *dev = ds->priv;
9404b20a07eSOleksij Rempel 
9414b20a07eSOleksij Rempel 	/* ksz88x3 uses the same tag schema as KSZ9893 */
9424b20a07eSOleksij Rempel 	return ksz_is_ksz88x3(dev) ?
9434b20a07eSOleksij Rempel 		DSA_TAG_PROTO_KSZ9893 : DSA_TAG_PROTO_KSZ8795;
944e66f840cSTristram Ha }
945e66f840cSTristram Ha 
9464b5baca0SMichael Grzeschik static void ksz8_get_strings(struct dsa_switch *ds, int port,
947e66f840cSTristram Ha 			     u32 stringset, uint8_t *buf)
948e66f840cSTristram Ha {
94965fe1acfSMichael Grzeschik 	struct ksz_device *dev = ds->priv;
950e66f840cSTristram Ha 	int i;
951e66f840cSTristram Ha 
95265fe1acfSMichael Grzeschik 	for (i = 0; i < dev->mib_cnt; i++) {
9534b20a07eSOleksij Rempel 		memcpy(buf + i * ETH_GSTRING_LEN,
9544b20a07eSOleksij Rempel 		       dev->mib_names[i].string, ETH_GSTRING_LEN);
955e66f840cSTristram Ha 	}
956e66f840cSTristram Ha }
957e66f840cSTristram Ha 
9584b5baca0SMichael Grzeschik static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
959e66f840cSTristram Ha {
960e66f840cSTristram Ha 	u8 data;
961e66f840cSTristram Ha 
962e66f840cSTristram Ha 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
963e66f840cSTristram Ha 	data &= ~PORT_VLAN_MEMBERSHIP;
964e66f840cSTristram Ha 	data |= (member & dev->port_mask);
965e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
966e66f840cSTristram Ha 	dev->ports[port].member = member;
967e66f840cSTristram Ha }
968e66f840cSTristram Ha 
9694b5baca0SMichael Grzeschik static void ksz8_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
970e66f840cSTristram Ha {
971e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
972e66f840cSTristram Ha 	int forward = dev->member;
973e66f840cSTristram Ha 	struct ksz_port *p;
974e66f840cSTristram Ha 	int member = -1;
975e66f840cSTristram Ha 	u8 data;
976e66f840cSTristram Ha 
977e66f840cSTristram Ha 	p = &dev->ports[port];
978e66f840cSTristram Ha 
979e66f840cSTristram Ha 	ksz_pread8(dev, port, P_STP_CTRL, &data);
980e66f840cSTristram Ha 	data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
981e66f840cSTristram Ha 
982e66f840cSTristram Ha 	switch (state) {
983e66f840cSTristram Ha 	case BR_STATE_DISABLED:
984e66f840cSTristram Ha 		data |= PORT_LEARN_DISABLE;
9854ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt)
986e66f840cSTristram Ha 			member = 0;
987e66f840cSTristram Ha 		break;
988e66f840cSTristram Ha 	case BR_STATE_LISTENING:
989e66f840cSTristram Ha 		data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
9904ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt &&
991e66f840cSTristram Ha 		    p->stp_state == BR_STATE_DISABLED)
992e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
993e66f840cSTristram Ha 		break;
994e66f840cSTristram Ha 	case BR_STATE_LEARNING:
995e66f840cSTristram Ha 		data |= PORT_RX_ENABLE;
996e66f840cSTristram Ha 		break;
997e66f840cSTristram Ha 	case BR_STATE_FORWARDING:
998e66f840cSTristram Ha 		data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
999e66f840cSTristram Ha 
1000e66f840cSTristram Ha 		/* This function is also used internally. */
1001e66f840cSTristram Ha 		if (port == dev->cpu_port)
1002e66f840cSTristram Ha 			break;
1003e66f840cSTristram Ha 
1004e66f840cSTristram Ha 		/* Port is a member of a bridge. */
1005e66f840cSTristram Ha 		if (dev->br_member & BIT(port)) {
1006e66f840cSTristram Ha 			dev->member |= BIT(port);
1007e66f840cSTristram Ha 			member = dev->member;
1008e66f840cSTristram Ha 		} else {
1009e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
1010e66f840cSTristram Ha 		}
1011e66f840cSTristram Ha 		break;
1012e66f840cSTristram Ha 	case BR_STATE_BLOCKING:
1013e66f840cSTristram Ha 		data |= PORT_LEARN_DISABLE;
10144ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt &&
1015e66f840cSTristram Ha 		    p->stp_state == BR_STATE_DISABLED)
1016e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
1017e66f840cSTristram Ha 		break;
1018e66f840cSTristram Ha 	default:
1019e66f840cSTristram Ha 		dev_err(ds->dev, "invalid STP state: %d\n", state);
1020e66f840cSTristram Ha 		return;
1021e66f840cSTristram Ha 	}
1022e66f840cSTristram Ha 
1023e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_STP_CTRL, data);
1024e66f840cSTristram Ha 	p->stp_state = state;
1025e66f840cSTristram Ha 	/* Port membership may share register with STP state. */
1026e66f840cSTristram Ha 	if (member >= 0 && member != p->member)
10274b5baca0SMichael Grzeschik 		ksz8_cfg_port_member(dev, port, (u8)member);
1028e66f840cSTristram Ha 
1029e66f840cSTristram Ha 	/* Check if forwarding needs to be updated. */
1030e66f840cSTristram Ha 	if (state != BR_STATE_FORWARDING) {
1031e66f840cSTristram Ha 		if (dev->br_member & BIT(port))
1032e66f840cSTristram Ha 			dev->member &= ~BIT(port);
1033e66f840cSTristram Ha 	}
1034e66f840cSTristram Ha 
1035e66f840cSTristram Ha 	/* When topology has changed the function ksz_update_port_member
1036e66f840cSTristram Ha 	 * should be called to modify port forwarding behavior.
1037e66f840cSTristram Ha 	 */
1038e66f840cSTristram Ha 	if (forward != dev->member)
1039e66f840cSTristram Ha 		ksz_update_port_member(dev, port);
1040e66f840cSTristram Ha }
1041e66f840cSTristram Ha 
10424b5baca0SMichael Grzeschik static void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
1043e66f840cSTristram Ha {
1044241ed719SMichael Grzeschik 	u8 learn[DSA_MAX_PORTS];
1045e66f840cSTristram Ha 	int first, index, cnt;
1046e66f840cSTristram Ha 	struct ksz_port *p;
1047e66f840cSTristram Ha 
1048241ed719SMichael Grzeschik 	if ((uint)port < dev->port_cnt) {
1049e66f840cSTristram Ha 		first = port;
1050e66f840cSTristram Ha 		cnt = port + 1;
1051e66f840cSTristram Ha 	} else {
1052e66f840cSTristram Ha 		/* Flush all ports. */
1053e66f840cSTristram Ha 		first = 0;
1054c9f4633bSMichael Grzeschik 		cnt = dev->port_cnt;
1055e66f840cSTristram Ha 	}
1056e66f840cSTristram Ha 	for (index = first; index < cnt; index++) {
1057e66f840cSTristram Ha 		p = &dev->ports[index];
1058e66f840cSTristram Ha 		if (!p->on)
1059e66f840cSTristram Ha 			continue;
1060e66f840cSTristram Ha 		ksz_pread8(dev, index, P_STP_CTRL, &learn[index]);
1061e66f840cSTristram Ha 		if (!(learn[index] & PORT_LEARN_DISABLE))
1062e66f840cSTristram Ha 			ksz_pwrite8(dev, index, P_STP_CTRL,
1063e66f840cSTristram Ha 				    learn[index] | PORT_LEARN_DISABLE);
1064e66f840cSTristram Ha 	}
1065e66f840cSTristram Ha 	ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
1066e66f840cSTristram Ha 	for (index = first; index < cnt; index++) {
1067e66f840cSTristram Ha 		p = &dev->ports[index];
1068e66f840cSTristram Ha 		if (!p->on)
1069e66f840cSTristram Ha 			continue;
1070e66f840cSTristram Ha 		if (!(learn[index] & PORT_LEARN_DISABLE))
1071e66f840cSTristram Ha 			ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]);
1072e66f840cSTristram Ha 	}
1073e66f840cSTristram Ha }
1074e66f840cSTristram Ha 
10754b5baca0SMichael Grzeschik static int ksz8_port_vlan_filtering(struct dsa_switch *ds, int port, bool flag,
107689153ed6SVladimir Oltean 				    struct netlink_ext_ack *extack)
1077e66f840cSTristram Ha {
1078e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1079e66f840cSTristram Ha 
10804b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev))
10814b20a07eSOleksij Rempel 		return -ENOTSUPP;
10824b20a07eSOleksij Rempel 
1083e66f840cSTristram Ha 	ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
1084e66f840cSTristram Ha 
1085e66f840cSTristram Ha 	return 0;
1086e66f840cSTristram Ha }
1087e66f840cSTristram Ha 
10884b5baca0SMichael Grzeschik static int ksz8_port_vlan_add(struct dsa_switch *ds, int port,
108931046a5fSVladimir Oltean 			      const struct switchdev_obj_port_vlan *vlan,
109031046a5fSVladimir Oltean 			      struct netlink_ext_ack *extack)
1091e66f840cSTristram Ha {
1092e66f840cSTristram Ha 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1093e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1094b7a9e0daSVladimir Oltean 	u16 data, new_pvid = 0;
1095e66f840cSTristram Ha 	u8 fid, member, valid;
1096e66f840cSTristram Ha 
10974b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev))
10984b20a07eSOleksij Rempel 		return -ENOTSUPP;
10994b20a07eSOleksij Rempel 
1100e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1101e66f840cSTristram Ha 
11024b5baca0SMichael Grzeschik 	ksz8_r_vlan_table(dev, vlan->vid, &data);
11039f73e112SMichael Grzeschik 	ksz8_from_vlan(dev, data, &fid, &member, &valid);
1104e66f840cSTristram Ha 
1105e66f840cSTristram Ha 	/* First time to setup the VLAN entry. */
1106e66f840cSTristram Ha 	if (!valid) {
1107e66f840cSTristram Ha 		/* Need to find a way to map VID to FID. */
1108e66f840cSTristram Ha 		fid = 1;
1109e66f840cSTristram Ha 		valid = 1;
1110e66f840cSTristram Ha 	}
1111e66f840cSTristram Ha 	member |= BIT(port);
1112e66f840cSTristram Ha 
11139f73e112SMichael Grzeschik 	ksz8_to_vlan(dev, fid, member, valid, &data);
11144b5baca0SMichael Grzeschik 	ksz8_w_vlan_table(dev, vlan->vid, data);
1115e66f840cSTristram Ha 
1116e66f840cSTristram Ha 	/* change PVID */
1117e66f840cSTristram Ha 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
1118b7a9e0daSVladimir Oltean 		new_pvid = vlan->vid;
1119e66f840cSTristram Ha 
1120e66f840cSTristram Ha 	if (new_pvid) {
1121b7a9e0daSVladimir Oltean 		u16 vid;
1122b7a9e0daSVladimir Oltean 
1123e66f840cSTristram Ha 		ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
1124e66f840cSTristram Ha 		vid &= 0xfff;
1125e66f840cSTristram Ha 		vid |= new_pvid;
1126e66f840cSTristram Ha 		ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
1127e66f840cSTristram Ha 	}
11281958d581SVladimir Oltean 
11291958d581SVladimir Oltean 	return 0;
1130e66f840cSTristram Ha }
1131e66f840cSTristram Ha 
11324b5baca0SMichael Grzeschik static int ksz8_port_vlan_del(struct dsa_switch *ds, int port,
1133e66f840cSTristram Ha 			      const struct switchdev_obj_port_vlan *vlan)
1134e66f840cSTristram Ha {
1135e66f840cSTristram Ha 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1136e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1137b7a9e0daSVladimir Oltean 	u16 data, pvid, new_pvid = 0;
1138e66f840cSTristram Ha 	u8 fid, member, valid;
1139e66f840cSTristram Ha 
11404b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev))
11414b20a07eSOleksij Rempel 		return -ENOTSUPP;
11424b20a07eSOleksij Rempel 
1143e66f840cSTristram Ha 	ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
1144e66f840cSTristram Ha 	pvid = pvid & 0xFFF;
1145e66f840cSTristram Ha 
1146e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1147e66f840cSTristram Ha 
11484b5baca0SMichael Grzeschik 	ksz8_r_vlan_table(dev, vlan->vid, &data);
11499f73e112SMichael Grzeschik 	ksz8_from_vlan(dev, data, &fid, &member, &valid);
1150e66f840cSTristram Ha 
1151e66f840cSTristram Ha 	member &= ~BIT(port);
1152e66f840cSTristram Ha 
1153e66f840cSTristram Ha 	/* Invalidate the entry if no more member. */
1154e66f840cSTristram Ha 	if (!member) {
1155e66f840cSTristram Ha 		fid = 0;
1156e66f840cSTristram Ha 		valid = 0;
1157e66f840cSTristram Ha 	}
1158e66f840cSTristram Ha 
1159b7a9e0daSVladimir Oltean 	if (pvid == vlan->vid)
1160e66f840cSTristram Ha 		new_pvid = 1;
1161e66f840cSTristram Ha 
11629f73e112SMichael Grzeschik 	ksz8_to_vlan(dev, fid, member, valid, &data);
11634b5baca0SMichael Grzeschik 	ksz8_w_vlan_table(dev, vlan->vid, data);
1164e66f840cSTristram Ha 
1165e66f840cSTristram Ha 	if (new_pvid != pvid)
1166e66f840cSTristram Ha 		ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid);
1167e66f840cSTristram Ha 
1168e66f840cSTristram Ha 	return 0;
1169e66f840cSTristram Ha }
1170e66f840cSTristram Ha 
11714b5baca0SMichael Grzeschik static int ksz8_port_mirror_add(struct dsa_switch *ds, int port,
1172e66f840cSTristram Ha 				struct dsa_mall_mirror_tc_entry *mirror,
1173e66f840cSTristram Ha 				bool ingress)
1174e66f840cSTristram Ha {
1175e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1176e66f840cSTristram Ha 
1177e66f840cSTristram Ha 	if (ingress) {
1178e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1179e66f840cSTristram Ha 		dev->mirror_rx |= BIT(port);
1180e66f840cSTristram Ha 	} else {
1181e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1182e66f840cSTristram Ha 		dev->mirror_tx |= BIT(port);
1183e66f840cSTristram Ha 	}
1184e66f840cSTristram Ha 
1185e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1186e66f840cSTristram Ha 
1187e66f840cSTristram Ha 	/* configure mirror port */
1188e66f840cSTristram Ha 	if (dev->mirror_rx || dev->mirror_tx)
1189e66f840cSTristram Ha 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1190e66f840cSTristram Ha 			     PORT_MIRROR_SNIFFER, true);
1191e66f840cSTristram Ha 
1192e66f840cSTristram Ha 	return 0;
1193e66f840cSTristram Ha }
1194e66f840cSTristram Ha 
11954b5baca0SMichael Grzeschik static void ksz8_port_mirror_del(struct dsa_switch *ds, int port,
1196e66f840cSTristram Ha 				 struct dsa_mall_mirror_tc_entry *mirror)
1197e66f840cSTristram Ha {
1198e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1199e66f840cSTristram Ha 	u8 data;
1200e66f840cSTristram Ha 
1201e66f840cSTristram Ha 	if (mirror->ingress) {
1202e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1203e66f840cSTristram Ha 		dev->mirror_rx &= ~BIT(port);
1204e66f840cSTristram Ha 	} else {
1205e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1206e66f840cSTristram Ha 		dev->mirror_tx &= ~BIT(port);
1207e66f840cSTristram Ha 	}
1208e66f840cSTristram Ha 
1209e66f840cSTristram Ha 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1210e66f840cSTristram Ha 
1211e66f840cSTristram Ha 	if (!dev->mirror_rx && !dev->mirror_tx)
1212e66f840cSTristram Ha 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1213e66f840cSTristram Ha 			     PORT_MIRROR_SNIFFER, false);
1214e66f840cSTristram Ha }
1215e66f840cSTristram Ha 
1216c2ac4d2aSMichael Grzeschik static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
1217e66f840cSTristram Ha {
1218e66f840cSTristram Ha 	struct ksz_port *p = &dev->ports[port];
1219c2ac4d2aSMichael Grzeschik 	u8 data8;
1220e66f840cSTristram Ha 
1221edecfa98SHelmut Grohne 	if (!p->interface && dev->compat_interface) {
1222edecfa98SHelmut Grohne 		dev_warn(dev->dev,
1223edecfa98SHelmut Grohne 			 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1224edecfa98SHelmut Grohne 			 "Please update your device tree.\n",
1225edecfa98SHelmut Grohne 			 port);
1226edecfa98SHelmut Grohne 		p->interface = dev->compat_interface;
1227edecfa98SHelmut Grohne 	}
1228edecfa98SHelmut Grohne 
1229e66f840cSTristram Ha 	/* Configure MII interface for proper network communication. */
1230e66f840cSTristram Ha 	ksz_read8(dev, REG_PORT_5_CTRL_6, &data8);
1231e66f840cSTristram Ha 	data8 &= ~PORT_INTERFACE_TYPE;
1232e66f840cSTristram Ha 	data8 &= ~PORT_GMII_1GPS_MODE;
1233edecfa98SHelmut Grohne 	switch (p->interface) {
1234e66f840cSTristram Ha 	case PHY_INTERFACE_MODE_MII:
1235e66f840cSTristram Ha 		p->phydev.speed = SPEED_100;
1236e66f840cSTristram Ha 		break;
1237e66f840cSTristram Ha 	case PHY_INTERFACE_MODE_RMII:
1238e66f840cSTristram Ha 		data8 |= PORT_INTERFACE_RMII;
1239e66f840cSTristram Ha 		p->phydev.speed = SPEED_100;
1240e66f840cSTristram Ha 		break;
1241e66f840cSTristram Ha 	case PHY_INTERFACE_MODE_GMII:
1242e66f840cSTristram Ha 		data8 |= PORT_GMII_1GPS_MODE;
1243e66f840cSTristram Ha 		data8 |= PORT_INTERFACE_GMII;
1244e66f840cSTristram Ha 		p->phydev.speed = SPEED_1000;
1245e66f840cSTristram Ha 		break;
1246e66f840cSTristram Ha 	default:
1247e66f840cSTristram Ha 		data8 &= ~PORT_RGMII_ID_IN_ENABLE;
1248e66f840cSTristram Ha 		data8 &= ~PORT_RGMII_ID_OUT_ENABLE;
1249edecfa98SHelmut Grohne 		if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1250edecfa98SHelmut Grohne 		    p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1251e66f840cSTristram Ha 			data8 |= PORT_RGMII_ID_IN_ENABLE;
1252edecfa98SHelmut Grohne 		if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1253edecfa98SHelmut Grohne 		    p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1254e66f840cSTristram Ha 			data8 |= PORT_RGMII_ID_OUT_ENABLE;
1255e66f840cSTristram Ha 		data8 |= PORT_GMII_1GPS_MODE;
1256e66f840cSTristram Ha 		data8 |= PORT_INTERFACE_RGMII;
1257e66f840cSTristram Ha 		p->phydev.speed = SPEED_1000;
1258e66f840cSTristram Ha 		break;
1259e66f840cSTristram Ha 	}
1260e66f840cSTristram Ha 	ksz_write8(dev, REG_PORT_5_CTRL_6, data8);
1261e66f840cSTristram Ha 	p->phydev.duplex = 1;
1262c2ac4d2aSMichael Grzeschik }
1263c2ac4d2aSMichael Grzeschik 
1264c2ac4d2aSMichael Grzeschik static void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1265c2ac4d2aSMichael Grzeschik {
1266c2ac4d2aSMichael Grzeschik 	struct ksz_port *p = &dev->ports[port];
12679f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
12689f73e112SMichael Grzeschik 	const u32 *masks;
1269c2ac4d2aSMichael Grzeschik 	u8 member;
1270c2ac4d2aSMichael Grzeschik 
12719f73e112SMichael Grzeschik 	masks = ksz8->masks;
12729f73e112SMichael Grzeschik 
1273c2ac4d2aSMichael Grzeschik 	/* enable broadcast storm limit */
1274c2ac4d2aSMichael Grzeschik 	ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1275c2ac4d2aSMichael Grzeschik 
12764b20a07eSOleksij Rempel 	if (!ksz_is_ksz88x3(dev))
1277c2ac4d2aSMichael Grzeschik 		ksz8795_set_prio_queue(dev, port, 4);
1278c2ac4d2aSMichael Grzeschik 
1279c2ac4d2aSMichael Grzeschik 	/* disable DiffServ priority */
1280c2ac4d2aSMichael Grzeschik 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
1281c2ac4d2aSMichael Grzeschik 
1282c2ac4d2aSMichael Grzeschik 	/* replace priority */
12839f73e112SMichael Grzeschik 	ksz_port_cfg(dev, port, P_802_1P_CTRL,
12849f73e112SMichael Grzeschik 		     masks[PORT_802_1P_REMAPPING], false);
1285c2ac4d2aSMichael Grzeschik 
1286c2ac4d2aSMichael Grzeschik 	/* enable 802.1p priority */
1287c2ac4d2aSMichael Grzeschik 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
1288c2ac4d2aSMichael Grzeschik 
1289c2ac4d2aSMichael Grzeschik 	if (cpu_port) {
12904b20a07eSOleksij Rempel 		if (!ksz_is_ksz88x3(dev))
1291c2ac4d2aSMichael Grzeschik 			ksz8795_cpu_interface_select(dev, port);
1292e66f840cSTristram Ha 
1293e66f840cSTristram Ha 		member = dev->port_mask;
1294e66f840cSTristram Ha 	} else {
1295e66f840cSTristram Ha 		member = dev->host_mask | p->vid_member;
1296e66f840cSTristram Ha 	}
12974b5baca0SMichael Grzeschik 	ksz8_cfg_port_member(dev, port, member);
1298e66f840cSTristram Ha }
1299e66f840cSTristram Ha 
13004b5baca0SMichael Grzeschik static void ksz8_config_cpu_port(struct dsa_switch *ds)
1301e66f840cSTristram Ha {
1302e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
13039f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
13049f73e112SMichael Grzeschik 	const u8 *regs = ksz8->regs;
1305e66f840cSTristram Ha 	struct ksz_port *p;
13069f73e112SMichael Grzeschik 	const u32 *masks;
1307e66f840cSTristram Ha 	u8 remote;
1308e66f840cSTristram Ha 	int i;
1309e66f840cSTristram Ha 
13109f73e112SMichael Grzeschik 	masks = ksz8->masks;
13119f73e112SMichael Grzeschik 
1312e66f840cSTristram Ha 	/* Switch marks the maximum frame with extra byte as oversize. */
1313e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true);
13149f73e112SMichael Grzeschik 	ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
1315e66f840cSTristram Ha 
1316e66f840cSTristram Ha 	p = &dev->ports[dev->cpu_port];
1317e66f840cSTristram Ha 	p->vid_member = dev->port_mask;
1318e66f840cSTristram Ha 	p->on = 1;
1319e66f840cSTristram Ha 
13204b5baca0SMichael Grzeschik 	ksz8_port_setup(dev, dev->cpu_port, true);
1321e66f840cSTristram Ha 	dev->member = dev->host_mask;
1322e66f840cSTristram Ha 
13234ce2a984SMichael Grzeschik 	for (i = 0; i < dev->phy_port_cnt; i++) {
1324e66f840cSTristram Ha 		p = &dev->ports[i];
1325e66f840cSTristram Ha 
1326e66f840cSTristram Ha 		/* Initialize to non-zero so that ksz_cfg_port_member() will
1327e66f840cSTristram Ha 		 * be called.
1328e66f840cSTristram Ha 		 */
1329e66f840cSTristram Ha 		p->vid_member = BIT(i);
1330e66f840cSTristram Ha 		p->member = dev->port_mask;
13314b5baca0SMichael Grzeschik 		ksz8_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1332e66f840cSTristram Ha 
1333e66f840cSTristram Ha 		/* Last port may be disabled. */
13344ce2a984SMichael Grzeschik 		if (i == dev->phy_port_cnt)
1335e66f840cSTristram Ha 			break;
1336e66f840cSTristram Ha 		p->on = 1;
1337e66f840cSTristram Ha 		p->phy = 1;
1338e66f840cSTristram Ha 	}
1339e66f840cSTristram Ha 	for (i = 0; i < dev->phy_port_cnt; i++) {
1340e66f840cSTristram Ha 		p = &dev->ports[i];
1341e66f840cSTristram Ha 		if (!p->on)
1342e66f840cSTristram Ha 			continue;
13434b20a07eSOleksij Rempel 		if (!ksz_is_ksz88x3(dev)) {
13449f73e112SMichael Grzeschik 			ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
1345e66f840cSTristram Ha 			if (remote & PORT_FIBER_MODE)
1346e66f840cSTristram Ha 				p->fiber = 1;
13474b20a07eSOleksij Rempel 		}
1348e66f840cSTristram Ha 		if (p->fiber)
1349e66f840cSTristram Ha 			ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1350e66f840cSTristram Ha 				     true);
1351e66f840cSTristram Ha 		else
1352e66f840cSTristram Ha 			ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1353e66f840cSTristram Ha 				     false);
1354e66f840cSTristram Ha 	}
1355e66f840cSTristram Ha }
1356e66f840cSTristram Ha 
13574b5baca0SMichael Grzeschik static int ksz8_setup(struct dsa_switch *ds)
1358e66f840cSTristram Ha {
1359e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1360e66f840cSTristram Ha 	struct alu_struct alu;
1361e66f840cSTristram Ha 	int i, ret = 0;
1362e66f840cSTristram Ha 
1363e66f840cSTristram Ha 	dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1364e66f840cSTristram Ha 				       dev->num_vlans, GFP_KERNEL);
1365e66f840cSTristram Ha 	if (!dev->vlan_cache)
1366e66f840cSTristram Ha 		return -ENOMEM;
1367e66f840cSTristram Ha 
13684b5baca0SMichael Grzeschik 	ret = ksz8_reset_switch(dev);
1369e66f840cSTristram Ha 	if (ret) {
1370e66f840cSTristram Ha 		dev_err(ds->dev, "failed to reset switch\n");
1371e66f840cSTristram Ha 		return ret;
1372e66f840cSTristram Ha 	}
1373e66f840cSTristram Ha 
1374e66f840cSTristram Ha 	ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1375e66f840cSTristram Ha 
1376e66f840cSTristram Ha 	/* Enable automatic fast aging when link changed detected. */
1377e66f840cSTristram Ha 	ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1378e66f840cSTristram Ha 
1379e66f840cSTristram Ha 	/* Enable aggressive back off algorithm in half duplex mode. */
1380e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1,
1381e66f840cSTristram Ha 			   SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1382e66f840cSTristram Ha 
1383e66f840cSTristram Ha 	/*
1384e66f840cSTristram Ha 	 * Make sure unicast VLAN boundary is set as default and
1385e66f840cSTristram Ha 	 * enable no excessive collision drop.
1386e66f840cSTristram Ha 	 */
1387e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2,
1388e66f840cSTristram Ha 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1389e66f840cSTristram Ha 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1390e66f840cSTristram Ha 
13914b5baca0SMichael Grzeschik 	ksz8_config_cpu_port(ds);
1392e66f840cSTristram Ha 
1393e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true);
1394e66f840cSTristram Ha 
1395e66f840cSTristram Ha 	ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1396e66f840cSTristram Ha 
1397e66f840cSTristram Ha 	ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1398e66f840cSTristram Ha 
1399e66f840cSTristram Ha 	/* set broadcast storm protection 10% rate */
1400e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL,
1401e66f840cSTristram Ha 			   BROADCAST_STORM_RATE,
1402e66f840cSTristram Ha 			   (BROADCAST_STORM_VALUE *
1403e66f840cSTristram Ha 			   BROADCAST_STORM_PROT_RATE) / 100);
1404e66f840cSTristram Ha 
140502ffbb02SMichael Grzeschik 	for (i = 0; i < (dev->num_vlans / 4); i++)
14064b5baca0SMichael Grzeschik 		ksz8_r_vlan_entries(dev, i);
1407e66f840cSTristram Ha 
1408e66f840cSTristram Ha 	/* Setup STP address for STP operation. */
1409e66f840cSTristram Ha 	memset(&alu, 0, sizeof(alu));
1410e66f840cSTristram Ha 	ether_addr_copy(alu.mac, eth_stp_addr);
1411e66f840cSTristram Ha 	alu.is_static = true;
1412e66f840cSTristram Ha 	alu.is_override = true;
1413e66f840cSTristram Ha 	alu.port_forward = dev->host_mask;
1414e66f840cSTristram Ha 
14154b5baca0SMichael Grzeschik 	ksz8_w_sta_mac_table(dev, 0, &alu);
1416e66f840cSTristram Ha 
1417e66f840cSTristram Ha 	ksz_init_mib_timer(dev);
1418e66f840cSTristram Ha 
14190ee2af4eSVladimir Oltean 	ds->configure_vlan_while_not_filtering = false;
14200ee2af4eSVladimir Oltean 
1421e66f840cSTristram Ha 	return 0;
1422e66f840cSTristram Ha }
1423e66f840cSTristram Ha 
1424*2c709e0bSMichael Grzeschik static void ksz8_validate(struct dsa_switch *ds, int port,
1425*2c709e0bSMichael Grzeschik 			  unsigned long *supported,
1426*2c709e0bSMichael Grzeschik 			  struct phylink_link_state *state)
1427*2c709e0bSMichael Grzeschik {
1428*2c709e0bSMichael Grzeschik 	__ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1429*2c709e0bSMichael Grzeschik 	struct ksz_device *dev = ds->priv;
1430*2c709e0bSMichael Grzeschik 
1431*2c709e0bSMichael Grzeschik 	if (port == dev->cpu_port) {
1432*2c709e0bSMichael Grzeschik 		if (state->interface != PHY_INTERFACE_MODE_RMII &&
1433*2c709e0bSMichael Grzeschik 		    state->interface != PHY_INTERFACE_MODE_MII &&
1434*2c709e0bSMichael Grzeschik 		    state->interface != PHY_INTERFACE_MODE_NA)
1435*2c709e0bSMichael Grzeschik 			goto unsupported;
1436*2c709e0bSMichael Grzeschik 	} else {
1437*2c709e0bSMichael Grzeschik 		if (state->interface != PHY_INTERFACE_MODE_INTERNAL &&
1438*2c709e0bSMichael Grzeschik 		    state->interface != PHY_INTERFACE_MODE_NA)
1439*2c709e0bSMichael Grzeschik 			goto unsupported;
1440*2c709e0bSMichael Grzeschik 	}
1441*2c709e0bSMichael Grzeschik 
1442*2c709e0bSMichael Grzeschik 	/* Allow all the expected bits */
1443*2c709e0bSMichael Grzeschik 	phylink_set_port_modes(mask);
1444*2c709e0bSMichael Grzeschik 	phylink_set(mask, Autoneg);
1445*2c709e0bSMichael Grzeschik 
1446*2c709e0bSMichael Grzeschik 	/* Silicon Errata Sheet (DS80000830A):
1447*2c709e0bSMichael Grzeschik 	 * "Port 1 does not respond to received flow control PAUSE frames"
1448*2c709e0bSMichael Grzeschik 	 * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
1449*2c709e0bSMichael Grzeschik 	 * switches.
1450*2c709e0bSMichael Grzeschik 	 */
1451*2c709e0bSMichael Grzeschik 	if (!ksz_is_ksz88x3(dev) || port)
1452*2c709e0bSMichael Grzeschik 		phylink_set(mask, Pause);
1453*2c709e0bSMichael Grzeschik 
1454*2c709e0bSMichael Grzeschik 	/* Asym pause is not supported on KSZ8863 and KSZ8873 */
1455*2c709e0bSMichael Grzeschik 	if (!ksz_is_ksz88x3(dev))
1456*2c709e0bSMichael Grzeschik 		phylink_set(mask, Asym_Pause);
1457*2c709e0bSMichael Grzeschik 
1458*2c709e0bSMichael Grzeschik 	/* 10M and 100M are only supported */
1459*2c709e0bSMichael Grzeschik 	phylink_set(mask, 10baseT_Half);
1460*2c709e0bSMichael Grzeschik 	phylink_set(mask, 10baseT_Full);
1461*2c709e0bSMichael Grzeschik 	phylink_set(mask, 100baseT_Half);
1462*2c709e0bSMichael Grzeschik 	phylink_set(mask, 100baseT_Full);
1463*2c709e0bSMichael Grzeschik 
1464*2c709e0bSMichael Grzeschik 	bitmap_and(supported, supported, mask,
1465*2c709e0bSMichael Grzeschik 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1466*2c709e0bSMichael Grzeschik 	bitmap_and(state->advertising, state->advertising, mask,
1467*2c709e0bSMichael Grzeschik 		   __ETHTOOL_LINK_MODE_MASK_NBITS);
1468*2c709e0bSMichael Grzeschik 
1469*2c709e0bSMichael Grzeschik 	return;
1470*2c709e0bSMichael Grzeschik 
1471*2c709e0bSMichael Grzeschik unsupported:
1472*2c709e0bSMichael Grzeschik 	bitmap_zero(supported, __ETHTOOL_LINK_MODE_MASK_NBITS);
1473*2c709e0bSMichael Grzeschik 	dev_err(ds->dev, "Unsupported interface: %s, port: %d\n",
1474*2c709e0bSMichael Grzeschik 		phy_modes(state->interface), port);
1475*2c709e0bSMichael Grzeschik }
1476*2c709e0bSMichael Grzeschik 
14774b5baca0SMichael Grzeschik static const struct dsa_switch_ops ksz8_switch_ops = {
14784b5baca0SMichael Grzeschik 	.get_tag_protocol	= ksz8_get_tag_protocol,
14794b5baca0SMichael Grzeschik 	.setup			= ksz8_setup,
1480e66f840cSTristram Ha 	.phy_read		= ksz_phy_read16,
1481e66f840cSTristram Ha 	.phy_write		= ksz_phy_write16,
1482*2c709e0bSMichael Grzeschik 	.phylink_validate	= ksz8_validate,
1483143a102eSCodrin Ciubotariu 	.phylink_mac_link_down	= ksz_mac_link_down,
1484e66f840cSTristram Ha 	.port_enable		= ksz_enable_port,
14854b5baca0SMichael Grzeschik 	.get_strings		= ksz8_get_strings,
1486e66f840cSTristram Ha 	.get_ethtool_stats	= ksz_get_ethtool_stats,
1487e66f840cSTristram Ha 	.get_sset_count		= ksz_sset_count,
1488e66f840cSTristram Ha 	.port_bridge_join	= ksz_port_bridge_join,
1489e66f840cSTristram Ha 	.port_bridge_leave	= ksz_port_bridge_leave,
14904b5baca0SMichael Grzeschik 	.port_stp_state_set	= ksz8_port_stp_state_set,
1491e66f840cSTristram Ha 	.port_fast_age		= ksz_port_fast_age,
14924b5baca0SMichael Grzeschik 	.port_vlan_filtering	= ksz8_port_vlan_filtering,
14934b5baca0SMichael Grzeschik 	.port_vlan_add		= ksz8_port_vlan_add,
14944b5baca0SMichael Grzeschik 	.port_vlan_del		= ksz8_port_vlan_del,
1495e66f840cSTristram Ha 	.port_fdb_dump		= ksz_port_fdb_dump,
1496e66f840cSTristram Ha 	.port_mdb_add           = ksz_port_mdb_add,
1497e66f840cSTristram Ha 	.port_mdb_del           = ksz_port_mdb_del,
14984b5baca0SMichael Grzeschik 	.port_mirror_add	= ksz8_port_mirror_add,
14994b5baca0SMichael Grzeschik 	.port_mirror_del	= ksz8_port_mirror_del,
1500e66f840cSTristram Ha };
1501e66f840cSTristram Ha 
15024b5baca0SMichael Grzeschik static u32 ksz8_get_port_addr(int port, int offset)
1503e66f840cSTristram Ha {
1504e66f840cSTristram Ha 	return PORT_CTRL_ADDR(port, offset);
1505e66f840cSTristram Ha }
1506e66f840cSTristram Ha 
15074b5baca0SMichael Grzeschik static int ksz8_switch_detect(struct ksz_device *dev)
1508e66f840cSTristram Ha {
1509e66f840cSTristram Ha 	u8 id1, id2;
1510e66f840cSTristram Ha 	u16 id16;
1511e66f840cSTristram Ha 	int ret;
1512e66f840cSTristram Ha 
1513e66f840cSTristram Ha 	/* read chip id */
1514e66f840cSTristram Ha 	ret = ksz_read16(dev, REG_CHIP_ID0, &id16);
1515e66f840cSTristram Ha 	if (ret)
1516e66f840cSTristram Ha 		return ret;
1517e66f840cSTristram Ha 
1518e66f840cSTristram Ha 	id1 = id16 >> 8;
1519e66f840cSTristram Ha 	id2 = id16 & SW_CHIP_ID_M;
15204b20a07eSOleksij Rempel 
15214b20a07eSOleksij Rempel 	switch (id1) {
15224b20a07eSOleksij Rempel 	case KSZ87_FAMILY_ID:
15234b20a07eSOleksij Rempel 		if ((id2 != CHIP_ID_94 && id2 != CHIP_ID_95))
1524e66f840cSTristram Ha 			return -ENODEV;
1525e66f840cSTristram Ha 
1526e66f840cSTristram Ha 		if (id2 == CHIP_ID_95) {
1527e66f840cSTristram Ha 			u8 val;
1528e66f840cSTristram Ha 
1529e66f840cSTristram Ha 			id2 = 0x95;
15304b20a07eSOleksij Rempel 			ksz_read8(dev, REG_PORT_STATUS_0, &val);
1531e66f840cSTristram Ha 			if (val & PORT_FIBER_MODE)
1532e66f840cSTristram Ha 				id2 = 0x65;
1533e66f840cSTristram Ha 		} else if (id2 == CHIP_ID_94) {
1534e66f840cSTristram Ha 			id2 = 0x94;
1535e66f840cSTristram Ha 		}
15364b20a07eSOleksij Rempel 		break;
15374b20a07eSOleksij Rempel 	case KSZ88_FAMILY_ID:
15384b20a07eSOleksij Rempel 		if (id2 != CHIP_ID_63)
15394b20a07eSOleksij Rempel 			return -ENODEV;
15404b20a07eSOleksij Rempel 		break;
15414b20a07eSOleksij Rempel 	default:
15424b20a07eSOleksij Rempel 		dev_err(dev->dev, "invalid family id: %d\n", id1);
15434b20a07eSOleksij Rempel 		return -ENODEV;
15444b20a07eSOleksij Rempel 	}
1545e66f840cSTristram Ha 	id16 &= ~0xff;
1546e66f840cSTristram Ha 	id16 |= id2;
1547e66f840cSTristram Ha 	dev->chip_id = id16;
1548e66f840cSTristram Ha 
1549e66f840cSTristram Ha 	return 0;
1550e66f840cSTristram Ha }
1551e66f840cSTristram Ha 
1552e66f840cSTristram Ha struct ksz_chip_data {
1553e66f840cSTristram Ha 	u16 chip_id;
1554e66f840cSTristram Ha 	const char *dev_name;
1555e66f840cSTristram Ha 	int num_vlans;
1556e66f840cSTristram Ha 	int num_alus;
1557e66f840cSTristram Ha 	int num_statics;
1558e66f840cSTristram Ha 	int cpu_ports;
1559e66f840cSTristram Ha 	int port_cnt;
1560e66f840cSTristram Ha };
1561e66f840cSTristram Ha 
15624b5baca0SMichael Grzeschik static const struct ksz_chip_data ksz8_switch_chips[] = {
1563e66f840cSTristram Ha 	{
1564e66f840cSTristram Ha 		.chip_id = 0x8795,
1565e66f840cSTristram Ha 		.dev_name = "KSZ8795",
1566e66f840cSTristram Ha 		.num_vlans = 4096,
1567e66f840cSTristram Ha 		.num_alus = 0,
1568e66f840cSTristram Ha 		.num_statics = 8,
1569e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
157094374dd1SMichael Grzeschik 		.port_cnt = 5,		/* total cpu and user ports */
1571e66f840cSTristram Ha 	},
1572e66f840cSTristram Ha 	{
1573c369d7fcSMarek Vasut 		/*
1574c369d7fcSMarek Vasut 		 * WARNING
1575c369d7fcSMarek Vasut 		 * =======
1576c369d7fcSMarek Vasut 		 * KSZ8794 is similar to KSZ8795, except the port map
1577c369d7fcSMarek Vasut 		 * contains a gap between external and CPU ports, the
1578c369d7fcSMarek Vasut 		 * port map is NOT continuous. The per-port register
1579c369d7fcSMarek Vasut 		 * map is shifted accordingly too, i.e. registers at
1580c369d7fcSMarek Vasut 		 * offset 0x40 are NOT used on KSZ8794 and they ARE
1581c369d7fcSMarek Vasut 		 * used on KSZ8795 for external port 3.
1582c369d7fcSMarek Vasut 		 *           external  cpu
1583c369d7fcSMarek Vasut 		 * KSZ8794   0,1,2      4
1584c369d7fcSMarek Vasut 		 * KSZ8795   0,1,2,3    4
1585c369d7fcSMarek Vasut 		 * KSZ8765   0,1,2,3    4
1586c369d7fcSMarek Vasut 		 */
1587e66f840cSTristram Ha 		.chip_id = 0x8794,
1588e66f840cSTristram Ha 		.dev_name = "KSZ8794",
1589e66f840cSTristram Ha 		.num_vlans = 4096,
1590e66f840cSTristram Ha 		.num_alus = 0,
1591e66f840cSTristram Ha 		.num_statics = 8,
1592e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
159394374dd1SMichael Grzeschik 		.port_cnt = 4,		/* total cpu and user ports */
1594e66f840cSTristram Ha 	},
1595e66f840cSTristram Ha 	{
1596e66f840cSTristram Ha 		.chip_id = 0x8765,
1597e66f840cSTristram Ha 		.dev_name = "KSZ8765",
1598e66f840cSTristram Ha 		.num_vlans = 4096,
1599e66f840cSTristram Ha 		.num_alus = 0,
1600e66f840cSTristram Ha 		.num_statics = 8,
1601e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
160294374dd1SMichael Grzeschik 		.port_cnt = 5,		/* total cpu and user ports */
1603e66f840cSTristram Ha 	},
16044b20a07eSOleksij Rempel 	{
16054b20a07eSOleksij Rempel 		.chip_id = 0x8830,
16064b20a07eSOleksij Rempel 		.dev_name = "KSZ8863/KSZ8873",
16074b20a07eSOleksij Rempel 		.num_vlans = 16,
16084b20a07eSOleksij Rempel 		.num_alus = 0,
16094b20a07eSOleksij Rempel 		.num_statics = 8,
16104b20a07eSOleksij Rempel 		.cpu_ports = 0x4,	/* can be configured as cpu port */
16114b20a07eSOleksij Rempel 		.port_cnt = 3,
16124b20a07eSOleksij Rempel 	},
1613e66f840cSTristram Ha };
1614e66f840cSTristram Ha 
16154b5baca0SMichael Grzeschik static int ksz8_switch_init(struct ksz_device *dev)
1616e66f840cSTristram Ha {
16179f73e112SMichael Grzeschik 	struct ksz8 *ksz8 = dev->priv;
1618e66f840cSTristram Ha 	int i;
1619e66f840cSTristram Ha 
16204b5baca0SMichael Grzeschik 	dev->ds->ops = &ksz8_switch_ops;
1621e66f840cSTristram Ha 
16224b5baca0SMichael Grzeschik 	for (i = 0; i < ARRAY_SIZE(ksz8_switch_chips); i++) {
16234b5baca0SMichael Grzeschik 		const struct ksz_chip_data *chip = &ksz8_switch_chips[i];
1624e66f840cSTristram Ha 
1625e66f840cSTristram Ha 		if (dev->chip_id == chip->chip_id) {
1626e66f840cSTristram Ha 			dev->name = chip->dev_name;
1627e66f840cSTristram Ha 			dev->num_vlans = chip->num_vlans;
1628e66f840cSTristram Ha 			dev->num_alus = chip->num_alus;
1629e66f840cSTristram Ha 			dev->num_statics = chip->num_statics;
1630c369d7fcSMarek Vasut 			dev->port_cnt = fls(chip->cpu_ports);
1631c369d7fcSMarek Vasut 			dev->cpu_port = fls(chip->cpu_ports) - 1;
1632c369d7fcSMarek Vasut 			dev->phy_port_cnt = dev->port_cnt - 1;
1633e66f840cSTristram Ha 			dev->cpu_ports = chip->cpu_ports;
1634c369d7fcSMarek Vasut 			dev->host_mask = chip->cpu_ports;
1635c369d7fcSMarek Vasut 			dev->port_mask = (BIT(dev->phy_port_cnt) - 1) |
1636c369d7fcSMarek Vasut 					 chip->cpu_ports;
1637e66f840cSTristram Ha 			break;
1638e66f840cSTristram Ha 		}
1639e66f840cSTristram Ha 	}
1640e66f840cSTristram Ha 
1641e66f840cSTristram Ha 	/* no switch found */
1642e66f840cSTristram Ha 	if (!dev->cpu_ports)
1643e66f840cSTristram Ha 		return -ENODEV;
1644e66f840cSTristram Ha 
16454b20a07eSOleksij Rempel 	if (ksz_is_ksz88x3(dev)) {
16464b20a07eSOleksij Rempel 		ksz8->regs = ksz8863_regs;
16474b20a07eSOleksij Rempel 		ksz8->masks = ksz8863_masks;
16484b20a07eSOleksij Rempel 		ksz8->shifts = ksz8863_shifts;
16494b20a07eSOleksij Rempel 		dev->mib_cnt = ARRAY_SIZE(ksz88xx_mib_names);
16504b20a07eSOleksij Rempel 		dev->mib_names = ksz88xx_mib_names;
16514b20a07eSOleksij Rempel 	} else {
16529f73e112SMichael Grzeschik 		ksz8->regs = ksz8795_regs;
16539f73e112SMichael Grzeschik 		ksz8->masks = ksz8795_masks;
16549f73e112SMichael Grzeschik 		ksz8->shifts = ksz8795_shifts;
16554b20a07eSOleksij Rempel 		dev->mib_cnt = ARRAY_SIZE(ksz87xx_mib_names);
16564b20a07eSOleksij Rempel 		dev->mib_names = ksz87xx_mib_names;
16574b20a07eSOleksij Rempel 	}
16589f73e112SMichael Grzeschik 
16594b20a07eSOleksij Rempel 	dev->reg_mib_cnt = MIB_COUNTER_NUM;
1660e66f840cSTristram Ha 
1661c9f4633bSMichael Grzeschik 	dev->ports = devm_kzalloc(dev->dev,
1662c9f4633bSMichael Grzeschik 				  dev->port_cnt * sizeof(struct ksz_port),
1663e66f840cSTristram Ha 				  GFP_KERNEL);
1664e66f840cSTristram Ha 	if (!dev->ports)
1665e66f840cSTristram Ha 		return -ENOMEM;
1666c9f4633bSMichael Grzeschik 	for (i = 0; i < dev->port_cnt; i++) {
1667e66f840cSTristram Ha 		mutex_init(&dev->ports[i].mib.cnt_mutex);
1668e66f840cSTristram Ha 		dev->ports[i].mib.counters =
1669e66f840cSTristram Ha 			devm_kzalloc(dev->dev,
1670e66f840cSTristram Ha 				     sizeof(u64) *
167165fe1acfSMichael Grzeschik 				     (dev->mib_cnt + 1),
1672e66f840cSTristram Ha 				     GFP_KERNEL);
1673e66f840cSTristram Ha 		if (!dev->ports[i].mib.counters)
1674e66f840cSTristram Ha 			return -ENOMEM;
1675e66f840cSTristram Ha 	}
1676e66f840cSTristram Ha 
1677af199a1aSCodrin Ciubotariu 	/* set the real number of ports */
167894374dd1SMichael Grzeschik 	dev->ds->num_ports = dev->port_cnt;
1679af199a1aSCodrin Ciubotariu 
1680e66f840cSTristram Ha 	return 0;
1681e66f840cSTristram Ha }
1682e66f840cSTristram Ha 
16834b5baca0SMichael Grzeschik static void ksz8_switch_exit(struct ksz_device *dev)
1684e66f840cSTristram Ha {
16854b5baca0SMichael Grzeschik 	ksz8_reset_switch(dev);
1686e66f840cSTristram Ha }
1687e66f840cSTristram Ha 
16884b5baca0SMichael Grzeschik static const struct ksz_dev_ops ksz8_dev_ops = {
16894b5baca0SMichael Grzeschik 	.get_port_addr = ksz8_get_port_addr,
16904b5baca0SMichael Grzeschik 	.cfg_port_member = ksz8_cfg_port_member,
16914b5baca0SMichael Grzeschik 	.flush_dyn_mac_table = ksz8_flush_dyn_mac_table,
16924b5baca0SMichael Grzeschik 	.port_setup = ksz8_port_setup,
16934b5baca0SMichael Grzeschik 	.r_phy = ksz8_r_phy,
16944b5baca0SMichael Grzeschik 	.w_phy = ksz8_w_phy,
16954b5baca0SMichael Grzeschik 	.r_dyn_mac_table = ksz8_r_dyn_mac_table,
16964b5baca0SMichael Grzeschik 	.r_sta_mac_table = ksz8_r_sta_mac_table,
16974b5baca0SMichael Grzeschik 	.w_sta_mac_table = ksz8_w_sta_mac_table,
16984b5baca0SMichael Grzeschik 	.r_mib_cnt = ksz8_r_mib_cnt,
16994b5baca0SMichael Grzeschik 	.r_mib_pkt = ksz8_r_mib_pkt,
17004b5baca0SMichael Grzeschik 	.freeze_mib = ksz8_freeze_mib,
17014b5baca0SMichael Grzeschik 	.port_init_cnt = ksz8_port_init_cnt,
17024b5baca0SMichael Grzeschik 	.shutdown = ksz8_reset_switch,
17034b5baca0SMichael Grzeschik 	.detect = ksz8_switch_detect,
17044b5baca0SMichael Grzeschik 	.init = ksz8_switch_init,
17054b5baca0SMichael Grzeschik 	.exit = ksz8_switch_exit,
1706e66f840cSTristram Ha };
1707e66f840cSTristram Ha 
17084b5baca0SMichael Grzeschik int ksz8_switch_register(struct ksz_device *dev)
1709e66f840cSTristram Ha {
17104b5baca0SMichael Grzeschik 	return ksz_switch_register(dev, &ksz8_dev_ops);
1711e66f840cSTristram Ha }
17124b5baca0SMichael Grzeschik EXPORT_SYMBOL(ksz8_switch_register);
1713e66f840cSTristram Ha 
1714e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1715e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1716e66f840cSTristram Ha MODULE_LICENSE("GPL");
1717