Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded Joseph CHAMG 3 participants 6 messages Dec 27, 2021 – Dec 28, 2021 1 message not in the archive

Subsystems (3)

v9 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v9 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
net: Add dm9051 driver

 .../bindings/net/davicom,dm9051.yaml          |   62 +
 drivers/net/ethernet/davicom/Kconfig          |   29 +
 drivers/net/ethernet/davicom/Makefile         |    1 +
 drivers/net/ethernet/davicom/dm9051.c         | 1019 +++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         |  189 +++
 5 files changed, 1300 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 v9 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 v9 2/2 net: Add dm9051 driver

In reply to Joseph CHAMG

v1-v4

Add davicom dm9051 spi ethernet driver. The driver work for the device platform with spi master

Test ok with raspberry pi 2 and pi 4, the spi configure used in my raspberry pi 4 is spi0.1, spi speed 31200000, and INT by pin 26.

v5

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.

v6

remove the redundant code that phylib has support, adjust to be the reasonable sequence,
fine tune comments, add comments for pause function support

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

v7

read/write registers must return error code to the callet, add to enable pause processing

v8

not parmanently set MAC by .ndo_set_mac_address

correct rx function such as clear ISR,
inblk avoid stack buffer,
simple skb buffer process and
easy use netif_rx_ni.

simplely queue init and wake the queues, limit the start_xmit function use netif_stop_queue.

descript that schedule delay is essential
for tx_work and rxctrl_work

eliminate ____cacheline_aligned and
add static int msg_enable.

v9

use phylib, no need 'select MII' in Kconfig, make it clear in dm9051_xfer when using spi_sync, improve the registers read/write so that error code return as far as possible up the call stack.

Cc Jakub Kicinski <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc Leon Romanovsky <[email protected]>
---
 drivers/net/ethernet/davicom/Kconfig  |   29 +
 drivers/net/ethernet/davicom/Makefile |    1 +
 drivers/net/ethernet/davicom/dm9051.c | 1019 +++++++++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h |  189 +++++
 4 files changed, 1238 insertions(+)
 create mode 100644 drivers/net/ethernet/davicom/dm9051.c
 create mode 100644 drivers/net/ethernet/davicom/dm9051.h
drivers/net/ethernet/davicom/Kconfig +29 -0
index 7af86b6d4150..887fe6e56d56 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,19 @@ 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
+	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 +1019 -0

1,019 lines not on this page · raw message

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

189 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#3 lore raw

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

Quoted 18 lines · On Mon, 27 Dec 2021 18:02:33 +0800 Joseph CHAMG wrote:
drivers/net/ethernet/davicom/dm9051.c new file +18 -0
+static inline void dm9051_phyup_lock(struct board_info *db)
+{
+	int val, ret;
+
+	mutex_lock(&db->addr_lock);
+
+	ret = dm9051_phy_read(db, MII_BMCR, &val);
+	if (ret)
+		return;
+
+	/* BMCR Power-up PHY */
+	ret = dm9051_phy_write(db, MII_BMCR, val & ~0x0800);
+	if (ret)
+		return;
+
+	/* GPR Power-up PHY */
+	dm9051_iow(db, DM9051_GPR, 0);
+	mdelay(1); /* need for activate phyxcer */

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+
+	mutex_unlock(&db->addr_lock);
+}
+
+static inline void dm9051_phydown_lock(struct board_info *db)
+{
+	mutex_lock(&db->addr_lock);
+	dm9051_iow(db, DM9051_GPR, 0x01); /* Power-Down PHY */
+	mutex_unlock(&db->addr_lock);
+}

Please remove the 'inline' qualifier from functions in C sources. Compiler will know which static functions to inline these days.

#4 lore raw

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

In reply to Joseph CHAMG

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+#define	dm9051_inb(db, reg, prxb)	dm9051_xfer(db, DM_SPI_RD | (reg), NULL, prxb, 1)
+#define	dm9051_outb(db, reg, ptxb)	dm9051_xfer(db, DM_SPI_WR | (reg), ptxb, NULL, 1)
+#define	dm9051_inblk(db, buff, len)	dm9051_xfer(db, DM_SPI_RD | DM_SPI_MRCMD, NULL, buff, len)
+#define	dm9051_outblk(db, buff, len)	dm9051_xfer(db, DM_SPI_WR | DM_SPI_MWCMD, buff, NULL, len)

