phy.c 87 KB

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