lo.c 27 KB

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