1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip KSZ9477 switch driver main logic
4  *
5  * Copyright (C) 2017-2019 Microchip Technology Inc.
6  */
7 
8 #include <linux/kernel.h>
9 #include <linux/module.h>
10 #include <linux/iopoll.h>
11 #include <linux/platform_data/microchip-ksz.h>
12 #include <linux/phy.h>
13 #include <linux/if_bridge.h>
14 #include <linux/if_vlan.h>
15 #include <net/dsa.h>
16 #include <net/switchdev.h>
17 
18 #include "ksz9477_reg.h"
19 #include "ksz_common.h"
20 #include "ksz9477.h"
21 
22 /* Used with variable features to indicate capabilities. */
23 #define GBIT_SUPPORT			BIT(0)
24 #define NEW_XMII			BIT(1)
25 #define IS_9893				BIT(2)
26 
27 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
28 {
29 	regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
30 }
31 
32 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
33 			 bool set)
34 {
35 	regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
36 			   bits, set ? bits : 0);
37 }
38 
39 static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
40 {
41 	regmap_update_bits(dev->regmap[2], addr, bits, set ? bits : 0);
42 }
43 
44 static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
45 			       u32 bits, bool set)
46 {
47 	regmap_update_bits(dev->regmap[2], PORT_CTRL_ADDR(port, offset),
48 			   bits, set ? bits : 0);
49 }
50 
51 int ksz9477_change_mtu(struct ksz_device *dev, int port, int mtu)
52 {
53 	u16 frame_size, max_frame = 0;
54 	int i;
55 
56 	frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
57 
58 	/* Cache the per-port MTU setting */
59 	dev->ports[port].max_frame = frame_size;
60 
61 	for (i = 0; i < dev->info->port_cnt; i++)
62 		max_frame = max(max_frame, dev->ports[i].max_frame);
63 
64 	return regmap_update_bits(dev->regmap[1], REG_SW_MTU__2,
65 				  REG_SW_MTU_MASK, max_frame);
66 }
67 
68 int ksz9477_max_mtu(struct ksz_device *dev, int port)
69 {
70 	return KSZ9477_MAX_FRAME_SIZE - VLAN_ETH_HLEN - ETH_FCS_LEN;
71 }
72 
73 static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
74 {
75 	unsigned int val;
76 
77 	return regmap_read_poll_timeout(dev->regmap[0], REG_SW_VLAN_CTRL,
78 					val, !(val & VLAN_START), 10, 1000);
79 }
80 
81 static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
82 				  u32 *vlan_table)
83 {
84 	int ret;
85 
86 	mutex_lock(&dev->vlan_mutex);
87 
88 	ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
89 	ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
90 
91 	/* wait to be cleared */
92 	ret = ksz9477_wait_vlan_ctrl_ready(dev);
93 	if (ret) {
94 		dev_dbg(dev->dev, "Failed to read vlan table\n");
95 		goto exit;
96 	}
97 
98 	ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]);
99 	ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]);
100 	ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]);
101 
102 	ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
103 
104 exit:
105 	mutex_unlock(&dev->vlan_mutex);
106 
107 	return ret;
108 }
109 
110 static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
111 				  u32 *vlan_table)
112 {
113 	int ret;
114 
115 	mutex_lock(&dev->vlan_mutex);
116 
117 	ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]);
118 	ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]);
119 	ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]);
120 
121 	ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
122 	ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
123 
124 	/* wait to be cleared */
125 	ret = ksz9477_wait_vlan_ctrl_ready(dev);
126 	if (ret) {
127 		dev_dbg(dev->dev, "Failed to write vlan table\n");
128 		goto exit;
129 	}
130 
131 	ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
132 
133 	/* update vlan cache table */
134 	dev->vlan_cache[vid].table[0] = vlan_table[0];
135 	dev->vlan_cache[vid].table[1] = vlan_table[1];
136 	dev->vlan_cache[vid].table[2] = vlan_table[2];
137 
138 exit:
139 	mutex_unlock(&dev->vlan_mutex);
140 
141 	return ret;
142 }
143 
144 static void ksz9477_read_table(struct ksz_device *dev, u32 *table)
145 {
146 	ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
147 	ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
148 	ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
149 	ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
150 }
151 
152 static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
153 {
154 	ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
155 	ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
156 	ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
157 	ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
158 }
159 
160 static int ksz9477_wait_alu_ready(struct ksz_device *dev)
161 {
162 	unsigned int val;
163 
164 	return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_CTRL__4,
165 					val, !(val & ALU_START), 10, 1000);
166 }
167 
168 static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
169 {
170 	unsigned int val;
171 
172 	return regmap_read_poll_timeout(dev->regmap[2],
173 					REG_SW_ALU_STAT_CTRL__4,
174 					val, !(val & ALU_STAT_START),
175 					10, 1000);
176 }
177 
178 int ksz9477_reset_switch(struct ksz_device *dev)
179 {
180 	u8 data8;
181 	u32 data32;
182 
183 	/* reset switch */
184 	ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
185 
186 	/* turn off SPI DO Edge select */
187 	regmap_update_bits(dev->regmap[0], REG_SW_GLOBAL_SERIAL_CTRL_0,
188 			   SPI_AUTO_EDGE_DETECTION, 0);
189 
190 	/* default configuration */
191 	ksz_read8(dev, REG_SW_LUE_CTRL_1, &data8);
192 	data8 = SW_AGING_ENABLE | SW_LINK_AUTO_AGING |
193 	      SW_SRC_ADDR_FILTER | SW_FLUSH_STP_TABLE | SW_FLUSH_MSTP_TABLE;
194 	ksz_write8(dev, REG_SW_LUE_CTRL_1, data8);
195 
196 	/* disable interrupts */
197 	ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
198 	ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F);
199 	ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
200 
201 	data8 = SW_ENABLE_REFCLKO;
202 	if (dev->synclko_disable)
203 		data8 = 0;
204 	else if (dev->synclko_125)
205 		data8 = SW_ENABLE_REFCLKO | SW_REFCLKO_IS_125MHZ;
206 	ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1, data8);
207 
208 	return 0;
209 }
210 
211 void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
212 {
213 	struct ksz_port *p = &dev->ports[port];
214 	unsigned int val;
215 	u32 data;
216 	int ret;
217 
218 	/* retain the flush/freeze bit */
219 	data = p->freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
220 	data |= MIB_COUNTER_READ;
221 	data |= (addr << MIB_COUNTER_INDEX_S);
222 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
223 
224 	ret = regmap_read_poll_timeout(dev->regmap[2],
225 			PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
226 			val, !(val & MIB_COUNTER_READ), 10, 1000);
227 	/* failed to read MIB. get out of loop */
228 	if (ret) {
229 		dev_dbg(dev->dev, "Failed to get MIB\n");
230 		return;
231 	}
232 
233 	/* count resets upon read */
234 	ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data);
235 	*cnt += data;
236 }
237 
238 void ksz9477_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
239 		       u64 *dropped, u64 *cnt)
240 {
241 	addr = dev->info->mib_names[addr].index;
242 	ksz9477_r_mib_cnt(dev, port, addr, cnt);
243 }
244 
245 void ksz9477_freeze_mib(struct ksz_device *dev, int port, bool freeze)
246 {
247 	u32 val = freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
248 	struct ksz_port *p = &dev->ports[port];
249 
250 	/* enable/disable the port for flush/freeze function */
251 	mutex_lock(&p->mib.cnt_mutex);
252 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, val);
253 
254 	/* used by MIB counter reading code to know freeze is enabled */
255 	p->freeze = freeze;
256 	mutex_unlock(&p->mib.cnt_mutex);
257 }
258 
259 void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
260 {
261 	struct ksz_port_mib *mib = &dev->ports[port].mib;
262 
263 	/* flush all enabled port MIB counters */
264 	mutex_lock(&mib->cnt_mutex);
265 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4,
266 		     MIB_COUNTER_FLUSH_FREEZE);
267 	ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
268 	ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
269 	mutex_unlock(&mib->cnt_mutex);
270 }
271 
272 void ksz9477_r_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 *data)
273 {
274 	u16 val = 0xffff;
275 
276 	/* No real PHY after this. Simulate the PHY.
277 	 * A fixed PHY can be setup in the device tree, but this function is
278 	 * still called for that port during initialization.
279 	 * For RGMII PHY there is no way to access it so the fixed PHY should
280 	 * be used.  For SGMII PHY the supporting code will be added later.
281 	 */
282 	if (addr >= dev->phy_port_cnt) {
283 		struct ksz_port *p = &dev->ports[addr];
284 
285 		switch (reg) {
286 		case MII_BMCR:
287 			val = 0x1140;
288 			break;
289 		case MII_BMSR:
290 			val = 0x796d;
291 			break;
292 		case MII_PHYSID1:
293 			val = 0x0022;
294 			break;
295 		case MII_PHYSID2:
296 			val = 0x1631;
297 			break;
298 		case MII_ADVERTISE:
299 			val = 0x05e1;
300 			break;
301 		case MII_LPA:
302 			val = 0xc5e1;
303 			break;
304 		case MII_CTRL1000:
305 			val = 0x0700;
306 			break;
307 		case MII_STAT1000:
308 			if (p->phydev.speed == SPEED_1000)
309 				val = 0x3800;
310 			else
311 				val = 0;
312 			break;
313 		}
314 	} else {
315 		ksz_pread16(dev, addr, 0x100 + (reg << 1), &val);
316 	}
317 
318 	*data = val;
319 }
320 
321 void ksz9477_w_phy(struct ksz_device *dev, u16 addr, u16 reg, u16 val)
322 {
323 	/* No real PHY after this. */
324 	if (addr >= dev->phy_port_cnt)
325 		return;
326 
327 	/* No gigabit support.  Do not write to this register. */
328 	if (!(dev->features & GBIT_SUPPORT) && reg == MII_CTRL1000)
329 		return;
330 
331 	ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val);
332 }
333 
334 void ksz9477_cfg_port_member(struct ksz_device *dev, int port, u8 member)
335 {
336 	ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member);
337 }
338 
339 void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
340 {
341 	const u16 *regs = dev->info->regs;
342 	u8 data;
343 
344 	regmap_update_bits(dev->regmap[0], REG_SW_LUE_CTRL_2,
345 			   SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S,
346 			   SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
347 
348 	if (port < dev->info->port_cnt) {
349 		/* flush individual port */
350 		ksz_pread8(dev, port, regs[P_STP_CTRL], &data);
351 		if (!(data & PORT_LEARN_DISABLE))
352 			ksz_pwrite8(dev, port, regs[P_STP_CTRL],
353 				    data | PORT_LEARN_DISABLE);
354 		ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
355 		ksz_pwrite8(dev, port, regs[P_STP_CTRL], data);
356 	} else {
357 		/* flush all */
358 		ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true);
359 	}
360 }
361 
362 int ksz9477_port_vlan_filtering(struct ksz_device *dev, int port,
363 				bool flag, struct netlink_ext_ack *extack)
364 {
365 	if (flag) {
366 		ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
367 			     PORT_VLAN_LOOKUP_VID_0, true);
368 		ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true);
369 	} else {
370 		ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false);
371 		ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
372 			     PORT_VLAN_LOOKUP_VID_0, false);
373 	}
374 
375 	return 0;
376 }
377 
378 int ksz9477_port_vlan_add(struct ksz_device *dev, int port,
379 			  const struct switchdev_obj_port_vlan *vlan,
380 			  struct netlink_ext_ack *extack)
381 {
382 	u32 vlan_table[3];
383 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
384 	int err;
385 
386 	err = ksz9477_get_vlan_table(dev, vlan->vid, vlan_table);
387 	if (err) {
388 		NL_SET_ERR_MSG_MOD(extack, "Failed to get vlan table");
389 		return err;
390 	}
391 
392 	vlan_table[0] = VLAN_VALID | (vlan->vid & VLAN_FID_M);
393 	if (untagged)
394 		vlan_table[1] |= BIT(port);
395 	else
396 		vlan_table[1] &= ~BIT(port);
397 	vlan_table[1] &= ~(BIT(dev->cpu_port));
398 
399 	vlan_table[2] |= BIT(port) | BIT(dev->cpu_port);
400 
401 	err = ksz9477_set_vlan_table(dev, vlan->vid, vlan_table);
402 	if (err) {
403 		NL_SET_ERR_MSG_MOD(extack, "Failed to set vlan table");
404 		return err;
405 	}
406 
407 	/* change PVID */
408 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
409 		ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vlan->vid);
410 
411 	return 0;
412 }
413 
414 int ksz9477_port_vlan_del(struct ksz_device *dev, int port,
415 			  const struct switchdev_obj_port_vlan *vlan)
416 {
417 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
418 	u32 vlan_table[3];
419 	u16 pvid;
420 
421 	ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
422 	pvid = pvid & 0xFFF;
423 
424 	if (ksz9477_get_vlan_table(dev, vlan->vid, vlan_table)) {
425 		dev_dbg(dev->dev, "Failed to get vlan table\n");
426 		return -ETIMEDOUT;
427 	}
428 
429 	vlan_table[2] &= ~BIT(port);
430 
431 	if (pvid == vlan->vid)
432 		pvid = 1;
433 
434 	if (untagged)
435 		vlan_table[1] &= ~BIT(port);
436 
437 	if (ksz9477_set_vlan_table(dev, vlan->vid, vlan_table)) {
438 		dev_dbg(dev->dev, "Failed to set vlan table\n");
439 		return -ETIMEDOUT;
440 	}
441 
442 	ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
443 
444 	return 0;
445 }
446 
447 int ksz9477_fdb_add(struct ksz_device *dev, int port,
448 		    const unsigned char *addr, u16 vid, struct dsa_db db)
449 {
450 	u32 alu_table[4];
451 	u32 data;
452 	int ret = 0;
453 
454 	mutex_lock(&dev->alu_mutex);
455 
456 	/* find any entry with mac & vid */
457 	data = vid << ALU_FID_INDEX_S;
458 	data |= ((addr[0] << 8) | addr[1]);
459 	ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
460 
461 	data = ((addr[2] << 24) | (addr[3] << 16));
462 	data |= ((addr[4] << 8) | addr[5]);
463 	ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
464 
465 	/* start read operation */
466 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
467 
468 	/* wait to be finished */
469 	ret = ksz9477_wait_alu_ready(dev);
470 	if (ret) {
471 		dev_dbg(dev->dev, "Failed to read ALU\n");
472 		goto exit;
473 	}
474 
475 	/* read ALU entry */
476 	ksz9477_read_table(dev, alu_table);
477 
478 	/* update ALU entry */
479 	alu_table[0] = ALU_V_STATIC_VALID;
480 	alu_table[1] |= BIT(port);
481 	if (vid)
482 		alu_table[1] |= ALU_V_USE_FID;
483 	alu_table[2] = (vid << ALU_V_FID_S);
484 	alu_table[2] |= ((addr[0] << 8) | addr[1]);
485 	alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
486 	alu_table[3] |= ((addr[4] << 8) | addr[5]);
487 
488 	ksz9477_write_table(dev, alu_table);
489 
490 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
491 
492 	/* wait to be finished */
493 	ret = ksz9477_wait_alu_ready(dev);
494 	if (ret)
495 		dev_dbg(dev->dev, "Failed to write ALU\n");
496 
497 exit:
498 	mutex_unlock(&dev->alu_mutex);
499 
500 	return ret;
501 }
502 
503 int ksz9477_fdb_del(struct ksz_device *dev, int port,
504 		    const unsigned char *addr, u16 vid, struct dsa_db db)
505 {
506 	u32 alu_table[4];
507 	u32 data;
508 	int ret = 0;
509 
510 	mutex_lock(&dev->alu_mutex);
511 
512 	/* read any entry with mac & vid */
513 	data = vid << ALU_FID_INDEX_S;
514 	data |= ((addr[0] << 8) | addr[1]);
515 	ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
516 
517 	data = ((addr[2] << 24) | (addr[3] << 16));
518 	data |= ((addr[4] << 8) | addr[5]);
519 	ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
520 
521 	/* start read operation */
522 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
523 
524 	/* wait to be finished */
525 	ret = ksz9477_wait_alu_ready(dev);
526 	if (ret) {
527 		dev_dbg(dev->dev, "Failed to read ALU\n");
528 		goto exit;
529 	}
530 
531 	ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
532 	if (alu_table[0] & ALU_V_STATIC_VALID) {
533 		ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]);
534 		ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]);
535 		ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]);
536 
537 		/* clear forwarding port */
538 		alu_table[2] &= ~BIT(port);
539 
540 		/* if there is no port to forward, clear table */
541 		if ((alu_table[2] & ALU_V_PORT_MAP) == 0) {
542 			alu_table[0] = 0;
543 			alu_table[1] = 0;
544 			alu_table[2] = 0;
545 			alu_table[3] = 0;
546 		}
547 	} else {
548 		alu_table[0] = 0;
549 		alu_table[1] = 0;
550 		alu_table[2] = 0;
551 		alu_table[3] = 0;
552 	}
553 
554 	ksz9477_write_table(dev, alu_table);
555 
556 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
557 
558 	/* wait to be finished */
559 	ret = ksz9477_wait_alu_ready(dev);
560 	if (ret)
561 		dev_dbg(dev->dev, "Failed to write ALU\n");
562 
563 exit:
564 	mutex_unlock(&dev->alu_mutex);
565 
566 	return ret;
567 }
568 
569 static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table)
570 {
571 	alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
572 	alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
573 	alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
574 	alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
575 			ALU_V_PRIO_AGE_CNT_M;
576 	alu->mstp = alu_table[0] & ALU_V_MSTP_M;
577 
578 	alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
579 	alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
580 	alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
581 
582 	alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
583 
584 	alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
585 	alu->mac[1] = alu_table[2] & 0xFF;
586 	alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
587 	alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
588 	alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
589 	alu->mac[5] = alu_table[3] & 0xFF;
590 }
591 
592 int ksz9477_fdb_dump(struct ksz_device *dev, int port,
593 		     dsa_fdb_dump_cb_t *cb, void *data)
594 {
595 	int ret = 0;
596 	u32 ksz_data;
597 	u32 alu_table[4];
598 	struct alu_struct alu;
599 	int timeout;
600 
601 	mutex_lock(&dev->alu_mutex);
602 
603 	/* start ALU search */
604 	ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH);
605 
606 	do {
607 		timeout = 1000;
608 		do {
609 			ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data);
610 			if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START))
611 				break;
612 			usleep_range(1, 10);
613 		} while (timeout-- > 0);
614 
615 		if (!timeout) {
616 			dev_dbg(dev->dev, "Failed to search ALU\n");
617 			ret = -ETIMEDOUT;
618 			goto exit;
619 		}
620 
621 		/* read ALU table */
622 		ksz9477_read_table(dev, alu_table);
623 
624 		ksz9477_convert_alu(&alu, alu_table);
625 
626 		if (alu.port_forward & BIT(port)) {
627 			ret = cb(alu.mac, alu.fid, alu.is_static, data);
628 			if (ret)
629 				goto exit;
630 		}
631 	} while (ksz_data & ALU_START);
632 
633 exit:
634 
635 	/* stop ALU search */
636 	ksz_write32(dev, REG_SW_ALU_CTRL__4, 0);
637 
638 	mutex_unlock(&dev->alu_mutex);
639 
640 	return ret;
641 }
642 
643 int ksz9477_mdb_add(struct ksz_device *dev, int port,
644 		    const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
645 {
646 	u32 static_table[4];
647 	u32 data;
648 	int index;
649 	u32 mac_hi, mac_lo;
650 	int err = 0;
651 
652 	mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
653 	mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
654 	mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
655 
656 	mutex_lock(&dev->alu_mutex);
657 
658 	for (index = 0; index < dev->info->num_statics; index++) {
659 		/* find empty slot first */
660 		data = (index << ALU_STAT_INDEX_S) |
661 			ALU_STAT_READ | ALU_STAT_START;
662 		ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
663 
664 		/* wait to be finished */
665 		err = ksz9477_wait_alu_sta_ready(dev);
666 		if (err) {
667 			dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
668 			goto exit;
669 		}
670 
671 		/* read ALU static table */
672 		ksz9477_read_table(dev, static_table);
673 
674 		if (static_table[0] & ALU_V_STATIC_VALID) {
675 			/* check this has same vid & mac address */
676 			if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
677 			    ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
678 			    static_table[3] == mac_lo) {
679 				/* found matching one */
680 				break;
681 			}
682 		} else {
683 			/* found empty one */
684 			break;
685 		}
686 	}
687 
688 	/* no available entry */
689 	if (index == dev->info->num_statics) {
690 		err = -ENOSPC;
691 		goto exit;
692 	}
693 
694 	/* add entry */
695 	static_table[0] = ALU_V_STATIC_VALID;
696 	static_table[1] |= BIT(port);
697 	if (mdb->vid)
698 		static_table[1] |= ALU_V_USE_FID;
699 	static_table[2] = (mdb->vid << ALU_V_FID_S);
700 	static_table[2] |= mac_hi;
701 	static_table[3] = mac_lo;
702 
703 	ksz9477_write_table(dev, static_table);
704 
705 	data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
706 	ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
707 
708 	/* wait to be finished */
709 	if (ksz9477_wait_alu_sta_ready(dev))
710 		dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
711 
712 exit:
713 	mutex_unlock(&dev->alu_mutex);
714 	return err;
715 }
716 
717 int ksz9477_mdb_del(struct ksz_device *dev, int port,
718 		    const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
719 {
720 	u32 static_table[4];
721 	u32 data;
722 	int index;
723 	int ret = 0;
724 	u32 mac_hi, mac_lo;
725 
726 	mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
727 	mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
728 	mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
729 
730 	mutex_lock(&dev->alu_mutex);
731 
732 	for (index = 0; index < dev->info->num_statics; index++) {
733 		/* find empty slot first */
734 		data = (index << ALU_STAT_INDEX_S) |
735 			ALU_STAT_READ | ALU_STAT_START;
736 		ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
737 
738 		/* wait to be finished */
739 		ret = ksz9477_wait_alu_sta_ready(dev);
740 		if (ret) {
741 			dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
742 			goto exit;
743 		}
744 
745 		/* read ALU static table */
746 		ksz9477_read_table(dev, static_table);
747 
748 		if (static_table[0] & ALU_V_STATIC_VALID) {
749 			/* check this has same vid & mac address */
750 
751 			if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
752 			    ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
753 			    static_table[3] == mac_lo) {
754 				/* found matching one */
755 				break;
756 			}
757 		}
758 	}
759 
760 	/* no available entry */
761 	if (index == dev->info->num_statics)
762 		goto exit;
763 
764 	/* clear port */
765 	static_table[1] &= ~BIT(port);
766 
767 	if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
768 		/* delete entry */
769 		static_table[0] = 0;
770 		static_table[1] = 0;
771 		static_table[2] = 0;
772 		static_table[3] = 0;
773 	}
774 
775 	ksz9477_write_table(dev, static_table);
776 
777 	data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
778 	ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
779 
780 	/* wait to be finished */
781 	ret = ksz9477_wait_alu_sta_ready(dev);
782 	if (ret)
783 		dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
784 
785 exit:
786 	mutex_unlock(&dev->alu_mutex);
787 
788 	return ret;
789 }
790 
791 int ksz9477_port_mirror_add(struct ksz_device *dev, int port,
792 			    struct dsa_mall_mirror_tc_entry *mirror,
793 			    bool ingress, struct netlink_ext_ack *extack)
794 {
795 	u8 data;
796 	int p;
797 
798 	/* Limit to one sniffer port
799 	 * Check if any of the port is already set for sniffing
800 	 * If yes, instruct the user to remove the previous entry & exit
801 	 */
802 	for (p = 0; p < dev->info->port_cnt; p++) {
803 		/* Skip the current sniffing port */
804 		if (p == mirror->to_local_port)
805 			continue;
806 
807 		ksz_pread8(dev, p, P_MIRROR_CTRL, &data);
808 
809 		if (data & PORT_MIRROR_SNIFFER) {
810 			NL_SET_ERR_MSG_MOD(extack,
811 					   "Sniffer port is already configured, delete existing rules & retry");
812 			return -EBUSY;
813 		}
814 	}
815 
816 	if (ingress)
817 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
818 	else
819 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
820 
821 	/* configure mirror port */
822 	ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
823 		     PORT_MIRROR_SNIFFER, true);
824 
825 	ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
826 
827 	return 0;
828 }
829 
830 void ksz9477_port_mirror_del(struct ksz_device *dev, int port,
831 			     struct dsa_mall_mirror_tc_entry *mirror)
832 {
833 	bool in_use = false;
834 	u8 data;
835 	int p;
836 
837 	if (mirror->ingress)
838 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
839 	else
840 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
841 
842 
843 	/* Check if any of the port is still referring to sniffer port */
844 	for (p = 0; p < dev->info->port_cnt; p++) {
845 		ksz_pread8(dev, p, P_MIRROR_CTRL, &data);
846 
847 		if ((data & (PORT_MIRROR_RX | PORT_MIRROR_TX))) {
848 			in_use = true;
849 			break;
850 		}
851 	}
852 
853 	/* delete sniffing if there are no other mirroring rules */
854 	if (!in_use)
855 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
856 			     PORT_MIRROR_SNIFFER, false);
857 }
858 
859 static bool ksz9477_get_gbit(struct ksz_device *dev, u8 data)
860 {
861 	bool gbit;
862 
863 	if (dev->features & NEW_XMII)
864 		gbit = !(data & PORT_MII_NOT_1GBIT);
865 	else
866 		gbit = !!(data & PORT_MII_1000MBIT_S1);
867 	return gbit;
868 }
869 
870 static void ksz9477_set_gbit(struct ksz_device *dev, bool gbit, u8 *data)
871 {
872 	if (dev->features & NEW_XMII) {
873 		if (gbit)
874 			*data &= ~PORT_MII_NOT_1GBIT;
875 		else
876 			*data |= PORT_MII_NOT_1GBIT;
877 	} else {
878 		if (gbit)
879 			*data |= PORT_MII_1000MBIT_S1;
880 		else
881 			*data &= ~PORT_MII_1000MBIT_S1;
882 	}
883 }
884 
885 static int ksz9477_get_xmii(struct ksz_device *dev, u8 data)
886 {
887 	int mode;
888 
889 	if (dev->features & NEW_XMII) {
890 		switch (data & PORT_MII_SEL_M) {
891 		case PORT_MII_SEL:
892 			mode = 0;
893 			break;
894 		case PORT_RMII_SEL:
895 			mode = 1;
896 			break;
897 		case PORT_GMII_SEL:
898 			mode = 2;
899 			break;
900 		default:
901 			mode = 3;
902 		}
903 	} else {
904 		switch (data & PORT_MII_SEL_M) {
905 		case PORT_MII_SEL_S1:
906 			mode = 0;
907 			break;
908 		case PORT_RMII_SEL_S1:
909 			mode = 1;
910 			break;
911 		case PORT_GMII_SEL_S1:
912 			mode = 2;
913 			break;
914 		default:
915 			mode = 3;
916 		}
917 	}
918 	return mode;
919 }
920 
921 static void ksz9477_set_xmii(struct ksz_device *dev, int mode, u8 *data)
922 {
923 	u8 xmii;
924 
925 	if (dev->features & NEW_XMII) {
926 		switch (mode) {
927 		case 0:
928 			xmii = PORT_MII_SEL;
929 			break;
930 		case 1:
931 			xmii = PORT_RMII_SEL;
932 			break;
933 		case 2:
934 			xmii = PORT_GMII_SEL;
935 			break;
936 		default:
937 			xmii = PORT_RGMII_SEL;
938 			break;
939 		}
940 	} else {
941 		switch (mode) {
942 		case 0:
943 			xmii = PORT_MII_SEL_S1;
944 			break;
945 		case 1:
946 			xmii = PORT_RMII_SEL_S1;
947 			break;
948 		case 2:
949 			xmii = PORT_GMII_SEL_S1;
950 			break;
951 		default:
952 			xmii = PORT_RGMII_SEL_S1;
953 			break;
954 		}
955 	}
956 	*data &= ~PORT_MII_SEL_M;
957 	*data |= xmii;
958 }
959 
960 static phy_interface_t ksz9477_get_interface(struct ksz_device *dev, int port)
961 {
962 	phy_interface_t interface;
963 	bool gbit;
964 	int mode;
965 	u8 data8;
966 
967 	if (port < dev->phy_port_cnt)
968 		return PHY_INTERFACE_MODE_NA;
969 	ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
970 	gbit = ksz9477_get_gbit(dev, data8);
971 	mode = ksz9477_get_xmii(dev, data8);
972 	switch (mode) {
973 	case 2:
974 		interface = PHY_INTERFACE_MODE_GMII;
975 		if (gbit)
976 			break;
977 		fallthrough;
978 	case 0:
979 		interface = PHY_INTERFACE_MODE_MII;
980 		break;
981 	case 1:
982 		interface = PHY_INTERFACE_MODE_RMII;
983 		break;
984 	default:
985 		interface = PHY_INTERFACE_MODE_RGMII;
986 		if (data8 & PORT_RGMII_ID_EG_ENABLE)
987 			interface = PHY_INTERFACE_MODE_RGMII_TXID;
988 		if (data8 & PORT_RGMII_ID_IG_ENABLE) {
989 			interface = PHY_INTERFACE_MODE_RGMII_RXID;
990 			if (data8 & PORT_RGMII_ID_EG_ENABLE)
991 				interface = PHY_INTERFACE_MODE_RGMII_ID;
992 		}
993 		break;
994 	}
995 	return interface;
996 }
997 
998 static void ksz9477_port_mmd_write(struct ksz_device *dev, int port,
999 				   u8 dev_addr, u16 reg_addr, u16 val)
1000 {
1001 	ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1002 		     MMD_SETUP(PORT_MMD_OP_INDEX, dev_addr));
1003 	ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, reg_addr);
1004 	ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1005 		     MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, dev_addr));
1006 	ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
1007 }
1008 
1009 static void ksz9477_phy_errata_setup(struct ksz_device *dev, int port)
1010 {
1011 	/* Apply PHY settings to address errata listed in
1012 	 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1013 	 * Silicon Errata and Data Sheet Clarification documents:
1014 	 *
1015 	 * Register settings are needed to improve PHY receive performance
1016 	 */
1017 	ksz9477_port_mmd_write(dev, port, 0x01, 0x6f, 0xdd0b);
1018 	ksz9477_port_mmd_write(dev, port, 0x01, 0x8f, 0x6032);
1019 	ksz9477_port_mmd_write(dev, port, 0x01, 0x9d, 0x248c);
1020 	ksz9477_port_mmd_write(dev, port, 0x01, 0x75, 0x0060);
1021 	ksz9477_port_mmd_write(dev, port, 0x01, 0xd3, 0x7777);
1022 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x06, 0x3008);
1023 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x08, 0x2001);
1024 
1025 	/* Transmit waveform amplitude can be improved
1026 	 * (1000BASE-T, 100BASE-TX, 10BASE-Te)
1027 	 */
1028 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x04, 0x00d0);
1029 
1030 	/* Energy Efficient Ethernet (EEE) feature select must
1031 	 * be manually disabled (except on KSZ8565 which is 100Mbit)
1032 	 */
1033 	if (dev->features & GBIT_SUPPORT)
1034 		ksz9477_port_mmd_write(dev, port, 0x07, 0x3c, 0x0000);
1035 
1036 	/* Register settings are required to meet data sheet
1037 	 * supply current specifications
1038 	 */
1039 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x13, 0x6eff);
1040 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x14, 0xe6ff);
1041 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x15, 0x6eff);
1042 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x16, 0xe6ff);
1043 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x17, 0x00ff);
1044 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x18, 0x43ff);
1045 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x19, 0xc3ff);
1046 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x1a, 0x6fff);
1047 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x1b, 0x07ff);
1048 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x1c, 0x0fff);
1049 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x1d, 0xe7ff);
1050 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x1e, 0xefff);
1051 	ksz9477_port_mmd_write(dev, port, 0x1c, 0x20, 0xeeee);
1052 }
1053 
1054 void ksz9477_get_caps(struct ksz_device *dev, int port,
1055 		      struct phylink_config *config)
1056 {
1057 	config->mac_capabilities = MAC_10 | MAC_100 | MAC_ASYM_PAUSE |
1058 				   MAC_SYM_PAUSE;
1059 
1060 	if (dev->features & GBIT_SUPPORT)
1061 		config->mac_capabilities |= MAC_1000FD;
1062 }
1063 
1064 void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1065 {
1066 	struct ksz_port *p = &dev->ports[port];
1067 	struct dsa_switch *ds = dev->ds;
1068 	u8 data8, member;
1069 	u16 data16;
1070 
1071 	/* enable tag tail for host port */
1072 	if (cpu_port)
1073 		ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE,
1074 			     true);
1075 
1076 	ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false);
1077 
1078 	/* set back pressure */
1079 	ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true);
1080 
1081 	/* enable broadcast storm limit */
1082 	ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1083 
1084 	/* disable DiffServ priority */
1085 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false);
1086 
1087 	/* replace priority */
1088 	ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING,
1089 		     false);
1090 	ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4,
1091 			   MTI_PVID_REPLACE, false);
1092 
1093 	/* enable 802.1p priority */
1094 	ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
1095 
1096 	if (port < dev->phy_port_cnt) {
1097 		/* do not force flow control */
1098 		ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1099 			     PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1100 			     false);
1101 
1102 		if (dev->info->phy_errata_9477)
1103 			ksz9477_phy_errata_setup(dev, port);
1104 	} else {
1105 		/* force flow control */
1106 		ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1107 			     PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1108 			     true);
1109 
1110 		/* configure MAC to 1G & RGMII mode */
1111 		ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1112 		switch (p->interface) {
1113 		case PHY_INTERFACE_MODE_MII:
1114 			ksz9477_set_xmii(dev, 0, &data8);
1115 			ksz9477_set_gbit(dev, false, &data8);
1116 			p->phydev.speed = SPEED_100;
1117 			break;
1118 		case PHY_INTERFACE_MODE_RMII:
1119 			ksz9477_set_xmii(dev, 1, &data8);
1120 			ksz9477_set_gbit(dev, false, &data8);
1121 			p->phydev.speed = SPEED_100;
1122 			break;
1123 		case PHY_INTERFACE_MODE_GMII:
1124 			ksz9477_set_xmii(dev, 2, &data8);
1125 			ksz9477_set_gbit(dev, true, &data8);
1126 			p->phydev.speed = SPEED_1000;
1127 			break;
1128 		default:
1129 			ksz9477_set_xmii(dev, 3, &data8);
1130 			ksz9477_set_gbit(dev, true, &data8);
1131 			data8 &= ~PORT_RGMII_ID_IG_ENABLE;
1132 			data8 &= ~PORT_RGMII_ID_EG_ENABLE;
1133 			if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1134 			    p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1135 				data8 |= PORT_RGMII_ID_IG_ENABLE;
1136 			if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1137 			    p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1138 				data8 |= PORT_RGMII_ID_EG_ENABLE;
1139 			/* On KSZ9893, disable RGMII in-band status support */
1140 			if (dev->features & IS_9893)
1141 				data8 &= ~PORT_MII_MAC_MODE;
1142 			p->phydev.speed = SPEED_1000;
1143 			break;
1144 		}
1145 		ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
1146 		p->phydev.duplex = 1;
1147 	}
1148 
1149 	if (cpu_port)
1150 		member = dsa_user_ports(ds);
1151 	else
1152 		member = BIT(dsa_upstream_port(ds, port));
1153 
1154 	ksz9477_cfg_port_member(dev, port, member);
1155 
1156 	/* clear pending interrupts */
1157 	if (port < dev->phy_port_cnt)
1158 		ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16);
1159 }
1160 
1161 void ksz9477_config_cpu_port(struct dsa_switch *ds)
1162 {
1163 	struct ksz_device *dev = ds->priv;
1164 	struct ksz_port *p;
1165 	int i;
1166 
1167 	for (i = 0; i < dev->info->port_cnt; i++) {
1168 		if (dsa_is_cpu_port(ds, i) &&
1169 		    (dev->info->cpu_ports & (1 << i))) {
1170 			phy_interface_t interface;
1171 			const char *prev_msg;
1172 			const char *prev_mode;
1173 
1174 			dev->cpu_port = i;
1175 			p = &dev->ports[i];
1176 
1177 			/* Read from XMII register to determine host port
1178 			 * interface.  If set specifically in device tree
1179 			 * note the difference to help debugging.
1180 			 */
1181 			interface = ksz9477_get_interface(dev, i);
1182 			if (!p->interface) {
1183 				if (dev->compat_interface) {
1184 					dev_warn(dev->dev,
1185 						 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1186 						 "Please update your device tree.\n",
1187 						 i);
1188 					p->interface = dev->compat_interface;
1189 				} else {
1190 					p->interface = interface;
1191 				}
1192 			}
1193 			if (interface && interface != p->interface) {
1194 				prev_msg = " instead of ";
1195 				prev_mode = phy_modes(interface);
1196 			} else {
1197 				prev_msg = "";
1198 				prev_mode = "";
1199 			}
1200 			dev_info(dev->dev,
1201 				 "Port%d: using phy mode %s%s%s\n",
1202 				 i,
1203 				 phy_modes(p->interface),
1204 				 prev_msg,
1205 				 prev_mode);
1206 
1207 			/* enable cpu port */
1208 			ksz9477_port_setup(dev, i, true);
1209 			p->on = 1;
1210 		}
1211 	}
1212 
1213 	for (i = 0; i < dev->info->port_cnt; i++) {
1214 		if (i == dev->cpu_port)
1215 			continue;
1216 		p = &dev->ports[i];
1217 
1218 		ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1219 		p->on = 1;
1220 		if (i < dev->phy_port_cnt)
1221 			p->phy = 1;
1222 		if (dev->chip_id == 0x00947700 && i == 6) {
1223 			p->sgmii = 1;
1224 
1225 			/* SGMII PHY detection code is not implemented yet. */
1226 			p->phy = 0;
1227 		}
1228 	}
1229 }
1230 
1231 int ksz9477_enable_stp_addr(struct ksz_device *dev)
1232 {
1233 	u32 data;
1234 	int ret;
1235 
1236 	/* Enable Reserved multicast table */
1237 	ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_RESV_MCAST_ENABLE, true);
1238 
1239 	/* Set the Override bit for forwarding BPDU packet to CPU */
1240 	ret = ksz_write32(dev, REG_SW_ALU_VAL_B,
1241 			  ALU_V_OVERRIDE | BIT(dev->cpu_port));
1242 	if (ret < 0)
1243 		return ret;
1244 
1245 	data = ALU_STAT_START | ALU_RESV_MCAST_ADDR;
1246 
1247 	ret = ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
1248 	if (ret < 0)
1249 		return ret;
1250 
1251 	/* wait to be finished */
1252 	ret = ksz9477_wait_alu_sta_ready(dev);
1253 	if (ret < 0) {
1254 		dev_err(dev->dev, "Failed to update Reserved Multicast table\n");
1255 		return ret;
1256 	}
1257 
1258 	return 0;
1259 }
1260 
1261 int ksz9477_setup(struct dsa_switch *ds)
1262 {
1263 	struct ksz_device *dev = ds->priv;
1264 	int ret = 0;
1265 
1266 	/* Required for port partitioning. */
1267 	ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY,
1268 		      true);
1269 
1270 	/* Do not work correctly with tail tagging. */
1271 	ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);
1272 
1273 	/* Enable REG_SW_MTU__2 reg by setting SW_JUMBO_PACKET */
1274 	ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_JUMBO_PACKET, true);
1275 
1276 	/* Now we can configure default MTU value */
1277 	ret = regmap_update_bits(dev->regmap[1], REG_SW_MTU__2, REG_SW_MTU_MASK,
1278 				 VLAN_ETH_FRAME_LEN + ETH_FCS_LEN);
1279 	if (ret)
1280 		return ret;
1281 
1282 	/* queue based egress rate limit */
1283 	ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true);
1284 
1285 	/* enable global MIB counter freeze function */
1286 	ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
1287 
1288 	return 0;
1289 }
1290 
1291 u32 ksz9477_get_port_addr(int port, int offset)
1292 {
1293 	return PORT_CTRL_ADDR(port, offset);
1294 }
1295 
1296 int ksz9477_switch_init(struct ksz_device *dev)
1297 {
1298 	u8 data8;
1299 	int ret;
1300 
1301 	dev->port_mask = (1 << dev->info->port_cnt) - 1;
1302 
1303 	/* turn off SPI DO Edge select */
1304 	ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
1305 	if (ret)
1306 		return ret;
1307 
1308 	data8 &= ~SPI_AUTO_EDGE_DETECTION;
1309 	ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
1310 	if (ret)
1311 		return ret;
1312 
1313 	ret = ksz_read8(dev, REG_GLOBAL_OPTIONS, &data8);
1314 	if (ret)
1315 		return ret;
1316 
1317 	/* Number of ports can be reduced depending on chip. */
1318 	dev->phy_port_cnt = 5;
1319 
1320 	/* Default capability is gigabit capable. */
1321 	dev->features = GBIT_SUPPORT;
1322 
1323 	if (dev->chip_id == KSZ9893_CHIP_ID) {
1324 		dev->features |= IS_9893;
1325 
1326 		/* Chip does not support gigabit. */
1327 		if (data8 & SW_QW_ABLE)
1328 			dev->features &= ~GBIT_SUPPORT;
1329 		dev->phy_port_cnt = 2;
1330 	} else {
1331 		/* Chip uses new XMII register definitions. */
1332 		dev->features |= NEW_XMII;
1333 
1334 		/* Chip does not support gigabit. */
1335 		if (!(data8 & SW_GIGABIT_ABLE))
1336 			dev->features &= ~GBIT_SUPPORT;
1337 	}
1338 
1339 	return 0;
1340 }
1341 
1342 void ksz9477_switch_exit(struct ksz_device *dev)
1343 {
1344 	ksz9477_reset_switch(dev);
1345 }
1346 
1347 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1348 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
1349 MODULE_LICENSE("GPL");
1350