ar9003_paprd.c 23 KB

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