eeprom.c 77 KB

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