ar9003_paprd.c 23 KB

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