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_platform.h>
15 #include <linux/if_bridge.h>
16 #include <linux/mdio.h>
17 #include <linux/gpio/consumer.h>
18 #include <linux/etherdevice.h>
22 #define MIB_DESC(_s, _o, _n) \
29 static const struct qca8k_mib_desc ar8327_mib
[] = {
30 MIB_DESC(1, 0x00, "RxBroad"),
31 MIB_DESC(1, 0x04, "RxPause"),
32 MIB_DESC(1, 0x08, "RxMulti"),
33 MIB_DESC(1, 0x0c, "RxFcsErr"),
34 MIB_DESC(1, 0x10, "RxAlignErr"),
35 MIB_DESC(1, 0x14, "RxRunt"),
36 MIB_DESC(1, 0x18, "RxFragment"),
37 MIB_DESC(1, 0x1c, "Rx64Byte"),
38 MIB_DESC(1, 0x20, "Rx128Byte"),
39 MIB_DESC(1, 0x24, "Rx256Byte"),
40 MIB_DESC(1, 0x28, "Rx512Byte"),
41 MIB_DESC(1, 0x2c, "Rx1024Byte"),
42 MIB_DESC(1, 0x30, "Rx1518Byte"),
43 MIB_DESC(1, 0x34, "RxMaxByte"),
44 MIB_DESC(1, 0x38, "RxTooLong"),
45 MIB_DESC(2, 0x3c, "RxGoodByte"),
46 MIB_DESC(2, 0x44, "RxBadByte"),
47 MIB_DESC(1, 0x4c, "RxOverFlow"),
48 MIB_DESC(1, 0x50, "Filtered"),
49 MIB_DESC(1, 0x54, "TxBroad"),
50 MIB_DESC(1, 0x58, "TxPause"),
51 MIB_DESC(1, 0x5c, "TxMulti"),
52 MIB_DESC(1, 0x60, "TxUnderRun"),
53 MIB_DESC(1, 0x64, "Tx64Byte"),
54 MIB_DESC(1, 0x68, "Tx128Byte"),
55 MIB_DESC(1, 0x6c, "Tx256Byte"),
56 MIB_DESC(1, 0x70, "Tx512Byte"),
57 MIB_DESC(1, 0x74, "Tx1024Byte"),
58 MIB_DESC(1, 0x78, "Tx1518Byte"),
59 MIB_DESC(1, 0x7c, "TxMaxByte"),
60 MIB_DESC(1, 0x80, "TxOverSize"),
61 MIB_DESC(2, 0x84, "TxByte"),
62 MIB_DESC(1, 0x8c, "TxCollision"),
63 MIB_DESC(1, 0x90, "TxAbortCol"),
64 MIB_DESC(1, 0x94, "TxMultiCol"),
65 MIB_DESC(1, 0x98, "TxSingleCol"),
66 MIB_DESC(1, 0x9c, "TxExcDefer"),
67 MIB_DESC(1, 0xa0, "TxDefer"),
68 MIB_DESC(1, 0xa4, "TxLateCol"),
71 /* The 32bit switch registers are accessed indirectly. To achieve this we need
72 * to set the page of the register. Track the last page that was set to reduce
75 static u16 qca8k_current_page
= 0xffff;
78 qca8k_split_addr(u32 regaddr
, u16
*r1
, u16
*r2
, u16
*page
)
87 *page
= regaddr
& 0x3ff;
91 qca8k_mii_read32(struct mii_bus
*bus
, int phy_id
, u32 regnum
)
96 ret
= bus
->read(bus
, phy_id
, regnum
);
99 ret
= bus
->read(bus
, phy_id
, regnum
+ 1);
104 dev_err_ratelimited(&bus
->dev
,
105 "failed to read qca8k 32bit register\n");
113 qca8k_mii_write32(struct mii_bus
*bus
, int phy_id
, u32 regnum
, u32 val
)
119 hi
= (u16
)(val
>> 16);
121 ret
= bus
->write(bus
, phy_id
, regnum
, lo
);
123 ret
= bus
->write(bus
, phy_id
, regnum
+ 1, hi
);
125 dev_err_ratelimited(&bus
->dev
,
126 "failed to write qca8k 32bit register\n");
130 qca8k_set_page(struct mii_bus
*bus
, u16 page
)
132 if (page
== qca8k_current_page
)
135 if (bus
->write(bus
, 0x18, 0, page
) < 0)
136 dev_err_ratelimited(&bus
->dev
,
137 "failed to set qca8k page\n");
138 qca8k_current_page
= page
;
142 qca8k_read(struct qca8k_priv
*priv
, u32 reg
)
147 qca8k_split_addr(reg
, &r1
, &r2
, &page
);
149 mutex_lock_nested(&priv
->bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
151 qca8k_set_page(priv
->bus
, page
);
152 val
= qca8k_mii_read32(priv
->bus
, 0x10 | r2
, r1
);
154 mutex_unlock(&priv
->bus
->mdio_lock
);
160 qca8k_write(struct qca8k_priv
*priv
, u32 reg
, u32 val
)
164 qca8k_split_addr(reg
, &r1
, &r2
, &page
);
166 mutex_lock_nested(&priv
->bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
168 qca8k_set_page(priv
->bus
, page
);
169 qca8k_mii_write32(priv
->bus
, 0x10 | r2
, r1
, val
);
171 mutex_unlock(&priv
->bus
->mdio_lock
);
175 qca8k_rmw(struct qca8k_priv
*priv
, u32 reg
, u32 mask
, u32 val
)
180 qca8k_split_addr(reg
, &r1
, &r2
, &page
);
182 mutex_lock_nested(&priv
->bus
->mdio_lock
, MDIO_MUTEX_NESTED
);
184 qca8k_set_page(priv
->bus
, page
);
185 ret
= qca8k_mii_read32(priv
->bus
, 0x10 | r2
, r1
);
188 qca8k_mii_write32(priv
->bus
, 0x10 | r2
, r1
, ret
);
190 mutex_unlock(&priv
->bus
->mdio_lock
);
196 qca8k_reg_set(struct qca8k_priv
*priv
, u32 reg
, u32 val
)
198 qca8k_rmw(priv
, reg
, 0, val
);
202 qca8k_reg_clear(struct qca8k_priv
*priv
, u32 reg
, u32 val
)
204 qca8k_rmw(priv
, reg
, val
, 0);
208 qca8k_regmap_read(void *ctx
, uint32_t reg
, uint32_t *val
)
210 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ctx
;
212 *val
= qca8k_read(priv
, reg
);
218 qca8k_regmap_write(void *ctx
, uint32_t reg
, uint32_t val
)
220 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ctx
;
222 qca8k_write(priv
, reg
, val
);
227 static const struct regmap_range qca8k_readable_ranges
[] = {
228 regmap_reg_range(0x0000, 0x00e4), /* Global control */
229 regmap_reg_range(0x0100, 0x0168), /* EEE control */
230 regmap_reg_range(0x0200, 0x0270), /* Parser control */
231 regmap_reg_range(0x0400, 0x0454), /* ACL */
232 regmap_reg_range(0x0600, 0x0718), /* Lookup */
233 regmap_reg_range(0x0800, 0x0b70), /* QM */
234 regmap_reg_range(0x0c00, 0x0c80), /* PKT */
235 regmap_reg_range(0x0e00, 0x0e98), /* L3 */
236 regmap_reg_range(0x1000, 0x10ac), /* MIB - Port0 */
237 regmap_reg_range(0x1100, 0x11ac), /* MIB - Port1 */
238 regmap_reg_range(0x1200, 0x12ac), /* MIB - Port2 */
239 regmap_reg_range(0x1300, 0x13ac), /* MIB - Port3 */
240 regmap_reg_range(0x1400, 0x14ac), /* MIB - Port4 */
241 regmap_reg_range(0x1500, 0x15ac), /* MIB - Port5 */
242 regmap_reg_range(0x1600, 0x16ac), /* MIB - Port6 */
246 static const struct regmap_access_table qca8k_readable_table
= {
247 .yes_ranges
= qca8k_readable_ranges
,
248 .n_yes_ranges
= ARRAY_SIZE(qca8k_readable_ranges
),
251 static struct regmap_config qca8k_regmap_config
= {
255 .max_register
= 0x16ac, /* end MIB - Port6 range */
256 .reg_read
= qca8k_regmap_read
,
257 .reg_write
= qca8k_regmap_write
,
258 .rd_table
= &qca8k_readable_table
,
262 qca8k_busy_wait(struct qca8k_priv
*priv
, u32 reg
, u32 mask
)
264 unsigned long timeout
;
266 timeout
= jiffies
+ msecs_to_jiffies(20);
268 /* loop until the busy flag has cleared */
270 u32 val
= qca8k_read(priv
, reg
);
271 int busy
= val
& mask
;
276 } while (!time_after_eq(jiffies
, timeout
));
278 return time_after_eq(jiffies
, timeout
);
282 qca8k_fdb_read(struct qca8k_priv
*priv
, struct qca8k_fdb
*fdb
)
287 /* load the ARL table into an array */
288 for (i
= 0; i
< 4; i
++)
289 reg
[i
] = qca8k_read(priv
, QCA8K_REG_ATU_DATA0
+ (i
* 4));
292 fdb
->vid
= (reg
[2] >> QCA8K_ATU_VID_S
) & QCA8K_ATU_VID_M
;
294 fdb
->aging
= reg
[2] & QCA8K_ATU_STATUS_M
;
295 /* portmask - 54:48 */
296 fdb
->port_mask
= (reg
[1] >> QCA8K_ATU_PORT_S
) & QCA8K_ATU_PORT_M
;
298 fdb
->mac
[0] = (reg
[1] >> QCA8K_ATU_ADDR0_S
) & 0xff;
299 fdb
->mac
[1] = reg
[1] & 0xff;
300 fdb
->mac
[2] = (reg
[0] >> QCA8K_ATU_ADDR2_S
) & 0xff;
301 fdb
->mac
[3] = (reg
[0] >> QCA8K_ATU_ADDR3_S
) & 0xff;
302 fdb
->mac
[4] = (reg
[0] >> QCA8K_ATU_ADDR4_S
) & 0xff;
303 fdb
->mac
[5] = reg
[0] & 0xff;
307 qca8k_fdb_write(struct qca8k_priv
*priv
, u16 vid
, u8 port_mask
, const u8
*mac
,
314 reg
[2] = (vid
& QCA8K_ATU_VID_M
) << QCA8K_ATU_VID_S
;
316 reg
[2] |= aging
& QCA8K_ATU_STATUS_M
;
317 /* portmask - 54:48 */
318 reg
[1] = (port_mask
& QCA8K_ATU_PORT_M
) << QCA8K_ATU_PORT_S
;
320 reg
[1] |= mac
[0] << QCA8K_ATU_ADDR0_S
;
322 reg
[0] |= mac
[2] << QCA8K_ATU_ADDR2_S
;
323 reg
[0] |= mac
[3] << QCA8K_ATU_ADDR3_S
;
324 reg
[0] |= mac
[4] << QCA8K_ATU_ADDR4_S
;
327 /* load the array into the ARL table */
328 for (i
= 0; i
< 3; i
++)
329 qca8k_write(priv
, QCA8K_REG_ATU_DATA0
+ (i
* 4), reg
[i
]);
333 qca8k_fdb_access(struct qca8k_priv
*priv
, enum qca8k_fdb_cmd cmd
, int port
)
337 /* Set the command and FDB index */
338 reg
= QCA8K_ATU_FUNC_BUSY
;
341 reg
|= QCA8K_ATU_FUNC_PORT_EN
;
342 reg
|= (port
& QCA8K_ATU_FUNC_PORT_M
) << QCA8K_ATU_FUNC_PORT_S
;
345 /* Write the function register triggering the table access */
346 qca8k_write(priv
, QCA8K_REG_ATU_FUNC
, reg
);
348 /* wait for completion */
349 if (qca8k_busy_wait(priv
, QCA8K_REG_ATU_FUNC
, QCA8K_ATU_FUNC_BUSY
))
352 /* Check for table full violation when adding an entry */
353 if (cmd
== QCA8K_FDB_LOAD
) {
354 reg
= qca8k_read(priv
, QCA8K_REG_ATU_FUNC
);
355 if (reg
& QCA8K_ATU_FUNC_FULL
)
363 qca8k_fdb_next(struct qca8k_priv
*priv
, struct qca8k_fdb
*fdb
, int port
)
367 qca8k_fdb_write(priv
, fdb
->vid
, fdb
->port_mask
, fdb
->mac
, fdb
->aging
);
368 ret
= qca8k_fdb_access(priv
, QCA8K_FDB_NEXT
, port
);
370 qca8k_fdb_read(priv
, fdb
);
376 qca8k_fdb_add(struct qca8k_priv
*priv
, const u8
*mac
, u16 port_mask
,
381 mutex_lock(&priv
->reg_mutex
);
382 qca8k_fdb_write(priv
, vid
, port_mask
, mac
, aging
);
383 ret
= qca8k_fdb_access(priv
, QCA8K_FDB_LOAD
, -1);
384 mutex_unlock(&priv
->reg_mutex
);
390 qca8k_fdb_del(struct qca8k_priv
*priv
, const u8
*mac
, u16 port_mask
, u16 vid
)
394 mutex_lock(&priv
->reg_mutex
);
395 qca8k_fdb_write(priv
, vid
, port_mask
, mac
, 0);
396 ret
= qca8k_fdb_access(priv
, QCA8K_FDB_PURGE
, -1);
397 mutex_unlock(&priv
->reg_mutex
);
403 qca8k_fdb_flush(struct qca8k_priv
*priv
)
405 mutex_lock(&priv
->reg_mutex
);
406 qca8k_fdb_access(priv
, QCA8K_FDB_FLUSH
, -1);
407 mutex_unlock(&priv
->reg_mutex
);
411 qca8k_mib_init(struct qca8k_priv
*priv
)
413 mutex_lock(&priv
->reg_mutex
);
414 qca8k_reg_set(priv
, QCA8K_REG_MIB
, QCA8K_MIB_FLUSH
| QCA8K_MIB_BUSY
);
415 qca8k_busy_wait(priv
, QCA8K_REG_MIB
, QCA8K_MIB_BUSY
);
416 qca8k_reg_set(priv
, QCA8K_REG_MIB
, QCA8K_MIB_CPU_KEEP
);
417 qca8k_write(priv
, QCA8K_REG_MODULE_EN
, QCA8K_MODULE_EN_MIB
);
418 mutex_unlock(&priv
->reg_mutex
);
422 qca8k_set_pad_ctrl(struct qca8k_priv
*priv
, int port
, int mode
)
428 reg
= QCA8K_REG_PORT0_PAD_CTRL
;
431 reg
= QCA8K_REG_PORT6_PAD_CTRL
;
434 pr_err("Can't set PAD_CTRL on port %d\n", port
);
438 /* Configure a port to be directly connected to an external
442 case PHY_INTERFACE_MODE_RGMII
:
443 /* RGMII mode means no delay so don't enable the delay */
444 val
= QCA8K_PORT_PAD_RGMII_EN
;
445 qca8k_write(priv
, reg
, val
);
447 case PHY_INTERFACE_MODE_RGMII_ID
:
448 /* RGMII_ID needs internal delay. This is enabled through
449 * PORT5_PAD_CTRL for all ports, rather than individual port
452 qca8k_write(priv
, reg
,
453 QCA8K_PORT_PAD_RGMII_EN
|
454 QCA8K_PORT_PAD_RGMII_TX_DELAY(QCA8K_MAX_DELAY
) |
455 QCA8K_PORT_PAD_RGMII_RX_DELAY(QCA8K_MAX_DELAY
));
456 qca8k_write(priv
, QCA8K_REG_PORT5_PAD_CTRL
,
457 QCA8K_PORT_PAD_RGMII_RX_DELAY_EN
);
459 case PHY_INTERFACE_MODE_SGMII
:
460 qca8k_write(priv
, reg
, QCA8K_PORT_PAD_SGMII_EN
);
463 pr_err("xMII mode %d not supported\n", mode
);
471 qca8k_port_set_status(struct qca8k_priv
*priv
, int port
, int enable
)
473 u32 mask
= QCA8K_PORT_STATUS_TXMAC
| QCA8K_PORT_STATUS_RXMAC
;
475 /* Port 0 and 6 have no internal PHY */
476 if (port
> 0 && port
< 6)
477 mask
|= QCA8K_PORT_STATUS_LINK_AUTO
;
480 qca8k_reg_set(priv
, QCA8K_REG_PORT_STATUS(port
), mask
);
482 qca8k_reg_clear(priv
, QCA8K_REG_PORT_STATUS(port
), mask
);
486 qca8k_port_to_phy(int port
)
489 * Port 0 has no internal phy.
490 * Port 1 has an internal PHY at MDIO address 0.
491 * Port 2 has an internal PHY at MDIO address 1.
493 * Port 5 has an internal PHY at MDIO address 4.
494 * Port 6 has no internal PHY.
501 qca8k_mdio_write(struct qca8k_priv
*priv
, int port
, u32 regnum
, u16 data
)
505 if (regnum
>= QCA8K_MDIO_MASTER_MAX_REG
)
508 /* callee is responsible for not passing bad ports,
509 * but we still would like to make spills impossible.
511 phy
= qca8k_port_to_phy(port
) % PHY_MAX_ADDR
;
512 val
= QCA8K_MDIO_MASTER_BUSY
| QCA8K_MDIO_MASTER_EN
|
513 QCA8K_MDIO_MASTER_WRITE
| QCA8K_MDIO_MASTER_PHY_ADDR(phy
) |
514 QCA8K_MDIO_MASTER_REG_ADDR(regnum
) |
515 QCA8K_MDIO_MASTER_DATA(data
);
517 qca8k_write(priv
, QCA8K_MDIO_MASTER_CTRL
, val
);
519 return qca8k_busy_wait(priv
, QCA8K_MDIO_MASTER_CTRL
,
520 QCA8K_MDIO_MASTER_BUSY
);
524 qca8k_mdio_read(struct qca8k_priv
*priv
, int port
, u32 regnum
)
528 if (regnum
>= QCA8K_MDIO_MASTER_MAX_REG
)
531 /* callee is responsible for not passing bad ports,
532 * but we still would like to make spills impossible.
534 phy
= qca8k_port_to_phy(port
) % PHY_MAX_ADDR
;
535 val
= QCA8K_MDIO_MASTER_BUSY
| QCA8K_MDIO_MASTER_EN
|
536 QCA8K_MDIO_MASTER_READ
| QCA8K_MDIO_MASTER_PHY_ADDR(phy
) |
537 QCA8K_MDIO_MASTER_REG_ADDR(regnum
);
539 qca8k_write(priv
, QCA8K_MDIO_MASTER_CTRL
, val
);
541 if (qca8k_busy_wait(priv
, QCA8K_MDIO_MASTER_CTRL
,
542 QCA8K_MDIO_MASTER_BUSY
))
545 val
= (qca8k_read(priv
, QCA8K_MDIO_MASTER_CTRL
) &
546 QCA8K_MDIO_MASTER_DATA_MASK
);
552 qca8k_phy_write(struct dsa_switch
*ds
, int port
, int regnum
, u16 data
)
554 struct qca8k_priv
*priv
= ds
->priv
;
556 return qca8k_mdio_write(priv
, port
, regnum
, data
);
560 qca8k_phy_read(struct dsa_switch
*ds
, int port
, int regnum
)
562 struct qca8k_priv
*priv
= ds
->priv
;
565 ret
= qca8k_mdio_read(priv
, port
, regnum
);
574 qca8k_setup_mdio_bus(struct qca8k_priv
*priv
)
576 u32 internal_mdio_mask
= 0, external_mdio_mask
= 0, reg
;
577 struct device_node
*ports
, *port
;
580 ports
= of_get_child_by_name(priv
->dev
->of_node
, "ports");
584 for_each_available_child_of_node(ports
, port
) {
585 err
= of_property_read_u32(port
, "reg", ®
);
592 if (!dsa_is_user_port(priv
->ds
, reg
))
595 if (of_property_read_bool(port
, "phy-handle"))
596 external_mdio_mask
|= BIT(reg
);
598 internal_mdio_mask
|= BIT(reg
);
602 if (!external_mdio_mask
&& !internal_mdio_mask
) {
603 dev_err(priv
->dev
, "no PHYs are defined.\n");
607 /* The QCA8K_MDIO_MASTER_EN Bit, which grants access to PHYs through
608 * the MDIO_MASTER register also _disconnects_ the external MDC
609 * passthrough to the internal PHYs. It's not possible to use both
610 * configurations at the same time!
612 * Because this came up during the review process:
613 * If the external mdio-bus driver is capable magically disabling
614 * the QCA8K_MDIO_MASTER_EN and mutex/spin-locking out the qca8k's
615 * accessors for the time being, it would be possible to pull this
618 if (!!external_mdio_mask
&& !!internal_mdio_mask
) {
619 dev_err(priv
->dev
, "either internal or external mdio bus configuration is supported.\n");
623 if (external_mdio_mask
) {
624 /* Make sure to disable the internal mdio bus in cases
625 * a dt-overlay and driver reload changed the configuration
628 qca8k_reg_clear(priv
, QCA8K_MDIO_MASTER_CTRL
,
629 QCA8K_MDIO_MASTER_EN
);
633 priv
->ops
.phy_read
= qca8k_phy_read
;
634 priv
->ops
.phy_write
= qca8k_phy_write
;
639 qca8k_setup(struct dsa_switch
*ds
)
641 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
642 phy_interface_t phy_mode
= PHY_INTERFACE_MODE_NA
;
646 /* Make sure that port 0 is the cpu port */
647 if (!dsa_is_cpu_port(ds
, 0)) {
648 pr_err("port 0 is not the CPU port\n");
652 mutex_init(&priv
->reg_mutex
);
654 /* Start by setting up the register mapping */
655 priv
->regmap
= devm_regmap_init(ds
->dev
, NULL
, priv
,
656 &qca8k_regmap_config
);
657 if (IS_ERR(priv
->regmap
))
658 pr_warn("regmap initialization failed");
660 ret
= qca8k_setup_mdio_bus(priv
);
664 /* Initialize CPU port pad mode (xMII type, delays...) */
665 ret
= of_get_phy_mode(dsa_to_port(ds
, QCA8K_CPU_PORT
)->dn
, &phy_mode
);
667 pr_err("Can't find phy-mode for master device\n");
670 ret
= qca8k_set_pad_ctrl(priv
, QCA8K_CPU_PORT
, phy_mode
);
674 /* Enable CPU Port, force it to maximum bandwidth and full-duplex */
675 mask
= QCA8K_PORT_STATUS_SPEED_1000
| QCA8K_PORT_STATUS_TXFLOW
|
676 QCA8K_PORT_STATUS_RXFLOW
| QCA8K_PORT_STATUS_DUPLEX
;
677 qca8k_write(priv
, QCA8K_REG_PORT_STATUS(QCA8K_CPU_PORT
), mask
);
678 qca8k_reg_set(priv
, QCA8K_REG_GLOBAL_FW_CTRL0
,
679 QCA8K_GLOBAL_FW_CTRL0_CPU_PORT_EN
);
680 qca8k_port_set_status(priv
, QCA8K_CPU_PORT
, 1);
681 priv
->port_sts
[QCA8K_CPU_PORT
].enabled
= 1;
683 /* Enable MIB counters */
684 qca8k_mib_init(priv
);
686 /* Enable QCA header mode on the cpu port */
687 qca8k_write(priv
, QCA8K_REG_PORT_HDR_CTRL(QCA8K_CPU_PORT
),
688 QCA8K_PORT_HDR_CTRL_ALL
<< QCA8K_PORT_HDR_CTRL_TX_S
|
689 QCA8K_PORT_HDR_CTRL_ALL
<< QCA8K_PORT_HDR_CTRL_RX_S
);
691 /* Disable forwarding by default on all ports */
692 for (i
= 0; i
< QCA8K_NUM_PORTS
; i
++)
693 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(i
),
694 QCA8K_PORT_LOOKUP_MEMBER
, 0);
696 /* Disable MAC by default on all user ports */
697 for (i
= 1; i
< QCA8K_NUM_PORTS
; i
++)
698 if (dsa_is_user_port(ds
, i
))
699 qca8k_port_set_status(priv
, i
, 0);
701 /* Forward all unknown frames to CPU port for Linux processing */
702 qca8k_write(priv
, QCA8K_REG_GLOBAL_FW_CTRL1
,
703 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_IGMP_DP_S
|
704 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_BC_DP_S
|
705 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_MC_DP_S
|
706 BIT(0) << QCA8K_GLOBAL_FW_CTRL1_UC_DP_S
);
708 /* Setup connection between CPU port & user ports */
709 for (i
= 0; i
< QCA8K_NUM_PORTS
; i
++) {
710 /* CPU port gets connected to all user ports of the switch */
711 if (dsa_is_cpu_port(ds
, i
)) {
712 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(QCA8K_CPU_PORT
),
713 QCA8K_PORT_LOOKUP_MEMBER
, dsa_user_ports(ds
));
716 /* Invividual user ports get connected to CPU port only */
717 if (dsa_is_user_port(ds
, i
)) {
718 int shift
= 16 * (i
% 2);
720 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(i
),
721 QCA8K_PORT_LOOKUP_MEMBER
,
722 BIT(QCA8K_CPU_PORT
));
724 /* Enable ARP Auto-learning by default */
725 qca8k_reg_set(priv
, QCA8K_PORT_LOOKUP_CTRL(i
),
726 QCA8K_PORT_LOOKUP_LEARN
);
728 /* For port based vlans to work we need to set the
731 qca8k_rmw(priv
, QCA8K_EGRESS_VLAN(i
),
732 0xffff << shift
, 1 << shift
);
733 qca8k_write(priv
, QCA8K_REG_PORT_VLAN_CTRL0(i
),
734 QCA8K_PORT_VLAN_CVID(1) |
735 QCA8K_PORT_VLAN_SVID(1));
739 /* Flush the FDB table */
740 qca8k_fdb_flush(priv
);
746 qca8k_adjust_link(struct dsa_switch
*ds
, int port
, struct phy_device
*phy
)
748 struct qca8k_priv
*priv
= ds
->priv
;
751 /* Force fixed-link setting for CPU port, skip others. */
752 if (!phy_is_pseudo_fixed_link(phy
))
756 switch (phy
->speed
) {
758 reg
= QCA8K_PORT_STATUS_SPEED_10
;
761 reg
= QCA8K_PORT_STATUS_SPEED_100
;
764 reg
= QCA8K_PORT_STATUS_SPEED_1000
;
767 dev_dbg(priv
->dev
, "port%d link speed %dMbps not supported.\n",
772 /* Set duplex mode */
773 if (phy
->duplex
== DUPLEX_FULL
)
774 reg
|= QCA8K_PORT_STATUS_DUPLEX
;
776 /* Force flow control */
777 if (dsa_is_cpu_port(ds
, port
))
778 reg
|= QCA8K_PORT_STATUS_RXFLOW
| QCA8K_PORT_STATUS_TXFLOW
;
780 /* Force link down before changing MAC options */
781 qca8k_port_set_status(priv
, port
, 0);
782 qca8k_write(priv
, QCA8K_REG_PORT_STATUS(port
), reg
);
783 qca8k_port_set_status(priv
, port
, 1);
787 qca8k_get_strings(struct dsa_switch
*ds
, int port
, u32 stringset
, uint8_t *data
)
791 if (stringset
!= ETH_SS_STATS
)
794 for (i
= 0; i
< ARRAY_SIZE(ar8327_mib
); i
++)
795 strncpy(data
+ i
* ETH_GSTRING_LEN
, ar8327_mib
[i
].name
,
800 qca8k_get_ethtool_stats(struct dsa_switch
*ds
, int port
,
803 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
804 const struct qca8k_mib_desc
*mib
;
808 for (i
= 0; i
< ARRAY_SIZE(ar8327_mib
); i
++) {
809 mib
= &ar8327_mib
[i
];
810 reg
= QCA8K_PORT_MIB_COUNTER(port
) + mib
->offset
;
812 data
[i
] = qca8k_read(priv
, reg
);
813 if (mib
->size
== 2) {
814 hi
= qca8k_read(priv
, reg
+ 4);
821 qca8k_get_sset_count(struct dsa_switch
*ds
, int port
, int sset
)
823 if (sset
!= ETH_SS_STATS
)
826 return ARRAY_SIZE(ar8327_mib
);
830 qca8k_set_mac_eee(struct dsa_switch
*ds
, int port
, struct ethtool_eee
*eee
)
832 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
833 u32 lpi_en
= QCA8K_REG_EEE_CTRL_LPI_EN(port
);
836 mutex_lock(&priv
->reg_mutex
);
837 reg
= qca8k_read(priv
, QCA8K_REG_EEE_CTRL
);
838 if (eee
->eee_enabled
)
842 qca8k_write(priv
, QCA8K_REG_EEE_CTRL
, reg
);
843 mutex_unlock(&priv
->reg_mutex
);
849 qca8k_get_mac_eee(struct dsa_switch
*ds
, int port
, struct ethtool_eee
*e
)
851 /* Nothing to do on the port's MAC */
856 qca8k_port_stp_state_set(struct dsa_switch
*ds
, int port
, u8 state
)
858 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
862 case BR_STATE_DISABLED
:
863 stp_state
= QCA8K_PORT_LOOKUP_STATE_DISABLED
;
865 case BR_STATE_BLOCKING
:
866 stp_state
= QCA8K_PORT_LOOKUP_STATE_BLOCKING
;
868 case BR_STATE_LISTENING
:
869 stp_state
= QCA8K_PORT_LOOKUP_STATE_LISTENING
;
871 case BR_STATE_LEARNING
:
872 stp_state
= QCA8K_PORT_LOOKUP_STATE_LEARNING
;
874 case BR_STATE_FORWARDING
:
876 stp_state
= QCA8K_PORT_LOOKUP_STATE_FORWARD
;
880 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(port
),
881 QCA8K_PORT_LOOKUP_STATE_MASK
, stp_state
);
885 qca8k_port_bridge_join(struct dsa_switch
*ds
, int port
, struct net_device
*br
)
887 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
888 int port_mask
= BIT(QCA8K_CPU_PORT
);
891 for (i
= 1; i
< QCA8K_NUM_PORTS
; i
++) {
892 if (dsa_to_port(ds
, i
)->bridge_dev
!= br
)
894 /* Add this port to the portvlan mask of the other ports
898 QCA8K_PORT_LOOKUP_CTRL(i
),
903 /* Add all other ports to this ports portvlan mask */
904 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(port
),
905 QCA8K_PORT_LOOKUP_MEMBER
, port_mask
);
911 qca8k_port_bridge_leave(struct dsa_switch
*ds
, int port
, struct net_device
*br
)
913 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
916 for (i
= 1; i
< QCA8K_NUM_PORTS
; i
++) {
917 if (dsa_to_port(ds
, i
)->bridge_dev
!= br
)
919 /* Remove this port to the portvlan mask of the other ports
922 qca8k_reg_clear(priv
,
923 QCA8K_PORT_LOOKUP_CTRL(i
),
927 /* Set the cpu port to be the only one in the portvlan mask of
930 qca8k_rmw(priv
, QCA8K_PORT_LOOKUP_CTRL(port
),
931 QCA8K_PORT_LOOKUP_MEMBER
, BIT(QCA8K_CPU_PORT
));
935 qca8k_port_enable(struct dsa_switch
*ds
, int port
,
936 struct phy_device
*phy
)
938 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
940 if (!dsa_is_user_port(ds
, port
))
943 qca8k_port_set_status(priv
, port
, 1);
944 priv
->port_sts
[port
].enabled
= 1;
946 phy_support_asym_pause(phy
);
952 qca8k_port_disable(struct dsa_switch
*ds
, int port
)
954 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
956 qca8k_port_set_status(priv
, port
, 0);
957 priv
->port_sts
[port
].enabled
= 0;
961 qca8k_port_fdb_insert(struct qca8k_priv
*priv
, const u8
*addr
,
962 u16 port_mask
, u16 vid
)
964 /* Set the vid to the port vlan id if no vid is set */
968 return qca8k_fdb_add(priv
, addr
, port_mask
, vid
,
969 QCA8K_ATU_STATUS_STATIC
);
973 qca8k_port_fdb_add(struct dsa_switch
*ds
, int port
,
974 const unsigned char *addr
, u16 vid
)
976 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
977 u16 port_mask
= BIT(port
);
979 return qca8k_port_fdb_insert(priv
, addr
, port_mask
, vid
);
983 qca8k_port_fdb_del(struct dsa_switch
*ds
, int port
,
984 const unsigned char *addr
, u16 vid
)
986 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
987 u16 port_mask
= BIT(port
);
992 return qca8k_fdb_del(priv
, addr
, port_mask
, vid
);
996 qca8k_port_fdb_dump(struct dsa_switch
*ds
, int port
,
997 dsa_fdb_dump_cb_t
*cb
, void *data
)
999 struct qca8k_priv
*priv
= (struct qca8k_priv
*)ds
->priv
;
1000 struct qca8k_fdb _fdb
= { 0 };
1001 int cnt
= QCA8K_NUM_FDB_RECORDS
;
1005 mutex_lock(&priv
->reg_mutex
);
1006 while (cnt
-- && !qca8k_fdb_next(priv
, &_fdb
, port
)) {
1009 is_static
= (_fdb
.aging
== QCA8K_ATU_STATUS_STATIC
);
1010 ret
= cb(_fdb
.mac
, _fdb
.vid
, is_static
, data
);
1014 mutex_unlock(&priv
->reg_mutex
);
1019 static enum dsa_tag_protocol
1020 qca8k_get_tag_protocol(struct dsa_switch
*ds
, int port
,
1021 enum dsa_tag_protocol mp
)
1023 return DSA_TAG_PROTO_QCA
;
1026 static const struct dsa_switch_ops qca8k_switch_ops
= {
1027 .get_tag_protocol
= qca8k_get_tag_protocol
,
1028 .setup
= qca8k_setup
,
1029 .adjust_link
= qca8k_adjust_link
,
1030 .get_strings
= qca8k_get_strings
,
1031 .get_ethtool_stats
= qca8k_get_ethtool_stats
,
1032 .get_sset_count
= qca8k_get_sset_count
,
1033 .get_mac_eee
= qca8k_get_mac_eee
,
1034 .set_mac_eee
= qca8k_set_mac_eee
,
1035 .port_enable
= qca8k_port_enable
,
1036 .port_disable
= qca8k_port_disable
,
1037 .port_stp_state_set
= qca8k_port_stp_state_set
,
1038 .port_bridge_join
= qca8k_port_bridge_join
,
1039 .port_bridge_leave
= qca8k_port_bridge_leave
,
1040 .port_fdb_add
= qca8k_port_fdb_add
,
1041 .port_fdb_del
= qca8k_port_fdb_del
,
1042 .port_fdb_dump
= qca8k_port_fdb_dump
,
1046 qca8k_sw_probe(struct mdio_device
*mdiodev
)
1048 struct qca8k_priv
*priv
;
1051 /* allocate the private data struct so that we can probe the switches
1054 priv
= devm_kzalloc(&mdiodev
->dev
, sizeof(*priv
), GFP_KERNEL
);
1058 priv
->bus
= mdiodev
->bus
;
1059 priv
->dev
= &mdiodev
->dev
;
1061 priv
->reset_gpio
= devm_gpiod_get_optional(priv
->dev
, "reset",
1063 if (IS_ERR(priv
->reset_gpio
))
1064 return PTR_ERR(priv
->reset_gpio
);
1066 if (priv
->reset_gpio
) {
1067 gpiod_set_value_cansleep(priv
->reset_gpio
, 1);
1068 /* The active low duration must be greater than 10 ms
1069 * and checkpatch.pl wants 20 ms.
1072 gpiod_set_value_cansleep(priv
->reset_gpio
, 0);
1075 /* read the switches ID register */
1076 id
= qca8k_read(priv
, QCA8K_REG_MASK_CTRL
);
1077 id
>>= QCA8K_MASK_CTRL_ID_S
;
1078 id
&= QCA8K_MASK_CTRL_ID_M
;
1079 if (id
!= QCA8K_ID_QCA8337
)
1082 priv
->ds
= devm_kzalloc(&mdiodev
->dev
, sizeof(*priv
->ds
),
1087 priv
->ds
->dev
= &mdiodev
->dev
;
1088 priv
->ds
->num_ports
= QCA8K_NUM_PORTS
;
1089 priv
->ds
->priv
= priv
;
1090 priv
->ops
= qca8k_switch_ops
;
1091 priv
->ds
->ops
= &priv
->ops
;
1092 mutex_init(&priv
->reg_mutex
);
1093 dev_set_drvdata(&mdiodev
->dev
, priv
);
1095 return dsa_register_switch(priv
->ds
);
1099 qca8k_sw_remove(struct mdio_device
*mdiodev
)
1101 struct qca8k_priv
*priv
= dev_get_drvdata(&mdiodev
->dev
);
1104 for (i
= 0; i
< QCA8K_NUM_PORTS
; i
++)
1105 qca8k_port_set_status(priv
, i
, 0);
1107 dsa_unregister_switch(priv
->ds
);
1110 #ifdef CONFIG_PM_SLEEP
1112 qca8k_set_pm(struct qca8k_priv
*priv
, int enable
)
1116 for (i
= 0; i
< QCA8K_NUM_PORTS
; i
++) {
1117 if (!priv
->port_sts
[i
].enabled
)
1120 qca8k_port_set_status(priv
, i
, enable
);
1124 static int qca8k_suspend(struct device
*dev
)
1126 struct qca8k_priv
*priv
= dev_get_drvdata(dev
);
1128 qca8k_set_pm(priv
, 0);
1130 return dsa_switch_suspend(priv
->ds
);
1133 static int qca8k_resume(struct device
*dev
)
1135 struct qca8k_priv
*priv
= dev_get_drvdata(dev
);
1137 qca8k_set_pm(priv
, 1);
1139 return dsa_switch_resume(priv
->ds
);
1141 #endif /* CONFIG_PM_SLEEP */
1143 static SIMPLE_DEV_PM_OPS(qca8k_pm_ops
,
1144 qca8k_suspend
, qca8k_resume
);
1146 static const struct of_device_id qca8k_of_match
[] = {
1147 { .compatible
= "qca,qca8334" },
1148 { .compatible
= "qca,qca8337" },
1152 static struct mdio_driver qca8kmdio_driver
= {
1153 .probe
= qca8k_sw_probe
,
1154 .remove
= qca8k_sw_remove
,
1157 .of_match_table
= qca8k_of_match
,
1158 .pm
= &qca8k_pm_ops
,
1162 mdio_module_driver(qca8kmdio_driver
);
1164 MODULE_AUTHOR("Mathieu Olivari, John Crispin <john@phrozen.org>");
1165 MODULE_DESCRIPTION("Driver for QCA8K ethernet switch family");
1166 MODULE_LICENSE("GPL v2");
1167 MODULE_ALIAS("platform:qca8k");