ar71xx: introduce ar71xx_switch_data
[openwrt/svn-archive/archive.git] / target / linux / ar71xx / files / drivers / net / ag71xx / ag71xx_ar7240.c
index fec03a671ffb229360cd1dbaac9f985838cab009..e96fe3626165be2f6019fd1d621d59a6d4c9e130 100644 (file)
 #define AR7240_PHY_ID1         0x004d
 #define AR7240_PHY_ID2         0xd041
 
-#define AR7240_PORT_MASK(_port)                BIT((_port))
-#define AR7240_PORT_MASK_ALL           BITM(AR7240_NUM_PORTS)
-#define AR7240_PORT_MASK_BUT(_port)    (AR7240_PORT_MASK_ALL & ~BIT((_port)))
-
 #define AR7240_MAX_VLANS       16
 
 #define sw_to_ar7240(_dev) container_of(_dev, struct ar7240sw, swdev)
 
 struct ar7240sw {
        struct mii_bus  *mii_bus;
+       struct ag71xx_switch_platform_data *swdata;
        struct switch_dev swdev;
+       int num_ports;
        bool vlan;
        u16 vlan_id[AR7240_MAX_VLANS];
        u8 vlan_table[AR7240_MAX_VLANS];
@@ -219,6 +217,21 @@ struct ar7240sw_hw_stat {
 
 static DEFINE_MUTEX(reg_mutex);
 
+static inline u32 ar7240sw_port_mask(struct ar7240sw *as, int port)
+{
+       return BIT(port);
+}
+
+static inline u32 ar7240sw_port_mask_all(struct ar7240sw *as)
+{
+       return BIT(as->swdev.ports) - 1;
+}
+
+static inline u32 ar7240sw_port_mask_but(struct ar7240sw *as, int port)
+{
+       return ar7240sw_port_mask_all(as) & ~BIT(port);
+}
+
 static inline u16 mk_phy_addr(u32 reg)
 {
        return 0x17 & ((reg >> 4) | 0x10);
@@ -498,15 +511,15 @@ static void ar7240sw_setup_port(struct ar7240sw *as, unsigned port, u8 portmask)
 
        if (!portmask) {
                if (port == AR7240_PORT_CPU)
-                       portmask = AR7240_PORT_MASK_BUT(AR7240_PORT_CPU);
+                       portmask = ar7240sw_port_mask_but(as, AR7240_PORT_CPU);
                else
-                       portmask = AR7240_PORT_MASK(AR7240_PORT_CPU);
+                       portmask = ar7240sw_port_mask(as, AR7240_PORT_CPU);
        }
 
        /* allow the port to talk to all other ports, but exclude its
         * own ID to prevent frames from being reflected back to the
         * port that they came from */
-       portmask &= AR7240_PORT_MASK_BUT(port);
+       portmask &= ar7240sw_port_mask_but(as, port);
 
        /* set default VID and and destination ports for this VLAN */
        vlan |= (portmask << AR7240_PORT_VLAN_DEST_PORTS_S);
@@ -577,7 +590,7 @@ ar7240_get_ports(struct switch_dev *dev, struct switch_val *val)
        int i;
 
        val->len = 0;
-       for (i = 0; i < AR7240_NUM_PORTS; i++) {
+       for (i = 0; i < as->swdev.ports; i++) {
                struct switch_port *p;
 
                if (!(ports & (1 << i)))
@@ -737,7 +750,7 @@ ar7240_hw_apply(struct switch_dev *dev)
                        if (!vp)
                                continue;
 
-                       for (i = 0; i < AR7240_NUM_PORTS; i++) {
+                       for (i = 0; i < as->swdev.ports; i++) {
                                u8 mask = (1 << i);
                                if (vp & mask)
                                        portmask[i] |= vp & ~mask;
@@ -751,7 +764,7 @@ ar7240_hw_apply(struct switch_dev *dev)
        } else {
                /* vlan disabled:
                 * isolate all ports, but connect them to the cpu port */
-               for (i = 0; i < AR7240_NUM_PORTS; i++) {
+               for (i = 0; i < as->swdev.ports; i++) {
                        if (i == AR7240_PORT_CPU)
                                continue;
 
@@ -761,7 +774,7 @@ ar7240_hw_apply(struct switch_dev *dev)
        }
 
        /* update the port destination mask registers and tag settings */
-       for (i = 0; i < AR7240_NUM_PORTS; i++)
+       for (i = 0; i < as->swdev.ports; i++)
                ar7240sw_setup_port(as, i, portmask[i]);
 
        return 0;
@@ -831,6 +844,7 @@ static const struct switch_dev_ops ar7240_ops = {
 
 static struct ar7240sw *ar7240_probe(struct ag71xx *ag)
 {
+       struct ag71xx_platform_data *pdata = ag71xx_get_pdata(ag);
        struct mii_bus *mii = ag->mii_bus;
        struct ar7240sw *as;
        struct switch_dev *swdev;
@@ -840,14 +854,22 @@ static struct ar7240sw *ar7240_probe(struct ag71xx *ag)
        u8 ver;
        int i;
 
+       phy_id1 = ar7240sw_phy_read(mii, 0, MII_PHYSID1);
+       phy_id2 = ar7240sw_phy_read(mii, 0, MII_PHYSID2);
+       if (phy_id1 != AR7240_PHY_ID1 || phy_id2 != AR7240_PHY_ID2) {
+               pr_err("%s: unknown phy id '%04x:%04x'\n",
+                      ag->dev->name, phy_id1, phy_id2);
+               return NULL;
+       }
+
        as = kzalloc(sizeof(*as), GFP_KERNEL);
        if (!as)
                return NULL;
 
        as->mii_bus = mii;
+       as->swdata = pdata->switch_data;
 
        ctrl = ar7240sw_reg_read(mii, AR7240_REG_MASK_CTRL);
-
        ver = (ctrl >> AR7240_MASK_CTRL_VERSION_S) & AR7240_MASK_CTRL_VERSION_M;
        if (ver != 1) {
                pr_err("%s: unsupported chip, ctrl=%08x\n",
@@ -855,17 +877,9 @@ static struct ar7240sw *ar7240_probe(struct ag71xx *ag)
                return NULL;
        }
 
-       phy_id1 = ar7240sw_phy_read(mii, 0, MII_PHYSID1);
-       phy_id2 = ar7240sw_phy_read(mii, 0, MII_PHYSID2);
-       if (phy_id1 != AR7240_PHY_ID1 || phy_id2 != AR7240_PHY_ID2) {
-               pr_err("%s: unknown phy id '%04x:%04x'\n",
-                      ag->dev->name, phy_id1, phy_id2);
-               return NULL;
-       }
-
        swdev = &as->swdev;
        swdev->name = "AR7240 built-in switch";
-       swdev->ports = AR7240_NUM_PORTS;
+       swdev->ports = AR7240_NUM_PORTS - 1;
        swdev->cpu_port = AR7240_PORT_CPU;
        swdev->vlans = AR7240_MAX_VLANS;
        swdev->ops = &ar7240_ops;
@@ -881,7 +895,7 @@ static struct ar7240sw *ar7240_probe(struct ag71xx *ag)
        for (i = 0; i < AR7240_MAX_VLANS; i++)
                as->vlan_id[i] = i;
 
-       as->vlan_table[0] = AR7240_PORT_MASK_ALL;
+       as->vlan_table[0] = ar7240sw_port_mask_all(as);
 
        return as;
 }