[PATCH v3 11/12] net: dsa: add KSZ9477 switch SPI driver

Sascha Hauer sha at pengutronix.de
Thu Apr 7 07:27:36 PDT 2022


On Thu, Apr 07, 2022 at 11:16:03AM +0200, Oleksij Rempel wrote:
> Add support for Microchip KSZ9477 switch.
> 
> With this driver we will be able to use KSZ9477 switch as port
> multiplexer.
> 
> Signed-off-by: Ahmad Fatoum <a.fatoum at pengutronix.de>
> ---
>  drivers/net/Kconfig                 |    8 +
>  drivers/net/Makefile                |    1 +
>  drivers/net/ksz9477.c               |  570 +++++++++
>  include/platform_data/ksz9477_reg.h | 1665 +++++++++++++++++++++++++++
>  4 files changed, 2244 insertions(+)
>  create mode 100644 drivers/net/ksz9477.c
>  create mode 100644 include/platform_data/ksz9477_reg.h
> 
> diff --git a/drivers/net/Kconfig b/drivers/net/Kconfig
> index 038eeb19a8..50c87c8ff8 100644
> --- a/drivers/net/Kconfig
> +++ b/drivers/net/Kconfig
> @@ -193,6 +193,14 @@ config DRIVER_NET_KS8851_MLL
>  	  This option enables support for the Micrel KS8851 MLL
>  	  ethernet chip.
>  
> +config DRIVER_NET_KSZ9477
> +	bool "KSZ9477 switch driver"
> +	depends on SPI
> +	select DSA
> +	help
> +	  This option enables support for the Microchip KSZ9477
> +	  switch chip.
> +
>  config DRIVER_NET_MACB
>  	bool "macb Ethernet driver"
>  	depends on HAS_MACB || COMPILE_TEST
> diff --git a/drivers/net/Makefile b/drivers/net/Makefile
> index 1c979047d9..fa3d4583a0 100644
> --- a/drivers/net/Makefile
> +++ b/drivers/net/Makefile
> @@ -26,6 +26,7 @@ obj-$(CONFIG_DRIVER_NET_FEC_IMX)	+= fec_imx.o
>  obj-$(CONFIG_DRIVER_NET_FSL_FMAN)	+= fsl-fman.o
>  obj-$(CONFIG_DRIVER_NET_GIANFAR)	+= gianfar.o
>  obj-$(CONFIG_DRIVER_NET_KS8851_MLL)	+= ks8851_mll.o
> +obj-$(CONFIG_DRIVER_NET_KSZ9477)	+= ksz9477.o
>  obj-$(CONFIG_DRIVER_NET_MACB)		+= macb.o
>  obj-$(CONFIG_DRIVER_NET_MICREL)		+= ksz8864rmn.o
>  obj-$(CONFIG_DRIVER_NET_MPC5200)	+= fec_mpc5200.o
> diff --git a/drivers/net/ksz9477.c b/drivers/net/ksz9477.c
> new file mode 100644
> index 0000000000..76eb672f21
> --- /dev/null
> +++ b/drivers/net/ksz9477.c
> @@ -0,0 +1,570 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +
> +#include <common.h>
> +#include <complete.h>
> +#include <dsa.h>
> +#include <gpiod.h>
> +#include <net.h>
> +#include <platform_data/ksz9477_reg.h>
> +#include <spi/spi.h>
> +
> +/* SPI frame opcodes */
> +#define KS_SPIOP_RD			3
> +#define KS_SPIOP_WR			2
> +
> +#define SPI_ADDR_SHIFT			24
> +#define SPI_ADDR_MASK			(BIT(SPI_ADDR_SHIFT) - 1)
> +#define SPI_TURNAROUND_SHIFT		5
> +
> +#define GBIT_SUPPORT			BIT(0)
> +#define NEW_XMII			BIT(1)
> +#define IS_9893				BIT(2)
> +#define KSZ9477_PHY_ERRATA		BIT(3)
> +
> +struct ksz_switch {
> +	struct spi_device *spi;
> +	struct dsa_switch ds;
> +	struct device_d *dev;
> +	int phy_port_cnt;
> +	u32 chip_id;
> +	u8 features;
> +};
> +
> +static int ksz9477_spi_read_reg(struct spi_device *spi, u32 reg, u8 *val,
> +				unsigned int len)
> +{
> +	u32 txbuf;
> +	u32 rxbuf;
> +	int ret;
> +
> +	txbuf = reg & SPI_ADDR_MASK;
> +	txbuf |= KS_SPIOP_RD << SPI_ADDR_SHIFT;
> +	txbuf <<= SPI_TURNAROUND_SHIFT;
> +	txbuf = cpu_to_be32(txbuf);
> +
> +	ret = spi_write_then_read(spi, &txbuf, 4, val, len);
> +	memcpy(&rxbuf, val, len);

rxbuf is set but unused.

> +
> +	return ret;
> +}
> +
> +static int ksz9477_spi_write_reg(struct spi_device *spi, u32 reg, u8 *val,
> +				 unsigned int len)
> +{
> +	u8 _txbuf[sizeof(u32) * 2] = {0};
> +	u32 *txbuf = (u32 *)&_txbuf[0];

Why an extra u32 pointer to an u8 buf? You could just use a

	u32 txbuf[2];

> +
> +	memcpy(&_txbuf[4], val, len);

For readability I would initialize txbuf in order, i.e. put the memcpy
after init of txbuf[0].


> +static int ksz_port_enable(struct dsa_port *dp, int port,
> +			   struct phy_device *phy)
> +{
> +	struct device_d *dev = dp->ds->dev;
> +	struct ksz_switch *priv = dev_get_priv(dev);
> +	u8 data8;
> +	int ret;
> +
> +	/* setup this port */
> +	ret = ksz_port_setup(priv, port, phy->interface);
> +	if (ret) {
> +		dev_err(dev, "port setup failed: %d\n", ret);
> +		return ret;
> +	}
> +
> +	/* enable port forwarding for this port */
> +	ksz_pread8(priv, port, REG_PORT_LUE_MSTP_STATE, &data8);
> +	data8 &= ~(PORT_TX_ENABLE | PORT_RX_ENABLE | PORT_LEARN_DISABLE);
> +	data8 |= (PORT_TX_ENABLE | PORT_RX_ENABLE);

No need to clear PORT_TX_ENABLE and PORT_RX_ENABLE before setting them
again.

Sascha

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |



More information about the barebox mailing list