lo.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036
  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 <stefano.brivio@polimi.it>
  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. static struct b43_lo_calib * b43_find_lo_calib(struct b43_txpower_lo_control *lo,
  29. const struct b43_bbatt *bbatt,
  30. const struct b43_rfatt *rfatt)
  31. {
  32. struct b43_lo_calib *c;
  33. list_for_each_entry(c, &lo->calib_list, list) {
  34. if (!b43_compare_bbatt(&c->bbatt, bbatt))
  35. continue;
  36. if (!b43_compare_rfatt(&c->rfatt, rfatt))
  37. continue;
  38. return c;
  39. }
  40. return NULL;
  41. }
  42. /* Write the LocalOscillator Control (adjust) value-pair. */
  43. static void b43_lo_write(struct b43_wldev *dev, struct b43_loctl *control)
  44. {
  45. struct b43_phy *phy = &dev->phy;
  46. u16 value;
  47. if (B43_DEBUG) {
  48. if (unlikely(abs(control->i) > 16 || abs(control->q) > 16)) {
  49. b43dbg(dev->wl, "Invalid LO control pair "
  50. "(I: %d, Q: %d)\n", control->i, control->q);
  51. dump_stack();
  52. return;
  53. }
  54. }
  55. B43_WARN_ON(phy->type != B43_PHYTYPE_G);
  56. value = (u8) (control->q);
  57. value |= ((u8) (control->i)) << 8;
  58. b43_phy_write(dev, B43_PHY_LO_CTL, value);
  59. }
  60. static u16 lo_measure_feedthrough(struct b43_wldev *dev,
  61. u16 lna, u16 pga, u16 trsw_rx)
  62. {
  63. struct b43_phy *phy = &dev->phy;
  64. u16 rfover;
  65. u16 feedthrough;
  66. if (phy->gmode) {
  67. lna <<= B43_PHY_RFOVERVAL_LNA_SHIFT;
  68. pga <<= B43_PHY_RFOVERVAL_PGA_SHIFT;
  69. B43_WARN_ON(lna & ~B43_PHY_RFOVERVAL_LNA);
  70. B43_WARN_ON(pga & ~B43_PHY_RFOVERVAL_PGA);
  71. /*FIXME This assertion fails B43_WARN_ON(trsw_rx & ~(B43_PHY_RFOVERVAL_TRSWRX |
  72. B43_PHY_RFOVERVAL_BW));
  73. */
  74. trsw_rx &= (B43_PHY_RFOVERVAL_TRSWRX | B43_PHY_RFOVERVAL_BW);
  75. /* Construct the RF Override Value */
  76. rfover = B43_PHY_RFOVERVAL_UNK;
  77. rfover |= pga;
  78. rfover |= lna;
  79. rfover |= trsw_rx;
  80. if ((dev->dev->bus->sprom.boardflags_lo & B43_BFL_EXTLNA)
  81. && phy->rev > 6)
  82. rfover |= B43_PHY_RFOVERVAL_EXTLNA;
  83. b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
  84. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  85. udelay(10);
  86. rfover |= B43_PHY_RFOVERVAL_BW_LBW;
  87. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  88. udelay(10);
  89. rfover |= B43_PHY_RFOVERVAL_BW_LPF;
  90. b43_phy_write(dev, B43_PHY_RFOVERVAL, rfover);
  91. udelay(10);
  92. b43_phy_write(dev, B43_PHY_PGACTL, 0xF300);
  93. } else {
  94. pga |= B43_PHY_PGACTL_UNKNOWN;
  95. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  96. udelay(10);
  97. pga |= B43_PHY_PGACTL_LOWBANDW;
  98. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  99. udelay(10);
  100. pga |= B43_PHY_PGACTL_LPF;
  101. b43_phy_write(dev, B43_PHY_PGACTL, pga);
  102. }
  103. udelay(21);
  104. feedthrough = b43_phy_read(dev, B43_PHY_LO_LEAKAGE);
  105. /* This is a good place to check if we need to relax a bit,
  106. * as this is the main function called regularly
  107. * in the LO calibration. */
  108. cond_resched();
  109. return feedthrough;
  110. }
  111. /* TXCTL Register and Value Table.
  112. * Returns the "TXCTL Register".
  113. * "value" is the "TXCTL Value".
  114. * "pad_mix_gain" is the PAD Mixer Gain.
  115. */
  116. static u16 lo_txctl_register_table(struct b43_wldev *dev,
  117. u16 * value, u16 * pad_mix_gain)
  118. {
  119. struct b43_phy *phy = &dev->phy;
  120. u16 reg, v, padmix;
  121. if (phy->type == B43_PHYTYPE_B) {
  122. v = 0x30;
  123. if (phy->radio_rev <= 5) {
  124. reg = 0x43;
  125. padmix = 0;
  126. } else {
  127. reg = 0x52;
  128. padmix = 5;
  129. }
  130. } else {
  131. if (phy->rev >= 2 && phy->radio_rev == 8) {
  132. reg = 0x43;
  133. v = 0x10;
  134. padmix = 2;
  135. } else {
  136. reg = 0x52;
  137. v = 0x30;
  138. padmix = 5;
  139. }
  140. }
  141. if (value)
  142. *value = v;
  143. if (pad_mix_gain)
  144. *pad_mix_gain = padmix;
  145. return reg;
  146. }
  147. static void lo_measure_txctl_values(struct b43_wldev *dev)
  148. {
  149. struct b43_phy *phy = &dev->phy;
  150. struct b43_txpower_lo_control *lo = phy->lo_control;
  151. u16 reg, mask;
  152. u16 trsw_rx, pga;
  153. u16 radio_pctl_reg;
  154. static const u8 tx_bias_values[] = {
  155. 0x09, 0x08, 0x0A, 0x01, 0x00,
  156. 0x02, 0x05, 0x04, 0x06,
  157. };
  158. static const u8 tx_magn_values[] = {
  159. 0x70, 0x40,
  160. };
  161. if (!has_loopback_gain(phy)) {
  162. radio_pctl_reg = 6;
  163. trsw_rx = 2;
  164. pga = 0;
  165. } else {
  166. int lb_gain; /* Loopback gain (in dB) */
  167. trsw_rx = 0;
  168. lb_gain = phy->max_lb_gain / 2;
  169. if (lb_gain > 10) {
  170. radio_pctl_reg = 0;
  171. pga = abs(10 - lb_gain) / 6;
  172. pga = clamp_val(pga, 0, 15);
  173. } else {
  174. int cmp_val;
  175. int tmp;
  176. pga = 0;
  177. cmp_val = 0x24;
  178. if ((phy->rev >= 2) &&
  179. (phy->radio_ver == 0x2050) && (phy->radio_rev == 8))
  180. cmp_val = 0x3C;
  181. tmp = lb_gain;
  182. if ((10 - lb_gain) < cmp_val)
  183. tmp = (10 - lb_gain);
  184. if (tmp < 0)
  185. tmp += 6;
  186. else
  187. tmp += 3;
  188. cmp_val /= 4;
  189. tmp /= 4;
  190. if (tmp >= cmp_val)
  191. radio_pctl_reg = cmp_val;
  192. else
  193. radio_pctl_reg = tmp;
  194. }
  195. }
  196. b43_radio_write16(dev, 0x43, (b43_radio_read16(dev, 0x43)
  197. & 0xFFF0) | radio_pctl_reg);
  198. b43_phy_set_baseband_attenuation(dev, 2);
  199. reg = lo_txctl_register_table(dev, &mask, NULL);
  200. mask = ~mask;
  201. b43_radio_write16(dev, reg, b43_radio_read16(dev, reg)
  202. & mask);
  203. if (has_tx_magnification(phy)) {
  204. int i, j;
  205. int feedthrough;
  206. int min_feedth = 0xFFFF;
  207. u8 tx_magn, tx_bias;
  208. for (i = 0; i < ARRAY_SIZE(tx_magn_values); i++) {
  209. tx_magn = tx_magn_values[i];
  210. b43_radio_write16(dev, 0x52,
  211. (b43_radio_read16(dev, 0x52)
  212. & 0xFF0F) | tx_magn);
  213. for (j = 0; j < ARRAY_SIZE(tx_bias_values); j++) {
  214. tx_bias = tx_bias_values[j];
  215. b43_radio_write16(dev, 0x52,
  216. (b43_radio_read16(dev, 0x52)
  217. & 0xFFF0) | tx_bias);
  218. feedthrough =
  219. lo_measure_feedthrough(dev, 0, pga,
  220. trsw_rx);
  221. if (feedthrough < min_feedth) {
  222. lo->tx_bias = tx_bias;
  223. lo->tx_magn = tx_magn;
  224. min_feedth = feedthrough;
  225. }
  226. if (lo->tx_bias == 0)
  227. break;
  228. }
  229. b43_radio_write16(dev, 0x52,
  230. (b43_radio_read16(dev, 0x52)
  231. & 0xFF00) | lo->tx_bias | lo->
  232. tx_magn);
  233. }
  234. } else {
  235. lo->tx_magn = 0;
  236. lo->tx_bias = 0;
  237. b43_radio_write16(dev, 0x52, b43_radio_read16(dev, 0x52)
  238. & 0xFFF0); /* TX bias == 0 */
  239. }
  240. lo->txctl_measured_time = jiffies;
  241. }
  242. static void lo_read_power_vector(struct b43_wldev *dev)
  243. {
  244. struct b43_phy *phy = &dev->phy;
  245. struct b43_txpower_lo_control *lo = phy->lo_control;
  246. int i;
  247. u64 tmp;
  248. u64 power_vector = 0;
  249. for (i = 0; i < 8; i += 2) {
  250. tmp = b43_shm_read16(dev, B43_SHM_SHARED, 0x310 + i);
  251. power_vector |= (tmp << (i * 8));
  252. /* Clear the vector on the device. */
  253. b43_shm_write16(dev, B43_SHM_SHARED, 0x310 + i, 0);
  254. }
  255. if (power_vector)
  256. lo->power_vector = power_vector;
  257. lo->pwr_vec_read_time = jiffies;
  258. }
  259. /* 802.11/LO/GPHY/MeasuringGains */
  260. static void lo_measure_gain_values(struct b43_wldev *dev,
  261. s16 max_rx_gain, int use_trsw_rx)
  262. {
  263. struct b43_phy *phy = &dev->phy;
  264. u16 tmp;
  265. if (max_rx_gain < 0)
  266. max_rx_gain = 0;
  267. if (has_loopback_gain(phy)) {
  268. int trsw_rx = 0;
  269. int trsw_rx_gain;
  270. if (use_trsw_rx) {
  271. trsw_rx_gain = phy->trsw_rx_gain / 2;
  272. if (max_rx_gain >= trsw_rx_gain) {
  273. trsw_rx_gain = max_rx_gain - trsw_rx_gain;
  274. trsw_rx = 0x20;
  275. }
  276. } else
  277. trsw_rx_gain = max_rx_gain;
  278. if (trsw_rx_gain < 9) {
  279. phy->lna_lod_gain = 0;
  280. } else {
  281. phy->lna_lod_gain = 1;
  282. trsw_rx_gain -= 8;
  283. }
  284. trsw_rx_gain = clamp_val(trsw_rx_gain, 0, 0x2D);
  285. phy->pga_gain = trsw_rx_gain / 3;
  286. if (phy->pga_gain >= 5) {
  287. phy->pga_gain -= 5;
  288. phy->lna_gain = 2;
  289. } else
  290. phy->lna_gain = 0;
  291. } else {
  292. phy->lna_gain = 0;
  293. phy->trsw_rx_gain = 0x20;
  294. if (max_rx_gain >= 0x14) {
  295. phy->lna_lod_gain = 1;
  296. phy->pga_gain = 2;
  297. } else if (max_rx_gain >= 0x12) {
  298. phy->lna_lod_gain = 1;
  299. phy->pga_gain = 1;
  300. } else if (max_rx_gain >= 0xF) {
  301. phy->lna_lod_gain = 1;
  302. phy->pga_gain = 0;
  303. } else {
  304. phy->lna_lod_gain = 0;
  305. phy->pga_gain = 0;
  306. }
  307. }
  308. tmp = b43_radio_read16(dev, 0x7A);
  309. if (phy->lna_lod_gain == 0)
  310. tmp &= ~0x0008;
  311. else
  312. tmp |= 0x0008;
  313. b43_radio_write16(dev, 0x7A, tmp);
  314. }
  315. struct lo_g_saved_values {
  316. u8 old_channel;
  317. /* Core registers */
  318. u16 reg_3F4;
  319. u16 reg_3E2;
  320. /* PHY registers */
  321. u16 phy_lo_mask;
  322. u16 phy_extg_01;
  323. u16 phy_dacctl_hwpctl;
  324. u16 phy_dacctl;
  325. u16 phy_cck_14;
  326. u16 phy_hpwr_tssictl;
  327. u16 phy_analogover;
  328. u16 phy_analogoverval;
  329. u16 phy_rfover;
  330. u16 phy_rfoverval;
  331. u16 phy_classctl;
  332. u16 phy_cck_3E;
  333. u16 phy_crs0;
  334. u16 phy_pgactl;
  335. u16 phy_cck_2A;
  336. u16 phy_syncctl;
  337. u16 phy_cck_30;
  338. u16 phy_cck_06;
  339. /* Radio registers */
  340. u16 radio_43;
  341. u16 radio_7A;
  342. u16 radio_52;
  343. };
  344. static void lo_measure_setup(struct b43_wldev *dev,
  345. struct lo_g_saved_values *sav)
  346. {
  347. struct ssb_sprom *sprom = &dev->dev->bus->sprom;
  348. struct b43_phy *phy = &dev->phy;
  349. struct b43_txpower_lo_control *lo = phy->lo_control;
  350. u16 tmp;
  351. if (b43_has_hardware_pctl(phy)) {
  352. sav->phy_lo_mask = b43_phy_read(dev, B43_PHY_LO_MASK);
  353. sav->phy_extg_01 = b43_phy_read(dev, B43_PHY_EXTG(0x01));
  354. sav->phy_dacctl_hwpctl = b43_phy_read(dev, B43_PHY_DACCTL);
  355. sav->phy_cck_14 = b43_phy_read(dev, B43_PHY_CCK(0x14));
  356. sav->phy_hpwr_tssictl = b43_phy_read(dev, B43_PHY_HPWR_TSSICTL);
  357. b43_phy_write(dev, B43_PHY_HPWR_TSSICTL,
  358. b43_phy_read(dev, B43_PHY_HPWR_TSSICTL)
  359. | 0x100);
  360. b43_phy_write(dev, B43_PHY_EXTG(0x01),
  361. b43_phy_read(dev, B43_PHY_EXTG(0x01))
  362. | 0x40);
  363. b43_phy_write(dev, B43_PHY_DACCTL,
  364. b43_phy_read(dev, B43_PHY_DACCTL)
  365. | 0x40);
  366. b43_phy_write(dev, B43_PHY_CCK(0x14),
  367. b43_phy_read(dev, B43_PHY_CCK(0x14))
  368. | 0x200);
  369. }
  370. if (phy->type == B43_PHYTYPE_B &&
  371. phy->radio_ver == 0x2050 && phy->radio_rev < 6) {
  372. b43_phy_write(dev, B43_PHY_CCK(0x16), 0x410);
  373. b43_phy_write(dev, B43_PHY_CCK(0x17), 0x820);
  374. }
  375. if (phy->rev >= 2) {
  376. sav->phy_analogover = b43_phy_read(dev, B43_PHY_ANALOGOVER);
  377. sav->phy_analogoverval =
  378. b43_phy_read(dev, B43_PHY_ANALOGOVERVAL);
  379. sav->phy_rfover = b43_phy_read(dev, B43_PHY_RFOVER);
  380. sav->phy_rfoverval = b43_phy_read(dev, B43_PHY_RFOVERVAL);
  381. sav->phy_classctl = b43_phy_read(dev, B43_PHY_CLASSCTL);
  382. sav->phy_cck_3E = b43_phy_read(dev, B43_PHY_CCK(0x3E));
  383. sav->phy_crs0 = b43_phy_read(dev, B43_PHY_CRS0);
  384. b43_phy_write(dev, B43_PHY_CLASSCTL,
  385. b43_phy_read(dev, B43_PHY_CLASSCTL)
  386. & 0xFFFC);
  387. b43_phy_write(dev, B43_PHY_CRS0, b43_phy_read(dev, B43_PHY_CRS0)
  388. & 0x7FFF);
  389. b43_phy_write(dev, B43_PHY_ANALOGOVER,
  390. b43_phy_read(dev, B43_PHY_ANALOGOVER)
  391. | 0x0003);
  392. b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
  393. b43_phy_read(dev, B43_PHY_ANALOGOVERVAL)
  394. & 0xFFFC);
  395. if (phy->type == B43_PHYTYPE_G) {
  396. if ((phy->rev >= 7) &&
  397. (sprom->boardflags_lo & B43_BFL_EXTLNA)) {
  398. b43_phy_write(dev, B43_PHY_RFOVER, 0x933);
  399. } else {
  400. b43_phy_write(dev, B43_PHY_RFOVER, 0x133);
  401. }
  402. } else {
  403. b43_phy_write(dev, B43_PHY_RFOVER, 0);
  404. }
  405. b43_phy_write(dev, B43_PHY_CCK(0x3E), 0);
  406. }
  407. sav->reg_3F4 = b43_read16(dev, 0x3F4);
  408. sav->reg_3E2 = b43_read16(dev, 0x3E2);
  409. sav->radio_43 = b43_radio_read16(dev, 0x43);
  410. sav->radio_7A = b43_radio_read16(dev, 0x7A);
  411. sav->phy_pgactl = b43_phy_read(dev, B43_PHY_PGACTL);
  412. sav->phy_cck_2A = b43_phy_read(dev, B43_PHY_CCK(0x2A));
  413. sav->phy_syncctl = b43_phy_read(dev, B43_PHY_SYNCCTL);
  414. sav->phy_dacctl = b43_phy_read(dev, B43_PHY_DACCTL);
  415. if (!has_tx_magnification(phy)) {
  416. sav->radio_52 = b43_radio_read16(dev, 0x52);
  417. sav->radio_52 &= 0x00F0;
  418. }
  419. if (phy->type == B43_PHYTYPE_B) {
  420. sav->phy_cck_30 = b43_phy_read(dev, B43_PHY_CCK(0x30));
  421. sav->phy_cck_06 = b43_phy_read(dev, B43_PHY_CCK(0x06));
  422. b43_phy_write(dev, B43_PHY_CCK(0x30), 0x00FF);
  423. b43_phy_write(dev, B43_PHY_CCK(0x06), 0x3F3F);
  424. } else {
  425. b43_write16(dev, 0x3E2, b43_read16(dev, 0x3E2)
  426. | 0x8000);
  427. }
  428. b43_write16(dev, 0x3F4, b43_read16(dev, 0x3F4)
  429. & 0xF000);
  430. tmp =
  431. (phy->type == B43_PHYTYPE_G) ? B43_PHY_LO_MASK : B43_PHY_CCK(0x2E);
  432. b43_phy_write(dev, tmp, 0x007F);
  433. tmp = sav->phy_syncctl;
  434. b43_phy_write(dev, B43_PHY_SYNCCTL, tmp & 0xFF7F);
  435. tmp = sav->radio_7A;
  436. b43_radio_write16(dev, 0x007A, tmp & 0xFFF0);
  437. b43_phy_write(dev, B43_PHY_CCK(0x2A), 0x8A3);
  438. if (phy->type == B43_PHYTYPE_G ||
  439. (phy->type == B43_PHYTYPE_B &&
  440. phy->radio_ver == 0x2050 && phy->radio_rev >= 6)) {
  441. b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x1003);
  442. } else
  443. b43_phy_write(dev, B43_PHY_CCK(0x2B), 0x0802);
  444. if (phy->rev >= 2)
  445. b43_dummy_transmission(dev);
  446. b43_radio_selectchannel(dev, 6, 0);
  447. b43_radio_read16(dev, 0x51); /* dummy read */
  448. if (phy->type == B43_PHYTYPE_G)
  449. b43_phy_write(dev, B43_PHY_CCK(0x2F), 0);
  450. /* Re-measure the txctl values, if needed. */
  451. if (time_before(lo->txctl_measured_time,
  452. jiffies - B43_LO_TXCTL_EXPIRE))
  453. lo_measure_txctl_values(dev);
  454. if (phy->type == B43_PHYTYPE_G && phy->rev >= 3) {
  455. b43_phy_write(dev, B43_PHY_LO_MASK, 0xC078);
  456. } else {
  457. if (phy->type == B43_PHYTYPE_B)
  458. b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
  459. else
  460. b43_phy_write(dev, B43_PHY_LO_MASK, 0x8078);
  461. }
  462. }
  463. static void lo_measure_restore(struct b43_wldev *dev,
  464. struct lo_g_saved_values *sav)
  465. {
  466. struct b43_phy *phy = &dev->phy;
  467. u16 tmp;
  468. if (phy->rev >= 2) {
  469. b43_phy_write(dev, B43_PHY_PGACTL, 0xE300);
  470. tmp = (phy->pga_gain << 8);
  471. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA0);
  472. udelay(5);
  473. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA2);
  474. udelay(2);
  475. b43_phy_write(dev, B43_PHY_RFOVERVAL, tmp | 0xA3);
  476. } else {
  477. tmp = (phy->pga_gain | 0xEFA0);
  478. b43_phy_write(dev, B43_PHY_PGACTL, tmp);
  479. }
  480. if (phy->type == B43_PHYTYPE_G) {
  481. if (phy->rev >= 3)
  482. b43_phy_write(dev, B43_PHY_CCK(0x2E), 0xC078);
  483. else
  484. b43_phy_write(dev, B43_PHY_CCK(0x2E), 0x8078);
  485. if (phy->rev >= 2)
  486. b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0202);
  487. else
  488. b43_phy_write(dev, B43_PHY_CCK(0x2F), 0x0101);
  489. }
  490. b43_write16(dev, 0x3F4, sav->reg_3F4);
  491. b43_phy_write(dev, B43_PHY_PGACTL, sav->phy_pgactl);
  492. b43_phy_write(dev, B43_PHY_CCK(0x2A), sav->phy_cck_2A);
  493. b43_phy_write(dev, B43_PHY_SYNCCTL, sav->phy_syncctl);
  494. b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl);
  495. b43_radio_write16(dev, 0x43, sav->radio_43);
  496. b43_radio_write16(dev, 0x7A, sav->radio_7A);
  497. if (!has_tx_magnification(phy)) {
  498. tmp = sav->radio_52;
  499. b43_radio_write16(dev, 0x52, (b43_radio_read16(dev, 0x52)
  500. & 0xFF0F) | tmp);
  501. }
  502. b43_write16(dev, 0x3E2, sav->reg_3E2);
  503. if (phy->type == B43_PHYTYPE_B &&
  504. phy->radio_ver == 0x2050 && phy->radio_rev <= 5) {
  505. b43_phy_write(dev, B43_PHY_CCK(0x30), sav->phy_cck_30);
  506. b43_phy_write(dev, B43_PHY_CCK(0x06), sav->phy_cck_06);
  507. }
  508. if (phy->rev >= 2) {
  509. b43_phy_write(dev, B43_PHY_ANALOGOVER, sav->phy_analogover);
  510. b43_phy_write(dev, B43_PHY_ANALOGOVERVAL,
  511. sav->phy_analogoverval);
  512. b43_phy_write(dev, B43_PHY_CLASSCTL, sav->phy_classctl);
  513. b43_phy_write(dev, B43_PHY_RFOVER, sav->phy_rfover);
  514. b43_phy_write(dev, B43_PHY_RFOVERVAL, sav->phy_rfoverval);
  515. b43_phy_write(dev, B43_PHY_CCK(0x3E), sav->phy_cck_3E);
  516. b43_phy_write(dev, B43_PHY_CRS0, sav->phy_crs0);
  517. }
  518. if (b43_has_hardware_pctl(phy)) {
  519. tmp = (sav->phy_lo_mask & 0xBFFF);
  520. b43_phy_write(dev, B43_PHY_LO_MASK, tmp);
  521. b43_phy_write(dev, B43_PHY_EXTG(0x01), sav->phy_extg_01);
  522. b43_phy_write(dev, B43_PHY_DACCTL, sav->phy_dacctl_hwpctl);
  523. b43_phy_write(dev, B43_PHY_CCK(0x14), sav->phy_cck_14);
  524. b43_phy_write(dev, B43_PHY_HPWR_TSSICTL, sav->phy_hpwr_tssictl);
  525. }
  526. b43_radio_selectchannel(dev, sav->old_channel, 1);
  527. }
  528. struct b43_lo_g_statemachine {
  529. int current_state;
  530. int nr_measured;
  531. int state_val_multiplier;
  532. u16 lowest_feedth;
  533. struct b43_loctl min_loctl;
  534. };
  535. /* Loop over each possible value in this state. */
  536. static int lo_probe_possible_loctls(struct b43_wldev *dev,
  537. struct b43_loctl *probe_loctl,
  538. struct b43_lo_g_statemachine *d)
  539. {
  540. struct b43_phy *phy = &dev->phy;
  541. struct b43_loctl test_loctl;
  542. struct b43_loctl orig_loctl;
  543. struct b43_loctl prev_loctl = {
  544. .i = -100,
  545. .q = -100,
  546. };
  547. int i;
  548. int begin, end;
  549. int found_lower = 0;
  550. u16 feedth;
  551. static const struct b43_loctl modifiers[] = {
  552. {.i = 1,.q = 1,},
  553. {.i = 1,.q = 0,},
  554. {.i = 1,.q = -1,},
  555. {.i = 0,.q = -1,},
  556. {.i = -1,.q = -1,},
  557. {.i = -1,.q = 0,},
  558. {.i = -1,.q = 1,},
  559. {.i = 0,.q = 1,},
  560. };
  561. if (d->current_state == 0) {
  562. begin = 1;
  563. end = 8;
  564. } else if (d->current_state % 2 == 0) {
  565. begin = d->current_state - 1;
  566. end = d->current_state + 1;
  567. } else {
  568. begin = d->current_state - 2;
  569. end = d->current_state + 2;
  570. }
  571. if (begin < 1)
  572. begin += 8;
  573. if (end > 8)
  574. end -= 8;
  575. memcpy(&orig_loctl, probe_loctl, sizeof(struct b43_loctl));
  576. i = begin;
  577. d->current_state = i;
  578. while (1) {
  579. B43_WARN_ON(!(i >= 1 && i <= 8));
  580. memcpy(&test_loctl, &orig_loctl, sizeof(struct b43_loctl));
  581. test_loctl.i += modifiers[i - 1].i * d->state_val_multiplier;
  582. test_loctl.q += modifiers[i - 1].q * d->state_val_multiplier;
  583. if ((test_loctl.i != prev_loctl.i ||
  584. test_loctl.q != prev_loctl.q) &&
  585. (abs(test_loctl.i) <= 16 && abs(test_loctl.q) <= 16)) {
  586. b43_lo_write(dev, &test_loctl);
  587. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  588. phy->pga_gain,
  589. phy->trsw_rx_gain);
  590. if (feedth < d->lowest_feedth) {
  591. memcpy(probe_loctl, &test_loctl,
  592. sizeof(struct b43_loctl));
  593. found_lower = 1;
  594. d->lowest_feedth = feedth;
  595. if ((d->nr_measured < 2) &&
  596. !has_loopback_gain(phy))
  597. break;
  598. }
  599. }
  600. memcpy(&prev_loctl, &test_loctl, sizeof(prev_loctl));
  601. if (i == end)
  602. break;
  603. if (i == 8)
  604. i = 1;
  605. else
  606. i++;
  607. d->current_state = i;
  608. }
  609. return found_lower;
  610. }
  611. static void lo_probe_loctls_statemachine(struct b43_wldev *dev,
  612. struct b43_loctl *loctl,
  613. int *max_rx_gain)
  614. {
  615. struct b43_phy *phy = &dev->phy;
  616. struct b43_lo_g_statemachine d;
  617. u16 feedth;
  618. int found_lower;
  619. struct b43_loctl probe_loctl;
  620. int max_repeat = 1, repeat_cnt = 0;
  621. d.nr_measured = 0;
  622. d.state_val_multiplier = 1;
  623. if (has_loopback_gain(phy))
  624. d.state_val_multiplier = 3;
  625. memcpy(&d.min_loctl, loctl, sizeof(struct b43_loctl));
  626. if (has_loopback_gain(phy))
  627. max_repeat = 4;
  628. do {
  629. b43_lo_write(dev, &d.min_loctl);
  630. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  631. phy->pga_gain,
  632. phy->trsw_rx_gain);
  633. if (feedth < 0x258) {
  634. if (feedth >= 0x12C)
  635. *max_rx_gain += 6;
  636. else
  637. *max_rx_gain += 3;
  638. feedth = lo_measure_feedthrough(dev, phy->lna_gain,
  639. phy->pga_gain,
  640. phy->trsw_rx_gain);
  641. }
  642. d.lowest_feedth = feedth;
  643. d.current_state = 0;
  644. do {
  645. B43_WARN_ON(!
  646. (d.current_state >= 0
  647. && d.current_state <= 8));
  648. memcpy(&probe_loctl, &d.min_loctl,
  649. sizeof(struct b43_loctl));
  650. found_lower =
  651. lo_probe_possible_loctls(dev, &probe_loctl, &d);
  652. if (!found_lower)
  653. break;
  654. if ((probe_loctl.i == d.min_loctl.i) &&
  655. (probe_loctl.q == d.min_loctl.q))
  656. break;
  657. memcpy(&d.min_loctl, &probe_loctl,
  658. sizeof(struct b43_loctl));
  659. d.nr_measured++;
  660. } while (d.nr_measured < 24);
  661. memcpy(loctl, &d.min_loctl, sizeof(struct b43_loctl));
  662. if (has_loopback_gain(phy)) {
  663. if (d.lowest_feedth > 0x1194)
  664. *max_rx_gain -= 6;
  665. else if (d.lowest_feedth < 0x5DC)
  666. *max_rx_gain += 3;
  667. if (repeat_cnt == 0) {
  668. if (d.lowest_feedth <= 0x5DC) {
  669. d.state_val_multiplier = 1;
  670. repeat_cnt++;
  671. } else
  672. d.state_val_multiplier = 2;
  673. } else if (repeat_cnt == 2)
  674. d.state_val_multiplier = 1;
  675. }
  676. lo_measure_gain_values(dev, *max_rx_gain,
  677. has_loopback_gain(phy));
  678. } while (++repeat_cnt < max_repeat);
  679. }
  680. static
  681. struct b43_lo_calib * b43_calibrate_lo_setting(struct b43_wldev *dev,
  682. const struct b43_bbatt *bbatt,
  683. const struct b43_rfatt *rfatt)
  684. {
  685. struct b43_phy *phy = &dev->phy;
  686. struct b43_loctl loctl = {
  687. .i = 0,
  688. .q = 0,
  689. };
  690. int max_rx_gain;
  691. struct b43_lo_calib *cal;
  692. struct lo_g_saved_values uninitialized_var(saved_regs);
  693. /* Values from the "TXCTL Register and Value Table" */
  694. u16 txctl_reg;
  695. u16 txctl_value;
  696. u16 pad_mix_gain;
  697. saved_regs.old_channel = phy->channel;
  698. b43_mac_suspend(dev);
  699. lo_measure_setup(dev, &saved_regs);
  700. txctl_reg = lo_txctl_register_table(dev, &txctl_value, &pad_mix_gain);
  701. b43_radio_write16(dev, 0x43,
  702. (b43_radio_read16(dev, 0x43) & 0xFFF0)
  703. | rfatt->att);
  704. b43_radio_write16(dev, txctl_reg,
  705. (b43_radio_read16(dev, txctl_reg) & ~txctl_value)
  706. | (rfatt->with_padmix) ? txctl_value : 0);
  707. max_rx_gain = rfatt->att * 2;
  708. max_rx_gain += bbatt->att / 2;
  709. if (rfatt->with_padmix)
  710. max_rx_gain -= pad_mix_gain;
  711. if (has_loopback_gain(phy))
  712. max_rx_gain += phy->max_lb_gain;
  713. lo_measure_gain_values(dev, max_rx_gain,
  714. has_loopback_gain(phy));
  715. b43_phy_set_baseband_attenuation(dev, bbatt->att);
  716. lo_probe_loctls_statemachine(dev, &loctl, &max_rx_gain);
  717. lo_measure_restore(dev, &saved_regs);
  718. b43_mac_enable(dev);
  719. if (b43_debug(dev, B43_DBG_LO)) {
  720. b43dbg(dev->wl, "LO: Calibrated for BB(%u), RF(%u,%u) "
  721. "=> I=%d Q=%d\n",
  722. bbatt->att, rfatt->att, rfatt->with_padmix,
  723. loctl.i, loctl.q);
  724. }
  725. cal = kmalloc(sizeof(*cal), GFP_KERNEL);
  726. if (!cal) {
  727. b43warn(dev->wl, "LO calib: out of memory\n");
  728. return NULL;
  729. }
  730. memcpy(&cal->bbatt, bbatt, sizeof(*bbatt));
  731. memcpy(&cal->rfatt, rfatt, sizeof(*rfatt));
  732. memcpy(&cal->ctl, &loctl, sizeof(loctl));
  733. cal->calib_time = jiffies;
  734. INIT_LIST_HEAD(&cal->list);
  735. return cal;
  736. }
  737. /* Get a calibrated LO setting for the given attenuation values.
  738. * Might return a NULL pointer under OOM! */
  739. static
  740. struct b43_lo_calib * b43_get_calib_lo_settings(struct b43_wldev *dev,
  741. const struct b43_bbatt *bbatt,
  742. const struct b43_rfatt *rfatt)
  743. {
  744. struct b43_txpower_lo_control *lo = dev->phy.lo_control;
  745. struct b43_lo_calib *c;
  746. c = b43_find_lo_calib(lo, bbatt, rfatt);
  747. if (c)
  748. return c;
  749. /* Not in the list of calibrated LO settings.
  750. * Calibrate it now. */
  751. c = b43_calibrate_lo_setting(dev, bbatt, rfatt);
  752. if (!c)
  753. return NULL;
  754. list_add(&c->list, &lo->calib_list);
  755. return c;
  756. }
  757. void b43_gphy_dc_lt_init(struct b43_wldev *dev, bool update_all)
  758. {
  759. struct b43_phy *phy = &dev->phy;
  760. struct b43_txpower_lo_control *lo = phy->lo_control;
  761. int i;
  762. int rf_offset, bb_offset;
  763. const struct b43_rfatt *rfatt;
  764. const struct b43_bbatt *bbatt;
  765. u64 power_vector;
  766. bool table_changed = 0;
  767. BUILD_BUG_ON(B43_DC_LT_SIZE != 32);
  768. B43_WARN_ON(lo->rfatt_list.len * lo->bbatt_list.len > 64);
  769. power_vector = lo->power_vector;
  770. if (!update_all && !power_vector)
  771. return; /* Nothing to do. */
  772. /* Suspend the MAC now to avoid continuous suspend/enable
  773. * cycles in the loop. */
  774. b43_mac_suspend(dev);
  775. for (i = 0; i < B43_DC_LT_SIZE * 2; i++) {
  776. struct b43_lo_calib *cal;
  777. int idx;
  778. u16 val;
  779. if (!update_all && !(power_vector & (((u64)1ULL) << i)))
  780. continue;
  781. /* Update the table entry for this power_vector bit.
  782. * The table rows are RFatt entries and columns are BBatt. */
  783. bb_offset = i / lo->rfatt_list.len;
  784. rf_offset = i % lo->rfatt_list.len;
  785. bbatt = &(lo->bbatt_list.list[bb_offset]);
  786. rfatt = &(lo->rfatt_list.list[rf_offset]);
  787. cal = b43_calibrate_lo_setting(dev, bbatt, rfatt);
  788. if (!cal) {
  789. b43warn(dev->wl, "LO: Could not "
  790. "calibrate DC table entry\n");
  791. continue;
  792. }
  793. /*FIXME: Is Q really in the low nibble? */
  794. val = (u8)(cal->ctl.q);
  795. val |= ((u8)(cal->ctl.i)) << 4;
  796. kfree(cal);
  797. /* Get the index into the hardware DC LT. */
  798. idx = i / 2;
  799. /* Change the table in memory. */
  800. if (i % 2) {
  801. /* Change the high byte. */
  802. lo->dc_lt[idx] = (lo->dc_lt[idx] & 0x00FF)
  803. | ((val & 0x00FF) << 8);
  804. } else {
  805. /* Change the low byte. */
  806. lo->dc_lt[idx] = (lo->dc_lt[idx] & 0xFF00)
  807. | (val & 0x00FF);
  808. }
  809. table_changed = 1;
  810. }
  811. if (table_changed) {
  812. /* The table changed in memory. Update the hardware table. */
  813. for (i = 0; i < B43_DC_LT_SIZE; i++)
  814. b43_phy_write(dev, 0x3A0 + i, lo->dc_lt[i]);
  815. }
  816. b43_mac_enable(dev);
  817. }
  818. /* Fixup the RF attenuation value for the case where we are
  819. * using the PAD mixer. */
  820. static inline void b43_lo_fixup_rfatt(struct b43_rfatt *rf)
  821. {
  822. if (!rf->with_padmix)
  823. return;
  824. if ((rf->att != 1) && (rf->att != 2) && (rf->att != 3))
  825. rf->att = 4;
  826. }
  827. void b43_lo_g_adjust(struct b43_wldev *dev)
  828. {
  829. struct b43_phy *phy = &dev->phy;
  830. struct b43_lo_calib *cal;
  831. struct b43_rfatt rf;
  832. memcpy(&rf, &phy->rfatt, sizeof(rf));
  833. b43_lo_fixup_rfatt(&rf);
  834. cal = b43_get_calib_lo_settings(dev, &phy->bbatt, &rf);
  835. if (!cal)
  836. return;
  837. b43_lo_write(dev, &cal->ctl);
  838. }
  839. void b43_lo_g_adjust_to(struct b43_wldev *dev,
  840. u16 rfatt, u16 bbatt, u16 tx_control)
  841. {
  842. struct b43_rfatt rf;
  843. struct b43_bbatt bb;
  844. struct b43_lo_calib *cal;
  845. memset(&rf, 0, sizeof(rf));
  846. memset(&bb, 0, sizeof(bb));
  847. rf.att = rfatt;
  848. bb.att = bbatt;
  849. b43_lo_fixup_rfatt(&rf);
  850. cal = b43_get_calib_lo_settings(dev, &bb, &rf);
  851. if (!cal)
  852. return;
  853. b43_lo_write(dev, &cal->ctl);
  854. }
  855. /* Periodic LO maintanance work */
  856. void b43_lo_g_maintanance_work(struct b43_wldev *dev)
  857. {
  858. struct b43_phy *phy = &dev->phy;
  859. struct b43_txpower_lo_control *lo = phy->lo_control;
  860. unsigned long now;
  861. unsigned long expire;
  862. struct b43_lo_calib *cal, *tmp;
  863. bool current_item_expired = 0;
  864. bool hwpctl;
  865. if (!lo)
  866. return;
  867. now = jiffies;
  868. hwpctl = b43_has_hardware_pctl(phy);
  869. if (hwpctl) {
  870. /* Read the power vector and update it, if needed. */
  871. expire = now - B43_LO_PWRVEC_EXPIRE;
  872. if (time_before(lo->pwr_vec_read_time, expire)) {
  873. lo_read_power_vector(dev);
  874. b43_gphy_dc_lt_init(dev, 0);
  875. }
  876. //FIXME Recalc the whole DC table from time to time?
  877. }
  878. if (hwpctl)
  879. return;
  880. /* Search for expired LO settings. Remove them.
  881. * Recalibrate the current setting, if expired. */
  882. expire = now - B43_LO_CALIB_EXPIRE;
  883. list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
  884. if (!time_before(cal->calib_time, expire))
  885. continue;
  886. /* This item expired. */
  887. if (b43_compare_bbatt(&cal->bbatt, &phy->bbatt) &&
  888. b43_compare_rfatt(&cal->rfatt, &phy->rfatt)) {
  889. B43_WARN_ON(current_item_expired);
  890. current_item_expired = 1;
  891. }
  892. if (b43_debug(dev, B43_DBG_LO)) {
  893. b43dbg(dev->wl, "LO: Item BB(%u), RF(%u,%u), "
  894. "I=%d, Q=%d expired\n",
  895. cal->bbatt.att, cal->rfatt.att,
  896. cal->rfatt.with_padmix,
  897. cal->ctl.i, cal->ctl.q);
  898. }
  899. list_del(&cal->list);
  900. kfree(cal);
  901. }
  902. if (current_item_expired || unlikely(list_empty(&lo->calib_list))) {
  903. /* Recalibrate currently used LO setting. */
  904. if (b43_debug(dev, B43_DBG_LO))
  905. b43dbg(dev->wl, "LO: Recalibrating current LO setting\n");
  906. cal = b43_calibrate_lo_setting(dev, &phy->bbatt, &phy->rfatt);
  907. if (cal) {
  908. list_add(&cal->list, &lo->calib_list);
  909. b43_lo_write(dev, &cal->ctl);
  910. } else
  911. b43warn(dev->wl, "Failed to recalibrate current LO setting\n");
  912. }
  913. }
  914. void b43_lo_g_cleanup(struct b43_wldev *dev)
  915. {
  916. struct b43_txpower_lo_control *lo = dev->phy.lo_control;
  917. struct b43_lo_calib *cal, *tmp;
  918. if (!lo)
  919. return;
  920. list_for_each_entry_safe(cal, tmp, &lo->calib_list, list) {
  921. list_del(&cal->list);
  922. kfree(cal);
  923. }
  924. }
  925. /* LO Initialization */
  926. void b43_lo_g_init(struct b43_wldev *dev)
  927. {
  928. struct b43_phy *phy = &dev->phy;
  929. if (b43_has_hardware_pctl(phy)) {
  930. lo_read_power_vector(dev);
  931. b43_gphy_dc_lt_init(dev, 1);
  932. }
  933. }