backfire: generic: rtl8366: move mii bus handling to the rtl8366_smi code (backport...
[10.03/openwrt.git] / target / linux / generic-2.6 / files / drivers / net / phy / rtl8366s.c
index c8e7dd6..9f37865 100644 (file)
@@ -16,7 +16,6 @@
 #include <linux/delay.h>
 #include <linux/skbuff.h>
 #include <linux/switch.h>
-#include <linux/phy.h>
 #include <linux/rtl8366s.h>
 
 #include "rtl8366_smi.h"
@@ -26,7 +25,7 @@
 #endif
 
 #define RTL8366S_DRIVER_DESC   "Realtek RTL8366S ethernet switch driver"
-#define RTL8366S_DRIVER_VER    "0.2.1"
+#define RTL8366S_DRIVER_VER    "0.2.2"
 
 #define RTL8366S_PHY_NO_MAX                 4
 #define RTL8366S_PHY_PAGE_MAX               7
 struct rtl8366s {
        struct device           *parent;
        struct rtl8366_smi      smi;
-       struct mii_bus          *mii_bus;
-       int                     mii_irq[PHY_MAX_ADDR];
        struct switch_dev       dev;
        char                    buf[4096];
 #ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
@@ -210,21 +207,26 @@ static struct mib_counter rtl8366s_mib_counters[RTL8366S_MIB_COUNT] = {
        { 38, 2, "Dot3StatsSymbolErrors             " },
        { 40, 2, "Dot3InPauseFrames                 " },
        { 42, 2, "Dot3ControlInUnknownOpcodes       " },
-       { 44, 2, "IfOutOctets                       " },
-       { 46, 2, "Dot3StatsSingleCollisionFrames    " },
-       { 48, 2, "Dot3StatMultipleCollisionFrames   " },
-       { 50, 2, "Dot3sDeferredTransmissions        " },
-       { 52, 2, "Dot3StatsLateCollisions           " },
-       { 54, 2, "EtherStatsCollisions              " },
-       { 56, 2, "Dot3StatsExcessiveCollisions      " },
-       { 58, 2, "Dot3OutPauseFrames                " },
-       { 60, 2, "Dot1dBasePortDelayExceededDiscards" },
-       { 62, 2, "Dot1dTpPortInDiscards             " },
-       { 64, 2, "IfOutUcastPkts                    " },
-       { 66, 2, "IfOutMulticastPkts                " },
-       { 68, 2, "IfOutBroadcastPkts                " },
+       { 44, 4, "IfOutOctets                       " },
+       { 48, 2, "Dot3StatsSingleCollisionFrames    " },
+       { 50, 2, "Dot3StatMultipleCollisionFrames   " },
+       { 52, 2, "Dot3sDeferredTransmissions        " },
+       { 54, 2, "Dot3StatsLateCollisions           " },
+       { 56, 2, "EtherStatsCollisions              " },
+       { 58, 2, "Dot3StatsExcessiveCollisions      " },
+       { 60, 2, "Dot3OutPauseFrames                " },
+       { 62, 2, "Dot1dBasePortDelayExceededDiscards" },
+       { 64, 2, "Dot1dTpPortInDiscards             " },
+       { 66, 2, "IfOutUcastPkts                    " },
+       { 68, 2, "IfOutMulticastPkts                " },
+       { 70, 2, "IfOutBroadcastPkts                " },
 };
 
+static inline struct rtl8366s *smi_to_rtl8366s(struct rtl8366_smi *smi)
+{
+       return container_of(smi, struct rtl8366s, smi);
+}
+
 static inline struct rtl8366s *sw_to_rtl8366s(struct switch_dev *sw)
 {
        return container_of(sw, struct rtl8366s, dev);
@@ -922,30 +924,32 @@ static void rtl8366s_debugfs_init(struct rtl8366s *rtl)
 
        node = debugfs_create_x16("reg", S_IRUGO | S_IWUSR, root, &g_dbg_reg);
        if (!node) {
-               dev_err(rtl->parent, "Creating debugfs file reg failed\n");
+               dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
+                       "reg");
                return;
        }
 
        node = debugfs_create_file("val", S_IRUGO | S_IWUSR, root, rtl,
                                   &fops_rtl8366s_regs);
        if (!node) {
-               dev_err(rtl->parent, "Creating debugfs file val failed\n");
+               dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
+                       "val");
                return;
        }
 
        node = debugfs_create_file("vlan", S_IRUSR, root, rtl,
                                   &fops_rtl8366s_vlan);
        if (!node) {
-               dev_err(rtl->parent,
-                       "Creating debugfs file vlan failed\n");
+               dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
+                       "vlan");
                return;
        }
 
        node = debugfs_create_file("mibs", S_IRUSR, root, rtl,
                                   &fops_rtl8366s_mibs);
        if (!node) {
-               dev_err(rtl->parent,
-                       "Creating debugfs file mibs failed\n");
+               dev_err(rtl->parent, "Creating debugfs file '%s' failed\n",
+                       "mibs");
                return;
        }
 }
