Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding support for KSZ9477 managed ethernet switch for saluki-v3 #153

Merged
merged 2 commits into from
Sep 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions arch/risc-v/src/mpfs/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -747,6 +747,8 @@ config MPFS_MAC_NO_BROADCAST

config MPFS_MAC_AUTONEG
bool "Use autonegotiation"
depends on !MPFS_ETH0_PHY_KSZ9477
depends on !MPFS_ETH1_PHY_KSZ9477
default y
---help---
Use PHY autonegotiation to determine speed and mode
Expand All @@ -765,6 +767,34 @@ config MPFS_PHYINIT
---help---
call mpfs_phy_boardinitialize() on init

config MPFS_ETH0_PHY_KSZ9477
bool "Use ksz9477 switch as an SGMII PHY for ETH0"
default n
select NET_KSZ9477
---help---
Select to use ksz9477 connected to SGMII

config MPFS_ETH1_PHY_KSZ9477
bool "Use ksz9477 switch as an SGMII PHY for ETH1"
default n
select NET_KSZ9477
---help---
Select to use ksz9477 connected to SGMII

config MPFS_ETH0_PHY_KSZ9477_I2C_BUS
int "Management I2C bus number for ksz9477 switch"
depends on MPFS_ETH0_PHY_KSZ9477
default 5
---help---
The i2c port number for switch management

config MPFS_ETH1_PHY_KSZ9477_I2C_BUS
int "Management I2C bus number for ksz9477 switch"
depends on MPFS_ETH1_PHY_KSZ9477
default 5
---help---
The i2c port number for switch management

if !MPFS_MAC_AUTONEG

config MPFS_MAC_ETHFD
Expand All @@ -776,6 +806,7 @@ config MPFS_MAC_ETHFD

choice
prompt "MAC Speed"
default MPFS_MAC_ETH1000MBPS if MPFS_ETH0_PHY_KSZ9477 || MPFS_ETH1_PHY_KSZ9477
default MPFS_MAC_ETH100MBPS
---help---
If autonegotiation is not used, then you must select the fixed speed
Expand Down
78 changes: 69 additions & 9 deletions arch/risc-v/src/mpfs/mpfs_ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,31 @@
# include <nuttx/net/pkt.h>
#endif

#include <nuttx/net/ksz9477.h>

#include "riscv_internal.h"
#include "mpfs_memorymap.h"
#include "mpfs_ethernet.h"
#include "mpfs_dsn.h"
#include "mpfs_i2c.h"

#if defined(CONFIG_MPFS_ETH0_PHY_KSZ9477) ||\
defined(CONFIG_MPFS_ETH1_PHY_KSZ9477)
# if !defined(CONFIG_MPFS_MAC_SGMII)
# error Using KSZ9477 as a PHY requires CONFIG_MPFS_MAC_SGMII to be set
# endif

# define ETH_HAS_KSZ_SWITCH

# ifdef CONFIG_MPFS_ETH0_PHY_KSZ9477
# define ETH_PHY_KSZ9477_I2C_BUS CONFIG_MPFS_ETH0_PHY_KSZ9477_I2C_BUS
# else
# define ETH_PHY_KSZ9477_I2C_BUS CONFIG_MPFS_ETH1_PHY_KSZ9477_I2C_BUS
# endif

#else
# define ETH_HAS_MDIO_PHY
#endif

#if defined(CONFIG_NET) && defined(CONFIG_MPFS_ETHMAC)

Expand Down Expand Up @@ -90,7 +111,7 @@
# endif
#endif

#ifndef CONFIG_MPFS_PHYADDR
#if defined(ETH_HAS_MDIO_PHY) && !defined(CONFIG_MPFS_PHYADDR)
# error "CONFIG_MPFS_PHYADDR must be defined in the NuttX configuration"
#endif

