Re: [PATCH v3 3/3] net: ftmac100: add mii read and write callbacks

2023-02-10 Thread Tom Rini
On Fri, Feb 03, 2023 at 10:09:04PM +0300, Sergei Antonov wrote:

> Register mii_bus with read and write callbacks to allow the 'mii'
> command to work. Use a timeout of 10 ms to wait for the R/W
> operations to complete.
> 
> Signed-off-by: Sergei Antonov 
> Reviewed-by: Rick Chen 
> Tested-by: Rick Chen 

Applied to u-boot/master, thanks!

-- 
Tom


signature.asc
Description: PGP signature


Re: [PATCH v3 3/3] net: ftmac100: add mii read and write callbacks

2023-02-08 Thread Rick Chen
> From: U-Boot  On Behalf Of Sergei Antonov
> Sent: Saturday, February 04, 2023 3:09 AM
> To: u-boot@lists.denx.de; rfried@gmail.com; joe.hershber...@ni.com
> Cc: Sergei Antonov 
> Subject: [PATCH v3 3/3] net: ftmac100: add mii read and write callbacks
>
> Register mii_bus with read and write callbacks to allow the 'mii'
> command to work. Use a timeout of 10 ms to wait for the R/W operations to 
> complete.
>
> Signed-off-by: Sergei Antonov 
> ---

Reviewed-by: Rick Chen 
Tested-by: Rick Chen 

I have verified this patch on AE350 platform and it work well with
dhcp command as below:


U-Boot 2023.04-rc1-00032-gf9f161b600-dirty (Feb 09 2023 - 10:55:03 +0800)

DRAM:  1 GiB
Core:  16 devices, 12 uclasses, devicetree: board
Flash: 64 MiB
MMC:
Loading Environment from SPIFlash... SF: Detected mx25u1635e with page
size 256 Bytes, erase size 4 KiB, total 2 MiB
OK
In:serial@f030
Out:   serial@f030
Err:   serial@f030
Net:   no alias for ethernet0
eth0: mac@e010
Hit any key to stop autoboot:  0
RISC-V #
RISC-V # dhcp 0x0600 10.0.12.60:Image-518
BOOTP broadcast 1
BOOTP broadcast 2
DHCP client bound to address 10.0.12.91 (280 ms)
Using mac@e010 device
TFTP from server 10.0.12.60; our IP address is 10.0.12.91
Filename 'Image-518'.
Load address: 0x600
Loading: #
 #
 #
 #
 #
 #
 #
 #
 #
 #
 #
 #
 #
 #
 ###
 300.8 KiB/s
done
Bytes transferred = 14098060 (d71e8c hex)
RISC-V # mii device
MII devices: 'eth0'
Current device: 'eth0'
RISC-V # mii info
PHY 0x00: OUI = 0x606E, Model = 0x0B, Rev = 0x01, 100baseT, FDX
RISC-V #


[PATCH v3 3/3] net: ftmac100: add mii read and write callbacks

2023-02-03 Thread Sergei Antonov
Register mii_bus with read and write callbacks to allow the 'mii'
command to work. Use a timeout of 10 ms to wait for the R/W
operations to complete.

Signed-off-by: Sergei Antonov 
---

v2 -> v3:
no change

v1 -> v2:
* fix a typo in the description
* add a dependency from MII to Kconfig

 drivers/net/Kconfig|   1 +
 drivers/net/ftmac100.c | 103 +
 drivers/net/ftmac100.h |   9 
 3 files changed, 113 insertions(+)

diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
index 62d2c03849af..6006cfc82d50 100644
--- a/drivers/net/Kconfig
+++ b/drivers/net/Kconfig
@@ -406,6 +406,7 @@ config FSL_FM_10GEC_REGULAR_NOTATION
 
 config FTMAC100
bool "Ftmac100 Ethernet Support"
+   select MII
help
  This MAC is present in Andestech SoCs.
 
diff --git a/drivers/net/ftmac100.c b/drivers/net/ftmac100.c
index e7b9d88ea9cd..fae3adc3de34 100644
--- a/drivers/net/ftmac100.c
+++ b/drivers/net/ftmac100.c
@@ -12,9 +12,13 @@
 #include 
 #include 
 #include 
+#include 
+#include 
+#include 
 #include 
 #include 
 #include 
+#include 
 
 #include "ftmac100.h"
 #include 
@@ -23,12 +27,16 @@ DECLARE_GLOBAL_DATA_PTR;
 
 #define ETH_ZLEN   60
 
