Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded Joseph CHAMG 4 participants 6 messages Jan 29, 2022 – Feb 3, 2022

Subsystems (3)

v16 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v16 cover ADD DM9051 ETHERNET DRIVER

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

Joseph CHAMG (1):
net: Add dm9051 driver

JosephCHANG (1):
yaml: Add dm9051 SPI network yaml file

 .../bindings/net/davicom,dm9051.yaml          |   62 +
 drivers/net/ethernet/davicom/Kconfig          |   31 +
 drivers/net/ethernet/davicom/Makefile         |    1 +
 drivers/net/ethernet/davicom/dm9051.c         | 1165 +++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         |  159 +++
 5 files changed, 1418 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 v16 1/2 yaml: Add dm9051 SPI network yaml file

From: JosephCHANG <[email protected]>

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
#2 lore raw

PATCH v16 2/2 net: Add dm9051 driver

In reply to Joseph CHAMG

Add davicom dm9051 spi ethernet driver, The driver work for the device platform which has the spi master

---
Cc: Jakub Kicinski <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: Leon Romanovsky <[email protected]>
Cc: andy Shevchenko <[email protected]>

v1-v4

Test ok with raspberry pi 2 and pi 4

v5

swapped to phylib for phy connection tasks

v6

remove the redundant code that phylib has support

v7

read/write registers must return error code to the caller

v8

not parmanently set MAC by .ndo_set_mac_address

v9

improve the registers read/write so that error code return as far as possible up the call stack.

v10

use regmap APIs for SPI and MDIO

v11

use regmap_read_poll_timeout
use corresponding regmap APIs, i.e. SPI, MDIO

v12

use mdiobus API instead of regmap MDIO APIs

v13

Simply all regmap APIs return value checked
Clear the fifo reset report
Eliminate redundant comments

Comment that DM9051_GPR register bit 0 function for power-up the internal phy, if this bit is updated from 1 to 0, then the whole dm9051 chip registers could not be accessed within 1 ms, so that has mdelay(1) to wait 1 ms

v14

To eliminate touching PHY registers, instead take advantage of phy_start to have it
Make neat to be readable exactly

v15

Process the mac address function to be exactly right Re-arrange functions for better style
Handle to auto negotiation to be right procedure

v16

Let netif_wake_queue at the bottom of dm9051_open
Add draining tx queue in dm9051_stop

 drivers/net/ethernet/davicom/Kconfig  |   31 +
 drivers/net/ethernet/davicom/Makefile |    1 +
 drivers/net/ethernet/davicom/dm9051.c | 1165 +++++++++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h |  159 ++++
 4 files changed, 1356 insertions(+)
 create mode 100644 drivers/net/ethernet/davicom/dm9051.c
 create mode 100644 drivers/net/ethernet/davicom/dm9051.h
drivers/net/ethernet/davicom/Kconfig +31 -0
index 7af86b6d4150..02e0caff98e3 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,21 @@ 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"
+	depends on SPI
+	select CRC32
+	select MDIO
+	select PHYLIB
+	select REGMAP_SPI
+	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 +1165 -0

1,165 lines not on this page · raw message

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

159 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

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

Quoted 9 lines · Joseph CHAMG

Add davicom dm9051 spi ethernet driver, The driver work for the device platform which has the spi master

---
Cc: Jakub Kicinski <[email protected]>
Cc: Andrew Lunn <[email protected]>
Cc: Leon Romanovsky <[email protected]>
Cc: andy Shevchenko <[email protected]>

<...>

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -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 1097

Quoted 5 lines
drivers/net/ethernet/davicom/dm9051.c new file +5 -0
+	kthread_init_worker(&db->kw);
+	kthread_init_work(&db->kw_rxctrl, dm9051_rxctl_delay);
+	kthread_init_work(&db->kw_tx, dm9051_tx_delay);
+
+	db->kwr_task_kw = kthread_run(kthread_worker_fn, &db->kw, "dm9051");

It is very unlikely that simple driver like this will need kthreads, does it really need?

Thanks

maintainer
#4 lore raw

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

In reply to Joseph CHAMG

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

