2 * Copyright (C) 2017 Pengutronix, Juergen Borleis <kernel@pengutronix.de>
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * version 2, as published by the Free Software Foundation.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #include <linux/kernel.h>
15 #include <linux/module.h>
16 #include <linux/gpio/consumer.h>
17 #include <linux/regmap.h>
18 #include <linux/mutex.h>
19 #include <linux/mii.h>
20 #include <linux/phy.h>
21 #include <linux/if_bridge.h>
22 #include <linux/etherdevice.h>
26 #define LAN9303_NUM_PORTS 3
28 /* 13.2 System Control and Status Registers
29 * Multiply register number by 4 to get address offset.
31 #define LAN9303_CHIP_REV 0x14
32 # define LAN9303_CHIP_ID 0x9303
33 #define LAN9303_IRQ_CFG 0x15
34 # define LAN9303_IRQ_CFG_IRQ_ENABLE BIT(8)
35 # define LAN9303_IRQ_CFG_IRQ_POL BIT(4)
36 # define LAN9303_IRQ_CFG_IRQ_TYPE BIT(0)
37 #define LAN9303_INT_STS 0x16
38 # define LAN9303_INT_STS_PHY_INT2 BIT(27)
39 # define LAN9303_INT_STS_PHY_INT1 BIT(26)
40 #define LAN9303_INT_EN 0x17
41 # define LAN9303_INT_EN_PHY_INT2_EN BIT(27)
42 # define LAN9303_INT_EN_PHY_INT1_EN BIT(26)
43 #define LAN9303_HW_CFG 0x1D
44 # define LAN9303_HW_CFG_READY BIT(27)
45 # define LAN9303_HW_CFG_AMDX_EN_PORT2 BIT(26)
46 # define LAN9303_HW_CFG_AMDX_EN_PORT1 BIT(25)
47 #define LAN9303_PMI_DATA 0x29
48 #define LAN9303_PMI_ACCESS 0x2A
49 # define LAN9303_PMI_ACCESS_PHY_ADDR(x) (((x) & 0x1f) << 11)
50 # define LAN9303_PMI_ACCESS_MIIRINDA(x) (((x) & 0x1f) << 6)
51 # define LAN9303_PMI_ACCESS_MII_BUSY BIT(0)
52 # define LAN9303_PMI_ACCESS_MII_WRITE BIT(1)
53 #define LAN9303_MANUAL_FC_1 0x68
54 #define LAN9303_MANUAL_FC_2 0x69
55 #define LAN9303_MANUAL_FC_0 0x6a
56 #define LAN9303_SWITCH_CSR_DATA 0x6b
57 #define LAN9303_SWITCH_CSR_CMD 0x6c
58 #define LAN9303_SWITCH_CSR_CMD_BUSY BIT(31)
59 #define LAN9303_SWITCH_CSR_CMD_RW BIT(30)
60 #define LAN9303_SWITCH_CSR_CMD_LANES (BIT(19) | BIT(18) | BIT(17) | BIT(16))
61 #define LAN9303_VIRT_PHY_BASE 0x70
62 #define LAN9303_VIRT_SPECIAL_CTRL 0x77
63 #define LAN9303_VIRT_SPECIAL_TURBO BIT(10) /*Turbo MII Enable*/
65 /*13.4 Switch Fabric Control and Status Registers
66 * Accessed indirectly via SWITCH_CSR_CMD, SWITCH_CSR_DATA.
68 #define LAN9303_SW_DEV_ID 0x0000
69 #define LAN9303_SW_RESET 0x0001
70 #define LAN9303_SW_RESET_RESET BIT(0)
71 #define LAN9303_SW_IMR 0x0004
72 #define LAN9303_SW_IPR 0x0005
73 #define LAN9303_MAC_VER_ID_0 0x0400
74 #define LAN9303_MAC_RX_CFG_0 0x0401
75 # define LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES BIT(1)
76 # define LAN9303_MAC_RX_CFG_X_RX_ENABLE BIT(0)
77 #define LAN9303_MAC_RX_UNDSZE_CNT_0 0x0410
78 #define LAN9303_MAC_RX_64_CNT_0 0x0411
79 #define LAN9303_MAC_RX_127_CNT_0 0x0412
80 #define LAN9303_MAC_RX_255_CNT_0 0x413
81 #define LAN9303_MAC_RX_511_CNT_0 0x0414
82 #define LAN9303_MAC_RX_1023_CNT_0 0x0415
83 #define LAN9303_MAC_RX_MAX_CNT_0 0x0416
84 #define LAN9303_MAC_RX_OVRSZE_CNT_0 0x0417
85 #define LAN9303_MAC_RX_PKTOK_CNT_0 0x0418
86 #define LAN9303_MAC_RX_CRCERR_CNT_0 0x0419
87 #define LAN9303_MAC_RX_MULCST_CNT_0 0x041a
88 #define LAN9303_MAC_RX_BRDCST_CNT_0 0x041b
89 #define LAN9303_MAC_RX_PAUSE_CNT_0 0x041c
90 #define LAN9303_MAC_RX_FRAG_CNT_0 0x041d
91 #define LAN9303_MAC_RX_JABB_CNT_0 0x041e
92 #define LAN9303_MAC_RX_ALIGN_CNT_0 0x041f
93 #define LAN9303_MAC_RX_PKTLEN_CNT_0 0x0420
94 #define LAN9303_MAC_RX_GOODPKTLEN_CNT_0 0x0421
95 #define LAN9303_MAC_RX_SYMBL_CNT_0 0x0422
96 #define LAN9303_MAC_RX_CTLFRM_CNT_0 0x0423
98 #define LAN9303_MAC_TX_CFG_0 0x0440
99 # define LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT (21 << 2)
100 # define LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE BIT(1)
101 # define LAN9303_MAC_TX_CFG_X_TX_ENABLE BIT(0)
102 #define LAN9303_MAC_TX_DEFER_CNT_0 0x0451
103 #define LAN9303_MAC_TX_PAUSE_CNT_0 0x0452
104 #define LAN9303_MAC_TX_PKTOK_CNT_0 0x0453
105 #define LAN9303_MAC_TX_64_CNT_0 0x0454
106 #define LAN9303_MAC_TX_127_CNT_0 0x0455
107 #define LAN9303_MAC_TX_255_CNT_0 0x0456
108 #define LAN9303_MAC_TX_511_CNT_0 0x0457
109 #define LAN9303_MAC_TX_1023_CNT_0 0x0458
110 #define LAN9303_MAC_TX_MAX_CNT_0 0x0459
111 #define LAN9303_MAC_TX_UNDSZE_CNT_0 0x045a
112 #define LAN9303_MAC_TX_PKTLEN_CNT_0 0x045c
113 #define LAN9303_MAC_TX_BRDCST_CNT_0 0x045d
114 #define LAN9303_MAC_TX_MULCST_CNT_0 0x045e
115 #define LAN9303_MAC_TX_LATECOL_0 0x045f
116 #define LAN9303_MAC_TX_EXCOL_CNT_0 0x0460
117 #define LAN9303_MAC_TX_SNGLECOL_CNT_0 0x0461
118 #define LAN9303_MAC_TX_MULTICOL_CNT_0 0x0462
119 #define LAN9303_MAC_TX_TOTALCOL_CNT_0 0x0463
121 #define LAN9303_MAC_VER_ID_1 0x0800
122 #define LAN9303_MAC_RX_CFG_1 0x0801
123 #define LAN9303_MAC_TX_CFG_1 0x0840
124 #define LAN9303_MAC_VER_ID_2 0x0c00
125 #define LAN9303_MAC_RX_CFG_2 0x0c01
126 #define LAN9303_MAC_TX_CFG_2 0x0c40
127 #define LAN9303_SWE_ALR_CMD 0x1800
128 # define LAN9303_ALR_CMD_MAKE_ENTRY BIT(2)
129 # define LAN9303_ALR_CMD_GET_FIRST BIT(1)
130 # define LAN9303_ALR_CMD_GET_NEXT BIT(0)
131 #define LAN9303_SWE_ALR_WR_DAT_0 0x1801
132 #define LAN9303_SWE_ALR_WR_DAT_1 0x1802
133 # define LAN9303_ALR_DAT1_VALID BIT(26)
134 # define LAN9303_ALR_DAT1_END_OF_TABL BIT(25)
135 # define LAN9303_ALR_DAT1_AGE_OVERRID BIT(25)
136 # define LAN9303_ALR_DAT1_STATIC BIT(24)
137 # define LAN9303_ALR_DAT1_PORT_BITOFFS 16
138 # define LAN9303_ALR_DAT1_PORT_MASK (7 << LAN9303_ALR_DAT1_PORT_BITOFFS)
139 #define LAN9303_SWE_ALR_RD_DAT_0 0x1805
140 #define LAN9303_SWE_ALR_RD_DAT_1 0x1806
141 #define LAN9303_SWE_ALR_CMD_STS 0x1808
142 # define ALR_STS_MAKE_PEND BIT(0)
143 #define LAN9303_SWE_VLAN_CMD 0x180b
144 # define LAN9303_SWE_VLAN_CMD_RNW BIT(5)
145 # define LAN9303_SWE_VLAN_CMD_PVIDNVLAN BIT(4)
146 #define LAN9303_SWE_VLAN_WR_DATA 0x180c
147 #define LAN9303_SWE_VLAN_RD_DATA 0x180e
148 # define LAN9303_SWE_VLAN_MEMBER_PORT2 BIT(17)
149 # define LAN9303_SWE_VLAN_UNTAG_PORT2 BIT(16)
150 # define LAN9303_SWE_VLAN_MEMBER_PORT1 BIT(15)
151 # define LAN9303_SWE_VLAN_UNTAG_PORT1 BIT(14)
152 # define LAN9303_SWE_VLAN_MEMBER_PORT0 BIT(13)
153 # define LAN9303_SWE_VLAN_UNTAG_PORT0 BIT(12)
154 #define LAN9303_SWE_VLAN_CMD_STS 0x1810
155 #define LAN9303_SWE_GLB_INGRESS_CFG 0x1840
156 # define LAN9303_SWE_GLB_INGR_IGMP_TRAP BIT(7)
157 # define LAN9303_SWE_GLB_INGR_IGMP_PORT(p) BIT(10 + p)
158 #define LAN9303_SWE_PORT_STATE 0x1843
159 # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT2 (0)
160 # define LAN9303_SWE_PORT_STATE_LEARNING_PORT2 BIT(5)
161 # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT2 BIT(4)
162 # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT1 (0)
163 # define LAN9303_SWE_PORT_STATE_LEARNING_PORT1 BIT(3)
164 # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT1 BIT(2)
165 # define LAN9303_SWE_PORT_STATE_FORWARDING_PORT0 (0)
166 # define LAN9303_SWE_PORT_STATE_LEARNING_PORT0 BIT(1)
167 # define LAN9303_SWE_PORT_STATE_BLOCKING_PORT0 BIT(0)
168 # define LAN9303_SWE_PORT_STATE_DISABLED_PORT0 (3)
169 #define LAN9303_SWE_PORT_MIRROR 0x1846
170 # define LAN9303_SWE_PORT_MIRROR_SNIFF_ALL BIT(8)
171 # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT2 BIT(7)
172 # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT1 BIT(6)
173 # define LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0 BIT(5)
174 # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2 BIT(4)
175 # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1 BIT(3)
176 # define LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT0 BIT(2)
177 # define LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING BIT(1)
178 # define LAN9303_SWE_PORT_MIRROR_ENABLE_TX_MIRRORING BIT(0)
179 # define LAN9303_SWE_PORT_MIRROR_DISABLED 0
180 #define LAN9303_SWE_INGRESS_PORT_TYPE 0x1847
181 #define LAN9303_SWE_INGRESS_PORT_TYPE_VLAN 3
182 #define LAN9303_BM_CFG 0x1c00
183 #define LAN9303_BM_EGRSS_PORT_TYPE 0x1c0c
184 # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT2 (BIT(17) | BIT(16))
185 # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT1 (BIT(9) | BIT(8))
186 # define LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0 (BIT(1) | BIT(0))
188 #define LAN9303_SWITCH_PORT_REG(port, reg0) (0x400 * (port) + (reg0))
190 /* the built-in PHYs are of type LAN911X */
191 #define MII_LAN911X_SPECIAL_MODES 0x12
192 #define MII_LAN911X_SPECIAL_CONTROL_STATUS 0x1f
194 static const struct regmap_range lan9303_valid_regs
[] = {
195 regmap_reg_range(0x14, 0x17), /* misc, interrupt */
196 regmap_reg_range(0x19, 0x19), /* endian test */
197 regmap_reg_range(0x1d, 0x1d), /* hardware config */
198 regmap_reg_range(0x23, 0x24), /* general purpose timer */
199 regmap_reg_range(0x27, 0x27), /* counter */
200 regmap_reg_range(0x29, 0x2a), /* PMI index regs */
201 regmap_reg_range(0x68, 0x6a), /* flow control */
202 regmap_reg_range(0x6b, 0x6c), /* switch fabric indirect regs */
203 regmap_reg_range(0x6d, 0x6f), /* misc */
204 regmap_reg_range(0x70, 0x77), /* virtual phy */
205 regmap_reg_range(0x78, 0x7a), /* GPIO */
206 regmap_reg_range(0x7c, 0x7e), /* MAC & reset */
207 regmap_reg_range(0x80, 0xb7), /* switch fabric direct regs (wr only) */
210 static const struct regmap_range lan9303_reserved_ranges
[] = {
211 regmap_reg_range(0x00, 0x13),
212 regmap_reg_range(0x18, 0x18),
213 regmap_reg_range(0x1a, 0x1c),
214 regmap_reg_range(0x1e, 0x22),
215 regmap_reg_range(0x25, 0x26),
216 regmap_reg_range(0x28, 0x28),
217 regmap_reg_range(0x2b, 0x67),
218 regmap_reg_range(0x7b, 0x7b),
219 regmap_reg_range(0x7f, 0x7f),
220 regmap_reg_range(0xb8, 0xff),
223 const struct regmap_access_table lan9303_register_set
= {
224 .yes_ranges
= lan9303_valid_regs
,
225 .n_yes_ranges
= ARRAY_SIZE(lan9303_valid_regs
),
226 .no_ranges
= lan9303_reserved_ranges
,
227 .n_no_ranges
= ARRAY_SIZE(lan9303_reserved_ranges
),
229 EXPORT_SYMBOL(lan9303_register_set
);
231 static int lan9303_read(struct regmap
*regmap
, unsigned int offset
, u32
*reg
)
235 /* we can lose arbitration for the I2C case, because the device
236 * tries to detect and read an external EEPROM after reset and acts as
237 * a master on the shared I2C bus itself. This conflicts with our
238 * attempts to access the device as a slave at the same moment.
240 for (i
= 0; i
< 5; i
++) {
241 ret
= regmap_read(regmap
, offset
, reg
);
252 static int lan9303_read_wait(struct lan9303
*chip
, int offset
, u32 mask
)
256 for (i
= 0; i
< 25; i
++) {
260 ret
= lan9303_read(chip
->regmap
, offset
, ®
);
262 dev_err(chip
->dev
, "%s failed to read offset %d: %d\n",
263 __func__
, offset
, ret
);
268 usleep_range(1000, 2000);
274 static int lan9303_virt_phy_reg_read(struct lan9303
*chip
, int regnum
)
279 if (regnum
> MII_EXPANSION
)
282 ret
= lan9303_read(chip
->regmap
, LAN9303_VIRT_PHY_BASE
+ regnum
, &val
);
289 static int lan9303_virt_phy_reg_write(struct lan9303
*chip
, int regnum
, u16 val
)
291 if (regnum
> MII_EXPANSION
)
294 return regmap_write(chip
->regmap
, LAN9303_VIRT_PHY_BASE
+ regnum
, val
);
297 static int lan9303_indirect_phy_wait_for_completion(struct lan9303
*chip
)
299 return lan9303_read_wait(chip
, LAN9303_PMI_ACCESS
,
300 LAN9303_PMI_ACCESS_MII_BUSY
);
303 static int lan9303_indirect_phy_read(struct lan9303
*chip
, int addr
, int regnum
)
308 val
= LAN9303_PMI_ACCESS_PHY_ADDR(addr
);
309 val
|= LAN9303_PMI_ACCESS_MIIRINDA(regnum
);
311 mutex_lock(&chip
->indirect_mutex
);
313 ret
= lan9303_indirect_phy_wait_for_completion(chip
);
317 /* start the MII read cycle */
318 ret
= regmap_write(chip
->regmap
, LAN9303_PMI_ACCESS
, val
);
322 ret
= lan9303_indirect_phy_wait_for_completion(chip
);
326 /* read the result of this operation */
327 ret
= lan9303_read(chip
->regmap
, LAN9303_PMI_DATA
, &val
);
331 mutex_unlock(&chip
->indirect_mutex
);
336 mutex_unlock(&chip
->indirect_mutex
);
340 static int lan9303_indirect_phy_write(struct lan9303
*chip
, int addr
,
346 reg
= LAN9303_PMI_ACCESS_PHY_ADDR(addr
);
347 reg
|= LAN9303_PMI_ACCESS_MIIRINDA(regnum
);
348 reg
|= LAN9303_PMI_ACCESS_MII_WRITE
;
350 mutex_lock(&chip
->indirect_mutex
);
352 ret
= lan9303_indirect_phy_wait_for_completion(chip
);
356 /* write the data first... */
357 ret
= regmap_write(chip
->regmap
, LAN9303_PMI_DATA
, val
);
361 /* ...then start the MII write cycle */
362 ret
= regmap_write(chip
->regmap
, LAN9303_PMI_ACCESS
, reg
);
365 mutex_unlock(&chip
->indirect_mutex
);
369 const struct lan9303_phy_ops lan9303_indirect_phy_ops
= {
370 .phy_read
= lan9303_indirect_phy_read
,
371 .phy_write
= lan9303_indirect_phy_write
,
373 EXPORT_SYMBOL_GPL(lan9303_indirect_phy_ops
);
375 static int lan9303_switch_wait_for_completion(struct lan9303
*chip
)
377 return lan9303_read_wait(chip
, LAN9303_SWITCH_CSR_CMD
,
378 LAN9303_SWITCH_CSR_CMD_BUSY
);
381 static int lan9303_write_switch_reg(struct lan9303
*chip
, u16 regnum
, u32 val
)
387 reg
|= LAN9303_SWITCH_CSR_CMD_LANES
;
388 reg
|= LAN9303_SWITCH_CSR_CMD_BUSY
;
390 mutex_lock(&chip
->indirect_mutex
);
392 ret
= lan9303_switch_wait_for_completion(chip
);
396 ret
= regmap_write(chip
->regmap
, LAN9303_SWITCH_CSR_DATA
, val
);
398 dev_err(chip
->dev
, "Failed to write csr data reg: %d\n", ret
);
403 ret
= regmap_write(chip
->regmap
, LAN9303_SWITCH_CSR_CMD
, reg
);
405 dev_err(chip
->dev
, "Failed to write csr command reg: %d\n",
409 mutex_unlock(&chip
->indirect_mutex
);
413 static int lan9303_read_switch_reg(struct lan9303
*chip
, u16 regnum
, u32
*val
)
419 reg
|= LAN9303_SWITCH_CSR_CMD_LANES
;
420 reg
|= LAN9303_SWITCH_CSR_CMD_RW
;
421 reg
|= LAN9303_SWITCH_CSR_CMD_BUSY
;
423 mutex_lock(&chip
->indirect_mutex
);
425 ret
= lan9303_switch_wait_for_completion(chip
);
430 ret
= regmap_write(chip
->regmap
, LAN9303_SWITCH_CSR_CMD
, reg
);
432 dev_err(chip
->dev
, "Failed to write csr command reg: %d\n",
437 ret
= lan9303_switch_wait_for_completion(chip
);
441 ret
= lan9303_read(chip
->regmap
, LAN9303_SWITCH_CSR_DATA
, val
);
443 dev_err(chip
->dev
, "Failed to read csr data reg: %d\n", ret
);
445 mutex_unlock(&chip
->indirect_mutex
);
449 static int lan9303_write_switch_reg_mask(struct lan9303
*chip
, u16 regnum
,
455 ret
= lan9303_read_switch_reg(chip
, regnum
, ®
);
459 reg
= (reg
& ~mask
) | val
;
461 return lan9303_write_switch_reg(chip
, regnum
, reg
);
464 static int lan9303_write_switch_port(struct lan9303
*chip
, int port
,
467 return lan9303_write_switch_reg(
468 chip
, LAN9303_SWITCH_PORT_REG(port
, regnum
), val
);
471 static int lan9303_read_switch_port(struct lan9303
*chip
, int port
,
472 u16 regnum
, u32
*val
)
474 return lan9303_read_switch_reg(
475 chip
, LAN9303_SWITCH_PORT_REG(port
, regnum
), val
);
478 static int lan9303_detect_phy_setup(struct lan9303
*chip
)
482 /* Calculate chip->phy_addr_base:
483 * Depending on the 'phy_addr_sel_strap' setting, the three phys are
484 * using IDs 0-1-2 or IDs 1-2-3. We cannot read back the
485 * 'phy_addr_sel_strap' setting directly, so we need a test, which
486 * configuration is active:
487 * Special reg 18 of phy 3 reads as 0x0000, if 'phy_addr_sel_strap' is 0
488 * and the IDs are 0-1-2, else it contains something different from
489 * 0x0000, which means 'phy_addr_sel_strap' is 1 and the IDs are 1-2-3.
490 * 0xffff is returned on MDIO read with no response.
492 reg
= chip
->ops
->phy_read(chip
, 3, MII_LAN911X_SPECIAL_MODES
);
494 dev_err(chip
->dev
, "Failed to detect phy config: %d\n", reg
);
498 chip
->phy_addr_base
= reg
!= 0 && reg
!= 0xffff;
500 dev_dbg(chip
->dev
, "Phy setup '%s' detected\n",
501 chip
->phy_addr_base
? "1-2-3" : "0-1-2");
506 /* Map ALR-port bits to port bitmap, and back */
507 static const int alrport_2_portmap
[] = {1, 2, 4, 0, 3, 5, 6, 7 };
508 static const int portmap_2_alrport
[] = {3, 0, 1, 4, 2, 5, 6, 7 };
510 /* Return pointer to first free ALR cache entry, return NULL if none */
511 static struct lan9303_alr_cache_entry
*
512 lan9303_alr_cache_find_free(struct lan9303
*chip
)
515 struct lan9303_alr_cache_entry
*entr
= chip
->alr_cache
;
517 for (i
= 0; i
< LAN9303_NUM_ALR_RECORDS
; i
++, entr
++)
518 if (entr
->port_map
== 0)
524 /* Return pointer to ALR cache entry matching MAC address */
525 static struct lan9303_alr_cache_entry
*
526 lan9303_alr_cache_find_mac(struct lan9303
*chip
, const u8
*mac_addr
)
529 struct lan9303_alr_cache_entry
*entr
= chip
->alr_cache
;
531 BUILD_BUG_ON_MSG(sizeof(struct lan9303_alr_cache_entry
) & 1,
532 "ether_addr_equal require u16 alignment");
534 for (i
= 0; i
< LAN9303_NUM_ALR_RECORDS
; i
++, entr
++)
535 if (ether_addr_equal(entr
->mac_addr
, mac_addr
))
541 static int lan9303_csr_reg_wait(struct lan9303
*chip
, int regno
, u32 mask
)
545 for (i
= 0; i
< 25; i
++) {
548 lan9303_read_switch_reg(chip
, regno
, ®
);
551 usleep_range(1000, 2000);
557 static int lan9303_alr_make_entry_raw(struct lan9303
*chip
, u32 dat0
, u32 dat1
)
559 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_WR_DAT_0
, dat0
);
560 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_WR_DAT_1
, dat1
);
561 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
,
562 LAN9303_ALR_CMD_MAKE_ENTRY
);
563 lan9303_csr_reg_wait(chip
, LAN9303_SWE_ALR_CMD_STS
, ALR_STS_MAKE_PEND
);
564 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
, 0);
569 typedef void alr_loop_cb_t(struct lan9303
*chip
, u32 dat0
, u32 dat1
,
570 int portmap
, void *ctx
);
572 static void lan9303_alr_loop(struct lan9303
*chip
, alr_loop_cb_t
*cb
, void *ctx
)
576 mutex_lock(&chip
->alr_mutex
);
577 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
,
578 LAN9303_ALR_CMD_GET_FIRST
);
579 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
, 0);
581 for (i
= 1; i
< LAN9303_NUM_ALR_RECORDS
; i
++) {
583 int alrport
, portmap
;
585 lan9303_read_switch_reg(chip
, LAN9303_SWE_ALR_RD_DAT_0
, &dat0
);
586 lan9303_read_switch_reg(chip
, LAN9303_SWE_ALR_RD_DAT_1
, &dat1
);
587 if (dat1
& LAN9303_ALR_DAT1_END_OF_TABL
)
590 alrport
= (dat1
& LAN9303_ALR_DAT1_PORT_MASK
) >>
591 LAN9303_ALR_DAT1_PORT_BITOFFS
;
592 portmap
= alrport_2_portmap
[alrport
];
594 cb(chip
, dat0
, dat1
, portmap
, ctx
);
596 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
,
597 LAN9303_ALR_CMD_GET_NEXT
);
598 lan9303_write_switch_reg(chip
, LAN9303_SWE_ALR_CMD
, 0);
600 mutex_unlock(&chip
->alr_mutex
);
603 static void alr_reg_to_mac(u32 dat0
, u32 dat1
, u8 mac
[6])
605 mac
[0] = (dat0
>> 0) & 0xff;
606 mac
[1] = (dat0
>> 8) & 0xff;
607 mac
[2] = (dat0
>> 16) & 0xff;
608 mac
[3] = (dat0
>> 24) & 0xff;
609 mac
[4] = (dat1
>> 0) & 0xff;
610 mac
[5] = (dat1
>> 8) & 0xff;
613 struct del_port_learned_ctx
{
617 /* Clear learned (non-static) entry on given port */
618 static void alr_loop_cb_del_port_learned(struct lan9303
*chip
, u32 dat0
,
619 u32 dat1
, int portmap
, void *ctx
)
621 struct del_port_learned_ctx
*del_ctx
= ctx
;
622 int port
= del_ctx
->port
;
624 if (((BIT(port
) & portmap
) == 0) || (dat1
& LAN9303_ALR_DAT1_STATIC
))
627 /* learned entries has only one port, we can just delete */
628 dat1
&= ~LAN9303_ALR_DAT1_VALID
; /* delete entry */
629 lan9303_alr_make_entry_raw(chip
, dat0
, dat1
);
632 struct port_fdb_dump_ctx
{
635 dsa_fdb_dump_cb_t
*cb
;
638 static void alr_loop_cb_fdb_port_dump(struct lan9303
*chip
, u32 dat0
,
639 u32 dat1
, int portmap
, void *ctx
)
641 struct port_fdb_dump_ctx
*dump_ctx
= ctx
;
645 if ((BIT(dump_ctx
->port
) & portmap
) == 0)
648 alr_reg_to_mac(dat0
, dat1
, mac
);
649 is_static
= !!(dat1
& LAN9303_ALR_DAT1_STATIC
);
650 dump_ctx
->cb(mac
, 0, is_static
, dump_ctx
->data
);
653 /* Set a static ALR entry. Delete entry if port_map is zero */
654 static void lan9303_alr_set_entry(struct lan9303
*chip
, const u8
*mac
,
655 u8 port_map
, bool stp_override
)
657 u32 dat0
, dat1
, alr_port
;
659 dev_dbg(chip
->dev
, "%s(%pM, %d)\n", __func__
, mac
, port_map
);
660 dat1
= LAN9303_ALR_DAT1_STATIC
;
662 dat1
|= LAN9303_ALR_DAT1_VALID
;
663 /* otherwise no ports: delete entry */
665 dat1
|= LAN9303_ALR_DAT1_AGE_OVERRID
;
667 alr_port
= portmap_2_alrport
[port_map
& 7];
668 dat1
&= ~LAN9303_ALR_DAT1_PORT_MASK
;
669 dat1
|= alr_port
<< LAN9303_ALR_DAT1_PORT_BITOFFS
;
672 dat0
|= (mac
[0] << 0);
673 dat0
|= (mac
[1] << 8);
674 dat0
|= (mac
[2] << 16);
675 dat0
|= (mac
[3] << 24);
677 dat1
|= (mac
[4] << 0);
678 dat1
|= (mac
[5] << 8);
680 lan9303_alr_make_entry_raw(chip
, dat0
, dat1
);
683 /* Add port to static ALR entry, create new static entry if needed */
684 static int lan9303_alr_add_port(struct lan9303
*chip
, const u8
*mac
, int port
,
687 struct lan9303_alr_cache_entry
*entr
;
689 mutex_lock(&chip
->alr_mutex
);
690 entr
= lan9303_alr_cache_find_mac(chip
, mac
);
691 if (!entr
) { /*New entry */
692 entr
= lan9303_alr_cache_find_free(chip
);
694 mutex_unlock(&chip
->alr_mutex
);
697 ether_addr_copy(entr
->mac_addr
, mac
);
699 entr
->port_map
|= BIT(port
);
700 entr
->stp_override
= stp_override
;
701 lan9303_alr_set_entry(chip
, mac
, entr
->port_map
, stp_override
);
702 mutex_unlock(&chip
->alr_mutex
);
707 /* Delete static port from ALR entry, delete entry if last port */
708 static int lan9303_alr_del_port(struct lan9303
*chip
, const u8
*mac
, int port
)
710 struct lan9303_alr_cache_entry
*entr
;
712 mutex_lock(&chip
->alr_mutex
);
713 entr
= lan9303_alr_cache_find_mac(chip
, mac
);
715 goto out
; /* no static entry found */
717 entr
->port_map
&= ~BIT(port
);
718 if (entr
->port_map
== 0) /* zero means its free again */
719 eth_zero_addr(entr
->mac_addr
);
720 lan9303_alr_set_entry(chip
, mac
, entr
->port_map
, entr
->stp_override
);
723 mutex_unlock(&chip
->alr_mutex
);
727 static int lan9303_disable_processing_port(struct lan9303
*chip
,
732 /* disable RX, but keep register reset default values else */
733 ret
= lan9303_write_switch_port(chip
, port
, LAN9303_MAC_RX_CFG_0
,
734 LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES
);
738 /* disable TX, but keep register reset default values else */
739 return lan9303_write_switch_port(chip
, port
, LAN9303_MAC_TX_CFG_0
,
740 LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT
|
741 LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE
);
744 static int lan9303_enable_processing_port(struct lan9303
*chip
,
749 /* enable RX and keep register reset default values else */
750 ret
= lan9303_write_switch_port(chip
, port
, LAN9303_MAC_RX_CFG_0
,
751 LAN9303_MAC_RX_CFG_X_REJECT_MAC_TYPES
|
752 LAN9303_MAC_RX_CFG_X_RX_ENABLE
);
756 /* enable TX and keep register reset default values else */
757 return lan9303_write_switch_port(chip
, port
, LAN9303_MAC_TX_CFG_0
,
758 LAN9303_MAC_TX_CFG_X_TX_IFG_CONFIG_DEFAULT
|
759 LAN9303_MAC_TX_CFG_X_TX_PAD_ENABLE
|
760 LAN9303_MAC_TX_CFG_X_TX_ENABLE
);
763 /* forward special tagged packets from port 0 to port 1 *or* port 2 */
764 static int lan9303_setup_tagging(struct lan9303
*chip
)
768 /* enable defining the destination port via special VLAN tagging
771 ret
= lan9303_write_switch_reg(chip
, LAN9303_SWE_INGRESS_PORT_TYPE
,
772 LAN9303_SWE_INGRESS_PORT_TYPE_VLAN
);
776 /* tag incoming packets at port 1 and 2 on their way to port 0 to be
777 * able to discover their source port
779 val
= LAN9303_BM_EGRSS_PORT_TYPE_SPECIAL_TAG_PORT0
;
780 return lan9303_write_switch_reg(chip
, LAN9303_BM_EGRSS_PORT_TYPE
, val
);
783 /* We want a special working switch:
784 * - do not forward packets between port 1 and 2
785 * - forward everything from port 1 to port 0
786 * - forward everything from port 2 to port 0
788 static int lan9303_separate_ports(struct lan9303
*chip
)
792 lan9303_alr_del_port(chip
, eth_stp_addr
, 0);
793 ret
= lan9303_write_switch_reg(chip
, LAN9303_SWE_PORT_MIRROR
,
794 LAN9303_SWE_PORT_MIRROR_SNIFFER_PORT0
|
795 LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT1
|
796 LAN9303_SWE_PORT_MIRROR_MIRRORED_PORT2
|
797 LAN9303_SWE_PORT_MIRROR_ENABLE_RX_MIRRORING
|
798 LAN9303_SWE_PORT_MIRROR_SNIFF_ALL
);
802 /* prevent port 1 and 2 from forwarding packets by their own */
803 return lan9303_write_switch_reg(chip
, LAN9303_SWE_PORT_STATE
,
804 LAN9303_SWE_PORT_STATE_FORWARDING_PORT0
|
805 LAN9303_SWE_PORT_STATE_BLOCKING_PORT1
|
806 LAN9303_SWE_PORT_STATE_BLOCKING_PORT2
);
809 static void lan9303_bridge_ports(struct lan9303
*chip
)
811 /* ports bridged: remove mirroring */
812 lan9303_write_switch_reg(chip
, LAN9303_SWE_PORT_MIRROR
,
813 LAN9303_SWE_PORT_MIRROR_DISABLED
);
815 lan9303_write_switch_reg(chip
, LAN9303_SWE_PORT_STATE
,
816 chip
->swe_port_state
);
817 lan9303_alr_add_port(chip
, eth_stp_addr
, 0, true);
820 static void lan9303_handle_reset(struct lan9303
*chip
)
822 if (!chip
->reset_gpio
)
825 if (chip
->reset_duration
!= 0)
826 msleep(chip
->reset_duration
);
828 /* release (deassert) reset and activate the device */
829 gpiod_set_value_cansleep(chip
->reset_gpio
, 0);
832 /* stop processing packets for all ports */
833 static int lan9303_disable_processing(struct lan9303
*chip
)
837 for (p
= 1; p
< LAN9303_NUM_PORTS
; p
++) {
838 int ret
= lan9303_disable_processing_port(chip
, p
);
847 static int lan9303_check_device(struct lan9303
*chip
)
852 ret
= lan9303_read(chip
->regmap
, LAN9303_CHIP_REV
, ®
);
854 dev_err(chip
->dev
, "failed to read chip revision register: %d\n",
856 if (!chip
->reset_gpio
) {
858 "hint: maybe failed due to missing reset GPIO\n");
863 if ((reg
>> 16) != LAN9303_CHIP_ID
) {
864 dev_err(chip
->dev
, "expecting LAN9303 chip, but found: %X\n",
869 /* The default state of the LAN9303 device is to forward packets between
870 * all ports (if not configured differently by an external EEPROM).
871 * The initial state of a DSA device must be forwarding packets only
872 * between the external and the internal ports and no forwarding
873 * between the external ports. In preparation we stop packet handling
874 * at all for now until the LAN9303 device is re-programmed accordingly.
876 ret
= lan9303_disable_processing(chip
);
878 dev_warn(chip
->dev
, "failed to disable switching %d\n", ret
);
880 dev_info(chip
->dev
, "Found LAN9303 rev. %u\n", reg
& 0xffff);
882 ret
= lan9303_detect_phy_setup(chip
);
885 "failed to discover phy bootstrap setup: %d\n", ret
);
892 /* ---------------------------- DSA -----------------------------------*/
894 static enum dsa_tag_protocol
lan9303_get_tag_protocol(struct dsa_switch
*ds
,
897 return DSA_TAG_PROTO_LAN9303
;
900 static int lan9303_setup(struct dsa_switch
*ds
)
902 struct lan9303
*chip
= ds
->priv
;
905 /* Make sure that port 0 is the cpu port */
906 if (!dsa_is_cpu_port(ds
, 0)) {
907 dev_err(chip
->dev
, "port 0 is not the CPU port\n");
911 ret
= lan9303_setup_tagging(chip
);
913 dev_err(chip
->dev
, "failed to setup port tagging %d\n", ret
);
915 ret
= lan9303_separate_ports(chip
);
917 dev_err(chip
->dev
, "failed to separate ports %d\n", ret
);
919 ret
= lan9303_enable_processing_port(chip
, 0);
921 dev_err(chip
->dev
, "failed to re-enable switching %d\n", ret
);
923 /* Trap IGMP to port 0 */
924 ret
= lan9303_write_switch_reg_mask(chip
, LAN9303_SWE_GLB_INGRESS_CFG
,
925 LAN9303_SWE_GLB_INGR_IGMP_TRAP
|
926 LAN9303_SWE_GLB_INGR_IGMP_PORT(0),
927 LAN9303_SWE_GLB_INGR_IGMP_PORT(1) |
928 LAN9303_SWE_GLB_INGR_IGMP_PORT(2));
930 dev_err(chip
->dev
, "failed to setup IGMP trap %d\n", ret
);
935 struct lan9303_mib_desc
{
936 unsigned int offset
; /* offset of first MAC */
940 static const struct lan9303_mib_desc lan9303_mib
[] = {
941 { .offset
= LAN9303_MAC_RX_BRDCST_CNT_0
, .name
= "RxBroad", },
942 { .offset
= LAN9303_MAC_RX_PAUSE_CNT_0
, .name
= "RxPause", },
943 { .offset
= LAN9303_MAC_RX_MULCST_CNT_0
, .name
= "RxMulti", },
944 { .offset
= LAN9303_MAC_RX_PKTOK_CNT_0
, .name
= "RxOk", },
945 { .offset
= LAN9303_MAC_RX_CRCERR_CNT_0
, .name
= "RxCrcErr", },
946 { .offset
= LAN9303_MAC_RX_ALIGN_CNT_0
, .name
= "RxAlignErr", },
947 { .offset
= LAN9303_MAC_RX_JABB_CNT_0
, .name
= "RxJabber", },
948 { .offset
= LAN9303_MAC_RX_FRAG_CNT_0
, .name
= "RxFragment", },
949 { .offset
= LAN9303_MAC_RX_64_CNT_0
, .name
= "Rx64Byte", },
950 { .offset
= LAN9303_MAC_RX_127_CNT_0
, .name
= "Rx128Byte", },
951 { .offset
= LAN9303_MAC_RX_255_CNT_0
, .name
= "Rx256Byte", },
952 { .offset
= LAN9303_MAC_RX_511_CNT_0
, .name
= "Rx512Byte", },
953 { .offset
= LAN9303_MAC_RX_1023_CNT_0
, .name
= "Rx1024Byte", },
954 { .offset
= LAN9303_MAC_RX_MAX_CNT_0
, .name
= "RxMaxByte", },
955 { .offset
= LAN9303_MAC_RX_PKTLEN_CNT_0
, .name
= "RxByteCnt", },
956 { .offset
= LAN9303_MAC_RX_SYMBL_CNT_0
, .name
= "RxSymbolCnt", },
957 { .offset
= LAN9303_MAC_RX_CTLFRM_CNT_0
, .name
= "RxCfs", },
958 { .offset
= LAN9303_MAC_RX_OVRSZE_CNT_0
, .name
= "RxOverFlow", },
959 { .offset
= LAN9303_MAC_TX_UNDSZE_CNT_0
, .name
= "TxShort", },
960 { .offset
= LAN9303_MAC_TX_BRDCST_CNT_0
, .name
= "TxBroad", },
961 { .offset
= LAN9303_MAC_TX_PAUSE_CNT_0
, .name
= "TxPause", },
962 { .offset
= LAN9303_MAC_TX_MULCST_CNT_0
, .name
= "TxMulti", },
963 { .offset
= LAN9303_MAC_RX_UNDSZE_CNT_0
, .name
= "TxUnderRun", },
964 { .offset
= LAN9303_MAC_TX_64_CNT_0
, .name
= "Tx64Byte", },
965 { .offset
= LAN9303_MAC_TX_127_CNT_0
, .name
= "Tx128Byte", },
966 { .offset
= LAN9303_MAC_TX_255_CNT_0
, .name
= "Tx256Byte", },
967 { .offset
= LAN9303_MAC_TX_511_CNT_0
, .name
= "Tx512Byte", },
968 { .offset
= LAN9303_MAC_TX_1023_CNT_0
, .name
= "Tx1024Byte", },
969 { .offset
= LAN9303_MAC_TX_MAX_CNT_0
, .name
= "TxMaxByte", },
970 { .offset
= LAN9303_MAC_TX_PKTLEN_CNT_0
, .name
= "TxByteCnt", },
971 { .offset
= LAN9303_MAC_TX_PKTOK_CNT_0
, .name
= "TxOk", },
972 { .offset
= LAN9303_MAC_TX_TOTALCOL_CNT_0
, .name
= "TxCollision", },
973 { .offset
= LAN9303_MAC_TX_MULTICOL_CNT_0
, .name
= "TxMultiCol", },
974 { .offset
= LAN9303_MAC_TX_SNGLECOL_CNT_0
, .name
= "TxSingleCol", },
975 { .offset
= LAN9303_MAC_TX_EXCOL_CNT_0
, .name
= "TxExcCol", },
976 { .offset
= LAN9303_MAC_TX_DEFER_CNT_0
, .name
= "TxDefer", },
977 { .offset
= LAN9303_MAC_TX_LATECOL_0
, .name
= "TxLateCol", },
980 static void lan9303_get_strings(struct dsa_switch
*ds
, int port
,
981 u32 stringset
, uint8_t *data
)
985 if (stringset
!= ETH_SS_STATS
)
988 for (u
= 0; u
< ARRAY_SIZE(lan9303_mib
); u
++) {
989 strncpy(data
+ u
* ETH_GSTRING_LEN
, lan9303_mib
[u
].name
,
994 static void lan9303_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
997 struct lan9303
*chip
= ds
->priv
;
1000 for (u
= 0; u
< ARRAY_SIZE(lan9303_mib
); u
++) {
1004 ret
= lan9303_read_switch_port(
1005 chip
, port
, lan9303_mib
[u
].offset
, ®
);
1008 dev_warn(chip
->dev
, "Reading status port %d reg %u failed\n",
1009 port
, lan9303_mib
[u
].offset
);
1014 static int lan9303_get_sset_count(struct dsa_switch
*ds
, int port
, int sset
)
1016 if (sset
!= ETH_SS_STATS
)
1019 return ARRAY_SIZE(lan9303_mib
);
1022 static int lan9303_phy_read(struct dsa_switch
*ds
, int phy
, int regnum
)
1024 struct lan9303
*chip
= ds
->priv
;
1025 int phy_base
= chip
->phy_addr_base
;
1027 if (phy
== phy_base
)
1028 return lan9303_virt_phy_reg_read(chip
, regnum
);
1029 if (phy
> phy_base
+ 2)
1032 return chip
->ops
->phy_read(chip
, phy
, regnum
);
1035 static int lan9303_phy_write(struct dsa_switch
*ds
, int phy
, int regnum
,
1038 struct lan9303
*chip
= ds
->priv
;
1039 int phy_base
= chip
->phy_addr_base
;
1041 if (phy
== phy_base
)
1042 return lan9303_virt_phy_reg_write(chip
, regnum
, val
);
1043 if (phy
> phy_base
+ 2)
1046 return chip
->ops
->phy_write(chip
, phy
, regnum
, val
);
1049 static void lan9303_adjust_link(struct dsa_switch
*ds
, int port
,
1050 struct phy_device
*phydev
)
1052 struct lan9303
*chip
= ds
->priv
;
1055 if (!phy_is_pseudo_fixed_link(phydev
))
1058 ctl
= lan9303_phy_read(ds
, port
, MII_BMCR
);
1060 ctl
&= ~BMCR_ANENABLE
;
1062 if (phydev
->speed
== SPEED_100
)
1063 ctl
|= BMCR_SPEED100
;
1064 else if (phydev
->speed
== SPEED_10
)
1065 ctl
&= ~BMCR_SPEED100
;
1067 dev_err(ds
->dev
, "unsupported speed: %d\n", phydev
->speed
);
1069 if (phydev
->duplex
== DUPLEX_FULL
)
1070 ctl
|= BMCR_FULLDPLX
;
1072 ctl
&= ~BMCR_FULLDPLX
;
1074 res
= lan9303_phy_write(ds
, port
, MII_BMCR
, ctl
);
1076 if (port
== chip
->phy_addr_base
) {
1077 /* Virtual Phy: Remove Turbo 200Mbit mode */
1078 lan9303_read(chip
->regmap
, LAN9303_VIRT_SPECIAL_CTRL
, &ctl
);
1080 ctl
&= ~LAN9303_VIRT_SPECIAL_TURBO
;
1081 res
= regmap_write(chip
->regmap
,
1082 LAN9303_VIRT_SPECIAL_CTRL
, ctl
);
1086 static int lan9303_port_enable(struct dsa_switch
*ds
, int port
,
1087 struct phy_device
*phy
)
1089 struct lan9303
*chip
= ds
->priv
;
1091 return lan9303_enable_processing_port(chip
, port
);
1094 static void lan9303_port_disable(struct dsa_switch
*ds
, int port
)
1096 struct lan9303
*chip
= ds
->priv
;
1098 lan9303_disable_processing_port(chip
, port
);
1099 lan9303_phy_write(ds
, chip
->phy_addr_base
+ port
, MII_BMCR
, BMCR_PDOWN
);
1102 static int lan9303_port_bridge_join(struct dsa_switch
*ds
, int port
,
1103 struct net_device
*br
)
1105 struct lan9303
*chip
= ds
->priv
;
1107 dev_dbg(chip
->dev
, "%s(port %d)\n", __func__
, port
);
1108 if (dsa_to_port(ds
, 1)->bridge_dev
== dsa_to_port(ds
, 2)->bridge_dev
) {
1109 lan9303_bridge_ports(chip
);
1110 chip
->is_bridged
= true; /* unleash stp_state_set() */
1116 static void lan9303_port_bridge_leave(struct dsa_switch
*ds
, int port
,
1117 struct net_device
*br
)
1119 struct lan9303
*chip
= ds
->priv
;
1121 dev_dbg(chip
->dev
, "%s(port %d)\n", __func__
, port
);
1122 if (chip
->is_bridged
) {
1123 lan9303_separate_ports(chip
);
1124 chip
->is_bridged
= false;
1128 static void lan9303_port_stp_state_set(struct dsa_switch
*ds
, int port
,
1131 int portmask
, portstate
;
1132 struct lan9303
*chip
= ds
->priv
;
1134 dev_dbg(chip
->dev
, "%s(port %d, state %d)\n",
1135 __func__
, port
, state
);
1138 case BR_STATE_DISABLED
:
1139 portstate
= LAN9303_SWE_PORT_STATE_DISABLED_PORT0
;
1141 case BR_STATE_BLOCKING
:
1142 case BR_STATE_LISTENING
:
1143 portstate
= LAN9303_SWE_PORT_STATE_BLOCKING_PORT0
;
1145 case BR_STATE_LEARNING
:
1146 portstate
= LAN9303_SWE_PORT_STATE_LEARNING_PORT0
;
1148 case BR_STATE_FORWARDING
:
1149 portstate
= LAN9303_SWE_PORT_STATE_FORWARDING_PORT0
;
1152 portstate
= LAN9303_SWE_PORT_STATE_DISABLED_PORT0
;
1153 dev_err(chip
->dev
, "unknown stp state: port %d, state %d\n",
1157 portmask
= 0x3 << (port
* 2);
1158 portstate
<<= (port
* 2);
1160 chip
->swe_port_state
= (chip
->swe_port_state
& ~portmask
) | portstate
;
1162 if (chip
->is_bridged
)
1163 lan9303_write_switch_reg(chip
, LAN9303_SWE_PORT_STATE
,
1164 chip
->swe_port_state
);
1165 /* else: touching SWE_PORT_STATE would break port separation */
1168 static void lan9303_port_fast_age(struct dsa_switch
*ds
, int port
)
1170 struct lan9303
*chip
= ds
->priv
;
1171 struct del_port_learned_ctx del_ctx
= {
1175 dev_dbg(chip
->dev
, "%s(%d)\n", __func__
, port
);
1176 lan9303_alr_loop(chip
, alr_loop_cb_del_port_learned
, &del_ctx
);
1179 static int lan9303_port_fdb_add(struct dsa_switch
*ds
, int port
,
1180 const unsigned char *addr
, u16 vid
)
1182 struct lan9303
*chip
= ds
->priv
;
1184 dev_dbg(chip
->dev
, "%s(%d, %pM, %d)\n", __func__
, port
, addr
, vid
);
1188 return lan9303_alr_add_port(chip
, addr
, port
, false);
1191 static int lan9303_port_fdb_del(struct dsa_switch
*ds
, int port
,
1192 const unsigned char *addr
, u16 vid
)
1195 struct lan9303
*chip
= ds
->priv
;
1197 dev_dbg(chip
->dev
, "%s(%d, %pM, %d)\n", __func__
, port
, addr
, vid
);
1200 lan9303_alr_del_port(chip
, addr
, port
);
1205 static int lan9303_port_fdb_dump(struct dsa_switch
*ds
, int port
,
1206 dsa_fdb_dump_cb_t
*cb
, void *data
)
1208 struct lan9303
*chip
= ds
->priv
;
1209 struct port_fdb_dump_ctx dump_ctx
= {
1215 dev_dbg(chip
->dev
, "%s(%d)\n", __func__
, port
);
1216 lan9303_alr_loop(chip
, alr_loop_cb_fdb_port_dump
, &dump_ctx
);
1221 static int lan9303_port_mdb_prepare(struct dsa_switch
*ds
, int port
,
1222 const struct switchdev_obj_port_mdb
*mdb
)
1224 struct lan9303
*chip
= ds
->priv
;
1226 dev_dbg(chip
->dev
, "%s(%d, %pM, %d)\n", __func__
, port
, mdb
->addr
,
1230 if (lan9303_alr_cache_find_mac(chip
, mdb
->addr
))
1232 if (!lan9303_alr_cache_find_free(chip
))
1238 static void lan9303_port_mdb_add(struct dsa_switch
*ds
, int port
,
1239 const struct switchdev_obj_port_mdb
*mdb
)
1241 struct lan9303
*chip
= ds
->priv
;
1243 dev_dbg(chip
->dev
, "%s(%d, %pM, %d)\n", __func__
, port
, mdb
->addr
,
1245 lan9303_alr_add_port(chip
, mdb
->addr
, port
, false);
1248 static int lan9303_port_mdb_del(struct dsa_switch
*ds
, int port
,
1249 const struct switchdev_obj_port_mdb
*mdb
)
1251 struct lan9303
*chip
= ds
->priv
;
1253 dev_dbg(chip
->dev
, "%s(%d, %pM, %d)\n", __func__
, port
, mdb
->addr
,
1257 lan9303_alr_del_port(chip
, mdb
->addr
, port
);
1262 static const struct dsa_switch_ops lan9303_switch_ops
= {
1263 .get_tag_protocol
= lan9303_get_tag_protocol
,
1264 .setup
= lan9303_setup
,
1265 .get_strings
= lan9303_get_strings
,
1266 .phy_read
= lan9303_phy_read
,
1267 .phy_write
= lan9303_phy_write
,
1268 .adjust_link
= lan9303_adjust_link
,
1269 .get_ethtool_stats
= lan9303_get_ethtool_stats
,
1270 .get_sset_count
= lan9303_get_sset_count
,
1271 .port_enable
= lan9303_port_enable
,
1272 .port_disable
= lan9303_port_disable
,
1273 .port_bridge_join
= lan9303_port_bridge_join
,
1274 .port_bridge_leave
= lan9303_port_bridge_leave
,
1275 .port_stp_state_set
= lan9303_port_stp_state_set
,
1276 .port_fast_age
= lan9303_port_fast_age
,
1277 .port_fdb_add
= lan9303_port_fdb_add
,
1278 .port_fdb_del
= lan9303_port_fdb_del
,
1279 .port_fdb_dump
= lan9303_port_fdb_dump
,
1280 .port_mdb_prepare
= lan9303_port_mdb_prepare
,
1281 .port_mdb_add
= lan9303_port_mdb_add
,
1282 .port_mdb_del
= lan9303_port_mdb_del
,
1285 static int lan9303_register_switch(struct lan9303
*chip
)
1289 chip
->ds
= dsa_switch_alloc(chip
->dev
, LAN9303_NUM_PORTS
);
1293 chip
->ds
->priv
= chip
;
1294 chip
->ds
->ops
= &lan9303_switch_ops
;
1295 base
= chip
->phy_addr_base
;
1296 chip
->ds
->phys_mii_mask
= GENMASK(LAN9303_NUM_PORTS
- 1 + base
, base
);
1298 return dsa_register_switch(chip
->ds
);
1301 static int lan9303_probe_reset_gpio(struct lan9303
*chip
,
1302 struct device_node
*np
)
1304 chip
->reset_gpio
= devm_gpiod_get_optional(chip
->dev
, "reset",
1306 if (IS_ERR(chip
->reset_gpio
))
1307 return PTR_ERR(chip
->reset_gpio
);
1309 if (!chip
->reset_gpio
) {
1310 dev_dbg(chip
->dev
, "No reset GPIO defined\n");
1314 chip
->reset_duration
= 200;
1317 of_property_read_u32(np
, "reset-duration",
1318 &chip
->reset_duration
);
1320 dev_dbg(chip
->dev
, "reset duration defaults to 200 ms\n");
1323 /* A sane reset duration should not be longer than 1s */
1324 if (chip
->reset_duration
> 1000)
1325 chip
->reset_duration
= 1000;
1330 int lan9303_probe(struct lan9303
*chip
, struct device_node
*np
)
1334 mutex_init(&chip
->indirect_mutex
);
1335 mutex_init(&chip
->alr_mutex
);
1337 ret
= lan9303_probe_reset_gpio(chip
, np
);
1341 lan9303_handle_reset(chip
);
1343 ret
= lan9303_check_device(chip
);
1347 ret
= lan9303_register_switch(chip
);
1349 dev_dbg(chip
->dev
, "Failed to register switch: %d\n", ret
);
1355 EXPORT_SYMBOL(lan9303_probe
);
1357 int lan9303_remove(struct lan9303
*chip
)
1361 rc
= lan9303_disable_processing(chip
);
1363 dev_warn(chip
->dev
, "shutting down failed\n");
1365 dsa_unregister_switch(chip
->ds
);
1367 /* assert reset to the whole device to prevent it from doing anything */
1368 gpiod_set_value_cansleep(chip
->reset_gpio
, 1);
1369 gpiod_unexport(chip
->reset_gpio
);
1373 EXPORT_SYMBOL(lan9303_remove
);
1375 MODULE_AUTHOR("Juergen Borleis <kernel@pengutronix.de>");
1376 MODULE_DESCRIPTION("Core driver for SMSC/Microchip LAN9303 three port ethernet switch");
1377 MODULE_LICENSE("GPL v2");