phy.c 67 KB

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