AR8216: make ARL age time configurable
[openwrt.git] / target / linux / generic / files / drivers / net / phy / rtl8366_smi.c
index c89e5a9..699234d 100644 (file)
 #include <linux/gpio.h>
 #include <linux/spinlock.h>
 #include <linux/skbuff.h>
+#include <linux/of.h>
+#include <linux/of_platform.h>
+#include <linux/of_gpio.h>
 #include <linux/rtl8366.h>
 
-#ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
+#ifdef CONFIG_RTL8366_SMI_DEBUG_FS
 #include <linux/debugfs.h>
 #endif
 
@@ -25,6 +28,9 @@
 
 #define RTL8366_SMI_ACK_RETRY_COUNT         5
 
+#define RTL8366_SMI_HW_STOP_DELAY              25      /* msecs */
+#define RTL8366_SMI_HW_START_DELAY             100     /* msecs */
+
 static inline void rtl8366_smi_clk_delay(struct rtl8366_smi *smi)
 {
        ndelay(smi->clk_delay);
@@ -144,7 +150,6 @@ static int rtl8366_smi_wait_for_ack(struct rtl8366_smi *smi)
 
                if (++retry_cnt > RTL8366_SMI_ACK_RETRY_COUNT) {
                        dev_err(smi->parent, "ACK timeout\n");
-                       dump_stack();
                        return -ETIMEDOUT;
                }
        } while (1);
@@ -309,6 +314,19 @@ int rtl8366_smi_rmwr(struct rtl8366_smi *smi, u32 addr, u32 mask, u32 data)
 }
 EXPORT_SYMBOL_GPL(rtl8366_smi_rmwr);
 
+static int rtl8366_reset(struct rtl8366_smi *smi)
+{
+       if (smi->hw_reset) {
+               smi->hw_reset(true);
+               msleep(RTL8366_SMI_HW_STOP_DELAY);
+               smi->hw_reset(false);
+               msleep(RTL8366_SMI_HW_START_DELAY);
+               return 0;
+       }
+
+       return smi->ops->reset_chip(smi);
+}
+
 static int rtl8366_mc_is_used(struct rtl8366_smi *smi, int mc_index, int *used)
 {
        int err;
@@ -580,7 +598,7 @@ static int rtl8366_init_vlan(struct rtl8366_smi *smi)
        return rtl8366_enable_vlan(smi, 1);
 }
 
