ar9003_paprd.c 21 KB

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