2 * ar8216.c: AR8216 switch driver
4 * Copyright (C) 2009 Felix Fietkau <nbd@openwrt.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/list.h>
21 #include <linux/if_ether.h>
22 #include <linux/skbuff.h>
23 #include <linux/netdevice.h>
24 #include <linux/netlink.h>
25 #include <linux/bitops.h>
26 #include <net/genetlink.h>
27 #include <linux/switch.h>
28 #include <linux/delay.h>
29 #include <linux/phy.h>
30 #include <linux/netdevice.h>
31 #include <linux/etherdevice.h>
34 /* size of the vlan table */
35 #define AR8X16_MAX_VLANS 128
36 #define AR8X16_PROBE_RETRIES 10
39 struct switch_dev dev
;
40 struct phy_device
*phy
;
41 u32 (*read
)(struct ar8216_priv
*priv
, int reg
);
42 void (*write
)(struct ar8216_priv
*priv
, int reg
, u32 val
);
43 const struct net_device_ops
*ndo_old
;
44 struct net_device_ops ndo
;
45 struct mutex reg_mutex
;
48 /* all fields below are cleared on reset */
50 u16 vlan_id
[AR8X16_MAX_VLANS
];
51 u8 vlan_table
[AR8X16_MAX_VLANS
];
53 u16 pvid
[AR8216_NUM_PORTS
];
56 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
59 split_addr(u32 regaddr
, u16
*r1
, u16
*r2
, u16
*page
)
68 *page
= regaddr
& 0x1ff;
72 ar8216_mii_read(struct ar8216_priv
*priv
, int reg
)
74 struct phy_device
*phy
= priv
->phy
;
78 split_addr((u32
) reg
, &r1
, &r2
, &page
);
79 phy
->bus
->write(phy
->bus
, 0x18, 0, page
);
80 msleep(1); /* wait for the page switch to propagate */
81 lo
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
);
82 hi
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
+ 1);
84 return (hi
<< 16) | lo
;
88 ar8216_mii_write(struct ar8216_priv
*priv
, int reg
, u32 val
)
90 struct phy_device
*phy
= priv
->phy
;
94 split_addr((u32
) reg
, &r1
, &r2
, &r3
);
95 phy
->bus
->write(phy
->bus
, 0x18, 0, r3
);
96 msleep(1); /* wait for the page switch to propagate */
99 hi
= (u16
) (val
>> 16);
100 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
+ 1, hi
);
101 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
, lo
);
105 ar8216_rmw(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
109 v
= priv
->read(priv
, reg
);
112 priv
->write(priv
, reg
, v
);
118 ar8216_id_chip(struct ar8216_priv
*priv
)
124 val
= ar8216_mii_read(priv
, AR8216_REG_CTRL
);
128 id
= val
& (AR8216_CTRL_REVISION
| AR8216_CTRL_VERSION
);
129 for (i
= 0; i
< AR8X16_PROBE_RETRIES
; i
++) {
132 val
= ar8216_mii_read(priv
, AR8216_REG_CTRL
);
136 t
= val
& (AR8216_CTRL_REVISION
| AR8216_CTRL_VERSION
);
148 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
149 (int)(id
>> AR8216_CTRL_VERSION_S
),
150 (int)(id
& AR8216_CTRL_REVISION
),
151 priv
->phy
->bus
->read(priv
->phy
->bus
, priv
->phy
->addr
, 2),
152 priv
->phy
->bus
->read(priv
->phy
->bus
, priv
->phy
->addr
, 3));
159 ar8216_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
160 struct switch_val
*val
)
162 struct ar8216_priv
*priv
= to_ar8216(dev
);
163 priv
->vlan
= !!val
->value
.i
;
168 ar8216_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
169 struct switch_val
*val
)
171 struct ar8216_priv
*priv
= to_ar8216(dev
);
172 val
->value
.i
= priv
->vlan
;
178 ar8216_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
180 struct ar8216_priv
*priv
= to_ar8216(dev
);
182 /* make sure no invalid PVIDs get set */
184 if (vlan
>= dev
->vlans
)
187 priv
->pvid
[port
] = vlan
;
192 ar8216_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
194 struct ar8216_priv
*priv
= to_ar8216(dev
);
195 *vlan
= priv
->pvid
[port
];
200 ar8216_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
201 struct switch_val
*val
)
203 struct ar8216_priv
*priv
= to_ar8216(dev
);
204 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
209 ar8216_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
210 struct switch_val
*val
)
212 struct ar8216_priv
*priv
= to_ar8216(dev
);
213 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
219 ar8216_mangle_tx(struct sk_buff
*skb
, struct net_device
*dev
)
221 struct ar8216_priv
*priv
= dev
->phy_ptr
;
230 if (unlikely(skb_headroom(skb
) < 2)) {
231 if (pskb_expand_head(skb
, 2, 0, GFP_ATOMIC
) < 0)
235 buf
= skb_push(skb
, 2);
240 return priv
->ndo_old
->ndo_start_xmit(skb
, dev
);
243 dev_kfree_skb_any(skb
);
248 ar8216_mangle_rx(struct sk_buff
*skb
, int napi
)
250 struct ar8216_priv
*priv
;
251 struct net_device
*dev
;
263 /* don't strip the header if vlan mode is disabled */
267 /* strip header, get vlan id */
271 /* check for vlan header presence */
272 if ((buf
[12 + 2] != 0x81) || (buf
[13 + 2] != 0x00))
277 /* no need to fix up packets coming from a tagged source */
278 if (priv
->vlan_tagged
& (1 << port
))
281 /* lookup port vid from local table, the switch passes an invalid vlan id */
282 vlan
= priv
->vlan_id
[priv
->pvid
[port
]];
285 buf
[14 + 2] |= vlan
>> 8;
286 buf
[15 + 2] = vlan
& 0xff;
289 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
292 return netif_receive_skb(skb
);
294 return netif_rx(skb
);
297 /* no vlan? eat the packet! */
298 dev_kfree_skb_any(skb
);
303 ar8216_netif_rx(struct sk_buff
*skb
)
305 return ar8216_mangle_rx(skb
, 0);
309 ar8216_netif_receive_skb(struct sk_buff
*skb
)
311 return ar8216_mangle_rx(skb
, 1);
315 static struct switch_attr ar8216_globals
[] = {
317 .type
= SWITCH_TYPE_INT
,
318 .name
= "enable_vlan",
319 .description
= "Enable VLAN mode",
320 .set
= ar8216_set_vlan
,
321 .get
= ar8216_get_vlan
,
326 static struct switch_attr ar8216_port
[] = {
329 static struct switch_attr ar8216_vlan
[] = {
331 .type
= SWITCH_TYPE_INT
,
333 .description
= "VLAN ID (0-4094)",
334 .set
= ar8216_set_vid
,
335 .get
= ar8216_get_vid
,
342 ar8216_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
344 struct ar8216_priv
*priv
= to_ar8216(dev
);
345 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
349 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
350 struct switch_port
*p
;
352 if (!(ports
& (1 << i
)))
355 p
= &val
->value
.ports
[val
->len
++];
357 if (priv
->vlan_tagged
& (1 << i
))
358 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
366 ar8216_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
368 struct ar8216_priv
*priv
= to_ar8216(dev
);
369 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
373 for (i
= 0; i
< val
->len
; i
++) {
374 struct switch_port
*p
= &val
->value
.ports
[i
];
376 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
377 priv
->vlan_tagged
|= (1 << p
->id
);
379 priv
->vlan_tagged
&= ~(1 << p
->id
);
380 priv
->pvid
[p
->id
] = val
->port_vlan
;
382 /* make sure that an untagged port does not
383 * appear in other vlans */
384 for (j
= 0; j
< AR8X16_MAX_VLANS
; j
++) {
385 if (j
== val
->port_vlan
)
387 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
397 ar8216_wait_bit(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
401 while ((priv
->read(priv
, reg
) & mask
) != val
) {
402 if (timeout
-- <= 0) {
403 printk(KERN_ERR
"ar8216: timeout waiting for operation to complete\n");
411 ar8216_vtu_op(struct ar8216_priv
*priv
, u32 op
, u32 val
)
413 if (ar8216_wait_bit(priv
, AR8216_REG_VTU
, AR8216_VTU_ACTIVE
, 0))
415 if ((op
& AR8216_VTU_OP
) == AR8216_VTU_OP_LOAD
) {
416 val
&= AR8216_VTUDATA_MEMBER
;
417 val
|= AR8216_VTUDATA_VALID
;
418 priv
->write(priv
, AR8216_REG_VTU_DATA
, val
);
420 op
|= AR8216_VTU_ACTIVE
;
421 priv
->write(priv
, AR8216_REG_VTU
, op
);
425 ar8216_hw_apply(struct switch_dev
*dev
)
427 struct ar8216_priv
*priv
= to_ar8216(dev
);
428 u8 portmask
[AR8216_NUM_PORTS
];
431 mutex_lock(&priv
->reg_mutex
);
432 /* flush all vlan translation unit entries */
433 ar8216_vtu_op(priv
, AR8216_VTU_OP_FLUSH
, 0);
435 memset(portmask
, 0, sizeof(portmask
));
437 /* calculate the port destination masks and load vlans
438 * into the vlan translation unit */
439 for (j
= 0; j
< AR8X16_MAX_VLANS
; j
++) {
440 u8 vp
= priv
->vlan_table
[j
];
445 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
448 portmask
[i
] |= vp
& ~mask
;
453 (priv
->vlan_id
[j
] << AR8216_VTU_VID_S
),
454 priv
->vlan_table
[j
]);
458 * isolate all ports, but connect them to the cpu port */
459 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
460 if (i
== AR8216_PORT_CPU
)
463 portmask
[i
] = 1 << AR8216_PORT_CPU
;
464 portmask
[AR8216_PORT_CPU
] |= (1 << i
);
468 /* update the port destination mask registers and tag settings */
469 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
474 pvid
= priv
->vlan_id
[priv
->pvid
[i
]];
479 if (priv
->vlan
&& (priv
->vlan_tagged
& (1 << i
))) {
480 egress
= AR8216_OUT_ADD_VLAN
;
482 egress
= AR8216_OUT_STRIP_VLAN
;
485 ingress
= AR8216_IN_SECURE
;
487 ingress
= AR8216_IN_PORT_ONLY
;
490 ar8216_rmw(priv
, AR8216_REG_PORT_CTRL(i
),
491 AR8216_PORT_CTRL_LEARN
| AR8216_PORT_CTRL_VLAN_MODE
|
492 AR8216_PORT_CTRL_SINGLE_VLAN
| AR8216_PORT_CTRL_STATE
|
493 AR8216_PORT_CTRL_HEADER
| AR8216_PORT_CTRL_LEARN_LOCK
,
494 AR8216_PORT_CTRL_LEARN
|
495 (priv
->vlan
&& i
== AR8216_PORT_CPU
&& (priv
->chip
== AR8216
) ?
496 AR8216_PORT_CTRL_HEADER
: 0) |
497 (egress
<< AR8216_PORT_CTRL_VLAN_MODE_S
) |
498 (AR8216_PORT_STATE_FORWARD
<< AR8216_PORT_CTRL_STATE_S
));
500 ar8216_rmw(priv
, AR8216_REG_PORT_VLAN(i
),
501 AR8216_PORT_VLAN_DEST_PORTS
| AR8216_PORT_VLAN_MODE
|
502 AR8216_PORT_VLAN_DEFAULT_ID
,
503 (portmask
[i
] << AR8216_PORT_VLAN_DEST_PORTS_S
) |
504 (ingress
<< AR8216_PORT_VLAN_MODE_S
) |
505 (pvid
<< AR8216_PORT_VLAN_DEFAULT_ID_S
));
507 mutex_unlock(&priv
->reg_mutex
);
512 ar8316_hw_init(struct ar8216_priv
*priv
) {
513 static int initialized
;
521 val
= priv
->read(priv
, 0x8);
523 if (priv
->phy
->interface
== PHY_INTERFACE_MODE_RGMII
) {
524 /* value taken from Ubiquiti RouterStation Pro */
525 if (val
== 0x81461bea) {
526 /* switch already intialized by bootloader */
530 priv
->write(priv
, 0x8, 0x81461bea);
531 } else if (priv
->phy
->interface
== PHY_INTERFACE_MODE_GMII
) {
532 /* value taken from AVM Fritz!Box 7390 sources */
533 if (val
== 0x010e5b71) {
534 /* switch already initialized by bootloader */
538 priv
->write(priv
, 0x8, 0x010e5b71);
540 /* no known value for phy interface */
541 printk(KERN_ERR
"ar8316: unsupported mii mode: %d.\n",
542 priv
->phy
->interface
);
546 /* standard atheros magic */
547 priv
->write(priv
, 0x38, 0xc000050e);
549 /* Initialize the ports */
550 bus
= priv
->phy
->bus
;
551 for (i
= 0; i
< 5; i
++) {
553 priv
->phy
->interface
== PHY_INTERFACE_MODE_RGMII
) {
554 /* work around for phy4 rgmii mode */
555 bus
->write(bus
, i
, MII_ATH_DBG_ADDR
, 0x12);
556 bus
->write(bus
, i
, MII_ATH_DBG_DATA
, 0x480c);
558 bus
->write(bus
, i
, MII_ATH_DBG_ADDR
, 0x0);
559 bus
->write(bus
, i
, MII_ATH_DBG_DATA
, 0x824e);
561 bus
->write(bus
, i
, MII_ATH_DBG_ADDR
, 0x5);
562 bus
->write(bus
, i
, MII_ATH_DBG_DATA
, 0x3d47);
566 /* initialize the port itself */
567 bus
->write(bus
, i
, MII_ADVERTISE
,
568 ADVERTISE_ALL
| ADVERTISE_PAUSE_CAP
| ADVERTISE_PAUSE_ASYM
);
569 bus
->write(bus
, i
, MII_CTRL1000
, ADVERTISE_1000FULL
);
570 bus
->write(bus
, i
, MII_BMCR
, BMCR_RESET
| BMCR_ANENABLE
);
578 ar8216_reset_switch(struct switch_dev
*dev
)
580 struct ar8216_priv
*priv
= to_ar8216(dev
);
583 mutex_lock(&priv
->reg_mutex
);
584 memset(&priv
->vlan
, 0, sizeof(struct ar8216_priv
) -
585 offsetof(struct ar8216_priv
, vlan
));
586 for (i
= 0; i
< AR8X16_MAX_VLANS
; i
++) {
587 priv
->vlan_id
[i
] = i
;
589 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
590 /* Enable port learning and tx */
591 priv
->write(priv
, AR8216_REG_PORT_CTRL(i
),
592 AR8216_PORT_CTRL_LEARN
|
593 (4 << AR8216_PORT_CTRL_STATE_S
));
595 priv
->write(priv
, AR8216_REG_PORT_VLAN(i
), 0);
597 /* Configure all PHYs */
598 if (i
== AR8216_PORT_CPU
) {
599 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
600 AR8216_PORT_STATUS_LINK_UP
|
601 ((priv
->chip
== AR8316
) ?
602 AR8216_PORT_SPEED_1000M
: AR8216_PORT_SPEED_100M
) |
603 AR8216_PORT_STATUS_TXMAC
|
604 AR8216_PORT_STATUS_RXMAC
|
605 ((priv
->chip
== AR8316
) ? AR8216_PORT_STATUS_RXFLOW
: 0) |
606 ((priv
->chip
== AR8316
) ? AR8216_PORT_STATUS_TXFLOW
: 0) |
607 AR8216_PORT_STATUS_DUPLEX
);
609 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
610 AR8216_PORT_STATUS_LINK_AUTO
);
613 /* XXX: undocumented magic from atheros, required! */
614 priv
->write(priv
, 0x38, 0xc000050e);
616 if (priv
->chip
== AR8216
) {
617 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
618 AR8216_GCTRL_MTU
, 1518 + 8 + 2);
619 } else if (priv
->chip
== AR8316
) {
620 /* enable jumbo frames */
621 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
622 AR8316_GCTRL_MTU
, 9018 + 8 + 2);
625 if (priv
->chip
== AR8316
) {
626 /* enable cpu port to receive multicast and broadcast frames */
627 priv
->write(priv
, AR8216_REG_FLOOD_MASK
, 0x003f003f);
629 mutex_unlock(&priv
->reg_mutex
);
630 return ar8216_hw_apply(dev
);
634 static const struct switch_dev_ops ar8216_ops
= {
636 .attr
= ar8216_globals
,
637 .n_attr
= ARRAY_SIZE(ar8216_globals
),
641 .n_attr
= ARRAY_SIZE(ar8216_port
),
645 .n_attr
= ARRAY_SIZE(ar8216_vlan
),
647 .get_port_pvid
= ar8216_get_pvid
,
648 .set_port_pvid
= ar8216_set_pvid
,
649 .get_vlan_ports
= ar8216_get_ports
,
650 .set_vlan_ports
= ar8216_set_ports
,
651 .apply_config
= ar8216_hw_apply
,
652 .reset_switch
= ar8216_reset_switch
,
656 ar8216_config_init(struct phy_device
*pdev
)
658 struct ar8216_priv
*priv
;
659 struct net_device
*dev
= pdev
->attached_dev
;
660 struct switch_dev
*swdev
;
663 priv
= kzalloc(sizeof(struct ar8216_priv
), GFP_KERNEL
);
669 priv
->chip
= ar8216_id_chip(priv
);
672 printk(KERN_INFO
"%s: AR%d switch driver attached.\n",
673 pdev
->attached_dev
->name
, priv
->chip
);
676 if (pdev
->addr
!= 0) {
677 if (priv
->chip
== AR8316
) {
678 pdev
->supported
|= SUPPORTED_1000baseT_Full
;
679 pdev
->advertising
|= ADVERTISED_1000baseT_Full
;
685 pdev
->supported
= priv
->chip
== AR8316
?
686 SUPPORTED_1000baseT_Full
: SUPPORTED_100baseT_Full
;
687 pdev
->advertising
= pdev
->supported
;
689 mutex_init(&priv
->reg_mutex
);
690 priv
->read
= ar8216_mii_read
;
691 priv
->write
= ar8216_mii_write
;
696 swdev
->cpu_port
= AR8216_PORT_CPU
;
697 swdev
->ops
= &ar8216_ops
;
699 if (priv
->chip
== AR8316
) {
700 swdev
->name
= "Atheros AR8316";
701 swdev
->vlans
= AR8X16_MAX_VLANS
;
702 /* port 5 connected to the other mac, therefore unusable */
703 swdev
->ports
= (AR8216_NUM_PORTS
- 1);
705 swdev
->name
= "Atheros AR8216";
706 swdev
->vlans
= AR8216_NUM_VLANS
;
707 swdev
->ports
= AR8216_NUM_PORTS
;
710 if ((ret
= register_switch(&priv
->dev
, pdev
->attached_dev
)) < 0) {
715 if (priv
->chip
== AR8316
) {
716 ret
= ar8316_hw_init(priv
);
723 ret
= ar8216_reset_switch(&priv
->dev
);
731 /* VID fixup only needed on ar8216 */
732 if (pdev
->addr
== 0 && priv
->chip
== AR8216
) {
734 pdev
->netif_receive_skb
= ar8216_netif_receive_skb
;
735 pdev
->netif_rx
= ar8216_netif_rx
;
736 priv
->ndo_old
= dev
->netdev_ops
;
737 memcpy(&priv
->ndo
, priv
->ndo_old
, sizeof(struct net_device_ops
));
738 priv
->ndo
.ndo_start_xmit
= ar8216_mangle_tx
;
739 dev
->netdev_ops
= &priv
->ndo
;
747 ar8216_read_status(struct phy_device
*phydev
)
749 struct ar8216_priv
*priv
= phydev
->priv
;
751 if (phydev
->addr
!= 0) {
752 return genphy_read_status(phydev
);
755 phydev
->speed
= priv
->chip
== AR8316
? SPEED_1000
: SPEED_100
;
756 phydev
->duplex
= DUPLEX_FULL
;
759 /* flush the address translation unit */
760 mutex_lock(&priv
->reg_mutex
);
761 ret
= ar8216_wait_bit(priv
, AR8216_REG_ATU
, AR8216_ATU_ACTIVE
, 0);
764 priv
->write(priv
, AR8216_REG_ATU
, AR8216_ATU_OP_FLUSH
);
767 mutex_unlock(&priv
->reg_mutex
);
769 phydev
->state
= PHY_RUNNING
;
770 netif_carrier_on(phydev
->attached_dev
);
771 phydev
->adjust_link(phydev
->attached_dev
);
777 ar8216_config_aneg(struct phy_device
*phydev
)
779 if (phydev
->addr
== 0)
782 return genphy_config_aneg(phydev
);
786 ar8216_probe(struct phy_device
*pdev
)
788 struct ar8216_priv priv
;
792 chip
= ar8216_id_chip(&priv
);
800 ar8216_remove(struct phy_device
*pdev
)
802 struct ar8216_priv
*priv
= pdev
->priv
;
803 struct net_device
*dev
= pdev
->attached_dev
;
808 if (priv
->ndo_old
&& dev
)
809 dev
->netdev_ops
= priv
->ndo_old
;
811 unregister_switch(&priv
->dev
);
815 static struct phy_driver ar8216_driver
= {
816 .phy_id
= 0x004d0000,
817 .name
= "Atheros AR8216/AR8316",
818 .phy_id_mask
= 0xffff0000,
819 .features
= PHY_BASIC_FEATURES
,
820 .probe
= ar8216_probe
,
821 .remove
= ar8216_remove
,
822 .config_init
= &ar8216_config_init
,
823 .config_aneg
= &ar8216_config_aneg
,
824 .read_status
= &ar8216_read_status
,
825 .driver
= { .owner
= THIS_MODULE
},
831 return phy_driver_register(&ar8216_driver
);
837 phy_driver_unregister(&ar8216_driver
);
840 module_init(ar8216_init
);
841 module_exit(ar8216_exit
);
842 MODULE_LICENSE("GPL");