ar9003_paprd.c 24 KB

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