ar9003_paprd.c 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866
  1. /*
  2. * Copyright (c) 2010-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/export.h>
  17. #include "hw.h"
  18. #include "ar9003_phy.h"
  19. void ar9003_paprd_enable(struct ath_hw *ah, bool val)
  20. {
  21. struct ath9k_channel *chan = ah->curchan;
  22. struct ar9300_eeprom *eep = &ah->eeprom.ar9300_eep;
  23. /*
  24. * 3 bits for modalHeader5G.papdRateMaskHt20
  25. * is used for sub-band disabling of PAPRD.
  26. * 5G band is divided into 3 sub-bands -- upper,
  27. * middle, lower.
  28. * if bit 30 of modalHeader5G.papdRateMaskHt20 is set
  29. * -- disable PAPRD for upper band 5GHz
  30. * if bit 29 of modalHeader5G.papdRateMaskHt20 is set
  31. * -- disable PAPRD for middle band 5GHz
  32. * if bit 28 of modalHeader5G.papdRateMaskHt20 is set
  33. * -- disable PAPRD for lower band 5GHz
  34. */
  35. if (IS_CHAN_5GHZ(chan)) {
  36. if (chan->channel >= UPPER_5G_SUB_BAND_START) {
  37. if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
  38. & BIT(30))
  39. val = false;
  40. } else if (chan->channel >= MID_5G_SUB_BAND_START) {
  41. if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
  42. & BIT(29))
  43. val = false;
  44. } else {
  45. if (le32_to_cpu(eep->modalHeader5G.papdRateMaskHt20)
  46. & BIT(28))
  47. val = false;
  48. }
  49. }
  50. if (val) {
  51. ah->paprd_table_write_done = true;
  52. ath9k_hw_apply_txpower(ah, chan);
  53. }
  54. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B0,
  55. AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
  56. if (ah->caps.tx_chainmask & BIT(1))
  57. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B1,
  58. AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
  59. if (ah->caps.tx_chainmask & BIT(2))
  60. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL0_B2,
  61. AR_PHY_PAPRD_CTRL0_PAPRD_ENABLE, !!val);
  62. }
  63. EXPORT_SYMBOL(ar9003_paprd_enable);
  64. static int ar9003_get_training_power_2g(struct ath_hw *ah)
  65. {
  66. struct ath9k_channel *chan = ah->curchan;
  67. unsigned int power, scale, delta;
  68. scale = ar9003_get_paprd_scale_factor(ah, chan);
  69. power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE5,
  70. AR_PHY_POWERTX_RATE5_POWERTXHT20_0);
  71. delta = abs((int) ah->paprd_target_power - (int) power);
  72. if (delta > scale)
  73. return -1;
  74. if (delta < 4)
  75. power -= 4 - delta;
  76. return power;
  77. }
  78. static int ar9003_get_training_power_5g(struct ath_hw *ah)
  79. {
  80. struct ath_common *common = ath9k_hw_common(ah);
  81. struct ath9k_channel *chan = ah->curchan;
  82. unsigned int power, scale, delta;
  83. scale = ar9003_get_paprd_scale_factor(ah, chan);
  84. if (IS_CHAN_HT40(chan))
  85. power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE8,
  86. AR_PHY_POWERTX_RATE8_POWERTXHT40_5);
  87. else
  88. power = REG_READ_FIELD(ah, AR_PHY_POWERTX_RATE6,
  89. AR_PHY_POWERTX_RATE6_POWERTXHT20_5);
  90. power += scale;
  91. delta = abs((int) ah->paprd_target_power - (int) power);
  92. if (delta > scale)
  93. return -1;
  94. switch (get_streams(ah->txchainmask)) {
  95. case 1:
  96. delta = 6;
  97. break;
  98. case 2:
  99. delta = 4;
  100. break;
  101. case 3:
  102. delta = 2;
  103. break;
  104. default:
  105. delta = 0;
  106. ath_dbg(common, CALIBRATE, "Invalid tx-chainmask: %u\n",
  107. ah->txchainmask);
  108. }
  109. power += delta;
  110. return power;
  111. }
  112. static int ar9003_paprd_setup_single_table(struct ath_hw *ah)
  113. {
  114. struct ath_common *common = ath9k_hw_common(ah);
  115. static const u32 ctrl0[3] = {
  116. AR_PHY_PAPRD_CTRL0_B0,
  117. AR_PHY_PAPRD_CTRL0_B1,
  118. AR_PHY_PAPRD_CTRL0_B2
  119. };
  120. static const u32 ctrl1[3] = {
  121. AR_PHY_PAPRD_CTRL1_B0,
  122. AR_PHY_PAPRD_CTRL1_B1,
  123. AR_PHY_PAPRD_CTRL1_B2
  124. };
  125. int training_power;
  126. int i, val;
  127. if (IS_CHAN_2GHZ(ah->curchan))
  128. training_power = ar9003_get_training_power_2g(ah);
  129. else
  130. training_power = ar9003_get_training_power_5g(ah);
  131. ath_dbg(common, CALIBRATE, "Training power: %d, Target power: %d\n",
  132. training_power, ah->paprd_target_power);
  133. if (training_power < 0) {
  134. ath_dbg(common, CALIBRATE,
  135. "PAPRD target power delta out of range\n");
  136. return -ERANGE;
  137. }
  138. ah->paprd_training_power = training_power;
  139. REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2AM, AR_PHY_PAPRD_AM2AM_MASK,
  140. ah->paprd_ratemask);
  141. REG_RMW_FIELD(ah, AR_PHY_PAPRD_AM2PM, AR_PHY_PAPRD_AM2PM_MASK,
  142. ah->paprd_ratemask);
  143. REG_RMW_FIELD(ah, AR_PHY_PAPRD_HT40, AR_PHY_PAPRD_HT40_MASK,
  144. ah->paprd_ratemask_ht40);
  145. for (i = 0; i < ah->caps.max_txchains; i++) {
  146. REG_RMW_FIELD(ah, ctrl0[i],
  147. AR_PHY_PAPRD_CTRL0_USE_SINGLE_TABLE_MASK, 1);
  148. REG_RMW_FIELD(ah, ctrl1[i],
  149. AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2PM_ENABLE, 1);
  150. REG_RMW_FIELD(ah, ctrl1[i],
  151. AR_PHY_PAPRD_CTRL1_ADAPTIVE_AM2AM_ENABLE, 1);
  152. REG_RMW_FIELD(ah, ctrl1[i],
  153. AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
  154. REG_RMW_FIELD(ah, ctrl1[i],
  155. AR_PHY_PAPRD_CTRL1_PA_GAIN_SCALE_FACT_MASK, 181);
  156. REG_RMW_FIELD(ah, ctrl1[i],
  157. AR_PHY_PAPRD_CTRL1_PAPRD_MAG_SCALE_FACT, 361);
  158. REG_RMW_FIELD(ah, ctrl1[i],
  159. AR_PHY_PAPRD_CTRL1_ADAPTIVE_SCALING_ENA, 0);
  160. REG_RMW_FIELD(ah, ctrl0[i],
  161. AR_PHY_PAPRD_CTRL0_PAPRD_MAG_THRSH, 3);
  162. }
  163. ar9003_paprd_enable(ah, false);
  164. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  165. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_SKIP, 0x30);
  166. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  167. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_LB_ENABLE, 1);
  168. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  169. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_TX_GAIN_FORCE, 1);
  170. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  171. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_RX_BB_GAIN_FORCE, 0);
  172. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  173. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_IQCORR_ENABLE, 0);
  174. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  175. AR_PHY_PAPRD_TRAINER_CNTL1_CF_PAPRD_AGC2_SETTLING, 28);
  176. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL1,
  177. AR_PHY_PAPRD_TRAINER_CNTL1_CF_CF_PAPRD_TRAIN_ENABLE, 1);
  178. val = AR_SREV_9462(ah) ? 0x91 : 147;
  179. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL2,
  180. AR_PHY_PAPRD_TRAINER_CNTL2_CF_PAPRD_INIT_RX_BB_GAIN, val);
  181. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  182. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_FINE_CORR_LEN, 4);
  183. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  184. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_COARSE_CORR_LEN, 4);
  185. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  186. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_NUM_CORR_STAGES, 7);
  187. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  188. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_MIN_LOOPBACK_DEL, 1);
  189. if (AR_SREV_9485(ah) || AR_SREV_9462(ah))
  190. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  191. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
  192. -3);
  193. else
  194. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  195. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_QUICK_DROP,
  196. -6);
  197. val = AR_SREV_9462(ah) ? -10 : -15;
  198. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  199. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_ADC_DESIRED_SIZE,
  200. val);
  201. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL3,
  202. AR_PHY_PAPRD_TRAINER_CNTL3_CF_PAPRD_BBTXMIX_DISABLE, 1);
  203. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
  204. AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_SAFETY_DELTA, 0);
  205. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
  206. AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_MIN_CORR, 400);
  207. REG_RMW_FIELD(ah, AR_PHY_PAPRD_TRAINER_CNTL4,
  208. AR_PHY_PAPRD_TRAINER_CNTL4_CF_PAPRD_NUM_TRAIN_SAMPLES,
  209. 100);
  210. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_0_B0,
  211. AR_PHY_PAPRD_PRE_POST_SCALING, 261376);
  212. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_1_B0,
  213. AR_PHY_PAPRD_PRE_POST_SCALING, 248079);
  214. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_2_B0,
  215. AR_PHY_PAPRD_PRE_POST_SCALING, 233759);
  216. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_3_B0,
  217. AR_PHY_PAPRD_PRE_POST_SCALING, 220464);
  218. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_4_B0,
  219. AR_PHY_PAPRD_PRE_POST_SCALING, 208194);
  220. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_5_B0,
  221. AR_PHY_PAPRD_PRE_POST_SCALING, 196949);
  222. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_6_B0,
  223. AR_PHY_PAPRD_PRE_POST_SCALING, 185706);
  224. REG_RMW_FIELD(ah, AR_PHY_PAPRD_PRE_POST_SCALE_7_B0,
  225. AR_PHY_PAPRD_PRE_POST_SCALING, 175487);
  226. return 0;
  227. }
  228. static void ar9003_paprd_get_gain_table(struct ath_hw *ah)
  229. {
  230. u32 *entry = ah->paprd_gain_table_entries;
  231. u8 *index = ah->paprd_gain_table_index;
  232. u32 reg = AR_PHY_TXGAIN_TABLE;
  233. int i;
  234. memset(entry, 0, sizeof(ah->paprd_gain_table_entries));
  235. memset(index, 0, sizeof(ah->paprd_gain_table_index));
  236. for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
  237. entry[i] = REG_READ(ah, reg);
  238. index[i] = (entry[i] >> 24) & 0xff;
  239. reg += 4;
  240. }
  241. }
  242. static unsigned int ar9003_get_desired_gain(struct ath_hw *ah, int chain,
  243. int target_power)
  244. {
  245. int olpc_gain_delta = 0, cl_gain_mod;
  246. int alpha_therm, alpha_volt;
  247. int therm_cal_value, volt_cal_value;
  248. int therm_value, volt_value;
  249. int thermal_gain_corr, voltage_gain_corr;
  250. int desired_scale, desired_gain = 0;
  251. u32 reg_olpc = 0, reg_cl_gain = 0;
  252. REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  253. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  254. desired_scale = REG_READ_FIELD(ah, AR_PHY_TPC_12,
  255. AR_PHY_TPC_12_DESIRED_SCALE_HT40_5);
  256. alpha_therm = REG_READ_FIELD(ah, AR_PHY_TPC_19,
  257. AR_PHY_TPC_19_ALPHA_THERM);
  258. alpha_volt = REG_READ_FIELD(ah, AR_PHY_TPC_19,
  259. AR_PHY_TPC_19_ALPHA_VOLT);
  260. therm_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
  261. AR_PHY_TPC_18_THERM_CAL_VALUE);
  262. volt_cal_value = REG_READ_FIELD(ah, AR_PHY_TPC_18,
  263. AR_PHY_TPC_18_VOLT_CAL_VALUE);
  264. therm_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
  265. AR_PHY_BB_THERM_ADC_4_LATEST_THERM_VALUE);
  266. volt_value = REG_READ_FIELD(ah, AR_PHY_BB_THERM_ADC_4,
  267. AR_PHY_BB_THERM_ADC_4_LATEST_VOLT_VALUE);
  268. switch (chain) {
  269. case 0:
  270. reg_olpc = AR_PHY_TPC_11_B0;
  271. reg_cl_gain = AR_PHY_CL_TAB_0;
  272. break;
  273. case 1:
  274. reg_olpc = AR_PHY_TPC_11_B1;
  275. reg_cl_gain = AR_PHY_CL_TAB_1;
  276. break;
  277. case 2:
  278. reg_olpc = AR_PHY_TPC_11_B2;
  279. reg_cl_gain = AR_PHY_CL_TAB_2;
  280. break;
  281. default:
  282. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  283. "Invalid chainmask: %d\n", chain);
  284. break;
  285. }
  286. olpc_gain_delta = REG_READ_FIELD(ah, reg_olpc,
  287. AR_PHY_TPC_11_OLPC_GAIN_DELTA);
  288. cl_gain_mod = REG_READ_FIELD(ah, reg_cl_gain,
  289. AR_PHY_CL_TAB_CL_GAIN_MOD);
  290. if (olpc_gain_delta >= 128)
  291. olpc_gain_delta = olpc_gain_delta - 256;
  292. thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
  293. (256 / 2)) / 256;
  294. voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
  295. (128 / 2)) / 128;
  296. desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
  297. voltage_gain_corr + desired_scale + cl_gain_mod;
  298. return desired_gain;
  299. }
  300. static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
  301. {
  302. int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
  303. int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
  304. u32 *gain_table_entries = ah->paprd_gain_table_entries;
  305. selected_gain_entry = gain_table_entries[gain_index];
  306. txbb1dbgain = selected_gain_entry & 0x7;
  307. txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
  308. txmxrgain = (selected_gain_entry >> 5) & 0xf;
  309. padrvgnA = (selected_gain_entry >> 9) & 0xf;
  310. padrvgnB = (selected_gain_entry >> 13) & 0xf;
  311. padrvgnC = (selected_gain_entry >> 17) & 0xf;
  312. padrvgnD = (selected_gain_entry >> 21) & 0x3;
  313. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  314. AR_PHY_TX_FORCED_GAIN_FORCED_TXBB1DBGAIN, txbb1dbgain);
  315. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  316. AR_PHY_TX_FORCED_GAIN_FORCED_TXBB6DBGAIN, txbb6dbgain);
  317. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  318. AR_PHY_TX_FORCED_GAIN_FORCED_TXMXRGAIN, txmxrgain);
  319. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  320. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNA, padrvgnA);
  321. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  322. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNB, padrvgnB);
  323. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  324. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNC, padrvgnC);
  325. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  326. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGND, padrvgnD);
  327. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  328. AR_PHY_TX_FORCED_GAIN_FORCED_ENABLE_PAL, 0);
  329. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  330. AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN, 0);
  331. REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
  332. REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCE_DAC_GAIN, 0);
  333. }
  334. static inline int find_expn(int num)
  335. {
  336. return fls(num) - 1;
  337. }
  338. static inline int find_proper_scale(int expn, int N)
  339. {
  340. return (expn > N) ? expn - 10 : 0;
  341. }
  342. #define NUM_BIN 23
  343. static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
  344. {
  345. unsigned int thresh_accum_cnt;
  346. int x_est[NUM_BIN + 1], Y[NUM_BIN + 1], theta[NUM_BIN + 1];
  347. int PA_in[NUM_BIN + 1];
  348. int B1_tmp[NUM_BIN + 1], B2_tmp[NUM_BIN + 1];
  349. unsigned int B1_abs_max, B2_abs_max;
  350. int max_index, scale_factor;
  351. int y_est[NUM_BIN + 1];
  352. int x_est_fxp1_nonlin, x_tilde[NUM_BIN + 1];
  353. unsigned int x_tilde_abs;
  354. int G_fxp, Y_intercept, order_x_by_y, M, I, L, sum_y_sqr, sum_y_quad;
  355. int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
  356. int Q_scale_B, Q_beta, Q_alpha, alpha, beta, order_1, order_2;
  357. int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
  358. int y5, y3, tmp;
  359. int theta_low_bin = 0;
  360. int i;
  361. /* disregard any bin that contains <= 16 samples */
  362. thresh_accum_cnt = 16;
  363. scale_factor = 5;
  364. max_index = 0;
  365. memset(theta, 0, sizeof(theta));
  366. memset(x_est, 0, sizeof(x_est));
  367. memset(Y, 0, sizeof(Y));
  368. memset(y_est, 0, sizeof(y_est));
  369. memset(x_tilde, 0, sizeof(x_tilde));
  370. for (i = 0; i < NUM_BIN; i++) {
  371. s32 accum_cnt, accum_tx, accum_rx, accum_ang;
  372. /* number of samples */
  373. accum_cnt = data_L[i] & 0xffff;
  374. if (accum_cnt <= thresh_accum_cnt)
  375. continue;
  376. /* sum(tx amplitude) */
  377. accum_tx = ((data_L[i] >> 16) & 0xffff) |
  378. ((data_U[i] & 0x7ff) << 16);
  379. /* sum(rx amplitude distance to lower bin edge) */
  380. accum_rx = ((data_U[i] >> 11) & 0x1f) |
  381. ((data_L[i + 23] & 0xffff) << 5);
  382. /* sum(angles) */
  383. accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
  384. ((data_U[i + 23] & 0x7ff) << 16);
  385. accum_tx <<= scale_factor;
  386. accum_rx <<= scale_factor;
  387. x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
  388. scale_factor;
  389. Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
  390. scale_factor) +
  391. (1 << scale_factor) * max_index + 16;
  392. if (accum_ang >= (1 << 26))
  393. accum_ang -= 1 << 27;
  394. theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
  395. accum_cnt;
  396. max_index++;
  397. }
  398. /*
  399. * Find average theta of first 5 bin and all of those to same value.
  400. * Curve is linear at that range.
  401. */
  402. for (i = 1; i < 6; i++)
  403. theta_low_bin += theta[i];
  404. theta_low_bin = theta_low_bin / 5;
  405. for (i = 1; i < 6; i++)
  406. theta[i] = theta_low_bin;
  407. /* Set values at origin */
  408. theta[0] = theta_low_bin;
  409. for (i = 0; i <= max_index; i++)
  410. theta[i] -= theta_low_bin;
  411. x_est[0] = 0;
  412. Y[0] = 0;
  413. scale_factor = 8;
  414. /* low signal gain */
  415. if (x_est[6] == x_est[3])
  416. return false;
  417. G_fxp =
  418. (((Y[6] - Y[3]) * 1 << scale_factor) +
  419. (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
  420. /* prevent division by zero */
  421. if (G_fxp == 0)
  422. return false;
  423. Y_intercept =
  424. (G_fxp * (x_est[0] - x_est[3]) +
  425. (1 << scale_factor)) / (1 << scale_factor) + Y[3];
  426. for (i = 0; i <= max_index; i++)
  427. y_est[i] = Y[i] - Y_intercept;
  428. for (i = 0; i <= 3; i++) {
  429. y_est[i] = i * 32;
  430. x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
  431. }
  432. if (y_est[max_index] == 0)
  433. return false;
  434. x_est_fxp1_nonlin =
  435. x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
  436. G_fxp) / G_fxp;
  437. order_x_by_y =
  438. (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
  439. if (order_x_by_y == 0)
  440. M = 10;
  441. else if (order_x_by_y == 1)
  442. M = 9;
  443. else
  444. M = 8;
  445. I = (max_index > 15) ? 7 : max_index >> 1;
  446. L = max_index - I;
  447. scale_factor = 8;
  448. sum_y_sqr = 0;
  449. sum_y_quad = 0;
  450. x_tilde_abs = 0;
  451. for (i = 0; i <= L; i++) {
  452. unsigned int y_sqr;
  453. unsigned int y_quad;
  454. unsigned int tmp_abs;
  455. /* prevent division by zero */
  456. if (y_est[i + I] == 0)
  457. return false;
  458. x_est_fxp1_nonlin =
  459. x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
  460. G_fxp) / G_fxp;
  461. x_tilde[i] =
  462. (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
  463. I];
  464. x_tilde[i] =
  465. (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
  466. x_tilde[i] =
  467. (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
  468. y_sqr =
  469. (y_est[i + I] * y_est[i + I] +
  470. (scale_factor * scale_factor)) / (scale_factor *
  471. scale_factor);
  472. tmp_abs = abs(x_tilde[i]);
  473. if (tmp_abs > x_tilde_abs)
  474. x_tilde_abs = tmp_abs;
  475. y_quad = y_sqr * y_sqr;
  476. sum_y_sqr = sum_y_sqr + y_sqr;
  477. sum_y_quad = sum_y_quad + y_quad;
  478. B1_tmp[i] = y_sqr * (L + 1);
  479. B2_tmp[i] = y_sqr;
  480. }
  481. B1_abs_max = 0;
  482. B2_abs_max = 0;
  483. for (i = 0; i <= L; i++) {
  484. int abs_val;
  485. B1_tmp[i] -= sum_y_sqr;
  486. B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
  487. abs_val = abs(B1_tmp[i]);
  488. if (abs_val > B1_abs_max)
  489. B1_abs_max = abs_val;
  490. abs_val = abs(B2_tmp[i]);
  491. if (abs_val > B2_abs_max)
  492. B2_abs_max = abs_val;
  493. }
  494. Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
  495. Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
  496. Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
  497. beta_raw = 0;
  498. alpha_raw = 0;
  499. for (i = 0; i <= L; i++) {
  500. x_tilde[i] = x_tilde[i] / (1 << Q_x);
  501. B1_tmp[i] = B1_tmp[i] / (1 << Q_B1);
  502. B2_tmp[i] = B2_tmp[i] / (1 << Q_B2);
  503. beta_raw = beta_raw + B1_tmp[i] * x_tilde[i];
  504. alpha_raw = alpha_raw + B2_tmp[i] * x_tilde[i];
  505. }
  506. scale_B =
  507. ((sum_y_quad / scale_factor) * (L + 1) -
  508. (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
  509. Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
  510. scale_B = scale_B / (1 << Q_scale_B);
  511. if (scale_B == 0)
  512. return false;
  513. Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
  514. Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
  515. beta_raw = beta_raw / (1 << Q_beta);
  516. alpha_raw = alpha_raw / (1 << Q_alpha);
  517. alpha = (alpha_raw << 10) / scale_B;
  518. beta = (beta_raw << 10) / scale_B;
  519. order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
  520. order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
  521. order1_5x = order_1 / 5;
  522. order2_3x = order_2 / 3;
  523. order1_5x_rem = order_1 - 5 * order1_5x;
  524. order2_3x_rem = order_2 - 3 * order2_3x;
  525. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  526. tmp = i * 32;
  527. y5 = ((beta * tmp) >> 6) >> order1_5x;
  528. y5 = (y5 * tmp) >> order1_5x;
  529. y5 = (y5 * tmp) >> order1_5x;
  530. y5 = (y5 * tmp) >> order1_5x;
  531. y5 = (y5 * tmp) >> order1_5x;
  532. y5 = y5 >> order1_5x_rem;
  533. y3 = (alpha * tmp) >> order2_3x;
  534. y3 = (y3 * tmp) >> order2_3x;
  535. y3 = (y3 * tmp) >> order2_3x;
  536. y3 = y3 >> order2_3x_rem;
  537. PA_in[i] = y5 + y3 + (256 * tmp) / G_fxp;
  538. if (i >= 2) {
  539. tmp = PA_in[i] - PA_in[i - 1];
  540. if (tmp < 0)
  541. PA_in[i] =
  542. PA_in[i - 1] + (PA_in[i - 1] -
  543. PA_in[i - 2]);
  544. }
  545. PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
  546. }
  547. beta_raw = 0;
  548. alpha_raw = 0;
  549. for (i = 0; i <= L; i++) {
  550. int theta_tilde =
  551. ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
  552. theta_tilde =
  553. ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
  554. theta_tilde =
  555. ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
  556. beta_raw = beta_raw + B1_tmp[i] * theta_tilde;
  557. alpha_raw = alpha_raw + B2_tmp[i] * theta_tilde;
  558. }
  559. Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
  560. Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
  561. beta_raw = beta_raw / (1 << Q_beta);
  562. alpha_raw = alpha_raw / (1 << Q_alpha);
  563. alpha = (alpha_raw << 10) / scale_B;
  564. beta = (beta_raw << 10) / scale_B;
  565. order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
  566. order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
  567. order1_5x = order_1 / 5;
  568. order2_3x = order_2 / 3;
  569. order1_5x_rem = order_1 - 5 * order1_5x;
  570. order2_3x_rem = order_2 - 3 * order2_3x;
  571. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  572. int PA_angle;
  573. /* pa_table[4] is calculated from PA_angle for i=5 */
  574. if (i == 4)
  575. continue;
  576. tmp = i * 32;
  577. if (beta > 0)
  578. y5 = (((beta * tmp - 64) >> 6) -
  579. (1 << order1_5x)) / (1 << order1_5x);
  580. else
  581. y5 = ((((beta * tmp - 64) >> 6) +
  582. (1 << order1_5x)) / (1 << order1_5x));
  583. y5 = (y5 * tmp) / (1 << order1_5x);
  584. y5 = (y5 * tmp) / (1 << order1_5x);
  585. y5 = (y5 * tmp) / (1 << order1_5x);
  586. y5 = (y5 * tmp) / (1 << order1_5x);
  587. y5 = y5 / (1 << order1_5x_rem);
  588. if (beta > 0)
  589. y3 = (alpha * tmp -
  590. (1 << order2_3x)) / (1 << order2_3x);
  591. else
  592. y3 = (alpha * tmp +
  593. (1 << order2_3x)) / (1 << order2_3x);
  594. y3 = (y3 * tmp) / (1 << order2_3x);
  595. y3 = (y3 * tmp) / (1 << order2_3x);
  596. y3 = y3 / (1 << order2_3x_rem);
  597. if (i < 4) {
  598. PA_angle = 0;
  599. } else {
  600. PA_angle = y5 + y3;
  601. if (PA_angle < -150)
  602. PA_angle = -150;
  603. else if (PA_angle > 150)
  604. PA_angle = 150;
  605. }
  606. pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
  607. if (i == 5) {
  608. PA_angle = (PA_angle + 2) >> 1;
  609. pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
  610. (PA_angle & 0x7ff);
  611. }
  612. }
  613. *gain = G_fxp;
  614. return true;
  615. }
  616. void ar9003_paprd_populate_single_table(struct ath_hw *ah,
  617. struct ath9k_hw_cal_data *caldata,
  618. int chain)
  619. {
  620. u32 *paprd_table_val = caldata->pa_table[chain];
  621. u32 small_signal_gain = caldata->small_signal_gain[chain];
  622. u32 training_power = ah->paprd_training_power;
  623. u32 reg = 0;
  624. int i;
  625. if (chain == 0)
  626. reg = AR_PHY_PAPRD_MEM_TAB_B0;
  627. else if (chain == 1)
  628. reg = AR_PHY_PAPRD_MEM_TAB_B1;
  629. else if (chain == 2)
  630. reg = AR_PHY_PAPRD_MEM_TAB_B2;
  631. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  632. REG_WRITE(ah, reg, paprd_table_val[i]);
  633. reg = reg + 4;
  634. }
  635. if (chain == 0)
  636. reg = AR_PHY_PA_GAIN123_B0;
  637. else if (chain == 1)
  638. reg = AR_PHY_PA_GAIN123_B1;
  639. else
  640. reg = AR_PHY_PA_GAIN123_B2;
  641. REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
  642. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
  643. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  644. training_power);
  645. if (ah->caps.tx_chainmask & BIT(1))
  646. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B1,
  647. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  648. training_power);
  649. if (ah->caps.tx_chainmask & BIT(2))
  650. /* val AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL correct? */
  651. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B2,
  652. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  653. training_power);
  654. }
  655. EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
  656. int ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
  657. {
  658. unsigned int i, desired_gain, gain_index;
  659. unsigned int train_power = ah->paprd_training_power;
  660. desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
  661. gain_index = 0;
  662. for (i = 0; i < PAPRD_GAIN_TABLE_ENTRIES; i++) {
  663. if (ah->paprd_gain_table_index[i] >= desired_gain)
  664. break;
  665. gain_index++;
  666. }
  667. ar9003_tx_force_gain(ah, gain_index);
  668. REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  669. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  670. return 0;
  671. }
  672. EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
  673. int ar9003_paprd_create_curve(struct ath_hw *ah,
  674. struct ath9k_hw_cal_data *caldata, int chain)
  675. {
  676. u16 *small_signal_gain = &caldata->small_signal_gain[chain];
  677. u32 *pa_table = caldata->pa_table[chain];
  678. u32 *data_L, *data_U;
  679. int i, status = 0;
  680. u32 *buf;
  681. u32 reg;
  682. memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
  683. buf = kmalloc(2 * 48 * sizeof(u32), GFP_ATOMIC);
  684. if (!buf)
  685. return -ENOMEM;
  686. data_L = &buf[0];
  687. data_U = &buf[48];
  688. REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
  689. AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
  690. reg = AR_PHY_CHAN_INFO_TAB_0;
  691. for (i = 0; i < 48; i++)
  692. data_L[i] = REG_READ(ah, reg + (i << 2));
  693. REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
  694. AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
  695. for (i = 0; i < 48; i++)
  696. data_U[i] = REG_READ(ah, reg + (i << 2));
  697. if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
  698. status = -2;
  699. REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  700. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  701. kfree(buf);
  702. return status;
  703. }
  704. EXPORT_SYMBOL(ar9003_paprd_create_curve);
  705. int ar9003_paprd_init_table(struct ath_hw *ah)
  706. {
  707. int ret;
  708. ret = ar9003_paprd_setup_single_table(ah);
  709. if (ret < 0)
  710. return ret;
  711. ar9003_paprd_get_gain_table(ah);
  712. return 0;
  713. }
  714. EXPORT_SYMBOL(ar9003_paprd_init_table);
  715. bool ar9003_paprd_is_done(struct ath_hw *ah)
  716. {
  717. int paprd_done, agc2_pwr;
  718. paprd_done = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  719. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  720. if (paprd_done == 0x1) {
  721. agc2_pwr = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  722. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_AGC2_PWR);
  723. ath_dbg(ath9k_hw_common(ah), CALIBRATE,
  724. "AGC2_PWR = 0x%x training done = 0x%x\n",
  725. agc2_pwr, paprd_done);
  726. /*
  727. * agc2_pwr range should not be less than 'IDEAL_AGC2_PWR_CHANGE'
  728. * when the training is completely done, otherwise retraining is
  729. * done to make sure the value is in ideal range
  730. */
  731. if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
  732. paprd_done = 0;
  733. }
  734. return !!paprd_done;
  735. }
  736. EXPORT_SYMBOL(ar9003_paprd_is_done);