• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Microchip KSZ8XXX series switch driver
4  *
5  * It supports the following switches:
6  * - KSZ8863, KSZ8873 aka KSZ88X3
7  * - KSZ8895, KSZ8864 aka KSZ8895 family
8  * - KSZ8794, KSZ8795, KSZ8765 aka KSZ87XX
9  * Note that it does NOT support:
10  * - KSZ8563, KSZ8567 - see KSZ9477 driver
11  *
12  * Copyright (C) 2017 Microchip Technology Inc.
13  *	Tristram Ha <Tristram.Ha@microchip.com>
14  */
15 
16 #include <linux/bitfield.h>
17 #include <linux/delay.h>
18 #include <linux/export.h>
19 #include <linux/gpio.h>
20 #include <linux/if_vlan.h>
21 #include <linux/kernel.h>
22 #include <linux/module.h>
23 #include <linux/platform_data/microchip-ksz.h>
24 #include <linux/phy.h>
25 #include <linux/etherdevice.h>
26 #include <linux/if_bridge.h>
27 #include <linux/micrel_phy.h>
28 #include <net/dsa.h>
29 #include <net/switchdev.h>
30 #include <linux/phylink.h>
31 
32 #include "ksz_common.h"
33 #include "ksz8_reg.h"
34 #include "ksz8.h"
35 
ksz_cfg(struct ksz_device * dev,u32 addr,u8 bits,bool set)36 static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
37 {
38 	regmap_update_bits(ksz_regmap_8(dev), addr, bits, set ? bits : 0);
39 }
40 
ksz_port_cfg(struct ksz_device * dev,int port,int offset,u8 bits,bool set)41 static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
42 			 bool set)
43 {
44 	regmap_update_bits(ksz_regmap_8(dev), PORT_CTRL_ADDR(port, offset),
45 			   bits, set ? bits : 0);
46 }
47 
48 /**
49  * ksz8_ind_write8 - EEE/ACL/PME indirect register write
50  * @dev: The device structure.
51  * @table: Function & table select, register 110.
52  * @addr: Indirect access control, register 111.
53  * @data: The data to be written.
54  *
55  * This function performs an indirect register write for EEE, ACL or
56  * PME switch functionalities. Both 8-bit registers 110 and 111 are
57  * written at once with ksz_write16, using the serial multiple write
58  * functionality.
59  *
60  * Return: 0 on success, or an error code on failure.
61  */
ksz8_ind_write8(struct ksz_device * dev,u8 table,u16 addr,u8 data)62 static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data)
63 {
64 	const u16 *regs;
65 	u16 ctrl_addr;
66 	int ret = 0;
67 
68 	regs = dev->info->regs;
69 
70 	mutex_lock(&dev->alu_mutex);
71 
72 	ctrl_addr = IND_ACC_TABLE(table) | addr;
73 	ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
74 	if (!ret)
75 		ret = ksz_write8(dev, regs[REG_IND_BYTE], data);
76 
77 	mutex_unlock(&dev->alu_mutex);
78 
79 	return ret;
80 }
81 
82 /**
83  * ksz8_ind_read8 - EEE/ACL/PME indirect register read
84  * @dev: The device structure.
85  * @table: Function & table select, register 110.
86  * @addr: Indirect access control, register 111.
87  * @val: The value read.
88  *
89  * This function performs an indirect register read for EEE, ACL or
90  * PME switch functionalities. Both 8-bit registers 110 and 111 are
91  * written at once with ksz_write16, using the serial multiple write
92  * functionality.
93  *
94  * Return: 0 on success, or an error code on failure.
95  */
ksz8_ind_read8(struct ksz_device * dev,u8 table,u16 addr,u8 * val)96 static int ksz8_ind_read8(struct ksz_device *dev, u8 table, u16 addr, u8 *val)
97 {
98 	const u16 *regs;
99 	u16 ctrl_addr;
100 	int ret = 0;
101 
102 	regs = dev->info->regs;
103 
104 	mutex_lock(&dev->alu_mutex);
105 
106 	ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
107 	ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
108 	if (!ret)
109 		ret = ksz_read8(dev, regs[REG_IND_BYTE], val);
110 
111 	mutex_unlock(&dev->alu_mutex);
112 
113 	return ret;
114 }
115 
ksz8_pme_write8(struct ksz_device * dev,u32 reg,u8 value)116 int ksz8_pme_write8(struct ksz_device *dev, u32 reg, u8 value)
117 {
118 	return ksz8_ind_write8(dev, (u8)(reg >> 8), (u8)(reg), value);
119 }
120 
ksz8_pme_pread8(struct ksz_device * dev,int port,int offset,u8 * data)121 int ksz8_pme_pread8(struct ksz_device *dev, int port, int offset, u8 *data)
122 {
123 	u8 table = (u8)(offset >> 8 | (port + 1));
124 
125 	return ksz8_ind_read8(dev, table, (u8)(offset), data);
126 }
127 
ksz8_pme_pwrite8(struct ksz_device * dev,int port,int offset,u8 data)128 int ksz8_pme_pwrite8(struct ksz_device *dev, int port, int offset, u8 data)
129 {
130 	u8 table = (u8)(offset >> 8 | (port + 1));
131 
132 	return ksz8_ind_write8(dev, table, (u8)(offset), data);
133 }
134 
ksz8_reset_switch(struct ksz_device * dev)135 int ksz8_reset_switch(struct ksz_device *dev)
136 {
137 	if (ksz_is_ksz88x3(dev)) {
138 		/* reset switch */
139 		ksz_cfg(dev, KSZ8863_REG_SW_RESET,
140 			KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true);
141 		ksz_cfg(dev, KSZ8863_REG_SW_RESET,
142 			KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false);
143 	} else {
144 		/* reset switch */
145 		ksz_write8(dev, REG_POWER_MANAGEMENT_1,
146 			   SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
147 		ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
148 	}
149 
150 	return 0;
151 }
152 
ksz8863_change_mtu(struct ksz_device * dev,int frame_size)153 static int ksz8863_change_mtu(struct ksz_device *dev, int frame_size)
154 {
155 	u8 ctrl2 = 0;
156 
157 	if (frame_size <= KSZ8_LEGAL_PACKET_SIZE)
158 		ctrl2 |= KSZ8863_LEGAL_PACKET_ENABLE;
159 	else if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
160 		ctrl2 |= KSZ8863_HUGE_PACKET_ENABLE;
161 
162 	return ksz_rmw8(dev, REG_SW_CTRL_2, KSZ8863_LEGAL_PACKET_ENABLE |
163 			KSZ8863_HUGE_PACKET_ENABLE, ctrl2);
164 }
165 
ksz8795_change_mtu(struct ksz_device * dev,int frame_size)166 static int ksz8795_change_mtu(struct ksz_device *dev, int frame_size)
167 {
168 	u8 ctrl1 = 0, ctrl2 = 0;
169 	int ret;
170 
171 	if (frame_size > KSZ8_LEGAL_PACKET_SIZE)
172 		ctrl2 |= SW_LEGAL_PACKET_DISABLE;
173 	if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
174 		ctrl1 |= SW_HUGE_PACKET;
175 
176 	ret = ksz_rmw8(dev, REG_SW_CTRL_1, SW_HUGE_PACKET, ctrl1);
177 	if (ret)
178 		return ret;
179 
180 	return ksz_rmw8(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, ctrl2);
181 }
182 
ksz8_change_mtu(struct ksz_device * dev,int port,int mtu)183 int ksz8_change_mtu(struct ksz_device *dev, int port, int mtu)
184 {
185 	u16 frame_size;
186 
187 	if (!dsa_is_cpu_port(dev->ds, port))
188 		return 0;
189 
190 	frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
191 
192 	switch (dev->chip_id) {
193 	case KSZ8795_CHIP_ID:
194 	case KSZ8794_CHIP_ID:
195 	case KSZ8765_CHIP_ID:
196 		return ksz8795_change_mtu(dev, frame_size);
197 	case KSZ88X3_CHIP_ID:
198 	case KSZ8864_CHIP_ID:
199 	case KSZ8895_CHIP_ID:
200 		return ksz8863_change_mtu(dev, frame_size);
201 	}
202 
203 	return -EOPNOTSUPP;
204 }
205 
ksz8_port_queue_split(struct ksz_device * dev,int port,int queues)206 static int ksz8_port_queue_split(struct ksz_device *dev, int port, int queues)
207 {
208 	u8 mask_4q, mask_2q;
209 	u8 reg_4q, reg_2q;
210 	u8 data_4q = 0;
211 	u8 data_2q = 0;
212 	int ret;
213 
214 	if (ksz_is_ksz88x3(dev)) {
215 		mask_4q = KSZ8873_PORT_4QUEUE_SPLIT_EN;
216 		mask_2q = KSZ8873_PORT_2QUEUE_SPLIT_EN;
217 		reg_4q = REG_PORT_CTRL_0;
218 		reg_2q = REG_PORT_CTRL_2;
219 
220 		/* KSZ8795 family switches have Weighted Fair Queueing (WFQ)
221 		 * enabled by default. Enable it for KSZ8873 family switches
222 		 * too. Default value for KSZ8873 family is strict priority,
223 		 * which should be enabled by using TC_SETUP_QDISC_ETS, not
224 		 * by default.
225 		 */
226 		ret = ksz_rmw8(dev, REG_SW_CTRL_3, WEIGHTED_FAIR_QUEUE_ENABLE,
227 			       WEIGHTED_FAIR_QUEUE_ENABLE);
228 		if (ret)
229 			return ret;
230 	} else {
231 		mask_4q = KSZ8795_PORT_4QUEUE_SPLIT_EN;
232 		mask_2q = KSZ8795_PORT_2QUEUE_SPLIT_EN;
233 		reg_4q = REG_PORT_CTRL_13;
234 		reg_2q = REG_PORT_CTRL_0;
235 
236 		/* TODO: this is legacy from initial KSZ8795 driver, should be
237 		 * moved to appropriate place in the future.
238 		 */
239 		ret = ksz_rmw8(dev, REG_SW_CTRL_19,
240 			       SW_OUT_RATE_LIMIT_QUEUE_BASED,
241 			       SW_OUT_RATE_LIMIT_QUEUE_BASED);
242 		if (ret)
243 			return ret;
244 	}
245 
246 	if (queues == 4)
247 		data_4q = mask_4q;
248 	else if (queues == 2)
249 		data_2q = mask_2q;
250 
251 	ret = ksz_prmw8(dev, port, reg_4q, mask_4q, data_4q);
252 	if (ret)
253 		return ret;
254 
255 	return ksz_prmw8(dev, port, reg_2q, mask_2q, data_2q);
256 }
257 
ksz8_all_queues_split(struct ksz_device * dev,int queues)258 int ksz8_all_queues_split(struct ksz_device *dev, int queues)
259 {
260 	struct dsa_switch *ds = dev->ds;
261 	const struct dsa_port *dp;
262 
263 	dsa_switch_for_each_port(dp, ds) {
264 		int ret = ksz8_port_queue_split(dev, dp->index, queues);
265 
266 		if (ret)
267 			return ret;
268 	}
269 
270 	return 0;
271 }
272 
ksz8_r_mib_cnt(struct ksz_device * dev,int port,u16 addr,u64 * cnt)273 void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
274 {
275 	const u32 *masks;
276 	const u16 *regs;
277 	u16 ctrl_addr;
278 	u32 data;
279 	u8 check;
280 	int loop;
281 
282 	masks = dev->info->masks;
283 	regs = dev->info->regs;
284 
285 	ctrl_addr = addr + dev->info->reg_mib_cnt * port;
286 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
287 
288 	mutex_lock(&dev->alu_mutex);
289 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
290 
291 	/* It is almost guaranteed to always read the valid bit because of
292 	 * slow SPI speed.
293 	 */
294 	for (loop = 2; loop > 0; loop--) {
295 		ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
296 
297 		if (check & masks[MIB_COUNTER_VALID]) {
298 			ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
299 			if (check & masks[MIB_COUNTER_OVERFLOW])
300 				*cnt += MIB_COUNTER_VALUE + 1;
301 			*cnt += data & MIB_COUNTER_VALUE;
302 			break;
303 		}
304 	}
305 	mutex_unlock(&dev->alu_mutex);
306 }
307 
ksz8795_r_mib_pkt(struct ksz_device * dev,int port,u16 addr,u64 * dropped,u64 * cnt)308 static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
309 			      u64 *dropped, u64 *cnt)
310 {
311 	const u32 *masks;
312 	const u16 *regs;
313 	u16 ctrl_addr;
314 	u32 data;
315 	u8 check;
316 	int loop;
317 
318 	masks = dev->info->masks;
319 	regs = dev->info->regs;
320 
321 	addr -= dev->info->reg_mib_cnt;
322 	ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
323 	ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
324 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
325 
326 	mutex_lock(&dev->alu_mutex);
327 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
328 
329 	/* It is almost guaranteed to always read the valid bit because of
330 	 * slow SPI speed.
331 	 */
332 	for (loop = 2; loop > 0; loop--) {
333 		ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
334 
335 		if (check & masks[MIB_COUNTER_VALID]) {
336 			ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
337 			if (addr < 2) {
338 				u64 total;
339 
340 				total = check & MIB_TOTAL_BYTES_H;
341 				total <<= 32;
342 				*cnt += total;
343 				*cnt += data;
344 				if (check & masks[MIB_COUNTER_OVERFLOW]) {
345 					total = MIB_TOTAL_BYTES_H + 1;
346 					total <<= 32;
347 					*cnt += total;
348 				}
349 			} else {
350 				if (check & masks[MIB_COUNTER_OVERFLOW])
351 					*cnt += MIB_PACKET_DROPPED + 1;
352 				*cnt += data & MIB_PACKET_DROPPED;
353 			}
354 			break;
355 		}
356 	}
357 	mutex_unlock(&dev->alu_mutex);
358 }
359 
ksz8863_r_mib_pkt(struct ksz_device * dev,int port,u16 addr,u64 * dropped,u64 * cnt)360 static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
361 			      u64 *dropped, u64 *cnt)
362 {
363 	u32 *last = (u32 *)dropped;
364 	const u16 *regs;
365 	u16 ctrl_addr;
366 	u32 data;
367 	u32 cur;
368 
369 	regs = dev->info->regs;
370 
371 	addr -= dev->info->reg_mib_cnt;
372 	ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
373 			   KSZ8863_MIB_PACKET_DROPPED_RX_0;
374 	if (ksz_is_8895_family(dev) &&
375 	    ctrl_addr == KSZ8863_MIB_PACKET_DROPPED_RX_0)
376 		ctrl_addr = KSZ8895_MIB_PACKET_DROPPED_RX_0;
377 	ctrl_addr += port;
378 	ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
379 
380 	mutex_lock(&dev->alu_mutex);
381 	ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
382 	ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
383 	mutex_unlock(&dev->alu_mutex);
384 
385 	data &= MIB_PACKET_DROPPED;
386 	cur = last[addr];
387 	if (data != cur) {
388 		last[addr] = data;
389 		if (data < cur)
390 			data += MIB_PACKET_DROPPED + 1;
391 		data -= cur;
392 		*cnt += data;
393 	}
394 }
395 
ksz8_r_mib_pkt(struct ksz_device * dev,int port,u16 addr,u64 * dropped,u64 * cnt)396 void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
397 		    u64 *dropped, u64 *cnt)
398 {
399 	if (is_ksz88xx(dev))
400 		ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
401 	else
402 		ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
403 }
404 
ksz8_freeze_mib(struct ksz_device * dev,int port,bool freeze)405 void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
406 {
407 	if (is_ksz88xx(dev))
408 		return;
409 
410 	/* enable the port for flush/freeze function */
411 	if (freeze)
412 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
413 	ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
414 
415 	/* disable the port after freeze is done */
416 	if (!freeze)
417 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
418 }
419 
ksz8_port_init_cnt(struct ksz_device * dev,int port)420 void ksz8_port_init_cnt(struct ksz_device *dev, int port)
421 {
422 	struct ksz_port_mib *mib = &dev->ports[port].mib;
423 	u64 *dropped;
424 
425 	/* For KSZ8795 family. */
426 	if (ksz_is_ksz87xx(dev)) {
427 		/* flush all enabled port MIB counters */
428 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
429 		ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
430 		ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
431 	}
432 
433 	mib->cnt_ptr = 0;
434 
435 	/* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
436 	while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
437 		dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
438 					&mib->counters[mib->cnt_ptr]);
439 		++mib->cnt_ptr;
440 	}
441 
442 	/* last one in storage */
443 	dropped = &mib->counters[dev->info->mib_cnt];
444 
445 	/* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
446 	while (mib->cnt_ptr < dev->info->mib_cnt) {
447 		dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
448 					dropped, &mib->counters[mib->cnt_ptr]);
449 		++mib->cnt_ptr;
450 	}
451 }
452 
ksz8_r_table(struct ksz_device * dev,int table,u16 addr,u64 * data)453 static int ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
454 {
455 	const u16 *regs;
456 	u16 ctrl_addr;
457 	int ret;
458 
459 	regs = dev->info->regs;
460 
461 	ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
462 
463 	mutex_lock(&dev->alu_mutex);
464 	ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
465 	if (ret)
466 		goto unlock_alu;
467 
468 	ret = ksz_read64(dev, regs[REG_IND_DATA_HI], data);
469 unlock_alu:
470 	mutex_unlock(&dev->alu_mutex);
471 
472 	return ret;
473 }
474 
ksz8_w_table(struct ksz_device * dev,int table,u16 addr,u64 data)475 static int ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
476 {
477 	const u16 *regs;
478 	u16 ctrl_addr;
479 	int ret;
480 
481 	regs = dev->info->regs;
482 
483 	ctrl_addr = IND_ACC_TABLE(table) | addr;
484 
485 	mutex_lock(&dev->alu_mutex);
486 	ret = ksz_write64(dev, regs[REG_IND_DATA_HI], data);
487 	if (ret)
488 		goto unlock_alu;
489 
490 	ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
491 unlock_alu:
492 	mutex_unlock(&dev->alu_mutex);
493 
494 	return ret;
495 }
496 
ksz8_valid_dyn_entry(struct ksz_device * dev,u8 * data)497 static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
498 {
499 	int timeout = 100;
500 	const u32 *masks;
501 	const u16 *regs;
502 	int ret;
503 
504 	masks = dev->info->masks;
505 	regs = dev->info->regs;
506 
507 	do {
508 		ret = ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
509 		if (ret)
510 			return ret;
511 
512 		timeout--;
513 	} while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
514 
515 	/* Entry is not ready for accessing. */
516 	if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY])
517 		return -ETIMEDOUT;
518 
519 	/* Entry is ready for accessing. */
520 	return ksz_read8(dev, regs[REG_IND_DATA_8], data);
521 }
522 
ksz8_r_dyn_mac_table(struct ksz_device * dev,u16 addr,u8 * mac_addr,u8 * fid,u8 * src_port,u16 * entries)523 static int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, u8 *mac_addr,
524 				u8 *fid, u8 *src_port, u16 *entries)
525 {
526 	u32 data_hi, data_lo;
527 	const u8 *shifts;
528 	const u32 *masks;
529 	const u16 *regs;
530 	u16 ctrl_addr;
531 	u64 buf = 0;
532 	u8 data;
533 	int cnt;
534 	int ret;
535 
536 	shifts = dev->info->shifts;
537 	masks = dev->info->masks;
538 	regs = dev->info->regs;
539 
540 	ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
541 
542 	mutex_lock(&dev->alu_mutex);
543 	ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
544 	if (ret)
545 		goto unlock_alu;
546 
547 	ret = ksz8_valid_dyn_entry(dev, &data);
548 	if (ret)
549 		goto unlock_alu;
550 
551 	if (data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY]) {
552 		*entries = 0;
553 		goto unlock_alu;
554 	}
555 
556 	ret = ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
557 	if (ret)
558 		goto unlock_alu;
559 
560 	data_hi = (u32)(buf >> 32);
561 	data_lo = (u32)buf;
562 
563 	/* Check out how many valid entry in the table. */
564 	cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
565 	cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
566 	cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
567 		shifts[DYNAMIC_MAC_ENTRIES];
568 	*entries = cnt + 1;
569 
570 	*fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
571 		shifts[DYNAMIC_MAC_FID];
572 	*src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
573 		shifts[DYNAMIC_MAC_SRC_PORT];
574 
575 	mac_addr[5] = (u8)data_lo;
576 	mac_addr[4] = (u8)(data_lo >> 8);
577 	mac_addr[3] = (u8)(data_lo >> 16);
578 	mac_addr[2] = (u8)(data_lo >> 24);
579 
580 	mac_addr[1] = (u8)data_hi;
581 	mac_addr[0] = (u8)(data_hi >> 8);
582 
583 unlock_alu:
584 	mutex_unlock(&dev->alu_mutex);
585 
586 	return ret;
587 }
588 
ksz8_r_sta_mac_table(struct ksz_device * dev,u16 addr,struct alu_struct * alu,bool * valid)589 static int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
590 				struct alu_struct *alu, bool *valid)
591 {
592 	u32 data_hi, data_lo;
593 	const u8 *shifts;
594 	const u32 *masks;
595 	u64 data;
596 	int ret;
597 
598 	shifts = dev->info->shifts;
599 	masks = dev->info->masks;
600 
601 	ret = ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
602 	if (ret)
603 		return ret;
604 
605 	data_hi = data >> 32;
606 	data_lo = (u32)data;
607 
608 	if (!(data_hi & (masks[STATIC_MAC_TABLE_VALID] |
609 			 masks[STATIC_MAC_TABLE_OVERRIDE]))) {
610 		*valid = false;
611 		return 0;
612 	}
613 
614 	alu->mac[5] = (u8)data_lo;
615 	alu->mac[4] = (u8)(data_lo >> 8);
616 	alu->mac[3] = (u8)(data_lo >> 16);
617 	alu->mac[2] = (u8)(data_lo >> 24);
618 	alu->mac[1] = (u8)data_hi;
619 	alu->mac[0] = (u8)(data_hi >> 8);
620 	alu->port_forward =
621 		(data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
622 			shifts[STATIC_MAC_FWD_PORTS];
623 	alu->is_override = (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
624 
625 	/* KSZ8795/KSZ8895 family switches have STATIC_MAC_TABLE_USE_FID and
626 	 * STATIC_MAC_TABLE_FID definitions off by 1 when doing read on the
627 	 * static MAC table compared to doing write.
628 	 */
629 	if (ksz_is_ksz87xx(dev) || ksz_is_8895_family(dev))
630 		data_hi >>= 1;
631 	alu->is_static = true;
632 	alu->is_use_fid = (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
633 	alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
634 		shifts[STATIC_MAC_FID];
635 
636 	*valid = true;
637 
638 	return 0;
639 }
640 
ksz8_w_sta_mac_table(struct ksz_device * dev,u16 addr,struct alu_struct * alu)641 static int ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
642 				struct alu_struct *alu)
643 {
644 	u32 data_hi, data_lo;
645 	const u8 *shifts;
646 	const u32 *masks;
647 	u64 data;
648 
649 	shifts = dev->info->shifts;
650 	masks = dev->info->masks;
651 
652 	data_lo = ((u32)alu->mac[2] << 24) |
653 		((u32)alu->mac[3] << 16) |
654 		((u32)alu->mac[4] << 8) | alu->mac[5];
655 	data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
656 	data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
657 
658 	if (alu->is_override)
659 		data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
660 	if (alu->is_use_fid) {
661 		data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
662 		data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
663 	}
664 	if (alu->is_static)
665 		data_hi |= masks[STATIC_MAC_TABLE_VALID];
666 	else
667 		data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
668 
669 	data = (u64)data_hi << 32 | data_lo;
670 
671 	return ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
672 }
673 
ksz8_from_vlan(struct ksz_device * dev,u32 vlan,u8 * fid,u8 * member,u8 * valid)674 static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
675 			   u8 *member, u8 *valid)
676 {
677 	const u8 *shifts;
678 	const u32 *masks;
679 
680 	shifts = dev->info->shifts;
681 	masks = dev->info->masks;
682 
683 	*fid = vlan & masks[VLAN_TABLE_FID];
684 	*member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
685 			shifts[VLAN_TABLE_MEMBERSHIP_S];
686 	*valid = !!(vlan & masks[VLAN_TABLE_VALID]);
687 }
688 
ksz8_to_vlan(struct ksz_device * dev,u8 fid,u8 member,u8 valid,u16 * vlan)689 static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
690 			 u16 *vlan)
691 {
692 	const u8 *shifts;
693 	const u32 *masks;
694 
695 	shifts = dev->info->shifts;
696 	masks = dev->info->masks;
697 
698 	*vlan = fid;
699 	*vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
700 	if (valid)
701 		*vlan |= masks[VLAN_TABLE_VALID];
702 }
703 
ksz8_r_vlan_entries(struct ksz_device * dev,u16 addr)704 static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
705 {
706 	const u8 *shifts;
707 	u64 data;
708 	int i;
709 
710 	shifts = dev->info->shifts;
711 
712 	ksz8_r_table(dev, TABLE_VLAN, addr, &data);
713 	addr *= 4;
714 	for (i = 0; i < 4; i++) {
715 		dev->vlan_cache[addr + i].table[0] = (u16)data;
716 		data >>= shifts[VLAN_TABLE];
717 	}
718 }
719 
ksz8_r_vlan_table(struct ksz_device * dev,u16 vid,u16 * vlan)720 static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
721 {
722 	int index;
723 	u16 *data;
724 	u16 addr;
725 	u64 buf;
726 
727 	data = (u16 *)&buf;
728 	addr = vid / 4;
729 	index = vid & 3;
730 	ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
731 	*vlan = data[index];
732 }
733 
ksz8_w_vlan_table(struct ksz_device * dev,u16 vid,u16 vlan)734 static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
735 {
736 	int index;
737 	u16 *data;
738 	u16 addr;
739 	u64 buf;
740 
741 	data = (u16 *)&buf;
742 	addr = vid / 4;
743 	index = vid & 3;
744 	ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
745 	data[index] = vlan;
746 	dev->vlan_cache[vid].table[0] = vlan;
747 	ksz8_w_table(dev, TABLE_VLAN, addr, buf);
748 }
749 
750 /**
751  * ksz879x_get_loopback - KSZ879x specific function to get loopback
752  *                        configuration status for a specific port
753  * @dev: Pointer to the device structure
754  * @port: Port number to query
755  * @val: Pointer to store the result
756  *
757  * This function reads the SMI registers to determine whether loopback mode
758  * is enabled for a specific port.
759  *
760  * Return: 0 on success, error code on failure.
761  */
ksz879x_get_loopback(struct ksz_device * dev,u16 port,u16 * val)762 static int ksz879x_get_loopback(struct ksz_device *dev, u16 port,
763 				u16 *val)
764 {
765 	u8 stat3;
766 	int ret;
767 
768 	ret = ksz_pread8(dev, port, REG_PORT_STATUS_3, &stat3);
769 	if (ret)
770 		return ret;
771 
772 	if (stat3 & PORT_PHY_LOOPBACK)
773 		*val |= BMCR_LOOPBACK;
774 
775 	return 0;
776 }
777 
778 /**
779  * ksz879x_set_loopback - KSZ879x specific function  to set loopback mode for
780  *			  a specific port
781  * @dev: Pointer to the device structure.
782  * @port: Port number to modify.
783  * @val: Value indicating whether to enable or disable loopback mode.
784  *
785  * This function translates loopback bit of the BMCR register into the
786  * corresponding hardware register bit value and writes it to the SMI interface.
787  *
788  * Return: 0 on success, error code on failure.
789  */
ksz879x_set_loopback(struct ksz_device * dev,u16 port,u16 val)790 static int ksz879x_set_loopback(struct ksz_device *dev, u16 port, u16 val)
791 {
792 	u8 stat3 = 0;
793 
794 	if (val & BMCR_LOOPBACK)
795 		stat3 |= PORT_PHY_LOOPBACK;
796 
797 	return ksz_prmw8(dev, port, REG_PORT_STATUS_3, PORT_PHY_LOOPBACK,
798 			 stat3);
799 }
800 
801 /**
802  * ksz8_r_phy_ctrl - Translates and reads from the SMI interface to a MIIM PHY
803  *		     Control register (Reg. 31).
804  * @dev: The KSZ device instance.
805  * @port: The port number to be read.
806  * @val: The value read from the SMI interface.
807  *
808  * This function reads the SMI interface and translates the hardware register
809  * bit values into their corresponding control settings for a MIIM PHY Control
810  * register.
811  *
812  * Return: 0 on success, error code on failure.
813  */
ksz8_r_phy_ctrl(struct ksz_device * dev,int port,u16 * val)814 static int ksz8_r_phy_ctrl(struct ksz_device *dev, int port, u16 *val)
815 {
816 	const u16 *regs = dev->info->regs;
817 	u8 reg_val;
818 	int ret;
819 
820 	*val = 0;
821 
822 	ret = ksz_pread8(dev, port, regs[P_LINK_STATUS], &reg_val);
823 	if (ret < 0)
824 		return ret;
825 
826 	if (reg_val & PORT_MDIX_STATUS)
827 		*val |= KSZ886X_CTRL_MDIX_STAT;
828 
829 	ret = ksz_pread8(dev, port, REG_PORT_LINK_MD_CTRL, &reg_val);
830 	if (ret < 0)
831 		return ret;
832 
833 	if (reg_val & PORT_FORCE_LINK)
834 		*val |= KSZ886X_CTRL_FORCE_LINK;
835 
836 	if (reg_val & PORT_POWER_SAVING)
837 		*val |= KSZ886X_CTRL_PWRSAVE;
838 
839 	if (reg_val & PORT_PHY_REMOTE_LOOPBACK)
840 		*val |= KSZ886X_CTRL_REMOTE_LOOPBACK;
841 
842 	return 0;
843 }
844 
845 /**
846  * ksz8_r_phy_bmcr - Translates and reads from the SMI interface to a MIIM PHY
847  *		     Basic mode control register (Reg. 0).
848  * @dev: The KSZ device instance.
849  * @port: The port number to be read.
850  * @val: The value read from the SMI interface.
851  *
852  * This function reads the SMI interface and translates the hardware register
853  * bit values into their corresponding control settings for a MIIM PHY Basic
854  * mode control register.
855  *
856  * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
857  * -------------------------------------------------------------------
858  * MIIM Bit                    | KSZ8794 Reg/Bit             | KSZ8873 Reg/Bit
859  * ----------------------------+-----------------------------+----------------
860  * Bit 15 - Soft Reset         | 0xF/4                       | Not supported
861  * Bit 14 - Loopback           | 0xD/0 (MAC), 0xF/7 (PHY)    ~ 0xD/0 (PHY)
862  * Bit 13 - Force 100          | 0xC/6                       = 0xC/6
863  * Bit 12 - AN Enable          | 0xC/7 (reverse logic)       ~ 0xC/7
864  * Bit 11 - Power Down         | 0xD/3                       = 0xD/3
865  * Bit 10 - PHY Isolate        | 0xF/5                       | Not supported
866  * Bit 9 - Restart AN          | 0xD/5                       = 0xD/5
867  * Bit 8 - Force Full-Duplex   | 0xC/5                       = 0xC/5
868  * Bit 7 - Collision Test/Res. | Not supported               | Not supported
869  * Bit 6 - Reserved            | Not supported               | Not supported
870  * Bit 5 - Hp_mdix             | 0x9/7                       ~ 0xF/7
871  * Bit 4 - Force MDI           | 0xD/1                       = 0xD/1
872  * Bit 3 - Disable MDIX        | 0xD/2                       = 0xD/2
873  * Bit 2 - Disable Far-End F.  | ????                        | 0xD/4
874  * Bit 1 - Disable Transmit    | 0xD/6                       = 0xD/6
875  * Bit 0 - Disable LED         | 0xD/7                       = 0xD/7
876  * -------------------------------------------------------------------
877  *
878  * Return: 0 on success, error code on failure.
879  */
ksz8_r_phy_bmcr(struct ksz_device * dev,u16 port,u16 * val)880 static int ksz8_r_phy_bmcr(struct ksz_device *dev, u16 port, u16 *val)
881 {
882 	const u16 *regs = dev->info->regs;
883 	u8 restart, speed, ctrl;
884 	int ret;
885 
886 	*val = 0;
887 
888 	ret = ksz_pread8(dev, port, regs[P_NEG_RESTART_CTRL], &restart);
889 	if (ret)
890 		return ret;
891 
892 	ret = ksz_pread8(dev, port, regs[P_SPEED_STATUS], &speed);
893 	if (ret)
894 		return ret;
895 
896 	ret = ksz_pread8(dev, port, regs[P_FORCE_CTRL], &ctrl);
897 	if (ret)
898 		return ret;
899 
900 	if (ctrl & PORT_FORCE_100_MBIT)
901 		*val |= BMCR_SPEED100;
902 
903 	if (ksz_is_ksz88x3(dev)) {
904 		if (restart & KSZ8873_PORT_PHY_LOOPBACK)
905 			*val |= BMCR_LOOPBACK;
906 
907 		if ((ctrl & PORT_AUTO_NEG_ENABLE))
908 			*val |= BMCR_ANENABLE;
909 	} else {
910 		ret = ksz879x_get_loopback(dev, port, val);
911 		if (ret)
912 			return ret;
913 
914 		if (!(ctrl & PORT_AUTO_NEG_DISABLE))
915 			*val |= BMCR_ANENABLE;
916 	}
917 
918 	if (restart & PORT_POWER_DOWN)
919 		*val |= BMCR_PDOWN;
920 
921 	if (restart & PORT_AUTO_NEG_RESTART)
922 		*val |= BMCR_ANRESTART;
923 
924 	if (ctrl & PORT_FORCE_FULL_DUPLEX)
925 		*val |= BMCR_FULLDPLX;
926 
927 	if (speed & PORT_HP_MDIX)
928 		*val |= KSZ886X_BMCR_HP_MDIX;
929 
930 	if (restart & PORT_FORCE_MDIX)
931 		*val |= KSZ886X_BMCR_FORCE_MDI;
932 
933 	if (restart & PORT_AUTO_MDIX_DISABLE)
934 		*val |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
935 
936 	if (restart & PORT_TX_DISABLE)
937 		*val |= KSZ886X_BMCR_DISABLE_TRANSMIT;
938 
939 	if (restart & PORT_LED_OFF)
940 		*val |= KSZ886X_BMCR_DISABLE_LED;
941 
942 	return 0;
943 }
944 
ksz8_r_phy(struct ksz_device * dev,u16 phy,u16 reg,u16 * val)945 int ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
946 {
947 	u8 ctrl, link, val1, val2;
948 	int processed = true;
949 	const u16 *regs;
950 	u16 data = 0;
951 	u16 p = phy;
952 	int ret;
953 
954 	regs = dev->info->regs;
955 
956 	switch (reg) {
957 	case MII_BMCR:
958 		ret = ksz8_r_phy_bmcr(dev, p, &data);
959 		if (ret)
960 			return ret;
961 		break;
962 	case MII_BMSR:
963 		ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
964 		if (ret)
965 			return ret;
966 
967 		data = BMSR_100FULL |
968 		       BMSR_100HALF |
969 		       BMSR_10FULL |
970 		       BMSR_10HALF |
971 		       BMSR_ANEGCAPABLE;
972 		if (link & PORT_AUTO_NEG_COMPLETE)
973 			data |= BMSR_ANEGCOMPLETE;
974 		if (link & PORT_STAT_LINK_GOOD)
975 			data |= BMSR_LSTATUS;
976 		break;
977 	case MII_PHYSID1:
978 		data = KSZ8795_ID_HI;
979 		break;
980 	case MII_PHYSID2:
981 		if (ksz_is_ksz88x3(dev))
982 			data = KSZ8863_ID_LO;
983 		else
984 			data = KSZ8795_ID_LO;
985 		break;
986 	case MII_ADVERTISE:
987 		ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
988 		if (ret)
989 			return ret;
990 
991 		data = ADVERTISE_CSMA;
992 		if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
993 			data |= ADVERTISE_PAUSE_CAP;
994 		if (ctrl & PORT_AUTO_NEG_100BTX_FD)
995 			data |= ADVERTISE_100FULL;
996 		if (ctrl & PORT_AUTO_NEG_100BTX)
997 			data |= ADVERTISE_100HALF;
998 		if (ctrl & PORT_AUTO_NEG_10BT_FD)
999 			data |= ADVERTISE_10FULL;
1000 		if (ctrl & PORT_AUTO_NEG_10BT)
1001 			data |= ADVERTISE_10HALF;
1002 		break;
1003 	case MII_LPA:
1004 		ret = ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
1005 		if (ret)
1006 			return ret;
1007 
1008 		data = LPA_SLCT;
1009 		if (link & PORT_REMOTE_SYM_PAUSE)
1010 			data |= LPA_PAUSE_CAP;
1011 		if (link & PORT_REMOTE_100BTX_FD)
1012 			data |= LPA_100FULL;
1013 		if (link & PORT_REMOTE_100BTX)
1014 			data |= LPA_100HALF;
1015 		if (link & PORT_REMOTE_10BT_FD)
1016 			data |= LPA_10FULL;
1017 		if (link & PORT_REMOTE_10BT)
1018 			data |= LPA_10HALF;
1019 		if (data & ~LPA_SLCT)
1020 			data |= LPA_LPACK;
1021 		break;
1022 	case PHY_REG_LINK_MD:
1023 		ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1);
1024 		if (ret)
1025 			return ret;
1026 
1027 		ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2);
1028 		if (ret)
1029 			return ret;
1030 
1031 		if (val1 & PORT_START_CABLE_DIAG)
1032 			data |= PHY_START_CABLE_DIAG;
1033 
1034 		if (val1 & PORT_CABLE_10M_SHORT)
1035 			data |= PHY_CABLE_10M_SHORT;
1036 
1037 		data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M,
1038 				FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1));
1039 
1040 		data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M,
1041 				(FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) |
1042 				FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2));
1043 		break;
1044 	case PHY_REG_PHY_CTRL:
1045 		ret = ksz8_r_phy_ctrl(dev, p, &data);
1046 		if (ret)
1047 			return ret;
1048 
1049 		break;
1050 	default:
1051 		processed = false;
1052 		break;
1053 	}
1054 	if (processed)
1055 		*val = data;
1056 
1057 	return 0;
1058 }
1059 
1060 /**
1061  * ksz8_w_phy_ctrl - Translates and writes to the SMI interface from a MIIM PHY
1062  *		     Control register (Reg. 31).
1063  * @dev: The KSZ device instance.
1064  * @port: The port number to be configured.
1065  * @val: The register value to be written.
1066  *
1067  * This function translates control settings from a MIIM PHY Control register
1068  * into their corresponding hardware register bit values for the SMI
1069  * interface.
1070  *
1071  * Return: 0 on success, error code on failure.
1072  */
ksz8_w_phy_ctrl(struct ksz_device * dev,int port,u16 val)1073 static int ksz8_w_phy_ctrl(struct ksz_device *dev, int port, u16 val)
1074 {
1075 	u8 reg_val = 0;
1076 	int ret;
1077 
1078 	if (val & KSZ886X_CTRL_FORCE_LINK)
1079 		reg_val |= PORT_FORCE_LINK;
1080 
1081 	if (val & KSZ886X_CTRL_PWRSAVE)
1082 		reg_val |= PORT_POWER_SAVING;
1083 
1084 	if (val & KSZ886X_CTRL_REMOTE_LOOPBACK)
1085 		reg_val |= PORT_PHY_REMOTE_LOOPBACK;
1086 
1087 	ret = ksz_prmw8(dev, port, REG_PORT_LINK_MD_CTRL, PORT_FORCE_LINK |
1088 			PORT_POWER_SAVING | PORT_PHY_REMOTE_LOOPBACK, reg_val);
1089 	return ret;
1090 }
1091 
1092 /**
1093  * ksz8_w_phy_bmcr - Translates and writes to the SMI interface from a MIIM PHY
1094  *		     Basic mode control register (Reg. 0).
1095  * @dev: The KSZ device instance.
1096  * @port: The port number to be configured.
1097  * @val: The register value to be written.
1098  *
1099  * This function translates control settings from a MIIM PHY Basic mode control
1100  * register into their corresponding hardware register bit values for the SMI
1101  * interface.
1102  *
1103  * MIIM Bit Mapping Comparison between KSZ8794 and KSZ8873
1104  * -------------------------------------------------------------------
1105  * MIIM Bit                    | KSZ8794 Reg/Bit             | KSZ8873 Reg/Bit
1106  * ----------------------------+-----------------------------+----------------
1107  * Bit 15 - Soft Reset         | 0xF/4                       | Not supported
1108  * Bit 14 - Loopback           | 0xD/0 (MAC), 0xF/7 (PHY)    ~ 0xD/0 (PHY)
1109  * Bit 13 - Force 100          | 0xC/6                       = 0xC/6
1110  * Bit 12 - AN Enable          | 0xC/7 (reverse logic)       ~ 0xC/7
1111  * Bit 11 - Power Down         | 0xD/3                       = 0xD/3
1112  * Bit 10 - PHY Isolate        | 0xF/5                       | Not supported
1113  * Bit 9 - Restart AN          | 0xD/5                       = 0xD/5
1114  * Bit 8 - Force Full-Duplex   | 0xC/5                       = 0xC/5
1115  * Bit 7 - Collision Test/Res. | Not supported               | Not supported
1116  * Bit 6 - Reserved            | Not supported               | Not supported
1117  * Bit 5 - Hp_mdix             | 0x9/7                       ~ 0xF/7
1118  * Bit 4 - Force MDI           | 0xD/1                       = 0xD/1
1119  * Bit 3 - Disable MDIX        | 0xD/2                       = 0xD/2
1120  * Bit 2 - Disable Far-End F.  | ????                        | 0xD/4
1121  * Bit 1 - Disable Transmit    | 0xD/6                       = 0xD/6
1122  * Bit 0 - Disable LED         | 0xD/7                       = 0xD/7
1123  * -------------------------------------------------------------------
1124  *
1125  * Return: 0 on success, error code on failure.
1126  */
ksz8_w_phy_bmcr(struct ksz_device * dev,u16 port,u16 val)1127 static int ksz8_w_phy_bmcr(struct ksz_device *dev, u16 port, u16 val)
1128 {
1129 	u8 restart, speed, ctrl, restart_mask;
1130 	const u16 *regs = dev->info->regs;
1131 	int ret;
1132 
1133 	/* Do not support PHY reset function. */
1134 	if (val & BMCR_RESET)
1135 		return 0;
1136 
1137 	speed = 0;
1138 	if (val & KSZ886X_BMCR_HP_MDIX)
1139 		speed |= PORT_HP_MDIX;
1140 
1141 	ret = ksz_prmw8(dev, port, regs[P_SPEED_STATUS], PORT_HP_MDIX, speed);
1142 	if (ret)
1143 		return ret;
1144 
1145 	ctrl = 0;
1146 	if (ksz_is_ksz88x3(dev)) {
1147 		if ((val & BMCR_ANENABLE))
1148 			ctrl |= PORT_AUTO_NEG_ENABLE;
1149 	} else {
1150 		if (!(val & BMCR_ANENABLE))
1151 			ctrl |= PORT_AUTO_NEG_DISABLE;
1152 
1153 		/* Fiber port does not support auto-negotiation. */
1154 		if (dev->ports[port].fiber)
1155 			ctrl |= PORT_AUTO_NEG_DISABLE;
1156 	}
1157 
1158 	if (val & BMCR_SPEED100)
1159 		ctrl |= PORT_FORCE_100_MBIT;
1160 
1161 	if (val & BMCR_FULLDPLX)
1162 		ctrl |= PORT_FORCE_FULL_DUPLEX;
1163 
1164 	ret = ksz_prmw8(dev, port, regs[P_FORCE_CTRL], PORT_FORCE_100_MBIT |
1165 		 /* PORT_AUTO_NEG_ENABLE and PORT_AUTO_NEG_DISABLE are the same
1166 		  * bits
1167 		  */
1168 		 PORT_FORCE_FULL_DUPLEX | PORT_AUTO_NEG_ENABLE, ctrl);
1169 	if (ret)
1170 		return ret;
1171 
1172 	restart = 0;
1173 	restart_mask = PORT_LED_OFF | PORT_TX_DISABLE | PORT_AUTO_NEG_RESTART |
1174 		PORT_POWER_DOWN | PORT_AUTO_MDIX_DISABLE | PORT_FORCE_MDIX;
1175 
1176 	if (val & KSZ886X_BMCR_DISABLE_LED)
1177 		restart |= PORT_LED_OFF;
1178 
1179 	if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
1180 		restart |= PORT_TX_DISABLE;
1181 
1182 	if (val & BMCR_ANRESTART)
1183 		restart |= PORT_AUTO_NEG_RESTART;
1184 
1185 	if (val & BMCR_PDOWN)
1186 		restart |= PORT_POWER_DOWN;
1187 
1188 	if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
1189 		restart |= PORT_AUTO_MDIX_DISABLE;
1190 
1191 	if (val & KSZ886X_BMCR_FORCE_MDI)
1192 		restart |= PORT_FORCE_MDIX;
1193 
1194 	if (ksz_is_ksz88x3(dev)) {
1195 		restart_mask |= KSZ8873_PORT_PHY_LOOPBACK;
1196 
1197 		if (val & BMCR_LOOPBACK)
1198 			restart |= KSZ8873_PORT_PHY_LOOPBACK;
1199 	} else {
1200 		ret = ksz879x_set_loopback(dev, port, val);
1201 		if (ret)
1202 			return ret;
1203 	}
1204 
1205 	return ksz_prmw8(dev, port, regs[P_NEG_RESTART_CTRL], restart_mask,
1206 			 restart);
1207 }
1208 
ksz8_w_phy(struct ksz_device * dev,u16 phy,u16 reg,u16 val)1209 int ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
1210 {
1211 	const u16 *regs;
1212 	u8 ctrl, data;
1213 	u16 p = phy;
1214 	int ret;
1215 
1216 	regs = dev->info->regs;
1217 
1218 	switch (reg) {
1219 	case MII_BMCR:
1220 		ret = ksz8_w_phy_bmcr(dev, p, val);
1221 		if (ret)
1222 			return ret;
1223 		break;
1224 	case MII_ADVERTISE:
1225 		ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
1226 		if (ret)
1227 			return ret;
1228 
1229 		data = ctrl;
1230 		data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
1231 			  PORT_AUTO_NEG_100BTX_FD |
1232 			  PORT_AUTO_NEG_100BTX |
1233 			  PORT_AUTO_NEG_10BT_FD |
1234 			  PORT_AUTO_NEG_10BT);
1235 		if (val & ADVERTISE_PAUSE_CAP)
1236 			data |= PORT_AUTO_NEG_SYM_PAUSE;
1237 		if (val & ADVERTISE_100FULL)
1238 			data |= PORT_AUTO_NEG_100BTX_FD;
1239 		if (val & ADVERTISE_100HALF)
1240 			data |= PORT_AUTO_NEG_100BTX;
1241 		if (val & ADVERTISE_10FULL)
1242 			data |= PORT_AUTO_NEG_10BT_FD;
1243 		if (val & ADVERTISE_10HALF)
1244 			data |= PORT_AUTO_NEG_10BT;
1245 
1246 		if (data != ctrl) {
1247 			ret = ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
1248 			if (ret)
1249 				return ret;
1250 		}
1251 		break;
1252 	case PHY_REG_LINK_MD:
1253 		if (val & PHY_START_CABLE_DIAG)
1254 			ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true);
1255 		break;
1256 
1257 	case PHY_REG_PHY_CTRL:
1258 		ret = ksz8_w_phy_ctrl(dev, p, val);
1259 		if (ret)
1260 			return ret;
1261 		break;
1262 	default:
1263 		break;
1264 	}
1265 
1266 	return 0;
1267 }
1268 
ksz8_cfg_port_member(struct ksz_device * dev,int port,u8 member)1269 void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
1270 {
1271 	u8 data;
1272 
1273 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1274 	data &= ~PORT_VLAN_MEMBERSHIP;
1275 	data |= (member & dev->port_mask);
1276 	ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
1277 }
1278 
ksz8_flush_dyn_mac_table(struct ksz_device * dev,int port)1279 void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
1280 {
1281 	u8 learn[DSA_MAX_PORTS];
1282 	int first, index, cnt;
1283 	const u16 *regs;
1284 
1285 	regs = dev->info->regs;
1286 
1287 	if ((uint)port < dev->info->port_cnt) {
1288 		first = port;
1289 		cnt = port + 1;
1290 	} else {
1291 		/* Flush all ports. */
1292 		first = 0;
1293 		cnt = dev->info->port_cnt;
1294 	}
1295 	for (index = first; index < cnt; index++) {
1296 		ksz_pread8(dev, index, regs[P_STP_CTRL], &learn[index]);
1297 		if (!(learn[index] & PORT_LEARN_DISABLE))
1298 			ksz_pwrite8(dev, index, regs[P_STP_CTRL],
1299 				    learn[index] | PORT_LEARN_DISABLE);
1300 	}
1301 	ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
1302 	for (index = first; index < cnt; index++) {
1303 		if (!(learn[index] & PORT_LEARN_DISABLE))
1304 			ksz_pwrite8(dev, index, regs[P_STP_CTRL], learn[index]);
1305 	}
1306 }
1307 
ksz8_fdb_dump(struct ksz_device * dev,int port,dsa_fdb_dump_cb_t * cb,void * data)1308 int ksz8_fdb_dump(struct ksz_device *dev, int port,
1309 		  dsa_fdb_dump_cb_t *cb, void *data)
1310 {
1311 	u8 mac[ETH_ALEN];
1312 	u8 src_port, fid;
1313 	u16 entries = 0;
1314 	int ret, i;
1315 
1316 	for (i = 0; i < KSZ8_DYN_MAC_ENTRIES; i++) {
1317 		ret = ksz8_r_dyn_mac_table(dev, i, mac, &fid, &src_port,
1318 					   &entries);
1319 		if (ret)
1320 			return ret;
1321 
1322 		if (i >= entries)
1323 			return 0;
1324 
1325 		if (port == src_port) {
1326 			ret = cb(mac, fid, false, data);
1327 			if (ret)
1328 				return ret;
1329 		}
1330 	}
1331 
1332 	return 0;
1333 }
1334 
ksz8_add_sta_mac(struct ksz_device * dev,int port,const unsigned char * addr,u16 vid)1335 static int ksz8_add_sta_mac(struct ksz_device *dev, int port,
1336 			    const unsigned char *addr, u16 vid)
1337 {
1338 	struct alu_struct alu;
1339 	int index, ret;
1340 	int empty = 0;
1341 
1342 	alu.port_forward = 0;
1343 	for (index = 0; index < dev->info->num_statics; index++) {
1344 		bool valid;
1345 
1346 		ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1347 		if (ret)
1348 			return ret;
1349 		if (!valid) {
1350 			/* Remember the first empty entry. */
1351 			if (!empty)
1352 				empty = index + 1;
1353 			continue;
1354 		}
1355 
1356 		if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1357 			break;
1358 	}
1359 
1360 	/* no available entry */
1361 	if (index == dev->info->num_statics && !empty)
1362 		return -ENOSPC;
1363 
1364 	/* add entry */
1365 	if (index == dev->info->num_statics) {
1366 		index = empty - 1;
1367 		memset(&alu, 0, sizeof(alu));
1368 		memcpy(alu.mac, addr, ETH_ALEN);
1369 		alu.is_static = true;
1370 	}
1371 	alu.port_forward |= BIT(port);
1372 	if (vid) {
1373 		alu.is_use_fid = true;
1374 
1375 		/* Need a way to map VID to FID. */
1376 		alu.fid = vid;
1377 	}
1378 
1379 	return ksz8_w_sta_mac_table(dev, index, &alu);
1380 }
1381 
ksz8_del_sta_mac(struct ksz_device * dev,int port,const unsigned char * addr,u16 vid)1382 static int ksz8_del_sta_mac(struct ksz_device *dev, int port,
1383 			    const unsigned char *addr, u16 vid)
1384 {
1385 	struct alu_struct alu;
1386 	int index, ret;
1387 
1388 	for (index = 0; index < dev->info->num_statics; index++) {
1389 		bool valid;
1390 
1391 		ret = ksz8_r_sta_mac_table(dev, index, &alu, &valid);
1392 		if (ret)
1393 			return ret;
1394 		if (!valid)
1395 			continue;
1396 
1397 		if (!memcmp(alu.mac, addr, ETH_ALEN) && alu.fid == vid)
1398 			break;
1399 	}
1400 
1401 	/* no available entry */
1402 	if (index == dev->info->num_statics)
1403 		return 0;
1404 
1405 	/* clear port */
1406 	alu.port_forward &= ~BIT(port);
1407 	if (!alu.port_forward)
1408 		alu.is_static = false;
1409 
1410 	return ksz8_w_sta_mac_table(dev, index, &alu);
1411 }
1412 
ksz8_mdb_add(struct ksz_device * dev,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)1413 int ksz8_mdb_add(struct ksz_device *dev, int port,
1414 		 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1415 {
1416 	return ksz8_add_sta_mac(dev, port, mdb->addr, mdb->vid);
1417 }
1418 
ksz8_mdb_del(struct ksz_device * dev,int port,const struct switchdev_obj_port_mdb * mdb,struct dsa_db db)1419 int ksz8_mdb_del(struct ksz_device *dev, int port,
1420 		 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1421 {
1422 	return ksz8_del_sta_mac(dev, port, mdb->addr, mdb->vid);
1423 }
1424 
ksz8_fdb_add(struct ksz_device * dev,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1425 int ksz8_fdb_add(struct ksz_device *dev, int port, const unsigned char *addr,
1426 		 u16 vid, struct dsa_db db)
1427 {
1428 	return ksz8_add_sta_mac(dev, port, addr, vid);
1429 }
1430 
ksz8_fdb_del(struct ksz_device * dev,int port,const unsigned char * addr,u16 vid,struct dsa_db db)1431 int ksz8_fdb_del(struct ksz_device *dev, int port, const unsigned char *addr,
1432 		 u16 vid, struct dsa_db db)
1433 {
1434 	return ksz8_del_sta_mac(dev, port, addr, vid);
1435 }
1436 
ksz8_port_vlan_filtering(struct ksz_device * dev,int port,bool flag,struct netlink_ext_ack * extack)1437 int ksz8_port_vlan_filtering(struct ksz_device *dev, int port, bool flag,
1438 			     struct netlink_ext_ack *extack)
1439 {
1440 	if (ksz_is_ksz88x3(dev))
1441 		return -ENOTSUPP;
1442 
1443 	/* Discard packets with VID not enabled on the switch */
1444 	ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
1445 
1446 	/* Discard packets with VID not enabled on the ingress port */
1447 	for (port = 0; port < dev->phy_port_cnt; ++port)
1448 		ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
1449 			     flag);
1450 
1451 	return 0;
1452 }
1453 
ksz8_port_enable_pvid(struct ksz_device * dev,int port,bool state)1454 static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state)
1455 {
1456 	if (ksz_is_ksz88x3(dev)) {
1457 		ksz_cfg(dev, REG_SW_INSERT_SRC_PVID,
1458 			0x03 << (4 - 2 * port), state);
1459 	} else {
1460 		ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00);
1461 	}
1462 }
1463 
ksz8_port_vlan_add(struct ksz_device * dev,int port,const struct switchdev_obj_port_vlan * vlan,struct netlink_ext_ack * extack)1464 int ksz8_port_vlan_add(struct ksz_device *dev, int port,
1465 		       const struct switchdev_obj_port_vlan *vlan,
1466 		       struct netlink_ext_ack *extack)
1467 {
1468 	bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1469 	struct ksz_port *p = &dev->ports[port];
1470 	u16 data, new_pvid = 0;
1471 	u8 fid, member, valid;
1472 
1473 	if (ksz_is_ksz88x3(dev))
1474 		return -ENOTSUPP;
1475 
1476 	/* If a VLAN is added with untagged flag different from the
1477 	 * port's Remove Tag flag, we need to change the latter.
1478 	 * Ignore VID 0, which is always untagged.
1479 	 * Ignore CPU port, which will always be tagged.
1480 	 */
1481 	if (untagged != p->remove_tag && vlan->vid != 0 &&
1482 	    port != dev->cpu_port) {
1483 		unsigned int vid;
1484 
1485 		/* Reject attempts to add a VLAN that requires the
1486 		 * Remove Tag flag to be changed, unless there are no
1487 		 * other VLANs currently configured.
1488 		 */
1489 		for (vid = 1; vid < dev->info->num_vlans; ++vid) {
1490 			/* Skip the VID we are going to add or reconfigure */
1491 			if (vid == vlan->vid)
1492 				continue;
1493 
1494 			ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0],
1495 				       &fid, &member, &valid);
1496 			if (valid && (member & BIT(port)))
1497 				return -EINVAL;
1498 		}
1499 
1500 		ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1501 		p->remove_tag = untagged;
1502 	}
1503 
1504 	ksz8_r_vlan_table(dev, vlan->vid, &data);
1505 	ksz8_from_vlan(dev, data, &fid, &member, &valid);
1506 
1507 	/* First time to setup the VLAN entry. */
1508 	if (!valid) {
1509 		/* Need to find a way to map VID to FID. */
1510 		fid = 1;
1511 		valid = 1;
1512 	}
1513 	member |= BIT(port);
1514 
1515 	ksz8_to_vlan(dev, fid, member, valid, &data);
1516 	ksz8_w_vlan_table(dev, vlan->vid, data);
1517 
1518 	/* change PVID */
1519 	if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
1520 		new_pvid = vlan->vid;
1521 
1522 	if (new_pvid) {
1523 		u16 vid;
1524 
1525 		ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
1526 		vid &= ~VLAN_VID_MASK;
1527 		vid |= new_pvid;
1528 		ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
1529 
1530 		ksz8_port_enable_pvid(dev, port, true);
1531 	}
1532 
1533 	return 0;
1534 }
1535 
ksz8_port_vlan_del(struct ksz_device * dev,int port,const struct switchdev_obj_port_vlan * vlan)1536 int ksz8_port_vlan_del(struct ksz_device *dev, int port,
1537 		       const struct switchdev_obj_port_vlan *vlan)
1538 {
1539 	u16 data, pvid;
1540 	u8 fid, member, valid;
1541 
1542 	if (ksz_is_ksz88x3(dev))
1543 		return -ENOTSUPP;
1544 
1545 	ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
1546 	pvid = pvid & 0xFFF;
1547 
1548 	ksz8_r_vlan_table(dev, vlan->vid, &data);
1549 	ksz8_from_vlan(dev, data, &fid, &member, &valid);
1550 
1551 	member &= ~BIT(port);
1552 
1553 	/* Invalidate the entry if no more member. */
1554 	if (!member) {
1555 		fid = 0;
1556 		valid = 0;
1557 	}
1558 
1559 	ksz8_to_vlan(dev, fid, member, valid, &data);
1560 	ksz8_w_vlan_table(dev, vlan->vid, data);
1561 
1562 	if (pvid == vlan->vid)
1563 		ksz8_port_enable_pvid(dev, port, false);
1564 
1565 	return 0;
1566 }
1567 
ksz8_port_mirror_add(struct ksz_device * dev,int port,struct dsa_mall_mirror_tc_entry * mirror,bool ingress,struct netlink_ext_ack * extack)1568 int ksz8_port_mirror_add(struct ksz_device *dev, int port,
1569 			 struct dsa_mall_mirror_tc_entry *mirror,
1570 			 bool ingress, struct netlink_ext_ack *extack)
1571 {
1572 	if (ingress) {
1573 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1574 		dev->mirror_rx |= BIT(port);
1575 	} else {
1576 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1577 		dev->mirror_tx |= BIT(port);
1578 	}
1579 
1580 	ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1581 
1582 	/* configure mirror port */
1583 	if (dev->mirror_rx || dev->mirror_tx)
1584 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1585 			     PORT_MIRROR_SNIFFER, true);
1586 
1587 	return 0;
1588 }
1589 
ksz8_port_mirror_del(struct ksz_device * dev,int port,struct dsa_mall_mirror_tc_entry * mirror)1590 void ksz8_port_mirror_del(struct ksz_device *dev, int port,
1591 			  struct dsa_mall_mirror_tc_entry *mirror)
1592 {
1593 	u8 data;
1594 
1595 	if (mirror->ingress) {
1596 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1597 		dev->mirror_rx &= ~BIT(port);
1598 	} else {
1599 		ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1600 		dev->mirror_tx &= ~BIT(port);
1601 	}
1602 
1603 	ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1604 
1605 	if (!dev->mirror_rx && !dev->mirror_tx)
1606 		ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1607 			     PORT_MIRROR_SNIFFER, false);
1608 }
1609 
ksz8795_cpu_interface_select(struct ksz_device * dev,int port)1610 static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
1611 {
1612 	struct ksz_port *p = &dev->ports[port];
1613 
1614 	if (!ksz_is_ksz87xx(dev))
1615 		return;
1616 
1617 	if (!p->interface && dev->compat_interface) {
1618 		dev_warn(dev->dev,
1619 			 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1620 			 "Please update your device tree.\n",
1621 			 port);
1622 		p->interface = dev->compat_interface;
1623 	}
1624 }
1625 
ksz8_port_setup(struct ksz_device * dev,int port,bool cpu_port)1626 void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1627 {
1628 	const u16 *regs = dev->info->regs;
1629 	struct dsa_switch *ds = dev->ds;
1630 	const u32 *masks;
1631 	int queues;
1632 	u8 member;
1633 
1634 	masks = dev->info->masks;
1635 
1636 	/* enable broadcast storm limit */
1637 	ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1638 
1639 	/* For KSZ88x3 enable only one queue by default, otherwise we won't
1640 	 * be able to get rid of PCP prios on Port 2.
1641 	 */
1642 	if (ksz_is_ksz88x3(dev))
1643 		queues = 1;
1644 	else
1645 		queues = dev->info->num_tx_queues;
1646 
1647 	ksz8_port_queue_split(dev, port, queues);
1648 
1649 	/* replace priority */
1650 	ksz_port_cfg(dev, port, P_802_1P_CTRL,
1651 		     masks[PORT_802_1P_REMAPPING], false);
1652 
1653 	if (cpu_port)
1654 		member = dsa_user_ports(ds);
1655 	else
1656 		member = BIT(dsa_upstream_port(ds, port));
1657 
1658 	ksz8_cfg_port_member(dev, port, member);
1659 
1660 	/* Disable all WoL options by default. Otherwise
1661 	 * ksz_switch_macaddr_get/put logic will not work properly.
1662 	 * CPU port 4 has no WoL functionality.
1663 	 */
1664 	if (ksz_is_ksz87xx(dev) && !cpu_port)
1665 		ksz8_pme_pwrite8(dev, port, regs[REG_PORT_PME_CTRL], 0);
1666 }
1667 
ksz88x3_config_rmii_clk(struct ksz_device * dev)1668 static void ksz88x3_config_rmii_clk(struct ksz_device *dev)
1669 {
1670 	struct dsa_port *cpu_dp = dsa_to_port(dev->ds, dev->cpu_port);
1671 	bool rmii_clk_internal;
1672 
1673 	if (!ksz_is_ksz88x3(dev))
1674 		return;
1675 
1676 	rmii_clk_internal = of_property_read_bool(cpu_dp->dn,
1677 						  "microchip,rmii-clk-internal");
1678 
1679 	ksz_cfg(dev, KSZ88X3_REG_FVID_AND_HOST_MODE,
1680 		KSZ88X3_PORT3_RMII_CLK_INTERNAL, rmii_clk_internal);
1681 }
1682 
ksz8_config_cpu_port(struct dsa_switch * ds)1683 void ksz8_config_cpu_port(struct dsa_switch *ds)
1684 {
1685 	struct ksz_device *dev = ds->priv;
1686 	struct ksz_port *p;
1687 	const u32 *masks;
1688 	const u16 *regs;
1689 	u8 remote;
1690 	int i;
1691 
1692 	masks = dev->info->masks;
1693 	regs = dev->info->regs;
1694 
1695 	ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
1696 
1697 	ksz8_port_setup(dev, dev->cpu_port, true);
1698 
1699 	ksz8795_cpu_interface_select(dev, dev->cpu_port);
1700 	ksz88x3_config_rmii_clk(dev);
1701 
1702 	for (i = 0; i < dev->phy_port_cnt; i++) {
1703 		ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1704 	}
1705 	for (i = 0; i < dev->phy_port_cnt; i++) {
1706 		p = &dev->ports[i];
1707 
1708 		/* For KSZ8795 family. */
1709 		if (ksz_is_ksz87xx(dev)) {
1710 			ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
1711 			if (remote & KSZ8_PORT_FIBER_MODE)
1712 				p->fiber = 1;
1713 		}
1714 		if (p->fiber)
1715 			ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1716 				     PORT_FORCE_FLOW_CTRL, true);
1717 		else
1718 			ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1719 				     PORT_FORCE_FLOW_CTRL, false);
1720 	}
1721 }
1722 
1723 /**
1724  * ksz8_phy_port_link_up - Configures ports with integrated PHYs
1725  * @dev: The KSZ device instance.
1726  * @port: The port number to configure.
1727  * @duplex: The desired duplex mode.
1728  * @tx_pause: If true, enables transmit pause.
1729  * @rx_pause: If true, enables receive pause.
1730  *
1731  * Description:
1732  * The function configures flow control settings for a given port based on the
1733  * desired settings and current duplex mode.
1734  *
1735  * According to the KSZ8873 datasheet, the PORT_FORCE_FLOW_CTRL bit in the
1736  * Port Control 2 register (0x1A for Port 1, 0x22 for Port 2, 0x32 for Port 3)
1737  * determines how flow control is handled on the port:
1738  *    "1 = will always enable full-duplex flow control on the port, regardless
1739  *         of AN result.
1740  *     0 = full-duplex flow control is enabled based on AN result."
1741  *
1742  * This means that the flow control behavior depends on the state of this bit:
1743  * - If PORT_FORCE_FLOW_CTRL is set to 1, the switch will ignore AN results and
1744  *   force flow control on the port.
1745  * - If PORT_FORCE_FLOW_CTRL is set to 0, the switch will enable or disable
1746  *   flow control based on the AN results.
1747  *
1748  * However, there is a potential limitation in this configuration. It is
1749  * currently not possible to force disable flow control on a port if we still
1750  * advertise pause support. While such a configuration is not currently
1751  * supported by Linux, and may not make practical sense, it's important to be
1752  * aware of this limitation when working with the KSZ8873 and similar devices.
1753  */
ksz8_phy_port_link_up(struct ksz_device * dev,int port,int duplex,bool tx_pause,bool rx_pause)1754 static void ksz8_phy_port_link_up(struct ksz_device *dev, int port, int duplex,
1755 				  bool tx_pause, bool rx_pause)
1756 {
1757 	const u16 *regs = dev->info->regs;
1758 	u8 sctrl = 0;
1759 
1760 	/* The KSZ8795 switch differs from the KSZ8873 by supporting
1761 	 * asymmetric pause control. However, since a single bit is used to
1762 	 * control both RX and TX pause, we can't enforce asymmetric pause
1763 	 * control - both TX and RX pause will be either enabled or disabled
1764 	 * together.
1765 	 *
1766 	 * If auto-negotiation is enabled, we usually allow the flow control to
1767 	 * be determined by the auto-negotiation process based on the
1768 	 * capabilities of both link partners. However, for KSZ8873, the
1769 	 * PORT_FORCE_FLOW_CTRL bit may be set by the hardware bootstrap,
1770 	 * ignoring the auto-negotiation result. Thus, even in auto-negotiation
1771 	 * mode, we need to ensure that the PORT_FORCE_FLOW_CTRL bit is
1772 	 * properly cleared.
1773 	 *
1774 	 * In the absence of pause auto-negotiation, we will enforce symmetric
1775 	 * pause control for both variants of switches - KSZ8873 and KSZ8795.
1776 	 *
1777 	 * Autoneg Pause Autoneg      rx,tx	PORT_FORCE_FLOW_CTRL
1778 	 * 1		1		x	0
1779 	 * 0		1		x	0 (flow control probably disabled)
1780 	 * x		0		1	1 (flow control force enabled)
1781 	 * 1		0		0	0 (flow control still depends on
1782 	 *					   aneg result due to hardware)
1783 	 * 0		0		0	0 (flow control probably disabled)
1784 	 */
1785 	if (dev->ports[port].manual_flow && tx_pause)
1786 		sctrl |= PORT_FORCE_FLOW_CTRL;
1787 
1788 	ksz_prmw8(dev, port, regs[P_STP_CTRL], PORT_FORCE_FLOW_CTRL, sctrl);
1789 }
1790 
1791 /**
1792  * ksz8_cpu_port_link_up - Configures the CPU port of the switch.
1793  * @dev: The KSZ device instance.
1794  * @speed: The desired link speed.
1795  * @duplex: The desired duplex mode.
1796  * @tx_pause: If true, enables transmit pause.
1797  * @rx_pause: If true, enables receive pause.
1798  *
1799  * Description:
1800  * The function configures flow control and speed settings for the CPU
1801  * port of the switch based on the desired settings, current duplex mode, and
1802  * speed.
1803  */
ksz8_cpu_port_link_up(struct ksz_device * dev,int speed,int duplex,bool tx_pause,bool rx_pause)1804 static void ksz8_cpu_port_link_up(struct ksz_device *dev, int speed, int duplex,
1805 				  bool tx_pause, bool rx_pause)
1806 {
1807 	const u16 *regs = dev->info->regs;
1808 	u8 ctrl = 0;
1809 
1810 	/* SW_FLOW_CTRL, SW_HALF_DUPLEX, and SW_10_MBIT bits are bootstrappable
1811 	 * at least on KSZ8873. They can have different values depending on your
1812 	 * board setup.
1813 	 */
1814 	if (tx_pause || rx_pause)
1815 		ctrl |= SW_FLOW_CTRL;
1816 
1817 	if (duplex == DUPLEX_HALF)
1818 		ctrl |= SW_HALF_DUPLEX;
1819 
1820 	/* This hardware only supports SPEED_10 and SPEED_100. For SPEED_10
1821 	 * we need to set the SW_10_MBIT bit. Otherwise, we can leave it 0.
1822 	 */
1823 	if (speed == SPEED_10)
1824 		ctrl |= SW_10_MBIT;
1825 
1826 	ksz_rmw8(dev, regs[S_BROADCAST_CTRL], SW_HALF_DUPLEX | SW_FLOW_CTRL |
1827 		 SW_10_MBIT, ctrl);
1828 }
1829 
ksz8_phylink_mac_link_up(struct phylink_config * config,struct phy_device * phydev,unsigned int mode,phy_interface_t interface,int speed,int duplex,bool tx_pause,bool rx_pause)1830 void ksz8_phylink_mac_link_up(struct phylink_config *config,
1831 			      struct phy_device *phydev, unsigned int mode,
1832 			      phy_interface_t interface, int speed, int duplex,
1833 			      bool tx_pause, bool rx_pause)
1834 {
1835 	struct dsa_port *dp = dsa_phylink_to_port(config);
1836 	struct ksz_device *dev = dp->ds->priv;
1837 	int port = dp->index;
1838 
1839 	/* If the port is the CPU port, apply special handling. Only the CPU
1840 	 * port is configured via global registers.
1841 	 */
1842 	if (dev->cpu_port == port)
1843 		ksz8_cpu_port_link_up(dev, speed, duplex, tx_pause, rx_pause);
1844 	else if (dev->info->internal_phy[port])
1845 		ksz8_phy_port_link_up(dev, port, duplex, tx_pause, rx_pause);
1846 }
1847 
ksz8_handle_global_errata(struct dsa_switch * ds)1848 static int ksz8_handle_global_errata(struct dsa_switch *ds)
1849 {
1850 	struct ksz_device *dev = ds->priv;
1851 	int ret = 0;
1852 
1853 	/* KSZ87xx Errata DS80000687C.
1854 	 * Module 2: Link drops with some EEE link partners.
1855 	 *   An issue with the EEE next page exchange between the
1856 	 *   KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in
1857 	 *   the link dropping.
1858 	 */
1859 	if (dev->info->ksz87xx_eee_link_erratum)
1860 		ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0);
1861 
1862 	return ret;
1863 }
1864 
ksz8_enable_stp_addr(struct ksz_device * dev)1865 int ksz8_enable_stp_addr(struct ksz_device *dev)
1866 {
1867 	struct alu_struct alu;
1868 
1869 	/* Setup STP address for STP operation. */
1870 	memset(&alu, 0, sizeof(alu));
1871 	ether_addr_copy(alu.mac, eth_stp_addr);
1872 	alu.is_static = true;
1873 	alu.is_override = true;
1874 	alu.port_forward = dev->info->cpu_ports;
1875 
1876 	return ksz8_w_sta_mac_table(dev, 0, &alu);
1877 }
1878 
ksz8_setup(struct dsa_switch * ds)1879 int ksz8_setup(struct dsa_switch *ds)
1880 {
1881 	struct ksz_device *dev = ds->priv;
1882 	const u16 *regs = dev->info->regs;
1883 	int i, ret = 0;
1884 
1885 	ds->mtu_enforcement_ingress = true;
1886 
1887 	/* We rely on software untagging on the CPU port, so that we
1888 	 * can support both tagged and untagged VLANs
1889 	 */
1890 	ds->untag_bridge_pvid = true;
1891 
1892 	/* VLAN filtering is partly controlled by the global VLAN
1893 	 * Enable flag
1894 	 */
1895 	ds->vlan_filtering_is_global = true;
1896 
1897 	/* Enable automatic fast aging when link changed detected. */
1898 	ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1899 
1900 	/* Enable aggressive back off algorithm in half duplex mode. */
1901 	regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_1,
1902 			   SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1903 
1904 	/*
1905 	 * Make sure unicast VLAN boundary is set as default and
1906 	 * enable no excessive collision drop.
1907 	 */
1908 	regmap_update_bits(ksz_regmap_8(dev), REG_SW_CTRL_2,
1909 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1910 			   UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1911 
1912 	ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1913 
1914 	ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1915 
1916 	if (!ksz_is_ksz88x3(dev))
1917 		ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
1918 
1919 	for (i = 0; i < (dev->info->num_vlans / 4); i++)
1920 		ksz8_r_vlan_entries(dev, i);
1921 
1922 	/* Make sure PME (WoL) is not enabled. If requested, it will
1923 	 * be enabled by ksz_wol_pre_shutdown(). Otherwise, some PMICs
1924 	 * do not like PME events changes before shutdown. PME only
1925 	 * available on KSZ87xx family.
1926 	 */
1927 	if (ksz_is_ksz87xx(dev)) {
1928 		ret = ksz8_pme_write8(dev, regs[REG_SW_PME_CTRL], 0);
1929 		if (!ret)
1930 			ret = ksz_rmw8(dev, REG_INT_ENABLE, INT_PME, 0);
1931 	}
1932 
1933 	if (!ret)
1934 		return ksz8_handle_global_errata(ds);
1935 	else
1936 		return ret;
1937 }
1938 
ksz8_get_caps(struct ksz_device * dev,int port,struct phylink_config * config)1939 void ksz8_get_caps(struct ksz_device *dev, int port,
1940 		   struct phylink_config *config)
1941 {
1942 	config->mac_capabilities = MAC_10 | MAC_100;
1943 
1944 	/* Silicon Errata Sheet (DS80000830A):
1945 	 * "Port 1 does not respond to received flow control PAUSE frames"
1946 	 * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
1947 	 * switches.
1948 	 */
1949 	if (!ksz_is_ksz88x3(dev) || port)
1950 		config->mac_capabilities |= MAC_SYM_PAUSE;
1951 
1952 	/* Asym pause is not supported on KSZ8863 and KSZ8873 */
1953 	if (!ksz_is_ksz88x3(dev))
1954 		config->mac_capabilities |= MAC_ASYM_PAUSE;
1955 }
1956 
ksz8_get_port_addr(int port,int offset)1957 u32 ksz8_get_port_addr(int port, int offset)
1958 {
1959 	return PORT_CTRL_ADDR(port, offset);
1960 }
1961 
ksz8_switch_init(struct ksz_device * dev)1962 int ksz8_switch_init(struct ksz_device *dev)
1963 {
1964 	dev->cpu_port = fls(dev->info->cpu_ports) - 1;
1965 	dev->phy_port_cnt = dev->info->port_cnt - 1;
1966 	dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports;
1967 
1968 	return 0;
1969 }
1970 
ksz8_switch_exit(struct ksz_device * dev)1971 void ksz8_switch_exit(struct ksz_device *dev)
1972 {
1973 	ksz8_reset_switch(dev);
1974 }
1975 
1976 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1977 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1978 MODULE_LICENSE("GPL");
1979