phy.c 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958
  1. /*
  2. * Copyright (c) 2004-2007 Reyk Floeter <reyk@openbsd.org>
  3. * Copyright (c) 2006-2009 Nick Kossifidis <mickflemm@gmail.com>
  4. * Copyright (c) 2007-2008 Jiri Slaby <jirislaby@gmail.com>
  5. * Copyright (c) 2008-2009 Felix Fietkau <nbd@openwrt.org>
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  17. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. *
  19. */
  20. /***********************\
  21. * PHY related functions *
  22. \***********************/
  23. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  24. #include <linux/delay.h>
  25. #include <linux/slab.h>
  26. #include <linux/sort.h>
  27. #include <linux/unaligned.h>
  28. #include "ath5k.h"
  29. #include "reg.h"
  30. #include "rfbuffer.h"
  31. #include "rfgain.h"
  32. #include "../regd.h"
  33. /**
  34. * DOC: PHY related functions
  35. *
  36. * Here we handle the low-level functions related to baseband
  37. * and analog frontend (RF) parts. This is by far the most complex
  38. * part of the hw code so make sure you know what you are doing.
  39. *
  40. * Here is a list of what this is all about:
  41. *
  42. * - Channel setting/switching
  43. *
  44. * - Automatic Gain Control (AGC) calibration
  45. *
  46. * - Noise Floor calibration
  47. *
  48. * - I/Q imbalance calibration (QAM correction)
  49. *
  50. * - Calibration due to thermal changes (gain_F)
  51. *
  52. * - Spur noise mitigation
  53. *
  54. * - RF/PHY initialization for the various operating modes and bwmodes
  55. *
  56. * - Antenna control
  57. *
  58. * - TX power control per channel/rate/packet type
  59. *
  60. * Also have in mind we never got documentation for most of these
  61. * functions, what we have comes mostly from Atheros's code, reverse
  62. * engineering and patent docs/presentations etc.
  63. */
  64. /******************\
  65. * Helper functions *
  66. \******************/
  67. /**
  68. * ath5k_hw_radio_revision() - Get the PHY Chip revision
  69. * @ah: The &struct ath5k_hw
  70. * @band: One of enum nl80211_band
  71. *
  72. * Returns the revision number of a 2GHz, 5GHz or single chip
  73. * radio.
  74. */
  75. u16
  76. ath5k_hw_radio_revision(struct ath5k_hw *ah, enum nl80211_band band)
  77. {
  78. unsigned int i;
  79. u32 srev;
  80. u16 ret;
  81. /*
  82. * Set the radio chip access register
  83. */
  84. switch (band) {
  85. case NL80211_BAND_2GHZ:
  86. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  87. break;
  88. case NL80211_BAND_5GHZ:
  89. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  90. break;
  91. default:
  92. return 0;
  93. }
  94. usleep_range(2000, 2500);
  95. /* ...wait until PHY is ready and read the selected radio revision */
  96. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  97. for (i = 0; i < 8; i++)
  98. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  99. if (ah->ah_version == AR5K_AR5210) {
  100. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(256)) >> 28) & 0xf;
  101. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  102. } else {
  103. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  104. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  105. ((srev & 0x0f) << 4), 8);
  106. }
  107. /* Reset to the 5GHz mode */
  108. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  109. return ret;
  110. }
  111. /**
  112. * ath5k_channel_ok() - Check if a channel is supported by the hw
  113. * @ah: The &struct ath5k_hw
  114. * @channel: The &struct ieee80211_channel
  115. *
  116. * Note: We don't do any regulatory domain checks here, it's just
  117. * a sanity check.
  118. */
  119. bool
  120. ath5k_channel_ok(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  121. {
  122. u16 freq = channel->center_freq;
  123. /* Check if the channel is in our supported range */
  124. if (channel->band == NL80211_BAND_2GHZ) {
  125. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  126. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  127. return true;
  128. } else if (channel->band == NL80211_BAND_5GHZ)
  129. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  130. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  131. return true;
  132. return false;
  133. }
  134. /**
  135. * ath5k_hw_chan_has_spur_noise() - Check if channel is sensitive to spur noise
  136. * @ah: The &struct ath5k_hw
  137. * @channel: The &struct ieee80211_channel
  138. */
  139. bool
  140. ath5k_hw_chan_has_spur_noise(struct ath5k_hw *ah,
  141. struct ieee80211_channel *channel)
  142. {
  143. u8 refclk_freq;
  144. if ((ah->ah_radio == AR5K_RF5112) ||
  145. (ah->ah_radio == AR5K_RF5413) ||
  146. (ah->ah_radio == AR5K_RF2413) ||
  147. (ah->ah_mac_version == (AR5K_SREV_AR2417 >> 4)))
  148. refclk_freq = 40;
  149. else
  150. refclk_freq = 32;
  151. if ((channel->center_freq % refclk_freq != 0) &&
  152. ((channel->center_freq % refclk_freq < 10) ||
  153. (channel->center_freq % refclk_freq > 22)))
  154. return true;
  155. else
  156. return false;
  157. }
  158. /**
  159. * ath5k_hw_rfb_op() - Perform an operation on the given RF Buffer
  160. * @ah: The &struct ath5k_hw
  161. * @rf_regs: The struct ath5k_rf_reg
  162. * @val: New value
  163. * @reg_id: RF register ID
  164. * @set: Indicate we need to swap data
  165. *
  166. * This is an internal function used to modify RF Banks before
  167. * writing them to AR5K_RF_BUFFER. Check out rfbuffer.h for more
  168. * infos.
  169. */
  170. static unsigned int
  171. ath5k_hw_rfb_op(struct ath5k_hw *ah, const struct ath5k_rf_reg *rf_regs,
  172. u32 val, u8 reg_id, bool set)
  173. {
  174. const struct ath5k_rf_reg *rfreg = NULL;
  175. u8 offset, bank, num_bits, col, position;
  176. u16 entry;
  177. u32 mask, data, last_bit, bits_shifted, first_bit;
  178. u32 *rfb;
  179. s32 bits_left;
  180. int i;
  181. data = 0;
  182. rfb = ah->ah_rf_banks;
  183. for (i = 0; i < ah->ah_rf_regs_count; i++) {
  184. if (rf_regs[i].index == reg_id) {
  185. rfreg = &rf_regs[i];
  186. break;
  187. }
  188. }
  189. if (rfb == NULL || rfreg == NULL) {
  190. ATH5K_PRINTF("Rf register not found!\n");
  191. /* should not happen */
  192. return 0;
  193. }
  194. bank = rfreg->bank;
  195. num_bits = rfreg->field.len;
  196. first_bit = rfreg->field.pos;
  197. col = rfreg->field.col;
  198. /* first_bit is an offset from bank's
  199. * start. Since we have all banks on
  200. * the same array, we use this offset
  201. * to mark each bank's start */
  202. offset = ah->ah_offset[bank];
  203. /* Boundary check */
  204. if (!(col <= 3 && num_bits <= 32 && first_bit + num_bits <= 319)) {
  205. ATH5K_PRINTF("invalid values at offset %u\n", offset);
  206. return 0;
  207. }
  208. entry = ((first_bit - 1) / 8) + offset;
  209. position = (first_bit - 1) % 8;
  210. if (set)
  211. data = ath5k_hw_bitswap(val, num_bits);
  212. for (bits_shifted = 0, bits_left = num_bits; bits_left > 0;
  213. position = 0, entry++) {
  214. last_bit = (position + bits_left > 8) ? 8 :
  215. position + bits_left;
  216. mask = (((1 << last_bit) - 1) ^ ((1 << position) - 1)) <<
  217. (col * 8);
  218. if (set) {
  219. rfb[entry] &= ~mask;
  220. rfb[entry] |= ((data << position) << (col * 8)) & mask;
  221. data >>= (8 - position);
  222. } else {
  223. data |= (((rfb[entry] & mask) >> (col * 8)) >> position)
  224. << bits_shifted;
  225. bits_shifted += last_bit - position;
  226. }
  227. bits_left -= 8 - position;
  228. }
  229. data = set ? 1 : ath5k_hw_bitswap(data, num_bits);
  230. return data;
  231. }
  232. /**
  233. * ath5k_hw_write_ofdm_timings() - set OFDM timings on AR5212
  234. * @ah: the &struct ath5k_hw
  235. * @channel: the currently set channel upon reset
  236. *
  237. * Write the delta slope coefficient (used on pilot tracking ?) for OFDM
  238. * operation on the AR5212 upon reset. This is a helper for ath5k_hw_phy_init.
  239. *
  240. * Since delta slope is floating point we split it on its exponent and
  241. * mantissa and provide these values on hw.
  242. *
  243. * For more infos i think this patent is related
  244. * "http://www.freepatentsonline.com/7184495.html"
  245. */
  246. static inline int
  247. ath5k_hw_write_ofdm_timings(struct ath5k_hw *ah,
  248. struct ieee80211_channel *channel)
  249. {
  250. /* Get exponent and mantissa and set it */
  251. u32 coef_scaled, coef_exp, coef_man,
  252. ds_coef_exp, ds_coef_man, clock;
  253. BUG_ON(!(ah->ah_version == AR5K_AR5212) ||
  254. (channel->hw_value == AR5K_MODE_11B));
  255. /* Get coefficient
  256. * ALGO: coef = (5 * clock / carrier_freq) / 2
  257. * we scale coef by shifting clock value by 24 for
  258. * better precision since we use integers */
  259. switch (ah->ah_bwmode) {
  260. case AR5K_BWMODE_40MHZ:
  261. clock = 40 * 2;
  262. break;
  263. case AR5K_BWMODE_10MHZ:
  264. clock = 40 / 2;
  265. break;
  266. case AR5K_BWMODE_5MHZ:
  267. clock = 40 / 4;
  268. break;
  269. default:
  270. clock = 40;
  271. break;
  272. }
  273. coef_scaled = ((5 * (clock << 24)) / 2) / channel->center_freq;
  274. /* Get exponent
  275. * ALGO: coef_exp = 14 - highest set bit position */
  276. coef_exp = ilog2(coef_scaled);
  277. /* Doesn't make sense if it's zero*/
  278. if (!coef_scaled || !coef_exp)
  279. return -EINVAL;
  280. /* Note: we've shifted coef_scaled by 24 */
  281. coef_exp = 14 - (coef_exp - 24);
  282. /* Get mantissa (significant digits)
  283. * ALGO: coef_mant = floor(coef_scaled* 2^coef_exp+0.5) */
  284. coef_man = coef_scaled +
  285. (1 << (24 - coef_exp - 1));
  286. /* Calculate delta slope coefficient exponent
  287. * and mantissa (remove scaling) and set them on hw */
  288. ds_coef_man = coef_man >> (24 - coef_exp);
  289. ds_coef_exp = coef_exp - 16;
  290. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  291. AR5K_PHY_TIMING_3_DSC_MAN, ds_coef_man);
  292. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_3,
  293. AR5K_PHY_TIMING_3_DSC_EXP, ds_coef_exp);
  294. return 0;
  295. }
  296. /**
  297. * ath5k_hw_phy_disable() - Disable PHY
  298. * @ah: The &struct ath5k_hw
  299. */
  300. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  301. {
  302. /*Just a try M.F.*/
  303. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  304. return 0;
  305. }
  306. /**
  307. * ath5k_hw_wait_for_synth() - Wait for synth to settle
  308. * @ah: The &struct ath5k_hw
  309. * @channel: The &struct ieee80211_channel
  310. */
  311. static void
  312. ath5k_hw_wait_for_synth(struct ath5k_hw *ah,
  313. struct ieee80211_channel *channel)
  314. {
  315. /*
  316. * On 5211+ read activation -> rx delay
  317. * and use it (100ns steps).
  318. */
  319. if (ah->ah_version != AR5K_AR5210) {
  320. u32 delay;
  321. delay = ath5k_hw_reg_read(ah, AR5K_PHY_RX_DELAY) &
  322. AR5K_PHY_RX_DELAY_M;
  323. delay = (channel->hw_value == AR5K_MODE_11B) ?
  324. ((delay << 2) / 22) : (delay / 10);
  325. if (ah->ah_bwmode == AR5K_BWMODE_10MHZ)
  326. delay = delay << 1;
  327. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ)
  328. delay = delay << 2;
  329. /* XXX: /2 on turbo ? Let's be safe
  330. * for now */
  331. usleep_range(100 + delay, 100 + (2 * delay));
  332. } else {
  333. usleep_range(1000, 1500);
  334. }
  335. }
  336. /**********************\
  337. * RF Gain optimization *
  338. \**********************/
  339. /**
  340. * DOC: RF Gain optimization
  341. *
  342. * This code is used to optimize RF gain on different environments
  343. * (temperature mostly) based on feedback from a power detector.
  344. *
  345. * It's only used on RF5111 and RF5112, later RF chips seem to have
  346. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  347. * no gain optimization ladder-.
  348. *
  349. * For more infos check out this patent doc
  350. * "http://www.freepatentsonline.com/7400691.html"
  351. *
  352. * This paper describes power drops as seen on the receiver due to
  353. * probe packets
  354. * "http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  355. * %20of%20Power%20Control.pdf"
  356. *
  357. * And this is the MadWiFi bug entry related to the above
  358. * "http://madwifi-project.org/ticket/1659"
  359. * with various measurements and diagrams
  360. */
  361. /**
  362. * ath5k_hw_rfgain_opt_init() - Initialize ah_gain during attach
  363. * @ah: The &struct ath5k_hw
  364. */
  365. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  366. {
  367. /* Initialize the gain optimization values */
  368. switch (ah->ah_radio) {
  369. case AR5K_RF5111:
  370. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  371. ah->ah_gain.g_low = 20;
  372. ah->ah_gain.g_high = 35;
  373. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  374. break;
  375. case AR5K_RF5112:
  376. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  377. ah->ah_gain.g_low = 20;
  378. ah->ah_gain.g_high = 85;
  379. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  380. break;
  381. default:
  382. return -EINVAL;
  383. }
  384. return 0;
  385. }
  386. /**
  387. * ath5k_hw_request_rfgain_probe() - Request a PAPD probe packet
  388. * @ah: The &struct ath5k_hw
  389. *
  390. * Schedules a gain probe check on the next transmitted packet.
  391. * That means our next packet is going to be sent with lower
  392. * tx power and a Peak to Average Power Detector (PAPD) will try
  393. * to measure the gain.
  394. *
  395. * TODO: Force a tx packet (bypassing PCU arbitrator etc)
  396. * just after we enable the probe so that we don't mess with
  397. * standard traffic.
  398. */
  399. static void
  400. ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  401. {
  402. /* Skip if gain calibration is inactive or
  403. * we already handle a probe request */
  404. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  405. return;
  406. /* Send the packet with 2dB below max power as
  407. * patent doc suggest */
  408. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_ofdm - 4,
  409. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  410. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  411. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  412. }
  413. /**
  414. * ath5k_hw_rf_gainf_corr() - Calculate Gain_F measurement correction
  415. * @ah: The &struct ath5k_hw
  416. *
  417. * Calculate Gain_F measurement correction
  418. * based on the current step for RF5112 rev. 2
  419. */
  420. static u32
  421. ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  422. {
  423. u32 mix, step;
  424. const struct ath5k_gain_opt *go;
  425. const struct ath5k_gain_opt_step *g_step;
  426. const struct ath5k_rf_reg *rf_regs;
  427. /* Only RF5112 Rev. 2 supports it */
  428. if ((ah->ah_radio != AR5K_RF5112) ||
  429. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  430. return 0;
  431. go = &rfgain_opt_5112;
  432. rf_regs = rf_regs_5112a;
  433. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  434. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  435. if (ah->ah_rf_banks == NULL)
  436. return 0;
  437. ah->ah_gain.g_f_corr = 0;
  438. /* No VGA (Variable Gain Amplifier) override, skip */
  439. if (ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR, false) != 1)
  440. return 0;
  441. /* Mix gain stepping */
  442. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXGAIN_STEP, false);
  443. /* Mix gain override */
  444. mix = g_step->gos_param[0];
  445. switch (mix) {
  446. case 3:
  447. ah->ah_gain.g_f_corr = step * 2;
  448. break;
  449. case 2:
  450. ah->ah_gain.g_f_corr = (step - 5) * 2;
  451. break;
  452. case 1:
  453. ah->ah_gain.g_f_corr = step;
  454. break;
  455. default:
  456. ah->ah_gain.g_f_corr = 0;
  457. break;
  458. }
  459. return ah->ah_gain.g_f_corr;
  460. }
  461. /**
  462. * ath5k_hw_rf_check_gainf_readback() - Validate Gain_F feedback from detector
  463. * @ah: The &struct ath5k_hw
  464. *
  465. * Check if current gain_F measurement is in the range of our
  466. * power detector windows. If we get a measurement outside range
  467. * we know it's not accurate (detectors can't measure anything outside
  468. * their detection window) so we must ignore it.
  469. *
  470. * Returns true if readback was O.K. or false on failure
  471. */
  472. static bool
  473. ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  474. {
  475. const struct ath5k_rf_reg *rf_regs;
  476. u32 step, mix_ovr, level[4];
  477. if (ah->ah_rf_banks == NULL)
  478. return false;
  479. if (ah->ah_radio == AR5K_RF5111) {
  480. rf_regs = rf_regs_5111;
  481. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  482. step = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_RFGAIN_STEP,
  483. false);
  484. level[0] = 0;
  485. level[1] = (step == 63) ? 50 : step + 4;
  486. level[2] = (step != 63) ? 64 : level[0];
  487. level[3] = level[2] + 50;
  488. ah->ah_gain.g_high = level[3] -
  489. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  490. ah->ah_gain.g_low = level[0] +
  491. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  492. } else {
  493. rf_regs = rf_regs_5112;
  494. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  495. mix_ovr = ath5k_hw_rfb_op(ah, rf_regs, 0, AR5K_RF_MIXVGA_OVR,
  496. false);
  497. level[0] = level[2] = 0;
  498. if (mix_ovr == 1) {
  499. level[1] = level[3] = 83;
  500. } else {
  501. level[1] = level[3] = 107;
  502. ah->ah_gain.g_high = 55;
  503. }
  504. }
  505. return (ah->ah_gain.g_current >= level[0] &&
  506. ah->ah_gain.g_current <= level[1]) ||
  507. (ah->ah_gain.g_current >= level[2] &&
  508. ah->ah_gain.g_current <= level[3]);
  509. }
  510. /**
  511. * ath5k_hw_rf_gainf_adjust() - Perform Gain_F adjustment
  512. * @ah: The &struct ath5k_hw
  513. *
  514. * Choose the right target gain based on current gain
  515. * and RF gain optimization ladder
  516. */
  517. static s8
  518. ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  519. {
  520. const struct ath5k_gain_opt *go;
  521. const struct ath5k_gain_opt_step *g_step;
  522. int ret = 0;
  523. switch (ah->ah_radio) {
  524. case AR5K_RF5111:
  525. go = &rfgain_opt_5111;
  526. break;
  527. case AR5K_RF5112:
  528. go = &rfgain_opt_5112;
  529. break;
  530. default:
  531. return 0;
  532. }
  533. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  534. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  535. /* Reached maximum */
  536. if (ah->ah_gain.g_step_idx == 0)
  537. return -1;
  538. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  539. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  540. ah->ah_gain.g_step_idx > 0;
  541. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  542. ah->ah_gain.g_target -= 2 *
  543. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  544. g_step->gos_gain);
  545. ret = 1;
  546. goto done;
  547. }
  548. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  549. /* Reached minimum */
  550. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  551. return -2;
  552. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  553. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  554. ah->ah_gain.g_step_idx < go->go_steps_count - 1;
  555. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  556. ah->ah_gain.g_target -= 2 *
  557. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  558. g_step->gos_gain);
  559. ret = 2;
  560. goto done;
  561. }
  562. done:
  563. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  564. "ret %d, gain step %u, current gain %u, target gain %u\n",
  565. ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
  566. ah->ah_gain.g_target);
  567. return ret;
  568. }
  569. /**
  570. * ath5k_hw_gainf_calibrate() - Do a gain_F calibration
  571. * @ah: The &struct ath5k_hw
  572. *
  573. * Main callback for thermal RF gain calibration engine
  574. * Check for a new gain reading and schedule an adjustment
  575. * if needed.
  576. *
  577. * Returns one of enum ath5k_rfgain codes
  578. */
  579. enum ath5k_rfgain
  580. ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  581. {
  582. u32 data, type;
  583. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  584. if (ah->ah_rf_banks == NULL ||
  585. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  586. return AR5K_RFGAIN_INACTIVE;
  587. /* No check requested, either engine is inactive
  588. * or an adjustment is already requested */
  589. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  590. goto done;
  591. /* Read the PAPD (Peak to Average Power Detector)
  592. * register */
  593. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  594. /* No probe is scheduled, read gain_F measurement */
  595. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  596. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  597. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  598. /* If tx packet is CCK correct the gain_F measurement
  599. * by cck ofdm gain delta */
  600. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  601. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  602. ah->ah_gain.g_current +=
  603. ee->ee_cck_ofdm_gain_delta;
  604. else
  605. ah->ah_gain.g_current +=
  606. AR5K_GAIN_CCK_PROBE_CORR;
  607. }
  608. /* Further correct gain_F measurement for
  609. * RF5112A radios */
  610. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  611. ath5k_hw_rf_gainf_corr(ah);
  612. ah->ah_gain.g_current =
  613. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  614. (ah->ah_gain.g_current - ah->ah_gain.g_f_corr) :
  615. 0;
  616. }
  617. /* Check if measurement is ok and if we need
  618. * to adjust gain, schedule a gain adjustment,
  619. * else switch back to the active state */
  620. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  621. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  622. ath5k_hw_rf_gainf_adjust(ah)) {
  623. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  624. } else {
  625. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  626. }
  627. }
  628. done:
  629. return ah->ah_gain.g_state;
  630. }
  631. /**
  632. * ath5k_hw_rfgain_init() - Write initial RF gain settings to hw
  633. * @ah: The &struct ath5k_hw
  634. * @band: One of enum nl80211_band
  635. *
  636. * Write initial RF gain table to set the RF sensitivity.
  637. *
  638. * NOTE: This one works on all RF chips and has nothing to do
  639. * with Gain_F calibration
  640. */
  641. static int
  642. ath5k_hw_rfgain_init(struct ath5k_hw *ah, enum nl80211_band band)
  643. {
  644. const struct ath5k_ini_rfgain *ath5k_rfg;
  645. unsigned int i, size, index;
  646. switch (ah->ah_radio) {
  647. case AR5K_RF5111:
  648. ath5k_rfg = rfgain_5111;
  649. size = ARRAY_SIZE(rfgain_5111);
  650. break;
  651. case AR5K_RF5112:
  652. ath5k_rfg = rfgain_5112;
  653. size = ARRAY_SIZE(rfgain_5112);
  654. break;
  655. case AR5K_RF2413:
  656. ath5k_rfg = rfgain_2413;
  657. size = ARRAY_SIZE(rfgain_2413);
  658. break;
  659. case AR5K_RF2316:
  660. ath5k_rfg = rfgain_2316;
  661. size = ARRAY_SIZE(rfgain_2316);
  662. break;
  663. case AR5K_RF5413:
  664. ath5k_rfg = rfgain_5413;
  665. size = ARRAY_SIZE(rfgain_5413);
  666. break;
  667. case AR5K_RF2317:
  668. case AR5K_RF2425:
  669. ath5k_rfg = rfgain_2425;
  670. size = ARRAY_SIZE(rfgain_2425);
  671. break;
  672. default:
  673. return -EINVAL;
  674. }
  675. index = (band == NL80211_BAND_2GHZ) ? 1 : 0;
  676. for (i = 0; i < size; i++) {
  677. AR5K_REG_WAIT(i);
  678. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[index],
  679. (u32)ath5k_rfg[i].rfg_register);
  680. }
  681. return 0;
  682. }
  683. /********************\
  684. * RF Registers setup *
  685. \********************/
  686. /**
  687. * ath5k_hw_rfregs_init() - Initialize RF register settings
  688. * @ah: The &struct ath5k_hw
  689. * @channel: The &struct ieee80211_channel
  690. * @mode: One of enum ath5k_driver_mode
  691. *
  692. * Setup RF registers by writing RF buffer on hw. For
  693. * more infos on this, check out rfbuffer.h
  694. */
  695. static int
  696. ath5k_hw_rfregs_init(struct ath5k_hw *ah,
  697. struct ieee80211_channel *channel,
  698. unsigned int mode)
  699. {
  700. const struct ath5k_rf_reg *rf_regs;
  701. const struct ath5k_ini_rfbuffer *ini_rfb;
  702. const struct ath5k_gain_opt *go = NULL;
  703. const struct ath5k_gain_opt_step *g_step;
  704. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  705. u8 ee_mode = 0;
  706. u32 *rfb;
  707. int i, obdb = -1, bank = -1;
  708. switch (ah->ah_radio) {
  709. case AR5K_RF5111:
  710. rf_regs = rf_regs_5111;
  711. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5111);
  712. ini_rfb = rfb_5111;
  713. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5111);
  714. go = &rfgain_opt_5111;
  715. break;
  716. case AR5K_RF5112:
  717. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  718. rf_regs = rf_regs_5112a;
  719. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112a);
  720. ini_rfb = rfb_5112a;
  721. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112a);
  722. } else {
  723. rf_regs = rf_regs_5112;
  724. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5112);
  725. ini_rfb = rfb_5112;
  726. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5112);
  727. }
  728. go = &rfgain_opt_5112;
  729. break;
  730. case AR5K_RF2413:
  731. rf_regs = rf_regs_2413;
  732. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2413);
  733. ini_rfb = rfb_2413;
  734. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2413);
  735. break;
  736. case AR5K_RF2316:
  737. rf_regs = rf_regs_2316;
  738. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2316);
  739. ini_rfb = rfb_2316;
  740. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2316);
  741. break;
  742. case AR5K_RF5413:
  743. rf_regs = rf_regs_5413;
  744. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_5413);
  745. ini_rfb = rfb_5413;
  746. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_5413);
  747. break;
  748. case AR5K_RF2317:
  749. rf_regs = rf_regs_2425;
  750. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  751. ini_rfb = rfb_2317;
  752. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2317);
  753. break;
  754. case AR5K_RF2425:
  755. rf_regs = rf_regs_2425;
  756. ah->ah_rf_regs_count = ARRAY_SIZE(rf_regs_2425);
  757. if (ah->ah_mac_srev < AR5K_SREV_AR2417) {
  758. ini_rfb = rfb_2425;
  759. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2425);
  760. } else {
  761. ini_rfb = rfb_2417;
  762. ah->ah_rf_banks_size = ARRAY_SIZE(rfb_2417);
  763. }
  764. break;
  765. default:
  766. return -EINVAL;
  767. }
  768. /* If it's the first time we set RF buffer, allocate
  769. * ah->ah_rf_banks based on ah->ah_rf_banks_size
  770. * we set above */
  771. if (ah->ah_rf_banks == NULL) {
  772. ah->ah_rf_banks = kmalloc_array(ah->ah_rf_banks_size,
  773. sizeof(u32),
  774. GFP_KERNEL);
  775. if (ah->ah_rf_banks == NULL) {
  776. ATH5K_ERR(ah, "out of memory\n");
  777. return -ENOMEM;
  778. }
  779. }
  780. /* Copy values to modify them */
  781. rfb = ah->ah_rf_banks;
  782. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  783. if (ini_rfb[i].rfb_bank >= AR5K_MAX_RF_BANKS) {
  784. ATH5K_ERR(ah, "invalid bank\n");
  785. return -EINVAL;
  786. }
  787. /* Bank changed, write down the offset */
  788. if (bank != ini_rfb[i].rfb_bank) {
  789. bank = ini_rfb[i].rfb_bank;
  790. ah->ah_offset[bank] = i;
  791. }
  792. rfb[i] = ini_rfb[i].rfb_mode_data[mode];
  793. }
  794. /* Set Output and Driver bias current (OB/DB) */
  795. if (channel->band == NL80211_BAND_2GHZ) {
  796. if (channel->hw_value == AR5K_MODE_11B)
  797. ee_mode = AR5K_EEPROM_MODE_11B;
  798. else
  799. ee_mode = AR5K_EEPROM_MODE_11G;
  800. /* For RF511X/RF211X combination we
  801. * use b_OB and b_DB parameters stored
  802. * in eeprom on ee->ee_ob[ee_mode][0]
  803. *
  804. * For all other chips we use OB/DB for 2GHz
  805. * stored in the b/g modal section just like
  806. * 802.11a on ee->ee_ob[ee_mode][1] */
  807. if ((ah->ah_radio == AR5K_RF5111) ||
  808. (ah->ah_radio == AR5K_RF5112))
  809. obdb = 0;
  810. else
  811. obdb = 1;
  812. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  813. AR5K_RF_OB_2GHZ, true);
  814. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  815. AR5K_RF_DB_2GHZ, true);
  816. /* RF5111 always needs OB/DB for 5GHz, even if we use 2GHz */
  817. } else if ((channel->band == NL80211_BAND_5GHZ) ||
  818. (ah->ah_radio == AR5K_RF5111)) {
  819. /* For 11a, Turbo and XR we need to choose
  820. * OB/DB based on frequency range */
  821. ee_mode = AR5K_EEPROM_MODE_11A;
  822. obdb = channel->center_freq >= 5725 ? 3 :
  823. (channel->center_freq >= 5500 ? 2 :
  824. (channel->center_freq >= 5260 ? 1 :
  825. (channel->center_freq > 4000 ? 0 : -1)));
  826. if (obdb < 0)
  827. return -EINVAL;
  828. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_ob[ee_mode][obdb],
  829. AR5K_RF_OB_5GHZ, true);
  830. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_db[ee_mode][obdb],
  831. AR5K_RF_DB_5GHZ, true);
  832. }
  833. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  834. /* Set turbo mode (N/A on RF5413) */
  835. if ((ah->ah_bwmode == AR5K_BWMODE_40MHZ) &&
  836. (ah->ah_radio != AR5K_RF5413))
  837. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_TURBO, false);
  838. /* Bank Modifications (chip-specific) */
  839. if (ah->ah_radio == AR5K_RF5111) {
  840. /* Set gain_F settings according to current step */
  841. if (channel->hw_value != AR5K_MODE_11B) {
  842. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_FRAME_CTL,
  843. AR5K_PHY_FRAME_CTL_TX_CLIP,
  844. g_step->gos_param[0]);
  845. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  846. AR5K_RF_PWD_90, true);
  847. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  848. AR5K_RF_PWD_84, true);
  849. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  850. AR5K_RF_RFGAIN_SEL, true);
  851. /* We programmed gain_F parameters, switch back
  852. * to active state */
  853. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  854. }
  855. /* Bank 6/7 setup */
  856. ath5k_hw_rfb_op(ah, rf_regs, !ee->ee_xpd[ee_mode],
  857. AR5K_RF_PWD_XPD, true);
  858. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_x_gain[ee_mode],
  859. AR5K_RF_XPD_GAIN, true);
  860. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  861. AR5K_RF_GAIN_I, true);
  862. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  863. AR5K_RF_PLO_SEL, true);
  864. /* Tweak power detectors for half/quarter rate support */
  865. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ ||
  866. ah->ah_bwmode == AR5K_BWMODE_10MHZ) {
  867. u8 wait_i;
  868. ath5k_hw_rfb_op(ah, rf_regs, 0x1f,
  869. AR5K_RF_WAIT_S, true);
  870. wait_i = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ?
  871. 0x1f : 0x10;
  872. ath5k_hw_rfb_op(ah, rf_regs, wait_i,
  873. AR5K_RF_WAIT_I, true);
  874. ath5k_hw_rfb_op(ah, rf_regs, 3,
  875. AR5K_RF_MAX_TIME, true);
  876. }
  877. }
  878. if (ah->ah_radio == AR5K_RF5112) {
  879. /* Set gain_F settings according to current step */
  880. if (channel->hw_value != AR5K_MODE_11B) {
  881. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[0],
  882. AR5K_RF_MIXGAIN_OVR, true);
  883. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[1],
  884. AR5K_RF_PWD_138, true);
  885. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[2],
  886. AR5K_RF_PWD_137, true);
  887. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[3],
  888. AR5K_RF_PWD_136, true);
  889. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[4],
  890. AR5K_RF_PWD_132, true);
  891. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[5],
  892. AR5K_RF_PWD_131, true);
  893. ath5k_hw_rfb_op(ah, rf_regs, g_step->gos_param[6],
  894. AR5K_RF_PWD_130, true);
  895. /* We programmed gain_F parameters, switch back
  896. * to active state */
  897. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  898. }
  899. /* Bank 6/7 setup */
  900. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_xpd[ee_mode],
  901. AR5K_RF_XPD_SEL, true);
  902. if (ah->ah_radio_5ghz_revision < AR5K_SREV_RAD_5112A) {
  903. /* Rev. 1 supports only one xpd */
  904. ath5k_hw_rfb_op(ah, rf_regs,
  905. ee->ee_x_gain[ee_mode],
  906. AR5K_RF_XPD_GAIN, true);
  907. } else {
  908. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  909. if (ee->ee_pd_gains[ee_mode] > 1) {
  910. ath5k_hw_rfb_op(ah, rf_regs,
  911. pdg_curve_to_idx[0],
  912. AR5K_RF_PD_GAIN_LO, true);
  913. ath5k_hw_rfb_op(ah, rf_regs,
  914. pdg_curve_to_idx[1],
  915. AR5K_RF_PD_GAIN_HI, true);
  916. } else {
  917. ath5k_hw_rfb_op(ah, rf_regs,
  918. pdg_curve_to_idx[0],
  919. AR5K_RF_PD_GAIN_LO, true);
  920. ath5k_hw_rfb_op(ah, rf_regs,
  921. pdg_curve_to_idx[0],
  922. AR5K_RF_PD_GAIN_HI, true);
  923. }
  924. /* Lower synth voltage on Rev 2 */
  925. if (ah->ah_radio == AR5K_RF5112 &&
  926. (ah->ah_radio_5ghz_revision & AR5K_SREV_REV) > 0) {
  927. ath5k_hw_rfb_op(ah, rf_regs, 2,
  928. AR5K_RF_HIGH_VC_CP, true);
  929. ath5k_hw_rfb_op(ah, rf_regs, 2,
  930. AR5K_RF_MID_VC_CP, true);
  931. ath5k_hw_rfb_op(ah, rf_regs, 2,
  932. AR5K_RF_LOW_VC_CP, true);
  933. ath5k_hw_rfb_op(ah, rf_regs, 2,
  934. AR5K_RF_PUSH_UP, true);
  935. }
  936. /* Decrease power consumption on 5213+ BaseBand */
  937. if (ah->ah_phy_revision >= AR5K_SREV_PHY_5212A) {
  938. ath5k_hw_rfb_op(ah, rf_regs, 1,
  939. AR5K_RF_PAD2GND, true);
  940. ath5k_hw_rfb_op(ah, rf_regs, 1,
  941. AR5K_RF_XB2_LVL, true);
  942. ath5k_hw_rfb_op(ah, rf_regs, 1,
  943. AR5K_RF_XB5_LVL, true);
  944. ath5k_hw_rfb_op(ah, rf_regs, 1,
  945. AR5K_RF_PWD_167, true);
  946. ath5k_hw_rfb_op(ah, rf_regs, 1,
  947. AR5K_RF_PWD_166, true);
  948. }
  949. }
  950. ath5k_hw_rfb_op(ah, rf_regs, ee->ee_i_gain[ee_mode],
  951. AR5K_RF_GAIN_I, true);
  952. /* Tweak power detector for half/quarter rates */
  953. if (ah->ah_bwmode == AR5K_BWMODE_5MHZ ||
  954. ah->ah_bwmode == AR5K_BWMODE_10MHZ) {
  955. u8 pd_delay;
  956. pd_delay = (ah->ah_bwmode == AR5K_BWMODE_5MHZ) ?
  957. 0xf : 0x8;
  958. ath5k_hw_rfb_op(ah, rf_regs, pd_delay,
  959. AR5K_RF_PD_PERIOD_A, true);
  960. ath5k_hw_rfb_op(ah, rf_regs, 0xf,
  961. AR5K_RF_PD_DELAY_A, true);
  962. }
  963. }
  964. if (ah->ah_radio == AR5K_RF5413 &&
  965. channel->band == NL80211_BAND_2GHZ) {
  966. ath5k_hw_rfb_op(ah, rf_regs, 1, AR5K_RF_DERBY_CHAN_SEL_MODE,
  967. true);
  968. /* Set optimum value for early revisions (on pci-e chips) */
  969. if (ah->ah_mac_srev >= AR5K_SREV_AR5424 &&
  970. ah->ah_mac_srev < AR5K_SREV_AR5413)
  971. ath5k_hw_rfb_op(ah, rf_regs, ath5k_hw_bitswap(6, 3),
  972. AR5K_RF_PWD_ICLOBUF_2G, true);
  973. }
  974. /* Write RF banks on hw */
  975. for (i = 0; i < ah->ah_rf_banks_size; i++) {
  976. AR5K_REG_WAIT(i);
  977. ath5k_hw_reg_write(ah, rfb[i], ini_rfb[i].rfb_ctrl_register);
  978. }
  979. return 0;
  980. }
  981. /**************************\
  982. PHY/RF channel functions
  983. \**************************/
  984. /**
  985. * ath5k_hw_rf5110_chan2athchan() - Convert channel freq on RF5110
  986. * @channel: The &struct ieee80211_channel
  987. *
  988. * Map channel frequency to IEEE channel number and convert it
  989. * to an internal channel value used by the RF5110 chipset.
  990. */
  991. static u32
  992. ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
  993. {
  994. u32 athchan;
  995. athchan = (ath5k_hw_bitswap(
  996. (ieee80211_frequency_to_channel(
  997. channel->center_freq) - 24) / 2, 5)
  998. << 1) | (1 << 6) | 0x1;
  999. return athchan;
  1000. }
  1001. /**
  1002. * ath5k_hw_rf5110_channel() - Set channel frequency on RF5110
  1003. * @ah: The &struct ath5k_hw
  1004. * @channel: The &struct ieee80211_channel
  1005. */
  1006. static int
  1007. ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  1008. struct ieee80211_channel *channel)
  1009. {
  1010. u32 data;
  1011. /*
  1012. * Set the channel and wait
  1013. */
  1014. data = ath5k_hw_rf5110_chan2athchan(channel);
  1015. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  1016. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  1017. usleep_range(1000, 1500);
  1018. return 0;
  1019. }
  1020. /**
  1021. * ath5k_hw_rf5111_chan2athchan() - Handle 2GHz channels on RF5111/2111
  1022. * @ieee: IEEE channel number
  1023. * @athchan: The &struct ath5k_athchan_2ghz
  1024. *
  1025. * In order to enable the RF2111 frequency converter on RF5111/2111 setups
  1026. * we need to add some offsets and extra flags to the data values we pass
  1027. * on to the PHY. So for every 2GHz channel this function gets called
  1028. * to do the conversion.
  1029. */
  1030. static int
  1031. ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  1032. struct ath5k_athchan_2ghz *athchan)
  1033. {
  1034. int channel;
  1035. /* Cast this value to catch negative channel numbers (>= -19) */
  1036. channel = (int)ieee;
  1037. /*
  1038. * Map 2GHz IEEE channel to 5GHz Atheros channel
  1039. */
  1040. if (channel <= 13) {
  1041. athchan->a2_athchan = 115 + channel;
  1042. athchan->a2_flags = 0x46;
  1043. } else if (channel == 14) {
  1044. athchan->a2_athchan = 124;
  1045. athchan->a2_flags = 0x44;
  1046. } else if (channel >= 15 && channel <= 26) {
  1047. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  1048. athchan->a2_flags = 0x46;
  1049. } else
  1050. return -EINVAL;
  1051. return 0;
  1052. }
  1053. /**
  1054. * ath5k_hw_rf5111_channel() - Set channel frequency on RF5111/2111
  1055. * @ah: The &struct ath5k_hw
  1056. * @channel: The &struct ieee80211_channel
  1057. */
  1058. static int
  1059. ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  1060. struct ieee80211_channel *channel)
  1061. {
  1062. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  1063. unsigned int ath5k_channel =
  1064. ieee80211_frequency_to_channel(channel->center_freq);
  1065. u32 data0, data1, clock;
  1066. int ret;
  1067. /*
  1068. * Set the channel on the RF5111 radio
  1069. */
  1070. data0 = data1 = 0;
  1071. if (channel->band == NL80211_BAND_2GHZ) {
  1072. /* Map 2GHz channel to 5GHz Atheros channel ID */
  1073. ret = ath5k_hw_rf5111_chan2athchan(
  1074. ieee80211_frequency_to_channel(channel->center_freq),
  1075. &ath5k_channel_2ghz);
  1076. if (ret)
  1077. return ret;
  1078. ath5k_channel = ath5k_channel_2ghz.a2_athchan;
  1079. data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
  1080. << 5) | (1 << 4);
  1081. }
  1082. if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
  1083. clock = 1;
  1084. data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
  1085. (clock << 1) | (1 << 10) | 1;
  1086. } else {
  1087. clock = 0;
  1088. data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
  1089. << 2) | (clock << 1) | (1 << 10) | 1;
  1090. }
  1091. ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
  1092. AR5K_RF_BUFFER);
  1093. ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
  1094. AR5K_RF_BUFFER_CONTROL_3);
  1095. return 0;
  1096. }
  1097. /**
  1098. * ath5k_hw_rf5112_channel() - Set channel frequency on 5112 and newer
  1099. * @ah: The &struct ath5k_hw
  1100. * @channel: The &struct ieee80211_channel
  1101. *
  1102. * On RF5112/2112 and newer we don't need to do any conversion.
  1103. * We pass the frequency value after a few modifications to the
  1104. * chip directly.
  1105. *
  1106. * NOTE: Make sure channel frequency given is within our range or else
  1107. * we might damage the chip ! Use ath5k_channel_ok before calling this one.
  1108. */
  1109. static int
  1110. ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
  1111. struct ieee80211_channel *channel)
  1112. {
  1113. u32 data, data0, data1, data2;
  1114. u16 c;
  1115. data = data0 = data1 = data2 = 0;
  1116. c = channel->center_freq;
  1117. /* My guess based on code:
  1118. * 2GHz RF has 2 synth modes, one with a Local Oscillator
  1119. * at 2224Hz and one with a LO at 2192Hz. IF is 1520Hz
  1120. * (3040/2). data0 is used to set the PLL divider and data1
  1121. * selects synth mode. */
  1122. if (c < 4800) {
  1123. /* Channel 14 and all frequencies with 2Hz spacing
  1124. * below/above (non-standard channels) */
  1125. if (!((c - 2224) % 5)) {
  1126. /* Same as (c - 2224) / 5 */
  1127. data0 = ((2 * (c - 704)) - 3040) / 10;
  1128. data1 = 1;
  1129. /* Channel 1 and all frequencies with 5Hz spacing
  1130. * below/above (standard channels without channel 14) */
  1131. } else if (!((c - 2192) % 5)) {
  1132. /* Same as (c - 2192) / 5 */
  1133. data0 = ((2 * (c - 672)) - 3040) / 10;
  1134. data1 = 0;
  1135. } else
  1136. return -EINVAL;
  1137. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  1138. /* This is more complex, we have a single synthesizer with
  1139. * 4 reference clock settings (?) based on frequency spacing
  1140. * and set using data2. LO is at 4800Hz and data0 is again used
  1141. * to set some divider.
  1142. *
  1143. * NOTE: There is an old atheros presentation at Stanford
  1144. * that mentions a method called dual direct conversion
  1145. * with 1GHz sliding IF for RF5110. Maybe that's what we
  1146. * have here, or an updated version. */
  1147. } else if ((c % 5) != 2 || c > 5435) {
  1148. if (!(c % 20) && c >= 5120) {
  1149. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  1150. data2 = ath5k_hw_bitswap(3, 2);
  1151. } else if (!(c % 10)) {
  1152. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  1153. data2 = ath5k_hw_bitswap(2, 2);
  1154. } else if (!(c % 5)) {
  1155. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  1156. data2 = ath5k_hw_bitswap(1, 2);
  1157. } else
  1158. return -EINVAL;
  1159. } else {
  1160. data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
  1161. data2 = ath5k_hw_bitswap(0, 2);
  1162. }
  1163. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  1164. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  1165. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  1166. return 0;
  1167. }
  1168. /**
  1169. * ath5k_hw_rf2425_channel() - Set channel frequency on RF2425
  1170. * @ah: The &struct ath5k_hw
  1171. * @channel: The &struct ieee80211_channel
  1172. *
  1173. * AR2425/2417 have a different 2GHz RF so code changes
  1174. * a little bit from RF5112.
  1175. */
  1176. static int
  1177. ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  1178. struct ieee80211_channel *channel)
  1179. {
  1180. u32 data, data0, data2;
  1181. u16 c;
  1182. data = data0 = data2 = 0;
  1183. c = channel->center_freq;
  1184. if (c < 4800) {
  1185. data0 = ath5k_hw_bitswap((c - 2272), 8);
  1186. data2 = 0;
  1187. /* ? 5GHz ? */
  1188. } else if ((c % 5) != 2 || c > 5435) {
  1189. if (!(c % 20) && c < 5120)
  1190. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  1191. else if (!(c % 10))
  1192. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  1193. else if (!(c % 5))
  1194. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  1195. else
  1196. return -EINVAL;
  1197. data2 = ath5k_hw_bitswap(1, 2);
  1198. } else {
  1199. data0 = ath5k_hw_bitswap((10 * (c - 2 - 4800)) / 25 + 1, 8);
  1200. data2 = ath5k_hw_bitswap(0, 2);
  1201. }
  1202. data = (data0 << 4) | data2 << 2 | 0x1001;
  1203. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  1204. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  1205. return 0;
  1206. }
  1207. /**
  1208. * ath5k_hw_channel() - Set a channel on the radio chip
  1209. * @ah: The &struct ath5k_hw
  1210. * @channel: The &struct ieee80211_channel
  1211. *
  1212. * This is the main function called to set a channel on the
  1213. * radio chip based on the radio chip version.
  1214. */
  1215. static int
  1216. ath5k_hw_channel(struct ath5k_hw *ah,
  1217. struct ieee80211_channel *channel)
  1218. {
  1219. int ret;
  1220. /*
  1221. * Check bounds supported by the PHY (we don't care about regulatory
  1222. * restrictions at this point).
  1223. */
  1224. if (!ath5k_channel_ok(ah, channel)) {
  1225. ATH5K_ERR(ah,
  1226. "channel frequency (%u MHz) out of supported "
  1227. "band range\n",
  1228. channel->center_freq);
  1229. return -EINVAL;
  1230. }
  1231. /*
  1232. * Set the channel and wait
  1233. */
  1234. switch (ah->ah_radio) {
  1235. case AR5K_RF5110:
  1236. ret = ath5k_hw_rf5110_channel(ah, channel);
  1237. break;
  1238. case AR5K_RF5111:
  1239. ret = ath5k_hw_rf5111_channel(ah, channel);
  1240. break;
  1241. case AR5K_RF2317:
  1242. case AR5K_RF2425:
  1243. ret = ath5k_hw_rf2425_channel(ah, channel);
  1244. break;
  1245. default:
  1246. ret = ath5k_hw_rf5112_channel(ah, channel);
  1247. break;
  1248. }
  1249. if (ret)
  1250. return ret;
  1251. /* Set JAPAN setting for channel 14 */
  1252. if (channel->center_freq == 2484) {
  1253. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  1254. AR5K_PHY_CCKTXCTL_JAPAN);
  1255. } else {
  1256. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  1257. AR5K_PHY_CCKTXCTL_WORLD);
  1258. }
  1259. ah->ah_current_channel = channel;
  1260. return 0;
  1261. }
  1262. /*****************\
  1263. PHY calibration
  1264. \*****************/
  1265. /**
  1266. * DOC: PHY Calibration routines
  1267. *
  1268. * Noise floor calibration: When we tell the hardware to
  1269. * perform a noise floor calibration by setting the
  1270. * AR5K_PHY_AGCCTL_NF bit on AR5K_PHY_AGCCTL, it will periodically
  1271. * sample-and-hold the minimum noise level seen at the antennas.
  1272. * This value is then stored in a ring buffer of recently measured
  1273. * noise floor values so we have a moving window of the last few
  1274. * samples. The median of the values in the history is then loaded
  1275. * into the hardware for its own use for RSSI and CCA measurements.
  1276. * This type of calibration doesn't interfere with traffic.
  1277. *
  1278. * AGC calibration: When we tell the hardware to perform
  1279. * an AGC (Automatic Gain Control) calibration by setting the
  1280. * AR5K_PHY_AGCCTL_CAL, hw disconnects the antennas and does
  1281. * a calibration on the DC offsets of ADCs. During this period
  1282. * rx/tx gets disabled so we have to deal with it on the driver
  1283. * part.
  1284. *
  1285. * I/Q calibration: When we tell the hardware to perform
  1286. * an I/Q calibration, it tries to correct I/Q imbalance and
  1287. * fix QAM constellation by sampling data from rxed frames.
  1288. * It doesn't interfere with traffic.
  1289. *
  1290. * For more infos on AGC and I/Q calibration check out patent doc
  1291. * #03/094463.
  1292. */
  1293. /**
  1294. * ath5k_hw_read_measured_noise_floor() - Read measured NF from hw
  1295. * @ah: The &struct ath5k_hw
  1296. */
  1297. static s32
  1298. ath5k_hw_read_measured_noise_floor(struct ath5k_hw *ah)
  1299. {
  1300. s32 val;
  1301. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  1302. return sign_extend32(AR5K_REG_MS(val, AR5K_PHY_NF_MINCCA_PWR), 8);
  1303. }
  1304. /**
  1305. * ath5k_hw_init_nfcal_hist() - Initialize NF calibration history buffer
  1306. * @ah: The &struct ath5k_hw
  1307. */
  1308. void
  1309. ath5k_hw_init_nfcal_hist(struct ath5k_hw *ah)
  1310. {
  1311. int i;
  1312. ah->ah_nfcal_hist.index = 0;
  1313. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++)
  1314. ah->ah_nfcal_hist.nfval[i] = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  1315. }
  1316. /**
  1317. * ath5k_hw_update_nfcal_hist() - Update NF calibration history buffer
  1318. * @ah: The &struct ath5k_hw
  1319. * @noise_floor: The NF we got from hw
  1320. */
  1321. static void ath5k_hw_update_nfcal_hist(struct ath5k_hw *ah, s16 noise_floor)
  1322. {
  1323. struct ath5k_nfcal_hist *hist = &ah->ah_nfcal_hist;
  1324. hist->index = (hist->index + 1) & (ATH5K_NF_CAL_HIST_MAX - 1);
  1325. hist->nfval[hist->index] = noise_floor;
  1326. }
  1327. static int cmps16(const void *a, const void *b)
  1328. {
  1329. return *(s16 *)a - *(s16 *)b;
  1330. }
  1331. /**
  1332. * ath5k_hw_get_median_noise_floor() - Get median NF from history buffer
  1333. * @ah: The &struct ath5k_hw
  1334. */
  1335. static s16
  1336. ath5k_hw_get_median_noise_floor(struct ath5k_hw *ah)
  1337. {
  1338. s16 sorted_nfval[ATH5K_NF_CAL_HIST_MAX];
  1339. int i;
  1340. memcpy(sorted_nfval, ah->ah_nfcal_hist.nfval, sizeof(sorted_nfval));
  1341. sort(sorted_nfval, ATH5K_NF_CAL_HIST_MAX, sizeof(s16), cmps16, NULL);
  1342. for (i = 0; i < ATH5K_NF_CAL_HIST_MAX; i++) {
  1343. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1344. "cal %d:%d\n", i, sorted_nfval[i]);
  1345. }
  1346. return sorted_nfval[(ATH5K_NF_CAL_HIST_MAX - 1) / 2];
  1347. }
  1348. /**
  1349. * ath5k_hw_update_noise_floor() - Update NF on hardware
  1350. * @ah: The &struct ath5k_hw
  1351. *
  1352. * This is the main function we call to perform a NF calibration,
  1353. * it reads NF from hardware, calculates the median and updates
  1354. * NF on hw.
  1355. */
  1356. void
  1357. ath5k_hw_update_noise_floor(struct ath5k_hw *ah)
  1358. {
  1359. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1360. u32 val;
  1361. s16 nf, threshold;
  1362. u8 ee_mode;
  1363. /* keep last value if calibration hasn't completed */
  1364. if (ath5k_hw_reg_read(ah, AR5K_PHY_AGCCTL) & AR5K_PHY_AGCCTL_NF) {
  1365. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1366. "NF did not complete in calibration window\n");
  1367. return;
  1368. }
  1369. ah->ah_cal_mask |= AR5K_CALIBRATION_NF;
  1370. ee_mode = ath5k_eeprom_mode_from_channel(ah, ah->ah_current_channel);
  1371. /* completed NF calibration, test threshold */
  1372. nf = ath5k_hw_read_measured_noise_floor(ah);
  1373. threshold = ee->ee_noise_floor_thr[ee_mode];
  1374. if (nf > threshold) {
  1375. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1376. "noise floor failure detected; "
  1377. "read %d, threshold %d\n",
  1378. nf, threshold);
  1379. nf = AR5K_TUNE_CCA_MAX_GOOD_VALUE;
  1380. }
  1381. ath5k_hw_update_nfcal_hist(ah, nf);
  1382. nf = ath5k_hw_get_median_noise_floor(ah);
  1383. /* load noise floor (in .5 dBm) so the hardware will use it */
  1384. val = ath5k_hw_reg_read(ah, AR5K_PHY_NF) & ~AR5K_PHY_NF_M;
  1385. val |= (nf * 2) & AR5K_PHY_NF_M;
  1386. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1387. AR5K_REG_MASKED_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1388. ~(AR5K_PHY_AGCCTL_NF_EN | AR5K_PHY_AGCCTL_NF_NOUPDATE));
  1389. ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_NF,
  1390. 0, false);
  1391. /*
  1392. * Load a high max CCA Power value (-50 dBm in .5 dBm units)
  1393. * so that we're not capped by the median we just loaded.
  1394. * This will be used as the initial value for the next noise
  1395. * floor calibration.
  1396. */
  1397. val = (val & ~AR5K_PHY_NF_M) | ((-50 * 2) & AR5K_PHY_NF_M);
  1398. ath5k_hw_reg_write(ah, val, AR5K_PHY_NF);
  1399. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1400. AR5K_PHY_AGCCTL_NF_EN |
  1401. AR5K_PHY_AGCCTL_NF_NOUPDATE |
  1402. AR5K_PHY_AGCCTL_NF);
  1403. ah->ah_noise_floor = nf;
  1404. ah->ah_cal_mask &= ~AR5K_CALIBRATION_NF;
  1405. ATH5K_DBG(ah, ATH5K_DEBUG_CALIBRATE,
  1406. "noise floor calibrated: %d\n", nf);
  1407. }
  1408. /**
  1409. * ath5k_hw_rf5110_calibrate() - Perform a PHY calibration on RF5110
  1410. * @ah: The &struct ath5k_hw
  1411. * @channel: The &struct ieee80211_channel
  1412. *
  1413. * Do a complete PHY calibration (AGC + NF + I/Q) on RF5110
  1414. */
  1415. static int
  1416. ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  1417. struct ieee80211_channel *channel)
  1418. {
  1419. u32 phy_sig, phy_agc, phy_sat, beacon;
  1420. int ret;
  1421. if (!(ah->ah_cal_mask & AR5K_CALIBRATION_FULL))
  1422. return 0;
  1423. /*
  1424. * Disable beacons and RX/TX queues, wait
  1425. */
  1426. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1427. AR5K_DIAG_SW_DIS_TX_5210 | AR5K_DIAG_SW_DIS_RX_5210);
  1428. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  1429. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  1430. usleep_range(2000, 2500);
  1431. /*
  1432. * Set the channel (with AGC turned off)
  1433. */
  1434. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1435. udelay(10);
  1436. ret = ath5k_hw_channel(ah, channel);
  1437. /*
  1438. * Activate PHY and wait
  1439. */
  1440. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  1441. usleep_range(1000, 1500);
  1442. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1443. if (ret)
  1444. return ret;
  1445. /*
  1446. * Calibrate the radio chip
  1447. */
  1448. /* Remember normal state */
  1449. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  1450. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  1451. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  1452. /* Update radio registers */
  1453. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1454. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1455. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1456. AR5K_PHY_AGCCOARSE_LO)) |
  1457. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1458. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1459. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1460. AR5K_PHY_ADCSAT_THR)) |
  1461. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1462. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1463. udelay(20);
  1464. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1465. udelay(10);
  1466. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1467. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1468. usleep_range(1000, 1500);
  1469. /*
  1470. * Enable calibration and wait until completion
  1471. */
  1472. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1473. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1474. AR5K_PHY_AGCCTL_CAL, 0, false);
  1475. /* Reset to normal state */
  1476. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1477. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1478. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1479. if (ret) {
  1480. ATH5K_ERR(ah, "calibration timeout (%uMHz)\n",
  1481. channel->center_freq);
  1482. return ret;
  1483. }
  1484. /*
  1485. * Re-enable RX/TX and beacons
  1486. */
  1487. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1488. AR5K_DIAG_SW_DIS_TX_5210 | AR5K_DIAG_SW_DIS_RX_5210);
  1489. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1490. return 0;
  1491. }
  1492. /**
  1493. * ath5k_hw_rf511x_iq_calibrate() - Perform I/Q calibration on RF5111 and newer
  1494. * @ah: The &struct ath5k_hw
  1495. */
  1496. static int
  1497. ath5k_hw_rf511x_iq_calibrate(struct ath5k_hw *ah)
  1498. {
  1499. u32 i_pwr, q_pwr;
  1500. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1501. int i;
  1502. /* Skip if I/Q calibration is not needed or if it's still running */
  1503. if (!ah->ah_iq_cal_needed)
  1504. return -EINVAL;
  1505. else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN) {
  1506. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1507. "I/Q calibration still running");
  1508. return -EBUSY;
  1509. }
  1510. /* Calibration has finished, get the results and re-run */
  1511. /* Work around for empty results which can apparently happen on 5212:
  1512. * Read registers up to 10 times until we get both i_pr and q_pwr */
  1513. for (i = 0; i <= 10; i++) {
  1514. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1515. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1516. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1517. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1518. "iq_corr:%x i_pwr:%x q_pwr:%x", iq_corr, i_pwr, q_pwr);
  1519. if (i_pwr && q_pwr)
  1520. break;
  1521. }
  1522. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1523. if (ah->ah_version == AR5K_AR5211)
  1524. q_coffd = q_pwr >> 6;
  1525. else
  1526. q_coffd = q_pwr >> 7;
  1527. /* In case i_coffd became zero, cancel calibration
  1528. * not only it's too small, it'll also result a divide
  1529. * by zero later on. */
  1530. if (i_coffd == 0 || q_coffd < 2)
  1531. return -ECANCELED;
  1532. /* Protect against loss of sign bits */
  1533. i_coff = (-iq_corr) / i_coffd;
  1534. i_coff = clamp(i_coff, -32, 31); /* signed 6 bit */
  1535. if (ah->ah_version == AR5K_AR5211)
  1536. q_coff = (i_pwr / q_coffd) - 64;
  1537. else
  1538. q_coff = (i_pwr / q_coffd) - 128;
  1539. q_coff = clamp(q_coff, -16, 15); /* signed 5 bit */
  1540. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1541. "new I:%d Q:%d (i_coffd:%x q_coffd:%x)",
  1542. i_coff, q_coff, i_coffd, q_coffd);
  1543. /* Commit new I/Q values (set enable bit last to match HAL sources) */
  1544. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_I_COFF, i_coff);
  1545. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_Q_Q_COFF, q_coff);
  1546. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE);
  1547. /* Re-enable calibration -if we don't we'll commit
  1548. * the same values again and again */
  1549. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1550. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1551. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1552. return 0;
  1553. }
  1554. /**
  1555. * ath5k_hw_phy_calibrate() - Perform a PHY calibration
  1556. * @ah: The &struct ath5k_hw
  1557. * @channel: The &struct ieee80211_channel
  1558. *
  1559. * The main function we call from above to perform
  1560. * a short or full PHY calibration based on RF chip
  1561. * and current channel
  1562. */
  1563. int
  1564. ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1565. struct ieee80211_channel *channel)
  1566. {
  1567. int ret;
  1568. if (ah->ah_radio == AR5K_RF5110)
  1569. return ath5k_hw_rf5110_calibrate(ah, channel);
  1570. ret = ath5k_hw_rf511x_iq_calibrate(ah);
  1571. if (ret) {
  1572. ATH5K_DBG_UNLIMIT(ah, ATH5K_DEBUG_CALIBRATE,
  1573. "No I/Q correction performed (%uMHz)\n",
  1574. channel->center_freq);
  1575. /* Happens all the time if there is not much
  1576. * traffic, consider it normal behaviour. */
  1577. ret = 0;
  1578. }
  1579. /* On full calibration request a PAPD probe for
  1580. * gainf calibration if needed */
  1581. if ((ah->ah_cal_mask & AR5K_CALIBRATION_FULL) &&
  1582. (ah->ah_radio == AR5K_RF5111 ||
  1583. ah->ah_radio == AR5K_RF5112) &&
  1584. channel->hw_value != AR5K_MODE_11B)
  1585. ath5k_hw_request_rfgain_probe(ah);
  1586. /* Update noise floor */
  1587. if (!(ah->ah_cal_mask & AR5K_CALIBRATION_NF))
  1588. ath5k_hw_update_noise_floor(ah);
  1589. return ret;
  1590. }
  1591. /***************************\
  1592. * Spur mitigation functions *
  1593. \***************************/
  1594. /**
  1595. * ath5k_hw_set_spur_mitigation_filter() - Configure SPUR filter
  1596. * @ah: The &struct ath5k_hw
  1597. * @channel: The &struct ieee80211_channel
  1598. *
  1599. * This function gets called during PHY initialization to
  1600. * configure the spur filter for the given channel. Spur is noise
  1601. * generated due to "reflection" effects, for more information on this
  1602. * method check out patent US7643810
  1603. */
  1604. static void
  1605. ath5k_hw_set_spur_mitigation_filter(struct ath5k_hw *ah,
  1606. struct ieee80211_channel *channel)
  1607. {
  1608. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1609. u32 mag_mask[4] = {0, 0, 0, 0};
  1610. u32 pilot_mask[2] = {0, 0};
  1611. /* Note: fbin values are scaled up by 2 */
  1612. u16 spur_chan_fbin, chan_fbin, symbol_width, spur_detection_window;
  1613. s32 spur_delta_phase, spur_freq_sigma_delta;
  1614. s32 spur_offset, num_symbols_x16;
  1615. u8 num_symbol_offsets, i, freq_band;
  1616. /* Convert current frequency to fbin value (the same way channels
  1617. * are stored on EEPROM, check out ath5k_eeprom_bin2freq) and scale
  1618. * up by 2 so we can compare it later */
  1619. if (channel->band == NL80211_BAND_2GHZ) {
  1620. chan_fbin = (channel->center_freq - 2300) * 10;
  1621. freq_band = AR5K_EEPROM_BAND_2GHZ;
  1622. } else {
  1623. chan_fbin = (channel->center_freq - 4900) * 10;
  1624. freq_band = AR5K_EEPROM_BAND_5GHZ;
  1625. }
  1626. /* Check if any spur_chan_fbin from EEPROM is
  1627. * within our current channel's spur detection range */
  1628. spur_chan_fbin = AR5K_EEPROM_NO_SPUR;
  1629. spur_detection_window = AR5K_SPUR_CHAN_WIDTH;
  1630. /* XXX: Half/Quarter channels ?*/
  1631. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  1632. spur_detection_window *= 2;
  1633. for (i = 0; i < AR5K_EEPROM_N_SPUR_CHANS; i++) {
  1634. spur_chan_fbin = ee->ee_spur_chans[i][freq_band];
  1635. /* Note: mask cleans AR5K_EEPROM_NO_SPUR flag
  1636. * so it's zero if we got nothing from EEPROM */
  1637. if (spur_chan_fbin == AR5K_EEPROM_NO_SPUR) {
  1638. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1639. break;
  1640. }
  1641. if ((chan_fbin - spur_detection_window <=
  1642. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK)) &&
  1643. (chan_fbin + spur_detection_window >=
  1644. (spur_chan_fbin & AR5K_EEPROM_SPUR_CHAN_MASK))) {
  1645. spur_chan_fbin &= AR5K_EEPROM_SPUR_CHAN_MASK;
  1646. break;
  1647. }
  1648. }
  1649. /* We need to enable spur filter for this channel */
  1650. if (spur_chan_fbin) {
  1651. spur_offset = spur_chan_fbin - chan_fbin;
  1652. /*
  1653. * Calculate deltas:
  1654. * spur_freq_sigma_delta -> spur_offset / sample_freq << 21
  1655. * spur_delta_phase -> spur_offset / chip_freq << 11
  1656. * Note: Both values have 100Hz resolution
  1657. */
  1658. switch (ah->ah_bwmode) {
  1659. case AR5K_BWMODE_40MHZ:
  1660. /* Both sample_freq and chip_freq are 80MHz */
  1661. spur_delta_phase = (spur_offset << 16) / 25;
  1662. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1663. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz * 2;
  1664. break;
  1665. case AR5K_BWMODE_10MHZ:
  1666. /* Both sample_freq and chip_freq are 20MHz (?) */
  1667. spur_delta_phase = (spur_offset << 18) / 25;
  1668. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1669. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 2;
  1670. break;
  1671. case AR5K_BWMODE_5MHZ:
  1672. /* Both sample_freq and chip_freq are 10MHz (?) */
  1673. spur_delta_phase = (spur_offset << 19) / 25;
  1674. spur_freq_sigma_delta = (spur_delta_phase >> 10);
  1675. symbol_width = AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz / 4;
  1676. break;
  1677. default:
  1678. if (channel->band == NL80211_BAND_5GHZ) {
  1679. /* Both sample_freq and chip_freq are 40MHz */
  1680. spur_delta_phase = (spur_offset << 17) / 25;
  1681. spur_freq_sigma_delta =
  1682. (spur_delta_phase >> 10);
  1683. symbol_width =
  1684. AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1685. } else {
  1686. /* sample_freq -> 40MHz chip_freq -> 44MHz
  1687. * (for b compatibility) */
  1688. spur_delta_phase = (spur_offset << 17) / 25;
  1689. spur_freq_sigma_delta =
  1690. (spur_offset << 8) / 55;
  1691. symbol_width =
  1692. AR5K_SPUR_SYMBOL_WIDTH_BASE_100Hz;
  1693. }
  1694. break;
  1695. }
  1696. /* Calculate pilot and magnitude masks */
  1697. /* Scale up spur_offset by 1000 to switch to 100HZ resolution
  1698. * and divide by symbol_width to find how many symbols we have
  1699. * Note: number of symbols is scaled up by 16 */
  1700. num_symbols_x16 = ((spur_offset * 1000) << 4) / symbol_width;
  1701. /* Spur is on a symbol if num_symbols_x16 % 16 is zero */
  1702. if (!(num_symbols_x16 & 0xF))
  1703. /* _X_ */
  1704. num_symbol_offsets = 3;
  1705. else
  1706. /* _xx_ */
  1707. num_symbol_offsets = 4;
  1708. for (i = 0; i < num_symbol_offsets; i++) {
  1709. /* Calculate pilot mask */
  1710. s32 curr_sym_off =
  1711. (num_symbols_x16 / 16) + i + 25;
  1712. /* Pilot magnitude mask seems to be a way to
  1713. * declare the boundaries for our detection
  1714. * window or something, it's 2 for the middle
  1715. * value(s) where the symbol is expected to be
  1716. * and 1 on the boundary values */
  1717. u8 plt_mag_map =
  1718. (i == 0 || i == (num_symbol_offsets - 1))
  1719. ? 1 : 2;
  1720. if (curr_sym_off >= 0 && curr_sym_off <= 32) {
  1721. if (curr_sym_off <= 25)
  1722. pilot_mask[0] |= 1 << curr_sym_off;
  1723. else if (curr_sym_off >= 27)
  1724. pilot_mask[0] |= 1 << (curr_sym_off - 1);
  1725. } else if (curr_sym_off >= 33 && curr_sym_off <= 52)
  1726. pilot_mask[1] |= 1 << (curr_sym_off - 33);
  1727. /* Calculate magnitude mask (for viterbi decoder) */
  1728. if (curr_sym_off >= -1 && curr_sym_off <= 14)
  1729. mag_mask[0] |=
  1730. plt_mag_map << (curr_sym_off + 1) * 2;
  1731. else if (curr_sym_off >= 15 && curr_sym_off <= 30)
  1732. mag_mask[1] |=
  1733. plt_mag_map << (curr_sym_off - 15) * 2;
  1734. else if (curr_sym_off >= 31 && curr_sym_off <= 46)
  1735. mag_mask[2] |=
  1736. plt_mag_map << (curr_sym_off - 31) * 2;
  1737. else if (curr_sym_off >= 47 && curr_sym_off <= 53)
  1738. mag_mask[3] |=
  1739. plt_mag_map << (curr_sym_off - 47) * 2;
  1740. }
  1741. /* Write settings on hw to enable spur filter */
  1742. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1743. AR5K_PHY_BIN_MASK_CTL_RATE, 0xff);
  1744. /* XXX: Self correlator also ? */
  1745. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  1746. AR5K_PHY_IQ_PILOT_MASK_EN |
  1747. AR5K_PHY_IQ_CHAN_MASK_EN |
  1748. AR5K_PHY_IQ_SPUR_FILT_EN);
  1749. /* Set delta phase and freq sigma delta */
  1750. ath5k_hw_reg_write(ah,
  1751. AR5K_REG_SM(spur_delta_phase,
  1752. AR5K_PHY_TIMING_11_SPUR_DELTA_PHASE) |
  1753. AR5K_REG_SM(spur_freq_sigma_delta,
  1754. AR5K_PHY_TIMING_11_SPUR_FREQ_SD) |
  1755. AR5K_PHY_TIMING_11_USE_SPUR_IN_AGC,
  1756. AR5K_PHY_TIMING_11);
  1757. /* Write pilot masks */
  1758. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_7);
  1759. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1760. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1761. pilot_mask[1]);
  1762. ath5k_hw_reg_write(ah, pilot_mask[0], AR5K_PHY_TIMING_9);
  1763. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1764. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1765. pilot_mask[1]);
  1766. /* Write magnitude masks */
  1767. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK_1);
  1768. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK_2);
  1769. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK_3);
  1770. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1771. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1772. mag_mask[3]);
  1773. ath5k_hw_reg_write(ah, mag_mask[0], AR5K_PHY_BIN_MASK2_1);
  1774. ath5k_hw_reg_write(ah, mag_mask[1], AR5K_PHY_BIN_MASK2_2);
  1775. ath5k_hw_reg_write(ah, mag_mask[2], AR5K_PHY_BIN_MASK2_3);
  1776. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1777. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1778. mag_mask[3]);
  1779. } else if (ath5k_hw_reg_read(ah, AR5K_PHY_IQ) &
  1780. AR5K_PHY_IQ_SPUR_FILT_EN) {
  1781. /* Clean up spur mitigation settings and disable filter */
  1782. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1783. AR5K_PHY_BIN_MASK_CTL_RATE, 0);
  1784. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_IQ,
  1785. AR5K_PHY_IQ_PILOT_MASK_EN |
  1786. AR5K_PHY_IQ_CHAN_MASK_EN |
  1787. AR5K_PHY_IQ_SPUR_FILT_EN);
  1788. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_11);
  1789. /* Clear pilot masks */
  1790. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_7);
  1791. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_8,
  1792. AR5K_PHY_TIMING_8_PILOT_MASK_2,
  1793. 0);
  1794. ath5k_hw_reg_write(ah, 0, AR5K_PHY_TIMING_9);
  1795. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_TIMING_10,
  1796. AR5K_PHY_TIMING_10_PILOT_MASK_2,
  1797. 0);
  1798. /* Clear magnitude masks */
  1799. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_1);
  1800. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_2);
  1801. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK_3);
  1802. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK_CTL,
  1803. AR5K_PHY_BIN_MASK_CTL_MASK_4,
  1804. 0);
  1805. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_1);
  1806. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_2);
  1807. ath5k_hw_reg_write(ah, 0, AR5K_PHY_BIN_MASK2_3);
  1808. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_BIN_MASK2_4,
  1809. AR5K_PHY_BIN_MASK2_4_MASK_4,
  1810. 0);
  1811. }
  1812. }
  1813. /*****************\
  1814. * Antenna control *
  1815. \*****************/
  1816. /**
  1817. * DOC: Antenna control
  1818. *
  1819. * Hw supports up to 14 antennas ! I haven't found any card that implements
  1820. * that. The maximum number of antennas I've seen is up to 4 (2 for 2GHz and 2
  1821. * for 5GHz). Antenna 1 (MAIN) should be omnidirectional, 2 (AUX)
  1822. * omnidirectional or sectorial and antennas 3-14 sectorial (or directional).
  1823. *
  1824. * We can have a single antenna for RX and multiple antennas for TX.
  1825. * RX antenna is our "default" antenna (usually antenna 1) set on
  1826. * DEFAULT_ANTENNA register and TX antenna is set on each TX control descriptor
  1827. * (0 for automatic selection, 1 - 14 antenna number).
  1828. *
  1829. * We can let hw do all the work doing fast antenna diversity for both
  1830. * tx and rx or we can do things manually. Here are the options we have
  1831. * (all are bits of STA_ID1 register):
  1832. *
  1833. * AR5K_STA_ID1_DEFAULT_ANTENNA -> When 0 is set as the TX antenna on TX
  1834. * control descriptor, use the default antenna to transmit or else use the last
  1835. * antenna on which we received an ACK.
  1836. *
  1837. * AR5K_STA_ID1_DESC_ANTENNA -> Update default antenna after each TX frame to
  1838. * the antenna on which we got the ACK for that frame.
  1839. *
  1840. * AR5K_STA_ID1_RTS_DEF_ANTENNA -> Use default antenna for RTS or else use the
  1841. * one on the TX descriptor.
  1842. *
  1843. * AR5K_STA_ID1_SELFGEN_DEF_ANT -> Use default antenna for self generated frames
  1844. * (ACKs etc), or else use current antenna (the one we just used for TX).
  1845. *
  1846. * Using the above we support the following scenarios:
  1847. *
  1848. * AR5K_ANTMODE_DEFAULT -> Hw handles antenna diversity etc automatically
  1849. *
  1850. * AR5K_ANTMODE_FIXED_A -> Only antenna A (MAIN) is present
  1851. *
  1852. * AR5K_ANTMODE_FIXED_B -> Only antenna B (AUX) is present
  1853. *
  1854. * AR5K_ANTMODE_SINGLE_AP -> Sta locked on a single ap
  1855. *
  1856. * AR5K_ANTMODE_SECTOR_AP -> AP with tx antenna set on tx desc
  1857. *
  1858. * AR5K_ANTMODE_SECTOR_STA -> STA with tx antenna set on tx desc
  1859. *
  1860. * AR5K_ANTMODE_DEBUG Debug mode -A -> Rx, B-> Tx-
  1861. *
  1862. * Also note that when setting antenna to F on tx descriptor card inverts
  1863. * current tx antenna.
  1864. */
  1865. /**
  1866. * ath5k_hw_set_def_antenna() - Set default rx antenna on AR5211/5212 and newer
  1867. * @ah: The &struct ath5k_hw
  1868. * @ant: Antenna number
  1869. */
  1870. static void
  1871. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, u8 ant)
  1872. {
  1873. if (ah->ah_version != AR5K_AR5210)
  1874. ath5k_hw_reg_write(ah, ant & 0x7, AR5K_DEFAULT_ANTENNA);
  1875. }
  1876. /**
  1877. * ath5k_hw_set_fast_div() - Enable/disable fast rx antenna diversity
  1878. * @ah: The &struct ath5k_hw
  1879. * @ee_mode: One of enum ath5k_driver_mode
  1880. * @enable: True to enable, false to disable
  1881. */
  1882. static void
  1883. ath5k_hw_set_fast_div(struct ath5k_hw *ah, u8 ee_mode, bool enable)
  1884. {
  1885. switch (ee_mode) {
  1886. case AR5K_EEPROM_MODE_11G:
  1887. /* XXX: This is set to
  1888. * disabled on initvals !!! */
  1889. case AR5K_EEPROM_MODE_11A:
  1890. if (enable)
  1891. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1892. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1893. else
  1894. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1895. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1896. break;
  1897. case AR5K_EEPROM_MODE_11B:
  1898. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  1899. AR5K_PHY_AGCCTL_OFDM_DIV_DIS);
  1900. break;
  1901. default:
  1902. return;
  1903. }
  1904. if (enable) {
  1905. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1906. AR5K_PHY_RESTART_DIV_GC, 4);
  1907. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1908. AR5K_PHY_FAST_ANT_DIV_EN);
  1909. } else {
  1910. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_RESTART,
  1911. AR5K_PHY_RESTART_DIV_GC, 0);
  1912. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_FAST_ANT_DIV,
  1913. AR5K_PHY_FAST_ANT_DIV_EN);
  1914. }
  1915. }
  1916. /**
  1917. * ath5k_hw_set_antenna_switch() - Set up antenna switch table
  1918. * @ah: The &struct ath5k_hw
  1919. * @ee_mode: One of enum ath5k_driver_mode
  1920. *
  1921. * Switch table comes from EEPROM and includes information on controlling
  1922. * the 2 antenna RX attenuators
  1923. */
  1924. void
  1925. ath5k_hw_set_antenna_switch(struct ath5k_hw *ah, u8 ee_mode)
  1926. {
  1927. u8 ant0, ant1;
  1928. /*
  1929. * In case a fixed antenna was set as default
  1930. * use the same switch table twice.
  1931. */
  1932. if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_A)
  1933. ant0 = ant1 = AR5K_ANT_SWTABLE_A;
  1934. else if (ah->ah_ant_mode == AR5K_ANTMODE_FIXED_B)
  1935. ant0 = ant1 = AR5K_ANT_SWTABLE_B;
  1936. else {
  1937. ant0 = AR5K_ANT_SWTABLE_A;
  1938. ant1 = AR5K_ANT_SWTABLE_B;
  1939. }
  1940. /* Set antenna idle switch table */
  1941. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_ANT_CTL,
  1942. AR5K_PHY_ANT_CTL_SWTABLE_IDLE,
  1943. (ah->ah_ant_ctl[ee_mode][AR5K_ANT_CTL] |
  1944. AR5K_PHY_ANT_CTL_TXRX_EN));
  1945. /* Set antenna switch tables */
  1946. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant0],
  1947. AR5K_PHY_ANT_SWITCH_TABLE_0);
  1948. ath5k_hw_reg_write(ah, ah->ah_ant_ctl[ee_mode][ant1],
  1949. AR5K_PHY_ANT_SWITCH_TABLE_1);
  1950. }
  1951. /**
  1952. * ath5k_hw_set_antenna_mode() - Set antenna operating mode
  1953. * @ah: The &struct ath5k_hw
  1954. * @ant_mode: One of enum ath5k_ant_mode
  1955. */
  1956. void
  1957. ath5k_hw_set_antenna_mode(struct ath5k_hw *ah, u8 ant_mode)
  1958. {
  1959. struct ieee80211_channel *channel = ah->ah_current_channel;
  1960. bool use_def_for_tx, update_def_on_tx, use_def_for_rts, fast_div;
  1961. bool use_def_for_sg;
  1962. int ee_mode;
  1963. u8 def_ant, tx_ant;
  1964. u32 sta_id1 = 0;
  1965. /* if channel is not initialized yet we can't set the antennas
  1966. * so just store the mode. it will be set on the next reset */
  1967. if (channel == NULL) {
  1968. ah->ah_ant_mode = ant_mode;
  1969. return;
  1970. }
  1971. def_ant = ah->ah_def_ant;
  1972. ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);
  1973. switch (ant_mode) {
  1974. case AR5K_ANTMODE_DEFAULT:
  1975. tx_ant = 0;
  1976. use_def_for_tx = false;
  1977. update_def_on_tx = false;
  1978. use_def_for_rts = false;
  1979. use_def_for_sg = false;
  1980. fast_div = true;
  1981. break;
  1982. case AR5K_ANTMODE_FIXED_A:
  1983. def_ant = 1;
  1984. tx_ant = 1;
  1985. use_def_for_tx = true;
  1986. update_def_on_tx = false;
  1987. use_def_for_rts = true;
  1988. use_def_for_sg = true;
  1989. fast_div = false;
  1990. break;
  1991. case AR5K_ANTMODE_FIXED_B:
  1992. def_ant = 2;
  1993. tx_ant = 2;
  1994. use_def_for_tx = true;
  1995. update_def_on_tx = false;
  1996. use_def_for_rts = true;
  1997. use_def_for_sg = true;
  1998. fast_div = false;
  1999. break;
  2000. case AR5K_ANTMODE_SINGLE_AP:
  2001. def_ant = 1; /* updated on tx */
  2002. tx_ant = 0;
  2003. use_def_for_tx = true;
  2004. update_def_on_tx = true;
  2005. use_def_for_rts = true;
  2006. use_def_for_sg = true;
  2007. fast_div = true;
  2008. break;
  2009. case AR5K_ANTMODE_SECTOR_AP:
  2010. tx_ant = 1; /* variable */
  2011. use_def_for_tx = false;
  2012. update_def_on_tx = false;
  2013. use_def_for_rts = true;
  2014. use_def_for_sg = false;
  2015. fast_div = false;
  2016. break;
  2017. case AR5K_ANTMODE_SECTOR_STA:
  2018. tx_ant = 1; /* variable */
  2019. use_def_for_tx = true;
  2020. update_def_on_tx = false;
  2021. use_def_for_rts = true;
  2022. use_def_for_sg = false;
  2023. fast_div = true;
  2024. break;
  2025. case AR5K_ANTMODE_DEBUG:
  2026. def_ant = 1;
  2027. tx_ant = 2;
  2028. use_def_for_tx = false;
  2029. update_def_on_tx = false;
  2030. use_def_for_rts = false;
  2031. use_def_for_sg = false;
  2032. fast_div = false;
  2033. break;
  2034. default:
  2035. return;
  2036. }
  2037. ah->ah_tx_ant = tx_ant;
  2038. ah->ah_ant_mode = ant_mode;
  2039. ah->ah_def_ant = def_ant;
  2040. sta_id1 |= use_def_for_tx ? AR5K_STA_ID1_DEFAULT_ANTENNA : 0;
  2041. sta_id1 |= update_def_on_tx ? AR5K_STA_ID1_DESC_ANTENNA : 0;
  2042. sta_id1 |= use_def_for_rts ? AR5K_STA_ID1_RTS_DEF_ANTENNA : 0;
  2043. sta_id1 |= use_def_for_sg ? AR5K_STA_ID1_SELFGEN_DEF_ANT : 0;
  2044. AR5K_REG_DISABLE_BITS(ah, AR5K_STA_ID1, AR5K_STA_ID1_ANTENNA_SETTINGS);
  2045. if (sta_id1)
  2046. AR5K_REG_ENABLE_BITS(ah, AR5K_STA_ID1, sta_id1);
  2047. ath5k_hw_set_antenna_switch(ah, ee_mode);
  2048. /* Note: set diversity before default antenna
  2049. * because it won't work correctly */
  2050. ath5k_hw_set_fast_div(ah, ee_mode, fast_div);
  2051. ath5k_hw_set_def_antenna(ah, def_ant);
  2052. }
  2053. /****************\
  2054. * TX power setup *
  2055. \****************/
  2056. /*
  2057. * Helper functions
  2058. */
  2059. /**
  2060. * ath5k_get_interpolated_value() - Get interpolated Y val between two points
  2061. * @target: X value of the middle point
  2062. * @x_left: X value of the left point
  2063. * @x_right: X value of the right point
  2064. * @y_left: Y value of the left point
  2065. * @y_right: Y value of the right point
  2066. */
  2067. static s16
  2068. ath5k_get_interpolated_value(s16 target, s16 x_left, s16 x_right,
  2069. s16 y_left, s16 y_right)
  2070. {
  2071. s16 ratio, result;
  2072. /* Avoid divide by zero and skip interpolation
  2073. * if we have the same point */
  2074. if ((x_left == x_right) || (y_left == y_right))
  2075. return y_left;
  2076. /*
  2077. * Since we use ints and not fps, we need to scale up in
  2078. * order to get a sane ratio value (or else we 'll eg. get
  2079. * always 1 instead of 1.25, 1.75 etc). We scale up by 100
  2080. * to have some accuracy both for 0.5 and 0.25 steps.
  2081. */
  2082. ratio = ((100 * y_right - 100 * y_left) / (x_right - x_left));
  2083. /* Now scale down to be in range */
  2084. result = y_left + (ratio * (target - x_left) / 100);
  2085. return result;
  2086. }
  2087. /**
  2088. * ath5k_get_linear_pcdac_min() - Find vertical boundary (min pwr) for the
  2089. * linear PCDAC curve
  2090. * @stepL: Left array with y values (pcdac steps)
  2091. * @stepR: Right array with y values (pcdac steps)
  2092. * @pwrL: Left array with x values (power steps)
  2093. * @pwrR: Right array with x values (power steps)
  2094. *
  2095. * Since we have the top of the curve and we draw the line below
  2096. * until we reach 1 (1 pcdac step) we need to know which point
  2097. * (x value) that is so that we don't go below x axis and have negative
  2098. * pcdac values when creating the curve, or fill the table with zeros.
  2099. */
  2100. static s16
  2101. ath5k_get_linear_pcdac_min(const u8 *stepL, const u8 *stepR,
  2102. const s16 *pwrL, const s16 *pwrR)
  2103. {
  2104. s8 tmp;
  2105. s16 min_pwrL, min_pwrR;
  2106. s16 pwr_i;
  2107. /* Some vendors write the same pcdac value twice !!! */
  2108. if (stepL[0] == stepL[1] || stepR[0] == stepR[1])
  2109. return max(pwrL[0], pwrR[0]);
  2110. if (pwrL[0] == pwrL[1])
  2111. min_pwrL = pwrL[0];
  2112. else {
  2113. pwr_i = pwrL[0];
  2114. do {
  2115. pwr_i--;
  2116. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  2117. pwrL[0], pwrL[1],
  2118. stepL[0], stepL[1]);
  2119. } while (tmp > 1);
  2120. min_pwrL = pwr_i;
  2121. }
  2122. if (pwrR[0] == pwrR[1])
  2123. min_pwrR = pwrR[0];
  2124. else {
  2125. pwr_i = pwrR[0];
  2126. do {
  2127. pwr_i--;
  2128. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  2129. pwrR[0], pwrR[1],
  2130. stepR[0], stepR[1]);
  2131. } while (tmp > 1);
  2132. min_pwrR = pwr_i;
  2133. }
  2134. /* Keep the right boundary so that it works for both curves */
  2135. return max(min_pwrL, min_pwrR);
  2136. }
  2137. /**
  2138. * ath5k_create_power_curve() - Create a Power to PDADC or PCDAC curve
  2139. * @pmin: Minimum power value (xmin)
  2140. * @pmax: Maximum power value (xmax)
  2141. * @pwr: Array of power steps (x values)
  2142. * @vpd: Array of matching PCDAC/PDADC steps (y values)
  2143. * @num_points: Number of provided points
  2144. * @vpd_table: Array to fill with the full PCDAC/PDADC values (y values)
  2145. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2146. *
  2147. * Interpolate (pwr,vpd) points to create a Power to PDADC or a
  2148. * Power to PCDAC curve.
  2149. *
  2150. * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
  2151. * steps (offsets) on y axis. Power can go up to 31.5dB and max
  2152. * PCDAC/PDADC step for each curve is 64 but we can write more than
  2153. * one curves on hw so we can go up to 128 (which is the max step we
  2154. * can write on the final table).
  2155. *
  2156. * We write y values (PCDAC/PDADC steps) on hw.
  2157. */
  2158. static void
  2159. ath5k_create_power_curve(s16 pmin, s16 pmax,
  2160. const s16 *pwr, const u8 *vpd,
  2161. u8 num_points,
  2162. u8 *vpd_table, u8 type)
  2163. {
  2164. u8 idx[2] = { 0, 1 };
  2165. s16 pwr_i = 2 * pmin;
  2166. int i;
  2167. if (num_points < 2)
  2168. return;
  2169. /* We want the whole line, so adjust boundaries
  2170. * to cover the entire power range. Note that
  2171. * power values are already 0.25dB so no need
  2172. * to multiply pwr_i by 2 */
  2173. if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
  2174. pwr_i = pmin;
  2175. pmin = 0;
  2176. pmax = 63;
  2177. }
  2178. /* Find surrounding turning points (TPs)
  2179. * and interpolate between them */
  2180. for (i = 0; (i <= (u16) (pmax - pmin)) &&
  2181. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  2182. /* We passed the right TP, move to the next set of TPs
  2183. * if we pass the last TP, extrapolate above using the last
  2184. * two TPs for ratio */
  2185. if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
  2186. idx[0]++;
  2187. idx[1]++;
  2188. }
  2189. vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
  2190. pwr[idx[0]], pwr[idx[1]],
  2191. vpd[idx[0]], vpd[idx[1]]);
  2192. /* Increase by 0.5dB
  2193. * (0.25 dB units) */
  2194. pwr_i += 2;
  2195. }
  2196. }
  2197. /**
  2198. * ath5k_get_chan_pcal_surrounding_piers() - Get surrounding calibration piers
  2199. * for a given channel.
  2200. * @ah: The &struct ath5k_hw
  2201. * @channel: The &struct ieee80211_channel
  2202. * @pcinfo_l: The &struct ath5k_chan_pcal_info to put the left cal. pier
  2203. * @pcinfo_r: The &struct ath5k_chan_pcal_info to put the right cal. pier
  2204. *
  2205. * Get the surrounding per-channel power calibration piers
  2206. * for a given frequency so that we can interpolate between
  2207. * them and come up with an appropriate dataset for our current
  2208. * channel.
  2209. */
  2210. static void
  2211. ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
  2212. struct ieee80211_channel *channel,
  2213. struct ath5k_chan_pcal_info **pcinfo_l,
  2214. struct ath5k_chan_pcal_info **pcinfo_r)
  2215. {
  2216. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2217. struct ath5k_chan_pcal_info *pcinfo;
  2218. u8 idx_l, idx_r;
  2219. u8 mode, max, i;
  2220. u32 target = channel->center_freq;
  2221. idx_l = 0;
  2222. idx_r = 0;
  2223. switch (channel->hw_value) {
  2224. case AR5K_EEPROM_MODE_11A:
  2225. pcinfo = ee->ee_pwr_cal_a;
  2226. mode = AR5K_EEPROM_MODE_11A;
  2227. break;
  2228. case AR5K_EEPROM_MODE_11B:
  2229. pcinfo = ee->ee_pwr_cal_b;
  2230. mode = AR5K_EEPROM_MODE_11B;
  2231. break;
  2232. case AR5K_EEPROM_MODE_11G:
  2233. default:
  2234. pcinfo = ee->ee_pwr_cal_g;
  2235. mode = AR5K_EEPROM_MODE_11G;
  2236. break;
  2237. }
  2238. max = ee->ee_n_piers[mode] - 1;
  2239. /* Frequency is below our calibrated
  2240. * range. Use the lowest power curve
  2241. * we have */
  2242. if (target < pcinfo[0].freq) {
  2243. idx_l = idx_r = 0;
  2244. goto done;
  2245. }
  2246. /* Frequency is above our calibrated
  2247. * range. Use the highest power curve
  2248. * we have */
  2249. if (target > pcinfo[max].freq) {
  2250. idx_l = idx_r = max;
  2251. goto done;
  2252. }
  2253. /* Frequency is inside our calibrated
  2254. * channel range. Pick the surrounding
  2255. * calibration piers so that we can
  2256. * interpolate */
  2257. for (i = 0; i <= max; i++) {
  2258. /* Frequency matches one of our calibration
  2259. * piers, no need to interpolate, just use
  2260. * that calibration pier */
  2261. if (pcinfo[i].freq == target) {
  2262. idx_l = idx_r = i;
  2263. goto done;
  2264. }
  2265. /* We found a calibration pier that's above
  2266. * frequency, use this pier and the previous
  2267. * one to interpolate */
  2268. if (target < pcinfo[i].freq) {
  2269. idx_r = i;
  2270. idx_l = idx_r - 1;
  2271. goto done;
  2272. }
  2273. }
  2274. done:
  2275. *pcinfo_l = &pcinfo[idx_l];
  2276. *pcinfo_r = &pcinfo[idx_r];
  2277. }
  2278. /**
  2279. * ath5k_get_rate_pcal_data() - Get the interpolated per-rate power
  2280. * calibration data
  2281. * @ah: The &struct ath5k_hw *ah,
  2282. * @channel: The &struct ieee80211_channel
  2283. * @rates: The &struct ath5k_rate_pcal_info to fill
  2284. *
  2285. * Get the surrounding per-rate power calibration data
  2286. * for a given frequency and interpolate between power
  2287. * values to set max target power supported by hw for
  2288. * each rate on this frequency.
  2289. */
  2290. static void
  2291. ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
  2292. struct ieee80211_channel *channel,
  2293. struct ath5k_rate_pcal_info *rates)
  2294. {
  2295. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2296. struct ath5k_rate_pcal_info *rpinfo;
  2297. u8 idx_l, idx_r;
  2298. u8 mode, max, i;
  2299. u32 target = channel->center_freq;
  2300. idx_l = 0;
  2301. idx_r = 0;
  2302. switch (channel->hw_value) {
  2303. case AR5K_MODE_11A:
  2304. rpinfo = ee->ee_rate_tpwr_a;
  2305. mode = AR5K_EEPROM_MODE_11A;
  2306. break;
  2307. case AR5K_MODE_11B:
  2308. rpinfo = ee->ee_rate_tpwr_b;
  2309. mode = AR5K_EEPROM_MODE_11B;
  2310. break;
  2311. case AR5K_MODE_11G:
  2312. default:
  2313. rpinfo = ee->ee_rate_tpwr_g;
  2314. mode = AR5K_EEPROM_MODE_11G;
  2315. break;
  2316. }
  2317. max = ee->ee_rate_target_pwr_num[mode] - 1;
  2318. /* Get the surrounding calibration
  2319. * piers - same as above */
  2320. if (target < rpinfo[0].freq) {
  2321. idx_l = idx_r = 0;
  2322. goto done;
  2323. }
  2324. if (target > rpinfo[max].freq) {
  2325. idx_l = idx_r = max;
  2326. goto done;
  2327. }
  2328. for (i = 0; i <= max; i++) {
  2329. if (rpinfo[i].freq == target) {
  2330. idx_l = idx_r = i;
  2331. goto done;
  2332. }
  2333. if (target < rpinfo[i].freq) {
  2334. idx_r = i;
  2335. idx_l = idx_r - 1;
  2336. goto done;
  2337. }
  2338. }
  2339. done:
  2340. /* Now interpolate power value, based on the frequency */
  2341. rates->freq = target;
  2342. rates->target_power_6to24 =
  2343. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2344. rpinfo[idx_r].freq,
  2345. rpinfo[idx_l].target_power_6to24,
  2346. rpinfo[idx_r].target_power_6to24);
  2347. rates->target_power_36 =
  2348. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2349. rpinfo[idx_r].freq,
  2350. rpinfo[idx_l].target_power_36,
  2351. rpinfo[idx_r].target_power_36);
  2352. rates->target_power_48 =
  2353. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2354. rpinfo[idx_r].freq,
  2355. rpinfo[idx_l].target_power_48,
  2356. rpinfo[idx_r].target_power_48);
  2357. rates->target_power_54 =
  2358. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  2359. rpinfo[idx_r].freq,
  2360. rpinfo[idx_l].target_power_54,
  2361. rpinfo[idx_r].target_power_54);
  2362. }
  2363. /**
  2364. * ath5k_get_max_ctl_power() - Get max edge power for a given frequency
  2365. * @ah: the &struct ath5k_hw
  2366. * @channel: The &struct ieee80211_channel
  2367. *
  2368. * Get the max edge power for this channel if
  2369. * we have such data from EEPROM's Conformance Test
  2370. * Limits (CTL), and limit max power if needed.
  2371. */
  2372. static void
  2373. ath5k_get_max_ctl_power(struct ath5k_hw *ah,
  2374. struct ieee80211_channel *channel)
  2375. {
  2376. struct ath_regulatory *regulatory = ath5k_hw_regulatory(ah);
  2377. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2378. struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
  2379. u8 *ctl_val = ee->ee_ctl;
  2380. s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
  2381. s16 edge_pwr = 0;
  2382. u8 rep_idx;
  2383. u8 i, ctl_mode;
  2384. u8 ctl_idx = 0xFF;
  2385. u32 target = channel->center_freq;
  2386. ctl_mode = ath_regd_get_band_ctl(regulatory, channel->band);
  2387. switch (channel->hw_value) {
  2388. case AR5K_MODE_11A:
  2389. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  2390. ctl_mode |= AR5K_CTL_TURBO;
  2391. else
  2392. ctl_mode |= AR5K_CTL_11A;
  2393. break;
  2394. case AR5K_MODE_11G:
  2395. if (ah->ah_bwmode == AR5K_BWMODE_40MHZ)
  2396. ctl_mode |= AR5K_CTL_TURBOG;
  2397. else
  2398. ctl_mode |= AR5K_CTL_11G;
  2399. break;
  2400. case AR5K_MODE_11B:
  2401. ctl_mode |= AR5K_CTL_11B;
  2402. break;
  2403. default:
  2404. return;
  2405. }
  2406. for (i = 0; i < ee->ee_ctls; i++) {
  2407. if (ctl_val[i] == ctl_mode) {
  2408. ctl_idx = i;
  2409. break;
  2410. }
  2411. }
  2412. /* If we have a CTL dataset available grab it and find the
  2413. * edge power for our frequency */
  2414. if (ctl_idx == 0xFF)
  2415. return;
  2416. /* Edge powers are sorted by frequency from lower
  2417. * to higher. Each CTL corresponds to 8 edge power
  2418. * measurements. */
  2419. rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
  2420. /* Don't do boundaries check because we
  2421. * might have more that one bands defined
  2422. * for this mode */
  2423. /* Get the edge power that's closer to our
  2424. * frequency */
  2425. for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
  2426. rep_idx += i;
  2427. if (target <= rep[rep_idx].freq)
  2428. edge_pwr = (s16) rep[rep_idx].edge;
  2429. }
  2430. if (edge_pwr)
  2431. ah->ah_txpower.txp_max_pwr = 4 * min(edge_pwr, max_chan_pwr);
  2432. }
  2433. /*
  2434. * Power to PCDAC table functions
  2435. */
  2436. /**
  2437. * DOC: Power to PCDAC table functions
  2438. *
  2439. * For RF5111 we have an XPD -eXternal Power Detector- curve
  2440. * for each calibrated channel. Each curve has 0,5dB Power steps
  2441. * on x axis and PCDAC steps (offsets) on y axis and looks like an
  2442. * exponential function. To recreate the curve we read 11 points
  2443. * from eeprom (eeprom.c) and interpolate here.
  2444. *
  2445. * For RF5112 we have 4 XPD -eXternal Power Detector- curves
  2446. * for each calibrated channel on 0, -6, -12 and -18dBm but we only
  2447. * use the higher (3) and the lower (0) curves. Each curve again has 0.5dB
  2448. * power steps on x axis and PCDAC steps on y axis and looks like a
  2449. * linear function. To recreate the curve and pass the power values
  2450. * on hw, we get 4 points for xpd 0 (lower gain -> max power)
  2451. * and 3 points for xpd 3 (higher gain -> lower power) from eeprom (eeprom.c)
  2452. * and interpolate here.
  2453. *
  2454. * For a given channel we get the calibrated points (piers) for it or
  2455. * -if we don't have calibration data for this specific channel- from the
  2456. * available surrounding channels we have calibration data for, after we do a
  2457. * linear interpolation between them. Then since we have our calibrated points
  2458. * for this channel, we do again a linear interpolation between them to get the
  2459. * whole curve.
  2460. *
  2461. * We finally write the Y values of the curve(s) (the PCDAC values) on hw
  2462. */
  2463. /**
  2464. * ath5k_fill_pwr_to_pcdac_table() - Fill Power to PCDAC table on RF5111
  2465. * @ah: The &struct ath5k_hw
  2466. * @table_min: Minimum power (x min)
  2467. * @table_max: Maximum power (x max)
  2468. *
  2469. * No further processing is needed for RF5111, the only thing we have to
  2470. * do is fill the values below and above calibration range since eeprom data
  2471. * may not cover the entire PCDAC table.
  2472. */
  2473. static void
  2474. ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
  2475. s16 *table_max)
  2476. {
  2477. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2478. u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
  2479. u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
  2480. s16 min_pwr, max_pwr;
  2481. /* Get table boundaries */
  2482. min_pwr = table_min[0];
  2483. pcdac_0 = pcdac_tmp[0];
  2484. max_pwr = table_max[0];
  2485. pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
  2486. /* Extrapolate below minimum using pcdac_0 */
  2487. pcdac_i = 0;
  2488. for (i = 0; i < min_pwr; i++)
  2489. pcdac_out[pcdac_i++] = pcdac_0;
  2490. /* Copy values from pcdac_tmp */
  2491. pwr_idx = min_pwr;
  2492. for (i = 0; pwr_idx <= max_pwr &&
  2493. pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
  2494. pcdac_out[pcdac_i++] = pcdac_tmp[i];
  2495. pwr_idx++;
  2496. }
  2497. /* Extrapolate above maximum */
  2498. while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
  2499. pcdac_out[pcdac_i++] = pcdac_n;
  2500. }
  2501. /**
  2502. * ath5k_combine_linear_pcdac_curves() - Combine available PCDAC Curves
  2503. * @ah: The &struct ath5k_hw
  2504. * @table_min: Minimum power (x min)
  2505. * @table_max: Maximum power (x max)
  2506. * @pdcurves: Number of pd curves
  2507. *
  2508. * Combine available XPD Curves and fill Linear Power to PCDAC table on RF5112
  2509. * RFX112 can have up to 2 curves (one for low txpower range and one for
  2510. * higher txpower range). We need to put them both on pcdac_out and place
  2511. * them in the correct location. In case we only have one curve available
  2512. * just fit it on pcdac_out (it's supposed to cover the entire range of
  2513. * available pwr levels since it's always the higher power curve). Extrapolate
  2514. * below and above final table if needed.
  2515. */
  2516. static void
  2517. ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
  2518. s16 *table_max, u8 pdcurves)
  2519. {
  2520. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2521. u8 *pcdac_low_pwr;
  2522. u8 *pcdac_high_pwr;
  2523. u8 *pcdac_tmp;
  2524. u8 pwr;
  2525. s16 max_pwr_idx;
  2526. s16 min_pwr_idx;
  2527. s16 mid_pwr_idx = 0;
  2528. /* Edge flag turns on the 7nth bit on the PCDAC
  2529. * to declare the higher power curve (force values
  2530. * to be greater than 64). If we only have one curve
  2531. * we don't need to set this, if we have 2 curves and
  2532. * fill the table backwards this can also be used to
  2533. * switch from higher power curve to lower power curve */
  2534. u8 edge_flag;
  2535. int i;
  2536. /* When we have only one curve available
  2537. * that's the higher power curve. If we have
  2538. * two curves the first is the high power curve
  2539. * and the next is the low power curve. */
  2540. if (pdcurves > 1) {
  2541. pcdac_low_pwr = ah->ah_txpower.tmpL[1];
  2542. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2543. mid_pwr_idx = table_max[1] - table_min[1] - 1;
  2544. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2545. /* If table size goes beyond 31.5dB, keep the
  2546. * upper 31.5dB range when setting tx power.
  2547. * Note: 126 = 31.5 dB in quarter dB steps */
  2548. if (table_max[0] - table_min[1] > 126)
  2549. min_pwr_idx = table_max[0] - 126;
  2550. else
  2551. min_pwr_idx = table_min[1];
  2552. /* Since we fill table backwards
  2553. * start from high power curve */
  2554. pcdac_tmp = pcdac_high_pwr;
  2555. edge_flag = 0x40;
  2556. } else {
  2557. pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
  2558. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  2559. min_pwr_idx = table_min[0];
  2560. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  2561. pcdac_tmp = pcdac_high_pwr;
  2562. edge_flag = 0;
  2563. }
  2564. /* This is used when setting tx power*/
  2565. ah->ah_txpower.txp_min_idx = min_pwr_idx / 2;
  2566. /* Fill Power to PCDAC table backwards */
  2567. pwr = max_pwr_idx;
  2568. for (i = 63; i >= 0; i--) {
  2569. /* Entering lower power range, reset
  2570. * edge flag and set pcdac_tmp to lower
  2571. * power curve.*/
  2572. if (edge_flag == 0x40 &&
  2573. (2 * pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
  2574. edge_flag = 0x00;
  2575. pcdac_tmp = pcdac_low_pwr;
  2576. pwr = mid_pwr_idx / 2;
  2577. }
  2578. /* Don't go below 1, extrapolate below if we have
  2579. * already switched to the lower power curve -or
  2580. * we only have one curve and edge_flag is zero
  2581. * anyway */
  2582. if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
  2583. while (i >= 0) {
  2584. pcdac_out[i] = pcdac_out[i + 1];
  2585. i--;
  2586. }
  2587. break;
  2588. }
  2589. pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
  2590. /* Extrapolate above if pcdac is greater than
  2591. * 126 -this can happen because we OR pcdac_out
  2592. * value with edge_flag on high power curve */
  2593. if (pcdac_out[i] > 126)
  2594. pcdac_out[i] = 126;
  2595. /* Decrease by a 0.5dB step */
  2596. pwr--;
  2597. }
  2598. }
  2599. /**
  2600. * ath5k_write_pcdac_table() - Write the PCDAC values on hw
  2601. * @ah: The &struct ath5k_hw
  2602. */
  2603. static void
  2604. ath5k_write_pcdac_table(struct ath5k_hw *ah)
  2605. {
  2606. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  2607. int i;
  2608. /*
  2609. * Write TX power values
  2610. */
  2611. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2612. ath5k_hw_reg_write(ah,
  2613. (((pcdac_out[2 * i + 0] << 8 | 0xff) & 0xffff) << 0) |
  2614. (((pcdac_out[2 * i + 1] << 8 | 0xff) & 0xffff) << 16),
  2615. AR5K_PHY_PCDAC_TXPOWER(i));
  2616. }
  2617. }
  2618. /*
  2619. * Power to PDADC table functions
  2620. */
  2621. /**
  2622. * DOC: Power to PDADC table functions
  2623. *
  2624. * For RF2413 and later we have a Power to PDADC table (Power Detector)
  2625. * instead of a PCDAC (Power Control) and 4 pd gain curves for each
  2626. * calibrated channel. Each curve has power on x axis in 0.5 db steps and
  2627. * PDADC steps on y axis and looks like an exponential function like the
  2628. * RF5111 curve.
  2629. *
  2630. * To recreate the curves we read the points from eeprom (eeprom.c)
  2631. * and interpolate here. Note that in most cases only 2 (higher and lower)
  2632. * curves are used (like RF5112) but vendors have the opportunity to include
  2633. * all 4 curves on eeprom. The final curve (higher power) has an extra
  2634. * point for better accuracy like RF5112.
  2635. *
  2636. * The process is similar to what we do above for RF5111/5112
  2637. */
  2638. /**
  2639. * ath5k_combine_pwr_to_pdadc_curves() - Combine the various PDADC curves
  2640. * @ah: The &struct ath5k_hw
  2641. * @pwr_min: Minimum power (x min)
  2642. * @pwr_max: Maximum power (x max)
  2643. * @pdcurves: Number of available curves
  2644. *
  2645. * Combine the various pd curves and create the final Power to PDADC table
  2646. * We can have up to 4 pd curves, we need to do a similar process
  2647. * as we do for RF5112. This time we don't have an edge_flag but we
  2648. * set the gain boundaries on a separate register.
  2649. */
  2650. static void
  2651. ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
  2652. s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
  2653. {
  2654. u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
  2655. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2656. u8 *pdadc_tmp;
  2657. s16 pdadc_0;
  2658. u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
  2659. u8 pd_gain_overlap;
  2660. /* Note: Register value is initialized on initvals
  2661. * there is no feedback from hw.
  2662. * XXX: What about pd_gain_overlap from EEPROM ? */
  2663. pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
  2664. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
  2665. /* Create final PDADC table */
  2666. for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
  2667. pdadc_tmp = ah->ah_txpower.tmpL[pdg];
  2668. if (pdg == pdcurves - 1)
  2669. /* 2 dB boundary stretch for last
  2670. * (higher power) curve */
  2671. gain_boundaries[pdg] = pwr_max[pdg] + 4;
  2672. else
  2673. /* Set gain boundary in the middle
  2674. * between this curve and the next one */
  2675. gain_boundaries[pdg] =
  2676. (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
  2677. /* Sanity check in case our 2 db stretch got out of
  2678. * range. */
  2679. if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
  2680. gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
  2681. /* For the first curve (lower power)
  2682. * start from 0 dB */
  2683. if (pdg == 0)
  2684. pdadc_0 = 0;
  2685. else
  2686. /* For the other curves use the gain overlap */
  2687. pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
  2688. pd_gain_overlap;
  2689. /* Force each power step to be at least 0.5 dB */
  2690. if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
  2691. pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
  2692. else
  2693. pwr_step = 1;
  2694. /* If pdadc_0 is negative, we need to extrapolate
  2695. * below this pdgain by a number of pwr_steps */
  2696. while ((pdadc_0 < 0) && (pdadc_i < 128)) {
  2697. s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
  2698. pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
  2699. pdadc_0++;
  2700. }
  2701. /* Set last pwr level, using gain boundaries */
  2702. pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
  2703. /* Limit it to be inside pwr range */
  2704. table_size = pwr_max[pdg] - pwr_min[pdg];
  2705. max_idx = min(pdadc_n, table_size);
  2706. /* Fill pdadc_out table */
  2707. while (pdadc_0 < max_idx && pdadc_i < 128)
  2708. pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
  2709. /* Need to extrapolate above this pdgain? */
  2710. if (pdadc_n <= max_idx)
  2711. continue;
  2712. /* Force each power step to be at least 0.5 dB */
  2713. if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
  2714. pwr_step = pdadc_tmp[table_size - 1] -
  2715. pdadc_tmp[table_size - 2];
  2716. else
  2717. pwr_step = 1;
  2718. /* Extrapolate above */
  2719. while ((pdadc_0 < (s16) pdadc_n) &&
  2720. (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
  2721. s16 tmp = pdadc_tmp[table_size - 1] +
  2722. (pdadc_0 - max_idx) * pwr_step;
  2723. pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
  2724. pdadc_0++;
  2725. }
  2726. }
  2727. while (pdg < AR5K_EEPROM_N_PD_GAINS) {
  2728. gain_boundaries[pdg] = gain_boundaries[pdg - 1];
  2729. pdg++;
  2730. }
  2731. while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
  2732. pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
  2733. pdadc_i++;
  2734. }
  2735. /* Set gain boundaries */
  2736. ath5k_hw_reg_write(ah,
  2737. AR5K_REG_SM(pd_gain_overlap,
  2738. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
  2739. AR5K_REG_SM(gain_boundaries[0],
  2740. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
  2741. AR5K_REG_SM(gain_boundaries[1],
  2742. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
  2743. AR5K_REG_SM(gain_boundaries[2],
  2744. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
  2745. AR5K_REG_SM(gain_boundaries[3],
  2746. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
  2747. AR5K_PHY_TPC_RG5);
  2748. /* Used for setting rate power table */
  2749. ah->ah_txpower.txp_min_idx = pwr_min[0];
  2750. }
  2751. /**
  2752. * ath5k_write_pwr_to_pdadc_table() - Write the PDADC values on hw
  2753. * @ah: The &struct ath5k_hw
  2754. * @ee_mode: One of enum ath5k_driver_mode
  2755. */
  2756. static void
  2757. ath5k_write_pwr_to_pdadc_table(struct ath5k_hw *ah, u8 ee_mode)
  2758. {
  2759. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2760. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  2761. u8 *pdg_to_idx = ee->ee_pdc_to_idx[ee_mode];
  2762. u8 pdcurves = ee->ee_pd_gains[ee_mode];
  2763. u32 reg;
  2764. u8 i;
  2765. /* Select the right pdgain curves */
  2766. /* Clear current settings */
  2767. reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
  2768. reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
  2769. AR5K_PHY_TPC_RG1_PDGAIN_2 |
  2770. AR5K_PHY_TPC_RG1_PDGAIN_3 |
  2771. AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2772. /*
  2773. * Use pd_gains curve from eeprom
  2774. *
  2775. * This overrides the default setting from initvals
  2776. * in case some vendors (e.g. Zcomax) don't use the default
  2777. * curves. If we don't honor their settings we 'll get a
  2778. * 5dB (1 * gain overlap ?) drop.
  2779. */
  2780. reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  2781. switch (pdcurves) {
  2782. case 3:
  2783. reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
  2784. fallthrough;
  2785. case 2:
  2786. reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
  2787. fallthrough;
  2788. case 1:
  2789. reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
  2790. break;
  2791. }
  2792. ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
  2793. /*
  2794. * Write TX power values
  2795. */
  2796. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  2797. u32 val = get_unaligned_le32(&pdadc_out[4 * i]);
  2798. ath5k_hw_reg_write(ah, val, AR5K_PHY_PDADC_TXPOWER(i));
  2799. }
  2800. }
  2801. /*
  2802. * Common code for PCDAC/PDADC tables
  2803. */
  2804. /**
  2805. * ath5k_setup_channel_powertable() - Set up power table for this channel
  2806. * @ah: The &struct ath5k_hw
  2807. * @channel: The &struct ieee80211_channel
  2808. * @ee_mode: One of enum ath5k_driver_mode
  2809. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2810. *
  2811. * This is the main function that uses all of the above
  2812. * to set PCDAC/PDADC table on hw for the current channel.
  2813. * This table is used for tx power calibration on the baseband,
  2814. * without it we get weird tx power levels and in some cases
  2815. * distorted spectral mask
  2816. */
  2817. static int
  2818. ath5k_setup_channel_powertable(struct ath5k_hw *ah,
  2819. struct ieee80211_channel *channel,
  2820. u8 ee_mode, u8 type)
  2821. {
  2822. struct ath5k_pdgain_info *pdg_L, *pdg_R;
  2823. struct ath5k_chan_pcal_info *pcinfo_L;
  2824. struct ath5k_chan_pcal_info *pcinfo_R;
  2825. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  2826. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  2827. s16 table_min[AR5K_EEPROM_N_PD_GAINS];
  2828. s16 table_max[AR5K_EEPROM_N_PD_GAINS];
  2829. u8 *tmpL;
  2830. u8 *tmpR;
  2831. u32 target = channel->center_freq;
  2832. int pdg, i;
  2833. /* Get surrounding freq piers for this channel */
  2834. ath5k_get_chan_pcal_surrounding_piers(ah, channel,
  2835. &pcinfo_L,
  2836. &pcinfo_R);
  2837. /* Loop over pd gain curves on
  2838. * surrounding freq piers by index */
  2839. for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
  2840. /* Fill curves in reverse order
  2841. * from lower power (max gain)
  2842. * to higher power. Use curve -> idx
  2843. * backmapping we did on eeprom init */
  2844. u8 idx = pdg_curve_to_idx[pdg];
  2845. /* Grab the needed curves by index */
  2846. pdg_L = &pcinfo_L->pd_curves[idx];
  2847. pdg_R = &pcinfo_R->pd_curves[idx];
  2848. /* Initialize the temp tables */
  2849. tmpL = ah->ah_txpower.tmpL[pdg];
  2850. tmpR = ah->ah_txpower.tmpR[pdg];
  2851. /* Set curve's x boundaries and create
  2852. * curves so that they cover the same
  2853. * range (if we don't do that one table
  2854. * will have values on some range and the
  2855. * other one won't have any so interpolation
  2856. * will fail) */
  2857. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2858. pdg_R->pd_pwr[0]) / 2;
  2859. table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2860. pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
  2861. /* Now create the curves on surrounding channels
  2862. * and interpolate if needed to get the final
  2863. * curve for this gain on this channel */
  2864. switch (type) {
  2865. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2866. /* Override min/max so that we don't loose
  2867. * accuracy (don't divide by 2) */
  2868. table_min[pdg] = min(pdg_L->pd_pwr[0],
  2869. pdg_R->pd_pwr[0]);
  2870. table_max[pdg] =
  2871. max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  2872. pdg_R->pd_pwr[pdg_R->pd_points - 1]);
  2873. /* Override minimum so that we don't get
  2874. * out of bounds while extrapolating
  2875. * below. Don't do this when we have 2
  2876. * curves and we are on the high power curve
  2877. * because table_min is ok in this case */
  2878. if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
  2879. table_min[pdg] =
  2880. ath5k_get_linear_pcdac_min(pdg_L->pd_step,
  2881. pdg_R->pd_step,
  2882. pdg_L->pd_pwr,
  2883. pdg_R->pd_pwr);
  2884. /* Don't go too low because we will
  2885. * miss the upper part of the curve.
  2886. * Note: 126 = 31.5dB (max power supported)
  2887. * in 0.25dB units */
  2888. if (table_max[pdg] - table_min[pdg] > 126)
  2889. table_min[pdg] = table_max[pdg] - 126;
  2890. }
  2891. fallthrough;
  2892. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2893. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2894. ath5k_create_power_curve(table_min[pdg],
  2895. table_max[pdg],
  2896. pdg_L->pd_pwr,
  2897. pdg_L->pd_step,
  2898. pdg_L->pd_points, tmpL, type);
  2899. /* We are in a calibration
  2900. * pier, no need to interpolate
  2901. * between freq piers */
  2902. if (pcinfo_L == pcinfo_R)
  2903. continue;
  2904. ath5k_create_power_curve(table_min[pdg],
  2905. table_max[pdg],
  2906. pdg_R->pd_pwr,
  2907. pdg_R->pd_step,
  2908. pdg_R->pd_points, tmpR, type);
  2909. break;
  2910. default:
  2911. return -EINVAL;
  2912. }
  2913. /* Interpolate between curves
  2914. * of surrounding freq piers to
  2915. * get the final curve for this
  2916. * pd gain. Re-use tmpL for interpolation
  2917. * output */
  2918. for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
  2919. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  2920. tmpL[i] = (u8) ath5k_get_interpolated_value(target,
  2921. (s16) pcinfo_L->freq,
  2922. (s16) pcinfo_R->freq,
  2923. (s16) tmpL[i],
  2924. (s16) tmpR[i]);
  2925. }
  2926. }
  2927. /* Now we have a set of curves for this
  2928. * channel on tmpL (x range is table_max - table_min
  2929. * and y values are tmpL[pdg][]) sorted in the same
  2930. * order as EEPROM (because we've used the backmapping).
  2931. * So for RF5112 it's from higher power to lower power
  2932. * and for RF2413 it's from lower power to higher power.
  2933. * For RF5111 we only have one curve. */
  2934. /* Fill min and max power levels for this
  2935. * channel by interpolating the values on
  2936. * surrounding channels to complete the dataset */
  2937. ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
  2938. (s16) pcinfo_L->freq,
  2939. (s16) pcinfo_R->freq,
  2940. pcinfo_L->min_pwr, pcinfo_R->min_pwr);
  2941. ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
  2942. (s16) pcinfo_L->freq,
  2943. (s16) pcinfo_R->freq,
  2944. pcinfo_L->max_pwr, pcinfo_R->max_pwr);
  2945. /* Fill PCDAC/PDADC table */
  2946. switch (type) {
  2947. case AR5K_PWRTABLE_LINEAR_PCDAC:
  2948. /* For RF5112 we can have one or two curves
  2949. * and each curve covers a certain power lvl
  2950. * range so we need to do some more processing */
  2951. ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
  2952. ee->ee_pd_gains[ee_mode]);
  2953. /* Set txp.offset so that we can
  2954. * match max power value with max
  2955. * table index */
  2956. ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
  2957. break;
  2958. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  2959. /* We are done for RF5111 since it has only
  2960. * one curve, just fit the curve on the table */
  2961. ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
  2962. /* No rate powertable adjustment for RF5111 */
  2963. ah->ah_txpower.txp_min_idx = 0;
  2964. ah->ah_txpower.txp_offset = 0;
  2965. break;
  2966. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2967. /* Set PDADC boundaries and fill
  2968. * final PDADC table */
  2969. ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
  2970. ee->ee_pd_gains[ee_mode]);
  2971. /* Set txp.offset, note that table_min
  2972. * can be negative */
  2973. ah->ah_txpower.txp_offset = table_min[0];
  2974. break;
  2975. default:
  2976. return -EINVAL;
  2977. }
  2978. ah->ah_txpower.txp_setup = true;
  2979. return 0;
  2980. }
  2981. /**
  2982. * ath5k_write_channel_powertable() - Set power table for current channel on hw
  2983. * @ah: The &struct ath5k_hw
  2984. * @ee_mode: One of enum ath5k_driver_mode
  2985. * @type: One of enum ath5k_powertable_type (eeprom.h)
  2986. */
  2987. static void
  2988. ath5k_write_channel_powertable(struct ath5k_hw *ah, u8 ee_mode, u8 type)
  2989. {
  2990. if (type == AR5K_PWRTABLE_PWR_TO_PDADC)
  2991. ath5k_write_pwr_to_pdadc_table(ah, ee_mode);
  2992. else
  2993. ath5k_write_pcdac_table(ah);
  2994. }
  2995. /**
  2996. * DOC: Per-rate tx power setting
  2997. *
  2998. * This is the code that sets the desired tx power limit (below
  2999. * maximum) on hw for each rate (we also have TPC that sets
  3000. * power per packet type). We do that by providing an index on the
  3001. * PCDAC/PDADC table we set up above, for each rate.
  3002. *
  3003. * For now we only limit txpower based on maximum tx power
  3004. * supported by hw (what's inside rate_info) + conformance test
  3005. * limits. We need to limit this even more, based on regulatory domain
  3006. * etc to be safe. Normally this is done from above so we don't care
  3007. * here, all we care is that the tx power we set will be O.K.
  3008. * for the hw (e.g. won't create noise on PA etc).
  3009. *
  3010. * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps -
  3011. * x values) and is indexed as follows:
  3012. * rates[0] - rates[7] -> OFDM rates
  3013. * rates[8] - rates[14] -> CCK rates
  3014. * rates[15] -> XR rates (they all have the same power)
  3015. */
  3016. /**
  3017. * ath5k_setup_rate_powertable() - Set up rate power table for a given tx power
  3018. * @ah: The &struct ath5k_hw
  3019. * @max_pwr: The maximum tx power requested in 0.5dB steps
  3020. * @rate_info: The &struct ath5k_rate_pcal_info to fill
  3021. * @ee_mode: One of enum ath5k_driver_mode
  3022. */
  3023. static void
  3024. ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
  3025. struct ath5k_rate_pcal_info *rate_info,
  3026. u8 ee_mode)
  3027. {
  3028. unsigned int i;
  3029. u16 *rates;
  3030. s16 rate_idx_scaled = 0;
  3031. /* max_pwr is power level we got from driver/user in 0.5dB
  3032. * units, switch to 0.25dB units so we can compare */
  3033. max_pwr *= 2;
  3034. max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
  3035. /* apply rate limits */
  3036. rates = ah->ah_txpower.txp_rates_power_table;
  3037. /* OFDM rates 6 to 24Mb/s */
  3038. for (i = 0; i < 5; i++)
  3039. rates[i] = min(max_pwr, rate_info->target_power_6to24);
  3040. /* Rest OFDM rates */
  3041. rates[5] = min(rates[0], rate_info->target_power_36);
  3042. rates[6] = min(rates[0], rate_info->target_power_48);
  3043. rates[7] = min(rates[0], rate_info->target_power_54);
  3044. /* CCK rates */
  3045. /* 1L */
  3046. rates[8] = min(rates[0], rate_info->target_power_6to24);
  3047. /* 2L */
  3048. rates[9] = min(rates[0], rate_info->target_power_36);
  3049. /* 2S */
  3050. rates[10] = min(rates[0], rate_info->target_power_36);
  3051. /* 5L */
  3052. rates[11] = min(rates[0], rate_info->target_power_48);
  3053. /* 5S */
  3054. rates[12] = min(rates[0], rate_info->target_power_48);
  3055. /* 11L */
  3056. rates[13] = min(rates[0], rate_info->target_power_54);
  3057. /* 11S */
  3058. rates[14] = min(rates[0], rate_info->target_power_54);
  3059. /* XR rates */
  3060. rates[15] = min(rates[0], rate_info->target_power_6to24);
  3061. /* CCK rates have different peak to average ratio
  3062. * so we have to tweak their power so that gainf
  3063. * correction works ok. For this we use OFDM to
  3064. * CCK delta from eeprom */
  3065. if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
  3066. (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
  3067. for (i = 8; i <= 15; i++)
  3068. rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
  3069. /* Save min/max and current tx power for this channel
  3070. * in 0.25dB units.
  3071. *
  3072. * Note: We use rates[0] for current tx power because
  3073. * it covers most of the rates, in most cases. It's our
  3074. * tx power limit and what the user expects to see. */
  3075. ah->ah_txpower.txp_min_pwr = 2 * rates[7];
  3076. ah->ah_txpower.txp_cur_pwr = 2 * rates[0];
  3077. /* Set max txpower for correct OFDM operation on all rates
  3078. * -that is the txpower for 54Mbit-, it's used for the PAPD
  3079. * gain probe and it's in 0.5dB units */
  3080. ah->ah_txpower.txp_ofdm = rates[7];
  3081. /* Now that we have all rates setup use table offset to
  3082. * match the power range set by user with the power indices
  3083. * on PCDAC/PDADC table */
  3084. for (i = 0; i < 16; i++) {
  3085. rate_idx_scaled = rates[i] + ah->ah_txpower.txp_offset;
  3086. /* Don't get out of bounds */
  3087. if (rate_idx_scaled > 63)
  3088. rate_idx_scaled = 63;
  3089. if (rate_idx_scaled < 0)
  3090. rate_idx_scaled = 0;
  3091. rates[i] = rate_idx_scaled;
  3092. }
  3093. }
  3094. /**
  3095. * ath5k_hw_txpower() - Set transmission power limit for a given channel
  3096. * @ah: The &struct ath5k_hw
  3097. * @channel: The &struct ieee80211_channel
  3098. * @txpower: Requested tx power in 0.5dB steps
  3099. *
  3100. * Combines all of the above to set the requested tx power limit
  3101. * on hw.
  3102. */
  3103. static int
  3104. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  3105. u8 txpower)
  3106. {
  3107. struct ath5k_rate_pcal_info rate_info;
  3108. struct ieee80211_channel *curr_channel = ah->ah_current_channel;
  3109. int ee_mode;
  3110. u8 type;
  3111. int ret;
  3112. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  3113. ATH5K_ERR(ah, "invalid tx power: %u\n", txpower);
  3114. return -EINVAL;
  3115. }
  3116. ee_mode = ath5k_eeprom_mode_from_channel(ah, channel);
  3117. /* Initialize TX power table */
  3118. switch (ah->ah_radio) {
  3119. case AR5K_RF5110:
  3120. /* TODO */
  3121. return 0;
  3122. case AR5K_RF5111:
  3123. type = AR5K_PWRTABLE_PWR_TO_PCDAC;
  3124. break;
  3125. case AR5K_RF5112:
  3126. type = AR5K_PWRTABLE_LINEAR_PCDAC;
  3127. break;
  3128. case AR5K_RF2413:
  3129. case AR5K_RF5413:
  3130. case AR5K_RF2316:
  3131. case AR5K_RF2317:
  3132. case AR5K_RF2425:
  3133. type = AR5K_PWRTABLE_PWR_TO_PDADC;
  3134. break;
  3135. default:
  3136. return -EINVAL;
  3137. }
  3138. /*
  3139. * If we don't change channel/mode skip tx powertable calculation
  3140. * and use the cached one.
  3141. */
  3142. if (!ah->ah_txpower.txp_setup ||
  3143. (channel->hw_value != curr_channel->hw_value) ||
  3144. (channel->center_freq != curr_channel->center_freq)) {
  3145. /* Reset TX power values but preserve requested
  3146. * tx power from above */
  3147. int requested_txpower = ah->ah_txpower.txp_requested;
  3148. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  3149. /* Restore TPC setting and requested tx power */
  3150. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  3151. ah->ah_txpower.txp_requested = requested_txpower;
  3152. /* Calculate the powertable */
  3153. ret = ath5k_setup_channel_powertable(ah, channel,
  3154. ee_mode, type);
  3155. if (ret)
  3156. return ret;
  3157. }
  3158. /* Write table on hw */
  3159. ath5k_write_channel_powertable(ah, ee_mode, type);
  3160. /* Limit max power if we have a CTL available */
  3161. ath5k_get_max_ctl_power(ah, channel);
  3162. /* FIXME: Antenna reduction stuff */
  3163. /* FIXME: Limit power on turbo modes */
  3164. /* FIXME: TPC scale reduction */
  3165. /* Get surrounding channels for per-rate power table
  3166. * calibration */
  3167. ath5k_get_rate_pcal_data(ah, channel, &rate_info);
  3168. /* Setup rate power table */
  3169. ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
  3170. /* Write rate power table on hw */
  3171. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  3172. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  3173. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  3174. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  3175. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  3176. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  3177. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  3178. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  3179. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  3180. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  3181. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  3182. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  3183. /* FIXME: TPC support */
  3184. if (ah->ah_txpower.txp_tpc) {
  3185. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  3186. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  3187. ath5k_hw_reg_write(ah,
  3188. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
  3189. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
  3190. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
  3191. AR5K_TPC);
  3192. } else {
  3193. ath5k_hw_reg_write(ah, AR5K_TUNE_MAX_TXPOWER,
  3194. AR5K_PHY_TXPOWER_RATE_MAX);
  3195. }
  3196. return 0;
  3197. }
  3198. /**
  3199. * ath5k_hw_set_txpower_limit() - Set txpower limit for the current channel
  3200. * @ah: The &struct ath5k_hw
  3201. * @txpower: The requested tx power limit in 0.5dB steps
  3202. *
  3203. * This function provides access to ath5k_hw_txpower to the driver in
  3204. * case user or an application changes it while PHY is running.
  3205. */
  3206. int
  3207. ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 txpower)
  3208. {
  3209. ATH5K_DBG(ah, ATH5K_DEBUG_TXPOWER,
  3210. "changing txpower to %d\n", txpower);
  3211. return ath5k_hw_txpower(ah, ah->ah_current_channel, txpower);
  3212. }
  3213. /*************\
  3214. Init function
  3215. \*************/
  3216. /**
  3217. * ath5k_hw_phy_init() - Initialize PHY
  3218. * @ah: The &struct ath5k_hw
  3219. * @channel: The @struct ieee80211_channel
  3220. * @mode: One of enum ath5k_driver_mode
  3221. * @fast: Try a fast channel switch instead
  3222. *
  3223. * This is the main function used during reset to initialize PHY
  3224. * or do a fast channel change if possible.
  3225. *
  3226. * NOTE: Do not call this one from the driver, it assumes PHY is in a
  3227. * warm reset state !
  3228. */
  3229. int
  3230. ath5k_hw_phy_init(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  3231. u8 mode, bool fast)
  3232. {
  3233. struct ieee80211_channel *curr_channel;
  3234. int ret, i;
  3235. u32 phy_tst1;
  3236. ret = 0;
  3237. /*
  3238. * Sanity check for fast flag
  3239. * Don't try fast channel change when changing modulation
  3240. * mode/band. We check for chip compatibility on
  3241. * ath5k_hw_reset.
  3242. */
  3243. curr_channel = ah->ah_current_channel;
  3244. if (fast && (channel->hw_value != curr_channel->hw_value))
  3245. return -EINVAL;
  3246. /*
  3247. * On fast channel change we only set the synth parameters
  3248. * while PHY is running, enable calibration and skip the rest.
  3249. */
  3250. if (fast) {
  3251. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_RFBUS_REQ,
  3252. AR5K_PHY_RFBUS_REQ_REQUEST);
  3253. for (i = 0; i < 100; i++) {
  3254. if (ath5k_hw_reg_read(ah, AR5K_PHY_RFBUS_GRANT))
  3255. break;
  3256. udelay(5);
  3257. }
  3258. /* Failed */
  3259. if (i >= 100)
  3260. return -EIO;
  3261. /* Set channel and wait for synth */
  3262. ret = ath5k_hw_channel(ah, channel);
  3263. if (ret)
  3264. return ret;
  3265. ath5k_hw_wait_for_synth(ah, channel);
  3266. }
  3267. /*
  3268. * Set TX power
  3269. *
  3270. * Note: We need to do that before we set
  3271. * RF buffer settings on 5211/5212+ so that we
  3272. * properly set curve indices.
  3273. */
  3274. ret = ath5k_hw_txpower(ah, channel, ah->ah_txpower.txp_requested ?
  3275. ah->ah_txpower.txp_requested * 2 :
  3276. AR5K_TUNE_MAX_TXPOWER);
  3277. if (ret)
  3278. return ret;
  3279. /* Write OFDM timings on 5212*/
  3280. if (ah->ah_version == AR5K_AR5212 &&
  3281. channel->hw_value != AR5K_MODE_11B) {
  3282. ret = ath5k_hw_write_ofdm_timings(ah, channel);
  3283. if (ret)
  3284. return ret;
  3285. /* Spur info is available only from EEPROM versions
  3286. * greater than 5.3, but the EEPROM routines will use
  3287. * static values for older versions */
  3288. if (ah->ah_mac_srev >= AR5K_SREV_AR5424)
  3289. ath5k_hw_set_spur_mitigation_filter(ah,
  3290. channel);
  3291. }
  3292. /* If we used fast channel switching
  3293. * we are done, release RF bus and
  3294. * fire up NF calibration.
  3295. *
  3296. * Note: Only NF calibration due to
  3297. * channel change, not AGC calibration
  3298. * since AGC is still running !
  3299. */
  3300. if (fast) {
  3301. /*
  3302. * Release RF Bus grant
  3303. */
  3304. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_RFBUS_REQ,
  3305. AR5K_PHY_RFBUS_REQ_REQUEST);
  3306. /*
  3307. * Start NF calibration
  3308. */
  3309. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  3310. AR5K_PHY_AGCCTL_NF);
  3311. return ret;
  3312. }
  3313. /*
  3314. * For 5210 we do all initialization using
  3315. * initvals, so we don't have to modify
  3316. * any settings (5210 also only supports
  3317. * a/aturbo modes)
  3318. */
  3319. if (ah->ah_version != AR5K_AR5210) {
  3320. /*
  3321. * Write initial RF gain settings
  3322. * This should work for both 5111/5112
  3323. */
  3324. ret = ath5k_hw_rfgain_init(ah, channel->band);
  3325. if (ret)
  3326. return ret;
  3327. usleep_range(1000, 1500);
  3328. /*
  3329. * Write RF buffer
  3330. */
  3331. ret = ath5k_hw_rfregs_init(ah, channel, mode);
  3332. if (ret)
  3333. return ret;
  3334. /*Enable/disable 802.11b mode on 5111
  3335. (enable 2111 frequency converter + CCK)*/
  3336. if (ah->ah_radio == AR5K_RF5111) {
  3337. if (mode == AR5K_MODE_11B)
  3338. AR5K_REG_ENABLE_BITS(ah, AR5K_TXCFG,
  3339. AR5K_TXCFG_B_MODE);
  3340. else
  3341. AR5K_REG_DISABLE_BITS(ah, AR5K_TXCFG,
  3342. AR5K_TXCFG_B_MODE);
  3343. }
  3344. } else if (ah->ah_version == AR5K_AR5210) {
  3345. usleep_range(1000, 1500);
  3346. /* Disable phy and wait */
  3347. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  3348. usleep_range(1000, 1500);
  3349. }
  3350. /* Set channel on PHY */
  3351. ret = ath5k_hw_channel(ah, channel);
  3352. if (ret)
  3353. return ret;
  3354. /*
  3355. * Enable the PHY and wait until completion
  3356. * This includes BaseBand and Synthesizer
  3357. * activation.
  3358. */
  3359. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  3360. ath5k_hw_wait_for_synth(ah, channel);
  3361. /*
  3362. * Perform ADC test to see if baseband is ready
  3363. * Set tx hold and check adc test register
  3364. */
  3365. phy_tst1 = ath5k_hw_reg_read(ah, AR5K_PHY_TST1);
  3366. ath5k_hw_reg_write(ah, AR5K_PHY_TST1_TXHOLD, AR5K_PHY_TST1);
  3367. for (i = 0; i <= 20; i++) {
  3368. if (!(ath5k_hw_reg_read(ah, AR5K_PHY_ADC_TEST) & 0x10))
  3369. break;
  3370. usleep_range(200, 250);
  3371. }
  3372. ath5k_hw_reg_write(ah, phy_tst1, AR5K_PHY_TST1);
  3373. /*
  3374. * Start automatic gain control calibration
  3375. *
  3376. * During AGC calibration RX path is re-routed to
  3377. * a power detector so we don't receive anything.
  3378. *
  3379. * This method is used to calibrate some static offsets
  3380. * used together with on-the fly I/Q calibration (the
  3381. * one performed via ath5k_hw_phy_calibrate), which doesn't
  3382. * interrupt rx path.
  3383. *
  3384. * While rx path is re-routed to the power detector we also
  3385. * start a noise floor calibration to measure the
  3386. * card's noise floor (the noise we measure when we are not
  3387. * transmitting or receiving anything).
  3388. *
  3389. * If we are in a noisy environment, AGC calibration may time
  3390. * out and/or noise floor calibration might timeout.
  3391. */
  3392. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  3393. AR5K_PHY_AGCCTL_CAL | AR5K_PHY_AGCCTL_NF);
  3394. /* At the same time start I/Q calibration for QAM constellation
  3395. * -no need for CCK- */
  3396. ah->ah_iq_cal_needed = false;
  3397. if (!(mode == AR5K_MODE_11B)) {
  3398. ah->ah_iq_cal_needed = true;
  3399. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  3400. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  3401. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ,
  3402. AR5K_PHY_IQ_RUN);
  3403. }
  3404. /* Wait for gain calibration to finish (we check for I/Q calibration
  3405. * during ath5k_phy_calibrate) */
  3406. if (ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  3407. AR5K_PHY_AGCCTL_CAL, 0, false)) {
  3408. ATH5K_ERR(ah, "gain calibration timeout (%uMHz)\n",
  3409. channel->center_freq);
  3410. }
  3411. /* Restore antenna mode */
  3412. ath5k_hw_set_antenna_mode(ah, ah->ah_ant_mode);
  3413. return ret;
  3414. }