treewide: remove redundant IS_ERR() before error code check
[linux/fpc-iii.git] / drivers / net / dsa / qca8k.c
blob9f4205b4439b46b385cadfd3047578a17c37ac74
1 // SPDX-License-Identifier: GPL-2.0
2 /*
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>
7 */
9 #include <linux/module.h>
10 #include <linux/phy.h>
11 #include <linux/netdevice.h>
12 #include <net/dsa.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>
20 #include "qca8k.h"
22 #define MIB_DESC(_s, _o, _n) \
23 { \
24 .size = (_s), \
25 .offset = (_o), \
26 .name = (_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
73 * mdio writes
75 static u16 qca8k_current_page = 0xffff;
77 static void
78 qca8k_split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
80 regaddr >>= 1;
81 *r1 = regaddr & 0x1e;
83 regaddr >>= 5;
84 *r2 = regaddr & 0x7;
86 regaddr >>= 3;
87 *page = regaddr & 0x3ff;
90 static u32
91 qca8k_mii_read32(struct mii_bus *bus, int phy_id, u32 regnum)
93 u32 val;
94 int ret;
96 ret = bus->read(bus, phy_id, regnum);
97 if (ret >= 0) {
98 val = ret;
99 ret = bus->read(bus, phy_id, regnum + 1);
100 val |= ret << 16;
103 if (ret < 0) {
104 dev_err_ratelimited(&bus->dev,
105 "failed to read qca8k 32bit register\n");
106 return ret;
109 return val;
112 static void
113 qca8k_mii_write32(struct mii_bus *bus, int phy_id, u32 regnum, u32 val)
115 u16 lo, hi;
116 int ret;
118 lo = val & 0xffff;
119 hi = (u16)(val >> 16);
121 ret = bus->write(bus, phy_id, regnum, lo);
122 if (ret >= 0)
123 ret = bus->write(bus, phy_id, regnum + 1, hi);
124 if (ret < 0)
125 dev_err_ratelimited(&bus->dev,
126 "failed to write qca8k 32bit register\n");
129 static void
130 qca8k_set_page(struct mii_bus *bus, u16 page)
132 if (page == qca8k_current_page)
133 return;
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;
141 static u32
142 qca8k_read(struct qca8k_priv *priv, u32 reg)
144 u16 r1, r2, page;
145 u32 val;
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);
156 return val;
159 static void
160 qca8k_write(struct qca8k_priv *priv, u32 reg, u32 val)
162 u16 r1, r2, page;
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);
174 static u32
175 qca8k_rmw(struct qca8k_priv *priv, u32 reg, u32 mask, u32 val)
177 u16 r1, r2, page;
178 u32 ret;
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);
186 ret &= ~mask;
187 ret |= val;
188 qca8k_mii_write32(priv->bus, 0x10 | r2, r1, ret);
190 mutex_unlock(&priv->bus->mdio_lock);
192 return ret;
195 static void
196 qca8k_reg_set(struct qca8k_priv *priv, u32 reg, u32 val)
198 qca8k_rmw(priv, reg, 0, val);
201 static void
202 qca8k_reg_clear(struct qca8k_priv *priv, u32 reg, u32 val)
204 qca8k_rmw(priv, reg, val, 0);
207 static int
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);
214 return 0;
217 static int
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);
224 return 0;
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 = {
252 .reg_bits = 16,
253 .val_bits = 32,
254 .reg_stride = 4,
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,
261 static int
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 */
269 do {
270 u32 val = qca8k_read(priv, reg);
271 int busy = val & mask;
273 if (!busy)
274 break;
275 cond_resched();
276 } while (!time_after_eq(jiffies, timeout));
278 return time_after_eq(jiffies, timeout);
281 static void
282 qca8k_fdb_read(struct qca8k_priv *priv, struct qca8k_fdb *fdb)
284 u32 reg[4];
285 int i;
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));
291 /* vid - 83:72 */
292 fdb->vid = (reg[2] >> QCA8K_ATU_VID_S) & QCA8K_ATU_VID_M;
293 /* aging - 67:64 */
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;
297 /* mac - 47:0 */
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;
306 static void
307 qca8k_fdb_write(struct qca8k_priv *priv, u16 vid, u8 port_mask, const u8 *mac,
308 u8 aging)
310 u32 reg[3] = { 0 };
311 int i;
313 /* vid - 83:72 */
314 reg[2] = (vid & QCA8K_ATU_VID_M) << QCA8K_ATU_VID_S;
315 /* aging - 67:64 */
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;
319 /* mac - 47:0 */
320 reg[1] |= mac[0] << QCA8K_ATU_ADDR0_S;
321 reg[1] |= mac[1];
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;
325 reg[0] |= mac[5];
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]);
332 static int
333 qca8k_fdb_access(struct qca8k_priv *priv, enum qca8k_fdb_cmd cmd, int port)
335 u32 reg;
337 /* Set the command and FDB index */
338 reg = QCA8K_ATU_FUNC_BUSY;
339 reg |= cmd;
340 if (port >= 0) {
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))
350 return -1;
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)
356 return -1;
359 return 0;
362 static int
363 qca8k_fdb_next(struct qca8k_priv *priv, struct qca8k_fdb *fdb, int port)
365 int ret;
367 qca8k_fdb_write(priv, fdb->vid, fdb->port_mask, fdb->mac, fdb->aging);
368 ret = qca8k_fdb_access(priv, QCA8K_FDB_NEXT, port);
369 if (ret >= 0)
370 qca8k_fdb_read(priv, fdb);
372 return ret;
375 static int
376 qca8k_fdb_add(struct qca8k_priv *priv, const u8 *mac, u16 port_mask,
377 u16 vid, u8 aging)
379 int ret;
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);
386 return ret;
389 static int
390 qca8k_fdb_del(struct qca8k_priv *priv, const u8 *mac, u16 port_mask, u16 vid)
392 int ret;
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);
399 return ret;
402 static void
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);
410 static void
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);
421 static int
422 qca8k_set_pad_ctrl(struct qca8k_priv *priv, int port, int mode)
424 u32 reg, val;
426 switch (port) {
427 case 0:
428 reg = QCA8K_REG_PORT0_PAD_CTRL;
429 break;
430 case 6:
431 reg = QCA8K_REG_PORT6_PAD_CTRL;
432 break;
433 default:
434 pr_err("Can't set PAD_CTRL on port %d\n", port);
435 return -EINVAL;
438 /* Configure a port to be directly connected to an external
439 * PHY or MAC.
441 switch (mode) {
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);
446 break;
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
450 * registers
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);
458 break;
459 case PHY_INTERFACE_MODE_SGMII:
460 qca8k_write(priv, reg, QCA8K_PORT_PAD_SGMII_EN);
461 break;
462 default:
463 pr_err("xMII mode %d not supported\n", mode);
464 return -EINVAL;
467 return 0;
470 static void
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;
479 if (enable)
480 qca8k_reg_set(priv, QCA8K_REG_PORT_STATUS(port), mask);
481 else
482 qca8k_reg_clear(priv, QCA8K_REG_PORT_STATUS(port), mask);
485 static u32
486 qca8k_port_to_phy(int port)
488 /* From Andrew Lunn:
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.
492 * ...
493 * Port 5 has an internal PHY at MDIO address 4.
494 * Port 6 has no internal PHY.
497 return port - 1;
500 static int
501 qca8k_mdio_write(struct qca8k_priv *priv, int port, u32 regnum, u16 data)
503 u32 phy, val;
505 if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
506 return -EINVAL;
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);
523 static int
524 qca8k_mdio_read(struct qca8k_priv *priv, int port, u32 regnum)
526 u32 phy, val;
528 if (regnum >= QCA8K_MDIO_MASTER_MAX_REG)
529 return -EINVAL;
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))
543 return -ETIMEDOUT;
545 val = (qca8k_read(priv, QCA8K_MDIO_MASTER_CTRL) &
546 QCA8K_MDIO_MASTER_DATA_MASK);
548 return val;
551 static int
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);
559 static int
560 qca8k_phy_read(struct dsa_switch *ds, int port, int regnum)
562 struct qca8k_priv *priv = ds->priv;
563 int ret;
565 ret = qca8k_mdio_read(priv, port, regnum);
567 if (ret < 0)
568 return 0xffff;
570 return ret;
573 static int
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;
578 int err;
580 ports = of_get_child_by_name(priv->dev->of_node, "ports");
581 if (!ports)
582 return -EINVAL;
584 for_each_available_child_of_node(ports, port) {
585 err = of_property_read_u32(port, "reg", &reg);
586 if (err) {
587 of_node_put(port);
588 of_node_put(ports);
589 return err;
592 if (!dsa_is_user_port(priv->ds, reg))
593 continue;
595 if (of_property_read_bool(port, "phy-handle"))
596 external_mdio_mask |= BIT(reg);
597 else
598 internal_mdio_mask |= BIT(reg);
601 of_node_put(ports);
602 if (!external_mdio_mask && !internal_mdio_mask) {
603 dev_err(priv->dev, "no PHYs are defined.\n");
604 return -EINVAL;
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
616 * off.
618 if (!!external_mdio_mask && !!internal_mdio_mask) {
619 dev_err(priv->dev, "either internal or external mdio bus configuration is supported.\n");
620 return -EINVAL;
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);
630 return 0;
633 priv->ops.phy_read = qca8k_phy_read;
634 priv->ops.phy_write = qca8k_phy_write;
635 return 0;
638 static int
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;
643 int ret, i;
644 u32 mask;
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");
649 return -EINVAL;
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);
661 if (ret)
662 return ret;
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);
666 if (ret) {
667 pr_err("Can't find phy-mode for master device\n");
668 return ret;
670 ret = qca8k_set_pad_ctrl(priv, QCA8K_CPU_PORT, phy_mode);
671 if (ret < 0)
672 return ret;
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
729 * default egress vid
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);
742 return 0;
745 static void
746 qca8k_adjust_link(struct dsa_switch *ds, int port, struct phy_device *phy)
748 struct qca8k_priv *priv = ds->priv;
749 u32 reg;
751 /* Force fixed-link setting for CPU port, skip others. */
752 if (!phy_is_pseudo_fixed_link(phy))
753 return;
755 /* Set port speed */
756 switch (phy->speed) {
757 case 10:
758 reg = QCA8K_PORT_STATUS_SPEED_10;
759 break;
760 case 100:
761 reg = QCA8K_PORT_STATUS_SPEED_100;
762 break;
763 case 1000:
764 reg = QCA8K_PORT_STATUS_SPEED_1000;
765 break;
766 default:
767 dev_dbg(priv->dev, "port%d link speed %dMbps not supported.\n",
768 port, phy->speed);
769 return;
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);
786 static void
787 qca8k_get_strings(struct dsa_switch *ds, int port, u32 stringset, uint8_t *data)
789 int i;
791 if (stringset != ETH_SS_STATS)
792 return;
794 for (i = 0; i < ARRAY_SIZE(ar8327_mib); i++)
795 strncpy(data + i * ETH_GSTRING_LEN, ar8327_mib[i].name,
796 ETH_GSTRING_LEN);
799 static void
800 qca8k_get_ethtool_stats(struct dsa_switch *ds, int port,
801 uint64_t *data)
803 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
804 const struct qca8k_mib_desc *mib;
805 u32 reg, i;
806 u64 hi;
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);
815 data[i] |= hi << 32;
820 static int
821 qca8k_get_sset_count(struct dsa_switch *ds, int port, int sset)
823 if (sset != ETH_SS_STATS)
824 return 0;
826 return ARRAY_SIZE(ar8327_mib);
829 static int
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);
834 u32 reg;
836 mutex_lock(&priv->reg_mutex);
837 reg = qca8k_read(priv, QCA8K_REG_EEE_CTRL);
838 if (eee->eee_enabled)
839 reg |= lpi_en;
840 else
841 reg &= ~lpi_en;
842 qca8k_write(priv, QCA8K_REG_EEE_CTRL, reg);
843 mutex_unlock(&priv->reg_mutex);
845 return 0;
848 static int
849 qca8k_get_mac_eee(struct dsa_switch *ds, int port, struct ethtool_eee *e)
851 /* Nothing to do on the port's MAC */
852 return 0;
855 static void
856 qca8k_port_stp_state_set(struct dsa_switch *ds, int port, u8 state)
858 struct qca8k_priv *priv = (struct qca8k_priv *)ds->priv;
859 u32 stp_state;
861 switch (state) {
862 case BR_STATE_DISABLED:
863 stp_state = QCA8K_PORT_LOOKUP_STATE_DISABLED;
864 break;
865 case BR_STATE_BLOCKING:
866 stp_state = QCA8K_PORT_LOOKUP_STATE_BLOCKING;
867 break;
868 case BR_STATE_LISTENING:
869 stp_state = QCA8K_PORT_LOOKUP_STATE_LISTENING;
870 break;
871 case BR_STATE_LEARNING:
872 stp_state = QCA8K_PORT_LOOKUP_STATE_LEARNING;
873 break;
874 case BR_STATE_FORWARDING:
875 default:
876 stp_state = QCA8K_PORT_LOOKUP_STATE_FORWARD;
877 break;
880 qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
881 QCA8K_PORT_LOOKUP_STATE_MASK, stp_state);
884 static int
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);
889 int i;
891 for (i = 1; i < QCA8K_NUM_PORTS; i++) {
892 if (dsa_to_port(ds, i)->bridge_dev != br)
893 continue;
894 /* Add this port to the portvlan mask of the other ports
895 * in the bridge
897 qca8k_reg_set(priv,
898 QCA8K_PORT_LOOKUP_CTRL(i),
899 BIT(port));
900 if (i != port)
901 port_mask |= BIT(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);
907 return 0;
910 static void
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;
914 int i;
916 for (i = 1; i < QCA8K_NUM_PORTS; i++) {
917 if (dsa_to_port(ds, i)->bridge_dev != br)
918 continue;
919 /* Remove this port to the portvlan mask of the other ports
920 * in the bridge
922 qca8k_reg_clear(priv,
923 QCA8K_PORT_LOOKUP_CTRL(i),
924 BIT(port));
927 /* Set the cpu port to be the only one in the portvlan mask of
928 * this port
930 qca8k_rmw(priv, QCA8K_PORT_LOOKUP_CTRL(port),
931 QCA8K_PORT_LOOKUP_MEMBER, BIT(QCA8K_CPU_PORT));
934 static int
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))
941 return 0;
943 qca8k_port_set_status(priv, port, 1);
944 priv->port_sts[port].enabled = 1;
946 phy_support_asym_pause(phy);
948 return 0;
951 static void
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;
960 static int
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 */
965 if (!vid)
966 vid = 1;
968 return qca8k_fdb_add(priv, addr, port_mask, vid,
969 QCA8K_ATU_STATUS_STATIC);
972 static int
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);
982 static int
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);
989 if (!vid)
990 vid = 1;
992 return qca8k_fdb_del(priv, addr, port_mask, vid);
995 static int
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;
1002 bool is_static;
1003 int ret = 0;
1005 mutex_lock(&priv->reg_mutex);
1006 while (cnt-- && !qca8k_fdb_next(priv, &_fdb, port)) {
1007 if (!_fdb.aging)
1008 break;
1009 is_static = (_fdb.aging == QCA8K_ATU_STATUS_STATIC);
1010 ret = cb(_fdb.mac, _fdb.vid, is_static, data);
1011 if (ret)
1012 break;
1014 mutex_unlock(&priv->reg_mutex);
1016 return 0;
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,
1045 static int
1046 qca8k_sw_probe(struct mdio_device *mdiodev)
1048 struct qca8k_priv *priv;
1049 u32 id;
1051 /* allocate the private data struct so that we can probe the switches
1052 * ID register
1054 priv = devm_kzalloc(&mdiodev->dev, sizeof(*priv), GFP_KERNEL);
1055 if (!priv)
1056 return -ENOMEM;
1058 priv->bus = mdiodev->bus;
1059 priv->dev = &mdiodev->dev;
1061 priv->reset_gpio = devm_gpiod_get_optional(priv->dev, "reset",
1062 GPIOD_ASIS);
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.
1071 msleep(20);
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)
1080 return -ENODEV;
1082 priv->ds = devm_kzalloc(&mdiodev->dev, sizeof(*priv->ds),
1083 QCA8K_NUM_PORTS);
1084 if (!priv->ds)
1085 return -ENOMEM;
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);
1098 static void
1099 qca8k_sw_remove(struct mdio_device *mdiodev)
1101 struct qca8k_priv *priv = dev_get_drvdata(&mdiodev->dev);
1102 int i;
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
1111 static void
1112 qca8k_set_pm(struct qca8k_priv *priv, int enable)
1114 int i;
1116 for (i = 0; i < QCA8K_NUM_PORTS; i++) {
1117 if (!priv->port_sts[i].enabled)
1118 continue;
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" },
1149 { /* sentinel */ },
1152 static struct mdio_driver qca8kmdio_driver = {
1153 .probe = qca8k_sw_probe,
1154 .remove = qca8k_sw_remove,
1155 .mdiodrv.driver = {
1156 .name = "qca8k",
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");