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