lo.c 27 KB

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