kernel: add driver for Marvell 88E6171 switch
[openwrt.git] / target / linux / generic / files / drivers / net / phy / rtl8367.c
index 9959ee3..4978a8c 100644 (file)
@@ -11,7 +11,9 @@
 #include <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/platform_device.h>
+#include <linux/device.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
 #include <linux/delay.h>
 #include <linux/skbuff.h>
 #include <linux/rtl8367.h>
@@ -1071,6 +1073,51 @@ static int rtl8367_led_blinkrate_set(struct rtl8366_smi *smi, unsigned int rate)
        return 0;
 }
 
+#ifdef CONFIG_OF
+static int rtl8367_extif_init_of(struct rtl8366_smi *smi, int id,
+                                const char *name)
+{
+       struct rtl8367_extif_config *cfg;
+       const __be32 *prop;
+       int size;
+       int err;
+
+       prop = of_get_property(smi->parent->of_node, name, &size);
+       if (!prop)
+               return rtl8367_extif_init(smi, id, NULL);
+
+       if (size != (9 * sizeof(*prop))) {
+               dev_err(smi->parent, "%s property is invalid\n", name);
+               return -EINVAL;
+       }
+
+       cfg = kzalloc(sizeof(struct rtl8367_extif_config), GFP_KERNEL);
+       if (!cfg)
+               return -ENOMEM;
+
+       cfg->txdelay = be32_to_cpup(prop++);
+       cfg->rxdelay = be32_to_cpup(prop++);
+       cfg->mode = be32_to_cpup(prop++);
+       cfg->ability.force_mode = be32_to_cpup(prop++);
+       cfg->ability.txpause = be32_to_cpup(prop++);
+       cfg->ability.rxpause = be32_to_cpup(prop++);
+       cfg->ability.link = be32_to_cpup(prop++);
+       cfg->ability.duplex = be32_to_cpup(prop++);
+       cfg->ability.speed = be32_to_cpup(prop++);
+
+       err = rtl8367_extif_init(smi, id, cfg);
+       kfree(cfg);
+
+       return err;
+}
+#else
+static int rtl8367_extif_init_of(struct rtl8366_smi *smi, int id,
+                                const char *name)
+{
+       return -EINVAL;
+}
+#endif
+
 static int rtl8367_setup(struct rtl8366_smi *smi)
 {
        struct rtl8367_platform_data *pdata;
@@ -1084,13 +1131,23 @@ static int rtl8367_setup(struct rtl8366_smi *smi)
                return err;
 
        /* initialize external interfaces */
-       err = rtl8367_extif_init(smi, 0, pdata->extif0_cfg);
-       if (err)
-               return err;
+       if (smi->parent->of_node) {
+               err = rtl8367_extif_init_of(smi, 0, "realtek,extif0");
+               if (err)
+                       return err;
 
-       err = rtl8367_extif_init(smi, 1, pdata->extif1_cfg);
-       if (err)
-               return err;
+               err = rtl8367_extif_init_of(smi, 1, "realtek,extif1");
+               if (err)
+                       return err;
+       } else {
+               err = rtl8367_extif_init(smi, 0, pdata->extif0_cfg);
+               if (err)
+                       return err;
+
+               err = rtl8367_extif_init(smi, 1, pdata->extif1_cfg);
+               if (err)
+                       return err;
+       }
 
        /* set maximum packet length to 1536 bytes */
        REG_RMW(smi, RTL8367_SWC0_REG, RTL8367_SWC0_MAX_LENGTH_MASK,
@@ -1350,6 +1407,9 @@ static int rtl8367_is_vlan_valid(struct rtl8366_smi *smi, unsigned vlan)
 {
        unsigned max = RTL8367_NUM_VLANS;
 
+       if (smi->vlan4k_enabled)
+               max = RTL8367_NUM_VIDS - 1;
+
        if (vlan == 0 || vlan >= max)
                return 0;
 
@@ -1475,31 +1535,6 @@ static int rtl8367_sw_reset_port_mibs(struct switch_dev *dev,
                                RTL8367_MIB_CTRL_PORT_RESET_MASK(port % 8));
 }
 
-static int rtl8367_sw_reset_switch(struct switch_dev *dev)
-{
-       struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
-       int err;
-
-       err = rtl8367_reset_chip(smi);
-       if (err)
-               return err;
-
-
-       err = rtl8367_setup(smi);
-       if (err)
-               return err;
-
-       err = rtl8366_reset_vlan(smi);
-       if (err)
-               return err;
-
-       err = rtl8366_enable_vlan(smi, 1);
-       if (err)
-               return err;
-
-       return rtl8366_enable_all_ports(smi, 1);
-}
-
 static struct switch_attr rtl8367_globals[] = {
        {
                .type = SWITCH_TYPE_INT,
@@ -1557,10 +1592,17 @@ static struct switch_attr rtl8367_vlan[] = {
                .max = 1,
                .set = NULL,
                .get = rtl8366_sw_get_vlan_info,
+       }, {
+               .type = SWITCH_TYPE_INT,
+               .name = "fid",
+               .description = "Get/Set vlan FID",
+               .max = RTL8367_FIDMAX,
+               .set = rtl8366_sw_set_vlan_fid,
+               .get = rtl8366_sw_get_vlan_fid,
        },
 };
 
-static const struct switch_dev_ops rtl8366m_ops = {
+static const struct switch_dev_ops rtl8367_sw_ops = {
        .attr_global = {
                .attr = rtl8367_globals,
                .n_attr = ARRAY_SIZE(rtl8367_globals),
@@ -1578,7 +1620,7 @@ static const struct switch_dev_ops rtl8366m_ops = {
        .set_vlan_ports = rtl8366_sw_set_vlan_ports,
        .get_port_pvid = rtl8366_sw_get_port_pvid,
        .set_port_pvid = rtl8366_sw_set_port_pvid,
-       .reset_switch = rtl8367_sw_reset_switch,
+       .reset_switch = rtl8366_sw_reset_switch,
        .get_port_link = rtl8367_sw_get_port_link,
 };
 
@@ -1591,7 +1633,7 @@ static int rtl8367_switch_init(struct rtl8366_smi *smi)
        dev->cpu_port = RTL8367_CPU_PORT_NUM;
        dev->ports = RTL8367_NUM_PORTS;
        dev->vlans = RTL8367_NUM_VIDS;
-       dev->ops = &rtl8366m_ops;
+       dev->ops = &rtl8367_sw_ops;
        dev->alias = dev_name(smi->parent);
 
        err = register_switch(dev, NULL);
@@ -1693,27 +1735,15 @@ static struct rtl8366_smi_ops rtl8367_smi_ops = {
        .enable_port    = rtl8367_enable_port,
 };
 
-static int __devinit rtl8367_probe(struct platform_device *pdev)
+static int rtl8367_probe(struct platform_device *pdev)
 {
-       struct rtl8367_platform_data *pdata;
        struct rtl8366_smi *smi;
        int err;
 
-       pdata = pdev->dev.platform_data;
-       if (!pdata) {
-               dev_err(&pdev->dev, "no platform data specified\n");
-               err = -EINVAL;
-               goto err_out;
-       }
-
-       smi = rtl8366_smi_alloc(&pdev->dev);
-       if (!smi) {
-               err = -ENOMEM;
-               goto err_out;
-       }
+       smi = rtl8366_smi_probe(pdev);
+       if (!smi)
+               return -ENODEV;
 
-       smi->gpio_sda = pdata->gpio_sda;
-       smi->gpio_sck = pdata->gpio_sck;
        smi->clk_delay = 1500;
        smi->cmd_read = 0xb9;
        smi->cmd_write = 0xb8;
@@ -1741,11 +1771,10 @@ static int __devinit rtl8367_probe(struct platform_device *pdev)
        rtl8366_smi_cleanup(smi);
  err_free_smi:
        kfree(smi);
- err_out:
        return err;
 }
 
-static int __devexit rtl8367_remove(struct platform_device *pdev)
+static int rtl8367_remove(struct platform_device *pdev)
 {
        struct rtl8366_smi *smi = platform_get_drvdata(pdev);
 
@@ -1767,13 +1796,24 @@ static void rtl8367_shutdown(struct platform_device *pdev)
                rtl8367_reset_chip(smi);
 }
 
+#ifdef CONFIG_OF
+static const struct of_device_id rtl8367_match[] = {
+       { .compatible = "realtek,rtl8367" },
+       {},
+};
+MODULE_DEVICE_TABLE(of, rtl8367_match);
+#endif
+
 static struct platform_driver rtl8367_driver = {
        .driver = {
                .name           = RTL8367_DRIVER_NAME,
                .owner          = THIS_MODULE,
+#ifdef CONFIG_OF
+               .of_match_table = of_match_ptr(rtl8367_match),
+#endif
        },
        .probe          = rtl8367_probe,
-       .remove         = __devexit_p(rtl8367_remove),
+       .remove         = rtl8367_remove,
        .shutdown       = rtl8367_shutdown,
 };