-#ifdef CONFIG_RTL8366S_PHY_DEBUG_FS
+#ifdef CONFIG_RTL8366_SMI_DEBUG_FS
 int rtl8366_debugfs_open(struct inode *inode, struct file *file)
 {
        file->private_data = inode->i_private;
@@ -725,7 +743,7 @@ static ssize_t rtl8366_write_debugfs_reg(struct file *file,
                buf[len - 1] = '\0';
 
 
-       if (strict_strtoul(buf, 16, &data)) {
+       if (kstrtoul(buf, 16, &data)) {
                dev_err(smi->parent, "Invalid reg value %s\n", buf);
        } else {
                err = rtl8366_smi_write_reg(smi, reg, data);
@@ -891,7 +909,7 @@ static void rtl8366_debugfs_remove(struct rtl8366_smi *smi)
 #else
 static inline void rtl8366_debugfs_init(struct rtl8366_smi *smi) {}
 static inline void rtl8366_debugfs_remove(struct rtl8366_smi *smi) {}
-#endif /* CONFIG_RTL8366S_PHY_DEBUG_FS */
+#endif /* CONFIG_RTL8366_SMI_DEBUG_FS */
 
 static int rtl8366_smi_mii_init(struct rtl8366_smi *smi)
 {
@@ -934,6 +952,31 @@ static void rtl8366_smi_mii_cleanup(struct rtl8366_smi *smi)
        mdiobus_free(smi->mii_bus);
 }
 
+int rtl8366_sw_reset_switch(struct switch_dev *dev)
+{
+       struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
+       int err;
+
+       err = rtl8366_reset(smi);
+       if (err)
+               return err;
+
+       err = smi->ops->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);
+}
+EXPORT_SYMBOL_GPL(rtl8366_sw_reset_switch);
+
 int rtl8366_sw_get_port_pvid(struct switch_dev *dev, int port, int *val)
 {
        struct rtl8366_smi *smi = sw_to_rtl8366_smi(dev);
@@ -1066,6 +1109,7 @@ int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
 
        port = &val->value.ports[0];
        for (i = 0; i < val->len; i++, port++) {
+               int pvid = 0;
                member |= BIT(port->id);
 
                if (!(port->flags & BIT(SWITCH_PORT_FLAG_TAGGED)))
@@ -1075,9 +1119,14 @@ int rtl8366_sw_set_vlan_ports(struct switch_dev *dev, struct switch_val *val)
                 * To ensure that we have a valid MC entry for this VLAN,
                 * initialize the port VLAN ID here.
                 */
-               err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
+               err = rtl8366_get_pvid(smi, port->id, &pvid);
                if (err < 0)
                        return err;
+               if (pvid == 0) {
+                       err = rtl8366_set_pvid(smi, port->id, val->port_vlan);
+                       if (err < 0)
+                               return err;
+               }
        }
 
        return rtl8366_set_vlan(smi, val->port_vlan, member, untag, 0);
@@ -1203,6 +1252,13 @@ static int __rtl8366_smi_init(struct rtl8366_smi *smi, const char *name)
        }
 
        spin_lock_init(&smi->lock);
+
+       /* start the switch */
+       if (smi->hw_reset) {
+               smi->hw_reset(false);
+               msleep(RTL8366_SMI_HW_START_DELAY);
+       }
+
        return 0;
 
  err_free_sda:
@@ -1213,6 +1269,9 @@ static int __rtl8366_smi_init(struct rtl8366_smi *smi, const char *name)
 
 static void __rtl8366_smi_cleanup(struct rtl8366_smi *smi)
 {
+       if (smi->hw_reset)
+               smi->hw_reset(true);
+
        gpio_free(smi->gpio_sck);
        gpio_free(smi->gpio_sda);
 }
@@ -1267,8 +1326,6 @@ int rtl8366_smi_init(struct rtl8366_smi *smi)
        if (err)
                goto err_out;
 
-       spin_lock_init(&smi->lock);
-
        dev_info(smi->parent, "using GPIO pins %u (SDA) and %u (SCK)\n",
                 smi->gpio_sda, smi->gpio_sck);
 
@@ -1278,6 +1335,10 @@ int rtl8366_smi_init(struct rtl8366_smi *smi)
                goto err_free_sck;
        }
 
+       err = rtl8366_reset(smi);
+       if (err)
+               goto err_free_sck;
+
        err = smi->ops->setup(smi);
        if (err) {
                dev_err(smi->parent, "chip setup failed, err=%d\n", err);
@@ -1314,11 +1375,75 @@ void rtl8366_smi_cleanup(struct rtl8366_smi *smi)
 {
        rtl8366_debugfs_remove(smi);
        rtl8366_smi_mii_cleanup(smi);
-       gpio_free(smi->gpio_sck);
-       gpio_free(smi->gpio_sda);
+       __rtl8366_smi_cleanup(smi);
 }
 EXPORT_SYMBOL_GPL(rtl8366_smi_cleanup);
 
+#ifdef CONFIG_OF
+int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi)
+{
+       int sck = of_get_named_gpio(pdev->dev.of_node, "gpio-sck", 0);
+       int sda = of_get_named_gpio(pdev->dev.of_node, "gpio-sda", 0);
+
+       if (!gpio_is_valid(sck) || !gpio_is_valid(sda)) {
+               dev_err(&pdev->dev, "gpios missing in devictree\n");
+               return -EINVAL;
+       }
+
+       smi->gpio_sda = sda;
+       smi->gpio_sck = sck;
+
+       return 0;
+}
+#else
+static inline int rtl8366_smi_probe_of(struct platform_device *pdev, struct rtl8366_smi *smi)
+{
+       return -ENODEV;
+}
+#endif
+
+int rtl8366_smi_probe_plat(struct platform_device *pdev, struct rtl8366_smi *smi)
+{
+       struct rtl8366_platform_data *pdata = pdev->dev.platform_data;
+
+       if (!pdev->dev.platform_data) {
+               dev_err(&pdev->dev, "no platform data specified\n");
+               return -EINVAL;
+       }
+
+       smi->gpio_sda = pdata->gpio_sda;
+       smi->gpio_sck = pdata->gpio_sck;
+       smi->hw_reset = pdata->hw_reset;
+
+       return 0;
+}
+
+
+struct rtl8366_smi *rtl8366_smi_probe(struct platform_device *pdev)
+{
+       struct rtl8366_smi *smi;
+       int err;
+
+       smi = rtl8366_smi_alloc(&pdev->dev);
+       if (!smi)
+               return NULL;
+
+       if (pdev->dev.of_node)
+               err = rtl8366_smi_probe_of(pdev, smi);
+       else
+               err = rtl8366_smi_probe_plat(pdev, smi);
+
+       if (err)
+               goto free_smi;
+
+       return smi;
+
+free_smi:
+       kfree(smi);
+       return NULL;
+}
+EXPORT_SYMBOL_GPL(rtl8366_smi_probe);
+
 MODULE_DESCRIPTION("Realtek RTL8366 SMI interface driver");
 MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>");
 MODULE_LICENSE("GPL v2");