Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded Joseph CHAMG 3 participants 5 messages Dec 16, 2021

Subsystems (3)

v6 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v6 cover ADD DM9051 ETHERNET DRIVER

remove the redundant code that phylib has support, adjust to be the reasonable sequence for init operations in dm9051_probe and phy_start

DM9051 is a spi interface chip,
need only 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          |  30 +
 drivers/net/ethernet/davicom/Makefile         |   1 +
 drivers/net/ethernet/davicom/dm9051.c         | 898 ++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         | 188 ++++
 5 files changed, 1179 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 v6 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 v6 2/2 net: Add dm9051 driver

In reply to Joseph CHAMG

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

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.

---
 drivers/net/ethernet/davicom/Kconfig  |  30 +
 drivers/net/ethernet/davicom/Makefile |   1 +
 drivers/net/ethernet/davicom/dm9051.c | 898 ++++++++++++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h | 188 ++++++
 4 files changed, 1117 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 +898 -0

898 lines not on this page · raw message

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

188 lines not on this page · raw message

Signature · 1 line
2.20.1
maintainer
#3 lore raw

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

Quoted 10 lines · On Thu, 16 Dec 2021 17:32:46 +0800 Joseph CHAMG wrote:

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

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.

Please install sparse and make sure the driver builds cleanly with W=1 C=1 flags.

drivers/net/ethernet/davicom/dm9051.c:349:26: warning: symbol 'dm9051_ethtool_ops' was not declared. Should it be static? drivers/net/ethernet/davicom/dm9051.c:381:31: warning: incorrect type in initializer (different base types)
drivers/net/ethernet/davicom/dm9051.c:381:31: expected int rxlen
drivers/net/ethernet/davicom/dm9051.c:381:31: got restricted __le16 [usertype] rxlen
drivers/net/ethernet/davicom/dm9051.c:421:31: warning: restricted __le16 degrades to integer drivers/net/ethernet/davicom/dm9051.c:426:23: warning: incorrect type in assignment (different base types)
drivers/net/ethernet/davicom/dm9051.c:426:23: expected int rxlen
drivers/net/ethernet/davicom/dm9051.c:426:23: got restricted __le16 [usertype] rxlen

#4 lore raw

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

In reply to Joseph CHAMG
Quoted 10 lines · Joseph CHAMG

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

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.

You have been asked two or three times now to include a list of what you changed relative to the previous version. This is expected for kernel patches, just look at other submissions.

