blob: 3475f82d6d2aecc3ea8cbc8a8bc9eabcb09e1185 [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0
2/*
3 * Microchip KSZ9477 switch driver main logic
4 *
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
6 */
7
8#include <linux/kernel.h>
9#include <linux/module.h>
10#include <linux/iopoll.h>
11#include <linux/platform_data/microchip-ksz.h>
12#include <linux/phy.h>
13#include <linux/if_bridge.h>
14#include <net/dsa.h>
15#include <net/switchdev.h>
16
17#include "ksz9477_reg.h"
18#include "ksz_common.h"
19
20/* Used with variable features to indicate capabilities. */
21#define GBIT_SUPPORT BIT(0)
22#define NEW_XMII BIT(1)
23#define IS_9893 BIT(2)
24
25static const struct {
26 int index;
27 char string[ETH_GSTRING_LEN];
28} ksz9477_mib_names[TOTAL_SWITCH_COUNTER_NUM] = {
29 { 0x00, "rx_hi" },
30 { 0x01, "rx_undersize" },
31 { 0x02, "rx_fragments" },
32 { 0x03, "rx_oversize" },
33 { 0x04, "rx_jabbers" },
34 { 0x05, "rx_symbol_err" },
35 { 0x06, "rx_crc_err" },
36 { 0x07, "rx_align_err" },
37 { 0x08, "rx_mac_ctrl" },
38 { 0x09, "rx_pause" },
39 { 0x0A, "rx_bcast" },
40 { 0x0B, "rx_mcast" },
41 { 0x0C, "rx_ucast" },
42 { 0x0D, "rx_64_or_less" },
43 { 0x0E, "rx_65_127" },
44 { 0x0F, "rx_128_255" },
45 { 0x10, "rx_256_511" },
46 { 0x11, "rx_512_1023" },
47 { 0x12, "rx_1024_1522" },
48 { 0x13, "rx_1523_2000" },
49 { 0x14, "rx_2001" },
50 { 0x15, "tx_hi" },
51 { 0x16, "tx_late_col" },
52 { 0x17, "tx_pause" },
53 { 0x18, "tx_bcast" },
54 { 0x19, "tx_mcast" },
55 { 0x1A, "tx_ucast" },
56 { 0x1B, "tx_deferred" },
57 { 0x1C, "tx_total_col" },
58 { 0x1D, "tx_exc_col" },
59 { 0x1E, "tx_single_col" },
60 { 0x1F, "tx_mult_col" },
61 { 0x80, "rx_total" },
62 { 0x81, "tx_total" },
63 { 0x82, "rx_discards" },
64 { 0x83, "tx_discards" },
65};
66
67static void ksz_cfg(struct ksz_device *dev, u32 addr, u8 bits, bool set)
68{
69 regmap_update_bits(dev->regmap[0], addr, bits, set ? bits : 0);
70}
71
72static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,
73 bool set)
74{
75 regmap_update_bits(dev->regmap[0], PORT_CTRL_ADDR(port, offset),
76 bits, set ? bits : 0);
77}
78
79static void ksz9477_cfg32(struct ksz_device *dev, u32 addr, u32 bits, bool set)
80{
81 regmap_update_bits(dev->regmap[2], addr, bits, set ? bits : 0);
82}
83
84static void ksz9477_port_cfg32(struct ksz_device *dev, int port, int offset,
85 u32 bits, bool set)
86{
87 regmap_update_bits(dev->regmap[2], PORT_CTRL_ADDR(port, offset),
88 bits, set ? bits : 0);
89}
90
91static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device *dev)
92{
93 unsigned int val;
94
95 return regmap_read_poll_timeout(dev->regmap[0], REG_SW_VLAN_CTRL,
96 val, !(val & VLAN_START), 10, 1000);
97}
98
99static int ksz9477_get_vlan_table(struct ksz_device *dev, u16 vid,
100 u32 *vlan_table)
101{
102 int ret;
103
104 mutex_lock(&dev->vlan_mutex);
105
106 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
107 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_READ | VLAN_START);
108
109 /* wait to be cleared */
110 ret = ksz9477_wait_vlan_ctrl_ready(dev);
111 if (ret) {
112 dev_dbg(dev->dev, "Failed to read vlan table\n");
113 goto exit;
114 }
115
116 ksz_read32(dev, REG_SW_VLAN_ENTRY__4, &vlan_table[0]);
117 ksz_read32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, &vlan_table[1]);
118 ksz_read32(dev, REG_SW_VLAN_ENTRY_PORTS__4, &vlan_table[2]);
119
120 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
121
122exit:
123 mutex_unlock(&dev->vlan_mutex);
124
125 return ret;
126}
127
128static int ksz9477_set_vlan_table(struct ksz_device *dev, u16 vid,
129 u32 *vlan_table)
130{
131 int ret;
132
133 mutex_lock(&dev->vlan_mutex);
134
135 ksz_write32(dev, REG_SW_VLAN_ENTRY__4, vlan_table[0]);
136 ksz_write32(dev, REG_SW_VLAN_ENTRY_UNTAG__4, vlan_table[1]);
137 ksz_write32(dev, REG_SW_VLAN_ENTRY_PORTS__4, vlan_table[2]);
138
139 ksz_write16(dev, REG_SW_VLAN_ENTRY_INDEX__2, vid & VLAN_INDEX_M);
140 ksz_write8(dev, REG_SW_VLAN_CTRL, VLAN_START | VLAN_WRITE);
141
142 /* wait to be cleared */
143 ret = ksz9477_wait_vlan_ctrl_ready(dev);
144 if (ret) {
145 dev_dbg(dev->dev, "Failed to write vlan table\n");
146 goto exit;
147 }
148
149 ksz_write8(dev, REG_SW_VLAN_CTRL, 0);
150
151 /* update vlan cache table */
152 dev->vlan_cache[vid].table[0] = vlan_table[0];
153 dev->vlan_cache[vid].table[1] = vlan_table[1];
154 dev->vlan_cache[vid].table[2] = vlan_table[2];
155
156exit:
157 mutex_unlock(&dev->vlan_mutex);
158
159 return ret;
160}
161
162static void ksz9477_read_table(struct ksz_device *dev, u32 *table)
163{
164 ksz_read32(dev, REG_SW_ALU_VAL_A, &table[0]);
165 ksz_read32(dev, REG_SW_ALU_VAL_B, &table[1]);
166 ksz_read32(dev, REG_SW_ALU_VAL_C, &table[2]);
167 ksz_read32(dev, REG_SW_ALU_VAL_D, &table[3]);
168}
169
170static void ksz9477_write_table(struct ksz_device *dev, u32 *table)
171{
172 ksz_write32(dev, REG_SW_ALU_VAL_A, table[0]);
173 ksz_write32(dev, REG_SW_ALU_VAL_B, table[1]);
174 ksz_write32(dev, REG_SW_ALU_VAL_C, table[2]);
175 ksz_write32(dev, REG_SW_ALU_VAL_D, table[3]);
176}
177
178static int ksz9477_wait_alu_ready(struct ksz_device *dev)
179{
180 unsigned int val;
181
182 return regmap_read_poll_timeout(dev->regmap[2], REG_SW_ALU_CTRL__4,
183 val, !(val & ALU_START), 10, 1000);
184}
185
186static int ksz9477_wait_alu_sta_ready(struct ksz_device *dev)
187{
188 unsigned int val;
189
190 return regmap_read_poll_timeout(dev->regmap[2],
191 REG_SW_ALU_STAT_CTRL__4,
192 val, !(val & ALU_STAT_START),
193 10, 1000);
194}
195
196static int ksz9477_reset_switch(struct ksz_device *dev)
197{
198 u8 data8;
199 u32 data32;
200
201 /* reset switch */
202 ksz_cfg(dev, REG_SW_OPERATION, SW_RESET, true);
203
204 /* turn off SPI DO Edge select */
205 regmap_update_bits(dev->regmap[0], REG_SW_GLOBAL_SERIAL_CTRL_0,
206 SPI_AUTO_EDGE_DETECTION, 0);
207
208 /* default configuration */
209 ksz_write8(dev, REG_SW_LUE_CTRL_1,
210 SW_AGING_ENABLE | SW_LINK_AUTO_AGING | SW_SRC_ADDR_FILTER);
211
212 /* disable interrupts */
213 ksz_write32(dev, REG_SW_INT_MASK__4, SWITCH_INT_MASK);
214 ksz_write32(dev, REG_SW_PORT_INT_MASK__4, 0x7F);
215 ksz_read32(dev, REG_SW_PORT_INT_STATUS__4, &data32);
216
217 /* set broadcast storm protection 10% rate */
218 regmap_update_bits(dev->regmap[1], REG_SW_MAC_CTRL_2,
219 BROADCAST_STORM_RATE,
220 (BROADCAST_STORM_VALUE *
221 BROADCAST_STORM_PROT_RATE) / 100);
222
223 if (dev->synclko_125)
224 ksz_write8(dev, REG_SW_GLOBAL_OUTPUT_CTRL__1,
225 SW_ENABLE_REFCLKO | SW_REFCLKO_IS_125MHZ);
226
227 return 0;
228}
229
230static void ksz9477_r_mib_cnt(struct ksz_device *dev, int port, u16 addr,
231 u64 *cnt)
232{
233 struct ksz_port *p = &dev->ports[port];
234 unsigned int val;
235 u32 data;
236 int ret;
237
238 /* retain the flush/freeze bit */
239 data = p->freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
240 data |= MIB_COUNTER_READ;
241 data |= (addr << MIB_COUNTER_INDEX_S);
242 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, data);
243
244 ret = regmap_read_poll_timeout(dev->regmap[2],
245 PORT_CTRL_ADDR(port, REG_PORT_MIB_CTRL_STAT__4),
246 val, !(val & MIB_COUNTER_READ), 10, 1000);
247 /* failed to read MIB. get out of loop */
248 if (ret) {
249 dev_dbg(dev->dev, "Failed to get MIB\n");
250 return;
251 }
252
253 /* count resets upon read */
254 ksz_pread32(dev, port, REG_PORT_MIB_DATA, &data);
255 *cnt += data;
256}
257
258static void ksz9477_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,
259 u64 *dropped, u64 *cnt)
260{
261 addr = ksz9477_mib_names[addr].index;
262 ksz9477_r_mib_cnt(dev, port, addr, cnt);
263}
264
265static void ksz9477_freeze_mib(struct ksz_device *dev, int port, bool freeze)
266{
267 u32 val = freeze ? MIB_COUNTER_FLUSH_FREEZE : 0;
268 struct ksz_port *p = &dev->ports[port];
269
270 /* enable/disable the port for flush/freeze function */
271 mutex_lock(&p->mib.cnt_mutex);
272 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, val);
273
274 /* used by MIB counter reading code to know freeze is enabled */
275 p->freeze = freeze;
276 mutex_unlock(&p->mib.cnt_mutex);
277}
278
279static void ksz9477_port_init_cnt(struct ksz_device *dev, int port)
280{
281 struct ksz_port_mib *mib = &dev->ports[port].mib;
282
283 /* flush all enabled port MIB counters */
284 mutex_lock(&mib->cnt_mutex);
285 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4,
286 MIB_COUNTER_FLUSH_FREEZE);
287 ksz_write8(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FLUSH);
288 ksz_pwrite32(dev, port, REG_PORT_MIB_CTRL_STAT__4, 0);
289 mutex_unlock(&mib->cnt_mutex);
290
291 mib->cnt_ptr = 0;
292 memset(mib->counters, 0, dev->mib_cnt * sizeof(u64));
293}
294
295static enum dsa_tag_protocol ksz9477_get_tag_protocol(struct dsa_switch *ds,
296 int port,
297 enum dsa_tag_protocol mp)
298{
299 enum dsa_tag_protocol proto = DSA_TAG_PROTO_KSZ9477;
300 struct ksz_device *dev = ds->priv;
301
302 if (dev->features & IS_9893)
303 proto = DSA_TAG_PROTO_KSZ9893;
304 return proto;
305}
306
307static int ksz9477_phy_read16(struct dsa_switch *ds, int addr, int reg)
308{
309 struct ksz_device *dev = ds->priv;
310 u16 val = 0xffff;
311
312 /* No real PHY after this. Simulate the PHY.
313 * A fixed PHY can be setup in the device tree, but this function is
314 * still called for that port during initialization.
315 * For RGMII PHY there is no way to access it so the fixed PHY should
316 * be used. For SGMII PHY the supporting code will be added later.
317 */
318 if (addr >= dev->phy_port_cnt) {
319 struct ksz_port *p = &dev->ports[addr];
320
321 switch (reg) {
322 case MII_BMCR:
323 val = 0x1140;
324 break;
325 case MII_BMSR:
326 val = 0x796d;
327 break;
328 case MII_PHYSID1:
329 val = 0x0022;
330 break;
331 case MII_PHYSID2:
332 val = 0x1631;
333 break;
334 case MII_ADVERTISE:
335 val = 0x05e1;
336 break;
337 case MII_LPA:
338 val = 0xc5e1;
339 break;
340 case MII_CTRL1000:
341 val = 0x0700;
342 break;
343 case MII_STAT1000:
344 if (p->phydev.speed == SPEED_1000)
345 val = 0x3800;
346 else
347 val = 0;
348 break;
349 }
350 } else {
351 ksz_pread16(dev, addr, 0x100 + (reg << 1), &val);
352 }
353
354 return val;
355}
356
357static int ksz9477_phy_write16(struct dsa_switch *ds, int addr, int reg,
358 u16 val)
359{
360 struct ksz_device *dev = ds->priv;
361
362 /* No real PHY after this. */
363 if (addr >= dev->phy_port_cnt)
364 return 0;
365
366 /* No gigabit support. Do not write to this register. */
367 if (!(dev->features & GBIT_SUPPORT) && reg == MII_CTRL1000)
368 return 0;
369 ksz_pwrite16(dev, addr, 0x100 + (reg << 1), val);
370
371 return 0;
372}
373
374static void ksz9477_get_strings(struct dsa_switch *ds, int port,
375 u32 stringset, uint8_t *buf)
376{
377 int i;
378
379 if (stringset != ETH_SS_STATS)
380 return;
381
382 for (i = 0; i < TOTAL_SWITCH_COUNTER_NUM; i++) {
383 memcpy(buf + i * ETH_GSTRING_LEN, ksz9477_mib_names[i].string,
384 ETH_GSTRING_LEN);
385 }
386}
387
388static void ksz9477_cfg_port_member(struct ksz_device *dev, int port,
389 u8 member)
390{
391 ksz_pwrite32(dev, port, REG_PORT_VLAN_MEMBERSHIP__4, member);
392 dev->ports[port].member = member;
393}
394
395static void ksz9477_port_stp_state_set(struct dsa_switch *ds, int port,
396 u8 state)
397{
398 struct ksz_device *dev = ds->priv;
399 struct ksz_port *p = &dev->ports[port];
400 u8 data;
401 int member = -1;
402 int forward = dev->member;
403
404 ksz_pread8(dev, port, P_STP_CTRL, &data);
405 data &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
406
407 switch (state) {
408 case BR_STATE_DISABLED:
409 data |= PORT_LEARN_DISABLE;
410 if (port != dev->cpu_port)
411 member = 0;
412 break;
413 case BR_STATE_LISTENING:
414 data |= (PORT_RX_ENABLE | PORT_LEARN_DISABLE);
415 if (port != dev->cpu_port &&
416 p->stp_state == BR_STATE_DISABLED)
417 member = dev->host_mask | p->vid_member;
418 break;
419 case BR_STATE_LEARNING:
420 data |= PORT_RX_ENABLE;
421 break;
422 case BR_STATE_FORWARDING:
423 data |= (PORT_TX_ENABLE | PORT_RX_ENABLE);
424
425 /* This function is also used internally. */
426 if (port == dev->cpu_port)
427 break;
428
429 member = dev->host_mask | p->vid_member;
430 mutex_lock(&dev->dev_mutex);
431
432 /* Port is a member of a bridge. */
433 if (dev->br_member & (1 << port)) {
434 dev->member |= (1 << port);
435 member = dev->member;
436 }
437 mutex_unlock(&dev->dev_mutex);
438 break;
439 case BR_STATE_BLOCKING:
440 data |= PORT_LEARN_DISABLE;
441 if (port != dev->cpu_port &&
442 p->stp_state == BR_STATE_DISABLED)
443 member = dev->host_mask | p->vid_member;
444 break;
445 default:
446 dev_err(ds->dev, "invalid STP state: %d\n", state);
447 return;
448 }
449
450 ksz_pwrite8(dev, port, P_STP_CTRL, data);
451 p->stp_state = state;
452 mutex_lock(&dev->dev_mutex);
453 if (data & PORT_RX_ENABLE)
454 dev->rx_ports |= (1 << port);
455 else
456 dev->rx_ports &= ~(1 << port);
457 if (data & PORT_TX_ENABLE)
458 dev->tx_ports |= (1 << port);
459 else
460 dev->tx_ports &= ~(1 << port);
461
462 /* Port membership may share register with STP state. */
463 if (member >= 0 && member != p->member)
464 ksz9477_cfg_port_member(dev, port, (u8)member);
465
466 /* Check if forwarding needs to be updated. */
467 if (state != BR_STATE_FORWARDING) {
468 if (dev->br_member & (1 << port))
469 dev->member &= ~(1 << port);
470 }
471
472 /* When topology has changed the function ksz_update_port_member
473 * should be called to modify port forwarding behavior.
474 */
475 if (forward != dev->member)
476 ksz_update_port_member(dev, port);
477 mutex_unlock(&dev->dev_mutex);
478}
479
480static void ksz9477_flush_dyn_mac_table(struct ksz_device *dev, int port)
481{
482 u8 data;
483
484 regmap_update_bits(dev->regmap[0], REG_SW_LUE_CTRL_2,
485 SW_FLUSH_OPTION_M << SW_FLUSH_OPTION_S,
486 SW_FLUSH_OPTION_DYN_MAC << SW_FLUSH_OPTION_S);
487
488 if (port < dev->mib_port_cnt) {
489 /* flush individual port */
490 ksz_pread8(dev, port, P_STP_CTRL, &data);
491 if (!(data & PORT_LEARN_DISABLE))
492 ksz_pwrite8(dev, port, P_STP_CTRL,
493 data | PORT_LEARN_DISABLE);
494 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_DYN_MAC_TABLE, true);
495 ksz_pwrite8(dev, port, P_STP_CTRL, data);
496 } else {
497 /* flush all */
498 ksz_cfg(dev, S_FLUSH_TABLE_CTRL, SW_FLUSH_STP_TABLE, true);
499 }
500}
501
502static int ksz9477_port_vlan_filtering(struct dsa_switch *ds, int port,
503 bool flag)
504{
505 struct ksz_device *dev = ds->priv;
506
507 if (flag) {
508 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
509 PORT_VLAN_LOOKUP_VID_0, true);
510 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, true);
511 } else {
512 ksz_cfg(dev, REG_SW_LUE_CTRL_0, SW_VLAN_ENABLE, false);
513 ksz_port_cfg(dev, port, REG_PORT_LUE_CTRL,
514 PORT_VLAN_LOOKUP_VID_0, false);
515 }
516
517 return 0;
518}
519
520static void ksz9477_port_vlan_add(struct dsa_switch *ds, int port,
521 const struct switchdev_obj_port_vlan *vlan)
522{
523 struct ksz_device *dev = ds->priv;
524 u32 vlan_table[3];
525 u16 vid;
526 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
527
528 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
529 if (ksz9477_get_vlan_table(dev, vid, vlan_table)) {
530 dev_dbg(dev->dev, "Failed to get vlan table\n");
531 return;
532 }
533
534 vlan_table[0] = VLAN_VALID | (vid & VLAN_FID_M);
535 if (untagged)
536 vlan_table[1] |= BIT(port);
537 else
538 vlan_table[1] &= ~BIT(port);
539 vlan_table[1] &= ~(BIT(dev->cpu_port));
540
541 vlan_table[2] |= BIT(port) | BIT(dev->cpu_port);
542
543 if (ksz9477_set_vlan_table(dev, vid, vlan_table)) {
544 dev_dbg(dev->dev, "Failed to set vlan table\n");
545 return;
546 }
547
548 /* change PVID */
549 if (vlan->flags & BRIDGE_VLAN_INFO_PVID)
550 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, vid);
551 }
552}
553
554static int ksz9477_port_vlan_del(struct dsa_switch *ds, int port,
555 const struct switchdev_obj_port_vlan *vlan)
556{
557 struct ksz_device *dev = ds->priv;
558 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
559 u32 vlan_table[3];
560 u16 vid;
561 u16 pvid;
562
563 ksz_pread16(dev, port, REG_PORT_DEFAULT_VID, &pvid);
564 pvid = pvid & 0xFFF;
565
566 for (vid = vlan->vid_begin; vid <= vlan->vid_end; vid++) {
567 if (ksz9477_get_vlan_table(dev, vid, vlan_table)) {
568 dev_dbg(dev->dev, "Failed to get vlan table\n");
569 return -ETIMEDOUT;
570 }
571
572 vlan_table[2] &= ~BIT(port);
573
574 if (pvid == vid)
575 pvid = 1;
576
577 if (untagged)
578 vlan_table[1] &= ~BIT(port);
579
580 if (ksz9477_set_vlan_table(dev, vid, vlan_table)) {
581 dev_dbg(dev->dev, "Failed to set vlan table\n");
582 return -ETIMEDOUT;
583 }
584 }
585
586 ksz_pwrite16(dev, port, REG_PORT_DEFAULT_VID, pvid);
587
588 return 0;
589}
590
591static int ksz9477_port_fdb_add(struct dsa_switch *ds, int port,
592 const unsigned char *addr, u16 vid)
593{
594 struct ksz_device *dev = ds->priv;
595 u32 alu_table[4];
596 u32 data;
597 int ret = 0;
598
599 mutex_lock(&dev->alu_mutex);
600
601 /* find any entry with mac & vid */
602 data = vid << ALU_FID_INDEX_S;
603 data |= ((addr[0] << 8) | addr[1]);
604 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
605
606 data = ((addr[2] << 24) | (addr[3] << 16));
607 data |= ((addr[4] << 8) | addr[5]);
608 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
609
610 /* start read operation */
611 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
612
613 /* wait to be finished */
614 ret = ksz9477_wait_alu_ready(dev);
615 if (ret) {
616 dev_dbg(dev->dev, "Failed to read ALU\n");
617 goto exit;
618 }
619
620 /* read ALU entry */
621 ksz9477_read_table(dev, alu_table);
622
623 /* update ALU entry */
624 alu_table[0] = ALU_V_STATIC_VALID;
625 alu_table[1] |= BIT(port);
626 if (vid)
627 alu_table[1] |= ALU_V_USE_FID;
628 alu_table[2] = (vid << ALU_V_FID_S);
629 alu_table[2] |= ((addr[0] << 8) | addr[1]);
630 alu_table[3] = ((addr[2] << 24) | (addr[3] << 16));
631 alu_table[3] |= ((addr[4] << 8) | addr[5]);
632
633 ksz9477_write_table(dev, alu_table);
634
635 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
636
637 /* wait to be finished */
638 ret = ksz9477_wait_alu_ready(dev);
639 if (ret)
640 dev_dbg(dev->dev, "Failed to write ALU\n");
641
642exit:
643 mutex_unlock(&dev->alu_mutex);
644
645 return ret;
646}
647
648static int ksz9477_port_fdb_del(struct dsa_switch *ds, int port,
649 const unsigned char *addr, u16 vid)
650{
651 struct ksz_device *dev = ds->priv;
652 u32 alu_table[4];
653 u32 data;
654 int ret = 0;
655
656 mutex_lock(&dev->alu_mutex);
657
658 /* read any entry with mac & vid */
659 data = vid << ALU_FID_INDEX_S;
660 data |= ((addr[0] << 8) | addr[1]);
661 ksz_write32(dev, REG_SW_ALU_INDEX_0, data);
662
663 data = ((addr[2] << 24) | (addr[3] << 16));
664 data |= ((addr[4] << 8) | addr[5]);
665 ksz_write32(dev, REG_SW_ALU_INDEX_1, data);
666
667 /* start read operation */
668 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_READ | ALU_START);
669
670 /* wait to be finished */
671 ret = ksz9477_wait_alu_ready(dev);
672 if (ret) {
673 dev_dbg(dev->dev, "Failed to read ALU\n");
674 goto exit;
675 }
676
677 ksz_read32(dev, REG_SW_ALU_VAL_A, &alu_table[0]);
678 if (alu_table[0] & ALU_V_STATIC_VALID) {
679 ksz_read32(dev, REG_SW_ALU_VAL_B, &alu_table[1]);
680 ksz_read32(dev, REG_SW_ALU_VAL_C, &alu_table[2]);
681 ksz_read32(dev, REG_SW_ALU_VAL_D, &alu_table[3]);
682
683 /* clear forwarding port */
684 alu_table[1] &= ~BIT(port);
685
686 /* if there is no port to forward, clear table */
687 if ((alu_table[1] & ALU_V_PORT_MAP) == 0) {
688 alu_table[0] = 0;
689 alu_table[1] = 0;
690 alu_table[2] = 0;
691 alu_table[3] = 0;
692 }
693 } else {
694 alu_table[0] = 0;
695 alu_table[1] = 0;
696 alu_table[2] = 0;
697 alu_table[3] = 0;
698 }
699
700 ksz9477_write_table(dev, alu_table);
701
702 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_WRITE | ALU_START);
703
704 /* wait to be finished */
705 ret = ksz9477_wait_alu_ready(dev);
706 if (ret)
707 dev_dbg(dev->dev, "Failed to write ALU\n");
708
709exit:
710 mutex_unlock(&dev->alu_mutex);
711
712 return ret;
713}
714
715static void ksz9477_convert_alu(struct alu_struct *alu, u32 *alu_table)
716{
717 alu->is_static = !!(alu_table[0] & ALU_V_STATIC_VALID);
718 alu->is_src_filter = !!(alu_table[0] & ALU_V_SRC_FILTER);
719 alu->is_dst_filter = !!(alu_table[0] & ALU_V_DST_FILTER);
720 alu->prio_age = (alu_table[0] >> ALU_V_PRIO_AGE_CNT_S) &
721 ALU_V_PRIO_AGE_CNT_M;
722 alu->mstp = alu_table[0] & ALU_V_MSTP_M;
723
724 alu->is_override = !!(alu_table[1] & ALU_V_OVERRIDE);
725 alu->is_use_fid = !!(alu_table[1] & ALU_V_USE_FID);
726 alu->port_forward = alu_table[1] & ALU_V_PORT_MAP;
727
728 alu->fid = (alu_table[2] >> ALU_V_FID_S) & ALU_V_FID_M;
729
730 alu->mac[0] = (alu_table[2] >> 8) & 0xFF;
731 alu->mac[1] = alu_table[2] & 0xFF;
732 alu->mac[2] = (alu_table[3] >> 24) & 0xFF;
733 alu->mac[3] = (alu_table[3] >> 16) & 0xFF;
734 alu->mac[4] = (alu_table[3] >> 8) & 0xFF;
735 alu->mac[5] = alu_table[3] & 0xFF;
736}
737
738static int ksz9477_port_fdb_dump(struct dsa_switch *ds, int port,
739 dsa_fdb_dump_cb_t *cb, void *data)
740{
741 struct ksz_device *dev = ds->priv;
742 int ret = 0;
743 u32 ksz_data;
744 u32 alu_table[4];
745 struct alu_struct alu;
746 int timeout;
747
748 mutex_lock(&dev->alu_mutex);
749
750 /* start ALU search */
751 ksz_write32(dev, REG_SW_ALU_CTRL__4, ALU_START | ALU_SEARCH);
752
753 do {
754 timeout = 1000;
755 do {
756 ksz_read32(dev, REG_SW_ALU_CTRL__4, &ksz_data);
757 if ((ksz_data & ALU_VALID) || !(ksz_data & ALU_START))
758 break;
759 usleep_range(1, 10);
760 } while (timeout-- > 0);
761
762 if (!timeout) {
763 dev_dbg(dev->dev, "Failed to search ALU\n");
764 ret = -ETIMEDOUT;
765 goto exit;
766 }
767
768 if (!(ksz_data & ALU_VALID))
769 continue;
770
771 /* read ALU table */
772 ksz9477_read_table(dev, alu_table);
773
774 ksz9477_convert_alu(&alu, alu_table);
775
776 if (alu.port_forward & BIT(port)) {
777 ret = cb(alu.mac, alu.fid, alu.is_static, data);
778 if (ret)
779 goto exit;
780 }
781 } while (ksz_data & ALU_START);
782
783exit:
784
785 /* stop ALU search */
786 ksz_write32(dev, REG_SW_ALU_CTRL__4, 0);
787
788 mutex_unlock(&dev->alu_mutex);
789
790 return ret;
791}
792
793static void ksz9477_port_mdb_add(struct dsa_switch *ds, int port,
794 const struct switchdev_obj_port_mdb *mdb)
795{
796 struct ksz_device *dev = ds->priv;
797 u32 static_table[4];
798 u32 data;
799 int index;
800 u32 mac_hi, mac_lo;
801
802 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
803 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
804 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
805
806 mutex_lock(&dev->alu_mutex);
807
808 for (index = 0; index < dev->num_statics; index++) {
809 /* find empty slot first */
810 data = (index << ALU_STAT_INDEX_S) |
811 ALU_STAT_READ | ALU_STAT_START;
812 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
813
814 /* wait to be finished */
815 if (ksz9477_wait_alu_sta_ready(dev)) {
816 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
817 goto exit;
818 }
819
820 /* read ALU static table */
821 ksz9477_read_table(dev, static_table);
822
823 if (static_table[0] & ALU_V_STATIC_VALID) {
824 /* check this has same vid & mac address */
825 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
826 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
827 static_table[3] == mac_lo) {
828 /* found matching one */
829 break;
830 }
831 } else {
832 /* found empty one */
833 break;
834 }
835 }
836
837 /* no available entry */
838 if (index == dev->num_statics)
839 goto exit;
840
841 /* add entry */
842 static_table[0] = ALU_V_STATIC_VALID;
843 static_table[1] |= BIT(port);
844 if (mdb->vid)
845 static_table[1] |= ALU_V_USE_FID;
846 static_table[2] = (mdb->vid << ALU_V_FID_S);
847 static_table[2] |= mac_hi;
848 static_table[3] = mac_lo;
849
850 ksz9477_write_table(dev, static_table);
851
852 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
853 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
854
855 /* wait to be finished */
856 if (ksz9477_wait_alu_sta_ready(dev))
857 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
858
859exit:
860 mutex_unlock(&dev->alu_mutex);
861}
862
863static int ksz9477_port_mdb_del(struct dsa_switch *ds, int port,
864 const struct switchdev_obj_port_mdb *mdb)
865{
866 struct ksz_device *dev = ds->priv;
867 u32 static_table[4];
868 u32 data;
869 int index;
870 int ret = 0;
871 u32 mac_hi, mac_lo;
872
873 mac_hi = ((mdb->addr[0] << 8) | mdb->addr[1]);
874 mac_lo = ((mdb->addr[2] << 24) | (mdb->addr[3] << 16));
875 mac_lo |= ((mdb->addr[4] << 8) | mdb->addr[5]);
876
877 mutex_lock(&dev->alu_mutex);
878
879 for (index = 0; index < dev->num_statics; index++) {
880 /* find empty slot first */
881 data = (index << ALU_STAT_INDEX_S) |
882 ALU_STAT_READ | ALU_STAT_START;
883 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
884
885 /* wait to be finished */
886 ret = ksz9477_wait_alu_sta_ready(dev);
887 if (ret) {
888 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
889 goto exit;
890 }
891
892 /* read ALU static table */
893 ksz9477_read_table(dev, static_table);
894
895 if (static_table[0] & ALU_V_STATIC_VALID) {
896 /* check this has same vid & mac address */
897
898 if (((static_table[2] >> ALU_V_FID_S) == mdb->vid) &&
899 ((static_table[2] & ALU_V_MAC_ADDR_HI) == mac_hi) &&
900 static_table[3] == mac_lo) {
901 /* found matching one */
902 break;
903 }
904 }
905 }
906
907 /* no available entry */
908 if (index == dev->num_statics)
909 goto exit;
910
911 /* clear port */
912 static_table[1] &= ~BIT(port);
913
914 if ((static_table[1] & ALU_V_PORT_MAP) == 0) {
915 /* delete entry */
916 static_table[0] = 0;
917 static_table[1] = 0;
918 static_table[2] = 0;
919 static_table[3] = 0;
920 }
921
922 ksz9477_write_table(dev, static_table);
923
924 data = (index << ALU_STAT_INDEX_S) | ALU_STAT_START;
925 ksz_write32(dev, REG_SW_ALU_STAT_CTRL__4, data);
926
927 /* wait to be finished */
928 ret = ksz9477_wait_alu_sta_ready(dev);
929 if (ret)
930 dev_dbg(dev->dev, "Failed to read ALU STATIC\n");
931
932exit:
933 mutex_unlock(&dev->alu_mutex);
934
935 return ret;
936}
937
938static int ksz9477_port_mirror_add(struct dsa_switch *ds, int port,
939 struct dsa_mall_mirror_tc_entry *mirror,
940 bool ingress)
941{
942 struct ksz_device *dev = ds->priv;
943
944 if (ingress)
945 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, true);
946 else
947 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, true);
948
949 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_SNIFFER, false);
950
951 /* configure mirror port */
952 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
953 PORT_MIRROR_SNIFFER, true);
954
955 ksz_cfg(dev, S_MIRROR_CTRL, SW_MIRROR_RX_TX, false);
956
957 return 0;
958}
959
960static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port,
961 struct dsa_mall_mirror_tc_entry *mirror)
962{
963 struct ksz_device *dev = ds->priv;
964 u8 data;
965
966 if (mirror->ingress)
967 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_RX, false);
968 else
969 ksz_port_cfg(dev, port, P_MIRROR_CTRL, PORT_MIRROR_TX, false);
970
971 ksz_pread8(dev, port, P_MIRROR_CTRL, &data);
972
973 if (!(data & (PORT_MIRROR_RX | PORT_MIRROR_TX)))
974 ksz_port_cfg(dev, mirror->to_local_port, P_MIRROR_CTRL,
975 PORT_MIRROR_SNIFFER, false);
976}
977
978static bool ksz9477_get_gbit(struct ksz_device *dev, u8 data)
979{
980 bool gbit;
981
982 if (dev->features & NEW_XMII)
983 gbit = !(data & PORT_MII_NOT_1GBIT);
984 else
985 gbit = !!(data & PORT_MII_1000MBIT_S1);
986 return gbit;
987}
988
989static void ksz9477_set_gbit(struct ksz_device *dev, bool gbit, u8 *data)
990{
991 if (dev->features & NEW_XMII) {
992 if (gbit)
993 *data &= ~PORT_MII_NOT_1GBIT;
994 else
995 *data |= PORT_MII_NOT_1GBIT;
996 } else {
997 if (gbit)
998 *data |= PORT_MII_1000MBIT_S1;
999 else
1000 *data &= ~PORT_MII_1000MBIT_S1;
1001 }
1002}
1003
1004static int ksz9477_get_xmii(struct ksz_device *dev, u8 data)
1005{
1006 int mode;
1007
1008 if (dev->features & NEW_XMII) {
1009 switch (data & PORT_MII_SEL_M) {
1010 case PORT_MII_SEL:
1011 mode = 0;
1012 break;
1013 case PORT_RMII_SEL:
1014 mode = 1;
1015 break;
1016 case PORT_GMII_SEL:
1017 mode = 2;
1018 break;
1019 default:
1020 mode = 3;
1021 }
1022 } else {
1023 switch (data & PORT_MII_SEL_M) {
1024 case PORT_MII_SEL_S1:
1025 mode = 0;
1026 break;
1027 case PORT_RMII_SEL_S1:
1028 mode = 1;
1029 break;
1030 case PORT_GMII_SEL_S1:
1031 mode = 2;
1032 break;
1033 default:
1034 mode = 3;
1035 }
1036 }
1037 return mode;
1038}
1039
1040static void ksz9477_set_xmii(struct ksz_device *dev, int mode, u8 *data)
1041{
1042 u8 xmii;
1043
1044 if (dev->features & NEW_XMII) {
1045 switch (mode) {
1046 case 0:
1047 xmii = PORT_MII_SEL;
1048 break;
1049 case 1:
1050 xmii = PORT_RMII_SEL;
1051 break;
1052 case 2:
1053 xmii = PORT_GMII_SEL;
1054 break;
1055 default:
1056 xmii = PORT_RGMII_SEL;
1057 break;
1058 }
1059 } else {
1060 switch (mode) {
1061 case 0:
1062 xmii = PORT_MII_SEL_S1;
1063 break;
1064 case 1:
1065 xmii = PORT_RMII_SEL_S1;
1066 break;
1067 case 2:
1068 xmii = PORT_GMII_SEL_S1;
1069 break;
1070 default:
1071 xmii = PORT_RGMII_SEL_S1;
1072 break;
1073 }
1074 }
1075 *data &= ~PORT_MII_SEL_M;
1076 *data |= xmii;
1077}
1078
1079static phy_interface_t ksz9477_get_interface(struct ksz_device *dev, int port)
1080{
1081 phy_interface_t interface;
1082 bool gbit;
1083 int mode;
1084 u8 data8;
1085
1086 if (port < dev->phy_port_cnt)
1087 return PHY_INTERFACE_MODE_NA;
1088 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1089 gbit = ksz9477_get_gbit(dev, data8);
1090 mode = ksz9477_get_xmii(dev, data8);
1091 switch (mode) {
1092 case 2:
1093 interface = PHY_INTERFACE_MODE_GMII;
1094 if (gbit)
1095 break;
1096 /* fall through */
1097 case 0:
1098 interface = PHY_INTERFACE_MODE_MII;
1099 break;
1100 case 1:
1101 interface = PHY_INTERFACE_MODE_RMII;
1102 break;
1103 default:
1104 interface = PHY_INTERFACE_MODE_RGMII;
1105 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1106 interface = PHY_INTERFACE_MODE_RGMII_TXID;
1107 if (data8 & PORT_RGMII_ID_IG_ENABLE) {
1108 interface = PHY_INTERFACE_MODE_RGMII_RXID;
1109 if (data8 & PORT_RGMII_ID_EG_ENABLE)
1110 interface = PHY_INTERFACE_MODE_RGMII_ID;
1111 }
1112 break;
1113 }
1114 return interface;
1115}
1116
1117static void ksz9477_port_mmd_write(struct ksz_device *dev, int port,
1118 u8 dev_addr, u16 reg_addr, u16 val)
1119{
1120 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1121 MMD_SETUP(PORT_MMD_OP_INDEX, dev_addr));
1122 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, reg_addr);
1123 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_SETUP,
1124 MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR, dev_addr));
1125 ksz_pwrite16(dev, port, REG_PORT_PHY_MMD_INDEX_DATA, val);
1126}
1127
1128static void ksz9477_phy_errata_setup(struct ksz_device *dev, int port)
1129{
1130 /* Apply PHY settings to address errata listed in
1131 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1132 * Silicon Errata and Data Sheet Clarification documents:
1133 *
1134 * Register settings are needed to improve PHY receive performance
1135 */
1136 ksz9477_port_mmd_write(dev, port, 0x01, 0x6f, 0xdd0b);
1137 ksz9477_port_mmd_write(dev, port, 0x01, 0x8f, 0x6032);
1138 ksz9477_port_mmd_write(dev, port, 0x01, 0x9d, 0x248c);
1139 ksz9477_port_mmd_write(dev, port, 0x01, 0x75, 0x0060);
1140 ksz9477_port_mmd_write(dev, port, 0x01, 0xd3, 0x7777);
1141 ksz9477_port_mmd_write(dev, port, 0x1c, 0x06, 0x3008);
1142 ksz9477_port_mmd_write(dev, port, 0x1c, 0x08, 0x2001);
1143
1144 /* Transmit waveform amplitude can be improved
1145 * (1000BASE-T, 100BASE-TX, 10BASE-Te)
1146 */
1147 ksz9477_port_mmd_write(dev, port, 0x1c, 0x04, 0x00d0);
1148
1149 /* Energy Efficient Ethernet (EEE) feature select must
1150 * be manually disabled (except on KSZ8565 which is 100Mbit)
1151 */
1152 if (dev->features & GBIT_SUPPORT)
1153 ksz9477_port_mmd_write(dev, port, 0x07, 0x3c, 0x0000);
1154
1155 /* Register settings are required to meet data sheet
1156 * supply current specifications
1157 */
1158 ksz9477_port_mmd_write(dev, port, 0x1c, 0x13, 0x6eff);
1159 ksz9477_port_mmd_write(dev, port, 0x1c, 0x14, 0xe6ff);
1160 ksz9477_port_mmd_write(dev, port, 0x1c, 0x15, 0x6eff);
1161 ksz9477_port_mmd_write(dev, port, 0x1c, 0x16, 0xe6ff);
1162 ksz9477_port_mmd_write(dev, port, 0x1c, 0x17, 0x00ff);
1163 ksz9477_port_mmd_write(dev, port, 0x1c, 0x18, 0x43ff);
1164 ksz9477_port_mmd_write(dev, port, 0x1c, 0x19, 0xc3ff);
1165 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1a, 0x6fff);
1166 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1b, 0x07ff);
1167 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1c, 0x0fff);
1168 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1d, 0xe7ff);
1169 ksz9477_port_mmd_write(dev, port, 0x1c, 0x1e, 0xefff);
1170 ksz9477_port_mmd_write(dev, port, 0x1c, 0x20, 0xeeee);
1171}
1172
1173static void ksz9477_port_setup(struct ksz_device *dev, int port, bool cpu_port)
1174{
1175 u8 data8;
1176 u8 member;
1177 u16 data16;
1178 struct ksz_port *p = &dev->ports[port];
1179
1180 /* enable tag tail for host port */
1181 if (cpu_port)
1182 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_TAIL_TAG_ENABLE,
1183 true);
1184
1185 ksz_port_cfg(dev, port, REG_PORT_CTRL_0, PORT_MAC_LOOPBACK, false);
1186
1187 /* set back pressure */
1188 ksz_port_cfg(dev, port, REG_PORT_MAC_CTRL_1, PORT_BACK_PRESSURE, true);
1189
1190 /* enable broadcast storm limit */
1191 ksz_port_cfg(dev, port, P_BCAST_STORM_CTRL, PORT_BROADCAST_STORM, true);
1192
1193 /* disable DiffServ priority */
1194 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_DIFFSERV_PRIO_ENABLE, false);
1195
1196 /* replace priority */
1197 ksz_port_cfg(dev, port, REG_PORT_MRI_MAC_CTRL, PORT_USER_PRIO_CEILING,
1198 false);
1199 ksz9477_port_cfg32(dev, port, REG_PORT_MTI_QUEUE_CTRL_0__4,
1200 MTI_PVID_REPLACE, false);
1201
1202 /* enable 802.1p priority */
1203 ksz_port_cfg(dev, port, P_PRIO_CTRL, PORT_802_1P_PRIO_ENABLE, true);
1204
1205 if (port < dev->phy_port_cnt) {
1206 /* do not force flow control */
1207 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1208 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1209 false);
1210
1211 if (dev->phy_errata_9477)
1212 ksz9477_phy_errata_setup(dev, port);
1213 } else {
1214 /* force flow control */
1215 ksz_port_cfg(dev, port, REG_PORT_CTRL_0,
1216 PORT_FORCE_TX_FLOW_CTRL | PORT_FORCE_RX_FLOW_CTRL,
1217 true);
1218
1219 /* configure MAC to 1G & RGMII mode */
1220 ksz_pread8(dev, port, REG_PORT_XMII_CTRL_1, &data8);
1221 switch (dev->interface) {
1222 case PHY_INTERFACE_MODE_MII:
1223 ksz9477_set_xmii(dev, 0, &data8);
1224 ksz9477_set_gbit(dev, false, &data8);
1225 p->phydev.speed = SPEED_100;
1226 break;
1227 case PHY_INTERFACE_MODE_RMII:
1228 ksz9477_set_xmii(dev, 1, &data8);
1229 ksz9477_set_gbit(dev, false, &data8);
1230 p->phydev.speed = SPEED_100;
1231 break;
1232 case PHY_INTERFACE_MODE_GMII:
1233 ksz9477_set_xmii(dev, 2, &data8);
1234 ksz9477_set_gbit(dev, true, &data8);
1235 p->phydev.speed = SPEED_1000;
1236 break;
1237 default:
1238 ksz9477_set_xmii(dev, 3, &data8);
1239 ksz9477_set_gbit(dev, true, &data8);
1240 data8 &= ~PORT_RGMII_ID_IG_ENABLE;
1241 data8 &= ~PORT_RGMII_ID_EG_ENABLE;
1242 if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1243 dev->interface == PHY_INTERFACE_MODE_RGMII_RXID)
1244 data8 |= PORT_RGMII_ID_IG_ENABLE;
1245 if (dev->interface == PHY_INTERFACE_MODE_RGMII_ID ||
1246 dev->interface == PHY_INTERFACE_MODE_RGMII_TXID)
1247 data8 |= PORT_RGMII_ID_EG_ENABLE;
1248 p->phydev.speed = SPEED_1000;
1249 break;
1250 }
1251 ksz_pwrite8(dev, port, REG_PORT_XMII_CTRL_1, data8);
1252 p->phydev.duplex = 1;
1253 }
1254 mutex_lock(&dev->dev_mutex);
1255 if (cpu_port) {
1256 member = dev->port_mask;
1257 dev->on_ports = dev->host_mask;
1258 dev->live_ports = dev->host_mask;
1259 } else {
1260 member = dev->host_mask | p->vid_member;
1261 dev->on_ports |= (1 << port);
1262
1263 /* Link was detected before port is enabled. */
1264 if (p->phydev.link)
1265 dev->live_ports |= (1 << port);
1266 }
1267 mutex_unlock(&dev->dev_mutex);
1268 ksz9477_cfg_port_member(dev, port, member);
1269
1270 /* clear pending interrupts */
1271 if (port < dev->phy_port_cnt)
1272 ksz_pread16(dev, port, REG_PORT_PHY_INT_ENABLE, &data16);
1273}
1274
1275static void ksz9477_config_cpu_port(struct dsa_switch *ds)
1276{
1277 struct ksz_device *dev = ds->priv;
1278 struct ksz_port *p;
1279 int i;
1280
1281 ds->num_ports = dev->port_cnt;
1282
1283 for (i = 0; i < dev->port_cnt; i++) {
1284 if (dsa_is_cpu_port(ds, i) && (dev->cpu_ports & (1 << i))) {
1285 phy_interface_t interface;
1286
1287 dev->cpu_port = i;
1288 dev->host_mask = (1 << dev->cpu_port);
1289 dev->port_mask |= dev->host_mask;
1290
1291 /* Read from XMII register to determine host port
1292 * interface. If set specifically in device tree
1293 * note the difference to help debugging.
1294 */
1295 interface = ksz9477_get_interface(dev, i);
1296 if (!dev->interface)
1297 dev->interface = interface;
1298 if (interface && interface != dev->interface)
1299 dev_info(dev->dev,
1300 "use %s instead of %s\n",
1301 phy_modes(dev->interface),
1302 phy_modes(interface));
1303
1304 /* enable cpu port */
1305 ksz9477_port_setup(dev, i, true);
1306 p = &dev->ports[dev->cpu_port];
1307 p->vid_member = dev->port_mask;
1308 p->on = 1;
1309 }
1310 }
1311
1312 dev->member = dev->host_mask;
1313
1314 for (i = 0; i < dev->mib_port_cnt; i++) {
1315 if (i == dev->cpu_port)
1316 continue;
1317 p = &dev->ports[i];
1318
1319 /* Initialize to non-zero so that ksz_cfg_port_member() will
1320 * be called.
1321 */
1322 p->vid_member = (1 << i);
1323 p->member = dev->port_mask;
1324 ksz9477_port_stp_state_set(ds, i, BR_STATE_DISABLED);
1325 p->on = 1;
1326 if (i < dev->phy_port_cnt)
1327 p->phy = 1;
1328 if (dev->chip_id == 0x00947700 && i == 6) {
1329 p->sgmii = 1;
1330
1331 /* SGMII PHY detection code is not implemented yet. */
1332 p->phy = 0;
1333 }
1334 }
1335}
1336
1337static int ksz9477_setup(struct dsa_switch *ds)
1338{
1339 struct ksz_device *dev = ds->priv;
1340 int ret = 0;
1341
1342 dev->vlan_cache = devm_kcalloc(dev->dev, sizeof(struct vlan_table),
1343 dev->num_vlans, GFP_KERNEL);
1344 if (!dev->vlan_cache)
1345 return -ENOMEM;
1346
1347 ret = ksz9477_reset_switch(dev);
1348 if (ret) {
1349 dev_err(ds->dev, "failed to reset switch\n");
1350 return ret;
1351 }
1352
1353 /* Required for port partitioning. */
1354 ksz9477_cfg32(dev, REG_SW_QM_CTRL__4, UNICAST_VLAN_BOUNDARY,
1355 true);
1356
1357 /* Do not work correctly with tail tagging. */
1358 ksz_cfg(dev, REG_SW_MAC_CTRL_0, SW_CHECK_LENGTH, false);
1359
1360 /* accept packet up to 2000bytes */
1361 ksz_cfg(dev, REG_SW_MAC_CTRL_1, SW_LEGAL_PACKET_DISABLE, true);
1362
1363 ksz9477_config_cpu_port(ds);
1364
1365 ksz_cfg(dev, REG_SW_MAC_CTRL_1, MULTICAST_STORM_DISABLE, true);
1366
1367 /* queue based egress rate limit */
1368 ksz_cfg(dev, REG_SW_MAC_CTRL_5, SW_OUT_RATE_LIMIT_QUEUE_BASED, true);
1369
1370 /* enable global MIB counter freeze function */
1371 ksz_cfg(dev, REG_SW_MAC_CTRL_6, SW_MIB_COUNTER_FREEZE, true);
1372
1373 /* start switch */
1374 ksz_cfg(dev, REG_SW_OPERATION, SW_START, true);
1375
1376 ksz_init_mib_timer(dev);
1377
1378 return 0;
1379}
1380
1381static const struct dsa_switch_ops ksz9477_switch_ops = {
1382 .get_tag_protocol = ksz9477_get_tag_protocol,
1383 .setup = ksz9477_setup,
1384 .phy_read = ksz9477_phy_read16,
1385 .phy_write = ksz9477_phy_write16,
1386 .adjust_link = ksz_adjust_link,
1387 .port_enable = ksz_enable_port,
1388 .port_disable = ksz_disable_port,
1389 .get_strings = ksz9477_get_strings,
1390 .get_ethtool_stats = ksz_get_ethtool_stats,
1391 .get_sset_count = ksz_sset_count,
1392 .port_bridge_join = ksz_port_bridge_join,
1393 .port_bridge_leave = ksz_port_bridge_leave,
1394 .port_stp_state_set = ksz9477_port_stp_state_set,
1395 .port_fast_age = ksz_port_fast_age,
1396 .port_vlan_filtering = ksz9477_port_vlan_filtering,
1397 .port_vlan_prepare = ksz_port_vlan_prepare,
1398 .port_vlan_add = ksz9477_port_vlan_add,
1399 .port_vlan_del = ksz9477_port_vlan_del,
1400 .port_fdb_dump = ksz9477_port_fdb_dump,
1401 .port_fdb_add = ksz9477_port_fdb_add,
1402 .port_fdb_del = ksz9477_port_fdb_del,
1403 .port_mdb_prepare = ksz_port_mdb_prepare,
1404 .port_mdb_add = ksz9477_port_mdb_add,
1405 .port_mdb_del = ksz9477_port_mdb_del,
1406 .port_mirror_add = ksz9477_port_mirror_add,
1407 .port_mirror_del = ksz9477_port_mirror_del,
1408};
1409
1410static u32 ksz9477_get_port_addr(int port, int offset)
1411{
1412 return PORT_CTRL_ADDR(port, offset);
1413}
1414
1415static int ksz9477_switch_detect(struct ksz_device *dev)
1416{
1417 u8 data8;
1418 u8 id_hi;
1419 u8 id_lo;
1420 u32 id32;
1421 int ret;
1422
1423 /* turn off SPI DO Edge select */
1424 ret = ksz_read8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, &data8);
1425 if (ret)
1426 return ret;
1427
1428 data8 &= ~SPI_AUTO_EDGE_DETECTION;
1429 ret = ksz_write8(dev, REG_SW_GLOBAL_SERIAL_CTRL_0, data8);
1430 if (ret)
1431 return ret;
1432
1433 /* read chip id */
1434 ret = ksz_read32(dev, REG_CHIP_ID0__1, &id32);
1435 if (ret)
1436 return ret;
1437 ret = ksz_read8(dev, REG_GLOBAL_OPTIONS, &data8);
1438 if (ret)
1439 return ret;
1440
1441 /* Number of ports can be reduced depending on chip. */
1442 dev->mib_port_cnt = TOTAL_PORT_NUM;
1443 dev->phy_port_cnt = 5;
1444
1445 /* Default capability is gigabit capable. */
1446 dev->features = GBIT_SUPPORT;
1447
1448 id_hi = (u8)(id32 >> 16);
1449 id_lo = (u8)(id32 >> 8);
1450 if ((id_lo & 0xf) == 3) {
1451 /* Chip is from KSZ9893 design. */
1452 dev->features |= IS_9893;
1453
1454 /* Chip does not support gigabit. */
1455 if (data8 & SW_QW_ABLE)
1456 dev->features &= ~GBIT_SUPPORT;
1457 dev->mib_port_cnt = 3;
1458 dev->phy_port_cnt = 2;
1459 } else {
1460 /* Chip uses new XMII register definitions. */
1461 dev->features |= NEW_XMII;
1462
1463 /* Chip does not support gigabit. */
1464 if (!(data8 & SW_GIGABIT_ABLE))
1465 dev->features &= ~GBIT_SUPPORT;
1466 }
1467
1468 /* Change chip id to known ones so it can be matched against them. */
1469 id32 = (id_hi << 16) | (id_lo << 8);
1470
1471 dev->chip_id = id32;
1472
1473 return 0;
1474}
1475
1476struct ksz_chip_data {
1477 u32 chip_id;
1478 const char *dev_name;
1479 int num_vlans;
1480 int num_alus;
1481 int num_statics;
1482 int cpu_ports;
1483 int port_cnt;
1484 bool phy_errata_9477;
1485};
1486
1487static const struct ksz_chip_data ksz9477_switch_chips[] = {
1488 {
1489 .chip_id = 0x00947700,
1490 .dev_name = "KSZ9477",
1491 .num_vlans = 4096,
1492 .num_alus = 4096,
1493 .num_statics = 16,
1494 .cpu_ports = 0x7F, /* can be configured as cpu port */
1495 .port_cnt = 7, /* total physical port count */
1496 .phy_errata_9477 = true,
1497 },
1498 {
1499 .chip_id = 0x00989700,
1500 .dev_name = "KSZ9897",
1501 .num_vlans = 4096,
1502 .num_alus = 4096,
1503 .num_statics = 16,
1504 .cpu_ports = 0x7F, /* can be configured as cpu port */
1505 .port_cnt = 7, /* total physical port count */
1506 .phy_errata_9477 = true,
1507 },
1508 {
1509 .chip_id = 0x00989300,
1510 .dev_name = "KSZ9893",
1511 .num_vlans = 4096,
1512 .num_alus = 4096,
1513 .num_statics = 16,
1514 .cpu_ports = 0x07, /* can be configured as cpu port */
1515 .port_cnt = 3, /* total port count */
1516 },
1517 {
1518 .chip_id = 0x00956700,
1519 .dev_name = "KSZ9567",
1520 .num_vlans = 4096,
1521 .num_alus = 4096,
1522 .num_statics = 16,
1523 .cpu_ports = 0x7F, /* can be configured as cpu port */
1524 .port_cnt = 7, /* total physical port count */
1525 .phy_errata_9477 = true,
1526 },
1527};
1528
1529static int ksz9477_switch_init(struct ksz_device *dev)
1530{
1531 int i;
1532
1533 dev->ds->ops = &ksz9477_switch_ops;
1534
1535 for (i = 0; i < ARRAY_SIZE(ksz9477_switch_chips); i++) {
1536 const struct ksz_chip_data *chip = &ksz9477_switch_chips[i];
1537
1538 if (dev->chip_id == chip->chip_id) {
1539 dev->name = chip->dev_name;
1540 dev->num_vlans = chip->num_vlans;
1541 dev->num_alus = chip->num_alus;
1542 dev->num_statics = chip->num_statics;
1543 dev->port_cnt = chip->port_cnt;
1544 dev->cpu_ports = chip->cpu_ports;
1545 dev->phy_errata_9477 = chip->phy_errata_9477;
1546
1547 break;
1548 }
1549 }
1550
1551 /* no switch found */
1552 if (!dev->port_cnt)
1553 return -ENODEV;
1554
1555 dev->port_mask = (1 << dev->port_cnt) - 1;
1556
1557 dev->reg_mib_cnt = SWITCH_COUNTER_NUM;
1558 dev->mib_cnt = TOTAL_SWITCH_COUNTER_NUM;
1559
1560 i = dev->mib_port_cnt;
1561 dev->ports = devm_kzalloc(dev->dev, sizeof(struct ksz_port) * i,
1562 GFP_KERNEL);
1563 if (!dev->ports)
1564 return -ENOMEM;
1565 for (i = 0; i < dev->mib_port_cnt; i++) {
1566 mutex_init(&dev->ports[i].mib.cnt_mutex);
1567 dev->ports[i].mib.counters =
1568 devm_kzalloc(dev->dev,
1569 sizeof(u64) *
1570 (TOTAL_SWITCH_COUNTER_NUM + 1),
1571 GFP_KERNEL);
1572 if (!dev->ports[i].mib.counters)
1573 return -ENOMEM;
1574 }
1575
1576 /* set the real number of ports */
1577 dev->ds->num_ports = dev->port_cnt;
1578
1579 return 0;
1580}
1581
1582static void ksz9477_switch_exit(struct ksz_device *dev)
1583{
1584 ksz9477_reset_switch(dev);
1585}
1586
1587static const struct ksz_dev_ops ksz9477_dev_ops = {
1588 .get_port_addr = ksz9477_get_port_addr,
1589 .cfg_port_member = ksz9477_cfg_port_member,
1590 .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table,
1591 .port_setup = ksz9477_port_setup,
1592 .r_mib_cnt = ksz9477_r_mib_cnt,
1593 .r_mib_pkt = ksz9477_r_mib_pkt,
1594 .freeze_mib = ksz9477_freeze_mib,
1595 .port_init_cnt = ksz9477_port_init_cnt,
1596 .shutdown = ksz9477_reset_switch,
1597 .detect = ksz9477_switch_detect,
1598 .init = ksz9477_switch_init,
1599 .exit = ksz9477_switch_exit,
1600};
1601
1602int ksz9477_switch_register(struct ksz_device *dev)
1603{
1604 int ret, i;
1605 struct phy_device *phydev;
1606
1607 ret = ksz_switch_register(dev, &ksz9477_dev_ops);
1608 if (ret)
1609 return ret;
1610
1611 for (i = 0; i < dev->phy_port_cnt; ++i) {
1612 if (!dsa_is_user_port(dev->ds, i))
1613 continue;
1614
1615 phydev = dsa_to_port(dev->ds, i)->slave->phydev;
1616
1617 /* The MAC actually cannot run in 1000 half-duplex mode. */
1618 phy_remove_link_mode(phydev,
1619 ETHTOOL_LINK_MODE_1000baseT_Half_BIT);
1620
1621 /* PHY does not support gigabit. */
1622 if (!(dev->features & GBIT_SUPPORT))
1623 phy_remove_link_mode(phydev,
1624 ETHTOOL_LINK_MODE_1000baseT_Full_BIT);
1625 }
1626 return ret;
1627}
1628EXPORT_SYMBOL(ksz9477_switch_register);
1629
1630MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1631MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
1632MODULE_LICENSE("GPL");