How many times do i have to say it, no wrappers. This makes it a lot less obvious you are throwing away the error code from dm9051_xfer().

Quoted 16 lines
drivers/net/ethernet/davicom/dm9051.c new file +16 -0
+/* spi low level code */
+static int dm9051_xfer(struct board_info *db, u8 cmd, u8 *txb, u8 *rxb, unsigned int len)
+{
+	struct device *dev = &db->spidev->dev;
+	int ret;
+
+	db->cmd[0] = cmd;
+	db->spi_xfer2[0].tx_buf = &db->cmd[0];
+	db->spi_xfer2[0].rx_buf = NULL;
+	db->spi_xfer2[0].len = 1;
+	db->spi_xfer2[1].tx_buf = txb;
+	db->spi_xfer2[1].rx_buf = rxb;
+	db->spi_xfer2[1].len = len;
+	ret = spi_sync(db->spidev, &db->spi_msg);
+	if (ret < 0)
+		dev_err(dev, "spi burst cmd 0x%02x, ret=%d\n", cmd, ret);

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	return ret;
+}
+
+static u8 dm9051_getreg(struct board_info *db, unsigned int reg)
+{
+	int ret;
+	u8 rxb[1];
+
+	ret = dm9051_inb(db, reg, rxb);
+	if (ret < 0) {

If ret < 0 you probably have had an SPI error, -EIO, or maybe -ETIMEDOUT?

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+		if (reg == DM9051_NSR)
+			return NSR_TX2END | NSR_TX1END;
+		if (reg == DM9051_EPCR)
+			return (u8)(~EPCR_ERRE);

So please add a comment explaining why it is O.K. to return these values when you probably cannot even talk to the device.

Review

Quoted 1 line
(quoted diff) +1 -0
+		return 0;

And here you should be returning the error code. And whatever called dm9051_getreg() needs to deal with that error code, probably returning it to its caller, etc.

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

Quoted 12 lines
drivers/net/ethernet/davicom/dm9051.c new file +11 -0
+	}
+	return rxb[0];
+}
+
 
+static int dm9051_phy_read(struct board_info *db, int reg, int *pvalue)
+{
+	int ret;
+	u8 check_val;
+	u8 eph, epl;
+
+	dm9051_iow(db, DM9051_EPAR, DM9051_PHY | reg);

dm9051_iow() returns an error code. You should check for an error, and return it. dm9051_mdio_read() already does the correct thing, passing the error code along. The MDIO core will then pass the error along to phylib, and phylib will probably report the error to user space and probably disable the PHY, since the PHY is no longer accessible. That is how error handling should work.

Since you still do not have the trivial things correct, i've not done a deep review yet. I would not expect this driver to be merged very soon, and until somebody does make a deeper review, there could be more serious things wrong with this driver, like locking.

Andrew

A message that is not in the archive (sent off-list or not yet received): CAHp75Vfgd=O_SukOrD4Adw4v7JdPBWsVsjwkj2-TiRy=Vk1mPA@mail.gmail.com

#6 lore raw

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

Quoted 18 lines
    +static int dm9051_mdio_read(struct mii_bus *mdiobus, int phy_id, int reg)
    +{
    +       struct board_info *db = mdiobus->priv;
    +       int val, ret;
    +
    +       if (phy_id == DM9051_PHY_ID) {
    +               mutex_lock(&db->addr_lock);
    +               ret = dm9051_phy_read(db, reg, &val);
    +               mutex_unlock(&db->addr_lock);
    +               if (ret)
    +                       return ret;
    +               return val;
    +       }
    +
    +       return 0xffff;
Quoted 1 line

Hmm.. can we rather use regmap APIs for SPI and MDIO?

Hi Andy

regmap via MDIO is very new, but yes it exists. This driver just needs SPI which has had regmap support for a long time.

However, i suspect the submitter is trying hard to keep the driver in a shape they can reuse the core with other OSes. At least that is my feeling give the way requests for changes are handled. Maybe forcing the use of regmap is actually a good idea, to break them away from that bad idea.

Andrew