2 * Microchip switch driver main logic
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include <linux/delay.h>
20 #include <linux/export.h>
21 #include <linux/gpio.h>
22 #include <linux/kernel.h>
23 #include <linux/module.h>
24 #include <linux/platform_data/microchip-ksz.h>
25 #include <linux/phy.h>
26 #include <linux/etherdevice.h>
27 #include <linux/if_bridge.h>
29 #include <net/switchdev.h>
35 char string
[ETH_GSTRING_LEN
];
36 } mib_names
[TOTAL_SWITCH_COUNTER_NUM
] = {
38 { 0x01, "rx_undersize" },
39 { 0x02, "rx_fragments" },
40 { 0x03, "rx_oversize" },
41 { 0x04, "rx_jabbers" },
42 { 0x05, "rx_symbol_err" },
43 { 0x06, "rx_crc_err" },
44 { 0x07, "rx_align_err" },
45 { 0x08, "rx_mac_ctrl" },
50 { 0x0D, "rx_64_or_less" },
51 { 0x0E, "rx_65_127" },
52 { 0x0F, "rx_128_255" },
53 { 0x10, "rx_256_511" },
54 { 0x11, "rx_512_1023" },
55 { 0x12, "rx_1024_1522" },
56 { 0x13, "rx_1523_2000" },
59 { 0x16, "tx_late_col" },
64 { 0x1B, "tx_deferred" },
65 { 0x1C, "tx_total_col" },
66 { 0x1D, "tx_exc_col" },
67 { 0x1E, "tx_single_col" },
68 { 0x1F, "tx_mult_col" },
71 { 0x82, "rx_discards" },
72 { 0x83, "tx_discards" },
75 static void ksz_cfg(struct ksz_device
*dev
, u32 addr
, u8 bits
, bool set
)
79 ksz_read8(dev
, addr
, &data
);
84 ksz_write8(dev
, addr
, data
);
87 static void ksz_cfg32(struct ksz_device
*dev
, u32 addr
, u32 bits
, bool set
)
91 ksz_read32(dev
, addr
, &data
);
96 ksz_write32(dev
, addr
, data
);
99 static void ksz_port_cfg(struct ksz_device
*dev
, int port
, int offset
, u8 bits
,
105 addr
= PORT_CTRL_ADDR(port
, offset
);
106 ksz_read8(dev
, addr
, &data
);
113 ksz_write8(dev
, addr
, data
);
116 static void ksz_port_cfg32(struct ksz_device
*dev
, int port
, int offset
,
122 addr
= PORT_CTRL_ADDR(port
, offset
);
123 ksz_read32(dev
, addr
, &data
);
130 ksz_write32(dev
, addr
, data
);
133 static int wait_vlan_ctrl_ready(struct ksz_device
*dev
, u32 waiton
, int timeout
)
138 ksz_read8(dev
, REG_SW_VLAN_CTRL
, &data
);
139 if (!(data
& waiton
))
142 } while (timeout
-- > 0);
150 static int get_vlan_table(struct dsa_switch
*ds
, u16 vid
, u32
*vlan_table
)
152 struct ksz_device
*dev
= ds
->priv
;
155 mutex_lock(&dev
->vlan_mutex
);
157 ksz_write16(dev
, REG_SW_VLAN_ENTRY_INDEX__2
, vid
& VLAN_INDEX_M
);
158 ksz_write8(dev
, REG_SW_VLAN_CTRL
, VLAN_READ
| VLAN_START
);
160 /* wait to be cleared */
161 ret
= wait_vlan_ctrl_ready(dev
, VLAN_START
, 1000);
163 dev_dbg(dev
->dev
, "Failed to read vlan table\n");
167 ksz_read32(dev
, REG_SW_VLAN_ENTRY__4
, &vlan_table
[0]);
168 ksz_read32(dev
, REG_SW_VLAN_ENTRY_UNTAG__4
, &vlan_table
[1]);
169 ksz_read32(dev
, REG_SW_VLAN_ENTRY_PORTS__4
, &vlan_table
[2]);
171 ksz_write8(dev
, REG_SW_VLAN_CTRL
, 0);
174 mutex_unlock(&dev
->vlan_mutex
);
179 static int set_vlan_table(struct dsa_switch
*ds
, u16 vid
, u32
*vlan_table
)
181 struct ksz_device
*dev
= ds
->priv
;
184 mutex_lock(&dev
->vlan_mutex
);
186 ksz_write32(dev
, REG_SW_VLAN_ENTRY__4
, vlan_table
[0]);
187 ksz_write32(dev
, REG_SW_VLAN_ENTRY_UNTAG__4
, vlan_table
[1]);
188 ksz_write32(dev
, REG_SW_VLAN_ENTRY_PORTS__4
, vlan_table
[2]);
190 ksz_write16(dev
, REG_SW_VLAN_ENTRY_INDEX__2
, vid
& VLAN_INDEX_M
);
191 ksz_write8(dev
, REG_SW_VLAN_CTRL
, VLAN_START
| VLAN_WRITE
);
193 /* wait to be cleared */
194 ret
= wait_vlan_ctrl_ready(dev
, VLAN_START
, 1000);
196 dev_dbg(dev
->dev
, "Failed to write vlan table\n");
200 ksz_write8(dev
, REG_SW_VLAN_CTRL
, 0);
202 /* update vlan cache table */
203 dev
->vlan_cache
[vid
].table
[0] = vlan_table
[0];
204 dev
->vlan_cache
[vid
].table
[1] = vlan_table
[1];
205 dev
->vlan_cache
[vid
].table
[2] = vlan_table
[2];
208 mutex_unlock(&dev
->vlan_mutex
);
213 static void read_table(struct dsa_switch
*ds
, u32
*table
)
215 struct ksz_device
*dev
= ds
->priv
;
217 ksz_read32(dev
, REG_SW_ALU_VAL_A
, &table
[0]);
218 ksz_read32(dev
, REG_SW_ALU_VAL_B
, &table
[1]);
219 ksz_read32(dev
, REG_SW_ALU_VAL_C
, &table
[2]);
220 ksz_read32(dev
, REG_SW_ALU_VAL_D
, &table
[3]);
223 static void write_table(struct dsa_switch
*ds
, u32
*table
)
225 struct ksz_device
*dev
= ds
->priv
;
227 ksz_write32(dev
, REG_SW_ALU_VAL_A
, table
[0]);
228 ksz_write32(dev
, REG_SW_ALU_VAL_B
, table
[1]);
229 ksz_write32(dev
, REG_SW_ALU_VAL_C
, table
[2]);
230 ksz_write32(dev
, REG_SW_ALU_VAL_D
, table
[3]);
233 static int wait_alu_ready(struct ksz_device
*dev
, u32 waiton
, int timeout
)
238 ksz_read32(dev
, REG_SW_ALU_CTRL__4
, &data
);
239 if (!(data
& waiton
))
242 } while (timeout
-- > 0);
250 static int wait_alu_sta_ready(struct ksz_device
*dev
, u32 waiton
, int timeout
)
255 ksz_read32(dev
, REG_SW_ALU_STAT_CTRL__4
, &data
);
256 if (!(data
& waiton
))
259 } while (timeout
-- > 0);
267 static int ksz_reset_switch(struct dsa_switch
*ds
)
269 struct ksz_device
*dev
= ds
->priv
;
275 ksz_cfg(dev
, REG_SW_OPERATION
, SW_RESET
, true);
277 /* turn off SPI DO Edge select */
278 ksz_read8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, &data8
);
279 data8
&= ~SPI_AUTO_EDGE_DETECTION
;
280 ksz_write8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, data8
);
282 /* default configuration */
283 ksz_read8(dev
, REG_SW_LUE_CTRL_1
, &data8
);
284 data8
= SW_AGING_ENABLE
| SW_LINK_AUTO_AGING
|
285 SW_SRC_ADDR_FILTER
| SW_FLUSH_STP_TABLE
| SW_FLUSH_MSTP_TABLE
;
286 ksz_write8(dev
, REG_SW_LUE_CTRL_1
, data8
);
288 /* disable interrupts */
289 ksz_write32(dev
, REG_SW_INT_MASK__4
, SWITCH_INT_MASK
);
290 ksz_write32(dev
, REG_SW_PORT_INT_MASK__4
, 0x7F);
291 ksz_read32(dev
, REG_SW_PORT_INT_STATUS__4
, &data32
);
293 /* set broadcast storm protection 10% rate */
294 ksz_read16(dev
, REG_SW_MAC_CTRL_2
, &data16
);
295 data16
&= ~BROADCAST_STORM_RATE
;
296 data16
|= (BROADCAST_STORM_VALUE
* BROADCAST_STORM_PROT_RATE
) / 100;
297 ksz_write16(dev
, REG_SW_MAC_CTRL_2
, data16
);
302 static void port_setup(struct ksz_device
*dev
, int port
, bool cpu_port
)
307 /* enable tag tail for host port */
309 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
, PORT_TAIL_TAG_ENABLE
,
312 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
, PORT_MAC_LOOPBACK
, false);
314 /* set back pressure */
315 ksz_port_cfg(dev
, port
, REG_PORT_MAC_CTRL_1
, PORT_BACK_PRESSURE
, true);
317 /* set flow control */
318 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
,
319 PORT_FORCE_TX_FLOW_CTRL
| PORT_FORCE_RX_FLOW_CTRL
, true);
321 /* enable broadcast storm limit */
322 ksz_port_cfg(dev
, port
, P_BCAST_STORM_CTRL
, PORT_BROADCAST_STORM
, true);
324 /* disable DiffServ priority */
325 ksz_port_cfg(dev
, port
, P_PRIO_CTRL
, PORT_DIFFSERV_PRIO_ENABLE
, false);
327 /* replace priority */
328 ksz_port_cfg(dev
, port
, REG_PORT_MRI_MAC_CTRL
, PORT_USER_PRIO_CEILING
,
330 ksz_port_cfg32(dev
, port
, REG_PORT_MTI_QUEUE_CTRL_0__4
,
331 MTI_PVID_REPLACE
, false);
333 /* enable 802.1p priority */
334 ksz_port_cfg(dev
, port
, P_PRIO_CTRL
, PORT_802_1P_PRIO_ENABLE
, true);
336 /* configure MAC to 1G & RGMII mode */
337 ksz_pread8(dev
, port
, REG_PORT_XMII_CTRL_1
, &data8
);
338 data8
|= PORT_RGMII_ID_EG_ENABLE
;
339 data8
&= ~PORT_MII_NOT_1GBIT
;
340 data8
&= ~PORT_MII_SEL_M
;
341 data8
|= PORT_RGMII_SEL
;
342 ksz_pwrite8(dev
, port
, REG_PORT_XMII_CTRL_1
, data8
);
344 /* clear pending interrupts */
345 ksz_pread16(dev
, port
, REG_PORT_PHY_INT_ENABLE
, &data16
);
348 static void ksz_config_cpu_port(struct dsa_switch
*ds
)
350 struct ksz_device
*dev
= ds
->priv
;
353 ds
->num_ports
= dev
->port_cnt
;
355 for (i
= 0; i
< ds
->num_ports
; i
++) {
356 if (dsa_is_cpu_port(ds
, i
) && (dev
->cpu_ports
& (1 << i
))) {
359 /* enable cpu port */
360 port_setup(dev
, i
, true);
365 static int ksz_setup(struct dsa_switch
*ds
)
367 struct ksz_device
*dev
= ds
->priv
;
370 dev
->vlan_cache
= devm_kcalloc(dev
->dev
, sizeof(struct vlan_table
),
371 dev
->num_vlans
, GFP_KERNEL
);
372 if (!dev
->vlan_cache
)
375 ret
= ksz_reset_switch(ds
);
377 dev_err(ds
->dev
, "failed to reset switch\n");
381 /* accept packet up to 2000bytes */
382 ksz_cfg(dev
, REG_SW_MAC_CTRL_1
, SW_LEGAL_PACKET_DISABLE
, true);
384 ksz_config_cpu_port(ds
);
386 ksz_cfg(dev
, REG_SW_MAC_CTRL_1
, MULTICAST_STORM_DISABLE
, true);
388 /* queue based egress rate limit */
389 ksz_cfg(dev
, REG_SW_MAC_CTRL_5
, SW_OUT_RATE_LIMIT_QUEUE_BASED
, true);
392 ksz_cfg(dev
, REG_SW_OPERATION
, SW_START
, true);
397 static enum dsa_tag_protocol
ksz_get_tag_protocol(struct dsa_switch
*ds
,
400 return DSA_TAG_PROTO_KSZ
;
403 static int ksz_phy_read16(struct dsa_switch
*ds
, int addr
, int reg
)
405 struct ksz_device
*dev
= ds
->priv
;
408 ksz_pread16(dev
, addr
, 0x100 + (reg
<< 1), &val
);
413 static int ksz_phy_write16(struct dsa_switch
*ds
, int addr
, int reg
, u16 val
)
415 struct ksz_device
*dev
= ds
->priv
;
417 ksz_pwrite16(dev
, addr
, 0x100 + (reg
<< 1), val
);
422 static int ksz_enable_port(struct dsa_switch
*ds
, int port
,
423 struct phy_device
*phy
)
425 struct ksz_device
*dev
= ds
->priv
;
427 /* setup slave port */
428 port_setup(dev
, port
, false);
433 static void ksz_disable_port(struct dsa_switch
*ds
, int port
,
434 struct phy_device
*phy
)
436 struct ksz_device
*dev
= ds
->priv
;
438 /* there is no port disable */
439 ksz_port_cfg(dev
, port
, REG_PORT_CTRL_0
, PORT_MAC_LOOPBACK
, true);
442 static int ksz_sset_count(struct dsa_switch
*ds
)
444 return TOTAL_SWITCH_COUNTER_NUM
;
447 static void ksz_get_strings(struct dsa_switch
*ds
, int port
, uint8_t *buf
)
451 for (i
= 0; i
< TOTAL_SWITCH_COUNTER_NUM
; i
++) {
452 memcpy(buf
+ i
* ETH_GSTRING_LEN
, mib_names
[i
].string
,
457 static void ksz_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
460 struct ksz_device
*dev
= ds
->priv
;
465 mutex_lock(&dev
->stats_mutex
);
467 for (i
= 0; i
< TOTAL_SWITCH_COUNTER_NUM
; i
++) {
468 data
= MIB_COUNTER_READ
;
469 data
|= ((mib_names
[i
].index
& 0xFF) << MIB_COUNTER_INDEX_S
);
470 ksz_pwrite32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
, data
);
474 ksz_pread32(dev
, port
, REG_PORT_MIB_CTRL_STAT__4
,
477 if (!(data
& MIB_COUNTER_READ
))
479 } while (timeout
-- > 0);
481 /* failed to read MIB. get out of loop */
483 dev_dbg(dev
->dev
, "Failed to get MIB\n");
487 /* count resets upon read */
488 ksz_pread32(dev
, port
, REG_PORT_MIB_DATA
, &data
);
490 dev
->mib_value
[i
] += (uint64_t)data
;
491 buf
[i
] = dev
->mib_value
[i
];
494 mutex_unlock(&dev
->stats_mutex
);
497 static void ksz_port_stp_state_set(struct dsa_switch
*ds
, int port
, u8 state
)
499 struct ksz_device
*dev
= ds
->priv
;
502 ksz_pread8(dev
, port
, P_STP_CTRL
, &data
);
503 data
&= ~(PORT_TX_ENABLE
| PORT_RX_ENABLE
| PORT_LEARN_DISABLE
);
506 case BR_STATE_DISABLED
:
507 data
|= PORT_LEARN_DISABLE
;
509 case BR_STATE_LISTENING
:
510 data
|= (PORT_RX_ENABLE
| PORT_LEARN_DISABLE
);
512 case BR_STATE_LEARNING
:
513 data
|= PORT_RX_ENABLE
;
515 case BR_STATE_FORWARDING
:
516 data
|= (PORT_TX_ENABLE
| PORT_RX_ENABLE
);
518 case BR_STATE_BLOCKING
:
519 data
|= PORT_LEARN_DISABLE
;
522 dev_err(ds
->dev
, "invalid STP state: %d\n", state
);
526 ksz_pwrite8(dev
, port
, P_STP_CTRL
, data
);
529 static void ksz_port_fast_age(struct dsa_switch
*ds
, int port
)
531 struct ksz_device
*dev
= ds
->priv
;
534 ksz_read8(dev
, REG_SW_LUE_CTRL_1
, &data8
);
535 data8
|= SW_FAST_AGING
;
536 ksz_write8(dev
, REG_SW_LUE_CTRL_1
, data8
);
538 data8
&= ~SW_FAST_AGING
;
539 ksz_write8(dev
, REG_SW_LUE_CTRL_1
, data8
);
542 static int ksz_port_vlan_filtering(struct dsa_switch
*ds
, int port
, bool flag
)
544 struct ksz_device
*dev
= ds
->priv
;
547 ksz_port_cfg(dev
, port
, REG_PORT_LUE_CTRL
,
548 PORT_VLAN_LOOKUP_VID_0
, true);
549 ksz_cfg32(dev
, REG_SW_QM_CTRL__4
, UNICAST_VLAN_BOUNDARY
, true);
550 ksz_cfg(dev
, REG_SW_LUE_CTRL_0
, SW_VLAN_ENABLE
, true);
552 ksz_cfg(dev
, REG_SW_LUE_CTRL_0
, SW_VLAN_ENABLE
, false);
553 ksz_cfg32(dev
, REG_SW_QM_CTRL__4
, UNICAST_VLAN_BOUNDARY
, false);
554 ksz_port_cfg(dev
, port
, REG_PORT_LUE_CTRL
,
555 PORT_VLAN_LOOKUP_VID_0
, false);
561 static int ksz_port_vlan_prepare(struct dsa_switch
*ds
, int port
,
562 const struct switchdev_obj_port_vlan
*vlan
)
569 static void ksz_port_vlan_add(struct dsa_switch
*ds
, int port
,
570 const struct switchdev_obj_port_vlan
*vlan
)
572 struct ksz_device
*dev
= ds
->priv
;
575 bool untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
577 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
578 if (get_vlan_table(ds
, vid
, vlan_table
)) {
579 dev_dbg(dev
->dev
, "Failed to get vlan table\n");
583 vlan_table
[0] = VLAN_VALID
| (vid
& VLAN_FID_M
);
585 vlan_table
[1] |= BIT(port
);
587 vlan_table
[1] &= ~BIT(port
);
588 vlan_table
[1] &= ~(BIT(dev
->cpu_port
));
590 vlan_table
[2] |= BIT(port
) | BIT(dev
->cpu_port
);
592 if (set_vlan_table(ds
, vid
, vlan_table
)) {
593 dev_dbg(dev
->dev
, "Failed to set vlan table\n");
598 if (vlan
->flags
& BRIDGE_VLAN_INFO_PVID
)
599 ksz_pwrite16(dev
, port
, REG_PORT_DEFAULT_VID
, vid
);
603 static int ksz_port_vlan_del(struct dsa_switch
*ds
, int port
,
604 const struct switchdev_obj_port_vlan
*vlan
)
606 struct ksz_device
*dev
= ds
->priv
;
607 bool untagged
= vlan
->flags
& BRIDGE_VLAN_INFO_UNTAGGED
;
612 ksz_pread16(dev
, port
, REG_PORT_DEFAULT_VID
, &pvid
);
615 for (vid
= vlan
->vid_begin
; vid
<= vlan
->vid_end
; vid
++) {
616 if (get_vlan_table(ds
, vid
, vlan_table
)) {
617 dev_dbg(dev
->dev
, "Failed to get vlan table\n");
621 vlan_table
[2] &= ~BIT(port
);
627 vlan_table
[1] &= ~BIT(port
);
629 if (set_vlan_table(ds
, vid
, vlan_table
)) {
630 dev_dbg(dev
->dev
, "Failed to set vlan table\n");
635 ksz_pwrite16(dev
, port
, REG_PORT_DEFAULT_VID
, pvid
);
659 static int ksz_port_fdb_add(struct dsa_switch
*ds
, int port
,
660 const unsigned char *addr
, u16 vid
)
662 struct ksz_device
*dev
= ds
->priv
;
667 mutex_lock(&dev
->alu_mutex
);
669 /* find any entry with mac & vid */
670 data
= vid
<< ALU_FID_INDEX_S
;
671 data
|= ((addr
[0] << 8) | addr
[1]);
672 ksz_write32(dev
, REG_SW_ALU_INDEX_0
, data
);
674 data
= ((addr
[2] << 24) | (addr
[3] << 16));
675 data
|= ((addr
[4] << 8) | addr
[5]);
676 ksz_write32(dev
, REG_SW_ALU_INDEX_1
, data
);
678 /* start read operation */
679 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_READ
| ALU_START
);
681 /* wait to be finished */
682 ret
= wait_alu_ready(dev
, ALU_START
, 1000);
684 dev_dbg(dev
->dev
, "Failed to read ALU\n");
689 read_table(ds
, alu_table
);
691 /* update ALU entry */
692 alu_table
[0] = ALU_V_STATIC_VALID
;
693 alu_table
[1] |= BIT(port
);
695 alu_table
[1] |= ALU_V_USE_FID
;
696 alu_table
[2] = (vid
<< ALU_V_FID_S
);
697 alu_table
[2] |= ((addr
[0] << 8) | addr
[1]);
698 alu_table
[3] = ((addr
[2] << 24) | (addr
[3] << 16));
699 alu_table
[3] |= ((addr
[4] << 8) | addr
[5]);
701 write_table(ds
, alu_table
);
703 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_WRITE
| ALU_START
);
705 /* wait to be finished */
706 ret
= wait_alu_ready(dev
, ALU_START
, 1000);
708 dev_dbg(dev
->dev
, "Failed to write ALU\n");
711 mutex_unlock(&dev
->alu_mutex
);
716 static int ksz_port_fdb_del(struct dsa_switch
*ds
, int port
,
717 const unsigned char *addr
, u16 vid
)
719 struct ksz_device
*dev
= ds
->priv
;
724 mutex_lock(&dev
->alu_mutex
);
726 /* read any entry with mac & vid */
727 data
= vid
<< ALU_FID_INDEX_S
;
728 data
|= ((addr
[0] << 8) | addr
[1]);
729 ksz_write32(dev
, REG_SW_ALU_INDEX_0
, data
);
731 data
= ((addr
[2] << 24) | (addr
[3] << 16));
732 data
|= ((addr
[4] << 8) | addr
[5]);
733 ksz_write32(dev
, REG_SW_ALU_INDEX_1
, data
);
735 /* start read operation */
736 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_READ
| ALU_START
);
738 /* wait to be finished */
739 ret
= wait_alu_ready(dev
, ALU_START
, 1000);
741 dev_dbg(dev
->dev
, "Failed to read ALU\n");
745 ksz_read32(dev
, REG_SW_ALU_VAL_A
, &alu_table
[0]);
746 if (alu_table
[0] & ALU_V_STATIC_VALID
) {
747 ksz_read32(dev
, REG_SW_ALU_VAL_B
, &alu_table
[1]);
748 ksz_read32(dev
, REG_SW_ALU_VAL_C
, &alu_table
[2]);
749 ksz_read32(dev
, REG_SW_ALU_VAL_D
, &alu_table
[3]);
751 /* clear forwarding port */
752 alu_table
[2] &= ~BIT(port
);
754 /* if there is no port to forward, clear table */
755 if ((alu_table
[2] & ALU_V_PORT_MAP
) == 0) {
768 write_table(ds
, alu_table
);
770 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_WRITE
| ALU_START
);
772 /* wait to be finished */
773 ret
= wait_alu_ready(dev
, ALU_START
, 1000);
775 dev_dbg(dev
->dev
, "Failed to write ALU\n");
778 mutex_unlock(&dev
->alu_mutex
);
783 static void convert_alu(struct alu_struct
*alu
, u32
*alu_table
)
785 alu
->is_static
= !!(alu_table
[0] & ALU_V_STATIC_VALID
);
786 alu
->is_src_filter
= !!(alu_table
[0] & ALU_V_SRC_FILTER
);
787 alu
->is_dst_filter
= !!(alu_table
[0] & ALU_V_DST_FILTER
);
788 alu
->prio_age
= (alu_table
[0] >> ALU_V_PRIO_AGE_CNT_S
) &
789 ALU_V_PRIO_AGE_CNT_M
;
790 alu
->mstp
= alu_table
[0] & ALU_V_MSTP_M
;
792 alu
->is_override
= !!(alu_table
[1] & ALU_V_OVERRIDE
);
793 alu
->is_use_fid
= !!(alu_table
[1] & ALU_V_USE_FID
);
794 alu
->port_forward
= alu_table
[1] & ALU_V_PORT_MAP
;
796 alu
->fid
= (alu_table
[2] >> ALU_V_FID_S
) & ALU_V_FID_M
;
798 alu
->mac
[0] = (alu_table
[2] >> 8) & 0xFF;
799 alu
->mac
[1] = alu_table
[2] & 0xFF;
800 alu
->mac
[2] = (alu_table
[3] >> 24) & 0xFF;
801 alu
->mac
[3] = (alu_table
[3] >> 16) & 0xFF;
802 alu
->mac
[4] = (alu_table
[3] >> 8) & 0xFF;
803 alu
->mac
[5] = alu_table
[3] & 0xFF;
806 static int ksz_port_fdb_dump(struct dsa_switch
*ds
, int port
,
807 dsa_fdb_dump_cb_t
*cb
, void *data
)
809 struct ksz_device
*dev
= ds
->priv
;
813 struct alu_struct alu
;
816 mutex_lock(&dev
->alu_mutex
);
818 /* start ALU search */
819 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, ALU_START
| ALU_SEARCH
);
824 ksz_read32(dev
, REG_SW_ALU_CTRL__4
, &ksz_data
);
825 if ((ksz_data
& ALU_VALID
) || !(ksz_data
& ALU_START
))
828 } while (timeout
-- > 0);
831 dev_dbg(dev
->dev
, "Failed to search ALU\n");
837 read_table(ds
, alu_table
);
839 convert_alu(&alu
, alu_table
);
841 if (alu
.port_forward
& BIT(port
)) {
842 ret
= cb(alu
.mac
, alu
.fid
, alu
.is_static
, data
);
846 } while (ksz_data
& ALU_START
);
850 /* stop ALU search */
851 ksz_write32(dev
, REG_SW_ALU_CTRL__4
, 0);
853 mutex_unlock(&dev
->alu_mutex
);
858 static int ksz_port_mdb_prepare(struct dsa_switch
*ds
, int port
,
859 const struct switchdev_obj_port_mdb
*mdb
)
865 static void ksz_port_mdb_add(struct dsa_switch
*ds
, int port
,
866 const struct switchdev_obj_port_mdb
*mdb
)
868 struct ksz_device
*dev
= ds
->priv
;
874 mac_hi
= ((mdb
->addr
[0] << 8) | mdb
->addr
[1]);
875 mac_lo
= ((mdb
->addr
[2] << 24) | (mdb
->addr
[3] << 16));
876 mac_lo
|= ((mdb
->addr
[4] << 8) | mdb
->addr
[5]);
878 mutex_lock(&dev
->alu_mutex
);
880 for (index
= 0; index
< dev
->num_statics
; index
++) {
881 /* find empty slot first */
882 data
= (index
<< ALU_STAT_INDEX_S
) |
883 ALU_STAT_READ
| ALU_STAT_START
;
884 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
886 /* wait to be finished */
887 if (wait_alu_sta_ready(dev
, ALU_STAT_START
, 1000) < 0) {
888 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
892 /* read ALU static table */
893 read_table(ds
, static_table
);
895 if (static_table
[0] & ALU_V_STATIC_VALID
) {
896 /* check this has same vid & mac address */
897 if (((static_table
[2] >> ALU_V_FID_S
) == (mdb
->vid
)) &&
898 ((static_table
[2] & ALU_V_MAC_ADDR_HI
) == mac_hi
) &&
899 (static_table
[3] == mac_lo
)) {
900 /* found matching one */
904 /* found empty one */
909 /* no available entry */
910 if (index
== dev
->num_statics
)
914 static_table
[0] = ALU_V_STATIC_VALID
;
915 static_table
[1] |= BIT(port
);
917 static_table
[1] |= ALU_V_USE_FID
;
918 static_table
[2] = (mdb
->vid
<< ALU_V_FID_S
);
919 static_table
[2] |= mac_hi
;
920 static_table
[3] = mac_lo
;
922 write_table(ds
, static_table
);
924 data
= (index
<< ALU_STAT_INDEX_S
) | ALU_STAT_START
;
925 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
927 /* wait to be finished */
928 if (wait_alu_sta_ready(dev
, ALU_STAT_START
, 1000) < 0)
929 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
932 mutex_unlock(&dev
->alu_mutex
);
935 static int ksz_port_mdb_del(struct dsa_switch
*ds
, int port
,
936 const struct switchdev_obj_port_mdb
*mdb
)
938 struct ksz_device
*dev
= ds
->priv
;
945 mac_hi
= ((mdb
->addr
[0] << 8) | mdb
->addr
[1]);
946 mac_lo
= ((mdb
->addr
[2] << 24) | (mdb
->addr
[3] << 16));
947 mac_lo
|= ((mdb
->addr
[4] << 8) | mdb
->addr
[5]);
949 mutex_lock(&dev
->alu_mutex
);
951 for (index
= 0; index
< dev
->num_statics
; index
++) {
952 /* find empty slot first */
953 data
= (index
<< ALU_STAT_INDEX_S
) |
954 ALU_STAT_READ
| ALU_STAT_START
;
955 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
957 /* wait to be finished */
958 ret
= wait_alu_sta_ready(dev
, ALU_STAT_START
, 1000);
960 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
964 /* read ALU static table */
965 read_table(ds
, static_table
);
967 if (static_table
[0] & ALU_V_STATIC_VALID
) {
968 /* check this has same vid & mac address */
970 if (((static_table
[2] >> ALU_V_FID_S
) == (mdb
->vid
)) &&
971 ((static_table
[2] & ALU_V_MAC_ADDR_HI
) == mac_hi
) &&
972 (static_table
[3] == mac_lo
)) {
973 /* found matching one */
979 /* no available entry */
980 if (index
== dev
->num_statics
) {
986 static_table
[1] &= ~BIT(port
);
988 if ((static_table
[1] & ALU_V_PORT_MAP
) == 0) {
996 write_table(ds
, static_table
);
998 data
= (index
<< ALU_STAT_INDEX_S
) | ALU_STAT_START
;
999 ksz_write32(dev
, REG_SW_ALU_STAT_CTRL__4
, data
);
1001 /* wait to be finished */
1002 ret
= wait_alu_sta_ready(dev
, ALU_STAT_START
, 1000);
1004 dev_dbg(dev
->dev
, "Failed to read ALU STATIC\n");
1007 mutex_unlock(&dev
->alu_mutex
);
1012 static int ksz_port_mirror_add(struct dsa_switch
*ds
, int port
,
1013 struct dsa_mall_mirror_tc_entry
*mirror
,
1016 struct ksz_device
*dev
= ds
->priv
;
1019 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_RX
, true);
1021 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_TX
, true);
1023 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_SNIFFER
, false);
1025 /* configure mirror port */
1026 ksz_port_cfg(dev
, mirror
->to_local_port
, P_MIRROR_CTRL
,
1027 PORT_MIRROR_SNIFFER
, true);
1029 ksz_cfg(dev
, S_MIRROR_CTRL
, SW_MIRROR_RX_TX
, false);
1034 static void ksz_port_mirror_del(struct dsa_switch
*ds
, int port
,
1035 struct dsa_mall_mirror_tc_entry
*mirror
)
1037 struct ksz_device
*dev
= ds
->priv
;
1040 if (mirror
->ingress
)
1041 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_RX
, false);
1043 ksz_port_cfg(dev
, port
, P_MIRROR_CTRL
, PORT_MIRROR_TX
, false);
1045 ksz_pread8(dev
, port
, P_MIRROR_CTRL
, &data
);
1047 if (!(data
& (PORT_MIRROR_RX
| PORT_MIRROR_TX
)))
1048 ksz_port_cfg(dev
, mirror
->to_local_port
, P_MIRROR_CTRL
,
1049 PORT_MIRROR_SNIFFER
, false);
1052 static const struct dsa_switch_ops ksz_switch_ops
= {
1053 .get_tag_protocol
= ksz_get_tag_protocol
,
1055 .phy_read
= ksz_phy_read16
,
1056 .phy_write
= ksz_phy_write16
,
1057 .port_enable
= ksz_enable_port
,
1058 .port_disable
= ksz_disable_port
,
1059 .get_strings
= ksz_get_strings
,
1060 .get_ethtool_stats
= ksz_get_ethtool_stats
,
1061 .get_sset_count
= ksz_sset_count
,
1062 .port_stp_state_set
= ksz_port_stp_state_set
,
1063 .port_fast_age
= ksz_port_fast_age
,
1064 .port_vlan_filtering
= ksz_port_vlan_filtering
,
1065 .port_vlan_prepare
= ksz_port_vlan_prepare
,
1066 .port_vlan_add
= ksz_port_vlan_add
,
1067 .port_vlan_del
= ksz_port_vlan_del
,
1068 .port_fdb_dump
= ksz_port_fdb_dump
,
1069 .port_fdb_add
= ksz_port_fdb_add
,
1070 .port_fdb_del
= ksz_port_fdb_del
,
1071 .port_mdb_prepare
= ksz_port_mdb_prepare
,
1072 .port_mdb_add
= ksz_port_mdb_add
,
1073 .port_mdb_del
= ksz_port_mdb_del
,
1074 .port_mirror_add
= ksz_port_mirror_add
,
1075 .port_mirror_del
= ksz_port_mirror_del
,
1078 struct ksz_chip_data
{
1080 const char *dev_name
;
1088 static const struct ksz_chip_data ksz_switch_chips
[] = {
1090 .chip_id
= 0x00947700,
1091 .dev_name
= "KSZ9477",
1095 .cpu_ports
= 0x7F, /* can be configured as cpu port */
1096 .port_cnt
= 7, /* total physical port count */
1100 static int ksz_switch_init(struct ksz_device
*dev
)
1104 mutex_init(&dev
->reg_mutex
);
1105 mutex_init(&dev
->stats_mutex
);
1106 mutex_init(&dev
->alu_mutex
);
1107 mutex_init(&dev
->vlan_mutex
);
1109 dev
->ds
->ops
= &ksz_switch_ops
;
1111 for (i
= 0; i
< ARRAY_SIZE(ksz_switch_chips
); i
++) {
1112 const struct ksz_chip_data
*chip
= &ksz_switch_chips
[i
];
1114 if (dev
->chip_id
== chip
->chip_id
) {
1115 dev
->name
= chip
->dev_name
;
1116 dev
->num_vlans
= chip
->num_vlans
;
1117 dev
->num_alus
= chip
->num_alus
;
1118 dev
->num_statics
= chip
->num_statics
;
1119 dev
->port_cnt
= chip
->port_cnt
;
1120 dev
->cpu_ports
= chip
->cpu_ports
;
1126 /* no switch found */
1133 struct ksz_device
*ksz_switch_alloc(struct device
*base
,
1134 const struct ksz_io_ops
*ops
,
1137 struct dsa_switch
*ds
;
1138 struct ksz_device
*swdev
;
1140 ds
= dsa_switch_alloc(base
, DSA_MAX_PORTS
);
1144 swdev
= devm_kzalloc(base
, sizeof(*swdev
), GFP_KERNEL
);
1157 EXPORT_SYMBOL(ksz_switch_alloc
);
1159 int ksz_switch_detect(struct ksz_device
*dev
)
1165 /* turn off SPI DO Edge select */
1166 ret
= ksz_read8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, &data8
);
1170 data8
&= ~SPI_AUTO_EDGE_DETECTION
;
1171 ret
= ksz_write8(dev
, REG_SW_GLOBAL_SERIAL_CTRL_0
, data8
);
1176 ret
= ksz_read32(dev
, REG_CHIP_ID0__1
, &id32
);
1180 dev
->chip_id
= id32
;
1184 EXPORT_SYMBOL(ksz_switch_detect
);
1186 int ksz_switch_register(struct ksz_device
*dev
)
1191 dev
->chip_id
= dev
->pdata
->chip_id
;
1193 if (ksz_switch_detect(dev
))
1196 ret
= ksz_switch_init(dev
);
1200 return dsa_register_switch(dev
->ds
);
1202 EXPORT_SYMBOL(ksz_switch_register
);
1204 void ksz_switch_remove(struct ksz_device
*dev
)
1206 dsa_unregister_switch(dev
->ds
);
1208 EXPORT_SYMBOL(ksz_switch_remove
);
1210 MODULE_AUTHOR("Woojung Huh <Woojung.Huh@microchip.com>");
1211 MODULE_DESCRIPTION("Microchip KSZ Series Switch DSA Driver");
1212 MODULE_LICENSE("GPL");