phy.c 79 KB

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