lo.c 28 KB

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