phy.c 82 KB

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