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>
36 int (*hardstart
)(struct sk_buff
*skb
, struct net_device
*dev
);
38 struct switch_dev dev
;
39 struct phy_device
*phy
;
40 u32 (*read
)(struct ar8216_priv
*priv
, int reg
);
41 void (*write
)(struct ar8216_priv
*priv
, int reg
, u32 val
);
43 /* all fields below are cleared on reset */
45 u8 vlan_id
[AR8216_NUM_VLANS
];
46 u8 vlan_table
[AR8216_NUM_VLANS
];
48 u16 pvid
[AR8216_NUM_PORTS
];
50 static struct switch_dev athdev
;
52 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
55 split_addr(u32 regaddr
, u16
*r1
, u16
*r2
, u16
*page
)
64 *page
= regaddr
& 0x1ff;
68 ar8216_mii_read(struct ar8216_priv
*priv
, int reg
)
70 struct phy_device
*phy
= priv
->phy
;
74 split_addr((u32
) reg
, &r1
, &r2
, &page
);
75 phy
->bus
->write(phy
->bus
, 0x18, 0, page
);
76 msleep(1); /* wait for the page switch to propagate */
77 lo
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
);
78 hi
= phy
->bus
->read(phy
->bus
, 0x10 | r2
, r1
+ 1);
80 return (hi
<< 16) | lo
;
84 ar8216_mii_write(struct ar8216_priv
*priv
, int reg
, u32 val
)
86 struct phy_device
*phy
= priv
->phy
;
90 split_addr((u32
) reg
, &r1
, &r2
, &r3
);
91 phy
->bus
->write(phy
->bus
, 0x18, 0, r3
);
92 msleep(1); /* wait for the page switch to propagate */
95 hi
= (u16
) (val
>> 16);
96 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
+ 1, hi
);
97 phy
->bus
->write(phy
->bus
, 0x10 | r2
, r1
, lo
);
101 ar8216_rmw(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
105 v
= priv
->read(priv
, reg
);
108 priv
->write(priv
, reg
, v
);
114 ar8216_set_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
115 struct switch_val
*val
)
117 struct ar8216_priv
*priv
= to_ar8216(dev
);
118 priv
->vlan
= !!val
->value
.i
;
123 ar8216_get_vlan(struct switch_dev
*dev
, const struct switch_attr
*attr
,
124 struct switch_val
*val
)
126 struct ar8216_priv
*priv
= to_ar8216(dev
);
127 val
->value
.i
= priv
->vlan
;
133 ar8216_set_pvid(struct switch_dev
*dev
, int port
, int vlan
)
135 struct ar8216_priv
*priv
= to_ar8216(dev
);
136 priv
->pvid
[port
] = vlan
;
141 ar8216_get_pvid(struct switch_dev
*dev
, int port
, int *vlan
)
143 struct ar8216_priv
*priv
= to_ar8216(dev
);
144 *vlan
= priv
->pvid
[port
];
149 ar8216_set_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
150 struct switch_val
*val
)
152 struct ar8216_priv
*priv
= to_ar8216(dev
);
153 priv
->vlan_id
[val
->port_vlan
] = val
->value
.i
;
158 ar8216_get_vid(struct switch_dev
*dev
, const struct switch_attr
*attr
,
159 struct switch_val
*val
)
161 struct ar8216_priv
*priv
= to_ar8216(dev
);
162 val
->value
.i
= priv
->vlan_id
[val
->port_vlan
];
168 ar8216_mangle_tx(struct sk_buff
*skb
, struct net_device
*dev
)
170 struct ar8216_priv
*priv
= dev
->phy_ptr
;
179 if (unlikely(skb_headroom(skb
) < 2)) {
180 if (pskb_expand_head(skb
, 2, 0, GFP_ATOMIC
) < 0)
184 buf
= skb_push(skb
, 2);
189 return priv
->hardstart(skb
, dev
);
192 dev_kfree_skb_any(skb
);
197 ar8216_mangle_rx(struct sk_buff
*skb
, int napi
)
199 struct ar8216_priv
*priv
;
200 struct net_device
*dev
;
212 /* don't strip the header if vlan mode is disabled */
216 /* strip header, get vlan id */
220 /* check for vlan header presence */
221 if ((buf
[12 + 2] != 0x81) || (buf
[13 + 2] != 0x00))
226 /* no need to fix up packets coming from a tagged source */
227 if (priv
->vlan_tagged
& (1 << port
))
230 /* lookup port vid from local table, the switch passes an invalid vlan id */
231 vlan
= priv
->pvid
[port
];
234 buf
[14 + 2] |= vlan
>> 8;
235 buf
[15 + 2] = vlan
& 0xff;
238 skb
->protocol
= eth_type_trans(skb
, skb
->dev
);
241 return netif_receive_skb(skb
);
243 return netif_rx(skb
);
246 /* no vlan? eat the packet! */
247 dev_kfree_skb_any(skb
);
252 ar8216_netif_rx(struct sk_buff
*skb
)
254 return ar8216_mangle_rx(skb
, 0);
258 ar8216_netif_receive_skb(struct sk_buff
*skb
)
260 return ar8216_mangle_rx(skb
, 1);
264 static struct switch_attr ar8216_globals
[] = {
266 .type
= SWITCH_TYPE_INT
,
267 .name
= "enable_vlan",
268 .description
= "Enable VLAN mode",
269 .set
= ar8216_set_vlan
,
270 .get
= ar8216_get_vlan
,
275 static struct switch_attr ar8216_port
[] = {
278 static struct switch_attr ar8216_vlan
[] = {
280 .type
= SWITCH_TYPE_INT
,
282 .description
= "VLAN ID",
283 .set
= ar8216_set_vid
,
284 .get
= ar8216_get_vid
,
291 ar8216_get_ports(struct switch_dev
*dev
, struct switch_val
*val
)
293 struct ar8216_priv
*priv
= to_ar8216(dev
);
294 u8 ports
= priv
->vlan_table
[val
->port_vlan
];
298 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
299 struct switch_port
*p
;
301 if (!(ports
& (1 << i
)))
304 p
= &val
->value
.ports
[val
->len
++];
306 if (priv
->vlan_tagged
& (1 << i
))
307 p
->flags
= (1 << SWITCH_PORT_FLAG_TAGGED
);
315 ar8216_set_ports(struct switch_dev
*dev
, struct switch_val
*val
)
317 struct ar8216_priv
*priv
= to_ar8216(dev
);
318 u8
*vt
= &priv
->vlan_table
[val
->port_vlan
];
322 for (i
= 0; i
< val
->len
; i
++) {
323 struct switch_port
*p
= &val
->value
.ports
[i
];
325 if (p
->flags
& (1 << SWITCH_PORT_FLAG_TAGGED
))
326 priv
->vlan_tagged
|= (1 << p
->id
);
328 priv
->vlan_tagged
&= ~(1 << p
->id
);
329 priv
->pvid
[p
->id
] = val
->port_vlan
;
331 /* make sure that an untagged port does not
332 * appear in other vlans */
333 for (j
= 0; j
< AR8216_NUM_VLANS
; j
++) {
334 if (j
== val
->port_vlan
)
336 priv
->vlan_table
[j
] &= ~(1 << p
->id
);
346 ar8216_wait_bit(struct ar8216_priv
*priv
, int reg
, u32 mask
, u32 val
)
350 while ((priv
->read(priv
, reg
) & mask
) != val
) {
351 if (timeout
-- <= 0) {
352 printk(KERN_ERR
"ar8216: timeout waiting for operation to complete\n");
360 ar8216_vtu_op(struct ar8216_priv
*priv
, u32 op
, u32 val
)
362 if (ar8216_wait_bit(priv
, AR8216_REG_VTU
, AR8216_VTU_ACTIVE
, 0))
364 if ((op
& AR8216_VTU_OP
) == AR8216_VTU_OP_LOAD
) {
365 val
&= AR8216_VTUDATA_MEMBER
;
366 val
|= AR8216_VTUDATA_VALID
;
367 priv
->write(priv
, AR8216_REG_VTU_DATA
, val
);
369 op
|= AR8216_VTU_ACTIVE
;
370 priv
->write(priv
, AR8216_REG_VTU
, op
);
374 ar8216_hw_apply(struct switch_dev
*dev
)
376 struct ar8216_priv
*priv
= to_ar8216(dev
);
377 u8 portmask
[AR8216_NUM_PORTS
];
380 /* flush all vlan translation unit entries */
381 ar8216_vtu_op(priv
, AR8216_VTU_OP_FLUSH
, 0);
383 memset(portmask
, 0, sizeof(portmask
));
385 /* calculate the port destination masks and load vlans
386 * into the vlan translation unit */
387 for (j
= 0; j
< AR8216_NUM_VLANS
; j
++) {
388 u8 vp
= priv
->vlan_table
[j
];
393 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
396 portmask
[i
] |= vp
& ~mask
;
399 if (!priv
->vlan_table
[j
])
404 (priv
->vlan_id
[j
] << AR8216_VTU_VID_S
),
405 priv
->vlan_table
[j
]);
409 * isolate all ports, but connect them to the cpu port */
410 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
411 if (i
== AR8216_PORT_CPU
)
414 portmask
[i
] = 1 << AR8216_PORT_CPU
;
415 portmask
[AR8216_PORT_CPU
] |= (1 << i
);
419 /* update the port destination mask registers and tag settings */
420 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
425 pvid
= priv
->vlan_id
[priv
->pvid
[i
]];
430 if (priv
->vlan
&& (priv
->vlan_tagged
& (1 << i
))) {
431 egress
= AR8216_OUT_ADD_VLAN
;
433 egress
= AR8216_OUT_STRIP_VLAN
;
435 ingress
= AR8216_IN_SECURE
;
437 ar8216_rmw(priv
, AR8216_REG_PORT_CTRL(i
),
438 AR8216_PORT_CTRL_LEARN
| AR8216_PORT_CTRL_VLAN_MODE
|
439 AR8216_PORT_CTRL_SINGLE_VLAN
| AR8216_PORT_CTRL_STATE
|
440 AR8216_PORT_CTRL_HEADER
| AR8216_PORT_CTRL_LEARN_LOCK
,
441 AR8216_PORT_CTRL_LEARN
|
442 (i
== AR8216_PORT_CPU
? AR8216_PORT_CTRL_HEADER
: 0) |
443 (egress
<< AR8216_PORT_CTRL_VLAN_MODE_S
) |
444 (AR8216_PORT_STATE_FORWARD
<< AR8216_PORT_CTRL_STATE_S
));
446 ar8216_rmw(priv
, AR8216_REG_PORT_VLAN(i
),
447 AR8216_PORT_VLAN_DEST_PORTS
| AR8216_PORT_VLAN_MODE
|
448 AR8216_PORT_VLAN_DEFAULT_ID
,
449 (portmask
[i
] << AR8216_PORT_VLAN_DEST_PORTS_S
) |
450 (ingress
<< AR8216_PORT_VLAN_MODE_S
) |
451 (pvid
<< AR8216_PORT_VLAN_DEFAULT_ID_S
));
458 ar8216_reset_switch(struct switch_dev
*dev
)
460 struct ar8216_priv
*priv
= to_ar8216(dev
);
463 memset(&priv
->vlan
, 0, sizeof(struct ar8216_priv
) -
464 offsetof(struct ar8216_priv
, vlan
));
465 for (i
= 0; i
< AR8216_NUM_VLANS
; i
++) {
466 priv
->vlan_id
[i
] = i
;
468 for (i
= 0; i
< AR8216_NUM_PORTS
; i
++) {
469 /* Enable port learning and tx */
470 priv
->write(priv
, AR8216_REG_PORT_CTRL(i
),
471 AR8216_PORT_CTRL_LEARN
|
472 (4 << AR8216_PORT_CTRL_STATE_S
));
474 priv
->write(priv
, AR8216_REG_PORT_VLAN(i
), 0);
476 /* Configure all PHYs */
477 if (i
== AR8216_PORT_CPU
) {
478 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
479 AR8216_PORT_STATUS_LINK_UP
|
480 AR8216_PORT_STATUS_SPEED
|
481 AR8216_PORT_STATUS_TXMAC
|
482 AR8216_PORT_STATUS_RXMAC
|
483 AR8216_PORT_STATUS_DUPLEX
);
485 priv
->write(priv
, AR8216_REG_PORT_STATUS(i
),
486 AR8216_PORT_STATUS_LINK_AUTO
);
489 /* XXX: undocumented magic from atheros, required! */
490 priv
->write(priv
, 0x38, 0xc000050e);
492 ar8216_rmw(priv
, AR8216_REG_GLOBAL_CTRL
,
493 AR8216_GCTRL_MTU
, 1518 + 8 + 2);
495 return ar8216_hw_apply(dev
);
499 ar8216_config_init(struct phy_device
*pdev
)
501 struct ar8216_priv
*priv
;
502 struct net_device
*dev
= pdev
->attached_dev
;
505 printk("%s: AR8216 PHY driver attached.\n", pdev
->attached_dev
->name
);
506 pdev
->supported
= ADVERTISED_100baseT_Full
;
507 pdev
->advertising
= ADVERTISED_100baseT_Full
;
509 priv
= kzalloc(sizeof(struct ar8216_priv
), GFP_KERNEL
);
514 priv
->read
= ar8216_mii_read
;
515 priv
->write
= ar8216_mii_write
;
516 memcpy(&priv
->dev
, &athdev
, sizeof(struct switch_dev
));
518 if ((ret
= register_switch(&priv
->dev
, pdev
->attached_dev
)) < 0) {
523 ret
= ar8216_reset_switch(&priv
->dev
);
529 priv
->hardstart
= dev
->hard_start_xmit
;
530 pdev
->netif_receive_skb
= ar8216_netif_receive_skb
;
531 pdev
->netif_rx
= ar8216_netif_rx
;
532 dev
->hard_start_xmit
= ar8216_mangle_tx
;
539 ar8216_read_status(struct phy_device
*phydev
)
541 struct ar8216_priv
*priv
= phydev
->priv
;
543 phydev
->speed
= SPEED_100
;
544 phydev
->duplex
= DUPLEX_FULL
;
545 phydev
->state
= PHY_UP
;
547 /* flush the address translation unit */
548 if (ar8216_wait_bit(priv
, AR8216_REG_ATU
, AR8216_ATU_ACTIVE
, 0))
551 priv
->write(priv
, AR8216_REG_ATU
, AR8216_ATU_OP_FLUSH
);
557 ar8216_config_aneg(struct phy_device
*phydev
)
563 ar8216_probe(struct phy_device
*pdev
)
565 struct ar8216_priv priv
;
571 val
= ar8216_mii_read(&priv
, AR8216_REG_CTRL
);
573 id
= (val
>> 8) & 0xff;
574 if ((id
!= 1) || (rev
!= 1))
581 ar8216_remove(struct phy_device
*pdev
)
583 struct ar8216_priv
*priv
= pdev
->priv
;
584 struct net_device
*dev
= pdev
->attached_dev
;
589 if (priv
->hardstart
&& dev
)
590 dev
->hard_start_xmit
= priv
->hardstart
;
591 unregister_switch(&priv
->dev
);
596 static struct switch_dev athdev
= {
597 .name
= "Atheros AR8216",
598 .cpu_port
= AR8216_PORT_CPU
,
599 .ports
= AR8216_NUM_PORTS
,
600 .vlans
= AR8216_NUM_VLANS
,
602 .attr
= ar8216_globals
,
603 .n_attr
= ARRAY_SIZE(ar8216_globals
),
607 .n_attr
= ARRAY_SIZE(ar8216_port
),
611 .n_attr
= ARRAY_SIZE(ar8216_vlan
),
613 .get_port_pvid
= ar8216_get_pvid
,
614 .set_port_pvid
= ar8216_set_pvid
,
615 .get_vlan_ports
= ar8216_get_ports
,
616 .set_vlan_ports
= ar8216_set_ports
,
617 .apply_config
= ar8216_hw_apply
,
618 .reset_switch
= ar8216_reset_switch
,
621 static struct phy_driver ar8216_driver
= {
622 .name
= "Atheros AR8216",
623 .features
= PHY_BASIC_FEATURES
,
624 .probe
= ar8216_probe
,
625 .remove
= ar8216_remove
,
626 .config_init
= &ar8216_config_init
,
627 .config_aneg
= &ar8216_config_aneg
,
628 .read_status
= &ar8216_read_status
,
629 .driver
= { .owner
= THIS_MODULE
},
635 return phy_driver_register(&ar8216_driver
);
641 phy_driver_unregister(&ar8216_driver
);
644 module_init(ar8216_init
);
645 module_exit(ar8216_exit
);
646 MODULE_LICENSE("GPL");