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
42 int (*hw_init)(struct ar8216_priv *priv);
43 void (*init_port)(struct ar8216_priv *priv, int port);
44 void (*setup_port)(struct ar8216_priv *priv, int port, u32 egress,
45 u32 ingress, u32 members, u32 pvid);
46 int (*atu_flush)(struct ar8216_priv *priv);
47 void (*vtu_flush)(struct ar8216_priv *priv);
51 struct switch_dev dev;
52 struct phy_device *phy;
53 u32 (*read)(struct ar8216_priv *priv, int reg);
54 void (*write)(struct ar8216_priv *priv, int reg, u32 val);
55 const struct net_device_ops *ndo_old;
56 struct net_device_ops ndo;
57 struct mutex reg_mutex;
59 const struct ar8xxx_chip *chip;
66 /* all fields below are cleared on reset */
68 u16 vlan_id[AR8X16_MAX_VLANS];
69 u8 vlan_table[AR8X16_MAX_VLANS];
71 u16 pvid[AR8216_NUM_PORTS];
74 #define to_ar8216(_dev) container_of(_dev, struct ar8216_priv, dev)
77 split_addr(u32 regaddr, u16 *r1, u16 *r2, u16 *page)
86 *page = regaddr & 0x1ff;
90 ar8216_mii_read(struct ar8216_priv *priv, int reg)
92 struct phy_device *phy = priv->phy;
93 struct mii_bus *bus = phy->bus;
97 split_addr((u32) reg, &r1, &r2, &page);
99 mutex_lock(&bus->mdio_lock);
101 bus->write(bus, 0x18, 0, page);
102 usleep_range(1000, 2000); /* wait for the page switch to propagate */
103 lo = bus->read(bus, 0x10 | r2, r1);
104 hi = bus->read(bus, 0x10 | r2, r1 + 1);
106 mutex_unlock(&bus->mdio_lock);
108 return (hi << 16) | lo;
112 ar8216_mii_write(struct ar8216_priv *priv, int reg, u32 val)
114 struct phy_device *phy = priv->phy;
115 struct mii_bus *bus = phy->bus;
119 split_addr((u32) reg, &r1, &r2, &r3);
121 hi = (u16) (val >> 16);
123 mutex_lock(&bus->mdio_lock);
125 bus->write(bus, 0x18, 0, r3);
126 usleep_range(1000, 2000); /* wait for the page switch to propagate */
127 bus->write(bus, 0x10 | r2, r1 + 1, hi);
128 bus->write(bus, 0x10 | r2, r1, lo);
130 mutex_unlock(&bus->mdio_lock);
134 ar8216_phy_dbg_write(struct ar8216_priv *priv, int phy_addr,
135 u16 dbg_addr, u16 dbg_data)
137 struct mii_bus *bus = priv->phy->bus;
139 mutex_lock(&bus->mdio_lock);
140 bus->write(bus, phy_addr, MII_ATH_DBG_ADDR, dbg_addr);
141 bus->write(bus, phy_addr, MII_ATH_DBG_DATA, dbg_data);
142 mutex_unlock(&bus->mdio_lock);
146 ar8216_rmw(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
150 lockdep_assert_held(&priv->reg_mutex);
152 v = priv->read(priv, reg);
155 priv->write(priv, reg, v);
161 ar8216_read_port_link(struct ar8216_priv *priv, int port,
162 struct switch_port_link *link)
167 memset(link, '\0', sizeof(*link));
169 status = priv->read(priv, AR8216_REG_PORT_STATUS(port));
171 link->aneg = !!(status & AR8216_PORT_STATUS_LINK_AUTO);
173 link->link = !!(status & AR8216_PORT_STATUS_LINK_UP);
180 link->duplex = !!(status & AR8216_PORT_STATUS_DUPLEX);
181 link->tx_flow = !!(status & AR8216_PORT_STATUS_TXFLOW);
182 link->rx_flow = !!(status & AR8216_PORT_STATUS_RXFLOW);
184 speed = (status & AR8216_PORT_STATUS_SPEED) >>
185 AR8216_PORT_STATUS_SPEED_S;
188 case AR8216_PORT_SPEED_10M:
189 link->speed = SWITCH_PORT_SPEED_10;
191 case AR8216_PORT_SPEED_100M:
192 link->speed = SWITCH_PORT_SPEED_100;
194 case AR8216_PORT_SPEED_1000M:
195 link->speed = SWITCH_PORT_SPEED_1000;
198 link->speed = SWITCH_PORT_SPEED_UNKNOWN;
204 ar8216_set_vlan(struct switch_dev *dev, const struct switch_attr *attr,
205 struct switch_val *val)
207 struct ar8216_priv *priv = to_ar8216(dev);
208 priv->vlan = !!val->value.i;
213 ar8216_get_vlan(struct switch_dev *dev, const struct switch_attr *attr,
214 struct switch_val *val)
216 struct ar8216_priv *priv = to_ar8216(dev);
217 val->value.i = priv->vlan;
223 ar8216_set_pvid(struct switch_dev *dev, int port, int vlan)
225 struct ar8216_priv *priv = to_ar8216(dev);
227 /* make sure no invalid PVIDs get set */
229 if (vlan >= dev->vlans)
232 priv->pvid[port] = vlan;
237 ar8216_get_pvid(struct switch_dev *dev, int port, int *vlan)
239 struct ar8216_priv *priv = to_ar8216(dev);
240 *vlan = priv->pvid[port];
245 ar8216_set_vid(struct switch_dev *dev, const struct switch_attr *attr,
246 struct switch_val *val)
248 struct ar8216_priv *priv = to_ar8216(dev);
249 priv->vlan_id[val->port_vlan] = val->value.i;
254 ar8216_get_vid(struct switch_dev *dev, const struct switch_attr *attr,
255 struct switch_val *val)
257 struct ar8216_priv *priv = to_ar8216(dev);
258 val->value.i = priv->vlan_id[val->port_vlan];
263 ar8216_get_port_link(struct switch_dev *dev, int port,
264 struct switch_port_link *link)
266 struct ar8216_priv *priv = to_ar8216(dev);
268 ar8216_read_port_link(priv, port, link);
273 ar8216_mangle_tx(struct sk_buff *skb, struct net_device *dev)
275 struct ar8216_priv *priv = dev->phy_ptr;
284 if (unlikely(skb_headroom(skb) < 2)) {
285 if (pskb_expand_head(skb, 2, 0, GFP_ATOMIC) < 0)
289 buf = skb_push(skb, 2);
294 return priv->ndo_old->ndo_start_xmit(skb, dev);
297 dev_kfree_skb_any(skb);
302 ar8216_mangle_rx(struct sk_buff *skb, int napi)
304 struct ar8216_priv *priv;
305 struct net_device *dev;
317 /* don't strip the header if vlan mode is disabled */
321 /* strip header, get vlan id */
325 /* check for vlan header presence */
326 if ((buf[12 + 2] != 0x81) || (buf[13 + 2] != 0x00))
331 /* no need to fix up packets coming from a tagged source */
332 if (priv->vlan_tagged & (1 << port))
335 /* lookup port vid from local table, the switch passes an invalid vlan id */
336 vlan = priv->vlan_id[priv->pvid[port]];
339 buf[14 + 2] |= vlan >> 8;
340 buf[15 + 2] = vlan & 0xff;
343 skb->protocol = eth_type_trans(skb, skb->dev);
346 return netif_receive_skb(skb);
348 return netif_rx(skb);
351 /* no vlan? eat the packet! */
352 dev_kfree_skb_any(skb);
357 ar8216_netif_rx(struct sk_buff *skb)
359 return ar8216_mangle_rx(skb, 0);
363 ar8216_netif_receive_skb(struct sk_buff *skb)
365 return ar8216_mangle_rx(skb, 1);
369 static struct switch_attr ar8216_globals[] = {
371 .type = SWITCH_TYPE_INT,
372 .name = "enable_vlan",
373 .description = "Enable VLAN mode",
374 .set = ar8216_set_vlan,
375 .get = ar8216_get_vlan,
380 static struct switch_attr ar8216_port[] = {
383 static struct switch_attr ar8216_vlan[] = {
385 .type = SWITCH_TYPE_INT,
387 .description = "VLAN ID (0-4094)",
388 .set = ar8216_set_vid,
389 .get = ar8216_get_vid,
396 ar8216_get_ports(struct switch_dev *dev, struct switch_val *val)
398 struct ar8216_priv *priv = to_ar8216(dev);
399 u8 ports = priv->vlan_table[val->port_vlan];
403 for (i = 0; i < AR8216_NUM_PORTS; i++) {
404 struct switch_port *p;
406 if (!(ports & (1 << i)))
409 p = &val->value.ports[val->len++];
411 if (priv->vlan_tagged & (1 << i))
412 p->flags = (1 << SWITCH_PORT_FLAG_TAGGED);
420 ar8216_set_ports(struct switch_dev *dev, struct switch_val *val)
422 struct ar8216_priv *priv = to_ar8216(dev);
423 u8 *vt = &priv->vlan_table[val->port_vlan];
427 for (i = 0; i < val->len; i++) {
428 struct switch_port *p = &val->value.ports[i];
430 if (p->flags & (1 << SWITCH_PORT_FLAG_TAGGED)) {
431 priv->vlan_tagged |= (1 << p->id);
433 priv->vlan_tagged &= ~(1 << p->id);
434 priv->pvid[p->id] = val->port_vlan;
436 /* make sure that an untagged port does not
437 * appear in other vlans */
438 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
439 if (j == val->port_vlan)
441 priv->vlan_table[j] &= ~(1 << p->id);
451 ar8216_wait_bit(struct ar8216_priv *priv, int reg, u32 mask, u32 val)
457 t = priv->read(priv, reg);
458 if ((t & mask) == val)
467 pr_err("ar8216: timeout on reg %08x: %08x & %08x != %08x\n",
468 (unsigned int) reg, t, mask, val);
473 ar8216_vtu_op(struct ar8216_priv *priv, u32 op, u32 val)
475 if (ar8216_wait_bit(priv, AR8216_REG_VTU, AR8216_VTU_ACTIVE, 0))
477 if ((op & AR8216_VTU_OP) == AR8216_VTU_OP_LOAD) {
478 val &= AR8216_VTUDATA_MEMBER;
479 val |= AR8216_VTUDATA_VALID;
480 priv->write(priv, AR8216_REG_VTU_DATA, val);
482 op |= AR8216_VTU_ACTIVE;
483 priv->write(priv, AR8216_REG_VTU, op);
487 ar8216_vtu_flush(struct ar8216_priv *priv)
489 ar8216_vtu_op(priv, AR8216_VTU_OP_FLUSH, 0);
493 ar8216_atu_flush(struct ar8216_priv *priv)
497 ret = ar8216_wait_bit(priv, AR8216_REG_ATU, AR8216_ATU_ACTIVE, 0);
499 priv->write(priv, AR8216_REG_ATU, AR8216_ATU_OP_FLUSH);
505 ar8216_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
506 u32 members, u32 pvid)
510 if (priv->vlan && port == AR8216_PORT_CPU && priv->chip_type == AR8216)
511 header = AR8216_PORT_CTRL_HEADER;
515 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
516 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
517 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
518 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
519 AR8216_PORT_CTRL_LEARN | header |
520 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
521 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
523 ar8216_rmw(priv, AR8216_REG_PORT_VLAN(port),
524 AR8216_PORT_VLAN_DEST_PORTS | AR8216_PORT_VLAN_MODE |
525 AR8216_PORT_VLAN_DEFAULT_ID,
526 (members << AR8216_PORT_VLAN_DEST_PORTS_S) |
527 (ingress << AR8216_PORT_VLAN_MODE_S) |
528 (pvid << AR8216_PORT_VLAN_DEFAULT_ID_S));
532 ar8236_setup_port(struct ar8216_priv *priv, int port, u32 egress, u32 ingress,
533 u32 members, u32 pvid)
535 ar8216_rmw(priv, AR8216_REG_PORT_CTRL(port),
536 AR8216_PORT_CTRL_LEARN | AR8216_PORT_CTRL_VLAN_MODE |
537 AR8216_PORT_CTRL_SINGLE_VLAN | AR8216_PORT_CTRL_STATE |
538 AR8216_PORT_CTRL_HEADER | AR8216_PORT_CTRL_LEARN_LOCK,
539 AR8216_PORT_CTRL_LEARN |
540 (egress << AR8216_PORT_CTRL_VLAN_MODE_S) |
541 (AR8216_PORT_STATE_FORWARD << AR8216_PORT_CTRL_STATE_S));
543 ar8216_rmw(priv, AR8236_REG_PORT_VLAN(port),
544 AR8236_PORT_VLAN_DEFAULT_ID,
545 (pvid << AR8236_PORT_VLAN_DEFAULT_ID_S));
547 ar8216_rmw(priv, AR8236_REG_PORT_VLAN2(port),
548 AR8236_PORT_VLAN2_VLAN_MODE |
549 AR8236_PORT_VLAN2_MEMBER,
550 (ingress << AR8236_PORT_VLAN2_VLAN_MODE_S) |
551 (members << AR8236_PORT_VLAN2_MEMBER_S));
555 ar8216_hw_apply(struct switch_dev *dev)
557 struct ar8216_priv *priv = to_ar8216(dev);
558 u8 portmask[AR8216_NUM_PORTS];
561 mutex_lock(&priv->reg_mutex);
562 /* flush all vlan translation unit entries */
563 priv->chip->vtu_flush(priv);
565 memset(portmask, 0, sizeof(portmask));
567 /* calculate the port destination masks and load vlans
568 * into the vlan translation unit */
569 for (j = 0; j < AR8X16_MAX_VLANS; j++) {
570 u8 vp = priv->vlan_table[j];
575 for (i = 0; i < AR8216_NUM_PORTS; i++) {
578 portmask[i] |= vp & ~mask;
583 (priv->vlan_id[j] << AR8216_VTU_VID_S),
584 priv->vlan_table[j]);
588 * isolate all ports, but connect them to the cpu port */
589 for (i = 0; i < AR8216_NUM_PORTS; i++) {
590 if (i == AR8216_PORT_CPU)
593 portmask[i] = 1 << AR8216_PORT_CPU;
594 portmask[AR8216_PORT_CPU] |= (1 << i);
598 /* update the port destination mask registers and tag settings */
599 for (i = 0; i < AR8216_NUM_PORTS; i++) {
604 pvid = priv->vlan_id[priv->pvid[i]];
605 if (priv->vlan_tagged & (1 << i))
606 egress = AR8216_OUT_ADD_VLAN;
608 egress = AR8216_OUT_STRIP_VLAN;
609 ingress = AR8216_IN_SECURE;
612 egress = AR8216_OUT_KEEP;
613 ingress = AR8216_IN_PORT_ONLY;
616 priv->chip->setup_port(priv, i, egress, ingress, portmask[i],
619 mutex_unlock(&priv->reg_mutex);
624 ar8216_hw_init(struct ar8216_priv *priv)
630 ar8236_hw_init(struct ar8216_priv *priv)
635 if (priv->initialized)
638 /* Initialize the PHYs */
639 bus = priv->phy->bus;
640 for (i = 0; i < 5; i++) {
641 mdiobus_write(bus, i, MII_ADVERTISE,
642 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP |
643 ADVERTISE_PAUSE_ASYM);
644 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
648 priv->initialized = true;
653 ar8316_hw_init(struct ar8216_priv *priv)
659 val = priv->read(priv, 0x8);
661 if (priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
662 if (priv->port4_phy) {
663 /* value taken from Ubiquiti RouterStation Pro */
665 printk(KERN_INFO "ar8316: Using port 4 as PHY\n");
668 printk(KERN_INFO "ar8316: Using port 4 as switch port\n");
670 } else if (priv->phy->interface == PHY_INTERFACE_MODE_GMII) {
671 /* value taken from AVM Fritz!Box 7390 sources */
674 /* no known value for phy interface */
675 printk(KERN_ERR "ar8316: unsupported mii mode: %d.\n",
676 priv->phy->interface);
683 priv->write(priv, 0x8, newval);
685 /* Initialize the ports */
686 bus = priv->phy->bus;
687 for (i = 0; i < 5; i++) {
688 if ((i == 4) && priv->port4_phy &&
689 priv->phy->interface == PHY_INTERFACE_MODE_RGMII) {
690 /* work around for phy4 rgmii mode */
691 ar8216_phy_dbg_write(priv, i, 0x12, 0x480c);
693 ar8216_phy_dbg_write(priv, i, 0x0, 0x824e);
695 ar8216_phy_dbg_write(priv, i, 0x5, 0x3d47);
699 /* initialize the port itself */
700 mdiobus_write(bus, i, MII_ADVERTISE,
701 ADVERTISE_ALL | ADVERTISE_PAUSE_CAP | ADVERTISE_PAUSE_ASYM);
702 mdiobus_write(bus, i, MII_CTRL1000, ADVERTISE_1000FULL);
703 mdiobus_write(bus, i, MII_BMCR, BMCR_RESET | BMCR_ANENABLE);
708 priv->initialized = true;
713 ar8216_init_globals(struct ar8216_priv *priv)
715 switch (priv->chip_type) {
717 /* standard atheros magic */
718 priv->write(priv, 0x38, 0xc000050e);
720 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
721 AR8216_GCTRL_MTU, 1518 + 8 + 2);
724 /* standard atheros magic */
725 priv->write(priv, 0x38, 0xc000050e);
727 /* enable cpu port to receive multicast and broadcast frames */
728 priv->write(priv, AR8216_REG_FLOOD_MASK, 0x003f003f);
732 /* enable jumbo frames */
733 ar8216_rmw(priv, AR8216_REG_GLOBAL_CTRL,
734 AR8316_GCTRL_MTU, 9018 + 8 + 2);
740 ar8216_init_port(struct ar8216_priv *priv, int port)
742 /* Enable port learning and tx */
743 priv->write(priv, AR8216_REG_PORT_CTRL(port),
744 AR8216_PORT_CTRL_LEARN |
745 (4 << AR8216_PORT_CTRL_STATE_S));
747 priv->write(priv, AR8216_REG_PORT_VLAN(port), 0);
749 if (port == AR8216_PORT_CPU) {
750 priv->write(priv, AR8216_REG_PORT_STATUS(port),
751 AR8216_PORT_STATUS_LINK_UP |
752 ((priv->chip_type == AR8316) ?
753 AR8216_PORT_SPEED_1000M : AR8216_PORT_SPEED_100M) |
754 AR8216_PORT_STATUS_TXMAC |
755 AR8216_PORT_STATUS_RXMAC |
756 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_RXFLOW : 0) |
757 ((priv->chip_type == AR8316) ? AR8216_PORT_STATUS_TXFLOW : 0) |
758 AR8216_PORT_STATUS_DUPLEX);
760 priv->write(priv, AR8216_REG_PORT_STATUS(port),
761 AR8216_PORT_STATUS_LINK_AUTO);
765 static const struct ar8xxx_chip ar8216_chip = {
766 .hw_init = ar8216_hw_init,
767 .init_port = ar8216_init_port,
768 .setup_port = ar8216_setup_port,
769 .atu_flush = ar8216_atu_flush,
770 .vtu_flush = ar8216_vtu_flush,
773 static const struct ar8xxx_chip ar8236_chip = {
774 .hw_init = ar8236_hw_init,
775 .init_port = ar8216_init_port,
776 .setup_port = ar8236_setup_port,
777 .atu_flush = ar8216_atu_flush,
778 .vtu_flush = ar8216_vtu_flush,
781 static const struct ar8xxx_chip ar8316_chip = {
782 .hw_init = ar8316_hw_init,
783 .init_port = ar8216_init_port,
784 .setup_port = ar8216_setup_port,
785 .atu_flush = ar8216_atu_flush,
786 .vtu_flush = ar8216_vtu_flush,
790 ar8216_reset_switch(struct switch_dev *dev)
792 struct ar8216_priv *priv = to_ar8216(dev);
795 mutex_lock(&priv->reg_mutex);
796 memset(&priv->vlan, 0, sizeof(struct ar8216_priv) -
797 offsetof(struct ar8216_priv, vlan));
799 for (i = 0; i < AR8X16_MAX_VLANS; i++)
800 priv->vlan_id[i] = i;
802 /* Configure all ports */
803 for (i = 0; i < AR8216_NUM_PORTS; i++)
804 priv->chip->init_port(priv, i);
806 ar8216_init_globals(priv);
807 mutex_unlock(&priv->reg_mutex);
809 return ar8216_hw_apply(dev);
812 static const struct switch_dev_ops ar8216_sw_ops = {
814 .attr = ar8216_globals,
815 .n_attr = ARRAY_SIZE(ar8216_globals),
819 .n_attr = ARRAY_SIZE(ar8216_port),
823 .n_attr = ARRAY_SIZE(ar8216_vlan),
825 .get_port_pvid = ar8216_get_pvid,
826 .set_port_pvid = ar8216_set_pvid,
827 .get_vlan_ports = ar8216_get_ports,
828 .set_vlan_ports = ar8216_set_ports,
829 .apply_config = ar8216_hw_apply,
830 .reset_switch = ar8216_reset_switch,
831 .get_port_link = ar8216_get_port_link,
835 ar8216_id_chip(struct ar8216_priv *priv)
841 priv->chip_type = UNKNOWN;
843 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
847 id = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
848 for (i = 0; i < AR8X16_PROBE_RETRIES; i++) {
851 val = ar8216_mii_read(priv, AR8216_REG_CTRL);
855 t = val & (AR8216_CTRL_REVISION | AR8216_CTRL_VERSION);
862 priv->chip_type = AR8216;
863 priv->chip = &ar8216_chip;
866 priv->chip_type = AR8236;
867 priv->chip = &ar8236_chip;
871 priv->chip_type = AR8316;
872 priv->chip = &ar8316_chip;
876 "ar8216: Unknown Atheros device [ver=%d, rev=%d, phy_id=%04x%04x]\n",
877 (int)(id >> AR8216_CTRL_VERSION_S),
878 (int)(id & AR8216_CTRL_REVISION),
879 mdiobus_read(priv->phy->bus, priv->phy->addr, 2),
880 mdiobus_read(priv->phy->bus, priv->phy->addr, 3));
889 ar8216_config_init(struct phy_device *pdev)
891 struct ar8216_priv *priv = pdev->priv;
892 struct net_device *dev = pdev->attached_dev;
893 struct switch_dev *swdev;
897 priv = kzalloc(sizeof(struct ar8216_priv), GFP_KERNEL);
904 ret = ar8216_id_chip(priv);
908 if (pdev->addr != 0) {
909 if (priv->chip_type == AR8316) {
910 pdev->supported |= SUPPORTED_1000baseT_Full;
911 pdev->advertising |= ADVERTISED_1000baseT_Full;
913 /* check if we're attaching to the switch twice */
914 pdev = pdev->bus->phy_map[0];
920 /* switch device has not been initialized, reuse priv */
922 priv->port4_phy = true;
929 /* switch device has been initialized, reinit */
931 priv->dev.ports = (AR8216_NUM_PORTS - 1);
932 priv->initialized = false;
933 priv->port4_phy = true;
934 ar8316_hw_init(priv);
942 printk(KERN_INFO "%s: AR%d switch driver attached.\n",
943 pdev->attached_dev->name, priv->chip_type);
945 pdev->supported = priv->chip_type == AR8316 ?
946 SUPPORTED_1000baseT_Full : SUPPORTED_100baseT_Full;
947 pdev->advertising = pdev->supported;
949 mutex_init(&priv->reg_mutex);
950 priv->read = ar8216_mii_read;
951 priv->write = ar8216_mii_write;
956 swdev->cpu_port = AR8216_PORT_CPU;
957 swdev->ops = &ar8216_sw_ops;
958 swdev->ports = AR8216_NUM_PORTS;
960 if (priv->chip_type == AR8316) {
961 swdev->name = "Atheros AR8316";
962 swdev->vlans = AR8X16_MAX_VLANS;
964 if (priv->port4_phy) {
965 /* port 5 connected to the other mac, therefore unusable */
966 swdev->ports = (AR8216_NUM_PORTS - 1);
968 } else if (priv->chip_type == AR8236) {
969 swdev->name = "Atheros AR8236";
970 swdev->vlans = AR8216_NUM_VLANS;
971 swdev->ports = AR8216_NUM_PORTS;
973 swdev->name = "Atheros AR8216";
974 swdev->vlans = AR8216_NUM_VLANS;
977 ret = register_switch(&priv->dev, pdev->attached_dev);
983 ret = priv->chip->hw_init(priv);
987 ret = ar8216_reset_switch(&priv->dev);
993 /* VID fixup only needed on ar8216 */
994 if (pdev->addr == 0 && priv->chip_type == AR8216) {
996 pdev->netif_receive_skb = ar8216_netif_receive_skb;
997 pdev->netif_rx = ar8216_netif_rx;
998 priv->ndo_old = dev->netdev_ops;
999 memcpy(&priv->ndo, priv->ndo_old, sizeof(struct net_device_ops));
1000 priv->ndo.ndo_start_xmit = ar8216_mangle_tx;
1001 dev->netdev_ops = &priv->ndo;
1014 ar8216_read_status(struct phy_device *phydev)
1016 struct ar8216_priv *priv = phydev->priv;
1017 struct switch_port_link link;
1020 if (phydev->addr != 0)
1021 return genphy_read_status(phydev);
1023 ar8216_read_port_link(priv, phydev->addr, &link);
1024 phydev->link = !!link.link;
1028 switch (link.speed) {
1029 case SWITCH_PORT_SPEED_10:
1030 phydev->speed = SPEED_10;
1032 case SWITCH_PORT_SPEED_100:
1033 phydev->speed = SPEED_100;
1035 case SWITCH_PORT_SPEED_1000:
1036 phydev->speed = SPEED_1000;
1041 phydev->duplex = link.duplex ? DUPLEX_FULL : DUPLEX_HALF;
1043 /* flush the address translation unit */
1044 mutex_lock(&priv->reg_mutex);
1045 ret = priv->chip->atu_flush(priv);
1046 mutex_unlock(&priv->reg_mutex);
1048 phydev->state = PHY_RUNNING;
1049 netif_carrier_on(phydev->attached_dev);
1050 phydev->adjust_link(phydev->attached_dev);
1056 ar8216_config_aneg(struct phy_device *phydev)
1058 if (phydev->addr == 0)
1061 return genphy_config_aneg(phydev);
1065 ar8216_probe(struct phy_device *pdev)
1067 struct ar8216_priv priv;
1070 return ar8216_id_chip(&priv);
1074 ar8216_remove(struct phy_device *pdev)
1076 struct ar8216_priv *priv = pdev->priv;
1077 struct net_device *dev = pdev->attached_dev;
1082 if (priv->ndo_old && dev)
1083 dev->netdev_ops = priv->ndo_old;
1084 if (pdev->addr == 0)
1085 unregister_switch(&priv->dev);
1089 static struct phy_driver ar8216_driver = {
1090 .phy_id = 0x004d0000,
1091 .name = "Atheros AR8216/AR8236/AR8316",
1092 .phy_id_mask = 0xffff0000,
1093 .features = PHY_BASIC_FEATURES,
1094 .probe = ar8216_probe,
1095 .remove = ar8216_remove,
1096 .config_init = &ar8216_config_init,
1097 .config_aneg = &ar8216_config_aneg,
1098 .read_status = &ar8216_read_status,
1099 .driver = { .owner = THIS_MODULE },
1105 return phy_driver_register(&ar8216_driver);
1111 phy_driver_unregister(&ar8216_driver);
1114 module_init(ar8216_init);
1115 module_exit(ar8216_exit);
1116 MODULE_LICENSE("GPL");