Quoted 21 lines
drivers/net/ethernet/davicom/dm9051.c new file +21 -0
+/* spi low level code */
+static int
+dm9051_xfer(struct board_info *db, u8 cmdphase, u8 *txb, u8 *rxb, unsigned int len)
+{
+	struct device *dev = &db->spidev->dev;
+	int ret = 0;
+
+	db->cmd[0] = cmdphase;
+	db->spi_xfer2[0].tx_buf = &db->cmd[0];
+	db->spi_xfer2[0].rx_buf = NULL;
+	db->spi_xfer2[0].len = 1;
+	if (!rxb) {
+		db->spi_xfer2[1].tx_buf = txb;
+		db->spi_xfer2[1].rx_buf = NULL;
+		db->spi_xfer2[1].len = len;
+	} else {
+		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_msg2);

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	if (ret < 0)
+		dev_err(dev, "dm9Err spi burst cmd 0x%02x, ret=%d\n", cmdphase, ret);
+	return ret;
+}
+
+static u8 dm9051_ior(struct board_info *db, unsigned int reg)
+{
+	u8 rxb[1];
+
+	dm9051_xfer(db, DM_SPI_RD | reg, NULL, rxb, 1);

dm9051_xfer() returns an error code. You should pass it up the call stack. We want to know about errors.

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

Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3 -0
+/* basic read/write to phy
+ */
+static int dm_phy_read(struct board_info *db, int reg)

Does this comment have any value? From the function name i can see this is a PHY read.

And you still don't have consistent prefix of dm9051_

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

Quoted 12 lines
drivers/net/ethernet/davicom/dm9051.c new file +12 -0
+{
+	int ret;
+	u8 check_val;
+
+	dm9051_iow(db, DM9051_EPAR, DM9051_PHY | reg);
+	dm9051_iow(db, DM9051_EPCR, EPCR_ERPRR | EPCR_EPOS);
+	ret = read_poll_timeout(dm9051_ior, check_val, !(check_val & EPCR_ERRE), 100, 10000,
+				true, db, DM9051_EPCR);
+	dm9051_iow(db, DM9051_EPCR, 0x0);
+	if (ret) {
+		netdev_err(db->ndev, "timeout read phy register\n");
+		return DM9051_PHY_NULLVALUE;

No, return whatever read_poll_timeout() returned, probably -ETIMEDOUT. You need to report the error all the way up the call stack.

Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+	}
+	ret = (dm9051_ior(db, DM9051_EPDRH) << 8) | dm9051_ior(db, DM9051_EPDRL);
+	return ret;
+}
+
+static void dm_phy_write(struct board_info *db, int reg, int value)
+{
+	int ret;
+	u8 check_val;

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+
+	dm9051_iow(db, DM9051_EPAR, DM9051_PHY | reg);
+	dm9051_iow(db, DM9051_EPDRL, value);
+	dm9051_iow(db, DM9051_EPDRH, value >> 8);
+	dm9051_iow(db, DM9051_EPCR, EPCR_EPOS | EPCR_ERPRW);
+	ret = read_poll_timeout(dm9051_ior, check_val, !(check_val & EPCR_ERRE), 100, 10000,
+				true, db, DM9051_EPCR);
+	dm9051_iow(db, DM9051_EPCR, 0x0);
+	if (ret)
+		netdev_err(db->ndev, "timeout write phy register\n");

And you need to return ret to the caller.

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

Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +13 -0
+static int dm9051_mdio_read(struct mii_bus *mdiobus, int phy_id, int reg)
+{
+	struct board_info *db = mdiobus->priv;
+	int val;
+
+	if (phy_id == DM9051_PHY_ID) {
+		mutex_lock(&db->addr_lock);
+		val = dm_phy_read(db, reg);
+		mutex_unlock(&db->addr_lock);
+		return val;
+	}
+
+	return DM9051_PHY_NULLVALUE;

Please just use 0xffff. Don't hide it.

The MDIO bus has a pull up on the data line. So if you try to read from a device which does not exist, you get all 1s returned. Registers are 16 bit wide, so you get 0xffff. When the MDIO is registered the bus will be probed, and when the read returns 0xffff it knows there is no device at that address. This is the only time you should use 0xffff. It is not an error, it simply indicates there is no device there.

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

Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4 -0
+
+/* read chip id
+ */
+static unsigned int dm9051_chipid(struct board_info *db)

Don't you think we can work out this function reads the chip id from the name of the function. Please only have comments if they are actually useful.

Useful comments tend to explain why something is being done, not what is being done.

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++;
+
+	dm9051_iow(db, DM9051_FCR, FCR_FLOW_ENABLE); /* FlowCtrl */

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +9 -0
+	dm9051_iow(db, DM9051_PPCR, PPCR_PAUSE_COUNT); /* Pause Pkt Count */
+	dm9051_iow(db, DM9051_LMCR, db->lcr_all); /* LEDMode1 */
+	dm9051_iow(db, DM9051_INTCR, INTCR_POL_LOW); /* INTCR */
+}
 
+static void dm_handle_link_change(struct net_device *ndev)
+{
+	/* MAC and phy are integrated together, such as link state, speed,
+	 * and Duplex are sync inside
+	 */

What about Pause?

dm9051_fifo_reset() does:

	dm9051_iow(db, DM9051_FCR, FCR_FLOW_ENABLE); /* FlowCtrl */

Is this enabling Pause processing? Do you need to disable it if autoneg decided it is not wanted?

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;
+	unsigned int id;

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	int ret = 0;
+
+	ndev = devm_alloc_etherdev(dev, sizeof(struct board_info));
+	if (!ndev)
+		return -ENOMEM;
+
+	SET_NETDEV_DEV(ndev, dev);
+	dev_set_drvdata(dev, ndev);
+	db = netdev_priv(ndev);
+	memset(db, 0, sizeof(struct board_info));

No need to use memset. If you look at how

devm_alloc_etherdev() is implemented you end up here:

https://elixir.bootlin.com/linux/v5.16-rc5/source/net/core/dev.c#L10801

The z in kvzalloc() means zero.

Andrew