eeprom.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547
  1. /*
  2. * Copyright (c) 2008-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. 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. void ath9k_hw_usb_gen_fill_eeprom(struct ath_hw *ah, u16 *eep_data,
  78. int eep_start_loc, int size)
  79. {
  80. int i = 0, j, addr;
  81. u32 addrdata[8];
  82. u32 data[8];
  83. for (addr = 0; addr < size; addr++) {
  84. addrdata[i] = AR5416_EEPROM_OFFSET +
  85. ((addr + eep_start_loc) << AR5416_EEPROM_S);
  86. i++;
  87. if (i == 8) {
  88. REG_READ_MULTI(ah, addrdata, data, i);
  89. for (j = 0; j < i; j++) {
  90. *eep_data = data[j];
  91. eep_data++;
  92. }
  93. i = 0;
  94. }
  95. }
  96. if (i != 0) {
  97. REG_READ_MULTI(ah, addrdata, data, i);
  98. for (j = 0; j < i; j++) {
  99. *eep_data = data[j];
  100. eep_data++;
  101. }
  102. }
  103. }
  104. bool ath9k_hw_nvram_read(struct ath_common *common, u32 off, u16 *data)
  105. {
  106. return common->bus_ops->eeprom_read(common, off, data);
  107. }
  108. void ath9k_hw_fill_vpd_table(u8 pwrMin, u8 pwrMax, u8 *pPwrList,
  109. u8 *pVpdList, u16 numIntercepts,
  110. u8 *pRetVpdList)
  111. {
  112. u16 i, k;
  113. u8 currPwr = pwrMin;
  114. u16 idxL = 0, idxR = 0;
  115. for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
  116. ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
  117. numIntercepts, &(idxL),
  118. &(idxR));
  119. if (idxR < 1)
  120. idxR = 1;
  121. if (idxL == numIntercepts - 1)
  122. idxL = (u16) (numIntercepts - 2);
  123. if (pPwrList[idxL] == pPwrList[idxR])
  124. k = pVpdList[idxL];
  125. else
  126. k = (u16)(((currPwr - pPwrList[idxL]) * pVpdList[idxR] +
  127. (pPwrList[idxR] - currPwr) * pVpdList[idxL]) /
  128. (pPwrList[idxR] - pPwrList[idxL]));
  129. pRetVpdList[i] = (u8) k;
  130. currPwr += 2;
  131. }
  132. }
  133. void ath9k_hw_get_legacy_target_powers(struct ath_hw *ah,
  134. struct ath9k_channel *chan,
  135. struct cal_target_power_leg *powInfo,
  136. u16 numChannels,
  137. struct cal_target_power_leg *pNewPower,
  138. u16 numRates, bool isExtTarget)
  139. {
  140. struct chan_centers centers;
  141. u16 clo, chi;
  142. int i;
  143. int matchIndex = -1, lowIndex = -1;
  144. u16 freq;
  145. ath9k_hw_get_channel_centers(ah, chan, &centers);
  146. freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
  147. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
  148. IS_CHAN_2GHZ(chan))) {
  149. matchIndex = 0;
  150. } else {
  151. for (i = 0; (i < numChannels) &&
  152. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  153. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  154. IS_CHAN_2GHZ(chan))) {
  155. matchIndex = i;
  156. break;
  157. } else if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  158. IS_CHAN_2GHZ(chan)) && i > 0 &&
  159. freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  160. IS_CHAN_2GHZ(chan))) {
  161. lowIndex = i - 1;
  162. break;
  163. }
  164. }
  165. if ((matchIndex == -1) && (lowIndex == -1))
  166. matchIndex = i - 1;
  167. }
  168. if (matchIndex != -1) {
  169. *pNewPower = powInfo[matchIndex];
  170. } else {
  171. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  172. IS_CHAN_2GHZ(chan));
  173. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  174. IS_CHAN_2GHZ(chan));
  175. for (i = 0; i < numRates; i++) {
  176. pNewPower->tPow2x[i] =
  177. (u8)ath9k_hw_interpolate(freq, clo, chi,
  178. powInfo[lowIndex].tPow2x[i],
  179. powInfo[lowIndex + 1].tPow2x[i]);
  180. }
  181. }
  182. }
  183. void ath9k_hw_get_target_powers(struct ath_hw *ah,
  184. struct ath9k_channel *chan,
  185. struct cal_target_power_ht *powInfo,
  186. u16 numChannels,
  187. struct cal_target_power_ht *pNewPower,
  188. u16 numRates, bool isHt40Target)
  189. {
  190. struct chan_centers centers;
  191. u16 clo, chi;
  192. int i;
  193. int matchIndex = -1, lowIndex = -1;
  194. u16 freq;
  195. ath9k_hw_get_channel_centers(ah, chan, &centers);
  196. freq = isHt40Target ? centers.synth_center : centers.ctl_center;
  197. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
  198. matchIndex = 0;
  199. } else {
  200. for (i = 0; (i < numChannels) &&
  201. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  202. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  203. IS_CHAN_2GHZ(chan))) {
  204. matchIndex = i;
  205. break;
  206. } else
  207. if (freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  208. IS_CHAN_2GHZ(chan)) && i > 0 &&
  209. freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  210. IS_CHAN_2GHZ(chan))) {
  211. lowIndex = i - 1;
  212. break;
  213. }
  214. }
  215. if ((matchIndex == -1) && (lowIndex == -1))
  216. matchIndex = i - 1;
  217. }
  218. if (matchIndex != -1) {
  219. *pNewPower = powInfo[matchIndex];
  220. } else {
  221. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  222. IS_CHAN_2GHZ(chan));
  223. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  224. IS_CHAN_2GHZ(chan));
  225. for (i = 0; i < numRates; i++) {
  226. pNewPower->tPow2x[i] = (u8)ath9k_hw_interpolate(freq,
  227. clo, chi,
  228. powInfo[lowIndex].tPow2x[i],
  229. powInfo[lowIndex + 1].tPow2x[i]);
  230. }
  231. }
  232. }
  233. u16 ath9k_hw_get_max_edge_power(u16 freq, struct cal_ctl_edges *pRdEdgesPower,
  234. bool is2GHz, int num_band_edges)
  235. {
  236. u16 twiceMaxEdgePower = MAX_RATE_POWER;
  237. int i;
  238. for (i = 0; (i < num_band_edges) &&
  239. (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  240. if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel, is2GHz)) {
  241. twiceMaxEdgePower = CTL_EDGE_TPOWER(pRdEdgesPower[i].ctl);
  242. break;
  243. } else if ((i > 0) &&
  244. (freq < ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
  245. is2GHz))) {
  246. if (ath9k_hw_fbin2freq(pRdEdgesPower[i - 1].bChannel,
  247. is2GHz) < freq &&
  248. CTL_EDGE_FLAGS(pRdEdgesPower[i - 1].ctl)) {
  249. twiceMaxEdgePower =
  250. CTL_EDGE_TPOWER(pRdEdgesPower[i - 1].ctl);
  251. }
  252. break;
  253. }
  254. }
  255. return twiceMaxEdgePower;
  256. }
  257. void ath9k_hw_update_regulatory_maxpower(struct ath_hw *ah)
  258. {
  259. struct ath_common *common = ath9k_hw_common(ah);
  260. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  261. switch (ar5416_get_ntxchains(ah->txchainmask)) {
  262. case 1:
  263. break;
  264. case 2:
  265. regulatory->max_power_level += INCREASE_MAXPOW_BY_TWO_CHAIN;
  266. break;
  267. case 3:
  268. regulatory->max_power_level += INCREASE_MAXPOW_BY_THREE_CHAIN;
  269. break;
  270. default:
  271. ath_dbg(common, ATH_DBG_EEPROM,
  272. "Invalid chainmask configuration\n");
  273. break;
  274. }
  275. }
  276. void ath9k_hw_get_gain_boundaries_pdadcs(struct ath_hw *ah,
  277. struct ath9k_channel *chan,
  278. void *pRawDataSet,
  279. u8 *bChans, u16 availPiers,
  280. u16 tPdGainOverlap,
  281. u16 *pPdGainBoundaries, u8 *pPDADCValues,
  282. u16 numXpdGains)
  283. {
  284. int i, j, k;
  285. int16_t ss;
  286. u16 idxL = 0, idxR = 0, numPiers;
  287. static u8 vpdTableL[AR5416_NUM_PD_GAINS]
  288. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  289. static u8 vpdTableR[AR5416_NUM_PD_GAINS]
  290. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  291. static u8 vpdTableI[AR5416_NUM_PD_GAINS]
  292. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  293. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  294. u8 minPwrT4[AR5416_NUM_PD_GAINS];
  295. u8 maxPwrT4[AR5416_NUM_PD_GAINS];
  296. int16_t vpdStep;
  297. int16_t tmpVal;
  298. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  299. bool match;
  300. int16_t minDelta = 0;
  301. struct chan_centers centers;
  302. int pdgain_boundary_default;
  303. struct cal_data_per_freq *data_def = pRawDataSet;
  304. struct cal_data_per_freq_4k *data_4k = pRawDataSet;
  305. struct cal_data_per_freq_ar9287 *data_9287 = pRawDataSet;
  306. bool eeprom_4k = AR_SREV_9285(ah) || AR_SREV_9271(ah);
  307. int intercepts;
  308. if (AR_SREV_9287(ah))
  309. intercepts = AR9287_PD_GAIN_ICEPTS;
  310. else
  311. intercepts = AR5416_PD_GAIN_ICEPTS;
  312. memset(&minPwrT4, 0, AR5416_NUM_PD_GAINS);
  313. ath9k_hw_get_channel_centers(ah, chan, &centers);
  314. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  315. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  316. break;
  317. }
  318. match = ath9k_hw_get_lower_upper_index((u8)FREQ2FBIN(centers.synth_center,
  319. IS_CHAN_2GHZ(chan)),
  320. bChans, numPiers, &idxL, &idxR);
  321. if (match) {
  322. if (AR_SREV_9287(ah)) {
  323. /* FIXME: array overrun? */
  324. for (i = 0; i < numXpdGains; i++) {
  325. minPwrT4[i] = data_9287[idxL].pwrPdg[i][0];
  326. maxPwrT4[i] = data_9287[idxL].pwrPdg[i][4];
  327. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  328. data_9287[idxL].pwrPdg[i],
  329. data_9287[idxL].vpdPdg[i],
  330. intercepts,
  331. vpdTableI[i]);
  332. }
  333. } else if (eeprom_4k) {
  334. for (i = 0; i < numXpdGains; i++) {
  335. minPwrT4[i] = data_4k[idxL].pwrPdg[i][0];
  336. maxPwrT4[i] = data_4k[idxL].pwrPdg[i][4];
  337. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  338. data_4k[idxL].pwrPdg[i],
  339. data_4k[idxL].vpdPdg[i],
  340. intercepts,
  341. vpdTableI[i]);
  342. }
  343. } else {
  344. for (i = 0; i < numXpdGains; i++) {
  345. minPwrT4[i] = data_def[idxL].pwrPdg[i][0];
  346. maxPwrT4[i] = data_def[idxL].pwrPdg[i][4];
  347. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  348. data_def[idxL].pwrPdg[i],
  349. data_def[idxL].vpdPdg[i],
  350. intercepts,
  351. vpdTableI[i]);
  352. }
  353. }
  354. } else {
  355. for (i = 0; i < numXpdGains; i++) {
  356. if (AR_SREV_9287(ah)) {
  357. pVpdL = data_9287[idxL].vpdPdg[i];
  358. pPwrL = data_9287[idxL].pwrPdg[i];
  359. pVpdR = data_9287[idxR].vpdPdg[i];
  360. pPwrR = data_9287[idxR].pwrPdg[i];
  361. } else if (eeprom_4k) {
  362. pVpdL = data_4k[idxL].vpdPdg[i];
  363. pPwrL = data_4k[idxL].pwrPdg[i];
  364. pVpdR = data_4k[idxR].vpdPdg[i];
  365. pPwrR = data_4k[idxR].pwrPdg[i];
  366. } else {
  367. pVpdL = data_def[idxL].vpdPdg[i];
  368. pPwrL = data_def[idxL].pwrPdg[i];
  369. pVpdR = data_def[idxR].vpdPdg[i];
  370. pPwrR = data_def[idxR].pwrPdg[i];
  371. }
  372. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  373. maxPwrT4[i] =
  374. min(pPwrL[intercepts - 1],
  375. pPwrR[intercepts - 1]);
  376. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  377. pPwrL, pVpdL,
  378. intercepts,
  379. vpdTableL[i]);
  380. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  381. pPwrR, pVpdR,
  382. intercepts,
  383. vpdTableR[i]);
  384. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  385. vpdTableI[i][j] =
  386. (u8)(ath9k_hw_interpolate((u16)
  387. FREQ2FBIN(centers.
  388. synth_center,
  389. IS_CHAN_2GHZ
  390. (chan)),
  391. bChans[idxL], bChans[idxR],
  392. vpdTableL[i][j], vpdTableR[i][j]));
  393. }
  394. }
  395. }
  396. k = 0;
  397. for (i = 0; i < numXpdGains; i++) {
  398. if (i == (numXpdGains - 1))
  399. pPdGainBoundaries[i] =
  400. (u16)(maxPwrT4[i] / 2);
  401. else
  402. pPdGainBoundaries[i] =
  403. (u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);
  404. pPdGainBoundaries[i] =
  405. min((u16)MAX_RATE_POWER, pPdGainBoundaries[i]);
  406. if ((i == 0) && !AR_SREV_5416_20_OR_LATER(ah)) {
  407. minDelta = pPdGainBoundaries[0] - 23;
  408. pPdGainBoundaries[0] = 23;
  409. } else {
  410. minDelta = 0;
  411. }
  412. if (i == 0) {
  413. if (AR_SREV_9280_20_OR_LATER(ah))
  414. ss = (int16_t)(0 - (minPwrT4[i] / 2));
  415. else
  416. ss = 0;
  417. } else {
  418. ss = (int16_t)((pPdGainBoundaries[i - 1] -
  419. (minPwrT4[i] / 2)) -
  420. tPdGainOverlap + 1 + minDelta);
  421. }
  422. vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
  423. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  424. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  425. tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
  426. pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
  427. ss++;
  428. }
  429. sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  430. tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
  431. (minPwrT4[i] / 2));
  432. maxIndex = (tgtIndex < sizeCurrVpdTable) ?
  433. tgtIndex : sizeCurrVpdTable;
  434. while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  435. pPDADCValues[k++] = vpdTableI[i][ss++];
  436. }
  437. vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
  438. vpdTableI[i][sizeCurrVpdTable - 2]);
  439. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  440. if (tgtIndex >= maxIndex) {
  441. while ((ss <= tgtIndex) &&
  442. (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  443. tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
  444. (ss - maxIndex + 1) * vpdStep));
  445. pPDADCValues[k++] = (u8)((tmpVal > 255) ?
  446. 255 : tmpVal);
  447. ss++;
  448. }
  449. }
  450. }
  451. if (eeprom_4k)
  452. pdgain_boundary_default = 58;
  453. else
  454. pdgain_boundary_default = pPdGainBoundaries[i - 1];
  455. while (i < AR5416_PD_GAINS_IN_MASK) {
  456. pPdGainBoundaries[i] = pdgain_boundary_default;
  457. i++;
  458. }
  459. while (k < AR5416_NUM_PDADC_VALUES) {
  460. pPDADCValues[k] = pPDADCValues[k - 1];
  461. k++;
  462. }
  463. }
  464. int ath9k_hw_eeprom_init(struct ath_hw *ah)
  465. {
  466. int status;
  467. if (AR_SREV_9300_20_OR_LATER(ah))
  468. ah->eep_ops = &eep_ar9300_ops;
  469. else if (AR_SREV_9287(ah)) {
  470. ah->eep_ops = &eep_ar9287_ops;
  471. } else if (AR_SREV_9285(ah) || AR_SREV_9271(ah)) {
  472. ah->eep_ops = &eep_4k_ops;
  473. } else {
  474. ah->eep_ops = &eep_def_ops;
  475. }
  476. if (!ah->eep_ops->fill_eeprom(ah))
  477. return -EIO;
  478. status = ah->eep_ops->check_eeprom(ah);
  479. return status;
  480. }