phy.c 90 KB

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