xref: /openbmc/linux/drivers/net/dsa/microchip/ksz8795.c (revision b7a9e0da2d1c954b7c38217a29e002528b90d174)
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>
18e66f840cSTristram Ha #include <net/dsa.h>
19e66f840cSTristram Ha #include <net/switchdev.h>
20e66f840cSTristram Ha 
21e66f840cSTristram Ha #include "ksz_common.h"
22e66f840cSTristram Ha #include "ksz8795_reg.h"
23e66f840cSTristram Ha 
24e66f840cSTristram Ha static const struct {
25e66f840cSTristram Ha 	char string[ETH_GSTRING_LEN];
2665fe1acfSMichael Grzeschik } mib_names[] = {
27e66f840cSTristram Ha 	{ "rx_hi" },
28e66f840cSTristram Ha 	{ "rx_undersize" },
29e66f840cSTristram Ha 	{ "rx_fragments" },
30e66f840cSTristram Ha 	{ "rx_oversize" },
31e66f840cSTristram Ha 	{ "rx_jabbers" },
32e66f840cSTristram Ha 	{ "rx_symbol_err" },
33e66f840cSTristram Ha 	{ "rx_crc_err" },
34e66f840cSTristram Ha 	{ "rx_align_err" },
35e66f840cSTristram Ha 	{ "rx_mac_ctrl" },
36e66f840cSTristram Ha 	{ "rx_pause" },
37e66f840cSTristram Ha 	{ "rx_bcast" },
38e66f840cSTristram Ha 	{ "rx_mcast" },
39e66f840cSTristram Ha 	{ "rx_ucast" },
40e66f840cSTristram Ha 	{ "rx_64_or_less" },
41e66f840cSTristram Ha 	{ "rx_65_127" },
42e66f840cSTristram Ha 	{ "rx_128_255" },
43e66f840cSTristram Ha 	{ "rx_256_511" },
44e66f840cSTristram Ha 	{ "rx_512_1023" },
45e66f840cSTristram Ha 	{ "rx_1024_1522" },
46e66f840cSTristram Ha 	{ "rx_1523_2000" },
47e66f840cSTristram Ha 	{ "rx_2001" },
48e66f840cSTristram Ha 	{ "tx_hi" },
49e66f840cSTristram Ha 	{ "tx_late_col" },
50e66f840cSTristram Ha 	{ "tx_pause" },
51e66f840cSTristram Ha 	{ "tx_bcast" },
52e66f840cSTristram Ha 	{ "tx_mcast" },
53e66f840cSTristram Ha 	{ "tx_ucast" },
54e66f840cSTristram Ha 	{ "tx_deferred" },
55e66f840cSTristram Ha 	{ "tx_total_col" },
56e66f840cSTristram Ha 	{ "tx_exc_col" },
57e66f840cSTristram Ha 	{ "tx_single_col" },
58e66f840cSTristram Ha 	{ "tx_mult_col" },
59e66f840cSTristram Ha 	{ "rx_total" },
60e66f840cSTristram Ha 	{ "tx_total" },
61e66f840cSTristram Ha 	{ "rx_discards" },
62e66f840cSTristram Ha 	{ "tx_discards" },
63e66f840cSTristram Ha };
64e66f840cSTristram Ha 
65e66f840cSTristram Ha static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
66e66f840cSTristram Ha {
67e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
68e66f840cSTristram Ha }
69e66f840cSTristram Ha 
70e66f840cSTristram Ha static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
71e66f840cSTristram Ha 			 bool set)
72e66f840cSTristram Ha {
73e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
74e66f840cSTristram Ha 			   bits, set ? bits : 0);
75e66f840cSTristram Ha }
76e66f840cSTristram Ha 
77e66f840cSTristram Ha static int ksz8795_reset_switch(struct ksz_device *dev)
78e66f840cSTristram Ha {
79e66f840cSTristram Ha 	/* reset switch */
80e66f840cSTristram Ha 	ksz_write8(dev, REG_POWER_MANAGEMENT_1,
81e66f840cSTristram Ha 		   SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
82e66f840cSTristram Ha 	ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
83e66f840cSTristram Ha 
84e66f840cSTristram Ha 	return 0;
85e66f840cSTristram Ha }
86e66f840cSTristram Ha 
87e66f840cSTristram Ha static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue)
88e66f840cSTristram Ha {
89e66f840cSTristram Ha 	u8 hi, lo;
90e66f840cSTristram Ha 
91e66f840cSTristram Ha 	/* Number of queues can only be 1, 2, or 4. */
92e66f840cSTristram Ha 	switch (queue) {
93e66f840cSTristram Ha 	case 4:
94e66f840cSTristram Ha 	case 3:
95e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_4;
96e66f840cSTristram Ha 		break;
97e66f840cSTristram Ha 	case 2:
98e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_2;
99e66f840cSTristram Ha 		break;
100e66f840cSTristram Ha 	default:
101e66f840cSTristram Ha 		queue = PORT_QUEUE_SPLIT_1;
102e66f840cSTristram Ha 	}
103e66f840cSTristram Ha 	ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo);
104e66f840cSTristram Ha 	ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi);
105e66f840cSTristram Ha 	lo &= ~PORT_QUEUE_SPLIT_L;
106e66f840cSTristram Ha 	if (queue & PORT_QUEUE_SPLIT_2)
107e66f840cSTristram Ha 		lo |= PORT_QUEUE_SPLIT_L;
108e66f840cSTristram Ha 	hi &= ~PORT_QUEUE_SPLIT_H;
109e66f840cSTristram Ha 	if (queue & PORT_QUEUE_SPLIT_4)
110e66f840cSTristram Ha 		hi |= PORT_QUEUE_SPLIT_H;
111e66f840cSTristram Ha 	ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo);
112e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi);
113e66f840cSTristram Ha 
114e66f840cSTristram Ha 	/* Default is port based for egress rate limit. */
115e66f840cSTristram Ha 	if (queue != PORT_QUEUE_SPLIT_1)
116e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED,
117e66f840cSTristram Ha 			true);
118e66f840cSTristram Ha }
119e66f840cSTristram Ha 
120e66f840cSTristram Ha static void ksz8795_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
121e66f840cSTristram Ha 			      u64 *cnt)
122e66f840cSTristram Ha {
123e66f840cSTristram Ha 	u16 ctrl_addr;
124e66f840cSTristram Ha 	u32 data;
125e66f840cSTristram Ha 	u8 check;
126e66f840cSTristram Ha 	int loop;
127e66f840cSTristram Ha 
12831b62c78SMichael Grzeschik 	ctrl_addr = addr + dev->reg_mib_cnt * port;
129e66f840cSTristram Ha 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
130e66f840cSTristram Ha 
131e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
132e66f840cSTristram Ha 	ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
133e66f840cSTristram Ha 
134e66f840cSTristram Ha 	/* It is almost guaranteed to always read the valid bit because of
135e66f840cSTristram Ha 	 * slow SPI speed.
136e66f840cSTristram Ha 	 */
137e66f840cSTristram Ha 	for (loop = 2; loop > 0; loop--) {
138e66f840cSTristram Ha 		ksz_read8(dev, REG_IND_MIB_CHECK, &check);
139e66f840cSTristram Ha 
140e66f840cSTristram Ha 		if (check & MIB_COUNTER_VALID) {
141e66f840cSTristram Ha 			ksz_read32(dev, REG_IND_DATA_LO, &data);
142e66f840cSTristram Ha 			if (check & MIB_COUNTER_OVERFLOW)
143e66f840cSTristram Ha 				*cnt += MIB_COUNTER_VALUE + 1;
144e66f840cSTristram Ha 			*cnt += data & MIB_COUNTER_VALUE;
145e66f840cSTristram Ha 			break;
146e66f840cSTristram Ha 		}
147e66f840cSTristram Ha 	}
148e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
149e66f840cSTristram Ha }
150e66f840cSTristram Ha 
151e66f840cSTristram Ha static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
152e66f840cSTristram Ha 			      u64 *dropped, u64 *cnt)
153e66f840cSTristram Ha {
154e66f840cSTristram Ha 	u16 ctrl_addr;
155e66f840cSTristram Ha 	u32 data;
156e66f840cSTristram Ha 	u8 check;
157e66f840cSTristram Ha 	int loop;
158e66f840cSTristram Ha 
15931b62c78SMichael Grzeschik 	addr -= dev->reg_mib_cnt;
160e66f840cSTristram Ha 	ctrl_addr = (KS_MIB_TOTAL_RX_1 - KS_MIB_TOTAL_RX_0) * port;
161e66f840cSTristram Ha 	ctrl_addr += addr + KS_MIB_TOTAL_RX_0;
162e66f840cSTristram Ha 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
163e66f840cSTristram Ha 
164e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
165e66f840cSTristram Ha 	ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
166e66f840cSTristram Ha 
167e66f840cSTristram Ha 	/* It is almost guaranteed to always read the valid bit because of
168e66f840cSTristram Ha 	 * slow SPI speed.
169e66f840cSTristram Ha 	 */
170e66f840cSTristram Ha 	for (loop = 2; loop > 0; loop--) {
171e66f840cSTristram Ha 		ksz_read8(dev, REG_IND_MIB_CHECK, &check);
172e66f840cSTristram Ha 
173e66f840cSTristram Ha 		if (check & MIB_COUNTER_VALID) {
174e66f840cSTristram Ha 			ksz_read32(dev, REG_IND_DATA_LO, &data);
175e66f840cSTristram Ha 			if (addr < 2) {
176e66f840cSTristram Ha 				u64 total;
177e66f840cSTristram Ha 
178e66f840cSTristram Ha 				total = check & MIB_TOTAL_BYTES_H;
179e66f840cSTristram Ha 				total <<= 32;
180e66f840cSTristram Ha 				*cnt += total;
181e66f840cSTristram Ha 				*cnt += data;
182e66f840cSTristram Ha 				if (check & MIB_COUNTER_OVERFLOW) {
183e66f840cSTristram Ha 					total = MIB_TOTAL_BYTES_H + 1;
184e66f840cSTristram Ha 					total <<= 32;
185e66f840cSTristram Ha 					*cnt += total;
186e66f840cSTristram Ha 				}
187e66f840cSTristram Ha 			} else {
188e66f840cSTristram Ha 				if (check & MIB_COUNTER_OVERFLOW)
189e66f840cSTristram Ha 					*cnt += MIB_PACKET_DROPPED + 1;
190e66f840cSTristram Ha 				*cnt += data & MIB_PACKET_DROPPED;
191e66f840cSTristram Ha 			}
192e66f840cSTristram Ha 			break;
193e66f840cSTristram Ha 		}
194e66f840cSTristram Ha 	}
195e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
196e66f840cSTristram Ha }
197e66f840cSTristram Ha 
198e66f840cSTristram Ha static void ksz8795_freeze_mib(struct ksz_device *dev, int port, bool freeze)
199e66f840cSTristram Ha {
200e66f840cSTristram Ha 	/* enable the port for flush/freeze function */
201e66f840cSTristram Ha 	if (freeze)
202e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
203e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
204e66f840cSTristram Ha 
205e66f840cSTristram Ha 	/* disable the port after freeze is done */
206e66f840cSTristram Ha 	if (!freeze)
207e66f840cSTristram Ha 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
208e66f840cSTristram Ha }
209e66f840cSTristram Ha 
210e66f840cSTristram Ha static void ksz8795_port_init_cnt(struct ksz_device *dev, int port)
211e66f840cSTristram Ha {
212e66f840cSTristram Ha 	struct ksz_port_mib *mib = &dev->ports[port].mib;
213e66f840cSTristram Ha 
214e66f840cSTristram Ha 	/* flush all enabled port MIB counters */
215e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
216e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
217e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
218e66f840cSTristram Ha 
219e66f840cSTristram Ha 	mib->cnt_ptr = 0;
220e66f840cSTristram Ha 
221e66f840cSTristram Ha 	/* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
222e66f840cSTristram Ha 	while (mib->cnt_ptr < dev->reg_mib_cnt) {
223e66f840cSTristram Ha 		dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
224e66f840cSTristram Ha 					&mib->counters[mib->cnt_ptr]);
225e66f840cSTristram Ha 		++mib->cnt_ptr;
226e66f840cSTristram Ha 	}
227e66f840cSTristram Ha 
228e66f840cSTristram Ha 	/* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
229e66f840cSTristram Ha 	while (mib->cnt_ptr < dev->mib_cnt) {
230e66f840cSTristram Ha 		dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
231e66f840cSTristram Ha 					NULL, &mib->counters[mib->cnt_ptr]);
232e66f840cSTristram Ha 		++mib->cnt_ptr;
233e66f840cSTristram Ha 	}
234e66f840cSTristram Ha 	mib->cnt_ptr = 0;
235e66f840cSTristram Ha 	memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
236e66f840cSTristram Ha }
237e66f840cSTristram Ha 
238e66f840cSTristram Ha static void ksz8795_r_table(struct ksz_device *dev, int table, u16 addr,
239e66f840cSTristram Ha 			    u64 *data)
240e66f840cSTristram Ha {
241e66f840cSTristram Ha 	u16 ctrl_addr;
242e66f840cSTristram Ha 
243e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
244e66f840cSTristram Ha 
245e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
246e66f840cSTristram Ha 	ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
247e66f840cSTristram Ha 	ksz_read64(dev, REG_IND_DATA_HI, data);
248e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
249e66f840cSTristram Ha }
250e66f840cSTristram Ha 
251e66f840cSTristram Ha static void ksz8795_w_table(struct ksz_device *dev, int table, u16 addr,
252e66f840cSTristram Ha 			    u64 data)
253e66f840cSTristram Ha {
254e66f840cSTristram Ha 	u16 ctrl_addr;
255e66f840cSTristram Ha 
256e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(table) | addr;
257e66f840cSTristram Ha 
258e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
259e66f840cSTristram Ha 	ksz_write64(dev, REG_IND_DATA_HI, data);
260e66f840cSTristram Ha 	ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
261e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
262e66f840cSTristram Ha }
263e66f840cSTristram Ha 
264e66f840cSTristram Ha static int ksz8795_valid_dyn_entry(struct ksz_device *dev, u8 *data)
265e66f840cSTristram Ha {
266e66f840cSTristram Ha 	int timeout = 100;
267e66f840cSTristram Ha 
268e66f840cSTristram Ha 	do {
269e66f840cSTristram Ha 		ksz_read8(dev, REG_IND_DATA_CHECK, data);
270e66f840cSTristram Ha 		timeout--;
271e66f840cSTristram Ha 	} while ((*data & DYNAMIC_MAC_TABLE_NOT_READY) && timeout);
272e66f840cSTristram Ha 
273e66f840cSTristram Ha 	/* Entry is not ready for accessing. */
274e66f840cSTristram Ha 	if (*data & DYNAMIC_MAC_TABLE_NOT_READY) {
275e66f840cSTristram Ha 		return -EAGAIN;
276e66f840cSTristram Ha 	/* Entry is ready for accessing. */
277e66f840cSTristram Ha 	} else {
278e66f840cSTristram Ha 		ksz_read8(dev, REG_IND_DATA_8, data);
279e66f840cSTristram Ha 
280e66f840cSTristram Ha 		/* There is no valid entry in the table. */
281e66f840cSTristram Ha 		if (*data & DYNAMIC_MAC_TABLE_MAC_EMPTY)
282e66f840cSTristram Ha 			return -ENXIO;
283e66f840cSTristram Ha 	}
284e66f840cSTristram Ha 	return 0;
285e66f840cSTristram Ha }
286e66f840cSTristram Ha 
287e66f840cSTristram Ha static int ksz8795_r_dyn_mac_table(struct ksz_device *dev, u16 addr,
288e66f840cSTristram Ha 				   u8 *mac_addr, u8 *fid, u8 *src_port,
289e66f840cSTristram Ha 				   u8 *timestamp, u16 *entries)
290e66f840cSTristram Ha {
291e66f840cSTristram Ha 	u32 data_hi, data_lo;
292e66f840cSTristram Ha 	u16 ctrl_addr;
293e66f840cSTristram Ha 	u8 data;
294e66f840cSTristram Ha 	int rc;
295e66f840cSTristram Ha 
296e66f840cSTristram Ha 	ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
297e66f840cSTristram Ha 
298e66f840cSTristram Ha 	mutex_lock(&dev->alu_mutex);
299e66f840cSTristram Ha 	ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
300e66f840cSTristram Ha 
301e66f840cSTristram Ha 	rc = ksz8795_valid_dyn_entry(dev, &data);
302e66f840cSTristram Ha 	if (rc == -EAGAIN) {
303e66f840cSTristram Ha 		if (addr == 0)
304e66f840cSTristram Ha 			*entries = 0;
305e66f840cSTristram Ha 	} else if (rc == -ENXIO) {
306e66f840cSTristram Ha 		*entries = 0;
307e66f840cSTristram Ha 	/* At least one valid entry in the table. */
308e66f840cSTristram Ha 	} else {
309e66f840cSTristram Ha 		u64 buf = 0;
310e66f840cSTristram Ha 		int cnt;
311e66f840cSTristram Ha 
312e66f840cSTristram Ha 		ksz_read64(dev, REG_IND_DATA_HI, &buf);
313e66f840cSTristram Ha 		data_hi = (u32)(buf >> 32);
314e66f840cSTristram Ha 		data_lo = (u32)buf;
315e66f840cSTristram Ha 
316e66f840cSTristram Ha 		/* Check out how many valid entry in the table. */
317e66f840cSTristram Ha 		cnt = data & DYNAMIC_MAC_TABLE_ENTRIES_H;
318e66f840cSTristram Ha 		cnt <<= DYNAMIC_MAC_ENTRIES_H_S;
319e66f840cSTristram Ha 		cnt |= (data_hi & DYNAMIC_MAC_TABLE_ENTRIES) >>
320e66f840cSTristram Ha 			DYNAMIC_MAC_ENTRIES_S;
321e66f840cSTristram Ha 		*entries = cnt + 1;
322e66f840cSTristram Ha 
323e66f840cSTristram Ha 		*fid = (data_hi & DYNAMIC_MAC_TABLE_FID) >>
324e66f840cSTristram Ha 			DYNAMIC_MAC_FID_S;
325e66f840cSTristram Ha 		*src_port = (data_hi & DYNAMIC_MAC_TABLE_SRC_PORT) >>
326e66f840cSTristram Ha 			DYNAMIC_MAC_SRC_PORT_S;
327e66f840cSTristram Ha 		*timestamp = (data_hi & DYNAMIC_MAC_TABLE_TIMESTAMP) >>
328e66f840cSTristram Ha 			DYNAMIC_MAC_TIMESTAMP_S;
329e66f840cSTristram Ha 
330e66f840cSTristram Ha 		mac_addr[5] = (u8)data_lo;
331e66f840cSTristram Ha 		mac_addr[4] = (u8)(data_lo >> 8);
332e66f840cSTristram Ha 		mac_addr[3] = (u8)(data_lo >> 16);
333e66f840cSTristram Ha 		mac_addr[2] = (u8)(data_lo >> 24);
334e66f840cSTristram Ha 
335e66f840cSTristram Ha 		mac_addr[1] = (u8)data_hi;
336e66f840cSTristram Ha 		mac_addr[0] = (u8)(data_hi >> 8);
337e66f840cSTristram Ha 		rc = 0;
338e66f840cSTristram Ha 	}
339e66f840cSTristram Ha 	mutex_unlock(&dev->alu_mutex);
340e66f840cSTristram Ha 
341e66f840cSTristram Ha 	return rc;
342e66f840cSTristram Ha }
343e66f840cSTristram Ha 
344e66f840cSTristram Ha static int ksz8795_r_sta_mac_table(struct ksz_device *dev, u16 addr,
345e66f840cSTristram Ha 				   struct alu_struct *alu)
346e66f840cSTristram Ha {
347e66f840cSTristram Ha 	u32 data_hi, data_lo;
348e66f840cSTristram Ha 	u64 data;
349e66f840cSTristram Ha 
350e66f840cSTristram Ha 	ksz8795_r_table(dev, TABLE_STATIC_MAC, addr, &data);
351e66f840cSTristram Ha 	data_hi = data >> 32;
352e66f840cSTristram Ha 	data_lo = (u32)data;
353e66f840cSTristram Ha 	if (data_hi & (STATIC_MAC_TABLE_VALID | STATIC_MAC_TABLE_OVERRIDE)) {
354e66f840cSTristram Ha 		alu->mac[5] = (u8)data_lo;
355e66f840cSTristram Ha 		alu->mac[4] = (u8)(data_lo >> 8);
356e66f840cSTristram Ha 		alu->mac[3] = (u8)(data_lo >> 16);
357e66f840cSTristram Ha 		alu->mac[2] = (u8)(data_lo >> 24);
358e66f840cSTristram Ha 		alu->mac[1] = (u8)data_hi;
359e66f840cSTristram Ha 		alu->mac[0] = (u8)(data_hi >> 8);
360e66f840cSTristram Ha 		alu->port_forward = (data_hi & STATIC_MAC_TABLE_FWD_PORTS) >>
361e66f840cSTristram Ha 			STATIC_MAC_FWD_PORTS_S;
362e66f840cSTristram Ha 		alu->is_override =
363e66f840cSTristram Ha 			(data_hi & STATIC_MAC_TABLE_OVERRIDE) ? 1 : 0;
364e66f840cSTristram Ha 		data_hi >>= 1;
365e66f840cSTristram Ha 		alu->is_use_fid = (data_hi & STATIC_MAC_TABLE_USE_FID) ? 1 : 0;
366e66f840cSTristram Ha 		alu->fid = (data_hi & STATIC_MAC_TABLE_FID) >>
367e66f840cSTristram Ha 			STATIC_MAC_FID_S;
368e66f840cSTristram Ha 		return 0;
369e66f840cSTristram Ha 	}
370e66f840cSTristram Ha 	return -ENXIO;
371e66f840cSTristram Ha }
372e66f840cSTristram Ha 
373e66f840cSTristram Ha static void ksz8795_w_sta_mac_table(struct ksz_device *dev, u16 addr,
374e66f840cSTristram Ha 				    struct alu_struct *alu)
375e66f840cSTristram Ha {
376e66f840cSTristram Ha 	u32 data_hi, data_lo;
377e66f840cSTristram Ha 	u64 data;
378e66f840cSTristram Ha 
379e66f840cSTristram Ha 	data_lo = ((u32)alu->mac[2] << 24) |
380e66f840cSTristram Ha 		((u32)alu->mac[3] << 16) |
381e66f840cSTristram Ha 		((u32)alu->mac[4] << 8) | alu->mac[5];
382e66f840cSTristram Ha 	data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
383e66f840cSTristram Ha 	data_hi |= (u32)alu->port_forward << STATIC_MAC_FWD_PORTS_S;
384e66f840cSTristram Ha 
385e66f840cSTristram Ha 	if (alu->is_override)
386e66f840cSTristram Ha 		data_hi |= STATIC_MAC_TABLE_OVERRIDE;
387e66f840cSTristram Ha 	if (alu->is_use_fid) {
388e66f840cSTristram Ha 		data_hi |= STATIC_MAC_TABLE_USE_FID;
389e66f840cSTristram Ha 		data_hi |= (u32)alu->fid << STATIC_MAC_FID_S;
390e66f840cSTristram Ha 	}
391e66f840cSTristram Ha 	if (alu->is_static)
392e66f840cSTristram Ha 		data_hi |= STATIC_MAC_TABLE_VALID;
393e66f840cSTristram Ha 	else
394e66f840cSTristram Ha 		data_hi &= ~STATIC_MAC_TABLE_OVERRIDE;
395e66f840cSTristram Ha 
396e66f840cSTristram Ha 	data = (u64)data_hi << 32 | data_lo;
397e66f840cSTristram Ha 	ksz8795_w_table(dev, TABLE_STATIC_MAC, addr, data);
398e66f840cSTristram Ha }
399e66f840cSTristram Ha 
400e66f840cSTristram Ha static void ksz8795_from_vlan(u16 vlan, u8 *fid, u8 *member, u8 *valid)
401e66f840cSTristram Ha {
402e66f840cSTristram Ha 	*fid = vlan & VLAN_TABLE_FID;
403e66f840cSTristram Ha 	*member = (vlan & VLAN_TABLE_MEMBERSHIP) >> VLAN_TABLE_MEMBERSHIP_S;
404e66f840cSTristram Ha 	*valid = !!(vlan & VLAN_TABLE_VALID);
405e66f840cSTristram Ha }
406e66f840cSTristram Ha 
407e66f840cSTristram Ha static void ksz8795_to_vlan(u8 fid, u8 member, u8 valid, u16 *vlan)
408e66f840cSTristram Ha {
409e66f840cSTristram Ha 	*vlan = fid;
410e66f840cSTristram Ha 	*vlan |= (u16)member << VLAN_TABLE_MEMBERSHIP_S;
411e66f840cSTristram Ha 	if (valid)
412e66f840cSTristram Ha 		*vlan |= VLAN_TABLE_VALID;
413e66f840cSTristram Ha }
414e66f840cSTristram Ha 
415e66f840cSTristram Ha static void ksz8795_r_vlan_entries(struct ksz_device *dev, u16 addr)
416e66f840cSTristram Ha {
417e66f840cSTristram Ha 	u64 data;
418e66f840cSTristram Ha 	int i;
419e66f840cSTristram Ha 
420e66f840cSTristram Ha 	ksz8795_r_table(dev, TABLE_VLAN, addr, &data);
4214ce2a984SMichael Grzeschik 	addr *= dev->phy_port_cnt;
4224ce2a984SMichael Grzeschik 	for (i = 0; i < dev->phy_port_cnt; i++) {
423e66f840cSTristram Ha 		dev->vlan_cache[addr + i].table[0] = (u16)data;
424e66f840cSTristram Ha 		data >>= VLAN_TABLE_S;
425e66f840cSTristram Ha 	}
426e66f840cSTristram Ha }
427e66f840cSTristram Ha 
428e66f840cSTristram Ha static void ksz8795_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
429e66f840cSTristram Ha {
430e66f840cSTristram Ha 	int index;
431e66f840cSTristram Ha 	u16 *data;
432e66f840cSTristram Ha 	u16 addr;
433e66f840cSTristram Ha 	u64 buf;
434e66f840cSTristram Ha 
435e66f840cSTristram Ha 	data = (u16 *)&buf;
4364ce2a984SMichael Grzeschik 	addr = vid / dev->phy_port_cnt;
437e66f840cSTristram Ha 	index = vid & 3;
438e66f840cSTristram Ha 	ksz8795_r_table(dev, TABLE_VLAN, addr, &buf);
439e66f840cSTristram Ha 	*vlan = data[index];
440e66f840cSTristram Ha }
441e66f840cSTristram Ha 
442e66f840cSTristram Ha static void ksz8795_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
443e66f840cSTristram Ha {
444e66f840cSTristram Ha 	int index;
445e66f840cSTristram Ha 	u16 *data;
446e66f840cSTristram Ha 	u16 addr;
447e66f840cSTristram Ha 	u64 buf;
448e66f840cSTristram Ha 
449e66f840cSTristram Ha 	data = (u16 *)&buf;
4504ce2a984SMichael Grzeschik 	addr = vid / dev->phy_port_cnt;
451e66f840cSTristram Ha 	index = vid & 3;
452e66f840cSTristram Ha 	ksz8795_r_table(dev, TABLE_VLAN, addr, &buf);
453e66f840cSTristram Ha 	data[index] = vlan;
454e66f840cSTristram Ha 	dev->vlan_cache[vid].table[0] = vlan;
455e66f840cSTristram Ha 	ksz8795_w_table(dev, TABLE_VLAN, addr, buf);
456e66f840cSTristram Ha }
457e66f840cSTristram Ha 
458e66f840cSTristram Ha static void ksz8795_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
459e66f840cSTristram Ha {
460e66f840cSTristram Ha 	u8 restart, speed, ctrl, link;
461e66f840cSTristram Ha 	int processed = true;
462e66f840cSTristram Ha 	u16 data = 0;
463e66f840cSTristram Ha 	u8 p = phy;
464e66f840cSTristram Ha 
465e66f840cSTristram Ha 	switch (reg) {
466e66f840cSTristram Ha 	case PHY_REG_CTRL:
467e66f840cSTristram Ha 		ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart);
468e66f840cSTristram Ha 		ksz_pread8(dev, p, P_SPEED_STATUS, &speed);
469e66f840cSTristram Ha 		ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl);
470e66f840cSTristram Ha 		if (restart & PORT_PHY_LOOPBACK)
471e66f840cSTristram Ha 			data |= PHY_LOOPBACK;
472e66f840cSTristram Ha 		if (ctrl & PORT_FORCE_100_MBIT)
473e66f840cSTristram Ha 			data |= PHY_SPEED_100MBIT;
474e66f840cSTristram Ha 		if (!(ctrl & PORT_AUTO_NEG_DISABLE))
475e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_ENABLE;
476e66f840cSTristram Ha 		if (restart & PORT_POWER_DOWN)
477e66f840cSTristram Ha 			data |= PHY_POWER_DOWN;
478e66f840cSTristram Ha 		if (restart & PORT_AUTO_NEG_RESTART)
479e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_RESTART;
480e66f840cSTristram Ha 		if (ctrl & PORT_FORCE_FULL_DUPLEX)
481e66f840cSTristram Ha 			data |= PHY_FULL_DUPLEX;
482e66f840cSTristram Ha 		if (speed & PORT_HP_MDIX)
483e66f840cSTristram Ha 			data |= PHY_HP_MDIX;
484e66f840cSTristram Ha 		if (restart & PORT_FORCE_MDIX)
485e66f840cSTristram Ha 			data |= PHY_FORCE_MDIX;
486e66f840cSTristram Ha 		if (restart & PORT_AUTO_MDIX_DISABLE)
487e66f840cSTristram Ha 			data |= PHY_AUTO_MDIX_DISABLE;
488e66f840cSTristram Ha 		if (restart & PORT_TX_DISABLE)
489e66f840cSTristram Ha 			data |= PHY_TRANSMIT_DISABLE;
490e66f840cSTristram Ha 		if (restart & PORT_LED_OFF)
491e66f840cSTristram Ha 			data |= PHY_LED_DISABLE;
492e66f840cSTristram Ha 		break;
493e66f840cSTristram Ha 	case PHY_REG_STATUS:
494e66f840cSTristram Ha 		ksz_pread8(dev, p, P_LINK_STATUS, &link);
495e66f840cSTristram Ha 		data = PHY_100BTX_FD_CAPABLE |
496e66f840cSTristram Ha 		       PHY_100BTX_CAPABLE |
497e66f840cSTristram Ha 		       PHY_10BT_FD_CAPABLE |
498e66f840cSTristram Ha 		       PHY_10BT_CAPABLE |
499e66f840cSTristram Ha 		       PHY_AUTO_NEG_CAPABLE;
500e66f840cSTristram Ha 		if (link & PORT_AUTO_NEG_COMPLETE)
501e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_ACKNOWLEDGE;
502e66f840cSTristram Ha 		if (link & PORT_STAT_LINK_GOOD)
503e66f840cSTristram Ha 			data |= PHY_LINK_STATUS;
504e66f840cSTristram Ha 		break;
505e66f840cSTristram Ha 	case PHY_REG_ID_1:
506e66f840cSTristram Ha 		data = KSZ8795_ID_HI;
507e66f840cSTristram Ha 		break;
508e66f840cSTristram Ha 	case PHY_REG_ID_2:
509e66f840cSTristram Ha 		data = KSZ8795_ID_LO;
510e66f840cSTristram Ha 		break;
511e66f840cSTristram Ha 	case PHY_REG_AUTO_NEGOTIATION:
512e66f840cSTristram Ha 		ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl);
513e66f840cSTristram Ha 		data = PHY_AUTO_NEG_802_3;
514e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
515e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_SYM_PAUSE;
516e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_100BTX_FD)
517e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_100BTX_FD;
518e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_100BTX)
519e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_100BTX;
520e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_10BT_FD)
521e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_10BT_FD;
522e66f840cSTristram Ha 		if (ctrl & PORT_AUTO_NEG_10BT)
523e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_10BT;
524e66f840cSTristram Ha 		break;
525e66f840cSTristram Ha 	case PHY_REG_REMOTE_CAPABILITY:
526e66f840cSTristram Ha 		ksz_pread8(dev, p, P_REMOTE_STATUS, &link);
527e66f840cSTristram Ha 		data = PHY_AUTO_NEG_802_3;
528e66f840cSTristram Ha 		if (link & PORT_REMOTE_SYM_PAUSE)
529e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_SYM_PAUSE;
530e66f840cSTristram Ha 		if (link & PORT_REMOTE_100BTX_FD)
531e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_100BTX_FD;
532e66f840cSTristram Ha 		if (link & PORT_REMOTE_100BTX)
533e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_100BTX;
534e66f840cSTristram Ha 		if (link & PORT_REMOTE_10BT_FD)
535e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_10BT_FD;
536e66f840cSTristram Ha 		if (link & PORT_REMOTE_10BT)
537e66f840cSTristram Ha 			data |= PHY_AUTO_NEG_10BT;
538e66f840cSTristram Ha 		if (data & ~PHY_AUTO_NEG_802_3)
539e66f840cSTristram Ha 			data |= PHY_REMOTE_ACKNOWLEDGE_NOT;
540e66f840cSTristram Ha 		break;
541e66f840cSTristram Ha 	default:
542e66f840cSTristram Ha 		processed = false;
543e66f840cSTristram Ha 		break;
544e66f840cSTristram Ha 	}
545e66f840cSTristram Ha 	if (processed)
546e66f840cSTristram Ha 		*val = data;
547e66f840cSTristram Ha }
548e66f840cSTristram Ha 
549e66f840cSTristram Ha static void ksz8795_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
550e66f840cSTristram Ha {
551e66f840cSTristram Ha 	u8 p = phy;
552e66f840cSTristram Ha 	u8 restart, speed, ctrl, data;
553e66f840cSTristram Ha 
554e66f840cSTristram Ha 	switch (reg) {
555e66f840cSTristram Ha 	case PHY_REG_CTRL:
556e66f840cSTristram Ha 
557e66f840cSTristram Ha 		/* Do not support PHY reset function. */
558e66f840cSTristram Ha 		if (val & PHY_RESET)
559e66f840cSTristram Ha 			break;
560e66f840cSTristram Ha 		ksz_pread8(dev, p, P_SPEED_STATUS, &speed);
561e66f840cSTristram Ha 		data = speed;
562e66f840cSTristram Ha 		if (val & PHY_HP_MDIX)
563e66f840cSTristram Ha 			data |= PORT_HP_MDIX;
564e66f840cSTristram Ha 		else
565e66f840cSTristram Ha 			data &= ~PORT_HP_MDIX;
566e66f840cSTristram Ha 		if (data != speed)
567e66f840cSTristram Ha 			ksz_pwrite8(dev, p, P_SPEED_STATUS, data);
568e66f840cSTristram Ha 		ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl);
569e66f840cSTristram Ha 		data = ctrl;
570e66f840cSTristram Ha 		if (!(val & PHY_AUTO_NEG_ENABLE))
571e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_DISABLE;
572e66f840cSTristram Ha 		else
573e66f840cSTristram Ha 			data &= ~PORT_AUTO_NEG_DISABLE;
574e66f840cSTristram Ha 
575e66f840cSTristram Ha 		/* Fiber port does not support auto-negotiation. */
576e66f840cSTristram Ha 		if (dev->ports[p].fiber)
577e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_DISABLE;
578e66f840cSTristram Ha 		if (val & PHY_SPEED_100MBIT)
579e66f840cSTristram Ha 			data |= PORT_FORCE_100_MBIT;
580e66f840cSTristram Ha 		else
581e66f840cSTristram Ha 			data &= ~PORT_FORCE_100_MBIT;
582e66f840cSTristram Ha 		if (val & PHY_FULL_DUPLEX)
583e66f840cSTristram Ha 			data |= PORT_FORCE_FULL_DUPLEX;
584e66f840cSTristram Ha 		else
585e66f840cSTristram Ha 			data &= ~PORT_FORCE_FULL_DUPLEX;
586e66f840cSTristram Ha 		if (data != ctrl)
587e66f840cSTristram Ha 			ksz_pwrite8(dev, p, P_FORCE_CTRL, data);
588e66f840cSTristram Ha 		ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart);
589e66f840cSTristram Ha 		data = restart;
590e66f840cSTristram Ha 		if (val & PHY_LED_DISABLE)
591e66f840cSTristram Ha 			data |= PORT_LED_OFF;
592e66f840cSTristram Ha 		else
593e66f840cSTristram Ha 			data &= ~PORT_LED_OFF;
594e66f840cSTristram Ha 		if (val & PHY_TRANSMIT_DISABLE)
595e66f840cSTristram Ha 			data |= PORT_TX_DISABLE;
596e66f840cSTristram Ha 		else
597e66f840cSTristram Ha 			data &= ~PORT_TX_DISABLE;
598e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_RESTART)
599e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_RESTART;
600e66f840cSTristram Ha 		else
601e66f840cSTristram Ha 			data &= ~(PORT_AUTO_NEG_RESTART);
602e66f840cSTristram Ha 		if (val & PHY_POWER_DOWN)
603e66f840cSTristram Ha 			data |= PORT_POWER_DOWN;
604e66f840cSTristram Ha 		else
605e66f840cSTristram Ha 			data &= ~PORT_POWER_DOWN;
606e66f840cSTristram Ha 		if (val & PHY_AUTO_MDIX_DISABLE)
607e66f840cSTristram Ha 			data |= PORT_AUTO_MDIX_DISABLE;
608e66f840cSTristram Ha 		else
609e66f840cSTristram Ha 			data &= ~PORT_AUTO_MDIX_DISABLE;
610e66f840cSTristram Ha 		if (val & PHY_FORCE_MDIX)
611e66f840cSTristram Ha 			data |= PORT_FORCE_MDIX;
612e66f840cSTristram Ha 		else
613e66f840cSTristram Ha 			data &= ~PORT_FORCE_MDIX;
614e66f840cSTristram Ha 		if (val & PHY_LOOPBACK)
615e66f840cSTristram Ha 			data |= PORT_PHY_LOOPBACK;
616e66f840cSTristram Ha 		else
617e66f840cSTristram Ha 			data &= ~PORT_PHY_LOOPBACK;
618e66f840cSTristram Ha 		if (data != restart)
619e66f840cSTristram Ha 			ksz_pwrite8(dev, p, P_NEG_RESTART_CTRL, data);
620e66f840cSTristram Ha 		break;
621e66f840cSTristram Ha 	case PHY_REG_AUTO_NEGOTIATION:
622e66f840cSTristram Ha 		ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl);
623e66f840cSTristram Ha 		data = ctrl;
624e66f840cSTristram Ha 		data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
625e66f840cSTristram Ha 			  PORT_AUTO_NEG_100BTX_FD |
626e66f840cSTristram Ha 			  PORT_AUTO_NEG_100BTX |
627e66f840cSTristram Ha 			  PORT_AUTO_NEG_10BT_FD |
628e66f840cSTristram Ha 			  PORT_AUTO_NEG_10BT);
629e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_SYM_PAUSE)
630e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_SYM_PAUSE;
631e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_100BTX_FD)
632e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_100BTX_FD;
633e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_100BTX)
634e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_100BTX;
635e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_10BT_FD)
636e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_10BT_FD;
637e66f840cSTristram Ha 		if (val & PHY_AUTO_NEG_10BT)
638e66f840cSTristram Ha 			data |= PORT_AUTO_NEG_10BT;
639e66f840cSTristram Ha 		if (data != ctrl)
640e66f840cSTristram Ha 			ksz_pwrite8(dev, p, P_LOCAL_CTRL, data);
641e66f840cSTristram Ha 		break;
642e66f840cSTristram Ha 	default:
643e66f840cSTristram Ha 		break;
644e66f840cSTristram Ha 	}
645e66f840cSTristram Ha }
646e66f840cSTristram Ha 
647e66f840cSTristram Ha static enum dsa_tag_protocol ksz8795_get_tag_protocol(struct dsa_switch *ds,
6484d776482SFlorian Fainelli 						      int port,
6494d776482SFlorian Fainelli 						      enum dsa_tag_protocol mp)
650e66f840cSTristram Ha {
651e66f840cSTristram Ha 	return DSA_TAG_PROTO_KSZ8795;
652e66f840cSTristram Ha }
653e66f840cSTristram Ha 
654e66f840cSTristram Ha static void ksz8795_get_strings(struct dsa_switch *ds, int port,
655e66f840cSTristram Ha 				u32 stringset, uint8_t *buf)
656e66f840cSTristram Ha {
65765fe1acfSMichael Grzeschik 	struct ksz_device *dev = ds->priv;
658e66f840cSTristram Ha 	int i;
659e66f840cSTristram Ha 
66065fe1acfSMichael Grzeschik 	for (i = 0; i < dev->mib_cnt; i++) {
661e66f840cSTristram Ha 		memcpy(buf + i * ETH_GSTRING_LEN, mib_names[i].string,
662e66f840cSTristram Ha 		       ETH_GSTRING_LEN);
663e66f840cSTristram Ha 	}
664e66f840cSTristram Ha }
665e66f840cSTristram Ha 
666e66f840cSTristram Ha static void ksz8795_cfg_port_member(struct ksz_device *dev, int port,
667e66f840cSTristram Ha 				    u8 member)
668e66f840cSTristram Ha {
669e66f840cSTristram Ha 	u8 data;
670e66f840cSTristram Ha 
671e66f840cSTristram Ha 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
672e66f840cSTristram Ha 	data &= ~PORT_VLAN_MEMBERSHIP;
673e66f840cSTristram Ha 	data |= (member & dev->port_mask);
674e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
675e66f840cSTristram Ha 	dev->ports[port].member = member;
676e66f840cSTristram Ha }
677e66f840cSTristram Ha 
678e66f840cSTristram Ha static void ksz8795_port_stp_state_set(struct dsa_switch *ds, int port,
679e66f840cSTristram Ha 				       u8 state)
680e66f840cSTristram Ha {
681e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
682e66f840cSTristram Ha 	int forward = dev->member;
683e66f840cSTristram Ha 	struct ksz_port *p;
684e66f840cSTristram Ha 	int member = -1;
685e66f840cSTristram Ha 	u8 data;
686e66f840cSTristram Ha 
687e66f840cSTristram Ha 	p = &dev->ports[port];
688e66f840cSTristram Ha 
689e66f840cSTristram Ha 	ksz_pread8(dev, port, P_STP_CTRL, &data);
690e66f840cSTristram Ha 	data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
691e66f840cSTristram Ha 
692e66f840cSTristram Ha 	switch (state) {
693e66f840cSTristram Ha 	case BR_STATE_DISABLED:
694e66f840cSTristram Ha 		data |= PORT_LEARN_DISABLE;
6954ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt)
696e66f840cSTristram Ha 			member = 0;
697e66f840cSTristram Ha 		break;
698e66f840cSTristram Ha 	case BR_STATE_LISTENING:
699e66f840cSTristram Ha 		data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
7004ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt &&
701e66f840cSTristram Ha 		    p->stp_state == BR_STATE_DISABLED)
702e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
703e66f840cSTristram Ha 		break;
704e66f840cSTristram Ha 	case BR_STATE_LEARNING:
705e66f840cSTristram Ha 		data |= PORT_RX_ENABLE;
706e66f840cSTristram Ha 		break;
707e66f840cSTristram Ha 	case BR_STATE_FORWARDING:
708e66f840cSTristram Ha 		data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
709e66f840cSTristram Ha 
710e66f840cSTristram Ha 		/* This function is also used internally. */
711e66f840cSTristram Ha 		if (port == dev->cpu_port)
712e66f840cSTristram Ha 			break;
713e66f840cSTristram Ha 
714e66f840cSTristram Ha 		/* Port is a member of a bridge. */
715e66f840cSTristram Ha 		if (dev->br_member & BIT(port)) {
716e66f840cSTristram Ha 			dev->member |= BIT(port);
717e66f840cSTristram Ha 			member = dev->member;
718e66f840cSTristram Ha 		} else {
719e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
720e66f840cSTristram Ha 		}
721e66f840cSTristram Ha 		break;
722e66f840cSTristram Ha 	case BR_STATE_BLOCKING:
723e66f840cSTristram Ha 		data |= PORT_LEARN_DISABLE;
7244ce2a984SMichael Grzeschik 		if (port < dev->phy_port_cnt &&
725e66f840cSTristram Ha 		    p->stp_state == BR_STATE_DISABLED)
726e66f840cSTristram Ha 			member = dev->host_mask | p->vid_member;
727e66f840cSTristram Ha 		break;
728e66f840cSTristram Ha 	default:
729e66f840cSTristram Ha 		dev_err(ds->dev, "invalid STP state: %d\n", state);
730e66f840cSTristram Ha 		return;
731e66f840cSTristram Ha 	}
732e66f840cSTristram Ha 
733e66f840cSTristram Ha 	ksz_pwrite8(dev, port, P_STP_CTRL, data);
734e66f840cSTristram Ha 	p->stp_state = state;
735e66f840cSTristram Ha 	/* Port membership may share register with STP state. */
736e66f840cSTristram Ha 	if (member >= 0 && member != p->member)
737e66f840cSTristram Ha 		ksz8795_cfg_port_member(dev, port, (u8)member);
738e66f840cSTristram Ha 
739e66f840cSTristram Ha 	/* Check if forwarding needs to be updated. */
740e66f840cSTristram Ha 	if (state != BR_STATE_FORWARDING) {
741e66f840cSTristram Ha 		if (dev->br_member & BIT(port))
742e66f840cSTristram Ha 			dev->member &= ~BIT(port);
743e66f840cSTristram Ha 	}
744e66f840cSTristram Ha 
745e66f840cSTristram Ha 	/* When topology has changed the function ksz_update_port_member
746e66f840cSTristram Ha 	 * should be called to modify port forwarding behavior.
747e66f840cSTristram Ha 	 */
748e66f840cSTristram Ha 	if (forward != dev->member)
749e66f840cSTristram Ha 		ksz_update_port_member(dev, port);
750e66f840cSTristram Ha }
751e66f840cSTristram Ha 
752e66f840cSTristram Ha static void ksz8795_flush_dyn_mac_table(struct ksz_device *dev, int port)
753e66f840cSTristram Ha {
754241ed719SMichael Grzeschik 	u8 learn[DSA_MAX_PORTS];
755e66f840cSTristram Ha 	int first, index, cnt;
756e66f840cSTristram Ha 	struct ksz_port *p;
757e66f840cSTristram Ha 
758241ed719SMichael Grzeschik 	if ((uint)port < dev->port_cnt) {
759e66f840cSTristram Ha 		first = port;
760e66f840cSTristram Ha 		cnt = port + 1;
761e66f840cSTristram Ha 	} else {
762e66f840cSTristram Ha 		/* Flush all ports. */
763e66f840cSTristram Ha 		first = 0;
764c9f4633bSMichael Grzeschik 		cnt = dev->port_cnt;
765e66f840cSTristram Ha 	}
766e66f840cSTristram Ha 	for (index = first; index < cnt; index++) {
767e66f840cSTristram Ha 		p = &dev->ports[index];
768e66f840cSTristram Ha 		if (!p->on)
769e66f840cSTristram Ha 			continue;
770e66f840cSTristram Ha 		ksz_pread8(dev, index, P_STP_CTRL, &learn[index]);
771e66f840cSTristram Ha 		if (!(learn[index] & PORT_LEARN_DISABLE))
772e66f840cSTristram Ha 			ksz_pwrite8(dev, index, P_STP_CTRL,
773e66f840cSTristram Ha 				    learn[index] | PORT_LEARN_DISABLE);
774e66f840cSTristram Ha 	}
775e66f840cSTristram Ha 	ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
776e66f840cSTristram Ha 	for (index = first; index < cnt; index++) {
777e66f840cSTristram Ha 		p = &dev->ports[index];
778e66f840cSTristram Ha 		if (!p->on)
779e66f840cSTristram Ha 			continue;
780e66f840cSTristram Ha 		if (!(learn[index] & PORT_LEARN_DISABLE))
781e66f840cSTristram Ha 			ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]);
782e66f840cSTristram Ha 	}
783e66f840cSTristram Ha }
784e66f840cSTristram Ha 
785e66f840cSTristram Ha static int ksz8795_port_vlan_filtering(struct dsa_switch *ds, int port,
7862e554a7aSVladimir Oltean 				       bool flag,
7872e554a7aSVladimir Oltean 				       struct switchdev_trans *trans)
788e66f840cSTristram Ha {
789e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
790e66f840cSTristram Ha 
7912e554a7aSVladimir Oltean 	if (switchdev_trans_ph_prepare(trans))
7922e554a7aSVladimir Oltean 		return 0;
7932e554a7aSVladimir Oltean 
794e66f840cSTristram Ha 	ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
795e66f840cSTristram Ha 
796e66f840cSTristram Ha 	return 0;
797e66f840cSTristram Ha }
798e66f840cSTristram Ha 
799e66f840cSTristram Ha static void ksz8795_port_vlan_add(struct dsa_switch *ds, int port,
800e66f840cSTristram Ha 				  const struct switchdev_obj_port_vlan *vlan)
801e66f840cSTristram Ha {
802e66f840cSTristram Ha 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
803e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
804*b7a9e0daSVladimir Oltean 	u16 data, new_pvid = 0;
805e66f840cSTristram Ha 	u8 fid, member, valid;
806e66f840cSTristram Ha 
807e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
808e66f840cSTristram Ha 
809*b7a9e0daSVladimir Oltean 	ksz8795_r_vlan_table(dev, vlan->vid, &data);
810e66f840cSTristram Ha 	ksz8795_from_vlan(data, &fid, &member, &valid);
811e66f840cSTristram Ha 
812e66f840cSTristram Ha 	/* First time to setup the VLAN entry. */
813e66f840cSTristram Ha 	if (!valid) {
814e66f840cSTristram Ha 		/* Need to find a way to map VID to FID. */
815e66f840cSTristram Ha 		fid = 1;
816e66f840cSTristram Ha 		valid = 1;
817e66f840cSTristram Ha 	}
818e66f840cSTristram Ha 	member |= BIT(port);
819e66f840cSTristram Ha 
820e66f840cSTristram Ha 	ksz8795_to_vlan(fid, member, valid, &data);
821*b7a9e0daSVladimir Oltean 	ksz8795_w_vlan_table(dev, vlan->vid, data);
822e66f840cSTristram Ha 
823e66f840cSTristram Ha 	/* change PVID */
824e66f840cSTristram Ha 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
825*b7a9e0daSVladimir Oltean 		new_pvid = vlan->vid;
826e66f840cSTristram Ha 
827e66f840cSTristram Ha 	if (new_pvid) {
828*b7a9e0daSVladimir Oltean 		u16 vid;
829*b7a9e0daSVladimir Oltean 
830e66f840cSTristram Ha 		ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
831e66f840cSTristram Ha 		vid &= 0xfff;
832e66f840cSTristram Ha 		vid |= new_pvid;
833e66f840cSTristram Ha 		ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
834e66f840cSTristram Ha 	}
835e66f840cSTristram Ha }
836e66f840cSTristram Ha 
837e66f840cSTristram Ha static int ksz8795_port_vlan_del(struct dsa_switch *ds, int port,
838e66f840cSTristram Ha 				 const struct switchdev_obj_port_vlan *vlan)
839e66f840cSTristram Ha {
840e66f840cSTristram Ha 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
841e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
842*b7a9e0daSVladimir Oltean 	u16 data, pvid, new_pvid = 0;
843e66f840cSTristram Ha 	u8 fid, member, valid;
844e66f840cSTristram Ha 
845e66f840cSTristram Ha 	ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
846e66f840cSTristram Ha 	pvid = pvid & 0xFFF;
847e66f840cSTristram Ha 
848e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
849e66f840cSTristram Ha 
850*b7a9e0daSVladimir Oltean 	ksz8795_r_vlan_table(dev, vlan->vid, &data);
851e66f840cSTristram Ha 	ksz8795_from_vlan(data, &fid, &member, &valid);
852e66f840cSTristram Ha 
853e66f840cSTristram Ha 	member &= ~BIT(port);
854e66f840cSTristram Ha 
855e66f840cSTristram Ha 	/* Invalidate the entry if no more member. */
856e66f840cSTristram Ha 	if (!member) {
857e66f840cSTristram Ha 		fid = 0;
858e66f840cSTristram Ha 		valid = 0;
859e66f840cSTristram Ha 	}
860e66f840cSTristram Ha 
861*b7a9e0daSVladimir Oltean 	if (pvid == vlan->vid)
862e66f840cSTristram Ha 		new_pvid = 1;
863e66f840cSTristram Ha 
864e66f840cSTristram Ha 	ksz8795_to_vlan(fid, member, valid, &data);
865*b7a9e0daSVladimir Oltean 	ksz8795_w_vlan_table(dev, vlan->vid, data);
866e66f840cSTristram Ha 
867e66f840cSTristram Ha 	if (new_pvid != pvid)
868e66f840cSTristram Ha 		ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid);
869e66f840cSTristram Ha 
870e66f840cSTristram Ha 	return 0;
871e66f840cSTristram Ha }
872e66f840cSTristram Ha 
873e66f840cSTristram Ha static int ksz8795_port_mirror_add(struct dsa_switch *ds, int port,
874e66f840cSTristram Ha 				   struct dsa_mall_mirror_tc_entry *mirror,
875e66f840cSTristram Ha 				   bool ingress)
876e66f840cSTristram Ha {
877e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
878e66f840cSTristram Ha 
879e66f840cSTristram Ha 	if (ingress) {
880e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
881e66f840cSTristram Ha 		dev->mirror_rx |= BIT(port);
882e66f840cSTristram Ha 	} else {
883e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
884e66f840cSTristram Ha 		dev->mirror_tx |= BIT(port);
885e66f840cSTristram Ha 	}
886e66f840cSTristram Ha 
887e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
888e66f840cSTristram Ha 
889e66f840cSTristram Ha 	/* configure mirror port */
890e66f840cSTristram Ha 	if (dev->mirror_rx || dev->mirror_tx)
891e66f840cSTristram Ha 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
892e66f840cSTristram Ha 			     PORT_MIRROR_SNIFFER, true);
893e66f840cSTristram Ha 
894e66f840cSTristram Ha 	return 0;
895e66f840cSTristram Ha }
896e66f840cSTristram Ha 
897e66f840cSTristram Ha static void ksz8795_port_mirror_del(struct dsa_switch *ds, int port,
898e66f840cSTristram Ha 				    struct dsa_mall_mirror_tc_entry *mirror)
899e66f840cSTristram Ha {
900e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
901e66f840cSTristram Ha 	u8 data;
902e66f840cSTristram Ha 
903e66f840cSTristram Ha 	if (mirror->ingress) {
904e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
905e66f840cSTristram Ha 		dev->mirror_rx &= ~BIT(port);
906e66f840cSTristram Ha 	} else {
907e66f840cSTristram Ha 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
908e66f840cSTristram Ha 		dev->mirror_tx &= ~BIT(port);
909e66f840cSTristram Ha 	}
910e66f840cSTristram Ha 
911e66f840cSTristram Ha 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
912e66f840cSTristram Ha 
913e66f840cSTristram Ha 	if (!dev->mirror_rx && !dev->mirror_tx)
914e66f840cSTristram Ha 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
915e66f840cSTristram Ha 			     PORT_MIRROR_SNIFFER, false);
916e66f840cSTristram Ha }
917e66f840cSTristram Ha 
918e66f840cSTristram Ha static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port)
919e66f840cSTristram Ha {
920e66f840cSTristram Ha 	struct ksz_port *p = &dev->ports[port];
921e66f840cSTristram Ha 	u8 data8, member;
922e66f840cSTristram Ha 
923e66f840cSTristram Ha 	/* enable broadcast storm limit */
924e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
925e66f840cSTristram Ha 
926e66f840cSTristram Ha 	ksz8795_set_prio_queue(dev, port, 4);
927e66f840cSTristram Ha 
928e66f840cSTristram Ha 	/* disable DiffServ priority */
929e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
930e66f840cSTristram Ha 
931e66f840cSTristram Ha 	/* replace priority */
932e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_802_1P_CTRL, PORT_802_1P_REMAPPING, false);
933e66f840cSTristram Ha 
934e66f840cSTristram Ha 	/* enable 802.1p priority */
935e66f840cSTristram Ha 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
936e66f840cSTristram Ha 
937e66f840cSTristram Ha 	if (cpu_port) {
938edecfa98SHelmut Grohne 		if (!p->interface && dev->compat_interface) {
939edecfa98SHelmut Grohne 			dev_warn(dev->dev,
940edecfa98SHelmut Grohne 				 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
941edecfa98SHelmut Grohne 				 "Please update your device tree.\n",
942edecfa98SHelmut Grohne 				 port);
943edecfa98SHelmut Grohne 			p->interface = dev->compat_interface;
944edecfa98SHelmut Grohne 		}
945edecfa98SHelmut Grohne 
946e66f840cSTristram Ha 		/* Configure MII interface for proper network communication. */
947e66f840cSTristram Ha 		ksz_read8(dev, REG_PORT_5_CTRL_6, &data8);
948e66f840cSTristram Ha 		data8 &= ~PORT_INTERFACE_TYPE;
949e66f840cSTristram Ha 		data8 &= ~PORT_GMII_1GPS_MODE;
950edecfa98SHelmut Grohne 		switch (p->interface) {
951e66f840cSTristram Ha 		case PHY_INTERFACE_MODE_MII:
952e66f840cSTristram Ha 			p->phydev.speed = SPEED_100;
953e66f840cSTristram Ha 			break;
954e66f840cSTristram Ha 		case PHY_INTERFACE_MODE_RMII:
955e66f840cSTristram Ha 			data8 |= PORT_INTERFACE_RMII;
956e66f840cSTristram Ha 			p->phydev.speed = SPEED_100;
957e66f840cSTristram Ha 			break;
958e66f840cSTristram Ha 		case PHY_INTERFACE_MODE_GMII:
959e66f840cSTristram Ha 			data8 |= PORT_GMII_1GPS_MODE;
960e66f840cSTristram Ha 			data8 |= PORT_INTERFACE_GMII;
961e66f840cSTristram Ha 			p->phydev.speed = SPEED_1000;
962e66f840cSTristram Ha 			break;
963e66f840cSTristram Ha 		default:
964e66f840cSTristram Ha 			data8 &= ~PORT_RGMII_ID_IN_ENABLE;
965e66f840cSTristram Ha 			data8 &= ~PORT_RGMII_ID_OUT_ENABLE;
966edecfa98SHelmut Grohne 			if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
967edecfa98SHelmut Grohne 			    p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
968e66f840cSTristram Ha 				data8 |= PORT_RGMII_ID_IN_ENABLE;
969edecfa98SHelmut Grohne 			if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
970edecfa98SHelmut Grohne 			    p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
971e66f840cSTristram Ha 				data8 |= PORT_RGMII_ID_OUT_ENABLE;
972e66f840cSTristram Ha 			data8 |= PORT_GMII_1GPS_MODE;
973e66f840cSTristram Ha 			data8 |= PORT_INTERFACE_RGMII;
974e66f840cSTristram Ha 			p->phydev.speed = SPEED_1000;
975e66f840cSTristram Ha 			break;
976e66f840cSTristram Ha 		}
977e66f840cSTristram Ha 		ksz_write8(dev, REG_PORT_5_CTRL_6, data8);
978e66f840cSTristram Ha 		p->phydev.duplex = 1;
979e66f840cSTristram Ha 
980e66f840cSTristram Ha 		member = dev->port_mask;
981e66f840cSTristram Ha 	} else {
982e66f840cSTristram Ha 		member = dev->host_mask | p->vid_member;
983e66f840cSTristram Ha 	}
984e66f840cSTristram Ha 	ksz8795_cfg_port_member(dev, port, member);
985e66f840cSTristram Ha }
986e66f840cSTristram Ha 
987e66f840cSTristram Ha static void ksz8795_config_cpu_port(struct dsa_switch *ds)
988e66f840cSTristram Ha {
989e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
990e66f840cSTristram Ha 	struct ksz_port *p;
991e66f840cSTristram Ha 	u8 remote;
992e66f840cSTristram Ha 	int i;
993e66f840cSTristram Ha 
994e66f840cSTristram Ha 	/* Switch marks the maximum frame with extra byte as oversize. */
995e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true);
996e66f840cSTristram Ha 	ksz_cfg(dev, S_TAIL_TAG_CTRL, SW_TAIL_TAG_ENABLE, true);
997e66f840cSTristram Ha 
998e66f840cSTristram Ha 	p = &dev->ports[dev->cpu_port];
999e66f840cSTristram Ha 	p->vid_member = dev->port_mask;
1000e66f840cSTristram Ha 	p->on = 1;
1001e66f840cSTristram Ha 
1002e66f840cSTristram Ha 	ksz8795_port_setup(dev, dev->cpu_port, true);
1003e66f840cSTristram Ha 	dev->member = dev->host_mask;
1004e66f840cSTristram Ha 
10054ce2a984SMichael Grzeschik 	for (i = 0; i < dev->phy_port_cnt; i++) {
1006e66f840cSTristram Ha 		p = &dev->ports[i];
1007e66f840cSTristram Ha 
1008e66f840cSTristram Ha 		/* Initialize to non-zero so that ksz_cfg_port_member() will
1009e66f840cSTristram Ha 		 * be called.
1010e66f840cSTristram Ha 		 */
1011e66f840cSTristram Ha 		p->vid_member = BIT(i);
1012e66f840cSTristram Ha 		p->member = dev->port_mask;
1013e66f840cSTristram Ha 		ksz8795_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1014e66f840cSTristram Ha 
1015e66f840cSTristram Ha 		/* Last port may be disabled. */
10164ce2a984SMichael Grzeschik 		if (i == dev->phy_port_cnt)
1017e66f840cSTristram Ha 			break;
1018e66f840cSTristram Ha 		p->on = 1;
1019e66f840cSTristram Ha 		p->phy = 1;
1020e66f840cSTristram Ha 	}
1021e66f840cSTristram Ha 	for (i = 0; i < dev->phy_port_cnt; i++) {
1022e66f840cSTristram Ha 		p = &dev->ports[i];
1023e66f840cSTristram Ha 		if (!p->on)
1024e66f840cSTristram Ha 			continue;
1025e66f840cSTristram Ha 		ksz_pread8(dev, i, P_REMOTE_STATUS, &remote);
1026e66f840cSTristram Ha 		if (remote & PORT_FIBER_MODE)
1027e66f840cSTristram Ha 			p->fiber = 1;
1028e66f840cSTristram Ha 		if (p->fiber)
1029e66f840cSTristram Ha 			ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1030e66f840cSTristram Ha 				     true);
1031e66f840cSTristram Ha 		else
1032e66f840cSTristram Ha 			ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1033e66f840cSTristram Ha 				     false);
1034e66f840cSTristram Ha 	}
1035e66f840cSTristram Ha }
1036e66f840cSTristram Ha 
1037e66f840cSTristram Ha static int ksz8795_setup(struct dsa_switch *ds)
1038e66f840cSTristram Ha {
1039e66f840cSTristram Ha 	struct ksz_device *dev = ds->priv;
1040e66f840cSTristram Ha 	struct alu_struct alu;
1041e66f840cSTristram Ha 	int i, ret = 0;
1042e66f840cSTristram Ha 
1043e66f840cSTristram Ha 	dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1044e66f840cSTristram Ha 				       dev->num_vlans, GFP_KERNEL);
1045e66f840cSTristram Ha 	if (!dev->vlan_cache)
1046e66f840cSTristram Ha 		return -ENOMEM;
1047e66f840cSTristram Ha 
1048e66f840cSTristram Ha 	ret = ksz8795_reset_switch(dev);
1049e66f840cSTristram Ha 	if (ret) {
1050e66f840cSTristram Ha 		dev_err(ds->dev, "failed to reset switch\n");
1051e66f840cSTristram Ha 		return ret;
1052e66f840cSTristram Ha 	}
1053e66f840cSTristram Ha 
1054e66f840cSTristram Ha 	ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1055e66f840cSTristram Ha 
1056e66f840cSTristram Ha 	/* Enable automatic fast aging when link changed detected. */
1057e66f840cSTristram Ha 	ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1058e66f840cSTristram Ha 
1059e66f840cSTristram Ha 	/* Enable aggressive back off algorithm in half duplex mode. */
1060e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1,
1061e66f840cSTristram Ha 			   SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1062e66f840cSTristram Ha 
1063e66f840cSTristram Ha 	/*
1064e66f840cSTristram Ha 	 * Make sure unicast VLAN boundary is set as default and
1065e66f840cSTristram Ha 	 * enable no excessive collision drop.
1066e66f840cSTristram Ha 	 */
1067e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2,
1068e66f840cSTristram Ha 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1069e66f840cSTristram Ha 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1070e66f840cSTristram Ha 
1071e66f840cSTristram Ha 	ksz8795_config_cpu_port(ds);
1072e66f840cSTristram Ha 
1073e66f840cSTristram Ha 	ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true);
1074e66f840cSTristram Ha 
1075e66f840cSTristram Ha 	ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1076e66f840cSTristram Ha 
1077e66f840cSTristram Ha 	ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1078e66f840cSTristram Ha 
1079e66f840cSTristram Ha 	/* set broadcast storm protection 10% rate */
1080e66f840cSTristram Ha 	regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL,
1081e66f840cSTristram Ha 			   BROADCAST_STORM_RATE,
1082e66f840cSTristram Ha 			   (BROADCAST_STORM_VALUE *
1083e66f840cSTristram Ha 			   BROADCAST_STORM_PROT_RATE) / 100);
1084e66f840cSTristram Ha 
108502ffbb02SMichael Grzeschik 	for (i = 0; i < (dev->num_vlans / 4); i++)
1086e66f840cSTristram Ha 		ksz8795_r_vlan_entries(dev, i);
1087e66f840cSTristram Ha 
1088e66f840cSTristram Ha 	/* Setup STP address for STP operation. */
1089e66f840cSTristram Ha 	memset(&alu, 0, sizeof(alu));
1090e66f840cSTristram Ha 	ether_addr_copy(alu.mac, eth_stp_addr);
1091e66f840cSTristram Ha 	alu.is_static = true;
1092e66f840cSTristram Ha 	alu.is_override = true;
1093e66f840cSTristram Ha 	alu.port_forward = dev->host_mask;
1094e66f840cSTristram Ha 
1095e66f840cSTristram Ha 	ksz8795_w_sta_mac_table(dev, 0, &alu);
1096e66f840cSTristram Ha 
1097e66f840cSTristram Ha 	ksz_init_mib_timer(dev);
1098e66f840cSTristram Ha 
1099e66f840cSTristram Ha 	return 0;
1100e66f840cSTristram Ha }
1101e66f840cSTristram Ha 
1102e66f840cSTristram Ha static const struct dsa_switch_ops ksz8795_switch_ops = {
1103e66f840cSTristram Ha 	.get_tag_protocol	= ksz8795_get_tag_protocol,
1104e66f840cSTristram Ha 	.setup			= ksz8795_setup,
1105e66f840cSTristram Ha 	.phy_read		= ksz_phy_read16,
1106e66f840cSTristram Ha 	.phy_write		= ksz_phy_write16,
1107143a102eSCodrin Ciubotariu 	.phylink_mac_link_down	= ksz_mac_link_down,
1108e66f840cSTristram Ha 	.port_enable		= ksz_enable_port,
1109e66f840cSTristram Ha 	.get_strings		= ksz8795_get_strings,
1110e66f840cSTristram Ha 	.get_ethtool_stats	= ksz_get_ethtool_stats,
1111e66f840cSTristram Ha 	.get_sset_count		= ksz_sset_count,
1112e66f840cSTristram Ha 	.port_bridge_join	= ksz_port_bridge_join,
1113e66f840cSTristram Ha 	.port_bridge_leave	= ksz_port_bridge_leave,
1114e66f840cSTristram Ha 	.port_stp_state_set	= ksz8795_port_stp_state_set,
1115e66f840cSTristram Ha 	.port_fast_age		= ksz_port_fast_age,
1116e66f840cSTristram Ha 	.port_vlan_filtering	= ksz8795_port_vlan_filtering,
1117e66f840cSTristram Ha 	.port_vlan_prepare	= ksz_port_vlan_prepare,
1118e66f840cSTristram Ha 	.port_vlan_add		= ksz8795_port_vlan_add,
1119e66f840cSTristram Ha 	.port_vlan_del		= ksz8795_port_vlan_del,
1120e66f840cSTristram Ha 	.port_fdb_dump		= ksz_port_fdb_dump,
1121e66f840cSTristram Ha 	.port_mdb_prepare       = ksz_port_mdb_prepare,
1122e66f840cSTristram Ha 	.port_mdb_add           = ksz_port_mdb_add,
1123e66f840cSTristram Ha 	.port_mdb_del           = ksz_port_mdb_del,
1124e66f840cSTristram Ha 	.port_mirror_add	= ksz8795_port_mirror_add,
1125e66f840cSTristram Ha 	.port_mirror_del	= ksz8795_port_mirror_del,
1126e66f840cSTristram Ha };
1127e66f840cSTristram Ha 
1128e66f840cSTristram Ha static u32 ksz8795_get_port_addr(int port, int offset)
1129e66f840cSTristram Ha {
1130e66f840cSTristram Ha 	return PORT_CTRL_ADDR(port, offset);
1131e66f840cSTristram Ha }
1132e66f840cSTristram Ha 
1133e66f840cSTristram Ha static int ksz8795_switch_detect(struct ksz_device *dev)
1134e66f840cSTristram Ha {
1135e66f840cSTristram Ha 	u8 id1, id2;
1136e66f840cSTristram Ha 	u16 id16;
1137e66f840cSTristram Ha 	int ret;
1138e66f840cSTristram Ha 
1139e66f840cSTristram Ha 	/* read chip id */
1140e66f840cSTristram Ha 	ret = ksz_read16(dev, REG_CHIP_ID0, &id16);
1141e66f840cSTristram Ha 	if (ret)
1142e66f840cSTristram Ha 		return ret;
1143e66f840cSTristram Ha 
1144e66f840cSTristram Ha 	id1 = id16 >> 8;
1145e66f840cSTristram Ha 	id2 = id16 & SW_CHIP_ID_M;
1146e66f840cSTristram Ha 	if (id1 != FAMILY_ID ||
1147e66f840cSTristram Ha 	    (id2 != CHIP_ID_94 && id2 != CHIP_ID_95))
1148e66f840cSTristram Ha 		return -ENODEV;
1149e66f840cSTristram Ha 
1150e66f840cSTristram Ha 	if (id2 == CHIP_ID_95) {
1151e66f840cSTristram Ha 		u8 val;
1152e66f840cSTristram Ha 
1153e66f840cSTristram Ha 		id2 = 0x95;
1154e66f840cSTristram Ha 		ksz_read8(dev, REG_PORT_1_STATUS_0, &val);
1155e66f840cSTristram Ha 		if (val & PORT_FIBER_MODE)
1156e66f840cSTristram Ha 			id2 = 0x65;
1157e66f840cSTristram Ha 	} else if (id2 == CHIP_ID_94) {
1158e66f840cSTristram Ha 		id2 = 0x94;
1159e66f840cSTristram Ha 	}
1160e66f840cSTristram Ha 	id16 &= ~0xff;
1161e66f840cSTristram Ha 	id16 |= id2;
1162e66f840cSTristram Ha 	dev->chip_id = id16;
1163e66f840cSTristram Ha 
1164e66f840cSTristram Ha 	return 0;
1165e66f840cSTristram Ha }
1166e66f840cSTristram Ha 
1167e66f840cSTristram Ha struct ksz_chip_data {
1168e66f840cSTristram Ha 	u16 chip_id;
1169e66f840cSTristram Ha 	const char *dev_name;
1170e66f840cSTristram Ha 	int num_vlans;
1171e66f840cSTristram Ha 	int num_alus;
1172e66f840cSTristram Ha 	int num_statics;
1173e66f840cSTristram Ha 	int cpu_ports;
1174e66f840cSTristram Ha 	int port_cnt;
1175e66f840cSTristram Ha };
1176e66f840cSTristram Ha 
1177e66f840cSTristram Ha static const struct ksz_chip_data ksz8795_switch_chips[] = {
1178e66f840cSTristram Ha 	{
1179e66f840cSTristram Ha 		.chip_id = 0x8795,
1180e66f840cSTristram Ha 		.dev_name = "KSZ8795",
1181e66f840cSTristram Ha 		.num_vlans = 4096,
1182e66f840cSTristram Ha 		.num_alus = 0,
1183e66f840cSTristram Ha 		.num_statics = 8,
1184e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
118594374dd1SMichael Grzeschik 		.port_cnt = 5,		/* total cpu and user ports */
1186e66f840cSTristram Ha 	},
1187e66f840cSTristram Ha 	{
1188e66f840cSTristram Ha 		.chip_id = 0x8794,
1189e66f840cSTristram Ha 		.dev_name = "KSZ8794",
1190e66f840cSTristram Ha 		.num_vlans = 4096,
1191e66f840cSTristram Ha 		.num_alus = 0,
1192e66f840cSTristram Ha 		.num_statics = 8,
1193e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
119494374dd1SMichael Grzeschik 		.port_cnt = 4,		/* total cpu and user ports */
1195e66f840cSTristram Ha 	},
1196e66f840cSTristram Ha 	{
1197e66f840cSTristram Ha 		.chip_id = 0x8765,
1198e66f840cSTristram Ha 		.dev_name = "KSZ8765",
1199e66f840cSTristram Ha 		.num_vlans = 4096,
1200e66f840cSTristram Ha 		.num_alus = 0,
1201e66f840cSTristram Ha 		.num_statics = 8,
1202e66f840cSTristram Ha 		.cpu_ports = 0x10,	/* can be configured as cpu port */
120394374dd1SMichael Grzeschik 		.port_cnt = 5,		/* total cpu and user ports */
1204e66f840cSTristram Ha 	},
1205e66f840cSTristram Ha };
1206e66f840cSTristram Ha 
1207e66f840cSTristram Ha static int ksz8795_switch_init(struct ksz_device *dev)
1208e66f840cSTristram Ha {
1209e66f840cSTristram Ha 	int i;
1210e66f840cSTristram Ha 
1211e66f840cSTristram Ha 	dev->ds->ops = &ksz8795_switch_ops;
1212e66f840cSTristram Ha 
1213e66f840cSTristram Ha 	for (i = 0; i < ARRAY_SIZE(ksz8795_switch_chips); i++) {
1214e66f840cSTristram Ha 		const struct ksz_chip_data *chip = &ksz8795_switch_chips[i];
1215e66f840cSTristram Ha 
1216e66f840cSTristram Ha 		if (dev->chip_id == chip->chip_id) {
1217e66f840cSTristram Ha 			dev->name = chip->dev_name;
1218e66f840cSTristram Ha 			dev->num_vlans = chip->num_vlans;
1219e66f840cSTristram Ha 			dev->num_alus = chip->num_alus;
1220e66f840cSTristram Ha 			dev->num_statics = chip->num_statics;
1221e66f840cSTristram Ha 			dev->port_cnt = chip->port_cnt;
1222e66f840cSTristram Ha 			dev->cpu_ports = chip->cpu_ports;
1223e66f840cSTristram Ha 
1224e66f840cSTristram Ha 			break;
1225e66f840cSTristram Ha 		}
1226e66f840cSTristram Ha 	}
1227e66f840cSTristram Ha 
1228e66f840cSTristram Ha 	/* no switch found */
1229e66f840cSTristram Ha 	if (!dev->cpu_ports)
1230e66f840cSTristram Ha 		return -ENODEV;
1231e66f840cSTristram Ha 
1232e66f840cSTristram Ha 	dev->port_mask = BIT(dev->port_cnt) - 1;
1233e66f840cSTristram Ha 	dev->port_mask |= dev->host_mask;
1234e66f840cSTristram Ha 
123531b62c78SMichael Grzeschik 	dev->reg_mib_cnt = KSZ8795_COUNTER_NUM;
123665fe1acfSMichael Grzeschik 	dev->mib_cnt = ARRAY_SIZE(mib_names);
1237e66f840cSTristram Ha 
123894374dd1SMichael Grzeschik 	dev->phy_port_cnt = dev->port_cnt - 1;
12397fc32b41SMichael Grzeschik 
1240c9f4633bSMichael Grzeschik 	dev->cpu_port = dev->port_cnt - 1;
12417fc32b41SMichael Grzeschik 	dev->host_mask = BIT(dev->cpu_port);
12427fc32b41SMichael Grzeschik 
1243c9f4633bSMichael Grzeschik 	dev->ports = devm_kzalloc(dev->dev,
1244c9f4633bSMichael Grzeschik 				  dev->port_cnt * sizeof(struct ksz_port),
1245e66f840cSTristram Ha 				  GFP_KERNEL);
1246e66f840cSTristram Ha 	if (!dev->ports)
1247e66f840cSTristram Ha 		return -ENOMEM;
1248c9f4633bSMichael Grzeschik 	for (i = 0; i < dev->port_cnt; i++) {
1249e66f840cSTristram Ha 		mutex_init(&dev->ports[i].mib.cnt_mutex);
1250e66f840cSTristram Ha 		dev->ports[i].mib.counters =
1251e66f840cSTristram Ha 			devm_kzalloc(dev->dev,
1252e66f840cSTristram Ha 				     sizeof(u64) *
125365fe1acfSMichael Grzeschik 				     (dev->mib_cnt + 1),
1254e66f840cSTristram Ha 				     GFP_KERNEL);
1255e66f840cSTristram Ha 		if (!dev->ports[i].mib.counters)
1256e66f840cSTristram Ha 			return -ENOMEM;
1257e66f840cSTristram Ha 	}
1258e66f840cSTristram Ha 
1259af199a1aSCodrin Ciubotariu 	/* set the real number of ports */
126094374dd1SMichael Grzeschik 	dev->ds->num_ports = dev->port_cnt;
1261af199a1aSCodrin Ciubotariu 
1262e66f840cSTristram Ha 	return 0;
1263e66f840cSTristram Ha }
1264e66f840cSTristram Ha 
1265e66f840cSTristram Ha static void ksz8795_switch_exit(struct ksz_device *dev)
1266e66f840cSTristram Ha {
1267e66f840cSTristram Ha 	ksz8795_reset_switch(dev);
1268e66f840cSTristram Ha }
1269e66f840cSTristram Ha 
1270e66f840cSTristram Ha static const struct ksz_dev_ops ksz8795_dev_ops = {
1271e66f840cSTristram Ha 	.get_port_addr = ksz8795_get_port_addr,
1272e66f840cSTristram Ha 	.cfg_port_member = ksz8795_cfg_port_member,
1273e66f840cSTristram Ha 	.flush_dyn_mac_table = ksz8795_flush_dyn_mac_table,
1274e66f840cSTristram Ha 	.port_setup = ksz8795_port_setup,
1275e66f840cSTristram Ha 	.r_phy = ksz8795_r_phy,
1276e66f840cSTristram Ha 	.w_phy = ksz8795_w_phy,
1277e66f840cSTristram Ha 	.r_dyn_mac_table = ksz8795_r_dyn_mac_table,
1278e66f840cSTristram Ha 	.r_sta_mac_table = ksz8795_r_sta_mac_table,
1279e66f840cSTristram Ha 	.w_sta_mac_table = ksz8795_w_sta_mac_table,
1280e66f840cSTristram Ha 	.r_mib_cnt = ksz8795_r_mib_cnt,
1281e66f840cSTristram Ha 	.r_mib_pkt = ksz8795_r_mib_pkt,
1282e66f840cSTristram Ha 	.freeze_mib = ksz8795_freeze_mib,
1283e66f840cSTristram Ha 	.port_init_cnt = ksz8795_port_init_cnt,
1284e66f840cSTristram Ha 	.shutdown = ksz8795_reset_switch,
1285e66f840cSTristram Ha 	.detect = ksz8795_switch_detect,
1286e66f840cSTristram Ha 	.init = ksz8795_switch_init,
1287e66f840cSTristram Ha 	.exit = ksz8795_switch_exit,
1288e66f840cSTristram Ha };
1289e66f840cSTristram Ha 
1290e66f840cSTristram Ha int ksz8795_switch_register(struct ksz_device *dev)
1291e66f840cSTristram Ha {
1292e66f840cSTristram Ha 	return ksz_switch_register(dev, &ksz8795_dev_ops);
1293e66f840cSTristram Ha }
1294e66f840cSTristram Ha EXPORT_SYMBOL(ksz8795_switch_register);
1295e66f840cSTristram Ha 
1296e66f840cSTristram Ha MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1297e66f840cSTristram Ha MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1298e66f840cSTristram Ha MODULE_LICENSE("GPL");
1299