Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded JosephCHANG 4 participants 6 messages Dec 15, 2021

Subsystems (3)

v5 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v5 cover ADD DM9051 ETHERNET DRIVER

Adjust Kconfig for reasonable dependence, make dm9051.c better by remove the wrappers, and swap to phylib

DM9051 is a spi interface chip,
need only cs/mosi/miso/clock with an interrupt gpio pin

JosephCHANG (2):
yaml: Add dm9051 SPI network yaml file
net: Add dm9051 driver

 .../bindings/net/davicom,dm9051.yaml          |  62 ++
 drivers/net/ethernet/davicom/Kconfig          |  30 +
 drivers/net/ethernet/davicom/Makefile         |   1 +
 drivers/net/ethernet/davicom/dm9051.c         | 913 ++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         | 187 ++++
 5 files changed, 1193 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/davicom,dm9051.yaml
 create mode 100644 drivers/net/ethernet/davicom/dm9051.c
 create mode 100644 drivers/net/ethernet/davicom/dm9051.h

base-commit: 9d922f5df53844228b9f7c62f2593f4f06c0b69b

Signature · 1 line
2.20.1
#1 lore raw

PATCH v5 1/2 yaml: Add dm9051 SPI network yaml file

This is a new yaml base data file for configure davicom dm9051 with device tree

---
 .../bindings/net/davicom,dm9051.yaml          | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/davicom,dm9051.yaml
Documentation/devicetree/bindings/net/davicom,dm9051.yaml new file +62 -0
new file mode 100644
index 000000000000..52e852fef753
@@ -0,0 +1,62 @@
+# SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
+%YAML 1.2
+---
+$id: http://devicetree.org/schemas/net/davicom,dm9051.yaml#
+$schema: http://devicetree.org/meta-schemas/core.yaml#
+
+title: Davicom DM9051 SPI Ethernet Controller
+
+maintainers:
+  - Joseph CHANG <[email protected]>
+
+description: |
+  The DM9051 is a fully integrated and cost-effective low pin count single
+  chip Fast Ethernet controller with a Serial Peripheral Interface (SPI).
+
+allOf:
+  - $ref: ethernet-controller.yaml#
+
+properties:
+  compatible:
+    const: davicom,dm9051
+
+  reg:
+    maxItems: 1
+
+  spi-max-frequency:
+    maximum: 45000000
+
+  interrupts:
+    maxItems: 1
+
+  local-mac-address: true
+
+  mac-address: true
+
+required:
+  - compatible
+  - reg
+  - spi-max-frequency
+  - interrupts
+
+additionalProperties: false
+
+examples:
+  # Raspberry Pi platform
+  - |
+    /* for Raspberry Pi with pin control stuff for GPIO irq */
+    #include <dt-bindings/interrupt-controller/irq.h>
+    #include <dt-bindings/gpio/gpio.h>
+    spi {
+        #address-cells = <1>;
+        #size-cells = <0>;
+
+        ethernet@0 {
+            compatible = "davicom,dm9051";
+            reg = <0>; /* spi chip select */
+            local-mac-address = [00 00 00 00 00 00];
+            interrupt-parent = <&gpio>;
+            interrupts = <26 IRQ_TYPE_LEVEL_LOW>;
+            spi-max-frequency = <31200000>;
+        };
+    };
Signature · 1 line
2.20.1
maintainer
#2 lore raw

Re: [PATCH v5, 1/2] yaml: Add dm9051 SPI network yaml file

Quoted 9 lines · JosephCHANG

This is a new yaml base data file for configure davicom dm9051 with device tree

---
 .../bindings/net/davicom,dm9051.yaml          | 62 +++++++++++++++++++
 1 file changed, 62 insertions(+)
 create mode 100644 Documentation/devicetree/bindings/net/davicom,dm9051.yaml

Please add Acked-by/Reviewed-by tags when posting new versions. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for acks received on the version they apply.

If a tag was not added on purpose, please state why and what changed.

maintainer
#3 lore raw

Re: [PATCH v5, 1/2] yaml: Add dm9051 SPI network yaml file

