phy.c 82 KB

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