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