/* * Copyright (c) 2015, Freescale Semiconductor, Inc. * Copyright 2016-2018 NXP * All rights reserved. * * This file contains PHY functions which are common to all ethernet phys. * * SPDX-License-Identifier: BSD-3-Clause */ #include "fsl_phy.h" #include <stdio.h> // Timeout for waiting for the processor to execute an SMI read/write operation #define SMI_TIMEOUT_COUNT 100000 status_t PHY_Write(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t data) { uint32_t counter; /* Clear the SMI interrupt event. */ ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK); /* Starts a SMI write command. */ ENET_StartSMIWrite(base, phyAddr, phyReg, kENET_MiiWriteValidFrame, data); /* Wait for SMI complete. */ for (counter = SMI_TIMEOUT_COUNT; counter > 0; counter--) { if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK) { break; } } /* Check for timeout. */ if (!counter) { return kStatus_PHY_SMIVisitTimeout; } /* Clear MII interrupt event. */ ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK); return kStatus_Success; } status_t PHY_Read(ENET_Type *base, uint32_t phyAddr, uint32_t phyReg, uint32_t *dataPtr) { assert(dataPtr); uint32_t counter; /* Clear the MII interrupt event. */ ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK); /* Starts a SMI read command operation. */ ENET_StartSMIRead(base, phyAddr, phyReg, kENET_MiiReadValidFrame); /* Wait for MII complete. */ for (counter = SMI_TIMEOUT_COUNT; counter > 0; counter--) { if (ENET_GetInterruptStatus(base) & ENET_EIR_MII_MASK) { break; } } /* Check for timeout. */ if (!counter) { return kStatus_PHY_SMIVisitTimeout; } /* Get data from MII register. */ *dataPtr = ENET_ReadSMIData(base); /* Clear MII interrupt event. */ ENET_ClearInterruptStatus(base, ENET_EIR_MII_MASK); return kStatus_Success; } status_t PHY_GetLinkStatus(ENET_Type *base, uint32_t phyAddr, bool *status) { assert(status); status_t result = kStatus_Success; uint32_t data; /* Read the basic status register. */ result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &data); if (result == kStatus_Success) { if (!(PHY_BSTATUS_LINKSTATUS_MASK & data)) { /* link down. */ *status = false; } else { /* link up. */ *status = true; } } return result; } status_t PHY_GetAutonegotiationStatus(ENET_Type *base, uint32_t phyAddr, bool *status) { assert(status); status_t result = kStatus_Success; uint32_t data; /* Read the basic status register. */ result = PHY_Read(base, phyAddr, PHY_BASICSTATUS_REG, &data); if (result == kStatus_Success) { *status = data & PHY_BSTATUS_AUTONEGCOMP_MASK; } return result; } status_t PHY_StartAutonegotiation(ENET_Type *base, uint32_t phyAddr) { uint32_t bmcrVal; status_t result = PHY_Read(base, phyAddr, PHY_BASICCONTROL_REG, &bmcrVal); bmcrVal |= PHY_BCTL_RESTART_AUTONEG_MASK; result |= PHY_Write(base, phyAddr, PHY_BASICCONTROL_REG, bmcrVal); return result; }