Skip to content

Commit

Permalink
arch/risc-v/src/mpfs/mpfs_ethernet.c: Add ksz9477 initialization
Browse files Browse the repository at this point in the history
This adds initialization of the ksz9477 switch when used instead of
a PHY, directly connected to SGMII

Signed-off-by: Jukka Laitinen <[email protected]>
  • Loading branch information
jlaitine committed Sep 5, 2023
1 parent 0257728 commit 5629997
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
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_PORT
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
67 changes: 59 additions & 8 deletions arch/risc-v/src/mpfs/mpfs_ethernet.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,24 @@
# 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
#else
# define ETH_HAS_MDIO_PHY
#endif

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

Expand Down Expand Up @@ -90,7 +104,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 +267,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 +354,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 +369,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 +1897,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 +2529,8 @@ static int mpfs_autonegotiate(struct mpfs_ethmac_s *priv)
}
#endif

#endif /* ETH_HAS_MDIO_PHY */

/****************************************************************************
* Function: mpfs_linkspeed
*
Expand All @@ -2518,7 +2546,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 @@ -3239,6 +3267,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 +3323,8 @@ static void mpfs_mdcclock(struct mpfs_ethmac_s *priv)
mac_putreg(priv, NETWORK_CONTROL, ncr);
}

#endif

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

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

#ifdef ETH_HAS_MDIO_PHY

/* Configure PHY clocking */

Expand All @@ -3328,7 +3362,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(CONFIG_MPFS_ETH0_PHY_KSZ9477_I2C_BUS);

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

#endif

return ret;
}

/****************************************************************************
Expand All @@ -3347,6 +3394,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 +3465,8 @@ static int mpfs_phyreset(struct mpfs_ethmac_s *priv)
return ret;
}

#endif

/****************************************************************************
* Function: mpfs_ethconfig
*
Expand Down

0 comments on commit 5629997

Please sign in to comment.