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_sw_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
217 struct switch_val *val)
219 struct ar8216_priv *priv = to_ar8216(dev);
220 priv->vlan = !!val->value.i;
225 ar8216_sw_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
226 struct switch_val *val)
228 struct ar8216_priv *priv = to_ar8216(dev);
229 val->value.i = priv->vlan;
235 ar8216_sw_set_pvid(struct switch_dev *dev, int port, int vlan)
237 struct ar8216_priv *priv = to_ar8216(dev);
239 /* make sure no invalid PVIDs get set */
241 if (vlan >= dev->vlans)
244 priv->pvid[port] = vlan;
249 ar8216_sw_get_pvid(struct switch_dev *dev, int port, int *vlan)
251 struct ar8216_priv *priv = to_ar8216(dev);
252 *vlan = priv->pvid[port];
257 ar8216_sw_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
258 struct switch_val *val)
260 struct ar8216_priv *priv = to_ar8216(dev);
261 priv->vlan_id[val->port_vlan] = val->value.i;
266 ar8216_sw_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
267 struct switch_val *val)
269 struct ar8216_priv *priv = to_ar8216(dev);
270 val->value.i = priv->vlan_id[val->port_vlan];
275 ar8216_sw_get_port_link(struct switch_dev *dev, int port,
276 struct switch_port_link *link)
278 struct ar8216_priv *priv = to_ar8216(dev);
280 ar8216_read_port_link(priv, port, link);
285 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
287 struct ar8216_priv *priv = dev->phy_ptr;
296 if (unlikely(skb_headroom(skb) < 2)) {
297 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
301 buf = skb_push(skb, 2);
306 return priv->ndo_old->ndo_start_xmit(skb, dev);
309 dev_kfree_skb_any(skb);
314 ar8216_mangle_rx(struct sk_buff *skb, int napi)
316 struct ar8216_priv *priv;
317 struct net_device *dev;
329 /* don't strip the header if vlan mode is disabled */
333 /* strip header, get vlan id */
337 /* check for vlan header presence */
338 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
343 /* no need to fix up packets coming from a tagged source */
344 if (priv->vlan_tagged & (1 << port))
347 /* lookup port vid from local table, the switch passes an invalid vlan id */
348 vlan = priv->vlan_id[priv->pvid[port]];
351 buf[14 + 2] |= vlan >> 8;
352 buf[15 + 2] = vlan & 0xff;
355 skb->protocol = eth_type_trans(skb, skb->dev);
358 return netif_receive_skb(skb);
360 return netif_rx(skb);
363 /* no vlan? eat the packet! */
364 dev_kfree_skb_any(skb);
369 ar8216_netif_rx(struct sk_buff *skb)
371 return ar8216_mangle_rx(skb, 0);
375 ar8216_netif_receive_skb(struct sk_buff *skb)
377 return ar8216_mangle_rx(skb, 1);
381 static struct switch_attr ar8216_globals[] = {
383 .type = SWITCH_TYPE_INT,
384 .name = "enable_vlan",
385 .description = "Enable VLAN mode",
386 .set = ar8216_sw_set_vlan,
387 .get = ar8216_sw_get_vlan,
392 static struct switch_attr ar8216_port[] = {
395 static struct switch_attr ar8216_vlan[] = {
397 .type = SWITCH_TYPE_INT,
399 .description = "VLAN ID (0-4094)",
400 .set = ar8216_sw_set_vid,
401 .get = ar8216_sw_get_vid,
408 ar8216_sw_get_ports(struct switch_dev *dev, struct switch_val *val)
410 struct ar8216_priv *priv = to_ar8216(dev);
411 u8 ports = priv->vlan_table[val->port_vlan];
415 for (i = 0; i < dev->ports; i++) {
416 struct switch_port *p;
418 if (!(ports & (1 << i)))
421 p = &val->value.ports[val->len++];
423 if (priv->vlan_tagged & (1 << i))
424 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
432 ar8216_sw_set_ports(struct switch_dev *dev, struct switch_val *val)
434 struct ar8216_priv *priv = to_ar8216(dev);
435 u8 *vt = &priv->vlan_table[val->port_vlan];
439 for (i = 0; i < val->len; i++) {
440 struct switch_port *p = &val->value.ports[i];
442 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
443 priv->vlan_tagged |= (1 << p->id);
445 priv->vlan_tagged &= ~(1 << p->id);
446 priv->pvid[p->id] = val->port_vlan;
448 /* make sure that an untagged port does not
449 * appear in other vlans */
450 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
451 if (j == val->port_vlan)
453 priv->vlan_table[j] &= ~(1 << p->id);
463 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
469 t = priv->read(priv, reg);
470 if ((t & mask) == val)
479 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
480 (unsigned int) reg, t, mask, val);
485 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
487 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
489 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
490 val &= AR8216_VTUDATA_MEMBER;
491 val |= AR8216_VTUDATA_VALID;
492 priv->write(priv, AR8216_REG_VTU_DATA, val);
494 op |= AR8216_VTU_ACTIVE;
495 priv->write(priv, AR8216_REG_VTU, op);
499 ar8216_vtu_flush(struct ar8216_priv *priv)
501 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
505 ar8216_vtu_load_vlan(struct ar8216_priv *priv, u32 vid, u32 port_mask)
509 op = AR8216_VTU_OP_LOAD | (vid << AR8216_VTU_VID_S);
510 ar8216_vtu_op(priv, op, port_mask);
514 ar8216_atu_flush(struct ar8216_priv *priv)
518 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
520 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
526 ar8216_read_port_status(struct ar8216_priv *priv, int port)
528 return priv->read(priv, AR8216_REG_PORT_STATUS(port));
532 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
533 u32 members, u32 pvid)
537 if (priv->vlan && port == AR8216_PORT_CPU && priv->chip_type == AR8216)
538 header = AR8216_PORT_CTRL_HEADER;
542 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
543 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
544 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
545 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
546 AR8216_PORT_CTRL_LEARN | header |
547 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
548 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
550 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
551 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
552 AR8216_PORT_VLAN_DEFAULT_ID,
553 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
554 (ingress << AR8216_PORT_VLAN_MODE_S) |
555 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
559 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
560 u32 members, u32 pvid)
562 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
563 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
564 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
565 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
566 AR8216_PORT_CTRL_LEARN |
567 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
568 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
570 ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
571 AR8236_PORT_VLAN_DEFAULT_ID,
572 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
574 ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
575 AR8236_PORT_VLAN2_VLAN_MODE |
576 AR8236_PORT_VLAN2_MEMBER,
577 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
578 (members << AR8236_PORT_VLAN2_MEMBER_S));
582 ar8216_sw_hw_apply(struct switch_dev *dev)
584 struct ar8216_priv *priv = to_ar8216(dev);
585 u8 portmask[AR8216_NUM_PORTS];
588 mutex_lock(&priv->reg_mutex);
589 /* flush all vlan translation unit entries */
590 priv->chip->vtu_flush(priv);
592 memset(portmask, 0, sizeof(portmask));
594 /* calculate the port destination masks and load vlans
595 * into the vlan translation unit */
596 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
597 u8 vp = priv->vlan_table[j];
602 for (i = 0; i < dev->ports; i++) {
605 portmask[i] |= vp & ~mask;
608 priv->chip->vtu_load_vlan(priv, priv->vlan_id[j],
609 priv->vlan_table[j]);
613 * isolate all ports, but connect them to the cpu port */
614 for (i = 0; i < dev->ports; i++) {
615 if (i == AR8216_PORT_CPU)
618 portmask[i] = 1 << AR8216_PORT_CPU;
619 portmask[AR8216_PORT_CPU] |= (1 << i);
623 /* update the port destination mask registers and tag settings */
624 for (i = 0; i < dev->ports; i++) {
629 pvid = priv->vlan_id[priv->pvid[i]];
630 if (priv->vlan_tagged & (1 << i))
631 egress = AR8216_OUT_ADD_VLAN;
633 egress = AR8216_OUT_STRIP_VLAN;
634 ingress = AR8216_IN_SECURE;
637 egress = AR8216_OUT_KEEP;
638 ingress = AR8216_IN_PORT_ONLY;
641 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
644 mutex_unlock(&priv->reg_mutex);
649 ar8216_hw_init(struct ar8216_priv *priv)
655 ar8236_hw_init(struct ar8216_priv *priv)
660 if (priv->initialized)
663 /* Initialize the PHYs */
664 bus = priv->phy->bus;
665 for (i = 0; i < 5; i++) {
666 mdiobus_write(bus, i, MII_ADVERTISE,
667 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
668 ADVERTISE_PAUSE_ASYM);
669 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
673 priv->initialized = true;
678 ar8316_hw_init(struct ar8216_priv *priv)
684 val = priv->read(priv, 0x8);
686 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
687 if (priv->port4_phy) {
688 /* value taken from Ubiquiti RouterStation Pro */
690 printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
693 printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
695 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
696 /* value taken from AVM Fritz!Box 7390 sources */
699 /* no known value for phy interface */
700 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
701 priv->phy->interface);
708 priv->write(priv, 0x8, newval);
710 /* Initialize the ports */
711 bus = priv->phy->bus;
712 for (i = 0; i < 5; i++) {
713 if ((i == 4) && priv->port4_phy &&
714 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
715 /* work around for phy4 rgmii mode */
716 ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
718 ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
720 ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
724 /* initialize the port itself */
725 mdiobus_write(bus, i, MII_ADVERTISE,
726 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
727 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
728 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
733 priv->initialized = true;
738 ar8216_init_globals(struct ar8216_priv *priv)
740 /* standard atheros magic */
741 priv->write(priv, 0x38, 0xc000050e);
743 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
744 AR8216_GCTRL_MTU, 1518 + 8 + 2);
748 ar8236_init_globals(struct ar8216_priv *priv)
750 /* enable jumbo frames */
751 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
752 AR8316_GCTRL_MTU, 9018 + 8 + 2);
756 ar8316_init_globals(struct ar8216_priv *priv)
758 /* standard atheros magic */
759 priv->write(priv, 0x38, 0xc000050e);
761 /* enable cpu port to receive multicast and broadcast frames */
762 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
764 /* enable jumbo frames */
765 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
766 AR8316_GCTRL_MTU, 9018 + 8 + 2);
770 ar8216_init_port(struct ar8216_priv *priv, int port)
772 /* Enable port learning and tx */
773 priv->write(priv, AR8216_REG_PORT_CTRL(port),
774 AR8216_PORT_CTRL_LEARN |
775 (4 << AR8216_PORT_CTRL_STATE_S));
777 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
779 if (port == AR8216_PORT_CPU) {
780 priv->write(priv, AR8216_REG_PORT_STATUS(port),
781 AR8216_PORT_STATUS_LINK_UP |
782 ar8xxx_has_gige(priv) ? AR8216_PORT_SPEED_1000M :
783 AR8216_PORT_SPEED_100M |
784 AR8216_PORT_STATUS_TXMAC |
785 AR8216_PORT_STATUS_RXMAC |
786 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
787 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
788 AR8216_PORT_STATUS_DUPLEX);
790 priv->write(priv, AR8216_REG_PORT_STATUS(port),
791 AR8216_PORT_STATUS_LINK_AUTO);
795 static const struct ar8xxx_chip ar8216_chip = {
796 .hw_init = ar8216_hw_init,
797 .init_globals = ar8216_init_globals,
798 .init_port = ar8216_init_port,
799 .setup_port = ar8216_setup_port,
800 .read_port_status = ar8216_read_port_status,
801 .atu_flush = ar8216_atu_flush,
802 .vtu_flush = ar8216_vtu_flush,
803 .vtu_load_vlan = ar8216_vtu_load_vlan,
806 static const struct ar8xxx_chip ar8236_chip = {
807 .hw_init = ar8236_hw_init,
808 .init_globals = ar8236_init_globals,
809 .init_port = ar8216_init_port,
810 .setup_port = ar8236_setup_port,
811 .read_port_status = ar8216_read_port_status,
812 .atu_flush = ar8216_atu_flush,
813 .vtu_flush = ar8216_vtu_flush,
814 .vtu_load_vlan = ar8216_vtu_load_vlan,
817 static const struct ar8xxx_chip ar8316_chip = {
818 .caps = AR8XXX_CAP_GIGE,
819 .hw_init = ar8316_hw_init,
820 .init_globals = ar8316_init_globals,
821 .init_port = ar8216_init_port,
822 .setup_port = ar8216_setup_port,
823 .read_port_status = ar8216_read_port_status,
824 .atu_flush = ar8216_atu_flush,
825 .vtu_flush = ar8216_vtu_flush,
826 .vtu_load_vlan = ar8216_vtu_load_vlan,
830 ar8216_sw_reset_switch(struct switch_dev *dev)
832 struct ar8216_priv *priv = to_ar8216(dev);
835 mutex_lock(&priv->reg_mutex);
836 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
837 offsetof(struct ar8216_priv, vlan));
839 for (i = 0; i < AR8X16_MAX_VLANS; i++)
840 priv->vlan_id[i] = i;
842 /* Configure all ports */
843 for (i = 0; i < dev->ports; i++)
844 priv->chip->init_port(priv, i);
846 priv->chip->init_globals(priv);
847 mutex_unlock(&priv->reg_mutex);
849 return ar8216_sw_hw_apply(dev);
852 static const struct switch_dev_ops ar8216_sw_ops = {
854 .attr = ar8216_globals,
855 .n_attr = ARRAY_SIZE(ar8216_globals),
859 .n_attr = ARRAY_SIZE(ar8216_port),
863 .n_attr = ARRAY_SIZE(ar8216_vlan),
865 .get_port_pvid = ar8216_sw_get_pvid,
866 .set_port_pvid = ar8216_sw_set_pvid,
867 .get_vlan_ports = ar8216_sw_get_ports,
868 .set_vlan_ports = ar8216_sw_set_ports,
869 .apply_config = ar8216_sw_hw_apply,
870 .reset_switch = ar8216_sw_reset_switch,
871 .get_port_link = ar8216_sw_get_port_link,
875 ar8216_id_chip(struct ar8216_priv *priv)
881 priv->chip_type = UNKNOWN;
883 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
887 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
888 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
891 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
895 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
902 priv->chip_type = AR8216;
903 priv->chip = &ar8216_chip;
906 priv->chip_type = AR8236;
907 priv->chip = &ar8236_chip;
911 priv->chip_type = AR8316;
912 priv->chip = &ar8316_chip;
916 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
917 (int)(id >> AR8216_CTRL_VERSION_S),
918 (int)(id & AR8216_CTRL_REVISION),
919 mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
920 mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
929 ar8216_config_init(struct phy_device *pdev)
931 struct ar8216_priv *priv = pdev->priv;
932 struct net_device *dev = pdev->attached_dev;
933 struct switch_dev *swdev;
937 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
944 ret = ar8216_id_chip(priv);
948 if (pdev->addr != 0) {
949 if (ar8xxx_has_gige(priv)) {
950 pdev->supported |= SUPPORTED_1000baseT_Full;
951 pdev->advertising |= ADVERTISED_1000baseT_Full;
954 if (priv->chip_type == AR8316) {
955 /* check if we're attaching to the switch twice */
956 pdev = pdev->bus->phy_map[0];
962 /* switch device has not been initialized, reuse priv */
964 priv->port4_phy = true;
971 /* switch device has been initialized, reinit */
973 priv->dev.ports = (AR8216_NUM_PORTS - 1);
974 priv->initialized = false;
975 priv->port4_phy = true;
976 ar8316_hw_init(priv);
984 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
985 pdev->attached_dev->name, priv->chip_type);
987 if (ar8xxx_has_gige(priv))
988 pdev->supported = SUPPORTED_1000baseT_Full;
990 pdev->supported = SUPPORTED_100baseT_Full;
991 pdev->advertising = pdev->supported;
993 mutex_init(&priv->reg_mutex);
994 priv->read = ar8216_mii_read;
995 priv->write = ar8216_mii_write;
1000 swdev->cpu_port = AR8216_PORT_CPU;
1001 swdev->ops = &ar8216_sw_ops;
1002 swdev->ports = AR8216_NUM_PORTS;
1004 if (priv->chip_type == AR8316) {
1005 swdev->name = "Atheros AR8316";
1006 swdev->vlans = AR8X16_MAX_VLANS;
1008 if (priv->port4_phy) {
1009 /* port 5 connected to the other mac, therefore unusable */
1010 swdev->ports = (AR8216_NUM_PORTS - 1);
1012 } else if (priv->chip_type == AR8236) {
1013 swdev->name = "Atheros AR8236";
1014 swdev->vlans = AR8216_NUM_VLANS;
1015 swdev->ports = AR8216_NUM_PORTS;
1017 swdev->name = "Atheros AR8216";
1018 swdev->vlans = AR8216_NUM_VLANS;
1021 ret = register_switch(&priv->dev, pdev->attached_dev);
1027 ret = priv->chip->hw_init(priv);
1031 ret = ar8216_sw_reset_switch(&priv->dev);
1035 dev->phy_ptr = priv;
1037 /* VID fixup only needed on ar8216 */
1038 if (pdev->addr == 0 && priv->chip_type == AR8216) {
1039 pdev->pkt_align = 2;
1040 pdev->netif_receive_skb = ar8216_netif_receive_skb;
1041 pdev->netif_rx = ar8216_netif_rx;
1042 priv->ndo_old = dev->netdev_ops;
1043 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
1044 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
1045 dev->netdev_ops = &priv->ndo;
1058 ar8216_read_status(struct phy_device *phydev)
1060 struct ar8216_priv *priv = phydev->priv;
1061 struct switch_port_link link;
1064 if (phydev->addr != 0)
1065 return genphy_read_status(phydev);
1067 ar8216_read_port_link(priv, phydev->addr, &link);
1068 phydev->link = !!link.link;
1072 switch (link.speed) {
1073 case SWITCH_PORT_SPEED_10:
1074 phydev->speed = SPEED_10;
1076 case SWITCH_PORT_SPEED_100:
1077 phydev->speed = SPEED_100;
1079 case SWITCH_PORT_SPEED_1000:
1080 phydev->speed = SPEED_1000;
1085 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1087 /* flush the address translation unit */
1088 mutex_lock(&priv->reg_mutex);
1089 ret = priv->chip->atu_flush(priv);
1090 mutex_unlock(&priv->reg_mutex);
1092 phydev->state = PHY_RUNNING;
1093 netif_carrier_on(phydev->attached_dev);
1094 phydev->adjust_link(phydev->attached_dev);
1100 ar8216_config_aneg(struct phy_device *phydev)
1102 if (phydev->addr == 0)
1105 return genphy_config_aneg(phydev);
1109 ar8216_probe(struct phy_device *pdev)
1111 struct ar8216_priv priv;
1114 return ar8216_id_chip(&priv);
1118 ar8216_remove(struct phy_device *pdev)
1120 struct ar8216_priv *priv = pdev->priv;
1121 struct net_device *dev = pdev->attached_dev;
1126 if (priv->ndo_old && dev)
1127 dev->netdev_ops = priv->ndo_old;
1128 if (pdev->addr == 0)
1129 unregister_switch(&priv->dev);
1133 static struct phy_driver ar8216_driver = {
1134 .phy_id = 0x004d0000,
1135 .name = "Atheros AR8216/AR8236/AR8316",
1136 .phy_id_mask = 0xffff0000,
1137 .features = PHY_BASIC_FEATURES,
1138 .probe = ar8216_probe,
1139 .remove = ar8216_remove,
1140 .config_init = &ar8216_config_init,
1141 .config_aneg = &ar8216_config_aneg,
1142 .read_status = &ar8216_read_status,
1143 .driver = { .owner = THIS_MODULE },
1149 return phy_driver_register(&ar8216_driver);
1155 phy_driver_unregister(&ar8216_driver);
1158 module_init(ar8216_init);
1159 module_exit(ar8216_exit);
1160 MODULE_LICENSE("GPL");