Added a new debugging message for AMRR statistic
[ralink_drivers/rt2860_fbsd8.git] / rt2860_rf.c
blobde95a2b57896d2e1d749fe29f8d8d3611ab2059e
2 /*-
3 * Copyright (c) 2009-2010 Alexander Egorenkov <egorenar@gmail.com>
4 * Copyright (c) 2009 Damien Bergamini <damien.bergamini@free.fr>
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
19 #include "rt2860_rf.h"
20 #include "rt2860_reg.h"
21 #include "rt2860_eeprom.h"
22 #include "rt2860_io.h"
23 #include "rt2860_debug.h"
26 * Static variables
29 static const struct rt2860_rf_prog
31 uint8_t chan;
32 uint32_t r1, r2, r3, r4;
33 } rt2860_rf_2850[] =
35 { 1, 0x98402ecc, 0x984c0786, 0x9816b455, 0x9800510b },
36 { 2, 0x98402ecc, 0x984c0786, 0x98168a55, 0x9800519f },
37 { 3, 0x98402ecc, 0x984c078a, 0x98168a55, 0x9800518b },
38 { 4, 0x98402ecc, 0x984c078a, 0x98168a55, 0x9800519f },
39 { 5, 0x98402ecc, 0x984c078e, 0x98168a55, 0x9800518b },
40 { 6, 0x98402ecc, 0x984c078e, 0x98168a55, 0x9800519f },
41 { 7, 0x98402ecc, 0x984c0792, 0x98168a55, 0x9800518b },
42 { 8, 0x98402ecc, 0x984c0792, 0x98168a55, 0x9800519f },
43 { 9, 0x98402ecc, 0x984c0796, 0x98168a55, 0x9800518b },
44 { 10, 0x98402ecc, 0x984c0796, 0x98168a55, 0x9800519f },
45 { 11, 0x98402ecc, 0x984c079a, 0x98168a55, 0x9800518b },
46 { 12, 0x98402ecc, 0x984c079a, 0x98168a55, 0x9800519f },
47 { 13, 0x98402ecc, 0x984c079e, 0x98168a55, 0x9800518b },
48 { 14, 0x98402ecc, 0x984c07a2, 0x98168a55, 0x98005193 },
49 { 36, 0x98402ecc, 0x984c099a, 0x98158a55, 0x980ed1a3 },
50 { 38, 0x98402ecc, 0x984c099e, 0x98158a55, 0x980ed193 },
51 { 40, 0x98402ec8, 0x984c0682, 0x98158a55, 0x980ed183 },
52 { 44, 0x98402ec8, 0x984c0682, 0x98158a55, 0x980ed1a3 },
53 { 46, 0x98402ec8, 0x984c0686, 0x98158a55, 0x980ed18b },
54 { 48, 0x98402ec8, 0x984c0686, 0x98158a55, 0x980ed19b },
55 { 52, 0x98402ec8, 0x984c068a, 0x98158a55, 0x980ed193 },
56 { 54, 0x98402ec8, 0x984c068a, 0x98158a55, 0x980ed1a3 },
57 { 56, 0x98402ec8, 0x984c068e, 0x98158a55, 0x980ed18b },
58 { 60, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed183 },
59 { 62, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed193 },
60 { 64, 0x98402ec8, 0x984c0692, 0x98158a55, 0x980ed1a3 },
61 { 100, 0x98402ec8, 0x984c06b2, 0x98178a55, 0x980ed783 },
62 { 102, 0x98402ec8, 0x985c06b2, 0x98578a55, 0x980ed793 },
63 { 104, 0x98402ec8, 0x985c06b2, 0x98578a55, 0x980ed1a3 },
64 { 108, 0x98402ecc, 0x985c0a32, 0x98578a55, 0x980ed193 },
65 { 110, 0x98402ecc, 0x984c0a36, 0x98178a55, 0x980ed183 },
66 { 112, 0x98402ecc, 0x984c0a36, 0x98178a55, 0x980ed19b },
67 { 116, 0x98402ecc, 0x984c0a3a, 0x98178a55, 0x980ed1a3 },
68 { 118, 0x98402ecc, 0x984c0a3e, 0x98178a55, 0x980ed193 },
69 { 120, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed183 },
70 { 124, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed193 },
71 { 126, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed15b },
72 { 128, 0x98402ec4, 0x984c0382, 0x98178a55, 0x980ed1a3 },
73 { 132, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed18b },
74 { 134, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed193 },
75 { 136, 0x98402ec4, 0x984c0386, 0x98178a55, 0x980ed19b },
76 { 140, 0x98402ec4, 0x984c038a, 0x98178a55, 0x980ed183 },
77 { 149, 0x98402ec4, 0x984c038a, 0x98178a55, 0x980ed1a7 },
78 { 151, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed187 },
79 { 153, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed18f },
80 { 157, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed19f },
81 { 159, 0x98402ec4, 0x984c038e, 0x98178a55, 0x980ed1a7 },
82 { 161, 0x98402ec4, 0x984c0392, 0x98178a55, 0x980ed187 },
83 { 165, 0x98402ec4, 0x984c0392, 0x98178a55, 0x980ed197 },
84 { 184, 0x95002ccc, 0x9500491e, 0x9509be55, 0x950c0a0b },
85 { 188, 0x95002ccc, 0x95004922, 0x9509be55, 0x950c0a13 },
86 { 192, 0x95002ccc, 0x95004926, 0x9509be55, 0x950c0a1b },
87 { 196, 0x95002ccc, 0x9500492a, 0x9509be55, 0x950c0a23 },
88 { 208, 0x95002ccc, 0x9500493a, 0x9509be55, 0x950c0a13 },
89 { 212, 0x95002ccc, 0x9500493e, 0x9509be55, 0x950c0a1b },
90 { 216, 0x95002ccc, 0x95004982, 0x9509be55, 0x950c0a23 },
94 * rt2860_rf_name
96 const char *rt2860_rf_name(int rf_rev)
98 switch (rf_rev)
100 case RT2860_EEPROM_RF_2820:
101 return "RT2820";
103 case RT2860_EEPROM_RF_2850:
104 return "RT2850";
106 case RT2860_EEPROM_RF_2720:
107 return "RT2720";
109 case RT2860_EEPROM_RF_2750:
110 return "RT2750";
112 default:
113 return "unknown";
118 * rt2860_rf_select_chan_group
120 void rt2860_rf_select_chan_group(struct rt2860_softc *sc,
121 struct ieee80211_channel *c)
123 struct ifnet *ifp;
124 struct ieee80211com *ic;
125 int chan, group;
126 uint32_t tmp;
128 ifp = sc->ifp;
129 ic = ifp->if_l2com;
131 chan = ieee80211_chan2ieee(ic, c);
132 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
133 return;
135 if (chan <= 14)
136 group = 0;
137 else if (chan <= 64)
138 group = 1;
139 else if (chan <= 128)
140 group = 2;
141 else
142 group = 3;
144 rt2860_io_bbp_write(sc, 62, 0x37 - sc->lna_gain[group]);
145 rt2860_io_bbp_write(sc, 63, 0x37 - sc->lna_gain[group]);
146 rt2860_io_bbp_write(sc, 64, 0x37 - sc->lna_gain[group]);
147 rt2860_io_bbp_write(sc, 86, 0x00);
149 if (group == 0)
151 if (sc->ext_lna_2ghz)
153 rt2860_io_bbp_write(sc, 82, 0x62);
154 rt2860_io_bbp_write(sc, 75, 0x46);
156 else
158 rt2860_io_bbp_write(sc, 82, 0x84);
159 rt2860_io_bbp_write(sc, 75, 0x50);
162 else
164 rt2860_io_bbp_write(sc, 82, 0xf2);
166 if (sc->ext_lna_5ghz)
167 rt2860_io_bbp_write(sc, 75, 0x46);
168 else
169 rt2860_io_bbp_write(sc, 75, 0x50);
172 if (group == 0)
174 tmp = 0x2e + sc->lna_gain[group];
176 else
178 if ((ic->ic_flags & IEEE80211_F_SCAN) || !IEEE80211_IS_CHAN_HT40(c))
179 tmp = 0x32 + sc->lna_gain[group] * 5 / 3;
180 else
181 tmp = 0x3a + sc->lna_gain[group] * 5 / 3;
184 rt2860_io_bbp_write(sc, 66, tmp);
186 tmp = RT2860_REG_RFTR_ENABLE | RT2860_REG_TRSW_ENABLE;
188 if (group == 0)
190 tmp |= RT2860_REG_PA_PE_G0_ENABLE | RT2860_REG_LNA_PE_G0_ENABLE;
192 if (sc->ntxpath > 1)
193 tmp |= RT2860_REG_PA_PE_G1_ENABLE;
195 if (sc->nrxpath > 1)
196 tmp |= RT2860_REG_LNA_PE_G1_ENABLE;
198 else
200 tmp |= RT2860_REG_PA_PE_A0_ENABLE | RT2860_REG_LNA_PE_A0_ENABLE;
202 if (sc->ntxpath > 1)
203 tmp |= RT2860_REG_PA_PE_A1_ENABLE;
205 if (sc->nrxpath > 1)
206 tmp |= RT2860_REG_LNA_PE_A1_ENABLE;
209 rt2860_io_mac_write(sc, RT2860_REG_TX_PIN_CFG, tmp);
211 tmp = rt2860_io_mac_read(sc, RT2860_REG_TX_BAND_CFG);
213 tmp &= ~(RT2860_REG_TX_BAND_BG | RT2860_REG_TX_BAND_A | RT2860_REG_TX_BAND_HT40_ABOVE);
215 if (group == 0)
216 tmp |= RT2860_REG_TX_BAND_BG;
217 else
218 tmp |= RT2860_REG_TX_BAND_A;
220 /* set central channel position */
222 if (IEEE80211_IS_CHAN_HT40U(c))
223 tmp |= RT2860_REG_TX_BAND_HT40_BELOW;
224 else if (IEEE80211_IS_CHAN_HT40D(c))
225 tmp |= RT2860_REG_TX_BAND_HT40_ABOVE;
226 else
227 tmp |= RT2860_REG_TX_BAND_HT40_BELOW;
229 rt2860_io_mac_write(sc, RT2860_REG_TX_BAND_CFG, tmp);
231 /* set bandwidth (20MHz or 40MHz) */
233 tmp = rt2860_io_bbp_read(sc, 4);
235 tmp &= ~0x18;
237 if (IEEE80211_IS_CHAN_HT40(c))
238 tmp |= 0x10;
240 rt2860_io_bbp_write(sc, 4, tmp);
242 /* set central channel position */
244 tmp = rt2860_io_bbp_read(sc, 3);
246 tmp &= ~0x20;
248 if (IEEE80211_IS_CHAN_HT40D(c))
249 tmp |= 0x20;
251 rt2860_io_bbp_write(sc, 3, tmp);
253 if (sc->mac_rev == 0x28600100)
255 if (!IEEE80211_IS_CHAN_HT40(c))
257 rt2860_io_bbp_write(sc, 69, 0x16);
258 rt2860_io_bbp_write(sc, 70, 0x08);
259 rt2860_io_bbp_write(sc, 73, 0x12);
261 else
263 rt2860_io_bbp_write(sc, 69, 0x1a);
264 rt2860_io_bbp_write(sc, 70, 0x0a);
265 rt2860_io_bbp_write(sc, 73, 0x16);
271 * rt2860_rf_set_chan
273 void rt2860_rf_set_chan(struct rt2860_softc *sc,
274 struct ieee80211_channel *c)
276 struct ifnet *ifp;
277 struct ieee80211com *ic;
278 const struct rt2860_rf_prog *prog;
279 uint32_t r1, r2, r3, r4;
280 int8_t txpow1, txpow2;
281 int i, chan;
283 ifp = sc->ifp;
284 ic = ifp->if_l2com;
285 prog = rt2860_rf_2850;
287 /* get central channel position */
289 chan = ieee80211_chan2ieee(ic, c);
291 if (IEEE80211_IS_CHAN_HT40U(c))
292 chan += 2;
293 else if (IEEE80211_IS_CHAN_HT40D(c))
294 chan -= 2;
296 RT2860_DPRINTF(sc, RT2860_DEBUG_CHAN,
297 "%s: RF set channel: channel=%u, HT%s%s\n",
298 device_get_nameunit(sc->dev),
299 ieee80211_chan2ieee(ic, c),
300 !IEEE80211_IS_CHAN_HT(c) ? " disabled" :
301 IEEE80211_IS_CHAN_HT20(c) ? "20":
302 IEEE80211_IS_CHAN_HT40U(c) ? "40U" : "40D",
303 (ic->ic_flags & IEEE80211_F_SCAN) ? ", scanning" : "");
305 if (chan == 0 || chan == IEEE80211_CHAN_ANY)
306 return;
308 for (i = 0; prog[i].chan != chan; i++);
310 r1 = prog[i].r1;
311 r2 = prog[i].r2;
312 r3 = prog[i].r3;
313 r4 = prog[i].r4;
315 txpow1 = sc->txpow1[i];
316 txpow2 = sc->txpow2[i];
318 if (sc->ntxpath == 1)
319 r2 |= (1 << 14);
321 if (sc->nrxpath == 2)
322 r2 |= (1 << 6);
323 else if (sc->nrxpath == 1)
324 r2 |= (1 << 17) | (1 << 6);
326 if (IEEE80211_IS_CHAN_2GHZ(c))
328 r3 = (r3 & 0xffffc1ff) | (txpow1 << 9);
329 r4 = (r4 & ~0x001f87c0) | (sc->rf_freq_off << 15) | (txpow2 << 6);
331 else
333 r3 = r3 & 0xffffc1ff;
334 r4 = (r4 & ~0x001f87c0) | (sc->rf_freq_off << 15);
336 if (txpow1 >= RT2860_EEPROM_TXPOW_5GHZ_MIN && txpow1 < 0)
338 txpow1 = (-RT2860_EEPROM_TXPOW_5GHZ_MIN + txpow1);
339 if (txpow1 > RT2860_EEPROM_TXPOW_5GHZ_MAX)
340 txpow1 = RT2860_EEPROM_TXPOW_5GHZ_MAX;
342 r3 |= (txpow1 << 10);
344 else
346 if (txpow1 > RT2860_EEPROM_TXPOW_5GHZ_MAX)
347 txpow1 = RT2860_EEPROM_TXPOW_5GHZ_MAX;
349 r3 |= (txpow1 << 10) | (1 << 9);
352 if (txpow2 >= RT2860_EEPROM_TXPOW_5GHZ_MIN && txpow2 < 0)
354 txpow2 = (-RT2860_EEPROM_TXPOW_5GHZ_MIN + txpow2);
355 if (txpow2 > RT2860_EEPROM_TXPOW_5GHZ_MAX)
356 txpow2 = RT2860_EEPROM_TXPOW_5GHZ_MAX;
358 r4 |= (txpow2 << 7);
360 else
362 if (txpow2 > RT2860_EEPROM_TXPOW_5GHZ_MAX)
363 txpow2 = RT2860_EEPROM_TXPOW_5GHZ_MAX;
365 r4 |= (txpow2 << 7) | (1 << 6);
369 if (!(ic->ic_flags & IEEE80211_F_SCAN) && IEEE80211_IS_CHAN_HT40(c))
370 r4 |= (1 << 21);
372 rt2860_io_rf_write(sc, RT2860_REG_RF_R1, r1);
373 rt2860_io_rf_write(sc, RT2860_REG_RF_R2, r2);
374 rt2860_io_rf_write(sc, RT2860_REG_RF_R3, r3 & ~(1 << 2));
375 rt2860_io_rf_write(sc, RT2860_REG_RF_R4, r4);
377 DELAY(200);
379 rt2860_io_rf_write(sc, RT2860_REG_RF_R1, r1);
380 rt2860_io_rf_write(sc, RT2860_REG_RF_R2, r2);
381 rt2860_io_rf_write(sc, RT2860_REG_RF_R3, r3 | (1 << 2));
382 rt2860_io_rf_write(sc, RT2860_REG_RF_R4, r4);
384 DELAY(200);
386 rt2860_io_rf_write(sc, RT2860_REG_RF_R1, r1);
387 rt2860_io_rf_write(sc, RT2860_REG_RF_R2, r2);
388 rt2860_io_rf_write(sc, RT2860_REG_RF_R3, r3 & ~(1 << 2));
389 rt2860_io_rf_write(sc, RT2860_REG_RF_R4, r4);
391 rt2860_rf_select_chan_group(sc, c);
393 DELAY(1000);