phy.c 67 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599
  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 = pwrL[0];
  1230. do {
  1231. pwr_i--;
  1232. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1233. pwrL[0], pwrL[1],
  1234. stepL[0], stepL[1]);
  1235. } while (tmp > 1);
  1236. min_pwrL = pwr_i;
  1237. pwr_i = pwrR[0];
  1238. do {
  1239. pwr_i--;
  1240. tmp = (s8) ath5k_get_interpolated_value(pwr_i,
  1241. pwrR[0], pwrR[1],
  1242. stepR[0], stepR[1]);
  1243. } while (tmp > 1);
  1244. min_pwrR = pwr_i;
  1245. /* Keep the right boundary so that it works for both curves */
  1246. return max(min_pwrL, min_pwrR);
  1247. }
  1248. /*
  1249. * Interpolate (pwr,vpd) points to create a Power to PDADC or a
  1250. * Power to PCDAC curve.
  1251. *
  1252. * Each curve has power on x axis (in 0.5dB units) and PCDAC/PDADC
  1253. * steps (offsets) on y axis. Power can go up to 31.5dB and max
  1254. * PCDAC/PDADC step for each curve is 64 but we can write more than
  1255. * one curves on hw so we can go up to 128 (which is the max step we
  1256. * can write on the final table).
  1257. *
  1258. * We write y values (PCDAC/PDADC steps) on hw.
  1259. */
  1260. static void
  1261. ath5k_create_power_curve(s16 pmin, s16 pmax,
  1262. const s16 *pwr, const u8 *vpd,
  1263. u8 num_points,
  1264. u8 *vpd_table, u8 type)
  1265. {
  1266. u8 idx[2] = { 0, 1 };
  1267. s16 pwr_i = 2*pmin;
  1268. int i;
  1269. if (num_points < 2)
  1270. return;
  1271. /* We want the whole line, so adjust boundaries
  1272. * to cover the entire power range. Note that
  1273. * power values are already 0.25dB so no need
  1274. * to multiply pwr_i by 2 */
  1275. if (type == AR5K_PWRTABLE_LINEAR_PCDAC) {
  1276. pwr_i = pmin;
  1277. pmin = 0;
  1278. pmax = 63;
  1279. }
  1280. /* Find surrounding turning points (TPs)
  1281. * and interpolate between them */
  1282. for (i = 0; (i <= (u16) (pmax - pmin)) &&
  1283. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  1284. /* We passed the right TP, move to the next set of TPs
  1285. * if we pass the last TP, extrapolate above using the last
  1286. * two TPs for ratio */
  1287. if ((pwr_i > pwr[idx[1]]) && (idx[1] < num_points - 1)) {
  1288. idx[0]++;
  1289. idx[1]++;
  1290. }
  1291. vpd_table[i] = (u8) ath5k_get_interpolated_value(pwr_i,
  1292. pwr[idx[0]], pwr[idx[1]],
  1293. vpd[idx[0]], vpd[idx[1]]);
  1294. /* Increase by 0.5dB
  1295. * (0.25 dB units) */
  1296. pwr_i += 2;
  1297. }
  1298. }
  1299. /*
  1300. * Get the surrounding per-channel power calibration piers
  1301. * for a given frequency so that we can interpolate between
  1302. * them and come up with an apropriate dataset for our current
  1303. * channel.
  1304. */
  1305. static void
  1306. ath5k_get_chan_pcal_surrounding_piers(struct ath5k_hw *ah,
  1307. struct ieee80211_channel *channel,
  1308. struct ath5k_chan_pcal_info **pcinfo_l,
  1309. struct ath5k_chan_pcal_info **pcinfo_r)
  1310. {
  1311. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1312. struct ath5k_chan_pcal_info *pcinfo;
  1313. u8 idx_l, idx_r;
  1314. u8 mode, max, i;
  1315. u32 target = channel->center_freq;
  1316. idx_l = 0;
  1317. idx_r = 0;
  1318. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1319. pcinfo = ee->ee_pwr_cal_b;
  1320. mode = AR5K_EEPROM_MODE_11B;
  1321. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1322. pcinfo = ee->ee_pwr_cal_g;
  1323. mode = AR5K_EEPROM_MODE_11G;
  1324. } else {
  1325. pcinfo = ee->ee_pwr_cal_a;
  1326. mode = AR5K_EEPROM_MODE_11A;
  1327. }
  1328. max = ee->ee_n_piers[mode] - 1;
  1329. /* Frequency is below our calibrated
  1330. * range. Use the lowest power curve
  1331. * we have */
  1332. if (target < pcinfo[0].freq) {
  1333. idx_l = idx_r = 0;
  1334. goto done;
  1335. }
  1336. /* Frequency is above our calibrated
  1337. * range. Use the highest power curve
  1338. * we have */
  1339. if (target > pcinfo[max].freq) {
  1340. idx_l = idx_r = max;
  1341. goto done;
  1342. }
  1343. /* Frequency is inside our calibrated
  1344. * channel range. Pick the surrounding
  1345. * calibration piers so that we can
  1346. * interpolate */
  1347. for (i = 0; i <= max; i++) {
  1348. /* Frequency matches one of our calibration
  1349. * piers, no need to interpolate, just use
  1350. * that calibration pier */
  1351. if (pcinfo[i].freq == target) {
  1352. idx_l = idx_r = i;
  1353. goto done;
  1354. }
  1355. /* We found a calibration pier that's above
  1356. * frequency, use this pier and the previous
  1357. * one to interpolate */
  1358. if (target < pcinfo[i].freq) {
  1359. idx_r = i;
  1360. idx_l = idx_r - 1;
  1361. goto done;
  1362. }
  1363. }
  1364. done:
  1365. *pcinfo_l = &pcinfo[idx_l];
  1366. *pcinfo_r = &pcinfo[idx_r];
  1367. return;
  1368. }
  1369. /*
  1370. * Get the surrounding per-rate power calibration data
  1371. * for a given frequency and interpolate between power
  1372. * values to set max target power supported by hw for
  1373. * each rate.
  1374. */
  1375. static void
  1376. ath5k_get_rate_pcal_data(struct ath5k_hw *ah,
  1377. struct ieee80211_channel *channel,
  1378. struct ath5k_rate_pcal_info *rates)
  1379. {
  1380. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1381. struct ath5k_rate_pcal_info *rpinfo;
  1382. u8 idx_l, idx_r;
  1383. u8 mode, max, i;
  1384. u32 target = channel->center_freq;
  1385. idx_l = 0;
  1386. idx_r = 0;
  1387. if (!(channel->hw_value & CHANNEL_OFDM)) {
  1388. rpinfo = ee->ee_rate_tpwr_b;
  1389. mode = AR5K_EEPROM_MODE_11B;
  1390. } else if (channel->hw_value & CHANNEL_2GHZ) {
  1391. rpinfo = ee->ee_rate_tpwr_g;
  1392. mode = AR5K_EEPROM_MODE_11G;
  1393. } else {
  1394. rpinfo = ee->ee_rate_tpwr_a;
  1395. mode = AR5K_EEPROM_MODE_11A;
  1396. }
  1397. max = ee->ee_rate_target_pwr_num[mode] - 1;
  1398. /* Get the surrounding calibration
  1399. * piers - same as above */
  1400. if (target < rpinfo[0].freq) {
  1401. idx_l = idx_r = 0;
  1402. goto done;
  1403. }
  1404. if (target > rpinfo[max].freq) {
  1405. idx_l = idx_r = max;
  1406. goto done;
  1407. }
  1408. for (i = 0; i <= max; i++) {
  1409. if (rpinfo[i].freq == target) {
  1410. idx_l = idx_r = i;
  1411. goto done;
  1412. }
  1413. if (target < rpinfo[i].freq) {
  1414. idx_r = i;
  1415. idx_l = idx_r - 1;
  1416. goto done;
  1417. }
  1418. }
  1419. done:
  1420. /* Now interpolate power value, based on the frequency */
  1421. rates->freq = target;
  1422. rates->target_power_6to24 =
  1423. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1424. rpinfo[idx_r].freq,
  1425. rpinfo[idx_l].target_power_6to24,
  1426. rpinfo[idx_r].target_power_6to24);
  1427. rates->target_power_36 =
  1428. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1429. rpinfo[idx_r].freq,
  1430. rpinfo[idx_l].target_power_36,
  1431. rpinfo[idx_r].target_power_36);
  1432. rates->target_power_48 =
  1433. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1434. rpinfo[idx_r].freq,
  1435. rpinfo[idx_l].target_power_48,
  1436. rpinfo[idx_r].target_power_48);
  1437. rates->target_power_54 =
  1438. ath5k_get_interpolated_value(target, rpinfo[idx_l].freq,
  1439. rpinfo[idx_r].freq,
  1440. rpinfo[idx_l].target_power_54,
  1441. rpinfo[idx_r].target_power_54);
  1442. }
  1443. /*
  1444. * Get the max edge power for this channel if
  1445. * we have such data from EEPROM's Conformance Test
  1446. * Limits (CTL), and limit max power if needed.
  1447. *
  1448. * FIXME: Only works for world regulatory domains
  1449. */
  1450. static void
  1451. ath5k_get_max_ctl_power(struct ath5k_hw *ah,
  1452. struct ieee80211_channel *channel)
  1453. {
  1454. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1455. struct ath5k_edge_power *rep = ee->ee_ctl_pwr;
  1456. u8 *ctl_val = ee->ee_ctl;
  1457. s16 max_chan_pwr = ah->ah_txpower.txp_max_pwr / 4;
  1458. s16 edge_pwr = 0;
  1459. u8 rep_idx;
  1460. u8 i, ctl_mode;
  1461. u8 ctl_idx = 0xFF;
  1462. u32 target = channel->center_freq;
  1463. /* Find out a CTL for our mode that's not mapped
  1464. * on a specific reg domain.
  1465. *
  1466. * TODO: Map our current reg domain to one of the 3 available
  1467. * reg domain ids so that we can support more CTLs. */
  1468. switch (channel->hw_value & CHANNEL_MODES) {
  1469. case CHANNEL_A:
  1470. ctl_mode = AR5K_CTL_11A | AR5K_CTL_NO_REGDOMAIN;
  1471. break;
  1472. case CHANNEL_G:
  1473. ctl_mode = AR5K_CTL_11G | AR5K_CTL_NO_REGDOMAIN;
  1474. break;
  1475. case CHANNEL_B:
  1476. ctl_mode = AR5K_CTL_11B | AR5K_CTL_NO_REGDOMAIN;
  1477. break;
  1478. case CHANNEL_T:
  1479. ctl_mode = AR5K_CTL_TURBO | AR5K_CTL_NO_REGDOMAIN;
  1480. break;
  1481. case CHANNEL_TG:
  1482. ctl_mode = AR5K_CTL_TURBOG | AR5K_CTL_NO_REGDOMAIN;
  1483. break;
  1484. case CHANNEL_XR:
  1485. /* Fall through */
  1486. default:
  1487. return;
  1488. }
  1489. for (i = 0; i < ee->ee_ctls; i++) {
  1490. if (ctl_val[i] == ctl_mode) {
  1491. ctl_idx = i;
  1492. break;
  1493. }
  1494. }
  1495. /* If we have a CTL dataset available grab it and find the
  1496. * edge power for our frequency */
  1497. if (ctl_idx == 0xFF)
  1498. return;
  1499. /* Edge powers are sorted by frequency from lower
  1500. * to higher. Each CTL corresponds to 8 edge power
  1501. * measurements. */
  1502. rep_idx = ctl_idx * AR5K_EEPROM_N_EDGES;
  1503. /* Don't do boundaries check because we
  1504. * might have more that one bands defined
  1505. * for this mode */
  1506. /* Get the edge power that's closer to our
  1507. * frequency */
  1508. for (i = 0; i < AR5K_EEPROM_N_EDGES; i++) {
  1509. rep_idx += i;
  1510. if (target <= rep[rep_idx].freq)
  1511. edge_pwr = (s16) rep[rep_idx].edge;
  1512. }
  1513. if (edge_pwr)
  1514. ah->ah_txpower.txp_max_pwr = 4*min(edge_pwr, max_chan_pwr);
  1515. }
  1516. /*
  1517. * Power to PCDAC table functions
  1518. */
  1519. /*
  1520. * Fill Power to PCDAC table on RF5111
  1521. *
  1522. * No further processing is needed for RF5111, the only thing we have to
  1523. * do is fill the values below and above calibration range since eeprom data
  1524. * may not cover the entire PCDAC table.
  1525. */
  1526. static void
  1527. ath5k_fill_pwr_to_pcdac_table(struct ath5k_hw *ah, s16* table_min,
  1528. s16 *table_max)
  1529. {
  1530. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1531. u8 *pcdac_tmp = ah->ah_txpower.tmpL[0];
  1532. u8 pcdac_0, pcdac_n, pcdac_i, pwr_idx, i;
  1533. s16 min_pwr, max_pwr;
  1534. /* Get table boundaries */
  1535. min_pwr = table_min[0];
  1536. pcdac_0 = pcdac_tmp[0];
  1537. max_pwr = table_max[0];
  1538. pcdac_n = pcdac_tmp[table_max[0] - table_min[0]];
  1539. /* Extrapolate below minimum using pcdac_0 */
  1540. pcdac_i = 0;
  1541. for (i = 0; i < min_pwr; i++)
  1542. pcdac_out[pcdac_i++] = pcdac_0;
  1543. /* Copy values from pcdac_tmp */
  1544. pwr_idx = min_pwr;
  1545. for (i = 0 ; pwr_idx <= max_pwr &&
  1546. pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE; i++) {
  1547. pcdac_out[pcdac_i++] = pcdac_tmp[i];
  1548. pwr_idx++;
  1549. }
  1550. /* Extrapolate above maximum */
  1551. while (pcdac_i < AR5K_EEPROM_POWER_TABLE_SIZE)
  1552. pcdac_out[pcdac_i++] = pcdac_n;
  1553. }
  1554. /*
  1555. * Combine available XPD Curves and fill Linear Power to PCDAC table
  1556. * on RF5112
  1557. *
  1558. * RFX112 can have up to 2 curves (one for low txpower range and one for
  1559. * higher txpower range). We need to put them both on pcdac_out and place
  1560. * them in the correct location. In case we only have one curve available
  1561. * just fit it on pcdac_out (it's supposed to cover the entire range of
  1562. * available pwr levels since it's always the higher power curve). Extrapolate
  1563. * below and above final table if needed.
  1564. */
  1565. static void
  1566. ath5k_combine_linear_pcdac_curves(struct ath5k_hw *ah, s16* table_min,
  1567. s16 *table_max, u8 pdcurves)
  1568. {
  1569. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1570. u8 *pcdac_low_pwr;
  1571. u8 *pcdac_high_pwr;
  1572. u8 *pcdac_tmp;
  1573. u8 pwr;
  1574. s16 max_pwr_idx;
  1575. s16 min_pwr_idx;
  1576. s16 mid_pwr_idx = 0;
  1577. /* Edge flag turs on the 7nth bit on the PCDAC
  1578. * to delcare the higher power curve (force values
  1579. * to be greater than 64). If we only have one curve
  1580. * we don't need to set this, if we have 2 curves and
  1581. * fill the table backwards this can also be used to
  1582. * switch from higher power curve to lower power curve */
  1583. u8 edge_flag;
  1584. int i;
  1585. /* When we have only one curve available
  1586. * that's the higher power curve. If we have
  1587. * two curves the first is the high power curve
  1588. * and the next is the low power curve. */
  1589. if (pdcurves > 1) {
  1590. pcdac_low_pwr = ah->ah_txpower.tmpL[1];
  1591. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  1592. mid_pwr_idx = table_max[1] - table_min[1] - 1;
  1593. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  1594. /* If table size goes beyond 31.5dB, keep the
  1595. * upper 31.5dB range when setting tx power.
  1596. * Note: 126 = 31.5 dB in quarter dB steps */
  1597. if (table_max[0] - table_min[1] > 126)
  1598. min_pwr_idx = table_max[0] - 126;
  1599. else
  1600. min_pwr_idx = table_min[1];
  1601. /* Since we fill table backwards
  1602. * start from high power curve */
  1603. pcdac_tmp = pcdac_high_pwr;
  1604. edge_flag = 0x40;
  1605. #if 0
  1606. /* If both min and max power limits are in lower
  1607. * power curve's range, only use the low power curve.
  1608. * TODO: min/max levels are related to target
  1609. * power values requested from driver/user
  1610. * XXX: Is this really needed ? */
  1611. if (min_pwr < table_max[1] &&
  1612. max_pwr < table_max[1]) {
  1613. edge_flag = 0;
  1614. pcdac_tmp = pcdac_low_pwr;
  1615. max_pwr_idx = (table_max[1] - table_min[1])/2;
  1616. }
  1617. #endif
  1618. } else {
  1619. pcdac_low_pwr = ah->ah_txpower.tmpL[1]; /* Zeroed */
  1620. pcdac_high_pwr = ah->ah_txpower.tmpL[0];
  1621. min_pwr_idx = table_min[0];
  1622. max_pwr_idx = (table_max[0] - table_min[0]) / 2;
  1623. pcdac_tmp = pcdac_high_pwr;
  1624. edge_flag = 0;
  1625. }
  1626. /* This is used when setting tx power*/
  1627. ah->ah_txpower.txp_min_idx = min_pwr_idx/2;
  1628. /* Fill Power to PCDAC table backwards */
  1629. pwr = max_pwr_idx;
  1630. for (i = 63; i >= 0; i--) {
  1631. /* Entering lower power range, reset
  1632. * edge flag and set pcdac_tmp to lower
  1633. * power curve.*/
  1634. if (edge_flag == 0x40 &&
  1635. (2*pwr <= (table_max[1] - table_min[0]) || pwr == 0)) {
  1636. edge_flag = 0x00;
  1637. pcdac_tmp = pcdac_low_pwr;
  1638. pwr = mid_pwr_idx/2;
  1639. }
  1640. /* Don't go below 1, extrapolate below if we have
  1641. * already swithced to the lower power curve -or
  1642. * we only have one curve and edge_flag is zero
  1643. * anyway */
  1644. if (pcdac_tmp[pwr] < 1 && (edge_flag == 0x00)) {
  1645. while (i >= 0) {
  1646. pcdac_out[i] = pcdac_out[i + 1];
  1647. i--;
  1648. }
  1649. break;
  1650. }
  1651. pcdac_out[i] = pcdac_tmp[pwr] | edge_flag;
  1652. /* Extrapolate above if pcdac is greater than
  1653. * 126 -this can happen because we OR pcdac_out
  1654. * value with edge_flag on high power curve */
  1655. if (pcdac_out[i] > 126)
  1656. pcdac_out[i] = 126;
  1657. /* Decrease by a 0.5dB step */
  1658. pwr--;
  1659. }
  1660. }
  1661. /* Write PCDAC values on hw */
  1662. static void
  1663. ath5k_setup_pcdac_table(struct ath5k_hw *ah)
  1664. {
  1665. u8 *pcdac_out = ah->ah_txpower.txp_pd_table;
  1666. int i;
  1667. /*
  1668. * Write TX power values
  1669. */
  1670. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1671. ath5k_hw_reg_write(ah,
  1672. (((pcdac_out[2*i + 0] << 8 | 0xff) & 0xffff) << 0) |
  1673. (((pcdac_out[2*i + 1] << 8 | 0xff) & 0xffff) << 16),
  1674. AR5K_PHY_PCDAC_TXPOWER(i));
  1675. }
  1676. }
  1677. /*
  1678. * Power to PDADC table functions
  1679. */
  1680. /*
  1681. * Set the gain boundaries and create final Power to PDADC table
  1682. *
  1683. * We can have up to 4 pd curves, we need to do a simmilar process
  1684. * as we do for RF5112. This time we don't have an edge_flag but we
  1685. * set the gain boundaries on a separate register.
  1686. */
  1687. static void
  1688. ath5k_combine_pwr_to_pdadc_curves(struct ath5k_hw *ah,
  1689. s16 *pwr_min, s16 *pwr_max, u8 pdcurves)
  1690. {
  1691. u8 gain_boundaries[AR5K_EEPROM_N_PD_GAINS];
  1692. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  1693. u8 *pdadc_tmp;
  1694. s16 pdadc_0;
  1695. u8 pdadc_i, pdadc_n, pwr_step, pdg, max_idx, table_size;
  1696. u8 pd_gain_overlap;
  1697. /* Note: Register value is initialized on initvals
  1698. * there is no feedback from hw.
  1699. * XXX: What about pd_gain_overlap from EEPROM ? */
  1700. pd_gain_overlap = (u8) ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG5) &
  1701. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP;
  1702. /* Create final PDADC table */
  1703. for (pdg = 0, pdadc_i = 0; pdg < pdcurves; pdg++) {
  1704. pdadc_tmp = ah->ah_txpower.tmpL[pdg];
  1705. if (pdg == pdcurves - 1)
  1706. /* 2 dB boundary stretch for last
  1707. * (higher power) curve */
  1708. gain_boundaries[pdg] = pwr_max[pdg] + 4;
  1709. else
  1710. /* Set gain boundary in the middle
  1711. * between this curve and the next one */
  1712. gain_boundaries[pdg] =
  1713. (pwr_max[pdg] + pwr_min[pdg + 1]) / 2;
  1714. /* Sanity check in case our 2 db stretch got out of
  1715. * range. */
  1716. if (gain_boundaries[pdg] > AR5K_TUNE_MAX_TXPOWER)
  1717. gain_boundaries[pdg] = AR5K_TUNE_MAX_TXPOWER;
  1718. /* For the first curve (lower power)
  1719. * start from 0 dB */
  1720. if (pdg == 0)
  1721. pdadc_0 = 0;
  1722. else
  1723. /* For the other curves use the gain overlap */
  1724. pdadc_0 = (gain_boundaries[pdg - 1] - pwr_min[pdg]) -
  1725. pd_gain_overlap;
  1726. /* Force each power step to be at least 0.5 dB */
  1727. if ((pdadc_tmp[1] - pdadc_tmp[0]) > 1)
  1728. pwr_step = pdadc_tmp[1] - pdadc_tmp[0];
  1729. else
  1730. pwr_step = 1;
  1731. /* If pdadc_0 is negative, we need to extrapolate
  1732. * below this pdgain by a number of pwr_steps */
  1733. while ((pdadc_0 < 0) && (pdadc_i < 128)) {
  1734. s16 tmp = pdadc_tmp[0] + pdadc_0 * pwr_step;
  1735. pdadc_out[pdadc_i++] = (tmp < 0) ? 0 : (u8) tmp;
  1736. pdadc_0++;
  1737. }
  1738. /* Set last pwr level, using gain boundaries */
  1739. pdadc_n = gain_boundaries[pdg] + pd_gain_overlap - pwr_min[pdg];
  1740. /* Limit it to be inside pwr range */
  1741. table_size = pwr_max[pdg] - pwr_min[pdg];
  1742. max_idx = (pdadc_n < table_size) ? pdadc_n : table_size;
  1743. /* Fill pdadc_out table */
  1744. while (pdadc_0 < max_idx)
  1745. pdadc_out[pdadc_i++] = pdadc_tmp[pdadc_0++];
  1746. /* Need to extrapolate above this pdgain? */
  1747. if (pdadc_n <= max_idx)
  1748. continue;
  1749. /* Force each power step to be at least 0.5 dB */
  1750. if ((pdadc_tmp[table_size - 1] - pdadc_tmp[table_size - 2]) > 1)
  1751. pwr_step = pdadc_tmp[table_size - 1] -
  1752. pdadc_tmp[table_size - 2];
  1753. else
  1754. pwr_step = 1;
  1755. /* Extrapolate above */
  1756. while ((pdadc_0 < (s16) pdadc_n) &&
  1757. (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2)) {
  1758. s16 tmp = pdadc_tmp[table_size - 1] +
  1759. (pdadc_0 - max_idx) * pwr_step;
  1760. pdadc_out[pdadc_i++] = (tmp > 127) ? 127 : (u8) tmp;
  1761. pdadc_0++;
  1762. }
  1763. }
  1764. while (pdg < AR5K_EEPROM_N_PD_GAINS) {
  1765. gain_boundaries[pdg] = gain_boundaries[pdg - 1];
  1766. pdg++;
  1767. }
  1768. while (pdadc_i < AR5K_EEPROM_POWER_TABLE_SIZE * 2) {
  1769. pdadc_out[pdadc_i] = pdadc_out[pdadc_i - 1];
  1770. pdadc_i++;
  1771. }
  1772. /* Set gain boundaries */
  1773. ath5k_hw_reg_write(ah,
  1774. AR5K_REG_SM(pd_gain_overlap,
  1775. AR5K_PHY_TPC_RG5_PD_GAIN_OVERLAP) |
  1776. AR5K_REG_SM(gain_boundaries[0],
  1777. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_1) |
  1778. AR5K_REG_SM(gain_boundaries[1],
  1779. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_2) |
  1780. AR5K_REG_SM(gain_boundaries[2],
  1781. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_3) |
  1782. AR5K_REG_SM(gain_boundaries[3],
  1783. AR5K_PHY_TPC_RG5_PD_GAIN_BOUNDARY_4),
  1784. AR5K_PHY_TPC_RG5);
  1785. /* Used for setting rate power table */
  1786. ah->ah_txpower.txp_min_idx = pwr_min[0];
  1787. }
  1788. /* Write PDADC values on hw */
  1789. static void
  1790. ath5k_setup_pwr_to_pdadc_table(struct ath5k_hw *ah,
  1791. u8 pdcurves, u8 *pdg_to_idx)
  1792. {
  1793. u8 *pdadc_out = ah->ah_txpower.txp_pd_table;
  1794. u32 reg;
  1795. u8 i;
  1796. /* Select the right pdgain curves */
  1797. /* Clear current settings */
  1798. reg = ath5k_hw_reg_read(ah, AR5K_PHY_TPC_RG1);
  1799. reg &= ~(AR5K_PHY_TPC_RG1_PDGAIN_1 |
  1800. AR5K_PHY_TPC_RG1_PDGAIN_2 |
  1801. AR5K_PHY_TPC_RG1_PDGAIN_3 |
  1802. AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  1803. /*
  1804. * Use pd_gains curve from eeprom
  1805. *
  1806. * This overrides the default setting from initvals
  1807. * in case some vendors (e.g. Zcomax) don't use the default
  1808. * curves. If we don't honor their settings we 'll get a
  1809. * 5dB (1 * gain overlap ?) drop.
  1810. */
  1811. reg |= AR5K_REG_SM(pdcurves, AR5K_PHY_TPC_RG1_NUM_PD_GAIN);
  1812. switch (pdcurves) {
  1813. case 3:
  1814. reg |= AR5K_REG_SM(pdg_to_idx[2], AR5K_PHY_TPC_RG1_PDGAIN_3);
  1815. /* Fall through */
  1816. case 2:
  1817. reg |= AR5K_REG_SM(pdg_to_idx[1], AR5K_PHY_TPC_RG1_PDGAIN_2);
  1818. /* Fall through */
  1819. case 1:
  1820. reg |= AR5K_REG_SM(pdg_to_idx[0], AR5K_PHY_TPC_RG1_PDGAIN_1);
  1821. break;
  1822. }
  1823. ath5k_hw_reg_write(ah, reg, AR5K_PHY_TPC_RG1);
  1824. /*
  1825. * Write TX power values
  1826. */
  1827. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1828. ath5k_hw_reg_write(ah,
  1829. ((pdadc_out[4*i + 0] & 0xff) << 0) |
  1830. ((pdadc_out[4*i + 1] & 0xff) << 8) |
  1831. ((pdadc_out[4*i + 2] & 0xff) << 16) |
  1832. ((pdadc_out[4*i + 3] & 0xff) << 24),
  1833. AR5K_PHY_PDADC_TXPOWER(i));
  1834. }
  1835. }
  1836. /*
  1837. * Common code for PCDAC/PDADC tables
  1838. */
  1839. /*
  1840. * This is the main function that uses all of the above
  1841. * to set PCDAC/PDADC table on hw for the current channel.
  1842. * This table is used for tx power calibration on the basband,
  1843. * without it we get weird tx power levels and in some cases
  1844. * distorted spectral mask
  1845. */
  1846. static int
  1847. ath5k_setup_channel_powertable(struct ath5k_hw *ah,
  1848. struct ieee80211_channel *channel,
  1849. u8 ee_mode, u8 type)
  1850. {
  1851. struct ath5k_pdgain_info *pdg_L, *pdg_R;
  1852. struct ath5k_chan_pcal_info *pcinfo_L;
  1853. struct ath5k_chan_pcal_info *pcinfo_R;
  1854. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  1855. u8 *pdg_curve_to_idx = ee->ee_pdc_to_idx[ee_mode];
  1856. s16 table_min[AR5K_EEPROM_N_PD_GAINS];
  1857. s16 table_max[AR5K_EEPROM_N_PD_GAINS];
  1858. u8 *tmpL;
  1859. u8 *tmpR;
  1860. u32 target = channel->center_freq;
  1861. int pdg, i;
  1862. /* Get surounding freq piers for this channel */
  1863. ath5k_get_chan_pcal_surrounding_piers(ah, channel,
  1864. &pcinfo_L,
  1865. &pcinfo_R);
  1866. /* Loop over pd gain curves on
  1867. * surounding freq piers by index */
  1868. for (pdg = 0; pdg < ee->ee_pd_gains[ee_mode]; pdg++) {
  1869. /* Fill curves in reverse order
  1870. * from lower power (max gain)
  1871. * to higher power. Use curve -> idx
  1872. * backmaping we did on eeprom init */
  1873. u8 idx = pdg_curve_to_idx[pdg];
  1874. /* Grab the needed curves by index */
  1875. pdg_L = &pcinfo_L->pd_curves[idx];
  1876. pdg_R = &pcinfo_R->pd_curves[idx];
  1877. /* Initialize the temp tables */
  1878. tmpL = ah->ah_txpower.tmpL[pdg];
  1879. tmpR = ah->ah_txpower.tmpR[pdg];
  1880. /* Set curve's x boundaries and create
  1881. * curves so that they cover the same
  1882. * range (if we don't do that one table
  1883. * will have values on some range and the
  1884. * other one won't have any so interpolation
  1885. * will fail) */
  1886. table_min[pdg] = min(pdg_L->pd_pwr[0],
  1887. pdg_R->pd_pwr[0]) / 2;
  1888. table_max[pdg] = max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  1889. pdg_R->pd_pwr[pdg_R->pd_points - 1]) / 2;
  1890. /* Now create the curves on surrounding channels
  1891. * and interpolate if needed to get the final
  1892. * curve for this gain on this channel */
  1893. switch (type) {
  1894. case AR5K_PWRTABLE_LINEAR_PCDAC:
  1895. /* Override min/max so that we don't loose
  1896. * accuracy (don't divide by 2) */
  1897. table_min[pdg] = min(pdg_L->pd_pwr[0],
  1898. pdg_R->pd_pwr[0]);
  1899. table_max[pdg] =
  1900. max(pdg_L->pd_pwr[pdg_L->pd_points - 1],
  1901. pdg_R->pd_pwr[pdg_R->pd_points - 1]);
  1902. /* Override minimum so that we don't get
  1903. * out of bounds while extrapolating
  1904. * below. Don't do this when we have 2
  1905. * curves and we are on the high power curve
  1906. * because table_min is ok in this case */
  1907. if (!(ee->ee_pd_gains[ee_mode] > 1 && pdg == 0)) {
  1908. table_min[pdg] =
  1909. ath5k_get_linear_pcdac_min(pdg_L->pd_step,
  1910. pdg_R->pd_step,
  1911. pdg_L->pd_pwr,
  1912. pdg_R->pd_pwr);
  1913. /* Don't go too low because we will
  1914. * miss the upper part of the curve.
  1915. * Note: 126 = 31.5dB (max power supported)
  1916. * in 0.25dB units */
  1917. if (table_max[pdg] - table_min[pdg] > 126)
  1918. table_min[pdg] = table_max[pdg] - 126;
  1919. }
  1920. /* Fall through */
  1921. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  1922. case AR5K_PWRTABLE_PWR_TO_PDADC:
  1923. ath5k_create_power_curve(table_min[pdg],
  1924. table_max[pdg],
  1925. pdg_L->pd_pwr,
  1926. pdg_L->pd_step,
  1927. pdg_L->pd_points, tmpL, type);
  1928. /* We are in a calibration
  1929. * pier, no need to interpolate
  1930. * between freq piers */
  1931. if (pcinfo_L == pcinfo_R)
  1932. continue;
  1933. ath5k_create_power_curve(table_min[pdg],
  1934. table_max[pdg],
  1935. pdg_R->pd_pwr,
  1936. pdg_R->pd_step,
  1937. pdg_R->pd_points, tmpR, type);
  1938. break;
  1939. default:
  1940. return -EINVAL;
  1941. }
  1942. /* Interpolate between curves
  1943. * of surounding freq piers to
  1944. * get the final curve for this
  1945. * pd gain. Re-use tmpL for interpolation
  1946. * output */
  1947. for (i = 0; (i < (u16) (table_max[pdg] - table_min[pdg])) &&
  1948. (i < AR5K_EEPROM_POWER_TABLE_SIZE); i++) {
  1949. tmpL[i] = (u8) ath5k_get_interpolated_value(target,
  1950. (s16) pcinfo_L->freq,
  1951. (s16) pcinfo_R->freq,
  1952. (s16) tmpL[i],
  1953. (s16) tmpR[i]);
  1954. }
  1955. }
  1956. /* Now we have a set of curves for this
  1957. * channel on tmpL (x range is table_max - table_min
  1958. * and y values are tmpL[pdg][]) sorted in the same
  1959. * order as EEPROM (because we've used the backmaping).
  1960. * So for RF5112 it's from higher power to lower power
  1961. * and for RF2413 it's from lower power to higher power.
  1962. * For RF5111 we only have one curve. */
  1963. /* Fill min and max power levels for this
  1964. * channel by interpolating the values on
  1965. * surounding channels to complete the dataset */
  1966. ah->ah_txpower.txp_min_pwr = ath5k_get_interpolated_value(target,
  1967. (s16) pcinfo_L->freq,
  1968. (s16) pcinfo_R->freq,
  1969. pcinfo_L->min_pwr, pcinfo_R->min_pwr);
  1970. ah->ah_txpower.txp_max_pwr = ath5k_get_interpolated_value(target,
  1971. (s16) pcinfo_L->freq,
  1972. (s16) pcinfo_R->freq,
  1973. pcinfo_L->max_pwr, pcinfo_R->max_pwr);
  1974. /* We are ready to go, fill PCDAC/PDADC
  1975. * table and write settings on hardware */
  1976. switch (type) {
  1977. case AR5K_PWRTABLE_LINEAR_PCDAC:
  1978. /* For RF5112 we can have one or two curves
  1979. * and each curve covers a certain power lvl
  1980. * range so we need to do some more processing */
  1981. ath5k_combine_linear_pcdac_curves(ah, table_min, table_max,
  1982. ee->ee_pd_gains[ee_mode]);
  1983. /* Set txp.offset so that we can
  1984. * match max power value with max
  1985. * table index */
  1986. ah->ah_txpower.txp_offset = 64 - (table_max[0] / 2);
  1987. /* Write settings on hw */
  1988. ath5k_setup_pcdac_table(ah);
  1989. break;
  1990. case AR5K_PWRTABLE_PWR_TO_PCDAC:
  1991. /* We are done for RF5111 since it has only
  1992. * one curve, just fit the curve on the table */
  1993. ath5k_fill_pwr_to_pcdac_table(ah, table_min, table_max);
  1994. /* No rate powertable adjustment for RF5111 */
  1995. ah->ah_txpower.txp_min_idx = 0;
  1996. ah->ah_txpower.txp_offset = 0;
  1997. /* Write settings on hw */
  1998. ath5k_setup_pcdac_table(ah);
  1999. break;
  2000. case AR5K_PWRTABLE_PWR_TO_PDADC:
  2001. /* Set PDADC boundaries and fill
  2002. * final PDADC table */
  2003. ath5k_combine_pwr_to_pdadc_curves(ah, table_min, table_max,
  2004. ee->ee_pd_gains[ee_mode]);
  2005. /* Write settings on hw */
  2006. ath5k_setup_pwr_to_pdadc_table(ah, pdg, pdg_curve_to_idx);
  2007. /* Set txp.offset, note that table_min
  2008. * can be negative */
  2009. ah->ah_txpower.txp_offset = table_min[0];
  2010. break;
  2011. default:
  2012. return -EINVAL;
  2013. }
  2014. return 0;
  2015. }
  2016. /*
  2017. * Per-rate tx power setting
  2018. *
  2019. * This is the code that sets the desired tx power (below
  2020. * maximum) on hw for each rate (we also have TPC that sets
  2021. * power per packet). We do that by providing an index on the
  2022. * PCDAC/PDADC table we set up.
  2023. */
  2024. /*
  2025. * Set rate power table
  2026. *
  2027. * For now we only limit txpower based on maximum tx power
  2028. * supported by hw (what's inside rate_info). We need to limit
  2029. * this even more, based on regulatory domain etc.
  2030. *
  2031. * Rate power table contains indices to PCDAC/PDADC table (0.5dB steps)
  2032. * and is indexed as follows:
  2033. * rates[0] - rates[7] -> OFDM rates
  2034. * rates[8] - rates[14] -> CCK rates
  2035. * rates[15] -> XR rates (they all have the same power)
  2036. */
  2037. static void
  2038. ath5k_setup_rate_powertable(struct ath5k_hw *ah, u16 max_pwr,
  2039. struct ath5k_rate_pcal_info *rate_info,
  2040. u8 ee_mode)
  2041. {
  2042. unsigned int i;
  2043. u16 *rates;
  2044. /* max_pwr is power level we got from driver/user in 0.5dB
  2045. * units, switch to 0.25dB units so we can compare */
  2046. max_pwr *= 2;
  2047. max_pwr = min(max_pwr, (u16) ah->ah_txpower.txp_max_pwr) / 2;
  2048. /* apply rate limits */
  2049. rates = ah->ah_txpower.txp_rates_power_table;
  2050. /* OFDM rates 6 to 24Mb/s */
  2051. for (i = 0; i < 5; i++)
  2052. rates[i] = min(max_pwr, rate_info->target_power_6to24);
  2053. /* Rest OFDM rates */
  2054. rates[5] = min(rates[0], rate_info->target_power_36);
  2055. rates[6] = min(rates[0], rate_info->target_power_48);
  2056. rates[7] = min(rates[0], rate_info->target_power_54);
  2057. /* CCK rates */
  2058. /* 1L */
  2059. rates[8] = min(rates[0], rate_info->target_power_6to24);
  2060. /* 2L */
  2061. rates[9] = min(rates[0], rate_info->target_power_36);
  2062. /* 2S */
  2063. rates[10] = min(rates[0], rate_info->target_power_36);
  2064. /* 5L */
  2065. rates[11] = min(rates[0], rate_info->target_power_48);
  2066. /* 5S */
  2067. rates[12] = min(rates[0], rate_info->target_power_48);
  2068. /* 11L */
  2069. rates[13] = min(rates[0], rate_info->target_power_54);
  2070. /* 11S */
  2071. rates[14] = min(rates[0], rate_info->target_power_54);
  2072. /* XR rates */
  2073. rates[15] = min(rates[0], rate_info->target_power_6to24);
  2074. /* CCK rates have different peak to average ratio
  2075. * so we have to tweak their power so that gainf
  2076. * correction works ok. For this we use OFDM to
  2077. * CCK delta from eeprom */
  2078. if ((ee_mode == AR5K_EEPROM_MODE_11G) &&
  2079. (ah->ah_phy_revision < AR5K_SREV_PHY_5212A))
  2080. for (i = 8; i <= 15; i++)
  2081. rates[i] -= ah->ah_txpower.txp_cck_ofdm_gainf_delta;
  2082. ah->ah_txpower.txp_min_pwr = rates[7];
  2083. ah->ah_txpower.txp_max_pwr = rates[0];
  2084. ah->ah_txpower.txp_ofdm = rates[7];
  2085. }
  2086. /*
  2087. * Set transmition power
  2088. */
  2089. int
  2090. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  2091. u8 ee_mode, u8 txpower)
  2092. {
  2093. struct ath5k_rate_pcal_info rate_info;
  2094. u8 type;
  2095. int ret;
  2096. ATH5K_TRACE(ah->ah_sc);
  2097. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  2098. ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
  2099. return -EINVAL;
  2100. }
  2101. if (txpower == 0)
  2102. txpower = AR5K_TUNE_DEFAULT_TXPOWER;
  2103. /* Reset TX power values */
  2104. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  2105. ah->ah_txpower.txp_tpc = AR5K_TUNE_TPC_TXPOWER;
  2106. ah->ah_txpower.txp_min_pwr = 0;
  2107. ah->ah_txpower.txp_max_pwr = AR5K_TUNE_MAX_TXPOWER;
  2108. /* Initialize TX power table */
  2109. switch (ah->ah_radio) {
  2110. case AR5K_RF5111:
  2111. type = AR5K_PWRTABLE_PWR_TO_PCDAC;
  2112. break;
  2113. case AR5K_RF5112:
  2114. type = AR5K_PWRTABLE_LINEAR_PCDAC;
  2115. break;
  2116. case AR5K_RF2413:
  2117. case AR5K_RF5413:
  2118. case AR5K_RF2316:
  2119. case AR5K_RF2317:
  2120. case AR5K_RF2425:
  2121. type = AR5K_PWRTABLE_PWR_TO_PDADC;
  2122. break;
  2123. default:
  2124. return -EINVAL;
  2125. }
  2126. /* FIXME: Only on channel/mode change */
  2127. ret = ath5k_setup_channel_powertable(ah, channel, ee_mode, type);
  2128. if (ret)
  2129. return ret;
  2130. /* Limit max power if we have a CTL available */
  2131. ath5k_get_max_ctl_power(ah, channel);
  2132. /* FIXME: Tx power limit for this regdomain
  2133. * XXX: Mac80211/CRDA will do that anyway ? */
  2134. /* FIXME: Antenna reduction stuff */
  2135. /* FIXME: Limit power on turbo modes */
  2136. /* FIXME: TPC scale reduction */
  2137. /* Get surounding channels for per-rate power table
  2138. * calibration */
  2139. ath5k_get_rate_pcal_data(ah, channel, &rate_info);
  2140. /* Setup rate power table */
  2141. ath5k_setup_rate_powertable(ah, txpower, &rate_info, ee_mode);
  2142. /* Write rate power table on hw */
  2143. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  2144. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  2145. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  2146. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  2147. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  2148. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  2149. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  2150. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  2151. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  2152. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  2153. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  2154. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  2155. /* FIXME: TPC support */
  2156. if (ah->ah_txpower.txp_tpc) {
  2157. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  2158. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2159. ath5k_hw_reg_write(ah,
  2160. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_ACK) |
  2161. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CTS) |
  2162. AR5K_REG_MS(AR5K_TUNE_MAX_TXPOWER, AR5K_TPC_CHIRP),
  2163. AR5K_TPC);
  2164. } else {
  2165. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
  2166. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  2167. }
  2168. return 0;
  2169. }
  2170. int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, u8 mode, u8 txpower)
  2171. {
  2172. /*Just a try M.F.*/
  2173. struct ieee80211_channel *channel = &ah->ah_current_channel;
  2174. ATH5K_TRACE(ah->ah_sc);
  2175. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
  2176. "changing txpower to %d\n", txpower);
  2177. return ath5k_hw_txpower(ah, channel, mode, txpower);
  2178. }
  2179. #undef _ATH5K_PHY