b.liu | e958203 | 2025-04-17 19:18:16 +0800 | [diff] [blame^] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
| 2 | /* |
| 3 | * Mediatek MT7530 DSA Switch driver |
| 4 | * Copyright (C) 2017 Sean Wang <sean.wang@mediatek.com> |
| 5 | */ |
| 6 | #include <linux/etherdevice.h> |
| 7 | #include <linux/if_bridge.h> |
| 8 | #include <linux/iopoll.h> |
| 9 | #include <linux/mdio.h> |
| 10 | #include <linux/mfd/syscon.h> |
| 11 | #include <linux/module.h> |
| 12 | #include <linux/netdevice.h> |
| 13 | #include <linux/of_mdio.h> |
| 14 | #include <linux/of_net.h> |
| 15 | #include <linux/of_platform.h> |
| 16 | #include <linux/phylink.h> |
| 17 | #include <linux/regmap.h> |
| 18 | #include <linux/regulator/consumer.h> |
| 19 | #include <linux/reset.h> |
| 20 | #include <linux/gpio/consumer.h> |
| 21 | #include <net/dsa.h> |
| 22 | |
| 23 | #include "mt7530.h" |
| 24 | |
| 25 | /* String, offset, and register size in bytes if different from 4 bytes */ |
| 26 | static const struct mt7530_mib_desc mt7530_mib[] = { |
| 27 | MIB_DESC(1, 0x00, "TxDrop"), |
| 28 | MIB_DESC(1, 0x04, "TxCrcErr"), |
| 29 | MIB_DESC(1, 0x08, "TxUnicast"), |
| 30 | MIB_DESC(1, 0x0c, "TxMulticast"), |
| 31 | MIB_DESC(1, 0x10, "TxBroadcast"), |
| 32 | MIB_DESC(1, 0x14, "TxCollision"), |
| 33 | MIB_DESC(1, 0x18, "TxSingleCollision"), |
| 34 | MIB_DESC(1, 0x1c, "TxMultipleCollision"), |
| 35 | MIB_DESC(1, 0x20, "TxDeferred"), |
| 36 | MIB_DESC(1, 0x24, "TxLateCollision"), |
| 37 | MIB_DESC(1, 0x28, "TxExcessiveCollistion"), |
| 38 | MIB_DESC(1, 0x2c, "TxPause"), |
| 39 | MIB_DESC(1, 0x30, "TxPktSz64"), |
| 40 | MIB_DESC(1, 0x34, "TxPktSz65To127"), |
| 41 | MIB_DESC(1, 0x38, "TxPktSz128To255"), |
| 42 | MIB_DESC(1, 0x3c, "TxPktSz256To511"), |
| 43 | MIB_DESC(1, 0x40, "TxPktSz512To1023"), |
| 44 | MIB_DESC(1, 0x44, "Tx1024ToMax"), |
| 45 | MIB_DESC(2, 0x48, "TxBytes"), |
| 46 | MIB_DESC(1, 0x60, "RxDrop"), |
| 47 | MIB_DESC(1, 0x64, "RxFiltering"), |
| 48 | MIB_DESC(1, 0x68, "RxUnicast"), |
| 49 | MIB_DESC(1, 0x6c, "RxMulticast"), |
| 50 | MIB_DESC(1, 0x70, "RxBroadcast"), |
| 51 | MIB_DESC(1, 0x74, "RxAlignErr"), |
| 52 | MIB_DESC(1, 0x78, "RxCrcErr"), |
| 53 | MIB_DESC(1, 0x7c, "RxUnderSizeErr"), |
| 54 | MIB_DESC(1, 0x80, "RxFragErr"), |
| 55 | MIB_DESC(1, 0x84, "RxOverSzErr"), |
| 56 | MIB_DESC(1, 0x88, "RxJabberErr"), |
| 57 | MIB_DESC(1, 0x8c, "RxPause"), |
| 58 | MIB_DESC(1, 0x90, "RxPktSz64"), |
| 59 | MIB_DESC(1, 0x94, "RxPktSz65To127"), |
| 60 | MIB_DESC(1, 0x98, "RxPktSz128To255"), |
| 61 | MIB_DESC(1, 0x9c, "RxPktSz256To511"), |
| 62 | MIB_DESC(1, 0xa0, "RxPktSz512To1023"), |
| 63 | MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"), |
| 64 | MIB_DESC(2, 0xa8, "RxBytes"), |
| 65 | MIB_DESC(1, 0xb0, "RxCtrlDrop"), |
| 66 | MIB_DESC(1, 0xb4, "RxIngressDrop"), |
| 67 | MIB_DESC(1, 0xb8, "RxArlDrop"), |
| 68 | }; |
| 69 | |
| 70 | static int |
| 71 | core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad) |
| 72 | { |
| 73 | struct mii_bus *bus = priv->bus; |
| 74 | int value, ret; |
| 75 | |
| 76 | /* Write the desired MMD Devad */ |
| 77 | ret = bus->write(bus, 0, MII_MMD_CTRL, devad); |
| 78 | if (ret < 0) |
| 79 | goto err; |
| 80 | |
| 81 | /* Write the desired MMD register address */ |
| 82 | ret = bus->write(bus, 0, MII_MMD_DATA, prtad); |
| 83 | if (ret < 0) |
| 84 | goto err; |
| 85 | |
| 86 | /* Select the Function : DATA with no post increment */ |
| 87 | ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR)); |
| 88 | if (ret < 0) |
| 89 | goto err; |
| 90 | |
| 91 | /* Read the content of the MMD's selected register */ |
| 92 | value = bus->read(bus, 0, MII_MMD_DATA); |
| 93 | |
| 94 | return value; |
| 95 | err: |
| 96 | dev_err(&bus->dev, "failed to read mmd register\n"); |
| 97 | |
| 98 | return ret; |
| 99 | } |
| 100 | |
| 101 | static int |
| 102 | core_write_mmd_indirect(struct mt7530_priv *priv, int prtad, |
| 103 | int devad, u32 data) |
| 104 | { |
| 105 | struct mii_bus *bus = priv->bus; |
| 106 | int ret; |
| 107 | |
| 108 | /* Write the desired MMD Devad */ |
| 109 | ret = bus->write(bus, 0, MII_MMD_CTRL, devad); |
| 110 | if (ret < 0) |
| 111 | goto err; |
| 112 | |
| 113 | /* Write the desired MMD register address */ |
| 114 | ret = bus->write(bus, 0, MII_MMD_DATA, prtad); |
| 115 | if (ret < 0) |
| 116 | goto err; |
| 117 | |
| 118 | /* Select the Function : DATA with no post increment */ |
| 119 | ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR)); |
| 120 | if (ret < 0) |
| 121 | goto err; |
| 122 | |
| 123 | /* Write the data into MMD's selected register */ |
| 124 | ret = bus->write(bus, 0, MII_MMD_DATA, data); |
| 125 | err: |
| 126 | if (ret < 0) |
| 127 | dev_err(&bus->dev, |
| 128 | "failed to write mmd register\n"); |
| 129 | return ret; |
| 130 | } |
| 131 | |
| 132 | static void |
| 133 | core_write(struct mt7530_priv *priv, u32 reg, u32 val) |
| 134 | { |
| 135 | struct mii_bus *bus = priv->bus; |
| 136 | |
| 137 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
| 138 | |
| 139 | core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val); |
| 140 | |
| 141 | mutex_unlock(&bus->mdio_lock); |
| 142 | } |
| 143 | |
| 144 | static void |
| 145 | core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set) |
| 146 | { |
| 147 | struct mii_bus *bus = priv->bus; |
| 148 | u32 val; |
| 149 | |
| 150 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
| 151 | |
| 152 | val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2); |
| 153 | val &= ~mask; |
| 154 | val |= set; |
| 155 | core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val); |
| 156 | |
| 157 | mutex_unlock(&bus->mdio_lock); |
| 158 | } |
| 159 | |
| 160 | static void |
| 161 | core_set(struct mt7530_priv *priv, u32 reg, u32 val) |
| 162 | { |
| 163 | core_rmw(priv, reg, 0, val); |
| 164 | } |
| 165 | |
| 166 | static void |
| 167 | core_clear(struct mt7530_priv *priv, u32 reg, u32 val) |
| 168 | { |
| 169 | core_rmw(priv, reg, val, 0); |
| 170 | } |
| 171 | |
| 172 | static int |
| 173 | mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val) |
| 174 | { |
| 175 | struct mii_bus *bus = priv->bus; |
| 176 | u16 page, r, lo, hi; |
| 177 | int ret; |
| 178 | |
| 179 | page = (reg >> 6) & 0x3ff; |
| 180 | r = (reg >> 2) & 0xf; |
| 181 | lo = val & 0xffff; |
| 182 | hi = val >> 16; |
| 183 | |
| 184 | /* MT7530 uses 31 as the pseudo port */ |
| 185 | ret = bus->write(bus, 0x1f, 0x1f, page); |
| 186 | if (ret < 0) |
| 187 | goto err; |
| 188 | |
| 189 | ret = bus->write(bus, 0x1f, r, lo); |
| 190 | if (ret < 0) |
| 191 | goto err; |
| 192 | |
| 193 | ret = bus->write(bus, 0x1f, 0x10, hi); |
| 194 | err: |
| 195 | if (ret < 0) |
| 196 | dev_err(&bus->dev, |
| 197 | "failed to write mt7530 register\n"); |
| 198 | return ret; |
| 199 | } |
| 200 | |
| 201 | static u32 |
| 202 | mt7530_mii_read(struct mt7530_priv *priv, u32 reg) |
| 203 | { |
| 204 | struct mii_bus *bus = priv->bus; |
| 205 | u16 page, r, lo, hi; |
| 206 | int ret; |
| 207 | |
| 208 | page = (reg >> 6) & 0x3ff; |
| 209 | r = (reg >> 2) & 0xf; |
| 210 | |
| 211 | /* MT7530 uses 31 as the pseudo port */ |
| 212 | ret = bus->write(bus, 0x1f, 0x1f, page); |
| 213 | if (ret < 0) { |
| 214 | dev_err(&bus->dev, |
| 215 | "failed to read mt7530 register\n"); |
| 216 | return ret; |
| 217 | } |
| 218 | |
| 219 | lo = bus->read(bus, 0x1f, r); |
| 220 | hi = bus->read(bus, 0x1f, 0x10); |
| 221 | |
| 222 | return (hi << 16) | (lo & 0xffff); |
| 223 | } |
| 224 | |
| 225 | static void |
| 226 | mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val) |
| 227 | { |
| 228 | struct mii_bus *bus = priv->bus; |
| 229 | |
| 230 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
| 231 | |
| 232 | mt7530_mii_write(priv, reg, val); |
| 233 | |
| 234 | mutex_unlock(&bus->mdio_lock); |
| 235 | } |
| 236 | |
| 237 | static u32 |
| 238 | _mt7530_read(struct mt7530_dummy_poll *p) |
| 239 | { |
| 240 | struct mii_bus *bus = p->priv->bus; |
| 241 | u32 val; |
| 242 | |
| 243 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
| 244 | |
| 245 | val = mt7530_mii_read(p->priv, p->reg); |
| 246 | |
| 247 | mutex_unlock(&bus->mdio_lock); |
| 248 | |
| 249 | return val; |
| 250 | } |
| 251 | |
| 252 | static u32 |
| 253 | mt7530_read(struct mt7530_priv *priv, u32 reg) |
| 254 | { |
| 255 | struct mt7530_dummy_poll p; |
| 256 | |
| 257 | INIT_MT7530_DUMMY_POLL(&p, priv, reg); |
| 258 | return _mt7530_read(&p); |
| 259 | } |
| 260 | |
| 261 | static void |
| 262 | mt7530_rmw(struct mt7530_priv *priv, u32 reg, |
| 263 | u32 mask, u32 set) |
| 264 | { |
| 265 | struct mii_bus *bus = priv->bus; |
| 266 | u32 val; |
| 267 | |
| 268 | mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); |
| 269 | |
| 270 | val = mt7530_mii_read(priv, reg); |
| 271 | val &= ~mask; |
| 272 | val |= set; |
| 273 | mt7530_mii_write(priv, reg, val); |
| 274 | |
| 275 | mutex_unlock(&bus->mdio_lock); |
| 276 | } |
| 277 | |
| 278 | static void |
| 279 | mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val) |
| 280 | { |
| 281 | mt7530_rmw(priv, reg, 0, val); |
| 282 | } |
| 283 | |
| 284 | static void |
| 285 | mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val) |
| 286 | { |
| 287 | mt7530_rmw(priv, reg, val, 0); |
| 288 | } |
| 289 | |
| 290 | static int |
| 291 | mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp) |
| 292 | { |
| 293 | u32 val; |
| 294 | int ret; |
| 295 | struct mt7530_dummy_poll p; |
| 296 | |
| 297 | /* Set the command operating upon the MAC address entries */ |
| 298 | val = ATC_BUSY | ATC_MAT(0) | cmd; |
| 299 | mt7530_write(priv, MT7530_ATC, val); |
| 300 | |
| 301 | INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC); |
| 302 | ret = readx_poll_timeout(_mt7530_read, &p, val, |
| 303 | !(val & ATC_BUSY), 20, 20000); |
| 304 | if (ret < 0) { |
| 305 | dev_err(priv->dev, "reset timeout\n"); |
| 306 | return ret; |
| 307 | } |
| 308 | |
| 309 | /* Additional sanity for read command if the specified |
| 310 | * entry is invalid |
| 311 | */ |
| 312 | val = mt7530_read(priv, MT7530_ATC); |
| 313 | if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID)) |
| 314 | return -EINVAL; |
| 315 | |
| 316 | if (rsp) |
| 317 | *rsp = val; |
| 318 | |
| 319 | return 0; |
| 320 | } |
| 321 | |
| 322 | static void |
| 323 | mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb) |
| 324 | { |
| 325 | u32 reg[3]; |
| 326 | int i; |
| 327 | |
| 328 | /* Read from ARL table into an array */ |
| 329 | for (i = 0; i < 3; i++) { |
| 330 | reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4)); |
| 331 | |
| 332 | dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n", |
| 333 | __func__, __LINE__, i, reg[i]); |
| 334 | } |
| 335 | |
| 336 | fdb->vid = (reg[1] >> CVID) & CVID_MASK; |
| 337 | fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK; |
| 338 | fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK; |
| 339 | fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK; |
| 340 | fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK; |
| 341 | fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK; |
| 342 | fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK; |
| 343 | fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK; |
| 344 | fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK; |
| 345 | fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT; |
| 346 | } |
| 347 | |
| 348 | static void |
| 349 | mt7530_fdb_write(struct mt7530_priv *priv, u16 vid, |
| 350 | u8 port_mask, const u8 *mac, |
| 351 | u8 aging, u8 type) |
| 352 | { |
| 353 | u32 reg[3] = { 0 }; |
| 354 | int i; |
| 355 | |
| 356 | reg[1] |= vid & CVID_MASK; |
| 357 | reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER; |
| 358 | reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP; |
| 359 | /* STATIC_ENT indicate that entry is static wouldn't |
| 360 | * be aged out and STATIC_EMP specified as erasing an |
| 361 | * entry |
| 362 | */ |
| 363 | reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS; |
| 364 | reg[1] |= mac[5] << MAC_BYTE_5; |
| 365 | reg[1] |= mac[4] << MAC_BYTE_4; |
| 366 | reg[0] |= mac[3] << MAC_BYTE_3; |
| 367 | reg[0] |= mac[2] << MAC_BYTE_2; |
| 368 | reg[0] |= mac[1] << MAC_BYTE_1; |
| 369 | reg[0] |= mac[0] << MAC_BYTE_0; |
| 370 | |
| 371 | /* Write array into the ARL table */ |
| 372 | for (i = 0; i < 3; i++) |
| 373 | mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]); |
| 374 | } |
| 375 | |
| 376 | static int |
| 377 | mt7530_pad_clk_setup(struct dsa_switch *ds, int mode) |
| 378 | { |
| 379 | struct mt7530_priv *priv = ds->priv; |
| 380 | u32 ncpo1, ssc_delta, trgint, i, xtal; |
| 381 | |
| 382 | xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK; |
| 383 | |
| 384 | if (xtal == HWTRAP_XTAL_20MHZ) { |
| 385 | dev_err(priv->dev, |
| 386 | "%s: MT7530 with a 20MHz XTAL is not supported!\n", |
| 387 | __func__); |
| 388 | return -EINVAL; |
| 389 | } |
| 390 | |
| 391 | switch (mode) { |
| 392 | case PHY_INTERFACE_MODE_RGMII: |
| 393 | trgint = 0; |
| 394 | /* PLL frequency: 125MHz */ |
| 395 | ncpo1 = 0x0c80; |
| 396 | break; |
| 397 | case PHY_INTERFACE_MODE_TRGMII: |
| 398 | trgint = 1; |
| 399 | if (priv->id == ID_MT7621) { |
| 400 | /* PLL frequency: 125MHz: 1.0GBit */ |
| 401 | if (xtal == HWTRAP_XTAL_40MHZ) |
| 402 | ncpo1 = 0x0640; |
| 403 | if (xtal == HWTRAP_XTAL_25MHZ) |
| 404 | ncpo1 = 0x0a00; |
| 405 | } else { /* PLL frequency: 250MHz: 2.0Gbit */ |
| 406 | if (xtal == HWTRAP_XTAL_40MHZ) |
| 407 | ncpo1 = 0x0c80; |
| 408 | if (xtal == HWTRAP_XTAL_25MHZ) |
| 409 | ncpo1 = 0x1400; |
| 410 | } |
| 411 | break; |
| 412 | default: |
| 413 | dev_err(priv->dev, "xMII mode %d not supported\n", mode); |
| 414 | return -EINVAL; |
| 415 | } |
| 416 | |
| 417 | if (xtal == HWTRAP_XTAL_25MHZ) |
| 418 | ssc_delta = 0x57; |
| 419 | else |
| 420 | ssc_delta = 0x87; |
| 421 | |
| 422 | mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK, |
| 423 | P6_INTF_MODE(trgint)); |
| 424 | |
| 425 | /* Lower Tx Driving for TRGMII path */ |
| 426 | for (i = 0 ; i < NUM_TRGMII_CTRL ; i++) |
| 427 | mt7530_write(priv, MT7530_TRGMII_TD_ODT(i), |
| 428 | TD_DM_DRVP(8) | TD_DM_DRVN(8)); |
| 429 | |
| 430 | /* Setup core clock for MT7530 */ |
| 431 | /* Disable MT7530 core clock */ |
| 432 | core_clear(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN); |
| 433 | |
| 434 | /* Disable PLL, since phy_device has not yet been created |
| 435 | * provided for phy_[read,write]_mmd_indirect is called, we |
| 436 | * provide our own core_write_mmd_indirect to complete this |
| 437 | * function. |
| 438 | */ |
| 439 | core_write_mmd_indirect(priv, |
| 440 | CORE_GSWPLL_GRP1, |
| 441 | MDIO_MMD_VEND2, |
| 442 | 0); |
| 443 | |
| 444 | /* Set core clock into 500Mhz */ |
| 445 | core_write(priv, CORE_GSWPLL_GRP2, |
| 446 | RG_GSWPLL_POSDIV_500M(1) | |
| 447 | RG_GSWPLL_FBKDIV_500M(25)); |
| 448 | |
| 449 | /* Enable PLL */ |
| 450 | core_write(priv, CORE_GSWPLL_GRP1, |
| 451 | RG_GSWPLL_EN_PRE | |
| 452 | RG_GSWPLL_POSDIV_200M(2) | |
| 453 | RG_GSWPLL_FBKDIV_200M(32)); |
| 454 | |
| 455 | /* Enable MT7530 core clock */ |
| 456 | core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN); |
| 457 | |
| 458 | /* Setup the MT7530 TRGMII Tx Clock */ |
| 459 | core_set(priv, CORE_TRGMII_GSW_CLK_CG, REG_GSWCK_EN); |
| 460 | core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1)); |
| 461 | core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0)); |
| 462 | core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta)); |
| 463 | core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta)); |
| 464 | core_write(priv, CORE_PLL_GROUP4, |
| 465 | RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN | |
| 466 | RG_SYSPLL_BIAS_LPF_EN); |
| 467 | core_write(priv, CORE_PLL_GROUP2, |
| 468 | RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN | |
| 469 | RG_SYSPLL_POSDIV(1)); |
| 470 | core_write(priv, CORE_PLL_GROUP7, |
| 471 | RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) | |
| 472 | RG_LCDDS_PWDB | RG_LCDDS_ISO_EN); |
| 473 | core_set(priv, CORE_TRGMII_GSW_CLK_CG, |
| 474 | REG_GSWCK_EN | REG_TRGMIICK_EN); |
| 475 | |
| 476 | if (!trgint) |
| 477 | for (i = 0 ; i < NUM_TRGMII_CTRL; i++) |
| 478 | mt7530_rmw(priv, MT7530_TRGMII_RD(i), |
| 479 | RD_TAP_MASK, RD_TAP(16)); |
| 480 | return 0; |
| 481 | } |
| 482 | |
| 483 | static void |
| 484 | mt7530_mib_reset(struct dsa_switch *ds) |
| 485 | { |
| 486 | struct mt7530_priv *priv = ds->priv; |
| 487 | |
| 488 | mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH); |
| 489 | mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE); |
| 490 | } |
| 491 | |
| 492 | static void |
| 493 | mt7530_port_set_status(struct mt7530_priv *priv, int port, int enable) |
| 494 | { |
| 495 | u32 mask = PMCR_TX_EN | PMCR_RX_EN | PMCR_FORCE_LNK; |
| 496 | |
| 497 | if (enable) |
| 498 | mt7530_set(priv, MT7530_PMCR_P(port), mask); |
| 499 | else |
| 500 | mt7530_clear(priv, MT7530_PMCR_P(port), mask); |
| 501 | } |
| 502 | |
| 503 | static int mt7530_phy_read(struct dsa_switch *ds, int port, int regnum) |
| 504 | { |
| 505 | struct mt7530_priv *priv = ds->priv; |
| 506 | |
| 507 | return mdiobus_read_nested(priv->bus, port, regnum); |
| 508 | } |
| 509 | |
| 510 | static int mt7530_phy_write(struct dsa_switch *ds, int port, int regnum, |
| 511 | u16 val) |
| 512 | { |
| 513 | struct mt7530_priv *priv = ds->priv; |
| 514 | |
| 515 | return mdiobus_write_nested(priv->bus, port, regnum, val); |
| 516 | } |
| 517 | |
| 518 | static void |
| 519 | mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset, |
| 520 | uint8_t *data) |
| 521 | { |
| 522 | int i; |
| 523 | |
| 524 | if (stringset != ETH_SS_STATS) |
| 525 | return; |
| 526 | |
| 527 | for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) |
| 528 | strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name, |
| 529 | ETH_GSTRING_LEN); |
| 530 | } |
| 531 | |
| 532 | static void |
| 533 | mt7530_get_ethtool_stats(struct dsa_switch *ds, int port, |
| 534 | uint64_t *data) |
| 535 | { |
| 536 | struct mt7530_priv *priv = ds->priv; |
| 537 | const struct mt7530_mib_desc *mib; |
| 538 | u32 reg, i; |
| 539 | u64 hi; |
| 540 | |
| 541 | for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) { |
| 542 | mib = &mt7530_mib[i]; |
| 543 | reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset; |
| 544 | |
| 545 | data[i] = mt7530_read(priv, reg); |
| 546 | if (mib->size == 2) { |
| 547 | hi = mt7530_read(priv, reg + 4); |
| 548 | data[i] |= hi << 32; |
| 549 | } |
| 550 | } |
| 551 | } |
| 552 | |
| 553 | static int |
| 554 | mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset) |
| 555 | { |
| 556 | if (sset != ETH_SS_STATS) |
| 557 | return 0; |
| 558 | |
| 559 | return ARRAY_SIZE(mt7530_mib); |
| 560 | } |
| 561 | |
| 562 | static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface) |
| 563 | { |
| 564 | struct mt7530_priv *priv = ds->priv; |
| 565 | u8 tx_delay = 0; |
| 566 | int val; |
| 567 | |
| 568 | mutex_lock(&priv->reg_mutex); |
| 569 | |
| 570 | val = mt7530_read(priv, MT7530_MHWTRAP); |
| 571 | |
| 572 | val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS; |
| 573 | val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL; |
| 574 | |
| 575 | switch (priv->p5_intf_sel) { |
| 576 | case P5_INTF_SEL_PHY_P0: |
| 577 | /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */ |
| 578 | val |= MHWTRAP_PHY0_SEL; |
| 579 | /* fall through */ |
| 580 | case P5_INTF_SEL_PHY_P4: |
| 581 | /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */ |
| 582 | val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS; |
| 583 | |
| 584 | /* Setup the MAC by default for the cpu port */ |
| 585 | mt7530_write(priv, MT7530_PMCR_P(5), 0x56300); |
| 586 | break; |
| 587 | case P5_INTF_SEL_GMAC5: |
| 588 | /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */ |
| 589 | val &= ~MHWTRAP_P5_DIS; |
| 590 | break; |
| 591 | case P5_DISABLED: |
| 592 | interface = PHY_INTERFACE_MODE_NA; |
| 593 | break; |
| 594 | default: |
| 595 | dev_err(ds->dev, "Unsupported p5_intf_sel %d\n", |
| 596 | priv->p5_intf_sel); |
| 597 | goto unlock_exit; |
| 598 | } |
| 599 | |
| 600 | /* Setup RGMII settings */ |
| 601 | if (phy_interface_mode_is_rgmii(interface)) { |
| 602 | val |= MHWTRAP_P5_RGMII_MODE; |
| 603 | |
| 604 | /* P5 RGMII RX Clock Control: delay setting for 1000M */ |
| 605 | mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN); |
| 606 | |
| 607 | /* Don't set delay in DSA mode */ |
| 608 | if (!dsa_is_dsa_port(priv->ds, 5) && |
| 609 | (interface == PHY_INTERFACE_MODE_RGMII_TXID || |
| 610 | interface == PHY_INTERFACE_MODE_RGMII_ID)) |
| 611 | tx_delay = 4; /* n * 0.5 ns */ |
| 612 | |
| 613 | /* P5 RGMII TX Clock Control: delay x */ |
| 614 | mt7530_write(priv, MT7530_P5RGMIITXCR, |
| 615 | CSR_RGMII_TXC_CFG(0x10 + tx_delay)); |
| 616 | |
| 617 | /* reduce P5 RGMII Tx driving, 8mA */ |
| 618 | mt7530_write(priv, MT7530_IO_DRV_CR, |
| 619 | P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1)); |
| 620 | } |
| 621 | |
| 622 | mt7530_write(priv, MT7530_MHWTRAP, val); |
| 623 | |
| 624 | dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n", |
| 625 | val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface)); |
| 626 | |
| 627 | priv->p5_interface = interface; |
| 628 | |
| 629 | unlock_exit: |
| 630 | mutex_unlock(&priv->reg_mutex); |
| 631 | } |
| 632 | |
| 633 | static int |
| 634 | mt7530_cpu_port_enable(struct mt7530_priv *priv, |
| 635 | int port) |
| 636 | { |
| 637 | /* Enable Mediatek header mode on the cpu port */ |
| 638 | mt7530_write(priv, MT7530_PVC_P(port), |
| 639 | PORT_SPEC_TAG); |
| 640 | |
| 641 | /* Unknown multicast frame forwarding to the cpu port */ |
| 642 | mt7530_rmw(priv, MT7530_MFC, UNM_FFP_MASK, UNM_FFP(BIT(port))); |
| 643 | |
| 644 | /* Set CPU port number */ |
| 645 | if (priv->id == ID_MT7530 || priv->id == ID_MT7621) |
| 646 | mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port)); |
| 647 | |
| 648 | /* CPU port gets connected to all user ports of |
| 649 | * the switch |
| 650 | */ |
| 651 | mt7530_write(priv, MT7530_PCR_P(port), |
| 652 | PCR_MATRIX(dsa_user_ports(priv->ds))); |
| 653 | |
| 654 | return 0; |
| 655 | } |
| 656 | |
| 657 | static int |
| 658 | mt7530_port_enable(struct dsa_switch *ds, int port, |
| 659 | struct phy_device *phy) |
| 660 | { |
| 661 | struct mt7530_priv *priv = ds->priv; |
| 662 | |
| 663 | if (!dsa_is_user_port(ds, port)) |
| 664 | return 0; |
| 665 | |
| 666 | mutex_lock(&priv->reg_mutex); |
| 667 | |
| 668 | /* Allow the user port gets connected to the cpu port and also |
| 669 | * restore the port matrix if the port is the member of a certain |
| 670 | * bridge. |
| 671 | */ |
| 672 | priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT)); |
| 673 | priv->ports[port].enable = true; |
| 674 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, |
| 675 | priv->ports[port].pm); |
| 676 | mt7530_port_set_status(priv, port, 0); |
| 677 | |
| 678 | mutex_unlock(&priv->reg_mutex); |
| 679 | |
| 680 | return 0; |
| 681 | } |
| 682 | |
| 683 | static void |
| 684 | mt7530_port_disable(struct dsa_switch *ds, int port) |
| 685 | { |
| 686 | struct mt7530_priv *priv = ds->priv; |
| 687 | |
| 688 | if (!dsa_is_user_port(ds, port)) |
| 689 | return; |
| 690 | |
| 691 | mutex_lock(&priv->reg_mutex); |
| 692 | |
| 693 | /* Clear up all port matrix which could be restored in the next |
| 694 | * enablement for the port. |
| 695 | */ |
| 696 | priv->ports[port].enable = false; |
| 697 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, |
| 698 | PCR_MATRIX_CLR); |
| 699 | mt7530_port_set_status(priv, port, 0); |
| 700 | |
| 701 | mutex_unlock(&priv->reg_mutex); |
| 702 | } |
| 703 | |
| 704 | static void |
| 705 | mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state) |
| 706 | { |
| 707 | struct mt7530_priv *priv = ds->priv; |
| 708 | u32 stp_state; |
| 709 | |
| 710 | switch (state) { |
| 711 | case BR_STATE_DISABLED: |
| 712 | stp_state = MT7530_STP_DISABLED; |
| 713 | break; |
| 714 | case BR_STATE_BLOCKING: |
| 715 | stp_state = MT7530_STP_BLOCKING; |
| 716 | break; |
| 717 | case BR_STATE_LISTENING: |
| 718 | stp_state = MT7530_STP_LISTENING; |
| 719 | break; |
| 720 | case BR_STATE_LEARNING: |
| 721 | stp_state = MT7530_STP_LEARNING; |
| 722 | break; |
| 723 | case BR_STATE_FORWARDING: |
| 724 | default: |
| 725 | stp_state = MT7530_STP_FORWARDING; |
| 726 | break; |
| 727 | } |
| 728 | |
| 729 | mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state); |
| 730 | } |
| 731 | |
| 732 | static int |
| 733 | mt7530_port_bridge_join(struct dsa_switch *ds, int port, |
| 734 | struct net_device *bridge) |
| 735 | { |
| 736 | struct mt7530_priv *priv = ds->priv; |
| 737 | u32 port_bitmap = BIT(MT7530_CPU_PORT); |
| 738 | int i; |
| 739 | |
| 740 | mutex_lock(&priv->reg_mutex); |
| 741 | |
| 742 | for (i = 0; i < MT7530_NUM_PORTS; i++) { |
| 743 | /* Add this port to the port matrix of the other ports in the |
| 744 | * same bridge. If the port is disabled, port matrix is kept |
| 745 | * and not being setup until the port becomes enabled. |
| 746 | */ |
| 747 | if (dsa_is_user_port(ds, i) && i != port) { |
| 748 | if (dsa_to_port(ds, i)->bridge_dev != bridge) |
| 749 | continue; |
| 750 | if (priv->ports[i].enable) |
| 751 | mt7530_set(priv, MT7530_PCR_P(i), |
| 752 | PCR_MATRIX(BIT(port))); |
| 753 | priv->ports[i].pm |= PCR_MATRIX(BIT(port)); |
| 754 | |
| 755 | port_bitmap |= BIT(i); |
| 756 | } |
| 757 | } |
| 758 | |
| 759 | /* Add the all other ports to this port matrix. */ |
| 760 | if (priv->ports[port].enable) |
| 761 | mt7530_rmw(priv, MT7530_PCR_P(port), |
| 762 | PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap)); |
| 763 | priv->ports[port].pm |= PCR_MATRIX(port_bitmap); |
| 764 | |
| 765 | mutex_unlock(&priv->reg_mutex); |
| 766 | |
| 767 | return 0; |
| 768 | } |
| 769 | |
| 770 | static void |
| 771 | mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port) |
| 772 | { |
| 773 | struct mt7530_priv *priv = ds->priv; |
| 774 | bool all_user_ports_removed = true; |
| 775 | int i; |
| 776 | |
| 777 | /* When a port is removed from the bridge, the port would be set up |
| 778 | * back to the default as is at initial boot which is a VLAN-unaware |
| 779 | * port. |
| 780 | */ |
| 781 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, |
| 782 | MT7530_PORT_MATRIX_MODE); |
| 783 | mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK, |
| 784 | VLAN_ATTR(MT7530_VLAN_TRANSPARENT) | |
| 785 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); |
| 786 | |
| 787 | for (i = 0; i < MT7530_NUM_PORTS; i++) { |
| 788 | if (dsa_is_user_port(ds, i) && |
| 789 | dsa_port_is_vlan_filtering(&ds->ports[i])) { |
| 790 | all_user_ports_removed = false; |
| 791 | break; |
| 792 | } |
| 793 | } |
| 794 | |
| 795 | /* CPU port also does the same thing until all user ports belonging to |
| 796 | * the CPU port get out of VLAN filtering mode. |
| 797 | */ |
| 798 | if (all_user_ports_removed) { |
| 799 | mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT), |
| 800 | PCR_MATRIX(dsa_user_ports(priv->ds))); |
| 801 | mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG |
| 802 | | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); |
| 803 | } |
| 804 | } |
| 805 | |
| 806 | static void |
| 807 | mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port) |
| 808 | { |
| 809 | struct mt7530_priv *priv = ds->priv; |
| 810 | |
| 811 | /* Trapped into security mode allows packet forwarding through VLAN |
| 812 | * table lookup. CPU port is set to fallback mode to let untagged |
| 813 | * frames pass through. |
| 814 | */ |
| 815 | if (dsa_is_cpu_port(ds, port)) |
| 816 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, |
| 817 | MT7530_PORT_FALLBACK_MODE); |
| 818 | else |
| 819 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, |
| 820 | MT7530_PORT_SECURITY_MODE); |
| 821 | |
| 822 | /* Set the port as a user port which is to be able to recognize VID |
| 823 | * from incoming packets before fetching entry within the VLAN table. |
| 824 | */ |
| 825 | mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK, |
| 826 | VLAN_ATTR(MT7530_VLAN_USER) | |
| 827 | PVC_EG_TAG(MT7530_VLAN_EG_DISABLED)); |
| 828 | } |
| 829 | |
| 830 | static void |
| 831 | mt7530_port_bridge_leave(struct dsa_switch *ds, int port, |
| 832 | struct net_device *bridge) |
| 833 | { |
| 834 | struct mt7530_priv *priv = ds->priv; |
| 835 | int i; |
| 836 | |
| 837 | mutex_lock(&priv->reg_mutex); |
| 838 | |
| 839 | for (i = 0; i < MT7530_NUM_PORTS; i++) { |
| 840 | /* Remove this port from the port matrix of the other ports |
| 841 | * in the same bridge. If the port is disabled, port matrix |
| 842 | * is kept and not being setup until the port becomes enabled. |
| 843 | */ |
| 844 | if (dsa_is_user_port(ds, i) && i != port) { |
| 845 | if (dsa_to_port(ds, i)->bridge_dev != bridge) |
| 846 | continue; |
| 847 | if (priv->ports[i].enable) |
| 848 | mt7530_clear(priv, MT7530_PCR_P(i), |
| 849 | PCR_MATRIX(BIT(port))); |
| 850 | priv->ports[i].pm &= ~PCR_MATRIX(BIT(port)); |
| 851 | } |
| 852 | } |
| 853 | |
| 854 | /* Set the cpu port to be the only one in the port matrix of |
| 855 | * this port. |
| 856 | */ |
| 857 | if (priv->ports[port].enable) |
| 858 | mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, |
| 859 | PCR_MATRIX(BIT(MT7530_CPU_PORT))); |
| 860 | priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT)); |
| 861 | |
| 862 | mutex_unlock(&priv->reg_mutex); |
| 863 | } |
| 864 | |
| 865 | static int |
| 866 | mt7530_port_fdb_add(struct dsa_switch *ds, int port, |
| 867 | const unsigned char *addr, u16 vid) |
| 868 | { |
| 869 | struct mt7530_priv *priv = ds->priv; |
| 870 | int ret; |
| 871 | u8 port_mask = BIT(port); |
| 872 | |
| 873 | mutex_lock(&priv->reg_mutex); |
| 874 | mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT); |
| 875 | ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); |
| 876 | mutex_unlock(&priv->reg_mutex); |
| 877 | |
| 878 | return ret; |
| 879 | } |
| 880 | |
| 881 | static int |
| 882 | mt7530_port_fdb_del(struct dsa_switch *ds, int port, |
| 883 | const unsigned char *addr, u16 vid) |
| 884 | { |
| 885 | struct mt7530_priv *priv = ds->priv; |
| 886 | int ret; |
| 887 | u8 port_mask = BIT(port); |
| 888 | |
| 889 | mutex_lock(&priv->reg_mutex); |
| 890 | mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP); |
| 891 | ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); |
| 892 | mutex_unlock(&priv->reg_mutex); |
| 893 | |
| 894 | return ret; |
| 895 | } |
| 896 | |
| 897 | static int |
| 898 | mt7530_port_fdb_dump(struct dsa_switch *ds, int port, |
| 899 | dsa_fdb_dump_cb_t *cb, void *data) |
| 900 | { |
| 901 | struct mt7530_priv *priv = ds->priv; |
| 902 | struct mt7530_fdb _fdb = { 0 }; |
| 903 | int cnt = MT7530_NUM_FDB_RECORDS; |
| 904 | int ret = 0; |
| 905 | u32 rsp = 0; |
| 906 | |
| 907 | mutex_lock(&priv->reg_mutex); |
| 908 | |
| 909 | ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp); |
| 910 | if (ret < 0) |
| 911 | goto err; |
| 912 | |
| 913 | do { |
| 914 | if (rsp & ATC_SRCH_HIT) { |
| 915 | mt7530_fdb_read(priv, &_fdb); |
| 916 | if (_fdb.port_mask & BIT(port)) { |
| 917 | ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp, |
| 918 | data); |
| 919 | if (ret < 0) |
| 920 | break; |
| 921 | } |
| 922 | } |
| 923 | } while (--cnt && |
| 924 | !(rsp & ATC_SRCH_END) && |
| 925 | !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp)); |
| 926 | err: |
| 927 | mutex_unlock(&priv->reg_mutex); |
| 928 | |
| 929 | return 0; |
| 930 | } |
| 931 | |
| 932 | static int |
| 933 | mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid) |
| 934 | { |
| 935 | struct mt7530_dummy_poll p; |
| 936 | u32 val; |
| 937 | int ret; |
| 938 | |
| 939 | val = VTCR_BUSY | VTCR_FUNC(cmd) | vid; |
| 940 | mt7530_write(priv, MT7530_VTCR, val); |
| 941 | |
| 942 | INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR); |
| 943 | ret = readx_poll_timeout(_mt7530_read, &p, val, |
| 944 | !(val & VTCR_BUSY), 20, 20000); |
| 945 | if (ret < 0) { |
| 946 | dev_err(priv->dev, "poll timeout\n"); |
| 947 | return ret; |
| 948 | } |
| 949 | |
| 950 | val = mt7530_read(priv, MT7530_VTCR); |
| 951 | if (val & VTCR_INVALID) { |
| 952 | dev_err(priv->dev, "read VTCR invalid\n"); |
| 953 | return -EINVAL; |
| 954 | } |
| 955 | |
| 956 | return 0; |
| 957 | } |
| 958 | |
| 959 | static int |
| 960 | mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, |
| 961 | bool vlan_filtering) |
| 962 | { |
| 963 | if (vlan_filtering) { |
| 964 | /* The port is being kept as VLAN-unaware port when bridge is |
| 965 | * set up with vlan_filtering not being set, Otherwise, the |
| 966 | * port and the corresponding CPU port is required the setup |
| 967 | * for becoming a VLAN-aware port. |
| 968 | */ |
| 969 | mt7530_port_set_vlan_aware(ds, port); |
| 970 | mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT); |
| 971 | } else { |
| 972 | mt7530_port_set_vlan_unaware(ds, port); |
| 973 | } |
| 974 | |
| 975 | return 0; |
| 976 | } |
| 977 | |
| 978 | static int |
| 979 | mt7530_port_vlan_prepare(struct dsa_switch *ds, int port, |
| 980 | const struct switchdev_obj_port_vlan *vlan) |
| 981 | { |
| 982 | /* nothing needed */ |
| 983 | |
| 984 | return 0; |
| 985 | } |
| 986 | |
| 987 | static void |
| 988 | mt7530_hw_vlan_add(struct mt7530_priv *priv, |
| 989 | struct mt7530_hw_vlan_entry *entry) |
| 990 | { |
| 991 | u8 new_members; |
| 992 | u32 val; |
| 993 | |
| 994 | new_members = entry->old_members | BIT(entry->port) | |
| 995 | BIT(MT7530_CPU_PORT); |
| 996 | |
| 997 | /* Validate the entry with independent learning, create egress tag per |
| 998 | * VLAN and joining the port as one of the port members. |
| 999 | */ |
| 1000 | val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID; |
| 1001 | mt7530_write(priv, MT7530_VAWD1, val); |
| 1002 | |
| 1003 | /* Decide whether adding tag or not for those outgoing packets from the |
| 1004 | * port inside the VLAN. |
| 1005 | */ |
| 1006 | val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG : |
| 1007 | MT7530_VLAN_EGRESS_TAG; |
| 1008 | mt7530_rmw(priv, MT7530_VAWD2, |
| 1009 | ETAG_CTRL_P_MASK(entry->port), |
| 1010 | ETAG_CTRL_P(entry->port, val)); |
| 1011 | |
| 1012 | /* CPU port is always taken as a tagged port for serving more than one |
| 1013 | * VLANs across and also being applied with egress type stack mode for |
| 1014 | * that VLAN tags would be appended after hardware special tag used as |
| 1015 | * DSA tag. |
| 1016 | */ |
| 1017 | mt7530_rmw(priv, MT7530_VAWD2, |
| 1018 | ETAG_CTRL_P_MASK(MT7530_CPU_PORT), |
| 1019 | ETAG_CTRL_P(MT7530_CPU_PORT, |
| 1020 | MT7530_VLAN_EGRESS_STACK)); |
| 1021 | } |
| 1022 | |
| 1023 | static void |
| 1024 | mt7530_hw_vlan_del(struct mt7530_priv *priv, |
| 1025 | struct mt7530_hw_vlan_entry *entry) |
| 1026 | { |
| 1027 | u8 new_members; |
| 1028 | u32 val; |
| 1029 | |
| 1030 | new_members = entry->old_members & ~BIT(entry->port); |
| 1031 | |
| 1032 | val = mt7530_read(priv, MT7530_VAWD1); |
| 1033 | if (!(val & VLAN_VALID)) { |
| 1034 | dev_err(priv->dev, |
| 1035 | "Cannot be deleted due to invalid entry\n"); |
| 1036 | return; |
| 1037 | } |
| 1038 | |
| 1039 | /* If certain member apart from CPU port is still alive in the VLAN, |
| 1040 | * the entry would be kept valid. Otherwise, the entry is got to be |
| 1041 | * disabled. |
| 1042 | */ |
| 1043 | if (new_members && new_members != BIT(MT7530_CPU_PORT)) { |
| 1044 | val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | |
| 1045 | VLAN_VALID; |
| 1046 | mt7530_write(priv, MT7530_VAWD1, val); |
| 1047 | } else { |
| 1048 | mt7530_write(priv, MT7530_VAWD1, 0); |
| 1049 | mt7530_write(priv, MT7530_VAWD2, 0); |
| 1050 | } |
| 1051 | } |
| 1052 | |
| 1053 | static void |
| 1054 | mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid, |
| 1055 | struct mt7530_hw_vlan_entry *entry, |
| 1056 | mt7530_vlan_op vlan_op) |
| 1057 | { |
| 1058 | u32 val; |
| 1059 | |
| 1060 | /* Fetch entry */ |
| 1061 | mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid); |
| 1062 | |
| 1063 | val = mt7530_read(priv, MT7530_VAWD1); |
| 1064 | |
| 1065 | entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK; |
| 1066 | |
| 1067 | /* Manipulate entry */ |
| 1068 | vlan_op(priv, entry); |
| 1069 | |
| 1070 | /* Flush result to hardware */ |
| 1071 | mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid); |
| 1072 | } |
| 1073 | |
| 1074 | static void |
| 1075 | mt7530_port_vlan_add(struct dsa_switch *ds, int port, |
| 1076 | const struct switchdev_obj_port_vlan *vlan) |
| 1077 | { |
| 1078 | bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; |
| 1079 | bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; |
| 1080 | struct mt7530_hw_vlan_entry new_entry; |
| 1081 | struct mt7530_priv *priv = ds->priv; |
| 1082 | u16 vid; |
| 1083 | |
| 1084 | mutex_lock(&priv->reg_mutex); |
| 1085 | |
| 1086 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 1087 | mt7530_hw_vlan_entry_init(&new_entry, port, untagged); |
| 1088 | mt7530_hw_vlan_update(priv, vid, &new_entry, |
| 1089 | mt7530_hw_vlan_add); |
| 1090 | } |
| 1091 | |
| 1092 | if (pvid) { |
| 1093 | mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, |
| 1094 | G0_PORT_VID(vlan->vid_end)); |
| 1095 | priv->ports[port].pvid = vlan->vid_end; |
| 1096 | } |
| 1097 | |
| 1098 | mutex_unlock(&priv->reg_mutex); |
| 1099 | } |
| 1100 | |
| 1101 | static int |
| 1102 | mt7530_port_vlan_del(struct dsa_switch *ds, int port, |
| 1103 | const struct switchdev_obj_port_vlan *vlan) |
| 1104 | { |
| 1105 | struct mt7530_hw_vlan_entry target_entry; |
| 1106 | struct mt7530_priv *priv = ds->priv; |
| 1107 | u16 vid, pvid; |
| 1108 | |
| 1109 | mutex_lock(&priv->reg_mutex); |
| 1110 | |
| 1111 | pvid = priv->ports[port].pvid; |
| 1112 | for (vid = vlan->vid_begin; vid <= vlan->vid_end; ++vid) { |
| 1113 | mt7530_hw_vlan_entry_init(&target_entry, port, 0); |
| 1114 | mt7530_hw_vlan_update(priv, vid, &target_entry, |
| 1115 | mt7530_hw_vlan_del); |
| 1116 | |
| 1117 | /* PVID is being restored to the default whenever the PVID port |
| 1118 | * is being removed from the VLAN. |
| 1119 | */ |
| 1120 | if (pvid == vid) |
| 1121 | pvid = G0_PORT_VID_DEF; |
| 1122 | } |
| 1123 | |
| 1124 | mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid); |
| 1125 | priv->ports[port].pvid = pvid; |
| 1126 | |
| 1127 | mutex_unlock(&priv->reg_mutex); |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | static int mt7530_port_mirror_add(struct dsa_switch *ds, int port, |
| 1133 | struct dsa_mall_mirror_tc_entry *mirror, |
| 1134 | bool ingress) |
| 1135 | { |
| 1136 | struct mt7530_priv *priv = ds->priv; |
| 1137 | u32 val; |
| 1138 | |
| 1139 | /* Check for existent entry */ |
| 1140 | if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port)) |
| 1141 | return -EEXIST; |
| 1142 | |
| 1143 | val = mt7530_read(priv, MT7530_MFC); |
| 1144 | |
| 1145 | /* MT7530 only supports one monitor port */ |
| 1146 | if (val & MIRROR_EN && MIRROR_PORT(val) != mirror->to_local_port) |
| 1147 | return -EEXIST; |
| 1148 | |
| 1149 | val |= MIRROR_EN; |
| 1150 | val &= ~MIRROR_MASK; |
| 1151 | val |= mirror->to_local_port; |
| 1152 | mt7530_write(priv, MT7530_MFC, val); |
| 1153 | |
| 1154 | val = mt7530_read(priv, MT7530_PCR_P(port)); |
| 1155 | if (ingress) { |
| 1156 | val |= PORT_RX_MIR; |
| 1157 | priv->mirror_rx |= BIT(port); |
| 1158 | } else { |
| 1159 | val |= PORT_TX_MIR; |
| 1160 | priv->mirror_tx |= BIT(port); |
| 1161 | } |
| 1162 | mt7530_write(priv, MT7530_PCR_P(port), val); |
| 1163 | |
| 1164 | return 0; |
| 1165 | } |
| 1166 | |
| 1167 | static void mt7530_port_mirror_del(struct dsa_switch *ds, int port, |
| 1168 | struct dsa_mall_mirror_tc_entry *mirror) |
| 1169 | { |
| 1170 | struct mt7530_priv *priv = ds->priv; |
| 1171 | u32 val; |
| 1172 | |
| 1173 | val = mt7530_read(priv, MT7530_PCR_P(port)); |
| 1174 | if (mirror->ingress) { |
| 1175 | val &= ~PORT_RX_MIR; |
| 1176 | priv->mirror_rx &= ~BIT(port); |
| 1177 | } else { |
| 1178 | val &= ~PORT_TX_MIR; |
| 1179 | priv->mirror_tx &= ~BIT(port); |
| 1180 | } |
| 1181 | mt7530_write(priv, MT7530_PCR_P(port), val); |
| 1182 | |
| 1183 | if (!priv->mirror_rx && !priv->mirror_tx) { |
| 1184 | val = mt7530_read(priv, MT7530_MFC); |
| 1185 | val &= ~MIRROR_EN; |
| 1186 | mt7530_write(priv, MT7530_MFC, val); |
| 1187 | } |
| 1188 | } |
| 1189 | |
| 1190 | static enum dsa_tag_protocol |
| 1191 | mtk_get_tag_protocol(struct dsa_switch *ds, int port, |
| 1192 | enum dsa_tag_protocol mp) |
| 1193 | { |
| 1194 | struct mt7530_priv *priv = ds->priv; |
| 1195 | |
| 1196 | if (port != MT7530_CPU_PORT) { |
| 1197 | dev_warn(priv->dev, |
| 1198 | "port not matched with tagging CPU port\n"); |
| 1199 | return DSA_TAG_PROTO_NONE; |
| 1200 | } else { |
| 1201 | return DSA_TAG_PROTO_MTK; |
| 1202 | } |
| 1203 | } |
| 1204 | |
| 1205 | static int |
| 1206 | mt7530_setup(struct dsa_switch *ds) |
| 1207 | { |
| 1208 | struct mt7530_priv *priv = ds->priv; |
| 1209 | struct device_node *phy_node; |
| 1210 | struct device_node *mac_np; |
| 1211 | struct mt7530_dummy_poll p; |
| 1212 | phy_interface_t interface; |
| 1213 | struct device_node *dn; |
| 1214 | u32 id, val; |
| 1215 | int ret, i; |
| 1216 | |
| 1217 | /* The parent node of master netdev which holds the common system |
| 1218 | * controller also is the container for two GMACs nodes representing |
| 1219 | * as two netdev instances. |
| 1220 | */ |
| 1221 | dn = ds->ports[MT7530_CPU_PORT].master->dev.of_node->parent; |
| 1222 | ds->configure_vlan_while_not_filtering = true; |
| 1223 | |
| 1224 | if (priv->id == ID_MT7530) { |
| 1225 | regulator_set_voltage(priv->core_pwr, 1000000, 1000000); |
| 1226 | ret = regulator_enable(priv->core_pwr); |
| 1227 | if (ret < 0) { |
| 1228 | dev_err(priv->dev, |
| 1229 | "Failed to enable core power: %d\n", ret); |
| 1230 | return ret; |
| 1231 | } |
| 1232 | |
| 1233 | regulator_set_voltage(priv->io_pwr, 3300000, 3300000); |
| 1234 | ret = regulator_enable(priv->io_pwr); |
| 1235 | if (ret < 0) { |
| 1236 | dev_err(priv->dev, "Failed to enable io pwr: %d\n", |
| 1237 | ret); |
| 1238 | return ret; |
| 1239 | } |
| 1240 | } |
| 1241 | |
| 1242 | /* Reset whole chip through gpio pin or memory-mapped registers for |
| 1243 | * different type of hardware |
| 1244 | */ |
| 1245 | if (priv->mcm) { |
| 1246 | reset_control_assert(priv->rstc); |
| 1247 | usleep_range(1000, 1100); |
| 1248 | reset_control_deassert(priv->rstc); |
| 1249 | } else { |
| 1250 | gpiod_set_value_cansleep(priv->reset, 0); |
| 1251 | usleep_range(1000, 1100); |
| 1252 | gpiod_set_value_cansleep(priv->reset, 1); |
| 1253 | } |
| 1254 | |
| 1255 | /* Waiting for MT7530 got to stable */ |
| 1256 | INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP); |
| 1257 | ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0, |
| 1258 | 20, 1000000); |
| 1259 | if (ret < 0) { |
| 1260 | dev_err(priv->dev, "reset timeout\n"); |
| 1261 | return ret; |
| 1262 | } |
| 1263 | |
| 1264 | id = mt7530_read(priv, MT7530_CREV); |
| 1265 | id >>= CHIP_NAME_SHIFT; |
| 1266 | if (id != MT7530_ID) { |
| 1267 | dev_err(priv->dev, "chip %x can't be supported\n", id); |
| 1268 | return -ENODEV; |
| 1269 | } |
| 1270 | |
| 1271 | /* Reset the switch through internal reset */ |
| 1272 | mt7530_write(priv, MT7530_SYS_CTRL, |
| 1273 | SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST | |
| 1274 | SYS_CTRL_REG_RST); |
| 1275 | |
| 1276 | /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */ |
| 1277 | val = mt7530_read(priv, MT7530_MHWTRAP); |
| 1278 | val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS; |
| 1279 | val |= MHWTRAP_MANUAL; |
| 1280 | mt7530_write(priv, MT7530_MHWTRAP, val); |
| 1281 | |
| 1282 | priv->p6_interface = PHY_INTERFACE_MODE_NA; |
| 1283 | |
| 1284 | /* Enable and reset MIB counters */ |
| 1285 | mt7530_mib_reset(ds); |
| 1286 | |
| 1287 | for (i = 0; i < MT7530_NUM_PORTS; i++) { |
| 1288 | /* Disable forwarding by default on all ports */ |
| 1289 | mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK, |
| 1290 | PCR_MATRIX_CLR); |
| 1291 | |
| 1292 | if (dsa_is_cpu_port(ds, i)) |
| 1293 | mt7530_cpu_port_enable(priv, i); |
| 1294 | else |
| 1295 | mt7530_port_disable(ds, i); |
| 1296 | |
| 1297 | /* Enable consistent egress tag */ |
| 1298 | mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK, |
| 1299 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); |
| 1300 | } |
| 1301 | |
| 1302 | /* Setup port 5 */ |
| 1303 | priv->p5_intf_sel = P5_DISABLED; |
| 1304 | interface = PHY_INTERFACE_MODE_NA; |
| 1305 | |
| 1306 | if (!dsa_is_unused_port(ds, 5)) { |
| 1307 | priv->p5_intf_sel = P5_INTF_SEL_GMAC5; |
| 1308 | interface = of_get_phy_mode(ds->ports[5].dn); |
| 1309 | } else { |
| 1310 | /* Scan the ethernet nodes. look for GMAC1, lookup used phy */ |
| 1311 | for_each_child_of_node(dn, mac_np) { |
| 1312 | if (!of_device_is_compatible(mac_np, |
| 1313 | "mediatek,eth-mac")) |
| 1314 | continue; |
| 1315 | |
| 1316 | ret = of_property_read_u32(mac_np, "reg", &id); |
| 1317 | if (ret < 0 || id != 1) |
| 1318 | continue; |
| 1319 | |
| 1320 | phy_node = of_parse_phandle(mac_np, "phy-handle", 0); |
| 1321 | if (!phy_node) |
| 1322 | continue; |
| 1323 | |
| 1324 | if (phy_node->parent == priv->dev->of_node->parent) { |
| 1325 | interface = of_get_phy_mode(mac_np); |
| 1326 | id = of_mdio_parse_addr(ds->dev, phy_node); |
| 1327 | if (id == 0) |
| 1328 | priv->p5_intf_sel = P5_INTF_SEL_PHY_P0; |
| 1329 | if (id == 4) |
| 1330 | priv->p5_intf_sel = P5_INTF_SEL_PHY_P4; |
| 1331 | } |
| 1332 | of_node_put(phy_node); |
| 1333 | break; |
| 1334 | } |
| 1335 | } |
| 1336 | |
| 1337 | mt7530_setup_port5(ds, interface); |
| 1338 | |
| 1339 | /* Flush the FDB table */ |
| 1340 | ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); |
| 1341 | if (ret < 0) |
| 1342 | return ret; |
| 1343 | |
| 1344 | return 0; |
| 1345 | } |
| 1346 | |
| 1347 | static void mt7530_phylink_mac_config(struct dsa_switch *ds, int port, |
| 1348 | unsigned int mode, |
| 1349 | const struct phylink_link_state *state) |
| 1350 | { |
| 1351 | struct mt7530_priv *priv = ds->priv; |
| 1352 | u32 mcr_cur, mcr_new; |
| 1353 | |
| 1354 | switch (port) { |
| 1355 | case 0: /* Internal phy */ |
| 1356 | case 1: |
| 1357 | case 2: |
| 1358 | case 3: |
| 1359 | case 4: |
| 1360 | if (state->interface != PHY_INTERFACE_MODE_GMII) |
| 1361 | return; |
| 1362 | break; |
| 1363 | case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */ |
| 1364 | if (priv->p5_interface == state->interface) |
| 1365 | break; |
| 1366 | if (!phy_interface_mode_is_rgmii(state->interface) && |
| 1367 | state->interface != PHY_INTERFACE_MODE_MII && |
| 1368 | state->interface != PHY_INTERFACE_MODE_GMII) |
| 1369 | return; |
| 1370 | |
| 1371 | mt7530_setup_port5(ds, state->interface); |
| 1372 | break; |
| 1373 | case 6: /* 1st cpu port */ |
| 1374 | if (priv->p6_interface == state->interface) |
| 1375 | break; |
| 1376 | |
| 1377 | if (state->interface != PHY_INTERFACE_MODE_RGMII && |
| 1378 | state->interface != PHY_INTERFACE_MODE_TRGMII) |
| 1379 | return; |
| 1380 | |
| 1381 | /* Setup TX circuit incluing relevant PAD and driving */ |
| 1382 | mt7530_pad_clk_setup(ds, state->interface); |
| 1383 | |
| 1384 | priv->p6_interface = state->interface; |
| 1385 | break; |
| 1386 | default: |
| 1387 | dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port); |
| 1388 | return; |
| 1389 | } |
| 1390 | |
| 1391 | if (phylink_autoneg_inband(mode)) { |
| 1392 | dev_err(ds->dev, "%s: in-band negotiation unsupported\n", |
| 1393 | __func__); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port)); |
| 1398 | mcr_new = mcr_cur; |
| 1399 | mcr_new &= ~(PMCR_FORCE_SPEED_1000 | PMCR_FORCE_SPEED_100 | |
| 1400 | PMCR_FORCE_FDX | PMCR_TX_FC_EN | PMCR_RX_FC_EN); |
| 1401 | mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN | |
| 1402 | PMCR_BACKPR_EN | PMCR_FORCE_MODE; |
| 1403 | |
| 1404 | /* Are we connected to external phy */ |
| 1405 | if (port == 5 && dsa_is_user_port(ds, 5)) |
| 1406 | mcr_new |= PMCR_EXT_PHY; |
| 1407 | |
| 1408 | switch (state->speed) { |
| 1409 | case SPEED_1000: |
| 1410 | mcr_new |= PMCR_FORCE_SPEED_1000; |
| 1411 | if (priv->eee_enable & BIT(port)) |
| 1412 | mcr_new |= PMCR_FORCE_EEE1G; |
| 1413 | break; |
| 1414 | case SPEED_100: |
| 1415 | mcr_new |= PMCR_FORCE_SPEED_100; |
| 1416 | if (priv->eee_enable & BIT(port)) |
| 1417 | mcr_new |= PMCR_FORCE_EEE100; |
| 1418 | break; |
| 1419 | } |
| 1420 | if (state->duplex == DUPLEX_FULL) { |
| 1421 | mcr_new |= PMCR_FORCE_FDX; |
| 1422 | if (state->pause & MLO_PAUSE_TX) |
| 1423 | mcr_new |= PMCR_TX_FC_EN; |
| 1424 | if (state->pause & MLO_PAUSE_RX) |
| 1425 | mcr_new |= PMCR_RX_FC_EN; |
| 1426 | } |
| 1427 | |
| 1428 | if (mcr_new != mcr_cur) |
| 1429 | mt7530_write(priv, MT7530_PMCR_P(port), mcr_new); |
| 1430 | } |
| 1431 | |
| 1432 | static void mt7530_phylink_mac_link_down(struct dsa_switch *ds, int port, |
| 1433 | unsigned int mode, |
| 1434 | phy_interface_t interface) |
| 1435 | { |
| 1436 | struct mt7530_priv *priv = ds->priv; |
| 1437 | |
| 1438 | mt7530_port_set_status(priv, port, 0); |
| 1439 | } |
| 1440 | |
| 1441 | static void mt7530_phylink_mac_link_up(struct dsa_switch *ds, int port, |
| 1442 | unsigned int mode, |
| 1443 | phy_interface_t interface, |
| 1444 | struct phy_device *phydev) |
| 1445 | { |
| 1446 | struct mt7530_priv *priv = ds->priv; |
| 1447 | |
| 1448 | mt7530_port_set_status(priv, port, 1); |
| 1449 | } |
| 1450 | |
| 1451 | static void mt7530_phylink_validate(struct dsa_switch *ds, int port, |
| 1452 | unsigned long *supported, |
| 1453 | struct phylink_link_state *state) |
| 1454 | { |
| 1455 | __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; |
| 1456 | |
| 1457 | switch (port) { |
| 1458 | case 0: /* Internal phy */ |
| 1459 | case 1: |
| 1460 | case 2: |
| 1461 | case 3: |
| 1462 | case 4: |
| 1463 | if (state->interface != PHY_INTERFACE_MODE_NA && |
| 1464 | state->interface != PHY_INTERFACE_MODE_GMII) |
| 1465 | goto unsupported; |
| 1466 | break; |
| 1467 | case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */ |
| 1468 | if (state->interface != PHY_INTERFACE_MODE_NA && |
| 1469 | !phy_interface_mode_is_rgmii(state->interface) && |
| 1470 | state->interface != PHY_INTERFACE_MODE_MII && |
| 1471 | state->interface != PHY_INTERFACE_MODE_GMII) |
| 1472 | goto unsupported; |
| 1473 | break; |
| 1474 | case 6: /* 1st cpu port */ |
| 1475 | if (state->interface != PHY_INTERFACE_MODE_NA && |
| 1476 | state->interface != PHY_INTERFACE_MODE_RGMII && |
| 1477 | state->interface != PHY_INTERFACE_MODE_TRGMII) |
| 1478 | goto unsupported; |
| 1479 | break; |
| 1480 | default: |
| 1481 | dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port); |
| 1482 | unsupported: |
| 1483 | linkmode_zero(supported); |
| 1484 | return; |
| 1485 | } |
| 1486 | |
| 1487 | phylink_set_port_modes(mask); |
| 1488 | phylink_set(mask, Autoneg); |
| 1489 | |
| 1490 | if (state->interface == PHY_INTERFACE_MODE_TRGMII) { |
| 1491 | phylink_set(mask, 1000baseT_Full); |
| 1492 | } else { |
| 1493 | phylink_set(mask, 10baseT_Half); |
| 1494 | phylink_set(mask, 10baseT_Full); |
| 1495 | phylink_set(mask, 100baseT_Half); |
| 1496 | phylink_set(mask, 100baseT_Full); |
| 1497 | |
| 1498 | if (state->interface != PHY_INTERFACE_MODE_MII) { |
| 1499 | /* This switch only supports 1G full-duplex. */ |
| 1500 | phylink_set(mask, 1000baseT_Full); |
| 1501 | if (port == 5) |
| 1502 | phylink_set(mask, 1000baseX_Full); |
| 1503 | } |
| 1504 | } |
| 1505 | |
| 1506 | phylink_set(mask, Pause); |
| 1507 | phylink_set(mask, Asym_Pause); |
| 1508 | |
| 1509 | linkmode_and(supported, supported, mask); |
| 1510 | linkmode_and(state->advertising, state->advertising, mask); |
| 1511 | } |
| 1512 | |
| 1513 | static int |
| 1514 | mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port, |
| 1515 | struct phylink_link_state *state) |
| 1516 | { |
| 1517 | struct mt7530_priv *priv = ds->priv; |
| 1518 | u32 pmsr; |
| 1519 | |
| 1520 | if (port < 0 || port >= MT7530_NUM_PORTS) |
| 1521 | return -EINVAL; |
| 1522 | |
| 1523 | pmsr = mt7530_read(priv, MT7530_PMSR_P(port)); |
| 1524 | |
| 1525 | state->link = (pmsr & PMSR_LINK); |
| 1526 | state->an_complete = state->link; |
| 1527 | state->duplex = !!(pmsr & PMSR_DPX); |
| 1528 | |
| 1529 | switch (pmsr & PMSR_SPEED_MASK) { |
| 1530 | case PMSR_SPEED_10: |
| 1531 | state->speed = SPEED_10; |
| 1532 | break; |
| 1533 | case PMSR_SPEED_100: |
| 1534 | state->speed = SPEED_100; |
| 1535 | break; |
| 1536 | case PMSR_SPEED_1000: |
| 1537 | state->speed = SPEED_1000; |
| 1538 | break; |
| 1539 | default: |
| 1540 | state->speed = SPEED_UNKNOWN; |
| 1541 | break; |
| 1542 | } |
| 1543 | |
| 1544 | state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX); |
| 1545 | if (pmsr & PMSR_RX_FC) |
| 1546 | state->pause |= MLO_PAUSE_RX; |
| 1547 | if (pmsr & PMSR_TX_FC) |
| 1548 | state->pause |= MLO_PAUSE_TX; |
| 1549 | |
| 1550 | return 1; |
| 1551 | } |
| 1552 | |
| 1553 | static int mt7530_get_mac_eee(struct dsa_switch *ds, int port, |
| 1554 | struct ethtool_eee *e) |
| 1555 | { |
| 1556 | struct mt7530_priv *priv = ds->priv; |
| 1557 | u32 eeecr, pmsr; |
| 1558 | |
| 1559 | e->eee_enabled = !!(priv->eee_enable & BIT(port)); |
| 1560 | |
| 1561 | if (e->eee_enabled) { |
| 1562 | eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port)); |
| 1563 | e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN); |
| 1564 | e->tx_lpi_timer = (eeecr >> 4) & 0xFFF; |
| 1565 | pmsr = mt7530_read(priv, MT7530_PMSR_P(port)); |
| 1566 | e->eee_active = e->eee_enabled && !!(pmsr & PMSR_EEE1G); |
| 1567 | } else { |
| 1568 | e->tx_lpi_enabled = 0; |
| 1569 | e->tx_lpi_timer = 0; |
| 1570 | e->eee_active = 0; |
| 1571 | } |
| 1572 | |
| 1573 | return 0; |
| 1574 | } |
| 1575 | |
| 1576 | static int mt7530_set_mac_eee(struct dsa_switch *ds, int port, |
| 1577 | struct ethtool_eee *e) |
| 1578 | { |
| 1579 | struct mt7530_priv *priv = ds->priv; |
| 1580 | u32 eeecr; |
| 1581 | |
| 1582 | if (e->tx_lpi_enabled && e->tx_lpi_timer > 0xFFF) |
| 1583 | return -EINVAL; |
| 1584 | |
| 1585 | if (e->eee_enabled) { |
| 1586 | priv->eee_enable |= BIT(port); |
| 1587 | //MT7530_PMEEECR_P |
| 1588 | eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port)); |
| 1589 | eeecr &= 0xFFFF0000; |
| 1590 | if (!e->tx_lpi_enabled) |
| 1591 | eeecr |= LPI_MODE_EN; |
| 1592 | eeecr |= LPI_THRESH(e->tx_lpi_timer); |
| 1593 | mt7530_write(priv, MT7530_PMEEECR_P(port), eeecr); |
| 1594 | } else { |
| 1595 | priv->eee_enable &= ~(BIT(port)); |
| 1596 | } |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
| 1601 | static const struct dsa_switch_ops mt7530_switch_ops = { |
| 1602 | .get_tag_protocol = mtk_get_tag_protocol, |
| 1603 | .setup = mt7530_setup, |
| 1604 | .get_strings = mt7530_get_strings, |
| 1605 | .phy_read = mt7530_phy_read, |
| 1606 | .phy_write = mt7530_phy_write, |
| 1607 | .get_ethtool_stats = mt7530_get_ethtool_stats, |
| 1608 | .get_sset_count = mt7530_get_sset_count, |
| 1609 | .port_enable = mt7530_port_enable, |
| 1610 | .port_disable = mt7530_port_disable, |
| 1611 | .port_stp_state_set = mt7530_stp_state_set, |
| 1612 | .port_bridge_join = mt7530_port_bridge_join, |
| 1613 | .port_bridge_leave = mt7530_port_bridge_leave, |
| 1614 | .port_fdb_add = mt7530_port_fdb_add, |
| 1615 | .port_fdb_del = mt7530_port_fdb_del, |
| 1616 | .port_fdb_dump = mt7530_port_fdb_dump, |
| 1617 | .port_vlan_filtering = mt7530_port_vlan_filtering, |
| 1618 | .port_vlan_prepare = mt7530_port_vlan_prepare, |
| 1619 | .port_vlan_add = mt7530_port_vlan_add, |
| 1620 | .port_vlan_del = mt7530_port_vlan_del, |
| 1621 | .port_mirror_add = mt7530_port_mirror_add, |
| 1622 | .port_mirror_del = mt7530_port_mirror_del, |
| 1623 | .phylink_validate = mt7530_phylink_validate, |
| 1624 | .phylink_mac_link_state = mt7530_phylink_mac_link_state, |
| 1625 | .phylink_mac_config = mt7530_phylink_mac_config, |
| 1626 | .phylink_mac_link_down = mt7530_phylink_mac_link_down, |
| 1627 | .phylink_mac_link_up = mt7530_phylink_mac_link_up, |
| 1628 | .get_mac_eee = mt7530_get_mac_eee, |
| 1629 | .set_mac_eee = mt7530_set_mac_eee, |
| 1630 | }; |
| 1631 | |
| 1632 | static const struct of_device_id mt7530_of_match[] = { |
| 1633 | { .compatible = "mediatek,mt7621", .data = (void *)ID_MT7621, }, |
| 1634 | { .compatible = "mediatek,mt7530", .data = (void *)ID_MT7530, }, |
| 1635 | { /* sentinel */ }, |
| 1636 | }; |
| 1637 | MODULE_DEVICE_TABLE(of, mt7530_of_match); |
| 1638 | |
| 1639 | static int |
| 1640 | mt7530_probe(struct mdio_device *mdiodev) |
| 1641 | { |
| 1642 | struct mt7530_priv *priv; |
| 1643 | struct device_node *dn; |
| 1644 | |
| 1645 | dn = mdiodev->dev.of_node; |
| 1646 | |
| 1647 | priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL); |
| 1648 | if (!priv) |
| 1649 | return -ENOMEM; |
| 1650 | |
| 1651 | priv->ds = dsa_switch_alloc(&mdiodev->dev, DSA_MAX_PORTS); |
| 1652 | if (!priv->ds) |
| 1653 | return -ENOMEM; |
| 1654 | |
| 1655 | /* Use medatek,mcm property to distinguish hardware type that would |
| 1656 | * casues a little bit differences on power-on sequence. |
| 1657 | */ |
| 1658 | priv->mcm = of_property_read_bool(dn, "mediatek,mcm"); |
| 1659 | if (priv->mcm) { |
| 1660 | dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n"); |
| 1661 | |
| 1662 | priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm"); |
| 1663 | if (IS_ERR(priv->rstc)) { |
| 1664 | dev_err(&mdiodev->dev, "Couldn't get our reset line\n"); |
| 1665 | return PTR_ERR(priv->rstc); |
| 1666 | } |
| 1667 | } |
| 1668 | |
| 1669 | /* Get the hardware identifier from the devicetree node. |
| 1670 | * We will need it for some of the clock and regulator setup. |
| 1671 | */ |
| 1672 | priv->id = (unsigned int)(unsigned long) |
| 1673 | of_device_get_match_data(&mdiodev->dev); |
| 1674 | |
| 1675 | if (priv->id == ID_MT7530) { |
| 1676 | priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core"); |
| 1677 | if (IS_ERR(priv->core_pwr)) |
| 1678 | return PTR_ERR(priv->core_pwr); |
| 1679 | |
| 1680 | priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io"); |
| 1681 | if (IS_ERR(priv->io_pwr)) |
| 1682 | return PTR_ERR(priv->io_pwr); |
| 1683 | } |
| 1684 | |
| 1685 | /* Not MCM that indicates switch works as the remote standalone |
| 1686 | * integrated circuit so the GPIO pin would be used to complete |
| 1687 | * the reset, otherwise memory-mapped register accessing used |
| 1688 | * through syscon provides in the case of MCM. |
| 1689 | */ |
| 1690 | if (!priv->mcm) { |
| 1691 | priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset", |
| 1692 | GPIOD_OUT_LOW); |
| 1693 | if (IS_ERR(priv->reset)) { |
| 1694 | dev_err(&mdiodev->dev, "Couldn't get our reset line\n"); |
| 1695 | return PTR_ERR(priv->reset); |
| 1696 | } |
| 1697 | } |
| 1698 | |
| 1699 | priv->bus = mdiodev->bus; |
| 1700 | priv->dev = &mdiodev->dev; |
| 1701 | priv->ds->priv = priv; |
| 1702 | priv->ds->ops = &mt7530_switch_ops; |
| 1703 | mutex_init(&priv->reg_mutex); |
| 1704 | dev_set_drvdata(&mdiodev->dev, priv); |
| 1705 | |
| 1706 | return dsa_register_switch(priv->ds); |
| 1707 | } |
| 1708 | |
| 1709 | static void |
| 1710 | mt7530_remove(struct mdio_device *mdiodev) |
| 1711 | { |
| 1712 | struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev); |
| 1713 | int ret = 0; |
| 1714 | |
| 1715 | ret = regulator_disable(priv->core_pwr); |
| 1716 | if (ret < 0) |
| 1717 | dev_err(priv->dev, |
| 1718 | "Failed to disable core power: %d\n", ret); |
| 1719 | |
| 1720 | ret = regulator_disable(priv->io_pwr); |
| 1721 | if (ret < 0) |
| 1722 | dev_err(priv->dev, "Failed to disable io pwr: %d\n", |
| 1723 | ret); |
| 1724 | |
| 1725 | dsa_unregister_switch(priv->ds); |
| 1726 | mutex_destroy(&priv->reg_mutex); |
| 1727 | } |
| 1728 | |
| 1729 | static struct mdio_driver mt7530_mdio_driver = { |
| 1730 | .probe = mt7530_probe, |
| 1731 | .remove = mt7530_remove, |
| 1732 | .mdiodrv.driver = { |
| 1733 | .name = "mt7530", |
| 1734 | .of_match_table = mt7530_of_match, |
| 1735 | }, |
| 1736 | }; |
| 1737 | |
| 1738 | mdio_module_driver(mt7530_mdio_driver); |
| 1739 | |
| 1740 | MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); |
| 1741 | MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch"); |
| 1742 | MODULE_LICENSE("GPL"); |