Loading...
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Microchip KSZ8795 switch driver
4 *
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
7 */
8
9#include <linux/bitfield.h>
10#include <linux/delay.h>
11#include <linux/export.h>
12#include <linux/gpio.h>
13#include <linux/if_vlan.h>
14#include <linux/kernel.h>
15#include <linux/module.h>
16#include <linux/platform_data/microchip-ksz.h>
17#include <linux/phy.h>
18#include <linux/etherdevice.h>
19#include <linux/if_bridge.h>
20#include <linux/micrel_phy.h>
21#include <net/dsa.h>
22#include <net/switchdev.h>
23#include <linux/phylink.h>
24
25#include "ksz_common.h"
26#include "ksz8795_reg.h"
27#include "ksz8.h"
28
29static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
30{
31 regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
32}
33
34static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
35 bool set)
36{
37 regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
38 bits, set ? bits : 0);
39}
40
41static int ksz8_ind_write8(struct ksz_device *dev, u8 table, u16 addr, u8 data)
42{
43 const u16 *regs;
44 u16 ctrl_addr;
45 int ret = 0;
46
47 regs = dev->info->regs;
48
49 mutex_lock(&dev->alu_mutex);
50
51 ctrl_addr = IND_ACC_TABLE(table) | addr;
52 ret = ksz_write8(dev, regs[REG_IND_BYTE], data);
53 if (!ret)
54 ret = ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
55
56 mutex_unlock(&dev->alu_mutex);
57
58 return ret;
59}
60
61int ksz8_reset_switch(struct ksz_device *dev)
62{
63 if (ksz_is_ksz88x3(dev)) {
64 /* reset switch */
65 ksz_cfg(dev, KSZ8863_REG_SW_RESET,
66 KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, true);
67 ksz_cfg(dev, KSZ8863_REG_SW_RESET,
68 KSZ8863_GLOBAL_SOFTWARE_RESET | KSZ8863_PCS_RESET, false);
69 } else {
70 /* reset switch */
71 ksz_write8(dev, REG_POWER_MANAGEMENT_1,
72 SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
73 ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
74 }
75
76 return 0;
77}
78
79static int ksz8863_change_mtu(struct ksz_device *dev, int frame_size)
80{
81 u8 ctrl2 = 0;
82
83 if (frame_size <= KSZ8_LEGAL_PACKET_SIZE)
84 ctrl2 |= KSZ8863_LEGAL_PACKET_ENABLE;
85 else if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
86 ctrl2 |= KSZ8863_HUGE_PACKET_ENABLE;
87
88 return ksz_rmw8(dev, REG_SW_CTRL_2, KSZ8863_LEGAL_PACKET_ENABLE |
89 KSZ8863_HUGE_PACKET_ENABLE, ctrl2);
90}
91
92static int ksz8795_change_mtu(struct ksz_device *dev, int frame_size)
93{
94 u8 ctrl1 = 0, ctrl2 = 0;
95 int ret;
96
97 if (frame_size > KSZ8_LEGAL_PACKET_SIZE)
98 ctrl2 |= SW_LEGAL_PACKET_DISABLE;
99 else if (frame_size > KSZ8863_NORMAL_PACKET_SIZE)
100 ctrl1 |= SW_HUGE_PACKET;
101
102 ret = ksz_rmw8(dev, REG_SW_CTRL_1, SW_HUGE_PACKET, ctrl1);
103 if (ret)
104 return ret;
105
106 return ksz_rmw8(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, ctrl2);
107}
108
109int ksz8_change_mtu(struct ksz_device *dev, int port, int mtu)
110{
111 u16 frame_size;
112
113 if (!dsa_is_cpu_port(dev->ds, port))
114 return 0;
115
116 frame_size = mtu + VLAN_ETH_HLEN + ETH_FCS_LEN;
117
118 switch (dev->chip_id) {
119 case KSZ8795_CHIP_ID:
120 case KSZ8794_CHIP_ID:
121 case KSZ8765_CHIP_ID:
122 return ksz8795_change_mtu(dev, frame_size);
123 case KSZ8830_CHIP_ID:
124 return ksz8863_change_mtu(dev, frame_size);
125 }
126
127 return -EOPNOTSUPP;
128}
129
130static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue)
131{
132 u8 hi, lo;
133
134 /* Number of queues can only be 1, 2, or 4. */
135 switch (queue) {
136 case 4:
137 case 3:
138 queue = PORT_QUEUE_SPLIT_4;
139 break;
140 case 2:
141 queue = PORT_QUEUE_SPLIT_2;
142 break;
143 default:
144 queue = PORT_QUEUE_SPLIT_1;
145 }
146 ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo);
147 ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi);
148 lo &= ~PORT_QUEUE_SPLIT_L;
149 if (queue & PORT_QUEUE_SPLIT_2)
150 lo |= PORT_QUEUE_SPLIT_L;
151 hi &= ~PORT_QUEUE_SPLIT_H;
152 if (queue & PORT_QUEUE_SPLIT_4)
153 hi |= PORT_QUEUE_SPLIT_H;
154 ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo);
155 ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi);
156
157 /* Default is port based for egress rate limit. */
158 if (queue != PORT_QUEUE_SPLIT_1)
159 ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED,
160 true);
161}
162
163void ksz8_r_mib_cnt(struct ksz_device *dev, int port, u16 addr, u64 *cnt)
164{
165 const u32 *masks;
166 const u16 *regs;
167 u16 ctrl_addr;
168 u32 data;
169 u8 check;
170 int loop;
171
172 masks = dev->info->masks;
173 regs = dev->info->regs;
174
175 ctrl_addr = addr + dev->info->reg_mib_cnt * port;
176 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
177
178 mutex_lock(&dev->alu_mutex);
179 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
180
181 /* It is almost guaranteed to always read the valid bit because of
182 * slow SPI speed.
183 */
184 for (loop = 2; loop > 0; loop--) {
185 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
186
187 if (check & masks[MIB_COUNTER_VALID]) {
188 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
189 if (check & masks[MIB_COUNTER_OVERFLOW])
190 *cnt += MIB_COUNTER_VALUE + 1;
191 *cnt += data & MIB_COUNTER_VALUE;
192 break;
193 }
194 }
195 mutex_unlock(&dev->alu_mutex);
196}
197
198static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
199 u64 *dropped, u64 *cnt)
200{
201 const u32 *masks;
202 const u16 *regs;
203 u16 ctrl_addr;
204 u32 data;
205 u8 check;
206 int loop;
207
208 masks = dev->info->masks;
209 regs = dev->info->regs;
210
211 addr -= dev->info->reg_mib_cnt;
212 ctrl_addr = (KSZ8795_MIB_TOTAL_RX_1 - KSZ8795_MIB_TOTAL_RX_0) * port;
213 ctrl_addr += addr + KSZ8795_MIB_TOTAL_RX_0;
214 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
215
216 mutex_lock(&dev->alu_mutex);
217 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
218
219 /* It is almost guaranteed to always read the valid bit because of
220 * slow SPI speed.
221 */
222 for (loop = 2; loop > 0; loop--) {
223 ksz_read8(dev, regs[REG_IND_MIB_CHECK], &check);
224
225 if (check & masks[MIB_COUNTER_VALID]) {
226 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
227 if (addr < 2) {
228 u64 total;
229
230 total = check & MIB_TOTAL_BYTES_H;
231 total <<= 32;
232 *cnt += total;
233 *cnt += data;
234 if (check & masks[MIB_COUNTER_OVERFLOW]) {
235 total = MIB_TOTAL_BYTES_H + 1;
236 total <<= 32;
237 *cnt += total;
238 }
239 } else {
240 if (check & masks[MIB_COUNTER_OVERFLOW])
241 *cnt += MIB_PACKET_DROPPED + 1;
242 *cnt += data & MIB_PACKET_DROPPED;
243 }
244 break;
245 }
246 }
247 mutex_unlock(&dev->alu_mutex);
248}
249
250static void ksz8863_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
251 u64 *dropped, u64 *cnt)
252{
253 u32 *last = (u32 *)dropped;
254 const u16 *regs;
255 u16 ctrl_addr;
256 u32 data;
257 u32 cur;
258
259 regs = dev->info->regs;
260
261 addr -= dev->info->reg_mib_cnt;
262 ctrl_addr = addr ? KSZ8863_MIB_PACKET_DROPPED_TX_0 :
263 KSZ8863_MIB_PACKET_DROPPED_RX_0;
264 ctrl_addr += port;
265 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
266
267 mutex_lock(&dev->alu_mutex);
268 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
269 ksz_read32(dev, regs[REG_IND_DATA_LO], &data);
270 mutex_unlock(&dev->alu_mutex);
271
272 data &= MIB_PACKET_DROPPED;
273 cur = last[addr];
274 if (data != cur) {
275 last[addr] = data;
276 if (data < cur)
277 data += MIB_PACKET_DROPPED + 1;
278 data -= cur;
279 *cnt += data;
280 }
281}
282
283void ksz8_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
284 u64 *dropped, u64 *cnt)
285{
286 if (ksz_is_ksz88x3(dev))
287 ksz8863_r_mib_pkt(dev, port, addr, dropped, cnt);
288 else
289 ksz8795_r_mib_pkt(dev, port, addr, dropped, cnt);
290}
291
292void ksz8_freeze_mib(struct ksz_device *dev, int port, bool freeze)
293{
294 if (ksz_is_ksz88x3(dev))
295 return;
296
297 /* enable the port for flush/freeze function */
298 if (freeze)
299 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
300 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
301
302 /* disable the port after freeze is done */
303 if (!freeze)
304 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
305}
306
307void ksz8_port_init_cnt(struct ksz_device *dev, int port)
308{
309 struct ksz_port_mib *mib = &dev->ports[port].mib;
310 u64 *dropped;
311
312 if (!ksz_is_ksz88x3(dev)) {
313 /* flush all enabled port MIB counters */
314 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
315 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
316 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
317 }
318
319 mib->cnt_ptr = 0;
320
321 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
322 while (mib->cnt_ptr < dev->info->reg_mib_cnt) {
323 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
324 &mib->counters[mib->cnt_ptr]);
325 ++mib->cnt_ptr;
326 }
327
328 /* last one in storage */
329 dropped = &mib->counters[dev->info->mib_cnt];
330
331 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
332 while (mib->cnt_ptr < dev->info->mib_cnt) {
333 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
334 dropped, &mib->counters[mib->cnt_ptr]);
335 ++mib->cnt_ptr;
336 }
337}
338
339static void ksz8_r_table(struct ksz_device *dev, int table, u16 addr, u64 *data)
340{
341 const u16 *regs;
342 u16 ctrl_addr;
343
344 regs = dev->info->regs;
345
346 ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
347
348 mutex_lock(&dev->alu_mutex);
349 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
350 ksz_read64(dev, regs[REG_IND_DATA_HI], data);
351 mutex_unlock(&dev->alu_mutex);
352}
353
354static void ksz8_w_table(struct ksz_device *dev, int table, u16 addr, u64 data)
355{
356 const u16 *regs;
357 u16 ctrl_addr;
358
359 regs = dev->info->regs;
360
361 ctrl_addr = IND_ACC_TABLE(table) | addr;
362
363 mutex_lock(&dev->alu_mutex);
364 ksz_write64(dev, regs[REG_IND_DATA_HI], data);
365 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
366 mutex_unlock(&dev->alu_mutex);
367}
368
369static int ksz8_valid_dyn_entry(struct ksz_device *dev, u8 *data)
370{
371 int timeout = 100;
372 const u32 *masks;
373 const u16 *regs;
374
375 masks = dev->info->masks;
376 regs = dev->info->regs;
377
378 do {
379 ksz_read8(dev, regs[REG_IND_DATA_CHECK], data);
380 timeout--;
381 } while ((*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) && timeout);
382
383 /* Entry is not ready for accessing. */
384 if (*data & masks[DYNAMIC_MAC_TABLE_NOT_READY]) {
385 return -EAGAIN;
386 /* Entry is ready for accessing. */
387 } else {
388 ksz_read8(dev, regs[REG_IND_DATA_8], data);
389
390 /* There is no valid entry in the table. */
391 if (*data & masks[DYNAMIC_MAC_TABLE_MAC_EMPTY])
392 return -ENXIO;
393 }
394 return 0;
395}
396
397int ksz8_r_dyn_mac_table(struct ksz_device *dev, u16 addr, u8 *mac_addr,
398 u8 *fid, u8 *src_port, u8 *timestamp, u16 *entries)
399{
400 u32 data_hi, data_lo;
401 const u8 *shifts;
402 const u32 *masks;
403 const u16 *regs;
404 u16 ctrl_addr;
405 u8 data;
406 int rc;
407
408 shifts = dev->info->shifts;
409 masks = dev->info->masks;
410 regs = dev->info->regs;
411
412 ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
413
414 mutex_lock(&dev->alu_mutex);
415 ksz_write16(dev, regs[REG_IND_CTRL_0], ctrl_addr);
416
417 rc = ksz8_valid_dyn_entry(dev, &data);
418 if (rc == -EAGAIN) {
419 if (addr == 0)
420 *entries = 0;
421 } else if (rc == -ENXIO) {
422 *entries = 0;
423 /* At least one valid entry in the table. */
424 } else {
425 u64 buf = 0;
426 int cnt;
427
428 ksz_read64(dev, regs[REG_IND_DATA_HI], &buf);
429 data_hi = (u32)(buf >> 32);
430 data_lo = (u32)buf;
431
432 /* Check out how many valid entry in the table. */
433 cnt = data & masks[DYNAMIC_MAC_TABLE_ENTRIES_H];
434 cnt <<= shifts[DYNAMIC_MAC_ENTRIES_H];
435 cnt |= (data_hi & masks[DYNAMIC_MAC_TABLE_ENTRIES]) >>
436 shifts[DYNAMIC_MAC_ENTRIES];
437 *entries = cnt + 1;
438
439 *fid = (data_hi & masks[DYNAMIC_MAC_TABLE_FID]) >>
440 shifts[DYNAMIC_MAC_FID];
441 *src_port = (data_hi & masks[DYNAMIC_MAC_TABLE_SRC_PORT]) >>
442 shifts[DYNAMIC_MAC_SRC_PORT];
443 *timestamp = (data_hi & masks[DYNAMIC_MAC_TABLE_TIMESTAMP]) >>
444 shifts[DYNAMIC_MAC_TIMESTAMP];
445
446 mac_addr[5] = (u8)data_lo;
447 mac_addr[4] = (u8)(data_lo >> 8);
448 mac_addr[3] = (u8)(data_lo >> 16);
449 mac_addr[2] = (u8)(data_lo >> 24);
450
451 mac_addr[1] = (u8)data_hi;
452 mac_addr[0] = (u8)(data_hi >> 8);
453 rc = 0;
454 }
455 mutex_unlock(&dev->alu_mutex);
456
457 return rc;
458}
459
460int ksz8_r_sta_mac_table(struct ksz_device *dev, u16 addr,
461 struct alu_struct *alu)
462{
463 u32 data_hi, data_lo;
464 const u8 *shifts;
465 const u32 *masks;
466 u64 data;
467
468 shifts = dev->info->shifts;
469 masks = dev->info->masks;
470
471 ksz8_r_table(dev, TABLE_STATIC_MAC, addr, &data);
472 data_hi = data >> 32;
473 data_lo = (u32)data;
474 if (data_hi & (masks[STATIC_MAC_TABLE_VALID] |
475 masks[STATIC_MAC_TABLE_OVERRIDE])) {
476 alu->mac[5] = (u8)data_lo;
477 alu->mac[4] = (u8)(data_lo >> 8);
478 alu->mac[3] = (u8)(data_lo >> 16);
479 alu->mac[2] = (u8)(data_lo >> 24);
480 alu->mac[1] = (u8)data_hi;
481 alu->mac[0] = (u8)(data_hi >> 8);
482 alu->port_forward =
483 (data_hi & masks[STATIC_MAC_TABLE_FWD_PORTS]) >>
484 shifts[STATIC_MAC_FWD_PORTS];
485 alu->is_override =
486 (data_hi & masks[STATIC_MAC_TABLE_OVERRIDE]) ? 1 : 0;
487 data_hi >>= 1;
488 alu->is_static = true;
489 alu->is_use_fid =
490 (data_hi & masks[STATIC_MAC_TABLE_USE_FID]) ? 1 : 0;
491 alu->fid = (data_hi & masks[STATIC_MAC_TABLE_FID]) >>
492 shifts[STATIC_MAC_FID];
493 return 0;
494 }
495 return -ENXIO;
496}
497
498void ksz8_w_sta_mac_table(struct ksz_device *dev, u16 addr,
499 struct alu_struct *alu)
500{
501 u32 data_hi, data_lo;
502 const u8 *shifts;
503 const u32 *masks;
504 u64 data;
505
506 shifts = dev->info->shifts;
507 masks = dev->info->masks;
508
509 data_lo = ((u32)alu->mac[2] << 24) |
510 ((u32)alu->mac[3] << 16) |
511 ((u32)alu->mac[4] << 8) | alu->mac[5];
512 data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
513 data_hi |= (u32)alu->port_forward << shifts[STATIC_MAC_FWD_PORTS];
514
515 if (alu->is_override)
516 data_hi |= masks[STATIC_MAC_TABLE_OVERRIDE];
517 if (alu->is_use_fid) {
518 data_hi |= masks[STATIC_MAC_TABLE_USE_FID];
519 data_hi |= (u32)alu->fid << shifts[STATIC_MAC_FID];
520 }
521 if (alu->is_static)
522 data_hi |= masks[STATIC_MAC_TABLE_VALID];
523 else
524 data_hi &= ~masks[STATIC_MAC_TABLE_OVERRIDE];
525
526 data = (u64)data_hi << 32 | data_lo;
527 ksz8_w_table(dev, TABLE_STATIC_MAC, addr, data);
528}
529
530static void ksz8_from_vlan(struct ksz_device *dev, u32 vlan, u8 *fid,
531 u8 *member, u8 *valid)
532{
533 const u8 *shifts;
534 const u32 *masks;
535
536 shifts = dev->info->shifts;
537 masks = dev->info->masks;
538
539 *fid = vlan & masks[VLAN_TABLE_FID];
540 *member = (vlan & masks[VLAN_TABLE_MEMBERSHIP]) >>
541 shifts[VLAN_TABLE_MEMBERSHIP_S];
542 *valid = !!(vlan & masks[VLAN_TABLE_VALID]);
543}
544
545static void ksz8_to_vlan(struct ksz_device *dev, u8 fid, u8 member, u8 valid,
546 u16 *vlan)
547{
548 const u8 *shifts;
549 const u32 *masks;
550
551 shifts = dev->info->shifts;
552 masks = dev->info->masks;
553
554 *vlan = fid;
555 *vlan |= (u16)member << shifts[VLAN_TABLE_MEMBERSHIP_S];
556 if (valid)
557 *vlan |= masks[VLAN_TABLE_VALID];
558}
559
560static void ksz8_r_vlan_entries(struct ksz_device *dev, u16 addr)
561{
562 const u8 *shifts;
563 u64 data;
564 int i;
565
566 shifts = dev->info->shifts;
567
568 ksz8_r_table(dev, TABLE_VLAN, addr, &data);
569 addr *= 4;
570 for (i = 0; i < 4; i++) {
571 dev->vlan_cache[addr + i].table[0] = (u16)data;
572 data >>= shifts[VLAN_TABLE];
573 }
574}
575
576static void ksz8_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
577{
578 int index;
579 u16 *data;
580 u16 addr;
581 u64 buf;
582
583 data = (u16 *)&buf;
584 addr = vid / 4;
585 index = vid & 3;
586 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
587 *vlan = data[index];
588}
589
590static void ksz8_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
591{
592 int index;
593 u16 *data;
594 u16 addr;
595 u64 buf;
596
597 data = (u16 *)&buf;
598 addr = vid / 4;
599 index = vid & 3;
600 ksz8_r_table(dev, TABLE_VLAN, addr, &buf);
601 data[index] = vlan;
602 dev->vlan_cache[vid].table[0] = vlan;
603 ksz8_w_table(dev, TABLE_VLAN, addr, buf);
604}
605
606int ksz8_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
607{
608 u8 restart, speed, ctrl, link;
609 int processed = true;
610 const u16 *regs;
611 u8 val1, val2;
612 u16 data = 0;
613 u8 p = phy;
614 int ret;
615
616 regs = dev->info->regs;
617
618 switch (reg) {
619 case MII_BMCR:
620 ret = ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
621 if (ret)
622 return ret;
623
624 ret = ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
625 if (ret)
626 return ret;
627
628 ret = ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
629 if (ret)
630 return ret;
631
632 if (restart & PORT_PHY_LOOPBACK)
633 data |= BMCR_LOOPBACK;
634 if (ctrl & PORT_FORCE_100_MBIT)
635 data |= BMCR_SPEED100;
636 if (ksz_is_ksz88x3(dev)) {
637 if ((ctrl & PORT_AUTO_NEG_ENABLE))
638 data |= BMCR_ANENABLE;
639 } else {
640 if (!(ctrl & PORT_AUTO_NEG_DISABLE))
641 data |= BMCR_ANENABLE;
642 }
643 if (restart & PORT_POWER_DOWN)
644 data |= BMCR_PDOWN;
645 if (restart & PORT_AUTO_NEG_RESTART)
646 data |= BMCR_ANRESTART;
647 if (ctrl & PORT_FORCE_FULL_DUPLEX)
648 data |= BMCR_FULLDPLX;
649 if (speed & PORT_HP_MDIX)
650 data |= KSZ886X_BMCR_HP_MDIX;
651 if (restart & PORT_FORCE_MDIX)
652 data |= KSZ886X_BMCR_FORCE_MDI;
653 if (restart & PORT_AUTO_MDIX_DISABLE)
654 data |= KSZ886X_BMCR_DISABLE_AUTO_MDIX;
655 if (restart & PORT_TX_DISABLE)
656 data |= KSZ886X_BMCR_DISABLE_TRANSMIT;
657 if (restart & PORT_LED_OFF)
658 data |= KSZ886X_BMCR_DISABLE_LED;
659 break;
660 case MII_BMSR:
661 ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
662 if (ret)
663 return ret;
664
665 data = BMSR_100FULL |
666 BMSR_100HALF |
667 BMSR_10FULL |
668 BMSR_10HALF |
669 BMSR_ANEGCAPABLE;
670 if (link & PORT_AUTO_NEG_COMPLETE)
671 data |= BMSR_ANEGCOMPLETE;
672 if (link & PORT_STAT_LINK_GOOD)
673 data |= BMSR_LSTATUS;
674 break;
675 case MII_PHYSID1:
676 data = KSZ8795_ID_HI;
677 break;
678 case MII_PHYSID2:
679 if (ksz_is_ksz88x3(dev))
680 data = KSZ8863_ID_LO;
681 else
682 data = KSZ8795_ID_LO;
683 break;
684 case MII_ADVERTISE:
685 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
686 if (ret)
687 return ret;
688
689 data = ADVERTISE_CSMA;
690 if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
691 data |= ADVERTISE_PAUSE_CAP;
692 if (ctrl & PORT_AUTO_NEG_100BTX_FD)
693 data |= ADVERTISE_100FULL;
694 if (ctrl & PORT_AUTO_NEG_100BTX)
695 data |= ADVERTISE_100HALF;
696 if (ctrl & PORT_AUTO_NEG_10BT_FD)
697 data |= ADVERTISE_10FULL;
698 if (ctrl & PORT_AUTO_NEG_10BT)
699 data |= ADVERTISE_10HALF;
700 break;
701 case MII_LPA:
702 ret = ksz_pread8(dev, p, regs[P_REMOTE_STATUS], &link);
703 if (ret)
704 return ret;
705
706 data = LPA_SLCT;
707 if (link & PORT_REMOTE_SYM_PAUSE)
708 data |= LPA_PAUSE_CAP;
709 if (link & PORT_REMOTE_100BTX_FD)
710 data |= LPA_100FULL;
711 if (link & PORT_REMOTE_100BTX)
712 data |= LPA_100HALF;
713 if (link & PORT_REMOTE_10BT_FD)
714 data |= LPA_10FULL;
715 if (link & PORT_REMOTE_10BT)
716 data |= LPA_10HALF;
717 if (data & ~LPA_SLCT)
718 data |= LPA_LPACK;
719 break;
720 case PHY_REG_LINK_MD:
721 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_CTRL, &val1);
722 if (ret)
723 return ret;
724
725 ret = ksz_pread8(dev, p, REG_PORT_LINK_MD_RESULT, &val2);
726 if (ret)
727 return ret;
728
729 if (val1 & PORT_START_CABLE_DIAG)
730 data |= PHY_START_CABLE_DIAG;
731
732 if (val1 & PORT_CABLE_10M_SHORT)
733 data |= PHY_CABLE_10M_SHORT;
734
735 data |= FIELD_PREP(PHY_CABLE_DIAG_RESULT_M,
736 FIELD_GET(PORT_CABLE_DIAG_RESULT_M, val1));
737
738 data |= FIELD_PREP(PHY_CABLE_FAULT_COUNTER_M,
739 (FIELD_GET(PORT_CABLE_FAULT_COUNTER_H, val1) << 8) |
740 FIELD_GET(PORT_CABLE_FAULT_COUNTER_L, val2));
741 break;
742 case PHY_REG_PHY_CTRL:
743 ret = ksz_pread8(dev, p, regs[P_LINK_STATUS], &link);
744 if (ret)
745 return ret;
746
747 if (link & PORT_MDIX_STATUS)
748 data |= KSZ886X_CTRL_MDIX_STAT;
749 break;
750 default:
751 processed = false;
752 break;
753 }
754 if (processed)
755 *val = data;
756
757 return 0;
758}
759
760int ksz8_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
761{
762 u8 restart, speed, ctrl, data;
763 const u16 *regs;
764 u8 p = phy;
765 int ret;
766
767 regs = dev->info->regs;
768
769 switch (reg) {
770 case MII_BMCR:
771
772 /* Do not support PHY reset function. */
773 if (val & BMCR_RESET)
774 break;
775 ret = ksz_pread8(dev, p, regs[P_SPEED_STATUS], &speed);
776 if (ret)
777 return ret;
778
779 data = speed;
780 if (val & KSZ886X_BMCR_HP_MDIX)
781 data |= PORT_HP_MDIX;
782 else
783 data &= ~PORT_HP_MDIX;
784
785 if (data != speed) {
786 ret = ksz_pwrite8(dev, p, regs[P_SPEED_STATUS], data);
787 if (ret)
788 return ret;
789 }
790
791 ret = ksz_pread8(dev, p, regs[P_FORCE_CTRL], &ctrl);
792 if (ret)
793 return ret;
794
795 data = ctrl;
796 if (ksz_is_ksz88x3(dev)) {
797 if ((val & BMCR_ANENABLE))
798 data |= PORT_AUTO_NEG_ENABLE;
799 else
800 data &= ~PORT_AUTO_NEG_ENABLE;
801 } else {
802 if (!(val & BMCR_ANENABLE))
803 data |= PORT_AUTO_NEG_DISABLE;
804 else
805 data &= ~PORT_AUTO_NEG_DISABLE;
806
807 /* Fiber port does not support auto-negotiation. */
808 if (dev->ports[p].fiber)
809 data |= PORT_AUTO_NEG_DISABLE;
810 }
811
812 if (val & BMCR_SPEED100)
813 data |= PORT_FORCE_100_MBIT;
814 else
815 data &= ~PORT_FORCE_100_MBIT;
816 if (val & BMCR_FULLDPLX)
817 data |= PORT_FORCE_FULL_DUPLEX;
818 else
819 data &= ~PORT_FORCE_FULL_DUPLEX;
820
821 if (data != ctrl) {
822 ret = ksz_pwrite8(dev, p, regs[P_FORCE_CTRL], data);
823 if (ret)
824 return ret;
825 }
826
827 ret = ksz_pread8(dev, p, regs[P_NEG_RESTART_CTRL], &restart);
828 if (ret)
829 return ret;
830
831 data = restart;
832 if (val & KSZ886X_BMCR_DISABLE_LED)
833 data |= PORT_LED_OFF;
834 else
835 data &= ~PORT_LED_OFF;
836 if (val & KSZ886X_BMCR_DISABLE_TRANSMIT)
837 data |= PORT_TX_DISABLE;
838 else
839 data &= ~PORT_TX_DISABLE;
840 if (val & BMCR_ANRESTART)
841 data |= PORT_AUTO_NEG_RESTART;
842 else
843 data &= ~(PORT_AUTO_NEG_RESTART);
844 if (val & BMCR_PDOWN)
845 data |= PORT_POWER_DOWN;
846 else
847 data &= ~PORT_POWER_DOWN;
848 if (val & KSZ886X_BMCR_DISABLE_AUTO_MDIX)
849 data |= PORT_AUTO_MDIX_DISABLE;
850 else
851 data &= ~PORT_AUTO_MDIX_DISABLE;
852 if (val & KSZ886X_BMCR_FORCE_MDI)
853 data |= PORT_FORCE_MDIX;
854 else
855 data &= ~PORT_FORCE_MDIX;
856 if (val & BMCR_LOOPBACK)
857 data |= PORT_PHY_LOOPBACK;
858 else
859 data &= ~PORT_PHY_LOOPBACK;
860
861 if (data != restart) {
862 ret = ksz_pwrite8(dev, p, regs[P_NEG_RESTART_CTRL],
863 data);
864 if (ret)
865 return ret;
866 }
867 break;
868 case MII_ADVERTISE:
869 ret = ksz_pread8(dev, p, regs[P_LOCAL_CTRL], &ctrl);
870 if (ret)
871 return ret;
872
873 data = ctrl;
874 data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
875 PORT_AUTO_NEG_100BTX_FD |
876 PORT_AUTO_NEG_100BTX |
877 PORT_AUTO_NEG_10BT_FD |
878 PORT_AUTO_NEG_10BT);
879 if (val & ADVERTISE_PAUSE_CAP)
880 data |= PORT_AUTO_NEG_SYM_PAUSE;
881 if (val & ADVERTISE_100FULL)
882 data |= PORT_AUTO_NEG_100BTX_FD;
883 if (val & ADVERTISE_100HALF)
884 data |= PORT_AUTO_NEG_100BTX;
885 if (val & ADVERTISE_10FULL)
886 data |= PORT_AUTO_NEG_10BT_FD;
887 if (val & ADVERTISE_10HALF)
888 data |= PORT_AUTO_NEG_10BT;
889
890 if (data != ctrl) {
891 ret = ksz_pwrite8(dev, p, regs[P_LOCAL_CTRL], data);
892 if (ret)
893 return ret;
894 }
895 break;
896 case PHY_REG_LINK_MD:
897 if (val & PHY_START_CABLE_DIAG)
898 ksz_port_cfg(dev, p, REG_PORT_LINK_MD_CTRL, PORT_START_CABLE_DIAG, true);
899 break;
900 default:
901 break;
902 }
903
904 return 0;
905}
906
907void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)
908{
909 u8 data;
910
911 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
912 data &= ~PORT_VLAN_MEMBERSHIP;
913 data |= (member & dev->port_mask);
914 ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
915}
916
917void ksz8_flush_dyn_mac_table(struct ksz_device *dev, int port)
918{
919 u8 learn[DSA_MAX_PORTS];
920 int first, index, cnt;
921 struct ksz_port *p;
922 const u16 *regs;
923
924 regs = dev->info->regs;
925
926 if ((uint)port < dev->info->port_cnt) {
927 first = port;
928 cnt = port + 1;
929 } else {
930 /* Flush all ports. */
931 first = 0;
932 cnt = dev->info->port_cnt;
933 }
934 for (index = first; index < cnt; index++) {
935 p = &dev->ports[index];
936 if (!p->on)
937 continue;
938 ksz_pread8(dev, index, regs[P_STP_CTRL], &learn[index]);
939 if (!(learn[index] & PORT_LEARN_DISABLE))
940 ksz_pwrite8(dev, index, regs[P_STP_CTRL],
941 learn[index] | PORT_LEARN_DISABLE);
942 }
943 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
944 for (index = first; index < cnt; index++) {
945 p = &dev->ports[index];
946 if (!p->on)
947 continue;
948 if (!(learn[index] & PORT_LEARN_DISABLE))
949 ksz_pwrite8(dev, index, regs[P_STP_CTRL], learn[index]);
950 }
951}
952
953int ksz8_fdb_dump(struct ksz_device *dev, int port,
954 dsa_fdb_dump_cb_t *cb, void *data)
955{
956 int ret = 0;
957 u16 i = 0;
958 u16 entries = 0;
959 u8 timestamp = 0;
960 u8 fid;
961 u8 member;
962 struct alu_struct alu;
963
964 do {
965 alu.is_static = false;
966 ret = ksz8_r_dyn_mac_table(dev, i, alu.mac, &fid, &member,
967 ×tamp, &entries);
968 if (!ret && (member & BIT(port))) {
969 ret = cb(alu.mac, alu.fid, alu.is_static, data);
970 if (ret)
971 break;
972 }
973 i++;
974 } while (i < entries);
975 if (i >= entries)
976 ret = 0;
977
978 return ret;
979}
980
981int ksz8_mdb_add(struct ksz_device *dev, int port,
982 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
983{
984 struct alu_struct alu;
985 int index;
986 int empty = 0;
987
988 alu.port_forward = 0;
989 for (index = 0; index < dev->info->num_statics; index++) {
990 if (!ksz8_r_sta_mac_table(dev, index, &alu)) {
991 /* Found one already in static MAC table. */
992 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
993 alu.fid == mdb->vid)
994 break;
995 /* Remember the first empty entry. */
996 } else if (!empty) {
997 empty = index + 1;
998 }
999 }
1000
1001 /* no available entry */
1002 if (index == dev->info->num_statics && !empty)
1003 return -ENOSPC;
1004
1005 /* add entry */
1006 if (index == dev->info->num_statics) {
1007 index = empty - 1;
1008 memset(&alu, 0, sizeof(alu));
1009 memcpy(alu.mac, mdb->addr, ETH_ALEN);
1010 alu.is_static = true;
1011 }
1012 alu.port_forward |= BIT(port);
1013 if (mdb->vid) {
1014 alu.is_use_fid = true;
1015
1016 /* Need a way to map VID to FID. */
1017 alu.fid = mdb->vid;
1018 }
1019 ksz8_w_sta_mac_table(dev, index, &alu);
1020
1021 return 0;
1022}
1023
1024int ksz8_mdb_del(struct ksz_device *dev, int port,
1025 const struct switchdev_obj_port_mdb *mdb, struct dsa_db db)
1026{
1027 struct alu_struct alu;
1028 int index;
1029
1030 for (index = 0; index < dev->info->num_statics; index++) {
1031 if (!ksz8_r_sta_mac_table(dev, index, &alu)) {
1032 /* Found one already in static MAC table. */
1033 if (!memcmp(alu.mac, mdb->addr, ETH_ALEN) &&
1034 alu.fid == mdb->vid)
1035 break;
1036 }
1037 }
1038
1039 /* no available entry */
1040 if (index == dev->info->num_statics)
1041 goto exit;
1042
1043 /* clear port */
1044 alu.port_forward &= ~BIT(port);
1045 if (!alu.port_forward)
1046 alu.is_static = false;
1047 ksz8_w_sta_mac_table(dev, index, &alu);
1048
1049exit:
1050 return 0;
1051}
1052
1053int ksz8_port_vlan_filtering(struct ksz_device *dev, int port, bool flag,
1054 struct netlink_ext_ack *extack)
1055{
1056 if (ksz_is_ksz88x3(dev))
1057 return -ENOTSUPP;
1058
1059 /* Discard packets with VID not enabled on the switch */
1060 ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
1061
1062 /* Discard packets with VID not enabled on the ingress port */
1063 for (port = 0; port < dev->phy_port_cnt; ++port)
1064 ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
1065 flag);
1066
1067 return 0;
1068}
1069
1070static void ksz8_port_enable_pvid(struct ksz_device *dev, int port, bool state)
1071{
1072 if (ksz_is_ksz88x3(dev)) {
1073 ksz_cfg(dev, REG_SW_INSERT_SRC_PVID,
1074 0x03 << (4 - 2 * port), state);
1075 } else {
1076 ksz_pwrite8(dev, port, REG_PORT_CTRL_12, state ? 0x0f : 0x00);
1077 }
1078}
1079
1080int ksz8_port_vlan_add(struct ksz_device *dev, int port,
1081 const struct switchdev_obj_port_vlan *vlan,
1082 struct netlink_ext_ack *extack)
1083{
1084 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1085 struct ksz_port *p = &dev->ports[port];
1086 u16 data, new_pvid = 0;
1087 u8 fid, member, valid;
1088
1089 if (ksz_is_ksz88x3(dev))
1090 return -ENOTSUPP;
1091
1092 /* If a VLAN is added with untagged flag different from the
1093 * port's Remove Tag flag, we need to change the latter.
1094 * Ignore VID 0, which is always untagged.
1095 * Ignore CPU port, which will always be tagged.
1096 */
1097 if (untagged != p->remove_tag && vlan->vid != 0 &&
1098 port != dev->cpu_port) {
1099 unsigned int vid;
1100
1101 /* Reject attempts to add a VLAN that requires the
1102 * Remove Tag flag to be changed, unless there are no
1103 * other VLANs currently configured.
1104 */
1105 for (vid = 1; vid < dev->info->num_vlans; ++vid) {
1106 /* Skip the VID we are going to add or reconfigure */
1107 if (vid == vlan->vid)
1108 continue;
1109
1110 ksz8_from_vlan(dev, dev->vlan_cache[vid].table[0],
1111 &fid, &member, &valid);
1112 if (valid && (member & BIT(port)))
1113 return -EINVAL;
1114 }
1115
1116 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
1117 p->remove_tag = untagged;
1118 }
1119
1120 ksz8_r_vlan_table(dev, vlan->vid, &data);
1121 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1122
1123 /* First time to setup the VLAN entry. */
1124 if (!valid) {
1125 /* Need to find a way to map VID to FID. */
1126 fid = 1;
1127 valid = 1;
1128 }
1129 member |= BIT(port);
1130
1131 ksz8_to_vlan(dev, fid, member, valid, &data);
1132 ksz8_w_vlan_table(dev, vlan->vid, data);
1133
1134 /* change PVID */
1135 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
1136 new_pvid = vlan->vid;
1137
1138 if (new_pvid) {
1139 u16 vid;
1140
1141 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
1142 vid &= ~VLAN_VID_MASK;
1143 vid |= new_pvid;
1144 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
1145
1146 ksz8_port_enable_pvid(dev, port, true);
1147 }
1148
1149 return 0;
1150}
1151
1152int ksz8_port_vlan_del(struct ksz_device *dev, int port,
1153 const struct switchdev_obj_port_vlan *vlan)
1154{
1155 u16 data, pvid;
1156 u8 fid, member, valid;
1157
1158 if (ksz_is_ksz88x3(dev))
1159 return -ENOTSUPP;
1160
1161 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
1162 pvid = pvid & 0xFFF;
1163
1164 ksz8_r_vlan_table(dev, vlan->vid, &data);
1165 ksz8_from_vlan(dev, data, &fid, &member, &valid);
1166
1167 member &= ~BIT(port);
1168
1169 /* Invalidate the entry if no more member. */
1170 if (!member) {
1171 fid = 0;
1172 valid = 0;
1173 }
1174
1175 ksz8_to_vlan(dev, fid, member, valid, &data);
1176 ksz8_w_vlan_table(dev, vlan->vid, data);
1177
1178 if (pvid == vlan->vid)
1179 ksz8_port_enable_pvid(dev, port, false);
1180
1181 return 0;
1182}
1183
1184int ksz8_port_mirror_add(struct ksz_device *dev, int port,
1185 struct dsa_mall_mirror_tc_entry *mirror,
1186 bool ingress, struct netlink_ext_ack *extack)
1187{
1188 if (ingress) {
1189 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
1190 dev->mirror_rx |= BIT(port);
1191 } else {
1192 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
1193 dev->mirror_tx |= BIT(port);
1194 }
1195
1196 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
1197
1198 /* configure mirror port */
1199 if (dev->mirror_rx || dev->mirror_tx)
1200 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1201 PORT_MIRROR_SNIFFER, true);
1202
1203 return 0;
1204}
1205
1206void ksz8_port_mirror_del(struct ksz_device *dev, int port,
1207 struct dsa_mall_mirror_tc_entry *mirror)
1208{
1209 u8 data;
1210
1211 if (mirror->ingress) {
1212 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
1213 dev->mirror_rx &= ~BIT(port);
1214 } else {
1215 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
1216 dev->mirror_tx &= ~BIT(port);
1217 }
1218
1219 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
1220
1221 if (!dev->mirror_rx && !dev->mirror_tx)
1222 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
1223 PORT_MIRROR_SNIFFER, false);
1224}
1225
1226static void ksz8795_cpu_interface_select(struct ksz_device *dev, int port)
1227{
1228 struct ksz_port *p = &dev->ports[port];
1229
1230 if (!p->interface && dev->compat_interface) {
1231 dev_warn(dev->dev,
1232 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1233 "Please update your device tree.\n",
1234 port);
1235 p->interface = dev->compat_interface;
1236 }
1237}
1238
1239void ksz8_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1240{
1241 struct dsa_switch *ds = dev->ds;
1242 const u32 *masks;
1243 u8 member;
1244
1245 masks = dev->info->masks;
1246
1247 /* enable broadcast storm limit */
1248 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1249
1250 if (!ksz_is_ksz88x3(dev))
1251 ksz8795_set_prio_queue(dev, port, 4);
1252
1253 /* disable DiffServ priority */
1254 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
1255
1256 /* replace priority */
1257 ksz_port_cfg(dev, port, P_802_1P_CTRL,
1258 masks[PORT_802_1P_REMAPPING], false);
1259
1260 /* enable 802.1p priority */
1261 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
1262
1263 if (cpu_port) {
1264 if (!ksz_is_ksz88x3(dev))
1265 ksz8795_cpu_interface_select(dev, port);
1266
1267 member = dsa_user_ports(ds);
1268 } else {
1269 member = BIT(dsa_upstream_port(ds, port));
1270 }
1271
1272 ksz8_cfg_port_member(dev, port, member);
1273}
1274
1275void ksz8_config_cpu_port(struct dsa_switch *ds)
1276{
1277 struct ksz_device *dev = ds->priv;
1278 struct ksz_port *p;
1279 const u32 *masks;
1280 const u16 *regs;
1281 u8 remote;
1282 int i;
1283
1284 masks = dev->info->masks;
1285 regs = dev->info->regs;
1286
1287 ksz_cfg(dev, regs[S_TAIL_TAG_CTRL], masks[SW_TAIL_TAG_ENABLE], true);
1288
1289 p = &dev->ports[dev->cpu_port];
1290 p->on = 1;
1291
1292 ksz8_port_setup(dev, dev->cpu_port, true);
1293
1294 for (i = 0; i < dev->phy_port_cnt; i++) {
1295 p = &dev->ports[i];
1296
1297 ksz_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1298
1299 /* Last port may be disabled. */
1300 if (i == dev->phy_port_cnt)
1301 break;
1302 p->on = 1;
1303 }
1304 for (i = 0; i < dev->phy_port_cnt; i++) {
1305 p = &dev->ports[i];
1306 if (!p->on)
1307 continue;
1308 if (!ksz_is_ksz88x3(dev)) {
1309 ksz_pread8(dev, i, regs[P_REMOTE_STATUS], &remote);
1310 if (remote & KSZ8_PORT_FIBER_MODE)
1311 p->fiber = 1;
1312 }
1313 if (p->fiber)
1314 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1315 PORT_FORCE_FLOW_CTRL, true);
1316 else
1317 ksz_port_cfg(dev, i, regs[P_STP_CTRL],
1318 PORT_FORCE_FLOW_CTRL, false);
1319 }
1320}
1321
1322static int ksz8_handle_global_errata(struct dsa_switch *ds)
1323{
1324 struct ksz_device *dev = ds->priv;
1325 int ret = 0;
1326
1327 /* KSZ87xx Errata DS80000687C.
1328 * Module 2: Link drops with some EEE link partners.
1329 * An issue with the EEE next page exchange between the
1330 * KSZ879x/KSZ877x/KSZ876x and some EEE link partners may result in
1331 * the link dropping.
1332 */
1333 if (dev->info->ksz87xx_eee_link_erratum)
1334 ret = ksz8_ind_write8(dev, TABLE_EEE, REG_IND_EEE_GLOB2_HI, 0);
1335
1336 return ret;
1337}
1338
1339int ksz8_enable_stp_addr(struct ksz_device *dev)
1340{
1341 struct alu_struct alu;
1342
1343 /* Setup STP address for STP operation. */
1344 memset(&alu, 0, sizeof(alu));
1345 ether_addr_copy(alu.mac, eth_stp_addr);
1346 alu.is_static = true;
1347 alu.is_override = true;
1348 alu.port_forward = dev->info->cpu_ports;
1349
1350 ksz8_w_sta_mac_table(dev, 0, &alu);
1351
1352 return 0;
1353}
1354
1355int ksz8_setup(struct dsa_switch *ds)
1356{
1357 struct ksz_device *dev = ds->priv;
1358 int i;
1359
1360 ds->mtu_enforcement_ingress = true;
1361
1362 /* We rely on software untagging on the CPU port, so that we
1363 * can support both tagged and untagged VLANs
1364 */
1365 ds->untag_bridge_pvid = true;
1366
1367 /* VLAN filtering is partly controlled by the global VLAN
1368 * Enable flag
1369 */
1370 ds->vlan_filtering_is_global = true;
1371
1372 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1373
1374 /* Enable automatic fast aging when link changed detected. */
1375 ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1376
1377 /* Enable aggressive back off algorithm in half duplex mode. */
1378 regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1,
1379 SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1380
1381 /*
1382 * Make sure unicast VLAN boundary is set as default and
1383 * enable no excessive collision drop.
1384 */
1385 regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2,
1386 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1387 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1388
1389 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1390
1391 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1392
1393 if (!ksz_is_ksz88x3(dev))
1394 ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
1395
1396 for (i = 0; i < (dev->info->num_vlans / 4); i++)
1397 ksz8_r_vlan_entries(dev, i);
1398
1399 return ksz8_handle_global_errata(ds);
1400}
1401
1402void ksz8_get_caps(struct ksz_device *dev, int port,
1403 struct phylink_config *config)
1404{
1405 config->mac_capabilities = MAC_10 | MAC_100;
1406
1407 /* Silicon Errata Sheet (DS80000830A):
1408 * "Port 1 does not respond to received flow control PAUSE frames"
1409 * So, disable Pause support on "Port 1" (port == 0) for all ksz88x3
1410 * switches.
1411 */
1412 if (!ksz_is_ksz88x3(dev) || port)
1413 config->mac_capabilities |= MAC_SYM_PAUSE;
1414
1415 /* Asym pause is not supported on KSZ8863 and KSZ8873 */
1416 if (!ksz_is_ksz88x3(dev))
1417 config->mac_capabilities |= MAC_ASYM_PAUSE;
1418}
1419
1420u32 ksz8_get_port_addr(int port, int offset)
1421{
1422 return PORT_CTRL_ADDR(port, offset);
1423}
1424
1425int ksz8_switch_init(struct ksz_device *dev)
1426{
1427 dev->cpu_port = fls(dev->info->cpu_ports) - 1;
1428 dev->phy_port_cnt = dev->info->port_cnt - 1;
1429 dev->port_mask = (BIT(dev->phy_port_cnt) - 1) | dev->info->cpu_ports;
1430
1431 return 0;
1432}
1433
1434void ksz8_switch_exit(struct ksz_device *dev)
1435{
1436 ksz8_reset_switch(dev);
1437}
1438
1439MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1440MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1441MODULE_LICENSE("GPL");
1// SPDX-License-Identifier: GPL-2.0
2/*
3 * Microchip KSZ8795 switch driver
4 *
5 * Copyright (C) 2017 Microchip Technology Inc.
6 * Tristram Ha <Tristram.Ha@microchip.com>
7 */
8
9#include <linux/delay.h>
10#include <linux/export.h>
11#include <linux/gpio.h>
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/platform_data/microchip-ksz.h>
15#include <linux/phy.h>
16#include <linux/etherdevice.h>
17#include <linux/if_bridge.h>
18#include <net/dsa.h>
19#include <net/switchdev.h>
20
21#include "ksz_common.h"
22#include "ksz8795_reg.h"
23
24static const struct {
25 char string[ETH_GSTRING_LEN];
26} mib_names[TOTAL_SWITCH_COUNTER_NUM] = {
27 { "rx_hi" },
28 { "rx_undersize" },
29 { "rx_fragments" },
30 { "rx_oversize" },
31 { "rx_jabbers" },
32 { "rx_symbol_err" },
33 { "rx_crc_err" },
34 { "rx_align_err" },
35 { "rx_mac_ctrl" },
36 { "rx_pause" },
37 { "rx_bcast" },
38 { "rx_mcast" },
39 { "rx_ucast" },
40 { "rx_64_or_less" },
41 { "rx_65_127" },
42 { "rx_128_255" },
43 { "rx_256_511" },
44 { "rx_512_1023" },
45 { "rx_1024_1522" },
46 { "rx_1523_2000" },
47 { "rx_2001" },
48 { "tx_hi" },
49 { "tx_late_col" },
50 { "tx_pause" },
51 { "tx_bcast" },
52 { "tx_mcast" },
53 { "tx_ucast" },
54 { "tx_deferred" },
55 { "tx_total_col" },
56 { "tx_exc_col" },
57 { "tx_single_col" },
58 { "tx_mult_col" },
59 { "rx_total" },
60 { "tx_total" },
61 { "rx_discards" },
62 { "tx_discards" },
63};
64
65static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
66{
67 regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
68}
69
70static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
71 bool set)
72{
73 regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
74 bits, set ? bits : 0);
75}
76
77static int ksz8795_reset_switch(struct ksz_device *dev)
78{
79 /* reset switch */
80 ksz_write8(dev, REG_POWER_MANAGEMENT_1,
81 SW_SOFTWARE_POWER_DOWN << SW_POWER_MANAGEMENT_MODE_S);
82 ksz_write8(dev, REG_POWER_MANAGEMENT_1, 0);
83
84 return 0;
85}
86
87static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue)
88{
89 u8 hi, lo;
90
91 /* Number of queues can only be 1, 2, or 4. */
92 switch (queue) {
93 case 4:
94 case 3:
95 queue = PORT_QUEUE_SPLIT_4;
96 break;
97 case 2:
98 queue = PORT_QUEUE_SPLIT_2;
99 break;
100 default:
101 queue = PORT_QUEUE_SPLIT_1;
102 }
103 ksz_pread8(dev, port, REG_PORT_CTRL_0, &lo);
104 ksz_pread8(dev, port, P_DROP_TAG_CTRL, &hi);
105 lo &= ~PORT_QUEUE_SPLIT_L;
106 if (queue & PORT_QUEUE_SPLIT_2)
107 lo |= PORT_QUEUE_SPLIT_L;
108 hi &= ~PORT_QUEUE_SPLIT_H;
109 if (queue & PORT_QUEUE_SPLIT_4)
110 hi |= PORT_QUEUE_SPLIT_H;
111 ksz_pwrite8(dev, port, REG_PORT_CTRL_0, lo);
112 ksz_pwrite8(dev, port, P_DROP_TAG_CTRL, hi);
113
114 /* Default is port based for egress rate limit. */
115 if (queue != PORT_QUEUE_SPLIT_1)
116 ksz_cfg(dev, REG_SW_CTRL_19, SW_OUT_RATE_LIMIT_QUEUE_BASED,
117 true);
118}
119
120static void ksz8795_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
121 u64 *cnt)
122{
123 u16 ctrl_addr;
124 u32 data;
125 u8 check;
126 int loop;
127
128 ctrl_addr = addr + SWITCH_COUNTER_NUM * port;
129 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
130
131 mutex_lock(&dev->alu_mutex);
132 ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
133
134 /* It is almost guaranteed to always read the valid bit because of
135 * slow SPI speed.
136 */
137 for (loop = 2; loop > 0; loop--) {
138 ksz_read8(dev, REG_IND_MIB_CHECK, &check);
139
140 if (check & MIB_COUNTER_VALID) {
141 ksz_read32(dev, REG_IND_DATA_LO, &data);
142 if (check & MIB_COUNTER_OVERFLOW)
143 *cnt += MIB_COUNTER_VALUE + 1;
144 *cnt += data & MIB_COUNTER_VALUE;
145 break;
146 }
147 }
148 mutex_unlock(&dev->alu_mutex);
149}
150
151static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
152 u64 *dropped, u64 *cnt)
153{
154 u16 ctrl_addr;
155 u32 data;
156 u8 check;
157 int loop;
158
159 addr -= SWITCH_COUNTER_NUM;
160 ctrl_addr = (KS_MIB_TOTAL_RX_1 - KS_MIB_TOTAL_RX_0) * port;
161 ctrl_addr += addr + KS_MIB_TOTAL_RX_0;
162 ctrl_addr |= IND_ACC_TABLE(TABLE_MIB | TABLE_READ);
163
164 mutex_lock(&dev->alu_mutex);
165 ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
166
167 /* It is almost guaranteed to always read the valid bit because of
168 * slow SPI speed.
169 */
170 for (loop = 2; loop > 0; loop--) {
171 ksz_read8(dev, REG_IND_MIB_CHECK, &check);
172
173 if (check & MIB_COUNTER_VALID) {
174 ksz_read32(dev, REG_IND_DATA_LO, &data);
175 if (addr < 2) {
176 u64 total;
177
178 total = check & MIB_TOTAL_BYTES_H;
179 total <<= 32;
180 *cnt += total;
181 *cnt += data;
182 if (check & MIB_COUNTER_OVERFLOW) {
183 total = MIB_TOTAL_BYTES_H + 1;
184 total <<= 32;
185 *cnt += total;
186 }
187 } else {
188 if (check & MIB_COUNTER_OVERFLOW)
189 *cnt += MIB_PACKET_DROPPED + 1;
190 *cnt += data & MIB_PACKET_DROPPED;
191 }
192 break;
193 }
194 }
195 mutex_unlock(&dev->alu_mutex);
196}
197
198static void ksz8795_freeze_mib(struct ksz_device *dev, int port, bool freeze)
199{
200 /* enable the port for flush/freeze function */
201 if (freeze)
202 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
203 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FREEZE, freeze);
204
205 /* disable the port after freeze is done */
206 if (!freeze)
207 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
208}
209
210static void ksz8795_port_init_cnt(struct ksz_device *dev, int port)
211{
212 struct ksz_port_mib *mib = &dev->ports[port].mib;
213
214 /* flush all enabled port MIB counters */
215 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), true);
216 ksz_cfg(dev, REG_SW_CTRL_6, SW_MIB_COUNTER_FLUSH, true);
217 ksz_cfg(dev, REG_SW_CTRL_6, BIT(port), false);
218
219 mib->cnt_ptr = 0;
220
221 /* Some ports may not have MIB counters before SWITCH_COUNTER_NUM. */
222 while (mib->cnt_ptr < dev->reg_mib_cnt) {
223 dev->dev_ops->r_mib_cnt(dev, port, mib->cnt_ptr,
224 &mib->counters[mib->cnt_ptr]);
225 ++mib->cnt_ptr;
226 }
227
228 /* Some ports may not have MIB counters after SWITCH_COUNTER_NUM. */
229 while (mib->cnt_ptr < dev->mib_cnt) {
230 dev->dev_ops->r_mib_pkt(dev, port, mib->cnt_ptr,
231 NULL, &mib->counters[mib->cnt_ptr]);
232 ++mib->cnt_ptr;
233 }
234 mib->cnt_ptr = 0;
235 memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
236}
237
238static void ksz8795_r_table(struct ksz_device *dev, int table, u16 addr,
239 u64 *data)
240{
241 u16 ctrl_addr;
242
243 ctrl_addr = IND_ACC_TABLE(table | TABLE_READ) | addr;
244
245 mutex_lock(&dev->alu_mutex);
246 ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
247 ksz_read64(dev, REG_IND_DATA_HI, data);
248 mutex_unlock(&dev->alu_mutex);
249}
250
251static void ksz8795_w_table(struct ksz_device *dev, int table, u16 addr,
252 u64 data)
253{
254 u16 ctrl_addr;
255
256 ctrl_addr = IND_ACC_TABLE(table) | addr;
257
258 mutex_lock(&dev->alu_mutex);
259 ksz_write64(dev, REG_IND_DATA_HI, data);
260 ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
261 mutex_unlock(&dev->alu_mutex);
262}
263
264static int ksz8795_valid_dyn_entry(struct ksz_device *dev, u8 *data)
265{
266 int timeout = 100;
267
268 do {
269 ksz_read8(dev, REG_IND_DATA_CHECK, data);
270 timeout--;
271 } while ((*data & DYNAMIC_MAC_TABLE_NOT_READY) && timeout);
272
273 /* Entry is not ready for accessing. */
274 if (*data & DYNAMIC_MAC_TABLE_NOT_READY) {
275 return -EAGAIN;
276 /* Entry is ready for accessing. */
277 } else {
278 ksz_read8(dev, REG_IND_DATA_8, data);
279
280 /* There is no valid entry in the table. */
281 if (*data & DYNAMIC_MAC_TABLE_MAC_EMPTY)
282 return -ENXIO;
283 }
284 return 0;
285}
286
287static int ksz8795_r_dyn_mac_table(struct ksz_device *dev, u16 addr,
288 u8 *mac_addr, u8 *fid, u8 *src_port,
289 u8 *timestamp, u16 *entries)
290{
291 u32 data_hi, data_lo;
292 u16 ctrl_addr;
293 u8 data;
294 int rc;
295
296 ctrl_addr = IND_ACC_TABLE(TABLE_DYNAMIC_MAC | TABLE_READ) | addr;
297
298 mutex_lock(&dev->alu_mutex);
299 ksz_write16(dev, REG_IND_CTRL_0, ctrl_addr);
300
301 rc = ksz8795_valid_dyn_entry(dev, &data);
302 if (rc == -EAGAIN) {
303 if (addr == 0)
304 *entries = 0;
305 } else if (rc == -ENXIO) {
306 *entries = 0;
307 /* At least one valid entry in the table. */
308 } else {
309 u64 buf = 0;
310 int cnt;
311
312 ksz_read64(dev, REG_IND_DATA_HI, &buf);
313 data_hi = (u32)(buf >> 32);
314 data_lo = (u32)buf;
315
316 /* Check out how many valid entry in the table. */
317 cnt = data & DYNAMIC_MAC_TABLE_ENTRIES_H;
318 cnt <<= DYNAMIC_MAC_ENTRIES_H_S;
319 cnt |= (data_hi & DYNAMIC_MAC_TABLE_ENTRIES) >>
320 DYNAMIC_MAC_ENTRIES_S;
321 *entries = cnt + 1;
322
323 *fid = (data_hi & DYNAMIC_MAC_TABLE_FID) >>
324 DYNAMIC_MAC_FID_S;
325 *src_port = (data_hi & DYNAMIC_MAC_TABLE_SRC_PORT) >>
326 DYNAMIC_MAC_SRC_PORT_S;
327 *timestamp = (data_hi & DYNAMIC_MAC_TABLE_TIMESTAMP) >>
328 DYNAMIC_MAC_TIMESTAMP_S;
329
330 mac_addr[5] = (u8)data_lo;
331 mac_addr[4] = (u8)(data_lo >> 8);
332 mac_addr[3] = (u8)(data_lo >> 16);
333 mac_addr[2] = (u8)(data_lo >> 24);
334
335 mac_addr[1] = (u8)data_hi;
336 mac_addr[0] = (u8)(data_hi >> 8);
337 rc = 0;
338 }
339 mutex_unlock(&dev->alu_mutex);
340
341 return rc;
342}
343
344static int ksz8795_r_sta_mac_table(struct ksz_device *dev, u16 addr,
345 struct alu_struct *alu)
346{
347 u32 data_hi, data_lo;
348 u64 data;
349
350 ksz8795_r_table(dev, TABLE_STATIC_MAC, addr, &data);
351 data_hi = data >> 32;
352 data_lo = (u32)data;
353 if (data_hi & (STATIC_MAC_TABLE_VALID | STATIC_MAC_TABLE_OVERRIDE)) {
354 alu->mac[5] = (u8)data_lo;
355 alu->mac[4] = (u8)(data_lo >> 8);
356 alu->mac[3] = (u8)(data_lo >> 16);
357 alu->mac[2] = (u8)(data_lo >> 24);
358 alu->mac[1] = (u8)data_hi;
359 alu->mac[0] = (u8)(data_hi >> 8);
360 alu->port_forward = (data_hi & STATIC_MAC_TABLE_FWD_PORTS) >>
361 STATIC_MAC_FWD_PORTS_S;
362 alu->is_override =
363 (data_hi & STATIC_MAC_TABLE_OVERRIDE) ? 1 : 0;
364 data_hi >>= 1;
365 alu->is_use_fid = (data_hi & STATIC_MAC_TABLE_USE_FID) ? 1 : 0;
366 alu->fid = (data_hi & STATIC_MAC_TABLE_FID) >>
367 STATIC_MAC_FID_S;
368 return 0;
369 }
370 return -ENXIO;
371}
372
373static void ksz8795_w_sta_mac_table(struct ksz_device *dev, u16 addr,
374 struct alu_struct *alu)
375{
376 u32 data_hi, data_lo;
377 u64 data;
378
379 data_lo = ((u32)alu->mac[2] << 24) |
380 ((u32)alu->mac[3] << 16) |
381 ((u32)alu->mac[4] << 8) | alu->mac[5];
382 data_hi = ((u32)alu->mac[0] << 8) | alu->mac[1];
383 data_hi |= (u32)alu->port_forward << STATIC_MAC_FWD_PORTS_S;
384
385 if (alu->is_override)
386 data_hi |= STATIC_MAC_TABLE_OVERRIDE;
387 if (alu->is_use_fid) {
388 data_hi |= STATIC_MAC_TABLE_USE_FID;
389 data_hi |= (u32)alu->fid << STATIC_MAC_FID_S;
390 }
391 if (alu->is_static)
392 data_hi |= STATIC_MAC_TABLE_VALID;
393 else
394 data_hi &= ~STATIC_MAC_TABLE_OVERRIDE;
395
396 data = (u64)data_hi << 32 | data_lo;
397 ksz8795_w_table(dev, TABLE_STATIC_MAC, addr, data);
398}
399
400static void ksz8795_from_vlan(u16 vlan, u8 *fid, u8 *member, u8 *valid)
401{
402 *fid = vlan & VLAN_TABLE_FID;
403 *member = (vlan & VLAN_TABLE_MEMBERSHIP) >> VLAN_TABLE_MEMBERSHIP_S;
404 *valid = !!(vlan & VLAN_TABLE_VALID);
405}
406
407static void ksz8795_to_vlan(u8 fid, u8 member, u8 valid, u16 *vlan)
408{
409 *vlan = fid;
410 *vlan |= (u16)member << VLAN_TABLE_MEMBERSHIP_S;
411 if (valid)
412 *vlan |= VLAN_TABLE_VALID;
413}
414
415static void ksz8795_r_vlan_entries(struct ksz_device *dev, u16 addr)
416{
417 u64 data;
418 int i;
419
420 ksz8795_r_table(dev, TABLE_VLAN, addr, &data);
421 addr *= 4;
422 for (i = 0; i < 4; i++) {
423 dev->vlan_cache[addr + i].table[0] = (u16)data;
424 data >>= VLAN_TABLE_S;
425 }
426}
427
428static void ksz8795_r_vlan_table(struct ksz_device *dev, u16 vid, u16 *vlan)
429{
430 int index;
431 u16 *data;
432 u16 addr;
433 u64 buf;
434
435 data = (u16 *)&buf;
436 addr = vid / 4;
437 index = vid & 3;
438 ksz8795_r_table(dev, TABLE_VLAN, addr, &buf);
439 *vlan = data[index];
440}
441
442static void ksz8795_w_vlan_table(struct ksz_device *dev, u16 vid, u16 vlan)
443{
444 int index;
445 u16 *data;
446 u16 addr;
447 u64 buf;
448
449 data = (u16 *)&buf;
450 addr = vid / 4;
451 index = vid & 3;
452 ksz8795_r_table(dev, TABLE_VLAN, addr, &buf);
453 data[index] = vlan;
454 dev->vlan_cache[vid].table[0] = vlan;
455 ksz8795_w_table(dev, TABLE_VLAN, addr, buf);
456}
457
458static void ksz8795_r_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 *val)
459{
460 u8 restart, speed, ctrl, link;
461 int processed = true;
462 u16 data = 0;
463 u8 p = phy;
464
465 switch (reg) {
466 case PHY_REG_CTRL:
467 ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart);
468 ksz_pread8(dev, p, P_SPEED_STATUS, &speed);
469 ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl);
470 if (restart & PORT_PHY_LOOPBACK)
471 data |= PHY_LOOPBACK;
472 if (ctrl & PORT_FORCE_100_MBIT)
473 data |= PHY_SPEED_100MBIT;
474 if (!(ctrl & PORT_AUTO_NEG_DISABLE))
475 data |= PHY_AUTO_NEG_ENABLE;
476 if (restart & PORT_POWER_DOWN)
477 data |= PHY_POWER_DOWN;
478 if (restart & PORT_AUTO_NEG_RESTART)
479 data |= PHY_AUTO_NEG_RESTART;
480 if (ctrl & PORT_FORCE_FULL_DUPLEX)
481 data |= PHY_FULL_DUPLEX;
482 if (speed & PORT_HP_MDIX)
483 data |= PHY_HP_MDIX;
484 if (restart & PORT_FORCE_MDIX)
485 data |= PHY_FORCE_MDIX;
486 if (restart & PORT_AUTO_MDIX_DISABLE)
487 data |= PHY_AUTO_MDIX_DISABLE;
488 if (restart & PORT_TX_DISABLE)
489 data |= PHY_TRANSMIT_DISABLE;
490 if (restart & PORT_LED_OFF)
491 data |= PHY_LED_DISABLE;
492 break;
493 case PHY_REG_STATUS:
494 ksz_pread8(dev, p, P_LINK_STATUS, &link);
495 data = PHY_100BTX_FD_CAPABLE |
496 PHY_100BTX_CAPABLE |
497 PHY_10BT_FD_CAPABLE |
498 PHY_10BT_CAPABLE |
499 PHY_AUTO_NEG_CAPABLE;
500 if (link & PORT_AUTO_NEG_COMPLETE)
501 data |= PHY_AUTO_NEG_ACKNOWLEDGE;
502 if (link & PORT_STAT_LINK_GOOD)
503 data |= PHY_LINK_STATUS;
504 break;
505 case PHY_REG_ID_1:
506 data = KSZ8795_ID_HI;
507 break;
508 case PHY_REG_ID_2:
509 data = KSZ8795_ID_LO;
510 break;
511 case PHY_REG_AUTO_NEGOTIATION:
512 ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl);
513 data = PHY_AUTO_NEG_802_3;
514 if (ctrl & PORT_AUTO_NEG_SYM_PAUSE)
515 data |= PHY_AUTO_NEG_SYM_PAUSE;
516 if (ctrl & PORT_AUTO_NEG_100BTX_FD)
517 data |= PHY_AUTO_NEG_100BTX_FD;
518 if (ctrl & PORT_AUTO_NEG_100BTX)
519 data |= PHY_AUTO_NEG_100BTX;
520 if (ctrl & PORT_AUTO_NEG_10BT_FD)
521 data |= PHY_AUTO_NEG_10BT_FD;
522 if (ctrl & PORT_AUTO_NEG_10BT)
523 data |= PHY_AUTO_NEG_10BT;
524 break;
525 case PHY_REG_REMOTE_CAPABILITY:
526 ksz_pread8(dev, p, P_REMOTE_STATUS, &link);
527 data = PHY_AUTO_NEG_802_3;
528 if (link & PORT_REMOTE_SYM_PAUSE)
529 data |= PHY_AUTO_NEG_SYM_PAUSE;
530 if (link & PORT_REMOTE_100BTX_FD)
531 data |= PHY_AUTO_NEG_100BTX_FD;
532 if (link & PORT_REMOTE_100BTX)
533 data |= PHY_AUTO_NEG_100BTX;
534 if (link & PORT_REMOTE_10BT_FD)
535 data |= PHY_AUTO_NEG_10BT_FD;
536 if (link & PORT_REMOTE_10BT)
537 data |= PHY_AUTO_NEG_10BT;
538 if (data & ~PHY_AUTO_NEG_802_3)
539 data |= PHY_REMOTE_ACKNOWLEDGE_NOT;
540 break;
541 default:
542 processed = false;
543 break;
544 }
545 if (processed)
546 *val = data;
547}
548
549static void ksz8795_w_phy(struct ksz_device *dev, u16 phy, u16 reg, u16 val)
550{
551 u8 p = phy;
552 u8 restart, speed, ctrl, data;
553
554 switch (reg) {
555 case PHY_REG_CTRL:
556
557 /* Do not support PHY reset function. */
558 if (val & PHY_RESET)
559 break;
560 ksz_pread8(dev, p, P_SPEED_STATUS, &speed);
561 data = speed;
562 if (val & PHY_HP_MDIX)
563 data |= PORT_HP_MDIX;
564 else
565 data &= ~PORT_HP_MDIX;
566 if (data != speed)
567 ksz_pwrite8(dev, p, P_SPEED_STATUS, data);
568 ksz_pread8(dev, p, P_FORCE_CTRL, &ctrl);
569 data = ctrl;
570 if (!(val & PHY_AUTO_NEG_ENABLE))
571 data |= PORT_AUTO_NEG_DISABLE;
572 else
573 data &= ~PORT_AUTO_NEG_DISABLE;
574
575 /* Fiber port does not support auto-negotiation. */
576 if (dev->ports[p].fiber)
577 data |= PORT_AUTO_NEG_DISABLE;
578 if (val & PHY_SPEED_100MBIT)
579 data |= PORT_FORCE_100_MBIT;
580 else
581 data &= ~PORT_FORCE_100_MBIT;
582 if (val & PHY_FULL_DUPLEX)
583 data |= PORT_FORCE_FULL_DUPLEX;
584 else
585 data &= ~PORT_FORCE_FULL_DUPLEX;
586 if (data != ctrl)
587 ksz_pwrite8(dev, p, P_FORCE_CTRL, data);
588 ksz_pread8(dev, p, P_NEG_RESTART_CTRL, &restart);
589 data = restart;
590 if (val & PHY_LED_DISABLE)
591 data |= PORT_LED_OFF;
592 else
593 data &= ~PORT_LED_OFF;
594 if (val & PHY_TRANSMIT_DISABLE)
595 data |= PORT_TX_DISABLE;
596 else
597 data &= ~PORT_TX_DISABLE;
598 if (val & PHY_AUTO_NEG_RESTART)
599 data |= PORT_AUTO_NEG_RESTART;
600 else
601 data &= ~(PORT_AUTO_NEG_RESTART);
602 if (val & PHY_POWER_DOWN)
603 data |= PORT_POWER_DOWN;
604 else
605 data &= ~PORT_POWER_DOWN;
606 if (val & PHY_AUTO_MDIX_DISABLE)
607 data |= PORT_AUTO_MDIX_DISABLE;
608 else
609 data &= ~PORT_AUTO_MDIX_DISABLE;
610 if (val & PHY_FORCE_MDIX)
611 data |= PORT_FORCE_MDIX;
612 else
613 data &= ~PORT_FORCE_MDIX;
614 if (val & PHY_LOOPBACK)
615 data |= PORT_PHY_LOOPBACK;
616 else
617 data &= ~PORT_PHY_LOOPBACK;
618 if (data != restart)
619 ksz_pwrite8(dev, p, P_NEG_RESTART_CTRL, data);
620 break;
621 case PHY_REG_AUTO_NEGOTIATION:
622 ksz_pread8(dev, p, P_LOCAL_CTRL, &ctrl);
623 data = ctrl;
624 data &= ~(PORT_AUTO_NEG_SYM_PAUSE |
625 PORT_AUTO_NEG_100BTX_FD |
626 PORT_AUTO_NEG_100BTX |
627 PORT_AUTO_NEG_10BT_FD |
628 PORT_AUTO_NEG_10BT);
629 if (val & PHY_AUTO_NEG_SYM_PAUSE)
630 data |= PORT_AUTO_NEG_SYM_PAUSE;
631 if (val & PHY_AUTO_NEG_100BTX_FD)
632 data |= PORT_AUTO_NEG_100BTX_FD;
633 if (val & PHY_AUTO_NEG_100BTX)
634 data |= PORT_AUTO_NEG_100BTX;
635 if (val & PHY_AUTO_NEG_10BT_FD)
636 data |= PORT_AUTO_NEG_10BT_FD;
637 if (val & PHY_AUTO_NEG_10BT)
638 data |= PORT_AUTO_NEG_10BT;
639 if (data != ctrl)
640 ksz_pwrite8(dev, p, P_LOCAL_CTRL, data);
641 break;
642 default:
643 break;
644 }
645}
646
647static enum dsa_tag_protocol ksz8795_get_tag_protocol(struct dsa_switch *ds,
648 int port,
649 enum dsa_tag_protocol mp)
650{
651 return DSA_TAG_PROTO_KSZ8795;
652}
653
654static void ksz8795_get_strings(struct dsa_switch *ds, int port,
655 u32 stringset, uint8_t *buf)
656{
657 int i;
658
659 for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) {
660 memcpy(buf + i * ETH_GSTRING_LEN, mib_names[i].string,
661 ETH_GSTRING_LEN);
662 }
663}
664
665static void ksz8795_cfg_port_member(struct ksz_device *dev, int port,
666 u8 member)
667{
668 u8 data;
669
670 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
671 data &= ~PORT_VLAN_MEMBERSHIP;
672 data |= (member & dev->port_mask);
673 ksz_pwrite8(dev, port, P_MIRROR_CTRL, data);
674 dev->ports[port].member = member;
675}
676
677static void ksz8795_port_stp_state_set(struct dsa_switch *ds, int port,
678 u8 state)
679{
680 struct ksz_device *dev = ds->priv;
681 int forward = dev->member;
682 struct ksz_port *p;
683 int member = -1;
684 u8 data;
685
686 p = &dev->ports[port];
687
688 ksz_pread8(dev, port, P_STP_CTRL, &data);
689 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
690
691 switch (state) {
692 case BR_STATE_DISABLED:
693 data |= PORT_LEARN_DISABLE;
694 if (port < SWITCH_PORT_NUM)
695 member = 0;
696 break;
697 case BR_STATE_LISTENING:
698 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
699 if (port < SWITCH_PORT_NUM &&
700 p->stp_state == BR_STATE_DISABLED)
701 member = dev->host_mask | p->vid_member;
702 break;
703 case BR_STATE_LEARNING:
704 data |= PORT_RX_ENABLE;
705 break;
706 case BR_STATE_FORWARDING:
707 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
708
709 /* This function is also used internally. */
710 if (port == dev->cpu_port)
711 break;
712
713 /* Port is a member of a bridge. */
714 if (dev->br_member & BIT(port)) {
715 dev->member |= BIT(port);
716 member = dev->member;
717 } else {
718 member = dev->host_mask | p->vid_member;
719 }
720 break;
721 case BR_STATE_BLOCKING:
722 data |= PORT_LEARN_DISABLE;
723 if (port < SWITCH_PORT_NUM &&
724 p->stp_state == BR_STATE_DISABLED)
725 member = dev->host_mask | p->vid_member;
726 break;
727 default:
728 dev_err(ds->dev, "invalid STP state: %d\n", state);
729 return;
730 }
731
732 ksz_pwrite8(dev, port, P_STP_CTRL, data);
733 p->stp_state = state;
734 /* Port membership may share register with STP state. */
735 if (member >= 0 && member != p->member)
736 ksz8795_cfg_port_member(dev, port, (u8)member);
737
738 /* Check if forwarding needs to be updated. */
739 if (state != BR_STATE_FORWARDING) {
740 if (dev->br_member & BIT(port))
741 dev->member &= ~BIT(port);
742 }
743
744 /* When topology has changed the function ksz_update_port_member
745 * should be called to modify port forwarding behavior.
746 */
747 if (forward != dev->member)
748 ksz_update_port_member(dev, port);
749}
750
751static void ksz8795_flush_dyn_mac_table(struct ksz_device *dev, int port)
752{
753 u8 learn[TOTAL_PORT_NUM];
754 int first, index, cnt;
755 struct ksz_port *p;
756
757 if ((uint)port < TOTAL_PORT_NUM) {
758 first = port;
759 cnt = port + 1;
760 } else {
761 /* Flush all ports. */
762 first = 0;
763 cnt = dev->mib_port_cnt;
764 }
765 for (index = first; index < cnt; index++) {
766 p = &dev->ports[index];
767 if (!p->on)
768 continue;
769 ksz_pread8(dev, index, P_STP_CTRL, &learn[index]);
770 if (!(learn[index] & PORT_LEARN_DISABLE))
771 ksz_pwrite8(dev, index, P_STP_CTRL,
772 learn[index] | PORT_LEARN_DISABLE);
773 }
774 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
775 for (index = first; index < cnt; index++) {
776 p = &dev->ports[index];
777 if (!p->on)
778 continue;
779 if (!(learn[index] & PORT_LEARN_DISABLE))
780 ksz_pwrite8(dev, index, P_STP_CTRL, learn[index]);
781 }
782}
783
784static int ksz8795_port_vlan_filtering(struct dsa_switch *ds, int port,
785 bool flag)
786{
787 struct ksz_device *dev = ds->priv;
788
789 ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
790
791 return 0;
792}
793
794static void ksz8795_port_vlan_add(struct dsa_switch *ds, int port,
795 const struct switchdev_obj_port_vlan *vlan)
796{
797 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
798 struct ksz_device *dev = ds->priv;
799 u16 data, vid, new_pvid = 0;
800 u8 fid, member, valid;
801
802 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
803
804 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
805 ksz8795_r_vlan_table(dev, vid, &data);
806 ksz8795_from_vlan(data, &fid, &member, &valid);
807
808 /* First time to setup the VLAN entry. */
809 if (!valid) {
810 /* Need to find a way to map VID to FID. */
811 fid = 1;
812 valid = 1;
813 }
814 member |= BIT(port);
815
816 ksz8795_to_vlan(fid, member, valid, &data);
817 ksz8795_w_vlan_table(dev, vid, data);
818
819 /* change PVID */
820 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
821 new_pvid = vid;
822 }
823
824 if (new_pvid) {
825 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
826 vid &= 0xfff;
827 vid |= new_pvid;
828 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
829 }
830}
831
832static int ksz8795_port_vlan_del(struct dsa_switch *ds, int port,
833 const struct switchdev_obj_port_vlan *vlan)
834{
835 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
836 struct ksz_device *dev = ds->priv;
837 u16 data, vid, pvid, new_pvid = 0;
838 u8 fid, member, valid;
839
840 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
841 pvid = pvid & 0xFFF;
842
843 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
844
845 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
846 ksz8795_r_vlan_table(dev, vid, &data);
847 ksz8795_from_vlan(data, &fid, &member, &valid);
848
849 member &= ~BIT(port);
850
851 /* Invalidate the entry if no more member. */
852 if (!member) {
853 fid = 0;
854 valid = 0;
855 }
856
857 if (pvid == vid)
858 new_pvid = 1;
859
860 ksz8795_to_vlan(fid, member, valid, &data);
861 ksz8795_w_vlan_table(dev, vid, data);
862 }
863
864 if (new_pvid != pvid)
865 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, pvid);
866
867 return 0;
868}
869
870static int ksz8795_port_mirror_add(struct dsa_switch *ds, int port,
871 struct dsa_mall_mirror_tc_entry *mirror,
872 bool ingress)
873{
874 struct ksz_device *dev = ds->priv;
875
876 if (ingress) {
877 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
878 dev->mirror_rx |= BIT(port);
879 } else {
880 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
881 dev->mirror_tx |= BIT(port);
882 }
883
884 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
885
886 /* configure mirror port */
887 if (dev->mirror_rx || dev->mirror_tx)
888 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
889 PORT_MIRROR_SNIFFER, true);
890
891 return 0;
892}
893
894static void ksz8795_port_mirror_del(struct dsa_switch *ds, int port,
895 struct dsa_mall_mirror_tc_entry *mirror)
896{
897 struct ksz_device *dev = ds->priv;
898 u8 data;
899
900 if (mirror->ingress) {
901 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
902 dev->mirror_rx &= ~BIT(port);
903 } else {
904 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
905 dev->mirror_tx &= ~BIT(port);
906 }
907
908 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
909
910 if (!dev->mirror_rx && !dev->mirror_tx)
911 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
912 PORT_MIRROR_SNIFFER, false);
913}
914
915static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port)
916{
917 struct ksz_port *p = &dev->ports[port];
918 u8 data8, member;
919
920 /* enable broadcast storm limit */
921 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
922
923 ksz8795_set_prio_queue(dev, port, 4);
924
925 /* disable DiffServ priority */
926 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
927
928 /* replace priority */
929 ksz_port_cfg(dev, port, P_802_1P_CTRL, PORT_802_1P_REMAPPING, false);
930
931 /* enable 802.1p priority */
932 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
933
934 if (cpu_port) {
935 if (!p->interface && dev->compat_interface) {
936 dev_warn(dev->dev,
937 "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
938 "Please update your device tree.\n",
939 port);
940 p->interface = dev->compat_interface;
941 }
942
943 /* Configure MII interface for proper network communication. */
944 ksz_read8(dev, REG_PORT_5_CTRL_6, &data8);
945 data8 &= ~PORT_INTERFACE_TYPE;
946 data8 &= ~PORT_GMII_1GPS_MODE;
947 switch (p->interface) {
948 case PHY_INTERFACE_MODE_MII:
949 p->phydev.speed = SPEED_100;
950 break;
951 case PHY_INTERFACE_MODE_RMII:
952 data8 |= PORT_INTERFACE_RMII;
953 p->phydev.speed = SPEED_100;
954 break;
955 case PHY_INTERFACE_MODE_GMII:
956 data8 |= PORT_GMII_1GPS_MODE;
957 data8 |= PORT_INTERFACE_GMII;
958 p->phydev.speed = SPEED_1000;
959 break;
960 default:
961 data8 &= ~PORT_RGMII_ID_IN_ENABLE;
962 data8 &= ~PORT_RGMII_ID_OUT_ENABLE;
963 if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
964 p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
965 data8 |= PORT_RGMII_ID_IN_ENABLE;
966 if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
967 p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
968 data8 |= PORT_RGMII_ID_OUT_ENABLE;
969 data8 |= PORT_GMII_1GPS_MODE;
970 data8 |= PORT_INTERFACE_RGMII;
971 p->phydev.speed = SPEED_1000;
972 break;
973 }
974 ksz_write8(dev, REG_PORT_5_CTRL_6, data8);
975 p->phydev.duplex = 1;
976
977 member = dev->port_mask;
978 } else {
979 member = dev->host_mask | p->vid_member;
980 }
981 ksz8795_cfg_port_member(dev, port, member);
982}
983
984static void ksz8795_config_cpu_port(struct dsa_switch *ds)
985{
986 struct ksz_device *dev = ds->priv;
987 struct ksz_port *p;
988 u8 remote;
989 int i;
990
991 ds->num_ports = dev->port_cnt + 1;
992
993 /* Switch marks the maximum frame with extra byte as oversize. */
994 ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true);
995 ksz_cfg(dev, S_TAIL_TAG_CTRL, SW_TAIL_TAG_ENABLE, true);
996
997 p = &dev->ports[dev->cpu_port];
998 p->vid_member = dev->port_mask;
999 p->on = 1;
1000
1001 ksz8795_port_setup(dev, dev->cpu_port, true);
1002 dev->member = dev->host_mask;
1003
1004 for (i = 0; i < SWITCH_PORT_NUM; i++) {
1005 p = &dev->ports[i];
1006
1007 /* Initialize to non-zero so that ksz_cfg_port_member() will
1008 * be called.
1009 */
1010 p->vid_member = BIT(i);
1011 p->member = dev->port_mask;
1012 ksz8795_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1013
1014 /* Last port may be disabled. */
1015 if (i == dev->port_cnt)
1016 break;
1017 p->on = 1;
1018 p->phy = 1;
1019 }
1020 for (i = 0; i < dev->phy_port_cnt; i++) {
1021 p = &dev->ports[i];
1022 if (!p->on)
1023 continue;
1024 ksz_pread8(dev, i, P_REMOTE_STATUS, &remote);
1025 if (remote & PORT_FIBER_MODE)
1026 p->fiber = 1;
1027 if (p->fiber)
1028 ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1029 true);
1030 else
1031 ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1032 false);
1033 }
1034}
1035
1036static int ksz8795_setup(struct dsa_switch *ds)
1037{
1038 struct ksz_device *dev = ds->priv;
1039 struct alu_struct alu;
1040 int i, ret = 0;
1041
1042 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1043 dev->num_vlans, GFP_KERNEL);
1044 if (!dev->vlan_cache)
1045 return -ENOMEM;
1046
1047 ret = ksz8795_reset_switch(dev);
1048 if (ret) {
1049 dev_err(ds->dev, "failed to reset switch\n");
1050 return ret;
1051 }
1052
1053 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1054
1055 /* Enable automatic fast aging when link changed detected. */
1056 ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1057
1058 /* Enable aggressive back off algorithm in half duplex mode. */
1059 regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1,
1060 SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1061
1062 /*
1063 * Make sure unicast VLAN boundary is set as default and
1064 * enable no excessive collision drop.
1065 */
1066 regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2,
1067 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1068 UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1069
1070 ksz8795_config_cpu_port(ds);
1071
1072 ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true);
1073
1074 ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1075
1076 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1077
1078 /* set broadcast storm protection 10% rate */
1079 regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL,
1080 BROADCAST_STORM_RATE,
1081 (BROADCAST_STORM_VALUE *
1082 BROADCAST_STORM_PROT_RATE) / 100);
1083
1084 for (i = 0; i < VLAN_TABLE_ENTRIES; i++)
1085 ksz8795_r_vlan_entries(dev, i);
1086
1087 /* Setup STP address for STP operation. */
1088 memset(&alu, 0, sizeof(alu));
1089 ether_addr_copy(alu.mac, eth_stp_addr);
1090 alu.is_static = true;
1091 alu.is_override = true;
1092 alu.port_forward = dev->host_mask;
1093
1094 ksz8795_w_sta_mac_table(dev, 0, &alu);
1095
1096 ksz_init_mib_timer(dev);
1097
1098 return 0;
1099}
1100
1101static const struct dsa_switch_ops ksz8795_switch_ops = {
1102 .get_tag_protocol = ksz8795_get_tag_protocol,
1103 .setup = ksz8795_setup,
1104 .phy_read = ksz_phy_read16,
1105 .phy_write = ksz_phy_write16,
1106 .phylink_mac_link_down = ksz_mac_link_down,
1107 .port_enable = ksz_enable_port,
1108 .get_strings = ksz8795_get_strings,
1109 .get_ethtool_stats = ksz_get_ethtool_stats,
1110 .get_sset_count = ksz_sset_count,
1111 .port_bridge_join = ksz_port_bridge_join,
1112 .port_bridge_leave = ksz_port_bridge_leave,
1113 .port_stp_state_set = ksz8795_port_stp_state_set,
1114 .port_fast_age = ksz_port_fast_age,
1115 .port_vlan_filtering = ksz8795_port_vlan_filtering,
1116 .port_vlan_prepare = ksz_port_vlan_prepare,
1117 .port_vlan_add = ksz8795_port_vlan_add,
1118 .port_vlan_del = ksz8795_port_vlan_del,
1119 .port_fdb_dump = ksz_port_fdb_dump,
1120 .port_mdb_prepare = ksz_port_mdb_prepare,
1121 .port_mdb_add = ksz_port_mdb_add,
1122 .port_mdb_del = ksz_port_mdb_del,
1123 .port_mirror_add = ksz8795_port_mirror_add,
1124 .port_mirror_del = ksz8795_port_mirror_del,
1125};
1126
1127static u32 ksz8795_get_port_addr(int port, int offset)
1128{
1129 return PORT_CTRL_ADDR(port, offset);
1130}
1131
1132static int ksz8795_switch_detect(struct ksz_device *dev)
1133{
1134 u8 id1, id2;
1135 u16 id16;
1136 int ret;
1137
1138 /* read chip id */
1139 ret = ksz_read16(dev, REG_CHIP_ID0, &id16);
1140 if (ret)
1141 return ret;
1142
1143 id1 = id16 >> 8;
1144 id2 = id16 & SW_CHIP_ID_M;
1145 if (id1 != FAMILY_ID ||
1146 (id2 != CHIP_ID_94 && id2 != CHIP_ID_95))
1147 return -ENODEV;
1148
1149 dev->mib_port_cnt = TOTAL_PORT_NUM;
1150 dev->phy_port_cnt = SWITCH_PORT_NUM;
1151 dev->port_cnt = SWITCH_PORT_NUM;
1152
1153 if (id2 == CHIP_ID_95) {
1154 u8 val;
1155
1156 id2 = 0x95;
1157 ksz_read8(dev, REG_PORT_1_STATUS_0, &val);
1158 if (val & PORT_FIBER_MODE)
1159 id2 = 0x65;
1160 } else if (id2 == CHIP_ID_94) {
1161 dev->port_cnt--;
1162 dev->last_port = dev->port_cnt;
1163 id2 = 0x94;
1164 }
1165 id16 &= ~0xff;
1166 id16 |= id2;
1167 dev->chip_id = id16;
1168
1169 dev->cpu_port = dev->mib_port_cnt - 1;
1170 dev->host_mask = BIT(dev->cpu_port);
1171
1172 return 0;
1173}
1174
1175struct ksz_chip_data {
1176 u16 chip_id;
1177 const char *dev_name;
1178 int num_vlans;
1179 int num_alus;
1180 int num_statics;
1181 int cpu_ports;
1182 int port_cnt;
1183};
1184
1185static const struct ksz_chip_data ksz8795_switch_chips[] = {
1186 {
1187 .chip_id = 0x8795,
1188 .dev_name = "KSZ8795",
1189 .num_vlans = 4096,
1190 .num_alus = 0,
1191 .num_statics = 8,
1192 .cpu_ports = 0x10, /* can be configured as cpu port */
1193 .port_cnt = 4, /* total physical port count */
1194 },
1195 {
1196 .chip_id = 0x8794,
1197 .dev_name = "KSZ8794",
1198 .num_vlans = 4096,
1199 .num_alus = 0,
1200 .num_statics = 8,
1201 .cpu_ports = 0x10, /* can be configured as cpu port */
1202 .port_cnt = 3, /* total physical port count */
1203 },
1204 {
1205 .chip_id = 0x8765,
1206 .dev_name = "KSZ8765",
1207 .num_vlans = 4096,
1208 .num_alus = 0,
1209 .num_statics = 8,
1210 .cpu_ports = 0x10, /* can be configured as cpu port */
1211 .port_cnt = 4, /* total physical port count */
1212 },
1213};
1214
1215static int ksz8795_switch_init(struct ksz_device *dev)
1216{
1217 int i;
1218
1219 dev->ds->ops = &ksz8795_switch_ops;
1220
1221 for (i = 0; i < ARRAY_SIZE(ksz8795_switch_chips); i++) {
1222 const struct ksz_chip_data *chip = &ksz8795_switch_chips[i];
1223
1224 if (dev->chip_id == chip->chip_id) {
1225 dev->name = chip->dev_name;
1226 dev->num_vlans = chip->num_vlans;
1227 dev->num_alus = chip->num_alus;
1228 dev->num_statics = chip->num_statics;
1229 dev->port_cnt = chip->port_cnt;
1230 dev->cpu_ports = chip->cpu_ports;
1231
1232 break;
1233 }
1234 }
1235
1236 /* no switch found */
1237 if (!dev->cpu_ports)
1238 return -ENODEV;
1239
1240 dev->port_mask = BIT(dev->port_cnt) - 1;
1241 dev->port_mask |= dev->host_mask;
1242
1243 dev->reg_mib_cnt = SWITCH_COUNTER_NUM;
1244 dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM;
1245
1246 i = dev->mib_port_cnt;
1247 dev->ports = devm_kzalloc(dev->dev, sizeof(struct ksz_port) * i,
1248 GFP_KERNEL);
1249 if (!dev->ports)
1250 return -ENOMEM;
1251 for (i = 0; i < dev->mib_port_cnt; i++) {
1252 mutex_init(&dev->ports[i].mib.cnt_mutex);
1253 dev->ports[i].mib.counters =
1254 devm_kzalloc(dev->dev,
1255 sizeof(u64) *
1256 (TOTAL_SWITCH_COUNTER_NUM + 1),
1257 GFP_KERNEL);
1258 if (!dev->ports[i].mib.counters)
1259 return -ENOMEM;
1260 }
1261
1262 /* set the real number of ports */
1263 dev->ds->num_ports = dev->port_cnt + 1;
1264
1265 return 0;
1266}
1267
1268static void ksz8795_switch_exit(struct ksz_device *dev)
1269{
1270 ksz8795_reset_switch(dev);
1271}
1272
1273static const struct ksz_dev_ops ksz8795_dev_ops = {
1274 .get_port_addr = ksz8795_get_port_addr,
1275 .cfg_port_member = ksz8795_cfg_port_member,
1276 .flush_dyn_mac_table = ksz8795_flush_dyn_mac_table,
1277 .port_setup = ksz8795_port_setup,
1278 .r_phy = ksz8795_r_phy,
1279 .w_phy = ksz8795_w_phy,
1280 .r_dyn_mac_table = ksz8795_r_dyn_mac_table,
1281 .r_sta_mac_table = ksz8795_r_sta_mac_table,
1282 .w_sta_mac_table = ksz8795_w_sta_mac_table,
1283 .r_mib_cnt = ksz8795_r_mib_cnt,
1284 .r_mib_pkt = ksz8795_r_mib_pkt,
1285 .freeze_mib = ksz8795_freeze_mib,
1286 .port_init_cnt = ksz8795_port_init_cnt,
1287 .shutdown = ksz8795_reset_switch,
1288 .detect = ksz8795_switch_detect,
1289 .init = ksz8795_switch_init,
1290 .exit = ksz8795_switch_exit,
1291};
1292
1293int ksz8795_switch_register(struct ksz_device *dev)
1294{
1295 return ksz_switch_register(dev, &ksz8795_dev_ops);
1296}
1297EXPORT_SYMBOL(ksz8795_switch_register);
1298
1299MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1300MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1301MODULE_LICENSE("GPL");