lo.c 27 KB

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