ADD DM9051 ETHERNET DRIVER
Subsystems (3)
v7 cover ADD DM9051 ETHERNET DRIVER
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 | 998 ++++++++++++++++++ drivers/net/ethernet/davicom/dm9051.h | 194 ++++ 5 files changed, 1285 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 v7 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 v7 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
--- drivers/net/ethernet/davicom/Kconfig | 30 + drivers/net/ethernet/davicom/Makefile | 1 + drivers/net/ethernet/davicom/dm9051.c | 998 ++++++++++++++++++++++++++ drivers/net/ethernet/davicom/dm9051.h | 194 +++++ 4 files changed, 1223 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
@@ -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
@@ -4,3 +4,4 @@ # obj-$(CONFIG_DM9000) += dm9000.o +obj-$(CONFIG_DM9051) += dm9051.o
drivers/net/ethernet/davicom/dm9051.c new file +998 -0
998 lines not on this page · raw message
drivers/net/ethernet/davicom/dm9051.h new file +194 -0
194 lines not on this page · raw message
Signature · 1 line
2.20.1
Jakub Kicinski
maintainerRe: [PATCH v7, 2/2] net: Add dm9051 driver
Quoted 9 lines · On Mon, 20 Dec 2021 19:33:42 +0800 Joseph CHAMG wrote:
drivers/net/ethernet/davicom/dm9051.c new file +9
-0+/* set mac permanently +*/ +static void dm9051_write_mac_lock (struct board_info *db) +{ +struct net_device *ndev = db->ndev; +int i, oft; + +netdev_dbg (ndev,"set_mac_address %pM\n" , ndev->dev_addr); +
drivers/net/ethernet/davicom/dm9051.c line 359
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+/* write to net device and chip */ +mutex_lock (&db->addr_lock); +for (i =0 , oft =DM9051_PAR ; i <ETH_ALEN ; i++, oft++) +dm9051_iow (db, oft, ndev->dev_addr[i]); +mutex_unlock (&db->addr_lock); + +/* write to EEPROM */ +for (i =0 ; i <ETH_ALEN ; i +=2 ) +dm9051_write_eeprom (db, i /2 , (u8 *)&ndev->dev_addr[i]); +}
Why set the MAC permanently? I don't thin users will expect that.
Quoted 31 lines
drivers/net/ethernet/davicom/dm9051.c new file +31
-0+/* reset while rx error found +*/ +static void dm9051_restart_dm9051 (struct board_info *db) +{ +struct net_device *ndev = db->ndev; +char *sbuff = (char *)db->prxhdr; +int rxlen =le16_to_cpu (db->prxhdr->rxlen); + +netdev_dbg (ndev,"dm9-rxhdr, Large-eror (rxhdr %02x %02x %02x %02x)\n" , + sbuff[0 ], sbuff[1 ], sbuff[2 ], sbuff[3 ]); +netdev_dbg (ndev,"dm9-pkt-Wrong, RxLen over-range (%x= %d > %x= %d)\n" , + rxlen, rxlen,DM9051_PKT_MAX ,DM9051_PKT_MAX ); + +dm9051_reset (db); +dm9051_restart_fifo_rst (db); + +/* phy mdiobus phy read/write is already enclose with mutex_lock/mutex_unlock */ +mutex_unlock (&db->addr_lock); +dm9051_restart_phy (db); +mutex_lock (&db->addr_lock); + +netdev_dbg (ndev," RxLenErr&MacOvrSft_Er %d, RST_c %d\n" , + db->bc.large_err_counter + db->bc.mac_ovrsft_counter, + db->bc.DO_FIFO_RST_counter); +} + +static int dm9051_loop_rx (struct board_info *db) +{ +struct net_device *ndev = db->ndev; +u8 rxbyte; +int ret, rxlen;
drivers/net/ethernet/davicom/dm9051.c line 526
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+char sbuff[DM_RXHDR_SIZE ]; +struct sk_buff *skb; +u8 *rdptr; +int scanrr =0 ; + +while (1 ) { + rxbyte =dm9051_ior (db,DM_SPI_MRCMDX );/* Dummy read */ + rxbyte =dm9051_ior (db,DM_SPI_MRCMDX );/* Dummy read */ +if (rxbyte !=DM9051_PKT_RDY ) { +dm9051_iow (db,DM9051_ISR ,0xff );/* Clear ISR, clear to stop mrcmd */
Should you not clear the ISR _before_ checking if there is a packet pending? What if a packet arrives between the two, does the HW re-raise the IRQ if the fifo is not empty?
drivers/net/ethernet/davicom/dm9051.c line 529
Quoted 3 lines
drivers/net/ethernet/davicom/dm9051.c new file +3
-0+break ;/* exhaust-empty */ + } + ret =dm9051_inblk (db, sbuff,DM_RXHDR_SIZE );
This is not a DMA, right? You can't DMA to a stack buffer, stack may be vmapped.
Quoted 13 lines
drivers/net/ethernet/davicom/dm9051.c new file +13
-0+if (ret <0 ) +break ; +dm9051_iow (db,DM9051_ISR ,0xff );/* Clear ISR, clear to stop mrcmd */ + + db->prxhdr = (struct dm9051_rxhdr *)sbuff; + rxlen =le16_to_cpu (db->prxhdr->rxlen); + +if (db->prxhdr->rxstatus &0xbf ) { +netdev_dbg (ndev,"warn : rxhdr.status 0x%02x\n" , + db->prxhdr->rxstatus); + } +if (rxlen >DM9051_PKT_MAX ) { + db->bc.large_err_counter++;
drivers/net/ethernet/davicom/dm9051.c line 552
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+dm9051_restart_dm9051 (db); +return scanrr; + } + + skb =dev_alloc_skb (rxlen +4 ); +if (!skb) { +dm9051_dumpblk (db, rxlen); +return scanrr; + } +skb_reserve (skb,2 );
is the two NET_IP_ALIGN? If so please use __netdev_alloc_skb_ip_align().
drivers/net/ethernet/davicom/dm9051.c line 564
Quoted 12 lines
drivers/net/ethernet/davicom/dm9051.c new file +12
-0+ rdptr = (u8 *)skb_put (skb, rxlen -4 ); + + ret =dm9051_inblk (db, rdptr, rxlen); +if (ret <0 ) +break ; + +dm9051_iow (db,DM9051_ISR ,0xff );/* Clear ISR, clear to stop mrcmd */ + + skb->protocol =eth_type_trans (skb, db->ndev); +if (db->ndev->features &NETIF_F_RXCSUM ) +skb_checksum_none_assert (skb); +if (in_interrupt ())
I don't think it can ever be in_interrupt(), just call netif_rx_ni()
Quoted 22 lines
drivers/net/ethernet/davicom/dm9051.c new file +21
-0+netif_rx (skb); + else +netif_rx_ni (skb ); + db->ndev->stats.rx_bytes += rxlen; + db->ndev->stats.rx_packets++; + scanrr++; + } +return scanrr; +} +static void dm9051_stopcode_lock (struct board_info *db) +{ +mutex_lock (&db->addr_lock); + +dm9051_iow (db,DM9051_GPR ,0x01 );/* Power-Down PHY */ +dm9051_iow (db,DM9051_RCR ,RCR_RX_DISABLE );/* Disable RX */ + +mutex_unlock (&db->addr_lock); +} + +/* Open network device +* Called when the network device is marked active, such as a user executing
drivers/net/ethernet/davicom/dm9051.c line 764
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+* 'ifconfig up' on the device +*/ +static int dm9051_open (struct net_device *ndev) +{ +struct board_info *db =to_dm9051_board (ndev); +int ret; + +skb_queue_head_init (&db->txq); +netif_start_queue (ndev); +netif_wake_queue (ndev);
Shouldn't you wake the queues _after_ all the init?
Also you don't need both of those, wake_queue() is enough.
drivers/net/ethernet/davicom/dm9051.c line 776
Quoted 11 lines
drivers/net/ethernet/davicom/dm9051.c new file +11
-0+ ret =dm9051_opencode_receiving (ndev, db); +if (ret <0 ) +return ret; + +dm9051_initcode_lock (ndev, db); +dm9051_imr_enable_lock_essential (db); +return 0 ; +} + +/* Close network device +* Called to close down a network device which has been active. Cancell any
s/cancell/cancel/
Quoted 11 lines
+ * work, shutdown the RX and TX process and then place the chip into a low
+ * power state while it is not being used
+ */drivers/net/ethernet/davicom/dm9051.c new file +8
-0+static int dm9051_stop (struct net_device *ndev) +{ +struct board_info *db =to_dm9051_board (ndev); + +phy_stop (db->phydev); +dm9051_stopcode_release (db); +netif_stop_queue (ndev); +dm9051_stopcode_lock (db);
drivers/net/ethernet/davicom/dm9051.c line 797
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+return 0 ; +} + +/* event: play a schedule starter in condition +*/ +static netdev_tx_t dm9051_start_xmit (struct sk_buff *skb,struct net_device *ndev) +{ +struct board_info *db =to_dm9051_board (ndev); + +skb_queue_tail (&db->txq, skb);/* add to skb */
You should probably enforce a limit on this queue size, and pause the qdisc once it fills up. This will prevent obvious buffer bloat problems.
drivers/net/ethernet/davicom/dm9051.c line 798
Quoted 1 line
drivers/net/ethernet/davicom/dm9051.c new file +1
-0+schedule_delayed_work (&db->tx_work,0 );
Same as below, why is it delayed if you always pass 0?
Quoted 26 lines
drivers/net/ethernet/davicom/dm9051.c new file +26
-0+return NETDEV_TX_OK ; +} + +/* event: play with a schedule starter +*/ +static void dm9051_set_multicast_list_schedule (struct net_device *ndev) +{ +struct board_info *db =to_dm9051_board (ndev); +u8 rcr =RCR_DIS_LONG |RCR_DIS_CRC |RCR_RXEN ; +struct netdev_hw_addr *ha; +u32 hash_val; + +/* rx control */ +if (ndev->flags &IFF_PROMISC ) { + rcr |=RCR_PRMSC ; +netdev_dbg (ndev,"set_multicast rcr |= RCR_PRMSC, rcr= %02x\n" , rcr); + } + +if (ndev->flags &IFF_ALLMULTI ) { + rcr |=RCR_ALL ; +netdev_dbg (ndev,"set_multicast rcr |= RCR_ALLMULTI, rcr= %02x\n" , rcr); + } + + db->rcr_all = rcr; + +/* broadcast address */
drivers/net/ethernet/davicom/dm9051.c line 834
Quoted 10 lines
drivers/net/ethernet/davicom/dm9051.c new file +10
-0+ db->hash_table[0 ] =0 ; + db->hash_table[1 ] =0 ; + db->hash_table[2 ] =0 ; + db->hash_table[3 ] =0x8000 ; + +/* the multicast address in Hash Table : 64 bits */ +netdev_for_each_mc_addr (ha, ndev) { + hash_val =ether_crc_le (6 , ha->addr) &0x3f ; + db->hash_table[hash_val /16 ] |= (u16 )1 << (hash_val %16 ); + }
This can theoretically race with the work reading these values, but in practice that's probably fine..
drivers/net/ethernet/davicom/dm9051.c line 836
Quoted 1 line
drivers/net/ethernet/davicom/dm9051.c new file +1
-0+schedule_delayed_work (&db->rxctrl_work,0 );
Do you ever schedule this work with a delay? Use a normal work if it doesn't need a delay.
drivers/net/ethernet/davicom/dm9051.h line 175
Quoted 6 lines
drivers/net/ethernet/davicom/dm9051.h new file +6
-0+u8 cmd[2 ]____cacheline_aligned ; +struct spi_transfer spi_xfer2[2 ]____cacheline_aligned ; +struct spi_message spi_msg2____cacheline_aligned ; +struct rx_ctl_mach bc____cacheline_aligned ; +struct flow_ctl_tag fl____cacheline_aligned ; +struct dm9051_rxhdr *prxhdr____cacheline_aligned ;
Do you really need all this cache line alignment? Isn't it better to group the structures into rx side and tx side instead of having each member be its own cache line?