lo.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  1. /*
  2. Broadcom B43 wireless driver
  3. G PHY LO (LocalOscillator) Measuring and Control routines
  4. Copyright (c) 2005 Martin Langer <martin-langer@gmx.de>,
  5. Copyright (c) 2005, 2006 Stefano Brivio <st3@riseup.net>
  6. Copyright (c) 2005-2007 Michael Buesch <mb@bu3sch.de>
  7. Copyright (c) 2005, 2006 Danny van Dyk <kugelfang@gentoo.org>
  8. Copyright (c) 2005, 2006 Andreas Jaggi <andreas.jaggi@waterwave.ch>
  9. This program is free software; you can redistribute it and/or modify
  10. it under the terms of the GNU General Public License as published by
  11. the Free Software Foundation; either version 2 of the License, or
  12. (at your option) any later version.
  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. You should have received a copy of the GNU General Public License
  18. along with this program; see the file COPYING. If not, write to
  19. the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
  20. Boston, MA 02110-1301, USA.
  21. */
  22. #include "b43.h"
  23. #include "lo.h"
  24. #include "phy.h"
  25. #include "main.h"
  26. #include <linux/delay.h>
  27. #include <linux/sched.h>
  28. /* Define to 1 to always calibrate all possible LO control pairs.
  29. * This is a workaround until we fix the partial LO calibration optimization. */
  30. #define B43_CALIB_ALL_LOCTLS 1
  31. /* Write the LocalOscillator Control (adjust) value-pair. */
  32. static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
  33. {
  34. struct b43_phy *phy = &dev->phy;
  35. u16 value;
  36. u16 reg;
  37. if (B43_DEBUG) {
  38. if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
  39. b43dbg(dev->wl, "Invalid LO control pair "
  40. "(I: %d, Q: %d)\n", control->i, control->q);
  41. dump_stack();
  42. return;
  43. }
  44. }
  45. value = (u8) (control->q);
  46. value |= ((u8) (control->i)) << 8;
  47. reg = (phy->type == B43_PHYTYPE_B) ? 0x002F : B43_PHY_LO_CTL;
  48. b43_phy_write(dev, reg, value);
  49. }
  50. static int assert_rfatt_and_bbatt(const struct b43_rfatt *rfatt,
  51. const struct b43_bbatt *bbatt,
  52. struct b43_wldev *dev)
  53. {
  54. int err = 0;
  55. /* Check the attenuation values against the LO control array sizes. */
  56. if (unlikely(rfatt->att >= B43_NR_RF)) {
  57. b43err(dev->wl, "rfatt(%u) >= size of LO array\n", rfatt->att);
  58. err = -EINVAL;
  59. }
  60. if (unlikely(bbatt->att >= B43_NR_BB)) {
  61. b43err(dev->wl, "bbatt(%u) >= size of LO array\n", bbatt->att);
  62. err = -EINVAL;
  63. }
  64. return err;
  65. }
  66. #if !B43_CALIB_ALL_LOCTLS
  67. static
  68. struct b43_loctl *b43_get_lo_g_ctl_nopadmix(struct b43_wldev *dev,
  69. const struct b43_rfatt *rfatt,
  70. const struct b43_bbatt *bbatt)
  71. {
  72. struct b43_phy *phy = &dev->phy;
  73. struct b43_txpower_lo_control *lo = phy->lo_control;
  74. if (assert_rfatt_and_bbatt(rfatt, bbatt, dev))
  75. return &(lo->no_padmix[0][0]); /* Just prevent a crash */
  76. return &(lo->no_padmix[bbatt->att][rfatt->att]);
  77. }
  78. #endif /* !B43_CALIB_ALL_LOCTLS */
  79. struct b43_loctl *b43_get_lo_g_ctl(struct b43_wldev *dev,
  80. const struct b43_rfatt *rfatt,
  81. const struct b43_bbatt *bbatt)
  82. {
  83. struct b43_phy *phy = &dev->phy;
  84. struct b43_txpower_lo_control *lo = phy->lo_control;
  85. if (assert_rfatt_and_bbatt(rfatt, bbatt, dev))
  86. return &(lo->no_padmix[0][0]); /* Just prevent a crash */
  87. if (rfatt->with_padmix)
  88. return &(lo->with_padmix[bbatt->att][rfatt->att]);
  89. return &(lo->no_padmix[bbatt->att][rfatt->att]);
  90. }
  91. /* Call a function for every possible LO control value-pair. */
  92. static void b43_call_for_each_loctl(struct b43_wldev *dev,
  93. void (*func) (struct b43_wldev *,
  94. struct b43_loctl *))
  95. {
  96. struct b43_phy *phy = &dev->phy;
  97. struct b43_txpower_lo_control *ctl = phy->lo_control;
  98. int i, j;
  99. for (i = 0; i < B43_NR_BB; i++) {
  100. for (j = 0; j < B43_NR_RF; j++)
  101. func(dev, &(ctl->with_padmix[i][j]));
  102. }
  103. for (i = 0; i < B43_NR_BB; i++) {
  104. for (j = 0; j < B43_NR_RF; j++)
  105. func(dev, &(ctl->no_padmix[i][j]));
  106. }
  107. }
  108. static u16 lo_b_r15_loop(struct b43_wldev *dev)
  109. {
  110. int i;
  111. u16 ret = 0;
  112. for (i = 0; i < 10; i++) {
  113. b43_phy_write(dev, 0x0015, 0xAFA0);
  114. udelay(1);
  115. b43_phy_write(dev, 0x0015, 0xEFA0);
  116. udelay(10);
  117. b43_phy_write(dev, 0x0015, 0xFFA0);
  118. udelay(40);
  119. ret += b43_phy_read(dev, 0x002C);
  120. }
  121. return ret;
  122. }
  123. void b43_lo_b_measure(struct b43_wldev *dev)
  124. {
  125. struct b43_phy *phy = &dev->phy;
  126. u16 regstack[12] = { 0 };
  127. u16 mls;
  128. u16 fval;
  129. int i, j;
  130. regstack[0] = b43_phy_read(dev, 0x0015);
  131. regstack[1] = b43_radio_read16(dev, 0x0052) & 0xFFF0;
  132. if (phy->radio_ver == 0x2053) {
  133. regstack[2] = b43_phy_read(dev, 0x000A);
  134. regstack[3] = b43_phy_read(dev, 0x002A);
  135. regstack[4] = b43_phy_read(dev, 0x0035);
  136. regstack[5] = b43_phy_read(dev, 0x0003);
  137. regstack[6] = b43_phy_read(dev, 0x0001);
  138. regstack[7] = b43_phy_read(dev, 0x0030);
  139. regstack[8] = b43_radio_read16(dev, 0x0043);
  140. regstack[9] = b43_radio_read16(dev, 0x007A);
  141. regstack[10] = b43_read16(dev, 0x03EC);
  142. regstack[11] = b43_radio_read16(dev, 0x0052) & 0x00F0;
  143. b43_phy_write(dev, 0x0030, 0x00FF);
  144. b43_write16(dev, 0x03EC, 0x3F3F);
  145. b43_phy_write(dev, 0x0035, regstack[4] & 0xFF7F);
  146. b43_radio_write16(dev, 0x007A, regstack[9] & 0xFFF0);
  147. }
  148. b43_phy_write(dev, 0x0015, 0xB000);
  149. b43_phy_write(dev, 0x002B, 0x0004);
  150. if (phy->radio_ver == 0x2053) {
  151. b43_phy_write(dev, 0x002B, 0x0203);
  152. b43_phy_write(dev, 0x002A, 0x08A3);
  153. }
  154. phy->minlowsig[0] = 0xFFFF;
  155. for (i = 0; i < 4; i++) {
  156. b43_radio_write16(dev, 0x0052, regstack[1] | i);
  157. lo_b_r15_loop(dev);
  158. }
  159. for (i = 0; i < 10; i++) {
  160. b43_radio_write16(dev, 0x0052, regstack[1] | i);
  161. mls = lo_b_r15_loop(dev) / 10;
  162. if (mls < phy->minlowsig[0]) {
  163. phy->minlowsig[0] = mls;
  164. phy->minlowsigpos[0] = i;
  165. }
  166. }
  167. b43_radio_write16(dev, 0x0052, regstack[1] | phy->minlowsigpos[0]);
  168. phy->minlowsig[1] = 0xFFFF;
  169. for (i = -4; i < 5; i += 2) {
  170. for (j = -4; j < 5; j += 2) {
  171. if (j < 0)
  172. fval = (0x0100 * i) + j + 0x0100;
  173. else
  174. fval = (0x0100 * i) + j;
  175. b43_phy_write(dev, 0x002F, fval);
  176. mls = lo_b_r15_loop(dev) / 10;
  177. if (mls < phy->minlowsig[1]) {
  178. phy->minlowsig[1] = mls;
  179. phy->minlowsigpos[1] = fval;
  180. }
  181. }
  182. }
  183. phy->minlowsigpos[1] += 0x0101;
  184. b43_phy_write(dev, 0x002F, phy->minlowsigpos[1]);
  185. if (phy->radio_ver == 0x2053) {
  186. b43_phy_write(dev, 0x000A, regstack[2]);
  187. b43_phy_write(dev, 0x002A, regstack[3]);
  188. b43_phy_write(dev, 0x0035, regstack[4]);
  189. b43_phy_write(dev, 0x0003, regstack[5]);
  190. b43_phy_write(dev, 0x0001, regstack[6]);
  191. b43_phy_write(dev, 0x0030, regstack[7]);
  192. b43_radio_write16(dev, 0x0043, regstack[8]);
  193. b43_radio_write16(dev, 0x007A, regstack[9]);
  194. b43_radio_write16(dev, 0x0052,
  195. (b43_radio_read16(dev, 0x0052) & 0x000F)
  196. | regstack[11]);
  197. b43_write16(dev, 0x03EC, regstack[10]);
  198. }
  199. b43_phy_write(dev, 0x0015, regstack[0]);
  200. }
  201. static u16 lo_measure_feedthrough(struct b43_wldev *dev,
  202. u16 lna, u16 pga, u16 trsw_rx)
  203. {
  204. struct b43_phy *phy = &dev->phy;
  205. u16 rfover;
  206. u16 feedthrough;
  207. if (phy->gmode) {
  208. lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
  209. pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
  210. B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
  211. B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
  212. /*FIXME This assertion fails B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
  213. B43_PHY_RFOVERVAL_BW));
  214. */
  215. trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
  216. /* Construct the RF Override Value */
  217. rfover = B43_PHY_RFOVERVAL_UNK;
  218. rfover |= pga;
  219. rfover |= lna;
  220. rfover |= trsw_rx;
  221. if ((dev->dev->bus->sprom.r1.boardflags_lo & B43_BFL_EXTLNA) &&
  222. phy->rev > 6)
  223. rfover |= B43_PHY_RFOVERVAL_EXTLNA;
  224. b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
  225. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  226. udelay(10);
  227. rfover |= B43_PHY_RFOVERVAL_BW_LBW;
  228. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  229. udelay(10);
  230. rfover |= B43_PHY_RFOVERVAL_BW_LPF;
  231. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  232. udelay(10);
  233. b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
  234. } else {
  235. pga |= B43_PHY_PGACTL_UNKNOWN;
  236. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  237. udelay(10);
  238. pga |= B43_PHY_PGACTL_LOWBANDW;
  239. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  240. udelay(10);
  241. pga |= B43_PHY_PGACTL_LPF;
  242. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  243. }
  244. udelay(21);
  245. feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
  246. /* This is a good place to check if we need to relax a bit,
  247. * as this is the main function called regularly
  248. * in the LO calibration. */
  249. cond_resched();
  250. return feedthrough;
  251. }
  252. /* TXCTL Register and Value Table.
  253. * Returns the "TXCTL Register".
  254. * "value" is the "TXCTL Value".
  255. * "pad_mix_gain" is the PAD Mixer Gain.
  256. */
  257. static u16 lo_txctl_register_table(struct b43_wldev *dev,
  258. u16 * value, u16 * pad_mix_gain)
  259. {
  260. struct b43_phy *phy = &dev->phy;
  261. u16 reg, v, padmix;
  262. if (phy->type == B43_PHYTYPE_B) {
  263. v = 0x30;
  264. if (phy->radio_rev <= 5) {
  265. reg = 0x43;
  266. padmix = 0;
  267. } else {
  268. reg = 0x52;
  269. padmix = 5;
  270. }
  271. } else {
  272. if (phy->rev >= 2 && phy->radio_rev == 8) {
  273. reg = 0x43;
  274. v = 0x10;
  275. padmix = 2;
  276. } else {
  277. reg = 0x52;
  278. v = 0x30;
  279. padmix = 5;
  280. }
  281. }
  282. if (value)
  283. *value = v;
  284. if (pad_mix_gain)
  285. *pad_mix_gain = padmix;
  286. return reg;
  287. }
  288. static void lo_measure_txctl_values(struct b43_wldev *dev)
  289. {
  290. struct b43_phy *phy = &dev->phy;
  291. struct b43_txpower_lo_control *lo = phy->lo_control;
  292. u16 reg, mask;
  293. u16 trsw_rx, pga;
  294. u16 radio_pctl_reg;
  295. static const u8 tx_bias_values[] = {
  296. 0x09, 0x08, 0x0A, 0x01, 0x00,
  297. 0x02, 0x05, 0x04, 0x06,
  298. };
  299. static const u8 tx_magn_values[] = {
  300. 0x70, 0x40,
  301. };
  302. if (!has_loopback_gain(phy)) {
  303. radio_pctl_reg = 6;
  304. trsw_rx = 2;
  305. pga = 0;
  306. } else {
  307. int lb_gain; /* Loopback gain (in dB) */
  308. trsw_rx = 0;
  309. lb_gain = phy->max_lb_gain / 2;
  310. if (lb_gain > 10) {
  311. radio_pctl_reg = 0;
  312. pga = abs(10 - lb_gain) / 6;
  313. pga = limit_value(pga, 0, 15);
  314. } else {
  315. int cmp_val;
  316. int tmp;
  317. pga = 0;
  318. cmp_val = 0x24;
  319. if ((phy->rev >= 2) &&
  320. (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
  321. cmp_val = 0x3C;
  322. tmp = lb_gain;
  323. if ((10 - lb_gain) < cmp_val)
  324. tmp = (10 - lb_gain);
  325. if (tmp < 0)
  326. tmp += 6;
  327. else
  328. tmp += 3;
  329. cmp_val /= 4;
  330. tmp /= 4;
  331. if (tmp >= cmp_val)
  332. radio_pctl_reg = cmp_val;
  333. else
  334. radio_pctl_reg = tmp;
  335. }
  336. }
  337. b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
  338. & 0xFFF0) | radio_pctl_reg);
  339. b43_phy_set_baseband_attenuation(dev, 2);
  340. reg = lo_txctl_register_table(dev, &mask, NULL);
  341. mask = ~mask;
  342. b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
  343. & mask);
  344. if (has_tx_magnification(phy)) {
  345. int i, j;
  346. int feedthrough;
  347. int min_feedth = 0xFFFF;
  348. u8 tx_magn, tx_bias;
  349. for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
  350. tx_magn = tx_magn_values[i];
  351. b43_radio_write16(dev, 0x52,
  352. (b43_radio_read16(dev, 0x52)
  353. & 0xFF0F) | tx_magn);
  354. for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
  355. tx_bias = tx_bias_values[j];
  356. b43_radio_write16(dev, 0x52,
  357. (b43_radio_read16(dev, 0x52)
  358. & 0xFFF0) | tx_bias);
  359. feedthrough =
  360. lo_measure_feedthrough(dev, 0, pga,
  361. trsw_rx);
  362. if (feedthrough < min_feedth) {
  363. lo->tx_bias = tx_bias;
  364. lo->tx_magn = tx_magn;
  365. min_feedth = feedthrough;
  366. }
  367. if (lo->tx_bias == 0)
  368. break;
  369. }
  370. b43_radio_write16(dev, 0x52,
  371. (b43_radio_read16(dev, 0x52)
  372. & 0xFF00) | lo->tx_bias | lo->
  373. tx_magn);
  374. }
  375. } else {
  376. lo->tx_magn = 0;
  377. lo->tx_bias = 0;
  378. b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
  379. & 0xFFF0); /* TX bias == 0 */
  380. }
  381. }
  382. static void lo_read_power_vector(struct b43_wldev *dev)
  383. {
  384. struct b43_phy *phy = &dev->phy;
  385. struct b43_txpower_lo_control *lo = phy->lo_control;
  386. u16 i;
  387. u64 tmp;
  388. u64 power_vector = 0;
  389. int rf_offset, bb_offset;
  390. struct b43_loctl *loctl;
  391. for (i = 0; i < 8; i += 2) {
  392. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
  393. /* Clear the top byte. We get holes in the bitmap... */
  394. tmp &= 0xFF;
  395. power_vector |= (tmp << (i * 8));
  396. /* Clear the vector on the device. */
  397. b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
  398. }
  399. if (power_vector)
  400. lo->power_vector = power_vector;
  401. power_vector = lo->power_vector;
  402. for (i = 0; i < 64; i++) {
  403. if (power_vector & ((u64) 1ULL << i)) {
  404. /* Now figure out which b43_loctl corresponds
  405. * to this bit.
  406. */
  407. rf_offset = i / lo->rfatt_list.len;
  408. bb_offset = i % lo->rfatt_list.len; //FIXME?
  409. loctl =
  410. b43_get_lo_g_ctl(dev,
  411. &lo->rfatt_list.list[rf_offset],
  412. &lo->bbatt_list.list[bb_offset]);
  413. /* And mark it as "used", as the device told us
  414. * through the bitmap it is using it.
  415. */
  416. loctl->used = 1;
  417. }
  418. }
  419. }
  420. /* 802.11/LO/GPHY/MeasuringGains */
  421. static void lo_measure_gain_values(struct b43_wldev *dev,
  422. s16 max_rx_gain, int use_trsw_rx)
  423. {
  424. struct b43_phy *phy = &dev->phy;
  425. u16 tmp;
  426. if (max_rx_gain < 0)
  427. max_rx_gain = 0;
  428. if (has_loopback_gain(phy)) {
  429. int trsw_rx = 0;
  430. int trsw_rx_gain;
  431. if (use_trsw_rx) {
  432. trsw_rx_gain = phy->trsw_rx_gain / 2;
  433. if (max_rx_gain >= trsw_rx_gain) {
  434. trsw_rx_gain = max_rx_gain - trsw_rx_gain;
  435. trsw_rx = 0x20;
  436. }
  437. } else
  438. trsw_rx_gain = max_rx_gain;
  439. if (trsw_rx_gain < 9) {
  440. phy->lna_lod_gain = 0;
  441. } else {
  442. phy->lna_lod_gain = 1;
  443. trsw_rx_gain -= 8;
  444. }
  445. trsw_rx_gain = limit_value(trsw_rx_gain, 0, 0x2D);
  446. phy->pga_gain = trsw_rx_gain / 3;
  447. if (phy->pga_gain >= 5) {
  448. phy->pga_gain -= 5;
  449. phy->lna_gain = 2;
  450. } else
  451. phy->lna_gain = 0;
  452. } else {
  453. phy->lna_gain = 0;
  454. phy->trsw_rx_gain = 0x20;
  455. if (max_rx_gain >= 0x14) {
  456. phy->lna_lod_gain = 1;
  457. phy->pga_gain = 2;
  458. } else if (max_rx_gain >= 0x12) {
  459. phy->lna_lod_gain = 1;
  460. phy->pga_gain = 1;
  461. } else if (max_rx_gain >= 0xF) {
  462. phy->lna_lod_gain = 1;
  463. phy->pga_gain = 0;
  464. } else {
  465. phy->lna_lod_gain = 0;
  466. phy->pga_gain = 0;
  467. }
  468. }
  469. tmp = b43_radio_read16(dev, 0x7A);
  470. if (phy->lna_lod_gain == 0)
  471. tmp &= ~0x0008;
  472. else
  473. tmp |= 0x0008;
  474. b43_radio_write16(dev, 0x7A, tmp);
  475. }
  476. struct lo_g_saved_values {
  477. u8 old_channel;
  478. /* Core registers */
  479. u16 reg_3F4;
  480. u16 reg_3E2;
  481. /* PHY registers */
  482. u16 phy_lo_mask;
  483. u16 phy_extg_01;
  484. u16 phy_dacctl_hwpctl;
  485. u16 phy_dacctl;
  486. u16 phy_base_14;
  487. u16 phy_hpwr_tssictl;
  488. u16 phy_analogover;
  489. u16 phy_analogoverval;
  490. u16 phy_rfover;
  491. u16 phy_rfoverval;
  492. u16 phy_classctl;
  493. u16 phy_base_3E;
  494. u16 phy_crs0;
  495. u16 phy_pgactl;
  496. u16 phy_base_2A;
  497. u16 phy_syncctl;
  498. u16 phy_base_30;
  499. u16 phy_base_06;
  500. /* Radio registers */
  501. u16 radio_43;
  502. u16 radio_7A;
  503. u16 radio_52;
  504. };
  505. static void lo_measure_setup(struct b43_wldev *dev,
  506. struct lo_g_saved_values *sav)
  507. {
  508. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  509. struct b43_phy *phy = &dev->phy;
  510. struct b43_txpower_lo_control *lo = phy->lo_control;
  511. u16 tmp;
  512. if (b43_has_hardware_pctl(phy)) {
  513. sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
  514. sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
  515. sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
  516. sav->phy_base_14 = b43_phy_read(dev, B43_PHY_BASE(0x14));
  517. sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
  518. b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
  519. b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
  520. | 0x100);
  521. b43_phy_write(dev, B43_PHY_EXTG(0x01),
  522. b43_phy_read(dev, B43_PHY_EXTG(0x01))
  523. | 0x40);
  524. b43_phy_write(dev, B43_PHY_DACCTL,
  525. b43_phy_read(dev, B43_PHY_DACCTL)
  526. | 0x40);
  527. b43_phy_write(dev, B43_PHY_BASE(0x14),
  528. b43_phy_read(dev, B43_PHY_BASE(0x14))
  529. | 0x200);
  530. }
  531. if (phy->type == B43_PHYTYPE_B &&
  532. phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
  533. b43_phy_write(dev, B43_PHY_BASE(0x16), 0x410);
  534. b43_phy_write(dev, B43_PHY_BASE(0x17), 0x820);
  535. }
  536. if (!lo->rebuild && b43_has_hardware_pctl(phy))
  537. lo_read_power_vector(dev);
  538. if (phy->rev >= 2) {
  539. sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
  540. sav->phy_analogoverval =
  541. b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
  542. sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
  543. sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
  544. sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
  545. sav->phy_base_3E = b43_phy_read(dev, B43_PHY_BASE(0x3E));
  546. sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
  547. b43_phy_write(dev, B43_PHY_CLASSCTL,
  548. b43_phy_read(dev, B43_PHY_CLASSCTL)
  549. & 0xFFFC);
  550. b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
  551. & 0x7FFF);
  552. b43_phy_write(dev, B43_PHY_ANALOGOVER,
  553. b43_phy_read(dev, B43_PHY_ANALOGOVER)
  554. | 0x0003);
  555. b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
  556. b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
  557. & 0xFFFC);
  558. if (phy->type == B43_PHYTYPE_G) {
  559. if ((phy->rev >= 7) &&
  560. (sprom->r1.boardflags_lo & B43_BFL_EXTLNA)) {
  561. b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
  562. } else {
  563. b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
  564. }
  565. } else {
  566. b43_phy_write(dev, B43_PHY_RFOVER, 0);
  567. }
  568. b43_phy_write(dev, B43_PHY_BASE(0x3E), 0);
  569. }
  570. sav->reg_3F4 = b43_read16(dev, 0x3F4);
  571. sav->reg_3E2 = b43_read16(dev, 0x3E2);
  572. sav->radio_43 = b43_radio_read16(dev, 0x43);
  573. sav->radio_7A = b43_radio_read16(dev, 0x7A);
  574. sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
  575. sav->phy_base_2A = b43_phy_read(dev, B43_PHY_BASE(0x2A));
  576. sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
  577. sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
  578. if (!has_tx_magnification(phy)) {
  579. sav->radio_52 = b43_radio_read16(dev, 0x52);
  580. sav->radio_52 &= 0x00F0;
  581. }
  582. if (phy->type == B43_PHYTYPE_B) {
  583. sav->phy_base_30 = b43_phy_read(dev, B43_PHY_BASE(0x30));
  584. sav->phy_base_06 = b43_phy_read(dev, B43_PHY_BASE(0x06));
  585. b43_phy_write(dev, B43_PHY_BASE(0x30), 0x00FF);
  586. b43_phy_write(dev, B43_PHY_BASE(0x06), 0x3F3F);
  587. } else {
  588. b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
  589. | 0x8000);
  590. }
  591. b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
  592. & 0xF000);
  593. tmp =
  594. (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_BASE(0x2E);
  595. b43_phy_write(dev, tmp, 0x007F);
  596. tmp = sav->phy_syncctl;
  597. b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
  598. tmp = sav->radio_7A;
  599. b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
  600. b43_phy_write(dev, B43_PHY_BASE(0x2A), 0x8A3);
  601. if (phy->type == B43_PHYTYPE_G ||
  602. (phy->type == B43_PHYTYPE_B &&
  603. phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
  604. b43_phy_write(dev, B43_PHY_BASE(0x2B), 0x1003);
  605. } else
  606. b43_phy_write(dev, B43_PHY_BASE(0x2B), 0x0802);
  607. if (phy->rev >= 2)
  608. b43_dummy_transmission(dev);
  609. b43_radio_selectchannel(dev, 6, 0);
  610. b43_radio_read16(dev, 0x51); /* dummy read */
  611. if (phy->type == B43_PHYTYPE_G)
  612. b43_phy_write(dev, B43_PHY_BASE(0x2F), 0);
  613. if (lo->rebuild)
  614. lo_measure_txctl_values(dev);
  615. if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
  616. b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
  617. } else {
  618. if (phy->type == B43_PHYTYPE_B)
  619. b43_phy_write(dev, B43_PHY_BASE(0x2E), 0x8078);
  620. else
  621. b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
  622. }
  623. }
  624. static void lo_measure_restore(struct b43_wldev *dev,
  625. struct lo_g_saved_values *sav)
  626. {
  627. struct b43_phy *phy = &dev->phy;
  628. struct b43_txpower_lo_control *lo = phy->lo_control;
  629. u16 tmp;
  630. if (phy->rev >= 2) {
  631. b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
  632. tmp = (phy->pga_gain << 8);
  633. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
  634. udelay(5);
  635. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
  636. udelay(2);
  637. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
  638. } else {
  639. tmp = (phy->pga_gain | 0xEFA0);
  640. b43_phy_write(dev, B43_PHY_PGACTL, tmp);
  641. }
  642. if (b43_has_hardware_pctl(phy)) {
  643. b43_gphy_dc_lt_init(dev);
  644. } else {
  645. if (lo->rebuild)
  646. b43_lo_g_adjust_to(dev, 3, 2, 0);
  647. else
  648. b43_lo_g_adjust(dev);
  649. }
  650. if (phy->type == B43_PHYTYPE_G) {
  651. if (phy->rev >= 3)
  652. b43_phy_write(dev, B43_PHY_BASE(0x2E), 0xC078);
  653. else
  654. b43_phy_write(dev, B43_PHY_BASE(0x2E), 0x8078);
  655. if (phy->rev >= 2)
  656. b43_phy_write(dev, B43_PHY_BASE(0x2F), 0x0202);
  657. else
  658. b43_phy_write(dev, B43_PHY_BASE(0x2F), 0x0101);
  659. }
  660. b43_write16(dev, 0x3F4, sav->reg_3F4);
  661. b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
  662. b43_phy_write(dev, B43_PHY_BASE(0x2A), sav->phy_base_2A);
  663. b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
  664. b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
  665. b43_radio_write16(dev, 0x43, sav->radio_43);
  666. b43_radio_write16(dev, 0x7A, sav->radio_7A);
  667. if (!has_tx_magnification(phy)) {
  668. tmp = sav->radio_52;
  669. b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
  670. & 0xFF0F) | tmp);
  671. }
  672. b43_write16(dev, 0x3E2, sav->reg_3E2);
  673. if (phy->type == B43_PHYTYPE_B &&
  674. phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
  675. b43_phy_write(dev, B43_PHY_BASE(0x30), sav->phy_base_30);
  676. b43_phy_write(dev, B43_PHY_BASE(0x06), sav->phy_base_06);
  677. }
  678. if (phy->rev >= 2) {
  679. b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
  680. b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
  681. sav->phy_analogoverval);
  682. b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
  683. b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
  684. b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
  685. b43_phy_write(dev, B43_PHY_BASE(0x3E), sav->phy_base_3E);
  686. b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
  687. }
  688. if (b43_has_hardware_pctl(phy)) {
  689. tmp = (sav->phy_lo_mask & 0xBFFF);
  690. b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
  691. b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
  692. b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
  693. b43_phy_write(dev, B43_PHY_BASE(0x14), sav->phy_base_14);
  694. b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
  695. }
  696. b43_radio_selectchannel(dev, sav->old_channel, 1);
  697. }
  698. struct b43_lo_g_statemachine {
  699. int current_state;
  700. int nr_measured;
  701. int state_val_multiplier;
  702. u16 lowest_feedth;
  703. struct b43_loctl min_loctl;
  704. };
  705. /* Loop over each possible value in this state. */
  706. static int lo_probe_possible_loctls(struct b43_wldev *dev,
  707. struct b43_loctl *probe_loctl,
  708. struct b43_lo_g_statemachine *d)
  709. {
  710. struct b43_phy *phy = &dev->phy;
  711. struct b43_txpower_lo_control *lo = phy->lo_control;
  712. struct b43_loctl test_loctl;
  713. struct b43_loctl orig_loctl;
  714. struct b43_loctl prev_loctl = {
  715. .i = -100,
  716. .q = -100,
  717. };
  718. int i;
  719. int begin, end;
  720. int found_lower = 0;
  721. u16 feedth;
  722. static const struct b43_loctl modifiers[] = {
  723. {.i = 1,.q = 1,},
  724. {.i = 1,.q = 0,},
  725. {.i = 1,.q = -1,},
  726. {.i = 0,.q = -1,},
  727. {.i = -1,.q = -1,},
  728. {.i = -1,.q = 0,},
  729. {.i = -1,.q = 1,},
  730. {.i = 0,.q = 1,},
  731. };
  732. if (d->current_state == 0) {
  733. begin = 1;
  734. end = 8;
  735. } else if (d->current_state % 2 == 0) {
  736. begin = d->current_state - 1;
  737. end = d->current_state + 1;
  738. } else {
  739. begin = d->current_state - 2;
  740. end = d->current_state + 2;
  741. }
  742. if (begin < 1)
  743. begin += 8;
  744. if (end > 8)
  745. end -= 8;
  746. memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
  747. i = begin;
  748. d->current_state = i;
  749. while (1) {
  750. B43_WARN_ON(!(i >= 1 && i <= 8));
  751. memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
  752. test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
  753. test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
  754. if ((test_loctl.i != prev_loctl.i ||
  755. test_loctl.q != prev_loctl.q) &&
  756. (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
  757. b43_lo_write(dev, &test_loctl);
  758. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  759. phy->pga_gain,
  760. phy->trsw_rx_gain);
  761. if (feedth < d->lowest_feedth) {
  762. memcpy(probe_loctl, &test_loctl,
  763. sizeof(struct b43_loctl));
  764. found_lower = 1;
  765. d->lowest_feedth = feedth;
  766. if ((d->nr_measured < 2) &&
  767. (!has_loopback_gain(phy) || lo->rebuild))
  768. break;
  769. }
  770. }
  771. memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
  772. if (i == end)
  773. break;
  774. if (i == 8)
  775. i = 1;
  776. else
  777. i++;
  778. d->current_state = i;
  779. }
  780. return found_lower;
  781. }
  782. static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
  783. struct b43_loctl *loctl,
  784. int *max_rx_gain)
  785. {
  786. struct b43_phy *phy = &dev->phy;
  787. struct b43_txpower_lo_control *lo = phy->lo_control;
  788. struct b43_lo_g_statemachine d;
  789. u16 feedth;
  790. int found_lower;
  791. struct b43_loctl probe_loctl;
  792. int max_repeat = 1, repeat_cnt = 0;
  793. d.nr_measured = 0;
  794. d.state_val_multiplier = 1;
  795. if (has_loopback_gain(phy) && !lo->rebuild)
  796. d.state_val_multiplier = 3;
  797. memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
  798. if (has_loopback_gain(phy) && lo->rebuild)
  799. max_repeat = 4;
  800. do {
  801. b43_lo_write(dev, &d.min_loctl);
  802. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  803. phy->pga_gain,
  804. phy->trsw_rx_gain);
  805. if (!lo->rebuild && feedth < 0x258) {
  806. if (feedth >= 0x12C)
  807. *max_rx_gain += 6;
  808. else
  809. *max_rx_gain += 3;
  810. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  811. phy->pga_gain,
  812. phy->trsw_rx_gain);
  813. }
  814. d.lowest_feedth = feedth;
  815. d.current_state = 0;
  816. do {
  817. B43_WARN_ON(!
  818. (d.current_state >= 0
  819. && d.current_state <= 8));
  820. memcpy(&probe_loctl, &d.min_loctl,
  821. sizeof(struct b43_loctl));
  822. found_lower =
  823. lo_probe_possible_loctls(dev, &probe_loctl, &d);
  824. if (!found_lower)
  825. break;
  826. if ((probe_loctl.i == d.min_loctl.i) &&
  827. (probe_loctl.q == d.min_loctl.q))
  828. break;
  829. memcpy(&d.min_loctl, &probe_loctl,
  830. sizeof(struct b43_loctl));
  831. d.nr_measured++;
  832. } while (d.nr_measured < 24);
  833. memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
  834. if (has_loopback_gain(phy)) {
  835. if (d.lowest_feedth > 0x1194)
  836. *max_rx_gain -= 6;
  837. else if (d.lowest_feedth < 0x5DC)
  838. *max_rx_gain += 3;
  839. if (repeat_cnt == 0) {
  840. if (d.lowest_feedth <= 0x5DC) {
  841. d.state_val_multiplier = 1;
  842. repeat_cnt++;
  843. } else
  844. d.state_val_multiplier = 2;
  845. } else if (repeat_cnt == 2)
  846. d.state_val_multiplier = 1;
  847. }
  848. lo_measure_gain_values(dev, *max_rx_gain,
  849. has_loopback_gain(phy));
  850. } while (++repeat_cnt < max_repeat);
  851. }
  852. #if B43_CALIB_ALL_LOCTLS
  853. static const struct b43_rfatt b43_full_rfatt_list_items[] = {
  854. { .att = 0, .with_padmix = 0, },
  855. { .att = 1, .with_padmix = 0, },
  856. { .att = 2, .with_padmix = 0, },
  857. { .att = 3, .with_padmix = 0, },
  858. { .att = 4, .with_padmix = 0, },
  859. { .att = 5, .with_padmix = 0, },
  860. { .att = 6, .with_padmix = 0, },
  861. { .att = 7, .with_padmix = 0, },
  862. { .att = 8, .with_padmix = 0, },
  863. { .att = 9, .with_padmix = 0, },
  864. { .att = 10, .with_padmix = 0, },
  865. { .att = 11, .with_padmix = 0, },
  866. { .att = 12, .with_padmix = 0, },
  867. { .att = 13, .with_padmix = 0, },
  868. { .att = 14, .with_padmix = 0, },
  869. { .att = 15, .with_padmix = 0, },
  870. { .att = 0, .with_padmix = 1, },
  871. { .att = 1, .with_padmix = 1, },
  872. { .att = 2, .with_padmix = 1, },
  873. { .att = 3, .with_padmix = 1, },
  874. { .att = 4, .with_padmix = 1, },
  875. { .att = 5, .with_padmix = 1, },
  876. { .att = 6, .with_padmix = 1, },
  877. { .att = 7, .with_padmix = 1, },
  878. { .att = 8, .with_padmix = 1, },
  879. { .att = 9, .with_padmix = 1, },
  880. { .att = 10, .with_padmix = 1, },
  881. { .att = 11, .with_padmix = 1, },
  882. { .att = 12, .with_padmix = 1, },
  883. { .att = 13, .with_padmix = 1, },
  884. { .att = 14, .with_padmix = 1, },
  885. { .att = 15, .with_padmix = 1, },
  886. };
  887. static const struct b43_rfatt_list b43_full_rfatt_list = {
  888. .list = b43_full_rfatt_list_items,
  889. .len = ARRAY_SIZE(b43_full_rfatt_list_items),
  890. };
  891. static const struct b43_bbatt b43_full_bbatt_list_items[] = {
  892. { .att = 0, },
  893. { .att = 1, },
  894. { .att = 2, },
  895. { .att = 3, },
  896. { .att = 4, },
  897. { .att = 5, },
  898. { .att = 6, },
  899. { .att = 7, },
  900. { .att = 8, },
  901. { .att = 9, },
  902. { .att = 10, },
  903. { .att = 11, },
  904. };
  905. static const struct b43_bbatt_list b43_full_bbatt_list = {
  906. .list = b43_full_bbatt_list_items,
  907. .len = ARRAY_SIZE(b43_full_bbatt_list_items),
  908. };
  909. #endif /* B43_CALIB_ALL_LOCTLS */
  910. static void lo_measure(struct b43_wldev *dev)
  911. {
  912. struct b43_phy *phy = &dev->phy;
  913. struct b43_txpower_lo_control *lo = phy->lo_control;
  914. struct b43_loctl loctl = {
  915. .i = 0,
  916. .q = 0,
  917. };
  918. struct b43_loctl *ploctl;
  919. int max_rx_gain;
  920. int rfidx, bbidx;
  921. const struct b43_bbatt_list *bbatt_list;
  922. const struct b43_rfatt_list *rfatt_list;
  923. /* Values from the "TXCTL Register and Value Table" */
  924. u16 txctl_reg;
  925. u16 txctl_value;
  926. u16 pad_mix_gain;
  927. bbatt_list = &lo->bbatt_list;
  928. rfatt_list = &lo->rfatt_list;
  929. #if B43_CALIB_ALL_LOCTLS
  930. bbatt_list = &b43_full_bbatt_list;
  931. rfatt_list = &b43_full_rfatt_list;
  932. #endif
  933. txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
  934. for (rfidx = 0; rfidx < rfatt_list->len; rfidx++) {
  935. b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
  936. & 0xFFF0) |
  937. rfatt_list->list[rfidx].att);
  938. b43_radio_write16(dev, txctl_reg,
  939. (b43_radio_read16(dev, txctl_reg)
  940. & ~txctl_value)
  941. | (rfatt_list->list[rfidx].with_padmix ?
  942. txctl_value : 0));
  943. for (bbidx = 0; bbidx < bbatt_list->len; bbidx++) {
  944. if (lo->rebuild) {
  945. #if B43_CALIB_ALL_LOCTLS
  946. ploctl = b43_get_lo_g_ctl(dev,
  947. &rfatt_list->list[rfidx],
  948. &bbatt_list->list[bbidx]);
  949. #else
  950. ploctl = b43_get_lo_g_ctl_nopadmix(dev,
  951. &rfatt_list->
  952. list[rfidx],
  953. &bbatt_list->
  954. list[bbidx]);
  955. #endif
  956. } else {
  957. ploctl = b43_get_lo_g_ctl(dev,
  958. &rfatt_list->list[rfidx],
  959. &bbatt_list->list[bbidx]);
  960. if (!ploctl->used)
  961. continue;
  962. }
  963. memcpy(&loctl, ploctl, sizeof(loctl));
  964. loctl.i = 0;
  965. loctl.q = 0;
  966. max_rx_gain = rfatt_list->list[rfidx].att * 2;
  967. max_rx_gain += bbatt_list->list[bbidx].att / 2;
  968. if (rfatt_list->list[rfidx].with_padmix)
  969. max_rx_gain -= pad_mix_gain;
  970. if (has_loopback_gain(phy))
  971. max_rx_gain += phy->max_lb_gain;
  972. lo_measure_gain_values(dev, max_rx_gain,
  973. has_loopback_gain(phy));
  974. b43_phy_set_baseband_attenuation(dev,
  975. bbatt_list->list[bbidx].att);
  976. lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
  977. if (phy->type == B43_PHYTYPE_B) {
  978. loctl.i++;
  979. loctl.q++;
  980. }
  981. b43_loctl_set_calibrated(&loctl, 1);
  982. memcpy(ploctl, &loctl, sizeof(loctl));
  983. }
  984. }
  985. }
  986. #if B43_DEBUG
  987. static void do_validate_loctl(struct b43_wldev *dev, struct b43_loctl *control)
  988. {
  989. const int is_initializing = (b43_status(dev) == B43_STAT_UNINIT);
  990. int i = control->i;
  991. int q = control->q;
  992. if (b43_loctl_is_calibrated(control)) {
  993. if ((abs(i) > 16) || (abs(q) > 16))
  994. goto error;
  995. } else {
  996. if (control->used)
  997. goto error;
  998. if (dev->phy.lo_control->rebuild) {
  999. control->i = 0;
  1000. control->q = 0;
  1001. if ((i != B43_LOCTL_POISON) ||
  1002. (q != B43_LOCTL_POISON))
  1003. goto error;
  1004. }
  1005. }
  1006. if (is_initializing && control->used)
  1007. goto error;
  1008. return;
  1009. error:
  1010. b43err(dev->wl, "LO control pair validation failed "
  1011. "(I: %d, Q: %d, used %u, calib: %u, initing: %d)\n",
  1012. i, q, control->used,
  1013. b43_loctl_is_calibrated(control),
  1014. is_initializing);
  1015. }
  1016. static void validate_all_loctls(struct b43_wldev *dev)
  1017. {
  1018. b43_call_for_each_loctl(dev, do_validate_loctl);
  1019. }
  1020. static void do_reset_calib(struct b43_wldev *dev, struct b43_loctl *control)
  1021. {
  1022. if (dev->phy.lo_control->rebuild ||
  1023. control->used) {
  1024. b43_loctl_set_calibrated(control, 0);
  1025. control->i = B43_LOCTL_POISON;
  1026. control->q = B43_LOCTL_POISON;
  1027. }
  1028. }
  1029. static void reset_all_loctl_calibration_states(struct b43_wldev *dev)
  1030. {
  1031. b43_call_for_each_loctl(dev, do_reset_calib);
  1032. }
  1033. #else /* B43_DEBUG */
  1034. static inline void validate_all_loctls(struct b43_wldev *dev) { }
  1035. static inline void reset_all_loctl_calibration_states(struct b43_wldev *dev) { }
  1036. #endif /* B43_DEBUG */
  1037. void b43_lo_g_measure(struct b43_wldev *dev)
  1038. {
  1039. struct b43_phy *phy = &dev->phy;
  1040. struct lo_g_saved_values uninitialized_var(sav);
  1041. B43_WARN_ON((phy->type != B43_PHYTYPE_B) &&
  1042. (phy->type != B43_PHYTYPE_G));
  1043. sav.old_channel = phy->channel;
  1044. lo_measure_setup(dev, &sav);
  1045. reset_all_loctl_calibration_states(dev);
  1046. lo_measure(dev);
  1047. lo_measure_restore(dev, &sav);
  1048. validate_all_loctls(dev);
  1049. phy->lo_control->lo_measured = 1;
  1050. phy->lo_control->rebuild = 0;
  1051. }
  1052. #if B43_DEBUG
  1053. static void validate_loctl_calibration(struct b43_wldev *dev,
  1054. struct b43_loctl *loctl,
  1055. struct b43_rfatt *rfatt,
  1056. struct b43_bbatt *bbatt)
  1057. {
  1058. if (b43_loctl_is_calibrated(loctl))
  1059. return;
  1060. if (!dev->phy.lo_control->lo_measured) {
  1061. /* On init we set the attenuation values before we
  1062. * calibrated the LO. I guess that's OK. */
  1063. return;
  1064. }
  1065. b43err(dev->wl, "Adjusting Local Oscillator to an uncalibrated "
  1066. "control pair: rfatt=%u,%spadmix bbatt=%u\n",
  1067. rfatt->att,
  1068. (rfatt->with_padmix) ? "" : "no-",
  1069. bbatt->att);
  1070. }
  1071. #else
  1072. static inline void validate_loctl_calibration(struct b43_wldev *dev,
  1073. struct b43_loctl *loctl,
  1074. struct b43_rfatt *rfatt,
  1075. struct b43_bbatt *bbatt)
  1076. {
  1077. }
  1078. #endif
  1079. static inline void fixup_rfatt_for_txcontrol(struct b43_rfatt *rf,
  1080. u8 tx_control)
  1081. {
  1082. if (tx_control & B43_TXCTL_TXMIX) {
  1083. if (rf->att < 5)
  1084. rf->att = 4;
  1085. }
  1086. }
  1087. void b43_lo_g_adjust(struct b43_wldev *dev)
  1088. {
  1089. struct b43_phy *phy = &dev->phy;
  1090. struct b43_rfatt rf;
  1091. struct b43_loctl *loctl;
  1092. memcpy(&rf, &phy->rfatt, sizeof(rf));
  1093. fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
  1094. loctl = b43_get_lo_g_ctl(dev, &rf, &phy->bbatt);
  1095. validate_loctl_calibration(dev, loctl, &rf, &phy->bbatt);
  1096. b43_lo_write(dev, loctl);
  1097. }
  1098. void b43_lo_g_adjust_to(struct b43_wldev *dev,
  1099. u16 rfatt, u16 bbatt, u16 tx_control)
  1100. {
  1101. struct b43_rfatt rf;
  1102. struct b43_bbatt bb;
  1103. struct b43_loctl *loctl;
  1104. memset(&rf, 0, sizeof(rf));
  1105. memset(&bb, 0, sizeof(bb));
  1106. rf.att = rfatt;
  1107. bb.att = bbatt;
  1108. fixup_rfatt_for_txcontrol(&rf, tx_control);
  1109. loctl = b43_get_lo_g_ctl(dev, &rf, &bb);
  1110. validate_loctl_calibration(dev, loctl, &rf, &bb);
  1111. b43_lo_write(dev, loctl);
  1112. }
  1113. static void do_mark_unused(struct b43_wldev *dev, struct b43_loctl *control)
  1114. {
  1115. control->used = 0;
  1116. }
  1117. void b43_lo_g_ctl_mark_all_unused(struct b43_wldev *dev)
  1118. {
  1119. struct b43_phy *phy = &dev->phy;
  1120. struct b43_txpower_lo_control *lo = phy->lo_control;
  1121. b43_call_for_each_loctl(dev, do_mark_unused);
  1122. lo->rebuild = 1;
  1123. }
  1124. void b43_lo_g_ctl_mark_cur_used(struct b43_wldev *dev)
  1125. {
  1126. struct b43_phy *phy = &dev->phy;
  1127. struct b43_rfatt rf;
  1128. memcpy(&rf, &phy->rfatt, sizeof(rf));
  1129. fixup_rfatt_for_txcontrol(&rf, phy->tx_control);
  1130. b43_get_lo_g_ctl(dev, &rf, &phy->bbatt)->used = 1;
  1131. }