blob: 41aea6a49af91ec949cde29af48f929cc9075ff2 [file] [log] [blame]
Michael Buesch424047e2008-01-09 16:13:56 +01001/*
2
3 Broadcom B43 wireless driver
4 IEEE 802.11n PHY support
5
6 Copyright (c) 2008 Michael Buesch <mb@bu3sch.de>
7
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
21 Boston, MA 02110-1301, USA.
22
23*/
24
John W. Linville819d7722008-01-17 16:57:10 -050025#include <linux/delay.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090026#include <linux/slab.h>
John W. Linville819d7722008-01-17 16:57:10 -050027#include <linux/types.h>
28
Michael Buesch424047e2008-01-09 16:13:56 +010029#include "b43.h"
Michael Buesch3d0da752008-08-30 02:27:19 +020030#include "phy_n.h"
Michael Buesch53a6e232008-01-13 21:23:44 +010031#include "tables_nphy.h"
Rafał Miłecki6db507f2010-10-14 19:33:36 +020032#include "radio_2055.h"
Rafał Miłecki5161bec2010-10-14 21:16:33 +020033#include "radio_2056.h"
Rafał Miłeckibbec3982010-01-15 14:31:39 +010034#include "main.h"
Michael Buesch424047e2008-01-09 16:13:56 +010035
Rafał Miłeckif8187b52010-01-15 12:34:21 +010036struct nphy_txgains {
37 u16 txgm[2];
38 u16 pga[2];
39 u16 pad[2];
40 u16 ipa[2];
41};
42
43struct nphy_iqcal_params {
44 u16 txgm;
45 u16 pga;
46 u16 pad;
47 u16 ipa;
48 u16 cal_gain;
49 u16 ncorr[5];
50};
51
52struct nphy_iq_est {
53 s32 iq0_prod;
54 u32 i0_pwr;
55 u32 q0_pwr;
56 s32 iq1_prod;
57 u32 i1_pwr;
58 u32 q1_pwr;
59};
Michael Buesch424047e2008-01-09 16:13:56 +010060
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +010061enum b43_nphy_rf_sequence {
62 B43_RFSEQ_RX2TX,
63 B43_RFSEQ_TX2RX,
64 B43_RFSEQ_RESET2RX,
65 B43_RFSEQ_UPDATE_GAINH,
66 B43_RFSEQ_UPDATE_GAINL,
67 B43_RFSEQ_UPDATE_GAINU,
68};
69
Rafał Miłecki76b002b2010-11-30 22:33:15 +010070enum b43_nphy_rssi_type {
71 B43_NPHY_RSSI_X = 0,
72 B43_NPHY_RSSI_Y,
73 B43_NPHY_RSSI_Z,
74 B43_NPHY_RSSI_PWRDET,
75 B43_NPHY_RSSI_TSSI_I,
76 B43_NPHY_RSSI_TSSI_Q,
77 B43_NPHY_RSSI_TBD,
78};
79
Rafał Miłecki161d5402010-11-28 12:59:43 +010080static void b43_nphy_stay_in_carrier_search(struct b43_wldev *dev,
81 bool enable);
Rafał Miłecki9501fef2010-01-30 20:18:07 +010082static void b43_nphy_set_rf_sequence(struct b43_wldev *dev, u8 cmd,
83 u8 *events, u8 *delays, u8 length);
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +010084static void b43_nphy_force_rf_sequence(struct b43_wldev *dev,
85 enum b43_nphy_rf_sequence seq);
Rafał Miłecki67cbc3e2010-02-04 12:23:08 +010086static void b43_nphy_rf_control_override(struct b43_wldev *dev, u16 field,
87 u16 value, u8 core, bool off);
88static void b43_nphy_rf_control_intc_override(struct b43_wldev *dev, u8 field,
89 u16 value, u8 core);
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +010090
Michael Buesch53a6e232008-01-13 21:23:44 +010091void b43_nphy_set_rxantenna(struct b43_wldev *dev, int antenna)
92{//TODO
93}
94
Michael Buesch18c8ade2008-08-28 19:33:40 +020095static void b43_nphy_op_adjust_txpower(struct b43_wldev *dev)
Michael Buesch53a6e232008-01-13 21:23:44 +010096{//TODO
97}
98
Michael Buesch18c8ade2008-08-28 19:33:40 +020099static enum b43_txpwr_result b43_nphy_op_recalc_txpower(struct b43_wldev *dev,
100 bool ignore_tssi)
101{//TODO
102 return B43_TXPWR_RES_DONE;
103}
104
Michael Bueschd1591312008-01-14 00:05:57 +0100105static void b43_chantab_radio_upload(struct b43_wldev *dev,
Rafał Miłeckif19ebe72010-03-29 00:53:15 +0200106 const struct b43_nphy_channeltab_entry_rev2 *e)
Michael Bueschd1591312008-01-14 00:05:57 +0100107{
Rafał Miłeckie5255ccc2010-02-27 13:03:35 +0100108 b43_radio_write(dev, B2055_PLL_REF, e->radio_pll_ref);
109 b43_radio_write(dev, B2055_RF_PLLMOD0, e->radio_rf_pllmod0);
110 b43_radio_write(dev, B2055_RF_PLLMOD1, e->radio_rf_pllmod1);
111 b43_radio_write(dev, B2055_VCO_CAPTAIL, e->radio_vco_captail);
112 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
113
114 b43_radio_write(dev, B2055_VCO_CAL1, e->radio_vco_cal1);
115 b43_radio_write(dev, B2055_VCO_CAL2, e->radio_vco_cal2);
116 b43_radio_write(dev, B2055_PLL_LFC1, e->radio_pll_lfc1);
117 b43_radio_write(dev, B2055_PLL_LFR1, e->radio_pll_lfr1);
118 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
119
120 b43_radio_write(dev, B2055_PLL_LFC2, e->radio_pll_lfc2);
121 b43_radio_write(dev, B2055_LGBUF_CENBUF, e->radio_lgbuf_cenbuf);
122 b43_radio_write(dev, B2055_LGEN_TUNE1, e->radio_lgen_tune1);
123 b43_radio_write(dev, B2055_LGEN_TUNE2, e->radio_lgen_tune2);
124 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
125
126 b43_radio_write(dev, B2055_C1_LGBUF_ATUNE, e->radio_c1_lgbuf_atune);
127 b43_radio_write(dev, B2055_C1_LGBUF_GTUNE, e->radio_c1_lgbuf_gtune);
128 b43_radio_write(dev, B2055_C1_RX_RFR1, e->radio_c1_rx_rfr1);
129 b43_radio_write(dev, B2055_C1_TX_PGAPADTN, e->radio_c1_tx_pgapadtn);
130 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
131
132 b43_radio_write(dev, B2055_C1_TX_MXBGTRIM, e->radio_c1_tx_mxbgtrim);
133 b43_radio_write(dev, B2055_C2_LGBUF_ATUNE, e->radio_c2_lgbuf_atune);
134 b43_radio_write(dev, B2055_C2_LGBUF_GTUNE, e->radio_c2_lgbuf_gtune);
135 b43_radio_write(dev, B2055_C2_RX_RFR1, e->radio_c2_rx_rfr1);
136 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
137
138 b43_radio_write(dev, B2055_C2_TX_PGAPADTN, e->radio_c2_tx_pgapadtn);
139 b43_radio_write(dev, B2055_C2_TX_MXBGTRIM, e->radio_c2_tx_mxbgtrim);
Michael Bueschd1591312008-01-14 00:05:57 +0100140}
141
Rafał Miłeckid4814e62010-12-21 23:57:48 +0100142static void b43_chantab_radio_2056_upload(struct b43_wldev *dev,
143 const struct b43_nphy_channeltab_entry_rev3 *e)
144{
145 b43_radio_write(dev, B2056_SYN_PLL_VCOCAL1, e->radio_syn_pll_vcocal1);
146 b43_radio_write(dev, B2056_SYN_PLL_VCOCAL2, e->radio_syn_pll_vcocal2);
147 b43_radio_write(dev, B2056_SYN_PLL_REFDIV, e->radio_syn_pll_refdiv);
148 b43_radio_write(dev, B2056_SYN_PLL_MMD2, e->radio_syn_pll_mmd2);
149 b43_radio_write(dev, B2056_SYN_PLL_MMD1, e->radio_syn_pll_mmd1);
150 b43_radio_write(dev, B2056_SYN_PLL_LOOPFILTER1,
151 e->radio_syn_pll_loopfilter1);
152 b43_radio_write(dev, B2056_SYN_PLL_LOOPFILTER2,
153 e->radio_syn_pll_loopfilter2);
154 b43_radio_write(dev, B2056_SYN_PLL_LOOPFILTER3,
155 e->radio_syn_pll_loopfilter3);
156 b43_radio_write(dev, B2056_SYN_PLL_LOOPFILTER4,
157 e->radio_syn_pll_loopfilter4);
158 b43_radio_write(dev, B2056_SYN_PLL_LOOPFILTER5,
159 e->radio_syn_pll_loopfilter5);
160 b43_radio_write(dev, B2056_SYN_RESERVED_ADDR27,
161 e->radio_syn_reserved_addr27);
162 b43_radio_write(dev, B2056_SYN_RESERVED_ADDR28,
163 e->radio_syn_reserved_addr28);
164 b43_radio_write(dev, B2056_SYN_RESERVED_ADDR29,
165 e->radio_syn_reserved_addr29);
166 b43_radio_write(dev, B2056_SYN_LOGEN_VCOBUF1,
167 e->radio_syn_logen_vcobuf1);
168 b43_radio_write(dev, B2056_SYN_LOGEN_MIXER2, e->radio_syn_logen_mixer2);
169 b43_radio_write(dev, B2056_SYN_LOGEN_BUF3, e->radio_syn_logen_buf3);
170 b43_radio_write(dev, B2056_SYN_LOGEN_BUF4, e->radio_syn_logen_buf4);
171
172 b43_radio_write(dev, B2056_RX0 | B2056_RX_LNAA_TUNE,
173 e->radio_rx0_lnaa_tune);
174 b43_radio_write(dev, B2056_RX0 | B2056_RX_LNAG_TUNE,
175 e->radio_rx0_lnag_tune);
176
177 b43_radio_write(dev, B2056_TX0 | B2056_TX_INTPAA_BOOST_TUNE,
178 e->radio_tx0_intpaa_boost_tune);
179 b43_radio_write(dev, B2056_TX0 | B2056_TX_INTPAG_BOOST_TUNE,
180 e->radio_tx0_intpag_boost_tune);
181 b43_radio_write(dev, B2056_TX0 | B2056_TX_PADA_BOOST_TUNE,
182 e->radio_tx0_pada_boost_tune);
183 b43_radio_write(dev, B2056_TX0 | B2056_TX_PADG_BOOST_TUNE,
184 e->radio_tx0_padg_boost_tune);
185 b43_radio_write(dev, B2056_TX0 | B2056_TX_PGAA_BOOST_TUNE,
186 e->radio_tx0_pgaa_boost_tune);
187 b43_radio_write(dev, B2056_TX0 | B2056_TX_PGAG_BOOST_TUNE,
188 e->radio_tx0_pgag_boost_tune);
189 b43_radio_write(dev, B2056_TX0 | B2056_TX_MIXA_BOOST_TUNE,
190 e->radio_tx0_mixa_boost_tune);
191 b43_radio_write(dev, B2056_TX0 | B2056_TX_MIXG_BOOST_TUNE,
192 e->radio_tx0_mixg_boost_tune);
193
194 b43_radio_write(dev, B2056_RX1 | B2056_RX_LNAA_TUNE,
195 e->radio_rx1_lnaa_tune);
196 b43_radio_write(dev, B2056_RX1 | B2056_RX_LNAG_TUNE,
197 e->radio_rx1_lnag_tune);
198
199 b43_radio_write(dev, B2056_TX1 | B2056_TX_INTPAA_BOOST_TUNE,
200 e->radio_tx1_intpaa_boost_tune);
201 b43_radio_write(dev, B2056_TX1 | B2056_TX_INTPAG_BOOST_TUNE,
202 e->radio_tx1_intpag_boost_tune);
203 b43_radio_write(dev, B2056_TX1 | B2056_TX_PADA_BOOST_TUNE,
204 e->radio_tx1_pada_boost_tune);
205 b43_radio_write(dev, B2056_TX1 | B2056_TX_PADG_BOOST_TUNE,
206 e->radio_tx1_padg_boost_tune);
207 b43_radio_write(dev, B2056_TX1 | B2056_TX_PGAA_BOOST_TUNE,
208 e->radio_tx1_pgaa_boost_tune);
209 b43_radio_write(dev, B2056_TX1 | B2056_TX_PGAG_BOOST_TUNE,
210 e->radio_tx1_pgag_boost_tune);
211 b43_radio_write(dev, B2056_TX1 | B2056_TX_MIXA_BOOST_TUNE,
212 e->radio_tx1_mixa_boost_tune);
213 b43_radio_write(dev, B2056_TX1 | B2056_TX_MIXG_BOOST_TUNE,
214 e->radio_tx1_mixg_boost_tune);
215}
216
217/* http://bcm-v4.sipsolutions.net/802.11/PHY/Radio/2056Setup */
218static void b43_radio_2056_setup(struct b43_wldev *dev,
219 const struct b43_nphy_channeltab_entry_rev3 *e)
220{
221 B43_WARN_ON(dev->phy.rev < 3);
222
223 b43_chantab_radio_2056_upload(dev, e);
224 /* TODO */
225 udelay(50);
226 /* VCO calibration */
227 b43_radio_write(dev, B2056_SYN_PLL_VCOCAL12, 0x00);
228 b43_radio_write(dev, B2056_TX_INTPAA_PA_MISC, 0x38);
229 b43_radio_write(dev, B2056_TX_INTPAA_PA_MISC, 0x18);
230 b43_radio_write(dev, B2056_TX_INTPAA_PA_MISC, 0x38);
231 b43_radio_write(dev, B2056_TX_INTPAA_PA_MISC, 0x39);
232 udelay(300);
233}
234
Michael Bueschd1591312008-01-14 00:05:57 +0100235static void b43_chantab_phy_upload(struct b43_wldev *dev,
Rafał Miłeckib15b3032010-03-29 00:53:13 +0200236 const struct b43_phy_n_sfo_cfg *e)
Michael Bueschd1591312008-01-14 00:05:57 +0100237{
238 b43_phy_write(dev, B43_NPHY_BW1A, e->phy_bw1a);
239 b43_phy_write(dev, B43_NPHY_BW2, e->phy_bw2);
240 b43_phy_write(dev, B43_NPHY_BW3, e->phy_bw3);
241 b43_phy_write(dev, B43_NPHY_BW4, e->phy_bw4);
242 b43_phy_write(dev, B43_NPHY_BW5, e->phy_bw5);
243 b43_phy_write(dev, B43_NPHY_BW6, e->phy_bw6);
244}
245
Rafał Miłecki161d5402010-11-28 12:59:43 +0100246/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxPwrCtrlEnable */
247static void b43_nphy_tx_power_ctrl(struct b43_wldev *dev, bool enable)
248{
249 struct b43_phy_n *nphy = dev->phy.n;
250 u8 i;
251 u16 tmp;
252
253 if (nphy->hang_avoid)
254 b43_nphy_stay_in_carrier_search(dev, 1);
255
256 nphy->txpwrctrl = enable;
257 if (!enable) {
258 if (dev->phy.rev >= 3)
259 ; /* TODO */
260
261 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x6840);
262 for (i = 0; i < 84; i++)
263 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0);
264
265 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x6C40);
266 for (i = 0; i < 84; i++)
267 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0);
268
269 tmp = B43_NPHY_TXPCTL_CMD_COEFF | B43_NPHY_TXPCTL_CMD_HWPCTLEN;
270 if (dev->phy.rev >= 3)
271 tmp |= B43_NPHY_TXPCTL_CMD_PCTLEN;
272 b43_phy_mask(dev, B43_NPHY_TXPCTL_CMD, ~tmp);
273
274 if (dev->phy.rev >= 3) {
275 b43_phy_set(dev, B43_NPHY_AFECTL_OVER1, 0x0100);
276 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x0100);
277 } else {
278 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x4000);
279 }
280
281 if (dev->phy.rev == 2)
282 b43_phy_maskset(dev, B43_NPHY_BPHY_CTL3,
283 ~B43_NPHY_BPHY_CTL3_SCALE, 0x53);
284 else if (dev->phy.rev < 2)
285 b43_phy_maskset(dev, B43_NPHY_BPHY_CTL3,
286 ~B43_NPHY_BPHY_CTL3_SCALE, 0x5A);
287
288 if (dev->phy.rev < 2 && 0)
289 ; /* TODO */
290 } else {
291 b43err(dev->wl, "enabling tx pwr ctrl not implemented yet\n");
292 }
293
294 if (nphy->hang_avoid)
295 b43_nphy_stay_in_carrier_search(dev, 0);
296}
297
298/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxPwrFix */
Michael Bueschd1591312008-01-14 00:05:57 +0100299static void b43_nphy_tx_power_fix(struct b43_wldev *dev)
300{
Rafał Miłecki161d5402010-11-28 12:59:43 +0100301 struct b43_phy_n *nphy = dev->phy.n;
Rafał Miłecki05814832011-05-18 02:06:39 +0200302 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Rafał Miłecki161d5402010-11-28 12:59:43 +0100303
304 u8 txpi[2], bbmult, i;
305 u16 tmp, radio_gain, dac_gain;
306 u16 freq = dev->phy.channel_freq;
307 u32 txgain;
308 /* u32 gaintbl; rev3+ */
309
310 if (nphy->hang_avoid)
311 b43_nphy_stay_in_carrier_search(dev, 1);
312
313 if (dev->phy.rev >= 3) {
314 txpi[0] = 40;
315 txpi[1] = 40;
316 } else if (sprom->revision < 4) {
317 txpi[0] = 72;
318 txpi[1] = 72;
319 } else {
320 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
321 txpi[0] = sprom->txpid2g[0];
322 txpi[1] = sprom->txpid2g[1];
323 } else if (freq >= 4900 && freq < 5100) {
324 txpi[0] = sprom->txpid5gl[0];
325 txpi[1] = sprom->txpid5gl[1];
326 } else if (freq >= 5100 && freq < 5500) {
327 txpi[0] = sprom->txpid5g[0];
328 txpi[1] = sprom->txpid5g[1];
329 } else if (freq >= 5500) {
330 txpi[0] = sprom->txpid5gh[0];
331 txpi[1] = sprom->txpid5gh[1];
332 } else {
333 txpi[0] = 91;
334 txpi[1] = 91;
335 }
336 }
337
338 /*
339 for (i = 0; i < 2; i++) {
340 nphy->txpwrindex[i].index_internal = txpi[i];
341 nphy->txpwrindex[i].index_internal_save = txpi[i];
342 }
343 */
344
345 for (i = 0; i < 2; i++) {
346 if (dev->phy.rev >= 3) {
Rafał Miłeckic7455cf2010-12-07 21:55:57 +0100347 /* FIXME: support 5GHz */
348 txgain = b43_ntab_tx_gain_rev3plus_2ghz[txpi[i]];
Rafał Miłecki161d5402010-11-28 12:59:43 +0100349 radio_gain = (txgain >> 16) & 0x1FFFF;
350 } else {
351 txgain = b43_ntab_tx_gain_rev0_1_2[txpi[i]];
352 radio_gain = (txgain >> 16) & 0x1FFF;
353 }
354
355 dac_gain = (txgain >> 8) & 0x3F;
356 bbmult = txgain & 0xFF;
357
358 if (dev->phy.rev >= 3) {
359 if (i == 0)
360 b43_phy_set(dev, B43_NPHY_AFECTL_OVER1, 0x0100);
361 else
362 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x0100);
363 } else {
364 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x4000);
365 }
366
367 if (i == 0)
368 b43_phy_write(dev, B43_NPHY_AFECTL_DACGAIN1, dac_gain);
369 else
370 b43_phy_write(dev, B43_NPHY_AFECTL_DACGAIN2, dac_gain);
371
372 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x1D10 + i);
373 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, radio_gain);
374
375 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x3C57);
376 tmp = b43_phy_read(dev, B43_NPHY_TABLE_DATALO);
377
378 if (i == 0)
379 tmp = (tmp & 0x00FF) | (bbmult << 8);
380 else
381 tmp = (tmp & 0xFF00) | bbmult;
382
383 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x3C57);
384 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, tmp);
385
386 if (0)
387 ; /* TODO */
388 }
389
390 b43_phy_mask(dev, B43_NPHY_BPHY_CTL2, ~B43_NPHY_BPHY_CTL2_LUT);
391
392 if (nphy->hang_avoid)
393 b43_nphy_stay_in_carrier_search(dev, 0);
Michael Bueschd1591312008-01-14 00:05:57 +0100394}
395
Rafał Miłecki7955de02010-02-27 13:03:39 +0100396
397/* http://bcm-v4.sipsolutions.net/802.11/PHY/Radio/2055Setup */
398static void b43_radio_2055_setup(struct b43_wldev *dev,
Rafał Miłeckif19ebe72010-03-29 00:53:15 +0200399 const struct b43_nphy_channeltab_entry_rev2 *e)
Rafał Miłecki7955de02010-02-27 13:03:39 +0100400{
401 B43_WARN_ON(dev->phy.rev >= 3);
402
403 b43_chantab_radio_upload(dev, e);
404 udelay(50);
Rafał Miłeckie58b1252010-03-29 00:53:16 +0200405 b43_radio_write(dev, B2055_VCO_CAL10, 0x05);
406 b43_radio_write(dev, B2055_VCO_CAL10, 0x45);
Rafał Miłecki7955de02010-02-27 13:03:39 +0100407 b43_read32(dev, B43_MMIO_MACCTL); /* flush writes */
Rafał Miłeckie58b1252010-03-29 00:53:16 +0200408 b43_radio_write(dev, B2055_VCO_CAL10, 0x65);
Rafał Miłecki7955de02010-02-27 13:03:39 +0100409 udelay(300);
410}
411
Michael Buesch53a6e232008-01-13 21:23:44 +0100412static void b43_radio_init2055_pre(struct b43_wldev *dev)
413{
414 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
415 ~B43_NPHY_RFCTL_CMD_PORFORCE);
416 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
417 B43_NPHY_RFCTL_CMD_CHIP0PU |
418 B43_NPHY_RFCTL_CMD_OEPORFORCE);
419 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
420 B43_NPHY_RFCTL_CMD_PORFORCE);
421}
422
423static void b43_radio_init2055_post(struct b43_wldev *dev)
424{
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100425 struct b43_phy_n *nphy = dev->phy.n;
Rafał Miłecki05814832011-05-18 02:06:39 +0200426 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesch53a6e232008-01-13 21:23:44 +0100427 int i;
428 u16 val;
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100429 bool workaround = false;
430
431 if (sprom->revision < 4)
Rafał Miłecki79d22322011-05-18 02:06:42 +0200432 workaround = (dev->dev->board_vendor != PCI_VENDOR_ID_BROADCOM
433 && dev->dev->board_type == 0x46D
434 && dev->dev->board_rev >= 0x41);
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100435 else
Rafał Miłecki7a4db8f2010-10-22 17:43:48 +0200436 workaround =
437 !(sprom->boardflags2_lo & B43_BFL2_RXBB_INT_REG_DIS);
Michael Buesch53a6e232008-01-13 21:23:44 +0100438
439 b43_radio_mask(dev, B2055_MASTER1, 0xFFF3);
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100440 if (workaround) {
441 b43_radio_mask(dev, B2055_C1_RX_BB_REG, 0x7F);
442 b43_radio_mask(dev, B2055_C2_RX_BB_REG, 0x7F);
Michael Buesch53a6e232008-01-13 21:23:44 +0100443 }
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100444 b43_radio_maskset(dev, B2055_RRCCAL_NOPTSEL, 0xFFC0, 0x2C);
445 b43_radio_write(dev, B2055_CAL_MISC, 0x3C);
Michael Buesch53a6e232008-01-13 21:23:44 +0100446 b43_radio_mask(dev, B2055_CAL_MISC, 0xFFBE);
Michael Buesch53a6e232008-01-13 21:23:44 +0100447 b43_radio_set(dev, B2055_CAL_LPOCTL, 0x80);
Michael Buesch53a6e232008-01-13 21:23:44 +0100448 b43_radio_set(dev, B2055_CAL_MISC, 0x1);
449 msleep(1);
450 b43_radio_set(dev, B2055_CAL_MISC, 0x40);
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100451 for (i = 0; i < 200; i++) {
452 val = b43_radio_read(dev, B2055_CAL_COUT2);
453 if (val & 0x80) {
454 i = 0;
Michael Buesch53a6e232008-01-13 21:23:44 +0100455 break;
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100456 }
Michael Buesch53a6e232008-01-13 21:23:44 +0100457 udelay(10);
458 }
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100459 if (i)
460 b43err(dev->wl, "radio post init timeout\n");
Michael Buesch53a6e232008-01-13 21:23:44 +0100461 b43_radio_mask(dev, B2055_CAL_LPOCTL, 0xFF7F);
Rafał Miłecki78159782010-10-06 07:50:08 +0200462 b43_switch_channel(dev, dev->phy.channel);
Rafał Miłecki036cafe2010-02-27 13:03:36 +0100463 b43_radio_write(dev, B2055_C1_RX_BB_LPF, 0x9);
464 b43_radio_write(dev, B2055_C2_RX_BB_LPF, 0x9);
465 b43_radio_write(dev, B2055_C1_RX_BB_MIDACHP, 0x83);
466 b43_radio_write(dev, B2055_C2_RX_BB_MIDACHP, 0x83);
467 b43_radio_maskset(dev, B2055_C1_LNA_GAINBST, 0xFFF8, 0x6);
468 b43_radio_maskset(dev, B2055_C2_LNA_GAINBST, 0xFFF8, 0x6);
469 if (!nphy->gain_boost) {
470 b43_radio_set(dev, B2055_C1_RX_RFSPC1, 0x2);
471 b43_radio_set(dev, B2055_C2_RX_RFSPC1, 0x2);
472 } else {
473 b43_radio_mask(dev, B2055_C1_RX_RFSPC1, 0xFFFD);
474 b43_radio_mask(dev, B2055_C2_RX_RFSPC1, 0xFFFD);
475 }
476 udelay(2);
Michael Buesch53a6e232008-01-13 21:23:44 +0100477}
478
Rafał Miłeckic2b7aef2010-02-27 13:03:34 +0100479/*
480 * Initialize a Broadcom 2055 N-radio
481 * http://bcm-v4.sipsolutions.net/802.11/Radio/2055/Init
482 */
Michael Buesch53a6e232008-01-13 21:23:44 +0100483static void b43_radio_init2055(struct b43_wldev *dev)
484{
485 b43_radio_init2055_pre(dev);
Rafał Miłeckia2d9bc62010-10-22 17:43:49 +0200486 if (b43_status(dev) < B43_STAT_INITIALIZED) {
487 /* Follow wl, not specs. Do not force uploading all regs */
488 b2055_upload_inittab(dev, 0, 0);
489 } else {
490 bool ghz5 = b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ;
491 b2055_upload_inittab(dev, ghz5, 0);
492 }
Michael Buesch53a6e232008-01-13 21:23:44 +0100493 b43_radio_init2055_post(dev);
494}
495
Rafał Miłeckiea7ee142010-12-21 17:13:44 +0100496static void b43_radio_init2056_pre(struct b43_wldev *dev)
497{
498 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
499 ~B43_NPHY_RFCTL_CMD_CHIP0PU);
500 /* Maybe wl meant to reset and set (order?) RFCTL_CMD_OEPORFORCE? */
501 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
502 B43_NPHY_RFCTL_CMD_OEPORFORCE);
503 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
504 ~B43_NPHY_RFCTL_CMD_OEPORFORCE);
505 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
506 B43_NPHY_RFCTL_CMD_CHIP0PU);
507}
508
509static void b43_radio_init2056_post(struct b43_wldev *dev)
510{
511 b43_radio_set(dev, B2056_SYN_COM_CTRL, 0xB);
512 b43_radio_set(dev, B2056_SYN_COM_PU, 0x2);
513 b43_radio_set(dev, B2056_SYN_COM_RESET, 0x2);
514 msleep(1);
515 b43_radio_mask(dev, B2056_SYN_COM_RESET, ~0x2);
516 b43_radio_mask(dev, B2056_SYN_PLL_MAST2, ~0xFC);
517 b43_radio_mask(dev, B2056_SYN_RCCAL_CTRL0, ~0x1);
518 /*
519 if (nphy->init_por)
520 Call Radio 2056 Recalibrate
521 */
522}
523
Rafał Miłecki4772ae12010-01-15 12:18:21 +0100524/*
Rafał Miłeckid817f4e2010-03-29 00:53:12 +0200525 * Initialize a Broadcom 2056 N-radio
526 * http://bcm-v4.sipsolutions.net/802.11/Radio/2056/Init
527 */
528static void b43_radio_init2056(struct b43_wldev *dev)
529{
Rafał Miłeckiea7ee142010-12-21 17:13:44 +0100530 b43_radio_init2056_pre(dev);
531 b2056_upload_inittabs(dev, 0, 0);
532 b43_radio_init2056_post(dev);
Rafał Miłeckid817f4e2010-03-29 00:53:12 +0200533}
534
Rafał Miłeckid817f4e2010-03-29 00:53:12 +0200535/*
Rafał Miłecki4772ae12010-01-15 12:18:21 +0100536 * Upload the N-PHY tables.
537 * http://bcm-v4.sipsolutions.net/802.11/PHY/N/InitTables
538 */
Michael Buesch95b66ba2008-01-18 01:09:25 +0100539static void b43_nphy_tables_init(struct b43_wldev *dev)
540{
Rafał Miłecki4772ae12010-01-15 12:18:21 +0100541 if (dev->phy.rev < 3)
542 b43_nphy_rev0_1_2_tables_init(dev);
543 else
544 b43_nphy_rev3plus_tables_init(dev);
Michael Buesch95b66ba2008-01-18 01:09:25 +0100545}
546
Rafał Miłeckie50cbcf2010-01-15 15:02:38 +0100547/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/PA%20override */
548static void b43_nphy_pa_override(struct b43_wldev *dev, bool enable)
549{
550 struct b43_phy_n *nphy = dev->phy.n;
551 enum ieee80211_band band;
552 u16 tmp;
553
554 if (!enable) {
555 nphy->rfctrl_intc1_save = b43_phy_read(dev,
556 B43_NPHY_RFCTL_INTC1);
557 nphy->rfctrl_intc2_save = b43_phy_read(dev,
558 B43_NPHY_RFCTL_INTC2);
559 band = b43_current_band(dev->wl);
560 if (dev->phy.rev >= 3) {
561 if (band == IEEE80211_BAND_5GHZ)
562 tmp = 0x600;
563 else
564 tmp = 0x480;
565 } else {
566 if (band == IEEE80211_BAND_5GHZ)
567 tmp = 0x180;
568 else
569 tmp = 0x120;
570 }
571 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, tmp);
572 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, tmp);
573 } else {
574 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1,
575 nphy->rfctrl_intc1_save);
576 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2,
577 nphy->rfctrl_intc2_save);
578 }
579}
580
Rafał Miłeckife3e46e2010-01-15 15:51:55 +0100581/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxLpFbw */
582static void b43_nphy_tx_lp_fbw(struct b43_wldev *dev)
583{
584 struct b43_phy_n *nphy = dev->phy.n;
585 u16 tmp;
586 enum ieee80211_band band = b43_current_band(dev->wl);
587 bool ipa = (nphy->ipa2g_on && band == IEEE80211_BAND_2GHZ) ||
588 (nphy->ipa5g_on && band == IEEE80211_BAND_5GHZ);
589
590 if (dev->phy.rev >= 3) {
591 if (ipa) {
592 tmp = 4;
593 b43_phy_write(dev, B43_NPHY_TXF_40CO_B32S2,
594 (((((tmp << 3) | tmp) << 3) | tmp) << 3) | tmp);
595 }
596
597 tmp = 1;
598 b43_phy_write(dev, B43_NPHY_TXF_40CO_B1S2,
599 (((((tmp << 3) | tmp) << 3) | tmp) << 3) | tmp);
600 }
601}
602
Rafał Miłecki4a933c82010-01-15 13:36:43 +0100603/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BmacPhyClkFgc */
604static void b43_nphy_bmac_clock_fgc(struct b43_wldev *dev, bool force)
605{
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +0200606 u32 tmp;
Rafał Miłecki4a933c82010-01-15 13:36:43 +0100607
608 if (dev->phy.type != B43_PHYTYPE_N)
609 return;
610
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +0200611 switch (dev->dev->bus_type) {
612#ifdef CONFIG_B43_SSB
613 case B43_BUS_SSB:
614 tmp = ssb_read32(dev->dev->sdev, SSB_TMSLOW);
615 if (force)
616 tmp |= SSB_TMSLOW_FGC;
617 else
618 tmp &= ~SSB_TMSLOW_FGC;
619 ssb_write32(dev->dev->sdev, SSB_TMSLOW, tmp);
620 break;
621#endif
622 }
Rafał Miłecki4a933c82010-01-15 13:36:43 +0100623}
624
625/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/CCA */
Michael Buesch95b66ba2008-01-18 01:09:25 +0100626static void b43_nphy_reset_cca(struct b43_wldev *dev)
627{
628 u16 bbcfg;
629
Rafał Miłecki4a933c82010-01-15 13:36:43 +0100630 b43_nphy_bmac_clock_fgc(dev, 1);
Michael Buesch95b66ba2008-01-18 01:09:25 +0100631 bbcfg = b43_phy_read(dev, B43_NPHY_BBCFG);
Rafał Miłecki4a933c82010-01-15 13:36:43 +0100632 b43_phy_write(dev, B43_NPHY_BBCFG, bbcfg | B43_NPHY_BBCFG_RSTCCA);
633 udelay(1);
634 b43_phy_write(dev, B43_NPHY_BBCFG, bbcfg & ~B43_NPHY_BBCFG_RSTCCA);
635 b43_nphy_bmac_clock_fgc(dev, 0);
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +0100636 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
Michael Buesch95b66ba2008-01-18 01:09:25 +0100637}
638
Rafał Miłeckiad9716e2010-01-17 13:03:40 +0100639/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/MIMOConfig */
640static void b43_nphy_update_mimo_config(struct b43_wldev *dev, s32 preamble)
641{
642 u16 mimocfg = b43_phy_read(dev, B43_NPHY_MIMOCFG);
643
644 mimocfg |= B43_NPHY_MIMOCFG_AUTO;
645 if (preamble == 1)
646 mimocfg |= B43_NPHY_MIMOCFG_GFMIX;
647 else
648 mimocfg &= ~B43_NPHY_MIMOCFG_GFMIX;
649
650 b43_phy_write(dev, B43_NPHY_MIMOCFG, mimocfg);
651}
652
Rafał Miłecki4f4ab6c2010-01-17 13:03:55 +0100653/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/Chains */
654static void b43_nphy_update_txrx_chain(struct b43_wldev *dev)
655{
656 struct b43_phy_n *nphy = dev->phy.n;
657
658 bool override = false;
659 u16 chain = 0x33;
660
661 if (nphy->txrx_chain == 0) {
662 chain = 0x11;
663 override = true;
664 } else if (nphy->txrx_chain == 1) {
665 chain = 0x22;
666 override = true;
667 }
668
669 b43_phy_maskset(dev, B43_NPHY_RFSEQCA,
670 ~(B43_NPHY_RFSEQCA_TXEN | B43_NPHY_RFSEQCA_RXEN),
671 chain);
672
673 if (override)
674 b43_phy_set(dev, B43_NPHY_RFSEQMODE,
675 B43_NPHY_RFSEQMODE_CAOVER);
676 else
677 b43_phy_mask(dev, B43_NPHY_RFSEQMODE,
678 ~B43_NPHY_RFSEQMODE_CAOVER);
679}
680
Rafał Miłecki2faa6b82010-01-15 15:26:12 +0100681/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RxIqEst */
682static void b43_nphy_rx_iq_est(struct b43_wldev *dev, struct nphy_iq_est *est,
683 u16 samps, u8 time, bool wait)
684{
685 int i;
686 u16 tmp;
687
688 b43_phy_write(dev, B43_NPHY_IQEST_SAMCNT, samps);
689 b43_phy_maskset(dev, B43_NPHY_IQEST_WT, ~B43_NPHY_IQEST_WT_VAL, time);
690 if (wait)
691 b43_phy_set(dev, B43_NPHY_IQEST_CMD, B43_NPHY_IQEST_CMD_MODE);
692 else
693 b43_phy_mask(dev, B43_NPHY_IQEST_CMD, ~B43_NPHY_IQEST_CMD_MODE);
694
695 b43_phy_set(dev, B43_NPHY_IQEST_CMD, B43_NPHY_IQEST_CMD_START);
696
697 for (i = 1000; i; i--) {
698 tmp = b43_phy_read(dev, B43_NPHY_IQEST_CMD);
699 if (!(tmp & B43_NPHY_IQEST_CMD_START)) {
700 est->i0_pwr = (b43_phy_read(dev, B43_NPHY_IQEST_IPACC_HI0) << 16) |
701 b43_phy_read(dev, B43_NPHY_IQEST_IPACC_LO0);
702 est->q0_pwr = (b43_phy_read(dev, B43_NPHY_IQEST_QPACC_HI0) << 16) |
703 b43_phy_read(dev, B43_NPHY_IQEST_QPACC_LO0);
704 est->iq0_prod = (b43_phy_read(dev, B43_NPHY_IQEST_IQACC_HI0) << 16) |
705 b43_phy_read(dev, B43_NPHY_IQEST_IQACC_LO0);
706
707 est->i1_pwr = (b43_phy_read(dev, B43_NPHY_IQEST_IPACC_HI1) << 16) |
708 b43_phy_read(dev, B43_NPHY_IQEST_IPACC_LO1);
709 est->q1_pwr = (b43_phy_read(dev, B43_NPHY_IQEST_QPACC_HI1) << 16) |
710 b43_phy_read(dev, B43_NPHY_IQEST_QPACC_LO1);
711 est->iq1_prod = (b43_phy_read(dev, B43_NPHY_IQEST_IQACC_HI1) << 16) |
712 b43_phy_read(dev, B43_NPHY_IQEST_IQACC_LO1);
713 return;
714 }
715 udelay(10);
716 }
717 memset(est, 0, sizeof(*est));
718}
719
Rafał Miłeckia67162a2010-01-15 15:16:25 +0100720/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RxIqCoeffs */
721static void b43_nphy_rx_iq_coeffs(struct b43_wldev *dev, bool write,
722 struct b43_phy_n_iq_comp *pcomp)
723{
724 if (write) {
725 b43_phy_write(dev, B43_NPHY_C1_RXIQ_COMPA0, pcomp->a0);
726 b43_phy_write(dev, B43_NPHY_C1_RXIQ_COMPB0, pcomp->b0);
727 b43_phy_write(dev, B43_NPHY_C2_RXIQ_COMPA1, pcomp->a1);
728 b43_phy_write(dev, B43_NPHY_C2_RXIQ_COMPB1, pcomp->b1);
729 } else {
730 pcomp->a0 = b43_phy_read(dev, B43_NPHY_C1_RXIQ_COMPA0);
731 pcomp->b0 = b43_phy_read(dev, B43_NPHY_C1_RXIQ_COMPB0);
732 pcomp->a1 = b43_phy_read(dev, B43_NPHY_C2_RXIQ_COMPA1);
733 pcomp->b1 = b43_phy_read(dev, B43_NPHY_C2_RXIQ_COMPB1);
734 }
735}
736
Rafał Miłeckic7455cf2010-12-07 21:55:57 +0100737#if 0
738/* Ready but not used anywhere */
Rafał Miłecki026816f2010-01-17 13:03:28 +0100739/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RxCalPhyCleanup */
740static void b43_nphy_rx_cal_phy_cleanup(struct b43_wldev *dev, u8 core)
741{
742 u16 *regs = dev->phy.n->tx_rx_cal_phy_saveregs;
743
744 b43_phy_write(dev, B43_NPHY_RFSEQCA, regs[0]);
745 if (core == 0) {
746 b43_phy_write(dev, B43_NPHY_AFECTL_C1, regs[1]);
747 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, regs[2]);
748 } else {
749 b43_phy_write(dev, B43_NPHY_AFECTL_C2, regs[1]);
750 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, regs[2]);
751 }
752 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, regs[3]);
753 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, regs[4]);
754 b43_phy_write(dev, B43_NPHY_RFCTL_RSSIO1, regs[5]);
755 b43_phy_write(dev, B43_NPHY_RFCTL_RSSIO2, regs[6]);
756 b43_phy_write(dev, B43_NPHY_TXF_40CO_B1S1, regs[7]);
757 b43_phy_write(dev, B43_NPHY_RFCTL_OVER, regs[8]);
758 b43_phy_write(dev, B43_NPHY_PAPD_EN0, regs[9]);
759 b43_phy_write(dev, B43_NPHY_PAPD_EN1, regs[10]);
760}
761
762/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RxCalPhySetup */
763static void b43_nphy_rx_cal_phy_setup(struct b43_wldev *dev, u8 core)
764{
765 u8 rxval, txval;
766 u16 *regs = dev->phy.n->tx_rx_cal_phy_saveregs;
767
768 regs[0] = b43_phy_read(dev, B43_NPHY_RFSEQCA);
769 if (core == 0) {
770 regs[1] = b43_phy_read(dev, B43_NPHY_AFECTL_C1);
771 regs[2] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER1);
772 } else {
773 regs[1] = b43_phy_read(dev, B43_NPHY_AFECTL_C2);
774 regs[2] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
775 }
776 regs[3] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC1);
777 regs[4] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC2);
778 regs[5] = b43_phy_read(dev, B43_NPHY_RFCTL_RSSIO1);
779 regs[6] = b43_phy_read(dev, B43_NPHY_RFCTL_RSSIO2);
780 regs[7] = b43_phy_read(dev, B43_NPHY_TXF_40CO_B1S1);
781 regs[8] = b43_phy_read(dev, B43_NPHY_RFCTL_OVER);
782 regs[9] = b43_phy_read(dev, B43_NPHY_PAPD_EN0);
783 regs[10] = b43_phy_read(dev, B43_NPHY_PAPD_EN1);
784
785 b43_phy_mask(dev, B43_NPHY_PAPD_EN0, ~0x0001);
786 b43_phy_mask(dev, B43_NPHY_PAPD_EN1, ~0x0001);
787
Larry Fingeracd82aa2010-07-21 11:48:05 -0500788 b43_phy_maskset(dev, B43_NPHY_RFSEQCA,
789 ~B43_NPHY_RFSEQCA_RXDIS & 0xFFFF,
Rafał Miłecki026816f2010-01-17 13:03:28 +0100790 ((1 - core) << B43_NPHY_RFSEQCA_RXDIS_SHIFT));
791 b43_phy_maskset(dev, B43_NPHY_RFSEQCA, ~B43_NPHY_RFSEQCA_TXEN,
792 ((1 - core) << B43_NPHY_RFSEQCA_TXEN_SHIFT));
793 b43_phy_maskset(dev, B43_NPHY_RFSEQCA, ~B43_NPHY_RFSEQCA_RXEN,
794 (core << B43_NPHY_RFSEQCA_RXEN_SHIFT));
795 b43_phy_maskset(dev, B43_NPHY_RFSEQCA, ~B43_NPHY_RFSEQCA_TXDIS,
796 (core << B43_NPHY_RFSEQCA_TXDIS_SHIFT));
797
798 if (core == 0) {
799 b43_phy_mask(dev, B43_NPHY_AFECTL_C1, ~0x0007);
800 b43_phy_set(dev, B43_NPHY_AFECTL_OVER1, 0x0007);
801 } else {
802 b43_phy_mask(dev, B43_NPHY_AFECTL_C2, ~0x0007);
803 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x0007);
804 }
805
Rafał Miłecki67cbc3e2010-02-04 12:23:08 +0100806 b43_nphy_rf_control_intc_override(dev, 2, 0, 3);
807 b43_nphy_rf_control_override(dev, 8, 0, 3, false);
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +0100808 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RX2TX);
Rafał Miłecki026816f2010-01-17 13:03:28 +0100809
810 if (core == 0) {
811 rxval = 1;
812 txval = 8;
813 } else {
814 rxval = 4;
815 txval = 2;
816 }
Rafał Miłecki67cbc3e2010-02-04 12:23:08 +0100817 b43_nphy_rf_control_intc_override(dev, 1, rxval, (core + 1));
818 b43_nphy_rf_control_intc_override(dev, 1, txval, (2 - core));
Rafał Miłecki026816f2010-01-17 13:03:28 +0100819}
Rafał Miłeckic7455cf2010-12-07 21:55:57 +0100820#endif
Rafał Miłecki026816f2010-01-17 13:03:28 +0100821
Rafał Miłecki34a56f22010-01-15 15:29:05 +0100822/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/CalcRxIqComp */
823static void b43_nphy_calc_rx_iq_comp(struct b43_wldev *dev, u8 mask)
824{
825 int i;
826 s32 iq;
827 u32 ii;
828 u32 qq;
829 int iq_nbits, qq_nbits;
830 int arsh, brsh;
831 u16 tmp, a, b;
832
833 struct nphy_iq_est est;
834 struct b43_phy_n_iq_comp old;
835 struct b43_phy_n_iq_comp new = { };
836 bool error = false;
837
838 if (mask == 0)
839 return;
840
841 b43_nphy_rx_iq_coeffs(dev, false, &old);
842 b43_nphy_rx_iq_coeffs(dev, true, &new);
843 b43_nphy_rx_iq_est(dev, &est, 0x4000, 32, false);
844 new = old;
845
846 for (i = 0; i < 2; i++) {
847 if (i == 0 && (mask & 1)) {
848 iq = est.iq0_prod;
849 ii = est.i0_pwr;
850 qq = est.q0_pwr;
851 } else if (i == 1 && (mask & 2)) {
852 iq = est.iq1_prod;
853 ii = est.i1_pwr;
854 qq = est.q1_pwr;
855 } else {
Rafał Miłecki34a56f22010-01-15 15:29:05 +0100856 continue;
857 }
858
859 if (ii + qq < 2) {
860 error = true;
861 break;
862 }
863
864 iq_nbits = fls(abs(iq));
865 qq_nbits = fls(qq);
866
867 arsh = iq_nbits - 20;
868 if (arsh >= 0) {
869 a = -((iq << (30 - iq_nbits)) + (ii >> (1 + arsh)));
870 tmp = ii >> arsh;
871 } else {
872 a = -((iq << (30 - iq_nbits)) + (ii << (-1 - arsh)));
873 tmp = ii << -arsh;
874 }
875 if (tmp == 0) {
876 error = true;
877 break;
878 }
879 a /= tmp;
880
881 brsh = qq_nbits - 11;
882 if (brsh >= 0) {
883 b = (qq << (31 - qq_nbits));
884 tmp = ii >> brsh;
885 } else {
886 b = (qq << (31 - qq_nbits));
887 tmp = ii << -brsh;
888 }
889 if (tmp == 0) {
890 error = true;
891 break;
892 }
893 b = int_sqrt(b / tmp - a * a) - (1 << 10);
894
895 if (i == 0 && (mask & 0x1)) {
896 if (dev->phy.rev >= 3) {
897 new.a0 = a & 0x3FF;
898 new.b0 = b & 0x3FF;
899 } else {
900 new.a0 = b & 0x3FF;
901 new.b0 = a & 0x3FF;
902 }
903 } else if (i == 1 && (mask & 0x2)) {
904 if (dev->phy.rev >= 3) {
905 new.a1 = a & 0x3FF;
906 new.b1 = b & 0x3FF;
907 } else {
908 new.a1 = b & 0x3FF;
909 new.b1 = a & 0x3FF;
910 }
911 }
912 }
913
914 if (error)
915 new = old;
916
917 b43_nphy_rx_iq_coeffs(dev, true, &new);
918}
919
Rafał Miłecki09146402010-01-15 15:17:10 +0100920/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxIqWar */
921static void b43_nphy_tx_iq_workaround(struct b43_wldev *dev)
922{
923 u16 array[4];
924 int i;
925
926 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x3C50);
927 for (i = 0; i < 4; i++)
928 array[i] = b43_phy_read(dev, B43_NPHY_TABLE_DATALO);
929
930 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_NPHY_TXIQW0, array[0]);
931 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_NPHY_TXIQW1, array[1]);
932 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_NPHY_TXIQW2, array[2]);
933 b43_shm_write16(dev, B43_SHM_SHARED, B43_SHM_SH_NPHY_TXIQW3, array[3]);
934}
935
Rafał Miłeckibbec3982010-01-15 14:31:39 +0100936/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/clip-detection */
Joe Perches20407ed2010-11-20 18:38:57 -0800937static void b43_nphy_write_clip_detection(struct b43_wldev *dev,
938 const u16 *clip_st)
Rafał Miłeckibbec3982010-01-15 14:31:39 +0100939{
940 b43_phy_write(dev, B43_NPHY_C1_CLIP1THRES, clip_st[0]);
941 b43_phy_write(dev, B43_NPHY_C2_CLIP1THRES, clip_st[1]);
942}
943
944/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/clip-detection */
945static void b43_nphy_read_clip_detection(struct b43_wldev *dev, u16 *clip_st)
946{
947 clip_st[0] = b43_phy_read(dev, B43_NPHY_C1_CLIP1THRES);
948 clip_st[1] = b43_phy_read(dev, B43_NPHY_C2_CLIP1THRES);
949}
950
Rafał Miłecki8987a9e2010-02-27 13:03:33 +0100951/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SuperSwitchInit */
952static void b43_nphy_superswitch_init(struct b43_wldev *dev, bool init)
953{
954 if (dev->phy.rev >= 3) {
955 if (!init)
956 return;
957 if (0 /* FIXME */) {
958 b43_ntab_write(dev, B43_NTAB16(9, 2), 0x211);
959 b43_ntab_write(dev, B43_NTAB16(9, 3), 0x222);
960 b43_ntab_write(dev, B43_NTAB16(9, 8), 0x144);
961 b43_ntab_write(dev, B43_NTAB16(9, 12), 0x188);
962 }
963 } else {
964 b43_phy_write(dev, B43_NPHY_GPIO_LOOEN, 0);
965 b43_phy_write(dev, B43_NPHY_GPIO_HIOEN, 0);
966
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +0200967 switch (dev->dev->bus_type) {
968#ifdef CONFIG_B43_SSB
969 case B43_BUS_SSB:
970 ssb_chipco_gpio_control(&dev->dev->sdev->bus->chipco,
971 0xFC00, 0xFC00);
972 break;
973#endif
974 }
975
Rafał Miłecki8987a9e2010-02-27 13:03:33 +0100976 b43_write32(dev, B43_MMIO_MACCTL,
977 b43_read32(dev, B43_MMIO_MACCTL) &
978 ~B43_MACCTL_GPOUTSMSK);
979 b43_write16(dev, B43_MMIO_GPIO_MASK,
980 b43_read16(dev, B43_MMIO_GPIO_MASK) | 0xFC00);
981 b43_write16(dev, B43_MMIO_GPIO_CONTROL,
982 b43_read16(dev, B43_MMIO_GPIO_CONTROL) & ~0xFC00);
983
984 if (init) {
985 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_LO1, 0x2D8);
986 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP1, 0x301);
987 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_LO2, 0x2D8);
988 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP2, 0x301);
989 }
990 }
991}
992
Rafał Miłeckibbec3982010-01-15 14:31:39 +0100993/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/classifier */
994static u16 b43_nphy_classifier(struct b43_wldev *dev, u16 mask, u16 val)
995{
996 u16 tmp;
997
Rafał Miłecki21d889d2011-05-18 02:06:38 +0200998 if (dev->dev->core_rev == 16)
Rafał Miłeckibbec3982010-01-15 14:31:39 +0100999 b43_mac_suspend(dev);
1000
1001 tmp = b43_phy_read(dev, B43_NPHY_CLASSCTL);
1002 tmp &= (B43_NPHY_CLASSCTL_CCKEN | B43_NPHY_CLASSCTL_OFDMEN |
1003 B43_NPHY_CLASSCTL_WAITEDEN);
1004 tmp &= ~mask;
1005 tmp |= (val & mask);
1006 b43_phy_maskset(dev, B43_NPHY_CLASSCTL, 0xFFF8, tmp);
1007
Rafał Miłecki21d889d2011-05-18 02:06:38 +02001008 if (dev->dev->core_rev == 16)
Rafał Miłeckibbec3982010-01-15 14:31:39 +01001009 b43_mac_enable(dev);
1010
1011 return tmp;
1012}
1013
Rafał Miłecki5c1a1402010-01-15 15:10:54 +01001014/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/carriersearch */
1015static void b43_nphy_stay_in_carrier_search(struct b43_wldev *dev, bool enable)
1016{
1017 struct b43_phy *phy = &dev->phy;
1018 struct b43_phy_n *nphy = phy->n;
1019
1020 if (enable) {
Joe Perches20407ed2010-11-20 18:38:57 -08001021 static const u16 clip[] = { 0xFFFF, 0xFFFF };
Rafał Miłecki5c1a1402010-01-15 15:10:54 +01001022 if (nphy->deaf_count++ == 0) {
1023 nphy->classifier_state = b43_nphy_classifier(dev, 0, 0);
1024 b43_nphy_classifier(dev, 0x7, 0);
1025 b43_nphy_read_clip_detection(dev, nphy->clip_state);
1026 b43_nphy_write_clip_detection(dev, clip);
1027 }
1028 b43_nphy_reset_cca(dev);
1029 } else {
1030 if (--nphy->deaf_count == 0) {
1031 b43_nphy_classifier(dev, 0x7, nphy->classifier_state);
1032 b43_nphy_write_clip_detection(dev, nphy->clip_state);
1033 }
1034 }
1035}
1036
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01001037/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/stop-playback */
1038static void b43_nphy_stop_playback(struct b43_wldev *dev)
1039{
1040 struct b43_phy_n *nphy = dev->phy.n;
1041 u16 tmp;
1042
1043 if (nphy->hang_avoid)
1044 b43_nphy_stay_in_carrier_search(dev, 1);
1045
1046 tmp = b43_phy_read(dev, B43_NPHY_SAMP_STAT);
1047 if (tmp & 0x1)
1048 b43_phy_set(dev, B43_NPHY_SAMP_CMD, B43_NPHY_SAMP_CMD_STOP);
1049 else if (tmp & 0x2)
Larry Fingeracd82aa2010-07-21 11:48:05 -05001050 b43_phy_mask(dev, B43_NPHY_IQLOCAL_CMDGCTL, 0x7FFF);
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01001051
1052 b43_phy_mask(dev, B43_NPHY_SAMP_CMD, ~0x0004);
1053
1054 if (nphy->bb_mult_save & 0x80000000) {
1055 tmp = nphy->bb_mult_save & 0xFFFF;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01001056 b43_ntab_write(dev, B43_NTAB16(15, 87), tmp);
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01001057 nphy->bb_mult_save = 0;
1058 }
1059
1060 if (nphy->hang_avoid)
1061 b43_nphy_stay_in_carrier_search(dev, 0);
1062}
1063
Rafał Miłecki9442e5b2010-02-04 12:23:12 +01001064/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SpurWar */
1065static void b43_nphy_spur_workaround(struct b43_wldev *dev)
1066{
1067 struct b43_phy_n *nphy = dev->phy.n;
1068
Rafał Miłecki204a6652010-10-14 19:33:34 +02001069 u8 channel = dev->phy.channel;
Rafał Miłecki9442e5b2010-02-04 12:23:12 +01001070 int tone[2] = { 57, 58 };
1071 u32 noise[2] = { 0x3FF, 0x3FF };
1072
1073 B43_WARN_ON(dev->phy.rev < 3);
1074
1075 if (nphy->hang_avoid)
1076 b43_nphy_stay_in_carrier_search(dev, 1);
1077
Rafał Miłecki9442e5b2010-02-04 12:23:12 +01001078 if (nphy->gband_spurwar_en) {
1079 /* TODO: N PHY Adjust Analog Pfbw (7) */
1080 if (channel == 11 && dev->phy.is_40mhz)
1081 ; /* TODO: N PHY Adjust Min Noise Var(2, tone, noise)*/
1082 else
1083 ; /* TODO: N PHY Adjust Min Noise Var(0, NULL, NULL)*/
1084 /* TODO: N PHY Adjust CRS Min Power (0x1E) */
1085 }
1086
1087 if (nphy->aband_spurwar_en) {
1088 if (channel == 54) {
1089 tone[0] = 0x20;
1090 noise[0] = 0x25F;
1091 } else if (channel == 38 || channel == 102 || channel == 118) {
1092 if (0 /* FIXME */) {
1093 tone[0] = 0x20;
1094 noise[0] = 0x21F;
1095 } else {
1096 tone[0] = 0;
1097 noise[0] = 0;
1098 }
1099 } else if (channel == 134) {
1100 tone[0] = 0x20;
1101 noise[0] = 0x21F;
1102 } else if (channel == 151) {
1103 tone[0] = 0x10;
1104 noise[0] = 0x23F;
1105 } else if (channel == 153 || channel == 161) {
1106 tone[0] = 0x30;
1107 noise[0] = 0x23F;
1108 } else {
1109 tone[0] = 0;
1110 noise[0] = 0;
1111 }
1112
1113 if (!tone[0] && !noise[0])
1114 ; /* TODO: N PHY Adjust Min Noise Var(1, tone, noise)*/
1115 else
1116 ; /* TODO: N PHY Adjust Min Noise Var(0, NULL, NULL)*/
1117 }
1118
1119 if (nphy->hang_avoid)
1120 b43_nphy_stay_in_carrier_search(dev, 0);
1121}
1122
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001123/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/AdjustLnaGainTbl */
1124static void b43_nphy_adjust_lna_gain_table(struct b43_wldev *dev)
1125{
1126 struct b43_phy_n *nphy = dev->phy.n;
1127
1128 u8 i;
1129 s16 tmp;
1130 u16 data[4];
1131 s16 gain[2];
1132 u16 minmax[2];
Joe Perches20407ed2010-11-20 18:38:57 -08001133 static const u16 lna_gain[4] = { -2, 10, 19, 25 };
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001134
1135 if (nphy->hang_avoid)
1136 b43_nphy_stay_in_carrier_search(dev, 1);
1137
1138 if (nphy->gain_boost) {
1139 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
1140 gain[0] = 6;
1141 gain[1] = 6;
1142 } else {
Rafał Miłecki204a6652010-10-14 19:33:34 +02001143 tmp = 40370 - 315 * dev->phy.channel;
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001144 gain[0] = ((tmp >> 13) + ((tmp >> 12) & 1));
Rafał Miłecki204a6652010-10-14 19:33:34 +02001145 tmp = 23242 - 224 * dev->phy.channel;
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001146 gain[1] = ((tmp >> 13) + ((tmp >> 12) & 1));
1147 }
1148 } else {
1149 gain[0] = 0;
1150 gain[1] = 0;
1151 }
1152
1153 for (i = 0; i < 2; i++) {
1154 if (nphy->elna_gain_config) {
1155 data[0] = 19 + gain[i];
1156 data[1] = 25 + gain[i];
1157 data[2] = 25 + gain[i];
1158 data[3] = 25 + gain[i];
1159 } else {
1160 data[0] = lna_gain[0] + gain[i];
1161 data[1] = lna_gain[1] + gain[i];
1162 data[2] = lna_gain[2] + gain[i];
1163 data[3] = lna_gain[3] + gain[i];
1164 }
Rafał Miłeckic0f05b92010-11-18 13:27:58 +01001165 b43_ntab_write_bulk(dev, B43_NTAB16(i, 8), 4, data);
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001166
1167 minmax[i] = 23 + gain[i];
1168 }
1169
1170 b43_phy_maskset(dev, B43_NPHY_C1_MINMAX_GAIN, ~B43_NPHY_C1_MINGAIN,
1171 minmax[0] << B43_NPHY_C1_MINGAIN_SHIFT);
1172 b43_phy_maskset(dev, B43_NPHY_C2_MINMAX_GAIN, ~B43_NPHY_C2_MINGAIN,
1173 minmax[1] << B43_NPHY_C2_MINGAIN_SHIFT);
1174
1175 if (nphy->hang_avoid)
1176 b43_nphy_stay_in_carrier_search(dev, 0);
1177}
1178
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001179/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/WorkaroundsGainCtrl */
Gábor Stefanike723ef32010-08-16 22:39:15 +02001180static void b43_nphy_gain_ctrl_workarounds(struct b43_wldev *dev)
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001181{
1182 struct b43_phy_n *nphy = dev->phy.n;
Rafał Miłecki05814832011-05-18 02:06:39 +02001183 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001184
1185 /* PHY rev 0, 1, 2 */
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001186 u8 i, j;
1187 u8 code;
Rafał Miłeckic0f05b92010-11-18 13:27:58 +01001188 u16 tmp;
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001189 u8 rfseq_events[3] = { 6, 8, 7 };
1190 u8 rfseq_delays[3] = { 10, 30, 1 };
1191
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001192 /* PHY rev >= 3 */
1193 bool ghz5;
1194 bool ext_lna;
1195 u16 rssi_gain;
1196 struct nphy_gain_ctl_workaround_entry *e;
1197 u8 lpf_gain[6] = { 0x00, 0x06, 0x0C, 0x12, 0x12, 0x12 };
1198 u8 lpf_bits[6] = { 0, 1, 2, 3, 3, 3 };
1199
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001200 if (dev->phy.rev >= 3) {
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001201 /* Prepare values */
1202 ghz5 = b43_phy_read(dev, B43_NPHY_BANDCTL)
1203 & B43_NPHY_BANDCTL_5GHZ;
1204 ext_lna = sprom->boardflags_lo & B43_BFL_EXTLNA;
1205 e = b43_nphy_get_gain_ctl_workaround_ent(dev, ghz5, ext_lna);
1206 if (ghz5 && dev->phy.rev >= 5)
1207 rssi_gain = 0x90;
1208 else
1209 rssi_gain = 0x50;
1210
1211 b43_phy_set(dev, B43_NPHY_RXCTL, 0x0040);
1212
1213 /* Set Clip 2 detect */
1214 b43_phy_set(dev, B43_NPHY_C1_CGAINI,
1215 B43_NPHY_C1_CGAINI_CL2DETECT);
1216 b43_phy_set(dev, B43_NPHY_C2_CGAINI,
1217 B43_NPHY_C2_CGAINI_CL2DETECT);
1218
1219 b43_radio_write(dev, B2056_RX0 | B2056_RX_BIASPOLE_LNAG1_IDAC,
1220 0x17);
1221 b43_radio_write(dev, B2056_RX1 | B2056_RX_BIASPOLE_LNAG1_IDAC,
1222 0x17);
1223 b43_radio_write(dev, B2056_RX0 | B2056_RX_LNAG2_IDAC, 0xF0);
1224 b43_radio_write(dev, B2056_RX1 | B2056_RX_LNAG2_IDAC, 0xF0);
1225 b43_radio_write(dev, B2056_RX0 | B2056_RX_RSSI_POLE, 0x00);
1226 b43_radio_write(dev, B2056_RX1 | B2056_RX_RSSI_POLE, 0x00);
1227 b43_radio_write(dev, B2056_RX0 | B2056_RX_RSSI_GAIN,
1228 rssi_gain);
1229 b43_radio_write(dev, B2056_RX1 | B2056_RX_RSSI_GAIN,
1230 rssi_gain);
1231 b43_radio_write(dev, B2056_RX0 | B2056_RX_BIASPOLE_LNAA1_IDAC,
1232 0x17);
1233 b43_radio_write(dev, B2056_RX1 | B2056_RX_BIASPOLE_LNAA1_IDAC,
1234 0x17);
1235 b43_radio_write(dev, B2056_RX0 | B2056_RX_LNAA2_IDAC, 0xFF);
1236 b43_radio_write(dev, B2056_RX1 | B2056_RX_LNAA2_IDAC, 0xFF);
1237
1238 b43_ntab_write_bulk(dev, B43_NTAB8(0, 8), 4, e->lna1_gain);
1239 b43_ntab_write_bulk(dev, B43_NTAB8(1, 8), 4, e->lna1_gain);
1240 b43_ntab_write_bulk(dev, B43_NTAB8(0, 16), 4, e->lna2_gain);
1241 b43_ntab_write_bulk(dev, B43_NTAB8(1, 16), 4, e->lna2_gain);
1242 b43_ntab_write_bulk(dev, B43_NTAB8(0, 32), 10, e->gain_db);
1243 b43_ntab_write_bulk(dev, B43_NTAB8(1, 32), 10, e->gain_db);
1244 b43_ntab_write_bulk(dev, B43_NTAB8(2, 32), 10, e->gain_bits);
1245 b43_ntab_write_bulk(dev, B43_NTAB8(3, 32), 10, e->gain_bits);
1246 b43_ntab_write_bulk(dev, B43_NTAB8(0, 0x40), 6, lpf_gain);
1247 b43_ntab_write_bulk(dev, B43_NTAB8(1, 0x40), 6, lpf_gain);
1248 b43_ntab_write_bulk(dev, B43_NTAB8(2, 0x40), 6, lpf_bits);
1249 b43_ntab_write_bulk(dev, B43_NTAB8(3, 0x40), 6, lpf_bits);
1250
1251 b43_phy_write(dev, B43_NPHY_C1_INITGAIN, e->init_gain);
1252 b43_phy_write(dev, 0x2A7, e->init_gain);
1253 b43_ntab_write_bulk(dev, B43_NTAB16(7, 0x106), 2,
1254 e->rfseq_init);
1255 b43_phy_write(dev, B43_NPHY_C1_INITGAIN, e->init_gain);
1256
1257 /* TODO: check defines. Do not match variables names */
1258 b43_phy_write(dev, B43_NPHY_C1_CLIP1_MEDGAIN, e->cliphi_gain);
1259 b43_phy_write(dev, 0x2A9, e->cliphi_gain);
1260 b43_phy_write(dev, B43_NPHY_C1_CLIP2_GAIN, e->clipmd_gain);
1261 b43_phy_write(dev, 0x2AB, e->clipmd_gain);
1262 b43_phy_write(dev, B43_NPHY_C2_CLIP1_HIGAIN, e->cliplo_gain);
1263 b43_phy_write(dev, 0x2AD, e->cliplo_gain);
1264
1265 b43_phy_maskset(dev, 0x27D, 0xFF00, e->crsmin);
1266 b43_phy_maskset(dev, 0x280, 0xFF00, e->crsminl);
1267 b43_phy_maskset(dev, 0x283, 0xFF00, e->crsminu);
1268 b43_phy_write(dev, B43_NPHY_C1_NBCLIPTHRES, e->nbclip);
1269 b43_phy_write(dev, B43_NPHY_C2_NBCLIPTHRES, e->nbclip);
1270 b43_phy_maskset(dev, B43_NPHY_C1_CLIPWBTHRES,
1271 ~B43_NPHY_C1_CLIPWBTHRES_CLIP2, e->wlclip);
1272 b43_phy_maskset(dev, B43_NPHY_C2_CLIPWBTHRES,
1273 ~B43_NPHY_C2_CLIPWBTHRES_CLIP2, e->wlclip);
1274 b43_phy_write(dev, B43_NPHY_CCK_SHIFTB_REF, 0x809C);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001275 } else {
1276 /* Set Clip 2 detect */
1277 b43_phy_set(dev, B43_NPHY_C1_CGAINI,
1278 B43_NPHY_C1_CGAINI_CL2DETECT);
1279 b43_phy_set(dev, B43_NPHY_C2_CGAINI,
1280 B43_NPHY_C2_CGAINI_CL2DETECT);
1281
1282 /* Set narrowband clip threshold */
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001283 b43_phy_write(dev, B43_NPHY_C1_NBCLIPTHRES, 0x84);
1284 b43_phy_write(dev, B43_NPHY_C2_NBCLIPTHRES, 0x84);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001285
1286 if (!dev->phy.is_40mhz) {
1287 /* Set dwell lengths */
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001288 b43_phy_write(dev, B43_NPHY_CLIP1_NBDWELL_LEN, 0x002B);
1289 b43_phy_write(dev, B43_NPHY_CLIP2_NBDWELL_LEN, 0x002B);
1290 b43_phy_write(dev, B43_NPHY_W1CLIP1_DWELL_LEN, 0x0009);
1291 b43_phy_write(dev, B43_NPHY_W1CLIP2_DWELL_LEN, 0x0009);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001292 }
1293
1294 /* Set wideband clip 2 threshold */
1295 b43_phy_maskset(dev, B43_NPHY_C1_CLIPWBTHRES,
1296 ~B43_NPHY_C1_CLIPWBTHRES_CLIP2,
1297 21);
1298 b43_phy_maskset(dev, B43_NPHY_C2_CLIPWBTHRES,
1299 ~B43_NPHY_C2_CLIPWBTHRES_CLIP2,
1300 21);
1301
1302 if (!dev->phy.is_40mhz) {
1303 b43_phy_maskset(dev, B43_NPHY_C1_CGAINI,
1304 ~B43_NPHY_C1_CGAINI_GAINBKOFF, 0x1);
1305 b43_phy_maskset(dev, B43_NPHY_C2_CGAINI,
1306 ~B43_NPHY_C2_CGAINI_GAINBKOFF, 0x1);
1307 b43_phy_maskset(dev, B43_NPHY_C1_CCK_CGAINI,
1308 ~B43_NPHY_C1_CCK_CGAINI_GAINBKOFF, 0x1);
1309 b43_phy_maskset(dev, B43_NPHY_C2_CCK_CGAINI,
1310 ~B43_NPHY_C2_CCK_CGAINI_GAINBKOFF, 0x1);
1311 }
1312
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001313 b43_phy_write(dev, B43_NPHY_CCK_SHIFTB_REF, 0x809C);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001314
1315 if (nphy->gain_boost) {
1316 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ &&
1317 dev->phy.is_40mhz)
1318 code = 4;
1319 else
1320 code = 5;
1321 } else {
1322 code = dev->phy.is_40mhz ? 6 : 7;
1323 }
1324
1325 /* Set HPVGA2 index */
1326 b43_phy_maskset(dev, B43_NPHY_C1_INITGAIN,
1327 ~B43_NPHY_C1_INITGAIN_HPVGA2,
1328 code << B43_NPHY_C1_INITGAIN_HPVGA2_SHIFT);
1329 b43_phy_maskset(dev, B43_NPHY_C2_INITGAIN,
1330 ~B43_NPHY_C2_INITGAIN_HPVGA2,
1331 code << B43_NPHY_C2_INITGAIN_HPVGA2_SHIFT);
1332
1333 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x1D06);
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001334 /* specs say about 2 loops, but wl does 4 */
1335 for (i = 0; i < 4; i++)
1336 b43_phy_write(dev, B43_NPHY_TABLE_DATALO,
1337 (code << 8 | 0x7C));
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001338
Rafał Miłeckid24019a2010-02-27 13:03:38 +01001339 b43_nphy_adjust_lna_gain_table(dev);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001340
1341 if (nphy->elna_gain_config) {
1342 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x0808);
1343 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x0);
1344 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1345 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1346 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1347
1348 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x0C08);
1349 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x0);
1350 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1351 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1352 b43_phy_write(dev, B43_NPHY_TABLE_DATALO, 0x1);
1353
1354 b43_phy_write(dev, B43_NPHY_TABLE_ADDR, 0x1D06);
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001355 /* specs say about 2 loops, but wl does 4 */
1356 for (i = 0; i < 4; i++)
1357 b43_phy_write(dev, B43_NPHY_TABLE_DATALO,
1358 (code << 8 | 0x74));
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001359 }
1360
1361 if (dev->phy.rev == 2) {
1362 for (i = 0; i < 4; i++) {
1363 b43_phy_write(dev, B43_NPHY_TABLE_ADDR,
1364 (0x0400 * i) + 0x0020);
Rafał Miłeckic0f05b92010-11-18 13:27:58 +01001365 for (j = 0; j < 21; j++) {
1366 tmp = j * (i < 2 ? 3 : 1);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001367 b43_phy_write(dev,
Rafał Miłeckic0f05b92010-11-18 13:27:58 +01001368 B43_NPHY_TABLE_DATALO, tmp);
1369 }
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001370 }
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001371 }
Rafał Miłecki8e60b042011-02-21 19:45:34 +01001372
1373 b43_nphy_set_rf_sequence(dev, 5,
1374 rfseq_events, rfseq_delays, 3);
1375 b43_phy_maskset(dev, B43_NPHY_OVER_DGAIN1,
1376 ~B43_NPHY_OVER_DGAIN_CCKDGECV & 0xFFFF,
1377 0x5A << B43_NPHY_OVER_DGAIN_CCKDGECV_SHIFT);
1378
1379 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)
1380 b43_phy_maskset(dev, B43_PHY_N(0xC5D),
1381 0xFF80, 4);
Rafał Miłeckief5127a2010-01-30 00:12:20 +01001382 }
1383}
1384
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001385/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/Workarounds */
1386static void b43_nphy_workarounds(struct b43_wldev *dev)
1387{
Rafał Miłecki05814832011-05-18 02:06:39 +02001388 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001389 struct b43_phy *phy = &dev->phy;
1390 struct b43_phy_n *nphy = phy->n;
1391
1392 u8 events1[7] = { 0x0, 0x1, 0x2, 0x8, 0x4, 0x5, 0x3 };
1393 u8 delays1[7] = { 0x8, 0x6, 0x6, 0x2, 0x4, 0x3C, 0x1 };
1394
1395 u8 events2[7] = { 0x0, 0x3, 0x5, 0x4, 0x2, 0x1, 0x8 };
1396 u8 delays2[7] = { 0x8, 0x6, 0x2, 0x4, 0x4, 0x6, 0x1 };
1397
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001398 u16 tmp16;
1399 u32 tmp32;
1400
Rafał Miłeckia5d35982010-11-18 13:27:59 +01001401 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001402 b43_nphy_classifier(dev, 1, 0);
1403 else
1404 b43_nphy_classifier(dev, 1, 1);
1405
1406 if (nphy->hang_avoid)
1407 b43_nphy_stay_in_carrier_search(dev, 1);
1408
1409 b43_phy_set(dev, B43_NPHY_IQFLIP,
1410 B43_NPHY_IQFLIP_ADC1 | B43_NPHY_IQFLIP_ADC2);
1411
1412 if (dev->phy.rev >= 3) {
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001413 tmp32 = b43_ntab_read(dev, B43_NTAB32(30, 0));
1414 tmp32 &= 0xffffff;
1415 b43_ntab_write(dev, B43_NTAB32(30, 0), tmp32);
1416
1417 b43_phy_write(dev, B43_NPHY_PHASETR_A0, 0x0125);
1418 b43_phy_write(dev, B43_NPHY_PHASETR_A1, 0x01B3);
1419 b43_phy_write(dev, B43_NPHY_PHASETR_A2, 0x0105);
1420 b43_phy_write(dev, B43_NPHY_PHASETR_B0, 0x016E);
1421 b43_phy_write(dev, B43_NPHY_PHASETR_B1, 0x00CD);
1422 b43_phy_write(dev, B43_NPHY_PHASETR_B2, 0x0020);
1423
1424 b43_phy_write(dev, B43_NPHY_C2_CLIP1_MEDGAIN, 0x000C);
1425 b43_phy_write(dev, 0x2AE, 0x000C);
1426
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001427 /* TODO */
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001428
1429 tmp16 = (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) ?
1430 0x2 : 0x9C40;
1431 b43_phy_write(dev, B43_NPHY_ENDROP_TLEN, tmp16);
1432
1433 b43_phy_maskset(dev, 0x294, 0xF0FF, 0x0700);
1434
1435 b43_ntab_write(dev, B43_NTAB32(16, 3), 0x18D);
1436 b43_ntab_write(dev, B43_NTAB32(16, 127), 0x18D);
1437
1438 b43_nphy_gain_ctrl_workarounds(dev);
1439
1440 b43_ntab_write(dev, B43_NTAB32(8, 0), 2);
1441 b43_ntab_write(dev, B43_NTAB32(8, 16), 2);
1442
1443 /* TODO */
1444
1445 b43_radio_write(dev, B2056_RX0 | B2056_RX_MIXA_MAST_BIAS, 0x00);
1446 b43_radio_write(dev, B2056_RX1 | B2056_RX_MIXA_MAST_BIAS, 0x00);
1447 b43_radio_write(dev, B2056_RX0 | B2056_RX_MIXA_BIAS_MAIN, 0x06);
1448 b43_radio_write(dev, B2056_RX1 | B2056_RX_MIXA_BIAS_MAIN, 0x06);
1449 b43_radio_write(dev, B2056_RX0 | B2056_RX_MIXA_BIAS_AUX, 0x07);
1450 b43_radio_write(dev, B2056_RX1 | B2056_RX_MIXA_BIAS_AUX, 0x07);
1451 b43_radio_write(dev, B2056_RX0 | B2056_RX_MIXA_LOB_BIAS, 0x88);
1452 b43_radio_write(dev, B2056_RX1 | B2056_RX_MIXA_LOB_BIAS, 0x88);
1453 b43_radio_write(dev, B2056_RX0 | B2056_RX_MIXG_CMFB_IDAC, 0x00);
1454 b43_radio_write(dev, B2056_RX1 | B2056_RX_MIXG_CMFB_IDAC, 0x00);
1455
1456 /* N PHY WAR TX Chain Update with hw_phytxchain as argument */
1457
Rafał Miłecki05814832011-05-18 02:06:39 +02001458 if ((sprom->boardflags2_lo & B43_BFL2_APLL_WAR &&
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001459 b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) ||
Rafał Miłecki05814832011-05-18 02:06:39 +02001460 (sprom->boardflags2_lo & B43_BFL2_GPLL_WAR &&
Rafał Miłeckiba9a6212011-03-01 21:40:41 +01001461 b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ))
1462 tmp32 = 0x00088888;
1463 else
1464 tmp32 = 0x88888888;
1465 b43_ntab_write(dev, B43_NTAB32(30, 1), tmp32);
1466 b43_ntab_write(dev, B43_NTAB32(30, 2), tmp32);
1467 b43_ntab_write(dev, B43_NTAB32(30, 3), tmp32);
1468
1469 if (dev->phy.rev == 4 &&
1470 b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
1471 b43_radio_write(dev, B2056_TX0 | B2056_TX_GMBB_IDAC,
1472 0x70);
1473 b43_radio_write(dev, B2056_TX1 | B2056_TX_GMBB_IDAC,
1474 0x70);
1475 }
1476
1477 b43_phy_write(dev, 0x224, 0x039C);
1478 b43_phy_write(dev, 0x225, 0x0357);
1479 b43_phy_write(dev, 0x226, 0x0317);
1480 b43_phy_write(dev, 0x227, 0x02D7);
1481 b43_phy_write(dev, 0x228, 0x039C);
1482 b43_phy_write(dev, 0x229, 0x0357);
1483 b43_phy_write(dev, 0x22A, 0x0317);
1484 b43_phy_write(dev, 0x22B, 0x02D7);
1485 b43_phy_write(dev, 0x22C, 0x039C);
1486 b43_phy_write(dev, 0x22D, 0x0357);
1487 b43_phy_write(dev, 0x22E, 0x0317);
1488 b43_phy_write(dev, 0x22F, 0x02D7);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001489 } else {
1490 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ &&
1491 nphy->band5g_pwrgain) {
1492 b43_radio_mask(dev, B2055_C1_TX_RF_SPARE, ~0x8);
1493 b43_radio_mask(dev, B2055_C2_TX_RF_SPARE, ~0x8);
1494 } else {
1495 b43_radio_set(dev, B2055_C1_TX_RF_SPARE, 0x8);
1496 b43_radio_set(dev, B2055_C2_TX_RF_SPARE, 0x8);
1497 }
1498
Rafał Miłeckid242b902010-12-09 20:56:00 +01001499 b43_ntab_write(dev, B43_NTAB16(8, 0x00), 0x000A);
1500 b43_ntab_write(dev, B43_NTAB16(8, 0x10), 0x000A);
1501 b43_ntab_write(dev, B43_NTAB16(8, 0x02), 0xCDAA);
1502 b43_ntab_write(dev, B43_NTAB16(8, 0x12), 0xCDAA);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001503
1504 if (dev->phy.rev < 2) {
Rafał Miłeckid242b902010-12-09 20:56:00 +01001505 b43_ntab_write(dev, B43_NTAB16(8, 0x08), 0x0000);
1506 b43_ntab_write(dev, B43_NTAB16(8, 0x18), 0x0000);
1507 b43_ntab_write(dev, B43_NTAB16(8, 0x07), 0x7AAB);
1508 b43_ntab_write(dev, B43_NTAB16(8, 0x17), 0x7AAB);
1509 b43_ntab_write(dev, B43_NTAB16(8, 0x06), 0x0800);
1510 b43_ntab_write(dev, B43_NTAB16(8, 0x16), 0x0800);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001511 }
1512
1513 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_LO1, 0x2D8);
1514 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP1, 0x301);
1515 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_LO2, 0x2D8);
1516 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP2, 0x301);
1517
Rafał Miłecki05814832011-05-18 02:06:39 +02001518 if (sprom->boardflags2_lo & 0x100 &&
Rafał Miłecki79d22322011-05-18 02:06:42 +02001519 dev->dev->board_type == 0x8B) {
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001520 delays1[0] = 0x1;
1521 delays1[5] = 0x14;
1522 }
Rafał Miłecki9501fef2010-01-30 20:18:07 +01001523 b43_nphy_set_rf_sequence(dev, 0, events1, delays1, 7);
1524 b43_nphy_set_rf_sequence(dev, 1, events2, delays2, 7);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001525
Gábor Stefanike723ef32010-08-16 22:39:15 +02001526 b43_nphy_gain_ctrl_workarounds(dev);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001527
1528 if (dev->phy.rev < 2) {
1529 if (b43_phy_read(dev, B43_NPHY_RXCTL) & 0x2)
Gábor Stefanike7f45d32010-08-16 22:39:14 +02001530 b43_hf_write(dev, b43_hf_read(dev) |
1531 B43_HF_MLADVW);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001532 } else if (dev->phy.rev == 2) {
1533 b43_phy_write(dev, B43_NPHY_CRSCHECK2, 0);
1534 b43_phy_write(dev, B43_NPHY_CRSCHECK3, 0);
1535 }
1536
1537 if (dev->phy.rev < 2)
1538 b43_phy_mask(dev, B43_NPHY_SCRAM_SIGCTL,
1539 ~B43_NPHY_SCRAM_SIGCTL_SCM);
1540
1541 /* Set phase track alpha and beta */
1542 b43_phy_write(dev, B43_NPHY_PHASETR_A0, 0x125);
1543 b43_phy_write(dev, B43_NPHY_PHASETR_A1, 0x1B3);
1544 b43_phy_write(dev, B43_NPHY_PHASETR_A2, 0x105);
1545 b43_phy_write(dev, B43_NPHY_PHASETR_B0, 0x16E);
1546 b43_phy_write(dev, B43_NPHY_PHASETR_B1, 0xCD);
1547 b43_phy_write(dev, B43_NPHY_PHASETR_B2, 0x20);
1548
1549 b43_phy_mask(dev, B43_NPHY_PIL_DW1,
Larry Fingeracd82aa2010-07-21 11:48:05 -05001550 ~B43_NPHY_PIL_DW_64QAM & 0xFFFF);
Rafał Miłecki28fd7da2010-01-30 00:12:19 +01001551 b43_phy_write(dev, B43_NPHY_TXF_20CO_S2B1, 0xB5);
1552 b43_phy_write(dev, B43_NPHY_TXF_20CO_S2B2, 0xA4);
1553 b43_phy_write(dev, B43_NPHY_TXF_20CO_S2B3, 0x00);
1554
1555 if (dev->phy.rev == 2)
1556 b43_phy_set(dev, B43_NPHY_FINERX2_CGC,
1557 B43_NPHY_FINERX2_CGC_DECGC);
1558 }
1559
1560 if (nphy->hang_avoid)
1561 b43_nphy_stay_in_carrier_search(dev, 0);
1562}
1563
Rafał Miłecki5f6393e2010-02-04 13:08:08 +01001564/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/LoadSampleTable */
1565static int b43_nphy_load_samples(struct b43_wldev *dev,
1566 struct b43_c32 *samples, u16 len) {
1567 struct b43_phy_n *nphy = dev->phy.n;
1568 u16 i;
1569 u32 *data;
1570
1571 data = kzalloc(len * sizeof(u32), GFP_KERNEL);
1572 if (!data) {
1573 b43err(dev->wl, "allocation for samples loading failed\n");
1574 return -ENOMEM;
1575 }
1576 if (nphy->hang_avoid)
1577 b43_nphy_stay_in_carrier_search(dev, 1);
1578
1579 for (i = 0; i < len; i++) {
1580 data[i] = (samples[i].i & 0x3FF << 10);
1581 data[i] |= samples[i].q & 0x3FF;
1582 }
1583 b43_ntab_write_bulk(dev, B43_NTAB32(17, 0), len, data);
1584
1585 kfree(data);
1586 if (nphy->hang_avoid)
1587 b43_nphy_stay_in_carrier_search(dev, 0);
1588 return 0;
1589}
1590
Rafał Miłecki59af0992010-01-22 01:53:16 +01001591/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/GenLoadSamples */
1592static u16 b43_nphy_gen_load_samples(struct b43_wldev *dev, u32 freq, u16 max,
1593 bool test)
1594{
1595 int i;
Rafał Miłeckif2982182010-01-25 19:00:01 +01001596 u16 bw, len, rot, angle;
Larry Fingerda860472010-01-26 16:42:02 -06001597 struct b43_c32 *samples;
Rafał Miłeckif2982182010-01-25 19:00:01 +01001598
Rafał Miłecki59af0992010-01-22 01:53:16 +01001599
1600 bw = (dev->phy.is_40mhz) ? 40 : 20;
1601 len = bw << 3;
1602
1603 if (test) {
1604 if (b43_phy_read(dev, B43_NPHY_BBCFG) & B43_NPHY_BBCFG_RSTRX)
1605 bw = 82;
1606 else
1607 bw = 80;
1608
1609 if (dev->phy.is_40mhz)
1610 bw <<= 1;
1611
1612 len = bw << 1;
1613 }
1614
Joe Perchesbaeb2ff2010-08-11 07:02:48 +00001615 samples = kcalloc(len, sizeof(struct b43_c32), GFP_KERNEL);
Rafał Miłecki40bd5202010-02-04 13:11:54 +01001616 if (!samples) {
1617 b43err(dev->wl, "allocation for samples generation failed\n");
1618 return 0;
1619 }
Rafał Miłecki59af0992010-01-22 01:53:16 +01001620 rot = (((freq * 36) / bw) << 16) / 100;
1621 angle = 0;
1622
Rafał Miłeckif2982182010-01-25 19:00:01 +01001623 for (i = 0; i < len; i++) {
1624 samples[i] = b43_cordic(angle);
1625 angle += rot;
1626 samples[i].q = CORDIC_CONVERT(samples[i].q * max);
1627 samples[i].i = CORDIC_CONVERT(samples[i].i * max);
Rafał Miłecki59af0992010-01-22 01:53:16 +01001628 }
1629
Rafał Miłecki5f6393e2010-02-04 13:08:08 +01001630 i = b43_nphy_load_samples(dev, samples, len);
Rafał Miłeckif2982182010-01-25 19:00:01 +01001631 kfree(samples);
Rafał Miłecki5f6393e2010-02-04 13:08:08 +01001632 return (i < 0) ? 0 : len;
Rafał Miłecki59af0992010-01-22 01:53:16 +01001633}
1634
Rafał Miłecki10a79872010-01-22 01:53:14 +01001635/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RunSamples */
1636static void b43_nphy_run_samples(struct b43_wldev *dev, u16 samps, u16 loops,
1637 u16 wait, bool iqmode, bool dac_test)
1638{
1639 struct b43_phy_n *nphy = dev->phy.n;
1640 int i;
1641 u16 seq_mode;
1642 u32 tmp;
1643
1644 if (nphy->hang_avoid)
1645 b43_nphy_stay_in_carrier_search(dev, true);
1646
1647 if ((nphy->bb_mult_save & 0x80000000) == 0) {
1648 tmp = b43_ntab_read(dev, B43_NTAB16(15, 87));
1649 nphy->bb_mult_save = (tmp & 0xFFFF) | 0x80000000;
1650 }
1651
1652 if (!dev->phy.is_40mhz)
1653 tmp = 0x6464;
1654 else
1655 tmp = 0x4747;
1656 b43_ntab_write(dev, B43_NTAB16(15, 87), tmp);
1657
1658 if (nphy->hang_avoid)
1659 b43_nphy_stay_in_carrier_search(dev, false);
1660
1661 b43_phy_write(dev, B43_NPHY_SAMP_DEPCNT, (samps - 1));
1662
1663 if (loops != 0xFFFF)
1664 b43_phy_write(dev, B43_NPHY_SAMP_LOOPCNT, (loops - 1));
1665 else
1666 b43_phy_write(dev, B43_NPHY_SAMP_LOOPCNT, loops);
1667
1668 b43_phy_write(dev, B43_NPHY_SAMP_WAITCNT, wait);
1669
1670 seq_mode = b43_phy_read(dev, B43_NPHY_RFSEQMODE);
1671
1672 b43_phy_set(dev, B43_NPHY_RFSEQMODE, B43_NPHY_RFSEQMODE_CAOVER);
1673 if (iqmode) {
1674 b43_phy_mask(dev, B43_NPHY_IQLOCAL_CMDGCTL, 0x7FFF);
1675 b43_phy_set(dev, B43_NPHY_IQLOCAL_CMDGCTL, 0x8000);
1676 } else {
1677 if (dac_test)
1678 b43_phy_write(dev, B43_NPHY_SAMP_CMD, 5);
1679 else
1680 b43_phy_write(dev, B43_NPHY_SAMP_CMD, 1);
1681 }
1682 for (i = 0; i < 100; i++) {
1683 if (b43_phy_read(dev, B43_NPHY_RFSEQST) & 1) {
1684 i = 0;
1685 break;
1686 }
1687 udelay(10);
1688 }
1689 if (i)
1690 b43err(dev->wl, "run samples timeout\n");
1691
1692 b43_phy_write(dev, B43_NPHY_RFSEQMODE, seq_mode);
1693}
1694
Rafał Miłecki59af0992010-01-22 01:53:16 +01001695/*
1696 * Transmits a known value for LO calibration
1697 * http://bcm-v4.sipsolutions.net/802.11/PHY/N/TXTone
1698 */
1699static int b43_nphy_tx_tone(struct b43_wldev *dev, u32 freq, u16 max_val,
1700 bool iqmode, bool dac_test)
1701{
1702 u16 samp = b43_nphy_gen_load_samples(dev, freq, max_val, dac_test);
1703 if (samp == 0)
1704 return -1;
1705 b43_nphy_run_samples(dev, samp, 0xFFFF, 0, iqmode, dac_test);
1706 return 0;
1707}
1708
Rafał Miłecki6dcd9d92010-01-15 16:24:57 +01001709/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxPwrCtrlCoefSetup */
1710static void b43_nphy_tx_pwr_ctrl_coef_setup(struct b43_wldev *dev)
1711{
1712 struct b43_phy_n *nphy = dev->phy.n;
1713 int i, j;
1714 u32 tmp;
1715 u32 cur_real, cur_imag, real_part, imag_part;
1716
1717 u16 buffer[7];
1718
1719 if (nphy->hang_avoid)
1720 b43_nphy_stay_in_carrier_search(dev, true);
1721
Rafał Miłecki91458342010-01-18 00:21:35 +01001722 b43_ntab_read_bulk(dev, B43_NTAB16(15, 80), 7, buffer);
Rafał Miłecki6dcd9d92010-01-15 16:24:57 +01001723
1724 for (i = 0; i < 2; i++) {
1725 tmp = ((buffer[i * 2] & 0x3FF) << 10) |
1726 (buffer[i * 2 + 1] & 0x3FF);
1727 b43_phy_write(dev, B43_NPHY_TABLE_ADDR,
1728 (((i + 26) << 10) | 320));
1729 for (j = 0; j < 128; j++) {
1730 b43_phy_write(dev, B43_NPHY_TABLE_DATAHI,
1731 ((tmp >> 16) & 0xFFFF));
1732 b43_phy_write(dev, B43_NPHY_TABLE_DATALO,
1733 (tmp & 0xFFFF));
1734 }
1735 }
1736
1737 for (i = 0; i < 2; i++) {
1738 tmp = buffer[5 + i];
1739 real_part = (tmp >> 8) & 0xFF;
1740 imag_part = (tmp & 0xFF);
1741 b43_phy_write(dev, B43_NPHY_TABLE_ADDR,
1742 (((i + 26) << 10) | 448));
1743
1744 if (dev->phy.rev >= 3) {
1745 cur_real = real_part;
1746 cur_imag = imag_part;
1747 tmp = ((cur_real & 0xFF) << 8) | (cur_imag & 0xFF);
1748 }
1749
1750 for (j = 0; j < 128; j++) {
1751 if (dev->phy.rev < 3) {
1752 cur_real = (real_part * loscale[j] + 128) >> 8;
1753 cur_imag = (imag_part * loscale[j] + 128) >> 8;
1754 tmp = ((cur_real & 0xFF) << 8) |
1755 (cur_imag & 0xFF);
1756 }
1757 b43_phy_write(dev, B43_NPHY_TABLE_DATAHI,
1758 ((tmp >> 16) & 0xFFFF));
1759 b43_phy_write(dev, B43_NPHY_TABLE_DATALO,
1760 (tmp & 0xFFFF));
1761 }
1762 }
1763
1764 if (dev->phy.rev >= 3) {
1765 b43_shm_write16(dev, B43_SHM_SHARED,
1766 B43_SHM_SH_NPHY_TXPWR_INDX0, 0xFFFF);
1767 b43_shm_write16(dev, B43_SHM_SHARED,
1768 B43_SHM_SH_NPHY_TXPWR_INDX1, 0xFFFF);
1769 }
1770
1771 if (nphy->hang_avoid)
1772 b43_nphy_stay_in_carrier_search(dev, false);
1773}
1774
Rafał Miłecki9501fef2010-01-30 20:18:07 +01001775/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SetRfSeq */
1776static void b43_nphy_set_rf_sequence(struct b43_wldev *dev, u8 cmd,
1777 u8 *events, u8 *delays, u8 length)
1778{
1779 struct b43_phy_n *nphy = dev->phy.n;
1780 u8 i;
1781 u8 end = (dev->phy.rev >= 3) ? 0x1F : 0x0F;
1782 u16 offset1 = cmd << 4;
1783 u16 offset2 = offset1 + 0x80;
1784
1785 if (nphy->hang_avoid)
1786 b43_nphy_stay_in_carrier_search(dev, true);
1787
1788 b43_ntab_write_bulk(dev, B43_NTAB8(7, offset1), length, events);
1789 b43_ntab_write_bulk(dev, B43_NTAB8(7, offset2), length, delays);
1790
1791 for (i = length; i < 16; i++) {
1792 b43_ntab_write(dev, B43_NTAB8(7, offset1 + i), end);
1793 b43_ntab_write(dev, B43_NTAB8(7, offset2 + i), 1);
1794 }
1795
1796 if (nphy->hang_avoid)
1797 b43_nphy_stay_in_carrier_search(dev, false);
1798}
1799
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +01001800/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/ForceRFSeq */
Michael Buesch95b66ba2008-01-18 01:09:25 +01001801static void b43_nphy_force_rf_sequence(struct b43_wldev *dev,
1802 enum b43_nphy_rf_sequence seq)
1803{
1804 static const u16 trigger[] = {
1805 [B43_RFSEQ_RX2TX] = B43_NPHY_RFSEQTR_RX2TX,
1806 [B43_RFSEQ_TX2RX] = B43_NPHY_RFSEQTR_TX2RX,
1807 [B43_RFSEQ_RESET2RX] = B43_NPHY_RFSEQTR_RST2RX,
1808 [B43_RFSEQ_UPDATE_GAINH] = B43_NPHY_RFSEQTR_UPGH,
1809 [B43_RFSEQ_UPDATE_GAINL] = B43_NPHY_RFSEQTR_UPGL,
1810 [B43_RFSEQ_UPDATE_GAINU] = B43_NPHY_RFSEQTR_UPGU,
1811 };
1812 int i;
Rafał Miłeckic57199b2010-01-17 13:04:08 +01001813 u16 seq_mode = b43_phy_read(dev, B43_NPHY_RFSEQMODE);
Michael Buesch95b66ba2008-01-18 01:09:25 +01001814
1815 B43_WARN_ON(seq >= ARRAY_SIZE(trigger));
1816
1817 b43_phy_set(dev, B43_NPHY_RFSEQMODE,
1818 B43_NPHY_RFSEQMODE_CAOVER | B43_NPHY_RFSEQMODE_TROVER);
1819 b43_phy_set(dev, B43_NPHY_RFSEQTR, trigger[seq]);
1820 for (i = 0; i < 200; i++) {
1821 if (!(b43_phy_read(dev, B43_NPHY_RFSEQST) & trigger[seq]))
1822 goto ok;
1823 msleep(1);
1824 }
1825 b43err(dev->wl, "RF sequence status timeout\n");
1826ok:
Rafał Miłeckic57199b2010-01-17 13:04:08 +01001827 b43_phy_write(dev, B43_NPHY_RFSEQMODE, seq_mode);
Michael Buesch95b66ba2008-01-18 01:09:25 +01001828}
1829
Rafał Miłecki75377b22010-01-22 01:53:13 +01001830/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RFCtrlOverride */
1831static void b43_nphy_rf_control_override(struct b43_wldev *dev, u16 field,
1832 u16 value, u8 core, bool off)
1833{
1834 int i;
1835 u8 index = fls(field);
1836 u8 addr, en_addr, val_addr;
1837 /* we expect only one bit set */
Rafał Miłecki3ed0fac2010-01-25 18:59:58 +01001838 B43_WARN_ON(field & (~(1 << (index - 1))));
Rafał Miłecki75377b22010-01-22 01:53:13 +01001839
1840 if (dev->phy.rev >= 3) {
1841 const struct nphy_rf_control_override_rev3 *rf_ctrl;
1842 for (i = 0; i < 2; i++) {
1843 if (index == 0 || index == 16) {
1844 b43err(dev->wl,
1845 "Unsupported RF Ctrl Override call\n");
1846 return;
1847 }
1848
1849 rf_ctrl = &tbl_rf_control_override_rev3[index - 1];
1850 en_addr = B43_PHY_N((i == 0) ?
1851 rf_ctrl->en_addr0 : rf_ctrl->en_addr1);
1852 val_addr = B43_PHY_N((i == 0) ?
1853 rf_ctrl->val_addr0 : rf_ctrl->val_addr1);
1854
1855 if (off) {
1856 b43_phy_mask(dev, en_addr, ~(field));
1857 b43_phy_mask(dev, val_addr,
1858 ~(rf_ctrl->val_mask));
1859 } else {
1860 if (core == 0 || ((1 << core) & i) != 0) {
1861 b43_phy_set(dev, en_addr, field);
1862 b43_phy_maskset(dev, val_addr,
1863 ~(rf_ctrl->val_mask),
1864 (value << rf_ctrl->val_shift));
1865 }
1866 }
1867 }
1868 } else {
1869 const struct nphy_rf_control_override_rev2 *rf_ctrl;
1870 if (off) {
1871 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER, ~(field));
1872 value = 0;
1873 } else {
1874 b43_phy_set(dev, B43_NPHY_RFCTL_OVER, field);
1875 }
1876
1877 for (i = 0; i < 2; i++) {
1878 if (index <= 1 || index == 16) {
1879 b43err(dev->wl,
1880 "Unsupported RF Ctrl Override call\n");
1881 return;
1882 }
1883
1884 if (index == 2 || index == 10 ||
1885 (index >= 13 && index <= 15)) {
1886 core = 1;
1887 }
1888
1889 rf_ctrl = &tbl_rf_control_override_rev2[index - 2];
1890 addr = B43_PHY_N((i == 0) ?
1891 rf_ctrl->addr0 : rf_ctrl->addr1);
1892
1893 if ((core & (1 << i)) != 0)
1894 b43_phy_maskset(dev, addr, ~(rf_ctrl->bmask),
1895 (value << rf_ctrl->shift));
1896
1897 b43_phy_set(dev, B43_NPHY_RFCTL_OVER, 0x1);
1898 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
1899 B43_NPHY_RFCTL_CMD_START);
1900 udelay(1);
1901 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER, 0xFFFE);
1902 }
1903 }
1904}
1905
Rafał Miłecki67cbc3e2010-02-04 12:23:08 +01001906/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RFCtrlIntcOverride */
1907static void b43_nphy_rf_control_intc_override(struct b43_wldev *dev, u8 field,
1908 u16 value, u8 core)
1909{
1910 u8 i, j;
1911 u16 reg, tmp, val;
1912
1913 B43_WARN_ON(dev->phy.rev < 3);
1914 B43_WARN_ON(field > 4);
1915
1916 for (i = 0; i < 2; i++) {
1917 if ((core == 1 && i == 1) || (core == 2 && !i))
1918 continue;
1919
1920 reg = (i == 0) ?
1921 B43_NPHY_RFCTL_INTC1 : B43_NPHY_RFCTL_INTC2;
1922 b43_phy_mask(dev, reg, 0xFBFF);
1923
1924 switch (field) {
1925 case 0:
1926 b43_phy_write(dev, reg, 0);
1927 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
1928 break;
1929 case 1:
1930 if (!i) {
1931 b43_phy_maskset(dev, B43_NPHY_RFCTL_INTC1,
1932 0xFC3F, (value << 6));
1933 b43_phy_maskset(dev, B43_NPHY_TXF_40CO_B1S1,
1934 0xFFFE, 1);
1935 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
1936 B43_NPHY_RFCTL_CMD_START);
1937 for (j = 0; j < 100; j++) {
1938 if (b43_phy_read(dev, B43_NPHY_RFCTL_CMD) & B43_NPHY_RFCTL_CMD_START) {
1939 j = 0;
1940 break;
1941 }
1942 udelay(10);
1943 }
1944 if (j)
1945 b43err(dev->wl,
1946 "intc override timeout\n");
1947 b43_phy_mask(dev, B43_NPHY_TXF_40CO_B1S1,
1948 0xFFFE);
1949 } else {
1950 b43_phy_maskset(dev, B43_NPHY_RFCTL_INTC2,
1951 0xFC3F, (value << 6));
1952 b43_phy_maskset(dev, B43_NPHY_RFCTL_OVER,
1953 0xFFFE, 1);
1954 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
1955 B43_NPHY_RFCTL_CMD_RXTX);
1956 for (j = 0; j < 100; j++) {
1957 if (b43_phy_read(dev, B43_NPHY_RFCTL_CMD) & B43_NPHY_RFCTL_CMD_RXTX) {
1958 j = 0;
1959 break;
1960 }
1961 udelay(10);
1962 }
1963 if (j)
1964 b43err(dev->wl,
1965 "intc override timeout\n");
1966 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER,
1967 0xFFFE);
1968 }
1969 break;
1970 case 2:
1971 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
1972 tmp = 0x0020;
1973 val = value << 5;
1974 } else {
1975 tmp = 0x0010;
1976 val = value << 4;
1977 }
1978 b43_phy_maskset(dev, reg, ~tmp, val);
1979 break;
1980 case 3:
1981 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
1982 tmp = 0x0001;
1983 val = value;
1984 } else {
1985 tmp = 0x0004;
1986 val = value << 2;
1987 }
1988 b43_phy_maskset(dev, reg, ~tmp, val);
1989 break;
1990 case 4:
1991 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
1992 tmp = 0x0002;
1993 val = value << 1;
1994 } else {
1995 tmp = 0x0008;
1996 val = value << 3;
1997 }
1998 b43_phy_maskset(dev, reg, ~tmp, val);
1999 break;
2000 }
2001 }
2002}
2003
Rafał Miłeckibec18642010-11-18 13:28:00 +01002004/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/BPHYInit */
Michael Buesch95b66ba2008-01-18 01:09:25 +01002005static void b43_nphy_bphy_init(struct b43_wldev *dev)
2006{
2007 unsigned int i;
2008 u16 val;
2009
2010 val = 0x1E1F;
Rafał Miłeckifee613b2010-11-18 21:11:41 +01002011 for (i = 0; i < 16; i++) {
Michael Buesch95b66ba2008-01-18 01:09:25 +01002012 b43_phy_write(dev, B43_PHY_N_BMODE(0x88 + i), val);
2013 val -= 0x202;
2014 }
2015 val = 0x3E3F;
2016 for (i = 0; i < 16; i++) {
Rafał Miłeckifee613b2010-11-18 21:11:41 +01002017 b43_phy_write(dev, B43_PHY_N_BMODE(0x98 + i), val);
Michael Buesch95b66ba2008-01-18 01:09:25 +01002018 val -= 0x202;
2019 }
2020 b43_phy_write(dev, B43_PHY_N_BMODE(0x38), 0x668);
2021}
2022
Rafał Miłecki3c956272010-01-15 14:38:32 +01002023/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/ScaleOffsetRssi */
2024static void b43_nphy_scale_offset_rssi(struct b43_wldev *dev, u16 scale,
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002025 s8 offset, u8 core, u8 rail,
2026 enum b43_nphy_rssi_type type)
Rafał Miłecki3c956272010-01-15 14:38:32 +01002027{
2028 u16 tmp;
2029 bool core1or5 = (core == 1) || (core == 5);
2030 bool core2or5 = (core == 2) || (core == 5);
2031
2032 offset = clamp_val(offset, -32, 31);
2033 tmp = ((scale & 0x3F) << 8) | (offset & 0x3F);
2034
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002035 if (core1or5 && (rail == 0) && (type == B43_NPHY_RSSI_Z))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002036 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_Z, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002037 if (core1or5 && (rail == 1) && (type == B43_NPHY_RSSI_Z))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002038 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_Z, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002039 if (core2or5 && (rail == 0) && (type == B43_NPHY_RSSI_Z))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002040 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_Z, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002041 if (core2or5 && (rail == 1) && (type == B43_NPHY_RSSI_Z))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002042 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_Z, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002043
2044 if (core1or5 && (rail == 0) && (type == B43_NPHY_RSSI_X))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002045 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_X, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002046 if (core1or5 && (rail == 1) && (type == B43_NPHY_RSSI_X))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002047 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_X, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002048 if (core2or5 && (rail == 0) && (type == B43_NPHY_RSSI_X))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002049 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_X, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002050 if (core2or5 && (rail == 1) && (type == B43_NPHY_RSSI_X))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002051 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_X, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002052
2053 if (core1or5 && (rail == 0) && (type == B43_NPHY_RSSI_Y))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002054 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_Y, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002055 if (core1or5 && (rail == 1) && (type == B43_NPHY_RSSI_Y))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002056 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_Y, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002057 if (core2or5 && (rail == 0) && (type == B43_NPHY_RSSI_Y))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002058 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_Y, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002059 if (core2or5 && (rail == 1) && (type == B43_NPHY_RSSI_Y))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002060 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_Y, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002061
2062 if (core1or5 && (rail == 0) && (type == B43_NPHY_RSSI_TBD))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002063 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_TBD, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002064 if (core1or5 && (rail == 1) && (type == B43_NPHY_RSSI_TBD))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002065 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_TBD, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002066 if (core2or5 && (rail == 0) && (type == B43_NPHY_RSSI_TBD))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002067 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_TBD, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002068 if (core2or5 && (rail == 1) && (type == B43_NPHY_RSSI_TBD))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002069 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_TBD, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002070
2071 if (core1or5 && (rail == 0) && (type == B43_NPHY_RSSI_PWRDET))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002072 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_PWRDET, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002073 if (core1or5 && (rail == 1) && (type == B43_NPHY_RSSI_PWRDET))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002074 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_PWRDET, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002075 if (core2or5 && (rail == 0) && (type == B43_NPHY_RSSI_PWRDET))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002076 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_PWRDET, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002077 if (core2or5 && (rail == 1) && (type == B43_NPHY_RSSI_PWRDET))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002078 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_PWRDET, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002079
2080 if (core1or5 && (type == B43_NPHY_RSSI_TSSI_I))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002081 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_TSSI, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002082 if (core2or5 && (type == B43_NPHY_RSSI_TSSI_I))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002083 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_TSSI, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002084
2085 if (core1or5 && (type == B43_NPHY_RSSI_TSSI_Q))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002086 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_TSSI, tmp);
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002087 if (core2or5 && (type == B43_NPHY_RSSI_TSSI_Q))
Rafał Miłecki3c956272010-01-15 14:38:32 +01002088 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_TSSI, tmp);
2089}
2090
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002091static void b43_nphy_rev2_rssi_select(struct b43_wldev *dev, u8 code, u8 type)
Rafał Miłecki3c956272010-01-15 14:38:32 +01002092{
2093 u16 val;
2094
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002095 if (type < 3)
2096 val = 0;
2097 else if (type == 6)
2098 val = 1;
2099 else if (type == 3)
2100 val = 2;
2101 else
2102 val = 3;
Rafał Miłecki3c956272010-01-15 14:38:32 +01002103
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002104 val = (val << 12) | (val << 14);
2105 b43_phy_maskset(dev, B43_NPHY_AFECTL_C1, 0x0FFF, val);
2106 b43_phy_maskset(dev, B43_NPHY_AFECTL_C2, 0x0FFF, val);
Rafał Miłecki3c956272010-01-15 14:38:32 +01002107
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002108 if (type < 3) {
2109 b43_phy_maskset(dev, B43_NPHY_RFCTL_RSSIO1, 0xFFCF,
2110 (type + 1) << 4);
2111 b43_phy_maskset(dev, B43_NPHY_RFCTL_RSSIO2, 0xFFCF,
2112 (type + 1) << 4);
2113 }
2114
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002115 if (code == 0) {
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002116 b43_phy_mask(dev, B43_NPHY_AFECTL_OVER, ~0x3000);
Rafał Miłecki3c956272010-01-15 14:38:32 +01002117 if (type < 3) {
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002118 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
2119 ~(B43_NPHY_RFCTL_CMD_RXEN |
2120 B43_NPHY_RFCTL_CMD_CORESEL));
2121 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER,
2122 ~(0x1 << 12 |
2123 0x1 << 5 |
2124 0x1 << 1 |
2125 0x1));
2126 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
2127 ~B43_NPHY_RFCTL_CMD_START);
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002128 udelay(20);
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002129 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER, ~0x1);
Rafał Miłecki3c956272010-01-15 14:38:32 +01002130 }
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002131 } else {
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002132 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x3000);
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002133 if (type < 3) {
2134 b43_phy_maskset(dev, B43_NPHY_RFCTL_CMD,
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002135 ~(B43_NPHY_RFCTL_CMD_RXEN |
2136 B43_NPHY_RFCTL_CMD_CORESEL),
2137 (B43_NPHY_RFCTL_CMD_RXEN |
2138 code << B43_NPHY_RFCTL_CMD_CORESEL_SHIFT));
2139 b43_phy_set(dev, B43_NPHY_RFCTL_OVER,
2140 (0x1 << 12 |
2141 0x1 << 5 |
2142 0x1 << 1 |
2143 0x1));
2144 b43_phy_set(dev, B43_NPHY_RFCTL_CMD,
2145 B43_NPHY_RFCTL_CMD_START);
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002146 udelay(20);
Rafał Miłecki99f6c2e2010-11-30 22:33:14 +01002147 b43_phy_mask(dev, B43_NPHY_RFCTL_OVER, ~0x1);
Rafał Miłecki3c956272010-01-15 14:38:32 +01002148 }
2149 }
2150}
2151
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002152static void b43_nphy_rev3_rssi_select(struct b43_wldev *dev, u8 code, u8 type)
2153{
Rafał Miłecki6e3b15a2010-01-30 20:18:04 +01002154 struct b43_phy_n *nphy = dev->phy.n;
2155 u8 i;
2156 u16 reg, val;
2157
2158 if (code == 0) {
2159 b43_phy_mask(dev, B43_NPHY_AFECTL_OVER1, 0xFDFF);
2160 b43_phy_mask(dev, B43_NPHY_AFECTL_OVER, 0xFDFF);
2161 b43_phy_mask(dev, B43_NPHY_AFECTL_C1, 0xFCFF);
2162 b43_phy_mask(dev, B43_NPHY_AFECTL_C2, 0xFCFF);
2163 b43_phy_mask(dev, B43_NPHY_TXF_40CO_B1S0, 0xFFDF);
2164 b43_phy_mask(dev, B43_NPHY_TXF_40CO_B32S1, 0xFFDF);
2165 b43_phy_mask(dev, B43_NPHY_RFCTL_LUT_TRSW_UP1, 0xFFC3);
2166 b43_phy_mask(dev, B43_NPHY_RFCTL_LUT_TRSW_UP2, 0xFFC3);
2167 } else {
2168 for (i = 0; i < 2; i++) {
2169 if ((code == 1 && i == 1) || (code == 2 && !i))
2170 continue;
2171
2172 reg = (i == 0) ?
2173 B43_NPHY_AFECTL_OVER1 : B43_NPHY_AFECTL_OVER;
2174 b43_phy_maskset(dev, reg, 0xFDFF, 0x0200);
2175
2176 if (type < 3) {
2177 reg = (i == 0) ?
2178 B43_NPHY_AFECTL_C1 :
2179 B43_NPHY_AFECTL_C2;
2180 b43_phy_maskset(dev, reg, 0xFCFF, 0);
2181
2182 reg = (i == 0) ?
2183 B43_NPHY_RFCTL_LUT_TRSW_UP1 :
2184 B43_NPHY_RFCTL_LUT_TRSW_UP2;
2185 b43_phy_maskset(dev, reg, 0xFFC3, 0);
2186
2187 if (type == 0)
2188 val = (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) ? 4 : 8;
2189 else if (type == 1)
2190 val = 16;
2191 else
2192 val = 32;
2193 b43_phy_set(dev, reg, val);
2194
2195 reg = (i == 0) ?
2196 B43_NPHY_TXF_40CO_B1S0 :
2197 B43_NPHY_TXF_40CO_B32S1;
2198 b43_phy_set(dev, reg, 0x0020);
2199 } else {
2200 if (type == 6)
2201 val = 0x0100;
2202 else if (type == 3)
2203 val = 0x0200;
2204 else
2205 val = 0x0300;
2206
2207 reg = (i == 0) ?
2208 B43_NPHY_AFECTL_C1 :
2209 B43_NPHY_AFECTL_C2;
2210
2211 b43_phy_maskset(dev, reg, 0xFCFF, val);
2212 b43_phy_maskset(dev, reg, 0xF3FF, val << 2);
2213
2214 if (type != 3 && type != 6) {
2215 enum ieee80211_band band =
2216 b43_current_band(dev->wl);
2217
2218 if ((nphy->ipa2g_on &&
2219 band == IEEE80211_BAND_2GHZ) ||
2220 (nphy->ipa5g_on &&
2221 band == IEEE80211_BAND_5GHZ))
2222 val = (band == IEEE80211_BAND_5GHZ) ? 0xC : 0xE;
2223 else
2224 val = 0x11;
2225 reg = (i == 0) ? 0x2000 : 0x3000;
2226 reg |= B2055_PADDRV;
2227 b43_radio_write16(dev, reg, val);
2228
2229 reg = (i == 0) ?
2230 B43_NPHY_AFECTL_OVER1 :
2231 B43_NPHY_AFECTL_OVER;
2232 b43_phy_set(dev, reg, 0x0200);
2233 }
2234 }
2235 }
2236 }
Rafał Miłecki99b82c42010-01-30 20:18:03 +01002237}
2238
2239/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RSSISel */
2240static void b43_nphy_rssi_select(struct b43_wldev *dev, u8 code, u8 type)
2241{
2242 if (dev->phy.rev >= 3)
2243 b43_nphy_rev3_rssi_select(dev, code, type);
2244 else
2245 b43_nphy_rev2_rssi_select(dev, code, type);
2246}
2247
Rafał Miłeckidfb4aa52010-01-15 14:45:13 +01002248/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SetRssi2055Vcm */
2249static void b43_nphy_set_rssi_2055_vcm(struct b43_wldev *dev, u8 type, u8 *buf)
2250{
2251 int i;
2252 for (i = 0; i < 2; i++) {
2253 if (type == 2) {
2254 if (i == 0) {
2255 b43_radio_maskset(dev, B2055_C1_B0NB_RSSIVCM,
2256 0xFC, buf[0]);
2257 b43_radio_maskset(dev, B2055_C1_RX_BB_RSSICTL5,
2258 0xFC, buf[1]);
2259 } else {
2260 b43_radio_maskset(dev, B2055_C2_B0NB_RSSIVCM,
2261 0xFC, buf[2 * i]);
2262 b43_radio_maskset(dev, B2055_C2_RX_BB_RSSICTL5,
2263 0xFC, buf[2 * i + 1]);
2264 }
2265 } else {
2266 if (i == 0)
2267 b43_radio_maskset(dev, B2055_C1_RX_BB_RSSICTL5,
2268 0xF3, buf[0] << 2);
2269 else
2270 b43_radio_maskset(dev, B2055_C2_RX_BB_RSSICTL5,
2271 0xF3, buf[2 * i + 1] << 2);
2272 }
2273 }
2274}
2275
2276/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/PollRssi */
2277static int b43_nphy_poll_rssi(struct b43_wldev *dev, u8 type, s32 *buf,
2278 u8 nsamp)
2279{
2280 int i;
2281 int out;
2282 u16 save_regs_phy[9];
2283 u16 s[2];
2284
2285 if (dev->phy.rev >= 3) {
2286 save_regs_phy[0] = b43_phy_read(dev,
2287 B43_NPHY_RFCTL_LUT_TRSW_UP1);
2288 save_regs_phy[1] = b43_phy_read(dev,
2289 B43_NPHY_RFCTL_LUT_TRSW_UP2);
2290 save_regs_phy[2] = b43_phy_read(dev, B43_NPHY_AFECTL_C1);
2291 save_regs_phy[3] = b43_phy_read(dev, B43_NPHY_AFECTL_C2);
2292 save_regs_phy[4] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER1);
2293 save_regs_phy[5] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
2294 save_regs_phy[6] = b43_phy_read(dev, B43_NPHY_TXF_40CO_B1S0);
2295 save_regs_phy[7] = b43_phy_read(dev, B43_NPHY_TXF_40CO_B32S1);
John W. Linville2eeb6fd2011-04-29 14:54:27 -04002296 save_regs_phy[8] = 0;
Rafał Miłecki05db8c52011-02-21 19:45:35 +01002297 } else {
Rafał Miłeckia529cec2010-11-18 21:11:42 +01002298 save_regs_phy[0] = b43_phy_read(dev, B43_NPHY_AFECTL_C1);
2299 save_regs_phy[1] = b43_phy_read(dev, B43_NPHY_AFECTL_C2);
2300 save_regs_phy[2] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
2301 save_regs_phy[3] = b43_phy_read(dev, B43_NPHY_RFCTL_CMD);
2302 save_regs_phy[4] = b43_phy_read(dev, B43_NPHY_RFCTL_OVER);
2303 save_regs_phy[5] = b43_phy_read(dev, B43_NPHY_RFCTL_RSSIO1);
2304 save_regs_phy[6] = b43_phy_read(dev, B43_NPHY_RFCTL_RSSIO2);
John W. Linville2eeb6fd2011-04-29 14:54:27 -04002305 save_regs_phy[7] = 0;
2306 save_regs_phy[8] = 0;
Rafał Miłeckidfb4aa52010-01-15 14:45:13 +01002307 }
2308
2309 b43_nphy_rssi_select(dev, 5, type);
2310
2311 if (dev->phy.rev < 2) {
2312 save_regs_phy[8] = b43_phy_read(dev, B43_NPHY_GPIO_SEL);
2313 b43_phy_write(dev, B43_NPHY_GPIO_SEL, 5);
2314 }
2315
2316 for (i = 0; i < 4; i++)
2317 buf[i] = 0;
2318
2319 for (i = 0; i < nsamp; i++) {
2320 if (dev->phy.rev < 2) {
2321 s[0] = b43_phy_read(dev, B43_NPHY_GPIO_LOOUT);
2322 s[1] = b43_phy_read(dev, B43_NPHY_GPIO_HIOUT);
2323 } else {
2324 s[0] = b43_phy_read(dev, B43_NPHY_RSSI1);
2325 s[1] = b43_phy_read(dev, B43_NPHY_RSSI2);
2326 }
2327
2328 buf[0] += ((s8)((s[0] & 0x3F) << 2)) >> 2;
2329 buf[1] += ((s8)(((s[0] >> 8) & 0x3F) << 2)) >> 2;
2330 buf[2] += ((s8)((s[1] & 0x3F) << 2)) >> 2;
2331 buf[3] += ((s8)(((s[1] >> 8) & 0x3F) << 2)) >> 2;
2332 }
2333 out = (buf[0] & 0xFF) << 24 | (buf[1] & 0xFF) << 16 |
2334 (buf[2] & 0xFF) << 8 | (buf[3] & 0xFF);
2335
2336 if (dev->phy.rev < 2)
2337 b43_phy_write(dev, B43_NPHY_GPIO_SEL, save_regs_phy[8]);
2338
2339 if (dev->phy.rev >= 3) {
2340 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP1,
2341 save_regs_phy[0]);
2342 b43_phy_write(dev, B43_NPHY_RFCTL_LUT_TRSW_UP2,
2343 save_regs_phy[1]);
2344 b43_phy_write(dev, B43_NPHY_AFECTL_C1, save_regs_phy[2]);
2345 b43_phy_write(dev, B43_NPHY_AFECTL_C2, save_regs_phy[3]);
2346 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, save_regs_phy[4]);
2347 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, save_regs_phy[5]);
2348 b43_phy_write(dev, B43_NPHY_TXF_40CO_B1S0, save_regs_phy[6]);
2349 b43_phy_write(dev, B43_NPHY_TXF_40CO_B32S1, save_regs_phy[7]);
Rafał Miłecki05db8c52011-02-21 19:45:35 +01002350 } else {
Rafał Miłeckia529cec2010-11-18 21:11:42 +01002351 b43_phy_write(dev, B43_NPHY_AFECTL_C1, save_regs_phy[0]);
2352 b43_phy_write(dev, B43_NPHY_AFECTL_C2, save_regs_phy[1]);
2353 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, save_regs_phy[2]);
2354 b43_phy_write(dev, B43_NPHY_RFCTL_CMD, save_regs_phy[3]);
2355 b43_phy_write(dev, B43_NPHY_RFCTL_OVER, save_regs_phy[4]);
2356 b43_phy_write(dev, B43_NPHY_RFCTL_RSSIO1, save_regs_phy[5]);
2357 b43_phy_write(dev, B43_NPHY_RFCTL_RSSIO2, save_regs_phy[6]);
Rafał Miłeckidfb4aa52010-01-15 14:45:13 +01002358 }
2359
2360 return out;
2361}
2362
Rafał Miłecki4cb99772010-01-15 13:40:58 +01002363/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RSSICal */
2364static void b43_nphy_rev2_rssi_cal(struct b43_wldev *dev, u8 type)
Michael Buesch95b66ba2008-01-18 01:09:25 +01002365{
Rafał Miłecki90b97382010-01-15 14:48:21 +01002366 int i, j;
2367 u8 state[4];
2368 u8 code, val;
2369 u16 class, override;
2370 u8 regs_save_radio[2];
2371 u16 regs_save_phy[2];
Rafał Miłecki8cbe6e62010-11-28 12:59:45 +01002372
Rafał Miłecki90b97382010-01-15 14:48:21 +01002373 s8 offset[4];
Rafał Miłecki8cbe6e62010-11-28 12:59:45 +01002374 u8 core;
2375 u8 rail;
Rafał Miłecki90b97382010-01-15 14:48:21 +01002376
2377 u16 clip_state[2];
2378 u16 clip_off[2] = { 0xFFFF, 0xFFFF };
2379 s32 results_min[4] = { };
2380 u8 vcm_final[4] = { };
2381 s32 results[4][4] = { };
2382 s32 miniq[4][2] = { };
2383
2384 if (type == 2) {
2385 code = 0;
2386 val = 6;
2387 } else if (type < 2) {
2388 code = 25;
2389 val = 4;
2390 } else {
2391 B43_WARN_ON(1);
2392 return;
2393 }
2394
2395 class = b43_nphy_classifier(dev, 0, 0);
2396 b43_nphy_classifier(dev, 7, 4);
2397 b43_nphy_read_clip_detection(dev, clip_state);
2398 b43_nphy_write_clip_detection(dev, clip_off);
2399
2400 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
2401 override = 0x140;
2402 else
2403 override = 0x110;
2404
2405 regs_save_phy[0] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC1);
2406 regs_save_radio[0] = b43_radio_read16(dev, B2055_C1_PD_RXTX);
2407 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, override);
2408 b43_radio_write16(dev, B2055_C1_PD_RXTX, val);
2409
2410 regs_save_phy[1] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC2);
2411 regs_save_radio[1] = b43_radio_read16(dev, B2055_C2_PD_RXTX);
2412 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, override);
2413 b43_radio_write16(dev, B2055_C2_PD_RXTX, val);
2414
2415 state[0] = b43_radio_read16(dev, B2055_C1_PD_RSSIMISC) & 0x07;
2416 state[1] = b43_radio_read16(dev, B2055_C2_PD_RSSIMISC) & 0x07;
2417 b43_radio_mask(dev, B2055_C1_PD_RSSIMISC, 0xF8);
2418 b43_radio_mask(dev, B2055_C2_PD_RSSIMISC, 0xF8);
2419 state[2] = b43_radio_read16(dev, B2055_C1_SP_RSSI) & 0x07;
2420 state[3] = b43_radio_read16(dev, B2055_C2_SP_RSSI) & 0x07;
2421
2422 b43_nphy_rssi_select(dev, 5, type);
2423 b43_nphy_scale_offset_rssi(dev, 0, 0, 5, 0, type);
2424 b43_nphy_scale_offset_rssi(dev, 0, 0, 5, 1, type);
2425
2426 for (i = 0; i < 4; i++) {
2427 u8 tmp[4];
2428 for (j = 0; j < 4; j++)
2429 tmp[j] = i;
2430 if (type != 1)
2431 b43_nphy_set_rssi_2055_vcm(dev, type, tmp);
2432 b43_nphy_poll_rssi(dev, type, results[i], 8);
2433 if (type < 2)
2434 for (j = 0; j < 2; j++)
2435 miniq[i][j] = min(results[i][2 * j],
2436 results[i][2 * j + 1]);
2437 }
2438
2439 for (i = 0; i < 4; i++) {
2440 s32 mind = 40;
2441 u8 minvcm = 0;
2442 s32 minpoll = 249;
2443 s32 curr;
2444 for (j = 0; j < 4; j++) {
2445 if (type == 2)
2446 curr = abs(results[j][i]);
2447 else
2448 curr = abs(miniq[j][i / 2] - code * 8);
2449
2450 if (curr < mind) {
2451 mind = curr;
2452 minvcm = j;
2453 }
2454
2455 if (results[j][i] < minpoll)
2456 minpoll = results[j][i];
2457 }
2458 results_min[i] = minpoll;
2459 vcm_final[i] = minvcm;
2460 }
2461
2462 if (type != 1)
2463 b43_nphy_set_rssi_2055_vcm(dev, type, vcm_final);
2464
2465 for (i = 0; i < 4; i++) {
2466 offset[i] = (code * 8) - results[vcm_final[i]][i];
2467
2468 if (offset[i] < 0)
2469 offset[i] = -((abs(offset[i]) + 4) / 8);
2470 else
2471 offset[i] = (offset[i] + 4) / 8;
2472
2473 if (results_min[i] == 248)
2474 offset[i] = code - 32;
2475
Rafał Miłecki8cbe6e62010-11-28 12:59:45 +01002476 core = (i / 2) ? 2 : 1;
2477 rail = (i % 2) ? 1 : 0;
2478
2479 b43_nphy_scale_offset_rssi(dev, 0, offset[i], core, rail,
2480 type);
Rafał Miłecki90b97382010-01-15 14:48:21 +01002481 }
2482
2483 b43_radio_maskset(dev, B2055_C1_PD_RSSIMISC, 0xF8, state[0]);
Rafał Miłecki0b81c232010-11-18 21:11:43 +01002484 b43_radio_maskset(dev, B2055_C2_PD_RSSIMISC, 0xF8, state[1]);
Rafał Miłecki90b97382010-01-15 14:48:21 +01002485
2486 switch (state[2]) {
2487 case 1:
2488 b43_nphy_rssi_select(dev, 1, 2);
2489 break;
2490 case 4:
2491 b43_nphy_rssi_select(dev, 1, 0);
2492 break;
2493 case 2:
2494 b43_nphy_rssi_select(dev, 1, 1);
2495 break;
2496 default:
2497 b43_nphy_rssi_select(dev, 1, 1);
2498 break;
2499 }
2500
2501 switch (state[3]) {
2502 case 1:
2503 b43_nphy_rssi_select(dev, 2, 2);
2504 break;
2505 case 4:
2506 b43_nphy_rssi_select(dev, 2, 0);
2507 break;
2508 default:
2509 b43_nphy_rssi_select(dev, 2, 1);
2510 break;
2511 }
2512
2513 b43_nphy_rssi_select(dev, 0, type);
2514
2515 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, regs_save_phy[0]);
2516 b43_radio_write16(dev, B2055_C1_PD_RXTX, regs_save_radio[0]);
2517 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, regs_save_phy[1]);
2518 b43_radio_write16(dev, B2055_C2_PD_RXTX, regs_save_radio[1]);
2519
2520 b43_nphy_classifier(dev, 7, class);
2521 b43_nphy_write_clip_detection(dev, clip_state);
Rafał Miłecki8c1d5a72010-11-28 12:59:44 +01002522 /* Specs don't say about reset here, but it makes wl and b43 dumps
2523 identical, it really seems wl performs this */
2524 b43_nphy_reset_cca(dev);
Rafał Miłecki4cb99772010-01-15 13:40:58 +01002525}
2526
2527/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RSSICalRev3 */
2528static void b43_nphy_rev3_rssi_cal(struct b43_wldev *dev)
2529{
2530 /* TODO */
2531}
2532
2533/*
2534 * RSSI Calibration
2535 * http://bcm-v4.sipsolutions.net/802.11/PHY/N/RSSICal
2536 */
2537static void b43_nphy_rssi_cal(struct b43_wldev *dev)
2538{
2539 if (dev->phy.rev >= 3) {
2540 b43_nphy_rev3_rssi_cal(dev);
2541 } else {
Rafał Miłecki76b002b2010-11-30 22:33:15 +01002542 b43_nphy_rev2_rssi_cal(dev, B43_NPHY_RSSI_Z);
2543 b43_nphy_rev2_rssi_cal(dev, B43_NPHY_RSSI_X);
2544 b43_nphy_rev2_rssi_cal(dev, B43_NPHY_RSSI_Y);
Rafał Miłecki4cb99772010-01-15 13:40:58 +01002545 }
Michael Buesch95b66ba2008-01-18 01:09:25 +01002546}
2547
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01002548/*
Rafał Miłecki42e15472010-01-15 15:06:47 +01002549 * Restore RSSI Calibration
2550 * http://bcm-v4.sipsolutions.net/802.11/PHY/N/RestoreRssiCal
2551 */
2552static void b43_nphy_restore_rssi_cal(struct b43_wldev *dev)
2553{
2554 struct b43_phy_n *nphy = dev->phy.n;
2555
2556 u16 *rssical_radio_regs = NULL;
2557 u16 *rssical_phy_regs = NULL;
2558
2559 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
Rafał Miłecki204a6652010-10-14 19:33:34 +02002560 if (!nphy->rssical_chanspec_2G.center_freq)
Rafał Miłecki42e15472010-01-15 15:06:47 +01002561 return;
2562 rssical_radio_regs = nphy->rssical_cache.rssical_radio_regs_2G;
2563 rssical_phy_regs = nphy->rssical_cache.rssical_phy_regs_2G;
2564 } else {
Rafał Miłecki204a6652010-10-14 19:33:34 +02002565 if (!nphy->rssical_chanspec_5G.center_freq)
Rafał Miłecki42e15472010-01-15 15:06:47 +01002566 return;
2567 rssical_radio_regs = nphy->rssical_cache.rssical_radio_regs_5G;
2568 rssical_phy_regs = nphy->rssical_cache.rssical_phy_regs_5G;
2569 }
2570
2571 /* TODO use some definitions */
2572 b43_radio_maskset(dev, 0x602B, 0xE3, rssical_radio_regs[0]);
2573 b43_radio_maskset(dev, 0x702B, 0xE3, rssical_radio_regs[1]);
2574
2575 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_Z, rssical_phy_regs[0]);
2576 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_Z, rssical_phy_regs[1]);
2577 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_Z, rssical_phy_regs[2]);
2578 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_Z, rssical_phy_regs[3]);
2579
2580 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_X, rssical_phy_regs[4]);
2581 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_X, rssical_phy_regs[5]);
2582 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_X, rssical_phy_regs[6]);
2583 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_X, rssical_phy_regs[7]);
2584
2585 b43_phy_write(dev, B43_NPHY_RSSIMC_0I_RSSI_Y, rssical_phy_regs[8]);
2586 b43_phy_write(dev, B43_NPHY_RSSIMC_0Q_RSSI_Y, rssical_phy_regs[9]);
2587 b43_phy_write(dev, B43_NPHY_RSSIMC_1I_RSSI_Y, rssical_phy_regs[10]);
2588 b43_phy_write(dev, B43_NPHY_RSSIMC_1Q_RSSI_Y, rssical_phy_regs[11]);
2589}
2590
Rafał Miłecki2f258b72010-01-15 15:18:35 +01002591/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/GetIpaGainTbl */
2592static const u32 *b43_nphy_get_ipa_gain_table(struct b43_wldev *dev)
2593{
2594 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
2595 if (dev->phy.rev >= 6) {
2596 /* TODO If the chip is 47162
2597 return txpwrctrl_tx_gain_ipa_rev5 */
2598 return txpwrctrl_tx_gain_ipa_rev6;
2599 } else if (dev->phy.rev >= 5) {
2600 return txpwrctrl_tx_gain_ipa_rev5;
2601 } else {
2602 return txpwrctrl_tx_gain_ipa;
2603 }
2604 } else {
2605 return txpwrctrl_tx_gain_ipa_5g;
2606 }
2607}
2608
Rafał Miłeckic4a92002010-01-15 15:55:18 +01002609/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxCalRadioSetup */
2610static void b43_nphy_tx_cal_radio_setup(struct b43_wldev *dev)
2611{
2612 struct b43_phy_n *nphy = dev->phy.n;
2613 u16 *save = nphy->tx_rx_cal_radio_saveregs;
Rafał Miłecki52cb5e92010-01-30 20:18:06 +01002614 u16 tmp;
2615 u8 offset, i;
Rafał Miłeckic4a92002010-01-15 15:55:18 +01002616
2617 if (dev->phy.rev >= 3) {
Rafał Miłecki52cb5e92010-01-30 20:18:06 +01002618 for (i = 0; i < 2; i++) {
2619 tmp = (i == 0) ? 0x2000 : 0x3000;
2620 offset = i * 11;
2621
2622 save[offset + 0] = b43_radio_read16(dev, B2055_CAL_RVARCTL);
2623 save[offset + 1] = b43_radio_read16(dev, B2055_CAL_LPOCTL);
2624 save[offset + 2] = b43_radio_read16(dev, B2055_CAL_TS);
2625 save[offset + 3] = b43_radio_read16(dev, B2055_CAL_RCCALRTS);
2626 save[offset + 4] = b43_radio_read16(dev, B2055_CAL_RCALRTS);
2627 save[offset + 5] = b43_radio_read16(dev, B2055_PADDRV);
2628 save[offset + 6] = b43_radio_read16(dev, B2055_XOCTL1);
2629 save[offset + 7] = b43_radio_read16(dev, B2055_XOCTL2);
2630 save[offset + 8] = b43_radio_read16(dev, B2055_XOREGUL);
2631 save[offset + 9] = b43_radio_read16(dev, B2055_XOMISC);
2632 save[offset + 10] = b43_radio_read16(dev, B2055_PLL_LFC1);
2633
2634 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
2635 b43_radio_write16(dev, tmp | B2055_CAL_RVARCTL, 0x0A);
2636 b43_radio_write16(dev, tmp | B2055_CAL_LPOCTL, 0x40);
2637 b43_radio_write16(dev, tmp | B2055_CAL_TS, 0x55);
2638 b43_radio_write16(dev, tmp | B2055_CAL_RCCALRTS, 0);
2639 b43_radio_write16(dev, tmp | B2055_CAL_RCALRTS, 0);
2640 if (nphy->ipa5g_on) {
2641 b43_radio_write16(dev, tmp | B2055_PADDRV, 4);
2642 b43_radio_write16(dev, tmp | B2055_XOCTL1, 1);
2643 } else {
2644 b43_radio_write16(dev, tmp | B2055_PADDRV, 0);
2645 b43_radio_write16(dev, tmp | B2055_XOCTL1, 0x2F);
2646 }
2647 b43_radio_write16(dev, tmp | B2055_XOCTL2, 0);
2648 } else {
2649 b43_radio_write16(dev, tmp | B2055_CAL_RVARCTL, 0x06);
2650 b43_radio_write16(dev, tmp | B2055_CAL_LPOCTL, 0x40);
2651 b43_radio_write16(dev, tmp | B2055_CAL_TS, 0x55);
2652 b43_radio_write16(dev, tmp | B2055_CAL_RCCALRTS, 0);
2653 b43_radio_write16(dev, tmp | B2055_CAL_RCALRTS, 0);
2654 b43_radio_write16(dev, tmp | B2055_XOCTL1, 0);
2655 if (nphy->ipa2g_on) {
2656 b43_radio_write16(dev, tmp | B2055_PADDRV, 6);
2657 b43_radio_write16(dev, tmp | B2055_XOCTL2,
2658 (dev->phy.rev < 5) ? 0x11 : 0x01);
2659 } else {
2660 b43_radio_write16(dev, tmp | B2055_PADDRV, 0);
2661 b43_radio_write16(dev, tmp | B2055_XOCTL2, 0);
2662 }
2663 }
2664 b43_radio_write16(dev, tmp | B2055_XOREGUL, 0);
2665 b43_radio_write16(dev, tmp | B2055_XOMISC, 0);
2666 b43_radio_write16(dev, tmp | B2055_PLL_LFC1, 0);
2667 }
Rafał Miłeckic4a92002010-01-15 15:55:18 +01002668 } else {
2669 save[0] = b43_radio_read16(dev, B2055_C1_TX_RF_IQCAL1);
2670 b43_radio_write16(dev, B2055_C1_TX_RF_IQCAL1, 0x29);
2671
2672 save[1] = b43_radio_read16(dev, B2055_C1_TX_RF_IQCAL2);
2673 b43_radio_write16(dev, B2055_C1_TX_RF_IQCAL2, 0x54);
2674
2675 save[2] = b43_radio_read16(dev, B2055_C2_TX_RF_IQCAL1);
2676 b43_radio_write16(dev, B2055_C2_TX_RF_IQCAL1, 0x29);
2677
2678 save[3] = b43_radio_read16(dev, B2055_C2_TX_RF_IQCAL2);
2679 b43_radio_write16(dev, B2055_C2_TX_RF_IQCAL2, 0x54);
2680
2681 save[3] = b43_radio_read16(dev, B2055_C1_PWRDET_RXTX);
2682 save[4] = b43_radio_read16(dev, B2055_C2_PWRDET_RXTX);
2683
2684 if (!(b43_phy_read(dev, B43_NPHY_BANDCTL) &
2685 B43_NPHY_BANDCTL_5GHZ)) {
2686 b43_radio_write16(dev, B2055_C1_PWRDET_RXTX, 0x04);
2687 b43_radio_write16(dev, B2055_C2_PWRDET_RXTX, 0x04);
2688 } else {
2689 b43_radio_write16(dev, B2055_C1_PWRDET_RXTX, 0x20);
2690 b43_radio_write16(dev, B2055_C2_PWRDET_RXTX, 0x20);
2691 }
2692
2693 if (dev->phy.rev < 2) {
2694 b43_radio_set(dev, B2055_C1_TX_BB_MXGM, 0x20);
2695 b43_radio_set(dev, B2055_C2_TX_BB_MXGM, 0x20);
2696 } else {
2697 b43_radio_mask(dev, B2055_C1_TX_BB_MXGM, ~0x20);
2698 b43_radio_mask(dev, B2055_C2_TX_BB_MXGM, ~0x20);
2699 }
2700 }
2701}
2702
Rafał Miłeckie9762492010-01-15 16:08:25 +01002703/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/IqCalGainParams */
2704static void b43_nphy_iq_cal_gain_params(struct b43_wldev *dev, u16 core,
2705 struct nphy_txgains target,
2706 struct nphy_iqcal_params *params)
2707{
2708 int i, j, indx;
2709 u16 gain;
2710
2711 if (dev->phy.rev >= 3) {
2712 params->txgm = target.txgm[core];
2713 params->pga = target.pga[core];
2714 params->pad = target.pad[core];
2715 params->ipa = target.ipa[core];
2716 params->cal_gain = (params->txgm << 12) | (params->pga << 8) |
2717 (params->pad << 4) | (params->ipa);
2718 for (j = 0; j < 5; j++)
2719 params->ncorr[j] = 0x79;
2720 } else {
2721 gain = (target.pad[core]) | (target.pga[core] << 4) |
2722 (target.txgm[core] << 8);
2723
2724 indx = (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) ?
2725 1 : 0;
2726 for (i = 0; i < 9; i++)
2727 if (tbl_iqcal_gainparams[indx][i][0] == gain)
2728 break;
2729 i = min(i, 8);
2730
2731 params->txgm = tbl_iqcal_gainparams[indx][i][1];
2732 params->pga = tbl_iqcal_gainparams[indx][i][2];
2733 params->pad = tbl_iqcal_gainparams[indx][i][3];
2734 params->cal_gain = (params->txgm << 7) | (params->pga << 4) |
2735 (params->pad << 2);
2736 for (j = 0; j < 4; j++)
2737 params->ncorr[j] = tbl_iqcal_gainparams[indx][i][4 + j];
2738 }
2739}
2740
Rafał Miłeckide7ed0c2010-01-15 16:06:35 +01002741/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/UpdateTxCalLadder */
2742static void b43_nphy_update_tx_cal_ladder(struct b43_wldev *dev, u16 core)
2743{
2744 struct b43_phy_n *nphy = dev->phy.n;
2745 int i;
2746 u16 scale, entry;
2747
2748 u16 tmp = nphy->txcal_bbmult;
2749 if (core == 0)
2750 tmp >>= 8;
2751 tmp &= 0xff;
2752
2753 for (i = 0; i < 18; i++) {
2754 scale = (ladder_lo[i].percent * tmp) / 100;
2755 entry = ((scale & 0xFF) << 8) | ladder_lo[i].g_env;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002756 b43_ntab_write(dev, B43_NTAB16(15, i), entry);
Rafał Miłeckide7ed0c2010-01-15 16:06:35 +01002757
2758 scale = (ladder_iq[i].percent * tmp) / 100;
2759 entry = ((scale & 0xFF) << 8) | ladder_iq[i].g_env;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002760 b43_ntab_write(dev, B43_NTAB16(15, i + 32), entry);
Rafał Miłeckide7ed0c2010-01-15 16:06:35 +01002761 }
2762}
2763
Rafał Miłecki45ca6972010-01-22 01:53:15 +01002764/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/ExtPaSetTxDigiFilts */
2765static void b43_nphy_ext_pa_set_tx_dig_filters(struct b43_wldev *dev)
2766{
2767 int i;
2768 for (i = 0; i < 15; i++)
2769 b43_phy_write(dev, B43_PHY_N(0x2C5 + i),
2770 tbl_tx_filter_coef_rev4[2][i]);
2771}
2772
2773/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/IpaSetTxDigiFilts */
2774static void b43_nphy_int_pa_set_tx_dig_filters(struct b43_wldev *dev)
2775{
2776 int i, j;
2777 /* B43_NPHY_TXF_20CO_S0A1, B43_NPHY_TXF_40CO_S0A1, unknown */
Joe Perches20407ed2010-11-20 18:38:57 -08002778 static const u16 offset[] = { 0x186, 0x195, 0x2C5 };
Rafał Miłecki45ca6972010-01-22 01:53:15 +01002779
2780 for (i = 0; i < 3; i++)
2781 for (j = 0; j < 15; j++)
2782 b43_phy_write(dev, B43_PHY_N(offset[i] + j),
2783 tbl_tx_filter_coef_rev4[i][j]);
2784
2785 if (dev->phy.is_40mhz) {
2786 for (j = 0; j < 15; j++)
2787 b43_phy_write(dev, B43_PHY_N(offset[0] + j),
2788 tbl_tx_filter_coef_rev4[3][j]);
2789 } else if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ) {
2790 for (j = 0; j < 15; j++)
2791 b43_phy_write(dev, B43_PHY_N(offset[0] + j),
2792 tbl_tx_filter_coef_rev4[5][j]);
2793 }
2794
2795 if (dev->phy.channel == 14)
2796 for (j = 0; j < 15; j++)
2797 b43_phy_write(dev, B43_PHY_N(offset[0] + j),
2798 tbl_tx_filter_coef_rev4[6][j]);
2799}
2800
Rafał Miłeckib0022e12010-01-15 15:40:50 +01002801/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/GetTxGain */
2802static struct nphy_txgains b43_nphy_get_tx_gains(struct b43_wldev *dev)
2803{
2804 struct b43_phy_n *nphy = dev->phy.n;
2805
2806 u16 curr_gain[2];
2807 struct nphy_txgains target;
2808 const u32 *table = NULL;
2809
Rafał Miłecki161d5402010-11-28 12:59:43 +01002810 if (!nphy->txpwrctrl) {
Rafał Miłeckib0022e12010-01-15 15:40:50 +01002811 int i;
2812
2813 if (nphy->hang_avoid)
2814 b43_nphy_stay_in_carrier_search(dev, true);
Rafał Miłecki91458342010-01-18 00:21:35 +01002815 b43_ntab_read_bulk(dev, B43_NTAB16(7, 0x110), 2, curr_gain);
Rafał Miłeckib0022e12010-01-15 15:40:50 +01002816 if (nphy->hang_avoid)
2817 b43_nphy_stay_in_carrier_search(dev, false);
2818
2819 for (i = 0; i < 2; ++i) {
2820 if (dev->phy.rev >= 3) {
2821 target.ipa[i] = curr_gain[i] & 0x000F;
2822 target.pad[i] = (curr_gain[i] & 0x00F0) >> 4;
2823 target.pga[i] = (curr_gain[i] & 0x0F00) >> 8;
2824 target.txgm[i] = (curr_gain[i] & 0x7000) >> 12;
2825 } else {
2826 target.ipa[i] = curr_gain[i] & 0x0003;
2827 target.pad[i] = (curr_gain[i] & 0x000C) >> 2;
2828 target.pga[i] = (curr_gain[i] & 0x0070) >> 4;
2829 target.txgm[i] = (curr_gain[i] & 0x0380) >> 7;
2830 }
2831 }
2832 } else {
2833 int i;
2834 u16 index[2];
2835 index[0] = (b43_phy_read(dev, B43_NPHY_C1_TXPCTL_STAT) &
2836 B43_NPHY_TXPCTL_STAT_BIDX) >>
2837 B43_NPHY_TXPCTL_STAT_BIDX_SHIFT;
2838 index[1] = (b43_phy_read(dev, B43_NPHY_C2_TXPCTL_STAT) &
2839 B43_NPHY_TXPCTL_STAT_BIDX) >>
2840 B43_NPHY_TXPCTL_STAT_BIDX_SHIFT;
2841
2842 for (i = 0; i < 2; ++i) {
2843 if (dev->phy.rev >= 3) {
2844 enum ieee80211_band band =
2845 b43_current_band(dev->wl);
2846
2847 if ((nphy->ipa2g_on &&
2848 band == IEEE80211_BAND_2GHZ) ||
2849 (nphy->ipa5g_on &&
2850 band == IEEE80211_BAND_5GHZ)) {
2851 table = b43_nphy_get_ipa_gain_table(dev);
2852 } else {
2853 if (band == IEEE80211_BAND_5GHZ) {
2854 if (dev->phy.rev == 3)
2855 table = b43_ntab_tx_gain_rev3_5ghz;
2856 else if (dev->phy.rev == 4)
2857 table = b43_ntab_tx_gain_rev4_5ghz;
2858 else
2859 table = b43_ntab_tx_gain_rev5plus_5ghz;
2860 } else {
2861 table = b43_ntab_tx_gain_rev3plus_2ghz;
2862 }
2863 }
2864
2865 target.ipa[i] = (table[index[i]] >> 16) & 0xF;
2866 target.pad[i] = (table[index[i]] >> 20) & 0xF;
2867 target.pga[i] = (table[index[i]] >> 24) & 0xF;
2868 target.txgm[i] = (table[index[i]] >> 28) & 0xF;
2869 } else {
2870 table = b43_ntab_tx_gain_rev0_1_2;
2871
2872 target.ipa[i] = (table[index[i]] >> 16) & 0x3;
2873 target.pad[i] = (table[index[i]] >> 18) & 0x3;
2874 target.pga[i] = (table[index[i]] >> 20) & 0x7;
2875 target.txgm[i] = (table[index[i]] >> 23) & 0x7;
2876 }
2877 }
2878 }
2879
2880 return target;
2881}
2882
Rafał Miłeckie53de672010-01-17 13:03:32 +01002883/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxCalPhyCleanup */
2884static void b43_nphy_tx_cal_phy_cleanup(struct b43_wldev *dev)
2885{
2886 u16 *regs = dev->phy.n->tx_rx_cal_phy_saveregs;
2887
2888 if (dev->phy.rev >= 3) {
2889 b43_phy_write(dev, B43_NPHY_AFECTL_C1, regs[0]);
2890 b43_phy_write(dev, B43_NPHY_AFECTL_C2, regs[1]);
2891 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, regs[2]);
2892 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, regs[3]);
2893 b43_phy_write(dev, B43_NPHY_BBCFG, regs[4]);
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002894 b43_ntab_write(dev, B43_NTAB16(8, 3), regs[5]);
2895 b43_ntab_write(dev, B43_NTAB16(8, 19), regs[6]);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002896 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, regs[7]);
2897 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, regs[8]);
2898 b43_phy_write(dev, B43_NPHY_PAPD_EN0, regs[9]);
2899 b43_phy_write(dev, B43_NPHY_PAPD_EN1, regs[10]);
2900 b43_nphy_reset_cca(dev);
2901 } else {
2902 b43_phy_maskset(dev, B43_NPHY_AFECTL_C1, 0x0FFF, regs[0]);
2903 b43_phy_maskset(dev, B43_NPHY_AFECTL_C2, 0x0FFF, regs[1]);
2904 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, regs[2]);
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002905 b43_ntab_write(dev, B43_NTAB16(8, 2), regs[3]);
2906 b43_ntab_write(dev, B43_NTAB16(8, 18), regs[4]);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002907 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, regs[5]);
2908 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, regs[6]);
2909 }
2910}
2911
2912/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/TxCalPhySetup */
2913static void b43_nphy_tx_cal_phy_setup(struct b43_wldev *dev)
2914{
2915 u16 *regs = dev->phy.n->tx_rx_cal_phy_saveregs;
2916 u16 tmp;
2917
2918 regs[0] = b43_phy_read(dev, B43_NPHY_AFECTL_C1);
2919 regs[1] = b43_phy_read(dev, B43_NPHY_AFECTL_C2);
2920 if (dev->phy.rev >= 3) {
2921 b43_phy_maskset(dev, B43_NPHY_AFECTL_C1, 0xF0FF, 0x0A00);
2922 b43_phy_maskset(dev, B43_NPHY_AFECTL_C2, 0xF0FF, 0x0A00);
2923
2924 tmp = b43_phy_read(dev, B43_NPHY_AFECTL_OVER1);
2925 regs[2] = tmp;
2926 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, tmp | 0x0600);
2927
2928 tmp = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
2929 regs[3] = tmp;
2930 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, tmp | 0x0600);
2931
2932 regs[4] = b43_phy_read(dev, B43_NPHY_BBCFG);
Larry Fingeracd82aa2010-07-21 11:48:05 -05002933 b43_phy_mask(dev, B43_NPHY_BBCFG,
2934 ~B43_NPHY_BBCFG_RSTRX & 0xFFFF);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002935
Rafał Miłeckic643a662010-01-18 00:21:27 +01002936 tmp = b43_ntab_read(dev, B43_NTAB16(8, 3));
Rafał Miłeckie53de672010-01-17 13:03:32 +01002937 regs[5] = tmp;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002938 b43_ntab_write(dev, B43_NTAB16(8, 3), 0);
Rafał Miłeckic643a662010-01-18 00:21:27 +01002939
2940 tmp = b43_ntab_read(dev, B43_NTAB16(8, 19));
Rafał Miłeckie53de672010-01-17 13:03:32 +01002941 regs[6] = tmp;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002942 b43_ntab_write(dev, B43_NTAB16(8, 19), 0);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002943 regs[7] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC1);
2944 regs[8] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC2);
2945
Rafał Miłecki67cbc3e2010-02-04 12:23:08 +01002946 b43_nphy_rf_control_intc_override(dev, 2, 1, 3);
2947 b43_nphy_rf_control_intc_override(dev, 1, 2, 1);
2948 b43_nphy_rf_control_intc_override(dev, 1, 8, 2);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002949
2950 regs[9] = b43_phy_read(dev, B43_NPHY_PAPD_EN0);
2951 regs[10] = b43_phy_read(dev, B43_NPHY_PAPD_EN1);
2952 b43_phy_mask(dev, B43_NPHY_PAPD_EN0, ~0x0001);
2953 b43_phy_mask(dev, B43_NPHY_PAPD_EN1, ~0x0001);
2954 } else {
2955 b43_phy_maskset(dev, B43_NPHY_AFECTL_C1, 0x0FFF, 0xA000);
2956 b43_phy_maskset(dev, B43_NPHY_AFECTL_C2, 0x0FFF, 0xA000);
2957 tmp = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
2958 regs[2] = tmp;
2959 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, tmp | 0x3000);
Rafał Miłeckic643a662010-01-18 00:21:27 +01002960 tmp = b43_ntab_read(dev, B43_NTAB16(8, 2));
Rafał Miłeckie53de672010-01-17 13:03:32 +01002961 regs[3] = tmp;
2962 tmp |= 0x2000;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002963 b43_ntab_write(dev, B43_NTAB16(8, 2), tmp);
Rafał Miłeckic643a662010-01-18 00:21:27 +01002964 tmp = b43_ntab_read(dev, B43_NTAB16(8, 18));
Rafał Miłeckie53de672010-01-17 13:03:32 +01002965 regs[4] = tmp;
2966 tmp |= 0x2000;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01002967 b43_ntab_write(dev, B43_NTAB16(8, 18), tmp);
Rafał Miłeckie53de672010-01-17 13:03:32 +01002968 regs[5] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC1);
2969 regs[6] = b43_phy_read(dev, B43_NPHY_RFCTL_INTC2);
2970 if (b43_current_band(dev->wl) == IEEE80211_BAND_5GHZ)
2971 tmp = 0x0180;
2972 else
2973 tmp = 0x0120;
2974 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, tmp);
2975 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, tmp);
2976 }
2977}
2978
Rafał Miłeckibbc6dc12010-02-04 12:23:11 +01002979/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SaveCal */
2980static void b43_nphy_save_cal(struct b43_wldev *dev)
2981{
2982 struct b43_phy_n *nphy = dev->phy.n;
2983
2984 struct b43_phy_n_iq_comp *rxcal_coeffs = NULL;
2985 u16 *txcal_radio_regs = NULL;
Rafał Miłecki902db912010-02-27 13:03:37 +01002986 struct b43_chanspec *iqcal_chanspec;
Rafał Miłeckibbc6dc12010-02-04 12:23:11 +01002987 u16 *table = NULL;
2988
2989 if (nphy->hang_avoid)
2990 b43_nphy_stay_in_carrier_search(dev, 1);
2991
2992 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
2993 rxcal_coeffs = &nphy->cal_cache.rxcal_coeffs_2G;
2994 txcal_radio_regs = nphy->cal_cache.txcal_radio_regs_2G;
2995 iqcal_chanspec = &nphy->iqcal_chanspec_2G;
2996 table = nphy->cal_cache.txcal_coeffs_2G;
2997 } else {
2998 rxcal_coeffs = &nphy->cal_cache.rxcal_coeffs_5G;
2999 txcal_radio_regs = nphy->cal_cache.txcal_radio_regs_5G;
3000 iqcal_chanspec = &nphy->iqcal_chanspec_5G;
3001 table = nphy->cal_cache.txcal_coeffs_5G;
3002 }
3003
3004 b43_nphy_rx_iq_coeffs(dev, false, rxcal_coeffs);
3005 /* TODO use some definitions */
3006 if (dev->phy.rev >= 3) {
3007 txcal_radio_regs[0] = b43_radio_read(dev, 0x2021);
3008 txcal_radio_regs[1] = b43_radio_read(dev, 0x2022);
3009 txcal_radio_regs[2] = b43_radio_read(dev, 0x3021);
3010 txcal_radio_regs[3] = b43_radio_read(dev, 0x3022);
3011 txcal_radio_regs[4] = b43_radio_read(dev, 0x2023);
3012 txcal_radio_regs[5] = b43_radio_read(dev, 0x2024);
3013 txcal_radio_regs[6] = b43_radio_read(dev, 0x3023);
3014 txcal_radio_regs[7] = b43_radio_read(dev, 0x3024);
3015 } else {
3016 txcal_radio_regs[0] = b43_radio_read(dev, 0x8B);
3017 txcal_radio_regs[1] = b43_radio_read(dev, 0xBA);
3018 txcal_radio_regs[2] = b43_radio_read(dev, 0x8D);
3019 txcal_radio_regs[3] = b43_radio_read(dev, 0xBC);
3020 }
Rafał Miłecki204a6652010-10-14 19:33:34 +02003021 iqcal_chanspec->center_freq = dev->phy.channel_freq;
3022 iqcal_chanspec->channel_type = dev->phy.channel_type;
Rafał Miłecki5818e982010-10-14 19:33:35 +02003023 b43_ntab_read_bulk(dev, B43_NTAB16(15, 80), 8, table);
Rafał Miłeckibbc6dc12010-02-04 12:23:11 +01003024
3025 if (nphy->hang_avoid)
3026 b43_nphy_stay_in_carrier_search(dev, 0);
3027}
3028
Rafał Miłecki2f258b72010-01-15 15:18:35 +01003029/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RestoreCal */
3030static void b43_nphy_restore_cal(struct b43_wldev *dev)
3031{
3032 struct b43_phy_n *nphy = dev->phy.n;
3033
3034 u16 coef[4];
3035 u16 *loft = NULL;
3036 u16 *table = NULL;
3037
3038 int i;
3039 u16 *txcal_radio_regs = NULL;
3040 struct b43_phy_n_iq_comp *rxcal_coeffs = NULL;
3041
3042 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
Rafał Miłecki204a6652010-10-14 19:33:34 +02003043 if (!nphy->iqcal_chanspec_2G.center_freq)
Rafał Miłecki2f258b72010-01-15 15:18:35 +01003044 return;
3045 table = nphy->cal_cache.txcal_coeffs_2G;
3046 loft = &nphy->cal_cache.txcal_coeffs_2G[5];
3047 } else {
Rafał Miłecki204a6652010-10-14 19:33:34 +02003048 if (!nphy->iqcal_chanspec_5G.center_freq)
Rafał Miłecki2f258b72010-01-15 15:18:35 +01003049 return;
3050 table = nphy->cal_cache.txcal_coeffs_5G;
3051 loft = &nphy->cal_cache.txcal_coeffs_5G[5];
3052 }
3053
Rafał Miłecki2581b142010-01-18 00:21:21 +01003054 b43_ntab_write_bulk(dev, B43_NTAB16(15, 80), 4, table);
Rafał Miłecki2f258b72010-01-15 15:18:35 +01003055
3056 for (i = 0; i < 4; i++) {
3057 if (dev->phy.rev >= 3)
3058 table[i] = coef[i];
3059 else
3060 coef[i] = 0;
3061 }
3062
Rafał Miłecki2581b142010-01-18 00:21:21 +01003063 b43_ntab_write_bulk(dev, B43_NTAB16(15, 88), 4, coef);
3064 b43_ntab_write_bulk(dev, B43_NTAB16(15, 85), 2, loft);
3065 b43_ntab_write_bulk(dev, B43_NTAB16(15, 93), 2, loft);
Rafał Miłecki2f258b72010-01-15 15:18:35 +01003066
3067 if (dev->phy.rev < 2)
3068 b43_nphy_tx_iq_workaround(dev);
3069
3070 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
3071 txcal_radio_regs = nphy->cal_cache.txcal_radio_regs_2G;
3072 rxcal_coeffs = &nphy->cal_cache.rxcal_coeffs_2G;
3073 } else {
3074 txcal_radio_regs = nphy->cal_cache.txcal_radio_regs_5G;
3075 rxcal_coeffs = &nphy->cal_cache.rxcal_coeffs_5G;
3076 }
3077
3078 /* TODO use some definitions */
3079 if (dev->phy.rev >= 3) {
3080 b43_radio_write(dev, 0x2021, txcal_radio_regs[0]);
3081 b43_radio_write(dev, 0x2022, txcal_radio_regs[1]);
3082 b43_radio_write(dev, 0x3021, txcal_radio_regs[2]);
3083 b43_radio_write(dev, 0x3022, txcal_radio_regs[3]);
3084 b43_radio_write(dev, 0x2023, txcal_radio_regs[4]);
3085 b43_radio_write(dev, 0x2024, txcal_radio_regs[5]);
3086 b43_radio_write(dev, 0x3023, txcal_radio_regs[6]);
3087 b43_radio_write(dev, 0x3024, txcal_radio_regs[7]);
3088 } else {
3089 b43_radio_write(dev, 0x8B, txcal_radio_regs[0]);
3090 b43_radio_write(dev, 0xBA, txcal_radio_regs[1]);
3091 b43_radio_write(dev, 0x8D, txcal_radio_regs[2]);
3092 b43_radio_write(dev, 0xBC, txcal_radio_regs[3]);
3093 }
3094 b43_nphy_rx_iq_coeffs(dev, true, rxcal_coeffs);
3095}
3096
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003097/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/CalTxIqlo */
3098static int b43_nphy_cal_tx_iq_lo(struct b43_wldev *dev,
3099 struct nphy_txgains target,
3100 bool full, bool mphase)
3101{
3102 struct b43_phy_n *nphy = dev->phy.n;
3103 int i;
3104 int error = 0;
3105 int freq;
3106 bool avoid = false;
3107 u8 length;
Rafał Miłeckifb23d862011-05-20 01:04:46 +02003108 u16 tmp, core, type, count, max, numb, last = 0, cmd;
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003109 const u16 *table;
3110 bool phy6or5x;
3111
3112 u16 buffer[11];
3113 u16 diq_start = 0;
3114 u16 save[2];
3115 u16 gain[2];
3116 struct nphy_iqcal_params params[2];
3117 bool updated[2] = { };
3118
3119 b43_nphy_stay_in_carrier_search(dev, true);
3120
3121 if (dev->phy.rev >= 4) {
3122 avoid = nphy->hang_avoid;
3123 nphy->hang_avoid = 0;
3124 }
3125
Rafał Miłecki91458342010-01-18 00:21:35 +01003126 b43_ntab_read_bulk(dev, B43_NTAB16(7, 0x110), 2, save);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003127
3128 for (i = 0; i < 2; i++) {
3129 b43_nphy_iq_cal_gain_params(dev, i, target, &params[i]);
3130 gain[i] = params[i].cal_gain;
3131 }
Rafał Miłecki2581b142010-01-18 00:21:21 +01003132
3133 b43_ntab_write_bulk(dev, B43_NTAB16(7, 0x110), 2, gain);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003134
3135 b43_nphy_tx_cal_radio_setup(dev);
Rafał Miłeckie53de672010-01-17 13:03:32 +01003136 b43_nphy_tx_cal_phy_setup(dev);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003137
3138 phy6or5x = dev->phy.rev >= 6 ||
3139 (dev->phy.rev == 5 && nphy->ipa2g_on &&
3140 b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ);
3141 if (phy6or5x) {
Rafał Miłecki38bb9022010-01-30 20:18:05 +01003142 if (dev->phy.is_40mhz) {
3143 b43_ntab_write_bulk(dev, B43_NTAB16(15, 0), 18,
3144 tbl_tx_iqlo_cal_loft_ladder_40);
3145 b43_ntab_write_bulk(dev, B43_NTAB16(15, 32), 18,
3146 tbl_tx_iqlo_cal_iqimb_ladder_40);
3147 } else {
3148 b43_ntab_write_bulk(dev, B43_NTAB16(15, 0), 18,
3149 tbl_tx_iqlo_cal_loft_ladder_20);
3150 b43_ntab_write_bulk(dev, B43_NTAB16(15, 32), 18,
3151 tbl_tx_iqlo_cal_iqimb_ladder_20);
3152 }
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003153 }
3154
3155 b43_phy_write(dev, B43_NPHY_IQLOCAL_CMDGCTL, 0x8AA9);
3156
Rafał Miłeckiaa4c7b2a22010-01-22 01:53:12 +01003157 if (!dev->phy.is_40mhz)
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003158 freq = 2500;
3159 else
3160 freq = 5000;
3161
3162 if (nphy->mphase_cal_phase_id > 2)
Rafał Miłecki10a79872010-01-22 01:53:14 +01003163 b43_nphy_run_samples(dev, (dev->phy.is_40mhz ? 40 : 20) * 8,
3164 0xFFFF, 0, true, false);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003165 else
Rafał Miłecki59af0992010-01-22 01:53:16 +01003166 error = b43_nphy_tx_tone(dev, freq, 250, true, false);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003167
3168 if (error == 0) {
3169 if (nphy->mphase_cal_phase_id > 2) {
3170 table = nphy->mphase_txcal_bestcoeffs;
3171 length = 11;
3172 if (dev->phy.rev < 3)
3173 length -= 2;
3174 } else {
3175 if (!full && nphy->txiqlocal_coeffsvalid) {
3176 table = nphy->txiqlocal_bestc;
3177 length = 11;
3178 if (dev->phy.rev < 3)
3179 length -= 2;
3180 } else {
3181 full = true;
3182 if (dev->phy.rev >= 3) {
3183 table = tbl_tx_iqlo_cal_startcoefs_nphyrev3;
3184 length = B43_NTAB_TX_IQLO_CAL_STARTCOEFS_REV3;
3185 } else {
3186 table = tbl_tx_iqlo_cal_startcoefs;
3187 length = B43_NTAB_TX_IQLO_CAL_STARTCOEFS;
3188 }
3189 }
3190 }
3191
Rafał Miłecki2581b142010-01-18 00:21:21 +01003192 b43_ntab_write_bulk(dev, B43_NTAB16(15, 64), length, table);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003193
3194 if (full) {
3195 if (dev->phy.rev >= 3)
3196 max = B43_NTAB_TX_IQLO_CAL_CMDS_FULLCAL_REV3;
3197 else
3198 max = B43_NTAB_TX_IQLO_CAL_CMDS_FULLCAL;
3199 } else {
3200 if (dev->phy.rev >= 3)
3201 max = B43_NTAB_TX_IQLO_CAL_CMDS_RECAL_REV3;
3202 else
3203 max = B43_NTAB_TX_IQLO_CAL_CMDS_RECAL;
3204 }
3205
3206 if (mphase) {
3207 count = nphy->mphase_txcal_cmdidx;
3208 numb = min(max,
3209 (u16)(count + nphy->mphase_txcal_numcmds));
3210 } else {
3211 count = 0;
3212 numb = max;
3213 }
3214
3215 for (; count < numb; count++) {
3216 if (full) {
3217 if (dev->phy.rev >= 3)
3218 cmd = tbl_tx_iqlo_cal_cmds_fullcal_nphyrev3[count];
3219 else
3220 cmd = tbl_tx_iqlo_cal_cmds_fullcal[count];
3221 } else {
3222 if (dev->phy.rev >= 3)
3223 cmd = tbl_tx_iqlo_cal_cmds_recal_nphyrev3[count];
3224 else
3225 cmd = tbl_tx_iqlo_cal_cmds_recal[count];
3226 }
3227
3228 core = (cmd & 0x3000) >> 12;
3229 type = (cmd & 0x0F00) >> 8;
3230
3231 if (phy6or5x && updated[core] == 0) {
3232 b43_nphy_update_tx_cal_ladder(dev, core);
3233 updated[core] = 1;
3234 }
3235
3236 tmp = (params[core].ncorr[type] << 8) | 0x66;
3237 b43_phy_write(dev, B43_NPHY_IQLOCAL_CMDNNUM, tmp);
3238
3239 if (type == 1 || type == 3 || type == 4) {
Rafał Miłeckic643a662010-01-18 00:21:27 +01003240 buffer[0] = b43_ntab_read(dev,
3241 B43_NTAB16(15, 69 + core));
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003242 diq_start = buffer[0];
3243 buffer[0] = 0;
Rafał Miłeckid41a3552010-01-18 00:21:17 +01003244 b43_ntab_write(dev, B43_NTAB16(15, 69 + core),
3245 0);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003246 }
3247
3248 b43_phy_write(dev, B43_NPHY_IQLOCAL_CMD, cmd);
3249 for (i = 0; i < 2000; i++) {
3250 tmp = b43_phy_read(dev, B43_NPHY_IQLOCAL_CMD);
3251 if (tmp & 0xC000)
3252 break;
3253 udelay(10);
3254 }
3255
Rafał Miłecki91458342010-01-18 00:21:35 +01003256 b43_ntab_read_bulk(dev, B43_NTAB16(15, 96), length,
3257 buffer);
Rafał Miłecki2581b142010-01-18 00:21:21 +01003258 b43_ntab_write_bulk(dev, B43_NTAB16(15, 64), length,
3259 buffer);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003260
3261 if (type == 1 || type == 3 || type == 4)
3262 buffer[0] = diq_start;
3263 }
3264
3265 if (mphase)
3266 nphy->mphase_txcal_cmdidx = (numb >= max) ? 0 : numb;
3267
3268 last = (dev->phy.rev < 3) ? 6 : 7;
3269
3270 if (!mphase || nphy->mphase_cal_phase_id == last) {
Rafał Miłecki2581b142010-01-18 00:21:21 +01003271 b43_ntab_write_bulk(dev, B43_NTAB16(15, 96), 4, buffer);
Rafał Miłecki91458342010-01-18 00:21:35 +01003272 b43_ntab_read_bulk(dev, B43_NTAB16(15, 80), 4, buffer);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003273 if (dev->phy.rev < 3) {
3274 buffer[0] = 0;
3275 buffer[1] = 0;
3276 buffer[2] = 0;
3277 buffer[3] = 0;
3278 }
Rafał Miłecki2581b142010-01-18 00:21:21 +01003279 b43_ntab_write_bulk(dev, B43_NTAB16(15, 88), 4,
3280 buffer);
Rafał Miłeckibc53e512010-04-01 23:11:10 +02003281 b43_ntab_read_bulk(dev, B43_NTAB16(15, 101), 2,
Rafał Miłecki2581b142010-01-18 00:21:21 +01003282 buffer);
3283 b43_ntab_write_bulk(dev, B43_NTAB16(15, 85), 2,
3284 buffer);
3285 b43_ntab_write_bulk(dev, B43_NTAB16(15, 93), 2,
3286 buffer);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003287 length = 11;
3288 if (dev->phy.rev < 3)
3289 length -= 2;
Rafał Miłecki91458342010-01-18 00:21:35 +01003290 b43_ntab_read_bulk(dev, B43_NTAB16(15, 96), length,
3291 nphy->txiqlocal_bestc);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003292 nphy->txiqlocal_coeffsvalid = true;
Rafał Miłecki204a6652010-10-14 19:33:34 +02003293 nphy->txiqlocal_chanspec.center_freq =
3294 dev->phy.channel_freq;
3295 nphy->txiqlocal_chanspec.channel_type =
3296 dev->phy.channel_type;
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003297 } else {
3298 length = 11;
3299 if (dev->phy.rev < 3)
3300 length -= 2;
Rafał Miłecki91458342010-01-18 00:21:35 +01003301 b43_ntab_read_bulk(dev, B43_NTAB16(15, 96), length,
3302 nphy->mphase_txcal_bestcoeffs);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003303 }
3304
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01003305 b43_nphy_stop_playback(dev);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003306 b43_phy_write(dev, B43_NPHY_IQLOCAL_CMDGCTL, 0);
3307 }
3308
Rafał Miłeckie53de672010-01-17 13:03:32 +01003309 b43_nphy_tx_cal_phy_cleanup(dev);
Rafał Miłecki2581b142010-01-18 00:21:21 +01003310 b43_ntab_write_bulk(dev, B43_NTAB16(7, 0x110), 2, save);
Rafał Miłeckifb43b8e2010-01-15 16:10:48 +01003311
3312 if (dev->phy.rev < 2 && (!mphase || nphy->mphase_cal_phase_id == last))
3313 b43_nphy_tx_iq_workaround(dev);
3314
3315 if (dev->phy.rev >= 4)
3316 nphy->hang_avoid = avoid;
3317
3318 b43_nphy_stay_in_carrier_search(dev, false);
3319
3320 return error;
3321}
3322
Rafał Miłecki984ff4f2010-02-04 12:23:10 +01003323/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/ReapplyTxCalCoeffs */
3324static void b43_nphy_reapply_tx_cal_coeffs(struct b43_wldev *dev)
3325{
3326 struct b43_phy_n *nphy = dev->phy.n;
3327 u8 i;
3328 u16 buffer[7];
3329 bool equal = true;
3330
Rafał Miłecki902db912010-02-27 13:03:37 +01003331 if (!nphy->txiqlocal_coeffsvalid ||
Rafał Miłecki204a6652010-10-14 19:33:34 +02003332 nphy->txiqlocal_chanspec.center_freq != dev->phy.channel_freq ||
3333 nphy->txiqlocal_chanspec.channel_type != dev->phy.channel_type)
Rafał Miłecki984ff4f2010-02-04 12:23:10 +01003334 return;
3335
3336 b43_ntab_read_bulk(dev, B43_NTAB16(15, 80), 7, buffer);
3337 for (i = 0; i < 4; i++) {
3338 if (buffer[i] != nphy->txiqlocal_bestc[i]) {
3339 equal = false;
3340 break;
3341 }
3342 }
3343
3344 if (!equal) {
3345 b43_ntab_write_bulk(dev, B43_NTAB16(15, 80), 4,
3346 nphy->txiqlocal_bestc);
3347 for (i = 0; i < 4; i++)
3348 buffer[i] = 0;
3349 b43_ntab_write_bulk(dev, B43_NTAB16(15, 88), 4,
3350 buffer);
3351 b43_ntab_write_bulk(dev, B43_NTAB16(15, 85), 2,
3352 &nphy->txiqlocal_bestc[5]);
3353 b43_ntab_write_bulk(dev, B43_NTAB16(15, 93), 2,
3354 &nphy->txiqlocal_bestc[5]);
3355 }
3356}
3357
Rafał Miłecki15931e32010-01-15 16:20:56 +01003358/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/CalRxIqRev2 */
3359static int b43_nphy_rev2_cal_rx_iq(struct b43_wldev *dev,
3360 struct nphy_txgains target, u8 type, bool debug)
3361{
3362 struct b43_phy_n *nphy = dev->phy.n;
3363 int i, j, index;
3364 u8 rfctl[2];
3365 u8 afectl_core;
3366 u16 tmp[6];
Rafał Miłeckic7455cf2010-12-07 21:55:57 +01003367 u16 uninitialized_var(cur_hpf1), uninitialized_var(cur_hpf2), cur_lna;
Rafał Miłecki15931e32010-01-15 16:20:56 +01003368 u32 real, imag;
3369 enum ieee80211_band band;
3370
3371 u8 use;
3372 u16 cur_hpf;
3373 u16 lna[3] = { 3, 3, 1 };
3374 u16 hpf1[3] = { 7, 2, 0 };
3375 u16 hpf2[3] = { 2, 0, 0 };
Rafał Miłeckide9a47f2010-01-18 00:21:49 +01003376 u32 power[3] = { };
Rafał Miłecki15931e32010-01-15 16:20:56 +01003377 u16 gain_save[2];
3378 u16 cal_gain[2];
3379 struct nphy_iqcal_params cal_params[2];
3380 struct nphy_iq_est est;
3381 int ret = 0;
3382 bool playtone = true;
3383 int desired = 13;
3384
3385 b43_nphy_stay_in_carrier_search(dev, 1);
3386
3387 if (dev->phy.rev < 2)
Rafał Miłecki984ff4f2010-02-04 12:23:10 +01003388 b43_nphy_reapply_tx_cal_coeffs(dev);
Rafał Miłecki91458342010-01-18 00:21:35 +01003389 b43_ntab_read_bulk(dev, B43_NTAB16(7, 0x110), 2, gain_save);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003390 for (i = 0; i < 2; i++) {
3391 b43_nphy_iq_cal_gain_params(dev, i, target, &cal_params[i]);
3392 cal_gain[i] = cal_params[i].cal_gain;
3393 }
Rafał Miłecki2581b142010-01-18 00:21:21 +01003394 b43_ntab_write_bulk(dev, B43_NTAB16(7, 0x110), 2, cal_gain);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003395
3396 for (i = 0; i < 2; i++) {
3397 if (i == 0) {
3398 rfctl[0] = B43_NPHY_RFCTL_INTC1;
3399 rfctl[1] = B43_NPHY_RFCTL_INTC2;
3400 afectl_core = B43_NPHY_AFECTL_C1;
3401 } else {
3402 rfctl[0] = B43_NPHY_RFCTL_INTC2;
3403 rfctl[1] = B43_NPHY_RFCTL_INTC1;
3404 afectl_core = B43_NPHY_AFECTL_C2;
3405 }
3406
3407 tmp[1] = b43_phy_read(dev, B43_NPHY_RFSEQCA);
3408 tmp[2] = b43_phy_read(dev, afectl_core);
3409 tmp[3] = b43_phy_read(dev, B43_NPHY_AFECTL_OVER);
3410 tmp[4] = b43_phy_read(dev, rfctl[0]);
3411 tmp[5] = b43_phy_read(dev, rfctl[1]);
3412
3413 b43_phy_maskset(dev, B43_NPHY_RFSEQCA,
Larry Fingeracd82aa2010-07-21 11:48:05 -05003414 ~B43_NPHY_RFSEQCA_RXDIS & 0xFFFF,
Rafał Miłecki15931e32010-01-15 16:20:56 +01003415 ((1 - i) << B43_NPHY_RFSEQCA_RXDIS_SHIFT));
3416 b43_phy_maskset(dev, B43_NPHY_RFSEQCA, ~B43_NPHY_RFSEQCA_TXEN,
3417 (1 - i));
3418 b43_phy_set(dev, afectl_core, 0x0006);
3419 b43_phy_set(dev, B43_NPHY_AFECTL_OVER, 0x0006);
3420
3421 band = b43_current_band(dev->wl);
3422
3423 if (nphy->rxcalparams & 0xFF000000) {
3424 if (band == IEEE80211_BAND_5GHZ)
3425 b43_phy_write(dev, rfctl[0], 0x140);
3426 else
3427 b43_phy_write(dev, rfctl[0], 0x110);
3428 } else {
3429 if (band == IEEE80211_BAND_5GHZ)
3430 b43_phy_write(dev, rfctl[0], 0x180);
3431 else
3432 b43_phy_write(dev, rfctl[0], 0x120);
3433 }
3434
3435 if (band == IEEE80211_BAND_5GHZ)
3436 b43_phy_write(dev, rfctl[1], 0x148);
3437 else
3438 b43_phy_write(dev, rfctl[1], 0x114);
3439
3440 if (nphy->rxcalparams & 0x10000) {
3441 b43_radio_maskset(dev, B2055_C1_GENSPARE2, 0xFC,
3442 (i + 1));
3443 b43_radio_maskset(dev, B2055_C2_GENSPARE2, 0xFC,
3444 (2 - i));
3445 }
3446
Rafał Miłecki30115c22010-10-22 17:43:45 +02003447 for (j = 0; j < 4; j++) {
Rafał Miłecki15931e32010-01-15 16:20:56 +01003448 if (j < 3) {
3449 cur_lna = lna[j];
3450 cur_hpf1 = hpf1[j];
3451 cur_hpf2 = hpf2[j];
3452 } else {
3453 if (power[1] > 10000) {
3454 use = 1;
3455 cur_hpf = cur_hpf1;
3456 index = 2;
3457 } else {
3458 if (power[0] > 10000) {
3459 use = 1;
3460 cur_hpf = cur_hpf1;
3461 index = 1;
3462 } else {
3463 index = 0;
3464 use = 2;
3465 cur_hpf = cur_hpf2;
3466 }
3467 }
3468 cur_lna = lna[index];
3469 cur_hpf1 = hpf1[index];
3470 cur_hpf2 = hpf2[index];
3471 cur_hpf += desired - hweight32(power[index]);
3472 cur_hpf = clamp_val(cur_hpf, 0, 10);
3473 if (use == 1)
3474 cur_hpf1 = cur_hpf;
3475 else
3476 cur_hpf2 = cur_hpf;
3477 }
3478
3479 tmp[0] = ((cur_hpf2 << 8) | (cur_hpf1 << 4) |
3480 (cur_lna << 2));
Rafał Miłecki75377b22010-01-22 01:53:13 +01003481 b43_nphy_rf_control_override(dev, 0x400, tmp[0], 3,
3482 false);
Rafał Miłeckide9a47f2010-01-18 00:21:49 +01003483 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01003484 b43_nphy_stop_playback(dev);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003485
3486 if (playtone) {
Rafał Miłecki59af0992010-01-22 01:53:16 +01003487 ret = b43_nphy_tx_tone(dev, 4000,
3488 (nphy->rxcalparams & 0xFFFF),
3489 false, false);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003490 playtone = false;
3491 } else {
Rafał Miłecki10a79872010-01-22 01:53:14 +01003492 b43_nphy_run_samples(dev, 160, 0xFFFF, 0,
3493 false, false);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003494 }
3495
3496 if (ret == 0) {
3497 if (j < 3) {
3498 b43_nphy_rx_iq_est(dev, &est, 1024, 32,
3499 false);
3500 if (i == 0) {
3501 real = est.i0_pwr;
3502 imag = est.q0_pwr;
3503 } else {
3504 real = est.i1_pwr;
3505 imag = est.q1_pwr;
3506 }
3507 power[i] = ((real + imag) / 1024) + 1;
3508 } else {
3509 b43_nphy_calc_rx_iq_comp(dev, 1 << i);
3510 }
Rafał Miłecki53ae8e82010-01-17 13:03:48 +01003511 b43_nphy_stop_playback(dev);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003512 }
3513
3514 if (ret != 0)
3515 break;
3516 }
3517
3518 b43_radio_mask(dev, B2055_C1_GENSPARE2, 0xFC);
3519 b43_radio_mask(dev, B2055_C2_GENSPARE2, 0xFC);
3520 b43_phy_write(dev, rfctl[1], tmp[5]);
3521 b43_phy_write(dev, rfctl[0], tmp[4]);
3522 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, tmp[3]);
3523 b43_phy_write(dev, afectl_core, tmp[2]);
3524 b43_phy_write(dev, B43_NPHY_RFSEQCA, tmp[1]);
3525
3526 if (ret != 0)
3527 break;
3528 }
3529
Rafał Miłecki75377b22010-01-22 01:53:13 +01003530 b43_nphy_rf_control_override(dev, 0x400, 0, 3, true);
Rafał Miłecki67c0d6e2010-01-17 13:04:02 +01003531 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
Rafał Miłecki2581b142010-01-18 00:21:21 +01003532 b43_ntab_write_bulk(dev, B43_NTAB16(7, 0x110), 2, gain_save);
Rafał Miłecki15931e32010-01-15 16:20:56 +01003533
3534 b43_nphy_stay_in_carrier_search(dev, 0);
3535
3536 return ret;
3537}
3538
3539static int b43_nphy_rev3_cal_rx_iq(struct b43_wldev *dev,
3540 struct nphy_txgains target, u8 type, bool debug)
3541{
3542 return -1;
3543}
3544
3545/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/CalRxIq */
3546static int b43_nphy_cal_rx_iq(struct b43_wldev *dev,
3547 struct nphy_txgains target, u8 type, bool debug)
3548{
3549 if (dev->phy.rev >= 3)
3550 return b43_nphy_rev3_cal_rx_iq(dev, target, type, debug);
3551 else
3552 return b43_nphy_rev2_cal_rx_iq(dev, target, type, debug);
3553}
3554
Gábor Stefanik4e687b22010-08-16 22:39:17 +02003555/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/RxCoreSetState */
3556static void b43_nphy_set_rx_core_state(struct b43_wldev *dev, u8 mask)
3557{
3558 struct b43_phy *phy = &dev->phy;
3559 struct b43_phy_n *nphy = phy->n;
Rafał Miłecki0b81c232010-11-18 21:11:43 +01003560 /* u16 buf[16]; it's rev3+ */
Gábor Stefanik4e687b22010-08-16 22:39:17 +02003561
Rafał Miłecki049fbfe2010-08-22 21:47:32 +02003562 nphy->phyrxchain = mask;
3563
Gábor Stefanik4e687b22010-08-16 22:39:17 +02003564 if (0 /* FIXME clk */)
3565 return;
3566
3567 b43_mac_suspend(dev);
3568
3569 if (nphy->hang_avoid)
3570 b43_nphy_stay_in_carrier_search(dev, true);
3571
3572 b43_phy_maskset(dev, B43_NPHY_RFSEQCA, ~B43_NPHY_RFSEQCA_RXEN,
3573 (mask & 0x3) << B43_NPHY_RFSEQCA_RXEN_SHIFT);
3574
Rafał Miłecki049fbfe2010-08-22 21:47:32 +02003575 if ((mask & 0x3) != 0x3) {
Gábor Stefanik4e687b22010-08-16 22:39:17 +02003576 b43_phy_write(dev, B43_NPHY_HPANT_SWTHRES, 1);
3577 if (dev->phy.rev >= 3) {
3578 /* TODO */
3579 }
3580 } else {
3581 b43_phy_write(dev, B43_NPHY_HPANT_SWTHRES, 0x1E);
3582 if (dev->phy.rev >= 3) {
3583 /* TODO */
3584 }
3585 }
3586
3587 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
3588
3589 if (nphy->hang_avoid)
3590 b43_nphy_stay_in_carrier_search(dev, false);
3591
3592 b43_mac_enable(dev);
3593}
3594
Rafał Miłecki42e15472010-01-15 15:06:47 +01003595/*
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003596 * Init N-PHY
3597 * http://bcm-v4.sipsolutions.net/802.11/PHY/Init/N
3598 */
Michael Buesch424047e2008-01-09 16:13:56 +01003599int b43_phy_initn(struct b43_wldev *dev)
3600{
Rafał Miłecki05814832011-05-18 02:06:39 +02003601 struct ssb_sprom *sprom = dev->dev->bus_sprom;
Michael Buesch95b66ba2008-01-18 01:09:25 +01003602 struct b43_phy *phy = &dev->phy;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003603 struct b43_phy_n *nphy = phy->n;
3604 u8 tx_pwr_state;
3605 struct nphy_txgains target;
Michael Buesch95b66ba2008-01-18 01:09:25 +01003606 u16 tmp;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003607 enum ieee80211_band tmp2;
3608 bool do_rssi_cal;
Michael Buesch424047e2008-01-09 16:13:56 +01003609
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003610 u16 clip[2];
3611 bool do_cal = false;
3612
3613 if ((dev->phy.rev >= 3) &&
Rafał Miłecki05814832011-05-18 02:06:39 +02003614 (sprom->boardflags_lo & B43_BFL_EXTLNA) &&
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003615 (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)) {
Rafał Miłecki6cbab0d2011-07-06 15:45:26 +02003616 switch (dev->dev->bus_type) {
3617#ifdef CONFIG_B43_SSB
3618 case B43_BUS_SSB:
3619 chipco_set32(&dev->dev->sdev->bus->chipco,
3620 SSB_CHIPCO_CHIPCTL, 0x40);
3621 break;
3622#endif
3623 }
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003624 }
3625 nphy->deaf_count = 0;
Michael Buesch95b66ba2008-01-18 01:09:25 +01003626 b43_nphy_tables_init(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003627 nphy->crsminpwr_adjusted = false;
3628 nphy->noisevars_adjusted = false;
Michael Buesch95b66ba2008-01-18 01:09:25 +01003629
3630 /* Clear all overrides */
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003631 if (dev->phy.rev >= 3) {
3632 b43_phy_write(dev, B43_NPHY_TXF_40CO_B1S1, 0);
3633 b43_phy_write(dev, B43_NPHY_RFCTL_OVER, 0);
3634 b43_phy_write(dev, B43_NPHY_TXF_40CO_B1S0, 0);
3635 b43_phy_write(dev, B43_NPHY_TXF_40CO_B32S1, 0);
3636 } else {
3637 b43_phy_write(dev, B43_NPHY_RFCTL_OVER, 0);
3638 }
Michael Buesch95b66ba2008-01-18 01:09:25 +01003639 b43_phy_write(dev, B43_NPHY_RFCTL_INTC1, 0);
3640 b43_phy_write(dev, B43_NPHY_RFCTL_INTC2, 0);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003641 if (dev->phy.rev < 6) {
3642 b43_phy_write(dev, B43_NPHY_RFCTL_INTC3, 0);
3643 b43_phy_write(dev, B43_NPHY_RFCTL_INTC4, 0);
3644 }
Michael Buesch95b66ba2008-01-18 01:09:25 +01003645 b43_phy_mask(dev, B43_NPHY_RFSEQMODE,
3646 ~(B43_NPHY_RFSEQMODE_CAOVER |
3647 B43_NPHY_RFSEQMODE_TROVER));
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003648 if (dev->phy.rev >= 3)
3649 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, 0);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003650 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, 0);
3651
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003652 if (dev->phy.rev <= 2) {
3653 tmp = (dev->phy.rev == 2) ? 0x3B : 0x40;
3654 b43_phy_maskset(dev, B43_NPHY_BPHY_CTL3,
3655 ~B43_NPHY_BPHY_CTL3_SCALE,
3656 tmp << B43_NPHY_BPHY_CTL3_SCALE_SHIFT);
3657 }
Michael Buesch95b66ba2008-01-18 01:09:25 +01003658 b43_phy_write(dev, B43_NPHY_AFESEQ_TX2RX_PUD_20M, 0x20);
3659 b43_phy_write(dev, B43_NPHY_AFESEQ_TX2RX_PUD_40M, 0x20);
3660
Rafał Miłecki05814832011-05-18 02:06:39 +02003661 if (sprom->boardflags2_lo & 0x100 ||
Rafał Miłecki79d22322011-05-18 02:06:42 +02003662 (dev->dev->board_vendor == PCI_VENDOR_ID_APPLE &&
3663 dev->dev->board_type == 0x8B))
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003664 b43_phy_write(dev, B43_NPHY_TXREALFD, 0xA0);
3665 else
3666 b43_phy_write(dev, B43_NPHY_TXREALFD, 0xB8);
3667 b43_phy_write(dev, B43_NPHY_MIMO_CRSTXEXT, 0xC8);
3668 b43_phy_write(dev, B43_NPHY_PLOAD_CSENSE_EXTLEN, 0x50);
3669 b43_phy_write(dev, B43_NPHY_TXRIFS_FRDEL, 0x30);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003670
Rafał Miłeckiad9716e2010-01-17 13:03:40 +01003671 b43_nphy_update_mimo_config(dev, nphy->preamble_override);
Rafał Miłecki4f4ab6c2010-01-17 13:03:55 +01003672 b43_nphy_update_txrx_chain(dev);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003673
3674 if (phy->rev < 2) {
3675 b43_phy_write(dev, B43_NPHY_DUP40_GFBL, 0xAA8);
3676 b43_phy_write(dev, B43_NPHY_DUP40_BL, 0x9A4);
3677 }
Michael Buesch95b66ba2008-01-18 01:09:25 +01003678
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003679 tmp2 = b43_current_band(dev->wl);
3680 if ((nphy->ipa2g_on && tmp2 == IEEE80211_BAND_2GHZ) ||
3681 (nphy->ipa5g_on && tmp2 == IEEE80211_BAND_5GHZ)) {
3682 b43_phy_set(dev, B43_NPHY_PAPD_EN0, 0x1);
3683 b43_phy_maskset(dev, B43_NPHY_EPS_TABLE_ADJ0, 0x007F,
3684 nphy->papd_epsilon_offset[0] << 7);
3685 b43_phy_set(dev, B43_NPHY_PAPD_EN1, 0x1);
3686 b43_phy_maskset(dev, B43_NPHY_EPS_TABLE_ADJ1, 0x007F,
3687 nphy->papd_epsilon_offset[1] << 7);
Rafał Miłecki45ca6972010-01-22 01:53:15 +01003688 b43_nphy_int_pa_set_tx_dig_filters(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003689 } else if (phy->rev >= 5) {
Rafał Miłecki45ca6972010-01-22 01:53:15 +01003690 b43_nphy_ext_pa_set_tx_dig_filters(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003691 }
3692
3693 b43_nphy_workarounds(dev);
3694
3695 /* Reset CCA, in init code it differs a little from standard way */
Rafał Miłecki730dd702010-01-15 16:38:07 +01003696 b43_nphy_bmac_clock_fgc(dev, 1);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003697 tmp = b43_phy_read(dev, B43_NPHY_BBCFG);
3698 b43_phy_write(dev, B43_NPHY_BBCFG, tmp | B43_NPHY_BBCFG_RSTCCA);
3699 b43_phy_write(dev, B43_NPHY_BBCFG, tmp & ~B43_NPHY_BBCFG_RSTCCA);
Rafał Miłecki730dd702010-01-15 16:38:07 +01003700 b43_nphy_bmac_clock_fgc(dev, 0);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003701
Rafał Miłecki858a1652011-05-10 16:05:33 +02003702 b43_mac_phy_clock_set(dev, true);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003703
Rafał Miłeckie50cbcf2010-01-15 15:02:38 +01003704 b43_nphy_pa_override(dev, false);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003705 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RX2TX);
3706 b43_nphy_force_rf_sequence(dev, B43_RFSEQ_RESET2RX);
Rafał Miłeckie50cbcf2010-01-15 15:02:38 +01003707 b43_nphy_pa_override(dev, true);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003708
Rafał Miłeckibbec3982010-01-15 14:31:39 +01003709 b43_nphy_classifier(dev, 0, 0);
3710 b43_nphy_read_clip_detection(dev, clip);
Rafał Miłeckibec18642010-11-18 13:28:00 +01003711 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)
3712 b43_nphy_bphy_init(dev);
3713
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003714 tx_pwr_state = nphy->txpwrctrl;
Rafał Miłecki161d5402010-11-28 12:59:43 +01003715 b43_nphy_tx_power_ctrl(dev, false);
3716 b43_nphy_tx_power_fix(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003717 /* TODO N PHY TX Power Control Idle TSSI */
3718 /* TODO N PHY TX Power Control Setup */
Michael Buesch95b66ba2008-01-18 01:09:25 +01003719
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003720 if (phy->rev >= 3) {
3721 /* TODO */
3722 } else {
Rafał Miłecki2581b142010-01-18 00:21:21 +01003723 b43_ntab_write_bulk(dev, B43_NTAB32(26, 192), 128,
3724 b43_ntab_tx_gain_rev0_1_2);
3725 b43_ntab_write_bulk(dev, B43_NTAB32(27, 192), 128,
3726 b43_ntab_tx_gain_rev0_1_2);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003727 }
3728
3729 if (nphy->phyrxchain != 3)
Gábor Stefanik4e687b22010-08-16 22:39:17 +02003730 b43_nphy_set_rx_core_state(dev, nphy->phyrxchain);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003731 if (nphy->mphase_cal_phase_id > 0)
3732 ;/* TODO PHY Periodic Calibration Multi-Phase Restart */
3733
3734 do_rssi_cal = false;
3735 if (phy->rev >= 3) {
3736 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)
Rafał Miłecki204a6652010-10-14 19:33:34 +02003737 do_rssi_cal = !nphy->rssical_chanspec_2G.center_freq;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003738 else
Rafał Miłecki204a6652010-10-14 19:33:34 +02003739 do_rssi_cal = !nphy->rssical_chanspec_5G.center_freq;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003740
3741 if (do_rssi_cal)
Rafał Miłecki4cb99772010-01-15 13:40:58 +01003742 b43_nphy_rssi_cal(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003743 else
Rafał Miłecki42e15472010-01-15 15:06:47 +01003744 b43_nphy_restore_rssi_cal(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003745 } else {
Rafał Miłecki4cb99772010-01-15 13:40:58 +01003746 b43_nphy_rssi_cal(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003747 }
3748
3749 if (!((nphy->measure_hold & 0x6) != 0)) {
3750 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)
Rafał Miłecki204a6652010-10-14 19:33:34 +02003751 do_cal = !nphy->iqcal_chanspec_2G.center_freq;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003752 else
Rafał Miłecki204a6652010-10-14 19:33:34 +02003753 do_cal = !nphy->iqcal_chanspec_5G.center_freq;
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003754
3755 if (nphy->mute)
3756 do_cal = false;
3757
3758 if (do_cal) {
Rafał Miłeckib0022e12010-01-15 15:40:50 +01003759 target = b43_nphy_get_tx_gains(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003760
3761 if (nphy->antsel_type == 2)
Rafał Miłecki8987a9e2010-02-27 13:03:33 +01003762 b43_nphy_superswitch_init(dev, true);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003763 if (nphy->perical != 2) {
Rafał Miłecki90b97382010-01-15 14:48:21 +01003764 b43_nphy_rssi_cal(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003765 if (phy->rev >= 3) {
3766 nphy->cal_orig_pwr_idx[0] =
3767 nphy->txpwrindex[0].index_internal;
3768 nphy->cal_orig_pwr_idx[1] =
3769 nphy->txpwrindex[1].index_internal;
3770 /* TODO N PHY Pre Calibrate TX Gain */
Rafał Miłeckib0022e12010-01-15 15:40:50 +01003771 target = b43_nphy_get_tx_gains(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003772 }
Rafał Miłeckie7797bf2010-11-30 22:33:16 +01003773 if (!b43_nphy_cal_tx_iq_lo(dev, target, true, false))
3774 if (b43_nphy_cal_rx_iq(dev, target, 2, 0) == 0)
3775 b43_nphy_save_cal(dev);
3776 } else if (nphy->mphase_cal_phase_id == 0)
3777 ;/* N PHY Periodic Calibration with arg 3 */
3778 } else {
3779 b43_nphy_restore_cal(dev);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003780 }
3781 }
3782
Rafał Miłecki6dcd9d92010-01-15 16:24:57 +01003783 b43_nphy_tx_pwr_ctrl_coef_setup(dev);
Rafał Miłecki161d5402010-11-28 12:59:43 +01003784 b43_nphy_tx_power_ctrl(dev, tx_pwr_state);
Rafał Miłecki0988a7a2010-01-15 13:27:29 +01003785 b43_phy_write(dev, B43_NPHY_TXMACIF_HOLDOFF, 0x0015);
3786 b43_phy_write(dev, B43_NPHY_TXMACDELAY, 0x0320);
3787 if (phy->rev >= 3 && phy->rev <= 6)
3788 b43_phy_write(dev, B43_NPHY_PLOAD_CSENSE_EXTLEN, 0x0014);
Rafał Miłeckife3e46e2010-01-15 15:51:55 +01003789 b43_nphy_tx_lp_fbw(dev);
Rafał Miłecki9442e5b2010-02-04 12:23:12 +01003790 if (phy->rev >= 3)
3791 b43_nphy_spur_workaround(dev);
Michael Buesch95b66ba2008-01-18 01:09:25 +01003792
Michael Buesch53a6e232008-01-13 21:23:44 +01003793 return 0;
Michael Buesch424047e2008-01-09 16:13:56 +01003794}
Michael Bueschef1a6282008-08-27 18:53:02 +02003795
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003796/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/ChanspecSetup */
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003797static void b43_nphy_channel_setup(struct b43_wldev *dev,
Rafał Miłeckib15b3032010-03-29 00:53:13 +02003798 const struct b43_phy_n_sfo_cfg *e,
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003799 struct ieee80211_channel *new_channel)
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003800{
3801 struct b43_phy *phy = &dev->phy;
3802 struct b43_phy_n *nphy = dev->phy.n;
3803
Rafał Miłecki087de742010-10-11 03:11:03 +02003804 u16 old_band_5ghz;
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003805 u32 tmp32;
3806
Rafał Miłecki087de742010-10-11 03:11:03 +02003807 old_band_5ghz =
3808 b43_phy_read(dev, B43_NPHY_BANDCTL) & B43_NPHY_BANDCTL_5GHZ;
3809 if (new_channel->band == IEEE80211_BAND_5GHZ && !old_band_5ghz) {
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003810 tmp32 = b43_read32(dev, B43_MMIO_PSM_PHY_HDR);
3811 b43_write32(dev, B43_MMIO_PSM_PHY_HDR, tmp32 | 4);
3812 b43_phy_set(dev, B43_PHY_B_BBCFG, 0xC000);
3813 b43_write32(dev, B43_MMIO_PSM_PHY_HDR, tmp32);
3814 b43_phy_set(dev, B43_NPHY_BANDCTL, B43_NPHY_BANDCTL_5GHZ);
Rafał Miłecki087de742010-10-11 03:11:03 +02003815 } else if (new_channel->band == IEEE80211_BAND_2GHZ && old_band_5ghz) {
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003816 b43_phy_mask(dev, B43_NPHY_BANDCTL, ~B43_NPHY_BANDCTL_5GHZ);
3817 tmp32 = b43_read32(dev, B43_MMIO_PSM_PHY_HDR);
3818 b43_write32(dev, B43_MMIO_PSM_PHY_HDR, tmp32 | 4);
Larry Fingeracd82aa2010-07-21 11:48:05 -05003819 b43_phy_mask(dev, B43_PHY_B_BBCFG, 0x3FFF);
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003820 b43_write32(dev, B43_MMIO_PSM_PHY_HDR, tmp32);
3821 }
3822
3823 b43_chantab_phy_upload(dev, e);
3824
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003825 if (new_channel->hw_value == 14) {
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003826 b43_nphy_classifier(dev, 2, 0);
3827 b43_phy_set(dev, B43_PHY_B_TEST, 0x0800);
3828 } else {
3829 b43_nphy_classifier(dev, 2, 2);
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003830 if (new_channel->band == IEEE80211_BAND_2GHZ)
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003831 b43_phy_mask(dev, B43_PHY_B_TEST, ~0x840);
3832 }
3833
Rafał Miłecki161d5402010-11-28 12:59:43 +01003834 if (!nphy->txpwrctrl)
Rafał Miłecki1b69ec72010-02-27 13:03:40 +01003835 b43_nphy_tx_power_fix(dev);
3836
3837 if (dev->phy.rev < 3)
3838 b43_nphy_adjust_lna_gain_table(dev);
3839
3840 b43_nphy_tx_lp_fbw(dev);
3841
3842 if (dev->phy.rev >= 3 && 0) {
3843 /* TODO */
3844 }
3845
3846 b43_phy_write(dev, B43_NPHY_NDATAT_DUP40, 0x3830);
3847
3848 if (phy->rev >= 3)
3849 b43_nphy_spur_workaround(dev);
3850}
3851
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003852/* http://bcm-v4.sipsolutions.net/802.11/PHY/N/SetChanspec */
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003853static int b43_nphy_set_channel(struct b43_wldev *dev,
3854 struct ieee80211_channel *channel,
3855 enum nl80211_channel_type channel_type)
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003856{
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003857 struct b43_phy *phy = &dev->phy;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003858
John W. Linville2eeb6fd2011-04-29 14:54:27 -04003859 const struct b43_nphy_channeltab_entry_rev2 *tabent_r2 = NULL;
3860 const struct b43_nphy_channeltab_entry_rev3 *tabent_r3 = NULL;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003861
3862 u8 tmp;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003863
3864 if (dev->phy.rev >= 3) {
Rafał Miłeckif2a6d6a2010-10-11 03:19:22 +02003865 tabent_r3 = b43_nphy_get_chantabent_rev3(dev,
3866 channel->center_freq);
Rafał Miłeckif19ebe72010-03-29 00:53:15 +02003867 if (!tabent_r3)
3868 return -ESRCH;
Rafał Miłeckiffd2d9b2010-03-29 00:53:14 +02003869 } else {
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003870 tabent_r2 = b43_nphy_get_chantabent_rev2(dev,
3871 channel->hw_value);
Rafał Miłeckif19ebe72010-03-29 00:53:15 +02003872 if (!tabent_r2)
Rafał Miłeckiffd2d9b2010-03-29 00:53:14 +02003873 return -ESRCH;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003874 }
3875
Rafał Miłecki204a6652010-10-14 19:33:34 +02003876 /* Channel is set later in common code, but we need to set it on our
3877 own to let this function's subcalls work properly. */
3878 phy->channel = channel->hw_value;
3879 phy->channel_freq = channel->center_freq;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003880
Rafał Miłeckie5c407f2010-10-11 03:11:02 +02003881 if (b43_channel_type_is_40mhz(phy->channel_type) !=
3882 b43_channel_type_is_40mhz(channel_type))
3883 ; /* TODO: BMAC BW Set (channel_type) */
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003884
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003885 if (channel_type == NL80211_CHAN_HT40PLUS)
3886 b43_phy_set(dev, B43_NPHY_RXCTL,
3887 B43_NPHY_RXCTL_BSELU20);
3888 else if (channel_type == NL80211_CHAN_HT40MINUS)
3889 b43_phy_mask(dev, B43_NPHY_RXCTL,
3890 ~B43_NPHY_RXCTL_BSELU20);
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003891
3892 if (dev->phy.rev >= 3) {
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003893 tmp = (channel->band == IEEE80211_BAND_5GHZ) ? 4 : 0;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003894 b43_radio_maskset(dev, 0x08, 0xFFFB, tmp);
Rafał Miłeckid4814e62010-12-21 23:57:48 +01003895 b43_radio_2056_setup(dev, tabent_r3);
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003896 b43_nphy_channel_setup(dev, &(tabent_r3->phy_regs), channel);
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003897 } else {
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003898 tmp = (channel->band == IEEE80211_BAND_5GHZ) ? 0x0020 : 0x0050;
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003899 b43_radio_maskset(dev, B2055_MASTER1, 0xFF8F, tmp);
Rafał Miłeckif19ebe72010-03-29 00:53:15 +02003900 b43_radio_2055_setup(dev, tabent_r2);
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02003901 b43_nphy_channel_setup(dev, &(tabent_r2->phy_regs), channel);
Rafał Miłeckieff66c52010-02-27 13:03:41 +01003902 }
3903
3904 return 0;
3905}
3906
Michael Bueschef1a6282008-08-27 18:53:02 +02003907static int b43_nphy_op_allocate(struct b43_wldev *dev)
3908{
3909 struct b43_phy_n *nphy;
3910
3911 nphy = kzalloc(sizeof(*nphy), GFP_KERNEL);
3912 if (!nphy)
3913 return -ENOMEM;
3914 dev->phy.n = nphy;
3915
Michael Bueschef1a6282008-08-27 18:53:02 +02003916 return 0;
3917}
3918
Michael Bueschfb111372008-09-02 13:00:34 +02003919static void b43_nphy_op_prepare_structs(struct b43_wldev *dev)
3920{
3921 struct b43_phy *phy = &dev->phy;
3922 struct b43_phy_n *nphy = phy->n;
3923
3924 memset(nphy, 0, sizeof(*nphy));
3925
Rafał Miłeckiaca434d2010-12-21 11:50:22 +01003926 nphy->hang_avoid = (phy->rev == 3 || phy->rev == 4);
Rafał Miłecki0b81c232010-11-18 21:11:43 +01003927 nphy->gain_boost = true; /* this way we follow wl, assume it is true */
3928 nphy->txrx_chain = 2; /* sth different than 0 and 1 for now */
3929 nphy->phyrxchain = 3; /* to avoid b43_nphy_set_rx_core_state like wl */
Rafał Miłecki8c1d5a72010-11-28 12:59:44 +01003930 nphy->perical = 2; /* avoid additional rssi cal on init (like wl) */
Michael Bueschfb111372008-09-02 13:00:34 +02003931}
3932
3933static void b43_nphy_op_free(struct b43_wldev *dev)
3934{
3935 struct b43_phy *phy = &dev->phy;
3936 struct b43_phy_n *nphy = phy->n;
3937
3938 kfree(nphy);
3939 phy->n = NULL;
3940}
3941
Michael Bueschef1a6282008-08-27 18:53:02 +02003942static int b43_nphy_op_init(struct b43_wldev *dev)
3943{
Michael Bueschfb111372008-09-02 13:00:34 +02003944 return b43_phy_initn(dev);
Michael Bueschef1a6282008-08-27 18:53:02 +02003945}
3946
3947static inline void check_phyreg(struct b43_wldev *dev, u16 offset)
3948{
3949#if B43_DEBUG
3950 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_OFDM_GPHY) {
3951 /* OFDM registers are onnly available on A/G-PHYs */
3952 b43err(dev->wl, "Invalid OFDM PHY access at "
3953 "0x%04X on N-PHY\n", offset);
3954 dump_stack();
3955 }
3956 if ((offset & B43_PHYROUTE) == B43_PHYROUTE_EXT_GPHY) {
3957 /* Ext-G registers are only available on G-PHYs */
3958 b43err(dev->wl, "Invalid EXT-G PHY access at "
3959 "0x%04X on N-PHY\n", offset);
3960 dump_stack();
3961 }
3962#endif /* B43_DEBUG */
3963}
3964
3965static u16 b43_nphy_op_read(struct b43_wldev *dev, u16 reg)
3966{
3967 check_phyreg(dev, reg);
3968 b43_write16(dev, B43_MMIO_PHY_CONTROL, reg);
3969 return b43_read16(dev, B43_MMIO_PHY_DATA);
3970}
3971
3972static void b43_nphy_op_write(struct b43_wldev *dev, u16 reg, u16 value)
3973{
3974 check_phyreg(dev, reg);
3975 b43_write16(dev, B43_MMIO_PHY_CONTROL, reg);
3976 b43_write16(dev, B43_MMIO_PHY_DATA, value);
3977}
3978
Rafał Miłecki755fd182010-12-07 09:42:06 +01003979static void b43_nphy_op_maskset(struct b43_wldev *dev, u16 reg, u16 mask,
3980 u16 set)
3981{
3982 check_phyreg(dev, reg);
3983 b43_write16(dev, B43_MMIO_PHY_CONTROL, reg);
3984 b43_write16(dev, B43_MMIO_PHY_DATA,
3985 (b43_read16(dev, B43_MMIO_PHY_DATA) & mask) | set);
3986}
3987
Michael Bueschef1a6282008-08-27 18:53:02 +02003988static u16 b43_nphy_op_radio_read(struct b43_wldev *dev, u16 reg)
3989{
3990 /* Register 1 is a 32-bit register. */
3991 B43_WARN_ON(reg == 1);
3992 /* N-PHY needs 0x100 for read access */
3993 reg |= 0x100;
3994
3995 b43_write16(dev, B43_MMIO_RADIO_CONTROL, reg);
3996 return b43_read16(dev, B43_MMIO_RADIO_DATA_LOW);
3997}
3998
3999static void b43_nphy_op_radio_write(struct b43_wldev *dev, u16 reg, u16 value)
4000{
4001 /* Register 1 is a 32-bit register. */
4002 B43_WARN_ON(reg == 1);
4003
4004 b43_write16(dev, B43_MMIO_RADIO_CONTROL, reg);
4005 b43_write16(dev, B43_MMIO_RADIO_DATA_LOW, value);
4006}
4007
Rafał Miłeckic2b7aef2010-02-27 13:03:34 +01004008/* http://bcm-v4.sipsolutions.net/802.11/Radio/Switch%20Radio */
Michael Bueschef1a6282008-08-27 18:53:02 +02004009static void b43_nphy_op_software_rfkill(struct b43_wldev *dev,
Johannes Berg19d337d2009-06-02 13:01:37 +02004010 bool blocked)
Rafał Miłeckic2b7aef2010-02-27 13:03:34 +01004011{
4012 if (b43_read32(dev, B43_MMIO_MACCTL) & B43_MACCTL_ENABLED)
4013 b43err(dev->wl, "MAC not suspended\n");
4014
4015 if (blocked) {
4016 b43_phy_mask(dev, B43_NPHY_RFCTL_CMD,
4017 ~B43_NPHY_RFCTL_CMD_CHIP0PU);
4018 if (dev->phy.rev >= 3) {
4019 b43_radio_mask(dev, 0x09, ~0x2);
4020
4021 b43_radio_write(dev, 0x204D, 0);
4022 b43_radio_write(dev, 0x2053, 0);
4023 b43_radio_write(dev, 0x2058, 0);
4024 b43_radio_write(dev, 0x205E, 0);
4025 b43_radio_mask(dev, 0x2062, ~0xF0);
4026 b43_radio_write(dev, 0x2064, 0);
4027
4028 b43_radio_write(dev, 0x304D, 0);
4029 b43_radio_write(dev, 0x3053, 0);
4030 b43_radio_write(dev, 0x3058, 0);
4031 b43_radio_write(dev, 0x305E, 0);
4032 b43_radio_mask(dev, 0x3062, ~0xF0);
4033 b43_radio_write(dev, 0x3064, 0);
4034 }
4035 } else {
4036 if (dev->phy.rev >= 3) {
Rafał Miłeckid817f4e2010-03-29 00:53:12 +02004037 b43_radio_init2056(dev);
Rafał Miłecki78159782010-10-06 07:50:08 +02004038 b43_switch_channel(dev, dev->phy.channel);
Rafał Miłeckic2b7aef2010-02-27 13:03:34 +01004039 } else {
4040 b43_radio_init2055(dev);
4041 }
4042 }
Michael Bueschef1a6282008-08-27 18:53:02 +02004043}
4044
Rafał Miłecki0f4091b2011-03-01 21:40:39 +01004045/* http://bcm-v4.sipsolutions.net/802.11/PHY/Anacore */
Michael Bueschcb24f572008-09-03 12:12:20 +02004046static void b43_nphy_op_switch_analog(struct b43_wldev *dev, bool on)
4047{
Rafał Miłecki2a870832011-06-19 13:30:20 +02004048 u16 override = on ? 0x0 : 0x7FFF;
4049 u16 core = on ? 0xD : 0x00FD;
Rafał Miłecki0f4091b2011-03-01 21:40:39 +01004050
Rafał Miłecki2a870832011-06-19 13:30:20 +02004051 if (dev->phy.rev >= 3) {
4052 if (on) {
4053 b43_phy_write(dev, B43_NPHY_AFECTL_C1, core);
4054 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, override);
4055 b43_phy_write(dev, B43_NPHY_AFECTL_C2, core);
4056 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, override);
4057 } else {
4058 b43_phy_write(dev, B43_NPHY_AFECTL_OVER1, override);
4059 b43_phy_write(dev, B43_NPHY_AFECTL_C1, core);
4060 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, override);
4061 b43_phy_write(dev, B43_NPHY_AFECTL_C2, core);
4062 }
4063 } else {
4064 b43_phy_write(dev, B43_NPHY_AFECTL_OVER, override);
4065 }
Michael Bueschcb24f572008-09-03 12:12:20 +02004066}
4067
Michael Bueschef1a6282008-08-27 18:53:02 +02004068static int b43_nphy_op_switch_channel(struct b43_wldev *dev,
4069 unsigned int new_channel)
4070{
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02004071 struct ieee80211_channel *channel = dev->wl->hw->conf.channel;
4072 enum nl80211_channel_type channel_type = dev->wl->hw->conf.channel_type;
Rafał Miłecki5e7ee092010-10-06 07:50:06 +02004073
Michael Bueschef1a6282008-08-27 18:53:02 +02004074 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ) {
4075 if ((new_channel < 1) || (new_channel > 14))
4076 return -EINVAL;
4077 } else {
4078 if (new_channel > 200)
4079 return -EINVAL;
4080 }
4081
Rafał Miłeckia656b6a2010-10-11 03:11:01 +02004082 return b43_nphy_set_channel(dev, channel, channel_type);
Michael Bueschef1a6282008-08-27 18:53:02 +02004083}
4084
4085static unsigned int b43_nphy_op_get_default_chan(struct b43_wldev *dev)
4086{
4087 if (b43_current_band(dev->wl) == IEEE80211_BAND_2GHZ)
4088 return 1;
4089 return 36;
4090}
4091
Michael Bueschef1a6282008-08-27 18:53:02 +02004092const struct b43_phy_operations b43_phyops_n = {
4093 .allocate = b43_nphy_op_allocate,
Michael Bueschfb111372008-09-02 13:00:34 +02004094 .free = b43_nphy_op_free,
4095 .prepare_structs = b43_nphy_op_prepare_structs,
Michael Bueschef1a6282008-08-27 18:53:02 +02004096 .init = b43_nphy_op_init,
Michael Bueschef1a6282008-08-27 18:53:02 +02004097 .phy_read = b43_nphy_op_read,
4098 .phy_write = b43_nphy_op_write,
Rafał Miłecki755fd182010-12-07 09:42:06 +01004099 .phy_maskset = b43_nphy_op_maskset,
Michael Bueschef1a6282008-08-27 18:53:02 +02004100 .radio_read = b43_nphy_op_radio_read,
4101 .radio_write = b43_nphy_op_radio_write,
4102 .software_rfkill = b43_nphy_op_software_rfkill,
Michael Bueschcb24f572008-09-03 12:12:20 +02004103 .switch_analog = b43_nphy_op_switch_analog,
Michael Bueschef1a6282008-08-27 18:53:02 +02004104 .switch_channel = b43_nphy_op_switch_channel,
4105 .get_default_chan = b43_nphy_op_get_default_chan,
Michael Buesch18c8ade2008-08-28 19:33:40 +02004106 .recalc_txpower = b43_nphy_op_recalc_txpower,
4107 .adjust_txpower = b43_nphy_op_adjust_txpower,
Michael Bueschef1a6282008-08-27 18:53:02 +02004108};