Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded Joseph CHAMG 3 participants 5 messages Feb 2, 2022 – Feb 4, 2022

Subsystems (3)

v17 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v17 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         | 1181 +++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         |  159 +++
 5 files changed, 1434 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 v17 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
maintainer
#2 lore raw

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

Quoted 4 lines · Joseph CHAMG

From: JosephCHANG <[email protected]>

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

Please address my comments on v13 that still apply.

Quoted 80 lines
---
 .../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
#3 lore raw

PATCH v17 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

v17

Chnage from kthread_work to work_struct

 drivers/net/ethernet/davicom/Kconfig  |   31 +
 drivers/net/ethernet/davicom/Makefile |    1 +
 drivers/net/ethernet/davicom/dm9051.c | 1181 +++++++++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h |  159 ++++
 4 files changed, 1372 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 +1181 -0

1,181 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
#4 lore raw

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

Quoted 3 lines · Joseph CHAMG

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

...

Quoted 1 line

+ ret = regmap_write(db->regmap_dm, DM9051_EPCR, 0x0);

0x0 --> 0

Quoted 2 lines
(quoted diff) +2 -0
+       if (ret)
+               return ret;

...

Quoted 1 line

+ return regmap_write(db->regmap_dm, DM9051_EPCR, 0x0);

Ditto.

...

Quoted 1 line

+ ret = regmap_update_bits(db->regmap_dm, DM9051_FCR, 0xff, fcr);

GENMASK(7, 0) ?

...

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

Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8 -0
+static int dm9051_map_chipid(struct board_info *db)
+{
+       struct device *dev = &db->spidev->dev;
+       unsigned int ret;
+       unsigned short wid;
+       u8 buff[6];
+
+       ret = regmap_bulk_read(db->regmap_dmbulk, DM9051_VIDL, buff, 6);

sizeof(buff)

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

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+       if (ret < 0) {
+               netif_err(db, drv, db->ndev, "%s: error %d bulk_read reg %02x\n",
+                         __func__, ret, DM9051_VIDL);
+               return ret;
+       }
 
+       wid = buff[3] << 8 | buff[2];

get_unaligned_le16()

Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8 -0
+       if (wid != DM9051_ID) {
+               dev_err(dev, "chipid error as %04x !\n", wid);
+               return -ENODEV;
+       }
+
+       dev_info(dev, "chip %04x found\n", wid);
+       return 0;
+}

...

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

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+static int dm9051_map_etherdev_par(struct net_device *ndev, struct board_info *db)
+{
+       u8 addr[ETH_ALEN];
+       int ret;
+
+       ret = regmap_bulk_read(db->regmap_dmbulk, DM9051_PAR, addr, ETH_ALEN);

sizeof(addr)

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

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+       if (ret < 0)
+               return ret;
+
+       if (!is_valid_ether_addr(addr)) {
+               eth_hw_addr_random(ndev);
 
+               ret = regmap_bulk_write(db->regmap_dmbulk, DM9051_PAR, ndev->dev_addr, ETH_ALEN);

Ditto.

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+               if (ret < 0)
+                       return ret;
+
+               dev_dbg(&db->spidev->dev, "Use random MAC address\n");
+               return 0;
+       }
+
+       eth_hw_addr_set(ndev, addr);
+       return 0;
+}

...

Quoted 1 line

+ db->mdiobus->phy_mask = (u32)~GENMASK(1, 1);

For a single bit it might be better to use BIT(1)

...

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

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +5 -0
+       ret = devm_mdiobus_register(&spi->dev, db->mdiobus);
+       if (ret)
+               dev_err(&spi->dev, "Could not register MDIO bus\n");
+
 
+       return 0;

return ret; ?

...

Quoted 1 line

+ snprintf(phy_id, MII_BUS_ID_SIZE + 3, PHY_ID_FMT,

sizeof(phy_id) + 3

Quoted 1 line

+ db->mdiobus->id, DM9051_PHY_ADDR);

...

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

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+       if (IS_ERR(db->phydev))
+               return PTR_ERR(db->phydev);
+       return 0;

return PTR_ERR_OR_ZERO();

...

Quoted 1 line

+ if ((rxbyte & 0xff) != DM9051_PKT_RDY)

GENMASK(7, 0) ?

Quoted 1 line

+ break; /* exhaust-empty */

...

