phy.c 108 KB

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