ADD DM9051 ETHERNET DRIVER
Subsystems (3)
v11 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 (2):
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 | 1169 +++++++++++++++++ drivers/net/ethernet/davicom/dm9051.h | 198 +++ 5 files changed, 1459 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
PATCH v11 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
@@ -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
PATCH v11 2/2 net: Add dm9051 driver
In reply to Joseph CHAMGv1-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.
v10
use regmap APIs for SPI and MDIO,
modify to correcting such as include header files
and program check styles
---
v11
eliminate the redundant code for struct regmap_config data use regmap_read_poll_timeout
use corresponding regmap APIs, i.e. MDIO, SPI
all read/write registers by regmap
all read/write registers with mutex lock by regmap
problem: regmap MDIO and SPI has no .reg_update_bits, I write it in the driver
problem: this chip can support bulk read/write to rx/tx data, but can not support bulk read/write to continue registers, so need read/write register one by one
--- drivers/net/ethernet/davicom/Kconfig | 29 + drivers/net/ethernet/davicom/Makefile | 1 + drivers/net/ethernet/davicom/dm9051.c | 1169 +++++++++++++++++++++++++ drivers/net/ethernet/davicom/dm9051.h | 198 +++++ 4 files changed, 1397 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
@@ -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
@@ -4,3 +4,4 @@ # obj-$(CONFIG_DM9000) += dm9000.o +obj-$(CONFIG_DM9051) += dm9051.o
drivers/net/ethernet/davicom/dm9051.c new file +1169 -0
1,169 lines not on this page · raw message
drivers/net/ethernet/davicom/dm9051.h new file +198 -0
198 lines not on this page · raw message
Signature · 1 line
2.20.1
Re: [PATCH v11, 2/2] net: Add dm9051 driver
Thanks for update, my comments below.
On Thu, Jan 13, 2022 at 9:46 AM Joseph CHAMG <[email protected]> wrote:
Missed commit message.
...
Quoted 7 lines
v1-v4
v5
v6
v7
v8
v9
v10
Changelog should go after the cutter '--- ' line below, it's strange that you did it correctly only for v11 changelog and not for the rest.
...
Quoted 4 lines
Cc Jakub Kicinski <[email protected]>Cc Andrew Lunn <[email protected]>Cc Leon Romanovsky <[email protected]>Cc andy Shevchenko <[email protected]>
Instead, utilize --cc parameter to git send-email ...
...
Quoted 1 line
Reported-by kernel test robot <[email protected]>
New driver can't be reported.
Quoted 3 lines
Signed-off-by Joseph CHAMG <[email protected]>---
v11
...
drivers/net/ethernet/davicom/Kconfig config DM9000_FORCE_SIMPLE_PHY_POLL · line 43
Quoted 5 lines
drivers/net/ethernet/davicom/Kconfig +5
-0+config DM9051 + tristate "DM9051 SPI support" + select PHYLIB + depends on SPI + select CRC32
Please group dependencies first followed by selections. Also, you missed to select corresponding regmap APIs (SPI and MDIO you mentioned).
...
Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11
-0+static int dm9051_map_read (struct board_info *db,u8 reg,unsigned int *prb) +{ +struct net_device *ndev = db->ndev; +int ret =regmap_read (db->regmap, reg, prb); + +if (unlikely (ret)) +netif_err (db, drv, ndev,"%s: error %d reading reg %02x\n" , + __func__, ret, reg); +return ret; +} +
drivers/net/ethernet/davicom/dm9051.c line 42
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+static int dm9051_map_write (struct board_info *db,u8 reg,u16 val) +{ +struct net_device *ndev = db->ndev; +int ret =regmap_write (db->regmap, reg, val); + +if (unlikely (ret)) +netif_err (db, drv, ndev,"%s: error %d writing reg %02x=%04x\n" , + __func__, ret, reg, val); +return ret; +}
Redefining callbacks for the sake of printing messages? Hmm... dunno if it's a good idea here.
...
drivers/net/ethernet/davicom/dm9051.c line 82
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6
-0+ ret =dm9051_map_write (db,DM9051_EPDRL , val &0xff );/* write ctl must once 8-bit */ +if (ret <0 ) +return ret; + ret =dm9051_map_write (db,DM9051_EPDRH , val >>8 );/* write ctl must once 8-bit */ +if (ret <0 ) +return ret;
Wouldn't be better to use bulk write for these?
...
drivers/net/ethernet/davicom/dm9051.c line 124
Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7
-0+ ret =dm9051_map_read (db,DM9051_EPDRH , &eph);/* read ctl must once 8-bit */ +if (ret) +return ret; + ret =dm9051_map_read (db,DM9051_EPDRL , &epl);/* read ctl must once 8-bit */ +if (ret) +return ret; + *val = (eph <<8 ) | epl;
Wouldn't be better to use bulk read for these?
...
drivers/net/ethernet/davicom/dm9051.c line 131
Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4
-0+static bool dm9051_regmap_volatile (struct device *dev,unsigned int reg) +{ +return true;/* true, register can not be cached */ +}
Do you need this wrapper?
...
drivers/net/ethernet/davicom/dm9051.c line 158
Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2
-0+ .lock = dm9051_reg_lock_mutex, + .unlock = dm9051_reg_unlock_mutex,
But this doesn't protect against interleaved accesses. Is it fine?
...
Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11
-0+static int dm9051_map_updbits (struct board_info *db,unsigned int reg, +unsigned int mask,unsigned int val) +{ +unsigned int set_mask = val & mask; +unsigned int readd =0 ;/* clear all insided bits */ +int ret =0 ; + + ret =regmap_read (db->regmap, reg, &readd); +if (ret <0 ) +return ret; +
drivers/net/ethernet/davicom/dm9051.c line 221
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+if ((readd & mask) != set_mask) { + readd &= ~mask; + readd |= set_mask; + + ret =regmap_write (db->regmap, reg, readd); +if (ret <0 ) +return ret; + } +return ret; +}
NIH regmap_update_bits().
...
drivers/net/ethernet/davicom/dm9051.c line 226
Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +4
-0+static bool dm9051_phymap_volatile (struct device *dev,unsigned int reg) +{ +return true;/* true, register can not be cached */ +}
Do you really need this?
...
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+static int dm9051_map_phy_updbits (struct board_info *db,unsigned int reg, +unsigned int mask,unsigned int val) +{ +unsigned int set_mask = mask & val; +unsigned int readd =0 ; +int ret; + + ret =ctrl_dm9051_phyread (db, reg, &readd); +if (ret) +return ret;
drivers/net/ethernet/davicom/dm9051.c line 256
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+ +if ((readd & mask) != set_mask) { + readd &= ~mask; + readd |= set_mask; + ret =ctrl_dm9051_phywrite (db, reg, readd); +if (ret) +return ret; + } +return ret; +}
NIH regmap_update_bits().
...
drivers/net/ethernet/davicom/dm9051.c line 301
Quoted 7 lines
drivers/net/ethernet/davicom/dm9051.c new file +7
-0+ ret =dm9051_map_read (db,DM9051_EPDRL , &mval);/* must read once 8-bit */ +if (ret <0 ) +return ret; + to[0 ] = mval; + ret =dm9051_map_read (db,DM9051_EPDRH , &mval);/* must read once 8-bit */ +if (ret <0 ) +return ret;
Why not _bulk operation?
...
drivers/net/ethernet/davicom/dm9051.c line 312
Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2
-0+dm9051_map_write (db,DM9051_EPDRH , data[1 ]);/* must write once 8-bit */ +dm9051_map_write (db,DM9051_EPDRL , data[0 ]);/* must write once 8-bit */
Ditto.
...
drivers/net/ethernet/davicom/dm9051.c line 332
Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +7
-0+ ret =dm9051_map_read (db,DM9051_PIDH , &wpidh); +if (ret <0 ) +return ret; + ret =dm9051_map_read (db,DM9051_PIDL , &wpidl); +if (ret <0 ) +return ret; + id = (wpidh <<8 ) | wpidl;
Ditto.
...
drivers/net/ethernet/davicom/dm9051.c line 338
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6
-0+if (id ==DM9051_ID ) { +dev_info (dev,"chip %04x found\n" , id); +return 0 ; + } + +dev_info (dev,"chipid error as %04x !\n" , id);
Why not dev_err()?
Quoted 1 line
+ return
-ENODEV;
Please, use standard pattern, i.e. check for errors first:
if (error condition) {
...
return err;
}...
drivers/net/ethernet/davicom/dm9051.c line 357
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6
-0+for (i =0 ; i <ETH_ALEN ; i++) { + ret =dm9051_map_read (db,DM9051_PAR + i, &mval); +if (unlikely (ret)) +return ret; + addr[i] = mval; + }
_bulk?
...
drivers/net/ethernet/davicom/dm9051.c line 366
Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8
-0+if (is_valid_ether_addr (addr)) { +eth_hw_addr_set (ndev, addr); +return 0 ; + } + +eth_hw_addr_random (ndev); +dev_dbg (&db->spidev->dev,"Use random MAC address\n" ); +return 0 ;
Check for (kinda) error first.
...
drivers/net/ethernet/davicom/dm9051.c line 408
Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2
-0+snprintf (phy_id,MII_BUS_ID_SIZE +3 ,PHY_ID_FMT , + db->mdiobus->id,DM9051_PHY_ID );
(1)
drivers/net/ethernet/davicom/dm9051.c line 411
Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.c new file +3
-0+ db->phydev =phy_connect (db->ndev, phy_id, dm9051_handle_link_change, +PHY_INTERFACE_MODE_MII ); +
This blank line is misplaced. Should be in (1).
drivers/net/ethernet/davicom/dm9051.c line 414
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
+}
...
Quoted 1 line
+ rdptr = (u8 *)skb_put(skb, rxlen - 4);
Do you need this casting?
...
drivers/net/ethernet/davicom/dm9051.c line 696
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6
-0+ ret =dm9051_map_write (db,DM9051_TXPLL , len); +if (ret <0 ) +return ret; + ret =dm9051_map_write (db,DM9051_TXPLH , len >>8 ); +if (ret <0 ) +return ret;
_bulk?
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* these registers can't write by inblk, must write one by one + */
Why? regmap bulk does exactly this (if properly configured).
Quoted 5 lines
drivers/net/ethernet/davicom/dm9051.c new file +5
-0+for (i =0 ; i <ETH_ALEN ; i++) { + result =dm9051_map_write (db,DM9051_PAR + i, ndev->dev_addr[i]); +if (result <0 ) + goto spi_err; + }
...
Review
Quoted 2 lines
(quoted diff) +2
-0+ /* these registers can't write by inblk, must write one by one + */
Ditto.
Quoted 8 lines
drivers/net/ethernet/davicom/dm9051.c new file +8
-0+for (oft =DM9051_MAR , i =0 ; i <4 ; i++) { + result =dm9051_map_write (db, oft++, db->hash_table[i]); +if (result <0 ) + goto spi_err; + result =dm9051_map_write (db, oft++, db->hash_table[i] >>8 ); +if (result <0 ) + goto spi_err; + }
...
Quoted 1 line
+ db->hash_table[hash_val / 16] |= (u16)1 << (
hash_val% 16);
Do you need casting? Can you use 1U or BIT()?
...
drivers/net/ethernet/davicom/dm9051.c line 1031
Quoted 9 lines
drivers/net/ethernet/davicom/dm9051.c new file +8
-0+static int devm_regmap_init_dm9051 (struct spi_device *spi,struct board_info *db) +{ + regconfig.lock_arg = db;/* regmap lock/unlock essential */ + + db->regmap =devm_regmap_init_spi (db->spidev, ®config); +if (IS_ERR (db->regmap)) +return PTR_ERR (db->regmap); +return 0 ;
return PTR_ERR_OR_ZERO(...);
Quoted 1 line
+}
...
Quoted 1 line
+
db->mdiobus->phy_mask= (u32)~BIT(1);
GENMASK()
...
drivers/net/ethernet/davicom/dm9051.c line 1053
Quoted 2 lines
drivers/net/ethernet/davicom/dm9051.c new file +2
-0+ ret =devm_mdiobus_register (&spi->dev, db->mdiobus); +if (ret <0 ) {
What does > 0 mean?
drivers/net/ethernet/davicom/dm9051.c line 1057
Quoted 5 lines
drivers/net/ethernet/davicom/dm9051.c new file +4
-0+dev_err (&spi->dev,"Could not register MDIO bus\n" ); +return ret; + } +return 0 ;
Can it be
return ret;
?
...
drivers/net/ethernet/davicom/dm9051.c line 1070
Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3
-0+if (IS_ERR (db->phymap)) +return PTR_ERR (db->phymap); +return 0 ;
As above.
...
drivers/net/ethernet/davicom/dm9051.c line 1101
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.c new file +6
-0+ db->kwr_task_kw =kthread_run (kthread_worker_fn, &db->kw,"dm9051" ); +if (IS_ERR (db->kwr_task_kw)) +return PTR_ERR (db->kwr_task_kw); + +mutex_init (&db->spi_lock); +mutex_init (&db->reg_mutex);
Are you sure it's good to have thread running without initializations of locks, etc?
...
drivers/net/ethernet/davicom/dm9051.c line 1154
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+static int dm9051_drv_remove (struct spi_device *spi) +{ +struct device *dev = &spi->dev; +struct net_device *ndev =dev_get_drvdata (dev); +struct board_info *db =to_dm9051_board (ndev); + +phy_disconnect (db->phydev); +kthread_stop (db->kwr_task_kw); +return 0 ; +}
Seems like a wrong order of the resource freeing.
...
Quoted 1 line
+++
b/drivers/net/ethernet/davicom/dm9051.h
Do oyu need it to be a separate header?
...
Quoted 1 line
+#include <
linux/bitfield.h>
Not sure I saw the user of this header below.
Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.h new file +2
-0+#include <linux/mutex.h> +#include <linux/netdevice.h>
...
drivers/net/ethernet/davicom/dm9051.h line 163
Quoted 4 lines
drivers/net/ethernet/davicom/dm9051.h new file +4
-0+struct rx_ctl_mach { +u16 status_err_counter;/* 'Status Err' counter */ +u16 large_err_counter;/* 'Large Err' counter */ +u16 DO_FIFO_RST_counter;/* 'fifo_reset' counter */
Can you rather have these comments in kernel doc?
Quoted 1 line
+};
...
Quoted 1 line
+struct
board_info
Why do you need this definition in the header?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Jakub Kicinski
maintainerRe: [PATCH v11, 2/2] net: Add dm9051 driver
Quoted 6 lines · On Thu, 13 Jan 2022 12:16:38 +0200 Andy Shevchenko wrote:
Quoted 4 lines
Cc Jakub Kicinski <[email protected]>Cc Andrew Lunn <[email protected]>Cc Leon Romanovsky <[email protected]>Cc andy Shevchenko <[email protected]>Instead, utilize --cc parameter to
git send-email ...
.. or put them under the --- so git cuts them off when patch is applied.