0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017 #include "b43.h"
0018 #include "lo.h"
0019 #include "phy_g.h"
0020 #include "main.h"
0021
0022 #include <linux/delay.h>
0023 #include <linux/sched.h>
0024 #include <linux/slab.h>
0025
0026
0027 static struct b43_lo_calib *b43_find_lo_calib(struct b43_txpower_lo_control *lo,
0028 const struct b43_bbatt *bbatt,
0029 const struct b43_rfatt *rfatt)
0030 {
0031 struct b43_lo_calib *c;
0032
0033 list_for_each_entry(c, &lo->calib_list, list) {
0034 if (!b43_compare_bbatt(&c->bbatt, bbatt))
0035 continue;
0036 if (!b43_compare_rfatt(&c->rfatt, rfatt))
0037 continue;
0038 return c;
0039 }
0040
0041 return NULL;
0042 }
0043
0044
0045 static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
0046 {
0047 struct b43_phy *phy = &dev->phy;
0048 u16 value;
0049
0050 if (B43_DEBUG) {
0051 if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
0052 b43dbg(dev->wl, "Invalid LO control pair "
0053 "(I: %d, Q: %d)\n", control->i, control->q);
0054 dump_stack();
0055 return;
0056 }
0057 }
0058 B43_WARN_ON(phy->type != B43_PHYTYPE_G);
0059
0060 value = (u8) (control->q);
0061 value |= ((u8) (control->i)) << 8;
0062 b43_phy_write(dev, B43_PHY_LO_CTL, value);
0063 }
0064
0065 static u16 lo_measure_feedthrough(struct b43_wldev *dev,
0066 u16 lna, u16 pga, u16 trsw_rx)
0067 {
0068 struct b43_phy *phy = &dev->phy;
0069 u16 rfover;
0070 u16 feedthrough;
0071
0072 if (phy->gmode) {
0073 lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
0074 pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
0075
0076 B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
0077 B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
0078
0079
0080
0081 trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
0082
0083
0084 rfover = B43_PHY_RFOVERVAL_UNK;
0085 rfover |= pga;
0086 rfover |= lna;
0087 rfover |= trsw_rx;
0088 if ((dev->dev->bus_sprom->boardflags_lo & B43_BFL_EXTLNA)
0089 && phy->rev > 6)
0090 rfover |= B43_PHY_RFOVERVAL_EXTLNA;
0091
0092 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
0093 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
0094 udelay(10);
0095 rfover |= B43_PHY_RFOVERVAL_BW_LBW;
0096 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
0097 udelay(10);
0098 rfover |= B43_PHY_RFOVERVAL_BW_LPF;
0099 b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
0100 udelay(10);
0101 b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
0102 } else {
0103 pga |= B43_PHY_PGACTL_UNKNOWN;
0104 b43_phy_write(dev, B43_PHY_PGACTL, pga);
0105 udelay(10);
0106 pga |= B43_PHY_PGACTL_LOWBANDW;
0107 b43_phy_write(dev, B43_PHY_PGACTL, pga);
0108 udelay(10);
0109 pga |= B43_PHY_PGACTL_LPF;
0110 b43_phy_write(dev, B43_PHY_PGACTL, pga);
0111 }
0112 udelay(21);
0113 feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
0114
0115
0116
0117
0118 cond_resched();
0119
0120 return feedthrough;
0121 }
0122
0123
0124
0125
0126
0127
0128 static u16 lo_txctl_register_table(struct b43_wldev *dev,
0129 u16 *value, u16 *pad_mix_gain)
0130 {
0131 struct b43_phy *phy = &dev->phy;
0132 u16 reg, v, padmix;
0133
0134 if (phy->type == B43_PHYTYPE_B) {
0135 v = 0x30;
0136 if (phy->radio_rev <= 5) {
0137 reg = 0x43;
0138 padmix = 0;
0139 } else {
0140 reg = 0x52;
0141 padmix = 5;
0142 }
0143 } else {
0144 if (phy->rev >= 2 && phy->radio_rev == 8) {
0145 reg = 0x43;
0146 v = 0x10;
0147 padmix = 2;
0148 } else {
0149 reg = 0x52;
0150 v = 0x30;
0151 padmix = 5;
0152 }
0153 }
0154 if (value)
0155 *value = v;
0156 if (pad_mix_gain)
0157 *pad_mix_gain = padmix;
0158
0159 return reg;
0160 }
0161
0162 static void lo_measure_txctl_values(struct b43_wldev *dev)
0163 {
0164 struct b43_phy *phy = &dev->phy;
0165 struct b43_phy_g *gphy = phy->g;
0166 struct b43_txpower_lo_control *lo = gphy->lo_control;
0167 u16 reg, mask;
0168 u16 trsw_rx, pga;
0169 u16 radio_pctl_reg;
0170
0171 static const u8 tx_bias_values[] = {
0172 0x09, 0x08, 0x0A, 0x01, 0x00,
0173 0x02, 0x05, 0x04, 0x06,
0174 };
0175 static const u8 tx_magn_values[] = {
0176 0x70, 0x40,
0177 };
0178
0179 if (!has_loopback_gain(phy)) {
0180 radio_pctl_reg = 6;
0181 trsw_rx = 2;
0182 pga = 0;
0183 } else {
0184 int lb_gain;
0185
0186 trsw_rx = 0;
0187 lb_gain = gphy->max_lb_gain / 2;
0188 if (lb_gain > 10) {
0189 radio_pctl_reg = 0;
0190 pga = abs(10 - lb_gain) / 6;
0191 pga = clamp_val(pga, 0, 15);
0192 } else {
0193 int cmp_val;
0194 int tmp;
0195
0196 pga = 0;
0197 cmp_val = 0x24;
0198 if ((phy->rev >= 2) &&
0199 (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
0200 cmp_val = 0x3C;
0201 tmp = lb_gain;
0202 if ((10 - lb_gain) < cmp_val)
0203 tmp = (10 - lb_gain);
0204 if (tmp < 0)
0205 tmp += 6;
0206 else
0207 tmp += 3;
0208 cmp_val /= 4;
0209 tmp /= 4;
0210 if (tmp >= cmp_val)
0211 radio_pctl_reg = cmp_val;
0212 else
0213 radio_pctl_reg = tmp;
0214 }
0215 }
0216 b43_radio_maskset(dev, 0x43, 0xFFF0, radio_pctl_reg);
0217 b43_gphy_set_baseband_attenuation(dev, 2);
0218
0219 reg = lo_txctl_register_table(dev, &mask, NULL);
0220 mask = ~mask;
0221 b43_radio_mask(dev, reg, mask);
0222
0223 if (has_tx_magnification(phy)) {
0224 int i, j;
0225 int feedthrough;
0226 int min_feedth = 0xFFFF;
0227 u8 tx_magn, tx_bias;
0228
0229 for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
0230 tx_magn = tx_magn_values[i];
0231 b43_radio_maskset(dev, 0x52, 0xFF0F, tx_magn);
0232 for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
0233 tx_bias = tx_bias_values[j];
0234 b43_radio_maskset(dev, 0x52, 0xFFF0, tx_bias);
0235 feedthrough =
0236 lo_measure_feedthrough(dev, 0, pga,
0237 trsw_rx);
0238 if (feedthrough < min_feedth) {
0239 lo->tx_bias = tx_bias;
0240 lo->tx_magn = tx_magn;
0241 min_feedth = feedthrough;
0242 }
0243 if (lo->tx_bias == 0)
0244 break;
0245 }
0246 b43_radio_write16(dev, 0x52,
0247 (b43_radio_read16(dev, 0x52)
0248 & 0xFF00) | lo->tx_bias | lo->
0249 tx_magn);
0250 }
0251 } else {
0252 lo->tx_magn = 0;
0253 lo->tx_bias = 0;
0254 b43_radio_mask(dev, 0x52, 0xFFF0);
0255 }
0256 lo->txctl_measured_time = jiffies;
0257 }
0258
0259 static void lo_read_power_vector(struct b43_wldev *dev)
0260 {
0261 struct b43_phy *phy = &dev->phy;
0262 struct b43_phy_g *gphy = phy->g;
0263 struct b43_txpower_lo_control *lo = gphy->lo_control;
0264 int i;
0265 u64 tmp;
0266 u64 power_vector = 0;
0267
0268 for (i = 0; i < 8; i += 2) {
0269 tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
0270 power_vector |= (tmp << (i * 8));
0271
0272 b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
0273 }
0274 if (power_vector)
0275 lo->power_vector = power_vector;
0276 lo->pwr_vec_read_time = jiffies;
0277 }
0278
0279
0280 static void lo_measure_gain_values(struct b43_wldev *dev,
0281 s16 max_rx_gain, int use_trsw_rx)
0282 {
0283 struct b43_phy *phy = &dev->phy;
0284 struct b43_phy_g *gphy = phy->g;
0285 u16 tmp;
0286
0287 if (max_rx_gain < 0)
0288 max_rx_gain = 0;
0289
0290 if (has_loopback_gain(phy)) {
0291 int trsw_rx_gain;
0292
0293 if (use_trsw_rx) {
0294 trsw_rx_gain = gphy->trsw_rx_gain / 2;
0295 if (max_rx_gain >= trsw_rx_gain) {
0296 trsw_rx_gain = max_rx_gain - trsw_rx_gain;
0297 }
0298 } else
0299 trsw_rx_gain = max_rx_gain;
0300 if (trsw_rx_gain < 9) {
0301 gphy->lna_lod_gain = 0;
0302 } else {
0303 gphy->lna_lod_gain = 1;
0304 trsw_rx_gain -= 8;
0305 }
0306 trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
0307 gphy->pga_gain = trsw_rx_gain / 3;
0308 if (gphy->pga_gain >= 5) {
0309 gphy->pga_gain -= 5;
0310 gphy->lna_gain = 2;
0311 } else
0312 gphy->lna_gain = 0;
0313 } else {
0314 gphy->lna_gain = 0;
0315 gphy->trsw_rx_gain = 0x20;
0316 if (max_rx_gain >= 0x14) {
0317 gphy->lna_lod_gain = 1;
0318 gphy->pga_gain = 2;
0319 } else if (max_rx_gain >= 0x12) {
0320 gphy->lna_lod_gain = 1;
0321 gphy->pga_gain = 1;
0322 } else if (max_rx_gain >= 0xF) {
0323 gphy->lna_lod_gain = 1;
0324 gphy->pga_gain = 0;
0325 } else {
0326 gphy->lna_lod_gain = 0;
0327 gphy->pga_gain = 0;
0328 }
0329 }
0330
0331 tmp = b43_radio_read16(dev, 0x7A);
0332 if (gphy->lna_lod_gain == 0)
0333 tmp &= ~0x0008;
0334 else
0335 tmp |= 0x0008;
0336 b43_radio_write16(dev, 0x7A, tmp);
0337 }
0338
0339 struct lo_g_saved_values {
0340 u8 old_channel;
0341
0342
0343 u16 reg_3F4;
0344 u16 reg_3E2;
0345
0346
0347 u16 phy_lo_mask;
0348 u16 phy_extg_01;
0349 u16 phy_dacctl_hwpctl;
0350 u16 phy_dacctl;
0351 u16 phy_cck_14;
0352 u16 phy_hpwr_tssictl;
0353 u16 phy_analogover;
0354 u16 phy_analogoverval;
0355 u16 phy_rfover;
0356 u16 phy_rfoverval;
0357 u16 phy_classctl;
0358 u16 phy_cck_3E;
0359 u16 phy_crs0;
0360 u16 phy_pgactl;
0361 u16 phy_cck_2A;
0362 u16 phy_syncctl;
0363 u16 phy_cck_30;
0364 u16 phy_cck_06;
0365
0366
0367 u16 radio_43;
0368 u16 radio_7A;
0369 u16 radio_52;
0370 };
0371
0372 static void lo_measure_setup(struct b43_wldev *dev,
0373 struct lo_g_saved_values *sav)
0374 {
0375 struct ssb_sprom *sprom = dev->dev->bus_sprom;
0376 struct b43_phy *phy = &dev->phy;
0377 struct b43_phy_g *gphy = phy->g;
0378 struct b43_txpower_lo_control *lo = gphy->lo_control;
0379 u16 tmp;
0380
0381 if (b43_has_hardware_pctl(dev)) {
0382 sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
0383 sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
0384 sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
0385 sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
0386 sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
0387
0388 b43_phy_set(dev, B43_PHY_HPWR_TSSICTL, 0x100);
0389 b43_phy_set(dev, B43_PHY_EXTG(0x01), 0x40);
0390 b43_phy_set(dev, B43_PHY_DACCTL, 0x40);
0391 b43_phy_set(dev, B43_PHY_CCK(0x14), 0x200);
0392 }
0393 if (phy->type == B43_PHYTYPE_B &&
0394 phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
0395 b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
0396 b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
0397 }
0398 if (phy->rev >= 2) {
0399 sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
0400 sav->phy_analogoverval =
0401 b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
0402 sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
0403 sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
0404 sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
0405 sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
0406 sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
0407
0408 b43_phy_mask(dev, B43_PHY_CLASSCTL, 0xFFFC);
0409 b43_phy_mask(dev, B43_PHY_CRS0, 0x7FFF);
0410 b43_phy_set(dev, B43_PHY_ANALOGOVER, 0x0003);
0411 b43_phy_mask(dev, B43_PHY_ANALOGOVERVAL, 0xFFFC);
0412 if (phy->type == B43_PHYTYPE_G) {
0413 if ((phy->rev >= 7) &&
0414 (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
0415 b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
0416 } else {
0417 b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
0418 }
0419 } else {
0420 b43_phy_write(dev, B43_PHY_RFOVER, 0);
0421 }
0422 b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
0423 }
0424 sav->reg_3F4 = b43_read16(dev, 0x3F4);
0425 sav->reg_3E2 = b43_read16(dev, 0x3E2);
0426 sav->radio_43 = b43_radio_read16(dev, 0x43);
0427 sav->radio_7A = b43_radio_read16(dev, 0x7A);
0428 sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
0429 sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
0430 sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
0431 sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
0432
0433 if (!has_tx_magnification(phy)) {
0434 sav->radio_52 = b43_radio_read16(dev, 0x52);
0435 sav->radio_52 &= 0x00F0;
0436 }
0437 if (phy->type == B43_PHYTYPE_B) {
0438 sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
0439 sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
0440 b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
0441 b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
0442 } else {
0443 b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
0444 | 0x8000);
0445 }
0446 b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
0447 & 0xF000);
0448
0449 tmp =
0450 (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
0451 b43_phy_write(dev, tmp, 0x007F);
0452
0453 tmp = sav->phy_syncctl;
0454 b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
0455 tmp = sav->radio_7A;
0456 b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
0457
0458 b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
0459 if (phy->type == B43_PHYTYPE_G ||
0460 (phy->type == B43_PHYTYPE_B &&
0461 phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
0462 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
0463 } else
0464 b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
0465 if (phy->rev >= 2)
0466 b43_dummy_transmission(dev, false, true);
0467 b43_gphy_channel_switch(dev, 6, 0);
0468 b43_radio_read16(dev, 0x51);
0469 if (phy->type == B43_PHYTYPE_G)
0470 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
0471
0472
0473 if (time_before(lo->txctl_measured_time,
0474 jiffies - B43_LO_TXCTL_EXPIRE))
0475 lo_measure_txctl_values(dev);
0476
0477 if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
0478 b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
0479 } else {
0480 if (phy->type == B43_PHYTYPE_B)
0481 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
0482 else
0483 b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
0484 }
0485 }
0486
0487 static void lo_measure_restore(struct b43_wldev *dev,
0488 struct lo_g_saved_values *sav)
0489 {
0490 struct b43_phy *phy = &dev->phy;
0491 struct b43_phy_g *gphy = phy->g;
0492 u16 tmp;
0493
0494 if (phy->rev >= 2) {
0495 b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
0496 tmp = (gphy->pga_gain << 8);
0497 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
0498 udelay(5);
0499 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
0500 udelay(2);
0501 b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
0502 } else {
0503 tmp = (gphy->pga_gain | 0xEFA0);
0504 b43_phy_write(dev, B43_PHY_PGACTL, tmp);
0505 }
0506 if (phy->type == B43_PHYTYPE_G) {
0507 if (phy->rev >= 3)
0508 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
0509 else
0510 b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
0511 if (phy->rev >= 2)
0512 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
0513 else
0514 b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
0515 }
0516 b43_write16(dev, 0x3F4, sav->reg_3F4);
0517 b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
0518 b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
0519 b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
0520 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
0521 b43_radio_write16(dev, 0x43, sav->radio_43);
0522 b43_radio_write16(dev, 0x7A, sav->radio_7A);
0523 if (!has_tx_magnification(phy)) {
0524 tmp = sav->radio_52;
0525 b43_radio_maskset(dev, 0x52, 0xFF0F, tmp);
0526 }
0527 b43_write16(dev, 0x3E2, sav->reg_3E2);
0528 if (phy->type == B43_PHYTYPE_B &&
0529 phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
0530 b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
0531 b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
0532 }
0533 if (phy->rev >= 2) {
0534 b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
0535 b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
0536 sav->phy_analogoverval);
0537 b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
0538 b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
0539 b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
0540 b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
0541 b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
0542 }
0543 if (b43_has_hardware_pctl(dev)) {
0544 tmp = (sav->phy_lo_mask & 0xBFFF);
0545 b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
0546 b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
0547 b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
0548 b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
0549 b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
0550 }
0551 b43_gphy_channel_switch(dev, sav->old_channel, 1);
0552 }
0553
0554 struct b43_lo_g_statemachine {
0555 int current_state;
0556 int nr_measured;
0557 int state_val_multiplier;
0558 u16 lowest_feedth;
0559 struct b43_loctl min_loctl;
0560 };
0561
0562
0563 static int lo_probe_possible_loctls(struct b43_wldev *dev,
0564 struct b43_loctl *probe_loctl,
0565 struct b43_lo_g_statemachine *d)
0566 {
0567 struct b43_phy *phy = &dev->phy;
0568 struct b43_phy_g *gphy = phy->g;
0569 struct b43_loctl test_loctl;
0570 struct b43_loctl orig_loctl;
0571 struct b43_loctl prev_loctl = {
0572 .i = -100,
0573 .q = -100,
0574 };
0575 int i;
0576 int begin, end;
0577 int found_lower = 0;
0578 u16 feedth;
0579
0580 static const struct b43_loctl modifiers[] = {
0581 {.i = 1,.q = 1,},
0582 {.i = 1,.q = 0,},
0583 {.i = 1,.q = -1,},
0584 {.i = 0,.q = -1,},
0585 {.i = -1,.q = -1,},
0586 {.i = -1,.q = 0,},
0587 {.i = -1,.q = 1,},
0588 {.i = 0,.q = 1,},
0589 };
0590
0591 if (d->current_state == 0) {
0592 begin = 1;
0593 end = 8;
0594 } else if (d->current_state % 2 == 0) {
0595 begin = d->current_state - 1;
0596 end = d->current_state + 1;
0597 } else {
0598 begin = d->current_state - 2;
0599 end = d->current_state + 2;
0600 }
0601 if (begin < 1)
0602 begin += 8;
0603 if (end > 8)
0604 end -= 8;
0605
0606 memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
0607 i = begin;
0608 d->current_state = i;
0609 while (1) {
0610 B43_WARN_ON(!(i >= 1 && i <= 8));
0611 memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
0612 test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
0613 test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
0614 if ((test_loctl.i != prev_loctl.i ||
0615 test_loctl.q != prev_loctl.q) &&
0616 (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
0617 b43_lo_write(dev, &test_loctl);
0618 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
0619 gphy->pga_gain,
0620 gphy->trsw_rx_gain);
0621 if (feedth < d->lowest_feedth) {
0622 memcpy(probe_loctl, &test_loctl,
0623 sizeof(struct b43_loctl));
0624 found_lower = 1;
0625 d->lowest_feedth = feedth;
0626 if ((d->nr_measured < 2) &&
0627 !has_loopback_gain(phy))
0628 break;
0629 }
0630 }
0631 memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
0632 if (i == end)
0633 break;
0634 if (i == 8)
0635 i = 1;
0636 else
0637 i++;
0638 d->current_state = i;
0639 }
0640
0641 return found_lower;
0642 }
0643
0644 static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
0645 struct b43_loctl *loctl,
0646 int *max_rx_gain)
0647 {
0648 struct b43_phy *phy = &dev->phy;
0649 struct b43_phy_g *gphy = phy->g;
0650 struct b43_lo_g_statemachine d;
0651 u16 feedth;
0652 int found_lower;
0653 struct b43_loctl probe_loctl;
0654 int max_repeat = 1, repeat_cnt = 0;
0655
0656 d.nr_measured = 0;
0657 d.state_val_multiplier = 1;
0658 if (has_loopback_gain(phy))
0659 d.state_val_multiplier = 3;
0660
0661 memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
0662 if (has_loopback_gain(phy))
0663 max_repeat = 4;
0664 do {
0665 b43_lo_write(dev, &d.min_loctl);
0666 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
0667 gphy->pga_gain,
0668 gphy->trsw_rx_gain);
0669 if (feedth < 0x258) {
0670 if (feedth >= 0x12C)
0671 *max_rx_gain += 6;
0672 else
0673 *max_rx_gain += 3;
0674 feedth = lo_measure_feedthrough(dev, gphy->lna_gain,
0675 gphy->pga_gain,
0676 gphy->trsw_rx_gain);
0677 }
0678 d.lowest_feedth = feedth;
0679
0680 d.current_state = 0;
0681 do {
0682 B43_WARN_ON(!
0683 (d.current_state >= 0
0684 && d.current_state <= 8));
0685 memcpy(&probe_loctl, &d.min_loctl,
0686 sizeof(struct b43_loctl));
0687 found_lower =
0688 lo_probe_possible_loctls(dev, &probe_loctl, &d);
0689 if (!found_lower)
0690 break;
0691 if ((probe_loctl.i == d.min_loctl.i) &&
0692 (probe_loctl.q == d.min_loctl.q))
0693 break;
0694 memcpy(&d.min_loctl, &probe_loctl,
0695 sizeof(struct b43_loctl));
0696 d.nr_measured++;
0697 } while (d.nr_measured < 24);
0698 memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
0699
0700 if (has_loopback_gain(phy)) {
0701 if (d.lowest_feedth > 0x1194)
0702 *max_rx_gain -= 6;
0703 else if (d.lowest_feedth < 0x5DC)
0704 *max_rx_gain += 3;
0705 if (repeat_cnt == 0) {
0706 if (d.lowest_feedth <= 0x5DC) {
0707 d.state_val_multiplier = 1;
0708 repeat_cnt++;
0709 } else
0710 d.state_val_multiplier = 2;
0711 } else if (repeat_cnt == 2)
0712 d.state_val_multiplier = 1;
0713 }
0714 lo_measure_gain_values(dev, *max_rx_gain,
0715 has_loopback_gain(phy));
0716 } while (++repeat_cnt < max_repeat);
0717 }
0718
0719 static
0720 struct b43_lo_calib *b43_calibrate_lo_setting(struct b43_wldev *dev,
0721 const struct b43_bbatt *bbatt,
0722 const struct b43_rfatt *rfatt)
0723 {
0724 struct b43_phy *phy = &dev->phy;
0725 struct b43_phy_g *gphy = phy->g;
0726 struct b43_loctl loctl = {
0727 .i = 0,
0728 .q = 0,
0729 };
0730 int max_rx_gain;
0731 struct b43_lo_calib *cal;
0732 struct lo_g_saved_values saved_regs;
0733
0734 u16 txctl_reg;
0735 u16 txctl_value;
0736 u16 pad_mix_gain;
0737
0738 saved_regs.old_channel = phy->channel;
0739 b43_mac_suspend(dev);
0740 lo_measure_setup(dev, &saved_regs);
0741
0742 txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
0743
0744 b43_radio_maskset(dev, 0x43, 0xFFF0, rfatt->att);
0745 b43_radio_maskset(dev, txctl_reg, ~txctl_value, (rfatt->with_padmix ? txctl_value :0));
0746
0747 max_rx_gain = rfatt->att * 2;
0748 max_rx_gain += bbatt->att / 2;
0749 if (rfatt->with_padmix)
0750 max_rx_gain -= pad_mix_gain;
0751 if (has_loopback_gain(phy))
0752 max_rx_gain += gphy->max_lb_gain;
0753 lo_measure_gain_values(dev, max_rx_gain,
0754 has_loopback_gain(phy));
0755
0756 b43_gphy_set_baseband_attenuation(dev, bbatt->att);
0757 lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
0758
0759 lo_measure_restore(dev, &saved_regs);
0760 b43_mac_enable(dev);
0761
0762 if (b43_debug(dev, B43_DBG_LO)) {
0763 b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
0764 "=> I=%d Q=%d\n",
0765 bbatt->att, rfatt->att, rfatt->with_padmix,
0766 loctl.i, loctl.q);
0767 }
0768
0769 cal = kmalloc(sizeof(*cal), GFP_KERNEL);
0770 if (!cal) {
0771 b43warn(dev->wl, "LO calib: out of memory\n");
0772 return NULL;
0773 }
0774 memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
0775 memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
0776 memcpy(&cal->ctl, &loctl, sizeof(loctl));
0777 cal->calib_time = jiffies;
0778 INIT_LIST_HEAD(&cal->list);
0779
0780 return cal;
0781 }
0782
0783
0784
0785 static
0786 struct b43_lo_calib *b43_get_calib_lo_settings(struct b43_wldev *dev,
0787 const struct b43_bbatt *bbatt,
0788 const struct b43_rfatt *rfatt)
0789 {
0790 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
0791 struct b43_lo_calib *c;
0792
0793 c = b43_find_lo_calib(lo, bbatt, rfatt);
0794 if (c)
0795 return c;
0796
0797
0798 c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
0799 if (!c)
0800 return NULL;
0801 list_add(&c->list, &lo->calib_list);
0802
0803 return c;
0804 }
0805
0806 void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
0807 {
0808 struct b43_phy *phy = &dev->phy;
0809 struct b43_phy_g *gphy = phy->g;
0810 struct b43_txpower_lo_control *lo = gphy->lo_control;
0811 int i;
0812 int rf_offset, bb_offset;
0813 const struct b43_rfatt *rfatt;
0814 const struct b43_bbatt *bbatt;
0815 u64 power_vector;
0816 bool table_changed = false;
0817
0818 BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
0819 B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
0820
0821 power_vector = lo->power_vector;
0822 if (!update_all && !power_vector)
0823 return;
0824
0825
0826
0827 b43_mac_suspend(dev);
0828
0829 for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
0830 struct b43_lo_calib *cal;
0831 int idx;
0832 u16 val;
0833
0834 if (!update_all && !(power_vector & (((u64)1ULL) << i)))
0835 continue;
0836
0837
0838 bb_offset = i / lo->rfatt_list.len;
0839 rf_offset = i % lo->rfatt_list.len;
0840 bbatt = &(lo->bbatt_list.list[bb_offset]);
0841 rfatt = &(lo->rfatt_list.list[rf_offset]);
0842
0843 cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
0844 if (!cal) {
0845 b43warn(dev->wl, "LO: Could not "
0846 "calibrate DC table entry\n");
0847 continue;
0848 }
0849
0850 val = (u8)(cal->ctl.q);
0851 val |= ((u8)(cal->ctl.i)) << 4;
0852 kfree(cal);
0853
0854
0855 idx = i / 2;
0856
0857 if (i % 2) {
0858
0859 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
0860 | ((val & 0x00FF) << 8);
0861 } else {
0862
0863 lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
0864 | (val & 0x00FF);
0865 }
0866 table_changed = true;
0867 }
0868 if (table_changed) {
0869
0870 for (i = 0; i < B43_DC_LT_SIZE; i++)
0871 b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
0872 }
0873 b43_mac_enable(dev);
0874 }
0875
0876
0877
0878 static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
0879 {
0880 if (!rf->with_padmix)
0881 return;
0882 if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
0883 rf->att = 4;
0884 }
0885
0886 void b43_lo_g_adjust(struct b43_wldev *dev)
0887 {
0888 struct b43_phy_g *gphy = dev->phy.g;
0889 struct b43_lo_calib *cal;
0890 struct b43_rfatt rf;
0891
0892 memcpy(&rf, &gphy->rfatt, sizeof(rf));
0893 b43_lo_fixup_rfatt(&rf);
0894
0895 cal = b43_get_calib_lo_settings(dev, &gphy->bbatt, &rf);
0896 if (!cal)
0897 return;
0898 b43_lo_write(dev, &cal->ctl);
0899 }
0900
0901 void b43_lo_g_adjust_to(struct b43_wldev *dev,
0902 u16 rfatt, u16 bbatt, u16 tx_control)
0903 {
0904 struct b43_rfatt rf;
0905 struct b43_bbatt bb;
0906 struct b43_lo_calib *cal;
0907
0908 memset(&rf, 0, sizeof(rf));
0909 memset(&bb, 0, sizeof(bb));
0910 rf.att = rfatt;
0911 bb.att = bbatt;
0912 b43_lo_fixup_rfatt(&rf);
0913 cal = b43_get_calib_lo_settings(dev, &bb, &rf);
0914 if (!cal)
0915 return;
0916 b43_lo_write(dev, &cal->ctl);
0917 }
0918
0919
0920 void b43_lo_g_maintenance_work(struct b43_wldev *dev)
0921 {
0922 struct b43_phy *phy = &dev->phy;
0923 struct b43_phy_g *gphy = phy->g;
0924 struct b43_txpower_lo_control *lo = gphy->lo_control;
0925 unsigned long now;
0926 unsigned long expire;
0927 struct b43_lo_calib *cal, *tmp;
0928 bool current_item_expired = false;
0929 bool hwpctl;
0930
0931 if (!lo)
0932 return;
0933 now = jiffies;
0934 hwpctl = b43_has_hardware_pctl(dev);
0935
0936 if (hwpctl) {
0937
0938 expire = now - B43_LO_PWRVEC_EXPIRE;
0939 if (time_before(lo->pwr_vec_read_time, expire)) {
0940 lo_read_power_vector(dev);
0941 b43_gphy_dc_lt_init(dev, 0);
0942 }
0943
0944 }
0945
0946 if (hwpctl)
0947 return;
0948
0949
0950 expire = now - B43_LO_CALIB_EXPIRE;
0951 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
0952 if (!time_before(cal->calib_time, expire))
0953 continue;
0954
0955 if (b43_compare_bbatt(&cal->bbatt, &gphy->bbatt) &&
0956 b43_compare_rfatt(&cal->rfatt, &gphy->rfatt)) {
0957 B43_WARN_ON(current_item_expired);
0958 current_item_expired = true;
0959 }
0960 if (b43_debug(dev, B43_DBG_LO)) {
0961 b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
0962 "I=%d, Q=%d expired\n",
0963 cal->bbatt.att, cal->rfatt.att,
0964 cal->rfatt.with_padmix,
0965 cal->ctl.i, cal->ctl.q);
0966 }
0967 list_del(&cal->list);
0968 kfree(cal);
0969 }
0970 if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
0971
0972 if (b43_debug(dev, B43_DBG_LO))
0973 b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
0974 cal = b43_calibrate_lo_setting(dev, &gphy->bbatt, &gphy->rfatt);
0975 if (cal) {
0976 list_add(&cal->list, &lo->calib_list);
0977 b43_lo_write(dev, &cal->ctl);
0978 } else
0979 b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
0980 }
0981 }
0982
0983 void b43_lo_g_cleanup(struct b43_wldev *dev)
0984 {
0985 struct b43_txpower_lo_control *lo = dev->phy.g->lo_control;
0986 struct b43_lo_calib *cal, *tmp;
0987
0988 if (!lo)
0989 return;
0990 list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
0991 list_del(&cal->list);
0992 kfree(cal);
0993 }
0994 }
0995
0996
0997 void b43_lo_g_init(struct b43_wldev *dev)
0998 {
0999 if (b43_has_hardware_pctl(dev)) {
1000 lo_read_power_vector(dev);
1001 b43_gphy_dc_lt_init(dev, 1);
1002 }
1003 }