eeprom.c 77 KB

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