2 * ar8216.c: AR8216 switch driver
4 * Copyright (C) 2009 Felix Fietkau <nbd@nbd.name>
5 * Copyright (C) 2011-2012 Gabor Juhos <juhosg@openwrt.org>
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/list.h>
22 #include <linux/if_ether.h>
23 #include <linux/skbuff.h>
24 #include <linux/netdevice.h>
25 #include <linux/netlink.h>
26 #include <linux/of_device.h>
27 #include <linux/of_mdio.h>
28 #include <linux/of_net.h>
29 #include <linux/bitops.h>
30 #include <net/genetlink.h>
31 #include <linux/switch.h>
32 #include <linux/delay.h>
33 #include <linux/phy.h>
34 #include <linux/etherdevice.h>
35 #include <linux/lockdep.h>
36 #include <linux/ar8216_platform.h>
37 #include <linux/workqueue.h>
38 #include <linux/version.h>
42 extern const struct ar8xxx_chip ar8327_chip;
43 extern const struct ar8xxx_chip ar8337_chip;
45 #define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
47 #define MIB_DESC(_s , _o, _n) \
54 static const struct ar8xxx_mib_desc ar8216_mibs[] = {
55 MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
56 MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
57 MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
58 MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
59 MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
60 MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
61 MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
62 MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
63 MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
64 MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
65 MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
66 MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
67 MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
68 MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
69 MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
70 MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
71 MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
72 MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
73 MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
74 MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
75 MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
76 MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
77 MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
78 MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
79 MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
80 MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
81 MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
82 MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
83 MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
84 MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
85 MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
86 MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
87 MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
88 MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
89 MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
90 MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
91 MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
94 const struct ar8xxx_mib_desc ar8236_mibs[39] = {
95 MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
96 MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
97 MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
98 MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
99 MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
100 MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
101 MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
102 MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
103 MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
104 MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
105 MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
106 MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
107 MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
108 MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
109 MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
110 MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
111 MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
112 MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
113 MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
114 MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
115 MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
116 MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
117 MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
118 MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
119 MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
120 MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
121 MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
122 MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
123 MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
124 MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
125 MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
126 MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
127 MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
128 MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
129 MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
130 MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
131 MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
132 MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
133 MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
136 static DEFINE_MUTEX(ar8xxx_dev_list_lock);
137 static LIST_HEAD(ar8xxx_dev_list);
139 /* inspired by phy_poll_reset in drivers/net/phy/phy_device.c */
141 ar8xxx_phy_poll_reset(struct mii_bus *bus)
143 unsigned int sleep_msecs = 20;
146 for (elapsed = sleep_msecs; elapsed <= 600;
147 elapsed += sleep_msecs) {
149 for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
150 ret = mdiobus_read(bus, i, MII_BMCR);
153 if (ret & BMCR_RESET)
155 if (i == AR8XXX_NUM_PHYS - 1) {
156 usleep_range(1000, 2000);
165 ar8xxx_phy_check_aneg(struct phy_device *phydev)
169 if (phydev->autoneg != AUTONEG_ENABLE)
172 * BMCR_ANENABLE might have been cleared
173 * by phy_init_hw in certain kernel versions
174 * therefore check for it
176 ret = phy_read(phydev, MII_BMCR);
179 if (ret & BMCR_ANENABLE)
182 dev_info(&phydev->mdio.dev, "ANEG disabled, re-enabling ...\n");
183 ret |= BMCR_ANENABLE | BMCR_ANRESTART;
184 return phy_write(phydev, MII_BMCR, ret);
188 ar8xxx_phy_init(struct ar8xxx_priv *priv)
193 bus = priv->sw_mii_bus ?: priv->mii_bus;
194 for (i = 0; i < AR8XXX_NUM_PHYS; i++) {
195 if (priv->chip->phy_fixup)
196 priv->chip->phy_fixup(priv, i);
198 /* initialize the port itself */
199 mdiobus_write(bus, i, MII_ADVERTISE,
200 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
201 if (ar8xxx_has_gige(priv))
202 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
203 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
206 ar8xxx_phy_poll_reset(bus);
210 ar8xxx_mii_read32(struct ar8xxx_priv *priv, int phy_id, int regnum)
212 struct mii_bus *bus = priv->mii_bus;
215 lo = bus->read(bus, phy_id, regnum);
216 hi = bus->read(bus, phy_id, regnum + 1);
218 return (hi << 16) | lo;
222 ar8xxx_mii_write32(struct ar8xxx_priv *priv, int phy_id, int regnum, u32 val)
224 struct mii_bus *bus = priv->mii_bus;
228 hi = (u16) (val >> 16);
230 if (priv->chip->mii_lo_first)
232 bus->write(bus, phy_id, regnum, lo);
233 bus->write(bus, phy_id, regnum + 1, hi);
235 bus->write(bus, phy_id, regnum + 1, hi);
236 bus->write(bus, phy_id, regnum, lo);
241 ar8xxx_read(struct ar8xxx_priv *priv, int reg)
243 struct mii_bus *bus = priv->mii_bus;
247 split_addr((u32) reg, &r1, &r2, &page);
249 mutex_lock(&bus->mdio_lock);
251 bus->write(bus, 0x18, 0, page);
252 wait_for_page_switch();
253 val = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
255 mutex_unlock(&bus->mdio_lock);
261 ar8xxx_write(struct ar8xxx_priv *priv, int reg, u32 val)
263 struct mii_bus *bus = priv->mii_bus;
266 split_addr((u32) reg, &r1, &r2, &page);
268 mutex_lock(&bus->mdio_lock);
270 bus->write(bus, 0x18, 0, page);
271 wait_for_page_switch();
272 ar8xxx_mii_write32(priv, 0x10 | r2, r1, val);
274 mutex_unlock(&bus->mdio_lock);
278 ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
280 struct mii_bus *bus = priv->mii_bus;
284 split_addr((u32) reg, &r1, &r2, &page);
286 mutex_lock(&bus->mdio_lock);
288 bus->write(bus, 0x18, 0, page);
289 wait_for_page_switch();
291 ret = ar8xxx_mii_read32(priv, 0x10 | r2, r1);
294 ar8xxx_mii_write32(priv, 0x10 | r2, r1, ret);
296 mutex_unlock(&bus->mdio_lock);
301 ar8xxx_phy_dbg_read(struct ar8xxx_priv *priv, int phy_addr,
302 u16 dbg_addr, u16 *dbg_data)
304 struct mii_bus *bus = priv->mii_bus;
306 mutex_lock(&bus->mdio_lock);
307 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
308 *dbg_data = bus->read(bus, phy_addr, MII_ATH_DBG_DATA);
309 mutex_unlock(&bus->mdio_lock);
313 ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
314 u16 dbg_addr, u16 dbg_data)
316 struct mii_bus *bus = priv->mii_bus;
318 mutex_lock(&bus->mdio_lock);
319 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
320 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
321 mutex_unlock(&bus->mdio_lock);
325 ar8xxx_phy_mmd_prep(struct mii_bus *bus, int phy_addr, u16 addr, u16 reg)
327 bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
328 bus->write(bus, phy_addr, MII_ATH_MMD_DATA, reg);
329 bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr | 0x4000);
333 ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 reg, u16 data)
335 struct mii_bus *bus = priv->mii_bus;
337 mutex_lock(&bus->mdio_lock);
338 ar8xxx_phy_mmd_prep(bus, phy_addr, addr, reg);
339 bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
340 mutex_unlock(&bus->mdio_lock);
344 ar8xxx_phy_mmd_read(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 reg)
346 struct mii_bus *bus = priv->mii_bus;
349 mutex_lock(&bus->mdio_lock);
350 ar8xxx_phy_mmd_prep(bus, phy_addr, addr, reg);
351 data = bus->read(bus, phy_addr, MII_ATH_MMD_DATA);
352 mutex_unlock(&bus->mdio_lock);
358 ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
363 for (i = 0; i < timeout; i++) {
366 t = ar8xxx_read(priv, reg);
367 if ((t & mask) == val)
370 usleep_range(1000, 2000);
378 ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
380 unsigned mib_func = priv->chip->mib_func;
383 lockdep_assert_held(&priv->mib_lock);
385 /* Capture the hardware statistics for all ports */
386 ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
388 /* Wait for the capturing to complete. */
389 ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
400 ar8xxx_mib_capture(struct ar8xxx_priv *priv)
402 return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
406 ar8xxx_mib_flush(struct ar8xxx_priv *priv)
408 return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
412 ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
418 WARN_ON(port >= priv->dev.ports);
420 lockdep_assert_held(&priv->mib_lock);
422 base = priv->chip->reg_port_stats_start +
423 priv->chip->reg_port_stats_length * port;
425 mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
426 for (i = 0; i < priv->chip->num_mibs; i++) {
427 const struct ar8xxx_mib_desc *mib;
430 mib = &priv->chip->mib_decs[i];
431 t = ar8xxx_read(priv, base + mib->offset);
432 if (mib->size == 2) {
435 hi = ar8xxx_read(priv, base + mib->offset + 4);
448 ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
449 struct switch_port_link *link)
454 memset(link, '\0', sizeof(*link));
456 status = priv->chip->read_port_status(priv, port);
458 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
460 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
464 if (priv->get_port_link) {
467 err = priv->get_port_link(port);
476 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
477 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
478 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
480 if (link->aneg && link->duplex && priv->chip->read_port_eee_status)
481 link->eee = priv->chip->read_port_eee_status(priv, port);
483 speed = (status & AR8216_PORT_STATUS_SPEED) >>
484 AR8216_PORT_STATUS_SPEED_S;
487 case AR8216_PORT_SPEED_10M:
488 link->speed = SWITCH_PORT_SPEED_10;
490 case AR8216_PORT_SPEED_100M:
491 link->speed = SWITCH_PORT_SPEED_100;
493 case AR8216_PORT_SPEED_1000M:
494 link->speed = SWITCH_PORT_SPEED_1000;
497 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
502 static struct sk_buff *
503 ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
505 struct ar8xxx_priv *priv = dev->phy_ptr;
514 if (unlikely(skb_headroom(skb) < 2)) {
515 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
519 buf = skb_push(skb, 2);
527 dev_kfree_skb_any(skb);
532 ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
534 struct ar8xxx_priv *priv;
542 /* don't strip the header if vlan mode is disabled */
546 /* strip header, get vlan id */
550 /* check for vlan header presence */
551 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
556 /* no need to fix up packets coming from a tagged source */
557 if (priv->vlan_tagged & (1 << port))
560 /* lookup port vid from local table, the switch passes an invalid vlan id */
561 vlan = priv->vlan_id[priv->pvid[port]];
564 buf[14 + 2] |= vlan >> 8;
565 buf[15 + 2] = vlan & 0xff;
569 ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
575 t = ar8xxx_read(priv, reg);
576 if ((t & mask) == val)
586 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
587 (unsigned int) reg, t, mask, val);
592 ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
594 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
596 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
597 val &= AR8216_VTUDATA_MEMBER;
598 val |= AR8216_VTUDATA_VALID;
599 ar8xxx_write(priv, AR8216_REG_VTU_DATA, val);
601 op |= AR8216_VTU_ACTIVE;
602 ar8xxx_write(priv, AR8216_REG_VTU, op);
606 ar8216_vtu_flush(struct ar8xxx_priv *priv)
608 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
612 ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
616 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
617 ar8216_vtu_op(priv, op, port_mask);
621 ar8216_atu_flush(struct ar8xxx_priv *priv)
625 ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
627 ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_OP_FLUSH |
634 ar8216_atu_flush_port(struct ar8xxx_priv *priv, int port)
639 ret = ar8216_wait_bit(priv, AR8216_REG_ATU_FUNC0, AR8216_ATU_ACTIVE, 0);
641 t = (port << AR8216_ATU_PORT_NUM_S) | AR8216_ATU_OP_FLUSH_PORT;
642 t |= AR8216_ATU_ACTIVE;
643 ar8xxx_write(priv, AR8216_REG_ATU_FUNC0, t);
650 ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
652 return ar8xxx_read(priv, AR8216_REG_PORT_STATUS(port));
656 ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
663 pvid = priv->vlan_id[priv->pvid[port]];
664 if (priv->vlan_tagged & (1 << port))
665 egress = AR8216_OUT_ADD_VLAN;
667 egress = AR8216_OUT_STRIP_VLAN;
668 ingress = AR8216_IN_SECURE;
671 egress = AR8216_OUT_KEEP;
672 ingress = AR8216_IN_PORT_ONLY;
675 if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
676 header = AR8216_PORT_CTRL_HEADER;
680 ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
681 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
682 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
683 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
684 AR8216_PORT_CTRL_LEARN | header |
685 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
686 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
688 ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
689 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
690 AR8216_PORT_VLAN_DEFAULT_ID,
691 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
692 (ingress << AR8216_PORT_VLAN_MODE_S) |
693 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
697 ar8216_hw_init(struct ar8xxx_priv *priv)
699 if (priv->initialized)
702 ar8xxx_phy_init(priv);
704 priv->initialized = true;
709 ar8216_init_globals(struct ar8xxx_priv *priv)
711 /* standard atheros magic */
712 ar8xxx_write(priv, 0x38, 0xc000050e);
714 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
715 AR8216_GCTRL_MTU, 1518 + 8 + 2);
719 __ar8216_init_port(struct ar8xxx_priv *priv, int port,
720 bool cpu_ge, bool flow_en)
722 /* Enable port learning and tx */
723 ar8xxx_write(priv, AR8216_REG_PORT_CTRL(port),
724 AR8216_PORT_CTRL_LEARN |
725 (4 << AR8216_PORT_CTRL_STATE_S));
727 ar8xxx_write(priv, AR8216_REG_PORT_VLAN(port), 0);
729 if (port == AR8216_PORT_CPU) {
730 ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
731 AR8216_PORT_STATUS_LINK_UP |
732 (cpu_ge ? AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
733 AR8216_PORT_STATUS_TXMAC |
734 AR8216_PORT_STATUS_RXMAC |
735 (flow_en ? AR8216_PORT_STATUS_RXFLOW : 0) |
736 (flow_en ? AR8216_PORT_STATUS_TXFLOW : 0) |
737 AR8216_PORT_STATUS_DUPLEX);
739 ar8xxx_write(priv, AR8216_REG_PORT_STATUS(port),
740 AR8216_PORT_STATUS_LINK_AUTO);
745 ar8216_init_port(struct ar8xxx_priv *priv, int port)
747 __ar8216_init_port(priv, port, ar8xxx_has_gige(priv),
748 chip_is_ar8316(priv));
752 ar8216_wait_atu_ready(struct ar8xxx_priv *priv, u16 r2, u16 r1)
756 while (ar8xxx_mii_read32(priv, r2, r1) & AR8216_ATU_ACTIVE && --timeout) {
762 pr_err("ar8216: timeout waiting for atu to become ready\n");
765 static void ar8216_get_arl_entry(struct ar8xxx_priv *priv,
766 struct arl_entry *a, u32 *status, enum arl_op op)
768 struct mii_bus *bus = priv->mii_bus;
770 u16 r1_func0, r1_func1, r1_func2;
771 u32 t, val0, val1, val2;
773 split_addr(AR8216_REG_ATU_FUNC0, &r1_func0, &r2, &page);
776 r1_func1 = (AR8216_REG_ATU_FUNC1 >> 1) & 0x1e;
777 r1_func2 = (AR8216_REG_ATU_FUNC2 >> 1) & 0x1e;
780 case AR8XXX_ARL_INITIALIZE:
781 /* all ATU registers are on the same page
782 * therefore set page only once
784 bus->write(bus, 0x18, 0, page);
785 wait_for_page_switch();
787 ar8216_wait_atu_ready(priv, r2, r1_func0);
789 ar8xxx_mii_write32(priv, r2, r1_func0, AR8216_ATU_OP_GET_NEXT);
790 ar8xxx_mii_write32(priv, r2, r1_func1, 0);
791 ar8xxx_mii_write32(priv, r2, r1_func2, 0);
793 case AR8XXX_ARL_GET_NEXT:
794 t = ar8xxx_mii_read32(priv, r2, r1_func0);
795 t |= AR8216_ATU_ACTIVE;
796 ar8xxx_mii_write32(priv, r2, r1_func0, t);
797 ar8216_wait_atu_ready(priv, r2, r1_func0);
799 val0 = ar8xxx_mii_read32(priv, r2, r1_func0);
800 val1 = ar8xxx_mii_read32(priv, r2, r1_func1);
801 val2 = ar8xxx_mii_read32(priv, r2, r1_func2);
803 *status = (val2 & AR8216_ATU_STATUS) >> AR8216_ATU_STATUS_S;
807 a->portmap = (val2 & AR8216_ATU_PORTS) >> AR8216_ATU_PORTS_S;
808 a->mac[0] = (val0 & AR8216_ATU_ADDR5) >> AR8216_ATU_ADDR5_S;
809 a->mac[1] = (val0 & AR8216_ATU_ADDR4) >> AR8216_ATU_ADDR4_S;
810 a->mac[2] = (val1 & AR8216_ATU_ADDR3) >> AR8216_ATU_ADDR3_S;
811 a->mac[3] = (val1 & AR8216_ATU_ADDR2) >> AR8216_ATU_ADDR2_S;
812 a->mac[4] = (val1 & AR8216_ATU_ADDR1) >> AR8216_ATU_ADDR1_S;
813 a->mac[5] = (val1 & AR8216_ATU_ADDR0) >> AR8216_ATU_ADDR0_S;
819 ar8216_phy_read(struct ar8xxx_priv *priv, int addr, int regnum)
824 if (addr >= AR8216_NUM_PORTS)
826 t = (regnum << AR8216_MDIO_CTRL_REG_ADDR_S) |
827 (addr << AR8216_MDIO_CTRL_PHY_ADDR_S) |
828 AR8216_MDIO_CTRL_MASTER_EN |
829 AR8216_MDIO_CTRL_BUSY |
830 AR8216_MDIO_CTRL_CMD_READ;
832 ar8xxx_write(priv, AR8216_REG_MDIO_CTRL, t);
833 err = ar8xxx_reg_wait(priv, AR8216_REG_MDIO_CTRL,
834 AR8216_MDIO_CTRL_BUSY, 0, 5);
836 val = ar8xxx_read(priv, AR8216_REG_MDIO_CTRL);
838 return val & AR8216_MDIO_CTRL_DATA_M;
842 ar8216_phy_write(struct ar8xxx_priv *priv, int addr, int regnum, u16 val)
847 if (addr >= AR8216_NUM_PORTS)
850 t = (addr << AR8216_MDIO_CTRL_PHY_ADDR_S) |
851 (regnum << AR8216_MDIO_CTRL_REG_ADDR_S) |
852 AR8216_MDIO_CTRL_MASTER_EN |
853 AR8216_MDIO_CTRL_BUSY |
854 AR8216_MDIO_CTRL_CMD_WRITE |
857 ar8xxx_write(priv, AR8216_REG_MDIO_CTRL, t);
858 ret = ar8xxx_reg_wait(priv, AR8216_REG_MDIO_CTRL,
859 AR8216_MDIO_CTRL_BUSY, 0, 5);
865 ar8229_hw_init(struct ar8xxx_priv *priv)
869 if (priv->initialized)
872 ar8xxx_write(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET);
873 ar8xxx_reg_wait(priv, AR8216_REG_CTRL, AR8216_CTRL_RESET, 0, 1000);
875 phy_if_mode = of_get_phy_mode(priv->pdev->of_node);
877 if (phy_if_mode == PHY_INTERFACE_MODE_GMII) {
878 ar8xxx_write(priv, AR8229_REG_OPER_MODE0,
879 AR8229_OPER_MODE0_MAC_GMII_EN);
880 } else if (phy_if_mode == PHY_INTERFACE_MODE_MII) {
881 ar8xxx_write(priv, AR8229_REG_OPER_MODE0,
882 AR8229_OPER_MODE0_PHY_MII_EN);
884 pr_err("ar8229: unsupported mii mode\n");
889 ar8xxx_write(priv, AR8229_REG_OPER_MODE1,
890 AR8229_REG_OPER_MODE1_PHY4_MII_EN);
892 ar8xxx_phy_init(priv);
894 priv->initialized = true;
899 ar8229_init_globals(struct ar8xxx_priv *priv)
902 /* Enable CPU port, and disable mirror port */
903 ar8xxx_write(priv, AR8216_REG_GLOBAL_CPUPORT,
904 AR8216_GLOBAL_CPUPORT_EN |
905 (15 << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
907 /* Setup TAG priority mapping */
908 ar8xxx_write(priv, AR8216_REG_TAG_PRIORITY, 0xfa50);
910 /* Enable aging, MAC replacing */
911 ar8xxx_write(priv, AR8216_REG_ATU_CTRL,
912 0x2b /* 5 min age time */ |
913 AR8216_ATU_CTRL_AGE_EN |
914 AR8216_ATU_CTRL_LEARN_CHANGE);
916 /* Enable ARP frame acknowledge */
917 ar8xxx_reg_set(priv, AR8229_REG_QM_CTRL,
918 AR8229_QM_CTRL_ARP_EN);
920 /* Enable Broadcast/Multicast frames transmitted to the CPU */
921 ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
922 AR8229_FLOOD_MASK_BC_DP(0) |
923 AR8229_FLOOD_MASK_MC_DP(0));
926 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
927 AR8236_GCTRL_MTU, AR8236_GCTRL_MTU);
929 /* Enable MIB counters */
930 ar8xxx_reg_set(priv, AR8216_REG_MIB_FUNC,
933 /* setup Service TAG */
934 ar8xxx_rmw(priv, AR8216_REG_SERVICE_TAG, AR8216_SERVICE_TAG_M, 0);
938 ar8229_init_port(struct ar8xxx_priv *priv, int port)
940 __ar8216_init_port(priv, port, true, true);
944 ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 members)
950 pvid = priv->vlan_id[priv->pvid[port]];
951 if (priv->vlan_tagged & (1 << port))
952 egress = AR8216_OUT_ADD_VLAN;
954 egress = AR8216_OUT_STRIP_VLAN;
955 ingress = AR8216_IN_SECURE;
958 egress = AR8216_OUT_KEEP;
959 ingress = AR8216_IN_PORT_ONLY;
962 ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
963 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
964 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
965 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
966 AR8216_PORT_CTRL_LEARN |
967 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
968 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
970 ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
971 AR8236_PORT_VLAN_DEFAULT_ID,
972 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
974 ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
975 AR8236_PORT_VLAN2_VLAN_MODE |
976 AR8236_PORT_VLAN2_MEMBER,
977 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
978 (members << AR8236_PORT_VLAN2_MEMBER_S));
982 ar8236_init_globals(struct ar8xxx_priv *priv)
984 /* enable jumbo frames */
985 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
986 AR8316_GCTRL_MTU, 9018 + 8 + 2);
988 /* enable cpu port to receive arp frames */
989 ar8xxx_reg_set(priv, AR8216_REG_ATU_CTRL,
990 AR8236_ATU_CTRL_RES);
992 /* enable cpu port to receive multicast and broadcast frames */
993 ar8xxx_reg_set(priv, AR8216_REG_FLOOD_MASK,
994 AR8236_FM_CPU_BROADCAST_EN | AR8236_FM_CPU_BCAST_FWD_EN);
996 /* Enable MIB counters */
997 ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
998 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
1003 ar8316_hw_init(struct ar8xxx_priv *priv)
1007 val = ar8xxx_read(priv, AR8316_REG_POSTRIP);
1009 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
1010 if (priv->port4_phy) {
1011 /* value taken from Ubiquiti RouterStation Pro */
1012 newval = 0x81461bea;
1013 pr_info("ar8316: Using port 4 as PHY\n");
1015 newval = 0x01261be2;
1016 pr_info("ar8316: Using port 4 as switch port\n");
1018 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
1019 /* value taken from AVM Fritz!Box 7390 sources */
1020 newval = 0x010e5b71;
1022 /* no known value for phy interface */
1023 pr_err("ar8316: unsupported mii mode: %d.\n",
1024 priv->phy->interface);
1031 ar8xxx_write(priv, AR8316_REG_POSTRIP, newval);
1033 if (priv->port4_phy &&
1034 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
1035 /* work around for phy4 rgmii mode */
1036 ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
1038 ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
1040 ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
1044 ar8xxx_phy_init(priv);
1047 priv->initialized = true;
1052 ar8316_init_globals(struct ar8xxx_priv *priv)
1054 /* standard atheros magic */
1055 ar8xxx_write(priv, 0x38, 0xc000050e);
1057 /* enable cpu port to receive multicast and broadcast frames */
1058 ar8xxx_write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
1060 /* enable jumbo frames */
1061 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
1062 AR8316_GCTRL_MTU, 9018 + 8 + 2);
1064 /* Enable MIB counters */
1065 ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
1066 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
1071 ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1072 struct switch_val *val)
1074 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1075 priv->vlan = !!val->value.i;
1080 ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1081 struct switch_val *val)
1083 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1084 val->value.i = priv->vlan;
1090 ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
1092 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1094 /* make sure no invalid PVIDs get set */
1096 if (vlan < 0 || vlan >= dev->vlans ||
1097 port < 0 || port >= AR8X16_MAX_PORTS)
1100 priv->pvid[port] = vlan;
1105 ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
1107 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1109 if (port < 0 || port >= AR8X16_MAX_PORTS)
1112 *vlan = priv->pvid[port];
1117 ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
1118 struct switch_val *val)
1120 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1122 if (val->port_vlan >= AR8X16_MAX_VLANS)
1125 priv->vlan_id[val->port_vlan] = val->value.i;
1130 ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
1131 struct switch_val *val)
1133 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1134 val->value.i = priv->vlan_id[val->port_vlan];
1139 ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
1140 struct switch_port_link *link)
1142 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1144 ar8216_read_port_link(priv, port, link);
1149 ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
1151 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1155 if (val->port_vlan >= AR8X16_MAX_VLANS)
1158 ports = priv->vlan_table[val->port_vlan];
1160 for (i = 0; i < dev->ports; i++) {
1161 struct switch_port *p;
1163 if (!(ports & (1 << i)))
1166 p = &val->value.ports[val->len++];
1168 if (priv->vlan_tagged & (1 << i))
1169 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
1177 ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
1179 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1180 u8 *vt = &priv->vlan_table[val->port_vlan];
1184 for (i = 0; i < val->len; i++) {
1185 struct switch_port *p = &val->value.ports[i];
1187 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
1188 priv->vlan_tagged |= (1 << p->id);
1190 priv->vlan_tagged &= ~(1 << p->id);
1191 priv->pvid[p->id] = val->port_vlan;
1193 /* make sure that an untagged port does not
1194 * appear in other vlans */
1195 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1196 if (j == val->port_vlan)
1198 priv->vlan_table[j] &= ~(1 << p->id);
1208 ar8216_set_mirror_regs(struct ar8xxx_priv *priv)
1212 /* reset all mirror registers */
1213 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
1214 AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
1215 (0xF << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
1216 for (port = 0; port < AR8216_NUM_PORTS; port++) {
1217 ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
1218 AR8216_PORT_CTRL_MIRROR_RX);
1220 ar8xxx_reg_clear(priv, AR8216_REG_PORT_CTRL(port),
1221 AR8216_PORT_CTRL_MIRROR_TX);
1224 /* now enable mirroring if necessary */
1225 if (priv->source_port >= AR8216_NUM_PORTS ||
1226 priv->monitor_port >= AR8216_NUM_PORTS ||
1227 priv->source_port == priv->monitor_port) {
1231 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CPUPORT,
1232 AR8216_GLOBAL_CPUPORT_MIRROR_PORT,
1233 (priv->monitor_port << AR8216_GLOBAL_CPUPORT_MIRROR_PORT_S));
1235 if (priv->mirror_rx)
1236 ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
1237 AR8216_PORT_CTRL_MIRROR_RX);
1239 if (priv->mirror_tx)
1240 ar8xxx_reg_set(priv, AR8216_REG_PORT_CTRL(priv->source_port),
1241 AR8216_PORT_CTRL_MIRROR_TX);
1245 ar8xxx_age_time_val(int age_time)
1247 return (age_time + AR8XXX_REG_ARL_CTRL_AGE_TIME_SECS / 2) /
1248 AR8XXX_REG_ARL_CTRL_AGE_TIME_SECS;
1252 ar8xxx_set_age_time(struct ar8xxx_priv *priv, int reg)
1254 u32 age_time = ar8xxx_age_time_val(priv->arl_age_time);
1255 ar8xxx_rmw(priv, reg, AR8216_ATU_CTRL_AGE_TIME, age_time << AR8216_ATU_CTRL_AGE_TIME_S);
1259 ar8xxx_sw_hw_apply(struct switch_dev *dev)
1261 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1262 const struct ar8xxx_chip *chip = priv->chip;
1263 u8 portmask[AR8X16_MAX_PORTS];
1266 mutex_lock(&priv->reg_mutex);
1267 /* flush all vlan translation unit entries */
1268 priv->chip->vtu_flush(priv);
1270 memset(portmask, 0, sizeof(portmask));
1272 /* calculate the port destination masks and load vlans
1273 * into the vlan translation unit */
1274 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1275 u8 vp = priv->vlan_table[j];
1280 for (i = 0; i < dev->ports; i++) {
1283 portmask[i] |= vp & ~mask;
1286 chip->vtu_load_vlan(priv, priv->vlan_id[j],
1287 priv->vlan_table[j]);
1291 * isolate all ports, but connect them to the cpu port */
1292 for (i = 0; i < dev->ports; i++) {
1293 if (i == AR8216_PORT_CPU)
1296 portmask[i] = 1 << AR8216_PORT_CPU;
1297 portmask[AR8216_PORT_CPU] |= (1 << i);
1301 /* update the port destination mask registers and tag settings */
1302 for (i = 0; i < dev->ports; i++) {
1303 chip->setup_port(priv, i, portmask[i]);
1306 chip->set_mirror_regs(priv);
1309 if (chip->reg_arl_ctrl)
1310 ar8xxx_set_age_time(priv, chip->reg_arl_ctrl);
1312 mutex_unlock(&priv->reg_mutex);
1317 ar8xxx_sw_reset_switch(struct switch_dev *dev)
1319 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1320 const struct ar8xxx_chip *chip = priv->chip;
1323 mutex_lock(&priv->reg_mutex);
1324 memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
1325 offsetof(struct ar8xxx_priv, vlan));
1327 for (i = 0; i < AR8X16_MAX_VLANS; i++)
1328 priv->vlan_id[i] = i;
1330 /* Configure all ports */
1331 for (i = 0; i < dev->ports; i++)
1332 chip->init_port(priv, i);
1334 priv->mirror_rx = false;
1335 priv->mirror_tx = false;
1336 priv->source_port = 0;
1337 priv->monitor_port = 0;
1338 priv->arl_age_time = AR8XXX_DEFAULT_ARL_AGE_TIME;
1340 chip->init_globals(priv);
1341 chip->atu_flush(priv);
1343 mutex_unlock(&priv->reg_mutex);
1345 return chip->sw_hw_apply(dev);
1349 ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
1350 const struct switch_attr *attr,
1351 struct switch_val *val)
1353 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1357 if (!ar8xxx_has_mib_counters(priv))
1360 mutex_lock(&priv->mib_lock);
1362 len = priv->dev.ports * priv->chip->num_mibs *
1363 sizeof(*priv->mib_stats);
1364 memset(priv->mib_stats, '\0', len);
1365 ret = ar8xxx_mib_flush(priv);
1372 mutex_unlock(&priv->mib_lock);
1377 ar8xxx_sw_set_mirror_rx_enable(struct switch_dev *dev,
1378 const struct switch_attr *attr,
1379 struct switch_val *val)
1381 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1383 mutex_lock(&priv->reg_mutex);
1384 priv->mirror_rx = !!val->value.i;
1385 priv->chip->set_mirror_regs(priv);
1386 mutex_unlock(&priv->reg_mutex);
1392 ar8xxx_sw_get_mirror_rx_enable(struct switch_dev *dev,
1393 const struct switch_attr *attr,
1394 struct switch_val *val)
1396 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1397 val->value.i = priv->mirror_rx;
1402 ar8xxx_sw_set_mirror_tx_enable(struct switch_dev *dev,
1403 const struct switch_attr *attr,
1404 struct switch_val *val)
1406 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1408 mutex_lock(&priv->reg_mutex);
1409 priv->mirror_tx = !!val->value.i;
1410 priv->chip->set_mirror_regs(priv);
1411 mutex_unlock(&priv->reg_mutex);
1417 ar8xxx_sw_get_mirror_tx_enable(struct switch_dev *dev,
1418 const struct switch_attr *attr,
1419 struct switch_val *val)
1421 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1422 val->value.i = priv->mirror_tx;
1427 ar8xxx_sw_set_mirror_monitor_port(struct switch_dev *dev,
1428 const struct switch_attr *attr,
1429 struct switch_val *val)
1431 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1433 mutex_lock(&priv->reg_mutex);
1434 priv->monitor_port = val->value.i;
1435 priv->chip->set_mirror_regs(priv);
1436 mutex_unlock(&priv->reg_mutex);
1442 ar8xxx_sw_get_mirror_monitor_port(struct switch_dev *dev,
1443 const struct switch_attr *attr,
1444 struct switch_val *val)
1446 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1447 val->value.i = priv->monitor_port;
1452 ar8xxx_sw_set_mirror_source_port(struct switch_dev *dev,
1453 const struct switch_attr *attr,
1454 struct switch_val *val)
1456 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1458 mutex_lock(&priv->reg_mutex);
1459 priv->source_port = val->value.i;
1460 priv->chip->set_mirror_regs(priv);
1461 mutex_unlock(&priv->reg_mutex);
1467 ar8xxx_sw_get_mirror_source_port(struct switch_dev *dev,
1468 const struct switch_attr *attr,
1469 struct switch_val *val)
1471 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1472 val->value.i = priv->source_port;
1477 ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
1478 const struct switch_attr *attr,
1479 struct switch_val *val)
1481 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1485 if (!ar8xxx_has_mib_counters(priv))
1488 port = val->port_vlan;
1489 if (port >= dev->ports)
1492 mutex_lock(&priv->mib_lock);
1493 ret = ar8xxx_mib_capture(priv);
1497 ar8xxx_mib_fetch_port_stat(priv, port, true);
1502 mutex_unlock(&priv->mib_lock);
1507 ar8xxx_byte_to_str(char *buf, int len, u64 byte)
1512 if (byte >= 0x40000000) { /* 1 GiB */
1513 b = byte * 10 / 0x40000000;
1515 } else if (byte >= 0x100000) { /* 1 MiB */
1516 b = byte * 10 / 0x100000;
1518 } else if (byte >= 0x400) { /* 1 KiB */
1519 b = byte * 10 / 0x400;
1525 if (strcmp(unit, "Byte"))
1526 snprintf(buf, len, "%lu.%lu %s", b / 10, b % 10, unit);
1528 snprintf(buf, len, "%lu %s", b, unit);
1532 ar8xxx_sw_get_port_mib(struct switch_dev *dev,
1533 const struct switch_attr *attr,
1534 struct switch_val *val)
1536 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1537 const struct ar8xxx_chip *chip = priv->chip;
1538 u64 *mib_stats, mib_data;
1541 char *buf = priv->buf;
1543 const char *mib_name;
1545 bool mib_stats_empty = true;
1547 if (!ar8xxx_has_mib_counters(priv))
1550 port = val->port_vlan;
1551 if (port >= dev->ports)
1554 mutex_lock(&priv->mib_lock);
1555 ret = ar8xxx_mib_capture(priv);
1559 ar8xxx_mib_fetch_port_stat(priv, port, false);
1561 len += snprintf(buf + len, sizeof(priv->buf) - len,
1564 mib_stats = &priv->mib_stats[port * chip->num_mibs];
1565 for (i = 0; i < chip->num_mibs; i++) {
1566 mib_name = chip->mib_decs[i].name;
1567 mib_data = mib_stats[i];
1568 len += snprintf(buf + len, sizeof(priv->buf) - len,
1569 "%-12s: %llu\n", mib_name, mib_data);
1570 if ((!strcmp(mib_name, "TxByte") ||
1571 !strcmp(mib_name, "RxGoodByte")) &&
1573 ar8xxx_byte_to_str(buf1, sizeof(buf1), mib_data);
1574 --len; /* discard newline at the end of buf */
1575 len += snprintf(buf + len, sizeof(priv->buf) - len,
1578 if (mib_stats_empty && mib_data)
1579 mib_stats_empty = false;
1582 if (mib_stats_empty)
1583 len = snprintf(buf, sizeof(priv->buf), "No MIB data");
1591 mutex_unlock(&priv->mib_lock);
1596 ar8xxx_sw_set_arl_age_time(struct switch_dev *dev, const struct switch_attr *attr,
1597 struct switch_val *val)
1599 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1600 int age_time = val->value.i;
1606 age_time_val = ar8xxx_age_time_val(age_time);
1607 if (age_time_val == 0 || age_time_val > 0xffff)
1610 priv->arl_age_time = age_time;
1615 ar8xxx_sw_get_arl_age_time(struct switch_dev *dev, const struct switch_attr *attr,
1616 struct switch_val *val)
1618 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1619 val->value.i = priv->arl_age_time;
1624 ar8xxx_sw_get_arl_table(struct switch_dev *dev,
1625 const struct switch_attr *attr,
1626 struct switch_val *val)
1628 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1629 struct mii_bus *bus = priv->mii_bus;
1630 const struct ar8xxx_chip *chip = priv->chip;
1631 char *buf = priv->arl_buf;
1632 int i, j, k, len = 0;
1633 struct arl_entry *a, *a1;
1636 if (!chip->get_arl_entry)
1639 mutex_lock(&priv->reg_mutex);
1640 mutex_lock(&bus->mdio_lock);
1642 chip->get_arl_entry(priv, NULL, NULL, AR8XXX_ARL_INITIALIZE);
1644 for(i = 0; i < AR8XXX_NUM_ARL_RECORDS; ++i) {
1645 a = &priv->arl_table[i];
1647 chip->get_arl_entry(priv, a, &status, AR8XXX_ARL_GET_NEXT);
1653 * ARL table can include multiple valid entries
1654 * per MAC, just with differing status codes
1656 for (j = 0; j < i; ++j) {
1657 a1 = &priv->arl_table[j];
1658 if (!memcmp(a->mac, a1->mac, sizeof(a->mac))) {
1659 /* ignore ports already seen in former entry */
1660 a->portmap &= ~a1->portmap;
1667 mutex_unlock(&bus->mdio_lock);
1669 len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
1670 "address resolution table\n");
1672 if (i == AR8XXX_NUM_ARL_RECORDS)
1673 len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
1674 "Too many entries found, displaying the first %d only!\n",
1675 AR8XXX_NUM_ARL_RECORDS);
1677 for (j = 0; j < priv->dev.ports; ++j) {
1678 for (k = 0; k < i; ++k) {
1679 a = &priv->arl_table[k];
1680 if (!(a->portmap & BIT(j)))
1682 len += snprintf(buf + len, sizeof(priv->arl_buf) - len,
1683 "Port %d: MAC %02x:%02x:%02x:%02x:%02x:%02x\n",
1685 a->mac[5], a->mac[4], a->mac[3],
1686 a->mac[2], a->mac[1], a->mac[0]);
1693 mutex_unlock(&priv->reg_mutex);
1699 ar8xxx_sw_set_flush_arl_table(struct switch_dev *dev,
1700 const struct switch_attr *attr,
1701 struct switch_val *val)
1703 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1706 mutex_lock(&priv->reg_mutex);
1707 ret = priv->chip->atu_flush(priv);
1708 mutex_unlock(&priv->reg_mutex);
1714 ar8xxx_sw_set_flush_port_arl_table(struct switch_dev *dev,
1715 const struct switch_attr *attr,
1716 struct switch_val *val)
1718 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1721 port = val->port_vlan;
1722 if (port >= dev->ports)
1725 mutex_lock(&priv->reg_mutex);
1726 ret = priv->chip->atu_flush_port(priv, port);
1727 mutex_unlock(&priv->reg_mutex);
1733 ar8xxx_phy_read(struct mii_bus *bus, int phy_addr, int reg_addr)
1735 struct ar8xxx_priv *priv = bus->priv;
1736 return priv->chip->phy_read(priv, phy_addr, reg_addr);
1740 ar8xxx_phy_write(struct mii_bus *bus, int phy_addr, int reg_addr,
1743 struct ar8xxx_priv *priv = bus->priv;
1744 return priv->chip->phy_write(priv, phy_addr, reg_addr, reg_val);
1747 static const struct switch_attr ar8xxx_sw_attr_globals[] = {
1749 .type = SWITCH_TYPE_INT,
1750 .name = "enable_vlan",
1751 .description = "Enable VLAN mode",
1752 .set = ar8xxx_sw_set_vlan,
1753 .get = ar8xxx_sw_get_vlan,
1757 .type = SWITCH_TYPE_NOVAL,
1758 .name = "reset_mibs",
1759 .description = "Reset all MIB counters",
1760 .set = ar8xxx_sw_set_reset_mibs,
1763 .type = SWITCH_TYPE_INT,
1764 .name = "enable_mirror_rx",
1765 .description = "Enable mirroring of RX packets",
1766 .set = ar8xxx_sw_set_mirror_rx_enable,
1767 .get = ar8xxx_sw_get_mirror_rx_enable,
1771 .type = SWITCH_TYPE_INT,
1772 .name = "enable_mirror_tx",
1773 .description = "Enable mirroring of TX packets",
1774 .set = ar8xxx_sw_set_mirror_tx_enable,
1775 .get = ar8xxx_sw_get_mirror_tx_enable,
1779 .type = SWITCH_TYPE_INT,
1780 .name = "mirror_monitor_port",
1781 .description = "Mirror monitor port",
1782 .set = ar8xxx_sw_set_mirror_monitor_port,
1783 .get = ar8xxx_sw_get_mirror_monitor_port,
1784 .max = AR8216_NUM_PORTS - 1
1787 .type = SWITCH_TYPE_INT,
1788 .name = "mirror_source_port",
1789 .description = "Mirror source port",
1790 .set = ar8xxx_sw_set_mirror_source_port,
1791 .get = ar8xxx_sw_get_mirror_source_port,
1792 .max = AR8216_NUM_PORTS - 1
1795 .type = SWITCH_TYPE_STRING,
1796 .name = "arl_table",
1797 .description = "Get ARL table",
1799 .get = ar8xxx_sw_get_arl_table,
1802 .type = SWITCH_TYPE_NOVAL,
1803 .name = "flush_arl_table",
1804 .description = "Flush ARL table",
1805 .set = ar8xxx_sw_set_flush_arl_table,
1809 const struct switch_attr ar8xxx_sw_attr_port[] = {
1811 .type = SWITCH_TYPE_NOVAL,
1812 .name = "reset_mib",
1813 .description = "Reset single port MIB counters",
1814 .set = ar8xxx_sw_set_port_reset_mib,
1817 .type = SWITCH_TYPE_STRING,
1819 .description = "Get port's MIB counters",
1821 .get = ar8xxx_sw_get_port_mib,
1824 .type = SWITCH_TYPE_NOVAL,
1825 .name = "flush_arl_table",
1826 .description = "Flush port's ARL table entries",
1827 .set = ar8xxx_sw_set_flush_port_arl_table,
1831 const struct switch_attr ar8xxx_sw_attr_vlan[1] = {
1833 .type = SWITCH_TYPE_INT,
1835 .description = "VLAN ID (0-4094)",
1836 .set = ar8xxx_sw_set_vid,
1837 .get = ar8xxx_sw_get_vid,
1842 static const struct switch_dev_ops ar8xxx_sw_ops = {
1844 .attr = ar8xxx_sw_attr_globals,
1845 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
1848 .attr = ar8xxx_sw_attr_port,
1849 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
1852 .attr = ar8xxx_sw_attr_vlan,
1853 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
1855 .get_port_pvid = ar8xxx_sw_get_pvid,
1856 .set_port_pvid = ar8xxx_sw_set_pvid,
1857 .get_vlan_ports = ar8xxx_sw_get_ports,
1858 .set_vlan_ports = ar8xxx_sw_set_ports,
1859 .apply_config = ar8xxx_sw_hw_apply,
1860 .reset_switch = ar8xxx_sw_reset_switch,
1861 .get_port_link = ar8xxx_sw_get_port_link,
1862 /* The following op is disabled as it hogs the CPU and degrades performance.
1863 An implementation has been attempted in 4d8a66d but reading MIB data is slow
1866 The high CPU load has been traced down to the ar8xxx_reg_wait() call in
1867 ar8xxx_mib_op(), which has to usleep_range() till the MIB busy flag set by
1868 the request to update the MIB counter is cleared. */
1870 .get_port_stats = ar8xxx_sw_get_port_stats,
1874 static const struct ar8xxx_chip ar8216_chip = {
1875 .caps = AR8XXX_CAP_MIB_COUNTERS,
1877 .reg_port_stats_start = 0x19000,
1878 .reg_port_stats_length = 0xa0,
1879 .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
1881 .name = "Atheros AR8216",
1882 .ports = AR8216_NUM_PORTS,
1883 .vlans = AR8216_NUM_VLANS,
1884 .swops = &ar8xxx_sw_ops,
1886 .hw_init = ar8216_hw_init,
1887 .init_globals = ar8216_init_globals,
1888 .init_port = ar8216_init_port,
1889 .setup_port = ar8216_setup_port,
1890 .read_port_status = ar8216_read_port_status,
1891 .atu_flush = ar8216_atu_flush,
1892 .atu_flush_port = ar8216_atu_flush_port,
1893 .vtu_flush = ar8216_vtu_flush,
1894 .vtu_load_vlan = ar8216_vtu_load_vlan,
1895 .set_mirror_regs = ar8216_set_mirror_regs,
1896 .get_arl_entry = ar8216_get_arl_entry,
1897 .sw_hw_apply = ar8xxx_sw_hw_apply,
1899 .num_mibs = ARRAY_SIZE(ar8216_mibs),
1900 .mib_decs = ar8216_mibs,
1901 .mib_func = AR8216_REG_MIB_FUNC
1904 static const struct ar8xxx_chip ar8229_chip = {
1905 .caps = AR8XXX_CAP_MIB_COUNTERS,
1907 .reg_port_stats_start = 0x20000,
1908 .reg_port_stats_length = 0x100,
1909 .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
1911 .name = "Atheros AR8229",
1912 .ports = AR8216_NUM_PORTS,
1913 .vlans = AR8216_NUM_VLANS,
1914 .swops = &ar8xxx_sw_ops,
1916 .hw_init = ar8229_hw_init,
1917 .init_globals = ar8229_init_globals,
1918 .init_port = ar8229_init_port,
1919 .phy_read = ar8216_phy_read,
1920 .phy_write = ar8216_phy_write,
1921 .setup_port = ar8236_setup_port,
1922 .read_port_status = ar8216_read_port_status,
1923 .atu_flush = ar8216_atu_flush,
1924 .atu_flush_port = ar8216_atu_flush_port,
1925 .vtu_flush = ar8216_vtu_flush,
1926 .vtu_load_vlan = ar8216_vtu_load_vlan,
1927 .set_mirror_regs = ar8216_set_mirror_regs,
1928 .get_arl_entry = ar8216_get_arl_entry,
1929 .sw_hw_apply = ar8xxx_sw_hw_apply,
1931 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1932 .mib_decs = ar8236_mibs,
1933 .mib_func = AR8216_REG_MIB_FUNC
1936 static const struct ar8xxx_chip ar8236_chip = {
1937 .caps = AR8XXX_CAP_MIB_COUNTERS,
1939 .reg_port_stats_start = 0x20000,
1940 .reg_port_stats_length = 0x100,
1941 .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
1943 .name = "Atheros AR8236",
1944 .ports = AR8216_NUM_PORTS,
1945 .vlans = AR8216_NUM_VLANS,
1946 .swops = &ar8xxx_sw_ops,
1948 .hw_init = ar8216_hw_init,
1949 .init_globals = ar8236_init_globals,
1950 .init_port = ar8216_init_port,
1951 .setup_port = ar8236_setup_port,
1952 .read_port_status = ar8216_read_port_status,
1953 .atu_flush = ar8216_atu_flush,
1954 .atu_flush_port = ar8216_atu_flush_port,
1955 .vtu_flush = ar8216_vtu_flush,
1956 .vtu_load_vlan = ar8216_vtu_load_vlan,
1957 .set_mirror_regs = ar8216_set_mirror_regs,
1958 .get_arl_entry = ar8216_get_arl_entry,
1959 .sw_hw_apply = ar8xxx_sw_hw_apply,
1961 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1962 .mib_decs = ar8236_mibs,
1963 .mib_func = AR8216_REG_MIB_FUNC
1966 static const struct ar8xxx_chip ar8316_chip = {
1967 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
1969 .reg_port_stats_start = 0x20000,
1970 .reg_port_stats_length = 0x100,
1971 .reg_arl_ctrl = AR8216_REG_ATU_CTRL,
1973 .name = "Atheros AR8316",
1974 .ports = AR8216_NUM_PORTS,
1975 .vlans = AR8X16_MAX_VLANS,
1976 .swops = &ar8xxx_sw_ops,
1978 .hw_init = ar8316_hw_init,
1979 .init_globals = ar8316_init_globals,
1980 .init_port = ar8216_init_port,
1981 .setup_port = ar8216_setup_port,
1982 .read_port_status = ar8216_read_port_status,
1983 .atu_flush = ar8216_atu_flush,
1984 .atu_flush_port = ar8216_atu_flush_port,
1985 .vtu_flush = ar8216_vtu_flush,
1986 .vtu_load_vlan = ar8216_vtu_load_vlan,
1987 .set_mirror_regs = ar8216_set_mirror_regs,
1988 .get_arl_entry = ar8216_get_arl_entry,
1989 .sw_hw_apply = ar8xxx_sw_hw_apply,
1991 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1992 .mib_decs = ar8236_mibs,
1993 .mib_func = AR8216_REG_MIB_FUNC
1997 ar8xxx_read_id(struct ar8xxx_priv *priv)
2003 val = ar8xxx_read(priv, AR8216_REG_CTRL);
2007 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
2008 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
2011 val = ar8xxx_read(priv, AR8216_REG_CTRL);
2015 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
2020 priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
2021 priv->chip_rev = (id & AR8216_CTRL_REVISION);
2026 ar8xxx_id_chip(struct ar8xxx_priv *priv)
2030 ret = ar8xxx_read_id(priv);
2034 switch (priv->chip_ver) {
2035 case AR8XXX_VER_AR8216:
2036 priv->chip = &ar8216_chip;
2038 case AR8XXX_VER_AR8236:
2039 priv->chip = &ar8236_chip;
2041 case AR8XXX_VER_AR8316:
2042 priv->chip = &ar8316_chip;
2044 case AR8XXX_VER_AR8327:
2045 priv->chip = &ar8327_chip;
2047 case AR8XXX_VER_AR8337:
2048 priv->chip = &ar8337_chip;
2051 pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
2052 priv->chip_ver, priv->chip_rev);
2061 ar8xxx_mib_work_func(struct work_struct *work)
2063 struct ar8xxx_priv *priv;
2066 priv = container_of(work, struct ar8xxx_priv, mib_work.work);
2068 mutex_lock(&priv->mib_lock);
2070 err = ar8xxx_mib_capture(priv);
2074 ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
2077 priv->mib_next_port++;
2078 if (priv->mib_next_port >= priv->dev.ports)
2079 priv->mib_next_port = 0;
2081 mutex_unlock(&priv->mib_lock);
2082 schedule_delayed_work(&priv->mib_work,
2083 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
2087 ar8xxx_mib_init(struct ar8xxx_priv *priv)
2091 if (!ar8xxx_has_mib_counters(priv))
2094 BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
2096 len = priv->dev.ports * priv->chip->num_mibs *
2097 sizeof(*priv->mib_stats);
2098 priv->mib_stats = kzalloc(len, GFP_KERNEL);
2100 if (!priv->mib_stats)
2107 ar8xxx_mib_start(struct ar8xxx_priv *priv)
2109 if (!ar8xxx_has_mib_counters(priv))
2112 schedule_delayed_work(&priv->mib_work,
2113 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
2117 ar8xxx_mib_stop(struct ar8xxx_priv *priv)
2119 if (!ar8xxx_has_mib_counters(priv))
2122 cancel_delayed_work_sync(&priv->mib_work);
2125 static struct ar8xxx_priv *
2128 struct ar8xxx_priv *priv;
2130 priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
2134 mutex_init(&priv->reg_mutex);
2135 mutex_init(&priv->mib_lock);
2136 INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
2142 ar8xxx_free(struct ar8xxx_priv *priv)
2144 if (priv->chip && priv->chip->cleanup)
2145 priv->chip->cleanup(priv);
2147 kfree(priv->chip_data);
2148 kfree(priv->mib_stats);
2153 ar8xxx_probe_switch(struct ar8xxx_priv *priv)
2155 const struct ar8xxx_chip *chip;
2156 struct switch_dev *swdev;
2162 swdev->cpu_port = AR8216_PORT_CPU;
2163 swdev->name = chip->name;
2164 swdev->vlans = chip->vlans;
2165 swdev->ports = chip->ports;
2166 swdev->ops = chip->swops;
2168 ret = ar8xxx_mib_init(priv);
2176 ar8xxx_start(struct ar8xxx_priv *priv)
2182 ret = priv->chip->hw_init(priv);
2186 ret = ar8xxx_sw_reset_switch(&priv->dev);
2192 ar8xxx_mib_start(priv);
2198 ar8xxx_phy_config_init(struct phy_device *phydev)
2200 struct ar8xxx_priv *priv = phydev->priv;
2201 struct net_device *dev = phydev->attached_dev;
2207 if (priv->chip->config_at_probe)
2208 return ar8xxx_phy_check_aneg(phydev);
2212 if (phydev->mdio.addr != 0) {
2213 if (chip_is_ar8316(priv)) {
2214 /* switch device has been initialized, reinit */
2215 priv->dev.ports = (AR8216_NUM_PORTS - 1);
2216 priv->initialized = false;
2217 priv->port4_phy = true;
2218 ar8316_hw_init(priv);
2225 ret = ar8xxx_start(priv);
2229 /* VID fixup only needed on ar8216 */
2230 if (chip_is_ar8216(priv)) {
2231 dev->phy_ptr = priv;
2232 dev->priv_flags |= IFF_NO_IP_ALIGN;
2233 dev->eth_mangle_rx = ar8216_mangle_rx;
2234 dev->eth_mangle_tx = ar8216_mangle_tx;
2241 ar8xxx_check_link_states(struct ar8xxx_priv *priv)
2243 bool link_new, changed = false;
2247 mutex_lock(&priv->reg_mutex);
2249 for (i = 0; i < priv->dev.ports; i++) {
2250 status = priv->chip->read_port_status(priv, i);
2251 link_new = !!(status & AR8216_PORT_STATUS_LINK_UP);
2252 if (link_new == priv->link_up[i])
2255 priv->link_up[i] = link_new;
2257 /* flush ARL entries for this port if it went down*/
2259 priv->chip->atu_flush_port(priv, i);
2260 dev_info(&priv->phy->mdio.dev, "Port %d is %s\n",
2261 i, link_new ? "up" : "down");
2264 mutex_unlock(&priv->reg_mutex);
2270 ar8xxx_phy_read_status(struct phy_device *phydev)
2272 struct ar8xxx_priv *priv = phydev->priv;
2273 struct switch_port_link link;
2275 /* check for switch port link changes */
2276 if (phydev->state == PHY_CHANGELINK)
2277 ar8xxx_check_link_states(priv);
2279 if (phydev->mdio.addr != 0)
2280 return genphy_read_status(phydev);
2282 ar8216_read_port_link(priv, phydev->mdio.addr, &link);
2283 phydev->link = !!link.link;
2287 switch (link.speed) {
2288 case SWITCH_PORT_SPEED_10:
2289 phydev->speed = SPEED_10;
2291 case SWITCH_PORT_SPEED_100:
2292 phydev->speed = SPEED_100;
2294 case SWITCH_PORT_SPEED_1000:
2295 phydev->speed = SPEED_1000;
2300 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
2302 phydev->state = PHY_RUNNING;
2303 netif_carrier_on(phydev->attached_dev);
2304 if (phydev->adjust_link)
2305 phydev->adjust_link(phydev->attached_dev);
2311 ar8xxx_phy_config_aneg(struct phy_device *phydev)
2313 if (phydev->mdio.addr == 0)
2316 return genphy_config_aneg(phydev);
2319 static const u32 ar8xxx_phy_ids[] = {
2321 0x004dd034, /* AR8327 */
2322 0x004dd036, /* AR8337 */
2325 0x004dd043, /* AR8236 */
2329 ar8xxx_phy_match(u32 phy_id)
2333 for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
2334 if (phy_id == ar8xxx_phy_ids[i])
2341 ar8xxx_is_possible(struct mii_bus *bus)
2343 unsigned int i, found_phys = 0;
2345 for (i = 0; i < 5; i++) {
2348 phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
2349 phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
2350 if (ar8xxx_phy_match(phy_id)) {
2352 } else if (phy_id) {
2353 pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
2354 dev_name(&bus->dev), i, phy_id);
2357 return !!found_phys;
2361 ar8xxx_phy_probe(struct phy_device *phydev)
2363 struct ar8xxx_priv *priv;
2364 struct switch_dev *swdev;
2367 /* skip PHYs at unused adresses */
2368 if (phydev->mdio.addr != 0 && phydev->mdio.addr != 3 && phydev->mdio.addr != 4)
2371 if (!ar8xxx_is_possible(phydev->mdio.bus))
2374 mutex_lock(&ar8xxx_dev_list_lock);
2375 list_for_each_entry(priv, &ar8xxx_dev_list, list)
2376 if (priv->mii_bus == phydev->mdio.bus)
2379 priv = ar8xxx_create();
2385 priv->mii_bus = phydev->mdio.bus;
2386 priv->pdev = &phydev->mdio.dev;
2388 ret = ar8xxx_id_chip(priv);
2392 ret = ar8xxx_probe_switch(priv);
2397 swdev->alias = dev_name(&priv->mii_bus->dev);
2398 ret = register_switch(swdev, NULL);
2402 pr_info("%s: %s rev. %u switch registered on %s\n",
2403 swdev->devname, swdev->name, priv->chip_rev,
2404 dev_name(&priv->mii_bus->dev));
2406 list_add(&priv->list, &ar8xxx_dev_list);
2411 if (phydev->mdio.addr == 0) {
2412 if (ar8xxx_has_gige(priv)) {
2413 phydev->supported = SUPPORTED_1000baseT_Full;
2414 phydev->advertising = ADVERTISED_1000baseT_Full;
2416 phydev->supported = SUPPORTED_100baseT_Full;
2417 phydev->advertising = ADVERTISED_100baseT_Full;
2420 if (priv->chip->config_at_probe) {
2423 ret = ar8xxx_start(priv);
2425 goto err_unregister_switch;
2428 if (ar8xxx_has_gige(priv)) {
2429 phydev->supported |= SUPPORTED_1000baseT_Full;
2430 phydev->advertising |= ADVERTISED_1000baseT_Full;
2432 if (priv->chip->phy_rgmii_set)
2433 priv->chip->phy_rgmii_set(priv, phydev);
2436 phydev->priv = priv;
2438 mutex_unlock(&ar8xxx_dev_list_lock);
2442 err_unregister_switch:
2443 if (--priv->use_count)
2446 unregister_switch(&priv->dev);
2451 mutex_unlock(&ar8xxx_dev_list_lock);
2456 ar8xxx_phy_detach(struct phy_device *phydev)
2458 struct net_device *dev = phydev->attached_dev;
2463 dev->phy_ptr = NULL;
2464 dev->priv_flags &= ~IFF_NO_IP_ALIGN;
2465 dev->eth_mangle_rx = NULL;
2466 dev->eth_mangle_tx = NULL;
2470 ar8xxx_phy_remove(struct phy_device *phydev)
2472 struct ar8xxx_priv *priv = phydev->priv;
2477 phydev->priv = NULL;
2479 mutex_lock(&ar8xxx_dev_list_lock);
2481 if (--priv->use_count > 0) {
2482 mutex_unlock(&ar8xxx_dev_list_lock);
2486 list_del(&priv->list);
2487 mutex_unlock(&ar8xxx_dev_list_lock);
2489 unregister_switch(&priv->dev);
2490 ar8xxx_mib_stop(priv);
2495 ar8xxx_phy_soft_reset(struct phy_device *phydev)
2497 /* we don't need an extra reset */
2501 static struct phy_driver ar8xxx_phy_driver[] = {
2503 .phy_id = 0x004d0000,
2504 .name = "Atheros AR8216/AR8236/AR8316",
2505 .phy_id_mask = 0xffff0000,
2506 .features = PHY_BASIC_FEATURES,
2507 .probe = ar8xxx_phy_probe,
2508 .remove = ar8xxx_phy_remove,
2509 .detach = ar8xxx_phy_detach,
2510 .config_init = ar8xxx_phy_config_init,
2511 .config_aneg = ar8xxx_phy_config_aneg,
2512 .read_status = ar8xxx_phy_read_status,
2513 .soft_reset = ar8xxx_phy_soft_reset,
2517 static const struct of_device_id ar8xxx_mdiodev_of_match[] = {
2519 .compatible = "qca,ar8229",
2520 .data = &ar8229_chip,
2522 .compatible = "qca,ar8236",
2523 .data = &ar8236_chip,
2525 .compatible = "qca,ar8327",
2526 .data = &ar8327_chip,
2532 ar8xxx_mdiodev_probe(struct mdio_device *mdiodev)
2534 const struct of_device_id *match;
2535 struct ar8xxx_priv *priv;
2536 struct switch_dev *swdev;
2537 struct device_node *mdio_node;
2540 match = of_match_device(ar8xxx_mdiodev_of_match, &mdiodev->dev);
2544 priv = ar8xxx_create();
2548 priv->mii_bus = mdiodev->bus;
2549 priv->pdev = &mdiodev->dev;
2550 priv->chip = (const struct ar8xxx_chip *) match->data;
2552 ret = ar8xxx_read_id(priv);
2556 ret = ar8xxx_probe_switch(priv);
2560 if (priv->chip->phy_read && priv->chip->phy_write) {
2561 priv->sw_mii_bus = devm_mdiobus_alloc(&mdiodev->dev);
2562 priv->sw_mii_bus->name = "ar8xxx-mdio";
2563 priv->sw_mii_bus->read = ar8xxx_phy_read;
2564 priv->sw_mii_bus->write = ar8xxx_phy_write;
2565 priv->sw_mii_bus->priv = priv;
2566 priv->sw_mii_bus->parent = &mdiodev->dev;
2567 snprintf(priv->sw_mii_bus->id, MII_BUS_ID_SIZE, "%s",
2568 dev_name(&mdiodev->dev));
2569 mdio_node = of_get_child_by_name(priv->pdev->of_node, "mdio-bus");
2570 ret = of_mdiobus_register(priv->sw_mii_bus, mdio_node);
2576 swdev->alias = dev_name(&mdiodev->dev);
2578 if (of_property_read_bool(priv->pdev->of_node, "qca,phy4-mii-enable")) {
2579 priv->port4_phy = true;
2583 ret = register_switch(swdev, NULL);
2587 pr_info("%s: %s rev. %u switch registered on %s\n",
2588 swdev->devname, swdev->name, priv->chip_rev,
2589 dev_name(&priv->mii_bus->dev));
2591 mutex_lock(&ar8xxx_dev_list_lock);
2592 list_add(&priv->list, &ar8xxx_dev_list);
2593 mutex_unlock(&ar8xxx_dev_list_lock);
2597 ret = ar8xxx_start(priv);
2599 goto err_unregister_switch;
2601 dev_set_drvdata(&mdiodev->dev, priv);
2605 err_unregister_switch:
2606 if (--priv->use_count)
2609 unregister_switch(&priv->dev);
2617 ar8xxx_mdiodev_remove(struct mdio_device *mdiodev)
2619 struct ar8xxx_priv *priv = dev_get_drvdata(&mdiodev->dev);
2624 mutex_lock(&ar8xxx_dev_list_lock);
2626 if (--priv->use_count > 0) {
2627 mutex_unlock(&ar8xxx_dev_list_lock);
2631 list_del(&priv->list);
2632 mutex_unlock(&ar8xxx_dev_list_lock);
2634 unregister_switch(&priv->dev);
2635 ar8xxx_mib_stop(priv);
2636 if(priv->sw_mii_bus)
2637 mdiobus_unregister(priv->sw_mii_bus);
2641 static struct mdio_driver ar8xxx_mdio_driver = {
2642 .probe = ar8xxx_mdiodev_probe,
2643 .remove = ar8xxx_mdiodev_remove,
2645 .name = "ar8xxx-switch",
2646 .of_match_table = ar8xxx_mdiodev_of_match,
2650 static int __init ar8216_init(void)
2654 ret = phy_drivers_register(ar8xxx_phy_driver,
2655 ARRAY_SIZE(ar8xxx_phy_driver),
2660 ret = mdio_driver_register(&ar8xxx_mdio_driver);
2662 phy_drivers_unregister(ar8xxx_phy_driver,
2663 ARRAY_SIZE(ar8xxx_phy_driver));
2667 module_init(ar8216_init);
2669 static void __exit ar8216_exit(void)
2671 mdio_driver_unregister(&ar8xxx_mdio_driver);
2672 phy_drivers_unregister(ar8xxx_phy_driver,
2673 ARRAY_SIZE(ar8xxx_phy_driver));
2675 module_exit(ar8216_exit);
2677 MODULE_LICENSE("GPL");