phy.c 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536
  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. *
  8. * Permission to use, copy, modify, and distribute this software for any
  9. * purpose with or without fee is hereby granted, provided that the above
  10. * copyright notice and this permission notice appear in all copies.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  13. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  14. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  15. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  16. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  17. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  18. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  19. *
  20. */
  21. #define _ATH5K_PHY
  22. #include <linux/delay.h>
  23. #include "ath5k.h"
  24. #include "reg.h"
  25. #include "base.h"
  26. #include "rfbuffer.h"
  27. #include "rfgain.h"
  28. /*
  29. * Used to modify RF Banks before writing them to AR5K_RF_BUFFER
  30. */
  31. static unsigned int ath5k_hw_rfregs_op(u32 *rf, u32 offset, u32 reg, u32 bits,
  32. u32 first, u32 col, bool set)
  33. {
  34. u32 mask, entry, last, data, shift, position;
  35. s32 left;
  36. int i;
  37. data = 0;
  38. if (rf == NULL)
  39. /* should not happen */
  40. return 0;
  41. if (!(col <= 3 && bits <= 32 && first + bits <= 319)) {
  42. ATH5K_PRINTF("invalid values at offset %u\n", offset);
  43. return 0;
  44. }
  45. entry = ((first - 1) / 8) + offset;
  46. position = (first - 1) % 8;
  47. if (set)
  48. data = ath5k_hw_bitswap(reg, bits);
  49. for (i = shift = 0, left = bits; left > 0; position = 0, entry++, i++) {
  50. last = (position + left > 8) ? 8 : position + left;
  51. mask = (((1 << last) - 1) ^ ((1 << position) - 1)) << (col * 8);
  52. if (set) {
  53. rf[entry] &= ~mask;
  54. rf[entry] |= ((data << position) << (col * 8)) & mask;
  55. data >>= (8 - position);
  56. } else {
  57. data = (((rf[entry] & mask) >> (col * 8)) >> position)
  58. << shift;
  59. shift += last - position;
  60. }
  61. left -= 8 - position;
  62. }
  63. data = set ? 1 : ath5k_hw_bitswap(data, bits);
  64. return data;
  65. }
  66. /**********************\
  67. * RF Gain optimization *
  68. \**********************/
  69. /*
  70. * This code is used to optimize rf gain on different environments
  71. * (temprature mostly) based on feedback from a power detector.
  72. *
  73. * It's only used on RF5111 and RF5112, later RF chips seem to have
  74. * auto adjustment on hw -notice they have a much smaller BANK 7 and
  75. * no gain optimization ladder-.
  76. *
  77. * For more infos check out this patent doc
  78. * http://www.freepatentsonline.com/7400691.html
  79. *
  80. * This paper describes power drops as seen on the receiver due to
  81. * probe packets
  82. * http://www.cnri.dit.ie/publications/ICT08%20-%20Practical%20Issues
  83. * %20of%20Power%20Control.pdf
  84. *
  85. * And this is the MadWiFi bug entry related to the above
  86. * http://madwifi-project.org/ticket/1659
  87. * with various measurements and diagrams
  88. *
  89. * TODO: Deal with power drops due to probes by setting an apropriate
  90. * tx power on the probe packets ! Make this part of the calibration process.
  91. */
  92. /* Initialize ah_gain durring attach */
  93. int ath5k_hw_rfgain_opt_init(struct ath5k_hw *ah)
  94. {
  95. /* Initialize the gain optimization values */
  96. switch (ah->ah_radio) {
  97. case AR5K_RF5111:
  98. ah->ah_gain.g_step_idx = rfgain_opt_5111.go_default;
  99. ah->ah_gain.g_low = 20;
  100. ah->ah_gain.g_high = 35;
  101. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  102. break;
  103. case AR5K_RF5112:
  104. ah->ah_gain.g_step_idx = rfgain_opt_5112.go_default;
  105. ah->ah_gain.g_low = 20;
  106. ah->ah_gain.g_high = 85;
  107. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  108. break;
  109. default:
  110. return -EINVAL;
  111. }
  112. return 0;
  113. }
  114. /* Schedule a gain probe check on the next transmited packet.
  115. * That means our next packet is going to be sent with lower
  116. * tx power and a Peak to Average Power Detector (PAPD) will try
  117. * to measure the gain.
  118. *
  119. * TODO: Use propper tx power setting for the probe packet so
  120. * that we don't observe a serious power drop on the receiver
  121. *
  122. * XXX: How about forcing a tx packet (bypassing PCU arbitrator etc)
  123. * just after we enable the probe so that we don't mess with
  124. * standard traffic ? Maybe it's time to use sw interrupts and
  125. * a probe tasklet !!!
  126. */
  127. static void ath5k_hw_request_rfgain_probe(struct ath5k_hw *ah)
  128. {
  129. /* Skip if gain calibration is inactive or
  130. * we already handle a probe request */
  131. if (ah->ah_gain.g_state != AR5K_RFGAIN_ACTIVE)
  132. return;
  133. ath5k_hw_reg_write(ah, AR5K_REG_SM(ah->ah_txpower.txp_max,
  134. AR5K_PHY_PAPD_PROBE_TXPOWER) |
  135. AR5K_PHY_PAPD_PROBE_TX_NEXT, AR5K_PHY_PAPD_PROBE);
  136. ah->ah_gain.g_state = AR5K_RFGAIN_READ_REQUESTED;
  137. }
  138. /* Calculate gain_F measurement correction
  139. * based on the current step for RF5112 rev. 2 */
  140. static u32 ath5k_hw_rf_gainf_corr(struct ath5k_hw *ah)
  141. {
  142. u32 mix, step;
  143. u32 *rf;
  144. const struct ath5k_gain_opt *go;
  145. const struct ath5k_gain_opt_step *g_step;
  146. /* Only RF5112 Rev. 2 supports it */
  147. if ((ah->ah_radio != AR5K_RF5112) ||
  148. (ah->ah_radio_5ghz_revision <= AR5K_SREV_RAD_5112A))
  149. return 0;
  150. go = &rfgain_opt_5112;
  151. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  152. if (ah->ah_rf_banks == NULL)
  153. return 0;
  154. rf = ah->ah_rf_banks;
  155. ah->ah_gain.g_f_corr = 0;
  156. /* No VGA (Variable Gain Amplifier) override, skip */
  157. if (ath5k_hw_rfregs_op(rf, ah->ah_offset[7], 0, 1, 36, 0, false) != 1)
  158. return 0;
  159. /* Mix gain stepping */
  160. step = ath5k_hw_rfregs_op(rf, ah->ah_offset[7], 0, 4, 32, 0, false);
  161. /* Mix gain override */
  162. mix = g_step->gos_param[0];
  163. switch (mix) {
  164. case 3:
  165. ah->ah_gain.g_f_corr = step * 2;
  166. break;
  167. case 2:
  168. ah->ah_gain.g_f_corr = (step - 5) * 2;
  169. break;
  170. case 1:
  171. ah->ah_gain.g_f_corr = step;
  172. break;
  173. default:
  174. ah->ah_gain.g_f_corr = 0;
  175. break;
  176. }
  177. return ah->ah_gain.g_f_corr;
  178. }
  179. /* Check if current gain_F measurement is in the range of our
  180. * power detector windows. If we get a measurement outside range
  181. * we know it's not accurate (detectors can't measure anything outside
  182. * their detection window) so we must ignore it */
  183. static bool ath5k_hw_rf_check_gainf_readback(struct ath5k_hw *ah)
  184. {
  185. u32 step, mix_ovr, level[4];
  186. u32 *rf;
  187. if (ah->ah_rf_banks == NULL)
  188. return false;
  189. rf = ah->ah_rf_banks;
  190. if (ah->ah_radio == AR5K_RF5111) {
  191. step = ath5k_hw_rfregs_op(rf, ah->ah_offset[7], 0, 6, 37, 0,
  192. false);
  193. level[0] = 0;
  194. level[1] = (step == 63) ? 50 : step + 4;
  195. level[2] = (step != 63) ? 64 : level[0];
  196. level[3] = level[2] + 50 ;
  197. ah->ah_gain.g_high = level[3] -
  198. (step == 63 ? AR5K_GAIN_DYN_ADJUST_HI_MARGIN : -5);
  199. ah->ah_gain.g_low = level[0] +
  200. (step == 63 ? AR5K_GAIN_DYN_ADJUST_LO_MARGIN : 0);
  201. } else {
  202. mix_ovr = ath5k_hw_rfregs_op(rf, ah->ah_offset[7], 0, 1, 36, 0,
  203. false);
  204. level[0] = level[2] = 0;
  205. if (mix_ovr == 1) {
  206. level[1] = level[3] = 83;
  207. } else {
  208. level[1] = level[3] = 107;
  209. ah->ah_gain.g_high = 55;
  210. }
  211. }
  212. return (ah->ah_gain.g_current >= level[0] &&
  213. ah->ah_gain.g_current <= level[1]) ||
  214. (ah->ah_gain.g_current >= level[2] &&
  215. ah->ah_gain.g_current <= level[3]);
  216. }
  217. /* Perform gain_F adjustment by choosing the right set
  218. * of parameters from rf gain optimization ladder */
  219. static s8 ath5k_hw_rf_gainf_adjust(struct ath5k_hw *ah)
  220. {
  221. const struct ath5k_gain_opt *go;
  222. const struct ath5k_gain_opt_step *g_step;
  223. int ret = 0;
  224. switch (ah->ah_radio) {
  225. case AR5K_RF5111:
  226. go = &rfgain_opt_5111;
  227. break;
  228. case AR5K_RF5112:
  229. go = &rfgain_opt_5112;
  230. break;
  231. default:
  232. return 0;
  233. }
  234. g_step = &go->go_step[ah->ah_gain.g_step_idx];
  235. if (ah->ah_gain.g_current >= ah->ah_gain.g_high) {
  236. /* Reached maximum */
  237. if (ah->ah_gain.g_step_idx == 0)
  238. return -1;
  239. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  240. ah->ah_gain.g_target >= ah->ah_gain.g_high &&
  241. ah->ah_gain.g_step_idx > 0;
  242. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  243. ah->ah_gain.g_target -= 2 *
  244. (go->go_step[--(ah->ah_gain.g_step_idx)].gos_gain -
  245. g_step->gos_gain);
  246. ret = 1;
  247. goto done;
  248. }
  249. if (ah->ah_gain.g_current <= ah->ah_gain.g_low) {
  250. /* Reached minimum */
  251. if (ah->ah_gain.g_step_idx == (go->go_steps_count - 1))
  252. return -2;
  253. for (ah->ah_gain.g_target = ah->ah_gain.g_current;
  254. ah->ah_gain.g_target <= ah->ah_gain.g_low &&
  255. ah->ah_gain.g_step_idx < go->go_steps_count-1;
  256. g_step = &go->go_step[ah->ah_gain.g_step_idx])
  257. ah->ah_gain.g_target -= 2 *
  258. (go->go_step[++ah->ah_gain.g_step_idx].gos_gain -
  259. g_step->gos_gain);
  260. ret = 2;
  261. goto done;
  262. }
  263. done:
  264. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  265. "ret %d, gain step %u, current gain %u, target gain %u\n",
  266. ret, ah->ah_gain.g_step_idx, ah->ah_gain.g_current,
  267. ah->ah_gain.g_target);
  268. return ret;
  269. }
  270. /* Main callback for thermal rf gain calibration engine
  271. * Check for a new gain reading and schedule an adjustment
  272. * if needed.
  273. *
  274. * TODO: Use sw interrupt to schedule reset if gain_F needs
  275. * adjustment */
  276. enum ath5k_rfgain ath5k_hw_gainf_calibrate(struct ath5k_hw *ah)
  277. {
  278. u32 data, type;
  279. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  280. ATH5K_TRACE(ah->ah_sc);
  281. if (ah->ah_rf_banks == NULL ||
  282. ah->ah_gain.g_state == AR5K_RFGAIN_INACTIVE)
  283. return AR5K_RFGAIN_INACTIVE;
  284. /* No check requested, either engine is inactive
  285. * or an adjustment is already requested */
  286. if (ah->ah_gain.g_state != AR5K_RFGAIN_READ_REQUESTED)
  287. goto done;
  288. /* Read the PAPD (Peak to Average Power Detector)
  289. * register */
  290. data = ath5k_hw_reg_read(ah, AR5K_PHY_PAPD_PROBE);
  291. /* No probe is scheduled, read gain_F measurement */
  292. if (!(data & AR5K_PHY_PAPD_PROBE_TX_NEXT)) {
  293. ah->ah_gain.g_current = data >> AR5K_PHY_PAPD_PROBE_GAINF_S;
  294. type = AR5K_REG_MS(data, AR5K_PHY_PAPD_PROBE_TYPE);
  295. /* If tx packet is CCK correct the gain_F measurement
  296. * by cck ofdm gain delta */
  297. if (type == AR5K_PHY_PAPD_PROBE_TYPE_CCK) {
  298. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  299. ah->ah_gain.g_current +=
  300. ee->ee_cck_ofdm_gain_delta;
  301. else
  302. ah->ah_gain.g_current +=
  303. AR5K_GAIN_CCK_PROBE_CORR;
  304. }
  305. /* Further correct gain_F measurement for
  306. * RF5112A radios */
  307. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  308. ath5k_hw_rf_gainf_corr(ah);
  309. ah->ah_gain.g_current =
  310. ah->ah_gain.g_current >= ah->ah_gain.g_f_corr ?
  311. (ah->ah_gain.g_current-ah->ah_gain.g_f_corr) :
  312. 0;
  313. }
  314. /* Check if measurement is ok and if we need
  315. * to adjust gain, schedule a gain adjustment,
  316. * else switch back to the acive state */
  317. if (ath5k_hw_rf_check_gainf_readback(ah) &&
  318. AR5K_GAIN_CHECK_ADJUST(&ah->ah_gain) &&
  319. ath5k_hw_rf_gainf_adjust(ah)) {
  320. ah->ah_gain.g_state = AR5K_RFGAIN_NEED_CHANGE;
  321. } else {
  322. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  323. }
  324. }
  325. done:
  326. return ah->ah_gain.g_state;
  327. }
  328. /* Write initial rf gain table to set the RF sensitivity
  329. * this one works on all RF chips and has nothing to do
  330. * with gain_F calibration */
  331. int ath5k_hw_rfgain_init(struct ath5k_hw *ah, unsigned int freq)
  332. {
  333. const struct ath5k_ini_rfgain *ath5k_rfg;
  334. unsigned int i, size;
  335. switch (ah->ah_radio) {
  336. case AR5K_RF5111:
  337. ath5k_rfg = rfgain_5111;
  338. size = ARRAY_SIZE(rfgain_5111);
  339. break;
  340. case AR5K_RF5112:
  341. ath5k_rfg = rfgain_5112;
  342. size = ARRAY_SIZE(rfgain_5112);
  343. break;
  344. case AR5K_RF2413:
  345. ath5k_rfg = rfgain_2413;
  346. size = ARRAY_SIZE(rfgain_2413);
  347. break;
  348. case AR5K_RF2316:
  349. ath5k_rfg = rfgain_2316;
  350. size = ARRAY_SIZE(rfgain_2316);
  351. break;
  352. case AR5K_RF5413:
  353. ath5k_rfg = rfgain_5413;
  354. size = ARRAY_SIZE(rfgain_5413);
  355. break;
  356. case AR5K_RF2317:
  357. case AR5K_RF2425:
  358. ath5k_rfg = rfgain_2425;
  359. size = ARRAY_SIZE(rfgain_2425);
  360. break;
  361. default:
  362. return -EINVAL;
  363. }
  364. switch (freq) {
  365. case AR5K_INI_RFGAIN_2GHZ:
  366. case AR5K_INI_RFGAIN_5GHZ:
  367. break;
  368. default:
  369. return -EINVAL;
  370. }
  371. for (i = 0; i < size; i++) {
  372. AR5K_REG_WAIT(i);
  373. ath5k_hw_reg_write(ah, ath5k_rfg[i].rfg_value[freq],
  374. (u32)ath5k_rfg[i].rfg_register);
  375. }
  376. return 0;
  377. }
  378. /********************\
  379. * RF Registers setup *
  380. \********************/
  381. /*
  382. * Read EEPROM Calibration data, modify RF Banks and Initialize RF5111
  383. */
  384. static int ath5k_hw_rf5111_rfregs(struct ath5k_hw *ah,
  385. struct ieee80211_channel *channel, unsigned int mode)
  386. {
  387. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  388. u32 *rf;
  389. const unsigned int rf_size = ARRAY_SIZE(rfb_5111);
  390. unsigned int i;
  391. int obdb = -1, bank = -1;
  392. u32 ee_mode;
  393. AR5K_ASSERT_ENTRY(mode, AR5K_MODE_MAX);
  394. rf = ah->ah_rf_banks;
  395. /* Copy values to modify them */
  396. for (i = 0; i < rf_size; i++) {
  397. if (rfb_5111[i].rfb_bank >= AR5K_RF5111_INI_RF_MAX_BANKS) {
  398. ATH5K_ERR(ah->ah_sc, "invalid bank\n");
  399. return -EINVAL;
  400. }
  401. if (bank != rfb_5111[i].rfb_bank) {
  402. bank = rfb_5111[i].rfb_bank;
  403. ah->ah_offset[bank] = i;
  404. }
  405. rf[i] = rfb_5111[i].rfb_mode_data[mode];
  406. }
  407. /* Modify bank 0 */
  408. if (channel->hw_value & CHANNEL_2GHZ) {
  409. if (channel->hw_value & CHANNEL_CCK)
  410. ee_mode = AR5K_EEPROM_MODE_11B;
  411. else
  412. ee_mode = AR5K_EEPROM_MODE_11G;
  413. obdb = 0;
  414. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[0],
  415. ee->ee_ob[ee_mode][obdb], 3, 119, 0, true))
  416. return -EINVAL;
  417. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[0],
  418. ee->ee_ob[ee_mode][obdb], 3, 122, 0, true))
  419. return -EINVAL;
  420. obdb = 1;
  421. /* Modify bank 6 */
  422. } else {
  423. /* For 11a, Turbo and XR */
  424. ee_mode = AR5K_EEPROM_MODE_11A;
  425. obdb = channel->center_freq >= 5725 ? 3 :
  426. (channel->center_freq >= 5500 ? 2 :
  427. (channel->center_freq >= 5260 ? 1 :
  428. (channel->center_freq > 4000 ? 0 : -1)));
  429. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  430. ee->ee_pwd_84, 1, 51, 3, true))
  431. return -EINVAL;
  432. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  433. ee->ee_pwd_90, 1, 45, 3, true))
  434. return -EINVAL;
  435. }
  436. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  437. !ee->ee_xpd[ee_mode], 1, 95, 0, true))
  438. return -EINVAL;
  439. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  440. ee->ee_x_gain[ee_mode], 4, 96, 0, true))
  441. return -EINVAL;
  442. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6], obdb >= 0 ?
  443. ee->ee_ob[ee_mode][obdb] : 0, 3, 104, 0, true))
  444. return -EINVAL;
  445. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6], obdb >= 0 ?
  446. ee->ee_db[ee_mode][obdb] : 0, 3, 107, 0, true))
  447. return -EINVAL;
  448. /* Modify bank 7 */
  449. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[7],
  450. ee->ee_i_gain[ee_mode], 6, 29, 0, true))
  451. return -EINVAL;
  452. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[7],
  453. ee->ee_xpd[ee_mode], 1, 4, 0, true))
  454. return -EINVAL;
  455. /* Write RF values */
  456. for (i = 0; i < rf_size; i++) {
  457. AR5K_REG_WAIT(i);
  458. ath5k_hw_reg_write(ah, rf[i], rfb_5111[i].rfb_ctrl_register);
  459. }
  460. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  461. return 0;
  462. }
  463. /*
  464. * Read EEPROM Calibration data, modify RF Banks and Initialize RF5112
  465. */
  466. static int ath5k_hw_rf5112_rfregs(struct ath5k_hw *ah,
  467. struct ieee80211_channel *channel, unsigned int mode)
  468. {
  469. const struct ath5k_ini_rfbuffer *rf_ini;
  470. struct ath5k_eeprom_info *ee = &ah->ah_capabilities.cap_eeprom;
  471. u32 *rf;
  472. unsigned int rf_size, i;
  473. int obdb = -1, bank = -1;
  474. u32 ee_mode;
  475. AR5K_ASSERT_ENTRY(mode, AR5K_MODE_MAX);
  476. rf = ah->ah_rf_banks;
  477. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A) {
  478. rf_ini = rfb_5112a;
  479. rf_size = ARRAY_SIZE(rfb_5112a);
  480. } else {
  481. rf_ini = rfb_5112;
  482. rf_size = ARRAY_SIZE(rfb_5112);
  483. }
  484. /* Copy values to modify them */
  485. for (i = 0; i < rf_size; i++) {
  486. if (rf_ini[i].rfb_bank >= AR5K_RF5112_INI_RF_MAX_BANKS) {
  487. ATH5K_ERR(ah->ah_sc, "invalid bank\n");
  488. return -EINVAL;
  489. }
  490. if (bank != rf_ini[i].rfb_bank) {
  491. bank = rf_ini[i].rfb_bank;
  492. ah->ah_offset[bank] = i;
  493. }
  494. rf[i] = rf_ini[i].rfb_mode_data[mode];
  495. }
  496. /* Modify bank 6 */
  497. if (channel->hw_value & CHANNEL_2GHZ) {
  498. if (channel->hw_value & CHANNEL_OFDM)
  499. ee_mode = AR5K_EEPROM_MODE_11G;
  500. else
  501. ee_mode = AR5K_EEPROM_MODE_11B;
  502. obdb = 0;
  503. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  504. ee->ee_ob[ee_mode][obdb], 3, 287, 0, true))
  505. return -EINVAL;
  506. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  507. ee->ee_ob[ee_mode][obdb], 3, 290, 0, true))
  508. return -EINVAL;
  509. } else {
  510. /* For 11a, Turbo and XR */
  511. ee_mode = AR5K_EEPROM_MODE_11A;
  512. obdb = channel->center_freq >= 5725 ? 3 :
  513. (channel->center_freq >= 5500 ? 2 :
  514. (channel->center_freq >= 5260 ? 1 :
  515. (channel->center_freq > 4000 ? 0 : -1)));
  516. if (obdb == -1)
  517. return -EINVAL;
  518. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  519. ee->ee_ob[ee_mode][obdb], 3, 279, 0, true))
  520. return -EINVAL;
  521. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  522. ee->ee_ob[ee_mode][obdb], 3, 282, 0, true))
  523. return -EINVAL;
  524. }
  525. ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  526. ee->ee_x_gain[ee_mode], 2, 270, 0, true);
  527. ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  528. ee->ee_x_gain[ee_mode], 2, 257, 0, true);
  529. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[6],
  530. ee->ee_xpd[ee_mode], 1, 302, 0, true))
  531. return -EINVAL;
  532. /* Modify bank 7 */
  533. if (!ath5k_hw_rfregs_op(rf, ah->ah_offset[7],
  534. ee->ee_i_gain[ee_mode], 6, 14, 0, true))
  535. return -EINVAL;
  536. /* Write RF values */
  537. for (i = 0; i < rf_size; i++)
  538. ath5k_hw_reg_write(ah, rf[i], rf_ini[i].rfb_ctrl_register);
  539. ah->ah_gain.g_state = AR5K_RFGAIN_ACTIVE;
  540. return 0;
  541. }
  542. /*
  543. * Initialize RF5413/5414 and future chips
  544. * (until we come up with a better solution)
  545. */
  546. static int ath5k_hw_rf5413_rfregs(struct ath5k_hw *ah,
  547. struct ieee80211_channel *channel, unsigned int mode)
  548. {
  549. const struct ath5k_ini_rfbuffer *rf_ini;
  550. u32 *rf;
  551. unsigned int rf_size, i;
  552. int bank = -1;
  553. AR5K_ASSERT_ENTRY(mode, AR5K_MODE_MAX);
  554. rf = ah->ah_rf_banks;
  555. switch (ah->ah_radio) {
  556. case AR5K_RF5413:
  557. rf_ini = rfb_5413;
  558. rf_size = ARRAY_SIZE(rfb_5413);
  559. break;
  560. case AR5K_RF2413:
  561. rf_ini = rfb_2413;
  562. rf_size = ARRAY_SIZE(rfb_2413);
  563. if (mode < 2) {
  564. ATH5K_ERR(ah->ah_sc,
  565. "invalid channel mode: %i\n", mode);
  566. return -EINVAL;
  567. }
  568. break;
  569. case AR5K_RF2425:
  570. rf_ini = rfb_2425;
  571. rf_size = ARRAY_SIZE(rfb_2425);
  572. if (mode < 2) {
  573. ATH5K_ERR(ah->ah_sc,
  574. "invalid channel mode: %i\n", mode);
  575. return -EINVAL;
  576. }
  577. break;
  578. default:
  579. return -EINVAL;
  580. }
  581. /* Copy values to modify them */
  582. for (i = 0; i < rf_size; i++) {
  583. if (rf_ini[i].rfb_bank >= AR5K_RF5112_INI_RF_MAX_BANKS) {
  584. ATH5K_ERR(ah->ah_sc, "invalid bank\n");
  585. return -EINVAL;
  586. }
  587. if (bank != rf_ini[i].rfb_bank) {
  588. bank = rf_ini[i].rfb_bank;
  589. ah->ah_offset[bank] = i;
  590. }
  591. rf[i] = rf_ini[i].rfb_mode_data[mode];
  592. }
  593. /*
  594. * After compairing dumps from different cards
  595. * we get the same RF_BUFFER settings (diff returns
  596. * 0 lines). It seems that RF_BUFFER settings are static
  597. * and are written unmodified (no EEPROM stuff
  598. * is used because calibration data would be
  599. * different between different cards and would result
  600. * different RF_BUFFER settings)
  601. */
  602. /* Write RF values */
  603. for (i = 0; i < rf_size; i++)
  604. ath5k_hw_reg_write(ah, rf[i], rf_ini[i].rfb_ctrl_register);
  605. return 0;
  606. }
  607. /*
  608. * Initialize RF
  609. */
  610. int ath5k_hw_rfregs(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  611. unsigned int mode)
  612. {
  613. int (*func)(struct ath5k_hw *, struct ieee80211_channel *, unsigned int);
  614. int ret;
  615. switch (ah->ah_radio) {
  616. case AR5K_RF5111:
  617. ah->ah_rf_banks_size = sizeof(rfb_5111);
  618. func = ath5k_hw_rf5111_rfregs;
  619. break;
  620. case AR5K_RF5112:
  621. if (ah->ah_radio_5ghz_revision >= AR5K_SREV_RAD_5112A)
  622. ah->ah_rf_banks_size = sizeof(rfb_5112a);
  623. else
  624. ah->ah_rf_banks_size = sizeof(rfb_5112);
  625. func = ath5k_hw_rf5112_rfregs;
  626. break;
  627. case AR5K_RF5413:
  628. ah->ah_rf_banks_size = sizeof(rfb_5413);
  629. func = ath5k_hw_rf5413_rfregs;
  630. break;
  631. case AR5K_RF2413:
  632. ah->ah_rf_banks_size = sizeof(rfb_2413);
  633. func = ath5k_hw_rf5413_rfregs;
  634. break;
  635. case AR5K_RF2425:
  636. ah->ah_rf_banks_size = sizeof(rfb_2425);
  637. func = ath5k_hw_rf5413_rfregs;
  638. break;
  639. default:
  640. return -EINVAL;
  641. }
  642. if (ah->ah_rf_banks == NULL) {
  643. /* XXX do extra checks? */
  644. ah->ah_rf_banks = kmalloc(ah->ah_rf_banks_size, GFP_KERNEL);
  645. if (ah->ah_rf_banks == NULL) {
  646. ATH5K_ERR(ah->ah_sc, "out of memory\n");
  647. return -ENOMEM;
  648. }
  649. }
  650. ret = func(ah, channel, mode);
  651. return ret;
  652. }
  653. /**************************\
  654. PHY/RF channel functions
  655. \**************************/
  656. /*
  657. * Check if a channel is supported
  658. */
  659. bool ath5k_channel_ok(struct ath5k_hw *ah, u16 freq, unsigned int flags)
  660. {
  661. /* Check if the channel is in our supported range */
  662. if (flags & CHANNEL_2GHZ) {
  663. if ((freq >= ah->ah_capabilities.cap_range.range_2ghz_min) &&
  664. (freq <= ah->ah_capabilities.cap_range.range_2ghz_max))
  665. return true;
  666. } else if (flags & CHANNEL_5GHZ)
  667. if ((freq >= ah->ah_capabilities.cap_range.range_5ghz_min) &&
  668. (freq <= ah->ah_capabilities.cap_range.range_5ghz_max))
  669. return true;
  670. return false;
  671. }
  672. /*
  673. * Convertion needed for RF5110
  674. */
  675. static u32 ath5k_hw_rf5110_chan2athchan(struct ieee80211_channel *channel)
  676. {
  677. u32 athchan;
  678. /*
  679. * Convert IEEE channel/MHz to an internal channel value used
  680. * by the AR5210 chipset. This has not been verified with
  681. * newer chipsets like the AR5212A who have a completely
  682. * different RF/PHY part.
  683. */
  684. athchan = (ath5k_hw_bitswap(
  685. (ieee80211_frequency_to_channel(
  686. channel->center_freq) - 24) / 2, 5)
  687. << 1) | (1 << 6) | 0x1;
  688. return athchan;
  689. }
  690. /*
  691. * Set channel on RF5110
  692. */
  693. static int ath5k_hw_rf5110_channel(struct ath5k_hw *ah,
  694. struct ieee80211_channel *channel)
  695. {
  696. u32 data;
  697. /*
  698. * Set the channel and wait
  699. */
  700. data = ath5k_hw_rf5110_chan2athchan(channel);
  701. ath5k_hw_reg_write(ah, data, AR5K_RF_BUFFER);
  702. ath5k_hw_reg_write(ah, 0, AR5K_RF_BUFFER_CONTROL_0);
  703. mdelay(1);
  704. return 0;
  705. }
  706. /*
  707. * Convertion needed for 5111
  708. */
  709. static int ath5k_hw_rf5111_chan2athchan(unsigned int ieee,
  710. struct ath5k_athchan_2ghz *athchan)
  711. {
  712. int channel;
  713. /* Cast this value to catch negative channel numbers (>= -19) */
  714. channel = (int)ieee;
  715. /*
  716. * Map 2GHz IEEE channel to 5GHz Atheros channel
  717. */
  718. if (channel <= 13) {
  719. athchan->a2_athchan = 115 + channel;
  720. athchan->a2_flags = 0x46;
  721. } else if (channel == 14) {
  722. athchan->a2_athchan = 124;
  723. athchan->a2_flags = 0x44;
  724. } else if (channel >= 15 && channel <= 26) {
  725. athchan->a2_athchan = ((channel - 14) * 4) + 132;
  726. athchan->a2_flags = 0x46;
  727. } else
  728. return -EINVAL;
  729. return 0;
  730. }
  731. /*
  732. * Set channel on 5111
  733. */
  734. static int ath5k_hw_rf5111_channel(struct ath5k_hw *ah,
  735. struct ieee80211_channel *channel)
  736. {
  737. struct ath5k_athchan_2ghz ath5k_channel_2ghz;
  738. unsigned int ath5k_channel =
  739. ieee80211_frequency_to_channel(channel->center_freq);
  740. u32 data0, data1, clock;
  741. int ret;
  742. /*
  743. * Set the channel on the RF5111 radio
  744. */
  745. data0 = data1 = 0;
  746. if (channel->hw_value & CHANNEL_2GHZ) {
  747. /* Map 2GHz channel to 5GHz Atheros channel ID */
  748. ret = ath5k_hw_rf5111_chan2athchan(
  749. ieee80211_frequency_to_channel(channel->center_freq),
  750. &ath5k_channel_2ghz);
  751. if (ret)
  752. return ret;
  753. ath5k_channel = ath5k_channel_2ghz.a2_athchan;
  754. data0 = ((ath5k_hw_bitswap(ath5k_channel_2ghz.a2_flags, 8) & 0xff)
  755. << 5) | (1 << 4);
  756. }
  757. if (ath5k_channel < 145 || !(ath5k_channel & 1)) {
  758. clock = 1;
  759. data1 = ((ath5k_hw_bitswap(ath5k_channel - 24, 8) & 0xff) << 2) |
  760. (clock << 1) | (1 << 10) | 1;
  761. } else {
  762. clock = 0;
  763. data1 = ((ath5k_hw_bitswap((ath5k_channel - 24) / 2, 8) & 0xff)
  764. << 2) | (clock << 1) | (1 << 10) | 1;
  765. }
  766. ath5k_hw_reg_write(ah, (data1 & 0xff) | ((data0 & 0xff) << 8),
  767. AR5K_RF_BUFFER);
  768. ath5k_hw_reg_write(ah, ((data1 >> 8) & 0xff) | (data0 & 0xff00),
  769. AR5K_RF_BUFFER_CONTROL_3);
  770. return 0;
  771. }
  772. /*
  773. * Set channel on 5112 and newer
  774. */
  775. static int ath5k_hw_rf5112_channel(struct ath5k_hw *ah,
  776. struct ieee80211_channel *channel)
  777. {
  778. u32 data, data0, data1, data2;
  779. u16 c;
  780. data = data0 = data1 = data2 = 0;
  781. c = channel->center_freq;
  782. if (c < 4800) {
  783. if (!((c - 2224) % 5)) {
  784. data0 = ((2 * (c - 704)) - 3040) / 10;
  785. data1 = 1;
  786. } else if (!((c - 2192) % 5)) {
  787. data0 = ((2 * (c - 672)) - 3040) / 10;
  788. data1 = 0;
  789. } else
  790. return -EINVAL;
  791. data0 = ath5k_hw_bitswap((data0 << 2) & 0xff, 8);
  792. } else if ((c - (c % 5)) != 2 || c > 5435) {
  793. if (!(c % 20) && c >= 5120) {
  794. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  795. data2 = ath5k_hw_bitswap(3, 2);
  796. } else if (!(c % 10)) {
  797. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  798. data2 = ath5k_hw_bitswap(2, 2);
  799. } else if (!(c % 5)) {
  800. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  801. data2 = ath5k_hw_bitswap(1, 2);
  802. } else
  803. return -EINVAL;
  804. } else {
  805. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  806. data2 = ath5k_hw_bitswap(0, 2);
  807. }
  808. data = (data0 << 4) | (data1 << 1) | (data2 << 2) | 0x1001;
  809. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  810. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  811. return 0;
  812. }
  813. /*
  814. * Set the channel on the RF2425
  815. */
  816. static int ath5k_hw_rf2425_channel(struct ath5k_hw *ah,
  817. struct ieee80211_channel *channel)
  818. {
  819. u32 data, data0, data2;
  820. u16 c;
  821. data = data0 = data2 = 0;
  822. c = channel->center_freq;
  823. if (c < 4800) {
  824. data0 = ath5k_hw_bitswap((c - 2272), 8);
  825. data2 = 0;
  826. /* ? 5GHz ? */
  827. } else if ((c - (c % 5)) != 2 || c > 5435) {
  828. if (!(c % 20) && c < 5120)
  829. data0 = ath5k_hw_bitswap(((c - 4800) / 20 << 2), 8);
  830. else if (!(c % 10))
  831. data0 = ath5k_hw_bitswap(((c - 4800) / 10 << 1), 8);
  832. else if (!(c % 5))
  833. data0 = ath5k_hw_bitswap((c - 4800) / 5, 8);
  834. else
  835. return -EINVAL;
  836. data2 = ath5k_hw_bitswap(1, 2);
  837. } else {
  838. data0 = ath5k_hw_bitswap((10 * (c - 2) - 4800) / 25 + 1, 8);
  839. data2 = ath5k_hw_bitswap(0, 2);
  840. }
  841. data = (data0 << 4) | data2 << 2 | 0x1001;
  842. ath5k_hw_reg_write(ah, data & 0xff, AR5K_RF_BUFFER);
  843. ath5k_hw_reg_write(ah, (data >> 8) & 0x7f, AR5K_RF_BUFFER_CONTROL_5);
  844. return 0;
  845. }
  846. /*
  847. * Set a channel on the radio chip
  848. */
  849. int ath5k_hw_channel(struct ath5k_hw *ah, struct ieee80211_channel *channel)
  850. {
  851. int ret;
  852. /*
  853. * Check bounds supported by the PHY (we don't care about regultory
  854. * restrictions at this point). Note: hw_value already has the band
  855. * (CHANNEL_2GHZ, or CHANNEL_5GHZ) so we inform ath5k_channel_ok()
  856. * of the band by that */
  857. if (!ath5k_channel_ok(ah, channel->center_freq, channel->hw_value)) {
  858. ATH5K_ERR(ah->ah_sc,
  859. "channel frequency (%u MHz) out of supported "
  860. "band range\n",
  861. channel->center_freq);
  862. return -EINVAL;
  863. }
  864. /*
  865. * Set the channel and wait
  866. */
  867. switch (ah->ah_radio) {
  868. case AR5K_RF5110:
  869. ret = ath5k_hw_rf5110_channel(ah, channel);
  870. break;
  871. case AR5K_RF5111:
  872. ret = ath5k_hw_rf5111_channel(ah, channel);
  873. break;
  874. case AR5K_RF2425:
  875. ret = ath5k_hw_rf2425_channel(ah, channel);
  876. break;
  877. default:
  878. ret = ath5k_hw_rf5112_channel(ah, channel);
  879. break;
  880. }
  881. if (ret)
  882. return ret;
  883. /* Set JAPAN setting for channel 14 */
  884. if (channel->center_freq == 2484) {
  885. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  886. AR5K_PHY_CCKTXCTL_JAPAN);
  887. } else {
  888. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_CCKTXCTL,
  889. AR5K_PHY_CCKTXCTL_WORLD);
  890. }
  891. ah->ah_current_channel.center_freq = channel->center_freq;
  892. ah->ah_current_channel.hw_value = channel->hw_value;
  893. ah->ah_turbo = channel->hw_value == CHANNEL_T ? true : false;
  894. return 0;
  895. }
  896. /*****************\
  897. PHY calibration
  898. \*****************/
  899. /**
  900. * ath5k_hw_noise_floor_calibration - perform PHY noise floor calibration
  901. *
  902. * @ah: struct ath5k_hw pointer we are operating on
  903. * @freq: the channel frequency, just used for error logging
  904. *
  905. * This function performs a noise floor calibration of the PHY and waits for
  906. * it to complete. Then the noise floor value is compared to some maximum
  907. * noise floor we consider valid.
  908. *
  909. * Note that this is different from what the madwifi HAL does: it reads the
  910. * noise floor and afterwards initiates the calibration. Since the noise floor
  911. * calibration can take some time to finish, depending on the current channel
  912. * use, that avoids the occasional timeout warnings we are seeing now.
  913. *
  914. * See the following link for an Atheros patent on noise floor calibration:
  915. * http://patft.uspto.gov/netacgi/nph-Parser?Sect1=PTO1&Sect2=HITOFF&d=PALL \
  916. * &p=1&u=%2Fnetahtml%2FPTO%2Fsrchnum.htm&r=1&f=G&l=50&s1=7245893.PN.&OS=PN/7
  917. *
  918. * XXX: Since during noise floor calibration antennas are detached according to
  919. * the patent, we should stop tx queues here.
  920. */
  921. int
  922. ath5k_hw_noise_floor_calibration(struct ath5k_hw *ah, short freq)
  923. {
  924. int ret;
  925. unsigned int i;
  926. s32 noise_floor;
  927. /*
  928. * Enable noise floor calibration
  929. */
  930. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL,
  931. AR5K_PHY_AGCCTL_NF);
  932. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  933. AR5K_PHY_AGCCTL_NF, 0, false);
  934. if (ret) {
  935. ATH5K_ERR(ah->ah_sc,
  936. "noise floor calibration timeout (%uMHz)\n", freq);
  937. return -EAGAIN;
  938. }
  939. /* Wait until the noise floor is calibrated and read the value */
  940. for (i = 20; i > 0; i--) {
  941. mdelay(1);
  942. noise_floor = ath5k_hw_reg_read(ah, AR5K_PHY_NF);
  943. noise_floor = AR5K_PHY_NF_RVAL(noise_floor);
  944. if (noise_floor & AR5K_PHY_NF_ACTIVE) {
  945. noise_floor = AR5K_PHY_NF_AVAL(noise_floor);
  946. if (noise_floor <= AR5K_TUNE_NOISE_FLOOR)
  947. break;
  948. }
  949. }
  950. ATH5K_DBG_UNLIMIT(ah->ah_sc, ATH5K_DEBUG_CALIBRATE,
  951. "noise floor %d\n", noise_floor);
  952. if (noise_floor > AR5K_TUNE_NOISE_FLOOR) {
  953. ATH5K_ERR(ah->ah_sc,
  954. "noise floor calibration failed (%uMHz)\n", freq);
  955. return -EAGAIN;
  956. }
  957. ah->ah_noise_floor = noise_floor;
  958. return 0;
  959. }
  960. /*
  961. * Perform a PHY calibration on RF5110
  962. * -Fix BPSK/QAM Constellation (I/Q correction)
  963. * -Calculate Noise Floor
  964. */
  965. static int ath5k_hw_rf5110_calibrate(struct ath5k_hw *ah,
  966. struct ieee80211_channel *channel)
  967. {
  968. u32 phy_sig, phy_agc, phy_sat, beacon;
  969. int ret;
  970. /*
  971. * Disable beacons and RX/TX queues, wait
  972. */
  973. AR5K_REG_ENABLE_BITS(ah, AR5K_DIAG_SW_5210,
  974. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  975. beacon = ath5k_hw_reg_read(ah, AR5K_BEACON_5210);
  976. ath5k_hw_reg_write(ah, beacon & ~AR5K_BEACON_ENABLE, AR5K_BEACON_5210);
  977. mdelay(2);
  978. /*
  979. * Set the channel (with AGC turned off)
  980. */
  981. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  982. udelay(10);
  983. ret = ath5k_hw_channel(ah, channel);
  984. /*
  985. * Activate PHY and wait
  986. */
  987. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_ENABLE, AR5K_PHY_ACT);
  988. mdelay(1);
  989. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  990. if (ret)
  991. return ret;
  992. /*
  993. * Calibrate the radio chip
  994. */
  995. /* Remember normal state */
  996. phy_sig = ath5k_hw_reg_read(ah, AR5K_PHY_SIG);
  997. phy_agc = ath5k_hw_reg_read(ah, AR5K_PHY_AGCCOARSE);
  998. phy_sat = ath5k_hw_reg_read(ah, AR5K_PHY_ADCSAT);
  999. /* Update radio registers */
  1000. ath5k_hw_reg_write(ah, (phy_sig & ~(AR5K_PHY_SIG_FIRPWR)) |
  1001. AR5K_REG_SM(-1, AR5K_PHY_SIG_FIRPWR), AR5K_PHY_SIG);
  1002. ath5k_hw_reg_write(ah, (phy_agc & ~(AR5K_PHY_AGCCOARSE_HI |
  1003. AR5K_PHY_AGCCOARSE_LO)) |
  1004. AR5K_REG_SM(-1, AR5K_PHY_AGCCOARSE_HI) |
  1005. AR5K_REG_SM(-127, AR5K_PHY_AGCCOARSE_LO), AR5K_PHY_AGCCOARSE);
  1006. ath5k_hw_reg_write(ah, (phy_sat & ~(AR5K_PHY_ADCSAT_ICNT |
  1007. AR5K_PHY_ADCSAT_THR)) |
  1008. AR5K_REG_SM(2, AR5K_PHY_ADCSAT_ICNT) |
  1009. AR5K_REG_SM(12, AR5K_PHY_ADCSAT_THR), AR5K_PHY_ADCSAT);
  1010. udelay(20);
  1011. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1012. udelay(10);
  1013. ath5k_hw_reg_write(ah, AR5K_PHY_RFSTG_DISABLE, AR5K_PHY_RFSTG);
  1014. AR5K_REG_DISABLE_BITS(ah, AR5K_PHY_AGC, AR5K_PHY_AGC_DISABLE);
  1015. mdelay(1);
  1016. /*
  1017. * Enable calibration and wait until completion
  1018. */
  1019. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_AGCCTL, AR5K_PHY_AGCCTL_CAL);
  1020. ret = ath5k_hw_register_timeout(ah, AR5K_PHY_AGCCTL,
  1021. AR5K_PHY_AGCCTL_CAL, 0, false);
  1022. /* Reset to normal state */
  1023. ath5k_hw_reg_write(ah, phy_sig, AR5K_PHY_SIG);
  1024. ath5k_hw_reg_write(ah, phy_agc, AR5K_PHY_AGCCOARSE);
  1025. ath5k_hw_reg_write(ah, phy_sat, AR5K_PHY_ADCSAT);
  1026. if (ret) {
  1027. ATH5K_ERR(ah->ah_sc, "calibration timeout (%uMHz)\n",
  1028. channel->center_freq);
  1029. return ret;
  1030. }
  1031. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1032. /*
  1033. * Re-enable RX/TX and beacons
  1034. */
  1035. AR5K_REG_DISABLE_BITS(ah, AR5K_DIAG_SW_5210,
  1036. AR5K_DIAG_SW_DIS_TX | AR5K_DIAG_SW_DIS_RX_5210);
  1037. ath5k_hw_reg_write(ah, beacon, AR5K_BEACON_5210);
  1038. return 0;
  1039. }
  1040. /*
  1041. * Perform a PHY calibration on RF5111/5112 and newer chips
  1042. */
  1043. static int ath5k_hw_rf511x_calibrate(struct ath5k_hw *ah,
  1044. struct ieee80211_channel *channel)
  1045. {
  1046. u32 i_pwr, q_pwr;
  1047. s32 iq_corr, i_coff, i_coffd, q_coff, q_coffd;
  1048. int i;
  1049. ATH5K_TRACE(ah->ah_sc);
  1050. if (!ah->ah_calibration ||
  1051. ath5k_hw_reg_read(ah, AR5K_PHY_IQ) & AR5K_PHY_IQ_RUN)
  1052. goto done;
  1053. /* Calibration has finished, get the results and re-run */
  1054. for (i = 0; i <= 10; i++) {
  1055. iq_corr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_CORR);
  1056. i_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_I);
  1057. q_pwr = ath5k_hw_reg_read(ah, AR5K_PHY_IQRES_CAL_PWR_Q);
  1058. }
  1059. i_coffd = ((i_pwr >> 1) + (q_pwr >> 1)) >> 7;
  1060. q_coffd = q_pwr >> 7;
  1061. /* No correction */
  1062. if (i_coffd == 0 || q_coffd == 0)
  1063. goto done;
  1064. i_coff = ((-iq_corr) / i_coffd) & 0x3f;
  1065. /* Boundary check */
  1066. if (i_coff > 31)
  1067. i_coff = 31;
  1068. if (i_coff < -32)
  1069. i_coff = -32;
  1070. q_coff = (((s32)i_pwr / q_coffd) - 128) & 0x1f;
  1071. /* Boundary check */
  1072. if (q_coff > 15)
  1073. q_coff = 15;
  1074. if (q_coff < -16)
  1075. q_coff = -16;
  1076. /* Commit new I/Q value */
  1077. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_CORR_ENABLE |
  1078. ((u32)q_coff) | ((u32)i_coff << AR5K_PHY_IQ_CORR_Q_I_COFF_S));
  1079. /* Re-enable calibration -if we don't we'll commit
  1080. * the same values again and again */
  1081. AR5K_REG_WRITE_BITS(ah, AR5K_PHY_IQ,
  1082. AR5K_PHY_IQ_CAL_NUM_LOG_MAX, 15);
  1083. AR5K_REG_ENABLE_BITS(ah, AR5K_PHY_IQ, AR5K_PHY_IQ_RUN);
  1084. done:
  1085. /* TODO: Separate noise floor calibration from I/Q calibration
  1086. * since noise floor calibration interrupts rx path while I/Q
  1087. * calibration doesn't. We don't need to run noise floor calibration
  1088. * as often as I/Q calibration.*/
  1089. ath5k_hw_noise_floor_calibration(ah, channel->center_freq);
  1090. /* Initiate a gain_F calibration */
  1091. ath5k_hw_request_rfgain_probe(ah);
  1092. return 0;
  1093. }
  1094. /*
  1095. * Perform a PHY calibration
  1096. */
  1097. int ath5k_hw_phy_calibrate(struct ath5k_hw *ah,
  1098. struct ieee80211_channel *channel)
  1099. {
  1100. int ret;
  1101. if (ah->ah_radio == AR5K_RF5110)
  1102. ret = ath5k_hw_rf5110_calibrate(ah, channel);
  1103. else
  1104. ret = ath5k_hw_rf511x_calibrate(ah, channel);
  1105. return ret;
  1106. }
  1107. int ath5k_hw_phy_disable(struct ath5k_hw *ah)
  1108. {
  1109. ATH5K_TRACE(ah->ah_sc);
  1110. /*Just a try M.F.*/
  1111. ath5k_hw_reg_write(ah, AR5K_PHY_ACT_DISABLE, AR5K_PHY_ACT);
  1112. return 0;
  1113. }
  1114. /********************\
  1115. Misc PHY functions
  1116. \********************/
  1117. /*
  1118. * Get the PHY Chip revision
  1119. */
  1120. u16 ath5k_hw_radio_revision(struct ath5k_hw *ah, unsigned int chan)
  1121. {
  1122. unsigned int i;
  1123. u32 srev;
  1124. u16 ret;
  1125. ATH5K_TRACE(ah->ah_sc);
  1126. /*
  1127. * Set the radio chip access register
  1128. */
  1129. switch (chan) {
  1130. case CHANNEL_2GHZ:
  1131. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_2GHZ, AR5K_PHY(0));
  1132. break;
  1133. case CHANNEL_5GHZ:
  1134. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1135. break;
  1136. default:
  1137. return 0;
  1138. }
  1139. mdelay(2);
  1140. /* ...wait until PHY is ready and read the selected radio revision */
  1141. ath5k_hw_reg_write(ah, 0x00001c16, AR5K_PHY(0x34));
  1142. for (i = 0; i < 8; i++)
  1143. ath5k_hw_reg_write(ah, 0x00010000, AR5K_PHY(0x20));
  1144. if (ah->ah_version == AR5K_AR5210) {
  1145. srev = ath5k_hw_reg_read(ah, AR5K_PHY(256) >> 28) & 0xf;
  1146. ret = (u16)ath5k_hw_bitswap(srev, 4) + 1;
  1147. } else {
  1148. srev = (ath5k_hw_reg_read(ah, AR5K_PHY(0x100)) >> 24) & 0xff;
  1149. ret = (u16)ath5k_hw_bitswap(((srev & 0xf0) >> 4) |
  1150. ((srev & 0x0f) << 4), 8);
  1151. }
  1152. /* Reset to the 5GHz mode */
  1153. ath5k_hw_reg_write(ah, AR5K_PHY_SHIFT_5GHZ, AR5K_PHY(0));
  1154. return ret;
  1155. }
  1156. void /*TODO:Boundary check*/
  1157. ath5k_hw_set_def_antenna(struct ath5k_hw *ah, unsigned int ant)
  1158. {
  1159. ATH5K_TRACE(ah->ah_sc);
  1160. /*Just a try M.F.*/
  1161. if (ah->ah_version != AR5K_AR5210)
  1162. ath5k_hw_reg_write(ah, ant, AR5K_DEFAULT_ANTENNA);
  1163. }
  1164. unsigned int ath5k_hw_get_def_antenna(struct ath5k_hw *ah)
  1165. {
  1166. ATH5K_TRACE(ah->ah_sc);
  1167. /*Just a try M.F.*/
  1168. if (ah->ah_version != AR5K_AR5210)
  1169. return ath5k_hw_reg_read(ah, AR5K_DEFAULT_ANTENNA);
  1170. return false; /*XXX: What do we return for 5210 ?*/
  1171. }
  1172. /*
  1173. * TX power setup
  1174. */
  1175. /*
  1176. * Initialize the tx power table (not fully implemented)
  1177. */
  1178. static void ath5k_txpower_table(struct ath5k_hw *ah,
  1179. struct ieee80211_channel *channel, s16 max_power)
  1180. {
  1181. unsigned int i, min, max, n;
  1182. u16 txpower, *rates;
  1183. rates = ah->ah_txpower.txp_rates;
  1184. txpower = AR5K_TUNE_DEFAULT_TXPOWER * 2;
  1185. if (max_power > txpower)
  1186. txpower = max_power > AR5K_TUNE_MAX_TXPOWER ?
  1187. AR5K_TUNE_MAX_TXPOWER : max_power;
  1188. for (i = 0; i < AR5K_MAX_RATES; i++)
  1189. rates[i] = txpower;
  1190. /* XXX setup target powers by rate */
  1191. ah->ah_txpower.txp_min = rates[7];
  1192. ah->ah_txpower.txp_max = rates[0];
  1193. ah->ah_txpower.txp_ofdm = rates[0];
  1194. /* Calculate the power table */
  1195. n = ARRAY_SIZE(ah->ah_txpower.txp_pcdac);
  1196. min = AR5K_EEPROM_PCDAC_START;
  1197. max = AR5K_EEPROM_PCDAC_STOP;
  1198. for (i = 0; i < n; i += AR5K_EEPROM_PCDAC_STEP)
  1199. ah->ah_txpower.txp_pcdac[i] =
  1200. #ifdef notyet
  1201. min + ((i * (max - min)) / n);
  1202. #else
  1203. min;
  1204. #endif
  1205. }
  1206. /*
  1207. * Set transmition power
  1208. */
  1209. int /*O.K. - txpower_table is unimplemented so this doesn't work*/
  1210. ath5k_hw_txpower(struct ath5k_hw *ah, struct ieee80211_channel *channel,
  1211. unsigned int txpower)
  1212. {
  1213. bool tpc = ah->ah_txpower.txp_tpc;
  1214. unsigned int i;
  1215. ATH5K_TRACE(ah->ah_sc);
  1216. if (txpower > AR5K_TUNE_MAX_TXPOWER) {
  1217. ATH5K_ERR(ah->ah_sc, "invalid tx power: %u\n", txpower);
  1218. return -EINVAL;
  1219. }
  1220. /*
  1221. * RF2413 for some reason can't
  1222. * transmit anything if we call
  1223. * this funtion, so we skip it
  1224. * until we fix txpower.
  1225. *
  1226. * XXX: Assume same for RF2425
  1227. * to be safe.
  1228. */
  1229. if ((ah->ah_radio == AR5K_RF2413) || (ah->ah_radio == AR5K_RF2425))
  1230. return 0;
  1231. /* Reset TX power values */
  1232. memset(&ah->ah_txpower, 0, sizeof(ah->ah_txpower));
  1233. ah->ah_txpower.txp_tpc = tpc;
  1234. /* Initialize TX power table */
  1235. ath5k_txpower_table(ah, channel, txpower);
  1236. /*
  1237. * Write TX power values
  1238. */
  1239. for (i = 0; i < (AR5K_EEPROM_POWER_TABLE_SIZE / 2); i++) {
  1240. ath5k_hw_reg_write(ah,
  1241. ((((ah->ah_txpower.txp_pcdac[(i << 1) + 1] << 8) | 0xff) & 0xffff) << 16) |
  1242. (((ah->ah_txpower.txp_pcdac[(i << 1) ] << 8) | 0xff) & 0xffff),
  1243. AR5K_PHY_PCDAC_TXPOWER(i));
  1244. }
  1245. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(3, 24) |
  1246. AR5K_TXPOWER_OFDM(2, 16) | AR5K_TXPOWER_OFDM(1, 8) |
  1247. AR5K_TXPOWER_OFDM(0, 0), AR5K_PHY_TXPOWER_RATE1);
  1248. ath5k_hw_reg_write(ah, AR5K_TXPOWER_OFDM(7, 24) |
  1249. AR5K_TXPOWER_OFDM(6, 16) | AR5K_TXPOWER_OFDM(5, 8) |
  1250. AR5K_TXPOWER_OFDM(4, 0), AR5K_PHY_TXPOWER_RATE2);
  1251. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(10, 24) |
  1252. AR5K_TXPOWER_CCK(9, 16) | AR5K_TXPOWER_CCK(15, 8) |
  1253. AR5K_TXPOWER_CCK(8, 0), AR5K_PHY_TXPOWER_RATE3);
  1254. ath5k_hw_reg_write(ah, AR5K_TXPOWER_CCK(14, 24) |
  1255. AR5K_TXPOWER_CCK(13, 16) | AR5K_TXPOWER_CCK(12, 8) |
  1256. AR5K_TXPOWER_CCK(11, 0), AR5K_PHY_TXPOWER_RATE4);
  1257. if (ah->ah_txpower.txp_tpc)
  1258. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX_TPC_ENABLE |
  1259. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  1260. else
  1261. ath5k_hw_reg_write(ah, AR5K_PHY_TXPOWER_RATE_MAX |
  1262. AR5K_TUNE_MAX_TXPOWER, AR5K_PHY_TXPOWER_RATE_MAX);
  1263. return 0;
  1264. }
  1265. int ath5k_hw_set_txpower_limit(struct ath5k_hw *ah, unsigned int power)
  1266. {
  1267. /*Just a try M.F.*/
  1268. struct ieee80211_channel *channel = &ah->ah_current_channel;
  1269. ATH5K_TRACE(ah->ah_sc);
  1270. ATH5K_DBG(ah->ah_sc, ATH5K_DEBUG_TXPOWER,
  1271. "changing txpower to %d\n", power);
  1272. return ath5k_hw_txpower(ah, channel, power);
  1273. }
  1274. #undef _ATH5K_PHY