phy.c 107 KB

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