ar9003_paprd.c 25 KB

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