+/* Timeout for a mdio read/write operation */
+#define FTMAC100_MDIO_TIMEOUT_USEC 1
+
 struct ftmac100_data {
struct ftmac100_txdes txdes[1];
struct ftmac100_rxdes rxdes[PKTBUFSRX];
int rx_index;
const char *name;
struct ftmac100 *ftmac100;
+   struct mii_dev *bus;
 };
 
 /*
@@ -322,10 +330,104 @@ static int ftmac100_of_to_plat(struct udevice *dev)
return 0;
 }
 
+/*
+ * struct mii_bus functions
+ */
+static int ftmac100_mdio_read(struct mii_dev *bus, int addr, int devad,
+ int reg)
+{
+   struct ftmac100_data *priv = bus->priv;
+   struct ftmac100 *ftmac100 = priv->ftmac100;
+   int phycr = FTMAC100_PHYCR_PHYAD(addr) |
+   FTMAC100_PHYCR_REGAD(reg) |
+   FTMAC100_PHYCR_MIIRD;
+   int ret;
+
+   writel(phycr, >phycr);
+
+   ret = readl_poll_timeout(>phycr, phycr,
+!(phycr & FTMAC100_PHYCR_MIIRD),
+FTMAC100_MDIO_TIMEOUT_USEC);
+   if (ret)
+   pr_err("%s: mdio read failed (addr=0x%x reg=0x%x)\n",
+  bus->name, addr, reg);
+   else
+   ret = phycr & FTMAC100_PHYCR_MIIRDATA;
+
+   return ret;
+}
+
+static int ftmac100_mdio_write(struct mii_dev *bus, int addr, int devad,
+  int reg, u16 value)
+{
+   struct ftmac100_data *priv = bus->priv;
+   struct ftmac100 *ftmac100 = priv->ftmac100;
+   int phycr = FTMAC100_PHYCR_PHYAD(addr) |
+   FTMAC100_PHYCR_REGAD(reg) |
+   FTMAC100_PHYCR_MIIWR;
+   int ret;
+
+   writel(value, >phywdata);
+   writel(phycr, >phycr);
+
+   ret = readl_poll_timeout(>phycr, phycr,
+!(phycr & FTMAC100_PHYCR_MIIWR),
+FTMAC100_MDIO_TIMEOUT_USEC);
+   if (ret)
+   pr_err("%s: mdio write failed (addr=0x%x reg=0x%x)\n",
+  bus->name, addr, reg);
+
+   return ret;
+}
+
+static int ftmac100_mdio_init(struct udevice *dev)
+{
+   struct ftmac100_data *priv = dev_get_priv(dev);
+   struct mii_dev *bus;
+   int ret;
+
+   bus = mdio_alloc();
+   if (!bus)
+   return -ENOMEM;
+
+   bus->read  = ftmac100_mdio_read;
+   bus->write = ftmac100_mdio_write;
+   bus->priv  = priv;
+
+   ret = mdio_register_seq(bus, dev_seq(dev));
+   if (ret) {
+   mdio_free(bus);
+   return ret;
+   }
+
+   priv->bus = bus;
+
+   return 0;
+}
+
 static int ftmac100_probe(struct udevice *dev)
 {
struct ftmac100_data *priv = dev_get_priv(dev);
priv->name = dev->name;
+   int ret = 0;
+
+   ret = ftmac100_mdio_init(dev);
+   if (ret) {
+   dev_err(dev, "Failed to initialize mdiobus: %d\n", ret);
+   goto out;
+   }
+
+out:
+   return ret;
+}
+
+static int ftmac100_remove(struct udevice *dev)
+{
+   struct ftmac100_data *priv = dev_get_priv(dev);
+
+   mdio_unregister(priv->bus);
+   mdio_free(priv->bus);
+
return 0;
 }
 
@@ -354,6 +456,7 @@ U_BOOT_DRIVER(ftmac100) = {
.bind   = ftmac100_bind,
.of_to_plat = ftmac100_of_to_plat,
.probe  = ftmac100_probe,
+   .remove = ftmac100_remove,
.ops= _ops,
.priv_auto  = sizeof(struct ftmac100_data),
.plat_auto  = sizeof(struct eth_pdata),
diff --git a/drivers/net/ftmac100.h b/drivers/net/ftmac100.h
index 75a49f628a69..21d339f835bf 100644
--- a/drivers/net/ftmac100.h
+++ b/drivers/net/ftmac100.h
@@ -92,6 +92,15 @@ struct ftmac100 {
 #define FTMAC100_MACCR_RX_MULTIPKT (1 << 16)
 #define FTMAC100_MACCR_RX_BROADPKT (1