phy.c 91 KB

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