eeprom.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515
  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 = 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 = CTL_EDGE_TPOWER(pRdEdgesPower[i].ctl);
  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. CTL_EDGE_FLAGS(pRdEdgesPower[i - 1].ctl)) {
  222. twiceMaxEdgePower =
  223. CTL_EDGE_TPOWER(pRdEdgesPower[i - 1].ctl);
  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_dbg(common, ATH_DBG_EEPROM,
  245. "Invalid chainmask configuration\n");
  246. break;
  247. }
  248. }
  249. void ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hw *ah,
  250. struct ath9k_channel *chan,
  251. void *pRawDataSet,
  252. u8 *bChans, u16 availPiers,
  253. u16 tPdGainOverlap,
  254. u16 *pPdGainBoundaries, u8 *pPDADCValues,
  255. u16 numXpdGains)
  256. {
  257. int i, j, k;
  258. int16_t ss;
  259. u16 idxL = 0, idxR = 0, numPiers;
  260. static u8 vpdTableL[AR5416_NUM_PD_GAINS]
  261. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  262. static u8 vpdTableR[AR5416_NUM_PD_GAINS]
  263. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  264. static u8 vpdTableI[AR5416_NUM_PD_GAINS]
  265. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  266. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  267. u8 minPwrT4[AR5416_NUM_PD_GAINS];
  268. u8 maxPwrT4[AR5416_NUM_PD_GAINS];
  269. int16_t vpdStep;
  270. int16_t tmpVal;
  271. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  272. bool match;
  273. int16_t minDelta = 0;
  274. struct chan_centers centers;
  275. int pdgain_boundary_default;
  276. struct cal_data_per_freq *data_def = pRawDataSet;
  277. struct cal_data_per_freq_4k *data_4k = pRawDataSet;
  278. struct cal_data_per_freq_ar9287 *data_9287 = pRawDataSet;
  279. bool eeprom_4k = AR_SREV_9285(ah) || AR_SREV_9271(ah);
  280. int intercepts;
  281. if (AR_SREV_9287(ah))
  282. intercepts = AR9287_PD_GAIN_ICEPTS;
  283. else
  284. intercepts = AR5416_PD_GAIN_ICEPTS;
  285. memset(&minPwrT4, 0, AR5416_NUM_PD_GAINS);
  286. ath9k_hw_get_channel_centers(ah, chan, &centers);
  287. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  288. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  289. break;
  290. }
  291. match = ath9k_hw_get_lower_upper_index((u8)FREQ2FBIN(centers.synth_center,
  292. IS_CHAN_2GHZ(chan)),
  293. bChans, numPiers, &idxL, &idxR);
  294. if (match) {
  295. if (AR_SREV_9287(ah)) {
  296. /* FIXME: array overrun? */
  297. for (i = 0; i < numXpdGains; i++) {
  298. minPwrT4[i] = data_9287[idxL].pwrPdg[i][0];
  299. maxPwrT4[i] = data_9287[idxL].pwrPdg[i][4];
  300. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  301. data_9287[idxL].pwrPdg[i],
  302. data_9287[idxL].vpdPdg[i],
  303. intercepts,
  304. vpdTableI[i]);
  305. }
  306. } else if (eeprom_4k) {
  307. for (i = 0; i < numXpdGains; i++) {
  308. minPwrT4[i] = data_4k[idxL].pwrPdg[i][0];
  309. maxPwrT4[i] = data_4k[idxL].pwrPdg[i][4];
  310. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  311. data_4k[idxL].pwrPdg[i],
  312. data_4k[idxL].vpdPdg[i],
  313. intercepts,
  314. vpdTableI[i]);
  315. }
  316. } else {
  317. for (i = 0; i < numXpdGains; i++) {
  318. minPwrT4[i] = data_def[idxL].pwrPdg[i][0];
  319. maxPwrT4[i] = data_def[idxL].pwrPdg[i][4];
  320. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  321. data_def[idxL].pwrPdg[i],
  322. data_def[idxL].vpdPdg[i],
  323. intercepts,
  324. vpdTableI[i]);
  325. }
  326. }
  327. } else {
  328. for (i = 0; i < numXpdGains; i++) {
  329. if (AR_SREV_9287(ah)) {
  330. pVpdL = data_9287[idxL].vpdPdg[i];
  331. pPwrL = data_9287[idxL].pwrPdg[i];
  332. pVpdR = data_9287[idxR].vpdPdg[i];
  333. pPwrR = data_9287[idxR].pwrPdg[i];
  334. } else if (eeprom_4k) {
  335. pVpdL = data_4k[idxL].vpdPdg[i];
  336. pPwrL = data_4k[idxL].pwrPdg[i];
  337. pVpdR = data_4k[idxR].vpdPdg[i];
  338. pPwrR = data_4k[idxR].pwrPdg[i];
  339. } else {
  340. pVpdL = data_def[idxL].vpdPdg[i];
  341. pPwrL = data_def[idxL].pwrPdg[i];
  342. pVpdR = data_def[idxR].vpdPdg[i];
  343. pPwrR = data_def[idxR].pwrPdg[i];
  344. }
  345. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  346. maxPwrT4[i] =
  347. min(pPwrL[intercepts - 1],
  348. pPwrR[intercepts - 1]);
  349. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  350. pPwrL, pVpdL,
  351. intercepts,
  352. vpdTableL[i]);
  353. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  354. pPwrR, pVpdR,
  355. intercepts,
  356. vpdTableR[i]);
  357. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  358. vpdTableI[i][j] =
  359. (u8)(ath9k_hw_interpolate((u16)
  360. FREQ2FBIN(centers.
  361. synth_center,
  362. IS_CHAN_2GHZ
  363. (chan)),
  364. bChans[idxL], bChans[idxR],
  365. vpdTableL[i][j], vpdTableR[i][j]));
  366. }
  367. }
  368. }
  369. k = 0;
  370. for (i = 0; i < numXpdGains; i++) {
  371. if (i == (numXpdGains - 1))
  372. pPdGainBoundaries[i] =
  373. (u16)(maxPwrT4[i] / 2);
  374. else
  375. pPdGainBoundaries[i] =
  376. (u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);
  377. pPdGainBoundaries[i] =
  378. min((u16)MAX_RATE_POWER, pPdGainBoundaries[i]);
  379. if ((i == 0) && !AR_SREV_5416_20_OR_LATER(ah)) {
  380. minDelta = pPdGainBoundaries[0] - 23;
  381. pPdGainBoundaries[0] = 23;
  382. } else {
  383. minDelta = 0;
  384. }
  385. if (i == 0) {
  386. if (AR_SREV_9280_20_OR_LATER(ah))
  387. ss = (int16_t)(0 - (minPwrT4[i] / 2));
  388. else
  389. ss = 0;
  390. } else {
  391. ss = (int16_t)((pPdGainBoundaries[i - 1] -
  392. (minPwrT4[i] / 2)) -
  393. tPdGainOverlap + 1 + minDelta);
  394. }
  395. vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
  396. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  397. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  398. tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
  399. pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
  400. ss++;
  401. }
  402. sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  403. tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
  404. (minPwrT4[i] / 2));
  405. maxIndex = (tgtIndex < sizeCurrVpdTable) ?
  406. tgtIndex : sizeCurrVpdTable;
  407. while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  408. pPDADCValues[k++] = vpdTableI[i][ss++];
  409. }
  410. vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
  411. vpdTableI[i][sizeCurrVpdTable - 2]);
  412. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  413. if (tgtIndex >= maxIndex) {
  414. while ((ss <= tgtIndex) &&
  415. (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  416. tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
  417. (ss - maxIndex + 1) * vpdStep));
  418. pPDADCValues[k++] = (u8)((tmpVal > 255) ?
  419. 255 : tmpVal);
  420. ss++;
  421. }
  422. }
  423. }
  424. if (eeprom_4k)
  425. pdgain_boundary_default = 58;
  426. else
  427. pdgain_boundary_default = pPdGainBoundaries[i - 1];
  428. while (i < AR5416_PD_GAINS_IN_MASK) {
  429. pPdGainBoundaries[i] = pdgain_boundary_default;
  430. i++;
  431. }
  432. while (k < AR5416_NUM_PDADC_VALUES) {
  433. pPDADCValues[k] = pPDADCValues[k - 1];
  434. k++;
  435. }
  436. }
  437. int ath9k_hw_eeprom_init(struct ath_hw *ah)
  438. {
  439. int status;
  440. if (AR_SREV_9300_20_OR_LATER(ah))
  441. ah->eep_ops = &eep_ar9300_ops;
  442. else if (AR_SREV_9287(ah)) {
  443. ah->eep_ops = &eep_ar9287_ops;
  444. } else if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
  445. ah->eep_ops = &eep_4k_ops;
  446. } else {
  447. ah->eep_ops = &eep_def_ops;
  448. }
  449. if (!ah->eep_ops->fill_eeprom(ah))
  450. return -EIO;
  451. status = ah->eep_ops->check_eeprom(ah);
  452. return status;
  453. }