phy.c 90 KB

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