phy.c 90 KB

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