blob: 82c00995a669049a066994550e25885dd225d63f [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001//============================================================
2//
3// File Name: Hal8192CDMOutSrc.c
4//
5// Description:
6//
7// This file is for 92CE/92CU outsource dynamic mechanism for partner.
8//
9//
10//============================================================
11
12#ifndef _HAL8192CDM_C_
13#define _HAL8192CDM_C_
14
15#ifdef __KERNEL__
16#include <linux/module.h>
17#include <linux/kernel.h>
18#include <linux/init.h>
19#include <asm/uaccess.h>
20#include <linux/fcntl.h>
21#include <linux/fs.h>
22#include <linux/file.h>
23#include <asm/unistd.h>
24#elif defined(__ECOS)
25#include <cyg/io/eth/rltk/819x/wrapper/sys_support.h>
26#include <cyg/io/eth/rltk/819x/wrapper/skbuff.h>
27#include <cyg/io/eth/rltk/819x/wrapper/timer.h>
28#include <cyg/io/eth/rltk/819x/wrapper/wrapper.h>
29#endif
30
31#include "./8192cd_cfg.h"
32#include "./8192cd.h"
33#include "./8192cd_hw.h"
34#include "./8192cd_headers.h"
35#include "./8192cd_debug.h"
36
37
38
39#ifdef __KERNEL__
40#ifdef __LINUX_2_6__
41#include <linux/syscalls.h>
42#else
43#include <linux/fs.h>
44#endif
45#endif
46
47#if defined(CONFIG_RTL_819X) && defined(USE_RLX_BSP)
48#if defined(CONFIG_OPENWRT_SDK) && !defined(CONFIG_ARCH_CPU_RLX)
49#include <bspchip.h>
50#else
51#include <bsp/bspchip.h>
52#endif //CONFIG_OPENWRT_SDK
53#endif
54
55
56//Analog Pre-distortion calibration
57#define APK_BB_REG_NUM 5
58#define APK_AFE_REG_NUM 16
59#define APK_CURVE_REG_NUM 4
60#define PATH_NUM 2
61
62//============================================================
63// Global var
64//============================================================
65
66#if !defined(USE_OUT_SRC) || defined(_OUTSRC_COEXIST)
67unsigned int ofdm_swing_table[] = {
68 0x7f8001fe, // 0, +6.0dB
69 0x788001e2, // 1, +5.5dB
70 0x71c001c7, // 2, +5.0dB
71 0x6b8001ae, // 3, +4.5dB
72 0x65400195, // 4, +4.0dB
73 0x5fc0017f, // 5, +3.5dB
74 0x5a400169, // 6, +3.0dB
75 0x55400155, // 7, +2.5dB
76 0x50800142, // 8, +2.0dB
77 0x4c000130, // 9, +1.5dB
78 0x47c0011f, // 10, +1.0dB
79 0x43c0010f, // 11, +0.5dB
80 0x40000100, // 12, +0dB
81 0x3c8000f2, // 13, -0.5dB
82 0x390000e4, // 14, -1.0dB
83 0x35c000d7, // 15, -1.5dB
84 0x32c000cb, // 16, -2.0dB
85 0x300000c0, // 17, -2.5dB
86 0x2d4000b5, // 18, -3.0dB
87 0x2ac000ab, // 19, -3.5dB
88 0x288000a2, // 20, -4.0dB
89 0x26000098, // 21, -4.5dB
90 0x24000090, // 22, -5.0dB
91 0x22000088, // 23, -5.5dB
92 0x20000080, // 24, -6.0dB
93 0x1e400079, // 25, -6.5dB
94 0x1c800072, // 26, -7.0dB
95 0x1b00006c, // 27. -7.5dB
96 0x19800066, // 28, -8.0dB
97 0x18000060, // 29, -8.5dB
98 0x16c0005b, // 30, -9.0dB
99 0x15800056, // 31, -9.5dB
100 0x14400051, // 32, -10.0dB
101 0x1300004c, // 33, -10.5dB
102 0x12000048, // 34, -11.0dB
103 0x11000044, // 35, -11.5dB
104 0x10000040, // 36, -12.0dB
105};
106
107
108unsigned int TxPwrTrk_OFDM_SwingTbl[TxPwrTrk_OFDM_SwingTbl_Len] = {
109 /* +6.0dB */ 0x7f8001fe,
110 /* +5.5dB */ 0x788001e2,
111 /* +5.0dB */ 0x71c001c7,
112 /* +4.5dB */ 0x6b8001ae,
113 /* +4.0dB */ 0x65400195,
114 /* +3.5dB */ 0x5fc0017f,
115 /* +3.0dB */ 0x5a400169,
116 /* +2.5dB */ 0x55400155,
117 /* +2.0dB */ 0x50800142,
118 /* +1.5dB */ 0x4c000130,
119 /* +1.0dB */ 0x47c0011f,
120 /* +0.5dB */ 0x43c0010f,
121 /* 0.0dB */ 0x40000100,
122 /* -0.5dB */ 0x3c8000f2,
123 /* -1.0dB */ 0x390000e4,
124 /* -1.5dB */ 0x35c000d7,
125 /* -2.0dB */ 0x32c000cb,
126 /* -2.5dB */ 0x300000c0,
127 /* -3.0dB */ 0x2d4000b5,
128 /* -3.5dB */ 0x2ac000ab,
129 /* -4.0dB */ 0x288000a2,
130 /* -4.5dB */ 0x26000098,
131 /* -5.0dB */ 0x24000090,
132 /* -5.5dB */ 0x22000088,
133 /* -6.0dB */ 0x20000080,
134 /* -6.5dB */ 0x1a00006c,
135 /* -7.0dB */ 0x1c800072,
136 /* -7.5dB */ 0x18000060,
137 /* -8.0dB */ 0x19800066,
138 /* -8.5dB */ 0x15800056,
139 /* -9.0dB */ 0x26c0005b,
140 /* -9.5dB */ 0x14400051,
141 /* -10.0dB */ 0x24400051,
142 /* -10.5dB */ 0x1300004c,
143 /* -11.0dB */ 0x12000048,
144 /* -11.5dB */ 0x11000044,
145 /* -12.0dB */ 0x10000040
146};
147
148unsigned char TxPwrTrk_CCK_SwingTbl[TxPwrTrk_CCK_SwingTbl_Len][8] = {
149 /* 0.0dB */ {0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04},
150 /* 0.5dB */ {0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04},
151 /* 1.0dB */ {0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03},
152 /* 1.5dB */ {0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03},
153 /* 2.0dB */ {0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03},
154 /* 2.5dB */ {0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03},
155 /* 3.0dB */ {0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03},
156 /* 3.5dB */ {0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03},
157 /* 4.0dB */ {0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02},
158 /* 4.5dB */ {0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02},
159 /* 5.0dB */ {0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02},
160 /* 5.5dB */ {0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02},
161 /* 6.0dB */ {0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02},
162 /* 6.5dB */ {0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02},
163 /* 7.0dB */ {0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02},
164 /* 7.5dB */ {0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02},
165 /* 8.0dB */ {0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01},
166 /* 8.5dB */ {0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02},
167 /* 9.0dB */ {0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01},
168 /* 9.5dB */ {0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
169 /* 10.0dB */ {0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01},
170 /* 10.5dB */ {0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01},
171 /* 11.0dB */ {0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}
172};
173
174unsigned char TxPwrTrk_CCK_SwingTbl_CH14[TxPwrTrk_CCK_SwingTbl_Len][8] = {
175 /* 0.0dB */ {0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00},
176 /* 0.5dB */ {0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00},
177 /* 1.0dB */ {0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00},
178 /* 1.5dB */ {0x2d, 0x2d, 0x27, 0x17, 0x00, 0x00, 0x00, 0x00},
179 /* 2.0dB */ {0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00},
180 /* 2.5dB */ {0x28, 0x28, 0x22, 0x14, 0x00, 0x00, 0x00, 0x00},
181 /* 3.0dB */ {0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00},
182 /* 3.5dB */ {0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00},
183 /* 4.0dB */ {0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00},
184 /* 4.5dB */ {0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00},
185 /* 5.0dB */ {0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00},
186 /* 5.5dB */ {0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00},
187 /* 6.0dB */ {0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00},
188 /* 6.5dB */ {0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00},
189 /* 7.0dB */ {0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00},
190 /* 7.5dB */ {0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00},
191 /* 8.0dB */ {0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00},
192 /* 8.5dB */ {0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00},
193 /* 9.0dB */ {0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00},
194 /* 9.5dB */ {0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
195 /* 10.0dB */ {0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00},
196 /* 10.5dB */ {0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00},
197 /* 11.0dB */ {0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}
198};
199
200//#ifndef USE_OUT_SRC
201
202unsigned char cck_swing_table_ch1_ch13[][8] = {
203{0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04}, // 0, +0dB
204{0x33, 0x32, 0x2b, 0x23, 0x1a, 0x11, 0x08, 0x04}, // 1, -0.5dB
205{0x30, 0x2f, 0x29, 0x21, 0x19, 0x10, 0x08, 0x03}, // 2, -1.0dB
206{0x2d, 0x2d, 0x27, 0x1f, 0x18, 0x0f, 0x08, 0x03}, // 3, -1.5dB
207{0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03}, // 4, -2.0dB
208{0x28, 0x28, 0x22, 0x1c, 0x15, 0x0d, 0x07, 0x03}, // 5, -2.5dB
209{0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03}, // 6, -3.0dB
210{0x24, 0x23, 0x1f, 0x19, 0x13, 0x0c, 0x06, 0x03}, // 7, -3.5dB
211{0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02}, // 8, -4.0dB
212{0x20, 0x20, 0x1b, 0x16, 0x11, 0x08, 0x05, 0x02}, // 9, -4.5dB
213{0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02}, // 10, -5.0dB
214{0x1d, 0x1c, 0x18, 0x14, 0x0f, 0x0a, 0x05, 0x02}, // 11, -5.5dB
215{0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02}, // 12, -6.0dB
216{0x1a, 0x19, 0x16, 0x12, 0x0d, 0x09, 0x04, 0x02}, // 13, -6.5dB
217{0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02}, // 14, -7.0dB
218{0x17, 0x16, 0x13, 0x10, 0x0c, 0x08, 0x04, 0x02}, // 15, -7.5dB
219{0x16, 0x15, 0x12, 0x0f, 0x0b, 0x07, 0x04, 0x01}, // 16, -8.0dB
220{0x14, 0x14, 0x11, 0x0e, 0x0b, 0x07, 0x03, 0x02}, // 17, -8.5dB
221{0x13, 0x13, 0x10, 0x0d, 0x0a, 0x06, 0x03, 0x01}, // 18, -9.0dB
222{0x12, 0x12, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, // 19, -9.5dB
223{0x11, 0x11, 0x0f, 0x0c, 0x09, 0x06, 0x03, 0x01}, // 20, -10.0dB
224{0x10, 0x10, 0x0e, 0x0b, 0x08, 0x05, 0x03, 0x01}, // 21, -10.5dB
225{0x0f, 0x0f, 0x0d, 0x0b, 0x08, 0x05, 0x03, 0x01}, // 22, -11.0dB
226{0x0e, 0x0e, 0x0c, 0x0a, 0x08, 0x05, 0x02, 0x01}, // 23, -11.5dB
227{0x0d, 0x0d, 0x0c, 0x0a, 0x07, 0x05, 0x02, 0x01}, // 24, -12.0dB
228{0x0d, 0x0c, 0x0b, 0x09, 0x07, 0x04, 0x02, 0x01}, // 25, -12.5dB
229{0x0c, 0x0c, 0x0a, 0x09, 0x06, 0x04, 0x02, 0x01}, // 26, -13.0dB
230{0x0b, 0x0b, 0x0a, 0x08, 0x06, 0x04, 0x02, 0x01}, // 27, -13.5dB
231{0x0b, 0x0a, 0x09, 0x08, 0x06, 0x04, 0x02, 0x01}, // 28, -14.0dB
232{0x0a, 0x0a, 0x09, 0x07, 0x05, 0x03, 0x02, 0x01}, // 29, -14.5dB
233{0x0a, 0x09, 0x08, 0x07, 0x05, 0x03, 0x02, 0x01}, // 30, -15.0dB
234{0x09, 0x09, 0x08, 0x06, 0x05, 0x03, 0x01, 0x01}, // 31, -15.5dB
235{0x09, 0x08, 0x07, 0x06, 0x04, 0x03, 0x01, 0x01} // 32, -16.0dB
236};
237
238unsigned char cck_swing_table_ch14 [][8]= {
239{0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00}, // 0, +0dB
240{0x33, 0x32, 0x2b, 0x19, 0x00, 0x00, 0x00, 0x00}, // 1, -0.5dB
241{0x30, 0x2f, 0x29, 0x18, 0x00, 0x00, 0x00, 0x00}, // 2, -1.0dB
242{0x2d, 0x2d, 0x17, 0x17, 0x00, 0x00, 0x00, 0x00}, // 3, -1.5dB
243{0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00}, // 4, -2.0dB
244{0x28, 0x28, 0x24, 0x14, 0x00, 0x00, 0x00, 0x00}, // 5, -2.5dB
245{0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00}, // 6, -3.0dB
246{0x24, 0x23, 0x1f, 0x12, 0x00, 0x00, 0x00, 0x00}, // 7, -3.5dB
247{0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00}, // 8, -4.0dB
248{0x20, 0x20, 0x1b, 0x10, 0x00, 0x00, 0x00, 0x00}, // 9, -4.5dB
249{0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00}, // 10, -5.0dB
250{0x1d, 0x1c, 0x18, 0x0e, 0x00, 0x00, 0x00, 0x00}, // 11, -5.5dB
251{0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00}, // 12, -6.0dB
252{0x1a, 0x19, 0x16, 0x0d, 0x00, 0x00, 0x00, 0x00}, // 13, -6.5dB
253{0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00}, // 14, -7.0dB
254{0x17, 0x16, 0x13, 0x0b, 0x00, 0x00, 0x00, 0x00}, // 15, -7.5dB
255{0x16, 0x15, 0x12, 0x0b, 0x00, 0x00, 0x00, 0x00}, // 16, -8.0dB
256{0x14, 0x14, 0x11, 0x0a, 0x00, 0x00, 0x00, 0x00}, // 17, -8.5dB
257{0x13, 0x13, 0x10, 0x0a, 0x00, 0x00, 0x00, 0x00}, // 18, -9.0dB
258{0x12, 0x12, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, // 19, -9.5dB
259{0x11, 0x11, 0x0f, 0x09, 0x00, 0x00, 0x00, 0x00}, // 20, -10.0dB
260{0x10, 0x10, 0x0e, 0x08, 0x00, 0x00, 0x00, 0x00}, // 21, -10.5dB
261{0x0f, 0x0f, 0x0d, 0x08, 0x00, 0x00, 0x00, 0x00}, // 22, -11.0dB
262{0x0e, 0x0e, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, // 23, -11.5dB
263{0x0d, 0x0d, 0x0c, 0x07, 0x00, 0x00, 0x00, 0x00}, // 24, -12.0dB
264{0x0d, 0x0c, 0x0b, 0x06, 0x00, 0x00, 0x00, 0x00}, // 25, -12.5dB
265{0x0c, 0x0c, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, // 26, -13.0dB
266{0x0b, 0x0b, 0x0a, 0x06, 0x00, 0x00, 0x00, 0x00}, // 27, -13.5dB
267{0x0b, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, // 28, -14.0dB
268{0x0a, 0x0a, 0x09, 0x05, 0x00, 0x00, 0x00, 0x00}, // 29, -14.5dB
269{0x0a, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, // 30, -15.0dB
270{0x09, 0x09, 0x08, 0x05, 0x00, 0x00, 0x00, 0x00}, // 31, -15.5dB
271{0x09, 0x08, 0x07, 0x04, 0x00, 0x00, 0x00, 0x00} // 32, -16.0dB
272};
273
274#endif
275
276//#if defined(_OUTSRC_COEXIST)
277#if !defined(USE_OUT_SRC) || defined(_OUTSRC_COEXIST)
278const int OFDM_TABLE_SIZE= sizeof(ofdm_swing_table)/sizeof(int);
279const int CCK_TABLE_SIZE= sizeof(cck_swing_table_ch1_ch13) >>3;
280#endif
281
282
283#ifdef CONFIG_RTL_92D_SUPPORT
284
285static unsigned int OFDMSwingTable_92D[] = {
286 0x7f8001fe, // 0, +6.0dB
287 0x788001e2, // 1, +5.5dB
288 0x71c001c7, // 2, +5.0dB
289 0x6b8001ae, // 3, +4.5dB
290 0x65400195, // 4, +4.0dB
291 0x5fc0017f, // 5, +3.5dB
292 0x5a400169, // 6, +3.0dB
293 0x55400155, // 7, +2.5dB
294 0x50800142, // 8, +2.0dB
295 0x4c000130, // 9, +1.5dB
296 0x47c0011f, // 10, +1.0dB
297 0x43c0010f, // 11, +0.5dB
298 0x40000100, // 12, +0dB
299 0x3c8000f2, // 13, -0.5dB
300 0x390000e4, // 14, -1.0dB
301 0x35c000d7, // 15, -1.5dB
302 0x32c000cb, // 16, -2.0dB
303 0x300000c0, // 17, -2.5dB
304 0x2d4000b5, // 18, -3.0dB
305 0x2ac000ab, // 19, -3.5dB
306 0x288000a2, // 20, -4.0dB
307 0x26000098, // 21, -4.5dB
308 0x24000090, // 22, -5.0dB
309 0x22000088, // 23, -5.5dB
310 0x20000080, // 24, -6.0dB
311 0x1e400079, // 25, -6.5dB
312 0x1c800072, // 26, -7.0dB
313 0x1b00006c, // 27. -7.5dB
314 0x19800066, // 28, -8.0dB
315 0x18000060, // 29, -8.5dB
316 0x16c0005b, // 30, -9.0dB
317 0x15800056, // 31, -9.5dB
318 0x14400051, // 32, -10.0dB
319 0x1300004c, // 33, -10.5dB
320 0x12000048, // 34, -11.0dB
321 0x11000044, // 35, -11.5dB
322 0x10000040, // 36, -12.0dB
323 0x0f00003c,// 37, -12.5dB
324 0x0e400039,// 38, -13.0dB
325 0x0d800036,// 39, -13.5dB
326 0x0cc00033,// 40, -14.0dB
327 0x0c000030,// 41, -14.5dB
328 0x0b40002d,// 42, -15.0dB
329};
330#endif
331
332
333#ifdef HW_ANT_SWITCH
334#define RXDVY_A_EN ((HW_DIV_ENABLE && !priv->pshare->rf_ft_var.antSw_select) ? 0x80 : 0)
335#define RXDVY_B_EN ((HW_DIV_ENABLE && priv->pshare->rf_ft_var.antSw_select) ? 0x80 : 0)
336#endif
337
338
339//3 ============================================================
340//3 DIG related functions
341//3 ============================================================
342#if 0
343int getIGIFor1RCCA(int value_IGI)
344{
345 #define ONERCCA_LOW_TH 0x30
346 #define ONERCCA_LOW_DIFF 8
347
348 if (value_IGI < ONERCCA_LOW_TH) {
349 if ((ONERCCA_LOW_TH - value_IGI) < ONERCCA_LOW_DIFF)
350 return ONERCCA_LOW_TH;
351 else
352 return value_IGI + ONERCCA_LOW_DIFF;
353 } else {
354 return value_IGI;
355 }
356}
357
358
359void set_DIG_state(struct rtl8192cd_priv *priv, int state)
360{
361 int value_IGI;
362
363 if (state) {
364 priv->pshare->DIG_on = 1;
365 priv->pshare->restore = 0;
366 }
367 else {
368 priv->pshare->DIG_on = 0;
369 if (priv->pshare->restore == 0) {
370 if (priv->pshare->rf_ft_var.use_ext_lna == 1)
371 value_IGI = 0x30;
372 else
373 value_IGI = 0x20;
374
375#if defined(HW_ANT_SWITCH)
376 // wirte new initial gain index into regC50/C58
377 if (priv->pshare->rf_ft_var.one_path_cca == 0) {
378 RTL_W8(0xc50, value_IGI | RXDVY_A_EN);
379#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
380 if (
381#ifdef CONFIG_RTL_92C_SUPPORT
382 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
383#endif
384#ifdef CONFIG_RTL_92D_SUPPORT
385#ifdef CONFIG_RTL_92C_SUPPORT
386 ||
387#endif
388 (GET_CHIP_VER(priv)==VERSION_8192D)
389#endif
390 )
391 RTL_W8(0xc58, value_IGI | RXDVY_B_EN);
392#endif
393 } else if (priv->pshare->rf_ft_var.one_path_cca == 1) {
394 RTL_W8(0xc50, value_IGI | RXDVY_A_EN);
395#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
396 if (
397#ifdef CONFIG_RTL_92C_SUPPORT
398 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
399#endif
400#ifdef CONFIG_RTL_92D_SUPPORT
401#ifdef CONFIG_RTL_92C_SUPPORT
402 ||
403#endif
404 (GET_CHIP_VER(priv)==VERSION_8192D)
405#endif
406 )
407 RTL_W8(0xc58, getIGIFor1RCCA(value_IGI) | RXDVY_B_EN);
408#endif
409 } else if (priv->pshare->rf_ft_var.one_path_cca == 2) {
410 RTL_W8(0xc50, getIGIFor1RCCA(value_IGI) | RXDVY_A_EN);
411#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
412 if (
413#ifdef CONFIG_RTL_92C_SUPPORT
414 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
415#endif
416#ifdef CONFIG_RTL_92D_SUPPORT
417#ifdef CONFIG_RTL_92C_SUPPORT
418 ||
419#endif
420 (GET_CHIP_VER(priv)==VERSION_8192D)
421#endif
422 )
423 RTL_W8(0xc58, value_IGI | RXDVY_B_EN);
424#endif
425 }
426#else
427 // Write IGI into HW
428 if (priv->pshare->rf_ft_var.one_path_cca == 0) {
429 RTL_W8(0xc50, value_IGI);
430#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
431 if (
432#ifdef CONFIG_RTL_92C_SUPPORT
433 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
434#endif
435#ifdef CONFIG_RTL_92D_SUPPORT
436#ifdef CONFIG_RTL_92C_SUPPORT
437 ||
438#endif
439 (GET_CHIP_VER(priv)==VERSION_8192D)
440#endif
441 )
442 RTL_W8(0xc58, value_IGI);
443#endif
444 } else if (priv->pshare->rf_ft_var.one_path_cca == 1) {
445 RTL_W8(0xc50, value_IGI);
446#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
447 if (
448#ifdef CONFIG_RTL_92C_SUPPORT
449 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
450#endif
451#ifdef CONFIG_RTL_92D_SUPPORT
452#ifdef CONFIG_RTL_92C_SUPPORT
453 ||
454#endif
455 (GET_CHIP_VER(priv)==VERSION_8192D)
456#endif
457 )
458 RTL_W8(0xc58, getIGIFor1RCCA(value_IGI));
459#endif
460 } else if (priv->pshare->rf_ft_var.one_path_cca == 2) {
461 RTL_W8(0xc50, getIGIFor1RCCA(value_IGI));
462#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
463 if (
464#ifdef CONFIG_RTL_92C_SUPPORT
465 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
466#endif
467#ifdef CONFIG_RTL_92D_SUPPORT
468#ifdef CONFIG_RTL_92C_SUPPORT
469 ||
470#endif
471 (GET_CHIP_VER(priv)==VERSION_8192D)
472#endif
473 )
474 RTL_W8(0xc58, value_IGI);
475#endif
476 }
477#endif
478 priv->pshare->restore = 1;
479 }
480#ifdef INTERFERENCE_CONTROL
481 priv->pshare->phw->signal_strength = 0;
482#endif
483 }
484}
485#endif
486
487#ifdef CONFIG_RTL_92D_SUPPORT
488void MP_DIG_process(unsigned long task_priv)
489{
490 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv *)task_priv;
491 u4Byte RXOK_cal, RxPWDBAve;
492 //unsigned int FA_cnt_ofdm = priv->pshare->ofdm_FA_cnt1 + priv->pshare->ofdm_FA_cnt2 +
493 // priv->pshare->ofdm_FA_cnt3 + priv->pshare->ofdm_FA_cnt4;
494 //unsigned int FA_cnt_cck = priv->pshare->cck_FA_cnt;
495
496 if (!(priv->drv_state & DRV_STATE_OPEN))
497 return;
498
499 if (!(priv->pshare->rf_ft_var.mp_specific && priv->pshare->mp_dig_on))
500 return;
501
502 //printk("===> %s, pBandType = %d\n", __FUNCTION__, priv->pmib->dot11RFEntry.phyBandSelect);
503
504 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G) {
505
506 FA_statistic(priv);
507
508 priv->pshare->LastNumQryPhyStatusAll = priv->pshare->NumQryPhyStatus;
509 priv->pshare->NumQryPhyStatus = priv->pshare->NumQryPhyStatusCCK + priv->pshare->NumQryPhyStatusOFDM;
510 RXOK_cal = priv->pshare->NumQryPhyStatus - priv->pshare->LastNumQryPhyStatusAll;
511
512 if (RXOK_cal == 0)
513 RxPWDBAve = 0;
514 else
515 RxPWDBAve = priv->pshare->RxPWDBAve/RXOK_cal;
516
517 priv->pshare->RxPWDBAve= 0;
518
519 //printk("RX OK = %d\n", RXOK_cal);
520 //printk("RSSI = %d\n", RxPWDBAve);
521 //printk("DIG = (%x, %x), Cnt_all = %d, Cnt_Ofdm_fail = %d, Cnt_Cck_fail = %d\n", RTL_R8(0xc50), RTL_R8(0xc58), priv->pshare->FA_total_cnt, FA_cnt_ofdm, FA_cnt_cck);
522
523 if (RXOK_cal >= 70) {
524 if (RxPWDBAve <= 40) {
525 RTL_W8(0xc50, 0x1C);
526 RTL_W8(0xc58, 0x1C);
527 } else if (RxPWDBAve > 45) {
528 RTL_W8(0xc50, 0x20);
529 RTL_W8(0xc58, 0x20);
530 }
531 }
532 else {
533 RTL_W8(0xc50, 0x20);
534 RTL_W8(0xc58, 0x20);
535 }
536 }
537 rtk_mod_timer(&priv->pshare->MP_DIGTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(700));
538}
539
540static __always_inline void MP_DIG_timer_cb(unsigned long task_priv)
541{
542 MP_DIG_process(task_priv);
543}
544DEFINE_TIMER_CALLBACK(MP_DIG_timer_cb);
545
546void init_MP_DIGTimer(struct rtl8192cd_priv *priv)
547{
548 rtk_timer_setup(&(priv->pshare->MP_DIGTimer), MP_DIG_timer_cb, (unsigned long)priv, 0);
549}
550#endif // CONFIG_RTL_92D_SUPPORT
551
552void DIG_process(struct rtl8192cd_priv *priv)
553{
554 #define DEAD_POINT_TH 10000
555 #define DOWN_IG_HIT_TH 5
556 #define DEAD_POINT_HIT_TH 3
557
558 unsigned char value_IGI;
559 signed char value8;
560 unsigned int IGI_target;
561 unsigned char Adap_IGI_Upper;
562#ifdef INTERFERENCE_CONTROL
563 unsigned short thd0 = priv->pshare->threshold0;
564 unsigned short thd1 = priv->pshare->threshold1;
565 unsigned short thd2 = priv->pshare->threshold2;
566#endif
567
568 if (priv->pshare->DIG_on == 1)
569 {
570#ifdef HIGH_POWER_EXT_LNA
571 if (priv->pshare->rf_ft_var.use_ext_lna == 1) {
572// priv->pshare->FA_upper = 0x42;
573 priv->pshare->FA_upper = MIN_NUM(0x42, priv->pshare->rssi_min+36);
574 priv->pshare->FA_lower = 0x30;
575 } else
576#endif
577 {
578 // Reset initial gain upper & lower bounds
579#ifdef DFS
580 if (!priv->pmib->dot11DFSEntry.disable_DFS &&
581 (OPMODE & WIFI_AP_STATE) &&
582 (((priv->pmib->dot11RFEntry.dot11channel >= 52) &&
583 (priv->pmib->dot11RFEntry.dot11channel <= 64)) ||
584 ((priv->pmib->dot11RFEntry.dot11channel >= 100) &&
585 (priv->pmib->dot11RFEntry.dot11channel <= 144)))){
586 priv->pshare->FA_upper = 0x24;
587 priv->pshare->FA_lower = 0x20;
588 }
589 else
590#endif
591 {
592#ifdef INTERFERENCE_CONTROL
593 priv->pshare->FA_lower = 0x20;
594
595 if (priv->pshare->rssi_min != 0xFF)
596 {
597// priv->pshare->FA_upper = 0x3E;
598
599 if (priv->pshare->rssi_min > 30)
600 priv->pshare->FA_lower = 0x24;
601 else if (priv->pshare->rssi_min > 25)
602 priv->pshare->FA_lower = 0x22;
603
604 // limit upper bound to prevent the minimal signal sta from disconnect
605// if ((priv->pshare->rssi_min + 10) < priv->pshare->FA_upper)
606// priv->pshare->FA_upper = priv->pshare->rssi_min + 10;
607 priv->pshare->FA_upper = MIN_NUM(0x3E, priv->pshare->rssi_min+20);
608 }
609 else // before link
610 {
611 priv->pshare->FA_upper = 0x32;
612
613 thd0 = 500;
614 thd1 = 8000;
615 thd2 = 10000;
616 }
617#else
618 if (priv->pmib->dot11RFEntry.tx2path) {
619 if (priv->pmib->dot11BssType.net_work_type == WIRELESS_11B)
620 priv->pshare->FA_upper = MIN_NUM(0x2A, priv->pshare->rssi_min+10);
621 else
622 priv->pshare->FA_upper = MIN_NUM(0x3E, priv->pshare->rssi_min+10);
623 }
624 else
625 priv->pshare->FA_upper = MIN_NUM(0x3E, priv->pshare->rssi_min+10);
626 priv->pshare->FA_lower = 0x20;
627
628 if (priv->pshare->rssi_min > 30)
629 priv->pshare->FA_lower = 0x24;
630 else if (priv->pshare->rssi_min > 25)
631 priv->pshare->FA_lower = 0x22;
632#endif
633 }
634 }
635
636 // determine a new initial gain index according to the sumation of all FA counters as well as upper & lower bounds
637 value8 = RTL_R8(0xc50);
638#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
639 if (
640#ifdef CONFIG_RTL_92C_SUPPORT
641 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
642#endif
643#ifdef CONFIG_RTL_92D_SUPPORT
644#ifdef CONFIG_RTL_92C_SUPPORT
645 ||
646#endif
647 (GET_CHIP_VER(priv)==VERSION_8192D)
648#endif
649 ) {
650 if(priv->pshare->rf_ft_var.one_path_cca==2)
651 value8 = RTL_R8(0xc58);
652 }
653#endif
654
655 value_IGI = (value8 & 0x7F);
656
657#if defined(CONFIG_RTL_NOISE_CONTROL_92C)
658 if(priv->pshare->rf_ft_var.dnc_enable)
659 if ((GET_CHIP_VER(priv) == VERSION_8192C)||(GET_CHIP_VER(priv) == VERSION_8188C)){
660 unsigned long tp_now = (priv->ext_stats.tx_avarage+priv->ext_stats.rx_avarage)>>17;
661#ifdef HIGH_POWER_EXT_LNA
662 if(priv->pshare->rf_ft_var.use_ext_lna) {
663 if( (priv->pshare->rssi_min > 50) ) {
664 if((!priv->pshare->DNC_on) && (value_IGI >= priv->pshare->FA_upper) && (priv->pshare->FA_total_cnt > priv->pshare->threshold2)) {
665 priv->pshare->DNC_on = 1;
666 priv->ext_stats.tp_average_pre = tp_now;
667 priv->pshare->FA_lower = 0x20;
668 phy_set_bb_reg(priv, 0x870, bMaskDWord, RTL_R32(0x870)|BIT(5)|BIT(6)|BIT(21)|BIT(22));
669
670#ifdef HW_ANT_SWITCH
671 phy_set_bb_reg(priv, 0xc50, bMaskByte0, priv->pshare->FA_lower | RXDVY_A_EN);
672 phy_set_bb_reg(priv, 0xc58, bMaskByte0, priv->pshare->FA_lower | RXDVY_B_EN);
673#else
674 phy_set_bb_reg(priv, 0xc50, bMaskByte0, priv->pshare->FA_lower);
675 phy_set_bb_reg(priv, 0xc58, bMaskByte0, priv->pshare->FA_lower);
676#endif
677
678 } else if(priv->pshare->DNC_on ==1) {
679 if(tp_now < priv->ext_stats.tp_average_pre + 2) {
680 priv->pshare->DNC_on = 0;
681 }
682 else {
683 priv->pshare->DNC_on =2;
684 priv->ext_stats.tp_average_pre = tp_now;
685 }
686 } else if(priv->pshare->DNC_on >= 2 ) {
687 if(( tp_now+10 < priv->ext_stats.tp_average_pre ) || (tp_now < 1) ) {
688 priv->pshare->DNC_on = 0;
689 } else if(priv->pshare->DNC_on<5) {
690 priv->ext_stats.tp_average_pre = tp_now;
691 ++priv->pshare->DNC_on;
692 }
693 }
694 }else {
695 priv->pshare->DNC_on = 0;
696 }
697
698 if( priv->pshare->DNC_on )
699 return;
700 else
701 phy_set_bb_reg(priv, 0x870, bMaskDWord, RTL_R32(0x870)& ~(BIT(5)|BIT(6)|BIT(21)|BIT(22)));
702
703 } else
704#endif
705 {
706 if( (priv->pshare->rssi_min > 40) && (value_IGI >= priv->pshare->FA_upper) ) {
707// unsigned long tp_now = (priv->ext_stats.tx_avarage+priv->ext_stats.rx_avarage)>>17;
708 if((!priv->pshare->DNC_on) && (priv->pshare->FA_total_cnt > priv->pshare->threshold2)) {
709 priv->pshare->DNC_on = 1;
710 priv->ext_stats.tp_average_pre = tp_now;
711 } else if(priv->pshare->DNC_on ==1) {
712 if(tp_now < priv->ext_stats.tp_average_pre + 2) {
713 priv->pshare->DNC_on = 0;
714 }
715 else {
716 priv->pshare->DNC_on = 2;
717 priv->ext_stats.tp_average_pre = tp_now;
718 }
719 } else if(priv->pshare->DNC_on >= 2 ) {
720 if((tp_now +10 < priv->ext_stats.tp_average_pre )
721 || ((priv->ext_stats.tp_average_pre < 10) && (priv->pshare->FA_total_cnt < priv->pshare->threshold1))) {
722 priv->pshare->DNC_on = 0;
723 } else if(priv->pshare->DNC_on<6) {
724 priv->ext_stats.tp_average_pre = tp_now;
725 ++priv->pshare->DNC_on;
726 }
727 }
728 if(priv->pshare->DNC_on) {
729 priv->pshare->FA_upper = 0x3e;
730 }
731 }else {
732 priv->pshare->DNC_on = 0;
733 }
734 }
735 }
736#endif
737
738 if ((priv->pshare->digDeadPoint == 0) && (priv->pshare->FA_total_cnt > DEAD_POINT_TH)) {
739 if ((priv->pshare->digDeadPointHitCount > 0) && (priv->pshare->digDeadPointCandidate == value_IGI)) {
740 priv->pshare->digDeadPointHitCount++;
741 if (priv->pshare->digDeadPointHitCount == DEAD_POINT_HIT_TH) {
742 priv->pshare->digDeadPoint = priv->pshare->digDeadPointCandidate;
743 }
744 } else {
745 priv->pshare->digDeadPointCandidate = value_IGI;
746 priv->pshare->digDeadPointHitCount = 1;
747 }
748 }
749
750#ifdef INTERFERENCE_CONTROL
751 if (priv->pshare->FA_total_cnt < thd0) {
752#else
753 if (priv->pshare->FA_total_cnt < priv->pshare->threshold0) {
754#endif
755 priv->pshare->digDownCount++;
756 if (priv->pshare->digDownCount > DOWN_IG_HIT_TH) {
757 // Reset deadpoint hit count
758 if ((priv->pshare->digDeadPoint == 0) && (priv->pshare->digDeadPointHitCount > 0) && (value_IGI == priv->pshare->digDeadPointCandidate))
759 priv->pshare->digDeadPointHitCount = 0;
760
761 value_IGI--;
762
763 // Check if the new value is dead point
764 if ((priv->pshare->digDeadPoint > 0) && (value_IGI == priv->pshare->digDeadPoint))
765 value_IGI++;
766 }
767#ifdef INTERFERENCE_CONTROL
768 } else if (priv->pshare->FA_total_cnt < thd1) {
769#else
770 } else if (priv->pshare->FA_total_cnt < priv->pshare->threshold1) {
771#endif
772 value_IGI += 0;
773 priv->pshare->digDownCount = 0;
774#ifdef INTERFERENCE_CONTROL
775 } else if (priv->pshare->FA_total_cnt < thd2) {
776#else
777 } else if (priv->pshare->FA_total_cnt < priv->pshare->threshold2) {
778#endif
779 value_IGI++;
780 priv->pshare->digDownCount = 0;
781#ifdef INTERFERENCE_CONTROL
782 } else if (priv->pshare->FA_total_cnt >= thd2) {
783#else
784 } else if (priv->pshare->FA_total_cnt >= priv->pshare->threshold2) {
785#endif
786 value_IGI += 2;
787 priv->pshare->digDownCount = 0;
788 } else {
789 priv->pshare->digDownCount = 0;
790 }
791
792 if (value_IGI > priv->pshare->FA_upper)
793 value_IGI = priv->pshare->FA_upper;
794 else if (value_IGI < priv->pshare->FA_lower)
795 value_IGI = priv->pshare->FA_lower;
796 if (priv->pshare->rf_ft_var.adaptivity_enable && priv->pshare->rf_ft_var.adaptivity_flag == TRUE)
797 {
798 Adap_IGI_Upper = priv->pshare->rf_ft_var.adaptivity_igi_upper;
799 if(value_IGI > Adap_IGI_Upper)
800 value_IGI = Adap_IGI_Upper;
801 }
802
803#if defined(HW_ANT_SWITCH)
804 // wirte new initial gain index into regC50/C58
805 if (priv->pshare->rf_ft_var.one_path_cca == 0) {
806 if (!(ODMPTR->support_ability & ODM_BB_ANT_DIV))
807 RTL_W8(0xc50, value_IGI);
808 else
809 RTL_W8(0xc50, value_IGI | RXDVY_A_EN);
810#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
811 if (
812#ifdef CONFIG_RTL_92C_SUPPORT
813 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
814#endif
815#ifdef CONFIG_RTL_92D_SUPPORT
816#ifdef CONFIG_RTL_92C_SUPPORT
817 ||
818#endif
819 (GET_CHIP_VER(priv)==VERSION_8192D)
820#endif
821 )
822 RTL_W8(0xc58, value_IGI | RXDVY_B_EN);
823#endif
824 } else if (priv->pshare->rf_ft_var.one_path_cca == 1) {
825 if (!(ODMPTR->support_ability & ODM_BB_ANT_DIV))
826 RTL_W8(0xc50, value_IGI);
827 else
828 RTL_W8(0xc50, value_IGI | RXDVY_A_EN);
829#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
830 if (
831#ifdef CONFIG_RTL_92C_SUPPORT
832 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
833#endif
834#ifdef CONFIG_RTL_92D_SUPPORT
835#ifdef CONFIG_RTL_92C_SUPPORT
836 ||
837#endif
838 (GET_CHIP_VER(priv)==VERSION_8192D)
839#endif
840 )
841 RTL_W8(0xc58, getIGIFor1RCCA(value_IGI) | RXDVY_B_EN);
842#endif
843 } else if (priv->pshare->rf_ft_var.one_path_cca == 2) {
844 if (!(ODMPTR->support_ability & ODM_BB_ANT_DIV))
845 RTL_W8(0xc50, value_IGI);
846 else
847 RTL_W8(0xc50, getIGIFor1RCCA(value_IGI) | RXDVY_A_EN);
848#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
849 if (
850#ifdef CONFIG_RTL_92C_SUPPORT
851 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
852#endif
853#ifdef CONFIG_RTL_92D_SUPPORT
854#ifdef CONFIG_RTL_92C_SUPPORT
855 ||
856#endif
857 (GET_CHIP_VER(priv)==VERSION_8192D)
858#endif
859 )
860 RTL_W8(0xc58, value_IGI| RXDVY_B_EN);
861#endif
862 }
863#else
864 // Write IGI into HW
865 if (priv->pshare->rf_ft_var.one_path_cca == 0) {
866 RTL_W8(0xc50, value_IGI);
867#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
868 if (
869#ifdef CONFIG_RTL_92C_SUPPORT
870 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
871#endif
872#ifdef CONFIG_RTL_92D_SUPPORT
873#ifdef CONFIG_RTL_92C_SUPPORT
874 ||
875#endif
876 (GET_CHIP_VER(priv)==VERSION_8192D)
877#endif
878 )
879 RTL_W8(0xc58, value_IGI);
880#endif
881 } else if (priv->pshare->rf_ft_var.one_path_cca == 1) {
882 RTL_W8(0xc50, value_IGI);
883#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
884 if (
885#ifdef CONFIG_RTL_92C_SUPPORT
886 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
887#endif
888#ifdef CONFIG_RTL_92D_SUPPORT
889#ifdef CONFIG_RTL_92C_SUPPORT
890 ||
891#endif
892 (GET_CHIP_VER(priv)==VERSION_8192D)
893#endif
894 )
895 RTL_W8(0xc58, getIGIFor1RCCA(value_IGI));
896#endif
897 } else if (priv->pshare->rf_ft_var.one_path_cca == 2) {
898 RTL_W8(0xc50, getIGIFor1RCCA(value_IGI));
899#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
900 if (
901#ifdef CONFIG_RTL_92C_SUPPORT
902 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
903#endif
904#ifdef CONFIG_RTL_92D_SUPPORT
905#ifdef CONFIG_RTL_92C_SUPPORT
906 ||
907#endif
908 (GET_CHIP_VER(priv)==VERSION_8192D)
909#endif
910 )
911 RTL_W8(0xc58, value_IGI);
912#endif
913 }
914#endif
915
916 }
917}
918
919#if 0
920void check_DIG_by_rssi(struct rtl8192cd_priv *priv, unsigned char rssi_strength)
921{
922 unsigned int dig_on = 0;
923
924 if (OPMODE & WIFI_SITE_MONITOR)
925 return;
926
927 if ((rssi_strength > priv->pshare->rf_ft_var.digGoUpperLevel)
928 && (rssi_strength < HP_LOWER+1) && (priv->pshare->phw->signal_strength != 2)) {
929#ifndef CONFIG_RTL_92D_SUPPORT
930 if (priv->pshare->is_40m_bw)
931 // RTL_W8(0xc87, (RTL_R8(0xc87) & 0xf) | 0x30); 92D
932 RTL_W8(0xc87, 0x30);
933 else
934 RTL_W8(0xc30, 0x44);
935#endif
936
937 if (priv->pshare->phw->signal_strength != 3)
938 dig_on++;
939
940 priv->pshare->phw->signal_strength = 2;
941 }
942 else if ((rssi_strength > HP_LOWER+5) && (priv->pshare->phw->signal_strength != 3)) {
943#ifndef CONFIG_RTL_92D_SUPPORT
944 if (priv->pshare->is_40m_bw)
945 // RTL_W8(0xc87, (RTL_R8(0xc87) & 0xf) | 0x30); 92D
946 RTL_W8(0xc87, 0x30);
947 else
948 RTL_W8(0xc30, 0x44);
949#endif
950
951 if (priv->pshare->phw->signal_strength != 2)
952 dig_on++;
953
954 priv->pshare->phw->signal_strength = 3;
955 }
956 else if (((rssi_strength < priv->pshare->rf_ft_var.digGoLowerLevel)
957 && (priv->pshare->phw->signal_strength != 1)) || !priv->pshare->phw->signal_strength) {
958 // DIG off
959// set_DIG_state(priv, 0);
960
961#ifndef CONFIG_RTL_92D_SUPPORT
962 if (priv->pshare->is_40m_bw)
963 //RTL_W8(0xc87, (RTL_R8(0xc87) & 0xf) | 0x30); 92D
964 RTL_W8(0xc87, 0x30);
965 else
966 RTL_W8(0xc30, 0x44);
967#endif
968
969 priv->pshare->phw->signal_strength = 1;
970 }
971
972 if (dig_on) {
973 // DIG on
974 set_DIG_state(priv, 1);
975 }
976
977 //check_DC_TH_by_rssi(priv, rssi_strength);
978}
979
980
981void DIG_for_site_survey(struct rtl8192cd_priv *priv, int do_ss)
982{
983 if (do_ss) {
984 // DIG off
985 set_DIG_state(priv, 0);
986 }
987 else {
988 // DIG on
989#ifndef INTERFERENCE_CONTROL
990 if (priv->pshare->phw->signal_strength > 1)
991#endif
992 {
993 set_DIG_state(priv, 1);
994 }
995 }
996}
997#endif
998
999#if 0
1000//#ifdef INTERFERENCE_CONTROL
1001void check_NBI_by_rssi(struct rtl8192cd_priv *priv, unsigned char rssi_strength)
1002{
1003 if (OPMODE & WIFI_SITE_MONITOR)
1004 return;
1005
1006 if (priv->pshare->phw->nbi_filter_on) {
1007 if (rssi_strength < 20) {
1008 priv->pshare->phw->nbi_filter_on = 0;
1009 RTL_W16(rOFDM0_RxDSP, RTL_R16(rOFDM0_RxDSP) & ~ BIT(9)); // NBI off
1010 }
1011 } else { // NBI OFF previous
1012 if (rssi_strength > 25) {
1013 priv->pshare->phw->nbi_filter_on = 1;
1014 RTL_W16(rOFDM0_RxDSP, RTL_R16(rOFDM0_RxDSP) | BIT(9)); // NBI on
1015 }
1016 }
1017}
1018#endif
1019
1020/*
1021 * dynamic CCK CCA enhance by rssi
1022 */
1023void CCK_CCA_dynamic_enhance(struct rtl8192cd_priv *priv, unsigned char rssi_strength)
1024{
1025#if 1
1026 unsigned int cck_fa = priv->pshare->FA_total_cnt;
1027 int rssi_thd = 30;
1028
1029 if (rssi_strength == 0xff) {
1030 if (cck_fa < 1000) {
1031 if (priv->pshare->phw->CCK_CCA_enhanced != 2) {
1032 RTL_W8(0xa0a, 0x40);
1033 priv->pshare->phw->CCK_CCA_enhanced = 2;
1034 }
1035 } else {
1036 if (priv->pshare->phw->CCK_CCA_enhanced != 1) {
1037 RTL_W8(0xa0a, 0x83);
1038 priv->pshare->phw->CCK_CCA_enhanced = 1;
1039 }
1040 }
1041 return;
1042 }
1043
1044 if (rssi_strength > rssi_thd+5) {
1045 if (priv->pshare->phw->CCK_CCA_enhanced != 0) {
1046 RTL_W8(0xa0a, 0xcd);
1047 priv->pshare->phw->CCK_CCA_enhanced = 0;
1048 }
1049 } else if (rssi_strength< rssi_thd) {
1050 if ((rssi_strength > 9) || (priv->assoc_num >1)) {
1051 if (priv->pshare->phw->CCK_CCA_enhanced != 1) {
1052 RTL_W8(0xa0a, 0x83);
1053 priv->pshare->phw->CCK_CCA_enhanced = 1;
1054 }
1055 } else {
1056 if(cck_fa<1000) {
1057 if (priv->pshare->phw->CCK_CCA_enhanced != 2) {
1058 RTL_W8(0xa0a, 0x40);
1059 priv->pshare->phw->CCK_CCA_enhanced = 2;
1060 }
1061 } else {
1062 if (priv->pshare->phw->CCK_CCA_enhanced != 1) {
1063 RTL_W8(0xa0a, 0x83);
1064 priv->pshare->phw->CCK_CCA_enhanced = 1;
1065 }
1066 }
1067 }
1068 }
1069
1070#else
1071
1072 if (rssi_strength == 0xff)
1073 return;
1074
1075 if (!priv->pshare->phw->CCK_CCA_enhanced && (rssi_strength < 30)) {
1076 priv->pshare->phw->CCK_CCA_enhanced = TRUE;
1077 RTL_W8(0xa0a, 0x83);
1078 }
1079 else if (priv->pshare->phw->CCK_CCA_enhanced && (rssi_strength > 35)) {
1080 priv->pshare->phw->CCK_CCA_enhanced = FALSE;
1081 RTL_W8(0xa0a, 0xcd);
1082 }
1083#endif
1084}
1085
1086
1087//3 ============================================================
1088//3 Dynamic Tx Power / Power Tracking
1089//3 ============================================================
1090
1091#ifdef HIGH_POWER_EXT_PA
1092void tx_power_control(struct rtl8192cd_priv *priv)
1093{
1094#ifndef SMP_SYNC
1095 unsigned long x;
1096#endif
1097
1098 int pwr_value = 0x10101010;
1099 if( priv->pshare->phw->signal_strength == 3 && priv->pshare->phw->lower_tx_power== 0) {
1100 SAVE_INT_AND_CLI(x);
1101 priv->pshare->phw->power_backup[0x00] = RTL_R32(REG_TX_AGC_A_RATE18_06);
1102 priv->pshare->phw->power_backup[0x01] = RTL_R32(REG_TX_AGC_A_RATE54_24);
1103 priv->pshare->phw->power_backup[0x02] = RTL_R32(REG_TX_AGC_B_RATE18_06);
1104 priv->pshare->phw->power_backup[0x03] = RTL_R32(REG_TX_AGC_B_RATE54_24);
1105 priv->pshare->phw->power_backup[0x04] = RTL_R32(REG_TX_AGC_A_MCS03_MCS00);
1106 priv->pshare->phw->power_backup[0x05] = RTL_R32(REG_TX_AGC_A_MCS07_MCS04);
1107 priv->pshare->phw->power_backup[0x06] = RTL_R32(REG_TX_AGC_A_MCS11_MCS08);
1108 priv->pshare->phw->power_backup[0x07] = RTL_R32(REG_TX_AGC_A_MCS15_MCS12);
1109 priv->pshare->phw->power_backup[0x08] = RTL_R32(REG_TX_AGC_B_MCS03_MCS00);
1110 priv->pshare->phw->power_backup[0x09] = RTL_R32(REG_TX_AGC_B_MCS07_MCS04);
1111 priv->pshare->phw->power_backup[0x0a] = RTL_R32(REG_TX_AGC_B_MCS11_MCS08);
1112 priv->pshare->phw->power_backup[0x0b] = RTL_R32(REG_TX_AGC_B_MCS15_MCS12);
1113 priv->pshare->phw->power_backup[0x0c] = RTL_R32(rTxAGC_A_CCK11_2_B_CCK11);
1114 priv->pshare->phw->power_backup[0x0d] = RTL_R32(REG_TX_AGC_A_CCK_1_MCS32);
1115 priv->pshare->phw->power_backup[0x0e] = RTL_R32(rTxAGC_B_CCK5_1_Mcs32);
1116 RTL_W32(REG_TX_AGC_A_RATE18_06, pwr_value);
1117 RTL_W32(REG_TX_AGC_A_RATE54_24, pwr_value);
1118 RTL_W32(REG_TX_AGC_B_RATE18_06, pwr_value);
1119 RTL_W32(REG_TX_AGC_B_RATE54_24, pwr_value);
1120 RTL_W32(REG_TX_AGC_A_MCS03_MCS00, pwr_value);
1121 RTL_W32(REG_TX_AGC_A_MCS07_MCS04, pwr_value);
1122 RTL_W32(REG_TX_AGC_A_MCS11_MCS08, pwr_value);
1123 RTL_W32(REG_TX_AGC_A_MCS15_MCS12, pwr_value);
1124 RTL_W32(REG_TX_AGC_B_MCS03_MCS00, pwr_value);
1125 RTL_W32(REG_TX_AGC_B_MCS07_MCS04, pwr_value);
1126 RTL_W32(REG_TX_AGC_B_MCS11_MCS08, pwr_value);
1127 RTL_W32(REG_TX_AGC_B_MCS15_MCS12, pwr_value);
1128 RTL_W32(rTxAGC_A_CCK11_2_B_CCK11, pwr_value);
1129 RTL_W32(REG_TX_AGC_A_CCK_1_MCS32, (pwr_value & 0x0000ff00) | (priv->pshare->phw->power_backup[0x0d] &0xffff00ff));
1130 RTL_W32(rTxAGC_B_CCK5_1_Mcs32, (pwr_value & 0xffffff00) | (priv->pshare->phw->power_backup[0x0e] &0x000000ff));
1131 priv->pshare->phw->lower_tx_power = 1;
1132 RESTORE_INT(x);
1133 }
1134 else if( priv->pshare->phw->signal_strength != 3 && priv->pshare->phw->lower_tx_power) {
1135 SAVE_INT_AND_CLI(x);
1136 RTL_W32(REG_TX_AGC_A_RATE18_06, priv->pshare->phw->power_backup[0x00]);
1137 RTL_W32(REG_TX_AGC_A_RATE54_24, priv->pshare->phw->power_backup[0x01]);
1138 RTL_W32(REG_TX_AGC_B_RATE18_06, priv->pshare->phw->power_backup[0x02]);
1139 RTL_W32(REG_TX_AGC_B_RATE54_24, priv->pshare->phw->power_backup[0x03]);
1140 RTL_W32(REG_TX_AGC_A_MCS03_MCS00, priv->pshare->phw->power_backup[0x04]);
1141 RTL_W32(REG_TX_AGC_A_MCS07_MCS04, priv->pshare->phw->power_backup[0x05]);
1142 RTL_W32(REG_TX_AGC_A_MCS11_MCS08, priv->pshare->phw->power_backup[0x06]);
1143 RTL_W32(REG_TX_AGC_A_MCS15_MCS12, priv->pshare->phw->power_backup[0x07]);
1144 RTL_W32(REG_TX_AGC_B_MCS03_MCS00, priv->pshare->phw->power_backup[0x08]);
1145 RTL_W32(REG_TX_AGC_B_MCS07_MCS04, priv->pshare->phw->power_backup[0x09]);
1146 RTL_W32(REG_TX_AGC_B_MCS11_MCS08, priv->pshare->phw->power_backup[0x0a]);
1147 RTL_W32(REG_TX_AGC_B_MCS15_MCS12, priv->pshare->phw->power_backup[0x0b]);
1148 RTL_W32(rTxAGC_A_CCK11_2_B_CCK11, priv->pshare->phw->power_backup[0x0c]);
1149 RTL_W32(REG_TX_AGC_A_CCK_1_MCS32, priv->pshare->phw->power_backup[0x0d]);
1150 RTL_W32(rTxAGC_B_CCK5_1_Mcs32, priv->pshare->phw->power_backup[0x0e]);
1151 priv->pshare->phw->lower_tx_power = 0;
1152 RESTORE_INT(x);
1153 }
1154}
1155#endif
1156
1157#if 0
1158int get_CCK_swing_index(struct rtl8192cd_priv *priv)
1159{
1160 int TempCCk, index=12, i;
1161 short channel;
1162#ifdef MP_TEST
1163 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific)
1164 channel=priv->pshare->working_channel;
1165 else
1166#endif
1167 channel = (priv->pmib->dot11RFEntry.dot11channel);
1168
1169 //Query CCK default setting From 0xa24
1170 TempCCk = phy_query_bb_reg(priv, rCCK0_TxFilter2, bMaskDWord)&bMaskCCK;
1171 TempCCk = cpu_to_le32(TempCCk);
1172 for(i=0 ; i<CCK_TABLE_SIZE ; i++) {
1173 if(channel==14) {
1174 if(memcmp((void*)&TempCCk, (void*)&cck_swing_table_ch14[i][2], 4)==0) {
1175 index = i;
1176 break;
1177 }
1178 } else {
1179 if(memcmp((void*)&TempCCk, (void*)&cck_swing_table_ch1_ch13[i][2], 4)==0) {
1180 index = i;
1181 break;
1182 }
1183 }
1184 }
1185 DEBUG_INFO("Initial reg0x%x = 0x%lx, CCK_index=0x%x, ch %d\n",
1186 rCCK0_TxFilter2, TempCCk, index, channel);
1187 return index;
1188}
1189
1190
1191void set_CCK_swing_index(struct rtl8192cd_priv *priv, short CCK_index)
1192{
1193 short channel;
1194#ifdef MP_TEST
1195 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific)
1196 channel=priv->pshare->working_channel;
1197 else
1198#endif
1199 channel = (priv->pmib->dot11RFEntry.dot11channel);
1200
1201
1202#ifdef CONFIG_RTL_88E_SUPPORT /*for 88e tx power tracking*/
1203 if(GET_CHIP_VER(priv) == VERSION_8188E){
1204 if(channel !=14) {
1205 RTL_W8( 0xa22, 0x1c);
1206 RTL_W8( 0xa23, 0x1a);
1207 RTL_W8( 0xa24, 0x18);
1208 RTL_W8( 0xa25, 0x12);
1209 RTL_W8( 0xa26, 0xe);
1210 RTL_W8( 0xa27, 0x8);
1211 RTL_W8( 0xa28, 0x4);
1212 RTL_W8( 0xa29, 0x2);
1213 }
1214 else{
1215 RTL_W8( 0xa22, 0x1c);
1216 RTL_W8( 0xa23, 0x1a);
1217 RTL_W8( 0xa24, 0x18);
1218 RTL_W8( 0xa25, 0x12);
1219 RTL_W8( 0xa26, 0x0);
1220 RTL_W8( 0xa27, 0x0);
1221 RTL_W8( 0xa28, 0x0);
1222 RTL_W8( 0xa29, 0x0);
1223 }
1224 }
1225 else
1226#endif
1227 if(channel !=14) {
1228 RTL_W8( 0xa22, cck_swing_table_ch1_ch13[CCK_index][0]);
1229 RTL_W8( 0xa23, cck_swing_table_ch1_ch13[CCK_index][1]);
1230 RTL_W8( 0xa24, cck_swing_table_ch1_ch13[CCK_index][2]);
1231 RTL_W8( 0xa25, cck_swing_table_ch1_ch13[CCK_index][3]);
1232 RTL_W8( 0xa26, cck_swing_table_ch1_ch13[CCK_index][4]);
1233 RTL_W8( 0xa27, cck_swing_table_ch1_ch13[CCK_index][5]);
1234 RTL_W8( 0xa28, cck_swing_table_ch1_ch13[CCK_index][6]);
1235 RTL_W8( 0xa29, cck_swing_table_ch1_ch13[CCK_index][7]);
1236 }
1237 else{
1238 RTL_W8( 0xa22, cck_swing_table_ch14[CCK_index][0]);
1239 RTL_W8( 0xa23, cck_swing_table_ch14[CCK_index][1]);
1240 RTL_W8( 0xa24, cck_swing_table_ch14[CCK_index][2]);
1241 RTL_W8( 0xa25, cck_swing_table_ch14[CCK_index][3]);
1242 RTL_W8( 0xa26, cck_swing_table_ch14[CCK_index][4]);
1243 RTL_W8( 0xa27, cck_swing_table_ch14[CCK_index][5]);
1244 RTL_W8( 0xa28, cck_swing_table_ch14[CCK_index][6]);
1245 RTL_W8( 0xa29, cck_swing_table_ch14[CCK_index][7]);
1246 }
1247}
1248#endif
1249
1250unsigned char getThermalValue(struct rtl8192cd_priv *priv)
1251{
1252 unsigned char ThermalValue;
1253 int sum=0, i=0;
1254 phy_set_rf_reg(priv, RF_PATH_A, RF_T_METER, bMask20Bits, 0x60);
1255 while ((phy_query_rf_reg(priv, RF_PATH_A, RF_T_METER, bMask20Bits, 1) > 0x1f) && ((i++) < 1000)) {//<20ms, test is in 20 us
1256 delay_us(20);
1257 }
1258 ThermalValue =(unsigned char)phy_query_rf_reg(priv, RF_PATH_A, RF_T_METER, bMask20Bits, 1) & 0x01f;
1259 priv->pshare->Thermal_idx = (priv->pshare->Thermal_idx+1)%8;
1260 priv->pshare->Thermal_log[ priv->pshare->Thermal_idx ] = ThermalValue;
1261 for(i=0; i<8; i++) {
1262 if(!priv->pshare->Thermal_log[i])
1263 return ThermalValue;
1264 sum += priv->pshare->Thermal_log[i];
1265 }
1266 return (sum+4)>>3;
1267}
1268
1269
1270#ifdef CONFIG_RTL_92C_SUPPORT
1271
1272#ifdef HIGH_POWER_EXT_PA
1273void swingIndexRemap2(int *a, int b, int i)
1274{
1275
1276 u8 index_mapping_HighPower_92C[4][15] = {
1277 {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 18, 18}, //2.4G, path A/MAC 0, decrease power
1278 {0, 2, 4, 5, 7, 8, 10, 11, 13, 14, 16, 17, 19, 20, 22}, //2.4G, path A/MAC 0, increase power
1279 {0, 1, 3, 4, 6, 7, 9, 10, 12, 13, 15, 16, 18, 18, 18}, //2.4G, path A/MAC 0, decrease power
1280 {0, 3, 5, 6, 8, 9, 11, 12, 14, 15, 17, 18, 20, 21, 23}, //2.4G, path A/MAC 0, increase power
1281 };
1282
1283 int d = RTL_ABS(*a, b);
1284 int offset = 0;
1285
1286
1287 if(i == 0)
1288 offset = 1;
1289 else
1290 offset = 3;
1291
1292 if(*a < b )
1293 {
1294 //printk("\n\n Increase Power !! \n\n");
1295 *a = b - index_mapping_HighPower_92C[offset][d];
1296 }
1297 else
1298 {
1299 //printk("\n\n Decrease Power !! \n\n");
1300 offset = offset - 1;
1301 *a = b + index_mapping_HighPower_92C[offset][d];
1302 }
1303
1304 //printk("\n\ a = %d, b = %d, offset = %d, d = %d, diff = %d \n\n",
1305 //*a, b, offset, d, index_mapping_HighPower_92C[offset][d]);
1306
1307}
1308void swingIndexRemap(int *a, int b)
1309{
1310 int d = (RTL_ABS(*a, b) *3)>>1;
1311 if(*a < b )
1312 *a = b - d;
1313 else
1314 *a = b + d;
1315}
1316#endif
1317
1318void tx_power_tracking(struct rtl8192cd_priv *priv)
1319{
1320 unsigned char ThermalValue = 0, delta, delta_LCK, delta_IQK;
1321 int ele_A, ele_D, value32, X, Y, ele_C;
1322 int OFDM_index[2]={0,0}, CCK_index;
1323 int i = 0;
1324 char is2T = ((GET_CHIP_VER(priv) == VERSION_8192C) ?1 :0);
1325 unsigned char TxPwrLevel[2];
1326 unsigned char channel, OFDM_min_index = 6, rf=1; //OFDM BB Swing should be less than +3.0dB, which is required by Arthur
1327#ifdef POWER_PERCENT_ADJUSTMENT
1328 signed char pwrdiff_percent;
1329#endif
1330#ifdef MP_TEST
1331 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific) {
1332 channel=priv->pshare->working_channel;
1333 if(priv->pshare->mp_txpwr_tracking == FALSE)
1334 return;
1335 } else
1336#endif
1337 {
1338 channel = (priv->pmib->dot11RFEntry.dot11channel);
1339 }
1340
1341 ThermalValue = getThermalValue(priv);
1342
1343 rf += is2T;
1344 if(ThermalValue) {
1345
1346 if(!priv->pshare->thermal_value) {
1347 priv->pshare->thermal_value = priv->pmib->dot11RFEntry.ther;
1348 priv->pshare->thermal_value_lck = ThermalValue;
1349 priv->pshare->thermal_value_iqk = ThermalValue;
1350
1351 //Query OFDM path A default setting
1352 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
1353 for(i=0; i<OFDM_TABLE_SIZE; i++) {
1354 if(ele_D == (ofdm_swing_table[i]&bMaskOFDM_D)) {
1355 priv->pshare->OFDM_index[0] = i;
1356 priv->pshare->OFDM_index0[0] = i;
1357 break;
1358 }
1359 }
1360
1361 //Query OFDM path B default setting
1362 if(is2T) {
1363 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
1364 for(i=0; i<OFDM_TABLE_SIZE; i++) {
1365 if(ele_D == (ofdm_swing_table[i]&bMaskOFDM_D)) {
1366 priv->pshare->OFDM_index[1] = i;
1367 priv->pshare->OFDM_index0[1] = i;
1368 break;
1369 }
1370 }
1371 }
1372 priv->pshare->CCK_index = get_CCK_swing_index(priv);
1373 priv->pshare->CCK_index0 = priv->pshare->CCK_index;
1374
1375 }
1376
1377 delta = RTL_ABS(ThermalValue, priv->pshare->thermal_value);
1378 delta_LCK = RTL_ABS(ThermalValue, priv->pshare->thermal_value_lck);
1379 delta_IQK = RTL_ABS(ThermalValue, priv->pshare->thermal_value_iqk);
1380
1381// printk("Readback Thermal Meter = 0x%lx pre thermal meter 0x%lx EEPROMthermalmeter 0x%lx delta 0x%lx delta_LCK 0x%lx delta_IQK 0x%lx\n",
1382// ThermalValue, priv->pshare->thermal_value, priv->pmib->dot11RFEntry.ther, delta, delta_LCK, delta_IQK);
1383
1384 if(delta_LCK > 1) {
1385 priv->pshare->thermal_value_lck = ThermalValue;
1386#ifdef MP_TEST
1387 if(priv->pshare->rf_ft_var.mp_specific)
1388 {
1389 if((OPMODE & WIFI_MP_CTX_BACKGROUND) && !(OPMODE & WIFI_MP_CTX_PACKET))
1390 printk("NOT do LCK during ctx !!!! \n");
1391 else
1392 phy_lc_calibrate(priv);
1393 }
1394 else
1395#endif
1396 phy_lc_calibrate(priv);
1397 }
1398
1399 if(delta > 0) {
1400 if(ThermalValue > priv->pshare->thermal_value) {
1401 for(i = 0; i < rf; i++)
1402 priv->pshare->OFDM_index[i] -= delta;
1403 priv->pshare->CCK_index -= delta;
1404 } else {
1405 for(i = 0; i < rf; i++)
1406 priv->pshare->OFDM_index[i] += delta;
1407 priv->pshare->CCK_index += delta;
1408 }
1409 if(ThermalValue > priv->pmib->dot11RFEntry.ther) {
1410 for(i = 0; i < rf; i++)
1411 OFDM_index[i] = priv->pshare->OFDM_index[i]+1;
1412 CCK_index = priv->pshare->CCK_index+1;
1413 } else {
1414 for(i = 0; i < rf; i++)
1415 OFDM_index[i] = priv->pshare->OFDM_index[i];
1416 CCK_index = priv->pshare->CCK_index;
1417 }
1418#ifdef MP_TEST
1419 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific) {
1420 TxPwrLevel[0] = priv->pshare->mp_txpwr_patha;
1421 TxPwrLevel[1] = priv->pshare->mp_txpwr_pathb;
1422 } else
1423#endif
1424 {
1425 TxPwrLevel[0] = priv->pmib->dot11RFEntry.pwrlevelHT40_1S_A[channel-1];
1426 TxPwrLevel[1] = priv->pmib->dot11RFEntry.pwrlevelHT40_1S_B[channel-1];
1427
1428 if (priv->pshare->CurrentChannelBW == CHANNEL_WIDTH_20) {
1429 unsigned char offset = (priv->pmib->dot11RFEntry.pwrdiffHT20[channel-1] & 0x0f);
1430 TxPwrLevel[0] = COUNT_SIGN_OFFSET(TxPwrLevel[0], offset);
1431 offset = ((priv->pmib->dot11RFEntry.pwrdiffOFDM[channel-1] & 0xf0) >> 4);
1432 TxPwrLevel[1] = COUNT_SIGN_OFFSET(TxPwrLevel[1], offset);
1433 }
1434#ifdef POWER_PERCENT_ADJUSTMENT
1435 pwrdiff_percent = PwrPercent2PwrLevel(priv->pmib->dot11RFEntry.power_percent);
1436 TxPwrLevel[0] = POWER_RANGE_CHECK(TxPwrLevel[0]+pwrdiff_percent);
1437 TxPwrLevel[1] = POWER_RANGE_CHECK(TxPwrLevel[1]+pwrdiff_percent);
1438#endif
1439 }
1440
1441// printk("TxPwrLevel[0]=%d, TxPwrLevel[1]=%d\n", TxPwrLevel[0], TxPwrLevel[1]);
1442 for(i = 0; i < rf; i++) {
1443 if(/*TxPwrLevel[i] >=0 &&*/ TxPwrLevel[i] <=26) {
1444 if(ThermalValue > priv->pmib->dot11RFEntry.ther) {
1445 if (delta < 5)
1446 OFDM_index[i] -= 1;
1447 else
1448 OFDM_index[i] -= 2;
1449 } else if(delta > 5 && ThermalValue < priv->pmib->dot11RFEntry.ther) {
1450 OFDM_index[i] += 1;
1451 }
1452 } else if (TxPwrLevel[i] >= 27 && TxPwrLevel[i] <= 32 && ThermalValue > priv->pmib->dot11RFEntry.ther) {
1453 if (delta < 5)
1454 OFDM_index[i] -= 1;
1455 else
1456 OFDM_index[i] -= 2;
1457 } else if (TxPwrLevel[i] >= 32 && TxPwrLevel[i] <= 38 && ThermalValue > priv->pmib->dot11RFEntry.ther && delta > 5) {
1458 OFDM_index[i] -= 1;
1459 }
1460
1461#ifdef _TRACKING_TABLE_FILE
1462 if (priv->pshare->rf_ft_var.pwr_track_file)
1463 {
1464 int d = 0;
1465
1466 OFDM_index[i] = priv->pshare->OFDM_index[i];
1467 d = RTL_ABS(OFDM_index[i], priv->pshare->OFDM_index0[i]);
1468
1469 if(OFDM_index[i] < priv->pshare->OFDM_index0[i])
1470 {
1471 OFDM_index[i] = priv->pshare->OFDM_index0[i] - get_tx_tracking_index(priv, channel, i, d, 0, 0);
1472 }
1473 else
1474 {
1475 OFDM_index[i] = priv->pshare->OFDM_index0[i] + get_tx_tracking_index(priv, channel, i, d, 1, 0);
1476 }
1477
1478 }
1479 else
1480#endif
1481 {
1482#ifdef HIGH_POWER_EXT_PA
1483 if (priv->pshare->rf_ft_var.use_ext_pa) {
1484 OFDM_index[i] = priv->pshare->OFDM_index[i];
1485 swingIndexRemap2(&OFDM_index[i], priv->pshare->OFDM_index0[i], i); //Modify HP tracking table, from Arthur 2012.02.13
1486 //swingIndexRemap(&OFDM_index[i], priv->pshare->OFDM_index0[i]);
1487 }
1488#endif
1489 }
1490 if(OFDM_index[i] > OFDM_TABLE_SIZE-1)
1491 OFDM_index[i] = OFDM_TABLE_SIZE-1;
1492 else if (OFDM_index[i] < OFDM_min_index)
1493 OFDM_index[i] = OFDM_min_index;
1494 }
1495 i=0;
1496 {
1497 if(/*TxPwrLevel[i] >=0 &&*/ TxPwrLevel[i] <=26) {
1498 if(ThermalValue > priv->pmib->dot11RFEntry.ther) {
1499 if (delta < 5)
1500 CCK_index -= 1;
1501 else
1502 CCK_index -= 2;
1503 } else if(delta > 5 && ThermalValue < priv->pmib->dot11RFEntry.ther) {
1504 CCK_index += 1;
1505 }
1506 } else if (TxPwrLevel[i] >= 27 && TxPwrLevel[i] <= 32 && ThermalValue > priv->pmib->dot11RFEntry.ther) {
1507 if (delta < 5)
1508 CCK_index -= 1;
1509 else
1510 CCK_index -= 2;
1511 } else if (TxPwrLevel[i] >= 32 && TxPwrLevel[i] <= 38 && ThermalValue > priv->pmib->dot11RFEntry.ther && delta > 5) {
1512 CCK_index -= 1;
1513 }
1514
1515#ifdef _TRACKING_TABLE_FILE
1516 if (priv->pshare->rf_ft_var.pwr_track_file)
1517 {
1518 int d = 0;
1519
1520 CCK_index = priv->pshare->CCK_index;
1521 d = RTL_ABS(CCK_index, priv->pshare->CCK_index0);
1522
1523 if(CCK_index < priv->pshare->CCK_index0)
1524 {
1525 CCK_index = priv->pshare->CCK_index0 - get_tx_tracking_index(priv, channel, i, d, 0, 1);
1526 }
1527 else
1528 {
1529 CCK_index = priv->pshare->CCK_index0 + get_tx_tracking_index(priv, channel, i, d, 1, 1);
1530 }
1531
1532 }
1533 else
1534#endif
1535 {
1536#ifdef HIGH_POWER_EXT_PA
1537 if (priv->pshare->rf_ft_var.use_ext_pa) {
1538 CCK_index = priv->pshare->CCK_index;
1539 swingIndexRemap2( &CCK_index, priv->pshare->CCK_index0, i); //Modify HP tracking table, from Arthur 2012.02.13
1540 //swingIndexRemap( &CCK_index, priv->pshare->CCK_index0);
1541 }
1542#endif
1543 }
1544 if(CCK_index > CCK_TABLE_SIZE-1)
1545 CCK_index = CCK_TABLE_SIZE-1;
1546 else if (CCK_index < 0)
1547 CCK_index = 0;
1548 }
1549
1550 //Adujst OFDM Ant_A according to IQK result
1551 ele_D = (ofdm_swing_table[(unsigned int)OFDM_index[0]] & 0xFFC00000)>>22;
1552 X = priv->pshare->rege94;
1553 Y = priv->pshare->rege9c;
1554
1555 if(X != 0) {
1556 if ((X & 0x00000200) != 0)
1557 X = X | 0xFFFFFC00;
1558 ele_A = ((X * ele_D)>>8)&0x000003FF;
1559
1560 //new element C = element D x Y
1561 if ((Y & 0x00000200) != 0)
1562 Y = Y | 0xFFFFFC00;
1563 ele_C = ((Y * ele_D)>>8)&0x000003FF;
1564
1565 //wirte new elements A, C, D to regC80 and regC94, element B is always 0
1566 value32 = (ele_D<<22)|((ele_C&0x3F)<<16)|ele_A;
1567 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, value32);
1568
1569 value32 = (ele_C&0x000003C0)>>6;
1570 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, value32);
1571
1572 value32 = ((X * ele_D)>>7)&0x01;
1573 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), value32);
1574
1575 } else {
1576 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, ofdm_swing_table[(unsigned int)OFDM_index[0]]);
1577 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, 0x00);
1578 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), 0x00);
1579 }
1580
1581
1582 set_CCK_swing_index(priv, CCK_index);
1583
1584
1585 if(is2T) {
1586 ele_D = (ofdm_swing_table[(unsigned int)OFDM_index[1]] & 0xFFC00000)>>22;
1587 X = priv->pshare->regeb4;
1588 Y = priv->pshare->regebc;
1589
1590 if(X != 0) {
1591 if ((X & 0x00000200) != 0) //consider minus
1592 X = X | 0xFFFFFC00;
1593 ele_A = ((X * ele_D)>>8)&0x000003FF;
1594
1595 //new element C = element D x Y
1596 if ((Y & 0x00000200) != 0)
1597 Y = Y | 0xFFFFFC00;
1598 ele_C = ((Y * ele_D)>>8)&0x00003FF;
1599
1600 //wirte new elements A, C, D to regC88 and regC9C, element B is always 0
1601 value32=(ele_D<<22)|((ele_C&0x3F)<<16) |ele_A;
1602 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, value32);
1603
1604 value32 = (ele_C&0x000003C0)>>6;
1605 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, value32);
1606
1607 value32 = ((X * ele_D)>>7)&0x01;
1608 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), value32);
1609
1610 } else {
1611 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, ofdm_swing_table[(unsigned int)OFDM_index[1]]);
1612 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, 0x00);
1613 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), 0x00);
1614 }
1615 }
1616 }
1617
1618 if(delta_IQK > 3) {
1619 priv->pshare->thermal_value_iqk = ThermalValue;
1620#ifdef MP_TEST
1621 if(priv->pshare->rf_ft_var.mp_specific)
1622 {
1623 if((OPMODE & WIFI_MP_CTX_BACKGROUND) && !(OPMODE & WIFI_MP_CTX_PACKET))
1624 printk("NOT do IQK during ctx !!!! \n");
1625 else
1626 phy_iq_calibrate(priv);
1627 }
1628 else
1629#endif
1630 phy_iq_calibrate(priv);
1631 }
1632
1633 //update thermal meter value
1634 priv->pshare->thermal_value = ThermalValue;
1635
1636 }
1637}
1638#endif
1639
1640
1641#ifdef RX_GAIN_TRACK_92D
1642static void rx_gain_tracking_92D(struct rtl8192cd_priv *priv)
1643{
1644 u8 index_mapping[Rx_index_mapping_NUM] = {
1645 0x0f, 0x0f, 0x0f, 0x0f, 0x0b,
1646 0x0a, 0x09, 0x08, 0x07, 0x06,
1647 0x05, 0x04, 0x04, 0x03, 0x02
1648 };
1649
1650 u8 eRFPath, curMaxRFPath;
1651 u32 u4tmp;
1652
1653 u4tmp = (index_mapping[(priv->pmib->dot11RFEntry.ther - priv->pshare->ThermalValue_RxGain)]) << 12;
1654
1655 DEBUG_INFO("===>%s interface %d Rx Gain %x\n", __FUNCTION__, priv->pshare->wlandev_idx, u4tmp);
1656
1657 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)
1658 curMaxRFPath = RF_PATH_B;
1659 else
1660 curMaxRFPath = RF_PATH_MAX;
1661
1662 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++)
1663 phy_set_rf_reg(priv, eRFPath, 0x3C, bMask20Bits, (priv->pshare->RegRF3C[eRFPath]&(~(0xF000)))|u4tmp);
1664
1665};
1666
1667#endif
1668
1669#if 0
1670//#ifdef CONFIG_RTL_88E_SUPPORT //for 88e tx power tracking
1671
1672void odm_reset_iqk_result(struct rtl8192cd_priv *priv)
1673{
1674/*
1675#if (DM_ODM_SUPPORT_TYPE == ODM_WIN || DM_ODM_SUPPORT_TYPE == ODM_CE)
1676 PADAPTER Adapter = p_dm_odm->Adapter;
1677 u1Byte i;
1678
1679 if (!IS_HARDWARE_TYPE_8192D(Adapter))
1680 return;
1681#endif
1682*/
1683
1684 unsigned char i;
1685
1686 //printk("PHY_ResetIQKResult:: settings regs %d default regs %d\n", sizeof(priv->pshare->IQKMatrixRegSetting)/sizeof(struct _IQK_MATRIX_REGS_SETTING), IQK_Matrix_Settings_NUM);
1687 //0xe94, 0xe9c, 0xea4, 0xeac, 0xeb4, 0xebc, 0xec4, 0xecc
1688
1689 for(i = 0; i < IQK_Matrix_Settings_NUM; i++)
1690 {
1691 {
1692 priv->pshare->IQKMatrixRegSetting[i].Value[0][0] =
1693 priv->pshare->IQKMatrixRegSetting[i].Value[0][2] =
1694 priv->pshare->IQKMatrixRegSetting[i].Value[0][4] =
1695 priv->pshare->IQKMatrixRegSetting[i].Value[0][6] = 0x100;
1696
1697 priv->pshare->IQKMatrixRegSetting[i].Value[0][1] =
1698 priv->pshare->IQKMatrixRegSetting[i].Value[0][3] =
1699 priv->pshare->IQKMatrixRegSetting[i].Value[0][5] =
1700 priv->pshare->IQKMatrixRegSetting[i].Value[0][7] = 0x0;
1701
1702 priv->pshare->IQKMatrixRegSetting[i].bIQKDone = FALSE;
1703
1704 }
1705 }
1706
1707}
1708
1709#define RF_PATH_A 0 //Radio Path A
1710#define OFDM_TABLE_SIZE_92D 43
1711
1712#define bRFRegOffsetMask 0xfffff
1713
1714
1715//091212 chiyokolin
1716void odm_txpowertracking_callback_thermal_meter_8188e(struct rtl8192cd_priv *priv)
1717{
1718
1719 //HAL_DATA_TYPE *pHalData = GET_HAL_DATA(Adapter);
1720
1721 unsigned char ThermalValue = 0, delta, delta_LCK, delta_IQK, offset;
1722 unsigned char ThermalValue_AVG_count = 0;
1723 unsigned int ThermalValue_AVG = 0;
1724 int ele_A=0, ele_D, TempCCk, X, value32;
1725 int Y, ele_C=0;
1726 char OFDM_index[2], CCK_index=0, OFDM_index_old[2]={0,0}, CCK_index_old=0, index;
1727 unsigned int i = 0, j = 0;
1728 char is2T = FALSE;
1729 //char bInteralPA = FALSE;
1730
1731 unsigned char OFDM_min_index = 6, rf; //OFDM BB Swing should be less than +3.0dB, which is required by Arthur
1732 unsigned char Indexforchannel = 0/*GetRightChnlPlaceforIQK(pHalData->CurrentChannel)*/;
1733 char OFDM_index_mapping[2][index_mapping_NUM_88E] = {
1734 {0, 0, 2, 3, 4, 4, //2.4G, decrease power
1735 5, 6, 7, 7, 8, 9,
1736 10, 10, 11}, // For lower temperature, 20120220 updated on 20120220.
1737 {0, 0, -1, -2, -3, -4, //2.4G, increase power
1738 -4, -4, -4, -5, -7, -8,
1739 -9, -9, -10},
1740 };
1741 unsigned char Thermal_mapping[2][index_mapping_NUM_88E] = {
1742 {0, 2, 4, 6, 8, 10, //2.4G, decrease power
1743 12, 14, 16, 18, 20, 22,
1744 24, 26, 27},
1745 {0, 2, 4, 6, 8, 10, //2.4G,, increase power
1746 12, 14, 16, 18, 20, 22,
1747 25, 25, 25},
1748 };
1749
1750 priv->pshare->TXPowerTrackingCallbackCnt++; //cosa add for debug
1751 priv->pshare->bTXPowerTrackingInit = TRUE;
1752
1753#if (MP_DRIVER == 1)
1754 priv->pshare->TxPowerTrackControl = 1; //priv->pshare->TxPowerTrackControl; //_eric_?? // <Kordan> We should keep updating the control variable according to HalData.
1755 // <Kordan> pshare->RegA24 will be initialized when ODM HW configuring, but MP configures with para files.
1756 priv->pshare->RegA24 = 0x090e1317;
1757#endif
1758
1759
1760#ifdef MP_TEST
1761 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific) {
1762 if(priv->pshare->mp_txpwr_tracking == FALSE)
1763 return;
1764 }
1765#endif
1766
1767 if(priv->pshare->Power_tracking_on_88E == 0)
1768 {
1769 priv->pshare->Power_tracking_on_88E = 1;
1770 phy_set_rf_reg(priv, RF_PATH_A, 0x42, (BIT(17) | BIT(16)), 0x03);
1771 return;
1772 }
1773 else
1774 {
1775
1776 priv->pshare->Power_tracking_on_88E = 0;
1777
1778 //printk("===>dm_TXPowerTrackingCallback_ThermalMeter_8188E txpowercontrol %d\n", priv->pshare->TxPowerTrackControl);
1779
1780 ThermalValue = (unsigned char)phy_query_rf_reg(priv, RF_PATH_A, RF_T_METER_88E, 0xfc00, 1); //0x42: RF Reg[15:10] 88E
1781
1782 printk("\nReadback Thermal Meter = 0x%x pre thermal meter 0x%x EEPROMthermalmeter 0x%x\n", ThermalValue, priv->pshare->ThermalValue, priv->pmib->dot11RFEntry.ther);
1783
1784 }
1785
1786 if(is2T)
1787 rf = 2;
1788 else
1789 rf = 1;
1790
1791 if(ThermalValue)
1792 {
1793// if(!pHalData->ThermalValue)
1794 {
1795 //Query OFDM path A default setting
1796 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
1797
1798 for(i=0; i<OFDM_TABLE_SIZE_92D; i++) //find the index
1799 {
1800 if(ele_D == (ofdm_swing_table[i]&bMaskOFDM_D))
1801 {
1802 OFDM_index_old[0] = (unsigned char)i;
1803 printk("Initial pathA ele_D reg0x%x = 0x%x, OFDM_index=0x%x\n",
1804 REG_OFDM_0_XA_TX_IQ_IMBALANCE, ele_D, OFDM_index_old[0]);
1805 break;
1806 }
1807 }
1808
1809 //Query OFDM path B default setting
1810 if(is2T)
1811 {
1812 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
1813 for(i=0; i<OFDM_TABLE_SIZE_92D; i++) //find the index
1814 {
1815 if(ele_D == (ofdm_swing_table[i]&bMaskOFDM_D))
1816 {
1817 OFDM_index_old[1] = (unsigned char)i;
1818 printk("Initial pathB ele_D reg0x%x = 0x%x, OFDM_index=0x%x\n",
1819 REG_OFDM_0_XB_TX_IQ_IMBALANCE, ele_D, OFDM_index_old[1]);
1820 break;
1821 }
1822 }
1823 }
1824
1825 {
1826 //Query CCK default setting From 0xa24
1827 TempCCk = priv->pshare->RegA24;
1828
1829 for(i=0 ; i<CCK_TABLE_SIZE ; i++)
1830 {
1831 if(priv->pshare->bCCKinCH14)
1832 {
1833 if(memcmp((void*)&TempCCk, (void*)&cck_swing_table_ch14[i][2], 4)==0)
1834 {
1835 CCK_index_old =(unsigned char) i;
1836 //printk("Initial reg0x%x = 0x%x, CCK_index=0x%x, ch 14 %d\n",
1837 //rCCK0_TxFilter2, TempCCk, CCK_index_old, priv->pshare->bCCKinCH14);
1838 break;
1839 }
1840 }
1841 else
1842 {
1843 //printk("RegA24: 0x%X, cck_swing_table_ch1_ch13[%d][2]: cck_swing_table_ch1_ch13[i][2]: 0x%X\n", TempCCk, i, cck_swing_table_ch1_ch13[i][2]);
1844 if(memcmp((void*)&TempCCk, (void*)&cck_swing_table_ch1_ch13[i][2], 4)==0)
1845 {
1846 CCK_index_old =(unsigned char) i;
1847 //printk("Initial reg0x%x = 0x%x, CCK_index=0x%x, ch14 %d\n",
1848 //rCCK0_TxFilter2, TempCCk, CCK_index_old, priv->pshare->bCCKinCH14);
1849 break;
1850 }
1851 }
1852 }
1853 }
1854
1855 if(!priv->pshare->ThermalValue)
1856 {
1857 priv->pshare->ThermalValue = priv->pmib->dot11RFEntry.ther;
1858 priv->pshare->ThermalValue_LCK = ThermalValue;
1859 priv->pshare->ThermalValue_IQK = ThermalValue;
1860
1861 for(i = 0; i < rf; i++)
1862 priv->pshare->OFDM_index[i] = OFDM_index_old[i];
1863 priv->pshare->CCK_index = CCK_index_old;
1864 }
1865
1866 if(priv->pshare->bReloadtxpowerindex)
1867 {
1868 printk("reload ofdm index for band switch\n");
1869 }
1870
1871 //calculate average thermal meter
1872 {
1873 priv->pshare->ThermalValue_AVG[priv->pshare->ThermalValue_AVG_index] = ThermalValue;
1874 priv->pshare->ThermalValue_AVG_index++;
1875 if(priv->pshare->ThermalValue_AVG_index == AVG_THERMAL_NUM_88E)
1876 priv->pshare->ThermalValue_AVG_index = 0;
1877
1878 for(i = 0; i < AVG_THERMAL_NUM_88E; i++)
1879 {
1880 if(priv->pshare->ThermalValue_AVG[i])
1881 {
1882 ThermalValue_AVG += priv->pshare->ThermalValue_AVG[i];
1883 ThermalValue_AVG_count++;
1884 }
1885 }
1886
1887 if(ThermalValue_AVG_count)
1888 {
1889 ThermalValue = (unsigned char)(ThermalValue_AVG / ThermalValue_AVG_count);
1890 printk("AVG Thermal Meter = 0x%x \n", ThermalValue);
1891 }
1892 }
1893 }
1894
1895 if(priv->pshare->bReloadtxpowerindex)
1896 {
1897 delta = ThermalValue > priv->pmib->dot11RFEntry.ther?(ThermalValue - priv->pmib->dot11RFEntry.ther):(priv->pmib->dot11RFEntry.ther - ThermalValue);
1898 priv->pshare->bReloadtxpowerindex = FALSE;
1899 priv->pshare->bDoneTxpower = FALSE;
1900 }
1901 else if(priv->pshare->bDoneTxpower)
1902 {
1903 delta = (ThermalValue > priv->pshare->ThermalValue)?(ThermalValue - priv->pshare->ThermalValue):(priv->pshare->ThermalValue - ThermalValue);
1904 }
1905 else
1906 {
1907 delta = ThermalValue > priv->pmib->dot11RFEntry.ther?(ThermalValue - priv->pmib->dot11RFEntry.ther):(priv->pmib->dot11RFEntry.ther - ThermalValue);
1908 }
1909 delta_LCK = (ThermalValue > priv->pshare->ThermalValue_LCK)?(ThermalValue - priv->pshare->ThermalValue_LCK):(priv->pshare->ThermalValue_LCK - ThermalValue);
1910 delta_IQK = (ThermalValue > priv->pshare->ThermalValue_IQK)?(ThermalValue - priv->pshare->ThermalValue_IQK):(priv->pshare->ThermalValue_IQK - ThermalValue);
1911
1912 printk("Readback Thermal Meter = 0x%x \npre thermal meter 0x%x EEPROMthermalmeter 0x%x delta 0x%x \ndelta_LCK 0x%x delta_IQK 0x%x \n", ThermalValue, priv->pshare->ThermalValue, priv->pshare->EEPROMThermalMeter, delta, delta_LCK, delta_IQK);
1913 printk("pre thermal meter LCK 0x%x \npre thermal meter IQK 0x%x \ndelta_LCK_bound 0x%x delta_IQK_bound 0x%x\n", priv->pshare->ThermalValue_LCK, priv->pshare->ThermalValue_IQK, priv->pshare->delta_lck, priv->pshare->delta_iqk);
1914
1915
1916 //if((delta_LCK > pHalData->delta_lck) && (pHalData->delta_lck != 0))
1917 if (delta_LCK >= 8) // Delta temperature is equal to or larger than 20 centigrade.
1918 {
1919 priv->pshare->ThermalValue_LCK = ThermalValue;
1920 phy_lc_calibrate(priv);
1921 }
1922
1923
1924 if(delta > 0 && priv->pshare->TxPowerTrackControl)
1925 {
1926 delta = ThermalValue > priv->pmib->dot11RFEntry.ther?(ThermalValue - priv->pmib->dot11RFEntry.ther):(priv->pmib->dot11RFEntry.ther - ThermalValue);
1927
1928 //calculate new OFDM / CCK offset
1929 {
1930 {
1931 if(ThermalValue > priv->pmib->dot11RFEntry.ther)
1932 j = 1;
1933 else
1934 j = 0;
1935
1936 for(offset = 0; offset < index_mapping_NUM_88E; offset++)
1937 {
1938 if(delta < Thermal_mapping[j][offset])
1939 {
1940 if(offset != 0)
1941 offset--;
1942 break;
1943 }
1944 }
1945 if(offset >= index_mapping_NUM_88E)
1946 offset = index_mapping_NUM_88E-1;
1947
1948 index = OFDM_index_mapping[j][offset];
1949
1950 printk("\nj = %d delta = %d, index = %d\n\n", j, delta, index);
1951
1952 for(i = 0; i < rf; i++)
1953 OFDM_index[i] = priv->pshare->OFDM_index[i] + OFDM_index_mapping[j][offset];
1954 CCK_index = priv->pshare->CCK_index + OFDM_index_mapping[j][offset];
1955 }
1956
1957 if(is2T)
1958 {
1959 printk("temp OFDM_A_index=0x%x, OFDM_B_index=0x%x, CCK_index=0x%x\n",
1960 priv->pshare->OFDM_index[0], priv->pshare->OFDM_index[1], priv->pshare->CCK_index);
1961 }
1962 else
1963 {
1964 printk("temp OFDM_A_index=0x%x, CCK_index=0x%x\n",
1965 priv->pshare->OFDM_index[0], priv->pshare->CCK_index);
1966 }
1967
1968 for(i = 0; i < rf; i++)
1969 {
1970 if(OFDM_index[i] > OFDM_TABLE_SIZE_92D-1)
1971 {
1972 OFDM_index[i] = OFDM_TABLE_SIZE_92D-1;
1973 }
1974 else if (OFDM_index[i] < OFDM_min_index)
1975 {
1976 OFDM_index[i] = OFDM_min_index;
1977 }
1978 }
1979
1980 {
1981 if(CCK_index > CCK_TABLE_SIZE-1)
1982 CCK_index = CCK_TABLE_SIZE-1;
1983 else if (CCK_index < 0)
1984 CCK_index = 0;
1985 }
1986
1987 if(is2T)
1988 {
1989 printk("new OFDM_A_index=0x%x, OFDM_B_index=0x%x, CCK_index=0x%x\n",
1990 OFDM_index[0], OFDM_index[1], CCK_index);
1991 }
1992 else
1993 {
1994 printk("new OFDM_A_index=0x%x, CCK_index=0x%x\n",
1995 OFDM_index[0], CCK_index);
1996 }
1997 }
1998
1999 //2 temporarily remove bNOPG
2000 //Config by SwingTable
2001 if(priv->pshare->TxPowerTrackControl /*&& !pHalData->bNOPG*/)
2002 {
2003 priv->pshare->bDoneTxpower = TRUE;
2004
2005 //Adujst OFDM Ant_A according to IQK result
2006 ele_D = (ofdm_swing_table[(unsigned char)OFDM_index[0]] & 0xFFC00000)>>22;
2007 X = priv->pshare->IQKMatrixRegSetting[Indexforchannel].Value[0][0];
2008 Y = priv->pshare->IQKMatrixRegSetting[Indexforchannel].Value[0][1];
2009
2010 if(X != 0)
2011 {
2012 if ((X & 0x00000200) != 0)
2013 X = X | 0xFFFFFC00;
2014 ele_A = ((X * ele_D)>>8)&0x000003FF;
2015
2016 //new element C = element D x Y
2017 if ((Y & 0x00000200) != 0)
2018 Y = Y | 0xFFFFFC00;
2019 ele_C = ((Y * ele_D)>>8)&0x000003FF;
2020
2021 //wirte new elements A, C, D to regC80 and regC94, element B is always 0
2022 value32 = (ele_D<<22)|((ele_C&0x3F)<<16)|ele_A;
2023 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, value32);
2024
2025 value32 = (ele_C&0x000003C0)>>6;
2026 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, value32);
2027
2028 value32 = ((X * ele_D)>>7)&0x01;
2029 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), value32);
2030
2031 }
2032 else
2033 {
2034 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, ofdm_swing_table[(unsigned char)OFDM_index[0]]);
2035 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, 0x00);
2036 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), 0x00);
2037 }
2038
2039 //printk("TxPwrTracking for path A: X = 0x%x, Y = 0x%x ele_A = 0x%x ele_C = 0x%x ele_D = 0x%x 0xe94 = 0x%x 0xe9c = 0x%x\n",
2040 //(unsigned int)X, (unsigned int)Y, (unsigned int)ele_A, (unsigned int)ele_C, (unsigned int)ele_D, (unsigned int)X, (unsigned int)Y);
2041
2042 {
2043 //Adjust CCK according to IQK result
2044 if(!priv->pshare->bCCKinCH14){
2045 RTL_W8(0xa22, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][0]);
2046 RTL_W8(0xa23, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][1]);
2047 RTL_W8(0xa24, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][2]);
2048 RTL_W8(0xa25, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][3]);
2049 RTL_W8(0xa26, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][4]);
2050 RTL_W8(0xa27, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][5]);
2051 RTL_W8(0xa28, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][6]);
2052 RTL_W8(0xa29, cck_swing_table_ch1_ch13[(unsigned char)CCK_index][7]);
2053 }
2054 else{
2055 RTL_W8(0xa22, cck_swing_table_ch14[(unsigned char)CCK_index][0]);
2056 RTL_W8(0xa23, cck_swing_table_ch14[(unsigned char)CCK_index][1]);
2057 RTL_W8(0xa24, cck_swing_table_ch14[(unsigned char)CCK_index][2]);
2058 RTL_W8(0xa25, cck_swing_table_ch14[(unsigned char)CCK_index][3]);
2059 RTL_W8(0xa26, cck_swing_table_ch14[(unsigned char)CCK_index][4]);
2060 RTL_W8(0xa27, cck_swing_table_ch14[(unsigned char)CCK_index][5]);
2061 RTL_W8(0xa28, cck_swing_table_ch14[(unsigned char)CCK_index][6]);
2062 RTL_W8(0xa29, cck_swing_table_ch14[(unsigned char)CCK_index][7]);
2063 }
2064 }
2065
2066 if(is2T)
2067 {
2068 ele_D = (ofdm_swing_table[(unsigned char)OFDM_index[1]] & 0xFFC00000)>>22;
2069
2070 //new element A = element D x X
2071 X = priv->pshare->IQKMatrixRegSetting[Indexforchannel].Value[0][4];
2072 Y = priv->pshare->IQKMatrixRegSetting[Indexforchannel].Value[0][5];
2073
2074 //if(X != 0 && pHalData->CurrentBandType92D == ODM_BAND_ON_2_4G)
2075 if((X != 0) && (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G))
2076
2077 {
2078 if ((X & 0x00000200) != 0) //consider minus
2079 X = X | 0xFFFFFC00;
2080 ele_A = ((X * ele_D)>>8)&0x000003FF;
2081
2082 //new element C = element D x Y
2083 if ((Y & 0x00000200) != 0)
2084 Y = Y | 0xFFFFFC00;
2085 ele_C = ((Y * ele_D)>>8)&0x00003FF;
2086
2087 //wirte new elements A, C, D to regC88 and regC9C, element B is always 0
2088 value32=(ele_D<<22)|((ele_C&0x3F)<<16) |ele_A;
2089 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, value32);
2090
2091 value32 = (ele_C&0x000003C0)>>6;
2092 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, value32);
2093
2094 value32 = ((X * ele_D)>>7)&0x01;
2095 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), value32);
2096
2097 }
2098 else
2099 {
2100 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, ofdm_swing_table[(unsigned char)OFDM_index[1]]);
2101 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, 0x00);
2102 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), 0x00);
2103 }
2104
2105 //printk("TxPwrTracking path B: X = 0x%x, Y = 0x%x ele_A = 0x%x ele_C = 0x%x ele_D = 0x%x 0xeb4 = 0x%x 0xebc = 0x%x\n",
2106 //(unsigned int)X, (unsigned int)Y, (unsigned int)ele_A, (unsigned int)ele_C, (unsigned int)ele_D, (unsigned int)X, (unsigned int)Y);
2107 }
2108
2109 printk("TxPwrTracking 0xc80 = 0x%x, 0xc94 = 0x%x RF 0x24 = 0x%x\n\n", phy_query_bb_reg(priv, 0xc80, bMaskDWord), phy_query_bb_reg(priv, 0xc94, bMaskDWord), phy_query_rf_reg(priv, RF_PATH_A, 0x24, bRFRegOffsetMask, 1));
2110 }
2111 }
2112
2113
2114#if 0 //DO NOT do IQK during 88E power tracking
2115 // if((delta_IQK > pHalData->delta_iqk) && (pHalData->delta_iqk != 0))
2116 if (delta_IQK >= 8) // Delta temperature is equal to or larger than 20 centigrade.
2117 {
2118 odm_reset_iqk_result(priv);
2119
2120/*
2121#if(DM_ODM_SUPPORT_TYPE & ODM_WIN)
2122#if (DEV_BUS_TYPE & RT_PCI_INTERFACE)
2123#if USE_WORKITEM
2124 platform_acquire_mutex(&pHalData->mxChnlBwControl);
2125#else
2126 platform_acquire_spin_lock(Adapter, RT_CHANNEL_AND_BANDWIDTH_SPINLOCK);
2127#endif
2128#elif((DEV_BUS_TYPE & RT_USB_INTERFACE) || (DEV_BUS_TYPE & RT_SDIO_INTERFACE))
2129 platform_acquire_mutex(&pHalData->mxChnlBwControl);
2130#endif
2131#endif
2132*/
2133 priv->pshare->ThermalValue_IQK= ThermalValue;
2134 phy_iq_calibrate_8188e(priv, FALSE);
2135
2136/*
2137#if(DM_ODM_SUPPORT_TYPE & ODM_WIN)
2138#if (DEV_BUS_TYPE & RT_PCI_INTERFACE)
2139#if USE_WORKITEM
2140 platform_release_mutex(&pHalData->mxChnlBwControl);
2141#else
2142 platform_release_spin_lock(Adapter, RT_CHANNEL_AND_BANDWIDTH_SPINLOCK);
2143#endif
2144#elif((DEV_BUS_TYPE & RT_USB_INTERFACE) || (DEV_BUS_TYPE & RT_SDIO_INTERFACE))
2145 platform_release_mutex(&pHalData->mxChnlBwControl);
2146#endif
2147#endif
2148*/
2149 }
2150#endif
2151
2152 //update thermal meter value
2153 if(priv->pshare->TxPowerTrackControl)
2154 {
2155 //Adapter->HalFunc.SetHalDefVarHandler(Adapter, HAL_DEF_THERMAL_VALUE, &ThermalValue);
2156 priv->pshare->ThermalValue = ThermalValue;
2157 }
2158
2159 }
2160
2161 //printk("<===dm_TXPowerTrackingCallback_ThermalMeter_8188E\n");
2162
2163 priv->pshare->TXPowercount = 0;
2164
2165}
2166
2167
2168
2169
2170#endif
2171
2172
2173
2174#ifdef CONFIG_RTL_92D_SUPPORT
2175
2176void getDeltaValue(struct rtl8192cd_priv *priv)
2177{
2178 unsigned int tempval[2];
2179
2180 tempval[0] = priv->pmib->dot11RFEntry.deltaIQK;
2181 tempval[1] = priv->pmib->dot11RFEntry.deltaLCK;
2182
2183 switch(tempval[0])
2184 {
2185 case 0:
2186 tempval[0] = 5;
2187 break;
2188
2189 case 1:
2190 tempval[0] = 4;
2191 break;
2192
2193 case 2:
2194 tempval[0] = 3;
2195 break;
2196
2197 case 3:
2198 default:
2199 tempval[0] = 0;
2200 break;
2201 }
2202
2203 switch(tempval[1])
2204 {
2205 case 0:
2206 tempval[1] = 4;
2207 break;
2208
2209 case 1:
2210 tempval[1] = 3;
2211 break;
2212
2213 case 2:
2214 tempval[1] = 2;
2215 break;
2216
2217 case 3:
2218 default:
2219 tempval[1] = 0;
2220 break;
2221 }
2222
2223 priv->pshare->delta_iqk = tempval[0];
2224 priv->pshare->delta_lck = tempval[1];
2225}
2226
2227void tx_power_tracking_92D(struct rtl8192cd_priv *priv)
2228{
2229 u8 ThermalValue = 0, delta, delta_LCK, delta_IQK, index[2], offset, ThermalValue_AVG_count = 0;
2230 u32 ThermalValue_AVG = 0;
2231 int ele_A, ele_D, X, value32, Y, ele_C;
2232 char OFDM_index[2], CCK_index=0;
2233 int i = 0;
2234 char is2T = ((priv->pmib->dot11RFEntry.macPhyMode != DUALMAC_DUALPHY) ?1 :0);
2235 u8 OFDM_min_index = 6, OFDM_min_index_internalPA = 5, rf=1, channel; //OFDM BB Swing should be less than +3.0dB, which is required by Arthur u1Byte OFDM_min_index = 6, rf; //OFDM BB Swing should be less than +3.0dB, which is required by Arthur
2236
2237 u8 index_mapping[5][index_mapping_NUM] = {
2238 {0, 1, 3, 6, 8, 9, //5G, path A/MAC 0, decrease power
2239 11, 13, 14, 16, 17, 18, 18},
2240 {0, 2, 4, 5, 7, 10, //5G, path A/MAC 0, increase power
2241 12, 14, 16, 18, 18, 18, 18},
2242 {0, 2, 3, 6, 8, 9, //5G, path B/MAC 1, decrease power
2243 11, 13, 14, 16, 17, 18, 18},
2244 {0, 2, 4, 5, 7, 10, //5G, path B/MAC 1, increase power
2245 13, 16, 16, 18, 18, 18, 18},
2246 {0, 1, 2, 3, 4, 5, //2.4G, for decreas power
2247 6, 7, 7, 8, 9, 10, 10},
2248 };
2249
2250#if defined(RTL8192D_INT_PA)
2251
2252 u8 index_mapping_internalPA[8][index_mapping_NUM] = {
2253 {0, 1, 3, 4, 6, 7, //5G, path A/MAC 0, ch36-64, decrease power
2254 9, 11, 13, 15, 16, 16, 16},
2255 {0, 1, 3, 4, 6, 7, //5G, path A/MAC 0, ch36-64, increase power
2256 9, 11, 13, 15, 16, 18, 20},
2257 {0, 1, 3, 4, 6, 7, //5G, path A/MAC 0, ch100-165, decrease power
2258 9, 11, 13, 15, 16, 16, 16},
2259 {0, 1, 3, 4, 6, 7, //5G, path A/MAC 0, ch100-165, increase power
2260 9, 11, 13, 15, 16, 18, 20},
2261 {0, 1, 3, 4, 6, 7, //5G, path B/MAC 1, ch36-64, decrease power
2262 9, 11, 13, 15, 16, 16, 16},
2263 {0, 1, 3, 4, 6, 7, //5G, path B/MAC 1, ch36-64, increase power
2264 9, 11, 13, 15, 16, 18, 20},
2265 {0, 1, 3, 4, 6, 7, //5G, path B/MAC 1, ch100-165, decrease power
2266 9, 11, 13, 15, 16, 16, 16},
2267 {0, 1, 3, 4, 6, 7, //5G, path B/MAC 1, ch100-165, increase power
2268 9, 11, 13, 15, 16, 18, 20},
2269 };
2270
2271 u8 bInteralPA[2];
2272
2273#endif
2274
2275#ifdef DPK_92D
2276 short index_mapping_DPK[4][index_mapping_DPK_NUM]={
2277 {0, 0, 1, 2, 2, //path A current thermal > PG thermal
2278 3, 4, 5, 5, 6,
2279 7, 7, 8, 9, 9},
2280 {0, 0, -1, -2, -3, //path A current thermal < PG thermal
2281 -3, -4, -5, -6, -6,
2282 -7, -8, -9, -9, -10},
2283 {0, 0, 1, 2, 2, //path B current thermal > PG thermal
2284 3, 4, 5, 5, 6,
2285 7, 7, 8, 9, 9},
2286 {0, 0, -1, -2, -3, //path B current thermal < PG thermal
2287 -3, -4, -5, -6, -6,
2288 -7, -8, -9, -9, -10}
2289 };
2290
2291 u8 delta_DPK;
2292 short index_DPK[2] = { 0xb68, 0xb6c }, value_DPK, value_DPK_shift;
2293 int j;
2294
2295 if(priv->pshare->bDPKworking) {
2296 DEBUG_INFO("DPK in progress abort tx power tracking \n");
2297 return;
2298 }
2299
2300#endif
2301
2302
2303#ifdef HIGH_POWER_EXT_PA //Modify HP tracking table, from Arthur 2012.02.13
2304
2305u8 index_mapping_HighPower_PA[12][index_mapping_NUM] = {
2306 {0, 2, 3, 4, 7, 8, 10, 12, 13, 15, 16, 17, 18}, //5G, path A/MAC 0, ch36-64, decrease power
2307 {0, 2, 4, 7, 8, 10, 11, 15, 17, 19, 21, 23, 23}, //5G, path A/MAC 0, ch36-64, increase power
2308 {0, 4, 5, 8, 9, 11, 14, 15, 16, 17, 18, 19, 20}, //5G, path A/MAC 0, ch100-140, decrease power
2309 {0, 2, 4, 5, 7, 9, 13, 15, 19, 21, 22, 23, 23}, //5G, path A/MAC 0, ch100-140, increase power
2310 {0, 4, 5, 8, 9, 11, 14, 15, 17, 18, 19, 20, 21}, //5G, path A/MAC 0, ch149-165, decrease power
2311 {0, 2, 4, 6, 8, 10, 14, 16, 19, 21, 22, 24, 24}, //5G, path A/MAC 0, ch149-165, increase power
2312 {0, 4, 5, 6, 8, 9, 11, 12, 13, 14, 15, 16, 17}, //5G, path B/MAC 1, ch36-64, decrease power
2313 {0, 2, 4, 7, 8, 10, 11, 15, 17, 19, 21, 23, 23}, //5G, path B/MAC 1, ch36-64, increase power
2314 {0, 3, 4, 6, 7, 9, 12, 13, 14, 15, 17, 18, 19}, //5G, path B/MAC 1, ch100-140, decrease power
2315 {0, 2, 4, 5, 7, 9, 13, 15, 19, 21, 22, 23, 23}, //5G, path B/MAC 1, ch100-140, increase power
2316 {0, 3, 4, 6, 7, 9, 12, 13, 15, 16, 17, 18, 19}, //5G, path B/MAC 1, ch149-165, decrease power
2317 {0, 3, 5, 7, 9, 11, 13, 17, 19, 21, 22, 23, 23}, //5G, path B/MAC 1, ch149-165, increase power
2318 };
2319
2320#endif
2321
2322#ifdef MP_TEST
2323 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific) {
2324 channel=priv->pshare->working_channel;
2325 if(priv->pshare->mp_txpwr_tracking == FALSE)
2326 return;
2327 } else
2328#endif
2329 {
2330 channel = (priv->pmib->dot11RFEntry.dot11channel);
2331 }
2332#if 0
2333 if (priv->pshare->pwr_trk_ongoing==0) {
2334 phy_set_rf_reg(priv, RF_PATH_A, RF_T_METER_92D, bMask20Bits, 0x30000);
2335 priv->pshare->pwr_trk_ongoing = 1;
2336 return;
2337 }
2338 else
2339#endif
2340 {
2341 ThermalValue =(unsigned char)phy_query_rf_reg(priv, RF_PATH_A, RF_T_METER_92D, 0xf800, 1);
2342// priv->pshare->pwr_trk_ongoing = 0;
2343#ifdef DPK_92D
2344 priv->pshare->ThermalValue_DPKtrack = ThermalValue;
2345#endif
2346 }
2347 DEBUG_INFO("Readback Thermal Meter = 0x%lx pre thermal meter 0x%lx EEPROMthermalmeter 0x%lx\n", ThermalValue,
2348 priv->pshare->thermal_value, priv->pmib->dot11RFEntry.ther);
2349
2350 if(is2T)
2351 rf = 2;
2352 else
2353 rf = 1;
2354
2355 if (ThermalValue) {
2356
2357 //Query OFDM path A default setting
2358 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
2359 for(i=0; i<OFDM_TABLE_SIZE_92D; i++) { //find the index
2360 if(ele_D == (OFDMSwingTable_92D[i]&bMaskOFDM_D)) {
2361 priv->pshare->OFDM_index0[0] = i;
2362 DEBUG_INFO("Initial pathA ele_D reg0x%x = 0x%lx, OFDM_index=0x%x\n",
2363 REG_OFDM_0_XA_TX_IQ_IMBALANCE, ele_D, priv->pshare->OFDM_index0[0]);
2364 break;
2365 }
2366 }
2367
2368 //Query OFDM path B default setting
2369 if(is2T) {
2370 ele_D = phy_query_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord)&bMaskOFDM_D;
2371 for(i=0; i<OFDM_TABLE_SIZE_92D; i++) {
2372 if(ele_D == (OFDMSwingTable_92D[i]&bMaskOFDM_D)) {
2373 priv->pshare->OFDM_index0[1] = i;
2374 DEBUG_INFO("Initial pathB ele_D reg0x%x = 0x%lx, OFDM_index=0x%x\n",
2375 REG_OFDM_0_XB_TX_IQ_IMBALANCE, ele_D, priv->pshare->OFDM_index0[1]);
2376 break;
2377 }
2378 }
2379 }
2380
2381 if(priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G) {
2382 priv->pshare->CCK_index0 = get_CCK_swing_index(priv);
2383 } else {
2384 priv->pshare->CCK_index0 = 12;
2385 }
2386
2387 if(!priv->pshare->thermal_value) {
2388 priv->pshare->thermal_value = priv->pmib->dot11RFEntry.ther;
2389 priv->pshare->thermal_value_lck = ThermalValue;
2390 priv->pshare->thermal_value_iqk = ThermalValue;
2391#ifdef RX_GAIN_TRACK_92D
2392 priv->pshare->ThermalValue_RxGain = priv->pmib->dot11RFEntry.ther;
2393#endif
2394#ifdef DPK_92D
2395 priv->pshare->ThermalValue_DPK = ThermalValue;
2396#endif
2397 for(i = 0; i < rf; i++)
2398 priv->pshare->OFDM_index[i] = priv->pshare->OFDM_index0[i];
2399 priv->pshare->CCK_index = priv->pshare->CCK_index0;
2400 }
2401
2402
2403 //calculate average thermal meter
2404 {
2405 priv->pshare->Thermal_log[priv->pshare->Thermal_idx] = ThermalValue;
2406 priv->pshare->Thermal_idx = (priv->pshare->Thermal_idx+1)%8;
2407
2408 for(i=0; i<8; i++) {
2409 if(priv->pshare->Thermal_log[i]) {
2410 ThermalValue_AVG += priv->pshare->Thermal_log[i];
2411 ThermalValue_AVG_count++;
2412 }
2413 }
2414
2415 if(ThermalValue_AVG_count)
2416 ThermalValue = (u8)(ThermalValue_AVG / ThermalValue_AVG_count);
2417 }
2418
2419
2420 delta = RTL_ABS(ThermalValue, priv->pshare->thermal_value);
2421 delta_LCK = RTL_ABS(ThermalValue, priv->pshare->thermal_value_lck);
2422 delta_IQK = RTL_ABS(ThermalValue, priv->pshare->thermal_value_iqk);
2423
2424// printk("Readback Thermal Meter = 0x%lx pre thermal meter 0x%lx EEPROMthermalmeter 0x%lx delta 0x%lx delta_LCK 0x%lx delta_IQK 0x%lx\n",
2425// ThermalValue, priv->pshare->thermal_value, priv->pmib->dot11RFEntry.ther, delta, delta_LCK, delta_IQK);
2426
2427 getDeltaValue(priv);
2428
2429#ifdef DPK_92D
2430
2431 if(priv->pshare->bDPKstore) {
2432
2433 priv->pshare->ThermalValue_DPK = ThermalValue;
2434 delta_DPK = 0;
2435
2436 for(j = 0; j < rf; j++) {
2437
2438 if(priv->pshare->ThermalValue_DPKstore > priv->pmib->dot11RFEntry.ther)
2439 value_DPK_shift = index_mapping_DPK[j*2][priv->pshare->ThermalValue_DPKstore- priv->pmib->dot11RFEntry.ther];
2440 else
2441 value_DPK_shift = index_mapping_DPK[j*2+1][priv->pmib->dot11RFEntry.ther- priv->pshare->ThermalValue_DPKstore];
2442
2443 for(i = 0; i < index_mapping_DPK_NUM; i++) {
2444 priv->pshare->index_mapping_DPK_current[j*2][i] =
2445 index_mapping_DPK[j*2][i]-value_DPK_shift;
2446 priv->pshare->index_mapping_DPK_current[j*2+1][i] =
2447 index_mapping_DPK[j*2+1][i]-value_DPK_shift;
2448 }
2449 }
2450 }
2451 else
2452 {
2453 delta_DPK = RTL_ABS(ThermalValue, priv->pshare->ThermalValue_DPK);
2454 }
2455
2456 for(j = 0; j < rf; j++) {
2457 if(!priv->pshare->bDPKdone[j])
2458 priv->pshare->OFDM_min_index_internalPA_DPK[j] = 0;
2459 }
2460
2461#endif
2462
2463#if 1
2464 if ((delta_LCK > priv->pshare->delta_lck) && (priv->pshare->delta_lck != 0)) {
2465 priv->pshare->thermal_value_lck = ThermalValue;
2466 PHY_LCCalibrate_92D(priv);
2467 }
2468#endif
2469 if(delta > 0
2470#ifdef DPK_92D
2471 ||(priv->pshare->bDPKstore)
2472#endif
2473 ){
2474 if(delta == 0 && priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G)
2475 goto TxPowerDPK;
2476#ifdef DPK_92D
2477 if(priv->pshare->bDPKstore)
2478 priv->pshare->bDPKstore = FALSE;
2479#endif
2480 delta = RTL_ABS(ThermalValue, priv->pmib->dot11RFEntry.ther);
2481
2482 //calculate new OFDM / CCK offset
2483 {
2484 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G){
2485
2486#ifdef _TRACKING_TABLE_FILE
2487 if (priv->pshare->rf_ft_var.pwr_track_file) {
2488 if(ThermalValue > priv->pmib->dot11RFEntry.ther)
2489 {
2490 for(i = 0; i < rf; i++)
2491 OFDM_index[i] = priv->pshare->OFDM_index[i] - get_tx_tracking_index(priv, channel, i, delta, 0, 0);
2492
2493 CCK_index = priv->pshare->CCK_index - get_tx_tracking_index(priv, channel, 0, delta, 0, 1);
2494 }
2495 else
2496 {
2497 for(i = 0; i < rf; i++)
2498 OFDM_index[i] = priv->pshare->OFDM_index[i] + get_tx_tracking_index(priv, channel, i, delta, 1, 0);
2499
2500 CCK_index = priv->pshare->CCK_index + get_tx_tracking_index(priv, channel, i, delta, 1, 1);
2501 }
2502 }
2503 else
2504#endif
2505 {
2506 offset = 4;
2507
2508 if(delta > index_mapping_NUM-1)
2509 index[0] = index_mapping[offset][index_mapping_NUM-1];
2510 else
2511 index[0] = index_mapping[offset][delta];
2512
2513 if(ThermalValue > priv->pmib->dot11RFEntry.ther) {
2514 for(i = 0; i < rf; i++)
2515 OFDM_index[i] = priv->pshare->OFDM_index[i] - delta;
2516 CCK_index = priv->pshare->CCK_index - delta;
2517 }
2518 else {
2519 for(i = 0; i < rf; i++)
2520 OFDM_index[i] = priv->pshare->OFDM_index[i] + index[0];
2521 CCK_index = priv->pshare->CCK_index + index[0];
2522 }
2523 }
2524 } else if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G) {
2525 for(i = 0; i < rf; i++){
2526
2527#if defined(RTL8192D_INT_PA)
2528
2529 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY && priv->pshare->wlandev_idx==1) //MAC 1 5G
2530 bInteralPA[i] = priv->pshare->phw->InternalPA5G[1];
2531 else
2532 bInteralPA[i] = priv->pshare->phw->InternalPA5G[i];
2533
2534 if(bInteralPA[i]) {
2535 if(priv->pshare->wlandev_idx == 1 || i == 1/*rf*/)
2536 offset = 4;
2537 else
2538 offset = 0;
2539 if(channel >= 100 && channel <= 165)
2540 offset += 2;
2541 }
2542 else
2543#endif
2544 {
2545 if(priv->pshare->wlandev_idx == 1 || i == 1)
2546 offset = 2;
2547 else
2548 offset = 0;
2549 }
2550
2551
2552#ifdef HIGH_POWER_EXT_PA //Modify HP tracking table, from Arthur 2012.02.13
2553 if(i == 0)
2554 {
2555 if(channel <= 99)
2556 offset = 0;
2557 else if(channel <= 140)
2558 offset = 2;
2559 else
2560 offset = 4;
2561 }
2562 else
2563 {
2564 if(channel <= 99)
2565 offset = 6;
2566 else if(channel <= 140)
2567 offset = 8;
2568 else
2569 offset = 10;
2570 }
2571#endif
2572
2573
2574 if(ThermalValue > priv->pmib->dot11RFEntry.ther) //set larger Tx power
2575 offset++;
2576#if defined(RTL8192D_INT_PA)
2577 if(bInteralPA[i]) {
2578 if(delta > index_mapping_NUM-1)
2579 index[i] = index_mapping_internalPA[offset][index_mapping_NUM-1];
2580 else
2581 index[i] = index_mapping_internalPA[offset][delta];
2582 } else
2583#endif
2584 {
2585 if(delta > index_mapping_NUM-1)
2586 index[i] = index_mapping[offset][index_mapping_NUM-1];
2587 else
2588 index[i] = index_mapping[offset][delta];
2589 }
2590
2591
2592
2593#ifdef _TRACKING_TABLE_FILE
2594 if (priv->pshare->rf_ft_var.pwr_track_file)
2595 {
2596 if(ThermalValue > priv->pmib->dot11RFEntry.ther)
2597 index[i] = get_tx_tracking_index(priv, channel, i, delta, 0, 0);
2598 else
2599 index[i] = get_tx_tracking_index(priv, channel, i, delta, 1, 0);
2600 }
2601 else
2602#endif
2603 {
2604#ifdef HIGH_POWER_EXT_PA //Modify HP tracking table, from Arthur 2012.02.13
2605 {
2606 if(delta > index_mapping_NUM-1)
2607 index[i] = index_mapping_HighPower_PA[offset][index_mapping_NUM-1];
2608 else
2609 index[i] = index_mapping_HighPower_PA[offset][delta];
2610
2611 //printk("\n\n offset = %d delta = %d \n", offset, delta);
2612 //printk("index[%d]= %d\n\n", i, index[i]);
2613 }
2614#endif
2615 }
2616
2617
2618
2619 if(ThermalValue > priv->pmib->dot11RFEntry.ther) //set larger Tx power
2620 {
2621#if 0
2622 if(bInteralPA[i] && ThermalValue > 0x12)
2623 index[i] = ((delta/2)*3+(delta%2));
2624#endif
2625 OFDM_index[i] = priv->pshare->OFDM_index[i] -index[i];
2626 }
2627 else
2628 {
2629 OFDM_index[i] = priv->pshare->OFDM_index[i] + index[i];
2630 }
2631 }
2632 }
2633
2634 if(is2T)
2635 {
2636 DEBUG_INFO("temp OFDM_A_index=0x%x, OFDM_B_index=0x%x, CCK_index=0x%x\n",
2637 priv->pshare->OFDM_index[0], priv->pshare->OFDM_index[1], priv->pshare->CCK_index);
2638 }
2639 else
2640 {
2641 DEBUG_INFO("temp OFDM_A_index=0x%x, CCK_index=0x%x\n",
2642 priv->pshare->OFDM_index[0], priv->pshare->CCK_index);
2643 }
2644
2645 for(i = 0; i < rf; i++)
2646 {
2647 if(OFDM_index[i] > OFDM_TABLE_SIZE_92D-1) {
2648 OFDM_index[i] = OFDM_TABLE_SIZE_92D-1;
2649 }
2650 else if(priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G) {
2651 if (OFDM_index[i] < (OFDM_min_index_internalPA))
2652 OFDM_index[i] = (OFDM_min_index_internalPA);
2653 } else if(bInteralPA[i]) {
2654#ifdef DPK_92D
2655 if (OFDM_index[i] < (OFDM_min_index_internalPA+ priv->pshare->OFDM_min_index_internalPA_DPK[i]))
2656 {
2657 priv->pshare->TxPowerLevelDPK[i] = OFDM_min_index_internalPA+ priv->pshare->OFDM_min_index_internalPA_DPK[i]-OFDM_index[i];
2658 OFDM_index[i] = (OFDM_min_index_internalPA+ priv->pshare->OFDM_min_index_internalPA_DPK[i]);
2659 }
2660 else
2661 {
2662 priv->pshare->TxPowerLevelDPK[i] = 0;
2663 }
2664#else
2665 if (OFDM_index[i] < (OFDM_min_index_internalPA))
2666 {
2667 OFDM_index[i] = (OFDM_min_index_internalPA);
2668 }
2669#endif
2670 } else if(OFDM_index[i] < OFDM_min_index) {
2671#ifdef HIGH_POWER_EXT_PA //Modify HP tracking table, from Arthur 2012.02.13
2672#else
2673 OFDM_index[i] = OFDM_min_index;
2674#endif
2675 }
2676 }
2677
2678 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G){
2679 if(CCK_index > CCK_TABLE_SIZE_92D-1)
2680 CCK_index = CCK_TABLE_SIZE_92D-1;
2681 else if (CCK_index < 0)
2682 CCK_index = 0;
2683 }
2684
2685 if(is2T) {
2686 DEBUG_INFO("new OFDM_A_index=0x%x, OFDM_B_index=0x%x, CCK_index=0x%x\n",
2687 OFDM_index[0], OFDM_index[1], CCK_index);
2688 }
2689 else
2690 {
2691 DEBUG_INFO("new OFDM_A_index=0x%x, CCK_index=0x%x\n",
2692 OFDM_index[0], CCK_index);
2693 }
2694 }
2695
2696 //Config by SwingTable
2697 {
2698 //Adujst OFDM Ant_A according to IQK result
2699 ele_D = (OFDMSwingTable_92D[(unsigned int)OFDM_index[0]] & 0xFFC00000)>>22;
2700 X = priv->pshare->RegE94;
2701 Y = priv->pshare->RegE9C;
2702
2703 if(X != 0 && (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G)){
2704 if ((X & 0x00000200) != 0)
2705 X = X | 0xFFFFFC00;
2706 ele_A = ((X * ele_D)>>8)&0x000003FF;
2707
2708 //new element C = element D x Y
2709 if ((Y & 0x00000200) != 0)
2710 Y = Y | 0xFFFFFC00;
2711 ele_C = ((Y * ele_D)>>8)&0x000003FF;
2712
2713 //wirte new elements A, C, D to regC80 and regC94, element B is always 0
2714 value32 = (ele_D<<22)|((ele_C&0x3F)<<16)|ele_A;
2715 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, value32);
2716
2717 value32 = (ele_C&0x000003C0)>>6;
2718 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, value32);
2719
2720 value32 = ((X * ele_D)>>7)&0x01;
2721 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), value32);
2722
2723 }
2724 else
2725 {
2726 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord, OFDMSwingTable_92D[(unsigned int)OFDM_index[0]]);
2727 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, bMaskH4Bits, 0x00);
2728 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(24), 0x00);
2729#ifdef MP_TEST
2730 if ((priv->pshare->rf_ft_var.mp_specific) && (!is2T)) {
2731 unsigned char str[50];
2732 sprintf(str, "patha=%d,pathb=%d", priv->pshare->mp_txpwr_patha, priv->pshare->mp_txpwr_pathb);
2733 mp_set_tx_power(priv, str);
2734 }
2735#endif
2736 }
2737 DEBUG_INFO("TxPwrTracking for interface %d path A: X = 0x%x, Y = 0x%x ele_A = 0x%x ele_C = 0x%x ele_D = 0x%x\n",
2738 priv->pshare->wlandev_idx, X, Y, ele_A, ele_C, ele_D);
2739
2740
2741 if(priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G)
2742 {
2743 //Adjust CCK according to IQK result
2744 set_CCK_swing_index(priv, CCK_index);
2745 }
2746
2747 if(is2T)
2748 {
2749 ele_D = (OFDMSwingTable_92D[(unsigned int)OFDM_index[1]] & 0xFFC00000)>>22;
2750
2751 //new element A = element D x X
2752 X = priv->pshare->RegEB4;
2753 Y = priv->pshare->RegEBC;
2754
2755 if(X != 0 && (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G)){
2756 if ((X & 0x00000200) != 0) //consider minus
2757 X = X | 0xFFFFFC00;
2758 ele_A = ((X * ele_D)>>8)&0x000003FF;
2759
2760 //new element C = element D x Y
2761 if ((Y & 0x00000200) != 0)
2762 Y = Y | 0xFFFFFC00;
2763 ele_C = ((Y * ele_D)>>8)&0x00003FF;
2764
2765 //wirte new elements A, C, D to regC88 and regC9C, element B is always 0
2766 value32=(ele_D<<22)|((ele_C&0x3F)<<16) |ele_A;
2767 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, value32);
2768
2769 value32 = (ele_C&0x000003C0)>>6;
2770 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, value32);
2771
2772 value32 = ((X * ele_D)>>7)&0x01;
2773 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), value32);
2774
2775 }
2776 else{
2777 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord, OFDMSwingTable_92D[(unsigned int)OFDM_index[1]]);
2778 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, bMaskH4Bits, 0x00);
2779 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(28), 0x00);
2780#ifdef MP_TEST
2781 if ((priv->pshare->rf_ft_var.mp_specific) ) {
2782 unsigned char str[50];
2783 sprintf(str, "patha=%d,pathb=%d", priv->pshare->mp_txpwr_patha, priv->pshare->mp_txpwr_pathb);
2784 mp_set_tx_power(priv, str);
2785
2786 }
2787#endif
2788 }
2789
2790 DEBUG_INFO("TxPwrTracking path B: X = 0x%x, Y = 0x%x ele_A = 0x%x ele_C = 0x%x ele_D = 0x%x\n",
2791 X, Y, ele_A, ele_C, ele_D);
2792 }
2793
2794 DEBUG_INFO("TxPwrTracking 0xc80 = 0x%x, 0xc94 = 0x%x RF 0x24 = 0x%x\n", phy_query_bb_reg(priv, 0xc80, bMaskDWord),
2795 phy_query_bb_reg(priv, 0xc94, bMaskDWord), phy_query_rf_reg(priv, RF_PATH_A, 0x24, bMask20Bits,1));
2796 }
2797 }
2798
2799TxPowerDPK:
2800#ifdef DPK_92D
2801 {
2802 char bNOPG = FALSE;
2803 unsigned char pwrlevelHT40_1S_A = priv->pmib->dot11RFEntry.pwrlevelHT40_1S_A[channel-1];
2804 if (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G)
2805 pwrlevelHT40_1S_A = priv->pmib->dot11RFEntry.pwrlevel5GHT40_1S_A[channel-1];
2806#ifdef CONFIG_RTL_92D_DMDP
2807 if ((priv->pmib->dot11RFEntry.macPhyMode==DUALMAC_DUALPHY) &&
2808 (priv->pshare->wlandev_idx == 1) && (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G))
2809 pwrlevelHT40_1S_A = priv->pmib->dot11RFEntry.pwrlevel5GHT40_1S_B[channel-1];
2810 if (pwrlevelHT40_1S_A == 0)
2811 bNOPG = TRUE;
2812#endif
2813
2814 //for DPK
2815 if(delta_DPK > 0 && !bNOPG /*&& pHalData->bDPKdone*/) {
2816 for(i = 0; i < rf; i++) {
2817 if(bInteralPA[i] && priv->pshare->bDPKdone[i]) {
2818 if(ThermalValue > priv->pmib->dot11RFEntry.ther)
2819 value_DPK = priv->pshare->index_mapping_DPK_current[i*2][ThermalValue-priv->pmib->dot11RFEntry.ther];
2820 else
2821 value_DPK = priv->pshare->index_mapping_DPK_current[i*2+1][priv->pmib->dot11RFEntry.ther-ThermalValue];
2822
2823 phy_set_bb_reg(priv, index_DPK[i], 0x7c00, value_DPK);
2824 }
2825 }
2826 priv->pshare->ThermalValue_DPK = ThermalValue;
2827 }
2828 }
2829#endif
2830 priv->pshare->pwr_trk_ongoing = 0;
2831#if 1
2832 if ((delta_IQK > priv->pshare->delta_iqk) && (priv->pshare->delta_iqk != 0)) {
2833 priv->pshare->thermal_value_iqk = ThermalValue;
2834 phy_iq_calibrate(priv);
2835 }
2836#endif
2837
2838#ifdef RX_GAIN_TRACK_92D
2839 if(priv->pmib->dot11RFEntry.ther && (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G) &&
2840 (ThermalValue < priv->pmib->dot11RFEntry.ther)) {
2841 priv->pshare->ThermalValue_RxGain = ThermalValue;
2842 rx_gain_tracking_92D(priv);
2843 }
2844#endif
2845
2846 //update thermal meter value
2847 priv->pshare->thermal_value = ThermalValue;
2848 }
2849}
2850
2851#endif
2852
2853//3 ============================================================
2854//3 EDCA Turbo
2855//3 ============================================================
2856
2857void init_EDCA_para(struct rtl8192cd_priv *priv, int mode)
2858{
2859 static unsigned int slot_time, VO_TXOP, VI_TXOP, sifs_time;
2860
2861 struct ParaRecord EDCA[4];
2862#ifdef RTL_MANUAL_EDCA
2863 //unsigned char acm_bitmap;
2864#endif
2865
2866 slot_time = 20;
2867 sifs_time = 10;
2868
2869 if (mode & WIRELESS_11N)
2870 sifs_time = 16;
2871
2872#ifdef RTL_MANUAL_EDCA
2873 if( priv->pmib->dot11QosEntry.ManualEDCA ) {
2874 memset(EDCA, 0, 4*sizeof(struct ParaRecord));
2875 if( OPMODE & WIFI_AP_STATE )
2876 memcpy(EDCA, priv->pmib->dot11QosEntry.AP_manualEDCA, 4*sizeof(struct ParaRecord));
2877 else
2878 memcpy(EDCA, priv->pmib->dot11QosEntry.STA_manualEDCA, 4*sizeof(struct ParaRecord));
2879
2880
2881 if ((mode & WIRELESS_11N) ||
2882 (mode & WIRELESS_11G)) {
2883 slot_time = 9;
2884 }
2885
2886
2887 RTL_W32(EDCA_VO_PARA, (EDCA[VO].TXOPlimit << 16) | (EDCA[VO].ECWmax << 12) | (EDCA[VO].ECWmin << 8) | (sifs_time + EDCA[VO].AIFSN * slot_time));
2888#ifdef WIFI_WMM
2889 if (QOS_ENABLE)
2890 RTL_W32(EDCA_VI_PARA, (EDCA[VI].TXOPlimit << 16) | (EDCA[VI].ECWmax << 12) | (EDCA[VI].ECWmin << 8) | (sifs_time + EDCA[VI].AIFSN * slot_time));
2891 else
2892#endif
2893 RTL_W32(EDCA_VI_PARA, (EDCA[BE].TXOPlimit << 16) | (EDCA[BE].ECWmax << 12) | (EDCA[BE].ECWmin << 8) | (sifs_time + EDCA[VI].AIFSN * slot_time));
2894
2895 RTL_W32(EDCA_BE_PARA, (EDCA[BE].TXOPlimit << 16) | (EDCA[BE].ECWmax << 12) | (EDCA[BE].ECWmin << 8) | (sifs_time + EDCA[BE].AIFSN * slot_time));
2896
2897 RTL_W32(EDCA_BK_PARA, (EDCA[BK].TXOPlimit << 16) | (EDCA[BK].ECWmax << 12) | (EDCA[BK].ECWmin << 8) | (sifs_time + EDCA[BK].AIFSN * slot_time));
2898 }else
2899#endif //RTL_MANUAL_EDCA
2900 {
2901 memset(EDCA, 0, 4*sizeof(struct ParaRecord));
2902 /* copy BE, BK from static data */
2903 if( OPMODE & WIFI_AP_STATE )
2904 memcpy(EDCA, rtl_ap_EDCA, 2*sizeof(struct ParaRecord));
2905 else
2906 memcpy(EDCA, rtl_sta_EDCA, 2*sizeof(struct ParaRecord));
2907
2908 /* VI, VO apply settings in AG by default */
2909 if( OPMODE & WIFI_AP_STATE )
2910 memcpy(&EDCA[2], &rtl_ap_EDCA[VI_AG], 2*sizeof(struct ParaRecord));
2911 else
2912 memcpy(&EDCA[2], &rtl_sta_EDCA[VI_AG], 2*sizeof(struct ParaRecord));
2913
2914 if ((mode & WIRELESS_11N) ||
2915 (mode & WIRELESS_11G)) {
2916 slot_time = 9;
2917 } else {
2918 /* replace with settings in B */
2919 if( OPMODE & WIFI_AP_STATE )
2920 memcpy(&EDCA[2], &rtl_ap_EDCA[VI], 2*sizeof(struct ParaRecord));
2921 else
2922 memcpy(&EDCA[2], &rtl_sta_EDCA[VI], 2*sizeof(struct ParaRecord));
2923 }
2924 VO_TXOP = EDCA[VO].TXOPlimit;
2925 VI_TXOP = EDCA[VI].TXOPlimit;
2926
2927 RTL_W32(EDCA_VO_PARA, (VO_TXOP << 16) | (EDCA[VO].ECWmax << 12) | (EDCA[VO].ECWmin << 8) | (sifs_time + EDCA[VO].AIFSN * slot_time));
2928#ifdef WIFI_WMM
2929 if (QOS_ENABLE)
2930 RTL_W32(EDCA_VI_PARA, (VI_TXOP << 16) | (EDCA[VI].ECWmax << 12) | (EDCA[VI].ECWmin << 8) | (sifs_time + EDCA[VI].AIFSN * slot_time));
2931 else
2932#endif
2933 RTL_W32(EDCA_VI_PARA, (EDCA[BK].ECWmax << 12) | (EDCA[BK].ECWmin << 8) | (sifs_time + EDCA[VI].AIFSN * slot_time));
2934
2935 RTL_W32(EDCA_BE_PARA, ((EDCA[BE].ECWmax) << 12) | (EDCA[BE].ECWmin << 8) | (sifs_time + EDCA[BE].AIFSN * slot_time));
2936 RTL_W32(EDCA_BK_PARA, (EDCA[BK].ECWmax << 12) | (EDCA[BK].ECWmin << 8) | (sifs_time + EDCA[BK].AIFSN * slot_time));
2937
2938
2939 RTL_W8(ACMHWCTRL, 0x00);
2940 }
2941
2942 priv->pshare->iot_mode_enable = 0;
2943 if (priv->pshare->rf_ft_var.wifi_beq_iot)
2944 priv->pshare->iot_mode_VI_exist = 0;
2945 priv->pshare->iot_mode_VO_exist = 0;
2946
2947#ifdef WMM_VIBE_PRI
2948 priv->pshare->iot_mode_BE_exist = 0;
2949#endif
2950#ifdef WMM_BEBK_PRI
2951 priv->pshare->iot_mode_BK_exist = 0;
2952#endif
2953#ifdef LOW_TP_TXOP
2954 priv->pshare->BE_cwmax_enhance = 0;
2955#endif
2956}
2957
2958void choose_IOT_main_sta(struct rtl8192cd_priv *priv, struct stat_info *pstat)
2959{
2960 int i, assoc_num = GET_ROOT(priv)->assoc_num;
2961#ifdef MBSSID
2962 if (GET_ROOT(priv)->pmib->miscEntry.vap_enable){
2963 for (i=0; i<RTL8192CD_NUM_VWLAN; ++i)
2964 assoc_num += GET_ROOT(priv)->pvap_priv[i]-> assoc_num;
2965 }
2966#endif
2967#ifdef UNIVERSAL_REPEATER
2968 if (IS_DRV_OPEN(GET_VXD_PRIV(GET_ROOT(priv))))
2969 assoc_num += GET_VXD_PRIV(GET_ROOT(priv))-> assoc_num;
2970#endif
2971#ifdef WDS
2972 if(GET_ROOT(priv)->pmib->dot11WdsInfo.wdsEnabled)
2973 assoc_num ++;
2974#endif
2975
2976 if (priv->pshare->rf_ft_var.low_tp_no_aggr) {
2977 unsigned long sta_tp = (pstat->current_tx_bytes + pstat->current_rx_bytes) >> 17;
2978 if (!pstat->low_tp_disable_ampdu && sta_tp <= 2)
2979 pstat->low_tp_disable_ampdu = 1;
2980 else if (pstat->low_tp_disable_ampdu && sta_tp >= 5)
2981 pstat->low_tp_disable_ampdu = 0;
2982 }
2983
2984 if ((GET_ROOT(priv)->up_time % 2) == 0) {
2985 unsigned int tx_2s_avg = 0;
2986 unsigned int rx_2s_avg = 0;
2987 int i=0, aggReady=0;
2988 unsigned long total_sum = (priv->pshare->current_tx_bytes+priv->pshare->current_rx_bytes);
2989
2990 pstat->current_tx_bytes += pstat->tx_byte_cnt;
2991 pstat->current_rx_bytes += pstat->rx_byte_cnt;
2992
2993 if (total_sum != 0) {
2994 if (total_sum <= 1000000) {
2995 tx_2s_avg = (unsigned int)((pstat->current_tx_bytes*100) / total_sum);
2996 rx_2s_avg = (unsigned int)((pstat->current_rx_bytes*100) / total_sum);
2997 } else {
2998 tx_2s_avg = (unsigned int)(pstat->current_tx_bytes / (total_sum / 100));
2999 rx_2s_avg = (unsigned int)(pstat->current_rx_bytes / (total_sum / 100));
3000 }
3001 }
3002
3003 for(i=0; i<8; i++)
3004 aggReady += (pstat->ADDBA_ready[i]);
3005
3006 if( (pstat->IOTPeer==HT_IOT_PEER_INTEL)
3007 || (pstat->ht_cap_len && aggReady)) {
3008
3009 if ((assoc_num==1) || (tx_2s_avg + rx_2s_avg >= 25)) {
3010 priv->pshare->highTP_found_pstat = pstat;
3011 } /*this STA's TXRX packet very close AP's total TXRX packet then let it as highTP_found_pstat*/
3012 else if(RTL_ABS((pstat->current_tx_bytes + pstat->current_rx_bytes) , total_sum)<50){
3013 priv->pshare->highTP_found_pstat = pstat;
3014 }
3015#ifdef CLIENT_MODE
3016 if (OPMODE & WIFI_STATION_STATE) {
3017// if(pstat->is_ralink_sta && ((tx_2s_avg + rx_2s_avg) >= 45))
3018 if(pstat->IOTPeer == HT_IOT_PEER_RALINK && ((tx_2s_avg + rx_2s_avg) >= 45))
3019 priv->pshare->highTP_found_pstat = pstat;
3020 }
3021#endif
3022 }
3023 }
3024 else {
3025 pstat->current_tx_bytes = pstat->tx_byte_cnt;
3026 pstat->current_rx_bytes = pstat->rx_byte_cnt;
3027 }
3028}
3029
3030
3031void rxBB_dm(struct rtl8192cd_priv *priv)
3032{
3033 if ((priv->up_time % 3) == 1) {
3034 if (priv->pshare->rssi_min != 0xff) {
3035 if (priv->pshare->rf_ft_var.dig_enable) {
3036 // for DIG checking
3037 check_DIG_by_rssi(priv, priv->pshare->rssi_min);
3038 }
3039//#ifdef INTERFERENCE_CONTROL
3040 if (priv->pshare->rf_ft_var.nbi_filter_enable) {
3041 check_NBI_by_rssi(priv, priv->pshare->rssi_min);
3042 }
3043//#endif
3044 }
3045
3046#ifdef MP_TEST
3047 if (!((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific))
3048#endif
3049 {
3050#ifdef HIGH_POWER_EXT_LNA
3051 if (!priv->pshare->rf_ft_var.use_ext_lna)
3052#endif
3053 CCK_CCA_dynamic_enhance(priv, priv->pshare->rssi_min);
3054 }
3055 }
3056 if (priv->pshare->rf_ft_var.adaptivity_enable){
3057 unsigned char IGI;
3058 IGI = RTL_R8(0xc50);
3059 rtl8192cd_Adaptivity(priv,IGI);
3060 }
3061}
3062
3063/*
3064 * IOT related functions
3065 */
3066void IOT_engine(struct rtl8192cd_priv *priv)
3067{
3068#ifdef WIFI_WMM
3069 unsigned int switch_turbo = 0;
3070#endif
3071#ifdef MBSSID
3072 int i;
3073#endif
3074 int assoc_num = priv->assoc_num;
3075 struct stat_info *pstat = priv->pshare->highTP_found_pstat;
3076
3077 if(pstat) {
3078 if((pstat->tx_avarage + pstat->rx_avarage) < (1<<17)) // 1M bps
3079 pstat = NULL;
3080 }
3081#if defined(RTL_MANUAL_EDCA) && defined(WIFI_WMM)
3082 if(priv->pmib->dot11QosEntry.ManualEDCA)
3083 return ;
3084#endif
3085
3086#ifdef MBSSID
3087 if (GET_ROOT(priv)->pmib->miscEntry.vap_enable){
3088 for (i=0; i<RTL8192CD_NUM_VWLAN; ++i)
3089 assoc_num += GET_ROOT(priv)->pvap_priv[i]-> assoc_num;
3090 }
3091#endif
3092#ifdef UNIVERSAL_REPEATER
3093 if (IS_DRV_OPEN(GET_VXD_PRIV(GET_ROOT(priv))))
3094 assoc_num += GET_VXD_PRIV(GET_ROOT(priv))-> assoc_num;
3095#endif
3096#ifdef WDS
3097 if(GET_ROOT(priv)->pmib->dot11WdsInfo.wdsEnabled)
3098 assoc_num ++;
3099#endif
3100
3101#ifdef WIFI_WMM
3102 if (QOS_ENABLE) {
3103 if (!priv->pmib->dot11OperationEntry.wifi_specific ||
3104 ((OPMODE & WIFI_AP_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific))) {
3105 if (priv->pshare->iot_mode_enable &&
3106 ((priv->pshare->phw->VO_pkt_count > 50) ||
3107 (priv->pshare->phw->VI_pkt_count > 50) ||
3108 (priv->pshare->phw->BK_pkt_count > 50))) {
3109 priv->pshare->iot_mode_enable = 0;
3110 switch_turbo++;
3111 } else if ((!priv->pshare->iot_mode_enable) &&
3112 ((priv->pshare->phw->VO_pkt_count < 50) &&
3113 (priv->pshare->phw->VI_pkt_count < 50) &&
3114 (priv->pshare->phw->BK_pkt_count < 50))) {
3115 priv->pshare->iot_mode_enable++;
3116 switch_turbo++;
3117 }
3118 }
3119
3120 if ((OPMODE & WIFI_AP_STATE) && priv->pmib->dot11OperationEntry.wifi_specific) {
3121 if (!priv->pshare->iot_mode_VO_exist && (priv->pshare->phw->VO_pkt_count > 50)) {
3122 priv->pshare->iot_mode_VO_exist++;
3123 switch_turbo++;
3124 } else if (priv->pshare->iot_mode_VO_exist && (priv->pshare->phw->VO_pkt_count < 50)) {
3125 priv->pshare->iot_mode_VO_exist = 0;
3126 switch_turbo++;
3127 }
3128
3129#ifdef WMM_VIBE_PRI
3130 if (priv->pshare->iot_mode_VO_exist) {
3131 //printk("[%s %d] BE_pkt_count=%d\n", __FUNCTION__, __LINE__, priv->pshare->phw->BE_pkt_count);
3132 if (!priv->pshare->iot_mode_BE_exist && (priv->pshare->phw->BE_pkt_count > 250)) {
3133 priv->pshare->iot_mode_BE_exist++;
3134 switch_turbo++;
3135 } else if (priv->pshare->iot_mode_BE_exist && (priv->pshare->phw->BE_pkt_count < 250)) {
3136 priv->pshare->iot_mode_BE_exist = 0;
3137 switch_turbo++;
3138 }
3139 }
3140#endif
3141
3142#ifdef WMM_BEBK_PRI
3143 if (priv->pshare->phw->BE_pkt_count) {
3144 //printk("[%s %d] BK_pkt_count=%d\n", __FUNCTION__, __LINE__, priv->pshare->phw->BK_pkt_count);
3145 if (!priv->pshare->iot_mode_BK_exist && (priv->pshare->phw->BK_pkt_count > 250)) {
3146 priv->pshare->iot_mode_BK_exist++;
3147 switch_turbo++;
3148 EDEBUG("switch_turbo++\n");
3149 } else if (priv->pshare->iot_mode_BE_exist && (priv->pshare->phw->BK_pkt_count < 250)) {
3150 priv->pshare->iot_mode_BK_exist = 0;
3151 switch_turbo++;
3152 EDEBUG("switch_turbo++\n");
3153 }
3154 }
3155#endif
3156
3157 if (priv->pshare->rf_ft_var.wifi_beq_iot) {
3158 if (!priv->pshare->iot_mode_VI_exist && (priv->pshare->phw->VI_rx_pkt_count > 50)) {
3159 priv->pshare->iot_mode_VI_exist++;
3160 switch_turbo++;
3161 } else if (priv->pshare->iot_mode_VI_exist && (priv->pshare->phw->VI_rx_pkt_count < 50)) {
3162 priv->pshare->iot_mode_VI_exist = 0;
3163 switch_turbo++;
3164 }
3165 }
3166
3167#ifdef CONFIG_RTL_92D_DMDP
3168 if ((GET_CHIP_VER(priv) == VERSION_8192D) && (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)) {
3169 unsigned int tp = (unsigned int)(priv->ext_stats.tx_avarage>>17) + (unsigned int)(priv->ext_stats.rx_avarage>>17);
3170 if (priv->pshare->rf_ft_var.wifi_beq_iot) {
3171 if (priv->pshare->wifi_beq_lower && priv->pshare->iot_mode_VI_exist && tp <= 20) {
3172 priv->pshare->wifi_beq_lower= 0;
3173 switch_turbo++;
3174 } else if (!priv->pshare->wifi_beq_lower&& (!priv->pshare->iot_mode_VI_exist || tp > 20)) {
3175 priv->pshare->wifi_beq_lower= 1;
3176 switch_turbo++;
3177 }
3178 }
3179 }
3180#endif
3181 }
3182
3183#ifdef CLIENT_MODE
3184 if ((OPMODE & WIFI_STATION_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific))
3185 {
3186 if (priv->pshare->iot_mode_enable &&
3187 (((priv->pshare->phw->VO_pkt_count > 50) ||
3188 (priv->pshare->phw->VI_pkt_count > 50) ||
3189 (priv->pshare->phw->BK_pkt_count > 50)) ||
3190 (pstat && (!pstat->ADDBA_ready[0]) & (!pstat->ADDBA_ready[3]))))
3191 {
3192 priv->pshare->iot_mode_enable = 0;
3193 switch_turbo++;
3194 }
3195 else if ((!priv->pshare->iot_mode_enable) &&
3196 (((priv->pshare->phw->VO_pkt_count < 50) &&
3197 (priv->pshare->phw->VI_pkt_count < 50) &&
3198 (priv->pshare->phw->BK_pkt_count < 50)) &&
3199 (pstat && (pstat->ADDBA_ready[0] | pstat->ADDBA_ready[3]))))
3200 {
3201 priv->pshare->iot_mode_enable++;
3202 switch_turbo++;
3203 }
3204 }
3205#endif
3206
3207 priv->pshare->phw->VO_pkt_count = 0;
3208 priv->pshare->phw->VI_pkt_count = 0;
3209 if (priv->pshare->rf_ft_var.wifi_beq_iot)
3210 priv->pshare->phw->VI_rx_pkt_count = 0;
3211 priv->pshare->phw->BK_pkt_count = 0;
3212 priv->pshare->phw->BE_pkt_count = 0;
3213 }
3214#endif
3215
3216 if ((priv->up_time % 2) == 0) {
3217 /*
3218 * decide EDCA content for different chip vendor
3219 */
3220#ifdef WIFI_WMM
3221 if (QOS_ENABLE && (!priv->pmib->dot11OperationEntry.wifi_specific ||
3222 ((OPMODE & WIFI_AP_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific == 2))
3223#ifdef CLIENT_MODE
3224 || ((OPMODE & WIFI_STATION_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific == 2))
3225#endif
3226 )) {
3227 if (pstat && pstat->rssi >= priv->pshare->rf_ft_var.txop_enlarge_upper) {
3228#ifdef LOW_TP_TXOP
3229
3230
3231 if (pstat->IOTPeer==HT_IOT_PEER_INTEL)
3232
3233 {
3234
3235 if (priv->pshare->txop_enlarge != 0xe) {
3236 priv->pshare->txop_enlarge = 0xe;
3237 if (priv->pshare->iot_mode_enable)
3238 switch_turbo++;
3239 }
3240 } else if (priv->pshare->txop_enlarge != 2) {
3241 priv->pshare->txop_enlarge = 2;
3242 if (priv->pshare->iot_mode_enable)
3243 switch_turbo++;
3244 }
3245#else //LOW_TP_TXOP
3246
3247 if (priv->pshare->txop_enlarge != 2) {
3248
3249
3250 {
3251 if (pstat->IOTPeer==HT_IOT_PEER_INTEL)
3252 priv->pshare->txop_enlarge = 0xe;
3253 else if (pstat->IOTPeer==HT_IOT_PEER_RALINK)
3254 priv->pshare->txop_enlarge = 0xd;
3255 else
3256 priv->pshare->txop_enlarge = 2;
3257
3258 }
3259
3260 if (priv->pshare->iot_mode_enable)
3261 switch_turbo++;
3262 }
3263#endif //LOW_TP_TXOP
3264 } else if (!pstat || pstat->rssi < priv->pshare->rf_ft_var.txop_enlarge_lower) {
3265 if (priv->pshare->txop_enlarge) {
3266 priv->pshare->txop_enlarge = 0;
3267 if (priv->pshare->iot_mode_enable)
3268 switch_turbo++;
3269 }
3270 }
3271#ifdef LOW_TP_TXOP
3272 // for Intel IOT, need to enlarge CW MAX from 6 to 10
3273
3274
3275 if (pstat && pstat->IOTPeer==HT_IOT_PEER_INTEL && (((pstat->tx_avarage+pstat->rx_avarage)>>10) <
3276 priv->pshare->rf_ft_var.cwmax_enhance_thd))
3277
3278 {
3279 if (!priv->pshare->BE_cwmax_enhance && priv->pshare->iot_mode_enable) {
3280 priv->pshare->BE_cwmax_enhance = 1;
3281 switch_turbo++;
3282 }
3283 } else {
3284 if (priv->pshare->BE_cwmax_enhance) {
3285 priv->pshare->BE_cwmax_enhance = 0;
3286 switch_turbo++;
3287 }
3288 }
3289#endif
3290 }
3291#endif
3292
3293 priv->pshare->current_tx_bytes = 0;
3294 priv->pshare->current_rx_bytes = 0;
3295 }
3296
3297#ifdef SW_TX_QUEUE
3298if (GET_HCI_TYPE(priv) == RTL_HCI_PCIE) {
3299 if ((assoc_num > 1) && (AMPDU_ENABLE))
3300 {
3301 unsigned int total_tp = (unsigned int)(priv->ext_stats.tx_avarage>>17)+(unsigned int)(priv->ext_stats.rx_avarage>>17);
3302 if(total_tp > 0) {
3303 if (((unsigned int)(priv->ext_stats.tx_avarage>>17) * 100/total_tp) > 30)
3304 {
3305 if (priv->pshare->swq_txmac_chg >= priv->pshare->rf_ft_var.swq_en_highthd)
3306 {
3307 if ((priv->pshare->swq_en == 0))
3308 {
3309 switch_turbo++;
3310 if (priv->pshare->txop_enlarge == 0)
3311 priv->pshare->txop_enlarge = 2;
3312 priv->pshare->swq_en = 1;
3313 priv->pshare->swqen_keeptime = priv->up_time;
3314 }
3315 else
3316 {
3317 if ((switch_turbo > 0) && (priv->pshare->txop_enlarge == 0) && (priv->pshare->iot_mode_enable != 0))
3318 {
3319 priv->pshare->txop_enlarge = 2;
3320 switch_turbo--;
3321 }
3322 }
3323 }
3324 else if(priv->pshare->swq_txmac_chg <= priv->pshare->rf_ft_var.swq_dis_lowthd)
3325 {
3326 priv->pshare->swq_en = 0;
3327 priv->pshare->swqen_keeptime = 0;
3328 }
3329 else if ((priv->pshare->swq_en == 1) && (switch_turbo > 0) && (priv->pshare->txop_enlarge == 0) && (priv->pshare->iot_mode_enable != 0))
3330 {
3331 priv->pshare->txop_enlarge = 2;
3332 switch_turbo--;
3333 }
3334 }
3335 else if (((unsigned int)(priv->ext_stats.tx_avarage>>17) * 100/total_tp) < 20)
3336 {
3337 priv->pshare->swq_en = 0;
3338 priv->pshare->swqen_keeptime = 0;
3339 }
3340 }
3341 //debug msg
3342 //printk("swq=%d,sw=%d,en=%d,mode=%d\n", priv->pshare->swq_en, switch_turbo, priv->pshare->txop_enlarge, priv->pshare->iot_mode_enable);
3343 }
3344#if 1//defined(CONFIG_RTL_819XD)
3345 else if( (assoc_num == 1) && (priv->up_time % 2)== 0 && (AMPDU_ENABLE) ) {
3346 //if (pstat) {
3347// if ((pstat) && pstat->is_intel_sta) {
3348 if ((pstat) && pstat->IOTPeer == HT_IOT_PEER_INTEL) {
3349// int en_thd = 14417920;
3350 //if ((priv->pshare->swq_en == 0) && (pstat->current_tx_bytes > en_thd) && (pstat->current_rx_bytes > en_thd) ) { //50Mbps
3351 if ((pstat->current_tx_bytes > 14417920) && (priv->pshare->swq_en == 0)) { // && (pstat->current_rx_bytes > 14417920) && (priv->pshare->swq_en == 0)) { //55Mbps
3352 priv->pshare->swq_en = 1;
3353 priv->pshare->swqen_keeptime = priv->up_time;
3354 }
3355 //else if ((priv->pshare->swq_en == 1) && ((pstat->tx_avarage < 4587520) || (pstat->rx_avarage < 4587520))) { //35Mbps
3356 else if ((pstat->tx_avarage < 9175040) && (priv->pshare->swq_en == 1)) { //35Mbps
3357 priv->pshare->swq_en = 0;
3358 priv->pshare->swqen_keeptime = 0;
3359 }
3360 }
3361 else {
3362 priv->pshare->swq_en = 0;
3363 priv->pshare->swqen_keeptime = 0;
3364 }
3365 }
3366#endif
3367}
3368#endif
3369
3370#ifdef WIFI_WMM
3371#ifdef LOW_TP_TXOP
3372 if ((!priv->pmib->dot11OperationEntry.wifi_specific || (priv->pmib->dot11OperationEntry.wifi_specific == 2))
3373 && QOS_ENABLE) {
3374
3375 if (switch_turbo || priv->pshare->rf_ft_var.low_tp_txop) {
3376 unsigned int thd_tp;
3377 unsigned char under_thd;
3378 unsigned int curr_tp;
3379
3380 if (priv->pmib->dot11BssType.net_work_type & (WIRELESS_11N | WIRELESS_11G))
3381 {
3382 struct rtl8192cd_priv *tmppriv;
3383 // Determine the upper bound throughput threshold.
3384 if (priv->pmib->dot11BssType.net_work_type & WIRELESS_11N) {
3385 if (priv->assoc_num && priv->assoc_num != priv->pshare->ht_sta_num)
3386 thd_tp = priv->pshare->rf_ft_var.low_tp_txop_thd_g;
3387 else
3388 thd_tp = priv->pshare->rf_ft_var.low_tp_txop_thd_n;
3389 }
3390 else
3391 thd_tp = priv->pshare->rf_ft_var.low_tp_txop_thd_g;
3392
3393 // Determine to close txop.
3394 curr_tp = (unsigned int)(priv->ext_stats.tx_avarage>>17) + (unsigned int)(priv->ext_stats.rx_avarage>>17);
3395#ifdef MBSSID
3396 if (GET_ROOT(priv)->pmib->miscEntry.vap_enable) {
3397 for (i=0; i<RTL8192CD_NUM_VWLAN; ++i) {
3398 tmppriv = GET_ROOT(priv)->pvap_priv[i];
3399 if (IS_DRV_OPEN(tmppriv))
3400 curr_tp += (unsigned int)(tmppriv->ext_stats.tx_avarage>>17) + (unsigned int)(tmppriv->ext_stats.rx_avarage>>17);
3401 }
3402 }
3403#endif
3404#ifdef UNIVERSAL_REPEATER
3405 tmppriv = GET_VXD_PRIV(GET_ROOT(priv));
3406 if (IS_DRV_OPEN(tmppriv))
3407 curr_tp += (unsigned int)(tmppriv->ext_stats.tx_avarage>>17) + (unsigned int)(tmppriv->ext_stats.rx_avarage>>17);
3408#endif
3409 if (curr_tp <= thd_tp && curr_tp >= priv->pshare->rf_ft_var.low_tp_txop_thd_low)
3410 under_thd = 1;
3411 else
3412 under_thd = 0;
3413 }
3414 else
3415 {
3416 under_thd = 0;
3417 }
3418
3419 if (switch_turbo) {
3420 priv->pshare->rf_ft_var.low_tp_txop_close = under_thd;
3421 priv->pshare->rf_ft_var.low_tp_txop_count = 0;
3422 } else if (priv->pshare->iot_mode_enable && (priv->pshare->rf_ft_var.low_tp_txop_close != under_thd)) {
3423 priv->pshare->rf_ft_var.low_tp_txop_count++;
3424 if (priv->pshare->rf_ft_var.low_tp_txop_close ||
3425 (priv->pshare->rf_ft_var.low_tp_txop_count ==priv->pshare->rf_ft_var.low_tp_txop_delay)) {
3426 priv->pshare->rf_ft_var.low_tp_txop_count = 0;
3427 priv->pshare->rf_ft_var.low_tp_txop_close = under_thd;
3428 switch_turbo++;
3429 }
3430 } else {
3431 priv->pshare->rf_ft_var.low_tp_txop_count = 0;
3432 }
3433 }
3434 }
3435#endif
3436
3437 if (switch_turbo)
3438 IOT_EDCA_switch(priv, priv->pmib->dot11BssType.net_work_type, priv->pshare->iot_mode_enable);
3439#endif
3440}
3441
3442
3443#ifdef WIFI_WMM
3444void IOT_EDCA_switch(struct rtl8192cd_priv *priv, int mode, char enable)
3445{
3446 unsigned int slot_time = 20, sifs_time = 10, BE_TXOP = 47, VI_TXOP = 94;
3447 unsigned int vi_cw_max = 4, vi_cw_min = 3, vi_aifs;
3448
3449 if (!(!priv->pmib->dot11OperationEntry.wifi_specific ||
3450 ((OPMODE & WIFI_AP_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific))
3451#ifdef CLIENT_MODE
3452 || ((OPMODE & WIFI_STATION_STATE) && (priv->pmib->dot11OperationEntry.wifi_specific))
3453#endif
3454 ))
3455 return;
3456
3457 if ((mode & WIRELESS_11N) && (priv->pshare->ht_sta_num
3458#ifdef WDS
3459 || ((OPMODE & WIFI_AP_STATE) && priv->pmib->dot11WdsInfo.wdsEnabled && priv->pmib->dot11WdsInfo.wdsNum)
3460#endif
3461 ))
3462 sifs_time = 16;
3463
3464 if (mode & (WIRELESS_11G | WIRELESS_11N)) {
3465 slot_time = 9;
3466 } else {
3467 BE_TXOP = 94;
3468 VI_TXOP = 188;
3469 }
3470
3471#if 0 //defined(CONFIG_RTL_8196D) || defined(CONFIG_RTL_8196E) || (defined(CONFIG_RTL_8197D) && !defined(CONFIG_PORT0_EXT_GIGA))
3472 if (priv->pshare->is_40m_bw)
3473 {
3474 BE_TXOP = 23;
3475 }
3476#endif
3477
3478 if ((OPMODE & WIFI_AP_STATE) && priv->pmib->dot11OperationEntry.wifi_specific) {
3479 if (priv->pshare->iot_mode_VO_exist) {
3480#ifdef WMM_VIBE_PRI
3481 if (priv->pshare->iot_mode_BE_exist) {
3482 vi_cw_max = 5;
3483 vi_cw_min = 3;
3484 vi_aifs = (sifs_time + ((OPMODE & WIFI_AP_STATE)?1:2) * slot_time);
3485 } else
3486#endif
3487 {
3488 vi_cw_max = 6;
3489 vi_cw_min = 4;
3490 vi_aifs = 0x2b;
3491 }
3492 } else {
3493 vi_aifs = (sifs_time + ((OPMODE & WIFI_AP_STATE)?1:2) * slot_time);
3494 }
3495
3496 RTL_W32(EDCA_VI_PARA, ((VI_TXOP*(1-priv->pshare->iot_mode_VO_exist)) << 16)
3497 | (vi_cw_max << 12) | (vi_cw_min << 8) | vi_aifs);
3498
3499#ifdef WMM_BEBK_PRI
3500#ifdef CONFIG_RTL_88E_SUPPORT
3501 if ((GET_CHIP_VER(priv) == VERSION_8188E ) && priv->pshare->iot_mode_BK_exist) {
3502 RTL_W32(EDCA_BK_PARA, (10 << 12) | (6 << 8) | 0x4f);
3503 }
3504#endif
3505#endif
3506 }
3507
3508 if (!enable || (priv->pshare->rf_ft_var.wifi_beq_iot && priv->pshare->iot_mode_VI_exist)) {
3509 if (priv->pshare->rf_ft_var.wifi_beq_iot && priv->pshare->iot_mode_VI_exist) {
3510#ifdef CONFIG_RTL_92D_DMDP
3511 if ((GET_CHIP_VER(priv) == VERSION_8192D) && (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)) {
3512 if (priv->pshare->wifi_beq_lower)
3513 RTL_W32(EDCA_BE_PARA, (10 << 12) | (4 << 8) | (sifs_time + 10 * slot_time));
3514 else
3515 RTL_W32(EDCA_BE_PARA, (6 << 12) | (4 << 8) | (sifs_time + 3 * slot_time));
3516 } else
3517#endif
3518 RTL_W32(EDCA_BE_PARA, (10 << 12) | (4 << 8) | 0x4f);
3519 } else {
3520 RTL_W32(EDCA_BE_PARA, (((OPMODE & WIFI_AP_STATE)?6:10) << 12) | (4 << 8)
3521 | (sifs_time + 3 * slot_time));
3522 }
3523 RTL_W16(RD_CTRL, RTL_R16(RD_CTRL) | DIS_TXOP_CFE);
3524 } else {
3525#ifdef LOW_TP_TXOP
3526 int txop;
3527 unsigned int cw_max;
3528 unsigned int txop_close;
3529
3530 cw_max = ((priv->pshare->BE_cwmax_enhance) ? 10 : 6);
3531 txop_close = ((priv->pshare->rf_ft_var.low_tp_txop && priv->pshare->rf_ft_var.low_tp_txop_close) ? 1 : 0);
3532 txop = (txop_close ? 0 : (BE_TXOP*2));
3533#endif
3534 if (priv->pshare->ht_sta_num
3535#ifdef WDS
3536 || ((OPMODE & WIFI_AP_STATE) && (mode & WIRELESS_11N) &&
3537 priv->pmib->dot11WdsInfo.wdsEnabled && priv->pmib->dot11WdsInfo.wdsNum)
3538#endif
3539 ) {
3540/*
3541 if (priv->pshare->txop_enlarge == 0xf) {
3542 // is 8192S client
3543 RTL_W32(EDCA_BE_PARA, ((BE_TXOP*2) << 16) |
3544 (6 << 12) | (4 << 8) | (sifs_time + slot_time+ 0xf)); // 0xf is 92s circuit delay
3545 priv->pshare->txop_enlarge = 2;
3546 }
3547 else
3548*/
3549 if (priv->pshare->txop_enlarge == 0xe) {
3550#ifndef LOW_TP_TXOP
3551 // is intel client, use a different edca value
3552#if 0 //defined(CONFIG_RTL_8196D) || defined(CONFIG_RTL_8196E) || (defined(CONFIG_RTL_8197D) && !defined(CONFIG_PORT0_EXT_GIGA))
3553 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (5 << 8) | 0x1f);
3554#else
3555 //RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (4 << 8) | 0x1f);
3556 if (get_rf_mimo_mode(priv) == RF_1T1R)
3557 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (5 << 8) | 0x1f);
3558 else
3559 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (8 << 12) | (5 << 8) | 0x1f);
3560
3561 RTL_W16(RD_CTRL, RTL_R16(RD_CTRL) & ~(DIS_TXOP_CFE));
3562#endif
3563 priv->pshare->txop_enlarge = 2;
3564 } else if (priv->pshare->txop_enlarge == 0xd) {
3565 // is intel ralink, use a different edca value
3566 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (4 << 12) | (3 << 8) | 0x19);
3567 priv->pshare->txop_enlarge = 2;
3568 } else {
3569 if (priv->pshare->txop_enlarge == 0)
3570 RTL_W16(RD_CTRL, RTL_R16(RD_CTRL) | DIS_TXOP_CFE);
3571
3572 if (get_rf_mimo_mode(priv) == RF_2T2R)
3573#if 0 //defined(CONFIG_RTL_8196D) || defined(CONFIG_RTL_8196E) || (defined(CONFIG_RTL_8197D) && !defined(CONFIG_PORT0_EXT_GIGA))
3574 RTL_W32(EDCA_BE_PARA, ((BE_TXOP*priv->pshare->txop_enlarge) << 16) |
3575 (6 << 12) | (5 << 8) | (sifs_time + 3 * slot_time));
3576#else
3577 RTL_W32(EDCA_BE_PARA, ((BE_TXOP*priv->pshare->txop_enlarge) << 16) |
3578 (6 << 12) | (4 << 8) | (sifs_time + 3 * slot_time));
3579#endif
3580 else
3581#if 0 //defined(CONFIG_RTL_8196D) || defined(CONFIG_RTL_8196E) || (defined(CONFIG_RTL_8197D) && !defined(CONFIG_PORT0_EXT_GIGA))
3582 RTL_W32(EDCA_BE_PARA, ((BE_TXOP*priv->pshare->txop_enlarge) << 16) |
3583 (5 << 12) | (4 << 8) | (sifs_time + 2 * slot_time));
3584#else
3585 RTL_W32(EDCA_BE_PARA, ((BE_TXOP*priv->pshare->txop_enlarge) << 16) |
3586 (5 << 12) | (3 << 8) | (sifs_time + 2 * slot_time));
3587#endif
3588
3589#else
3590 // is intel client, use a different edca value
3591 if (get_rf_mimo_mode(priv) == RF_1T1R)
3592 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (5 << 8) | 0x1f);
3593 else
3594 RTL_W32(EDCA_BE_PARA, (txop << 16) | (cw_max << 12) | (4 << 8) | 0x1f);
3595 } else {
3596 txop = (txop_close ? 0: (BE_TXOP*priv->pshare->txop_enlarge));
3597
3598 if (get_rf_mimo_mode(priv) == RF_2T2R)
3599 RTL_W32(EDCA_BE_PARA, (txop << 16) | (cw_max << 12) | (4 << 8) | (sifs_time + 3 * slot_time));
3600 else
3601 RTL_W32(EDCA_BE_PARA, (txop << 16) | (((priv->pshare->BE_cwmax_enhance) ? 10 : 5) << 12) |
3602 (3 << 8) | (sifs_time + 2 * slot_time));
3603#endif
3604 }
3605 } else {
3606#ifdef LOW_TP_TXOP
3607 RTL_W32(EDCA_BE_PARA, (txop << 16) | (cw_max << 12) | (4 << 8) | (sifs_time + 3 * slot_time));
3608#else
3609#if defined(CONFIG_RTL_8196D) || defined(CONFIG_RTL_8197DL) || defined(CONFIG_RTL_8196E) || (defined(CONFIG_RTL_8197D) && !defined(CONFIG_PORT0_EXT_GIGA))
3610 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (5 << 8) | (sifs_time + 3 * slot_time));
3611#else
3612 RTL_W32(EDCA_BE_PARA, (BE_TXOP*2 << 16) | (6 << 12) | (4 << 8) | (sifs_time + 3 * slot_time));
3613#endif
3614 if (priv->pshare->txop_enlarge == 0xe)
3615 RTL_W16(RD_CTRL, RTL_R16(RD_CTRL) & ~(DIS_TXOP_CFE));
3616 else
3617 RTL_W16(RD_CTRL, RTL_R16(RD_CTRL) | DIS_TXOP_CFE);
3618#endif
3619 }
3620/*
3621 if (priv->pmib->dot11OperationEntry.wifi_specific == 2) {
3622 RTL_W16(NAV_PROT_LEN, 0x01C0);
3623 RTL_W8(CFEND_TH, 0xFF);
3624 set_fw_reg(priv, 0xfd000ab0, 0, 0);
3625 }
3626*/
3627 }
3628}
3629
3630//end of IOT_EDCA_switch
3631//========================================================================================================
3632
3633#if 0
3634void check_NAV_prot_len(struct rtl8192cd_priv *priv, struct stat_info *pstat, unsigned int disassoc)
3635{
3636 if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11N) && pstat
3637 && pstat->ht_cap_len && pstat->is_intel_sta) {
3638 if (!disassoc && (pstat->MIMO_ps & _HT_MIMO_PS_DYNAMIC_)) {
3639 setSTABitMap(&priv->pshare->mimo_ps_dynamic_sta, pstat->cmn_info.aid);
3640 } else {
3641 clearSTABitMap(&priv->pshare->mimo_ps_dynamic_sta, pstat->cmn_info.aid);
3642 }
3643
3644#ifdef CONFIG_RTL_88E_SUPPORT
3645 if (GET_CHIP_VER(priv) != VERSION_8188E)
3646#endif
3647 {
3648 if (orSTABitMap(&priv->pshare->mimo_ps_dynamic_sta) {
3649 RTL_W8(NAV_PROT_LEN, 0x40);
3650 } else {
3651 RTL_W8(NAV_PROT_LEN, 0x20);
3652 }
3653 }
3654 }
3655}
3656#endif
3657#endif
3658
3659
3660//3 ============================================================
3661//3 FA statistic functions
3662//3 ============================================================
3663#if 0
3664
3665#if !defined(CONFIG_RTL_NEW_AUTOCH)
3666static
3667#endif
3668void reset_FA_reg(struct rtl8192cd_priv *priv)
3669{
3670#if !defined(CONFIG_RTL_NEW_AUTOCH)
3671 unsigned char value8;
3672
3673 value8 = RTL_R8(0xd03);
3674 RTL_W8(0xd03, value8 | 0x08); // regD00[27]=1 to reset these OFDM FA counters
3675 value8 = RTL_R8(0xd03);
3676 RTL_W8(0xd03, value8 & 0xF7); // regD00[27]=0 to start counting
3677 value8 = RTL_R8(0xa2d);
3678 RTL_W8(0xa2d, value8 & 0x3F); // regA2D[7:6]=00 to disable counting
3679 value8 = RTL_R8(0xa2d);
3680 RTL_W8(0xa2d, value8 | 0x80); // regA2D[7:6]=10 to enable counting
3681#else
3682 /* cck CCA */
3683 phy_set_bb_reg(priv, 0xa2c, BIT(13) | BIT(12), 0);
3684 phy_set_bb_reg(priv, 0xa2c, BIT(13) | BIT(12), 2);
3685 /* cck FA*/
3686 phy_set_bb_reg(priv, 0xa2c, BIT(15) | BIT(14), 0);
3687 phy_set_bb_reg(priv, 0xa2c, BIT(15) | BIT(14), 2);
3688 /* ofdm */
3689 phy_set_bb_reg(priv, 0xd00, BIT(27), 1);
3690 phy_set_bb_reg(priv, 0xd00, BIT(27), 0);
3691#endif
3692
3693#if defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_NOISE_CONTROL)
3694 if (GET_CHIP_VER(priv) == VERSION_8192D){
3695 phy_set_bb_reg(priv, 0xf14, BIT(16),1);
3696 phy_set_bb_reg(priv, 0xf14, BIT(16),0);
3697 RTL_W32(RXERR_RPT, RTL_R32(RXERR_RPT)|BIT(27));
3698 RTL_W32(RXERR_RPT, RTL_R32(RXERR_RPT)&(~BIT(27)));
3699 }
3700#endif
3701
3702#ifdef CONFIG_RTL_88E_SUPPORT
3703 if (GET_CHIP_VER(priv)==VERSION_8188E) {
3704 phy_set_bb_reg(priv, 0xc0c, BIT(31), 1);
3705 phy_set_bb_reg(priv, 0xc0c, BIT(31), 0);
3706 }
3707#endif
3708}
3709
3710#if defined(CONFIG_RTL_NEW_AUTOCH)
3711void hold_CCA_FA_counter(struct rtl8192cd_priv *priv)
3712{
3713 /* hold cck CCA & FA counter */
3714 phy_set_bb_reg(priv, 0xa2c, BIT(12), 1);
3715 phy_set_bb_reg(priv, 0xa2c, BIT(14), 1);
3716
3717 /* hold ofdm CCA & FA counter */
3718 phy_set_bb_reg(priv, 0xc00, BIT(31), 1);
3719 phy_set_bb_reg(priv, 0xd00, BIT(31), 1);
3720}
3721
3722void release_CCA_FA_counter(struct rtl8192cd_priv *priv)
3723{
3724 /* release cck CCA & FA counter */
3725 phy_set_bb_reg(priv, 0xa2c, BIT(12), 0);
3726 phy_set_bb_reg(priv, 0xa2c, BIT(14), 0);
3727
3728 /* release ofdm CCA & FA counter */
3729 phy_set_bb_reg(priv, 0xc00, BIT(31), 0);
3730 phy_set_bb_reg(priv, 0xd00, BIT(31), 0);
3731
3732#ifdef CONFIG_RTL_88E_SUPPORT
3733 if (GET_CHIP_VER(priv)==VERSION_8188E) {
3734 phy_set_bb_reg(priv, 0xc0c, BIT(31), 1);
3735 phy_set_bb_reg(priv, 0xc0c, BIT(31), 0);
3736 }
3737#endif
3738}
3739
3740
3741void _FA_statistic(struct rtl8192cd_priv *priv)
3742{
3743 // read OFDM FA counters
3744 priv->pshare->ofdm_FA_cnt1 = RTL_R16(0xda2);
3745 priv->pshare->ofdm_FA_cnt2 = RTL_R16(0xda4);
3746 priv->pshare->ofdm_FA_cnt3 = RTL_R16(0xda6);
3747 priv->pshare->ofdm_FA_cnt4 = RTL_R16(0xda8);
3748
3749 priv->pshare->cck_FA_cnt = (RTL_R8(0xa5b) << 8) + RTL_R8(0xa5c);
3750
3751#ifdef INTERFERENCE_CONTROL
3752 priv->pshare->ofdm_FA_total_cnt = (unsigned int) priv->pshare->ofdm_FA_cnt1 +
3753 priv->pshare->ofdm_FA_cnt2 + priv->pshare->ofdm_FA_cnt3 +
3754 priv->pshare->ofdm_FA_cnt4 + RTL_R16(0xcf0) + RTL_R16(0xcf2);
3755
3756 priv->pshare->FA_total_cnt = priv->pshare->ofdm_FA_total_cnt + priv->pshare->cck_FA_cnt;
3757#else
3758 priv->pshare->FA_total_cnt = priv->pshare->ofdm_FA_cnt1 + priv->pshare->ofdm_FA_cnt2 +
3759 priv->pshare->ofdm_FA_cnt3 + priv->pshare->ofdm_FA_cnt4 +
3760 priv->pshare->cck_FA_cnt + RTL_R16(0xcf0) + RTL_R16(0xcf2);
3761#endif
3762}
3763#endif
3764
3765void FA_statistic(struct rtl8192cd_priv *priv)
3766{
3767
3768#if defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_NOISE_CONTROL)
3769 if (GET_CHIP_VER(priv) == VERSION_8192D){
3770// priv->pshare->F90_cnt = phy_query_bb_reg(priv, 0xf90, bMaskHWord);
3771 priv->pshare->F94_cnt = phy_query_bb_reg(priv, 0xf94, bMaskHWord);
3772 priv->pshare->F94_cntOK = phy_query_bb_reg(priv, 0xf94, bMaskLWord);
3773 RTL_W32(RXERR_RPT,(RTL_R32(RXERR_RPT)&0x0fffffff)|0x70000000);
3774 priv->pshare->Reg664_cnt = RTL_R32(RXERR_RPT) & 0xfffff;
3775 RTL_W32(RXERR_RPT,(RTL_R32(RXERR_RPT)&0x0fffffff)|0x60000000);
3776 priv->pshare->Reg664_cntOK = RTL_R32(RXERR_RPT) & 0xfffff;
3777 }
3778#endif
3779
3780#if !defined(CONFIG_RTL_NEW_AUTOCH)
3781 signed char value8;
3782
3783 // read OFDM FA counters
3784 priv->pshare->ofdm_FA_cnt1 = RTL_R16(0xda2);
3785 priv->pshare->ofdm_FA_cnt2 = RTL_R16(0xda4);
3786 priv->pshare->ofdm_FA_cnt3 = RTL_R16(0xda6);
3787 priv->pshare->ofdm_FA_cnt4 = RTL_R16(0xda8);
3788
3789 // read the CCK FA counters
3790 value8 = RTL_R8(0xa2d);
3791 RTL_W8(0xa2d, value8 | 0x40); // regA2D[6]=1 to hold and read the CCK FA counters
3792 priv->pshare->cck_FA_cnt = RTL_R8(0xa5b);
3793 priv->pshare->cck_FA_cnt = priv->pshare->cck_FA_cnt << 8;
3794 priv->pshare->cck_FA_cnt += RTL_R8(0xa5c);
3795#ifdef INTERFERENCE_CONTROL
3796 priv->pshare->ofdm_FA_total_cnt = (unsigned int) priv->pshare->ofdm_FA_cnt1 +
3797 priv->pshare->ofdm_FA_cnt2 + priv->pshare->ofdm_FA_cnt3 +
3798 priv->pshare->ofdm_FA_cnt4 + RTL_R16(0xcf0) + RTL_R16(0xcf2);
3799
3800 priv->pshare->FA_total_cnt = priv->pshare->ofdm_FA_total_cnt + priv->pshare->cck_FA_cnt;
3801#else
3802
3803 priv->pshare->FA_total_cnt = priv->pshare->ofdm_FA_cnt1 + priv->pshare->ofdm_FA_cnt2 +
3804 priv->pshare->ofdm_FA_cnt3 + priv->pshare->ofdm_FA_cnt4 +
3805 priv->pshare->cck_FA_cnt + RTL_R16(0xcf0) + RTL_R16(0xcf2);
3806#endif
3807 if (priv->pshare->rf_ft_var.rssi_dump)
3808 priv->pshare->CCA_total_cnt = ((RTL_R8(0xa60)<<8)|RTL_R8(0xa61)) + RTL_R16(0xda0);
3809#else
3810 hold_CCA_FA_counter(priv);
3811 _FA_statistic(priv);
3812
3813 if (priv->pshare->rf_ft_var.rssi_dump)
3814 priv->pshare->CCA_total_cnt = ((RTL_R8(0xa60)<<8)|RTL_R8(0xa61)) + RTL_R16(0xda0);
3815
3816 release_CCA_FA_counter(priv);
3817#endif
3818
3819 reset_FA_reg(priv);
3820
3821#if defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_NOISE_CONTROL)
3822 if (GET_CHIP_VER(priv) == VERSION_8192D){
3823 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G && !(OPMODE & WIFI_SITE_MONITOR)) {
3824 if (priv->pshare->DNC_on == 0){
3825 //if ((priv->pshare->F94_cnt + priv->pshare->F90_cnt)> 3000){
3826 /* Reg 664: x > y && x > 1000
3827 Reg F94: x > 0.75*y && x > 1000 */
3828 if (((priv->pshare->Reg664_cnt>priv->pshare->Reg664_cntOK) && (priv->pshare->Reg664_cnt > 1000))||
3829 ((priv->pshare->F94_cnt > ((priv->pshare->Reg664_cntOK*3)>>2)) && (priv->pshare->F94_cnt > 1000))) {
3830 priv->ext_stats.tp_average_pre = (priv->ext_stats.tx_avarage+priv->ext_stats.rx_avarage)>>17;
3831 priv->pshare->DNC_on = 1;
3832 priv->pshare->DNC_chk_cnt = 1;
3833 priv->pshare->DNC_chk = 2; // 0: don't check, 1; check, 2: just entering DNC
3834 //phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0x00a00000);
3835 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07600760);
3836 phy_set_bb_reg(priv, 0xc50, bMaskByte0, 0x20);
3837 phy_set_bb_reg(priv, 0xc58, bMaskByte0, 0x20);
3838 //printk("Dynamic Noise Control ON\n");
3839 }
3840 } else {
3841 if ((priv->pshare->DNC_chk_cnt % 5)==0){ // check every 5*2=10 seconds
3842 unsigned long tp_now = (priv->ext_stats.tx_avarage+priv->ext_stats.rx_avarage)>>17;
3843 priv->pshare->DNC_chk_cnt = 0;
3844
3845 if ((priv->pshare->DNC_chk == 2) && (tp_now < priv->ext_stats.tp_average_pre+5)){
3846 //no advantage, leave DNC state
3847 priv->pshare->DNC_on = 0;
3848 priv->pshare->DNC_chk = 0;
3849 //phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0);
3850 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07000700);
3851 }
3852 else
3853 {
3854 priv->pshare->DNC_chk = 0;
3855
3856 /* If TP < 20M or TP varies more than 5M. Start Checking...*/
3857 if ((tp_now < 20) || ((tp_now < (priv->ext_stats.tp_average_pre-5))|| (tp_now > (priv->ext_stats.tp_average_pre+5)))){
3858 priv->pshare->DNC_chk = 1;
3859 //phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0);
3860 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07000700);
3861 if (!rtk_timer_pending(&priv->dnc_timer)) {
3862 //printk("... Start Check Noise ...\n");
3863 rtk_mod_timer(&priv->dnc_timer, jiffies + RTL_MILISECONDS_TO_JIFFIES(100)); // 100 ms
3864 }
3865 }
3866 }
3867
3868 priv->ext_stats.tp_average_pre = tp_now;
3869
3870 } else if ((priv->pshare->DNC_chk_cnt % 5)==1 && priv->pshare->DNC_chk == 1) {
3871 priv->pshare->DNC_chk = 0;
3872 //if ((priv->pshare->F94_cnt + priv->pshare->F90_cnt) < 120) {
3873 if ((priv->pshare->F94_cnt + priv->pshare->Reg664_cnt) < 120) {
3874 priv->pshare->DNC_on = 0;
3875 //phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0);
3876 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07000700);
3877 //printk("Dynamic Noise Control OFF\n");
3878 }
3879 }
3880 priv->pshare->DNC_chk_cnt++;
3881 }
3882 }
3883 }
3884#endif
3885}
3886
3887
3888//3 ============================================================
3889//3 Rate Adaptive
3890//3 ============================================================
3891
3892void check_RA_by_rssi(struct rtl8192cd_priv *priv, struct stat_info *pstat)
3893{
3894 int level = 0;
3895
3896 switch (pstat->cmn_info.ra_info.rssi_level) {
3897 case 1:
3898 if (pstat->rssi >= priv->pshare->rf_ft_var.raGoDownUpper)
3899 level = 1;
3900 else if ((pstat->rssi >= priv->pshare->rf_ft_var.raGoDown20MLower) ||
3901 ((priv->pshare->is_40m_bw) && (pstat->ht_cap_len) &&
3902 (pstat->rssi >= priv->pshare->rf_ft_var.raGoDown40MLower) &&
3903 (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SUPPORT_CH_WDTH_))))
3904 level = 2;
3905 else
3906 level = 3;
3907 break;
3908 case 2:
3909 if (pstat->rssi > priv->pshare->rf_ft_var.raGoUpUpper)
3910 level = 1;
3911 else if ((pstat->rssi < priv->pshare->rf_ft_var.raGoDown40MLower) ||
3912 ((!pstat->ht_cap_len || !priv->pshare->is_40m_bw ||
3913 !(pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SUPPORT_CH_WDTH_))) &&
3914 (pstat->rssi < priv->pshare->rf_ft_var.raGoDown20MLower)))
3915 level = 3;
3916 else
3917 level = 2;
3918 break;
3919 case 3:
3920 if (pstat->rssi > priv->pshare->rf_ft_var.raGoUpUpper)
3921 level = 1;
3922 else if ((pstat->rssi > priv->pshare->rf_ft_var.raGoUp20MLower) ||
3923 ((priv->pshare->is_40m_bw) && (pstat->ht_cap_len) &&
3924 (pstat->rssi > priv->pshare->rf_ft_var.raGoUp40MLower) &&
3925 (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SUPPORT_CH_WDTH_))))
3926 level = 2;
3927 else
3928 level = 3;
3929 break;
3930 default:
3931 if (isErpSta(pstat))
3932 DEBUG_ERR("wrong rssi level setting\n");
3933 break;
3934 }
3935
3936 if (level != pstat->cmn_info.ra_info.rssi_level) {
3937 pstat->cmn_info.ra_info.rssi_level = level;
3938#ifdef CONFIG_RTL_88E_SUPPORT
3939 if (GET_CHIP_VER(priv)==VERSION_8188E) {
3940#ifdef TXREPORT
3941 add_RATid(priv, pstat);
3942#endif
3943 } else
3944#endif
3945 {
3946#if defined(CONFIG_RTL_92D_SUPPORT) || defined(CONFIG_RTL_92C_SUPPORT)
3947 add_update_RATid(priv, pstat);
3948#endif
3949 }
3950 }
3951}
3952#endif
3953
3954#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
3955
3956void check_txrate_by_reg(struct rtl8192cd_priv *priv, struct stat_info *pstat)
3957{
3958 unsigned char initial_rate = 0x7f;
3959 unsigned char legacyRA =0 ;
3960
3961 if( should_restrict_Nrate(priv, pstat) && is_fixedMCSTxRate(priv, pstat))
3962 legacyRA = 1;
3963
3964 if (!pstat->cmn_info.ra_info.disable_ra)
3965 {
3966 if (is_auto_rate(priv , pstat) || legacyRA)
3967 {
3968 initial_rate = RTL_R8(INIDATA_RATE_SEL + REMAP_AID(pstat)) & 0x7f;
3969 if (initial_rate == 0x7f)
3970 return;
3971
3972 if ((initial_rate&0x3f) < 12) {
3973 pstat->current_tx_rate = dot11_rate_table[initial_rate&0x3f];
3974 pstat->ht_current_tx_info &= ~TX_USE_SHORT_GI;
3975 } else {
3976 pstat->current_tx_rate = HT_RATE_ID + ((initial_rate&0x3f) -12);
3977 if (initial_rate & BIT(6))
3978 pstat->ht_current_tx_info |= TX_USE_SHORT_GI;
3979 else
3980 pstat->ht_current_tx_info &= ~TX_USE_SHORT_GI;
3981 }
3982
3983 priv->pshare->current_tx_rate = pstat->current_tx_rate;
3984 priv->pshare->ht_current_tx_info = pstat->ht_current_tx_info;
3985 } else if (pstat->ht_cap_len) {
3986 unsigned int is_sgi = 0;
3987
3988 if (priv->pshare->is_40m_bw && (pstat->tx_bw == CHANNEL_WIDTH_40)
3989#ifdef WIFI_11N_2040_COEXIST
3990 && !((OPMODE & WIFI_AP_STATE) && priv->pmib->dot11nConfigEntry.dot11nCoexist &&
3991 (priv->bg_ap_timeout || orForce20_Switch20Map(priv)
3992 ))
3993#endif
3994 ) {
3995 if (priv->pmib->dot11nConfigEntry.dot11nShortGIfor40M
3996 && (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SHORTGI_40M_)))
3997 is_sgi++;
3998 } else if (priv->pmib->dot11nConfigEntry.dot11nShortGIfor20M
3999 && (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SHORTGI_20M_))) {
4000 is_sgi++;
4001 }
4002
4003 if (is_sgi)
4004 pstat->ht_current_tx_info |= TX_USE_SHORT_GI;
4005 else
4006 pstat->ht_current_tx_info &= ~TX_USE_SHORT_GI;
4007 }
4008
4009 if (priv->pshare->is_40m_bw && (pstat->tx_bw == CHANNEL_WIDTH_40))
4010 pstat->ht_current_tx_info |= TX_USE_40M_MODE;
4011 else
4012 pstat->ht_current_tx_info &= ~TX_USE_40M_MODE;
4013
4014 priv->pshare->ht_current_tx_info = pstat->ht_current_tx_info;
4015 } else {
4016 DEBUG_INFO("sta has no aid found to check current tx rate\n");
4017 }
4018}
4019#endif
4020#if 0
4021void add_RATid(struct rtl8192cd_priv *priv, struct stat_info *pstat)
4022{
4023 unsigned char limit=16;
4024 int i;
4025 unsigned long flags;
4026 unsigned int update_reg=0;
4027
4028 SAVE_INT_AND_CLI(flags);
4029
4030 pstat->tx_ra_bitmap = 0;
4031
4032 for (i=0; i<32; i++) {
4033 if (pstat->bssrateset[i])
4034 pstat->tx_ra_bitmap |= get_bit_value_from_ieee_value(pstat->bssrateset[i]&0x7f);
4035 }
4036
4037 if (pstat->ht_cap_len) {
4038 if ((pstat->MIMO_ps & _HT_MIMO_PS_STATIC_) ||
4039 (get_rf_mimo_mode(priv)== RF_1T2R) ||
4040 (get_rf_mimo_mode(priv)== RF_1T1R))
4041 limit=8;
4042
4043 for (i=0; i<limit; i++) {
4044 if (pstat->ht_cap_buf.support_mcs[i/8] & BIT(i%8))
4045 pstat->tx_ra_bitmap |= BIT(i+12);
4046 }
4047 }
4048
4049 if (pstat->ht_cap_len) {
4050 unsigned int set_sgi = 0;
4051 if (priv->pshare->is_40m_bw && (pstat->tx_bw == CHANNEL_WIDTH_40)
4052#ifdef WIFI_11N_2040_COEXIST
4053 && !((OPMODE & WIFI_AP_STATE) && priv->pmib->dot11nConfigEntry.dot11nCoexist &&
4054 (priv->bg_ap_timeout || orForce20_Switch20Map(priv)
4055 ))
4056#endif
4057 ) {
4058 if (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SHORTGI_40M_)
4059 && priv->pmib->dot11nConfigEntry.dot11nShortGIfor40M)
4060 set_sgi++;
4061 } else if (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SHORTGI_20M_) &&
4062 priv->pmib->dot11nConfigEntry.dot11nShortGIfor20M) {
4063 set_sgi++;
4064 }
4065
4066 if (set_sgi) {
4067#if defined(CONFIG_RTL_88E_SUPPORT) && defined(TXREPORT)
4068 if (GET_CHIP_VER(priv)==VERSION_8188E)
4069 priv->pshare->RaInfo[pstat->cmn_info.aid].SGIEnable = 1;
4070 else
4071#endif
4072 pstat->tx_ra_bitmap |= BIT(28);
4073 }
4074#if defined(CONFIG_RTL_88E_SUPPORT) && defined(TXREPORT)
4075 else {
4076 if (GET_CHIP_VER(priv)==VERSION_8188E)
4077 priv->pshare->RaInfo[pstat->cmn_info.aid].SGIEnable = 0;
4078 }
4079#endif
4080 }
4081#if defined(CONFIG_RTL_88E_SUPPORT) && defined(TXREPORT)
4082 else {
4083 if (GET_CHIP_VER(priv)==VERSION_8188E)
4084 priv->pshare->RaInfo[pstat->cmn_info.aid].SGIEnable = 0;
4085 }
4086#endif
4087
4088 if ((pstat->cmn_info.ra_info.rssi_level < 1) || (pstat->cmn_info.ra_info.rssi_level > 3)) {
4089 if (pstat->rssi >= priv->pshare->rf_ft_var.raGoDownUpper)
4090 pstat->cmn_info.ra_info.rssi_level = 1;
4091 else if ((pstat->rssi >= priv->pshare->rf_ft_var.raGoDown20MLower) ||
4092 ((priv->pshare->is_40m_bw) && (pstat->ht_cap_len) &&
4093 (pstat->rssi >= priv->pshare->rf_ft_var.raGoDown40MLower) &&
4094 (pstat->ht_cap_buf.ht_cap_info & cpu_to_le16(_HTCAP_SUPPORT_CH_WDTH_))))
4095 pstat->cmn_info.ra_info.rssi_level = 2;
4096 else
4097 pstat->cmn_info.ra_info.rssi_level = 3;
4098 }
4099
4100 if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11A) &&
4101 ((OPMODE & WIFI_AP_STATE) || (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G)))
4102 pstat->tx_ra_bitmap &= 0xfffffff0; //disable cck rate
4103
4104#ifdef P2P_SUPPORT
4105 if(pstat->is_p2p_client){
4106 pstat->tx_ra_bitmap &= 0xfffffff0; //disable cck rate
4107 }
4108#endif
4109
4110 // rate adaptive by rssi
4111 if ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11N) && pstat->ht_cap_len && (!should_restrict_Nrate(priv, pstat))) {
4112 if ((get_rf_mimo_mode(priv) == RF_1T2R) || (get_rf_mimo_mode(priv) == RF_1T1R)) {
4113 switch (pstat->cmn_info.ra_info.rssi_level) {
4114 case 1:
4115 pstat->tx_ra_bitmap &= 0x100f0000;
4116 break;
4117 case 2:
4118 pstat->tx_ra_bitmap &= 0x100ff000;
4119 break;
4120 case 3:
4121 if (priv->pshare->is_40m_bw)
4122 pstat->tx_ra_bitmap &= 0x100ff005;
4123 else
4124 pstat->tx_ra_bitmap &= 0x100ff001;
4125 break;
4126 }
4127 } else {
4128 switch (pstat->cmn_info.ra_info.rssi_level) {
4129 case 1:
4130 pstat->tx_ra_bitmap &= 0x1f8f0000;
4131 break;
4132 case 2:
4133 pstat->tx_ra_bitmap &= 0x1f8ff000;
4134 break;
4135 case 3:
4136 if (priv->pshare->is_40m_bw)
4137 pstat->tx_ra_bitmap &= 0x010ff005;
4138 else
4139 pstat->tx_ra_bitmap &= 0x010ff001;
4140 break;
4141 }
4142
4143 // Don't need to mask high rates due to new rate adaptive parameters
4144 //if (pstat->is_broadcom_sta) // use MCS12 as the highest rate vs. Broadcom sta
4145 // pstat->tx_ra_bitmap &= 0x81ffffff;
4146
4147 // NIC driver will report not supporting MCS15 and MCS14 in asoc req
4148 //if (pstat->is_rtl8190_sta && !pstat->is_2t_mimo_sta)
4149 // pstat->tx_ra_bitmap &= 0x83ffffff; // if Realtek 1x2 sta, don't use MCS15 and MCS14
4150 }
4151 }
4152 else if (((priv->pmib->dot11BssType.net_work_type & WIRELESS_11G) && isErpSta(pstat)) ||
4153 ((priv->pmib->dot11BssType.net_work_type & WIRELESS_11A) &&
4154 ((OPMODE & WIFI_AP_STATE) || (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G))))
4155 {
4156 switch (pstat->cmn_info.ra_info.rssi_level) {
4157 case 1:
4158 pstat->tx_ra_bitmap &= 0x00000f00;
4159 break;
4160 case 2:
4161 pstat->tx_ra_bitmap &= 0x00000ff0;
4162 break;
4163 case 3:
4164 pstat->tx_ra_bitmap &= 0x00000ff5;
4165 break;
4166 }
4167 } else {
4168 pstat->tx_ra_bitmap &= 0x0000000d;
4169 }
4170
4171// Client mode IOT issue, Button 2009.07.17
4172#ifdef CLIENT_MODE
4173 if(OPMODE & WIFI_STATION_STATE) {
4174 if(!pstat->is_rtl8192s_sta && pstat->is_realtek_sta && pstat->is_legacy_encrpt)
4175 pstat->tx_ra_bitmap &= 0x0001ffff; // up to MCS4
4176 }
4177#endif
4178#if defined(CONFIG_RTL_92D_SUPPORT) && defined (USB_POWER_SUPPORT)
4179 if ((GET_CHIP_VER(priv)==VERSION_8192D) && (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G))
4180 pstat->tx_ra_bitmap &= USB_RA_MASK;
4181#endif
4182
4183 update_remapAid(priv, pstat);
4184
4185#if defined(CONFIG_RTL_88E_SUPPORT) && defined(TXREPORT)
4186 if (GET_CHIP_VER(priv)==VERSION_8188E) {
4187#ifndef RATEADAPTIVE_BY_ODM
4188 if (pstat->tx_ra_bitmap & 0xff000) {
4189 if (priv->pshare->is_40m_bw)
4190 priv->pshare->RaInfo[pstat->cmn_info.aid].RateID = ARFR_1T_40M;
4191 else
4192 priv->pshare->RaInfo[pstat->cmn_info.aid].RateID = ARFR_1T_20M;
4193 } else if (pstat->tx_ra_bitmap & 0xff0) {
4194 priv->pshare->RaInfo[pstat->cmn_info.aid].RateID = ARFR_BG_MIX;
4195 } else {
4196 priv->pshare->RaInfo[pstat->cmn_info.aid].RateID = ARFR_B_ONLY;
4197 }
4198
4199 priv->pshare->RaInfo[pstat->cmn_info.aid].RateMask = pstat->tx_ra_bitmap;
4200 ARFBRefresh(priv, &priv->pshare->RaInfo[pstat->cmn_info.aid]);
4201#else
4202 struct _odm_ra_info_* pRAInfo = &(ODMPTR->ra_info[pstat->cmn_info.aid]);
4203 if (pstat->tx_ra_bitmap & 0xff000) {
4204 if (priv->pshare->is_40m_bw)
4205 pRAInfo->RateID = ARFR_1T_40M;
4206 else
4207 pRAInfo->RateID = ARFR_1T_20M;
4208 } else if (pstat->tx_ra_bitmap & 0xff0) {
4209 pRAInfo->RateID = ARFR_BG_MIX;
4210 } else {
4211 pRAInfo->RateID = ARFR_B_ONLY;
4212 }
4213 odm_ra_update_rate_info_8188e(ODMPTR, pstat->cmn_info.aid, pRAInfo->RateID, pstat->tx_ra_bitmap, pRAInfo->RateSGI);
4214#endif
4215 } else
4216#endif
4217#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
4218 {
4219
4220 if (!pstat->cmn_info.ra_info.disable_ra)
4221 {
4222#ifdef CONFIG_RTL_92D_SUPPORT
4223 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G)
4224 {
4225 pstat->tx_ra_bitmap &= 0xfffffff0;
4226 if (pstat->tx_ra_bitmap & 0xff00000) {
4227 if (priv->pshare->is_40m_bw)
4228 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_Band_A_40M, pstat->tx_ra_bitmap);
4229 else
4230 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_Band_A_20M, pstat->tx_ra_bitmap);
4231 update_reg++;
4232 } else if (pstat->tx_ra_bitmap & 0xff000) {
4233 if (priv->pshare->is_40m_bw)
4234 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_Band_A_40M, pstat->tx_ra_bitmap);
4235 else
4236 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_Band_A_20M, pstat->tx_ra_bitmap);
4237 } else if (pstat->tx_ra_bitmap & 0xff0) {
4238 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_Band_A_BMC, pstat->tx_ra_bitmap);
4239 } else {
4240 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_Band_A_BMC, pstat->tx_ra_bitmap);
4241 }
4242 } else
4243#endif
4244 {
4245 if (pstat->tx_ra_bitmap & 0xff00000) {
4246 if (priv->pshare->is_40m_bw)
4247 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_40M, pstat->tx_ra_bitmap);
4248 else
4249 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_2T_20M, pstat->tx_ra_bitmap);
4250 update_reg++;
4251 } else if (pstat->tx_ra_bitmap & 0xff000) {
4252 if (priv->pshare->is_40m_bw)
4253 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_1T_40M, pstat->tx_ra_bitmap);
4254 else
4255 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_1T_20M, pstat->tx_ra_bitmap);
4256 } else if (pstat->tx_ra_bitmap & 0xff0) {
4257 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_BG_MIX, pstat->tx_ra_bitmap);
4258 } else {
4259 set_RATid_cmd(priv, REMAP_AID(pstat), ARFR_B_ONLY, pstat->tx_ra_bitmap);
4260 }
4261 }
4262
4263 /*
4264 * Rate adaptive algorithm.
4265 * If the STA is 2R, we set the inti rate to MCS 15
4266 */
4267 if (update_reg) {
4268 if (!pstat->check_init_tx_rate && (pstat->rssi > 55)) {
4269 pstat->check_init_tx_rate = 1;
4270 }
4271 }
4272 DEBUG_INFO("Add id %d val %08x to ratr\n", pstat->cmn_info.aid, pstat->tx_ra_bitmap);
4273 } else {
4274#ifdef CONFIG_RTL_92D_SUPPORT
4275 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G) {
4276 if (priv->pshare->is_40m_bw)
4277 set_RATid_cmd(priv, priv->pshare->fw_support_sta_num, ARFR_2T_Band_A_40M, 0x1ffffff0);
4278 else
4279 set_RATid_cmd(priv, priv->pshare->fw_support_sta_num, ARFR_2T_Band_A_20M, 0x1ffffff0);
4280 } else
4281#endif
4282 {
4283 if (priv->pshare->is_40m_bw)
4284 set_RATid_cmd(priv, priv->pshare->fw_support_sta_num, ARFR_2T_40M, 0x1fffffff);
4285 else
4286 set_RATid_cmd(priv, priv->pshare->fw_support_sta_num, ARFR_2T_20M, 0x1fffffff);
4287 }
4288
4289 }
4290 }
4291#endif
4292
4293 RESTORE_INT(flags);
4294}
4295#endif
4296
4297void set_rssi_cmd(struct rtl8192cd_priv *priv, struct stat_info *pstat)
4298{
4299#ifndef SMP_SYNC
4300 unsigned long flags;
4301#endif
4302 unsigned int content = 0;
4303
4304 int rssi = pstat->rssi;
4305
4306#ifdef HIGH_POWER_EXT_PA
4307 if( priv->pshare->rf_ft_var.use_ext_pa )
4308 rssi += RSSI_DIFF_PA;
4309 if( rssi > 100)
4310 rssi = 100;
4311#endif
4312
4313
4314 SAVE_INT_AND_CLI(flags);
4315
4316 /*
4317 * set rssi
4318 */
4319 content = rssi<< 24;
4320
4321#ifdef CONFIG_RTL_92D_SUPPORT
4322 /*
4323 * set max macid
4324 */
4325 if (GET_CHIP_VER(priv) == VERSION_8192D){
4326 content |= priv->pshare->max_fw_macid << 16;
4327 }
4328#endif
4329
4330 /*
4331 * set macid
4332 */
4333 content |= REMAP_AID(pstat) << 8;
4334
4335 /*
4336 * set cmd id
4337 */
4338 content |= H2C_CMD_RSSI;
4339
4340 signin_h2c_cmd(priv, content, 0);
4341
4342 RESTORE_INT(flags);
4343}
4344
4345#if defined(CONFIG_RTL_92D_SUPPORT) || defined(CONFIG_RTL_92C_SUPPORT)
4346#ifdef CONFIG_PCI_HCI
4347static __always_inline void add_rssi_timer(unsigned long task_priv)
4348{
4349 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv *)task_priv;
4350 struct stat_info *pstat = NULL;
4351 unsigned int set_timer = 0;
4352 unsigned long flags = 0;
4353
4354 if (!(priv->drv_state & DRV_STATE_OPEN))
4355 return;
4356
4357 if (rtk_timer_pending(&priv->add_rssi_timer))
4358 rtk_del_timer_sync(&priv->add_rssi_timer);
4359
4360#ifdef PCIE_POWER_SAVING
4361 if ((priv->pwr_state == L2) || (priv->pwr_state == L1))
4362 return;
4363#endif
4364
4365 if (!list_empty(&priv->addrssi_list)) {
4366 pstat = list_entry(priv->addrssi_list.next, struct stat_info, addrssi_list);
4367 if (!pstat)
4368 return;
4369
4370 if (!is_h2c_buf_occupy(priv)) {
4371 set_rssi_cmd(priv, pstat);
4372 if (!list_empty(&pstat->addrssi_list)) {
4373 SAVE_INT_AND_CLI(flags);
4374 SMP_LOCK(flags);
4375 list_del_init(&pstat->addrssi_list);
4376 RESTORE_INT(flags);
4377 SMP_UNLOCK(flags);
4378 }
4379
4380 if (!list_empty(&priv->addrssi_list))
4381 set_timer++;
4382 } else {
4383 set_timer++;
4384 }
4385 }
4386
4387 if (set_timer)
4388 rtk_mod_timer(&priv->add_rssi_timer, jiffies + RTL_MILISECONDS_TO_JIFFIES(50)); // 50 ms
4389}
4390DEFINE_TIMER_CALLBACK(add_rssi_timer);
4391
4392void init_add_rssi_timer(struct rtl8192cd_priv *priv)
4393{
4394 rtk_timer_setup(&(priv->add_rssi_timer), add_rssi_timer, (unsigned long)priv, 0);
4395}
4396
4397void add_update_rssi(struct rtl8192cd_priv *priv, struct stat_info *pstat)
4398{
4399#ifndef SMP_SYNC
4400 unsigned long flags;
4401#endif
4402
4403 if (is_h2c_buf_occupy(priv)) {
4404 if (list_empty(&pstat->addrssi_list)) {
4405 SAVE_INT_AND_CLI(flags);
4406 list_add_tail(&(pstat->addrssi_list), &(priv->addrssi_list));
4407 RESTORE_INT(flags);
4408
4409 if (!rtk_timer_pending(&priv->add_rssi_timer))
4410 rtk_mod_timer(&priv->add_rssi_timer, jiffies + RTL_MILISECONDS_TO_JIFFIES(50)); // 50 ms
4411 }
4412 } else {
4413 set_rssi_cmd(priv, pstat);
4414 }
4415}
4416#endif // CONFIG_PCI_HCI
4417
4418#ifdef CONFIG_USB_HCI
4419void add_update_rssi(struct rtl8192cd_priv *priv, struct stat_info *pstat)
4420{
4421 notify_update_sta_rssi(priv, pstat);
4422}
4423#endif // CONFIG_USB_HCI
4424#endif // CONFIG_RTL_92D_SUPPORT || CONFIG_RTL_92C_SUPPORT
4425
4426
4427void set_RATid_cmd(struct rtl8192cd_priv *priv, unsigned int macid, unsigned int rateid, unsigned int ratemask)
4428{
4429 unsigned int content = 0;
4430 unsigned short ext_content = 0;
4431
4432//#ifdef CONFIG_RTL_8812_SUPPORT
4433 if(! CHIP_VER_92X_SERIES(priv))
4434 return;
4435//#endif
4436
4437 /*
4438 * set ratemask
4439 */
4440 ext_content = ratemask & 0xffff;
4441 content = ((ratemask & 0xfff0000) >> 16) << 8;
4442
4443 /*
4444 * set short GI
4445 */
4446 if (ratemask & BIT(28))
4447 content |= BIT(29);
4448
4449 /*
4450 * set macid (station aid)
4451 */
4452 content |= (macid & 0x1f) << 24;
4453
4454 /*
4455 * set rateid (ARFR table)
4456 */
4457 content |= (rateid & 0xf) << 20;
4458
4459 /*
4460 * set ext_content used
4461 */
4462 content |= BIT(7);
4463
4464 /*
4465 * set cmd id
4466 */
4467 content |= H2C_CMD_MACID;
4468
4469 signin_h2c_cmd(priv, content, ext_content);
4470}
4471
4472
4473
4474//3 ============================================================
4475//3 EDCCA
4476//3 ============================================================
4477#if 0
4478void check_EDCCA(struct rtl8192cd_priv *priv, short rssi)
4479{
4480 if ((priv->pshare->rf_ft_var.edcca_thd) && (priv->pmib->dot11RFEntry.dot11channel==14
4481 || priv->pshare->is_40m_bw
4482#if defined(CONFIG_RTL_92D_SUPPORT)
4483 || (priv->pmib->dot11RFEntry.phyBandSelect & PHY_BAND_5G)
4484#endif
4485 )) {
4486 if((rssi > priv->pshare->rf_ft_var.edcca_thd) && (priv->pshare->phw->EDCCA_on == 0)) {
4487 RTL_W32(REG_OFDM_0_ECCA_THRESHOLD, 0xfc03fd);
4488 priv->pshare->phw->EDCCA_on =1;
4489 } else if( (rssi < priv->pshare->rf_ft_var.edcca_thd-5) && priv->pshare->phw->EDCCA_on) {
4490 RTL_W32(REG_OFDM_0_ECCA_THRESHOLD, 0x7f037f);
4491 priv->pshare->phw->EDCCA_on =0;
4492 }
4493 }
4494 if ((!priv->pshare->rf_ft_var.edcca_thd) && priv->pshare->phw->EDCCA_on) {
4495 RTL_W32(0xc4c, 0x7f037f);
4496 priv->pshare->phw->EDCCA_on = 0;
4497 }
4498}
4499#endif
4500
4501//3 ============================================================
4502//3 Antenna Diversity
4503//3 ============================================================
4504#ifdef SW_ANT_SWITCH
4505
4506//
4507// 20100514 Luke/Joseph:
4508// Add new function to reset antenna diversity state after link.
4509//
4510
4511void resetSwAntDivVariable(struct rtl8192cd_priv *priv)
4512{
4513 priv->pshare->RSSI_sum_R = 0;
4514 priv->pshare->RSSI_cnt_R = 0;
4515 priv->pshare->RSSI_sum_L = 0;
4516 priv->pshare->RSSI_cnt_L = 0;
4517 priv->pshare->TXByteCnt_R = 0;
4518 priv->pshare->TXByteCnt_L = 0;
4519 priv->pshare->RXByteCnt_R = 0;
4520 priv->pshare->RXByteCnt_L = 0;
4521
4522}
4523void sw_ant_div_rest_after_link(struct rtl8192cd_priv *priv)
4524{
4525 priv->pshare->RSSI_test = FALSE;
4526 priv->pshare->DM_SWAT_Table.try_flag = SWAW_STEP_RESET;
4527 memset(priv->pshare->DM_SWAT_Table.SelectAntennaMap, 0, sizeof(priv->pshare->DM_SWAT_Table.SelectAntennaMap));
4528 priv->pshare->DM_SWAT_Table.mapIndex = 0;
4529 priv->pshare->lastTxOkCnt = priv->net_stats.tx_bytes;
4530 priv->pshare->lastRxOkCnt = priv->net_stats.rx_bytes;
4531 resetSwAntDivVariable(priv);
4532}
4533
4534
4535void dm_SW_AntennaSwitchInit(struct rtl8192cd_priv *priv)
4536{
4537 if(!priv->pshare->rf_ft_var.antSw_enable)
4538 return;
4539
4540// if (GET_CHIP_VER(priv) == VERSION_8188C)
4541 if(get_rf_mimo_mode(priv)== RF_1T1R)
4542 priv->pshare->rf_ft_var.antSw_select = 0;
4543
4544 //RT_TRACE(COMP_SWAS, DBG_LOUD, ("SWAS:Init SW Antenna Switch\n"));
4545 resetSwAntDivVariable(priv);
4546 priv->pshare->DM_SWAT_Table.CurAntenna = Antenna_L;
4547 priv->pshare->DM_SWAT_Table.try_flag = SWAW_STEP_RESET;
4548 memset(priv->pshare->DM_SWAT_Table.SelectAntennaMap, 0, sizeof(priv->pshare->DM_SWAT_Table.SelectAntennaMap));
4549 priv->pshare->DM_SWAT_Table.mapIndex = 0;
4550
4551#ifdef GPIO_ANT_SWITCH
4552#ifdef CONFIG_RTL_92D_DMDP
4553 if(priv->pshare->wlandev_idx==0)
4554 priv->pshare->rf_ft_var.antHw_enable=0;
4555 else
4556 return;
4557#endif
4558// GPIO 45 :
4559// GPIO_MOD => data port
4560// GPIO_IO_SEL => output
4561 RTL_W32(GPIO_PIN_CTRL, 0x00300000| RTL_R32(GPIO_PIN_CTRL));
4562 phy_set_bb_reg(priv, GPIO_PIN_CTRL, 0x3000, priv->pshare->DM_SWAT_Table.CurAntenna);
4563 RTL_W32(rFPGA0_XCD_RFParameter, RTL_R32(rFPGA0_XCD_RFParameter)| BIT(15)|BIT(16)); // enable ANTSEL
4564
4565#else
4566 RTL_W32(LEDCFG, RTL_R32(LEDCFG) | BIT(23) ); //enable LED[1:0] pin as ANTSEL
4567
4568 if ( !priv->pshare->rf_ft_var.antSw_select) {
4569 RTL_W32(REG_FPGA0_XAB_RF_PARAMETER, RTL_R32(REG_FPGA0_XAB_RF_PARAMETER) | BIT(13) ); //select ANTESEL from path A
4570 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) | BIT(8)| BIT(9) ); // enable ANTSEL A as SW control
4571 RTL_W32(REG_FPGA0_XA_RF_INTERFACE_OE, (RTL_R32(REG_FPGA0_XA_RF_INTERFACE_OE) & ~(BIT(8)|BIT(9)))| 0x01<<8 ); // 0x01: left antenna, 0x02: right antenna
4572 } else {
4573 RTL_W32(REG_FPGA0_XAB_RF_PARAMETER, RTL_R32(REG_FPGA0_XAB_RF_PARAMETER) & ~ BIT(13) ); //select ANTESEL from path B
4574 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) | BIT(24)| BIT(25) ); // enable ANTSEL B as SW control
4575 RTL_W32(REG_FPGA0_XB_RF_INTERFACE_OE, (RTL_R32(REG_FPGA0_XB_RF_INTERFACE_OE) & ~(BIT(8)|BIT(9)))| 0x01<<8 ); // 0x01: left antenna, 0x02: right antenna
4576 }
4577 RTL_W16(rFPGA0_TxInfo, (RTL_R16(rFPGA0_TxInfo)&0xf0ff) | BIT(8) ); // b11-b8=0001
4578#endif
4579
4580 // Move the timer initialization to InitializeVariables function.
4581 //platform_initialize_timer(Adapter, &pMgntInfo->SwAntennaSwitchTimer, (void *)dm_SW_AntennaSwitchCallback, NULL, "SwAntennaSwitchTimer");
4582}
4583//
4584// 20100514 Luke/Joseph:
4585// Add new function for antenna diversity after link.
4586// This is the main function of antenna diversity after link.
4587// This function is called in HalDmWatchDog() and dm_SW_AntennaSwitchCallback().
4588// HalDmWatchDog() calls this function with SWAW_STEP_PEAK to initialize the antenna test.
4589// In SWAW_STEP_PEAK, another antenna and a 500ms timer will be set for testing.
4590// After 500ms, dm_SW_AntennaSwitchCallback() calls this function to compare the signal just
4591// listened on the air with the RSSI of original antenna.
4592// It chooses the antenna with better RSSI.
4593// There is also a aged policy for error trying. Each error trying will cost more 5 seconds waiting
4594// penalty to get next try.
4595//
4596void dm_SW_AntennaSwitch(struct rtl8192cd_priv *priv, char Step)
4597{
4598 unsigned int curTxOkCnt, curRxOkCnt;
4599 unsigned int CurByteCnt, PreByteCnt;
4600 int Score_R=0, Score_L=0;
4601 int RSSI_R, RSSI_L;
4602 char nextAntenna=priv->pshare->DM_SWAT_Table.CurAntenna;
4603 int i;
4604
4605//1 1. Determine which condition should turn off Antenna Diversity
4606
4607#ifdef MP_TEST
4608 if ((OPMODE & WIFI_MP_STATE) || priv->pshare->rf_ft_var.mp_specific)
4609 return;
4610#endif
4611
4612// if(!(GET_CHIP_VER(priv) == VERSION_8188C) || !priv->pshare->rf_ft_var.antSw_enable)
4613// return;
4614
4615 if((!priv->assoc_num)
4616#ifdef PCIE_POWER_SAVING
4617 || (priv->pwr_state == L2) || (priv->pwr_state == L1)
4618#endif
4619 ){
4620 sw_ant_div_rest_after_link(priv);
4621 return;
4622 }
4623
4624 // Handling step mismatch condition.
4625 // Peak step is not finished at last time. Recover the variable and check again.
4626 if( Step != priv->pshare->DM_SWAT_Table.try_flag)
4627 {
4628 sw_ant_div_rest_after_link(priv);
4629 }
4630
4631//1 2. Initialization: Select a assocaiated AP or STA as RSSI target
4632 if(priv->pshare->DM_SWAT_Table.try_flag == SWAW_STEP_RESET) {
4633#ifdef CLIENT_MODE
4634 if((OPMODE & (WIFI_STATION_STATE | WIFI_ASOC_STATE)) == (WIFI_STATION_STATE | WIFI_ASOC_STATE)) {
4635 // Target: Infrastructure mode AP.
4636 priv->pshare->RSSI_target = NULL;
4637 }
4638#endif
4639 resetSwAntDivVariable(priv);
4640 priv->pshare->DM_SWAT_Table.try_flag = SWAW_STEP_PEAK;
4641 return;
4642 }
4643 else {
4644
4645//1 3. Antenna Diversity
4646
4647 //2 Calculate TX and RX OK bytes
4648
4649 curTxOkCnt = priv->net_stats.tx_bytes - priv->pshare->lastTxOkCnt;
4650 curRxOkCnt = priv->net_stats.rx_bytes - priv->pshare->lastRxOkCnt;
4651 priv->pshare->lastTxOkCnt = priv->net_stats.tx_bytes;
4652 priv->pshare->lastRxOkCnt = priv->net_stats.rx_bytes;
4653
4654 //2 Try State
4655 if(priv->pshare->DM_SWAT_Table.try_flag == SWAW_STEP_DETERMINE) {
4656 //3 1. Seperately caculate TX and RX OK byte counter for ant A and B
4657 if(priv->pshare->DM_SWAT_Table.CurAntenna == Antenna_R) {
4658 priv->pshare->TXByteCnt_R += curTxOkCnt;
4659 priv->pshare->RXByteCnt_R += curRxOkCnt;
4660 } else {
4661 priv->pshare->TXByteCnt_L += curTxOkCnt;
4662 priv->pshare->RXByteCnt_L += curRxOkCnt;
4663 }
4664
4665 //3 2. Change anntena for testing
4666 if(priv->pshare->DM_SWAT_Table.rssi_trying != 0) {
4667 nextAntenna = (priv->pshare->DM_SWAT_Table.CurAntenna ) ^ Antenna_MAX;
4668 priv->pshare->DM_SWAT_Table.rssi_trying--;
4669 }
4670
4671 //2 Try State End: Determine the best antenna
4672
4673 if(priv->pshare->DM_SWAT_Table.rssi_trying==0) {
4674 nextAntenna = priv->pshare->DM_SWAT_Table.CurAntenna;
4675 priv->pshare->DM_SWAT_Table.mapIndex = (priv->pshare->DM_SWAT_Table.mapIndex+1)%SELANT_MAP_SIZE;
4676
4677 //3 TP Mode: Determine the best antenna by throuhgput
4678 if(priv->pshare->DM_SWAT_Table.TestMode == TP_MODE) {
4679
4680
4681
4682 //3 (1) Saperately caculate total byte count for two antennas
4683 if(priv->pshare->DM_SWAT_Table.CurAntenna == Antenna_R) {
4684 CurByteCnt = (priv->pshare->TXByteCnt_R + (priv->pshare->RXByteCnt_R<<1));
4685 PreByteCnt = (priv->pshare->TXByteCnt_L + (priv->pshare->RXByteCnt_L<<1));
4686 } else {
4687 CurByteCnt = (priv->pshare->TXByteCnt_L + (priv->pshare->RXByteCnt_L<<1));
4688 PreByteCnt = (priv->pshare->TXByteCnt_R + (priv->pshare->RXByteCnt_R<<1));
4689 }
4690
4691 //3 (2) Throughput Normalization
4692 if(priv->pshare->TrafficLoad == TRAFFIC_HIGH)
4693 CurByteCnt >>=3;
4694 else if(priv->pshare->TrafficLoad == TRAFFIC_LOW)
4695 CurByteCnt >>=1;
4696
4697 if(priv->pshare->DM_SWAT_Table.CurAntenna == Antenna_R) {
4698 priv->pshare->DM_SWAT_Table.SelectAntennaMap[0][priv->pshare->DM_SWAT_Table.mapIndex] = PreByteCnt;
4699 priv->pshare->DM_SWAT_Table.SelectAntennaMap[1][priv->pshare->DM_SWAT_Table.mapIndex] = CurByteCnt;
4700 } else {
4701 priv->pshare->DM_SWAT_Table.SelectAntennaMap[0][priv->pshare->DM_SWAT_Table.mapIndex] = CurByteCnt;
4702 priv->pshare->DM_SWAT_Table.SelectAntennaMap[1][priv->pshare->DM_SWAT_Table.mapIndex] = PreByteCnt;
4703 }
4704
4705 Score_R = Score_L=0;
4706 for (i= 0; i<SELANT_MAP_SIZE; i++) {
4707 Score_L += priv->pshare->DM_SWAT_Table.SelectAntennaMap[0][i];
4708 Score_R += priv->pshare->DM_SWAT_Table.SelectAntennaMap[1][i];
4709 }
4710
4711 nextAntenna = (Score_L > Score_R) ? Antenna_L : Antenna_R;
4712
4713 if(priv->pshare->rf_ft_var.ant_dump&8)
4714 panic_printk("Mode TP, select Ant%d, [Score1=%d,Score2=%d]\n", nextAntenna, Score_L, Score_R);
4715
4716 }
4717
4718 //3 RSSI Mode: Determine the best anntena by RSSI
4719 else if(priv->pshare->DM_SWAT_Table.TestMode == RSSI_MODE) {
4720
4721 //2 Saperately caculate average RSSI for two antennas
4722 RSSI_L = RSSI_R = 0;
4723
4724 if(priv->pshare->RSSI_cnt_R > 0)
4725 RSSI_R = priv->pshare->RSSI_sum_R/priv->pshare->RSSI_cnt_R;
4726 if(priv->pshare->RSSI_cnt_L > 0)
4727 RSSI_L = priv->pshare->RSSI_sum_L/priv->pshare->RSSI_cnt_L;
4728
4729 if(RSSI_L && RSSI_R )
4730 nextAntenna = (RSSI_L > RSSI_R) ? Antenna_L : Antenna_R;
4731
4732 if(priv->pshare->rf_ft_var.ant_dump&8)
4733 panic_printk("Mode RSSI, RSSI_R=%d(%d), RSSI_L=%d(%d), Ant=%d\n",
4734 RSSI_R, priv->pshare->RSSI_cnt_R, RSSI_L, priv->pshare->RSSI_cnt_L, nextAntenna);
4735
4736 }
4737
4738 //3 Reset state
4739 resetSwAntDivVariable(priv);
4740 priv->pshare->DM_SWAT_Table.try_flag = SWAW_STEP_PEAK;
4741 priv->pshare->RSSI_test = FALSE;
4742 }
4743 }
4744
4745 //1 Normal State
4746 else if(priv->pshare->DM_SWAT_Table.try_flag == SWAW_STEP_PEAK) {
4747
4748 //3 Determine TP/RSSI mode by TRX OK count
4749 if((curRxOkCnt+curTxOkCnt) > TP_MODE_THD) {
4750 //2 Determine current traffic is high or low
4751 if((curTxOkCnt+curRxOkCnt) > TRAFFIC_THRESHOLD)
4752 priv->pshare->TrafficLoad = TRAFFIC_HIGH;
4753 else
4754 priv->pshare->TrafficLoad = TRAFFIC_LOW;
4755
4756 priv->pshare->DM_SWAT_Table.rssi_trying = 10;
4757 priv->pshare->DM_SWAT_Table.TestMode = TP_MODE;
4758 } else {
4759
4760 int idx = 0;
4761 struct stat_info* pEntry = findNextSTA(priv, &idx);
4762 priv->pshare->RSSI_target = NULL;
4763 while(pEntry) {
4764 if(pEntry && pEntry->expire_to) {
4765 if(!priv->pshare->RSSI_target)
4766 priv->pshare->RSSI_target = pEntry;
4767 else if( pEntry->rssi < priv->pshare->RSSI_target->rssi )
4768 priv->pshare->RSSI_target = pEntry;
4769 }
4770 pEntry = findNextSTA(priv, &idx);
4771 };
4772
4773 priv->pshare->DM_SWAT_Table.rssi_trying = 6;
4774 priv->pshare->DM_SWAT_Table.TestMode = RSSI_MODE;
4775
4776 if(priv->pshare->RSSI_target == NULL) {
4777 sw_ant_div_rest_after_link(priv);
4778 return;
4779 }
4780
4781 //3 reset state
4782 memset(priv->pshare->DM_SWAT_Table.SelectAntennaMap, 0, sizeof(priv->pshare->DM_SWAT_Table.SelectAntennaMap));
4783 }
4784
4785 //3 Begin to enter Try State
4786 nextAntenna = (priv->pshare->DM_SWAT_Table.CurAntenna ) ^ Antenna_MAX;
4787 priv->pshare->DM_SWAT_Table.try_flag = SWAW_STEP_DETERMINE;
4788 priv->pshare->RSSI_test = TRUE;
4789
4790 //3 Reset variables
4791 resetSwAntDivVariable(priv);
4792 }
4793 }
4794
4795//1 4.Change TRX antenna
4796 if(nextAntenna != priv->pshare->DM_SWAT_Table.CurAntenna) {
4797#if defined(CONFIG_PCI_HCI)
4798 if (GET_HCI_TYPE(priv) == RTL_HCI_PCIE) {
4799#ifdef GPIO_ANT_SWITCH
4800 phy_set_bb_reg(priv, GPIO_PIN_CTRL, 0x3000, nextAntenna);
4801#else
4802 if (!priv->pshare->rf_ft_var.antSw_select)
4803 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, 0x300, nextAntenna);
4804 else
4805 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, 0x300, nextAntenna);
4806#endif
4807
4808//1 5.Reset Statistics
4809 priv->pshare->DM_SWAT_Table.CurAntenna = nextAntenna;
4810 }
4811#endif
4812#if defined(CONFIG_USB_HCI) || defined(CONFIG_SDIO_HCI)
4813 if ((GET_HCI_TYPE(priv) == RTL_HCI_USB) || (GET_HCI_TYPE(priv) == RTL_HCI_SDIO)) {
4814 notify_antenna_switch(priv, nextAntenna);
4815 }
4816#endif
4817 }
4818
4819//1 6.Set next timer
4820
4821 if(priv->pshare->DM_SWAT_Table.rssi_trying == 0) {
4822 return;
4823 }
4824
4825 if(priv->pshare->DM_SWAT_Table.TestMode == RSSI_MODE) {
4826 rtk_mod_timer(&priv->pshare->swAntennaSwitchTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(400)); // 400 ms
4827 } else if(priv->pshare->DM_SWAT_Table.TestMode == TP_MODE) {
4828
4829 if(priv->pshare->TrafficLoad == TRAFFIC_HIGH) {
4830 if(priv->pshare->DM_SWAT_Table.rssi_trying%2 == 0)
4831 rtk_mod_timer(&priv->pshare->swAntennaSwitchTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(10)); // 10 ms
4832 else
4833 rtk_mod_timer(&priv->pshare->swAntennaSwitchTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(80)); // 80 ms
4834
4835 } else if(priv->pshare->TrafficLoad == TRAFFIC_LOW) {
4836 if(priv->pshare->DM_SWAT_Table.rssi_trying%2 == 0)
4837 rtk_mod_timer(&priv->pshare->swAntennaSwitchTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(40)); // 40 ms
4838 else
4839 rtk_mod_timer(&priv->pshare->swAntennaSwitchTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(80)); // 80 ms
4840 }
4841 }
4842
4843}
4844
4845
4846static __always_inline void dm_SW_AntennaSwitchCallback(unsigned long task_priv)
4847{
4848 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv*)task_priv;
4849#ifndef SMP_SYNC
4850 unsigned long flags = 0;
4851#endif
4852 if (!(priv->drv_state & DRV_STATE_OPEN))
4853 return;
4854
4855 SAVE_INT_AND_CLI(flags);
4856 dm_SW_AntennaSwitch(priv, SWAW_STEP_DETERMINE);
4857 RESTORE_INT(flags);
4858}
4859DEFINE_TIMER_CALLBACK(dm_SW_AntennaSwitchCallback);
4860
4861void init_swAntennaSwitchTimer(struct rtl8192cd_priv *priv)
4862{
4863 rtk_timer_setup(&(priv->pshare->swAntennaSwitchTimer), dm_SW_AntennaSwitchCallback, (unsigned long)priv, 0);
4864}
4865
4866//
4867// 20100514 Luke/Joseph:
4868// This function is used to gather the RSSI information for antenna testing.
4869// It selects the RSSI of the peer STA that we want to know.
4870//
4871void dm_SWAW_RSSI_Check(struct rtl8192cd_priv *priv, struct rx_frinfo *pfrinfo)
4872{
4873 struct stat_info* pEntry = NULL;
4874 pEntry = get_stainfo(priv, GetAddr2Ptr(get_pframe(pfrinfo)));
4875
4876 if((priv->pshare->RSSI_target==NULL)||(priv->pshare->RSSI_target==pEntry)) {
4877 //1 RSSI for SW Antenna Switch
4878 if(priv->pshare->DM_SWAT_Table.CurAntenna == Antenna_R)
4879 {
4880 priv->pshare->RSSI_sum_R += pfrinfo->rf_info.rssi;
4881 priv->pshare->RSSI_cnt_R++;
4882 } else {
4883 priv->pshare->RSSI_sum_L += pfrinfo->rf_info.rssi;
4884 priv->pshare->RSSI_cnt_L++;
4885 }
4886 }
4887}
4888
4889#ifndef HW_ANT_SWITCH
4890
4891int diversity_antenna_select(struct rtl8192cd_priv *priv, unsigned char *data)
4892{
4893 int ant = _atoi(data, 16);
4894// if(GET_CHIP_VER(priv) != VERSION_8188C)
4895// return 0;
4896
4897#ifdef PCIE_POWER_SAVING
4898 PCIeWakeUp(priv, POWER_DOWN_T0);
4899#endif
4900 if(ant==Antenna_L || ant==Antenna_R) {
4901#ifdef GPIO_ANT_SWITCH
4902 phy_set_bb_reg(priv, GPIO_PIN_CTRL, 0x3000, ant);
4903#else
4904 if (!priv->pshare->rf_ft_var.antSw_select)
4905 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, 0x300, ant);
4906 else
4907 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, 0x300, ant);
4908#endif
4909 priv->pshare->DM_SWAT_Table.CurAntenna = ant;
4910 priv->pshare->rf_ft_var.antSw_enable = 0;
4911 sw_ant_div_rest_after_link(priv);
4912 memset(priv->pshare->DM_SWAT_Table.SelectAntennaMap, 0, sizeof(priv->pshare->DM_SWAT_Table.SelectAntennaMap));
4913 return 1;
4914 } else {
4915 priv->pshare->rf_ft_var.antSw_enable = 1;
4916 priv->pshare->lastTxOkCnt = priv->net_stats.tx_bytes;
4917 priv->pshare->lastRxOkCnt = priv->net_stats.rx_bytes;
4918
4919 return 0;
4920 }
4921}
4922#endif
4923#endif
4924#if defined(HW_ANT_SWITCH)
4925
4926void dm_HW_AntennaSwitchInit(struct rtl8192cd_priv *priv)
4927{
4928 if(!priv->pshare->rf_ft_var.antHw_enable)
4929 return;
4930
4931 if (!(ODMPTR->support_ability & ODM_BB_ANT_DIV))
4932 return;
4933
4934#ifdef SW_ANT_SWITCH
4935 priv->pshare->rf_ft_var.antSw_enable =0;
4936#endif
4937
4938// if (GET_CHIP_VER(priv) == VERSION_8188C)
4939 if(get_rf_mimo_mode(priv)== RF_1T1R)
4940 priv->pshare->rf_ft_var.antSw_select = 0;
4941
4942 if ( !priv->pshare->rf_ft_var.antSw_select) {
4943 RTL_W32(REG_FPGA0_XAB_RF_PARAMETER, RTL_R32(REG_FPGA0_XAB_RF_PARAMETER) | BIT(13) ); //select ANTESEL from path A
4944 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) & ~(BIT(8)| BIT(9)) ); // ANTSEL as HW control
4945 RTL_W32(REG_FPGA0_XA_RF_INTERFACE_OE, (RTL_R32(REG_FPGA0_XA_RF_INTERFACE_OE) & ~(BIT(8)|BIT(9)))| 0x01<<8 ); // 0x01: left antenna, 0x02: right antenna
4946 RTL_W8(0xc50, RTL_R8(0xc50) | BIT(7)); // Enable Hardware antenna switch
4947 RTL_W32(0xc54, RTL_R32(0xc54) | BIT(23) ); // Decide final antenna by comparing 2 antennas' pwdb
4948 } else {
4949 RTL_W32(REG_FPGA0_XAB_RF_PARAMETER, RTL_R32(REG_FPGA0_XAB_RF_PARAMETER) & ~ BIT(13) ); //select ANTESEL from path B
4950 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) & ~(BIT(24)| BIT(25)) ); // ANTSEL as HW control
4951 RTL_W32(REG_FPGA0_XB_RF_INTERFACE_OE, (RTL_R32(REG_FPGA0_XB_RF_INTERFACE_OE) & ~(BIT(8)|BIT(9)))| 0x01<<8 ); // 0x01: left antenna, 0x02: right antenna
4952#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
4953 if (
4954#ifdef CONFIG_RTL_92C_SUPPORT
4955 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
4956#endif
4957#ifdef CONFIG_RTL_92D_SUPPORT
4958#ifdef CONFIG_RTL_92C_SUPPORT
4959 ||
4960#endif
4961 (GET_CHIP_VER(priv)==VERSION_8192D)
4962#endif
4963 )
4964 RTL_W8(0xc58, RTL_R8(0xc58) | BIT(7)); // Enable Hardware antenna switch
4965#endif
4966 RTL_W32(0xc5C, RTL_R32(0xc5c) | BIT(23) ); // Decide final antenna by comparing 2 antennas' pwdb
4967 }
4968
4969 priv->pshare->rf_ft_var.CurAntenna = 0;
4970
4971 RTL_W32(LEDCFG, RTL_R32(LEDCFG) | BIT(23) ); //enable LED[1:0] pin as ANTSEL
4972 RTL_W16(0xca4, (RTL_R16(0xca4) & ~(0xfff))|0x0c0); // Pwdb threshold=12dB
4973 RTL_W32(0x874, RTL_R32(0x874) & ~ BIT(23) ); // No update ANTSEL during GNT_BT=1
4974 RTL_W16(rFPGA0_TxInfo, (RTL_R16(rFPGA0_TxInfo)&0xf0ff) | BIT(8) ); // b11-b8=0001
4975 RTL_W32(0x80c, RTL_R32(0x80c) | BIT(21) ); // assign antenna by tx desc
4976
4977 // CCK setting
4978 RTL_W8(0xa01, RTL_R8(0xa01) | BIT(7)); // enable hw ant diversity
4979 RTL_W8(0xa0c, (RTL_R8(0xa0c) & 0xe0) | 0x0f ); // b4=0, b3:0 = 1111 32 sample
4980 RTL_W8(0xa11, RTL_R8(0xa11) | BIT(5)); // do not change default optional antenna
4981 RTL_W8(0xa14, (RTL_R8(0xa14) & 0xe0) | 0x08 ); // default : optional = 1:1
4982#ifdef GPIO_ANT_SWITCH
4983 phy_set_bb_reg(priv, rFPGA0_XCD_RFParameter, 0x40000000, 0x01); // enable ANTSEL
4984#endif
4985
4986}
4987
4988void setRxIdleAnt(struct rtl8192cd_priv *priv, char Ant)
4989{
4990 if(priv->pshare->rf_ft_var.CurAntenna != Ant) {
4991 if(Ant) {
4992 RTL_W32(0x858, 0x65a965a9);
4993// RTL_W8(0x6d8, RTL_R8(0x6d8) | BIT(6) );
4994 }
4995 else {
4996 RTL_W32(0x858, 0x569a569a);
4997// RTL_W8(0x6d8, RTL_R8(0x6d8) & (~ BIT(6)));
4998 }
4999 priv->pshare->rf_ft_var.CurAntenna = Ant;
5000 }
5001}
5002
5003void dm_STA_Ant_Select(struct rtl8192cd_priv *priv, struct stat_info *pstat)
5004{
5005 int ScoreA=0, ScoreB=0, i, nextAnt= pstat->CurAntenna, idleAnt=priv->pshare->rf_ft_var.CurAntenna;
5006
5007 if((priv->pshare->rf_ft_var.CurAntenna & 0x80)
5008 || ((pstat->hwRxAntSel[0] + pstat->hwRxAntSel[1])==0 && (pstat->cckPktCount[0] + pstat->cckPktCount[1])<10) )
5009 return;
5010
5011 for(i=0; i<2; i++) {
5012 if(pstat->cckPktCount[i]==0 && pstat->hwRxAntSel[i]==0)
5013 pstat->AntRSSI[i] = 0;
5014 }
5015
5016 if(pstat->hwRxAntSel[0] || pstat->hwRxAntSel[1]) {
5017 ScoreA = pstat->hwRxAntSel[0];
5018 ScoreB = pstat->hwRxAntSel[1];
5019
5020 if(ScoreA != ScoreB) {
5021 if(ScoreA > ScoreB)
5022 nextAnt = 0;
5023 else
5024 nextAnt = 1;
5025 }
5026 } else {
5027 ScoreA = pstat->cckPktCount[0];
5028 ScoreB = pstat->cckPktCount[1];
5029
5030 if(ScoreA > 5*ScoreB)
5031 nextAnt = 0;
5032 else if(ScoreB > 5*ScoreA)
5033 nextAnt = 1;
5034 else if(ScoreA > ScoreB)
5035 nextAnt = 1;
5036 else if(ScoreB > ScoreA)
5037 nextAnt = 0;
5038 }
5039
5040 pstat->CurAntenna = nextAnt;
5041
5042 if(priv->pshare->rf_ft_var.ant_dump&2) {
5043 panic_printk("id=%d, OFDM/CCK: (%d, %d/%d, %d), RSSI:(%d, %d), ant=%d, RxIdle=%d\n",
5044 pstat->cmn_info.aid,
5045 pstat->hwRxAntSel[1],
5046 pstat->hwRxAntSel[0],
5047 pstat->cckPktCount[1],
5048 pstat->cckPktCount[0],
5049 pstat->AntRSSI[1],
5050 pstat->AntRSSI[0],
5051 (pstat->CurAntenna==0 ? 2: 1)
5052 ,((priv->pshare->rf_ft_var.CurAntenna&1)==0 ? 2 : 1)
5053 );
5054 }
5055
5056 if(pstat->AntRSSI[idleAnt]==0)
5057 pstat->AntRSSI[idleAnt] = pstat->AntRSSI[idleAnt^1];
5058
5059// reset variables
5060 pstat->hwRxAntSel[1] = pstat->hwRxAntSel[0] =0;
5061 pstat->cckPktCount[1]= pstat->cckPktCount[0] =0;
5062
5063}
5064
5065void dm_HW_IdleAntennaSelect(struct rtl8192cd_priv *priv)
5066{
5067 struct stat_info *pstat, *pstat_min=NULL;
5068 struct list_head *phead, *plist;
5069 int rssi_min= 0xff, i;
5070#ifdef SMP_SYNC
5071 unsigned long flags = 0;
5072#endif
5073
5074 if(priv->pshare->rf_ft_var.CurAntenna & 0x80)
5075 return;
5076
5077 phead = &priv->asoc_list;
5078
5079 SMP_LOCK_ASOC_LIST(flags);
5080
5081 plist = phead->next;
5082 while(plist != phead) {
5083 pstat = list_entry(plist, struct stat_info, asoc_list);
5084 plist = plist->next;
5085
5086 if((pstat->expire_to) && (pstat->AntRSSI[0] || pstat->AntRSSI[1])) {
5087 int rssi = (pstat->AntRSSI[0] < pstat->AntRSSI[1]) ? pstat->AntRSSI[0] : pstat->AntRSSI[1];
5088 if((!pstat_min) || ( rssi < rssi_min) ) {
5089 pstat_min = pstat;
5090 rssi_min = rssi;
5091 }
5092 }
5093 };
5094
5095 SMP_UNLOCK_ASOC_LIST(flags);
5096
5097 if(pstat_min)
5098 setRxIdleAnt(priv, pstat_min->CurAntenna);
5099
5100
5101#ifdef TX_SHORTCUT
5102 if (!priv->pmib->dot11OperationEntry.disable_txsc) {
5103 SMP_LOCK_ASOC_LIST(flags);
5104
5105 plist = phead->next;
5106 while(plist != phead) {
5107 pstat = list_entry(plist, struct stat_info, asoc_list);
5108 plist = plist->next;
5109
5110 if(pstat->expire_to) {
5111 for (i=0; i<MAX_TXSC_ENTRY; i++) {
5112 struct tx_desc *pdesc= &(pstat->tx_sc_ent[i].hwdesc1);
5113 pdesc->Dword2 &= set_desc(~ (BIT(24)|BIT(25)));
5114 if((pstat->CurAntenna^priv->pshare->rf_ft_var.CurAntenna)&1)
5115 pdesc->Dword2 |= set_desc(BIT(24)|BIT(25));
5116 #ifdef CONFIG_PCI_HCI
5117 if (GET_HCI_TYPE(priv) == RTL_HCI_PCIE) {
5118 pdesc= &(pstat->tx_sc_ent[i].hwdesc2);
5119 pdesc->Dword2 &= set_desc(~ (BIT(24)|BIT(25)));
5120 if((pstat->CurAntenna^priv->pshare->rf_ft_var.CurAntenna)&1)
5121 pdesc->Dword2 |= set_desc(BIT(24)|BIT(25));
5122 }
5123 #endif
5124 }
5125 }
5126 };
5127
5128 SMP_UNLOCK_ASOC_LIST(flags);
5129 }
5130#endif
5131
5132}
5133
5134int diversity_antenna_select(struct rtl8192cd_priv *priv, unsigned char *data)
5135{
5136 int ant = _atoi(data, 16);
5137
5138#ifdef PCIE_POWER_SAVING
5139 PCIeWakeUp(priv, POWER_DOWN_T0);
5140#endif
5141
5142 if (ant==Antenna_L || ant==Antenna_R) {
5143
5144#ifdef GPIO_ANT_SWITCH
5145 if(priv->pshare->rf_ft_var.antSw_enable) {
5146 phy_set_bb_reg(priv, GPIO_PIN_CTRL, 0x3000, ant);
5147 } else
5148#endif
5149 {
5150 if ( !priv->pshare->rf_ft_var.antSw_select) {
5151 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) | BIT(8)| BIT(9) ); // ANTSEL A as SW control
5152 RTL_W8(0xc50, RTL_R8(0xc50) & (~ BIT(7))); // rx OFDM SW control
5153 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, 0x300, ant);
5154 } else {
5155 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) | BIT(24)| BIT(25) ); // ANTSEL B as HW control
5156 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, 0x300, ant);
5157#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
5158 if (
5159#ifdef CONFIG_RTL_92C_SUPPORT
5160 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
5161#endif
5162#ifdef CONFIG_RTL_92D_SUPPORT
5163#ifdef CONFIG_RTL_92C_SUPPORT
5164 ||
5165#endif
5166 (GET_CHIP_VER(priv)==VERSION_8192D)
5167#endif
5168 )
5169 RTL_W8(0xc58, RTL_R8(0xc58) & (~ BIT(7))); // rx OFDM SW control
5170#endif
5171 }
5172 RTL_W8(0xa01, RTL_R8(0xa01) & (~ BIT(7))); // rx CCK SW control
5173 RTL_W32(0x80c, RTL_R32(0x80c) & (~ BIT(21))); // select ant by tx desc
5174 RTL_W32(0x858, 0x569a569a);
5175 }
5176#ifdef HW_ANT_SWITCH
5177 if(HW_DIV_ENABLE)
5178 priv->pshare->rf_ft_var.antHw_enable = BIT(5);
5179#endif
5180 priv->pshare->rf_ft_var.CurAntenna = (ant%2);
5181#ifdef SW_ANT_SWITCH
5182 if(SW_DIV_ENABLE)
5183 priv->pshare->rf_ft_var.antSw_enable = BIT(5);
5184 priv->pshare->DM_SWAT_Table.CurAntenna = ant;
5185 priv->pshare->RSSI_test =0;
5186#endif
5187 }
5188 else if(ant==0){
5189#ifdef GPIO_ANT_SWITCH
5190 if(priv->pshare->rf_ft_var.antHw_enable)
5191#endif
5192 {
5193 if (!priv->pshare->rf_ft_var.antSw_select) {
5194 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) & ~(BIT(8)| BIT(9)) );
5195 RTL_W8(0xc50, RTL_R8(0xc50) | BIT(7)); // OFDM HW control
5196 } else {
5197 RTL_W32(REG_FPGA0_XAB_RF_INTERFACE_SW, RTL_R32(REG_FPGA0_XAB_RF_INTERFACE_SW) & ~(BIT(24)| BIT(25)) );
5198#if defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT)
5199 if (
5200#ifdef CONFIG_RTL_92C_SUPPORT
5201 (GET_CHIP_VER(priv)==VERSION_8192C) || (GET_CHIP_VER(priv)==VERSION_8188C)
5202#endif
5203#ifdef CONFIG_RTL_92D_SUPPORT
5204#ifdef CONFIG_RTL_92C_SUPPORT
5205 ||
5206#endif
5207 (GET_CHIP_VER(priv)==VERSION_8192D)
5208#endif
5209 )
5210 RTL_W8(0xc58, RTL_R8(0xc58) | BIT(7)); // OFDM HW control
5211#endif
5212 }
5213 RTL_W8(0xa01, RTL_R8(0xa01) | BIT(7)); // CCK HW control
5214 RTL_W32(0x80c, RTL_R32(0x80c) | BIT(21) ); // by tx desc
5215 priv->pshare->rf_ft_var.CurAntenna = 0;
5216 RTL_W32(0x858, 0x569a569a);
5217 priv->pshare->rf_ft_var.antHw_enable = 1;
5218#ifdef SW_ANT_SWITCH
5219 priv->pshare->rf_ft_var.antSw_enable = 0;
5220 priv->pshare->RSSI_test =0;
5221#endif
5222 }
5223#ifdef SW_ANT_SWITCH
5224 if(priv->pshare->rf_ft_var.antSw_enable) {
5225 dm_SW_AntennaSwitchInit(priv);
5226 RTL_W32(0x858, 0x569a569a);
5227 priv->pshare->lastTxOkCnt = priv->net_stats.tx_bytes;
5228 priv->pshare->lastRxOkCnt = priv->net_stats.rx_bytes;
5229 priv->pshare->rf_ft_var.antHw_enable = 0;
5230 priv->pshare->rf_ft_var.antSw_enable = 1;
5231 }
5232#endif
5233
5234 }
5235#if defined(SW_ANT_SWITCH) && !defined(GPIO_ANT_SWITCH)
5236 else if(ant==3) {
5237 if(!priv->pshare->rf_ft_var.antSw_enable) {
5238 dm_SW_AntennaSwitchInit(priv);
5239 RTL_W32(0x858, 0x569a569a);
5240 priv->pshare->lastTxOkCnt = priv->net_stats.tx_bytes;
5241 priv->pshare->lastRxOkCnt = priv->net_stats.rx_bytes;
5242 }
5243#ifdef CONFIG_RTL_88E_SUPPORT
5244 if (GET_CHIP_VER(priv)==VERSION_8188E) {
5245 RTL_W8(0xc50, RTL_R8(0xc50) & (~ BIT(7))); // rx OFDM SW control
5246 } else
5247#endif
5248 {
5249 if ( !priv->pshare->rf_ft_var.antSw_select)
5250 RTL_W8(0xc50, RTL_R8(0xc50) & (~ BIT(7))); // rx OFDM SW control
5251 else
5252 RTL_W8(0xc58, RTL_R8(0xc58) & (~ BIT(7))); // rx OFDM SW control
5253 }
5254
5255 RTL_W8(0xa01, RTL_R8(0xa01) & (~ BIT(7))); // rx CCK SW control
5256 RTL_W32(0x80c, RTL_R32(0x80c) & (~ BIT(21))); // select ant by tx desc
5257 priv->pshare->rf_ft_var.antHw_enable = 0;
5258 priv->pshare->rf_ft_var.antSw_enable = 1;
5259
5260 }
5261#endif
5262
5263 return 1;
5264}
5265
5266#endif
5267
5268//3 ============================================================
5269//3 Dynamic Noise Control
5270//3 ============================================================
5271
5272#if defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_NOISE_CONTROL)
5273static __always_inline void dnc_timer(unsigned long task_priv)
5274{
5275 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv *)task_priv;
5276 struct stat_info *pstat = NULL;
5277 unsigned int set_timer = 0;
5278 unsigned long flags;
5279
5280 if (!(priv->drv_state & DRV_STATE_OPEN))
5281 return;
5282
5283 if (rtk_timer_pending(&priv->dnc_timer))
5284 rtk_del_timer_sync(&priv->dnc_timer);
5285
5286#ifdef PCIE_POWER_SAVING
5287 if ((priv->pwr_state == L2) || (priv->pwr_state == L1))
5288 return;
5289#endif
5290
5291 if (priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G) {
5292 //phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0x00a00000);
5293 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07600760);
5294 phy_set_bb_reg(priv, 0xc50, bMaskByte0, 0x20);
5295 phy_set_bb_reg(priv, 0xc58, bMaskByte0, 0x20);
5296 }
5297}
5298DEFINE_TIMER_CALLBACK(dnc_timer);
5299
5300void init_dnc_timer(struct rtl8192cd_priv *priv)
5301{
5302 rtk_timer_setup(&(priv->dnc_timer), dnc_timer, (unsigned long)priv, 0);
5303}
5304#endif // defined(CONFIG_RTL_92D_SUPPORT) && defined(CONFIG_RTL_NOISE_CONTROL)
5305
5306
5307//3 ============================================================
5308//3 Leaving STA check
5309//3 ============================================================
5310#if 0
5311//#if defined(DETECT_STA_EXISTANCE) //&& (defined(CONFIG_RTL_92C_SUPPORT) || defined(CONFIG_RTL_92D_SUPPORT))
5312// Check for STA existance. If STA disappears, disconnect it. Added by Annie, 2010-08-10.
5313void DetectSTAExistance(struct rtl8192cd_priv *priv, struct tx_rpt *report, struct stat_info *pstat)
5314{
5315 unsigned char tmpbuf[16];
5316
5317 // Parameters
5318 const unsigned int maxTxFailCnt = 300; // MAX Tx fail packet count
5319 const unsigned int minTxFailCnt = 30; // MIN Tx fail packet count; this value should be less than maxTxFailCnt.
5320 const unsigned int txFailSecThr= 3; // threshold of Tx Fail Time (in second)
5321
5322 // Temporarily change Retry Limit when TxFail. (tfrl: TxFailRetryLimit)
5323 const unsigned char TFRL = 7; // New Retry Limit value
5324 const unsigned char TFRL_FailCnt = 2; // Tx Fail Count threshold to set Retry Limit
5325 const unsigned char TFRL_SetTime = 2; // Time to set Retry Limit (in second)
5326 const unsigned char TFRL_RcvTime = 10; // Time to recover Retry Limit (in second)
5327
5328 if(OPMODE & WIFI_STATION_STATE)
5329 return;
5330
5331 if( report->txok != 0 )
5332 { // Reset Counter
5333 pstat->tx_conti_fail_cnt = 0;
5334 pstat->tx_last_good_time = priv->up_time;
5335 pstat->leave = 0;
5336 }
5337 else if( report->txfail != 0 )
5338 {
5339 pstat->tx_conti_fail_cnt += report->txfail;
5340 DEBUG_WARN( "detect: txfail=%d, tx_conti_fail_cnt=%d\n", report->txfail, pstat->tx_conti_fail_cnt );
5341
5342 if( CHIP_VER_92X_SERIES(priv) && (priv->up_time >= (pstat->tx_last_good_time+TFRL_SetTime)) &&
5343 pstat->tx_conti_fail_cnt >= TFRL_FailCnt &&
5344 #if defined(CONFIG_RTL8672) || defined (NOT_RTK_BSP)
5345 !pstat->ht_cap_len && // legacy rate only
5346 #endif
5347 !priv->pshare->bRLShortened )
5348 { // Shorten retry limit, because AP spending too much time to send out g mode STA pending packets in HW queue.
5349 RTL_W16(RL, (TFRL&SRL_Mask)<<SRL_SHIFT|(TFRL&LRL_Mask)<<LRL_SHIFT);
5350 priv->pshare->bRLShortened = TRUE;
5351 DEBUG_WARN( "== Shorten RetryLimit to 0x%04X ==\n", RTL_R16(RL) );
5352 }
5353
5354 if( (pstat->tx_conti_fail_cnt >= maxTxFailCnt) ||
5355 (pstat->tx_conti_fail_cnt >= minTxFailCnt && priv->up_time >= (pstat->tx_last_good_time+txFailSecThr) )
5356 )
5357 { // This STA is considered as disappeared, so delete it.
5358 DEBUG_WARN( "** tx_conti_fail_cnt=%d (min=%d,max=%d)\n", pstat->tx_conti_fail_cnt, minTxFailCnt, maxTxFailCnt);
5359 DEBUG_WARN( "** tx_last_good_time=%d, up_time=%d (Thr:%d)\n", (int)pstat->tx_last_good_time, (int)priv->up_time, txFailSecThr );
5360 DEBUG_WARN( "AP is going to del_sta %02X:%02X:%02X:%02X:%02X:%02X\n", pstat->cmn_info.mac_addr[0],pstat->cmn_info.mac_addr[1],pstat->cmn_info.mac_addr[2],pstat->cmn_info.mac_addr[3],pstat->cmn_info.mac_addr[4],pstat->cmn_info.mac_addr[5] );
5361
5362 sprintf((char *)tmpbuf, "%02x%02x%02x%02x%02x%02x", pstat->cmn_info.mac_addr[0],pstat->cmn_info.mac_addr[1],pstat->cmn_info.mac_addr[2],pstat->cmn_info.mac_addr[3],pstat->cmn_info.mac_addr[4],pstat->cmn_info.mac_addr[5]);
5363
5364// del_sta(priv, tmpbuf);
5365 ++(pstat->leave);
5366
5367 if(CHIP_VER_92X_SERIES(priv) {
5368 if (rtk_timer_pending(&priv->pshare->rl_recover_timer))
5369 rtk_del_timer_sync (&priv->pshare->rl_recover_timer);
5370 rtk_mod_timer(&priv->pshare->rl_recover_timer, jiffies + EXPIRE_TO*TFRL_RcvTime);
5371 }
5372
5373 // Reset Counter
5374 pstat->tx_conti_fail_cnt = 0;
5375 pstat->tx_last_good_time = priv->up_time;
5376 }
5377 }
5378}
5379
5380// Timer callback function to recover hardware retry limit register. Added by Annie, 2010-08-10.
5381#if defined(__KERNEL__) || defined(__OSK__)
5382void RetryLimitRecovery(unsigned long task_priv)
5383#elif defined(__ECOS)
5384void RetryLimitRecovery(void *task_priv)
5385#endif
5386{
5387 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv *)task_priv;
5388 if( priv->pshare->bRLShortened )
5389 {
5390 RTL_W16(RL, priv->pshare->RL_setting);
5391 priv->pshare->bRLShortened = FALSE;
5392 DEBUG_WARN( "== Recover RetryLimit to 0x%04X ==\n", RTL_R16(RL) );
5393 }
5394}
5395
5396// Chack STA leaving status; per interface. Added by Annie, 2010-08-10.
5397unsigned char NoLeavingSTA(struct rtl8192cd_priv *priv)
5398{
5399 unsigned char bStaAllOK = TRUE;
5400 struct list_head *phead, *plist;
5401 struct stat_info *pstat;
5402
5403 phead = &priv->asoc_list;
5404 if (!netif_running(priv->dev) || list_empty(phead))
5405 return bStaAllOK;
5406
5407 plist = phead->next;
5408 while (plist != phead) {
5409 pstat = list_entry(plist, struct stat_info, asoc_list);
5410 if( pstat->tx_conti_fail_cnt != 0 ) {
5411 bStaAllOK = FALSE;
5412 break;
5413 }
5414 plist = plist->next;
5415 }
5416
5417 return bStaAllOK;
5418}
5419
5420// Chack STA leaving status for all active interface and recover retry limit register value. Added by Annie, 2010-08-10.
5421void LeavingSTA_RLCheck(struct rtl8192cd_priv *priv)
5422{
5423 unsigned char bIfAllOK = TRUE;
5424 static int AllOKTimes = 0;
5425#ifdef MBSSID
5426 int i;
5427#endif
5428 // Parameter
5429 const unsigned char TFRL_RcvTime = 10; // Time to recover Retry Limit (in second)
5430
5431 if( !NoLeavingSTA(priv) )
5432 bIfAllOK = FALSE;
5433
5434#ifdef UNIVERSAL_REPEATER
5435 if (IS_ROOT_INTERFACE(priv) && GET_VXD_PRIV(priv) ) {
5436 if( !NoLeavingSTA(GET_VXD_PRIV(priv)) )
5437 bIfAllOK = FALSE;
5438 }
5439#endif
5440
5441#ifdef MBSSID
5442 if (IS_ROOT_INTERFACE(priv)) {
5443 if (GET_ROOT(priv)->pmib->miscEntry.vap_enable) {
5444 for (i=0; i<RTL8192CD_NUM_VWLAN; i++) {
5445 if (IS_DRV_OPEN(priv->pvap_priv[i])) {
5446 if( !NoLeavingSTA(priv->pvap_priv[i]) )
5447 bIfAllOK = FALSE;
5448 }
5449 }
5450 }
5451 }
5452#endif
5453
5454 if( bIfAllOK ) {
5455 AllOKTimes ++;
5456
5457 if( AllOKTimes >= TFRL_RcvTime )
5458#if defined(__KERNEL__) || defined(__OSK__)
5459 RetryLimitRecovery((unsigned long)priv);
5460#elif defined(__ECOS)
5461 RetryLimitRecovery((void *)priv);
5462#endif
5463 }
5464 else {
5465 AllOKTimes = 0;
5466 }
5467}
5468#endif
5469
5470
5471
5472#ifdef CONFIG_RTL_92C_SUPPORT
5473
5474/*
5475 * PA Analog Pre-distortion Calibration R06
5476 */
5477void APK_MAIN(struct rtl8192cd_priv *priv, unsigned int is2T)
5478{
5479 unsigned int reg_d[PATH_NUM];
5480 unsigned int tmpReg, index, offset, path, i=0, pathbound = PATH_NUM, apkbound=6;
5481 unsigned int BB_backup[APK_BB_REG_NUM];
5482 unsigned int BB_REG[APK_BB_REG_NUM] = {0x904, 0xc04, 0x800, 0xc08, 0x874};
5483 unsigned int BB_AP_MODE[APK_BB_REG_NUM] = {0x00000020, 0x00a05430, 0x02040000, 0x000800e4, 0x00204000};
5484 unsigned int BB_normal_AP_MODE[APK_BB_REG_NUM] = {0x00000020, 0x00a05430, 0x02040000, 0x000800e4, 0x22204000};
5485 unsigned int AFE_backup[APK_AFE_REG_NUM];
5486 unsigned int AFE_REG[APK_AFE_REG_NUM] = { 0x85c, 0xe6c, 0xe70, 0xe74, 0xe78, 0xe7c, 0xe80, 0xe84,
5487 0xe88, 0xe8c, 0xed0, 0xed4, 0xed8, 0xedc, 0xee0, 0xeec};
5488 unsigned int MAC_backup[IQK_MAC_REG_NUM];
5489 unsigned int MAC_REG[IQK_MAC_REG_NUM] = {0x522, 0x550, 0x551, 0x040};
5490 unsigned int APK_RF_init_value[PATH_NUM][APK_BB_REG_NUM] = {{0x0852c, 0x1852c, 0x5852c, 0x1852c, 0x5852c},
5491 {0x2852e, 0x0852e, 0x3852e, 0x0852e, 0x0852e}};
5492 unsigned int APK_normal_RF_init_value[PATH_NUM][APK_BB_REG_NUM] =
5493 { {0x0852c, 0x0a52c, 0x3a52c, 0x5a52c, 0x5a52c}, //path settings equal to path b settings
5494 {0x0852c, 0x0a52c, 0x5a52c, 0x5a52c, 0x5a52c} };
5495
5496 unsigned int APK_RF_value_0[PATH_NUM][APK_BB_REG_NUM] =
5497 { {0x52019, 0x52014, 0x52013, 0x5200f, 0x5208d},
5498 {0x5201a, 0x52019, 0x52016, 0x52033, 0x52050}};
5499
5500 unsigned int APK_normal_RF_value_0[PATH_NUM][APK_BB_REG_NUM] =
5501 { {0x52019, 0x52017, 0x52010, 0x5200d, 0x5206a}, //path settings equal to path b settings
5502 {0x52019, 0x52017, 0x52010, 0x5200d, 0x5206a} };
5503
5504 unsigned int AFE_on_off[PATH_NUM] = {0x04db25a4, 0x0b1b25a4}; //path A on path B off / path A off path B on
5505 unsigned int APK_offset[PATH_NUM] = {0xb68, 0xb6c};
5506 unsigned int APK_normal_offset[PATH_NUM] = {0xb28, 0xb98};
5507 unsigned int APK_value[PATH_NUM] = {0x92fc0000, 0x12fc0000};
5508 unsigned int APK_normal_value[PATH_NUM] = {0x92680000, 0x12680000};
5509 char APK_delta_mapping[APK_BB_REG_NUM][13] = {{-4, -3, -2, -2, -1, -1, 0, 1, 2, 3, 4, 5, 6},
5510 {-4, -3, -2, -2, -1, -1, 0, 1, 2, 3, 4, 5, 6},
5511 {-6, -4, -2, -2, -1, -1, 0, 1, 2, 3, 4, 5, 6},
5512 {-1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6},
5513 {-11, -9, -7, -5, -3, -1, 0, 0, 0, 0, 0, 0, 0}};
5514 unsigned int APK_normal_setting_value_1[13] =
5515 { 0x01017018, 0xf7ed8f84, 0x1b1a1816, 0x2522201e, 0x322e2b28,
5516 0x433f3a36, 0x5b544e49, 0x7b726a62, 0xa69a8f84, 0xdfcfc0b3,
5517 0x12680000, 0x00880000, 0x00880000 };
5518 unsigned int APK_normal_setting_value_2[16] =
5519 { 0x01c7021d, 0x01670183, 0x01000123, 0x00bf00e2, 0x008d00a3,
5520 0x0068007b, 0x004d0059, 0x003a0042, 0x002b0031, 0x001f0025,
5521 0x0017001b, 0x00110014, 0x000c000f, 0x0009000b, 0x00070008,
5522 0x00050006 };
5523
5524
5525 unsigned int APK_normal_RF_init_value_old[PATH_NUM][APK_BB_REG_NUM] =
5526 {{0x0852c, 0x5a52c, 0x0a52c, 0x5a52c, 0x4a52c}, //path settings equal to path b settings
5527 {0x0852c, 0x5a52c, 0x0a52c, 0x5a52c, 0x4a52c}};
5528 unsigned int APK_normal_RF_value_0_old[PATH_NUM][APK_BB_REG_NUM] =
5529 {{0x52019, 0x52017, 0x52010, 0x5200d, 0x5200a}, //path settings equal to path b settings
5530 {0x52019, 0x52017, 0x52010, 0x5200d, 0x5200a}};
5531 unsigned int APK_normal_setting_value_1_old[13] =
5532 {0x01017018, 0xf7ed8f84, 0x40372d20, 0x5b554e48, 0x6f6a6560,
5533 0x807c7873, 0x8f8b8884, 0x9d999693, 0xa9a6a3a0, 0xb5b2afac,
5534 0x12680000, 0x00880000, 0x00880000};
5535 unsigned int APK_normal_setting_value_2_old[16] =
5536 {0x00810100, 0x00400056, 0x002b0032, 0x001f0024, 0x0019001c,
5537 0x00150017, 0x00120013, 0x00100011, 0x000e000f, 0x000c000d,
5538 0x000b000c, 0x000a000b, 0x0009000a, 0x00090009, 0x00080008,
5539 0x00080008};
5540 unsigned int AP_curve[PATH_NUM][APK_CURVE_REG_NUM];
5541
5542 unsigned int APK_result[PATH_NUM][APK_BB_REG_NUM]; //val_1_1a, val_1_2a, val_2a, val_3a, val_4a
5543 unsigned int ThermalValue = 0;
5544 int BB_offset, delta_V, delta_offset;
5545 int newVerAPK = (IS_UMC_A_CUT_88C(priv)) ? 1 : 0;
5546 unsigned int *pAPK_normal_setting_value_1 = APK_normal_setting_value_1, *pAPK_normal_setting_value_2 = APK_normal_setting_value_2 ;
5547#ifdef HIGH_POWER_EXT_PA
5548 unsigned int tmp0x870=0, tmp0x860=0, tmp0x864=0;
5549
5550 if(priv->pshare->rf_ft_var.use_ext_pa)
5551 newVerAPK = 1;
5552#endif
5553
5554 if(!newVerAPK) {
5555 apkbound = 12;
5556 pAPK_normal_setting_value_1 = APK_normal_setting_value_1_old;
5557 pAPK_normal_setting_value_2 = APK_normal_setting_value_2_old;
5558 }
5559
5560 if(!is2T)
5561 pathbound = 1;
5562
5563 for(index = 0; index < PATH_NUM; index ++) {
5564 APK_offset[index] = APK_normal_offset[index];
5565 APK_value[index] = APK_normal_value[index];
5566 AFE_on_off[index] = 0x6fdb25a4;
5567 }
5568
5569 for(index = 0; index < APK_BB_REG_NUM; index ++) {
5570 for(path = 0; path < pathbound; path++) {
5571 if(newVerAPK) {
5572 APK_RF_init_value[path][index] = APK_normal_RF_init_value[path][index];
5573 APK_RF_value_0[path][index] = APK_normal_RF_value_0[path][index];
5574 } else {
5575 APK_RF_init_value[path][index] = APK_normal_RF_init_value_old[path][index];
5576 APK_RF_value_0[path][index] = APK_normal_RF_value_0_old[path][index];
5577 }
5578
5579 }
5580 BB_AP_MODE[index] = BB_normal_AP_MODE[index];
5581 }
5582
5583 /*
5584 * save BB default value
5585 */
5586 for(index = 1; index < APK_BB_REG_NUM ; index++)
5587 BB_backup[index] = phy_query_bb_reg(priv, BB_REG[index], bMaskDWord);
5588
5589#ifdef HIGH_POWER_EXT_PA
5590 if (priv->pshare->rf_ft_var.use_ext_pa) {
5591 tmp0x870 = phy_query_bb_reg(priv, 0x870, bMaskDWord);
5592 tmp0x860 = phy_query_bb_reg(priv, 0x860, bMaskDWord);
5593 tmp0x864 = phy_query_bb_reg(priv, 0x864, bMaskDWord);
5594 }
5595#endif
5596
5597 //save MAC default value
5598 _phy_save_mac_registers(priv, MAC_REG, MAC_backup);
5599
5600 //save AFE default value
5601 _phy_save_adda_registers(priv, AFE_REG, AFE_backup, APK_AFE_REG_NUM);
5602
5603 for(path = 0; path < pathbound; path++) {
5604 /*
5605 * save old AP curve
5606 */
5607 if(path == RF_PATH_A) {
5608 /*
5609 * path A APK
5610 * load APK setting
5611 * path-A
5612 */
5613 offset = 0xb00;
5614 for(index = 0; index < 11; index ++) {
5615 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_1[index]);
5616 offset += 0x04;
5617 }
5618 phy_set_bb_reg(priv, 0xb98, bMaskDWord, 0x12680000);
5619
5620 offset = 0xb68;
5621 for(; index < 13; index ++) {
5622 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_1[index]);
5623 offset += 0x04;
5624 }
5625
5626 /*
5627 * page-B1
5628 */
5629 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x400000);
5630
5631 /*
5632 *path A
5633 */
5634 offset = 0xb00;
5635 for(index = 0; index < 16; index++) {
5636 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_2[index]);
5637 offset += 0x04;
5638 }
5639 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
5640 } else if(path == RF_PATH_B) {
5641 /*
5642 * path B APK
5643 * load APK setting
5644 * path-B
5645 */
5646 offset = 0xb70;
5647 for(index = 0; index < 10; index ++) {
5648 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_1[index]);
5649 offset += 0x04;
5650 }
5651 phy_set_bb_reg(priv, 0xb28, bMaskDWord, 0x12680000);
5652 phy_set_bb_reg(priv, 0xb98, bMaskDWord, 0x12680000);
5653
5654 offset = 0xb68;
5655 index = 11;
5656 for(; index < 13; index ++) {
5657 //offset 0xb68, 0xb6c
5658 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_1[index]);
5659 offset += 0x04;
5660 }
5661
5662 /*
5663 * page-B1
5664 */
5665 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x400000);
5666
5667 /*
5668 * path B
5669 */
5670 offset = 0xb60;
5671 for(index = 0; index < 16; index++) {
5672 phy_set_bb_reg(priv, offset, bMaskDWord, pAPK_normal_setting_value_2[index]);
5673 offset += 0x04;
5674 }
5675 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
5676 }
5677
5678 if(!newVerAPK) {
5679 tmpReg = phy_query_rf_reg(priv, (unsigned int)path, 0x3, bMaskDWord, 1);
5680
5681 AP_curve[path][0] = tmpReg & 0x1F; //[4:0]
5682
5683 tmpReg = phy_query_rf_reg(priv, (unsigned int)path, 0x4, bMaskDWord, 1);
5684 AP_curve[path][1] = (tmpReg & 0xF8000) >> 15; //[19:15]
5685 AP_curve[path][2] = (tmpReg & 0x7C00) >> 10; //[14:10]
5686 AP_curve[path][3] = (tmpReg & 0x3E0) >> 5; //[9:5]
5687 }
5688
5689 /*
5690 * save RF default value
5691 */
5692 reg_d[path] = phy_query_rf_reg(priv, (unsigned int)path, 0xd, bMaskDWord, 1);
5693
5694 /*
5695 * Path A AFE all on, path B AFE All off or vise versa
5696 */
5697 for(index = 0; index < APK_AFE_REG_NUM ; index++)
5698 phy_set_bb_reg(priv, AFE_REG[index], bMaskDWord, AFE_on_off[path]);
5699
5700 /*
5701 * BB to AP mode
5702 */
5703 if(path == RF_PATH_A) {
5704 for(index = 1; index < APK_BB_REG_NUM ; index++)
5705 phy_set_bb_reg(priv, BB_REG[index], bMaskDWord, BB_AP_MODE[index]);
5706 }
5707
5708#ifdef HIGH_POWER_EXT_PA
5709 if (priv->pshare->rf_ft_var.use_ext_pa) {
5710 phy_set_bb_reg(priv, 0x870, BIT(10), 1);
5711 phy_set_bb_reg(priv, 0x870, BIT(26), 1);
5712 phy_set_bb_reg(priv, 0x860, BIT(10), 0);
5713 phy_set_bb_reg(priv, 0x864, BIT(10), 0);
5714 }
5715#endif
5716
5717 if(newVerAPK) {
5718 if(path == RF_PATH_A) {
5719 phy_set_bb_reg(priv, 0xe30 , bMaskDWord, 0x01008c00);
5720 phy_set_bb_reg(priv, 0xe34 , bMaskDWord, 0x01008c00);
5721 } else if(path == RF_PATH_B) {
5722 phy_set_bb_reg(priv, 0xe50 , bMaskDWord, 0x01008c00);
5723 phy_set_bb_reg(priv, 0xe54 , bMaskDWord, 0x01008c00);
5724 }
5725 }
5726
5727 //MAC settings
5728 _phy_mac_setting_calibration(priv, MAC_REG, MAC_backup);
5729
5730
5731 if(path == RF_PATH_A) {
5732 //Path B to standby mode
5733 phy_set_rf_reg(priv, RF_PATH_B, 0x0, bMaskDWord, 0x10000);
5734 } else {
5735 //Path A to standby mode
5736 phy_set_rf_reg(priv, RF_PATH_A, 0x00, bMaskDWord, 0x10000);
5737 phy_set_rf_reg(priv, RF_PATH_A, 0x10, bMaskDWord, 0x1000f);
5738 phy_set_rf_reg(priv, RF_PATH_A, 0x11, bMaskDWord, 0x20103);
5739 }
5740
5741 /*
5742 * Check Thermal value delta
5743 */
5744 if (priv->pmib->dot11RFEntry.ther) {
5745 ThermalValue = phy_query_rf_reg(priv, RF_PATH_A, 0x24, 0x1f, 1) & 0xff;
5746 ThermalValue -= priv->pmib->dot11RFEntry.ther;
5747 }
5748
5749 delta_offset = ((ThermalValue+14)/2);
5750 if(delta_offset < 0)
5751 delta_offset = 0;
5752 else if (delta_offset > 12)
5753 delta_offset = 12;
5754
5755 //AP calibration
5756 for(index = 1; index < APK_BB_REG_NUM; index++) {
5757 tmpReg = APK_RF_init_value[path][index];
5758 if (priv->pmib->dot11RFEntry.ther) {
5759 BB_offset = (tmpReg & 0xF0000) >> 16;
5760
5761 if(!(tmpReg & BIT(15))) //sign bit 0
5762 BB_offset = -BB_offset;
5763 delta_V = APK_delta_mapping[index][delta_offset];
5764 BB_offset += delta_V;
5765
5766 if(BB_offset < 0) {
5767 tmpReg = tmpReg & (~BIT(15));
5768 BB_offset = -BB_offset;
5769 } else {
5770 tmpReg = tmpReg | BIT(15);
5771 }
5772 tmpReg = (tmpReg & 0xFFF0FFFF) | (BB_offset << 16);
5773 }
5774
5775 if(newVerAPK)
5776 phy_set_rf_reg(priv, (unsigned int)path, 0xc, bMaskDWord, 0x8992e);
5777 else
5778 phy_set_rf_reg(priv, (unsigned int)path, 0xc, bMaskDWord, 0x8992f);
5779
5780 phy_set_rf_reg(priv, (unsigned int)path, 0x0, bMaskDWord, APK_RF_value_0[path][index]);
5781 phy_set_rf_reg(priv, (unsigned int)path, 0xd, bMaskDWord, tmpReg);
5782
5783 /*
5784 * PA11+PAD01111, one shot
5785 */
5786 i = 0;
5787 do {
5788 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x800000);
5789 phy_set_bb_reg(priv, APK_offset[path], bMaskDWord, APK_value[0]);
5790 delay_ms(3);
5791 phy_set_bb_reg(priv, APK_offset[path], bMaskDWord, APK_value[1]);
5792 delay_ms(20);
5793 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
5794
5795 if(!newVerAPK) {
5796 tmpReg = phy_query_rf_reg(priv, (unsigned int)path, 0xb, bMaskDWord, 1);
5797 tmpReg = (tmpReg & 0x3E00) >> 9;
5798 } else {
5799 if(path == RF_PATH_A)
5800 tmpReg = phy_query_bb_reg(priv, 0xbd8, 0x03E00000);
5801 else
5802 tmpReg = phy_query_bb_reg(priv, 0xbd8, 0xF8000000);
5803 }
5804 i++;
5805 } while((tmpReg > apkbound) && i < 4);
5806
5807 APK_result[path][index] = tmpReg;
5808 }
5809 }
5810
5811 /*
5812 * reload MAC default value
5813 */
5814 _phy_reload_mac_registers(priv, MAC_REG, MAC_backup);
5815
5816 /*
5817 * reload BB default value
5818 */
5819 for(index = 1; index < APK_BB_REG_NUM ; index++)
5820 phy_set_bb_reg(priv, BB_REG[index], bMaskDWord, BB_backup[index]);
5821
5822#ifdef HIGH_POWER_EXT_PA
5823 if (priv->pshare->rf_ft_var.use_ext_pa) {
5824 phy_set_bb_reg(priv, 0x870, bMaskDWord, tmp0x870);
5825 phy_set_bb_reg(priv, 0x860, bMaskDWord, tmp0x860);
5826 phy_set_bb_reg(priv, 0x864, bMaskDWord, tmp0x864);
5827 }
5828#endif
5829
5830 /*
5831 * reload AFE default value
5832 */
5833 _phy_reload_adda_registers(priv, AFE_REG, AFE_backup, 16);
5834
5835
5836 /*
5837 * reload RF path default value
5838 */
5839 for(path = 0; path < pathbound; path++) {
5840 phy_set_rf_reg(priv, (unsigned int)path, 0xd, bMaskDWord, reg_d[path]);
5841 if(path == RF_PATH_B) {
5842 phy_set_rf_reg(priv, RF_PATH_A, 0x10, bMaskDWord, 0x1000f);
5843 phy_set_rf_reg(priv, RF_PATH_A, 0x11, bMaskDWord, 0x20101);
5844 }
5845
5846 if(newVerAPK) {
5847 if (APK_result[path][1] > 6)
5848 APK_result[path][1] = 6;
5849 } else {
5850 if(APK_result[path][1] < 1)
5851 APK_result[path][1] = 1;
5852 else if (APK_result[path][1] > 5)
5853 APK_result[path][1] = 5;
5854
5855 if(APK_result[path][2] < 2)
5856 APK_result[path][2] = 2;
5857 else if (APK_result[path][2] > 6)
5858 APK_result[path][2] = 6;
5859
5860 if(APK_result[path][3] < 2)
5861 APK_result[path][3] = 2;
5862 else if (APK_result[path][3] > 6)
5863 APK_result[path][3] = 6;
5864
5865 if(APK_result[path][4] < 5)
5866 APK_result[path][4] = 5;
5867 else if (APK_result[path][4] > 9)
5868 APK_result[path][4] = 9;
5869 }
5870 }
5871
5872 for(path = 0; path < pathbound; path++) {
5873 phy_set_rf_reg(priv, (unsigned int)path, 0x3, bMaskDWord,
5874 ((APK_result[path][1] << 15) | (APK_result[path][1] << 10) | (APK_result[path][1] << 5) | APK_result[path][1]));
5875 if(newVerAPK) {
5876 if(path == RF_PATH_A)
5877 phy_set_rf_reg(priv, (unsigned int)path, 0x4, bMaskDWord,
5878 ((APK_result[path][1] << 15) | (APK_result[path][1] << 10) | (0x00 << 5) | 0x05));
5879 else
5880 phy_set_rf_reg(priv, (unsigned int)path, 0x4, bMaskDWord,
5881 ((APK_result[path][1] << 15) | (APK_result[path][1] << 10) | (0x02 << 5) | 0x05));
5882 phy_set_rf_reg(priv, (unsigned int)path, 0xe, bMaskDWord,
5883 ((0x08 << 15) | (0x08 << 10) | (0x08 << 5) | 0x08));
5884 } else {
5885 phy_set_rf_reg(priv, (unsigned int)path, 0x4, bMaskDWord,
5886 ((APK_result[path][1] << 15) | (APK_result[path][1] << 10) | (APK_result[path][2] << 5) | APK_result[path][3]));
5887 phy_set_rf_reg(priv, (unsigned int)path, 0xe, bMaskDWord,
5888 ((APK_result[path][4] << 15) | (APK_result[path][4] << 10) | (APK_result[path][4] << 5) | APK_result[path][4]));
5889 }
5890 }
5891}
5892
5893
5894
5895/*
5896return FALSE => do IQK again
5897*/
5898char _PHY_SimularityCompare(struct rtl8192cd_priv *priv, int result[][8], unsigned char c1, unsigned char c2)
5899{
5900 unsigned int i, j, diff, SimularityBitMap, bound = 0;
5901 unsigned char final_candidate[2] = {0xFF, 0xFF}; //for path A and path B
5902 char bResult = TRUE, is2T = (GET_CHIP_VER(priv) == VERSION_8192C ? 1 : 0);
5903
5904 bound = (is2T) ? 8 : 4;
5905 SimularityBitMap = 0;
5906
5907 for( i = 0; i < bound; i++ ) {
5908 diff = (result[c1][i] > result[c2][i]) ? (result[c1][i] - result[c2][i]) : (result[c2][i] - result[c1][i]);
5909 if (diff > MAX_TOLERANCE) {
5910 if((i == 2 || i == 6) && !SimularityBitMap) {
5911 if( result[c1][i]+ result[c1][i+1] == 0)
5912 final_candidate[(i>>2)] = c2;
5913 else if (result[c2][i]+result[c2][i+1] == 0)
5914 final_candidate[(i>>2)] = c1;
5915 else
5916 SimularityBitMap |= (1<<i);
5917 }
5918 else
5919 SimularityBitMap |= (1<<i);
5920 }
5921 }
5922
5923 if ( SimularityBitMap == 0) {
5924 for( i = 0; i < (bound>>2); i++ ) {
5925 if(final_candidate[i] != 0xFF) {
5926 for( j = (i<<2); j < ((i+1)<<2)-2; j++)
5927 result[3][j] = result[final_candidate[i]][j];
5928 bResult = FALSE;
5929 }
5930 }
5931 return bResult;
5932 }
5933 else if (!(SimularityBitMap & 0x03)) { //path A TX OK
5934 for(i = 0; i < 2; i++)
5935 result[3][i] = result[c1][i];
5936 return FALSE;
5937 }
5938 else if (!(SimularityBitMap & 0x0c)) { //path A RX OK
5939 for(i = 2; i < 4; i++)
5940 result[3][i] = result[c1][i];
5941 return FALSE;
5942 }
5943 else if (!(SimularityBitMap & 0x30) && is2T) { //path B TX OK
5944 for(i = 4; i < 6; i++)
5945 result[3][i] = result[c1][i];
5946 return FALSE;
5947 }
5948 else if (!(SimularityBitMap & 0xc0) && is2T) { //path B RX OK
5949 for(i = 6; i < 8; i++)
5950 result[3][i] = result[c1][i];
5951 return FALSE;
5952 }
5953 else
5954 return FALSE;
5955
5956}
5957
5958
5959//bit0 = 1 => Tx OK, bit1 = 1 => Rx OK
5960unsigned char _PHY_PathA_IQK(struct rtl8192cd_priv *priv, char configPathB)
5961{
5962 unsigned int reg_eac, reg_e94, reg_e9c, reg_ea4;
5963 unsigned char result = 0x00;
5964
5965
5966 // RTPRINT(FINIT, INIT_IQK, ("Path-A IQK setting!\n"));
5967#if defined(HIGH_POWER_EXT_PA) && defined(CONFIG_RTL_92C_SUPPORT)
5968 if (priv->pshare->rf_ft_var.use_ext_pa){
5969
5970 //path-A IQK setting
5971 phy_set_bb_reg(priv, 0xe30, bMaskDWord, 0x18008c1f);
5972 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x18008c1f);
5973 phy_set_bb_reg(priv, 0xe38, bMaskDWord, 0x82140102);
5974 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x28160804);
5975
5976 //path-B IQK setting
5977 if(configPathB) {
5978 phy_set_bb_reg(priv, 0xe50, bMaskDWord, 0x18008c22);
5979 phy_set_bb_reg(priv, 0xe54, bMaskDWord, 0x18008c22);
5980 phy_set_bb_reg(priv, 0xe58, bMaskDWord, 0x82140102);
5981 phy_set_bb_reg(priv, 0xe5c, bMaskDWord, 0x28160804);
5982 }
5983}else
5984#endif
5985{
5986 //path-A IQK setting
5987 phy_set_bb_reg(priv, 0xe30, bMaskDWord, 0x10008c1f);
5988 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x10008c1f);
5989 phy_set_bb_reg(priv, 0xe38, bMaskDWord, 0x82140102);
5990 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, ((configPathB |IS_UMC_B_CUT_88C(priv)) ? 0x28160202 : 0x28160502));
5991
5992 //path-B IQK setting
5993 if(configPathB) {
5994 phy_set_bb_reg(priv, 0xe50, bMaskDWord, 0x10008c22);
5995 phy_set_bb_reg(priv, 0xe54, bMaskDWord, 0x10008c22);
5996 phy_set_bb_reg(priv, 0xe58, bMaskDWord, 0x82140102);
5997 phy_set_bb_reg(priv, 0xe5c, bMaskDWord, 0x28160202);
5998 }
5999}
6000
6001
6002
6003 //LO calibration setting
6004 phy_set_bb_reg(priv, 0xe4c, bMaskDWord, 0x001028d1);
6005
6006 //One shot, path A LOK & IQK
6007 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf9000000);
6008 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf8000000);
6009
6010 // delay x ms
6011 delay_ms(IQK_DELAY_TIME);
6012
6013 // Check failed
6014 reg_eac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6015 reg_e94 = phy_query_bb_reg(priv, 0xe94, bMaskDWord);
6016 reg_e9c= phy_query_bb_reg(priv, 0xe9c, bMaskDWord);
6017 reg_ea4= phy_query_bb_reg(priv, 0xea4, bMaskDWord);
6018
6019 if(!(reg_eac & BIT(28)) &&
6020 (((reg_e94 & 0x03FF0000)>>16) != 0x142) &&
6021 (((reg_e9c & 0x03FF0000)>>16) != 0x42) )
6022 result |= 0x01;
6023 else //if Tx not OK, ignore Rx
6024 return result;
6025
6026 if(!(reg_eac & BIT(27)) && //if Tx is OK, check whether Rx is OK
6027 (((reg_ea4 & 0x03FF0000)>>16) != 0x132) &&
6028 (((reg_eac & 0x03FF0000)>>16) != 0x36))
6029 result |= 0x02;
6030 else {
6031// RTPRINT(FINIT, INIT_IQK, ("Path A Rx IQK fail!!\n"));
6032 }
6033
6034 return result;
6035}
6036
6037//bit0 = 1 => Tx OK, bit1 = 1 => Rx OK
6038unsigned char _PHY_PathB_IQK(struct rtl8192cd_priv *priv)
6039{
6040 unsigned int reg_eac, reg_eb4, reg_ebc, reg_ec4, reg_ecc;
6041 unsigned char result = 0x00;
6042#if 0
6043 //path-B IQK setting
6044 RTPRINT(FINIT, INIT_IQK, ("Path-B IQK setting!\n"));
6045 phy_set_bb_reg(pAdapter, 0xe50, bMaskDWord, 0x10008c22);
6046 phy_set_bb_reg(pAdapter, 0xe54, bMaskDWord, 0x10008c22);
6047 phy_set_bb_reg(pAdapter, 0xe58, bMaskDWord, 0x82140102);
6048 phy_set_bb_reg(pAdapter, 0xe5c, bMaskDWord, 0x28160202);
6049
6050 //LO calibration setting
6051 RTPRINT(FINIT, INIT_IQK, ("LO calibration setting!\n"));
6052 phy_set_bb_reg(pAdapter, 0xe4c, bMaskDWord, 0x001028d1);
6053#endif
6054 //One shot, path B LOK & IQK
6055// RTPRINT(FINIT, INIT_IQK, ("One shot, path A LOK & IQK!\n"));
6056 phy_set_bb_reg(priv, 0xe60, bMaskDWord, 0x00000002);
6057 phy_set_bb_reg(priv, 0xe60, bMaskDWord, 0x00000000);
6058
6059 // delay x ms
6060 delay_ms(IQK_DELAY_TIME);
6061
6062 // Check failed
6063 reg_eac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6064 reg_eb4 = phy_query_bb_reg(priv, 0xeb4, bMaskDWord);
6065 reg_ebc= phy_query_bb_reg(priv, 0xebc, bMaskDWord);
6066 reg_ec4= phy_query_bb_reg(priv, 0xec4, bMaskDWord);
6067 reg_ecc= phy_query_bb_reg(priv, 0xecc, bMaskDWord);
6068
6069 if(!(reg_eac & BIT(31)) &&
6070 (((reg_eb4 & 0x03FF0000)>>16) != 0x142) &&
6071 (((reg_ebc & 0x03FF0000)>>16) != 0x42))
6072 result |= 0x01;
6073 else
6074 return result;
6075
6076 if(!(reg_eac & BIT(30)) &&
6077 (((reg_ec4 & 0x03FF0000)>>16) != 0x132) &&
6078 (((reg_ecc & 0x03FF0000)>>16) != 0x36))
6079 result |= 0x02;
6080 else {
6081// RTPRINT(FINIT, INIT_IQK, ("Path B Rx IQK fail!!\n"));
6082 }
6083
6084 return result;
6085
6086}
6087
6088void _phy_path_a_fill_iqk_matrix(struct rtl8192cd_priv *priv, char bIQKOK, int result[][8], unsigned char final_candidate, char bTxOnly)
6089{
6090 int oldval_0, X, TX0_A, reg;
6091 int Y, TX0_C;
6092
6093 if(final_candidate == 0xFF)
6094 return;
6095
6096 else if(bIQKOK) {
6097 oldval_0 = (phy_query_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, bMaskDWord) >> 22) & 0x3FF;
6098
6099 X = result[final_candidate][0];
6100 if ((X & 0x00000200) != 0)
6101 X = X | 0xFFFFFC00;
6102 TX0_A = (X * oldval_0) >> 8;
6103 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, 0x3FF, TX0_A);
6104 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(31), ((X* oldval_0>>7) & 0x1));
6105
6106 Y = result[final_candidate][1];
6107 if ((Y & 0x00000200) != 0)
6108 Y = Y | 0xFFFFFC00;
6109 TX0_C = (Y * oldval_0) >> 8;
6110 phy_set_bb_reg(priv, REG_OFDM_0_XC_TX_AFE, 0xF0000000, ((TX0_C&0x3C0)>>6));
6111 phy_set_bb_reg(priv, REG_OFDM_0_XA_TX_IQ_IMBALANCE, 0x003F0000, (TX0_C&0x3F));
6112 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(29), ((Y* oldval_0>>7) & 0x1));
6113
6114 if(bTxOnly) {
6115// RTPRINT(FINIT, INIT_IQK, ("_phy_path_a_fill_iqk_matrix only Tx OK\n"));
6116 return;
6117 }
6118
6119 reg = result[final_candidate][2];
6120 phy_set_bb_reg(priv, REG_OFDM_0_XA_RX_IQ_IMBALANCE, 0x3FF, reg);
6121
6122 reg = result[final_candidate][3] & 0x3F;
6123 phy_set_bb_reg(priv, REG_OFDM_0_XA_RX_IQ_IMBALANCE, 0xFC00, reg);
6124
6125 reg = (result[final_candidate][3] >> 6) & 0xF;
6126 phy_set_bb_reg(priv, 0xca0, 0xF0000000, reg);
6127 }
6128}
6129
6130
6131void _phy_path_b_fill_iqk_matrix(struct rtl8192cd_priv *priv, char bIQKOK, int result[][8], unsigned char final_candidate, char bTxOnly)
6132{
6133 int oldval_1, X, TX1_A, reg;
6134 int Y, TX1_C;
6135
6136 //RTPRINT(FINIT, INIT_IQK, ("Path B IQ Calibration %s !\n",(bIQKOK)?"Success":"Failed"));
6137
6138 if(final_candidate == 0xFF)
6139 return;
6140
6141 else if(bIQKOK)
6142 {
6143 oldval_1 = (phy_query_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, bMaskDWord) >> 22) & 0x3FF;
6144
6145 X = result[final_candidate][4];
6146 if ((X & 0x00000200) != 0)
6147 X = X | 0xFFFFFC00;
6148 TX1_A = (X * oldval_1) >> 8;
6149 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, 0x3FF, TX1_A);
6150 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(27), ((X* oldval_1>>7) & 0x1));
6151
6152 Y = result[final_candidate][5];
6153 if ((Y & 0x00000200) != 0)
6154 Y = Y | 0xFFFFFC00;
6155 TX1_C = (Y * oldval_1) >> 8;
6156 phy_set_bb_reg(priv, REG_OFDM_0_XD_TX_AFE, 0xF0000000, ((TX1_C&0x3C0)>>6));
6157 phy_set_bb_reg(priv, REG_OFDM_0_XB_TX_IQ_IMBALANCE, 0x003F0000, (TX1_C&0x3F));
6158 phy_set_bb_reg(priv, REG_OFDM_0_ECCA_THRESHOLD, BIT(25), ((Y* oldval_1>>7) & 0x1));
6159
6160 if(bTxOnly)
6161 return;
6162
6163 reg = result[final_candidate][6];
6164 phy_set_bb_reg(priv, REG_OFDM_0_XB_RX_IQ_IMBALANCE, 0x3FF, reg);
6165
6166 reg = result[final_candidate][7] & 0x3F;
6167 phy_set_bb_reg(priv, REG_OFDM_0_XB_RX_IQ_IMBALANCE, 0xFC00, reg);
6168
6169 reg = (result[final_candidate][7] >> 6) & 0xF;
6170 phy_set_bb_reg(priv, REG_OFDM_0_AGC_RSSI_TABLE, 0x0000F000, reg);
6171 }
6172}
6173
6174void _PHY_PathAStandBy_8192C(struct rtl8192cd_priv *priv)
6175{
6176 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
6177 phy_set_bb_reg(priv, 0x840, bMaskDWord, 0x00010000);
6178 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
6179}
6180
6181void _PHY_IQCalibrate(struct rtl8192cd_priv *priv, int result[][8], unsigned char t, char is2T)
6182{
6183 unsigned int i;
6184 unsigned char path_aok, path_bok;
6185 unsigned int ADDA_REG[IQK_ADDA_REG_NUM] = { 0x85c, 0xe6c, 0xe70, 0xe74,
6186 0xe78, 0xe7c, 0xe80, 0xe84,
6187 0xe88, 0xe8c, 0xed0, 0xed4,
6188 0xed8, 0xedc, 0xee0, 0xeec };
6189 unsigned int IQK_MAC_REG[IQK_MAC_REG_NUM] = {0x522, 0x550, 0x551, 0x040};
6190
6191 char isNormal = IS_TEST_CHIP(priv) ? 0 : 1;
6192 unsigned int retryCount = 2;
6193
6194#ifdef MP_TEST
6195 if(priv->pshare->rf_ft_var.mp_specific)
6196 retryCount = 9;
6197#endif
6198
6199 if(t==0) {
6200 // Save ADDA parameters, turn Path A ADDA on
6201 _phy_save_adda_registers(priv, ADDA_REG, priv->pshare->ADDA_backup, APK_AFE_REG_NUM);
6202 _phy_save_mac_registers(priv, IQK_MAC_REG, priv->pshare->IQK_MAC_backup);
6203 }
6204
6205 _phy_path_adda_on(priv, ADDA_REG, TRUE, is2T);
6206
6207 if(t==0) {
6208 // Store 0xC04, 0xC08, 0x874 vale
6209 priv->pshare->RegC04 = phy_query_bb_reg(priv, 0xc04, bMaskDWord);
6210 priv->pshare->RegC08 = phy_query_bb_reg(priv, 0xc08, bMaskDWord);
6211 priv->pshare->Reg874 = phy_query_bb_reg(priv, 0x874, bMaskDWord);
6212 }
6213
6214 //MAC settings
6215 _phy_mac_setting_calibration(priv, IQK_MAC_REG, priv->pshare->IQK_MAC_backup);
6216
6217 //phy_set_bb_reg(priv, 0x800, bMaskDWord, (phy_query_bb_reg(priv, 0x800, bMaskDWord)& ~ BIT(24)));
6218 phy_set_bb_reg(priv, 0xa04, bMaskDWord, (0x0f000000 | (phy_query_bb_reg(priv, 0xa04, bMaskDWord))) );
6219 phy_set_bb_reg(priv, 0xc04, bMaskDWord, 0x03a05600);
6220 phy_set_bb_reg(priv, 0xc08, bMaskDWord, 0x000800e4);
6221 phy_set_bb_reg(priv, 0x874, bMaskDWord, 0x22204000);
6222
6223 phy_set_bb_reg(priv, 0x870, BIT(10), 1);
6224 phy_set_bb_reg(priv, 0x870, BIT(26), 1);
6225 phy_set_bb_reg(priv, 0x860, BIT(10), 0);
6226 phy_set_bb_reg(priv, 0x864, BIT(10), 0);
6227
6228#if defined(HIGH_POWER_EXT_PA) && defined(CONFIG_RTL_92C_SUPPORT)
6229 //Allen TRSW to TX for external PA
6230 phy_set_bb_reg(priv, 0x870, 0x60, 3);
6231 phy_set_bb_reg(priv, 0x870, 0x600000, 3);
6232 phy_set_bb_reg(priv, 0x860, 0x60, 1);
6233 phy_set_bb_reg(priv, 0x864, 0x60, 1);
6234#endif
6235
6236 if(is2T) {
6237 phy_set_bb_reg(priv, 0x840, bMaskDWord, 0x00010000);
6238 phy_set_bb_reg(priv, 0x844, bMaskDWord, 0x00010000);
6239 }
6240
6241 //Page B init
6242 if(isNormal)
6243 phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x00080000);
6244 else
6245 phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x0f600000);
6246
6247 if(is2T) {
6248 if(isNormal)
6249 phy_set_bb_reg(priv, 0xb6c, bMaskDWord, 0x00080000);
6250 else
6251 phy_set_bb_reg(priv, 0xb6c, bMaskDWord, 0x0f600000);
6252 }
6253
6254 // IQ calibration setting
6255 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
6256 phy_set_bb_reg(priv, 0xe40, bMaskDWord, 0x01007c00);
6257 phy_set_bb_reg(priv, 0xe44, bMaskDWord, 0x01004800);
6258
6259 for(i = 0 ; i < retryCount ; i++){
6260 path_aok = _PHY_PathA_IQK(priv, is2T);
6261 if(path_aok == 0x03){
6262 result[t][0] = (phy_query_bb_reg(priv, 0xe94, bMaskDWord)&0x3FF0000)>>16;
6263 result[t][1] = (phy_query_bb_reg(priv, 0xe9c, bMaskDWord)&0x3FF0000)>>16;
6264 result[t][2] = (phy_query_bb_reg(priv, 0xea4, bMaskDWord)&0x3FF0000)>>16;
6265 result[t][3] = (phy_query_bb_reg(priv, 0xeac, bMaskDWord)&0x3FF0000)>>16;
6266 break;
6267 }
6268 else if (i == (retryCount-1) && path_aok == 0x01) //Tx IQK OK
6269 {
6270 result[t][0] = (phy_query_bb_reg(priv, 0xe94, bMaskDWord)&0x3FF0000)>>16;
6271 result[t][1] = (phy_query_bb_reg(priv, 0xe9c, bMaskDWord)&0x3FF0000)>>16;
6272 }
6273 }
6274
6275 if(0x00 == path_aok){
6276// RTPRINT(FINIT, INIT_IQK, ("Path A IQK failed!!\n"));
6277 }
6278
6279 if(is2T){
6280 _PHY_PathAStandBy_8192C(priv);
6281
6282 // Turn Path B ADDA on
6283 _phy_path_adda_on(priv, ADDA_REG, FALSE, is2T);
6284
6285 for(i = 0 ; i < retryCount ; i++){
6286 path_bok = _PHY_PathB_IQK(priv);
6287 if(path_bok == 0x03){
6288// RTPRINT(FINIT, INIT_IQK, ("Path B IQK Success!!\n"));
6289 result[t][4] = (phy_query_bb_reg(priv, 0xeb4, bMaskDWord)&0x3FF0000)>>16;
6290 result[t][5] = (phy_query_bb_reg(priv, 0xebc, bMaskDWord)&0x3FF0000)>>16;
6291 result[t][6] = (phy_query_bb_reg(priv, 0xec4, bMaskDWord)&0x3FF0000)>>16;
6292 result[t][7] = (phy_query_bb_reg(priv, 0xecc, bMaskDWord)&0x3FF0000)>>16;
6293 break;
6294 }
6295 else if (i == (retryCount - 1) && path_bok == 0x01) //Tx IQK OK
6296 {
6297// RTPRINT(FINIT, INIT_IQK, ("Path B Only Tx IQK Success!!\n"));
6298 result[t][4] = (phy_query_bb_reg(priv, 0xeb4, bMaskDWord)&0x3FF0000)>>16;
6299 result[t][5] = (phy_query_bb_reg(priv, 0xebc, bMaskDWord)&0x3FF0000)>>16;
6300 }
6301 }
6302
6303 if(0x00 == path_bok){
6304// RTPRINT(FINIT, INIT_IQK, ("Path B IQK failed!!\n"));
6305 }
6306 }
6307
6308 //Back to BB mode, load original value
6309// RTPRINT(FINIT, INIT_IQK, ("IQK:Back to BB mode, load original value!\n"));
6310 phy_set_bb_reg(priv, 0xc04, bMaskDWord, priv->pshare->RegC04);
6311 phy_set_bb_reg(priv, 0x874, bMaskDWord, priv->pshare->Reg874);
6312 phy_set_bb_reg(priv, 0xc08, bMaskDWord, priv->pshare->RegC08);
6313
6314 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
6315
6316 // Restore RX initial gain
6317 phy_set_bb_reg(priv, 0x840, bMaskDWord, 0x00032ed3);
6318
6319 if(is2T)
6320 phy_set_bb_reg(priv, 0x844, bMaskDWord, 0x00032ed3);
6321
6322 if(t!=0) {
6323 // Reload ADDA power saving parameters
6324 _phy_reload_adda_registers(priv, ADDA_REG, priv->pshare->ADDA_backup, 16);
6325
6326 // Reload MAC parameters
6327 _phy_reload_mac_registers(priv, IQK_MAC_REG, priv->pshare->IQK_MAC_backup);
6328 }
6329}
6330
6331
6332void PHY_IQCalibrate_92C(struct rtl8192cd_priv *priv)
6333{
6334 int result[4][8]; //last is final result
6335 unsigned char i, final_candidate;
6336 char bPathAOK, bPathBOK;
6337 int RegE94, RegE9C, RegEA4, RegEAC, RegEB4, RegEBC, RegEC4, RegECC, RegTmp = 0;
6338 char is12simular, is13simular, is23simular;
6339 unsigned int temp_870, temp_860, temp_864, temp_a04;
6340
6341#ifdef MP_TEST
6342 if (!priv->pshare->rf_ft_var.mp_specific)
6343#endif
6344 {
6345 if (priv->pshare->iqk_2g_done)
6346 return;
6347 priv->pshare->iqk_2g_done = 1;
6348 }
6349
6350 priv->pshare->IQK_total_cnt++;
6351
6352 temp_870 = phy_query_bb_reg(priv, 0x870, bMaskDWord);
6353 temp_860 = phy_query_bb_reg(priv, 0x860, bMaskDWord);
6354 temp_864 = phy_query_bb_reg(priv, 0x864, bMaskDWord);
6355 //temp_800 = phy_query_bb_reg(priv, 0x800, bMaskDWord);
6356 temp_a04 = phy_query_bb_reg(priv, 0xa04, bMaskDWord);
6357
6358 memset(result, 0, sizeof(result));
6359
6360 final_candidate = 0xff;
6361 bPathAOK = FALSE;
6362 bPathBOK = FALSE;
6363 is12simular = FALSE;
6364 is23simular = FALSE;
6365 is13simular = FALSE;
6366
6367 for (i=0; i<3; i++) {
6368 _PHY_IQCalibrate(priv, result, i, (GET_CHIP_VER(priv) == VERSION_8192C ? 1 : 0));
6369
6370 if(i == 1) {
6371 is12simular = _PHY_SimularityCompare(priv, result, 0, 1);
6372 if(is12simular) {
6373 final_candidate = 0;
6374 break;
6375 }
6376 }
6377
6378 if(i == 2) {
6379 is13simular = _PHY_SimularityCompare(priv, result, 0, 2);
6380 if(is13simular) {
6381 final_candidate = 0;
6382 break;
6383 }
6384
6385 is23simular = _PHY_SimularityCompare(priv, result, 1, 2);
6386 if(is23simular)
6387 final_candidate = 1;
6388 else
6389 {
6390 for(i = 0; i < 8; i++)
6391 RegTmp += result[3][i];
6392
6393 if(RegTmp != 0)
6394 final_candidate = 3;
6395 else
6396 final_candidate = 0xFF;
6397 }
6398 }
6399 }
6400
6401
6402 RTL_W32(0x870, temp_870);
6403 RTL_W32(0x860, temp_860);
6404 RTL_W32(0x864, temp_864);
6405 //RTL_W32(0x800, temp_800);
6406 RTL_W32(0xa04, temp_a04);
6407
6408 //load 0xe30 IQC default value
6409 if(GET_CHIP_VER(priv) == VERSION_8188C) {
6410 RTL_W32(0xe30, 0x01008c00);
6411 RTL_W32(0xe34, 0x01008c00);
6412 }
6413
6414 for (i=0; i<4; i++) {
6415 RegE94 = result[i][0];
6416 RegE9C = result[i][1];
6417 RegEA4 = result[i][2];
6418 RegEAC = result[i][3];
6419 RegEB4 = result[i][4];
6420 RegEBC = result[i][5];
6421 RegEC4 = result[i][6];
6422 RegECC = result[i][7];
6423 DEBUG_INFO("IQK: RegE94=%x RegE9C=%x RegEA4=%x RegEAC=%x RegEB4=%x RegEBC=%x RegEC4=%x RegECC=%x\n ", RegE94, RegE9C, RegEA4, RegEAC, RegEB4, RegEBC, RegEC4, RegECC);
6424 }
6425
6426 if(final_candidate != 0xff) {
6427 priv->pshare->rege94 = RegE94 = result[final_candidate][0];
6428 priv->pshare->rege9c = RegE9C = result[final_candidate][1];
6429 RegEA4 = result[final_candidate][2];
6430 RegEAC = result[final_candidate][3];
6431 priv->pshare->regeb4 = RegEB4 = result[final_candidate][4];
6432 priv->pshare->regebc = RegEBC = result[final_candidate][5];
6433 RegEC4 = result[final_candidate][6];
6434 RegECC = result[final_candidate][7];
6435 DEBUG_INFO ("IQK: final_candidate is %x\n",final_candidate);
6436 DEBUG_INFO ("IQK: RegE94=%x RegE9C=%x RegEA4=%x RegEAC=%x RegEB4=%x RegEBC=%x RegEC4=%x RegECC=%x\n ", RegE94, RegE9C, RegEA4, RegEAC, RegEB4, RegEBC, RegEC4, RegECC);
6437 bPathAOK = bPathBOK = TRUE;
6438 }
6439 else {
6440 priv->pshare->rege94 = priv->pshare->regeb4 = 0x100; //X default value
6441 priv->pshare->rege9c = priv->pshare->regebc = 0x0; //Y default value
6442 priv->pshare->IQK_fail_cnt++;
6443 }
6444
6445 if((RegE94 != 0)/*&&(RegEA4 != 0)*/)
6446 _phy_path_a_fill_iqk_matrix(priv, bPathAOK, result, final_candidate, (RegEA4 == 0)? 1 :0);
6447 if(GET_CHIP_VER(priv) == VERSION_8192C){
6448 if((RegEB4 != 0)/*&&(RegEC4 != 0)*/)
6449 _phy_path_b_fill_iqk_matrix(priv, bPathBOK, result, final_candidate, (RegEC4 == 0)? 1 :0);
6450 }
6451}
6452
6453#endif
6454
6455
6456// 92d IQK
6457#ifdef CONFIG_RTL_92D_SUPPORT
6458void IQK_92D_5G_n(struct rtl8192cd_priv *priv)
6459{
6460 unsigned int temp_a04, temp_c04, temp_874, temp_c08, temp_870,
6461 temp_860, temp_864, temp_88c, temp_c50, temp_c58, temp_b30,
6462 switch2PI=0, X, reg; //oldval_0, oldval_1, TX0_A, TX1_A;
6463 u8 temp_522, temp_550, temp_551;
6464 unsigned int cal_num=0, cal_retry=0, ADDA_backup[IQK_ADDA_REG_NUM];
6465 int Y, result[8][3], result_final[8]={0,0,0,0,0,0,0,0}; //TX0_C, TX1_C;
6466
6467 unsigned int i, RX0REG0xe40[3], RX0REG0xe40_final=0, REG0xe40, REG0xe94, REG0xe9c, delay_count;
6468 unsigned int REG0xeac, RX1REG0xe40[3], RX1REG0xe40_final=0, REG0xeb4, REG0xea4,REG0xec4;
6469 unsigned char TX0IQKOK = FALSE, TX1IQKOK = FALSE;
6470 unsigned int TX_X0, TX_Y0, TX_X1, TX_Y1, RX_X0, RX_Y0, RX_X1, RX_Y1;
6471 unsigned int ADDA_REG[IQK_ADDA_REG_NUM] = {0x85c, 0xe6c, 0xe70, 0xe74, 0xe78, 0xe7c, 0xe80, 0xe84,
6472 0xe88, 0xe8c, 0xed0, 0xed4, 0xed8, 0xedc, 0xee0, 0xeec};
6473#ifdef CONFIG_RTL_92D_DMDP
6474 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)
6475 return IQK_92D_5G_phy0_n(priv);
6476#endif
6477
6478 //always do IQK for MP mode
6479#ifdef MP_TEST
6480 if (!priv->pshare->rf_ft_var.mp_specific)
6481#endif
6482 {
6483 if (priv->pmib->dot11RFEntry.macPhyMode == SINGLEMAC_SINGLEPHY) {
6484 if (priv->pshare->iqk_5g_done)
6485 return;
6486 priv->pshare->iqk_5g_done = 1;
6487 }
6488 }
6489
6490 printk(">> %s \n",__FUNCTION__);
6491
6492 watchdog_kick();
6493
6494 /*
6495 * Save MAC default value
6496 */
6497 temp_522 = RTL_R8(0x522);
6498 temp_550 = RTL_R8(0x550);
6499 temp_551 = RTL_R8(0x551);
6500
6501 /*
6502 * Save BB Parameter
6503 */
6504 temp_a04 = RTL_R32(0xa04);//RTL_R32(0x800);
6505 temp_c04 = RTL_R32(0xc04);
6506 temp_874 = RTL_R32(0x874);
6507 temp_c08 = RTL_R32(0xc08);
6508 temp_870 = RTL_R32(0x870);
6509 temp_860 = RTL_R32(0x860);
6510 temp_864 = RTL_R32(0x864);
6511 temp_88c = RTL_R32(0x88c);
6512 temp_c50 = RTL_R32(0xc50); // 01/11/2011 update
6513 temp_c58 = RTL_R32(0xc58); // 01/11/2011 update
6514 temp_b30 = RTL_R32(0xb30); // 03/03/2011 update
6515
6516 /*
6517 * Save AFE Parameters
6518 */
6519 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
6520 ADDA_backup[i] = RTL_R32(ADDA_REG[i]);
6521
6522 /*
6523 * ==============
6524 * Path-A TX/RX IQK
6525 * ==============
6526 */
6527 while (cal_num < 3) {
6528 /*
6529 * Path-A AFE all on
6530 */
6531 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
6532 RTL_W32(ADDA_REG[i], 0x04db25a4);
6533
6534 /*
6535 * MAC register setting
6536 */
6537 RTL_W8(0x522, 0x3f);
6538 RTL_W8(0x550, RTL_R8(0x550)& (~BIT(3)));
6539 RTL_W8(0x551, RTL_R8(0x551)& (~BIT(3)));
6540
6541 /*
6542 * IQK must be done in PI mode
6543 */
6544 if (!phy_query_bb_reg(priv, 0x820, BIT(8)) || !phy_query_bb_reg(priv, 0x828, BIT(8))) {
6545 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000100);
6546 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000100);
6547 switch2PI++;
6548 }
6549
6550 /*
6551 * BB setting
6552 */
6553 //phy_set_bb_reg(priv, 0x800, BIT(24), 0);
6554 phy_set_bb_reg(priv, 0xa04, bMaskDWord, (0x0f000000 | temp_a04) );
6555 phy_set_bb_reg(priv, 0xc04, bMaskDWord, 0x03a05600);
6556 phy_set_bb_reg(priv, 0xc08, bMaskDWord, 0x000800e4);
6557 phy_set_bb_reg(priv, 0x874, bMaskDWord, 0x22208000);
6558 phy_set_bb_reg(priv, 0x88c, BIT(23)|BIT(22)|BIT(21)|BIT(20), 0xf);
6559 phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0x00a00000); // 03/03/2011 update
6560
6561 /*
6562 * AP or IQK
6563 */
6564 //phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x0f600000);
6565 //phy_set_bb_reg(priv, 0xb6c, bMaskDWord, 0x0f600000);
6566
6567 // IQK-R03 2011/02/16 update
6568
6569 //path A AP setting for IQK
6570 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0);
6571 phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x20000000);
6572 //path B AP setting for IQK
6573 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0);
6574 phy_set_bb_reg(priv, 0xb6c, bMaskDWord, 0x20000000);
6575
6576 /*
6577 * IQK global setting
6578 */
6579 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
6580 phy_set_bb_reg(priv, 0xe40, bMaskDWord, 0x10007c00);
6581 phy_set_bb_reg(priv, 0xe44, bMaskDWord, 0x01004800);
6582
6583 /*
6584 * path-A IQK setting
6585 */
6586 phy_set_bb_reg(priv, 0xe30, bMaskDWord, 0x18008c1f);
6587 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x18008c1f);
6588 phy_set_bb_reg(priv, 0xe38, bMaskDWord, 0x82140307); // 01/11/2011 update
6589#ifdef USB_POWER_SUPPORT
6590 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x68160c66);
6591#else
6592 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x68160960); // 01/11/2011 update
6593#endif
6594
6595 /*
6596 * path-B IQK setting
6597 */
6598 phy_set_bb_reg(priv, 0xe50, bMaskDWord, 0x18008c2f);
6599 phy_set_bb_reg(priv, 0xe54, bMaskDWord, 0x18008c2f);
6600 phy_set_bb_reg(priv, 0xe58, bMaskDWord, 0x82110000);
6601 phy_set_bb_reg(priv, 0xe5c, bMaskDWord, 0x68110000);
6602
6603 /*
6604 * LO calibration setting
6605 */
6606 phy_set_bb_reg(priv, 0xe4c, bMaskDWord, 0x00462911);
6607
6608#ifdef USB_POWER_SUPPORT
6609 // path-A TRSW setting
6610 phy_set_bb_reg(priv, 0x870, BIT(6)|BIT(5), 3);
6611 phy_set_bb_reg(priv, 0x860, BIT(6)|BIT(5), 3);
6612#else
6613 /*
6614 * path-A PA on
6615 */
6616 /*
6617 phy_set_bb_reg(priv, 0x870, BIT(11)|BIT(10), 3);
6618 phy_set_bb_reg(priv, 0x870, BIT(6)|BIT(5), 3);
6619 phy_set_bb_reg(priv, 0x860, BIT(11)|BIT(10), 3);
6620 */
6621 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07000f60); // 01/11/2011 update
6622 phy_set_bb_reg(priv, 0x860, bMaskDWord, 0x66e60e30); // 01/11/2011 update
6623#endif
6624 /*
6625 * One shot, path A LOK & IQK
6626 */
6627 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf9000000);
6628 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf8000000);
6629
6630 /*
6631 * Delay 10 ms
6632 */
6633 delay_ms(10);
6634
6635 delay_count = 0;
6636 while (1){
6637 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6638 if ((REG0xeac&BIT(26))||(delay_count>20)){
6639 break;
6640 }else {
6641 delay_ms(1);
6642 delay_count++;
6643 }
6644 }
6645 /*
6646 * Check_TX_IQK_A_result
6647 */
6648 REG0xe40 = phy_query_bb_reg(priv, 0xe40, bMaskDWord);
6649 REG0xe94 = phy_query_bb_reg(priv, 0xe94, bMaskDWord);
6650 if(((REG0xeac&BIT(28)) == 0) && (((REG0xe94&0x3FF0000)>>16)!=0x142)) {
6651 TX0IQKOK = TRUE;
6652 REG0xe9c = phy_query_bb_reg(priv, 0xe9c, bMaskDWord);
6653 TX_X0 = (phy_query_bb_reg(priv, 0xe94, bMaskDWord)&0x3FF0000)>>16;
6654 TX_Y0 = (phy_query_bb_reg(priv, 0xe9c, bMaskDWord)&0x3FF0000)>>16;
6655 RX0REG0xe40[cal_num] = (REG0xe40 & 0xfc00fc00) | (TX_X0<<16) | TX_Y0;
6656 DEBUG_INFO("TX_X0 %08x TX_Y0 %08x RX0REG0xe40 %08x\n", TX_X0, TX_Y0, RX0REG0xe40[cal_num]);
6657 result[0][cal_num] = TX_X0;
6658 result[1][cal_num] = TX_Y0;
6659 } else {
6660 TX0IQKOK = FALSE;
6661 if (++cal_retry >= 10) {
6662 printk("%s Path-A Tx/Rx Check\n",__FUNCTION__);
6663 break;
6664 }
6665 }
6666
6667 /*
6668 * Check_RX_IQK_A_result
6669 */
6670 if(TX0IQKOK == TRUE) {
6671 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6672 REG0xea4 = phy_query_bb_reg(priv, 0xea4, bMaskDWord);
6673 if(((REG0xeac&BIT(27)) == 0) && (((REG0xea4&0x3FF0000)>>16)!=0x132)) {
6674 RX_X0 = (phy_query_bb_reg(priv, 0xea4, bMaskDWord)&0x3FF0000)>>16;
6675 RX_Y0 = (phy_query_bb_reg(priv, 0xeac, bMaskDWord)&0x3FF0000)>>16;
6676 DEBUG_INFO("RX_X0 %08x RX_Y0 %08x\n", RX_X0, RX_Y0);
6677 result[2][cal_num] = RX_X0;
6678 result[3][cal_num] = RX_Y0;
6679 cal_num++;
6680 } else {
6681 phy_set_bb_reg(priv, 0xc14, bMaskDWord, 0x40000100);
6682 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x19008c00);
6683 if (++cal_retry >= 10) {
6684 printk("%s Path-A Tx/Rx Check\n",__FUNCTION__);
6685 break;
6686 }
6687 }
6688 }
6689 }
6690
6691 if (cal_num == 3) {
6692 result_final[0] = get_mean_of_2_close_value(result[0]);
6693 result_final[1] = get_mean_of_2_close_value(result[1]);
6694 result_final[2] = get_mean_of_2_close_value(result[2]);
6695 result_final[3] = get_mean_of_2_close_value(result[3]);
6696 RX0REG0xe40_final = 0x80000000 | get_mean_of_2_close_value(RX0REG0xe40);
6697
6698 priv->pshare->RegE94=result_final[0];
6699 priv->pshare->RegE9C=result_final[1];
6700 } else {
6701 priv->pshare->RegE94=0x100;
6702 priv->pshare->RegE9C=0x00;
6703 }
6704
6705 /*
6706 * Path-A PA off
6707 */
6708 phy_set_bb_reg(priv, 0x870, bMaskDWord, temp_870);
6709 phy_set_bb_reg(priv, 0x860, bMaskDWord, temp_860);
6710
6711
6712 /*
6713 * ==============
6714 * Path-B TX/RX IQK
6715 * ==============
6716 */
6717 cal_num = cal_retry = 0;
6718 while (cal_num < 3) {
6719 /*
6720 * Path-B AFE all on
6721 */
6722 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
6723 phy_set_bb_reg(priv, ADDA_REG[i], bMaskDWord, 0x0b1b25a4);
6724
6725 /*
6726 * path-A IQK setting
6727 */
6728 phy_set_bb_reg(priv, 0xe30, bMaskDWord, 0x18008c1f);
6729 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x18008c1f);
6730 phy_set_bb_reg(priv, 0xe38, bMaskDWord, 0x82110000);
6731 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x68110000);
6732
6733 /*
6734 * path-B IQK setting
6735 */
6736 phy_set_bb_reg(priv, 0xe50, bMaskDWord, 0x18008c22);
6737 phy_set_bb_reg(priv, 0xe54, bMaskDWord, 0x18008c22);
6738 phy_set_bb_reg(priv, 0xe58, bMaskDWord, 0x82140307); // 01/11/2011 update
6739
6740 // 01/11/2011 update
6741#ifdef USB_POWER_SUPPORT
6742 phy_set_bb_reg(priv, 0xe5c, bMaskDWord, 0x68160c66);
6743#else
6744 phy_set_bb_reg(priv, 0xe5c, bMaskDWord, 0x68160960); // 01/11/2011 update
6745#endif
6746
6747 /*
6748 * LO calibration setting
6749 */
6750 phy_set_bb_reg(priv, 0xe4c, bMaskDWord, 0x00462911);
6751
6752#ifdef USB_POWER_SUPPORT
6753 phy_set_bb_reg(priv, 0x870, BIT(22)|BIT(21), 3);
6754 phy_set_bb_reg(priv, 0x864, BIT(6)|BIT(5), 3);
6755#else
6756 /*
6757 * path-B PA on
6758 */
6759 /*
6760 phy_set_bb_reg(priv, 0x870, BIT(27)|BIT(26), 3);
6761 phy_set_bb_reg(priv, 0x870, BIT(22)|BIT(21), 3);
6762 phy_set_bb_reg(priv, 0x864, BIT(11)|BIT(10), 3);
6763 */
6764 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x0f600700);
6765 phy_set_bb_reg(priv, 0x864, bMaskDWord, 0x061f0d30);
6766#endif
6767
6768 /*
6769 * One shot, path A LOK & IQK
6770 */
6771 phy_set_bb_reg(priv, 0xe60, bMaskDWord, 0x00000002);
6772 phy_set_bb_reg(priv, 0xe60, bMaskDWord, 0x00000000);
6773
6774 /*
6775 * Delay 10 ms
6776 */
6777 delay_ms(10);
6778
6779 delay_count = 0;
6780 while (1){
6781 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6782 if ((REG0xeac&BIT(29))||(delay_count>20)){
6783 break;
6784 }else {
6785 delay_ms(1);
6786 delay_count++;
6787 }
6788 }
6789 /*
6790 * Check_TX_IQK_B_result
6791 */
6792 REG0xe40 = phy_query_bb_reg(priv, 0xe40, bMaskDWord);
6793 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6794 REG0xeb4 = phy_query_bb_reg(priv, 0xeb4, bMaskDWord);
6795 if(((REG0xeac&BIT(31)) == 0) && ((REG0xeb4&0x3FF0000)!=0x142)) {
6796 TX1IQKOK = TRUE;
6797 TX_X1 = (phy_query_bb_reg(priv, 0xeb4, bMaskDWord)&0x3FF0000)>>16;
6798 TX_Y1 = (phy_query_bb_reg(priv, 0xebc, bMaskDWord)&0x3FF0000)>>16;
6799 RX1REG0xe40[cal_num] = (REG0xe40 & 0xfc00fc00) | (TX_X1<<16) | TX_Y1;
6800 DEBUG_INFO("TX_X1 %08x TX_Y1 %08x RX1REG0xe40 %08x\n", TX_X1, TX_Y1, RX1REG0xe40[cal_num]);
6801 result[4][cal_num] = TX_X1;
6802 result[5][cal_num] = TX_Y1;
6803 } else {
6804 TX1IQKOK = FALSE;
6805 if (++cal_retry >= 10) {
6806 printk("%s Path-B Tx/Rx Check\n",__FUNCTION__);
6807 break;
6808 }
6809 }
6810
6811 /*
6812 * Check_RX_IQK_B_result
6813 */
6814 if(TX1IQKOK == TRUE) {
6815 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
6816 REG0xec4 = phy_query_bb_reg(priv, 0xec4, bMaskDWord);
6817 if(((REG0xeac&BIT(30)) == 0) && (((REG0xec4&0x3FF0000)>>16)!=0x132)) {
6818 RX_X1 = (phy_query_bb_reg(priv, 0xec4, bMaskDWord)&0x3FF0000)>>16;
6819 RX_Y1 = (phy_query_bb_reg(priv, 0xecc, bMaskDWord)&0x3FF0000)>>16;
6820 DEBUG_INFO("RX_X1 %08x RX_Y1 %08x\n", RX_X1, RX_Y1);
6821 result[6][cal_num] = RX_X1;
6822 result[7][cal_num] = RX_Y1;
6823 cal_num++;
6824 } else {
6825 phy_set_bb_reg(priv, 0xc1c, bMaskDWord, 0x40000100);
6826 phy_set_bb_reg(priv, 0xe54, bMaskDWord, 0x19008c00);
6827 if (++cal_retry >= 10) {
6828 printk("%s Path-B Tx/Rx Check\n",__FUNCTION__);
6829 break;
6830 }
6831 }
6832 }
6833 }
6834
6835 if (cal_num == 3) {
6836 result_final[4] = get_mean_of_2_close_value(result[4]);
6837 result_final[5] = get_mean_of_2_close_value(result[5]);
6838 result_final[6] = get_mean_of_2_close_value(result[6]);
6839 result_final[7] = get_mean_of_2_close_value(result[7]);
6840 RX1REG0xe40_final = 0x80000000 | get_mean_of_2_close_value(RX1REG0xe40);
6841
6842 priv->pshare->RegEB4=result_final[4];
6843 priv->pshare->RegEBC=result_final[5];
6844 } else {
6845 priv->pshare->RegEB4=0x100;
6846 priv->pshare->RegEBC=0x00;
6847 }
6848
6849 /*
6850 * Fill IQK result for Path A
6851 */
6852 if (result_final[0]) {
6853 /*
6854 oldval_0 = (phy_query_bb_reg(priv, 0xc80, bMaskDWord) >> 22) & 0x3FF;
6855 X = result_final[0];
6856 if ((X & 0x00000200) != 0)
6857 X = X | 0xFFFFFC00;
6858 TX0_A = (X * oldval_0) >> 8;
6859 phy_set_bb_reg(priv, 0xc80, 0x3FF, TX0_A);
6860 phy_set_bb_reg(priv, 0xc4c, BIT(24), ((X* oldval_0>>7) & 0x1));
6861
6862 Y = result_final[1];
6863 if ((Y & 0x00000200) != 0)
6864 Y = Y | 0xFFFFFC00;
6865 TX0_C = (Y * oldval_0) >> 8;
6866 phy_set_bb_reg(priv, 0xc94, 0xF0000000, ((TX0_C&0x3C0)>>6));
6867 phy_set_bb_reg(priv, 0xc80, 0x003F0000, (TX0_C&0x3F));
6868 phy_set_bb_reg(priv, 0xc4c, BIT(26), ((Y* oldval_0>>7) & 0x1));
6869 */
6870
6871 // IQK-R03 2011/02/16 update
6872 X = result_final[0];
6873 Y = result_final[1];
6874 //printk("X=%x Y=%x\n",X,Y);
6875 //Path-A OFDM_A
6876 phy_set_bb_reg(priv, 0xe30, 0x03FF0000, X);
6877 phy_set_bb_reg(priv, 0xc4c, BIT(24), 0);
6878 //Path-A OFDM_C
6879 phy_set_bb_reg(priv, 0xe30, 0x000003FF, Y);
6880 phy_set_bb_reg(priv, 0xc4c, BIT(26), 0);
6881
6882 if(result_final[2]) {
6883 reg = result_final[2];
6884 phy_set_bb_reg(priv, 0xc14, 0x3FF, reg);
6885 reg = result_final[3] & 0x3F;
6886 phy_set_bb_reg(priv, 0xc14, 0xFC00, reg);
6887
6888 reg = (result_final[3] >> 6) & 0xF;
6889 phy_set_bb_reg(priv, 0xca0, 0xF0000000, reg);
6890
6891 phy_set_bb_reg(priv, 0xe34, 0x03FF0000, result_final[2]); // X
6892 phy_set_bb_reg(priv, 0xe34, 0x3FF, result_final[3]); //Y
6893 }
6894 }
6895
6896 /*
6897 * Fill IQK result for Path B
6898 */
6899 if (result_final[4]) {
6900 /*
6901 oldval_1 = (phy_query_bb_reg(priv, 0xc88, bMaskDWord) >> 22) & 0x3FF;
6902
6903 X = result_final[4];
6904 if ((X & 0x00000200) != 0)
6905 X = X | 0xFFFFFC00;
6906 TX1_A = (X * oldval_1) >> 8;
6907 phy_set_bb_reg(priv, 0xc88, 0x3FF, TX1_A);
6908 phy_set_bb_reg(priv, 0xc4c, BIT(28), ((X* oldval_1>>7) & 0x1));
6909
6910 Y = result_final[5];
6911 if ((Y & 0x00000200) != 0)
6912 Y = Y | 0xFFFFFC00;
6913 TX1_C = (Y * oldval_1) >> 8;
6914 phy_set_bb_reg(priv, 0xc9c, 0xF0000000, ((TX1_C&0x3C0)>>6));
6915 phy_set_bb_reg(priv, 0xc88, 0x003F0000, (TX1_C&0x3F));
6916 phy_set_bb_reg(priv, 0xc4c, BIT(30), ((Y* oldval_1>>7) & 0x1));
6917 */
6918
6919 // IQK-R03 2011/02/16 update
6920 X = result_final[4];
6921 Y = result_final[5];
6922 //printk("X=%x Y=%x\n",X,Y);
6923 //Path-A OFDM_A
6924 phy_set_bb_reg(priv, 0xe50, 0x03FF0000, X);
6925 phy_set_bb_reg(priv, 0xc4c, BIT(28), 0);
6926 //Path-A OFDM_C
6927 phy_set_bb_reg(priv, 0xe50, 0x000003FF, Y);
6928 phy_set_bb_reg(priv, 0xc4c, BIT(30), 0);
6929
6930 if(result_final[6]) {
6931 reg = result_final[6];
6932 phy_set_bb_reg(priv, 0xc1c, 0x3FF, reg);
6933
6934 reg = result_final[7] & 0x3F;
6935 phy_set_bb_reg(priv, 0xc1c, 0xFC00, reg);
6936
6937 reg = (result_final[7] >> 6) & 0xF;
6938 phy_set_bb_reg(priv, 0xc78, 0x0000F000, reg);
6939
6940 phy_set_bb_reg(priv, 0xe54, 0x03FF0000, result_final[6]); // X
6941 phy_set_bb_reg(priv, 0xe54, 0x3FF, result_final[7]); //Y
6942 }
6943 }
6944
6945 /*
6946 * Path B PA off
6947 */
6948 phy_set_bb_reg(priv, 0x870, bMaskDWord, temp_870);
6949 phy_set_bb_reg(priv, 0x864, bMaskDWord, temp_864);
6950
6951 /*
6952 * Exit IQK mode
6953 */
6954 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
6955 phy_set_bb_reg(priv, 0xc04, bMaskDWord, temp_c04);
6956 phy_set_bb_reg(priv, 0xc08, bMaskDWord, temp_c08);
6957 phy_set_bb_reg(priv, 0x874, bMaskDWord, temp_874);
6958 //phy_set_bb_reg(priv, 0x800, bMaskDWord, temp_800);
6959 phy_set_bb_reg(priv, 0xa04, bMaskDWord, temp_a04);
6960 phy_set_bb_reg(priv, 0x88c, bMaskDWord, temp_88c);
6961 phy_set_bb_reg(priv, 0xb30, bMaskDWord, temp_b30); // 03/03/2011 update
6962 //phy_set_bb_reg(priv, 0x840, bMaskDWord, 0x00032fff); // 01/11/2011 update
6963 //phy_set_bb_reg(priv, 0x844, bMaskDWord, 0x00032fff); // 01/11/2011 update
6964
6965 // IQK-R03 2011/02/16 update
6966 //path A IQ path to DP block
6967 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x010170b8);
6968 //path B IQ path to DP block
6969 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x010170b8);
6970
6971 //path AB to initial gain
6972 phy_set_bb_reg(priv, 0xc50, bMaskDWord, 0x50); // 01/11/2011 update
6973 phy_set_bb_reg(priv, 0xc50, bMaskDWord, temp_c50); // 01/11/2011 update
6974 phy_set_bb_reg(priv, 0xc58, bMaskDWord, 0x50); // 01/11/2011 update
6975 phy_set_bb_reg(priv, 0xc58, bMaskDWord, temp_c58); // 01/11/2011 update
6976
6977
6978 /*
6979 * Reload MAC default value
6980 */
6981 RTL_W8(0x550, temp_550);
6982 RTL_W8(0x551, temp_551);
6983 RTL_W8(0x522, temp_522);
6984
6985 /*
6986 * Switch back to SI if needed, after IQK
6987 */
6988 if (switch2PI) {
6989 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000000);
6990 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000000);
6991 }
6992
6993 /*
6994 * Reload ADDA power saving parameters
6995 */
6996 for(i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
6997 phy_set_bb_reg(priv, ADDA_REG[i], bMaskDWord, ADDA_backup[i]);
6998
6999
7000#if 0 //def CLIENT_MODE
7001 clnt_save_IQK_res(priv);
7002#endif
7003
7004 if (!TX0IQKOK && !TX1IQKOK)
7005 priv->pshare->IQK_fail_cnt++;
7006}
7007
7008
7009void IQK_92D_2G(struct rtl8192cd_priv *priv)
7010{
7011 unsigned int cal_num=0, cal_retry=0, Oldval=0, temp_c04=0, temp_c08=0, temp_874=0, temp_eac;
7012 unsigned int cal_e94, cal_e9c, cal_ea4, cal_eac, cal_eb4, cal_ebc, cal_ec4, cal_ecc;
7013 unsigned int X, Y, val_e94[3], val_e9c[3], val_ea4[3], val_eac[3], val_eb4[3], val_ebc[3], val_ec4[3], val_ecc[3];
7014 unsigned int ADDA_REG[IQK_ADDA_REG_NUM] = {0x85c, 0xe6c, 0xe70, 0xe74, 0xe78, 0xe7c, 0xe80, 0xe84,
7015 0xe88, 0xe8c, 0xed0, 0xed4, 0xed8, 0xedc, 0xee0, 0xeec};
7016 unsigned int ADDA_backup[IQK_ADDA_REG_NUM], i;
7017 u8 temp_522, temp_550, temp_551;
7018 u32 temp_040, temp_a04, temp_870, temp_860, temp_864, temp_88c;
7019 u8 switch2PI = 0;
7020 unsigned char IQKOK = 0;
7021
7022#ifdef CONFIG_RTL_92D_DMDP
7023 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)
7024 return IQK_92D_2G_phy1(priv);
7025#endif
7026
7027 //always do IQK for MP mode
7028#ifdef MP_TEST
7029 if (!priv->pshare->rf_ft_var.mp_specific)
7030#endif
7031 {
7032 if (priv->pmib->dot11RFEntry.macPhyMode == SINGLEMAC_SINGLEPHY) {
7033 if (priv->pshare->iqk_2g_done)
7034 return;
7035 priv->pshare->iqk_2g_done = 1;
7036 }
7037 }
7038
7039 printk(">> %s \n",__FUNCTION__);
7040
7041 // Save ADDA power saving parameters
7042 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7043 ADDA_backup[i] = RTL_R32(ADDA_REG[i]);
7044
7045 /*
7046 * Save MAC default value
7047 */
7048 temp_522 = RTL_R8(0x522);
7049 temp_550 = RTL_R8(0x550);
7050 temp_551 = RTL_R8(0x551);
7051 temp_040 = RTL_R32(0x40);
7052
7053 // Save BB default
7054 temp_a04 = RTL_R32(0xa04);//RTL_R32(0x800);
7055 temp_870 = RTL_R32(0x870);
7056 temp_860 = RTL_R32(0x860);
7057 temp_864 = RTL_R32(0x864);
7058 temp_88c = RTL_R32(0x88c);
7059
7060 // Path-A ADDA all on
7061 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7062 RTL_W32(ADDA_REG[i], 0x04db25a4);
7063
7064 // IQ&LO calibration Setting
7065 //IQK must be done in PI mode
7066 if (!phy_query_bb_reg(priv, 0x820, BIT(8)) || !phy_query_bb_reg(priv, 0x828, BIT(8))) {
7067 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000100);
7068 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000100);
7069 switch2PI++;
7070 }
7071
7072 //MAC register setting
7073 RTL_W8(0x522, 0x3f);
7074 RTL_W8(0x550, RTL_R8(0x550)& (~BIT(3)));
7075 RTL_W8(0x551, RTL_R8(0x551)& (~BIT(3)));
7076 RTL_W32(0x40, 0);
7077
7078 //BB setting
7079 temp_c04 = RTL_R32(0xc04);
7080 temp_c08 = RTL_R32(0xc08);
7081 temp_874 = RTL_R32(0x874);
7082 //phy_set_bb_reg(priv,0x800,BIT(24),0);
7083 phy_set_bb_reg(priv, 0xa04, bMaskDWord, (0x0f000000 | temp_a04) );
7084 RTL_W32(0xc04, 0x03a05600);
7085 RTL_W32(0xc08, 0x000800e4);
7086 RTL_W32(0x874, 0x22204000);
7087
7088 phy_set_bb_reg(priv, 0x870, BIT(10), 1);
7089 phy_set_bb_reg(priv, 0x870, BIT(26), 1);
7090 phy_set_bb_reg(priv, 0x860, BIT(10), 0);
7091 phy_set_bb_reg(priv, 0x864, BIT(10), 0);
7092
7093 phy_set_bb_reg(priv,0x88c,0x00f00000,0xf);
7094 RTL_W32(0x840, 0x00010000);
7095 RTL_W32(0x844, 0x00010000);
7096
7097 //AP or IQK
7098 RTL_W32(0xb68 , 0x0f600000);
7099 RTL_W32(0xb6c , 0x0f600000);
7100
7101 // IQK setting
7102 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
7103 RTL_W32(0xe40, 0x01007c00);
7104 RTL_W32(0xe44, 0x01004800);
7105 // path-A IQK setting
7106 RTL_W32(0xe30, 0x10008c1f);
7107 RTL_W32(0xe34, 0x10008c1f);
7108 RTL_W32(0xe38, 0x82140102);
7109 RTL_W32(0xe3c, 0x28160206);
7110 // path-B IQK setting
7111 RTL_W32(0xe50, 0x10008c22);
7112 RTL_W32(0xe54, 0x10008c22);
7113 RTL_W32(0xe58, 0x82140102);
7114 RTL_W32(0xe5c, 0x28160206);
7115 // LO calibration setting
7116 RTL_W32(0xe4c, 0x00462911);
7117
7118 // delay to ensure Path-A IQK success
7119 delay_ms(10);
7120
7121 // step 4: One shot, path A LOK & IQK
7122 while (cal_num < 3) {
7123 // One shot, path A LOK & IQK
7124 RTL_W32(0xe48, 0xf9000000);
7125 RTL_W32(0xe48, 0xf8000000);
7126 // delay 1ms
7127 delay_ms(1);
7128
7129 // check fail bit and check abnormal condition, then fill BB IQ matrix
7130 cal_e94 = (RTL_R32(0xe94) >> 16) & 0x3ff;
7131 cal_e9c = (RTL_R32(0xe9c) >> 16) & 0x3ff;
7132 cal_ea4 = (RTL_R32(0xea4) >> 16) & 0x3ff;
7133 temp_eac = RTL_R32(0xeac);
7134 cal_eac = (temp_eac >> 16) & 0x3ff;
7135 if (!(temp_eac & BIT(28)) && !(temp_eac & BIT(27)) &&
7136 (cal_e94 != 0x142) && (cal_e9c != 0x42) &&
7137 (cal_ea4 != 0x132) && (cal_eac != 0x36)) {
7138 val_e94[cal_num] = cal_e94;
7139 val_e9c[cal_num] = cal_e9c;
7140 val_ea4[cal_num] = cal_ea4;
7141 val_eac[cal_num] = cal_eac;
7142 cal_num++;
7143 } else {
7144 if (++cal_retry >= 10) {
7145 printk("%s Path-A Check\n",__FUNCTION__);
7146 break;
7147 }
7148 }
7149 }
7150
7151 if (cal_num == 3) {
7152 cal_e94 = get_mean_of_2_close_value(val_e94);
7153 cal_e9c = get_mean_of_2_close_value(val_e9c);
7154 cal_ea4 = get_mean_of_2_close_value(val_ea4);
7155 cal_eac = get_mean_of_2_close_value(val_eac);
7156
7157 priv->pshare->RegE94=cal_e94;
7158 priv->pshare->RegE9C=cal_e9c;
7159
7160 Oldval = (RTL_R32(0xc80) >> 22) & 0x3ff;
7161
7162 X = cal_e94;
7163 phy_set_bb_reg(priv, 0xc80, 0x3ff, (X * Oldval)>>8);
7164 phy_set_bb_reg(priv, 0xc4c, BIT(24), ((X * Oldval) >> 7) & 0x1);
7165
7166 Y = cal_e9c;
7167 if ((Y & 0x00000200) != 0)
7168 Y = Y | 0xFFFFFC00;
7169 phy_set_bb_reg(priv, 0xc94, 0xf0000000, (((Y * Oldval) >> 8) >> 6) & 0xf);
7170 phy_set_bb_reg(priv, 0xc80, 0x003f0000, ((Y * Oldval) >> 8) & 0x3f);
7171 phy_set_bb_reg(priv, 0xc4c, BIT(26), ((Y * Oldval) >> 7) & 0x1);
7172
7173 phy_set_bb_reg(priv, 0xc14, 0x3ff, cal_ea4);
7174
7175 phy_set_bb_reg(priv, 0xc14, 0xfc00, cal_eac & 0x3f);
7176
7177 phy_set_bb_reg(priv, 0xca0, 0xf0000000, (cal_eac >> 6) & 0xf);
7178 }else {
7179 IQKOK++;
7180 priv->pshare->RegE94=0x100;
7181 priv->pshare->RegE9C=0x00;
7182 }
7183
7184 // step 5: Path-A standby mode
7185 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
7186 RTL_W32(0x840, 0x00010000);
7187 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
7188
7189 // step 6: Path-B ADDA all on
7190 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7191 RTL_W32(ADDA_REG[i], 0x0b1b25a4);
7192
7193 // step 7: One shot, path B LOK & IQK
7194 cal_num = 0;
7195 cal_retry = 0;
7196 while (cal_num < 3) {
7197 // One shot, path B LOK & IQK
7198 RTL_W32(0xe60, 2);
7199 RTL_W32(0xe60, 0);
7200 // delay 1ms
7201 delay_ms(1);
7202
7203 // check fail bit and check abnormal condition, then fill BB IQ matrix
7204 cal_eb4 = (RTL_R32(0xeb4) >> 16) & 0x3ff;
7205 cal_ebc = (RTL_R32(0xebc) >> 16) & 0x3ff;
7206 cal_ec4 = (RTL_R32(0xec4) >> 16) & 0x3ff;
7207 cal_ecc = (RTL_R32(0xecc) >> 16) & 0x3ff;
7208 temp_eac = RTL_R32(0xeac);
7209 if (!(temp_eac & BIT(31)) && !(temp_eac & BIT(30)) &&
7210 (cal_eb4 != 0x142) && (cal_ebc != 0x42) &&
7211 (cal_ec4 != 0x132) && (cal_ecc != 0x36)) {
7212 val_eb4[cal_num] = cal_eb4;
7213 val_ebc[cal_num] = cal_ebc;
7214 val_ec4[cal_num] = cal_ec4;
7215 val_ecc[cal_num] = cal_ecc;
7216 cal_num++;
7217 } else {
7218 if (++cal_retry >= 10) {
7219 printk("%s Path-B Check\n",__FUNCTION__);
7220 break;
7221 }
7222 }
7223 }
7224
7225 if (cal_num == 3) {
7226 cal_eb4 = get_mean_of_2_close_value(val_eb4);
7227 cal_ebc = get_mean_of_2_close_value(val_ebc);
7228 cal_ec4 = get_mean_of_2_close_value(val_ec4);
7229 cal_ecc = get_mean_of_2_close_value(val_ecc);
7230
7231 priv->pshare->RegEB4=cal_eb4;
7232 priv->pshare->RegEBC=cal_ebc;
7233
7234 Oldval = (RTL_R32(0xc88) >> 22) & 0x3ff;
7235
7236 X = cal_eb4;
7237 phy_set_bb_reg(priv, 0xc88, 0x3ff, (X * Oldval) >> 8 );
7238 phy_set_bb_reg(priv, 0xc4c, BIT(28), ((X * Oldval) >> 7) & 0x1);
7239
7240 Y = cal_ebc;
7241 if ((Y & 0x00000200) != 0)
7242 Y = Y | 0xFFFFFC00;
7243 phy_set_bb_reg(priv, 0xc9c, 0xf0000000, (((Y * Oldval) >> 8 ) >> 6) & 0xf);
7244 phy_set_bb_reg(priv, 0xc88, 0x003f0000, ((Y * Oldval) >> 8 ) & 0x3f);
7245 phy_set_bb_reg(priv, 0xc4c, BIT(30), ((Y * Oldval) >> 7) & 0x1);
7246
7247 phy_set_bb_reg(priv, 0xc1c, 0x3ff, cal_ec4);
7248
7249 phy_set_bb_reg(priv, 0xc1c, 0xfc00, cal_ecc & 0x3f);
7250
7251 phy_set_bb_reg(priv, 0xc78, 0xf000, (cal_ecc >> 6) & 0xf);
7252 }else {
7253 IQKOK++;
7254 priv->pshare->RegEB4=0x100;
7255 priv->pshare->RegEBC=0x00;
7256 }
7257
7258 // step 8: back to BB mode, load original values
7259 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
7260 RTL_W32(0xc04, temp_c04);
7261 RTL_W32(0xc08, temp_c08);
7262 RTL_W32(0x874, temp_874);
7263 //RTL_W32(0x800, temp_800);
7264 RTL_W32(0xa04, temp_a04);
7265 RTL_W32(0x88c, temp_88c);
7266 RTL_W32(0x840, 0x32fff);
7267 RTL_W32(0x844, 0x32fff);
7268 RTL_W32(0x870, temp_870);
7269 RTL_W32(0x860, temp_860);
7270 RTL_W32(0x864, temp_864);
7271
7272 /*
7273 * Switch back to SI if needed, after IQK
7274 */
7275 if (switch2PI) {
7276 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000000);
7277 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000000);
7278 }
7279
7280 watchdog_kick();
7281
7282 /*
7283 * Reload ADDA power saving parameters
7284 */
7285 for(i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7286 phy_set_bb_reg(priv, ADDA_REG[i], bMaskDWord, ADDA_backup[i]);
7287
7288 /*
7289 * Reload MAC default value
7290 */
7291 RTL_W8(0x550, temp_550);
7292 RTL_W8(0x551, temp_551);
7293 RTL_W32(0x40, temp_040);
7294 RTL_W8(0x522, temp_522);
7295
7296
7297#if 0 //def CLIENT_MODE
7298 clnt_save_IQK_res(priv);
7299#endif
7300 if (!IQKOK)
7301 priv->pshare->IQK_fail_cnt++;
7302}
7303#ifdef CONFIG_RTL_92D_DMDP
7304
7305void IQK_92D_2G_phy1(struct rtl8192cd_priv *priv)
7306{
7307 unsigned int cal_num=0, cal_retry=0, Oldval=0, temp_c04=0, temp_c08=0, temp_874=0, temp_eac;
7308 unsigned int cal_e94, cal_e9c, cal_ea4, cal_eac;
7309 unsigned int X, Y, val_e94[3], val_e9c[3], val_ea4[3], val_eac[3];
7310 unsigned int ADDA_REG[IQK_ADDA_REG_NUM] = {0x85c, 0xe6c, 0xe70, 0xe74, 0xe78, 0xe7c, 0xe80, 0xe84,
7311 0xe88, 0xe8c, 0xed0, 0xed4, 0xed8, 0xedc, 0xee0, 0xeec};
7312 unsigned int ADDA_backup[IQK_ADDA_REG_NUM], i;
7313 u8 temp_522, temp_550, temp_551;
7314 u32 temp_040, temp_870, temp_860, temp_864, temp_a04, temp_88c;
7315 u8 switch2PI = 0;
7316 unsigned char IQKOK = 0;
7317
7318#ifdef MP_TEST
7319 if (!priv->pshare->rf_ft_var.mp_specific)
7320#endif
7321 {
7322 if (priv->pshare->iqk_2g_done)
7323 return;
7324 priv->pshare->iqk_2g_done = 1;
7325 }
7326
7327 printk(">> %s \n",__FUNCTION__);
7328
7329 // Save ADDA power saving parameters
7330 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7331 ADDA_backup[i] = RTL_R32(ADDA_REG[i]);
7332
7333 /*
7334 * Save MAC default value
7335 */
7336 temp_522 = RTL_R8(0x522);
7337 temp_550 = RTL_R8(0x550);
7338 temp_551 = RTL_R8(0x551);
7339 temp_040 = RTL_R32(0x40);
7340
7341 // Save BB default
7342 temp_a04 = RTL_R32(0xa04);//RTL_R32(0x800);
7343 temp_870 = RTL_R32(0x870);
7344 temp_860 = RTL_R32(0x860);
7345 temp_864 = RTL_R32(0x864);
7346 temp_88c = RTL_R32(0x88c);
7347
7348 // Path-A ADDA all on
7349 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++) {
7350#ifdef NON_INTR_ANTDIV
7351 if (DMDP_PHY_QueryBBReg(0, 0xb30,BIT(27)))
7352 RTL_W32(ADDA_REG[i], 0x04db25a4);
7353 else
7354#endif
7355 RTL_W32(ADDA_REG[i], 0x0b1b25a4);
7356 }
7357
7358
7359 // IQ&LO calibration Setting
7360
7361 /*
7362 * IQK must be done in PI mode
7363 */
7364 if (!phy_query_bb_reg(priv, 0x820, BIT(8)) || !phy_query_bb_reg(priv, 0x828, BIT(8))) {
7365 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000100);
7366 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000100);
7367 switch2PI++;
7368 }
7369
7370 //MAC register setting
7371 RTL_W8(0x522, 0x3f);
7372 RTL_W8(0x550, RTL_R8(0x550)& (~BIT(3)));
7373 RTL_W8(0x551, RTL_R8(0x551)& (~BIT(3)));
7374 RTL_W32(0x40, 0);
7375
7376 //BB setting
7377 temp_c04 = RTL_R32(0xc04);
7378 temp_c08 = RTL_R32(0xc08);
7379 temp_874 = RTL_R32(0x874);
7380 //phy_set_bb_reg(priv, 0x800, BIT(24), 0);
7381 phy_set_bb_reg(priv, 0xa04, bMaskDWord, (0x0f000000 | temp_a04) );
7382 RTL_W32(0xc04, 0x03a05600);
7383 RTL_W32(0xc08, 0x000800e4);
7384 RTL_W32(0x874, 0x22204000);
7385
7386 phy_set_bb_reg(priv, 0x870, BIT(10), 1);
7387 phy_set_bb_reg(priv, 0x870, BIT(26), 1);
7388 phy_set_bb_reg(priv, 0x860, BIT(10), 0);
7389 phy_set_bb_reg(priv, 0x864, BIT(10), 0);
7390
7391 phy_set_bb_reg(priv,0x88c,0x00f00000,0xf);
7392
7393 RTL_W32(0x840, 0x00010000);
7394
7395 //AP or IQK
7396 RTL_W32(0xb68 , 0x0f600000);
7397 RTL_W32(0xb6c , 0x0f600000);
7398
7399 // IQK setting
7400 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
7401 RTL_W32(0xe40, 0x01007c00);
7402 RTL_W32(0xe44, 0x01004800);
7403
7404 // path-A IQK setting
7405 RTL_W32(0xe30, 0x10008c22);
7406 RTL_W32(0xe34, 0x10008c22);
7407 RTL_W32(0xe38, 0x82140102);
7408 RTL_W32(0xe3c, 0x28160206);
7409
7410 // LO calibration setting
7411 RTL_W32(0xe4c, 0x00462911);
7412
7413 // delay to ensure Path-A IQK success
7414 delay_ms(10);
7415
7416 // step 4: One shot, path A LOK & IQK
7417 while (cal_num < 3) {
7418 // One shot, path A LOK & IQK
7419 RTL_W32(0xe48, 0xf9000000);
7420 RTL_W32(0xe48, 0xf8000000);
7421 // delay 1ms
7422 delay_ms(1);
7423
7424 // check fail bit and check abnormal condition, then fill BB IQ matrix
7425 cal_e94 = (RTL_R32(0xe94) >> 16) & 0x3ff;
7426 cal_e9c = (RTL_R32(0xe9c) >> 16) & 0x3ff;
7427 cal_ea4 = (RTL_R32(0xea4) >> 16) & 0x3ff;
7428 temp_eac = RTL_R32(0xeac);
7429 cal_eac = (temp_eac >> 16) & 0x3ff;
7430 if (!(temp_eac & BIT(28)) && !(temp_eac & BIT(27)) &&
7431 (cal_e94 != 0x142) && (cal_e9c != 0x42) &&
7432 (cal_ea4 != 0x132) && (cal_eac != 0x36)) {
7433 val_e94[cal_num] = cal_e94;
7434 val_e9c[cal_num] = cal_e9c;
7435 val_ea4[cal_num] = cal_ea4;
7436 val_eac[cal_num] = cal_eac;
7437 cal_num++;
7438 } else {
7439 if (++cal_retry >= 10) {
7440 printk("%s Path-A Check\n",__FUNCTION__);
7441 break;
7442 }
7443 }
7444 }
7445
7446 if (cal_num == 3) {
7447 cal_e94 = get_mean_of_2_close_value(val_e94);
7448 cal_e9c = get_mean_of_2_close_value(val_e9c);
7449 cal_ea4 = get_mean_of_2_close_value(val_ea4);
7450 cal_eac = get_mean_of_2_close_value(val_eac);
7451
7452 priv->pshare->RegE94=cal_e94;
7453 priv->pshare->RegE9C=cal_e9c;
7454
7455 Oldval = (RTL_R32(0xc80) >> 22) & 0x3ff;
7456
7457 X = cal_e94;
7458 phy_set_bb_reg(priv, 0xc80, 0x3ff, (X * Oldval) >> 8);
7459 phy_set_bb_reg(priv, 0xc4c, BIT(24), ((X * Oldval) >> 7) & 0x1);
7460
7461 Y = cal_e9c;
7462 if ((Y & 0x00000200) != 0)
7463 Y = Y | 0xFFFFFC00;
7464 phy_set_bb_reg(priv, 0xc94, 0xf0000000, (((Y * Oldval) >> 8) >> 6) & 0xf);
7465 phy_set_bb_reg(priv, 0xc80, 0x003f0000, ((Y * Oldval) >> 8) & 0x3f);
7466 phy_set_bb_reg(priv, 0xc4c, BIT(26), ((Y * Oldval) >> 7) & 0x1);
7467
7468 phy_set_bb_reg(priv, 0xc14, 0x3ff, cal_ea4);
7469
7470 phy_set_bb_reg(priv, 0xc14, 0xfc00, cal_eac & 0x3f);
7471
7472 phy_set_bb_reg(priv, 0xca0, 0xf0000000, (cal_eac >> 6) & 0xf);
7473 }else {
7474 IQKOK++;
7475 priv->pshare->RegE94=0x100;
7476 priv->pshare->RegE9C=0x00;
7477 }
7478
7479 // back to BB mode, load original values
7480 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
7481 RTL_W32(0xc04, temp_c04);
7482 RTL_W32(0xc08, temp_c08);
7483 RTL_W32(0x874, temp_874);
7484 //RTL_W32(0x800, temp_800);
7485 RTL_W32(0xa04, temp_a04);
7486
7487 RTL_W32(0x88c, temp_88c);
7488 RTL_W32(0x840, 0x32fff);
7489 RTL_W32(0x870, temp_870);
7490 RTL_W32(0x860, temp_860);
7491 RTL_W32(0x864, temp_864);
7492
7493 // return to SI mode
7494 if (switch2PI) {
7495 RTL_W32(0x820, 0x01000000);
7496 RTL_W32(0x828, 0x01000000);
7497 }
7498
7499 watchdog_kick();
7500
7501 /*
7502 * Reload ADDA power saving parameters
7503 */
7504 for(i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7505 phy_set_bb_reg(priv, ADDA_REG[i], bMaskDWord, ADDA_backup[i]);
7506
7507 /*
7508 * Reload MAC default value
7509 */
7510 RTL_W8(0x550, temp_550);
7511 RTL_W8(0x551, temp_551);
7512 RTL_W32(0x40, temp_040);
7513 RTL_W8(0x522, temp_522);
7514
7515 if (!IQKOK)
7516 priv->pshare->IQK_fail_cnt++;
7517}
7518
7519
7520void IQK_92D_5G_phy0_n(struct rtl8192cd_priv *priv)
7521{
7522 unsigned int temp_a04, temp_c04, temp_874, temp_c08, temp_870, temp_860, temp_88c, temp_c50, temp_b30,
7523 switch2PI=0, X, reg; //, oldval_0, TX0_A;
7524 u8 temp_522, temp_550, temp_551;
7525 unsigned int cal_num=0, cal_retry=0, ADDA_backup[IQK_ADDA_REG_NUM];
7526 int Y, result[8][3], result_final[8]={0,0,0,0,0,0,0,0}; //, TX0_C;
7527
7528 unsigned int i, RX0REG0xe40[3], RX0REG0xe40_final=0, REG0xe40, REG0xe94, REG0xe9c, delay_count;
7529 unsigned int REG0xeac, REG0xea4;
7530 unsigned char TX0IQKOK = FALSE;
7531 unsigned int TX_X0, TX_Y0, RX_X0, RX_Y0;
7532 unsigned int ADDA_REG[IQK_ADDA_REG_NUM] = {0x85c, 0xe6c, 0xe70, 0xe74, 0xe78, 0xe7c, 0xe80, 0xe84,
7533 0xe88, 0xe8c, 0xed0, 0xed4, 0xed8, 0xedc, 0xee0, 0xeec};
7534
7535#ifdef MP_TEST
7536 if (!priv->pshare->rf_ft_var.mp_specific)
7537#endif
7538 {
7539 if (priv->pshare->iqk_5g_done)
7540 return;
7541 priv->pshare->iqk_5g_done = 1;
7542 }
7543
7544 printk(">> %s \n",__FUNCTION__);
7545
7546 watchdog_kick();
7547
7548 /*
7549 * Save MAC default value
7550 */
7551 temp_522 = RTL_R8(0x522);
7552 temp_550 = RTL_R8(0x550);
7553 temp_551 = RTL_R8(0x551);
7554
7555 /*
7556 * Save BB Parameter
7557 */
7558 temp_a04 = RTL_R32(0xa04);//RTL_R32(0x800);
7559 temp_c04 = RTL_R32(0xc04);
7560 temp_874 = RTL_R32(0x874);
7561 temp_c08 = RTL_R32(0xc08);
7562 temp_870 = RTL_R32(0x870);
7563 temp_860 = RTL_R32(0x860);
7564 temp_88c = RTL_R32(0x88c);
7565 temp_c50 = RTL_R32(0xc50);
7566 temp_b30 = RTL_R32(0xb30); // 03/03/2011 update
7567
7568 /*
7569 * Save AFE Parameters
7570 */
7571 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7572 ADDA_backup[i] = RTL_R32(ADDA_REG[i]);
7573
7574 /*
7575 * ==============
7576 * Path-A TX/RX IQK
7577 * ==============
7578 */
7579 while (cal_num < 3) {
7580 /*
7581 * Path-A AFE all on
7582 */
7583 for( i = 0 ; i < IQK_ADDA_REG_NUM ; i++) {
7584#ifdef NON_INTR_ANTDIV
7585 if (DMDP_PHY_QueryBBReg(0, 0xb30,BIT(27)))
7586 RTL_W32(ADDA_REG[i], 0x0b1b25a4);
7587 else
7588#endif
7589 RTL_W32(ADDA_REG[i], 0x04db25a4);
7590 }
7591
7592 /*
7593 * MAC register setting
7594 */
7595 RTL_W8(0x522, 0x3f);
7596 RTL_W8(0x550, RTL_R8(0x550)& (~BIT(3)));
7597 RTL_W8(0x551, RTL_R8(0x551)& (~BIT(3)));
7598
7599 /*
7600 * IQK must be done in PI mode
7601 */
7602 if (!phy_query_bb_reg(priv, 0x820, BIT(8)) || !phy_query_bb_reg(priv, 0x828, BIT(8))) {
7603 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000100);
7604 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000100);
7605 switch2PI++;
7606 }
7607
7608 /*
7609 * BB setting
7610 */
7611 //phy_set_bb_reg(priv, 0x800, BIT(24), 0);
7612 phy_set_bb_reg(priv, 0xa04, bMaskDWord, (0x0f000000 | temp_a04) );
7613 phy_set_bb_reg(priv, 0xc04, bMaskDWord, 0x03a05600);
7614 phy_set_bb_reg(priv, 0xc08, bMaskDWord, 0x000800e4);
7615 phy_set_bb_reg(priv, 0x874, bMaskDWord, 0x22208000);
7616 phy_set_bb_reg(priv, 0x88c, BIT(23)|BIT(22)|BIT(21)|BIT(20), 0xf);
7617 phy_set_bb_reg(priv, 0xb30, bMaskDWord, 0x00a00000); // 03/03/2011 update
7618
7619 /*
7620 * AP or IQK
7621 */
7622 //phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x0f600000);
7623 //phy_set_bb_reg(priv, 0xb6c, bMaskDWord, 0x0f600000);
7624
7625 // IQK-R03 2011/02/16 update
7626 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0);
7627 phy_set_bb_reg(priv, 0xb68, bMaskDWord, 0x20000000);
7628
7629 /*
7630 * IQK global setting
7631 */
7632 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x808000);
7633 phy_set_bb_reg(priv, 0xe40, bMaskDWord, 0x10007c00);
7634 phy_set_bb_reg(priv, 0xe44, bMaskDWord, 0x01004800);
7635
7636 /*
7637 * path-A IQK setting
7638 */
7639 phy_set_bb_reg(priv, 0xe30, bMaskDWord, 0x18008c1f);
7640 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x18008c1f);
7641 phy_set_bb_reg(priv, 0xe38, bMaskDWord, 0x82140307); // 01/11/2011 update
7642
7643#ifdef USB_POWER_SUPPORT
7644 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x68160c66);
7645#else
7646 phy_set_bb_reg(priv, 0xe3c, bMaskDWord, 0x68160960); // 01/11/2011 update
7647#endif
7648 /*
7649 * LO calibration setting
7650 */
7651 phy_set_bb_reg(priv, 0xe4c, bMaskDWord, 0x00462911);
7652
7653#ifdef USB_POWER_SUPPORT
7654 // PHY0 TRSW seting
7655 phy_set_bb_reg(priv, 0x870, BIT(6)|BIT(5), 3);
7656 phy_set_bb_reg(priv, 0x860, BIT(6)|BIT(5), 3);
7657#else
7658 /*
7659 * path-A PA on
7660 */
7661 /*
7662 phy_set_bb_reg(priv, 0x870, BIT(11)|BIT(10), 3);
7663 phy_set_bb_reg(priv, 0x870, BIT(6)|BIT(5), 3);
7664 phy_set_bb_reg(priv, 0x860, BIT(11)|BIT(10), 3);
7665 */
7666 phy_set_bb_reg(priv, 0x870, bMaskDWord, 0x07000f60); // 01/11/2011 update
7667 phy_set_bb_reg(priv, 0x860, bMaskDWord, 0x66e60e30); // 01/11/2011 update
7668#endif
7669 /*
7670 * One shot, path A LOK & IQK
7671 */
7672 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf9000000);
7673 phy_set_bb_reg(priv, 0xe48, bMaskDWord, 0xf8000000);
7674
7675 /*
7676 * Delay 10 ms
7677 */
7678 delay_ms(10);
7679
7680 delay_count = 0;
7681 while (1){
7682 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
7683 if ((REG0xeac&BIT(26))||(delay_count>20)){
7684 break;
7685 }else {
7686 delay_ms(1);
7687 delay_count++;
7688 }
7689 }
7690
7691 /*
7692 * Check_TX_IQK_A_result
7693 */
7694 REG0xe40 = phy_query_bb_reg(priv, 0xe40, bMaskDWord);
7695 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
7696 REG0xe94 = phy_query_bb_reg(priv, 0xe94, bMaskDWord);
7697 if(((REG0xeac&BIT(28)) == 0) && (((REG0xe94&0x3FF0000)>>16)!=0x142)) {
7698 TX0IQKOK = TRUE;
7699 REG0xe9c = phy_query_bb_reg(priv, 0xe9c, bMaskDWord);
7700 TX_X0 = (phy_query_bb_reg(priv, 0xe94, bMaskDWord)&0x3FF0000)>>16;
7701 TX_Y0 = (phy_query_bb_reg(priv, 0xe9c, bMaskDWord)&0x3FF0000)>>16;
7702 RX0REG0xe40[cal_num] = (REG0xe40 & 0xfc00fc00) | (TX_X0<<16) | TX_Y0;
7703 DEBUG_INFO("TX_X0 %08x TX_Y0 %08x RX0REG0xe40 %08x\n", TX_X0, TX_Y0, RX0REG0xe40[cal_num]);
7704 result[0][cal_num] = TX_X0;
7705 result[1][cal_num] = TX_Y0;
7706 } else {
7707 TX0IQKOK = FALSE;
7708 if (++cal_retry >= 10) {
7709 printk("%s Path-A Tx/Rx Check\n",__FUNCTION__);
7710 break;
7711 }
7712 }
7713
7714 /*
7715 * Check_RX_IQK_A_result
7716 */
7717 if(TX0IQKOK == TRUE) {
7718 REG0xeac = phy_query_bb_reg(priv, 0xeac, bMaskDWord);
7719 REG0xea4 = phy_query_bb_reg(priv, 0xea4, bMaskDWord);
7720 if(((REG0xeac&BIT(27)) == 0) && (((REG0xea4&0x3FF0000)>>16)!=0x132)) {
7721 RX_X0 = (phy_query_bb_reg(priv, 0xea4, bMaskDWord)&0x3FF0000)>>16;
7722 RX_Y0 = (phy_query_bb_reg(priv, 0xeac, bMaskDWord)&0x3FF0000)>>16;
7723 DEBUG_INFO("RX_X0 %08x RX_Y0 %08x\n", RX_X0, RX_Y0);
7724 result[2][cal_num] = RX_X0;
7725 result[3][cal_num] = RX_Y0;
7726 cal_num++;
7727 } else {
7728 phy_set_bb_reg(priv, 0xc14, bMaskDWord, 0x40000100);
7729 phy_set_bb_reg(priv, 0xe34, bMaskDWord, 0x19008c00);
7730 if (++cal_retry >= 10) {
7731 printk("%s Path-A Tx/Rx Check\n",__FUNCTION__);
7732 break;
7733 }
7734 }
7735 }
7736 }
7737
7738 if (cal_num == 3) {
7739 result_final[0] = get_mean_of_2_close_value(result[0]);
7740 result_final[1] = get_mean_of_2_close_value(result[1]);
7741 result_final[2] = get_mean_of_2_close_value(result[2]);
7742 result_final[3] = get_mean_of_2_close_value(result[3]);
7743 RX0REG0xe40_final = 0x80000000 | get_mean_of_2_close_value(RX0REG0xe40);
7744
7745 priv->pshare->RegE94=result_final[0];
7746 priv->pshare->RegE9C=result_final[1];
7747 } else {
7748 priv->pshare->RegE94=0x100;
7749 priv->pshare->RegE9C=0x00;
7750 }
7751
7752 /*
7753 * Fill IQK result for Path A
7754 */
7755 if (result_final[0]) {
7756 /*
7757 oldval_0 = (phy_query_bb_reg(priv, 0xc80, bMaskDWord) >> 22) & 0x3FF;
7758 X = result_final[0];
7759 if ((X & 0x00000200) != 0)
7760 X = X | 0xFFFFFC00;
7761 TX0_A = (X * oldval_0) >> 8;
7762 phy_set_bb_reg(priv, 0xc80, 0x3FF, TX0_A);
7763 phy_set_bb_reg(priv, 0xc4c, BIT(24), ((X* oldval_0>>7) & 0x1));
7764
7765 Y = result_final[1];
7766 if ((Y & 0x00000200) != 0)
7767 Y = Y | 0xFFFFFC00;
7768 TX0_C = (Y * oldval_0) >> 8;
7769 phy_set_bb_reg(priv, 0xc94, 0xF0000000, ((TX0_C&0x3C0)>>6));
7770 phy_set_bb_reg(priv, 0xc80, 0x003F0000, (TX0_C&0x3F));
7771 phy_set_bb_reg(priv, 0xc4c, BIT(26), ((Y* oldval_0>>7) & 0x1));
7772 */
7773
7774 // IQK-R03 2011/02/16 update
7775 X = result_final[0];
7776 Y = result_final[1];
7777 //printk("X=%x Y=%x\n",X,Y);
7778 //Path-A OFDM_A
7779 phy_set_bb_reg(priv, 0xe30, 0x03FF0000, X);
7780 phy_set_bb_reg(priv, 0xc4c, BIT(24), 0);
7781 //Path-A OFDM_C
7782 phy_set_bb_reg(priv, 0xe30, 0x000003FF, Y);
7783 phy_set_bb_reg(priv, 0xc4c, BIT(26), 0);
7784
7785
7786 if(result_final[2]) {
7787 reg = result_final[2];
7788 phy_set_bb_reg(priv, 0xc14, 0x3FF, reg);
7789
7790 reg = result_final[3] & 0x3F;
7791 phy_set_bb_reg(priv, 0xc14, 0xFC00, reg);
7792
7793 reg = (result_final[3] >> 6) & 0xF;
7794 phy_set_bb_reg(priv, 0xca0, 0xF0000000, reg);
7795
7796 phy_set_bb_reg(priv, 0xe34, 0x03FF0000, result_final[2]); // X
7797 phy_set_bb_reg(priv, 0xe34, 0x3FF, result_final[3]); //Y
7798 }
7799 }
7800
7801 /*
7802 * Path-A PA off
7803 */
7804 phy_set_bb_reg(priv, 0x870, bMaskDWord, temp_870);
7805 phy_set_bb_reg(priv, 0x860, bMaskDWord, temp_860);
7806
7807 /*
7808 * Exit IQK mode
7809 */
7810 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
7811 phy_set_bb_reg(priv, 0xc04, bMaskDWord, temp_c04);
7812 phy_set_bb_reg(priv, 0xc08, bMaskDWord, temp_c08);
7813 phy_set_bb_reg(priv, 0x874, bMaskDWord, temp_874);
7814 //phy_set_bb_reg(priv, 0x800, bMaskDWord, temp_800);
7815 phy_set_bb_reg(priv, 0xa04, bMaskDWord, temp_a04);
7816 phy_set_bb_reg(priv, 0x88c, bMaskDWord, temp_88c);
7817 phy_set_bb_reg(priv, 0xb30, bMaskDWord, temp_b30); // 03/03/2011 update
7818 //phy_set_bb_reg(priv, 0x840, bMaskDWord, 0x00032fff); // 01/11/2011 update
7819
7820 //PHY0 IQ path to DP block
7821 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x010170b8);
7822
7823 phy_set_bb_reg(priv, 0xc50, bMaskDWord, 0x50);
7824 phy_set_bb_reg(priv, 0xc50, bMaskDWord, temp_c50);
7825
7826 /*
7827 * Reload MAC default value
7828 */
7829 RTL_W8(0x550, temp_550);
7830 RTL_W8(0x551, temp_551);
7831 RTL_W8(0x522, temp_522);
7832
7833 /*
7834 * Switch back to SI if needed, after IQK
7835 */
7836 if (switch2PI) {
7837 phy_set_bb_reg(priv, 0x820, bMaskDWord, 0x01000000);
7838 phy_set_bb_reg(priv, 0x828, bMaskDWord, 0x01000000);
7839 }
7840
7841 /*
7842 * Reload ADDA power saving parameters
7843 */
7844 for(i = 0 ; i < IQK_ADDA_REG_NUM ; i++)
7845 phy_set_bb_reg(priv, ADDA_REG[i], bMaskDWord, ADDA_backup[i]);
7846
7847 if (!TX0IQKOK)
7848 priv->pshare->IQK_fail_cnt++;
7849}
7850#endif
7851
7852#ifdef SW_LCK_92D
7853
7854#define TARGET_CHNL_NUM_5G 221
7855#define TARGET_CHNL_NUM_2G 14
7856#define CV_CURVE_CNT 64
7857
7858unsigned int CurveIndex_5G[TARGET_CHNL_NUM_5G]={0};
7859unsigned int CurveIndex_2G[TARGET_CHNL_NUM_2G]={0};
7860
7861static unsigned int TargetChnl_5G[TARGET_CHNL_NUM_5G] = {
786225141, 25116, 25091, 25066, 25041,
786325016, 24991, 24966, 24941, 24917,
786424892, 24867, 24843, 24818, 24794,
786524770, 24765, 24721, 24697, 24672,
786624648, 24624, 24600, 24576, 24552,
786724528, 24504, 24480, 24457, 24433,
786824409, 24385, 24362, 24338, 24315,
786924291, 24268, 24245, 24221, 24198,
787024175, 24151, 24128, 24105, 24082,
787124059, 24036, 24013, 23990, 23967,
787223945, 23922, 23899, 23876, 23854,
787323831, 23809, 23786, 23764, 23741,
787423719, 23697, 23674, 23652, 23630,
787523608, 23586, 23564, 23541, 23519,
787623498, 23476, 23454, 23432, 23410,
787723388, 23367, 23345, 23323, 23302,
787823280, 23259, 23237, 23216, 23194,
787923173, 23152, 23130, 23109, 23088,
788023067, 23046, 23025, 23003, 22982,
788122962, 22941, 22920, 22899, 22878,
788222857, 22837, 22816, 22795, 22775,
788322754, 22733, 22713, 22692, 22672,
788422652, 22631, 22611, 22591, 22570,
788522550, 22530, 22510, 22490, 22469,
788622449, 22429, 22409, 22390, 22370,
788722350, 22336, 22310, 22290, 22271,
788822251, 22231, 22212, 22192, 22173,
788922153, 22134, 22114, 22095, 22075,
789022056, 22037, 22017, 21998, 21979,
789121960, 21941, 21921, 21902, 21883,
789221864, 21845, 21826, 21807, 21789,
789321770, 21751, 21732, 21713, 21695,
789421676, 21657, 21639, 21620, 21602,
789521583, 21565, 21546, 21528, 21509,
789621491, 21473, 21454, 21436, 21418,
789721400, 21381, 21363, 21345, 21327,
789821309, 21291, 21273, 21255, 21237,
789921219, 21201, 21183, 21166, 21148,
790021130, 21112, 21095, 21077, 21059,
790121042, 21024, 21007, 20989, 20972,
790225679, 25653, 25627, 25601, 25575,
790325549, 25523, 25497, 25471, 25446,
790425420, 25394, 25369, 25343, 25318,
790525292, 25267, 25242, 25216, 25191,
790625166 };
7907
7908static unsigned int TargetChnl_2G[TARGET_CHNL_NUM_2G] = { // channel 1~14
790926084, 26030, 25976, 25923, 25869, 25816, 25764,
791025711, 25658, 25606, 25554, 25502, 25451, 25328
7911};
7912
7913void _PHY_CalcCurvIndex(struct rtl8192cd_priv *priv, unsigned int *TargetChnl,
7914 unsigned int *CurveCountVal, char is5G, unsigned int *CurveIndex)
7915{
7916 unsigned int smallestABSVal = 0xffffffff, u4tmp;
7917 unsigned char i, j;
7918 unsigned char chnl_num = is5G?(TARGET_CHNL_NUM_5G) : (TARGET_CHNL_NUM_2G);
7919
7920
7921 for(i=0; i<chnl_num; i++)
7922 {
7923 //if(is5G && !IsLegal5GChannel(pAdapter, i+1))
7924 //continue;
7925
7926 CurveIndex[i] = 0;
7927 for(j=0; j<(CV_CURVE_CNT*2); j++)
7928 {
7929 u4tmp = RTL_ABS(TargetChnl[i], CurveCountVal[j]);
7930 //if (i==115)
7931 //printk("cv[%d]=%x\n", j, u4tmp);
7932 if(u4tmp < smallestABSVal)
7933 {
7934 CurveIndex[i] = j;
7935 smallestABSVal = u4tmp;
7936 }
7937 }
7938
7939 smallestABSVal = 0xffffffff;
7940 }
7941}
7942
7943void phy_ReloadLCKSetting(struct rtl8192cd_priv *priv)
7944{
7945 unsigned int eRFPath = priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G? RF_PATH_A:(priv->pmib->dot11RFEntry.macPhyMode==SINGLEMAC_SINGLEPHY?RF_PATH_B:RF_PATH_A);
7946 unsigned int u4tmp = 0;
7947// unsigned char bNeedPowerDownRadio = FALSE;
7948 unsigned int channel = priv->pshare->RegRF18[eRFPath]&0xff;
7949 //unsigned int channel = phy_query_rf_reg(priv, eRFPath, rRfChannel, 0xff, 1);
7950
7951 DEBUG_INFO("====>phy_ReloadLCKSetting interface %d path %d ch %d [0x%05x]\n", priv->pshare->wlandev_idx, eRFPath, channel, priv->pshare->RegRF28[eRFPath]);
7952
7953 //only for 92D C-cut SMSP
7954 if(GET_CHIP_VER(priv)!=VERSION_8192D
7955#ifdef CONFIG_RTL_92C_SUPPORT
7956 || IS_TEST_CHIP(priv)
7957#endif
7958 )
7959 return;
7960
7961 if(priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_5G)
7962 {
7963 //Path-A for 5G
7964 u4tmp = CurveIndex_5G[channel-1];
7965 //printk("%s ver 1 set RF-A, 5G, 0x28 = 0x%x !!\n",__FUNCTION__, u4tmp);
7966#ifdef CONFIG_RTL_92D_DMDP
7967 if(priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY && priv->pshare->wlandev_idx == 1)
7968 {
7969 priv->pshare->RegRF28[RF_PATH_A] = RTL_SET_MASK(priv->pshare->RegRF28[RF_PATH_A],0x3f800,u4tmp,11); //DMDP_PHY_SetRFReg(0, RF_PATH_A, 0x28, 0x3f800, u4tmp);
7970 DMDP_PHY_SetRFReg(0, RF_PATH_A, 0x28, bMask20Bits, priv->pshare->RegRF28[RF_PATH_A]);
7971 }else
7972#endif
7973 {
7974 priv->pshare->RegRF28[eRFPath] = RTL_SET_MASK(priv->pshare->RegRF28[eRFPath],0x3f800,u4tmp,11); //phy_set_rf_reg(priv, eRFPath, 0x28, 0x3f800, u4tmp);
7975 phy_set_rf_reg(priv, eRFPath, 0x28, bMask20Bits, priv->pshare->RegRF28[eRFPath]);
7976 }
7977 DEBUG_INFO("%s ver 3 set RF-B, 2G, 0x28 = 0x%05x [0x%05x]!!\n", __FUNCTION__, phy_query_rf_reg(priv, eRFPath, 0x28, bMask20Bits, 1), priv->pshare->RegRF28[eRFPath]);
7978 }
7979 else if(priv->pmib->dot11RFEntry.phyBandSelect == PHY_BAND_2G)
7980 {
7981 u4tmp = CurveIndex_2G[channel-1];
7982 //printk("%s ver 3 set RF-B, 2G, 0x28 = 0x%x !!\n", __FUNCTION__, u4tmp);
7983#ifdef CONFIG_RTL_92D_DMDP
7984 if(priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY && priv->pshare->wlandev_idx == 0)
7985 {
7986 priv->pshare->RegRF28[RF_PATH_A] = RTL_SET_MASK(priv->pshare->RegRF28[RF_PATH_A],0x3f800,u4tmp,11); //DMDP_PHY_SetRFReg(1, RF_PATH_A, 0x28, 0x3f800, u4tmp);
7987 DMDP_PHY_SetRFReg(1, RF_PATH_A, 0x28, bMask20Bits, priv->pshare->RegRF28[RF_PATH_A]);
7988 }else
7989#endif
7990 {
7991 priv->pshare->RegRF28[eRFPath] = RTL_SET_MASK(priv->pshare->RegRF28[eRFPath],0x3f800,u4tmp,11); // phy_set_rf_reg(priv, eRFPath, 0x28, 0x3f800, u4tmp);
7992 phy_set_rf_reg(priv, eRFPath, 0x28, bMask20Bits, priv->pshare->RegRF28[eRFPath]);
7993 }
7994 DEBUG_INFO("%s ver 3 set RF-B, 2G, 0x28 = 0x%05x [0x%05x]!!\n", __FUNCTION__, phy_query_rf_reg(priv, eRFPath, 0x28, bMask20Bits, 1), priv->pshare->RegRF28[eRFPath]);
7995 }
7996
7997}
7998
7999/* Software LCK */
8000void PHY_LCCalibrate_92D(struct rtl8192cd_priv *priv)
8001{
8002 unsigned char tmpReg;
8003 unsigned int RF_mode[2];
8004 unsigned int eRFPath, curMaxRFPath;
8005 unsigned int i;
8006 unsigned int curveCountVal[CV_CURVE_CNT*2]={0};
8007 unsigned short timeout = 800, timecount = 0;
8008
8009 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)
8010 curMaxRFPath = RF_PATH_B;
8011 else
8012 curMaxRFPath = RF_PATH_MAX;
8013
8014 //Check continuous TX and Packet TX
8015 tmpReg = RTL_R8(0xd03);
8016
8017 if ((tmpReg & 0x70) != 0) // Deal with contisuous TX case
8018 RTL_W8(0xd03, tmpReg&0x8F); // disable all continuous TX
8019 else // Deal with Packet TX case
8020 RTL_W8(TXPAUSE, 0xFF); // block all queues
8021
8022 phy_set_bb_reg(priv, REG_FPGA0_ANALOG_PARAMETER4, 0xF00000, 0x0F);
8023
8024 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++) {
8025 // 1. Read original RF mode
8026 RF_mode[eRFPath] = phy_query_rf_reg(priv, eRFPath, 0x00, bMask20Bits, 1);
8027
8028 // 2. Set RF mode = standby mode
8029 phy_set_rf_reg(priv, eRFPath, 0x00, 0x70000, 0x01);
8030
8031 // switch CV-curve control by LC-calibration
8032 phy_set_rf_reg(priv, eRFPath, 0x2B, BIT(17), 0x0);
8033
8034 // jenyu suggest
8035 phy_set_rf_reg(priv, eRFPath, 0x28, BIT(8), 0x1);
8036
8037 //4. Set LC calibration begin
8038 phy_set_rf_reg(priv, eRFPath, 0x18, BIT(15), 0x01);
8039
8040 while(!(phy_query_rf_reg(priv, eRFPath, 0x2A, BIT(11), 1) &&
8041 timecount <= timeout)){
8042 //DEBUG_INFO("PHY_LCK delay for %d ms=2\n", timecount);
8043 delay_ms(50);
8044 timecount+=50;
8045 }
8046
8047 //u4tmp = phy_query_rf_reg(priv, eRFPath, 0x28, bMask20Bits, 1);
8048
8049 memset((void *)curveCountVal, 0, CV_CURVE_CNT*2);
8050
8051 //Set LC calibration off
8052 phy_set_rf_reg(priv, eRFPath, 0x18, BIT(15), 0x00);
8053
8054 // jenyu suggest
8055 phy_set_rf_reg(priv, eRFPath, 0x28, BIT(8), 0x0);
8056
8057 //save Curve-counting number
8058 for(i=0; i<CV_CURVE_CNT; i++)
8059 {
8060 unsigned int readVal=0, readVal2=0;
8061
8062 phy_set_rf_reg(priv, eRFPath, 0x3F, 0x7f, i);
8063
8064 phy_set_rf_reg(priv, eRFPath, 0x4D, bMask20Bits, 0);
8065
8066 readVal = phy_query_rf_reg(priv, eRFPath, 0x4F, bMask20Bits, 1);
8067
8068 curveCountVal[2*i+1] = (readVal & 0xfffe0) >> 5;
8069
8070 readVal2 = phy_query_rf_reg(priv, eRFPath, 0x50, 0xffc00, 1);
8071
8072 curveCountVal[2*i] = (((readVal & 0x1F) << 10) | readVal2);
8073 }
8074
8075 if(eRFPath == RF_PATH_A
8076#ifdef CONFIG_RTL_92D_DMDP
8077 && priv->pshare->wlandev_idx == 0
8078#endif
8079 )
8080 _PHY_CalcCurvIndex(priv, TargetChnl_5G, curveCountVal, TRUE, CurveIndex_5G);
8081 else
8082 _PHY_CalcCurvIndex(priv, TargetChnl_2G, curveCountVal, FALSE, CurveIndex_2G);
8083
8084 // switch CV-curve control mode
8085 phy_set_rf_reg(priv, eRFPath, 0x2B, BIT(17), 0x1);
8086
8087 // store 0x28 for Reload_LCK
8088 priv->pshare->RegRF28[eRFPath] = phy_query_rf_reg(priv, eRFPath, 0x28, bMask20Bits, 1);
8089 }
8090
8091 //Restore original situation
8092 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++)
8093 {
8094 phy_set_rf_reg(priv, eRFPath, 0x00, bMask20Bits, RF_mode[eRFPath]);
8095 }
8096
8097 if((tmpReg&0x70) != 0)
8098 {
8099 //Path-A
8100 RTL_W8(0xd03, tmpReg);
8101 }
8102 else // Deal with Packet TX case
8103 {
8104 RTL_W8(TXPAUSE, 0x00);
8105 }
8106
8107 phy_set_bb_reg(priv, REG_FPGA0_ANALOG_PARAMETER4, 0xF00000, 0x00);
8108
8109 phy_ReloadLCKSetting(priv);
8110
8111}
8112
8113#else
8114/* Hardware LCK */
8115static void PHY_LCCalibrate_92D(struct rtl8192cd_priv *priv)
8116{
8117 unsigned char tmpReg;
8118 unsigned int RF_mode[2], tmpu4Byte[2];
8119
8120 unsigned int eRFPath, curMaxRFPath;
8121 unsigned char timeout = 800, timecount = 0;
8122
8123 if (priv->pmib->dot11RFEntry.macPhyMode == DUALMAC_DUALPHY)
8124 curMaxRFPath = RF_PATH_B;
8125 else
8126 curMaxRFPath = RF_PATH_MAX;
8127
8128
8129 // Check continuous TX and Packet TX
8130 tmpReg = RTL_R8(0xd03);
8131
8132 if ((tmpReg & 0x70) != 0) // Deal with contisuous TX case
8133 RTL_W8(0xd03, tmpReg&0x8F); // disable all continuous TX
8134 else // Deal with Packet TX case
8135 RTL_W8(TXPAUSE, 0xFF); // block all queues
8136
8137 phy_set_bb_reg(priv, REG_FPGA0_ANALOG_PARAMETER4, 0xF00000, 0x0F);
8138
8139 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++) {
8140 // 1. Read original RF mode
8141 RF_mode[eRFPath] = phy_query_rf_reg(priv, eRFPath, 0x00, bMask20Bits, 1);
8142
8143 // 2. Set RF mode = standby mode
8144 phy_set_rf_reg(priv, eRFPath, 0x00, 0x70000, 0x01);
8145
8146 tmpu4Byte[eRFPath] = phy_query_rf_reg(priv, eRFPath, 0x28, bMask20Bits, 1);
8147 phy_set_rf_reg(priv, eRFPath, 0x28, 0x700, 0x07);
8148
8149 //4. Set LC calibration begin
8150 phy_set_rf_reg(priv, eRFPath, 0x18, 0x08000, 0x01);
8151 }
8152
8153 watchdog_kick();
8154
8155 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++) {
8156 while(!(phy_query_rf_reg(priv, eRFPath, 0x2A, BIT(11), 1) &&
8157 timecount <= timeout)){
8158 DEBUG_INFO("PHY_LCK delay for %d ms=2\n", timecount);
8159 delay_ms(50);
8160 timecount+=50;
8161 }
8162 }
8163
8164 for(eRFPath = RF_PATH_A; eRFPath < curMaxRFPath; eRFPath++) {
8165 phy_set_rf_reg(priv, eRFPath, 0x28, bMask20Bits, tmpu4Byte[eRFPath]);
8166 priv->pshare->RegRF28[eRFPath] = tmpu4Byte[eRFPath];
8167 phy_set_rf_reg(priv, eRFPath, 0x00, bMask20Bits, RF_mode[eRFPath]);
8168 }
8169
8170 // Restore original situation
8171 if ((tmpReg & 0x70) != 0) // Deal with contisuous TX case
8172 RTL_W8(0xd03, tmpReg);
8173 else // Deal with Packet TX case
8174 RTL_W8(TXPAUSE, 0x00);
8175
8176 phy_set_bb_reg(priv, REG_FPGA0_ANALOG_PARAMETER4, 0xF00000, 0x0);
8177
8178}
8179
8180
8181#endif //LCK_SW
8182
8183#ifdef DPK_92D
8184
8185#if 1 //copy from driver of station team
8186#define RF_AC 0x00
8187
8188#define REG_PDP_ANT_A 0xb00
8189#define rBndA 0xb30
8190#define REG_PDP_ANT_B 0xb70
8191#define rBndB 0xba0
8192
8193#define RF_MODE1 0x10
8194#define RF_MODE2 0x11
8195
8196#define rTxAGC_A_CCK11_2_B_CCK11 0x86c
8197
8198#define RF_TX_G3 0x22
8199
8200#define RF_TXPA_G1 0x31 // RF TX PA control
8201#define RF_TXPA_G2 0x32 // RF TX PA control
8202#define RF_TXPA_G3 0x33 // RF TX PA control
8203#define RF_LOBF_9 0x38
8204#define RF_RXRF_A3 0x3C
8205#define RF_TRSW 0x3F
8206
8207#define RF_TXPA_G1 0x31 // RF TX PA control
8208#define RF_TXPA_G2 0x32 // RF TX PA control
8209#define RF_TXPA_G3 0x33 // RF TX PA control
8210#define RF_LOBF_9 0x38
8211#define RF_RXRF_A3 0x3C
8212#define RF_TRSW 0x3F
8213
8214#define RF_TXRF_A2 0x41
8215#define RF_TXPA_G4 0x46
8216#define RF_TXPA_A4 0x4B
8217
8218#define RF_IQADJ_G1 0x01
8219#define RF_IQADJ_G2 0x02
8220#define RF_BS_PA_APSET_G1_G4 0x03
8221#define RF_BS_PA_APSET_G5_G8 0x04
8222#define RF_POW_TRSW 0x05
8223
8224#define DP_OFFSET_NUM 9
8225#define DP_AP_CUREVE_SELECT_NUM 3
8226#define DP_gain_loss 1
8227#define DP_PA_BIAS_NUM 4
8228
8229#define rTxAGC_B_CCK5_1_Mcs32 0x838
8230
8231#define RF_TXBIAS 0x16
8232#endif
8233
8234#define DPK_DEBUG(fmt,args...)
8235
8236#define DP_BB_REG_NUM 7
8237//#define DP_BB_REG_NUM_A 11
8238//#define DP_BB_REG_NUM_B 10
8239#define DP_BB_REG_NUM_A 10
8240#define DP_BB_REG_NUM_B 9
8241
8242#define DP_BB_REG_NUM_settings 6
8243#define DP_BB_REG_NUM_loop 30
8244#define DP_BB_REG_NUM_loop_tx 12
8245#define DP_BB_REG_NUM_loop_rx 8
8246#define DP_BB_REG_NUM_loop_pa 4
8247#define DP_RF_REG_NUM 4
8248#define DP_SRAM_NUM 16
8249//#define DP_SRAM_NUM_db 22
8250#define DP_SRAM_NUM_db 86
8251
8252#define DP_PATH_NUM 2
8253#define DP_PA_MODEL_NUM 32
8254#define DP_PA_MODEL_RUN_NUM 8
8255#define DP_PA_MODEL_PER_RUN_NUM 4
8256#define DP_RETRY_LIMIT 10
8257#define DP_DPK_NUM 3
8258#define DP_DPK_VALUE_NUM 2
8259#if 1
8260#define DP_GAIN_LOSS_BOUND_NUM 14
8261#else
8262#define DP_GAIN_LOSS_BOUND_NUM 8
8263#endif
8264#define DP_OFFSET_NUM 9
8265//#define DP_AP_CUREVE_SELECT_NUM 2 // 3
8266#define DP_gain_loss 1
8267
8268
8269static __always_inline void rtl8192cd_DPK_timer(unsigned long task_priv)
8270{
8271 struct rtl8192cd_priv *priv = (struct rtl8192cd_priv *)task_priv;
8272
8273 if (!(priv->drv_state & DRV_STATE_OPEN))
8274 return;
8275
8276 if (priv->pshare->pwr_trk_ongoing){
8277 DPK_DEBUG("==>_PHY_DigitalPredistortion() TxPowerTrackingInProgress() delay 100ms\n");
8278 rtk_mod_timer(&priv->pshare->DPKTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(100));
8279 }else{
8280 PHY_DPCalibrate(priv);
8281 }
8282}
8283DEFINE_TIMER_CALLBACK(rtl8192cd_DPK_timer);
8284
8285void init_DPKTimer(struct rtl8192cd_priv *priv)
8286{
8287 rtk_timer_setup(&(priv->pshare->DPKTimer), rtl8192cd_DPK_timer, (unsigned long)priv, 0);
8288}
8289
8290void _PHY_DPK_polling(struct rtl8192cd_priv *priv)
8291{
8292 unsigned int delaycount = 0, delaybound = 30, delay = 800;
8293 unsigned int u4tmp;
8294
8295 delaycount = 0;
8296
8297 do{
8298 delay_us(delay);
8299
8300 u4tmp = phy_query_bb_reg(priv, 0xdf4, bMaskDWord);
8301 //RTPRINT(FINIT, INIT_IQK, ("0xdf4 = 0x%x, delay %d us\n", u4tmp, delaycount*delay+800));
8302 delaycount++;
8303 delay = 100;
8304 u4tmp = (u4tmp & BIT(26)) >> 26;
8305 }while(u4tmp == 0x01 && delaycount < delaybound);
8306
8307}
8308
8309// if AP curve check fail return FALSE
8310int _PHY_DPK_AP_curve_check(struct rtl8192cd_priv *priv, unsigned int *PA_power, unsigned int RegiesterNum)
8311{
8312 unsigned int PA_power_temp[DP_PA_MODEL_NUM], i = 0, index = 5,
8313 base = 532, ref1, ref2;
8314 int power_I, power_Q;
8315
8316 //store I, Q
8317
8318 for(i = 0; i < DP_PA_MODEL_NUM; i++){
8319 power_I = (PA_power[i] >> 8);
8320 if(power_I & BIT(7))
8321 power_I |= bMaskH3Bytes;
8322
8323 power_Q = PA_power[i] & bMaskByte0;
8324 if(power_Q & BIT(7))
8325 power_Q |= bMaskH3Bytes;
8326
8327 PA_power_temp[i] = power_I*power_I+power_Q*power_Q;
8328 }
8329
8330 ref1 = PA_power_temp[0];
8331 for(i = 0; i < index; i++)
8332 ref1 = (ref1 > PA_power_temp[i])?ref1:PA_power_temp[i];
8333
8334 ref2 = PA_power_temp[index];
8335 for(i = index; i < index*2; i++)
8336 ref2 = (ref2 > PA_power_temp[i])?ref2:PA_power_temp[i];
8337
8338 DPK_DEBUG("==>_PHY_DPK_AP_cureve_check ref1 = 0x%x ref2 = 0x%x\n", ref1, ref2);
8339
8340 if(ref1 == 0)
8341 return FALSE;
8342
8343 return ((ref2 << 9)/ref1) < base;
8344}
8345
8346// if DPK fail return FALSE
8347int _PHY_DPK_check(struct rtl8192cd_priv *priv, unsigned int *PA_power, unsigned int RegiesterNum)
8348{
8349 unsigned int base = 407, PA_power_temp[2], i = 0;
8350 int power_I, power_Q;
8351
8352 while(i ==0 || i == (RegiesterNum-1))
8353 {
8354 power_I = (PA_power[i] >> 8);
8355 if(power_I & BIT(7))
8356 power_I |= bMaskH3Bytes;
8357
8358 power_Q = PA_power[i] & bMaskByte0;
8359 if(power_Q & BIT(7))
8360 power_Q |= bMaskH3Bytes;
8361
8362 PA_power_temp[i==0?0:1] = power_I*power_I+ power_Q*power_Q;
8363
8364 DPK_DEBUG("==>_PHY_DPK_check pa_power_temp[%d] 0x%x\n", i, PA_power_temp[i==0?0:1]);
8365
8366 if(i == 0)
8367 i = RegiesterNum -1;
8368 else if (i == RegiesterNum -1)
8369 break;
8370 }
8371
8372 //normalization
8373 if(PA_power_temp[0] == 0)
8374 return TRUE;
8375 else
8376 return (((PA_power_temp[1] << 9) /PA_power_temp[0]) <= base);
8377
8378}
8379
8380int _PHY_Find_Tx_Power_Index(struct rtl8192cd_priv *priv, unsigned int *PA_power, unsigned char path, unsigned char bPlus3db, char bDecreaseTxIndex, unsigned char *tx_index_out)
8381{
8382 unsigned char i, tx_index = bDecreaseTxIndex?0x0f:0x15;
8383 unsigned int tmpReg[11], tmpBase, RegNum = 11, base = /*323*/256;
8384 unsigned int PA_power_normal[11];
8385// unsigned int check_base =bPlus3db?(400-base):(323-base);
8386 unsigned int check_base =bPlus3db?400:323;
8387
8388 int power_I, power_Q;
8389 int index = -1;
8390
8391 DPK_DEBUG("==>tx_index minus %d bplus3db %d\n", base, bPlus3db);
8392
8393 _PHY_DPK_polling(priv);
8394
8395 if(path == RF_PATH_A)
8396 {
8397 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x01017018);
8398 tmpReg[0] = phy_query_bb_reg(priv, 0xbdc, bMaskDWord);
8399 tmpReg[1] = phy_query_bb_reg(priv, 0xbe8, bMaskDWord);
8400
8401 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x01017019);
8402 tmpReg[2] = phy_query_bb_reg(priv, 0xbdc, bMaskDWord);
8403 tmpReg[3] = phy_query_bb_reg(priv, 0xbe0, bMaskDWord);
8404 tmpReg[4] = phy_query_bb_reg(priv, 0xbe8, bMaskDWord);
8405
8406 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x0101701a);
8407 tmpReg[5] = phy_query_bb_reg(priv, 0xbe0, bMaskDWord);
8408
8409 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x0101701b);
8410 tmpReg[6] = phy_query_bb_reg(priv, 0xbdc, bMaskDWord);
8411 tmpReg[7] = phy_query_bb_reg(priv, 0xbe8, bMaskDWord);
8412
8413 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x0101701c);
8414 tmpReg[8] = phy_query_bb_reg(priv, 0xbe8, bMaskDWord);
8415
8416 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x0101701e);
8417 tmpReg[9] = phy_query_bb_reg(priv, 0xbdc, bMaskDWord);
8418
8419 phy_set_bb_reg(priv, 0xb00, bMaskDWord, 0x0101701f);
8420 tmpReg[10] = phy_query_bb_reg(priv, 0xbe8, bMaskDWord);
8421
8422 //RTPRINT(FINIT, INIT_IQK, ("==>_PHY_Find_Tx_Power_Index path A\n"));
8423
8424 }
8425 else if(path == RF_PATH_B)
8426 {
8427 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x01017018);
8428 tmpReg[0] = phy_query_bb_reg(priv, 0xbec, bMaskDWord);
8429 tmpReg[1] = phy_query_bb_reg(priv, 0xbf8, bMaskDWord);
8430
8431 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x01017019);
8432 tmpReg[2] = phy_query_bb_reg(priv, 0xbec, bMaskDWord);
8433 tmpReg[3] = phy_query_bb_reg(priv, 0xbf0, bMaskDWord);
8434 tmpReg[4] = phy_query_bb_reg(priv, 0xbf8, bMaskDWord);
8435
8436 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x0101701a);
8437 tmpReg[5] = phy_query_bb_reg(priv, 0xbf4, bMaskDWord);
8438
8439 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x0101701b);
8440 tmpReg[6] = phy_query_bb_reg(priv, 0xbec, bMaskDWord);
8441 tmpReg[7] = phy_query_bb_reg(priv, 0xbf8, bMaskDWord);
8442
8443 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x0101701c);
8444 tmpReg[8] = phy_query_bb_reg(priv, 0xbf8, bMaskDWord);
8445
8446 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x0101701e);
8447 tmpReg[9] = phy_query_bb_reg(priv, 0xbec, bMaskDWord);
8448
8449 phy_set_bb_reg(priv, 0xb70, bMaskDWord, 0x0101701f);
8450 tmpReg[10] = phy_query_bb_reg(priv, 0xbf8, bMaskDWord);
8451
8452 //RTPRINT(FINIT, INIT_IQK, ("==>_PHY_Find_Tx_Power_Index path B\n"));
8453
8454 }
8455
8456 for(i = 0; i < RegNum; i++)
8457 {
8458 power_I = (tmpReg[i] >> 8);
8459 if(power_I & BIT(7))
8460 power_I |= bMaskH3Bytes;
8461
8462 power_Q = tmpReg[i] & bMaskByte0;
8463 if(power_Q & BIT(7))
8464 power_Q |= bMaskH3Bytes;
8465
8466 PA_power[i] = power_I*power_I+ power_Q*power_Q;
8467 }
8468
8469 //normalization
8470 tmpBase = PA_power[0];
8471 //RTPRINT(FINIT, INIT_IQK, ("==>PA_power START normalized\n"));
8472
8473 if(tmpBase == 0)
8474 DPK_DEBUG("==>PA_power[0] is ZERO !!!!!\n");
8475
8476 for(i = 0; i < RegNum; i++)
8477 {
8478 if(tmpBase != 0)
8479 PA_power[i] = (PA_power[i] << 9) /tmpBase;
8480 else
8481 PA_power[i] = (PA_power[i] << 9) ;
8482 PA_power_normal[i] = PA_power[i];
8483 PA_power[i] = (PA_power[i] > base)?(PA_power[i] - base):(base - PA_power[i]);
8484 DPK_DEBUG("==>PA_power normalized index %d value 0x%x\n", i, PA_power[i]);
8485 }
8486
8487 //choose min for TX index to do DPK
8488 base = bMaskDWord;
8489 for(i = 0; i < RegNum; i++)
8490 {
8491 if(PA_power[i] < base)
8492 {
8493 base = PA_power[i];
8494 index = i;
8495 }
8496 }
8497
8498 if(index == -1)
8499 {
8500 tx_index = 0x1c;
8501 index = 0x1f - tx_index;
8502 }
8503 else
8504 {
8505 tx_index += index;
8506 }
8507
8508 DPK_DEBUG("==>tx_index result 0x%x PA_power[%d] = 0x%x\n", tx_index, index, PA_power[index]);
8509
8510 *tx_index_out = tx_index;
8511
8512 //Check pattern reliability
8513 if(((PA_power_normal[index] > check_base) && (tx_index == 0x1f)) ||
8514 ((PA_power_normal[10] > base) && (!bPlus3db)) ||
8515 ((tx_index < 0x1a) && (!bPlus3db)) ||
8516 ((tx_index < 0x13) && (bDecreaseTxIndex))
8517 )
8518 return FALSE;
8519 else
8520 return TRUE;
8521// return tx_index;
8522
8523}
8524
8525unsigned char _PHY_Find_Rx_Power_Index(struct rtl8192cd_priv *priv, unsigned char tx_index, unsigned char rx_index, unsigned char path, char *bDecreaseTxIndex)
8526{
8527// u1Byte rx_index = 0x04;
8528 unsigned int tmpReg;
8529 int power_I, power_Q, tmp;
8530 unsigned char bPlus = FALSE, bMinus = FALSE;
8531 unsigned short offset[2][2] = {{ //path, offset
8532 0xb28, 0xbe8},{
8533 0xb98, 0xbf8}};
8534 int round = 0;
8535
8536 while (TRUE) {
8537 tmpReg = 0x52000 | tx_index | (rx_index << 5);
8538 phy_set_rf_reg(priv, path, RF_AC, bMask20Bits, tmpReg);
8539 //RTPRINT(FINIT, INIT_IQK, ("==>RF 0ffset 0 = 0x%x readback = 0x%x\n", tmpReg,
8540 //phy_query_rf_reg(pAdapter, path, RF_AC, bRFRegOffsetMask)));
8541
8542 //----send one shot signal----//
8543 phy_set_bb_reg(priv, offset[path][0], bMaskDWord, 0x80080000); //0xb28, 0xb98
8544 phy_set_bb_reg(priv, offset[path][0], bMaskDWord, 0x00080000);
8545
8546 _PHY_DPK_polling(priv);
8547
8548 tmpReg = phy_query_bb_reg(priv, offset[path][1], bMaskDWord);
8549 power_I = ((tmpReg & bMaskByte1) >> 8);
8550 power_Q = tmpReg & bMaskByte0;
8551
8552 if(power_I & BIT(7))
8553 {
8554 power_I |= bMaskH3Bytes;
8555 power_I = 0-power_I; //absolute value
8556 }
8557
8558 if(power_Q & BIT(7))
8559 {
8560 power_Q |= bMaskH3Bytes;
8561 power_Q = 0-power_Q;
8562 }
8563 //RTPRINT(FINIT, INIT_IQK, ("==>rx_index 0x%x I = 0x%x Q = 0x%x offset 0xbe8 = 0x%x\n", rx_index, power_I, power_Q, tmpReg));
8564
8565 tmp = (power_I > power_Q)? power_I:power_Q;
8566
8567#if 0
8568 if((rx_index == 0 && tmp > 0x6f)||(rx_index == 31 && tmp < 0x50))
8569 break;
8570#endif
8571
8572 if((tmp<= 0x6f && tmp >= 0x50) )
8573 {
8574 break;
8575 }
8576 else if(tmp < 0x50)
8577 {
8578 bPlus = TRUE;
8579 if(bMinus)
8580 {
8581 rx_index++;
8582 break;
8583 }
8584// rx_index++;
8585 rx_index += 2;
8586 }
8587 else if (tmp > 0x6f)
8588 {
8589 bMinus = TRUE;
8590 if(bPlus)
8591 {
8592 rx_index--;
8593 break;
8594 }
8595// rx_index--;
8596 rx_index -= 2;
8597 }
8598
8599 if(rx_index == 0 || rx_index == 31)
8600 break;
8601
8602 if (++round > 10000) {
8603 panic_printk("%s[%d] while (1) goes too many\n", __FUNCTION__, __LINE__);
8604 break;
8605 }
8606 }
8607 if(rx_index == 0 && tmp > 0x6f)
8608 *bDecreaseTxIndex = TRUE;
8609
8610 DPK_DEBUG("==>rx_index FINAL 0x%x I = 0x%x Q = 0x%x\n", rx_index, power_I, power_Q);
8611
8612 return rx_index;
8613
8614}
8615
8616void PHY_DPCalibrate(struct rtl8192cd_priv *priv)
8617{
8618 char is2T = ((priv->pmib->dot11RFEntry.macPhyMode != DUALMAC_DUALPHY) ?1 :0);
8619
8620 unsigned int tmpReg, value32/*, checkbit*/;
8621 unsigned int AFE_backup[IQK_ADDA_REG_NUM];
8622 static unsigned int AFE_REG[IQK_ADDA_REG_NUM] = {
8623 REG_FPGA0_XCD_SWITCH_CONTROL, 0xe6c, 0xe70, 0xe74, 0xe78,
8624 0xe7c, 0xe80, 0xe84, 0xe88, 0xe8c,
8625 0xed0, 0xed4, 0xed8, 0xedc, 0xee0,
8626 0xeec};
8627
8628 static unsigned int BB_backup[DP_BB_REG_NUM];
8629 static unsigned int BB_REG[DP_BB_REG_NUM] = {
8630 REG_OFDM_0_TRX_PATH_ENABLE, REG_FPGA0_RFMOD,
8631 REG_OFDM_0_TR_MUX_PAR, REG_FPGA0_XCD_RF_INTERFACE_SW,
8632 REG_FPGA0_ANALOG_PARAMETER4, REG_FPGA0_XAB_RF_INTERFACE_SW,
8633 REG_TX_AGC_B_CCK_11_A_CCK_2_11
8634 };
8635
8636 static unsigned int BB_backup_A[DP_BB_REG_NUM_A];
8637 static unsigned int BB_REG_A[DP_BB_REG_NUM_A] = {
8638 REG_FPGA0_XA_RF_INTERFACE_OE, REG_TX_AGC_A_RATE18_06,
8639 REG_TX_AGC_A_RATE54_24, REG_TX_AGC_A_CCK_1_MCS32,
8640 0xe0c, REG_TX_AGC_A_MCS03_MCS00,
8641 REG_TX_AGC_A_MCS07_MCS04, REG_TX_AGC_A_MCS11_MCS08,
8642 REG_TX_AGC_A_MCS15_MCS12, REG_OFDM_0_XA_AGC_CORE1/*,
8643 rBndA*/
8644 };
8645
8646
8647 static unsigned int BB_backup_B[DP_BB_REG_NUM_B];
8648 static unsigned int BB_REG_B[DP_BB_REG_NUM_B] = {
8649 REG_FPGA0_XB_RF_INTERFACE_OE, REG_TX_AGC_B_RATE18_06,
8650 REG_TX_AGC_B_RATE54_24, REG_TX_AGC_B_CCK_1_55_MCS32,
8651 REG_TX_AGC_B_MCS03_MCS00, REG_TX_AGC_B_MCS07_MCS04,
8652 REG_TX_AGC_B_MCS11_MCS08, REG_TX_AGC_B_MCS15_MCS12,
8653 REG_OFDM_0_XB_AGC_CORE1 /*, rBndB*/
8654 };
8655
8656 static unsigned int BB_settings[DP_BB_REG_NUM_settings] = {
8657 0x00a05430, 0x02040000, 0x000800e4, 0x22208000,
8658 0xccf000c0/*, 0x07600760*/};
8659
8660 static unsigned int BB_REG_loop[DP_PATH_NUM][DP_BB_REG_NUM_loop] = {
8661 {0xb00, 0xb04, 0xb28, 0xb68,
8662 0xb08, 0xb0c, 0xb10, 0xb14,
8663 0xb18, 0xb1c, 0xb20, 0xb24,
8664 0xe28, 0xb00, 0xb04, 0xb08,
8665 0xb0c, 0xb10, 0xb14, 0xb18,
8666 0xb1c, 0xb20, 0xb24, 0xb28,
8667 0xb2c, rBndA, 0xb34, 0xb38,
8668 0xb3c, 0xe28},
8669 {0xb70, 0xb74, 0xb98, 0xb6C,
8670 0xb78, 0xb7c, 0xb80, 0xb84,
8671 0xb88, 0xb8c, 0xb90, 0xb94,
8672 0xe28, 0xb60, 0xb64, 0xb68,
8673 0xb6c, 0xb70, 0xb74, 0xb78,
8674 0xb7c, 0xb80, 0xb84, 0xb88,
8675 0xb8c, 0xb90, 0xb94, 0xb98,
8676 0xb9c, 0xe28}
8677 };
8678
8679 static unsigned int BB_settings_loop[DP_BB_REG_NUM_loop] = {
8680 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8681 0x41382e21, 0x5b554f48, 0x6f6b6661, 0x817d7874,
8682 0x908c8884, 0x9d9a9793, 0xaaa7a4a1, 0xb6b3b0ad,
8683 0x40000000, 0x7d327c18, 0x7e057db3, 0x7e5f7e37,
8684 0x7e967e7c, 0x7ebe7eac, 0x7ed77ecc, 0x7eee7ee4,
8685 0x7f017ef9, 0x7f0e7f07, 0x7f1c7f15, 0x7f267f20,
8686 0x7f2f7f2a, 0x7f377f34, 0x7f3e7f3b, 0x7f457f42,
8687 0x7f4b7f48, 0x00000000
8688 };
8689
8690 static unsigned int BB_settings_loop_3db[DP_BB_REG_NUM_loop] = {
8691 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8692 0x5b4e402e, 0x7f776f65, 0x9c968f88, 0xb5afa8a3,
8693 0xcac4bfb9, 0xdcd8d4ce, 0xeeeae6e2, 0xfffbf7f2,
8694 0x40000000, 0x7dfe7d32, 0x7e967e59, 0x7ed77eba,
8695 0x7efd7eeb, 0x7f1a7f0e, 0x7f2d7f25, 0x7f3c7f36,
8696 0x7f4a7f44, 0x7f547f4e, 0x7f5d7f58, 0x7f657f60,
8697 0x7f6a7f68, 0x7f717f6e, 0x7f767f73, 0x7f7b7f78,
8698 0x7f7f7f7d, 0x00000000
8699 };
8700
8701 static unsigned int BB_settings_loop_tx[DP_BB_REG_NUM_loop] = {
8702 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8703 0x21212121, 0x21212121, 0x21212121, 0x21212121,
8704 0x21212121, 0x21212121, 0x21212121, 0x21212121,
8705 0x40000000, 0x7c187c18, 0x7c187c18, 0x7c187c18,
8706 0x7c187c18, 0x7c187c18, 0x7c187c18, 0x7c187c18,
8707 0x7c187c18, 0x7c187c18, 0x7c187c18, 0x7c187c18,
8708 0x7c187c18, 0x7c187c18, 0x7c187c18, 0x7c187c18,
8709 0x7c187c18, 0x00000000
8710 };
8711
8712 static unsigned int BB_settings_loop_tx_3db[DP_BB_REG_NUM_loop] = {
8713 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8714 0x2e2e2e2e, 0x2e2e2e2e, 0x2e2e2e2e, 0x2e2e2e2e,
8715 0x2e2e2e2e, 0x2e2e2e2e, 0x2e2e2e2e, 0x2e2e2e2e,
8716 0x40000000, 0x7d327d32, 0x7d327d32, 0x7d327d32,
8717 0x7d327d32, 0x7d327d32, 0x7d327d32, 0x7d327d32,
8718 0x7d327d32, 0x7d327d32, 0x7d327d32, 0x7d327d32,
8719 0x7d327d32, 0x7d327d32, 0x7d327d32, 0x7d327d32,
8720 0x7d327d32, 0x00000000
8721 };
8722
8723
8724 //for find 2dB loss point
8725 static unsigned int BB_settings_loop_tx_2[DP_BB_REG_NUM_loop] = {
8726 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8727 0x41382e21, 0x5b554f48, 0x6f6b6661, 0x817d7874,
8728 0x908c8884, 0x9d9a9793, 0xaaa7a4a1, 0xb6b3b0ad,
8729 0x40000000, 0x7d327c18, 0x7e057db3, 0x7e5f7e37,
8730 0x7e967e7c, 0x7ebe7eac, 0x7ed77ecc, 0x7eee7ee4,
8731 0x7f017ef9, 0x7f0e7f07, 0x7f1c7f15, 0x7f267f20,
8732 0x7f2f7f2a, 0x7f377f34, 0x7f3e7f3b, 0x7f457f42,
8733 0x7f4b7f48, 0x00000000
8734 };
8735
8736 //for find 2dB loss point
8737 static unsigned int BB_settings_loop_tx_2_3db[DP_BB_REG_NUM_loop] = {
8738 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8739 0x5b4e402e, 0x7f776f65, 0x9c968f88, 0xb5afa8a3,
8740 0xcac4bfb9, 0xdcd8d4ce, 0xeeeae6e2, 0xfffbf7f2,
8741 0x40000000, 0x7dfe7d32, 0x7e967e59, 0x7ed77eba,
8742 0x7efd7eeb, 0x7f1a7f0e, 0x7f2d7f25, 0x7f3c7f36,
8743 0x7f4a7f44, 0x7f547f4e, 0x7f5d7f58, 0x7f657f60,
8744 0x7f6a7f68, 0x7f717f6e, 0x7f767f73, 0x7f7b7f78,
8745 0x7f7f7f7d, 0x00000000
8746 };
8747
8748
8749 static unsigned int BB_settings_loop_rx[DP_BB_REG_NUM_loop_rx] = {
8750 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8751 0x21212121, 0x40000000, 0x7c187c18, 0x00000000
8752 };
8753
8754 static unsigned int BB_settings_loop_rx_3db[DP_BB_REG_NUM_loop_rx] = {
8755 0x01017e18, 0xf76d9f84, 0x00080000, 0x11880000,
8756 0x2e2e2e2e, 0x40000000, 0x7d327d32, 0x00000000
8757 };
8758
8759 static unsigned int BB_settings_loop_pa[DP_BB_REG_NUM_loop_pa] = {
8760 0x02096eb8, 0xf76d9f84, 0x00044499, 0x02880140
8761 };
8762
8763 static unsigned int BB_settings_loop_dp[DP_BB_REG_NUM_loop_pa] = {
8764 0x01017098, 0x776d9f84, 0x00000000, 0x08080000
8765 };
8766
8767 unsigned int *BB_settings_temp;
8768
8769 static unsigned char Sram_db_settings[DP_SRAM_NUM_db] = {
8770 0xfe, 0xf0, 0xe3, 0xd6, 0xca,
8771 0xbf, 0xb4, 0xaa, 0xa0, 0x97,
8772 0x8f, 0x87, 0x7f, 0x78, 0x71,
8773 0x6b, 0x65, 0x5f, 0x5a, 0x55,
8774 0x50, 0x4c, 0x47, 0x43, 0x40,
8775 0x3c, 0x39, 0x35, 0x32, 0x2f,
8776 0x2d, 0x2a, 0x28, 0x26, 0x23,
8777 0x21, 0x20, 0x1e, 0x1c, 0x1a,
8778 0x19, 0x18, 0x16, 0x16, 0x14,
8779 0x13, 0x12, 0x11, 0x10, 0x0f,
8780 0x0e, 0x0d, 0x0c, 0x0c, 0x0b,
8781 0x0a, 0x0a, 0x09, 0x09, 0x08,
8782 0x08, 0x07, 0x07, 0x06, 0x06,
8783 0x06, 0x05, 0x05, 0x05, 0x04,
8784 0x04, 0x04, 0x04, 0x03, 0x03,
8785 0x03, 0x03, 0x03, 0x02, 0x02,
8786 0x02, 0x02, 0x02, 0x02, 0x02,
8787 0x01
8788 };
8789
8790 //unsigned int pwsf[DP_SRAM_NUM];
8791
8792 static unsigned int offset[2][DP_OFFSET_NUM] = {{ //path, offset
8793 0xe34, 0xb28, 0xb00, 0xbdc, 0xbc0,
8794 0xbe8, REG_OFDM_0_XA_TX_IQ_IMBALANCE, rBndA,
8795 0xb68},{
8796 0xe54, 0xb98, 0xb70, 0xbec, 0xbc4,
8797 0xbf8, REG_OFDM_0_XB_TX_IQ_IMBALANCE, rBndB,
8798 0xb6c}};
8799
8800 //unsigned char OFDM_min_index = 6, OFDM_min_index_internalPA = 3;
8801 unsigned char OFDM_index[2];
8802 //unsigned char retrycount = 0, retrybound = 1;
8803
8804 unsigned int RF_backup[DP_PATH_NUM][DP_RF_REG_NUM];
8805 static unsigned int RF_REG[DP_RF_REG_NUM] = {
8806 RF_TX_G3, RF_TXPA_A4, RF_RXRF_A3,
8807 RF_BS_PA_APSET_G1_G4/*, RF_BS_PA_APSET_G5_G8,
8808 RF_BS_PA_APSET_G9_G11*/};
8809
8810 static unsigned int RF_AP_curve_select[DP_AP_CUREVE_SELECT_NUM] = {
8811 0x7bdef, 0x94a52, 0xa5294/*, 0xb5ad6*/ };
8812
8813 static unsigned int RF_PA_BIAS[3][DP_PA_BIAS_NUM] = {{ //40MHz / 20MHz, original
8814 0xe189f, 0xa189f, 0x6189f, 0x2189f },{
8815 0xe087f, 0xa087f, 0x6087f, 0x2087f },{
8816 0xe1874, 0xa1874, 0x61874, 0x21874}};
8817
8818 unsigned int PA_model_backup[DP_PATH_NUM][DP_PA_MODEL_NUM];
8819
8820 unsigned int PA_power[DP_PATH_NUM][DP_PA_MODEL_RUN_NUM*2];
8821
8822#if DP_gain_loss == 1
8823
8824 int power_I, power_Q, coef;
8825
8826 int gain_loss_backup[DP_PATH_NUM][DP_PA_MODEL_NUM]; //I,Q
8827
8828 static unsigned int gain_loss_bound[DP_GAIN_LOSS_BOUND_NUM] = {
8829 63676, 60114 , 56751 , 53577 , 49145,
8830 47750, 45079 , 42557 , 40177 , 37929 ,
8831 35807 , 33804 , 31913, 30128
8832 };
8833
8834 static int gain_loss_coef[DP_GAIN_LOSS_BOUND_NUM+1] = {
8835 512, 527, 543, 558, 573,
8836 589, 609, 625, 645, 666,
8837 681, 701, 722, 742, 768
8838 };
8839
8840// BOOLEAN bNegative = FALSE;
8841// unsigned char index_for_zero_db = 24, AP_curve_index = 0;
8842 unsigned char GainLossIndex = 0; //0db, 0x40
8843 char SramIndex = 24;
8844 unsigned char /*index_for_zero_db = 6,*/ AP_curve_index = 0;
8845
8846#else
8847
8848 unsigned char index_for_zero_db = 6, AP_curve_index = 0;
8849 int power_I, power_Q;
8850 static unsigned int gain_loss_bound[DP_GAIN_LOSS_BOUND_NUM] = {
8851 61870, 55142, 49145, 43801, 39037,
8852 34792, 31008, 27636
8853 };
8854#endif
8855
8856 unsigned int MAC_backup[IQK_MAC_REG_NUM];
8857 static unsigned int MAC_REG[IQK_MAC_REG_NUM] = {
8858 0x522, 0x550, 0x551, 0x040};
8859
8860 //unsigned int AFE_on_off[PATH_NUM] = {
8861 // 0x04db25a4, 0x0b1b25a4}; //path A on path B path A off path B on
8862
8863 unsigned char path_num, /*path_bound,*/ path = RF_PATH_A, i, j, tx_index, rx_index;
8864 int index, index_1, index_repeat;
8865
8866 char bInternalPA = FALSE;
8867#if (DP_gain_loss != 1)
8868 char SkipStep5 = FALSE;
8869#endif
8870 char bPlus3db = FALSE, bDecreaseTxIndex = FALSE, bDecreaseTxIndexWithRx = FALSE;
8871
8872#ifdef DFS
8873 if ((priv->pshare->rf_ft_var.dfsdelayiqk) &&
8874 (OPMODE & WIFI_AP_STATE) &&
8875 !priv->pmib->dot11DFSEntry.disable_DFS &&
8876 (rtk_timer_pending(&priv->ch_avail_chk_timer) ||
8877 priv->pmib->dot11DFSEntry.disable_tx))
8878 return;
8879#endif
8880
8881 DPK_DEBUG("==>_PHY_DigitalPredistortion() interface index %d is2T = %d\n", priv->pshare->wlandev_idx, is2T); //anchin
8882
8883 DPK_DEBUG("_PHY_DigitalPredistortion for %s\n", (is2T ? "2T2R" : "1T1R"));
8884
8885 DPK_DEBUG("==>_PHY_DigitalPredistortion() current thermal meter = 0x%x PG thermal meter = 0x%x bPlus3db %d\n",
8886 priv->pshare->ThermalValue_DPKtrack, priv->pmib->dot11RFEntry.ther, bPlus3db);
8887
8888 if ((priv->pmib->dot11RFEntry.phyBandSelect!= PHY_BAND_5G)||(GET_CHIP_VER(priv)!=VERSION_8192D))
8889 return;
8890
8891 bInternalPA = priv->pshare->rf_ft_var.use_intpa92d;
8892
8893 if(!is2T)
8894 path_num = 1;
8895 else
8896 path_num = 2;
8897
8898 if(!bInternalPA) {
8899 DPK_DEBUG("==>_PHY_DigitalPredistortion() NOT internal5G\n");
8900 return;
8901 }
8902
8903 if(priv->pshare->pwr_trk_ongoing){
8904 DPK_DEBUG("==>_PHY_DigitalPredistortion() TxPowerTrackingInProgress() delay 100ms\n");
8905 rtk_mod_timer(&priv->pshare->DPKTimer, jiffies + RTL_MILISECONDS_TO_JIFFIES(100));
8906 return;
8907 }
8908
8909 OFDM_index[RF_PATH_A] = priv->pshare->OFDM_index[RF_PATH_A];
8910 OFDM_index[RF_PATH_B] = priv->pshare->OFDM_index[RF_PATH_B];
8911
8912 DPK_DEBUG("original index 0x%x \n", priv->pshare->OFDM_index[0]);
8913
8914 priv->pshare->bDPKworking = TRUE;
8915
8916 //save global setting
8917 //save BB default value
8918 _phy_save_adda_registers(priv, BB_REG, BB_backup, DP_BB_REG_NUM);
8919
8920 //save MAC default value
8921 _phy_save_mac_registers(priv, MAC_REG, MAC_backup);
8922
8923 //save AFE default value
8924 _phy_save_adda_registers(priv, AFE_REG, AFE_backup, IQK_ADDA_REG_NUM);
8925
8926 //save path A default value
8927 //save path A BB default value
8928 _phy_save_adda_registers(priv, BB_REG_A, BB_backup_A, DP_BB_REG_NUM_A);
8929
8930 //save path B BB default value
8931 if(is2T)
8932 _phy_save_adda_registers(priv, BB_REG_B, BB_backup_B, DP_BB_REG_NUM_B);
8933
8934 //save pathA/B RF default value
8935 for(path=0; path<path_num; path++){
8936 for(index=0; index<DP_RF_REG_NUM; index++)
8937 RF_backup[path][index] = phy_query_rf_reg(priv, path, RF_REG[index], bMaskDWord, 1);
8938 }
8939
8940 //BB register setting
8941 for(index = 0; index < DP_BB_REG_NUM_settings; index++)
8942 phy_set_bb_reg(priv, BB_REG[index], bMaskDWord, BB_settings[index]);
8943
8944 //BB path A debug setting
8945 phy_set_bb_reg(priv, rFPGA1_DebugSelect, bMaskDWord, 0x00000302);
8946
8947 //BB pah A register setting: fix TRSW to TX, external PA on, external LAN off
8948 if(!bInternalPA)
8949 {
8950 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x07600f60);
8951 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, ~(BIT8|BIT9), 0x66e60a30);
8952 }
8953 else
8954 {
8955 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x07600760);
8956 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, ~(BIT8|BIT9), 0x66e60230);
8957 }
8958 phy_set_bb_reg(priv, rBndA, 0xF00000, 0x0a);
8959
8960 //BB pah B register setting: fix TRSW to TX, external PA off, external LNA off
8961 if(is2T)
8962 {
8963 if(!bInternalPA)
8964 {
8965 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x0f600f60);
8966 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, bMaskDWord, 0x061f0130);
8967 }
8968 else
8969 {
8970 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x07600760);
8971 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, bMaskDWord, 0x061f0130);
8972 }
8973 phy_set_bb_reg(priv, rBndB, 0xF00000, 0x0a);
8974 }
8975
8976 //MAC register setting
8977 _phy_mac_setting_calibration(priv, MAC_REG, MAC_backup);
8978
8979 //path A/B DPK
8980 //Path-A/B AFE all on
8981 for(path=0; path<path_num; path++)
8982 {
8983
8984 //if(is2T && !pHalData->InternalPA5G[path])
8985 //continue;
8986
8987 if(path == RF_PATH_B)
8988 {
8989 //BB pah A register setting:fix TRSW to TX;external LNA off
8990 if(!bInternalPA)
8991 {
8992 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x0f600f60);
8993 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, ~(BIT8|BIT9), 0x66e60230);
8994 }
8995 else
8996 {
8997 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x07600760);
8998 phy_set_bb_reg(priv, REG_FPGA0_XA_RF_INTERFACE_OE, ~(BIT8|BIT9), 0x66e60230);
8999 }
9000 phy_set_bb_reg(priv, rBndA, 0xF00000, 0x0a);
9001
9002 //BB pah B register setting:fix TRSW to TX;external LNA off
9003 if(is2T)
9004 {
9005 if(!bInternalPA)
9006 {
9007 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x0f600f60);
9008 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, bMaskDWord, 0x061f0930);
9009 }
9010 else
9011 {
9012 phy_set_bb_reg(priv, REG_FPGA0_XAB_RF_INTERFACE_SW, bMaskDWord, 0x07600760);
9013 phy_set_bb_reg(priv, REG_FPGA0_XB_RF_INTERFACE_OE, bMaskDWord, 0x061f0130);
9014 }
9015 phy_set_bb_reg(priv, rBndB, 0xF00000, 0x0a);
9016 }
9017 }
9018
9019 AP_curve_index = 1;
9020 rx_index = 0x06;
9021 bPlus3db = FALSE;
9022 bDecreaseTxIndex = FALSE;
9023
9024 if(path == RF_PATH_A)
9025 {
9026 _phy_path_adda_on(priv, AFE_REG, TRUE, is2T);
9027 }
9028 else
9029 {
9030 _phy_path_adda_on(priv, AFE_REG, FALSE, is2T);
9031 }
9032
9033 if(path == RF_PATH_B)
9034 phy_set_bb_reg(priv, rFPGA1_DebugSelect, bMaskDWord, 0x00000303);
9035
9036 //path A/B RF setting
9037 //internal lopback off
9038 if(path == RF_PATH_A && !bInternalPA)
9039 {
9040 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE1, bMask20Bits, 0x5007f);
9041 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE2, bMask20Bits, 0x6f1f9);
9042 }
9043 else if(path == RF_PATH_B)
9044 {
9045 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE1, bMask20Bits, 0x1000f);
9046 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE2, bMask20Bits, 0x60103);
9047 }
9048
9049 phy_set_rf_reg(priv, path, RF_RXRF_A3, bMask20Bits, 0xef456);
9050
9051 //Path A/B to standby mode
9052 phy_set_rf_reg(priv, path==RF_PATH_A?RF_PATH_B:RF_PATH_A,
9053 RF_AC, bMask20Bits, 0x10000);
9054
9055 //set DPK PA bias table
9056 index = priv->pshare->CurrentChannelBW == CHANNEL_WIDTH_40?0:1;
9057 if(path == RF_PATH_A)
9058 {
9059 for(i = 0; i < path_num; i++)
9060 {
9061 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9062 phy_set_rf_reg(priv, i, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[index][j]);
9063 }
9064 }
9065
9066Step1:
9067
9068 DPK_DEBUG("==>AP curve select 0x%x bplus3db %d path%s!!\n", RF_AP_curve_select[AP_curve_index], bPlus3db, path==RF_PATH_A?"A":"B");
9069
9070 //RF setting for AP curve selection
9071 //default AP curve = 15
9072 phy_set_rf_reg(priv, path, RF_BS_PA_APSET_G1_G4, bMask20Bits, RF_AP_curve_select[AP_curve_index]);
9073
9074 //////////////////////////////////////////////////
9075 // step 1: find RF TX/RX index
9076 /////////////////////////////////////////////////
9077 //find RF TX index
9078 //=============================
9079 // PAGE_B for Path-A PM setting
9080 //=============================
9081 // open inner loopback @ b00[19]:od 0xb00 0x01097018
9082 if(bPlus3db)
9083 BB_settings_temp = &(BB_settings_loop_tx_3db[0]);
9084 else
9085 BB_settings_temp = &(BB_settings_loop_tx[0]);
9086 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_temp, DP_BB_REG_NUM_loop);
9087
9088 if(bDecreaseTxIndex)
9089 tx_index = 0x19;
9090 else
9091 tx_index = 0x1f;
9092 bDecreaseTxIndexWithRx = FALSE;
9093
9094 //Set Tx GAC = 0x1f, than find Rx AGC
9095 rx_index = _PHY_Find_Rx_Power_Index(priv, tx_index, rx_index, path, &bDecreaseTxIndexWithRx);
9096 if(bDecreaseTxIndexWithRx)
9097 {
9098 if(bDecreaseTxIndex)
9099 {
9100 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017098); //0xb00, 0xb70
9101 phy_set_bb_reg(priv, offset[path][8], bMaskDWord, 0x28080000); //0xb68, 0xb6c
9102
9103 for(i = 3; i < DP_RF_REG_NUM; i++)
9104 phy_set_rf_reg(priv, path, RF_REG[i], bMask20Bits, RF_backup[path][i]);
9105
9106 //set original DPK bias table
9107 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9108 phy_set_rf_reg(priv, path, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[2][j]);
9109 continue;
9110 }
9111 else
9112 {
9113 bDecreaseTxIndex = TRUE;
9114 goto Step1;
9115 }
9116 }
9117
9118 //find 2dB loss point
9119 //=============================
9120 // PAGE_B for Path-A PM setting
9121 //=============================
9122 // open inner loopback @ b00[19]:od 0xb00 0x01097018
9123 if(bPlus3db)
9124 BB_settings_temp = &(BB_settings_loop_tx_2_3db[0]);
9125 else
9126 BB_settings_temp = &(BB_settings_loop_tx_2[0]);
9127 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_temp, DP_BB_REG_NUM_loop);
9128
9129 //RF setting
9130 phy_set_rf_reg(priv, path, RF_AC, bMask20Bits, 0x52000 | tx_index | (rx_index << 5));
9131
9132 //----send one shot signal----//
9133 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x80080000); //0xb28, 0xb98
9134 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x00080000);
9135
9136 //get power
9137 if(!_PHY_Find_Tx_Power_Index(priv, PA_power[path], path, bPlus3db, bDecreaseTxIndex, &tx_index))
9138 {
9139 if(/*tx_index == 0x1f &&*/ !bPlus3db)
9140 {
9141 if(bDecreaseTxIndex)
9142 {
9143 if(tx_index < 0x11)
9144 {
9145 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017098); //0xb00, 0xb70
9146 phy_set_bb_reg(priv, offset[path][8], bMaskDWord, 0x28080000); //0xb68, 0xb6c
9147
9148 for(i = 3; i < DP_RF_REG_NUM; i++)
9149 phy_set_rf_reg(priv, path, RF_REG[i], bMask20Bits, RF_backup[path][i]);
9150
9151 //set original DPK bias table
9152 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9153 phy_set_rf_reg(priv, path, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[2][j]);
9154
9155 continue;
9156 }
9157 else
9158 {
9159 //RTPRINT(FINIT, INIT_IQK, ("==>Check pattern reliability path%s SUCCESS tx_index = 0x1b!!!!\n", path==RF90_PATH_A?"A":"B"));
9160 }
9161 }
9162 else if(tx_index < 0x1a)
9163 {
9164 bDecreaseTxIndex = TRUE;
9165 goto Step1;
9166 }
9167 else
9168 {
9169 bPlus3db = TRUE;
9170 goto Step1;
9171 }
9172 }
9173 else if(tx_index == 0x1f)
9174 {
9175 DPK_DEBUG("==>Check pattern reliability path%s FAIL!!!!\n", path==RF_PATH_A?"A":"B");
9176 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017098); //0xb00, 0xb70
9177 phy_set_bb_reg(priv, offset[path][8], bMaskDWord, 0x28080000); //0xb68, 0xb6c
9178
9179 for(i = 3; i < DP_RF_REG_NUM; i++)
9180 phy_set_rf_reg(priv, path, RF_REG[i], bMask20Bits, RF_backup[path][i]);
9181
9182 //set original DPK bias table
9183 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9184 phy_set_rf_reg(priv, path, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[2][j]);
9185
9186 continue;
9187 }
9188
9189
9190 }
9191 else
9192 {
9193 DPK_DEBUG("==>Check pattern reliability path%s SUCCESS!!!!\n", path==RF_PATH_A?"A":"B");
9194 }
9195
9196 //find RF RX index
9197 //=============================
9198 // PAGE_B for Path-A PM setting
9199 //=============================
9200 // open inner loopback @ b00[19]:od 0xb00 0x01097018
9201 if(bPlus3db)
9202 BB_settings_temp = &(BB_settings_loop_rx_3db[0]);
9203 else
9204 BB_settings_temp = &(BB_settings_loop_rx[0]);
9205
9206 for(i = 0; i < 4; i++)
9207 phy_set_bb_reg(priv, BB_REG_loop[path][i], bMaskDWord, BB_settings_temp[i]);
9208 for(; i < 12; i++)
9209 phy_set_bb_reg(priv, BB_REG_loop[path][i], bMaskDWord, BB_settings_temp[4]);
9210 phy_set_bb_reg(priv, BB_REG_loop[path][i], bMaskDWord, BB_settings_temp[5]);
9211 for(; i < 29; i++)
9212 phy_set_bb_reg(priv, BB_REG_loop[path][i], bMaskDWord, BB_settings_temp[6]);
9213 phy_set_bb_reg(priv, BB_REG_loop[path][i], bMaskDWord, BB_settings_temp[7]);
9214
9215 rx_index = _PHY_Find_Rx_Power_Index(priv, tx_index, rx_index, path, &bDecreaseTxIndex);
9216
9217 //////////////////////////////////////
9218 //2.measure PA model
9219 //////////////////////////////////////
9220 //=========================================
9221 //PAGE_B for Path-A PAS setting //=========================================
9222 // open inner loopback @ b00[19]:10 od 0xb00 0x01097018
9223 if(bPlus3db)
9224 BB_settings_temp = &(BB_settings_loop_3db[0]);
9225 else
9226 BB_settings_temp = &(BB_settings_loop[0]);
9227 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_temp, DP_BB_REG_NUM_loop);
9228
9229 //LNA VDD to gnd
9230 phy_set_rf_reg(priv,path, RF_AC, bMask20Bits, 0x52000 | tx_index | (rx_index << 5));
9231
9232 //----send one shot signal----//
9233 // Path A
9234 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x80080000); //0xb28, 0xb98
9235 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x00080000);
9236
9237 phy_set_rf_reg(priv, RF_PATH_A, RF_T_METER, BIT17|BIT16, 0x03);
9238
9239 _PHY_DPK_polling(priv);
9240
9241 priv->pshare->ThermalValue_DPKstore = (unsigned char)phy_query_rf_reg(priv, RF_PATH_A, RF_T_METER, 0xf800, 1); //0x42: RF Reg[15:11] 92D
9242
9243 // read PA model and save to PA_model_A[32]
9244 for(i = 0; i < DP_PA_MODEL_RUN_NUM; i++)
9245 {
9246 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017018+i); //0xb00, 0xb70
9247 for(index = 0; index < DP_PA_MODEL_PER_RUN_NUM; index++)
9248 {
9249 PA_model_backup[path][i*4+index] = phy_query_bb_reg(priv, offset[path][3]+index*4, bMaskDWord); //0xbdc, 0xbec
9250 DPK_DEBUG("==>PA_model_backup index %d value 0x%x()\n", i*4+index, PA_model_backup[path][i*4+index]);
9251 }
9252 }
9253
9254#if 0
9255 //find appropriate AP curve
9256 if(AP_curve_index != (DP_AP_CUREVE_SELECT_NUM-1))
9257 {
9258 if(!_PHY_DPK_AP_curve_check(priv, PA_model_backup[path], DP_PA_MODEL_NUM))
9259 {
9260 DPK_DEBUG("==>find appropriate AP curve 0x%x path%s FAIL!!!!\n", RF_AP_curve_select[AP_curve_index], path==RF_PATH_A?"A":"B");
9261 AP_curve_index++;
9262 if(AP_curve_index < DP_AP_CUREVE_SELECT_NUM)
9263 goto Step1;
9264 }
9265 else
9266 {
9267 DPK_DEBUG("==>find appropriate AP curve path%s SUCCESS!!!!\n", path==RF_PATH_A?"A":"B");
9268 }
9269 }
9270#endif
9271
9272 //check PA model
9273 if(!_PHY_DPK_check(priv, PA_model_backup[path], DP_PA_MODEL_NUM))
9274 {
9275 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017098); // add in 2011-06-02
9276 phy_set_bb_reg(priv, offset[path][8], bMaskDWord, 0x28080000); //0xb68, 0xb6c
9277
9278 for(i = 3; i < DP_RF_REG_NUM; i++)
9279 phy_set_rf_reg(priv, path, RF_REG[i], bMaskDWord, RF_backup[path][i]);
9280 DPK_DEBUG("==>PA model path%s FAIL!!!!\n", path==RF_PATH_A?"A":"B");
9281 priv->pshare->bDPKdone[path] = FALSE;
9282 //set original DPK bias table
9283 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9284 phy_set_rf_reg(priv, path, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[2][j]);
9285
9286 continue;
9287 }
9288 else
9289 {
9290 DPK_DEBUG("==>PA model path%s SUCCESS!!!!\n", path==RF_PATH_A?"A":"B");
9291// priv->pshare->bDPKdone[path] = TRUE;
9292// priv->pshare->bDPKstore = TRUE;
9293 }
9294
9295 /////////////////////////////////////////////////////////////////////////////////////////////////////
9296 // step 3: fill PA model to DP Calibration
9297 /////////////////////////////////////////////////////////////////////////////////////////////////////
9298 //fill BB TX index for the DPK reference
9299 DPK_DEBUG("==>fill PA model to DP Calibration\n");
9300
9301 if(path == RF_PATH_A){
9302 for(index = 0; index < DP_PA_MODEL_RUN_NUM; index++){
9303 if(index != 3){
9304 phy_set_bb_reg(priv, 0xe00+index*4, bMaskDWord, 0x3c3c3c3c);
9305 } else {
9306 phy_set_bb_reg(priv, 0xe00+index*4, bMaskDWord, 0x03903c3c);
9307 }
9308 }
9309 phy_set_bb_reg(priv, 0x86c, bMaskDWord, 0x3c3c3c3c);
9310 }else if (path == RF_PATH_B){
9311 for(index = 0; index < 4; index++) {
9312 phy_set_bb_reg(priv, 0x830+index*4, bMaskDWord, 0x3c3c3c3c);
9313 }
9314 for(index = 0; index < 2; index++) {
9315 phy_set_bb_reg(priv, 0x848+index*4, bMaskDWord, 0x3c3c3c3c);
9316 }
9317 for(index = 0; index < 2; index++) {
9318 phy_set_bb_reg(priv, 0x868+index*4, bMaskDWord, 0x3c3c3c3c);
9319 }
9320 }
9321
9322 // SRAM boundary setting
9323 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x400000);
9324
9325 if(priv->pshare->phw->bNewTxGainTable)
9326 phy_set_bb_reg(priv, offset[path][4], bMaskDWord, 0x0008421f); //0xbc0, 0xbc4
9327 else
9328 phy_set_bb_reg(priv, offset[path][4], bMaskDWord, 0x0009ce7f); //0xbc0, 0xbc4
9329
9330 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
9331
9332 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_loop_pa, DP_BB_REG_NUM_loop_pa);
9333
9334 // fill PA model to page B1 registers
9335 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0x400000);
9336 for(index = 0; index < (DP_PA_MODEL_NUM/2); index++){ //path A = 0xb00, path B = 0xb60
9337 phy_set_bb_reg(priv, 0xb00+index*4+path*0x60, bMaskDWord,
9338 (PA_model_backup[path][index*2+1] << 16) | PA_model_backup[path][index*2]);
9339 }
9340 phy_set_bb_reg(priv, 0xe28, 0xffffff00, 0);
9341
9342 //one shot
9343 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x80044499); //0xb28, 0xb98
9344 phy_set_bb_reg(priv, offset[path][1], bMaskDWord, 0x00044499);
9345
9346 _PHY_DPK_polling(priv);
9347
9348#if 1
9349 //////////////////////////////////////////////////////
9350 // step 4: calculate gain loss caused by DP
9351 //////////////////////////////////////////////////////
9352 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x0029701f); //0xb00, 0xb70
9353 tmpReg = phy_query_bb_reg(priv, offset[path][5], bMaskDWord); //0xbe8, 0xbf8
9354
9355 power_I = (tmpReg >> 16);
9356 if(power_I & BIT(15))
9357 power_I |= bMaskLWord; ////////ZZZZZZZZZZZZZZZZZZ
9358
9359 power_Q = tmpReg & bMaskLWord;
9360 if(power_Q & BIT(15))
9361 power_Q |= bMaskHWord;
9362
9363 DPK_DEBUG("0x%x = 0x%x power_I = 0x%x power_Q = 0x%x\n", offset[path][5], tmpReg, power_I, power_Q);
9364
9365 tmpReg = power_I*power_I + power_Q*power_Q;
9366
9367 DPK_DEBUG("gain loss = 0x%x \n", tmpReg);
9368
9369 if(tmpReg < 26090)
9370 {
9371 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x01017098); //0xb00, 0xb70
9372 phy_set_bb_reg(priv, offset[path][8], bMaskDWord, 0x28080000); //0xb68, 0xb6c
9373
9374 for(i = 3; i < DP_RF_REG_NUM; i++)
9375 phy_set_rf_reg(priv, path, RF_REG[i], bMask20Bits, RF_backup[path][i]);
9376 priv->pshare->bDPKdone[path] = FALSE;
9377 //set original DPK bias table
9378 for(j = 0; j < DP_PA_BIAS_NUM; j++)
9379 phy_set_rf_reg(priv, path, RF_TXBIAS, bMask20Bits, RF_PA_BIAS[2][j]);
9380
9381 continue;
9382 }
9383 else
9384 {
9385 priv->pshare->bDPKdone[path] = TRUE;
9386 priv->pshare->bDPKstore = TRUE;
9387 }
9388
9389 for(i = 0; i < DP_GAIN_LOSS_BOUND_NUM; i++)
9390 {
9391#if DP_gain_loss == 1
9392 if(tmpReg > gain_loss_bound[i]/* || i == (DP_GAIN_LOSS_BOUND_NUM -1)*/)
9393#else
9394 if(tmpReg > gain_loss_bound[i] || i == (DP_GAIN_LOSS_BOUND_NUM -1))
9395#endif
9396 {
9397#if DP_gain_loss == 0
9398 if(i == 0)
9399 break;
9400
9401 index = OFDM_index[path] > i?OFDM_index[path]-i:0;
9402 if(index < OFDM_min_index_internalPA)
9403 index = OFDM_min_index_internalPA;
9404 phy_set_bb_reg(priv, offset[path][6], bMaskDWord, ofdm_swing_table[index]); //0xc80, 0xc88
9405 DPK_DEBUG("original index 0x%x gain_loss minus index 0x%x\n", priv->pshare->OFDM_index[0], i);
9406#endif
9407 break;
9408 }
9409 }
9410
9411#if DP_gain_loss == 1
9412
9413 DPK_DEBUG("gain_loss Compensated coefficient %d\n", gain_loss_coef[i]);
9414 coef = gain_loss_coef[i];
9415 GainLossIndex = i;
9416 priv->pshare->OFDM_min_index_internalPA_DPK[path] = GainLossIndex == 0?0:(GainLossIndex/2+GainLossIndex%2);
9417
9418 //read DP LUT value from register
9419 for(i = 0; i < DP_PA_MODEL_RUN_NUM; i++)
9420 {
9421 phy_set_bb_reg(priv, offset[path][2], bMaskDWord, 0x00297018+i); //0xb00, 0xb70
9422 for(index = 0; index < DP_PA_MODEL_PER_RUN_NUM; index++)
9423 {
9424 tmpReg = (i == 0 && index==0)?0x01000000:phy_query_bb_reg(priv, offset[path][3]+index*4, bMaskDWord); //0xbdc, 0xbec
9425
9426 gain_loss_backup[1][i*4+index] = (tmpReg >> 16); //I
9427 if(gain_loss_backup[1][i*4+index] & BIT(15))
9428 gain_loss_backup[1][i*4+index] |= bMaskHWord;
9429
9430 gain_loss_backup[0][i*4+index] = (tmpReg & bMaskLWord); //Q
9431 if(gain_loss_backup[0][i*4+index] & BIT(15))
9432 gain_loss_backup[0][i*4+index] |= bMaskHWord;
9433 DPK_DEBUG("==>DP LUT index %d value 0x%x() I = 0x%x, Q = 0x%x\n", i*4+index, tmpReg, gain_loss_backup[1][i*4+index], gain_loss_backup[0][i*4+index]);
9434
9435 //gain * LUT
9436 for(j = 0; j < 2; j++)
9437 {
9438 // RTPRINT(FINIT, INIT_IQK, ("==>0DP LUT sram %s index %d value %d()\n", j == 0?"Q":"I", i*4+index, gain_loss_backup[j][i*4+index]));
9439
9440 gain_loss_backup[j][i*4+index] = (gain_loss_backup[j][i*4+index] * coef) / (int)(512);
9441 // RTPRINT(FINIT, INIT_IQK, ("==>1DP LUT sram %s index %d value 0x%x()\n", j == 0?"Q":"I", i*4+index, gain_loss_backup[j][i*4+index]));
9442
9443 gain_loss_backup[j][i*4+index] = gain_loss_backup[j][i*4+index] >= (int)(512)?(int)(511):gain_loss_backup[j][i*4+index] < (int)(-512)?(int)(-512):gain_loss_backup[j][i*4+index];
9444 // RTPRINT(FINIT, INIT_IQK, ("==>2DP LUT sram %s index %d value 0x%x()\n", j == 0?"Q":"I", i*4+index, gain_loss_backup[j][i*4+index]));
9445
9446 gain_loss_backup[j][i*4+index] = gain_loss_backup[j][i*4+index] >> 2;
9447 // RTPRINT(FINIT, INIT_IQK, ("==>3DP LUT sram %s index %d value 0x%x()\n", j == 0?"Q":"I", i*4+index, gain_loss_backup[j][i*4+index]));
9448
9449 }
9450 tmpReg = ((gain_loss_backup[1][i*4+index] & bMaskByte0) << 8 ) | ((gain_loss_backup[0][i*4+index] & bMaskByte0));
9451 gain_loss_backup[0][i*4+index] = tmpReg & bMaskLWord;
9452 DPK_DEBUG("==>DP LUT sram index %d value 0x%x()\n", i*4+index, tmpReg);
9453 }
9454 }
9455
9456 //write DP LUT into sram
9457 for(i = 0; i < DP_PA_MODEL_NUM; i++)
9458 {
9459 value32 = (path==RF_PATH_A?((i%2 == 0)?0x01000000:0x02000000):
9460 ((i%2 == 0)?0x04000000:0x08000000)) |
9461 gain_loss_backup[0][(DP_PA_MODEL_NUM-1)-i] |( (i/2) << 16);
9462 DPK_DEBUG("0xb2c value = 0x%x\n", value32);
9463 phy_set_bb_reg(priv, 0xb2c , bMaskDWord, value32);
9464 }
9465// phy_set_bb_reg(priv, 0xb2c , bMaskDWord, 0x00000000);
9466
9467#endif
9468
9469#endif
9470
9471 ///////////////////////////////////////////////////////////////
9472 // step 5: Enable Digital Predistortion
9473 ///////////////////////////////////////////////////////////////
9474 // LUT from sram
9475#if DP_gain_loss == 1
9476 {
9477 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_loop_dp, DP_BB_REG_NUM_loop_pa);
9478
9479 // pwsf boundary
9480 phy_set_bb_reg(priv, offset[path][7], bMaskDWord, 0x000fffff); //0xb30, 0xba0
9481
9482 // write pwsf to sram
9483 //find tx_index index value
9484 SramIndex = 24; //restore default value
9485 SramIndex -= GainLossIndex;
9486 if(bPlus3db)
9487 SramIndex += 3*4;
9488
9489 SramIndex = SramIndex >= DP_SRAM_NUM_db?DP_SRAM_NUM_db-1:(SramIndex<0?0:SramIndex);
9490
9491 DPK_DEBUG("tx_index = 0x%x, sram value 0x%x gainloss index %d bPlus3db %d\n", tx_index, Sram_db_settings[SramIndex], GainLossIndex, bPlus3db);
9492
9493 index = 0x1f - tx_index;
9494 if(SramIndex >= index*4)
9495 {
9496 index = SramIndex - index*4;
9497 index_repeat = -2;
9498 SramIndex = -2;
9499 }
9500 else
9501 {
9502 index_repeat = index - SramIndex/4;
9503 SramIndex %= 4;
9504 index = 0;
9505 }
9506
9507 index = index >= DP_SRAM_NUM_db?DP_SRAM_NUM_db-1:index;
9508 if(index_repeat == 1)
9509 index_1 = SramIndex;
9510 else
9511 index_1 = index < (DP_SRAM_NUM_db-1)?(index_repeat==-2?index+1*4:index):index;
9512
9513 DPK_DEBUG("0x1f value = 0x%x, index 0x%x repeat %d SramIndex %d\n", Sram_db_settings[index], index, index_repeat, SramIndex);
9514
9515 for(i = 0; i < DP_SRAM_NUM; i++)
9516 {
9517 value32 = (path==RF_PATH_A?0x10000000:0x20000000) | (i << 16) |
9518 (Sram_db_settings[index_1] << 8 )| Sram_db_settings[index];
9519 DPK_DEBUG("0xb2c value = 0x%x\n", value32);
9520
9521 phy_set_bb_reg(priv, 0xb2c , bMaskDWord, value32);
9522 if(index_repeat >= 0)
9523 index_repeat -= 2;
9524 else if(index_repeat == -1)
9525 index_repeat = -2;
9526
9527 if((index < (DP_SRAM_NUM_db-1)-1))
9528 {
9529 if(index_repeat == -2)
9530 {
9531 index+=2*4;
9532 index_1 = index < (DP_SRAM_NUM_db-1)?index+1*4:index;
9533 }
9534 if(index_repeat == 0)
9535 {
9536 index = SramIndex;
9537 index_1 = index < (DP_SRAM_NUM_db-1)?index+1*4:index;
9538 }
9539 else if(index_repeat == 1)
9540 {
9541 index_1 = SramIndex;
9542 }
9543 else if(index_repeat == -1)
9544 {
9545 index = index_1+1*4;
9546 index_1 = index < (DP_SRAM_NUM_db-1)?index+1*4:index;
9547 }
9548 }
9549 else
9550 {
9551 index = index_1 = (DP_SRAM_NUM_db-1);
9552 }
9553
9554 index = index < DP_SRAM_NUM_db?index:DP_SRAM_NUM_db-1;
9555 index_1 = index_1 < DP_SRAM_NUM_db?index_1:DP_SRAM_NUM_db-1;
9556 }
9557 }
9558#else
9559 if(!SkipStep5)
9560 {
9561 _PHY_SetADDARegisters(priv, BB_REG_loop[path], BB_settings_loop_dp, DP_BB_REG_NUM_loop_pa);
9562
9563 // pwsf boundary
9564 phy_set_bb_reg(priv, offset[path][7], bMaskDWord, 0x000fffff); //0xb30, 0xba0
9565
9566 // write pwsf to sram
9567 //find RF0x1f index value
9568 if(bPlus3db)
9569 tx_index += 3;
9570
9571// tx_index = 0x21;
9572
9573 index = 0x1f - tx_index;
9574 if(index_for_zero_db >= index)
9575 {
9576 index = index_for_zero_db - index;
9577 index_repeat = -2;
9578 }
9579 else
9580 {
9581 index_repeat = index - index_for_zero_db;
9582 index = 0;
9583 }
9584
9585 index = index >= DP_SRAM_NUM_db?DP_SRAM_NUM_db-1:index;
9586 index_1 = index < (DP_SRAM_NUM_db-1)?(index_repeat==-2?index+1:index):index;
9587
9588 DPK_DEBUG("0x1f value = 0x%x, index 0x%x repeat %d\n", Sram_db_settings[index], index, index_repeat);
9589
9590 for(i = 0; i < DP_SRAM_NUM; i++)
9591 {
9592 value32 = (path==RF_PATH_A?0x10000000:0x20000000) | (i << 16) |
9593 (Sram_db_settings[index_1] << 8 )| Sram_db_settings[index];
9594 DPK_DEBUG("0xb2c value = 0x%x\n", value32);
9595
9596 phy_set_bb_reg(priv, 0xb2c , bMaskDWord, value32);
9597 if(index_repeat >= 0)
9598 index_repeat -= 2;
9599 else if(index_repeat == -1)
9600 index_repeat = -2;
9601
9602 if((index < (DP_SRAM_NUM_db-1)-1))
9603 {
9604 if(index_repeat == -2)
9605 {
9606 index += 2;
9607 index_1 = index < (DP_SRAM_NUM_db-1)?index+1:index;
9608 }
9609 if(index_repeat == 0)
9610 {
9611 index_1 = index < (DP_SRAM_NUM_db-1)?index+1:index;
9612 }
9613 else if(index_repeat == -1)
9614 {
9615 index++;
9616 index_1 = index < (DP_SRAM_NUM_db-1)?index+1:index;
9617 }
9618 }
9619 else
9620 {
9621 index = index_1 = (DP_SRAM_NUM_db-1);
9622 }
9623 }
9624 }
9625#endif
9626 }
9627
9628 //reload RF default value
9629 for(path = 0; path<path_num; path++){
9630 for( i = 2 ; i < 3 ; i++){
9631 phy_set_rf_reg(priv, path, RF_REG[i], bMask20Bits, RF_backup[path][i]);
9632 }
9633 }
9634
9635 //Reload standby mode default value (if path B excute DPK)
9636 if(is2T && priv->pshare->phw->InternalPA5G[RF_PATH_B])
9637 {
9638 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE1, bMask20Bits, 0x1000f);
9639 phy_set_rf_reg(priv, RF_PATH_A, RF_MODE2, bMask20Bits, 0x60101);
9640 }
9641
9642 //reload BB default value
9643 for(index=0; index<DP_BB_REG_NUM; index++)
9644 phy_set_bb_reg(priv, BB_REG[index], bMaskDWord, BB_backup[index]);
9645
9646 //external LNA on
9647 phy_set_bb_reg(priv, rBndA, 0xF00000, 0x00);
9648
9649 if(is2T)
9650 phy_set_bb_reg(priv, rBndB, 0xF00000, 0x00);
9651
9652 //Reload path A BB default value
9653 _phy_reload_adda_registers(priv, BB_REG_A, BB_backup_A, DP_BB_REG_NUM_A);
9654
9655
9656#if 1 //Return to Rx mode after dpk
9657 //printk("BB_REG_A[9] 0x%x BB_backup_A[9] 0x%x\n\n", BB_REG_A[9], BB_backup_A[9]);
9658 phy_set_bb_reg(priv, BB_REG_A[9], bMaskByte0, 0x50);
9659 phy_set_bb_reg(priv, BB_REG_A[9], bMaskDWord, BB_backup_A[9]);
9660#endif
9661
9662 //Reload path B default value
9663 if(is2T)
9664 _phy_reload_adda_registers(priv, BB_REG_B, BB_backup_B, DP_BB_REG_NUM_B);
9665
9666#if 1 //Return to Rx mode after dpk
9667 //printk("BB_REG_B[8] 0x%x BB_backup_B[8] 0x%x\n\n", BB_REG_B[8], BB_backup_B[8]);
9668 phy_set_bb_reg(priv, BB_REG_B[8], bMaskByte0, 0x50);
9669 phy_set_bb_reg(priv, BB_REG_B[8], bMaskDWord, BB_backup_B[8]);
9670#endif
9671
9672 //reload AFE default value
9673 _phy_reload_adda_registers(priv, AFE_REG, AFE_backup, IQK_ADDA_REG_NUM);
9674
9675 //reload MAC default value
9676 _phy_reload_mac_registers(priv, MAC_REG, MAC_backup);
9677
9678 priv->pshare->bDPKworking = FALSE;
9679
9680 DPK_DEBUG("<==_PHY_DigitalPredistortion()\n");
9681}
9682
9683#endif
9684#endif
9685
9686#endif