eeprom.c 7.1 KB

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