ar9002_phy.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. /**
  17. * DOC: Programming Atheros 802.11n analog front end radios
  18. *
  19. * AR5416 MAC based PCI devices and AR518 MAC based PCI-Express
  20. * devices have either an external AR2133 analog front end radio for single
  21. * band 2.4 GHz communication or an AR5133 analog front end radio for dual
  22. * band 2.4 GHz / 5 GHz communication.
  23. *
  24. * All devices after the AR5416 and AR5418 family starting with the AR9280
  25. * have their analog front radios, MAC/BB and host PCIe/USB interface embedded
  26. * into a single-chip and require less programming.
  27. *
  28. * The following single-chips exist with a respective embedded radio:
  29. *
  30. * AR9280 - 11n dual-band 2x2 MIMO for PCIe
  31. * AR9281 - 11n single-band 1x2 MIMO for PCIe
  32. * AR9285 - 11n single-band 1x1 for PCIe
  33. * AR9287 - 11n single-band 2x2 MIMO for PCIe
  34. *
  35. * AR9220 - 11n dual-band 2x2 MIMO for PCI
  36. * AR9223 - 11n single-band 2x2 MIMO for PCI
  37. *
  38. * AR9287 - 11n single-band 1x1 MIMO for USB
  39. */
  40. #include "hw.h"
  41. #include "ar9002_phy.h"
  42. /**
  43. * ar9002_hw_set_channel - set channel on single-chip device
  44. * @ah: atheros hardware structure
  45. * @chan:
  46. *
  47. * This is the function to change channel on single-chip devices, that is
  48. * all devices after ar9280.
  49. *
  50. * This function takes the channel value in MHz and sets
  51. * hardware channel value. Assumes writes have been enabled to analog bus.
  52. *
  53. * Actual Expression,
  54. *
  55. * For 2GHz channel,
  56. * Channel Frequency = (3/4) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)
  57. * (freq_ref = 40MHz)
  58. *
  59. * For 5GHz channel,
  60. * Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^10)
  61. * (freq_ref = 40MHz/(24>>amodeRefSel))
  62. */
  63. static int ar9002_hw_set_channel(struct ath_hw *ah, struct ath9k_channel *chan)
  64. {
  65. u16 bMode, fracMode, aModeRefSel = 0;
  66. u32 freq, ndiv, channelSel = 0, channelFrac = 0, reg32 = 0;
  67. struct chan_centers centers;
  68. u32 refDivA = 24;
  69. ath9k_hw_get_channel_centers(ah, chan, &centers);
  70. freq = centers.synth_center;
  71. reg32 = REG_READ(ah, AR_PHY_SYNTH_CONTROL);
  72. reg32 &= 0xc0000000;
  73. if (freq < 4800) { /* 2 GHz, fractional mode */
  74. u32 txctl;
  75. int regWrites = 0;
  76. bMode = 1;
  77. fracMode = 1;
  78. aModeRefSel = 0;
  79. channelSel = CHANSEL_2G(freq);
  80. if (AR_SREV_9287_11_OR_LATER(ah)) {
  81. if (freq == 2484) {
  82. /* Enable channel spreading for channel 14 */
  83. REG_WRITE_ARRAY(&ah->iniCckfirJapan2484,
  84. 1, regWrites);
  85. } else {
  86. REG_WRITE_ARRAY(&ah->iniCckfirNormal,
  87. 1, regWrites);
  88. }
  89. } else {
  90. txctl = REG_READ(ah, AR_PHY_CCK_TX_CTRL);
  91. if (freq == 2484) {
  92. /* Enable channel spreading for channel 14 */
  93. REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
  94. txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
  95. } else {
  96. REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
  97. txctl & ~AR_PHY_CCK_TX_CTRL_JAPAN);
  98. }
  99. }
  100. } else {
  101. bMode = 0;
  102. fracMode = 0;
  103. switch (ah->eep_ops->get_eeprom(ah, EEP_FRAC_N_5G)) {
  104. case 0:
  105. if (IS_CHAN_HALF_RATE(chan) || IS_CHAN_QUARTER_RATE(chan))
  106. aModeRefSel = 0;
  107. else if ((freq % 20) == 0)
  108. aModeRefSel = 3;
  109. else if ((freq % 10) == 0)
  110. aModeRefSel = 2;
  111. if (aModeRefSel)
  112. break;
  113. case 1:
  114. default:
  115. aModeRefSel = 0;
  116. /*
  117. * Enable 2G (fractional) mode for channels
  118. * which are 5MHz spaced.
  119. */
  120. fracMode = 1;
  121. refDivA = 1;
  122. channelSel = CHANSEL_5G(freq);
  123. /* RefDivA setting */
  124. ath9k_hw_analog_shift_rmw(ah, AR_AN_SYNTH9,
  125. AR_AN_SYNTH9_REFDIVA,
  126. AR_AN_SYNTH9_REFDIVA_S, refDivA);
  127. }
  128. if (!fracMode) {
  129. ndiv = (freq * (refDivA >> aModeRefSel)) / 60;
  130. channelSel = ndiv & 0x1ff;
  131. channelFrac = (ndiv & 0xfffffe00) * 2;
  132. channelSel = (channelSel << 17) | channelFrac;
  133. }
  134. }
  135. reg32 = reg32 |
  136. (bMode << 29) |
  137. (fracMode << 28) | (aModeRefSel << 26) | (channelSel);
  138. REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);
  139. ah->curchan = chan;
  140. return 0;
  141. }
  142. /**
  143. * ar9002_hw_spur_mitigate - convert baseband spur frequency
  144. * @ah: atheros hardware structure
  145. * @chan:
  146. *
  147. * For single-chip solutions. Converts to baseband spur frequency given the
  148. * input channel frequency and compute register settings below.
  149. */
  150. static void ar9002_hw_spur_mitigate(struct ath_hw *ah,
  151. struct ath9k_channel *chan)
  152. {
  153. int bb_spur = AR_NO_SPUR;
  154. int freq;
  155. int bin, cur_bin;
  156. int bb_spur_off, spur_subchannel_sd;
  157. int spur_freq_sd;
  158. int spur_delta_phase;
  159. int denominator;
  160. int upper, lower, cur_vit_mask;
  161. int tmp, newVal;
  162. int i;
  163. static const int pilot_mask_reg[4] = {
  164. AR_PHY_TIMING7, AR_PHY_TIMING8,
  165. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  166. };
  167. static const int chan_mask_reg[4] = {
  168. AR_PHY_TIMING9, AR_PHY_TIMING10,
  169. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  170. };
  171. static const int inc[4] = { 0, 100, 0, 0 };
  172. struct chan_centers centers;
  173. int8_t mask_m[123];
  174. int8_t mask_p[123];
  175. int8_t mask_amt;
  176. int tmp_mask;
  177. int cur_bb_spur;
  178. bool is2GHz = IS_CHAN_2GHZ(chan);
  179. memset(&mask_m, 0, sizeof(int8_t) * 123);
  180. memset(&mask_p, 0, sizeof(int8_t) * 123);
  181. ath9k_hw_get_channel_centers(ah, chan, &centers);
  182. freq = centers.synth_center;
  183. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  184. cur_bb_spur = ah->eep_ops->get_spur_channel(ah, i, is2GHz);
  185. if (AR_NO_SPUR == cur_bb_spur)
  186. break;
  187. if (is2GHz)
  188. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_2GHZ;
  189. else
  190. cur_bb_spur = (cur_bb_spur / 10) + AR_BASE_FREQ_5GHZ;
  191. cur_bb_spur = cur_bb_spur - freq;
  192. if (IS_CHAN_HT40(chan)) {
  193. if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT40) &&
  194. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT40)) {
  195. bb_spur = cur_bb_spur;
  196. break;
  197. }
  198. } else if ((cur_bb_spur > -AR_SPUR_FEEQ_BOUND_HT20) &&
  199. (cur_bb_spur < AR_SPUR_FEEQ_BOUND_HT20)) {
  200. bb_spur = cur_bb_spur;
  201. break;
  202. }
  203. }
  204. if (AR_NO_SPUR == bb_spur) {
  205. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  206. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  207. return;
  208. } else {
  209. REG_CLR_BIT(ah, AR_PHY_FORCE_CLKEN_CCK,
  210. AR_PHY_FORCE_CLKEN_CCK_MRC_MUX);
  211. }
  212. bin = bb_spur * 320;
  213. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  214. ENABLE_REGWRITE_BUFFER(ah);
  215. newVal = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  216. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  217. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  218. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  219. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), newVal);
  220. newVal = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  221. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  222. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  223. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  224. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  225. REG_WRITE(ah, AR_PHY_SPUR_REG, newVal);
  226. if (IS_CHAN_HT40(chan)) {
  227. if (bb_spur < 0) {
  228. spur_subchannel_sd = 1;
  229. bb_spur_off = bb_spur + 10;
  230. } else {
  231. spur_subchannel_sd = 0;
  232. bb_spur_off = bb_spur - 10;
  233. }
  234. } else {
  235. spur_subchannel_sd = 0;
  236. bb_spur_off = bb_spur;
  237. }
  238. if (IS_CHAN_HT40(chan))
  239. spur_delta_phase =
  240. ((bb_spur * 262144) /
  241. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  242. else
  243. spur_delta_phase =
  244. ((bb_spur * 524288) /
  245. 10) & AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  246. denominator = IS_CHAN_2GHZ(chan) ? 44 : 40;
  247. spur_freq_sd = ((bb_spur_off * 2048) / denominator) & 0x3ff;
  248. newVal = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  249. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  250. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  251. REG_WRITE(ah, AR_PHY_TIMING11, newVal);
  252. newVal = spur_subchannel_sd << AR_PHY_SFCORR_SPUR_SUBCHNL_SD_S;
  253. REG_WRITE(ah, AR_PHY_SFCORR_EXT, newVal);
  254. cur_bin = -6000;
  255. upper = bin + 100;
  256. lower = bin - 100;
  257. for (i = 0; i < 4; i++) {
  258. int pilot_mask = 0;
  259. int chan_mask = 0;
  260. int bp = 0;
  261. for (bp = 0; bp < 30; bp++) {
  262. if ((cur_bin > lower) && (cur_bin < upper)) {
  263. pilot_mask = pilot_mask | 0x1 << bp;
  264. chan_mask = chan_mask | 0x1 << bp;
  265. }
  266. cur_bin += 100;
  267. }
  268. cur_bin += inc[i];
  269. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  270. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  271. }
  272. cur_vit_mask = 6100;
  273. upper = bin + 120;
  274. lower = bin - 120;
  275. for (i = 0; i < 123; i++) {
  276. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  277. /* workaround for gcc bug #37014 */
  278. volatile int tmp_v = abs(cur_vit_mask - bin);
  279. if (tmp_v < 75)
  280. mask_amt = 1;
  281. else
  282. mask_amt = 0;
  283. if (cur_vit_mask < 0)
  284. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  285. else
  286. mask_p[cur_vit_mask / 100] = mask_amt;
  287. }
  288. cur_vit_mask -= 100;
  289. }
  290. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  291. | (mask_m[48] << 26) | (mask_m[49] << 24)
  292. | (mask_m[50] << 22) | (mask_m[51] << 20)
  293. | (mask_m[52] << 18) | (mask_m[53] << 16)
  294. | (mask_m[54] << 14) | (mask_m[55] << 12)
  295. | (mask_m[56] << 10) | (mask_m[57] << 8)
  296. | (mask_m[58] << 6) | (mask_m[59] << 4)
  297. | (mask_m[60] << 2) | (mask_m[61] << 0);
  298. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  299. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  300. tmp_mask = (mask_m[31] << 28)
  301. | (mask_m[32] << 26) | (mask_m[33] << 24)
  302. | (mask_m[34] << 22) | (mask_m[35] << 20)
  303. | (mask_m[36] << 18) | (mask_m[37] << 16)
  304. | (mask_m[48] << 14) | (mask_m[39] << 12)
  305. | (mask_m[40] << 10) | (mask_m[41] << 8)
  306. | (mask_m[42] << 6) | (mask_m[43] << 4)
  307. | (mask_m[44] << 2) | (mask_m[45] << 0);
  308. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  309. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  310. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  311. | (mask_m[18] << 26) | (mask_m[18] << 24)
  312. | (mask_m[20] << 22) | (mask_m[20] << 20)
  313. | (mask_m[22] << 18) | (mask_m[22] << 16)
  314. | (mask_m[24] << 14) | (mask_m[24] << 12)
  315. | (mask_m[25] << 10) | (mask_m[26] << 8)
  316. | (mask_m[27] << 6) | (mask_m[28] << 4)
  317. | (mask_m[29] << 2) | (mask_m[30] << 0);
  318. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  319. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  320. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  321. | (mask_m[2] << 26) | (mask_m[3] << 24)
  322. | (mask_m[4] << 22) | (mask_m[5] << 20)
  323. | (mask_m[6] << 18) | (mask_m[7] << 16)
  324. | (mask_m[8] << 14) | (mask_m[9] << 12)
  325. | (mask_m[10] << 10) | (mask_m[11] << 8)
  326. | (mask_m[12] << 6) | (mask_m[13] << 4)
  327. | (mask_m[14] << 2) | (mask_m[15] << 0);
  328. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  329. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  330. tmp_mask = (mask_p[15] << 28)
  331. | (mask_p[14] << 26) | (mask_p[13] << 24)
  332. | (mask_p[12] << 22) | (mask_p[11] << 20)
  333. | (mask_p[10] << 18) | (mask_p[9] << 16)
  334. | (mask_p[8] << 14) | (mask_p[7] << 12)
  335. | (mask_p[6] << 10) | (mask_p[5] << 8)
  336. | (mask_p[4] << 6) | (mask_p[3] << 4)
  337. | (mask_p[2] << 2) | (mask_p[1] << 0);
  338. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  339. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  340. tmp_mask = (mask_p[30] << 28)
  341. | (mask_p[29] << 26) | (mask_p[28] << 24)
  342. | (mask_p[27] << 22) | (mask_p[26] << 20)
  343. | (mask_p[25] << 18) | (mask_p[24] << 16)
  344. | (mask_p[23] << 14) | (mask_p[22] << 12)
  345. | (mask_p[21] << 10) | (mask_p[20] << 8)
  346. | (mask_p[19] << 6) | (mask_p[18] << 4)
  347. | (mask_p[17] << 2) | (mask_p[16] << 0);
  348. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  349. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  350. tmp_mask = (mask_p[45] << 28)
  351. | (mask_p[44] << 26) | (mask_p[43] << 24)
  352. | (mask_p[42] << 22) | (mask_p[41] << 20)
  353. | (mask_p[40] << 18) | (mask_p[39] << 16)
  354. | (mask_p[38] << 14) | (mask_p[37] << 12)
  355. | (mask_p[36] << 10) | (mask_p[35] << 8)
  356. | (mask_p[34] << 6) | (mask_p[33] << 4)
  357. | (mask_p[32] << 2) | (mask_p[31] << 0);
  358. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  359. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  360. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  361. | (mask_p[59] << 26) | (mask_p[58] << 24)
  362. | (mask_p[57] << 22) | (mask_p[56] << 20)
  363. | (mask_p[55] << 18) | (mask_p[54] << 16)
  364. | (mask_p[53] << 14) | (mask_p[52] << 12)
  365. | (mask_p[51] << 10) | (mask_p[50] << 8)
  366. | (mask_p[49] << 6) | (mask_p[48] << 4)
  367. | (mask_p[47] << 2) | (mask_p[46] << 0);
  368. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  369. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  370. REGWRITE_BUFFER_FLUSH(ah);
  371. }
  372. static void ar9002_olc_init(struct ath_hw *ah)
  373. {
  374. u32 i;
  375. if (!OLC_FOR_AR9280_20_LATER)
  376. return;
  377. if (OLC_FOR_AR9287_10_LATER) {
  378. REG_SET_BIT(ah, AR_PHY_TX_PWRCTRL9,
  379. AR_PHY_TX_PWRCTRL9_RES_DC_REMOVAL);
  380. ath9k_hw_analog_shift_rmw(ah, AR9287_AN_TXPC0,
  381. AR9287_AN_TXPC0_TXPCMODE,
  382. AR9287_AN_TXPC0_TXPCMODE_S,
  383. AR9287_AN_TXPC0_TXPCMODE_TEMPSENSE);
  384. udelay(100);
  385. } else {
  386. for (i = 0; i < AR9280_TX_GAIN_TABLE_SIZE; i++)
  387. ah->originalGain[i] =
  388. MS(REG_READ(ah, AR_PHY_TX_GAIN_TBL1 + i * 4),
  389. AR_PHY_TX_GAIN);
  390. ah->PDADCdelta = 0;
  391. }
  392. }
  393. static u32 ar9002_hw_compute_pll_control(struct ath_hw *ah,
  394. struct ath9k_channel *chan)
  395. {
  396. int ref_div = 5;
  397. int pll_div = 0x2c;
  398. u32 pll;
  399. if (chan && IS_CHAN_5GHZ(chan) && !IS_CHAN_A_FAST_CLOCK(ah, chan)) {
  400. if (AR_SREV_9280_20(ah)) {
  401. ref_div = 10;
  402. pll_div = 0x50;
  403. } else {
  404. pll_div = 0x28;
  405. }
  406. }
  407. pll = SM(ref_div, AR_RTC_9160_PLL_REFDIV);
  408. pll |= SM(pll_div, AR_RTC_9160_PLL_DIV);
  409. if (chan && IS_CHAN_HALF_RATE(chan))
  410. pll |= SM(0x1, AR_RTC_9160_PLL_CLKSEL);
  411. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  412. pll |= SM(0x2, AR_RTC_9160_PLL_CLKSEL);
  413. return pll;
  414. }
  415. static void ar9002_hw_do_getnf(struct ath_hw *ah,
  416. int16_t nfarray[NUM_NF_READINGS])
  417. {
  418. int16_t nf;
  419. nf = MS(REG_READ(ah, AR_PHY_CCA), AR9280_PHY_MINCCA_PWR);
  420. nfarray[0] = sign_extend32(nf, 8);
  421. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR9280_PHY_EXT_MINCCA_PWR);
  422. if (IS_CHAN_HT40(ah->curchan))
  423. nfarray[3] = sign_extend32(nf, 8);
  424. if (!(ah->rxchainmask & BIT(1)))
  425. return;
  426. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA), AR9280_PHY_CH1_MINCCA_PWR);
  427. nfarray[1] = sign_extend32(nf, 8);
  428. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR9280_PHY_CH1_EXT_MINCCA_PWR);
  429. if (IS_CHAN_HT40(ah->curchan))
  430. nfarray[4] = sign_extend32(nf, 8);
  431. }
  432. static void ar9002_hw_set_nf_limits(struct ath_hw *ah)
  433. {
  434. if (AR_SREV_9285(ah)) {
  435. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9285_2GHZ;
  436. ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9285_2GHZ;
  437. ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9285_2GHZ;
  438. } else if (AR_SREV_9287(ah)) {
  439. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9287_2GHZ;
  440. ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9287_2GHZ;
  441. ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9287_2GHZ;
  442. } else if (AR_SREV_9271(ah)) {
  443. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9271_2GHZ;
  444. ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9271_2GHZ;
  445. ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9271_2GHZ;
  446. } else {
  447. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_2GHZ;
  448. ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_2GHZ;
  449. ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_9280_2GHZ;
  450. ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9280_5GHZ;
  451. ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_9280_5GHZ;
  452. ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_9280_5GHZ;
  453. }
  454. }
  455. static void ar9002_hw_antdiv_comb_conf_get(struct ath_hw *ah,
  456. struct ath_hw_antcomb_conf *antconf)
  457. {
  458. u32 regval;
  459. regval = REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
  460. antconf->main_lna_conf = (regval & AR_PHY_9285_ANT_DIV_MAIN_LNACONF) >>
  461. AR_PHY_9285_ANT_DIV_MAIN_LNACONF_S;
  462. antconf->alt_lna_conf = (regval & AR_PHY_9285_ANT_DIV_ALT_LNACONF) >>
  463. AR_PHY_9285_ANT_DIV_ALT_LNACONF_S;
  464. antconf->fast_div_bias = (regval & AR_PHY_9285_FAST_DIV_BIAS) >>
  465. AR_PHY_9285_FAST_DIV_BIAS_S;
  466. antconf->lna1_lna2_switch_delta = -1;
  467. antconf->lna1_lna2_delta = -3;
  468. antconf->div_group = 0;
  469. }
  470. static void ar9002_hw_antdiv_comb_conf_set(struct ath_hw *ah,
  471. struct ath_hw_antcomb_conf *antconf)
  472. {
  473. u32 regval;
  474. regval = REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
  475. regval &= ~(AR_PHY_9285_ANT_DIV_MAIN_LNACONF |
  476. AR_PHY_9285_ANT_DIV_ALT_LNACONF |
  477. AR_PHY_9285_FAST_DIV_BIAS);
  478. regval |= ((antconf->main_lna_conf << AR_PHY_9285_ANT_DIV_MAIN_LNACONF_S)
  479. & AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
  480. regval |= ((antconf->alt_lna_conf << AR_PHY_9285_ANT_DIV_ALT_LNACONF_S)
  481. & AR_PHY_9285_ANT_DIV_ALT_LNACONF);
  482. regval |= ((antconf->fast_div_bias << AR_PHY_9285_FAST_DIV_BIAS_S)
  483. & AR_PHY_9285_FAST_DIV_BIAS);
  484. REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regval);
  485. }
  486. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  487. static void ar9002_hw_set_bt_ant_diversity(struct ath_hw *ah, bool enable)
  488. {
  489. struct ath_btcoex_hw *btcoex = &ah->btcoex_hw;
  490. u8 antdiv_ctrl1, antdiv_ctrl2;
  491. u32 regval;
  492. if (enable) {
  493. antdiv_ctrl1 = ATH_BT_COEX_ANTDIV_CONTROL1_ENABLE;
  494. antdiv_ctrl2 = ATH_BT_COEX_ANTDIV_CONTROL2_ENABLE;
  495. /*
  496. * Don't disable BT ant to allow BB to control SWCOM.
  497. */
  498. btcoex->bt_coex_mode2 &= (~(AR_BT_DISABLE_BT_ANT));
  499. REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex->bt_coex_mode2);
  500. REG_WRITE(ah, AR_PHY_SWITCH_COM, ATH_BT_COEX_ANT_DIV_SWITCH_COM);
  501. REG_RMW(ah, AR_PHY_SWITCH_CHAIN_0, 0, 0xf0000000);
  502. } else {
  503. /*
  504. * Disable antenna diversity, use LNA1 only.
  505. */
  506. antdiv_ctrl1 = ATH_BT_COEX_ANTDIV_CONTROL1_FIXED_A;
  507. antdiv_ctrl2 = ATH_BT_COEX_ANTDIV_CONTROL2_FIXED_A;
  508. /*
  509. * Disable BT Ant. to allow concurrent BT and WLAN receive.
  510. */
  511. btcoex->bt_coex_mode2 |= AR_BT_DISABLE_BT_ANT;
  512. REG_WRITE(ah, AR_BT_COEX_MODE2, btcoex->bt_coex_mode2);
  513. /*
  514. * Program SWCOM table to make sure RF switch always parks
  515. * at BT side.
  516. */
  517. REG_WRITE(ah, AR_PHY_SWITCH_COM, 0);
  518. REG_RMW(ah, AR_PHY_SWITCH_CHAIN_0, 0, 0xf0000000);
  519. }
  520. regval = REG_READ(ah, AR_PHY_MULTICHAIN_GAIN_CTL);
  521. regval &= (~(AR_PHY_9285_ANT_DIV_CTL_ALL));
  522. /*
  523. * Clear ant_fast_div_bias [14:9] since for WB195,
  524. * the main LNA is always LNA1.
  525. */
  526. regval &= (~(AR_PHY_9285_FAST_DIV_BIAS));
  527. regval |= SM(antdiv_ctrl1, AR_PHY_9285_ANT_DIV_CTL);
  528. regval |= SM(antdiv_ctrl2, AR_PHY_9285_ANT_DIV_ALT_LNACONF);
  529. regval |= SM((antdiv_ctrl2 >> 2), AR_PHY_9285_ANT_DIV_MAIN_LNACONF);
  530. regval |= SM((antdiv_ctrl1 >> 1), AR_PHY_9285_ANT_DIV_ALT_GAINTB);
  531. regval |= SM((antdiv_ctrl1 >> 2), AR_PHY_9285_ANT_DIV_MAIN_GAINTB);
  532. REG_WRITE(ah, AR_PHY_MULTICHAIN_GAIN_CTL, regval);
  533. regval = REG_READ(ah, AR_PHY_CCK_DETECT);
  534. regval &= (~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
  535. regval |= SM((antdiv_ctrl1 >> 3), AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV);
  536. REG_WRITE(ah, AR_PHY_CCK_DETECT, regval);
  537. }
  538. #endif
  539. static void ar9002_hw_spectral_scan_config(struct ath_hw *ah,
  540. struct ath_spec_scan *param)
  541. {
  542. u8 count;
  543. if (!param->enabled) {
  544. REG_CLR_BIT(ah, AR_PHY_SPECTRAL_SCAN,
  545. AR_PHY_SPECTRAL_SCAN_ENABLE);
  546. return;
  547. }
  548. REG_SET_BIT(ah, AR_PHY_RADAR_0, AR_PHY_RADAR_0_FFT_ENA);
  549. REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN, AR_PHY_SPECTRAL_SCAN_ENABLE);
  550. if (param->short_repeat)
  551. REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN,
  552. AR_PHY_SPECTRAL_SCAN_SHORT_REPEAT);
  553. else
  554. REG_CLR_BIT(ah, AR_PHY_SPECTRAL_SCAN,
  555. AR_PHY_SPECTRAL_SCAN_SHORT_REPEAT);
  556. /* on AR92xx, the highest bit of count will make the the chip send
  557. * spectral samples endlessly. Check if this really was intended,
  558. * and fix otherwise.
  559. */
  560. count = param->count;
  561. if (param->endless) {
  562. if (AR_SREV_9271(ah))
  563. count = 0;
  564. else
  565. count = 0x80;
  566. } else if (count & 0x80)
  567. count = 0x7f;
  568. REG_RMW_FIELD(ah, AR_PHY_SPECTRAL_SCAN,
  569. AR_PHY_SPECTRAL_SCAN_COUNT, count);
  570. REG_RMW_FIELD(ah, AR_PHY_SPECTRAL_SCAN,
  571. AR_PHY_SPECTRAL_SCAN_PERIOD, param->period);
  572. REG_RMW_FIELD(ah, AR_PHY_SPECTRAL_SCAN,
  573. AR_PHY_SPECTRAL_SCAN_FFT_PERIOD, param->fft_period);
  574. return;
  575. }
  576. static void ar9002_hw_spectral_scan_trigger(struct ath_hw *ah)
  577. {
  578. REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN, AR_PHY_SPECTRAL_SCAN_ENABLE);
  579. /* Activate spectral scan */
  580. REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN,
  581. AR_PHY_SPECTRAL_SCAN_ACTIVE);
  582. }
  583. static void ar9002_hw_spectral_scan_wait(struct ath_hw *ah)
  584. {
  585. struct ath_common *common = ath9k_hw_common(ah);
  586. /* Poll for spectral scan complete */
  587. if (!ath9k_hw_wait(ah, AR_PHY_SPECTRAL_SCAN,
  588. AR_PHY_SPECTRAL_SCAN_ACTIVE,
  589. 0, AH_WAIT_TIMEOUT)) {
  590. ath_err(common, "spectral scan wait failed\n");
  591. return;
  592. }
  593. }
  594. static void ar9002_hw_tx99_start(struct ath_hw *ah, u32 qnum)
  595. {
  596. REG_SET_BIT(ah, 0x9864, 0x7f000);
  597. REG_SET_BIT(ah, 0x9924, 0x7f00fe);
  598. REG_CLR_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
  599. REG_WRITE(ah, AR_CR, AR_CR_RXD);
  600. REG_WRITE(ah, AR_DLCL_IFS(qnum), 0);
  601. REG_WRITE(ah, AR_D_GBL_IFS_SIFS, 20);
  602. REG_WRITE(ah, AR_D_GBL_IFS_EIFS, 20);
  603. REG_WRITE(ah, AR_D_FPCTL, 0x10|qnum);
  604. REG_WRITE(ah, AR_TIME_OUT, 0x00000400);
  605. REG_WRITE(ah, AR_DRETRY_LIMIT(qnum), 0xffffffff);
  606. REG_SET_BIT(ah, AR_QMISC(qnum), AR_Q_MISC_DCU_EARLY_TERM_REQ);
  607. }
  608. static void ar9002_hw_tx99_stop(struct ath_hw *ah)
  609. {
  610. REG_SET_BIT(ah, AR_DIAG_SW, AR_DIAG_RX_DIS);
  611. }
  612. void ar9002_hw_attach_phy_ops(struct ath_hw *ah)
  613. {
  614. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  615. struct ath_hw_ops *ops = ath9k_hw_ops(ah);
  616. priv_ops->set_rf_regs = NULL;
  617. priv_ops->rf_set_freq = ar9002_hw_set_channel;
  618. priv_ops->spur_mitigate_freq = ar9002_hw_spur_mitigate;
  619. priv_ops->olc_init = ar9002_olc_init;
  620. priv_ops->compute_pll_control = ar9002_hw_compute_pll_control;
  621. priv_ops->do_getnf = ar9002_hw_do_getnf;
  622. ops->antdiv_comb_conf_get = ar9002_hw_antdiv_comb_conf_get;
  623. ops->antdiv_comb_conf_set = ar9002_hw_antdiv_comb_conf_set;
  624. ops->spectral_scan_config = ar9002_hw_spectral_scan_config;
  625. ops->spectral_scan_trigger = ar9002_hw_spectral_scan_trigger;
  626. ops->spectral_scan_wait = ar9002_hw_spectral_scan_wait;
  627. #ifdef CONFIG_ATH9K_BTCOEX_SUPPORT
  628. ops->set_bt_ant_diversity = ar9002_hw_set_bt_ant_diversity;
  629. #endif
  630. ops->tx99_start = ar9002_hw_tx99_start;
  631. ops->tx99_stop = ar9002_hw_tx99_stop;
  632. ar9002_hw_set_nf_limits(ah);
  633. }