Path: blob/main/sys/contrib/ncsw/Peripherals/FM/MAC/tgec_mii_acc.c
48524 views
/*1* Copyright 2008-2012 Freescale Semiconductor Inc.2*3* Redistribution and use in source and binary forms, with or without4* modification, are permitted provided that the following conditions are met:5* * Redistributions of source code must retain the above copyright6* notice, this list of conditions and the following disclaimer.7* * Redistributions in binary form must reproduce the above copyright8* notice, this list of conditions and the following disclaimer in the9* documentation and/or other materials provided with the distribution.10* * Neither the name of Freescale Semiconductor nor the11* names of its contributors may be used to endorse or promote products12* derived from this software without specific prior written permission.13*14*15* ALTERNATIVELY, this software may be distributed under the terms of the16* GNU General Public License ("GPL") as published by the Free Software17* Foundation, either version 2 of that License or (at your option) any18* later version.19*20* THIS SOFTWARE IS PROVIDED BY Freescale Semiconductor ``AS IS'' AND ANY21* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED22* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE23* DISCLAIMED. IN NO EVENT SHALL Freescale Semiconductor BE LIABLE FOR ANY24* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES25* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;26* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND27* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT28* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS29* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.30*/31323334#include "error_ext.h"35#include "std_ext.h"36#include "fm_mac.h"37#include "tgec.h"38#include "xx_ext.h"3940#include "fm_common.h"414243/*****************************************************************************/44t_Error TGEC_MII_WritePhyReg(t_Handle h_Tgec,45uint8_t phyAddr,46uint8_t reg,47uint16_t data)48{49t_Tgec *p_Tgec = (t_Tgec *)h_Tgec;50t_TgecMiiAccessMemMap *p_MiiAccess;51uint32_t cfgStatusReg;5253SANITY_CHECK_RETURN_ERROR(p_Tgec, E_INVALID_HANDLE);54SANITY_CHECK_RETURN_ERROR(p_Tgec->p_MiiMemMap, E_INVALID_HANDLE);5556p_MiiAccess = p_Tgec->p_MiiMemMap;5758/* Configure MII */59cfgStatusReg = GET_UINT32(p_MiiAccess->mdio_cfg_status);60cfgStatusReg &= ~MIIMCOM_DIV_MASK;61/* (one half of fm clock => 2.5Mhz) */62cfgStatusReg |=((((p_Tgec->fmMacControllerDriver.clkFreq*10)/2)/25) << MIIMCOM_DIV_SHIFT);63WRITE_UINT32(p_MiiAccess->mdio_cfg_status, cfgStatusReg);6465while ((GET_UINT32(p_MiiAccess->mdio_cfg_status)) & MIIMIND_BUSY)66XX_UDelay (1);6768WRITE_UINT32(p_MiiAccess->mdio_command, phyAddr);6970WRITE_UINT32(p_MiiAccess->mdio_regaddr, reg);7172CORE_MemoryBarrier();7374while ((GET_UINT32(p_MiiAccess->mdio_cfg_status)) & MIIMIND_BUSY)75XX_UDelay (1);7677WRITE_UINT32(p_MiiAccess->mdio_data, data);7879CORE_MemoryBarrier();8081while ((GET_UINT32(p_MiiAccess->mdio_data)) & MIIDATA_BUSY)82XX_UDelay (1);8384return E_OK;85}8687/*****************************************************************************/88t_Error TGEC_MII_ReadPhyReg(t_Handle h_Tgec,89uint8_t phyAddr,90uint8_t reg,91uint16_t *p_Data)92{93t_Tgec *p_Tgec = (t_Tgec *)h_Tgec;94t_TgecMiiAccessMemMap *p_MiiAccess;95uint32_t cfgStatusReg;9697SANITY_CHECK_RETURN_ERROR(p_Tgec, E_INVALID_HANDLE);98SANITY_CHECK_RETURN_ERROR(p_Tgec->p_MiiMemMap, E_INVALID_HANDLE);99100p_MiiAccess = p_Tgec->p_MiiMemMap;101102/* Configure MII */103cfgStatusReg = GET_UINT32(p_MiiAccess->mdio_cfg_status);104cfgStatusReg &= ~MIIMCOM_DIV_MASK;105/* (one half of fm clock => 2.5Mhz) */106cfgStatusReg |=((((p_Tgec->fmMacControllerDriver.clkFreq*10)/2)/25) << MIIMCOM_DIV_SHIFT);107WRITE_UINT32(p_MiiAccess->mdio_cfg_status, cfgStatusReg);108109while ((GET_UINT32(p_MiiAccess->mdio_cfg_status)) & MIIMIND_BUSY)110XX_UDelay (1);111112WRITE_UINT32(p_MiiAccess->mdio_command, phyAddr);113114WRITE_UINT32(p_MiiAccess->mdio_regaddr, reg);115116CORE_MemoryBarrier();117118while ((GET_UINT32(p_MiiAccess->mdio_cfg_status)) & MIIMIND_BUSY)119XX_UDelay (1);120121WRITE_UINT32(p_MiiAccess->mdio_command, (uint32_t)(phyAddr | MIIMCOM_READ_CYCLE));122123CORE_MemoryBarrier();124125while ((GET_UINT32(p_MiiAccess->mdio_data)) & MIIDATA_BUSY)126XX_UDelay (1);127128*p_Data = (uint16_t)GET_UINT32(p_MiiAccess->mdio_data);129130cfgStatusReg = GET_UINT32(p_MiiAccess->mdio_cfg_status);131132if (cfgStatusReg & MIIMIND_READ_ERROR)133RETURN_ERROR(MINOR, E_INVALID_VALUE,134("Read Error: phyAddr 0x%x, dev 0x%x, reg 0x%x, cfgStatusReg 0x%x",135((phyAddr & 0xe0)>>5), (phyAddr & 0x1f), reg, cfgStatusReg));136137return E_OK;138}139140141