mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sascha Hauer <sha@pengutronix.de>
To: Oleksij Rempel <o.rempel@pengutronix.de>
Cc: barebox@lists.infradead.org, Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: Re: [PATCH v3 11/12] net: dsa: add KSZ9477 switch SPI driver
Date: Thu, 7 Apr 2022 16:27:36 +0200	[thread overview]
Message-ID: <20220407142736.GL4012@pengutronix.de> (raw)
In-Reply-To: <20220407091604.956577-12-o.rempel@pengutronix.de>

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@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 |

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


  reply	other threads:[~2022-04-07 14:29 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-04-07  9:15 [PATCH v3 00/12] provide DSA support Oleksij Rempel
2022-04-07  9:15 ` [PATCH v3 01/12] net: add RX preprocessor support Oleksij Rempel
2022-04-07  9:15 ` [PATCH v3 02/12] net: add of_find_eth_device_by_node() function Oleksij Rempel
2022-04-07 13:36   ` Sascha Hauer
2022-04-07  9:15 ` [PATCH v3 03/12] net: add DSA framework to support basic switch functionality Oleksij Rempel
2022-04-07 13:56   ` Sascha Hauer
2022-04-12  8:14   ` Sascha Hauer
2022-04-07  9:15 ` [PATCH v3 04/12] driver: add dev_get_priv() helper Oleksij Rempel
2022-04-07  9:15 ` [PATCH v3 05/12] net: port part of if_vlan header from kernel v5.17 Oleksij Rempel
2022-04-07  9:15 ` [PATCH v3 06/12] spi: port spi_sync_transfer() function " Oleksij Rempel
2022-04-07  9:15 ` [PATCH v3 07/12] net: mdio: add MDIO_DEVAD_NONE define Oleksij Rempel
2022-04-07  9:16 ` [PATCH v3 08/12] net: phy: make sure MDIO bus is probed if we search for the PHY Oleksij Rempel
2022-04-07  9:16 ` [PATCH v3 09/12] of_net: add rev-rmii support Oleksij Rempel
2022-04-07  9:16 ` [PATCH v3 10/12] net: dsa: add support for SJA11xx switches Oleksij Rempel
2022-04-07 14:04   ` Sascha Hauer
2022-04-07  9:16 ` [PATCH v3 11/12] net: dsa: add KSZ9477 switch SPI driver Oleksij Rempel
2022-04-07 14:27   ` Sascha Hauer [this message]
2022-04-07  9:16 ` [PATCH v3 12/12] add ethlog Oleksij Rempel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220407142736.GL4012@pengutronix.de \
    --to=sha@pengutronix.de \
    --cc=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=o.rempel@pengutronix.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox