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 ar8216_priv
*priv
);
67 void (*init_globals
)(struct ar8216_priv
*priv
);
68 void (*init_port
)(struct ar8216_priv
*priv
, int port
);
69 void (*setup_port
)(struct ar8216_priv
*priv
, int port
, u32 egress
,
70 u32 ingress
, u32 members
, u32 pvid
);
71 u32 (*read_port_status
)(struct ar8216_priv
*priv
, int port
);
72 int (*atu_flush
)(struct ar8216_priv
*priv
);
73 void (*vtu_flush
)(struct ar8216_priv
*priv
);
74 void (*vtu_load_vlan
)(struct ar8216_priv
*priv
, u32 vid
, u32 port_mask
);
76 const struct ar8xxx_mib_desc
*mib_decs
;
81 struct switch_dev dev
;
82 struct mii_bus
*mii_bus
;
83 struct phy_device
*phy
;
84 u32 (*read
)(struct ar8216_priv
*priv
, int reg
);
85 void (*write
)(struct ar8216_priv
*priv
, int reg
, u32 val
);
86 const struct net_device_ops
*ndo_old
;
87 struct net_device_ops ndo
;
88 struct mutex reg_mutex
;
91 const struct ar8xxx_chip
*chip
;
99 struct mutex mib_lock
;
100 struct delayed_work mib_work
;
104 /* all fields below are cleared on reset */
106 u16 vlan_id
[AR8X16_MAX_VLANS
];
107 u8 vlan_table
[AR8X16_MAX_VLANS
];
109 u16 pvid
[AR8X16_MAX_PORTS
];
112 #define MIB_DESC(_s , _o, _n) \
119 static const struct ar8xxx_mib_desc ar8216_mibs
[] = {
120 MIB_DESC(1, AR8216_STATS_RXBROAD
, "RxBroad"),
121 MIB_DESC(1, AR8216_STATS_RXPAUSE
, "RxPause"),
122 MIB_DESC(1, AR8216_STATS_RXMULTI
, "RxMulti"),
123 MIB_DESC(1, AR8216_STATS_RXFCSERR
, "RxFcsErr"),
124 MIB_DESC(1, AR8216_STATS_RXALIGNERR
, "RxAlignErr"),
125 MIB_DESC(1, AR8216_STATS_RXRUNT
, "RxRunt"),
126 MIB_DESC(1, AR8216_STATS_RXFRAGMENT
, "RxFragment"),
127 MIB_DESC(1, AR8216_STATS_RX64BYTE
, "Rx64Byte"),
128 MIB_DESC(1, AR8216_STATS_RX128BYTE
, "Rx128Byte"),
129 MIB_DESC(1, AR8216_STATS_RX256BYTE
, "Rx256Byte"),
130 MIB_DESC(1, AR8216_STATS_RX512BYTE
, "Rx512Byte"),
131 MIB_DESC(1, AR8216_STATS_RX1024BYTE
, "Rx1024Byte"),
132 MIB_DESC(1, AR8216_STATS_RXMAXBYTE
, "RxMaxByte"),
133 MIB_DESC(1, AR8216_STATS_RXTOOLONG
, "RxTooLong"),
134 MIB_DESC(2, AR8216_STATS_RXGOODBYTE
, "RxGoodByte"),
135 MIB_DESC(2, AR8216_STATS_RXBADBYTE
, "RxBadByte"),
136 MIB_DESC(1, AR8216_STATS_RXOVERFLOW
, "RxOverFlow"),
137 MIB_DESC(1, AR8216_STATS_FILTERED
, "Filtered"),
138 MIB_DESC(1, AR8216_STATS_TXBROAD
, "TxBroad"),
139 MIB_DESC(1, AR8216_STATS_TXPAUSE
, "TxPause"),
140 MIB_DESC(1, AR8216_STATS_TXMULTI
, "TxMulti"),
141 MIB_DESC(1, AR8216_STATS_TXUNDERRUN
, "TxUnderRun"),
142 MIB_DESC(1, AR8216_STATS_TX64BYTE
, "Tx64Byte"),
143 MIB_DESC(1, AR8216_STATS_TX128BYTE
, "Tx128Byte"),
144 MIB_DESC(1, AR8216_STATS_TX256BYTE
, "Tx256Byte"),
145 MIB_DESC(1, AR8216_STATS_TX512BYTE
, "Tx512Byte"),
146 MIB_DESC(1, AR8216_STATS_TX1024BYTE
, "Tx1024Byte"),
147 MIB_DESC(1, AR8216_STATS_TXMAXBYTE
, "TxMaxByte"),
148 MIB_DESC(1, AR8216_STATS_TXOVERSIZE
, "TxOverSize"),
149 MIB_DESC(2, AR8216_STATS_TXBYTE
, "TxByte"),
150 MIB_DESC(1, AR8216_STATS_TXCOLLISION
, "TxCollision"),
151 MIB_DESC(1, AR8216_STATS_TXABORTCOL
, "TxAbortCol"),
152 MIB_DESC(1, AR8216_STATS_TXMULTICOL
, "TxMultiCol"),
153 MIB_DESC(1, AR8216_STATS_TXSINGLECOL
, "TxSingleCol"),
154 MIB_DESC(1, AR8216_STATS_TXEXCDEFER
, "TxExcDefer"),
155 MIB_DESC(1, AR8216_STATS_TXDEFER
, "TxDefer"),
156 MIB_DESC(1, AR8216_STATS_TXLATECOL
, "TxLateCol"),
159 static const struct ar8xxx_mib_desc ar8236_mibs
[] = {
160 MIB_DESC(1, AR8236_STATS_RXBROAD
, "RxBroad"),
161 MIB_DESC(1, AR8236_STATS_RXPAUSE
, "RxPause"),
162 MIB_DESC(1, AR8236_STATS_RXMULTI
, "RxMulti"),
163 MIB_DESC(1, AR8236_STATS_RXFCSERR
, "RxFcsErr"),
164 MIB_DESC(1, AR8236_STATS_RXALIGNERR
, "RxAlignErr"),
165 MIB_DESC(1, AR8236_STATS_RXRUNT
, "RxRunt"),
166 MIB_DESC(1, AR8236_STATS_RXFRAGMENT
, "RxFragment"),
167 MIB_DESC(1, AR8236_STATS_RX64BYTE
, "Rx64Byte"),
168 MIB_DESC(1, AR8236_STATS_RX128BYTE
, "Rx128Byte"),
169 MIB_DESC(1, AR8236_STATS_RX256BYTE
, "Rx256Byte"),
170 MIB_DESC(1, AR8236_STATS_RX512BYTE
, "Rx512Byte"),
171 MIB_DESC(1, AR8236_STATS_RX1024BYTE
, "Rx1024Byte"),
172 MIB_DESC(1, AR8236_STATS_RX1518BYTE
, "Rx1518Byte"),
173 MIB_DESC(1, AR8236_STATS_RXMAXBYTE
, "RxMaxByte"),
174 MIB_DESC(1, AR8236_STATS_RXTOOLONG
, "RxTooLong"),
175 MIB_DESC(2, AR8236_STATS_RXGOODBYTE
, "RxGoodByte"),
176 MIB_DESC(2, AR8236_STATS_RXBADBYTE
, "RxBadByte"),
177 MIB_DESC(1, AR8236_STATS_RXOVERFLOW
, "RxOverFlow"),
178 MIB_DESC(1, AR8236_STATS_FILTERED
, "Filtered"),
179 MIB_DESC(1, AR8236_STATS_TXBROAD
, "TxBroad"),
180 MIB_DESC(1, AR8236_STATS_TXPAUSE
, "TxPause"),
181 MIB_DESC(1, AR8236_STATS_TXMULTI
, "TxMulti"),
182 MIB_DESC(1, AR8236_STATS_TXUNDERRUN
, "TxUnderRun"),
183 MIB_DESC(1, AR8236_STATS_TX64BYTE
, "Tx64Byte"),
184 MIB_DESC(1, AR8236_STATS_TX128BYTE
, "Tx128Byte"),
185 MIB_DESC(1, AR8236_STATS_TX256BYTE
, "Tx256Byte"),
186 MIB_DESC(1, AR8236_STATS_TX512BYTE
, "Tx512Byte"),
187 MIB_DESC(1, AR8236_STATS_TX1024BYTE
, "Tx1024Byte"),
188 MIB_DESC(1, AR8236_STATS_TX1518BYTE
, "Tx1518Byte"),
189 MIB_DESC(1, AR8236_STATS_TXMAXBYTE
, "TxMaxByte"),
190 MIB_DESC(1, AR8236_STATS_TXOVERSIZE
, "TxOverSize"),
191 MIB_DESC(2, AR8236_STATS_TXBYTE
, "TxByte"),
192 MIB_DESC(1, AR8236_STATS_TXCOLLISION
, "TxCollision"),
193 MIB_DESC(1, AR8236_STATS_TXABORTCOL
, "TxAbortCol"),
194 MIB_DESC(1, AR8236_STATS_TXMULTICOL
, "TxMultiCol"),
195 MIB_DESC(1, AR8236_STATS_TXSINGLECOL
, "TxSingleCol"),
196 MIB_DESC(1, AR8236_STATS_TXEXCDEFER
, "TxExcDefer"),
197 MIB_DESC(1, AR8236_STATS_TXDEFER
, "TxDefer"),
198 MIB_DESC(1, AR8236_STATS_TXLATECOL
, "TxLateCol"),
201 static inline struct ar8216_priv
*
202 swdev_to_ar8216(struct switch_dev
*swdev
)
204 return container_of(swdev
, struct ar8216_priv
, dev
);
207 static inline bool ar8xxx_has_gige(struct ar8216_priv
*priv
)
209 return priv
->chip
->caps
& AR8XXX_CAP_GIGE
;
212 static inline bool ar8xxx_has_mib_counters(struct ar8216_priv
*priv
)
214 return priv
->chip
->caps
& AR8XXX_CAP_MIB_COUNTERS
;
217 static inline bool chip_is_ar8216(struct ar8216_priv
*priv
)
219 return priv
->chip_ver
== AR8XXX_VER_AR8216
;
222 static inline bool chip_is_ar8236(struct ar8216_priv
*priv
)
224 return priv
->chip_ver
== AR8XXX_VER_AR8236
;
227 static inline bool chip_is_ar8316(struct ar8216_priv
*priv
)
229 return priv
->chip_ver
== AR8XXX_VER_AR8316
;
232 static inline bool chip_is_ar8327(struct ar8216_priv
*priv
)
234 return priv
->chip_ver
== AR8XXX_VER_AR8327
;
238 split_addr(u32 regaddr
, u16
*r1
, u16
*r2
, u16
*page
)
241 *r1
= regaddr
& 0x1e;
247 *page
= regaddr
& 0x1ff;
251 ar8216_mii_read(struct ar8216_priv
*priv
, int reg
)
253 struct mii_bus
*bus
= priv
->mii_bus
;
257 split_addr((u32
) reg
, &r1
, &r2
, &page
);
259 mutex_lock(&bus
->mdio_lock
);
261 bus
->write(bus
, 0x18, 0, page
);
262 usleep_range(1000, 2000); /* wait for the page switch to propagate */
263 lo
= bus
->read(bus
, 0x10 | r2
, r1
);
264 hi
= bus
->read(bus
, 0x10 | r2
, r1
+ 1);
266 mutex_unlock(&bus
->mdio_lock
);
268 return (hi
<< 16) | lo
;
272 ar8216_mii_write(struct ar8216_priv
*priv
, int reg
, u32 val
)
274 struct mii_bus
*bus
= priv
->mii_bus
;
278 split_addr((u32
) reg
, &r1
, &r2
, &r3
);
280 hi
= (u16
) (val
>> 16);
282 mutex_lock(&bus
->mdio_lock
);
284 bus
->write(bus
, 0x18, 0, r3
);
285 usleep_range(1000, 2000); /* wait for the page switch to propagate */
286 if (priv
->mii_lo_first
) {
287 bus
->write(bus
, 0x10 | r2
, r1
, lo
);
288 bus
->write(bus
, 0x10 | r2
, r1
+ 1, hi
);
290 bus
->write(bus
, 0x10 | r2
, r1
+ 1, hi
);
291 bus
->write(bus
, 0x10 | r2
, r1
, lo
);
294 mutex_unlock(&bus
->mdio_lock
);
298 ar8216_phy_dbg_write(struct ar8216_priv
*priv
, int phy_addr
,
299 u16 dbg_addr
, u16 dbg_data
)
301 struct mii_bus
*bus
= priv
->mii_bus
;
303 mutex_lock(&bus
->mdio_lock
);
304 bus
->write(bus
, phy_addr
, MII_ATH_DBG_ADDR
, dbg_addr
);
305 bus
->write(bus
, phy_addr
, MII_ATH_DBG_DATA
, dbg_data
);
306 mutex_unlock(&bus
->mdio_lock
);
310 ar8216_phy_mmd_write(struct ar8216_priv
*priv
, int phy_addr
, u16 addr
, u16 data
)
312 struct mii_bus
*bus
= priv
->mii_bus
;
314 mutex_lock(&bus
->mdio_lock
);
315 bus
->write(bus
, phy_addr
, MII_ATH_MMD_ADDR
, addr
);
316 bus
->write(bus
, phy_addr
, MII_ATH_MMD_DATA
, data
);
317 mutex_unlock(&bus
->mdio_lock
);
321 ar8216_rmw(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
325 lockdep_assert_held(&priv
->reg_mutex
);
327 v
= priv
->read(priv
, reg
);
330 priv
->write(priv
, reg
, v
);
336 ar8216_reg_set(struct ar8216_priv
*priv
, int reg
, u32 val
)
340 lockdep_assert_held(&priv
->reg_mutex
);
342 v
= priv
->read(priv
, reg
);
344 priv
->write(priv
, reg
, v
);
348 ar8216_reg_wait(struct ar8216_priv
*priv
, u32 reg
, u32 mask
, u32 val
,
353 for (i
= 0; i
< timeout
; i
++) {
356 t
= priv
->read(priv
, reg
);
357 if ((t
& mask
) == val
)
360 usleep_range(1000, 2000);
367 ar8216_mib_op(struct ar8216_priv
*priv
, u32 op
)
372 lockdep_assert_held(&priv
->mib_lock
);
374 if (chip_is_ar8327(priv
))
375 mib_func
= AR8327_REG_MIB_FUNC
;
377 mib_func
= AR8216_REG_MIB_FUNC
;
379 mutex_lock(&priv
->reg_mutex
);
380 /* Capture the hardware statistics for all ports */
381 ar8216_rmw(priv
, mib_func
, AR8216_MIB_FUNC
, (op
<< AR8216_MIB_FUNC_S
));
382 mutex_unlock(&priv
->reg_mutex
);
384 /* Wait for the capturing to complete. */
385 ret
= ar8216_reg_wait(priv
, mib_func
, AR8216_MIB_BUSY
, 0, 10);
396 ar8216_mib_capture(struct ar8216_priv
*priv
)
398 return ar8216_mib_op(priv
, AR8216_MIB_FUNC_CAPTURE
);
402 ar8216_mib_flush(struct ar8216_priv
*priv
)
404 return ar8216_mib_op(priv
, AR8216_MIB_FUNC_FLUSH
);
408 ar8216_mib_fetch_port_stat(struct ar8216_priv
*priv
, int port
, bool flush
)
414 WARN_ON(port
>= priv
->dev
.ports
);
416 lockdep_assert_held(&priv
->mib_lock
);
418 if (chip_is_ar8327(priv
))
419 base
= AR8327_REG_PORT_STATS_BASE(port
);
420 else if (chip_is_ar8236(priv
) ||
421 chip_is_ar8316(priv
))
422 base
= AR8236_REG_PORT_STATS_BASE(port
);
424 base
= AR8216_REG_PORT_STATS_BASE(port
);
426 mib_stats
= &priv
->mib_stats
[port
* priv
->chip
->num_mibs
];
427 for (i
= 0; i
< priv
->chip
->num_mibs
; i
++) {
428 const struct ar8xxx_mib_desc
*mib
;
431 mib
= &priv
->chip
->mib_decs
[i
];
432 t
= priv
->read(priv
, base
+ mib
->offset
);
433 if (mib
->size
== 2) {
436 hi
= priv
->read(priv
, base
+ mib
->offset
+ 4);
448 ar8216_read_port_link(struct ar8216_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
);
467 link
->duplex
= !!(status
& AR8216_PORT_STATUS_DUPLEX
);
468 link
->tx_flow
= !!(status
& AR8216_PORT_STATUS_TXFLOW
);
469 link
->rx_flow
= !!(status
& AR8216_PORT_STATUS_RXFLOW
);
471 speed
= (status
& AR8216_PORT_STATUS_SPEED
) >>
472 AR8216_PORT_STATUS_SPEED_S
;
475 case AR8216_PORT_SPEED_10M
:
476 link
->speed
= SWITCH_PORT_SPEED_10
;
478 case AR8216_PORT_SPEED_100M
:
479 link
->speed
= SWITCH_PORT_SPEED_100
;
481 case AR8216_PORT_SPEED_1000M
:
482 link
->speed
= SWITCH_PORT_SPEED_1000
;
485 link
->speed
= SWITCH_PORT_SPEED_UNKNOWN
;
490 static struct sk_buff
*
491 ar8216_mangle_tx(struct net_device
*dev
, struct sk_buff
*skb
)
493 struct ar8216_priv
*priv
= dev
->phy_ptr
;
502 if (unlikely(skb_headroom(skb
) < 2)) {
503 if (pskb_expand_head(skb
, 2, 0, GFP_ATOMIC
) < 0)
507 buf
= skb_push(skb
, 2);
515 dev_kfree_skb_any(skb
);
520 ar8216_mangle_rx(struct net_device
*dev
, struct sk_buff
*skb
)
522 struct ar8216_priv
*priv
;
530 /* don't strip the header if vlan mode is disabled */
534 /* strip header, get vlan id */
538 /* check for vlan header presence */
539 if ((buf
[12 + 2] != 0x81) || (buf
[13 + 2] != 0x00))
544 /* no need to fix up packets coming from a tagged source */
545 if (priv
->vlan_tagged
& (1 << port
))
548 /* lookup port vid from local table, the switch passes an invalid vlan id */
549 vlan
= priv
->vlan_id
[priv
->pvid
[port
]];
552 buf
[14 + 2] |= vlan
>> 8;
553 buf
[15 + 2] = vlan
& 0xff;
557 ar8216_wait_bit(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
563 t
= priv
->read(priv
, reg
);
564 if ((t
& mask
) == val
)
573 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
574 (unsigned int) reg
, t
, mask
, val
);
579 ar8216_vtu_op(struct ar8216_priv
*priv
, u32 op
, u32 val
)
581 if (ar8216_wait_bit(priv
, AR8216_REG_VTU
, AR8216_VTU_ACTIVE
, 0))
583 if ((op
& AR8216_VTU_OP
) == AR8216_VTU_OP_LOAD
) {
584 val
&= AR8216_VTUDATA_MEMBER
;
585 val
|= AR8216_VTUDATA_VALID
;
586 priv
->write(priv
, AR8216_REG_VTU_DATA
, val
);
588 op
|= AR8216_VTU_ACTIVE
;
589 priv
->write(priv
, AR8216_REG_VTU
, op
);
593 ar8216_vtu_flush(struct ar8216_priv
*priv
)
595 ar8216_vtu_op(priv
, AR8216_VTU_OP_FLUSH
, 0);
599 ar8216_vtu_load_vlan(struct ar8216_priv
*priv
, u32 vid
, u32 port_mask
)
603 op
= AR8216_VTU_OP_LOAD
| (vid
<< AR8216_VTU_VID_S
);
604 ar8216_vtu_op(priv
, op
, port_mask
);
608 ar8216_atu_flush(struct ar8216_priv
*priv
)
612 ret
= ar8216_wait_bit(priv
, AR8216_REG_ATU
, AR8216_ATU_ACTIVE
, 0);
614 priv
->write(priv
, AR8216_REG_ATU
, AR8216_ATU_OP_FLUSH
);
620 ar8216_read_port_status(struct ar8216_priv
*priv
, int port
)
622 return priv
->read(priv
, AR8216_REG_PORT_STATUS(port
));
626 ar8216_setup_port(struct ar8216_priv
*priv
, int port
, u32 egress
, u32 ingress
,
627 u32 members
, u32 pvid
)
631 if (chip_is_ar8216(priv
) && priv
->vlan
&& port
== AR8216_PORT_CPU
)
632 header
= AR8216_PORT_CTRL_HEADER
;
636 ar8216_rmw(priv
, AR8216_REG_PORT_CTRL(port
),
637 AR8216_PORT_CTRL_LEARN
| AR8216_PORT_CTRL_VLAN_MODE
|
638 AR8216_PORT_CTRL_SINGLE_VLAN
| AR8216_PORT_CTRL_STATE
|
639 AR8216_PORT_CTRL_HEADER
| AR8216_PORT_CTRL_LEARN_LOCK
,
640 AR8216_PORT_CTRL_LEARN
| header
|
641 (egress
<< AR8216_PORT_CTRL_VLAN_MODE_S
) |
642 (AR8216_PORT_STATE_FORWARD
<< AR8216_PORT_CTRL_STATE_S
));
644 ar8216_rmw(priv
, AR8216_REG_PORT_VLAN(port
),
645 AR8216_PORT_VLAN_DEST_PORTS
| AR8216_PORT_VLAN_MODE
|
646 AR8216_PORT_VLAN_DEFAULT_ID
,
647 (members
<< AR8216_PORT_VLAN_DEST_PORTS_S
) |
648 (ingress
<< AR8216_PORT_VLAN_MODE_S
) |
649 (pvid
<< AR8216_PORT_VLAN_DEFAULT_ID_S
));
653 ar8216_hw_init(struct ar8216_priv
*priv
)
659 ar8216_init_globals(struct ar8216_priv
*priv
)
661 /* standard atheros magic */
662 priv
->write(priv
, 0x38, 0xc000050e);
664 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
665 AR8216_GCTRL_MTU
, 1518 + 8 + 2);
669 ar8216_init_port(struct ar8216_priv
*priv
, int port
)
671 /* Enable port learning and tx */
672 priv
->write(priv
, AR8216_REG_PORT_CTRL(port
),
673 AR8216_PORT_CTRL_LEARN
|
674 (4 << AR8216_PORT_CTRL_STATE_S
));
676 priv
->write(priv
, AR8216_REG_PORT_VLAN(port
), 0);
678 if (port
== AR8216_PORT_CPU
) {
679 priv
->write(priv
, AR8216_REG_PORT_STATUS(port
),
680 AR8216_PORT_STATUS_LINK_UP
|
681 (ar8xxx_has_gige(priv
) ?
682 AR8216_PORT_SPEED_1000M
: AR8216_PORT_SPEED_100M
) |
683 AR8216_PORT_STATUS_TXMAC
|
684 AR8216_PORT_STATUS_RXMAC
|
685 (chip_is_ar8316(priv
) ? AR8216_PORT_STATUS_RXFLOW
: 0) |
686 (chip_is_ar8316(priv
) ? AR8216_PORT_STATUS_TXFLOW
: 0) |
687 AR8216_PORT_STATUS_DUPLEX
);
689 priv
->write(priv
, AR8216_REG_PORT_STATUS(port
),
690 AR8216_PORT_STATUS_LINK_AUTO
);
694 static const struct ar8xxx_chip ar8216_chip
= {
695 .caps
= AR8XXX_CAP_MIB_COUNTERS
,
697 .hw_init
= ar8216_hw_init
,
698 .init_globals
= ar8216_init_globals
,
699 .init_port
= ar8216_init_port
,
700 .setup_port
= ar8216_setup_port
,
701 .read_port_status
= ar8216_read_port_status
,
702 .atu_flush
= ar8216_atu_flush
,
703 .vtu_flush
= ar8216_vtu_flush
,
704 .vtu_load_vlan
= ar8216_vtu_load_vlan
,
706 .num_mibs
= ARRAY_SIZE(ar8216_mibs
),
707 .mib_decs
= ar8216_mibs
,
711 ar8236_setup_port(struct ar8216_priv
*priv
, int port
, u32 egress
, u32 ingress
,
712 u32 members
, u32 pvid
)
714 ar8216_rmw(priv
, AR8216_REG_PORT_CTRL(port
),
715 AR8216_PORT_CTRL_LEARN
| AR8216_PORT_CTRL_VLAN_MODE
|
716 AR8216_PORT_CTRL_SINGLE_VLAN
| AR8216_PORT_CTRL_STATE
|
717 AR8216_PORT_CTRL_HEADER
| AR8216_PORT_CTRL_LEARN_LOCK
,
718 AR8216_PORT_CTRL_LEARN
|
719 (egress
<< AR8216_PORT_CTRL_VLAN_MODE_S
) |
720 (AR8216_PORT_STATE_FORWARD
<< AR8216_PORT_CTRL_STATE_S
));
722 ar8216_rmw(priv
, AR8236_REG_PORT_VLAN(port
),
723 AR8236_PORT_VLAN_DEFAULT_ID
,
724 (pvid
<< AR8236_PORT_VLAN_DEFAULT_ID_S
));
726 ar8216_rmw(priv
, AR8236_REG_PORT_VLAN2(port
),
727 AR8236_PORT_VLAN2_VLAN_MODE
|
728 AR8236_PORT_VLAN2_MEMBER
,
729 (ingress
<< AR8236_PORT_VLAN2_VLAN_MODE_S
) |
730 (members
<< AR8236_PORT_VLAN2_MEMBER_S
));
734 ar8236_hw_init(struct ar8216_priv
*priv
)
739 if (priv
->initialized
)
742 /* Initialize the PHYs */
744 for (i
= 0; i
< 5; i
++) {
745 mdiobus_write(bus
, i
, MII_ADVERTISE
,
746 ADVERTISE_ALL
| ADVERTISE_PAUSE_CAP
|
747 ADVERTISE_PAUSE_ASYM
);
748 mdiobus_write(bus
, i
, MII_BMCR
, BMCR_RESET
| BMCR_ANENABLE
);
752 priv
->initialized
= true;
757 ar8236_init_globals(struct ar8216_priv
*priv
)
759 /* enable jumbo frames */
760 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
761 AR8316_GCTRL_MTU
, 9018 + 8 + 2);
763 /* Enable MIB counters */
764 ar8216_rmw(priv
, AR8216_REG_MIB_FUNC
, AR8216_MIB_FUNC
| AR8236_MIB_EN
,
765 (AR8216_MIB_FUNC_NO_OP
<< AR8216_MIB_FUNC_S
) |
769 static const struct ar8xxx_chip ar8236_chip
= {
770 .caps
= AR8XXX_CAP_MIB_COUNTERS
,
771 .hw_init
= ar8236_hw_init
,
772 .init_globals
= ar8236_init_globals
,
773 .init_port
= ar8216_init_port
,
774 .setup_port
= ar8236_setup_port
,
775 .read_port_status
= ar8216_read_port_status
,
776 .atu_flush
= ar8216_atu_flush
,
777 .vtu_flush
= ar8216_vtu_flush
,
778 .vtu_load_vlan
= ar8216_vtu_load_vlan
,
780 .num_mibs
= ARRAY_SIZE(ar8236_mibs
),
781 .mib_decs
= ar8236_mibs
,
785 ar8316_hw_init(struct ar8216_priv
*priv
)
791 val
= priv
->read(priv
, 0x8);
793 if (priv
->phy
->interface
== PHY_INTERFACE_MODE_RGMII
) {
794 if (priv
->port4_phy
) {
795 /* value taken from Ubiquiti RouterStation Pro */
797 printk(KERN_INFO
"ar8316: Using port 4 as PHY\n");
800 printk(KERN_INFO
"ar8316: Using port 4 as switch port\n");
802 } else if (priv
->phy
->interface
== PHY_INTERFACE_MODE_GMII
) {
803 /* value taken from AVM Fritz!Box 7390 sources */
806 /* no known value for phy interface */
807 printk(KERN_ERR
"ar8316: unsupported mii mode: %d.\n",
808 priv
->phy
->interface
);
815 priv
->write(priv
, 0x8, newval
);
817 /* Initialize the ports */
819 for (i
= 0; i
< 5; i
++) {
820 if ((i
== 4) && priv
->port4_phy
&&
821 priv
->phy
->interface
== PHY_INTERFACE_MODE_RGMII
) {
822 /* work around for phy4 rgmii mode */
823 ar8216_phy_dbg_write(priv
, i
, 0x12, 0x480c);
825 ar8216_phy_dbg_write(priv
, i
, 0x0, 0x824e);
827 ar8216_phy_dbg_write(priv
, i
, 0x5, 0x3d47);
831 /* initialize the port itself */
832 mdiobus_write(bus
, i
, MII_ADVERTISE
,
833 ADVERTISE_ALL
| ADVERTISE_PAUSE_CAP
| ADVERTISE_PAUSE_ASYM
);
834 mdiobus_write(bus
, i
, MII_CTRL1000
, ADVERTISE_1000FULL
);
835 mdiobus_write(bus
, i
, MII_BMCR
, BMCR_RESET
| BMCR_ANENABLE
);
840 priv
->initialized
= true;
845 ar8316_init_globals(struct ar8216_priv
*priv
)
847 /* standard atheros magic */
848 priv
->write(priv
, 0x38, 0xc000050e);
850 /* enable cpu port to receive multicast and broadcast frames */
851 priv
->write(priv
, AR8216_REG_FLOOD_MASK
, 0x003f003f);
853 /* enable jumbo frames */
854 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
855 AR8316_GCTRL_MTU
, 9018 + 8 + 2);
857 /* Enable MIB counters */
858 ar8216_rmw(priv
, AR8216_REG_MIB_FUNC
, AR8216_MIB_FUNC
| AR8236_MIB_EN
,
859 (AR8216_MIB_FUNC_NO_OP
<< AR8216_MIB_FUNC_S
) |
863 static const struct ar8xxx_chip ar8316_chip
= {
864 .caps
= AR8XXX_CAP_GIGE
| AR8XXX_CAP_MIB_COUNTERS
,
865 .hw_init
= ar8316_hw_init
,
866 .init_globals
= ar8316_init_globals
,
867 .init_port
= ar8216_init_port
,
868 .setup_port
= ar8216_setup_port
,
869 .read_port_status
= ar8216_read_port_status
,
870 .atu_flush
= ar8216_atu_flush
,
871 .vtu_flush
= ar8216_vtu_flush
,
872 .vtu_load_vlan
= ar8216_vtu_load_vlan
,
874 .num_mibs
= ARRAY_SIZE(ar8236_mibs
),
875 .mib_decs
= ar8236_mibs
,
879 ar8327_get_pad_cfg(struct ar8327_pad_cfg
*cfg
)
891 case AR8327_PAD_MAC2MAC_MII
:
892 t
= AR8327_PAD_MAC_MII_EN
;
894 t
|= AR8327_PAD_MAC_MII_RXCLK_SEL
;
896 t
|= AR8327_PAD_MAC_MII_TXCLK_SEL
;
899 case AR8327_PAD_MAC2MAC_GMII
:
900 t
= AR8327_PAD_MAC_GMII_EN
;
902 t
|= AR8327_PAD_MAC_GMII_RXCLK_SEL
;
904 t
|= AR8327_PAD_MAC_GMII_TXCLK_SEL
;
907 case AR8327_PAD_MAC_SGMII
:
908 t
= AR8327_PAD_SGMII_EN
;
911 * WAR for the QUalcomm Atheros AP136 board.
912 * It seems that RGMII TX/RX delay settings needs to be
913 * applied for SGMII mode as well, The ethernet is not
914 * reliable without this.
916 t
|= cfg
->txclk_delay_sel
<< AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S
;
917 t
|= cfg
->rxclk_delay_sel
<< AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S
;
918 if (cfg
->rxclk_delay_en
)
919 t
|= AR8327_PAD_RGMII_RXCLK_DELAY_EN
;
920 if (cfg
->txclk_delay_en
)
921 t
|= AR8327_PAD_RGMII_TXCLK_DELAY_EN
;
923 if (cfg
->sgmii_delay_en
)
924 t
|= AR8327_PAD_SGMII_DELAY_EN
;
928 case AR8327_PAD_MAC2PHY_MII
:
929 t
= AR8327_PAD_PHY_MII_EN
;
931 t
|= AR8327_PAD_PHY_MII_RXCLK_SEL
;
933 t
|= AR8327_PAD_PHY_MII_TXCLK_SEL
;
936 case AR8327_PAD_MAC2PHY_GMII
:
937 t
= AR8327_PAD_PHY_GMII_EN
;
938 if (cfg
->pipe_rxclk_sel
)
939 t
|= AR8327_PAD_PHY_GMII_PIPE_RXCLK_SEL
;
941 t
|= AR8327_PAD_PHY_GMII_RXCLK_SEL
;
943 t
|= AR8327_PAD_PHY_GMII_TXCLK_SEL
;
946 case AR8327_PAD_MAC_RGMII
:
947 t
= AR8327_PAD_RGMII_EN
;
948 t
|= cfg
->txclk_delay_sel
<< AR8327_PAD_RGMII_TXCLK_DELAY_SEL_S
;
949 t
|= cfg
->rxclk_delay_sel
<< AR8327_PAD_RGMII_RXCLK_DELAY_SEL_S
;
950 if (cfg
->rxclk_delay_en
)
951 t
|= AR8327_PAD_RGMII_RXCLK_DELAY_EN
;
952 if (cfg
->txclk_delay_en
)
953 t
|= AR8327_PAD_RGMII_TXCLK_DELAY_EN
;
956 case AR8327_PAD_PHY_GMII
:
957 t
= AR8327_PAD_PHYX_GMII_EN
;
960 case AR8327_PAD_PHY_RGMII
:
961 t
= AR8327_PAD_PHYX_RGMII_EN
;
964 case AR8327_PAD_PHY_MII
:
965 t
= AR8327_PAD_PHYX_MII_EN
;
973 ar8327_phy_fixup(struct ar8216_priv
*priv
, int phy
)
975 switch (priv
->chip_rev
) {
977 /* For 100M waveform */
978 ar8216_phy_dbg_write(priv
, phy
, 0, 0x02ea);
979 /* Turn on Gigabit clock */
980 ar8216_phy_dbg_write(priv
, phy
, 0x3d, 0x68a0);
984 ar8216_phy_mmd_write(priv
, phy
, 0x7, 0x3c);
985 ar8216_phy_mmd_write(priv
, phy
, 0x4007, 0x0);
988 ar8216_phy_mmd_write(priv
, phy
, 0x3, 0x800d);
989 ar8216_phy_mmd_write(priv
, phy
, 0x4003, 0x803f);
991 ar8216_phy_dbg_write(priv
, phy
, 0x3d, 0x6860);
992 ar8216_phy_dbg_write(priv
, phy
, 0x5, 0x2c46);
993 ar8216_phy_dbg_write(priv
, phy
, 0x3c, 0x6000);
999 ar8327_hw_init(struct ar8216_priv
*priv
)
1001 struct ar8327_platform_data
*pdata
;
1002 struct ar8327_led_cfg
*led_cfg
;
1003 struct mii_bus
*bus
;
1008 pdata
= priv
->phy
->dev
.platform_data
;
1012 t
= ar8327_get_pad_cfg(pdata
->pad0_cfg
);
1013 priv
->write(priv
, AR8327_REG_PAD0_MODE
, t
);
1014 t
= ar8327_get_pad_cfg(pdata
->pad5_cfg
);
1015 priv
->write(priv
, AR8327_REG_PAD5_MODE
, t
);
1016 t
= ar8327_get_pad_cfg(pdata
->pad6_cfg
);
1017 priv
->write(priv
, AR8327_REG_PAD6_MODE
, t
);
1019 pos
= priv
->read(priv
, AR8327_REG_POWER_ON_STRIP
);
1022 led_cfg
= pdata
->led_cfg
;
1024 if (led_cfg
->open_drain
)
1025 new_pos
|= AR8327_POWER_ON_STRIP_LED_OPEN_EN
;
1027 new_pos
&= ~AR8327_POWER_ON_STRIP_LED_OPEN_EN
;
1029 priv
->write(priv
, AR8327_REG_LED_CTRL0
, led_cfg
->led_ctrl0
);
1030 priv
->write(priv
, AR8327_REG_LED_CTRL1
, led_cfg
->led_ctrl1
);
1031 priv
->write(priv
, AR8327_REG_LED_CTRL2
, led_cfg
->led_ctrl2
);
1032 priv
->write(priv
, AR8327_REG_LED_CTRL3
, led_cfg
->led_ctrl3
);
1035 if (new_pos
!= pos
) {
1036 new_pos
|= AR8327_POWER_ON_STRIP_POWER_ON_SEL
;
1037 priv
->write(priv
, AR8327_REG_POWER_ON_STRIP
, new_pos
);
1040 bus
= priv
->mii_bus
;
1041 for (i
= 0; i
< AR8327_NUM_PHYS
; i
++) {
1042 ar8327_phy_fixup(priv
, i
);
1044 /* start aneg on the PHY */
1045 mdiobus_write(bus
, i
, MII_ADVERTISE
, ADVERTISE_ALL
|
1046 ADVERTISE_PAUSE_CAP
|
1047 ADVERTISE_PAUSE_ASYM
);
1048 mdiobus_write(bus
, i
, MII_CTRL1000
, ADVERTISE_1000FULL
);
1049 mdiobus_write(bus
, i
, MII_BMCR
, BMCR_RESET
| BMCR_ANENABLE
);
1058 ar8327_init_globals(struct ar8216_priv
*priv
)
1062 /* enable CPU port and disable mirror port */
1063 t
= AR8327_FWD_CTRL0_CPU_PORT_EN
|
1064 AR8327_FWD_CTRL0_MIRROR_PORT
;
1065 priv
->write(priv
, AR8327_REG_FWD_CTRL0
, t
);
1067 /* forward multicast and broadcast frames to CPU */
1068 t
= (AR8327_PORTS_ALL
<< AR8327_FWD_CTRL1_UC_FLOOD_S
) |
1069 (AR8327_PORTS_ALL
<< AR8327_FWD_CTRL1_MC_FLOOD_S
) |
1070 (AR8327_PORTS_ALL
<< AR8327_FWD_CTRL1_BC_FLOOD_S
);
1071 priv
->write(priv
, AR8327_REG_FWD_CTRL1
, t
);
1074 ar8216_rmw(priv
, AR8327_REG_MAX_FRAME_SIZE
,
1075 AR8327_MAX_FRAME_SIZE_MTU
, 1518 + 8 + 2);
1077 /* Enable MIB counters */
1078 ar8216_reg_set(priv
, AR8327_REG_MODULE_EN
,
1079 AR8327_MODULE_EN_MIB
);
1083 ar8327_config_port(struct ar8216_priv
*priv
, unsigned int port
,
1084 struct ar8327_port_cfg
*cfg
)
1088 if (!cfg
|| !cfg
->force_link
) {
1089 priv
->write(priv
, AR8327_REG_PORT_STATUS(port
),
1090 AR8216_PORT_STATUS_LINK_AUTO
);
1094 t
= AR8216_PORT_STATUS_TXMAC
| AR8216_PORT_STATUS_RXMAC
;
1095 t
|= cfg
->duplex
? AR8216_PORT_STATUS_DUPLEX
: 0;
1096 t
|= cfg
->rxpause
? AR8216_PORT_STATUS_RXFLOW
: 0;
1097 t
|= cfg
->txpause
? AR8216_PORT_STATUS_TXFLOW
: 0;
1099 switch (cfg
->speed
) {
1100 case AR8327_PORT_SPEED_10
:
1101 t
|= AR8216_PORT_SPEED_10M
;
1103 case AR8327_PORT_SPEED_100
:
1104 t
|= AR8216_PORT_SPEED_100M
;
1106 case AR8327_PORT_SPEED_1000
:
1107 t
|= AR8216_PORT_SPEED_1000M
;
1111 priv
->write(priv
, AR8327_REG_PORT_STATUS(port
), t
);
1115 ar8327_init_port(struct ar8216_priv
*priv
, int port
)
1117 struct ar8327_platform_data
*pdata
;
1118 struct ar8327_port_cfg
*cfg
;
1121 pdata
= priv
->phy
->dev
.platform_data
;
1123 if (pdata
&& port
== AR8216_PORT_CPU
)
1124 cfg
= &pdata
->port0_cfg
;
1125 else if (pdata
&& port
== 6)
1126 cfg
= &pdata
->port6_cfg
;
1130 ar8327_config_port(priv
, port
, cfg
);
1132 priv
->write(priv
, AR8327_REG_PORT_HEADER(port
), 0);
1134 t
= 1 << AR8327_PORT_VLAN0_DEF_SVID_S
;
1135 t
|= 1 << AR8327_PORT_VLAN0_DEF_CVID_S
;
1136 priv
->write(priv
, AR8327_REG_PORT_VLAN0(port
), t
);
1138 t
= AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH
<< AR8327_PORT_VLAN1_OUT_MODE_S
;
1139 priv
->write(priv
, AR8327_REG_PORT_VLAN1(port
), t
);
1141 t
= AR8327_PORT_LOOKUP_LEARN
;
1142 t
|= AR8216_PORT_STATE_FORWARD
<< AR8327_PORT_LOOKUP_STATE_S
;
1143 priv
->write(priv
, AR8327_REG_PORT_LOOKUP(port
), t
);
1147 ar8327_read_port_status(struct ar8216_priv
*priv
, int port
)
1149 return priv
->read(priv
, AR8327_REG_PORT_STATUS(port
));
1153 ar8327_atu_flush(struct ar8216_priv
*priv
)
1157 ret
= ar8216_wait_bit(priv
, AR8327_REG_ATU_FUNC
,
1158 AR8327_ATU_FUNC_BUSY
, 0);
1160 priv
->write(priv
, AR8327_REG_ATU_FUNC
,
1161 AR8327_ATU_FUNC_OP_FLUSH
);
1167 ar8327_vtu_op(struct ar8216_priv
*priv
, u32 op
, u32 val
)
1169 if (ar8216_wait_bit(priv
, AR8327_REG_VTU_FUNC1
,
1170 AR8327_VTU_FUNC1_BUSY
, 0))
1173 if ((op
& AR8327_VTU_FUNC1_OP
) == AR8327_VTU_FUNC1_OP_LOAD
)
1174 priv
->write(priv
, AR8327_REG_VTU_FUNC0
, val
);
1176 op
|= AR8327_VTU_FUNC1_BUSY
;
1177 priv
->write(priv
, AR8327_REG_VTU_FUNC1
, op
);
1181 ar8327_vtu_flush(struct ar8216_priv
*priv
)
1183 ar8327_vtu_op(priv
, AR8327_VTU_FUNC1_OP_FLUSH
, 0);
1187 ar8327_vtu_load_vlan(struct ar8216_priv
*priv
, u32 vid
, u32 port_mask
)
1193 op
= AR8327_VTU_FUNC1_OP_LOAD
| (vid
<< AR8327_VTU_FUNC1_VID_S
);
1194 val
= AR8327_VTU_FUNC0_VALID
| AR8327_VTU_FUNC0_IVL
;
1195 for (i
= 0; i
< AR8327_NUM_PORTS
; i
++) {
1198 if ((port_mask
& BIT(i
)) == 0)
1199 mode
= AR8327_VTU_FUNC0_EG_MODE_NOT
;
1200 else if (priv
->vlan
== 0)
1201 mode
= AR8327_VTU_FUNC0_EG_MODE_KEEP
;
1202 else if (priv
->vlan_tagged
& BIT(i
))
1203 mode
= AR8327_VTU_FUNC0_EG_MODE_TAG
;
1205 mode
= AR8327_VTU_FUNC0_EG_MODE_UNTAG
;
1207 val
|= mode
<< AR8327_VTU_FUNC0_EG_MODE_S(i
);
1209 ar8327_vtu_op(priv
, op
, val
);
1213 ar8327_setup_port(struct ar8216_priv
*priv
, int port
, u32 egress
, u32 ingress
,
1214 u32 members
, u32 pvid
)
1219 t
= pvid
<< AR8327_PORT_VLAN0_DEF_SVID_S
;
1220 t
|= pvid
<< AR8327_PORT_VLAN0_DEF_CVID_S
;
1221 priv
->write(priv
, AR8327_REG_PORT_VLAN0(port
), t
);
1223 mode
= AR8327_PORT_VLAN1_OUT_MODE_UNMOD
;
1225 case AR8216_OUT_KEEP
:
1226 mode
= AR8327_PORT_VLAN1_OUT_MODE_UNTOUCH
;
1228 case AR8216_OUT_STRIP_VLAN
:
1229 mode
= AR8327_PORT_VLAN1_OUT_MODE_UNTAG
;
1231 case AR8216_OUT_ADD_VLAN
:
1232 mode
= AR8327_PORT_VLAN1_OUT_MODE_TAG
;
1236 t
= AR8327_PORT_VLAN1_PORT_VLAN_PROP
;
1237 t
|= mode
<< AR8327_PORT_VLAN1_OUT_MODE_S
;
1238 priv
->write(priv
, AR8327_REG_PORT_VLAN1(port
), t
);
1241 t
|= AR8327_PORT_LOOKUP_LEARN
;
1242 t
|= ingress
<< AR8327_PORT_LOOKUP_IN_MODE_S
;
1243 t
|= AR8216_PORT_STATE_FORWARD
<< AR8327_PORT_LOOKUP_STATE_S
;
1244 priv
->write(priv
, AR8327_REG_PORT_LOOKUP(port
), t
);
1247 static const struct ar8xxx_chip ar8327_chip
= {
1248 .caps
= AR8XXX_CAP_GIGE
| AR8XXX_CAP_MIB_COUNTERS
,
1249 .hw_init
= ar8327_hw_init
,
1250 .init_globals
= ar8327_init_globals
,
1251 .init_port
= ar8327_init_port
,
1252 .setup_port
= ar8327_setup_port
,
1253 .read_port_status
= ar8327_read_port_status
,
1254 .atu_flush
= ar8327_atu_flush
,
1255 .vtu_flush
= ar8327_vtu_flush
,
1256 .vtu_load_vlan
= ar8327_vtu_load_vlan
,
1258 .num_mibs
= ARRAY_SIZE(ar8236_mibs
),
1259 .mib_decs
= ar8236_mibs
,
1263 ar8216_sw_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
1264 struct switch_val
*val
)
1266 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1267 priv
->vlan
= !!val
->value
.i
;
1272 ar8216_sw_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
1273 struct switch_val
*val
)
1275 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1276 val
->value
.i
= priv
->vlan
;
1282 ar8216_sw_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
1284 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1286 /* make sure no invalid PVIDs get set */
1288 if (vlan
>= dev
->vlans
)
1291 priv
->pvid
[port
] = vlan
;
1296 ar8216_sw_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
1298 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1299 *vlan
= priv
->pvid
[port
];
1304 ar8216_sw_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
1305 struct switch_val
*val
)
1307 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1308 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
1313 ar8216_sw_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
1314 struct switch_val
*val
)
1316 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1317 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
1322 ar8216_sw_get_port_link(struct switch_dev
*dev
, int port
,
1323 struct switch_port_link
*link
)
1325 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1327 ar8216_read_port_link(priv
, port
, link
);
1332 ar8216_sw_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1334 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1335 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
1339 for (i
= 0; i
< dev
->ports
; i
++) {
1340 struct switch_port
*p
;
1342 if (!(ports
& (1 << i
)))
1345 p
= &val
->value
.ports
[val
->len
++];
1347 if (priv
->vlan_tagged
& (1 << i
))
1348 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
1356 ar8216_sw_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
1358 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1359 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
1363 for (i
= 0; i
< val
->len
; i
++) {
1364 struct switch_port
*p
= &val
->value
.ports
[i
];
1366 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
)) {
1367 priv
->vlan_tagged
|= (1 << p
->id
);
1369 priv
->vlan_tagged
&= ~(1 << p
->id
);
1370 priv
->pvid
[p
->id
] = val
->port_vlan
;
1372 /* make sure that an untagged port does not
1373 * appear in other vlans */
1374 for (j
= 0; j
< AR8X16_MAX_VLANS
; j
++) {
1375 if (j
== val
->port_vlan
)
1377 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
1387 ar8216_sw_hw_apply(struct switch_dev
*dev
)
1389 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1390 u8 portmask
[AR8X16_MAX_PORTS
];
1393 mutex_lock(&priv
->reg_mutex
);
1394 /* flush all vlan translation unit entries */
1395 priv
->chip
->vtu_flush(priv
);
1397 memset(portmask
, 0, sizeof(portmask
));
1399 /* calculate the port destination masks and load vlans
1400 * into the vlan translation unit */
1401 for (j
= 0; j
< AR8X16_MAX_VLANS
; j
++) {
1402 u8 vp
= priv
->vlan_table
[j
];
1407 for (i
= 0; i
< dev
->ports
; i
++) {
1410 portmask
[i
] |= vp
& ~mask
;
1413 priv
->chip
->vtu_load_vlan(priv
, priv
->vlan_id
[j
],
1414 priv
->vlan_table
[j
]);
1418 * isolate all ports, but connect them to the cpu port */
1419 for (i
= 0; i
< dev
->ports
; i
++) {
1420 if (i
== AR8216_PORT_CPU
)
1423 portmask
[i
] = 1 << AR8216_PORT_CPU
;
1424 portmask
[AR8216_PORT_CPU
] |= (1 << i
);
1428 /* update the port destination mask registers and tag settings */
1429 for (i
= 0; i
< dev
->ports
; i
++) {
1430 int egress
, ingress
;
1434 pvid
= priv
->vlan_id
[priv
->pvid
[i
]];
1435 if (priv
->vlan_tagged
& (1 << i
))
1436 egress
= AR8216_OUT_ADD_VLAN
;
1438 egress
= AR8216_OUT_STRIP_VLAN
;
1439 ingress
= AR8216_IN_SECURE
;
1442 egress
= AR8216_OUT_KEEP
;
1443 ingress
= AR8216_IN_PORT_ONLY
;
1446 priv
->chip
->setup_port(priv
, i
, egress
, ingress
, portmask
[i
],
1449 mutex_unlock(&priv
->reg_mutex
);
1454 ar8216_sw_reset_switch(struct switch_dev
*dev
)
1456 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1459 mutex_lock(&priv
->reg_mutex
);
1460 memset(&priv
->vlan
, 0, sizeof(struct ar8216_priv
) -
1461 offsetof(struct ar8216_priv
, vlan
));
1463 for (i
= 0; i
< AR8X16_MAX_VLANS
; i
++)
1464 priv
->vlan_id
[i
] = i
;
1466 /* Configure all ports */
1467 for (i
= 0; i
< dev
->ports
; i
++)
1468 priv
->chip
->init_port(priv
, i
);
1470 priv
->chip
->init_globals(priv
);
1471 mutex_unlock(&priv
->reg_mutex
);
1473 return ar8216_sw_hw_apply(dev
);
1477 ar8216_sw_set_reset_mibs(struct switch_dev
*dev
,
1478 const struct switch_attr
*attr
,
1479 struct switch_val
*val
)
1481 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1485 if (!ar8xxx_has_mib_counters(priv
))
1488 mutex_lock(&priv
->mib_lock
);
1490 len
= priv
->dev
.ports
* priv
->chip
->num_mibs
*
1491 sizeof(*priv
->mib_stats
);
1492 memset(priv
->mib_stats
, '\0', len
);
1493 ret
= ar8216_mib_flush(priv
);
1500 mutex_unlock(&priv
->mib_lock
);
1505 ar8216_sw_set_port_reset_mib(struct switch_dev
*dev
,
1506 const struct switch_attr
*attr
,
1507 struct switch_val
*val
)
1509 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1513 if (!ar8xxx_has_mib_counters(priv
))
1516 port
= val
->port_vlan
;
1517 if (port
>= dev
->ports
)
1520 mutex_lock(&priv
->mib_lock
);
1521 ret
= ar8216_mib_capture(priv
);
1525 ar8216_mib_fetch_port_stat(priv
, port
, true);
1530 mutex_unlock(&priv
->mib_lock
);
1535 ar8216_sw_get_port_mib(struct switch_dev
*dev
,
1536 const struct switch_attr
*attr
,
1537 struct switch_val
*val
)
1539 struct ar8216_priv
*priv
= swdev_to_ar8216(dev
);
1540 const struct ar8xxx_chip
*chip
= priv
->chip
;
1544 char *buf
= priv
->buf
;
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
= ar8216_mib_capture(priv
);
1559 ar8216_mib_fetch_port_stat(priv
, port
, false);
1561 len
+= snprintf(buf
+ len
, sizeof(priv
->buf
) - len
,
1562 "Port %d MIB counters\n",
1565 mib_stats
= &priv
->mib_stats
[port
* chip
->num_mibs
];
1566 for (i
= 0; i
< chip
->num_mibs
; i
++)
1567 len
+= snprintf(buf
+ len
, sizeof(priv
->buf
) - len
,
1569 chip
->mib_decs
[i
].name
,
1578 mutex_unlock(&priv
->mib_lock
);
1582 static struct switch_attr ar8216_globals
[] = {
1584 .type
= SWITCH_TYPE_INT
,
1585 .name
= "enable_vlan",
1586 .description
= "Enable VLAN mode",
1587 .set
= ar8216_sw_set_vlan
,
1588 .get
= ar8216_sw_get_vlan
,
1592 .type
= SWITCH_TYPE_NOVAL
,
1593 .name
= "reset_mibs",
1594 .description
= "Reset all MIB counters",
1595 .set
= ar8216_sw_set_reset_mibs
,
1600 static struct switch_attr ar8216_port
[] = {
1602 .type
= SWITCH_TYPE_NOVAL
,
1603 .name
= "reset_mib",
1604 .description
= "Reset single port MIB counters",
1605 .set
= ar8216_sw_set_port_reset_mib
,
1608 .type
= SWITCH_TYPE_STRING
,
1610 .description
= "Get port's MIB counters",
1612 .get
= ar8216_sw_get_port_mib
,
1616 static struct switch_attr ar8216_vlan
[] = {
1618 .type
= SWITCH_TYPE_INT
,
1620 .description
= "VLAN ID (0-4094)",
1621 .set
= ar8216_sw_set_vid
,
1622 .get
= ar8216_sw_get_vid
,
1627 static const struct switch_dev_ops ar8216_sw_ops
= {
1629 .attr
= ar8216_globals
,
1630 .n_attr
= ARRAY_SIZE(ar8216_globals
),
1633 .attr
= ar8216_port
,
1634 .n_attr
= ARRAY_SIZE(ar8216_port
),
1637 .attr
= ar8216_vlan
,
1638 .n_attr
= ARRAY_SIZE(ar8216_vlan
),
1640 .get_port_pvid
= ar8216_sw_get_pvid
,
1641 .set_port_pvid
= ar8216_sw_set_pvid
,
1642 .get_vlan_ports
= ar8216_sw_get_ports
,
1643 .set_vlan_ports
= ar8216_sw_set_ports
,
1644 .apply_config
= ar8216_sw_hw_apply
,
1645 .reset_switch
= ar8216_sw_reset_switch
,
1646 .get_port_link
= ar8216_sw_get_port_link
,
1650 ar8216_id_chip(struct ar8216_priv
*priv
)
1656 val
= ar8216_mii_read(priv
, AR8216_REG_CTRL
);
1660 id
= val
& (AR8216_CTRL_REVISION
| AR8216_CTRL_VERSION
);
1661 for (i
= 0; i
< AR8X16_PROBE_RETRIES
; i
++) {
1664 val
= ar8216_mii_read(priv
, AR8216_REG_CTRL
);
1668 t
= val
& (AR8216_CTRL_REVISION
| AR8216_CTRL_VERSION
);
1673 priv
->chip_ver
= (id
& AR8216_CTRL_VERSION
) >> AR8216_CTRL_VERSION_S
;
1674 priv
->chip_rev
= (id
& AR8216_CTRL_REVISION
);
1676 switch (priv
->chip_ver
) {
1677 case AR8XXX_VER_AR8216
:
1678 priv
->chip
= &ar8216_chip
;
1680 case AR8XXX_VER_AR8236
:
1681 priv
->chip
= &ar8236_chip
;
1683 case AR8XXX_VER_AR8316
:
1684 priv
->chip
= &ar8316_chip
;
1686 case AR8XXX_VER_AR8327
:
1687 priv
->mii_lo_first
= true;
1688 priv
->chip
= &ar8327_chip
;
1692 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
1693 priv
->chip_ver
, priv
->chip_rev
,
1694 mdiobus_read(priv
->mii_bus
, priv
->phy
->addr
, 2),
1695 mdiobus_read(priv
->mii_bus
, priv
->phy
->addr
, 3));
1704 ar8xxx_mib_work_func(struct work_struct
*work
)
1706 struct ar8216_priv
*priv
;
1709 priv
= container_of(work
, struct ar8216_priv
, mib_work
.work
);
1711 mutex_lock(&priv
->mib_lock
);
1713 err
= ar8216_mib_capture(priv
);
1717 ar8216_mib_fetch_port_stat(priv
, priv
->mib_next_port
, false);
1720 priv
->mib_next_port
++;
1721 if (priv
->mib_next_port
>= priv
->dev
.ports
)
1722 priv
->mib_next_port
= 0;
1724 mutex_unlock(&priv
->mib_lock
);
1725 schedule_delayed_work(&priv
->mib_work
,
1726 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY
));
1730 ar8xxx_mib_init(struct ar8216_priv
*priv
)
1734 if (!ar8xxx_has_mib_counters(priv
))
1737 BUG_ON(!priv
->chip
->mib_decs
|| !priv
->chip
->num_mibs
);
1739 len
= priv
->dev
.ports
* priv
->chip
->num_mibs
*
1740 sizeof(*priv
->mib_stats
);
1741 priv
->mib_stats
= kzalloc(len
, GFP_KERNEL
);
1743 if (!priv
->mib_stats
)
1746 mutex_init(&priv
->mib_lock
);
1747 INIT_DELAYED_WORK(&priv
->mib_work
, ar8xxx_mib_work_func
);
1753 ar8xxx_mib_start(struct ar8216_priv
*priv
)
1755 if (!ar8xxx_has_mib_counters(priv
))
1758 schedule_delayed_work(&priv
->mib_work
,
1759 msecs_to_jiffies(AR8XXX_MIB_WORK_DELAY
));
1763 ar8xxx_mib_cleanup(struct ar8216_priv
*priv
)
1765 if (!ar8xxx_has_mib_counters(priv
))
1768 cancel_delayed_work(&priv
->mib_work
);
1769 kfree(priv
->mib_stats
);
1773 ar8216_config_init(struct phy_device
*pdev
)
1775 struct ar8216_priv
*priv
= pdev
->priv
;
1776 struct net_device
*dev
= pdev
->attached_dev
;
1777 struct switch_dev
*swdev
;
1781 priv
= kzalloc(sizeof(struct ar8216_priv
), GFP_KERNEL
);
1786 priv
->mii_bus
= pdev
->bus
;
1789 ret
= ar8216_id_chip(priv
);
1793 if (pdev
->addr
!= 0) {
1794 if (ar8xxx_has_gige(priv
)) {
1795 pdev
->supported
|= SUPPORTED_1000baseT_Full
;
1796 pdev
->advertising
|= ADVERTISED_1000baseT_Full
;
1799 if (chip_is_ar8316(priv
)) {
1800 /* check if we're attaching to the switch twice */
1801 pdev
= pdev
->bus
->phy_map
[0];
1807 /* switch device has not been initialized, reuse priv */
1809 priv
->port4_phy
= true;
1816 /* switch device has been initialized, reinit */
1818 priv
->dev
.ports
= (AR8216_NUM_PORTS
- 1);
1819 priv
->initialized
= false;
1820 priv
->port4_phy
= true;
1821 ar8316_hw_init(priv
);
1829 if (ar8xxx_has_gige(priv
))
1830 pdev
->supported
= SUPPORTED_1000baseT_Full
;
1832 pdev
->supported
= SUPPORTED_100baseT_Full
;
1833 pdev
->advertising
= pdev
->supported
;
1835 mutex_init(&priv
->reg_mutex
);
1836 priv
->read
= ar8216_mii_read
;
1837 priv
->write
= ar8216_mii_write
;
1842 swdev
->cpu_port
= AR8216_PORT_CPU
;
1843 swdev
->ops
= &ar8216_sw_ops
;
1844 swdev
->ports
= AR8216_NUM_PORTS
;
1846 if (chip_is_ar8316(priv
)) {
1847 swdev
->name
= "Atheros AR8316";
1848 swdev
->vlans
= AR8X16_MAX_VLANS
;
1850 if (priv
->port4_phy
) {
1851 /* port 5 connected to the other mac, therefore unusable */
1852 swdev
->ports
= (AR8216_NUM_PORTS
- 1);
1854 } else if (chip_is_ar8236(priv
)) {
1855 swdev
->name
= "Atheros AR8236";
1856 swdev
->vlans
= AR8216_NUM_VLANS
;
1857 swdev
->ports
= AR8216_NUM_PORTS
;
1858 } else if (chip_is_ar8327(priv
)) {
1859 swdev
->name
= "Atheros AR8327";
1860 swdev
->vlans
= AR8X16_MAX_VLANS
;
1861 swdev
->ports
= AR8327_NUM_PORTS
;
1863 swdev
->name
= "Atheros AR8216";
1864 swdev
->vlans
= AR8216_NUM_VLANS
;
1867 ret
= ar8xxx_mib_init(priv
);
1871 ret
= register_switch(swdev
, pdev
->attached_dev
);
1873 goto err_cleanup_mib
;
1875 printk(KERN_INFO
"%s: %s switch driver attached.\n",
1876 pdev
->attached_dev
->name
, swdev
->name
);
1880 ret
= priv
->chip
->hw_init(priv
);
1882 goto err_unregister_switch
;
1884 ret
= ar8216_sw_reset_switch(&priv
->dev
);
1886 goto err_unregister_switch
;
1888 dev
->phy_ptr
= priv
;
1890 /* VID fixup only needed on ar8216 */
1891 if (chip_is_ar8216(priv
) && pdev
->addr
== 0) {
1892 dev
->priv_flags
|= IFF_NO_IP_ALIGN
;
1893 dev
->eth_mangle_rx
= ar8216_mangle_rx
;
1894 dev
->eth_mangle_tx
= ar8216_mangle_tx
;
1899 ar8xxx_mib_start(priv
);
1903 err_unregister_switch
:
1904 unregister_switch(&priv
->dev
);
1906 ar8xxx_mib_cleanup(priv
);
1914 ar8216_read_status(struct phy_device
*phydev
)
1916 struct ar8216_priv
*priv
= phydev
->priv
;
1917 struct switch_port_link link
;
1920 if (phydev
->addr
!= 0)
1921 return genphy_read_status(phydev
);
1923 ar8216_read_port_link(priv
, phydev
->addr
, &link
);
1924 phydev
->link
= !!link
.link
;
1928 switch (link
.speed
) {
1929 case SWITCH_PORT_SPEED_10
:
1930 phydev
->speed
= SPEED_10
;
1932 case SWITCH_PORT_SPEED_100
:
1933 phydev
->speed
= SPEED_100
;
1935 case SWITCH_PORT_SPEED_1000
:
1936 phydev
->speed
= SPEED_1000
;
1941 phydev
->duplex
= link
.duplex
? DUPLEX_FULL
: DUPLEX_HALF
;
1943 /* flush the address translation unit */
1944 mutex_lock(&priv
->reg_mutex
);
1945 ret
= priv
->chip
->atu_flush(priv
);
1946 mutex_unlock(&priv
->reg_mutex
);
1948 phydev
->state
= PHY_RUNNING
;
1949 netif_carrier_on(phydev
->attached_dev
);
1950 phydev
->adjust_link(phydev
->attached_dev
);
1956 ar8216_config_aneg(struct phy_device
*phydev
)
1958 if (phydev
->addr
== 0)
1961 return genphy_config_aneg(phydev
);
1965 ar8216_probe(struct phy_device
*pdev
)
1967 struct ar8216_priv
*priv
;
1970 priv
= kzalloc(sizeof(struct ar8216_priv
), GFP_KERNEL
);
1974 priv
->mii_bus
= pdev
->bus
;
1977 ret
= ar8216_id_chip(priv
);
1984 ar8216_detach(struct phy_device
*pdev
)
1986 struct net_device
*dev
= pdev
->attached_dev
;
1991 dev
->phy_ptr
= NULL
;
1992 dev
->priv_flags
&= ~IFF_NO_IP_ALIGN
;
1993 dev
->eth_mangle_rx
= NULL
;
1994 dev
->eth_mangle_tx
= NULL
;
1998 ar8216_remove(struct phy_device
*pdev
)
2000 struct ar8216_priv
*priv
= pdev
->priv
;
2007 if (pdev
->addr
== 0)
2008 unregister_switch(&priv
->dev
);
2010 ar8xxx_mib_cleanup(priv
);
2014 static struct phy_driver ar8216_driver
= {
2015 .phy_id
= 0x004d0000,
2016 .name
= "Atheros AR8216/AR8236/AR8316",
2017 .phy_id_mask
= 0xffff0000,
2018 .features
= PHY_BASIC_FEATURES
,
2019 .probe
= ar8216_probe
,
2020 .remove
= ar8216_remove
,
2021 .detach
= ar8216_detach
,
2022 .config_init
= &ar8216_config_init
,
2023 .config_aneg
= &ar8216_config_aneg
,
2024 .read_status
= &ar8216_read_status
,
2025 .driver
= { .owner
= THIS_MODULE
},
2031 return phy_driver_register(&ar8216_driver
);
2037 phy_driver_unregister(&ar8216_driver
);
2040 module_init(ar8216_init
);
2041 module_exit(ar8216_exit
);
2042 MODULE_LICENSE("GPL");