Expand Down Expand Up @@ -253,7 +274,9 @@ struct mpfs_ethmac_s
irq_t mac_q_int[MPFS_MAC_QUEUE_COUNT]; /* irq numbers */
uint8_t ifup : 1; /* true:ifup false:ifdown */
uint8_t intf; /* Ethernet interface number */
#ifdef ETH_HAS_MDIO_PHY
uint8_t phyaddr; /* PHY address */
#endif
struct wdog_s txtimeout; /* TX timeout timer */
struct wdog_s rxtimeout; /* RX timeout timer */
struct work_s irqwork; /* For deferring interrupt work to the work queue */
Expand Down Expand Up @@ -338,10 +361,13 @@ static int mpfs_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg);

/* PHY Initialization */

static int mpfs_phyinit(struct mpfs_ethmac_s *priv);

#ifdef ETH_HAS_MDIO_PHY

static void mpfs_enablemdio(struct mpfs_ethmac_s *priv);
static void mpfs_disablemdio(struct mpfs_ethmac_s *priv);
static int mpfs_phyreset(struct mpfs_ethmac_s *priv);
static int mpfs_phyinit(struct mpfs_ethmac_s *priv);
static int mpfs_phyread(struct mpfs_ethmac_s *priv, uint8_t phyaddr,
uint8_t regaddr, uint16_t *phyval);
static int mpfs_phywrite(struct mpfs_ethmac_s *priv, uint8_t phyaddr,
Expand All @@ -350,16 +376,21 @@ static int mpfs_phywait(struct mpfs_ethmac_s *priv);
static int mpfs_phyfind(struct mpfs_ethmac_s *priv, uint8_t *phyaddr);
#ifdef CONFIG_MPFS_MAC_AUTONEG
static int mpfs_autonegotiate(struct mpfs_ethmac_s *priv);
#else
static void mpfs_linkspeed(struct mpfs_ethmac_s *priv);
#endif

#if defined(CONFIG_DEBUG_NET) && defined(CONFIG_DEBUG_INFO)
#endif /* ETH_HAS_MDIO_PHY */

#if defined(CONFIG_DEBUG_NET) && defined(CONFIG_DEBUG_INFO) && \
defined(ETH_HAS_MDIO_PHY)
static void mpfs_phydump(struct mpfs_ethmac_s *priv);
#else
# define mpfs_phydump(priv)
#endif

#ifndef CONFIG_MPFS_MAC_AUTONEG
static void mpfs_linkspeed(struct mpfs_ethmac_s *priv);
#endif

/* MAC/DMA Initialization */

static void mpfs_txreset(struct mpfs_ethmac_s *priv);
Expand Down Expand Up @@ -1873,6 +1904,8 @@ static int mpfs_rmmac(struct net_driver_s *dev, const uint8_t *mac)
}
#endif

#ifdef ETH_HAS_MDIO_PHY

/****************************************************************************
* Function: mpfs_enablemdio
*
Expand Down Expand Up @@ -2503,6 +2536,8 @@ static int mpfs_autonegotiate(struct mpfs_ethmac_s *priv)
}
#endif

#endif /* ETH_HAS_MDIO_PHY */

/****************************************************************************
* Function: mpfs_linkspeed
*
Expand All @@ -2518,7 +2553,7 @@ static int mpfs_autonegotiate(struct mpfs_ethmac_s *priv)
*
****************************************************************************/

