phy.c 81 KB

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