eeprom.c 78 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824
  1. /*
  2. * Copyright (c) 2008 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include "core.h"
  17. #include "hw.h"
  18. #include "reg.h"
  19. #include "phy.h"
  20. static void ath9k_hw_analog_shift_rmw(struct ath_hal *ah,
  21. u32 reg, u32 mask,
  22. u32 shift, u32 val)
  23. {
  24. u32 regVal;
  25. regVal = REG_READ(ah, reg) & ~mask;
  26. regVal |= (val << shift) & mask;
  27. REG_WRITE(ah, reg, regVal);
  28. if (ah->ah_config.analog_shiftreg)
  29. udelay(100);
  30. return;
  31. }
  32. static inline u16 ath9k_hw_fbin2freq(u8 fbin, bool is2GHz)
  33. {
  34. if (fbin == AR5416_BCHAN_UNUSED)
  35. return fbin;
  36. return (u16) ((is2GHz) ? (2300 + fbin) : (4800 + 5 * fbin));
  37. }
  38. static inline int16_t ath9k_hw_interpolate(u16 target,
  39. u16 srcLeft, u16 srcRight,
  40. int16_t targetLeft,
  41. int16_t targetRight)
  42. {
  43. int16_t rv;
  44. if (srcRight == srcLeft) {
  45. rv = targetLeft;
  46. } else {
  47. rv = (int16_t) (((target - srcLeft) * targetRight +
  48. (srcRight - target) * targetLeft) /
  49. (srcRight - srcLeft));
  50. }
  51. return rv;
  52. }
  53. static inline bool ath9k_hw_get_lower_upper_index(u8 target, u8 *pList,
  54. u16 listSize, u16 *indexL,
  55. u16 *indexR)
  56. {
  57. u16 i;
  58. if (target <= pList[0]) {
  59. *indexL = *indexR = 0;
  60. return true;
  61. }
  62. if (target >= pList[listSize - 1]) {
  63. *indexL = *indexR = (u16) (listSize - 1);
  64. return true;
  65. }
  66. for (i = 0; i < listSize - 1; i++) {
  67. if (pList[i] == target) {
  68. *indexL = *indexR = i;
  69. return true;
  70. }
  71. if (target < pList[i + 1]) {
  72. *indexL = i;
  73. *indexR = (u16) (i + 1);
  74. return false;
  75. }
  76. }
  77. return false;
  78. }
  79. static bool ath9k_hw_eeprom_read(struct ath_hal *ah, u32 off, u16 *data)
  80. {
  81. (void)REG_READ(ah, AR5416_EEPROM_OFFSET + (off << AR5416_EEPROM_S));
  82. if (!ath9k_hw_wait(ah,
  83. AR_EEPROM_STATUS_DATA,
  84. AR_EEPROM_STATUS_DATA_BUSY |
  85. AR_EEPROM_STATUS_DATA_PROT_ACCESS, 0)) {
  86. return false;
  87. }
  88. *data = MS(REG_READ(ah, AR_EEPROM_STATUS_DATA),
  89. AR_EEPROM_STATUS_DATA_VAL);
  90. return true;
  91. }
  92. static int ath9k_hw_flash_map(struct ath_hal *ah)
  93. {
  94. struct ath_hal_5416 *ahp = AH5416(ah);
  95. ahp->ah_cal_mem = ioremap(AR5416_EEPROM_START_ADDR, AR5416_EEPROM_MAX);
  96. if (!ahp->ah_cal_mem) {
  97. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  98. "cannot remap eeprom region \n");
  99. return -EIO;
  100. }
  101. return 0;
  102. }
  103. static bool ath9k_hw_flash_read(struct ath_hal *ah, u32 off, u16 *data)
  104. {
  105. struct ath_hal_5416 *ahp = AH5416(ah);
  106. *data = ioread16(ahp->ah_cal_mem + off);
  107. return true;
  108. }
  109. static inline bool ath9k_hw_nvram_read(struct ath_hal *ah, u32 off, u16 *data)
  110. {
  111. if (ath9k_hw_use_flash(ah))
  112. return ath9k_hw_flash_read(ah, off, data);
  113. else
  114. return ath9k_hw_eeprom_read(ah, off, data);
  115. }
  116. static bool ath9k_hw_fill_4k_eeprom(struct ath_hal *ah)
  117. {
  118. #define SIZE_EEPROM_4K (sizeof(struct ar5416_eeprom_4k) / sizeof(u16))
  119. struct ath_hal_5416 *ahp = AH5416(ah);
  120. struct ar5416_eeprom_4k *eep = &ahp->ah_eeprom.map4k;
  121. u16 *eep_data;
  122. int addr, eep_start_loc = 0;
  123. eep_start_loc = 64;
  124. if (!ath9k_hw_use_flash(ah)) {
  125. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  126. "Reading from EEPROM, not flash\n");
  127. }
  128. eep_data = (u16 *)eep;
  129. for (addr = 0; addr < SIZE_EEPROM_4K; addr++) {
  130. if (!ath9k_hw_nvram_read(ah, addr + eep_start_loc, eep_data)) {
  131. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  132. "Unable to read eeprom region \n");
  133. return false;
  134. }
  135. eep_data++;
  136. }
  137. return true;
  138. #undef SIZE_EEPROM_4K
  139. }
  140. static bool ath9k_hw_fill_def_eeprom(struct ath_hal *ah)
  141. {
  142. #define SIZE_EEPROM_DEF (sizeof(struct ar5416_eeprom_def) / sizeof(u16))
  143. struct ath_hal_5416 *ahp = AH5416(ah);
  144. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  145. u16 *eep_data;
  146. int addr, ar5416_eep_start_loc = 0x100;
  147. eep_data = (u16 *)eep;
  148. for (addr = 0; addr < SIZE_EEPROM_DEF; addr++) {
  149. if (!ath9k_hw_nvram_read(ah, addr + ar5416_eep_start_loc,
  150. eep_data)) {
  151. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  152. "Unable to read eeprom region\n");
  153. return false;
  154. }
  155. eep_data++;
  156. }
  157. return true;
  158. #undef SIZE_EEPROM_DEF
  159. }
  160. static bool (*ath9k_fill_eeprom[]) (struct ath_hal *) = {
  161. ath9k_hw_fill_def_eeprom,
  162. ath9k_hw_fill_4k_eeprom
  163. };
  164. static inline bool ath9k_hw_fill_eeprom(struct ath_hal *ah)
  165. {
  166. struct ath_hal_5416 *ahp = AH5416(ah);
  167. return ath9k_fill_eeprom[ahp->ah_eep_map](ah);
  168. }
  169. static int ath9k_hw_check_def_eeprom(struct ath_hal *ah)
  170. {
  171. struct ath_hal_5416 *ahp = AH5416(ah);
  172. struct ar5416_eeprom_def *eep =
  173. (struct ar5416_eeprom_def *) &ahp->ah_eeprom.def;
  174. u16 *eepdata, temp, magic, magic2;
  175. u32 sum = 0, el;
  176. bool need_swap = false;
  177. int i, addr, size;
  178. if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET,
  179. &magic)) {
  180. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  181. "Reading Magic # failed\n");
  182. return false;
  183. }
  184. if (!ath9k_hw_use_flash(ah)) {
  185. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  186. "Read Magic = 0x%04X\n", magic);
  187. if (magic != AR5416_EEPROM_MAGIC) {
  188. magic2 = swab16(magic);
  189. if (magic2 == AR5416_EEPROM_MAGIC) {
  190. size = sizeof(struct ar5416_eeprom_def);
  191. need_swap = true;
  192. eepdata = (u16 *) (&ahp->ah_eeprom);
  193. for (addr = 0; addr < size / sizeof(u16); addr++) {
  194. temp = swab16(*eepdata);
  195. *eepdata = temp;
  196. eepdata++;
  197. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  198. "0x%04X ", *eepdata);
  199. if (((addr + 1) % 6) == 0)
  200. DPRINTF(ah->ah_sc,
  201. ATH_DBG_EEPROM, "\n");
  202. }
  203. } else {
  204. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  205. "Invalid EEPROM Magic. "
  206. "endianness mismatch.\n");
  207. return -EINVAL;
  208. }
  209. }
  210. }
  211. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n",
  212. need_swap ? "True" : "False");
  213. if (need_swap)
  214. el = swab16(ahp->ah_eeprom.def.baseEepHeader.length);
  215. else
  216. el = ahp->ah_eeprom.def.baseEepHeader.length;
  217. if (el > sizeof(struct ar5416_eeprom_def))
  218. el = sizeof(struct ar5416_eeprom_def) / sizeof(u16);
  219. else
  220. el = el / sizeof(u16);
  221. eepdata = (u16 *)(&ahp->ah_eeprom);
  222. for (i = 0; i < el; i++)
  223. sum ^= *eepdata++;
  224. if (need_swap) {
  225. u32 integer, j;
  226. u16 word;
  227. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  228. "EEPROM Endianness is not native.. Changing \n");
  229. word = swab16(eep->baseEepHeader.length);
  230. eep->baseEepHeader.length = word;
  231. word = swab16(eep->baseEepHeader.checksum);
  232. eep->baseEepHeader.checksum = word;
  233. word = swab16(eep->baseEepHeader.version);
  234. eep->baseEepHeader.version = word;
  235. word = swab16(eep->baseEepHeader.regDmn[0]);
  236. eep->baseEepHeader.regDmn[0] = word;
  237. word = swab16(eep->baseEepHeader.regDmn[1]);
  238. eep->baseEepHeader.regDmn[1] = word;
  239. word = swab16(eep->baseEepHeader.rfSilent);
  240. eep->baseEepHeader.rfSilent = word;
  241. word = swab16(eep->baseEepHeader.blueToothOptions);
  242. eep->baseEepHeader.blueToothOptions = word;
  243. word = swab16(eep->baseEepHeader.deviceCap);
  244. eep->baseEepHeader.deviceCap = word;
  245. for (j = 0; j < ARRAY_SIZE(eep->modalHeader); j++) {
  246. struct modal_eep_header *pModal =
  247. &eep->modalHeader[j];
  248. integer = swab32(pModal->antCtrlCommon);
  249. pModal->antCtrlCommon = integer;
  250. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  251. integer = swab32(pModal->antCtrlChain[i]);
  252. pModal->antCtrlChain[i] = integer;
  253. }
  254. for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
  255. word = swab16(pModal->spurChans[i].spurChan);
  256. pModal->spurChans[i].spurChan = word;
  257. }
  258. }
  259. }
  260. if (sum != 0xffff || ar5416_get_eep_ver(ahp) != AR5416_EEP_VER ||
  261. ar5416_get_eep_rev(ahp) < AR5416_EEP_NO_BACK_VER) {
  262. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  263. "Bad EEPROM checksum 0x%x or revision 0x%04x\n",
  264. sum, ar5416_get_eep_ver(ahp));
  265. return -EINVAL;
  266. }
  267. return 0;
  268. }
  269. static int ath9k_hw_check_4k_eeprom(struct ath_hal *ah)
  270. {
  271. #define EEPROM_4K_SIZE (sizeof(struct ar5416_eeprom_4k) / sizeof(u16))
  272. struct ath_hal_5416 *ahp = AH5416(ah);
  273. struct ar5416_eeprom_4k *eep =
  274. (struct ar5416_eeprom_4k *) &ahp->ah_eeprom.map4k;
  275. u16 *eepdata, temp, magic, magic2;
  276. u32 sum = 0, el;
  277. bool need_swap = false;
  278. int i, addr;
  279. if (!ath9k_hw_use_flash(ah)) {
  280. if (!ath9k_hw_nvram_read(ah, AR5416_EEPROM_MAGIC_OFFSET,
  281. &magic)) {
  282. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  283. "Reading Magic # failed\n");
  284. return false;
  285. }
  286. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  287. "Read Magic = 0x%04X\n", magic);
  288. if (magic != AR5416_EEPROM_MAGIC) {
  289. magic2 = swab16(magic);
  290. if (magic2 == AR5416_EEPROM_MAGIC) {
  291. need_swap = true;
  292. eepdata = (u16 *) (&ahp->ah_eeprom);
  293. for (addr = 0; addr < EEPROM_4K_SIZE; addr++) {
  294. temp = swab16(*eepdata);
  295. *eepdata = temp;
  296. eepdata++;
  297. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  298. "0x%04X ", *eepdata);
  299. if (((addr + 1) % 6) == 0)
  300. DPRINTF(ah->ah_sc,
  301. ATH_DBG_EEPROM, "\n");
  302. }
  303. } else {
  304. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  305. "Invalid EEPROM Magic. "
  306. "endianness mismatch.\n");
  307. return -EINVAL;
  308. }
  309. }
  310. }
  311. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "need_swap = %s.\n",
  312. need_swap ? "True" : "False");
  313. if (need_swap)
  314. el = swab16(ahp->ah_eeprom.map4k.baseEepHeader.length);
  315. else
  316. el = ahp->ah_eeprom.map4k.baseEepHeader.length;
  317. if (el > sizeof(struct ar5416_eeprom_def))
  318. el = sizeof(struct ar5416_eeprom_4k) / sizeof(u16);
  319. else
  320. el = el / sizeof(u16);
  321. eepdata = (u16 *)(&ahp->ah_eeprom);
  322. for (i = 0; i < el; i++)
  323. sum ^= *eepdata++;
  324. if (need_swap) {
  325. u32 integer;
  326. u16 word;
  327. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  328. "EEPROM Endianness is not native.. Changing \n");
  329. word = swab16(eep->baseEepHeader.length);
  330. eep->baseEepHeader.length = word;
  331. word = swab16(eep->baseEepHeader.checksum);
  332. eep->baseEepHeader.checksum = word;
  333. word = swab16(eep->baseEepHeader.version);
  334. eep->baseEepHeader.version = word;
  335. word = swab16(eep->baseEepHeader.regDmn[0]);
  336. eep->baseEepHeader.regDmn[0] = word;
  337. word = swab16(eep->baseEepHeader.regDmn[1]);
  338. eep->baseEepHeader.regDmn[1] = word;
  339. word = swab16(eep->baseEepHeader.rfSilent);
  340. eep->baseEepHeader.rfSilent = word;
  341. word = swab16(eep->baseEepHeader.blueToothOptions);
  342. eep->baseEepHeader.blueToothOptions = word;
  343. word = swab16(eep->baseEepHeader.deviceCap);
  344. eep->baseEepHeader.deviceCap = word;
  345. integer = swab32(eep->modalHeader.antCtrlCommon);
  346. eep->modalHeader.antCtrlCommon = integer;
  347. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  348. integer = swab32(eep->modalHeader.antCtrlChain[i]);
  349. eep->modalHeader.antCtrlChain[i] = integer;
  350. }
  351. for (i = 0; i < AR5416_EEPROM_MODAL_SPURS; i++) {
  352. word = swab16(eep->modalHeader.spurChans[i].spurChan);
  353. eep->modalHeader.spurChans[i].spurChan = word;
  354. }
  355. }
  356. if (sum != 0xffff || ar5416_get_eep4k_ver(ahp) != AR5416_EEP_VER ||
  357. ar5416_get_eep4k_rev(ahp) < AR5416_EEP_NO_BACK_VER) {
  358. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  359. "Bad EEPROM checksum 0x%x or revision 0x%04x\n",
  360. sum, ar5416_get_eep4k_ver(ahp));
  361. return -EINVAL;
  362. }
  363. return 0;
  364. #undef EEPROM_4K_SIZE
  365. }
  366. static int (*ath9k_check_eeprom[]) (struct ath_hal *) = {
  367. ath9k_hw_check_def_eeprom,
  368. ath9k_hw_check_4k_eeprom
  369. };
  370. static inline int ath9k_hw_check_eeprom(struct ath_hal *ah)
  371. {
  372. struct ath_hal_5416 *ahp = AH5416(ah);
  373. return ath9k_check_eeprom[ahp->ah_eep_map](ah);
  374. }
  375. static inline bool ath9k_hw_fill_vpd_table(u8 pwrMin, u8 pwrMax, u8 *pPwrList,
  376. u8 *pVpdList, u16 numIntercepts,
  377. u8 *pRetVpdList)
  378. {
  379. u16 i, k;
  380. u8 currPwr = pwrMin;
  381. u16 idxL = 0, idxR = 0;
  382. for (i = 0; i <= (pwrMax - pwrMin) / 2; i++) {
  383. ath9k_hw_get_lower_upper_index(currPwr, pPwrList,
  384. numIntercepts, &(idxL),
  385. &(idxR));
  386. if (idxR < 1)
  387. idxR = 1;
  388. if (idxL == numIntercepts - 1)
  389. idxL = (u16) (numIntercepts - 2);
  390. if (pPwrList[idxL] == pPwrList[idxR])
  391. k = pVpdList[idxL];
  392. else
  393. k = (u16)(((currPwr - pPwrList[idxL]) * pVpdList[idxR] +
  394. (pPwrList[idxR] - currPwr) * pVpdList[idxL]) /
  395. (pPwrList[idxR] - pPwrList[idxL]));
  396. pRetVpdList[i] = (u8) k;
  397. currPwr += 2;
  398. }
  399. return true;
  400. }
  401. static void ath9k_hw_get_4k_gain_boundaries_pdadcs(struct ath_hal *ah,
  402. struct ath9k_channel *chan,
  403. struct cal_data_per_freq_4k *pRawDataSet,
  404. u8 *bChans, u16 availPiers,
  405. u16 tPdGainOverlap, int16_t *pMinCalPower,
  406. u16 *pPdGainBoundaries, u8 *pPDADCValues,
  407. u16 numXpdGains)
  408. {
  409. #define TMP_VAL_VPD_TABLE \
  410. ((vpdTableI[i][sizeCurrVpdTable - 1] + (ss - maxIndex + 1) * vpdStep));
  411. int i, j, k;
  412. int16_t ss;
  413. u16 idxL = 0, idxR = 0, numPiers;
  414. static u8 vpdTableL[AR5416_EEP4K_NUM_PD_GAINS]
  415. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  416. static u8 vpdTableR[AR5416_EEP4K_NUM_PD_GAINS]
  417. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  418. static u8 vpdTableI[AR5416_EEP4K_NUM_PD_GAINS]
  419. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  420. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  421. u8 minPwrT4[AR5416_EEP4K_NUM_PD_GAINS];
  422. u8 maxPwrT4[AR5416_EEP4K_NUM_PD_GAINS];
  423. int16_t vpdStep;
  424. int16_t tmpVal;
  425. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  426. bool match;
  427. int16_t minDelta = 0;
  428. struct chan_centers centers;
  429. #define PD_GAIN_BOUNDARY_DEFAULT 58;
  430. ath9k_hw_get_channel_centers(ah, chan, &centers);
  431. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  432. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  433. break;
  434. }
  435. match = ath9k_hw_get_lower_upper_index(
  436. (u8)FREQ2FBIN(centers.synth_center,
  437. IS_CHAN_2GHZ(chan)), bChans, numPiers,
  438. &idxL, &idxR);
  439. if (match) {
  440. for (i = 0; i < numXpdGains; i++) {
  441. minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
  442. maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
  443. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  444. pRawDataSet[idxL].pwrPdg[i],
  445. pRawDataSet[idxL].vpdPdg[i],
  446. AR5416_EEP4K_PD_GAIN_ICEPTS,
  447. vpdTableI[i]);
  448. }
  449. } else {
  450. for (i = 0; i < numXpdGains; i++) {
  451. pVpdL = pRawDataSet[idxL].vpdPdg[i];
  452. pPwrL = pRawDataSet[idxL].pwrPdg[i];
  453. pVpdR = pRawDataSet[idxR].vpdPdg[i];
  454. pPwrR = pRawDataSet[idxR].pwrPdg[i];
  455. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  456. maxPwrT4[i] =
  457. min(pPwrL[AR5416_EEP4K_PD_GAIN_ICEPTS - 1],
  458. pPwrR[AR5416_EEP4K_PD_GAIN_ICEPTS - 1]);
  459. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  460. pPwrL, pVpdL,
  461. AR5416_EEP4K_PD_GAIN_ICEPTS,
  462. vpdTableL[i]);
  463. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  464. pPwrR, pVpdR,
  465. AR5416_EEP4K_PD_GAIN_ICEPTS,
  466. vpdTableR[i]);
  467. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  468. vpdTableI[i][j] =
  469. (u8)(ath9k_hw_interpolate((u16)
  470. FREQ2FBIN(centers.
  471. synth_center,
  472. IS_CHAN_2GHZ
  473. (chan)),
  474. bChans[idxL], bChans[idxR],
  475. vpdTableL[i][j], vpdTableR[i][j]));
  476. }
  477. }
  478. }
  479. *pMinCalPower = (int16_t)(minPwrT4[0] / 2);
  480. k = 0;
  481. for (i = 0; i < numXpdGains; i++) {
  482. if (i == (numXpdGains - 1))
  483. pPdGainBoundaries[i] =
  484. (u16)(maxPwrT4[i] / 2);
  485. else
  486. pPdGainBoundaries[i] =
  487. (u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);
  488. pPdGainBoundaries[i] =
  489. min((u16)AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);
  490. if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah)) {
  491. minDelta = pPdGainBoundaries[0] - 23;
  492. pPdGainBoundaries[0] = 23;
  493. } else {
  494. minDelta = 0;
  495. }
  496. if (i == 0) {
  497. if (AR_SREV_9280_10_OR_LATER(ah))
  498. ss = (int16_t)(0 - (minPwrT4[i] / 2));
  499. else
  500. ss = 0;
  501. } else {
  502. ss = (int16_t)((pPdGainBoundaries[i - 1] -
  503. (minPwrT4[i] / 2)) -
  504. tPdGainOverlap + 1 + minDelta);
  505. }
  506. vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
  507. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  508. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  509. tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
  510. pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
  511. ss++;
  512. }
  513. sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  514. tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
  515. (minPwrT4[i] / 2));
  516. maxIndex = (tgtIndex < sizeCurrVpdTable) ?
  517. tgtIndex : sizeCurrVpdTable;
  518. while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1)))
  519. pPDADCValues[k++] = vpdTableI[i][ss++];
  520. vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
  521. vpdTableI[i][sizeCurrVpdTable - 2]);
  522. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  523. if (tgtIndex > maxIndex) {
  524. while ((ss <= tgtIndex) &&
  525. (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  526. tmpVal = (int16_t) TMP_VAL_VPD_TABLE;
  527. pPDADCValues[k++] = (u8)((tmpVal > 255) ?
  528. 255 : tmpVal);
  529. ss++;
  530. }
  531. }
  532. }
  533. while (i < AR5416_EEP4K_PD_GAINS_IN_MASK) {
  534. pPdGainBoundaries[i] = PD_GAIN_BOUNDARY_DEFAULT;
  535. i++;
  536. }
  537. while (k < AR5416_NUM_PDADC_VALUES) {
  538. pPDADCValues[k] = pPDADCValues[k - 1];
  539. k++;
  540. }
  541. return;
  542. #undef TMP_VAL_VPD_TABLE
  543. }
  544. static void ath9k_hw_get_def_gain_boundaries_pdadcs(struct ath_hal *ah,
  545. struct ath9k_channel *chan,
  546. struct cal_data_per_freq *pRawDataSet,
  547. u8 *bChans, u16 availPiers,
  548. u16 tPdGainOverlap, int16_t *pMinCalPower,
  549. u16 *pPdGainBoundaries, u8 *pPDADCValues,
  550. u16 numXpdGains)
  551. {
  552. int i, j, k;
  553. int16_t ss;
  554. u16 idxL = 0, idxR = 0, numPiers;
  555. static u8 vpdTableL[AR5416_NUM_PD_GAINS]
  556. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  557. static u8 vpdTableR[AR5416_NUM_PD_GAINS]
  558. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  559. static u8 vpdTableI[AR5416_NUM_PD_GAINS]
  560. [AR5416_MAX_PWR_RANGE_IN_HALF_DB];
  561. u8 *pVpdL, *pVpdR, *pPwrL, *pPwrR;
  562. u8 minPwrT4[AR5416_NUM_PD_GAINS];
  563. u8 maxPwrT4[AR5416_NUM_PD_GAINS];
  564. int16_t vpdStep;
  565. int16_t tmpVal;
  566. u16 sizeCurrVpdTable, maxIndex, tgtIndex;
  567. bool match;
  568. int16_t minDelta = 0;
  569. struct chan_centers centers;
  570. ath9k_hw_get_channel_centers(ah, chan, &centers);
  571. for (numPiers = 0; numPiers < availPiers; numPiers++) {
  572. if (bChans[numPiers] == AR5416_BCHAN_UNUSED)
  573. break;
  574. }
  575. match = ath9k_hw_get_lower_upper_index((u8)FREQ2FBIN(centers.synth_center,
  576. IS_CHAN_2GHZ(chan)),
  577. bChans, numPiers, &idxL, &idxR);
  578. if (match) {
  579. for (i = 0; i < numXpdGains; i++) {
  580. minPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][0];
  581. maxPwrT4[i] = pRawDataSet[idxL].pwrPdg[i][4];
  582. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  583. pRawDataSet[idxL].pwrPdg[i],
  584. pRawDataSet[idxL].vpdPdg[i],
  585. AR5416_PD_GAIN_ICEPTS,
  586. vpdTableI[i]);
  587. }
  588. } else {
  589. for (i = 0; i < numXpdGains; i++) {
  590. pVpdL = pRawDataSet[idxL].vpdPdg[i];
  591. pPwrL = pRawDataSet[idxL].pwrPdg[i];
  592. pVpdR = pRawDataSet[idxR].vpdPdg[i];
  593. pPwrR = pRawDataSet[idxR].pwrPdg[i];
  594. minPwrT4[i] = max(pPwrL[0], pPwrR[0]);
  595. maxPwrT4[i] =
  596. min(pPwrL[AR5416_PD_GAIN_ICEPTS - 1],
  597. pPwrR[AR5416_PD_GAIN_ICEPTS - 1]);
  598. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  599. pPwrL, pVpdL,
  600. AR5416_PD_GAIN_ICEPTS,
  601. vpdTableL[i]);
  602. ath9k_hw_fill_vpd_table(minPwrT4[i], maxPwrT4[i],
  603. pPwrR, pVpdR,
  604. AR5416_PD_GAIN_ICEPTS,
  605. vpdTableR[i]);
  606. for (j = 0; j <= (maxPwrT4[i] - minPwrT4[i]) / 2; j++) {
  607. vpdTableI[i][j] =
  608. (u8)(ath9k_hw_interpolate((u16)
  609. FREQ2FBIN(centers.
  610. synth_center,
  611. IS_CHAN_2GHZ
  612. (chan)),
  613. bChans[idxL], bChans[idxR],
  614. vpdTableL[i][j], vpdTableR[i][j]));
  615. }
  616. }
  617. }
  618. *pMinCalPower = (int16_t)(minPwrT4[0] / 2);
  619. k = 0;
  620. for (i = 0; i < numXpdGains; i++) {
  621. if (i == (numXpdGains - 1))
  622. pPdGainBoundaries[i] =
  623. (u16)(maxPwrT4[i] / 2);
  624. else
  625. pPdGainBoundaries[i] =
  626. (u16)((maxPwrT4[i] + minPwrT4[i + 1]) / 4);
  627. pPdGainBoundaries[i] =
  628. min((u16)AR5416_MAX_RATE_POWER, pPdGainBoundaries[i]);
  629. if ((i == 0) && !AR_SREV_5416_V20_OR_LATER(ah)) {
  630. minDelta = pPdGainBoundaries[0] - 23;
  631. pPdGainBoundaries[0] = 23;
  632. } else {
  633. minDelta = 0;
  634. }
  635. if (i == 0) {
  636. if (AR_SREV_9280_10_OR_LATER(ah))
  637. ss = (int16_t)(0 - (minPwrT4[i] / 2));
  638. else
  639. ss = 0;
  640. } else {
  641. ss = (int16_t)((pPdGainBoundaries[i - 1] -
  642. (minPwrT4[i] / 2)) -
  643. tPdGainOverlap + 1 + minDelta);
  644. }
  645. vpdStep = (int16_t)(vpdTableI[i][1] - vpdTableI[i][0]);
  646. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  647. while ((ss < 0) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  648. tmpVal = (int16_t)(vpdTableI[i][0] + ss * vpdStep);
  649. pPDADCValues[k++] = (u8)((tmpVal < 0) ? 0 : tmpVal);
  650. ss++;
  651. }
  652. sizeCurrVpdTable = (u8) ((maxPwrT4[i] - minPwrT4[i]) / 2 + 1);
  653. tgtIndex = (u8)(pPdGainBoundaries[i] + tPdGainOverlap -
  654. (minPwrT4[i] / 2));
  655. maxIndex = (tgtIndex < sizeCurrVpdTable) ?
  656. tgtIndex : sizeCurrVpdTable;
  657. while ((ss < maxIndex) && (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  658. pPDADCValues[k++] = vpdTableI[i][ss++];
  659. }
  660. vpdStep = (int16_t)(vpdTableI[i][sizeCurrVpdTable - 1] -
  661. vpdTableI[i][sizeCurrVpdTable - 2]);
  662. vpdStep = (int16_t)((vpdStep < 1) ? 1 : vpdStep);
  663. if (tgtIndex > maxIndex) {
  664. while ((ss <= tgtIndex) &&
  665. (k < (AR5416_NUM_PDADC_VALUES - 1))) {
  666. tmpVal = (int16_t)((vpdTableI[i][sizeCurrVpdTable - 1] +
  667. (ss - maxIndex + 1) * vpdStep));
  668. pPDADCValues[k++] = (u8)((tmpVal > 255) ?
  669. 255 : tmpVal);
  670. ss++;
  671. }
  672. }
  673. }
  674. while (i < AR5416_PD_GAINS_IN_MASK) {
  675. pPdGainBoundaries[i] = pPdGainBoundaries[i - 1];
  676. i++;
  677. }
  678. while (k < AR5416_NUM_PDADC_VALUES) {
  679. pPDADCValues[k] = pPDADCValues[k - 1];
  680. k++;
  681. }
  682. return;
  683. }
  684. static void ath9k_hw_get_legacy_target_powers(struct ath_hal *ah,
  685. struct ath9k_channel *chan,
  686. struct cal_target_power_leg *powInfo,
  687. u16 numChannels,
  688. struct cal_target_power_leg *pNewPower,
  689. u16 numRates, bool isExtTarget)
  690. {
  691. struct chan_centers centers;
  692. u16 clo, chi;
  693. int i;
  694. int matchIndex = -1, lowIndex = -1;
  695. u16 freq;
  696. ath9k_hw_get_channel_centers(ah, chan, &centers);
  697. freq = (isExtTarget) ? centers.ext_center : centers.ctl_center;
  698. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel,
  699. IS_CHAN_2GHZ(chan))) {
  700. matchIndex = 0;
  701. } else {
  702. for (i = 0; (i < numChannels) &&
  703. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  704. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  705. IS_CHAN_2GHZ(chan))) {
  706. matchIndex = i;
  707. break;
  708. } else if ((freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  709. IS_CHAN_2GHZ(chan))) &&
  710. (freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  711. IS_CHAN_2GHZ(chan)))) {
  712. lowIndex = i - 1;
  713. break;
  714. }
  715. }
  716. if ((matchIndex == -1) && (lowIndex == -1))
  717. matchIndex = i - 1;
  718. }
  719. if (matchIndex != -1) {
  720. *pNewPower = powInfo[matchIndex];
  721. } else {
  722. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  723. IS_CHAN_2GHZ(chan));
  724. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  725. IS_CHAN_2GHZ(chan));
  726. for (i = 0; i < numRates; i++) {
  727. pNewPower->tPow2x[i] =
  728. (u8)ath9k_hw_interpolate(freq, clo, chi,
  729. powInfo[lowIndex].tPow2x[i],
  730. powInfo[lowIndex + 1].tPow2x[i]);
  731. }
  732. }
  733. }
  734. static void ath9k_hw_get_target_powers(struct ath_hal *ah,
  735. struct ath9k_channel *chan,
  736. struct cal_target_power_ht *powInfo,
  737. u16 numChannels,
  738. struct cal_target_power_ht *pNewPower,
  739. u16 numRates, bool isHt40Target)
  740. {
  741. struct chan_centers centers;
  742. u16 clo, chi;
  743. int i;
  744. int matchIndex = -1, lowIndex = -1;
  745. u16 freq;
  746. ath9k_hw_get_channel_centers(ah, chan, &centers);
  747. freq = isHt40Target ? centers.synth_center : centers.ctl_center;
  748. if (freq <= ath9k_hw_fbin2freq(powInfo[0].bChannel, IS_CHAN_2GHZ(chan))) {
  749. matchIndex = 0;
  750. } else {
  751. for (i = 0; (i < numChannels) &&
  752. (powInfo[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  753. if (freq == ath9k_hw_fbin2freq(powInfo[i].bChannel,
  754. IS_CHAN_2GHZ(chan))) {
  755. matchIndex = i;
  756. break;
  757. } else
  758. if ((freq < ath9k_hw_fbin2freq(powInfo[i].bChannel,
  759. IS_CHAN_2GHZ(chan))) &&
  760. (freq > ath9k_hw_fbin2freq(powInfo[i - 1].bChannel,
  761. IS_CHAN_2GHZ(chan)))) {
  762. lowIndex = i - 1;
  763. break;
  764. }
  765. }
  766. if ((matchIndex == -1) && (lowIndex == -1))
  767. matchIndex = i - 1;
  768. }
  769. if (matchIndex != -1) {
  770. *pNewPower = powInfo[matchIndex];
  771. } else {
  772. clo = ath9k_hw_fbin2freq(powInfo[lowIndex].bChannel,
  773. IS_CHAN_2GHZ(chan));
  774. chi = ath9k_hw_fbin2freq(powInfo[lowIndex + 1].bChannel,
  775. IS_CHAN_2GHZ(chan));
  776. for (i = 0; i < numRates; i++) {
  777. pNewPower->tPow2x[i] = (u8)ath9k_hw_interpolate(freq,
  778. clo, chi,
  779. powInfo[lowIndex].tPow2x[i],
  780. powInfo[lowIndex + 1].tPow2x[i]);
  781. }
  782. }
  783. }
  784. static u16 ath9k_hw_get_max_edge_power(u16 freq,
  785. struct cal_ctl_edges *pRdEdgesPower,
  786. bool is2GHz, int num_band_edges)
  787. {
  788. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  789. int i;
  790. for (i = 0; (i < num_band_edges) &&
  791. (pRdEdgesPower[i].bChannel != AR5416_BCHAN_UNUSED); i++) {
  792. if (freq == ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel, is2GHz)) {
  793. twiceMaxEdgePower = pRdEdgesPower[i].tPower;
  794. break;
  795. } else if ((i > 0) &&
  796. (freq < ath9k_hw_fbin2freq(pRdEdgesPower[i].bChannel,
  797. is2GHz))) {
  798. if (ath9k_hw_fbin2freq(pRdEdgesPower[i - 1].bChannel,
  799. is2GHz) < freq &&
  800. pRdEdgesPower[i - 1].flag) {
  801. twiceMaxEdgePower =
  802. pRdEdgesPower[i - 1].tPower;
  803. }
  804. break;
  805. }
  806. }
  807. return twiceMaxEdgePower;
  808. }
  809. static bool ath9k_hw_set_def_power_cal_table(struct ath_hal *ah,
  810. struct ath9k_channel *chan,
  811. int16_t *pTxPowerIndexOffset)
  812. {
  813. struct ath_hal_5416 *ahp = AH5416(ah);
  814. struct ar5416_eeprom_def *pEepData = &ahp->ah_eeprom.def;
  815. struct cal_data_per_freq *pRawDataset;
  816. u8 *pCalBChans = NULL;
  817. u16 pdGainOverlap_t2;
  818. static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
  819. u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
  820. u16 numPiers, i, j;
  821. int16_t tMinCalPower;
  822. u16 numXpdGain, xpdMask;
  823. u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
  824. u32 reg32, regOffset, regChainOffset;
  825. int16_t modalIdx;
  826. modalIdx = IS_CHAN_2GHZ(chan) ? 1 : 0;
  827. xpdMask = pEepData->modalHeader[modalIdx].xpdGain;
  828. if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  829. AR5416_EEP_MINOR_VER_2) {
  830. pdGainOverlap_t2 =
  831. pEepData->modalHeader[modalIdx].pdGainOverlap;
  832. } else {
  833. pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
  834. AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
  835. }
  836. if (IS_CHAN_2GHZ(chan)) {
  837. pCalBChans = pEepData->calFreqPier2G;
  838. numPiers = AR5416_NUM_2G_CAL_PIERS;
  839. } else {
  840. pCalBChans = pEepData->calFreqPier5G;
  841. numPiers = AR5416_NUM_5G_CAL_PIERS;
  842. }
  843. numXpdGain = 0;
  844. for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
  845. if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
  846. if (numXpdGain >= AR5416_NUM_PD_GAINS)
  847. break;
  848. xpdGainValues[numXpdGain] =
  849. (u16)(AR5416_PD_GAINS_IN_MASK - i);
  850. numXpdGain++;
  851. }
  852. }
  853. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
  854. (numXpdGain - 1) & 0x3);
  855. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
  856. xpdGainValues[0]);
  857. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
  858. xpdGainValues[1]);
  859. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
  860. xpdGainValues[2]);
  861. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  862. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  863. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5) &&
  864. (i != 0)) {
  865. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  866. } else
  867. regChainOffset = i * 0x1000;
  868. if (pEepData->baseEepHeader.txMask & (1 << i)) {
  869. if (IS_CHAN_2GHZ(chan))
  870. pRawDataset = pEepData->calPierData2G[i];
  871. else
  872. pRawDataset = pEepData->calPierData5G[i];
  873. ath9k_hw_get_def_gain_boundaries_pdadcs(ah, chan,
  874. pRawDataset, pCalBChans,
  875. numPiers, pdGainOverlap_t2,
  876. &tMinCalPower, gainBoundaries,
  877. pdadcValues, numXpdGain);
  878. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  879. REG_WRITE(ah,
  880. AR_PHY_TPCRG5 + regChainOffset,
  881. SM(pdGainOverlap_t2,
  882. AR_PHY_TPCRG5_PD_GAIN_OVERLAP)
  883. | SM(gainBoundaries[0],
  884. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
  885. | SM(gainBoundaries[1],
  886. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
  887. | SM(gainBoundaries[2],
  888. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
  889. | SM(gainBoundaries[3],
  890. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
  891. }
  892. regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
  893. for (j = 0; j < 32; j++) {
  894. reg32 = ((pdadcValues[4 * j + 0] & 0xFF) << 0) |
  895. ((pdadcValues[4 * j + 1] & 0xFF) << 8) |
  896. ((pdadcValues[4 * j + 2] & 0xFF) << 16)|
  897. ((pdadcValues[4 * j + 3] & 0xFF) << 24);
  898. REG_WRITE(ah, regOffset, reg32);
  899. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  900. "PDADC (%d,%4x): %4.4x %8.8x\n",
  901. i, regChainOffset, regOffset,
  902. reg32);
  903. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  904. "PDADC: Chain %d | PDADC %3d "
  905. "Value %3d | PDADC %3d Value %3d | "
  906. "PDADC %3d Value %3d | PDADC %3d "
  907. "Value %3d |\n",
  908. i, 4 * j, pdadcValues[4 * j],
  909. 4 * j + 1, pdadcValues[4 * j + 1],
  910. 4 * j + 2, pdadcValues[4 * j + 2],
  911. 4 * j + 3,
  912. pdadcValues[4 * j + 3]);
  913. regOffset += 4;
  914. }
  915. }
  916. }
  917. *pTxPowerIndexOffset = 0;
  918. return true;
  919. }
  920. static bool ath9k_hw_set_4k_power_cal_table(struct ath_hal *ah,
  921. struct ath9k_channel *chan,
  922. int16_t *pTxPowerIndexOffset)
  923. {
  924. struct ath_hal_5416 *ahp = AH5416(ah);
  925. struct ar5416_eeprom_4k *pEepData = &ahp->ah_eeprom.map4k;
  926. struct cal_data_per_freq_4k *pRawDataset;
  927. u8 *pCalBChans = NULL;
  928. u16 pdGainOverlap_t2;
  929. static u8 pdadcValues[AR5416_NUM_PDADC_VALUES];
  930. u16 gainBoundaries[AR5416_PD_GAINS_IN_MASK];
  931. u16 numPiers, i, j;
  932. int16_t tMinCalPower;
  933. u16 numXpdGain, xpdMask;
  934. u16 xpdGainValues[AR5416_NUM_PD_GAINS] = { 0, 0, 0, 0 };
  935. u32 reg32, regOffset, regChainOffset;
  936. xpdMask = pEepData->modalHeader.xpdGain;
  937. if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  938. AR5416_EEP_MINOR_VER_2) {
  939. pdGainOverlap_t2 =
  940. pEepData->modalHeader.pdGainOverlap;
  941. } else {
  942. pdGainOverlap_t2 = (u16)(MS(REG_READ(ah, AR_PHY_TPCRG5),
  943. AR_PHY_TPCRG5_PD_GAIN_OVERLAP));
  944. }
  945. pCalBChans = pEepData->calFreqPier2G;
  946. numPiers = AR5416_NUM_2G_CAL_PIERS;
  947. numXpdGain = 0;
  948. for (i = 1; i <= AR5416_PD_GAINS_IN_MASK; i++) {
  949. if ((xpdMask >> (AR5416_PD_GAINS_IN_MASK - i)) & 1) {
  950. if (numXpdGain >= AR5416_NUM_PD_GAINS)
  951. break;
  952. xpdGainValues[numXpdGain] =
  953. (u16)(AR5416_PD_GAINS_IN_MASK - i);
  954. numXpdGain++;
  955. }
  956. }
  957. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_NUM_PD_GAIN,
  958. (numXpdGain - 1) & 0x3);
  959. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_1,
  960. xpdGainValues[0]);
  961. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_2,
  962. xpdGainValues[1]);
  963. REG_RMW_FIELD(ah, AR_PHY_TPCRG1, AR_PHY_TPCRG1_PD_GAIN_3,
  964. xpdGainValues[2]);
  965. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  966. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  967. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5) &&
  968. (i != 0)) {
  969. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  970. } else
  971. regChainOffset = i * 0x1000;
  972. if (pEepData->baseEepHeader.txMask & (1 << i)) {
  973. pRawDataset = pEepData->calPierData2G[i];
  974. ath9k_hw_get_4k_gain_boundaries_pdadcs(ah, chan,
  975. pRawDataset, pCalBChans,
  976. numPiers, pdGainOverlap_t2,
  977. &tMinCalPower, gainBoundaries,
  978. pdadcValues, numXpdGain);
  979. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  980. REG_WRITE(ah, AR_PHY_TPCRG5 + regChainOffset,
  981. SM(pdGainOverlap_t2,
  982. AR_PHY_TPCRG5_PD_GAIN_OVERLAP)
  983. | SM(gainBoundaries[0],
  984. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_1)
  985. | SM(gainBoundaries[1],
  986. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_2)
  987. | SM(gainBoundaries[2],
  988. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_3)
  989. | SM(gainBoundaries[3],
  990. AR_PHY_TPCRG5_PD_GAIN_BOUNDARY_4));
  991. }
  992. regOffset = AR_PHY_BASE + (672 << 2) + regChainOffset;
  993. for (j = 0; j < 32; j++) {
  994. reg32 = ((pdadcValues[4 * j + 0] & 0xFF) << 0) |
  995. ((pdadcValues[4 * j + 1] & 0xFF) << 8) |
  996. ((pdadcValues[4 * j + 2] & 0xFF) << 16)|
  997. ((pdadcValues[4 * j + 3] & 0xFF) << 24);
  998. REG_WRITE(ah, regOffset, reg32);
  999. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  1000. "PDADC (%d,%4x): %4.4x %8.8x\n",
  1001. i, regChainOffset, regOffset,
  1002. reg32);
  1003. DPRINTF(ah->ah_sc, ATH_DBG_REG_IO,
  1004. "PDADC: Chain %d | "
  1005. "PDADC %3d Value %3d | "
  1006. "PDADC %3d Value %3d | "
  1007. "PDADC %3d Value %3d | "
  1008. "PDADC %3d Value %3d |\n",
  1009. i, 4 * j, pdadcValues[4 * j],
  1010. 4 * j + 1, pdadcValues[4 * j + 1],
  1011. 4 * j + 2, pdadcValues[4 * j + 2],
  1012. 4 * j + 3,
  1013. pdadcValues[4 * j + 3]);
  1014. regOffset += 4;
  1015. }
  1016. }
  1017. }
  1018. *pTxPowerIndexOffset = 0;
  1019. return true;
  1020. }
  1021. static bool ath9k_hw_set_def_power_per_rate_table(struct ath_hal *ah,
  1022. struct ath9k_channel *chan,
  1023. int16_t *ratesArray,
  1024. u16 cfgCtl,
  1025. u16 AntennaReduction,
  1026. u16 twiceMaxRegulatoryPower,
  1027. u16 powerLimit)
  1028. {
  1029. #define REDUCE_SCALED_POWER_BY_TWO_CHAIN 6 /* 10*log10(2)*2 */
  1030. #define REDUCE_SCALED_POWER_BY_THREE_CHAIN 10 /* 10*log10(3)*2 */
  1031. struct ath_hal_5416 *ahp = AH5416(ah);
  1032. struct ar5416_eeprom_def *pEepData = &ahp->ah_eeprom.def;
  1033. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  1034. static const u16 tpScaleReductionTable[5] =
  1035. { 0, 3, 6, 9, AR5416_MAX_RATE_POWER };
  1036. int i;
  1037. int16_t twiceLargestAntenna;
  1038. struct cal_ctl_data *rep;
  1039. struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
  1040. 0, { 0, 0, 0, 0}
  1041. };
  1042. struct cal_target_power_leg targetPowerOfdmExt = {
  1043. 0, { 0, 0, 0, 0} }, targetPowerCckExt = {
  1044. 0, { 0, 0, 0, 0 }
  1045. };
  1046. struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
  1047. 0, {0, 0, 0, 0}
  1048. };
  1049. u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
  1050. u16 ctlModesFor11a[] =
  1051. { CTL_11A, CTL_5GHT20, CTL_11A_EXT, CTL_5GHT40 };
  1052. u16 ctlModesFor11g[] =
  1053. { CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
  1054. CTL_2GHT40
  1055. };
  1056. u16 numCtlModes, *pCtlMode, ctlMode, freq;
  1057. struct chan_centers centers;
  1058. int tx_chainmask;
  1059. u16 twiceMinEdgePower;
  1060. tx_chainmask = ahp->ah_txchainmask;
  1061. ath9k_hw_get_channel_centers(ah, chan, &centers);
  1062. twiceLargestAntenna = max(
  1063. pEepData->modalHeader
  1064. [IS_CHAN_2GHZ(chan)].antennaGainCh[0],
  1065. pEepData->modalHeader
  1066. [IS_CHAN_2GHZ(chan)].antennaGainCh[1]);
  1067. twiceLargestAntenna = max((u8)twiceLargestAntenna,
  1068. pEepData->modalHeader
  1069. [IS_CHAN_2GHZ(chan)].antennaGainCh[2]);
  1070. twiceLargestAntenna = (int16_t)min(AntennaReduction -
  1071. twiceLargestAntenna, 0);
  1072. maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
  1073. if (ah->ah_tpScale != ATH9K_TP_SCALE_MAX) {
  1074. maxRegAllowedPower -=
  1075. (tpScaleReductionTable[(ah->ah_tpScale)] * 2);
  1076. }
  1077. scaledPower = min(powerLimit, maxRegAllowedPower);
  1078. switch (ar5416_get_ntxchains(tx_chainmask)) {
  1079. case 1:
  1080. break;
  1081. case 2:
  1082. scaledPower -= REDUCE_SCALED_POWER_BY_TWO_CHAIN;
  1083. break;
  1084. case 3:
  1085. scaledPower -= REDUCE_SCALED_POWER_BY_THREE_CHAIN;
  1086. break;
  1087. }
  1088. scaledPower = max((u16)0, scaledPower);
  1089. if (IS_CHAN_2GHZ(chan)) {
  1090. numCtlModes = ARRAY_SIZE(ctlModesFor11g) -
  1091. SUB_NUM_CTL_MODES_AT_2G_40;
  1092. pCtlMode = ctlModesFor11g;
  1093. ath9k_hw_get_legacy_target_powers(ah, chan,
  1094. pEepData->calTargetPowerCck,
  1095. AR5416_NUM_2G_CCK_TARGET_POWERS,
  1096. &targetPowerCck, 4, false);
  1097. ath9k_hw_get_legacy_target_powers(ah, chan,
  1098. pEepData->calTargetPower2G,
  1099. AR5416_NUM_2G_20_TARGET_POWERS,
  1100. &targetPowerOfdm, 4, false);
  1101. ath9k_hw_get_target_powers(ah, chan,
  1102. pEepData->calTargetPower2GHT20,
  1103. AR5416_NUM_2G_20_TARGET_POWERS,
  1104. &targetPowerHt20, 8, false);
  1105. if (IS_CHAN_HT40(chan)) {
  1106. numCtlModes = ARRAY_SIZE(ctlModesFor11g);
  1107. ath9k_hw_get_target_powers(ah, chan,
  1108. pEepData->calTargetPower2GHT40,
  1109. AR5416_NUM_2G_40_TARGET_POWERS,
  1110. &targetPowerHt40, 8, true);
  1111. ath9k_hw_get_legacy_target_powers(ah, chan,
  1112. pEepData->calTargetPowerCck,
  1113. AR5416_NUM_2G_CCK_TARGET_POWERS,
  1114. &targetPowerCckExt, 4, true);
  1115. ath9k_hw_get_legacy_target_powers(ah, chan,
  1116. pEepData->calTargetPower2G,
  1117. AR5416_NUM_2G_20_TARGET_POWERS,
  1118. &targetPowerOfdmExt, 4, true);
  1119. }
  1120. } else {
  1121. numCtlModes = ARRAY_SIZE(ctlModesFor11a) -
  1122. SUB_NUM_CTL_MODES_AT_5G_40;
  1123. pCtlMode = ctlModesFor11a;
  1124. ath9k_hw_get_legacy_target_powers(ah, chan,
  1125. pEepData->calTargetPower5G,
  1126. AR5416_NUM_5G_20_TARGET_POWERS,
  1127. &targetPowerOfdm, 4, false);
  1128. ath9k_hw_get_target_powers(ah, chan,
  1129. pEepData->calTargetPower5GHT20,
  1130. AR5416_NUM_5G_20_TARGET_POWERS,
  1131. &targetPowerHt20, 8, false);
  1132. if (IS_CHAN_HT40(chan)) {
  1133. numCtlModes = ARRAY_SIZE(ctlModesFor11a);
  1134. ath9k_hw_get_target_powers(ah, chan,
  1135. pEepData->calTargetPower5GHT40,
  1136. AR5416_NUM_5G_40_TARGET_POWERS,
  1137. &targetPowerHt40, 8, true);
  1138. ath9k_hw_get_legacy_target_powers(ah, chan,
  1139. pEepData->calTargetPower5G,
  1140. AR5416_NUM_5G_20_TARGET_POWERS,
  1141. &targetPowerOfdmExt, 4, true);
  1142. }
  1143. }
  1144. for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
  1145. bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
  1146. (pCtlMode[ctlMode] == CTL_2GHT40);
  1147. if (isHt40CtlMode)
  1148. freq = centers.synth_center;
  1149. else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
  1150. freq = centers.ext_center;
  1151. else
  1152. freq = centers.ctl_center;
  1153. if (ar5416_get_eep_ver(ahp) == 14 && ar5416_get_eep_rev(ahp) <= 2)
  1154. twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  1155. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1156. "LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
  1157. "EXT_ADDITIVE %d\n",
  1158. ctlMode, numCtlModes, isHt40CtlMode,
  1159. (pCtlMode[ctlMode] & EXT_ADDITIVE));
  1160. for (i = 0; (i < AR5416_NUM_CTLS) && pEepData->ctlIndex[i]; i++) {
  1161. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1162. " LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
  1163. "pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
  1164. "chan %d\n",
  1165. i, cfgCtl, pCtlMode[ctlMode],
  1166. pEepData->ctlIndex[i], chan->channel);
  1167. if ((((cfgCtl & ~CTL_MODE_M) |
  1168. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  1169. pEepData->ctlIndex[i]) ||
  1170. (((cfgCtl & ~CTL_MODE_M) |
  1171. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  1172. ((pEepData->ctlIndex[i] & CTL_MODE_M) | SD_NO_CTL))) {
  1173. rep = &(pEepData->ctlData[i]);
  1174. twiceMinEdgePower = ath9k_hw_get_max_edge_power(freq,
  1175. rep->ctlEdges[ar5416_get_ntxchains(tx_chainmask) - 1],
  1176. IS_CHAN_2GHZ(chan), AR5416_NUM_BAND_EDGES);
  1177. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1178. " MATCH-EE_IDX %d: ch %d is2 %d "
  1179. "2xMinEdge %d chainmask %d chains %d\n",
  1180. i, freq, IS_CHAN_2GHZ(chan),
  1181. twiceMinEdgePower, tx_chainmask,
  1182. ar5416_get_ntxchains
  1183. (tx_chainmask));
  1184. if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
  1185. twiceMaxEdgePower = min(twiceMaxEdgePower,
  1186. twiceMinEdgePower);
  1187. } else {
  1188. twiceMaxEdgePower = twiceMinEdgePower;
  1189. break;
  1190. }
  1191. }
  1192. }
  1193. minCtlPower = min(twiceMaxEdgePower, scaledPower);
  1194. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1195. " SEL-Min ctlMode %d pCtlMode %d "
  1196. "2xMaxEdge %d sP %d minCtlPwr %d\n",
  1197. ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
  1198. scaledPower, minCtlPower);
  1199. switch (pCtlMode[ctlMode]) {
  1200. case CTL_11B:
  1201. for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x); i++) {
  1202. targetPowerCck.tPow2x[i] =
  1203. min((u16)targetPowerCck.tPow2x[i],
  1204. minCtlPower);
  1205. }
  1206. break;
  1207. case CTL_11A:
  1208. case CTL_11G:
  1209. for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x); i++) {
  1210. targetPowerOfdm.tPow2x[i] =
  1211. min((u16)targetPowerOfdm.tPow2x[i],
  1212. minCtlPower);
  1213. }
  1214. break;
  1215. case CTL_5GHT20:
  1216. case CTL_2GHT20:
  1217. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++) {
  1218. targetPowerHt20.tPow2x[i] =
  1219. min((u16)targetPowerHt20.tPow2x[i],
  1220. minCtlPower);
  1221. }
  1222. break;
  1223. case CTL_11B_EXT:
  1224. targetPowerCckExt.tPow2x[0] = min((u16)
  1225. targetPowerCckExt.tPow2x[0],
  1226. minCtlPower);
  1227. break;
  1228. case CTL_11A_EXT:
  1229. case CTL_11G_EXT:
  1230. targetPowerOfdmExt.tPow2x[0] = min((u16)
  1231. targetPowerOfdmExt.tPow2x[0],
  1232. minCtlPower);
  1233. break;
  1234. case CTL_5GHT40:
  1235. case CTL_2GHT40:
  1236. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
  1237. targetPowerHt40.tPow2x[i] =
  1238. min((u16)targetPowerHt40.tPow2x[i],
  1239. minCtlPower);
  1240. }
  1241. break;
  1242. default:
  1243. break;
  1244. }
  1245. }
  1246. ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
  1247. ratesArray[rate18mb] = ratesArray[rate24mb] =
  1248. targetPowerOfdm.tPow2x[0];
  1249. ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
  1250. ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
  1251. ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
  1252. ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
  1253. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
  1254. ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
  1255. if (IS_CHAN_2GHZ(chan)) {
  1256. ratesArray[rate1l] = targetPowerCck.tPow2x[0];
  1257. ratesArray[rate2s] = ratesArray[rate2l] =
  1258. targetPowerCck.tPow2x[1];
  1259. ratesArray[rate5_5s] = ratesArray[rate5_5l] =
  1260. targetPowerCck.tPow2x[2];
  1261. ;
  1262. ratesArray[rate11s] = ratesArray[rate11l] =
  1263. targetPowerCck.tPow2x[3];
  1264. ;
  1265. }
  1266. if (IS_CHAN_HT40(chan)) {
  1267. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
  1268. ratesArray[rateHt40_0 + i] =
  1269. targetPowerHt40.tPow2x[i];
  1270. }
  1271. ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
  1272. ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
  1273. ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
  1274. if (IS_CHAN_2GHZ(chan)) {
  1275. ratesArray[rateExtCck] =
  1276. targetPowerCckExt.tPow2x[0];
  1277. }
  1278. }
  1279. return true;
  1280. }
  1281. static bool ath9k_hw_set_4k_power_per_rate_table(struct ath_hal *ah,
  1282. struct ath9k_channel *chan,
  1283. int16_t *ratesArray,
  1284. u16 cfgCtl,
  1285. u16 AntennaReduction,
  1286. u16 twiceMaxRegulatoryPower,
  1287. u16 powerLimit)
  1288. {
  1289. struct ath_hal_5416 *ahp = AH5416(ah);
  1290. struct ar5416_eeprom_4k *pEepData = &ahp->ah_eeprom.map4k;
  1291. u16 twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  1292. static const u16 tpScaleReductionTable[5] =
  1293. { 0, 3, 6, 9, AR5416_MAX_RATE_POWER };
  1294. int i;
  1295. int16_t twiceLargestAntenna;
  1296. struct cal_ctl_data_4k *rep;
  1297. struct cal_target_power_leg targetPowerOfdm, targetPowerCck = {
  1298. 0, { 0, 0, 0, 0}
  1299. };
  1300. struct cal_target_power_leg targetPowerOfdmExt = {
  1301. 0, { 0, 0, 0, 0} }, targetPowerCckExt = {
  1302. 0, { 0, 0, 0, 0 }
  1303. };
  1304. struct cal_target_power_ht targetPowerHt20, targetPowerHt40 = {
  1305. 0, {0, 0, 0, 0}
  1306. };
  1307. u16 scaledPower = 0, minCtlPower, maxRegAllowedPower;
  1308. u16 ctlModesFor11g[] =
  1309. { CTL_11B, CTL_11G, CTL_2GHT20, CTL_11B_EXT, CTL_11G_EXT,
  1310. CTL_2GHT40
  1311. };
  1312. u16 numCtlModes, *pCtlMode, ctlMode, freq;
  1313. struct chan_centers centers;
  1314. int tx_chainmask;
  1315. u16 twiceMinEdgePower;
  1316. tx_chainmask = ahp->ah_txchainmask;
  1317. ath9k_hw_get_channel_centers(ah, chan, &centers);
  1318. twiceLargestAntenna = pEepData->modalHeader.antennaGainCh[0];
  1319. twiceLargestAntenna = (int16_t)min(AntennaReduction -
  1320. twiceLargestAntenna, 0);
  1321. maxRegAllowedPower = twiceMaxRegulatoryPower + twiceLargestAntenna;
  1322. if (ah->ah_tpScale != ATH9K_TP_SCALE_MAX) {
  1323. maxRegAllowedPower -=
  1324. (tpScaleReductionTable[(ah->ah_tpScale)] * 2);
  1325. }
  1326. scaledPower = min(powerLimit, maxRegAllowedPower);
  1327. scaledPower = max((u16)0, scaledPower);
  1328. numCtlModes = ARRAY_SIZE(ctlModesFor11g) - SUB_NUM_CTL_MODES_AT_2G_40;
  1329. pCtlMode = ctlModesFor11g;
  1330. ath9k_hw_get_legacy_target_powers(ah, chan,
  1331. pEepData->calTargetPowerCck,
  1332. AR5416_NUM_2G_CCK_TARGET_POWERS,
  1333. &targetPowerCck, 4, false);
  1334. ath9k_hw_get_legacy_target_powers(ah, chan,
  1335. pEepData->calTargetPower2G,
  1336. AR5416_NUM_2G_20_TARGET_POWERS,
  1337. &targetPowerOfdm, 4, false);
  1338. ath9k_hw_get_target_powers(ah, chan,
  1339. pEepData->calTargetPower2GHT20,
  1340. AR5416_NUM_2G_20_TARGET_POWERS,
  1341. &targetPowerHt20, 8, false);
  1342. if (IS_CHAN_HT40(chan)) {
  1343. numCtlModes = ARRAY_SIZE(ctlModesFor11g);
  1344. ath9k_hw_get_target_powers(ah, chan,
  1345. pEepData->calTargetPower2GHT40,
  1346. AR5416_NUM_2G_40_TARGET_POWERS,
  1347. &targetPowerHt40, 8, true);
  1348. ath9k_hw_get_legacy_target_powers(ah, chan,
  1349. pEepData->calTargetPowerCck,
  1350. AR5416_NUM_2G_CCK_TARGET_POWERS,
  1351. &targetPowerCckExt, 4, true);
  1352. ath9k_hw_get_legacy_target_powers(ah, chan,
  1353. pEepData->calTargetPower2G,
  1354. AR5416_NUM_2G_20_TARGET_POWERS,
  1355. &targetPowerOfdmExt, 4, true);
  1356. }
  1357. for (ctlMode = 0; ctlMode < numCtlModes; ctlMode++) {
  1358. bool isHt40CtlMode = (pCtlMode[ctlMode] == CTL_5GHT40) ||
  1359. (pCtlMode[ctlMode] == CTL_2GHT40);
  1360. if (isHt40CtlMode)
  1361. freq = centers.synth_center;
  1362. else if (pCtlMode[ctlMode] & EXT_ADDITIVE)
  1363. freq = centers.ext_center;
  1364. else
  1365. freq = centers.ctl_center;
  1366. if (ar5416_get_eep_ver(ahp) == 14 &&
  1367. ar5416_get_eep_rev(ahp) <= 2)
  1368. twiceMaxEdgePower = AR5416_MAX_RATE_POWER;
  1369. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1370. "LOOP-Mode ctlMode %d < %d, isHt40CtlMode %d, "
  1371. "EXT_ADDITIVE %d\n",
  1372. ctlMode, numCtlModes, isHt40CtlMode,
  1373. (pCtlMode[ctlMode] & EXT_ADDITIVE));
  1374. for (i = 0; (i < AR5416_NUM_CTLS) &&
  1375. pEepData->ctlIndex[i]; i++) {
  1376. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1377. " LOOP-Ctlidx %d: cfgCtl 0x%2.2x "
  1378. "pCtlMode 0x%2.2x ctlIndex 0x%2.2x "
  1379. "chan %d\n",
  1380. i, cfgCtl, pCtlMode[ctlMode],
  1381. pEepData->ctlIndex[i], chan->channel);
  1382. if ((((cfgCtl & ~CTL_MODE_M) |
  1383. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  1384. pEepData->ctlIndex[i]) ||
  1385. (((cfgCtl & ~CTL_MODE_M) |
  1386. (pCtlMode[ctlMode] & CTL_MODE_M)) ==
  1387. ((pEepData->ctlIndex[i] & CTL_MODE_M) |
  1388. SD_NO_CTL))) {
  1389. rep = &(pEepData->ctlData[i]);
  1390. twiceMinEdgePower =
  1391. ath9k_hw_get_max_edge_power(freq,
  1392. rep->ctlEdges[ar5416_get_ntxchains
  1393. (tx_chainmask) - 1],
  1394. IS_CHAN_2GHZ(chan),
  1395. AR5416_EEP4K_NUM_BAND_EDGES);
  1396. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1397. " MATCH-EE_IDX %d: ch %d is2 %d "
  1398. "2xMinEdge %d chainmask %d chains %d\n",
  1399. i, freq, IS_CHAN_2GHZ(chan),
  1400. twiceMinEdgePower, tx_chainmask,
  1401. ar5416_get_ntxchains
  1402. (tx_chainmask));
  1403. if ((cfgCtl & ~CTL_MODE_M) == SD_NO_CTL) {
  1404. twiceMaxEdgePower =
  1405. min(twiceMaxEdgePower,
  1406. twiceMinEdgePower);
  1407. } else {
  1408. twiceMaxEdgePower = twiceMinEdgePower;
  1409. break;
  1410. }
  1411. }
  1412. }
  1413. minCtlPower = (u8)min(twiceMaxEdgePower, scaledPower);
  1414. DPRINTF(ah->ah_sc, ATH_DBG_POWER_MGMT,
  1415. " SEL-Min ctlMode %d pCtlMode %d "
  1416. "2xMaxEdge %d sP %d minCtlPwr %d\n",
  1417. ctlMode, pCtlMode[ctlMode], twiceMaxEdgePower,
  1418. scaledPower, minCtlPower);
  1419. switch (pCtlMode[ctlMode]) {
  1420. case CTL_11B:
  1421. for (i = 0; i < ARRAY_SIZE(targetPowerCck.tPow2x);
  1422. i++) {
  1423. targetPowerCck.tPow2x[i] =
  1424. min((u16)targetPowerCck.tPow2x[i],
  1425. minCtlPower);
  1426. }
  1427. break;
  1428. case CTL_11G:
  1429. for (i = 0; i < ARRAY_SIZE(targetPowerOfdm.tPow2x);
  1430. i++) {
  1431. targetPowerOfdm.tPow2x[i] =
  1432. min((u16)targetPowerOfdm.tPow2x[i],
  1433. minCtlPower);
  1434. }
  1435. break;
  1436. case CTL_2GHT20:
  1437. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x);
  1438. i++) {
  1439. targetPowerHt20.tPow2x[i] =
  1440. min((u16)targetPowerHt20.tPow2x[i],
  1441. minCtlPower);
  1442. }
  1443. break;
  1444. case CTL_11B_EXT:
  1445. targetPowerCckExt.tPow2x[0] = min((u16)
  1446. targetPowerCckExt.tPow2x[0],
  1447. minCtlPower);
  1448. break;
  1449. case CTL_11G_EXT:
  1450. targetPowerOfdmExt.tPow2x[0] = min((u16)
  1451. targetPowerOfdmExt.tPow2x[0],
  1452. minCtlPower);
  1453. break;
  1454. case CTL_2GHT40:
  1455. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x);
  1456. i++) {
  1457. targetPowerHt40.tPow2x[i] =
  1458. min((u16)targetPowerHt40.tPow2x[i],
  1459. minCtlPower);
  1460. }
  1461. break;
  1462. default:
  1463. break;
  1464. }
  1465. }
  1466. ratesArray[rate6mb] = ratesArray[rate9mb] = ratesArray[rate12mb] =
  1467. ratesArray[rate18mb] = ratesArray[rate24mb] =
  1468. targetPowerOfdm.tPow2x[0];
  1469. ratesArray[rate36mb] = targetPowerOfdm.tPow2x[1];
  1470. ratesArray[rate48mb] = targetPowerOfdm.tPow2x[2];
  1471. ratesArray[rate54mb] = targetPowerOfdm.tPow2x[3];
  1472. ratesArray[rateXr] = targetPowerOfdm.tPow2x[0];
  1473. for (i = 0; i < ARRAY_SIZE(targetPowerHt20.tPow2x); i++)
  1474. ratesArray[rateHt20_0 + i] = targetPowerHt20.tPow2x[i];
  1475. ratesArray[rate1l] = targetPowerCck.tPow2x[0];
  1476. ratesArray[rate2s] = ratesArray[rate2l] = targetPowerCck.tPow2x[1];
  1477. ratesArray[rate5_5s] = ratesArray[rate5_5l] = targetPowerCck.tPow2x[2];
  1478. ratesArray[rate11s] = ratesArray[rate11l] = targetPowerCck.tPow2x[3];
  1479. if (IS_CHAN_HT40(chan)) {
  1480. for (i = 0; i < ARRAY_SIZE(targetPowerHt40.tPow2x); i++) {
  1481. ratesArray[rateHt40_0 + i] =
  1482. targetPowerHt40.tPow2x[i];
  1483. }
  1484. ratesArray[rateDupOfdm] = targetPowerHt40.tPow2x[0];
  1485. ratesArray[rateDupCck] = targetPowerHt40.tPow2x[0];
  1486. ratesArray[rateExtOfdm] = targetPowerOfdmExt.tPow2x[0];
  1487. ratesArray[rateExtCck] = targetPowerCckExt.tPow2x[0];
  1488. }
  1489. return true;
  1490. }
  1491. static int ath9k_hw_def_set_txpower(struct ath_hal *ah,
  1492. struct ath9k_channel *chan,
  1493. u16 cfgCtl,
  1494. u8 twiceAntennaReduction,
  1495. u8 twiceMaxRegulatoryPower,
  1496. u8 powerLimit)
  1497. {
  1498. struct ath_hal_5416 *ahp = AH5416(ah);
  1499. struct ar5416_eeprom_def *pEepData = &ahp->ah_eeprom.def;
  1500. struct modal_eep_header *pModal =
  1501. &(pEepData->modalHeader[IS_CHAN_2GHZ(chan)]);
  1502. int16_t ratesArray[Ar5416RateSize];
  1503. int16_t txPowerIndexOffset = 0;
  1504. u8 ht40PowerIncForPdadc = 2;
  1505. int i;
  1506. memset(ratesArray, 0, sizeof(ratesArray));
  1507. if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  1508. AR5416_EEP_MINOR_VER_2) {
  1509. ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
  1510. }
  1511. if (!ath9k_hw_set_def_power_per_rate_table(ah, chan,
  1512. &ratesArray[0], cfgCtl,
  1513. twiceAntennaReduction,
  1514. twiceMaxRegulatoryPower,
  1515. powerLimit)) {
  1516. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  1517. "ath9k_hw_set_txpower: unable to set "
  1518. "tx power per rate table\n");
  1519. return -EIO;
  1520. }
  1521. if (!ath9k_hw_set_def_power_cal_table(ah, chan, &txPowerIndexOffset)) {
  1522. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  1523. "ath9k_hw_set_txpower: unable to set power table\n");
  1524. return -EIO;
  1525. }
  1526. for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
  1527. ratesArray[i] = (int16_t)(txPowerIndexOffset + ratesArray[i]);
  1528. if (ratesArray[i] > AR5416_MAX_RATE_POWER)
  1529. ratesArray[i] = AR5416_MAX_RATE_POWER;
  1530. }
  1531. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1532. for (i = 0; i < Ar5416RateSize; i++)
  1533. ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
  1534. }
  1535. REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
  1536. ATH9K_POW_SM(ratesArray[rate18mb], 24)
  1537. | ATH9K_POW_SM(ratesArray[rate12mb], 16)
  1538. | ATH9K_POW_SM(ratesArray[rate9mb], 8)
  1539. | ATH9K_POW_SM(ratesArray[rate6mb], 0));
  1540. REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
  1541. ATH9K_POW_SM(ratesArray[rate54mb], 24)
  1542. | ATH9K_POW_SM(ratesArray[rate48mb], 16)
  1543. | ATH9K_POW_SM(ratesArray[rate36mb], 8)
  1544. | ATH9K_POW_SM(ratesArray[rate24mb], 0));
  1545. if (IS_CHAN_2GHZ(chan)) {
  1546. REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
  1547. ATH9K_POW_SM(ratesArray[rate2s], 24)
  1548. | ATH9K_POW_SM(ratesArray[rate2l], 16)
  1549. | ATH9K_POW_SM(ratesArray[rateXr], 8)
  1550. | ATH9K_POW_SM(ratesArray[rate1l], 0));
  1551. REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
  1552. ATH9K_POW_SM(ratesArray[rate11s], 24)
  1553. | ATH9K_POW_SM(ratesArray[rate11l], 16)
  1554. | ATH9K_POW_SM(ratesArray[rate5_5s], 8)
  1555. | ATH9K_POW_SM(ratesArray[rate5_5l], 0));
  1556. }
  1557. REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
  1558. ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
  1559. | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
  1560. | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
  1561. | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
  1562. REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
  1563. ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
  1564. | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
  1565. | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
  1566. | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
  1567. if (IS_CHAN_HT40(chan)) {
  1568. REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
  1569. ATH9K_POW_SM(ratesArray[rateHt40_3] +
  1570. ht40PowerIncForPdadc, 24)
  1571. | ATH9K_POW_SM(ratesArray[rateHt40_2] +
  1572. ht40PowerIncForPdadc, 16)
  1573. | ATH9K_POW_SM(ratesArray[rateHt40_1] +
  1574. ht40PowerIncForPdadc, 8)
  1575. | ATH9K_POW_SM(ratesArray[rateHt40_0] +
  1576. ht40PowerIncForPdadc, 0));
  1577. REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
  1578. ATH9K_POW_SM(ratesArray[rateHt40_7] +
  1579. ht40PowerIncForPdadc, 24)
  1580. | ATH9K_POW_SM(ratesArray[rateHt40_6] +
  1581. ht40PowerIncForPdadc, 16)
  1582. | ATH9K_POW_SM(ratesArray[rateHt40_5] +
  1583. ht40PowerIncForPdadc, 8)
  1584. | ATH9K_POW_SM(ratesArray[rateHt40_4] +
  1585. ht40PowerIncForPdadc, 0));
  1586. REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
  1587. ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
  1588. | ATH9K_POW_SM(ratesArray[rateExtCck], 16)
  1589. | ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
  1590. | ATH9K_POW_SM(ratesArray[rateDupCck], 0));
  1591. }
  1592. REG_WRITE(ah, AR_PHY_POWER_TX_SUB,
  1593. ATH9K_POW_SM(pModal->pwrDecreaseFor3Chain, 6)
  1594. | ATH9K_POW_SM(pModal->pwrDecreaseFor2Chain, 0));
  1595. i = rate6mb;
  1596. if (IS_CHAN_HT40(chan))
  1597. i = rateHt40_0;
  1598. else if (IS_CHAN_HT20(chan))
  1599. i = rateHt20_0;
  1600. if (AR_SREV_9280_10_OR_LATER(ah))
  1601. ah->ah_maxPowerLevel =
  1602. ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
  1603. else
  1604. ah->ah_maxPowerLevel = ratesArray[i];
  1605. return 0;
  1606. }
  1607. static int ath9k_hw_4k_set_txpower(struct ath_hal *ah,
  1608. struct ath9k_channel *chan,
  1609. u16 cfgCtl,
  1610. u8 twiceAntennaReduction,
  1611. u8 twiceMaxRegulatoryPower,
  1612. u8 powerLimit)
  1613. {
  1614. struct ath_hal_5416 *ahp = AH5416(ah);
  1615. struct ar5416_eeprom_4k *pEepData = &ahp->ah_eeprom.map4k;
  1616. struct modal_eep_4k_header *pModal = &pEepData->modalHeader;
  1617. int16_t ratesArray[Ar5416RateSize];
  1618. int16_t txPowerIndexOffset = 0;
  1619. u8 ht40PowerIncForPdadc = 2;
  1620. int i;
  1621. memset(ratesArray, 0, sizeof(ratesArray));
  1622. if ((pEepData->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  1623. AR5416_EEP_MINOR_VER_2) {
  1624. ht40PowerIncForPdadc = pModal->ht40PowerIncForPdadc;
  1625. }
  1626. if (!ath9k_hw_set_4k_power_per_rate_table(ah, chan,
  1627. &ratesArray[0], cfgCtl,
  1628. twiceAntennaReduction,
  1629. twiceMaxRegulatoryPower,
  1630. powerLimit)) {
  1631. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  1632. "ath9k_hw_set_txpower: unable to set "
  1633. "tx power per rate table\n");
  1634. return -EIO;
  1635. }
  1636. if (!ath9k_hw_set_4k_power_cal_table(ah, chan, &txPowerIndexOffset)) {
  1637. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  1638. "ath9k_hw_set_txpower: unable to set power table\n");
  1639. return -EIO;
  1640. }
  1641. for (i = 0; i < ARRAY_SIZE(ratesArray); i++) {
  1642. ratesArray[i] = (int16_t)(txPowerIndexOffset + ratesArray[i]);
  1643. if (ratesArray[i] > AR5416_MAX_RATE_POWER)
  1644. ratesArray[i] = AR5416_MAX_RATE_POWER;
  1645. }
  1646. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1647. for (i = 0; i < Ar5416RateSize; i++)
  1648. ratesArray[i] -= AR5416_PWR_TABLE_OFFSET * 2;
  1649. }
  1650. REG_WRITE(ah, AR_PHY_POWER_TX_RATE1,
  1651. ATH9K_POW_SM(ratesArray[rate18mb], 24)
  1652. | ATH9K_POW_SM(ratesArray[rate12mb], 16)
  1653. | ATH9K_POW_SM(ratesArray[rate9mb], 8)
  1654. | ATH9K_POW_SM(ratesArray[rate6mb], 0));
  1655. REG_WRITE(ah, AR_PHY_POWER_TX_RATE2,
  1656. ATH9K_POW_SM(ratesArray[rate54mb], 24)
  1657. | ATH9K_POW_SM(ratesArray[rate48mb], 16)
  1658. | ATH9K_POW_SM(ratesArray[rate36mb], 8)
  1659. | ATH9K_POW_SM(ratesArray[rate24mb], 0));
  1660. if (IS_CHAN_2GHZ(chan)) {
  1661. REG_WRITE(ah, AR_PHY_POWER_TX_RATE3,
  1662. ATH9K_POW_SM(ratesArray[rate2s], 24)
  1663. | ATH9K_POW_SM(ratesArray[rate2l], 16)
  1664. | ATH9K_POW_SM(ratesArray[rateXr], 8)
  1665. | ATH9K_POW_SM(ratesArray[rate1l], 0));
  1666. REG_WRITE(ah, AR_PHY_POWER_TX_RATE4,
  1667. ATH9K_POW_SM(ratesArray[rate11s], 24)
  1668. | ATH9K_POW_SM(ratesArray[rate11l], 16)
  1669. | ATH9K_POW_SM(ratesArray[rate5_5s], 8)
  1670. | ATH9K_POW_SM(ratesArray[rate5_5l], 0));
  1671. }
  1672. REG_WRITE(ah, AR_PHY_POWER_TX_RATE5,
  1673. ATH9K_POW_SM(ratesArray[rateHt20_3], 24)
  1674. | ATH9K_POW_SM(ratesArray[rateHt20_2], 16)
  1675. | ATH9K_POW_SM(ratesArray[rateHt20_1], 8)
  1676. | ATH9K_POW_SM(ratesArray[rateHt20_0], 0));
  1677. REG_WRITE(ah, AR_PHY_POWER_TX_RATE6,
  1678. ATH9K_POW_SM(ratesArray[rateHt20_7], 24)
  1679. | ATH9K_POW_SM(ratesArray[rateHt20_6], 16)
  1680. | ATH9K_POW_SM(ratesArray[rateHt20_5], 8)
  1681. | ATH9K_POW_SM(ratesArray[rateHt20_4], 0));
  1682. if (IS_CHAN_HT40(chan)) {
  1683. REG_WRITE(ah, AR_PHY_POWER_TX_RATE7,
  1684. ATH9K_POW_SM(ratesArray[rateHt40_3] +
  1685. ht40PowerIncForPdadc, 24)
  1686. | ATH9K_POW_SM(ratesArray[rateHt40_2] +
  1687. ht40PowerIncForPdadc, 16)
  1688. | ATH9K_POW_SM(ratesArray[rateHt40_1] +
  1689. ht40PowerIncForPdadc, 8)
  1690. | ATH9K_POW_SM(ratesArray[rateHt40_0] +
  1691. ht40PowerIncForPdadc, 0));
  1692. REG_WRITE(ah, AR_PHY_POWER_TX_RATE8,
  1693. ATH9K_POW_SM(ratesArray[rateHt40_7] +
  1694. ht40PowerIncForPdadc, 24)
  1695. | ATH9K_POW_SM(ratesArray[rateHt40_6] +
  1696. ht40PowerIncForPdadc, 16)
  1697. | ATH9K_POW_SM(ratesArray[rateHt40_5] +
  1698. ht40PowerIncForPdadc, 8)
  1699. | ATH9K_POW_SM(ratesArray[rateHt40_4] +
  1700. ht40PowerIncForPdadc, 0));
  1701. REG_WRITE(ah, AR_PHY_POWER_TX_RATE9,
  1702. ATH9K_POW_SM(ratesArray[rateExtOfdm], 24)
  1703. | ATH9K_POW_SM(ratesArray[rateExtCck], 16)
  1704. | ATH9K_POW_SM(ratesArray[rateDupOfdm], 8)
  1705. | ATH9K_POW_SM(ratesArray[rateDupCck], 0));
  1706. }
  1707. i = rate6mb;
  1708. if (IS_CHAN_HT40(chan))
  1709. i = rateHt40_0;
  1710. else if (IS_CHAN_HT20(chan))
  1711. i = rateHt20_0;
  1712. if (AR_SREV_9280_10_OR_LATER(ah))
  1713. ah->ah_maxPowerLevel =
  1714. ratesArray[i] + AR5416_PWR_TABLE_OFFSET * 2;
  1715. else
  1716. ah->ah_maxPowerLevel = ratesArray[i];
  1717. return 0;
  1718. }
  1719. static int (*ath9k_set_txpower[]) (struct ath_hal *,
  1720. struct ath9k_channel *,
  1721. u16, u8, u8, u8) = {
  1722. ath9k_hw_def_set_txpower,
  1723. ath9k_hw_4k_set_txpower
  1724. };
  1725. int ath9k_hw_set_txpower(struct ath_hal *ah,
  1726. struct ath9k_channel *chan,
  1727. u16 cfgCtl,
  1728. u8 twiceAntennaReduction,
  1729. u8 twiceMaxRegulatoryPower,
  1730. u8 powerLimit)
  1731. {
  1732. struct ath_hal_5416 *ahp = AH5416(ah);
  1733. return ath9k_set_txpower[ahp->ah_eep_map](ah, chan, cfgCtl,
  1734. twiceAntennaReduction, twiceMaxRegulatoryPower,
  1735. powerLimit);
  1736. }
  1737. static void ath9k_hw_set_def_addac(struct ath_hal *ah,
  1738. struct ath9k_channel *chan)
  1739. {
  1740. #define XPA_LVL_FREQ(cnt) (pModal->xpaBiasLvlFreq[cnt])
  1741. struct modal_eep_header *pModal;
  1742. struct ath_hal_5416 *ahp = AH5416(ah);
  1743. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  1744. u8 biaslevel;
  1745. if (ah->ah_macVersion != AR_SREV_VERSION_9160)
  1746. return;
  1747. if (ar5416_get_eep_rev(ahp) < AR5416_EEP_MINOR_VER_7)
  1748. return;
  1749. pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  1750. if (pModal->xpaBiasLvl != 0xff) {
  1751. biaslevel = pModal->xpaBiasLvl;
  1752. } else {
  1753. u16 resetFreqBin, freqBin, freqCount = 0;
  1754. struct chan_centers centers;
  1755. ath9k_hw_get_channel_centers(ah, chan, &centers);
  1756. resetFreqBin = FREQ2FBIN(centers.synth_center,
  1757. IS_CHAN_2GHZ(chan));
  1758. freqBin = XPA_LVL_FREQ(0) & 0xff;
  1759. biaslevel = (u8) (XPA_LVL_FREQ(0) >> 14);
  1760. freqCount++;
  1761. while (freqCount < 3) {
  1762. if (XPA_LVL_FREQ(freqCount) == 0x0)
  1763. break;
  1764. freqBin = XPA_LVL_FREQ(freqCount) & 0xff;
  1765. if (resetFreqBin >= freqBin)
  1766. biaslevel = (u8)(XPA_LVL_FREQ(freqCount) >> 14);
  1767. else
  1768. break;
  1769. freqCount++;
  1770. }
  1771. }
  1772. if (IS_CHAN_2GHZ(chan)) {
  1773. INI_RA(&ahp->ah_iniAddac, 7, 1) = (INI_RA(&ahp->ah_iniAddac,
  1774. 7, 1) & (~0x18)) | biaslevel << 3;
  1775. } else {
  1776. INI_RA(&ahp->ah_iniAddac, 6, 1) = (INI_RA(&ahp->ah_iniAddac,
  1777. 6, 1) & (~0xc0)) | biaslevel << 6;
  1778. }
  1779. #undef XPA_LVL_FREQ
  1780. }
  1781. static void ath9k_hw_set_4k_addac(struct ath_hal *ah,
  1782. struct ath9k_channel *chan)
  1783. {
  1784. struct modal_eep_4k_header *pModal;
  1785. struct ath_hal_5416 *ahp = AH5416(ah);
  1786. struct ar5416_eeprom_4k *eep = &ahp->ah_eeprom.map4k;
  1787. u8 biaslevel;
  1788. if (ah->ah_macVersion != AR_SREV_VERSION_9160)
  1789. return;
  1790. if (ar5416_get_eep_rev(ahp) < AR5416_EEP_MINOR_VER_7)
  1791. return;
  1792. pModal = &eep->modalHeader;
  1793. if (pModal->xpaBiasLvl != 0xff) {
  1794. biaslevel = pModal->xpaBiasLvl;
  1795. INI_RA(&ahp->ah_iniAddac, 7, 1) =
  1796. (INI_RA(&ahp->ah_iniAddac, 7, 1) & (~0x18)) | biaslevel << 3;
  1797. }
  1798. }
  1799. static void (*ath9k_set_addac[]) (struct ath_hal *, struct ath9k_channel *) = {
  1800. ath9k_hw_set_def_addac,
  1801. ath9k_hw_set_4k_addac
  1802. };
  1803. void ath9k_hw_set_addac(struct ath_hal *ah, struct ath9k_channel *chan)
  1804. {
  1805. struct ath_hal_5416 *ahp = AH5416(ah);
  1806. ath9k_set_addac[ahp->ah_eep_map](ah, chan);
  1807. }
  1808. /* XXX: Clean me up, make me more legible */
  1809. static bool ath9k_hw_eeprom_set_def_board_values(struct ath_hal *ah,
  1810. struct ath9k_channel *chan)
  1811. {
  1812. struct modal_eep_header *pModal;
  1813. struct ath_hal_5416 *ahp = AH5416(ah);
  1814. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  1815. int i, regChainOffset;
  1816. u8 txRxAttenLocal;
  1817. u16 ant_config;
  1818. pModal = &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  1819. txRxAttenLocal = IS_CHAN_2GHZ(chan) ? 23 : 44;
  1820. ath9k_hw_get_eeprom_antenna_cfg(ah, chan, 0, &ant_config);
  1821. REG_WRITE(ah, AR_PHY_SWITCH_COM, ant_config);
  1822. for (i = 0; i < AR5416_MAX_CHAINS; i++) {
  1823. if (AR_SREV_9280(ah)) {
  1824. if (i >= 2)
  1825. break;
  1826. }
  1827. if (AR_SREV_5416_V20_OR_LATER(ah) &&
  1828. (ahp->ah_rxchainmask == 5 || ahp->ah_txchainmask == 5)
  1829. && (i != 0))
  1830. regChainOffset = (i == 1) ? 0x2000 : 0x1000;
  1831. else
  1832. regChainOffset = i * 0x1000;
  1833. REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
  1834. pModal->antCtrlChain[i]);
  1835. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
  1836. (REG_READ(ah,
  1837. AR_PHY_TIMING_CTRL4(0) +
  1838. regChainOffset) &
  1839. ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
  1840. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
  1841. SM(pModal->iqCalICh[i],
  1842. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
  1843. SM(pModal->iqCalQCh[i],
  1844. AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
  1845. if ((i == 0) || AR_SREV_5416_V20_OR_LATER(ah)) {
  1846. if ((eep->baseEepHeader.version &
  1847. AR5416_EEP_VER_MINOR_MASK) >=
  1848. AR5416_EEP_MINOR_VER_3) {
  1849. txRxAttenLocal = pModal->txRxAttenCh[i];
  1850. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1851. REG_RMW_FIELD(ah,
  1852. AR_PHY_GAIN_2GHZ +
  1853. regChainOffset,
  1854. AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN,
  1855. pModal->
  1856. bswMargin[i]);
  1857. REG_RMW_FIELD(ah,
  1858. AR_PHY_GAIN_2GHZ +
  1859. regChainOffset,
  1860. AR_PHY_GAIN_2GHZ_XATTEN1_DB,
  1861. pModal->
  1862. bswAtten[i]);
  1863. REG_RMW_FIELD(ah,
  1864. AR_PHY_GAIN_2GHZ +
  1865. regChainOffset,
  1866. AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
  1867. pModal->
  1868. xatten2Margin[i]);
  1869. REG_RMW_FIELD(ah,
  1870. AR_PHY_GAIN_2GHZ +
  1871. regChainOffset,
  1872. AR_PHY_GAIN_2GHZ_XATTEN2_DB,
  1873. pModal->
  1874. xatten2Db[i]);
  1875. } else {
  1876. REG_WRITE(ah,
  1877. AR_PHY_GAIN_2GHZ +
  1878. regChainOffset,
  1879. (REG_READ(ah,
  1880. AR_PHY_GAIN_2GHZ +
  1881. regChainOffset) &
  1882. ~AR_PHY_GAIN_2GHZ_BSW_MARGIN)
  1883. | SM(pModal->
  1884. bswMargin[i],
  1885. AR_PHY_GAIN_2GHZ_BSW_MARGIN));
  1886. REG_WRITE(ah,
  1887. AR_PHY_GAIN_2GHZ +
  1888. regChainOffset,
  1889. (REG_READ(ah,
  1890. AR_PHY_GAIN_2GHZ +
  1891. regChainOffset) &
  1892. ~AR_PHY_GAIN_2GHZ_BSW_ATTEN)
  1893. | SM(pModal->bswAtten[i],
  1894. AR_PHY_GAIN_2GHZ_BSW_ATTEN));
  1895. }
  1896. }
  1897. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1898. REG_RMW_FIELD(ah,
  1899. AR_PHY_RXGAIN +
  1900. regChainOffset,
  1901. AR9280_PHY_RXGAIN_TXRX_ATTEN,
  1902. txRxAttenLocal);
  1903. REG_RMW_FIELD(ah,
  1904. AR_PHY_RXGAIN +
  1905. regChainOffset,
  1906. AR9280_PHY_RXGAIN_TXRX_MARGIN,
  1907. pModal->rxTxMarginCh[i]);
  1908. } else {
  1909. REG_WRITE(ah,
  1910. AR_PHY_RXGAIN + regChainOffset,
  1911. (REG_READ(ah,
  1912. AR_PHY_RXGAIN +
  1913. regChainOffset) &
  1914. ~AR_PHY_RXGAIN_TXRX_ATTEN) |
  1915. SM(txRxAttenLocal,
  1916. AR_PHY_RXGAIN_TXRX_ATTEN));
  1917. REG_WRITE(ah,
  1918. AR_PHY_GAIN_2GHZ +
  1919. regChainOffset,
  1920. (REG_READ(ah,
  1921. AR_PHY_GAIN_2GHZ +
  1922. regChainOffset) &
  1923. ~AR_PHY_GAIN_2GHZ_RXTX_MARGIN) |
  1924. SM(pModal->rxTxMarginCh[i],
  1925. AR_PHY_GAIN_2GHZ_RXTX_MARGIN));
  1926. }
  1927. }
  1928. }
  1929. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1930. if (IS_CHAN_2GHZ(chan)) {
  1931. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
  1932. AR_AN_RF2G1_CH0_OB,
  1933. AR_AN_RF2G1_CH0_OB_S,
  1934. pModal->ob);
  1935. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH0,
  1936. AR_AN_RF2G1_CH0_DB,
  1937. AR_AN_RF2G1_CH0_DB_S,
  1938. pModal->db);
  1939. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
  1940. AR_AN_RF2G1_CH1_OB,
  1941. AR_AN_RF2G1_CH1_OB_S,
  1942. pModal->ob_ch1);
  1943. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF2G1_CH1,
  1944. AR_AN_RF2G1_CH1_DB,
  1945. AR_AN_RF2G1_CH1_DB_S,
  1946. pModal->db_ch1);
  1947. } else {
  1948. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
  1949. AR_AN_RF5G1_CH0_OB5,
  1950. AR_AN_RF5G1_CH0_OB5_S,
  1951. pModal->ob);
  1952. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH0,
  1953. AR_AN_RF5G1_CH0_DB5,
  1954. AR_AN_RF5G1_CH0_DB5_S,
  1955. pModal->db);
  1956. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
  1957. AR_AN_RF5G1_CH1_OB5,
  1958. AR_AN_RF5G1_CH1_OB5_S,
  1959. pModal->ob_ch1);
  1960. ath9k_hw_analog_shift_rmw(ah, AR_AN_RF5G1_CH1,
  1961. AR_AN_RF5G1_CH1_DB5,
  1962. AR_AN_RF5G1_CH1_DB5_S,
  1963. pModal->db_ch1);
  1964. }
  1965. ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
  1966. AR_AN_TOP2_XPABIAS_LVL,
  1967. AR_AN_TOP2_XPABIAS_LVL_S,
  1968. pModal->xpaBiasLvl);
  1969. ath9k_hw_analog_shift_rmw(ah, AR_AN_TOP2,
  1970. AR_AN_TOP2_LOCALBIAS,
  1971. AR_AN_TOP2_LOCALBIAS_S,
  1972. pModal->local_bias);
  1973. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM, "ForceXPAon: %d\n",
  1974. pModal->force_xpaon);
  1975. REG_RMW_FIELD(ah, AR_PHY_XPA_CFG, AR_PHY_FORCE_XPA_CFG,
  1976. pModal->force_xpaon);
  1977. }
  1978. REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
  1979. pModal->switchSettling);
  1980. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
  1981. pModal->adcDesiredSize);
  1982. if (!AR_SREV_9280_10_OR_LATER(ah))
  1983. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
  1984. AR_PHY_DESIRED_SZ_PGA,
  1985. pModal->pgaDesiredSize);
  1986. REG_WRITE(ah, AR_PHY_RF_CTL4,
  1987. SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF)
  1988. | SM(pModal->txEndToXpaOff,
  1989. AR_PHY_RF_CTL4_TX_END_XPAB_OFF)
  1990. | SM(pModal->txFrameToXpaOn,
  1991. AR_PHY_RF_CTL4_FRAME_XPAA_ON)
  1992. | SM(pModal->txFrameToXpaOn,
  1993. AR_PHY_RF_CTL4_FRAME_XPAB_ON));
  1994. REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
  1995. pModal->txEndToRxOn);
  1996. if (AR_SREV_9280_10_OR_LATER(ah)) {
  1997. REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
  1998. pModal->thresh62);
  1999. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0,
  2000. AR_PHY_EXT_CCA0_THRESH62,
  2001. pModal->thresh62);
  2002. } else {
  2003. REG_RMW_FIELD(ah, AR_PHY_CCA, AR_PHY_CCA_THRESH62,
  2004. pModal->thresh62);
  2005. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
  2006. AR_PHY_EXT_CCA_THRESH62,
  2007. pModal->thresh62);
  2008. }
  2009. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  2010. AR5416_EEP_MINOR_VER_2) {
  2011. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2,
  2012. AR_PHY_TX_END_DATA_START,
  2013. pModal->txFrameToDataStart);
  2014. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
  2015. pModal->txFrameToPaOn);
  2016. }
  2017. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  2018. AR5416_EEP_MINOR_VER_3) {
  2019. if (IS_CHAN_HT40(chan))
  2020. REG_RMW_FIELD(ah, AR_PHY_SETTLING,
  2021. AR_PHY_SETTLING_SWITCH,
  2022. pModal->swSettleHt40);
  2023. }
  2024. return true;
  2025. }
  2026. static bool ath9k_hw_eeprom_set_4k_board_values(struct ath_hal *ah,
  2027. struct ath9k_channel *chan)
  2028. {
  2029. struct modal_eep_4k_header *pModal;
  2030. struct ath_hal_5416 *ahp = AH5416(ah);
  2031. struct ar5416_eeprom_4k *eep = &ahp->ah_eeprom.map4k;
  2032. int regChainOffset;
  2033. u8 txRxAttenLocal;
  2034. u16 ant_config = 0;
  2035. u8 ob[5], db1[5], db2[5];
  2036. u8 ant_div_control1, ant_div_control2;
  2037. u32 regVal;
  2038. pModal = &eep->modalHeader;
  2039. txRxAttenLocal = 23;
  2040. ath9k_hw_get_eeprom_antenna_cfg(ah, chan, 0, &ant_config);
  2041. REG_WRITE(ah, AR_PHY_SWITCH_COM, ant_config);
  2042. regChainOffset = 0;
  2043. REG_WRITE(ah, AR_PHY_SWITCH_CHAIN_0 + regChainOffset,
  2044. pModal->antCtrlChain[0]);
  2045. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset,
  2046. (REG_READ(ah, AR_PHY_TIMING_CTRL4(0) + regChainOffset) &
  2047. ~(AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF |
  2048. AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF)) |
  2049. SM(pModal->iqCalICh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_I_COFF) |
  2050. SM(pModal->iqCalQCh[0], AR_PHY_TIMING_CTRL4_IQCORR_Q_Q_COFF));
  2051. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  2052. AR5416_EEP_MINOR_VER_3) {
  2053. txRxAttenLocal = pModal->txRxAttenCh[0];
  2054. REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
  2055. AR_PHY_GAIN_2GHZ_XATTEN1_MARGIN, pModal->bswMargin[0]);
  2056. REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
  2057. AR_PHY_GAIN_2GHZ_XATTEN1_DB, pModal->bswAtten[0]);
  2058. REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
  2059. AR_PHY_GAIN_2GHZ_XATTEN2_MARGIN,
  2060. pModal->xatten2Margin[0]);
  2061. REG_RMW_FIELD(ah, AR_PHY_GAIN_2GHZ + regChainOffset,
  2062. AR_PHY_GAIN_2GHZ_XATTEN2_DB, pModal->xatten2Db[0]);
  2063. }
  2064. REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
  2065. AR9280_PHY_RXGAIN_TXRX_ATTEN, txRxAttenLocal);
  2066. REG_RMW_FIELD(ah, AR_PHY_RXGAIN + regChainOffset,
  2067. AR9280_PHY_RXGAIN_TXRX_MARGIN, pModal->rxTxMarginCh[0]);
  2068. if (AR_SREV_9285_11(ah))
  2069. REG_WRITE(ah, AR9285_AN_TOP4, (AR9285_AN_TOP4_DEFAULT | 0x14));
  2070. /* Initialize Ant Diversity settings from EEPROM */
  2071. if (pModal->version == 3) {
  2072. ant_div_control1 = ((pModal->ob_234 >> 12) & 0xf);
  2073. ant_div_control2 = ((pModal->db1_234 >> 12) & 0xf);
  2074. regVal = REG_READ(ah, 0x99ac);
  2075. regVal &= (~(0x7f000000));
  2076. regVal |= ((ant_div_control1 & 0x1) << 24);
  2077. regVal |= (((ant_div_control1 >> 1) & 0x1) << 29);
  2078. regVal |= (((ant_div_control1 >> 2) & 0x1) << 30);
  2079. regVal |= ((ant_div_control2 & 0x3) << 25);
  2080. regVal |= (((ant_div_control2 >> 2) & 0x3) << 27);
  2081. REG_WRITE(ah, 0x99ac, regVal);
  2082. regVal = REG_READ(ah, 0x99ac);
  2083. regVal = REG_READ(ah, 0xa208);
  2084. regVal &= (~(0x1 << 13));
  2085. regVal |= (((ant_div_control1 >> 3) & 0x1) << 13);
  2086. REG_WRITE(ah, 0xa208, regVal);
  2087. regVal = REG_READ(ah, 0xa208);
  2088. }
  2089. if (pModal->version >= 2) {
  2090. ob[0] = (pModal->ob_01 & 0xf);
  2091. ob[1] = (pModal->ob_01 >> 4) & 0xf;
  2092. ob[2] = (pModal->ob_234 & 0xf);
  2093. ob[3] = ((pModal->ob_234 >> 4) & 0xf);
  2094. ob[4] = ((pModal->ob_234 >> 8) & 0xf);
  2095. db1[0] = (pModal->db1_01 & 0xf);
  2096. db1[1] = ((pModal->db1_01 >> 4) & 0xf);
  2097. db1[2] = (pModal->db1_234 & 0xf);
  2098. db1[3] = ((pModal->db1_234 >> 4) & 0xf);
  2099. db1[4] = ((pModal->db1_234 >> 8) & 0xf);
  2100. db2[0] = (pModal->db2_01 & 0xf);
  2101. db2[1] = ((pModal->db2_01 >> 4) & 0xf);
  2102. db2[2] = (pModal->db2_234 & 0xf);
  2103. db2[3] = ((pModal->db2_234 >> 4) & 0xf);
  2104. db2[4] = ((pModal->db2_234 >> 8) & 0xf);
  2105. } else if (pModal->version == 1) {
  2106. DPRINTF(ah->ah_sc, ATH_DBG_EEPROM,
  2107. "EEPROM Model version is set to 1 \n");
  2108. ob[0] = (pModal->ob_01 & 0xf);
  2109. ob[1] = ob[2] = ob[3] = ob[4] = (pModal->ob_01 >> 4) & 0xf;
  2110. db1[0] = (pModal->db1_01 & 0xf);
  2111. db1[1] = db1[2] = db1[3] =
  2112. db1[4] = ((pModal->db1_01 >> 4) & 0xf);
  2113. db2[0] = (pModal->db2_01 & 0xf);
  2114. db2[1] = db2[2] = db2[3] =
  2115. db2[4] = ((pModal->db2_01 >> 4) & 0xf);
  2116. } else {
  2117. int i;
  2118. for (i = 0; i < 5; i++) {
  2119. ob[i] = pModal->ob_01;
  2120. db1[i] = pModal->db1_01;
  2121. db2[i] = pModal->db1_01;
  2122. }
  2123. }
  2124. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2125. AR9285_AN_RF2G3_OB_0, AR9285_AN_RF2G3_OB_0_S, ob[0]);
  2126. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2127. AR9285_AN_RF2G3_OB_1, AR9285_AN_RF2G3_OB_1_S, ob[1]);
  2128. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2129. AR9285_AN_RF2G3_OB_2, AR9285_AN_RF2G3_OB_2_S, ob[2]);
  2130. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2131. AR9285_AN_RF2G3_OB_3, AR9285_AN_RF2G3_OB_3_S, ob[3]);
  2132. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2133. AR9285_AN_RF2G3_OB_4, AR9285_AN_RF2G3_OB_4_S, ob[4]);
  2134. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2135. AR9285_AN_RF2G3_DB1_0, AR9285_AN_RF2G3_DB1_0_S, db1[0]);
  2136. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2137. AR9285_AN_RF2G3_DB1_1, AR9285_AN_RF2G3_DB1_1_S, db1[1]);
  2138. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G3,
  2139. AR9285_AN_RF2G3_DB1_2, AR9285_AN_RF2G3_DB1_2_S, db1[2]);
  2140. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2141. AR9285_AN_RF2G4_DB1_3, AR9285_AN_RF2G4_DB1_3_S, db1[3]);
  2142. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2143. AR9285_AN_RF2G4_DB1_4, AR9285_AN_RF2G4_DB1_4_S, db1[4]);
  2144. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2145. AR9285_AN_RF2G4_DB2_0, AR9285_AN_RF2G4_DB2_0_S, db2[0]);
  2146. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2147. AR9285_AN_RF2G4_DB2_1, AR9285_AN_RF2G4_DB2_1_S, db2[1]);
  2148. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2149. AR9285_AN_RF2G4_DB2_2, AR9285_AN_RF2G4_DB2_2_S, db2[2]);
  2150. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2151. AR9285_AN_RF2G4_DB2_3, AR9285_AN_RF2G4_DB2_3_S, db2[3]);
  2152. ath9k_hw_analog_shift_rmw(ah, AR9285_AN_RF2G4,
  2153. AR9285_AN_RF2G4_DB2_4, AR9285_AN_RF2G4_DB2_4_S, db2[4]);
  2154. if (AR_SREV_9285_11(ah))
  2155. REG_WRITE(ah, AR9285_AN_TOP4, AR9285_AN_TOP4_DEFAULT);
  2156. REG_RMW_FIELD(ah, AR_PHY_SETTLING, AR_PHY_SETTLING_SWITCH,
  2157. pModal->switchSettling);
  2158. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ, AR_PHY_DESIRED_SZ_ADC,
  2159. pModal->adcDesiredSize);
  2160. REG_WRITE(ah, AR_PHY_RF_CTL4,
  2161. SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAA_OFF) |
  2162. SM(pModal->txEndToXpaOff, AR_PHY_RF_CTL4_TX_END_XPAB_OFF) |
  2163. SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAA_ON) |
  2164. SM(pModal->txFrameToXpaOn, AR_PHY_RF_CTL4_FRAME_XPAB_ON));
  2165. REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_A2_RX_ON,
  2166. pModal->txEndToRxOn);
  2167. REG_RMW_FIELD(ah, AR_PHY_CCA, AR9280_PHY_CCA_THRESH62,
  2168. pModal->thresh62);
  2169. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA0, AR_PHY_EXT_CCA0_THRESH62,
  2170. pModal->thresh62);
  2171. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  2172. AR5416_EEP_MINOR_VER_2) {
  2173. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_DATA_START,
  2174. pModal->txFrameToDataStart);
  2175. REG_RMW_FIELD(ah, AR_PHY_RF_CTL2, AR_PHY_TX_END_PA_ON,
  2176. pModal->txFrameToPaOn);
  2177. }
  2178. if ((eep->baseEepHeader.version & AR5416_EEP_VER_MINOR_MASK) >=
  2179. AR5416_EEP_MINOR_VER_3) {
  2180. if (IS_CHAN_HT40(chan))
  2181. REG_RMW_FIELD(ah, AR_PHY_SETTLING,
  2182. AR_PHY_SETTLING_SWITCH,
  2183. pModal->swSettleHt40);
  2184. }
  2185. return true;
  2186. }
  2187. static bool (*ath9k_eeprom_set_board_values[])(struct ath_hal *,
  2188. struct ath9k_channel *) = {
  2189. ath9k_hw_eeprom_set_def_board_values,
  2190. ath9k_hw_eeprom_set_4k_board_values
  2191. };
  2192. bool ath9k_hw_eeprom_set_board_values(struct ath_hal *ah,
  2193. struct ath9k_channel *chan)
  2194. {
  2195. struct ath_hal_5416 *ahp = AH5416(ah);
  2196. return ath9k_eeprom_set_board_values[ahp->ah_eep_map](ah, chan);
  2197. }
  2198. static int ath9k_hw_get_def_eeprom_antenna_cfg(struct ath_hal *ah,
  2199. struct ath9k_channel *chan,
  2200. u8 index, u16 *config)
  2201. {
  2202. struct ath_hal_5416 *ahp = AH5416(ah);
  2203. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  2204. struct modal_eep_header *pModal =
  2205. &(eep->modalHeader[IS_CHAN_2GHZ(chan)]);
  2206. struct base_eep_header *pBase = &eep->baseEepHeader;
  2207. switch (index) {
  2208. case 0:
  2209. *config = pModal->antCtrlCommon & 0xFFFF;
  2210. return 0;
  2211. case 1:
  2212. if (pBase->version >= 0x0E0D) {
  2213. if (pModal->useAnt1) {
  2214. *config =
  2215. ((pModal->antCtrlCommon & 0xFFFF0000) >> 16);
  2216. return 0;
  2217. }
  2218. }
  2219. break;
  2220. default:
  2221. break;
  2222. }
  2223. return -EINVAL;
  2224. }
  2225. static int ath9k_hw_get_4k_eeprom_antenna_cfg(struct ath_hal *ah,
  2226. struct ath9k_channel *chan,
  2227. u8 index, u16 *config)
  2228. {
  2229. struct ath_hal_5416 *ahp = AH5416(ah);
  2230. struct ar5416_eeprom_4k *eep = &ahp->ah_eeprom.map4k;
  2231. struct modal_eep_4k_header *pModal = &eep->modalHeader;
  2232. switch (index) {
  2233. case 0:
  2234. *config = pModal->antCtrlCommon & 0xFFFF;
  2235. return 0;
  2236. default:
  2237. break;
  2238. }
  2239. return -EINVAL;
  2240. }
  2241. static int (*ath9k_get_eeprom_antenna_cfg[])(struct ath_hal *,
  2242. struct ath9k_channel *,
  2243. u8, u16 *) = {
  2244. ath9k_hw_get_def_eeprom_antenna_cfg,
  2245. ath9k_hw_get_4k_eeprom_antenna_cfg
  2246. };
  2247. int ath9k_hw_get_eeprom_antenna_cfg(struct ath_hal *ah,
  2248. struct ath9k_channel *chan,
  2249. u8 index, u16 *config)
  2250. {
  2251. struct ath_hal_5416 *ahp = AH5416(ah);
  2252. return ath9k_get_eeprom_antenna_cfg[ahp->ah_eep_map](ah, chan,
  2253. index, config);
  2254. }
  2255. static u8 ath9k_hw_get_4k_num_ant_config(struct ath_hal *ah,
  2256. enum ieee80211_band freq_band)
  2257. {
  2258. return 1;
  2259. }
  2260. static u8 ath9k_hw_get_def_num_ant_config(struct ath_hal *ah,
  2261. enum ieee80211_band freq_band)
  2262. {
  2263. struct ath_hal_5416 *ahp = AH5416(ah);
  2264. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  2265. struct modal_eep_header *pModal =
  2266. &(eep->modalHeader[ATH9K_HAL_FREQ_BAND_2GHZ == freq_band]);
  2267. struct base_eep_header *pBase = &eep->baseEepHeader;
  2268. u8 num_ant_config;
  2269. num_ant_config = 1;
  2270. if (pBase->version >= 0x0E0D)
  2271. if (pModal->useAnt1)
  2272. num_ant_config += 1;
  2273. return num_ant_config;
  2274. }
  2275. static u8 (*ath9k_get_num_ant_config[])(struct ath_hal *,
  2276. enum ieee80211_band) = {
  2277. ath9k_hw_get_def_num_ant_config,
  2278. ath9k_hw_get_4k_num_ant_config
  2279. };
  2280. u8 ath9k_hw_get_num_ant_config(struct ath_hal *ah,
  2281. enum ieee80211_band freq_band)
  2282. {
  2283. struct ath_hal_5416 *ahp = AH5416(ah);
  2284. return ath9k_get_num_ant_config[ahp->ah_eep_map](ah, freq_band);
  2285. }
  2286. u16 ath9k_hw_eeprom_get_spur_chan(struct ath_hal *ah, u16 i, bool is2GHz)
  2287. {
  2288. #define EEP_MAP4K_SPURCHAN \
  2289. (ahp->ah_eeprom.map4k.modalHeader.spurChans[i].spurChan)
  2290. #define EEP_DEF_SPURCHAN \
  2291. (ahp->ah_eeprom.def.modalHeader[is2GHz].spurChans[i].spurChan)
  2292. struct ath_hal_5416 *ahp = AH5416(ah);
  2293. u16 spur_val = AR_NO_SPUR;
  2294. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2295. "Getting spur idx %d is2Ghz. %d val %x\n",
  2296. i, is2GHz, ah->ah_config.spurchans[i][is2GHz]);
  2297. switch (ah->ah_config.spurmode) {
  2298. case SPUR_DISABLE:
  2299. break;
  2300. case SPUR_ENABLE_IOCTL:
  2301. spur_val = ah->ah_config.spurchans[i][is2GHz];
  2302. DPRINTF(ah->ah_sc, ATH_DBG_ANI,
  2303. "Getting spur val from new loc. %d\n", spur_val);
  2304. break;
  2305. case SPUR_ENABLE_EEPROM:
  2306. if (ahp->ah_eep_map == EEP_MAP_4KBITS)
  2307. spur_val = EEP_MAP4K_SPURCHAN;
  2308. else
  2309. spur_val = EEP_DEF_SPURCHAN;
  2310. break;
  2311. }
  2312. return spur_val;
  2313. #undef EEP_DEF_SPURCHAN
  2314. #undef EEP_MAP4K_SPURCHAN
  2315. }
  2316. static u32 ath9k_hw_get_eeprom_4k(struct ath_hal *ah,
  2317. enum eeprom_param param)
  2318. {
  2319. struct ath_hal_5416 *ahp = AH5416(ah);
  2320. struct ar5416_eeprom_4k *eep = &ahp->ah_eeprom.map4k;
  2321. struct modal_eep_4k_header *pModal = &eep->modalHeader;
  2322. struct base_eep_header_4k *pBase = &eep->baseEepHeader;
  2323. switch (param) {
  2324. case EEP_NFTHRESH_2:
  2325. return pModal[1].noiseFloorThreshCh[0];
  2326. case AR_EEPROM_MAC(0):
  2327. return pBase->macAddr[0] << 8 | pBase->macAddr[1];
  2328. case AR_EEPROM_MAC(1):
  2329. return pBase->macAddr[2] << 8 | pBase->macAddr[3];
  2330. case AR_EEPROM_MAC(2):
  2331. return pBase->macAddr[4] << 8 | pBase->macAddr[5];
  2332. case EEP_REG_0:
  2333. return pBase->regDmn[0];
  2334. case EEP_REG_1:
  2335. return pBase->regDmn[1];
  2336. case EEP_OP_CAP:
  2337. return pBase->deviceCap;
  2338. case EEP_OP_MODE:
  2339. return pBase->opCapFlags;
  2340. case EEP_RF_SILENT:
  2341. return pBase->rfSilent;
  2342. case EEP_OB_2:
  2343. return pModal->ob_01;
  2344. case EEP_DB_2:
  2345. return pModal->db1_01;
  2346. case EEP_MINOR_REV:
  2347. return pBase->version & AR5416_EEP_VER_MINOR_MASK;
  2348. case EEP_TX_MASK:
  2349. return pBase->txMask;
  2350. case EEP_RX_MASK:
  2351. return pBase->rxMask;
  2352. default:
  2353. return 0;
  2354. }
  2355. }
  2356. static u32 ath9k_hw_get_eeprom_def(struct ath_hal *ah,
  2357. enum eeprom_param param)
  2358. {
  2359. struct ath_hal_5416 *ahp = AH5416(ah);
  2360. struct ar5416_eeprom_def *eep = &ahp->ah_eeprom.def;
  2361. struct modal_eep_header *pModal = eep->modalHeader;
  2362. struct base_eep_header *pBase = &eep->baseEepHeader;
  2363. switch (param) {
  2364. case EEP_NFTHRESH_5:
  2365. return pModal[0].noiseFloorThreshCh[0];
  2366. case EEP_NFTHRESH_2:
  2367. return pModal[1].noiseFloorThreshCh[0];
  2368. case AR_EEPROM_MAC(0):
  2369. return pBase->macAddr[0] << 8 | pBase->macAddr[1];
  2370. case AR_EEPROM_MAC(1):
  2371. return pBase->macAddr[2] << 8 | pBase->macAddr[3];
  2372. case AR_EEPROM_MAC(2):
  2373. return pBase->macAddr[4] << 8 | pBase->macAddr[5];
  2374. case EEP_REG_0:
  2375. return pBase->regDmn[0];
  2376. case EEP_REG_1:
  2377. return pBase->regDmn[1];
  2378. case EEP_OP_CAP:
  2379. return pBase->deviceCap;
  2380. case EEP_OP_MODE:
  2381. return pBase->opCapFlags;
  2382. case EEP_RF_SILENT:
  2383. return pBase->rfSilent;
  2384. case EEP_OB_5:
  2385. return pModal[0].ob;
  2386. case EEP_DB_5:
  2387. return pModal[0].db;
  2388. case EEP_OB_2:
  2389. return pModal[1].ob;
  2390. case EEP_DB_2:
  2391. return pModal[1].db;
  2392. case EEP_MINOR_REV:
  2393. return pBase->version & AR5416_EEP_VER_MINOR_MASK;
  2394. case EEP_TX_MASK:
  2395. return pBase->txMask;
  2396. case EEP_RX_MASK:
  2397. return pBase->rxMask;
  2398. case EEP_RXGAIN_TYPE:
  2399. return pBase->rxGainType;
  2400. case EEP_TXGAIN_TYPE:
  2401. return pBase->txGainType;
  2402. default:
  2403. return 0;
  2404. }
  2405. }
  2406. static u32 (*ath9k_get_eeprom[])(struct ath_hal *, enum eeprom_param) = {
  2407. ath9k_hw_get_eeprom_def,
  2408. ath9k_hw_get_eeprom_4k
  2409. };
  2410. u32 ath9k_hw_get_eeprom(struct ath_hal *ah,
  2411. enum eeprom_param param)
  2412. {
  2413. struct ath_hal_5416 *ahp = AH5416(ah);
  2414. return ath9k_get_eeprom[ahp->ah_eep_map](ah, param);
  2415. }
  2416. int ath9k_hw_eeprom_attach(struct ath_hal *ah)
  2417. {
  2418. int status;
  2419. struct ath_hal_5416 *ahp = AH5416(ah);
  2420. if (ath9k_hw_use_flash(ah))
  2421. ath9k_hw_flash_map(ah);
  2422. if (AR_SREV_9285(ah))
  2423. ahp->ah_eep_map = EEP_MAP_4KBITS;
  2424. else
  2425. ahp->ah_eep_map = EEP_MAP_DEFAULT;
  2426. if (!ath9k_hw_fill_eeprom(ah))
  2427. return -EIO;
  2428. status = ath9k_hw_check_eeprom(ah);
  2429. return status;
  2430. }