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_irq.h> 14 #include <linux/of_mdio.h> 15 #include <linux/of_net.h> 16 #include <linux/of_platform.h> 17 #include <linux/phylink.h> 18 #include <linux/regmap.h> 19 #include <linux/regulator/consumer.h> 20 #include <linux/reset.h> 21 #include <linux/gpio/consumer.h> 22 #include <linux/gpio/driver.h> 23 #include <net/dsa.h> 24 25 #include "mt7530.h" 26 27 /* String, offset, and register size in bytes if different from 4 bytes */ 28 static const struct mt7530_mib_desc mt7530_mib[] = { 29 MIB_DESC(1, 0x00, "TxDrop"), 30 MIB_DESC(1, 0x04, "TxCrcErr"), 31 MIB_DESC(1, 0x08, "TxUnicast"), 32 MIB_DESC(1, 0x0c, "TxMulticast"), 33 MIB_DESC(1, 0x10, "TxBroadcast"), 34 MIB_DESC(1, 0x14, "TxCollision"), 35 MIB_DESC(1, 0x18, "TxSingleCollision"), 36 MIB_DESC(1, 0x1c, "TxMultipleCollision"), 37 MIB_DESC(1, 0x20, "TxDeferred"), 38 MIB_DESC(1, 0x24, "TxLateCollision"), 39 MIB_DESC(1, 0x28, "TxExcessiveCollistion"), 40 MIB_DESC(1, 0x2c, "TxPause"), 41 MIB_DESC(1, 0x30, "TxPktSz64"), 42 MIB_DESC(1, 0x34, "TxPktSz65To127"), 43 MIB_DESC(1, 0x38, "TxPktSz128To255"), 44 MIB_DESC(1, 0x3c, "TxPktSz256To511"), 45 MIB_DESC(1, 0x40, "TxPktSz512To1023"), 46 MIB_DESC(1, 0x44, "Tx1024ToMax"), 47 MIB_DESC(2, 0x48, "TxBytes"), 48 MIB_DESC(1, 0x60, "RxDrop"), 49 MIB_DESC(1, 0x64, "RxFiltering"), 50 MIB_DESC(1, 0x6c, "RxMulticast"), 51 MIB_DESC(1, 0x70, "RxBroadcast"), 52 MIB_DESC(1, 0x74, "RxAlignErr"), 53 MIB_DESC(1, 0x78, "RxCrcErr"), 54 MIB_DESC(1, 0x7c, "RxUnderSizeErr"), 55 MIB_DESC(1, 0x80, "RxFragErr"), 56 MIB_DESC(1, 0x84, "RxOverSzErr"), 57 MIB_DESC(1, 0x88, "RxJabberErr"), 58 MIB_DESC(1, 0x8c, "RxPause"), 59 MIB_DESC(1, 0x90, "RxPktSz64"), 60 MIB_DESC(1, 0x94, "RxPktSz65To127"), 61 MIB_DESC(1, 0x98, "RxPktSz128To255"), 62 MIB_DESC(1, 0x9c, "RxPktSz256To511"), 63 MIB_DESC(1, 0xa0, "RxPktSz512To1023"), 64 MIB_DESC(1, 0xa4, "RxPktSz1024ToMax"), 65 MIB_DESC(2, 0xa8, "RxBytes"), 66 MIB_DESC(1, 0xb0, "RxCtrlDrop"), 67 MIB_DESC(1, 0xb4, "RxIngressDrop"), 68 MIB_DESC(1, 0xb8, "RxArlDrop"), 69 }; 70 71 /* Since phy_device has not yet been created and 72 * phy_{read,write}_mmd_indirect is not available, we provide our own 73 * core_{read,write}_mmd_indirect with core_{clear,write,set} wrappers 74 * to complete this function. 75 */ 76 static int 77 core_read_mmd_indirect(struct mt7530_priv *priv, int prtad, int devad) 78 { 79 struct mii_bus *bus = priv->bus; 80 int value, ret; 81 82 /* Write the desired MMD Devad */ 83 ret = bus->write(bus, 0, MII_MMD_CTRL, devad); 84 if (ret < 0) 85 goto err; 86 87 /* Write the desired MMD register address */ 88 ret = bus->write(bus, 0, MII_MMD_DATA, prtad); 89 if (ret < 0) 90 goto err; 91 92 /* Select the Function : DATA with no post increment */ 93 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR)); 94 if (ret < 0) 95 goto err; 96 97 /* Read the content of the MMD's selected register */ 98 value = bus->read(bus, 0, MII_MMD_DATA); 99 100 return value; 101 err: 102 dev_err(&bus->dev, "failed to read mmd register\n"); 103 104 return ret; 105 } 106 107 static int 108 core_write_mmd_indirect(struct mt7530_priv *priv, int prtad, 109 int devad, u32 data) 110 { 111 struct mii_bus *bus = priv->bus; 112 int ret; 113 114 /* Write the desired MMD Devad */ 115 ret = bus->write(bus, 0, MII_MMD_CTRL, devad); 116 if (ret < 0) 117 goto err; 118 119 /* Write the desired MMD register address */ 120 ret = bus->write(bus, 0, MII_MMD_DATA, prtad); 121 if (ret < 0) 122 goto err; 123 124 /* Select the Function : DATA with no post increment */ 125 ret = bus->write(bus, 0, MII_MMD_CTRL, (devad | MII_MMD_CTRL_NOINCR)); 126 if (ret < 0) 127 goto err; 128 129 /* Write the data into MMD's selected register */ 130 ret = bus->write(bus, 0, MII_MMD_DATA, data); 131 err: 132 if (ret < 0) 133 dev_err(&bus->dev, 134 "failed to write mmd register\n"); 135 return ret; 136 } 137 138 static void 139 core_write(struct mt7530_priv *priv, u32 reg, u32 val) 140 { 141 struct mii_bus *bus = priv->bus; 142 143 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 144 145 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val); 146 147 mutex_unlock(&bus->mdio_lock); 148 } 149 150 static void 151 core_rmw(struct mt7530_priv *priv, u32 reg, u32 mask, u32 set) 152 { 153 struct mii_bus *bus = priv->bus; 154 u32 val; 155 156 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 157 158 val = core_read_mmd_indirect(priv, reg, MDIO_MMD_VEND2); 159 val &= ~mask; 160 val |= set; 161 core_write_mmd_indirect(priv, reg, MDIO_MMD_VEND2, val); 162 163 mutex_unlock(&bus->mdio_lock); 164 } 165 166 static void 167 core_set(struct mt7530_priv *priv, u32 reg, u32 val) 168 { 169 core_rmw(priv, reg, 0, val); 170 } 171 172 static void 173 core_clear(struct mt7530_priv *priv, u32 reg, u32 val) 174 { 175 core_rmw(priv, reg, val, 0); 176 } 177 178 static int 179 mt7530_mii_write(struct mt7530_priv *priv, u32 reg, u32 val) 180 { 181 struct mii_bus *bus = priv->bus; 182 u16 page, r, lo, hi; 183 int ret; 184 185 page = (reg >> 6) & 0x3ff; 186 r = (reg >> 2) & 0xf; 187 lo = val & 0xffff; 188 hi = val >> 16; 189 190 /* MT7530 uses 31 as the pseudo port */ 191 ret = bus->write(bus, 0x1f, 0x1f, page); 192 if (ret < 0) 193 goto err; 194 195 ret = bus->write(bus, 0x1f, r, lo); 196 if (ret < 0) 197 goto err; 198 199 ret = bus->write(bus, 0x1f, 0x10, hi); 200 err: 201 if (ret < 0) 202 dev_err(&bus->dev, 203 "failed to write mt7530 register\n"); 204 return ret; 205 } 206 207 static u32 208 mt7530_mii_read(struct mt7530_priv *priv, u32 reg) 209 { 210 struct mii_bus *bus = priv->bus; 211 u16 page, r, lo, hi; 212 int ret; 213 214 page = (reg >> 6) & 0x3ff; 215 r = (reg >> 2) & 0xf; 216 217 /* MT7530 uses 31 as the pseudo port */ 218 ret = bus->write(bus, 0x1f, 0x1f, page); 219 if (ret < 0) { 220 dev_err(&bus->dev, 221 "failed to read mt7530 register\n"); 222 return ret; 223 } 224 225 lo = bus->read(bus, 0x1f, r); 226 hi = bus->read(bus, 0x1f, 0x10); 227 228 return (hi << 16) | (lo & 0xffff); 229 } 230 231 static void 232 mt7530_write(struct mt7530_priv *priv, u32 reg, u32 val) 233 { 234 struct mii_bus *bus = priv->bus; 235 236 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 237 238 mt7530_mii_write(priv, reg, val); 239 240 mutex_unlock(&bus->mdio_lock); 241 } 242 243 static u32 244 _mt7530_unlocked_read(struct mt7530_dummy_poll *p) 245 { 246 return mt7530_mii_read(p->priv, p->reg); 247 } 248 249 static u32 250 _mt7530_read(struct mt7530_dummy_poll *p) 251 { 252 struct mii_bus *bus = p->priv->bus; 253 u32 val; 254 255 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 256 257 val = mt7530_mii_read(p->priv, p->reg); 258 259 mutex_unlock(&bus->mdio_lock); 260 261 return val; 262 } 263 264 static u32 265 mt7530_read(struct mt7530_priv *priv, u32 reg) 266 { 267 struct mt7530_dummy_poll p; 268 269 INIT_MT7530_DUMMY_POLL(&p, priv, reg); 270 return _mt7530_read(&p); 271 } 272 273 static void 274 mt7530_rmw(struct mt7530_priv *priv, u32 reg, 275 u32 mask, u32 set) 276 { 277 struct mii_bus *bus = priv->bus; 278 u32 val; 279 280 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 281 282 val = mt7530_mii_read(priv, reg); 283 val &= ~mask; 284 val |= set; 285 mt7530_mii_write(priv, reg, val); 286 287 mutex_unlock(&bus->mdio_lock); 288 } 289 290 static void 291 mt7530_set(struct mt7530_priv *priv, u32 reg, u32 val) 292 { 293 mt7530_rmw(priv, reg, 0, val); 294 } 295 296 static void 297 mt7530_clear(struct mt7530_priv *priv, u32 reg, u32 val) 298 { 299 mt7530_rmw(priv, reg, val, 0); 300 } 301 302 static int 303 mt7530_fdb_cmd(struct mt7530_priv *priv, enum mt7530_fdb_cmd cmd, u32 *rsp) 304 { 305 u32 val; 306 int ret; 307 struct mt7530_dummy_poll p; 308 309 /* Set the command operating upon the MAC address entries */ 310 val = ATC_BUSY | ATC_MAT(0) | cmd; 311 mt7530_write(priv, MT7530_ATC, val); 312 313 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_ATC); 314 ret = readx_poll_timeout(_mt7530_read, &p, val, 315 !(val & ATC_BUSY), 20, 20000); 316 if (ret < 0) { 317 dev_err(priv->dev, "reset timeout\n"); 318 return ret; 319 } 320 321 /* Additional sanity for read command if the specified 322 * entry is invalid 323 */ 324 val = mt7530_read(priv, MT7530_ATC); 325 if ((cmd == MT7530_FDB_READ) && (val & ATC_INVALID)) 326 return -EINVAL; 327 328 if (rsp) 329 *rsp = val; 330 331 return 0; 332 } 333 334 static void 335 mt7530_fdb_read(struct mt7530_priv *priv, struct mt7530_fdb *fdb) 336 { 337 u32 reg[3]; 338 int i; 339 340 /* Read from ARL table into an array */ 341 for (i = 0; i < 3; i++) { 342 reg[i] = mt7530_read(priv, MT7530_TSRA1 + (i * 4)); 343 344 dev_dbg(priv->dev, "%s(%d) reg[%d]=0x%x\n", 345 __func__, __LINE__, i, reg[i]); 346 } 347 348 fdb->vid = (reg[1] >> CVID) & CVID_MASK; 349 fdb->aging = (reg[2] >> AGE_TIMER) & AGE_TIMER_MASK; 350 fdb->port_mask = (reg[2] >> PORT_MAP) & PORT_MAP_MASK; 351 fdb->mac[0] = (reg[0] >> MAC_BYTE_0) & MAC_BYTE_MASK; 352 fdb->mac[1] = (reg[0] >> MAC_BYTE_1) & MAC_BYTE_MASK; 353 fdb->mac[2] = (reg[0] >> MAC_BYTE_2) & MAC_BYTE_MASK; 354 fdb->mac[3] = (reg[0] >> MAC_BYTE_3) & MAC_BYTE_MASK; 355 fdb->mac[4] = (reg[1] >> MAC_BYTE_4) & MAC_BYTE_MASK; 356 fdb->mac[5] = (reg[1] >> MAC_BYTE_5) & MAC_BYTE_MASK; 357 fdb->noarp = ((reg[2] >> ENT_STATUS) & ENT_STATUS_MASK) == STATIC_ENT; 358 } 359 360 static void 361 mt7530_fdb_write(struct mt7530_priv *priv, u16 vid, 362 u8 port_mask, const u8 *mac, 363 u8 aging, u8 type) 364 { 365 u32 reg[3] = { 0 }; 366 int i; 367 368 reg[1] |= vid & CVID_MASK; 369 reg[2] |= (aging & AGE_TIMER_MASK) << AGE_TIMER; 370 reg[2] |= (port_mask & PORT_MAP_MASK) << PORT_MAP; 371 /* STATIC_ENT indicate that entry is static wouldn't 372 * be aged out and STATIC_EMP specified as erasing an 373 * entry 374 */ 375 reg[2] |= (type & ENT_STATUS_MASK) << ENT_STATUS; 376 reg[1] |= mac[5] << MAC_BYTE_5; 377 reg[1] |= mac[4] << MAC_BYTE_4; 378 reg[0] |= mac[3] << MAC_BYTE_3; 379 reg[0] |= mac[2] << MAC_BYTE_2; 380 reg[0] |= mac[1] << MAC_BYTE_1; 381 reg[0] |= mac[0] << MAC_BYTE_0; 382 383 /* Write array into the ARL table */ 384 for (i = 0; i < 3; i++) 385 mt7530_write(priv, MT7530_ATA1 + (i * 4), reg[i]); 386 } 387 388 /* Setup TX circuit including relevant PAD and driving */ 389 static int 390 mt7530_pad_clk_setup(struct dsa_switch *ds, phy_interface_t interface) 391 { 392 struct mt7530_priv *priv = ds->priv; 393 u32 ncpo1, ssc_delta, trgint, i, xtal; 394 395 xtal = mt7530_read(priv, MT7530_MHWTRAP) & HWTRAP_XTAL_MASK; 396 397 if (xtal == HWTRAP_XTAL_20MHZ) { 398 dev_err(priv->dev, 399 "%s: MT7530 with a 20MHz XTAL is not supported!\n", 400 __func__); 401 return -EINVAL; 402 } 403 404 switch (interface) { 405 case PHY_INTERFACE_MODE_RGMII: 406 trgint = 0; 407 /* PLL frequency: 125MHz */ 408 ncpo1 = 0x0c80; 409 break; 410 case PHY_INTERFACE_MODE_TRGMII: 411 trgint = 1; 412 if (priv->id == ID_MT7621) { 413 /* PLL frequency: 150MHz: 1.2GBit */ 414 if (xtal == HWTRAP_XTAL_40MHZ) 415 ncpo1 = 0x0780; 416 if (xtal == HWTRAP_XTAL_25MHZ) 417 ncpo1 = 0x0a00; 418 } else { /* PLL frequency: 250MHz: 2.0Gbit */ 419 if (xtal == HWTRAP_XTAL_40MHZ) 420 ncpo1 = 0x0c80; 421 if (xtal == HWTRAP_XTAL_25MHZ) 422 ncpo1 = 0x1400; 423 } 424 break; 425 default: 426 dev_err(priv->dev, "xMII interface %d not supported\n", 427 interface); 428 return -EINVAL; 429 } 430 431 if (xtal == HWTRAP_XTAL_25MHZ) 432 ssc_delta = 0x57; 433 else 434 ssc_delta = 0x87; 435 436 mt7530_rmw(priv, MT7530_P6ECR, P6_INTF_MODE_MASK, 437 P6_INTF_MODE(trgint)); 438 439 /* Lower Tx Driving for TRGMII path */ 440 for (i = 0 ; i < NUM_TRGMII_CTRL ; i++) 441 mt7530_write(priv, MT7530_TRGMII_TD_ODT(i), 442 TD_DM_DRVP(8) | TD_DM_DRVN(8)); 443 444 /* Disable MT7530 core and TRGMII Tx clocks */ 445 core_clear(priv, CORE_TRGMII_GSW_CLK_CG, 446 REG_GSWCK_EN | REG_TRGMIICK_EN); 447 448 /* Setup core clock for MT7530 */ 449 /* Disable PLL */ 450 core_write(priv, CORE_GSWPLL_GRP1, 0); 451 452 /* Set core clock into 500Mhz */ 453 core_write(priv, CORE_GSWPLL_GRP2, 454 RG_GSWPLL_POSDIV_500M(1) | 455 RG_GSWPLL_FBKDIV_500M(25)); 456 457 /* Enable PLL */ 458 core_write(priv, CORE_GSWPLL_GRP1, 459 RG_GSWPLL_EN_PRE | 460 RG_GSWPLL_POSDIV_200M(2) | 461 RG_GSWPLL_FBKDIV_200M(32)); 462 463 /* Setup the MT7530 TRGMII Tx Clock */ 464 core_write(priv, CORE_PLL_GROUP5, RG_LCDDS_PCW_NCPO1(ncpo1)); 465 core_write(priv, CORE_PLL_GROUP6, RG_LCDDS_PCW_NCPO0(0)); 466 core_write(priv, CORE_PLL_GROUP10, RG_LCDDS_SSC_DELTA(ssc_delta)); 467 core_write(priv, CORE_PLL_GROUP11, RG_LCDDS_SSC_DELTA1(ssc_delta)); 468 core_write(priv, CORE_PLL_GROUP4, 469 RG_SYSPLL_DDSFBK_EN | RG_SYSPLL_BIAS_EN | 470 RG_SYSPLL_BIAS_LPF_EN); 471 core_write(priv, CORE_PLL_GROUP2, 472 RG_SYSPLL_EN_NORMAL | RG_SYSPLL_VODEN | 473 RG_SYSPLL_POSDIV(1)); 474 core_write(priv, CORE_PLL_GROUP7, 475 RG_LCDDS_PCW_NCPO_CHG | RG_LCCDS_C(3) | 476 RG_LCDDS_PWDB | RG_LCDDS_ISO_EN); 477 478 /* Enable MT7530 core and TRGMII Tx clocks */ 479 core_set(priv, CORE_TRGMII_GSW_CLK_CG, 480 REG_GSWCK_EN | REG_TRGMIICK_EN); 481 482 if (!trgint) 483 for (i = 0 ; i < NUM_TRGMII_CTRL; i++) 484 mt7530_rmw(priv, MT7530_TRGMII_RD(i), 485 RD_TAP_MASK, RD_TAP(16)); 486 return 0; 487 } 488 489 static bool mt7531_dual_sgmii_supported(struct mt7530_priv *priv) 490 { 491 u32 val; 492 493 val = mt7530_read(priv, MT7531_TOP_SIG_SR); 494 495 return (val & PAD_DUAL_SGMII_EN) != 0; 496 } 497 498 static int 499 mt7531_pad_setup(struct dsa_switch *ds, phy_interface_t interface) 500 { 501 struct mt7530_priv *priv = ds->priv; 502 u32 top_sig; 503 u32 hwstrap; 504 u32 xtal; 505 u32 val; 506 507 if (mt7531_dual_sgmii_supported(priv)) 508 return 0; 509 510 val = mt7530_read(priv, MT7531_CREV); 511 top_sig = mt7530_read(priv, MT7531_TOP_SIG_SR); 512 hwstrap = mt7530_read(priv, MT7531_HWTRAP); 513 if ((val & CHIP_REV_M) > 0) 514 xtal = (top_sig & PAD_MCM_SMI_EN) ? HWTRAP_XTAL_FSEL_40MHZ : 515 HWTRAP_XTAL_FSEL_25MHZ; 516 else 517 xtal = hwstrap & HWTRAP_XTAL_FSEL_MASK; 518 519 /* Step 1 : Disable MT7531 COREPLL */ 520 val = mt7530_read(priv, MT7531_PLLGP_EN); 521 val &= ~EN_COREPLL; 522 mt7530_write(priv, MT7531_PLLGP_EN, val); 523 524 /* Step 2: switch to XTAL output */ 525 val = mt7530_read(priv, MT7531_PLLGP_EN); 526 val |= SW_CLKSW; 527 mt7530_write(priv, MT7531_PLLGP_EN, val); 528 529 val = mt7530_read(priv, MT7531_PLLGP_CR0); 530 val &= ~RG_COREPLL_EN; 531 mt7530_write(priv, MT7531_PLLGP_CR0, val); 532 533 /* Step 3: disable PLLGP and enable program PLLGP */ 534 val = mt7530_read(priv, MT7531_PLLGP_EN); 535 val |= SW_PLLGP; 536 mt7530_write(priv, MT7531_PLLGP_EN, val); 537 538 /* Step 4: program COREPLL output frequency to 500MHz */ 539 val = mt7530_read(priv, MT7531_PLLGP_CR0); 540 val &= ~RG_COREPLL_POSDIV_M; 541 val |= 2 << RG_COREPLL_POSDIV_S; 542 mt7530_write(priv, MT7531_PLLGP_CR0, val); 543 usleep_range(25, 35); 544 545 switch (xtal) { 546 case HWTRAP_XTAL_FSEL_25MHZ: 547 val = mt7530_read(priv, MT7531_PLLGP_CR0); 548 val &= ~RG_COREPLL_SDM_PCW_M; 549 val |= 0x140000 << RG_COREPLL_SDM_PCW_S; 550 mt7530_write(priv, MT7531_PLLGP_CR0, val); 551 break; 552 case HWTRAP_XTAL_FSEL_40MHZ: 553 val = mt7530_read(priv, MT7531_PLLGP_CR0); 554 val &= ~RG_COREPLL_SDM_PCW_M; 555 val |= 0x190000 << RG_COREPLL_SDM_PCW_S; 556 mt7530_write(priv, MT7531_PLLGP_CR0, val); 557 break; 558 } 559 560 /* Set feedback divide ratio update signal to high */ 561 val = mt7530_read(priv, MT7531_PLLGP_CR0); 562 val |= RG_COREPLL_SDM_PCW_CHG; 563 mt7530_write(priv, MT7531_PLLGP_CR0, val); 564 /* Wait for at least 16 XTAL clocks */ 565 usleep_range(10, 20); 566 567 /* Step 5: set feedback divide ratio update signal to low */ 568 val = mt7530_read(priv, MT7531_PLLGP_CR0); 569 val &= ~RG_COREPLL_SDM_PCW_CHG; 570 mt7530_write(priv, MT7531_PLLGP_CR0, val); 571 572 /* Enable 325M clock for SGMII */ 573 mt7530_write(priv, MT7531_ANA_PLLGP_CR5, 0xad0000); 574 575 /* Enable 250SSC clock for RGMII */ 576 mt7530_write(priv, MT7531_ANA_PLLGP_CR2, 0x4f40000); 577 578 /* Step 6: Enable MT7531 PLL */ 579 val = mt7530_read(priv, MT7531_PLLGP_CR0); 580 val |= RG_COREPLL_EN; 581 mt7530_write(priv, MT7531_PLLGP_CR0, val); 582 583 val = mt7530_read(priv, MT7531_PLLGP_EN); 584 val |= EN_COREPLL; 585 mt7530_write(priv, MT7531_PLLGP_EN, val); 586 usleep_range(25, 35); 587 588 return 0; 589 } 590 591 static void 592 mt7530_mib_reset(struct dsa_switch *ds) 593 { 594 struct mt7530_priv *priv = ds->priv; 595 596 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_FLUSH); 597 mt7530_write(priv, MT7530_MIB_CCR, CCR_MIB_ACTIVATE); 598 } 599 600 static int mt7530_phy_read(struct mt7530_priv *priv, int port, int regnum) 601 { 602 return mdiobus_read_nested(priv->bus, port, regnum); 603 } 604 605 static int mt7530_phy_write(struct mt7530_priv *priv, int port, int regnum, 606 u16 val) 607 { 608 return mdiobus_write_nested(priv->bus, port, regnum, val); 609 } 610 611 static int 612 mt7531_ind_c45_phy_read(struct mt7530_priv *priv, int port, int devad, 613 int regnum) 614 { 615 struct mii_bus *bus = priv->bus; 616 struct mt7530_dummy_poll p; 617 u32 reg, val; 618 int ret; 619 620 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC); 621 622 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 623 624 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 625 !(val & MT7531_PHY_ACS_ST), 20, 100000); 626 if (ret < 0) { 627 dev_err(priv->dev, "poll timeout\n"); 628 goto out; 629 } 630 631 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) | 632 MT7531_MDIO_DEV_ADDR(devad) | regnum; 633 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST); 634 635 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 636 !(val & MT7531_PHY_ACS_ST), 20, 100000); 637 if (ret < 0) { 638 dev_err(priv->dev, "poll timeout\n"); 639 goto out; 640 } 641 642 reg = MT7531_MDIO_CL45_READ | MT7531_MDIO_PHY_ADDR(port) | 643 MT7531_MDIO_DEV_ADDR(devad); 644 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST); 645 646 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 647 !(val & MT7531_PHY_ACS_ST), 20, 100000); 648 if (ret < 0) { 649 dev_err(priv->dev, "poll timeout\n"); 650 goto out; 651 } 652 653 ret = val & MT7531_MDIO_RW_DATA_MASK; 654 out: 655 mutex_unlock(&bus->mdio_lock); 656 657 return ret; 658 } 659 660 static int 661 mt7531_ind_c45_phy_write(struct mt7530_priv *priv, int port, int devad, 662 int regnum, u32 data) 663 { 664 struct mii_bus *bus = priv->bus; 665 struct mt7530_dummy_poll p; 666 u32 val, reg; 667 int ret; 668 669 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC); 670 671 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 672 673 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 674 !(val & MT7531_PHY_ACS_ST), 20, 100000); 675 if (ret < 0) { 676 dev_err(priv->dev, "poll timeout\n"); 677 goto out; 678 } 679 680 reg = MT7531_MDIO_CL45_ADDR | MT7531_MDIO_PHY_ADDR(port) | 681 MT7531_MDIO_DEV_ADDR(devad) | regnum; 682 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST); 683 684 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 685 !(val & MT7531_PHY_ACS_ST), 20, 100000); 686 if (ret < 0) { 687 dev_err(priv->dev, "poll timeout\n"); 688 goto out; 689 } 690 691 reg = MT7531_MDIO_CL45_WRITE | MT7531_MDIO_PHY_ADDR(port) | 692 MT7531_MDIO_DEV_ADDR(devad) | data; 693 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST); 694 695 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 696 !(val & MT7531_PHY_ACS_ST), 20, 100000); 697 if (ret < 0) { 698 dev_err(priv->dev, "poll timeout\n"); 699 goto out; 700 } 701 702 out: 703 mutex_unlock(&bus->mdio_lock); 704 705 return ret; 706 } 707 708 static int 709 mt7531_ind_c22_phy_read(struct mt7530_priv *priv, int port, int regnum) 710 { 711 struct mii_bus *bus = priv->bus; 712 struct mt7530_dummy_poll p; 713 int ret; 714 u32 val; 715 716 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC); 717 718 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 719 720 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 721 !(val & MT7531_PHY_ACS_ST), 20, 100000); 722 if (ret < 0) { 723 dev_err(priv->dev, "poll timeout\n"); 724 goto out; 725 } 726 727 val = MT7531_MDIO_CL22_READ | MT7531_MDIO_PHY_ADDR(port) | 728 MT7531_MDIO_REG_ADDR(regnum); 729 730 mt7530_mii_write(priv, MT7531_PHY_IAC, val | MT7531_PHY_ACS_ST); 731 732 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, val, 733 !(val & MT7531_PHY_ACS_ST), 20, 100000); 734 if (ret < 0) { 735 dev_err(priv->dev, "poll timeout\n"); 736 goto out; 737 } 738 739 ret = val & MT7531_MDIO_RW_DATA_MASK; 740 out: 741 mutex_unlock(&bus->mdio_lock); 742 743 return ret; 744 } 745 746 static int 747 mt7531_ind_c22_phy_write(struct mt7530_priv *priv, int port, int regnum, 748 u16 data) 749 { 750 struct mii_bus *bus = priv->bus; 751 struct mt7530_dummy_poll p; 752 int ret; 753 u32 reg; 754 755 INIT_MT7530_DUMMY_POLL(&p, priv, MT7531_PHY_IAC); 756 757 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 758 759 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg, 760 !(reg & MT7531_PHY_ACS_ST), 20, 100000); 761 if (ret < 0) { 762 dev_err(priv->dev, "poll timeout\n"); 763 goto out; 764 } 765 766 reg = MT7531_MDIO_CL22_WRITE | MT7531_MDIO_PHY_ADDR(port) | 767 MT7531_MDIO_REG_ADDR(regnum) | data; 768 769 mt7530_mii_write(priv, MT7531_PHY_IAC, reg | MT7531_PHY_ACS_ST); 770 771 ret = readx_poll_timeout(_mt7530_unlocked_read, &p, reg, 772 !(reg & MT7531_PHY_ACS_ST), 20, 100000); 773 if (ret < 0) { 774 dev_err(priv->dev, "poll timeout\n"); 775 goto out; 776 } 777 778 out: 779 mutex_unlock(&bus->mdio_lock); 780 781 return ret; 782 } 783 784 static int 785 mt7531_ind_phy_read(struct mt7530_priv *priv, int port, int regnum) 786 { 787 int devad; 788 int ret; 789 790 if (regnum & MII_ADDR_C45) { 791 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f; 792 ret = mt7531_ind_c45_phy_read(priv, port, devad, 793 regnum & MII_REGADDR_C45_MASK); 794 } else { 795 ret = mt7531_ind_c22_phy_read(priv, port, regnum); 796 } 797 798 return ret; 799 } 800 801 static int 802 mt7531_ind_phy_write(struct mt7530_priv *priv, int port, int regnum, 803 u16 data) 804 { 805 int devad; 806 int ret; 807 808 if (regnum & MII_ADDR_C45) { 809 devad = (regnum >> MII_DEVADDR_C45_SHIFT) & 0x1f; 810 ret = mt7531_ind_c45_phy_write(priv, port, devad, 811 regnum & MII_REGADDR_C45_MASK, 812 data); 813 } else { 814 ret = mt7531_ind_c22_phy_write(priv, port, regnum, data); 815 } 816 817 return ret; 818 } 819 820 static int 821 mt753x_phy_read(struct mii_bus *bus, int port, int regnum) 822 { 823 struct mt7530_priv *priv = bus->priv; 824 825 return priv->info->phy_read(priv, port, regnum); 826 } 827 828 static int 829 mt753x_phy_write(struct mii_bus *bus, int port, int regnum, u16 val) 830 { 831 struct mt7530_priv *priv = bus->priv; 832 833 return priv->info->phy_write(priv, port, regnum, val); 834 } 835 836 static void 837 mt7530_get_strings(struct dsa_switch *ds, int port, u32 stringset, 838 uint8_t *data) 839 { 840 int i; 841 842 if (stringset != ETH_SS_STATS) 843 return; 844 845 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) 846 strncpy(data + i * ETH_GSTRING_LEN, mt7530_mib[i].name, 847 ETH_GSTRING_LEN); 848 } 849 850 static void 851 mt7530_get_ethtool_stats(struct dsa_switch *ds, int port, 852 uint64_t *data) 853 { 854 struct mt7530_priv *priv = ds->priv; 855 const struct mt7530_mib_desc *mib; 856 u32 reg, i; 857 u64 hi; 858 859 for (i = 0; i < ARRAY_SIZE(mt7530_mib); i++) { 860 mib = &mt7530_mib[i]; 861 reg = MT7530_PORT_MIB_COUNTER(port) + mib->offset; 862 863 data[i] = mt7530_read(priv, reg); 864 if (mib->size == 2) { 865 hi = mt7530_read(priv, reg + 4); 866 data[i] |= hi << 32; 867 } 868 } 869 } 870 871 static int 872 mt7530_get_sset_count(struct dsa_switch *ds, int port, int sset) 873 { 874 if (sset != ETH_SS_STATS) 875 return 0; 876 877 return ARRAY_SIZE(mt7530_mib); 878 } 879 880 static int 881 mt7530_set_ageing_time(struct dsa_switch *ds, unsigned int msecs) 882 { 883 struct mt7530_priv *priv = ds->priv; 884 unsigned int secs = msecs / 1000; 885 unsigned int tmp_age_count; 886 unsigned int error = -1; 887 unsigned int age_count; 888 unsigned int age_unit; 889 890 /* Applied timer is (AGE_CNT + 1) * (AGE_UNIT + 1) seconds */ 891 if (secs < 1 || secs > (AGE_CNT_MAX + 1) * (AGE_UNIT_MAX + 1)) 892 return -ERANGE; 893 894 /* iterate through all possible age_count to find the closest pair */ 895 for (tmp_age_count = 0; tmp_age_count <= AGE_CNT_MAX; ++tmp_age_count) { 896 unsigned int tmp_age_unit = secs / (tmp_age_count + 1) - 1; 897 898 if (tmp_age_unit <= AGE_UNIT_MAX) { 899 unsigned int tmp_error = secs - 900 (tmp_age_count + 1) * (tmp_age_unit + 1); 901 902 /* found a closer pair */ 903 if (error > tmp_error) { 904 error = tmp_error; 905 age_count = tmp_age_count; 906 age_unit = tmp_age_unit; 907 } 908 909 /* found the exact match, so break the loop */ 910 if (!error) 911 break; 912 } 913 } 914 915 mt7530_write(priv, MT7530_AAC, AGE_CNT(age_count) | AGE_UNIT(age_unit)); 916 917 return 0; 918 } 919 920 static void mt7530_setup_port5(struct dsa_switch *ds, phy_interface_t interface) 921 { 922 struct mt7530_priv *priv = ds->priv; 923 u8 tx_delay = 0; 924 int val; 925 926 mutex_lock(&priv->reg_mutex); 927 928 val = mt7530_read(priv, MT7530_MHWTRAP); 929 930 val |= MHWTRAP_MANUAL | MHWTRAP_P5_MAC_SEL | MHWTRAP_P5_DIS; 931 val &= ~MHWTRAP_P5_RGMII_MODE & ~MHWTRAP_PHY0_SEL; 932 933 switch (priv->p5_intf_sel) { 934 case P5_INTF_SEL_PHY_P0: 935 /* MT7530_P5_MODE_GPHY_P0: 2nd GMAC -> P5 -> P0 */ 936 val |= MHWTRAP_PHY0_SEL; 937 fallthrough; 938 case P5_INTF_SEL_PHY_P4: 939 /* MT7530_P5_MODE_GPHY_P4: 2nd GMAC -> P5 -> P4 */ 940 val &= ~MHWTRAP_P5_MAC_SEL & ~MHWTRAP_P5_DIS; 941 942 /* Setup the MAC by default for the cpu port */ 943 mt7530_write(priv, MT7530_PMCR_P(5), 0x56300); 944 break; 945 case P5_INTF_SEL_GMAC5: 946 /* MT7530_P5_MODE_GMAC: P5 -> External phy or 2nd GMAC */ 947 val &= ~MHWTRAP_P5_DIS; 948 break; 949 case P5_DISABLED: 950 interface = PHY_INTERFACE_MODE_NA; 951 break; 952 default: 953 dev_err(ds->dev, "Unsupported p5_intf_sel %d\n", 954 priv->p5_intf_sel); 955 goto unlock_exit; 956 } 957 958 /* Setup RGMII settings */ 959 if (phy_interface_mode_is_rgmii(interface)) { 960 val |= MHWTRAP_P5_RGMII_MODE; 961 962 /* P5 RGMII RX Clock Control: delay setting for 1000M */ 963 mt7530_write(priv, MT7530_P5RGMIIRXCR, CSR_RGMII_EDGE_ALIGN); 964 965 /* Don't set delay in DSA mode */ 966 if (!dsa_is_dsa_port(priv->ds, 5) && 967 (interface == PHY_INTERFACE_MODE_RGMII_TXID || 968 interface == PHY_INTERFACE_MODE_RGMII_ID)) 969 tx_delay = 4; /* n * 0.5 ns */ 970 971 /* P5 RGMII TX Clock Control: delay x */ 972 mt7530_write(priv, MT7530_P5RGMIITXCR, 973 CSR_RGMII_TXC_CFG(0x10 + tx_delay)); 974 975 /* reduce P5 RGMII Tx driving, 8mA */ 976 mt7530_write(priv, MT7530_IO_DRV_CR, 977 P5_IO_CLK_DRV(1) | P5_IO_DATA_DRV(1)); 978 } 979 980 mt7530_write(priv, MT7530_MHWTRAP, val); 981 982 dev_dbg(ds->dev, "Setup P5, HWTRAP=0x%x, intf_sel=%s, phy-mode=%s\n", 983 val, p5_intf_modes(priv->p5_intf_sel), phy_modes(interface)); 984 985 priv->p5_interface = interface; 986 987 unlock_exit: 988 mutex_unlock(&priv->reg_mutex); 989 } 990 991 static int 992 mt753x_cpu_port_enable(struct dsa_switch *ds, int port) 993 { 994 struct mt7530_priv *priv = ds->priv; 995 int ret; 996 997 /* Setup max capability of CPU port at first */ 998 if (priv->info->cpu_port_config) { 999 ret = priv->info->cpu_port_config(ds, port); 1000 if (ret) 1001 return ret; 1002 } 1003 1004 /* Enable Mediatek header mode on the cpu port */ 1005 mt7530_write(priv, MT7530_PVC_P(port), 1006 PORT_SPEC_TAG); 1007 1008 /* Disable flooding by default */ 1009 mt7530_rmw(priv, MT7530_MFC, BC_FFP_MASK | UNM_FFP_MASK | UNU_FFP_MASK, 1010 BC_FFP(BIT(port)) | UNM_FFP(BIT(port)) | UNU_FFP(BIT(port))); 1011 1012 /* Set CPU port number */ 1013 if (priv->id == ID_MT7621) 1014 mt7530_rmw(priv, MT7530_MFC, CPU_MASK, CPU_EN | CPU_PORT(port)); 1015 1016 /* CPU port gets connected to all user ports of 1017 * the switch. 1018 */ 1019 mt7530_write(priv, MT7530_PCR_P(port), 1020 PCR_MATRIX(dsa_user_ports(priv->ds))); 1021 1022 return 0; 1023 } 1024 1025 static int 1026 mt7530_port_enable(struct dsa_switch *ds, int port, 1027 struct phy_device *phy) 1028 { 1029 struct mt7530_priv *priv = ds->priv; 1030 1031 if (!dsa_is_user_port(ds, port)) 1032 return 0; 1033 1034 mutex_lock(&priv->reg_mutex); 1035 1036 /* Allow the user port gets connected to the cpu port and also 1037 * restore the port matrix if the port is the member of a certain 1038 * bridge. 1039 */ 1040 priv->ports[port].pm |= PCR_MATRIX(BIT(MT7530_CPU_PORT)); 1041 priv->ports[port].enable = true; 1042 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, 1043 priv->ports[port].pm); 1044 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK); 1045 1046 mutex_unlock(&priv->reg_mutex); 1047 1048 return 0; 1049 } 1050 1051 static void 1052 mt7530_port_disable(struct dsa_switch *ds, int port) 1053 { 1054 struct mt7530_priv *priv = ds->priv; 1055 1056 if (!dsa_is_user_port(ds, port)) 1057 return; 1058 1059 mutex_lock(&priv->reg_mutex); 1060 1061 /* Clear up all port matrix which could be restored in the next 1062 * enablement for the port. 1063 */ 1064 priv->ports[port].enable = false; 1065 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, 1066 PCR_MATRIX_CLR); 1067 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK); 1068 1069 mutex_unlock(&priv->reg_mutex); 1070 } 1071 1072 static int 1073 mt7530_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu) 1074 { 1075 struct mt7530_priv *priv = ds->priv; 1076 struct mii_bus *bus = priv->bus; 1077 int length; 1078 u32 val; 1079 1080 /* When a new MTU is set, DSA always set the CPU port's MTU to the 1081 * largest MTU of the slave ports. Because the switch only has a global 1082 * RX length register, only allowing CPU port here is enough. 1083 */ 1084 if (!dsa_is_cpu_port(ds, port)) 1085 return 0; 1086 1087 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED); 1088 1089 val = mt7530_mii_read(priv, MT7530_GMACCR); 1090 val &= ~MAX_RX_PKT_LEN_MASK; 1091 1092 /* RX length also includes Ethernet header, MTK tag, and FCS length */ 1093 length = new_mtu + ETH_HLEN + MTK_HDR_LEN + ETH_FCS_LEN; 1094 if (length <= 1522) { 1095 val |= MAX_RX_PKT_LEN_1522; 1096 } else if (length <= 1536) { 1097 val |= MAX_RX_PKT_LEN_1536; 1098 } else if (length <= 1552) { 1099 val |= MAX_RX_PKT_LEN_1552; 1100 } else { 1101 val &= ~MAX_RX_JUMBO_MASK; 1102 val |= MAX_RX_JUMBO(DIV_ROUND_UP(length, 1024)); 1103 val |= MAX_RX_PKT_LEN_JUMBO; 1104 } 1105 1106 mt7530_mii_write(priv, MT7530_GMACCR, val); 1107 1108 mutex_unlock(&bus->mdio_lock); 1109 1110 return 0; 1111 } 1112 1113 static int 1114 mt7530_port_max_mtu(struct dsa_switch *ds, int port) 1115 { 1116 return MT7530_MAX_MTU; 1117 } 1118 1119 static void 1120 mt7530_stp_state_set(struct dsa_switch *ds, int port, u8 state) 1121 { 1122 struct mt7530_priv *priv = ds->priv; 1123 u32 stp_state; 1124 1125 switch (state) { 1126 case BR_STATE_DISABLED: 1127 stp_state = MT7530_STP_DISABLED; 1128 break; 1129 case BR_STATE_BLOCKING: 1130 stp_state = MT7530_STP_BLOCKING; 1131 break; 1132 case BR_STATE_LISTENING: 1133 stp_state = MT7530_STP_LISTENING; 1134 break; 1135 case BR_STATE_LEARNING: 1136 stp_state = MT7530_STP_LEARNING; 1137 break; 1138 case BR_STATE_FORWARDING: 1139 default: 1140 stp_state = MT7530_STP_FORWARDING; 1141 break; 1142 } 1143 1144 mt7530_rmw(priv, MT7530_SSP_P(port), FID_PST_MASK, stp_state); 1145 } 1146 1147 static int 1148 mt7530_port_pre_bridge_flags(struct dsa_switch *ds, int port, 1149 struct switchdev_brport_flags flags, 1150 struct netlink_ext_ack *extack) 1151 { 1152 if (flags.mask & ~(BR_LEARNING | BR_FLOOD | BR_MCAST_FLOOD | 1153 BR_BCAST_FLOOD)) 1154 return -EINVAL; 1155 1156 return 0; 1157 } 1158 1159 static int 1160 mt7530_port_bridge_flags(struct dsa_switch *ds, int port, 1161 struct switchdev_brport_flags flags, 1162 struct netlink_ext_ack *extack) 1163 { 1164 struct mt7530_priv *priv = ds->priv; 1165 1166 if (flags.mask & BR_LEARNING) 1167 mt7530_rmw(priv, MT7530_PSC_P(port), SA_DIS, 1168 flags.val & BR_LEARNING ? 0 : SA_DIS); 1169 1170 if (flags.mask & BR_FLOOD) 1171 mt7530_rmw(priv, MT7530_MFC, UNU_FFP(BIT(port)), 1172 flags.val & BR_FLOOD ? UNU_FFP(BIT(port)) : 0); 1173 1174 if (flags.mask & BR_MCAST_FLOOD) 1175 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)), 1176 flags.val & BR_MCAST_FLOOD ? UNM_FFP(BIT(port)) : 0); 1177 1178 if (flags.mask & BR_BCAST_FLOOD) 1179 mt7530_rmw(priv, MT7530_MFC, BC_FFP(BIT(port)), 1180 flags.val & BR_BCAST_FLOOD ? BC_FFP(BIT(port)) : 0); 1181 1182 return 0; 1183 } 1184 1185 static int 1186 mt7530_port_set_mrouter(struct dsa_switch *ds, int port, bool mrouter, 1187 struct netlink_ext_ack *extack) 1188 { 1189 struct mt7530_priv *priv = ds->priv; 1190 1191 mt7530_rmw(priv, MT7530_MFC, UNM_FFP(BIT(port)), 1192 mrouter ? UNM_FFP(BIT(port)) : 0); 1193 1194 return 0; 1195 } 1196 1197 static int 1198 mt7530_port_bridge_join(struct dsa_switch *ds, int port, 1199 struct net_device *bridge) 1200 { 1201 struct mt7530_priv *priv = ds->priv; 1202 u32 port_bitmap = BIT(MT7530_CPU_PORT); 1203 int i; 1204 1205 mutex_lock(&priv->reg_mutex); 1206 1207 for (i = 0; i < MT7530_NUM_PORTS; i++) { 1208 /* Add this port to the port matrix of the other ports in the 1209 * same bridge. If the port is disabled, port matrix is kept 1210 * and not being setup until the port becomes enabled. 1211 */ 1212 if (dsa_is_user_port(ds, i) && i != port) { 1213 if (dsa_to_port(ds, i)->bridge_dev != bridge) 1214 continue; 1215 if (priv->ports[i].enable) 1216 mt7530_set(priv, MT7530_PCR_P(i), 1217 PCR_MATRIX(BIT(port))); 1218 priv->ports[i].pm |= PCR_MATRIX(BIT(port)); 1219 1220 port_bitmap |= BIT(i); 1221 } 1222 } 1223 1224 /* Add the all other ports to this port matrix. */ 1225 if (priv->ports[port].enable) 1226 mt7530_rmw(priv, MT7530_PCR_P(port), 1227 PCR_MATRIX_MASK, PCR_MATRIX(port_bitmap)); 1228 priv->ports[port].pm |= PCR_MATRIX(port_bitmap); 1229 1230 mutex_unlock(&priv->reg_mutex); 1231 1232 return 0; 1233 } 1234 1235 static void 1236 mt7530_port_set_vlan_unaware(struct dsa_switch *ds, int port) 1237 { 1238 struct mt7530_priv *priv = ds->priv; 1239 bool all_user_ports_removed = true; 1240 int i; 1241 1242 /* When a port is removed from the bridge, the port would be set up 1243 * back to the default as is at initial boot which is a VLAN-unaware 1244 * port. 1245 */ 1246 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, 1247 MT7530_PORT_MATRIX_MODE); 1248 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK, 1249 VLAN_ATTR(MT7530_VLAN_TRANSPARENT) | 1250 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); 1251 1252 for (i = 0; i < MT7530_NUM_PORTS; i++) { 1253 if (dsa_is_user_port(ds, i) && 1254 dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) { 1255 all_user_ports_removed = false; 1256 break; 1257 } 1258 } 1259 1260 /* CPU port also does the same thing until all user ports belonging to 1261 * the CPU port get out of VLAN filtering mode. 1262 */ 1263 if (all_user_ports_removed) { 1264 mt7530_write(priv, MT7530_PCR_P(MT7530_CPU_PORT), 1265 PCR_MATRIX(dsa_user_ports(priv->ds))); 1266 mt7530_write(priv, MT7530_PVC_P(MT7530_CPU_PORT), PORT_SPEC_TAG 1267 | PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); 1268 } 1269 } 1270 1271 static void 1272 mt7530_port_set_vlan_aware(struct dsa_switch *ds, int port) 1273 { 1274 struct mt7530_priv *priv = ds->priv; 1275 1276 /* The real fabric path would be decided on the membership in the 1277 * entry of VLAN table. PCR_MATRIX set up here with ALL_MEMBERS 1278 * means potential VLAN can be consisting of certain subset of all 1279 * ports. 1280 */ 1281 mt7530_rmw(priv, MT7530_PCR_P(port), 1282 PCR_MATRIX_MASK, PCR_MATRIX(MT7530_ALL_MEMBERS)); 1283 1284 /* Trapped into security mode allows packet forwarding through VLAN 1285 * table lookup. CPU port is set to fallback mode to let untagged 1286 * frames pass through. 1287 */ 1288 if (dsa_is_cpu_port(ds, port)) 1289 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, 1290 MT7530_PORT_FALLBACK_MODE); 1291 else 1292 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_PORT_VLAN_MASK, 1293 MT7530_PORT_SECURITY_MODE); 1294 1295 /* Set the port as a user port which is to be able to recognize VID 1296 * from incoming packets before fetching entry within the VLAN table. 1297 */ 1298 mt7530_rmw(priv, MT7530_PVC_P(port), VLAN_ATTR_MASK | PVC_EG_TAG_MASK, 1299 VLAN_ATTR(MT7530_VLAN_USER) | 1300 PVC_EG_TAG(MT7530_VLAN_EG_DISABLED)); 1301 } 1302 1303 static void 1304 mt7530_port_bridge_leave(struct dsa_switch *ds, int port, 1305 struct net_device *bridge) 1306 { 1307 struct mt7530_priv *priv = ds->priv; 1308 int i; 1309 1310 mutex_lock(&priv->reg_mutex); 1311 1312 for (i = 0; i < MT7530_NUM_PORTS; i++) { 1313 /* Remove this port from the port matrix of the other ports 1314 * in the same bridge. If the port is disabled, port matrix 1315 * is kept and not being setup until the port becomes enabled. 1316 * And the other port's port matrix cannot be broken when the 1317 * other port is still a VLAN-aware port. 1318 */ 1319 if (dsa_is_user_port(ds, i) && i != port && 1320 !dsa_port_is_vlan_filtering(dsa_to_port(ds, i))) { 1321 if (dsa_to_port(ds, i)->bridge_dev != bridge) 1322 continue; 1323 if (priv->ports[i].enable) 1324 mt7530_clear(priv, MT7530_PCR_P(i), 1325 PCR_MATRIX(BIT(port))); 1326 priv->ports[i].pm &= ~PCR_MATRIX(BIT(port)); 1327 } 1328 } 1329 1330 /* Set the cpu port to be the only one in the port matrix of 1331 * this port. 1332 */ 1333 if (priv->ports[port].enable) 1334 mt7530_rmw(priv, MT7530_PCR_P(port), PCR_MATRIX_MASK, 1335 PCR_MATRIX(BIT(MT7530_CPU_PORT))); 1336 priv->ports[port].pm = PCR_MATRIX(BIT(MT7530_CPU_PORT)); 1337 1338 mutex_unlock(&priv->reg_mutex); 1339 } 1340 1341 static int 1342 mt7530_port_fdb_add(struct dsa_switch *ds, int port, 1343 const unsigned char *addr, u16 vid) 1344 { 1345 struct mt7530_priv *priv = ds->priv; 1346 int ret; 1347 u8 port_mask = BIT(port); 1348 1349 mutex_lock(&priv->reg_mutex); 1350 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT); 1351 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); 1352 mutex_unlock(&priv->reg_mutex); 1353 1354 return ret; 1355 } 1356 1357 static int 1358 mt7530_port_fdb_del(struct dsa_switch *ds, int port, 1359 const unsigned char *addr, u16 vid) 1360 { 1361 struct mt7530_priv *priv = ds->priv; 1362 int ret; 1363 u8 port_mask = BIT(port); 1364 1365 mutex_lock(&priv->reg_mutex); 1366 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_EMP); 1367 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); 1368 mutex_unlock(&priv->reg_mutex); 1369 1370 return ret; 1371 } 1372 1373 static int 1374 mt7530_port_fdb_dump(struct dsa_switch *ds, int port, 1375 dsa_fdb_dump_cb_t *cb, void *data) 1376 { 1377 struct mt7530_priv *priv = ds->priv; 1378 struct mt7530_fdb _fdb = { 0 }; 1379 int cnt = MT7530_NUM_FDB_RECORDS; 1380 int ret = 0; 1381 u32 rsp = 0; 1382 1383 mutex_lock(&priv->reg_mutex); 1384 1385 ret = mt7530_fdb_cmd(priv, MT7530_FDB_START, &rsp); 1386 if (ret < 0) 1387 goto err; 1388 1389 do { 1390 if (rsp & ATC_SRCH_HIT) { 1391 mt7530_fdb_read(priv, &_fdb); 1392 if (_fdb.port_mask & BIT(port)) { 1393 ret = cb(_fdb.mac, _fdb.vid, _fdb.noarp, 1394 data); 1395 if (ret < 0) 1396 break; 1397 } 1398 } 1399 } while (--cnt && 1400 !(rsp & ATC_SRCH_END) && 1401 !mt7530_fdb_cmd(priv, MT7530_FDB_NEXT, &rsp)); 1402 err: 1403 mutex_unlock(&priv->reg_mutex); 1404 1405 return 0; 1406 } 1407 1408 static int 1409 mt7530_port_mdb_add(struct dsa_switch *ds, int port, 1410 const struct switchdev_obj_port_mdb *mdb) 1411 { 1412 struct mt7530_priv *priv = ds->priv; 1413 const u8 *addr = mdb->addr; 1414 u16 vid = mdb->vid; 1415 u8 port_mask = 0; 1416 int ret; 1417 1418 mutex_lock(&priv->reg_mutex); 1419 1420 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP); 1421 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL)) 1422 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP) 1423 & PORT_MAP_MASK; 1424 1425 port_mask |= BIT(port); 1426 mt7530_fdb_write(priv, vid, port_mask, addr, -1, STATIC_ENT); 1427 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); 1428 1429 mutex_unlock(&priv->reg_mutex); 1430 1431 return ret; 1432 } 1433 1434 static int 1435 mt7530_port_mdb_del(struct dsa_switch *ds, int port, 1436 const struct switchdev_obj_port_mdb *mdb) 1437 { 1438 struct mt7530_priv *priv = ds->priv; 1439 const u8 *addr = mdb->addr; 1440 u16 vid = mdb->vid; 1441 u8 port_mask = 0; 1442 int ret; 1443 1444 mutex_lock(&priv->reg_mutex); 1445 1446 mt7530_fdb_write(priv, vid, 0, addr, 0, STATIC_EMP); 1447 if (!mt7530_fdb_cmd(priv, MT7530_FDB_READ, NULL)) 1448 port_mask = (mt7530_read(priv, MT7530_ATRD) >> PORT_MAP) 1449 & PORT_MAP_MASK; 1450 1451 port_mask &= ~BIT(port); 1452 mt7530_fdb_write(priv, vid, port_mask, addr, -1, 1453 port_mask ? STATIC_ENT : STATIC_EMP); 1454 ret = mt7530_fdb_cmd(priv, MT7530_FDB_WRITE, NULL); 1455 1456 mutex_unlock(&priv->reg_mutex); 1457 1458 return ret; 1459 } 1460 1461 static int 1462 mt7530_vlan_cmd(struct mt7530_priv *priv, enum mt7530_vlan_cmd cmd, u16 vid) 1463 { 1464 struct mt7530_dummy_poll p; 1465 u32 val; 1466 int ret; 1467 1468 val = VTCR_BUSY | VTCR_FUNC(cmd) | vid; 1469 mt7530_write(priv, MT7530_VTCR, val); 1470 1471 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_VTCR); 1472 ret = readx_poll_timeout(_mt7530_read, &p, val, 1473 !(val & VTCR_BUSY), 20, 20000); 1474 if (ret < 0) { 1475 dev_err(priv->dev, "poll timeout\n"); 1476 return ret; 1477 } 1478 1479 val = mt7530_read(priv, MT7530_VTCR); 1480 if (val & VTCR_INVALID) { 1481 dev_err(priv->dev, "read VTCR invalid\n"); 1482 return -EINVAL; 1483 } 1484 1485 return 0; 1486 } 1487 1488 static int 1489 mt7530_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering, 1490 struct netlink_ext_ack *extack) 1491 { 1492 if (vlan_filtering) { 1493 /* The port is being kept as VLAN-unaware port when bridge is 1494 * set up with vlan_filtering not being set, Otherwise, the 1495 * port and the corresponding CPU port is required the setup 1496 * for becoming a VLAN-aware port. 1497 */ 1498 mt7530_port_set_vlan_aware(ds, port); 1499 mt7530_port_set_vlan_aware(ds, MT7530_CPU_PORT); 1500 } else { 1501 mt7530_port_set_vlan_unaware(ds, port); 1502 } 1503 1504 return 0; 1505 } 1506 1507 static void 1508 mt7530_hw_vlan_add(struct mt7530_priv *priv, 1509 struct mt7530_hw_vlan_entry *entry) 1510 { 1511 u8 new_members; 1512 u32 val; 1513 1514 new_members = entry->old_members | BIT(entry->port) | 1515 BIT(MT7530_CPU_PORT); 1516 1517 /* Validate the entry with independent learning, create egress tag per 1518 * VLAN and joining the port as one of the port members. 1519 */ 1520 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | VLAN_VALID; 1521 mt7530_write(priv, MT7530_VAWD1, val); 1522 1523 /* Decide whether adding tag or not for those outgoing packets from the 1524 * port inside the VLAN. 1525 */ 1526 val = entry->untagged ? MT7530_VLAN_EGRESS_UNTAG : 1527 MT7530_VLAN_EGRESS_TAG; 1528 mt7530_rmw(priv, MT7530_VAWD2, 1529 ETAG_CTRL_P_MASK(entry->port), 1530 ETAG_CTRL_P(entry->port, val)); 1531 1532 /* CPU port is always taken as a tagged port for serving more than one 1533 * VLANs across and also being applied with egress type stack mode for 1534 * that VLAN tags would be appended after hardware special tag used as 1535 * DSA tag. 1536 */ 1537 mt7530_rmw(priv, MT7530_VAWD2, 1538 ETAG_CTRL_P_MASK(MT7530_CPU_PORT), 1539 ETAG_CTRL_P(MT7530_CPU_PORT, 1540 MT7530_VLAN_EGRESS_STACK)); 1541 } 1542 1543 static void 1544 mt7530_hw_vlan_del(struct mt7530_priv *priv, 1545 struct mt7530_hw_vlan_entry *entry) 1546 { 1547 u8 new_members; 1548 u32 val; 1549 1550 new_members = entry->old_members & ~BIT(entry->port); 1551 1552 val = mt7530_read(priv, MT7530_VAWD1); 1553 if (!(val & VLAN_VALID)) { 1554 dev_err(priv->dev, 1555 "Cannot be deleted due to invalid entry\n"); 1556 return; 1557 } 1558 1559 /* If certain member apart from CPU port is still alive in the VLAN, 1560 * the entry would be kept valid. Otherwise, the entry is got to be 1561 * disabled. 1562 */ 1563 if (new_members && new_members != BIT(MT7530_CPU_PORT)) { 1564 val = IVL_MAC | VTAG_EN | PORT_MEM(new_members) | 1565 VLAN_VALID; 1566 mt7530_write(priv, MT7530_VAWD1, val); 1567 } else { 1568 mt7530_write(priv, MT7530_VAWD1, 0); 1569 mt7530_write(priv, MT7530_VAWD2, 0); 1570 } 1571 } 1572 1573 static void 1574 mt7530_hw_vlan_update(struct mt7530_priv *priv, u16 vid, 1575 struct mt7530_hw_vlan_entry *entry, 1576 mt7530_vlan_op vlan_op) 1577 { 1578 u32 val; 1579 1580 /* Fetch entry */ 1581 mt7530_vlan_cmd(priv, MT7530_VTCR_RD_VID, vid); 1582 1583 val = mt7530_read(priv, MT7530_VAWD1); 1584 1585 entry->old_members = (val >> PORT_MEM_SHFT) & PORT_MEM_MASK; 1586 1587 /* Manipulate entry */ 1588 vlan_op(priv, entry); 1589 1590 /* Flush result to hardware */ 1591 mt7530_vlan_cmd(priv, MT7530_VTCR_WR_VID, vid); 1592 } 1593 1594 static int 1595 mt7530_port_vlan_add(struct dsa_switch *ds, int port, 1596 const struct switchdev_obj_port_vlan *vlan, 1597 struct netlink_ext_ack *extack) 1598 { 1599 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED; 1600 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID; 1601 struct mt7530_hw_vlan_entry new_entry; 1602 struct mt7530_priv *priv = ds->priv; 1603 1604 mutex_lock(&priv->reg_mutex); 1605 1606 mt7530_hw_vlan_entry_init(&new_entry, port, untagged); 1607 mt7530_hw_vlan_update(priv, vlan->vid, &new_entry, mt7530_hw_vlan_add); 1608 1609 if (pvid) { 1610 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, 1611 G0_PORT_VID(vlan->vid)); 1612 priv->ports[port].pvid = vlan->vid; 1613 } 1614 1615 mutex_unlock(&priv->reg_mutex); 1616 1617 return 0; 1618 } 1619 1620 static int 1621 mt7530_port_vlan_del(struct dsa_switch *ds, int port, 1622 const struct switchdev_obj_port_vlan *vlan) 1623 { 1624 struct mt7530_hw_vlan_entry target_entry; 1625 struct mt7530_priv *priv = ds->priv; 1626 u16 pvid; 1627 1628 mutex_lock(&priv->reg_mutex); 1629 1630 pvid = priv->ports[port].pvid; 1631 mt7530_hw_vlan_entry_init(&target_entry, port, 0); 1632 mt7530_hw_vlan_update(priv, vlan->vid, &target_entry, 1633 mt7530_hw_vlan_del); 1634 1635 /* PVID is being restored to the default whenever the PVID port 1636 * is being removed from the VLAN. 1637 */ 1638 if (pvid == vlan->vid) 1639 pvid = G0_PORT_VID_DEF; 1640 1641 mt7530_rmw(priv, MT7530_PPBV1_P(port), G0_PORT_VID_MASK, pvid); 1642 priv->ports[port].pvid = pvid; 1643 1644 mutex_unlock(&priv->reg_mutex); 1645 1646 return 0; 1647 } 1648 1649 static int mt753x_mirror_port_get(unsigned int id, u32 val) 1650 { 1651 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_GET(val) : 1652 MIRROR_PORT(val); 1653 } 1654 1655 static int mt753x_mirror_port_set(unsigned int id, u32 val) 1656 { 1657 return (id == ID_MT7531) ? MT7531_MIRROR_PORT_SET(val) : 1658 MIRROR_PORT(val); 1659 } 1660 1661 static int mt753x_port_mirror_add(struct dsa_switch *ds, int port, 1662 struct dsa_mall_mirror_tc_entry *mirror, 1663 bool ingress) 1664 { 1665 struct mt7530_priv *priv = ds->priv; 1666 int monitor_port; 1667 u32 val; 1668 1669 /* Check for existent entry */ 1670 if ((ingress ? priv->mirror_rx : priv->mirror_tx) & BIT(port)) 1671 return -EEXIST; 1672 1673 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id)); 1674 1675 /* MT7530 only supports one monitor port */ 1676 monitor_port = mt753x_mirror_port_get(priv->id, val); 1677 if (val & MT753X_MIRROR_EN(priv->id) && 1678 monitor_port != mirror->to_local_port) 1679 return -EEXIST; 1680 1681 val |= MT753X_MIRROR_EN(priv->id); 1682 val &= ~MT753X_MIRROR_MASK(priv->id); 1683 val |= mt753x_mirror_port_set(priv->id, mirror->to_local_port); 1684 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val); 1685 1686 val = mt7530_read(priv, MT7530_PCR_P(port)); 1687 if (ingress) { 1688 val |= PORT_RX_MIR; 1689 priv->mirror_rx |= BIT(port); 1690 } else { 1691 val |= PORT_TX_MIR; 1692 priv->mirror_tx |= BIT(port); 1693 } 1694 mt7530_write(priv, MT7530_PCR_P(port), val); 1695 1696 return 0; 1697 } 1698 1699 static void mt753x_port_mirror_del(struct dsa_switch *ds, int port, 1700 struct dsa_mall_mirror_tc_entry *mirror) 1701 { 1702 struct mt7530_priv *priv = ds->priv; 1703 u32 val; 1704 1705 val = mt7530_read(priv, MT7530_PCR_P(port)); 1706 if (mirror->ingress) { 1707 val &= ~PORT_RX_MIR; 1708 priv->mirror_rx &= ~BIT(port); 1709 } else { 1710 val &= ~PORT_TX_MIR; 1711 priv->mirror_tx &= ~BIT(port); 1712 } 1713 mt7530_write(priv, MT7530_PCR_P(port), val); 1714 1715 if (!priv->mirror_rx && !priv->mirror_tx) { 1716 val = mt7530_read(priv, MT753X_MIRROR_REG(priv->id)); 1717 val &= ~MT753X_MIRROR_EN(priv->id); 1718 mt7530_write(priv, MT753X_MIRROR_REG(priv->id), val); 1719 } 1720 } 1721 1722 static enum dsa_tag_protocol 1723 mtk_get_tag_protocol(struct dsa_switch *ds, int port, 1724 enum dsa_tag_protocol mp) 1725 { 1726 struct mt7530_priv *priv = ds->priv; 1727 1728 if (port != MT7530_CPU_PORT) { 1729 dev_warn(priv->dev, 1730 "port not matched with tagging CPU port\n"); 1731 return DSA_TAG_PROTO_NONE; 1732 } else { 1733 return DSA_TAG_PROTO_MTK; 1734 } 1735 } 1736 1737 #ifdef CONFIG_GPIOLIB 1738 static inline u32 1739 mt7530_gpio_to_bit(unsigned int offset) 1740 { 1741 /* Map GPIO offset to register bit 1742 * [ 2: 0] port 0 LED 0..2 as GPIO 0..2 1743 * [ 6: 4] port 1 LED 0..2 as GPIO 3..5 1744 * [10: 8] port 2 LED 0..2 as GPIO 6..8 1745 * [14:12] port 3 LED 0..2 as GPIO 9..11 1746 * [18:16] port 4 LED 0..2 as GPIO 12..14 1747 */ 1748 return BIT(offset + offset / 3); 1749 } 1750 1751 static int 1752 mt7530_gpio_get(struct gpio_chip *gc, unsigned int offset) 1753 { 1754 struct mt7530_priv *priv = gpiochip_get_data(gc); 1755 u32 bit = mt7530_gpio_to_bit(offset); 1756 1757 return !!(mt7530_read(priv, MT7530_LED_GPIO_DATA) & bit); 1758 } 1759 1760 static void 1761 mt7530_gpio_set(struct gpio_chip *gc, unsigned int offset, int value) 1762 { 1763 struct mt7530_priv *priv = gpiochip_get_data(gc); 1764 u32 bit = mt7530_gpio_to_bit(offset); 1765 1766 if (value) 1767 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit); 1768 else 1769 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit); 1770 } 1771 1772 static int 1773 mt7530_gpio_get_direction(struct gpio_chip *gc, unsigned int offset) 1774 { 1775 struct mt7530_priv *priv = gpiochip_get_data(gc); 1776 u32 bit = mt7530_gpio_to_bit(offset); 1777 1778 return (mt7530_read(priv, MT7530_LED_GPIO_DIR) & bit) ? 1779 GPIO_LINE_DIRECTION_OUT : GPIO_LINE_DIRECTION_IN; 1780 } 1781 1782 static int 1783 mt7530_gpio_direction_input(struct gpio_chip *gc, unsigned int offset) 1784 { 1785 struct mt7530_priv *priv = gpiochip_get_data(gc); 1786 u32 bit = mt7530_gpio_to_bit(offset); 1787 1788 mt7530_clear(priv, MT7530_LED_GPIO_OE, bit); 1789 mt7530_clear(priv, MT7530_LED_GPIO_DIR, bit); 1790 1791 return 0; 1792 } 1793 1794 static int 1795 mt7530_gpio_direction_output(struct gpio_chip *gc, unsigned int offset, int value) 1796 { 1797 struct mt7530_priv *priv = gpiochip_get_data(gc); 1798 u32 bit = mt7530_gpio_to_bit(offset); 1799 1800 mt7530_set(priv, MT7530_LED_GPIO_DIR, bit); 1801 1802 if (value) 1803 mt7530_set(priv, MT7530_LED_GPIO_DATA, bit); 1804 else 1805 mt7530_clear(priv, MT7530_LED_GPIO_DATA, bit); 1806 1807 mt7530_set(priv, MT7530_LED_GPIO_OE, bit); 1808 1809 return 0; 1810 } 1811 1812 static int 1813 mt7530_setup_gpio(struct mt7530_priv *priv) 1814 { 1815 struct device *dev = priv->dev; 1816 struct gpio_chip *gc; 1817 1818 gc = devm_kzalloc(dev, sizeof(*gc), GFP_KERNEL); 1819 if (!gc) 1820 return -ENOMEM; 1821 1822 mt7530_write(priv, MT7530_LED_GPIO_OE, 0); 1823 mt7530_write(priv, MT7530_LED_GPIO_DIR, 0); 1824 mt7530_write(priv, MT7530_LED_IO_MODE, 0); 1825 1826 gc->label = "mt7530"; 1827 gc->parent = dev; 1828 gc->owner = THIS_MODULE; 1829 gc->get_direction = mt7530_gpio_get_direction; 1830 gc->direction_input = mt7530_gpio_direction_input; 1831 gc->direction_output = mt7530_gpio_direction_output; 1832 gc->get = mt7530_gpio_get; 1833 gc->set = mt7530_gpio_set; 1834 gc->base = -1; 1835 gc->ngpio = 15; 1836 gc->can_sleep = true; 1837 1838 return devm_gpiochip_add_data(dev, gc, priv); 1839 } 1840 #endif /* CONFIG_GPIOLIB */ 1841 1842 static irqreturn_t 1843 mt7530_irq_thread_fn(int irq, void *dev_id) 1844 { 1845 struct mt7530_priv *priv = dev_id; 1846 bool handled = false; 1847 u32 val; 1848 int p; 1849 1850 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED); 1851 val = mt7530_mii_read(priv, MT7530_SYS_INT_STS); 1852 mt7530_mii_write(priv, MT7530_SYS_INT_STS, val); 1853 mutex_unlock(&priv->bus->mdio_lock); 1854 1855 for (p = 0; p < MT7530_NUM_PHYS; p++) { 1856 if (BIT(p) & val) { 1857 unsigned int irq; 1858 1859 irq = irq_find_mapping(priv->irq_domain, p); 1860 handle_nested_irq(irq); 1861 handled = true; 1862 } 1863 } 1864 1865 return IRQ_RETVAL(handled); 1866 } 1867 1868 static void 1869 mt7530_irq_mask(struct irq_data *d) 1870 { 1871 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d); 1872 1873 priv->irq_enable &= ~BIT(d->hwirq); 1874 } 1875 1876 static void 1877 mt7530_irq_unmask(struct irq_data *d) 1878 { 1879 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d); 1880 1881 priv->irq_enable |= BIT(d->hwirq); 1882 } 1883 1884 static void 1885 mt7530_irq_bus_lock(struct irq_data *d) 1886 { 1887 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d); 1888 1889 mutex_lock_nested(&priv->bus->mdio_lock, MDIO_MUTEX_NESTED); 1890 } 1891 1892 static void 1893 mt7530_irq_bus_sync_unlock(struct irq_data *d) 1894 { 1895 struct mt7530_priv *priv = irq_data_get_irq_chip_data(d); 1896 1897 mt7530_mii_write(priv, MT7530_SYS_INT_EN, priv->irq_enable); 1898 mutex_unlock(&priv->bus->mdio_lock); 1899 } 1900 1901 static struct irq_chip mt7530_irq_chip = { 1902 .name = KBUILD_MODNAME, 1903 .irq_mask = mt7530_irq_mask, 1904 .irq_unmask = mt7530_irq_unmask, 1905 .irq_bus_lock = mt7530_irq_bus_lock, 1906 .irq_bus_sync_unlock = mt7530_irq_bus_sync_unlock, 1907 }; 1908 1909 static int 1910 mt7530_irq_map(struct irq_domain *domain, unsigned int irq, 1911 irq_hw_number_t hwirq) 1912 { 1913 irq_set_chip_data(irq, domain->host_data); 1914 irq_set_chip_and_handler(irq, &mt7530_irq_chip, handle_simple_irq); 1915 irq_set_nested_thread(irq, true); 1916 irq_set_noprobe(irq); 1917 1918 return 0; 1919 } 1920 1921 static const struct irq_domain_ops mt7530_irq_domain_ops = { 1922 .map = mt7530_irq_map, 1923 .xlate = irq_domain_xlate_onecell, 1924 }; 1925 1926 static void 1927 mt7530_setup_mdio_irq(struct mt7530_priv *priv) 1928 { 1929 struct dsa_switch *ds = priv->ds; 1930 int p; 1931 1932 for (p = 0; p < MT7530_NUM_PHYS; p++) { 1933 if (BIT(p) & ds->phys_mii_mask) { 1934 unsigned int irq; 1935 1936 irq = irq_create_mapping(priv->irq_domain, p); 1937 ds->slave_mii_bus->irq[p] = irq; 1938 } 1939 } 1940 } 1941 1942 static int 1943 mt7530_setup_irq(struct mt7530_priv *priv) 1944 { 1945 struct device *dev = priv->dev; 1946 struct device_node *np = dev->of_node; 1947 int ret; 1948 1949 if (!of_property_read_bool(np, "interrupt-controller")) { 1950 dev_info(dev, "no interrupt support\n"); 1951 return 0; 1952 } 1953 1954 priv->irq = of_irq_get(np, 0); 1955 if (priv->irq <= 0) { 1956 dev_err(dev, "failed to get parent IRQ: %d\n", priv->irq); 1957 return priv->irq ? : -EINVAL; 1958 } 1959 1960 priv->irq_domain = irq_domain_add_linear(np, MT7530_NUM_PHYS, 1961 &mt7530_irq_domain_ops, priv); 1962 if (!priv->irq_domain) { 1963 dev_err(dev, "failed to create IRQ domain\n"); 1964 return -ENOMEM; 1965 } 1966 1967 /* This register must be set for MT7530 to properly fire interrupts */ 1968 if (priv->id != ID_MT7531) 1969 mt7530_set(priv, MT7530_TOP_SIG_CTRL, TOP_SIG_CTRL_NORMAL); 1970 1971 ret = request_threaded_irq(priv->irq, NULL, mt7530_irq_thread_fn, 1972 IRQF_ONESHOT, KBUILD_MODNAME, priv); 1973 if (ret) { 1974 irq_domain_remove(priv->irq_domain); 1975 dev_err(dev, "failed to request IRQ: %d\n", ret); 1976 return ret; 1977 } 1978 1979 return 0; 1980 } 1981 1982 static void 1983 mt7530_free_mdio_irq(struct mt7530_priv *priv) 1984 { 1985 int p; 1986 1987 for (p = 0; p < MT7530_NUM_PHYS; p++) { 1988 if (BIT(p) & priv->ds->phys_mii_mask) { 1989 unsigned int irq; 1990 1991 irq = irq_find_mapping(priv->irq_domain, p); 1992 irq_dispose_mapping(irq); 1993 } 1994 } 1995 } 1996 1997 static void 1998 mt7530_free_irq_common(struct mt7530_priv *priv) 1999 { 2000 free_irq(priv->irq, priv); 2001 irq_domain_remove(priv->irq_domain); 2002 } 2003 2004 static void 2005 mt7530_free_irq(struct mt7530_priv *priv) 2006 { 2007 mt7530_free_mdio_irq(priv); 2008 mt7530_free_irq_common(priv); 2009 } 2010 2011 static int 2012 mt7530_setup_mdio(struct mt7530_priv *priv) 2013 { 2014 struct dsa_switch *ds = priv->ds; 2015 struct device *dev = priv->dev; 2016 struct mii_bus *bus; 2017 static int idx; 2018 int ret; 2019 2020 bus = devm_mdiobus_alloc(dev); 2021 if (!bus) 2022 return -ENOMEM; 2023 2024 ds->slave_mii_bus = bus; 2025 bus->priv = priv; 2026 bus->name = KBUILD_MODNAME "-mii"; 2027 snprintf(bus->id, MII_BUS_ID_SIZE, KBUILD_MODNAME "-%d", idx++); 2028 bus->read = mt753x_phy_read; 2029 bus->write = mt753x_phy_write; 2030 bus->parent = dev; 2031 bus->phy_mask = ~ds->phys_mii_mask; 2032 2033 if (priv->irq) 2034 mt7530_setup_mdio_irq(priv); 2035 2036 ret = mdiobus_register(bus); 2037 if (ret) { 2038 dev_err(dev, "failed to register MDIO bus: %d\n", ret); 2039 if (priv->irq) 2040 mt7530_free_mdio_irq(priv); 2041 } 2042 2043 return ret; 2044 } 2045 2046 static int 2047 mt7530_setup(struct dsa_switch *ds) 2048 { 2049 struct mt7530_priv *priv = ds->priv; 2050 struct device_node *phy_node; 2051 struct device_node *mac_np; 2052 struct mt7530_dummy_poll p; 2053 phy_interface_t interface; 2054 struct device_node *dn; 2055 u32 id, val; 2056 int ret, i; 2057 2058 /* The parent node of master netdev which holds the common system 2059 * controller also is the container for two GMACs nodes representing 2060 * as two netdev instances. 2061 */ 2062 dn = dsa_to_port(ds, MT7530_CPU_PORT)->master->dev.of_node->parent; 2063 ds->mtu_enforcement_ingress = true; 2064 2065 if (priv->id == ID_MT7530) { 2066 regulator_set_voltage(priv->core_pwr, 1000000, 1000000); 2067 ret = regulator_enable(priv->core_pwr); 2068 if (ret < 0) { 2069 dev_err(priv->dev, 2070 "Failed to enable core power: %d\n", ret); 2071 return ret; 2072 } 2073 2074 regulator_set_voltage(priv->io_pwr, 3300000, 3300000); 2075 ret = regulator_enable(priv->io_pwr); 2076 if (ret < 0) { 2077 dev_err(priv->dev, "Failed to enable io pwr: %d\n", 2078 ret); 2079 return ret; 2080 } 2081 } 2082 2083 /* Reset whole chip through gpio pin or memory-mapped registers for 2084 * different type of hardware 2085 */ 2086 if (priv->mcm) { 2087 reset_control_assert(priv->rstc); 2088 usleep_range(1000, 1100); 2089 reset_control_deassert(priv->rstc); 2090 } else { 2091 gpiod_set_value_cansleep(priv->reset, 0); 2092 usleep_range(1000, 1100); 2093 gpiod_set_value_cansleep(priv->reset, 1); 2094 } 2095 2096 /* Waiting for MT7530 got to stable */ 2097 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP); 2098 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0, 2099 20, 1000000); 2100 if (ret < 0) { 2101 dev_err(priv->dev, "reset timeout\n"); 2102 return ret; 2103 } 2104 2105 id = mt7530_read(priv, MT7530_CREV); 2106 id >>= CHIP_NAME_SHIFT; 2107 if (id != MT7530_ID) { 2108 dev_err(priv->dev, "chip %x can't be supported\n", id); 2109 return -ENODEV; 2110 } 2111 2112 /* Reset the switch through internal reset */ 2113 mt7530_write(priv, MT7530_SYS_CTRL, 2114 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST | 2115 SYS_CTRL_REG_RST); 2116 2117 /* Enable Port 6 only; P5 as GMAC5 which currently is not supported */ 2118 val = mt7530_read(priv, MT7530_MHWTRAP); 2119 val &= ~MHWTRAP_P6_DIS & ~MHWTRAP_PHY_ACCESS; 2120 val |= MHWTRAP_MANUAL; 2121 mt7530_write(priv, MT7530_MHWTRAP, val); 2122 2123 priv->p6_interface = PHY_INTERFACE_MODE_NA; 2124 2125 /* Enable and reset MIB counters */ 2126 mt7530_mib_reset(ds); 2127 2128 for (i = 0; i < MT7530_NUM_PORTS; i++) { 2129 /* Disable forwarding by default on all ports */ 2130 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK, 2131 PCR_MATRIX_CLR); 2132 2133 if (dsa_is_cpu_port(ds, i)) { 2134 ret = mt753x_cpu_port_enable(ds, i); 2135 if (ret) 2136 return ret; 2137 } else { 2138 mt7530_port_disable(ds, i); 2139 2140 /* Disable learning by default on all user ports */ 2141 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS); 2142 } 2143 /* Enable consistent egress tag */ 2144 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK, 2145 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); 2146 } 2147 2148 /* Setup port 5 */ 2149 priv->p5_intf_sel = P5_DISABLED; 2150 interface = PHY_INTERFACE_MODE_NA; 2151 2152 if (!dsa_is_unused_port(ds, 5)) { 2153 priv->p5_intf_sel = P5_INTF_SEL_GMAC5; 2154 ret = of_get_phy_mode(dsa_to_port(ds, 5)->dn, &interface); 2155 if (ret && ret != -ENODEV) 2156 return ret; 2157 } else { 2158 /* Scan the ethernet nodes. look for GMAC1, lookup used phy */ 2159 for_each_child_of_node(dn, mac_np) { 2160 if (!of_device_is_compatible(mac_np, 2161 "mediatek,eth-mac")) 2162 continue; 2163 2164 ret = of_property_read_u32(mac_np, "reg", &id); 2165 if (ret < 0 || id != 1) 2166 continue; 2167 2168 phy_node = of_parse_phandle(mac_np, "phy-handle", 0); 2169 if (!phy_node) 2170 continue; 2171 2172 if (phy_node->parent == priv->dev->of_node->parent) { 2173 ret = of_get_phy_mode(mac_np, &interface); 2174 if (ret && ret != -ENODEV) { 2175 of_node_put(mac_np); 2176 return ret; 2177 } 2178 id = of_mdio_parse_addr(ds->dev, phy_node); 2179 if (id == 0) 2180 priv->p5_intf_sel = P5_INTF_SEL_PHY_P0; 2181 if (id == 4) 2182 priv->p5_intf_sel = P5_INTF_SEL_PHY_P4; 2183 } 2184 of_node_put(mac_np); 2185 of_node_put(phy_node); 2186 break; 2187 } 2188 } 2189 2190 #ifdef CONFIG_GPIOLIB 2191 if (of_property_read_bool(priv->dev->of_node, "gpio-controller")) { 2192 ret = mt7530_setup_gpio(priv); 2193 if (ret) 2194 return ret; 2195 } 2196 #endif /* CONFIG_GPIOLIB */ 2197 2198 mt7530_setup_port5(ds, interface); 2199 2200 /* Flush the FDB table */ 2201 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); 2202 if (ret < 0) 2203 return ret; 2204 2205 return 0; 2206 } 2207 2208 static int 2209 mt7531_setup(struct dsa_switch *ds) 2210 { 2211 struct mt7530_priv *priv = ds->priv; 2212 struct mt7530_dummy_poll p; 2213 u32 val, id; 2214 int ret, i; 2215 2216 /* Reset whole chip through gpio pin or memory-mapped registers for 2217 * different type of hardware 2218 */ 2219 if (priv->mcm) { 2220 reset_control_assert(priv->rstc); 2221 usleep_range(1000, 1100); 2222 reset_control_deassert(priv->rstc); 2223 } else { 2224 gpiod_set_value_cansleep(priv->reset, 0); 2225 usleep_range(1000, 1100); 2226 gpiod_set_value_cansleep(priv->reset, 1); 2227 } 2228 2229 /* Waiting for MT7530 got to stable */ 2230 INIT_MT7530_DUMMY_POLL(&p, priv, MT7530_HWTRAP); 2231 ret = readx_poll_timeout(_mt7530_read, &p, val, val != 0, 2232 20, 1000000); 2233 if (ret < 0) { 2234 dev_err(priv->dev, "reset timeout\n"); 2235 return ret; 2236 } 2237 2238 id = mt7530_read(priv, MT7531_CREV); 2239 id >>= CHIP_NAME_SHIFT; 2240 2241 if (id != MT7531_ID) { 2242 dev_err(priv->dev, "chip %x can't be supported\n", id); 2243 return -ENODEV; 2244 } 2245 2246 /* Reset the switch through internal reset */ 2247 mt7530_write(priv, MT7530_SYS_CTRL, 2248 SYS_CTRL_PHY_RST | SYS_CTRL_SW_RST | 2249 SYS_CTRL_REG_RST); 2250 2251 if (mt7531_dual_sgmii_supported(priv)) { 2252 priv->p5_intf_sel = P5_INTF_SEL_GMAC5_SGMII; 2253 2254 /* Let ds->slave_mii_bus be able to access external phy. */ 2255 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO11_RG_RXD2_MASK, 2256 MT7531_EXT_P_MDC_11); 2257 mt7530_rmw(priv, MT7531_GPIO_MODE1, MT7531_GPIO12_RG_RXD3_MASK, 2258 MT7531_EXT_P_MDIO_12); 2259 } else { 2260 priv->p5_intf_sel = P5_INTF_SEL_GMAC5; 2261 } 2262 dev_dbg(ds->dev, "P5 support %s interface\n", 2263 p5_intf_modes(priv->p5_intf_sel)); 2264 2265 mt7530_rmw(priv, MT7531_GPIO_MODE0, MT7531_GPIO0_MASK, 2266 MT7531_GPIO0_INTERRUPT); 2267 2268 /* Let phylink decide the interface later. */ 2269 priv->p5_interface = PHY_INTERFACE_MODE_NA; 2270 priv->p6_interface = PHY_INTERFACE_MODE_NA; 2271 2272 /* Enable PHY core PLL, since phy_device has not yet been created 2273 * provided for phy_[read,write]_mmd_indirect is called, we provide 2274 * our own mt7531_ind_mmd_phy_[read,write] to complete this 2275 * function. 2276 */ 2277 val = mt7531_ind_c45_phy_read(priv, MT753X_CTRL_PHY_ADDR, 2278 MDIO_MMD_VEND2, CORE_PLL_GROUP4); 2279 val |= MT7531_PHY_PLL_BYPASS_MODE; 2280 val &= ~MT7531_PHY_PLL_OFF; 2281 mt7531_ind_c45_phy_write(priv, MT753X_CTRL_PHY_ADDR, MDIO_MMD_VEND2, 2282 CORE_PLL_GROUP4, val); 2283 2284 /* BPDU to CPU port */ 2285 mt7530_rmw(priv, MT7531_CFC, MT7531_CPU_PMAP_MASK, 2286 BIT(MT7530_CPU_PORT)); 2287 mt7530_rmw(priv, MT753X_BPC, MT753X_BPDU_PORT_FW_MASK, 2288 MT753X_BPDU_CPU_ONLY); 2289 2290 /* Enable and reset MIB counters */ 2291 mt7530_mib_reset(ds); 2292 2293 for (i = 0; i < MT7530_NUM_PORTS; i++) { 2294 /* Disable forwarding by default on all ports */ 2295 mt7530_rmw(priv, MT7530_PCR_P(i), PCR_MATRIX_MASK, 2296 PCR_MATRIX_CLR); 2297 2298 mt7530_set(priv, MT7531_DBG_CNT(i), MT7531_DIS_CLR); 2299 2300 if (dsa_is_cpu_port(ds, i)) { 2301 ret = mt753x_cpu_port_enable(ds, i); 2302 if (ret) 2303 return ret; 2304 } else { 2305 mt7530_port_disable(ds, i); 2306 2307 /* Disable learning by default on all user ports */ 2308 mt7530_set(priv, MT7530_PSC_P(i), SA_DIS); 2309 } 2310 2311 /* Enable consistent egress tag */ 2312 mt7530_rmw(priv, MT7530_PVC_P(i), PVC_EG_TAG_MASK, 2313 PVC_EG_TAG(MT7530_VLAN_EG_CONSISTENT)); 2314 } 2315 2316 ds->mtu_enforcement_ingress = true; 2317 2318 /* Flush the FDB table */ 2319 ret = mt7530_fdb_cmd(priv, MT7530_FDB_FLUSH, NULL); 2320 if (ret < 0) 2321 return ret; 2322 2323 return 0; 2324 } 2325 2326 static bool 2327 mt7530_phy_mode_supported(struct dsa_switch *ds, int port, 2328 const struct phylink_link_state *state) 2329 { 2330 struct mt7530_priv *priv = ds->priv; 2331 2332 switch (port) { 2333 case 0 ... 4: /* Internal phy */ 2334 if (state->interface != PHY_INTERFACE_MODE_GMII) 2335 return false; 2336 break; 2337 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */ 2338 if (!phy_interface_mode_is_rgmii(state->interface) && 2339 state->interface != PHY_INTERFACE_MODE_MII && 2340 state->interface != PHY_INTERFACE_MODE_GMII) 2341 return false; 2342 break; 2343 case 6: /* 1st cpu port */ 2344 if (state->interface != PHY_INTERFACE_MODE_RGMII && 2345 state->interface != PHY_INTERFACE_MODE_TRGMII) 2346 return false; 2347 break; 2348 default: 2349 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__, 2350 port); 2351 return false; 2352 } 2353 2354 return true; 2355 } 2356 2357 static bool mt7531_is_rgmii_port(struct mt7530_priv *priv, u32 port) 2358 { 2359 return (port == 5) && (priv->p5_intf_sel != P5_INTF_SEL_GMAC5_SGMII); 2360 } 2361 2362 static bool 2363 mt7531_phy_mode_supported(struct dsa_switch *ds, int port, 2364 const struct phylink_link_state *state) 2365 { 2366 struct mt7530_priv *priv = ds->priv; 2367 2368 switch (port) { 2369 case 0 ... 4: /* Internal phy */ 2370 if (state->interface != PHY_INTERFACE_MODE_GMII) 2371 return false; 2372 break; 2373 case 5: /* 2nd cpu port supports either rgmii or sgmii/8023z */ 2374 if (mt7531_is_rgmii_port(priv, port)) 2375 return phy_interface_mode_is_rgmii(state->interface); 2376 fallthrough; 2377 case 6: /* 1st cpu port supports sgmii/8023z only */ 2378 if (state->interface != PHY_INTERFACE_MODE_SGMII && 2379 !phy_interface_mode_is_8023z(state->interface)) 2380 return false; 2381 break; 2382 default: 2383 dev_err(priv->dev, "%s: unsupported port: %i\n", __func__, 2384 port); 2385 return false; 2386 } 2387 2388 return true; 2389 } 2390 2391 static bool 2392 mt753x_phy_mode_supported(struct dsa_switch *ds, int port, 2393 const struct phylink_link_state *state) 2394 { 2395 struct mt7530_priv *priv = ds->priv; 2396 2397 return priv->info->phy_mode_supported(ds, port, state); 2398 } 2399 2400 static int 2401 mt753x_pad_setup(struct dsa_switch *ds, const struct phylink_link_state *state) 2402 { 2403 struct mt7530_priv *priv = ds->priv; 2404 2405 return priv->info->pad_setup(ds, state->interface); 2406 } 2407 2408 static int 2409 mt7530_mac_config(struct dsa_switch *ds, int port, unsigned int mode, 2410 phy_interface_t interface) 2411 { 2412 struct mt7530_priv *priv = ds->priv; 2413 2414 /* Only need to setup port5. */ 2415 if (port != 5) 2416 return 0; 2417 2418 mt7530_setup_port5(priv->ds, interface); 2419 2420 return 0; 2421 } 2422 2423 static int mt7531_rgmii_setup(struct mt7530_priv *priv, u32 port, 2424 phy_interface_t interface, 2425 struct phy_device *phydev) 2426 { 2427 u32 val; 2428 2429 if (!mt7531_is_rgmii_port(priv, port)) { 2430 dev_err(priv->dev, "RGMII mode is not available for port %d\n", 2431 port); 2432 return -EINVAL; 2433 } 2434 2435 val = mt7530_read(priv, MT7531_CLKGEN_CTRL); 2436 val |= GP_CLK_EN; 2437 val &= ~GP_MODE_MASK; 2438 val |= GP_MODE(MT7531_GP_MODE_RGMII); 2439 val &= ~CLK_SKEW_IN_MASK; 2440 val |= CLK_SKEW_IN(MT7531_CLK_SKEW_NO_CHG); 2441 val &= ~CLK_SKEW_OUT_MASK; 2442 val |= CLK_SKEW_OUT(MT7531_CLK_SKEW_NO_CHG); 2443 val |= TXCLK_NO_REVERSE | RXCLK_NO_DELAY; 2444 2445 /* Do not adjust rgmii delay when vendor phy driver presents. */ 2446 if (!phydev || phy_driver_is_genphy(phydev)) { 2447 val &= ~(TXCLK_NO_REVERSE | RXCLK_NO_DELAY); 2448 switch (interface) { 2449 case PHY_INTERFACE_MODE_RGMII: 2450 val |= TXCLK_NO_REVERSE; 2451 val |= RXCLK_NO_DELAY; 2452 break; 2453 case PHY_INTERFACE_MODE_RGMII_RXID: 2454 val |= TXCLK_NO_REVERSE; 2455 break; 2456 case PHY_INTERFACE_MODE_RGMII_TXID: 2457 val |= RXCLK_NO_DELAY; 2458 break; 2459 case PHY_INTERFACE_MODE_RGMII_ID: 2460 break; 2461 default: 2462 return -EINVAL; 2463 } 2464 } 2465 mt7530_write(priv, MT7531_CLKGEN_CTRL, val); 2466 2467 return 0; 2468 } 2469 2470 static void mt7531_sgmii_validate(struct mt7530_priv *priv, int port, 2471 unsigned long *supported) 2472 { 2473 /* Port5 supports ethier RGMII or SGMII. 2474 * Port6 supports SGMII only. 2475 */ 2476 switch (port) { 2477 case 5: 2478 if (mt7531_is_rgmii_port(priv, port)) 2479 break; 2480 fallthrough; 2481 case 6: 2482 phylink_set(supported, 1000baseX_Full); 2483 phylink_set(supported, 2500baseX_Full); 2484 phylink_set(supported, 2500baseT_Full); 2485 } 2486 } 2487 2488 static void 2489 mt7531_sgmii_link_up_force(struct dsa_switch *ds, int port, 2490 unsigned int mode, phy_interface_t interface, 2491 int speed, int duplex) 2492 { 2493 struct mt7530_priv *priv = ds->priv; 2494 unsigned int val; 2495 2496 /* For adjusting speed and duplex of SGMII force mode. */ 2497 if (interface != PHY_INTERFACE_MODE_SGMII || 2498 phylink_autoneg_inband(mode)) 2499 return; 2500 2501 /* SGMII force mode setting */ 2502 val = mt7530_read(priv, MT7531_SGMII_MODE(port)); 2503 val &= ~MT7531_SGMII_IF_MODE_MASK; 2504 2505 switch (speed) { 2506 case SPEED_10: 2507 val |= MT7531_SGMII_FORCE_SPEED_10; 2508 break; 2509 case SPEED_100: 2510 val |= MT7531_SGMII_FORCE_SPEED_100; 2511 break; 2512 case SPEED_1000: 2513 val |= MT7531_SGMII_FORCE_SPEED_1000; 2514 break; 2515 } 2516 2517 /* MT7531 SGMII 1G force mode can only work in full duplex mode, 2518 * no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not. 2519 */ 2520 if ((speed == SPEED_10 || speed == SPEED_100) && 2521 duplex != DUPLEX_FULL) 2522 val |= MT7531_SGMII_FORCE_HALF_DUPLEX; 2523 2524 mt7530_write(priv, MT7531_SGMII_MODE(port), val); 2525 } 2526 2527 static bool mt753x_is_mac_port(u32 port) 2528 { 2529 return (port == 5 || port == 6); 2530 } 2531 2532 static int mt7531_sgmii_setup_mode_force(struct mt7530_priv *priv, u32 port, 2533 phy_interface_t interface) 2534 { 2535 u32 val; 2536 2537 if (!mt753x_is_mac_port(port)) 2538 return -EINVAL; 2539 2540 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 2541 MT7531_SGMII_PHYA_PWD); 2542 2543 val = mt7530_read(priv, MT7531_PHYA_CTRL_SIGNAL3(port)); 2544 val &= ~MT7531_RG_TPHY_SPEED_MASK; 2545 /* Setup 2.5 times faster clock for 2.5Gbps data speeds with 10B/8B 2546 * encoding. 2547 */ 2548 val |= (interface == PHY_INTERFACE_MODE_2500BASEX) ? 2549 MT7531_RG_TPHY_SPEED_3_125G : MT7531_RG_TPHY_SPEED_1_25G; 2550 mt7530_write(priv, MT7531_PHYA_CTRL_SIGNAL3(port), val); 2551 2552 mt7530_clear(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE); 2553 2554 /* MT7531 SGMII 1G and 2.5G force mode can only work in full duplex 2555 * mode, no matter MT7531_SGMII_FORCE_HALF_DUPLEX is set or not. 2556 */ 2557 mt7530_rmw(priv, MT7531_SGMII_MODE(port), 2558 MT7531_SGMII_IF_MODE_MASK | MT7531_SGMII_REMOTE_FAULT_DIS, 2559 MT7531_SGMII_FORCE_SPEED_1000); 2560 2561 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0); 2562 2563 return 0; 2564 } 2565 2566 static int mt7531_sgmii_setup_mode_an(struct mt7530_priv *priv, int port, 2567 phy_interface_t interface) 2568 { 2569 if (!mt753x_is_mac_port(port)) 2570 return -EINVAL; 2571 2572 mt7530_set(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 2573 MT7531_SGMII_PHYA_PWD); 2574 2575 mt7530_rmw(priv, MT7531_PHYA_CTRL_SIGNAL3(port), 2576 MT7531_RG_TPHY_SPEED_MASK, MT7531_RG_TPHY_SPEED_1_25G); 2577 2578 mt7530_set(priv, MT7531_SGMII_MODE(port), 2579 MT7531_SGMII_REMOTE_FAULT_DIS | 2580 MT7531_SGMII_SPEED_DUPLEX_AN); 2581 2582 mt7530_rmw(priv, MT7531_PCS_SPEED_ABILITY(port), 2583 MT7531_SGMII_TX_CONFIG_MASK, 1); 2584 2585 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_ENABLE); 2586 2587 mt7530_set(priv, MT7531_PCS_CONTROL_1(port), MT7531_SGMII_AN_RESTART); 2588 2589 mt7530_write(priv, MT7531_QPHY_PWR_STATE_CTRL(port), 0); 2590 2591 return 0; 2592 } 2593 2594 static void mt7531_sgmii_restart_an(struct dsa_switch *ds, int port) 2595 { 2596 struct mt7530_priv *priv = ds->priv; 2597 u32 val; 2598 2599 /* Only restart AN when AN is enabled */ 2600 val = mt7530_read(priv, MT7531_PCS_CONTROL_1(port)); 2601 if (val & MT7531_SGMII_AN_ENABLE) { 2602 val |= MT7531_SGMII_AN_RESTART; 2603 mt7530_write(priv, MT7531_PCS_CONTROL_1(port), val); 2604 } 2605 } 2606 2607 static int 2608 mt7531_mac_config(struct dsa_switch *ds, int port, unsigned int mode, 2609 phy_interface_t interface) 2610 { 2611 struct mt7530_priv *priv = ds->priv; 2612 struct phy_device *phydev; 2613 struct dsa_port *dp; 2614 2615 if (!mt753x_is_mac_port(port)) { 2616 dev_err(priv->dev, "port %d is not a MAC port\n", port); 2617 return -EINVAL; 2618 } 2619 2620 switch (interface) { 2621 case PHY_INTERFACE_MODE_RGMII: 2622 case PHY_INTERFACE_MODE_RGMII_ID: 2623 case PHY_INTERFACE_MODE_RGMII_RXID: 2624 case PHY_INTERFACE_MODE_RGMII_TXID: 2625 dp = dsa_to_port(ds, port); 2626 phydev = dp->slave->phydev; 2627 return mt7531_rgmii_setup(priv, port, interface, phydev); 2628 case PHY_INTERFACE_MODE_SGMII: 2629 return mt7531_sgmii_setup_mode_an(priv, port, interface); 2630 case PHY_INTERFACE_MODE_NA: 2631 case PHY_INTERFACE_MODE_1000BASEX: 2632 case PHY_INTERFACE_MODE_2500BASEX: 2633 if (phylink_autoneg_inband(mode)) 2634 return -EINVAL; 2635 2636 return mt7531_sgmii_setup_mode_force(priv, port, interface); 2637 default: 2638 return -EINVAL; 2639 } 2640 2641 return -EINVAL; 2642 } 2643 2644 static int 2645 mt753x_mac_config(struct dsa_switch *ds, int port, unsigned int mode, 2646 const struct phylink_link_state *state) 2647 { 2648 struct mt7530_priv *priv = ds->priv; 2649 2650 return priv->info->mac_port_config(ds, port, mode, state->interface); 2651 } 2652 2653 static void 2654 mt753x_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode, 2655 const struct phylink_link_state *state) 2656 { 2657 struct mt7530_priv *priv = ds->priv; 2658 u32 mcr_cur, mcr_new; 2659 2660 if (!mt753x_phy_mode_supported(ds, port, state)) 2661 goto unsupported; 2662 2663 switch (port) { 2664 case 0 ... 4: /* Internal phy */ 2665 if (state->interface != PHY_INTERFACE_MODE_GMII) 2666 goto unsupported; 2667 break; 2668 case 5: /* 2nd cpu port with phy of port 0 or 4 / external phy */ 2669 if (priv->p5_interface == state->interface) 2670 break; 2671 2672 if (mt753x_mac_config(ds, port, mode, state) < 0) 2673 goto unsupported; 2674 2675 if (priv->p5_intf_sel != P5_DISABLED) 2676 priv->p5_interface = state->interface; 2677 break; 2678 case 6: /* 1st cpu port */ 2679 if (priv->p6_interface == state->interface) 2680 break; 2681 2682 mt753x_pad_setup(ds, state); 2683 2684 if (mt753x_mac_config(ds, port, mode, state) < 0) 2685 goto unsupported; 2686 2687 priv->p6_interface = state->interface; 2688 break; 2689 default: 2690 unsupported: 2691 dev_err(ds->dev, "%s: unsupported %s port: %i\n", 2692 __func__, phy_modes(state->interface), port); 2693 return; 2694 } 2695 2696 if (phylink_autoneg_inband(mode) && 2697 state->interface != PHY_INTERFACE_MODE_SGMII) { 2698 dev_err(ds->dev, "%s: in-band negotiation unsupported\n", 2699 __func__); 2700 return; 2701 } 2702 2703 mcr_cur = mt7530_read(priv, MT7530_PMCR_P(port)); 2704 mcr_new = mcr_cur; 2705 mcr_new &= ~PMCR_LINK_SETTINGS_MASK; 2706 mcr_new |= PMCR_IFG_XMIT(1) | PMCR_MAC_MODE | PMCR_BACKOFF_EN | 2707 PMCR_BACKPR_EN | PMCR_FORCE_MODE_ID(priv->id); 2708 2709 /* Are we connected to external phy */ 2710 if (port == 5 && dsa_is_user_port(ds, 5)) 2711 mcr_new |= PMCR_EXT_PHY; 2712 2713 if (mcr_new != mcr_cur) 2714 mt7530_write(priv, MT7530_PMCR_P(port), mcr_new); 2715 } 2716 2717 static void 2718 mt753x_phylink_mac_an_restart(struct dsa_switch *ds, int port) 2719 { 2720 struct mt7530_priv *priv = ds->priv; 2721 2722 if (!priv->info->mac_pcs_an_restart) 2723 return; 2724 2725 priv->info->mac_pcs_an_restart(ds, port); 2726 } 2727 2728 static void mt753x_phylink_mac_link_down(struct dsa_switch *ds, int port, 2729 unsigned int mode, 2730 phy_interface_t interface) 2731 { 2732 struct mt7530_priv *priv = ds->priv; 2733 2734 mt7530_clear(priv, MT7530_PMCR_P(port), PMCR_LINK_SETTINGS_MASK); 2735 } 2736 2737 static void mt753x_mac_pcs_link_up(struct dsa_switch *ds, int port, 2738 unsigned int mode, phy_interface_t interface, 2739 int speed, int duplex) 2740 { 2741 struct mt7530_priv *priv = ds->priv; 2742 2743 if (!priv->info->mac_pcs_link_up) 2744 return; 2745 2746 priv->info->mac_pcs_link_up(ds, port, mode, interface, speed, duplex); 2747 } 2748 2749 static void mt753x_phylink_mac_link_up(struct dsa_switch *ds, int port, 2750 unsigned int mode, 2751 phy_interface_t interface, 2752 struct phy_device *phydev, 2753 int speed, int duplex, 2754 bool tx_pause, bool rx_pause) 2755 { 2756 struct mt7530_priv *priv = ds->priv; 2757 u32 mcr; 2758 2759 mt753x_mac_pcs_link_up(ds, port, mode, interface, speed, duplex); 2760 2761 mcr = PMCR_RX_EN | PMCR_TX_EN | PMCR_FORCE_LNK; 2762 2763 /* MT753x MAC works in 1G full duplex mode for all up-clocked 2764 * variants. 2765 */ 2766 if (interface == PHY_INTERFACE_MODE_TRGMII || 2767 (phy_interface_mode_is_8023z(interface))) { 2768 speed = SPEED_1000; 2769 duplex = DUPLEX_FULL; 2770 } 2771 2772 switch (speed) { 2773 case SPEED_1000: 2774 mcr |= PMCR_FORCE_SPEED_1000; 2775 break; 2776 case SPEED_100: 2777 mcr |= PMCR_FORCE_SPEED_100; 2778 break; 2779 } 2780 if (duplex == DUPLEX_FULL) { 2781 mcr |= PMCR_FORCE_FDX; 2782 if (tx_pause) 2783 mcr |= PMCR_TX_FC_EN; 2784 if (rx_pause) 2785 mcr |= PMCR_RX_FC_EN; 2786 } 2787 2788 if (mode == MLO_AN_PHY && phydev && phy_init_eee(phydev, 0) >= 0) { 2789 switch (speed) { 2790 case SPEED_1000: 2791 mcr |= PMCR_FORCE_EEE1G; 2792 break; 2793 case SPEED_100: 2794 mcr |= PMCR_FORCE_EEE100; 2795 break; 2796 } 2797 } 2798 2799 mt7530_set(priv, MT7530_PMCR_P(port), mcr); 2800 } 2801 2802 static int 2803 mt7531_cpu_port_config(struct dsa_switch *ds, int port) 2804 { 2805 struct mt7530_priv *priv = ds->priv; 2806 phy_interface_t interface; 2807 int speed; 2808 int ret; 2809 2810 switch (port) { 2811 case 5: 2812 if (mt7531_is_rgmii_port(priv, port)) 2813 interface = PHY_INTERFACE_MODE_RGMII; 2814 else 2815 interface = PHY_INTERFACE_MODE_2500BASEX; 2816 2817 priv->p5_interface = interface; 2818 break; 2819 case 6: 2820 interface = PHY_INTERFACE_MODE_2500BASEX; 2821 2822 mt7531_pad_setup(ds, interface); 2823 2824 priv->p6_interface = interface; 2825 break; 2826 default: 2827 return -EINVAL; 2828 } 2829 2830 if (interface == PHY_INTERFACE_MODE_2500BASEX) 2831 speed = SPEED_2500; 2832 else 2833 speed = SPEED_1000; 2834 2835 ret = mt7531_mac_config(ds, port, MLO_AN_FIXED, interface); 2836 if (ret) 2837 return ret; 2838 mt7530_write(priv, MT7530_PMCR_P(port), 2839 PMCR_CPU_PORT_SETTING(priv->id)); 2840 mt753x_phylink_mac_link_up(ds, port, MLO_AN_FIXED, interface, NULL, 2841 speed, DUPLEX_FULL, true, true); 2842 2843 return 0; 2844 } 2845 2846 static void 2847 mt7530_mac_port_validate(struct dsa_switch *ds, int port, 2848 unsigned long *supported) 2849 { 2850 if (port == 5) 2851 phylink_set(supported, 1000baseX_Full); 2852 } 2853 2854 static void mt7531_mac_port_validate(struct dsa_switch *ds, int port, 2855 unsigned long *supported) 2856 { 2857 struct mt7530_priv *priv = ds->priv; 2858 2859 mt7531_sgmii_validate(priv, port, supported); 2860 } 2861 2862 static void 2863 mt753x_phylink_validate(struct dsa_switch *ds, int port, 2864 unsigned long *supported, 2865 struct phylink_link_state *state) 2866 { 2867 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, }; 2868 struct mt7530_priv *priv = ds->priv; 2869 2870 if (state->interface != PHY_INTERFACE_MODE_NA && 2871 !mt753x_phy_mode_supported(ds, port, state)) { 2872 linkmode_zero(supported); 2873 return; 2874 } 2875 2876 phylink_set_port_modes(mask); 2877 2878 if (state->interface != PHY_INTERFACE_MODE_TRGMII || 2879 !phy_interface_mode_is_8023z(state->interface)) { 2880 phylink_set(mask, 10baseT_Half); 2881 phylink_set(mask, 10baseT_Full); 2882 phylink_set(mask, 100baseT_Half); 2883 phylink_set(mask, 100baseT_Full); 2884 phylink_set(mask, Autoneg); 2885 } 2886 2887 /* This switch only supports 1G full-duplex. */ 2888 if (state->interface != PHY_INTERFACE_MODE_MII) 2889 phylink_set(mask, 1000baseT_Full); 2890 2891 priv->info->mac_port_validate(ds, port, mask); 2892 2893 phylink_set(mask, Pause); 2894 phylink_set(mask, Asym_Pause); 2895 2896 linkmode_and(supported, supported, mask); 2897 linkmode_and(state->advertising, state->advertising, mask); 2898 2899 /* We can only operate at 2500BaseX or 1000BaseX. If requested 2900 * to advertise both, only report advertising at 2500BaseX. 2901 */ 2902 phylink_helper_basex_speed(state); 2903 } 2904 2905 static int 2906 mt7530_phylink_mac_link_state(struct dsa_switch *ds, int port, 2907 struct phylink_link_state *state) 2908 { 2909 struct mt7530_priv *priv = ds->priv; 2910 u32 pmsr; 2911 2912 if (port < 0 || port >= MT7530_NUM_PORTS) 2913 return -EINVAL; 2914 2915 pmsr = mt7530_read(priv, MT7530_PMSR_P(port)); 2916 2917 state->link = (pmsr & PMSR_LINK); 2918 state->an_complete = state->link; 2919 state->duplex = !!(pmsr & PMSR_DPX); 2920 2921 switch (pmsr & PMSR_SPEED_MASK) { 2922 case PMSR_SPEED_10: 2923 state->speed = SPEED_10; 2924 break; 2925 case PMSR_SPEED_100: 2926 state->speed = SPEED_100; 2927 break; 2928 case PMSR_SPEED_1000: 2929 state->speed = SPEED_1000; 2930 break; 2931 default: 2932 state->speed = SPEED_UNKNOWN; 2933 break; 2934 } 2935 2936 state->pause &= ~(MLO_PAUSE_RX | MLO_PAUSE_TX); 2937 if (pmsr & PMSR_RX_FC) 2938 state->pause |= MLO_PAUSE_RX; 2939 if (pmsr & PMSR_TX_FC) 2940 state->pause |= MLO_PAUSE_TX; 2941 2942 return 1; 2943 } 2944 2945 static int 2946 mt7531_sgmii_pcs_get_state_an(struct mt7530_priv *priv, int port, 2947 struct phylink_link_state *state) 2948 { 2949 u32 status, val; 2950 u16 config_reg; 2951 2952 status = mt7530_read(priv, MT7531_PCS_CONTROL_1(port)); 2953 state->link = !!(status & MT7531_SGMII_LINK_STATUS); 2954 if (state->interface == PHY_INTERFACE_MODE_SGMII && 2955 (status & MT7531_SGMII_AN_ENABLE)) { 2956 val = mt7530_read(priv, MT7531_PCS_SPEED_ABILITY(port)); 2957 config_reg = val >> 16; 2958 2959 switch (config_reg & LPA_SGMII_SPD_MASK) { 2960 case LPA_SGMII_1000: 2961 state->speed = SPEED_1000; 2962 break; 2963 case LPA_SGMII_100: 2964 state->speed = SPEED_100; 2965 break; 2966 case LPA_SGMII_10: 2967 state->speed = SPEED_10; 2968 break; 2969 default: 2970 dev_err(priv->dev, "invalid sgmii PHY speed\n"); 2971 state->link = false; 2972 return -EINVAL; 2973 } 2974 2975 if (config_reg & LPA_SGMII_FULL_DUPLEX) 2976 state->duplex = DUPLEX_FULL; 2977 else 2978 state->duplex = DUPLEX_HALF; 2979 } 2980 2981 return 0; 2982 } 2983 2984 static int 2985 mt7531_phylink_mac_link_state(struct dsa_switch *ds, int port, 2986 struct phylink_link_state *state) 2987 { 2988 struct mt7530_priv *priv = ds->priv; 2989 2990 if (state->interface == PHY_INTERFACE_MODE_SGMII) 2991 return mt7531_sgmii_pcs_get_state_an(priv, port, state); 2992 2993 return -EOPNOTSUPP; 2994 } 2995 2996 static int 2997 mt753x_phylink_mac_link_state(struct dsa_switch *ds, int port, 2998 struct phylink_link_state *state) 2999 { 3000 struct mt7530_priv *priv = ds->priv; 3001 3002 return priv->info->mac_port_get_state(ds, port, state); 3003 } 3004 3005 static int 3006 mt753x_setup(struct dsa_switch *ds) 3007 { 3008 struct mt7530_priv *priv = ds->priv; 3009 int ret = priv->info->sw_setup(ds); 3010 3011 if (ret) 3012 return ret; 3013 3014 ret = mt7530_setup_irq(priv); 3015 if (ret) 3016 return ret; 3017 3018 ret = mt7530_setup_mdio(priv); 3019 if (ret && priv->irq) 3020 mt7530_free_irq_common(priv); 3021 3022 return ret; 3023 } 3024 3025 static int mt753x_get_mac_eee(struct dsa_switch *ds, int port, 3026 struct ethtool_eee *e) 3027 { 3028 struct mt7530_priv *priv = ds->priv; 3029 u32 eeecr = mt7530_read(priv, MT7530_PMEEECR_P(port)); 3030 3031 e->tx_lpi_enabled = !(eeecr & LPI_MODE_EN); 3032 e->tx_lpi_timer = GET_LPI_THRESH(eeecr); 3033 3034 return 0; 3035 } 3036 3037 static int mt753x_set_mac_eee(struct dsa_switch *ds, int port, 3038 struct ethtool_eee *e) 3039 { 3040 struct mt7530_priv *priv = ds->priv; 3041 u32 set, mask = LPI_THRESH_MASK | LPI_MODE_EN; 3042 3043 if (e->tx_lpi_timer > 0xFFF) 3044 return -EINVAL; 3045 3046 set = SET_LPI_THRESH(e->tx_lpi_timer); 3047 if (!e->tx_lpi_enabled) 3048 /* Force LPI Mode without a delay */ 3049 set |= LPI_MODE_EN; 3050 mt7530_rmw(priv, MT7530_PMEEECR_P(port), mask, set); 3051 3052 return 0; 3053 } 3054 3055 static const struct dsa_switch_ops mt7530_switch_ops = { 3056 .get_tag_protocol = mtk_get_tag_protocol, 3057 .setup = mt753x_setup, 3058 .get_strings = mt7530_get_strings, 3059 .get_ethtool_stats = mt7530_get_ethtool_stats, 3060 .get_sset_count = mt7530_get_sset_count, 3061 .set_ageing_time = mt7530_set_ageing_time, 3062 .port_enable = mt7530_port_enable, 3063 .port_disable = mt7530_port_disable, 3064 .port_change_mtu = mt7530_port_change_mtu, 3065 .port_max_mtu = mt7530_port_max_mtu, 3066 .port_stp_state_set = mt7530_stp_state_set, 3067 .port_pre_bridge_flags = mt7530_port_pre_bridge_flags, 3068 .port_bridge_flags = mt7530_port_bridge_flags, 3069 .port_set_mrouter = mt7530_port_set_mrouter, 3070 .port_bridge_join = mt7530_port_bridge_join, 3071 .port_bridge_leave = mt7530_port_bridge_leave, 3072 .port_fdb_add = mt7530_port_fdb_add, 3073 .port_fdb_del = mt7530_port_fdb_del, 3074 .port_fdb_dump = mt7530_port_fdb_dump, 3075 .port_mdb_add = mt7530_port_mdb_add, 3076 .port_mdb_del = mt7530_port_mdb_del, 3077 .port_vlan_filtering = mt7530_port_vlan_filtering, 3078 .port_vlan_add = mt7530_port_vlan_add, 3079 .port_vlan_del = mt7530_port_vlan_del, 3080 .port_mirror_add = mt753x_port_mirror_add, 3081 .port_mirror_del = mt753x_port_mirror_del, 3082 .phylink_validate = mt753x_phylink_validate, 3083 .phylink_mac_link_state = mt753x_phylink_mac_link_state, 3084 .phylink_mac_config = mt753x_phylink_mac_config, 3085 .phylink_mac_an_restart = mt753x_phylink_mac_an_restart, 3086 .phylink_mac_link_down = mt753x_phylink_mac_link_down, 3087 .phylink_mac_link_up = mt753x_phylink_mac_link_up, 3088 .get_mac_eee = mt753x_get_mac_eee, 3089 .set_mac_eee = mt753x_set_mac_eee, 3090 }; 3091 3092 static const struct mt753x_info mt753x_table[] = { 3093 [ID_MT7621] = { 3094 .id = ID_MT7621, 3095 .sw_setup = mt7530_setup, 3096 .phy_read = mt7530_phy_read, 3097 .phy_write = mt7530_phy_write, 3098 .pad_setup = mt7530_pad_clk_setup, 3099 .phy_mode_supported = mt7530_phy_mode_supported, 3100 .mac_port_validate = mt7530_mac_port_validate, 3101 .mac_port_get_state = mt7530_phylink_mac_link_state, 3102 .mac_port_config = mt7530_mac_config, 3103 }, 3104 [ID_MT7530] = { 3105 .id = ID_MT7530, 3106 .sw_setup = mt7530_setup, 3107 .phy_read = mt7530_phy_read, 3108 .phy_write = mt7530_phy_write, 3109 .pad_setup = mt7530_pad_clk_setup, 3110 .phy_mode_supported = mt7530_phy_mode_supported, 3111 .mac_port_validate = mt7530_mac_port_validate, 3112 .mac_port_get_state = mt7530_phylink_mac_link_state, 3113 .mac_port_config = mt7530_mac_config, 3114 }, 3115 [ID_MT7531] = { 3116 .id = ID_MT7531, 3117 .sw_setup = mt7531_setup, 3118 .phy_read = mt7531_ind_phy_read, 3119 .phy_write = mt7531_ind_phy_write, 3120 .pad_setup = mt7531_pad_setup, 3121 .cpu_port_config = mt7531_cpu_port_config, 3122 .phy_mode_supported = mt7531_phy_mode_supported, 3123 .mac_port_validate = mt7531_mac_port_validate, 3124 .mac_port_get_state = mt7531_phylink_mac_link_state, 3125 .mac_port_config = mt7531_mac_config, 3126 .mac_pcs_an_restart = mt7531_sgmii_restart_an, 3127 .mac_pcs_link_up = mt7531_sgmii_link_up_force, 3128 }, 3129 }; 3130 3131 static const struct of_device_id mt7530_of_match[] = { 3132 { .compatible = "mediatek,mt7621", .data = &mt753x_table[ID_MT7621], }, 3133 { .compatible = "mediatek,mt7530", .data = &mt753x_table[ID_MT7530], }, 3134 { .compatible = "mediatek,mt7531", .data = &mt753x_table[ID_MT7531], }, 3135 { /* sentinel */ }, 3136 }; 3137 MODULE_DEVICE_TABLE(of, mt7530_of_match); 3138 3139 static int 3140 mt7530_probe(struct mdio_device *mdiodev) 3141 { 3142 struct mt7530_priv *priv; 3143 struct device_node *dn; 3144 3145 dn = mdiodev->dev.of_node; 3146 3147 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL); 3148 if (!priv) 3149 return -ENOMEM; 3150 3151 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL); 3152 if (!priv->ds) 3153 return -ENOMEM; 3154 3155 priv->ds->dev = &mdiodev->dev; 3156 priv->ds->num_ports = DSA_MAX_PORTS; 3157 3158 /* Use medatek,mcm property to distinguish hardware type that would 3159 * casues a little bit differences on power-on sequence. 3160 */ 3161 priv->mcm = of_property_read_bool(dn, "mediatek,mcm"); 3162 if (priv->mcm) { 3163 dev_info(&mdiodev->dev, "MT7530 adapts as multi-chip module\n"); 3164 3165 priv->rstc = devm_reset_control_get(&mdiodev->dev, "mcm"); 3166 if (IS_ERR(priv->rstc)) { 3167 dev_err(&mdiodev->dev, "Couldn't get our reset line\n"); 3168 return PTR_ERR(priv->rstc); 3169 } 3170 } 3171 3172 /* Get the hardware identifier from the devicetree node. 3173 * We will need it for some of the clock and regulator setup. 3174 */ 3175 priv->info = of_device_get_match_data(&mdiodev->dev); 3176 if (!priv->info) 3177 return -EINVAL; 3178 3179 /* Sanity check if these required device operations are filled 3180 * properly. 3181 */ 3182 if (!priv->info->sw_setup || !priv->info->pad_setup || 3183 !priv->info->phy_read || !priv->info->phy_write || 3184 !priv->info->phy_mode_supported || 3185 !priv->info->mac_port_validate || 3186 !priv->info->mac_port_get_state || !priv->info->mac_port_config) 3187 return -EINVAL; 3188 3189 priv->id = priv->info->id; 3190 3191 if (priv->id == ID_MT7530) { 3192 priv->core_pwr = devm_regulator_get(&mdiodev->dev, "core"); 3193 if (IS_ERR(priv->core_pwr)) 3194 return PTR_ERR(priv->core_pwr); 3195 3196 priv->io_pwr = devm_regulator_get(&mdiodev->dev, "io"); 3197 if (IS_ERR(priv->io_pwr)) 3198 return PTR_ERR(priv->io_pwr); 3199 } 3200 3201 /* Not MCM that indicates switch works as the remote standalone 3202 * integrated circuit so the GPIO pin would be used to complete 3203 * the reset, otherwise memory-mapped register accessing used 3204 * through syscon provides in the case of MCM. 3205 */ 3206 if (!priv->mcm) { 3207 priv->reset = devm_gpiod_get_optional(&mdiodev->dev, "reset", 3208 GPIOD_OUT_LOW); 3209 if (IS_ERR(priv->reset)) { 3210 dev_err(&mdiodev->dev, "Couldn't get our reset line\n"); 3211 return PTR_ERR(priv->reset); 3212 } 3213 } 3214 3215 priv->bus = mdiodev->bus; 3216 priv->dev = &mdiodev->dev; 3217 priv->ds->priv = priv; 3218 priv->ds->ops = &mt7530_switch_ops; 3219 mutex_init(&priv->reg_mutex); 3220 dev_set_drvdata(&mdiodev->dev, priv); 3221 3222 return dsa_register_switch(priv->ds); 3223 } 3224 3225 static void 3226 mt7530_remove(struct mdio_device *mdiodev) 3227 { 3228 struct mt7530_priv *priv = dev_get_drvdata(&mdiodev->dev); 3229 int ret = 0; 3230 3231 ret = regulator_disable(priv->core_pwr); 3232 if (ret < 0) 3233 dev_err(priv->dev, 3234 "Failed to disable core power: %d\n", ret); 3235 3236 ret = regulator_disable(priv->io_pwr); 3237 if (ret < 0) 3238 dev_err(priv->dev, "Failed to disable io pwr: %d\n", 3239 ret); 3240 3241 if (priv->irq) 3242 mt7530_free_irq(priv); 3243 3244 dsa_unregister_switch(priv->ds); 3245 mutex_destroy(&priv->reg_mutex); 3246 } 3247 3248 static struct mdio_driver mt7530_mdio_driver = { 3249 .probe = mt7530_probe, 3250 .remove = mt7530_remove, 3251 .mdiodrv.driver = { 3252 .name = "mt7530", 3253 .of_match_table = mt7530_of_match, 3254 }, 3255 }; 3256 3257 mdio_module_driver(mt7530_mdio_driver); 3258 3259 MODULE_AUTHOR("Sean Wang <sean.wang@mediatek.com>"); 3260 MODULE_DESCRIPTION("Driver for Mediatek MT7530 Switch"); 3261 MODULE_LICENSE("GPL"); 3262