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 <andreamrl@tiscali.it>
6 This files contains programming code for the rtl8225
9 *Many* thanks to Realtek Corp. for their great support!
14 #include "r8180_rtl8225.h"
15 #include "r8180_93cx6.h"
17 #include "ieee80211/dot11d.h"
20 static void write_rtl8225(struct net_device *dev, u8 adr, u16 data)
25 u32 bangdata = (data << 4) | (adr & 0xf);
26 struct r8180_priv *priv = ieee80211_priv(dev);
28 out = read_nic_word(dev, RFPinsOutput) & 0xfff3;
30 write_nic_word(dev, RFPinsEnable,
31 (read_nic_word(dev, RFPinsEnable) | 0x7));
33 select = read_nic_word(dev, RFPinsSelect);
35 write_nic_word(dev, RFPinsSelect, select | 0x7 |
36 ((priv->card_type == USB) ? 0 : SW_CONTROL_GPIO));
38 force_pci_posting(dev);
41 write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
43 force_pci_posting(dev);
46 write_nic_word(dev, RFPinsOutput, out);
48 force_pci_posting(dev);
51 for (i = 15; i >= 0; i--) {
52 bit = (bangdata & (1 << i)) >> i;
54 write_nic_word(dev, RFPinsOutput, bit | out);
56 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
57 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
60 bit = (bangdata & (1 << i)) >> i;
62 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
63 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
65 write_nic_word(dev, RFPinsOutput, bit | out);
69 write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
71 force_pci_posting(dev);
74 write_nic_word(dev, RFPinsOutput, out |
75 ((priv->card_type == USB) ? 4 : BB_HOST_BANG_EN));
77 write_nic_word(dev, RFPinsSelect, select |
78 ((priv->card_type == USB) ? 0 : SW_CONTROL_GPIO));
80 if (priv->card_type == USB)
83 rtl8185_rf_pins_enable(dev);
86 static const u16 rtl8225bcd_rxgain[] = {
87 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
88 0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
89 0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
90 0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
91 0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
92 0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
93 0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
94 0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
95 0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
96 0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
97 0x07aa, 0x07ab, 0x07ac, 0x07ad, 0x07b0, 0x07b1, 0x07b2, 0x07b3,
98 0x07b4, 0x07b5, 0x07b8, 0x07b9, 0x07ba, 0x07bb, 0x07bb
102 static const u8 rtl8225_agc[] = {
103 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e, 0x9e,
104 0x9d, 0x9c, 0x9b, 0x9a, 0x99, 0x98, 0x97, 0x96,
105 0x95, 0x94, 0x93, 0x92, 0x91, 0x90, 0x8f, 0x8e,
106 0x8d, 0x8c, 0x8b, 0x8a, 0x89, 0x88, 0x87, 0x86,
107 0x85, 0x84, 0x83, 0x82, 0x81, 0x80, 0x3f, 0x3e,
108 0x3d, 0x3c, 0x3b, 0x3a, 0x39, 0x38, 0x37, 0x36,
109 0x35, 0x34, 0x33, 0x32, 0x31, 0x30, 0x2f, 0x2e,
110 0x2d, 0x2c, 0x2b, 0x2a, 0x29, 0x28, 0x27, 0x26,
111 0x25, 0x24, 0x23, 0x22, 0x21, 0x20, 0x1f, 0x1e,
112 0x1d, 0x1c, 0x1b, 0x1a, 0x19, 0x18, 0x17, 0x16,
113 0x15, 0x14, 0x13, 0x12, 0x11, 0x10, 0x0f, 0x0e,
114 0x0d, 0x0c, 0x0b, 0x0a, 0x09, 0x08, 0x07, 0x06,
115 0x05, 0x04, 0x03, 0x02, 0x01, 0x01, 0x01, 0x01,
116 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
117 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
118 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
121 static const u8 rtl8225_gain[] = {
122 0x23, 0x88, 0x7c, 0xa5, /* -82dBm */
123 0x23, 0x88, 0x7c, 0xb5, /* -82dBm */
124 0x23, 0x88, 0x7c, 0xc5, /* -82dBm */
125 0x33, 0x80, 0x79, 0xc5, /* -78dBm */
126 0x43, 0x78, 0x76, 0xc5, /* -74dBm */
127 0x53, 0x60, 0x73, 0xc5, /* -70dBm */
128 0x63, 0x58, 0x70, 0xc5, /* -66dBm */
131 static const u8 rtl8225_tx_gain_cck_ofdm[] = {
132 0x02, 0x06, 0x0e, 0x1e, 0x3e, 0x7e
135 static const u8 rtl8225_tx_power_cck[] = {
136 0x18, 0x17, 0x15, 0x11, 0x0c, 0x08, 0x04, 0x02,
137 0x1b, 0x1a, 0x17, 0x13, 0x0e, 0x09, 0x04, 0x02,
138 0x1f, 0x1e, 0x1a, 0x15, 0x10, 0x0a, 0x05, 0x02,
139 0x22, 0x21, 0x1d, 0x18, 0x11, 0x0b, 0x06, 0x02,
140 0x26, 0x25, 0x21, 0x1b, 0x14, 0x0d, 0x06, 0x03,
141 0x2b, 0x2a, 0x25, 0x1e, 0x16, 0x0e, 0x07, 0x03
144 static const u8 rtl8225_tx_power_cck_ch14[] = {
145 0x18, 0x17, 0x15, 0x0c, 0x00, 0x00, 0x00, 0x00,
146 0x1b, 0x1a, 0x17, 0x0e, 0x00, 0x00, 0x00, 0x00,
147 0x1f, 0x1e, 0x1a, 0x0f, 0x00, 0x00, 0x00, 0x00,
148 0x22, 0x21, 0x1d, 0x11, 0x00, 0x00, 0x00, 0x00,
149 0x26, 0x25, 0x21, 0x13, 0x00, 0x00, 0x00, 0x00,
150 0x2b, 0x2a, 0x25, 0x15, 0x00, 0x00, 0x00, 0x00
153 static const u8 rtl8225_tx_power_ofdm[] = {
154 0x80, 0x90, 0xa2, 0xb5, 0xcb, 0xe4
157 static const u32 rtl8225_chan[] = {
159 0x0080, 0x0100, 0x0180, 0x0200, 0x0280, 0x0300, 0x0380,
160 0x0400, 0x0480, 0x0500, 0x0580, 0x0600, 0x0680, 0x074A,
163 static void rtl8225_SetTXPowerLevel(struct net_device *dev, short ch)
165 struct r8180_priv *priv = ieee80211_priv(dev);
170 const u8 *cck_power_table;
171 u8 max_cck_power_level;
172 u8 max_ofdm_power_level;
173 u8 min_ofdm_power_level;
174 u8 cck_power_level = 0xff & priv->chtxpwr[ch];
175 u8 ofdm_power_level = 0xff & priv->chtxpwr_ofdm[ch];
177 if (priv->card_type == USB) {
178 max_cck_power_level = 11;
179 max_ofdm_power_level = 25;
180 min_ofdm_power_level = 10;
182 max_cck_power_level = 35;
183 max_ofdm_power_level = 35;
184 min_ofdm_power_level = 0;
187 if (cck_power_level > max_cck_power_level)
188 cck_power_level = max_cck_power_level;
190 GainIdx = cck_power_level % 6;
191 GainSetting = cck_power_level / 6;
194 cck_power_table = rtl8225_tx_power_cck_ch14;
196 cck_power_table = rtl8225_tx_power_cck;
198 write_nic_byte(dev, TX_GAIN_CCK,
199 rtl8225_tx_gain_cck_ofdm[GainSetting] >> 1);
201 for (i = 0; i < 8; i++) {
202 power = cck_power_table[GainIdx * 8 + i];
203 write_phy_cck(dev, 0x44 + i, power);
206 /* FIXME Is this delay really needeed ? */
207 force_pci_posting(dev);
210 if (ofdm_power_level > (max_ofdm_power_level - min_ofdm_power_level))
211 ofdm_power_level = max_ofdm_power_level;
213 ofdm_power_level += min_ofdm_power_level;
215 if (ofdm_power_level > 35)
216 ofdm_power_level = 35;
218 GainIdx = ofdm_power_level % 6;
219 GainSetting = ofdm_power_level / 6;
221 rtl8185_set_anaparam2(dev, RTL8225_ANAPARAM2_ON);
223 write_phy_ofdm(dev, 2, 0x42);
224 write_phy_ofdm(dev, 6, 0x00);
225 write_phy_ofdm(dev, 8, 0x00);
227 write_nic_byte(dev, TX_GAIN_OFDM,
228 rtl8225_tx_gain_cck_ofdm[GainSetting] >> 1);
230 power = rtl8225_tx_power_ofdm[GainIdx];
232 write_phy_ofdm(dev, 5, power);
233 write_phy_ofdm(dev, 7, power);
235 force_pci_posting(dev);
239 static const u8 rtl8225z2_threshold[] = {
240 0x8d, 0x8d, 0x8d, 0x8d, 0x9d, 0xad, 0xbd,
243 static const u8 rtl8225z2_gain_bg[] = {
244 0x23, 0x15, 0xa5, /* -82-1dBm */
245 0x23, 0x15, 0xb5, /* -82-2dBm */
246 0x23, 0x15, 0xc5, /* -82-3dBm */
247 0x33, 0x15, 0xc5, /* -78dBm */
248 0x43, 0x15, 0xc5, /* -74dBm */
249 0x53, 0x15, 0xc5, /* -70dBm */
250 0x63, 0x15, 0xc5, /* -66dBm */
253 static const u8 rtl8225z2_gain_a[] = {
254 0x13, 0x27, 0x5a, /* -82dBm */
255 0x23, 0x23, 0x58, /* -82dBm */
256 0x33, 0x1f, 0x56, /* -82dBm */
257 0x43, 0x1b, 0x54, /* -78dBm */
258 0x53, 0x17, 0x51, /* -74dBm */
259 0x63, 0x24, 0x4f, /* -70dBm */
260 0x73, 0x0f, 0x4c, /* -66dBm */
263 static const u16 rtl8225z2_rxgain[] = {
264 0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
265 0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
266 0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
267 0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
268 0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
269 0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
270 0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
271 0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
272 0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
273 0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
274 0x03aa, 0x03ab, 0x03ac, 0x03ad, 0x03b0, 0x03b1, 0x03b2, 0x03b3,
275 0x03b4, 0x03b5, 0x03b8, 0x03b9, 0x03ba, 0x03bb, 0x03bb
279 static const u8 ZEBRA2_CCK_OFDM_GAIN_SETTING[] = {
280 0x00, 0x01, 0x02, 0x03, 0x04, 0x05,
281 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b,
282 0x0c, 0x0d, 0x0e, 0x0f, 0x10, 0x11,
283 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
284 0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d,
285 0x1e, 0x1f, 0x20, 0x21, 0x22, 0x23,
288 static const u8 rtl8225z2_tx_power_ofdm[] = {
289 0x42, 0x00, 0x40, 0x00, 0x40
292 static const u8 rtl8225z2_tx_power_cck_ch14[] = {
293 0x36, 0x35, 0x2e, 0x1b, 0x00, 0x00, 0x00, 0x00
296 static const u8 rtl8225z2_tx_power_cck[] = {
297 0x36, 0x35, 0x2e, 0x25, 0x1c, 0x12, 0x09, 0x04
300 void rtl8225z2_set_gain(struct net_device *dev, short gain)
302 const u8 *rtl8225_gain;
303 struct r8180_priv *priv = ieee80211_priv(dev);
304 u8 mode = priv->ieee80211->mode;
306 if (mode == IEEE_B || mode == IEEE_G)
307 rtl8225_gain = rtl8225z2_gain_bg;
309 rtl8225_gain = rtl8225z2_gain_a;
311 write_phy_ofdm(dev, 0x0b, rtl8225_gain[gain * 3]);
312 write_phy_ofdm(dev, 0x1b, rtl8225_gain[gain * 3 + 1]);
313 write_phy_ofdm(dev, 0x1d, rtl8225_gain[gain * 3 + 2]);
314 write_phy_ofdm(dev, 0x21, 0x37);
317 static u32 read_rtl8225(struct net_device *dev, u8 adr)
319 u32 data2Write = ((u32)(adr & 0x1f)) << 27;
322 u16 oval,oval2,oval3,tmp;
329 oval = read_nic_word(dev, RFPinsOutput);
330 oval2 = read_nic_word(dev, RFPinsEnable);
331 oval3 = read_nic_word(dev, RFPinsSelect);
333 write_nic_word(dev, RFPinsEnable, (oval2|0xf));
334 write_nic_word(dev, RFPinsSelect, (oval3|0xf));
340 write_nic_word(dev, RFPinsOutput, oval | BB_HOST_BANG_EN ); udelay(4);
342 write_nic_word(dev, RFPinsOutput, oval ); udelay(5);
346 mask = (low2high) ? 0x01 : (((u32)0x01)<<(32-1));
348 for (i = 0; i < wLength/2; i++) {
349 bit = ((data2Write&mask) != 0) ? 1 : 0;
350 write_nic_word(dev, RFPinsOutput, bit|oval | rw); udelay(1);
352 write_nic_word(dev, RFPinsOutput, bit|oval | BB_HOST_BANG_CLK | rw); udelay(2);
353 write_nic_word(dev, RFPinsOutput, bit|oval | BB_HOST_BANG_CLK | rw); udelay(2);
355 mask = (low2high) ? (mask<<1): (mask>>1);
358 rw = BB_HOST_BANG_RW;
359 write_nic_word(dev, RFPinsOutput, bit|oval | BB_HOST_BANG_CLK | rw); udelay(2);
360 write_nic_word(dev, RFPinsOutput, bit|oval | rw); udelay(2);
364 bit = ((data2Write&mask) != 0) ? 1: 0;
366 write_nic_word(dev, RFPinsOutput, oval|bit|rw| BB_HOST_BANG_CLK); udelay(2);
367 write_nic_word(dev, RFPinsOutput, oval|bit|rw| BB_HOST_BANG_CLK); udelay(2);
369 write_nic_word(dev, RFPinsOutput, oval| bit |rw); udelay(1);
371 mask = (low2high) ? (mask<<1) : (mask>>1);
374 write_nic_word(dev, RFPinsOutput, rw|oval); udelay(2);
375 mask = (low2high) ? 0x01 : (((u32)0x01) << (12-1));
378 * We must set data pin to HW controled, otherwise RF can't driver it
379 * and value RF register won't be able to read back properly.
381 write_nic_word(dev, RFPinsEnable, (oval2 & (~0x01)));
383 for (i = 0; i < rLength; i++) {
384 write_nic_word(dev, RFPinsOutput, rw|oval); udelay(1);
386 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK); udelay(2);
387 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK); udelay(2);
388 write_nic_word(dev, RFPinsOutput, rw|oval|BB_HOST_BANG_CLK); udelay(2);
389 tmp = read_nic_word(dev, RFPinsInput);
391 dataRead |= (tmp & BB_HOST_BANG_CLK ? mask : 0);
393 write_nic_word(dev, RFPinsOutput, (rw|oval)); udelay(2);
395 mask = (low2high) ? (mask<<1) : (mask>>1);
398 write_nic_word(dev, RFPinsOutput, BB_HOST_BANG_EN|BB_HOST_BANG_RW|oval); udelay(2);
400 write_nic_word(dev, RFPinsEnable, oval2);
401 write_nic_word(dev, RFPinsSelect, oval3); /* Set To SW Switch */
402 write_nic_word(dev, RFPinsOutput, 0x3a0);
407 short rtl8225_is_V_z2(struct net_device *dev)
411 if (read_rtl8225(dev, 8) != 0x588)
413 else /* reg 9 pg 1 = 24 */
414 if (read_rtl8225(dev, 9) != 0x700)
417 /* sw back to pg 0 */
418 write_rtl8225(dev, 0, 0xb7);
423 void rtl8225z2_rf_close(struct net_device *dev)
425 RF_WriteReg(dev, 0x4, 0x1f);
427 force_pci_posting(dev);
430 rtl8180_set_anaparam(dev, RTL8225z2_ANAPARAM_OFF);
431 rtl8185_set_anaparam2(dev, RTL8225z2_ANAPARAM2_OFF);
435 * Map dBm into Tx power index according to current HW model, for example,
436 * RF and PA, and current wireless mode.
438 s8 DbmToTxPwrIdx(struct r8180_priv *priv, WIRELESS_MODE WirelessMode,
441 bool bUseDefault = true;
445 * OFDM Power in dBm = Index * 0.5 + 0
446 * CCK Power in dBm = Index * 0.25 + 13
450 if (WirelessMode == WIRELESS_MODE_G) {
452 tmp = (2 * PowerInDbm);
456 else if (tmp > 40) /* 40 means 20 dBm. */
460 } else if (WirelessMode == WIRELESS_MODE_B) {
462 tmp = (4 * PowerInDbm) - 52;
466 else if (tmp > 28) /* 28 means 20 dBm. */
473 * TRUE if we want to use a default implementation.
474 * We shall set it to FALSE when we have exact translation formular
475 * for target IC. 070622, by rcnjko.
480 else if (PowerInDbm > 35)
483 TxPwrIdx = (u8)PowerInDbm;
489 void rtl8225z2_SetTXPowerLevel(struct net_device *dev, short ch)
491 struct r8180_priv *priv = ieee80211_priv(dev);
492 u8 max_cck_power_level;
493 u8 max_ofdm_power_level;
494 u8 min_ofdm_power_level;
495 char cck_power_level = (char)(0xff & priv->chtxpwr[ch]);
496 char ofdm_power_level = (char)(0xff & priv->chtxpwr_ofdm[ch]);
498 if (IS_DOT11D_ENABLE(priv->ieee80211) &&
499 IS_DOT11D_STATE_DONE(priv->ieee80211)) {
500 u8 MaxTxPwrInDbm = DOT11D_GetMaxTxPwrInDbm(priv->ieee80211, ch);
501 u8 CckMaxPwrIdx = DbmToTxPwrIdx(priv, WIRELESS_MODE_B, MaxTxPwrInDbm);
502 u8 OfdmMaxPwrIdx = DbmToTxPwrIdx(priv, WIRELESS_MODE_G, MaxTxPwrInDbm);
504 if (cck_power_level > CckMaxPwrIdx)
505 cck_power_level = CckMaxPwrIdx;
506 if (ofdm_power_level > OfdmMaxPwrIdx)
507 ofdm_power_level = OfdmMaxPwrIdx;
510 max_cck_power_level = 15;
511 max_ofdm_power_level = 25;
512 min_ofdm_power_level = 10;
514 if (cck_power_level > 35)
515 cck_power_level = 35;
517 write_nic_byte(dev, CCK_TXAGC,
518 (ZEBRA2_CCK_OFDM_GAIN_SETTING[(u8)cck_power_level]));
519 force_pci_posting(dev);
522 if (ofdm_power_level > 35)
523 ofdm_power_level = 35;
526 write_phy_ofdm(dev, 2, 0x42);
527 write_phy_ofdm(dev, 5, 0x00);
528 write_phy_ofdm(dev, 6, 0x40);
529 write_phy_ofdm(dev, 7, 0x00);
530 write_phy_ofdm(dev, 8, 0x40);
533 write_nic_byte(dev, OFDM_TXAGC,
534 ZEBRA2_CCK_OFDM_GAIN_SETTING[(u8)ofdm_power_level]);
536 if (ofdm_power_level <= 11) {
537 write_phy_ofdm(dev, 0x07, 0x5c);
538 write_phy_ofdm(dev, 0x09, 0x5c);
541 if (ofdm_power_level <= 17) {
542 write_phy_ofdm(dev, 0x07, 0x54);
543 write_phy_ofdm(dev, 0x09, 0x54);
545 write_phy_ofdm(dev, 0x07, 0x50);
546 write_phy_ofdm(dev, 0x09, 0x50);
549 force_pci_posting(dev);
553 void rtl8225z2_rf_set_chan(struct net_device *dev, short ch)
555 rtl8225z2_SetTXPowerLevel(dev, ch);
557 RF_WriteReg(dev, 0x7, rtl8225_chan[ch]);
559 if ((RF_ReadReg(dev, 0x7) & 0x0F80) != rtl8225_chan[ch])
560 RF_WriteReg(dev, 0x7, rtl8225_chan[ch]);
564 force_pci_posting(dev);
568 static void rtl8225_host_pci_init(struct net_device *dev)
570 write_nic_word(dev, RFPinsOutput, 0x480);
572 rtl8185_rf_pins_enable(dev);
574 write_nic_word(dev, RFPinsSelect, 0x88 | SW_CONTROL_GPIO);
576 write_nic_byte(dev, GP_ENABLE, 0);
578 force_pci_posting(dev);
581 /* bit 6 is for RF on/off detection */
582 write_nic_word(dev, GP_ENABLE, 0xff & (~(1 << 6)));
585 static void rtl8225_rf_set_chan(struct net_device *dev, short ch)
587 struct r8180_priv *priv = ieee80211_priv(dev);
588 short gset = (priv->ieee80211->state == IEEE80211_LINKED &&
589 ieee80211_is_54g(priv->ieee80211->current_network)) ||
590 priv->ieee80211->iw_mode == IW_MODE_MONITOR;
592 rtl8225_SetTXPowerLevel(dev, ch);
594 write_rtl8225(dev, 0x7, rtl8225_chan[ch]);
596 force_pci_posting(dev);
600 write_nic_byte(dev, SIFS, 0x22);
601 write_nic_byte(dev, DIFS, 0x14);
603 write_nic_byte(dev, SIFS, 0x44);
604 write_nic_byte(dev, DIFS, 0x24);
607 if (priv->ieee80211->state == IEEE80211_LINKED &&
608 ieee80211_is_shortslot(priv->ieee80211->current_network))
609 write_nic_byte(dev, SLOT, 0x9);
611 write_nic_byte(dev, SLOT, 0x14);
614 write_nic_byte(dev, EIFS, 81);
615 write_nic_byte(dev, CW_VAL, 0x73);
617 write_nic_byte(dev, EIFS, 81);
618 write_nic_byte(dev, CW_VAL, 0xa5);
622 void rtl8225z2_rf_init(struct net_device *dev)
624 struct r8180_priv *priv = ieee80211_priv(dev);
630 priv->chan = channel;
632 if (priv->card_type != USB)
633 rtl8225_host_pci_init(dev);
635 write_nic_dword(dev, RF_TIMING, 0x000a8008);
637 brsr = read_nic_word(dev, BRSR);
639 write_nic_word(dev, BRSR, 0xffff);
641 write_nic_dword(dev, RF_PARA, 0x100044);
643 rtl8180_set_mode(dev, EPROM_CMD_CONFIG);
644 write_nic_byte(dev, CONFIG3, 0x44);
645 rtl8180_set_mode(dev, EPROM_CMD_NORMAL);
647 rtl8185_rf_pins_enable(dev);
649 write_rtl8225(dev, 0x0, 0x2bf); mdelay(1);
650 write_rtl8225(dev, 0x1, 0xee0); mdelay(1);
651 write_rtl8225(dev, 0x2, 0x44d); mdelay(1);
652 write_rtl8225(dev, 0x3, 0x441); mdelay(1);
653 write_rtl8225(dev, 0x4, 0x8c3); mdelay(1);
654 write_rtl8225(dev, 0x5, 0xc72); mdelay(1);
655 write_rtl8225(dev, 0x6, 0xe6); mdelay(1);
656 write_rtl8225(dev, 0x7, ((priv->card_type == USB)? 0x82a : rtl8225_chan[channel])); mdelay(1);
657 write_rtl8225(dev, 0x8, 0x3f); mdelay(1);
658 write_rtl8225(dev, 0x9, 0x335); mdelay(1);
659 write_rtl8225(dev, 0xa, 0x9d4); mdelay(1);
660 write_rtl8225(dev, 0xb, 0x7bb); mdelay(1);
661 write_rtl8225(dev, 0xc, 0x850); mdelay(1);
662 write_rtl8225(dev, 0xd, 0xcdf); mdelay(1);
663 write_rtl8225(dev, 0xe, 0x2b); mdelay(1);
664 write_rtl8225(dev, 0xf, 0x114);
668 write_rtl8225(dev, 0x0, 0x1b7);
670 for (i = 0; i < 95; i++) {
671 write_rtl8225(dev, 0x1, (u8)(i + 1));
672 write_rtl8225(dev, 0x2, rtl8225z2_rxgain[i]);
675 write_rtl8225(dev, 0x3, 0x80);
676 write_rtl8225(dev, 0x5, 0x4);
678 write_rtl8225(dev, 0x0, 0xb7);
680 write_rtl8225(dev, 0x2, 0xc4d);
682 if (priv->card_type == USB) {
685 write_rtl8225(dev, 0x2, 0x44d);
689 /* FIXME!! rtl8187 we have to check if calibrarion
690 * is successful and eventually cal. again (repeat
691 * the two write on reg 2)
693 data = read_rtl8225(dev, 6);
694 if (!(data & 0x00000080)) {
695 write_rtl8225(dev, 0x02, 0x0c4d);
696 force_pci_posting(dev); mdelay(200);
697 write_rtl8225(dev, 0x02, 0x044d);
698 force_pci_posting(dev); mdelay(100);
699 data = read_rtl8225(dev, 6);
700 if (!(data & 0x00000080))
701 DMESGW("RF Calibration Failed!!!!\n");
706 write_rtl8225(dev, 0x0, 0x2bf);
708 if (priv->card_type != USB)
709 rtl8185_rf_pins_enable(dev);
711 for (i = 0; i < 128; i++) {
712 data = rtl8225_agc[i];
714 addr = i + 0x80; /* enable writing AGC table */
715 write_phy_ofdm(dev, 0xb, data);
718 write_phy_ofdm(dev, 0xa, addr);
722 force_pci_posting(dev);
725 write_phy_ofdm(dev, 0x00, 0x01); mdelay(1);
726 write_phy_ofdm(dev, 0x01, 0x02); mdelay(1);
727 write_phy_ofdm(dev, 0x02, ((priv->card_type == USB) ? 0x42 : 0x62)); mdelay(1);
728 write_phy_ofdm(dev, 0x03, 0x00); mdelay(1);
729 write_phy_ofdm(dev, 0x04, 0x00); mdelay(1);
730 write_phy_ofdm(dev, 0x05, 0x00); mdelay(1);
731 write_phy_ofdm(dev, 0x06, 0x40); mdelay(1);
732 write_phy_ofdm(dev, 0x07, 0x00); mdelay(1);
733 write_phy_ofdm(dev, 0x08, 0x40); mdelay(1);
734 write_phy_ofdm(dev, 0x09, 0xfe); mdelay(1);
735 write_phy_ofdm(dev, 0x0a, 0x08); mdelay(1);
736 write_phy_ofdm(dev, 0x0b, 0x80); mdelay(1);
737 write_phy_ofdm(dev, 0x0c, 0x01); mdelay(1);
738 write_phy_ofdm(dev, 0x0d, 0x43);
739 write_phy_ofdm(dev, 0x0e, 0xd3); mdelay(1);
740 write_phy_ofdm(dev, 0x0f, 0x38); mdelay(1);
741 write_phy_ofdm(dev, 0x10, 0x84); mdelay(1);
742 write_phy_ofdm(dev, 0x11, 0x07); mdelay(1);
743 write_phy_ofdm(dev, 0x12, 0x20); mdelay(1);
744 write_phy_ofdm(dev, 0x13, 0x20); mdelay(1);
745 write_phy_ofdm(dev, 0x14, 0x00); mdelay(1);
746 write_phy_ofdm(dev, 0x15, 0x40); mdelay(1);
747 write_phy_ofdm(dev, 0x16, 0x00); mdelay(1);
748 write_phy_ofdm(dev, 0x17, 0x40); mdelay(1);
749 write_phy_ofdm(dev, 0x18, 0xef); mdelay(1);
750 write_phy_ofdm(dev, 0x19, 0x19); mdelay(1);
751 write_phy_ofdm(dev, 0x1a, 0x20); mdelay(1);
752 write_phy_ofdm(dev, 0x1b, 0x15); mdelay(1);
753 write_phy_ofdm(dev, 0x1c, 0x04); mdelay(1);
754 write_phy_ofdm(dev, 0x1d, 0xc5); mdelay(1);
755 write_phy_ofdm(dev, 0x1e, 0x95); mdelay(1);
756 write_phy_ofdm(dev, 0x1f, 0x75); mdelay(1);
757 write_phy_ofdm(dev, 0x20, 0x1f); mdelay(1);
758 write_phy_ofdm(dev, 0x21, 0x17); mdelay(1);
759 write_phy_ofdm(dev, 0x22, 0x16); mdelay(1);
760 write_phy_ofdm(dev, 0x23, 0x80); mdelay(1); /* FIXME maybe not needed */
761 write_phy_ofdm(dev, 0x24, 0x46); mdelay(1);
762 write_phy_ofdm(dev, 0x25, 0x00); mdelay(1);
763 write_phy_ofdm(dev, 0x26, 0x90); mdelay(1);
764 write_phy_ofdm(dev, 0x27, 0x88); mdelay(1);
766 rtl8225z2_set_gain(dev,4);
768 write_phy_cck(dev, 0x0, 0x98); mdelay(1);
769 write_phy_cck(dev, 0x3, 0x20); mdelay(1);
770 write_phy_cck(dev, 0x4, 0x7e); mdelay(1);
771 write_phy_cck(dev, 0x5, 0x12); mdelay(1);
772 write_phy_cck(dev, 0x6, 0xfc); mdelay(1);
773 write_phy_cck(dev, 0x7, 0x78); mdelay(1);
774 write_phy_cck(dev, 0x8, 0x2e); mdelay(1);
775 write_phy_cck(dev, 0x10, ((priv->card_type == USB) ? 0x9b: 0x93)); mdelay(1);
776 write_phy_cck(dev, 0x11, 0x88); mdelay(1);
777 write_phy_cck(dev, 0x12, 0x47); mdelay(1);
778 write_phy_cck(dev, 0x13, 0xd0);
779 write_phy_cck(dev, 0x19, 0x00);
780 write_phy_cck(dev, 0x1a, 0xa0);
781 write_phy_cck(dev, 0x1b, 0x08);
782 write_phy_cck(dev, 0x40, 0x86); /* CCK Carrier Sense Threshold */
783 write_phy_cck(dev, 0x41, 0x8d); mdelay(1);
784 write_phy_cck(dev, 0x42, 0x15); mdelay(1);
785 write_phy_cck(dev, 0x43, 0x18); mdelay(1);
786 write_phy_cck(dev, 0x44, 0x36); mdelay(1);
787 write_phy_cck(dev, 0x45, 0x35); mdelay(1);
788 write_phy_cck(dev, 0x46, 0x2e); mdelay(1);
789 write_phy_cck(dev, 0x47, 0x25); mdelay(1);
790 write_phy_cck(dev, 0x48, 0x1c); mdelay(1);
791 write_phy_cck(dev, 0x49, 0x12); mdelay(1);
792 write_phy_cck(dev, 0x4a, 0x09); mdelay(1);
793 write_phy_cck(dev, 0x4b, 0x04); mdelay(1);
794 write_phy_cck(dev, 0x4c, 0x05); mdelay(1);
796 write_nic_byte(dev, 0x5b, 0x0d); mdelay(1);
798 rtl8225z2_SetTXPowerLevel(dev, channel);
800 /* RX antenna default to A */
801 write_phy_cck(dev, 0x11, 0x9b); mdelay(1); /* B: 0xDB */
802 write_phy_ofdm(dev, 0x26, 0x90); mdelay(1); /* B: 0x10 */
804 rtl8185_tx_antenna(dev, 0x03); /* B: 0x00 */
806 /* switch to high-speed 3-wire
807 * last digit. 2 for both cck and ofdm
809 if (priv->card_type == USB)
810 write_nic_dword(dev, 0x94, 0x3dc00002);
812 write_nic_dword(dev, 0x94, 0x15c00002);
813 rtl8185_rf_pins_enable(dev);
816 rtl8225_rf_set_chan(dev, priv->chan);
819 void rtl8225z2_rf_set_mode(struct net_device *dev)
821 struct r8180_priv *priv = ieee80211_priv(dev);
823 if (priv->ieee80211->mode == IEEE_A) {
824 write_rtl8225(dev, 0x5, 0x1865);
825 write_nic_dword(dev, RF_PARA, 0x10084);
826 write_nic_dword(dev, RF_TIMING, 0xa8008);
827 write_phy_ofdm(dev, 0x0, 0x0);
828 write_phy_ofdm(dev, 0xa, 0x6);
829 write_phy_ofdm(dev, 0xb, 0x99);
830 write_phy_ofdm(dev, 0xf, 0x20);
831 write_phy_ofdm(dev, 0x11, 0x7);
833 rtl8225z2_set_gain(dev,4);
835 write_phy_ofdm(dev,0x15, 0x40);
836 write_phy_ofdm(dev,0x17, 0x40);
838 write_nic_dword(dev, 0x94,0x10000000);
840 write_rtl8225(dev, 0x5, 0x1864);
841 write_nic_dword(dev, RF_PARA, 0x10044);
842 write_nic_dword(dev, RF_TIMING, 0xa8008);
843 write_phy_ofdm(dev, 0x0, 0x1);
844 write_phy_ofdm(dev, 0xa, 0x6);
845 write_phy_ofdm(dev, 0xb, 0x99);
846 write_phy_ofdm(dev, 0xf, 0x20);
847 write_phy_ofdm(dev, 0x11, 0x7);
849 rtl8225z2_set_gain(dev,4);
851 write_phy_ofdm(dev,0x15, 0x40);
852 write_phy_ofdm(dev,0x17, 0x40);
854 write_nic_dword(dev, 0x94,0x04000002);
858 #define MAX_DOZE_WAITING_TIMES_85B 20
859 #define MAX_POLLING_24F_TIMES_87SE 10
860 #define LPS_MAX_SLEEP_WAITING_TIMES_87SE 5
862 bool SetZebraRFPowerState8185(struct net_device *dev,
863 RT_RF_POWER_STATE eRFPowerState)
865 struct r8180_priv *priv = ieee80211_priv(dev);
866 u8 btCR9346, btConfig3;
867 bool bActionAllowed = true, bTurnOffBB = true;
873 if (priv->SetRFPowerStateInProgress == true)
876 priv->SetRFPowerStateInProgress = true;
878 btCR9346 = read_nic_byte(dev, CR9346);
879 write_nic_byte(dev, CR9346, (btCR9346 | 0xC0));
881 btConfig3 = read_nic_byte(dev, CONFIG3);
882 write_nic_byte(dev, CONFIG3, (btConfig3 | CONFIG3_PARM_En));
884 switch (priv->rf_chip) {
886 switch (eRFPowerState) {
888 RF_WriteReg(dev,0x4,0x9FF);
890 write_nic_dword(dev, ANAPARAM, ANAPARM_ON);
891 write_nic_dword(dev, ANAPARAM2, ANAPARM2_ON);
893 write_nic_byte(dev, CONFIG4, priv->RFProgType);
895 /* turn on CCK and OFDM */
896 u1bTmp = read_nic_byte(dev, 0x24E);
897 write_nic_byte(dev, 0x24E, (u1bTmp & (~(BIT5 | BIT6))));
909 switch (eRFPowerState) {
911 write_nic_word(dev, 0x37C, 0x00EC);
914 write_nic_byte(dev, 0x54, 0x00);
915 write_nic_byte(dev, 0x62, 0x00);
918 RF_WriteReg(dev, 0x0, 0x009f); udelay(500);
919 RF_WriteReg(dev, 0x4, 0x0972); udelay(500);
921 /* turn on RF again */
922 RF_WriteReg(dev, 0x0, 0x009f); udelay(500);
923 RF_WriteReg(dev, 0x4, 0x0972); udelay(500);
926 write_phy_ofdm(dev,0x10,0x40);
927 write_phy_ofdm(dev,0x12,0x40);
929 /* Avoid power down at init time. */
930 write_nic_byte(dev, CONFIG4, priv->RFProgType);
932 u1bTmp = read_nic_byte(dev, 0x24E);
933 write_nic_byte(dev, 0x24E, (u1bTmp & (~(BIT5 | BIT6))));
936 for (QueueID = 0, i = 0; QueueID < 6;) {
937 if (get_curr_tx_free_desc(dev, QueueID) == priv->txringcount) {
941 priv->TxPollingTimes ++;
942 if (priv->TxPollingTimes >= LPS_MAX_SLEEP_WAITING_TIMES_87SE) {
943 bActionAllowed = false;
950 if (bActionAllowed) {
951 /* turn off BB RXIQ matrix to cut off rx signal */
952 write_phy_ofdm(dev, 0x10, 0x00);
953 write_phy_ofdm(dev, 0x12, 0x00);
956 RF_WriteReg(dev, 0x4, 0x0000);
957 RF_WriteReg(dev, 0x0, 0x0000);
959 /* turn off AFE except PLL */
960 write_nic_byte(dev, 0x62, 0xff);
961 write_nic_byte(dev, 0x54, 0xec);
968 u8 tmp24F = read_nic_byte(dev, 0x24f);
970 if ((tmp24F == 0x01) || (tmp24F == 0x09)) {
976 priv->TxPollingTimes++;
978 if (priv->TxPollingTimes >= LPS_MAX_SLEEP_WAITING_TIMES_87SE) {
989 u1bTmp = read_nic_byte(dev, 0x24E);
990 write_nic_byte(dev, 0x24E, (u1bTmp | BIT5 | BIT6));
992 /* turn off AFE PLL */
993 write_nic_byte(dev, 0x54, 0xFC);
994 write_nic_word(dev, 0x37C, 0x00FC);
999 for (QueueID = 0, i = 0; QueueID < 6;) {
1000 if (get_curr_tx_free_desc(dev, QueueID) == priv->txringcount) {
1008 if (i >= MAX_DOZE_WAITING_TIMES_85B)
1012 /* turn off BB RXIQ matrix to cut off rx signal */
1013 write_phy_ofdm(dev, 0x10, 0x00);
1014 write_phy_ofdm(dev, 0x12, 0x00);
1017 RF_WriteReg(dev, 0x4, 0x0000);
1018 RF_WriteReg(dev, 0x0, 0x0000);
1020 /* turn off AFE except PLL */
1021 write_nic_byte(dev, 0x62, 0xff);
1022 write_nic_byte(dev, 0x54, 0xec);
1031 u8 tmp24F = read_nic_byte(dev, 0x24f);
1033 if ((tmp24F == 0x01) || (tmp24F == 0x09)) {
1042 if (i > MAX_POLLING_24F_TIMES_87SE)
1049 u1bTmp = read_nic_byte(dev, 0x24E);
1050 write_nic_byte(dev, 0x24E, (u1bTmp | BIT5 | BIT6));
1052 /* turn off AFE PLL (80M) */
1053 write_nic_byte(dev, 0x54, 0xFC);
1054 write_nic_word(dev, 0x37C, 0x00FC);
1059 printk("SetZebraRFPowerState8185(): unknown state to set: 0x%X!!!\n", eRFPowerState);
1065 btConfig3 &= ~(CONFIG3_PARM_En);
1066 write_nic_byte(dev, CONFIG3, btConfig3);
1068 btCR9346 &= ~(0xC0);
1069 write_nic_byte(dev, CR9346, btCR9346);
1071 if (bResult && bActionAllowed)
1072 priv->eRFPowerState = eRFPowerState;
1074 priv->SetRFPowerStateInProgress = false;
1076 return bResult && bActionAllowed;
1079 void rtl8225z4_rf_sleep(struct net_device *dev)
1081 MgntActSet_RF_State(dev, eRfSleep, RF_CHANGE_BY_PS);
1084 void rtl8225z4_rf_wakeup(struct net_device *dev)
1086 MgntActSet_RF_State(dev, eRfOn, RF_CHANGE_BY_PS);