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];
55 static struct switch_dev athdev;
57 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
60 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
69 *page = regaddr & 0x1ff;
73 ar8216_mii_read(struct ar8216_priv *priv, int reg)
75 struct phy_device *phy = priv->phy;
79 split_addr((u32) reg, &r1, &r2, &page);
80 phy->bus->write(phy->bus, 0x18, 0, page);
81 msleep(1); /* wait for the page switch to propagate */
82 lo = phy->bus->read(phy->bus, 0x10 | r2, r1);
83 hi = phy->bus->read(phy->bus, 0x10 | r2, r1 + 1);
85 return (hi << 16) | lo;
89 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
91 struct phy_device *phy = priv->phy;
95 split_addr((u32) reg, &r1, &r2, &r3);
96 phy->bus->write(phy->bus, 0x18, 0, r3);
97 msleep(1); /* wait for the page switch to propagate */
100 hi = (u16) (val >> 16);
101 phy->bus->write(phy->bus, 0x10 | r2, r1 + 1, hi);
102 phy->bus->write(phy->bus, 0x10 | r2, r1, lo);
106 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
110 v = priv->read(priv, reg);
113 priv->write(priv, reg, v);
119 ar8216_id_chip(struct ar8216_priv *priv)
125 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
129 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
130 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
133 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
137 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
149 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
150 (int)(id >> AR8216_CTRL_VERSION_S),
151 (int)(id & AR8216_CTRL_REVISION),
152 priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 2),
153 priv->phy->bus->read(priv->phy->bus, priv->phy->addr, 3));
160 ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
161 struct switch_val *val)
163 struct ar8216_priv *priv = to_ar8216(dev);
164 priv->vlan = !!val->value.i;
169 ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
170 struct switch_val *val)
172 struct ar8216_priv *priv = to_ar8216(dev);
173 val->value.i = priv->vlan;
179 ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
181 struct ar8216_priv *priv = to_ar8216(dev);
183 /* make sure no invalid PVIDs get set */
185 if (vlan >= dev->vlans)
188 priv->pvid[port] = vlan;
193 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
195 struct ar8216_priv *priv = to_ar8216(dev);
196 *vlan = priv->pvid[port];
201 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
202 struct switch_val *val)
204 struct ar8216_priv *priv = to_ar8216(dev);
205 priv->vlan_id[val->port_vlan] = val->value.i;
210 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
211 struct switch_val *val)
213 struct ar8216_priv *priv = to_ar8216(dev);
214 val->value.i = priv->vlan_id[val->port_vlan];
220 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
222 struct ar8216_priv *priv = dev->phy_ptr;
231 if (unlikely(skb_headroom(skb) < 2)) {
232 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
236 buf = skb_push(skb, 2);
241 return priv->ndo_old->ndo_start_xmit(skb, dev);
244 dev_kfree_skb_any(skb);
249 ar8216_mangle_rx(struct sk_buff *skb, int napi)
251 struct ar8216_priv *priv;
252 struct net_device *dev;
264 /* don't strip the header if vlan mode is disabled */
268 /* strip header, get vlan id */
272 /* check for vlan header presence */
273 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
278 /* no need to fix up packets coming from a tagged source */
279 if (priv->vlan_tagged & (1 << port))
282 /* lookup port vid from local table, the switch passes an invalid vlan id */
283 vlan = priv->vlan_id[priv->pvid[port]];
286 buf[14 + 2] |= vlan >> 8;
287 buf[15 + 2] = vlan & 0xff;
290 skb->protocol = eth_type_trans(skb, skb->dev);
293 return netif_receive_skb(skb);
295 return netif_rx(skb);
298 /* no vlan? eat the packet! */
299 dev_kfree_skb_any(skb);
304 ar8216_netif_rx(struct sk_buff *skb)
306 return ar8216_mangle_rx(skb, 0);
310 ar8216_netif_receive_skb(struct sk_buff *skb)
312 return ar8216_mangle_rx(skb, 1);
316 static struct switch_attr ar8216_globals[] = {
318 .type = SWITCH_TYPE_INT,
319 .name = "enable_vlan",
320 .description = "Enable VLAN mode",
321 .set = ar8216_set_vlan,
322 .get = ar8216_get_vlan,
327 static struct switch_attr ar8216_port[] = {
330 static struct switch_attr ar8216_vlan[] = {
332 .type = SWITCH_TYPE_INT,
334 .description = "VLAN ID",
335 .set = ar8216_set_vid,
336 .get = ar8216_get_vid,
343 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
345 struct ar8216_priv *priv = to_ar8216(dev);
346 u8 ports = priv->vlan_table[val->port_vlan];
350 for (i = 0; i < AR8216_NUM_PORTS; i++) {
351 struct switch_port *p;
353 if (!(ports & (1 << i)))
356 p = &val->value.ports[val->len++];
358 if (priv->vlan_tagged & (1 << i))
359 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
367 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
369 struct ar8216_priv *priv = to_ar8216(dev);
370 u8 *vt = &priv->vlan_table[val->port_vlan];
374 for (i = 0; i < val->len; i++) {
375 struct switch_port *p = &val->value.ports[i];
377 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED))
378 priv->vlan_tagged |= (1 << p->id);
380 priv->vlan_tagged &= ~(1 << p->id);
381 priv->pvid[p->id] = val->port_vlan;
383 /* make sure that an untagged port does not
384 * appear in other vlans */
385 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
386 if (j == val->port_vlan)
388 priv->vlan_table[j] &= ~(1 << p->id);
398 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
402 while ((priv->read(priv, reg) & mask) != val) {
403 if (timeout-- <= 0) {
404 printk(KERN_ERR "ar8216: timeout waiting for operation to complete\n");
412 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
414 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
416 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
417 val &= AR8216_VTUDATA_MEMBER;
418 val |= AR8216_VTUDATA_VALID;
419 priv->write(priv, AR8216_REG_VTU_DATA, val);
421 op |= AR8216_VTU_ACTIVE;
422 priv->write(priv, AR8216_REG_VTU, op);
426 ar8216_hw_apply(struct switch_dev *dev)
428 struct ar8216_priv *priv = to_ar8216(dev);
429 u8 portmask[AR8216_NUM_PORTS];
432 mutex_lock(&priv->reg_mutex);
433 /* flush all vlan translation unit entries */
434 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
436 memset(portmask, 0, sizeof(portmask));
438 /* calculate the port destination masks and load vlans
439 * into the vlan translation unit */
440 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
441 u8 vp = priv->vlan_table[j];
446 for (i = 0; i < AR8216_NUM_PORTS; i++) {
449 portmask[i] |= vp & ~mask;
454 (priv->vlan_id[j] << AR8216_VTU_VID_S),
455 priv->vlan_table[j]);
459 * isolate all ports, but connect them to the cpu port */
460 for (i = 0; i < AR8216_NUM_PORTS; i++) {
461 if (i == AR8216_PORT_CPU)
464 portmask[i] = 1 << AR8216_PORT_CPU;
465 portmask[AR8216_PORT_CPU] |= (1 << i);
469 /* update the port destination mask registers and tag settings */
470 for (i = 0; i < AR8216_NUM_PORTS; i++) {
475 pvid = priv->vlan_id[priv->pvid[i]];
480 if (priv->vlan && (priv->vlan_tagged & (1 << i))) {
481 egress = AR8216_OUT_ADD_VLAN;
483 egress = AR8216_OUT_STRIP_VLAN;
486 ingress = AR8216_IN_SECURE;
488 ingress = AR8216_IN_PORT_ONLY;
491 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(i),
492 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
493 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
494 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
495 AR8216_PORT_CTRL_LEARN |
496 (priv->vlan && i == AR8216_PORT_CPU && (priv->chip == AR8216) ?
497 AR8216_PORT_CTRL_HEADER : 0) |
498 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
499 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
501 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(i),
502 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
503 AR8216_PORT_VLAN_DEFAULT_ID,
504 (portmask[i] << AR8216_PORT_VLAN_DEST_PORTS_S) |
505 (ingress << AR8216_PORT_VLAN_MODE_S) |
506 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
508 mutex_unlock(&priv->reg_mutex);
513 ar8316_hw_init(struct ar8216_priv *priv) {
514 static int initialized;
522 val = priv->read(priv, 0x8);
524 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
525 /* value taken from Ubiquiti RouterStation Pro */
526 if (val == 0x81461bea) {
527 /* switch already intialized by bootloader */
531 priv->write(priv, 0x8, 0x81461bea);
532 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
533 /* value taken from AVM Fritz!Box 7390 sources */
534 if (val == 0x010e5b71) {
535 /* switch already initialized by bootloader */
539 priv->write(priv, 0x8, 0x010e5b71);
541 /* no known value for phy interface */
542 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
543 priv->phy->interface);
547 /* standard atheros magic */
548 priv->write(priv, 0x38, 0xc000050e);
550 /* Initialize the ports */
551 bus = priv->phy->bus;
552 for (i = 0; i < 5; i++) {
554 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
555 /* work around for phy4 rgmii mode */
556 bus->write(bus, i, MII_ATH_DBG_ADDR, 0x12);
557 bus->write(bus, i, MII_ATH_DBG_DATA, 0x480c);
559 bus->write(bus, i, MII_ATH_DBG_ADDR, 0x0);
560 bus->write(bus, i, MII_ATH_DBG_DATA, 0x824e);
562 bus->write(bus, i, MII_ATH_DBG_ADDR, 0x5);
563 bus->write(bus, i, MII_ATH_DBG_DATA, 0x3d47);
567 /* initialize the port itself */
568 bus->write(bus, i, MII_ADVERTISE,
569 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
570 bus->write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
571 bus->write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
579 ar8216_reset_switch(struct switch_dev *dev)
581 struct ar8216_priv *priv = to_ar8216(dev);
584 mutex_lock(&priv->reg_mutex);
585 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
586 offsetof(struct ar8216_priv, vlan));
587 for (i = 0; i < AR8X16_MAX_VLANS; i++) {
588 priv->vlan_id[i] = i;
590 for (i = 0; i < AR8216_NUM_PORTS; i++) {
591 /* Enable port learning and tx */
592 priv->write(priv, AR8216_REG_PORT_CTRL(i),
593 AR8216_PORT_CTRL_LEARN |
594 (4 << AR8216_PORT_CTRL_STATE_S));
596 priv->write(priv, AR8216_REG_PORT_VLAN(i), 0);
598 /* Configure all PHYs */
599 if (i == AR8216_PORT_CPU) {
600 priv->write(priv, AR8216_REG_PORT_STATUS(i),
601 AR8216_PORT_STATUS_LINK_UP |
602 ((priv->chip == AR8316) ?
603 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
604 AR8216_PORT_STATUS_TXMAC |
605 AR8216_PORT_STATUS_RXMAC |
606 ((priv->chip == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
607 ((priv->chip == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
608 AR8216_PORT_STATUS_DUPLEX);
610 priv->write(priv, AR8216_REG_PORT_STATUS(i),
611 AR8216_PORT_STATUS_LINK_AUTO);
614 /* XXX: undocumented magic from atheros, required! */
615 priv->write(priv, 0x38, 0xc000050e);
617 if (priv->chip == AR8216) {
618 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
619 AR8216_GCTRL_MTU, 1518 + 8 + 2);
620 } else if (priv->chip == AR8316) {
621 /* enable jumbo frames */
622 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
623 AR8316_GCTRL_MTU, 9018 + 8 + 2);
626 if (priv->chip == AR8316) {
627 /* enable cpu port to receive multicast and broadcast frames */
628 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
630 mutex_unlock(&priv->reg_mutex);
631 return ar8216_hw_apply(dev);
635 ar8216_config_init(struct phy_device *pdev)
637 struct ar8216_priv *priv;
638 struct net_device *dev = pdev->attached_dev;
641 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
647 priv->chip = ar8216_id_chip(priv);
650 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
651 pdev->attached_dev->name, priv->chip);
654 if (pdev->addr != 0) {
655 if (priv->chip == AR8316) {
656 pdev->supported |= SUPPORTED_1000baseT_Full;
657 pdev->advertising |= ADVERTISED_1000baseT_Full;
663 pdev->supported = priv->chip == AR8316 ?
664 SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
665 pdev->advertising = pdev->supported;
667 mutex_init(&priv->reg_mutex);
668 priv->read = ar8216_mii_read;
669 priv->write = ar8216_mii_write;
670 memcpy(&priv->dev, &athdev, sizeof(struct switch_dev));
673 if (priv->chip == AR8316) {
674 priv->dev.name = "Atheros AR8316";
675 priv->dev.vlans = AR8X16_MAX_VLANS;
676 /* port 5 connected to the other mac, therefore unusable */
677 priv->dev.ports = (AR8216_NUM_PORTS - 1);
680 if ((ret = register_switch(&priv->dev, pdev->attached_dev)) < 0) {
685 if (priv->chip == AR8316) {
686 ret = ar8316_hw_init(priv);
693 ret = ar8216_reset_switch(&priv->dev);
701 /* VID fixup only needed on ar8216 */
702 if (pdev->addr == 0 && priv->chip == AR8216) {
704 pdev->netif_receive_skb = ar8216_netif_receive_skb;
705 pdev->netif_rx = ar8216_netif_rx;
706 priv->ndo_old = dev->netdev_ops;
707 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
708 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
709 dev->netdev_ops = &priv->ndo;
717 ar8216_read_status(struct phy_device *phydev)
719 struct ar8216_priv *priv = phydev->priv;
721 if (phydev->addr != 0) {
722 return genphy_read_status(phydev);
725 phydev->speed = priv->chip == AR8316 ? SPEED_1000 : SPEED_100;
726 phydev->duplex = DUPLEX_FULL;
729 /* flush the address translation unit */
730 mutex_lock(&priv->reg_mutex);
731 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
734 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
737 mutex_unlock(&priv->reg_mutex);
739 phydev->state = PHY_RUNNING;
740 netif_carrier_on(phydev->attached_dev);
741 phydev->adjust_link(phydev->attached_dev);
747 ar8216_config_aneg(struct phy_device *phydev)
749 if (phydev->addr == 0)
752 return genphy_config_aneg(phydev);
756 ar8216_probe(struct phy_device *pdev)
758 struct ar8216_priv priv;
762 chip = ar8216_id_chip(&priv);
770 ar8216_remove(struct phy_device *pdev)
772 struct ar8216_priv *priv = pdev->priv;
773 struct net_device *dev = pdev->attached_dev;
778 if (priv->ndo_old && dev)
779 dev->netdev_ops = priv->ndo_old;
781 unregister_switch(&priv->dev);
786 static struct switch_dev athdev = {
787 .name = "Atheros AR8216",
788 .cpu_port = AR8216_PORT_CPU,
789 .ports = AR8216_NUM_PORTS,
790 .vlans = AR8216_NUM_VLANS,
792 .attr = ar8216_globals,
793 .n_attr = ARRAY_SIZE(ar8216_globals),
797 .n_attr = ARRAY_SIZE(ar8216_port),
801 .n_attr = ARRAY_SIZE(ar8216_vlan),
803 .get_port_pvid = ar8216_get_pvid,
804 .set_port_pvid = ar8216_set_pvid,
805 .get_vlan_ports = ar8216_get_ports,
806 .set_vlan_ports = ar8216_set_ports,
807 .apply_config = ar8216_hw_apply,
808 .reset_switch = ar8216_reset_switch,
811 static struct phy_driver ar8216_driver = {
812 .phy_id = 0x004d0000,
813 .name = "Atheros AR8216/AR8316",
814 .phy_id_mask = 0xffff0000,
815 .features = PHY_BASIC_FEATURES,
816 .probe = ar8216_probe,
817 .remove = ar8216_remove,
818 .config_init = &ar8216_config_init,
819 .config_aneg = &ar8216_config_aneg,
820 .read_status = &ar8216_read_status,
821 .driver = { .owner = THIS_MODULE },
827 return phy_driver_register(&ar8216_driver);
833 phy_driver_unregister(&ar8216_driver);
836 module_init(ar8216_init);
837 module_exit(ar8216_exit);
838 MODULE_LICENSE("GPL");