phy.c 82 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140
  1. /*
  2. * PHY functions
  3. *
  4. * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
  5. * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
  6. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  7. * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
  8. *
  9. * Permission to use, copy, modify, and distribute this software for any
  10. * purpose with or without fee is hereby granted, provided that the above
  11. * copyright notice and this permission notice appear in all copies.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  14. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  15. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  16. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  17. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  18. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  19. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  20. *
  21. */
  22. #include <linux/delay.h>
  23. #include "ath5k.h"
  24. #include "reg.h"
  25. #include "base.h"
  26. #include "rfbuffer.h"
  27. #include "rfgain.h"
  28. /*
  29. * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
  30. */
  31. static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
  32. const struct ath5k_rf_reg *rf_regs,
  33. u32 val, u8 reg_id, bool set)
  34. {
  35. const struct ath5k_rf_reg *rfreg = NULL;
  36. u8 offset, bank, num_bits, col, position;
  37. u16 entry;
  38. u32 mask, data, last_bit, bits_shifted, first_bit;
  39. u32 *rfb;
  40. s32 bits_left;
  41. int i;
  42. data = 0;
  43. rfb = ah->ah_rf_banks;
  44. for (i = 0; i < ah->ah_rf_regs_count; i++) {
  45. if (rf_regs[i].index == reg_id) {
  46. rfreg = &rf_regs[i];
  47. break;
  48. }
  49. }
  50. if (rfb == NULL || rfreg == NULL) {
  51. ATH5K_PRINTF("Rf register not found!\n");
  52. /* should not happen */
  53. return 0;
  54. }
  55. bank = rfreg->bank;
  56. num_bits = rfreg->field.len;
  57. first_bit = rfreg->field.pos;
  58. col = rfreg->field.col;
  59. /* first_bit is an offset from bank's
  60. * start. Since we have all banks on
  61. * the same array, we use this offset
  62. * to mark each bank's start */
  63. offset = ah->ah_offset[bank];
  64. /* Boundary check */
  65. if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
  66. ATH5K_PRINTF("invalid values at offset %u\n", offset);
  67. return 0;
  68. }
  69. entry = ((first_bit - 1) / 8) + offset;
  70. position = (first_bit - 1) % 8;
  71. if (set)
  72. data = ath5k_hw_bitswap(val, num_bits);
  73. for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
  74. position = 0, entry++) {
  75. last_bit = (position + bits_left > 8) ? 8 :
  76. position + bits_left;
  77. mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
  78. (col * 8);
  79. if (set) {
  80. rfb[entry] &= ~mask;
  81. rfb[entry] |= ((data << position) << (col * 8)) & mask;
  82. data >>= (8 - position);
  83. } else {
  84. data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
  85. << bits_shifted;
  86. bits_shifted += last_bit - position;
  87. }
  88. bits_left -= 8 - position;
  89. }
  90. data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
  91. return data;
  92. }
  93. /**********************\
  94. * RF Gain optimization *
  95. \**********************/
  96. /*
  97. * This code is used to optimize rf gain on different environments
  98. * (temperature mostly) based on feedback from a power detector.
  99. *
  100. * It's only used on RF5111 and RF5112, later RF chips seem to have
  101. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  102. * no gain optimization ladder-.
  103. *
  104. * For more infos check out this patent doc
  105. * http://www.freepatentsonline.com/7400691.html
  106. *
  107. * This paper describes power drops as seen on the receiver due to
  108. * probe packets
  109. * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  110. * %20of%20Power%20Control.pdf
  111. *
  112. * And this is the MadWiFi bug entry related to the above
  113. * http://madwifi-project.org/ticket/1659
  114. * with various measurements and diagrams
  115. *
  116. * TODO: Deal with power drops due to probes by setting an apropriate
  117. * tx power on the probe packets ! Make this part of the calibration process.
  118. */
  119. /* Initialize ah_gain durring attach */
  120. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  121. {
  122. /* Initialize the gain optimization values */
  123. switch (ah->ah_radio) {
  124. case AR5K_RF5111:
  125. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  126. ah->ah_gain.g_low = 20;
  127. ah->ah_gain.g_high = 35;
  128. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  129. break;
  130. case AR5K_RF5112:
  131. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  132. ah->ah_gain.g_low = 20;
  133. ah->ah_gain.g_high = 85;
  134. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  135. break;
  136. default:
  137. return -EINVAL;
  138. }
  139. return 0;
  140. }
  141. /* Schedule a gain probe check on the next transmited packet.
  142. * That means our next packet is going to be sent with lower
  143. * tx power and a Peak to Average Power Detector (PAPD) will try
  144. * to measure the gain.
  145. *
  146. * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
  147. * just after we enable the probe so that we don't mess with
  148. * standard traffic ? Maybe it's time to use sw interrupts and
  149. * a probe tasklet !!!
  150. */
  151. static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  152. {
  153. /* Skip if gain calibration is inactive or
  154. * we already handle a probe request */
  155. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  156. return;
  157. /* Send the packet with 2dB below max power as
  158. * patent doc suggest */
  159. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_ofdm - 4,
  160. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  161. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  162. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  163. }
  164. /* Calculate gain_F measurement correction
  165. * based on the current step for RF5112 rev. 2 */
  166. static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  167. {
  168. u32 mix, step;
  169. u32 *rf;
  170. const struct ath5k_gain_opt *go;
  171. const struct ath5k_gain_opt_step *g_step;
  172. const struct ath5k_rf_reg *rf_regs;
  173. /* Only RF5112 Rev. 2 supports it */
  174. if ((ah->ah_radio != AR5K_RF5112) ||
  175. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  176. return 0;
  177. go = &rfgain_opt_5112;
  178. rf_regs = rf_regs_5112a;
  179. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  180. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  181. if (ah->ah_rf_banks == NULL)
  182. return 0;
  183. rf = ah->ah_rf_banks;
  184. ah->ah_gain.g_f_corr = 0;
  185. /* No VGA (Variable Gain Amplifier) override, skip */
  186. if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
  187. return 0;
  188. /* Mix gain stepping */
  189. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
  190. /* Mix gain override */
  191. mix = g_step->gos_param[0];
  192. switch (mix) {
  193. case 3:
  194. ah->ah_gain.g_f_corr = step * 2;
  195. break;
  196. case 2:
  197. ah->ah_gain.g_f_corr = (step - 5) * 2;
  198. break;
  199. case 1:
  200. ah->ah_gain.g_f_corr = step;
  201. break;
  202. default:
  203. ah->ah_gain.g_f_corr = 0;
  204. break;
  205. }
  206. return ah->ah_gain.g_f_corr;
  207. }
  208. /* Check if current gain_F measurement is in the range of our
  209. * power detector windows. If we get a measurement outside range
  210. * we know it's not accurate (detectors can't measure anything outside
  211. * their detection window) so we must ignore it */
  212. static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  213. {
  214. const struct ath5k_rf_reg *rf_regs;
  215. u32 step, mix_ovr, level[4];
  216. u32 *rf;
  217. if (ah->ah_rf_banks == NULL)
  218. return false;
  219. rf = ah->ah_rf_banks;
  220. if (ah->ah_radio == AR5K_RF5111) {
  221. rf_regs = rf_regs_5111;
  222. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  223. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
  224. false);
  225. level[0] = 0;
  226. level[1] = (step == 63) ? 50 : step + 4;
  227. level[2] = (step != 63) ? 64 : level[0];
  228. level[3] = level[2] + 50 ;
  229. ah->ah_gain.g_high = level[3] -
  230. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  231. ah->ah_gain.g_low = level[0] +
  232. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  233. } else {
  234. rf_regs = rf_regs_5112;
  235. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  236. mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
  237. false);
  238. level[0] = level[2] = 0;
  239. if (mix_ovr == 1) {
  240. level[1] = level[3] = 83;
  241. } else {
  242. level[1] = level[3] = 107;
  243. ah->ah_gain.g_high = 55;
  244. }
  245. }
  246. return (ah->ah_gain.g_current >= level[0] &&
  247. ah->ah_gain.g_current <= level[1]) ||
  248. (ah->ah_gain.g_current >= level[2] &&
  249. ah->ah_gain.g_current <= level[3]);
  250. }
  251. /* Perform gain_F adjustment by choosing the right set
  252. * of parameters from rf gain optimization ladder */
  253. static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  254. {
  255. const struct ath5k_gain_opt *go;
  256. const struct ath5k_gain_opt_step *g_step;
  257. int ret = 0;
  258. switch (ah->ah_radio) {
  259. case AR5K_RF5111:
  260. go = &rfgain_opt_5111;
  261. break;
  262. case AR5K_RF5112:
  263. go = &rfgain_opt_5112;
  264. break;
  265. default:
  266. return 0;
  267. }
  268. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  269. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  270. /* Reached maximum */
  271. if (ah->ah_gain.g_step_idx == 0)
  272. return -1;
  273. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  274. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  275. ah->ah_gain.g_step_idx > 0;
  276. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  277. ah->ah_gain.g_target -= 2 *
  278. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  279. g_step->gos_gain);
  280. ret = 1;
  281. goto done;
  282. }
  283. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  284. /* Reached minimum */
  285. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  286. return -2;
  287. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  288. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  289. ah->ah_gain.g_step_idx < go->go_steps_count-1;
  290. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  291. ah->ah_gain.g_target -= 2 *
  292. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  293. g_step->gos_gain);
  294. ret = 2;
  295. goto done;
  296. }
  297. done:
  298. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  299. "ret %d, gain step %u, current gain %u, target gain %u\n",
  300. ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
  301. ah->ah_gain.g_target);
  302. return ret;
  303. }
  304. /* Main callback for thermal rf gain calibration engine
  305. * Check for a new gain reading and schedule an adjustment
  306. * if needed.
  307. *
  308. * TODO: Use sw interrupt to schedule reset if gain_F needs
  309. * adjustment */
  310. enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  311. {
  312. u32 data, type;
  313. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  314. ATH5K_TRACE(ah->ah_sc);
  315. if (ah->ah_rf_banks == NULL ||
  316. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  317. return AR5K_RFGAIN_INACTIVE;
  318. /* No check requested, either engine is inactive
  319. * or an adjustment is already requested */
  320. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  321. goto done;
  322. /* Read the PAPD (Peak to Average Power Detector)
  323. * register */
  324. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  325. /* No probe is scheduled, read gain_F measurement */
  326. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  327. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  328. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  329. /* If tx packet is CCK correct the gain_F measurement
  330. * by cck ofdm gain delta */
  331. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  332. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  333. ah->ah_gain.g_current +=
  334. ee->ee_cck_ofdm_gain_delta;
  335. else
  336. ah->ah_gain.g_current +=
  337. AR5K_GAIN_CCK_PROBE_CORR;
  338. }
  339. /* Further correct gain_F measurement for
  340. * RF5112A radios */
  341. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  342. ath5k_hw_rf_gainf_corr(ah);
  343. ah->ah_gain.g_current =
  344. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  345. (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
  346. 0;
  347. }
  348. /* Check if measurement is ok and if we need
  349. * to adjust gain, schedule a gain adjustment,
  350. * else switch back to the acive state */
  351. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  352. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  353. ath5k_hw_rf_gainf_adjust(ah)) {
  354. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  355. } else {
  356. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  357. }
  358. }
  359. done:
  360. return ah->ah_gain.g_state;
  361. }
  362. /* Write initial rf gain table to set the RF sensitivity
  363. * this one works on all RF chips and has nothing to do
  364. * with gain_F calibration */
  365. int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
  366. {
  367. const struct ath5k_ini_rfgain *ath5k_rfg;
  368. unsigned int i, size;
  369. switch (ah->ah_radio) {
  370. case AR5K_RF5111:
  371. ath5k_rfg = rfgain_5111;
  372. size = ARRAY_SIZE(rfgain_5111);
  373. break;
  374. case AR5K_RF5112:
  375. ath5k_rfg = rfgain_5112;
  376. size = ARRAY_SIZE(rfgain_5112);
  377. break;
  378. case AR5K_RF2413:
  379. ath5k_rfg = rfgain_2413;
  380. size = ARRAY_SIZE(rfgain_2413);
  381. break;
  382. case AR5K_RF2316:
  383. ath5k_rfg = rfgain_2316;
  384. size = ARRAY_SIZE(rfgain_2316);
  385. break;
  386. case AR5K_RF5413:
  387. ath5k_rfg = rfgain_5413;
  388. size = ARRAY_SIZE(rfgain_5413);
  389. break;
  390. case AR5K_RF2317:
  391. case AR5K_RF2425:
  392. ath5k_rfg = rfgain_2425;
  393. size = ARRAY_SIZE(rfgain_2425);
  394. break;
  395. default:
  396. return -EINVAL;
  397. }
  398. switch (freq) {
  399. case AR5K_INI_RFGAIN_2GHZ:
  400. case AR5K_INI_RFGAIN_5GHZ:
  401. break;
  402. default:
  403. return -EINVAL;
  404. }
  405. for (i = 0; i < size; i++) {
  406. AR5K_REG_WAIT(i);
  407. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
  408. (u32)ath5k_rfg[i].rfg_register);
  409. }
  410. return 0;
  411. }
  412. /********************\
  413. * RF Registers setup *
  414. \********************/
  415. /*
  416. * Setup RF registers by writing rf buffer on hw
  417. */
  418. int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  419. unsigned int mode)
  420. {
  421. const struct ath5k_rf_reg *rf_regs;
  422. const struct ath5k_ini_rfbuffer *ini_rfb;
  423. const struct ath5k_gain_opt *go = NULL;
  424. const struct ath5k_gain_opt_step *g_step;
  425. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  426. u8 ee_mode = 0;
  427. u32 *rfb;
  428. int i, obdb = -1, bank = -1;
  429. switch (ah->ah_radio) {
  430. case AR5K_RF5111:
  431. rf_regs = rf_regs_5111;
  432. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  433. ini_rfb = rfb_5111;
  434. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
  435. go = &rfgain_opt_5111;
  436. break;
  437. case AR5K_RF5112:
  438. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  439. rf_regs = rf_regs_5112a;
  440. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  441. ini_rfb = rfb_5112a;
  442. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
  443. } else {
  444. rf_regs = rf_regs_5112;
  445. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  446. ini_rfb = rfb_5112;
  447. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
  448. }
  449. go = &rfgain_opt_5112;
  450. break;
  451. case AR5K_RF2413:
  452. rf_regs = rf_regs_2413;
  453. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
  454. ini_rfb = rfb_2413;
  455. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
  456. break;
  457. case AR5K_RF2316:
  458. rf_regs = rf_regs_2316;
  459. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
  460. ini_rfb = rfb_2316;
  461. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
  462. break;
  463. case AR5K_RF5413:
  464. rf_regs = rf_regs_5413;
  465. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
  466. ini_rfb = rfb_5413;
  467. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
  468. break;
  469. case AR5K_RF2317:
  470. rf_regs = rf_regs_2425;
  471. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  472. ini_rfb = rfb_2317;
  473. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
  474. break;
  475. case AR5K_RF2425:
  476. rf_regs = rf_regs_2425;
  477. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  478. if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
  479. ini_rfb = rfb_2425;
  480. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
  481. } else {
  482. ini_rfb = rfb_2417;
  483. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
  484. }
  485. break;
  486. default:
  487. return -EINVAL;
  488. }
  489. /* If it's the first time we set rf buffer, allocate
  490. * ah->ah_rf_banks based on ah->ah_rf_banks_size
  491. * we set above */
  492. if (ah->ah_rf_banks == NULL) {
  493. ah->ah_rf_banks = kmalloc(sizeof(u32) * ah->ah_rf_banks_size,
  494. GFP_KERNEL);
  495. if (ah->ah_rf_banks == NULL) {
  496. ATH5K_ERR(ah->ah_sc, "out of memory\n");
  497. return -ENOMEM;
  498. }
  499. }
  500. /* Copy values to modify them */
  501. rfb = ah->ah_rf_banks;
  502. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  503. if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
  504. ATH5K_ERR(ah->ah_sc, "invalid bank\n");
  505. return -EINVAL;
  506. }
  507. /* Bank changed, write down the offset */
  508. if (bank != ini_rfb[i].rfb_bank) {
  509. bank = ini_rfb[i].rfb_bank;
  510. ah->ah_offset[bank] = i;
  511. }
  512. rfb[i] = ini_rfb[i].rfb_mode_data[mode];
  513. }
  514. /* Set Output and Driver bias current (OB/DB) */
  515. if (channel->hw_value & CHANNEL_2GHZ) {
  516. if (channel->hw_value & CHANNEL_CCK)
  517. ee_mode = AR5K_EEPROM_MODE_11B;
  518. else
  519. ee_mode = AR5K_EEPROM_MODE_11G;
  520. /* For RF511X/RF211X combination we
  521. * use b_OB and b_DB parameters stored
  522. * in eeprom on ee->ee_ob[ee_mode][0]
  523. *
  524. * For all other chips we use OB/DB for 2Ghz
  525. * stored in the b/g modal section just like
  526. * 802.11a on ee->ee_ob[ee_mode][1] */
  527. if ((ah->ah_radio == AR5K_RF5111) ||
  528. (ah->ah_radio == AR5K_RF5112))
  529. obdb = 0;
  530. else
  531. obdb = 1;
  532. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  533. AR5K_RF_OB_2GHZ, true);
  534. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  535. AR5K_RF_DB_2GHZ, true);
  536. /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
  537. } else if ((channel->hw_value & CHANNEL_5GHZ) ||
  538. (ah->ah_radio == AR5K_RF5111)) {
  539. /* For 11a, Turbo and XR we need to choose
  540. * OB/DB based on frequency range */
  541. ee_mode = AR5K_EEPROM_MODE_11A;
  542. obdb = channel->center_freq >= 5725 ? 3 :
  543. (channel->center_freq >= 5500 ? 2 :
  544. (channel->center_freq >= 5260 ? 1 :
  545. (channel->center_freq > 4000 ? 0 : -1)));
  546. if (obdb < 0)
  547. return -EINVAL;
  548. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  549. AR5K_RF_OB_5GHZ, true);
  550. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  551. AR5K_RF_DB_5GHZ, true);
  552. }
  553. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  554. /* Bank Modifications (chip-specific) */
  555. if (ah->ah_radio == AR5K_RF5111) {
  556. /* Set gain_F settings according to current step */
  557. if (channel->hw_value & CHANNEL_OFDM) {
  558. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  559. AR5K_PHY_FRAME_CTL_TX_CLIP,
  560. g_step->gos_param[0]);
  561. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  562. AR5K_RF_PWD_90, true);
  563. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  564. AR5K_RF_PWD_84, true);
  565. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  566. AR5K_RF_RFGAIN_SEL, true);
  567. /* We programmed gain_F parameters, switch back
  568. * to active state */
  569. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  570. }
  571. /* Bank 6/7 setup */
  572. ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
  573. AR5K_RF_PWD_XPD, true);
  574. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
  575. AR5K_RF_XPD_GAIN, true);
  576. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  577. AR5K_RF_GAIN_I, true);
  578. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  579. AR5K_RF_PLO_SEL, true);
  580. /* TODO: Half/quarter channel support */
  581. }
  582. if (ah->ah_radio == AR5K_RF5112) {
  583. /* Set gain_F settings according to current step */
  584. if (channel->hw_value & CHANNEL_OFDM) {
  585. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
  586. AR5K_RF_MIXGAIN_OVR, true);
  587. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  588. AR5K_RF_PWD_138, true);
  589. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  590. AR5K_RF_PWD_137, true);
  591. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  592. AR5K_RF_PWD_136, true);
  593. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
  594. AR5K_RF_PWD_132, true);
  595. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
  596. AR5K_RF_PWD_131, true);
  597. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
  598. AR5K_RF_PWD_130, true);
  599. /* We programmed gain_F parameters, switch back
  600. * to active state */
  601. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  602. }
  603. /* Bank 6/7 setup */
  604. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  605. AR5K_RF_XPD_SEL, true);
  606. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  607. /* Rev. 1 supports only one xpd */
  608. ath5k_hw_rfb_op(ah, rf_regs,
  609. ee->ee_x_gain[ee_mode],
  610. AR5K_RF_XPD_GAIN, true);
  611. } else {
  612. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  613. if (ee->ee_pd_gains[ee_mode] > 1) {
  614. ath5k_hw_rfb_op(ah, rf_regs,
  615. pdg_curve_to_idx[0],
  616. AR5K_RF_PD_GAIN_LO, true);
  617. ath5k_hw_rfb_op(ah, rf_regs,
  618. pdg_curve_to_idx[1],
  619. AR5K_RF_PD_GAIN_HI, true);
  620. } else {
  621. ath5k_hw_rfb_op(ah, rf_regs,
  622. pdg_curve_to_idx[0],
  623. AR5K_RF_PD_GAIN_LO, true);
  624. ath5k_hw_rfb_op(ah, rf_regs,
  625. pdg_curve_to_idx[0],
  626. AR5K_RF_PD_GAIN_HI, true);
  627. }
  628. /* Lower synth voltage on Rev 2 */
  629. ath5k_hw_rfb_op(ah, rf_regs, 2,
  630. AR5K_RF_HIGH_VC_CP, true);
  631. ath5k_hw_rfb_op(ah, rf_regs, 2,
  632. AR5K_RF_MID_VC_CP, true);
  633. ath5k_hw_rfb_op(ah, rf_regs, 2,
  634. AR5K_RF_LOW_VC_CP, true);
  635. ath5k_hw_rfb_op(ah, rf_regs, 2,
  636. AR5K_RF_PUSH_UP, true);
  637. /* Decrease power consumption on 5213+ BaseBand */
  638. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  639. ath5k_hw_rfb_op(ah, rf_regs, 1,
  640. AR5K_RF_PAD2GND, true);
  641. ath5k_hw_rfb_op(ah, rf_regs, 1,
  642. AR5K_RF_XB2_LVL, true);
  643. ath5k_hw_rfb_op(ah, rf_regs, 1,
  644. AR5K_RF_XB5_LVL, true);
  645. ath5k_hw_rfb_op(ah, rf_regs, 1,
  646. AR5K_RF_PWD_167, true);
  647. ath5k_hw_rfb_op(ah, rf_regs, 1,
  648. AR5K_RF_PWD_166, true);
  649. }
  650. }
  651. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  652. AR5K_RF_GAIN_I, true);
  653. /* TODO: Half/quarter channel support */
  654. }
  655. if (ah->ah_radio == AR5K_RF5413 &&
  656. channel->hw_value & CHANNEL_2GHZ) {
  657. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
  658. true);
  659. /* Set optimum value for early revisions (on pci-e chips) */
  660. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  661. ah->ah_mac_srev < AR5K_SREV_AR5413)
  662. ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
  663. AR5K_RF_PWD_ICLOBUF_2G, true);
  664. }
  665. /* Write RF banks on hw */
  666. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  667. AR5K_REG_WAIT(i);
  668. ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
  669. }
  670. return 0;
  671. }
  672. /**************************\
  673. PHY/RF channel functions
  674. \**************************/
  675. /*
  676. * Check if a channel is supported
  677. */
  678. bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
  679. {
  680. /* Check if the channel is in our supported range */
  681. if (flags & CHANNEL_2GHZ) {
  682. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  683. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  684. return true;
  685. } else if (flags & CHANNEL_5GHZ)
  686. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  687. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  688. return true;
  689. return false;
  690. }
  691. /*
  692. * Convertion needed for RF5110
  693. */
  694. static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
  695. {
  696. u32 athchan;
  697. /*
  698. * Convert IEEE channel/MHz to an internal channel value used
  699. * by the AR5210 chipset. This has not been verified with
  700. * newer chipsets like the AR5212A who have a completely
  701. * different RF/PHY part.
  702. */
  703. athchan = (ath5k_hw_bitswap(
  704. (ieee80211_frequency_to_channel(
  705. channel->center_freq) - 24) / 2, 5)
  706. << 1) | (1 << 6) | 0x1;
  707. return athchan;
  708. }
  709. /*
  710. * Set channel on RF5110
  711. */
  712. static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  713. struct ieee80211_channel *channel)
  714. {
  715. u32 data;
  716. /*
  717. * Set the channel and wait
  718. */
  719. data = ath5k_hw_rf5110_chan2athchan(channel);
  720. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  721. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  722. mdelay(1);
  723. return 0;
  724. }
  725. /*
  726. * Convertion needed for 5111
  727. */
  728. static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  729. struct ath5k_athchan_2ghz *athchan)
  730. {
  731. int channel;
  732. /* Cast this value to catch negative channel numbers (>= -19) */
  733. channel = (int)ieee;
  734. /*
  735. * Map 2GHz IEEE channel to 5GHz Atheros channel
  736. */
  737. if (channel <= 13) {
  738. athchan->a2_athchan = 115 + channel;
  739. athchan->a2_flags = 0x46;
  740. } else if (channel == 14) {
  741. athchan->a2_athchan = 124;
  742. athchan->a2_flags = 0x44;
  743. } else if (channel >= 15 && channel <= 26) {
  744. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  745. athchan->a2_flags = 0x46;
  746. } else
  747. return -EINVAL;
  748. return 0;
  749. }
  750. /*
  751. * Set channel on 5111
  752. */
  753. static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  754. struct ieee80211_channel *channel)
  755. {
  756. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  757. unsigned int ath5k_channel =
  758. ieee80211_frequency_to_channel(channel->center_freq);
  759. u32 data0, data1, clock;
  760. int ret;
  761. /*
  762. * Set the channel on the RF5111 radio
  763. */
  764. data0 = data1 = 0;
  765. if (channel->hw_value & CHANNEL_2GHZ) {
  766. /* Map 2GHz channel to 5GHz Atheros channel ID */
  767. ret = ath5k_hw_rf5111_chan2athchan(
  768. ieee80211_frequency_to_channel(channel->center_freq),
  769. &ath5k_channel_2ghz);
  770. if (ret)
  771. return ret;
  772. ath5k_channel = ath5k_channel_2ghz.a2_athchan;
  773. data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
  774. << 5) | (1 << 4);
  775. }
  776. if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
  777. clock = 1;
  778. data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
  779. (clock << 1) | (1 << 10) | 1;
  780. } else {
  781. clock = 0;
  782. data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
  783. << 2) | (clock << 1) | (1 << 10) | 1;
  784. }
  785. ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
  786. AR5K_RF_BUFFER);
  787. ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
  788. AR5K_RF_BUFFER_CONTROL_3);
  789. return 0;
  790. }
  791. /*
  792. * Set channel on 5112 and newer
  793. */
  794. static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
  795. struct ieee80211_channel *channel)
  796. {
  797. u32 data, data0, data1, data2;
  798. u16 c;
  799. data = data0 = data1 = data2 = 0;
  800. c = channel->center_freq;
  801. if (c < 4800) {
  802. if (!((c - 2224) % 5)) {
  803. data0 = ((2 * (c - 704)) - 3040) / 10;
  804. data1 = 1;
  805. } else if (!((c - 2192) % 5)) {
  806. data0 = ((2 * (c - 672)) - 3040) / 10;
  807. data1 = 0;
  808. } else
  809. return -EINVAL;
  810. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  811. } else if ((c - (c % 5)) != 2 || c > 5435) {
  812. if (!(c % 20) && c >= 5120) {
  813. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  814. data2 = ath5k_hw_bitswap(3, 2);
  815. } else if (!(c % 10)) {
  816. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  817. data2 = ath5k_hw_bitswap(2, 2);
  818. } else if (!(c % 5)) {
  819. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  820. data2 = ath5k_hw_bitswap(1, 2);
  821. } else
  822. return -EINVAL;
  823. } else {
  824. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  825. data2 = ath5k_hw_bitswap(0, 2);
  826. }
  827. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  828. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  829. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  830. return 0;
  831. }
  832. /*
  833. * Set the channel on the RF2425
  834. */
  835. static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  836. struct ieee80211_channel *channel)
  837. {
  838. u32 data, data0, data2;
  839. u16 c;
  840. data = data0 = data2 = 0;
  841. c = channel->center_freq;
  842. if (c < 4800) {
  843. data0 = ath5k_hw_bitswap((c - 2272), 8);
  844. data2 = 0;
  845. /* ? 5GHz ? */
  846. } else if ((c - (c % 5)) != 2 || c > 5435) {
  847. if (!(c % 20) && c < 5120)
  848. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  849. else if (!(c % 10))
  850. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  851. else if (!(c % 5))
  852. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  853. else
  854. return -EINVAL;
  855. data2 = ath5k_hw_bitswap(1, 2);
  856. } else {
  857. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  858. data2 = ath5k_hw_bitswap(0, 2);
  859. }
  860. data = (data0 << 4) | data2 << 2 | 0x1001;
  861. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  862. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  863. return 0;
  864. }
  865. /*
  866. * Set a channel on the radio chip
  867. */
  868. int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  869. {
  870. int ret;
  871. /*
  872. * Check bounds supported by the PHY (we don't care about regultory
  873. * restrictions at this point). Note: hw_value already has the band
  874. * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
  875. * of the band by that */
  876. if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
  877. ATH5K_ERR(ah->ah_sc,
  878. "channel frequency (%u MHz) out of supported "
  879. "band range\n",
  880. channel->center_freq);
  881. return -EINVAL;
  882. }
  883. /*
  884. * Set the channel and wait
  885. */
  886. switch (ah->ah_radio) {
  887. case AR5K_RF5110:
  888. ret = ath5k_hw_rf5110_channel(ah, channel);
  889. break;
  890. case AR5K_RF5111:
  891. ret = ath5k_hw_rf5111_channel(ah, channel);
  892. break;
  893. case AR5K_RF2425:
  894. ret = ath5k_hw_rf2425_channel(ah, channel);
  895. break;
  896. default:
  897. ret = ath5k_hw_rf5112_channel(ah, channel);
  898. break;
  899. }
  900. if (ret)
  901. return ret;
  902. /* Set JAPAN setting for channel 14 */
  903. if (channel->center_freq == 2484) {
  904. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  905. AR5K_PHY_CCKTXCTL_JAPAN);
  906. } else {
  907. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  908. AR5K_PHY_CCKTXCTL_WORLD);
  909. }
  910. ah->ah_current_channel = channel;
  911. ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false;
  912. return 0;
  913. }
  914. /*****************\
  915. PHY calibration
  916. \*****************/
  917. void
  918. ath5k_hw_calibration_poll(struct ath5k_hw *ah)
  919. {
  920. /* Calibration interval in jiffies */
  921. unsigned long cal_intval;
  922. cal_intval = msecs_to_jiffies(ah->ah_cal_intval * 1000);
  923. /* Initialize timestamp if needed */
  924. if (!ah->ah_cal_tstamp)
  925. ah->ah_cal_tstamp = jiffies;
  926. /* For now we always do full calibration
  927. * Mark software interrupt mask and fire software
  928. * interrupt (bit gets auto-cleared) */
  929. if (time_is_before_eq_jiffies(ah->ah_cal_tstamp + cal_intval)) {
  930. ah->ah_cal_tstamp = jiffies;
  931. ah->ah_swi_mask = AR5K_SWI_FULL_CALIBRATION;
  932. AR5K_REG_ENABLE_BITS(ah, AR5K_CR, AR5K_CR_SWI);
  933. }
  934. }
  935. static int sign_extend(int val, const int nbits)
  936. {
  937. int order = BIT(nbits-1);
  938. return (val ^ order) - order;
  939. }
  940. static s32 ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)
  941. {
  942. s32 val;
  943. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  944. return sign_extend(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 9);
  945. }
  946. void ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)
  947. {
  948. int i;
  949. ah->ah_nfcal_hist.index = 0;
  950. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++)
  951. ah->ah_nfcal_hist.nfval[i] = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  952. }
  953. static void ath5k_hw_update_nfcal_hist(struct ath5k_hw *ah, s16 noise_floor)
  954. {
  955. struct ath5k_nfcal_hist *hist = &ah->ah_nfcal_hist;
  956. hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX-1);
  957. hist->nfval[hist->index] = noise_floor;
  958. }
  959. static s16 ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)
  960. {
  961. s16 sort[ATH5K_NF_CAL_HIST_MAX];
  962. s16 tmp;
  963. int i, j;
  964. memcpy(sort, ah->ah_nfcal_hist.nfval, sizeof(sort));
  965. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX - 1; i++) {
  966. for (j = 1; j < ATH5K_NF_CAL_HIST_MAX - i; j++) {
  967. if (sort[j] > sort[j-1]) {
  968. tmp = sort[j];
  969. sort[j] = sort[j-1];
  970. sort[j-1] = tmp;
  971. }
  972. }
  973. }
  974. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++) {
  975. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  976. "cal %d:%d\n", i, sort[i]);
  977. }
  978. return sort[(ATH5K_NF_CAL_HIST_MAX-1) / 2];
  979. }
  980. /*
  981. * When we tell the hardware to perform a noise floor calibration
  982. * by setting the AR5K_PHY_AGCCTL_NF bit, it will periodically
  983. * sample-and-hold the minimum noise level seen at the antennas.
  984. * This value is then stored in a ring buffer of recently measured
  985. * noise floor values so we have a moving window of the last few
  986. * samples.
  987. *
  988. * The median of the values in the history is then loaded into the
  989. * hardware for its own use for RSSI and CCA measurements.
  990. */
  991. static void ath5k_hw_update_noise_floor(struct ath5k_hw *ah)
  992. {
  993. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  994. u32 val;
  995. s16 nf, threshold;
  996. u8 ee_mode;
  997. /* keep last value if calibration hasn't completed */
  998. if (ath5k_hw_reg_read(ah, AR5K_PHY_AGCCTL) & AR5K_PHY_AGCCTL_NF) {
  999. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  1000. "NF did not complete in calibration window\n");
  1001. return;
  1002. }
  1003. switch (ah->ah_current_channel->hw_value & CHANNEL_MODES) {
  1004. case CHANNEL_A:
  1005. case CHANNEL_T:
  1006. case CHANNEL_XR:
  1007. ee_mode = AR5K_EEPROM_MODE_11A;
  1008. break;
  1009. case CHANNEL_G:
  1010. case CHANNEL_TG:
  1011. ee_mode = AR5K_EEPROM_MODE_11G;
  1012. break;
  1013. default:
  1014. case CHANNEL_B:
  1015. ee_mode = AR5K_EEPROM_MODE_11B;
  1016. break;
  1017. }
  1018. /* completed NF calibration, test threshold */
  1019. nf = ath5k_hw_read_measured_noise_floor(ah);
  1020. threshold = ee->ee_noise_floor_thr[ee_mode];
  1021. if (nf > threshold) {
  1022. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  1023. "noise floor failure detected; "
  1024. "read %d, threshold %d\n",
  1025. nf, threshold);
  1026. nf = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  1027. }
  1028. ath5k_hw_update_nfcal_hist(ah, nf);
  1029. nf = ath5k_hw_get_median_noise_floor(ah);
  1030. /* load noise floor (in .5 dBm) so the hardware will use it */
  1031. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF) & ~AR5K_PHY_NF_M;
  1032. val |= (nf * 2) & AR5K_PHY_NF_M;
  1033. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1034. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1035. ~(AR5K_PHY_AGCCTL_NF_EN | AR5K_PHY_AGCCTL_NF_NOUPDATE));
  1036. ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1037. 0, false);
  1038. /*
  1039. * Load a high max CCA Power value (-50 dBm in .5 dBm units)
  1040. * so that we're not capped by the median we just loaded.
  1041. * This will be used as the initial value for the next noise
  1042. * floor calibration.
  1043. */
  1044. val = (val & ~AR5K_PHY_NF_M) | ((-50 * 2) & AR5K_PHY_NF_M);
  1045. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1046. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1047. AR5K_PHY_AGCCTL_NF_EN |
  1048. AR5K_PHY_AGCCTL_NF_NOUPDATE |
  1049. AR5K_PHY_AGCCTL_NF);
  1050. ah->ah_noise_floor = nf;
  1051. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  1052. "noise floor calibrated: %d\n", nf);
  1053. }
  1054. /*
  1055. * Perform a PHY calibration on RF5110
  1056. * -Fix BPSK/QAM Constellation (I/Q correction)
  1057. * -Calculate Noise Floor
  1058. */
  1059. static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  1060. struct ieee80211_channel *channel)
  1061. {
  1062. u32 phy_sig, phy_agc, phy_sat, beacon;
  1063. int ret;
  1064. /*
  1065. * Disable beacons and RX/TX queues, wait
  1066. */
  1067. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1068. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  1069. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  1070. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  1071. mdelay(2);
  1072. /*
  1073. * Set the channel (with AGC turned off)
  1074. */
  1075. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1076. udelay(10);
  1077. ret = ath5k_hw_channel(ah, channel);
  1078. /*
  1079. * Activate PHY and wait
  1080. */
  1081. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1082. mdelay(1);
  1083. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1084. if (ret)
  1085. return ret;
  1086. /*
  1087. * Calibrate the radio chip
  1088. */
  1089. /* Remember normal state */
  1090. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  1091. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  1092. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  1093. /* Update radio registers */
  1094. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1095. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1096. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1097. AR5K_PHY_AGCCOARSE_LO)) |
  1098. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1099. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1100. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1101. AR5K_PHY_ADCSAT_THR)) |
  1102. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1103. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1104. udelay(20);
  1105. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1106. udelay(10);
  1107. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1108. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1109. mdelay(1);
  1110. /*
  1111. * Enable calibration and wait until completion
  1112. */
  1113. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1114. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1115. AR5K_PHY_AGCCTL_CAL, 0, false);
  1116. /* Reset to normal state */
  1117. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1118. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1119. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1120. if (ret) {
  1121. ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
  1122. channel->center_freq);
  1123. return ret;
  1124. }
  1125. ath5k_hw_update_noise_floor(ah);
  1126. /*
  1127. * Re-enable RX/TX and beacons
  1128. */
  1129. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1130. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  1131. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1132. return 0;
  1133. }
  1134. /*
  1135. * Perform a PHY calibration on RF5111/5112 and newer chips
  1136. */
  1137. static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
  1138. struct ieee80211_channel *channel)
  1139. {
  1140. u32 i_pwr, q_pwr;
  1141. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1142. int i;
  1143. ATH5K_TRACE(ah->ah_sc);
  1144. if (!ah->ah_calibration ||
  1145. ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
  1146. goto done;
  1147. /* Calibration has finished, get the results and re-run */
  1148. for (i = 0; i <= 10; i++) {
  1149. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1150. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1151. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1152. }
  1153. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1154. if (ah->ah_version == AR5K_AR5211)
  1155. q_coffd = q_pwr >> 6;
  1156. else
  1157. q_coffd = q_pwr >> 7;
  1158. /* No correction */
  1159. if (i_coffd == 0 || q_coffd == 0)
  1160. goto done;
  1161. i_coff = ((-iq_corr) / i_coffd);
  1162. /* Boundary check */
  1163. if (i_coff > 31)
  1164. i_coff = 31;
  1165. if (i_coff < -32)
  1166. i_coff = -32;
  1167. if (ah->ah_version == AR5K_AR5211)
  1168. q_coff = (i_pwr / q_coffd) - 64;
  1169. else
  1170. q_coff = (i_pwr / q_coffd) - 128;
  1171. /* Boundary check */
  1172. if (q_coff > 15)
  1173. q_coff = 15;
  1174. if (q_coff < -16)
  1175. q_coff = -16;
  1176. /* Commit new I/Q value */
  1177. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
  1178. ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
  1179. /* Re-enable calibration -if we don't we'll commit
  1180. * the same values again and again */
  1181. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1182. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1183. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1184. done:
  1185. /* TODO: Separate noise floor calibration from I/Q calibration
  1186. * since noise floor calibration interrupts rx path while I/Q
  1187. * calibration doesn't. We don't need to run noise floor calibration
  1188. * as often as I/Q calibration.*/
  1189. ath5k_hw_update_noise_floor(ah);
  1190. /* Initiate a gain_F calibration */
  1191. ath5k_hw_request_rfgain_probe(ah);
  1192. return 0;
  1193. }
  1194. /*
  1195. * Perform a PHY calibration
  1196. */
  1197. int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1198. struct ieee80211_channel *channel)
  1199. {
  1200. int ret;
  1201. if (ah->ah_radio == AR5K_RF5110)
  1202. ret = ath5k_hw_rf5110_calibrate(ah, channel);
  1203. else
  1204. ret = ath5k_hw_rf511x_calibrate(ah, channel);
  1205. return ret;
  1206. }
  1207. /***************************\
  1208. * Spur mitigation functions *
  1209. \***************************/
  1210. bool ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
  1211. struct ieee80211_channel *channel)
  1212. {
  1213. u8 refclk_freq;
  1214. if ((ah->ah_radio == AR5K_RF5112) ||
  1215. (ah->ah_radio == AR5K_RF5413) ||
  1216. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  1217. refclk_freq = 40;
  1218. else
  1219. refclk_freq = 32;
  1220. if ((channel->center_freq % refclk_freq != 0) &&
  1221. ((channel->center_freq % refclk_freq < 10) ||
  1222. (channel->center_freq % refclk_freq > 22)))
  1223. return true;
  1224. else
  1225. return false;
  1226. }
  1227. void
  1228. ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,
  1229. struct ieee80211_channel *channel)
  1230. {
  1231. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1232. u32 mag_mask[4] = {0, 0, 0, 0};
  1233. u32 pilot_mask[2] = {0, 0};
  1234. /* Note: fbin values are scaled up by 2 */
  1235. u16 spur_chan_fbin, chan_fbin, symbol_width, spur_detection_window;
  1236. s32 spur_delta_phase, spur_freq_sigma_delta;
  1237. s32 spur_offset, num_symbols_x16;
  1238. u8 num_symbol_offsets, i, freq_band;
  1239. /* Convert current frequency to fbin value (the same way channels
  1240. * are stored on EEPROM, check out ath5k_eeprom_bin2freq) and scale
  1241. * up by 2 so we can compare it later */
  1242. if (channel->hw_value & CHANNEL_2GHZ) {
  1243. chan_fbin = (channel->center_freq - 2300) * 10;
  1244. freq_band = AR5K_EEPROM_BAND_2GHZ;
  1245. } else {
  1246. chan_fbin = (channel->center_freq - 4900) * 10;
  1247. freq_band = AR5K_EEPROM_BAND_5GHZ;
  1248. }
  1249. /* Check if any spur_chan_fbin from EEPROM is
  1250. * within our current channel's spur detection range */
  1251. spur_chan_fbin = AR5K_EEPROM_NO_SPUR;
  1252. spur_detection_window = AR5K_SPUR_CHAN_WIDTH;
  1253. /* XXX: Half/Quarter channels ?*/
  1254. if (channel->hw_value & CHANNEL_TURBO)
  1255. spur_detection_window *= 2;
  1256. for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
  1257. spur_chan_fbin = ee->ee_spur_chans[i][freq_band];
  1258. /* Note: mask cleans AR5K_EEPROM_NO_SPUR flag
  1259. * so it's zero if we got nothing from EEPROM */
  1260. if (spur_chan_fbin == AR5K_EEPROM_NO_SPUR) {
  1261. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1262. break;
  1263. }
  1264. if ((chan_fbin - spur_detection_window <=
  1265. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK)) &&
  1266. (chan_fbin + spur_detection_window >=
  1267. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK))) {
  1268. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1269. break;
  1270. }
  1271. }
  1272. /* We need to enable spur filter for this channel */
  1273. if (spur_chan_fbin) {
  1274. spur_offset = spur_chan_fbin - chan_fbin;
  1275. /*
  1276. * Calculate deltas:
  1277. * spur_freq_sigma_delta -> spur_offset / sample_freq << 21
  1278. * spur_delta_phase -> spur_offset / chip_freq << 11
  1279. * Note: Both values have 100KHz resolution
  1280. */
  1281. /* XXX: Half/Quarter rate channels ? */
  1282. switch (channel->hw_value) {
  1283. case CHANNEL_A:
  1284. /* Both sample_freq and chip_freq are 40MHz */
  1285. spur_delta_phase = (spur_offset << 17) / 25;
  1286. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1287. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1288. break;
  1289. case CHANNEL_G:
  1290. /* sample_freq -> 40MHz chip_freq -> 44MHz
  1291. * (for b compatibility) */
  1292. spur_freq_sigma_delta = (spur_offset << 8) / 55;
  1293. spur_delta_phase = (spur_offset << 17) / 25;
  1294. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1295. break;
  1296. case CHANNEL_T:
  1297. case CHANNEL_TG:
  1298. /* Both sample_freq and chip_freq are 80MHz */
  1299. spur_delta_phase = (spur_offset << 16) / 25;
  1300. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1301. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_TURBO_100Hz;
  1302. break;
  1303. default:
  1304. return;
  1305. }
  1306. /* Calculate pilot and magnitude masks */
  1307. /* Scale up spur_offset by 1000 to switch to 100HZ resolution
  1308. * and divide by symbol_width to find how many symbols we have
  1309. * Note: number of symbols is scaled up by 16 */
  1310. num_symbols_x16 = ((spur_offset * 1000) << 4) / symbol_width;
  1311. /* Spur is on a symbol if num_symbols_x16 % 16 is zero */
  1312. if (!(num_symbols_x16 & 0xF))
  1313. /* _X_ */
  1314. num_symbol_offsets = 3;
  1315. else
  1316. /* _xx_ */
  1317. num_symbol_offsets = 4;
  1318. for (i = 0; i < num_symbol_offsets; i++) {
  1319. /* Calculate pilot mask */
  1320. s32 curr_sym_off =
  1321. (num_symbols_x16 / 16) + i + 25;
  1322. /* Pilot magnitude mask seems to be a way to
  1323. * declare the boundaries for our detection
  1324. * window or something, it's 2 for the middle
  1325. * value(s) where the symbol is expected to be
  1326. * and 1 on the boundary values */
  1327. u8 plt_mag_map =
  1328. (i == 0 || i == (num_symbol_offsets - 1))
  1329. ? 1 : 2;
  1330. if (curr_sym_off >= 0 && curr_sym_off <= 32) {
  1331. if (curr_sym_off <= 25)
  1332. pilot_mask[0] |= 1 << curr_sym_off;
  1333. else if (curr_sym_off >= 27)
  1334. pilot_mask[0] |= 1 << (curr_sym_off - 1);
  1335. } else if (curr_sym_off >= 33 && curr_sym_off <= 52)
  1336. pilot_mask[1] |= 1 << (curr_sym_off - 33);
  1337. /* Calculate magnitude mask (for viterbi decoder) */
  1338. if (curr_sym_off >= -1 && curr_sym_off <= 14)
  1339. mag_mask[0] |=
  1340. plt_mag_map << (curr_sym_off + 1) * 2;
  1341. else if (curr_sym_off >= 15 && curr_sym_off <= 30)
  1342. mag_mask[1] |=
  1343. plt_mag_map << (curr_sym_off - 15) * 2;
  1344. else if (curr_sym_off >= 31 && curr_sym_off <= 46)
  1345. mag_mask[2] |=
  1346. plt_mag_map << (curr_sym_off - 31) * 2;
  1347. else if (curr_sym_off >= 46 && curr_sym_off <= 53)
  1348. mag_mask[3] |=
  1349. plt_mag_map << (curr_sym_off - 47) * 2;
  1350. }
  1351. /* Write settings on hw to enable spur filter */
  1352. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1353. AR5K_PHY_BIN_MASK_CTL_RATE, 0xff);
  1354. /* XXX: Self correlator also ? */
  1355. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1356. AR5K_PHY_IQ_PILOT_MASK_EN |
  1357. AR5K_PHY_IQ_CHAN_MASK_EN |
  1358. AR5K_PHY_IQ_SPUR_FILT_EN);
  1359. /* Set delta phase and freq sigma delta */
  1360. ath5k_hw_reg_write(ah,
  1361. AR5K_REG_SM(spur_delta_phase,
  1362. AR5K_PHY_TIMING_11_SPUR_DELTA_PHASE) |
  1363. AR5K_REG_SM(spur_freq_sigma_delta,
  1364. AR5K_PHY_TIMING_11_SPUR_FREQ_SD) |
  1365. AR5K_PHY_TIMING_11_USE_SPUR_IN_AGC,
  1366. AR5K_PHY_TIMING_11);
  1367. /* Write pilot masks */
  1368. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_7);
  1369. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1370. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1371. pilot_mask[1]);
  1372. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_9);
  1373. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1374. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1375. pilot_mask[1]);
  1376. /* Write magnitude masks */
  1377. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK_1);
  1378. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK_2);
  1379. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK_3);
  1380. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1381. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1382. mag_mask[3]);
  1383. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK2_1);
  1384. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK2_2);
  1385. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK2_3);
  1386. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1387. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1388. mag_mask[3]);
  1389. } else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) &
  1390. AR5K_PHY_IQ_SPUR_FILT_EN) {
  1391. /* Clean up spur mitigation settings and disable fliter */
  1392. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1393. AR5K_PHY_BIN_MASK_CTL_RATE, 0);
  1394. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_IQ,
  1395. AR5K_PHY_IQ_PILOT_MASK_EN |
  1396. AR5K_PHY_IQ_CHAN_MASK_EN |
  1397. AR5K_PHY_IQ_SPUR_FILT_EN);
  1398. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_11);
  1399. /* Clear pilot masks */
  1400. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_7);
  1401. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1402. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1403. 0);
  1404. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_9);
  1405. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1406. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1407. 0);
  1408. /* Clear magnitude masks */
  1409. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_1);
  1410. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_2);
  1411. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_3);
  1412. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1413. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1414. 0);
  1415. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_1);
  1416. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_2);
  1417. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_3);
  1418. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1419. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1420. 0);
  1421. }
  1422. }
  1423. /********************\
  1424. Misc PHY functions
  1425. \********************/
  1426. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  1427. {
  1428. ATH5K_TRACE(ah->ah_sc);
  1429. /*Just a try M.F.*/
  1430. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  1431. return 0;
  1432. }
  1433. /*
  1434. * Get the PHY Chip revision
  1435. */
  1436. u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
  1437. {
  1438. unsigned int i;
  1439. u32 srev;
  1440. u16 ret;
  1441. ATH5K_TRACE(ah->ah_sc);
  1442. /*
  1443. * Set the radio chip access register
  1444. */
  1445. switch (chan) {
  1446. case CHANNEL_2GHZ:
  1447. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  1448. break;
  1449. case CHANNEL_5GHZ:
  1450. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1451. break;
  1452. default:
  1453. return 0;
  1454. }
  1455. mdelay(2);
  1456. /* ...wait until PHY is ready and read the selected radio revision */
  1457. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  1458. for (i = 0; i < 8; i++)
  1459. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  1460. if (ah->ah_version == AR5K_AR5210) {
  1461. srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
  1462. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  1463. } else {
  1464. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  1465. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  1466. ((srev & 0x0f) << 4), 8);
  1467. }
  1468. /* Reset to the 5GHz mode */
  1469. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1470. return ret;
  1471. }
  1472. /*****************\
  1473. * Antenna control *
  1474. \*****************/
  1475. static void /*TODO:Boundary check*/
  1476. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)
  1477. {
  1478. ATH5K_TRACE(ah->ah_sc);
  1479. if (ah->ah_version != AR5K_AR5210)
  1480. ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);
  1481. }
  1482. /*
  1483. * Enable/disable fast rx antenna diversity
  1484. */
  1485. static void
  1486. ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)
  1487. {
  1488. switch (ee_mode) {
  1489. case AR5K_EEPROM_MODE_11G:
  1490. /* XXX: This is set to
  1491. * disabled on initvals !!! */
  1492. case AR5K_EEPROM_MODE_11A:
  1493. if (enable)
  1494. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1495. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1496. else
  1497. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1498. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1499. break;
  1500. case AR5K_EEPROM_MODE_11B:
  1501. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1502. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1503. break;
  1504. default:
  1505. return;
  1506. }
  1507. if (enable) {
  1508. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1509. AR5K_PHY_RESTART_DIV_GC, 0xc);
  1510. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1511. AR5K_PHY_FAST_ANT_DIV_EN);
  1512. } else {
  1513. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1514. AR5K_PHY_RESTART_DIV_GC, 0x8);
  1515. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1516. AR5K_PHY_FAST_ANT_DIV_EN);
  1517. }
  1518. }
  1519. /*
  1520. * Set antenna operating mode
  1521. */
  1522. void
  1523. ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)
  1524. {
  1525. struct ieee80211_channel *channel = ah->ah_current_channel;
  1526. bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;
  1527. bool use_def_for_sg;
  1528. u8 def_ant, tx_ant, ee_mode;
  1529. u32 sta_id1 = 0;
  1530. def_ant = ah->ah_def_ant;
  1531. ATH5K_TRACE(ah->ah_sc);
  1532. switch (channel->hw_value & CHANNEL_MODES) {
  1533. case CHANNEL_A:
  1534. case CHANNEL_T:
  1535. case CHANNEL_XR:
  1536. ee_mode = AR5K_EEPROM_MODE_11A;
  1537. break;
  1538. case CHANNEL_G:
  1539. case CHANNEL_TG:
  1540. ee_mode = AR5K_EEPROM_MODE_11G;
  1541. break;
  1542. case CHANNEL_B:
  1543. ee_mode = AR5K_EEPROM_MODE_11B;
  1544. break;
  1545. default:
  1546. ATH5K_ERR(ah->ah_sc,
  1547. "invalid channel: %d\n", channel->center_freq);
  1548. return;
  1549. }
  1550. switch (ant_mode) {
  1551. case AR5K_ANTMODE_DEFAULT:
  1552. tx_ant = 0;
  1553. use_def_for_tx = false;
  1554. update_def_on_tx = false;
  1555. use_def_for_rts = false;
  1556. use_def_for_sg = false;
  1557. fast_div = true;
  1558. break;
  1559. case AR5K_ANTMODE_FIXED_A:
  1560. def_ant = 1;
  1561. tx_ant = 0;
  1562. use_def_for_tx = true;
  1563. update_def_on_tx = false;
  1564. use_def_for_rts = true;
  1565. use_def_for_sg = true;
  1566. fast_div = false;
  1567. break;
  1568. case AR5K_ANTMODE_FIXED_B:
  1569. def_ant = 2;
  1570. tx_ant = 0;
  1571. use_def_for_tx = true;
  1572. update_def_on_tx = false;
  1573. use_def_for_rts = true;
  1574. use_def_for_sg = true;
  1575. fast_div = false;
  1576. break;
  1577. case AR5K_ANTMODE_SINGLE_AP:
  1578. def_ant = 1; /* updated on tx */
  1579. tx_ant = 0;
  1580. use_def_for_tx = true;
  1581. update_def_on_tx = true;
  1582. use_def_for_rts = true;
  1583. use_def_for_sg = true;
  1584. fast_div = true;
  1585. break;
  1586. case AR5K_ANTMODE_SECTOR_AP:
  1587. tx_ant = 1; /* variable */
  1588. use_def_for_tx = false;
  1589. update_def_on_tx = false;
  1590. use_def_for_rts = true;
  1591. use_def_for_sg = false;
  1592. fast_div = false;
  1593. break;
  1594. case AR5K_ANTMODE_SECTOR_STA:
  1595. tx_ant = 1; /* variable */
  1596. use_def_for_tx = true;
  1597. update_def_on_tx = false;
  1598. use_def_for_rts = true;
  1599. use_def_for_sg = false;
  1600. fast_div = true;
  1601. break;
  1602. case AR5K_ANTMODE_DEBUG:
  1603. def_ant = 1;
  1604. tx_ant = 2;
  1605. use_def_for_tx = false;
  1606. update_def_on_tx = false;
  1607. use_def_for_rts = false;
  1608. use_def_for_sg = false;
  1609. fast_div = false;
  1610. break;
  1611. default:
  1612. return;
  1613. }
  1614. ah->ah_tx_ant = tx_ant;
  1615. ah->ah_ant_mode = ant_mode;
  1616. ah->ah_def_ant = def_ant;
  1617. sta_id1 |= use_def_for_tx ? AR5K_STA_ID1_DEFAULT_ANTENNA : 0;
  1618. sta_id1 |= update_def_on_tx ? AR5K_STA_ID1_DESC_ANTENNA : 0;
  1619. sta_id1 |= use_def_for_rts ? AR5K_STA_ID1_RTS_DEF_ANTENNA : 0;
  1620. sta_id1 |= use_def_for_sg ? AR5K_STA_ID1_SELFGEN_DEF_ANT : 0;
  1621. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_ANTENNA_SETTINGS);
  1622. if (sta_id1)
  1623. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, sta_id1);
  1624. /* Note: set diversity before default antenna
  1625. * because it won't work correctly */
  1626. ath5k_hw_set_fast_div(ah, ee_mode, fast_div);
  1627. ath5k_hw_set_def_antenna(ah, def_ant);
  1628. }
  1629. /****************\
  1630. * TX power setup *
  1631. \****************/
  1632. /*
  1633. * Helper functions
  1634. */
  1635. /*
  1636. * Do linear interpolation between two given (x, y) points
  1637. */
  1638. static s16
  1639. ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
  1640. s16 y_left, s16 y_right)
  1641. {
  1642. s16 ratio, result;
  1643. /* Avoid divide by zero and skip interpolation
  1644. * if we have the same point */
  1645. if ((x_left == x_right) || (y_left == y_right))
  1646. return y_left;
  1647. /*
  1648. * Since we use ints and not fps, we need to scale up in
  1649. * order to get a sane ratio value (or else we 'll eg. get
  1650. * always 1 instead of 1.25, 1.75 etc). We scale up by 100
  1651. * to have some accuracy both for 0.5 and 0.25 steps.
  1652. */
  1653. ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left));
  1654. /* Now scale down to be in range */
  1655. result = y_left + (ratio * (target - x_left) / 100);
  1656. return result;
  1657. }
  1658. /*
  1659. * Find vertical boundary (min pwr) for the linear PCDAC curve.
  1660. *
  1661. * Since we have the top of the curve and we draw the line below
  1662. * until we reach 1 (1 pcdac step) we need to know which point
  1663. * (x value) that is so that we don't go below y axis and have negative
  1664. * pcdac values when creating the curve, or fill the table with zeroes.
  1665. */
  1666. static s16
  1667. ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
  1668. const s16 *pwrL, const s16 *pwrR)
  1669. {
  1670. s8 tmp;
  1671. s16 min_pwrL, min_pwrR;
  1672. s16 pwr_i;
  1673. /* Some vendors write the same pcdac value twice !!! */
  1674. if (stepL[0] == stepL[1] || stepR[0] == stepR[1])
  1675. return max(pwrL[0], pwrR[0]);
  1676. if (pwrL[0] == pwrL[1])
  1677. min_pwrL = pwrL[0];
  1678. else {
  1679. pwr_i = pwrL[0];
  1680. do {
  1681. pwr_i--;
  1682. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1683. pwrL[0], pwrL[1],
  1684. stepL[0], stepL[1]);
  1685. } while (tmp > 1);
  1686. min_pwrL = pwr_i;
  1687. }
  1688. if (pwrR[0] == pwrR[1])
  1689. min_pwrR = pwrR[0];
  1690. else {
  1691. pwr_i = pwrR[0];
  1692. do {
  1693. pwr_i--;
  1694. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1695. pwrR[0], pwrR[1],
  1696. stepR[0], stepR[1]);
  1697. } while (tmp > 1);
  1698. min_pwrR = pwr_i;
  1699. }
  1700. /* Keep the right boundary so that it works for both curves */
  1701. return max(min_pwrL, min_pwrR);
  1702. }
  1703. /*
  1704. * Interpolate (pwr,vpd) points to create a Power to PDADC or a
  1705. * Power to PCDAC curve.
  1706. *
  1707. * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
  1708. * steps (offsets) on y axis. Power can go up to 31.5dB and max
  1709. * PCDAC/PDADC step for each curve is 64 but we can write more than
  1710. * one curves on hw so we can go up to 128 (which is the max step we
  1711. * can write on the final table).
  1712. *
  1713. * We write y values (PCDAC/PDADC steps) on hw.
  1714. */
  1715. static void
  1716. ath5k_create_power_curve(s16 pmin, s16 pmax,
  1717. const s16 *pwr, const u8 *vpd,
  1718. u8 num_points,
  1719. u8 *vpd_table, u8 type)
  1720. {
  1721. u8 idx[2] = { 0, 1 };
  1722. s16 pwr_i = 2*pmin;
  1723. int i;
  1724. if (num_points < 2)
  1725. return;
  1726. /* We want the whole line, so adjust boundaries
  1727. * to cover the entire power range. Note that
  1728. * power values are already 0.25dB so no need
  1729. * to multiply pwr_i by 2 */
  1730. if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
  1731. pwr_i = pmin;
  1732. pmin = 0;
  1733. pmax = 63;
  1734. }
  1735. /* Find surrounding turning points (TPs)
  1736. * and interpolate between them */
  1737. for (i = 0; (i <= (u16) (pmax - pmin)) &&
  1738. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  1739. /* We passed the right TP, move to the next set of TPs
  1740. * if we pass the last TP, extrapolate above using the last
  1741. * two TPs for ratio */
  1742. if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
  1743. idx[0]++;
  1744. idx[1]++;
  1745. }
  1746. vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
  1747. pwr[idx[0]], pwr[idx[1]],
  1748. vpd[idx[0]], vpd[idx[1]]);
  1749. /* Increase by 0.5dB
  1750. * (0.25 dB units) */
  1751. pwr_i += 2;
  1752. }
  1753. }
  1754. /*
  1755. * Get the surrounding per-channel power calibration piers
  1756. * for a given frequency so that we can interpolate between
  1757. * them and come up with an apropriate dataset for our current
  1758. * channel.
  1759. */
  1760. static void
  1761. ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
  1762. struct ieee80211_channel *channel,
  1763. struct ath5k_chan_pcal_info **pcinfo_l,
  1764. struct ath5k_chan_pcal_info **pcinfo_r)
  1765. {
  1766. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1767. struct ath5k_chan_pcal_info *pcinfo;
  1768. u8 idx_l, idx_r;
  1769. u8 mode, max, i;
  1770. u32 target = channel->center_freq;
  1771. idx_l = 0;
  1772. idx_r = 0;
  1773. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1774. pcinfo = ee->ee_pwr_cal_b;
  1775. mode = AR5K_EEPROM_MODE_11B;
  1776. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1777. pcinfo = ee->ee_pwr_cal_g;
  1778. mode = AR5K_EEPROM_MODE_11G;
  1779. } else {
  1780. pcinfo = ee->ee_pwr_cal_a;
  1781. mode = AR5K_EEPROM_MODE_11A;
  1782. }
  1783. max = ee->ee_n_piers[mode] - 1;
  1784. /* Frequency is below our calibrated
  1785. * range. Use the lowest power curve
  1786. * we have */
  1787. if (target < pcinfo[0].freq) {
  1788. idx_l = idx_r = 0;
  1789. goto done;
  1790. }
  1791. /* Frequency is above our calibrated
  1792. * range. Use the highest power curve
  1793. * we have */
  1794. if (target > pcinfo[max].freq) {
  1795. idx_l = idx_r = max;
  1796. goto done;
  1797. }
  1798. /* Frequency is inside our calibrated
  1799. * channel range. Pick the surrounding
  1800. * calibration piers so that we can
  1801. * interpolate */
  1802. for (i = 0; i <= max; i++) {
  1803. /* Frequency matches one of our calibration
  1804. * piers, no need to interpolate, just use
  1805. * that calibration pier */
  1806. if (pcinfo[i].freq == target) {
  1807. idx_l = idx_r = i;
  1808. goto done;
  1809. }
  1810. /* We found a calibration pier that's above
  1811. * frequency, use this pier and the previous
  1812. * one to interpolate */
  1813. if (target < pcinfo[i].freq) {
  1814. idx_r = i;
  1815. idx_l = idx_r - 1;
  1816. goto done;
  1817. }
  1818. }
  1819. done:
  1820. *pcinfo_l = &pcinfo[idx_l];
  1821. *pcinfo_r = &pcinfo[idx_r];
  1822. return;
  1823. }
  1824. /*
  1825. * Get the surrounding per-rate power calibration data
  1826. * for a given frequency and interpolate between power
  1827. * values to set max target power supported by hw for
  1828. * each rate.
  1829. */
  1830. static void
  1831. ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
  1832. struct ieee80211_channel *channel,
  1833. struct ath5k_rate_pcal_info *rates)
  1834. {
  1835. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1836. struct ath5k_rate_pcal_info *rpinfo;
  1837. u8 idx_l, idx_r;
  1838. u8 mode, max, i;
  1839. u32 target = channel->center_freq;
  1840. idx_l = 0;
  1841. idx_r = 0;
  1842. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1843. rpinfo = ee->ee_rate_tpwr_b;
  1844. mode = AR5K_EEPROM_MODE_11B;
  1845. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1846. rpinfo = ee->ee_rate_tpwr_g;
  1847. mode = AR5K_EEPROM_MODE_11G;
  1848. } else {
  1849. rpinfo = ee->ee_rate_tpwr_a;
  1850. mode = AR5K_EEPROM_MODE_11A;
  1851. }
  1852. max = ee->ee_rate_target_pwr_num[mode] - 1;
  1853. /* Get the surrounding calibration
  1854. * piers - same as above */
  1855. if (target < rpinfo[0].freq) {
  1856. idx_l = idx_r = 0;
  1857. goto done;
  1858. }
  1859. if (target > rpinfo[max].freq) {
  1860. idx_l = idx_r = max;
  1861. goto done;
  1862. }
  1863. for (i = 0; i <= max; i++) {
  1864. if (rpinfo[i].freq == target) {
  1865. idx_l = idx_r = i;
  1866. goto done;
  1867. }
  1868. if (target < rpinfo[i].freq) {
  1869. idx_r = i;
  1870. idx_l = idx_r - 1;
  1871. goto done;
  1872. }
  1873. }
  1874. done:
  1875. /* Now interpolate power value, based on the frequency */
  1876. rates->freq = target;
  1877. rates->target_power_6to24 =
  1878. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1879. rpinfo[idx_r].freq,
  1880. rpinfo[idx_l].target_power_6to24,
  1881. rpinfo[idx_r].target_power_6to24);
  1882. rates->target_power_36 =
  1883. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1884. rpinfo[idx_r].freq,
  1885. rpinfo[idx_l].target_power_36,
  1886. rpinfo[idx_r].target_power_36);
  1887. rates->target_power_48 =
  1888. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1889. rpinfo[idx_r].freq,
  1890. rpinfo[idx_l].target_power_48,
  1891. rpinfo[idx_r].target_power_48);
  1892. rates->target_power_54 =
  1893. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1894. rpinfo[idx_r].freq,
  1895. rpinfo[idx_l].target_power_54,
  1896. rpinfo[idx_r].target_power_54);
  1897. }
  1898. /*
  1899. * Get the max edge power for this channel if
  1900. * we have such data from EEPROM's Conformance Test
  1901. * Limits (CTL), and limit max power if needed.
  1902. */
  1903. static void
  1904. ath5k_get_max_ctl_power(struct ath5k_hw *ah,
  1905. struct ieee80211_channel *channel)
  1906. {
  1907. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  1908. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1909. struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
  1910. u8 *ctl_val = ee->ee_ctl;
  1911. s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
  1912. s16 edge_pwr = 0;
  1913. u8 rep_idx;
  1914. u8 i, ctl_mode;
  1915. u8 ctl_idx = 0xFF;
  1916. u32 target = channel->center_freq;
  1917. ctl_mode = ath_regd_get_band_ctl(regulatory, channel->band);
  1918. switch (channel->hw_value & CHANNEL_MODES) {
  1919. case CHANNEL_A:
  1920. ctl_mode |= AR5K_CTL_11A;
  1921. break;
  1922. case CHANNEL_G:
  1923. ctl_mode |= AR5K_CTL_11G;
  1924. break;
  1925. case CHANNEL_B:
  1926. ctl_mode |= AR5K_CTL_11B;
  1927. break;
  1928. case CHANNEL_T:
  1929. ctl_mode |= AR5K_CTL_TURBO;
  1930. break;
  1931. case CHANNEL_TG:
  1932. ctl_mode |= AR5K_CTL_TURBOG;
  1933. break;
  1934. case CHANNEL_XR:
  1935. /* Fall through */
  1936. default:
  1937. return;
  1938. }
  1939. for (i = 0; i < ee->ee_ctls; i++) {
  1940. if (ctl_val[i] == ctl_mode) {
  1941. ctl_idx = i;
  1942. break;
  1943. }
  1944. }
  1945. /* If we have a CTL dataset available grab it and find the
  1946. * edge power for our frequency */
  1947. if (ctl_idx == 0xFF)
  1948. return;
  1949. /* Edge powers are sorted by frequency from lower
  1950. * to higher. Each CTL corresponds to 8 edge power
  1951. * measurements. */
  1952. rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
  1953. /* Don't do boundaries check because we
  1954. * might have more that one bands defined
  1955. * for this mode */
  1956. /* Get the edge power that's closer to our
  1957. * frequency */
  1958. for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
  1959. rep_idx += i;
  1960. if (target <= rep[rep_idx].freq)
  1961. edge_pwr = (s16) rep[rep_idx].edge;
  1962. }
  1963. if (edge_pwr)
  1964. ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
  1965. }
  1966. /*
  1967. * Power to PCDAC table functions
  1968. */
  1969. /*
  1970. * Fill Power to PCDAC table on RF5111
  1971. *
  1972. * No further processing is needed for RF5111, the only thing we have to
  1973. * do is fill the values below and above calibration range since eeprom data
  1974. * may not cover the entire PCDAC table.
  1975. */
  1976. static void
  1977. ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
  1978. s16 *table_max)
  1979. {
  1980. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1981. u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
  1982. u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
  1983. s16 min_pwr, max_pwr;
  1984. /* Get table boundaries */
  1985. min_pwr = table_min[0];
  1986. pcdac_0 = pcdac_tmp[0];
  1987. max_pwr = table_max[0];
  1988. pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
  1989. /* Extrapolate below minimum using pcdac_0 */
  1990. pcdac_i = 0;
  1991. for (i = 0; i < min_pwr; i++)
  1992. pcdac_out[pcdac_i++] = pcdac_0;
  1993. /* Copy values from pcdac_tmp */
  1994. pwr_idx = min_pwr;
  1995. for (i = 0 ; pwr_idx <= max_pwr &&
  1996. pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
  1997. pcdac_out[pcdac_i++] = pcdac_tmp[i];
  1998. pwr_idx++;
  1999. }
  2000. /* Extrapolate above maximum */
  2001. while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
  2002. pcdac_out[pcdac_i++] = pcdac_n;
  2003. }
  2004. /*
  2005. * Combine available XPD Curves and fill Linear Power to PCDAC table
  2006. * on RF5112
  2007. *
  2008. * RFX112 can have up to 2 curves (one for low txpower range and one for
  2009. * higher txpower range). We need to put them both on pcdac_out and place
  2010. * them in the correct location. In case we only have one curve available
  2011. * just fit it on pcdac_out (it's supposed to cover the entire range of
  2012. * available pwr levels since it's always the higher power curve). Extrapolate
  2013. * below and above final table if needed.
  2014. */
  2015. static void
  2016. ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
  2017. s16 *table_max, u8 pdcurves)
  2018. {
  2019. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2020. u8 *pcdac_low_pwr;
  2021. u8 *pcdac_high_pwr;
  2022. u8 *pcdac_tmp;
  2023. u8 pwr;
  2024. s16 max_pwr_idx;
  2025. s16 min_pwr_idx;
  2026. s16 mid_pwr_idx = 0;
  2027. /* Edge flag turs on the 7nth bit on the PCDAC
  2028. * to delcare the higher power curve (force values
  2029. * to be greater than 64). If we only have one curve
  2030. * we don't need to set this, if we have 2 curves and
  2031. * fill the table backwards this can also be used to
  2032. * switch from higher power curve to lower power curve */
  2033. u8 edge_flag;
  2034. int i;
  2035. /* When we have only one curve available
  2036. * that's the higher power curve. If we have
  2037. * two curves the first is the high power curve
  2038. * and the next is the low power curve. */
  2039. if (pdcurves > 1) {
  2040. pcdac_low_pwr = ah->ah_txpower.tmpL[1];
  2041. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2042. mid_pwr_idx = table_max[1] - table_min[1] - 1;
  2043. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2044. /* If table size goes beyond 31.5dB, keep the
  2045. * upper 31.5dB range when setting tx power.
  2046. * Note: 126 = 31.5 dB in quarter dB steps */
  2047. if (table_max[0] - table_min[1] > 126)
  2048. min_pwr_idx = table_max[0] - 126;
  2049. else
  2050. min_pwr_idx = table_min[1];
  2051. /* Since we fill table backwards
  2052. * start from high power curve */
  2053. pcdac_tmp = pcdac_high_pwr;
  2054. edge_flag = 0x40;
  2055. #if 0
  2056. /* If both min and max power limits are in lower
  2057. * power curve's range, only use the low power curve.
  2058. * TODO: min/max levels are related to target
  2059. * power values requested from driver/user
  2060. * XXX: Is this really needed ? */
  2061. if (min_pwr < table_max[1] &&
  2062. max_pwr < table_max[1]) {
  2063. edge_flag = 0;
  2064. pcdac_tmp = pcdac_low_pwr;
  2065. max_pwr_idx = (table_max[1] - table_min[1])/2;
  2066. }
  2067. #endif
  2068. } else {
  2069. pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
  2070. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2071. min_pwr_idx = table_min[0];
  2072. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2073. pcdac_tmp = pcdac_high_pwr;
  2074. edge_flag = 0;
  2075. }
  2076. /* This is used when setting tx power*/
  2077. ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
  2078. /* Fill Power to PCDAC table backwards */
  2079. pwr = max_pwr_idx;
  2080. for (i = 63; i >= 0; i--) {
  2081. /* Entering lower power range, reset
  2082. * edge flag and set pcdac_tmp to lower
  2083. * power curve.*/
  2084. if (edge_flag == 0x40 &&
  2085. (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
  2086. edge_flag = 0x00;
  2087. pcdac_tmp = pcdac_low_pwr;
  2088. pwr = mid_pwr_idx/2;
  2089. }
  2090. /* Don't go below 1, extrapolate below if we have
  2091. * already swithced to the lower power curve -or
  2092. * we only have one curve and edge_flag is zero
  2093. * anyway */
  2094. if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
  2095. while (i >= 0) {
  2096. pcdac_out[i] = pcdac_out[i + 1];
  2097. i--;
  2098. }
  2099. break;
  2100. }
  2101. pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
  2102. /* Extrapolate above if pcdac is greater than
  2103. * 126 -this can happen because we OR pcdac_out
  2104. * value with edge_flag on high power curve */
  2105. if (pcdac_out[i] > 126)
  2106. pcdac_out[i] = 126;
  2107. /* Decrease by a 0.5dB step */
  2108. pwr--;
  2109. }
  2110. }
  2111. /* Write PCDAC values on hw */
  2112. static void
  2113. ath5k_setup_pcdac_table(struct ath5k_hw *ah)
  2114. {
  2115. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2116. int i;
  2117. /*
  2118. * Write TX power values
  2119. */
  2120. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2121. ath5k_hw_reg_write(ah,
  2122. (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
  2123. (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
  2124. AR5K_PHY_PCDAC_TXPOWER(i));
  2125. }
  2126. }
  2127. /*
  2128. * Power to PDADC table functions
  2129. */
  2130. /*
  2131. * Set the gain boundaries and create final Power to PDADC table
  2132. *
  2133. * We can have up to 4 pd curves, we need to do a simmilar process
  2134. * as we do for RF5112. This time we don't have an edge_flag but we
  2135. * set the gain boundaries on a separate register.
  2136. */
  2137. static void
  2138. ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
  2139. s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
  2140. {
  2141. u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
  2142. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2143. u8 *pdadc_tmp;
  2144. s16 pdadc_0;
  2145. u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
  2146. u8 pd_gain_overlap;
  2147. /* Note: Register value is initialized on initvals
  2148. * there is no feedback from hw.
  2149. * XXX: What about pd_gain_overlap from EEPROM ? */
  2150. pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
  2151. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
  2152. /* Create final PDADC table */
  2153. for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
  2154. pdadc_tmp = ah->ah_txpower.tmpL[pdg];
  2155. if (pdg == pdcurves - 1)
  2156. /* 2 dB boundary stretch for last
  2157. * (higher power) curve */
  2158. gain_boundaries[pdg] = pwr_max[pdg] + 4;
  2159. else
  2160. /* Set gain boundary in the middle
  2161. * between this curve and the next one */
  2162. gain_boundaries[pdg] =
  2163. (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
  2164. /* Sanity check in case our 2 db stretch got out of
  2165. * range. */
  2166. if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
  2167. gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
  2168. /* For the first curve (lower power)
  2169. * start from 0 dB */
  2170. if (pdg == 0)
  2171. pdadc_0 = 0;
  2172. else
  2173. /* For the other curves use the gain overlap */
  2174. pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
  2175. pd_gain_overlap;
  2176. /* Force each power step to be at least 0.5 dB */
  2177. if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
  2178. pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
  2179. else
  2180. pwr_step = 1;
  2181. /* If pdadc_0 is negative, we need to extrapolate
  2182. * below this pdgain by a number of pwr_steps */
  2183. while ((pdadc_0 < 0) && (pdadc_i < 128)) {
  2184. s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
  2185. pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
  2186. pdadc_0++;
  2187. }
  2188. /* Set last pwr level, using gain boundaries */
  2189. pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
  2190. /* Limit it to be inside pwr range */
  2191. table_size = pwr_max[pdg] - pwr_min[pdg];
  2192. max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
  2193. /* Fill pdadc_out table */
  2194. while (pdadc_0 < max_idx)
  2195. pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
  2196. /* Need to extrapolate above this pdgain? */
  2197. if (pdadc_n <= max_idx)
  2198. continue;
  2199. /* Force each power step to be at least 0.5 dB */
  2200. if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
  2201. pwr_step = pdadc_tmp[table_size - 1] -
  2202. pdadc_tmp[table_size - 2];
  2203. else
  2204. pwr_step = 1;
  2205. /* Extrapolate above */
  2206. while ((pdadc_0 < (s16) pdadc_n) &&
  2207. (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
  2208. s16 tmp = pdadc_tmp[table_size - 1] +
  2209. (pdadc_0 - max_idx) * pwr_step;
  2210. pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
  2211. pdadc_0++;
  2212. }
  2213. }
  2214. while (pdg < AR5K_EEPROM_N_PD_GAINS) {
  2215. gain_boundaries[pdg] = gain_boundaries[pdg - 1];
  2216. pdg++;
  2217. }
  2218. while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
  2219. pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
  2220. pdadc_i++;
  2221. }
  2222. /* Set gain boundaries */
  2223. ath5k_hw_reg_write(ah,
  2224. AR5K_REG_SM(pd_gain_overlap,
  2225. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
  2226. AR5K_REG_SM(gain_boundaries[0],
  2227. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
  2228. AR5K_REG_SM(gain_boundaries[1],
  2229. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
  2230. AR5K_REG_SM(gain_boundaries[2],
  2231. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
  2232. AR5K_REG_SM(gain_boundaries[3],
  2233. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
  2234. AR5K_PHY_TPC_RG5);
  2235. /* Used for setting rate power table */
  2236. ah->ah_txpower.txp_min_idx = pwr_min[0];
  2237. }
  2238. /* Write PDADC values on hw */
  2239. static void
  2240. ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
  2241. u8 pdcurves, u8 *pdg_to_idx)
  2242. {
  2243. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2244. u32 reg;
  2245. u8 i;
  2246. /* Select the right pdgain curves */
  2247. /* Clear current settings */
  2248. reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
  2249. reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
  2250. AR5K_PHY_TPC_RG1_PDGAIN_2 |
  2251. AR5K_PHY_TPC_RG1_PDGAIN_3 |
  2252. AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2253. /*
  2254. * Use pd_gains curve from eeprom
  2255. *
  2256. * This overrides the default setting from initvals
  2257. * in case some vendors (e.g. Zcomax) don't use the default
  2258. * curves. If we don't honor their settings we 'll get a
  2259. * 5dB (1 * gain overlap ?) drop.
  2260. */
  2261. reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2262. switch (pdcurves) {
  2263. case 3:
  2264. reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
  2265. /* Fall through */
  2266. case 2:
  2267. reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
  2268. /* Fall through */
  2269. case 1:
  2270. reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
  2271. break;
  2272. }
  2273. ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
  2274. /*
  2275. * Write TX power values
  2276. */
  2277. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2278. ath5k_hw_reg_write(ah,
  2279. ((pdadc_out[4*i + 0] & 0xff) << 0) |
  2280. ((pdadc_out[4*i + 1] & 0xff) << 8) |
  2281. ((pdadc_out[4*i + 2] & 0xff) << 16) |
  2282. ((pdadc_out[4*i + 3] & 0xff) << 24),
  2283. AR5K_PHY_PDADC_TXPOWER(i));
  2284. }
  2285. }
  2286. /*
  2287. * Common code for PCDAC/PDADC tables
  2288. */
  2289. /*
  2290. * This is the main function that uses all of the above
  2291. * to set PCDAC/PDADC table on hw for the current channel.
  2292. * This table is used for tx power calibration on the basband,
  2293. * without it we get weird tx power levels and in some cases
  2294. * distorted spectral mask
  2295. */
  2296. static int
  2297. ath5k_setup_channel_powertable(struct ath5k_hw *ah,
  2298. struct ieee80211_channel *channel,
  2299. u8 ee_mode, u8 type)
  2300. {
  2301. struct ath5k_pdgain_info *pdg_L, *pdg_R;
  2302. struct ath5k_chan_pcal_info *pcinfo_L;
  2303. struct ath5k_chan_pcal_info *pcinfo_R;
  2304. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2305. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  2306. s16 table_min[AR5K_EEPROM_N_PD_GAINS];
  2307. s16 table_max[AR5K_EEPROM_N_PD_GAINS];
  2308. u8 *tmpL;
  2309. u8 *tmpR;
  2310. u32 target = channel->center_freq;
  2311. int pdg, i;
  2312. /* Get surounding freq piers for this channel */
  2313. ath5k_get_chan_pcal_surrounding_piers(ah, channel,
  2314. &pcinfo_L,
  2315. &pcinfo_R);
  2316. /* Loop over pd gain curves on
  2317. * surounding freq piers by index */
  2318. for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
  2319. /* Fill curves in reverse order
  2320. * from lower power (max gain)
  2321. * to higher power. Use curve -> idx
  2322. * backmapping we did on eeprom init */
  2323. u8 idx = pdg_curve_to_idx[pdg];
  2324. /* Grab the needed curves by index */
  2325. pdg_L = &pcinfo_L->pd_curves[idx];
  2326. pdg_R = &pcinfo_R->pd_curves[idx];
  2327. /* Initialize the temp tables */
  2328. tmpL = ah->ah_txpower.tmpL[pdg];
  2329. tmpR = ah->ah_txpower.tmpR[pdg];
  2330. /* Set curve's x boundaries and create
  2331. * curves so that they cover the same
  2332. * range (if we don't do that one table
  2333. * will have values on some range and the
  2334. * other one won't have any so interpolation
  2335. * will fail) */
  2336. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2337. pdg_R->pd_pwr[0]) / 2;
  2338. table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2339. pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
  2340. /* Now create the curves on surrounding channels
  2341. * and interpolate if needed to get the final
  2342. * curve for this gain on this channel */
  2343. switch (type) {
  2344. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2345. /* Override min/max so that we don't loose
  2346. * accuracy (don't divide by 2) */
  2347. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2348. pdg_R->pd_pwr[0]);
  2349. table_max[pdg] =
  2350. max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2351. pdg_R->pd_pwr[pdg_R->pd_points - 1]);
  2352. /* Override minimum so that we don't get
  2353. * out of bounds while extrapolating
  2354. * below. Don't do this when we have 2
  2355. * curves and we are on the high power curve
  2356. * because table_min is ok in this case */
  2357. if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
  2358. table_min[pdg] =
  2359. ath5k_get_linear_pcdac_min(pdg_L->pd_step,
  2360. pdg_R->pd_step,
  2361. pdg_L->pd_pwr,
  2362. pdg_R->pd_pwr);
  2363. /* Don't go too low because we will
  2364. * miss the upper part of the curve.
  2365. * Note: 126 = 31.5dB (max power supported)
  2366. * in 0.25dB units */
  2367. if (table_max[pdg] - table_min[pdg] > 126)
  2368. table_min[pdg] = table_max[pdg] - 126;
  2369. }
  2370. /* Fall through */
  2371. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2372. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2373. ath5k_create_power_curve(table_min[pdg],
  2374. table_max[pdg],
  2375. pdg_L->pd_pwr,
  2376. pdg_L->pd_step,
  2377. pdg_L->pd_points, tmpL, type);
  2378. /* We are in a calibration
  2379. * pier, no need to interpolate
  2380. * between freq piers */
  2381. if (pcinfo_L == pcinfo_R)
  2382. continue;
  2383. ath5k_create_power_curve(table_min[pdg],
  2384. table_max[pdg],
  2385. pdg_R->pd_pwr,
  2386. pdg_R->pd_step,
  2387. pdg_R->pd_points, tmpR, type);
  2388. break;
  2389. default:
  2390. return -EINVAL;
  2391. }
  2392. /* Interpolate between curves
  2393. * of surounding freq piers to
  2394. * get the final curve for this
  2395. * pd gain. Re-use tmpL for interpolation
  2396. * output */
  2397. for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
  2398. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  2399. tmpL[i] = (u8) ath5k_get_interpolated_value(target,
  2400. (s16) pcinfo_L->freq,
  2401. (s16) pcinfo_R->freq,
  2402. (s16) tmpL[i],
  2403. (s16) tmpR[i]);
  2404. }
  2405. }
  2406. /* Now we have a set of curves for this
  2407. * channel on tmpL (x range is table_max - table_min
  2408. * and y values are tmpL[pdg][]) sorted in the same
  2409. * order as EEPROM (because we've used the backmapping).
  2410. * So for RF5112 it's from higher power to lower power
  2411. * and for RF2413 it's from lower power to higher power.
  2412. * For RF5111 we only have one curve. */
  2413. /* Fill min and max power levels for this
  2414. * channel by interpolating the values on
  2415. * surounding channels to complete the dataset */
  2416. ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
  2417. (s16) pcinfo_L->freq,
  2418. (s16) pcinfo_R->freq,
  2419. pcinfo_L->min_pwr, pcinfo_R->min_pwr);
  2420. ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
  2421. (s16) pcinfo_L->freq,
  2422. (s16) pcinfo_R->freq,
  2423. pcinfo_L->max_pwr, pcinfo_R->max_pwr);
  2424. /* We are ready to go, fill PCDAC/PDADC
  2425. * table and write settings on hardware */
  2426. switch (type) {
  2427. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2428. /* For RF5112 we can have one or two curves
  2429. * and each curve covers a certain power lvl
  2430. * range so we need to do some more processing */
  2431. ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
  2432. ee->ee_pd_gains[ee_mode]);
  2433. /* Set txp.offset so that we can
  2434. * match max power value with max
  2435. * table index */
  2436. ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
  2437. /* Write settings on hw */
  2438. ath5k_setup_pcdac_table(ah);
  2439. break;
  2440. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2441. /* We are done for RF5111 since it has only
  2442. * one curve, just fit the curve on the table */
  2443. ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
  2444. /* No rate powertable adjustment for RF5111 */
  2445. ah->ah_txpower.txp_min_idx = 0;
  2446. ah->ah_txpower.txp_offset = 0;
  2447. /* Write settings on hw */
  2448. ath5k_setup_pcdac_table(ah);
  2449. break;
  2450. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2451. /* Set PDADC boundaries and fill
  2452. * final PDADC table */
  2453. ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
  2454. ee->ee_pd_gains[ee_mode]);
  2455. /* Write settings on hw */
  2456. ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
  2457. /* Set txp.offset, note that table_min
  2458. * can be negative */
  2459. ah->ah_txpower.txp_offset = table_min[0];
  2460. break;
  2461. default:
  2462. return -EINVAL;
  2463. }
  2464. return 0;
  2465. }
  2466. /*
  2467. * Per-rate tx power setting
  2468. *
  2469. * This is the code that sets the desired tx power (below
  2470. * maximum) on hw for each rate (we also have TPC that sets
  2471. * power per packet). We do that by providing an index on the
  2472. * PCDAC/PDADC table we set up.
  2473. */
  2474. /*
  2475. * Set rate power table
  2476. *
  2477. * For now we only limit txpower based on maximum tx power
  2478. * supported by hw (what's inside rate_info). We need to limit
  2479. * this even more, based on regulatory domain etc.
  2480. *
  2481. * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
  2482. * and is indexed as follows:
  2483. * rates[0] - rates[7] -> OFDM rates
  2484. * rates[8] - rates[14] -> CCK rates
  2485. * rates[15] -> XR rates (they all have the same power)
  2486. */
  2487. static void
  2488. ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
  2489. struct ath5k_rate_pcal_info *rate_info,
  2490. u8 ee_mode)
  2491. {
  2492. unsigned int i;
  2493. u16 *rates;
  2494. /* max_pwr is power level we got from driver/user in 0.5dB
  2495. * units, switch to 0.25dB units so we can compare */
  2496. max_pwr *= 2;
  2497. max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
  2498. /* apply rate limits */
  2499. rates = ah->ah_txpower.txp_rates_power_table;
  2500. /* OFDM rates 6 to 24Mb/s */
  2501. for (i = 0; i < 5; i++)
  2502. rates[i] = min(max_pwr, rate_info->target_power_6to24);
  2503. /* Rest OFDM rates */
  2504. rates[5] = min(rates[0], rate_info->target_power_36);
  2505. rates[6] = min(rates[0], rate_info->target_power_48);
  2506. rates[7] = min(rates[0], rate_info->target_power_54);
  2507. /* CCK rates */
  2508. /* 1L */
  2509. rates[8] = min(rates[0], rate_info->target_power_6to24);
  2510. /* 2L */
  2511. rates[9] = min(rates[0], rate_info->target_power_36);
  2512. /* 2S */
  2513. rates[10] = min(rates[0], rate_info->target_power_36);
  2514. /* 5L */
  2515. rates[11] = min(rates[0], rate_info->target_power_48);
  2516. /* 5S */
  2517. rates[12] = min(rates[0], rate_info->target_power_48);
  2518. /* 11L */
  2519. rates[13] = min(rates[0], rate_info->target_power_54);
  2520. /* 11S */
  2521. rates[14] = min(rates[0], rate_info->target_power_54);
  2522. /* XR rates */
  2523. rates[15] = min(rates[0], rate_info->target_power_6to24);
  2524. /* CCK rates have different peak to average ratio
  2525. * so we have to tweak their power so that gainf
  2526. * correction works ok. For this we use OFDM to
  2527. * CCK delta from eeprom */
  2528. if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
  2529. (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
  2530. for (i = 8; i <= 15; i++)
  2531. rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
  2532. /* Now that we have all rates setup use table offset to
  2533. * match the power range set by user with the power indices
  2534. * on PCDAC/PDADC table */
  2535. for (i = 0; i < 16; i++) {
  2536. rates[i] += ah->ah_txpower.txp_offset;
  2537. /* Don't get out of bounds */
  2538. if (rates[i] > 63)
  2539. rates[i] = 63;
  2540. }
  2541. /* Min/max in 0.25dB units */
  2542. ah->ah_txpower.txp_min_pwr = 2 * rates[7];
  2543. ah->ah_txpower.txp_max_pwr = 2 * rates[0];
  2544. ah->ah_txpower.txp_ofdm = rates[7];
  2545. }
  2546. /*
  2547. * Set transmition power
  2548. */
  2549. int
  2550. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  2551. u8 ee_mode, u8 txpower)
  2552. {
  2553. struct ath5k_rate_pcal_info rate_info;
  2554. u8 type;
  2555. int ret;
  2556. ATH5K_TRACE(ah->ah_sc);
  2557. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  2558. ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
  2559. return -EINVAL;
  2560. }
  2561. /* Reset TX power values */
  2562. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  2563. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  2564. ah->ah_txpower.txp_min_pwr = 0;
  2565. ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
  2566. /* Initialize TX power table */
  2567. switch (ah->ah_radio) {
  2568. case AR5K_RF5111:
  2569. type = AR5K_PWRTABLE_PWR_TO_PCDAC;
  2570. break;
  2571. case AR5K_RF5112:
  2572. type = AR5K_PWRTABLE_LINEAR_PCDAC;
  2573. break;
  2574. case AR5K_RF2413:
  2575. case AR5K_RF5413:
  2576. case AR5K_RF2316:
  2577. case AR5K_RF2317:
  2578. case AR5K_RF2425:
  2579. type = AR5K_PWRTABLE_PWR_TO_PDADC;
  2580. break;
  2581. default:
  2582. return -EINVAL;
  2583. }
  2584. /* FIXME: Only on channel/mode change */
  2585. ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
  2586. if (ret)
  2587. return ret;
  2588. /* Limit max power if we have a CTL available */
  2589. ath5k_get_max_ctl_power(ah, channel);
  2590. /* FIXME: Tx power limit for this regdomain
  2591. * XXX: Mac80211/CRDA will do that anyway ? */
  2592. /* FIXME: Antenna reduction stuff */
  2593. /* FIXME: Limit power on turbo modes */
  2594. /* FIXME: TPC scale reduction */
  2595. /* Get surounding channels for per-rate power table
  2596. * calibration */
  2597. ath5k_get_rate_pcal_data(ah, channel, &rate_info);
  2598. /* Setup rate power table */
  2599. ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
  2600. /* Write rate power table on hw */
  2601. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  2602. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  2603. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  2604. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  2605. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  2606. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  2607. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  2608. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  2609. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  2610. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  2611. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  2612. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  2613. /* FIXME: TPC support */
  2614. if (ah->ah_txpower.txp_tpc) {
  2615. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  2616. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2617. ath5k_hw_reg_write(ah,
  2618. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
  2619. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
  2620. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
  2621. AR5K_TPC);
  2622. } else {
  2623. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
  2624. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2625. }
  2626. return 0;
  2627. }
  2628. int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)
  2629. {
  2630. /*Just a try M.F.*/
  2631. struct ieee80211_channel *channel = ah->ah_current_channel;
  2632. u8 ee_mode;
  2633. ATH5K_TRACE(ah->ah_sc);
  2634. switch (channel->hw_value & CHANNEL_MODES) {
  2635. case CHANNEL_A:
  2636. case CHANNEL_T:
  2637. case CHANNEL_XR:
  2638. ee_mode = AR5K_EEPROM_MODE_11A;
  2639. break;
  2640. case CHANNEL_G:
  2641. case CHANNEL_TG:
  2642. ee_mode = AR5K_EEPROM_MODE_11G;
  2643. break;
  2644. case CHANNEL_B:
  2645. ee_mode = AR5K_EEPROM_MODE_11B;
  2646. break;
  2647. default:
  2648. ATH5K_ERR(ah->ah_sc,
  2649. "invalid channel: %d\n", channel->center_freq);
  2650. return -EINVAL;
  2651. }
  2652. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
  2653. "changing txpower to %d\n", txpower);
  2654. return ath5k_hw_txpower(ah, channel, ee_mode, txpower);
  2655. }