ar9003_paprd.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826
  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 < 32; 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;
  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;
  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. if (chain == 0)
  245. reg = AR_PHY_TPC_11_B0;
  246. else if (chain == 1)
  247. reg = AR_PHY_TPC_11_B1;
  248. else
  249. reg = AR_PHY_TPC_11_B2;
  250. olpc_gain_delta = REG_READ_FIELD(ah, reg,
  251. AR_PHY_TPC_11_OLPC_GAIN_DELTA);
  252. if (olpc_gain_delta >= 128)
  253. olpc_gain_delta = olpc_gain_delta - 256;
  254. thermal_gain_corr = (alpha_therm * (therm_value - therm_cal_value) +
  255. (256 / 2)) / 256;
  256. voltage_gain_corr = (alpha_volt * (volt_value - volt_cal_value) +
  257. (128 / 2)) / 128;
  258. desired_gain = target_power - olpc_gain_delta - thermal_gain_corr -
  259. voltage_gain_corr + desired_scale;
  260. return desired_gain;
  261. }
  262. static void ar9003_tx_force_gain(struct ath_hw *ah, unsigned int gain_index)
  263. {
  264. int selected_gain_entry, txbb1dbgain, txbb6dbgain, txmxrgain;
  265. int padrvgnA, padrvgnB, padrvgnC, padrvgnD;
  266. u32 *gain_table_entries = ah->paprd_gain_table_entries;
  267. selected_gain_entry = gain_table_entries[gain_index];
  268. txbb1dbgain = selected_gain_entry & 0x7;
  269. txbb6dbgain = (selected_gain_entry >> 3) & 0x3;
  270. txmxrgain = (selected_gain_entry >> 5) & 0xf;
  271. padrvgnA = (selected_gain_entry >> 9) & 0xf;
  272. padrvgnB = (selected_gain_entry >> 13) & 0xf;
  273. padrvgnC = (selected_gain_entry >> 17) & 0xf;
  274. padrvgnD = (selected_gain_entry >> 21) & 0x3;
  275. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  276. AR_PHY_TX_FORCED_GAIN_FORCED_TXBB1DBGAIN, txbb1dbgain);
  277. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  278. AR_PHY_TX_FORCED_GAIN_FORCED_TXBB6DBGAIN, txbb6dbgain);
  279. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  280. AR_PHY_TX_FORCED_GAIN_FORCED_TXMXRGAIN, txmxrgain);
  281. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  282. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNA, padrvgnA);
  283. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  284. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNB, padrvgnB);
  285. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  286. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGNC, padrvgnC);
  287. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  288. AR_PHY_TX_FORCED_GAIN_FORCED_PADRVGND, padrvgnD);
  289. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  290. AR_PHY_TX_FORCED_GAIN_FORCED_ENABLE_PAL, 0);
  291. REG_RMW_FIELD(ah, AR_PHY_TX_FORCED_GAIN,
  292. AR_PHY_TX_FORCED_GAIN_FORCE_TX_GAIN, 0);
  293. REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCED_DAC_GAIN, 0);
  294. REG_RMW_FIELD(ah, AR_PHY_TPC_1, AR_PHY_TPC_1_FORCE_DAC_GAIN, 0);
  295. }
  296. static inline int find_expn(int num)
  297. {
  298. return fls(num) - 1;
  299. }
  300. static inline int find_proper_scale(int expn, int N)
  301. {
  302. return (expn > N) ? expn - 10 : 0;
  303. }
  304. #define NUM_BIN 23
  305. static bool create_pa_curve(u32 *data_L, u32 *data_U, u32 *pa_table, u16 *gain)
  306. {
  307. unsigned int thresh_accum_cnt;
  308. int x_est[NUM_BIN + 1], Y[NUM_BIN + 1], theta[NUM_BIN + 1];
  309. int PA_in[NUM_BIN + 1];
  310. int B1_tmp[NUM_BIN + 1], B2_tmp[NUM_BIN + 1];
  311. unsigned int B1_abs_max, B2_abs_max;
  312. int max_index, scale_factor;
  313. int y_est[NUM_BIN + 1];
  314. int x_est_fxp1_nonlin, x_tilde[NUM_BIN + 1];
  315. unsigned int x_tilde_abs;
  316. int G_fxp, Y_intercept, order_x_by_y, M, I, L, sum_y_sqr, sum_y_quad;
  317. int Q_x, Q_B1, Q_B2, beta_raw, alpha_raw, scale_B;
  318. int Q_scale_B, Q_beta, Q_alpha, alpha, beta, order_1, order_2;
  319. int order1_5x, order2_3x, order1_5x_rem, order2_3x_rem;
  320. int y5, y3, tmp;
  321. int theta_low_bin = 0;
  322. int i;
  323. /* disregard any bin that contains <= 16 samples */
  324. thresh_accum_cnt = 16;
  325. scale_factor = 5;
  326. max_index = 0;
  327. memset(theta, 0, sizeof(theta));
  328. memset(x_est, 0, sizeof(x_est));
  329. memset(Y, 0, sizeof(Y));
  330. memset(y_est, 0, sizeof(y_est));
  331. memset(x_tilde, 0, sizeof(x_tilde));
  332. for (i = 0; i < NUM_BIN; i++) {
  333. s32 accum_cnt, accum_tx, accum_rx, accum_ang;
  334. /* number of samples */
  335. accum_cnt = data_L[i] & 0xffff;
  336. if (accum_cnt <= thresh_accum_cnt)
  337. continue;
  338. /* sum(tx amplitude) */
  339. accum_tx = ((data_L[i] >> 16) & 0xffff) |
  340. ((data_U[i] & 0x7ff) << 16);
  341. /* sum(rx amplitude distance to lower bin edge) */
  342. accum_rx = ((data_U[i] >> 11) & 0x1f) |
  343. ((data_L[i + 23] & 0xffff) << 5);
  344. /* sum(angles) */
  345. accum_ang = ((data_L[i + 23] >> 16) & 0xffff) |
  346. ((data_U[i + 23] & 0x7ff) << 16);
  347. accum_tx <<= scale_factor;
  348. accum_rx <<= scale_factor;
  349. x_est[i + 1] = (((accum_tx + accum_cnt) / accum_cnt) + 32) >>
  350. scale_factor;
  351. Y[i + 1] = ((((accum_rx + accum_cnt) / accum_cnt) + 32) >>
  352. scale_factor) +
  353. (1 << scale_factor) * max_index + 16;
  354. if (accum_ang >= (1 << 26))
  355. accum_ang -= 1 << 27;
  356. theta[i + 1] = ((accum_ang * (1 << scale_factor)) + accum_cnt) /
  357. accum_cnt;
  358. max_index++;
  359. }
  360. /*
  361. * Find average theta of first 5 bin and all of those to same value.
  362. * Curve is linear at that range.
  363. */
  364. for (i = 1; i < 6; i++)
  365. theta_low_bin += theta[i];
  366. theta_low_bin = theta_low_bin / 5;
  367. for (i = 1; i < 6; i++)
  368. theta[i] = theta_low_bin;
  369. /* Set values at origin */
  370. theta[0] = theta_low_bin;
  371. for (i = 0; i <= max_index; i++)
  372. theta[i] -= theta_low_bin;
  373. x_est[0] = 0;
  374. Y[0] = 0;
  375. scale_factor = 8;
  376. /* low signal gain */
  377. if (x_est[6] == x_est[3])
  378. return false;
  379. G_fxp =
  380. (((Y[6] - Y[3]) * 1 << scale_factor) +
  381. (x_est[6] - x_est[3])) / (x_est[6] - x_est[3]);
  382. /* prevent division by zero */
  383. if (G_fxp == 0)
  384. return false;
  385. Y_intercept =
  386. (G_fxp * (x_est[0] - x_est[3]) +
  387. (1 << scale_factor)) / (1 << scale_factor) + Y[3];
  388. for (i = 0; i <= max_index; i++)
  389. y_est[i] = Y[i] - Y_intercept;
  390. for (i = 0; i <= 3; i++) {
  391. y_est[i] = i * 32;
  392. x_est[i] = ((y_est[i] * 1 << scale_factor) + G_fxp) / G_fxp;
  393. }
  394. if (y_est[max_index] == 0)
  395. return false;
  396. x_est_fxp1_nonlin =
  397. x_est[max_index] - ((1 << scale_factor) * y_est[max_index] +
  398. G_fxp) / G_fxp;
  399. order_x_by_y =
  400. (x_est_fxp1_nonlin + y_est[max_index]) / y_est[max_index];
  401. if (order_x_by_y == 0)
  402. M = 10;
  403. else if (order_x_by_y == 1)
  404. M = 9;
  405. else
  406. M = 8;
  407. I = (max_index > 15) ? 7 : max_index >> 1;
  408. L = max_index - I;
  409. scale_factor = 8;
  410. sum_y_sqr = 0;
  411. sum_y_quad = 0;
  412. x_tilde_abs = 0;
  413. for (i = 0; i <= L; i++) {
  414. unsigned int y_sqr;
  415. unsigned int y_quad;
  416. unsigned int tmp_abs;
  417. /* prevent division by zero */
  418. if (y_est[i + I] == 0)
  419. return false;
  420. x_est_fxp1_nonlin =
  421. x_est[i + I] - ((1 << scale_factor) * y_est[i + I] +
  422. G_fxp) / G_fxp;
  423. x_tilde[i] =
  424. (x_est_fxp1_nonlin * (1 << M) + y_est[i + I]) / y_est[i +
  425. I];
  426. x_tilde[i] =
  427. (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
  428. x_tilde[i] =
  429. (x_tilde[i] * (1 << M) + y_est[i + I]) / y_est[i + I];
  430. y_sqr =
  431. (y_est[i + I] * y_est[i + I] +
  432. (scale_factor * scale_factor)) / (scale_factor *
  433. scale_factor);
  434. tmp_abs = abs(x_tilde[i]);
  435. if (tmp_abs > x_tilde_abs)
  436. x_tilde_abs = tmp_abs;
  437. y_quad = y_sqr * y_sqr;
  438. sum_y_sqr = sum_y_sqr + y_sqr;
  439. sum_y_quad = sum_y_quad + y_quad;
  440. B1_tmp[i] = y_sqr * (L + 1);
  441. B2_tmp[i] = y_sqr;
  442. }
  443. B1_abs_max = 0;
  444. B2_abs_max = 0;
  445. for (i = 0; i <= L; i++) {
  446. int abs_val;
  447. B1_tmp[i] -= sum_y_sqr;
  448. B2_tmp[i] = sum_y_quad - sum_y_sqr * B2_tmp[i];
  449. abs_val = abs(B1_tmp[i]);
  450. if (abs_val > B1_abs_max)
  451. B1_abs_max = abs_val;
  452. abs_val = abs(B2_tmp[i]);
  453. if (abs_val > B2_abs_max)
  454. B2_abs_max = abs_val;
  455. }
  456. Q_x = find_proper_scale(find_expn(x_tilde_abs), 10);
  457. Q_B1 = find_proper_scale(find_expn(B1_abs_max), 10);
  458. Q_B2 = find_proper_scale(find_expn(B2_abs_max), 10);
  459. beta_raw = 0;
  460. alpha_raw = 0;
  461. for (i = 0; i <= L; i++) {
  462. x_tilde[i] = x_tilde[i] / (1 << Q_x);
  463. B1_tmp[i] = B1_tmp[i] / (1 << Q_B1);
  464. B2_tmp[i] = B2_tmp[i] / (1 << Q_B2);
  465. beta_raw = beta_raw + B1_tmp[i] * x_tilde[i];
  466. alpha_raw = alpha_raw + B2_tmp[i] * x_tilde[i];
  467. }
  468. scale_B =
  469. ((sum_y_quad / scale_factor) * (L + 1) -
  470. (sum_y_sqr / scale_factor) * sum_y_sqr) * scale_factor;
  471. Q_scale_B = find_proper_scale(find_expn(abs(scale_B)), 10);
  472. scale_B = scale_B / (1 << Q_scale_B);
  473. if (scale_B == 0)
  474. return false;
  475. Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
  476. Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
  477. beta_raw = beta_raw / (1 << Q_beta);
  478. alpha_raw = alpha_raw / (1 << Q_alpha);
  479. alpha = (alpha_raw << 10) / scale_B;
  480. beta = (beta_raw << 10) / scale_B;
  481. order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B;
  482. order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B;
  483. order1_5x = order_1 / 5;
  484. order2_3x = order_2 / 3;
  485. order1_5x_rem = order_1 - 5 * order1_5x;
  486. order2_3x_rem = order_2 - 3 * order2_3x;
  487. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  488. tmp = i * 32;
  489. y5 = ((beta * tmp) >> 6) >> order1_5x;
  490. y5 = (y5 * tmp) >> order1_5x;
  491. y5 = (y5 * tmp) >> order1_5x;
  492. y5 = (y5 * tmp) >> order1_5x;
  493. y5 = (y5 * tmp) >> order1_5x;
  494. y5 = y5 >> order1_5x_rem;
  495. y3 = (alpha * tmp) >> order2_3x;
  496. y3 = (y3 * tmp) >> order2_3x;
  497. y3 = (y3 * tmp) >> order2_3x;
  498. y3 = y3 >> order2_3x_rem;
  499. PA_in[i] = y5 + y3 + (256 * tmp) / G_fxp;
  500. if (i >= 2) {
  501. tmp = PA_in[i] - PA_in[i - 1];
  502. if (tmp < 0)
  503. PA_in[i] =
  504. PA_in[i - 1] + (PA_in[i - 1] -
  505. PA_in[i - 2]);
  506. }
  507. PA_in[i] = (PA_in[i] < 1400) ? PA_in[i] : 1400;
  508. }
  509. beta_raw = 0;
  510. alpha_raw = 0;
  511. for (i = 0; i <= L; i++) {
  512. int theta_tilde =
  513. ((theta[i + I] << M) + y_est[i + I]) / y_est[i + I];
  514. theta_tilde =
  515. ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
  516. theta_tilde =
  517. ((theta_tilde << M) + y_est[i + I]) / y_est[i + I];
  518. beta_raw = beta_raw + B1_tmp[i] * theta_tilde;
  519. alpha_raw = alpha_raw + B2_tmp[i] * theta_tilde;
  520. }
  521. Q_beta = find_proper_scale(find_expn(abs(beta_raw)), 10);
  522. Q_alpha = find_proper_scale(find_expn(abs(alpha_raw)), 10);
  523. beta_raw = beta_raw / (1 << Q_beta);
  524. alpha_raw = alpha_raw / (1 << Q_alpha);
  525. alpha = (alpha_raw << 10) / scale_B;
  526. beta = (beta_raw << 10) / scale_B;
  527. order_1 = 3 * M - Q_x - Q_B1 - Q_beta + 10 + Q_scale_B + 5;
  528. order_2 = 3 * M - Q_x - Q_B2 - Q_alpha + 10 + Q_scale_B + 5;
  529. order1_5x = order_1 / 5;
  530. order2_3x = order_2 / 3;
  531. order1_5x_rem = order_1 - 5 * order1_5x;
  532. order2_3x_rem = order_2 - 3 * order2_3x;
  533. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  534. int PA_angle;
  535. /* pa_table[4] is calculated from PA_angle for i=5 */
  536. if (i == 4)
  537. continue;
  538. tmp = i * 32;
  539. if (beta > 0)
  540. y5 = (((beta * tmp - 64) >> 6) -
  541. (1 << order1_5x)) / (1 << order1_5x);
  542. else
  543. y5 = ((((beta * tmp - 64) >> 6) +
  544. (1 << order1_5x)) / (1 << order1_5x));
  545. y5 = (y5 * tmp) / (1 << order1_5x);
  546. y5 = (y5 * tmp) / (1 << order1_5x);
  547. y5 = (y5 * tmp) / (1 << order1_5x);
  548. y5 = (y5 * tmp) / (1 << order1_5x);
  549. y5 = y5 / (1 << order1_5x_rem);
  550. if (beta > 0)
  551. y3 = (alpha * tmp -
  552. (1 << order2_3x)) / (1 << order2_3x);
  553. else
  554. y3 = (alpha * tmp +
  555. (1 << order2_3x)) / (1 << order2_3x);
  556. y3 = (y3 * tmp) / (1 << order2_3x);
  557. y3 = (y3 * tmp) / (1 << order2_3x);
  558. y3 = y3 / (1 << order2_3x_rem);
  559. if (i < 4) {
  560. PA_angle = 0;
  561. } else {
  562. PA_angle = y5 + y3;
  563. if (PA_angle < -150)
  564. PA_angle = -150;
  565. else if (PA_angle > 150)
  566. PA_angle = 150;
  567. }
  568. pa_table[i] = ((PA_in[i] & 0x7ff) << 11) + (PA_angle & 0x7ff);
  569. if (i == 5) {
  570. PA_angle = (PA_angle + 2) >> 1;
  571. pa_table[i - 1] = ((PA_in[i - 1] & 0x7ff) << 11) +
  572. (PA_angle & 0x7ff);
  573. }
  574. }
  575. *gain = G_fxp;
  576. return true;
  577. }
  578. void ar9003_paprd_populate_single_table(struct ath_hw *ah,
  579. struct ath9k_hw_cal_data *caldata,
  580. int chain)
  581. {
  582. u32 *paprd_table_val = caldata->pa_table[chain];
  583. u32 small_signal_gain = caldata->small_signal_gain[chain];
  584. u32 training_power = ah->paprd_training_power;
  585. u32 reg = 0;
  586. int i;
  587. if (chain == 0)
  588. reg = AR_PHY_PAPRD_MEM_TAB_B0;
  589. else if (chain == 1)
  590. reg = AR_PHY_PAPRD_MEM_TAB_B1;
  591. else if (chain == 2)
  592. reg = AR_PHY_PAPRD_MEM_TAB_B2;
  593. for (i = 0; i < PAPRD_TABLE_SZ; i++) {
  594. REG_WRITE(ah, reg, paprd_table_val[i]);
  595. reg = reg + 4;
  596. }
  597. if (chain == 0)
  598. reg = AR_PHY_PA_GAIN123_B0;
  599. else if (chain == 1)
  600. reg = AR_PHY_PA_GAIN123_B1;
  601. else
  602. reg = AR_PHY_PA_GAIN123_B2;
  603. REG_RMW_FIELD(ah, reg, AR_PHY_PA_GAIN123_PA_GAIN1, small_signal_gain);
  604. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B0,
  605. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  606. training_power);
  607. if (ah->caps.tx_chainmask & BIT(1))
  608. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B1,
  609. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  610. training_power);
  611. if (ah->caps.tx_chainmask & BIT(2))
  612. REG_RMW_FIELD(ah, AR_PHY_PAPRD_CTRL1_B2,
  613. AR_PHY_PAPRD_CTRL1_PAPRD_POWER_AT_AM2AM_CAL,
  614. training_power);
  615. }
  616. EXPORT_SYMBOL(ar9003_paprd_populate_single_table);
  617. int ar9003_paprd_setup_gain_table(struct ath_hw *ah, int chain)
  618. {
  619. unsigned int i, desired_gain, gain_index;
  620. unsigned int train_power = ah->paprd_training_power;
  621. desired_gain = ar9003_get_desired_gain(ah, chain, train_power);
  622. gain_index = 0;
  623. for (i = 0; i < 32; i++) {
  624. if (ah->paprd_gain_table_index[i] >= desired_gain)
  625. break;
  626. gain_index++;
  627. }
  628. ar9003_tx_force_gain(ah, gain_index);
  629. REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  630. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  631. return 0;
  632. }
  633. EXPORT_SYMBOL(ar9003_paprd_setup_gain_table);
  634. int ar9003_paprd_create_curve(struct ath_hw *ah,
  635. struct ath9k_hw_cal_data *caldata, int chain)
  636. {
  637. u16 *small_signal_gain = &caldata->small_signal_gain[chain];
  638. u32 *pa_table = caldata->pa_table[chain];
  639. u32 *data_L, *data_U;
  640. int i, status = 0;
  641. u32 *buf;
  642. u32 reg;
  643. memset(caldata->pa_table[chain], 0, sizeof(caldata->pa_table[chain]));
  644. buf = kmalloc(2 * 48 * sizeof(u32), GFP_ATOMIC);
  645. if (!buf)
  646. return -ENOMEM;
  647. data_L = &buf[0];
  648. data_U = &buf[48];
  649. REG_CLR_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
  650. AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
  651. reg = AR_PHY_CHAN_INFO_TAB_0;
  652. for (i = 0; i < 48; i++)
  653. data_L[i] = REG_READ(ah, reg + (i << 2));
  654. REG_SET_BIT(ah, AR_PHY_CHAN_INFO_MEMORY,
  655. AR_PHY_CHAN_INFO_MEMORY_CHANINFOMEM_S2_READ);
  656. for (i = 0; i < 48; i++)
  657. data_U[i] = REG_READ(ah, reg + (i << 2));
  658. if (!create_pa_curve(data_L, data_U, pa_table, small_signal_gain))
  659. status = -2;
  660. REG_CLR_BIT(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  661. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  662. kfree(buf);
  663. return status;
  664. }
  665. EXPORT_SYMBOL(ar9003_paprd_create_curve);
  666. int ar9003_paprd_init_table(struct ath_hw *ah)
  667. {
  668. int ret;
  669. ret = ar9003_paprd_setup_single_table(ah);
  670. if (ret < 0)
  671. return ret;
  672. ar9003_paprd_get_gain_table(ah);
  673. return 0;
  674. }
  675. EXPORT_SYMBOL(ar9003_paprd_init_table);
  676. bool ar9003_paprd_is_done(struct ath_hw *ah)
  677. {
  678. int paprd_done, agc2_pwr;
  679. paprd_done = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  680. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_TRAIN_DONE);
  681. if (paprd_done == 0x1) {
  682. agc2_pwr = REG_READ_FIELD(ah, AR_PHY_PAPRD_TRAINER_STAT1,
  683. AR_PHY_PAPRD_TRAINER_STAT1_PAPRD_AGC2_PWR);
  684. ath_dbg(ath9k_hw_common(ah), ATH_DBG_CALIBRATE,
  685. "AGC2_PWR = 0x%x training done = 0x%x\n",
  686. agc2_pwr, paprd_done);
  687. /*
  688. * agc2_pwr range should not be less than 'IDEAL_AGC2_PWR_CHANGE'
  689. * when the training is completely done, otherwise retraining is
  690. * done to make sure the value is in ideal range
  691. */
  692. if (agc2_pwr <= PAPRD_IDEAL_AGC2_PWR_RANGE)
  693. paprd_done = 0;
  694. }
  695. return !!paprd_done;
  696. }
  697. EXPORT_SYMBOL(ar9003_paprd_is_done);