1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
4 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
5 * Copyright (c) 2015, 2019, The Linux Foundation. All rights reserved.
6 * Copyright (c) 2016 John Crispin <john@phrozen.org>
9 #include <linux/module.h>
10 #include <linux/phy.h>
11 #include <linux/netdevice.h>
13 #include <linux/of_net.h>
14 #include <linux/of_mdio.h>
15 #include <linux/of_platform.h>
16 #include <linux/if_bridge.h>
17 #include <linux/mdio.h>
18 #include <linux/phylink.h>
19 #include <linux/gpio/consumer.h>
20 #include <linux/etherdevice.h>
24 #define MIB_DESC(_s, _o, _n) \
31 static const struct qca8k_mib_desc ar8327_mib[] = {
32 MIB_DESC(1, 0x00, "RxBroad"),
33 MIB_DESC(1, 0x04, "RxPause"),
34 MIB_DESC(1, 0x08, "RxMulti"),
35 MIB_DESC(1, 0x0c, "RxFcsErr"),
36 MIB_DESC(1, 0x10, "RxAlignErr"),
37 MIB_DESC(1, 0x14, "RxRunt"),
38 MIB_DESC(1, 0x18, "RxFragment"),
39 MIB_DESC(1, 0x1c, "Rx64Byte"),
40 MIB_DESC(1, 0x20, "Rx128Byte"),
41 MIB_DESC(1, 0x24, "Rx256Byte"),
42 MIB_DESC(1, 0x28, "Rx512Byte"),
43 MIB_DESC(1, 0x2c, "Rx1024Byte"),
44 MIB_DESC(1, 0x30, "Rx1518Byte"),
45 MIB_DESC(1, 0x34, "RxMaxByte"),
46 MIB_DESC(1, 0x38, "RxTooLong"),
47 MIB_DESC(2, 0x3c, "RxGoodByte"),
48 MIB_DESC(2, 0x44, "RxBadByte"),
49 MIB_DESC(1, 0x4c, "RxOverFlow"),
50 MIB_DESC(1, 0x50, "Filtered"),
51 MIB_DESC(1, 0x54, "TxBroad"),
52 MIB_DESC(1, 0x58, "TxPause"),
53 MIB_DESC(1, 0x5c, "TxMulti"),
54 MIB_DESC(1, 0x60, "TxUnderRun"),
55 MIB_DESC(1, 0x64, "Tx64Byte"),
56 MIB_DESC(1, 0x68, "Tx128Byte"),
57 MIB_DESC(1, 0x6c, "Tx256Byte"),
58 MIB_DESC(1, 0x70, "Tx512Byte"),
59 MIB_DESC(1, 0x74, "Tx1024Byte"),
60 MIB_DESC(1, 0x78, "Tx1518Byte"),
61 MIB_DESC(1, 0x7c, "TxMaxByte"),
62 MIB_DESC(1, 0x80, "TxOverSize"),
63 MIB_DESC(2, 0x84, "TxByte"),
64 MIB_DESC(1, 0x8c, "TxCollision"),
65 MIB_DESC(1, 0x90, "TxAbortCol"),
66 MIB_DESC(1, 0x94, "TxMultiCol"),
67 MIB_DESC(1, 0x98, "TxSingleCol"),
68 MIB_DESC(1, 0x9c, "TxExcDefer"),
69 MIB_DESC(1, 0xa0, "TxDefer"),
70 MIB_DESC(1, 0xa4, "TxLateCol"),
73 /* The 32bit switch registers are accessed indirectly. To achieve this we need
74 * to set the page of the register. Track the last page that was set to reduce
77 static u16 qca8k_current_page = 0xffff;
80 qca8k_split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
89 *page = regaddr & 0x3ff;
93 qca8k_mii_read32(struct mii_bus *bus, int phy_id, u32 regnum, u32 *val)
97 ret = bus->read(bus, phy_id, regnum);
100 ret = bus->read(bus, phy_id, regnum + 1);
105 dev_err_ratelimited(&bus->dev,
106 "failed to read qca8k 32bit register\n");
115 qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val)
121 hi = (u16)(val >> 16);
123 ret = bus->write(bus, phy_id, regnum, lo);
125 ret = bus->write(bus, phy_id, regnum + 1, hi);
127 dev_err_ratelimited(&bus->dev,
128 "failed to write qca8k 32bit register\n");
132 qca8k_set_page(struct mii_bus *bus, u16 page)
136 if (page == qca8k_current_page)
139 ret = bus->write(bus, 0x18, 0, page);
141 dev_err_ratelimited(&bus->dev,
142 "failed to set qca8k page\n");
146 qca8k_current_page = page;
147 usleep_range(1000, 2000);
152 qca8k_read(struct qca8k_priv *priv, u32 reg, u32 *val)
154 struct mii_bus *bus = priv->bus;
158 qca8k_split_addr(reg, &r1, &r2, &page);
160 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
162 ret = qca8k_set_page(bus, page);
166 ret = qca8k_mii_read32(bus, 0x10 | r2, r1, val);
169 mutex_unlock(&bus->mdio_lock);
174 qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
176 struct mii_bus *bus = priv->bus;
180 qca8k_split_addr(reg, &r1, &r2, &page);
182 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
184 ret = qca8k_set_page(bus, page);
188 qca8k_mii_write32(bus, 0x10 | r2, r1, val);
191 mutex_unlock(&bus->mdio_lock);
196 qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 write_val)
198 struct mii_bus *bus = priv->bus;
203 qca8k_split_addr(reg, &r1, &r2, &page);
205 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
207 ret = qca8k_set_page(bus, page);
211 ret = qca8k_mii_read32(bus, 0x10 | r2, r1, &val);
217 qca8k_mii_write32(bus, 0x10 | r2, r1, val);
220 mutex_unlock(&bus->mdio_lock);
226 qca8k_reg_set(struct qca8k_priv *priv, u32 reg, u32 val)
228 return qca8k_rmw(priv, reg, 0, val);
232 qca8k_reg_clear(struct qca8k_priv *priv, u32 reg, u32 val)
234 return qca8k_rmw(priv, reg, val, 0);
238 qca8k_regmap_read(void *ctx, uint32_t reg, uint32_t *val)
240 struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
242 return qca8k_read(priv, reg, val);
246 qca8k_regmap_write(void *ctx, uint32_t reg, uint32_t val)
248 struct qca8k_priv *priv = (struct qca8k_priv *)ctx;
250 return qca8k_write(priv, reg, val);
253 static const struct regmap_range qca8k_readable_ranges[] = {
254 regmap_reg_range(0x0000, 0x00e4), /* Global control */
255 regmap_reg_range(0x0100, 0x0168), /* EEE control */
256 regmap_reg_range(0x0200, 0x0270), /* Parser control */
257 regmap_reg_range(0x0400, 0x0454), /* ACL */
258 regmap_reg_range(0x0600, 0x0718), /* Lookup */
259 regmap_reg_range(0x0800, 0x0b70), /* QM */
260 regmap_reg_range(0x0c00, 0x0c80), /* PKT */
261 regmap_reg_range(0x0e00, 0x0e98), /* L3 */
262 regmap_reg_range(0x1000, 0x10ac), /* MIB - Port0 */
263 regmap_reg_range(0x1100, 0x11ac), /* MIB - Port1 */
264 regmap_reg_range(0x1200, 0x12ac), /* MIB - Port2 */
265 regmap_reg_range(0x1300, 0x13ac), /* MIB - Port3 */
266 regmap_reg_range(0x1400, 0x14ac), /* MIB - Port4 */
267 regmap_reg_range(0x1500, 0x15ac), /* MIB - Port5 */
268 regmap_reg_range(0x1600, 0x16ac), /* MIB - Port6 */
272 static const struct regmap_access_table qca8k_readable_table = {
273 .yes_ranges = qca8k_readable_ranges,
274 .n_yes_ranges = ARRAY_SIZE(qca8k_readable_ranges),
277 static struct regmap_config qca8k_regmap_config = {
281 .max_register = 0x16ac, /* end MIB - Port6 range */
282 .reg_read = qca8k_regmap_read,
283 .reg_write = qca8k_regmap_write,
284 .rd_table = &qca8k_readable_table,
288 qca8k_busy_wait(struct qca8k_priv *priv, u32 reg, u32 mask)
293 ret = read_poll_timeout(qca8k_read, ret1, !(val & mask),
294 0, QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
297 /* Check if qca8k_read has failed for a different reason
298 * before returning -ETIMEDOUT
300 if (ret < 0 && ret1 < 0)
307 qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
312 /* load the ARL table into an array */
313 for (i = 0; i < 4; i++) {
314 ret = qca8k_read(priv, QCA8K_REG_ATU_DATA0 + (i * 4), &val);
322 fdb->vid = (reg[2] >> QCA8K_ATU_VID_S) & QCA8K_ATU_VID_M;
324 fdb->aging = reg[2] & QCA8K_ATU_STATUS_M;
325 /* portmask - 54:48 */
326 fdb->port_mask = (reg[1] >> QCA8K_ATU_PORT_S) & QCA8K_ATU_PORT_M;
328 fdb->mac[0] = (reg[1] >> QCA8K_ATU_ADDR0_S) & 0xff;
329 fdb->mac[1] = reg[1] & 0xff;
330 fdb->mac[2] = (reg[0] >> QCA8K_ATU_ADDR2_S) & 0xff;
331 fdb->mac[3] = (reg[0] >> QCA8K_ATU_ADDR3_S) & 0xff;
332 fdb->mac[4] = (reg[0] >> QCA8K_ATU_ADDR4_S) & 0xff;
333 fdb->mac[5] = reg[0] & 0xff;
339 qca8k_fdb_write(struct qca8k_priv *priv, u16 vid, u8 port_mask, const u8 *mac,
346 reg[2] = (vid & QCA8K_ATU_VID_M) << QCA8K_ATU_VID_S;
348 reg[2] |= aging & QCA8K_ATU_STATUS_M;
349 /* portmask - 54:48 */
350 reg[1] = (port_mask & QCA8K_ATU_PORT_M) << QCA8K_ATU_PORT_S;
352 reg[1] |= mac[0] << QCA8K_ATU_ADDR0_S;
354 reg[0] |= mac[2] << QCA8K_ATU_ADDR2_S;
355 reg[0] |= mac[3] << QCA8K_ATU_ADDR3_S;
356 reg[0] |= mac[4] << QCA8K_ATU_ADDR4_S;
359 /* load the array into the ARL table */
360 for (i = 0; i < 3; i++)
361 qca8k_write(priv, QCA8K_REG_ATU_DATA0 + (i * 4), reg[i]);
365 qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
370 /* Set the command and FDB index */
371 reg = QCA8K_ATU_FUNC_BUSY;
374 reg |= QCA8K_ATU_FUNC_PORT_EN;
375 reg |= (port & QCA8K_ATU_FUNC_PORT_M) << QCA8K_ATU_FUNC_PORT_S;
378 /* Write the function register triggering the table access */
379 ret = qca8k_write(priv, QCA8K_REG_ATU_FUNC, reg);
383 /* wait for completion */
384 ret = qca8k_busy_wait(priv, QCA8K_REG_ATU_FUNC, QCA8K_ATU_FUNC_BUSY);
388 /* Check for table full violation when adding an entry */
389 if (cmd == QCA8K_FDB_LOAD) {
390 ret = qca8k_read(priv, QCA8K_REG_ATU_FUNC, ®);
393 if (reg & QCA8K_ATU_FUNC_FULL)
401 qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port)
405 qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging);
406 ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port);
410 return qca8k_fdb_read(priv, fdb);
414 qca8k_fdb_add(struct qca8k_priv *priv, const u8 *mac, u16 port_mask,
419 mutex_lock(&priv->reg_mutex);
420 qca8k_fdb_write(priv, vid, port_mask, mac, aging);
421 ret = qca8k_fdb_access(priv, QCA8K_FDB_LOAD, -1);
422 mutex_unlock(&priv->reg_mutex);
428 qca8k_fdb_del(struct qca8k_priv *priv, const u8 *mac, u16 port_mask, u16 vid)
432 mutex_lock(&priv->reg_mutex);
433 qca8k_fdb_write(priv, vid, port_mask, mac, 0);
434 ret = qca8k_fdb_access(priv, QCA8K_FDB_PURGE, -1);
435 mutex_unlock(&priv->reg_mutex);
441 qca8k_fdb_flush(struct qca8k_priv *priv)
443 mutex_lock(&priv->reg_mutex);
444 qca8k_fdb_access(priv, QCA8K_FDB_FLUSH, -1);
445 mutex_unlock(&priv->reg_mutex);
449 qca8k_vlan_access(struct qca8k_priv *priv, enum qca8k_vlan_cmd cmd, u16 vid)
454 /* Set the command and VLAN index */
455 reg = QCA8K_VTU_FUNC1_BUSY;
457 reg |= vid << QCA8K_VTU_FUNC1_VID_S;
459 /* Write the function register triggering the table access */
460 ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC1, reg);
464 /* wait for completion */
465 ret = qca8k_busy_wait(priv, QCA8K_REG_VTU_FUNC1, QCA8K_VTU_FUNC1_BUSY);
469 /* Check for table full violation when adding an entry */
470 if (cmd == QCA8K_VLAN_LOAD) {
471 ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC1, ®);
474 if (reg & QCA8K_VTU_FUNC1_FULL)
482 qca8k_vlan_add(struct qca8k_priv *priv, u8 port, u16 vid, bool untagged)
488 We do the right thing with VLAN 0 and treat it as untagged while
489 preserving the tag on egress.
494 mutex_lock(&priv->reg_mutex);
495 ret = qca8k_vlan_access(priv, QCA8K_VLAN_READ, vid);
499 ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC0, ®);
502 reg |= QCA8K_VTU_FUNC0_VALID | QCA8K_VTU_FUNC0_IVL_EN;
503 reg &= ~(QCA8K_VTU_FUNC0_EG_MODE_MASK << QCA8K_VTU_FUNC0_EG_MODE_S(port));
505 reg |= QCA8K_VTU_FUNC0_EG_MODE_UNTAG <<
506 QCA8K_VTU_FUNC0_EG_MODE_S(port);
508 reg |= QCA8K_VTU_FUNC0_EG_MODE_TAG <<
509 QCA8K_VTU_FUNC0_EG_MODE_S(port);
511 ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
514 ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);
517 mutex_unlock(&priv->reg_mutex);
523 qca8k_vlan_del(struct qca8k_priv *priv, u8 port, u16 vid)
529 mutex_lock(&priv->reg_mutex);
530 ret = qca8k_vlan_access(priv, QCA8K_VLAN_READ, vid);
534 ret = qca8k_read(priv, QCA8K_REG_VTU_FUNC0, ®);
537 reg &= ~(3 << QCA8K_VTU_FUNC0_EG_MODE_S(port));
538 reg |= QCA8K_VTU_FUNC0_EG_MODE_NOT <<
539 QCA8K_VTU_FUNC0_EG_MODE_S(port);
541 /* Check if we're the last member to be removed */
543 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
544 mask = QCA8K_VTU_FUNC0_EG_MODE_NOT;
545 mask <<= QCA8K_VTU_FUNC0_EG_MODE_S(i);
547 if ((reg & mask) != mask) {
554 ret = qca8k_vlan_access(priv, QCA8K_VLAN_PURGE, vid);
556 ret = qca8k_write(priv, QCA8K_REG_VTU_FUNC0, reg);
559 ret = qca8k_vlan_access(priv, QCA8K_VLAN_LOAD, vid);
563 mutex_unlock(&priv->reg_mutex);
569 qca8k_mib_init(struct qca8k_priv *priv)
573 mutex_lock(&priv->reg_mutex);
574 ret = qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_FLUSH | QCA8K_MIB_BUSY);
578 ret = qca8k_busy_wait(priv, QCA8K_REG_MIB, QCA8K_MIB_BUSY);
582 ret = qca8k_reg_set(priv, QCA8K_REG_MIB, QCA8K_MIB_CPU_KEEP);
586 ret = qca8k_write(priv, QCA8K_REG_MODULE_EN, QCA8K_MODULE_EN_MIB);
589 mutex_unlock(&priv->reg_mutex);
594 qca8k_port_set_status(struct qca8k_priv *priv, int port, int enable)
596 u32 mask = QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC;
598 /* Port 0 and 6 have no internal PHY */
599 if (port > 0 && port < 6)
600 mask |= QCA8K_PORT_STATUS_LINK_AUTO;
603 qca8k_reg_set(priv, QCA8K_REG_PORT_STATUS(port), mask);
605 qca8k_reg_clear(priv, QCA8K_REG_PORT_STATUS(port), mask);
609 qca8k_port_to_phy(int port)
612 * Port 0 has no internal phy.
613 * Port 1 has an internal PHY at MDIO address 0.
614 * Port 2 has an internal PHY at MDIO address 1.
616 * Port 5 has an internal PHY at MDIO address 4.
617 * Port 6 has no internal PHY.
624 qca8k_mdio_busy_wait(struct mii_bus *bus, u32 reg, u32 mask)
630 qca8k_split_addr(reg, &r1, &r2, &page);
632 ret = read_poll_timeout(qca8k_mii_read32, ret1, !(val & mask), 0,
633 QCA8K_BUSY_WAIT_TIMEOUT * USEC_PER_MSEC, false,
634 bus, 0x10 | r2, r1, &val);
636 /* Check if qca8k_read has failed for a different reason
637 * before returnting -ETIMEDOUT
639 if (ret < 0 && ret1 < 0)
646 qca8k_mdio_write(struct mii_bus *salve_bus, int phy, int regnum, u16 data)
648 struct qca8k_priv *priv = salve_bus->priv;
649 struct mii_bus *bus = priv->bus;
654 if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
657 val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
658 QCA8K_MDIO_MASTER_WRITE | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
659 QCA8K_MDIO_MASTER_REG_ADDR(regnum) |
660 QCA8K_MDIO_MASTER_DATA(data);
662 qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);
664 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
666 ret = qca8k_set_page(bus, page);
670 qca8k_mii_write32(bus, 0x10 | r2, r1, val);
672 ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
673 QCA8K_MDIO_MASTER_BUSY);
676 /* even if the busy_wait timeouts try to clear the MASTER_EN */
677 qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
679 mutex_unlock(&bus->mdio_lock);
685 qca8k_mdio_read(struct mii_bus *salve_bus, int phy, int regnum)
687 struct qca8k_priv *priv = salve_bus->priv;
688 struct mii_bus *bus = priv->bus;
693 if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
696 val = QCA8K_MDIO_MASTER_BUSY | QCA8K_MDIO_MASTER_EN |
697 QCA8K_MDIO_MASTER_READ | QCA8K_MDIO_MASTER_PHY_ADDR(phy) |
698 QCA8K_MDIO_MASTER_REG_ADDR(regnum);
700 qca8k_split_addr(QCA8K_MDIO_MASTER_CTRL, &r1, &r2, &page);
702 mutex_lock_nested(&bus->mdio_lock, MDIO_MUTEX_NESTED);
704 ret = qca8k_set_page(bus, page);
708 qca8k_mii_write32(bus, 0x10 | r2, r1, val);
710 ret = qca8k_mdio_busy_wait(bus, QCA8K_MDIO_MASTER_CTRL,
711 QCA8K_MDIO_MASTER_BUSY);
715 ret = qca8k_mii_read32(bus, 0x10 | r2, r1, &val);
718 /* even if the busy_wait timeouts try to clear the MASTER_EN */
719 qca8k_mii_write32(bus, 0x10 | r2, r1, 0);
721 mutex_unlock(&bus->mdio_lock);
724 ret = val & QCA8K_MDIO_MASTER_DATA_MASK;
730 qca8k_phy_write(struct dsa_switch *ds, int port, int regnum, u16 data)
732 struct qca8k_priv *priv = ds->priv;
734 /* Check if the legacy mapping should be used and the
735 * port is not correctly mapped to the right PHY in the
738 if (priv->legacy_phy_port_mapping)
739 port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
741 return qca8k_mdio_write(priv->bus, port, regnum, data);
745 qca8k_phy_read(struct dsa_switch *ds, int port, int regnum)
747 struct qca8k_priv *priv = ds->priv;
750 /* Check if the legacy mapping should be used and the
751 * port is not correctly mapped to the right PHY in the
754 if (priv->legacy_phy_port_mapping)
755 port = qca8k_port_to_phy(port) % PHY_MAX_ADDR;
757 ret = qca8k_mdio_read(priv->bus, port, regnum);
766 qca8k_mdio_register(struct qca8k_priv *priv, struct device_node *mdio)
768 struct dsa_switch *ds = priv->ds;
771 bus = devm_mdiobus_alloc(ds->dev);
776 bus->priv = (void *)priv;
777 bus->name = "qca8k slave mii";
778 bus->read = qca8k_mdio_read;
779 bus->write = qca8k_mdio_write;
780 snprintf(bus->id, MII_BUS_ID_SIZE, "qca8k-%d",
783 bus->parent = ds->dev;
784 bus->phy_mask = ~ds->phys_mii_mask;
786 ds->slave_mii_bus = bus;
788 return devm_of_mdiobus_register(priv->dev, bus, mdio);
792 qca8k_setup_mdio_bus(struct qca8k_priv *priv)
794 u32 internal_mdio_mask = 0, external_mdio_mask = 0, reg;
795 struct device_node *ports, *port, *mdio;
796 phy_interface_t mode;
799 ports = of_get_child_by_name(priv->dev->of_node, "ports");
801 ports = of_get_child_by_name(priv->dev->of_node, "ethernet-ports");
806 for_each_available_child_of_node(ports, port) {
807 err = of_property_read_u32(port, "reg", ®);
814 if (!dsa_is_user_port(priv->ds, reg))
817 of_get_phy_mode(port, &mode);
819 if (of_property_read_bool(port, "phy-handle") &&
820 mode != PHY_INTERFACE_MODE_INTERNAL)
821 external_mdio_mask |= BIT(reg);
823 internal_mdio_mask |= BIT(reg);
827 if (!external_mdio_mask && !internal_mdio_mask) {
828 dev_err(priv->dev, "no PHYs are defined.\n");
832 /* The QCA8K_MDIO_MASTER_EN Bit, which grants access to PHYs through
833 * the MDIO_MASTER register also _disconnects_ the external MDC
834 * passthrough to the internal PHYs. It's not possible to use both
835 * configurations at the same time!
837 * Because this came up during the review process:
838 * If the external mdio-bus driver is capable magically disabling
839 * the QCA8K_MDIO_MASTER_EN and mutex/spin-locking out the qca8k's
840 * accessors for the time being, it would be possible to pull this
843 if (!!external_mdio_mask && !!internal_mdio_mask) {
844 dev_err(priv->dev, "either internal or external mdio bus configuration is supported.\n");
848 if (external_mdio_mask) {
849 /* Make sure to disable the internal mdio bus in cases
850 * a dt-overlay and driver reload changed the configuration
853 return qca8k_reg_clear(priv, QCA8K_MDIO_MASTER_CTRL,
854 QCA8K_MDIO_MASTER_EN);
857 /* Check if the devicetree declare the port:phy mapping */
858 mdio = of_get_child_by_name(priv->dev->of_node, "mdio");
859 if (of_device_is_available(mdio)) {
860 err = qca8k_mdio_register(priv, mdio);
867 /* If a mapping can't be found the legacy mapping is used,
868 * using the qca8k_port_to_phy function
870 priv->legacy_phy_port_mapping = true;
871 priv->ops.phy_read = qca8k_phy_read;
872 priv->ops.phy_write = qca8k_phy_write;
878 qca8k_setup_of_rgmii_delay(struct qca8k_priv *priv)
880 struct device_node *port_dn;
881 phy_interface_t mode;
885 /* CPU port is already checked */
886 dp = dsa_to_port(priv->ds, 0);
890 /* Check if port 0 is set to the correct type */
891 of_get_phy_mode(port_dn, &mode);
892 if (mode != PHY_INTERFACE_MODE_RGMII_ID &&
893 mode != PHY_INTERFACE_MODE_RGMII_RXID &&
894 mode != PHY_INTERFACE_MODE_RGMII_TXID) {
899 case PHY_INTERFACE_MODE_RGMII_ID:
900 case PHY_INTERFACE_MODE_RGMII_RXID:
901 if (of_property_read_u32(port_dn, "rx-internal-delay-ps", &val))
904 /* Switch regs accept value in ns, convert ps to ns */
907 if (val > QCA8K_MAX_DELAY) {
908 dev_err(priv->dev, "rgmii rx delay is limited to a max value of 3ns, setting to the max value");
912 priv->rgmii_rx_delay = val;
913 /* Stop here if we need to check only for rx delay */
914 if (mode != PHY_INTERFACE_MODE_RGMII_ID)
918 case PHY_INTERFACE_MODE_RGMII_TXID:
919 if (of_property_read_u32(port_dn, "tx-internal-delay-ps", &val))
922 /* Switch regs accept value in ns, convert ps to ns */
925 if (val > QCA8K_MAX_DELAY) {
926 dev_err(priv->dev, "rgmii tx delay is limited to a max value of 3ns, setting to the max value");
930 priv->rgmii_tx_delay = val;
940 qca8k_setup(struct dsa_switch *ds)
942 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
946 /* Make sure that port 0 is the cpu port */
947 if (!dsa_is_cpu_port(ds, 0)) {
948 dev_err(priv->dev, "port 0 is not the CPU port");
952 mutex_init(&priv->reg_mutex);
954 /* Start by setting up the register mapping */
955 priv->regmap = devm_regmap_init(ds->dev, NULL, priv,
956 &qca8k_regmap_config);
957 if (IS_ERR(priv->regmap))
958 dev_warn(priv->dev, "regmap initialization failed");
960 ret = qca8k_setup_mdio_bus(priv);
964 ret = qca8k_setup_of_rgmii_delay(priv);
968 /* Enable CPU Port */
969 ret = qca8k_reg_set(priv, QCA8K_REG_GLOBAL_FW_CTRL0,
970 QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN);
972 dev_err(priv->dev, "failed enabling CPU port");
976 /* Enable MIB counters */
977 ret = qca8k_mib_init(priv);
979 dev_warn(priv->dev, "mib init failed");
981 /* Enable QCA header mode on the cpu port */
982 ret = qca8k_write(priv, QCA8K_REG_PORT_HDR_CTRL(QCA8K_CPU_PORT),
983 QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_TX_S |
984 QCA8K_PORT_HDR_CTRL_ALL << QCA8K_PORT_HDR_CTRL_RX_S);
986 dev_err(priv->dev, "failed enabling QCA header mode");
990 /* Disable forwarding by default on all ports */
991 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
992 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
993 QCA8K_PORT_LOOKUP_MEMBER, 0);
998 /* Disable MAC by default on all ports */
999 for (i = 1; i < QCA8K_NUM_PORTS; i++)
1000 qca8k_port_set_status(priv, i, 0);
1002 /* Forward all unknown frames to CPU port for Linux processing */
1003 ret = qca8k_write(priv, QCA8K_REG_GLOBAL_FW_CTRL1,
1004 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_S |
1005 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_BC_DP_S |
1006 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_MC_DP_S |
1007 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_UC_DP_S);
1011 /* Setup connection between CPU port & user ports */
1012 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1013 /* CPU port gets connected to all user ports of the switch */
1014 if (dsa_is_cpu_port(ds, i)) {
1015 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(QCA8K_CPU_PORT),
1016 QCA8K_PORT_LOOKUP_MEMBER, dsa_user_ports(ds));
1021 /* Individual user ports get connected to CPU port only */
1022 if (dsa_is_user_port(ds, i)) {
1023 int shift = 16 * (i % 2);
1025 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(i),
1026 QCA8K_PORT_LOOKUP_MEMBER,
1027 BIT(QCA8K_CPU_PORT));
1031 /* Enable ARP Auto-learning by default */
1032 ret = qca8k_reg_set(priv, QCA8K_PORT_LOOKUP_CTRL(i),
1033 QCA8K_PORT_LOOKUP_LEARN);
1037 /* For port based vlans to work we need to set the
1038 * default egress vid
1040 ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(i),
1042 QCA8K_PORT_VID_DEF << shift);
1046 ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(i),
1047 QCA8K_PORT_VLAN_CVID(QCA8K_PORT_VID_DEF) |
1048 QCA8K_PORT_VLAN_SVID(QCA8K_PORT_VID_DEF));
1054 /* The port 5 of the qca8337 have some problem in flood condition. The
1055 * original legacy driver had some specific buffer and priority settings
1056 * for the different port suggested by the QCA switch team. Add this
1057 * missing settings to improve switch stability under load condition.
1058 * This problem is limited to qca8337 and other qca8k switch are not affected.
1060 if (priv->switch_id == QCA8K_ID_QCA8337) {
1061 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1063 /* The 2 CPU port and port 5 requires some different
1064 * priority than any other ports.
1069 mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
1070 QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
1071 QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x4) |
1072 QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x4) |
1073 QCA8K_PORT_HOL_CTRL0_EG_PRI4(0x6) |
1074 QCA8K_PORT_HOL_CTRL0_EG_PRI5(0x8) |
1075 QCA8K_PORT_HOL_CTRL0_EG_PORT(0x1e);
1078 mask = QCA8K_PORT_HOL_CTRL0_EG_PRI0(0x3) |
1079 QCA8K_PORT_HOL_CTRL0_EG_PRI1(0x4) |
1080 QCA8K_PORT_HOL_CTRL0_EG_PRI2(0x6) |
1081 QCA8K_PORT_HOL_CTRL0_EG_PRI3(0x8) |
1082 QCA8K_PORT_HOL_CTRL0_EG_PORT(0x19);
1084 qca8k_write(priv, QCA8K_REG_PORT_HOL_CTRL0(i), mask);
1086 mask = QCA8K_PORT_HOL_CTRL1_ING(0x6) |
1087 QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
1088 QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
1089 QCA8K_PORT_HOL_CTRL1_WRED_EN;
1090 qca8k_rmw(priv, QCA8K_REG_PORT_HOL_CTRL1(i),
1091 QCA8K_PORT_HOL_CTRL1_ING_BUF |
1092 QCA8K_PORT_HOL_CTRL1_EG_PRI_BUF_EN |
1093 QCA8K_PORT_HOL_CTRL1_EG_PORT_BUF_EN |
1094 QCA8K_PORT_HOL_CTRL1_WRED_EN,
1099 /* Special GLOBAL_FC_THRESH value are needed for ar8327 switch */
1100 if (priv->switch_id == QCA8K_ID_QCA8327) {
1101 mask = QCA8K_GLOBAL_FC_GOL_XON_THRES(288) |
1102 QCA8K_GLOBAL_FC_GOL_XOFF_THRES(496);
1103 qca8k_rmw(priv, QCA8K_REG_GLOBAL_FC_THRESH,
1104 QCA8K_GLOBAL_FC_GOL_XON_THRES_S |
1105 QCA8K_GLOBAL_FC_GOL_XOFF_THRES_S,
1109 /* Setup our port MTUs to match power on defaults */
1110 for (i = 0; i < QCA8K_NUM_PORTS; i++)
1111 priv->port_mtu[i] = ETH_FRAME_LEN + ETH_FCS_LEN;
1112 ret = qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, ETH_FRAME_LEN + ETH_FCS_LEN);
1114 dev_warn(priv->dev, "failed setting MTU settings");
1116 /* Flush the FDB table */
1117 qca8k_fdb_flush(priv);
1119 /* We don't have interrupts for link changes, so we need to poll */
1120 ds->pcs_poll = true;
1126 qca8k_phylink_mac_config(struct dsa_switch *ds, int port, unsigned int mode,
1127 const struct phylink_link_state *state)
1129 struct qca8k_priv *priv = ds->priv;
1134 case 0: /* 1st CPU port */
1135 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
1136 state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1137 state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
1138 state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1139 state->interface != PHY_INTERFACE_MODE_SGMII)
1142 reg = QCA8K_REG_PORT0_PAD_CTRL;
1149 /* Internal PHY, nothing to do */
1151 case 6: /* 2nd CPU port / external PHY */
1152 if (state->interface != PHY_INTERFACE_MODE_RGMII &&
1153 state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1154 state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
1155 state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1156 state->interface != PHY_INTERFACE_MODE_SGMII &&
1157 state->interface != PHY_INTERFACE_MODE_1000BASEX)
1160 reg = QCA8K_REG_PORT6_PAD_CTRL;
1163 dev_err(ds->dev, "%s: unsupported port: %i\n", __func__, port);
1167 if (port != 6 && phylink_autoneg_inband(mode)) {
1168 dev_err(ds->dev, "%s: in-band negotiation unsupported\n",
1173 switch (state->interface) {
1174 case PHY_INTERFACE_MODE_RGMII:
1175 /* RGMII mode means no delay so don't enable the delay */
1176 qca8k_write(priv, reg, QCA8K_PORT_PAD_RGMII_EN);
1178 case PHY_INTERFACE_MODE_RGMII_ID:
1179 case PHY_INTERFACE_MODE_RGMII_TXID:
1180 case PHY_INTERFACE_MODE_RGMII_RXID:
1181 /* RGMII_ID needs internal delay. This is enabled through
1182 * PORT5_PAD_CTRL for all ports, rather than individual port
1185 qca8k_write(priv, reg,
1186 QCA8K_PORT_PAD_RGMII_EN |
1187 QCA8K_PORT_PAD_RGMII_TX_DELAY(priv->rgmii_tx_delay) |
1188 QCA8K_PORT_PAD_RGMII_RX_DELAY(priv->rgmii_rx_delay) |
1189 QCA8K_PORT_PAD_RGMII_TX_DELAY_EN |
1190 QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
1191 /* QCA8337 requires to set rgmii rx delay */
1192 if (priv->switch_id == QCA8K_ID_QCA8337)
1193 qca8k_write(priv, QCA8K_REG_PORT5_PAD_CTRL,
1194 QCA8K_PORT_PAD_RGMII_RX_DELAY_EN);
1196 case PHY_INTERFACE_MODE_SGMII:
1197 case PHY_INTERFACE_MODE_1000BASEX:
1198 /* Enable SGMII on the port */
1199 qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN);
1201 /* Enable/disable SerDes auto-negotiation as necessary */
1202 ret = qca8k_read(priv, QCA8K_REG_PWS, &val);
1205 if (phylink_autoneg_inband(mode))
1206 val &= ~QCA8K_PWS_SERDES_AEN_DIS;
1208 val |= QCA8K_PWS_SERDES_AEN_DIS;
1209 qca8k_write(priv, QCA8K_REG_PWS, val);
1211 /* Configure the SGMII parameters */
1212 ret = qca8k_read(priv, QCA8K_REG_SGMII_CTRL, &val);
1216 val |= QCA8K_SGMII_EN_PLL | QCA8K_SGMII_EN_RX |
1217 QCA8K_SGMII_EN_TX | QCA8K_SGMII_EN_SD;
1219 if (dsa_is_cpu_port(ds, port)) {
1220 /* CPU port, we're talking to the CPU MAC, be a PHY */
1221 val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
1222 val |= QCA8K_SGMII_MODE_CTRL_PHY;
1223 } else if (state->interface == PHY_INTERFACE_MODE_SGMII) {
1224 val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
1225 val |= QCA8K_SGMII_MODE_CTRL_MAC;
1226 } else if (state->interface == PHY_INTERFACE_MODE_1000BASEX) {
1227 val &= ~QCA8K_SGMII_MODE_CTRL_MASK;
1228 val |= QCA8K_SGMII_MODE_CTRL_BASEX;
1231 qca8k_write(priv, QCA8K_REG_SGMII_CTRL, val);
1234 dev_err(ds->dev, "xMII mode %s not supported for port %d\n",
1235 phy_modes(state->interface), port);
1241 qca8k_phylink_validate(struct dsa_switch *ds, int port,
1242 unsigned long *supported,
1243 struct phylink_link_state *state)
1245 __ETHTOOL_DECLARE_LINK_MODE_MASK(mask) = { 0, };
1248 case 0: /* 1st CPU port */
1249 if (state->interface != PHY_INTERFACE_MODE_NA &&
1250 state->interface != PHY_INTERFACE_MODE_RGMII &&
1251 state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1252 state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
1253 state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1254 state->interface != PHY_INTERFACE_MODE_SGMII)
1263 if (state->interface != PHY_INTERFACE_MODE_NA &&
1264 state->interface != PHY_INTERFACE_MODE_GMII &&
1265 state->interface != PHY_INTERFACE_MODE_INTERNAL)
1268 case 6: /* 2nd CPU port / external PHY */
1269 if (state->interface != PHY_INTERFACE_MODE_NA &&
1270 state->interface != PHY_INTERFACE_MODE_RGMII &&
1271 state->interface != PHY_INTERFACE_MODE_RGMII_ID &&
1272 state->interface != PHY_INTERFACE_MODE_RGMII_TXID &&
1273 state->interface != PHY_INTERFACE_MODE_RGMII_RXID &&
1274 state->interface != PHY_INTERFACE_MODE_SGMII &&
1275 state->interface != PHY_INTERFACE_MODE_1000BASEX)
1280 linkmode_zero(supported);
1284 phylink_set_port_modes(mask);
1285 phylink_set(mask, Autoneg);
1287 phylink_set(mask, 1000baseT_Full);
1288 phylink_set(mask, 10baseT_Half);
1289 phylink_set(mask, 10baseT_Full);
1290 phylink_set(mask, 100baseT_Half);
1291 phylink_set(mask, 100baseT_Full);
1293 if (state->interface == PHY_INTERFACE_MODE_1000BASEX)
1294 phylink_set(mask, 1000baseX_Full);
1296 phylink_set(mask, Pause);
1297 phylink_set(mask, Asym_Pause);
1299 linkmode_and(supported, supported, mask);
1300 linkmode_and(state->advertising, state->advertising, mask);
1304 qca8k_phylink_mac_link_state(struct dsa_switch *ds, int port,
1305 struct phylink_link_state *state)
1307 struct qca8k_priv *priv = ds->priv;
1311 ret = qca8k_read(priv, QCA8K_REG_PORT_STATUS(port), ®);
1315 state->link = !!(reg & QCA8K_PORT_STATUS_LINK_UP);
1316 state->an_complete = state->link;
1317 state->an_enabled = !!(reg & QCA8K_PORT_STATUS_LINK_AUTO);
1318 state->duplex = (reg & QCA8K_PORT_STATUS_DUPLEX) ? DUPLEX_FULL :
1321 switch (reg & QCA8K_PORT_STATUS_SPEED) {
1322 case QCA8K_PORT_STATUS_SPEED_10:
1323 state->speed = SPEED_10;
1325 case QCA8K_PORT_STATUS_SPEED_100:
1326 state->speed = SPEED_100;
1328 case QCA8K_PORT_STATUS_SPEED_1000:
1329 state->speed = SPEED_1000;
1332 state->speed = SPEED_UNKNOWN;
1336 state->pause = MLO_PAUSE_NONE;
1337 if (reg & QCA8K_PORT_STATUS_RXFLOW)
1338 state->pause |= MLO_PAUSE_RX;
1339 if (reg & QCA8K_PORT_STATUS_TXFLOW)
1340 state->pause |= MLO_PAUSE_TX;
1346 qca8k_phylink_mac_link_down(struct dsa_switch *ds, int port, unsigned int mode,
1347 phy_interface_t interface)
1349 struct qca8k_priv *priv = ds->priv;
1351 qca8k_port_set_status(priv, port, 0);
1355 qca8k_phylink_mac_link_up(struct dsa_switch *ds, int port, unsigned int mode,
1356 phy_interface_t interface, struct phy_device *phydev,
1357 int speed, int duplex, bool tx_pause, bool rx_pause)
1359 struct qca8k_priv *priv = ds->priv;
1362 if (phylink_autoneg_inband(mode)) {
1363 reg = QCA8K_PORT_STATUS_LINK_AUTO;
1367 reg = QCA8K_PORT_STATUS_SPEED_10;
1370 reg = QCA8K_PORT_STATUS_SPEED_100;
1373 reg = QCA8K_PORT_STATUS_SPEED_1000;
1376 reg = QCA8K_PORT_STATUS_LINK_AUTO;
1380 if (duplex == DUPLEX_FULL)
1381 reg |= QCA8K_PORT_STATUS_DUPLEX;
1383 if (rx_pause || dsa_is_cpu_port(ds, port))
1384 reg |= QCA8K_PORT_STATUS_RXFLOW;
1386 if (tx_pause || dsa_is_cpu_port(ds, port))
1387 reg |= QCA8K_PORT_STATUS_TXFLOW;
1390 reg |= QCA8K_PORT_STATUS_TXMAC | QCA8K_PORT_STATUS_RXMAC;
1392 qca8k_write(priv, QCA8K_REG_PORT_STATUS(port), reg);
1396 qca8k_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data)
1400 if (stringset != ETH_SS_STATS)
1403 for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++)
1404 strncpy(data + i * ETH_GSTRING_LEN, ar8327_mib[i].name,
1409 qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
1412 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1413 const struct qca8k_mib_desc *mib;
1418 for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++) {
1419 mib = &ar8327_mib[i];
1420 reg = QCA8K_PORT_MIB_COUNTER(port) + mib->offset;
1422 ret = qca8k_read(priv, reg, &val);
1426 if (mib->size == 2) {
1427 ret = qca8k_read(priv, reg + 4, &hi);
1434 data[i] |= (u64)hi << 32;
1439 qca8k_get_sset_count(struct dsa_switch *ds, int port, int sset)
1441 if (sset != ETH_SS_STATS)
1444 return ARRAY_SIZE(ar8327_mib);
1448 qca8k_set_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *eee)
1450 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1451 u32 lpi_en = QCA8K_REG_EEE_CTRL_LPI_EN(port);
1455 mutex_lock(&priv->reg_mutex);
1456 ret = qca8k_read(priv, QCA8K_REG_EEE_CTRL, ®);
1462 if (eee->eee_enabled)
1466 ret = qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg);
1469 mutex_unlock(&priv->reg_mutex);
1474 qca8k_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
1476 /* Nothing to do on the port's MAC */
1481 qca8k_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
1483 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1487 case BR_STATE_DISABLED:
1488 stp_state = QCA8K_PORT_LOOKUP_STATE_DISABLED;
1490 case BR_STATE_BLOCKING:
1491 stp_state = QCA8K_PORT_LOOKUP_STATE_BLOCKING;
1493 case BR_STATE_LISTENING:
1494 stp_state = QCA8K_PORT_LOOKUP_STATE_LISTENING;
1496 case BR_STATE_LEARNING:
1497 stp_state = QCA8K_PORT_LOOKUP_STATE_LEARNING;
1499 case BR_STATE_FORWARDING:
1501 stp_state = QCA8K_PORT_LOOKUP_STATE_FORWARD;
1505 qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1506 QCA8K_PORT_LOOKUP_STATE_MASK, stp_state);
1510 qca8k_port_bridge_join(struct dsa_switch *ds, int port, struct net_device *br)
1512 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1513 int port_mask = BIT(QCA8K_CPU_PORT);
1516 for (i = 1; i < QCA8K_NUM_PORTS; i++) {
1517 if (dsa_to_port(ds, i)->bridge_dev != br)
1519 /* Add this port to the portvlan mask of the other ports
1522 ret = qca8k_reg_set(priv,
1523 QCA8K_PORT_LOOKUP_CTRL(i),
1528 port_mask |= BIT(i);
1531 /* Add all other ports to this ports portvlan mask */
1532 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1533 QCA8K_PORT_LOOKUP_MEMBER, port_mask);
1539 qca8k_port_bridge_leave(struct dsa_switch *ds, int port, struct net_device *br)
1541 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1544 for (i = 1; i < QCA8K_NUM_PORTS; i++) {
1545 if (dsa_to_port(ds, i)->bridge_dev != br)
1547 /* Remove this port to the portvlan mask of the other ports
1550 qca8k_reg_clear(priv,
1551 QCA8K_PORT_LOOKUP_CTRL(i),
1555 /* Set the cpu port to be the only one in the portvlan mask of
1558 qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1559 QCA8K_PORT_LOOKUP_MEMBER, BIT(QCA8K_CPU_PORT));
1563 qca8k_port_enable(struct dsa_switch *ds, int port,
1564 struct phy_device *phy)
1566 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1568 qca8k_port_set_status(priv, port, 1);
1569 priv->port_sts[port].enabled = 1;
1571 if (dsa_is_user_port(ds, port))
1572 phy_support_asym_pause(phy);
1578 qca8k_port_disable(struct dsa_switch *ds, int port)
1580 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1582 qca8k_port_set_status(priv, port, 0);
1583 priv->port_sts[port].enabled = 0;
1587 qca8k_port_change_mtu(struct dsa_switch *ds, int port, int new_mtu)
1589 struct qca8k_priv *priv = ds->priv;
1592 priv->port_mtu[port] = new_mtu;
1594 for (i = 0; i < QCA8K_NUM_PORTS; i++)
1595 if (priv->port_mtu[i] > mtu)
1596 mtu = priv->port_mtu[i];
1598 /* Include L2 header / FCS length */
1599 return qca8k_write(priv, QCA8K_MAX_FRAME_SIZE, mtu + ETH_HLEN + ETH_FCS_LEN);
1603 qca8k_port_max_mtu(struct dsa_switch *ds, int port)
1605 return QCA8K_MAX_MTU;
1609 qca8k_port_fdb_insert(struct qca8k_priv *priv, const u8 *addr,
1610 u16 port_mask, u16 vid)
1612 /* Set the vid to the port vlan id if no vid is set */
1614 vid = QCA8K_PORT_VID_DEF;
1616 return qca8k_fdb_add(priv, addr, port_mask, vid,
1617 QCA8K_ATU_STATUS_STATIC);
1621 qca8k_port_fdb_add(struct dsa_switch *ds, int port,
1622 const unsigned char *addr, u16 vid)
1624 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1625 u16 port_mask = BIT(port);
1627 return qca8k_port_fdb_insert(priv, addr, port_mask, vid);
1631 qca8k_port_fdb_del(struct dsa_switch *ds, int port,
1632 const unsigned char *addr, u16 vid)
1634 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1635 u16 port_mask = BIT(port);
1638 vid = QCA8K_PORT_VID_DEF;
1640 return qca8k_fdb_del(priv, addr, port_mask, vid);
1644 qca8k_port_fdb_dump(struct dsa_switch *ds, int port,
1645 dsa_fdb_dump_cb_t *cb, void *data)
1647 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
1648 struct qca8k_fdb _fdb = { 0 };
1649 int cnt = QCA8K_NUM_FDB_RECORDS;
1653 mutex_lock(&priv->reg_mutex);
1654 while (cnt-- && !qca8k_fdb_next(priv, &_fdb, port)) {
1657 is_static = (_fdb.aging == QCA8K_ATU_STATUS_STATIC);
1658 ret = cb(_fdb.mac, _fdb.vid, is_static, data);
1662 mutex_unlock(&priv->reg_mutex);
1668 qca8k_port_vlan_filtering(struct dsa_switch *ds, int port, bool vlan_filtering,
1669 struct netlink_ext_ack *extack)
1671 struct qca8k_priv *priv = ds->priv;
1674 if (vlan_filtering) {
1675 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1676 QCA8K_PORT_LOOKUP_VLAN_MODE,
1677 QCA8K_PORT_LOOKUP_VLAN_MODE_SECURE);
1679 ret = qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
1680 QCA8K_PORT_LOOKUP_VLAN_MODE,
1681 QCA8K_PORT_LOOKUP_VLAN_MODE_NONE);
1688 qca8k_port_vlan_add(struct dsa_switch *ds, int port,
1689 const struct switchdev_obj_port_vlan *vlan,
1690 struct netlink_ext_ack *extack)
1692 bool untagged = vlan->flags & BRIDGE_VLAN_INFO_UNTAGGED;
1693 bool pvid = vlan->flags & BRIDGE_VLAN_INFO_PVID;
1694 struct qca8k_priv *priv = ds->priv;
1697 ret = qca8k_vlan_add(priv, port, vlan->vid, untagged);
1699 dev_err(priv->dev, "Failed to add VLAN to port %d (%d)", port, ret);
1704 int shift = 16 * (port % 2);
1706 ret = qca8k_rmw(priv, QCA8K_EGRESS_VLAN(port),
1707 0xfff << shift, vlan->vid << shift);
1711 ret = qca8k_write(priv, QCA8K_REG_PORT_VLAN_CTRL0(port),
1712 QCA8K_PORT_VLAN_CVID(vlan->vid) |
1713 QCA8K_PORT_VLAN_SVID(vlan->vid));
1720 qca8k_port_vlan_del(struct dsa_switch *ds, int port,
1721 const struct switchdev_obj_port_vlan *vlan)
1723 struct qca8k_priv *priv = ds->priv;
1726 ret = qca8k_vlan_del(priv, port, vlan->vid);
1728 dev_err(priv->dev, "Failed to delete VLAN from port %d (%d)", port, ret);
1733 static u32 qca8k_get_phy_flags(struct dsa_switch *ds, int port)
1735 struct qca8k_priv *priv = ds->priv;
1737 /* Communicate to the phy internal driver the switch revision.
1738 * Based on the switch revision different values needs to be
1739 * set to the dbg and mmd reg on the phy.
1740 * The first 2 bit are used to communicate the switch revision
1741 * to the phy driver.
1743 if (port > 0 && port < 6)
1744 return priv->switch_revision;
1749 static enum dsa_tag_protocol
1750 qca8k_get_tag_protocol(struct dsa_switch *ds, int port,
1751 enum dsa_tag_protocol mp)
1753 return DSA_TAG_PROTO_QCA;
1756 static const struct dsa_switch_ops qca8k_switch_ops = {
1757 .get_tag_protocol = qca8k_get_tag_protocol,
1758 .setup = qca8k_setup,
1759 .get_strings = qca8k_get_strings,
1760 .get_ethtool_stats = qca8k_get_ethtool_stats,
1761 .get_sset_count = qca8k_get_sset_count,
1762 .get_mac_eee = qca8k_get_mac_eee,
1763 .set_mac_eee = qca8k_set_mac_eee,
1764 .port_enable = qca8k_port_enable,
1765 .port_disable = qca8k_port_disable,
1766 .port_change_mtu = qca8k_port_change_mtu,
1767 .port_max_mtu = qca8k_port_max_mtu,
1768 .port_stp_state_set = qca8k_port_stp_state_set,
1769 .port_bridge_join = qca8k_port_bridge_join,
1770 .port_bridge_leave = qca8k_port_bridge_leave,
1771 .port_fdb_add = qca8k_port_fdb_add,
1772 .port_fdb_del = qca8k_port_fdb_del,
1773 .port_fdb_dump = qca8k_port_fdb_dump,
1774 .port_vlan_filtering = qca8k_port_vlan_filtering,
1775 .port_vlan_add = qca8k_port_vlan_add,
1776 .port_vlan_del = qca8k_port_vlan_del,
1777 .phylink_validate = qca8k_phylink_validate,
1778 .phylink_mac_link_state = qca8k_phylink_mac_link_state,
1779 .phylink_mac_config = qca8k_phylink_mac_config,
1780 .phylink_mac_link_down = qca8k_phylink_mac_link_down,
1781 .phylink_mac_link_up = qca8k_phylink_mac_link_up,
1782 .get_phy_flags = qca8k_get_phy_flags,
1785 static int qca8k_read_switch_id(struct qca8k_priv *priv)
1787 const struct qca8k_match_data *data;
1792 /* get the switches ID from the compatible */
1793 data = of_device_get_match_data(priv->dev);
1797 ret = qca8k_read(priv, QCA8K_REG_MASK_CTRL, &val);
1801 id = QCA8K_MASK_CTRL_DEVICE_ID(val & QCA8K_MASK_CTRL_DEVICE_ID_MASK);
1802 if (id != data->id) {
1803 dev_err(priv->dev, "Switch id detected %x but expected %x", id, data->id);
1807 priv->switch_id = id;
1809 /* Save revision to communicate to the internal PHY driver */
1810 priv->switch_revision = (val & QCA8K_MASK_CTRL_REV_ID_MASK);
1816 qca8k_sw_probe(struct mdio_device *mdiodev)
1818 struct qca8k_priv *priv;
1821 /* allocate the private data struct so that we can probe the switches
1824 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1828 priv->bus = mdiodev->bus;
1829 priv->dev = &mdiodev->dev;
1831 priv->reset_gpio = devm_gpiod_get_optional(priv->dev, "reset",
1833 if (IS_ERR(priv->reset_gpio))
1834 return PTR_ERR(priv->reset_gpio);
1836 if (priv->reset_gpio) {
1837 gpiod_set_value_cansleep(priv->reset_gpio, 1);
1838 /* The active low duration must be greater than 10 ms
1839 * and checkpatch.pl wants 20 ms.
1842 gpiod_set_value_cansleep(priv->reset_gpio, 0);
1845 /* Check the detected switch id */
1846 ret = qca8k_read_switch_id(priv);
1850 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds), GFP_KERNEL);
1854 priv->ds->dev = &mdiodev->dev;
1855 priv->ds->num_ports = QCA8K_NUM_PORTS;
1856 priv->ds->priv = priv;
1857 priv->ops = qca8k_switch_ops;
1858 priv->ds->ops = &priv->ops;
1859 mutex_init(&priv->reg_mutex);
1860 dev_set_drvdata(&mdiodev->dev, priv);
1862 return dsa_register_switch(priv->ds);
1866 qca8k_sw_remove(struct mdio_device *mdiodev)
1868 struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev);
1871 for (i = 0; i < QCA8K_NUM_PORTS; i++)
1872 qca8k_port_set_status(priv, i, 0);
1874 dsa_unregister_switch(priv->ds);
1877 #ifdef CONFIG_PM_SLEEP
1879 qca8k_set_pm(struct qca8k_priv *priv, int enable)
1883 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1884 if (!priv->port_sts[i].enabled)
1887 qca8k_port_set_status(priv, i, enable);
1891 static int qca8k_suspend(struct device *dev)
1893 struct qca8k_priv *priv = dev_get_drvdata(dev);
1895 qca8k_set_pm(priv, 0);
1897 return dsa_switch_suspend(priv->ds);
1900 static int qca8k_resume(struct device *dev)
1902 struct qca8k_priv *priv = dev_get_drvdata(dev);
1904 qca8k_set_pm(priv, 1);
1906 return dsa_switch_resume(priv->ds);
1908 #endif /* CONFIG_PM_SLEEP */
1910 static SIMPLE_DEV_PM_OPS(qca8k_pm_ops,
1911 qca8k_suspend, qca8k_resume);
1913 static const struct qca8k_match_data qca832x = {
1914 .id = QCA8K_ID_QCA8327,
1917 static const struct qca8k_match_data qca833x = {
1918 .id = QCA8K_ID_QCA8337,
1921 static const struct of_device_id qca8k_of_match[] = {
1922 { .compatible = "qca,qca8327", .data = &qca832x },
1923 { .compatible = "qca,qca8334", .data = &qca833x },
1924 { .compatible = "qca,qca8337", .data = &qca833x },
1928 static struct mdio_driver qca8kmdio_driver = {
1929 .probe = qca8k_sw_probe,
1930 .remove = qca8k_sw_remove,
1933 .of_match_table = qca8k_of_match,
1934 .pm = &qca8k_pm_ops,
1938 mdio_module_driver(qca8kmdio_driver);
1940 MODULE_AUTHOR("Mathieu Olivari, John Crispin <john@phrozen.org>");
1941 MODULE_DESCRIPTION("Driver for QCA8K ethernet switch family");
1942 MODULE_LICENSE("GPL v2");
1943 MODULE_ALIAS("platform:qca8k");