Skip to contentSkip to conversation

ADD DM9051 ETHERNET DRIVER

PATCH Superseded Joseph CHAMG 3 participants 5 messages Jan 27, 2022 – Jan 28, 2022

Subsystems (3)

v14 of 20 Superseded Latest is v20 →

lore
Share & tools
lore
#0 lore raw

v14 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         | 1151 +++++++++++++++++
 drivers/net/ethernet/davicom/dm9051.h         |  159 +++
 5 files changed, 1404 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 v14 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 v14 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

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

1,151 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
maintainer
#3 lore raw

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

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

Quoted 13 lines · On Thu, 27 Jan 2022 11:27:01 +0800 Joseph CHAMG wrote:

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

drivers/net/ethernet/davicom/dm9051.c new file +8 -0
+/* event: write into the mac registers and eeprom directly
+ */
+static int dm9051_set_mac_address(struct net_device *ndev, void *p)
+{
+	struct board_info *db = to_dm9051_board(ndev);
+	int ret;
+
+	ret = eth_mac_addr(ndev, p);

You should not be using this helper if the write can fail. See what this function does internally and:

- put the eth_prepare_mac_addr_change() call here

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

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7 -0
+	if (ret < 0)
+		return ret;
+
+	ret = regmap_bulk_write(db->regmap_dmbulk, DM9051_PAR, ndev->dev_addr, ETH_ALEN);
+	if (ret < 0)
+		netif_err(db, drv, ndev, "%s: error %d bulk writing reg %02x, len %d\n",
+			  __func__, ret, DM9051_PAR, ETH_ALEN);

- put the eth_commit_mac_addr_change() call here

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

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +5 -0
+	return ret;
+}
 
+static void dm9051_netdev(struct net_device *ndev)
+{
+	ndev->mtu = 1500;

Unnecessary, ether_setup() does this already.

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

Quoted 1 line
drivers/net/ethernet/davicom/dm9051.c new file +1 -0
+	ndev->if_port = IF_PORT_100BASET;

Why set this? The if_port API is a leftover from very old 10Mbit Ethernet days, we have ethtool link APIs now.

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

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+	ndev->netdev_ops = &dm9051_netdev_ops;
+	ndev->ethtool_ops = &dm9051_ethtool_ops;

Just inline there two lines into the caller and remove the helper. dm9051_netdev() does not sound like a function that does setup.

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7 -0
+}
+
+static int dm9051_map_init(struct spi_device *spi, struct board_info *db)
+{
+	/* create two regmap instances,
+	 * run read/write and bulk_read/bulk_write individually,
+	 * to resolve regmap execution confliction problem

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	 */
+	regconfigdm.lock_arg = db;
+	db->regmap_dm = devm_regmap_init_spi(db->spidev, &regconfigdm);
+
+	if (IS_ERR(db->regmap_dm))
+		return PTR_ERR_OR_ZERO(db->regmap_dm);
+
+	regconfigdmbulk.lock_arg = db;
+	db->regmap_dmbulk = devm_regmap_init_spi(db->spidev, &regconfigdmbulk);
+

Please remove all the empty lines between function call and error checking the result.

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

Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2 -0
+	if (IS_ERR(db->regmap_dmbulk))
+		return PTR_ERR_OR_ZERO(db->regmap_dmbulk);

Why _OR_ZERO() when you're in a IS_ERR() condition already?

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

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

All the state must be initialized before netdev is registered, otherwise another thread may immediately open the device and start to transmit.

Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6 -0
+	return 0;
+
+err_stopthread:
+	kthread_stop(db->kwr_task_kw);
+	return ret;
+}
#4 lore raw

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

In reply to Joseph CHAMG

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

Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7 -0
+static int dm9051_mdiobus_read(struct mii_bus *bus, int addr, int regnum)
+{
+	struct board_info *db = bus->priv;
+	unsigned int val = 0xffff;
+	int ret;
+
+	if (addr == DM9051_PHY_ID) {

Thanks for fixing the variable name. But don't you think it would of made sense to rename DM9051_PHY_ID as well? DM9051_PHY_ADDR?

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

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

Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10 -0
+	u8 fcr = 0;
+	int ret;
+
+	db->eth_pause = *pause;
+
+	if (pause->autoneg)
+		db->phydev->autoneg = AUTONEG_ENABLE;
+	else
+		db->phydev->autoneg = AUTONEG_DISABLE;
+

pause->autoneg means that pause is negotiated as part of autoneg in general. But pause->autoneg does not mean turn on autoneg. The ksetting calls should be used for that.

If pause->autoneg is false, you write the pause settings direct to the MAC. If it is true, you should call phy_set_sym_pause(). Once negotiation has completed the link change callback will be called, and you program the MAC with what has been negotiated.

Andrew