1 // SPDX-License-Identifier: GPL-2.0
3 * Microchip KSZ9477 switch driver main logic
5 * Copyright (C) 2017-2019 Microchip Technology Inc.
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>
15 #include <net/switchdev.h>
17 #include "ksz9477_reg.h"
18 #include "ksz_common.h"
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)
27 char string
[ETH_GSTRING_LEN
];
28 } ksz9477_mib_names
[TOTAL_SWITCH_COUNTER_NUM
] = {
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" },
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" },
51 { 0x16, "tx_late_col" },
56 { 0x1B, "tx_deferred" },
57 { 0x1C, "tx_total_col" },
58 { 0x1D, "tx_exc_col" },
59 { 0x1E, "tx_single_col" },
60 { 0x1F, "tx_mult_col" },
63 { 0x82, "rx_discards" },
64 { 0x83, "tx_discards" },
67 static void ksz_cfg(struct ksz_device
*dev
, u32 addr
, u8 bits
, bool set
)
69 regmap_update_bits(dev
->regmap
[0], addr
, bits
, set
? bits
: 0);
72 static void ksz_port_cfg(struct ksz_device
*dev
, int port
, int offset
, u8 bits
,
75 regmap_update_bits(dev
->regmap
[0], PORT_CTRL_ADDR(port
, offset
),
76 bits
, set
? bits
: 0);
79 static void ksz9477_cfg32(struct ksz_device
*dev
, u32 addr
, u32 bits
, bool set
)
81 regmap_update_bits(dev
->regmap
[2], addr
, bits
, set
? bits
: 0);
84 static void ksz9477_port_cfg32(struct ksz_device
*dev
, int port
, int offset
,
87 regmap_update_bits(dev
->regmap
[2], PORT_CTRL_ADDR(port
, offset
),
88 bits
, set
? bits
: 0);
91 static int ksz9477_wait_vlan_ctrl_ready(struct ksz_device
*dev
)
95 return regmap_read_poll_timeout(dev
->regmap
[0], REG_SW_VLAN_CTRL
,
96 val
, !(val
& VLAN_START
), 10, 1000);
99 static int ksz9477_get_vlan_table(struct ksz_device
*dev
, u16 vid
,
104 mutex_lock(&dev
->vlan_mutex
);
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
);
109 /* wait to be cleared */
110 ret
= ksz9477_wait_vlan_ctrl_ready(dev
);
112 dev_dbg(dev
->dev
, "Failed to read vlan table\n");
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]);
120 ksz_write8(dev
, REG_SW_VLAN_CTRL
, 0);
123 mutex_unlock(&dev
->vlan_mutex
);
128 static int ksz9477_set_vlan_table(struct ksz_device
*dev
, u16 vid
,
133 mutex_lock(&dev
->vlan_mutex
);
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]);
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
);
142 /* wait to be cleared */
143 ret
= ksz9477_wait_vlan_ctrl_ready(dev
);
145 dev_dbg(dev
->dev
, "Failed to write vlan table\n");
149 ksz_write8(dev
, REG_SW_VLAN_CTRL
, 0);
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];
157 mutex_unlock(&dev
->vlan_mutex
);
162 static void ksz9477_read_table(struct ksz_device
*dev
, u32
*table
)
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]);
170 static void ksz9477_write_table(struct ksz_device
*dev
, u32
*table
)
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]);
178 static int ksz9477_wait_alu_ready(struct ksz_device
*dev
)
182 return regmap_read_poll_timeout(dev
->regmap
[2], REG_SW_ALU_CTRL__4
,
183 val
, !(val
& ALU_START
), 10, 1000);
186 static int ksz9477_wait_alu_sta_ready(struct ksz_device
*dev
)
190 return regmap_read_poll_timeout(dev
->regmap
[2],
191 REG_SW_ALU_STAT_CTRL__4
,
192 val
, !(val
& ALU_STAT_START
),
196 static int ksz9477_reset_switch(struct ksz_device
*dev
)
202 ksz_cfg(dev
, REG_SW_OPERATION
, SW_RESET
, true);
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);
208 /* default configuration */
209 ksz_read8(dev
, REG_SW_LUE_CTRL_1
, &data8
);
210 data8
= SW_AGING_ENABLE
| SW_LINK_AUTO_AGING
|
211 SW_SRC_ADDR_FILTER
| SW_FLUSH_STP_TABLE
| SW_FLUSH_MSTP_TABLE
;
212 ksz_write8(dev
, REG_SW_LUE_CTRL_1
, data8
);
214 /* disable interrupts */
215 ksz_write32(dev
, REG_SW_INT_MASK__4
, SWITCH_INT_MASK
);
216 ksz_write32(dev
, REG_SW_PORT_INT_MASK__4
, 0x7F);
217 ksz_read32(dev
, REG_SW_PORT_INT_STATUS__4
, &data32
);
219 /* set broadcast storm protection 10% rate */
220 regmap_update_bits(dev
->regmap
[1], REG_SW_MAC_CTRL_2
,
221 BROADCAST_STORM_RATE
,
222 (BROADCAST_STORM_VALUE
*
223 BROADCAST_STORM_PROT_RATE
) / 100);
225 if (dev
->synclko_125
)
226 ksz_write8(dev
, REG_SW_GLOBAL_OUTPUT_CTRL__1
,
227 SW_ENABLE_REFCLKO
| SW_REFCLKO_IS_125MHZ
);
232 static void ksz9477_r_mib_cnt(struct ksz_device
*dev
, int port
, u16 addr
,
235 struct ksz_port
*p
= &dev
->ports
[port
];
240 /* retain the flush/freeze bit */
241 data
= p
->freeze
? MIB_COUNTER_FLUSH_FREEZE
: 0;
242 data
|= MIB_COUNTER_READ
;
243 data
|= (addr
<< MIB_COUNTER_INDEX_S
);
244 ksz_pwrite32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
, data
);
246 ret
= regmap_read_poll_timeout(dev
->regmap
[2],
247 PORT_CTRL_ADDR(port
, REG_PORT_MIB_CTRL_STAT__4
),
248 val
, !(val
& MIB_COUNTER_READ
), 10, 1000);
249 /* failed to read MIB. get out of loop */
251 dev_dbg(dev
->dev
, "Failed to get MIB\n");
255 /* count resets upon read */
256 ksz_pread32(dev
, port
, REG_PORT_MIB_DATA
, &data
);
260 static void ksz9477_r_mib_pkt(struct ksz_device
*dev
, int port
, u16 addr
,
261 u64
*dropped
, u64
*cnt
)
263 addr
= ksz9477_mib_names
[addr
].index
;
264 ksz9477_r_mib_cnt(dev
, port
, addr
, cnt
);
267 static void ksz9477_freeze_mib(struct ksz_device
*dev
, int port
, bool freeze
)
269 u32 val
= freeze
? MIB_COUNTER_FLUSH_FREEZE
: 0;
270 struct ksz_port
*p
= &dev
->ports
[port
];
272 /* enable/disable the port for flush/freeze function */
273 mutex_lock(&p
->mib
.cnt_mutex
);
274 ksz_pwrite32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
, val
);
276 /* used by MIB counter reading code to know freeze is enabled */
278 mutex_unlock(&p
->mib
.cnt_mutex
);
281 static void ksz9477_port_init_cnt(struct ksz_device
*dev
, int port
)
283 struct ksz_port_mib
*mib
= &dev
->ports
[port
].mib
;
285 /* flush all enabled port MIB counters */
286 mutex_lock(&mib
->cnt_mutex
);
287 ksz_pwrite32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
,
288 MIB_COUNTER_FLUSH_FREEZE
);
289 ksz_write8(dev
, REG_SW_MAC_CTRL_6
, SW_MIB_COUNTER_FLUSH
);
290 ksz_pwrite32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
, 0);
291 mutex_unlock(&mib
->cnt_mutex
);
294 memset(mib
->counters
, 0, dev
->mib_cnt
* sizeof(u64
));
297 static enum dsa_tag_protocol
ksz9477_get_tag_protocol(struct dsa_switch
*ds
,
299 enum dsa_tag_protocol mp
)
301 enum dsa_tag_protocol proto
= DSA_TAG_PROTO_KSZ9477
;
302 struct ksz_device
*dev
= ds
->priv
;
304 if (dev
->features
& IS_9893
)
305 proto
= DSA_TAG_PROTO_KSZ9893
;
309 static int ksz9477_phy_read16(struct dsa_switch
*ds
, int addr
, int reg
)
311 struct ksz_device
*dev
= ds
->priv
;
314 /* No real PHY after this. Simulate the PHY.
315 * A fixed PHY can be setup in the device tree, but this function is
316 * still called for that port during initialization.
317 * For RGMII PHY there is no way to access it so the fixed PHY should
318 * be used. For SGMII PHY the supporting code will be added later.
320 if (addr
>= dev
->phy_port_cnt
) {
321 struct ksz_port
*p
= &dev
->ports
[addr
];
346 if (p
->phydev
.speed
== SPEED_1000
)
353 ksz_pread16(dev
, addr
, 0x100 + (reg
<< 1), &val
);
359 static int ksz9477_phy_write16(struct dsa_switch
*ds
, int addr
, int reg
,
362 struct ksz_device
*dev
= ds
->priv
;
364 /* No real PHY after this. */
365 if (addr
>= dev
->phy_port_cnt
)
368 /* No gigabit support. Do not write to this register. */
369 if (!(dev
->features
& GBIT_SUPPORT
) && reg
== MII_CTRL1000
)
371 ksz_pwrite16(dev
, addr
, 0x100 + (reg
<< 1), val
);
376 static void ksz9477_get_strings(struct dsa_switch
*ds
, int port
,
377 u32 stringset
, uint8_t *buf
)
381 if (stringset
!= ETH_SS_STATS
)
384 for (i
= 0; i
< TOTAL_SWITCH_COUNTER_NUM
; i
++) {
385 memcpy(buf
+ i
* ETH_GSTRING_LEN
, ksz9477_mib_names
[i
].string
,
390 static void ksz9477_cfg_port_member(struct ksz_device
*dev
, int port
,
393 ksz_pwrite32(dev
, port
, REG_PORT_VLAN_MEMBERSHIP__4
, member
);
394 dev
->ports
[port
].member
= member
;
397 static void ksz9477_port_stp_state_set(struct dsa_switch
*ds
, int port
,
400 struct ksz_device
*dev
= ds
->priv
;
401 struct ksz_port
*p
= &dev
->ports
[port
];
404 int forward
= dev
->member
;
406 ksz_pread8(dev
, port
, P_STP_CTRL
, &data
);
407 data
&= ~(PORT_TX_ENABLE
| PORT_RX_ENABLE
| PORT_LEARN_DISABLE
);
410 case BR_STATE_DISABLED
:
411 data
|= PORT_LEARN_DISABLE
;
412 if (port
!= dev
->cpu_port
)
415 case BR_STATE_LISTENING
:
416 data
|= (PORT_RX_ENABLE
| PORT_LEARN_DISABLE
);
417 if (port
!= dev
->cpu_port
&&
418 p
->stp_state
== BR_STATE_DISABLED
)
419 member
= dev
->host_mask
| p
->vid_member
;
421 case BR_STATE_LEARNING
:
422 data
|= PORT_RX_ENABLE
;
424 case BR_STATE_FORWARDING
:
425 data
|= (PORT_TX_ENABLE
| PORT_RX_ENABLE
);
427 /* This function is also used internally. */
428 if (port
== dev
->cpu_port
)
431 member
= dev
->host_mask
| p
->vid_member
;
432 mutex_lock(&dev
->dev_mutex
);
434 /* Port is a member of a bridge. */
435 if (dev
->br_member
& (1 << port
)) {
436 dev
->member
|= (1 << port
);
437 member
= dev
->member
;
439 mutex_unlock(&dev
->dev_mutex
);
441 case BR_STATE_BLOCKING
:
442 data
|= PORT_LEARN_DISABLE
;
443 if (port
!= dev
->cpu_port
&&
444 p
->stp_state
== BR_STATE_DISABLED
)
445 member
= dev
->host_mask
| p
->vid_member
;
448 dev_err(ds
->dev
, "invalid STP state: %d\n", state
);
452 ksz_pwrite8(dev
, port
, P_STP_CTRL
, data
);
453 p
->stp_state
= state
;
454 mutex_lock(&dev
->dev_mutex
);
455 /* Port membership may share register with STP state. */
456 if (member
>= 0 && member
!= p
->member
)
457 ksz9477_cfg_port_member(dev
, port
, (u8
)member
);
459 /* Check if forwarding needs to be updated. */
460 if (state
!= BR_STATE_FORWARDING
) {
461 if (dev
->br_member
& (1 << port
))
462 dev
->member
&= ~(1 << port
);
465 /* When topology has changed the function ksz_update_port_member
466 * should be called to modify port forwarding behavior.
468 if (forward
!= dev
->member
)
469 ksz_update_port_member(dev
, port
);
470 mutex_unlock(&dev
->dev_mutex
);
473 static void ksz9477_flush_dyn_mac_table(struct ksz_device
*dev
, int port
)
477 regmap_update_bits(dev
->regmap
[0], REG_SW_LUE_CTRL_2
,
478 SW_FLUSH_OPTION_M
<< SW_FLUSH_OPTION_S
,
479 SW_FLUSH_OPTION_DYN_MAC
<< SW_FLUSH_OPTION_S
);
481 if (port
< dev
->mib_port_cnt
) {
482 /* flush individual port */
483 ksz_pread8(dev
, port
, P_STP_CTRL
, &data
);
484 if (!(data
& PORT_LEARN_DISABLE
))
485 ksz_pwrite8(dev
, port
, P_STP_CTRL
,
486 data
| PORT_LEARN_DISABLE
);
487 ksz_cfg(dev
, S_FLUSH_TABLE_CTRL
, SW_FLUSH_DYN_MAC_TABLE
, true);
488 ksz_pwrite8(dev
, port
, P_STP_CTRL
, data
);
491 ksz_cfg(dev
, S_FLUSH_TABLE_CTRL
, SW_FLUSH_STP_TABLE
, true);
495 static int ksz9477_port_vlan_filtering(struct dsa_switch
*ds
, int port
,
498 struct ksz_device
*dev
= ds
->priv
;
501 ksz_port_cfg(dev
, port
, REG_PORT_LUE_CTRL
,
502 PORT_VLAN_LOOKUP_VID_0
, true);
503 ksz_cfg(dev
, REG_SW_LUE_CTRL_0
, SW_VLAN_ENABLE
, true);
505 ksz_cfg(dev
, REG_SW_LUE_CTRL_0
, SW_VLAN_ENABLE
, false);
506 ksz_port_cfg(dev
, port
, REG_PORT_LUE_CTRL
,
507 PORT_VLAN_LOOKUP_VID_0
, false);
513 static void ksz9477_port_vlan_add(struct dsa_switch
*ds
, int port
,
514 const struct switchdev_obj_port_vlan
*vlan
)
516 struct ksz_device
*dev
= ds
->priv
;
519 bool untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
521 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
522 if (ksz9477_get_vlan_table(dev
, vid
, vlan_table
)) {
523 dev_dbg(dev
->dev
, "Failed to get vlan table\n");
527 vlan_table
[0] = VLAN_VALID
| (vid
& VLAN_FID_M
);
529 vlan_table
[1] |= BIT(port
);
531 vlan_table
[1] &= ~BIT(port
);
532 vlan_table
[1] &= ~(BIT(dev
->cpu_port
));
534 vlan_table
[2] |= BIT(port
) | BIT(dev
->cpu_port
);
536 if (ksz9477_set_vlan_table(dev
, vid
, vlan_table
)) {
537 dev_dbg(dev
->dev
, "Failed to set vlan table\n");
542 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
)
543 ksz_pwrite16(dev
, port
, REG_PORT_DEFAULT_VID
, vid
);
547 static int ksz9477_port_vlan_del(struct dsa_switch
*ds
, int port
,
548 const struct switchdev_obj_port_vlan
*vlan
)
550 struct ksz_device
*dev
= ds
->priv
;
551 bool untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
556 ksz_pread16(dev
, port
, REG_PORT_DEFAULT_VID
, &pvid
);
559 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
560 if (ksz9477_get_vlan_table(dev
, vid
, vlan_table
)) {
561 dev_dbg(dev
->dev
, "Failed to get vlan table\n");
565 vlan_table
[2] &= ~BIT(port
);
571 vlan_table
[1] &= ~BIT(port
);
573 if (ksz9477_set_vlan_table(dev
, vid
, vlan_table
)) {
574 dev_dbg(dev
->dev
, "Failed to set vlan table\n");
579 ksz_pwrite16(dev
, port
, REG_PORT_DEFAULT_VID
, pvid
);
584 static int ksz9477_port_fdb_add(struct dsa_switch
*ds
, int port
,
585 const unsigned char *addr
, u16 vid
)
587 struct ksz_device
*dev
= ds
->priv
;
592 mutex_lock(&dev
->alu_mutex
);
594 /* find any entry with mac & vid */
595 data
= vid
<< ALU_FID_INDEX_S
;
596 data
|= ((addr
[0] << 8) | addr
[1]);
597 ksz_write32(dev
, REG_SW_ALU_INDEX_0
, data
);
599 data
= ((addr
[2] << 24) | (addr
[3] << 16));
600 data
|= ((addr
[4] << 8) | addr
[5]);
601 ksz_write32(dev
, REG_SW_ALU_INDEX_1
, data
);
603 /* start read operation */
604 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_READ
| ALU_START
);
606 /* wait to be finished */
607 ret
= ksz9477_wait_alu_ready(dev
);
609 dev_dbg(dev
->dev
, "Failed to read ALU\n");
614 ksz9477_read_table(dev
, alu_table
);
616 /* update ALU entry */
617 alu_table
[0] = ALU_V_STATIC_VALID
;
618 alu_table
[1] |= BIT(port
);
620 alu_table
[1] |= ALU_V_USE_FID
;
621 alu_table
[2] = (vid
<< ALU_V_FID_S
);
622 alu_table
[2] |= ((addr
[0] << 8) | addr
[1]);
623 alu_table
[3] = ((addr
[2] << 24) | (addr
[3] << 16));
624 alu_table
[3] |= ((addr
[4] << 8) | addr
[5]);
626 ksz9477_write_table(dev
, alu_table
);
628 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_WRITE
| ALU_START
);
630 /* wait to be finished */
631 ret
= ksz9477_wait_alu_ready(dev
);
633 dev_dbg(dev
->dev
, "Failed to write ALU\n");
636 mutex_unlock(&dev
->alu_mutex
);
641 static int ksz9477_port_fdb_del(struct dsa_switch
*ds
, int port
,
642 const unsigned char *addr
, u16 vid
)
644 struct ksz_device
*dev
= ds
->priv
;
649 mutex_lock(&dev
->alu_mutex
);
651 /* read any entry with mac & vid */
652 data
= vid
<< ALU_FID_INDEX_S
;
653 data
|= ((addr
[0] << 8) | addr
[1]);
654 ksz_write32(dev
, REG_SW_ALU_INDEX_0
, data
);
656 data
= ((addr
[2] << 24) | (addr
[3] << 16));
657 data
|= ((addr
[4] << 8) | addr
[5]);
658 ksz_write32(dev
, REG_SW_ALU_INDEX_1
, data
);
660 /* start read operation */
661 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_READ
| ALU_START
);
663 /* wait to be finished */
664 ret
= ksz9477_wait_alu_ready(dev
);
666 dev_dbg(dev
->dev
, "Failed to read ALU\n");
670 ksz_read32(dev
, REG_SW_ALU_VAL_A
, &alu_table
[0]);
671 if (alu_table
[0] & ALU_V_STATIC_VALID
) {
672 ksz_read32(dev
, REG_SW_ALU_VAL_B
, &alu_table
[1]);
673 ksz_read32(dev
, REG_SW_ALU_VAL_C
, &alu_table
[2]);
674 ksz_read32(dev
, REG_SW_ALU_VAL_D
, &alu_table
[3]);
676 /* clear forwarding port */
677 alu_table
[2] &= ~BIT(port
);
679 /* if there is no port to forward, clear table */
680 if ((alu_table
[2] & ALU_V_PORT_MAP
) == 0) {
693 ksz9477_write_table(dev
, alu_table
);
695 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_WRITE
| ALU_START
);
697 /* wait to be finished */
698 ret
= ksz9477_wait_alu_ready(dev
);
700 dev_dbg(dev
->dev
, "Failed to write ALU\n");
703 mutex_unlock(&dev
->alu_mutex
);
708 static void ksz9477_convert_alu(struct alu_struct
*alu
, u32
*alu_table
)
710 alu
->is_static
= !!(alu_table
[0] & ALU_V_STATIC_VALID
);
711 alu
->is_src_filter
= !!(alu_table
[0] & ALU_V_SRC_FILTER
);
712 alu
->is_dst_filter
= !!(alu_table
[0] & ALU_V_DST_FILTER
);
713 alu
->prio_age
= (alu_table
[0] >> ALU_V_PRIO_AGE_CNT_S
) &
714 ALU_V_PRIO_AGE_CNT_M
;
715 alu
->mstp
= alu_table
[0] & ALU_V_MSTP_M
;
717 alu
->is_override
= !!(alu_table
[1] & ALU_V_OVERRIDE
);
718 alu
->is_use_fid
= !!(alu_table
[1] & ALU_V_USE_FID
);
719 alu
->port_forward
= alu_table
[1] & ALU_V_PORT_MAP
;
721 alu
->fid
= (alu_table
[2] >> ALU_V_FID_S
) & ALU_V_FID_M
;
723 alu
->mac
[0] = (alu_table
[2] >> 8) & 0xFF;
724 alu
->mac
[1] = alu_table
[2] & 0xFF;
725 alu
->mac
[2] = (alu_table
[3] >> 24) & 0xFF;
726 alu
->mac
[3] = (alu_table
[3] >> 16) & 0xFF;
727 alu
->mac
[4] = (alu_table
[3] >> 8) & 0xFF;
728 alu
->mac
[5] = alu_table
[3] & 0xFF;
731 static int ksz9477_port_fdb_dump(struct dsa_switch
*ds
, int port
,
732 dsa_fdb_dump_cb_t
*cb
, void *data
)
734 struct ksz_device
*dev
= ds
->priv
;
738 struct alu_struct alu
;
741 mutex_lock(&dev
->alu_mutex
);
743 /* start ALU search */
744 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_START
| ALU_SEARCH
);
749 ksz_read32(dev
, REG_SW_ALU_CTRL__4
, &ksz_data
);
750 if ((ksz_data
& ALU_VALID
) || !(ksz_data
& ALU_START
))
753 } while (timeout
-- > 0);
756 dev_dbg(dev
->dev
, "Failed to search ALU\n");
762 ksz9477_read_table(dev
, alu_table
);
764 ksz9477_convert_alu(&alu
, alu_table
);
766 if (alu
.port_forward
& BIT(port
)) {
767 ret
= cb(alu
.mac
, alu
.fid
, alu
.is_static
, data
);
771 } while (ksz_data
& ALU_START
);
775 /* stop ALU search */
776 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, 0);
778 mutex_unlock(&dev
->alu_mutex
);
783 static void ksz9477_port_mdb_add(struct dsa_switch
*ds
, int port
,
784 const struct switchdev_obj_port_mdb
*mdb
)
786 struct ksz_device
*dev
= ds
->priv
;
792 mac_hi
= ((mdb
->addr
[0] << 8) | mdb
->addr
[1]);
793 mac_lo
= ((mdb
->addr
[2] << 24) | (mdb
->addr
[3] << 16));
794 mac_lo
|= ((mdb
->addr
[4] << 8) | mdb
->addr
[5]);
796 mutex_lock(&dev
->alu_mutex
);
798 for (index
= 0; index
< dev
->num_statics
; index
++) {
799 /* find empty slot first */
800 data
= (index
<< ALU_STAT_INDEX_S
) |
801 ALU_STAT_READ
| ALU_STAT_START
;
802 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
804 /* wait to be finished */
805 if (ksz9477_wait_alu_sta_ready(dev
)) {
806 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
810 /* read ALU static table */
811 ksz9477_read_table(dev
, static_table
);
813 if (static_table
[0] & ALU_V_STATIC_VALID
) {
814 /* check this has same vid & mac address */
815 if (((static_table
[2] >> ALU_V_FID_S
) == mdb
->vid
) &&
816 ((static_table
[2] & ALU_V_MAC_ADDR_HI
) == mac_hi
) &&
817 static_table
[3] == mac_lo
) {
818 /* found matching one */
822 /* found empty one */
827 /* no available entry */
828 if (index
== dev
->num_statics
)
832 static_table
[0] = ALU_V_STATIC_VALID
;
833 static_table
[1] |= BIT(port
);
835 static_table
[1] |= ALU_V_USE_FID
;
836 static_table
[2] = (mdb
->vid
<< ALU_V_FID_S
);
837 static_table
[2] |= mac_hi
;
838 static_table
[3] = mac_lo
;
840 ksz9477_write_table(dev
, static_table
);
842 data
= (index
<< ALU_STAT_INDEX_S
) | ALU_STAT_START
;
843 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
845 /* wait to be finished */
846 if (ksz9477_wait_alu_sta_ready(dev
))
847 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
850 mutex_unlock(&dev
->alu_mutex
);
853 static int ksz9477_port_mdb_del(struct dsa_switch
*ds
, int port
,
854 const struct switchdev_obj_port_mdb
*mdb
)
856 struct ksz_device
*dev
= ds
->priv
;
863 mac_hi
= ((mdb
->addr
[0] << 8) | mdb
->addr
[1]);
864 mac_lo
= ((mdb
->addr
[2] << 24) | (mdb
->addr
[3] << 16));
865 mac_lo
|= ((mdb
->addr
[4] << 8) | mdb
->addr
[5]);
867 mutex_lock(&dev
->alu_mutex
);
869 for (index
= 0; index
< dev
->num_statics
; index
++) {
870 /* find empty slot first */
871 data
= (index
<< ALU_STAT_INDEX_S
) |
872 ALU_STAT_READ
| ALU_STAT_START
;
873 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
875 /* wait to be finished */
876 ret
= ksz9477_wait_alu_sta_ready(dev
);
878 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
882 /* read ALU static table */
883 ksz9477_read_table(dev
, static_table
);
885 if (static_table
[0] & ALU_V_STATIC_VALID
) {
886 /* check this has same vid & mac address */
888 if (((static_table
[2] >> ALU_V_FID_S
) == mdb
->vid
) &&
889 ((static_table
[2] & ALU_V_MAC_ADDR_HI
) == mac_hi
) &&
890 static_table
[3] == mac_lo
) {
891 /* found matching one */
897 /* no available entry */
898 if (index
== dev
->num_statics
)
902 static_table
[1] &= ~BIT(port
);
904 if ((static_table
[1] & ALU_V_PORT_MAP
) == 0) {
912 ksz9477_write_table(dev
, static_table
);
914 data
= (index
<< ALU_STAT_INDEX_S
) | ALU_STAT_START
;
915 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
917 /* wait to be finished */
918 ret
= ksz9477_wait_alu_sta_ready(dev
);
920 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
923 mutex_unlock(&dev
->alu_mutex
);
928 static int ksz9477_port_mirror_add(struct dsa_switch
*ds
, int port
,
929 struct dsa_mall_mirror_tc_entry
*mirror
,
932 struct ksz_device
*dev
= ds
->priv
;
935 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_RX
, true);
937 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_TX
, true);
939 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_SNIFFER
, false);
941 /* configure mirror port */
942 ksz_port_cfg(dev
, mirror
->to_local_port
, P_MIRROR_CTRL
,
943 PORT_MIRROR_SNIFFER
, true);
945 ksz_cfg(dev
, S_MIRROR_CTRL
, SW_MIRROR_RX_TX
, false);
950 static void ksz9477_port_mirror_del(struct dsa_switch
*ds
, int port
,
951 struct dsa_mall_mirror_tc_entry
*mirror
)
953 struct ksz_device
*dev
= ds
->priv
;
957 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_RX
, false);
959 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_TX
, false);
961 ksz_pread8(dev
, port
, P_MIRROR_CTRL
, &data
);
963 if (!(data
& (PORT_MIRROR_RX
| PORT_MIRROR_TX
)))
964 ksz_port_cfg(dev
, mirror
->to_local_port
, P_MIRROR_CTRL
,
965 PORT_MIRROR_SNIFFER
, false);
968 static bool ksz9477_get_gbit(struct ksz_device
*dev
, u8 data
)
972 if (dev
->features
& NEW_XMII
)
973 gbit
= !(data
& PORT_MII_NOT_1GBIT
);
975 gbit
= !!(data
& PORT_MII_1000MBIT_S1
);
979 static void ksz9477_set_gbit(struct ksz_device
*dev
, bool gbit
, u8
*data
)
981 if (dev
->features
& NEW_XMII
) {
983 *data
&= ~PORT_MII_NOT_1GBIT
;
985 *data
|= PORT_MII_NOT_1GBIT
;
988 *data
|= PORT_MII_1000MBIT_S1
;
990 *data
&= ~PORT_MII_1000MBIT_S1
;
994 static int ksz9477_get_xmii(struct ksz_device
*dev
, u8 data
)
998 if (dev
->features
& NEW_XMII
) {
999 switch (data
& PORT_MII_SEL_M
) {
1013 switch (data
& PORT_MII_SEL_M
) {
1014 case PORT_MII_SEL_S1
:
1017 case PORT_RMII_SEL_S1
:
1020 case PORT_GMII_SEL_S1
:
1030 static void ksz9477_set_xmii(struct ksz_device
*dev
, int mode
, u8
*data
)
1034 if (dev
->features
& NEW_XMII
) {
1037 xmii
= PORT_MII_SEL
;
1040 xmii
= PORT_RMII_SEL
;
1043 xmii
= PORT_GMII_SEL
;
1046 xmii
= PORT_RGMII_SEL
;
1052 xmii
= PORT_MII_SEL_S1
;
1055 xmii
= PORT_RMII_SEL_S1
;
1058 xmii
= PORT_GMII_SEL_S1
;
1061 xmii
= PORT_RGMII_SEL_S1
;
1065 *data
&= ~PORT_MII_SEL_M
;
1069 static phy_interface_t
ksz9477_get_interface(struct ksz_device
*dev
, int port
)
1071 phy_interface_t interface
;
1076 if (port
< dev
->phy_port_cnt
)
1077 return PHY_INTERFACE_MODE_NA
;
1078 ksz_pread8(dev
, port
, REG_PORT_XMII_CTRL_1
, &data8
);
1079 gbit
= ksz9477_get_gbit(dev
, data8
);
1080 mode
= ksz9477_get_xmii(dev
, data8
);
1083 interface
= PHY_INTERFACE_MODE_GMII
;
1088 interface
= PHY_INTERFACE_MODE_MII
;
1091 interface
= PHY_INTERFACE_MODE_RMII
;
1094 interface
= PHY_INTERFACE_MODE_RGMII
;
1095 if (data8
& PORT_RGMII_ID_EG_ENABLE
)
1096 interface
= PHY_INTERFACE_MODE_RGMII_TXID
;
1097 if (data8
& PORT_RGMII_ID_IG_ENABLE
) {
1098 interface
= PHY_INTERFACE_MODE_RGMII_RXID
;
1099 if (data8
& PORT_RGMII_ID_EG_ENABLE
)
1100 interface
= PHY_INTERFACE_MODE_RGMII_ID
;
1107 static void ksz9477_port_mmd_write(struct ksz_device
*dev
, int port
,
1108 u8 dev_addr
, u16 reg_addr
, u16 val
)
1110 ksz_pwrite16(dev
, port
, REG_PORT_PHY_MMD_SETUP
,
1111 MMD_SETUP(PORT_MMD_OP_INDEX
, dev_addr
));
1112 ksz_pwrite16(dev
, port
, REG_PORT_PHY_MMD_INDEX_DATA
, reg_addr
);
1113 ksz_pwrite16(dev
, port
, REG_PORT_PHY_MMD_SETUP
,
1114 MMD_SETUP(PORT_MMD_OP_DATA_NO_INCR
, dev_addr
));
1115 ksz_pwrite16(dev
, port
, REG_PORT_PHY_MMD_INDEX_DATA
, val
);
1118 static void ksz9477_phy_errata_setup(struct ksz_device
*dev
, int port
)
1120 /* Apply PHY settings to address errata listed in
1121 * KSZ9477, KSZ9897, KSZ9896, KSZ9567, KSZ8565
1122 * Silicon Errata and Data Sheet Clarification documents:
1124 * Register settings are needed to improve PHY receive performance
1126 ksz9477_port_mmd_write(dev
, port
, 0x01, 0x6f, 0xdd0b);
1127 ksz9477_port_mmd_write(dev
, port
, 0x01, 0x8f, 0x6032);
1128 ksz9477_port_mmd_write(dev
, port
, 0x01, 0x9d, 0x248c);
1129 ksz9477_port_mmd_write(dev
, port
, 0x01, 0x75, 0x0060);
1130 ksz9477_port_mmd_write(dev
, port
, 0x01, 0xd3, 0x7777);
1131 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x06, 0x3008);
1132 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x08, 0x2001);
1134 /* Transmit waveform amplitude can be improved
1135 * (1000BASE-T, 100BASE-TX, 10BASE-Te)
1137 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x04, 0x00d0);
1139 /* Energy Efficient Ethernet (EEE) feature select must
1140 * be manually disabled (except on KSZ8565 which is 100Mbit)
1142 if (dev
->features
& GBIT_SUPPORT
)
1143 ksz9477_port_mmd_write(dev
, port
, 0x07, 0x3c, 0x0000);
1145 /* Register settings are required to meet data sheet
1146 * supply current specifications
1148 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x13, 0x6eff);
1149 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x14, 0xe6ff);
1150 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x15, 0x6eff);
1151 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x16, 0xe6ff);
1152 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x17, 0x00ff);
1153 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x18, 0x43ff);
1154 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x19, 0xc3ff);
1155 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x1a, 0x6fff);
1156 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x1b, 0x07ff);
1157 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x1c, 0x0fff);
1158 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x1d, 0xe7ff);
1159 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x1e, 0xefff);
1160 ksz9477_port_mmd_write(dev
, port
, 0x1c, 0x20, 0xeeee);
1163 static void ksz9477_port_setup(struct ksz_device
*dev
, int port
, bool cpu_port
)
1168 struct ksz_port
*p
= &dev
->ports
[port
];
1170 /* enable tag tail for host port */
1172 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
, PORT_TAIL_TAG_ENABLE
,
1175 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
, PORT_MAC_LOOPBACK
, false);
1177 /* set back pressure */
1178 ksz_port_cfg(dev
, port
, REG_PORT_MAC_CTRL_1
, PORT_BACK_PRESSURE
, true);
1180 /* enable broadcast storm limit */
1181 ksz_port_cfg(dev
, port
, P_BCAST_STORM_CTRL
, PORT_BROADCAST_STORM
, true);
1183 /* disable DiffServ priority */
1184 ksz_port_cfg(dev
, port
, P_PRIO_CTRL
, PORT_DIFFSERV_PRIO_ENABLE
, false);
1186 /* replace priority */
1187 ksz_port_cfg(dev
, port
, REG_PORT_MRI_MAC_CTRL
, PORT_USER_PRIO_CEILING
,
1189 ksz9477_port_cfg32(dev
, port
, REG_PORT_MTI_QUEUE_CTRL_0__4
,
1190 MTI_PVID_REPLACE
, false);
1192 /* enable 802.1p priority */
1193 ksz_port_cfg(dev
, port
, P_PRIO_CTRL
, PORT_802_1P_PRIO_ENABLE
, true);
1195 if (port
< dev
->phy_port_cnt
) {
1196 /* do not force flow control */
1197 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
,
1198 PORT_FORCE_TX_FLOW_CTRL
| PORT_FORCE_RX_FLOW_CTRL
,
1201 if (dev
->phy_errata_9477
)
1202 ksz9477_phy_errata_setup(dev
, port
);
1204 /* force flow control */
1205 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
,
1206 PORT_FORCE_TX_FLOW_CTRL
| PORT_FORCE_RX_FLOW_CTRL
,
1209 /* configure MAC to 1G & RGMII mode */
1210 ksz_pread8(dev
, port
, REG_PORT_XMII_CTRL_1
, &data8
);
1211 switch (dev
->interface
) {
1212 case PHY_INTERFACE_MODE_MII
:
1213 ksz9477_set_xmii(dev
, 0, &data8
);
1214 ksz9477_set_gbit(dev
, false, &data8
);
1215 p
->phydev
.speed
= SPEED_100
;
1217 case PHY_INTERFACE_MODE_RMII
:
1218 ksz9477_set_xmii(dev
, 1, &data8
);
1219 ksz9477_set_gbit(dev
, false, &data8
);
1220 p
->phydev
.speed
= SPEED_100
;
1222 case PHY_INTERFACE_MODE_GMII
:
1223 ksz9477_set_xmii(dev
, 2, &data8
);
1224 ksz9477_set_gbit(dev
, true, &data8
);
1225 p
->phydev
.speed
= SPEED_1000
;
1228 ksz9477_set_xmii(dev
, 3, &data8
);
1229 ksz9477_set_gbit(dev
, true, &data8
);
1230 data8
&= ~PORT_RGMII_ID_IG_ENABLE
;
1231 data8
&= ~PORT_RGMII_ID_EG_ENABLE
;
1232 if (dev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
1233 dev
->interface
== PHY_INTERFACE_MODE_RGMII_RXID
)
1234 data8
|= PORT_RGMII_ID_IG_ENABLE
;
1235 if (dev
->interface
== PHY_INTERFACE_MODE_RGMII_ID
||
1236 dev
->interface
== PHY_INTERFACE_MODE_RGMII_TXID
)
1237 data8
|= PORT_RGMII_ID_EG_ENABLE
;
1238 p
->phydev
.speed
= SPEED_1000
;
1241 ksz_pwrite8(dev
, port
, REG_PORT_XMII_CTRL_1
, data8
);
1242 p
->phydev
.duplex
= 1;
1244 mutex_lock(&dev
->dev_mutex
);
1246 member
= dev
->port_mask
;
1248 member
= dev
->host_mask
| p
->vid_member
;
1249 mutex_unlock(&dev
->dev_mutex
);
1250 ksz9477_cfg_port_member(dev
, port
, member
);
1252 /* clear pending interrupts */
1253 if (port
< dev
->phy_port_cnt
)
1254 ksz_pread16(dev
, port
, REG_PORT_PHY_INT_ENABLE
, &data16
);
1257 static void ksz9477_config_cpu_port(struct dsa_switch
*ds
)
1259 struct ksz_device
*dev
= ds
->priv
;
1263 ds
->num_ports
= dev
->port_cnt
;
1265 for (i
= 0; i
< dev
->port_cnt
; i
++) {
1266 if (dsa_is_cpu_port(ds
, i
) && (dev
->cpu_ports
& (1 << i
))) {
1267 phy_interface_t interface
;
1270 dev
->host_mask
= (1 << dev
->cpu_port
);
1271 dev
->port_mask
|= dev
->host_mask
;
1273 /* Read from XMII register to determine host port
1274 * interface. If set specifically in device tree
1275 * note the difference to help debugging.
1277 interface
= ksz9477_get_interface(dev
, i
);
1278 if (!dev
->interface
)
1279 dev
->interface
= interface
;
1280 if (interface
&& interface
!= dev
->interface
)
1282 "use %s instead of %s\n",
1283 phy_modes(dev
->interface
),
1284 phy_modes(interface
));
1286 /* enable cpu port */
1287 ksz9477_port_setup(dev
, i
, true);
1288 p
= &dev
->ports
[dev
->cpu_port
];
1289 p
->vid_member
= dev
->port_mask
;
1294 dev
->member
= dev
->host_mask
;
1296 for (i
= 0; i
< dev
->mib_port_cnt
; i
++) {
1297 if (i
== dev
->cpu_port
)
1301 /* Initialize to non-zero so that ksz_cfg_port_member() will
1304 p
->vid_member
= (1 << i
);
1305 p
->member
= dev
->port_mask
;
1306 ksz9477_port_stp_state_set(ds
, i
, BR_STATE_DISABLED
);
1308 if (i
< dev
->phy_port_cnt
)
1310 if (dev
->chip_id
== 0x00947700 && i
== 6) {
1313 /* SGMII PHY detection code is not implemented yet. */
1319 static int ksz9477_setup(struct dsa_switch
*ds
)
1321 struct ksz_device
*dev
= ds
->priv
;
1324 dev
->vlan_cache
= devm_kcalloc(dev
->dev
, sizeof(struct vlan_table
),
1325 dev
->num_vlans
, GFP_KERNEL
);
1326 if (!dev
->vlan_cache
)
1329 ret
= ksz9477_reset_switch(dev
);
1331 dev_err(ds
->dev
, "failed to reset switch\n");
1335 /* Required for port partitioning. */
1336 ksz9477_cfg32(dev
, REG_SW_QM_CTRL__4
, UNICAST_VLAN_BOUNDARY
,
1339 /* Do not work correctly with tail tagging. */
1340 ksz_cfg(dev
, REG_SW_MAC_CTRL_0
, SW_CHECK_LENGTH
, false);
1342 /* accept packet up to 2000bytes */
1343 ksz_cfg(dev
, REG_SW_MAC_CTRL_1
, SW_LEGAL_PACKET_DISABLE
, true);
1345 ksz9477_config_cpu_port(ds
);
1347 ksz_cfg(dev
, REG_SW_MAC_CTRL_1
, MULTICAST_STORM_DISABLE
, true);
1349 /* queue based egress rate limit */
1350 ksz_cfg(dev
, REG_SW_MAC_CTRL_5
, SW_OUT_RATE_LIMIT_QUEUE_BASED
, true);
1352 /* enable global MIB counter freeze function */
1353 ksz_cfg(dev
, REG_SW_MAC_CTRL_6
, SW_MIB_COUNTER_FREEZE
, true);
1356 ksz_cfg(dev
, REG_SW_OPERATION
, SW_START
, true);
1358 ksz_init_mib_timer(dev
);
1363 static const struct dsa_switch_ops ksz9477_switch_ops
= {
1364 .get_tag_protocol
= ksz9477_get_tag_protocol
,
1365 .setup
= ksz9477_setup
,
1366 .phy_read
= ksz9477_phy_read16
,
1367 .phy_write
= ksz9477_phy_write16
,
1368 .phylink_mac_link_down
= ksz_mac_link_down
,
1369 .port_enable
= ksz_enable_port
,
1370 .get_strings
= ksz9477_get_strings
,
1371 .get_ethtool_stats
= ksz_get_ethtool_stats
,
1372 .get_sset_count
= ksz_sset_count
,
1373 .port_bridge_join
= ksz_port_bridge_join
,
1374 .port_bridge_leave
= ksz_port_bridge_leave
,
1375 .port_stp_state_set
= ksz9477_port_stp_state_set
,
1376 .port_fast_age
= ksz_port_fast_age
,
1377 .port_vlan_filtering
= ksz9477_port_vlan_filtering
,
1378 .port_vlan_prepare
= ksz_port_vlan_prepare
,
1379 .port_vlan_add
= ksz9477_port_vlan_add
,
1380 .port_vlan_del
= ksz9477_port_vlan_del
,
1381 .port_fdb_dump
= ksz9477_port_fdb_dump
,
1382 .port_fdb_add
= ksz9477_port_fdb_add
,
1383 .port_fdb_del
= ksz9477_port_fdb_del
,
1384 .port_mdb_prepare
= ksz_port_mdb_prepare
,
1385 .port_mdb_add
= ksz9477_port_mdb_add
,
1386 .port_mdb_del
= ksz9477_port_mdb_del
,
1387 .port_mirror_add
= ksz9477_port_mirror_add
,
1388 .port_mirror_del
= ksz9477_port_mirror_del
,
1391 static u32
ksz9477_get_port_addr(int port
, int offset
)
1393 return PORT_CTRL_ADDR(port
, offset
);
1396 static int ksz9477_switch_detect(struct ksz_device
*dev
)
1404 /* turn off SPI DO Edge select */
1405 ret
= ksz_read8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, &data8
);
1409 data8
&= ~SPI_AUTO_EDGE_DETECTION
;
1410 ret
= ksz_write8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, data8
);
1415 ret
= ksz_read32(dev
, REG_CHIP_ID0__1
, &id32
);
1418 ret
= ksz_read8(dev
, REG_GLOBAL_OPTIONS
, &data8
);
1422 /* Number of ports can be reduced depending on chip. */
1423 dev
->mib_port_cnt
= TOTAL_PORT_NUM
;
1424 dev
->phy_port_cnt
= 5;
1426 /* Default capability is gigabit capable. */
1427 dev
->features
= GBIT_SUPPORT
;
1429 id_hi
= (u8
)(id32
>> 16);
1430 id_lo
= (u8
)(id32
>> 8);
1431 if ((id_lo
& 0xf) == 3) {
1432 /* Chip is from KSZ9893 design. */
1433 dev
->features
|= IS_9893
;
1435 /* Chip does not support gigabit. */
1436 if (data8
& SW_QW_ABLE
)
1437 dev
->features
&= ~GBIT_SUPPORT
;
1438 dev
->mib_port_cnt
= 3;
1439 dev
->phy_port_cnt
= 2;
1441 /* Chip uses new XMII register definitions. */
1442 dev
->features
|= NEW_XMII
;
1444 /* Chip does not support gigabit. */
1445 if (!(data8
& SW_GIGABIT_ABLE
))
1446 dev
->features
&= ~GBIT_SUPPORT
;
1449 /* Change chip id to known ones so it can be matched against them. */
1450 id32
= (id_hi
<< 16) | (id_lo
<< 8);
1452 dev
->chip_id
= id32
;
1457 struct ksz_chip_data
{
1459 const char *dev_name
;
1465 bool phy_errata_9477
;
1468 static const struct ksz_chip_data ksz9477_switch_chips
[] = {
1470 .chip_id
= 0x00947700,
1471 .dev_name
= "KSZ9477",
1475 .cpu_ports
= 0x7F, /* can be configured as cpu port */
1476 .port_cnt
= 7, /* total physical port count */
1477 .phy_errata_9477
= true,
1480 .chip_id
= 0x00989700,
1481 .dev_name
= "KSZ9897",
1485 .cpu_ports
= 0x7F, /* can be configured as cpu port */
1486 .port_cnt
= 7, /* total physical port count */
1487 .phy_errata_9477
= true,
1490 .chip_id
= 0x00989300,
1491 .dev_name
= "KSZ9893",
1495 .cpu_ports
= 0x07, /* can be configured as cpu port */
1496 .port_cnt
= 3, /* total port count */
1499 .chip_id
= 0x00956700,
1500 .dev_name
= "KSZ9567",
1504 .cpu_ports
= 0x7F, /* can be configured as cpu port */
1505 .port_cnt
= 7, /* total physical port count */
1509 static int ksz9477_switch_init(struct ksz_device
*dev
)
1513 dev
->ds
->ops
= &ksz9477_switch_ops
;
1515 for (i
= 0; i
< ARRAY_SIZE(ksz9477_switch_chips
); i
++) {
1516 const struct ksz_chip_data
*chip
= &ksz9477_switch_chips
[i
];
1518 if (dev
->chip_id
== chip
->chip_id
) {
1519 dev
->name
= chip
->dev_name
;
1520 dev
->num_vlans
= chip
->num_vlans
;
1521 dev
->num_alus
= chip
->num_alus
;
1522 dev
->num_statics
= chip
->num_statics
;
1523 dev
->port_cnt
= chip
->port_cnt
;
1524 dev
->cpu_ports
= chip
->cpu_ports
;
1525 dev
->phy_errata_9477
= chip
->phy_errata_9477
;
1531 /* no switch found */
1535 dev
->port_mask
= (1 << dev
->port_cnt
) - 1;
1537 dev
->reg_mib_cnt
= SWITCH_COUNTER_NUM
;
1538 dev
->mib_cnt
= TOTAL_SWITCH_COUNTER_NUM
;
1540 i
= dev
->mib_port_cnt
;
1541 dev
->ports
= devm_kzalloc(dev
->dev
, sizeof(struct ksz_port
) * i
,
1545 for (i
= 0; i
< dev
->mib_port_cnt
; i
++) {
1546 mutex_init(&dev
->ports
[i
].mib
.cnt_mutex
);
1547 dev
->ports
[i
].mib
.counters
=
1548 devm_kzalloc(dev
->dev
,
1550 (TOTAL_SWITCH_COUNTER_NUM
+ 1),
1552 if (!dev
->ports
[i
].mib
.counters
)
1556 /* set the real number of ports */
1557 dev
->ds
->num_ports
= dev
->port_cnt
;
1562 static void ksz9477_switch_exit(struct ksz_device
*dev
)
1564 ksz9477_reset_switch(dev
);
1567 static const struct ksz_dev_ops ksz9477_dev_ops
= {
1568 .get_port_addr
= ksz9477_get_port_addr
,
1569 .cfg_port_member
= ksz9477_cfg_port_member
,
1570 .flush_dyn_mac_table
= ksz9477_flush_dyn_mac_table
,
1571 .port_setup
= ksz9477_port_setup
,
1572 .r_mib_cnt
= ksz9477_r_mib_cnt
,
1573 .r_mib_pkt
= ksz9477_r_mib_pkt
,
1574 .freeze_mib
= ksz9477_freeze_mib
,
1575 .port_init_cnt
= ksz9477_port_init_cnt
,
1576 .shutdown
= ksz9477_reset_switch
,
1577 .detect
= ksz9477_switch_detect
,
1578 .init
= ksz9477_switch_init
,
1579 .exit
= ksz9477_switch_exit
,
1582 int ksz9477_switch_register(struct ksz_device
*dev
)
1585 struct phy_device
*phydev
;
1587 ret
= ksz_switch_register(dev
, &ksz9477_dev_ops
);
1591 for (i
= 0; i
< dev
->phy_port_cnt
; ++i
) {
1592 if (!dsa_is_user_port(dev
->ds
, i
))
1595 phydev
= dsa_to_port(dev
->ds
, i
)->slave
->phydev
;
1597 /* The MAC actually cannot run in 1000 half-duplex mode. */
1598 phy_remove_link_mode(phydev
,
1599 ETHTOOL_LINK_MODE_1000baseT_Half_BIT
);
1601 /* PHY does not support gigabit. */
1602 if (!(dev
->features
& GBIT_SUPPORT
))
1603 phy_remove_link_mode(phydev
,
1604 ETHTOOL_LINK_MODE_1000baseT_Full_BIT
);
1608 EXPORT_SYMBOL(ksz9477_switch_register
);
1610 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1611 MODULE_DESCRIPTION("Microchip KSZ9477 Series Switch DSA Driver");
1612 MODULE_LICENSE("GPL");