Quoted 11 lines · On Wed, 15 Dec 2021 09:51:16 -0600 Rob Herring wrote:
Quoted 4 lines · JosephCHANG

This is a new yaml base data file for configure davicom dm9051 with device tree

Please add Acked-by/Reviewed-by tags when posting new versions. However, there's no need to repost patches *only* to add the tags. The upstream maintainer will do that for acks received on the version they apply.

If a tag was not added on purpose, please state why and what changed.

And general changelog would be useful, what changed between v4 and v5? Again, no need to create v6 just for that, you can reply with the change log and include it with next versions if there are any.

#4 lore raw

PATCH v5 2/2 net: Add dm9051 driver

In reply to JosephCHANG

Add davicom dm9051 spi ethernet driver, The driver work with the device platform's spi master.

Work to eliminate the wrappers to be clear for read, swapped to phylib for phy connection tasks.

Tested with raspberry pi 4. Test for netwroking function, CAT5 cable unplug/plug and also ethtool detect for link state, and all are ok.

---
 drivers/net/ethernet/davicom/Kconfig  |  30 +
 drivers/net/ethernet/davicom/Makefile |   1 +
 drivers/net/ethernet/davicom/dm9051.c | 913 ++++++++++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h | 187 ++++++
 4 files changed, 1131 insertions(+)
 create mode 100644 drivers/net/ethernet/davicom/dm9051.c
 create mode 100644 drivers/net/ethernet/davicom/dm9051.h
drivers/net/ethernet/davicom/Kconfig +30 -0
index 7af86b6d4150..cc8cee5b993d 100644
@@ -3,6 +3,19 @@
 # Davicom device configuration
 #
 
+config NET_VENDOR_DAVICOM
+	bool "Davicom devices"
+	default y
+	help
+	  If you have a network (Ethernet) card belonging to this class, say Y.
+
+	  Note that the answer to this question doesn't directly affect the
+	  kernel: saying N will just cause the configurator to skip all
+	  the questions about Davicom devices. If you say Y, you will be asked
+	  for your specific card in the following selections.
+
+if NET_VENDOR_DAVICOM
+
 config DM9000
 	tristate "DM9000 support"
 	depends on ARM || MIPS || COLDFIRE || NIOS2 || COMPILE_TEST
@@ -22,3 +35,20 @@ config DM9000_FORCE_SIMPLE_PHY_POLL
 	  bit to determine if the link is up or down instead of the more
 	  costly MII PHY reads. Note, this will not work if the chip is
 	  operating with an external PHY.
+
+config DM9051
+	tristate "DM9051 SPI support"
+	select PHYLIB
+	depends on SPI
+	select CRC32
+	select MII
+	help
+	  Support for DM9051 SPI chipset.
+
+	  To compile this driver as a module, choose M here.  The module
+	  will be called dm9051.
+
+	  The SPI mode for the host's SPI master to access DM9051 is mode
+	  0 on the SPI bus.
+
+endif # NET_VENDOR_DAVICOM
drivers/net/ethernet/davicom/Makefile +1 -0
index 173c87d21076..225f85bc1f53 100644
@@ -4,3 +4,4 @@
 #
 
 obj-$(CONFIG_DM9000) += dm9000.o
+obj-$(CONFIG_DM9051) += dm9051.o
drivers/net/ethernet/davicom/dm9051.c new file +913 -0

913 lines not on this page · raw message

drivers/net/ethernet/davicom/dm9051.h new file +187 -0

187 lines not on this page · raw message

Signature · 1 line
2.20.1
#5 lore raw

Re: [PATCH v5, 2/2] net: Add dm9051 driver

Quoted 1 line

+static u8 ior(struct board_info *db, unsigned int reg)

Can we have some meaningful names please. And don't forget namespace prefix, ideally dm9051_

drivers/net/ethernet/davicom/dm9051.c line 64

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+{
+	u8 rxb[1];
+
+	dm9051_xfer(db, DM_SPI_RD | reg, NULL, rxb, 1);
+	return rxb[0];
+}
+
+/* chip ID display */
+static u8 iior(struct device *dev, struct board_info *db, unsigned int reg)

