1 /*
2 * Copyright (c) 2021 HiSilicon (Shanghai) Technologies CO., LIMITED.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "hieth.h"
17 #include "mdio.h"
18
WaitMdioReady(struct HiethNetdevLocal * ld)19 static int32_t WaitMdioReady(struct HiethNetdevLocal *ld)
20 {
21 int32_t timeout = 1000;
22
23 while (--timeout && !TestMdioReady(ld)) {
24 udelay(1);
25 }
26 return timeout;
27 }
28
HiethMdioRead(struct HiethNetdevLocal * ld,int32_t phyAddr,int32_t regNum)29 int32_t HiethMdioRead(struct HiethNetdevLocal *ld, int32_t phyAddr, int32_t regNum)
30 {
31 int32_t val = 0;
32
33 HiethAssert((!((uint32_t)phyAddr & (~0x1F))) && (!((uint32_t)regNum & (~0x1F))));
34 if (!WaitMdioReady(ld)) {
35 HiethError("mdio busy");
36 return val;
37 }
38 MdioStartPhyread(ld, phyAddr, regNum);
39 if (WaitMdioReady(ld)) {
40 val = MdioGetPhyreadVal(ld);
41 } else {
42 HiethError("read timeout");
43 }
44
45 return val;
46 }
47
HiethMdioWrite(struct HiethNetdevLocal * ld,int32_t phyAddr,int32_t regNum,int32_t val)48 int32_t HiethMdioWrite(struct HiethNetdevLocal *ld, int32_t phyAddr, int32_t regNum, int32_t val)
49 {
50 HiethAssert((!((uint32_t)phyAddr & (~0x1F))) && (!((uint32_t)regNum & (~0x1F))));
51 HiethTrace(HIETHTRACE_LEVEL_L4, "phyAddr = %d, regNum = %d", phyAddr, regNum);
52
53 OsalSpinLockIrq(&hiethGlbRegLock);
54
55 if (!WaitMdioReady(ld)) {
56 HiethError("mdio busy");
57 OsalSpinUnlockIrq(&hiethGlbRegLock);
58 return HDF_FAILURE;
59 }
60 MdioPhyWrite(ld, phyAddr, regNum, val);
61 OsalSpinUnlockIrq(&hiethGlbRegLock);
62 return HDF_SUCCESS;
63 }
64
HiethMdioReset(struct HiethNetdevLocal * ld)65 int32_t HiethMdioReset(struct HiethNetdevLocal *ld)
66 {
67 MdioRegReset(ld);
68 return HDF_SUCCESS;
69 }
70
HiethMdioInit(struct HiethNetdevLocal * ld)71 int32_t HiethMdioInit(struct HiethNetdevLocal *ld)
72 {
73 int32_t ret;
74 ret = HiethMdioReset(ld);
75 if (ret != HDF_SUCCESS) {
76 HDF_LOGE("%s: HiethMdioReset failed", __func__);
77 return HDF_FAILURE;
78 }
79 return HDF_SUCCESS;
80 }
81
HiethMdioExit(struct HiethNetdevLocal * ld)82 void HiethMdioExit(struct HiethNetdevLocal *ld)
83 {
84 }
85