]> git.karo-electronics.de Git - mv-sheeva.git/blob - drivers/staging/rtl8187se/r8180_rtl8225.c
a132f6a36a62999c71c1d0cf09f63de005180204
[mv-sheeva.git] / drivers / staging / rtl8187se / r8180_rtl8225.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 <andreamrl@tiscali.it>
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
13
14
15 #include "r8180_hw.h"
16 #include "r8180_rtl8225.h"
17
18
19 u8 rtl8225_gain[]={
20         0x23,0x88,0x7c,0xa5,// -82dbm
21         0x23,0x88,0x7c,0xb5,// -82dbm
22         0x23,0x88,0x7c,0xc5,// -82dbm
23         0x33,0x80,0x79,0xc5,// -78dbm
24         0x43,0x78,0x76,0xc5,// -74dbm
25         0x53,0x60,0x73,0xc5,// -70dbm
26         0x63,0x58,0x70,0xc5,// -66dbm
27 };
28
29 u32 rtl8225_chan[] ={
30               0,
31                 0x0080, //ch1
32                 0x0100, //ch2
33                 0x0180, //ch3
34                 0x0200, //ch4
35                 0x0280,
36                 0x0300,
37                 0x0380,
38                 0x0400,
39                 0x0480,
40                 0x0500,
41                 0x0580,
42                 0x0600,
43                 0x0680,
44                 0x074A, //ch14
45 };
46
47 u16 rtl8225bcd_rxgain[]={
48         0x0400, 0x0401, 0x0402, 0x0403, 0x0404, 0x0405, 0x0408, 0x0409,
49         0x040a, 0x040b, 0x0502, 0x0503, 0x0504, 0x0505, 0x0540, 0x0541,
50         0x0542, 0x0543, 0x0544, 0x0545, 0x0580, 0x0581, 0x0582, 0x0583,
51         0x0584, 0x0585, 0x0588, 0x0589, 0x058a, 0x058b, 0x0643, 0x0644,
52         0x0645, 0x0680, 0x0681, 0x0682, 0x0683, 0x0684, 0x0685, 0x0688,
53         0x0689, 0x068a, 0x068b, 0x068c, 0x0742, 0x0743, 0x0744, 0x0745,
54         0x0780, 0x0781, 0x0782, 0x0783, 0x0784, 0x0785, 0x0788, 0x0789,
55         0x078a, 0x078b, 0x078c, 0x078d, 0x0790, 0x0791, 0x0792, 0x0793,
56         0x0794, 0x0795, 0x0798, 0x0799, 0x079a, 0x079b, 0x079c, 0x079d,
57         0x07a0, 0x07a1, 0x07a2, 0x07a3, 0x07a4, 0x07a5, 0x07a8, 0x07a9,
58         0x07aa, 0x07ab, 0x07ac, 0x07ad, 0x07b0, 0x07b1, 0x07b2, 0x07b3,
59         0x07b4, 0x07b5, 0x07b8, 0x07b9, 0x07ba, 0x07bb, 0x07bb
60
61 };
62
63 u8 rtl8225_agc[]={
64         0x9e,0x9e,0x9e,0x9e,0x9e,0x9e,0x9e,0x9e,0x9d,0x9c,0x9b,0x9a,0x99,0x98,0x97,0x96,
65         0x95,0x94,0x93,0x92,0x91,0x90,0x8f,0x8e,0x8d,0x8c,0x8b,0x8a,0x89,0x88,0x87,0x86,
66         0x85,0x84,0x83,0x82,0x81,0x80,0x3f,0x3e,0x3d,0x3c,0x3b,0x3a,0x39,0x38,0x37,0x36,
67         0x35,0x34,0x33,0x32,0x31,0x30,0x2f,0x2e,0x2d,0x2c,0x2b,0x2a,0x29,0x28,0x27,0x26,
68         0x25,0x24,0x23,0x22,0x21,0x20,0x1f,0x1e,0x1d,0x1c,0x1b,0x1a,0x19,0x18,0x17,0x16,
69         0x15,0x14,0x13,0x12,0x11,0x10,0x0f,0x0e,0x0d,0x0c,0x0b,0x0a,0x09,0x08,0x07,0x06,
70         0x05,0x04,0x03,0x02,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
71         0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,0x01,
72 };
73
74
75 u8 rtl8225_tx_gain_cck_ofdm[]={
76         0x02,0x06,0x0e,0x1e,0x3e,0x7e
77 };
78
79
80 u8 rtl8225_tx_power_ofdm[]={
81         0x80,0x90,0xa2,0xb5,0xcb,0xe4
82 };
83
84
85 u8 rtl8225_tx_power_cck_ch14[]={
86         0x18,0x17,0x15,0x0c,0x00,0x00,0x00,0x00,
87         0x1b,0x1a,0x17,0x0e,0x00,0x00,0x00,0x00,
88         0x1f,0x1e,0x1a,0x0f,0x00,0x00,0x00,0x00,
89         0x22,0x21,0x1d,0x11,0x00,0x00,0x00,0x00,
90         0x26,0x25,0x21,0x13,0x00,0x00,0x00,0x00,
91         0x2b,0x2a,0x25,0x15,0x00,0x00,0x00,0x00
92 };
93
94
95 u8 rtl8225_tx_power_cck[]={
96         0x18,0x17,0x15,0x11,0x0c,0x08,0x04,0x02,
97         0x1b,0x1a,0x17,0x13,0x0e,0x09,0x04,0x02,
98         0x1f,0x1e,0x1a,0x15,0x10,0x0a,0x05,0x02,
99         0x22,0x21,0x1d,0x18,0x11,0x0b,0x06,0x02,
100         0x26,0x25,0x21,0x1b,0x14,0x0d,0x06,0x03,
101         0x2b,0x2a,0x25,0x1e,0x16,0x0e,0x07,0x03
102 };
103
104
105 void rtl8225_set_gain(struct net_device *dev, short gain)
106 {
107         write_phy_ofdm(dev, 0x0d, rtl8225_gain[gain * 4]);
108         write_phy_ofdm(dev, 0x23, rtl8225_gain[gain * 4 + 1]);
109         write_phy_ofdm(dev, 0x1b, rtl8225_gain[gain * 4 + 2]);
110         write_phy_ofdm(dev, 0x1d, rtl8225_gain[gain * 4 + 3]);
111 }
112
113 void write_rtl8225(struct net_device *dev, u8 adr, u16 data)
114 {
115         int i;
116         u16 out,select;
117         u8 bit;
118         u32 bangdata = (data << 4) | (adr & 0xf);
119         struct r8180_priv *priv = ieee80211_priv(dev);
120
121         out = read_nic_word(dev, RFPinsOutput) & 0xfff3;
122
123         write_nic_word(dev,RFPinsEnable,
124                 (read_nic_word(dev,RFPinsEnable) | 0x7));
125
126         select = read_nic_word(dev, RFPinsSelect);
127
128         write_nic_word(dev, RFPinsSelect, select | 0x7 |
129                 ((priv->card_type == USB) ? 0 : SW_CONTROL_GPIO));
130
131         force_pci_posting(dev);
132         udelay(10);
133
134         write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN );//| 0x1fff);
135
136         force_pci_posting(dev);
137         udelay(2);
138
139         write_nic_word(dev, RFPinsOutput, out);
140
141         force_pci_posting(dev);
142         udelay(10);
143
144
145         for(i=15; i>=0;i--){
146
147                 bit = (bangdata & (1<<i)) >> i;
148
149                 write_nic_word(dev, RFPinsOutput, bit | out);
150
151                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
152                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
153
154                 i--;
155                 bit = (bangdata & (1<<i)) >> i;
156
157                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
158                 write_nic_word(dev, RFPinsOutput, bit | out | BB_HOST_BANG_CLK);
159
160                 write_nic_word(dev, RFPinsOutput, bit | out);
161
162         }
163
164         write_nic_word(dev, RFPinsOutput, out | BB_HOST_BANG_EN);
165
166         force_pci_posting(dev);
167         udelay(10);
168
169         write_nic_word(dev, RFPinsOutput, out |
170                 ((priv->card_type == USB) ? 4 : BB_HOST_BANG_EN));
171
172         write_nic_word(dev, RFPinsSelect, select |
173                 ((priv->card_type == USB) ? 0 : SW_CONTROL_GPIO));
174
175         if(priv->card_type == USB)
176                 mdelay(2);
177         else
178                 rtl8185_rf_pins_enable(dev);
179 }
180
181 void rtl8225_SetTXPowerLevel(struct net_device *dev, short ch)
182 {
183         struct r8180_priv *priv = ieee80211_priv(dev);
184
185         int GainIdx;
186         int GainSetting;
187         int i;
188         u8 power;
189         u8 *cck_power_table;
190         u8 max_cck_power_level;
191         u8 max_ofdm_power_level;
192         u8 min_ofdm_power_level;
193         u8 cck_power_level = 0xff & priv->chtxpwr[ch];
194         u8 ofdm_power_level = 0xff & priv->chtxpwr_ofdm[ch];
195
196         if(priv->card_type == USB){
197                 max_cck_power_level = 11;
198                 max_ofdm_power_level = 25; //  12 -> 25
199                 min_ofdm_power_level = 10;
200         }else{
201                 max_cck_power_level = 35;
202                 max_ofdm_power_level = 35;
203                 min_ofdm_power_level = 0;
204         }
205         /* CCK power setting */
206         if(cck_power_level > max_cck_power_level)
207                 cck_power_level = max_cck_power_level;
208         GainIdx=cck_power_level % 6;
209         GainSetting=cck_power_level / 6;
210
211         if(ch == 14)
212                 cck_power_table = rtl8225_tx_power_cck_ch14;
213         else
214                 cck_power_table = rtl8225_tx_power_cck;
215
216 //      if(priv->card_8185 == 1 && priv->card_8185_Bversion ){
217                 /*Ver B*/
218 //              write_nic_byte(dev, TX_GAIN_CCK, rtl8225_tx_gain_cck_ofdm[GainSetting]);
219 //      }else{
220                 /*Ver C - D */
221         write_nic_byte(dev, TX_GAIN_CCK, rtl8225_tx_gain_cck_ofdm[GainSetting]>>1);
222 //      }
223
224         for(i=0;i<8;i++){
225
226                 power = cck_power_table[GainIdx * 8 + i];
227                 write_phy_cck(dev, 0x44 + i, power);
228         }
229
230         /* FIXME Is this delay really needeed ? */
231         force_pci_posting(dev);
232         mdelay(1);
233
234         /* OFDM power setting */
235 //  Old:
236 //      if(ofdm_power_level > max_ofdm_power_level)
237 //              ofdm_power_level = 35;
238 //      ofdm_power_level += min_ofdm_power_level;
239 //  Latest:
240         if(ofdm_power_level > (max_ofdm_power_level - min_ofdm_power_level))
241                 ofdm_power_level = max_ofdm_power_level;
242         else
243                 ofdm_power_level += min_ofdm_power_level;
244         if(ofdm_power_level > 35)
245                 ofdm_power_level = 35;
246 //
247
248         GainIdx=ofdm_power_level % 6;
249         GainSetting=ofdm_power_level / 6;
250 #if 1
251 //      if(priv->card_type == USB){
252                 rtl8185_set_anaparam2(dev,RTL8225_ANAPARAM2_ON);
253
254                 write_phy_ofdm(dev,2,0x42);
255                 write_phy_ofdm(dev,6,0);
256                 write_phy_ofdm(dev,8,0);
257 //      }
258 #endif
259 //      if(priv->card_8185 == 1 && priv->card_8185_Bversion){
260 //              /*Ver B*/
261 //              write_nic_byte(dev, TX_GAIN_OFDM, rtl8225_tx_gain_cck_ofdm[GainSetting]);
262 //      }else{
263                 /*Ver C - D */
264         write_nic_byte(dev, TX_GAIN_OFDM, rtl8225_tx_gain_cck_ofdm[GainSetting]>>1);
265 //      }
266
267
268         power = rtl8225_tx_power_ofdm[GainIdx];
269
270         write_phy_ofdm(dev, 0x5, power);
271         write_phy_ofdm(dev, 0x7, power);
272
273         force_pci_posting(dev);
274         mdelay(1);
275         //write_nic_byte(dev, TX_AGC_CONTROL,4);
276 }
277
278 void rtl8225_rf_set_chan(struct net_device *dev, short ch)
279 {
280         struct r8180_priv *priv = ieee80211_priv(dev);
281         short gset = (priv->ieee80211->state == IEEE80211_LINKED &&
282                 ieee80211_is_54g(priv->ieee80211->current_network)) ||
283                 priv->ieee80211->iw_mode == IW_MODE_MONITOR;
284
285         rtl8225_SetTXPowerLevel(dev, ch);
286
287         write_rtl8225(dev, 0x7, rtl8225_chan[ch]);
288
289         force_pci_posting(dev);
290         mdelay(10);
291
292         // A mode sifs 0x44, difs 34-14, slot 9, eifs 23, cwm 3, cwM 7, ctstoself 0x10
293         if(gset){
294                 write_nic_byte(dev,SIFS,0x22);// SIFS: 0x22
295                 write_nic_byte(dev,DIFS,0x14); //DIFS: 20
296                 //write_nic_byte(dev,DIFS,20); //DIFS: 20
297         }else{
298                 write_nic_byte(dev,SIFS,0x44);// SIFS: 0x22
299                 write_nic_byte(dev,DIFS,50 - 14); //DIFS: 36
300         }
301         if(priv->ieee80211->state == IEEE80211_LINKED &&
302                 ieee80211_is_shortslot(priv->ieee80211->current_network))
303                 write_nic_byte(dev,SLOT,0x9); //SLOT: 9
304
305         else
306                 write_nic_byte(dev,SLOT,0x14); //SLOT: 20 (0x14)
307
308
309         if(gset){
310                 write_nic_byte(dev,EIFS,81);//91 - 20); // EIFS: 91 (0x5B)
311                 write_nic_byte(dev,CW_VAL,0x73); //CW VALUE: 0x37
312                 //DMESG("using G net params");
313         }else{
314                 write_nic_byte(dev,EIFS,81); // EIFS: 91 (0x5B)
315                 write_nic_byte(dev,CW_VAL,0xa5); //CW VALUE: 0x37
316                 //DMESG("using B net params");
317         }
318
319
320 }
321
322 void rtl8225_host_pci_init(struct net_device *dev)
323 {
324         write_nic_word(dev, RFPinsOutput, 0x480);
325
326         rtl8185_rf_pins_enable(dev);
327
328         //if(priv->card_8185 == 2 && priv->enable_gpio0 ) /* version D */
329         //write_nic_word(dev, RFPinsSelect, 0x88);
330         //else
331         write_nic_word(dev, RFPinsSelect, 0x88 | SW_CONTROL_GPIO); /* 0x488 | SW_CONTROL_GPIO */
332
333         write_nic_byte(dev, GP_ENABLE, 0);
334
335         force_pci_posting(dev);
336         mdelay(200);
337
338         write_nic_word(dev, GP_ENABLE, 0xff & (~(1<<6))); /* bit 6 is for RF on/off detection */
339
340
341 }
342
343 void rtl8225_host_usb_init(struct net_device *dev)
344 {
345 }