lo.c 27 KB

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