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 struct switch_dev dev;
37 struct phy_device *phy;
38 u32 (*read)(struct ar8216_priv *priv, int reg);
39 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
40 const struct net_device_ops *ndo_old;
41 struct net_device_ops ndo;
43 /* all fields below are cleared on reset */
45 u16 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);
137 /* make sure no invalid PVIDs get set */
139 if (vlan >= AR8216_NUM_VLANS)
142 priv->pvid[port] = vlan;
147 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
149 struct ar8216_priv *priv = to_ar8216(dev);
150 *vlan = priv->pvid[port];
155 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
156 struct switch_val *val)
158 struct ar8216_priv *priv = to_ar8216(dev);
159 priv->vlan_id[val->port_vlan] = val->value.i;
164 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
165 struct switch_val *val)
167 struct ar8216_priv *priv = to_ar8216(dev);
168 val->value.i = priv->vlan_id[val->port_vlan];
174 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
176 struct ar8216_priv *priv = dev->phy_ptr;
185 if (unlikely(skb_headroom(skb) < 2)) {
186 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
190 buf = skb_push(skb, 2);
195 return priv->ndo_old->ndo_start_xmit(skb, dev);
198 dev_kfree_skb_any(skb);
203 ar8216_mangle_rx(struct sk_buff *skb, int napi)
205 struct ar8216_priv *priv;
206 struct net_device *dev;
218 /* don't strip the header if vlan mode is disabled */
222 /* strip header, get vlan id */
226 /* check for vlan header presence */
227 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
232 /* no need to fix up packets coming from a tagged source */
233 if (priv->vlan_tagged & (1 << port))
236 /* lookup port vid from local table, the switch passes an invalid vlan id */
237 vlan = priv->vlan_id[priv->pvid[port]];
240 buf[14 + 2] |= vlan >> 8;
241 buf[15 + 2] = vlan & 0xff;
244 skb->protocol = eth_type_trans(skb, skb->dev);
247 return netif_receive_skb(skb);
249 return netif_rx(skb);
252 /* no vlan? eat the packet! */
253 dev_kfree_skb_any(skb);
258 ar8216_netif_rx(struct sk_buff *skb)
260 return ar8216_mangle_rx(skb, 0);
264 ar8216_netif_receive_skb(struct sk_buff *skb)
266 return ar8216_mangle_rx(skb, 1);
270 static struct switch_attr ar8216_globals[] = {
272 .type = SWITCH_TYPE_INT,
273 .name = "enable_vlan",
274 .description = "Enable VLAN mode",
275 .set = ar8216_set_vlan,
276 .get = ar8216_get_vlan,
281 static struct switch_attr ar8216_port[] = {
284 static struct switch_attr ar8216_vlan[] = {
286 .type = SWITCH_TYPE_INT,
288 .description = "VLAN ID",
289 .set = ar8216_set_vid,
290 .get = ar8216_get_vid,
297 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
299 struct ar8216_priv *priv = to_ar8216(dev);
300 u8 ports = priv->vlan_table[val->port_vlan];
304 for (i = 0; i < AR8216_NUM_PORTS; i++) {
305 struct switch_port *p;
307 if (!(ports & (1 << i)))
310 p = &val->value.ports[val->len++];
312 if (priv->vlan_tagged & (1 << i))
313 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
321 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
323 struct ar8216_priv *priv = to_ar8216(dev);
324 u8 *vt = &priv->vlan_table[val->port_vlan];
328 for (i = 0; i < val->len; i++) {
329 struct switch_port *p = &val->value.ports[i];
331 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
332 priv->vlan_tagged |= (1 << p->id);
334 priv->vlan_tagged &= ~(1 << p->id);
335 priv->pvid[p->id] = val->port_vlan;
337 /* make sure that an untagged port does not
338 * appear in other vlans */
339 for (j = 0; j < AR8216_NUM_VLANS; j++) {
340 if (j == val->port_vlan)
342 priv->vlan_table[j] &= ~(1 << p->id);
352 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
356 while ((priv->read(priv, reg) & mask) != val) {
357 if (timeout-- <= 0) {
358 printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
366 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
368 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
370 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
371 val &= AR8216_VTUDATA_MEMBER;
372 val |= AR8216_VTUDATA_VALID;
373 priv->write(priv, AR8216_REG_VTU_DATA, val);
375 op |= AR8216_VTU_ACTIVE;
376 priv->write(priv, AR8216_REG_VTU, op);
380 ar8216_hw_apply(struct switch_dev *dev)
382 struct ar8216_priv *priv = to_ar8216(dev);
383 u8 portmask[AR8216_NUM_PORTS];
386 /* flush all vlan translation unit entries */
387 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
389 memset(portmask, 0, sizeof(portmask));
391 /* calculate the port destination masks and load vlans
392 * into the vlan translation unit */
393 for (j = 0; j < AR8216_NUM_VLANS; j++) {
394 u8 vp = priv->vlan_table[j];
399 for (i = 0; i < AR8216_NUM_PORTS; i++) {
402 portmask[i] |= vp & ~mask;
407 (priv->vlan_id[j] << AR8216_VTU_VID_S),
408 priv->vlan_table[j]);
412 * isolate all ports, but connect them to the cpu port */
413 for (i = 0; i < AR8216_NUM_PORTS; i++) {
414 if (i == AR8216_PORT_CPU)
417 portmask[i] = 1 << AR8216_PORT_CPU;
418 portmask[AR8216_PORT_CPU] |= (1 << i);
422 /* update the port destination mask registers and tag settings */
423 for (i = 0; i < AR8216_NUM_PORTS; i++) {
428 pvid = priv->vlan_id[priv->pvid[i]];
433 if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
434 egress = AR8216_OUT_ADD_VLAN;
436 egress = AR8216_OUT_STRIP_VLAN;
439 ingress = AR8216_IN_SECURE;
441 ingress = AR8216_IN_PORT_ONLY;
444 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
445 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
446 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
447 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
448 AR8216_PORT_CTRL_LEARN |
449 (priv->vlan && i == AR8216_PORT_CPU ?
450 AR8216_PORT_CTRL_HEADER : 0) |
451 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
452 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
454 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
455 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
456 AR8216_PORT_VLAN_DEFAULT_ID,
457 (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
458 (ingress << AR8216_PORT_VLAN_MODE_S) |
459 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
466 ar8216_reset_switch(struct switch_dev *dev)
468 struct ar8216_priv *priv = to_ar8216(dev);
471 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
472 offsetof(struct ar8216_priv, vlan));
473 for (i = 0; i < AR8216_NUM_VLANS; i++) {
474 priv->vlan_id[i] = i;
476 for (i = 0; i < AR8216_NUM_PORTS; i++) {
477 /* Enable port learning and tx */
478 priv->write(priv, AR8216_REG_PORT_CTRL(i),
479 AR8216_PORT_CTRL_LEARN |
480 (4 << AR8216_PORT_CTRL_STATE_S));
482 priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
484 /* Configure all PHYs */
485 if (i == AR8216_PORT_CPU) {
486 priv->write(priv, AR8216_REG_PORT_STATUS(i),
487 AR8216_PORT_STATUS_LINK_UP |
488 AR8216_PORT_SPEED_100M |
489 AR8216_PORT_STATUS_TXMAC |
490 AR8216_PORT_STATUS_RXMAC |
491 AR8216_PORT_STATUS_DUPLEX);
493 priv->write(priv, AR8216_REG_PORT_STATUS(i),
494 AR8216_PORT_STATUS_LINK_AUTO);
497 /* XXX: undocumented magic from atheros, required! */
498 priv->write(priv, 0x38, 0xc000050e);
500 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
501 AR8216_GCTRL_MTU, 1518 + 8 + 2);
503 return ar8216_hw_apply(dev);
507 ar8216_config_init(struct phy_device *pdev)
509 struct ar8216_priv *priv;
510 struct net_device *dev = pdev->attached_dev;
513 printk("%s: AR8216 PHY driver attached.\n", pdev->attached_dev->name);
514 pdev->supported = ADVERTISED_100baseT_Full;
515 pdev->advertising = ADVERTISED_100baseT_Full;
517 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
522 priv->read = ar8216_mii_read;
523 priv->write = ar8216_mii_write;
524 memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
526 if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
531 ret = ar8216_reset_switch(&priv->dev);
537 pdev->netif_receive_skb = ar8216_netif_receive_skb;
538 pdev->netif_rx = ar8216_netif_rx;
540 priv->ndo_old = dev->netdev_ops;
541 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
542 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
543 dev->netdev_ops = &priv->ndo;
550 ar8216_read_status(struct phy_device *phydev)
552 struct ar8216_priv *priv = phydev->priv;
554 phydev->speed = SPEED_100;
555 phydev->duplex = DUPLEX_FULL;
558 /* flush the address translation unit */
559 if (ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0))
562 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
564 phydev->state = PHY_RUNNING;
565 netif_carrier_on(phydev->attached_dev);
566 phydev->adjust_link(phydev->attached_dev);
572 ar8216_config_aneg(struct phy_device *phydev)
578 ar8216_probe(struct phy_device *pdev)
580 struct ar8216_priv priv;
586 val = ar8216_mii_read(&priv, AR8216_REG_CTRL);
587 rev = val & AR8216_CTRL_REVISION;
588 id = (val & AR8216_CTRL_VERSION) >> AR8216_CTRL_VERSION_S;
589 if ((id != 1) || (rev != 1))
596 ar8216_remove(struct phy_device *pdev)
598 struct ar8216_priv *priv = pdev->priv;
599 struct net_device *dev = pdev->attached_dev;
604 if (priv->ndo_old && dev)
605 dev->netdev_ops = priv->ndo_old;
606 unregister_switch(&priv->dev);
611 static struct switch_dev athdev = {
612 .name = "Atheros AR8216",
613 .cpu_port = AR8216_PORT_CPU,
614 .ports = AR8216_NUM_PORTS,
615 .vlans = AR8216_NUM_VLANS,
617 .attr = ar8216_globals,
618 .n_attr = ARRAY_SIZE(ar8216_globals),
622 .n_attr = ARRAY_SIZE(ar8216_port),
626 .n_attr = ARRAY_SIZE(ar8216_vlan),
628 .get_port_pvid = ar8216_get_pvid,
629 .set_port_pvid = ar8216_set_pvid,
630 .get_vlan_ports = ar8216_get_ports,
631 .set_vlan_ports = ar8216_set_ports,
632 .apply_config = ar8216_hw_apply,
633 .reset_switch = ar8216_reset_switch,
636 static struct phy_driver ar8216_driver = {
637 .name = "Atheros AR8216",
638 .features = PHY_BASIC_FEATURES,
639 .probe = ar8216_probe,
640 .remove = ar8216_remove,
641 .config_init = &ar8216_config_init,
642 .config_aneg = &ar8216_config_aneg,
643 .read_status = &ar8216_read_status,
644 .driver = { .owner = THIS_MODULE },
650 return phy_driver_register(&ar8216_driver);
656 phy_driver_unregister(&ar8216_driver);
659 module_init(ar8216_init);
660 module_exit(ar8216_exit);
661 MODULE_LICENSE("GPL");