spi/topcliff-pch: Fix Kconfig dependencies
[cascardo/linux.git] / drivers / staging / rtl8187se / r8180_rtl8225z2.c
1 /*
2  * This is part of the rtl8180-sa2400 driver
3  * released under the GPL (See file COPYING for details).
4  * Copyright (c) 2005 Andrea Merello <andrea.merello@gmail.com>
5  *
6  * This files contains programming code for the rtl8225
7  * radio frontend.
8  *
9  * *Many* thanks to Realtek Corp. for their great support!
10  */
11
12 #include "r8180_hw.h"
13 #include "r8180_rtl8225.h"
14 #include "r8180_93cx6.h"
15
16 #include "ieee80211/dot11d.h"
17
18 static void write_rtl8225(struct net_device *dev, u8 adr, u16 data)
19 {
20         int i;
21         u16 out, select;
22         u8 bit;
23         u32 bangdata = (data << 4) | (adr & 0xf);
24
25         out = read_nic_word(dev, RFPinsOutput) & 0xfff3;
26
27         write_nic_word(dev, RFPinsEnable,
28                 (read_nic_word(dev, RFPinsEnable) | 0x7));
29
30         select = read_nic_word(dev, RFPinsSelect);
31
32         write_nic_word(dev, RFPinsSelect, select | 0x7 |
33                        SW_CONTROL_GPIO);
34
35         force_pci_posting(dev);
36         udelay(10);
37
38         write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
39
40         force_pci_posting(dev);
41         udelay(2);
42
43         write_nic_word(dev, RFPinsOutput, out);
44
45         force_pci_posting(dev);
46         udelay(10);
47
48         for (i = 15; i >= 0; i--) {
49                 bit = (bangdata & (1 << i)) >> i;
50
51                 write_nic_word(dev, RFPinsOutput, bit | out);
52
53                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
54                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
55
56                 i--;
57                 bit = (bangdata & (1 << i)) >> i;
58
59                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
60                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
61
62                 write_nic_word(dev, RFPinsOutput, bit | out);
63
64         }
65
66         write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
67
68         force_pci_posting(dev);
69         udelay(10);
70
71         write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
72
73         write_nic_word(dev, RFPinsSelect, select | SW_CONTROL_GPIO);
74
75         rtl8185_rf_pins_enable(dev);
76 }
77
78 static const u8 rtl8225_agc[] = {
79         0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e,
80         0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96,
81         0x95, 0x94, 0x93, 0x92, 0x91, 0x90, 0x8f, 0x8e,
82         0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86,
83         0x85, 0x84, 0x83, 0x82, 0x81, 0x80, 0x3f, 0x3e,
84         0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38, 0x37, 0x36,
85         0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f, 0x2e,
86         0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26,
87         0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e,
88         0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16,
89         0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e,
90         0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06,
91         0x05, 0x04, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01,
92         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
93         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
94         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
95 };
96
97 static const u32 rtl8225_chan[] = {
98         0,
99         0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380,
100         0x0400, 0x0480, 0x0500, 0x0580, 0x0600, 0x0680, 0x074A,
101 };
102
103 static const u8 rtl8225z2_gain_bg[] = {
104         0x23, 0x15, 0xa5, /* -82-1dBm */
105         0x23, 0x15, 0xb5, /* -82-2dBm */
106         0x23, 0x15, 0xc5, /* -82-3dBm */
107         0x33, 0x15, 0xc5, /* -78dBm */
108         0x43, 0x15, 0xc5, /* -74dBm */
109         0x53, 0x15, 0xc5, /* -70dBm */
110         0x63, 0x15, 0xc5, /* -66dBm */
111 };
112
113 static const u8 rtl8225z2_gain_a[] = {
114         0x13, 0x27, 0x5a, /* -82dBm */
115         0x23, 0x23, 0x58, /* -82dBm */
116         0x33, 0x1f, 0x56, /* -82dBm */
117         0x43, 0x1b, 0x54, /* -78dBm */
118         0x53, 0x17, 0x51, /* -74dBm */
119         0x63, 0x24, 0x4f, /* -70dBm */
120         0x73, 0x0f, 0x4c, /* -66dBm */
121 };
122
123 static const u16 rtl8225z2_rxgain[] = {
124         0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
125         0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
126         0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
127         0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
128         0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
129         0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
130         0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
131         0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
132         0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
133         0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
134         0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03b0, 0x03b1, 0x03b2, 0x03b3,
135         0x03b4, 0x03b5, 0x03b8, 0x03b9, 0x03ba, 0x03bb
136
137 };
138
139 static void rtl8225z2_set_gain(struct net_device *dev, short gain)
140 {
141         const u8 *rtl8225_gain;
142         struct r8180_priv *priv = ieee80211_priv(dev);
143         u8 mode = priv->ieee80211->mode;
144
145         if (mode == IEEE_B || mode == IEEE_G)
146                 rtl8225_gain = rtl8225z2_gain_bg;
147         else
148                 rtl8225_gain = rtl8225z2_gain_a;
149
150         write_phy_ofdm(dev, 0x0b, rtl8225_gain[gain * 3]);
151         write_phy_ofdm(dev, 0x1b, rtl8225_gain[gain * 3 + 1]);
152         write_phy_ofdm(dev, 0x1d, rtl8225_gain[gain * 3 + 2]);
153         write_phy_ofdm(dev, 0x21, 0x37);
154 }
155
156 static u32 read_rtl8225(struct net_device *dev, u8 adr)
157 {
158         u32 data2Write = ((u32)(adr & 0x1f)) << 27;
159         u32 dataRead;
160         u32 mask;
161         u16 oval, oval2, oval3, tmp;
162         int i;
163         short bit, rw;
164         u8 wLength = 6;
165         u8 rLength = 12;
166         u8 low2high = 0;
167
168         oval = read_nic_word(dev, RFPinsOutput);
169         oval2 = read_nic_word(dev, RFPinsEnable);
170         oval3 = read_nic_word(dev, RFPinsSelect);
171
172         write_nic_word(dev, RFPinsEnable, (oval2|0xf));
173         write_nic_word(dev, RFPinsSelect, (oval3|0xf));
174
175         dataRead = 0;
176
177         oval &= ~0xf;
178
179         write_nic_word(dev, RFPinsOutput, oval | BB_HOST_BANG_EN);
180         udelay(4);
181
182         write_nic_word(dev, RFPinsOutput, oval);
183         udelay(5);
184
185         rw = 0;
186
187         mask = (low2high) ? 0x01 : (((u32)0x01)<<(32-1));
188
189         for (i = 0; i < wLength/2; i++) {
190                 bit = ((data2Write&mask) != 0) ? 1 : 0;
191                 write_nic_word(dev, RFPinsOutput, bit | oval | rw);
192                 udelay(1);
193
194                 write_nic_word(dev, RFPinsOutput,
195                                 bit | oval | BB_HOST_BANG_CLK | rw);
196                 udelay(2);
197                 write_nic_word(dev, RFPinsOutput,
198                                 bit | oval | BB_HOST_BANG_CLK | rw);
199                 udelay(2);
200
201                 mask = (low2high) ? (mask<<1) : (mask>>1);
202
203                 if (i == 2) {
204                         rw = BB_HOST_BANG_RW;
205                         write_nic_word(dev, RFPinsOutput,
206                                         bit | oval | BB_HOST_BANG_CLK | rw);
207                         udelay(2);
208                         write_nic_word(dev, RFPinsOutput, bit | oval | rw);
209                         udelay(2);
210                         break;
211                 }
212
213                 bit = ((data2Write&mask) != 0) ? 1 : 0;
214
215                 write_nic_word(dev, RFPinsOutput,
216                                 oval | bit | rw | BB_HOST_BANG_CLK);
217                 udelay(2);
218                 write_nic_word(dev, RFPinsOutput,
219                                 oval | bit | rw | BB_HOST_BANG_CLK);
220                 udelay(2);
221
222                 write_nic_word(dev, RFPinsOutput, oval | bit | rw);
223                 udelay(1);
224
225                 mask = (low2high) ? (mask<<1) : (mask>>1);
226         }
227
228         write_nic_word(dev, RFPinsOutput, rw|oval);
229         udelay(2);
230         mask = (low2high) ? 0x01 : (((u32)0x01) << (12-1));
231
232         /*
233          * We must set data pin to HW controlled, otherwise RF can't driver it
234          * and value RF register won't be able to read back properly.
235          */
236         write_nic_word(dev, RFPinsEnable, (oval2 & (~0x01)));
237
238         for (i = 0; i < rLength; i++) {
239                 write_nic_word(dev, RFPinsOutput, rw|oval); udelay(1);
240
241                 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK);
242                 udelay(2);
243                 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK);
244                 udelay(2);
245                 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK);
246                 udelay(2);
247                 tmp = read_nic_word(dev, RFPinsInput);
248
249                 dataRead |= (tmp & BB_HOST_BANG_CLK ? mask : 0);
250
251                 write_nic_word(dev, RFPinsOutput, (rw|oval)); udelay(2);
252
253                 mask = (low2high) ? (mask<<1) : (mask>>1);
254         }
255
256         write_nic_word(dev, RFPinsOutput,
257                         BB_HOST_BANG_EN | BB_HOST_BANG_RW | oval);
258         udelay(2);
259
260         write_nic_word(dev, RFPinsEnable, oval2);
261         write_nic_word(dev, RFPinsSelect, oval3); /* Set To SW Switch */
262         write_nic_word(dev, RFPinsOutput, 0x3a0);
263
264         return dataRead;
265 }
266
267 void rtl8225z2_rf_close(struct net_device *dev)
268 {
269         RF_WriteReg(dev, 0x4, 0x1f);
270
271         force_pci_posting(dev);
272         mdelay(1);
273
274         rtl8180_set_anaparam(dev, RTL8225z2_ANAPARAM_OFF);
275         rtl8185_set_anaparam2(dev, RTL8225z2_ANAPARAM2_OFF);
276 }
277
278 /*
279  * Map dBm into Tx power index according to current HW model, for example,
280  * RF and PA, and current wireless mode.
281  */
282 static s8 DbmToTxPwrIdx(struct r8180_priv *priv,
283                         enum wireless_mode mode, s32 PowerInDbm)
284 {
285         bool bUseDefault = true;
286         s8 TxPwrIdx = 0;
287
288         /*
289          * OFDM Power in dBm = Index * 0.5 + 0
290          * CCK Power in dBm = Index * 0.25 + 13
291          */
292         s32 tmp = 0;
293
294         if (mode == WIRELESS_MODE_G) {
295                 bUseDefault = false;
296                 tmp = (2 * PowerInDbm);
297
298                 if (tmp < 0)
299                         TxPwrIdx = 0;
300                 else if (tmp > 40) /* 40 means 20 dBm. */
301                         TxPwrIdx = 40;
302                 else
303                         TxPwrIdx = (s8)tmp;
304         } else if (mode == WIRELESS_MODE_B) {
305                 bUseDefault = false;
306                 tmp = (4 * PowerInDbm) - 52;
307
308                 if (tmp < 0)
309                         TxPwrIdx = 0;
310                 else if (tmp > 28) /* 28 means 20 dBm. */
311                         TxPwrIdx = 28;
312                 else
313                         TxPwrIdx = (s8)tmp;
314         }
315
316         /*
317          * TRUE if we want to use a default implementation.
318          * We shall set it to FALSE when we have exact translation formula
319          * for target IC. 070622, by rcnjko.
320          */
321         if (bUseDefault) {
322                 if (PowerInDbm < 0)
323                         TxPwrIdx = 0;
324                 else if (PowerInDbm > 35)
325                         TxPwrIdx = 35;
326                 else
327                         TxPwrIdx = (u8)PowerInDbm;
328         }
329
330         return TxPwrIdx;
331 }
332
333 void rtl8225z2_SetTXPowerLevel(struct net_device *dev, short ch)
334 {
335         struct r8180_priv *priv = ieee80211_priv(dev);
336         u8 max_cck_power_level;
337         u8 max_ofdm_power_level;
338         u8 min_ofdm_power_level;
339         char cck_power_level = (char)(0xff & priv->chtxpwr[ch]);
340         char ofdm_power_level = (char)(0xff & priv->chtxpwr_ofdm[ch]);
341
342         if (IS_DOT11D_ENABLE(priv->ieee80211) &&
343             IS_DOT11D_STATE_DONE(priv->ieee80211)) {
344                 u8 MaxTxPwrInDbm = DOT11D_GetMaxTxPwrInDbm(priv->ieee80211, ch);
345                 u8 CckMaxPwrIdx = DbmToTxPwrIdx(priv, WIRELESS_MODE_B,
346                                                         MaxTxPwrInDbm);
347                 u8 OfdmMaxPwrIdx = DbmToTxPwrIdx(priv, WIRELESS_MODE_G,
348                                                         MaxTxPwrInDbm);
349
350                 if (cck_power_level > CckMaxPwrIdx)
351                         cck_power_level = CckMaxPwrIdx;
352                 if (ofdm_power_level > OfdmMaxPwrIdx)
353                         ofdm_power_level = OfdmMaxPwrIdx;
354         }
355
356         max_cck_power_level = 15;
357         max_ofdm_power_level = 25;
358         min_ofdm_power_level = 10;
359
360         if (cck_power_level > 35)
361                 cck_power_level = 35;
362
363         write_nic_byte(dev, CCK_TXAGC, cck_power_level);
364         force_pci_posting(dev);
365         mdelay(1);
366
367         if (ofdm_power_level > 35)
368                 ofdm_power_level = 35;
369
370         if (priv->up == 0) {
371                 write_phy_ofdm(dev, 2, 0x42);
372                 write_phy_ofdm(dev, 5, 0x00);
373                 write_phy_ofdm(dev, 6, 0x40);
374                 write_phy_ofdm(dev, 7, 0x00);
375                 write_phy_ofdm(dev, 8, 0x40);
376         }
377
378         write_nic_byte(dev, OFDM_TXAGC, ofdm_power_level);
379
380         if (ofdm_power_level <= 11) {
381                 write_phy_ofdm(dev, 0x07, 0x5c);
382                 write_phy_ofdm(dev, 0x09, 0x5c);
383         }
384
385         if (ofdm_power_level <= 17) {
386                 write_phy_ofdm(dev, 0x07, 0x54);
387                 write_phy_ofdm(dev, 0x09, 0x54);
388         } else {
389                 write_phy_ofdm(dev, 0x07, 0x50);
390                 write_phy_ofdm(dev, 0x09, 0x50);
391         }
392
393         force_pci_posting(dev);
394         mdelay(1);
395 }
396
397 void rtl8225z2_rf_set_chan(struct net_device *dev, short ch)
398 {
399         rtl8225z2_SetTXPowerLevel(dev, ch);
400
401         RF_WriteReg(dev, 0x7, rtl8225_chan[ch]);
402
403         if ((RF_ReadReg(dev, 0x7) & 0x0F80) != rtl8225_chan[ch])
404                 RF_WriteReg(dev, 0x7, rtl8225_chan[ch]);
405
406         mdelay(1);
407
408         force_pci_posting(dev);
409         mdelay(10);
410 }
411
412 static void rtl8225_host_pci_init(struct net_device *dev)
413 {
414         write_nic_word(dev, RFPinsOutput, 0x480);
415
416         rtl8185_rf_pins_enable(dev);
417
418         write_nic_word(dev, RFPinsSelect, 0x88 | SW_CONTROL_GPIO);
419
420         write_nic_byte(dev, GP_ENABLE, 0);
421
422         force_pci_posting(dev);
423         mdelay(200);
424
425         /* bit 6 is for RF on/off detection */
426         write_nic_word(dev, GP_ENABLE, 0xff & (~(1 << 6)));
427 }
428
429 void rtl8225z2_rf_init(struct net_device *dev)
430 {
431         struct r8180_priv *priv = ieee80211_priv(dev);
432         int i;
433         short channel = 1;
434         u16 brsr;
435         u32 data;
436
437         priv->chan = channel;
438
439         rtl8225_host_pci_init(dev);
440
441         write_nic_dword(dev, RF_TIMING, 0x000a8008);
442
443         brsr = read_nic_word(dev, BRSR);
444
445         write_nic_word(dev, BRSR, 0xffff);
446
447         write_nic_dword(dev, RF_PARA, 0x100044);
448
449         rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
450         write_nic_byte(dev, CONFIG3, 0x44);
451         rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
452
453         rtl8185_rf_pins_enable(dev);
454
455         write_rtl8225(dev, 0x0, 0x2bf); mdelay(1);
456         write_rtl8225(dev, 0x1, 0xee0); mdelay(1);
457         write_rtl8225(dev, 0x2, 0x44d); mdelay(1);
458         write_rtl8225(dev, 0x3, 0x441); mdelay(1);
459         write_rtl8225(dev, 0x4, 0x8c3); mdelay(1);
460         write_rtl8225(dev, 0x5, 0xc72); mdelay(1);
461         write_rtl8225(dev, 0x6, 0xe6);  mdelay(1);
462         write_rtl8225(dev, 0x7, rtl8225_chan[channel]);  mdelay(1);
463         write_rtl8225(dev, 0x8, 0x3f);  mdelay(1);
464         write_rtl8225(dev, 0x9, 0x335); mdelay(1);
465         write_rtl8225(dev, 0xa, 0x9d4); mdelay(1);
466         write_rtl8225(dev, 0xb, 0x7bb); mdelay(1);
467         write_rtl8225(dev, 0xc, 0x850); mdelay(1);
468         write_rtl8225(dev, 0xd, 0xcdf); mdelay(1);
469         write_rtl8225(dev, 0xe, 0x2b);  mdelay(1);
470         write_rtl8225(dev, 0xf, 0x114);
471
472         mdelay(100);
473
474         write_rtl8225(dev, 0x0, 0x1b7);
475
476         for (i = 0; i < ARRAY_SIZE(rtl8225z2_rxgain); i++) {
477                 write_rtl8225(dev, 0x1, i + 1);
478                 write_rtl8225(dev, 0x2, rtl8225z2_rxgain[i]);
479         }
480
481         write_rtl8225(dev, 0x3, 0x80);
482         write_rtl8225(dev, 0x5, 0x4);
483
484         write_rtl8225(dev, 0x0, 0xb7);
485
486         write_rtl8225(dev, 0x2, 0xc4d);
487
488         /* FIXME!! rtl8187 we have to check if calibrarion
489          * is successful and eventually cal. again (repeat
490          * the two write on reg 2)
491          */
492         data = read_rtl8225(dev, 6);
493         if (!(data & 0x00000080)) {
494                 write_rtl8225(dev, 0x02, 0x0c4d);
495                 force_pci_posting(dev); mdelay(200);
496                 write_rtl8225(dev, 0x02, 0x044d);
497                 force_pci_posting(dev); mdelay(100);
498                 data = read_rtl8225(dev, 6);
499                 if (!(data & 0x00000080))
500                         DMESGW("RF Calibration Failed!!!!\n");
501         }
502
503         mdelay(200);
504
505         write_rtl8225(dev, 0x0, 0x2bf);
506
507         for (i = 0; i < ARRAY_SIZE(rtl8225_agc); i++) {
508                 write_phy_ofdm(dev, 0xb, rtl8225_agc[i]);
509                 mdelay(1);
510
511                 /* enable writing AGC table */
512                 write_phy_ofdm(dev, 0xa, i + 0x80);
513                 mdelay(1);
514         }
515
516         force_pci_posting(dev);
517         mdelay(1);
518
519         write_phy_ofdm(dev, 0x00, 0x01); mdelay(1);
520         write_phy_ofdm(dev, 0x01, 0x02); mdelay(1);
521         write_phy_ofdm(dev, 0x02, 0x62); mdelay(1);
522         write_phy_ofdm(dev, 0x03, 0x00); mdelay(1);
523         write_phy_ofdm(dev, 0x04, 0x00); mdelay(1);
524         write_phy_ofdm(dev, 0x05, 0x00); mdelay(1);
525         write_phy_ofdm(dev, 0x06, 0x40); mdelay(1);
526         write_phy_ofdm(dev, 0x07, 0x00); mdelay(1);
527         write_phy_ofdm(dev, 0x08, 0x40); mdelay(1);
528         write_phy_ofdm(dev, 0x09, 0xfe); mdelay(1);
529         write_phy_ofdm(dev, 0x0a, 0x08); mdelay(1);
530         write_phy_ofdm(dev, 0x0b, 0x80); mdelay(1);
531         write_phy_ofdm(dev, 0x0c, 0x01); mdelay(1);
532         write_phy_ofdm(dev, 0x0d, 0x43);
533         write_phy_ofdm(dev, 0x0e, 0xd3); mdelay(1);
534         write_phy_ofdm(dev, 0x0f, 0x38); mdelay(1);
535         write_phy_ofdm(dev, 0x10, 0x84); mdelay(1);
536         write_phy_ofdm(dev, 0x11, 0x07); mdelay(1);
537         write_phy_ofdm(dev, 0x12, 0x20); mdelay(1);
538         write_phy_ofdm(dev, 0x13, 0x20); mdelay(1);
539         write_phy_ofdm(dev, 0x14, 0x00); mdelay(1);
540         write_phy_ofdm(dev, 0x15, 0x40); mdelay(1);
541         write_phy_ofdm(dev, 0x16, 0x00); mdelay(1);
542         write_phy_ofdm(dev, 0x17, 0x40); mdelay(1);
543         write_phy_ofdm(dev, 0x18, 0xef); mdelay(1);
544         write_phy_ofdm(dev, 0x19, 0x19); mdelay(1);
545         write_phy_ofdm(dev, 0x1a, 0x20); mdelay(1);
546         write_phy_ofdm(dev, 0x1b, 0x15); mdelay(1);
547         write_phy_ofdm(dev, 0x1c, 0x04); mdelay(1);
548         write_phy_ofdm(dev, 0x1d, 0xc5); mdelay(1);
549         write_phy_ofdm(dev, 0x1e, 0x95); mdelay(1);
550         write_phy_ofdm(dev, 0x1f, 0x75); mdelay(1);
551         write_phy_ofdm(dev, 0x20, 0x1f); mdelay(1);
552         write_phy_ofdm(dev, 0x21, 0x17); mdelay(1);
553         write_phy_ofdm(dev, 0x22, 0x16); mdelay(1);
554         write_phy_ofdm(dev, 0x23, 0x80); mdelay(1); /* FIXME maybe not needed */
555         write_phy_ofdm(dev, 0x24, 0x46); mdelay(1);
556         write_phy_ofdm(dev, 0x25, 0x00); mdelay(1);
557         write_phy_ofdm(dev, 0x26, 0x90); mdelay(1);
558         write_phy_ofdm(dev, 0x27, 0x88); mdelay(1);
559
560         rtl8225z2_set_gain(dev, 4);
561
562         write_phy_cck(dev, 0x0, 0x98); mdelay(1);
563         write_phy_cck(dev, 0x3, 0x20); mdelay(1);
564         write_phy_cck(dev, 0x4, 0x7e); mdelay(1);
565         write_phy_cck(dev, 0x5, 0x12); mdelay(1);
566         write_phy_cck(dev, 0x6, 0xfc); mdelay(1);
567         write_phy_cck(dev, 0x7, 0x78); mdelay(1);
568         write_phy_cck(dev, 0x8, 0x2e); mdelay(1);
569         write_phy_cck(dev, 0x10, 0x93); mdelay(1);
570         write_phy_cck(dev, 0x11, 0x88); mdelay(1);
571         write_phy_cck(dev, 0x12, 0x47); mdelay(1);
572         write_phy_cck(dev, 0x13, 0xd0);
573         write_phy_cck(dev, 0x19, 0x00);
574         write_phy_cck(dev, 0x1a, 0xa0);
575         write_phy_cck(dev, 0x1b, 0x08);
576         write_phy_cck(dev, 0x40, 0x86); /* CCK Carrier Sense Threshold */
577         write_phy_cck(dev, 0x41, 0x8d); mdelay(1);
578         write_phy_cck(dev, 0x42, 0x15); mdelay(1);
579         write_phy_cck(dev, 0x43, 0x18); mdelay(1);
580         write_phy_cck(dev, 0x44, 0x36); mdelay(1);
581         write_phy_cck(dev, 0x45, 0x35); mdelay(1);
582         write_phy_cck(dev, 0x46, 0x2e); mdelay(1);
583         write_phy_cck(dev, 0x47, 0x25); mdelay(1);
584         write_phy_cck(dev, 0x48, 0x1c); mdelay(1);
585         write_phy_cck(dev, 0x49, 0x12); mdelay(1);
586         write_phy_cck(dev, 0x4a, 0x09); mdelay(1);
587         write_phy_cck(dev, 0x4b, 0x04); mdelay(1);
588         write_phy_cck(dev, 0x4c, 0x05); mdelay(1);
589
590         write_nic_byte(dev, 0x5b, 0x0d); mdelay(1);
591
592         rtl8225z2_SetTXPowerLevel(dev, channel);
593
594         /* RX antenna default to A */
595         write_phy_cck(dev, 0x11, 0x9b); mdelay(1);              /* B: 0xDB */
596         write_phy_ofdm(dev, 0x26, 0x90); mdelay(1);             /* B: 0x10 */
597
598         rtl8185_tx_antenna(dev, 0x03);                          /* B: 0x00 */
599
600         /* switch to high-speed 3-wire
601          * last digit. 2 for both cck and ofdm
602          */
603         write_nic_dword(dev, 0x94, 0x15c00002);
604         rtl8185_rf_pins_enable(dev);
605
606         rtl8225z2_rf_set_chan(dev, priv->chan);
607 }
608
609 #define MAX_DOZE_WAITING_TIMES_85B              20
610 #define MAX_POLLING_24F_TIMES_87SE              10
611 #define LPS_MAX_SLEEP_WAITING_TIMES_87SE        5
612
613 bool SetZebraRFPowerState8185(struct net_device *dev,
614                               enum rt_rf_power_state eRFPowerState)
615 {
616         struct r8180_priv *priv = ieee80211_priv(dev);
617         u8                      btCR9346, btConfig3;
618         bool bActionAllowed = true, bTurnOffBB = true;
619         u8                      u1bTmp;
620         int                     i;
621         bool            bResult = true;
622         u8                      QueueID;
623
624         if (priv->SetRFPowerStateInProgress == true)
625                 return false;
626
627         priv->SetRFPowerStateInProgress = true;
628
629         btCR9346 = read_nic_byte(dev, CR9346);
630         write_nic_byte(dev, CR9346, (btCR9346 | 0xC0));
631
632         btConfig3 = read_nic_byte(dev, CONFIG3);
633         write_nic_byte(dev, CONFIG3, (btConfig3 | CONFIG3_PARM_En));
634
635         switch (eRFPowerState) {
636         case RF_ON:
637                 write_nic_word(dev, 0x37C, 0x00EC);
638
639                 /* turn on AFE */
640                 write_nic_byte(dev, 0x54, 0x00);
641                 write_nic_byte(dev, 0x62, 0x00);
642
643                 /* turn on RF */
644                 RF_WriteReg(dev, 0x0, 0x009f); udelay(500);
645                 RF_WriteReg(dev, 0x4, 0x0972); udelay(500);
646
647                 /* turn on RF again */
648                 RF_WriteReg(dev, 0x0, 0x009f); udelay(500);
649                 RF_WriteReg(dev, 0x4, 0x0972); udelay(500);
650
651                 /* turn on BB */
652                 write_phy_ofdm(dev, 0x10, 0x40);
653                 write_phy_ofdm(dev, 0x12, 0x40);
654
655                 /* Avoid power down at init time. */
656                 write_nic_byte(dev, CONFIG4, priv->RFProgType);
657
658                 u1bTmp = read_nic_byte(dev, 0x24E);
659                 write_nic_byte(dev, 0x24E, (u1bTmp & (~(BIT5 | BIT6))));
660                 break;
661         case RF_SLEEP:
662                 for (QueueID = 0, i = 0; QueueID < 6;) {
663                         if (get_curr_tx_free_desc(dev, QueueID) ==
664                                                         priv->txringcount) {
665                                 QueueID++;
666                                 continue;
667                         } else {
668                                 priv->TxPollingTimes++;
669                                 if (priv->TxPollingTimes >=
670                                         LPS_MAX_SLEEP_WAITING_TIMES_87SE) {
671                                         bActionAllowed = false;
672                                         break;
673                                 } else
674                                         udelay(10);
675                         }
676                 }
677
678                 if (bActionAllowed) {
679                         /* turn off BB RXIQ matrix to cut off rx signal */
680                         write_phy_ofdm(dev, 0x10, 0x00);
681                         write_phy_ofdm(dev, 0x12, 0x00);
682
683                         /* turn off RF */
684                         RF_WriteReg(dev, 0x4, 0x0000);
685                         RF_WriteReg(dev, 0x0, 0x0000);
686
687                         /* turn off AFE except PLL */
688                         write_nic_byte(dev, 0x62, 0xff);
689                         write_nic_byte(dev, 0x54, 0xec);
690
691                         mdelay(1);
692
693                         {
694                                 int i = 0;
695                                 while (true) {
696                                         u8 tmp24F = read_nic_byte(dev, 0x24f);
697
698                                         if ((tmp24F == 0x01) ||
699                                                         (tmp24F == 0x09)) {
700                                                 bTurnOffBB = true;
701                                                 break;
702                                         } else {
703                                                 udelay(10);
704                                                 i++;
705                                                 priv->TxPollingTimes++;
706
707                                                 if (priv->TxPollingTimes >= LPS_MAX_SLEEP_WAITING_TIMES_87SE) {
708                                                         bTurnOffBB = false;
709                                                         break;
710                                                 } else
711                                                         udelay(10);
712                                         }
713                                 }
714                         }
715
716                         if (bTurnOffBB) {
717                                 /* turn off BB */
718                                 u1bTmp = read_nic_byte(dev, 0x24E);
719                                 write_nic_byte(dev, 0x24E,
720                                                 (u1bTmp | BIT5 | BIT6));
721
722                                 /* turn off AFE PLL */
723                                 write_nic_byte(dev, 0x54, 0xFC);
724                                 write_nic_word(dev, 0x37C, 0x00FC);
725                         }
726                 }
727                 break;
728         case RF_OFF:
729                 for (QueueID = 0, i = 0; QueueID < 6;) {
730                         if (get_curr_tx_free_desc(dev, QueueID) ==
731                                         priv->txringcount) {
732                                 QueueID++;
733                                 continue;
734                         } else {
735                                 udelay(10);
736                                 i++;
737                         }
738
739                         if (i >= MAX_DOZE_WAITING_TIMES_85B)
740                                 break;
741                 }
742
743                 /* turn off BB RXIQ matrix to cut off rx signal */
744                 write_phy_ofdm(dev, 0x10, 0x00);
745                 write_phy_ofdm(dev, 0x12, 0x00);
746
747                 /* turn off RF */
748                 RF_WriteReg(dev, 0x4, 0x0000);
749                 RF_WriteReg(dev, 0x0, 0x0000);
750
751                 /* turn off AFE except PLL */
752                 write_nic_byte(dev, 0x62, 0xff);
753                 write_nic_byte(dev, 0x54, 0xec);
754
755                 mdelay(1);
756
757                 {
758                         int i = 0;
759
760                         while (true) {
761                                 u8 tmp24F = read_nic_byte(dev, 0x24f);
762
763                                 if ((tmp24F == 0x01) || (tmp24F == 0x09)) {
764                                         bTurnOffBB = true;
765                                         break;
766                                 } else {
767                                         bTurnOffBB = false;
768                                         udelay(10);
769                                         i++;
770                                 }
771
772                                 if (i > MAX_POLLING_24F_TIMES_87SE)
773                                         break;
774                         }
775                 }
776
777                 if (bTurnOffBB) {
778                         /* turn off BB */
779                         u1bTmp = read_nic_byte(dev, 0x24E);
780                         write_nic_byte(dev, 0x24E, (u1bTmp | BIT5 | BIT6));
781
782                         /* turn off AFE PLL (80M) */
783                         write_nic_byte(dev, 0x54, 0xFC);
784                         write_nic_word(dev, 0x37C, 0x00FC);
785                 }
786                 break;
787         }
788
789         btConfig3 &= ~(CONFIG3_PARM_En);
790         write_nic_byte(dev, CONFIG3, btConfig3);
791
792         btCR9346 &= ~(0xC0);
793         write_nic_byte(dev, CR9346, btCR9346);
794
795         if (bResult && bActionAllowed)
796                 priv->eRFPowerState = eRFPowerState;
797
798         priv->SetRFPowerStateInProgress = false;
799
800         return bResult && bActionAllowed;
801 }
802
803 void rtl8225z4_rf_sleep(struct net_device *dev)
804 {
805         MgntActSet_RF_State(dev, RF_SLEEP, RF_CHANGE_BY_PS);
806 }
807
808 void rtl8225z4_rf_wakeup(struct net_device *dev)
809 {
810         MgntActSet_RF_State(dev, RF_ON, RF_CHANGE_BY_PS);
811 }