eeprom.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. /*
  2. * Copyright (c) 2008-2009 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. static inline u16 ath9k_hw_fbin2freq(u8 fbin, bool is2GHz)
  18. {
  19. if (fbin == AR5416_BCHAN_UNUSED)
  20. return fbin;
  21. return (u16) ((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
  22. }
  23. void ath9k_hw_analog_shift_regwrite(struct ath_hw *ah, u32 reg, u32 val)
  24. {
  25. REG_WRITE(ah, reg, val);
  26. if (ah->config.analog_shiftreg)
  27. udelay(100);
  28. }
  29. void ath9k_hw_analog_shift_rmw(struct ath_hw *ah, u32 reg, u32 mask,
  30. u32 shift, u32 val)
  31. {
  32. u32 regVal;
  33. regVal = REG_READ(ah, reg) & ~mask;
  34. regVal |= (val << shift) & mask;
  35. REG_WRITE(ah, reg, regVal);
  36. if (ah->config.analog_shiftreg)
  37. udelay(100);
  38. }
  39. int16_t ath9k_hw_interpolate(u16 target, u16 srcLeft, u16 srcRight,
  40. int16_t targetLeft, int16_t targetRight)
  41. {
  42. int16_t rv;
  43. if (srcRight == srcLeft) {
  44. rv = targetLeft;
  45. } else {
  46. rv = (int16_t) (((target - srcLeft) * targetRight +
  47. (srcRight - target) * targetLeft) /
  48. (srcRight - srcLeft));
  49. }
  50. return rv;
  51. }
  52. bool ath9k_hw_get_lower_upper_index(u8 target, u8 *pList, u16 listSize,
  53. u16 *indexL, u16 *indexR)
  54. {
  55. u16 i;
  56. if (target <= pList[0]) {
  57. *indexL = *indexR = 0;
  58. return true;
  59. }
  60. if (target >= pList[listSize - 1]) {
  61. *indexL = *indexR = (u16) (listSize - 1);
  62. return true;
  63. }
  64. for (i = 0; i < listSize - 1; i++) {
  65. if (pList[i] == target) {
  66. *indexL = *indexR = i;
  67. return true;
  68. }
  69. if (target < pList[i + 1]) {
  70. *indexL = i;
  71. *indexR = (u16) (i + 1);
  72. return false;
  73. }
  74. }
  75. return false;
  76. }
  77. bool ath9k_hw_nvram_read(struct ath_common *common, u32 off, u16 *data)
  78. {
  79. return common->bus_ops->eeprom_read(common, off, data);
  80. }
  81. void ath9k_hw_fill_vpd_table(u8 pwrMin, u8 pwrMax, u8 *pPwrList,
  82. u8 *pVpdList, u16 numIntercepts,
  83. u8 *pRetVpdList)
  84. {
  85. u16 i, k;
  86. u8 currPwr = pwrMin;
  87. u16 idxL = 0, idxR = 0;
  88. for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
  89. ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
  90. numIntercepts, &(idxL),
  91. &(idxR));
  92. if (idxR < 1)
  93. idxR = 1;
  94. if (idxL == numIntercepts - 1)
  95. idxL = (u16) (numIntercepts - 2);
  96. if (pPwrList[idxL] == pPwrList[idxR])
  97. k = pVpdList[idxL];
  98. else
  99. k = (u16)(((currPwr - pPwrList[idxL]) * pVpdList[idxR] +
  100. (pPwrList[idxR] - currPwr) * pVpdList[idxL]) /
  101. (pPwrList[idxR] - pPwrList[idxL]));
  102. pRetVpdList[i] = (u8) k;
  103. currPwr += 2;
  104. }
  105. }
  106. void ath9k_hw_get_legacy_target_powers(struct ath_hw *ah,
  107. struct ath9k_channel *chan,
  108. struct cal_target_power_leg *powInfo,
  109. u16 numChannels,
  110. struct cal_target_power_leg *pNewPower,
  111. u16 numRates, bool isExtTarget)
  112. {
  113. struct chan_centers centers;
  114. u16 clo, chi;
  115. int i;
  116. int matchIndex = -1, lowIndex = -1;
  117. u16 freq;
  118. ath9k_hw_get_channel_centers(ah, chan, &centers);
  119. freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
  120. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
  121. IS_CHAN_2GHZ(chan))) {
  122. matchIndex = 0;
  123. } else {
  124. for (i = 0; (i < numChannels) &&
  125. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  126. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  127. IS_CHAN_2GHZ(chan))) {
  128. matchIndex = i;
  129. break;
  130. } else if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  131. IS_CHAN_2GHZ(chan)) && i > 0 &&
  132. freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  133. IS_CHAN_2GHZ(chan))) {
  134. lowIndex = i - 1;
  135. break;
  136. }
  137. }
  138. if ((matchIndex == -1) && (lowIndex == -1))
  139. matchIndex = i - 1;
  140. }
  141. if (matchIndex != -1) {
  142. *pNewPower = powInfo[matchIndex];
  143. } else {
  144. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  145. IS_CHAN_2GHZ(chan));
  146. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  147. IS_CHAN_2GHZ(chan));
  148. for (i = 0; i < numRates; i++) {
  149. pNewPower->tPow2x[i] =
  150. (u8)ath9k_hw_interpolate(freq, clo, chi,
  151. powInfo[lowIndex].tPow2x[i],
  152. powInfo[lowIndex + 1].tPow2x[i]);
  153. }
  154. }
  155. }
  156. void ath9k_hw_get_target_powers(struct ath_hw *ah,
  157. struct ath9k_channel *chan,
  158. struct cal_target_power_ht *powInfo,
  159. u16 numChannels,
  160. struct cal_target_power_ht *pNewPower,
  161. u16 numRates, bool isHt40Target)
  162. {
  163. struct chan_centers centers;
  164. u16 clo, chi;
  165. int i;
  166. int matchIndex = -1, lowIndex = -1;
  167. u16 freq;
  168. ath9k_hw_get_channel_centers(ah, chan, &centers);
  169. freq = isHt40Target ? centers.synth_center : centers.ctl_center;
  170. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
  171. matchIndex = 0;
  172. } else {
  173. for (i = 0; (i < numChannels) &&
  174. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  175. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  176. IS_CHAN_2GHZ(chan))) {
  177. matchIndex = i;
  178. break;
  179. } else
  180. if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  181. IS_CHAN_2GHZ(chan)) && i > 0 &&
  182. freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  183. IS_CHAN_2GHZ(chan))) {
  184. lowIndex = i - 1;
  185. break;
  186. }
  187. }
  188. if ((matchIndex == -1) && (lowIndex == -1))
  189. matchIndex = i - 1;
  190. }
  191. if (matchIndex != -1) {
  192. *pNewPower = powInfo[matchIndex];
  193. } else {
  194. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  195. IS_CHAN_2GHZ(chan));
  196. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  197. IS_CHAN_2GHZ(chan));
  198. for (i = 0; i < numRates; i++) {
  199. pNewPower->tPow2x[i] = (u8)ath9k_hw_interpolate(freq,
  200. clo, chi,
  201. powInfo[lowIndex].tPow2x[i],
  202. powInfo[lowIndex + 1].tPow2x[i]);
  203. }
  204. }
  205. }
  206. u16 ath9k_hw_get_max_edge_power(u16 freq, struct cal_ctl_edges *pRdEdgesPower,
  207. bool is2GHz, int num_band_edges)
  208. {
  209. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  210. int i;
  211. for (i = 0; (i < num_band_edges) &&
  212. (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  213. if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel, is2GHz)) {
  214. twiceMaxEdgePower = pRdEdgesPower[i].tPower;
  215. break;
  216. } else if ((i > 0) &&
  217. (freq < ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
  218. is2GHz))) {
  219. if (ath9k_hw_fbin2freq(pRdEdgesPower[i - 1].bChannel,
  220. is2GHz) < freq &&
  221. pRdEdgesPower[i - 1].flag) {
  222. twiceMaxEdgePower =
  223. pRdEdgesPower[i - 1].tPower;
  224. }
  225. break;
  226. }
  227. }
  228. return twiceMaxEdgePower;
  229. }
  230. void ath9k_hw_update_regulatory_maxpower(struct ath_hw *ah)
  231. {
  232. struct ath_common *common = ath9k_hw_common(ah);
  233. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  234. switch (ar5416_get_ntxchains(ah->txchainmask)) {
  235. case 1:
  236. break;
  237. case 2:
  238. regulatory->max_power_level += INCREASE_MAXPOW_BY_TWO_CHAIN;
  239. break;
  240. case 3:
  241. regulatory->max_power_level += INCREASE_MAXPOW_BY_THREE_CHAIN;
  242. break;
  243. default:
  244. ath_print(common, ATH_DBG_EEPROM,
  245. "Invalid chainmask configuration\n");
  246. break;
  247. }
  248. }
  249. int ath9k_hw_eeprom_init(struct ath_hw *ah)
  250. {
  251. int status;
  252. if (AR_SREV_9300_20_OR_LATER(ah))
  253. ah->eep_ops = &eep_ar9300_ops;
  254. else if (AR_SREV_9287(ah)) {
  255. ah->eep_ops = &eep_ar9287_ops;
  256. } else if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
  257. ah->eep_ops = &eep_4k_ops;
  258. } else {
  259. ah->eep_ops = &eep_def_ops;
  260. }
  261. if (!ah->eep_ops->fill_eeprom(ah))
  262. return -EIO;
  263. status = ah->eep_ops->check_eeprom(ah);
  264. return status;
  265. }