#ifndef CONFIG_MPFS_MAC_AUTONEG
#if !defined(CONFIG_MPFS_MAC_AUTONEG) || !defined(ETH_HAS_MDIO_PHY)
static void mpfs_linkspeed(struct mpfs_ethmac_s *priv)
{
uint32_t regval;
Expand Down Expand Up @@ -2594,14 +2629,15 @@ static void mpfs_linkspeed(struct mpfs_ethmac_s *priv)
#ifdef CONFIG_NETDEV_IOCTL
static int mpfs_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg)
{
#ifdef CONFIG_NETDEV_PHY_IOCTL
#if defined(CONFIG_NETDEV_PHY_IOCTL) && defined(ETH_HAS_MDIO_PHY)
struct mpfs_ethmac_s *priv = (struct mpfs_ethmac_s *)dev->d_private;
#endif
int ret;

switch (cmd)
{
#ifdef CONFIG_NETDEV_PHY_IOCTL
#ifdef ETH_HAS_MDIO_PHY
#ifdef CONFIG_ARCH_PHY_INTERRUPT
case SIOCMIINOTIFY: /* Set up for PHY event notifications */
{
Expand Down Expand Up @@ -2665,6 +2701,7 @@ static int mpfs_ioctl(struct net_driver_s *dev, int cmd, unsigned long arg)
mpfs_disablemdio(priv);
}
break;
#endif /* ETH_HAS_MDIO_PHY */
#endif /* CONFIG_NETDEV_PHY_IOCTL */

default:
Expand Down Expand Up @@ -3239,6 +3276,8 @@ static int mpfs_macenable(struct mpfs_ethmac_s *priv)
*
****************************************************************************/

#ifdef ETH_HAS_MDIO_PHY

static void mpfs_mdcclock(struct mpfs_ethmac_s *priv)
{
uint32_t ncfgr;
Expand Down Expand Up @@ -3293,6 +3332,8 @@ static void mpfs_mdcclock(struct mpfs_ethmac_s *priv)
mac_putreg(priv, NETWORK_CONTROL, ncr);
}

#endif

/****************************************************************************
* Function: mpfs_phyinit
*
Expand All @@ -3309,7 +3350,9 @@ static void mpfs_mdcclock(struct mpfs_ethmac_s *priv)

static int mpfs_phyinit(struct mpfs_ethmac_s *priv)
{
int ret;
int ret = -EINVAL;

#ifdef ETH_HAS_MDIO_PHY

/* Configure PHY clocking */

Expand All @@ -3328,7 +3371,20 @@ static int mpfs_phyinit(struct mpfs_ethmac_s *priv)
/* We have a PHY address. Reset the PHY */

mpfs_phyreset(priv);
return OK;

#elif defined(ETH_HAS_KSZ_SWITCH)
struct i2c_master_s *bus;

bus = mpfs_i2cbus_initialize(ETH_PHY_KSZ9477_I2C_BUS);

if (bus)
{
ret = ksz9477_i2c_init(bus, KSZ9477_PORT_SGMII);
}

#endif

return ret;
}

/****************************************************************************
Expand All @@ -3347,6 +3403,8 @@ static int mpfs_phyinit(struct mpfs_ethmac_s *priv)
*
****************************************************************************/

#ifdef ETH_HAS_MDIO_PHY

static int mpfs_phyreset(struct mpfs_ethmac_s *priv)
{
uint16_t mcr;
Expand Down Expand Up @@ -3416,6 +3474,8 @@ static int mpfs_phyreset(struct mpfs_ethmac_s *priv)
return ret;
}

#endif

/****************************************************************************
* Function: mpfs_ethconfig
*
Expand Down
24 changes: 24 additions & 0 deletions drivers/net/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,30 @@ menuconfig NET_W5500
References:
W5500 Datasheet, Version 1.0.9, 2013 WIZnet Co., Ltd.

config NET_KSZ9477
bool "Management interface for ksz9477 ethernet switch"
default n
---help---
Support for Microchip/Micrel KSZ9477 managed switch. To use this,
one must also select the management interface (I2C / SPI) and
call the driver's init from board initialization code or from the
ethernet driver.

choice
prompt "Management bus for the kzs9477 switch"
default NET_KSZ9477_I2C
depends on NET_KSZ9477
---help---
Select the used management interface

config NET_KSZ9477_I2C
bool "Use I2C management interface"

config NET_KSZ9477_SPI
bool "Use SPI management interface"

endchoice

if NET_W5500

config NET_W5500_NINTERFACES
Expand Down
9 changes: 9 additions & 0 deletions drivers/net/Make.defs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,15 @@ ifeq ($(CONFIG_ARCH_PHY_INTERRUPT),y)
CSRCS += phy_notify.c
endif

ifeq ($(CONFIG_NET_KSZ9477),y)
CSRCS += ksz9477.c

ifeq ($(CONFIG_NET_KSZ9477_I2C),y)
CSRCS += ksz9477_i2c.c
endif

endif

# Include network build support

DEPPATH += --dep-path net
Expand Down
Loading