phy.c 108 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962
  1. /*
  2. * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  5. * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. *
  19. */
  20. /***********************\
  21. * PHY related functions *
  22. \***********************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <asm/unaligned.h>
  27. #include "ath5k.h"
  28. #include "reg.h"
  29. #include "rfbuffer.h"
  30. #include "rfgain.h"
  31. #include "../regd.h"
  32. /**
  33. * DOC: PHY related functions
  34. *
  35. * Here we handle the low-level functions related to baseband
  36. * and analog frontend (RF) parts. This is by far the most complex
  37. * part of the hw code so make sure you know what you are doing.
  38. *
  39. * Here is a list of what this is all about:
  40. *
  41. * - Channel setting/switching
  42. *
  43. * - Automatic Gain Control (AGC) calibration
  44. *
  45. * - Noise Floor calibration
  46. *
  47. * - I/Q imbalance calibration (QAM correction)
  48. *
  49. * - Calibration due to thermal changes (gain_F)
  50. *
  51. * - Spur noise mitigation
  52. *
  53. * - RF/PHY initialization for the various operating modes and bwmodes
  54. *
  55. * - Antenna control
  56. *
  57. * - TX power control per channel/rate/packet type
  58. *
  59. * Also have in mind we never got documentation for most of these
  60. * functions, what we have comes mostly from Atheros's code, reverse
  61. * engineering and patent docs/presentations etc.
  62. */
  63. /******************\
  64. * Helper functions *
  65. \******************/
  66. /**
  67. * ath5k_hw_radio_revision() - Get the PHY Chip revision
  68. * @ah: The &struct ath5k_hw
  69. * @band: One of enum nl80211_band
  70. *
  71. * Returns the revision number of a 2GHz, 5GHz or single chip
  72. * radio.
  73. */
  74. u16
  75. ath5k_hw_radio_revision(struct ath5k_hw *ah, enum nl80211_band band)
  76. {
  77. unsigned int i;
  78. u32 srev;
  79. u16 ret;
  80. /*
  81. * Set the radio chip access register
  82. */
  83. switch (band) {
  84. case NL80211_BAND_2GHZ:
  85. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  86. break;
  87. case NL80211_BAND_5GHZ:
  88. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  89. break;
  90. default:
  91. return 0;
  92. }
  93. usleep_range(2000, 2500);
  94. /* ...wait until PHY is ready and read the selected radio revision */
  95. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  96. for (i = 0; i < 8; i++)
  97. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  98. if (ah->ah_version == AR5K_AR5210) {
  99. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(256)) >> 28) & 0xf;
  100. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  101. } else {
  102. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  103. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  104. ((srev & 0x0f) << 4), 8);
  105. }
  106. /* Reset to the 5GHz mode */
  107. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  108. return ret;
  109. }
  110. /**
  111. * ath5k_channel_ok() - Check if a channel is supported by the hw
  112. * @ah: The &struct ath5k_hw
  113. * @channel: The &struct ieee80211_channel
  114. *
  115. * Note: We don't do any regulatory domain checks here, it's just
  116. * a sanity check.
  117. */
  118. bool
  119. ath5k_channel_ok(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  120. {
  121. u16 freq = channel->center_freq;
  122. /* Check if the channel is in our supported range */
  123. if (channel->band == NL80211_BAND_2GHZ) {
  124. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  125. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  126. return true;
  127. } else if (channel->band == NL80211_BAND_5GHZ)
  128. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  129. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  130. return true;
  131. return false;
  132. }
  133. /**
  134. * ath5k_hw_chan_has_spur_noise() - Check if channel is sensitive to spur noise
  135. * @ah: The &struct ath5k_hw
  136. * @channel: The &struct ieee80211_channel
  137. */
  138. bool
  139. ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
  140. struct ieee80211_channel *channel)
  141. {
  142. u8 refclk_freq;
  143. if ((ah->ah_radio == AR5K_RF5112) ||
  144. (ah->ah_radio == AR5K_RF5413) ||
  145. (ah->ah_radio == AR5K_RF2413) ||
  146. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  147. refclk_freq = 40;
  148. else
  149. refclk_freq = 32;
  150. if ((channel->center_freq % refclk_freq != 0) &&
  151. ((channel->center_freq % refclk_freq < 10) ||
  152. (channel->center_freq % refclk_freq > 22)))
  153. return true;
  154. else
  155. return false;
  156. }
  157. /**
  158. * ath5k_hw_rfb_op() - Perform an operation on the given RF Buffer
  159. * @ah: The &struct ath5k_hw
  160. * @rf_regs: The struct ath5k_rf_reg
  161. * @val: New value
  162. * @reg_id: RF register ID
  163. * @set: Indicate we need to swap data
  164. *
  165. * This is an internal function used to modify RF Banks before
  166. * writing them to AR5K_RF_BUFFER. Check out rfbuffer.h for more
  167. * infos.
  168. */
  169. static unsigned int
  170. ath5k_hw_rfb_op(struct ath5k_hw *ah, const struct ath5k_rf_reg *rf_regs,
  171. u32 val, u8 reg_id, bool set)
  172. {
  173. const struct ath5k_rf_reg *rfreg = NULL;
  174. u8 offset, bank, num_bits, col, position;
  175. u16 entry;
  176. u32 mask, data, last_bit, bits_shifted, first_bit;
  177. u32 *rfb;
  178. s32 bits_left;
  179. int i;
  180. data = 0;
  181. rfb = ah->ah_rf_banks;
  182. for (i = 0; i < ah->ah_rf_regs_count; i++) {
  183. if (rf_regs[i].index == reg_id) {
  184. rfreg = &rf_regs[i];
  185. break;
  186. }
  187. }
  188. if (rfb == NULL || rfreg == NULL) {
  189. ATH5K_PRINTF("Rf register not found!\n");
  190. /* should not happen */
  191. return 0;
  192. }
  193. bank = rfreg->bank;
  194. num_bits = rfreg->field.len;
  195. first_bit = rfreg->field.pos;
  196. col = rfreg->field.col;
  197. /* first_bit is an offset from bank's
  198. * start. Since we have all banks on
  199. * the same array, we use this offset
  200. * to mark each bank's start */
  201. offset = ah->ah_offset[bank];
  202. /* Boundary check */
  203. if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
  204. ATH5K_PRINTF("invalid values at offset %u\n", offset);
  205. return 0;
  206. }
  207. entry = ((first_bit - 1) / 8) + offset;
  208. position = (first_bit - 1) % 8;
  209. if (set)
  210. data = ath5k_hw_bitswap(val, num_bits);
  211. for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
  212. position = 0, entry++) {
  213. last_bit = (position + bits_left > 8) ? 8 :
  214. position + bits_left;
  215. mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
  216. (col * 8);
  217. if (set) {
  218. rfb[entry] &= ~mask;
  219. rfb[entry] |= ((data << position) << (col * 8)) & mask;
  220. data >>= (8 - position);
  221. } else {
  222. data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
  223. << bits_shifted;
  224. bits_shifted += last_bit - position;
  225. }
  226. bits_left -= 8 - position;
  227. }
  228. data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
  229. return data;
  230. }
  231. /**
  232. * ath5k_hw_write_ofdm_timings() - set OFDM timings on AR5212
  233. * @ah: the &struct ath5k_hw
  234. * @channel: the currently set channel upon reset
  235. *
  236. * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
  237. * operation on the AR5212 upon reset. This is a helper for ath5k_hw_phy_init.
  238. *
  239. * Since delta slope is floating point we split it on its exponent and
  240. * mantissa and provide these values on hw.
  241. *
  242. * For more infos i think this patent is related
  243. * "http://www.freepatentsonline.com/7184495.html"
  244. */
  245. static inline int
  246. ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  247. struct ieee80211_channel *channel)
  248. {
  249. /* Get exponent and mantissa and set it */
  250. u32 coef_scaled, coef_exp, coef_man,
  251. ds_coef_exp, ds_coef_man, clock;
  252. BUG_ON(!(ah->ah_version == AR5K_AR5212) ||
  253. (channel->hw_value == AR5K_MODE_11B));
  254. /* Get coefficient
  255. * ALGO: coef = (5 * clock / carrier_freq) / 2
  256. * we scale coef by shifting clock value by 24 for
  257. * better precision since we use integers */
  258. switch (ah->ah_bwmode) {
  259. case AR5K_BWMODE_40MHZ:
  260. clock = 40 * 2;
  261. break;
  262. case AR5K_BWMODE_10MHZ:
  263. clock = 40 / 2;
  264. break;
  265. case AR5K_BWMODE_5MHZ:
  266. clock = 40 / 4;
  267. break;
  268. default:
  269. clock = 40;
  270. break;
  271. }
  272. coef_scaled = ((5 * (clock << 24)) / 2) / channel->center_freq;
  273. /* Get exponent
  274. * ALGO: coef_exp = 14 - highest set bit position */
  275. coef_exp = ilog2(coef_scaled);
  276. /* Doesn't make sense if it's zero*/
  277. if (!coef_scaled || !coef_exp)
  278. return -EINVAL;
  279. /* Note: we've shifted coef_scaled by 24 */
  280. coef_exp = 14 - (coef_exp - 24);
  281. /* Get mantissa (significant digits)
  282. * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */
  283. coef_man = coef_scaled +
  284. (1 << (24 - coef_exp - 1));
  285. /* Calculate delta slope coefficient exponent
  286. * and mantissa (remove scaling) and set them on hw */
  287. ds_coef_man = coef_man >> (24 - coef_exp);
  288. ds_coef_exp = coef_exp - 16;
  289. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  290. AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
  291. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  292. AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
  293. return 0;
  294. }
  295. /**
  296. * ath5k_hw_phy_disable() - Disable PHY
  297. * @ah: The &struct ath5k_hw
  298. */
  299. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  300. {
  301. /*Just a try M.F.*/
  302. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  303. return 0;
  304. }
  305. /**
  306. * ath5k_hw_wait_for_synth() - Wait for synth to settle
  307. * @ah: The &struct ath5k_hw
  308. * @channel: The &struct ieee80211_channel
  309. */
  310. static void
  311. ath5k_hw_wait_for_synth(struct ath5k_hw *ah,
  312. struct ieee80211_channel *channel)
  313. {
  314. /*
  315. * On 5211+ read activation -> rx delay
  316. * and use it (100ns steps).
  317. */
  318. if (ah->ah_version != AR5K_AR5210) {
  319. u32 delay;
  320. delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  321. AR5K_PHY_RX_DELAY_M;
  322. delay = (channel->hw_value == AR5K_MODE_11B) ?
  323. ((delay << 2) / 22) : (delay / 10);
  324. if (ah->ah_bwmode == AR5K_BWMODE_10MHZ)
  325. delay = delay << 1;
  326. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ)
  327. delay = delay << 2;
  328. /* XXX: /2 on turbo ? Let's be safe
  329. * for now */
  330. usleep_range(100 + delay, 100 + (2 * delay));
  331. } else {
  332. usleep_range(1000, 1500);
  333. }
  334. }
  335. /**********************\
  336. * RF Gain optimization *
  337. \**********************/
  338. /**
  339. * DOC: RF Gain optimization
  340. *
  341. * This code is used to optimize RF gain on different environments
  342. * (temperature mostly) based on feedback from a power detector.
  343. *
  344. * It's only used on RF5111 and RF5112, later RF chips seem to have
  345. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  346. * no gain optimization ladder-.
  347. *
  348. * For more infos check out this patent doc
  349. * "http://www.freepatentsonline.com/7400691.html"
  350. *
  351. * This paper describes power drops as seen on the receiver due to
  352. * probe packets
  353. * "http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  354. * %20of%20Power%20Control.pdf"
  355. *
  356. * And this is the MadWiFi bug entry related to the above
  357. * "http://madwifi-project.org/ticket/1659"
  358. * with various measurements and diagrams
  359. */
  360. /**
  361. * ath5k_hw_rfgain_opt_init() - Initialize ah_gain during attach
  362. * @ah: The &struct ath5k_hw
  363. */
  364. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  365. {
  366. /* Initialize the gain optimization values */
  367. switch (ah->ah_radio) {
  368. case AR5K_RF5111:
  369. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  370. ah->ah_gain.g_low = 20;
  371. ah->ah_gain.g_high = 35;
  372. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  373. break;
  374. case AR5K_RF5112:
  375. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  376. ah->ah_gain.g_low = 20;
  377. ah->ah_gain.g_high = 85;
  378. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  379. break;
  380. default:
  381. return -EINVAL;
  382. }
  383. return 0;
  384. }
  385. /**
  386. * ath5k_hw_request_rfgain_probe() - Request a PAPD probe packet
  387. * @ah: The &struct ath5k_hw
  388. *
  389. * Schedules a gain probe check on the next transmitted packet.
  390. * That means our next packet is going to be sent with lower
  391. * tx power and a Peak to Average Power Detector (PAPD) will try
  392. * to measure the gain.
  393. *
  394. * TODO: Force a tx packet (bypassing PCU arbitrator etc)
  395. * just after we enable the probe so that we don't mess with
  396. * standard traffic.
  397. */
  398. static void
  399. ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  400. {
  401. /* Skip if gain calibration is inactive or
  402. * we already handle a probe request */
  403. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  404. return;
  405. /* Send the packet with 2dB below max power as
  406. * patent doc suggest */
  407. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_ofdm - 4,
  408. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  409. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  410. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  411. }
  412. /**
  413. * ath5k_hw_rf_gainf_corr() - Calculate Gain_F measurement correction
  414. * @ah: The &struct ath5k_hw
  415. *
  416. * Calculate Gain_F measurement correction
  417. * based on the current step for RF5112 rev. 2
  418. */
  419. static u32
  420. ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  421. {
  422. u32 mix, step;
  423. const struct ath5k_gain_opt *go;
  424. const struct ath5k_gain_opt_step *g_step;
  425. const struct ath5k_rf_reg *rf_regs;
  426. /* Only RF5112 Rev. 2 supports it */
  427. if ((ah->ah_radio != AR5K_RF5112) ||
  428. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  429. return 0;
  430. go = &rfgain_opt_5112;
  431. rf_regs = rf_regs_5112a;
  432. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  433. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  434. if (ah->ah_rf_banks == NULL)
  435. return 0;
  436. ah->ah_gain.g_f_corr = 0;
  437. /* No VGA (Variable Gain Amplifier) override, skip */
  438. if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
  439. return 0;
  440. /* Mix gain stepping */
  441. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
  442. /* Mix gain override */
  443. mix = g_step->gos_param[0];
  444. switch (mix) {
  445. case 3:
  446. ah->ah_gain.g_f_corr = step * 2;
  447. break;
  448. case 2:
  449. ah->ah_gain.g_f_corr = (step - 5) * 2;
  450. break;
  451. case 1:
  452. ah->ah_gain.g_f_corr = step;
  453. break;
  454. default:
  455. ah->ah_gain.g_f_corr = 0;
  456. break;
  457. }
  458. return ah->ah_gain.g_f_corr;
  459. }
  460. /**
  461. * ath5k_hw_rf_check_gainf_readback() - Validate Gain_F feedback from detector
  462. * @ah: The &struct ath5k_hw
  463. *
  464. * Check if current gain_F measurement is in the range of our
  465. * power detector windows. If we get a measurement outside range
  466. * we know it's not accurate (detectors can't measure anything outside
  467. * their detection window) so we must ignore it.
  468. *
  469. * Returns true if readback was O.K. or false on failure
  470. */
  471. static bool
  472. ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  473. {
  474. const struct ath5k_rf_reg *rf_regs;
  475. u32 step, mix_ovr, level[4];
  476. if (ah->ah_rf_banks == NULL)
  477. return false;
  478. if (ah->ah_radio == AR5K_RF5111) {
  479. rf_regs = rf_regs_5111;
  480. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  481. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
  482. false);
  483. level[0] = 0;
  484. level[1] = (step == 63) ? 50 : step + 4;
  485. level[2] = (step != 63) ? 64 : level[0];
  486. level[3] = level[2] + 50;
  487. ah->ah_gain.g_high = level[3] -
  488. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  489. ah->ah_gain.g_low = level[0] +
  490. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  491. } else {
  492. rf_regs = rf_regs_5112;
  493. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  494. mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
  495. false);
  496. level[0] = level[2] = 0;
  497. if (mix_ovr == 1) {
  498. level[1] = level[3] = 83;
  499. } else {
  500. level[1] = level[3] = 107;
  501. ah->ah_gain.g_high = 55;
  502. }
  503. }
  504. return (ah->ah_gain.g_current >= level[0] &&
  505. ah->ah_gain.g_current <= level[1]) ||
  506. (ah->ah_gain.g_current >= level[2] &&
  507. ah->ah_gain.g_current <= level[3]);
  508. }
  509. /**
  510. * ath5k_hw_rf_gainf_adjust() - Perform Gain_F adjustment
  511. * @ah: The &struct ath5k_hw
  512. *
  513. * Choose the right target gain based on current gain
  514. * and RF gain optimization ladder
  515. */
  516. static s8
  517. ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  518. {
  519. const struct ath5k_gain_opt *go;
  520. const struct ath5k_gain_opt_step *g_step;
  521. int ret = 0;
  522. switch (ah->ah_radio) {
  523. case AR5K_RF5111:
  524. go = &rfgain_opt_5111;
  525. break;
  526. case AR5K_RF5112:
  527. go = &rfgain_opt_5112;
  528. break;
  529. default:
  530. return 0;
  531. }
  532. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  533. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  534. /* Reached maximum */
  535. if (ah->ah_gain.g_step_idx == 0)
  536. return -1;
  537. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  538. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  539. ah->ah_gain.g_step_idx > 0;
  540. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  541. ah->ah_gain.g_target -= 2 *
  542. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  543. g_step->gos_gain);
  544. ret = 1;
  545. goto done;
  546. }
  547. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  548. /* Reached minimum */
  549. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  550. return -2;
  551. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  552. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  553. ah->ah_gain.g_step_idx < go->go_steps_count - 1;
  554. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  555. ah->ah_gain.g_target -= 2 *
  556. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  557. g_step->gos_gain);
  558. ret = 2;
  559. goto done;
  560. }
  561. done:
  562. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  563. "ret %d, gain step %u, current gain %u, target gain %u\n",
  564. ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
  565. ah->ah_gain.g_target);
  566. return ret;
  567. }
  568. /**
  569. * ath5k_hw_gainf_calibrate() - Do a gain_F calibration
  570. * @ah: The &struct ath5k_hw
  571. *
  572. * Main callback for thermal RF gain calibration engine
  573. * Check for a new gain reading and schedule an adjustment
  574. * if needed.
  575. *
  576. * Returns one of enum ath5k_rfgain codes
  577. */
  578. enum ath5k_rfgain
  579. ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  580. {
  581. u32 data, type;
  582. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  583. if (ah->ah_rf_banks == NULL ||
  584. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  585. return AR5K_RFGAIN_INACTIVE;
  586. /* No check requested, either engine is inactive
  587. * or an adjustment is already requested */
  588. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  589. goto done;
  590. /* Read the PAPD (Peak to Average Power Detector)
  591. * register */
  592. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  593. /* No probe is scheduled, read gain_F measurement */
  594. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  595. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  596. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  597. /* If tx packet is CCK correct the gain_F measurement
  598. * by cck ofdm gain delta */
  599. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  600. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  601. ah->ah_gain.g_current +=
  602. ee->ee_cck_ofdm_gain_delta;
  603. else
  604. ah->ah_gain.g_current +=
  605. AR5K_GAIN_CCK_PROBE_CORR;
  606. }
  607. /* Further correct gain_F measurement for
  608. * RF5112A radios */
  609. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  610. ath5k_hw_rf_gainf_corr(ah);
  611. ah->ah_gain.g_current =
  612. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  613. (ah->ah_gain.g_current - ah->ah_gain.g_f_corr) :
  614. 0;
  615. }
  616. /* Check if measurement is ok and if we need
  617. * to adjust gain, schedule a gain adjustment,
  618. * else switch back to the active state */
  619. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  620. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  621. ath5k_hw_rf_gainf_adjust(ah)) {
  622. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  623. } else {
  624. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  625. }
  626. }
  627. done:
  628. return ah->ah_gain.g_state;
  629. }
  630. /**
  631. * ath5k_hw_rfgain_init() - Write initial RF gain settings to hw
  632. * @ah: The &struct ath5k_hw
  633. * @band: One of enum nl80211_band
  634. *
  635. * Write initial RF gain table to set the RF sensitivity.
  636. *
  637. * NOTE: This one works on all RF chips and has nothing to do
  638. * with Gain_F calibration
  639. */
  640. static int
  641. ath5k_hw_rfgain_init(struct ath5k_hw *ah, enum nl80211_band band)
  642. {
  643. const struct ath5k_ini_rfgain *ath5k_rfg;
  644. unsigned int i, size, index;
  645. switch (ah->ah_radio) {
  646. case AR5K_RF5111:
  647. ath5k_rfg = rfgain_5111;
  648. size = ARRAY_SIZE(rfgain_5111);
  649. break;
  650. case AR5K_RF5112:
  651. ath5k_rfg = rfgain_5112;
  652. size = ARRAY_SIZE(rfgain_5112);
  653. break;
  654. case AR5K_RF2413:
  655. ath5k_rfg = rfgain_2413;
  656. size = ARRAY_SIZE(rfgain_2413);
  657. break;
  658. case AR5K_RF2316:
  659. ath5k_rfg = rfgain_2316;
  660. size = ARRAY_SIZE(rfgain_2316);
  661. break;
  662. case AR5K_RF5413:
  663. ath5k_rfg = rfgain_5413;
  664. size = ARRAY_SIZE(rfgain_5413);
  665. break;
  666. case AR5K_RF2317:
  667. case AR5K_RF2425:
  668. ath5k_rfg = rfgain_2425;
  669. size = ARRAY_SIZE(rfgain_2425);
  670. break;
  671. default:
  672. return -EINVAL;
  673. }
  674. index = (band == NL80211_BAND_2GHZ) ? 1 : 0;
  675. for (i = 0; i < size; i++) {
  676. AR5K_REG_WAIT(i);
  677. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[index],
  678. (u32)ath5k_rfg[i].rfg_register);
  679. }
  680. return 0;
  681. }
  682. /********************\
  683. * RF Registers setup *
  684. \********************/
  685. /**
  686. * ath5k_hw_rfregs_init() - Initialize RF register settings
  687. * @ah: The &struct ath5k_hw
  688. * @channel: The &struct ieee80211_channel
  689. * @mode: One of enum ath5k_driver_mode
  690. *
  691. * Setup RF registers by writing RF buffer on hw. For
  692. * more infos on this, check out rfbuffer.h
  693. */
  694. static int
  695. ath5k_hw_rfregs_init(struct ath5k_hw *ah,
  696. struct ieee80211_channel *channel,
  697. unsigned int mode)
  698. {
  699. const struct ath5k_rf_reg *rf_regs;
  700. const struct ath5k_ini_rfbuffer *ini_rfb;
  701. const struct ath5k_gain_opt *go = NULL;
  702. const struct ath5k_gain_opt_step *g_step;
  703. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  704. u8 ee_mode = 0;
  705. u32 *rfb;
  706. int i, obdb = -1, bank = -1;
  707. switch (ah->ah_radio) {
  708. case AR5K_RF5111:
  709. rf_regs = rf_regs_5111;
  710. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  711. ini_rfb = rfb_5111;
  712. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
  713. go = &rfgain_opt_5111;
  714. break;
  715. case AR5K_RF5112:
  716. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  717. rf_regs = rf_regs_5112a;
  718. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  719. ini_rfb = rfb_5112a;
  720. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
  721. } else {
  722. rf_regs = rf_regs_5112;
  723. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  724. ini_rfb = rfb_5112;
  725. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
  726. }
  727. go = &rfgain_opt_5112;
  728. break;
  729. case AR5K_RF2413:
  730. rf_regs = rf_regs_2413;
  731. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
  732. ini_rfb = rfb_2413;
  733. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
  734. break;
  735. case AR5K_RF2316:
  736. rf_regs = rf_regs_2316;
  737. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
  738. ini_rfb = rfb_2316;
  739. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
  740. break;
  741. case AR5K_RF5413:
  742. rf_regs = rf_regs_5413;
  743. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
  744. ini_rfb = rfb_5413;
  745. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
  746. break;
  747. case AR5K_RF2317:
  748. rf_regs = rf_regs_2425;
  749. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  750. ini_rfb = rfb_2317;
  751. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
  752. break;
  753. case AR5K_RF2425:
  754. rf_regs = rf_regs_2425;
  755. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  756. if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
  757. ini_rfb = rfb_2425;
  758. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
  759. } else {
  760. ini_rfb = rfb_2417;
  761. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
  762. }
  763. break;
  764. default:
  765. return -EINVAL;
  766. }
  767. /* If it's the first time we set RF buffer, allocate
  768. * ah->ah_rf_banks based on ah->ah_rf_banks_size
  769. * we set above */
  770. if (ah->ah_rf_banks == NULL) {
  771. ah->ah_rf_banks = kmalloc_array(ah->ah_rf_banks_size,
  772. sizeof(u32),
  773. GFP_KERNEL);
  774. if (ah->ah_rf_banks == NULL) {
  775. ATH5K_ERR(ah, "out of memory\n");
  776. return -ENOMEM;
  777. }
  778. }
  779. /* Copy values to modify them */
  780. rfb = ah->ah_rf_banks;
  781. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  782. if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
  783. ATH5K_ERR(ah, "invalid bank\n");
  784. return -EINVAL;
  785. }
  786. /* Bank changed, write down the offset */
  787. if (bank != ini_rfb[i].rfb_bank) {
  788. bank = ini_rfb[i].rfb_bank;
  789. ah->ah_offset[bank] = i;
  790. }
  791. rfb[i] = ini_rfb[i].rfb_mode_data[mode];
  792. }
  793. /* Set Output and Driver bias current (OB/DB) */
  794. if (channel->band == NL80211_BAND_2GHZ) {
  795. if (channel->hw_value == AR5K_MODE_11B)
  796. ee_mode = AR5K_EEPROM_MODE_11B;
  797. else
  798. ee_mode = AR5K_EEPROM_MODE_11G;
  799. /* For RF511X/RF211X combination we
  800. * use b_OB and b_DB parameters stored
  801. * in eeprom on ee->ee_ob[ee_mode][0]
  802. *
  803. * For all other chips we use OB/DB for 2GHz
  804. * stored in the b/g modal section just like
  805. * 802.11a on ee->ee_ob[ee_mode][1] */
  806. if ((ah->ah_radio == AR5K_RF5111) ||
  807. (ah->ah_radio == AR5K_RF5112))
  808. obdb = 0;
  809. else
  810. obdb = 1;
  811. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  812. AR5K_RF_OB_2GHZ, true);
  813. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  814. AR5K_RF_DB_2GHZ, true);
  815. /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
  816. } else if ((channel->band == NL80211_BAND_5GHZ) ||
  817. (ah->ah_radio == AR5K_RF5111)) {
  818. /* For 11a, Turbo and XR we need to choose
  819. * OB/DB based on frequency range */
  820. ee_mode = AR5K_EEPROM_MODE_11A;
  821. obdb = channel->center_freq >= 5725 ? 3 :
  822. (channel->center_freq >= 5500 ? 2 :
  823. (channel->center_freq >= 5260 ? 1 :
  824. (channel->center_freq > 4000 ? 0 : -1)));
  825. if (obdb < 0)
  826. return -EINVAL;
  827. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  828. AR5K_RF_OB_5GHZ, true);
  829. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  830. AR5K_RF_DB_5GHZ, true);
  831. }
  832. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  833. /* Set turbo mode (N/A on RF5413) */
  834. if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) &&
  835. (ah->ah_radio != AR5K_RF5413))
  836. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_TURBO, false);
  837. /* Bank Modifications (chip-specific) */
  838. if (ah->ah_radio == AR5K_RF5111) {
  839. /* Set gain_F settings according to current step */
  840. if (channel->hw_value != AR5K_MODE_11B) {
  841. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  842. AR5K_PHY_FRAME_CTL_TX_CLIP,
  843. g_step->gos_param[0]);
  844. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  845. AR5K_RF_PWD_90, true);
  846. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  847. AR5K_RF_PWD_84, true);
  848. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  849. AR5K_RF_RFGAIN_SEL, true);
  850. /* We programmed gain_F parameters, switch back
  851. * to active state */
  852. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  853. }
  854. /* Bank 6/7 setup */
  855. ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
  856. AR5K_RF_PWD_XPD, true);
  857. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
  858. AR5K_RF_XPD_GAIN, true);
  859. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  860. AR5K_RF_GAIN_I, true);
  861. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  862. AR5K_RF_PLO_SEL, true);
  863. /* Tweak power detectors for half/quarter rate support */
  864. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ ||
  865. ah->ah_bwmode == AR5K_BWMODE_10MHZ) {
  866. u8 wait_i;
  867. ath5k_hw_rfb_op(ah, rf_regs, 0x1f,
  868. AR5K_RF_WAIT_S, true);
  869. wait_i = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ?
  870. 0x1f : 0x10;
  871. ath5k_hw_rfb_op(ah, rf_regs, wait_i,
  872. AR5K_RF_WAIT_I, true);
  873. ath5k_hw_rfb_op(ah, rf_regs, 3,
  874. AR5K_RF_MAX_TIME, true);
  875. }
  876. }
  877. if (ah->ah_radio == AR5K_RF5112) {
  878. /* Set gain_F settings according to current step */
  879. if (channel->hw_value != AR5K_MODE_11B) {
  880. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
  881. AR5K_RF_MIXGAIN_OVR, true);
  882. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  883. AR5K_RF_PWD_138, true);
  884. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  885. AR5K_RF_PWD_137, true);
  886. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  887. AR5K_RF_PWD_136, true);
  888. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
  889. AR5K_RF_PWD_132, true);
  890. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
  891. AR5K_RF_PWD_131, true);
  892. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
  893. AR5K_RF_PWD_130, true);
  894. /* We programmed gain_F parameters, switch back
  895. * to active state */
  896. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  897. }
  898. /* Bank 6/7 setup */
  899. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  900. AR5K_RF_XPD_SEL, true);
  901. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  902. /* Rev. 1 supports only one xpd */
  903. ath5k_hw_rfb_op(ah, rf_regs,
  904. ee->ee_x_gain[ee_mode],
  905. AR5K_RF_XPD_GAIN, true);
  906. } else {
  907. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  908. if (ee->ee_pd_gains[ee_mode] > 1) {
  909. ath5k_hw_rfb_op(ah, rf_regs,
  910. pdg_curve_to_idx[0],
  911. AR5K_RF_PD_GAIN_LO, true);
  912. ath5k_hw_rfb_op(ah, rf_regs,
  913. pdg_curve_to_idx[1],
  914. AR5K_RF_PD_GAIN_HI, true);
  915. } else {
  916. ath5k_hw_rfb_op(ah, rf_regs,
  917. pdg_curve_to_idx[0],
  918. AR5K_RF_PD_GAIN_LO, true);
  919. ath5k_hw_rfb_op(ah, rf_regs,
  920. pdg_curve_to_idx[0],
  921. AR5K_RF_PD_GAIN_HI, true);
  922. }
  923. /* Lower synth voltage on Rev 2 */
  924. if (ah->ah_radio == AR5K_RF5112 &&
  925. (ah->ah_radio_5ghz_revision & AR5K_SREV_REV) > 0) {
  926. ath5k_hw_rfb_op(ah, rf_regs, 2,
  927. AR5K_RF_HIGH_VC_CP, true);
  928. ath5k_hw_rfb_op(ah, rf_regs, 2,
  929. AR5K_RF_MID_VC_CP, true);
  930. ath5k_hw_rfb_op(ah, rf_regs, 2,
  931. AR5K_RF_LOW_VC_CP, true);
  932. ath5k_hw_rfb_op(ah, rf_regs, 2,
  933. AR5K_RF_PUSH_UP, true);
  934. }
  935. /* Decrease power consumption on 5213+ BaseBand */
  936. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  937. ath5k_hw_rfb_op(ah, rf_regs, 1,
  938. AR5K_RF_PAD2GND, true);
  939. ath5k_hw_rfb_op(ah, rf_regs, 1,
  940. AR5K_RF_XB2_LVL, true);
  941. ath5k_hw_rfb_op(ah, rf_regs, 1,
  942. AR5K_RF_XB5_LVL, true);
  943. ath5k_hw_rfb_op(ah, rf_regs, 1,
  944. AR5K_RF_PWD_167, true);
  945. ath5k_hw_rfb_op(ah, rf_regs, 1,
  946. AR5K_RF_PWD_166, true);
  947. }
  948. }
  949. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  950. AR5K_RF_GAIN_I, true);
  951. /* Tweak power detector for half/quarter rates */
  952. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ ||
  953. ah->ah_bwmode == AR5K_BWMODE_10MHZ) {
  954. u8 pd_delay;
  955. pd_delay = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ?
  956. 0xf : 0x8;
  957. ath5k_hw_rfb_op(ah, rf_regs, pd_delay,
  958. AR5K_RF_PD_PERIOD_A, true);
  959. ath5k_hw_rfb_op(ah, rf_regs, 0xf,
  960. AR5K_RF_PD_DELAY_A, true);
  961. }
  962. }
  963. if (ah->ah_radio == AR5K_RF5413 &&
  964. channel->band == NL80211_BAND_2GHZ) {
  965. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
  966. true);
  967. /* Set optimum value for early revisions (on pci-e chips) */
  968. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  969. ah->ah_mac_srev < AR5K_SREV_AR5413)
  970. ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
  971. AR5K_RF_PWD_ICLOBUF_2G, true);
  972. }
  973. /* Write RF banks on hw */
  974. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  975. AR5K_REG_WAIT(i);
  976. ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
  977. }
  978. return 0;
  979. }
  980. /**************************\
  981. PHY/RF channel functions
  982. \**************************/
  983. /**
  984. * ath5k_hw_rf5110_chan2athchan() - Convert channel freq on RF5110
  985. * @channel: The &struct ieee80211_channel
  986. *
  987. * Map channel frequency to IEEE channel number and convert it
  988. * to an internal channel value used by the RF5110 chipset.
  989. */
  990. static u32
  991. ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
  992. {
  993. u32 athchan;
  994. athchan = (ath5k_hw_bitswap(
  995. (ieee80211_frequency_to_channel(
  996. channel->center_freq) - 24) / 2, 5)
  997. << 1) | (1 << 6) | 0x1;
  998. return athchan;
  999. }
  1000. /**
  1001. * ath5k_hw_rf5110_channel() - Set channel frequency on RF5110
  1002. * @ah: The &struct ath5k_hw
  1003. * @channel: The &struct ieee80211_channel
  1004. */
  1005. static int
  1006. ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  1007. struct ieee80211_channel *channel)
  1008. {
  1009. u32 data;
  1010. /*
  1011. * Set the channel and wait
  1012. */
  1013. data = ath5k_hw_rf5110_chan2athchan(channel);
  1014. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  1015. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  1016. usleep_range(1000, 1500);
  1017. return 0;
  1018. }
  1019. /**
  1020. * ath5k_hw_rf5111_chan2athchan() - Handle 2GHz channels on RF5111/2111
  1021. * @ieee: IEEE channel number
  1022. * @athchan: The &struct ath5k_athchan_2ghz
  1023. *
  1024. * In order to enable the RF2111 frequency converter on RF5111/2111 setups
  1025. * we need to add some offsets and extra flags to the data values we pass
  1026. * on to the PHY. So for every 2GHz channel this function gets called
  1027. * to do the conversion.
  1028. */
  1029. static int
  1030. ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  1031. struct ath5k_athchan_2ghz *athchan)
  1032. {
  1033. int channel;
  1034. /* Cast this value to catch negative channel numbers (>= -19) */
  1035. channel = (int)ieee;
  1036. /*
  1037. * Map 2GHz IEEE channel to 5GHz Atheros channel
  1038. */
  1039. if (channel <= 13) {
  1040. athchan->a2_athchan = 115 + channel;
  1041. athchan->a2_flags = 0x46;
  1042. } else if (channel == 14) {
  1043. athchan->a2_athchan = 124;
  1044. athchan->a2_flags = 0x44;
  1045. } else if (channel >= 15 && channel <= 26) {
  1046. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  1047. athchan->a2_flags = 0x46;
  1048. } else
  1049. return -EINVAL;
  1050. return 0;
  1051. }
  1052. /**
  1053. * ath5k_hw_rf5111_channel() - Set channel frequency on RF5111/2111
  1054. * @ah: The &struct ath5k_hw
  1055. * @channel: The &struct ieee80211_channel
  1056. */
  1057. static int
  1058. ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  1059. struct ieee80211_channel *channel)
  1060. {
  1061. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  1062. unsigned int ath5k_channel =
  1063. ieee80211_frequency_to_channel(channel->center_freq);
  1064. u32 data0, data1, clock;
  1065. int ret;
  1066. /*
  1067. * Set the channel on the RF5111 radio
  1068. */
  1069. data0 = data1 = 0;
  1070. if (channel->band == NL80211_BAND_2GHZ) {
  1071. /* Map 2GHz channel to 5GHz Atheros channel ID */
  1072. ret = ath5k_hw_rf5111_chan2athchan(
  1073. ieee80211_frequency_to_channel(channel->center_freq),
  1074. &ath5k_channel_2ghz);
  1075. if (ret)
  1076. return ret;
  1077. ath5k_channel = ath5k_channel_2ghz.a2_athchan;
  1078. data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
  1079. << 5) | (1 << 4);
  1080. }
  1081. if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
  1082. clock = 1;
  1083. data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
  1084. (clock << 1) | (1 << 10) | 1;
  1085. } else {
  1086. clock = 0;
  1087. data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
  1088. << 2) | (clock << 1) | (1 << 10) | 1;
  1089. }
  1090. ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
  1091. AR5K_RF_BUFFER);
  1092. ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
  1093. AR5K_RF_BUFFER_CONTROL_3);
  1094. return 0;
  1095. }
  1096. /**
  1097. * ath5k_hw_rf5112_channel() - Set channel frequency on 5112 and newer
  1098. * @ah: The &struct ath5k_hw
  1099. * @channel: The &struct ieee80211_channel
  1100. *
  1101. * On RF5112/2112 and newer we don't need to do any conversion.
  1102. * We pass the frequency value after a few modifications to the
  1103. * chip directly.
  1104. *
  1105. * NOTE: Make sure channel frequency given is within our range or else
  1106. * we might damage the chip ! Use ath5k_channel_ok before calling this one.
  1107. */
  1108. static int
  1109. ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
  1110. struct ieee80211_channel *channel)
  1111. {
  1112. u32 data, data0, data1, data2;
  1113. u16 c;
  1114. data = data0 = data1 = data2 = 0;
  1115. c = channel->center_freq;
  1116. /* My guess based on code:
  1117. * 2GHz RF has 2 synth modes, one with a Local Oscillator
  1118. * at 2224Hz and one with a LO at 2192Hz. IF is 1520Hz
  1119. * (3040/2). data0 is used to set the PLL divider and data1
  1120. * selects synth mode. */
  1121. if (c < 4800) {
  1122. /* Channel 14 and all frequencies with 2Hz spacing
  1123. * below/above (non-standard channels) */
  1124. if (!((c - 2224) % 5)) {
  1125. /* Same as (c - 2224) / 5 */
  1126. data0 = ((2 * (c - 704)) - 3040) / 10;
  1127. data1 = 1;
  1128. /* Channel 1 and all frequencies with 5Hz spacing
  1129. * below/above (standard channels without channel 14) */
  1130. } else if (!((c - 2192) % 5)) {
  1131. /* Same as (c - 2192) / 5 */
  1132. data0 = ((2 * (c - 672)) - 3040) / 10;
  1133. data1 = 0;
  1134. } else
  1135. return -EINVAL;
  1136. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  1137. /* This is more complex, we have a single synthesizer with
  1138. * 4 reference clock settings (?) based on frequency spacing
  1139. * and set using data2. LO is at 4800Hz and data0 is again used
  1140. * to set some divider.
  1141. *
  1142. * NOTE: There is an old atheros presentation at Stanford
  1143. * that mentions a method called dual direct conversion
  1144. * with 1GHz sliding IF for RF5110. Maybe that's what we
  1145. * have here, or an updated version. */
  1146. } else if ((c % 5) != 2 || c > 5435) {
  1147. if (!(c % 20) && c >= 5120) {
  1148. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  1149. data2 = ath5k_hw_bitswap(3, 2);
  1150. } else if (!(c % 10)) {
  1151. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  1152. data2 = ath5k_hw_bitswap(2, 2);
  1153. } else if (!(c % 5)) {
  1154. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  1155. data2 = ath5k_hw_bitswap(1, 2);
  1156. } else
  1157. return -EINVAL;
  1158. } else {
  1159. data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
  1160. data2 = ath5k_hw_bitswap(0, 2);
  1161. }
  1162. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  1163. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  1164. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  1165. return 0;
  1166. }
  1167. /**
  1168. * ath5k_hw_rf2425_channel() - Set channel frequency on RF2425
  1169. * @ah: The &struct ath5k_hw
  1170. * @channel: The &struct ieee80211_channel
  1171. *
  1172. * AR2425/2417 have a different 2GHz RF so code changes
  1173. * a little bit from RF5112.
  1174. */
  1175. static int
  1176. ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  1177. struct ieee80211_channel *channel)
  1178. {
  1179. u32 data, data0, data2;
  1180. u16 c;
  1181. data = data0 = data2 = 0;
  1182. c = channel->center_freq;
  1183. if (c < 4800) {
  1184. data0 = ath5k_hw_bitswap((c - 2272), 8);
  1185. data2 = 0;
  1186. /* ? 5GHz ? */
  1187. } else if ((c % 5) != 2 || c > 5435) {
  1188. if (!(c % 20) && c < 5120)
  1189. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  1190. else if (!(c % 10))
  1191. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  1192. else if (!(c % 5))
  1193. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  1194. else
  1195. return -EINVAL;
  1196. data2 = ath5k_hw_bitswap(1, 2);
  1197. } else {
  1198. data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
  1199. data2 = ath5k_hw_bitswap(0, 2);
  1200. }
  1201. data = (data0 << 4) | data2 << 2 | 0x1001;
  1202. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  1203. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  1204. return 0;
  1205. }
  1206. /**
  1207. * ath5k_hw_channel() - Set a channel on the radio chip
  1208. * @ah: The &struct ath5k_hw
  1209. * @channel: The &struct ieee80211_channel
  1210. *
  1211. * This is the main function called to set a channel on the
  1212. * radio chip based on the radio chip version.
  1213. */
  1214. static int
  1215. ath5k_hw_channel(struct ath5k_hw *ah,
  1216. struct ieee80211_channel *channel)
  1217. {
  1218. int ret;
  1219. /*
  1220. * Check bounds supported by the PHY (we don't care about regulatory
  1221. * restrictions at this point).
  1222. */
  1223. if (!ath5k_channel_ok(ah, channel)) {
  1224. ATH5K_ERR(ah,
  1225. "channel frequency (%u MHz) out of supported "
  1226. "band range\n",
  1227. channel->center_freq);
  1228. return -EINVAL;
  1229. }
  1230. /*
  1231. * Set the channel and wait
  1232. */
  1233. switch (ah->ah_radio) {
  1234. case AR5K_RF5110:
  1235. ret = ath5k_hw_rf5110_channel(ah, channel);
  1236. break;
  1237. case AR5K_RF5111:
  1238. ret = ath5k_hw_rf5111_channel(ah, channel);
  1239. break;
  1240. case AR5K_RF2317:
  1241. case AR5K_RF2425:
  1242. ret = ath5k_hw_rf2425_channel(ah, channel);
  1243. break;
  1244. default:
  1245. ret = ath5k_hw_rf5112_channel(ah, channel);
  1246. break;
  1247. }
  1248. if (ret)
  1249. return ret;
  1250. /* Set JAPAN setting for channel 14 */
  1251. if (channel->center_freq == 2484) {
  1252. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  1253. AR5K_PHY_CCKTXCTL_JAPAN);
  1254. } else {
  1255. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  1256. AR5K_PHY_CCKTXCTL_WORLD);
  1257. }
  1258. ah->ah_current_channel = channel;
  1259. return 0;
  1260. }
  1261. /*****************\
  1262. PHY calibration
  1263. \*****************/
  1264. /**
  1265. * DOC: PHY Calibration routines
  1266. *
  1267. * Noise floor calibration: When we tell the hardware to
  1268. * perform a noise floor calibration by setting the
  1269. * AR5K_PHY_AGCCTL_NF bit on AR5K_PHY_AGCCTL, it will periodically
  1270. * sample-and-hold the minimum noise level seen at the antennas.
  1271. * This value is then stored in a ring buffer of recently measured
  1272. * noise floor values so we have a moving window of the last few
  1273. * samples. The median of the values in the history is then loaded
  1274. * into the hardware for its own use for RSSI and CCA measurements.
  1275. * This type of calibration doesn't interfere with traffic.
  1276. *
  1277. * AGC calibration: When we tell the hardware to perform
  1278. * an AGC (Automatic Gain Control) calibration by setting the
  1279. * AR5K_PHY_AGCCTL_CAL, hw disconnects the antennas and does
  1280. * a calibration on the DC offsets of ADCs. During this period
  1281. * rx/tx gets disabled so we have to deal with it on the driver
  1282. * part.
  1283. *
  1284. * I/Q calibration: When we tell the hardware to perform
  1285. * an I/Q calibration, it tries to correct I/Q imbalance and
  1286. * fix QAM constellation by sampling data from rxed frames.
  1287. * It doesn't interfere with traffic.
  1288. *
  1289. * For more infos on AGC and I/Q calibration check out patent doc
  1290. * #03/094463.
  1291. */
  1292. /**
  1293. * ath5k_hw_read_measured_noise_floor() - Read measured NF from hw
  1294. * @ah: The &struct ath5k_hw
  1295. */
  1296. static s32
  1297. ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)
  1298. {
  1299. s32 val;
  1300. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  1301. return sign_extend32(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 8);
  1302. }
  1303. /**
  1304. * ath5k_hw_init_nfcal_hist() - Initialize NF calibration history buffer
  1305. * @ah: The &struct ath5k_hw
  1306. */
  1307. void
  1308. ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)
  1309. {
  1310. int i;
  1311. ah->ah_nfcal_hist.index = 0;
  1312. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++)
  1313. ah->ah_nfcal_hist.nfval[i] = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  1314. }
  1315. /**
  1316. * ath5k_hw_update_nfcal_hist() - Update NF calibration history buffer
  1317. * @ah: The &struct ath5k_hw
  1318. * @noise_floor: The NF we got from hw
  1319. */
  1320. static void ath5k_hw_update_nfcal_hist(struct ath5k_hw *ah, s16 noise_floor)
  1321. {
  1322. struct ath5k_nfcal_hist *hist = &ah->ah_nfcal_hist;
  1323. hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX - 1);
  1324. hist->nfval[hist->index] = noise_floor;
  1325. }
  1326. /**
  1327. * ath5k_hw_get_median_noise_floor() - Get median NF from history buffer
  1328. * @ah: The &struct ath5k_hw
  1329. */
  1330. static s16
  1331. ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)
  1332. {
  1333. s16 sort[ATH5K_NF_CAL_HIST_MAX];
  1334. s16 tmp;
  1335. int i, j;
  1336. memcpy(sort, ah->ah_nfcal_hist.nfval, sizeof(sort));
  1337. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX - 1; i++) {
  1338. for (j = 1; j < ATH5K_NF_CAL_HIST_MAX - i; j++) {
  1339. if (sort[j] > sort[j - 1]) {
  1340. tmp = sort[j];
  1341. sort[j] = sort[j - 1];
  1342. sort[j - 1] = tmp;
  1343. }
  1344. }
  1345. }
  1346. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++) {
  1347. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1348. "cal %d:%d\n", i, sort[i]);
  1349. }
  1350. return sort[(ATH5K_NF_CAL_HIST_MAX - 1) / 2];
  1351. }
  1352. /**
  1353. * ath5k_hw_update_noise_floor() - Update NF on hardware
  1354. * @ah: The &struct ath5k_hw
  1355. *
  1356. * This is the main function we call to perform a NF calibration,
  1357. * it reads NF from hardware, calculates the median and updates
  1358. * NF on hw.
  1359. */
  1360. void
  1361. ath5k_hw_update_noise_floor(struct ath5k_hw *ah)
  1362. {
  1363. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1364. u32 val;
  1365. s16 nf, threshold;
  1366. u8 ee_mode;
  1367. /* keep last value if calibration hasn't completed */
  1368. if (ath5k_hw_reg_read(ah, AR5K_PHY_AGCCTL) & AR5K_PHY_AGCCTL_NF) {
  1369. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1370. "NF did not complete in calibration window\n");
  1371. return;
  1372. }
  1373. ah->ah_cal_mask |= AR5K_CALIBRATION_NF;
  1374. ee_mode = ath5k_eeprom_mode_from_channel(ah, ah->ah_current_channel);
  1375. /* completed NF calibration, test threshold */
  1376. nf = ath5k_hw_read_measured_noise_floor(ah);
  1377. threshold = ee->ee_noise_floor_thr[ee_mode];
  1378. if (nf > threshold) {
  1379. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1380. "noise floor failure detected; "
  1381. "read %d, threshold %d\n",
  1382. nf, threshold);
  1383. nf = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  1384. }
  1385. ath5k_hw_update_nfcal_hist(ah, nf);
  1386. nf = ath5k_hw_get_median_noise_floor(ah);
  1387. /* load noise floor (in .5 dBm) so the hardware will use it */
  1388. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF) & ~AR5K_PHY_NF_M;
  1389. val |= (nf * 2) & AR5K_PHY_NF_M;
  1390. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1391. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1392. ~(AR5K_PHY_AGCCTL_NF_EN | AR5K_PHY_AGCCTL_NF_NOUPDATE));
  1393. ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1394. 0, false);
  1395. /*
  1396. * Load a high max CCA Power value (-50 dBm in .5 dBm units)
  1397. * so that we're not capped by the median we just loaded.
  1398. * This will be used as the initial value for the next noise
  1399. * floor calibration.
  1400. */
  1401. val = (val & ~AR5K_PHY_NF_M) | ((-50 * 2) & AR5K_PHY_NF_M);
  1402. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1403. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1404. AR5K_PHY_AGCCTL_NF_EN |
  1405. AR5K_PHY_AGCCTL_NF_NOUPDATE |
  1406. AR5K_PHY_AGCCTL_NF);
  1407. ah->ah_noise_floor = nf;
  1408. ah->ah_cal_mask &= ~AR5K_CALIBRATION_NF;
  1409. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1410. "noise floor calibrated: %d\n", nf);
  1411. }
  1412. /**
  1413. * ath5k_hw_rf5110_calibrate() - Perform a PHY calibration on RF5110
  1414. * @ah: The &struct ath5k_hw
  1415. * @channel: The &struct ieee80211_channel
  1416. *
  1417. * Do a complete PHY calibration (AGC + NF + I/Q) on RF5110
  1418. */
  1419. static int
  1420. ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  1421. struct ieee80211_channel *channel)
  1422. {
  1423. u32 phy_sig, phy_agc, phy_sat, beacon;
  1424. int ret;
  1425. if (!(ah->ah_cal_mask & AR5K_CALIBRATION_FULL))
  1426. return 0;
  1427. /*
  1428. * Disable beacons and RX/TX queues, wait
  1429. */
  1430. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1431. AR5K_DIAG_SW_DIS_TX_5210 | AR5K_DIAG_SW_DIS_RX_5210);
  1432. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  1433. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  1434. usleep_range(2000, 2500);
  1435. /*
  1436. * Set the channel (with AGC turned off)
  1437. */
  1438. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1439. udelay(10);
  1440. ret = ath5k_hw_channel(ah, channel);
  1441. /*
  1442. * Activate PHY and wait
  1443. */
  1444. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1445. usleep_range(1000, 1500);
  1446. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1447. if (ret)
  1448. return ret;
  1449. /*
  1450. * Calibrate the radio chip
  1451. */
  1452. /* Remember normal state */
  1453. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  1454. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  1455. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  1456. /* Update radio registers */
  1457. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1458. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1459. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1460. AR5K_PHY_AGCCOARSE_LO)) |
  1461. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1462. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1463. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1464. AR5K_PHY_ADCSAT_THR)) |
  1465. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1466. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1467. udelay(20);
  1468. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1469. udelay(10);
  1470. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1471. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1472. usleep_range(1000, 1500);
  1473. /*
  1474. * Enable calibration and wait until completion
  1475. */
  1476. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1477. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1478. AR5K_PHY_AGCCTL_CAL, 0, false);
  1479. /* Reset to normal state */
  1480. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1481. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1482. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1483. if (ret) {
  1484. ATH5K_ERR(ah, "calibration timeout (%uMHz)\n",
  1485. channel->center_freq);
  1486. return ret;
  1487. }
  1488. /*
  1489. * Re-enable RX/TX and beacons
  1490. */
  1491. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1492. AR5K_DIAG_SW_DIS_TX_5210 | AR5K_DIAG_SW_DIS_RX_5210);
  1493. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1494. return 0;
  1495. }
  1496. /**
  1497. * ath5k_hw_rf511x_iq_calibrate() - Perform I/Q calibration on RF5111 and newer
  1498. * @ah: The &struct ath5k_hw
  1499. */
  1500. static int
  1501. ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)
  1502. {
  1503. u32 i_pwr, q_pwr;
  1504. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1505. int i;
  1506. /* Skip if I/Q calibration is not needed or if it's still running */
  1507. if (!ah->ah_iq_cal_needed)
  1508. return -EINVAL;
  1509. else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN) {
  1510. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1511. "I/Q calibration still running");
  1512. return -EBUSY;
  1513. }
  1514. /* Calibration has finished, get the results and re-run */
  1515. /* Work around for empty results which can apparently happen on 5212:
  1516. * Read registers up to 10 times until we get both i_pr and q_pwr */
  1517. for (i = 0; i <= 10; i++) {
  1518. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1519. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1520. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1521. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1522. "iq_corr:%x i_pwr:%x q_pwr:%x", iq_corr, i_pwr, q_pwr);
  1523. if (i_pwr && q_pwr)
  1524. break;
  1525. }
  1526. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1527. if (ah->ah_version == AR5K_AR5211)
  1528. q_coffd = q_pwr >> 6;
  1529. else
  1530. q_coffd = q_pwr >> 7;
  1531. /* In case i_coffd became zero, cancel calibration
  1532. * not only it's too small, it'll also result a divide
  1533. * by zero later on. */
  1534. if (i_coffd == 0 || q_coffd < 2)
  1535. return -ECANCELED;
  1536. /* Protect against loss of sign bits */
  1537. i_coff = (-iq_corr) / i_coffd;
  1538. i_coff = clamp(i_coff, -32, 31); /* signed 6 bit */
  1539. if (ah->ah_version == AR5K_AR5211)
  1540. q_coff = (i_pwr / q_coffd) - 64;
  1541. else
  1542. q_coff = (i_pwr / q_coffd) - 128;
  1543. q_coff = clamp(q_coff, -16, 15); /* signed 5 bit */
  1544. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1545. "new I:%d Q:%d (i_coffd:%x q_coffd:%x)",
  1546. i_coff, q_coff, i_coffd, q_coffd);
  1547. /* Commit new I/Q values (set enable bit last to match HAL sources) */
  1548. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF, i_coff);
  1549. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF, q_coff);
  1550. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  1551. /* Re-enable calibration -if we don't we'll commit
  1552. * the same values again and again */
  1553. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1554. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1555. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1556. return 0;
  1557. }
  1558. /**
  1559. * ath5k_hw_phy_calibrate() - Perform a PHY calibration
  1560. * @ah: The &struct ath5k_hw
  1561. * @channel: The &struct ieee80211_channel
  1562. *
  1563. * The main function we call from above to perform
  1564. * a short or full PHY calibration based on RF chip
  1565. * and current channel
  1566. */
  1567. int
  1568. ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1569. struct ieee80211_channel *channel)
  1570. {
  1571. int ret;
  1572. if (ah->ah_radio == AR5K_RF5110)
  1573. return ath5k_hw_rf5110_calibrate(ah, channel);
  1574. ret = ath5k_hw_rf511x_iq_calibrate(ah);
  1575. if (ret) {
  1576. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1577. "No I/Q correction performed (%uMHz)\n",
  1578. channel->center_freq);
  1579. /* Happens all the time if there is not much
  1580. * traffic, consider it normal behaviour. */
  1581. ret = 0;
  1582. }
  1583. /* On full calibration request a PAPD probe for
  1584. * gainf calibration if needed */
  1585. if ((ah->ah_cal_mask & AR5K_CALIBRATION_FULL) &&
  1586. (ah->ah_radio == AR5K_RF5111 ||
  1587. ah->ah_radio == AR5K_RF5112) &&
  1588. channel->hw_value != AR5K_MODE_11B)
  1589. ath5k_hw_request_rfgain_probe(ah);
  1590. /* Update noise floor */
  1591. if (!(ah->ah_cal_mask & AR5K_CALIBRATION_NF))
  1592. ath5k_hw_update_noise_floor(ah);
  1593. return ret;
  1594. }
  1595. /***************************\
  1596. * Spur mitigation functions *
  1597. \***************************/
  1598. /**
  1599. * ath5k_hw_set_spur_mitigation_filter() - Configure SPUR filter
  1600. * @ah: The &struct ath5k_hw
  1601. * @channel: The &struct ieee80211_channel
  1602. *
  1603. * This function gets called during PHY initialization to
  1604. * configure the spur filter for the given channel. Spur is noise
  1605. * generated due to "reflection" effects, for more information on this
  1606. * method check out patent US7643810
  1607. */
  1608. static void
  1609. ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,
  1610. struct ieee80211_channel *channel)
  1611. {
  1612. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1613. u32 mag_mask[4] = {0, 0, 0, 0};
  1614. u32 pilot_mask[2] = {0, 0};
  1615. /* Note: fbin values are scaled up by 2 */
  1616. u16 spur_chan_fbin, chan_fbin, symbol_width, spur_detection_window;
  1617. s32 spur_delta_phase, spur_freq_sigma_delta;
  1618. s32 spur_offset, num_symbols_x16;
  1619. u8 num_symbol_offsets, i, freq_band;
  1620. /* Convert current frequency to fbin value (the same way channels
  1621. * are stored on EEPROM, check out ath5k_eeprom_bin2freq) and scale
  1622. * up by 2 so we can compare it later */
  1623. if (channel->band == NL80211_BAND_2GHZ) {
  1624. chan_fbin = (channel->center_freq - 2300) * 10;
  1625. freq_band = AR5K_EEPROM_BAND_2GHZ;
  1626. } else {
  1627. chan_fbin = (channel->center_freq - 4900) * 10;
  1628. freq_band = AR5K_EEPROM_BAND_5GHZ;
  1629. }
  1630. /* Check if any spur_chan_fbin from EEPROM is
  1631. * within our current channel's spur detection range */
  1632. spur_chan_fbin = AR5K_EEPROM_NO_SPUR;
  1633. spur_detection_window = AR5K_SPUR_CHAN_WIDTH;
  1634. /* XXX: Half/Quarter channels ?*/
  1635. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  1636. spur_detection_window *= 2;
  1637. for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
  1638. spur_chan_fbin = ee->ee_spur_chans[i][freq_band];
  1639. /* Note: mask cleans AR5K_EEPROM_NO_SPUR flag
  1640. * so it's zero if we got nothing from EEPROM */
  1641. if (spur_chan_fbin == AR5K_EEPROM_NO_SPUR) {
  1642. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1643. break;
  1644. }
  1645. if ((chan_fbin - spur_detection_window <=
  1646. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK)) &&
  1647. (chan_fbin + spur_detection_window >=
  1648. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK))) {
  1649. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1650. break;
  1651. }
  1652. }
  1653. /* We need to enable spur filter for this channel */
  1654. if (spur_chan_fbin) {
  1655. spur_offset = spur_chan_fbin - chan_fbin;
  1656. /*
  1657. * Calculate deltas:
  1658. * spur_freq_sigma_delta -> spur_offset / sample_freq << 21
  1659. * spur_delta_phase -> spur_offset / chip_freq << 11
  1660. * Note: Both values have 100Hz resolution
  1661. */
  1662. switch (ah->ah_bwmode) {
  1663. case AR5K_BWMODE_40MHZ:
  1664. /* Both sample_freq and chip_freq are 80MHz */
  1665. spur_delta_phase = (spur_offset << 16) / 25;
  1666. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1667. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz * 2;
  1668. break;
  1669. case AR5K_BWMODE_10MHZ:
  1670. /* Both sample_freq and chip_freq are 20MHz (?) */
  1671. spur_delta_phase = (spur_offset << 18) / 25;
  1672. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1673. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 2;
  1674. break;
  1675. case AR5K_BWMODE_5MHZ:
  1676. /* Both sample_freq and chip_freq are 10MHz (?) */
  1677. spur_delta_phase = (spur_offset << 19) / 25;
  1678. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1679. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 4;
  1680. break;
  1681. default:
  1682. if (channel->band == NL80211_BAND_5GHZ) {
  1683. /* Both sample_freq and chip_freq are 40MHz */
  1684. spur_delta_phase = (spur_offset << 17) / 25;
  1685. spur_freq_sigma_delta =
  1686. (spur_delta_phase >> 10);
  1687. symbol_width =
  1688. AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1689. } else {
  1690. /* sample_freq -> 40MHz chip_freq -> 44MHz
  1691. * (for b compatibility) */
  1692. spur_delta_phase = (spur_offset << 17) / 25;
  1693. spur_freq_sigma_delta =
  1694. (spur_offset << 8) / 55;
  1695. symbol_width =
  1696. AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1697. }
  1698. break;
  1699. }
  1700. /* Calculate pilot and magnitude masks */
  1701. /* Scale up spur_offset by 1000 to switch to 100HZ resolution
  1702. * and divide by symbol_width to find how many symbols we have
  1703. * Note: number of symbols is scaled up by 16 */
  1704. num_symbols_x16 = ((spur_offset * 1000) << 4) / symbol_width;
  1705. /* Spur is on a symbol if num_symbols_x16 % 16 is zero */
  1706. if (!(num_symbols_x16 & 0xF))
  1707. /* _X_ */
  1708. num_symbol_offsets = 3;
  1709. else
  1710. /* _xx_ */
  1711. num_symbol_offsets = 4;
  1712. for (i = 0; i < num_symbol_offsets; i++) {
  1713. /* Calculate pilot mask */
  1714. s32 curr_sym_off =
  1715. (num_symbols_x16 / 16) + i + 25;
  1716. /* Pilot magnitude mask seems to be a way to
  1717. * declare the boundaries for our detection
  1718. * window or something, it's 2 for the middle
  1719. * value(s) where the symbol is expected to be
  1720. * and 1 on the boundary values */
  1721. u8 plt_mag_map =
  1722. (i == 0 || i == (num_symbol_offsets - 1))
  1723. ? 1 : 2;
  1724. if (curr_sym_off >= 0 && curr_sym_off <= 32) {
  1725. if (curr_sym_off <= 25)
  1726. pilot_mask[0] |= 1 << curr_sym_off;
  1727. else if (curr_sym_off >= 27)
  1728. pilot_mask[0] |= 1 << (curr_sym_off - 1);
  1729. } else if (curr_sym_off >= 33 && curr_sym_off <= 52)
  1730. pilot_mask[1] |= 1 << (curr_sym_off - 33);
  1731. /* Calculate magnitude mask (for viterbi decoder) */
  1732. if (curr_sym_off >= -1 && curr_sym_off <= 14)
  1733. mag_mask[0] |=
  1734. plt_mag_map << (curr_sym_off + 1) * 2;
  1735. else if (curr_sym_off >= 15 && curr_sym_off <= 30)
  1736. mag_mask[1] |=
  1737. plt_mag_map << (curr_sym_off - 15) * 2;
  1738. else if (curr_sym_off >= 31 && curr_sym_off <= 46)
  1739. mag_mask[2] |=
  1740. plt_mag_map << (curr_sym_off - 31) * 2;
  1741. else if (curr_sym_off >= 47 && curr_sym_off <= 53)
  1742. mag_mask[3] |=
  1743. plt_mag_map << (curr_sym_off - 47) * 2;
  1744. }
  1745. /* Write settings on hw to enable spur filter */
  1746. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1747. AR5K_PHY_BIN_MASK_CTL_RATE, 0xff);
  1748. /* XXX: Self correlator also ? */
  1749. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1750. AR5K_PHY_IQ_PILOT_MASK_EN |
  1751. AR5K_PHY_IQ_CHAN_MASK_EN |
  1752. AR5K_PHY_IQ_SPUR_FILT_EN);
  1753. /* Set delta phase and freq sigma delta */
  1754. ath5k_hw_reg_write(ah,
  1755. AR5K_REG_SM(spur_delta_phase,
  1756. AR5K_PHY_TIMING_11_SPUR_DELTA_PHASE) |
  1757. AR5K_REG_SM(spur_freq_sigma_delta,
  1758. AR5K_PHY_TIMING_11_SPUR_FREQ_SD) |
  1759. AR5K_PHY_TIMING_11_USE_SPUR_IN_AGC,
  1760. AR5K_PHY_TIMING_11);
  1761. /* Write pilot masks */
  1762. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_7);
  1763. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1764. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1765. pilot_mask[1]);
  1766. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_9);
  1767. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1768. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1769. pilot_mask[1]);
  1770. /* Write magnitude masks */
  1771. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK_1);
  1772. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK_2);
  1773. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK_3);
  1774. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1775. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1776. mag_mask[3]);
  1777. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK2_1);
  1778. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK2_2);
  1779. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK2_3);
  1780. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1781. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1782. mag_mask[3]);
  1783. } else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) &
  1784. AR5K_PHY_IQ_SPUR_FILT_EN) {
  1785. /* Clean up spur mitigation settings and disable filter */
  1786. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1787. AR5K_PHY_BIN_MASK_CTL_RATE, 0);
  1788. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_IQ,
  1789. AR5K_PHY_IQ_PILOT_MASK_EN |
  1790. AR5K_PHY_IQ_CHAN_MASK_EN |
  1791. AR5K_PHY_IQ_SPUR_FILT_EN);
  1792. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_11);
  1793. /* Clear pilot masks */
  1794. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_7);
  1795. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1796. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1797. 0);
  1798. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_9);
  1799. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1800. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1801. 0);
  1802. /* Clear magnitude masks */
  1803. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_1);
  1804. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_2);
  1805. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_3);
  1806. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1807. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1808. 0);
  1809. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_1);
  1810. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_2);
  1811. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_3);
  1812. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1813. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1814. 0);
  1815. }
  1816. }
  1817. /*****************\
  1818. * Antenna control *
  1819. \*****************/
  1820. /**
  1821. * DOC: Antenna control
  1822. *
  1823. * Hw supports up to 14 antennas ! I haven't found any card that implements
  1824. * that. The maximum number of antennas I've seen is up to 4 (2 for 2GHz and 2
  1825. * for 5GHz). Antenna 1 (MAIN) should be omnidirectional, 2 (AUX)
  1826. * omnidirectional or sectorial and antennas 3-14 sectorial (or directional).
  1827. *
  1828. * We can have a single antenna for RX and multiple antennas for TX.
  1829. * RX antenna is our "default" antenna (usually antenna 1) set on
  1830. * DEFAULT_ANTENNA register and TX antenna is set on each TX control descriptor
  1831. * (0 for automatic selection, 1 - 14 antenna number).
  1832. *
  1833. * We can let hw do all the work doing fast antenna diversity for both
  1834. * tx and rx or we can do things manually. Here are the options we have
  1835. * (all are bits of STA_ID1 register):
  1836. *
  1837. * AR5K_STA_ID1_DEFAULT_ANTENNA -> When 0 is set as the TX antenna on TX
  1838. * control descriptor, use the default antenna to transmit or else use the last
  1839. * antenna on which we received an ACK.
  1840. *
  1841. * AR5K_STA_ID1_DESC_ANTENNA -> Update default antenna after each TX frame to
  1842. * the antenna on which we got the ACK for that frame.
  1843. *
  1844. * AR5K_STA_ID1_RTS_DEF_ANTENNA -> Use default antenna for RTS or else use the
  1845. * one on the TX descriptor.
  1846. *
  1847. * AR5K_STA_ID1_SELFGEN_DEF_ANT -> Use default antenna for self generated frames
  1848. * (ACKs etc), or else use current antenna (the one we just used for TX).
  1849. *
  1850. * Using the above we support the following scenarios:
  1851. *
  1852. * AR5K_ANTMODE_DEFAULT -> Hw handles antenna diversity etc automatically
  1853. *
  1854. * AR5K_ANTMODE_FIXED_A -> Only antenna A (MAIN) is present
  1855. *
  1856. * AR5K_ANTMODE_FIXED_B -> Only antenna B (AUX) is present
  1857. *
  1858. * AR5K_ANTMODE_SINGLE_AP -> Sta locked on a single ap
  1859. *
  1860. * AR5K_ANTMODE_SECTOR_AP -> AP with tx antenna set on tx desc
  1861. *
  1862. * AR5K_ANTMODE_SECTOR_STA -> STA with tx antenna set on tx desc
  1863. *
  1864. * AR5K_ANTMODE_DEBUG Debug mode -A -> Rx, B-> Tx-
  1865. *
  1866. * Also note that when setting antenna to F on tx descriptor card inverts
  1867. * current tx antenna.
  1868. */
  1869. /**
  1870. * ath5k_hw_set_def_antenna() - Set default rx antenna on AR5211/5212 and newer
  1871. * @ah: The &struct ath5k_hw
  1872. * @ant: Antenna number
  1873. */
  1874. static void
  1875. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)
  1876. {
  1877. if (ah->ah_version != AR5K_AR5210)
  1878. ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);
  1879. }
  1880. /**
  1881. * ath5k_hw_set_fast_div() - Enable/disable fast rx antenna diversity
  1882. * @ah: The &struct ath5k_hw
  1883. * @ee_mode: One of enum ath5k_driver_mode
  1884. * @enable: True to enable, false to disable
  1885. */
  1886. static void
  1887. ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)
  1888. {
  1889. switch (ee_mode) {
  1890. case AR5K_EEPROM_MODE_11G:
  1891. /* XXX: This is set to
  1892. * disabled on initvals !!! */
  1893. case AR5K_EEPROM_MODE_11A:
  1894. if (enable)
  1895. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1896. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1897. else
  1898. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1899. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1900. break;
  1901. case AR5K_EEPROM_MODE_11B:
  1902. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1903. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1904. break;
  1905. default:
  1906. return;
  1907. }
  1908. if (enable) {
  1909. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1910. AR5K_PHY_RESTART_DIV_GC, 4);
  1911. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1912. AR5K_PHY_FAST_ANT_DIV_EN);
  1913. } else {
  1914. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1915. AR5K_PHY_RESTART_DIV_GC, 0);
  1916. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1917. AR5K_PHY_FAST_ANT_DIV_EN);
  1918. }
  1919. }
  1920. /**
  1921. * ath5k_hw_set_antenna_switch() - Set up antenna switch table
  1922. * @ah: The &struct ath5k_hw
  1923. * @ee_mode: One of enum ath5k_driver_mode
  1924. *
  1925. * Switch table comes from EEPROM and includes information on controlling
  1926. * the 2 antenna RX attenuators
  1927. */
  1928. void
  1929. ath5k_hw_set_antenna_switch(struct ath5k_hw *ah, u8 ee_mode)
  1930. {
  1931. u8 ant0, ant1;
  1932. /*
  1933. * In case a fixed antenna was set as default
  1934. * use the same switch table twice.
  1935. */
  1936. if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
  1937. ant0 = ant1 = AR5K_ANT_SWTABLE_A;
  1938. else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
  1939. ant0 = ant1 = AR5K_ANT_SWTABLE_B;
  1940. else {
  1941. ant0 = AR5K_ANT_SWTABLE_A;
  1942. ant1 = AR5K_ANT_SWTABLE_B;
  1943. }
  1944. /* Set antenna idle switch table */
  1945. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
  1946. AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
  1947. (ah->ah_ant_ctl[ee_mode][AR5K_ANT_CTL] |
  1948. AR5K_PHY_ANT_CTL_TXRX_EN));
  1949. /* Set antenna switch tables */
  1950. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant0],
  1951. AR5K_PHY_ANT_SWITCH_TABLE_0);
  1952. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant1],
  1953. AR5K_PHY_ANT_SWITCH_TABLE_1);
  1954. }
  1955. /**
  1956. * ath5k_hw_set_antenna_mode() - Set antenna operating mode
  1957. * @ah: The &struct ath5k_hw
  1958. * @ant_mode: One of enum ath5k_ant_mode
  1959. */
  1960. void
  1961. ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)
  1962. {
  1963. struct ieee80211_channel *channel = ah->ah_current_channel;
  1964. bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;
  1965. bool use_def_for_sg;
  1966. int ee_mode;
  1967. u8 def_ant, tx_ant;
  1968. u32 sta_id1 = 0;
  1969. /* if channel is not initialized yet we can't set the antennas
  1970. * so just store the mode. it will be set on the next reset */
  1971. if (channel == NULL) {
  1972. ah->ah_ant_mode = ant_mode;
  1973. return;
  1974. }
  1975. def_ant = ah->ah_def_ant;
  1976. ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);
  1977. switch (ant_mode) {
  1978. case AR5K_ANTMODE_DEFAULT:
  1979. tx_ant = 0;
  1980. use_def_for_tx = false;
  1981. update_def_on_tx = false;
  1982. use_def_for_rts = false;
  1983. use_def_for_sg = false;
  1984. fast_div = true;
  1985. break;
  1986. case AR5K_ANTMODE_FIXED_A:
  1987. def_ant = 1;
  1988. tx_ant = 1;
  1989. use_def_for_tx = true;
  1990. update_def_on_tx = false;
  1991. use_def_for_rts = true;
  1992. use_def_for_sg = true;
  1993. fast_div = false;
  1994. break;
  1995. case AR5K_ANTMODE_FIXED_B:
  1996. def_ant = 2;
  1997. tx_ant = 2;
  1998. use_def_for_tx = true;
  1999. update_def_on_tx = false;
  2000. use_def_for_rts = true;
  2001. use_def_for_sg = true;
  2002. fast_div = false;
  2003. break;
  2004. case AR5K_ANTMODE_SINGLE_AP:
  2005. def_ant = 1; /* updated on tx */
  2006. tx_ant = 0;
  2007. use_def_for_tx = true;
  2008. update_def_on_tx = true;
  2009. use_def_for_rts = true;
  2010. use_def_for_sg = true;
  2011. fast_div = true;
  2012. break;
  2013. case AR5K_ANTMODE_SECTOR_AP:
  2014. tx_ant = 1; /* variable */
  2015. use_def_for_tx = false;
  2016. update_def_on_tx = false;
  2017. use_def_for_rts = true;
  2018. use_def_for_sg = false;
  2019. fast_div = false;
  2020. break;
  2021. case AR5K_ANTMODE_SECTOR_STA:
  2022. tx_ant = 1; /* variable */
  2023. use_def_for_tx = true;
  2024. update_def_on_tx = false;
  2025. use_def_for_rts = true;
  2026. use_def_for_sg = false;
  2027. fast_div = true;
  2028. break;
  2029. case AR5K_ANTMODE_DEBUG:
  2030. def_ant = 1;
  2031. tx_ant = 2;
  2032. use_def_for_tx = false;
  2033. update_def_on_tx = false;
  2034. use_def_for_rts = false;
  2035. use_def_for_sg = false;
  2036. fast_div = false;
  2037. break;
  2038. default:
  2039. return;
  2040. }
  2041. ah->ah_tx_ant = tx_ant;
  2042. ah->ah_ant_mode = ant_mode;
  2043. ah->ah_def_ant = def_ant;
  2044. sta_id1 |= use_def_for_tx ? AR5K_STA_ID1_DEFAULT_ANTENNA : 0;
  2045. sta_id1 |= update_def_on_tx ? AR5K_STA_ID1_DESC_ANTENNA : 0;
  2046. sta_id1 |= use_def_for_rts ? AR5K_STA_ID1_RTS_DEF_ANTENNA : 0;
  2047. sta_id1 |= use_def_for_sg ? AR5K_STA_ID1_SELFGEN_DEF_ANT : 0;
  2048. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_ANTENNA_SETTINGS);
  2049. if (sta_id1)
  2050. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, sta_id1);
  2051. ath5k_hw_set_antenna_switch(ah, ee_mode);
  2052. /* Note: set diversity before default antenna
  2053. * because it won't work correctly */
  2054. ath5k_hw_set_fast_div(ah, ee_mode, fast_div);
  2055. ath5k_hw_set_def_antenna(ah, def_ant);
  2056. }
  2057. /****************\
  2058. * TX power setup *
  2059. \****************/
  2060. /*
  2061. * Helper functions
  2062. */
  2063. /**
  2064. * ath5k_get_interpolated_value() - Get interpolated Y val between two points
  2065. * @target: X value of the middle point
  2066. * @x_left: X value of the left point
  2067. * @x_right: X value of the right point
  2068. * @y_left: Y value of the left point
  2069. * @y_right: Y value of the right point
  2070. */
  2071. static s16
  2072. ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
  2073. s16 y_left, s16 y_right)
  2074. {
  2075. s16 ratio, result;
  2076. /* Avoid divide by zero and skip interpolation
  2077. * if we have the same point */
  2078. if ((x_left == x_right) || (y_left == y_right))
  2079. return y_left;
  2080. /*
  2081. * Since we use ints and not fps, we need to scale up in
  2082. * order to get a sane ratio value (or else we 'll eg. get
  2083. * always 1 instead of 1.25, 1.75 etc). We scale up by 100
  2084. * to have some accuracy both for 0.5 and 0.25 steps.
  2085. */
  2086. ratio = ((100 * y_right - 100 * y_left) / (x_right - x_left));
  2087. /* Now scale down to be in range */
  2088. result = y_left + (ratio * (target - x_left) / 100);
  2089. return result;
  2090. }
  2091. /**
  2092. * ath5k_get_linear_pcdac_min() - Find vertical boundary (min pwr) for the
  2093. * linear PCDAC curve
  2094. * @stepL: Left array with y values (pcdac steps)
  2095. * @stepR: Right array with y values (pcdac steps)
  2096. * @pwrL: Left array with x values (power steps)
  2097. * @pwrR: Right array with x values (power steps)
  2098. *
  2099. * Since we have the top of the curve and we draw the line below
  2100. * until we reach 1 (1 pcdac step) we need to know which point
  2101. * (x value) that is so that we don't go below x axis and have negative
  2102. * pcdac values when creating the curve, or fill the table with zeros.
  2103. */
  2104. static s16
  2105. ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
  2106. const s16 *pwrL, const s16 *pwrR)
  2107. {
  2108. s8 tmp;
  2109. s16 min_pwrL, min_pwrR;
  2110. s16 pwr_i;
  2111. /* Some vendors write the same pcdac value twice !!! */
  2112. if (stepL[0] == stepL[1] || stepR[0] == stepR[1])
  2113. return max(pwrL[0], pwrR[0]);
  2114. if (pwrL[0] == pwrL[1])
  2115. min_pwrL = pwrL[0];
  2116. else {
  2117. pwr_i = pwrL[0];
  2118. do {
  2119. pwr_i--;
  2120. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  2121. pwrL[0], pwrL[1],
  2122. stepL[0], stepL[1]);
  2123. } while (tmp > 1);
  2124. min_pwrL = pwr_i;
  2125. }
  2126. if (pwrR[0] == pwrR[1])
  2127. min_pwrR = pwrR[0];
  2128. else {
  2129. pwr_i = pwrR[0];
  2130. do {
  2131. pwr_i--;
  2132. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  2133. pwrR[0], pwrR[1],
  2134. stepR[0], stepR[1]);
  2135. } while (tmp > 1);
  2136. min_pwrR = pwr_i;
  2137. }
  2138. /* Keep the right boundary so that it works for both curves */
  2139. return max(min_pwrL, min_pwrR);
  2140. }
  2141. /**
  2142. * ath5k_create_power_curve() - Create a Power to PDADC or PCDAC curve
  2143. * @pmin: Minimum power value (xmin)
  2144. * @pmax: Maximum power value (xmax)
  2145. * @pwr: Array of power steps (x values)
  2146. * @vpd: Array of matching PCDAC/PDADC steps (y values)
  2147. * @num_points: Number of provided points
  2148. * @vpd_table: Array to fill with the full PCDAC/PDADC values (y values)
  2149. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2150. *
  2151. * Interpolate (pwr,vpd) points to create a Power to PDADC or a
  2152. * Power to PCDAC curve.
  2153. *
  2154. * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
  2155. * steps (offsets) on y axis. Power can go up to 31.5dB and max
  2156. * PCDAC/PDADC step for each curve is 64 but we can write more than
  2157. * one curves on hw so we can go up to 128 (which is the max step we
  2158. * can write on the final table).
  2159. *
  2160. * We write y values (PCDAC/PDADC steps) on hw.
  2161. */
  2162. static void
  2163. ath5k_create_power_curve(s16 pmin, s16 pmax,
  2164. const s16 *pwr, const u8 *vpd,
  2165. u8 num_points,
  2166. u8 *vpd_table, u8 type)
  2167. {
  2168. u8 idx[2] = { 0, 1 };
  2169. s16 pwr_i = 2 * pmin;
  2170. int i;
  2171. if (num_points < 2)
  2172. return;
  2173. /* We want the whole line, so adjust boundaries
  2174. * to cover the entire power range. Note that
  2175. * power values are already 0.25dB so no need
  2176. * to multiply pwr_i by 2 */
  2177. if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
  2178. pwr_i = pmin;
  2179. pmin = 0;
  2180. pmax = 63;
  2181. }
  2182. /* Find surrounding turning points (TPs)
  2183. * and interpolate between them */
  2184. for (i = 0; (i <= (u16) (pmax - pmin)) &&
  2185. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  2186. /* We passed the right TP, move to the next set of TPs
  2187. * if we pass the last TP, extrapolate above using the last
  2188. * two TPs for ratio */
  2189. if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
  2190. idx[0]++;
  2191. idx[1]++;
  2192. }
  2193. vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
  2194. pwr[idx[0]], pwr[idx[1]],
  2195. vpd[idx[0]], vpd[idx[1]]);
  2196. /* Increase by 0.5dB
  2197. * (0.25 dB units) */
  2198. pwr_i += 2;
  2199. }
  2200. }
  2201. /**
  2202. * ath5k_get_chan_pcal_surrounding_piers() - Get surrounding calibration piers
  2203. * for a given channel.
  2204. * @ah: The &struct ath5k_hw
  2205. * @channel: The &struct ieee80211_channel
  2206. * @pcinfo_l: The &struct ath5k_chan_pcal_info to put the left cal. pier
  2207. * @pcinfo_r: The &struct ath5k_chan_pcal_info to put the right cal. pier
  2208. *
  2209. * Get the surrounding per-channel power calibration piers
  2210. * for a given frequency so that we can interpolate between
  2211. * them and come up with an appropriate dataset for our current
  2212. * channel.
  2213. */
  2214. static void
  2215. ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
  2216. struct ieee80211_channel *channel,
  2217. struct ath5k_chan_pcal_info **pcinfo_l,
  2218. struct ath5k_chan_pcal_info **pcinfo_r)
  2219. {
  2220. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2221. struct ath5k_chan_pcal_info *pcinfo;
  2222. u8 idx_l, idx_r;
  2223. u8 mode, max, i;
  2224. u32 target = channel->center_freq;
  2225. idx_l = 0;
  2226. idx_r = 0;
  2227. switch (channel->hw_value) {
  2228. case AR5K_EEPROM_MODE_11A:
  2229. pcinfo = ee->ee_pwr_cal_a;
  2230. mode = AR5K_EEPROM_MODE_11A;
  2231. break;
  2232. case AR5K_EEPROM_MODE_11B:
  2233. pcinfo = ee->ee_pwr_cal_b;
  2234. mode = AR5K_EEPROM_MODE_11B;
  2235. break;
  2236. case AR5K_EEPROM_MODE_11G:
  2237. default:
  2238. pcinfo = ee->ee_pwr_cal_g;
  2239. mode = AR5K_EEPROM_MODE_11G;
  2240. break;
  2241. }
  2242. max = ee->ee_n_piers[mode] - 1;
  2243. /* Frequency is below our calibrated
  2244. * range. Use the lowest power curve
  2245. * we have */
  2246. if (target < pcinfo[0].freq) {
  2247. idx_l = idx_r = 0;
  2248. goto done;
  2249. }
  2250. /* Frequency is above our calibrated
  2251. * range. Use the highest power curve
  2252. * we have */
  2253. if (target > pcinfo[max].freq) {
  2254. idx_l = idx_r = max;
  2255. goto done;
  2256. }
  2257. /* Frequency is inside our calibrated
  2258. * channel range. Pick the surrounding
  2259. * calibration piers so that we can
  2260. * interpolate */
  2261. for (i = 0; i <= max; i++) {
  2262. /* Frequency matches one of our calibration
  2263. * piers, no need to interpolate, just use
  2264. * that calibration pier */
  2265. if (pcinfo[i].freq == target) {
  2266. idx_l = idx_r = i;
  2267. goto done;
  2268. }
  2269. /* We found a calibration pier that's above
  2270. * frequency, use this pier and the previous
  2271. * one to interpolate */
  2272. if (target < pcinfo[i].freq) {
  2273. idx_r = i;
  2274. idx_l = idx_r - 1;
  2275. goto done;
  2276. }
  2277. }
  2278. done:
  2279. *pcinfo_l = &pcinfo[idx_l];
  2280. *pcinfo_r = &pcinfo[idx_r];
  2281. }
  2282. /**
  2283. * ath5k_get_rate_pcal_data() - Get the interpolated per-rate power
  2284. * calibration data
  2285. * @ah: The &struct ath5k_hw *ah,
  2286. * @channel: The &struct ieee80211_channel
  2287. * @rates: The &struct ath5k_rate_pcal_info to fill
  2288. *
  2289. * Get the surrounding per-rate power calibration data
  2290. * for a given frequency and interpolate between power
  2291. * values to set max target power supported by hw for
  2292. * each rate on this frequency.
  2293. */
  2294. static void
  2295. ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
  2296. struct ieee80211_channel *channel,
  2297. struct ath5k_rate_pcal_info *rates)
  2298. {
  2299. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2300. struct ath5k_rate_pcal_info *rpinfo;
  2301. u8 idx_l, idx_r;
  2302. u8 mode, max, i;
  2303. u32 target = channel->center_freq;
  2304. idx_l = 0;
  2305. idx_r = 0;
  2306. switch (channel->hw_value) {
  2307. case AR5K_MODE_11A:
  2308. rpinfo = ee->ee_rate_tpwr_a;
  2309. mode = AR5K_EEPROM_MODE_11A;
  2310. break;
  2311. case AR5K_MODE_11B:
  2312. rpinfo = ee->ee_rate_tpwr_b;
  2313. mode = AR5K_EEPROM_MODE_11B;
  2314. break;
  2315. case AR5K_MODE_11G:
  2316. default:
  2317. rpinfo = ee->ee_rate_tpwr_g;
  2318. mode = AR5K_EEPROM_MODE_11G;
  2319. break;
  2320. }
  2321. max = ee->ee_rate_target_pwr_num[mode] - 1;
  2322. /* Get the surrounding calibration
  2323. * piers - same as above */
  2324. if (target < rpinfo[0].freq) {
  2325. idx_l = idx_r = 0;
  2326. goto done;
  2327. }
  2328. if (target > rpinfo[max].freq) {
  2329. idx_l = idx_r = max;
  2330. goto done;
  2331. }
  2332. for (i = 0; i <= max; i++) {
  2333. if (rpinfo[i].freq == target) {
  2334. idx_l = idx_r = i;
  2335. goto done;
  2336. }
  2337. if (target < rpinfo[i].freq) {
  2338. idx_r = i;
  2339. idx_l = idx_r - 1;
  2340. goto done;
  2341. }
  2342. }
  2343. done:
  2344. /* Now interpolate power value, based on the frequency */
  2345. rates->freq = target;
  2346. rates->target_power_6to24 =
  2347. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2348. rpinfo[idx_r].freq,
  2349. rpinfo[idx_l].target_power_6to24,
  2350. rpinfo[idx_r].target_power_6to24);
  2351. rates->target_power_36 =
  2352. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2353. rpinfo[idx_r].freq,
  2354. rpinfo[idx_l].target_power_36,
  2355. rpinfo[idx_r].target_power_36);
  2356. rates->target_power_48 =
  2357. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2358. rpinfo[idx_r].freq,
  2359. rpinfo[idx_l].target_power_48,
  2360. rpinfo[idx_r].target_power_48);
  2361. rates->target_power_54 =
  2362. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2363. rpinfo[idx_r].freq,
  2364. rpinfo[idx_l].target_power_54,
  2365. rpinfo[idx_r].target_power_54);
  2366. }
  2367. /**
  2368. * ath5k_get_max_ctl_power() - Get max edge power for a given frequency
  2369. * @ah: the &struct ath5k_hw
  2370. * @channel: The &struct ieee80211_channel
  2371. *
  2372. * Get the max edge power for this channel if
  2373. * we have such data from EEPROM's Conformance Test
  2374. * Limits (CTL), and limit max power if needed.
  2375. */
  2376. static void
  2377. ath5k_get_max_ctl_power(struct ath5k_hw *ah,
  2378. struct ieee80211_channel *channel)
  2379. {
  2380. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  2381. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2382. struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
  2383. u8 *ctl_val = ee->ee_ctl;
  2384. s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
  2385. s16 edge_pwr = 0;
  2386. u8 rep_idx;
  2387. u8 i, ctl_mode;
  2388. u8 ctl_idx = 0xFF;
  2389. u32 target = channel->center_freq;
  2390. ctl_mode = ath_regd_get_band_ctl(regulatory, channel->band);
  2391. switch (channel->hw_value) {
  2392. case AR5K_MODE_11A:
  2393. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  2394. ctl_mode |= AR5K_CTL_TURBO;
  2395. else
  2396. ctl_mode |= AR5K_CTL_11A;
  2397. break;
  2398. case AR5K_MODE_11G:
  2399. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  2400. ctl_mode |= AR5K_CTL_TURBOG;
  2401. else
  2402. ctl_mode |= AR5K_CTL_11G;
  2403. break;
  2404. case AR5K_MODE_11B:
  2405. ctl_mode |= AR5K_CTL_11B;
  2406. break;
  2407. default:
  2408. return;
  2409. }
  2410. for (i = 0; i < ee->ee_ctls; i++) {
  2411. if (ctl_val[i] == ctl_mode) {
  2412. ctl_idx = i;
  2413. break;
  2414. }
  2415. }
  2416. /* If we have a CTL dataset available grab it and find the
  2417. * edge power for our frequency */
  2418. if (ctl_idx == 0xFF)
  2419. return;
  2420. /* Edge powers are sorted by frequency from lower
  2421. * to higher. Each CTL corresponds to 8 edge power
  2422. * measurements. */
  2423. rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
  2424. /* Don't do boundaries check because we
  2425. * might have more that one bands defined
  2426. * for this mode */
  2427. /* Get the edge power that's closer to our
  2428. * frequency */
  2429. for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
  2430. rep_idx += i;
  2431. if (target <= rep[rep_idx].freq)
  2432. edge_pwr = (s16) rep[rep_idx].edge;
  2433. }
  2434. if (edge_pwr)
  2435. ah->ah_txpower.txp_max_pwr = 4 * min(edge_pwr, max_chan_pwr);
  2436. }
  2437. /*
  2438. * Power to PCDAC table functions
  2439. */
  2440. /**
  2441. * DOC: Power to PCDAC table functions
  2442. *
  2443. * For RF5111 we have an XPD -eXternal Power Detector- curve
  2444. * for each calibrated channel. Each curve has 0,5dB Power steps
  2445. * on x axis and PCDAC steps (offsets) on y axis and looks like an
  2446. * exponential function. To recreate the curve we read 11 points
  2447. * from eeprom (eeprom.c) and interpolate here.
  2448. *
  2449. * For RF5112 we have 4 XPD -eXternal Power Detector- curves
  2450. * for each calibrated channel on 0, -6, -12 and -18dBm but we only
  2451. * use the higher (3) and the lower (0) curves. Each curve again has 0.5dB
  2452. * power steps on x axis and PCDAC steps on y axis and looks like a
  2453. * linear function. To recreate the curve and pass the power values
  2454. * on hw, we get 4 points for xpd 0 (lower gain -> max power)
  2455. * and 3 points for xpd 3 (higher gain -> lower power) from eeprom (eeprom.c)
  2456. * and interpolate here.
  2457. *
  2458. * For a given channel we get the calibrated points (piers) for it or
  2459. * -if we don't have calibration data for this specific channel- from the
  2460. * available surrounding channels we have calibration data for, after we do a
  2461. * linear interpolation between them. Then since we have our calibrated points
  2462. * for this channel, we do again a linear interpolation between them to get the
  2463. * whole curve.
  2464. *
  2465. * We finally write the Y values of the curve(s) (the PCDAC values) on hw
  2466. */
  2467. /**
  2468. * ath5k_fill_pwr_to_pcdac_table() - Fill Power to PCDAC table on RF5111
  2469. * @ah: The &struct ath5k_hw
  2470. * @table_min: Minimum power (x min)
  2471. * @table_max: Maximum power (x max)
  2472. *
  2473. * No further processing is needed for RF5111, the only thing we have to
  2474. * do is fill the values below and above calibration range since eeprom data
  2475. * may not cover the entire PCDAC table.
  2476. */
  2477. static void
  2478. ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
  2479. s16 *table_max)
  2480. {
  2481. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2482. u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
  2483. u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
  2484. s16 min_pwr, max_pwr;
  2485. /* Get table boundaries */
  2486. min_pwr = table_min[0];
  2487. pcdac_0 = pcdac_tmp[0];
  2488. max_pwr = table_max[0];
  2489. pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
  2490. /* Extrapolate below minimum using pcdac_0 */
  2491. pcdac_i = 0;
  2492. for (i = 0; i < min_pwr; i++)
  2493. pcdac_out[pcdac_i++] = pcdac_0;
  2494. /* Copy values from pcdac_tmp */
  2495. pwr_idx = min_pwr;
  2496. for (i = 0; pwr_idx <= max_pwr &&
  2497. pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
  2498. pcdac_out[pcdac_i++] = pcdac_tmp[i];
  2499. pwr_idx++;
  2500. }
  2501. /* Extrapolate above maximum */
  2502. while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
  2503. pcdac_out[pcdac_i++] = pcdac_n;
  2504. }
  2505. /**
  2506. * ath5k_combine_linear_pcdac_curves() - Combine available PCDAC Curves
  2507. * @ah: The &struct ath5k_hw
  2508. * @table_min: Minimum power (x min)
  2509. * @table_max: Maximum power (x max)
  2510. * @pdcurves: Number of pd curves
  2511. *
  2512. * Combine available XPD Curves and fill Linear Power to PCDAC table on RF5112
  2513. * RFX112 can have up to 2 curves (one for low txpower range and one for
  2514. * higher txpower range). We need to put them both on pcdac_out and place
  2515. * them in the correct location. In case we only have one curve available
  2516. * just fit it on pcdac_out (it's supposed to cover the entire range of
  2517. * available pwr levels since it's always the higher power curve). Extrapolate
  2518. * below and above final table if needed.
  2519. */
  2520. static void
  2521. ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
  2522. s16 *table_max, u8 pdcurves)
  2523. {
  2524. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2525. u8 *pcdac_low_pwr;
  2526. u8 *pcdac_high_pwr;
  2527. u8 *pcdac_tmp;
  2528. u8 pwr;
  2529. s16 max_pwr_idx;
  2530. s16 min_pwr_idx;
  2531. s16 mid_pwr_idx = 0;
  2532. /* Edge flag turns on the 7nth bit on the PCDAC
  2533. * to declare the higher power curve (force values
  2534. * to be greater than 64). If we only have one curve
  2535. * we don't need to set this, if we have 2 curves and
  2536. * fill the table backwards this can also be used to
  2537. * switch from higher power curve to lower power curve */
  2538. u8 edge_flag;
  2539. int i;
  2540. /* When we have only one curve available
  2541. * that's the higher power curve. If we have
  2542. * two curves the first is the high power curve
  2543. * and the next is the low power curve. */
  2544. if (pdcurves > 1) {
  2545. pcdac_low_pwr = ah->ah_txpower.tmpL[1];
  2546. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2547. mid_pwr_idx = table_max[1] - table_min[1] - 1;
  2548. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2549. /* If table size goes beyond 31.5dB, keep the
  2550. * upper 31.5dB range when setting tx power.
  2551. * Note: 126 = 31.5 dB in quarter dB steps */
  2552. if (table_max[0] - table_min[1] > 126)
  2553. min_pwr_idx = table_max[0] - 126;
  2554. else
  2555. min_pwr_idx = table_min[1];
  2556. /* Since we fill table backwards
  2557. * start from high power curve */
  2558. pcdac_tmp = pcdac_high_pwr;
  2559. edge_flag = 0x40;
  2560. } else {
  2561. pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
  2562. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2563. min_pwr_idx = table_min[0];
  2564. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2565. pcdac_tmp = pcdac_high_pwr;
  2566. edge_flag = 0;
  2567. }
  2568. /* This is used when setting tx power*/
  2569. ah->ah_txpower.txp_min_idx = min_pwr_idx / 2;
  2570. /* Fill Power to PCDAC table backwards */
  2571. pwr = max_pwr_idx;
  2572. for (i = 63; i >= 0; i--) {
  2573. /* Entering lower power range, reset
  2574. * edge flag and set pcdac_tmp to lower
  2575. * power curve.*/
  2576. if (edge_flag == 0x40 &&
  2577. (2 * pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
  2578. edge_flag = 0x00;
  2579. pcdac_tmp = pcdac_low_pwr;
  2580. pwr = mid_pwr_idx / 2;
  2581. }
  2582. /* Don't go below 1, extrapolate below if we have
  2583. * already switched to the lower power curve -or
  2584. * we only have one curve and edge_flag is zero
  2585. * anyway */
  2586. if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
  2587. while (i >= 0) {
  2588. pcdac_out[i] = pcdac_out[i + 1];
  2589. i--;
  2590. }
  2591. break;
  2592. }
  2593. pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
  2594. /* Extrapolate above if pcdac is greater than
  2595. * 126 -this can happen because we OR pcdac_out
  2596. * value with edge_flag on high power curve */
  2597. if (pcdac_out[i] > 126)
  2598. pcdac_out[i] = 126;
  2599. /* Decrease by a 0.5dB step */
  2600. pwr--;
  2601. }
  2602. }
  2603. /**
  2604. * ath5k_write_pcdac_table() - Write the PCDAC values on hw
  2605. * @ah: The &struct ath5k_hw
  2606. */
  2607. static void
  2608. ath5k_write_pcdac_table(struct ath5k_hw *ah)
  2609. {
  2610. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2611. int i;
  2612. /*
  2613. * Write TX power values
  2614. */
  2615. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2616. ath5k_hw_reg_write(ah,
  2617. (((pcdac_out[2 * i + 0] << 8 | 0xff) & 0xffff) << 0) |
  2618. (((pcdac_out[2 * i + 1] << 8 | 0xff) & 0xffff) << 16),
  2619. AR5K_PHY_PCDAC_TXPOWER(i));
  2620. }
  2621. }
  2622. /*
  2623. * Power to PDADC table functions
  2624. */
  2625. /**
  2626. * DOC: Power to PDADC table functions
  2627. *
  2628. * For RF2413 and later we have a Power to PDADC table (Power Detector)
  2629. * instead of a PCDAC (Power Control) and 4 pd gain curves for each
  2630. * calibrated channel. Each curve has power on x axis in 0.5 db steps and
  2631. * PDADC steps on y axis and looks like an exponential function like the
  2632. * RF5111 curve.
  2633. *
  2634. * To recreate the curves we read the points from eeprom (eeprom.c)
  2635. * and interpolate here. Note that in most cases only 2 (higher and lower)
  2636. * curves are used (like RF5112) but vendors have the opportunity to include
  2637. * all 4 curves on eeprom. The final curve (higher power) has an extra
  2638. * point for better accuracy like RF5112.
  2639. *
  2640. * The process is similar to what we do above for RF5111/5112
  2641. */
  2642. /**
  2643. * ath5k_combine_pwr_to_pdadc_curves() - Combine the various PDADC curves
  2644. * @ah: The &struct ath5k_hw
  2645. * @pwr_min: Minimum power (x min)
  2646. * @pwr_max: Maximum power (x max)
  2647. * @pdcurves: Number of available curves
  2648. *
  2649. * Combine the various pd curves and create the final Power to PDADC table
  2650. * We can have up to 4 pd curves, we need to do a similar process
  2651. * as we do for RF5112. This time we don't have an edge_flag but we
  2652. * set the gain boundaries on a separate register.
  2653. */
  2654. static void
  2655. ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
  2656. s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
  2657. {
  2658. u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
  2659. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2660. u8 *pdadc_tmp;
  2661. s16 pdadc_0;
  2662. u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
  2663. u8 pd_gain_overlap;
  2664. /* Note: Register value is initialized on initvals
  2665. * there is no feedback from hw.
  2666. * XXX: What about pd_gain_overlap from EEPROM ? */
  2667. pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
  2668. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
  2669. /* Create final PDADC table */
  2670. for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
  2671. pdadc_tmp = ah->ah_txpower.tmpL[pdg];
  2672. if (pdg == pdcurves - 1)
  2673. /* 2 dB boundary stretch for last
  2674. * (higher power) curve */
  2675. gain_boundaries[pdg] = pwr_max[pdg] + 4;
  2676. else
  2677. /* Set gain boundary in the middle
  2678. * between this curve and the next one */
  2679. gain_boundaries[pdg] =
  2680. (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
  2681. /* Sanity check in case our 2 db stretch got out of
  2682. * range. */
  2683. if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
  2684. gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
  2685. /* For the first curve (lower power)
  2686. * start from 0 dB */
  2687. if (pdg == 0)
  2688. pdadc_0 = 0;
  2689. else
  2690. /* For the other curves use the gain overlap */
  2691. pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
  2692. pd_gain_overlap;
  2693. /* Force each power step to be at least 0.5 dB */
  2694. if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
  2695. pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
  2696. else
  2697. pwr_step = 1;
  2698. /* If pdadc_0 is negative, we need to extrapolate
  2699. * below this pdgain by a number of pwr_steps */
  2700. while ((pdadc_0 < 0) && (pdadc_i < 128)) {
  2701. s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
  2702. pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
  2703. pdadc_0++;
  2704. }
  2705. /* Set last pwr level, using gain boundaries */
  2706. pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
  2707. /* Limit it to be inside pwr range */
  2708. table_size = pwr_max[pdg] - pwr_min[pdg];
  2709. max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
  2710. /* Fill pdadc_out table */
  2711. while (pdadc_0 < max_idx && pdadc_i < 128)
  2712. pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
  2713. /* Need to extrapolate above this pdgain? */
  2714. if (pdadc_n <= max_idx)
  2715. continue;
  2716. /* Force each power step to be at least 0.5 dB */
  2717. if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
  2718. pwr_step = pdadc_tmp[table_size - 1] -
  2719. pdadc_tmp[table_size - 2];
  2720. else
  2721. pwr_step = 1;
  2722. /* Extrapolate above */
  2723. while ((pdadc_0 < (s16) pdadc_n) &&
  2724. (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
  2725. s16 tmp = pdadc_tmp[table_size - 1] +
  2726. (pdadc_0 - max_idx) * pwr_step;
  2727. pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
  2728. pdadc_0++;
  2729. }
  2730. }
  2731. while (pdg < AR5K_EEPROM_N_PD_GAINS) {
  2732. gain_boundaries[pdg] = gain_boundaries[pdg - 1];
  2733. pdg++;
  2734. }
  2735. while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
  2736. pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
  2737. pdadc_i++;
  2738. }
  2739. /* Set gain boundaries */
  2740. ath5k_hw_reg_write(ah,
  2741. AR5K_REG_SM(pd_gain_overlap,
  2742. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
  2743. AR5K_REG_SM(gain_boundaries[0],
  2744. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
  2745. AR5K_REG_SM(gain_boundaries[1],
  2746. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
  2747. AR5K_REG_SM(gain_boundaries[2],
  2748. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
  2749. AR5K_REG_SM(gain_boundaries[3],
  2750. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
  2751. AR5K_PHY_TPC_RG5);
  2752. /* Used for setting rate power table */
  2753. ah->ah_txpower.txp_min_idx = pwr_min[0];
  2754. }
  2755. /**
  2756. * ath5k_write_pwr_to_pdadc_table() - Write the PDADC values on hw
  2757. * @ah: The &struct ath5k_hw
  2758. * @ee_mode: One of enum ath5k_driver_mode
  2759. */
  2760. static void
  2761. ath5k_write_pwr_to_pdadc_table(struct ath5k_hw *ah, u8 ee_mode)
  2762. {
  2763. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2764. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2765. u8 *pdg_to_idx = ee->ee_pdc_to_idx[ee_mode];
  2766. u8 pdcurves = ee->ee_pd_gains[ee_mode];
  2767. u32 reg;
  2768. u8 i;
  2769. /* Select the right pdgain curves */
  2770. /* Clear current settings */
  2771. reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
  2772. reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
  2773. AR5K_PHY_TPC_RG1_PDGAIN_2 |
  2774. AR5K_PHY_TPC_RG1_PDGAIN_3 |
  2775. AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2776. /*
  2777. * Use pd_gains curve from eeprom
  2778. *
  2779. * This overrides the default setting from initvals
  2780. * in case some vendors (e.g. Zcomax) don't use the default
  2781. * curves. If we don't honor their settings we 'll get a
  2782. * 5dB (1 * gain overlap ?) drop.
  2783. */
  2784. reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2785. switch (pdcurves) {
  2786. case 3:
  2787. reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
  2788. /* Fall through */
  2789. case 2:
  2790. reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
  2791. /* Fall through */
  2792. case 1:
  2793. reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
  2794. break;
  2795. }
  2796. ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
  2797. /*
  2798. * Write TX power values
  2799. */
  2800. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2801. u32 val = get_unaligned_le32(&pdadc_out[4 * i]);
  2802. ath5k_hw_reg_write(ah, val, AR5K_PHY_PDADC_TXPOWER(i));
  2803. }
  2804. }
  2805. /*
  2806. * Common code for PCDAC/PDADC tables
  2807. */
  2808. /**
  2809. * ath5k_setup_channel_powertable() - Set up power table for this channel
  2810. * @ah: The &struct ath5k_hw
  2811. * @channel: The &struct ieee80211_channel
  2812. * @ee_mode: One of enum ath5k_driver_mode
  2813. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2814. *
  2815. * This is the main function that uses all of the above
  2816. * to set PCDAC/PDADC table on hw for the current channel.
  2817. * This table is used for tx power calibration on the baseband,
  2818. * without it we get weird tx power levels and in some cases
  2819. * distorted spectral mask
  2820. */
  2821. static int
  2822. ath5k_setup_channel_powertable(struct ath5k_hw *ah,
  2823. struct ieee80211_channel *channel,
  2824. u8 ee_mode, u8 type)
  2825. {
  2826. struct ath5k_pdgain_info *pdg_L, *pdg_R;
  2827. struct ath5k_chan_pcal_info *pcinfo_L;
  2828. struct ath5k_chan_pcal_info *pcinfo_R;
  2829. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2830. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  2831. s16 table_min[AR5K_EEPROM_N_PD_GAINS];
  2832. s16 table_max[AR5K_EEPROM_N_PD_GAINS];
  2833. u8 *tmpL;
  2834. u8 *tmpR;
  2835. u32 target = channel->center_freq;
  2836. int pdg, i;
  2837. /* Get surrounding freq piers for this channel */
  2838. ath5k_get_chan_pcal_surrounding_piers(ah, channel,
  2839. &pcinfo_L,
  2840. &pcinfo_R);
  2841. /* Loop over pd gain curves on
  2842. * surrounding freq piers by index */
  2843. for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
  2844. /* Fill curves in reverse order
  2845. * from lower power (max gain)
  2846. * to higher power. Use curve -> idx
  2847. * backmapping we did on eeprom init */
  2848. u8 idx = pdg_curve_to_idx[pdg];
  2849. /* Grab the needed curves by index */
  2850. pdg_L = &pcinfo_L->pd_curves[idx];
  2851. pdg_R = &pcinfo_R->pd_curves[idx];
  2852. /* Initialize the temp tables */
  2853. tmpL = ah->ah_txpower.tmpL[pdg];
  2854. tmpR = ah->ah_txpower.tmpR[pdg];
  2855. /* Set curve's x boundaries and create
  2856. * curves so that they cover the same
  2857. * range (if we don't do that one table
  2858. * will have values on some range and the
  2859. * other one won't have any so interpolation
  2860. * will fail) */
  2861. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2862. pdg_R->pd_pwr[0]) / 2;
  2863. table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2864. pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
  2865. /* Now create the curves on surrounding channels
  2866. * and interpolate if needed to get the final
  2867. * curve for this gain on this channel */
  2868. switch (type) {
  2869. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2870. /* Override min/max so that we don't loose
  2871. * accuracy (don't divide by 2) */
  2872. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2873. pdg_R->pd_pwr[0]);
  2874. table_max[pdg] =
  2875. max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2876. pdg_R->pd_pwr[pdg_R->pd_points - 1]);
  2877. /* Override minimum so that we don't get
  2878. * out of bounds while extrapolating
  2879. * below. Don't do this when we have 2
  2880. * curves and we are on the high power curve
  2881. * because table_min is ok in this case */
  2882. if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
  2883. table_min[pdg] =
  2884. ath5k_get_linear_pcdac_min(pdg_L->pd_step,
  2885. pdg_R->pd_step,
  2886. pdg_L->pd_pwr,
  2887. pdg_R->pd_pwr);
  2888. /* Don't go too low because we will
  2889. * miss the upper part of the curve.
  2890. * Note: 126 = 31.5dB (max power supported)
  2891. * in 0.25dB units */
  2892. if (table_max[pdg] - table_min[pdg] > 126)
  2893. table_min[pdg] = table_max[pdg] - 126;
  2894. }
  2895. /* Fall through */
  2896. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2897. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2898. ath5k_create_power_curve(table_min[pdg],
  2899. table_max[pdg],
  2900. pdg_L->pd_pwr,
  2901. pdg_L->pd_step,
  2902. pdg_L->pd_points, tmpL, type);
  2903. /* We are in a calibration
  2904. * pier, no need to interpolate
  2905. * between freq piers */
  2906. if (pcinfo_L == pcinfo_R)
  2907. continue;
  2908. ath5k_create_power_curve(table_min[pdg],
  2909. table_max[pdg],
  2910. pdg_R->pd_pwr,
  2911. pdg_R->pd_step,
  2912. pdg_R->pd_points, tmpR, type);
  2913. break;
  2914. default:
  2915. return -EINVAL;
  2916. }
  2917. /* Interpolate between curves
  2918. * of surrounding freq piers to
  2919. * get the final curve for this
  2920. * pd gain. Re-use tmpL for interpolation
  2921. * output */
  2922. for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
  2923. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  2924. tmpL[i] = (u8) ath5k_get_interpolated_value(target,
  2925. (s16) pcinfo_L->freq,
  2926. (s16) pcinfo_R->freq,
  2927. (s16) tmpL[i],
  2928. (s16) tmpR[i]);
  2929. }
  2930. }
  2931. /* Now we have a set of curves for this
  2932. * channel on tmpL (x range is table_max - table_min
  2933. * and y values are tmpL[pdg][]) sorted in the same
  2934. * order as EEPROM (because we've used the backmapping).
  2935. * So for RF5112 it's from higher power to lower power
  2936. * and for RF2413 it's from lower power to higher power.
  2937. * For RF5111 we only have one curve. */
  2938. /* Fill min and max power levels for this
  2939. * channel by interpolating the values on
  2940. * surrounding channels to complete the dataset */
  2941. ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
  2942. (s16) pcinfo_L->freq,
  2943. (s16) pcinfo_R->freq,
  2944. pcinfo_L->min_pwr, pcinfo_R->min_pwr);
  2945. ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
  2946. (s16) pcinfo_L->freq,
  2947. (s16) pcinfo_R->freq,
  2948. pcinfo_L->max_pwr, pcinfo_R->max_pwr);
  2949. /* Fill PCDAC/PDADC table */
  2950. switch (type) {
  2951. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2952. /* For RF5112 we can have one or two curves
  2953. * and each curve covers a certain power lvl
  2954. * range so we need to do some more processing */
  2955. ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
  2956. ee->ee_pd_gains[ee_mode]);
  2957. /* Set txp.offset so that we can
  2958. * match max power value with max
  2959. * table index */
  2960. ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
  2961. break;
  2962. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2963. /* We are done for RF5111 since it has only
  2964. * one curve, just fit the curve on the table */
  2965. ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
  2966. /* No rate powertable adjustment for RF5111 */
  2967. ah->ah_txpower.txp_min_idx = 0;
  2968. ah->ah_txpower.txp_offset = 0;
  2969. break;
  2970. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2971. /* Set PDADC boundaries and fill
  2972. * final PDADC table */
  2973. ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
  2974. ee->ee_pd_gains[ee_mode]);
  2975. /* Set txp.offset, note that table_min
  2976. * can be negative */
  2977. ah->ah_txpower.txp_offset = table_min[0];
  2978. break;
  2979. default:
  2980. return -EINVAL;
  2981. }
  2982. ah->ah_txpower.txp_setup = true;
  2983. return 0;
  2984. }
  2985. /**
  2986. * ath5k_write_channel_powertable() - Set power table for current channel on hw
  2987. * @ah: The &struct ath5k_hw
  2988. * @ee_mode: One of enum ath5k_driver_mode
  2989. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2990. */
  2991. static void
  2992. ath5k_write_channel_powertable(struct ath5k_hw *ah, u8 ee_mode, u8 type)
  2993. {
  2994. if (type == AR5K_PWRTABLE_PWR_TO_PDADC)
  2995. ath5k_write_pwr_to_pdadc_table(ah, ee_mode);
  2996. else
  2997. ath5k_write_pcdac_table(ah);
  2998. }
  2999. /**
  3000. * DOC: Per-rate tx power setting
  3001. *
  3002. * This is the code that sets the desired tx power limit (below
  3003. * maximum) on hw for each rate (we also have TPC that sets
  3004. * power per packet type). We do that by providing an index on the
  3005. * PCDAC/PDADC table we set up above, for each rate.
  3006. *
  3007. * For now we only limit txpower based on maximum tx power
  3008. * supported by hw (what's inside rate_info) + conformance test
  3009. * limits. We need to limit this even more, based on regulatory domain
  3010. * etc to be safe. Normally this is done from above so we don't care
  3011. * here, all we care is that the tx power we set will be O.K.
  3012. * for the hw (e.g. won't create noise on PA etc).
  3013. *
  3014. * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps -
  3015. * x values) and is indexed as follows:
  3016. * rates[0] - rates[7] -> OFDM rates
  3017. * rates[8] - rates[14] -> CCK rates
  3018. * rates[15] -> XR rates (they all have the same power)
  3019. */
  3020. /**
  3021. * ath5k_setup_rate_powertable() - Set up rate power table for a given tx power
  3022. * @ah: The &struct ath5k_hw
  3023. * @max_pwr: The maximum tx power requested in 0.5dB steps
  3024. * @rate_info: The &struct ath5k_rate_pcal_info to fill
  3025. * @ee_mode: One of enum ath5k_driver_mode
  3026. */
  3027. static void
  3028. ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
  3029. struct ath5k_rate_pcal_info *rate_info,
  3030. u8 ee_mode)
  3031. {
  3032. unsigned int i;
  3033. u16 *rates;
  3034. s16 rate_idx_scaled = 0;
  3035. /* max_pwr is power level we got from driver/user in 0.5dB
  3036. * units, switch to 0.25dB units so we can compare */
  3037. max_pwr *= 2;
  3038. max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
  3039. /* apply rate limits */
  3040. rates = ah->ah_txpower.txp_rates_power_table;
  3041. /* OFDM rates 6 to 24Mb/s */
  3042. for (i = 0; i < 5; i++)
  3043. rates[i] = min(max_pwr, rate_info->target_power_6to24);
  3044. /* Rest OFDM rates */
  3045. rates[5] = min(rates[0], rate_info->target_power_36);
  3046. rates[6] = min(rates[0], rate_info->target_power_48);
  3047. rates[7] = min(rates[0], rate_info->target_power_54);
  3048. /* CCK rates */
  3049. /* 1L */
  3050. rates[8] = min(rates[0], rate_info->target_power_6to24);
  3051. /* 2L */
  3052. rates[9] = min(rates[0], rate_info->target_power_36);
  3053. /* 2S */
  3054. rates[10] = min(rates[0], rate_info->target_power_36);
  3055. /* 5L */
  3056. rates[11] = min(rates[0], rate_info->target_power_48);
  3057. /* 5S */
  3058. rates[12] = min(rates[0], rate_info->target_power_48);
  3059. /* 11L */
  3060. rates[13] = min(rates[0], rate_info->target_power_54);
  3061. /* 11S */
  3062. rates[14] = min(rates[0], rate_info->target_power_54);
  3063. /* XR rates */
  3064. rates[15] = min(rates[0], rate_info->target_power_6to24);
  3065. /* CCK rates have different peak to average ratio
  3066. * so we have to tweak their power so that gainf
  3067. * correction works ok. For this we use OFDM to
  3068. * CCK delta from eeprom */
  3069. if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
  3070. (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
  3071. for (i = 8; i <= 15; i++)
  3072. rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
  3073. /* Save min/max and current tx power for this channel
  3074. * in 0.25dB units.
  3075. *
  3076. * Note: We use rates[0] for current tx power because
  3077. * it covers most of the rates, in most cases. It's our
  3078. * tx power limit and what the user expects to see. */
  3079. ah->ah_txpower.txp_min_pwr = 2 * rates[7];
  3080. ah->ah_txpower.txp_cur_pwr = 2 * rates[0];
  3081. /* Set max txpower for correct OFDM operation on all rates
  3082. * -that is the txpower for 54Mbit-, it's used for the PAPD
  3083. * gain probe and it's in 0.5dB units */
  3084. ah->ah_txpower.txp_ofdm = rates[7];
  3085. /* Now that we have all rates setup use table offset to
  3086. * match the power range set by user with the power indices
  3087. * on PCDAC/PDADC table */
  3088. for (i = 0; i < 16; i++) {
  3089. rate_idx_scaled = rates[i] + ah->ah_txpower.txp_offset;
  3090. /* Don't get out of bounds */
  3091. if (rate_idx_scaled > 63)
  3092. rate_idx_scaled = 63;
  3093. if (rate_idx_scaled < 0)
  3094. rate_idx_scaled = 0;
  3095. rates[i] = rate_idx_scaled;
  3096. }
  3097. }
  3098. /**
  3099. * ath5k_hw_txpower() - Set transmission power limit for a given channel
  3100. * @ah: The &struct ath5k_hw
  3101. * @channel: The &struct ieee80211_channel
  3102. * @txpower: Requested tx power in 0.5dB steps
  3103. *
  3104. * Combines all of the above to set the requested tx power limit
  3105. * on hw.
  3106. */
  3107. static int
  3108. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  3109. u8 txpower)
  3110. {
  3111. struct ath5k_rate_pcal_info rate_info;
  3112. struct ieee80211_channel *curr_channel = ah->ah_current_channel;
  3113. int ee_mode;
  3114. u8 type;
  3115. int ret;
  3116. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  3117. ATH5K_ERR(ah, "invalid tx power: %u\n", txpower);
  3118. return -EINVAL;
  3119. }
  3120. ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);
  3121. /* Initialize TX power table */
  3122. switch (ah->ah_radio) {
  3123. case AR5K_RF5110:
  3124. /* TODO */
  3125. return 0;
  3126. case AR5K_RF5111:
  3127. type = AR5K_PWRTABLE_PWR_TO_PCDAC;
  3128. break;
  3129. case AR5K_RF5112:
  3130. type = AR5K_PWRTABLE_LINEAR_PCDAC;
  3131. break;
  3132. case AR5K_RF2413:
  3133. case AR5K_RF5413:
  3134. case AR5K_RF2316:
  3135. case AR5K_RF2317:
  3136. case AR5K_RF2425:
  3137. type = AR5K_PWRTABLE_PWR_TO_PDADC;
  3138. break;
  3139. default:
  3140. return -EINVAL;
  3141. }
  3142. /*
  3143. * If we don't change channel/mode skip tx powertable calculation
  3144. * and use the cached one.
  3145. */
  3146. if (!ah->ah_txpower.txp_setup ||
  3147. (channel->hw_value != curr_channel->hw_value) ||
  3148. (channel->center_freq != curr_channel->center_freq)) {
  3149. /* Reset TX power values but preserve requested
  3150. * tx power from above */
  3151. int requested_txpower = ah->ah_txpower.txp_requested;
  3152. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  3153. /* Restore TPC setting and requested tx power */
  3154. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  3155. ah->ah_txpower.txp_requested = requested_txpower;
  3156. /* Calculate the powertable */
  3157. ret = ath5k_setup_channel_powertable(ah, channel,
  3158. ee_mode, type);
  3159. if (ret)
  3160. return ret;
  3161. }
  3162. /* Write table on hw */
  3163. ath5k_write_channel_powertable(ah, ee_mode, type);
  3164. /* Limit max power if we have a CTL available */
  3165. ath5k_get_max_ctl_power(ah, channel);
  3166. /* FIXME: Antenna reduction stuff */
  3167. /* FIXME: Limit power on turbo modes */
  3168. /* FIXME: TPC scale reduction */
  3169. /* Get surrounding channels for per-rate power table
  3170. * calibration */
  3171. ath5k_get_rate_pcal_data(ah, channel, &rate_info);
  3172. /* Setup rate power table */
  3173. ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
  3174. /* Write rate power table on hw */
  3175. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  3176. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  3177. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  3178. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  3179. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  3180. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  3181. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  3182. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  3183. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  3184. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  3185. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  3186. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  3187. /* FIXME: TPC support */
  3188. if (ah->ah_txpower.txp_tpc) {
  3189. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  3190. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  3191. ath5k_hw_reg_write(ah,
  3192. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
  3193. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
  3194. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
  3195. AR5K_TPC);
  3196. } else {
  3197. ath5k_hw_reg_write(ah, AR5K_TUNE_MAX_TXPOWER,
  3198. AR5K_PHY_TXPOWER_RATE_MAX);
  3199. }
  3200. return 0;
  3201. }
  3202. /**
  3203. * ath5k_hw_set_txpower_limit() - Set txpower limit for the current channel
  3204. * @ah: The &struct ath5k_hw
  3205. * @txpower: The requested tx power limit in 0.5dB steps
  3206. *
  3207. * This function provides access to ath5k_hw_txpower to the driver in
  3208. * case user or an application changes it while PHY is running.
  3209. */
  3210. int
  3211. ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)
  3212. {
  3213. ATH5K_DBG(ah, ATH5K_DEBUG_TXPOWER,
  3214. "changing txpower to %d\n", txpower);
  3215. return ath5k_hw_txpower(ah, ah->ah_current_channel, txpower);
  3216. }
  3217. /*************\
  3218. Init function
  3219. \*************/
  3220. /**
  3221. * ath5k_hw_phy_init() - Initialize PHY
  3222. * @ah: The &struct ath5k_hw
  3223. * @channel: The @struct ieee80211_channel
  3224. * @mode: One of enum ath5k_driver_mode
  3225. * @fast: Try a fast channel switch instead
  3226. *
  3227. * This is the main function used during reset to initialize PHY
  3228. * or do a fast channel change if possible.
  3229. *
  3230. * NOTE: Do not call this one from the driver, it assumes PHY is in a
  3231. * warm reset state !
  3232. */
  3233. int
  3234. ath5k_hw_phy_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  3235. u8 mode, bool fast)
  3236. {
  3237. struct ieee80211_channel *curr_channel;
  3238. int ret, i;
  3239. u32 phy_tst1;
  3240. ret = 0;
  3241. /*
  3242. * Sanity check for fast flag
  3243. * Don't try fast channel change when changing modulation
  3244. * mode/band. We check for chip compatibility on
  3245. * ath5k_hw_reset.
  3246. */
  3247. curr_channel = ah->ah_current_channel;
  3248. if (fast && (channel->hw_value != curr_channel->hw_value))
  3249. return -EINVAL;
  3250. /*
  3251. * On fast channel change we only set the synth parameters
  3252. * while PHY is running, enable calibration and skip the rest.
  3253. */
  3254. if (fast) {
  3255. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_RFBUS_REQ,
  3256. AR5K_PHY_RFBUS_REQ_REQUEST);
  3257. for (i = 0; i < 100; i++) {
  3258. if (ath5k_hw_reg_read(ah, AR5K_PHY_RFBUS_GRANT))
  3259. break;
  3260. udelay(5);
  3261. }
  3262. /* Failed */
  3263. if (i >= 100)
  3264. return -EIO;
  3265. /* Set channel and wait for synth */
  3266. ret = ath5k_hw_channel(ah, channel);
  3267. if (ret)
  3268. return ret;
  3269. ath5k_hw_wait_for_synth(ah, channel);
  3270. }
  3271. /*
  3272. * Set TX power
  3273. *
  3274. * Note: We need to do that before we set
  3275. * RF buffer settings on 5211/5212+ so that we
  3276. * properly set curve indices.
  3277. */
  3278. ret = ath5k_hw_txpower(ah, channel, ah->ah_txpower.txp_requested ?
  3279. ah->ah_txpower.txp_requested * 2 :
  3280. AR5K_TUNE_MAX_TXPOWER);
  3281. if (ret)
  3282. return ret;
  3283. /* Write OFDM timings on 5212*/
  3284. if (ah->ah_version == AR5K_AR5212 &&
  3285. channel->hw_value != AR5K_MODE_11B) {
  3286. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  3287. if (ret)
  3288. return ret;
  3289. /* Spur info is available only from EEPROM versions
  3290. * greater than 5.3, but the EEPROM routines will use
  3291. * static values for older versions */
  3292. if (ah->ah_mac_srev >= AR5K_SREV_AR5424)
  3293. ath5k_hw_set_spur_mitigation_filter(ah,
  3294. channel);
  3295. }
  3296. /* If we used fast channel switching
  3297. * we are done, release RF bus and
  3298. * fire up NF calibration.
  3299. *
  3300. * Note: Only NF calibration due to
  3301. * channel change, not AGC calibration
  3302. * since AGC is still running !
  3303. */
  3304. if (fast) {
  3305. /*
  3306. * Release RF Bus grant
  3307. */
  3308. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_RFBUS_REQ,
  3309. AR5K_PHY_RFBUS_REQ_REQUEST);
  3310. /*
  3311. * Start NF calibration
  3312. */
  3313. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  3314. AR5K_PHY_AGCCTL_NF);
  3315. return ret;
  3316. }
  3317. /*
  3318. * For 5210 we do all initialization using
  3319. * initvals, so we don't have to modify
  3320. * any settings (5210 also only supports
  3321. * a/aturbo modes)
  3322. */
  3323. if (ah->ah_version != AR5K_AR5210) {
  3324. /*
  3325. * Write initial RF gain settings
  3326. * This should work for both 5111/5112
  3327. */
  3328. ret = ath5k_hw_rfgain_init(ah, channel->band);
  3329. if (ret)
  3330. return ret;
  3331. usleep_range(1000, 1500);
  3332. /*
  3333. * Write RF buffer
  3334. */
  3335. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  3336. if (ret)
  3337. return ret;
  3338. /*Enable/disable 802.11b mode on 5111
  3339. (enable 2111 frequency converter + CCK)*/
  3340. if (ah->ah_radio == AR5K_RF5111) {
  3341. if (mode == AR5K_MODE_11B)
  3342. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  3343. AR5K_TXCFG_B_MODE);
  3344. else
  3345. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  3346. AR5K_TXCFG_B_MODE);
  3347. }
  3348. } else if (ah->ah_version == AR5K_AR5210) {
  3349. usleep_range(1000, 1500);
  3350. /* Disable phy and wait */
  3351. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  3352. usleep_range(1000, 1500);
  3353. }
  3354. /* Set channel on PHY */
  3355. ret = ath5k_hw_channel(ah, channel);
  3356. if (ret)
  3357. return ret;
  3358. /*
  3359. * Enable the PHY and wait until completion
  3360. * This includes BaseBand and Synthesizer
  3361. * activation.
  3362. */
  3363. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  3364. ath5k_hw_wait_for_synth(ah, channel);
  3365. /*
  3366. * Perform ADC test to see if baseband is ready
  3367. * Set tx hold and check adc test register
  3368. */
  3369. phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  3370. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  3371. for (i = 0; i <= 20; i++) {
  3372. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  3373. break;
  3374. usleep_range(200, 250);
  3375. }
  3376. ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
  3377. /*
  3378. * Start automatic gain control calibration
  3379. *
  3380. * During AGC calibration RX path is re-routed to
  3381. * a power detector so we don't receive anything.
  3382. *
  3383. * This method is used to calibrate some static offsets
  3384. * used together with on-the fly I/Q calibration (the
  3385. * one performed via ath5k_hw_phy_calibrate), which doesn't
  3386. * interrupt rx path.
  3387. *
  3388. * While rx path is re-routed to the power detector we also
  3389. * start a noise floor calibration to measure the
  3390. * card's noise floor (the noise we measure when we are not
  3391. * transmitting or receiving anything).
  3392. *
  3393. * If we are in a noisy environment, AGC calibration may time
  3394. * out and/or noise floor calibration might timeout.
  3395. */
  3396. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  3397. AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF);
  3398. /* At the same time start I/Q calibration for QAM constellation
  3399. * -no need for CCK- */
  3400. ah->ah_iq_cal_needed = false;
  3401. if (!(mode == AR5K_MODE_11B)) {
  3402. ah->ah_iq_cal_needed = true;
  3403. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  3404. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  3405. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  3406. AR5K_PHY_IQ_RUN);
  3407. }
  3408. /* Wait for gain calibration to finish (we check for I/Q calibration
  3409. * during ath5k_phy_calibrate) */
  3410. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  3411. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  3412. ATH5K_ERR(ah, "gain calibration timeout (%uMHz)\n",
  3413. channel->center_freq);
  3414. }
  3415. /* Restore antenna mode */
  3416. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  3417. return ret;
  3418. }