ar9003_paprd.c 23 KB

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