phy.c 81 KB

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