ar9003_paprd.c 23 KB

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