What does the extra i mean?

drivers/net/ethernet/davicom/dm9051.c line 72

Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8 -0
+{
+	u8 rxdata;
+
+	rxdata = ior(db, reg);
+	if (reg == DM9051_PIDL || reg == DM9051_PIDH)
+		dev_dbg(dev, "dm905.MOSI [%02x][..]\n", reg);
+	if (reg == DM9051_PIDL || reg == DM9051_PIDH)
+		dev_info(dev, "dm905.MISO [..][%02x]\n", rxdata);

At appears to be the same condition for both? Is that wrong, or can they be combined?

drivers/net/ethernet/davicom/dm9051.c line 84

Quoted 12 lines
drivers/net/ethernet/davicom/dm9051.c new file +12 -0
+	return rxdata;
+}
+
+static void iow(struct board_info *db, unsigned int reg, unsigned int val)
+{
+	u8 txb[1];
+
+	txb[0] = val;
+	dm9051_xfer(db, DM_SPI_WR | reg, txb, NULL, 1);
+}
+
+static void dm9inblk(struct board_info *db, u8 *buff, unsigned int len)

Please make your namespace prefix uniform. I would suggest dm9051_.

drivers/net/ethernet/davicom/dm9051.c line 107

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+static int dm_phy_read_func(struct board_info *db, int reg)
+{
+	int ret;
+	u8 check_val;
+
+	iow(db, DM9051_EPAR, DM9051_PHY | reg);
+	iow(db, DM9051_EPCR, EPCR_ERPRR | EPCR_EPOS);
+	read_poll_timeout(ior, check_val, !(check_val & EPCR_ERRE), 100, 10000,
+			  true, db, DM9051_EPCR);

read_poll_timeout() return an error code. Don't ignore it.

drivers/net/ethernet/davicom/dm9051.c line 113

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+	iow(db, DM9051_EPCR, 0x0);
+	ret = (ior(db, DM9051_EPDRH) << 8) | ior(db, DM9051_EPDRL);
+	return ret;
+}
+
+static void dm_phy_write_func(struct board_info *db, int reg, int value)

The _func does not add anything useful. Please remove them all.

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+{
+	u8 check_val;
+
+	iow(db, DM9051_EPAR, DM9051_PHY | reg);
+	iow(db, DM9051_EPDRL, value);
+	iow(db, DM9051_EPDRH, value >> 8);
+	iow(db, DM9051_EPCR, EPCR_EPOS | EPCR_ERPRW);
+	read_poll_timeout(ior, check_val, !(check_val & EPCR_ERRE), 100, 10000,
+			  true, db, DM9051_EPCR);
+	iow(db, DM9051_EPCR, 0x0);
+}

drivers/net/ethernet/davicom/dm9051.c line 167

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
 
