2020-08-30 11:34:01 +03:00
// SPDX-License-Identifier: (GPL-2.0+ OR BSD-3-Clause)
/* Copyright 2020 NXP
* Lynx PCS MDIO helpers
*/
# include <linux/mdio.h>
# include <linux/phylink.h>
# include <linux/pcs-lynx.h>
2023-06-07 12:58:54 +01:00
# include <linux/property.h>
2020-08-30 11:34:01 +03:00
# define SGMII_CLOCK_PERIOD_NS 8 /* PCS is clocked at 125 MHz */
# define LINK_TIMER_VAL(ns) ((u32)((ns) / SGMII_CLOCK_PERIOD_NS))
# define LINK_TIMER_LO 0x12
# define LINK_TIMER_HI 0x13
# define IF_MODE 0x14
# define IF_MODE_SGMII_EN BIT(0)
# define IF_MODE_USE_SGMII_AN BIT(1)
# define IF_MODE_SPEED(x) (((x) << 2) & GENMASK(3, 2))
# define IF_MODE_SPEED_MSK GENMASK(3, 2)
# define IF_MODE_HALF_DUPLEX BIT(4)
2021-12-28 21:03:06 -08:00
struct lynx_pcs {
struct phylink_pcs pcs ;
struct mdio_device * mdio ;
} ;
2020-08-30 11:34:01 +03:00
enum sgmii_speed {
SGMII_SPEED_10 = 0 ,
SGMII_SPEED_100 = 1 ,
SGMII_SPEED_1000 = 2 ,
SGMII_SPEED_2500 = 2 ,
} ;
# define phylink_pcs_to_lynx(pl_pcs) container_of((pl_pcs), struct lynx_pcs, pcs)
2021-12-28 21:03:06 -08:00
# define lynx_to_phylink_pcs(lynx) (&(lynx)->pcs)
2020-08-30 11:34:01 +03:00
static void lynx_pcs_get_state_usxgmii ( struct mdio_device * pcs ,
struct phylink_link_state * state )
{
struct mii_bus * bus = pcs - > bus ;
int addr = pcs - > addr ;
int status , lpa ;
status = mdiobus_c45_read ( bus , addr , MDIO_MMD_VEND2 , MII_BMSR ) ;
if ( status < 0 )
return ;
state - > link = ! ! ( status & MDIO_STAT1_LSTATUS ) ;
state - > an_complete = ! ! ( status & MDIO_AN_STAT1_COMPLETE ) ;
if ( ! state - > link | | ! state - > an_complete )
return ;
lpa = mdiobus_c45_read ( bus , addr , MDIO_MMD_VEND2 , MII_LPA ) ;
if ( lpa < 0 )
return ;
phylink_decode_usxgmii_word ( state , lpa ) ;
}
static void lynx_pcs_get_state_2500basex ( struct mdio_device * pcs ,
struct phylink_link_state * state )
{
int bmsr , lpa ;
2022-06-21 09:53:02 +01:00
bmsr = mdiodev_read ( pcs , MII_BMSR ) ;
lpa = mdiodev_read ( pcs , MII_LPA ) ;
2020-08-30 11:34:01 +03:00
if ( bmsr < 0 | | lpa < 0 ) {
state - > link = false ;
return ;
}
state - > link = ! ! ( bmsr & BMSR_LSTATUS ) ;
state - > an_complete = ! ! ( bmsr & BMSR_ANEGCOMPLETE ) ;
if ( ! state - > link )
return ;
state - > speed = SPEED_2500 ;
state - > pause | = MLO_PAUSE_TX | MLO_PAUSE_RX ;
state - > duplex = DUPLEX_FULL ;
}
static void lynx_pcs_get_state ( struct phylink_pcs * pcs ,
struct phylink_link_state * state )
{
struct lynx_pcs * lynx = phylink_pcs_to_lynx ( pcs ) ;
switch ( state - > interface ) {
2021-02-05 10:39:59 +00:00
case PHY_INTERFACE_MODE_1000BASEX :
2020-08-30 11:34:01 +03:00
case PHY_INTERFACE_MODE_SGMII :
case PHY_INTERFACE_MODE_QSGMII :
phylink_mii_c22_pcs_get_state ( lynx - > mdio , state ) ;
break ;
case PHY_INTERFACE_MODE_2500BASEX :
lynx_pcs_get_state_2500basex ( lynx - > mdio , state ) ;
break ;
case PHY_INTERFACE_MODE_USXGMII :
lynx_pcs_get_state_usxgmii ( lynx - > mdio , state ) ;
break ;
2020-09-23 18:41:21 +03:00
case PHY_INTERFACE_MODE_10GBASER :
phylink_mii_c45_pcs_get_state ( lynx - > mdio , state ) ;
break ;
2020-08-30 11:34:01 +03:00
default :
break ;
}
dev_dbg ( & lynx - > mdio - > dev ,
2023-03-15 14:46:49 +00:00
" mode=%s/%s/%s link=%u an_complete=%u \n " ,
2020-08-30 11:34:01 +03:00
phy_modes ( state - > interface ) ,
phy_speed_to_str ( state - > speed ) ,
phy_duplex_to_str ( state - > duplex ) ,
2023-03-15 14:46:49 +00:00
state - > link , state - > an_complete ) ;
2020-08-30 11:34:01 +03:00
}
2023-06-16 13:06:32 +01:00
static int lynx_pcs_config_giga ( struct mdio_device * pcs ,
2022-06-23 13:25:30 +01:00
phy_interface_t interface ,
2023-06-16 13:06:32 +01:00
const unsigned long * advertising ,
unsigned int neg_mode )
2021-02-05 10:39:59 +00:00
{
2023-01-12 14:37:06 +00:00
int link_timer_ns ;
2021-02-05 10:39:59 +00:00
u32 link_timer ;
2020-08-30 11:34:01 +03:00
u16 if_mode ;
int err ;
2023-01-12 14:37:06 +00:00
link_timer_ns = phylink_get_link_timer_ns ( interface ) ;
if ( link_timer_ns > 0 ) {
link_timer = LINK_TIMER_VAL ( link_timer_ns ) ;
2022-06-21 09:53:02 +01:00
mdiodev_write ( pcs , LINK_TIMER_LO , link_timer & 0xffff ) ;
mdiodev_write ( pcs , LINK_TIMER_HI , link_timer > > 16 ) ;
2023-01-12 14:37:06 +00:00
}
2022-06-23 13:25:30 +01:00
2023-01-12 14:37:06 +00:00
if ( interface = = PHY_INTERFACE_MODE_1000BASEX ) {
2022-06-23 13:25:30 +01:00
if_mode = 0 ;
} else {
2023-06-16 13:06:32 +01:00
/* SGMII and QSGMII */
2022-06-23 13:25:30 +01:00
if_mode = IF_MODE_SGMII_EN ;
2023-06-16 13:06:32 +01:00
if ( neg_mode = = PHYLINK_PCS_NEG_INBAND_ENABLED )
2022-06-23 13:25:30 +01:00
if_mode | = IF_MODE_USE_SGMII_AN ;
2020-08-30 11:34:01 +03:00
}
2022-06-23 13:25:30 +01:00
2022-06-21 09:53:02 +01:00
err = mdiodev_modify ( pcs , IF_MODE ,
2020-08-30 11:34:01 +03:00
IF_MODE_SGMII_EN | IF_MODE_USE_SGMII_AN ,
if_mode ) ;
if ( err )
return err ;
2023-06-16 13:06:32 +01:00
return phylink_mii_c22_pcs_config ( pcs , interface , advertising ,
neg_mode ) ;
2020-08-30 11:34:01 +03:00
}
2023-06-16 13:06:48 +01:00
static int lynx_pcs_config_usxgmii ( struct mdio_device * pcs ,
const unsigned long * advertising ,
unsigned int neg_mode )
2020-08-30 11:34:01 +03:00
{
struct mii_bus * bus = pcs - > bus ;
int addr = pcs - > addr ;
2023-06-16 13:06:48 +01:00
if ( neg_mode ! = PHYLINK_PCS_NEG_INBAND_ENABLED ) {
2020-08-30 11:34:01 +03:00
dev_err ( & pcs - > dev , " USXGMII only supports in-band AN for now \n " ) ;
return - EOPNOTSUPP ;
}
/* Configure device ability for the USXGMII Replicator */
return mdiobus_c45_write ( bus , addr , MDIO_MMD_VEND2 , MII_ADVERTISE ,
MDIO_USXGMII_10G | MDIO_USXGMII_LINK |
MDIO_USXGMII_FULL_DUPLEX |
ADVERTISE_SGMII | ADVERTISE_LPACK ) ;
}
2023-06-16 13:06:48 +01:00
static int lynx_pcs_config ( struct phylink_pcs * pcs , unsigned int neg_mode ,
2020-08-30 11:34:01 +03:00
phy_interface_t ifmode ,
2023-06-16 13:06:48 +01:00
const unsigned long * advertising , bool permit )
2020-08-30 11:34:01 +03:00
{
struct lynx_pcs * lynx = phylink_pcs_to_lynx ( pcs ) ;
switch ( ifmode ) {
2021-02-05 10:39:59 +00:00
case PHY_INTERFACE_MODE_1000BASEX :
2020-08-30 11:34:01 +03:00
case PHY_INTERFACE_MODE_SGMII :
case PHY_INTERFACE_MODE_QSGMII :
2023-06-16 13:06:32 +01:00
return lynx_pcs_config_giga ( lynx - > mdio , ifmode , advertising ,
neg_mode ) ;
2020-08-30 11:34:01 +03:00
case PHY_INTERFACE_MODE_2500BASEX :
2023-06-16 13:06:48 +01:00
if ( neg_mode = = PHYLINK_PCS_NEG_INBAND_ENABLED ) {
2020-08-30 11:34:01 +03:00
dev_err ( & lynx - > mdio - > dev ,
" AN not supported on 3.125GHz SerDes lane \n " ) ;
return - EOPNOTSUPP ;
}
break ;
case PHY_INTERFACE_MODE_USXGMII :
2023-06-16 13:06:48 +01:00
return lynx_pcs_config_usxgmii ( lynx - > mdio , advertising ,
neg_mode ) ;
2020-09-23 18:41:21 +03:00
case PHY_INTERFACE_MODE_10GBASER :
/* Nothing to do here for 10GBASER */
break ;
2020-08-30 11:34:01 +03:00
default :
return - EOPNOTSUPP ;
}
return 0 ;
}
2021-02-05 10:39:59 +00:00
static void lynx_pcs_an_restart ( struct phylink_pcs * pcs )
{
struct lynx_pcs * lynx = phylink_pcs_to_lynx ( pcs ) ;
phylink_mii_c22_pcs_an_restart ( lynx - > mdio ) ;
}
2023-06-16 13:06:48 +01:00
static void lynx_pcs_link_up_sgmii ( struct mdio_device * pcs ,
unsigned int neg_mode ,
2020-08-30 11:34:01 +03:00
int speed , int duplex )
{
u16 if_mode = 0 , sgmii_speed ;
/* The PCS needs to be configured manually only
* when not operating on in - band mode
*/
2023-08-11 14:53:52 +03:00
if ( neg_mode = = PHYLINK_PCS_NEG_INBAND_ENABLED )
2020-08-30 11:34:01 +03:00
return ;
if ( duplex = = DUPLEX_HALF )
if_mode | = IF_MODE_HALF_DUPLEX ;
switch ( speed ) {
case SPEED_1000 :
sgmii_speed = SGMII_SPEED_1000 ;
break ;
case SPEED_100 :
sgmii_speed = SGMII_SPEED_100 ;
break ;
case SPEED_10 :
sgmii_speed = SGMII_SPEED_10 ;
break ;
case SPEED_UNKNOWN :
/* Silently don't do anything */
return ;
default :
dev_err ( & pcs - > dev , " Invalid PCS speed %d \n " , speed ) ;
return ;
}
if_mode | = IF_MODE_SPEED ( sgmii_speed ) ;
2022-06-21 09:53:02 +01:00
mdiodev_modify ( pcs , IF_MODE ,
2020-08-30 11:34:01 +03:00
IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK ,
if_mode ) ;
}
/* 2500Base-X is SerDes protocol 7 on Felix and 6 on ENETC. It is a SerDes lane
* clocked at 3.125 GHz which encodes symbols with 8 b / 10 b and does not have
* auto - negotiation of any link parameters . Electrically it is compatible with
* a single lane of XAUI .
* The hardware reference manual wants to call this mode SGMII , but it isn ' t
* really , since the fundamental features of SGMII :
* - Downgrading the link speed by duplicating symbols
* - Auto - negotiation
* are not there .
* The speed is configured at 1000 in the IF_MODE because the clock frequency
* is actually given by a PLL configured in the Reset Configuration Word ( RCW ) .
* Since there is no difference between fixed speed SGMII w / o AN and 802.3 z w / o
* AN , we call this PHY interface type 2500 Base - X . In case a PHY negotiates a
* lower link speed on line side , the system - side interface remains fixed at
* 2500 Mbps and we do rate adaptation through pause frames .
*/
static void lynx_pcs_link_up_2500basex ( struct mdio_device * pcs ,
2023-06-16 13:06:48 +01:00
unsigned int neg_mode ,
2020-08-30 11:34:01 +03:00
int speed , int duplex )
{
u16 if_mode = 0 ;
2023-06-16 13:06:48 +01:00
if ( neg_mode = = PHYLINK_PCS_NEG_INBAND_ENABLED ) {
2020-08-30 11:34:01 +03:00
dev_err ( & pcs - > dev , " AN not supported for 2500BaseX \n " ) ;
return ;
}
if ( duplex = = DUPLEX_HALF )
if_mode | = IF_MODE_HALF_DUPLEX ;
if_mode | = IF_MODE_SPEED ( SGMII_SPEED_2500 ) ;
2022-06-21 09:53:02 +01:00
mdiodev_modify ( pcs , IF_MODE ,
2020-08-30 11:34:01 +03:00
IF_MODE_HALF_DUPLEX | IF_MODE_SPEED_MSK ,
if_mode ) ;
}
2023-06-16 13:06:48 +01:00
static void lynx_pcs_link_up ( struct phylink_pcs * pcs , unsigned int neg_mode ,
2020-08-30 11:34:01 +03:00
phy_interface_t interface ,
int speed , int duplex )
{
struct lynx_pcs * lynx = phylink_pcs_to_lynx ( pcs ) ;
switch ( interface ) {
case PHY_INTERFACE_MODE_SGMII :
case PHY_INTERFACE_MODE_QSGMII :
2023-06-16 13:06:48 +01:00
lynx_pcs_link_up_sgmii ( lynx - > mdio , neg_mode , speed , duplex ) ;
2020-08-30 11:34:01 +03:00
break ;
case PHY_INTERFACE_MODE_2500BASEX :
2023-06-16 13:06:48 +01:00
lynx_pcs_link_up_2500basex ( lynx - > mdio , neg_mode , speed , duplex ) ;
2020-08-30 11:34:01 +03:00
break ;
case PHY_INTERFACE_MODE_USXGMII :
/* At the moment, only in-band AN is supported for USXGMII
* so nothing to do in link_up
*/
break ;
default :
break ;
}
}
static const struct phylink_pcs_ops lynx_pcs_phylink_ops = {
. pcs_get_state = lynx_pcs_get_state ,
. pcs_config = lynx_pcs_config ,
2021-02-05 10:39:59 +00:00
. pcs_an_restart = lynx_pcs_an_restart ,
2020-08-30 11:34:01 +03:00
. pcs_link_up = lynx_pcs_link_up ,
} ;
2023-06-07 12:58:44 +01:00
static struct phylink_pcs * lynx_pcs_create ( struct mdio_device * mdio )
2020-08-30 11:34:01 +03:00
{
2021-12-28 21:03:10 -08:00
struct lynx_pcs * lynx ;
2020-08-30 11:34:01 +03:00
2021-12-28 21:03:10 -08:00
lynx = kzalloc ( sizeof ( * lynx ) , GFP_KERNEL ) ;
if ( ! lynx )
2023-06-07 12:58:49 +01:00
return ERR_PTR ( - ENOMEM ) ;
2020-08-30 11:34:01 +03:00
2023-05-26 11:14:39 +01:00
mdio_device_get ( mdio ) ;
2021-12-28 21:03:10 -08:00
lynx - > mdio = mdio ;
lynx - > pcs . ops = & lynx_pcs_phylink_ops ;
2023-06-16 13:06:48 +01:00
lynx - > pcs . neg_mode = true ;
2021-12-28 21:03:10 -08:00
lynx - > pcs . poll = true ;
2020-08-30 11:34:01 +03:00
2021-12-28 21:03:10 -08:00
return lynx_to_phylink_pcs ( lynx ) ;
2020-08-30 11:34:01 +03:00
}
2023-05-26 11:14:39 +01:00
struct phylink_pcs * lynx_pcs_create_mdiodev ( struct mii_bus * bus , int addr )
{
struct mdio_device * mdio ;
struct phylink_pcs * pcs ;
mdio = mdio_device_create ( bus , addr ) ;
if ( IS_ERR ( mdio ) )
return ERR_CAST ( mdio ) ;
pcs = lynx_pcs_create ( mdio ) ;
/* lynx_create() has taken a refcount on the mdiodev if it was
* successful . If lynx_create ( ) fails , this will free the mdio
* device here . In any case , we don ' t need to hold our reference
* anymore , and putting it here will allow mdio_device_put ( ) in
* lynx_destroy ( ) to automatically free the mdio device .
*/
mdio_device_put ( mdio ) ;
return pcs ;
}
EXPORT_SYMBOL ( lynx_pcs_create_mdiodev ) ;
2023-06-07 12:58:54 +01:00
/*
* lynx_pcs_create_fwnode ( ) creates a lynx PCS instance from the fwnode
* device indicated by node .
*
* Returns :
* - ENODEV if the fwnode is marked unavailable
* - EPROBE_DEFER if we fail to find the device
* - ENOMEM if we fail to allocate memory
* pointer to a phylink_pcs on success
*/
2023-06-07 12:58:29 +01:00
struct phylink_pcs * lynx_pcs_create_fwnode ( struct fwnode_handle * node )
{
struct mdio_device * mdio ;
struct phylink_pcs * pcs ;
2023-06-07 12:58:54 +01:00
if ( ! fwnode_device_is_available ( node ) )
return ERR_PTR ( - ENODEV ) ;
2023-06-07 12:58:29 +01:00
mdio = fwnode_mdio_find_device ( node ) ;
if ( ! mdio )
return ERR_PTR ( - EPROBE_DEFER ) ;
pcs = lynx_pcs_create ( mdio ) ;
/* lynx_create() has taken a refcount on the mdiodev if it was
* successful . If lynx_create ( ) fails , this will free the mdio
* device here . In any case , we don ' t need to hold our reference
* anymore , and putting it here will allow mdio_device_put ( ) in
* lynx_destroy ( ) to automatically free the mdio device .
*/
mdio_device_put ( mdio ) ;
return pcs ;
}
EXPORT_SYMBOL_GPL ( lynx_pcs_create_fwnode ) ;
2021-12-28 21:03:06 -08:00
void lynx_pcs_destroy ( struct phylink_pcs * pcs )
2020-08-30 11:34:01 +03:00
{
2021-12-28 21:03:06 -08:00
struct lynx_pcs * lynx = phylink_pcs_to_lynx ( pcs ) ;
2023-05-26 11:14:39 +01:00
mdio_device_put ( lynx - > mdio ) ;
2021-12-28 21:03:06 -08:00
kfree ( lynx ) ;
2020-08-30 11:34:01 +03:00
}
EXPORT_SYMBOL ( lynx_pcs_destroy ) ;
MODULE_LICENSE ( " Dual BSD/GPL " ) ;