common: Move ARM cache operations out of common.h
[oweals/u-boot.git] / drivers / net / ravb.c
index ab45a31d6a60a04b2d00b0c4eefc1b2e60110daa..fb4a628d63caf156eb8dc6bb8183955874075254 100644 (file)
@@ -1,3 +1,4 @@
+// SPDX-License-Identifier: GPL-2.0+
 /*
  * drivers/net/ravb.c
  *     This file is driver for Renesas Ethernet AVB.
@@ -5,11 +6,11 @@
  * Copyright (C) 2015-2017  Renesas Electronics Corporation
  *
  * Based on the SuperH Ethernet driver.
- *
- * SPDX-License-Identifier:    GPL-2.0+
  */
 
 #include <common.h>
+#include <clk.h>
+#include <cpu_func.h>
 #include <dm.h>
 #include <errno.h>
 #include <miiphy.h>
@@ -17,6 +18,7 @@
 #include <linux/mii.h>
 #include <wait_bit.h>
 #include <asm/io.h>
+#include <asm/gpio.h>
 
 /* Registers */
 #define RAVB_REG_CCC           0x000
@@ -45,6 +47,8 @@
 #define CSR_OPS                        0x0000000F
 #define CSR_OPS_CONFIG         BIT(1)
 
+#define APSR_TDM               BIT(14)
+
 #define TCCR_TSRQ0             BIT(0)
 
 #define RFLR_RFL_MIN           0x05EE
@@ -120,6 +124,8 @@ struct ravb_priv {
        struct phy_device       *phydev;
        struct mii_dev          *bus;
        void __iomem            *iobase;
+       struct clk              clk;
+       struct gpio_desc        reset_gpio;
 };
 
 static inline void ravb_flush_dcache(u32 addr, u32 len)
@@ -218,8 +224,8 @@ static int ravb_reset(struct udevice *dev)
        writel(CCC_OPC_CONFIG, eth->iobase + RAVB_REG_CCC);
 
        /* Check the operating mode is changed to the config mode. */
-       return wait_for_bit(dev->name, (void *)eth->iobase + RAVB_REG_CSR,
-                           CSR_OPS_CONFIG, true, 100, true);
+       return wait_for_bit_le32(eth->iobase + RAVB_REG_CSR,
+                                CSR_OPS_CONFIG, true, 100, true);
 }
 
 static void ravb_base_desc_init(struct ravb_priv *eth)
@@ -298,21 +304,30 @@ static int ravb_phy_config(struct udevice *dev)
        struct ravb_priv *eth = dev_get_priv(dev);
        struct eth_pdata *pdata = dev_get_platdata(dev);
        struct phy_device *phydev;
-       int reg;
+       int mask = 0xffffffff, reg;
+
+       if (dm_gpio_is_valid(&eth->reset_gpio)) {
+               dm_gpio_set_value(&eth->reset_gpio, 1);
+               mdelay(20);
+               dm_gpio_set_value(&eth->reset_gpio, 0);
+               mdelay(1);
+       }
 
-       phydev = phy_connect(eth->bus, pdata->phy_interface,
-                            dev, PHY_INTERFACE_MODE_RGMII_ID);
+       phydev = phy_find_by_mask(eth->bus, mask, pdata->phy_interface);
        if (!phydev)
                return -ENODEV;
 
+       phy_connect_dev(phydev, dev);
+
        eth->phydev = phydev;
 
-       /* 10BASE is not supported for Ethernet AVB MAC */
-       phydev->supported &= ~(SUPPORTED_10baseT_Full
-                              | SUPPORTED_10baseT_Half);
+       phydev->supported &= SUPPORTED_100baseT_Full |
+                            SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
+                            SUPPORTED_TP | SUPPORTED_MII | SUPPORTED_Pause |
+                            SUPPORTED_Asym_Pause;
+
        if (pdata->max_speed != 1000) {
-               phydev->supported &= ~(SUPPORTED_1000baseT_Half
-                                      | SUPPORTED_1000baseT_Full);
+               phydev->supported &= ~SUPPORTED_1000baseT_Full;
                reg = phy_read(phydev, -1, MII_CTRL1000);
                reg &= ~(BIT(9) | BIT(8));
                phy_write(phydev, -1, MII_CTRL1000, reg);
@@ -377,9 +392,14 @@ static int ravb_dmac_init(struct udevice *dev)
        /* FIFO size set */
        writel(0x00222210, eth->iobase + RAVB_REG_TGC);
 
-       /* Delay CLK: 2ns */
-       if (pdata->max_speed == 1000)
-               writel(BIT(14), eth->iobase + RAVB_REG_APSR);
+       /* Delay CLK: 2ns (not applicable on R-Car E3/D3) */
+       if ((rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77990) ||
+           (rmobile_get_cpu_type() == RMOBILE_CPU_TYPE_R8A77995))
+               return 0;
+
+       if ((pdata->phy_interface == PHY_INTERFACE_MODE_RGMII_ID) ||
+           (pdata->phy_interface == PHY_INTERFACE_MODE_RGMII_TXID))
+               writel(APSR_TDM, eth->iobase + RAVB_REG_APSR);
 
        return 0;
 }
