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>
32 #include <linux/lockdep.h>
35 /* size of the vlan table */
36 #define AR8X16_MAX_VLANS 128
37 #define AR8X16_PROBE_RETRIES 10
41 #define AR8XXX_CAP_GIGE BIT(0)
46 int (*hw_init)(struct ar8216_priv *priv);
47 void (*init_globals)(struct ar8216_priv *priv);
48 void (*init_port)(struct ar8216_priv *priv, int port);
49 void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
50 u32 ingress, u32 members, u32 pvid);
51 u32 (*read_port_status)(struct ar8216_priv *priv, int port);
52 int (*atu_flush)(struct ar8216_priv *priv);
53 void (*vtu_flush)(struct ar8216_priv *priv);
54 void (*vtu_load_vlan)(struct ar8216_priv *priv, u32 vid, u32 port_mask);
58 struct switch_dev dev;
59 struct phy_device *phy;
60 u32 (*read)(struct ar8216_priv *priv, int reg);
61 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
62 const struct net_device_ops *ndo_old;
63 struct net_device_ops ndo;
64 struct mutex reg_mutex;
66 const struct ar8xxx_chip *chip;
73 /* all fields below are cleared on reset */
75 u16 vlan_id[AR8X16_MAX_VLANS];
76 u8 vlan_table[AR8X16_MAX_VLANS];
78 u16 pvid[AR8216_NUM_PORTS];
81 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
83 static inline bool ar8xxx_has_gige(struct ar8216_priv *priv)
85 return priv->chip->caps & AR8XXX_CAP_GIGE;
89 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
98 *page = regaddr & 0x1ff;
102 ar8216_mii_read(struct ar8216_priv *priv, int reg)
104 struct phy_device *phy = priv->phy;
105 struct mii_bus *bus = phy->bus;
109 split_addr((u32) reg, &r1, &r2, &page);
111 mutex_lock(&bus->mdio_lock);
113 bus->write(bus, 0x18, 0, page);
114 usleep_range(1000, 2000); /* wait for the page switch to propagate */
115 lo = bus->read(bus, 0x10 | r2, r1);
116 hi = bus->read(bus, 0x10 | r2, r1 + 1);
118 mutex_unlock(&bus->mdio_lock);
120 return (hi << 16) | lo;
124 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
126 struct phy_device *phy = priv->phy;
127 struct mii_bus *bus = phy->bus;
131 split_addr((u32) reg, &r1, &r2, &r3);
133 hi = (u16) (val >> 16);
135 mutex_lock(&bus->mdio_lock);
137 bus->write(bus, 0x18, 0, r3);
138 usleep_range(1000, 2000); /* wait for the page switch to propagate */
139 bus->write(bus, 0x10 | r2, r1 + 1, hi);
140 bus->write(bus, 0x10 | r2, r1, lo);
142 mutex_unlock(&bus->mdio_lock);
146 ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
147 u16 dbg_addr, u16 dbg_data)
149 struct mii_bus *bus = priv->phy->bus;
151 mutex_lock(&bus->mdio_lock);
152 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
153 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
154 mutex_unlock(&bus->mdio_lock);
158 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
162 lockdep_assert_held(&priv->reg_mutex);
164 v = priv->read(priv, reg);
167 priv->write(priv, reg, v);
173 ar8216_read_port_link(struct ar8216_priv *priv, int port,
174 struct switch_port_link *link)
179 memset(link, '\0', sizeof(*link));
181 status = priv->chip->read_port_status(priv, port);
183 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
185 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
192 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
193 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
194 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
196 speed = (status & AR8216_PORT_STATUS_SPEED) >>
197 AR8216_PORT_STATUS_SPEED_S;
200 case AR8216_PORT_SPEED_10M:
201 link->speed = SWITCH_PORT_SPEED_10;
203 case AR8216_PORT_SPEED_100M:
204 link->speed = SWITCH_PORT_SPEED_100;
206 case AR8216_PORT_SPEED_1000M:
207 link->speed = SWITCH_PORT_SPEED_1000;
210 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
216 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
218 struct ar8216_priv *priv = dev->phy_ptr;
227 if (unlikely(skb_headroom(skb) < 2)) {
228 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
232 buf = skb_push(skb, 2);
237 return priv->ndo_old->ndo_start_xmit(skb, dev);
240 dev_kfree_skb_any(skb);
245 ar8216_mangle_rx(struct sk_buff *skb, int napi)
247 struct ar8216_priv *priv;
248 struct net_device *dev;
260 /* don't strip the header if vlan mode is disabled */
264 /* strip header, get vlan id */
268 /* check for vlan header presence */
269 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
274 /* no need to fix up packets coming from a tagged source */
275 if (priv->vlan_tagged & (1 << port))
278 /* lookup port vid from local table, the switch passes an invalid vlan id */
279 vlan = priv->vlan_id[priv->pvid[port]];
282 buf[14 + 2] |= vlan >> 8;
283 buf[15 + 2] = vlan & 0xff;
286 skb->protocol = eth_type_trans(skb, skb->dev);
289 return netif_receive_skb(skb);
291 return netif_rx(skb);
294 /* no vlan? eat the packet! */
295 dev_kfree_skb_any(skb);
300 ar8216_netif_rx(struct sk_buff *skb)
302 return ar8216_mangle_rx(skb, 0);
306 ar8216_netif_receive_skb(struct sk_buff *skb)
308 return ar8216_mangle_rx(skb, 1);
312 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
318 t = priv->read(priv, reg);
319 if ((t & mask) == val)
328 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
329 (unsigned int) reg, t, mask, val);
334 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
336 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
338 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
339 val &= AR8216_VTUDATA_MEMBER;
340 val |= AR8216_VTUDATA_VALID;
341 priv->write(priv, AR8216_REG_VTU_DATA, val);
343 op |= AR8216_VTU_ACTIVE;
344 priv->write(priv, AR8216_REG_VTU, op);
348 ar8216_vtu_flush(struct ar8216_priv *priv)
350 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
354 ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
358 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
359 ar8216_vtu_op(priv, op, port_mask);
363 ar8216_atu_flush(struct ar8216_priv *priv)
367 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
369 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
375 ar8216_read_port_status(struct ar8216_priv *priv, int port)
377 return priv->read(priv, AR8216_REG_PORT_STATUS(port));
381 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
382 u32 members, u32 pvid)
386 if (priv->vlan && port == AR8216_PORT_CPU && priv->chip_type == AR8216)
387 header = AR8216_PORT_CTRL_HEADER;
391 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
392 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
393 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
394 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
395 AR8216_PORT_CTRL_LEARN | header |
396 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
397 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
399 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
400 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
401 AR8216_PORT_VLAN_DEFAULT_ID,
402 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
403 (ingress << AR8216_PORT_VLAN_MODE_S) |
404 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
408 ar8216_hw_init(struct ar8216_priv *priv)
414 ar8216_init_globals(struct ar8216_priv *priv)
416 /* standard atheros magic */
417 priv->write(priv, 0x38, 0xc000050e);
419 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
420 AR8216_GCTRL_MTU, 1518 + 8 + 2);
424 ar8216_init_port(struct ar8216_priv *priv, int port)
426 /* Enable port learning and tx */
427 priv->write(priv, AR8216_REG_PORT_CTRL(port),
428 AR8216_PORT_CTRL_LEARN |
429 (4 << AR8216_PORT_CTRL_STATE_S));
431 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
433 if (port == AR8216_PORT_CPU) {
434 priv->write(priv, AR8216_REG_PORT_STATUS(port),
435 AR8216_PORT_STATUS_LINK_UP |
436 ar8xxx_has_gige(priv) ? AR8216_PORT_SPEED_1000M :
437 AR8216_PORT_SPEED_100M |
438 AR8216_PORT_STATUS_TXMAC |
439 AR8216_PORT_STATUS_RXMAC |
440 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
441 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
442 AR8216_PORT_STATUS_DUPLEX);
444 priv->write(priv, AR8216_REG_PORT_STATUS(port),
445 AR8216_PORT_STATUS_LINK_AUTO);
449 static const struct ar8xxx_chip ar8216_chip = {
450 .hw_init = ar8216_hw_init,
451 .init_globals = ar8216_init_globals,
452 .init_port = ar8216_init_port,
453 .setup_port = ar8216_setup_port,
454 .read_port_status = ar8216_read_port_status,
455 .atu_flush = ar8216_atu_flush,
456 .vtu_flush = ar8216_vtu_flush,
457 .vtu_load_vlan = ar8216_vtu_load_vlan,
461 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
462 u32 members, u32 pvid)
464 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
465 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
466 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
467 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
468 AR8216_PORT_CTRL_LEARN |
469 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
470 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
472 ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
473 AR8236_PORT_VLAN_DEFAULT_ID,
474 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
476 ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
477 AR8236_PORT_VLAN2_VLAN_MODE |
478 AR8236_PORT_VLAN2_MEMBER,
479 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
480 (members << AR8236_PORT_VLAN2_MEMBER_S));
484 ar8236_hw_init(struct ar8216_priv *priv)
489 if (priv->initialized)
492 /* Initialize the PHYs */
493 bus = priv->phy->bus;
494 for (i = 0; i < 5; i++) {
495 mdiobus_write(bus, i, MII_ADVERTISE,
496 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
497 ADVERTISE_PAUSE_ASYM);
498 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
502 priv->initialized = true;
507 ar8236_init_globals(struct ar8216_priv *priv)
509 /* enable jumbo frames */
510 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
511 AR8316_GCTRL_MTU, 9018 + 8 + 2);
514 static const struct ar8xxx_chip ar8236_chip = {
515 .hw_init = ar8236_hw_init,
516 .init_globals = ar8236_init_globals,
517 .init_port = ar8216_init_port,
518 .setup_port = ar8236_setup_port,
519 .read_port_status = ar8216_read_port_status,
520 .atu_flush = ar8216_atu_flush,
521 .vtu_flush = ar8216_vtu_flush,
522 .vtu_load_vlan = ar8216_vtu_load_vlan,
526 ar8316_hw_init(struct ar8216_priv *priv)
532 val = priv->read(priv, 0x8);
534 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
535 if (priv->port4_phy) {
536 /* value taken from Ubiquiti RouterStation Pro */
538 printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
541 printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
543 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
544 /* value taken from AVM Fritz!Box 7390 sources */
547 /* no known value for phy interface */
548 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
549 priv->phy->interface);
556 priv->write(priv, 0x8, newval);
558 /* Initialize the ports */
559 bus = priv->phy->bus;
560 for (i = 0; i < 5; i++) {
561 if ((i == 4) && priv->port4_phy &&
562 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
563 /* work around for phy4 rgmii mode */
564 ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
566 ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
568 ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
572 /* initialize the port itself */
573 mdiobus_write(bus, i, MII_ADVERTISE,
574 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
575 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
576 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
581 priv->initialized = true;
586 ar8316_init_globals(struct ar8216_priv *priv)
588 /* standard atheros magic */
589 priv->write(priv, 0x38, 0xc000050e);
591 /* enable cpu port to receive multicast and broadcast frames */
592 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
594 /* enable jumbo frames */
595 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
596 AR8316_GCTRL_MTU, 9018 + 8 + 2);
599 static const struct ar8xxx_chip ar8316_chip = {
600 .caps = AR8XXX_CAP_GIGE,
601 .hw_init = ar8316_hw_init,
602 .init_globals = ar8316_init_globals,
603 .init_port = ar8216_init_port,
604 .setup_port = ar8216_setup_port,
605 .read_port_status = ar8216_read_port_status,
606 .atu_flush = ar8216_atu_flush,
607 .vtu_flush = ar8216_vtu_flush,
608 .vtu_load_vlan = ar8216_vtu_load_vlan,
612 ar8216_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
613 struct switch_val *val)
615 struct ar8216_priv *priv = to_ar8216(dev);
616 priv->vlan = !!val->value.i;
621 ar8216_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
622 struct switch_val *val)
624 struct ar8216_priv *priv = to_ar8216(dev);
625 val->value.i = priv->vlan;
631 ar8216_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
633 struct ar8216_priv *priv = to_ar8216(dev);
635 /* make sure no invalid PVIDs get set */
637 if (vlan >= dev->vlans)
640 priv->pvid[port] = vlan;
645 ar8216_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
647 struct ar8216_priv *priv = to_ar8216(dev);
648 *vlan = priv->pvid[port];
653 ar8216_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
654 struct switch_val *val)
656 struct ar8216_priv *priv = to_ar8216(dev);
657 priv->vlan_id[val->port_vlan] = val->value.i;
662 ar8216_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
663 struct switch_val *val)
665 struct ar8216_priv *priv = to_ar8216(dev);
666 val->value.i = priv->vlan_id[val->port_vlan];
671 ar8216_sw_get_port_link(struct switch_dev *dev, int port,
672 struct switch_port_link *link)
674 struct ar8216_priv *priv = to_ar8216(dev);
676 ar8216_read_port_link(priv, port, link);
681 ar8216_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
683 struct ar8216_priv *priv = to_ar8216(dev);
684 u8 ports = priv->vlan_table[val->port_vlan];
688 for (i = 0; i < dev->ports; i++) {
689 struct switch_port *p;
691 if (!(ports & (1 << i)))
694 p = &val->value.ports[val->len++];
696 if (priv->vlan_tagged & (1 << i))
697 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
705 ar8216_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
707 struct ar8216_priv *priv = to_ar8216(dev);
708 u8 *vt = &priv->vlan_table[val->port_vlan];
712 for (i = 0; i < val->len; i++) {
713 struct switch_port *p = &val->value.ports[i];
715 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
716 priv->vlan_tagged |= (1 << p->id);
718 priv->vlan_tagged &= ~(1 << p->id);
719 priv->pvid[p->id] = val->port_vlan;
721 /* make sure that an untagged port does not
722 * appear in other vlans */
723 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
724 if (j == val->port_vlan)
726 priv->vlan_table[j] &= ~(1 << p->id);
736 ar8216_sw_hw_apply(struct switch_dev *dev)
738 struct ar8216_priv *priv = to_ar8216(dev);
739 u8 portmask[AR8216_NUM_PORTS];
742 mutex_lock(&priv->reg_mutex);
743 /* flush all vlan translation unit entries */
744 priv->chip->vtu_flush(priv);
746 memset(portmask, 0, sizeof(portmask));
748 /* calculate the port destination masks and load vlans
749 * into the vlan translation unit */
750 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
751 u8 vp = priv->vlan_table[j];
756 for (i = 0; i < dev->ports; i++) {
759 portmask[i] |= vp & ~mask;
762 priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
763 priv->vlan_table[j]);
767 * isolate all ports, but connect them to the cpu port */
768 for (i = 0; i < dev->ports; i++) {
769 if (i == AR8216_PORT_CPU)
772 portmask[i] = 1 << AR8216_PORT_CPU;
773 portmask[AR8216_PORT_CPU] |= (1 << i);
777 /* update the port destination mask registers and tag settings */
778 for (i = 0; i < dev->ports; i++) {
783 pvid = priv->vlan_id[priv->pvid[i]];
784 if (priv->vlan_tagged & (1 << i))
785 egress = AR8216_OUT_ADD_VLAN;
787 egress = AR8216_OUT_STRIP_VLAN;
788 ingress = AR8216_IN_SECURE;
791 egress = AR8216_OUT_KEEP;
792 ingress = AR8216_IN_PORT_ONLY;
795 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
798 mutex_unlock(&priv->reg_mutex);
803 ar8216_sw_reset_switch(struct switch_dev *dev)
805 struct ar8216_priv *priv = to_ar8216(dev);
808 mutex_lock(&priv->reg_mutex);
809 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
810 offsetof(struct ar8216_priv, vlan));
812 for (i = 0; i < AR8X16_MAX_VLANS; i++)
813 priv->vlan_id[i] = i;
815 /* Configure all ports */
816 for (i = 0; i < dev->ports; i++)
817 priv->chip->init_port(priv, i);
819 priv->chip->init_globals(priv);
820 mutex_unlock(&priv->reg_mutex);
822 return ar8216_sw_hw_apply(dev);
825 static struct switch_attr ar8216_globals[] = {
827 .type = SWITCH_TYPE_INT,
828 .name = "enable_vlan",
829 .description = "Enable VLAN mode",
830 .set = ar8216_sw_set_vlan,
831 .get = ar8216_sw_get_vlan,
836 static struct switch_attr ar8216_port[] = {
839 static struct switch_attr ar8216_vlan[] = {
841 .type = SWITCH_TYPE_INT,
843 .description = "VLAN ID (0-4094)",
844 .set = ar8216_sw_set_vid,
845 .get = ar8216_sw_get_vid,
850 static const struct switch_dev_ops ar8216_sw_ops = {
852 .attr = ar8216_globals,
853 .n_attr = ARRAY_SIZE(ar8216_globals),
857 .n_attr = ARRAY_SIZE(ar8216_port),
861 .n_attr = ARRAY_SIZE(ar8216_vlan),
863 .get_port_pvid = ar8216_sw_get_pvid,
864 .set_port_pvid = ar8216_sw_set_pvid,
865 .get_vlan_ports = ar8216_sw_get_ports,
866 .set_vlan_ports = ar8216_sw_set_ports,
867 .apply_config = ar8216_sw_hw_apply,
868 .reset_switch = ar8216_sw_reset_switch,
869 .get_port_link = ar8216_sw_get_port_link,
873 ar8216_id_chip(struct ar8216_priv *priv)
879 priv->chip_type = UNKNOWN;
881 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
885 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
886 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
889 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
893 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
900 priv->chip_type = AR8216;
901 priv->chip = &ar8216_chip;
904 priv->chip_type = AR8236;
905 priv->chip = &ar8236_chip;
909 priv->chip_type = AR8316;
910 priv->chip = &ar8316_chip;
914 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
915 (int)(id >> AR8216_CTRL_VERSION_S),
916 (int)(id & AR8216_CTRL_REVISION),
917 mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
918 mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
927 ar8216_config_init(struct phy_device *pdev)
929 struct ar8216_priv *priv = pdev->priv;
930 struct net_device *dev = pdev->attached_dev;
931 struct switch_dev *swdev;
935 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
942 ret = ar8216_id_chip(priv);
946 if (pdev->addr != 0) {
947 if (ar8xxx_has_gige(priv)) {
948 pdev->supported |= SUPPORTED_1000baseT_Full;
949 pdev->advertising |= ADVERTISED_1000baseT_Full;
952 if (priv->chip_type == AR8316) {
953 /* check if we're attaching to the switch twice */
954 pdev = pdev->bus->phy_map[0];
960 /* switch device has not been initialized, reuse priv */
962 priv->port4_phy = true;
969 /* switch device has been initialized, reinit */
971 priv->dev.ports = (AR8216_NUM_PORTS - 1);
972 priv->initialized = false;
973 priv->port4_phy = true;
974 ar8316_hw_init(priv);
982 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
983 pdev->attached_dev->name, priv->chip_type);
985 if (ar8xxx_has_gige(priv))
986 pdev->supported = SUPPORTED_1000baseT_Full;
988 pdev->supported = SUPPORTED_100baseT_Full;
989 pdev->advertising = pdev->supported;
991 mutex_init(&priv->reg_mutex);
992 priv->read = ar8216_mii_read;
993 priv->write = ar8216_mii_write;
998 swdev->cpu_port = AR8216_PORT_CPU;
999 swdev->ops = &ar8216_sw_ops;
1000 swdev->ports = AR8216_NUM_PORTS;
1002 if (priv->chip_type == AR8316) {
1003 swdev->name = "Atheros AR8316";
1004 swdev->vlans = AR8X16_MAX_VLANS;
1006 if (priv->port4_phy) {
1007 /* port 5 connected to the other mac, therefore unusable */
1008 swdev->ports = (AR8216_NUM_PORTS - 1);
1010 } else if (priv->chip_type == AR8236) {
1011 swdev->name = "Atheros AR8236";
1012 swdev->vlans = AR8216_NUM_VLANS;
1013 swdev->ports = AR8216_NUM_PORTS;
1015 swdev->name = "Atheros AR8216";
1016 swdev->vlans = AR8216_NUM_VLANS;
1019 ret = register_switch(&priv->dev, pdev->attached_dev);
1025 ret = priv->chip->hw_init(priv);
1029 ret = ar8216_sw_reset_switch(&priv->dev);
1033 dev->phy_ptr = priv;
1035 /* VID fixup only needed on ar8216 */
1036 if (pdev->addr == 0 && priv->chip_type == AR8216) {
1037 pdev->pkt_align = 2;
1038 pdev->netif_receive_skb = ar8216_netif_receive_skb;
1039 pdev->netif_rx = ar8216_netif_rx;
1040 priv->ndo_old = dev->netdev_ops;
1041 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
1042 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
1043 dev->netdev_ops = &priv->ndo;
1056 ar8216_read_status(struct phy_device *phydev)
1058 struct ar8216_priv *priv = phydev->priv;
1059 struct switch_port_link link;
1062 if (phydev->addr != 0)
1063 return genphy_read_status(phydev);
1065 ar8216_read_port_link(priv, phydev->addr, &link);
1066 phydev->link = !!link.link;
1070 switch (link.speed) {
1071 case SWITCH_PORT_SPEED_10:
1072 phydev->speed = SPEED_10;
1074 case SWITCH_PORT_SPEED_100:
1075 phydev->speed = SPEED_100;
1077 case SWITCH_PORT_SPEED_1000:
1078 phydev->speed = SPEED_1000;
1083 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1085 /* flush the address translation unit */
1086 mutex_lock(&priv->reg_mutex);
1087 ret = priv->chip->atu_flush(priv);
1088 mutex_unlock(&priv->reg_mutex);
1090 phydev->state = PHY_RUNNING;
1091 netif_carrier_on(phydev->attached_dev);
1092 phydev->adjust_link(phydev->attached_dev);
1098 ar8216_config_aneg(struct phy_device *phydev)
1100 if (phydev->addr == 0)
1103 return genphy_config_aneg(phydev);
1107 ar8216_probe(struct phy_device *pdev)
1109 struct ar8216_priv priv;
1112 return ar8216_id_chip(&priv);
1116 ar8216_remove(struct phy_device *pdev)
1118 struct ar8216_priv *priv = pdev->priv;
1119 struct net_device *dev = pdev->attached_dev;
1124 if (priv->ndo_old && dev)
1125 dev->netdev_ops = priv->ndo_old;
1126 if (pdev->addr == 0)
1127 unregister_switch(&priv->dev);
1131 static struct phy_driver ar8216_driver = {
1132 .phy_id = 0x004d0000,
1133 .name = "Atheros AR8216/AR8236/AR8316",
1134 .phy_id_mask = 0xffff0000,
1135 .features = PHY_BASIC_FEATURES,
1136 .probe = ar8216_probe,
1137 .remove = ar8216_remove,
1138 .config_init = &ar8216_config_init,
1139 .config_aneg = &ar8216_config_aneg,
1140 .read_status = &ar8216_read_status,
1141 .driver = { .owner = THIS_MODULE },
1147 return phy_driver_register(&ar8216_driver);
1153 phy_driver_unregister(&ar8216_driver);
1156 module_init(ar8216_init);
1157 module_exit(ar8216_exit);
1158 MODULE_LICENSE("GPL");