ar9003_paprd.c 21 KB

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