@@ -387,7 +407,7 @@ static int ravb_dmac_init(struct udevice *dev)
 static int ravb_config(struct udevice *dev)
 {
        struct ravb_priv *eth = dev_get_priv(dev);
-       struct phy_device *phy;
+       struct phy_device *phy = eth->phydev;
        u32 mask = ECMR_CHG_DM | ECMR_RE | ECMR_TE;
        int ret;
 
@@ -398,13 +418,6 @@ static int ravb_config(struct udevice *dev)
        ravb_mac_init(eth);
        ravb_write_hwaddr(dev);
 
-       /* Configure phy */
-       ret = ravb_phy_config(dev);
-       if (ret)
-               return ret;
-
-       phy = eth->phydev;
-
        ret = phy_startup(phy);
        if (ret)
                return ret;
@@ -426,7 +439,7 @@ static int ravb_config(struct udevice *dev)
        return 0;
 }
 
-int ravb_start(struct udevice *dev)
+static int ravb_start(struct udevice *dev)
 {
        struct ravb_priv *eth = dev_get_priv(dev);
        int ret;
@@ -451,6 +464,9 @@ int ravb_start(struct udevice *dev)
 
 static void ravb_stop(struct udevice *dev)
 {
+       struct ravb_priv *eth = dev_get_priv(dev);
+
+       phy_shutdown(eth->phydev);
        ravb_reset(dev);
 }
 
@@ -458,6 +474,7 @@ static int ravb_probe(struct udevice *dev)
 {
        struct eth_pdata *pdata = dev_get_platdata(dev);
        struct ravb_priv *eth = dev_get_priv(dev);
+       struct ofnode_phandle_args phandle_args;
        struct mii_dev *mdiodev;
        void __iomem *iobase;
        int ret;
@@ -465,6 +482,21 @@ static int ravb_probe(struct udevice *dev)
        iobase = map_physmem(pdata->iobase, 0x1000, MAP_NOCACHE);
        eth->iobase = iobase;
 
+       ret = clk_get_by_index(dev, 0, &eth->clk);
+       if (ret < 0)
+               goto err_mdio_alloc;
+
+       ret = dev_read_phandle_with_args(dev, "phy-handle", NULL, 0, 0, &phandle_args);
+       if (!ret) {
+               gpio_request_by_name_nodev(phandle_args.node, "reset-gpios", 0,
+                                          &eth->reset_gpio, GPIOD_IS_OUT);
+       }
+
+       if (!dm_gpio_is_valid(&eth->reset_gpio)) {
+               gpio_request_by_name(dev, "reset-gpios", 0, &eth->reset_gpio,
+                                    GPIOD_IS_OUT);
+       }
+
        mdiodev = mdio_alloc();
        if (!mdiodev) {
                ret = -ENOMEM;
@@ -482,8 +514,23 @@ static int ravb_probe(struct udevice *dev)
 
        eth->bus = miiphy_get_dev_by_name(dev->name);
 
+       /* Bring up PHY */
+       ret = clk_enable(&eth->clk);
+       if (ret)
+               goto err_mdio_register;
+
+       ret = ravb_reset(dev);
+       if (ret)
+               goto err_mdio_reset;
+
+       ret = ravb_phy_config(dev);
+       if (ret)
+               goto err_mdio_reset;
+
        return 0;
 
+err_mdio_reset:
+       clk_disable(&eth->clk);
 err_mdio_register:
        mdio_free(mdiodev);
 err_mdio_alloc:
@@ -495,9 +542,13 @@ static int ravb_remove(struct udevice *dev)
 {
        struct ravb_priv *eth = dev_get_priv(dev);
 
+       clk_disable(&eth->clk);
+
        free(eth->phydev);
        mdio_unregister(eth->bus);
        mdio_free(eth->bus);
+       if (dm_gpio_is_valid(&eth->reset_gpio))
+               dm_gpio_free(dev, &eth->reset_gpio);
        unmap_physmem(eth->iobase, MAP_NOCACHE);
 
        return 0;
@@ -589,9 +640,50 @@ static const struct eth_ops ravb_ops = {
        .write_hwaddr           = ravb_write_hwaddr,
 };
 
+int ravb_ofdata_to_platdata(struct udevice *dev)
+{
+       struct eth_pdata *pdata = dev_get_platdata(dev);
+       const char *phy_mode;
+       const fdt32_t *cell;
+       int ret = 0;
+
+       pdata->iobase = devfdt_get_addr(dev);
+       pdata->phy_interface = -1;
+       phy_mode = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "phy-mode",
+                              NULL);
+       if (phy_mode)
+               pdata->phy_interface = phy_get_interface_by_name(phy_mode);
+       if (pdata->phy_interface == -1) {
+               debug("%s: Invalid PHY interface '%s'\n", __func__, phy_mode);
+               return -EINVAL;
+       }
+
+       pdata->max_speed = 1000;
+       cell = fdt_getprop(gd->fdt_blob, dev_of_offset(dev), "max-speed", NULL);
+       if (cell)
+               pdata->max_speed = fdt32_to_cpu(*cell);
+
+       sprintf(bb_miiphy_buses[0].name, dev->name);
+
+       return ret;
+}
+
+static const struct udevice_id ravb_ids[] = {
+       { .compatible = "renesas,etheravb-r8a7795" },
+       { .compatible = "renesas,etheravb-r8a7796" },
+       { .compatible = "renesas,etheravb-r8a77965" },
+       { .compatible = "renesas,etheravb-r8a77970" },
+       { .compatible = "renesas,etheravb-r8a77990" },
+       { .compatible = "renesas,etheravb-r8a77995" },
+       { .compatible = "renesas,etheravb-rcar-gen3" },
+       { }
+};
+
 U_BOOT_DRIVER(eth_ravb) = {
        .name           = "ravb",
        .id             = UCLASS_ETH,
+       .of_match       = ravb_ids,
+       .ofdata_to_platdata = ravb_ofdata_to_platdata,
        .probe          = ravb_probe,
        .remove         = ravb_remove,
        .ops            = &ravb_ops,