2 * ar8216.c: AR8216 switch driver
4 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
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/bitops.h>
27 #include <net/genetlink.h>
28 #include <linux/switch.h>
29 #include <linux/delay.h>
30 #include <linux/phy.h>
31 #include <linux/netdevice.h>
32 #include <linux/etherdevice.h>
33 #include <linux/lockdep.h>
34 #include <linux/ar8216_platform.h>
35 #include <linux/workqueue.h>
38 /* size of the vlan table */
39 #define AR8X16_MAX_VLANS 128
40 #define AR8X16_PROBE_RETRIES 10
41 #define AR8X16_MAX_PORTS 8
43 #define AR8XXX_MIB_WORK_DELAY 2000 /* msecs */
47 #define AR8XXX_CAP_GIGE BIT(0)
48 #define AR8XXX_CAP_MIB_COUNTERS BIT(1)
51 AR8XXX_VER_AR8216 = 0x01,
52 AR8XXX_VER_AR8236 = 0x03,
53 AR8XXX_VER_AR8316 = 0x10,
54 AR8XXX_VER_AR8327 = 0x12,
57 struct ar8xxx_mib_desc {
66 int (*hw_init)(struct ar8xxx_priv *priv);
67 void (*init_globals)(struct ar8xxx_priv *priv);
68 void (*init_port)(struct ar8xxx_priv *priv, int port);
69 void (*setup_port)(struct ar8xxx_priv *priv, int port, u32 egress,
70 u32 ingress, u32 members, u32 pvid);
71 u32 (*read_port_status)(struct ar8xxx_priv *priv, int port);
72 int (*atu_flush)(struct ar8xxx_priv *priv);
73 void (*vtu_flush)(struct ar8xxx_priv *priv);
74 void (*vtu_load_vlan)(struct ar8xxx_priv *priv, u32 vid, u32 port_mask);
76 const struct ar8xxx_mib_desc *mib_decs;
86 struct switch_dev dev;
87 struct mii_bus *mii_bus;
88 struct phy_device *phy;
89 u32 (*read)(struct ar8xxx_priv *priv, int reg);
90 void (*write)(struct ar8xxx_priv *priv, int reg, u32 val);
91 const struct net_device_ops *ndo_old;
92 struct net_device_ops ndo;
93 struct mutex reg_mutex;
96 const struct ar8xxx_chip *chip;
98 struct ar8327_data ar8327;
107 struct mutex mib_lock;
108 struct delayed_work mib_work;
112 struct list_head list;
113 unsigned int use_count;
115 /* all fields below are cleared on reset */
117 u16 vlan_id[AR8X16_MAX_VLANS];
118 u8 vlan_table[AR8X16_MAX_VLANS];
120 u16 pvid[AR8X16_MAX_PORTS];
123 #define MIB_DESC(_s , _o, _n) \
130 static const struct ar8xxx_mib_desc ar8216_mibs[] = {
131 MIB_DESC(1, AR8216_STATS_RXBROAD, "RxBroad"),
132 MIB_DESC(1, AR8216_STATS_RXPAUSE, "RxPause"),
133 MIB_DESC(1, AR8216_STATS_RXMULTI, "RxMulti"),
134 MIB_DESC(1, AR8216_STATS_RXFCSERR, "RxFcsErr"),
135 MIB_DESC(1, AR8216_STATS_RXALIGNERR, "RxAlignErr"),
136 MIB_DESC(1, AR8216_STATS_RXRUNT, "RxRunt"),
137 MIB_DESC(1, AR8216_STATS_RXFRAGMENT, "RxFragment"),
138 MIB_DESC(1, AR8216_STATS_RX64BYTE, "Rx64Byte"),
139 MIB_DESC(1, AR8216_STATS_RX128BYTE, "Rx128Byte"),
140 MIB_DESC(1, AR8216_STATS_RX256BYTE, "Rx256Byte"),
141 MIB_DESC(1, AR8216_STATS_RX512BYTE, "Rx512Byte"),
142 MIB_DESC(1, AR8216_STATS_RX1024BYTE, "Rx1024Byte"),
143 MIB_DESC(1, AR8216_STATS_RXMAXBYTE, "RxMaxByte"),
144 MIB_DESC(1, AR8216_STATS_RXTOOLONG, "RxTooLong"),
145 MIB_DESC(2, AR8216_STATS_RXGOODBYTE, "RxGoodByte"),
146 MIB_DESC(2, AR8216_STATS_RXBADBYTE, "RxBadByte"),
147 MIB_DESC(1, AR8216_STATS_RXOVERFLOW, "RxOverFlow"),
148 MIB_DESC(1, AR8216_STATS_FILTERED, "Filtered"),
149 MIB_DESC(1, AR8216_STATS_TXBROAD, "TxBroad"),
150 MIB_DESC(1, AR8216_STATS_TXPAUSE, "TxPause"),
151 MIB_DESC(1, AR8216_STATS_TXMULTI, "TxMulti"),
152 MIB_DESC(1, AR8216_STATS_TXUNDERRUN, "TxUnderRun"),
153 MIB_DESC(1, AR8216_STATS_TX64BYTE, "Tx64Byte"),
154 MIB_DESC(1, AR8216_STATS_TX128BYTE, "Tx128Byte"),
155 MIB_DESC(1, AR8216_STATS_TX256BYTE, "Tx256Byte"),
156 MIB_DESC(1, AR8216_STATS_TX512BYTE, "Tx512Byte"),
157 MIB_DESC(1, AR8216_STATS_TX1024BYTE, "Tx1024Byte"),
158 MIB_DESC(1, AR8216_STATS_TXMAXBYTE, "TxMaxByte"),
159 MIB_DESC(1, AR8216_STATS_TXOVERSIZE, "TxOverSize"),
160 MIB_DESC(2, AR8216_STATS_TXBYTE, "TxByte"),
161 MIB_DESC(1, AR8216_STATS_TXCOLLISION, "TxCollision"),
162 MIB_DESC(1, AR8216_STATS_TXABORTCOL, "TxAbortCol"),
163 MIB_DESC(1, AR8216_STATS_TXMULTICOL, "TxMultiCol"),
164 MIB_DESC(1, AR8216_STATS_TXSINGLECOL, "TxSingleCol"),
165 MIB_DESC(1, AR8216_STATS_TXEXCDEFER, "TxExcDefer"),
166 MIB_DESC(1, AR8216_STATS_TXDEFER, "TxDefer"),
167 MIB_DESC(1, AR8216_STATS_TXLATECOL, "TxLateCol"),
170 static const struct ar8xxx_mib_desc ar8236_mibs[] = {
171 MIB_DESC(1, AR8236_STATS_RXBROAD, "RxBroad"),
172 MIB_DESC(1, AR8236_STATS_RXPAUSE, "RxPause"),
173 MIB_DESC(1, AR8236_STATS_RXMULTI, "RxMulti"),
174 MIB_DESC(1, AR8236_STATS_RXFCSERR, "RxFcsErr"),
175 MIB_DESC(1, AR8236_STATS_RXALIGNERR, "RxAlignErr"),
176 MIB_DESC(1, AR8236_STATS_RXRUNT, "RxRunt"),
177 MIB_DESC(1, AR8236_STATS_RXFRAGMENT, "RxFragment"),
178 MIB_DESC(1, AR8236_STATS_RX64BYTE, "Rx64Byte"),
179 MIB_DESC(1, AR8236_STATS_RX128BYTE, "Rx128Byte"),
180 MIB_DESC(1, AR8236_STATS_RX256BYTE, "Rx256Byte"),
181 MIB_DESC(1, AR8236_STATS_RX512BYTE, "Rx512Byte"),
182 MIB_DESC(1, AR8236_STATS_RX1024BYTE, "Rx1024Byte"),
183 MIB_DESC(1, AR8236_STATS_RX1518BYTE, "Rx1518Byte"),
184 MIB_DESC(1, AR8236_STATS_RXMAXBYTE, "RxMaxByte"),
185 MIB_DESC(1, AR8236_STATS_RXTOOLONG, "RxTooLong"),
186 MIB_DESC(2, AR8236_STATS_RXGOODBYTE, "RxGoodByte"),
187 MIB_DESC(2, AR8236_STATS_RXBADBYTE, "RxBadByte"),
188 MIB_DESC(1, AR8236_STATS_RXOVERFLOW, "RxOverFlow"),
189 MIB_DESC(1, AR8236_STATS_FILTERED, "Filtered"),
190 MIB_DESC(1, AR8236_STATS_TXBROAD, "TxBroad"),
191 MIB_DESC(1, AR8236_STATS_TXPAUSE, "TxPause"),
192 MIB_DESC(1, AR8236_STATS_TXMULTI, "TxMulti"),
193 MIB_DESC(1, AR8236_STATS_TXUNDERRUN, "TxUnderRun"),
194 MIB_DESC(1, AR8236_STATS_TX64BYTE, "Tx64Byte"),
195 MIB_DESC(1, AR8236_STATS_TX128BYTE, "Tx128Byte"),
196 MIB_DESC(1, AR8236_STATS_TX256BYTE, "Tx256Byte"),
197 MIB_DESC(1, AR8236_STATS_TX512BYTE, "Tx512Byte"),
198 MIB_DESC(1, AR8236_STATS_TX1024BYTE, "Tx1024Byte"),
199 MIB_DESC(1, AR8236_STATS_TX1518BYTE, "Tx1518Byte"),
200 MIB_DESC(1, AR8236_STATS_TXMAXBYTE, "TxMaxByte"),
201 MIB_DESC(1, AR8236_STATS_TXOVERSIZE, "TxOverSize"),
202 MIB_DESC(2, AR8236_STATS_TXBYTE, "TxByte"),
203 MIB_DESC(1, AR8236_STATS_TXCOLLISION, "TxCollision"),
204 MIB_DESC(1, AR8236_STATS_TXABORTCOL, "TxAbortCol"),
205 MIB_DESC(1, AR8236_STATS_TXMULTICOL, "TxMultiCol"),
206 MIB_DESC(1, AR8236_STATS_TXSINGLECOL, "TxSingleCol"),
207 MIB_DESC(1, AR8236_STATS_TXEXCDEFER, "TxExcDefer"),
208 MIB_DESC(1, AR8236_STATS_TXDEFER, "TxDefer"),
209 MIB_DESC(1, AR8236_STATS_TXLATECOL, "TxLateCol"),
212 static DEFINE_MUTEX(ar8xxx_dev_list_lock);
213 static LIST_HEAD(ar8xxx_dev_list);
215 static inline struct ar8xxx_priv *
216 swdev_to_ar8xxx(struct switch_dev *swdev)
218 return container_of(swdev, struct ar8xxx_priv, dev);
221 static inline bool ar8xxx_has_gige(struct ar8xxx_priv *priv)
223 return priv->chip->caps & AR8XXX_CAP_GIGE;
226 static inline bool ar8xxx_has_mib_counters(struct ar8xxx_priv *priv)
228 return priv->chip->caps & AR8XXX_CAP_MIB_COUNTERS;
231 static inline bool chip_is_ar8216(struct ar8xxx_priv *priv)
233 return priv->chip_ver == AR8XXX_VER_AR8216;
236 static inline bool chip_is_ar8236(struct ar8xxx_priv *priv)
238 return priv->chip_ver == AR8XXX_VER_AR8236;
241 static inline bool chip_is_ar8316(struct ar8xxx_priv *priv)
243 return priv->chip_ver == AR8XXX_VER_AR8316;
246 static inline bool chip_is_ar8327(struct ar8xxx_priv *priv)
248 return priv->chip_ver == AR8XXX_VER_AR8327;
252 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
255 *r1 = regaddr & 0x1e;
261 *page = regaddr & 0x1ff;
265 ar8xxx_mii_read(struct ar8xxx_priv *priv, int reg)
267 struct mii_bus *bus = priv->mii_bus;
271 split_addr((u32) reg, &r1, &r2, &page);
273 mutex_lock(&bus->mdio_lock);
275 bus->write(bus, 0x18, 0, page);
276 usleep_range(1000, 2000); /* wait for the page switch to propagate */
277 lo = bus->read(bus, 0x10 | r2, r1);
278 hi = bus->read(bus, 0x10 | r2, r1 + 1);
280 mutex_unlock(&bus->mdio_lock);
282 return (hi << 16) | lo;
286 ar8xxx_mii_write(struct ar8xxx_priv *priv, int reg, u32 val)
288 struct mii_bus *bus = priv->mii_bus;
292 split_addr((u32) reg, &r1, &r2, &r3);
294 hi = (u16) (val >> 16);
296 mutex_lock(&bus->mdio_lock);
298 bus->write(bus, 0x18, 0, r3);
299 usleep_range(1000, 2000); /* wait for the page switch to propagate */
300 if (priv->mii_lo_first) {
301 bus->write(bus, 0x10 | r2, r1, lo);
302 bus->write(bus, 0x10 | r2, r1 + 1, hi);
304 bus->write(bus, 0x10 | r2, r1 + 1, hi);
305 bus->write(bus, 0x10 | r2, r1, lo);
308 mutex_unlock(&bus->mdio_lock);
312 ar8xxx_phy_dbg_write(struct ar8xxx_priv *priv, int phy_addr,
313 u16 dbg_addr, u16 dbg_data)
315 struct mii_bus *bus = priv->mii_bus;
317 mutex_lock(&bus->mdio_lock);
318 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
319 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
320 mutex_unlock(&bus->mdio_lock);
324 ar8xxx_phy_mmd_write(struct ar8xxx_priv *priv, int phy_addr, u16 addr, u16 data)
326 struct mii_bus *bus = priv->mii_bus;
328 mutex_lock(&bus->mdio_lock);
329 bus->write(bus, phy_addr, MII_ATH_MMD_ADDR, addr);
330 bus->write(bus, phy_addr, MII_ATH_MMD_DATA, data);
331 mutex_unlock(&bus->mdio_lock);
335 ar8xxx_rmw(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
339 lockdep_assert_held(&priv->reg_mutex);
341 v = priv->read(priv, reg);
344 priv->write(priv, reg, v);
350 ar8xxx_reg_set(struct ar8xxx_priv *priv, int reg, u32 val)
354 lockdep_assert_held(&priv->reg_mutex);
356 v = priv->read(priv, reg);
358 priv->write(priv, reg, v);
362 ar8xxx_reg_wait(struct ar8xxx_priv *priv, u32 reg, u32 mask, u32 val,
367 for (i = 0; i < timeout; i++) {
370 t = priv->read(priv, reg);
371 if ((t & mask) == val)
374 usleep_range(1000, 2000);
381 ar8xxx_mib_op(struct ar8xxx_priv *priv, u32 op)
386 lockdep_assert_held(&priv->mib_lock);
388 if (chip_is_ar8327(priv))
389 mib_func = AR8327_REG_MIB_FUNC;
391 mib_func = AR8216_REG_MIB_FUNC;
393 mutex_lock(&priv->reg_mutex);
394 /* Capture the hardware statistics for all ports */
395 ar8xxx_rmw(priv, mib_func, AR8216_MIB_FUNC, (op << AR8216_MIB_FUNC_S));
396 mutex_unlock(&priv->reg_mutex);
398 /* Wait for the capturing to complete. */
399 ret = ar8xxx_reg_wait(priv, mib_func, AR8216_MIB_BUSY, 0, 10);
410 ar8xxx_mib_capture(struct ar8xxx_priv *priv)
412 return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_CAPTURE);
416 ar8xxx_mib_flush(struct ar8xxx_priv *priv)
418 return ar8xxx_mib_op(priv, AR8216_MIB_FUNC_FLUSH);
422 ar8xxx_mib_fetch_port_stat(struct ar8xxx_priv *priv, int port, bool flush)
428 WARN_ON(port >= priv->dev.ports);
430 lockdep_assert_held(&priv->mib_lock);
432 if (chip_is_ar8327(priv))
433 base = AR8327_REG_PORT_STATS_BASE(port);
434 else if (chip_is_ar8236(priv) ||
435 chip_is_ar8316(priv))
436 base = AR8236_REG_PORT_STATS_BASE(port);
438 base = AR8216_REG_PORT_STATS_BASE(port);
440 mib_stats = &priv->mib_stats[port * priv->chip->num_mibs];
441 for (i = 0; i < priv->chip->num_mibs; i++) {
442 const struct ar8xxx_mib_desc *mib;
445 mib = &priv->chip->mib_decs[i];
446 t = priv->read(priv, base + mib->offset);
447 if (mib->size == 2) {
450 hi = priv->read(priv, base + mib->offset + 4);
462 ar8216_read_port_link(struct ar8xxx_priv *priv, int port,
463 struct switch_port_link *link)
468 memset(link, '\0', sizeof(*link));
470 status = priv->chip->read_port_status(priv, port);
472 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
474 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
481 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
482 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
483 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
485 speed = (status & AR8216_PORT_STATUS_SPEED) >>
486 AR8216_PORT_STATUS_SPEED_S;
489 case AR8216_PORT_SPEED_10M:
490 link->speed = SWITCH_PORT_SPEED_10;
492 case AR8216_PORT_SPEED_100M:
493 link->speed = SWITCH_PORT_SPEED_100;
495 case AR8216_PORT_SPEED_1000M:
496 link->speed = SWITCH_PORT_SPEED_1000;
499 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
504 static struct sk_buff *
505 ar8216_mangle_tx(struct net_device *dev, struct sk_buff *skb)
507 struct ar8xxx_priv *priv = dev->phy_ptr;
516 if (unlikely(skb_headroom(skb) < 2)) {
517 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
521 buf = skb_push(skb, 2);
529 dev_kfree_skb_any(skb);
534 ar8216_mangle_rx(struct net_device *dev, struct sk_buff *skb)
536 struct ar8xxx_priv *priv;
544 /* don't strip the header if vlan mode is disabled */
548 /* strip header, get vlan id */
552 /* check for vlan header presence */
553 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
558 /* no need to fix up packets coming from a tagged source */
559 if (priv->vlan_tagged & (1 << port))
562 /* lookup port vid from local table, the switch passes an invalid vlan id */
563 vlan = priv->vlan_id[priv->pvid[port]];
566 buf[14 + 2] |= vlan >> 8;
567 buf[15 + 2] = vlan & 0xff;
571 ar8216_wait_bit(struct ar8xxx_priv *priv, int reg, u32 mask, u32 val)
577 t = priv->read(priv, reg);
578 if ((t & mask) == val)
587 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
588 (unsigned int) reg, t, mask, val);
593 ar8216_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
595 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
597 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
598 val &= AR8216_VTUDATA_MEMBER;
599 val |= AR8216_VTUDATA_VALID;
600 priv->write(priv, AR8216_REG_VTU_DATA, val);
602 op |= AR8216_VTU_ACTIVE;
603 priv->write(priv, AR8216_REG_VTU, op);
607 ar8216_vtu_flush(struct ar8xxx_priv *priv)
609 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
613 ar8216_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
617 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
618 ar8216_vtu_op(priv, op, port_mask);
622 ar8216_atu_flush(struct ar8xxx_priv *priv)
626 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
628 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
634 ar8216_read_port_status(struct ar8xxx_priv *priv, int port)
636 return priv->read(priv, AR8216_REG_PORT_STATUS(port));
640 ar8216_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
641 u32 members, u32 pvid)
645 if (chip_is_ar8216(priv) && priv->vlan && port == AR8216_PORT_CPU)
646 header = AR8216_PORT_CTRL_HEADER;
650 ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
651 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
652 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
653 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
654 AR8216_PORT_CTRL_LEARN | header |
655 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
656 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
658 ar8xxx_rmw(priv, AR8216_REG_PORT_VLAN(port),
659 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
660 AR8216_PORT_VLAN_DEFAULT_ID,
661 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
662 (ingress << AR8216_PORT_VLAN_MODE_S) |
663 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
667 ar8216_hw_init(struct ar8xxx_priv *priv)
673 ar8216_init_globals(struct ar8xxx_priv *priv)
675 /* standard atheros magic */
676 priv->write(priv, 0x38, 0xc000050e);
678 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
679 AR8216_GCTRL_MTU, 1518 + 8 + 2);
683 ar8216_init_port(struct ar8xxx_priv *priv, int port)
685 /* Enable port learning and tx */
686 priv->write(priv, AR8216_REG_PORT_CTRL(port),
687 AR8216_PORT_CTRL_LEARN |
688 (4 << AR8216_PORT_CTRL_STATE_S));
690 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
692 if (port == AR8216_PORT_CPU) {
693 priv->write(priv, AR8216_REG_PORT_STATUS(port),
694 AR8216_PORT_STATUS_LINK_UP |
695 (ar8xxx_has_gige(priv) ?
696 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
697 AR8216_PORT_STATUS_TXMAC |
698 AR8216_PORT_STATUS_RXMAC |
699 (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_RXFLOW : 0) |
700 (chip_is_ar8316(priv) ? AR8216_PORT_STATUS_TXFLOW : 0) |
701 AR8216_PORT_STATUS_DUPLEX);
703 priv->write(priv, AR8216_REG_PORT_STATUS(port),
704 AR8216_PORT_STATUS_LINK_AUTO);
708 static const struct ar8xxx_chip ar8216_chip = {
709 .caps = AR8XXX_CAP_MIB_COUNTERS,
711 .hw_init = ar8216_hw_init,
712 .init_globals = ar8216_init_globals,
713 .init_port = ar8216_init_port,
714 .setup_port = ar8216_setup_port,
715 .read_port_status = ar8216_read_port_status,
716 .atu_flush = ar8216_atu_flush,
717 .vtu_flush = ar8216_vtu_flush,
718 .vtu_load_vlan = ar8216_vtu_load_vlan,
720 .num_mibs = ARRAY_SIZE(ar8216_mibs),
721 .mib_decs = ar8216_mibs,
725 ar8236_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
726 u32 members, u32 pvid)
728 ar8xxx_rmw(priv, AR8216_REG_PORT_CTRL(port),
729 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
730 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
731 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
732 AR8216_PORT_CTRL_LEARN |
733 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
734 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
736 ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN(port),
737 AR8236_PORT_VLAN_DEFAULT_ID,
738 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
740 ar8xxx_rmw(priv, AR8236_REG_PORT_VLAN2(port),
741 AR8236_PORT_VLAN2_VLAN_MODE |
742 AR8236_PORT_VLAN2_MEMBER,
743 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
744 (members << AR8236_PORT_VLAN2_MEMBER_S));
748 ar8236_hw_init(struct ar8xxx_priv *priv)
753 if (priv->initialized)
756 /* Initialize the PHYs */
758 for (i = 0; i < 5; i++) {
759 mdiobus_write(bus, i, MII_ADVERTISE,
760 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
761 ADVERTISE_PAUSE_ASYM);
762 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
766 priv->initialized = true;
771 ar8236_init_globals(struct ar8xxx_priv *priv)
773 /* enable jumbo frames */
774 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
775 AR8316_GCTRL_MTU, 9018 + 8 + 2);
777 /* Enable MIB counters */
778 ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
779 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
783 static const struct ar8xxx_chip ar8236_chip = {
784 .caps = AR8XXX_CAP_MIB_COUNTERS,
785 .hw_init = ar8236_hw_init,
786 .init_globals = ar8236_init_globals,
787 .init_port = ar8216_init_port,
788 .setup_port = ar8236_setup_port,
789 .read_port_status = ar8216_read_port_status,
790 .atu_flush = ar8216_atu_flush,
791 .vtu_flush = ar8216_vtu_flush,
792 .vtu_load_vlan = ar8216_vtu_load_vlan,
794 .num_mibs = ARRAY_SIZE(ar8236_mibs),
795 .mib_decs = ar8236_mibs,
799 ar8316_hw_init(struct ar8xxx_priv *priv)
805 val = priv->read(priv, AR8316_REG_POSTRIP);
807 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
808 if (priv->port4_phy) {
809 /* value taken from Ubiquiti RouterStation Pro */
811 pr_info("ar8316: Using port 4 as PHY\n");
814 pr_info("ar8316: Using port 4 as switch port\n");
816 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
817 /* value taken from AVM Fritz!Box 7390 sources */
820 /* no known value for phy interface */
821 pr_err("ar8316: unsupported mii mode: %d.\n",
822 priv->phy->interface);
829 priv->write(priv, AR8316_REG_POSTRIP, newval);
831 if (priv->port4_phy &&
832 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
833 /* work around for phy4 rgmii mode */
834 ar8xxx_phy_dbg_write(priv, 4, 0x12, 0x480c);
836 ar8xxx_phy_dbg_write(priv, 4, 0x0, 0x824e);
838 ar8xxx_phy_dbg_write(priv, 4, 0x5, 0x3d47);
842 /* Initialize the ports */
844 for (i = 0; i < 5; i++) {
845 /* initialize the port itself */
846 mdiobus_write(bus, i, MII_ADVERTISE,
847 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
848 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
849 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
855 priv->initialized = true;
860 ar8316_init_globals(struct ar8xxx_priv *priv)
862 /* standard atheros magic */
863 priv->write(priv, 0x38, 0xc000050e);
865 /* enable cpu port to receive multicast and broadcast frames */
866 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
868 /* enable jumbo frames */
869 ar8xxx_rmw(priv, AR8216_REG_GLOBAL_CTRL,
870 AR8316_GCTRL_MTU, 9018 + 8 + 2);
872 /* Enable MIB counters */
873 ar8xxx_rmw(priv, AR8216_REG_MIB_FUNC, AR8216_MIB_FUNC | AR8236_MIB_EN,
874 (AR8216_MIB_FUNC_NO_OP << AR8216_MIB_FUNC_S) |
878 static const struct ar8xxx_chip ar8316_chip = {
879 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
880 .hw_init = ar8316_hw_init,
881 .init_globals = ar8316_init_globals,
882 .init_port = ar8216_init_port,
883 .setup_port = ar8216_setup_port,
884 .read_port_status = ar8216_read_port_status,
885 .atu_flush = ar8216_atu_flush,
886 .vtu_flush = ar8216_vtu_flush,
887 .vtu_load_vlan = ar8216_vtu_load_vlan,
889 .num_mibs = ARRAY_SIZE(ar8236_mibs),
890 .mib_decs = ar8236_mibs,
894 ar8327_get_pad_cfg(struct ar8327_pad_cfg *cfg)
906 case AR8327_PAD_MAC2MAC_MII:
907 t = AR8327_PAD_MAC_MII_EN;
909 t |= AR8327_PAD_MAC_MII_RXCLK_SEL;
911 t |= AR8327_PAD_MAC_MII_TXCLK_SEL;
914 case AR8327_PAD_MAC2MAC_GMII:
915 t = AR8327_PAD_MAC_GMII_EN;
917 t |= AR8327_PAD_MAC_GMII_RXCLK_SEL;
919 t |= AR8327_PAD_MAC_GMII_TXCLK_SEL;
922 case AR8327_PAD_MAC_SGMII:
923 t = AR8327_PAD_SGMII_EN;
926 * WAR for the QUalcomm Atheros AP136 board.
927 * It seems that RGMII TX/RX delay settings needs to be
928 * applied for SGMII mode as well, The ethernet is not
929 * reliable without this.
931 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
932 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
933 if (cfg->rxclk_delay_en)
934 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
935 if (cfg->txclk_delay_en)
936 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
938 if (cfg->sgmii_delay_en)
939 t |= AR8327_PAD_SGMII_DELAY_EN;
943 case AR8327_PAD_MAC2PHY_MII:
944 t = AR8327_PAD_PHY_MII_EN;
946 t |= AR8327_PAD_PHY_MII_RXCLK_SEL;
948 t |= AR8327_PAD_PHY_MII_TXCLK_SEL;
951 case AR8327_PAD_MAC2PHY_GMII:
952 t = AR8327_PAD_PHY_GMII_EN;
953 if (cfg->pipe_rxclk_sel)
954 t |= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL;
956 t |= AR8327_PAD_PHY_GMII_RXCLK_SEL;
958 t |= AR8327_PAD_PHY_GMII_TXCLK_SEL;
961 case AR8327_PAD_MAC_RGMII:
962 t = AR8327_PAD_RGMII_EN;
963 t |= cfg->txclk_delay_sel << AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S;
964 t |= cfg->rxclk_delay_sel << AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S;
965 if (cfg->rxclk_delay_en)
966 t |= AR8327_PAD_RGMII_RXCLK_DELAY_EN;
967 if (cfg->txclk_delay_en)
968 t |= AR8327_PAD_RGMII_TXCLK_DELAY_EN;
971 case AR8327_PAD_PHY_GMII:
972 t = AR8327_PAD_PHYX_GMII_EN;
975 case AR8327_PAD_PHY_RGMII:
976 t = AR8327_PAD_PHYX_RGMII_EN;
979 case AR8327_PAD_PHY_MII:
980 t = AR8327_PAD_PHYX_MII_EN;
988 ar8327_phy_fixup(struct ar8xxx_priv *priv, int phy)
990 switch (priv->chip_rev) {
992 /* For 100M waveform */
993 ar8xxx_phy_dbg_write(priv, phy, 0, 0x02ea);
994 /* Turn on Gigabit clock */
995 ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x68a0);
999 ar8xxx_phy_mmd_write(priv, phy, 0x7, 0x3c);
1000 ar8xxx_phy_mmd_write(priv, phy, 0x4007, 0x0);
1003 ar8xxx_phy_mmd_write(priv, phy, 0x3, 0x800d);
1004 ar8xxx_phy_mmd_write(priv, phy, 0x4003, 0x803f);
1006 ar8xxx_phy_dbg_write(priv, phy, 0x3d, 0x6860);
1007 ar8xxx_phy_dbg_write(priv, phy, 0x5, 0x2c46);
1008 ar8xxx_phy_dbg_write(priv, phy, 0x3c, 0x6000);
1014 ar8327_get_port_init_status(struct ar8327_port_cfg *cfg)
1018 if (!cfg->force_link)
1019 return AR8216_PORT_STATUS_LINK_AUTO;
1021 t = AR8216_PORT_STATUS_TXMAC | AR8216_PORT_STATUS_RXMAC;
1022 t |= cfg->duplex ? AR8216_PORT_STATUS_DUPLEX : 0;
1023 t |= cfg->rxpause ? AR8216_PORT_STATUS_RXFLOW : 0;
1024 t |= cfg->txpause ? AR8216_PORT_STATUS_TXFLOW : 0;
1026 switch (cfg->speed) {
1027 case AR8327_PORT_SPEED_10:
1028 t |= AR8216_PORT_SPEED_10M;
1030 case AR8327_PORT_SPEED_100:
1031 t |= AR8216_PORT_SPEED_100M;
1033 case AR8327_PORT_SPEED_1000:
1034 t |= AR8216_PORT_SPEED_1000M;
1042 ar8327_hw_config_pdata(struct ar8xxx_priv *priv,
1043 struct ar8327_platform_data *pdata)
1045 struct ar8327_led_cfg *led_cfg;
1046 struct ar8327_data *data;
1053 data = &priv->chip_data.ar8327;
1055 data->port0_status = ar8327_get_port_init_status(&pdata->port0_cfg);
1056 data->port6_status = ar8327_get_port_init_status(&pdata->port6_cfg);
1058 t = ar8327_get_pad_cfg(pdata->pad0_cfg);
1059 priv->write(priv, AR8327_REG_PAD0_MODE, t);
1060 t = ar8327_get_pad_cfg(pdata->pad5_cfg);
1061 priv->write(priv, AR8327_REG_PAD5_MODE, t);
1062 t = ar8327_get_pad_cfg(pdata->pad6_cfg);
1063 priv->write(priv, AR8327_REG_PAD6_MODE, t);
1065 pos = priv->read(priv, AR8327_REG_POWER_ON_STRIP);
1068 led_cfg = pdata->led_cfg;
1070 if (led_cfg->open_drain)
1071 new_pos |= AR8327_POWER_ON_STRIP_LED_OPEN_EN;
1073 new_pos &= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN;
1075 priv->write(priv, AR8327_REG_LED_CTRL0, led_cfg->led_ctrl0);
1076 priv->write(priv, AR8327_REG_LED_CTRL1, led_cfg->led_ctrl1);
1077 priv->write(priv, AR8327_REG_LED_CTRL2, led_cfg->led_ctrl2);
1078 priv->write(priv, AR8327_REG_LED_CTRL3, led_cfg->led_ctrl3);
1081 if (new_pos != pos) {
1082 new_pos |= AR8327_POWER_ON_STRIP_POWER_ON_SEL;
1083 priv->write(priv, AR8327_REG_POWER_ON_STRIP, new_pos);
1090 ar8327_hw_init(struct ar8xxx_priv *priv)
1092 struct mii_bus *bus;
1096 ret = ar8327_hw_config_pdata(priv, priv->phy->dev.platform_data);
1100 bus = priv->mii_bus;
1101 for (i = 0; i < AR8327_NUM_PHYS; i++) {
1102 ar8327_phy_fixup(priv, i);
1104 /* start aneg on the PHY */
1105 mdiobus_write(bus, i, MII_ADVERTISE, ADVERTISE_ALL |
1106 ADVERTISE_PAUSE_CAP |
1107 ADVERTISE_PAUSE_ASYM);
1108 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
1109 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
1118 ar8327_init_globals(struct ar8xxx_priv *priv)
1122 /* enable CPU port and disable mirror port */
1123 t = AR8327_FWD_CTRL0_CPU_PORT_EN |
1124 AR8327_FWD_CTRL0_MIRROR_PORT;
1125 priv->write(priv, AR8327_REG_FWD_CTRL0, t);
1127 /* forward multicast and broadcast frames to CPU */
1128 t = (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_UC_FLOOD_S) |
1129 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_MC_FLOOD_S) |
1130 (AR8327_PORTS_ALL << AR8327_FWD_CTRL1_BC_FLOOD_S);
1131 priv->write(priv, AR8327_REG_FWD_CTRL1, t);
1134 ar8xxx_rmw(priv, AR8327_REG_MAX_FRAME_SIZE,
1135 AR8327_MAX_FRAME_SIZE_MTU, 1518 + 8 + 2);
1137 /* Enable MIB counters */
1138 ar8xxx_reg_set(priv, AR8327_REG_MODULE_EN,
1139 AR8327_MODULE_EN_MIB);
1143 ar8327_init_port(struct ar8xxx_priv *priv, int port)
1147 if (port == AR8216_PORT_CPU)
1148 t = priv->chip_data.ar8327.port0_status;
1150 t = priv->chip_data.ar8327.port6_status;
1152 t = AR8216_PORT_STATUS_LINK_AUTO;
1154 priv->write(priv, AR8327_REG_PORT_STATUS(port), t);
1155 priv->write(priv, AR8327_REG_PORT_HEADER(port), 0);
1157 t = 1 << AR8327_PORT_VLAN0_DEF_SVID_S;
1158 t |= 1 << AR8327_PORT_VLAN0_DEF_CVID_S;
1159 priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
1161 t = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH << AR8327_PORT_VLAN1_OUT_MODE_S;
1162 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
1164 t = AR8327_PORT_LOOKUP_LEARN;
1165 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
1166 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
1170 ar8327_read_port_status(struct ar8xxx_priv *priv, int port)
1172 return priv->read(priv, AR8327_REG_PORT_STATUS(port));
1176 ar8327_atu_flush(struct ar8xxx_priv *priv)
1180 ret = ar8216_wait_bit(priv, AR8327_REG_ATU_FUNC,
1181 AR8327_ATU_FUNC_BUSY, 0);
1183 priv->write(priv, AR8327_REG_ATU_FUNC,
1184 AR8327_ATU_FUNC_OP_FLUSH);
1190 ar8327_vtu_op(struct ar8xxx_priv *priv, u32 op, u32 val)
1192 if (ar8216_wait_bit(priv, AR8327_REG_VTU_FUNC1,
1193 AR8327_VTU_FUNC1_BUSY, 0))
1196 if ((op & AR8327_VTU_FUNC1_OP) == AR8327_VTU_FUNC1_OP_LOAD)
1197 priv->write(priv, AR8327_REG_VTU_FUNC0, val);
1199 op |= AR8327_VTU_FUNC1_BUSY;
1200 priv->write(priv, AR8327_REG_VTU_FUNC1, op);
1204 ar8327_vtu_flush(struct ar8xxx_priv *priv)
1206 ar8327_vtu_op(priv, AR8327_VTU_FUNC1_OP_FLUSH, 0);
1210 ar8327_vtu_load_vlan(struct ar8xxx_priv *priv, u32 vid, u32 port_mask)
1216 op = AR8327_VTU_FUNC1_OP_LOAD | (vid << AR8327_VTU_FUNC1_VID_S);
1217 val = AR8327_VTU_FUNC0_VALID | AR8327_VTU_FUNC0_IVL;
1218 for (i = 0; i < AR8327_NUM_PORTS; i++) {
1221 if ((port_mask & BIT(i)) == 0)
1222 mode = AR8327_VTU_FUNC0_EG_MODE_NOT;
1223 else if (priv->vlan == 0)
1224 mode = AR8327_VTU_FUNC0_EG_MODE_KEEP;
1225 else if (priv->vlan_tagged & BIT(i))
1226 mode = AR8327_VTU_FUNC0_EG_MODE_TAG;
1228 mode = AR8327_VTU_FUNC0_EG_MODE_UNTAG;
1230 val |= mode << AR8327_VTU_FUNC0_EG_MODE_S(i);
1232 ar8327_vtu_op(priv, op, val);
1236 ar8327_setup_port(struct ar8xxx_priv *priv, int port, u32 egress, u32 ingress,
1237 u32 members, u32 pvid)
1242 t = pvid << AR8327_PORT_VLAN0_DEF_SVID_S;
1243 t |= pvid << AR8327_PORT_VLAN0_DEF_CVID_S;
1244 priv->write(priv, AR8327_REG_PORT_VLAN0(port), t);
1246 mode = AR8327_PORT_VLAN1_OUT_MODE_UNMOD;
1248 case AR8216_OUT_KEEP:
1249 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH;
1251 case AR8216_OUT_STRIP_VLAN:
1252 mode = AR8327_PORT_VLAN1_OUT_MODE_UNTAG;
1254 case AR8216_OUT_ADD_VLAN:
1255 mode = AR8327_PORT_VLAN1_OUT_MODE_TAG;
1259 t = AR8327_PORT_VLAN1_PORT_VLAN_PROP;
1260 t |= mode << AR8327_PORT_VLAN1_OUT_MODE_S;
1261 priv->write(priv, AR8327_REG_PORT_VLAN1(port), t);
1264 t |= AR8327_PORT_LOOKUP_LEARN;
1265 t |= ingress << AR8327_PORT_LOOKUP_IN_MODE_S;
1266 t |= AR8216_PORT_STATE_FORWARD << AR8327_PORT_LOOKUP_STATE_S;
1267 priv->write(priv, AR8327_REG_PORT_LOOKUP(port), t);
1270 static const struct ar8xxx_chip ar8327_chip = {
1271 .caps = AR8XXX_CAP_GIGE | AR8XXX_CAP_MIB_COUNTERS,
1272 .hw_init = ar8327_hw_init,
1273 .init_globals = ar8327_init_globals,
1274 .init_port = ar8327_init_port,
1275 .setup_port = ar8327_setup_port,
1276 .read_port_status = ar8327_read_port_status,
1277 .atu_flush = ar8327_atu_flush,
1278 .vtu_flush = ar8327_vtu_flush,
1279 .vtu_load_vlan = ar8327_vtu_load_vlan,
1281 .num_mibs = ARRAY_SIZE(ar8236_mibs),
1282 .mib_decs = ar8236_mibs,
1286 ar8xxx_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1287 struct switch_val *val)
1289 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1290 priv->vlan = !!val->value.i;
1295 ar8xxx_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
1296 struct switch_val *val)
1298 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1299 val->value.i = priv->vlan;
1305 ar8xxx_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
1307 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1309 /* make sure no invalid PVIDs get set */
1311 if (vlan >= dev->vlans)
1314 priv->pvid[port] = vlan;
1319 ar8xxx_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
1321 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1322 *vlan = priv->pvid[port];
1327 ar8xxx_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
1328 struct switch_val *val)
1330 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1331 priv->vlan_id[val->port_vlan] = val->value.i;
1336 ar8xxx_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
1337 struct switch_val *val)
1339 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1340 val->value.i = priv->vlan_id[val->port_vlan];
1345 ar8xxx_sw_get_port_link(struct switch_dev *dev, int port,
1346 struct switch_port_link *link)
1348 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1350 ar8216_read_port_link(priv, port, link);
1355 ar8xxx_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
1357 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1358 u8 ports = priv->vlan_table[val->port_vlan];
1362 for (i = 0; i < dev->ports; i++) {
1363 struct switch_port *p;
1365 if (!(ports & (1 << i)))
1368 p = &val->value.ports[val->len++];
1370 if (priv->vlan_tagged & (1 << i))
1371 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
1379 ar8xxx_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
1381 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1382 u8 *vt = &priv->vlan_table[val->port_vlan];
1386 for (i = 0; i < val->len; i++) {
1387 struct switch_port *p = &val->value.ports[i];
1389 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
1390 priv->vlan_tagged |= (1 << p->id);
1392 priv->vlan_tagged &= ~(1 << p->id);
1393 priv->pvid[p->id] = val->port_vlan;
1395 /* make sure that an untagged port does not
1396 * appear in other vlans */
1397 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1398 if (j == val->port_vlan)
1400 priv->vlan_table[j] &= ~(1 << p->id);
1410 ar8xxx_sw_hw_apply(struct switch_dev *dev)
1412 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1413 u8 portmask[AR8X16_MAX_PORTS];
1416 mutex_lock(&priv->reg_mutex);
1417 /* flush all vlan translation unit entries */
1418 priv->chip->vtu_flush(priv);
1420 memset(portmask, 0, sizeof(portmask));
1422 /* calculate the port destination masks and load vlans
1423 * into the vlan translation unit */
1424 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
1425 u8 vp = priv->vlan_table[j];
1430 for (i = 0; i < dev->ports; i++) {
1433 portmask[i] |= vp & ~mask;
1436 priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
1437 priv->vlan_table[j]);
1441 * isolate all ports, but connect them to the cpu port */
1442 for (i = 0; i < dev->ports; i++) {
1443 if (i == AR8216_PORT_CPU)
1446 portmask[i] = 1 << AR8216_PORT_CPU;
1447 portmask[AR8216_PORT_CPU] |= (1 << i);
1451 /* update the port destination mask registers and tag settings */
1452 for (i = 0; i < dev->ports; i++) {
1453 int egress, ingress;
1457 pvid = priv->vlan_id[priv->pvid[i]];
1458 if (priv->vlan_tagged & (1 << i))
1459 egress = AR8216_OUT_ADD_VLAN;
1461 egress = AR8216_OUT_STRIP_VLAN;
1462 ingress = AR8216_IN_SECURE;
1465 egress = AR8216_OUT_KEEP;
1466 ingress = AR8216_IN_PORT_ONLY;
1469 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
1472 mutex_unlock(&priv->reg_mutex);
1477 ar8xxx_sw_reset_switch(struct switch_dev *dev)
1479 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1482 mutex_lock(&priv->reg_mutex);
1483 memset(&priv->vlan, 0, sizeof(struct ar8xxx_priv) -
1484 offsetof(struct ar8xxx_priv, vlan));
1486 for (i = 0; i < AR8X16_MAX_VLANS; i++)
1487 priv->vlan_id[i] = i;
1489 /* Configure all ports */
1490 for (i = 0; i < dev->ports; i++)
1491 priv->chip->init_port(priv, i);
1493 priv->chip->init_globals(priv);
1494 mutex_unlock(&priv->reg_mutex);
1496 return ar8xxx_sw_hw_apply(dev);
1500 ar8xxx_sw_set_reset_mibs(struct switch_dev *dev,
1501 const struct switch_attr *attr,
1502 struct switch_val *val)
1504 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1508 if (!ar8xxx_has_mib_counters(priv))
1511 mutex_lock(&priv->mib_lock);
1513 len = priv->dev.ports * priv->chip->num_mibs *
1514 sizeof(*priv->mib_stats);
1515 memset(priv->mib_stats, '\0', len);
1516 ret = ar8xxx_mib_flush(priv);
1523 mutex_unlock(&priv->mib_lock);
1528 ar8xxx_sw_set_port_reset_mib(struct switch_dev *dev,
1529 const struct switch_attr *attr,
1530 struct switch_val *val)
1532 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1536 if (!ar8xxx_has_mib_counters(priv))
1539 port = val->port_vlan;
1540 if (port >= dev->ports)
1543 mutex_lock(&priv->mib_lock);
1544 ret = ar8xxx_mib_capture(priv);
1548 ar8xxx_mib_fetch_port_stat(priv, port, true);
1553 mutex_unlock(&priv->mib_lock);
1558 ar8xxx_sw_get_port_mib(struct switch_dev *dev,
1559 const struct switch_attr *attr,
1560 struct switch_val *val)
1562 struct ar8xxx_priv *priv = swdev_to_ar8xxx(dev);
1563 const struct ar8xxx_chip *chip = priv->chip;
1567 char *buf = priv->buf;
1570 if (!ar8xxx_has_mib_counters(priv))
1573 port = val->port_vlan;
1574 if (port >= dev->ports)
1577 mutex_lock(&priv->mib_lock);
1578 ret = ar8xxx_mib_capture(priv);
1582 ar8xxx_mib_fetch_port_stat(priv, port, false);
1584 len += snprintf(buf + len, sizeof(priv->buf) - len,
1585 "Port %d MIB counters\n",
1588 mib_stats = &priv->mib_stats[port * chip->num_mibs];
1589 for (i = 0; i < chip->num_mibs; i++)
1590 len += snprintf(buf + len, sizeof(priv->buf) - len,
1592 chip->mib_decs[i].name,
1601 mutex_unlock(&priv->mib_lock);
1605 static struct switch_attr ar8xxx_sw_attr_globals[] = {
1607 .type = SWITCH_TYPE_INT,
1608 .name = "enable_vlan",
1609 .description = "Enable VLAN mode",
1610 .set = ar8xxx_sw_set_vlan,
1611 .get = ar8xxx_sw_get_vlan,
1615 .type = SWITCH_TYPE_NOVAL,
1616 .name = "reset_mibs",
1617 .description = "Reset all MIB counters",
1618 .set = ar8xxx_sw_set_reset_mibs,
1623 static struct switch_attr ar8xxx_sw_attr_port[] = {
1625 .type = SWITCH_TYPE_NOVAL,
1626 .name = "reset_mib",
1627 .description = "Reset single port MIB counters",
1628 .set = ar8xxx_sw_set_port_reset_mib,
1631 .type = SWITCH_TYPE_STRING,
1633 .description = "Get port's MIB counters",
1635 .get = ar8xxx_sw_get_port_mib,
1639 static struct switch_attr ar8xxx_sw_attr_vlan[] = {
1641 .type = SWITCH_TYPE_INT,
1643 .description = "VLAN ID (0-4094)",
1644 .set = ar8xxx_sw_set_vid,
1645 .get = ar8xxx_sw_get_vid,
1650 static const struct switch_dev_ops ar8xxx_sw_ops = {
1652 .attr = ar8xxx_sw_attr_globals,
1653 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_globals),
1656 .attr = ar8xxx_sw_attr_port,
1657 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_port),
1660 .attr = ar8xxx_sw_attr_vlan,
1661 .n_attr = ARRAY_SIZE(ar8xxx_sw_attr_vlan),
1663 .get_port_pvid = ar8xxx_sw_get_pvid,
1664 .set_port_pvid = ar8xxx_sw_set_pvid,
1665 .get_vlan_ports = ar8xxx_sw_get_ports,
1666 .set_vlan_ports = ar8xxx_sw_set_ports,
1667 .apply_config = ar8xxx_sw_hw_apply,
1668 .reset_switch = ar8xxx_sw_reset_switch,
1669 .get_port_link = ar8xxx_sw_get_port_link,
1673 ar8xxx_id_chip(struct ar8xxx_priv *priv)
1679 val = priv->read(priv, AR8216_REG_CTRL);
1683 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1684 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
1687 val = priv->read(priv, AR8216_REG_CTRL);
1691 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
1696 priv->chip_ver = (id & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
1697 priv->chip_rev = (id & AR8216_CTRL_REVISION);
1699 switch (priv->chip_ver) {
1700 case AR8XXX_VER_AR8216:
1701 priv->chip = &ar8216_chip;
1703 case AR8XXX_VER_AR8236:
1704 priv->chip = &ar8236_chip;
1706 case AR8XXX_VER_AR8316:
1707 priv->chip = &ar8316_chip;
1709 case AR8XXX_VER_AR8327:
1710 priv->mii_lo_first = true;
1711 priv->chip = &ar8327_chip;
1714 pr_err("ar8216: Unknown Atheros device [ver=%d, rev=%d]\n",
1715 priv->chip_ver, priv->chip_rev);
1724 ar8xxx_mib_work_func(struct work_struct *work)
1726 struct ar8xxx_priv *priv;
1729 priv = container_of(work, struct ar8xxx_priv, mib_work.work);
1731 mutex_lock(&priv->mib_lock);
1733 err = ar8xxx_mib_capture(priv);
1737 ar8xxx_mib_fetch_port_stat(priv, priv->mib_next_port, false);
1740 priv->mib_next_port++;
1741 if (priv->mib_next_port >= priv->dev.ports)
1742 priv->mib_next_port = 0;
1744 mutex_unlock(&priv->mib_lock);
1745 schedule_delayed_work(&priv->mib_work,
1746 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
1750 ar8xxx_mib_init(struct ar8xxx_priv *priv)
1754 if (!ar8xxx_has_mib_counters(priv))
1757 BUG_ON(!priv->chip->mib_decs || !priv->chip->num_mibs);
1759 len = priv->dev.ports * priv->chip->num_mibs *
1760 sizeof(*priv->mib_stats);
1761 priv->mib_stats = kzalloc(len, GFP_KERNEL);
1763 if (!priv->mib_stats)
1770 ar8xxx_mib_start(struct ar8xxx_priv *priv)
1772 if (!ar8xxx_has_mib_counters(priv))
1775 schedule_delayed_work(&priv->mib_work,
1776 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY));
1780 ar8xxx_mib_stop(struct ar8xxx_priv *priv)
1782 if (!ar8xxx_has_mib_counters(priv))
1785 cancel_delayed_work(&priv->mib_work);
1788 static struct ar8xxx_priv *
1791 struct ar8xxx_priv *priv;
1793 priv = kzalloc(sizeof(struct ar8xxx_priv), GFP_KERNEL);
1797 mutex_init(&priv->reg_mutex);
1798 mutex_init(&priv->mib_lock);
1799 INIT_DELAYED_WORK(&priv->mib_work, ar8xxx_mib_work_func);
1805 ar8xxx_free(struct ar8xxx_priv *priv)
1807 kfree(priv->mib_stats);
1811 static struct ar8xxx_priv *
1812 ar8xxx_create_mii(struct mii_bus *bus)
1814 struct ar8xxx_priv *priv;
1816 priv = ar8xxx_create();
1818 priv->mii_bus = bus;
1819 priv->read = ar8xxx_mii_read;
1820 priv->write = ar8xxx_mii_write;
1827 ar8xxx_probe_switch(struct ar8xxx_priv *priv)
1829 struct switch_dev *swdev;
1832 ret = ar8xxx_id_chip(priv);
1837 swdev->cpu_port = AR8216_PORT_CPU;
1838 swdev->ops = &ar8xxx_sw_ops;
1840 if (chip_is_ar8316(priv)) {
1841 swdev->name = "Atheros AR8316";
1842 swdev->vlans = AR8X16_MAX_VLANS;
1843 swdev->ports = AR8216_NUM_PORTS;
1844 } else if (chip_is_ar8236(priv)) {
1845 swdev->name = "Atheros AR8236";
1846 swdev->vlans = AR8216_NUM_VLANS;
1847 swdev->ports = AR8216_NUM_PORTS;
1848 } else if (chip_is_ar8327(priv)) {
1849 swdev->name = "Atheros AR8327";
1850 swdev->vlans = AR8X16_MAX_VLANS;
1851 swdev->ports = AR8327_NUM_PORTS;
1853 swdev->name = "Atheros AR8216";
1854 swdev->vlans = AR8216_NUM_VLANS;
1855 swdev->ports = AR8216_NUM_PORTS;
1858 ret = ar8xxx_mib_init(priv);
1866 ar8xxx_phy_config_init(struct phy_device *phydev)
1868 struct ar8xxx_priv *priv = phydev->priv;
1869 struct net_device *dev = phydev->attached_dev;
1877 if (phydev->addr != 0) {
1878 if (chip_is_ar8316(priv)) {
1879 /* switch device has been initialized, reinit */
1880 priv->dev.ports = (AR8216_NUM_PORTS - 1);
1881 priv->initialized = false;
1882 priv->port4_phy = true;
1883 ar8316_hw_init(priv);
1892 ret = priv->chip->hw_init(priv);
1896 ret = ar8xxx_sw_reset_switch(&priv->dev);
1900 /* VID fixup only needed on ar8216 */
1901 if (chip_is_ar8216(priv)) {
1902 dev->phy_ptr = priv;
1903 dev->priv_flags |= IFF_NO_IP_ALIGN;
1904 dev->eth_mangle_rx = ar8216_mangle_rx;
1905 dev->eth_mangle_tx = ar8216_mangle_tx;
1910 ar8xxx_mib_start(priv);
1916 ar8xxx_phy_read_status(struct phy_device *phydev)
1918 struct ar8xxx_priv *priv = phydev->priv;
1919 struct switch_port_link link;
1922 if (phydev->addr != 0)
1923 return genphy_read_status(phydev);
1925 ar8216_read_port_link(priv, phydev->addr, &link);
1926 phydev->link = !!link.link;
1930 switch (link.speed) {
1931 case SWITCH_PORT_SPEED_10:
1932 phydev->speed = SPEED_10;
1934 case SWITCH_PORT_SPEED_100:
1935 phydev->speed = SPEED_100;
1937 case SWITCH_PORT_SPEED_1000:
1938 phydev->speed = SPEED_1000;
1943 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1945 /* flush the address translation unit */
1946 mutex_lock(&priv->reg_mutex);
1947 ret = priv->chip->atu_flush(priv);
1948 mutex_unlock(&priv->reg_mutex);
1950 phydev->state = PHY_RUNNING;
1951 netif_carrier_on(phydev->attached_dev);
1952 phydev->adjust_link(phydev->attached_dev);
1958 ar8xxx_phy_config_aneg(struct phy_device *phydev)
1960 if (phydev->addr == 0)
1963 return genphy_config_aneg(phydev);
1966 static const u32 ar8xxx_phy_ids[] = {
1973 ar8xxx_phy_match(u32 phy_id)
1977 for (i = 0; i < ARRAY_SIZE(ar8xxx_phy_ids); i++)
1978 if (phy_id == ar8xxx_phy_ids[i])
1985 ar8xxx_is_possible(struct mii_bus *bus)
1989 for (i = 0; i < 4; i++) {
1992 phy_id = mdiobus_read(bus, i, MII_PHYSID1) << 16;
1993 phy_id |= mdiobus_read(bus, i, MII_PHYSID2);
1994 if (!ar8xxx_phy_match(phy_id)) {
1995 pr_debug("ar8xxx: unknown PHY at %s:%02x id:%08x\n",
1996 dev_name(&bus->dev), i, phy_id);
2005 ar8xxx_phy_probe(struct phy_device *phydev)
2007 struct ar8xxx_priv *priv;
2008 struct switch_dev *swdev;
2011 /* skip PHYs at unused adresses */
2012 if (phydev->addr != 0 && phydev->addr != 4)
2015 if (!ar8xxx_is_possible(phydev->bus))
2018 mutex_lock(&ar8xxx_dev_list_lock);
2019 list_for_each_entry(priv, &ar8xxx_dev_list, list)
2020 if (priv->mii_bus == phydev->bus)
2023 priv = ar8xxx_create_mii(phydev->bus);
2029 ret = ar8xxx_probe_switch(priv);
2034 swdev->alias = dev_name(&priv->mii_bus->dev);
2035 ret = register_switch(swdev, NULL);
2039 pr_info("%s: %s switch registered on %s\n",
2040 swdev->devname, swdev->name, dev_name(&priv->mii_bus->dev));
2043 if (phydev->addr == 0) {
2044 if (ar8xxx_has_gige(priv)) {
2045 phydev->supported = SUPPORTED_1000baseT_Full;
2046 phydev->advertising = ADVERTISED_1000baseT_Full;
2048 phydev->supported = SUPPORTED_100baseT_Full;
2049 phydev->advertising = ADVERTISED_100baseT_Full;
2052 if (ar8xxx_has_gige(priv)) {
2053 phydev->supported |= SUPPORTED_1000baseT_Full;
2054 phydev->advertising |= ADVERTISED_1000baseT_Full;
2058 phydev->priv = priv;
2061 list_add(&priv->list, &ar8xxx_dev_list);
2063 mutex_unlock(&ar8xxx_dev_list_lock);
2070 mutex_unlock(&ar8xxx_dev_list_lock);
2075 ar8xxx_phy_detach(struct phy_device *phydev)
2077 struct net_device *dev = phydev->attached_dev;
2082 dev->phy_ptr = NULL;
2083 dev->priv_flags &= ~IFF_NO_IP_ALIGN;
2084 dev->eth_mangle_rx = NULL;
2085 dev->eth_mangle_tx = NULL;
2089 ar8xxx_phy_remove(struct phy_device *phydev)
2091 struct ar8xxx_priv *priv = phydev->priv;
2096 phydev->priv = NULL;
2097 if (--priv->use_count > 0)
2100 mutex_lock(&ar8xxx_dev_list_lock);
2101 list_del(&priv->list);
2102 mutex_unlock(&ar8xxx_dev_list_lock);
2104 unregister_switch(&priv->dev);
2105 ar8xxx_mib_stop(priv);
2109 static struct phy_driver ar8xxx_phy_driver = {
2110 .phy_id = 0x004d0000,
2111 .name = "Atheros AR8216/AR8236/AR8316",
2112 .phy_id_mask = 0xffff0000,
2113 .features = PHY_BASIC_FEATURES,
2114 .probe = ar8xxx_phy_probe,
2115 .remove = ar8xxx_phy_remove,
2116 .detach = ar8xxx_phy_detach,
2117 .config_init = ar8xxx_phy_config_init,
2118 .config_aneg = ar8xxx_phy_config_aneg,
2119 .read_status = ar8xxx_phy_read_status,
2120 .driver = { .owner = THIS_MODULE },
2126 return phy_driver_register(&ar8xxx_phy_driver);
2132 phy_driver_unregister(&ar8xxx_phy_driver);
2135 module_init(ar8xxx_init);
2136 module_exit(ar8xxx_exit);
2137 MODULE_LICENSE("GPL");