+
+static int dm9051_mdio_read(struct mii_bus *mdiobus, int phy_id_unused, int reg)
+{
+	struct board_info *db = mdiobus->priv;
+	int val;
+
+	mutex_lock(&db->addr_lock);
+	val = dm_phy_read_func(db, reg);
+	mutex_unlock(&db->addr_lock);

When you register the mdiobus, the MDIO core will probe all 32 addresses on the bus. It looks like you PHY is on address 1. You should return 0xffff for all addresses other than 1.

drivers/net/ethernet/davicom/dm9051.c line 178

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+
+	return val;
+}
+
+static int dm9051_mdio_write(struct mii_bus *mdiobus, int phy_id_unused, int reg, u16 val)
+{
+	struct board_info *db = mdiobus->priv;
+
+	mutex_lock(&db->addr_lock);
+	dm_phy_write_func(db, reg, val);
+	mutex_unlock(&db->addr_lock);

And here you should return -ENODEV for any address other than 1.

drivers/net/ethernet/davicom/dm9051.c line 216

Quoted 5 lines
drivers/net/ethernet/davicom/dm9051.c new file +5 -0
+static void dm9051_fifo_reset(struct board_info *db)
+{
+	db->bc.DO_FIFO_RST_counter++;
+	dm_phy_write_func(db, MII_ADVERTISE, ADVERTISE_PAUSE_CAP |
+			  ADVERTISE_ALL | ADVERTISE_CSMA); /* for fcr, essential */

The MAC driver should not be accessing the PHY registers. That is the PHY drivers job.

drivers/net/ethernet/davicom/dm9051.c line 222

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+	iow(db, DM9051_FCR, FCR_FLOW_ENABLE); /* FlowCtrl */
+	iow(db, DM9051_PPCR, PPCR_PAUSE_COUNT); /* Pause Pkt Count */
+	iow(db, DM9051_LMCR, db->lcr_all); /* LEDMode1 */
+	iow(db, DM9051_INTCR, INTCR_POL_LOW); /* INTCR */
+}
+

> +/* loop rx

Quoted 2 lines

+ */
+static int dm9051_lrx(struct board_info *db)

Why not just call the function dm9051_loop_rx() so you don't need the comment?

drivers/net/ethernet/davicom/dm9051.c line 449

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+/* single tx
+ */
+static int dm9051_stx(struct board_info *db, u8 *buff, unsigned int len)

dm9051_single_tx() ?

The fact you have a comment suggests the function name is not good.

drivers/net/ethernet/davicom/dm9051.c line 460

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+{
+	int ret;
+	u8 check_val;
+
+	/* shorter waiting time with tx-end check */
+	ret = read_poll_timeout(ior, check_val, check_val & (NSR_TX2END | NSR_TX1END),
+				1, 20, false, db, DM9051_NSR);
+	dm9outblk(db, buff, len);
+	iow(db, DM9051_TXPLL, len);
+	iow(db, DM9051_TXPLH, len >> 8);
+	iow(db, DM9051_TCR, TCR_TXREQ);

Does it make sense to perform these writes if the timeout failed?

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+	return ret;
+}
+
+static int dm9051_send(struct board_info *db)
+{
+	struct net_device *ndev = db->ndev;

drivers/net/ethernet/davicom/dm9051.c line 476

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	int ntx = 0;
+
+	while (!skb_queue_empty(&db->txq)) {
+		struct sk_buff *skb;
+
+		skb = skb_dequeue(&db->txq);
+		if (skb) {
+			ntx++;
+			if (dm9051_stx(db, skb->data, skb->len))
+				netdev_dbg(ndev, "timeout %d--- WARNING---do-ntx\n", ntx);

You should be returning the error up the call stack.

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+			ndev->stats.tx_bytes += skb->len;
+			ndev->stats.tx_packets++;
+			dev_kfree_skb(skb);
+		}
+	}
+	return ntx;

drivers/net/ethernet/davicom/dm9051.c line 492

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+}
+
+/* end with enable the interrupt mask
+ */
+static irqreturn_t dm9051_rx_threaded_irq(int irq, void *pw)
+{
+	struct board_info *db = pw;
+	int nrx;
+
+	mutex_lock(&db->spi_lock); /* dlywork essential */

dlywork?

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+	dm_imr_disable_lock_essential(db); /* set imr disable */
+	if (netif_carrier_ok(db->ndev)) {
+		mutex_lock(&db->addr_lock);
+		do {
+			nrx = dm9051_lrx(db);
+			dm9051_send(db); /* for more performance */
+		} while (nrx);
+		mutex_unlock(&db->addr_lock);
+	}
+	dm_imr_enable_lock_essential(db); /* set imr enable */
+	mutex_unlock(&db->spi_lock); /* dlywork essential */

drivers/net/ethernet/davicom/dm9051.c

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	return IRQ_HANDLED;
+}
+
+/* end with enable the interrupt mask
+ */
+static void dm_stopcode_lock(struct board_info *db)
+{
+	mutex_lock(&db->addr_lock);
+
+	dm_phy_write_func(db, MII_BMCR, BMCR_RESET); /* PHY RESET */

The PHY driver should do this.

drivers/net/ethernet/davicom/dm9051.c line 616

Quoted 12 lines
drivers/net/ethernet/davicom/dm9051.c new file +12 -0
+	iow(db, DM9051_GPR, 0x01); /* Power-Down PHY */
+	iow(db, DM9051_RCR, RCR_RX_DISABLE);	/* Disable RX */
+
+	mutex_unlock(&db->addr_lock);
+}
+
+/* handle link change
+ */
+static void dm_handle_link_change(struct net_device *ndev)
+{
+	struct phy_device *phydev = ndev->phydev;
+	struct board_info *db = netdev_priv(ndev);

Don't you need to tell the MAC about the link speed? Duplex?

drivers/net/ethernet/davicom/dm9051.c line 627

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+
+	phy_print_status(phydev);
+
+	if (db->link != phydev->link) {
+		db->link = phydev->link;
+		netdev_dbg(ndev, "dm_handle_link link= %d\n", phydev->link);
+
+		if (phydev->link)
+			netif_carrier_on(ndev);
+		else
+			netif_carrier_off(ndev);

phylib will handle the carrier for you.

drivers/net/ethernet/davicom/dm9051.c line 643

Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +13 -0
+static int dm_phy_connect(struct board_info *db)
+{
+	char phy_id[MII_BUS_ID_SIZE + 3];
+
+	snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT,
+		 db->mdiobus->id, DM9051_PHY_ID);
+	db->phydev = phy_connect(db->ndev, phy_id, dm_handle_link_change,
+				 PHY_INTERFACE_MODE_MII);
+
+	if (IS_ERR(db->phydev))
+		return PTR_ERR(db->phydev);
+
+	db->phydev->irq = PHY_POLL;

Polling is the default, there is no need to set this.

drivers/net/ethernet/davicom/dm9051.c line 649

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+	return 0;
+}
+
+static void dm_phy_start(struct board_info *db)
+{
+	db->link = 0;

db->link does not appear to be useless.

drivers/net/ethernet/davicom/dm9051.c line 651

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+	phy_start(db->phydev);
+	phy_set_asym_pause(db->phydev, true, true);

You should do this before calling start.

But i don't see any code using the results of the autoneg. Does the MAC really support pause?

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+static int dm9051_open(struct net_device *ndev)
+{
+	struct board_info *db = netdev_priv(ndev);
+	int ret;

drivers/net/ethernet/davicom/dm9051.c line 671

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+
+	dm_opencode_lock(ndev, db);
+
+	skb_queue_head_init(&db->txq);
+	netif_start_queue(ndev);
+	netif_wake_queue(ndev);
+
+	ret = dm_opencode_receiving(ndev, db);
+	if (ret < 0) {
+		netdev_err(ndev, "failed to get irq\n");

It makes more sense to put that error inside dm_opencode_receiving() where you actually failed to get the interrupt.

Quoted 14 lines
drivers/net/ethernet/davicom/dm9051.c new file +14 -0
+		return ret;
+	}
+
+	dm_phy_start(db);
+	netdev_dbg(ndev, "[dm_open] %pM irq_no %d ACTIVE_LOW\n", ndev->dev_addr, ndev->irq);
+	return 0;
+}
+
+static int dm9051_probe(struct spi_device *spi)
+{
+	struct device *dev = &spi->dev;
+	struct net_device *ndev;
+	struct board_info *db;
+	int ret = 0;

...

drivers/net/ethernet/davicom/dm9051.c line 879

Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8 -0
+
+	ret = devm_register_netdev(dev, ndev);
+	if (ret) {
+		dev_err(dev, "failed to register network device\n");
+		goto err_netdev;
+	}
+
+	dm_operation_clear(db);

This should probably be before devm_register_netdev() since the device can be opened while still inside devm_register_netdev()..

drivers/net/ethernet/davicom/dm9051.c line 888

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+	return 0;
+
+err_netdev:
+	phy_disconnect(db->phydev);
+err_phycnnt:
+err_mdiobus:
+err_chipid:
+	return ret;
+}

Andrew