lo.c 27 KB

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