GNU Linux-libre 5.10.153-gnu1
[releases.git] / drivers / net / dsa / microchip / ksz8795.c
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
24 static 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
65 static 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
70 static 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
77 static 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
87 static 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
120 static 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
151 static 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
198 static 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
210 static 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
238 static 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
251 static 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
264 static 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
287 static 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
344 static 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
373 static 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
400 static 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
407 static 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
415 static 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
428 static 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
442 static 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
458 static 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
549 static 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
647 static 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
654 static 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
665 static 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
677 static 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
751 static 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
784 static int ksz8795_port_vlan_filtering(struct dsa_switch *ds, int port,
785                                        bool flag,
786                                        struct switchdev_trans *trans)
787 {
788         struct ksz_device *dev = ds->priv;
789
790         if (switchdev_trans_ph_prepare(trans))
791                 return 0;
792
793         /* Discard packets with VID not enabled on the switch */
794         ksz_cfg(dev, S_MIRROR_CTRL, SW_VLAN_ENABLE, flag);
795
796         /* Discard packets with VID not enabled on the ingress port */
797         for (port = 0; port < dev->phy_port_cnt; ++port)
798                 ksz_port_cfg(dev, port, REG_PORT_CTRL_2, PORT_INGRESS_FILTER,
799                              flag);
800
801         return 0;
802 }
803
804 static bool ksz8795_port_vlan_changes_remove_tag(
805         struct dsa_switch *ds, int port,
806         const struct switchdev_obj_port_vlan *vlan)
807 {
808         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
809         struct ksz_device *dev = ds->priv;
810         struct ksz_port *p = &dev->ports[port];
811
812         /* If a VLAN is added with untagged flag different from the
813          * port's Remove Tag flag, we need to change the latter.
814          * Ignore VID 0, which is always untagged.
815          * Ignore CPU port, which will always be tagged.
816          */
817         return untagged != p->remove_tag &&
818                 !(vlan->vid_begin == 0 && vlan->vid_end == 0) &&
819                 port != dev->cpu_port;
820 }
821
822 int ksz8795_port_vlan_prepare(struct dsa_switch *ds, int port,
823                               const struct switchdev_obj_port_vlan *vlan)
824 {
825         struct ksz_device *dev = ds->priv;
826
827         /* Reject attempts to add a VLAN that requires the Remove Tag
828          * flag to be changed, unless there are no other VLANs
829          * currently configured.
830          */
831         if (ksz8795_port_vlan_changes_remove_tag(ds, port, vlan)) {
832                 unsigned int vid;
833
834                 for (vid = 1; vid < dev->num_vlans; ++vid) {
835                         u8 fid, member, valid;
836
837                         /* Skip the VIDs we are going to add or reconfigure */
838                         if (vid == vlan->vid_begin) {
839                                 vid = vlan->vid_end;
840                                 continue;
841                         }
842
843                         ksz8795_from_vlan(dev->vlan_cache[vid].table[0],
844                                           &fid, &member, &valid);
845                         if (valid && (member & BIT(port)))
846                                 return -EINVAL;
847                 }
848         }
849
850         return ksz_port_vlan_prepare(ds, port, vlan);
851 }
852
853 static void ksz8795_port_vlan_add(struct dsa_switch *ds, int port,
854                                   const struct switchdev_obj_port_vlan *vlan)
855 {
856         bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
857         struct ksz_device *dev = ds->priv;
858         struct ksz_port *p = &dev->ports[port];
859         u16 data, vid, new_pvid = 0;
860         u8 fid, member, valid;
861
862         if (ksz8795_port_vlan_changes_remove_tag(ds, port, vlan)) {
863                 ksz_port_cfg(dev, port, P_TAG_CTRL, PORT_REMOVE_TAG, untagged);
864                 p->remove_tag = untagged;
865         }
866
867         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
868                 ksz8795_r_vlan_table(dev, vid, &data);
869                 ksz8795_from_vlan(data, &fid, &member, &valid);
870
871                 /* First time to setup the VLAN entry. */
872                 if (!valid) {
873                         /* Need to find a way to map VID to FID. */
874                         fid = 1;
875                         valid = 1;
876                 }
877                 member |= BIT(port);
878
879                 ksz8795_to_vlan(fid, member, valid, &data);
880                 ksz8795_w_vlan_table(dev, vid, data);
881
882                 /* change PVID */
883                 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
884                         new_pvid = vid;
885         }
886
887         if (new_pvid) {
888                 ksz_pread16(dev, port, REG_PORT_CTRL_VID, &vid);
889                 vid &= ~VLAN_VID_MASK;
890                 vid |= new_pvid;
891                 ksz_pwrite16(dev, port, REG_PORT_CTRL_VID, vid);
892
893                 ksz_pwrite8(dev, port, REG_PORT_CTRL_12, 0x0f);
894         }
895 }
896
897 static int ksz8795_port_vlan_del(struct dsa_switch *ds, int port,
898                                  const struct switchdev_obj_port_vlan *vlan)
899 {
900         struct ksz_device *dev = ds->priv;
901         u16 data, vid, pvid;
902         u8 fid, member, valid;
903         bool del_pvid = false;
904
905         ksz_pread16(dev, port, REG_PORT_CTRL_VID, &pvid);
906         pvid = pvid & 0xFFF;
907
908         for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
909                 ksz8795_r_vlan_table(dev, vid, &data);
910                 ksz8795_from_vlan(data, &fid, &member, &valid);
911
912                 member &= ~BIT(port);
913
914                 /* Invalidate the entry if no more member. */
915                 if (!member) {
916                         fid = 0;
917                         valid = 0;
918                 }
919
920                 if (pvid == vid)
921                         del_pvid = true;
922
923                 ksz8795_to_vlan(fid, member, valid, &data);
924                 ksz8795_w_vlan_table(dev, vid, data);
925         }
926
927         if (del_pvid)
928                 ksz_pwrite8(dev, port, REG_PORT_CTRL_12, 0x00);
929
930         return 0;
931 }
932
933 static int ksz8795_port_mirror_add(struct dsa_switch *ds, int port,
934                                    struct dsa_mall_mirror_tc_entry *mirror,
935                                    bool ingress)
936 {
937         struct ksz_device *dev = ds->priv;
938
939         if (ingress) {
940                 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
941                 dev->mirror_rx |= BIT(port);
942         } else {
943                 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
944                 dev->mirror_tx |= BIT(port);
945         }
946
947         ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
948
949         /* configure mirror port */
950         if (dev->mirror_rx || dev->mirror_tx)
951                 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
952                              PORT_MIRROR_SNIFFER, true);
953
954         return 0;
955 }
956
957 static void ksz8795_port_mirror_del(struct dsa_switch *ds, int port,
958                                     struct dsa_mall_mirror_tc_entry *mirror)
959 {
960         struct ksz_device *dev = ds->priv;
961         u8 data;
962
963         if (mirror->ingress) {
964                 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
965                 dev->mirror_rx &= ~BIT(port);
966         } else {
967                 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
968                 dev->mirror_tx &= ~BIT(port);
969         }
970
971         ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
972
973         if (!dev->mirror_rx && !dev->mirror_tx)
974                 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
975                              PORT_MIRROR_SNIFFER, false);
976 }
977
978 static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port)
979 {
980         struct ksz_port *p = &dev->ports[port];
981         u8 data8, member;
982
983         /* enable broadcast storm limit */
984         ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
985
986         ksz8795_set_prio_queue(dev, port, 4);
987
988         /* disable DiffServ priority */
989         ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_ENABLE, false);
990
991         /* replace priority */
992         ksz_port_cfg(dev, port, P_802_1P_CTRL, PORT_802_1P_REMAPPING, false);
993
994         /* enable 802.1p priority */
995         ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_ENABLE, true);
996
997         if (cpu_port) {
998                 if (!p->interface && dev->compat_interface) {
999                         dev_warn(dev->dev,
1000                                  "Using legacy switch \"phy-mode\" property, because it is missing on port %d node. "
1001                                  "Please update your device tree.\n",
1002                                  port);
1003                         p->interface = dev->compat_interface;
1004                 }
1005
1006                 /* Configure MII interface for proper network communication. */
1007                 ksz_read8(dev, REG_PORT_5_CTRL_6, &data8);
1008                 data8 &= ~PORT_INTERFACE_TYPE;
1009                 data8 &= ~PORT_GMII_1GPS_MODE;
1010                 switch (p->interface) {
1011                 case PHY_INTERFACE_MODE_MII:
1012                         p->phydev.speed = SPEED_100;
1013                         break;
1014                 case PHY_INTERFACE_MODE_RMII:
1015                         data8 |= PORT_INTERFACE_RMII;
1016                         p->phydev.speed = SPEED_100;
1017                         break;
1018                 case PHY_INTERFACE_MODE_GMII:
1019                         data8 |= PORT_GMII_1GPS_MODE;
1020                         data8 |= PORT_INTERFACE_GMII;
1021                         p->phydev.speed = SPEED_1000;
1022                         break;
1023                 default:
1024                         data8 &= ~PORT_RGMII_ID_IN_ENABLE;
1025                         data8 &= ~PORT_RGMII_ID_OUT_ENABLE;
1026                         if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1027                             p->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1028                                 data8 |= PORT_RGMII_ID_IN_ENABLE;
1029                         if (p->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1030                             p->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1031                                 data8 |= PORT_RGMII_ID_OUT_ENABLE;
1032                         data8 |= PORT_GMII_1GPS_MODE;
1033                         data8 |= PORT_INTERFACE_RGMII;
1034                         p->phydev.speed = SPEED_1000;
1035                         break;
1036                 }
1037                 ksz_write8(dev, REG_PORT_5_CTRL_6, data8);
1038                 p->phydev.duplex = 1;
1039
1040                 member = dev->port_mask;
1041         } else {
1042                 member = dev->host_mask | p->vid_member;
1043         }
1044         ksz8795_cfg_port_member(dev, port, member);
1045 }
1046
1047 static void ksz8795_config_cpu_port(struct dsa_switch *ds)
1048 {
1049         struct ksz_device *dev = ds->priv;
1050         struct ksz_port *p;
1051         u8 remote;
1052         int i;
1053
1054         ds->num_ports = dev->port_cnt + 1;
1055
1056         /* Switch marks the maximum frame with extra byte as oversize. */
1057         ksz_cfg(dev, REG_SW_CTRL_2, SW_LEGAL_PACKET_DISABLE, true);
1058         ksz_cfg(dev, S_TAIL_TAG_CTRL, SW_TAIL_TAG_ENABLE, true);
1059
1060         p = &dev->ports[dev->cpu_port];
1061         p->vid_member = dev->port_mask;
1062         p->on = 1;
1063
1064         ksz8795_port_setup(dev, dev->cpu_port, true);
1065         dev->member = dev->host_mask;
1066
1067         for (i = 0; i < SWITCH_PORT_NUM; i++) {
1068                 p = &dev->ports[i];
1069
1070                 /* Initialize to non-zero so that ksz_cfg_port_member() will
1071                  * be called.
1072                  */
1073                 p->vid_member = BIT(i);
1074                 p->member = dev->port_mask;
1075                 ksz8795_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1076
1077                 /* Last port may be disabled. */
1078                 if (i == dev->port_cnt)
1079                         break;
1080                 p->on = 1;
1081                 p->phy = 1;
1082         }
1083         for (i = 0; i < dev->phy_port_cnt; i++) {
1084                 p = &dev->ports[i];
1085                 if (!p->on)
1086                         continue;
1087                 ksz_pread8(dev, i, P_REMOTE_STATUS, &remote);
1088                 if (remote & PORT_FIBER_MODE)
1089                         p->fiber = 1;
1090                 if (p->fiber)
1091                         ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1092                                      true);
1093                 else
1094                         ksz_port_cfg(dev, i, P_STP_CTRL, PORT_FORCE_FLOW_CTRL,
1095                                      false);
1096         }
1097 }
1098
1099 static int ksz8795_setup(struct dsa_switch *ds)
1100 {
1101         struct ksz_device *dev = ds->priv;
1102         struct alu_struct alu;
1103         int i, ret = 0;
1104
1105         dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1106                                        dev->num_vlans, GFP_KERNEL);
1107         if (!dev->vlan_cache)
1108                 return -ENOMEM;
1109
1110         ret = ksz8795_reset_switch(dev);
1111         if (ret) {
1112                 dev_err(ds->dev, "failed to reset switch\n");
1113                 return ret;
1114         }
1115
1116         ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_FLOW_CTRL, true);
1117
1118         /* Enable automatic fast aging when link changed detected. */
1119         ksz_cfg(dev, S_LINK_AGING_CTRL, SW_LINK_AUTO_AGING, true);
1120
1121         /* Enable aggressive back off algorithm in half duplex mode. */
1122         regmap_update_bits(dev->regmap[0], REG_SW_CTRL_1,
1123                            SW_AGGR_BACKOFF, SW_AGGR_BACKOFF);
1124
1125         /*
1126          * Make sure unicast VLAN boundary is set as default and
1127          * enable no excessive collision drop.
1128          */
1129         regmap_update_bits(dev->regmap[0], REG_SW_CTRL_2,
1130                            UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP,
1131                            UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP);
1132
1133         ksz8795_config_cpu_port(ds);
1134
1135         ksz_cfg(dev, REG_SW_CTRL_2, MULTICAST_STORM_DISABLE, true);
1136
1137         ksz_cfg(dev, S_REPLACE_VID_CTRL, SW_REPLACE_VID, false);
1138
1139         ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
1140
1141         ksz_cfg(dev, REG_SW_CTRL_19, SW_INS_TAG_ENABLE, true);
1142
1143         /* set broadcast storm protection 10% rate */
1144         regmap_update_bits(dev->regmap[1], S_REPLACE_VID_CTRL,
1145                            BROADCAST_STORM_RATE,
1146                            (BROADCAST_STORM_VALUE *
1147                            BROADCAST_STORM_PROT_RATE) / 100);
1148
1149         for (i = 0; i < VLAN_TABLE_ENTRIES; i++)
1150                 ksz8795_r_vlan_entries(dev, i);
1151
1152         /* Setup STP address for STP operation. */
1153         memset(&alu, 0, sizeof(alu));
1154         ether_addr_copy(alu.mac, eth_stp_addr);
1155         alu.is_static = true;
1156         alu.is_override = true;
1157         alu.port_forward = dev->host_mask;
1158
1159         ksz8795_w_sta_mac_table(dev, 0, &alu);
1160
1161         ksz_init_mib_timer(dev);
1162
1163         return 0;
1164 }
1165
1166 static const struct dsa_switch_ops ksz8795_switch_ops = {
1167         .get_tag_protocol       = ksz8795_get_tag_protocol,
1168         .setup                  = ksz8795_setup,
1169         .phy_read               = ksz_phy_read16,
1170         .phy_write              = ksz_phy_write16,
1171         .phylink_mac_link_down  = ksz_mac_link_down,
1172         .port_enable            = ksz_enable_port,
1173         .get_strings            = ksz8795_get_strings,
1174         .get_ethtool_stats      = ksz_get_ethtool_stats,
1175         .get_sset_count         = ksz_sset_count,
1176         .port_bridge_join       = ksz_port_bridge_join,
1177         .port_bridge_leave      = ksz_port_bridge_leave,
1178         .port_stp_state_set     = ksz8795_port_stp_state_set,
1179         .port_fast_age          = ksz_port_fast_age,
1180         .port_vlan_filtering    = ksz8795_port_vlan_filtering,
1181         .port_vlan_prepare      = ksz8795_port_vlan_prepare,
1182         .port_vlan_add          = ksz8795_port_vlan_add,
1183         .port_vlan_del          = ksz8795_port_vlan_del,
1184         .port_fdb_dump          = ksz_port_fdb_dump,
1185         .port_mdb_prepare       = ksz_port_mdb_prepare,
1186         .port_mdb_add           = ksz_port_mdb_add,
1187         .port_mdb_del           = ksz_port_mdb_del,
1188         .port_mirror_add        = ksz8795_port_mirror_add,
1189         .port_mirror_del        = ksz8795_port_mirror_del,
1190 };
1191
1192 static u32 ksz8795_get_port_addr(int port, int offset)
1193 {
1194         return PORT_CTRL_ADDR(port, offset);
1195 }
1196
1197 static int ksz8795_switch_detect(struct ksz_device *dev)
1198 {
1199         u8 id1, id2;
1200         u16 id16;
1201         int ret;
1202
1203         /* read chip id */
1204         ret = ksz_read16(dev, REG_CHIP_ID0, &id16);
1205         if (ret)
1206                 return ret;
1207
1208         id1 = id16 >> 8;
1209         id2 = id16 & SW_CHIP_ID_M;
1210         if (id1 != FAMILY_ID ||
1211             (id2 != CHIP_ID_94 && id2 != CHIP_ID_95))
1212                 return -ENODEV;
1213
1214         dev->mib_port_cnt = TOTAL_PORT_NUM;
1215         dev->phy_port_cnt = SWITCH_PORT_NUM;
1216         dev->port_cnt = SWITCH_PORT_NUM;
1217
1218         if (id2 == CHIP_ID_95) {
1219                 u8 val;
1220
1221                 id2 = 0x95;
1222                 ksz_read8(dev, REG_PORT_1_STATUS_0, &val);
1223                 if (val & PORT_FIBER_MODE)
1224                         id2 = 0x65;
1225         } else if (id2 == CHIP_ID_94) {
1226                 dev->port_cnt--;
1227                 dev->last_port = dev->port_cnt;
1228                 id2 = 0x94;
1229         }
1230         id16 &= ~0xff;
1231         id16 |= id2;
1232         dev->chip_id = id16;
1233
1234         dev->cpu_port = dev->mib_port_cnt - 1;
1235         dev->host_mask = BIT(dev->cpu_port);
1236
1237         return 0;
1238 }
1239
1240 struct ksz_chip_data {
1241         u16 chip_id;
1242         const char *dev_name;
1243         int num_vlans;
1244         int num_alus;
1245         int num_statics;
1246         int cpu_ports;
1247         int port_cnt;
1248 };
1249
1250 static const struct ksz_chip_data ksz8795_switch_chips[] = {
1251         {
1252                 .chip_id = 0x8795,
1253                 .dev_name = "KSZ8795",
1254                 .num_vlans = 4096,
1255                 .num_alus = 0,
1256                 .num_statics = 8,
1257                 .cpu_ports = 0x10,      /* can be configured as cpu port */
1258                 .port_cnt = 4,          /* total physical port count */
1259         },
1260         {
1261                 .chip_id = 0x8794,
1262                 .dev_name = "KSZ8794",
1263                 .num_vlans = 4096,
1264                 .num_alus = 0,
1265                 .num_statics = 8,
1266                 .cpu_ports = 0x10,      /* can be configured as cpu port */
1267                 .port_cnt = 3,          /* total physical port count */
1268         },
1269         {
1270                 .chip_id = 0x8765,
1271                 .dev_name = "KSZ8765",
1272                 .num_vlans = 4096,
1273                 .num_alus = 0,
1274                 .num_statics = 8,
1275                 .cpu_ports = 0x10,      /* can be configured as cpu port */
1276                 .port_cnt = 4,          /* total physical port count */
1277         },
1278 };
1279
1280 static int ksz8795_switch_init(struct ksz_device *dev)
1281 {
1282         int i;
1283
1284         dev->ds->ops = &ksz8795_switch_ops;
1285
1286         for (i = 0; i < ARRAY_SIZE(ksz8795_switch_chips); i++) {
1287                 const struct ksz_chip_data *chip = &ksz8795_switch_chips[i];
1288
1289                 if (dev->chip_id == chip->chip_id) {
1290                         dev->name = chip->dev_name;
1291                         dev->num_vlans = chip->num_vlans;
1292                         dev->num_alus = chip->num_alus;
1293                         dev->num_statics = chip->num_statics;
1294                         dev->port_cnt = chip->port_cnt;
1295                         dev->cpu_ports = chip->cpu_ports;
1296
1297                         break;
1298                 }
1299         }
1300
1301         /* no switch found */
1302         if (!dev->cpu_ports)
1303                 return -ENODEV;
1304
1305         dev->port_mask = BIT(dev->port_cnt) - 1;
1306         dev->port_mask |= dev->host_mask;
1307
1308         dev->reg_mib_cnt = SWITCH_COUNTER_NUM;
1309         dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM;
1310
1311         i = dev->mib_port_cnt;
1312         dev->ports = devm_kzalloc(dev->dev, sizeof(struct ksz_port) * i,
1313                                   GFP_KERNEL);
1314         if (!dev->ports)
1315                 return -ENOMEM;
1316         for (i = 0; i < dev->mib_port_cnt; i++) {
1317                 mutex_init(&dev->ports[i].mib.cnt_mutex);
1318                 dev->ports[i].mib.counters =
1319                         devm_kzalloc(dev->dev,
1320                                      sizeof(u64) *
1321                                      (TOTAL_SWITCH_COUNTER_NUM + 1),
1322                                      GFP_KERNEL);
1323                 if (!dev->ports[i].mib.counters)
1324                         return -ENOMEM;
1325         }
1326
1327         /* set the real number of ports */
1328         dev->ds->num_ports = dev->port_cnt + 1;
1329
1330         /* We rely on software untagging on the CPU port, so that we
1331          * can support both tagged and untagged VLANs
1332          */
1333         dev->ds->untag_bridge_pvid = true;
1334
1335         /* VLAN filtering is partly controlled by the global VLAN
1336          * Enable flag
1337          */
1338         dev->ds->vlan_filtering_is_global = true;
1339
1340         return 0;
1341 }
1342
1343 static void ksz8795_switch_exit(struct ksz_device *dev)
1344 {
1345         ksz8795_reset_switch(dev);
1346 }
1347
1348 static const struct ksz_dev_ops ksz8795_dev_ops = {
1349         .get_port_addr = ksz8795_get_port_addr,
1350         .cfg_port_member = ksz8795_cfg_port_member,
1351         .flush_dyn_mac_table = ksz8795_flush_dyn_mac_table,
1352         .port_setup = ksz8795_port_setup,
1353         .r_phy = ksz8795_r_phy,
1354         .w_phy = ksz8795_w_phy,
1355         .r_dyn_mac_table = ksz8795_r_dyn_mac_table,
1356         .r_sta_mac_table = ksz8795_r_sta_mac_table,
1357         .w_sta_mac_table = ksz8795_w_sta_mac_table,
1358         .r_mib_cnt = ksz8795_r_mib_cnt,
1359         .r_mib_pkt = ksz8795_r_mib_pkt,
1360         .freeze_mib = ksz8795_freeze_mib,
1361         .port_init_cnt = ksz8795_port_init_cnt,
1362         .shutdown = ksz8795_reset_switch,
1363         .detect = ksz8795_switch_detect,
1364         .init = ksz8795_switch_init,
1365         .exit = ksz8795_switch_exit,
1366 };
1367
1368 int ksz8795_switch_register(struct ksz_device *dev)
1369 {
1370         return ksz_switch_register(dev, &ksz8795_dev_ops);
1371 }
1372 EXPORT_SYMBOL(ksz8795_switch_register);
1373
1374 MODULE_AUTHOR("Tristram Ha <Tristram.Ha@microchip.com>");
1375 MODULE_DESCRIPTION("Microchip KSZ8795 Series Switch DSA Driver");
1376 MODULE_LICENSE("GPL");