Skip to content

Commit

Permalink
fec: Restart PPS after link state change
Browse files Browse the repository at this point in the history
On link state change, the controller gets reset,
causing PPS to drop out and the PHC to lose its
time and calibration. So we restart it if needed,
restoring calibration and time registers.

Changes since v2:
* Add `fec_ptp_save_state()`/`fec_ptp_restore_state()`
* Use `ktime_get_real_ns()`
* Use `BIT()` macro
Changes since v1:
* More ECR #define's
* Stop PPS in `fec_ptp_stop()`

Signed-off-by: Csókás Bence <[email protected]>
Signed-off-by: David S. Miller <[email protected]>
  • Loading branch information
Csókás Bence authored and davem330 committed Aug 24, 2022
1 parent d5485d9 commit f799592
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 4 deletions.
10 changes: 10 additions & 0 deletions drivers/net/ethernet/freescale/fec.h
Original file line number Diff line number Diff line change
Expand Up @@ -634,6 +634,13 @@ struct fec_enet_private {
int pps_enable;
unsigned int next_counter;

struct {
struct timespec64 ts_phc;
u64 ns_sys;
u32 at_corr;
u8 at_inc_corr;
} ptp_saved_state;

u64 ethtool_stats[];
};

Expand All @@ -644,5 +651,8 @@ void fec_ptp_disable_hwts(struct net_device *ndev);
int fec_ptp_set(struct net_device *ndev, struct ifreq *ifr);
int fec_ptp_get(struct net_device *ndev, struct ifreq *ifr);

void fec_ptp_save_state(struct fec_enet_private *fep);
int fec_ptp_restore_state(struct fec_enet_private *fep);

/****************************************************************************/
#endif /* FEC_H */
42 changes: 38 additions & 4 deletions drivers/net/ethernet/freescale/fec_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -285,8 +285,11 @@ MODULE_PARM_DESC(macaddr, "FEC Ethernet MAC address");
#define FEC_MMFR_TA (2 << 16)
#define FEC_MMFR_DATA(v) (v & 0xffff)
/* FEC ECR bits definition */
#define FEC_ECR_MAGICEN (1 << 2)
#define FEC_ECR_SLEEP (1 << 3)
#define FEC_ECR_RESET BIT(0)
#define FEC_ECR_ETHEREN BIT(1)
#define FEC_ECR_MAGICEN BIT(2)
#define FEC_ECR_SLEEP BIT(3)
#define FEC_ECR_EN1588 BIT(4)

#define FEC_MII_TIMEOUT 30000 /* us */

Expand Down Expand Up @@ -982,6 +985,9 @@ fec_restart(struct net_device *ndev)
u32 temp_mac[2];
u32 rcntl = OPT_FRAME_SIZE | 0x04;
u32 ecntl = 0x2; /* ETHEREN */
struct ptp_clock_request ptp_rq = { .type = PTP_CLK_REQ_PPS };

fec_ptp_save_state(fep);

/* Whack a reset. We should wait for this.
* For i.MX6SX SOC, enet use AXI bus, we use disable MAC
Expand Down Expand Up @@ -1135,7 +1141,7 @@ fec_restart(struct net_device *ndev)
}

if (fep->bufdesc_ex)
ecntl |= (1 << 4);
ecntl |= FEC_ECR_EN1588;

if (fep->quirks & FEC_QUIRK_DELAYED_CLKS_SUPPORT &&
fep->rgmii_txc_dly)
Expand All @@ -1156,6 +1162,14 @@ fec_restart(struct net_device *ndev)
if (fep->bufdesc_ex)
fec_ptp_start_cyclecounter(ndev);

/* Restart PPS if needed */
if (fep->pps_enable) {
/* Clear flag so fec_ptp_enable_pps() doesn't return immediately */
fep->pps_enable = 0;
fec_ptp_restore_state(fep);
fep->ptp_caps.enable(&fep->ptp_caps, &ptp_rq, 1);
}