@@ -1087,19 +1091,24 @@ static int rtl8366s_sw_get_port_link(struct switch_dev *dev,
        if (val->port_vlan % 2)
                data = data >> 8;
 
-       len = snprintf(rtl->buf, sizeof(rtl->buf),
-                       "port:%d link:%s speed:%s %s-duplex %s%s%s",
-                       val->port_vlan,
-                       (data & RTL8366S_PORT_STATUS_LINK_MASK) ? "up" : "down",
-                       rtl8366s_speed_str(data &
+       if (data & RTL8366S_PORT_STATUS_LINK_MASK) {
+               len = snprintf(rtl->buf, sizeof(rtl->buf),
+                               "port:%d link:up speed:%s %s-duplex %s%s%s",
+                               val->port_vlan,
+                               rtl8366s_speed_str(data &
                                          RTL8366S_PORT_STATUS_SPEED_MASK),
-                       (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) ?
-                               "full" : "half",
-                       (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) ?
-                               "tx-pause ": "",
-                       (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) ?
-                               "rx-pause " : "",
-                       (data & RTL8366S_PORT_STATUS_AN_MASK) ? "nway ": "");
+                               (data & RTL8366S_PORT_STATUS_DUPLEX_MASK) ?
+                                       "full" : "half",
+                               (data & RTL8366S_PORT_STATUS_TXPAUSE_MASK) ?
+                                       "tx-pause ": "",
+                               (data & RTL8366S_PORT_STATUS_RXPAUSE_MASK) ?
+                                       "rx-pause " : "",
+                               (data & RTL8366S_PORT_STATUS_AN_MASK) ?
+                                       "nway ": "");
+       } else {
+               len = snprintf(rtl->buf, sizeof(rtl->buf), "port:%d link: down",
+                               val->port_vlan);
+       }
 
        val->value.s = rtl->buf;
        val->len = len;
@@ -1462,7 +1471,7 @@ static void rtl8366s_switch_cleanup(struct rtl8366s *rtl)
 
 static int rtl8366s_mii_read(struct mii_bus *bus, int addr, int reg)
 {
-       struct rtl8366s *rtl = bus->priv;
+       struct rtl8366s *rtl = smi_to_rtl8366s(bus->priv);
        u32 val = 0;
        int err;
 
@@ -1475,7 +1484,7 @@ static int rtl8366s_mii_read(struct mii_bus *bus, int addr, int reg)
 
 static int rtl8366s_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
 {
-       struct rtl8366s *rtl = bus->priv;
+       struct rtl8366s *rtl = smi_to_rtl8366s(bus->priv);
        u32 t;
        int err;
 
@@ -1486,63 +1495,33 @@ static int rtl8366s_mii_write(struct mii_bus *bus, int addr, int reg, u16 val)
        return err;
 }
 
-static int rtl8366s_mii_init(struct rtl8366s *rtl)
+static int rtl8366s_mii_bus_match(struct mii_bus *bus)
 {
-       int ret;
-       int i;
+       return (bus->read == rtl8366s_mii_read &&
+               bus->write == rtl8366s_mii_write);
+}
 
-       rtl->mii_bus = mdiobus_alloc();
-       if (rtl->mii_bus == NULL) {
-               ret = -ENOMEM;
-               goto err;
-       }
+static int rtl8366s_setup(struct rtl8366s *rtl)
+{
+       int ret;
 
-       rtl->mii_bus->priv = (void *) rtl;
-       rtl->mii_bus->name = "rtl8366-rtl";
-       rtl->mii_bus->read = rtl8366s_mii_read;
-       rtl->mii_bus->write = rtl8366s_mii_write;
-       snprintf(rtl->mii_bus->id, MII_BUS_ID_SIZE, "%s",
-                dev_name(rtl->parent));
-       rtl->mii_bus->parent = rtl->parent;
-       rtl->mii_bus->phy_mask = ~(0x1f);
-       rtl->mii_bus->irq = rtl->mii_irq;
-       for (i = 0; i < PHY_MAX_ADDR; i++)
-               rtl->mii_irq[i] = PHY_POLL;
-
-       ret = mdiobus_register(rtl->mii_bus);
+       ret = rtl8366s_reset_chip(rtl);
        if (ret)
-               goto err_free;
+               return ret;
 
+       rtl8366s_debugfs_init(rtl);
        return 0;
-
- err_free:
-       mdiobus_free(rtl->mii_bus);
- err:
-       return ret;
-}
-
-static void rtl8366s_mii_cleanup(struct rtl8366s *rtl)
-{
-       mdiobus_unregister(rtl->mii_bus);
-       mdiobus_free(rtl->mii_bus);
 }
 
-static int rtl8366s_mii_bus_match(struct mii_bus *bus)
+static int rtl8366s_detect(struct rtl8366_smi *smi)
 {
-       return (bus->read == rtl8366s_mii_read &&
-               bus->write == rtl8366s_mii_write);
-}
-
-static int rtl8366s_setup(struct rtl8366s *rtl)
-{
-       struct rtl8366_smi *smi = &rtl->smi;
        u32 chip_id = 0;
        u32 chip_ver = 0;
        int ret;
 
        ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_ID_REG, &chip_id);
        if (ret) {
-               dev_err(rtl->parent, "unable to read chip id\n");
+               dev_err(smi->parent, "unable to read chip id\n");
                return ret;
        }
 
@@ -1550,28 +1529,29 @@ static int rtl8366s_setup(struct rtl8366s *rtl)
        case RTL8366S_CHIP_ID_8366:
                break;
        default:
-               dev_err(rtl->parent, "unknown chip id (%04x)\n", chip_id);
+               dev_err(smi->parent, "unknown chip id (%04x)\n", chip_id);
                return -ENODEV;
        }
 
        ret = rtl8366_smi_read_reg(smi, RTL8366S_CHIP_VERSION_CTRL_REG,
                                   &chip_ver);
        if (ret) {
-               dev_err(rtl->parent, "unable to read chip version\n");
+               dev_err(smi->parent, "unable to read chip version\n");
                return ret;
        }
 
-       dev_info(rtl->parent, "RTL%04x ver. %u chip found\n",
+       dev_info(smi->parent, "RTL%04x ver. %u chip found\n",
                 chip_id, chip_ver & RTL8366S_CHIP_VERSION_MASK);
 
-       ret = rtl8366s_reset_chip(rtl);
-       if (ret)
-               return ret;
-
-       rtl8366s_debugfs_init(rtl);
        return 0;
 }
 
+static struct rtl8366_smi_ops rtl8366s_smi_ops = {
+       .detect         = rtl8366s_detect,
+       .mii_read       = rtl8366s_mii_read,
+       .mii_write      = rtl8366s_mii_write,
+};
+
 static int __init rtl8366s_probe(struct platform_device *pdev)
 {
        static int rtl8366_smi_version_printed;
@@ -1604,6 +1584,7 @@ static int __init rtl8366s_probe(struct platform_device *pdev)
        smi->parent = &pdev->dev;
        smi->gpio_sda = pdata->gpio_sda;
        smi->gpio_sck = pdata->gpio_sck;
+       smi->ops = &rtl8366s_smi_ops;
 
        err = rtl8366_smi_init(smi);
        if (err)
@@ -1615,18 +1596,12 @@ static int __init rtl8366s_probe(struct platform_device *pdev)
        if (err)
                goto err_clear_drvdata;
 
-       err = rtl8366s_mii_init(rtl);
-       if (err)
-               goto err_clear_drvdata;
-
        err = rtl8366s_switch_init(rtl);
        if (err)
-               goto err_mii_cleanup;
+               goto err_clear_drvdata;
 
        return 0;
 
- err_mii_cleanup:
-       rtl8366s_mii_cleanup(rtl);
  err_clear_drvdata:
        platform_set_drvdata(pdev, NULL);
        rtl8366_smi_cleanup(smi);
@@ -1669,7 +1644,6 @@ static int __devexit rtl8366s_remove(struct platform_device *pdev)
        if (rtl) {
                rtl8366s_switch_cleanup(rtl);
                rtl8366s_debugfs_remove(rtl);
-               rtl8366s_mii_cleanup(rtl);
                platform_set_drvdata(pdev, NULL);
                rtl8366_smi_cleanup(&rtl->smi);
                kfree(rtl);