eeprom.c 48 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796
  1. /*
  2. * Copyright (c) 2004-2008 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
  5. *
  6. * Permission to use, copy, modify, and distribute this software for any
  7. * purpose with or without fee is hereby granted, provided that the above
  8. * copyright notice and this permission notice appear in all copies.
  9. *
  10. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  11. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  12. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  13. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  14. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  15. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  16. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  17. *
  18. */
  19. /*************************************\
  20. * EEPROM access functions and helpers *
  21. \*************************************/
  22. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  23. #include <linux/slab.h>
  24. #include "ath5k.h"
  25. #include "reg.h"
  26. #include "debug.h"
  27. /******************\
  28. * Helper functions *
  29. \******************/
  30. /*
  31. * Translate binary channel representation in EEPROM to frequency
  32. */
  33. static u16 ath5k_eeprom_bin2freq(struct ath5k_eeprom_info *ee, u16 bin,
  34. unsigned int mode)
  35. {
  36. u16 val;
  37. if (bin == AR5K_EEPROM_CHANNEL_DIS)
  38. return bin;
  39. if (mode == AR5K_EEPROM_MODE_11A) {
  40. if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
  41. val = (5 * bin) + 4800;
  42. else
  43. val = bin > 62 ? (10 * 62) + (5 * (bin - 62)) + 5100 :
  44. (bin * 10) + 5100;
  45. } else {
  46. if (ee->ee_version > AR5K_EEPROM_VERSION_3_2)
  47. val = bin + 2300;
  48. else
  49. val = bin + 2400;
  50. }
  51. return val;
  52. }
  53. /*********\
  54. * Parsers *
  55. \*********/
  56. /*
  57. * Initialize eeprom & capabilities structs
  58. */
  59. static int
  60. ath5k_eeprom_init_header(struct ath5k_hw *ah)
  61. {
  62. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  63. u16 val;
  64. u32 cksum, offset, eep_max = AR5K_EEPROM_INFO_MAX;
  65. /*
  66. * Read values from EEPROM and store them in the capability structure
  67. */
  68. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MAGIC, ee_magic);
  69. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_PROTECT, ee_protect);
  70. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_REG_DOMAIN, ee_regdomain);
  71. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_VERSION, ee_version);
  72. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_HDR, ee_header);
  73. /* Return if we have an old EEPROM */
  74. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_0)
  75. return 0;
  76. /*
  77. * Validate the checksum of the EEPROM date. There are some
  78. * devices with invalid EEPROMs.
  79. */
  80. AR5K_EEPROM_READ(AR5K_EEPROM_SIZE_UPPER, val);
  81. if (val) {
  82. eep_max = (val & AR5K_EEPROM_SIZE_UPPER_MASK) <<
  83. AR5K_EEPROM_SIZE_ENDLOC_SHIFT;
  84. AR5K_EEPROM_READ(AR5K_EEPROM_SIZE_LOWER, val);
  85. eep_max = (eep_max | val) - AR5K_EEPROM_INFO_BASE;
  86. /*
  87. * Fail safe check to prevent stupid loops due
  88. * to busted EEPROMs. XXX: This value is likely too
  89. * big still, waiting on a better value.
  90. */
  91. if (eep_max > (3 * AR5K_EEPROM_INFO_MAX)) {
  92. ATH5K_ERR(ah, "Invalid max custom EEPROM size: "
  93. "%d (0x%04x) max expected: %d (0x%04x)\n",
  94. eep_max, eep_max,
  95. 3 * AR5K_EEPROM_INFO_MAX,
  96. 3 * AR5K_EEPROM_INFO_MAX);
  97. return -EIO;
  98. }
  99. }
  100. for (cksum = 0, offset = 0; offset < eep_max; offset++) {
  101. AR5K_EEPROM_READ(AR5K_EEPROM_INFO(offset), val);
  102. cksum ^= val;
  103. }
  104. if (cksum != AR5K_EEPROM_INFO_CKSUM) {
  105. ATH5K_ERR(ah, "Invalid EEPROM "
  106. "checksum: 0x%04x eep_max: 0x%04x (%s)\n",
  107. cksum, eep_max,
  108. eep_max == AR5K_EEPROM_INFO_MAX ?
  109. "default size" : "custom size");
  110. return -EIO;
  111. }
  112. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_ANT_GAIN(ah->ah_ee_version),
  113. ee_ant_gain);
  114. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) {
  115. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC0, ee_misc0);
  116. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC1, ee_misc1);
  117. /* XXX: Don't know which versions include these two */
  118. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC2, ee_misc2);
  119. if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3)
  120. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC3, ee_misc3);
  121. if (ee->ee_version >= AR5K_EEPROM_VERSION_5_0) {
  122. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC4, ee_misc4);
  123. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC5, ee_misc5);
  124. AR5K_EEPROM_READ_HDR(AR5K_EEPROM_MISC6, ee_misc6);
  125. }
  126. }
  127. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_3) {
  128. AR5K_EEPROM_READ(AR5K_EEPROM_OBDB0_2GHZ, val);
  129. ee->ee_ob[AR5K_EEPROM_MODE_11B][0] = val & 0x7;
  130. ee->ee_db[AR5K_EEPROM_MODE_11B][0] = (val >> 3) & 0x7;
  131. AR5K_EEPROM_READ(AR5K_EEPROM_OBDB1_2GHZ, val);
  132. ee->ee_ob[AR5K_EEPROM_MODE_11G][0] = val & 0x7;
  133. ee->ee_db[AR5K_EEPROM_MODE_11G][0] = (val >> 3) & 0x7;
  134. }
  135. AR5K_EEPROM_READ(AR5K_EEPROM_IS_HB63, val);
  136. if ((ah->ah_mac_version == (AR5K_SREV_AR2425 >> 4)) && val)
  137. ee->ee_is_hb63 = true;
  138. else
  139. ee->ee_is_hb63 = false;
  140. AR5K_EEPROM_READ(AR5K_EEPROM_RFKILL, val);
  141. ee->ee_rfkill_pin = (u8) AR5K_REG_MS(val, AR5K_EEPROM_RFKILL_GPIO_SEL);
  142. ee->ee_rfkill_pol = val & AR5K_EEPROM_RFKILL_POLARITY ? true : false;
  143. /* Check if PCIE_OFFSET points to PCIE_SERDES_SECTION
  144. * and enable serdes programming if needed.
  145. *
  146. * XXX: Serdes values seem to be fixed so
  147. * no need to read them here, we write them
  148. * during ath5k_hw_init */
  149. AR5K_EEPROM_READ(AR5K_EEPROM_PCIE_OFFSET, val);
  150. ee->ee_serdes = (val == AR5K_EEPROM_PCIE_SERDES_SECTION) ?
  151. true : false;
  152. return 0;
  153. }
  154. /*
  155. * Read antenna infos from eeprom
  156. */
  157. static int ath5k_eeprom_read_ants(struct ath5k_hw *ah, u32 *offset,
  158. unsigned int mode)
  159. {
  160. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  161. u32 o = *offset;
  162. u16 val;
  163. int i = 0;
  164. AR5K_EEPROM_READ(o++, val);
  165. ee->ee_switch_settling[mode] = (val >> 8) & 0x7f;
  166. ee->ee_atn_tx_rx[mode] = (val >> 2) & 0x3f;
  167. ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
  168. AR5K_EEPROM_READ(o++, val);
  169. ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
  170. ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
  171. ee->ee_ant_control[mode][i++] = val & 0x3f;
  172. AR5K_EEPROM_READ(o++, val);
  173. ee->ee_ant_control[mode][i++] = (val >> 10) & 0x3f;
  174. ee->ee_ant_control[mode][i++] = (val >> 4) & 0x3f;
  175. ee->ee_ant_control[mode][i] = (val << 2) & 0x3f;
  176. AR5K_EEPROM_READ(o++, val);
  177. ee->ee_ant_control[mode][i++] |= (val >> 14) & 0x3;
  178. ee->ee_ant_control[mode][i++] = (val >> 8) & 0x3f;
  179. ee->ee_ant_control[mode][i++] = (val >> 2) & 0x3f;
  180. ee->ee_ant_control[mode][i] = (val << 4) & 0x3f;
  181. AR5K_EEPROM_READ(o++, val);
  182. ee->ee_ant_control[mode][i++] |= (val >> 12) & 0xf;
  183. ee->ee_ant_control[mode][i++] = (val >> 6) & 0x3f;
  184. ee->ee_ant_control[mode][i++] = val & 0x3f;
  185. /* Get antenna switch tables */
  186. ah->ah_ant_ctl[mode][AR5K_ANT_CTL] =
  187. (ee->ee_ant_control[mode][0] << 4);
  188. ah->ah_ant_ctl[mode][AR5K_ANT_SWTABLE_A] =
  189. ee->ee_ant_control[mode][1] |
  190. (ee->ee_ant_control[mode][2] << 6) |
  191. (ee->ee_ant_control[mode][3] << 12) |
  192. (ee->ee_ant_control[mode][4] << 18) |
  193. (ee->ee_ant_control[mode][5] << 24);
  194. ah->ah_ant_ctl[mode][AR5K_ANT_SWTABLE_B] =
  195. ee->ee_ant_control[mode][6] |
  196. (ee->ee_ant_control[mode][7] << 6) |
  197. (ee->ee_ant_control[mode][8] << 12) |
  198. (ee->ee_ant_control[mode][9] << 18) |
  199. (ee->ee_ant_control[mode][10] << 24);
  200. /* return new offset */
  201. *offset = o;
  202. return 0;
  203. }
  204. /*
  205. * Read supported modes and some mode-specific calibration data
  206. * from eeprom
  207. */
  208. static int ath5k_eeprom_read_modes(struct ath5k_hw *ah, u32 *offset,
  209. unsigned int mode)
  210. {
  211. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  212. u32 o = *offset;
  213. u16 val;
  214. ee->ee_n_piers[mode] = 0;
  215. AR5K_EEPROM_READ(o++, val);
  216. ee->ee_adc_desired_size[mode] = (s8)((val >> 8) & 0xff);
  217. switch (mode) {
  218. case AR5K_EEPROM_MODE_11A:
  219. ee->ee_ob[mode][3] = (val >> 5) & 0x7;
  220. ee->ee_db[mode][3] = (val >> 2) & 0x7;
  221. ee->ee_ob[mode][2] = (val << 1) & 0x7;
  222. AR5K_EEPROM_READ(o++, val);
  223. ee->ee_ob[mode][2] |= (val >> 15) & 0x1;
  224. ee->ee_db[mode][2] = (val >> 12) & 0x7;
  225. ee->ee_ob[mode][1] = (val >> 9) & 0x7;
  226. ee->ee_db[mode][1] = (val >> 6) & 0x7;
  227. ee->ee_ob[mode][0] = (val >> 3) & 0x7;
  228. ee->ee_db[mode][0] = val & 0x7;
  229. break;
  230. case AR5K_EEPROM_MODE_11G:
  231. case AR5K_EEPROM_MODE_11B:
  232. ee->ee_ob[mode][1] = (val >> 4) & 0x7;
  233. ee->ee_db[mode][1] = val & 0x7;
  234. break;
  235. }
  236. AR5K_EEPROM_READ(o++, val);
  237. ee->ee_tx_end2xlna_enable[mode] = (val >> 8) & 0xff;
  238. ee->ee_thr_62[mode] = val & 0xff;
  239. if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
  240. ee->ee_thr_62[mode] = mode == AR5K_EEPROM_MODE_11A ? 15 : 28;
  241. AR5K_EEPROM_READ(o++, val);
  242. ee->ee_tx_end2xpa_disable[mode] = (val >> 8) & 0xff;
  243. ee->ee_tx_frm2xpa_enable[mode] = val & 0xff;
  244. AR5K_EEPROM_READ(o++, val);
  245. ee->ee_pga_desired_size[mode] = (val >> 8) & 0xff;
  246. if ((val & 0xff) & 0x80)
  247. ee->ee_noise_floor_thr[mode] = -((((val & 0xff) ^ 0xff)) + 1);
  248. else
  249. ee->ee_noise_floor_thr[mode] = val & 0xff;
  250. if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2)
  251. ee->ee_noise_floor_thr[mode] =
  252. mode == AR5K_EEPROM_MODE_11A ? -54 : -1;
  253. AR5K_EEPROM_READ(o++, val);
  254. ee->ee_xlna_gain[mode] = (val >> 5) & 0xff;
  255. ee->ee_x_gain[mode] = (val >> 1) & 0xf;
  256. ee->ee_xpd[mode] = val & 0x1;
  257. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
  258. mode != AR5K_EEPROM_MODE_11B)
  259. ee->ee_fixed_bias[mode] = (val >> 13) & 0x1;
  260. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_3) {
  261. AR5K_EEPROM_READ(o++, val);
  262. ee->ee_false_detect[mode] = (val >> 6) & 0x7f;
  263. if (mode == AR5K_EEPROM_MODE_11A)
  264. ee->ee_xr_power[mode] = val & 0x3f;
  265. else {
  266. /* b_DB_11[bg] and b_OB_11[bg] */
  267. ee->ee_ob[mode][0] = val & 0x7;
  268. ee->ee_db[mode][0] = (val >> 3) & 0x7;
  269. }
  270. }
  271. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_3_4) {
  272. ee->ee_i_gain[mode] = AR5K_EEPROM_I_GAIN;
  273. ee->ee_cck_ofdm_power_delta = AR5K_EEPROM_CCK_OFDM_DELTA;
  274. } else {
  275. ee->ee_i_gain[mode] = (val >> 13) & 0x7;
  276. AR5K_EEPROM_READ(o++, val);
  277. ee->ee_i_gain[mode] |= (val << 3) & 0x38;
  278. if (mode == AR5K_EEPROM_MODE_11G) {
  279. ee->ee_cck_ofdm_power_delta = (val >> 3) & 0xff;
  280. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_6)
  281. ee->ee_scaled_cck_delta = (val >> 11) & 0x1f;
  282. }
  283. }
  284. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0 &&
  285. mode == AR5K_EEPROM_MODE_11A) {
  286. ee->ee_i_cal[mode] = (val >> 8) & 0x3f;
  287. ee->ee_q_cal[mode] = (val >> 3) & 0x1f;
  288. }
  289. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_0)
  290. goto done;
  291. /* Note: >= v5 have bg freq piers on another location
  292. * so these freq piers are ignored for >= v5 (should be 0xff
  293. * anyway) */
  294. switch (mode) {
  295. case AR5K_EEPROM_MODE_11A:
  296. if (ah->ah_ee_version < AR5K_EEPROM_VERSION_4_1)
  297. break;
  298. AR5K_EEPROM_READ(o++, val);
  299. ee->ee_margin_tx_rx[mode] = val & 0x3f;
  300. break;
  301. case AR5K_EEPROM_MODE_11B:
  302. AR5K_EEPROM_READ(o++, val);
  303. ee->ee_pwr_cal_b[0].freq =
  304. ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
  305. if (ee->ee_pwr_cal_b[0].freq != AR5K_EEPROM_CHANNEL_DIS)
  306. ee->ee_n_piers[mode]++;
  307. ee->ee_pwr_cal_b[1].freq =
  308. ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
  309. if (ee->ee_pwr_cal_b[1].freq != AR5K_EEPROM_CHANNEL_DIS)
  310. ee->ee_n_piers[mode]++;
  311. AR5K_EEPROM_READ(o++, val);
  312. ee->ee_pwr_cal_b[2].freq =
  313. ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
  314. if (ee->ee_pwr_cal_b[2].freq != AR5K_EEPROM_CHANNEL_DIS)
  315. ee->ee_n_piers[mode]++;
  316. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  317. ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
  318. break;
  319. case AR5K_EEPROM_MODE_11G:
  320. AR5K_EEPROM_READ(o++, val);
  321. ee->ee_pwr_cal_g[0].freq =
  322. ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
  323. if (ee->ee_pwr_cal_g[0].freq != AR5K_EEPROM_CHANNEL_DIS)
  324. ee->ee_n_piers[mode]++;
  325. ee->ee_pwr_cal_g[1].freq =
  326. ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
  327. if (ee->ee_pwr_cal_g[1].freq != AR5K_EEPROM_CHANNEL_DIS)
  328. ee->ee_n_piers[mode]++;
  329. AR5K_EEPROM_READ(o++, val);
  330. ee->ee_turbo_max_power[mode] = val & 0x7f;
  331. ee->ee_xr_power[mode] = (val >> 7) & 0x3f;
  332. AR5K_EEPROM_READ(o++, val);
  333. ee->ee_pwr_cal_g[2].freq =
  334. ath5k_eeprom_bin2freq(ee, val & 0xff, mode);
  335. if (ee->ee_pwr_cal_g[2].freq != AR5K_EEPROM_CHANNEL_DIS)
  336. ee->ee_n_piers[mode]++;
  337. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_1)
  338. ee->ee_margin_tx_rx[mode] = (val >> 8) & 0x3f;
  339. AR5K_EEPROM_READ(o++, val);
  340. ee->ee_i_cal[mode] = (val >> 5) & 0x3f;
  341. ee->ee_q_cal[mode] = val & 0x1f;
  342. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_2) {
  343. AR5K_EEPROM_READ(o++, val);
  344. ee->ee_cck_ofdm_gain_delta = val & 0xff;
  345. }
  346. break;
  347. }
  348. /*
  349. * Read turbo mode information on newer EEPROM versions
  350. */
  351. if (ee->ee_version < AR5K_EEPROM_VERSION_5_0)
  352. goto done;
  353. switch (mode) {
  354. case AR5K_EEPROM_MODE_11A:
  355. ee->ee_switch_settling_turbo[mode] = (val >> 6) & 0x7f;
  356. ee->ee_atn_tx_rx_turbo[mode] = (val >> 13) & 0x7;
  357. AR5K_EEPROM_READ(o++, val);
  358. ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x7) << 3;
  359. ee->ee_margin_tx_rx_turbo[mode] = (val >> 3) & 0x3f;
  360. ee->ee_adc_desired_size_turbo[mode] = (val >> 9) & 0x7f;
  361. AR5K_EEPROM_READ(o++, val);
  362. ee->ee_adc_desired_size_turbo[mode] |= (val & 0x1) << 7;
  363. ee->ee_pga_desired_size_turbo[mode] = (val >> 1) & 0xff;
  364. if (AR5K_EEPROM_EEMAP(ee->ee_misc0) >= 2)
  365. ee->ee_pd_gain_overlap = (val >> 9) & 0xf;
  366. break;
  367. case AR5K_EEPROM_MODE_11G:
  368. ee->ee_switch_settling_turbo[mode] = (val >> 8) & 0x7f;
  369. ee->ee_atn_tx_rx_turbo[mode] = (val >> 15) & 0x7;
  370. AR5K_EEPROM_READ(o++, val);
  371. ee->ee_atn_tx_rx_turbo[mode] |= (val & 0x1f) << 1;
  372. ee->ee_margin_tx_rx_turbo[mode] = (val >> 5) & 0x3f;
  373. ee->ee_adc_desired_size_turbo[mode] = (val >> 11) & 0x7f;
  374. AR5K_EEPROM_READ(o++, val);
  375. ee->ee_adc_desired_size_turbo[mode] |= (val & 0x7) << 5;
  376. ee->ee_pga_desired_size_turbo[mode] = (val >> 3) & 0xff;
  377. break;
  378. }
  379. done:
  380. /* return new offset */
  381. *offset = o;
  382. return 0;
  383. }
  384. /* Read mode-specific data (except power calibration data) */
  385. static int
  386. ath5k_eeprom_init_modes(struct ath5k_hw *ah)
  387. {
  388. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  389. u32 mode_offset[3];
  390. unsigned int mode;
  391. u32 offset;
  392. int ret;
  393. /*
  394. * Get values for all modes
  395. */
  396. mode_offset[AR5K_EEPROM_MODE_11A] = AR5K_EEPROM_MODES_11A(ah->ah_ee_version);
  397. mode_offset[AR5K_EEPROM_MODE_11B] = AR5K_EEPROM_MODES_11B(ah->ah_ee_version);
  398. mode_offset[AR5K_EEPROM_MODE_11G] = AR5K_EEPROM_MODES_11G(ah->ah_ee_version);
  399. ee->ee_turbo_max_power[AR5K_EEPROM_MODE_11A] =
  400. AR5K_EEPROM_HDR_T_5GHZ_DBM(ee->ee_header);
  401. for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++) {
  402. offset = mode_offset[mode];
  403. ret = ath5k_eeprom_read_ants(ah, &offset, mode);
  404. if (ret)
  405. return ret;
  406. ret = ath5k_eeprom_read_modes(ah, &offset, mode);
  407. if (ret)
  408. return ret;
  409. }
  410. /* override for older eeprom versions for better performance */
  411. if (ah->ah_ee_version <= AR5K_EEPROM_VERSION_3_2) {
  412. ee->ee_thr_62[AR5K_EEPROM_MODE_11A] = 15;
  413. ee->ee_thr_62[AR5K_EEPROM_MODE_11B] = 28;
  414. ee->ee_thr_62[AR5K_EEPROM_MODE_11G] = 28;
  415. }
  416. return 0;
  417. }
  418. /* Read the frequency piers for each mode (mostly used on newer eeproms with 0xff
  419. * frequency mask) */
  420. static inline int
  421. ath5k_eeprom_read_freq_list(struct ath5k_hw *ah, int *offset, int max,
  422. struct ath5k_chan_pcal_info *pc, unsigned int mode)
  423. {
  424. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  425. int o = *offset;
  426. int i = 0;
  427. u8 freq1, freq2;
  428. u16 val;
  429. ee->ee_n_piers[mode] = 0;
  430. while (i < max) {
  431. AR5K_EEPROM_READ(o++, val);
  432. freq1 = val & 0xff;
  433. if (!freq1)
  434. break;
  435. pc[i++].freq = ath5k_eeprom_bin2freq(ee,
  436. freq1, mode);
  437. ee->ee_n_piers[mode]++;
  438. freq2 = (val >> 8) & 0xff;
  439. if (!freq2)
  440. break;
  441. pc[i++].freq = ath5k_eeprom_bin2freq(ee,
  442. freq2, mode);
  443. ee->ee_n_piers[mode]++;
  444. }
  445. /* return new offset */
  446. *offset = o;
  447. return 0;
  448. }
  449. /* Read frequency piers for 802.11a */
  450. static int
  451. ath5k_eeprom_init_11a_pcal_freq(struct ath5k_hw *ah, int offset)
  452. {
  453. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  454. struct ath5k_chan_pcal_info *pcal = ee->ee_pwr_cal_a;
  455. int i;
  456. u16 val;
  457. u8 mask;
  458. if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
  459. ath5k_eeprom_read_freq_list(ah, &offset,
  460. AR5K_EEPROM_N_5GHZ_CHAN, pcal,
  461. AR5K_EEPROM_MODE_11A);
  462. } else {
  463. mask = AR5K_EEPROM_FREQ_M(ah->ah_ee_version);
  464. AR5K_EEPROM_READ(offset++, val);
  465. pcal[0].freq = (val >> 9) & mask;
  466. pcal[1].freq = (val >> 2) & mask;
  467. pcal[2].freq = (val << 5) & mask;
  468. AR5K_EEPROM_READ(offset++, val);
  469. pcal[2].freq |= (val >> 11) & 0x1f;
  470. pcal[3].freq = (val >> 4) & mask;
  471. pcal[4].freq = (val << 3) & mask;
  472. AR5K_EEPROM_READ(offset++, val);
  473. pcal[4].freq |= (val >> 13) & 0x7;
  474. pcal[5].freq = (val >> 6) & mask;
  475. pcal[6].freq = (val << 1) & mask;
  476. AR5K_EEPROM_READ(offset++, val);
  477. pcal[6].freq |= (val >> 15) & 0x1;
  478. pcal[7].freq = (val >> 8) & mask;
  479. pcal[8].freq = (val >> 1) & mask;
  480. pcal[9].freq = (val << 6) & mask;
  481. AR5K_EEPROM_READ(offset++, val);
  482. pcal[9].freq |= (val >> 10) & 0x3f;
  483. /* Fixed number of piers */
  484. ee->ee_n_piers[AR5K_EEPROM_MODE_11A] = 10;
  485. for (i = 0; i < AR5K_EEPROM_N_5GHZ_CHAN; i++) {
  486. pcal[i].freq = ath5k_eeprom_bin2freq(ee,
  487. pcal[i].freq, AR5K_EEPROM_MODE_11A);
  488. }
  489. }
  490. return 0;
  491. }
  492. /* Read frequency piers for 802.11bg on eeprom versions >= 5 and eemap >= 2 */
  493. static inline int
  494. ath5k_eeprom_init_11bg_2413(struct ath5k_hw *ah, unsigned int mode, int offset)
  495. {
  496. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  497. struct ath5k_chan_pcal_info *pcal;
  498. switch (mode) {
  499. case AR5K_EEPROM_MODE_11B:
  500. pcal = ee->ee_pwr_cal_b;
  501. break;
  502. case AR5K_EEPROM_MODE_11G:
  503. pcal = ee->ee_pwr_cal_g;
  504. break;
  505. default:
  506. return -EINVAL;
  507. }
  508. ath5k_eeprom_read_freq_list(ah, &offset,
  509. AR5K_EEPROM_N_2GHZ_CHAN_2413, pcal,
  510. mode);
  511. return 0;
  512. }
  513. /*
  514. * Read power calibration for RF5111 chips
  515. *
  516. * For RF5111 we have an XPD -eXternal Power Detector- curve
  517. * for each calibrated channel. Each curve has 0,5dB Power steps
  518. * on x axis and PCDAC steps (offsets) on y axis and looks like an
  519. * exponential function. To recreate the curve we read 11 points
  520. * here and interpolate later.
  521. */
  522. /* Used to match PCDAC steps with power values on RF5111 chips
  523. * (eeprom versions < 4). For RF5111 we have 11 pre-defined PCDAC
  524. * steps that match with the power values we read from eeprom. On
  525. * older eeprom versions (< 3.2) these steps are equally spaced at
  526. * 10% of the pcdac curve -until the curve reaches its maximum-
  527. * (11 steps from 0 to 100%) but on newer eeprom versions (>= 3.2)
  528. * these 11 steps are spaced in a different way. This function returns
  529. * the pcdac steps based on eeprom version and curve min/max so that we
  530. * can have pcdac/pwr points.
  531. */
  532. static inline void
  533. ath5k_get_pcdac_intercepts(struct ath5k_hw *ah, u8 min, u8 max, u8 *vp)
  534. {
  535. static const u16 intercepts3[] = {
  536. 0, 5, 10, 20, 30, 50, 70, 85, 90, 95, 100
  537. };
  538. static const u16 intercepts3_2[] = {
  539. 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100
  540. };
  541. const u16 *ip;
  542. int i;
  543. if (ah->ah_ee_version >= AR5K_EEPROM_VERSION_3_2)
  544. ip = intercepts3_2;
  545. else
  546. ip = intercepts3;
  547. for (i = 0; i < ARRAY_SIZE(intercepts3); i++)
  548. vp[i] = (ip[i] * max + (100 - ip[i]) * min) / 100;
  549. }
  550. static int
  551. ath5k_eeprom_free_pcal_info(struct ath5k_hw *ah, int mode)
  552. {
  553. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  554. struct ath5k_chan_pcal_info *chinfo;
  555. u8 pier, pdg;
  556. switch (mode) {
  557. case AR5K_EEPROM_MODE_11A:
  558. if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
  559. return 0;
  560. chinfo = ee->ee_pwr_cal_a;
  561. break;
  562. case AR5K_EEPROM_MODE_11B:
  563. if (!AR5K_EEPROM_HDR_11B(ee->ee_header))
  564. return 0;
  565. chinfo = ee->ee_pwr_cal_b;
  566. break;
  567. case AR5K_EEPROM_MODE_11G:
  568. if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
  569. return 0;
  570. chinfo = ee->ee_pwr_cal_g;
  571. break;
  572. default:
  573. return -EINVAL;
  574. }
  575. for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
  576. if (!chinfo[pier].pd_curves)
  577. continue;
  578. for (pdg = 0; pdg < AR5K_EEPROM_N_PD_CURVES; pdg++) {
  579. struct ath5k_pdgain_info *pd =
  580. &chinfo[pier].pd_curves[pdg];
  581. kfree(pd->pd_step);
  582. kfree(pd->pd_pwr);
  583. }
  584. kfree(chinfo[pier].pd_curves);
  585. }
  586. return 0;
  587. }
  588. /* Convert RF5111 specific data to generic raw data
  589. * used by interpolation code */
  590. static int
  591. ath5k_eeprom_convert_pcal_info_5111(struct ath5k_hw *ah, int mode,
  592. struct ath5k_chan_pcal_info *chinfo)
  593. {
  594. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  595. struct ath5k_chan_pcal_info_rf5111 *pcinfo;
  596. struct ath5k_pdgain_info *pd;
  597. u8 pier, point, idx;
  598. u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
  599. /* Fill raw data for each calibration pier */
  600. for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
  601. pcinfo = &chinfo[pier].rf5111_info;
  602. /* Allocate pd_curves for this cal pier */
  603. chinfo[pier].pd_curves =
  604. kcalloc(AR5K_EEPROM_N_PD_CURVES,
  605. sizeof(struct ath5k_pdgain_info),
  606. GFP_KERNEL);
  607. if (!chinfo[pier].pd_curves)
  608. goto err_out;
  609. /* Only one curve for RF5111
  610. * find out which one and place
  611. * in pd_curves.
  612. * Note: ee_x_gain is reversed here */
  613. for (idx = 0; idx < AR5K_EEPROM_N_PD_CURVES; idx++) {
  614. if (!((ee->ee_x_gain[mode] >> idx) & 0x1)) {
  615. pdgain_idx[0] = idx;
  616. break;
  617. }
  618. }
  619. ee->ee_pd_gains[mode] = 1;
  620. pd = &chinfo[pier].pd_curves[idx];
  621. pd->pd_points = AR5K_EEPROM_N_PWR_POINTS_5111;
  622. /* Allocate pd points for this curve */
  623. pd->pd_step = kcalloc(AR5K_EEPROM_N_PWR_POINTS_5111,
  624. sizeof(u8), GFP_KERNEL);
  625. if (!pd->pd_step)
  626. goto err_out;
  627. pd->pd_pwr = kcalloc(AR5K_EEPROM_N_PWR_POINTS_5111,
  628. sizeof(s16), GFP_KERNEL);
  629. if (!pd->pd_pwr)
  630. goto err_out;
  631. /* Fill raw dataset
  632. * (convert power to 0.25dB units
  633. * for RF5112 compatibility) */
  634. for (point = 0; point < pd->pd_points; point++) {
  635. /* Absolute values */
  636. pd->pd_pwr[point] = 2 * pcinfo->pwr[point];
  637. /* Already sorted */
  638. pd->pd_step[point] = pcinfo->pcdac[point];
  639. }
  640. /* Set min/max pwr */
  641. chinfo[pier].min_pwr = pd->pd_pwr[0];
  642. chinfo[pier].max_pwr = pd->pd_pwr[10];
  643. }
  644. return 0;
  645. err_out:
  646. ath5k_eeprom_free_pcal_info(ah, mode);
  647. return -ENOMEM;
  648. }
  649. /* Parse EEPROM data */
  650. static int
  651. ath5k_eeprom_read_pcal_info_5111(struct ath5k_hw *ah, int mode)
  652. {
  653. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  654. struct ath5k_chan_pcal_info *pcal;
  655. int offset, ret;
  656. int i;
  657. u16 val;
  658. offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
  659. switch (mode) {
  660. case AR5K_EEPROM_MODE_11A:
  661. if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
  662. return 0;
  663. ret = ath5k_eeprom_init_11a_pcal_freq(ah,
  664. offset + AR5K_EEPROM_GROUP1_OFFSET);
  665. if (ret < 0)
  666. return ret;
  667. offset += AR5K_EEPROM_GROUP2_OFFSET;
  668. pcal = ee->ee_pwr_cal_a;
  669. break;
  670. case AR5K_EEPROM_MODE_11B:
  671. if (!AR5K_EEPROM_HDR_11B(ee->ee_header) &&
  672. !AR5K_EEPROM_HDR_11G(ee->ee_header))
  673. return 0;
  674. pcal = ee->ee_pwr_cal_b;
  675. offset += AR5K_EEPROM_GROUP3_OFFSET;
  676. /* fixed piers */
  677. pcal[0].freq = 2412;
  678. pcal[1].freq = 2447;
  679. pcal[2].freq = 2484;
  680. ee->ee_n_piers[mode] = 3;
  681. break;
  682. case AR5K_EEPROM_MODE_11G:
  683. if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
  684. return 0;
  685. pcal = ee->ee_pwr_cal_g;
  686. offset += AR5K_EEPROM_GROUP4_OFFSET;
  687. /* fixed piers */
  688. pcal[0].freq = 2312;
  689. pcal[1].freq = 2412;
  690. pcal[2].freq = 2484;
  691. ee->ee_n_piers[mode] = 3;
  692. break;
  693. default:
  694. return -EINVAL;
  695. }
  696. for (i = 0; i < ee->ee_n_piers[mode]; i++) {
  697. struct ath5k_chan_pcal_info_rf5111 *cdata =
  698. &pcal[i].rf5111_info;
  699. AR5K_EEPROM_READ(offset++, val);
  700. cdata->pcdac_max = ((val >> 10) & AR5K_EEPROM_PCDAC_M);
  701. cdata->pcdac_min = ((val >> 4) & AR5K_EEPROM_PCDAC_M);
  702. cdata->pwr[0] = ((val << 2) & AR5K_EEPROM_POWER_M);
  703. AR5K_EEPROM_READ(offset++, val);
  704. cdata->pwr[0] |= ((val >> 14) & 0x3);
  705. cdata->pwr[1] = ((val >> 8) & AR5K_EEPROM_POWER_M);
  706. cdata->pwr[2] = ((val >> 2) & AR5K_EEPROM_POWER_M);
  707. cdata->pwr[3] = ((val << 4) & AR5K_EEPROM_POWER_M);
  708. AR5K_EEPROM_READ(offset++, val);
  709. cdata->pwr[3] |= ((val >> 12) & 0xf);
  710. cdata->pwr[4] = ((val >> 6) & AR5K_EEPROM_POWER_M);
  711. cdata->pwr[5] = (val & AR5K_EEPROM_POWER_M);
  712. AR5K_EEPROM_READ(offset++, val);
  713. cdata->pwr[6] = ((val >> 10) & AR5K_EEPROM_POWER_M);
  714. cdata->pwr[7] = ((val >> 4) & AR5K_EEPROM_POWER_M);
  715. cdata->pwr[8] = ((val << 2) & AR5K_EEPROM_POWER_M);
  716. AR5K_EEPROM_READ(offset++, val);
  717. cdata->pwr[8] |= ((val >> 14) & 0x3);
  718. cdata->pwr[9] = ((val >> 8) & AR5K_EEPROM_POWER_M);
  719. cdata->pwr[10] = ((val >> 2) & AR5K_EEPROM_POWER_M);
  720. ath5k_get_pcdac_intercepts(ah, cdata->pcdac_min,
  721. cdata->pcdac_max, cdata->pcdac);
  722. }
  723. return ath5k_eeprom_convert_pcal_info_5111(ah, mode, pcal);
  724. }
  725. /*
  726. * Read power calibration for RF5112 chips
  727. *
  728. * For RF5112 we have 4 XPD -eXternal Power Detector- curves
  729. * for each calibrated channel on 0, -6, -12 and -18dBm but we only
  730. * use the higher (3) and the lower (0) curves. Each curve has 0.5dB
  731. * power steps on x axis and PCDAC steps on y axis and looks like a
  732. * linear function. To recreate the curve and pass the power values
  733. * on hw, we read 4 points for xpd 0 (lower gain -> max power)
  734. * and 3 points for xpd 3 (higher gain -> lower power) here and
  735. * interpolate later.
  736. *
  737. * Note: Many vendors just use xpd 0 so xpd 3 is zeroed.
  738. */
  739. /* Convert RF5112 specific data to generic raw data
  740. * used by interpolation code */
  741. static int
  742. ath5k_eeprom_convert_pcal_info_5112(struct ath5k_hw *ah, int mode,
  743. struct ath5k_chan_pcal_info *chinfo)
  744. {
  745. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  746. struct ath5k_chan_pcal_info_rf5112 *pcinfo;
  747. u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
  748. unsigned int pier, pdg, point;
  749. /* Fill raw data for each calibration pier */
  750. for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
  751. pcinfo = &chinfo[pier].rf5112_info;
  752. /* Allocate pd_curves for this cal pier */
  753. chinfo[pier].pd_curves =
  754. kcalloc(AR5K_EEPROM_N_PD_CURVES,
  755. sizeof(struct ath5k_pdgain_info),
  756. GFP_KERNEL);
  757. if (!chinfo[pier].pd_curves)
  758. goto err_out;
  759. /* Fill pd_curves */
  760. for (pdg = 0; pdg < ee->ee_pd_gains[mode]; pdg++) {
  761. u8 idx = pdgain_idx[pdg];
  762. struct ath5k_pdgain_info *pd =
  763. &chinfo[pier].pd_curves[idx];
  764. /* Lowest gain curve (max power) */
  765. if (pdg == 0) {
  766. /* One more point for better accuracy */
  767. pd->pd_points = AR5K_EEPROM_N_XPD0_POINTS;
  768. /* Allocate pd points for this curve */
  769. pd->pd_step = kcalloc(pd->pd_points,
  770. sizeof(u8), GFP_KERNEL);
  771. if (!pd->pd_step)
  772. goto err_out;
  773. pd->pd_pwr = kcalloc(pd->pd_points,
  774. sizeof(s16), GFP_KERNEL);
  775. if (!pd->pd_pwr)
  776. goto err_out;
  777. /* Fill raw dataset
  778. * (all power levels are in 0.25dB units) */
  779. pd->pd_step[0] = pcinfo->pcdac_x0[0];
  780. pd->pd_pwr[0] = pcinfo->pwr_x0[0];
  781. for (point = 1; point < pd->pd_points;
  782. point++) {
  783. /* Absolute values */
  784. pd->pd_pwr[point] =
  785. pcinfo->pwr_x0[point];
  786. /* Deltas */
  787. pd->pd_step[point] =
  788. pd->pd_step[point - 1] +
  789. pcinfo->pcdac_x0[point];
  790. }
  791. /* Set min power for this frequency */
  792. chinfo[pier].min_pwr = pd->pd_pwr[0];
  793. /* Highest gain curve (min power) */
  794. } else if (pdg == 1) {
  795. pd->pd_points = AR5K_EEPROM_N_XPD3_POINTS;
  796. /* Allocate pd points for this curve */
  797. pd->pd_step = kcalloc(pd->pd_points,
  798. sizeof(u8), GFP_KERNEL);
  799. if (!pd->pd_step)
  800. goto err_out;
  801. pd->pd_pwr = kcalloc(pd->pd_points,
  802. sizeof(s16), GFP_KERNEL);
  803. if (!pd->pd_pwr)
  804. goto err_out;
  805. /* Fill raw dataset
  806. * (all power levels are in 0.25dB units) */
  807. for (point = 0; point < pd->pd_points;
  808. point++) {
  809. /* Absolute values */
  810. pd->pd_pwr[point] =
  811. pcinfo->pwr_x3[point];
  812. /* Fixed points */
  813. pd->pd_step[point] =
  814. pcinfo->pcdac_x3[point];
  815. }
  816. /* Since we have a higher gain curve
  817. * override min power */
  818. chinfo[pier].min_pwr = pd->pd_pwr[0];
  819. }
  820. }
  821. }
  822. return 0;
  823. err_out:
  824. ath5k_eeprom_free_pcal_info(ah, mode);
  825. return -ENOMEM;
  826. }
  827. /* Parse EEPROM data */
  828. static int
  829. ath5k_eeprom_read_pcal_info_5112(struct ath5k_hw *ah, int mode)
  830. {
  831. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  832. struct ath5k_chan_pcal_info_rf5112 *chan_pcal_info;
  833. struct ath5k_chan_pcal_info *gen_chan_info;
  834. u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
  835. u32 offset;
  836. u8 i, c;
  837. u16 val;
  838. u8 pd_gains = 0;
  839. /* Count how many curves we have and
  840. * identify them (which one of the 4
  841. * available curves we have on each count).
  842. * Curves are stored from lower (x0) to
  843. * higher (x3) gain */
  844. for (i = 0; i < AR5K_EEPROM_N_PD_CURVES; i++) {
  845. /* ee_x_gain[mode] is x gain mask */
  846. if ((ee->ee_x_gain[mode] >> i) & 0x1)
  847. pdgain_idx[pd_gains++] = i;
  848. }
  849. ee->ee_pd_gains[mode] = pd_gains;
  850. if (pd_gains == 0 || pd_gains > 2)
  851. return -EINVAL;
  852. switch (mode) {
  853. case AR5K_EEPROM_MODE_11A:
  854. /*
  855. * Read 5GHz EEPROM channels
  856. */
  857. offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
  858. ath5k_eeprom_init_11a_pcal_freq(ah, offset);
  859. offset += AR5K_EEPROM_GROUP2_OFFSET;
  860. gen_chan_info = ee->ee_pwr_cal_a;
  861. break;
  862. case AR5K_EEPROM_MODE_11B:
  863. offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
  864. if (AR5K_EEPROM_HDR_11A(ee->ee_header))
  865. offset += AR5K_EEPROM_GROUP3_OFFSET;
  866. /* NB: frequency piers parsed during mode init */
  867. gen_chan_info = ee->ee_pwr_cal_b;
  868. break;
  869. case AR5K_EEPROM_MODE_11G:
  870. offset = AR5K_EEPROM_GROUPS_START(ee->ee_version);
  871. if (AR5K_EEPROM_HDR_11A(ee->ee_header))
  872. offset += AR5K_EEPROM_GROUP4_OFFSET;
  873. else if (AR5K_EEPROM_HDR_11B(ee->ee_header))
  874. offset += AR5K_EEPROM_GROUP2_OFFSET;
  875. /* NB: frequency piers parsed during mode init */
  876. gen_chan_info = ee->ee_pwr_cal_g;
  877. break;
  878. default:
  879. return -EINVAL;
  880. }
  881. for (i = 0; i < ee->ee_n_piers[mode]; i++) {
  882. chan_pcal_info = &gen_chan_info[i].rf5112_info;
  883. /* Power values in quarter dB
  884. * for the lower xpd gain curve
  885. * (0 dBm -> higher output power) */
  886. for (c = 0; c < AR5K_EEPROM_N_XPD0_POINTS; c++) {
  887. AR5K_EEPROM_READ(offset++, val);
  888. chan_pcal_info->pwr_x0[c] = (s8) (val & 0xff);
  889. chan_pcal_info->pwr_x0[++c] = (s8) ((val >> 8) & 0xff);
  890. }
  891. /* PCDAC steps
  892. * corresponding to the above power
  893. * measurements */
  894. AR5K_EEPROM_READ(offset++, val);
  895. chan_pcal_info->pcdac_x0[1] = (val & 0x1f);
  896. chan_pcal_info->pcdac_x0[2] = ((val >> 5) & 0x1f);
  897. chan_pcal_info->pcdac_x0[3] = ((val >> 10) & 0x1f);
  898. /* Power values in quarter dB
  899. * for the higher xpd gain curve
  900. * (18 dBm -> lower output power) */
  901. AR5K_EEPROM_READ(offset++, val);
  902. chan_pcal_info->pwr_x3[0] = (s8) (val & 0xff);
  903. chan_pcal_info->pwr_x3[1] = (s8) ((val >> 8) & 0xff);
  904. AR5K_EEPROM_READ(offset++, val);
  905. chan_pcal_info->pwr_x3[2] = (val & 0xff);
  906. /* PCDAC steps
  907. * corresponding to the above power
  908. * measurements (fixed) */
  909. chan_pcal_info->pcdac_x3[0] = 20;
  910. chan_pcal_info->pcdac_x3[1] = 35;
  911. chan_pcal_info->pcdac_x3[2] = 63;
  912. if (ee->ee_version >= AR5K_EEPROM_VERSION_4_3) {
  913. chan_pcal_info->pcdac_x0[0] = ((val >> 8) & 0x3f);
  914. /* Last xpd0 power level is also channel maximum */
  915. gen_chan_info[i].max_pwr = chan_pcal_info->pwr_x0[3];
  916. } else {
  917. chan_pcal_info->pcdac_x0[0] = 1;
  918. gen_chan_info[i].max_pwr = (s8) ((val >> 8) & 0xff);
  919. }
  920. }
  921. return ath5k_eeprom_convert_pcal_info_5112(ah, mode, gen_chan_info);
  922. }
  923. /*
  924. * Read power calibration for RF2413 chips
  925. *
  926. * For RF2413 we have a Power to PDDAC table (Power Detector)
  927. * instead of a PCDAC and 4 pd gain curves for each calibrated channel.
  928. * Each curve has power on x axis in 0.5 db steps and PDDADC steps on y
  929. * axis and looks like an exponential function like the RF5111 curve.
  930. *
  931. * To recreate the curves we read here the points and interpolate
  932. * later. Note that in most cases only 2 (higher and lower) curves are
  933. * used (like RF5112) but vendors have the opportunity to include all
  934. * 4 curves on eeprom. The final curve (higher power) has an extra
  935. * point for better accuracy like RF5112.
  936. */
  937. /* For RF2413 power calibration data doesn't start on a fixed location and
  938. * if a mode is not supported, its section is missing -not zeroed-.
  939. * So we need to calculate the starting offset for each section by using
  940. * these two functions */
  941. /* Return the size of each section based on the mode and the number of pd
  942. * gains available (maximum 4). */
  943. static inline unsigned int
  944. ath5k_pdgains_size_2413(struct ath5k_eeprom_info *ee, unsigned int mode)
  945. {
  946. static const unsigned int pdgains_size[] = { 4, 6, 9, 12 };
  947. unsigned int sz;
  948. sz = pdgains_size[ee->ee_pd_gains[mode] - 1];
  949. sz *= ee->ee_n_piers[mode];
  950. return sz;
  951. }
  952. /* Return the starting offset for a section based on the modes supported
  953. * and each section's size. */
  954. static unsigned int
  955. ath5k_cal_data_offset_2413(struct ath5k_eeprom_info *ee, int mode)
  956. {
  957. u32 offset = AR5K_EEPROM_CAL_DATA_START(ee->ee_misc4);
  958. switch (mode) {
  959. case AR5K_EEPROM_MODE_11G:
  960. if (AR5K_EEPROM_HDR_11B(ee->ee_header))
  961. offset += ath5k_pdgains_size_2413(ee,
  962. AR5K_EEPROM_MODE_11B) +
  963. AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
  964. /* fall through */
  965. case AR5K_EEPROM_MODE_11B:
  966. if (AR5K_EEPROM_HDR_11A(ee->ee_header))
  967. offset += ath5k_pdgains_size_2413(ee,
  968. AR5K_EEPROM_MODE_11A) +
  969. AR5K_EEPROM_N_5GHZ_CHAN / 2;
  970. /* fall through */
  971. case AR5K_EEPROM_MODE_11A:
  972. break;
  973. default:
  974. break;
  975. }
  976. return offset;
  977. }
  978. /* Convert RF2413 specific data to generic raw data
  979. * used by interpolation code */
  980. static int
  981. ath5k_eeprom_convert_pcal_info_2413(struct ath5k_hw *ah, int mode,
  982. struct ath5k_chan_pcal_info *chinfo)
  983. {
  984. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  985. struct ath5k_chan_pcal_info_rf2413 *pcinfo;
  986. u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
  987. unsigned int pier, pdg, point;
  988. /* Fill raw data for each calibration pier */
  989. for (pier = 0; pier < ee->ee_n_piers[mode]; pier++) {
  990. pcinfo = &chinfo[pier].rf2413_info;
  991. /* Allocate pd_curves for this cal pier */
  992. chinfo[pier].pd_curves =
  993. kcalloc(AR5K_EEPROM_N_PD_CURVES,
  994. sizeof(struct ath5k_pdgain_info),
  995. GFP_KERNEL);
  996. if (!chinfo[pier].pd_curves)
  997. goto err_out;
  998. /* Fill pd_curves */
  999. for (pdg = 0; pdg < ee->ee_pd_gains[mode]; pdg++) {
  1000. u8 idx = pdgain_idx[pdg];
  1001. struct ath5k_pdgain_info *pd =
  1002. &chinfo[pier].pd_curves[idx];
  1003. /* One more point for the highest power
  1004. * curve (lowest gain) */
  1005. if (pdg == ee->ee_pd_gains[mode] - 1)
  1006. pd->pd_points = AR5K_EEPROM_N_PD_POINTS;
  1007. else
  1008. pd->pd_points = AR5K_EEPROM_N_PD_POINTS - 1;
  1009. /* Allocate pd points for this curve */
  1010. pd->pd_step = kcalloc(pd->pd_points,
  1011. sizeof(u8), GFP_KERNEL);
  1012. if (!pd->pd_step)
  1013. goto err_out;
  1014. pd->pd_pwr = kcalloc(pd->pd_points,
  1015. sizeof(s16), GFP_KERNEL);
  1016. if (!pd->pd_pwr)
  1017. goto err_out;
  1018. /* Fill raw dataset
  1019. * convert all pwr levels to
  1020. * quarter dB for RF5112 compatibility */
  1021. pd->pd_step[0] = pcinfo->pddac_i[pdg];
  1022. pd->pd_pwr[0] = 4 * pcinfo->pwr_i[pdg];
  1023. for (point = 1; point < pd->pd_points; point++) {
  1024. pd->pd_pwr[point] = pd->pd_pwr[point - 1] +
  1025. 2 * pcinfo->pwr[pdg][point - 1];
  1026. pd->pd_step[point] = pd->pd_step[point - 1] +
  1027. pcinfo->pddac[pdg][point - 1];
  1028. }
  1029. /* Highest gain curve -> min power */
  1030. if (pdg == 0)
  1031. chinfo[pier].min_pwr = pd->pd_pwr[0];
  1032. /* Lowest gain curve -> max power */
  1033. if (pdg == ee->ee_pd_gains[mode] - 1)
  1034. chinfo[pier].max_pwr =
  1035. pd->pd_pwr[pd->pd_points - 1];
  1036. }
  1037. }
  1038. return 0;
  1039. err_out:
  1040. ath5k_eeprom_free_pcal_info(ah, mode);
  1041. return -ENOMEM;
  1042. }
  1043. /* Parse EEPROM data */
  1044. static int
  1045. ath5k_eeprom_read_pcal_info_2413(struct ath5k_hw *ah, int mode)
  1046. {
  1047. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1048. struct ath5k_chan_pcal_info_rf2413 *pcinfo;
  1049. struct ath5k_chan_pcal_info *chinfo;
  1050. u8 *pdgain_idx = ee->ee_pdc_to_idx[mode];
  1051. u32 offset;
  1052. int idx, i;
  1053. u16 val;
  1054. u8 pd_gains = 0;
  1055. /* Count how many curves we have and
  1056. * identify them (which one of the 4
  1057. * available curves we have on each count).
  1058. * Curves are stored from higher to
  1059. * lower gain so we go backwards */
  1060. for (idx = AR5K_EEPROM_N_PD_CURVES - 1; idx >= 0; idx--) {
  1061. /* ee_x_gain[mode] is x gain mask */
  1062. if ((ee->ee_x_gain[mode] >> idx) & 0x1)
  1063. pdgain_idx[pd_gains++] = idx;
  1064. }
  1065. ee->ee_pd_gains[mode] = pd_gains;
  1066. if (pd_gains == 0)
  1067. return -EINVAL;
  1068. offset = ath5k_cal_data_offset_2413(ee, mode);
  1069. switch (mode) {
  1070. case AR5K_EEPROM_MODE_11A:
  1071. if (!AR5K_EEPROM_HDR_11A(ee->ee_header))
  1072. return 0;
  1073. ath5k_eeprom_init_11a_pcal_freq(ah, offset);
  1074. offset += AR5K_EEPROM_N_5GHZ_CHAN / 2;
  1075. chinfo = ee->ee_pwr_cal_a;
  1076. break;
  1077. case AR5K_EEPROM_MODE_11B:
  1078. if (!AR5K_EEPROM_HDR_11B(ee->ee_header))
  1079. return 0;
  1080. ath5k_eeprom_init_11bg_2413(ah, mode, offset);
  1081. offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
  1082. chinfo = ee->ee_pwr_cal_b;
  1083. break;
  1084. case AR5K_EEPROM_MODE_11G:
  1085. if (!AR5K_EEPROM_HDR_11G(ee->ee_header))
  1086. return 0;
  1087. ath5k_eeprom_init_11bg_2413(ah, mode, offset);
  1088. offset += AR5K_EEPROM_N_2GHZ_CHAN_2413 / 2;
  1089. chinfo = ee->ee_pwr_cal_g;
  1090. break;
  1091. default:
  1092. return -EINVAL;
  1093. }
  1094. for (i = 0; i < ee->ee_n_piers[mode]; i++) {
  1095. pcinfo = &chinfo[i].rf2413_info;
  1096. /*
  1097. * Read pwr_i, pddac_i and the first
  1098. * 2 pd points (pwr, pddac)
  1099. */
  1100. AR5K_EEPROM_READ(offset++, val);
  1101. pcinfo->pwr_i[0] = val & 0x1f;
  1102. pcinfo->pddac_i[0] = (val >> 5) & 0x7f;
  1103. pcinfo->pwr[0][0] = (val >> 12) & 0xf;
  1104. AR5K_EEPROM_READ(offset++, val);
  1105. pcinfo->pddac[0][0] = val & 0x3f;
  1106. pcinfo->pwr[0][1] = (val >> 6) & 0xf;
  1107. pcinfo->pddac[0][1] = (val >> 10) & 0x3f;
  1108. AR5K_EEPROM_READ(offset++, val);
  1109. pcinfo->pwr[0][2] = val & 0xf;
  1110. pcinfo->pddac[0][2] = (val >> 4) & 0x3f;
  1111. pcinfo->pwr[0][3] = 0;
  1112. pcinfo->pddac[0][3] = 0;
  1113. if (pd_gains > 1) {
  1114. /*
  1115. * Pd gain 0 is not the last pd gain
  1116. * so it only has 2 pd points.
  1117. * Continue with pd gain 1.
  1118. */
  1119. pcinfo->pwr_i[1] = (val >> 10) & 0x1f;
  1120. pcinfo->pddac_i[1] = (val >> 15) & 0x1;
  1121. AR5K_EEPROM_READ(offset++, val);
  1122. pcinfo->pddac_i[1] |= (val & 0x3F) << 1;
  1123. pcinfo->pwr[1][0] = (val >> 6) & 0xf;
  1124. pcinfo->pddac[1][0] = (val >> 10) & 0x3f;
  1125. AR5K_EEPROM_READ(offset++, val);
  1126. pcinfo->pwr[1][1] = val & 0xf;
  1127. pcinfo->pddac[1][1] = (val >> 4) & 0x3f;
  1128. pcinfo->pwr[1][2] = (val >> 10) & 0xf;
  1129. pcinfo->pddac[1][2] = (val >> 14) & 0x3;
  1130. AR5K_EEPROM_READ(offset++, val);
  1131. pcinfo->pddac[1][2] |= (val & 0xF) << 2;
  1132. pcinfo->pwr[1][3] = 0;
  1133. pcinfo->pddac[1][3] = 0;
  1134. } else if (pd_gains == 1) {
  1135. /*
  1136. * Pd gain 0 is the last one so
  1137. * read the extra point.
  1138. */
  1139. pcinfo->pwr[0][3] = (val >> 10) & 0xf;
  1140. pcinfo->pddac[0][3] = (val >> 14) & 0x3;
  1141. AR5K_EEPROM_READ(offset++, val);
  1142. pcinfo->pddac[0][3] |= (val & 0xF) << 2;
  1143. }
  1144. /*
  1145. * Proceed with the other pd_gains
  1146. * as above.
  1147. */
  1148. if (pd_gains > 2) {
  1149. pcinfo->pwr_i[2] = (val >> 4) & 0x1f;
  1150. pcinfo->pddac_i[2] = (val >> 9) & 0x7f;
  1151. AR5K_EEPROM_READ(offset++, val);
  1152. pcinfo->pwr[2][0] = (val >> 0) & 0xf;
  1153. pcinfo->pddac[2][0] = (val >> 4) & 0x3f;
  1154. pcinfo->pwr[2][1] = (val >> 10) & 0xf;
  1155. pcinfo->pddac[2][1] = (val >> 14) & 0x3;
  1156. AR5K_EEPROM_READ(offset++, val);
  1157. pcinfo->pddac[2][1] |= (val & 0xF) << 2;
  1158. pcinfo->pwr[2][2] = (val >> 4) & 0xf;
  1159. pcinfo->pddac[2][2] = (val >> 8) & 0x3f;
  1160. pcinfo->pwr[2][3] = 0;
  1161. pcinfo->pddac[2][3] = 0;
  1162. } else if (pd_gains == 2) {
  1163. pcinfo->pwr[1][3] = (val >> 4) & 0xf;
  1164. pcinfo->pddac[1][3] = (val >> 8) & 0x3f;
  1165. }
  1166. if (pd_gains > 3) {
  1167. pcinfo->pwr_i[3] = (val >> 14) & 0x3;
  1168. AR5K_EEPROM_READ(offset++, val);
  1169. pcinfo->pwr_i[3] |= ((val >> 0) & 0x7) << 2;
  1170. pcinfo->pddac_i[3] = (val >> 3) & 0x7f;
  1171. pcinfo->pwr[3][0] = (val >> 10) & 0xf;
  1172. pcinfo->pddac[3][0] = (val >> 14) & 0x3;
  1173. AR5K_EEPROM_READ(offset++, val);
  1174. pcinfo->pddac[3][0] |= (val & 0xF) << 2;
  1175. pcinfo->pwr[3][1] = (val >> 4) & 0xf;
  1176. pcinfo->pddac[3][1] = (val >> 8) & 0x3f;
  1177. pcinfo->pwr[3][2] = (val >> 14) & 0x3;
  1178. AR5K_EEPROM_READ(offset++, val);
  1179. pcinfo->pwr[3][2] |= ((val >> 0) & 0x3) << 2;
  1180. pcinfo->pddac[3][2] = (val >> 2) & 0x3f;
  1181. pcinfo->pwr[3][3] = (val >> 8) & 0xf;
  1182. pcinfo->pddac[3][3] = (val >> 12) & 0xF;
  1183. AR5K_EEPROM_READ(offset++, val);
  1184. pcinfo->pddac[3][3] |= ((val >> 0) & 0x3) << 4;
  1185. } else if (pd_gains == 3) {
  1186. pcinfo->pwr[2][3] = (val >> 14) & 0x3;
  1187. AR5K_EEPROM_READ(offset++, val);
  1188. pcinfo->pwr[2][3] |= ((val >> 0) & 0x3) << 2;
  1189. pcinfo->pddac[2][3] = (val >> 2) & 0x3f;
  1190. }
  1191. }
  1192. return ath5k_eeprom_convert_pcal_info_2413(ah, mode, chinfo);
  1193. }
  1194. /*
  1195. * Read per rate target power (this is the maximum tx power
  1196. * supported by the card). This info is used when setting
  1197. * tx power, no matter the channel.
  1198. *
  1199. * This also works for v5 EEPROMs.
  1200. */
  1201. static int
  1202. ath5k_eeprom_read_target_rate_pwr_info(struct ath5k_hw *ah, unsigned int mode)
  1203. {
  1204. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1205. struct ath5k_rate_pcal_info *rate_pcal_info;
  1206. u8 *rate_target_pwr_num;
  1207. u32 offset;
  1208. u16 val;
  1209. int i;
  1210. offset = AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1);
  1211. rate_target_pwr_num = &ee->ee_rate_target_pwr_num[mode];
  1212. switch (mode) {
  1213. case AR5K_EEPROM_MODE_11A:
  1214. offset += AR5K_EEPROM_TARGET_PWR_OFF_11A(ee->ee_version);
  1215. rate_pcal_info = ee->ee_rate_tpwr_a;
  1216. ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_5GHZ_RATE_CHAN;
  1217. break;
  1218. case AR5K_EEPROM_MODE_11B:
  1219. offset += AR5K_EEPROM_TARGET_PWR_OFF_11B(ee->ee_version);
  1220. rate_pcal_info = ee->ee_rate_tpwr_b;
  1221. ee->ee_rate_target_pwr_num[mode] = 2; /* 3rd is g mode's 1st */
  1222. break;
  1223. case AR5K_EEPROM_MODE_11G:
  1224. offset += AR5K_EEPROM_TARGET_PWR_OFF_11G(ee->ee_version);
  1225. rate_pcal_info = ee->ee_rate_tpwr_g;
  1226. ee->ee_rate_target_pwr_num[mode] = AR5K_EEPROM_N_2GHZ_CHAN;
  1227. break;
  1228. default:
  1229. return -EINVAL;
  1230. }
  1231. /* Different freq mask for older eeproms (<= v3.2) */
  1232. if (ee->ee_version <= AR5K_EEPROM_VERSION_3_2) {
  1233. for (i = 0; i < (*rate_target_pwr_num); i++) {
  1234. AR5K_EEPROM_READ(offset++, val);
  1235. rate_pcal_info[i].freq =
  1236. ath5k_eeprom_bin2freq(ee, (val >> 9) & 0x7f, mode);
  1237. rate_pcal_info[i].target_power_6to24 = ((val >> 3) & 0x3f);
  1238. rate_pcal_info[i].target_power_36 = (val << 3) & 0x3f;
  1239. AR5K_EEPROM_READ(offset++, val);
  1240. if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
  1241. val == 0) {
  1242. (*rate_target_pwr_num) = i;
  1243. break;
  1244. }
  1245. rate_pcal_info[i].target_power_36 |= ((val >> 13) & 0x7);
  1246. rate_pcal_info[i].target_power_48 = ((val >> 7) & 0x3f);
  1247. rate_pcal_info[i].target_power_54 = ((val >> 1) & 0x3f);
  1248. }
  1249. } else {
  1250. for (i = 0; i < (*rate_target_pwr_num); i++) {
  1251. AR5K_EEPROM_READ(offset++, val);
  1252. rate_pcal_info[i].freq =
  1253. ath5k_eeprom_bin2freq(ee, (val >> 8) & 0xff, mode);
  1254. rate_pcal_info[i].target_power_6to24 = ((val >> 2) & 0x3f);
  1255. rate_pcal_info[i].target_power_36 = (val << 4) & 0x3f;
  1256. AR5K_EEPROM_READ(offset++, val);
  1257. if (rate_pcal_info[i].freq == AR5K_EEPROM_CHANNEL_DIS ||
  1258. val == 0) {
  1259. (*rate_target_pwr_num) = i;
  1260. break;
  1261. }
  1262. rate_pcal_info[i].target_power_36 |= (val >> 12) & 0xf;
  1263. rate_pcal_info[i].target_power_48 = ((val >> 6) & 0x3f);
  1264. rate_pcal_info[i].target_power_54 = (val & 0x3f);
  1265. }
  1266. }
  1267. return 0;
  1268. }
  1269. /*
  1270. * Read per channel calibration info from EEPROM
  1271. *
  1272. * This info is used to calibrate the baseband power table. Imagine
  1273. * that for each channel there is a power curve that's hw specific
  1274. * (depends on amplifier etc) and we try to "correct" this curve using
  1275. * offsets we pass on to phy chip (baseband -> before amplifier) so that
  1276. * it can use accurate power values when setting tx power (takes amplifier's
  1277. * performance on each channel into account).
  1278. *
  1279. * EEPROM provides us with the offsets for some pre-calibrated channels
  1280. * and we have to interpolate to create the full table for these channels and
  1281. * also the table for any channel.
  1282. */
  1283. static int
  1284. ath5k_eeprom_read_pcal_info(struct ath5k_hw *ah)
  1285. {
  1286. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1287. int (*read_pcal)(struct ath5k_hw *hw, int mode);
  1288. int mode;
  1289. int err;
  1290. if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_4_0) &&
  1291. (AR5K_EEPROM_EEMAP(ee->ee_misc0) == 1))
  1292. read_pcal = ath5k_eeprom_read_pcal_info_5112;
  1293. else if ((ah->ah_ee_version >= AR5K_EEPROM_VERSION_5_0) &&
  1294. (AR5K_EEPROM_EEMAP(ee->ee_misc0) == 2))
  1295. read_pcal = ath5k_eeprom_read_pcal_info_2413;
  1296. else
  1297. read_pcal = ath5k_eeprom_read_pcal_info_5111;
  1298. for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G;
  1299. mode++) {
  1300. err = read_pcal(ah, mode);
  1301. if (err)
  1302. return err;
  1303. err = ath5k_eeprom_read_target_rate_pwr_info(ah, mode);
  1304. if (err < 0)
  1305. return err;
  1306. }
  1307. return 0;
  1308. }
  1309. /* Read conformance test limits used for regulatory control */
  1310. static int
  1311. ath5k_eeprom_read_ctl_info(struct ath5k_hw *ah)
  1312. {
  1313. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1314. struct ath5k_edge_power *rep;
  1315. unsigned int fmask, pmask;
  1316. unsigned int ctl_mode;
  1317. int i, j;
  1318. u32 offset;
  1319. u16 val;
  1320. pmask = AR5K_EEPROM_POWER_M;
  1321. fmask = AR5K_EEPROM_FREQ_M(ee->ee_version);
  1322. offset = AR5K_EEPROM_CTL(ee->ee_version);
  1323. ee->ee_ctls = AR5K_EEPROM_N_CTLS(ee->ee_version);
  1324. for (i = 0; i < ee->ee_ctls; i += 2) {
  1325. AR5K_EEPROM_READ(offset++, val);
  1326. ee->ee_ctl[i] = (val >> 8) & 0xff;
  1327. ee->ee_ctl[i + 1] = val & 0xff;
  1328. }
  1329. offset = AR5K_EEPROM_GROUP8_OFFSET;
  1330. if (ee->ee_version >= AR5K_EEPROM_VERSION_4_0)
  1331. offset += AR5K_EEPROM_TARGET_PWRSTART(ee->ee_misc1) -
  1332. AR5K_EEPROM_GROUP5_OFFSET;
  1333. else
  1334. offset += AR5K_EEPROM_GROUPS_START(ee->ee_version);
  1335. rep = ee->ee_ctl_pwr;
  1336. for (i = 0; i < ee->ee_ctls; i++) {
  1337. switch (ee->ee_ctl[i] & AR5K_CTL_MODE_M) {
  1338. case AR5K_CTL_11A:
  1339. case AR5K_CTL_TURBO:
  1340. ctl_mode = AR5K_EEPROM_MODE_11A;
  1341. break;
  1342. default:
  1343. ctl_mode = AR5K_EEPROM_MODE_11G;
  1344. break;
  1345. }
  1346. if (ee->ee_ctl[i] == 0) {
  1347. if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3)
  1348. offset += 8;
  1349. else
  1350. offset += 7;
  1351. rep += AR5K_EEPROM_N_EDGES;
  1352. continue;
  1353. }
  1354. if (ee->ee_version >= AR5K_EEPROM_VERSION_3_3) {
  1355. for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
  1356. AR5K_EEPROM_READ(offset++, val);
  1357. rep[j].freq = (val >> 8) & fmask;
  1358. rep[j + 1].freq = val & fmask;
  1359. }
  1360. for (j = 0; j < AR5K_EEPROM_N_EDGES; j += 2) {
  1361. AR5K_EEPROM_READ(offset++, val);
  1362. rep[j].edge = (val >> 8) & pmask;
  1363. rep[j].flag = (val >> 14) & 1;
  1364. rep[j + 1].edge = val & pmask;
  1365. rep[j + 1].flag = (val >> 6) & 1;
  1366. }
  1367. } else {
  1368. AR5K_EEPROM_READ(offset++, val);
  1369. rep[0].freq = (val >> 9) & fmask;
  1370. rep[1].freq = (val >> 2) & fmask;
  1371. rep[2].freq = (val << 5) & fmask;
  1372. AR5K_EEPROM_READ(offset++, val);
  1373. rep[2].freq |= (val >> 11) & 0x1f;
  1374. rep[3].freq = (val >> 4) & fmask;
  1375. rep[4].freq = (val << 3) & fmask;
  1376. AR5K_EEPROM_READ(offset++, val);
  1377. rep[4].freq |= (val >> 13) & 0x7;
  1378. rep[5].freq = (val >> 6) & fmask;
  1379. rep[6].freq = (val << 1) & fmask;
  1380. AR5K_EEPROM_READ(offset++, val);
  1381. rep[6].freq |= (val >> 15) & 0x1;
  1382. rep[7].freq = (val >> 8) & fmask;
  1383. rep[0].edge = (val >> 2) & pmask;
  1384. rep[1].edge = (val << 4) & pmask;
  1385. AR5K_EEPROM_READ(offset++, val);
  1386. rep[1].edge |= (val >> 12) & 0xf;
  1387. rep[2].edge = (val >> 6) & pmask;
  1388. rep[3].edge = val & pmask;
  1389. AR5K_EEPROM_READ(offset++, val);
  1390. rep[4].edge = (val >> 10) & pmask;
  1391. rep[5].edge = (val >> 4) & pmask;
  1392. rep[6].edge = (val << 2) & pmask;
  1393. AR5K_EEPROM_READ(offset++, val);
  1394. rep[6].edge |= (val >> 14) & 0x3;
  1395. rep[7].edge = (val >> 8) & pmask;
  1396. }
  1397. for (j = 0; j < AR5K_EEPROM_N_EDGES; j++) {
  1398. rep[j].freq = ath5k_eeprom_bin2freq(ee,
  1399. rep[j].freq, ctl_mode);
  1400. }
  1401. rep += AR5K_EEPROM_N_EDGES;
  1402. }
  1403. return 0;
  1404. }
  1405. static int
  1406. ath5k_eeprom_read_spur_chans(struct ath5k_hw *ah)
  1407. {
  1408. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1409. u32 offset;
  1410. u16 val;
  1411. int ret = 0, i;
  1412. offset = AR5K_EEPROM_CTL(ee->ee_version) +
  1413. AR5K_EEPROM_N_CTLS(ee->ee_version);
  1414. if (ee->ee_version < AR5K_EEPROM_VERSION_5_3) {
  1415. /* No spur info for 5GHz */
  1416. ee->ee_spur_chans[0][0] = AR5K_EEPROM_NO_SPUR;
  1417. /* 2 channels for 2GHz (2464/2420) */
  1418. ee->ee_spur_chans[0][1] = AR5K_EEPROM_5413_SPUR_CHAN_1;
  1419. ee->ee_spur_chans[1][1] = AR5K_EEPROM_5413_SPUR_CHAN_2;
  1420. ee->ee_spur_chans[2][1] = AR5K_EEPROM_NO_SPUR;
  1421. } else if (ee->ee_version >= AR5K_EEPROM_VERSION_5_3) {
  1422. for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
  1423. AR5K_EEPROM_READ(offset, val);
  1424. ee->ee_spur_chans[i][0] = val;
  1425. AR5K_EEPROM_READ(offset + AR5K_EEPROM_N_SPUR_CHANS,
  1426. val);
  1427. ee->ee_spur_chans[i][1] = val;
  1428. offset++;
  1429. }
  1430. }
  1431. return ret;
  1432. }
  1433. /***********************\
  1434. * Init/Detach functions *
  1435. \***********************/
  1436. /*
  1437. * Initialize eeprom data structure
  1438. */
  1439. int
  1440. ath5k_eeprom_init(struct ath5k_hw *ah)
  1441. {
  1442. int err;
  1443. err = ath5k_eeprom_init_header(ah);
  1444. if (err < 0)
  1445. return err;
  1446. err = ath5k_eeprom_init_modes(ah);
  1447. if (err < 0)
  1448. return err;
  1449. err = ath5k_eeprom_read_pcal_info(ah);
  1450. if (err < 0)
  1451. return err;
  1452. err = ath5k_eeprom_read_ctl_info(ah);
  1453. if (err < 0)
  1454. return err;
  1455. err = ath5k_eeprom_read_spur_chans(ah);
  1456. if (err < 0)
  1457. return err;
  1458. return 0;
  1459. }
  1460. void
  1461. ath5k_eeprom_detach(struct ath5k_hw *ah)
  1462. {
  1463. u8 mode;
  1464. for (mode = AR5K_EEPROM_MODE_11A; mode <= AR5K_EEPROM_MODE_11G; mode++)
  1465. ath5k_eeprom_free_pcal_info(ah, mode);
  1466. }
  1467. int
  1468. ath5k_eeprom_mode_from_channel(struct ath5k_hw *ah,
  1469. struct ieee80211_channel *channel)
  1470. {
  1471. switch (channel->hw_value) {
  1472. case AR5K_MODE_11A:
  1473. return AR5K_EEPROM_MODE_11A;
  1474. case AR5K_MODE_11G:
  1475. return AR5K_EEPROM_MODE_11G;
  1476. case AR5K_MODE_11B:
  1477. return AR5K_EEPROM_MODE_11B;
  1478. default:
  1479. ATH5K_WARN(ah, "channel is not A/B/G!");
  1480. return AR5K_EEPROM_MODE_11A;
  1481. }
  1482. }