1 // SPDX-License-Identifier: GPL-2.0 2 /* Realtek SMI subdriver for the Realtek RTL8365MB-VC ethernet switch. 3 * 4 * Copyright (C) 2021 Alvin Šipraga <alsi@bang-olufsen.dk> 5 * Copyright (C) 2021 Michael Rasmussen <mir@bang-olufsen.dk> 6 * 7 * The RTL8365MB-VC is a 4+1 port 10/100/1000M switch controller. It includes 4 8 * integrated PHYs for the user facing ports, and an extension interface which 9 * can be connected to the CPU - or another PHY - via either MII, RMII, or 10 * RGMII. The switch is configured via the Realtek Simple Management Interface 11 * (SMI), which uses the MDIO/MDC lines. 12 * 13 * Below is a simplified block diagram of the chip and its relevant interfaces. 14 * 15 * .-----------------------------------. 16 * | | 17 * UTP <---------------> Giga PHY <-> PCS <-> P0 GMAC | 18 * UTP <---------------> Giga PHY <-> PCS <-> P1 GMAC | 19 * UTP <---------------> Giga PHY <-> PCS <-> P2 GMAC | 20 * UTP <---------------> Giga PHY <-> PCS <-> P3 GMAC | 21 * | | 22 * CPU/PHY <-MII/RMII/RGMII---> Extension <---> Extension | 23 * | interface 1 GMAC 1 | 24 * | | 25 * SMI driver/ <-MDC/SCL---> Management ~~~~~~~~~~~~~~ | 26 * EEPROM <-MDIO/SDA--> interface ~REALTEK ~~~~~ | 27 * | ~RTL8365MB ~~~ | 28 * | ~GXXXC TAIWAN~ | 29 * GPIO <--------------> Reset ~~~~~~~~~~~~~~ | 30 * | | 31 * Interrupt <----------> Link UP/DOWN events | 32 * controller | | 33 * '-----------------------------------' 34 * 35 * The driver uses DSA to integrate the 4 user and 1 extension ports into the 36 * kernel. Netdevices are created for the user ports, as are PHY devices for 37 * their integrated PHYs. The device tree firmware should also specify the link 38 * partner of the extension port - either via a fixed-link or other phy-handle. 39 * See the device tree bindings for more detailed information. Note that the 40 * driver has only been tested with a fixed-link, but in principle it should not 41 * matter. 42 * 43 * NOTE: Currently, only the RGMII, SGMII and HSGMII interfaces are implemented 44 * in this driver. 45 * 46 * The interrupt line is asserted on link UP/DOWN events. The driver creates a 47 * custom irqchip to handle this interrupt and demultiplex the events by reading 48 * the status registers via SMI. Interrupts are then propagated to the relevant 49 * PHY device. 50 * 51 * The EEPROM contains initial register values which the chip will read over I2C 52 * upon hardware reset. It is also possible to omit the EEPROM. In both cases, 53 * the driver will manually reprogram some registers using jam tables to reach 54 * an initial state defined by the vendor driver. 55 * 56 * This Linux driver is written based on an OS-agnostic vendor driver from 57 * Realtek. The reference GPL-licensed sources can be found in the OpenWrt 58 * source tree under the name rtl8367c. The vendor driver claims to support a 59 * number of similar switch controllers from Realtek, but the only hardware we 60 * have is the RTL8365MB-VC. Moreover, there does not seem to be any chip under 61 * the name RTL8367C. Although one wishes that the 'C' stood for some kind of 62 * common hardware revision, there exist examples of chips with the suffix -VC 63 * which are explicitly not supported by the rtl8367c driver and which instead 64 * require the rtl8367d vendor driver. With all this uncertainty, the driver has 65 * been modestly named rtl8365mb. Future implementors may wish to rename things 66 * accordingly. 67 * 68 * In the same family of chips, some carry up to 8 user ports and up to 2 69 * extension ports. Where possible this driver tries to make things generic, but 70 * more work must be done to support these configurations. According to 71 * documentation from Realtek, the family should include the following chips: 72 * 73 * - RTL8363NB 74 * - RTL8363NB-VB 75 * - RTL8363SC 76 * - RTL8363SC-VB 77 * - RTL8364NB 78 * - RTL8364NB-VB 79 * - RTL8365MB-VC 80 * - RTL8366SC 81 * - RTL8367RB-VB 82 * - RTL8367SB 83 * - RTL8367S 84 * - RTL8370MB 85 * - RTL8310SR 86 * 87 * Some of the register logic for these additional chips has been skipped over 88 * while implementing this driver. It is therefore not possible to assume that 89 * things will work out-of-the-box for other chips, and a careful review of the 90 * vendor driver may be needed to expand support. The RTL8365MB-VC seems to be 91 * one of the simpler chips. 92 */ 93 94 #include <linux/bitfield.h> 95 #include <linux/bitops.h> 96 #include <linux/interrupt.h> 97 #include <linux/irqdomain.h> 98 #include <linux/mii.h> 99 #include <linux/mutex.h> 100 #include <linux/of_irq.h> 101 #include <linux/regmap.h> 102 #include <linux/if_bridge.h> 103 #include <linux/if_vlan.h> 104 #include <linux/mdio.h> 105 #include <linux/phylink.h> 106 107 #include "realtek.h" 108 #include "realtek-smi.h" 109 #include "realtek-mdio.h" 110 #include "rtl83xx.h" 111 #include "rtl8365mb_l2.h" 112 #include "rtl8365mb_vlan.h" 113 114 /* Family-specific data and limits */ 115 #define RTL8365MB_PHYADDRMAX 7 116 #define RTL8365MB_NUM_PHYREGS 32 117 #define RTL8365MB_PHYREGMAX (RTL8365MB_NUM_PHYREGS - 1) 118 #define RTL8365MB_MAX_NUM_PORTS 11 119 /* Valid for the whole family except RTL8370B, which has 4160 entries. 120 * RTL8370B is mentioned in vendor code but it might not even belong 121 * to the same RTL8367C family. 122 */ 123 #define RTL8365MB_LEARN_LIMIT_MAX 2112 124 #define RTL8365MB_MAX_NUM_EXTINTS 3 125 126 /* Chip identification registers */ 127 #define RTL8365MB_CHIP_ID_REG 0x1300 128 129 #define RTL8365MB_CHIP_VER_REG 0x1301 130 131 #define RTL8365MB_MAGIC_REG 0x13C2 132 #define RTL8365MB_MAGIC_VALUE 0x0249 133 134 /* Chip reset register */ 135 #define RTL8365MB_CHIP_RESET_REG 0x1322 136 #define RTL8365MB_CHIP_RESET_DW8051_MASK BIT(4) 137 #define RTL8365MB_CHIP_RESET_SW_MASK BIT(1) 138 #define RTL8365MB_CHIP_RESET_HW_MASK BIT(0) 139 140 /* Interrupt polarity register */ 141 #define RTL8365MB_INTR_POLARITY_REG 0x1100 142 #define RTL8365MB_INTR_POLARITY_MASK BIT(0) 143 #define RTL8365MB_INTR_POLARITY_HIGH 0 144 #define RTL8365MB_INTR_POLARITY_LOW 1 145 146 /* Interrupt control/status register - enable/check specific interrupt types */ 147 #define RTL8365MB_INTR_CTRL_REG 0x1101 148 #define RTL8365MB_INTR_STATUS_REG 0x1102 149 #define RTL8365MB_INTR_SLIENT_START_2_MASK BIT(12) 150 #define RTL8365MB_INTR_SLIENT_START_MASK BIT(11) 151 #define RTL8365MB_INTR_ACL_ACTION_MASK BIT(9) 152 #define RTL8365MB_INTR_CABLE_DIAG_FIN_MASK BIT(8) 153 #define RTL8365MB_INTR_INTERRUPT_8051_MASK BIT(7) 154 #define RTL8365MB_INTR_LOOP_DETECTION_MASK BIT(6) 155 #define RTL8365MB_INTR_GREEN_TIMER_MASK BIT(5) 156 #define RTL8365MB_INTR_SPECIAL_CONGEST_MASK BIT(4) 157 #define RTL8365MB_INTR_SPEED_CHANGE_MASK BIT(3) 158 #define RTL8365MB_INTR_LEARN_OVER_MASK BIT(2) 159 #define RTL8365MB_INTR_METER_EXCEEDED_MASK BIT(1) 160 #define RTL8365MB_INTR_LINK_CHANGE_MASK BIT(0) 161 #define RTL8365MB_INTR_ALL_MASK \ 162 (RTL8365MB_INTR_SLIENT_START_2_MASK | \ 163 RTL8365MB_INTR_SLIENT_START_MASK | \ 164 RTL8365MB_INTR_ACL_ACTION_MASK | \ 165 RTL8365MB_INTR_CABLE_DIAG_FIN_MASK | \ 166 RTL8365MB_INTR_INTERRUPT_8051_MASK | \ 167 RTL8365MB_INTR_LOOP_DETECTION_MASK | \ 168 RTL8365MB_INTR_GREEN_TIMER_MASK | \ 169 RTL8365MB_INTR_SPECIAL_CONGEST_MASK | \ 170 RTL8365MB_INTR_SPEED_CHANGE_MASK | \ 171 RTL8365MB_INTR_LEARN_OVER_MASK | \ 172 RTL8365MB_INTR_METER_EXCEEDED_MASK | \ 173 RTL8365MB_INTR_LINK_CHANGE_MASK) 174 175 #define RTL8365MB_PORT_MASK GENMASK(10, 0) 176 177 /* Per-port interrupt type status registers */ 178 #define RTL8365MB_PORT_LINKDOWN_IND_REG 0x1106 179 #define RTL8365MB_PORT_LINKDOWN_IND_MASK RTL8365MB_PORT_MASK 180 181 #define RTL8365MB_PORT_LINKUP_IND_REG 0x1107 182 #define RTL8365MB_PORT_LINKUP_IND_MASK RTL8365MB_PORT_MASK 183 184 /* PHY indirect access registers */ 185 #define RTL8365MB_INDIRECT_ACCESS_CTRL_REG 0x1F00 186 #define RTL8365MB_INDIRECT_ACCESS_CTRL_RW_MASK BIT(1) 187 #define RTL8365MB_INDIRECT_ACCESS_CTRL_RW_READ 0 188 #define RTL8365MB_INDIRECT_ACCESS_CTRL_RW_WRITE 1 189 #define RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_MASK BIT(0) 190 #define RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_VALUE 1 191 #define RTL8365MB_INDIRECT_ACCESS_STATUS_REG 0x1F01 192 #define RTL8365MB_INDIRECT_ACCESS_ADDRESS_REG 0x1F02 193 #define RTL8365MB_INDIRECT_ACCESS_ADDRESS_OCPADR_5_1_MASK GENMASK(4, 0) 194 #define RTL8365MB_INDIRECT_ACCESS_ADDRESS_PHYNUM_MASK GENMASK(7, 5) 195 #define RTL8365MB_INDIRECT_ACCESS_ADDRESS_OCPADR_9_6_MASK GENMASK(11, 8) 196 #define RTL8365MB_PHY_BASE 0x2000 197 #define RTL8365MB_INDIRECT_ACCESS_WRITE_DATA_REG 0x1F03 198 #define RTL8365MB_INDIRECT_ACCESS_READ_DATA_REG 0x1F04 199 200 /* PHY OCP address prefix register */ 201 #define RTL8365MB_GPHY_OCP_MSB_0_REG 0x1D15 202 #define RTL8365MB_GPHY_OCP_MSB_0_CFG_CPU_OCPADR_MASK GENMASK(11, 6) 203 #define RTL8365MB_PHY_OCP_ADDR_PREFIX_MASK GENMASK(15, 10) 204 205 /* The full 16-bit OCP address is split across two registers: bits [15:10] are 206 * the prefix (RTL8365MB_PHY_OCP_ADDR_PREFIX_MASK above), and bits [9:1] go into 207 * the ADDRESS register as two fields, [5:1] and [9:6]. Bit 0 is always 0 - PHY 208 * OCP registers are 2-byte aligned. 209 */ 210 #define RTL8365MB_PHY_OCP_ADDR_5_1_MASK GENMASK(5, 1) 211 #define RTL8365MB_PHY_OCP_ADDR_9_6_MASK GENMASK(9, 6) 212 213 /* The PHY OCP addresses of PHY registers 0~31 start here */ 214 #define RTL8365MB_PHY_OCP_ADDR_PHYREG_BASE 0xA400 215 216 #define RTL8365MB_PHY_OCP_ADDR_EEE_ABLE 0xA5C4 217 #define RTL8365MB_PHY_OCP_ADDR_EEE_ADV 0xA5D0 218 #define RTL8365MB_PHY_OCP_ADDR_EEE_LPABLE 0xA5D2 219 220 /* External interface port mode values - used in DIGITAL_INTERFACE_SELECT */ 221 #define RTL8365MB_EXT_PORT_MODE_DISABLE 0 222 #define RTL8365MB_EXT_PORT_MODE_RGMII 1 223 #define RTL8365MB_EXT_PORT_MODE_MII_MAC 2 224 #define RTL8365MB_EXT_PORT_MODE_MII_PHY 3 225 #define RTL8365MB_EXT_PORT_MODE_TMII_MAC 4 226 #define RTL8365MB_EXT_PORT_MODE_TMII_PHY 5 227 #define RTL8365MB_EXT_PORT_MODE_GMII 6 228 #define RTL8365MB_EXT_PORT_MODE_RMII_MAC 7 229 #define RTL8365MB_EXT_PORT_MODE_RMII_PHY 8 230 #define RTL8365MB_EXT_PORT_MODE_SGMII 9 231 #define RTL8365MB_EXT_PORT_MODE_HSGMII 10 232 #define RTL8365MB_EXT_PORT_MODE_1000X_100FX 11 233 #define RTL8365MB_EXT_PORT_MODE_1000X 12 234 #define RTL8365MB_EXT_PORT_MODE_100FX 13 235 236 /* External interface mode configuration registers 0~1 */ 237 #define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0 0x1305 /* EXT0,EXT1 */ 238 #define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1 0x13C3 /* EXT2 */ 239 #define RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(_extint) \ 240 ((_extint) <= 1 ? RTL8365MB_DIGITAL_INTERFACE_SELECT_REG0 : \ 241 (_extint) == 2 ? RTL8365MB_DIGITAL_INTERFACE_SELECT_REG1 : \ 242 0x0) 243 #define RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(_extint) \ 244 (0xF << (((_extint) % 2) * 4)) 245 #define RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(_extint) \ 246 (((_extint) % 2) * 4) 247 248 /* External interface RGMII TX/RX delay configuration registers 0~2 */ 249 #define RTL8365MB_EXT_RGMXF_REG0 0x1306 /* EXT0 */ 250 #define RTL8365MB_EXT_RGMXF_REG1 0x1307 /* EXT1 */ 251 #define RTL8365MB_EXT_RGMXF_REG2 0x13C5 /* EXT2 */ 252 #define RTL8365MB_EXT_RGMXF_REG(_extint) \ 253 ((_extint) == 0 ? RTL8365MB_EXT_RGMXF_REG0 : \ 254 (_extint) == 1 ? RTL8365MB_EXT_RGMXF_REG1 : \ 255 (_extint) == 2 ? RTL8365MB_EXT_RGMXF_REG2 : \ 256 0x0) 257 #define RTL8365MB_EXT_RGMXF_RXDELAY_MASK GENMASK(2, 0) 258 #define RTL8365MB_EXT_RGMXF_TXDELAY_MASK BIT(3) 259 260 /* External interface line rate bypass register - one bit per external 261 * interface, indexed by the external port number with port 5 (the first 262 * external port) as the base. Other RTL8367 families index this register 263 * differently (e.g. the RTL8367R uses (id + 1) % 2), so this mapping only 264 * holds for the RTL8367C-style parts this driver supports. 265 */ 266 #define RTL8365MB_BYPASS_LINE_RATE_REG 0x03F7 267 #define RTL8365MB_BYPASS_LINE_RATE_MASK(_port) BIT((_port) - 5) 268 269 /* Port 6 ingress and egress rate limiter registers. Each limit is a 19-bit 270 * value in units of 8 Kbps, split across a 16-bit LSB register (CTRL0) and a 271 * 3-bit MSB field (CTRL1). The chip resets them to 0x1FFFF; see 272 * rtl8365mb_sds_raise_rate_limits(). 273 */ 274 #define RTL8365MB_INGRESSBW_PORT6_RATE_CTRL0_REG 0x00CF 275 #define RTL8365MB_INGRESSBW_PORT6_RATE_CTRL1_REG 0x00D0 276 #define RTL8365MB_INGRESSBW_PORT6_RATE_CTRL1_MASK GENMASK(2, 0) 277 #define RTL8365MB_PORT6_EGRESSBW_CTRL0_REG 0x0398 278 #define RTL8365MB_PORT6_EGRESSBW_CTRL1_REG 0x0399 279 #define RTL8365MB_PORT6_EGRESSBW_CTRL1_MASK GENMASK(2, 0) 280 281 /* SerDes indirect access registers */ 282 #define RTL8365MB_SDS_INDACS_CMD_REG 0x6600 283 #define RTL8365MB_SDS_INDACS_CMD_BUSY_MASK BIT(8) 284 #define RTL8365MB_SDS_INDACS_CMD_RUN_MASK BIT(7) 285 #define RTL8365MB_SDS_INDACS_CMD_WR_MASK BIT(6) 286 #define RTL8365MB_SDS_INDACS_ADR_REG 0x6601 287 #define RTL8365MB_SDS_INDACS_DATA_REG 0x6602 288 289 /* SerDes miscellaneous configuration register */ 290 #define RTL8365MB_SDS_MISC_REG 0x1D11 291 #define RTL8365MB_SDS_MISC_SGMII_RXFC_MASK BIT(14) 292 #define RTL8365MB_SDS_MISC_SGMII_TXFC_MASK BIT(13) 293 #define RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK BIT(11) 294 #define RTL8365MB_SDS_MISC_SGMII_FDUP_MASK BIT(10) 295 #define RTL8365MB_SDS_MISC_SGMII_LINK_MASK BIT(9) 296 #define RTL8365MB_SDS_MISC_SGMII_SPD_MASK GENMASK(8, 7) 297 #define RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK BIT(6) 298 299 /* SerDes internal registers, accessed via the SDS_INDACS registers. The BMCR 300 * data path reset holds BMCR_ANENABLE | BMCR_ISOLATE while toggling the 301 * vendor-specific low bits from phase 1 to phase 2, which triggers a data path 302 * reset and PLL resync. 303 */ 304 #define RTL8365MB_SDS_REG_BMCR 0x0000 305 #define RTL8365MB_SDS_BMCR_DPRST_PHASE1 (BMCR_ANENABLE | BMCR_ISOLATE | 0x1) 306 #define RTL8365MB_SDS_BMCR_DPRST_PHASE2 (BMCR_ANENABLE | BMCR_ISOLATE | 0x3) 307 #define RTL8365MB_SDS_REG_NWAY 0x0002 308 #define RTL8365MB_SDS_NWAY_EN_MASK BIT(9) 309 #define RTL8365MB_SDS_NWAY_RESTART_MASK BIT(8) 310 #define RTL8365MB_SDS_REG_RESET 0x0003 311 #define RTL8365MB_SDS_RESET_DEASSERT 0x7106 312 #define RTL8365MB_SDS_REG_LINK_STATUS 0x003d 313 #define RTL8365MB_SDS_LINK_STATUS_LINK_MASK BIT(4) 314 315 /* The embedded SerDes can only be muxed to external interface 1 (MAC8), 316 * which is port 6. 317 */ 318 #define RTL8365MB_SDS_EXT_INTERFACE_ID 1 319 #define RTL8365MB_SDS_EXT_INTERFACE_PORT 6 320 321 /* Line rate bypass bit for the SerDes external interface */ 322 #define RTL8365MB_SDS_BYPASS_LINE_RATE_MASK \ 323 RTL8365MB_BYPASS_LINE_RATE_MASK(RTL8365MB_SDS_EXT_INTERFACE_PORT) 324 325 /* SerDes tuning parameter variant selector. The vendor driver picks between 326 * two sets of SerDes tuning parameters based on this chip option. Reading it 327 * requires first arming the read by writing a magic key to the arm register, 328 * then disarming it afterwards. 329 */ 330 #define RTL8365MB_SDS_OPTION_ARM_REG 0x13C0 331 #define RTL8365MB_SDS_OPTION_ARM_KEY 0x0249 332 #define RTL8365MB_SDS_OPTION_REG 0x13C1 333 334 /* Embedded DW8051 microcontroller control registers. The microcontroller 335 * can run firmware to manage the SerDes link, but this driver keeps it in 336 * reset and disabled: phylink already performs the link management that 337 * the firmware would otherwise do. 338 */ 339 #define RTL8365MB_MISC_CFG0_REG 0x130C 340 #define RTL8365MB_MISC_CFG0_DW8051_EN_MASK BIT(5) 341 342 /* External interface port speed values - used in DIGITAL_INTERFACE_FORCE */ 343 #define RTL8365MB_PORT_SPEED_10M 0 344 #define RTL8365MB_PORT_SPEED_100M 1 345 #define RTL8365MB_PORT_SPEED_1000M 2 346 347 /* External interface force configuration registers 0~2 */ 348 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0 0x1310 /* EXT0 */ 349 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1 0x1311 /* EXT1 */ 350 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2 0x13C4 /* EXT2 */ 351 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(_extint) \ 352 ((_extint) == 0 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG0 : \ 353 (_extint) == 1 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG1 : \ 354 (_extint) == 2 ? RTL8365MB_DIGITAL_INTERFACE_FORCE_REG2 : \ 355 0x0) 356 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_EN_MASK BIT(12) 357 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_NWAY_MASK BIT(7) 358 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_TXPAUSE_MASK BIT(6) 359 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_RXPAUSE_MASK BIT(5) 360 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_LINK_MASK BIT(4) 361 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_DUPLEX_MASK BIT(2) 362 #define RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK GENMASK(1, 0) 363 364 /* CPU port mask register - controls which ports are treated as CPU ports */ 365 #define RTL8365MB_CPU_PORT_MASK_REG 0x1219 366 #define RTL8365MB_CPU_PORT_MASK_MASK RTL8365MB_PORT_MASK 367 368 /* CPU control register */ 369 #define RTL8365MB_CPU_CTRL_REG 0x121A 370 #define RTL8365MB_CPU_CTRL_TRAP_PORT_EXT_MASK BIT(10) 371 #define RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK BIT(9) 372 #define RTL8365MB_CPU_CTRL_RXBYTECOUNT_MASK BIT(7) 373 #define RTL8365MB_CPU_CTRL_TAG_POSITION_MASK BIT(6) 374 #define RTL8365MB_CPU_CTRL_TRAP_PORT_MASK GENMASK(5, 3) 375 #define RTL8365MB_CPU_CTRL_INSERTMODE_MASK GENMASK(2, 1) 376 #define RTL8365MB_CPU_CTRL_EN_MASK BIT(0) 377 378 /* Maximum packet length register */ 379 #define RTL8365MB_CFG0_MAX_LEN_REG 0x088C 380 #define RTL8365MB_CFG0_MAX_LEN_MASK GENMASK(13, 0) 381 #define RTL8365MB_CFG0_MAX_LEN_MAX 0x3FFF 382 383 /* Port learning limit registers */ 384 #define RTL8365MB_LUT_PORT_LEARN_LIMIT_BASE 0x0A20 385 #define RTL8365MB_LUT_PORT_LEARN_LIMIT_REG(_physport) \ 386 (RTL8365MB_LUT_PORT_LEARN_LIMIT_BASE + (_physport)) 387 388 /* Port isolation (forwarding mask) registers */ 389 #define RTL8365MB_PORT_ISOLATION_REG_BASE 0x08A2 390 #define RTL8365MB_PORT_ISOLATION_REG(_physport) \ 391 (RTL8365MB_PORT_ISOLATION_REG_BASE + (_physport)) 392 #define RTL8365MB_PORT_ISOLATION_MASK RTL8365MB_PORT_MASK 393 394 /* Extended filter ID registers - used to key forwarding database with IVL */ 395 #define RTL8365MB_EFID_MASK GENMASK(2, 0) 396 #define RTL8365MB_PORT_EFID_REG_BASE 0x0A32 397 #define RTL8365MB_PORT_EFID_REG(_p) \ 398 (RTL8365MB_PORT_EFID_REG_BASE + ((_p) >> 2)) 399 #define RTL8365MB_PORT_EFID_OFFSET(_p) (((_p) & 0x3) << 2) 400 #define RTL8365MB_PORT_EFID_MASK(_p) \ 401 (RTL8365MB_EFID_MASK << RTL8365MB_PORT_EFID_OFFSET(_p)) 402 403 /* MSTP port state registers - indexed by tree instance */ 404 #define RTL8365MB_MSTI_CTRL_BASE 0x0A00 405 #define RTL8365MB_MSTI_CTRL_REG(_msti, _physport) \ 406 (RTL8365MB_MSTI_CTRL_BASE + ((_msti) << 1) + ((_physport) >> 3)) 407 #define RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET(_physport) ((_physport) << 1) 408 #define RTL8365MB_MSTI_CTRL_PORT_STATE_MASK(_physport) \ 409 (0x3 << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET((_physport))) 410 411 /* Unknown unicast DA flooding port mask */ 412 #define RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_REG 0x0890 413 #define RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_MASK RTL8365MB_PORT_MASK 414 415 /* Unknown multicast DA flooding port mask */ 416 #define RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_REG 0x0891 417 #define RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_MASK RTL8365MB_PORT_MASK 418 419 /* Broadcast flooding port mask */ 420 #define RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_REG 0x0892 421 #define RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_MASK RTL8365MB_PORT_MASK 422 423 #define RTL8365MB_SUPPORTED_BRIDGE_FLAGS \ 424 (BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | BR_BCAST_FLOOD) 425 426 /* Miscellaneous port configuration register, incl. VLAN egress mode */ 427 #define RTL8365MB_PORT_MISC_CFG_REG_BASE 0x000E 428 #define RTL8365MB_PORT_MISC_CFG_REG(_p) \ 429 (RTL8365MB_PORT_MISC_CFG_REG_BASE + ((_p) << 5)) 430 #define RTL8365MB_PORT_MISC_CFG_SMALL_TAG_IPG_MASK BIT(15) 431 #define RTL8365MB_PORT_MISC_CFG_TX_ITFSP_MODE_MASK BIT(14) 432 #define RTL8365MB_PORT_MISC_CFG_FLOWCTRL_INDEP_MASK BIT(13) 433 #define RTL8365MB_PORT_MISC_CFG_DOT1Q_REMARK_ENABLE_MASK BIT(12) 434 #define RTL8365MB_PORT_MISC_CFG_INGRESSBW_FLOWCTRL_MASK BIT(11) 435 #define RTL8365MB_PORT_MISC_CFG_INGRESSBW_IFG_MASK BIT(10) 436 #define RTL8365MB_PORT_MISC_CFG_RX_SPC_MASK BIT(9) 437 #define RTL8365MB_PORT_MISC_CFG_CRC_SKIP_MASK BIT(8) 438 #define RTL8365MB_PORT_MISC_CFG_PKTGEN_TX_FIRST_MASK BIT(7) 439 #define RTL8365MB_PORT_MISC_CFG_MAC_LOOPBACK_MASK BIT(6) 440 /* See &rtl8365mb_vlan_egress_mode */ 441 #define RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK GENMASK(5, 4) 442 #define RTL8365MB_PORT_MISC_CFG_CONGESTION_SUSTAIN_TIME_MASK GENMASK(3, 0) 443 444 /** 445 * enum rtl8365mb_vlan_egress_mode - port VLAN egress mode 446 * @RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL: follow untag mask in VLAN4k table entry 447 * @RTL8365MB_VLAN_EGRESS_MODE_KEEP: the VLAN tag format of egressed packets 448 * will remain the same as their ingressed format, but the priority and VID 449 * fields may be altered 450 * @RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG: always egress with priority tag 451 * @RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP: the VLAN tag format of egressed 452 * packets will remain the same as their ingressed format, and neither the 453 * priority nor VID fields can be altered 454 */ 455 enum rtl8365mb_vlan_egress_mode { 456 RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL = 0, 457 RTL8365MB_VLAN_EGRESS_MODE_KEEP = 1, 458 RTL8365MB_VLAN_EGRESS_MODE_PRI_TAG = 2, 459 RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP = 3, 460 }; 461 462 /* VLAN control register */ 463 #define RTL8365MB_VLAN_CTRL_REG 0x07A8 464 #define RTL8365MB_VLAN_CTRL_EN_MASK BIT(0) 465 466 /* VLAN ingress filter register */ 467 #define RTL8365MB_VLAN_INGRESS_REG 0x07A9 468 #define RTL8365MB_VLAN_INGRESS_MASK GENMASK(10, 0) 469 #define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_OFFSET(_p) (_p) 470 #define RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(_p) BIT(_p) 471 472 /* VLAN "transparent" setting registers */ 473 #define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE 0x09D0 474 #define RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(_p) \ 475 (RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG_BASE + (_p)) 476 477 /* MIB counter value registers */ 478 #define RTL8365MB_MIB_COUNTER_BASE 0x1000 479 #define RTL8365MB_MIB_COUNTER_REG(_x) (RTL8365MB_MIB_COUNTER_BASE + (_x)) 480 481 /* MIB counter address register */ 482 #define RTL8365MB_MIB_ADDRESS_REG 0x1004 483 #define RTL8365MB_MIB_ADDRESS_PORT_OFFSET 0x007C 484 #define RTL8365MB_MIB_ADDRESS(_p, _x) \ 485 (((RTL8365MB_MIB_ADDRESS_PORT_OFFSET) * (_p) + (_x)) >> 2) 486 487 #define RTL8365MB_MIB_CTRL0_REG 0x1005 488 #define RTL8365MB_MIB_CTRL0_RESET_MASK BIT(1) 489 #define RTL8365MB_MIB_CTRL0_BUSY_MASK BIT(0) 490 491 /* The DSA callback .get_stats64 runs in atomic context, so we are not allowed 492 * to block. On the other hand, accessing MIB counters absolutely requires us to 493 * block. The solution is thus to schedule work which polls the MIB counters 494 * asynchronously and updates some private data, which the callback can then 495 * fetch atomically. Three seconds should be a good enough polling interval. 496 */ 497 #define RTL8365MB_STATS_INTERVAL_JIFFIES (3 * HZ) 498 499 enum rtl8365mb_mib_counter_index { 500 RTL8365MB_MIB_ifInOctets, 501 RTL8365MB_MIB_dot3StatsFCSErrors, 502 RTL8365MB_MIB_dot3StatsSymbolErrors, 503 RTL8365MB_MIB_dot3InPauseFrames, 504 RTL8365MB_MIB_dot3ControlInUnknownOpcodes, 505 RTL8365MB_MIB_etherStatsFragments, 506 RTL8365MB_MIB_etherStatsJabbers, 507 RTL8365MB_MIB_ifInUcastPkts, 508 RTL8365MB_MIB_etherStatsDropEvents, 509 RTL8365MB_MIB_ifInMulticastPkts, 510 RTL8365MB_MIB_ifInBroadcastPkts, 511 RTL8365MB_MIB_inMldChecksumError, 512 RTL8365MB_MIB_inIgmpChecksumError, 513 RTL8365MB_MIB_inMldSpecificQuery, 514 RTL8365MB_MIB_inMldGeneralQuery, 515 RTL8365MB_MIB_inIgmpSpecificQuery, 516 RTL8365MB_MIB_inIgmpGeneralQuery, 517 RTL8365MB_MIB_inMldLeaves, 518 RTL8365MB_MIB_inIgmpLeaves, 519 RTL8365MB_MIB_etherStatsOctets, 520 RTL8365MB_MIB_etherStatsUnderSizePkts, 521 RTL8365MB_MIB_etherOversizeStats, 522 RTL8365MB_MIB_etherStatsPkts64Octets, 523 RTL8365MB_MIB_etherStatsPkts65to127Octets, 524 RTL8365MB_MIB_etherStatsPkts128to255Octets, 525 RTL8365MB_MIB_etherStatsPkts256to511Octets, 526 RTL8365MB_MIB_etherStatsPkts512to1023Octets, 527 RTL8365MB_MIB_etherStatsPkts1024to1518Octets, 528 RTL8365MB_MIB_ifOutOctets, 529 RTL8365MB_MIB_dot3StatsSingleCollisionFrames, 530 RTL8365MB_MIB_dot3StatsMultipleCollisionFrames, 531 RTL8365MB_MIB_dot3StatsDeferredTransmissions, 532 RTL8365MB_MIB_dot3StatsLateCollisions, 533 RTL8365MB_MIB_etherStatsCollisions, 534 RTL8365MB_MIB_dot3StatsExcessiveCollisions, 535 RTL8365MB_MIB_dot3OutPauseFrames, 536 RTL8365MB_MIB_ifOutDiscards, 537 RTL8365MB_MIB_dot1dTpPortInDiscards, 538 RTL8365MB_MIB_ifOutUcastPkts, 539 RTL8365MB_MIB_ifOutMulticastPkts, 540 RTL8365MB_MIB_ifOutBroadcastPkts, 541 RTL8365MB_MIB_outOampduPkts, 542 RTL8365MB_MIB_inOampduPkts, 543 RTL8365MB_MIB_inIgmpJoinsSuccess, 544 RTL8365MB_MIB_inIgmpJoinsFail, 545 RTL8365MB_MIB_inMldJoinsSuccess, 546 RTL8365MB_MIB_inMldJoinsFail, 547 RTL8365MB_MIB_inReportSuppressionDrop, 548 RTL8365MB_MIB_inLeaveSuppressionDrop, 549 RTL8365MB_MIB_outIgmpReports, 550 RTL8365MB_MIB_outIgmpLeaves, 551 RTL8365MB_MIB_outIgmpGeneralQuery, 552 RTL8365MB_MIB_outIgmpSpecificQuery, 553 RTL8365MB_MIB_outMldReports, 554 RTL8365MB_MIB_outMldLeaves, 555 RTL8365MB_MIB_outMldGeneralQuery, 556 RTL8365MB_MIB_outMldSpecificQuery, 557 RTL8365MB_MIB_inKnownMulticastPkts, 558 RTL8365MB_MIB_END, 559 }; 560 561 struct rtl8365mb_mib_counter { 562 u32 offset; 563 u32 length; 564 const char *name; 565 }; 566 567 #define RTL8365MB_MAKE_MIB_COUNTER(_offset, _length, _name) \ 568 [RTL8365MB_MIB_ ## _name] = { _offset, _length, #_name } 569 570 static struct rtl8365mb_mib_counter rtl8365mb_mib_counters[] = { 571 RTL8365MB_MAKE_MIB_COUNTER(0, 4, ifInOctets), 572 RTL8365MB_MAKE_MIB_COUNTER(4, 2, dot3StatsFCSErrors), 573 RTL8365MB_MAKE_MIB_COUNTER(6, 2, dot3StatsSymbolErrors), 574 RTL8365MB_MAKE_MIB_COUNTER(8, 2, dot3InPauseFrames), 575 RTL8365MB_MAKE_MIB_COUNTER(10, 2, dot3ControlInUnknownOpcodes), 576 RTL8365MB_MAKE_MIB_COUNTER(12, 2, etherStatsFragments), 577 RTL8365MB_MAKE_MIB_COUNTER(14, 2, etherStatsJabbers), 578 RTL8365MB_MAKE_MIB_COUNTER(16, 2, ifInUcastPkts), 579 RTL8365MB_MAKE_MIB_COUNTER(18, 2, etherStatsDropEvents), 580 RTL8365MB_MAKE_MIB_COUNTER(20, 2, ifInMulticastPkts), 581 RTL8365MB_MAKE_MIB_COUNTER(22, 2, ifInBroadcastPkts), 582 RTL8365MB_MAKE_MIB_COUNTER(24, 2, inMldChecksumError), 583 RTL8365MB_MAKE_MIB_COUNTER(26, 2, inIgmpChecksumError), 584 RTL8365MB_MAKE_MIB_COUNTER(28, 2, inMldSpecificQuery), 585 RTL8365MB_MAKE_MIB_COUNTER(30, 2, inMldGeneralQuery), 586 RTL8365MB_MAKE_MIB_COUNTER(32, 2, inIgmpSpecificQuery), 587 RTL8365MB_MAKE_MIB_COUNTER(34, 2, inIgmpGeneralQuery), 588 RTL8365MB_MAKE_MIB_COUNTER(36, 2, inMldLeaves), 589 RTL8365MB_MAKE_MIB_COUNTER(38, 2, inIgmpLeaves), 590 RTL8365MB_MAKE_MIB_COUNTER(40, 4, etherStatsOctets), 591 RTL8365MB_MAKE_MIB_COUNTER(44, 2, etherStatsUnderSizePkts), 592 RTL8365MB_MAKE_MIB_COUNTER(46, 2, etherOversizeStats), 593 RTL8365MB_MAKE_MIB_COUNTER(48, 2, etherStatsPkts64Octets), 594 RTL8365MB_MAKE_MIB_COUNTER(50, 2, etherStatsPkts65to127Octets), 595 RTL8365MB_MAKE_MIB_COUNTER(52, 2, etherStatsPkts128to255Octets), 596 RTL8365MB_MAKE_MIB_COUNTER(54, 2, etherStatsPkts256to511Octets), 597 RTL8365MB_MAKE_MIB_COUNTER(56, 2, etherStatsPkts512to1023Octets), 598 RTL8365MB_MAKE_MIB_COUNTER(58, 2, etherStatsPkts1024to1518Octets), 599 RTL8365MB_MAKE_MIB_COUNTER(60, 4, ifOutOctets), 600 RTL8365MB_MAKE_MIB_COUNTER(64, 2, dot3StatsSingleCollisionFrames), 601 RTL8365MB_MAKE_MIB_COUNTER(66, 2, dot3StatsMultipleCollisionFrames), 602 RTL8365MB_MAKE_MIB_COUNTER(68, 2, dot3StatsDeferredTransmissions), 603 RTL8365MB_MAKE_MIB_COUNTER(70, 2, dot3StatsLateCollisions), 604 RTL8365MB_MAKE_MIB_COUNTER(72, 2, etherStatsCollisions), 605 RTL8365MB_MAKE_MIB_COUNTER(74, 2, dot3StatsExcessiveCollisions), 606 RTL8365MB_MAKE_MIB_COUNTER(76, 2, dot3OutPauseFrames), 607 RTL8365MB_MAKE_MIB_COUNTER(78, 2, ifOutDiscards), 608 RTL8365MB_MAKE_MIB_COUNTER(80, 2, dot1dTpPortInDiscards), 609 RTL8365MB_MAKE_MIB_COUNTER(82, 2, ifOutUcastPkts), 610 RTL8365MB_MAKE_MIB_COUNTER(84, 2, ifOutMulticastPkts), 611 RTL8365MB_MAKE_MIB_COUNTER(86, 2, ifOutBroadcastPkts), 612 RTL8365MB_MAKE_MIB_COUNTER(88, 2, outOampduPkts), 613 RTL8365MB_MAKE_MIB_COUNTER(90, 2, inOampduPkts), 614 RTL8365MB_MAKE_MIB_COUNTER(92, 4, inIgmpJoinsSuccess), 615 RTL8365MB_MAKE_MIB_COUNTER(96, 2, inIgmpJoinsFail), 616 RTL8365MB_MAKE_MIB_COUNTER(98, 2, inMldJoinsSuccess), 617 RTL8365MB_MAKE_MIB_COUNTER(100, 2, inMldJoinsFail), 618 RTL8365MB_MAKE_MIB_COUNTER(102, 2, inReportSuppressionDrop), 619 RTL8365MB_MAKE_MIB_COUNTER(104, 2, inLeaveSuppressionDrop), 620 RTL8365MB_MAKE_MIB_COUNTER(106, 2, outIgmpReports), 621 RTL8365MB_MAKE_MIB_COUNTER(108, 2, outIgmpLeaves), 622 RTL8365MB_MAKE_MIB_COUNTER(110, 2, outIgmpGeneralQuery), 623 RTL8365MB_MAKE_MIB_COUNTER(112, 2, outIgmpSpecificQuery), 624 RTL8365MB_MAKE_MIB_COUNTER(114, 2, outMldReports), 625 RTL8365MB_MAKE_MIB_COUNTER(116, 2, outMldLeaves), 626 RTL8365MB_MAKE_MIB_COUNTER(118, 2, outMldGeneralQuery), 627 RTL8365MB_MAKE_MIB_COUNTER(120, 2, outMldSpecificQuery), 628 RTL8365MB_MAKE_MIB_COUNTER(122, 2, inKnownMulticastPkts), 629 }; 630 631 static_assert(ARRAY_SIZE(rtl8365mb_mib_counters) == RTL8365MB_MIB_END); 632 633 struct rtl8365mb_jam_tbl_entry { 634 u16 reg; 635 u16 val; 636 }; 637 638 /* Lifted from the vendor driver sources */ 639 static const struct rtl8365mb_jam_tbl_entry rtl8365mb_init_jam_8365mb_vc[] = { 640 { 0x13EB, 0x15BB }, { 0x1303, 0x06D6 }, { 0x1304, 0x0700 }, 641 { 0x13E2, 0x003F }, { 0x13F9, 0x0090 }, { 0x121E, 0x03CA }, 642 { 0x1233, 0x0352 }, { 0x1237, 0x00A0 }, { 0x123A, 0x0030 }, 643 { 0x1239, 0x0084 }, { 0x0301, 0x1000 }, { 0x1349, 0x001F }, 644 { 0x18E0, 0x4004 }, { 0x122B, 0x241C }, { 0x1305, 0xC000 }, 645 { 0x13F0, 0x0000 }, 646 }; 647 648 static const struct rtl8365mb_jam_tbl_entry rtl8365mb_init_jam_common[] = { 649 { 0x1200, 0x7FCB }, { 0x0884, 0x0003 }, { 0x06EB, 0x0001 }, 650 { 0x03Fa, 0x0007 }, { 0x08C8, 0x00C0 }, { 0x0A30, 0x020E }, 651 { 0x0800, 0x0000 }, { 0x0802, 0x0000 }, { 0x09DA, 0x0013 }, 652 { 0x1D32, 0x0002 }, 653 }; 654 655 /* SGMII SerDes tuning parameters, lifted from the vendor driver sources. The 656 * vendor driver keeps two variants of this table and selects between them 657 * based on the chip option register; these are the values for a non-zero 658 * option, which is what RTL8367S parts seen so far report. See 659 * rtl8365mb_sds_probe_option(). 660 */ 661 static const struct rtl8365mb_jam_tbl_entry rtl8365mb_sds_jam_sgmii[] = { 662 { 0x0480, 0x04D7 }, { 0x0481, 0xF994 }, { 0x0482, 0x2420 }, 663 { 0x0483, 0x6960 }, { 0x0484, 0x9728 }, { 0x0423, 0x9D85 }, 664 { 0x0424, 0xD810 }, { 0x002E, 0x83F2 }, 665 }; 666 667 /* HSGMII SerDes tuning parameters, lifted from the vendor driver sources. As 668 * with the SGMII table, the vendor driver keeps several variants and selects 669 * one based on the chip option register; these are the values for a non-zero 670 * option, which is what RTL8367S parts seen so far report. See 671 * rtl8365mb_sds_probe_option(). 672 */ 673 static const struct rtl8365mb_jam_tbl_entry rtl8365mb_sds_jam_hsgmii[] = { 674 { 0x0500, 0x82F0 }, { 0x0501, 0xF195 }, { 0x0502, 0x31A2 }, 675 { 0x0503, 0x7960 }, { 0x0504, 0x9728 }, { 0x0423, 0x9D85 }, 676 { 0x0424, 0xD810 }, { 0x0001, 0x0F80 }, { 0x002E, 0x83F2 }, 677 }; 678 679 enum rtl8365mb_phy_interface_mode { 680 RTL8365MB_PHY_INTERFACE_MODE_INVAL = 0, 681 RTL8365MB_PHY_INTERFACE_MODE_INTERNAL = BIT(0), 682 RTL8365MB_PHY_INTERFACE_MODE_MII = BIT(1), 683 RTL8365MB_PHY_INTERFACE_MODE_TMII = BIT(2), 684 RTL8365MB_PHY_INTERFACE_MODE_RMII = BIT(3), 685 RTL8365MB_PHY_INTERFACE_MODE_RGMII = BIT(4), 686 RTL8365MB_PHY_INTERFACE_MODE_SGMII = BIT(5), 687 RTL8365MB_PHY_INTERFACE_MODE_HSGMII = BIT(6), 688 }; 689 690 /** 691 * struct rtl8365mb_extint - external interface info 692 * @port: the port with an external interface 693 * @id: the external interface ID, which is either 0, 1, or 2 694 * @supported_interfaces: a bitmask of supported PHY interface modes 695 * 696 * Represents a mapping: port -> { id, supported_interfaces }. To be embedded 697 * in &struct rtl8365mb_chip_info for every port with an external interface. 698 */ 699 struct rtl8365mb_extint { 700 int port; 701 int id; 702 unsigned int supported_interfaces; 703 }; 704 705 /** 706 * struct rtl8365mb_chip_info - static chip-specific info 707 * @name: human-readable chip name 708 * @chip_id: chip identifier 709 * @chip_ver: chip silicon revision 710 * @extints: available external interfaces 711 * @jam_table: chip-specific initialization jam table 712 * @jam_size: size of the chip's jam table 713 * 714 * These data are specific to a given chip in the family of switches supported 715 * by this driver. When adding support for another chip in the family, a new 716 * chip info should be added to the rtl8365mb_chip_infos array. 717 */ 718 struct rtl8365mb_chip_info { 719 const char *name; 720 u32 chip_id; 721 u32 chip_ver; 722 const struct rtl8365mb_extint extints[RTL8365MB_MAX_NUM_EXTINTS]; 723 const struct rtl8365mb_jam_tbl_entry *jam_table; 724 size_t jam_size; 725 }; 726 727 /* Chip info for each supported switch in the family */ 728 #define PHY_INTF(_mode) (RTL8365MB_PHY_INTERFACE_MODE_ ## _mode) 729 static const struct rtl8365mb_chip_info rtl8365mb_chip_infos[] = { 730 { 731 .name = "RTL8365MB-VC", 732 .chip_id = 0x6367, 733 .chip_ver = 0x0040, 734 .extints = { 735 { 6, 1, PHY_INTF(MII) | PHY_INTF(TMII) | 736 PHY_INTF(RMII) | PHY_INTF(RGMII) }, 737 }, 738 .jam_table = rtl8365mb_init_jam_8365mb_vc, 739 .jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc), 740 }, 741 { 742 .name = "RTL8367S", 743 .chip_id = 0x6367, 744 .chip_ver = 0x00A0, 745 .extints = { 746 { 6, 1, PHY_INTF(SGMII) | PHY_INTF(HSGMII) }, 747 { 7, 2, PHY_INTF(MII) | PHY_INTF(TMII) | 748 PHY_INTF(RMII) | PHY_INTF(RGMII) }, 749 }, 750 .jam_table = rtl8365mb_init_jam_8365mb_vc, 751 .jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc), 752 }, 753 { 754 .name = "RTL8367SB", 755 .chip_id = 0x6367, 756 .chip_ver = 0x0010, 757 .extints = { 758 { 6, 1, PHY_INTF(MII) | PHY_INTF(TMII) | 759 PHY_INTF(RMII) | PHY_INTF(RGMII) | 760 PHY_INTF(SGMII) | PHY_INTF(HSGMII) }, 761 { 7, 2, PHY_INTF(MII) | PHY_INTF(TMII) | 762 PHY_INTF(RMII) | PHY_INTF(RGMII) }, 763 }, 764 .jam_table = rtl8365mb_init_jam_8365mb_vc, 765 .jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc), 766 }, 767 { 768 .name = "RTL8367RB-VB", 769 .chip_id = 0x6367, 770 .chip_ver = 0x0020, 771 .extints = { 772 { 6, 1, PHY_INTF(MII) | PHY_INTF(TMII) | 773 PHY_INTF(RMII) | PHY_INTF(RGMII) }, 774 { 7, 2, PHY_INTF(MII) | PHY_INTF(TMII) | 775 PHY_INTF(RMII) | PHY_INTF(RGMII) }, 776 }, 777 .jam_table = rtl8365mb_init_jam_8365mb_vc, 778 .jam_size = ARRAY_SIZE(rtl8365mb_init_jam_8365mb_vc), 779 }, 780 }; 781 782 enum rtl8365mb_stp_state { 783 RTL8365MB_STP_STATE_DISABLED = 0, 784 RTL8365MB_STP_STATE_BLOCKING = 1, 785 RTL8365MB_STP_STATE_LEARNING = 2, 786 RTL8365MB_STP_STATE_FORWARDING = 3, 787 }; 788 789 enum rtl8365mb_cpu_insert { 790 RTL8365MB_CPU_INSERT_TO_ALL = 0, 791 RTL8365MB_CPU_INSERT_TO_TRAPPING = 1, 792 RTL8365MB_CPU_INSERT_TO_NONE = 2, 793 }; 794 795 enum rtl8365mb_cpu_position { 796 RTL8365MB_CPU_POS_AFTER_SA = 0, 797 RTL8365MB_CPU_POS_BEFORE_CRC = 1, 798 }; 799 800 enum rtl8365mb_cpu_format { 801 RTL8365MB_CPU_FORMAT_8BYTES = 0, 802 RTL8365MB_CPU_FORMAT_4BYTES = 1, 803 }; 804 805 enum rtl8365mb_cpu_rxlen { 806 RTL8365MB_CPU_RXLEN_72BYTES = 0, 807 RTL8365MB_CPU_RXLEN_64BYTES = 1, 808 }; 809 810 /** 811 * struct rtl8365mb_cpu - CPU port configuration 812 * @enable: enable/disable hardware insertion of CPU tag in switch->CPU frames 813 * @mask: port mask of ports that parse should parse CPU tags 814 * @trap_port: forward trapped frames to this port 815 * @insert: CPU tag insertion mode in switch->CPU frames 816 * @position: position of CPU tag in frame 817 * @rx_length: minimum CPU RX length 818 * @format: CPU tag format 819 * 820 * Represents the CPU tagging and CPU port configuration of the switch. These 821 * settings are configurable at runtime. 822 */ 823 struct rtl8365mb_cpu { 824 bool enable; 825 u32 mask; 826 u32 trap_port; 827 enum rtl8365mb_cpu_insert insert; 828 enum rtl8365mb_cpu_position position; 829 enum rtl8365mb_cpu_rxlen rx_length; 830 enum rtl8365mb_cpu_format format; 831 }; 832 833 /** 834 * struct rtl8365mb_port - private per-port data 835 * @priv: pointer to parent realtek_priv data 836 * @index: DSA port index, same as dsa_port::index 837 * @stats: link statistics populated by rtl8365mb_stats_poll, ready for atomic 838 * access via rtl8365mb_get_stats64 839 * @stats_lock: protect the stats structure during read/update 840 * @mib_work: delayed work for polling MIB counters 841 */ 842 struct rtl8365mb_port { 843 struct realtek_priv *priv; 844 unsigned int index; 845 struct rtnl_link_stats64 stats; 846 spinlock_t stats_lock; 847 struct delayed_work mib_work; 848 }; 849 850 /** 851 * struct rtl8365mb - driver private data 852 * @priv: pointer to parent realtek_priv data 853 * @irq: registered IRQ or zero 854 * @chip_info: chip-specific info about the attached switch 855 * @cpu: CPU tagging and CPU port configuration for this chip 856 * @mib_lock: prevent concurrent reads of MIB counters 857 * @ports: per-port data 858 * @pcs: PCS for the SerDes external interface 859 * @sds_supported: SerDes tuning parameters match the chip option, so the 860 * SerDes interface modes can be advertised 861 * 862 * Private data for this driver. 863 */ 864 struct rtl8365mb { 865 struct realtek_priv *priv; 866 int irq; 867 const struct rtl8365mb_chip_info *chip_info; 868 struct rtl8365mb_cpu cpu; 869 struct mutex mib_lock; 870 struct rtl8365mb_port ports[RTL8365MB_MAX_NUM_PORTS]; 871 struct phylink_pcs pcs; 872 bool sds_supported; 873 }; 874 875 #define pcs_to_rtl8365mb(_pcs) container_of((_pcs), struct rtl8365mb, pcs) 876 877 static int rtl8365mb_phy_poll_busy(struct realtek_priv *priv) 878 { 879 u32 val; 880 881 return regmap_read_poll_timeout(priv->map_nolock, 882 RTL8365MB_INDIRECT_ACCESS_STATUS_REG, 883 val, !val, 10, 100); 884 } 885 886 static int rtl8365mb_phy_ocp_prepare(struct realtek_priv *priv, int phy, 887 u32 ocp_addr) 888 { 889 u16 ocp_addr_lo = FIELD_GET(RTL8365MB_PHY_OCP_ADDR_5_1_MASK, ocp_addr); 890 u16 ocp_addr_hi = FIELD_GET(RTL8365MB_PHY_OCP_ADDR_9_6_MASK, ocp_addr); 891 u32 val; 892 int ret; 893 894 /* Set OCP prefix */ 895 val = FIELD_GET(RTL8365MB_PHY_OCP_ADDR_PREFIX_MASK, ocp_addr); 896 ret = regmap_update_bits( 897 priv->map_nolock, RTL8365MB_GPHY_OCP_MSB_0_REG, 898 RTL8365MB_GPHY_OCP_MSB_0_CFG_CPU_OCPADR_MASK, 899 FIELD_PREP(RTL8365MB_GPHY_OCP_MSB_0_CFG_CPU_OCPADR_MASK, val)); 900 if (ret) 901 return ret; 902 903 /* Set PHY register address */ 904 val = RTL8365MB_PHY_BASE; 905 val |= FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_ADDRESS_PHYNUM_MASK, phy); 906 val |= FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_ADDRESS_OCPADR_5_1_MASK, 907 ocp_addr_lo); 908 val |= FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_ADDRESS_OCPADR_9_6_MASK, 909 ocp_addr_hi); 910 ret = regmap_write(priv->map_nolock, 911 RTL8365MB_INDIRECT_ACCESS_ADDRESS_REG, val); 912 if (ret) 913 return ret; 914 915 return 0; 916 } 917 918 static int rtl8365mb_phy_ocp_read(struct realtek_priv *priv, int phy, 919 u32 ocp_addr, u16 *data) 920 { 921 u32 val; 922 int ret; 923 924 rtl83xx_lock(priv); 925 926 ret = rtl8365mb_phy_poll_busy(priv); 927 if (ret) 928 goto out; 929 930 ret = rtl8365mb_phy_ocp_prepare(priv, phy, ocp_addr); 931 if (ret) 932 goto out; 933 934 /* Execute read operation */ 935 val = FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_MASK, 936 RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_VALUE) | 937 FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_CTRL_RW_MASK, 938 RTL8365MB_INDIRECT_ACCESS_CTRL_RW_READ); 939 ret = regmap_write(priv->map_nolock, RTL8365MB_INDIRECT_ACCESS_CTRL_REG, 940 val); 941 if (ret) 942 goto out; 943 944 ret = rtl8365mb_phy_poll_busy(priv); 945 if (ret) 946 goto out; 947 948 /* Get PHY register data */ 949 ret = regmap_read(priv->map_nolock, 950 RTL8365MB_INDIRECT_ACCESS_READ_DATA_REG, &val); 951 if (ret) 952 goto out; 953 954 *data = val & 0xFFFF; 955 956 out: 957 rtl83xx_unlock(priv); 958 959 return ret; 960 } 961 962 static int rtl8365mb_phy_ocp_write(struct realtek_priv *priv, int phy, 963 u32 ocp_addr, u16 data) 964 { 965 u32 val; 966 int ret; 967 968 rtl83xx_lock(priv); 969 970 ret = rtl8365mb_phy_poll_busy(priv); 971 if (ret) 972 goto out; 973 974 ret = rtl8365mb_phy_ocp_prepare(priv, phy, ocp_addr); 975 if (ret) 976 goto out; 977 978 /* Set PHY register data */ 979 ret = regmap_write(priv->map_nolock, 980 RTL8365MB_INDIRECT_ACCESS_WRITE_DATA_REG, data); 981 if (ret) 982 goto out; 983 984 /* Execute write operation */ 985 val = FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_MASK, 986 RTL8365MB_INDIRECT_ACCESS_CTRL_CMD_VALUE) | 987 FIELD_PREP(RTL8365MB_INDIRECT_ACCESS_CTRL_RW_MASK, 988 RTL8365MB_INDIRECT_ACCESS_CTRL_RW_WRITE); 989 ret = regmap_write(priv->map_nolock, RTL8365MB_INDIRECT_ACCESS_CTRL_REG, 990 val); 991 if (ret) 992 goto out; 993 994 ret = rtl8365mb_phy_poll_busy(priv); 995 if (ret) 996 goto out; 997 998 out: 999 rtl83xx_unlock(priv); 1000 1001 return ret; 1002 } 1003 1004 static int rtl8365mb_phy_read(struct realtek_priv *priv, int phy, int regnum) 1005 { 1006 u32 ocp_addr; 1007 u16 val; 1008 int ret; 1009 1010 if (phy > RTL8365MB_PHYADDRMAX) 1011 return -EINVAL; 1012 1013 if (regnum > RTL8365MB_PHYREGMAX) 1014 return -EINVAL; 1015 1016 ocp_addr = RTL8365MB_PHY_OCP_ADDR_PHYREG_BASE + regnum * 2; 1017 1018 ret = rtl8365mb_phy_ocp_read(priv, phy, ocp_addr, &val); 1019 if (ret) { 1020 dev_err(priv->dev, 1021 "failed to read PHY%d reg %02x @ %04x, ret %pe\n", phy, 1022 regnum, ocp_addr, ERR_PTR(ret)); 1023 return ret; 1024 } 1025 1026 dev_dbg(priv->dev, "read PHY%d register 0x%02x @ %04x, val <- %04x\n", 1027 phy, regnum, ocp_addr, val); 1028 1029 return val; 1030 } 1031 1032 static int rtl8365mb_phy_write(struct realtek_priv *priv, int phy, int regnum, 1033 u16 val) 1034 { 1035 u32 ocp_addr; 1036 int ret; 1037 1038 if (phy > RTL8365MB_PHYADDRMAX) 1039 return -EINVAL; 1040 1041 if (regnum > RTL8365MB_PHYREGMAX) 1042 return -EINVAL; 1043 1044 ocp_addr = RTL8365MB_PHY_OCP_ADDR_PHYREG_BASE + regnum * 2; 1045 1046 ret = rtl8365mb_phy_ocp_write(priv, phy, ocp_addr, val); 1047 if (ret) { 1048 dev_err(priv->dev, 1049 "failed to write PHY%d reg %02x @ %04x, ret %pe\n", phy, 1050 regnum, ocp_addr, ERR_PTR(ret)); 1051 return ret; 1052 } 1053 1054 dev_dbg(priv->dev, "write PHY%d register 0x%02x @ %04x, val -> %04x\n", 1055 phy, regnum, ocp_addr, val); 1056 1057 return 0; 1058 } 1059 1060 static int rtl8365mb_phy_read_c45(struct realtek_priv *priv, int phy, int devad, 1061 int regnum) 1062 { 1063 u32 ocp_addr; 1064 u16 val; 1065 int ret; 1066 1067 if (phy > RTL8365MB_PHYADDRMAX) 1068 return -EINVAL; 1069 1070 if (devad == MDIO_MMD_PCS && regnum == MDIO_PCS_EEE_ABLE) 1071 ocp_addr = RTL8365MB_PHY_OCP_ADDR_EEE_ABLE; 1072 else if (devad == MDIO_MMD_AN && regnum == MDIO_AN_EEE_ADV) 1073 ocp_addr = RTL8365MB_PHY_OCP_ADDR_EEE_ADV; 1074 else if (devad == MDIO_MMD_AN && regnum == MDIO_AN_EEE_LPABLE) 1075 ocp_addr = RTL8365MB_PHY_OCP_ADDR_EEE_LPABLE; 1076 else 1077 /* Only the EEE registers are mapped; others read as 0, as the 1078 * hardware does, so the generic MMD code is not tripped up by 1079 * an error. 1080 */ 1081 return 0; 1082 1083 ret = rtl8365mb_phy_ocp_read(priv, phy, ocp_addr, &val); 1084 if (ret) { 1085 dev_err(priv->dev, 1086 "failed to read PHY%d OCP %04x, ret %d\n", phy, ocp_addr, 1087 ret); 1088 return ret; 1089 } 1090 1091 return val; 1092 } 1093 1094 static int rtl8365mb_phy_write_c45(struct realtek_priv *priv, int phy, 1095 int devad, int regnum, u16 val) 1096 { 1097 int ret; 1098 1099 if (phy > RTL8365MB_PHYADDRMAX) 1100 return -EINVAL; 1101 1102 /* Only the EEE advertisement register is writable; writes to other 1103 * registers are ignored, as the hardware does. 1104 */ 1105 if (devad != MDIO_MMD_AN || regnum != MDIO_AN_EEE_ADV) 1106 return 0; 1107 1108 ret = rtl8365mb_phy_ocp_write(priv, phy, RTL8365MB_PHY_OCP_ADDR_EEE_ADV, 1109 val); 1110 if (ret) { 1111 dev_err(priv->dev, 1112 "failed to write PHY%d OCP %04x, ret %d\n", phy, 1113 RTL8365MB_PHY_OCP_ADDR_EEE_ADV, ret); 1114 return ret; 1115 } 1116 1117 return 0; 1118 } 1119 1120 static const struct rtl8365mb_extint * 1121 rtl8365mb_get_port_extint(struct realtek_priv *priv, int port) 1122 { 1123 struct rtl8365mb *mb = priv->chip_data; 1124 int i; 1125 1126 for (i = 0; i < RTL8365MB_MAX_NUM_EXTINTS; i++) { 1127 const struct rtl8365mb_extint *extint = 1128 &mb->chip_info->extints[i]; 1129 1130 if (!extint->supported_interfaces) 1131 continue; 1132 1133 if (extint->port == port) 1134 return extint; 1135 } 1136 1137 return NULL; 1138 } 1139 1140 static enum dsa_tag_protocol 1141 rtl8365mb_get_tag_protocol(struct dsa_switch *ds, int port, 1142 enum dsa_tag_protocol mp) 1143 { 1144 struct realtek_priv *priv = ds->priv; 1145 struct rtl8365mb_cpu *cpu; 1146 struct rtl8365mb *mb; 1147 1148 mb = priv->chip_data; 1149 cpu = &mb->cpu; 1150 1151 if (cpu->position == RTL8365MB_CPU_POS_BEFORE_CRC) 1152 return DSA_TAG_PROTO_RTL8_4T; 1153 1154 return DSA_TAG_PROTO_RTL8_4; 1155 } 1156 1157 static int rtl8365mb_ext_config_rgmii(struct realtek_priv *priv, int port, 1158 phy_interface_t interface) 1159 { 1160 const struct rtl8365mb_extint *extint = 1161 rtl8365mb_get_port_extint(priv, port); 1162 struct dsa_switch *ds = &priv->ds; 1163 struct device_node *dn; 1164 struct dsa_port *dp; 1165 int tx_delay = 0; 1166 int rx_delay = 0; 1167 u32 val; 1168 int ret; 1169 1170 if (!extint) 1171 return -ENODEV; 1172 1173 dp = dsa_to_port(ds, port); 1174 dn = dp->dn; 1175 1176 /* Set the RGMII TX/RX delay 1177 * 1178 * The Realtek vendor driver indicates the following possible 1179 * configuration settings: 1180 * 1181 * TX delay: 1182 * 0 = no delay, 1 = 2 ns delay 1183 * RX delay: 1184 * 0 = no delay, 7 = maximum delay 1185 * Each step is approximately 0.3 ns, so the maximum delay is about 1186 * 2.1 ns. 1187 * 1188 * The vendor driver also states that this must be configured *before* 1189 * forcing the external interface into a particular mode, which is done 1190 * in the rtl8365mb_phylink_mac_link_{up,down} functions. 1191 * 1192 * Only configure an RGMII TX (resp. RX) delay if the 1193 * tx-internal-delay-ps (resp. rx-internal-delay-ps) OF property is 1194 * specified. We ignore the detail of the RGMII interface mode 1195 * (RGMII_{RXID, TXID, etc.}), as this is considered to be a PHY-only 1196 * property. 1197 */ 1198 if (!of_property_read_u32(dn, "tx-internal-delay-ps", &val)) { 1199 val = val / 1000; /* convert to ns */ 1200 1201 if (val == 0 || val == 2) 1202 tx_delay = val / 2; 1203 else 1204 dev_warn(priv->dev, 1205 "RGMII TX delay must be 0 or 2 ns\n"); 1206 } 1207 1208 if (!of_property_read_u32(dn, "rx-internal-delay-ps", &val)) { 1209 val = DIV_ROUND_CLOSEST(val, 300); /* convert to 0.3 ns step */ 1210 1211 if (val <= 7) 1212 rx_delay = val; 1213 else 1214 dev_warn(priv->dev, 1215 "RGMII RX delay must be 0 to 2.1 ns\n"); 1216 } 1217 1218 ret = regmap_update_bits( 1219 priv->map, RTL8365MB_EXT_RGMXF_REG(extint->id), 1220 RTL8365MB_EXT_RGMXF_TXDELAY_MASK | 1221 RTL8365MB_EXT_RGMXF_RXDELAY_MASK, 1222 FIELD_PREP(RTL8365MB_EXT_RGMXF_TXDELAY_MASK, tx_delay) | 1223 FIELD_PREP(RTL8365MB_EXT_RGMXF_RXDELAY_MASK, rx_delay)); 1224 if (ret) 1225 return ret; 1226 1227 ret = regmap_update_bits( 1228 priv->map, RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(extint->id), 1229 RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(extint->id), 1230 RTL8365MB_EXT_PORT_MODE_RGMII 1231 << RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET( 1232 extint->id)); 1233 if (ret) 1234 return ret; 1235 1236 return 0; 1237 } 1238 1239 static int rtl8365mb_sds_write(struct realtek_priv *priv, u16 addr, u16 data) 1240 { 1241 int ret; 1242 1243 ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_DATA_REG, data); 1244 if (ret) 1245 return ret; 1246 1247 ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_ADR_REG, addr); 1248 if (ret) 1249 return ret; 1250 1251 /* The SerDes indirect access engine completes the command within the 1252 * register write transaction, so there is no need to wait or poll for 1253 * completion before the next access, matching the vendor driver. 1254 */ 1255 return regmap_write(priv->map, RTL8365MB_SDS_INDACS_CMD_REG, 1256 RTL8365MB_SDS_INDACS_CMD_RUN_MASK | 1257 RTL8365MB_SDS_INDACS_CMD_WR_MASK); 1258 } 1259 1260 static int rtl8365mb_sds_read(struct realtek_priv *priv, u16 addr, u16 *data) 1261 { 1262 u32 val; 1263 int ret; 1264 1265 ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_ADR_REG, addr); 1266 if (ret) 1267 return ret; 1268 1269 ret = regmap_write(priv->map, RTL8365MB_SDS_INDACS_CMD_REG, 1270 RTL8365MB_SDS_INDACS_CMD_RUN_MASK); 1271 if (ret) 1272 return ret; 1273 1274 /* Wait for the indirect read to complete: the engine clears the BUSY 1275 * bit once the data register holds the result. 1276 */ 1277 ret = regmap_read_poll_timeout(priv->map, RTL8365MB_SDS_INDACS_CMD_REG, 1278 val, 1279 !(val & RTL8365MB_SDS_INDACS_CMD_BUSY_MASK), 1280 10, 1000); 1281 if (ret) 1282 return ret; 1283 1284 ret = regmap_read(priv->map, RTL8365MB_SDS_INDACS_DATA_REG, &val); 1285 if (ret) 1286 return ret; 1287 1288 *data = val; 1289 1290 return 0; 1291 } 1292 1293 /* The vendor driver selects between two sets of SerDes tuning parameters based 1294 * on the chip option register. Only the variant for a non-zero option has been 1295 * tested on real hardware - the RTL8367S parts seen so far all report 1. The 1296 * variant for option 0 uses different tuning values that cannot be verified, 1297 * so probe the option once at setup and only advertise the SerDes interface 1298 * modes when the tuning parameters are known to match, so that an unsupported 1299 * variant fails at phylink validation time rather than when configuring the 1300 * link. 1301 */ 1302 static int rtl8365mb_sds_probe_option(struct realtek_priv *priv) 1303 { 1304 struct rtl8365mb *mb = priv->chip_data; 1305 const struct rtl8365mb_extint *extint; 1306 u32 option; 1307 int ret; 1308 int i; 1309 1310 /* Nothing to probe if no external interface is wired to the SerDes */ 1311 for (i = 0; i < RTL8365MB_MAX_NUM_EXTINTS; i++) { 1312 extint = &mb->chip_info->extints[i]; 1313 1314 if (extint->supported_interfaces & 1315 (RTL8365MB_PHY_INTERFACE_MODE_SGMII | 1316 RTL8365MB_PHY_INTERFACE_MODE_HSGMII)) 1317 break; 1318 } 1319 if (i == RTL8365MB_MAX_NUM_EXTINTS) 1320 return 0; 1321 1322 ret = regmap_write(priv->map, RTL8365MB_SDS_OPTION_ARM_REG, 1323 RTL8365MB_SDS_OPTION_ARM_KEY); 1324 if (ret) 1325 return ret; 1326 1327 ret = regmap_read(priv->map, RTL8365MB_SDS_OPTION_REG, &option); 1328 if (ret) 1329 return ret; 1330 1331 ret = regmap_write(priv->map, RTL8365MB_SDS_OPTION_ARM_REG, 0); 1332 if (ret) 1333 return ret; 1334 1335 if (option == 0) { 1336 dev_warn(priv->dev, 1337 "unsupported SerDes tuning variant (chip option 0), disabling SerDes interface modes\n"); 1338 return 0; 1339 } 1340 1341 mb->sds_supported = true; 1342 1343 return 0; 1344 } 1345 1346 /* The vendor driver raises the port 6 ingress and egress rate limiters to 1347 * their maximum in its switch init, unconditionally for the whole chip 1348 * family. The chip reset in rtl8365mb_setup() puts them back to their reset 1349 * default of 0x1FFFF, a ~1.048 Gbps limit which caps the aggregate 1350 * throughput of an HSGMII CPU port at roughly 1 Gbps. The vendor 1351 * documentation describes the reset default as disabling the limiter, but 1352 * the cap has been observed on hardware. Raise them likewise, to 0x7FFFF 1353 * (~4.19 Gbps, above the HSGMII line rate). The related HSGMII scheduler 1354 * line rate register (LINE_RATE_HSG_H, 0x03FA) is already set to its 1355 * maximum by the common init jam table. 1356 */ 1357 static int rtl8365mb_sds_raise_rate_limits(struct realtek_priv *priv) 1358 { 1359 int ret; 1360 1361 ret = regmap_write(priv->map, RTL8365MB_INGRESSBW_PORT6_RATE_CTRL0_REG, 1362 0xFFFF); 1363 if (ret) 1364 return ret; 1365 1366 ret = regmap_update_bits(priv->map, 1367 RTL8365MB_INGRESSBW_PORT6_RATE_CTRL1_REG, 1368 RTL8365MB_INGRESSBW_PORT6_RATE_CTRL1_MASK, 1369 RTL8365MB_INGRESSBW_PORT6_RATE_CTRL1_MASK); 1370 if (ret) 1371 return ret; 1372 1373 ret = regmap_write(priv->map, RTL8365MB_PORT6_EGRESSBW_CTRL0_REG, 1374 0xFFFF); 1375 if (ret) 1376 return ret; 1377 1378 return regmap_update_bits(priv->map, RTL8365MB_PORT6_EGRESSBW_CTRL1_REG, 1379 RTL8365MB_PORT6_EGRESSBW_CTRL1_MASK, 1380 RTL8365MB_PORT6_EGRESSBW_CTRL1_MASK); 1381 } 1382 1383 static int rtl8365mb_pcs_config(struct phylink_pcs *pcs, unsigned int neg_mode, 1384 phy_interface_t interface, 1385 const unsigned long *advertising, 1386 bool permit_pause_to_mac) 1387 { 1388 const struct rtl8365mb_jam_tbl_entry *sds_jam; 1389 const int id = RTL8365MB_SDS_EXT_INTERFACE_ID; 1390 struct rtl8365mb *mb = pcs_to_rtl8365mb(pcs); 1391 struct realtek_priv *priv; 1392 size_t sds_jam_size; 1393 u32 mode; 1394 u16 val; 1395 int ret; 1396 int i; 1397 1398 priv = mb->priv; 1399 1400 if (interface == PHY_INTERFACE_MODE_2500BASEX) { 1401 sds_jam = rtl8365mb_sds_jam_hsgmii; 1402 sds_jam_size = ARRAY_SIZE(rtl8365mb_sds_jam_hsgmii); 1403 mode = RTL8365MB_EXT_PORT_MODE_HSGMII; 1404 } else { 1405 sds_jam = rtl8365mb_sds_jam_sgmii; 1406 sds_jam_size = ARRAY_SIZE(rtl8365mb_sds_jam_sgmii); 1407 mode = RTL8365MB_EXT_PORT_MODE_SGMII; 1408 } 1409 1410 /* Hold the embedded DW8051 microcontroller in reset and keep it 1411 * disabled. The vendor driver loads firmware into it to manage the 1412 * SerDes link, but the firmware only duplicates work that phylink 1413 * already does: it polls the port status and forces the external 1414 * interface configuration in the very registers this driver manages. 1415 * Letting it run would race with phylink. 1416 */ 1417 ret = regmap_update_bits(priv->map, RTL8365MB_CHIP_RESET_REG, 1418 RTL8365MB_CHIP_RESET_DW8051_MASK, 1419 RTL8365MB_CHIP_RESET_DW8051_MASK); 1420 if (ret) 1421 return ret; 1422 1423 ret = regmap_update_bits(priv->map, RTL8365MB_MISC_CFG0_REG, 1424 RTL8365MB_MISC_CFG0_DW8051_EN_MASK, 0); 1425 if (ret) 1426 return ret; 1427 1428 /* The vendor driver clears the line rate bypass for all interface 1429 * modes except TMII. 1430 */ 1431 ret = regmap_update_bits(priv->map, RTL8365MB_BYPASS_LINE_RATE_REG, 1432 RTL8365MB_SDS_BYPASS_LINE_RATE_MASK, 0); 1433 if (ret) 1434 return ret; 1435 1436 /* Tune the SerDes with vendor-prescribed parameters */ 1437 for (i = 0; i < sds_jam_size; i++) { 1438 ret = rtl8365mb_sds_write(priv, sds_jam[i].reg, 1439 sds_jam[i].val); 1440 if (ret) 1441 return ret; 1442 } 1443 1444 /* Mux the SerDes to MAC8 in the requested mode */ 1445 ret = regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG, 1446 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK | 1447 RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK, 1448 mode == RTL8365MB_EXT_PORT_MODE_SGMII ? 1449 RTL8365MB_SDS_MISC_MAC8_SEL_SGMII_MASK : 1450 RTL8365MB_SDS_MISC_MAC8_SEL_HSGMII_MASK); 1451 if (ret) 1452 return ret; 1453 1454 val = mode << RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_OFFSET(id); 1455 ret = regmap_update_bits(priv->map, 1456 RTL8365MB_DIGITAL_INTERFACE_SELECT_REG(id), 1457 RTL8365MB_DIGITAL_INTERFACE_SELECT_MODE_MASK(id), 1458 val); 1459 if (ret) 1460 return ret; 1461 1462 /* Take the SerDes out of reset. The vendor driver does this only 1463 * after the SerDes mux and the interface mode are configured. 1464 */ 1465 ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_RESET, 1466 RTL8365MB_SDS_RESET_DEASSERT); 1467 if (ret) 1468 return ret; 1469 1470 /* Reset the SerDes data path and resync its PLL, mirroring what the 1471 * vendor firmware does right after deasserting the SerDes reset. 1472 * This flushes the FIFOs and ensures a clean state for the link, 1473 * preventing silent drops and CRC errors. 1474 */ 1475 ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR, 1476 RTL8365MB_SDS_BMCR_DPRST_PHASE1); 1477 if (ret) 1478 return ret; 1479 1480 ret = rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_BMCR, 1481 RTL8365MB_SDS_BMCR_DPRST_PHASE2); 1482 if (ret) 1483 return ret; 1484 1485 /* Keep SGMII in-band autonegotiation disabled: the link parameters are 1486 * forced from rtl8365mb_pcs_link_up() instead. 1487 */ 1488 ret = rtl8365mb_sds_read(priv, RTL8365MB_SDS_REG_NWAY, &val); 1489 if (ret) 1490 return ret; 1491 1492 val &= ~RTL8365MB_SDS_NWAY_EN_MASK; 1493 val |= RTL8365MB_SDS_NWAY_RESTART_MASK; 1494 1495 return rtl8365mb_sds_write(priv, RTL8365MB_SDS_REG_NWAY, val); 1496 } 1497 1498 static bool rtl8365mb_interface_is_serdes(phy_interface_t interface) 1499 { 1500 return interface == PHY_INTERFACE_MODE_SGMII || 1501 interface == PHY_INTERFACE_MODE_2500BASEX; 1502 } 1503 1504 static unsigned int rtl8365mb_pcs_inband_caps(struct phylink_pcs *pcs, 1505 phy_interface_t interface) 1506 { 1507 /* In-band autonegotiation is not implemented; the link is always 1508 * forced. Report that to phylink so that it never selects an 1509 * in-band-enabled negotiation mode for this PCS. 1510 */ 1511 return LINK_INBAND_DISABLE; 1512 } 1513 1514 static void rtl8365mb_pcs_get_state(struct phylink_pcs *pcs, 1515 unsigned int neg_mode, 1516 struct phylink_link_state *state) 1517 { 1518 struct rtl8365mb *mb = pcs_to_rtl8365mb(pcs); 1519 struct realtek_priv *priv = mb->priv; 1520 u16 status; 1521 u32 val; 1522 int ret; 1523 1524 /* In-band autonegotiation is not implemented, so the link parameters are 1525 * forced from rtl8365mb_pcs_link_up(). The real link state must still be 1526 * read from the SerDes itself: the embedded DW8051 microcontroller that 1527 * the vendor firmware uses to poll the SerDes is kept disabled (see 1528 * rtl8365mb_pcs_config()), so the link status register can be read 1529 * directly through the SDS_INDACS window without racing the auto-poll. 1530 */ 1531 ret = rtl8365mb_sds_read(priv, RTL8365MB_SDS_REG_LINK_STATUS, &status); 1532 if (ret) { 1533 state->link = false; 1534 return; 1535 } 1536 1537 state->link = !!(status & RTL8365MB_SDS_LINK_STATUS_LINK_MASK); 1538 state->an_complete = state->link; 1539 if (!state->link) 1540 return; 1541 1542 /* The speed and duplex are forced; read them back from the values 1543 * programmed into the SerDes MISC register. 1544 */ 1545 ret = regmap_read(priv->map, RTL8365MB_SDS_MISC_REG, &val); 1546 if (ret) { 1547 state->link = false; 1548 return; 1549 } 1550 1551 state->duplex = (val & RTL8365MB_SDS_MISC_SGMII_FDUP_MASK) ? 1552 DUPLEX_FULL : DUPLEX_HALF; 1553 1554 switch (FIELD_GET(RTL8365MB_SDS_MISC_SGMII_SPD_MASK, val)) { 1555 case RTL8365MB_PORT_SPEED_1000M: 1556 state->speed = 1557 state->interface == PHY_INTERFACE_MODE_2500BASEX ? 1558 SPEED_2500 : SPEED_1000; 1559 break; 1560 case RTL8365MB_PORT_SPEED_100M: 1561 state->speed = SPEED_100; 1562 break; 1563 case RTL8365MB_PORT_SPEED_10M: 1564 state->speed = SPEED_10; 1565 break; 1566 } 1567 } 1568 1569 static void rtl8365mb_pcs_link_up(struct phylink_pcs *pcs, 1570 unsigned int neg_mode, 1571 phy_interface_t interface, int speed, 1572 int duplex) 1573 { 1574 struct rtl8365mb *mb = pcs_to_rtl8365mb(pcs); 1575 struct realtek_priv *priv = mb->priv; 1576 u32 mask = RTL8365MB_SDS_MISC_SGMII_FDUP_MASK | 1577 RTL8365MB_SDS_MISC_SGMII_LINK_MASK | 1578 RTL8365MB_SDS_MISC_SGMII_SPD_MASK; 1579 u32 val = RTL8365MB_SDS_MISC_SGMII_LINK_MASK; 1580 u32 r_speed; 1581 int ret; 1582 1583 /* The speed field has no value for 2.5 Gbps: the rate is determined by 1584 * the HSGMII SerDes configuration, and the vendor driver programs the 1585 * 1 Gbps value here. 1586 */ 1587 if (speed == SPEED_2500 || speed == SPEED_1000) { 1588 r_speed = RTL8365MB_PORT_SPEED_1000M; 1589 } else if (speed == SPEED_100) { 1590 r_speed = RTL8365MB_PORT_SPEED_100M; 1591 } else if (speed == SPEED_10) { 1592 r_speed = RTL8365MB_PORT_SPEED_10M; 1593 } else { 1594 dev_err(priv->dev, "unsupported SerDes speed %s\n", 1595 phy_speed_to_str(speed)); 1596 return; 1597 } 1598 1599 val |= FIELD_PREP(RTL8365MB_SDS_MISC_SGMII_SPD_MASK, r_speed); 1600 1601 if (duplex == DUPLEX_FULL) 1602 val |= RTL8365MB_SDS_MISC_SGMII_FDUP_MASK; 1603 1604 /* pcs_link_up() carries no pause information, so the SerDes flow 1605 * control bits are programmed together with the MAC external interface 1606 * force from rtl8365mb_phylink_mac_link_up(), where the resolved pause 1607 * modes are known. 1608 */ 1609 ret = regmap_update_bits(priv->map, RTL8365MB_SDS_MISC_REG, mask, val); 1610 if (ret) { 1611 dev_err(priv->dev, "failed to force SerDes link: %pe\n", 1612 ERR_PTR(ret)); 1613 return; 1614 } 1615 } 1616 1617 static const struct phylink_pcs_ops rtl8365mb_pcs_ops = { 1618 .pcs_inband_caps = rtl8365mb_pcs_inband_caps, 1619 .pcs_config = rtl8365mb_pcs_config, 1620 .pcs_get_state = rtl8365mb_pcs_get_state, 1621 .pcs_link_up = rtl8365mb_pcs_link_up, 1622 }; 1623 1624 static int rtl8365mb_ext_config_forcemode(struct realtek_priv *priv, int port, 1625 bool link, int speed, int duplex, 1626 bool tx_pause, bool rx_pause) 1627 { 1628 const struct rtl8365mb_extint *extint = 1629 rtl8365mb_get_port_extint(priv, port); 1630 u32 r_tx_pause; 1631 u32 r_rx_pause; 1632 u32 r_duplex; 1633 u32 r_speed; 1634 u32 r_link; 1635 int val; 1636 int ret; 1637 1638 if (!extint) 1639 return -ENODEV; 1640 1641 if (link) { 1642 /* Force the link up with the desired configuration */ 1643 r_link = 1; 1644 r_rx_pause = rx_pause ? 1 : 0; 1645 r_tx_pause = tx_pause ? 1 : 0; 1646 1647 /* The speed field has no value for 2.5 Gbps: the rate is 1648 * determined by the HSGMII SerDes configuration, and the 1649 * vendor driver programs the 1 Gbps value here. 1650 */ 1651 if (speed == SPEED_2500 || speed == SPEED_1000) { 1652 r_speed = RTL8365MB_PORT_SPEED_1000M; 1653 } else if (speed == SPEED_100) { 1654 r_speed = RTL8365MB_PORT_SPEED_100M; 1655 } else if (speed == SPEED_10) { 1656 r_speed = RTL8365MB_PORT_SPEED_10M; 1657 } else { 1658 dev_err(priv->dev, "unsupported port speed %s\n", 1659 phy_speed_to_str(speed)); 1660 return -EINVAL; 1661 } 1662 1663 if (duplex == DUPLEX_FULL) { 1664 r_duplex = 1; 1665 } else if (duplex == DUPLEX_HALF) { 1666 r_duplex = 0; 1667 } else { 1668 dev_err(priv->dev, "unsupported duplex %s\n", 1669 phy_duplex_to_str(duplex)); 1670 return -EINVAL; 1671 } 1672 } else { 1673 /* Force the link down and reset any programmed configuration */ 1674 r_link = 0; 1675 r_tx_pause = 0; 1676 r_rx_pause = 0; 1677 r_speed = 0; 1678 r_duplex = 0; 1679 } 1680 1681 val = FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_EN_MASK, 1) | 1682 FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_TXPAUSE_MASK, 1683 r_tx_pause) | 1684 FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_RXPAUSE_MASK, 1685 r_rx_pause) | 1686 FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_LINK_MASK, r_link) | 1687 FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_DUPLEX_MASK, 1688 r_duplex) | 1689 FIELD_PREP(RTL8365MB_DIGITAL_INTERFACE_FORCE_SPEED_MASK, r_speed); 1690 ret = regmap_write(priv->map, 1691 RTL8365MB_DIGITAL_INTERFACE_FORCE_REG(extint->id), 1692 val); 1693 if (ret) 1694 return ret; 1695 1696 return 0; 1697 } 1698 1699 static void rtl8365mb_phylink_get_caps(struct dsa_switch *ds, int port, 1700 struct phylink_config *config) 1701 { 1702 const struct rtl8365mb_extint *extint = 1703 rtl8365mb_get_port_extint(ds->priv, port); 1704 struct realtek_priv *priv = ds->priv; 1705 struct rtl8365mb *mb = priv->chip_data; 1706 1707 config->mac_capabilities = MAC_SYM_PAUSE | MAC_ASYM_PAUSE | 1708 MAC_10 | MAC_100 | MAC_1000FD; 1709 1710 if (!extint) { 1711 __set_bit(PHY_INTERFACE_MODE_INTERNAL, 1712 config->supported_interfaces); 1713 1714 /* GMII is the default interface mode for phylib, so 1715 * we have to support it for ports with integrated PHY. 1716 */ 1717 __set_bit(PHY_INTERFACE_MODE_GMII, 1718 config->supported_interfaces); 1719 1720 /* Integrated PHYs support EEE at 100M/1G; the hardware manages 1721 * LPI on its own, so just advertise LPI awareness to phylink. 1722 */ 1723 memcpy(config->lpi_interfaces, config->supported_interfaces, 1724 sizeof(config->lpi_interfaces)); 1725 config->lpi_capabilities = MAC_100FD | MAC_1000FD; 1726 config->eee_enabled_default = true; 1727 return; 1728 } 1729 1730 /* Populate according to the modes supported by _this driver_, 1731 * not necessarily the modes supported by the hardware, some of 1732 * which remain unimplemented. 1733 */ 1734 1735 if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_RGMII) 1736 phy_interface_set_rgmii(config->supported_interfaces); 1737 1738 if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_SGMII && 1739 mb->sds_supported) 1740 __set_bit(PHY_INTERFACE_MODE_SGMII, 1741 config->supported_interfaces); 1742 1743 if (extint->supported_interfaces & RTL8365MB_PHY_INTERFACE_MODE_HSGMII && 1744 mb->sds_supported) { 1745 __set_bit(PHY_INTERFACE_MODE_2500BASEX, 1746 config->supported_interfaces); 1747 config->mac_capabilities |= MAC_2500FD; 1748 } 1749 } 1750 1751 static struct phylink_pcs * 1752 rtl8365mb_phylink_mac_select_pcs(struct phylink_config *config, 1753 phy_interface_t interface) 1754 { 1755 struct dsa_port *dp = dsa_phylink_to_port(config); 1756 struct realtek_priv *priv = dp->ds->priv; 1757 struct rtl8365mb *mb = priv->chip_data; 1758 1759 if (rtl8365mb_interface_is_serdes(interface)) 1760 return &mb->pcs; 1761 1762 return NULL; 1763 } 1764 1765 static void rtl8365mb_phylink_mac_config(struct phylink_config *config, 1766 unsigned int mode, 1767 const struct phylink_link_state *state) 1768 { 1769 struct dsa_port *dp = dsa_phylink_to_port(config); 1770 struct realtek_priv *priv = dp->ds->priv; 1771 u8 port = dp->index; 1772 int ret; 1773 1774 if (mode != MLO_AN_PHY && mode != MLO_AN_FIXED) { 1775 dev_err(priv->dev, 1776 "port %d supports only conventional PHY or fixed-link\n", 1777 port); 1778 return; 1779 } 1780 1781 if (phy_interface_mode_is_rgmii(state->interface)) { 1782 ret = rtl8365mb_ext_config_rgmii(priv, port, state->interface); 1783 if (ret) 1784 dev_err(priv->dev, 1785 "failed to configure RGMII mode on port %d: %pe\n", 1786 port, ERR_PTR(ret)); 1787 return; 1788 } 1789 1790 /* SGMII and 2500base-x are handled by the SerDes PCS, configured 1791 * through the phylink_pcs ops, so nothing to do here for them. 1792 */ 1793 if (rtl8365mb_interface_is_serdes(state->interface)) 1794 return; 1795 1796 /* TODO: Implement MII and RMII modes, which the RTL8365MB-VC also 1797 * supports 1798 */ 1799 } 1800 1801 static void rtl8365mb_phylink_mac_link_down(struct phylink_config *config, 1802 unsigned int mode, 1803 phy_interface_t interface) 1804 { 1805 struct dsa_port *dp = dsa_phylink_to_port(config); 1806 struct realtek_priv *priv = dp->ds->priv; 1807 struct rtl8365mb_port *p; 1808 struct rtl8365mb *mb; 1809 u8 port = dp->index; 1810 int ret; 1811 1812 mb = priv->chip_data; 1813 p = &mb->ports[port]; 1814 cancel_delayed_work_sync(&p->mib_work); 1815 1816 /* phylink has no pcs_link_down callback, so on the SerDes path only the 1817 * MAC external interface force is reset here. Clearing the MAC force is 1818 * enough to bring the link down; the SerDes keeps presenting its last 1819 * forced state until the next pcs_link_up() reprograms it. 1820 */ 1821 if (phy_interface_mode_is_rgmii(interface) || 1822 rtl8365mb_interface_is_serdes(interface)) { 1823 ret = rtl8365mb_ext_config_forcemode(priv, port, false, 0, 0, 1824 false, false); 1825 if (ret) 1826 dev_err(priv->dev, 1827 "failed to reset forced mode on port %d: %pe\n", 1828 port, ERR_PTR(ret)); 1829 1830 return; 1831 } 1832 } 1833 1834 static void rtl8365mb_phylink_mac_link_up(struct phylink_config *config, 1835 struct phy_device *phydev, 1836 unsigned int mode, 1837 phy_interface_t interface, 1838 int speed, int duplex, bool tx_pause, 1839 bool rx_pause) 1840 { 1841 struct dsa_port *dp = dsa_phylink_to_port(config); 1842 struct realtek_priv *priv = dp->ds->priv; 1843 struct rtl8365mb_port *p; 1844 struct rtl8365mb *mb; 1845 u8 port = dp->index; 1846 int ret; 1847 1848 mb = priv->chip_data; 1849 p = &mb->ports[port]; 1850 schedule_delayed_work(&p->mib_work, 0); 1851 1852 /* The SerDes forced link state is programmed by the PCS in 1853 * rtl8365mb_pcs_link_up(); here only the MAC external interface force 1854 * is configured, for both RGMII and SerDes. 1855 */ 1856 if (phy_interface_mode_is_rgmii(interface) || 1857 rtl8365mb_interface_is_serdes(interface)) { 1858 ret = rtl8365mb_ext_config_forcemode(priv, port, true, speed, 1859 duplex, tx_pause, 1860 rx_pause); 1861 if (ret) { 1862 dev_err(priv->dev, 1863 "failed to force mode on port %d: %pe\n", port, 1864 ERR_PTR(ret)); 1865 return; 1866 } 1867 1868 /* The SerDes has its own pause enables; program them from 1869 * the resolved pause modes, as the vendor driver does when 1870 * forcing the link on a SerDes external interface. These 1871 * bits, not the MAC force pause bits, gate pause on the 1872 * SerDes external interface: flow control testing shows 1873 * that pause frames are only emitted with the SerDes TXFC 1874 * bit set, while the MAC force pause bits alone have no 1875 * effect on this port. This is done here rather than in 1876 * rtl8365mb_pcs_link_up() because pcs_link_up() carries no 1877 * pause information. 1878 */ 1879 if (rtl8365mb_interface_is_serdes(interface)) { 1880 u32 val = 0; 1881 1882 if (tx_pause) 1883 val |= RTL8365MB_SDS_MISC_SGMII_TXFC_MASK; 1884 if (rx_pause) 1885 val |= RTL8365MB_SDS_MISC_SGMII_RXFC_MASK; 1886 1887 ret = regmap_update_bits(priv->map, 1888 RTL8365MB_SDS_MISC_REG, 1889 RTL8365MB_SDS_MISC_SGMII_TXFC_MASK | 1890 RTL8365MB_SDS_MISC_SGMII_RXFC_MASK, 1891 val); 1892 if (ret) 1893 dev_err(priv->dev, 1894 "failed to force SerDes pause modes on port %d: %pe\n", 1895 port, ERR_PTR(ret)); 1896 } 1897 1898 return; 1899 } 1900 } 1901 1902 static int rtl8365mb_port_change_mtu(struct dsa_switch *ds, int port, 1903 int new_mtu) 1904 { 1905 struct realtek_priv *priv = ds->priv; 1906 int frame_size; 1907 1908 /* When a new MTU is set, DSA always sets the CPU port's MTU to the 1909 * largest MTU of the user ports. Because the switch only has a global 1910 * RX length register, only allowing CPU port here is enough. 1911 */ 1912 if (!dsa_is_cpu_port(ds, port)) 1913 return 0; 1914 1915 frame_size = new_mtu + VLAN_ETH_HLEN + ETH_FCS_LEN; 1916 1917 dev_dbg(priv->dev, "changing mtu to %d (frame size: %d)\n", 1918 new_mtu, frame_size); 1919 1920 return regmap_update_bits(priv->map, RTL8365MB_CFG0_MAX_LEN_REG, 1921 RTL8365MB_CFG0_MAX_LEN_MASK, 1922 FIELD_PREP(RTL8365MB_CFG0_MAX_LEN_MASK, 1923 frame_size)); 1924 } 1925 1926 static int rtl8365mb_port_max_mtu(struct dsa_switch *ds, int port) 1927 { 1928 return RTL8365MB_CFG0_MAX_LEN_MAX - VLAN_ETH_HLEN - ETH_FCS_LEN; 1929 } 1930 1931 static void rtl8365mb_port_stp_state_set(struct dsa_switch *ds, int port, 1932 u8 state) 1933 { 1934 struct realtek_priv *priv = ds->priv; 1935 enum rtl8365mb_stp_state val; 1936 int msti = 0; 1937 1938 switch (state) { 1939 case BR_STATE_DISABLED: 1940 val = RTL8365MB_STP_STATE_DISABLED; 1941 break; 1942 case BR_STATE_BLOCKING: 1943 case BR_STATE_LISTENING: 1944 val = RTL8365MB_STP_STATE_BLOCKING; 1945 break; 1946 case BR_STATE_LEARNING: 1947 val = RTL8365MB_STP_STATE_LEARNING; 1948 break; 1949 case BR_STATE_FORWARDING: 1950 val = RTL8365MB_STP_STATE_FORWARDING; 1951 break; 1952 default: 1953 dev_err(priv->dev, "invalid STP state: %u\n", state); 1954 return; 1955 } 1956 1957 regmap_update_bits(priv->map, RTL8365MB_MSTI_CTRL_REG(msti, port), 1958 RTL8365MB_MSTI_CTRL_PORT_STATE_MASK(port), 1959 val << RTL8365MB_MSTI_CTRL_PORT_STATE_OFFSET(port)); 1960 } 1961 1962 static int rtl8365mb_port_set_transparent(struct realtek_priv *priv, 1963 int igr_port, int egr_port, 1964 bool enable) 1965 { 1966 dev_dbg(priv->dev, "%s transparent VLAN from %d to %d\n", 1967 enable ? "Enable" : "Disable", igr_port, egr_port); 1968 1969 /* "Transparent" between the two ports means that packets forwarded by 1970 * igr_port and egressed on egr_port will not be filtered by the usual 1971 * VLAN membership settings. 1972 */ 1973 return regmap_update_bits(priv->map, 1974 RTL8365MB_VLAN_EGRESS_TRANSPARENT_REG(egr_port), 1975 BIT(igr_port), enable ? BIT(igr_port) : 0); 1976 } 1977 1978 static int rtl8365mb_port_set_ingress_filtering(struct realtek_priv *priv, 1979 int port, bool enable) 1980 { 1981 /* Ingress filtering enabled: Discard VLAN-tagged frames if the port is 1982 * not a member of the VLAN with which the packet is associated. 1983 * Untagged packets will also be discarded unless the port has a PVID 1984 * programmed. Priority-tagged frames are treated as untagged frames. 1985 * 1986 * Ingress filtering disabled: Accept all tagged and untagged frames. 1987 */ 1988 return regmap_update_bits(priv->map, RTL8365MB_VLAN_INGRESS_REG, 1989 RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port), 1990 enable ? 1991 RTL8365MB_VLAN_INGRESS_FILTER_PORT_EN_MASK(port) : 1992 0); 1993 } 1994 1995 static int 1996 rtl8365mb_port_set_vlan_egress_mode(struct realtek_priv *priv, int port, 1997 enum rtl8365mb_vlan_egress_mode mode) 1998 { 1999 u32 val; 2000 2001 val = FIELD_PREP(RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, mode); 2002 return regmap_update_bits(priv->map, 2003 RTL8365MB_PORT_MISC_CFG_REG(port), 2004 RTL8365MB_PORT_MISC_CFG_VLAN_EGRESS_MODE_MASK, val); 2005 } 2006 2007 static int rtl8365mb_port_vlan_filtering(struct dsa_switch *ds, int port, 2008 bool vlan_filtering, 2009 struct netlink_ext_ack *extack) 2010 { 2011 enum rtl8365mb_frame_ingress accepted_frame, prev_accepted_frame; 2012 enum rtl8365mb_vlan_egress_mode mode; 2013 struct realtek_priv *priv = ds->priv; 2014 u32 configured_ports = 0; 2015 struct dsa_port *dp; 2016 u16 pvid_vid; 2017 int ret; 2018 2019 dev_dbg(priv->dev, "port %d: %s VLAN filtering\n", port, 2020 vlan_filtering ? "enable" : "disable"); 2021 2022 ret = rtl8365mb_vlan_port_get_framefilter(priv, port, 2023 &prev_accepted_frame); 2024 if (ret) { 2025 NL_SET_ERR_MSG_MOD(extack, 2026 "Failed to get current framefilter"); 2027 return ret; 2028 } 2029 2030 /* While filtering, only accepts untagged frames if PVID is enabled */ 2031 if (vlan_filtering) { 2032 ret = rtl8365mb_vlan_port_get_pvid(priv, port, &pvid_vid); 2033 if (ret) 2034 return ret; 2035 2036 if (pvid_vid) 2037 accepted_frame = RTL8365MB_FRAME_TYPE_ANY_FRAME; 2038 else 2039 accepted_frame = RTL8365MB_FRAME_TYPE_TAGGED_ONLY; 2040 } else { 2041 accepted_frame = RTL8365MB_FRAME_TYPE_ANY_FRAME; 2042 } 2043 2044 /* When vlan filter is enable/disabled in a bridge, this function is 2045 * called for all member ports. We need to enable/disable ingress 2046 * VLAN membership check. 2047 */ 2048 ret = rtl8365mb_port_set_ingress_filtering(priv, port, vlan_filtering); 2049 if (ret) 2050 return ret; 2051 2052 /* However, we also enable/disable egress filtering because the switch 2053 * still consider the egress interface VLAN membership to forward the 2054 * traffic. We enable/disable that check disabling/enabling transparent 2055 * VLAN between the ingress port and all other available ports. 2056 */ 2057 dsa_switch_for_each_available_port(dp, ds) { 2058 /* port isolation will still keep traffic inside the bridge */ 2059 ret = rtl8365mb_port_set_transparent(priv, port, dp->index, 2060 !vlan_filtering); 2061 if (ret) 2062 goto undo_transparent; 2063 2064 configured_ports |= BIT(dp->index); 2065 } 2066 2067 if (accepted_frame != prev_accepted_frame) { 2068 ret = rtl8365mb_vlan_port_set_framefilter(priv, port, 2069 accepted_frame); 2070 if (ret) { 2071 NL_SET_ERR_MSG_MOD(extack, 2072 "Failed to set port framefilter"); 2073 goto undo_transparent; 2074 } 2075 } 2076 2077 /* When VLAN filtering is disabled, preserve frames exactly as received. 2078 * Otherwise, the VLAN egress pipeline may still alter tag state 2079 * according to VLAN membership and untag configuration. 2080 */ 2081 if (vlan_filtering) 2082 mode = RTL8365MB_VLAN_EGRESS_MODE_ORIGINAL; 2083 else 2084 mode = RTL8365MB_VLAN_EGRESS_MODE_REAL_KEEP; 2085 2086 ret = rtl8365mb_port_set_vlan_egress_mode(priv, port, mode); 2087 if (ret) 2088 goto undo_set_framefilter; 2089 2090 return ret; 2091 2092 undo_set_framefilter: 2093 if (prev_accepted_frame != accepted_frame) 2094 rtl8365mb_vlan_port_set_framefilter(priv, port, 2095 prev_accepted_frame); 2096 undo_transparent: 2097 /* The DSA core guarantees this callback is only invoked on an actual 2098 * state transition, ensuring the previous hardware state was the 2099 * opposite (!vlan_filtering). It is also called during setup but, in 2100 * that case, any failure here aborts the entire switch initialization. 2101 * 2102 * VLAN_INGRESS and VLAN_EGRESS_TRANSPARENT states are directly derived 2103 * from vlan_filtering. That way, we can simply undo it without 2104 * checking the current HW state as we do with VLAN_EGRESS_MODE. 2105 */ 2106 dsa_switch_for_each_port(dp, ds) { 2107 if (configured_ports & BIT(dp->index)) 2108 rtl8365mb_port_set_transparent(priv, port, dp->index, 2109 vlan_filtering); 2110 } 2111 2112 rtl8365mb_port_set_ingress_filtering(priv, port, !vlan_filtering); 2113 2114 return ret; 2115 } 2116 2117 static int rtl8365mb_port_vlan_add(struct dsa_switch *ds, int port, 2118 const struct switchdev_obj_port_vlan *vlan, 2119 struct netlink_ext_ack *extack) 2120 { 2121 bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); 2122 bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID); 2123 u16 pvid_vid; 2124 struct realtek_priv *priv = ds->priv; 2125 int ret; 2126 2127 dev_dbg(priv->dev, "add VLAN %d on port %d, %s, %s\n", 2128 vlan->vid, port, untagged ? "untagged" : "tagged", 2129 pvid ? "PVID" : "no PVID"); 2130 2131 /* VID == 0 is reserved in this driver */ 2132 if (vlan->vid == 0) { 2133 NL_SET_ERR_MSG_MOD(extack, 2134 "VLAN 0 is reserved by this driver"); 2135 return -EOPNOTSUPP; 2136 } 2137 2138 mutex_lock(&priv->vlan_lock); 2139 2140 ret = rtl8365mb_vlan_port_get_pvid(priv, port, &pvid_vid); 2141 if (ret) 2142 goto out_unlock; 2143 2144 /* Set PVID if needed */ 2145 if (pvid) { 2146 ret = rtl8365mb_vlan_pvid_port_set(ds, port, vlan->vid, 2147 extack); 2148 if (ret) 2149 goto out_unlock; 2150 } else { 2151 /* or try to unset it if not */ 2152 ret = rtl8365mb_vlan_pvid_port_clear(ds, port, vlan->vid); 2153 if (ret) 2154 goto out_unlock; 2155 } 2156 2157 /* add port to vlan4k. It knows nothing about PVID */ 2158 ret = rtl8365mb_vlan_4k_port_add(ds, port, vlan, extack); 2159 if (ret) 2160 goto undo_set_pvid; 2161 2162 ret = 0; 2163 goto out_unlock; 2164 2165 undo_set_pvid: 2166 /* undo the pvid definition */ 2167 if (pvid != (pvid_vid == vlan->vid)) { 2168 if (pvid_vid) 2169 (void)rtl8365mb_vlan_pvid_port_set(ds, port, pvid_vid, 2170 NULL); 2171 else 2172 (void)rtl8365mb_vlan_pvid_port_clear(ds, port, 2173 vlan->vid); 2174 } 2175 out_unlock: 2176 mutex_unlock(&priv->vlan_lock); 2177 return ret; 2178 } 2179 2180 static int rtl8365mb_port_vlan_del(struct dsa_switch *ds, int port, 2181 const struct switchdev_obj_port_vlan *vlan) 2182 { 2183 bool untagged = !!(vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED); 2184 bool pvid = !!(vlan->flags & BRIDGE_VLAN_INFO_PVID); 2185 struct realtek_priv *priv = ds->priv; 2186 int ret; 2187 2188 dev_dbg(priv->dev, "del VLAN %d on port %d, %s, %s\n", 2189 vlan->vid, port, untagged ? "untagged" : "tagged", 2190 pvid ? "PVID" : "no PVID"); 2191 2192 /* VID == 0 is reserved in this driver */ 2193 if (vlan->vid == 0) 2194 return -EOPNOTSUPP; 2195 2196 mutex_lock(&priv->vlan_lock); 2197 ret = rtl8365mb_vlan_pvid_port_clear(ds, port, vlan->vid); 2198 if (ret) 2199 goto out_unlock; 2200 2201 ret = rtl8365mb_vlan_4k_port_del(ds, port, vlan); 2202 /* There is little incentive to try to undo the removal of PVID (if it 2203 * was really in use) as an error here might indicate the ASIC stopped 2204 * to answer. 2205 */ 2206 2207 out_unlock: 2208 mutex_unlock(&priv->vlan_lock); 2209 return ret; 2210 } 2211 2212 /* VLAN support is always enabled in the switch. 2213 * 2214 * Standalone forwarding relies on transparent VLAN mode combined with per-port 2215 * isolation masks restricting egress to CPU ports only. 2216 * 2217 */ 2218 static int rtl8365mb_vlan_setup(struct dsa_switch *ds) 2219 { 2220 struct realtek_priv *priv = ds->priv; 2221 struct dsa_port *dp; 2222 int ret; 2223 2224 dsa_switch_for_each_available_port(dp, ds) { 2225 /* Disable vlan-filtering for all ports */ 2226 ret = rtl8365mb_port_vlan_filtering(ds, dp->index, false, NULL); 2227 if (ret) { 2228 dev_err(priv->dev, 2229 "Failed to disable vlan filtering on port %d\n", 2230 dp->index); 2231 return ret; 2232 } 2233 } 2234 2235 /* VLAN is always enabled. */ 2236 ret = regmap_update_bits(priv->map, RTL8365MB_VLAN_CTRL_REG, 2237 RTL8365MB_VLAN_CTRL_EN_MASK, 2238 FIELD_PREP(RTL8365MB_VLAN_CTRL_EN_MASK, 1)); 2239 return ret; 2240 } 2241 2242 static int rtl8365mb_port_set_learning(struct realtek_priv *priv, int port, 2243 bool enable) 2244 { 2245 /* Enable/disable learning by limiting the number of L2 addresses the 2246 * port can learn. Realtek documentation states that a limit of zero 2247 * disables learning. When enabling learning, set it to the chip's 2248 * maximum. 2249 */ 2250 return regmap_write(priv->map, RTL8365MB_LUT_PORT_LEARN_LIMIT_REG(port), 2251 enable ? RTL8365MB_LEARN_LIMIT_MAX : 0); 2252 } 2253 2254 static int rtl8365mb_port_set_ucast_flood(struct realtek_priv *priv, int port, 2255 bool enable) 2256 { 2257 /* Frames with unknown unicast DA will be flooded to a programmable 2258 * port mask that by default includes all ports. Add or remove 2259 * the specified port from this port mask accordingly. 2260 */ 2261 return regmap_update_bits(priv->map, 2262 RTL8365MB_UNKNOWN_UNICAST_FLOODING_PMASK_REG, 2263 BIT(port), enable ? BIT(port) : 0); 2264 } 2265 2266 static int rtl8365mb_port_set_mcast_flood(struct realtek_priv *priv, int port, 2267 bool enable) 2268 { 2269 return regmap_update_bits(priv->map, 2270 RTL8365MB_UNKNOWN_MULTICAST_FLOODING_PMASK_REG, 2271 BIT(port), enable ? BIT(port) : 0); 2272 } 2273 2274 static int rtl8365mb_port_set_bcast_flood(struct realtek_priv *priv, int port, 2275 bool enable) 2276 { 2277 return regmap_update_bits(priv->map, 2278 RTL8365MB_UNKNOWN_BROADCAST_FLOODING_PMASK_REG, 2279 BIT(port), enable ? BIT(port) : 0); 2280 } 2281 2282 static int rtl8365mb_port_pre_bridge_flags(struct dsa_switch *ds, int port, 2283 struct switchdev_brport_flags flags, 2284 struct netlink_ext_ack *extack) 2285 { 2286 struct realtek_priv *priv = ds->priv; 2287 2288 dev_dbg(priv->dev, "pre_bridge_flags port:%d flags:%lx supported:%lx\n", 2289 port, flags.mask, RTL8365MB_SUPPORTED_BRIDGE_FLAGS); 2290 2291 if (flags.mask & ~RTL8365MB_SUPPORTED_BRIDGE_FLAGS) 2292 return -EINVAL; 2293 2294 return 0; 2295 } 2296 2297 static int rtl8365mb_port_set_efid(struct realtek_priv *priv, int port, 2298 u32 efid) 2299 { 2300 return regmap_update_bits(priv->map, RTL8365MB_PORT_EFID_REG(port), 2301 RTL8365MB_PORT_EFID_MASK(port), 2302 efid << RTL8365MB_PORT_EFID_OFFSET(port)); 2303 } 2304 2305 /* Port isolation manipulation functions. 2306 * 2307 * The port isolation register controls the forwarding mask of a given 2308 * port. The switch will not forward packets ingressed on a given port 2309 * to ports which are not enabled in its forwarding mask. 2310 * 2311 * The port forwarding mask has the highest priority in forwarding 2312 * decisions. The only exception to this rule is when the switch 2313 * receives a packet on its CPU port with ALLOW=0. In that case the TX 2314 * field of the CPU tag will override the forwarding port mask. 2315 */ 2316 static int rtl8365mb_port_set_isolation(struct realtek_priv *priv, int port, 2317 u32 mask) 2318 { 2319 return regmap_write(priv->map, RTL8365MB_PORT_ISOLATION_REG(port), 2320 mask); 2321 } 2322 2323 static int rtl8365mb_port_add_isolation(struct realtek_priv *priv, int port, 2324 u32 mask) 2325 { 2326 return regmap_update_bits(priv->map, RTL8365MB_PORT_ISOLATION_REG(port), 2327 mask, mask); 2328 } 2329 2330 static int rtl8365mb_port_remove_isolation(struct realtek_priv *priv, int port, 2331 u32 mask) 2332 { 2333 return regmap_update_bits(priv->map, RTL8365MB_PORT_ISOLATION_REG(port), 2334 mask, 0); 2335 } 2336 2337 static int rtl8365mb_mib_counter_read(struct realtek_priv *priv, int port, 2338 u32 offset, u32 length, u64 *mibvalue) 2339 { 2340 u64 tmpvalue = 0; 2341 u32 val; 2342 int ret; 2343 int i; 2344 2345 /* The MIB address is an SRAM address. We request a particular address 2346 * and then poll the control register before reading the value from some 2347 * counter registers. 2348 */ 2349 ret = regmap_write(priv->map, RTL8365MB_MIB_ADDRESS_REG, 2350 RTL8365MB_MIB_ADDRESS(port, offset)); 2351 if (ret) 2352 return ret; 2353 2354 /* Poll for completion */ 2355 ret = regmap_read_poll_timeout(priv->map, RTL8365MB_MIB_CTRL0_REG, val, 2356 !(val & RTL8365MB_MIB_CTRL0_BUSY_MASK), 2357 10, 100); 2358 if (ret) 2359 return ret; 2360 2361 /* Presumably this indicates a MIB counter read failure */ 2362 if (val & RTL8365MB_MIB_CTRL0_RESET_MASK) 2363 return -EIO; 2364 2365 /* There are four MIB counter registers each holding a 16 bit word of a 2366 * MIB counter. Depending on the offset, we should read from the upper 2367 * two or lower two registers. In case the MIB counter is 4 words, we 2368 * read from all four registers. 2369 */ 2370 if (length == 4) 2371 offset = 3; 2372 else 2373 offset = (offset + 1) % 4; 2374 2375 /* Read the MIB counter 16 bits at a time */ 2376 for (i = 0; i < length; i++) { 2377 ret = regmap_read(priv->map, 2378 RTL8365MB_MIB_COUNTER_REG(offset - i), &val); 2379 if (ret) 2380 return ret; 2381 2382 tmpvalue = ((tmpvalue) << 16) | (val & 0xFFFF); 2383 } 2384 2385 /* Only commit the result if no error occurred */ 2386 *mibvalue = tmpvalue; 2387 2388 return 0; 2389 } 2390 2391 static void rtl8365mb_get_ethtool_stats(struct dsa_switch *ds, int port, u64 *data) 2392 { 2393 struct realtek_priv *priv = ds->priv; 2394 struct rtl8365mb *mb; 2395 int ret; 2396 int i; 2397 2398 mb = priv->chip_data; 2399 2400 mutex_lock(&mb->mib_lock); 2401 for (i = 0; i < RTL8365MB_MIB_END; i++) { 2402 struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i]; 2403 2404 ret = rtl8365mb_mib_counter_read(priv, port, mib->offset, 2405 mib->length, &data[i]); 2406 if (ret) { 2407 dev_err(priv->dev, 2408 "failed to read port %d counters: %pe\n", port, 2409 ERR_PTR(ret)); 2410 break; 2411 } 2412 } 2413 mutex_unlock(&mb->mib_lock); 2414 } 2415 2416 static void rtl8365mb_get_strings(struct dsa_switch *ds, int port, u32 stringset, u8 *data) 2417 { 2418 int i; 2419 2420 if (stringset != ETH_SS_STATS) 2421 return; 2422 2423 for (i = 0; i < RTL8365MB_MIB_END; i++) { 2424 struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i]; 2425 ethtool_puts(&data, mib->name); 2426 } 2427 } 2428 2429 static int rtl8365mb_get_sset_count(struct dsa_switch *ds, int port, int sset) 2430 { 2431 if (sset != ETH_SS_STATS) 2432 return -EOPNOTSUPP; 2433 2434 return RTL8365MB_MIB_END; 2435 } 2436 2437 static void rtl8365mb_get_phy_stats(struct dsa_switch *ds, int port, 2438 struct ethtool_eth_phy_stats *phy_stats) 2439 { 2440 struct realtek_priv *priv = ds->priv; 2441 struct rtl8365mb_mib_counter *mib; 2442 struct rtl8365mb *mb; 2443 2444 mb = priv->chip_data; 2445 mib = &rtl8365mb_mib_counters[RTL8365MB_MIB_dot3StatsSymbolErrors]; 2446 2447 mutex_lock(&mb->mib_lock); 2448 rtl8365mb_mib_counter_read(priv, port, mib->offset, mib->length, 2449 &phy_stats->SymbolErrorDuringCarrier); 2450 mutex_unlock(&mb->mib_lock); 2451 } 2452 2453 static void rtl8365mb_get_mac_stats(struct dsa_switch *ds, int port, 2454 struct ethtool_eth_mac_stats *mac_stats) 2455 { 2456 u64 cnt[RTL8365MB_MIB_END] = { 2457 [RTL8365MB_MIB_ifOutOctets] = 1, 2458 [RTL8365MB_MIB_ifOutUcastPkts] = 1, 2459 [RTL8365MB_MIB_ifOutMulticastPkts] = 1, 2460 [RTL8365MB_MIB_ifOutBroadcastPkts] = 1, 2461 [RTL8365MB_MIB_dot3OutPauseFrames] = 1, 2462 [RTL8365MB_MIB_ifOutDiscards] = 1, 2463 [RTL8365MB_MIB_ifInOctets] = 1, 2464 [RTL8365MB_MIB_ifInUcastPkts] = 1, 2465 [RTL8365MB_MIB_ifInMulticastPkts] = 1, 2466 [RTL8365MB_MIB_ifInBroadcastPkts] = 1, 2467 [RTL8365MB_MIB_dot3InPauseFrames] = 1, 2468 [RTL8365MB_MIB_dot3StatsSingleCollisionFrames] = 1, 2469 [RTL8365MB_MIB_dot3StatsMultipleCollisionFrames] = 1, 2470 [RTL8365MB_MIB_dot3StatsFCSErrors] = 1, 2471 [RTL8365MB_MIB_dot3StatsDeferredTransmissions] = 1, 2472 [RTL8365MB_MIB_dot3StatsLateCollisions] = 1, 2473 [RTL8365MB_MIB_dot3StatsExcessiveCollisions] = 1, 2474 2475 }; 2476 struct realtek_priv *priv = ds->priv; 2477 struct rtl8365mb *mb; 2478 int ret; 2479 int i; 2480 2481 mb = priv->chip_data; 2482 2483 mutex_lock(&mb->mib_lock); 2484 for (i = 0; i < RTL8365MB_MIB_END; i++) { 2485 struct rtl8365mb_mib_counter *mib = &rtl8365mb_mib_counters[i]; 2486 2487 /* Only fetch required MIB counters (marked = 1 above) */ 2488 if (!cnt[i]) 2489 continue; 2490 2491 ret = rtl8365mb_mib_counter_read(priv, port, mib->offset, 2492 mib->length, &cnt[i]); 2493 if (ret) 2494 break; 2495 } 2496 mutex_unlock(&mb->mib_lock); 2497 2498 /* The RTL8365MB-VC exposes MIB objects, which we have to translate into 2499 * IEEE 802.3 Managed Objects. This is not always completely faithful, 2500 * but we try out best. See RFC 3635 for a detailed treatment of the 2501 * subject. 2502 */ 2503 2504 mac_stats->FramesTransmittedOK = cnt[RTL8365MB_MIB_ifOutUcastPkts] + 2505 cnt[RTL8365MB_MIB_ifOutMulticastPkts] + 2506 cnt[RTL8365MB_MIB_ifOutBroadcastPkts] + 2507 cnt[RTL8365MB_MIB_dot3OutPauseFrames] - 2508 cnt[RTL8365MB_MIB_ifOutDiscards]; 2509 mac_stats->SingleCollisionFrames = 2510 cnt[RTL8365MB_MIB_dot3StatsSingleCollisionFrames]; 2511 mac_stats->MultipleCollisionFrames = 2512 cnt[RTL8365MB_MIB_dot3StatsMultipleCollisionFrames]; 2513 mac_stats->FramesReceivedOK = cnt[RTL8365MB_MIB_ifInUcastPkts] + 2514 cnt[RTL8365MB_MIB_ifInMulticastPkts] + 2515 cnt[RTL8365MB_MIB_ifInBroadcastPkts] + 2516 cnt[RTL8365MB_MIB_dot3InPauseFrames]; 2517 mac_stats->FrameCheckSequenceErrors = 2518 cnt[RTL8365MB_MIB_dot3StatsFCSErrors]; 2519 mac_stats->OctetsTransmittedOK = cnt[RTL8365MB_MIB_ifOutOctets] - 2520 18 * mac_stats->FramesTransmittedOK; 2521 mac_stats->FramesWithDeferredXmissions = 2522 cnt[RTL8365MB_MIB_dot3StatsDeferredTransmissions]; 2523 mac_stats->LateCollisions = cnt[RTL8365MB_MIB_dot3StatsLateCollisions]; 2524 mac_stats->FramesAbortedDueToXSColls = 2525 cnt[RTL8365MB_MIB_dot3StatsExcessiveCollisions]; 2526 mac_stats->OctetsReceivedOK = cnt[RTL8365MB_MIB_ifInOctets] - 2527 18 * mac_stats->FramesReceivedOK; 2528 mac_stats->MulticastFramesXmittedOK = 2529 cnt[RTL8365MB_MIB_ifOutMulticastPkts]; 2530 mac_stats->BroadcastFramesXmittedOK = 2531 cnt[RTL8365MB_MIB_ifOutBroadcastPkts]; 2532 mac_stats->MulticastFramesReceivedOK = 2533 cnt[RTL8365MB_MIB_ifInMulticastPkts]; 2534 mac_stats->BroadcastFramesReceivedOK = 2535 cnt[RTL8365MB_MIB_ifInBroadcastPkts]; 2536 } 2537 2538 static void rtl8365mb_get_ctrl_stats(struct dsa_switch *ds, int port, 2539 struct ethtool_eth_ctrl_stats *ctrl_stats) 2540 { 2541 struct realtek_priv *priv = ds->priv; 2542 struct rtl8365mb_mib_counter *mib; 2543 struct rtl8365mb *mb; 2544 2545 mb = priv->chip_data; 2546 mib = &rtl8365mb_mib_counters[RTL8365MB_MIB_dot3ControlInUnknownOpcodes]; 2547 2548 mutex_lock(&mb->mib_lock); 2549 rtl8365mb_mib_counter_read(priv, port, mib->offset, mib->length, 2550 &ctrl_stats->UnsupportedOpcodesReceived); 2551 mutex_unlock(&mb->mib_lock); 2552 } 2553 2554 static void rtl8365mb_stats_update(struct realtek_priv *priv, int port) 2555 { 2556 u64 cnt[RTL8365MB_MIB_END] = { 2557 [RTL8365MB_MIB_ifOutOctets] = 1, 2558 [RTL8365MB_MIB_ifOutUcastPkts] = 1, 2559 [RTL8365MB_MIB_ifOutMulticastPkts] = 1, 2560 [RTL8365MB_MIB_ifOutBroadcastPkts] = 1, 2561 [RTL8365MB_MIB_ifOutDiscards] = 1, 2562 [RTL8365MB_MIB_ifInOctets] = 1, 2563 [RTL8365MB_MIB_ifInUcastPkts] = 1, 2564 [RTL8365MB_MIB_ifInMulticastPkts] = 1, 2565 [RTL8365MB_MIB_ifInBroadcastPkts] = 1, 2566 [RTL8365MB_MIB_etherStatsDropEvents] = 1, 2567 [RTL8365MB_MIB_etherStatsCollisions] = 1, 2568 [RTL8365MB_MIB_etherStatsFragments] = 1, 2569 [RTL8365MB_MIB_etherStatsJabbers] = 1, 2570 [RTL8365MB_MIB_dot3StatsFCSErrors] = 1, 2571 [RTL8365MB_MIB_dot3StatsLateCollisions] = 1, 2572 }; 2573 struct rtl8365mb *mb = priv->chip_data; 2574 struct rtnl_link_stats64 *stats; 2575 int ret; 2576 int i; 2577 2578 stats = &mb->ports[port].stats; 2579 2580 mutex_lock(&mb->mib_lock); 2581 for (i = 0; i < RTL8365MB_MIB_END; i++) { 2582 struct rtl8365mb_mib_counter *c = &rtl8365mb_mib_counters[i]; 2583 2584 /* Only fetch required MIB counters (marked = 1 above) */ 2585 if (!cnt[i]) 2586 continue; 2587 2588 ret = rtl8365mb_mib_counter_read(priv, port, c->offset, 2589 c->length, &cnt[i]); 2590 if (ret) 2591 break; 2592 } 2593 mutex_unlock(&mb->mib_lock); 2594 2595 /* Don't update statistics if there was an error reading the counters */ 2596 if (ret) 2597 return; 2598 2599 spin_lock(&mb->ports[port].stats_lock); 2600 2601 stats->rx_packets = cnt[RTL8365MB_MIB_ifInUcastPkts] + 2602 cnt[RTL8365MB_MIB_ifInMulticastPkts] + 2603 cnt[RTL8365MB_MIB_ifInBroadcastPkts]; 2604 2605 stats->tx_packets = cnt[RTL8365MB_MIB_ifOutUcastPkts] + 2606 cnt[RTL8365MB_MIB_ifOutMulticastPkts] + 2607 cnt[RTL8365MB_MIB_ifOutBroadcastPkts]; 2608 2609 /* if{In,Out}Octets includes FCS - remove it */ 2610 stats->rx_bytes = cnt[RTL8365MB_MIB_ifInOctets] - 4 * stats->rx_packets; 2611 stats->tx_bytes = 2612 cnt[RTL8365MB_MIB_ifOutOctets] - 4 * stats->tx_packets; 2613 2614 stats->rx_dropped = cnt[RTL8365MB_MIB_etherStatsDropEvents]; 2615 stats->tx_dropped = cnt[RTL8365MB_MIB_ifOutDiscards]; 2616 2617 stats->multicast = cnt[RTL8365MB_MIB_ifInMulticastPkts]; 2618 stats->collisions = cnt[RTL8365MB_MIB_etherStatsCollisions]; 2619 2620 stats->rx_length_errors = cnt[RTL8365MB_MIB_etherStatsFragments] + 2621 cnt[RTL8365MB_MIB_etherStatsJabbers]; 2622 stats->rx_crc_errors = cnt[RTL8365MB_MIB_dot3StatsFCSErrors]; 2623 stats->rx_errors = stats->rx_length_errors + stats->rx_crc_errors; 2624 2625 stats->tx_aborted_errors = cnt[RTL8365MB_MIB_ifOutDiscards]; 2626 stats->tx_window_errors = cnt[RTL8365MB_MIB_dot3StatsLateCollisions]; 2627 stats->tx_errors = stats->tx_aborted_errors + stats->tx_window_errors; 2628 2629 spin_unlock(&mb->ports[port].stats_lock); 2630 } 2631 2632 static void rtl8365mb_stats_poll(struct work_struct *work) 2633 { 2634 struct rtl8365mb_port *p = container_of(to_delayed_work(work), 2635 struct rtl8365mb_port, 2636 mib_work); 2637 struct realtek_priv *priv = p->priv; 2638 2639 rtl8365mb_stats_update(priv, p->index); 2640 2641 schedule_delayed_work(&p->mib_work, RTL8365MB_STATS_INTERVAL_JIFFIES); 2642 } 2643 2644 static void rtl8365mb_get_stats64(struct dsa_switch *ds, int port, 2645 struct rtnl_link_stats64 *s) 2646 { 2647 struct realtek_priv *priv = ds->priv; 2648 struct rtl8365mb_port *p; 2649 struct rtl8365mb *mb; 2650 2651 mb = priv->chip_data; 2652 p = &mb->ports[port]; 2653 2654 spin_lock(&p->stats_lock); 2655 memcpy(s, &p->stats, sizeof(*s)); 2656 spin_unlock(&p->stats_lock); 2657 } 2658 2659 static int rtl8365mb_stats_setup(struct realtek_priv *priv) 2660 { 2661 struct rtl8365mb *mb = priv->chip_data; 2662 struct dsa_switch *ds = &priv->ds; 2663 struct dsa_port *dp; 2664 int ret; 2665 2666 /* Per-chip global mutex to protect MIB counter access, since doing 2667 * so requires accessing a series of registers in a particular order. 2668 */ 2669 ret = devm_mutex_init(priv->dev, &mb->mib_lock); 2670 if (ret) 2671 return ret; 2672 2673 dsa_switch_for_each_available_port(dp, ds) { 2674 struct rtl8365mb_port *p = &mb->ports[dp->index]; 2675 2676 /* Per-port spinlock to protect the stats64 data */ 2677 spin_lock_init(&p->stats_lock); 2678 2679 /* This work polls the MIB counters and keeps the stats64 data 2680 * up-to-date. 2681 */ 2682 INIT_DELAYED_WORK(&p->mib_work, rtl8365mb_stats_poll); 2683 } 2684 2685 return 0; 2686 } 2687 2688 static void rtl8365mb_stats_teardown(struct realtek_priv *priv) 2689 { 2690 struct rtl8365mb *mb = priv->chip_data; 2691 struct dsa_switch *ds = &priv->ds; 2692 struct dsa_port *dp; 2693 2694 dsa_switch_for_each_available_port(dp, ds) { 2695 struct rtl8365mb_port *p = &mb->ports[dp->index]; 2696 2697 cancel_delayed_work_sync(&p->mib_work); 2698 } 2699 } 2700 2701 static int rtl8365mb_get_and_clear_status_reg(struct realtek_priv *priv, u32 reg, 2702 u32 *val) 2703 { 2704 int ret; 2705 2706 ret = regmap_read(priv->map, reg, val); 2707 if (ret) 2708 return ret; 2709 2710 return regmap_write(priv->map, reg, *val); 2711 } 2712 2713 static irqreturn_t rtl8365mb_irq(int irq, void *data) 2714 { 2715 struct realtek_priv *priv = data; 2716 unsigned long line_changes = 0; 2717 u32 stat; 2718 int line; 2719 int ret; 2720 2721 ret = rtl8365mb_get_and_clear_status_reg(priv, RTL8365MB_INTR_STATUS_REG, 2722 &stat); 2723 if (ret) 2724 goto out_error; 2725 2726 if (stat & RTL8365MB_INTR_LINK_CHANGE_MASK) { 2727 u32 linkdown_ind; 2728 u32 linkup_ind; 2729 u32 val; 2730 2731 ret = rtl8365mb_get_and_clear_status_reg( 2732 priv, RTL8365MB_PORT_LINKUP_IND_REG, &val); 2733 if (ret) 2734 goto out_error; 2735 2736 linkup_ind = FIELD_GET(RTL8365MB_PORT_LINKUP_IND_MASK, val); 2737 2738 ret = rtl8365mb_get_and_clear_status_reg( 2739 priv, RTL8365MB_PORT_LINKDOWN_IND_REG, &val); 2740 if (ret) 2741 goto out_error; 2742 2743 linkdown_ind = FIELD_GET(RTL8365MB_PORT_LINKDOWN_IND_MASK, val); 2744 2745 line_changes = linkup_ind | linkdown_ind; 2746 } 2747 2748 if (!line_changes) 2749 goto out_none; 2750 2751 for_each_set_bit(line, &line_changes, priv->num_ports) { 2752 int child_irq = irq_find_mapping(priv->irqdomain, line); 2753 2754 if (!child_irq) 2755 continue; 2756 2757 handle_nested_irq(child_irq); 2758 } 2759 2760 return IRQ_HANDLED; 2761 2762 out_error: 2763 dev_err(priv->dev, "failed to read interrupt status: %pe\n", 2764 ERR_PTR(ret)); 2765 2766 out_none: 2767 return IRQ_NONE; 2768 } 2769 2770 static struct irq_chip rtl8365mb_irq_chip = { 2771 .name = "rtl8365mb", 2772 /* The hardware doesn't support masking IRQs on a per-port basis */ 2773 }; 2774 2775 static int rtl8365mb_irq_map(struct irq_domain *domain, unsigned int irq, 2776 irq_hw_number_t hwirq) 2777 { 2778 struct realtek_priv *priv = domain->host_data; 2779 struct rtl8365mb *mb = priv->chip_data; 2780 2781 irq_set_chip_data(irq, priv); 2782 irq_set_chip_and_handler(irq, &rtl8365mb_irq_chip, handle_simple_irq); 2783 irq_set_nested_thread(irq, 1); 2784 irq_set_noprobe(irq); 2785 irq_set_parent(irq, mb->irq); 2786 2787 return 0; 2788 } 2789 2790 static void rtl8365mb_irq_unmap(struct irq_domain *d, unsigned int irq) 2791 { 2792 irq_set_nested_thread(irq, 0); 2793 irq_set_chip_and_handler(irq, NULL, NULL); 2794 irq_set_chip_data(irq, NULL); 2795 } 2796 2797 static const struct irq_domain_ops rtl8365mb_irqdomain_ops = { 2798 .map = rtl8365mb_irq_map, 2799 .unmap = rtl8365mb_irq_unmap, 2800 .xlate = irq_domain_xlate_onecell, 2801 }; 2802 2803 static int rtl8365mb_set_irq_enable(struct realtek_priv *priv, bool enable) 2804 { 2805 return regmap_update_bits(priv->map, RTL8365MB_INTR_CTRL_REG, 2806 RTL8365MB_INTR_LINK_CHANGE_MASK, 2807 FIELD_PREP(RTL8365MB_INTR_LINK_CHANGE_MASK, 2808 enable ? 1 : 0)); 2809 } 2810 2811 static int rtl8365mb_irq_enable(struct realtek_priv *priv) 2812 { 2813 return rtl8365mb_set_irq_enable(priv, true); 2814 } 2815 2816 static int rtl8365mb_irq_disable(struct realtek_priv *priv) 2817 { 2818 return rtl8365mb_set_irq_enable(priv, false); 2819 } 2820 2821 static int rtl8365mb_irq_setup(struct realtek_priv *priv) 2822 { 2823 struct rtl8365mb *mb = priv->chip_data; 2824 struct dsa_switch *ds = &priv->ds; 2825 struct device_node *intc; 2826 struct dsa_port *dp; 2827 u32 irq_trig; 2828 int virq; 2829 int irq; 2830 u32 val; 2831 int ret; 2832 2833 intc = of_get_child_by_name(priv->dev->of_node, "interrupt-controller"); 2834 if (!intc) { 2835 dev_err(priv->dev, "missing child interrupt-controller node\n"); 2836 return -EINVAL; 2837 } 2838 2839 /* rtl8365mb IRQs cascade off this one */ 2840 irq = of_irq_get(intc, 0); 2841 if (irq <= 0) { 2842 if (!irq) { 2843 dev_err(priv->dev, "failed to map IRQ\n"); 2844 ret = -EINVAL; 2845 } else { 2846 ret = dev_err_probe(priv->dev, irq, 2847 "failed to get parent irq\n"); 2848 } 2849 goto out_put_node; 2850 } 2851 2852 /* Store the irq so that we know to map and free it during teardown */ 2853 mb->irq = irq; 2854 2855 priv->irqdomain = irq_domain_create_linear(of_fwnode_handle(intc), priv->num_ports, 2856 &rtl8365mb_irqdomain_ops, priv); 2857 if (!priv->irqdomain) { 2858 dev_err(priv->dev, "failed to add irq domain\n"); 2859 ret = -ENOMEM; 2860 goto out_put_node; 2861 } 2862 2863 dsa_switch_for_each_available_port(dp, ds) { 2864 virq = irq_create_mapping(priv->irqdomain, dp->index); 2865 if (!virq) { 2866 dev_err(priv->dev, 2867 "failed to create irq domain mapping\n"); 2868 ret = -EINVAL; 2869 goto out_remove_irqdomain; 2870 } 2871 2872 irq_set_parent(virq, irq); 2873 } 2874 2875 /* Configure chip interrupt signal polarity */ 2876 irq_trig = irq_get_trigger_type(irq); 2877 switch (irq_trig) { 2878 case IRQF_TRIGGER_RISING: 2879 case IRQF_TRIGGER_HIGH: 2880 val = RTL8365MB_INTR_POLARITY_HIGH; 2881 break; 2882 case IRQF_TRIGGER_FALLING: 2883 case IRQF_TRIGGER_LOW: 2884 val = RTL8365MB_INTR_POLARITY_LOW; 2885 break; 2886 default: 2887 dev_err(priv->dev, "unsupported irq trigger type %u\n", 2888 irq_trig); 2889 ret = -EINVAL; 2890 goto out_remove_irqdomain; 2891 } 2892 2893 ret = regmap_update_bits(priv->map, RTL8365MB_INTR_POLARITY_REG, 2894 RTL8365MB_INTR_POLARITY_MASK, 2895 FIELD_PREP(RTL8365MB_INTR_POLARITY_MASK, val)); 2896 if (ret) 2897 goto out_remove_irqdomain; 2898 2899 /* Disable the interrupt in case the chip has it enabled on reset */ 2900 ret = rtl8365mb_irq_disable(priv); 2901 if (ret) 2902 goto out_remove_irqdomain; 2903 2904 /* Clear the interrupt status register */ 2905 ret = regmap_write(priv->map, RTL8365MB_INTR_STATUS_REG, 2906 RTL8365MB_INTR_ALL_MASK); 2907 if (ret) 2908 goto out_remove_irqdomain; 2909 2910 ret = request_threaded_irq(irq, NULL, rtl8365mb_irq, IRQF_ONESHOT, 2911 "rtl8365mb", priv); 2912 if (ret) { 2913 dev_err(priv->dev, "failed to request irq: %pe\n", 2914 ERR_PTR(ret)); 2915 goto out_remove_irqdomain; 2916 } 2917 2918 ret = rtl8365mb_irq_enable(priv); 2919 if (ret) 2920 goto out_free_irq; 2921 2922 of_node_put(intc); 2923 2924 return 0; 2925 2926 out_free_irq: 2927 free_irq(mb->irq, priv); 2928 2929 out_remove_irqdomain: 2930 dsa_switch_for_each_port(dp, ds) { 2931 virq = irq_find_mapping(priv->irqdomain, dp->index); 2932 2933 if (virq) 2934 irq_dispose_mapping(virq); 2935 } 2936 2937 irq_domain_remove(priv->irqdomain); 2938 priv->irqdomain = NULL; 2939 2940 out_put_node: 2941 mb->irq = 0; 2942 of_node_put(intc); 2943 2944 return ret; 2945 } 2946 2947 static void rtl8365mb_irq_teardown(struct realtek_priv *priv) 2948 { 2949 struct rtl8365mb *mb = priv->chip_data; 2950 struct dsa_switch *ds = &priv->ds; 2951 struct dsa_port *dp; 2952 int virq; 2953 2954 if (mb->irq) { 2955 free_irq(mb->irq, priv); 2956 mb->irq = 0; 2957 } 2958 2959 if (priv->irqdomain) { 2960 /* Unused ports with a linked PHY still have an active IRQ 2961 * mapping that must be disposed of during teardown. Loop 2962 * through all ports. 2963 */ 2964 dsa_switch_for_each_port(dp, ds) { 2965 virq = irq_find_mapping(priv->irqdomain, dp->index); 2966 2967 if (virq) 2968 irq_dispose_mapping(virq); 2969 } 2970 2971 irq_domain_remove(priv->irqdomain); 2972 priv->irqdomain = NULL; 2973 } 2974 } 2975 2976 static int rtl8365mb_cpu_config(struct realtek_priv *priv) 2977 { 2978 struct rtl8365mb *mb = priv->chip_data; 2979 struct rtl8365mb_cpu *cpu = &mb->cpu; 2980 u32 val; 2981 int ret; 2982 2983 ret = regmap_update_bits(priv->map, RTL8365MB_CPU_PORT_MASK_REG, 2984 RTL8365MB_CPU_PORT_MASK_MASK, 2985 FIELD_PREP(RTL8365MB_CPU_PORT_MASK_MASK, 2986 cpu->mask)); 2987 if (ret) 2988 return ret; 2989 2990 val = FIELD_PREP(RTL8365MB_CPU_CTRL_EN_MASK, cpu->enable ? 1 : 0) | 2991 FIELD_PREP(RTL8365MB_CPU_CTRL_INSERTMODE_MASK, cpu->insert) | 2992 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_POSITION_MASK, cpu->position) | 2993 FIELD_PREP(RTL8365MB_CPU_CTRL_RXBYTECOUNT_MASK, cpu->rx_length) | 2994 FIELD_PREP(RTL8365MB_CPU_CTRL_TAG_FORMAT_MASK, cpu->format) | 2995 FIELD_PREP(RTL8365MB_CPU_CTRL_TRAP_PORT_MASK, cpu->trap_port & 0x7) | 2996 FIELD_PREP(RTL8365MB_CPU_CTRL_TRAP_PORT_EXT_MASK, 2997 cpu->trap_port >> 3 & 0x1); 2998 ret = regmap_write(priv->map, RTL8365MB_CPU_CTRL_REG, val); 2999 if (ret) 3000 return ret; 3001 3002 return 0; 3003 } 3004 3005 static int rtl8365mb_change_tag_protocol(struct dsa_switch *ds, 3006 enum dsa_tag_protocol proto) 3007 { 3008 struct realtek_priv *priv = ds->priv; 3009 struct rtl8365mb_cpu *cpu; 3010 struct rtl8365mb *mb; 3011 3012 mb = priv->chip_data; 3013 cpu = &mb->cpu; 3014 3015 switch (proto) { 3016 case DSA_TAG_PROTO_RTL8_4: 3017 cpu->format = RTL8365MB_CPU_FORMAT_8BYTES; 3018 cpu->position = RTL8365MB_CPU_POS_AFTER_SA; 3019 break; 3020 case DSA_TAG_PROTO_RTL8_4T: 3021 cpu->format = RTL8365MB_CPU_FORMAT_8BYTES; 3022 cpu->position = RTL8365MB_CPU_POS_BEFORE_CRC; 3023 break; 3024 /* The switch also supports a 4-byte format, similar to rtl4a but with 3025 * the same 0x04 8-bit version and probably 8-bit port source/dest. 3026 * There is no public doc about it. Not supported yet and it will probably 3027 * never be. 3028 */ 3029 default: 3030 return -EPROTONOSUPPORT; 3031 } 3032 3033 return rtl8365mb_cpu_config(priv); 3034 } 3035 3036 static int rtl8365mb_switch_init(struct realtek_priv *priv) 3037 { 3038 struct rtl8365mb *mb = priv->chip_data; 3039 const struct rtl8365mb_chip_info *ci; 3040 int ret; 3041 int i; 3042 3043 ci = mb->chip_info; 3044 3045 /* Do any chip-specific init jam before getting to the common stuff */ 3046 if (ci->jam_table) { 3047 for (i = 0; i < ci->jam_size; i++) { 3048 ret = regmap_write(priv->map, ci->jam_table[i].reg, 3049 ci->jam_table[i].val); 3050 if (ret) 3051 return ret; 3052 } 3053 } 3054 3055 /* Common init jam */ 3056 for (i = 0; i < ARRAY_SIZE(rtl8365mb_init_jam_common); i++) { 3057 ret = regmap_write(priv->map, rtl8365mb_init_jam_common[i].reg, 3058 rtl8365mb_init_jam_common[i].val); 3059 if (ret) 3060 return ret; 3061 } 3062 3063 return 0; 3064 } 3065 3066 static int rtl8365mb_reset_chip(struct realtek_priv *priv) 3067 { 3068 u32 val; 3069 3070 priv->write_reg_noack(priv, RTL8365MB_CHIP_RESET_REG, 3071 FIELD_PREP(RTL8365MB_CHIP_RESET_HW_MASK, 1)); 3072 3073 /* Realtek documentation says the chip needs 1 second to reset. Sleep 3074 * for 100 ms before accessing any registers to prevent ACK timeouts. 3075 */ 3076 msleep(100); 3077 return regmap_read_poll_timeout(priv->map, RTL8365MB_CHIP_RESET_REG, val, 3078 !(val & RTL8365MB_CHIP_RESET_HW_MASK), 3079 20000, 1e6); 3080 } 3081 3082 static int rtl8365mb_setup(struct dsa_switch *ds) 3083 { 3084 struct realtek_priv *priv = ds->priv; 3085 struct rtl8365mb_cpu *cpu; 3086 u32 downports_mask = 0; 3087 u32 upports_mask = 0; 3088 struct rtl8365mb *mb; 3089 struct dsa_port *dp; 3090 int ret; 3091 3092 mb = priv->chip_data; 3093 cpu = &mb->cpu; 3094 3095 mb->pcs.ops = &rtl8365mb_pcs_ops; 3096 3097 /* The SerDes has no link interrupt wired up, so phylink must poll the 3098 * PCS for link changes when it tracks the link through pcs_get_state() 3099 * (in-band mode with autonegotiation disabled). 3100 */ 3101 mb->pcs.poll = true; 3102 3103 ret = rtl8365mb_reset_chip(priv); 3104 if (ret) { 3105 dev_err(priv->dev, "failed to reset chip: %pe\n", 3106 ERR_PTR(ret)); 3107 goto out_error; 3108 } 3109 3110 ret = rtl8365mb_sds_probe_option(priv); 3111 if (ret) { 3112 dev_err(priv->dev, "failed to probe SerDes chip option: %pe\n", 3113 ERR_PTR(ret)); 3114 goto out_error; 3115 } 3116 3117 /* Configure switch to vendor-defined initial state */ 3118 ret = rtl8365mb_switch_init(priv); 3119 if (ret) { 3120 dev_err(priv->dev, "failed to initialize switch: %pe\n", 3121 ERR_PTR(ret)); 3122 goto out_error; 3123 } 3124 3125 if (mb->sds_supported) { 3126 ret = rtl8365mb_sds_raise_rate_limits(priv); 3127 if (ret) { 3128 dev_err(priv->dev, 3129 "failed to raise port rate limits: %pe\n", 3130 ERR_PTR(ret)); 3131 goto out_error; 3132 } 3133 } 3134 3135 /* Set up cascading IRQs */ 3136 ret = rtl8365mb_irq_setup(priv); 3137 if (ret == -EPROBE_DEFER) 3138 return ret; 3139 else if (ret) 3140 dev_info(priv->dev, "no interrupt support\n"); 3141 3142 dsa_switch_for_each_port(dp, ds) { 3143 /* Cascading (DSA links) is not supported yet. 3144 * Historically, the driver has always been broken 3145 * without a dedicated CPU port because CPU tagging 3146 * would be disabled, rendering the switch entirely 3147 * non-functional for DSA operations. 3148 */ 3149 if (dsa_port_is_dsa(dp)) { 3150 dev_err(priv->dev, "Cascading (DSA link) not supported\n"); 3151 ret = -EOPNOTSUPP; 3152 goto out_teardown_irq; 3153 } 3154 } 3155 3156 /* Start with all ports blocked, including unused ports */ 3157 dsa_switch_for_each_port(dp, ds) { 3158 struct rtl8365mb_port *p = &mb->ports[dp->index]; 3159 3160 /* Set the initial STP state of all ports to DISABLED, otherwise 3161 * ports will still forward frames to the CPU despite being 3162 * administratively down by default. 3163 */ 3164 rtl8365mb_port_stp_state_set(ds, dp->index, BR_STATE_DISABLED); 3165 3166 /* Start with all port completely isolated */ 3167 ret = rtl8365mb_port_set_isolation(priv, dp->index, 0); 3168 if (ret) 3169 goto out_teardown_irq; 3170 3171 /* Set the default EFID 0 for standalone mode */ 3172 ret = rtl8365mb_port_set_efid(priv, dp->index, 0); 3173 if (ret) 3174 goto out_teardown_irq; 3175 3176 /* Disable learning */ 3177 ret = rtl8365mb_port_set_learning(priv, dp->index, false); 3178 if (ret) 3179 goto out_teardown_irq; 3180 3181 /* Enable all types of flooding */ 3182 ret = rtl83xx_setup_port_flood_control(priv, dp->index); 3183 if (ret) 3184 goto out_teardown_irq; 3185 3186 /* Set up per-port private data */ 3187 p->priv = priv; 3188 p->index = dp->index; 3189 3190 /* Collect CPU ports. If we support cascade switches, it should 3191 * also include the upstream DSA ports. 3192 */ 3193 if (!dsa_port_is_cpu(dp)) 3194 continue; 3195 3196 upports_mask |= BIT(dp->index); 3197 } 3198 3199 /* Configure user ports */ 3200 dsa_switch_for_each_port(dp, ds) { 3201 if (!dsa_port_is_user(dp)) 3202 continue; 3203 3204 /* Forward only to the CPU */ 3205 ret = rtl8365mb_port_set_isolation(priv, dp->index, 3206 upports_mask); 3207 if (ret) 3208 goto out_teardown_irq; 3209 3210 /* If we support cascade switches, it should also include the 3211 * downstream DSA ports. 3212 */ 3213 downports_mask |= BIT(dp->index); 3214 } 3215 3216 /* Configure CPU tagging */ 3217 /* If we support cascade switches, it should also include the upstream 3218 * DSA ports. 3219 */ 3220 dsa_switch_for_each_cpu_port(dp, ds) { 3221 /* Use the first CPU port as trap_port */ 3222 if (cpu->trap_port == RTL8365MB_MAX_NUM_PORTS) 3223 cpu->trap_port = dp->index; 3224 3225 /* Forward to all user ports */ 3226 ret = rtl8365mb_port_set_isolation(priv, dp->index, 3227 downports_mask); 3228 if (ret) 3229 goto out_teardown_irq; 3230 } 3231 3232 cpu->mask = upports_mask; 3233 cpu->enable = cpu->mask > 0; 3234 3235 if (!cpu->enable) { 3236 dev_err(priv->dev, "no CPU port defined\n"); 3237 ret = -EINVAL; 3238 goto out_teardown_irq; 3239 } 3240 3241 ret = rtl8365mb_cpu_config(priv); 3242 if (ret) 3243 goto out_teardown_irq; 3244 3245 ret = rtl8365mb_port_change_mtu(ds, cpu->trap_port, ETH_DATA_LEN); 3246 if (ret) 3247 goto out_teardown_irq; 3248 3249 ds->assisted_learning_on_cpu_port = true; 3250 ds->fdb_isolation = true; 3251 /* The EFID is 3 bits, but EFID 0 is reserved for standalone ports */ 3252 ds->max_num_bridges = FIELD_MAX(RTL8365MB_EFID_MASK); 3253 3254 ds->configure_vlan_while_not_filtering = true; 3255 3256 /* Set up VLAN */ 3257 ret = rtl8365mb_vlan_setup(ds); 3258 if (ret) 3259 goto out_teardown_irq; 3260 3261 ret = rtl83xx_setup_user_mdio(ds); 3262 if (ret) { 3263 dev_err(priv->dev, "could not set up MDIO bus\n"); 3264 goto out_teardown_irq; 3265 } 3266 3267 /* Start statistics counter polling */ 3268 ret = rtl8365mb_stats_setup(priv); 3269 if (ret) { 3270 dev_err(priv->dev, "failed to setup stats: %pe\n", 3271 ERR_PTR(ret)); 3272 goto out_teardown_irq; 3273 } 3274 3275 return 0; 3276 3277 out_teardown_irq: 3278 rtl8365mb_irq_teardown(priv); 3279 3280 out_error: 3281 return ret; 3282 } 3283 3284 static void rtl8365mb_teardown(struct dsa_switch *ds) 3285 { 3286 struct realtek_priv *priv = ds->priv; 3287 3288 rtl8365mb_stats_teardown(priv); 3289 rtl8365mb_irq_teardown(priv); 3290 } 3291 3292 static int rtl8365mb_get_chip_id_and_ver(struct regmap *map, u32 *id, u32 *ver) 3293 { 3294 int ret; 3295 3296 /* For some reason we have to write a magic value to an arbitrary 3297 * register whenever accessing the chip ID/version registers. 3298 */ 3299 ret = regmap_write(map, RTL8365MB_MAGIC_REG, RTL8365MB_MAGIC_VALUE); 3300 if (ret) 3301 return ret; 3302 3303 ret = regmap_read(map, RTL8365MB_CHIP_ID_REG, id); 3304 if (ret) 3305 return ret; 3306 3307 ret = regmap_read(map, RTL8365MB_CHIP_VER_REG, ver); 3308 if (ret) 3309 return ret; 3310 3311 /* Reset magic register */ 3312 ret = regmap_write(map, RTL8365MB_MAGIC_REG, 0); 3313 if (ret) 3314 return ret; 3315 3316 return 0; 3317 } 3318 3319 static int rtl8365mb_detect(struct realtek_priv *priv) 3320 { 3321 struct rtl8365mb *mb = priv->chip_data; 3322 u32 chip_id; 3323 u32 chip_ver; 3324 int ret; 3325 int i; 3326 3327 ret = rtl8365mb_get_chip_id_and_ver(priv->map, &chip_id, &chip_ver); 3328 if (ret) { 3329 dev_err(priv->dev, "failed to read chip id and version: %pe\n", 3330 ERR_PTR(ret)); 3331 return ret; 3332 } 3333 3334 for (i = 0; i < ARRAY_SIZE(rtl8365mb_chip_infos); i++) { 3335 const struct rtl8365mb_chip_info *ci = &rtl8365mb_chip_infos[i]; 3336 3337 if (ci->chip_id == chip_id && ci->chip_ver == chip_ver) { 3338 mb->chip_info = ci; 3339 break; 3340 } 3341 } 3342 3343 if (!mb->chip_info) { 3344 dev_err(priv->dev, 3345 "unrecognized switch (id=0x%04x, ver=0x%04x)", chip_id, 3346 chip_ver); 3347 return -ENODEV; 3348 } 3349 3350 dev_info(priv->dev, "found an %s switch\n", mb->chip_info->name); 3351 3352 priv->num_ports = RTL8365MB_MAX_NUM_PORTS; 3353 mb->priv = priv; 3354 mb->cpu.trap_port = RTL8365MB_MAX_NUM_PORTS; 3355 mb->cpu.insert = RTL8365MB_CPU_INSERT_TO_ALL; 3356 mb->cpu.position = RTL8365MB_CPU_POS_AFTER_SA; 3357 mb->cpu.rx_length = RTL8365MB_CPU_RXLEN_64BYTES; 3358 mb->cpu.format = RTL8365MB_CPU_FORMAT_8BYTES; 3359 3360 return 0; 3361 } 3362 3363 static int rtl8365mb_phylink_mac_enable_tx_lpi(struct phylink_config *config, 3364 u32 timer, bool tx_clock_stop) 3365 { 3366 /* The hardware manages LPI itself; there is no MAC-level LPI control. 3367 * This callback only signals LPI awareness to phylink. 3368 */ 3369 return 0; 3370 } 3371 3372 static void rtl8365mb_phylink_mac_disable_tx_lpi(struct phylink_config *config) 3373 { 3374 } 3375 3376 static const struct phylink_mac_ops rtl8365mb_phylink_mac_ops = { 3377 .mac_select_pcs = rtl8365mb_phylink_mac_select_pcs, 3378 .mac_config = rtl8365mb_phylink_mac_config, 3379 .mac_link_down = rtl8365mb_phylink_mac_link_down, 3380 .mac_link_up = rtl8365mb_phylink_mac_link_up, 3381 .mac_enable_tx_lpi = rtl8365mb_phylink_mac_enable_tx_lpi, 3382 .mac_disable_tx_lpi = rtl8365mb_phylink_mac_disable_tx_lpi, 3383 }; 3384 3385 static bool rtl8365mb_support_eee(struct dsa_switch *ds, int port) 3386 { 3387 /* Only integrated-PHY ports support EEE, not the external RGMII ports. */ 3388 return !rtl8365mb_get_port_extint(ds->priv, port); 3389 } 3390 3391 static int rtl8365mb_set_mac_eee(struct dsa_switch *ds, int port, 3392 struct ethtool_keee *e) 3393 { 3394 struct realtek_priv *priv = ds->priv; 3395 3396 /* The only LPI timing control (tx_lpi_timer) is a single global 3397 * per-speed register shared by all ports, so it cannot be set from this 3398 * per-port callback; leave it at its reset default. Per-port EEE is 3399 * driven through the PHY advertisement. Reject the per-port TX LPI knobs 3400 * rather than silently ignoring them. 3401 */ 3402 if (!e->tx_lpi_enabled) { 3403 dev_err(priv->dev, "disabling EEE TX LPI is not supported\n"); 3404 return -EOPNOTSUPP; 3405 } 3406 3407 if (e->tx_lpi_timer) { 3408 dev_err(priv->dev, 3409 "setting the EEE TX LPI timer is not supported\n"); 3410 return -EOPNOTSUPP; 3411 } 3412 3413 return 0; 3414 } 3415 3416 static const struct dsa_switch_ops rtl8365mb_switch_ops = { 3417 .get_tag_protocol = rtl8365mb_get_tag_protocol, 3418 .change_tag_protocol = rtl8365mb_change_tag_protocol, 3419 .setup = rtl8365mb_setup, 3420 .teardown = rtl8365mb_teardown, 3421 .support_eee = rtl8365mb_support_eee, 3422 .set_mac_eee = rtl8365mb_set_mac_eee, 3423 .phylink_get_caps = rtl8365mb_phylink_get_caps, 3424 .port_bridge_join = rtl83xx_port_bridge_join, 3425 .port_bridge_leave = rtl83xx_port_bridge_leave, 3426 .port_pre_bridge_flags = rtl8365mb_port_pre_bridge_flags, 3427 .port_bridge_flags = rtl83xx_port_bridge_flags, 3428 .port_stp_state_set = rtl8365mb_port_stp_state_set, 3429 .port_fast_age = rtl83xx_port_fast_age, 3430 .port_fdb_add = rtl83xx_port_fdb_add, 3431 .port_fdb_del = rtl83xx_port_fdb_del, 3432 .port_fdb_dump = rtl83xx_port_fdb_dump, 3433 .port_mdb_add = rtl83xx_port_mdb_add, 3434 .port_mdb_del = rtl83xx_port_mdb_del, 3435 .port_vlan_add = rtl8365mb_port_vlan_add, 3436 .port_vlan_del = rtl8365mb_port_vlan_del, 3437 .port_vlan_filtering = rtl8365mb_port_vlan_filtering, 3438 .get_strings = rtl8365mb_get_strings, 3439 .get_ethtool_stats = rtl8365mb_get_ethtool_stats, 3440 .get_sset_count = rtl8365mb_get_sset_count, 3441 .get_eth_phy_stats = rtl8365mb_get_phy_stats, 3442 .get_eth_mac_stats = rtl8365mb_get_mac_stats, 3443 .get_eth_ctrl_stats = rtl8365mb_get_ctrl_stats, 3444 .get_stats64 = rtl8365mb_get_stats64, 3445 .port_change_mtu = rtl8365mb_port_change_mtu, 3446 .port_max_mtu = rtl8365mb_port_max_mtu, 3447 .port_hsr_join = dsa_port_simple_hsr_join, 3448 .port_hsr_leave = dsa_port_simple_hsr_leave, 3449 }; 3450 3451 static const struct realtek_ops rtl8365mb_ops = { 3452 .detect = rtl8365mb_detect, 3453 .port_add_isolation = rtl8365mb_port_add_isolation, 3454 .port_remove_isolation = rtl8365mb_port_remove_isolation, 3455 .port_set_efid = rtl8365mb_port_set_efid, 3456 .port_set_learning = rtl8365mb_port_set_learning, 3457 .port_set_ucast_flood = rtl8365mb_port_set_ucast_flood, 3458 .port_set_mcast_flood = rtl8365mb_port_set_mcast_flood, 3459 .port_set_bcast_flood = rtl8365mb_port_set_bcast_flood, 3460 .l2_add_uc = rtl8365mb_l2_add_uc, 3461 .l2_del_uc = rtl8365mb_l2_del_uc, 3462 .l2_get_next_uc = rtl8365mb_l2_get_next_uc, 3463 .l2_add_mc = rtl8365mb_l2_add_mc, 3464 .l2_del_mc = rtl8365mb_l2_del_mc, 3465 .l2_flush = rtl8365mb_l2_flush, 3466 .phy_read = rtl8365mb_phy_read, 3467 .phy_write = rtl8365mb_phy_write, 3468 .phy_read_c45 = rtl8365mb_phy_read_c45, 3469 .phy_write_c45 = rtl8365mb_phy_write_c45, 3470 }; 3471 3472 static const char *const rtl8365mb_supplies[] = { 3473 "avddh", "avddl", "dvddio", "dvddio1", "dvddl", "pllvddl", 3474 }; 3475 3476 const struct realtek_variant rtl8365mb_variant = { 3477 .ds_ops = &rtl8365mb_switch_ops, 3478 .ops = &rtl8365mb_ops, 3479 .phylink_mac_ops = &rtl8365mb_phylink_mac_ops, 3480 .clk_delay = 10, 3481 .cmd_read = 0xb9, 3482 .cmd_write = 0xb8, 3483 .chip_data_sz = sizeof(struct rtl8365mb), 3484 .supplies = rtl8365mb_supplies, 3485 .num_supplies = ARRAY_SIZE(rtl8365mb_supplies), 3486 }; 3487 3488 static const struct of_device_id rtl8365mb_of_match[] = { 3489 { .compatible = "realtek,rtl8365mb", .data = &rtl8365mb_variant, }, 3490 { /* sentinel */ } 3491 }; 3492 MODULE_DEVICE_TABLE(of, rtl8365mb_of_match); 3493 3494 static struct platform_driver rtl8365mb_smi_driver = { 3495 .driver = { 3496 .name = "rtl8365mb-smi", 3497 .of_match_table = rtl8365mb_of_match, 3498 }, 3499 .probe = realtek_smi_probe, 3500 .remove = realtek_smi_remove, 3501 .shutdown = realtek_smi_shutdown, 3502 }; 3503 3504 static struct mdio_driver rtl8365mb_mdio_driver = { 3505 .mdiodrv.driver = { 3506 .name = "rtl8365mb-mdio", 3507 .of_match_table = rtl8365mb_of_match, 3508 }, 3509 .probe = realtek_mdio_probe, 3510 .remove = realtek_mdio_remove, 3511 .shutdown = realtek_mdio_shutdown, 3512 }; 3513 3514 static int rtl8365mb_init(void) 3515 { 3516 int ret; 3517 3518 ret = realtek_mdio_driver_register(&rtl8365mb_mdio_driver); 3519 if (ret) 3520 return ret; 3521 3522 ret = realtek_smi_driver_register(&rtl8365mb_smi_driver); 3523 if (ret) { 3524 realtek_mdio_driver_unregister(&rtl8365mb_mdio_driver); 3525 return ret; 3526 } 3527 3528 return 0; 3529 } 3530 module_init(rtl8365mb_init); 3531 3532 static void __exit rtl8365mb_exit(void) 3533 { 3534 realtek_smi_driver_unregister(&rtl8365mb_smi_driver); 3535 realtek_mdio_driver_unregister(&rtl8365mb_mdio_driver); 3536 } 3537 module_exit(rtl8365mb_exit); 3538 3539 MODULE_AUTHOR("Alvin Šipraga <alsi@bang-olufsen.dk>"); 3540 MODULE_DESCRIPTION("Driver for RTL8365MB-VC ethernet switch"); 3541 MODULE_LICENSE("GPL"); 3542 MODULE_IMPORT_NS("REALTEK_DSA"); 3543