/* Enable interrupts we wish to service */
if (fep->link)
writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);
Expand Down Expand Up @@ -1206,6 +1220,8 @@ fec_stop(struct net_device *ndev)
struct fec_enet_private *fep = netdev_priv(ndev);
u32 rmii_mode = readl(fep->hwp + FEC_R_CNTRL) & (1 << 8);
u32 val;
struct ptp_clock_request ptp_rq = { .type = PTP_CLK_REQ_PPS };
u32 ecntl = 0;

/* We cannot expect a graceful transmit stop without link !!! */
if (fep->link) {
Expand All @@ -1215,6 +1231,8 @@ fec_stop(struct net_device *ndev)
netdev_err(ndev, "Graceful transmit stop did not complete!\n");
}

fec_ptp_save_state(fep);

/* Whack a reset. We should wait for this.
* For i.MX6SX SOC, enet use AXI bus, we use disable MAC
* instead of reset MAC itself.
Expand All @@ -1234,12 +1252,28 @@ fec_stop(struct net_device *ndev)
writel(fep->phy_speed, fep->hwp + FEC_MII_SPEED);
writel(FEC_DEFAULT_IMASK, fep->hwp + FEC_IMASK);

if (fep->bufdesc_ex)
ecntl |= FEC_ECR_EN1588;

/* We have to keep ENET enabled to have MII interrupt stay working */
if (fep->quirks & FEC_QUIRK_ENET_MAC &&
!(fep->wol_flag & FEC_WOL_FLAG_SLEEP_ON)) {
writel(2, fep->hwp + FEC_ECNTRL);
ecntl |= FEC_ECR_ETHEREN;
writel(rmii_mode, fep->hwp + FEC_R_CNTRL);
}

writel(ecntl, fep->hwp + FEC_ECNTRL);

if (fep->bufdesc_ex)
fec_ptp_start_cyclecounter(ndev);

/* Restart PPS if needed */
if (fep->pps_enable) {
/* Clear flag so fec_ptp_enable_pps() doesn't return immediately */
fep->pps_enable = 0;
fec_ptp_restore_state(fep);
fep->ptp_caps.enable(&fep->ptp_caps, &ptp_rq, 1);
}
}


Expand Down
29 changes: 29 additions & 0 deletions drivers/net/ethernet/freescale/fec_ptp.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,36 @@ void fec_ptp_stop(struct platform_device *pdev)
struct net_device *ndev = platform_get_drvdata(pdev);
struct fec_enet_private *fep = netdev_priv(ndev);

if (fep->pps_enable)
fec_ptp_enable_pps(fep, 0);

cancel_delayed_work_sync(&fep->time_keep);
if (fep->ptp_clock)
ptp_clock_unregister(fep->ptp_clock);
}

void fec_ptp_save_state(struct fec_enet_private *fep)
{
u32 atime_inc_corr;

fec_ptp_gettime(&fep->ptp_caps, &fep->ptp_saved_state.ts_phc);
fep->ptp_saved_state.ns_sys = ktime_get_ns();

fep->ptp_saved_state.at_corr = readl(fep->hwp + FEC_ATIME_CORR);
atime_inc_corr = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_CORR_MASK;
fep->ptp_saved_state.at_inc_corr = (u8)(atime_inc_corr >> FEC_T_INC_CORR_OFFSET);
}

int fec_ptp_restore_state(struct fec_enet_private *fep)
{
u32 atime_inc = readl(fep->hwp + FEC_ATIME_INC) & FEC_T_INC_MASK;
u64 ns_sys;

writel(fep->ptp_saved_state.at_corr, fep->hwp + FEC_ATIME_CORR);
atime_inc |= ((u32)fep->ptp_saved_state.at_inc_corr) << FEC_T_INC_CORR_OFFSET;
writel(atime_inc, fep->hwp + FEC_ATIME_INC);

ns_sys = ktime_get_ns() - fep->ptp_saved_state.ns_sys;
timespec64_add_ns(&fep->ptp_saved_state.ts_phc, ns_sys);
return fec_ptp_settime(&fep->ptp_caps, &fep->ptp_saved_state.ts_phc);
}

0 comments on commit f799592

Please sign in to comment.