11bd4fe43Sopenharmony_ci/*
21bd4fe43Sopenharmony_ci * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
31bd4fe43Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
41bd4fe43Sopenharmony_ci * you may not use this file except in compliance with the License.
51bd4fe43Sopenharmony_ci * You may obtain a copy of the License at
61bd4fe43Sopenharmony_ci *
71bd4fe43Sopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
81bd4fe43Sopenharmony_ci *
91bd4fe43Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software
101bd4fe43Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
111bd4fe43Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
121bd4fe43Sopenharmony_ci * See the License for the specific language governing permissions and
131bd4fe43Sopenharmony_ci * limitations under the License.
141bd4fe43Sopenharmony_ci */
151bd4fe43Sopenharmony_ci
161bd4fe43Sopenharmony_ci#include "hieth.h"
171bd4fe43Sopenharmony_ci#include "mdio.h"
181bd4fe43Sopenharmony_ci
191bd4fe43Sopenharmony_cistatic int32_t WaitMdioReady(struct HiethNetdevLocal *ld)
201bd4fe43Sopenharmony_ci{
211bd4fe43Sopenharmony_ci    int32_t timeout = 1000;
221bd4fe43Sopenharmony_ci
231bd4fe43Sopenharmony_ci    while (--timeout && !TestMdioReady(ld)) {
241bd4fe43Sopenharmony_ci        udelay(1);
251bd4fe43Sopenharmony_ci    }
261bd4fe43Sopenharmony_ci    return timeout;
271bd4fe43Sopenharmony_ci}
281bd4fe43Sopenharmony_ci
291bd4fe43Sopenharmony_ciint32_t HiethMdioRead(struct HiethNetdevLocal *ld, int32_t phyAddr, int32_t regNum)
301bd4fe43Sopenharmony_ci{
311bd4fe43Sopenharmony_ci    int32_t val = 0;
321bd4fe43Sopenharmony_ci
331bd4fe43Sopenharmony_ci    HiethAssert((!((uint32_t)phyAddr & (~0x1F))) && (!((uint32_t)regNum & (~0x1F))));
341bd4fe43Sopenharmony_ci    if (!WaitMdioReady(ld)) {
351bd4fe43Sopenharmony_ci        HiethError("mdio busy");
361bd4fe43Sopenharmony_ci        return val;
371bd4fe43Sopenharmony_ci    }
381bd4fe43Sopenharmony_ci    MdioStartPhyread(ld, phyAddr, regNum);
391bd4fe43Sopenharmony_ci    if (WaitMdioReady(ld)) {
401bd4fe43Sopenharmony_ci        val = MdioGetPhyreadVal(ld);
411bd4fe43Sopenharmony_ci    } else {
421bd4fe43Sopenharmony_ci        HiethError("read timeout");
431bd4fe43Sopenharmony_ci    }
441bd4fe43Sopenharmony_ci
451bd4fe43Sopenharmony_ci    return val;
461bd4fe43Sopenharmony_ci}
471bd4fe43Sopenharmony_ci
481bd4fe43Sopenharmony_ciint32_t HiethMdioWrite(struct HiethNetdevLocal *ld, int32_t phyAddr, int32_t regNum, int32_t val)
491bd4fe43Sopenharmony_ci{
501bd4fe43Sopenharmony_ci    HiethAssert((!((uint32_t)phyAddr & (~0x1F))) && (!((uint32_t)regNum & (~0x1F))));
511bd4fe43Sopenharmony_ci    HiethTrace(HIETHTRACE_LEVEL_L4, "phyAddr = %d, regNum = %d", phyAddr, regNum);
521bd4fe43Sopenharmony_ci
531bd4fe43Sopenharmony_ci    OsalSpinLockIrq(&hiethGlbRegLock);
541bd4fe43Sopenharmony_ci
551bd4fe43Sopenharmony_ci    if (!WaitMdioReady(ld)) {
561bd4fe43Sopenharmony_ci        HiethError("mdio busy");
571bd4fe43Sopenharmony_ci        OsalSpinUnlockIrq(&hiethGlbRegLock);
581bd4fe43Sopenharmony_ci        return HDF_FAILURE;
591bd4fe43Sopenharmony_ci    }
601bd4fe43Sopenharmony_ci    MdioPhyWrite(ld, phyAddr, regNum, val);
611bd4fe43Sopenharmony_ci    OsalSpinUnlockIrq(&hiethGlbRegLock);
621bd4fe43Sopenharmony_ci    return HDF_SUCCESS;
631bd4fe43Sopenharmony_ci}
641bd4fe43Sopenharmony_ci
651bd4fe43Sopenharmony_ciint32_t HiethMdioReset(struct HiethNetdevLocal *ld)
661bd4fe43Sopenharmony_ci{
671bd4fe43Sopenharmony_ci    MdioRegReset(ld);
681bd4fe43Sopenharmony_ci    return HDF_SUCCESS;
691bd4fe43Sopenharmony_ci}
701bd4fe43Sopenharmony_ci
711bd4fe43Sopenharmony_ciint32_t HiethMdioInit(struct HiethNetdevLocal *ld)
721bd4fe43Sopenharmony_ci{
731bd4fe43Sopenharmony_ci    int32_t ret;
741bd4fe43Sopenharmony_ci    ret = HiethMdioReset(ld);
751bd4fe43Sopenharmony_ci    if (ret != HDF_SUCCESS) {
761bd4fe43Sopenharmony_ci        HDF_LOGE("%s: HiethMdioReset failed", __func__);
771bd4fe43Sopenharmony_ci        return HDF_FAILURE;
781bd4fe43Sopenharmony_ci    }
791bd4fe43Sopenharmony_ci    return HDF_SUCCESS;
801bd4fe43Sopenharmony_ci}
811bd4fe43Sopenharmony_ci
821bd4fe43Sopenharmony_civoid HiethMdioExit(struct HiethNetdevLocal *ld)
831bd4fe43Sopenharmony_ci{
841bd4fe43Sopenharmony_ci}
85