phy.c 90 KB

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