xref: /OK3568_Linux_fs/kernel/drivers/net/wireless/rockchip_wlan/rtl8821cs/hal/hal_phy.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 /******************************************************************************
3  *
4  * Copyright(c) 2007 - 2017 Realtek Corporation.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of version 2 of the GNU General Public License as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  *****************************************************************************/
16 #define _HAL_PHY_C_
17 
18 #include <drv_types.h>
19 
20 /**
21 * Function:	PHY_CalculateBitShift
22 *
23 * OverView:	Get shifted position of the BitMask
24 *
25 * Input:
26 *			u32		BitMask,
27 *
28 * Output:	none
29 * Return:		u32		Return the shift bit bit position of the mask
30 */
31 u32
PHY_CalculateBitShift(u32 BitMask)32 PHY_CalculateBitShift(
33 	u32 BitMask
34 )
35 {
36 	u32 i;
37 
38 	for (i = 0; i <= 31; i++) {
39 		if (((BitMask >> i) &  0x1) == 1)
40 			break;
41 	}
42 
43 	return i;
44 }
45 
46 
47 #ifdef CONFIG_RF_SHADOW_RW
48 /* ********************************************************************************
49  *	Constant.
50  * ********************************************************************************
51  * 2008/11/20 MH For Debug only, RF */
52 static RF_SHADOW_T RF_Shadow[RF6052_MAX_PATH][RF6052_MAX_REG];
53 
54 /*
55  * ==> RF shadow Operation API Code Section!!!
56  *
57  *-----------------------------------------------------------------------------
58  * Function:	PHY_RFShadowRead
59  *				PHY_RFShadowWrite
60  *				PHY_RFShadowCompare
61  *				PHY_RFShadowRecorver
62  *				PHY_RFShadowCompareAll
63  *				PHY_RFShadowRecorverAll
64  *				PHY_RFShadowCompareFlagSet
65  *				PHY_RFShadowRecorverFlagSet
66  *
67  * Overview:	When we set RF register, we must write shadow at first.
68  *			When we are running, we must compare shadow abd locate error addr.
69  *			Decide to recorver or not.
70  *
71  * Input:       NONE
72  *
73  * Output:      NONE
74  *
75  * Return:      NONE
76  *
77  * Revised History:
78  * When			Who		Remark
79  * 11/20/2008	MHC		Create Version 0.
80  *
81  *---------------------------------------------------------------------------*/
82 u32
PHY_RFShadowRead(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset)83 PHY_RFShadowRead(
84 		PADAPTER		Adapter,
85 		enum rf_path		eRFPath,
86 		u32				Offset)
87 {
88 	return	RF_Shadow[eRFPath][Offset].Value;
89 
90 }	/* PHY_RFShadowRead */
91 
92 
93 void
PHY_RFShadowWrite(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset,u32 Data)94 PHY_RFShadowWrite(
95 		PADAPTER		Adapter,
96 		enum rf_path		eRFPath,
97 		u32				Offset,
98 		u32				Data)
99 {
100 	RF_Shadow[eRFPath][Offset].Value = (Data & bRFRegOffsetMask);
101 	RF_Shadow[eRFPath][Offset].Driver_Write = _TRUE;
102 
103 }	/* PHY_RFShadowWrite */
104 
105 
106 BOOLEAN
PHY_RFShadowCompare(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset)107 PHY_RFShadowCompare(
108 		PADAPTER		Adapter,
109 		enum rf_path		eRFPath,
110 		u32				Offset)
111 {
112 	u32	reg;
113 	/* Check if we need to check the register */
114 	if (RF_Shadow[eRFPath][Offset].Compare == _TRUE) {
115 		reg = rtw_hal_read_rfreg(Adapter, eRFPath, Offset, bRFRegOffsetMask);
116 		/* Compare shadow and real rf register for 20bits!! */
117 		if (RF_Shadow[eRFPath][Offset].Value != reg) {
118 			/* Locate error position. */
119 			RF_Shadow[eRFPath][Offset].ErrorOrNot = _TRUE;
120 		}
121 		return RF_Shadow[eRFPath][Offset].ErrorOrNot ;
122 	}
123 	return _FALSE;
124 }	/* PHY_RFShadowCompare */
125 
126 
127 void
PHY_RFShadowRecorver(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset)128 PHY_RFShadowRecorver(
129 		PADAPTER		Adapter,
130 		enum rf_path		eRFPath,
131 		u32				Offset)
132 {
133 	/* Check if the address is error */
134 	if (RF_Shadow[eRFPath][Offset].ErrorOrNot == _TRUE) {
135 		/* Check if we need to recorver the register. */
136 		if (RF_Shadow[eRFPath][Offset].Recorver == _TRUE) {
137 			rtw_hal_write_rfreg(Adapter, eRFPath, Offset, bRFRegOffsetMask,
138 					    RF_Shadow[eRFPath][Offset].Value);
139 		}
140 	}
141 
142 }	/* PHY_RFShadowRecorver */
143 
144 
145 void
PHY_RFShadowCompareAll(PADAPTER Adapter)146 PHY_RFShadowCompareAll(
147 		PADAPTER			Adapter)
148 {
149 	enum rf_path	eRFPath = RF_PATH_A;
150 	u32		Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter);
151 
152 	for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) {
153 		for (Offset = 0; Offset < maxReg; Offset++)
154 			PHY_RFShadowCompare(Adapter, eRFPath, Offset);
155 	}
156 
157 }	/* PHY_RFShadowCompareAll */
158 
159 
160 void
PHY_RFShadowRecorverAll(PADAPTER Adapter)161 PHY_RFShadowRecorverAll(
162 		PADAPTER			Adapter)
163 {
164 	enum rf_path		eRFPath = RF_PATH_A;
165 	u32		Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter);
166 
167 	for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) {
168 		for (Offset = 0; Offset < maxReg; Offset++)
169 			PHY_RFShadowRecorver(Adapter, eRFPath, Offset);
170 	}
171 
172 }	/* PHY_RFShadowRecorverAll */
173 
174 
175 void
PHY_RFShadowCompareFlagSet(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset,u8 Type)176 PHY_RFShadowCompareFlagSet(
177 		PADAPTER		Adapter,
178 		enum rf_path		eRFPath,
179 		u32				Offset,
180 		u8				Type)
181 {
182 	/* Set True or False!!! */
183 	RF_Shadow[eRFPath][Offset].Compare = Type;
184 
185 }	/* PHY_RFShadowCompareFlagSet */
186 
187 
188 void
PHY_RFShadowRecorverFlagSet(PADAPTER Adapter,enum rf_path eRFPath,u32 Offset,u8 Type)189 PHY_RFShadowRecorverFlagSet(
190 		PADAPTER		Adapter,
191 		enum rf_path		eRFPath,
192 		u32				Offset,
193 		u8				Type)
194 {
195 	/* Set True or False!!! */
196 	RF_Shadow[eRFPath][Offset].Recorver = Type;
197 
198 }	/* PHY_RFShadowRecorverFlagSet */
199 
200 
201 void
PHY_RFShadowCompareFlagSetAll(PADAPTER Adapter)202 PHY_RFShadowCompareFlagSetAll(
203 		PADAPTER			Adapter)
204 {
205 	enum rf_path	eRFPath = RF_PATH_A;
206 	u32		Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter);
207 
208 	for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) {
209 		for (Offset = 0; Offset < maxReg; Offset++) {
210 			/* 2008/11/20 MH For S3S4 test, we only check reg 26/27 now!!!! */
211 			if (Offset != 0x26 && Offset != 0x27)
212 				PHY_RFShadowCompareFlagSet(Adapter, eRFPath, Offset, _FALSE);
213 			else
214 				PHY_RFShadowCompareFlagSet(Adapter, eRFPath, Offset, _TRUE);
215 		}
216 	}
217 
218 }	/* PHY_RFShadowCompareFlagSetAll */
219 
220 
221 void
PHY_RFShadowRecorverFlagSetAll(PADAPTER Adapter)222 PHY_RFShadowRecorverFlagSetAll(
223 		PADAPTER			Adapter)
224 {
225 	enum rf_path		eRFPath = RF_PATH_A;
226 	u32		Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter);
227 
228 	for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) {
229 		for (Offset = 0; Offset < maxReg; Offset++) {
230 			/* 2008/11/20 MH For S3S4 test, we only check reg 26/27 now!!!! */
231 			if (Offset != 0x26 && Offset != 0x27)
232 				PHY_RFShadowRecorverFlagSet(Adapter, eRFPath, Offset, _FALSE);
233 			else
234 				PHY_RFShadowRecorverFlagSet(Adapter, eRFPath, Offset, _TRUE);
235 		}
236 	}
237 
238 }	/* PHY_RFShadowCompareFlagSetAll */
239 
240 void
PHY_RFShadowRefresh(PADAPTER Adapter)241 PHY_RFShadowRefresh(
242 		PADAPTER			Adapter)
243 {
244 	enum rf_path		eRFPath = RF_PATH_A;
245 	u32		Offset = 0, maxReg = GET_RF6052_REAL_MAX_REG(Adapter);
246 
247 	for (eRFPath = 0; eRFPath < RF6052_MAX_PATH; eRFPath++) {
248 		for (Offset = 0; Offset < maxReg; Offset++) {
249 			RF_Shadow[eRFPath][Offset].Value = 0;
250 			RF_Shadow[eRFPath][Offset].Compare = _FALSE;
251 			RF_Shadow[eRFPath][Offset].Recorver  = _FALSE;
252 			RF_Shadow[eRFPath][Offset].ErrorOrNot = _FALSE;
253 			RF_Shadow[eRFPath][Offset].Driver_Write = _FALSE;
254 		}
255 	}
256 
257 }	/* PHY_RFShadowRead */
258 #endif /*CONFIG_RF_SHADOW_RW*/
259