Quoted 1 line

+ ret = regmap_write(db->regmap_dm, DM9051_ISR, 0xff); /* to stop mrcmd */

Ditto.

Quoted 2 lines
(quoted diff) +2 -0
+               if (ret)
+                       return ret;

...

Quoted 1 line

+ ret = regmap_write(db->regmap_dm, DM9051_ISR, 0xff); /* to stop mrcmd */

Ditto.

Perhaps it needs its own definition after all?

Quoted 2 lines
(quoted diff) +2 -0
+               if (ret)
+                       return ret;

...

Quoted 1 line

+spi_err:

out_unlock:

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+       mutex_unlock(&db->spi_lockm);
+
+       return IRQ_HANDLED;

...

Quoted 1 line

+ result = regmap_bulk_write(db->regmap_dmbulk, DM9051_MAR, db->rctl.hash_table, 8);

Is hash_table an array? Then sizeof() or ARRAY_SIZE() should work.

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

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+       if (result < 0) {
+               netif_err(db, drv, ndev, "%s: error %d bulk writing reg %02x, len %d\n",
+                         __func__, result, DM9051_MAR, 8);

Ditto.

Quoted 2 lines
(quoted diff) +2 -0
+               goto spi_err;
+       }

...

Quoted 1 line

+spi_err:

out_unlock:

You need to describe what will be done if one goes to the label.

Quoted 1 line

+ mutex_unlock(&db->spi_lockm);

...

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+       /* The whole dm9051 chip registers could not be accessed within 1 ms
+        * after above GPR power on control
+        */
+       mdelay(1);

Why atomic?

...

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

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+       ret = request_threaded_irq(spi->irq, NULL, dm9051_rx_threaded_irq,
+                                  IRQF_TRIGGER_LOW | IRQF_ONESHOT,

Why do you ignore IRQ flags from DT?

Quoted 1 line

+ ndev->name, db);

...

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

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+       /* the multicast address in Hash Table : 64 bits */
+       netdev_for_each_mc_addr(ha, ndev) {
+               hash_val = ether_crc_le(ETH_ALEN, ha->addr) & 0x3f;

GENMASK() ?

Quoted 1 line

+ rxctrl.hash_table[hash_val / 16] |= 1U << (hash_val % 16);

BIT() ?

Quoted 1 line

+ }

...

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+       /* schedule work to do the actual set of the data if needed */
 
+       if (memcmp(&db->rctl, &rxctrl, sizeof(rxctrl))) {
+               memcpy(&db->rctl, &rxctrl, sizeof(rxctrl));

Hmm... This is interesting...

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+               schedule_work(&db->rxctrl_work);
+       }

...

Quoted 1 line

+ ret = regmap_bulk_write(db->regmap_dmbulk, DM9051_PAR, ndev->dev_addr, ETH_ALEN);

sizeof() ?

...

Quoted 1 line

+ int ret = 0;

Redundant assignment.

...

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

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +5 -0
+       ret = devm_register_netdev(dev, ndev);
+       if (ret) {
 
+               dev_err(dev, "failed to register network device\n");
+               phy_disconnect(db->phydev);
+               return ret;

phy_disconnect();
return dev_err_probe();

Quoted 1 line

+ }

...

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

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+err_stopthread:
+       return ret;

Useless. Return in-place.

...

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

Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+static struct spi_driver dm9051_driver = {
+       .driver = {
+               .name = DRVNAME_9051,
+               .of_match_table = dm9051_match_table,
+       },
+       .probe = dm9051_probe,
+       .remove = dm9051_drv_remove,
+       .id_table = dm9051_id_table,
+};
 
+

Redundant blank line.

Quoted 1 line

+module_spi_driver(dm9051_driver);

...

+ bits.h

drivers/net/ethernet/davicom/dm9051.h line 11

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.h new file +2 -0
+#include <linux/netdevice.h>
 
+#include <linux/mii.h>

How is that header being used in this header?

Quoted 1 line

+#include <linux/types.h>

...

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

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.h new file +2 -0
+#define INTCR_POL_LOW          BIT(0)
+#define INTCR_POL_HIGH         (0 << 0)

In this case, be consistent:

#define INTCR_POL_LOW (1 << 0)
#define INTCR_POL_HIGH (0 << 0)

Signature · 2 lines
With Best Regards,
Andy Shevchenko