You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

ath5k_phy.c 67KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586
  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. * Lightly modified for gPXE, July 2009, by Joshua Oreman <oremanj@rwcr.net>.
  10. *
  11. * Permission to use, copy, modify, and distribute this software for any
  12. * purpose with or without fee is hereby granted, provided that the above
  13. * copyright notice and this permission notice appear in all copies.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  16. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  17. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  18. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  21. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22. *
  23. */
  24. FILE_LICENCE ( MIT );
  25. #define _ATH5K_PHY
  26. #include <unistd.h>
  27. #include <stdlib.h>
  28. #include "ath5k.h"
  29. #include "reg.h"
  30. #include "base.h"
  31. #include "rfbuffer.h"
  32. #include "rfgain.h"
  33. static inline int min(int x, int y)
  34. {
  35. return (x < y) ? x : y;
  36. }
  37. static inline int max(int x, int y)
  38. {
  39. return (x > y) ? x : y;
  40. }
  41. /*
  42. * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
  43. */
  44. static unsigned int ath5k_hw_rfb_op(struct ath5k_hw *ah,
  45. const struct ath5k_rf_reg *rf_regs,
  46. u32 val, u8 reg_id, int set)
  47. {
  48. const struct ath5k_rf_reg *rfreg = NULL;
  49. u8 offset, bank, num_bits, col, position;
  50. u16 entry;
  51. u32 mask, data, last_bit, bits_shifted, first_bit;
  52. u32 *rfb;
  53. s32 bits_left;
  54. unsigned i;
  55. data = 0;
  56. rfb = ah->ah_rf_banks;
  57. for (i = 0; i < ah->ah_rf_regs_count; i++) {
  58. if (rf_regs[i].index == reg_id) {
  59. rfreg = &rf_regs[i];
  60. break;
  61. }
  62. }
  63. if (rfb == NULL || rfreg == NULL) {
  64. DBG("ath5k: RF register not found!\n");
  65. /* should not happen */
  66. return 0;
  67. }
  68. bank = rfreg->bank;
  69. num_bits = rfreg->field.len;
  70. first_bit = rfreg->field.pos;
  71. col = rfreg->field.col;
  72. /* first_bit is an offset from bank's
  73. * start. Since we have all banks on
  74. * the same array, we use this offset
  75. * to mark each bank's start */
  76. offset = ah->ah_offset[bank];
  77. /* Boundary check */
  78. if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
  79. DBG("ath5k: RF invalid values at offset %d\n", offset);
  80. return 0;
  81. }
  82. entry = ((first_bit - 1) / 8) + offset;
  83. position = (first_bit - 1) % 8;
  84. if (set)
  85. data = ath5k_hw_bitswap(val, num_bits);
  86. for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
  87. position = 0, entry++) {
  88. last_bit = (position + bits_left > 8) ? 8 :
  89. position + bits_left;
  90. mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
  91. (col * 8);
  92. if (set) {
  93. rfb[entry] &= ~mask;
  94. rfb[entry] |= ((data << position) << (col * 8)) & mask;
  95. data >>= (8 - position);
  96. } else {
  97. data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
  98. << bits_shifted;
  99. bits_shifted += last_bit - position;
  100. }
  101. bits_left -= 8 - position;
  102. }
  103. data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
  104. return data;
  105. }
  106. /**********************\
  107. * RF Gain optimization *
  108. \**********************/
  109. /*
  110. * This code is used to optimize rf gain on different environments
  111. * (temprature mostly) based on feedback from a power detector.
  112. *
  113. * It's only used on RF5111 and RF5112, later RF chips seem to have
  114. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  115. * no gain optimization ladder-.
  116. *
  117. * For more infos check out this patent doc
  118. * http://www.freepatentsonline.com/7400691.html
  119. *
  120. * This paper describes power drops as seen on the receiver due to
  121. * probe packets
  122. * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  123. * %20of%20Power%20Control.pdf
  124. *
  125. * And this is the MadWiFi bug entry related to the above
  126. * http://madwifi-project.org/ticket/1659
  127. * with various measurements and diagrams
  128. *
  129. * TODO: Deal with power drops due to probes by setting an apropriate
  130. * tx power on the probe packets ! Make this part of the calibration process.
  131. */
  132. /* Initialize ah_gain durring attach */
  133. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  134. {
  135. /* Initialize the gain optimization values */
  136. switch (ah->ah_radio) {
  137. case AR5K_RF5111:
  138. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  139. ah->ah_gain.g_low = 20;
  140. ah->ah_gain.g_high = 35;
  141. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  142. break;
  143. case AR5K_RF5112:
  144. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  145. ah->ah_gain.g_low = 20;
  146. ah->ah_gain.g_high = 85;
  147. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  148. break;
  149. default:
  150. return -EINVAL;
  151. }
  152. return 0;
  153. }
  154. /* Schedule a gain probe check on the next transmited packet.
  155. * That means our next packet is going to be sent with lower
  156. * tx power and a Peak to Average Power Detector (PAPD) will try
  157. * to measure the gain.
  158. *
  159. * TODO: Use propper tx power setting for the probe packet so
  160. * that we don't observe a serious power drop on the receiver
  161. *
  162. * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
  163. * just after we enable the probe so that we don't mess with
  164. * standard traffic ? Maybe it's time to use sw interrupts and
  165. * a probe tasklet !!!
  166. */
  167. static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  168. {
  169. /* Skip if gain calibration is inactive or
  170. * we already handle a probe request */
  171. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  172. return;
  173. /* Send the packet with 2dB below max power as
  174. * patent doc suggest */
  175. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_max_pwr - 4,
  176. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  177. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  178. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  179. }
  180. /* Calculate gain_F measurement correction
  181. * based on the current step for RF5112 rev. 2 */
  182. static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  183. {
  184. u32 mix, step;
  185. u32 *rf;
  186. const struct ath5k_gain_opt *go;
  187. const struct ath5k_gain_opt_step *g_step;
  188. const struct ath5k_rf_reg *rf_regs;
  189. /* Only RF5112 Rev. 2 supports it */
  190. if ((ah->ah_radio != AR5K_RF5112) ||
  191. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  192. return 0;
  193. go = &rfgain_opt_5112;
  194. rf_regs = rf_regs_5112a;
  195. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  196. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  197. if (ah->ah_rf_banks == NULL)
  198. return 0;
  199. rf = ah->ah_rf_banks;
  200. ah->ah_gain.g_f_corr = 0;
  201. /* No VGA (Variable Gain Amplifier) override, skip */
  202. if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, 0) != 1)
  203. return 0;
  204. /* Mix gain stepping */
  205. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, 0);
  206. /* Mix gain override */
  207. mix = g_step->gos_param[0];
  208. switch (mix) {
  209. case 3:
  210. ah->ah_gain.g_f_corr = step * 2;
  211. break;
  212. case 2:
  213. ah->ah_gain.g_f_corr = (step - 5) * 2;
  214. break;
  215. case 1:
  216. ah->ah_gain.g_f_corr = step;
  217. break;
  218. default:
  219. ah->ah_gain.g_f_corr = 0;
  220. break;
  221. }
  222. return ah->ah_gain.g_f_corr;
  223. }
  224. /* Check if current gain_F measurement is in the range of our
  225. * power detector windows. If we get a measurement outside range
  226. * we know it's not accurate (detectors can't measure anything outside
  227. * their detection window) so we must ignore it */
  228. static int ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  229. {
  230. const struct ath5k_rf_reg *rf_regs;
  231. u32 step, mix_ovr, level[4];
  232. u32 *rf;
  233. if (ah->ah_rf_banks == NULL)
  234. return 0;
  235. rf = ah->ah_rf_banks;
  236. if (ah->ah_radio == AR5K_RF5111) {
  237. rf_regs = rf_regs_5111;
  238. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  239. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
  240. 0);
  241. level[0] = 0;
  242. level[1] = (step == 63) ? 50 : step + 4;
  243. level[2] = (step != 63) ? 64 : level[0];
  244. level[3] = level[2] + 50 ;
  245. ah->ah_gain.g_high = level[3] -
  246. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  247. ah->ah_gain.g_low = level[0] +
  248. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  249. } else {
  250. rf_regs = rf_regs_5112;
  251. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  252. mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
  253. 0);
  254. level[0] = level[2] = 0;
  255. if (mix_ovr == 1) {
  256. level[1] = level[3] = 83;
  257. } else {
  258. level[1] = level[3] = 107;
  259. ah->ah_gain.g_high = 55;
  260. }
  261. }
  262. return (ah->ah_gain.g_current >= level[0] &&
  263. ah->ah_gain.g_current <= level[1]) ||
  264. (ah->ah_gain.g_current >= level[2] &&
  265. ah->ah_gain.g_current <= level[3]);
  266. }
  267. /* Perform gain_F adjustment by choosing the right set
  268. * of parameters from rf gain optimization ladder */
  269. static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  270. {
  271. const struct ath5k_gain_opt *go;
  272. const struct ath5k_gain_opt_step *g_step;
  273. int ret = 0;
  274. switch (ah->ah_radio) {
  275. case AR5K_RF5111:
  276. go = &rfgain_opt_5111;
  277. break;
  278. case AR5K_RF5112:
  279. go = &rfgain_opt_5112;
  280. break;
  281. default:
  282. return 0;
  283. }
  284. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  285. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  286. /* Reached maximum */
  287. if (ah->ah_gain.g_step_idx == 0)
  288. return -1;
  289. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  290. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  291. ah->ah_gain.g_step_idx > 0;
  292. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  293. ah->ah_gain.g_target -= 2 *
  294. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  295. g_step->gos_gain);
  296. ret = 1;
  297. goto done;
  298. }
  299. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  300. /* Reached minimum */
  301. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  302. return -2;
  303. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  304. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  305. ah->ah_gain.g_step_idx < go->go_steps_count-1;
  306. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  307. ah->ah_gain.g_target -= 2 *
  308. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  309. g_step->gos_gain);
  310. ret = 2;
  311. goto done;
  312. }
  313. done:
  314. DBG2("ath5k RF adjust: ret %d, gain step %d, current gain %d, "
  315. "target gain %d\n", ret, ah->ah_gain.g_step_idx,
  316. ah->ah_gain.g_current, ah->ah_gain.g_target);
  317. return ret;
  318. }
  319. /* Main callback for thermal rf gain calibration engine
  320. * Check for a new gain reading and schedule an adjustment
  321. * if needed.
  322. *
  323. * TODO: Use sw interrupt to schedule reset if gain_F needs
  324. * adjustment */
  325. enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  326. {
  327. u32 data, type;
  328. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  329. if (ah->ah_rf_banks == NULL ||
  330. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  331. return AR5K_RFGAIN_INACTIVE;
  332. /* No check requested, either engine is inactive
  333. * or an adjustment is already requested */
  334. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  335. goto done;
  336. /* Read the PAPD (Peak to Average Power Detector)
  337. * register */
  338. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  339. /* No probe is scheduled, read gain_F measurement */
  340. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  341. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  342. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  343. /* If tx packet is CCK correct the gain_F measurement
  344. * by cck ofdm gain delta */
  345. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  346. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  347. ah->ah_gain.g_current +=
  348. ee->ee_cck_ofdm_gain_delta;
  349. else
  350. ah->ah_gain.g_current +=
  351. AR5K_GAIN_CCK_PROBE_CORR;
  352. }
  353. /* Further correct gain_F measurement for
  354. * RF5112A radios */
  355. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  356. ath5k_hw_rf_gainf_corr(ah);
  357. ah->ah_gain.g_current =
  358. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  359. (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
  360. 0;
  361. }
  362. /* Check if measurement is ok and if we need
  363. * to adjust gain, schedule a gain adjustment,
  364. * else switch back to the acive state */
  365. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  366. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  367. ath5k_hw_rf_gainf_adjust(ah)) {
  368. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  369. } else {
  370. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  371. }
  372. }
  373. done:
  374. return ah->ah_gain.g_state;
  375. }
  376. /* Write initial rf gain table to set the RF sensitivity
  377. * this one works on all RF chips and has nothing to do
  378. * with gain_F calibration */
  379. int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
  380. {
  381. const struct ath5k_ini_rfgain *ath5k_rfg;
  382. unsigned int i, size;
  383. switch (ah->ah_radio) {
  384. case AR5K_RF5111:
  385. ath5k_rfg = rfgain_5111;
  386. size = ARRAY_SIZE(rfgain_5111);
  387. break;
  388. case AR5K_RF5112:
  389. ath5k_rfg = rfgain_5112;
  390. size = ARRAY_SIZE(rfgain_5112);
  391. break;
  392. case AR5K_RF2413:
  393. ath5k_rfg = rfgain_2413;
  394. size = ARRAY_SIZE(rfgain_2413);
  395. break;
  396. case AR5K_RF2316:
  397. ath5k_rfg = rfgain_2316;
  398. size = ARRAY_SIZE(rfgain_2316);
  399. break;
  400. case AR5K_RF5413:
  401. ath5k_rfg = rfgain_5413;
  402. size = ARRAY_SIZE(rfgain_5413);
  403. break;
  404. case AR5K_RF2317:
  405. case AR5K_RF2425:
  406. ath5k_rfg = rfgain_2425;
  407. size = ARRAY_SIZE(rfgain_2425);
  408. break;
  409. default:
  410. return -EINVAL;
  411. }
  412. switch (freq) {
  413. case AR5K_INI_RFGAIN_2GHZ:
  414. case AR5K_INI_RFGAIN_5GHZ:
  415. break;
  416. default:
  417. return -EINVAL;
  418. }
  419. for (i = 0; i < size; i++) {
  420. AR5K_REG_WAIT(i);
  421. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
  422. (u32)ath5k_rfg[i].rfg_register);
  423. }
  424. return 0;
  425. }
  426. /********************\
  427. * RF Registers setup *
  428. \********************/
  429. /*
  430. * Setup RF registers by writing rf buffer on hw
  431. */
  432. int ath5k_hw_rfregs_init(struct ath5k_hw *ah, struct net80211_channel *channel,
  433. unsigned int mode)
  434. {
  435. const struct ath5k_rf_reg *rf_regs;
  436. const struct ath5k_ini_rfbuffer *ini_rfb;
  437. const struct ath5k_gain_opt *go = NULL;
  438. const struct ath5k_gain_opt_step *g_step;
  439. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  440. u8 ee_mode = 0;
  441. u32 *rfb;
  442. int obdb = -1, bank = -1;
  443. unsigned i;
  444. switch (ah->ah_radio) {
  445. case AR5K_RF5111:
  446. rf_regs = rf_regs_5111;
  447. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  448. ini_rfb = rfb_5111;
  449. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
  450. go = &rfgain_opt_5111;
  451. break;
  452. case AR5K_RF5112:
  453. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  454. rf_regs = rf_regs_5112a;
  455. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  456. ini_rfb = rfb_5112a;
  457. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
  458. } else {
  459. rf_regs = rf_regs_5112;
  460. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  461. ini_rfb = rfb_5112;
  462. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
  463. }
  464. go = &rfgain_opt_5112;
  465. break;
  466. case AR5K_RF2413:
  467. rf_regs = rf_regs_2413;
  468. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
  469. ini_rfb = rfb_2413;
  470. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
  471. break;
  472. case AR5K_RF2316:
  473. rf_regs = rf_regs_2316;
  474. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
  475. ini_rfb = rfb_2316;
  476. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
  477. break;
  478. case AR5K_RF5413:
  479. rf_regs = rf_regs_5413;
  480. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
  481. ini_rfb = rfb_5413;
  482. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
  483. break;
  484. case AR5K_RF2317:
  485. rf_regs = rf_regs_2425;
  486. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  487. ini_rfb = rfb_2317;
  488. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
  489. break;
  490. case AR5K_RF2425:
  491. rf_regs = rf_regs_2425;
  492. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  493. if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
  494. ini_rfb = rfb_2425;
  495. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
  496. } else {
  497. ini_rfb = rfb_2417;
  498. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
  499. }
  500. break;
  501. default:
  502. return -EINVAL;
  503. }
  504. /* If it's the first time we set rf buffer, allocate
  505. * ah->ah_rf_banks based on ah->ah_rf_banks_size
  506. * we set above */
  507. if (ah->ah_rf_banks == NULL) {
  508. ah->ah_rf_banks = malloc(sizeof(u32) * ah->ah_rf_banks_size);
  509. if (ah->ah_rf_banks == NULL) {
  510. return -ENOMEM;
  511. }
  512. }
  513. /* Copy values to modify them */
  514. rfb = ah->ah_rf_banks;
  515. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  516. if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
  517. DBG("ath5k: invalid RF register bank\n");
  518. return -EINVAL;
  519. }
  520. /* Bank changed, write down the offset */
  521. if (bank != ini_rfb[i].rfb_bank) {
  522. bank = ini_rfb[i].rfb_bank;
  523. ah->ah_offset[bank] = i;
  524. }
  525. rfb[i] = ini_rfb[i].rfb_mode_data[mode];
  526. }
  527. /* Set Output and Driver bias current (OB/DB) */
  528. if (channel->hw_value & CHANNEL_2GHZ) {
  529. if (channel->hw_value & CHANNEL_CCK)
  530. ee_mode = AR5K_EEPROM_MODE_11B;
  531. else
  532. ee_mode = AR5K_EEPROM_MODE_11G;
  533. /* For RF511X/RF211X combination we
  534. * use b_OB and b_DB parameters stored
  535. * in eeprom on ee->ee_ob[ee_mode][0]
  536. *
  537. * For all other chips we use OB/DB for 2Ghz
  538. * stored in the b/g modal section just like
  539. * 802.11a on ee->ee_ob[ee_mode][1] */
  540. if ((ah->ah_radio == AR5K_RF5111) ||
  541. (ah->ah_radio == AR5K_RF5112))
  542. obdb = 0;
  543. else
  544. obdb = 1;
  545. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  546. AR5K_RF_OB_2GHZ, 1);
  547. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  548. AR5K_RF_DB_2GHZ, 1);
  549. /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
  550. } else if ((channel->hw_value & CHANNEL_5GHZ) ||
  551. (ah->ah_radio == AR5K_RF5111)) {
  552. /* For 11a, Turbo and XR we need to choose
  553. * OB/DB based on frequency range */
  554. ee_mode = AR5K_EEPROM_MODE_11A;
  555. obdb = channel->center_freq >= 5725 ? 3 :
  556. (channel->center_freq >= 5500 ? 2 :
  557. (channel->center_freq >= 5260 ? 1 :
  558. (channel->center_freq > 4000 ? 0 : -1)));
  559. if (obdb < 0)
  560. return -EINVAL;
  561. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  562. AR5K_RF_OB_5GHZ, 1);
  563. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  564. AR5K_RF_DB_5GHZ, 1);
  565. }
  566. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  567. /* Bank Modifications (chip-specific) */
  568. if (ah->ah_radio == AR5K_RF5111) {
  569. /* Set gain_F settings according to current step */
  570. if (channel->hw_value & CHANNEL_OFDM) {
  571. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  572. AR5K_PHY_FRAME_CTL_TX_CLIP,
  573. g_step->gos_param[0]);
  574. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  575. AR5K_RF_PWD_90, 1);
  576. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  577. AR5K_RF_PWD_84, 1);
  578. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  579. AR5K_RF_RFGAIN_SEL, 1);
  580. /* We programmed gain_F parameters, switch back
  581. * to active state */
  582. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  583. }
  584. /* Bank 6/7 setup */
  585. ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
  586. AR5K_RF_PWD_XPD, 1);
  587. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
  588. AR5K_RF_XPD_GAIN, 1);
  589. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  590. AR5K_RF_GAIN_I, 1);
  591. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  592. AR5K_RF_PLO_SEL, 1);
  593. /* TODO: Half/quarter channel support */
  594. }
  595. if (ah->ah_radio == AR5K_RF5112) {
  596. /* Set gain_F settings according to current step */
  597. if (channel->hw_value & CHANNEL_OFDM) {
  598. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
  599. AR5K_RF_MIXGAIN_OVR, 1);
  600. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  601. AR5K_RF_PWD_138, 1);
  602. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  603. AR5K_RF_PWD_137, 1);
  604. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  605. AR5K_RF_PWD_136, 1);
  606. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
  607. AR5K_RF_PWD_132, 1);
  608. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
  609. AR5K_RF_PWD_131, 1);
  610. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
  611. AR5K_RF_PWD_130, 1);
  612. /* We programmed gain_F parameters, switch back
  613. * to active state */
  614. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  615. }
  616. /* Bank 6/7 setup */
  617. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  618. AR5K_RF_XPD_SEL, 1);
  619. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  620. /* Rev. 1 supports only one xpd */
  621. ath5k_hw_rfb_op(ah, rf_regs,
  622. ee->ee_x_gain[ee_mode],
  623. AR5K_RF_XPD_GAIN, 1);
  624. } else {
  625. /* TODO: Set high and low gain bits */
  626. ath5k_hw_rfb_op(ah, rf_regs,
  627. ee->ee_x_gain[ee_mode],
  628. AR5K_RF_PD_GAIN_LO, 1);
  629. ath5k_hw_rfb_op(ah, rf_regs,
  630. ee->ee_x_gain[ee_mode],
  631. AR5K_RF_PD_GAIN_HI, 1);
  632. /* Lower synth voltage on Rev 2 */
  633. ath5k_hw_rfb_op(ah, rf_regs, 2,
  634. AR5K_RF_HIGH_VC_CP, 1);
  635. ath5k_hw_rfb_op(ah, rf_regs, 2,
  636. AR5K_RF_MID_VC_CP, 1);
  637. ath5k_hw_rfb_op(ah, rf_regs, 2,
  638. AR5K_RF_LOW_VC_CP, 1);
  639. ath5k_hw_rfb_op(ah, rf_regs, 2,
  640. AR5K_RF_PUSH_UP, 1);
  641. /* Decrease power consumption on 5213+ BaseBand */
  642. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  643. ath5k_hw_rfb_op(ah, rf_regs, 1,
  644. AR5K_RF_PAD2GND, 1);
  645. ath5k_hw_rfb_op(ah, rf_regs, 1,
  646. AR5K_RF_XB2_LVL, 1);
  647. ath5k_hw_rfb_op(ah, rf_regs, 1,
  648. AR5K_RF_XB5_LVL, 1);
  649. ath5k_hw_rfb_op(ah, rf_regs, 1,
  650. AR5K_RF_PWD_167, 1);
  651. ath5k_hw_rfb_op(ah, rf_regs, 1,
  652. AR5K_RF_PWD_166, 1);
  653. }
  654. }
  655. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  656. AR5K_RF_GAIN_I, 1);
  657. /* TODO: Half/quarter channel support */
  658. }
  659. if (ah->ah_radio == AR5K_RF5413 &&
  660. channel->hw_value & CHANNEL_2GHZ) {
  661. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
  662. 1);
  663. /* Set optimum value for early revisions (on pci-e chips) */
  664. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  665. ah->ah_mac_srev < AR5K_SREV_AR5413)
  666. ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
  667. AR5K_RF_PWD_ICLOBUF_2G, 1);
  668. }
  669. /* Write RF banks on hw */
  670. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  671. AR5K_REG_WAIT(i);
  672. ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
  673. }
  674. return 0;
  675. }
  676. /**************************\
  677. PHY/RF channel functions
  678. \**************************/
  679. /*
  680. * Check if a channel is supported
  681. */
  682. int ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
  683. {
  684. /* Check if the channel is in our supported range */
  685. if (flags & CHANNEL_2GHZ) {
  686. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  687. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  688. return 1;
  689. } else if (flags & CHANNEL_5GHZ)
  690. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  691. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  692. return 1;
  693. return 0;
  694. }
  695. /*
  696. * Convertion needed for RF5110
  697. */
  698. static u32 ath5k_hw_rf5110_chan2athchan(struct net80211_channel *channel)
  699. {
  700. u32 athchan;
  701. /*
  702. * Convert IEEE channel/MHz to an internal channel value used
  703. * by the AR5210 chipset. This has not been verified with
  704. * newer chipsets like the AR5212A who have a completely
  705. * different RF/PHY part.
  706. */
  707. athchan = (ath5k_hw_bitswap((ath5k_freq_to_channel(channel->center_freq)
  708. - 24) / 2, 5) << 1)
  709. | (1 << 6) | 0x1;
  710. return athchan;
  711. }
  712. /*
  713. * Set channel on RF5110
  714. */
  715. static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  716. struct net80211_channel *channel)
  717. {
  718. u32 data;
  719. /*
  720. * Set the channel and wait
  721. */
  722. data = ath5k_hw_rf5110_chan2athchan(channel);
  723. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  724. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  725. mdelay(1);
  726. return 0;
  727. }
  728. /*
  729. * Convertion needed for 5111
  730. */
  731. static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  732. struct ath5k_athchan_2ghz *athchan)
  733. {
  734. int channel;
  735. /* Cast this value to catch negative channel numbers (>= -19) */
  736. channel = (int)ieee;
  737. /*
  738. * Map 2GHz IEEE channel to 5GHz Atheros channel
  739. */
  740. if (channel <= 13) {
  741. athchan->a2_athchan = 115 + channel;
  742. athchan->a2_flags = 0x46;
  743. } else if (channel == 14) {
  744. athchan->a2_athchan = 124;
  745. athchan->a2_flags = 0x44;
  746. } else if (channel >= 15 && channel <= 26) {
  747. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  748. athchan->a2_flags = 0x46;
  749. } else
  750. return -EINVAL;
  751. return 0;
  752. }
  753. /*
  754. * Set channel on 5111
  755. */
  756. static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  757. struct net80211_channel *channel)
  758. {
  759. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  760. unsigned int ath5k_channel = ath5k_freq_to_channel(channel->center_freq);
  761. u32 data0, data1, clock;
  762. int ret;
  763. /*
  764. * Set the channel on the RF5111 radio
  765. */
  766. data0 = data1 = 0;
  767. if (channel->hw_value & CHANNEL_2GHZ) {
  768. /* Map 2GHz channel to 5GHz Atheros channel ID */
  769. ret = ath5k_hw_rf5111_chan2athchan(ath5k_channel,
  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 net80211_channel *channel)
  797. {
  798. u32 data, data0, data1, data2;
  799. u16 c;
  800. data = data0 = data1 = data2 = 0;
  801. c = channel->center_freq;
  802. if (c < 4800) {
  803. if (!((c - 2224) % 5)) {
  804. data0 = ((2 * (c - 704)) - 3040) / 10;
  805. data1 = 1;
  806. } else if (!((c - 2192) % 5)) {
  807. data0 = ((2 * (c - 672)) - 3040) / 10;
  808. data1 = 0;
  809. } else
  810. return -EINVAL;
  811. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  812. } else if ((c - (c % 5)) != 2 || c > 5435) {
  813. if (!(c % 20) && c >= 5120) {
  814. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  815. data2 = ath5k_hw_bitswap(3, 2);
  816. } else if (!(c % 10)) {
  817. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  818. data2 = ath5k_hw_bitswap(2, 2);
  819. } else if (!(c % 5)) {
  820. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  821. data2 = ath5k_hw_bitswap(1, 2);
  822. } else
  823. return -EINVAL;
  824. } else {
  825. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  826. data2 = ath5k_hw_bitswap(0, 2);
  827. }
  828. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  829. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  830. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  831. return 0;
  832. }
  833. /*
  834. * Set the channel on the RF2425
  835. */
  836. static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  837. struct net80211_channel *channel)
  838. {
  839. u32 data, data0, data2;
  840. u16 c;
  841. data = data0 = data2 = 0;
  842. c = channel->center_freq;
  843. if (c < 4800) {
  844. data0 = ath5k_hw_bitswap((c - 2272), 8);
  845. data2 = 0;
  846. /* ? 5GHz ? */
  847. } else if ((c - (c % 5)) != 2 || c > 5435) {
  848. if (!(c % 20) && c < 5120)
  849. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  850. else if (!(c % 10))
  851. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  852. else if (!(c % 5))
  853. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  854. else
  855. return -EINVAL;
  856. data2 = ath5k_hw_bitswap(1, 2);
  857. } else {
  858. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  859. data2 = ath5k_hw_bitswap(0, 2);
  860. }
  861. data = (data0 << 4) | data2 << 2 | 0x1001;
  862. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  863. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  864. return 0;
  865. }
  866. /*
  867. * Set a channel on the radio chip
  868. */
  869. int ath5k_hw_channel(struct ath5k_hw *ah, struct net80211_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. DBG("ath5k: channel frequency (%d MHz) out of supported "
  879. "range\n", channel->center_freq);
  880. return -EINVAL;
  881. }
  882. /*
  883. * Set the channel and wait
  884. */
  885. switch (ah->ah_radio) {
  886. case AR5K_RF5110:
  887. ret = ath5k_hw_rf5110_channel(ah, channel);
  888. break;
  889. case AR5K_RF5111:
  890. ret = ath5k_hw_rf5111_channel(ah, channel);
  891. break;
  892. case AR5K_RF2425:
  893. ret = ath5k_hw_rf2425_channel(ah, channel);
  894. break;
  895. default:
  896. ret = ath5k_hw_rf5112_channel(ah, channel);
  897. break;
  898. }
  899. if (ret) {
  900. DBG("ath5k: setting channel failed: %s\n", strerror(ret));
  901. return ret;
  902. }
  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 ? 1 : 0);
  913. return 0;
  914. }
  915. /*****************\
  916. PHY calibration
  917. \*****************/
  918. /**
  919. * ath5k_hw_noise_floor_calibration - perform PHY noise floor calibration
  920. *
  921. * @ah: struct ath5k_hw pointer we are operating on
  922. * @freq: the channel frequency, just used for error logging
  923. *
  924. * This function performs a noise floor calibration of the PHY and waits for
  925. * it to complete. Then the noise floor value is compared to some maximum
  926. * noise floor we consider valid.
  927. *
  928. * Note that this is different from what the madwifi HAL does: it reads the
  929. * noise floor and afterwards initiates the calibration. Since the noise floor
  930. * calibration can take some time to finish, depending on the current channel
  931. * use, that avoids the occasional timeout warnings we are seeing now.
  932. *
  933. * See the following link for an Atheros patent on noise floor calibration:
  934. * http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL \
  935. * &p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=7245893.PN.&OS=PN/7
  936. *
  937. * XXX: Since during noise floor calibration antennas are detached according to
  938. * the patent, we should stop tx queues here.
  939. */
  940. int
  941. ath5k_hw_noise_floor_calibration(struct ath5k_hw *ah, short freq)
  942. {
  943. int ret;
  944. unsigned int i;
  945. s32 noise_floor;
  946. /*
  947. * Enable noise floor calibration
  948. */
  949. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  950. AR5K_PHY_AGCCTL_NF);
  951. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  952. AR5K_PHY_AGCCTL_NF, 0, 0);
  953. if (ret) {
  954. DBG("ath5k: noise floor calibration timeout (%d MHz)\n", freq);
  955. return -EAGAIN;
  956. }
  957. /* Wait until the noise floor is calibrated and read the value */
  958. for (i = 20; i > 0; i--) {
  959. mdelay(1);
  960. noise_floor = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  961. noise_floor = AR5K_PHY_NF_RVAL(noise_floor);
  962. if (noise_floor & AR5K_PHY_NF_ACTIVE) {
  963. noise_floor = AR5K_PHY_NF_AVAL(noise_floor);
  964. if (noise_floor <= AR5K_TUNE_NOISE_FLOOR)
  965. break;
  966. }
  967. }
  968. DBG2("ath5k: noise floor %d\n", noise_floor);
  969. if (noise_floor > AR5K_TUNE_NOISE_FLOOR) {
  970. DBG("ath5k: noise floor calibration failed (%d MHz)\n", freq);
  971. return -EAGAIN;
  972. }
  973. ah->ah_noise_floor = noise_floor;
  974. return 0;
  975. }
  976. /*
  977. * Perform a PHY calibration on RF5110
  978. * -Fix BPSK/QAM Constellation (I/Q correction)
  979. * -Calculate Noise Floor
  980. */
  981. static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  982. struct net80211_channel *channel)
  983. {
  984. u32 phy_sig, phy_agc, phy_sat, beacon;
  985. int ret;
  986. /*
  987. * Disable beacons and RX/TX queues, wait
  988. */
  989. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  990. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  991. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  992. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  993. mdelay(2);
  994. /*
  995. * Set the channel (with AGC turned off)
  996. */
  997. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  998. udelay(10);
  999. ret = ath5k_hw_channel(ah, channel);
  1000. /*
  1001. * Activate PHY and wait
  1002. */
  1003. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1004. mdelay(1);
  1005. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1006. if (ret)
  1007. return ret;
  1008. /*
  1009. * Calibrate the radio chip
  1010. */
  1011. /* Remember normal state */
  1012. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  1013. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  1014. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  1015. /* Update radio registers */
  1016. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1017. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1018. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1019. AR5K_PHY_AGCCOARSE_LO)) |
  1020. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1021. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1022. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1023. AR5K_PHY_ADCSAT_THR)) |
  1024. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1025. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1026. udelay(20);
  1027. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1028. udelay(10);
  1029. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1030. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1031. mdelay(1);
  1032. /*
  1033. * Enable calibration and wait until completion
  1034. */
  1035. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1036. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1037. AR5K_PHY_AGCCTL_CAL, 0, 0);
  1038. /* Reset to normal state */
  1039. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1040. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1041. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1042. if (ret) {
  1043. DBG("ath5k: calibration timeout (%d MHz)\n",
  1044. channel->center_freq);
  1045. return ret;
  1046. }
  1047. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1048. /*
  1049. * Re-enable RX/TX and beacons
  1050. */
  1051. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1052. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  1053. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1054. return 0;
  1055. }
  1056. /*
  1057. * Perform a PHY calibration on RF5111/5112 and newer chips
  1058. */
  1059. static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
  1060. struct net80211_channel *channel)
  1061. {
  1062. u32 i_pwr, q_pwr;
  1063. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1064. int i;
  1065. if (!ah->ah_calibration ||
  1066. ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
  1067. goto done;
  1068. /* Calibration has finished, get the results and re-run */
  1069. for (i = 0; i <= 10; i++) {
  1070. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1071. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1072. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1073. }
  1074. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1075. q_coffd = q_pwr >> 7;
  1076. /* No correction */
  1077. if (i_coffd == 0 || q_coffd == 0)
  1078. goto done;
  1079. i_coff = ((-iq_corr) / i_coffd) & 0x3f;
  1080. /* Boundary check */
  1081. if (i_coff > 31)
  1082. i_coff = 31;
  1083. if (i_coff < -32)
  1084. i_coff = -32;
  1085. q_coff = (((s32)i_pwr / q_coffd) - 128) & 0x1f;
  1086. /* Boundary check */
  1087. if (q_coff > 15)
  1088. q_coff = 15;
  1089. if (q_coff < -16)
  1090. q_coff = -16;
  1091. /* Commit new I/Q value */
  1092. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
  1093. ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
  1094. /* Re-enable calibration -if we don't we'll commit
  1095. * the same values again and again */
  1096. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1097. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1098. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1099. done:
  1100. /* TODO: Separate noise floor calibration from I/Q calibration
  1101. * since noise floor calibration interrupts rx path while I/Q
  1102. * calibration doesn't. We don't need to run noise floor calibration
  1103. * as often as I/Q calibration.*/
  1104. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1105. /* Initiate a gain_F calibration */
  1106. ath5k_hw_request_rfgain_probe(ah);
  1107. return 0;
  1108. }
  1109. /*
  1110. * Perform a PHY calibration
  1111. */
  1112. int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1113. struct net80211_channel *channel)
  1114. {
  1115. int ret;
  1116. if (ah->ah_radio == AR5K_RF5110)
  1117. ret = ath5k_hw_rf5110_calibrate(ah, channel);
  1118. else
  1119. ret = ath5k_hw_rf511x_calibrate(ah, channel);
  1120. return ret;
  1121. }
  1122. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  1123. {
  1124. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  1125. return 0;
  1126. }
  1127. /********************\
  1128. Misc PHY functions
  1129. \********************/
  1130. /*
  1131. * Get the PHY Chip revision
  1132. */
  1133. u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
  1134. {
  1135. unsigned int i;
  1136. u32 srev;
  1137. u16 ret;
  1138. /*
  1139. * Set the radio chip access register
  1140. */
  1141. switch (chan) {
  1142. case CHANNEL_2GHZ:
  1143. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  1144. break;
  1145. case CHANNEL_5GHZ:
  1146. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1147. break;
  1148. default:
  1149. return 0;
  1150. }
  1151. mdelay(2);
  1152. /* ...wait until PHY is ready and read the selected radio revision */
  1153. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  1154. for (i = 0; i < 8; i++)
  1155. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  1156. if (ah->ah_version == AR5K_AR5210) {
  1157. srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
  1158. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  1159. } else {
  1160. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  1161. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  1162. ((srev & 0x0f) << 4), 8);
  1163. }
  1164. /* Reset to the 5GHz mode */
  1165. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1166. return ret;
  1167. }
  1168. void /*TODO:Boundary check*/
  1169. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, unsigned int ant)
  1170. {
  1171. if (ah->ah_version != AR5K_AR5210)
  1172. ath5k_hw_reg_write(ah, ant, AR5K_DEFAULT_ANTENNA);
  1173. }
  1174. unsigned int ath5k_hw_get_def_antenna(struct ath5k_hw *ah)
  1175. {
  1176. if (ah->ah_version != AR5K_AR5210)
  1177. return ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  1178. return 0; /*XXX: What do we return for 5210 ?*/
  1179. }
  1180. /****************\
  1181. * TX power setup *
  1182. \****************/
  1183. /*
  1184. * Helper functions
  1185. */
  1186. /*
  1187. * Do linear interpolation between two given (x, y) points
  1188. */
  1189. static s16
  1190. ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
  1191. s16 y_left, s16 y_right)
  1192. {
  1193. s16 ratio, result;
  1194. /* Avoid divide by zero and skip interpolation
  1195. * if we have the same point */
  1196. if ((x_left == x_right) || (y_left == y_right))
  1197. return y_left;
  1198. /*
  1199. * Since we use ints and not fps, we need to scale up in
  1200. * order to get a sane ratio value (or else we 'll eg. get
  1201. * always 1 instead of 1.25, 1.75 etc). We scale up by 100
  1202. * to have some accuracy both for 0.5 and 0.25 steps.
  1203. */
  1204. ratio = ((100 * y_right - 100 * y_left)/(x_right - x_left));
  1205. /* Now scale down to be in range */
  1206. result = y_left + (ratio * (target - x_left) / 100);
  1207. return result;
  1208. }
  1209. /*
  1210. * Find vertical boundary (min pwr) for the linear PCDAC curve.
  1211. *
  1212. * Since we have the top of the curve and we draw the line below
  1213. * until we reach 1 (1 pcdac step) we need to know which point
  1214. * (x value) that is so that we don't go below y axis and have negative
  1215. * pcdac values when creating the curve, or fill the table with zeroes.
  1216. */
  1217. static s16
  1218. ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
  1219. const s16 *pwrL, const s16 *pwrR)
  1220. {
  1221. s8 tmp;
  1222. s16 min_pwrL, min_pwrR;
  1223. s16 pwr_i;
  1224. if (pwrL[0] == pwrL[1])
  1225. min_pwrL = pwrL[0];
  1226. else {
  1227. pwr_i = pwrL[0];
  1228. do {
  1229. pwr_i--;
  1230. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1231. pwrL[0], pwrL[1],
  1232. stepL[0], stepL[1]);
  1233. } while (tmp > 1);
  1234. min_pwrL = pwr_i;
  1235. }
  1236. if (pwrR[0] == pwrR[1])
  1237. min_pwrR = pwrR[0];
  1238. else {
  1239. pwr_i = pwrR[0];
  1240. do {
  1241. pwr_i--;
  1242. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1243. pwrR[0], pwrR[1],
  1244. stepR[0], stepR[1]);
  1245. } while (tmp > 1);
  1246. min_pwrR = pwr_i;
  1247. }
  1248. /* Keep the right boundary so that it works for both curves */
  1249. return max(min_pwrL, min_pwrR);
  1250. }
  1251. /*
  1252. * Interpolate (pwr,vpd) points to create a Power to PDADC or a
  1253. * Power to PCDAC curve.
  1254. *
  1255. * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
  1256. * steps (offsets) on y axis. Power can go up to 31.5dB and max
  1257. * PCDAC/PDADC step for each curve is 64 but we can write more than
  1258. * one curves on hw so we can go up to 128 (which is the max step we
  1259. * can write on the final table).
  1260. *
  1261. * We write y values (PCDAC/PDADC steps) on hw.
  1262. */
  1263. static void
  1264. ath5k_create_power_curve(s16 pmin, s16 pmax,
  1265. const s16 *pwr, const u8 *vpd,
  1266. u8 num_points,
  1267. u8 *vpd_table, u8 type)
  1268. {
  1269. u8 idx[2] = { 0, 1 };
  1270. s16 pwr_i = 2*pmin;
  1271. int i;
  1272. if (num_points < 2)
  1273. return;
  1274. /* We want the whole line, so adjust boundaries
  1275. * to cover the entire power range. Note that
  1276. * power values are already 0.25dB so no need
  1277. * to multiply pwr_i by 2 */
  1278. if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
  1279. pwr_i = pmin;
  1280. pmin = 0;
  1281. pmax = 63;
  1282. }
  1283. /* Find surrounding turning points (TPs)
  1284. * and interpolate between them */
  1285. for (i = 0; (i <= (u16) (pmax - pmin)) &&
  1286. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  1287. /* We passed the right TP, move to the next set of TPs
  1288. * if we pass the last TP, extrapolate above using the last
  1289. * two TPs for ratio */
  1290. if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
  1291. idx[0]++;
  1292. idx[1]++;
  1293. }
  1294. vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
  1295. pwr[idx[0]], pwr[idx[1]],
  1296. vpd[idx[0]], vpd[idx[1]]);
  1297. /* Increase by 0.5dB
  1298. * (0.25 dB units) */
  1299. pwr_i += 2;
  1300. }
  1301. }
  1302. /*
  1303. * Get the surrounding per-channel power calibration piers
  1304. * for a given frequency so that we can interpolate between
  1305. * them and come up with an apropriate dataset for our current
  1306. * channel.
  1307. */
  1308. static void
  1309. ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
  1310. struct net80211_channel *channel,
  1311. struct ath5k_chan_pcal_info **pcinfo_l,
  1312. struct ath5k_chan_pcal_info **pcinfo_r)
  1313. {
  1314. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1315. struct ath5k_chan_pcal_info *pcinfo;
  1316. u8 idx_l, idx_r;
  1317. u8 mode, max, i;
  1318. u32 target = channel->center_freq;
  1319. idx_l = 0;
  1320. idx_r = 0;
  1321. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1322. pcinfo = ee->ee_pwr_cal_b;
  1323. mode = AR5K_EEPROM_MODE_11B;
  1324. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1325. pcinfo = ee->ee_pwr_cal_g;
  1326. mode = AR5K_EEPROM_MODE_11G;
  1327. } else {
  1328. pcinfo = ee->ee_pwr_cal_a;
  1329. mode = AR5K_EEPROM_MODE_11A;
  1330. }
  1331. max = ee->ee_n_piers[mode] - 1;
  1332. /* Frequency is below our calibrated
  1333. * range. Use the lowest power curve
  1334. * we have */
  1335. if (target < pcinfo[0].freq) {
  1336. idx_l = idx_r = 0;
  1337. goto done;
  1338. }
  1339. /* Frequency is above our calibrated
  1340. * range. Use the highest power curve
  1341. * we have */
  1342. if (target > pcinfo[max].freq) {
  1343. idx_l = idx_r = max;
  1344. goto done;
  1345. }
  1346. /* Frequency is inside our calibrated
  1347. * channel range. Pick the surrounding
  1348. * calibration piers so that we can
  1349. * interpolate */
  1350. for (i = 0; i <= max; i++) {
  1351. /* Frequency matches one of our calibration
  1352. * piers, no need to interpolate, just use
  1353. * that calibration pier */
  1354. if (pcinfo[i].freq == target) {
  1355. idx_l = idx_r = i;
  1356. goto done;
  1357. }
  1358. /* We found a calibration pier that's above
  1359. * frequency, use this pier and the previous
  1360. * one to interpolate */
  1361. if (target < pcinfo[i].freq) {
  1362. idx_r = i;
  1363. idx_l = idx_r - 1;
  1364. goto done;
  1365. }
  1366. }
  1367. done:
  1368. *pcinfo_l = &pcinfo[idx_l];
  1369. *pcinfo_r = &pcinfo[idx_r];
  1370. return;
  1371. }
  1372. /*
  1373. * Get the surrounding per-rate power calibration data
  1374. * for a given frequency and interpolate between power
  1375. * values to set max target power supported by hw for
  1376. * each rate.
  1377. */
  1378. static void
  1379. ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
  1380. struct net80211_channel *channel,
  1381. struct ath5k_rate_pcal_info *rates)
  1382. {
  1383. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1384. struct ath5k_rate_pcal_info *rpinfo;
  1385. u8 idx_l, idx_r;
  1386. u8 mode, max, i;
  1387. u32 target = channel->center_freq;
  1388. idx_l = 0;
  1389. idx_r = 0;
  1390. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1391. rpinfo = ee->ee_rate_tpwr_b;
  1392. mode = AR5K_EEPROM_MODE_11B;
  1393. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1394. rpinfo = ee->ee_rate_tpwr_g;
  1395. mode = AR5K_EEPROM_MODE_11G;
  1396. } else {
  1397. rpinfo = ee->ee_rate_tpwr_a;
  1398. mode = AR5K_EEPROM_MODE_11A;
  1399. }
  1400. max = ee->ee_rate_target_pwr_num[mode] - 1;
  1401. /* Get the surrounding calibration
  1402. * piers - same as above */
  1403. if (target < rpinfo[0].freq) {
  1404. idx_l = idx_r = 0;
  1405. goto done;
  1406. }
  1407. if (target > rpinfo[max].freq) {
  1408. idx_l = idx_r = max;
  1409. goto done;
  1410. }
  1411. for (i = 0; i <= max; i++) {
  1412. if (rpinfo[i].freq == target) {
  1413. idx_l = idx_r = i;
  1414. goto done;
  1415. }
  1416. if (target < rpinfo[i].freq) {
  1417. idx_r = i;
  1418. idx_l = idx_r - 1;
  1419. goto done;
  1420. }
  1421. }
  1422. done:
  1423. /* Now interpolate power value, based on the frequency */
  1424. rates->freq = target;
  1425. rates->target_power_6to24 =
  1426. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1427. rpinfo[idx_r].freq,
  1428. rpinfo[idx_l].target_power_6to24,
  1429. rpinfo[idx_r].target_power_6to24);
  1430. rates->target_power_36 =
  1431. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1432. rpinfo[idx_r].freq,
  1433. rpinfo[idx_l].target_power_36,
  1434. rpinfo[idx_r].target_power_36);
  1435. rates->target_power_48 =
  1436. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1437. rpinfo[idx_r].freq,
  1438. rpinfo[idx_l].target_power_48,
  1439. rpinfo[idx_r].target_power_48);
  1440. rates->target_power_54 =
  1441. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1442. rpinfo[idx_r].freq,
  1443. rpinfo[idx_l].target_power_54,
  1444. rpinfo[idx_r].target_power_54);
  1445. }
  1446. /*
  1447. * Get the max edge power for this channel if
  1448. * we have such data from EEPROM's Conformance Test
  1449. * Limits (CTL), and limit max power if needed.
  1450. *
  1451. * FIXME: Only works for world regulatory domains
  1452. */
  1453. static void
  1454. ath5k_get_max_ctl_power(struct ath5k_hw *ah,
  1455. struct net80211_channel *channel)
  1456. {
  1457. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1458. struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
  1459. u8 *ctl_val = ee->ee_ctl;
  1460. s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
  1461. s16 edge_pwr = 0;
  1462. u8 rep_idx;
  1463. u8 i, ctl_mode;
  1464. u8 ctl_idx = 0xFF;
  1465. u32 target = channel->center_freq;
  1466. /* Find out a CTL for our mode that's not mapped
  1467. * on a specific reg domain.
  1468. *
  1469. * TODO: Map our current reg domain to one of the 3 available
  1470. * reg domain ids so that we can support more CTLs. */
  1471. switch (channel->hw_value & CHANNEL_MODES) {
  1472. case CHANNEL_A:
  1473. ctl_mode = AR5K_CTL_11A | AR5K_CTL_NO_REGDOMAIN;
  1474. break;
  1475. case CHANNEL_G:
  1476. ctl_mode = AR5K_CTL_11G | AR5K_CTL_NO_REGDOMAIN;
  1477. break;
  1478. case CHANNEL_B:
  1479. ctl_mode = AR5K_CTL_11B | AR5K_CTL_NO_REGDOMAIN;
  1480. break;
  1481. case CHANNEL_T:
  1482. ctl_mode = AR5K_CTL_TURBO | AR5K_CTL_NO_REGDOMAIN;
  1483. break;
  1484. case CHANNEL_TG:
  1485. ctl_mode = AR5K_CTL_TURBOG | AR5K_CTL_NO_REGDOMAIN;
  1486. break;
  1487. case CHANNEL_XR:
  1488. /* Fall through */
  1489. default:
  1490. return;
  1491. }
  1492. for (i = 0; i < ee->ee_ctls; i++) {
  1493. if (ctl_val[i] == ctl_mode) {
  1494. ctl_idx = i;
  1495. break;
  1496. }
  1497. }
  1498. /* If we have a CTL dataset available grab it and find the
  1499. * edge power for our frequency */
  1500. if (ctl_idx == 0xFF)
  1501. return;
  1502. /* Edge powers are sorted by frequency from lower
  1503. * to higher. Each CTL corresponds to 8 edge power
  1504. * measurements. */
  1505. rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
  1506. /* Don't do boundaries check because we
  1507. * might have more that one bands defined
  1508. * for this mode */
  1509. /* Get the edge power that's closer to our
  1510. * frequency */
  1511. for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
  1512. rep_idx += i;
  1513. if (target <= rep[rep_idx].freq)
  1514. edge_pwr = (s16) rep[rep_idx].edge;
  1515. }
  1516. if (edge_pwr) {
  1517. ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
  1518. }
  1519. }
  1520. /*
  1521. * Power to PCDAC table functions
  1522. */
  1523. /*
  1524. * Fill Power to PCDAC table on RF5111
  1525. *
  1526. * No further processing is needed for RF5111, the only thing we have to
  1527. * do is fill the values below and above calibration range since eeprom data
  1528. * may not cover the entire PCDAC table.
  1529. */
  1530. static void
  1531. ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
  1532. s16 *table_max)
  1533. {
  1534. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1535. u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
  1536. u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
  1537. s16 min_pwr, max_pwr;
  1538. /* Get table boundaries */
  1539. min_pwr = table_min[0];
  1540. pcdac_0 = pcdac_tmp[0];
  1541. max_pwr = table_max[0];
  1542. pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
  1543. /* Extrapolate below minimum using pcdac_0 */
  1544. pcdac_i = 0;
  1545. for (i = 0; i < min_pwr; i++)
  1546. pcdac_out[pcdac_i++] = pcdac_0;
  1547. /* Copy values from pcdac_tmp */
  1548. pwr_idx = min_pwr;
  1549. for (i = 0 ; pwr_idx <= max_pwr &&
  1550. pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
  1551. pcdac_out[pcdac_i++] = pcdac_tmp[i];
  1552. pwr_idx++;
  1553. }
  1554. /* Extrapolate above maximum */
  1555. while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
  1556. pcdac_out[pcdac_i++] = pcdac_n;
  1557. }
  1558. /*
  1559. * Combine available XPD Curves and fill Linear Power to PCDAC table
  1560. * on RF5112
  1561. *
  1562. * RFX112 can have up to 2 curves (one for low txpower range and one for
  1563. * higher txpower range). We need to put them both on pcdac_out and place
  1564. * them in the correct location. In case we only have one curve available
  1565. * just fit it on pcdac_out (it's supposed to cover the entire range of
  1566. * available pwr levels since it's always the higher power curve). Extrapolate
  1567. * below and above final table if needed.
  1568. */
  1569. static void
  1570. ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
  1571. s16 *table_max, u8 pdcurves)
  1572. {
  1573. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1574. u8 *pcdac_low_pwr;
  1575. u8 *pcdac_high_pwr;
  1576. u8 *pcdac_tmp;
  1577. u8 pwr;
  1578. s16 max_pwr_idx;
  1579. s16 min_pwr_idx;
  1580. s16 mid_pwr_idx = 0;
  1581. /* Edge flag turs on the 7nth bit on the PCDAC
  1582. * to delcare the higher power curve (force values
  1583. * to be greater than 64). If we only have one curve
  1584. * we don't need to set this, if we have 2 curves and
  1585. * fill the table backwards this can also be used to
  1586. * switch from higher power curve to lower power curve */
  1587. u8 edge_flag;
  1588. int i;
  1589. /* When we have only one curve available
  1590. * that's the higher power curve. If we have
  1591. * two curves the first is the high power curve
  1592. * and the next is the low power curve. */
  1593. if (pdcurves > 1) {
  1594. pcdac_low_pwr = ah->ah_txpower.tmpL[1];
  1595. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  1596. mid_pwr_idx = table_max[1] - table_min[1] - 1;
  1597. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  1598. /* If table size goes beyond 31.5dB, keep the
  1599. * upper 31.5dB range when setting tx power.
  1600. * Note: 126 = 31.5 dB in quarter dB steps */
  1601. if (table_max[0] - table_min[1] > 126)
  1602. min_pwr_idx = table_max[0] - 126;
  1603. else
  1604. min_pwr_idx = table_min[1];
  1605. /* Since we fill table backwards
  1606. * start from high power curve */
  1607. pcdac_tmp = pcdac_high_pwr;
  1608. edge_flag = 0x40;
  1609. } else {
  1610. pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
  1611. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  1612. min_pwr_idx = table_min[0];
  1613. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  1614. pcdac_tmp = pcdac_high_pwr;
  1615. edge_flag = 0;
  1616. }
  1617. /* This is used when setting tx power*/
  1618. ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
  1619. /* Fill Power to PCDAC table backwards */
  1620. pwr = max_pwr_idx;
  1621. for (i = 63; i >= 0; i--) {
  1622. /* Entering lower power range, reset
  1623. * edge flag and set pcdac_tmp to lower
  1624. * power curve.*/
  1625. if (edge_flag == 0x40 &&
  1626. (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
  1627. edge_flag = 0x00;
  1628. pcdac_tmp = pcdac_low_pwr;
  1629. pwr = mid_pwr_idx/2;
  1630. }
  1631. /* Don't go below 1, extrapolate below if we have
  1632. * already swithced to the lower power curve -or
  1633. * we only have one curve and edge_flag is zero
  1634. * anyway */
  1635. if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
  1636. while (i >= 0) {
  1637. pcdac_out[i] = pcdac_out[i + 1];
  1638. i--;
  1639. }
  1640. break;
  1641. }
  1642. pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
  1643. /* Extrapolate above if pcdac is greater than
  1644. * 126 -this can happen because we OR pcdac_out
  1645. * value with edge_flag on high power curve */
  1646. if (pcdac_out[i] > 126)
  1647. pcdac_out[i] = 126;
  1648. /* Decrease by a 0.5dB step */
  1649. pwr--;
  1650. }
  1651. }
  1652. /* Write PCDAC values on hw */
  1653. static void
  1654. ath5k_setup_pcdac_table(struct ath5k_hw *ah)
  1655. {
  1656. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1657. int i;
  1658. /*
  1659. * Write TX power values
  1660. */
  1661. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1662. ath5k_hw_reg_write(ah,
  1663. (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
  1664. (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
  1665. AR5K_PHY_PCDAC_TXPOWER(i));
  1666. }
  1667. }
  1668. /*
  1669. * Power to PDADC table functions
  1670. */
  1671. /*
  1672. * Set the gain boundaries and create final Power to PDADC table
  1673. *
  1674. * We can have up to 4 pd curves, we need to do a simmilar process
  1675. * as we do for RF5112. This time we don't have an edge_flag but we
  1676. * set the gain boundaries on a separate register.
  1677. */
  1678. static void
  1679. ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
  1680. s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
  1681. {
  1682. u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
  1683. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  1684. u8 *pdadc_tmp;
  1685. s16 pdadc_0;
  1686. u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
  1687. u8 pd_gain_overlap;
  1688. /* Note: Register value is initialized on initvals
  1689. * there is no feedback from hw.
  1690. * XXX: What about pd_gain_overlap from EEPROM ? */
  1691. pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
  1692. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
  1693. /* Create final PDADC table */
  1694. for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
  1695. pdadc_tmp = ah->ah_txpower.tmpL[pdg];
  1696. if (pdg == pdcurves - 1)
  1697. /* 2 dB boundary stretch for last
  1698. * (higher power) curve */
  1699. gain_boundaries[pdg] = pwr_max[pdg] + 4;
  1700. else
  1701. /* Set gain boundary in the middle
  1702. * between this curve and the next one */
  1703. gain_boundaries[pdg] =
  1704. (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
  1705. /* Sanity check in case our 2 db stretch got out of
  1706. * range. */
  1707. if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
  1708. gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
  1709. /* For the first curve (lower power)
  1710. * start from 0 dB */
  1711. if (pdg == 0)
  1712. pdadc_0 = 0;
  1713. else
  1714. /* For the other curves use the gain overlap */
  1715. pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
  1716. pd_gain_overlap;
  1717. /* Force each power step to be at least 0.5 dB */
  1718. if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
  1719. pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
  1720. else
  1721. pwr_step = 1;
  1722. /* If pdadc_0 is negative, we need to extrapolate
  1723. * below this pdgain by a number of pwr_steps */
  1724. while ((pdadc_0 < 0) && (pdadc_i < 128)) {
  1725. s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
  1726. pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
  1727. pdadc_0++;
  1728. }
  1729. /* Set last pwr level, using gain boundaries */
  1730. pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
  1731. /* Limit it to be inside pwr range */
  1732. table_size = pwr_max[pdg] - pwr_min[pdg];
  1733. max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
  1734. /* Fill pdadc_out table */
  1735. while (pdadc_0 < max_idx)
  1736. pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
  1737. /* Need to extrapolate above this pdgain? */
  1738. if (pdadc_n <= max_idx)
  1739. continue;
  1740. /* Force each power step to be at least 0.5 dB */
  1741. if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
  1742. pwr_step = pdadc_tmp[table_size - 1] -
  1743. pdadc_tmp[table_size - 2];
  1744. else
  1745. pwr_step = 1;
  1746. /* Extrapolate above */
  1747. while ((pdadc_0 < (s16) pdadc_n) &&
  1748. (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
  1749. s16 tmp = pdadc_tmp[table_size - 1] +
  1750. (pdadc_0 - max_idx) * pwr_step;
  1751. pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
  1752. pdadc_0++;
  1753. }
  1754. }
  1755. while (pdg < AR5K_EEPROM_N_PD_GAINS) {
  1756. gain_boundaries[pdg] = gain_boundaries[pdg - 1];
  1757. pdg++;
  1758. }
  1759. while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
  1760. pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
  1761. pdadc_i++;
  1762. }
  1763. /* Set gain boundaries */
  1764. ath5k_hw_reg_write(ah,
  1765. AR5K_REG_SM(pd_gain_overlap,
  1766. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
  1767. AR5K_REG_SM(gain_boundaries[0],
  1768. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
  1769. AR5K_REG_SM(gain_boundaries[1],
  1770. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
  1771. AR5K_REG_SM(gain_boundaries[2],
  1772. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
  1773. AR5K_REG_SM(gain_boundaries[3],
  1774. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
  1775. AR5K_PHY_TPC_RG5);
  1776. /* Used for setting rate power table */
  1777. ah->ah_txpower.txp_min_idx = pwr_min[0];
  1778. }
  1779. /* Write PDADC values on hw */
  1780. static void
  1781. ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
  1782. u8 pdcurves, u8 *pdg_to_idx)
  1783. {
  1784. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  1785. u32 reg;
  1786. u8 i;
  1787. /* Select the right pdgain curves */
  1788. /* Clear current settings */
  1789. reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
  1790. reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
  1791. AR5K_PHY_TPC_RG1_PDGAIN_2 |
  1792. AR5K_PHY_TPC_RG1_PDGAIN_3 |
  1793. AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  1794. /*
  1795. * Use pd_gains curve from eeprom
  1796. *
  1797. * This overrides the default setting from initvals
  1798. * in case some vendors (e.g. Zcomax) don't use the default
  1799. * curves. If we don't honor their settings we 'll get a
  1800. * 5dB (1 * gain overlap ?) drop.
  1801. */
  1802. reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  1803. switch (pdcurves) {
  1804. case 3:
  1805. reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
  1806. /* Fall through */
  1807. case 2:
  1808. reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
  1809. /* Fall through */
  1810. case 1:
  1811. reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
  1812. break;
  1813. }
  1814. ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
  1815. /*
  1816. * Write TX power values
  1817. */
  1818. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1819. ath5k_hw_reg_write(ah,
  1820. ((pdadc_out[4*i + 0] & 0xff) << 0) |
  1821. ((pdadc_out[4*i + 1] & 0xff) << 8) |
  1822. ((pdadc_out[4*i + 2] & 0xff) << 16) |
  1823. ((pdadc_out[4*i + 3] & 0xff) << 24),
  1824. AR5K_PHY_PDADC_TXPOWER(i));
  1825. }
  1826. }
  1827. /*
  1828. * Common code for PCDAC/PDADC tables
  1829. */
  1830. /*
  1831. * This is the main function that uses all of the above
  1832. * to set PCDAC/PDADC table on hw for the current channel.
  1833. * This table is used for tx power calibration on the basband,
  1834. * without it we get weird tx power levels and in some cases
  1835. * distorted spectral mask
  1836. */
  1837. static int
  1838. ath5k_setup_channel_powertable(struct ath5k_hw *ah,
  1839. struct net80211_channel *channel,
  1840. u8 ee_mode, u8 type)
  1841. {
  1842. struct ath5k_pdgain_info *pdg_L, *pdg_R;
  1843. struct ath5k_chan_pcal_info *pcinfo_L;
  1844. struct ath5k_chan_pcal_info *pcinfo_R;
  1845. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1846. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  1847. s16 table_min[AR5K_EEPROM_N_PD_GAINS];
  1848. s16 table_max[AR5K_EEPROM_N_PD_GAINS];
  1849. u8 *tmpL;
  1850. u8 *tmpR;
  1851. u32 target = channel->center_freq;
  1852. int pdg, i;
  1853. /* Get surounding freq piers for this channel */
  1854. ath5k_get_chan_pcal_surrounding_piers(ah, channel,
  1855. &pcinfo_L,
  1856. &pcinfo_R);
  1857. /* Loop over pd gain curves on
  1858. * surounding freq piers by index */
  1859. for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
  1860. /* Fill curves in reverse order
  1861. * from lower power (max gain)
  1862. * to higher power. Use curve -> idx
  1863. * backmaping we did on eeprom init */
  1864. u8 idx = pdg_curve_to_idx[pdg];
  1865. /* Grab the needed curves by index */
  1866. pdg_L = &pcinfo_L->pd_curves[idx];
  1867. pdg_R = &pcinfo_R->pd_curves[idx];
  1868. /* Initialize the temp tables */
  1869. tmpL = ah->ah_txpower.tmpL[pdg];
  1870. tmpR = ah->ah_txpower.tmpR[pdg];
  1871. /* Set curve's x boundaries and create
  1872. * curves so that they cover the same
  1873. * range (if we don't do that one table
  1874. * will have values on some range and the
  1875. * other one won't have any so interpolation
  1876. * will fail) */
  1877. table_min[pdg] = min(pdg_L->pd_pwr[0],
  1878. pdg_R->pd_pwr[0]) / 2;
  1879. table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  1880. pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
  1881. /* Now create the curves on surrounding channels
  1882. * and interpolate if needed to get the final
  1883. * curve for this gain on this channel */
  1884. switch (type) {
  1885. case AR5K_PWRTABLE_LINEAR_PCDAC:
  1886. /* Override min/max so that we don't loose
  1887. * accuracy (don't divide by 2) */
  1888. table_min[pdg] = min(pdg_L->pd_pwr[0],
  1889. pdg_R->pd_pwr[0]);
  1890. table_max[pdg] =
  1891. max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  1892. pdg_R->pd_pwr[pdg_R->pd_points - 1]);
  1893. /* Override minimum so that we don't get
  1894. * out of bounds while extrapolating
  1895. * below. Don't do this when we have 2
  1896. * curves and we are on the high power curve
  1897. * because table_min is ok in this case */
  1898. if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
  1899. table_min[pdg] =
  1900. ath5k_get_linear_pcdac_min(pdg_L->pd_step,
  1901. pdg_R->pd_step,
  1902. pdg_L->pd_pwr,
  1903. pdg_R->pd_pwr);
  1904. /* Don't go too low because we will
  1905. * miss the upper part of the curve.
  1906. * Note: 126 = 31.5dB (max power supported)
  1907. * in 0.25dB units */
  1908. if (table_max[pdg] - table_min[pdg] > 126)
  1909. table_min[pdg] = table_max[pdg] - 126;
  1910. }
  1911. /* Fall through */
  1912. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  1913. case AR5K_PWRTABLE_PWR_TO_PDADC:
  1914. ath5k_create_power_curve(table_min[pdg],
  1915. table_max[pdg],
  1916. pdg_L->pd_pwr,
  1917. pdg_L->pd_step,
  1918. pdg_L->pd_points, tmpL, type);
  1919. /* We are in a calibration
  1920. * pier, no need to interpolate
  1921. * between freq piers */
  1922. if (pcinfo_L == pcinfo_R)
  1923. continue;
  1924. ath5k_create_power_curve(table_min[pdg],
  1925. table_max[pdg],
  1926. pdg_R->pd_pwr,
  1927. pdg_R->pd_step,
  1928. pdg_R->pd_points, tmpR, type);
  1929. break;
  1930. default:
  1931. return -EINVAL;
  1932. }
  1933. /* Interpolate between curves
  1934. * of surounding freq piers to
  1935. * get the final curve for this
  1936. * pd gain. Re-use tmpL for interpolation
  1937. * output */
  1938. for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
  1939. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  1940. tmpL[i] = (u8) ath5k_get_interpolated_value(target,
  1941. (s16) pcinfo_L->freq,
  1942. (s16) pcinfo_R->freq,
  1943. (s16) tmpL[i],
  1944. (s16) tmpR[i]);
  1945. }
  1946. }
  1947. /* Now we have a set of curves for this
  1948. * channel on tmpL (x range is table_max - table_min
  1949. * and y values are tmpL[pdg][]) sorted in the same
  1950. * order as EEPROM (because we've used the backmaping).
  1951. * So for RF5112 it's from higher power to lower power
  1952. * and for RF2413 it's from lower power to higher power.
  1953. * For RF5111 we only have one curve. */
  1954. /* Fill min and max power levels for this
  1955. * channel by interpolating the values on
  1956. * surounding channels to complete the dataset */
  1957. ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
  1958. (s16) pcinfo_L->freq,
  1959. (s16) pcinfo_R->freq,
  1960. pcinfo_L->min_pwr, pcinfo_R->min_pwr);
  1961. ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
  1962. (s16) pcinfo_L->freq,
  1963. (s16) pcinfo_R->freq,
  1964. pcinfo_L->max_pwr, pcinfo_R->max_pwr);
  1965. /* We are ready to go, fill PCDAC/PDADC
  1966. * table and write settings on hardware */
  1967. switch (type) {
  1968. case AR5K_PWRTABLE_LINEAR_PCDAC:
  1969. /* For RF5112 we can have one or two curves
  1970. * and each curve covers a certain power lvl
  1971. * range so we need to do some more processing */
  1972. ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
  1973. ee->ee_pd_gains[ee_mode]);
  1974. /* Set txp.offset so that we can
  1975. * match max power value with max
  1976. * table index */
  1977. ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
  1978. /* Write settings on hw */
  1979. ath5k_setup_pcdac_table(ah);
  1980. break;
  1981. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  1982. /* We are done for RF5111 since it has only
  1983. * one curve, just fit the curve on the table */
  1984. ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
  1985. /* No rate powertable adjustment for RF5111 */
  1986. ah->ah_txpower.txp_min_idx = 0;
  1987. ah->ah_txpower.txp_offset = 0;
  1988. /* Write settings on hw */
  1989. ath5k_setup_pcdac_table(ah);
  1990. break;
  1991. case AR5K_PWRTABLE_PWR_TO_PDADC:
  1992. /* Set PDADC boundaries and fill
  1993. * final PDADC table */
  1994. ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
  1995. ee->ee_pd_gains[ee_mode]);
  1996. /* Write settings on hw */
  1997. ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
  1998. /* Set txp.offset, note that table_min
  1999. * can be negative */
  2000. ah->ah_txpower.txp_offset = table_min[0];
  2001. break;
  2002. default:
  2003. return -EINVAL;
  2004. }
  2005. return 0;
  2006. }
  2007. /*
  2008. * Per-rate tx power setting
  2009. *
  2010. * This is the code that sets the desired tx power (below
  2011. * maximum) on hw for each rate (we also have TPC that sets
  2012. * power per packet). We do that by providing an index on the
  2013. * PCDAC/PDADC table we set up.
  2014. */
  2015. /*
  2016. * Set rate power table
  2017. *
  2018. * For now we only limit txpower based on maximum tx power
  2019. * supported by hw (what's inside rate_info). We need to limit
  2020. * this even more, based on regulatory domain etc.
  2021. *
  2022. * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
  2023. * and is indexed as follows:
  2024. * rates[0] - rates[7] -> OFDM rates
  2025. * rates[8] - rates[14] -> CCK rates
  2026. * rates[15] -> XR rates (they all have the same power)
  2027. */
  2028. static void
  2029. ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
  2030. struct ath5k_rate_pcal_info *rate_info,
  2031. u8 ee_mode)
  2032. {
  2033. unsigned int i;
  2034. u16 *rates;
  2035. /* max_pwr is power level we got from driver/user in 0.5dB
  2036. * units, switch to 0.25dB units so we can compare */
  2037. max_pwr *= 2;
  2038. max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
  2039. /* apply rate limits */
  2040. rates = ah->ah_txpower.txp_rates_power_table;
  2041. /* OFDM rates 6 to 24Mb/s */
  2042. for (i = 0; i < 5; i++)
  2043. rates[i] = min(max_pwr, rate_info->target_power_6to24);
  2044. /* Rest OFDM rates */
  2045. rates[5] = min(rates[0], rate_info->target_power_36);
  2046. rates[6] = min(rates[0], rate_info->target_power_48);
  2047. rates[7] = min(rates[0], rate_info->target_power_54);
  2048. /* CCK rates */
  2049. /* 1L */
  2050. rates[8] = min(rates[0], rate_info->target_power_6to24);
  2051. /* 2L */
  2052. rates[9] = min(rates[0], rate_info->target_power_36);
  2053. /* 2S */
  2054. rates[10] = min(rates[0], rate_info->target_power_36);
  2055. /* 5L */
  2056. rates[11] = min(rates[0], rate_info->target_power_48);
  2057. /* 5S */
  2058. rates[12] = min(rates[0], rate_info->target_power_48);
  2059. /* 11L */
  2060. rates[13] = min(rates[0], rate_info->target_power_54);
  2061. /* 11S */
  2062. rates[14] = min(rates[0], rate_info->target_power_54);
  2063. /* XR rates */
  2064. rates[15] = min(rates[0], rate_info->target_power_6to24);
  2065. /* CCK rates have different peak to average ratio
  2066. * so we have to tweak their power so that gainf
  2067. * correction works ok. For this we use OFDM to
  2068. * CCK delta from eeprom */
  2069. if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
  2070. (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
  2071. for (i = 8; i <= 15; i++)
  2072. rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
  2073. ah->ah_txpower.txp_min_pwr = rates[7];
  2074. ah->ah_txpower.txp_max_pwr = rates[0];
  2075. ah->ah_txpower.txp_ofdm = rates[7];
  2076. }
  2077. /*
  2078. * Set transmition power
  2079. */
  2080. int
  2081. ath5k_hw_txpower(struct ath5k_hw *ah, struct net80211_channel *channel,
  2082. u8 ee_mode, u8 txpower)
  2083. {
  2084. struct ath5k_rate_pcal_info rate_info;
  2085. u8 type;
  2086. int ret;
  2087. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  2088. DBG("ath5k: invalid tx power %d\n", txpower);
  2089. return -EINVAL;
  2090. }
  2091. if (txpower == 0)
  2092. txpower = AR5K_TUNE_DEFAULT_TXPOWER;
  2093. /* Reset TX power values */
  2094. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  2095. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  2096. ah->ah_txpower.txp_min_pwr = 0;
  2097. ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
  2098. /* Initialize TX power table */
  2099. switch (ah->ah_radio) {
  2100. case AR5K_RF5111:
  2101. type = AR5K_PWRTABLE_PWR_TO_PCDAC;
  2102. break;
  2103. case AR5K_RF5112:
  2104. type = AR5K_PWRTABLE_LINEAR_PCDAC;
  2105. break;
  2106. case AR5K_RF2413:
  2107. case AR5K_RF5413:
  2108. case AR5K_RF2316:
  2109. case AR5K_RF2317:
  2110. case AR5K_RF2425:
  2111. type = AR5K_PWRTABLE_PWR_TO_PDADC;
  2112. break;
  2113. default:
  2114. return -EINVAL;
  2115. }
  2116. /* FIXME: Only on channel/mode change */
  2117. ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
  2118. if (ret)
  2119. return ret;
  2120. /* Limit max power if we have a CTL available */
  2121. ath5k_get_max_ctl_power(ah, channel);
  2122. /* FIXME: Tx power limit for this regdomain
  2123. * XXX: Mac80211/CRDA will do that anyway ? */
  2124. /* FIXME: Antenna reduction stuff */
  2125. /* FIXME: Limit power on turbo modes */
  2126. /* FIXME: TPC scale reduction */
  2127. /* Get surounding channels for per-rate power table
  2128. * calibration */
  2129. ath5k_get_rate_pcal_data(ah, channel, &rate_info);
  2130. /* Setup rate power table */
  2131. ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
  2132. /* Write rate power table on hw */
  2133. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  2134. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  2135. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  2136. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  2137. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  2138. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  2139. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  2140. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  2141. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  2142. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  2143. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  2144. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  2145. /* FIXME: TPC support */
  2146. if (ah->ah_txpower.txp_tpc) {
  2147. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  2148. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2149. ath5k_hw_reg_write(ah,
  2150. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
  2151. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
  2152. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
  2153. AR5K_TPC);
  2154. } else {
  2155. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
  2156. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2157. }
  2158. return 0;
  2159. }
  2160. int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 mode, u8 txpower)
  2161. {
  2162. struct net80211_channel *channel = ah->ah_current_channel;
  2163. DBG2("ath5k: changing txpower to %d\n", txpower);
  2164. return ath5k_hw_txpower(ah, channel, mode, txpower);
  2165. }
  2166. #undef _ATH5K_PHY