eeprom.c 48 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797
  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. }