phy.c 81 KB

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