Quoted 3 lines · On Sun, 30 Jan 2022 00:43:46 +0800 Joseph CHAMG wrote:
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+		rdptr = skb_put(skb, rxlen - 4);
+		ret = regmap_noinc_read(db->regmap_dm, DM_SPI_MRCMD, rdptr, rxlen);
+		if (ret) {

should be counted as rx_error

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

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7 -0
+			dev_kfree_skb(skb);
+			return ret;
+		}
+
+		ret = regmap_write(db->regmap_dm, DM9051_ISR, 0xff); /* to stop mrcmd */
+		if (ret)
+			return ret;

leaks skb, also should be counted as rx_error

Quoted 42 lines
drivers/net/ethernet/davicom/dm9051.c new file +42 -0
+		skb->protocol = eth_type_trans(skb, db->ndev);
+		if (db->ndev->features & NETIF_F_RXCSUM)
+			skb_checksum_none_assert(skb);
+		netif_rx_ni(skb);
+		db->ndev->stats.rx_bytes += rxlen;
+		db->ndev->stats.rx_packets++;
+		scanrr++;
+	} while (!ret);
+
+	return scanrr;
+}
+
+/* transmit a packet,
+ * return value,
+ *   0 - succeed
+ *  -ETIMEDOUT - timeout error
+ */
+static int dm9051_single_tx(struct board_info *db, u8 *buff, unsigned int len)
+{
+	int ret;
+
+	ret = dm9051_map_xmitpoll(db);
+	if (ret)
+		return ret;
+
+	ret = regmap_noinc_write(db->regmap_dm, DM_SPI_MWCMD, buff, len);
+	if (ret)
+		return ret;
+
+	ret = regmap_bulk_write(db->regmap_dmbulk, DM9051_TXPLL, &len, 2);
+	if (ret < 0)
+		return ret;
+
+	return regmap_write(db->regmap_dm, DM9051_TCR, TCR_TXREQ);
+}
+
+static int dm9051_loop_tx(struct board_info *db)
+{
+	struct net_device *ndev = db->ndev;
+	int ntx = 0;
+	int ret;
+

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	while (!skb_queue_empty(&db->txq)) {
+		struct sk_buff *skb;
+
+		skb = skb_dequeue(&db->txq);
+		if (skb) {
+			ntx++;
+			ret = dm9051_single_tx(db, skb->data, skb->len);
+			dev_kfree_skb(skb);
+			if (ret < 0)
+				return 0;

Should be counted as tx error?

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+			ndev->stats.tx_bytes += skb->len;
+			ndev->stats.tx_packets++;
+		}
+
+		if (netif_queue_stopped(ndev) &&
+		    (skb_queue_len(&db->txq) < DM9051_TX_QUE_LO_WATER))
+			netif_wake_queue(ndev);
+	}
+
+	return ntx;
#5 lore raw

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

In reply to Joseph CHAMG

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

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+static int dm9051_update_fcr(struct board_info *db)
+{
+	u8 fcr = 0;
+	int ret;
+
+	if (db->pause.rx_pause)
+		fcr |= FCR_BKPM | FCR_FLCE;
+	if (db->pause.tx_pause)
+		fcr |= FCR_TXPEN;
+
+	ret = regmap_update_bits(db->regmap_dm, DM9051_FCR, 0xff, fcr);

Is 0xff correct here? You only seem interested in FCR_BKPM, FCR_FLCE, FCR_TXPEN so i would of expected a value based around those.

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+	if (ret)
+		netif_err(db, drv, db->ndev, "%s: error %d update bits reg %02x\n",
+			  __func__, ret, DM9051_FCR);
+	return ret;
+}
+
+static int dm9051_set_fcr(struct board_info *db)
+{
+	u8 fcr = 0;
+	int ret;
+

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	if (db->pause.rx_pause)
+		fcr |= FCR_BKPM | FCR_FLCE;
+	if (db->pause.tx_pause)
+		fcr |= FCR_TXPEN;
+
+	ret = regmap_write(db->regmap_dm, DM9051_FCR, fcr);
+	if (ret)
+		netif_err(db, drv, db->ndev, "%s: error %d write reg %02x\n",
+			  __func__, ret, DM9051_FCR);
+	return ret;

I guess you can combine this code somehow, make one call the other?

Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +13 -0
+static int dm9051_mdio_register(struct board_info *db)
+{
+	struct spi_device *spi = db->spidev;
+	int ret;
+
+	db->mdiobus = devm_mdiobus_alloc(&spi->dev);
+	if (!db->mdiobus)
+		return -ENOMEM;
+
+	db->mdiobus->priv = db;
+	db->mdiobus->read = dm9051_mdiobus_read;
+	db->mdiobus->write = dm9051_mdiobus_write;
+	db->mdiobus->name = "dm9051-mdiobus";

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	db->mdiobus->phy_mask = (u32)~GENMASK(1, 1);
+	db->mdiobus->parent = &spi->dev;
+	snprintf(db->mdiobus->id, MII_BUS_ID_SIZE,
+		 "dm9051-%s.%u", dev_name(&spi->dev), spi->chip_select);
+
+	ret = devm_mdiobus_register(&spi->dev, db->mdiobus);
+	if (ret)
+		dev_err(&spi->dev, "Could not register MDIO bus\n");
+
+	return 0;

You should return ret here, since an error might of occurred.

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+static void dm9051_handle_link_change(struct net_device *ndev)
+{
+	struct board_info *db = to_dm9051_board(ndev);
+	int lcl_adv, rmt_adv;
+
+	phy_print_status(db->phydev);
+
+	/* only write pause settings to mac. since mac and phy are integrated
+	 * together, such as link state, speed and duplex are sync already

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	 */
+	if (db->phydev->link) {
+		if (db->pause.autoneg == AUTONEG_ENABLE) {
+			lcl_adv = linkmode_adv_to_mii_adv_t(db->phydev->advertising);
+			rmt_adv = linkmode_adv_to_mii_adv_t(db->phydev->lp_advertising);
+
+			if (lcl_adv & rmt_adv & ADVERTISE_PAUSE_CAP) {
+				db->pause.rx_pause = true;
+				db->pause.tx_pause = true;
+			}

Please look at phydev->pause. It gives you the resolved value, you don't need to work it out for yourself. phydev->asym_pause tells you about asymmetric pause, but you hardware does not support that, so you don't need it.

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

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+static int dm9051_set_pauseparam(struct net_device *ndev,
+				 struct ethtool_pauseparam *pause)
+{
+	struct board_info *db = to_dm9051_board(ndev);
+
+	db->pause = *pause;
+
+	if (pause->autoneg == AUTONEG_DISABLE) {
+		db->phydev->autoneg = AUTONEG_DISABLE;

As i said before, ksetting is used to change this, not pause. Please don't set phydev->autoneg like this.

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+		return dm9051_update_fcr(db);
+	}
+
+	db->phydev->autoneg = AUTONEG_ENABLE;

Nor here.

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

Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +12 -0
+	phy_set_sym_pause(db->phydev, pause->rx_pause, pause->tx_pause,
+			  pause->autoneg);
+	phy_start_aneg(db->phydev);
+	return 0;
+}
 
+static irqreturn_t dm9051_rx_threaded_irq(int irq, void *pw)
+{
+	struct board_info *db = pw;
+	int result, result_tx;
+
+	mutex_lock(&db->spi_lockm);
+	if (netif_carrier_ok(db->ndev)) {

Why is carrier relevant here? Maybe the device is trying to give you the last packets before the carrier went down?

It is also interesting that you don't look at the interrupt service register. Often you need to clear the interrupt by reading the interrupt service register.

Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +13 -0
+		result = regmap_write(db->regmap_dm, DM9051_IMR, IMR_PAR); /* disable int */
+		if (result)
+			goto spi_err;
+
+		do {
+			result = dm9051_loop_rx(db); /* threaded irq rx */
+			if (result < 0)
+				goto spi_err;
+			result_tx = dm9051_loop_tx(db); /* more tx better performance */
+			if (result_tx < 0)
+				goto spi_err;
+		} while (result > 0);
+

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+		result = regmap_write(db->regmap_dm, DM9051_IMR, db->imr_all); /* enable int */
+		if (result)
+			goto spi_err;
+	}
+spi_err:
+	mutex_unlock(&db->spi_lockm);
+
+	return IRQ_HANDLED;
+}
+

Andrew