lo.c 26 KB

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