ar5008_phy.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388
  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. #include "hw.h"
  17. #include "hw-ops.h"
  18. #include "../regd.h"
  19. #include "ar9002_phy.h"
  20. /* All code below is for AR5008, AR9001, AR9002 */
  21. #define AR5008_OFDM_RATES 8
  22. #define AR5008_HT_SS_RATES 8
  23. #define AR5008_HT_DS_RATES 8
  24. #define AR5008_HT20_SHIFT 16
  25. #define AR5008_HT40_SHIFT 24
  26. #define AR5008_11NA_OFDM_SHIFT 0
  27. #define AR5008_11NA_HT_SS_SHIFT 8
  28. #define AR5008_11NA_HT_DS_SHIFT 16
  29. #define AR5008_11NG_OFDM_SHIFT 4
  30. #define AR5008_11NG_HT_SS_SHIFT 12
  31. #define AR5008_11NG_HT_DS_SHIFT 20
  32. /*
  33. * register values to turn OFDM weak signal detection OFF
  34. */
  35. static const int m1ThreshLow_off = 127;
  36. static const int m2ThreshLow_off = 127;
  37. static const int m1Thresh_off = 127;
  38. static const int m2Thresh_off = 127;
  39. static const int m2CountThr_off = 31;
  40. static const int m2CountThrLow_off = 63;
  41. static const int m1ThreshLowExt_off = 127;
  42. static const int m2ThreshLowExt_off = 127;
  43. static const int m1ThreshExt_off = 127;
  44. static const int m2ThreshExt_off = 127;
  45. static const u32 ar5416Bank0[][2] = {
  46. /* Addr allmodes */
  47. {0x000098b0, 0x1e5795e5},
  48. {0x000098e0, 0x02008020},
  49. };
  50. static const u32 ar5416Bank1[][2] = {
  51. /* Addr allmodes */
  52. {0x000098b0, 0x02108421},
  53. {0x000098ec, 0x00000008},
  54. };
  55. static const u32 ar5416Bank2[][2] = {
  56. /* Addr allmodes */
  57. {0x000098b0, 0x0e73ff17},
  58. {0x000098e0, 0x00000420},
  59. };
  60. static const u32 ar5416Bank3[][3] = {
  61. /* Addr 5G 2G */
  62. {0x000098f0, 0x01400018, 0x01c00018},
  63. };
  64. static const u32 ar5416Bank7[][2] = {
  65. /* Addr allmodes */
  66. {0x0000989c, 0x00000500},
  67. {0x0000989c, 0x00000800},
  68. {0x000098cc, 0x0000000e},
  69. };
  70. static const struct ar5416IniArray bank0 = STATIC_INI_ARRAY(ar5416Bank0);
  71. static const struct ar5416IniArray bank1 = STATIC_INI_ARRAY(ar5416Bank1);
  72. static const struct ar5416IniArray bank2 = STATIC_INI_ARRAY(ar5416Bank2);
  73. static const struct ar5416IniArray bank3 = STATIC_INI_ARRAY(ar5416Bank3);
  74. static const struct ar5416IniArray bank7 = STATIC_INI_ARRAY(ar5416Bank7);
  75. static void ar5008_write_bank6(struct ath_hw *ah, unsigned int *writecnt)
  76. {
  77. struct ar5416IniArray *array = &ah->iniBank6;
  78. u32 *data = ah->analogBank6Data;
  79. int r;
  80. ENABLE_REGWRITE_BUFFER(ah);
  81. for (r = 0; r < array->ia_rows; r++) {
  82. REG_WRITE(ah, INI_RA(array, r, 0), data[r]);
  83. DO_DELAY(*writecnt);
  84. }
  85. REGWRITE_BUFFER_FLUSH(ah);
  86. }
  87. /*
  88. * ar5008_hw_phy_modify_rx_buffer() - perform analog swizzling of parameters
  89. *
  90. * Performs analog "swizzling" of parameters into their location.
  91. * Used on external AR2133/AR5133 radios.
  92. */
  93. static void ar5008_hw_phy_modify_rx_buffer(u32 *rfBuf, u32 reg32,
  94. u32 numBits, u32 firstBit,
  95. u32 column)
  96. {
  97. u32 tmp32, mask, arrayEntry, lastBit;
  98. int32_t bitPosition, bitsLeft;
  99. tmp32 = ath9k_hw_reverse_bits(reg32, numBits);
  100. arrayEntry = (firstBit - 1) / 8;
  101. bitPosition = (firstBit - 1) % 8;
  102. bitsLeft = numBits;
  103. while (bitsLeft > 0) {
  104. lastBit = (bitPosition + bitsLeft > 8) ?
  105. 8 : bitPosition + bitsLeft;
  106. mask = (((1 << lastBit) - 1) ^ ((1 << bitPosition) - 1)) <<
  107. (column * 8);
  108. rfBuf[arrayEntry] &= ~mask;
  109. rfBuf[arrayEntry] |= ((tmp32 << bitPosition) <<
  110. (column * 8)) & mask;
  111. bitsLeft -= 8 - bitPosition;
  112. tmp32 = tmp32 >> (8 - bitPosition);
  113. bitPosition = 0;
  114. arrayEntry++;
  115. }
  116. }
  117. /*
  118. * Fix on 2.4 GHz band for orientation sensitivity issue by increasing
  119. * rf_pwd_icsyndiv.
  120. *
  121. * Theoretical Rules:
  122. * if 2 GHz band
  123. * if forceBiasAuto
  124. * if synth_freq < 2412
  125. * bias = 0
  126. * else if 2412 <= synth_freq <= 2422
  127. * bias = 1
  128. * else // synth_freq > 2422
  129. * bias = 2
  130. * else if forceBias > 0
  131. * bias = forceBias & 7
  132. * else
  133. * no change, use value from ini file
  134. * else
  135. * no change, invalid band
  136. *
  137. * 1st Mod:
  138. * 2422 also uses value of 2
  139. * <approved>
  140. *
  141. * 2nd Mod:
  142. * Less than 2412 uses value of 0, 2412 and above uses value of 2
  143. */
  144. static void ar5008_hw_force_bias(struct ath_hw *ah, u16 synth_freq)
  145. {
  146. struct ath_common *common = ath9k_hw_common(ah);
  147. u32 tmp_reg;
  148. int reg_writes = 0;
  149. u32 new_bias = 0;
  150. if (!AR_SREV_5416(ah) || synth_freq >= 3000)
  151. return;
  152. BUG_ON(AR_SREV_9280_20_OR_LATER(ah));
  153. if (synth_freq < 2412)
  154. new_bias = 0;
  155. else if (synth_freq < 2422)
  156. new_bias = 1;
  157. else
  158. new_bias = 2;
  159. /* pre-reverse this field */
  160. tmp_reg = ath9k_hw_reverse_bits(new_bias, 3);
  161. ath_dbg(common, CONFIG, "Force rf_pwd_icsyndiv to %1d on %4d\n",
  162. new_bias, synth_freq);
  163. /* swizzle rf_pwd_icsyndiv */
  164. ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data, tmp_reg, 3, 181, 3);
  165. /* write Bank 6 with new params */
  166. ar5008_write_bank6(ah, &reg_writes);
  167. }
  168. /*
  169. * ar5008_hw_set_channel - tune to a channel on the external AR2133/AR5133 radios
  170. *
  171. * For the external AR2133/AR5133 radios, takes the MHz channel value and set
  172. * the channel value. Assumes writes enabled to analog bus and bank6 register
  173. * cache in ah->analogBank6Data.
  174. */
  175. static int ar5008_hw_set_channel(struct ath_hw *ah, struct ath9k_channel *chan)
  176. {
  177. struct ath_common *common = ath9k_hw_common(ah);
  178. u32 channelSel = 0;
  179. u32 bModeSynth = 0;
  180. u32 aModeRefSel = 0;
  181. u32 reg32 = 0;
  182. u16 freq;
  183. struct chan_centers centers;
  184. ath9k_hw_get_channel_centers(ah, chan, &centers);
  185. freq = centers.synth_center;
  186. if (freq < 4800) {
  187. u32 txctl;
  188. if (((freq - 2192) % 5) == 0) {
  189. channelSel = ((freq - 672) * 2 - 3040) / 10;
  190. bModeSynth = 0;
  191. } else if (((freq - 2224) % 5) == 0) {
  192. channelSel = ((freq - 704) * 2 - 3040) / 10;
  193. bModeSynth = 1;
  194. } else {
  195. ath_err(common, "Invalid channel %u MHz\n", freq);
  196. return -EINVAL;
  197. }
  198. channelSel = (channelSel << 2) & 0xff;
  199. channelSel = ath9k_hw_reverse_bits(channelSel, 8);
  200. txctl = REG_READ(ah, AR_PHY_CCK_TX_CTRL);
  201. if (freq == 2484) {
  202. REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
  203. txctl | AR_PHY_CCK_TX_CTRL_JAPAN);
  204. } else {
  205. REG_WRITE(ah, AR_PHY_CCK_TX_CTRL,
  206. txctl & ~AR_PHY_CCK_TX_CTRL_JAPAN);
  207. }
  208. } else if ((freq % 20) == 0 && freq >= 5120) {
  209. channelSel =
  210. ath9k_hw_reverse_bits(((freq - 4800) / 20 << 2), 8);
  211. aModeRefSel = ath9k_hw_reverse_bits(1, 2);
  212. } else if ((freq % 10) == 0) {
  213. channelSel =
  214. ath9k_hw_reverse_bits(((freq - 4800) / 10 << 1), 8);
  215. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah))
  216. aModeRefSel = ath9k_hw_reverse_bits(2, 2);
  217. else
  218. aModeRefSel = ath9k_hw_reverse_bits(1, 2);
  219. } else if ((freq % 5) == 0) {
  220. channelSel = ath9k_hw_reverse_bits((freq - 4800) / 5, 8);
  221. aModeRefSel = ath9k_hw_reverse_bits(1, 2);
  222. } else {
  223. ath_err(common, "Invalid channel %u MHz\n", freq);
  224. return -EINVAL;
  225. }
  226. ar5008_hw_force_bias(ah, freq);
  227. reg32 =
  228. (channelSel << 8) | (aModeRefSel << 2) | (bModeSynth << 1) |
  229. (1 << 5) | 0x1;
  230. REG_WRITE(ah, AR_PHY(0x37), reg32);
  231. ah->curchan = chan;
  232. return 0;
  233. }
  234. void ar5008_hw_cmn_spur_mitigate(struct ath_hw *ah,
  235. struct ath9k_channel *chan, int bin)
  236. {
  237. int cur_bin;
  238. int upper, lower, cur_vit_mask;
  239. int i;
  240. int8_t mask_m[123] = {0};
  241. int8_t mask_p[123] = {0};
  242. int8_t mask_amt;
  243. int tmp_mask;
  244. static const int pilot_mask_reg[4] = {
  245. AR_PHY_TIMING7, AR_PHY_TIMING8,
  246. AR_PHY_PILOT_MASK_01_30, AR_PHY_PILOT_MASK_31_60
  247. };
  248. static const int chan_mask_reg[4] = {
  249. AR_PHY_TIMING9, AR_PHY_TIMING10,
  250. AR_PHY_CHANNEL_MASK_01_30, AR_PHY_CHANNEL_MASK_31_60
  251. };
  252. static const int inc[4] = { 0, 100, 0, 0 };
  253. cur_bin = -6000;
  254. upper = bin + 100;
  255. lower = bin - 100;
  256. for (i = 0; i < 4; i++) {
  257. int pilot_mask = 0;
  258. int chan_mask = 0;
  259. int bp = 0;
  260. for (bp = 0; bp < 30; bp++) {
  261. if ((cur_bin > lower) && (cur_bin < upper)) {
  262. pilot_mask = pilot_mask | 0x1 << bp;
  263. chan_mask = chan_mask | 0x1 << bp;
  264. }
  265. cur_bin += 100;
  266. }
  267. cur_bin += inc[i];
  268. REG_WRITE(ah, pilot_mask_reg[i], pilot_mask);
  269. REG_WRITE(ah, chan_mask_reg[i], chan_mask);
  270. }
  271. cur_vit_mask = 6100;
  272. upper = bin + 120;
  273. lower = bin - 120;
  274. for (i = 0; i < ARRAY_SIZE(mask_m); i++) {
  275. if ((cur_vit_mask > lower) && (cur_vit_mask < upper)) {
  276. /* workaround for gcc bug #37014 */
  277. volatile int tmp_v = abs(cur_vit_mask - bin);
  278. if (tmp_v < 75)
  279. mask_amt = 1;
  280. else
  281. mask_amt = 0;
  282. if (cur_vit_mask < 0)
  283. mask_m[abs(cur_vit_mask / 100)] = mask_amt;
  284. else
  285. mask_p[cur_vit_mask / 100] = mask_amt;
  286. }
  287. cur_vit_mask -= 100;
  288. }
  289. tmp_mask = (mask_m[46] << 30) | (mask_m[47] << 28)
  290. | (mask_m[48] << 26) | (mask_m[49] << 24)
  291. | (mask_m[50] << 22) | (mask_m[51] << 20)
  292. | (mask_m[52] << 18) | (mask_m[53] << 16)
  293. | (mask_m[54] << 14) | (mask_m[55] << 12)
  294. | (mask_m[56] << 10) | (mask_m[57] << 8)
  295. | (mask_m[58] << 6) | (mask_m[59] << 4)
  296. | (mask_m[60] << 2) | (mask_m[61] << 0);
  297. REG_WRITE(ah, AR_PHY_BIN_MASK_1, tmp_mask);
  298. REG_WRITE(ah, AR_PHY_VIT_MASK2_M_46_61, tmp_mask);
  299. tmp_mask = (mask_m[31] << 28)
  300. | (mask_m[32] << 26) | (mask_m[33] << 24)
  301. | (mask_m[34] << 22) | (mask_m[35] << 20)
  302. | (mask_m[36] << 18) | (mask_m[37] << 16)
  303. | (mask_m[48] << 14) | (mask_m[39] << 12)
  304. | (mask_m[40] << 10) | (mask_m[41] << 8)
  305. | (mask_m[42] << 6) | (mask_m[43] << 4)
  306. | (mask_m[44] << 2) | (mask_m[45] << 0);
  307. REG_WRITE(ah, AR_PHY_BIN_MASK_2, tmp_mask);
  308. REG_WRITE(ah, AR_PHY_MASK2_M_31_45, tmp_mask);
  309. tmp_mask = (mask_m[16] << 30) | (mask_m[16] << 28)
  310. | (mask_m[18] << 26) | (mask_m[18] << 24)
  311. | (mask_m[20] << 22) | (mask_m[20] << 20)
  312. | (mask_m[22] << 18) | (mask_m[22] << 16)
  313. | (mask_m[24] << 14) | (mask_m[24] << 12)
  314. | (mask_m[25] << 10) | (mask_m[26] << 8)
  315. | (mask_m[27] << 6) | (mask_m[28] << 4)
  316. | (mask_m[29] << 2) | (mask_m[30] << 0);
  317. REG_WRITE(ah, AR_PHY_BIN_MASK_3, tmp_mask);
  318. REG_WRITE(ah, AR_PHY_MASK2_M_16_30, tmp_mask);
  319. tmp_mask = (mask_m[0] << 30) | (mask_m[1] << 28)
  320. | (mask_m[2] << 26) | (mask_m[3] << 24)
  321. | (mask_m[4] << 22) | (mask_m[5] << 20)
  322. | (mask_m[6] << 18) | (mask_m[7] << 16)
  323. | (mask_m[8] << 14) | (mask_m[9] << 12)
  324. | (mask_m[10] << 10) | (mask_m[11] << 8)
  325. | (mask_m[12] << 6) | (mask_m[13] << 4)
  326. | (mask_m[14] << 2) | (mask_m[15] << 0);
  327. REG_WRITE(ah, AR_PHY_MASK_CTL, tmp_mask);
  328. REG_WRITE(ah, AR_PHY_MASK2_M_00_15, tmp_mask);
  329. tmp_mask = (mask_p[15] << 28)
  330. | (mask_p[14] << 26) | (mask_p[13] << 24)
  331. | (mask_p[12] << 22) | (mask_p[11] << 20)
  332. | (mask_p[10] << 18) | (mask_p[9] << 16)
  333. | (mask_p[8] << 14) | (mask_p[7] << 12)
  334. | (mask_p[6] << 10) | (mask_p[5] << 8)
  335. | (mask_p[4] << 6) | (mask_p[3] << 4)
  336. | (mask_p[2] << 2) | (mask_p[1] << 0);
  337. REG_WRITE(ah, AR_PHY_BIN_MASK2_1, tmp_mask);
  338. REG_WRITE(ah, AR_PHY_MASK2_P_15_01, tmp_mask);
  339. tmp_mask = (mask_p[30] << 28)
  340. | (mask_p[29] << 26) | (mask_p[28] << 24)
  341. | (mask_p[27] << 22) | (mask_p[26] << 20)
  342. | (mask_p[25] << 18) | (mask_p[24] << 16)
  343. | (mask_p[23] << 14) | (mask_p[22] << 12)
  344. | (mask_p[21] << 10) | (mask_p[20] << 8)
  345. | (mask_p[19] << 6) | (mask_p[18] << 4)
  346. | (mask_p[17] << 2) | (mask_p[16] << 0);
  347. REG_WRITE(ah, AR_PHY_BIN_MASK2_2, tmp_mask);
  348. REG_WRITE(ah, AR_PHY_MASK2_P_30_16, tmp_mask);
  349. tmp_mask = (mask_p[45] << 28)
  350. | (mask_p[44] << 26) | (mask_p[43] << 24)
  351. | (mask_p[42] << 22) | (mask_p[41] << 20)
  352. | (mask_p[40] << 18) | (mask_p[39] << 16)
  353. | (mask_p[38] << 14) | (mask_p[37] << 12)
  354. | (mask_p[36] << 10) | (mask_p[35] << 8)
  355. | (mask_p[34] << 6) | (mask_p[33] << 4)
  356. | (mask_p[32] << 2) | (mask_p[31] << 0);
  357. REG_WRITE(ah, AR_PHY_BIN_MASK2_3, tmp_mask);
  358. REG_WRITE(ah, AR_PHY_MASK2_P_45_31, tmp_mask);
  359. tmp_mask = (mask_p[61] << 30) | (mask_p[60] << 28)
  360. | (mask_p[59] << 26) | (mask_p[58] << 24)
  361. | (mask_p[57] << 22) | (mask_p[56] << 20)
  362. | (mask_p[55] << 18) | (mask_p[54] << 16)
  363. | (mask_p[53] << 14) | (mask_p[52] << 12)
  364. | (mask_p[51] << 10) | (mask_p[50] << 8)
  365. | (mask_p[49] << 6) | (mask_p[48] << 4)
  366. | (mask_p[47] << 2) | (mask_p[46] << 0);
  367. REG_WRITE(ah, AR_PHY_BIN_MASK2_4, tmp_mask);
  368. REG_WRITE(ah, AR_PHY_MASK2_P_61_45, tmp_mask);
  369. }
  370. /*
  371. * ar5008_hw_spur_mitigate - convert baseband spur frequency for external radios
  372. *
  373. * For non single-chip solutions. Converts to baseband spur frequency given the
  374. * input channel frequency and compute register settings below.
  375. */
  376. static void ar5008_hw_spur_mitigate(struct ath_hw *ah,
  377. struct ath9k_channel *chan)
  378. {
  379. int bb_spur = AR_NO_SPUR;
  380. int bin;
  381. int spur_freq_sd;
  382. int spur_delta_phase;
  383. int denominator;
  384. int tmp, new;
  385. int i;
  386. int cur_bb_spur;
  387. bool is2GHz = IS_CHAN_2GHZ(chan);
  388. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  389. cur_bb_spur = ah->eep_ops->get_spur_channel(ah, i, is2GHz);
  390. if (AR_NO_SPUR == cur_bb_spur)
  391. break;
  392. cur_bb_spur = cur_bb_spur - (chan->channel * 10);
  393. if ((cur_bb_spur > -95) && (cur_bb_spur < 95)) {
  394. bb_spur = cur_bb_spur;
  395. break;
  396. }
  397. }
  398. if (AR_NO_SPUR == bb_spur)
  399. return;
  400. bin = bb_spur * 32;
  401. tmp = REG_READ(ah, AR_PHY_TIMING_CTRL4(0));
  402. new = tmp | (AR_PHY_TIMING_CTRL4_ENABLE_SPUR_RSSI |
  403. AR_PHY_TIMING_CTRL4_ENABLE_SPUR_FILTER |
  404. AR_PHY_TIMING_CTRL4_ENABLE_CHAN_MASK |
  405. AR_PHY_TIMING_CTRL4_ENABLE_PILOT_MASK);
  406. REG_WRITE(ah, AR_PHY_TIMING_CTRL4(0), new);
  407. new = (AR_PHY_SPUR_REG_MASK_RATE_CNTL |
  408. AR_PHY_SPUR_REG_ENABLE_MASK_PPM |
  409. AR_PHY_SPUR_REG_MASK_RATE_SELECT |
  410. AR_PHY_SPUR_REG_ENABLE_VIT_SPUR_RSSI |
  411. SM(SPUR_RSSI_THRESH, AR_PHY_SPUR_REG_SPUR_RSSI_THRESH));
  412. REG_WRITE(ah, AR_PHY_SPUR_REG, new);
  413. spur_delta_phase = ((bb_spur * 524288) / 100) &
  414. AR_PHY_TIMING11_SPUR_DELTA_PHASE;
  415. denominator = IS_CHAN_2GHZ(chan) ? 440 : 400;
  416. spur_freq_sd = ((bb_spur * 2048) / denominator) & 0x3ff;
  417. new = (AR_PHY_TIMING11_USE_SPUR_IN_AGC |
  418. SM(spur_freq_sd, AR_PHY_TIMING11_SPUR_FREQ_SD) |
  419. SM(spur_delta_phase, AR_PHY_TIMING11_SPUR_DELTA_PHASE));
  420. REG_WRITE(ah, AR_PHY_TIMING11, new);
  421. ar5008_hw_cmn_spur_mitigate(ah, chan, bin);
  422. }
  423. /**
  424. * ar5008_hw_rf_alloc_ext_banks - allocates banks for external radio programming
  425. * @ah: atheros hardware structure
  426. *
  427. * Only required for older devices with external AR2133/AR5133 radios.
  428. */
  429. static int ar5008_hw_rf_alloc_ext_banks(struct ath_hw *ah)
  430. {
  431. int size = ah->iniBank6.ia_rows * sizeof(u32);
  432. if (AR_SREV_9280_20_OR_LATER(ah))
  433. return 0;
  434. ah->analogBank6Data = devm_kzalloc(ah->dev, size, GFP_KERNEL);
  435. if (!ah->analogBank6Data)
  436. return -ENOMEM;
  437. return 0;
  438. }
  439. /* *
  440. * ar5008_hw_set_rf_regs - programs rf registers based on EEPROM
  441. * @ah: atheros hardware structure
  442. * @chan:
  443. * @modesIndex:
  444. *
  445. * Used for the external AR2133/AR5133 radios.
  446. *
  447. * Reads the EEPROM header info from the device structure and programs
  448. * all rf registers. This routine requires access to the analog
  449. * rf device. This is not required for single-chip devices.
  450. */
  451. static bool ar5008_hw_set_rf_regs(struct ath_hw *ah,
  452. struct ath9k_channel *chan,
  453. u16 modesIndex)
  454. {
  455. u32 eepMinorRev;
  456. u32 ob5GHz = 0, db5GHz = 0;
  457. u32 ob2GHz = 0, db2GHz = 0;
  458. int regWrites = 0;
  459. int i;
  460. /*
  461. * Software does not need to program bank data
  462. * for single chip devices, that is AR9280 or anything
  463. * after that.
  464. */
  465. if (AR_SREV_9280_20_OR_LATER(ah))
  466. return true;
  467. /* Setup rf parameters */
  468. eepMinorRev = ah->eep_ops->get_eeprom_rev(ah);
  469. for (i = 0; i < ah->iniBank6.ia_rows; i++)
  470. ah->analogBank6Data[i] = INI_RA(&ah->iniBank6, i, modesIndex);
  471. /* Only the 5 or 2 GHz OB/DB need to be set for a mode */
  472. if (eepMinorRev >= 2) {
  473. if (IS_CHAN_2GHZ(chan)) {
  474. ob2GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_2);
  475. db2GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_2);
  476. ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
  477. ob2GHz, 3, 197, 0);
  478. ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
  479. db2GHz, 3, 194, 0);
  480. } else {
  481. ob5GHz = ah->eep_ops->get_eeprom(ah, EEP_OB_5);
  482. db5GHz = ah->eep_ops->get_eeprom(ah, EEP_DB_5);
  483. ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
  484. ob5GHz, 3, 203, 0);
  485. ar5008_hw_phy_modify_rx_buffer(ah->analogBank6Data,
  486. db5GHz, 3, 200, 0);
  487. }
  488. }
  489. /* Write Analog registers */
  490. REG_WRITE_ARRAY(&bank0, 1, regWrites);
  491. REG_WRITE_ARRAY(&bank1, 1, regWrites);
  492. REG_WRITE_ARRAY(&bank2, 1, regWrites);
  493. REG_WRITE_ARRAY(&bank3, modesIndex, regWrites);
  494. ar5008_write_bank6(ah, &regWrites);
  495. REG_WRITE_ARRAY(&bank7, 1, regWrites);
  496. return true;
  497. }
  498. static void ar5008_hw_init_bb(struct ath_hw *ah,
  499. struct ath9k_channel *chan)
  500. {
  501. u32 synthDelay;
  502. synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  503. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  504. ath9k_hw_synth_delay(ah, chan, synthDelay);
  505. }
  506. static void ar5008_hw_init_chain_masks(struct ath_hw *ah)
  507. {
  508. int rx_chainmask, tx_chainmask;
  509. rx_chainmask = ah->rxchainmask;
  510. tx_chainmask = ah->txchainmask;
  511. switch (rx_chainmask) {
  512. case 0x5:
  513. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  514. AR_PHY_SWAP_ALT_CHAIN);
  515. fallthrough;
  516. case 0x3:
  517. if (ah->hw_version.macVersion == AR_SREV_REVISION_5416_10) {
  518. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, 0x7);
  519. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, 0x7);
  520. break;
  521. }
  522. fallthrough;
  523. case 0x1:
  524. case 0x2:
  525. case 0x7:
  526. ENABLE_REGWRITE_BUFFER(ah);
  527. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  528. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  529. break;
  530. default:
  531. ENABLE_REGWRITE_BUFFER(ah);
  532. break;
  533. }
  534. REG_WRITE(ah, AR_SELFGEN_MASK, tx_chainmask);
  535. REGWRITE_BUFFER_FLUSH(ah);
  536. if (tx_chainmask == 0x5) {
  537. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  538. AR_PHY_SWAP_ALT_CHAIN);
  539. }
  540. if (AR_SREV_9100(ah))
  541. REG_WRITE(ah, AR_PHY_ANALOG_SWAP,
  542. REG_READ(ah, AR_PHY_ANALOG_SWAP) | 0x00000001);
  543. }
  544. static void ar5008_hw_override_ini(struct ath_hw *ah,
  545. struct ath9k_channel *chan)
  546. {
  547. u32 val;
  548. /*
  549. * Set the RX_ABORT and RX_DIS and clear if off only after
  550. * RXE is set for MAC. This prevents frames with corrupted
  551. * descriptor status.
  552. */
  553. REG_SET_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  554. if (AR_SREV_9280_20_OR_LATER(ah)) {
  555. /*
  556. * For AR9280 and above, there is a new feature that allows
  557. * Multicast search based on both MAC Address and Key ID.
  558. * By default, this feature is enabled. But since the driver
  559. * is not using this feature, we switch it off; otherwise
  560. * multicast search based on MAC addr only will fail.
  561. */
  562. val = REG_READ(ah, AR_PCU_MISC_MODE2) &
  563. (~AR_ADHOC_MCAST_KEYID_ENABLE);
  564. if (!AR_SREV_9271(ah))
  565. val &= ~AR_PCU_MISC_MODE2_HWWAR1;
  566. if (AR_SREV_9287_11_OR_LATER(ah))
  567. val = val & (~AR_PCU_MISC_MODE2_HWWAR2);
  568. val |= AR_PCU_MISC_MODE2_CFP_IGNORE;
  569. REG_WRITE(ah, AR_PCU_MISC_MODE2, val);
  570. }
  571. if (AR_SREV_9280_20_OR_LATER(ah))
  572. return;
  573. /*
  574. * Disable BB clock gating
  575. * Necessary to avoid issues on AR5416 2.0
  576. */
  577. REG_WRITE(ah, 0x9800 + (651 << 2), 0x11);
  578. /*
  579. * Disable RIFS search on some chips to avoid baseband
  580. * hang issues.
  581. */
  582. if (AR_SREV_9100(ah) || AR_SREV_9160(ah)) {
  583. val = REG_READ(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS);
  584. val &= ~AR_PHY_RIFS_INIT_DELAY;
  585. REG_WRITE(ah, AR_PHY_HEAVY_CLIP_FACTOR_RIFS, val);
  586. }
  587. }
  588. static void ar5008_hw_set_channel_regs(struct ath_hw *ah,
  589. struct ath9k_channel *chan)
  590. {
  591. u32 phymode;
  592. u32 enableDacFifo = 0;
  593. if (AR_SREV_9285_12_OR_LATER(ah))
  594. enableDacFifo = (REG_READ(ah, AR_PHY_TURBO) &
  595. AR_PHY_FC_ENABLE_DAC_FIFO);
  596. phymode = AR_PHY_FC_HT_EN | AR_PHY_FC_SHORT_GI_40
  597. | AR_PHY_FC_SINGLE_HT_LTF1 | AR_PHY_FC_WALSH | enableDacFifo;
  598. if (IS_CHAN_HT40(chan)) {
  599. phymode |= AR_PHY_FC_DYN2040_EN;
  600. if (IS_CHAN_HT40PLUS(chan))
  601. phymode |= AR_PHY_FC_DYN2040_PRI_CH;
  602. }
  603. ENABLE_REGWRITE_BUFFER(ah);
  604. REG_WRITE(ah, AR_PHY_TURBO, phymode);
  605. /* This function do only REG_WRITE, so
  606. * we can include it to REGWRITE_BUFFER. */
  607. ath9k_hw_set11nmac2040(ah, chan);
  608. REG_WRITE(ah, AR_GTXTO, 25 << AR_GTXTO_TIMEOUT_LIMIT_S);
  609. REG_WRITE(ah, AR_CST, 0xF << AR_CST_TIMEOUT_LIMIT_S);
  610. REGWRITE_BUFFER_FLUSH(ah);
  611. }
  612. static int ar5008_hw_process_ini(struct ath_hw *ah,
  613. struct ath9k_channel *chan)
  614. {
  615. struct ath_common *common = ath9k_hw_common(ah);
  616. int i, regWrites = 0;
  617. u32 modesIndex, freqIndex;
  618. if (IS_CHAN_5GHZ(chan)) {
  619. freqIndex = 1;
  620. modesIndex = IS_CHAN_HT40(chan) ? 2 : 1;
  621. } else {
  622. freqIndex = 2;
  623. modesIndex = IS_CHAN_HT40(chan) ? 3 : 4;
  624. }
  625. /*
  626. * Set correct baseband to analog shift setting to
  627. * access analog chips.
  628. */
  629. REG_WRITE(ah, AR_PHY(0), 0x00000007);
  630. /* Write ADDAC shifts */
  631. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_EXTERNAL_RADIO);
  632. if (ah->eep_ops->set_addac)
  633. ah->eep_ops->set_addac(ah, chan);
  634. REG_WRITE_ARRAY(&ah->iniAddac, 1, regWrites);
  635. REG_WRITE(ah, AR_PHY_ADC_SERIAL_CTL, AR_PHY_SEL_INTERNAL_ADDAC);
  636. ENABLE_REGWRITE_BUFFER(ah);
  637. for (i = 0; i < ah->iniModes.ia_rows; i++) {
  638. u32 reg = INI_RA(&ah->iniModes, i, 0);
  639. u32 val = INI_RA(&ah->iniModes, i, modesIndex);
  640. if (reg == AR_AN_TOP2 && ah->need_an_top2_fixup)
  641. val &= ~AR_AN_TOP2_PWDCLKIND;
  642. REG_WRITE(ah, reg, val);
  643. if (reg >= 0x7800 && reg < 0x78a0
  644. && ah->config.analog_shiftreg
  645. && (common->bus_ops->ath_bus_type != ATH_USB)) {
  646. udelay(100);
  647. }
  648. DO_DELAY(regWrites);
  649. }
  650. REGWRITE_BUFFER_FLUSH(ah);
  651. if (AR_SREV_9280(ah) || AR_SREV_9287_11_OR_LATER(ah))
  652. REG_WRITE_ARRAY(&ah->iniModesRxGain, modesIndex, regWrites);
  653. if (AR_SREV_9280(ah) || AR_SREV_9285_12_OR_LATER(ah) ||
  654. AR_SREV_9287_11_OR_LATER(ah))
  655. REG_WRITE_ARRAY(&ah->iniModesTxGain, modesIndex, regWrites);
  656. if (AR_SREV_9271_10(ah)) {
  657. REG_SET_BIT(ah, AR_PHY_SPECTRAL_SCAN, AR_PHY_SPECTRAL_SCAN_ENA);
  658. REG_RMW_FIELD(ah, AR_PHY_RF_CTL3, AR_PHY_TX_END_TO_ADC_ON, 0xa);
  659. }
  660. ENABLE_REGWRITE_BUFFER(ah);
  661. /* Write common array parameters */
  662. for (i = 0; i < ah->iniCommon.ia_rows; i++) {
  663. u32 reg = INI_RA(&ah->iniCommon, i, 0);
  664. u32 val = INI_RA(&ah->iniCommon, i, 1);
  665. REG_WRITE(ah, reg, val);
  666. if (reg >= 0x7800 && reg < 0x78a0
  667. && ah->config.analog_shiftreg
  668. && (common->bus_ops->ath_bus_type != ATH_USB)) {
  669. udelay(100);
  670. }
  671. DO_DELAY(regWrites);
  672. }
  673. REGWRITE_BUFFER_FLUSH(ah);
  674. REG_WRITE_ARRAY(&ah->iniBB_RfGain, freqIndex, regWrites);
  675. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  676. REG_WRITE_ARRAY(&ah->iniModesFastClock, modesIndex,
  677. regWrites);
  678. ar5008_hw_override_ini(ah, chan);
  679. ar5008_hw_set_channel_regs(ah, chan);
  680. ar5008_hw_init_chain_masks(ah);
  681. ath9k_olc_init(ah);
  682. ath9k_hw_apply_txpower(ah, chan, false);
  683. /* Write analog registers */
  684. if (!ath9k_hw_set_rf_regs(ah, chan, freqIndex)) {
  685. ath_err(ath9k_hw_common(ah), "ar5416SetRfRegs failed\n");
  686. return -EIO;
  687. }
  688. return 0;
  689. }
  690. static void ar5008_hw_set_rfmode(struct ath_hw *ah, struct ath9k_channel *chan)
  691. {
  692. u32 rfMode = 0;
  693. if (chan == NULL)
  694. return;
  695. if (IS_CHAN_2GHZ(chan))
  696. rfMode |= AR_PHY_MODE_DYNAMIC;
  697. else
  698. rfMode |= AR_PHY_MODE_OFDM;
  699. if (!AR_SREV_9280_20_OR_LATER(ah))
  700. rfMode |= (IS_CHAN_5GHZ(chan)) ?
  701. AR_PHY_MODE_RF5GHZ : AR_PHY_MODE_RF2GHZ;
  702. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  703. rfMode |= (AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE);
  704. REG_WRITE(ah, AR_PHY_MODE, rfMode);
  705. }
  706. static void ar5008_hw_mark_phy_inactive(struct ath_hw *ah)
  707. {
  708. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  709. }
  710. static void ar5008_hw_set_delta_slope(struct ath_hw *ah,
  711. struct ath9k_channel *chan)
  712. {
  713. u32 coef_scaled, ds_coef_exp, ds_coef_man;
  714. u32 clockMhzScaled = 0x64000000;
  715. struct chan_centers centers;
  716. if (IS_CHAN_HALF_RATE(chan))
  717. clockMhzScaled = clockMhzScaled >> 1;
  718. else if (IS_CHAN_QUARTER_RATE(chan))
  719. clockMhzScaled = clockMhzScaled >> 2;
  720. ath9k_hw_get_channel_centers(ah, chan, &centers);
  721. coef_scaled = clockMhzScaled / centers.synth_center;
  722. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  723. &ds_coef_exp);
  724. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  725. AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
  726. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  727. AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
  728. coef_scaled = (9 * coef_scaled) / 10;
  729. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  730. &ds_coef_exp);
  731. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  732. AR_PHY_HALFGI_DSC_MAN, ds_coef_man);
  733. REG_RMW_FIELD(ah, AR_PHY_HALFGI,
  734. AR_PHY_HALFGI_DSC_EXP, ds_coef_exp);
  735. }
  736. static bool ar5008_hw_rfbus_req(struct ath_hw *ah)
  737. {
  738. REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
  739. return ath9k_hw_wait(ah, AR_PHY_RFBUS_GRANT, AR_PHY_RFBUS_GRANT_EN,
  740. AR_PHY_RFBUS_GRANT_EN, AH_WAIT_TIMEOUT);
  741. }
  742. static void ar5008_hw_rfbus_done(struct ath_hw *ah)
  743. {
  744. u32 synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  745. ath9k_hw_synth_delay(ah, ah->curchan, synthDelay);
  746. REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
  747. }
  748. static void ar5008_restore_chainmask(struct ath_hw *ah)
  749. {
  750. int rx_chainmask = ah->rxchainmask;
  751. if ((rx_chainmask == 0x5) || (rx_chainmask == 0x3)) {
  752. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx_chainmask);
  753. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx_chainmask);
  754. }
  755. }
  756. static u32 ar9160_hw_compute_pll_control(struct ath_hw *ah,
  757. struct ath9k_channel *chan)
  758. {
  759. u32 pll;
  760. pll = SM(0x5, AR_RTC_9160_PLL_REFDIV);
  761. if (chan && IS_CHAN_HALF_RATE(chan))
  762. pll |= SM(0x1, AR_RTC_9160_PLL_CLKSEL);
  763. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  764. pll |= SM(0x2, AR_RTC_9160_PLL_CLKSEL);
  765. if (chan && IS_CHAN_5GHZ(chan))
  766. pll |= SM(0x50, AR_RTC_9160_PLL_DIV);
  767. else
  768. pll |= SM(0x58, AR_RTC_9160_PLL_DIV);
  769. return pll;
  770. }
  771. static u32 ar5008_hw_compute_pll_control(struct ath_hw *ah,
  772. struct ath9k_channel *chan)
  773. {
  774. u32 pll;
  775. pll = AR_RTC_PLL_REFDIV_5 | AR_RTC_PLL_DIV2;
  776. if (chan && IS_CHAN_HALF_RATE(chan))
  777. pll |= SM(0x1, AR_RTC_PLL_CLKSEL);
  778. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  779. pll |= SM(0x2, AR_RTC_PLL_CLKSEL);
  780. if (chan && IS_CHAN_5GHZ(chan))
  781. pll |= SM(0xa, AR_RTC_PLL_DIV);
  782. else
  783. pll |= SM(0xb, AR_RTC_PLL_DIV);
  784. return pll;
  785. }
  786. static bool ar5008_hw_ani_control_new(struct ath_hw *ah,
  787. enum ath9k_ani_cmd cmd,
  788. int param)
  789. {
  790. struct ath_common *common = ath9k_hw_common(ah);
  791. struct ath9k_channel *chan = ah->curchan;
  792. struct ar5416AniState *aniState = &ah->ani;
  793. s32 value;
  794. switch (cmd & ah->ani_function) {
  795. case ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION:{
  796. /*
  797. * on == 1 means ofdm weak signal detection is ON
  798. * on == 1 is the default, for less noise immunity
  799. *
  800. * on == 0 means ofdm weak signal detection is OFF
  801. * on == 0 means more noise imm
  802. */
  803. u32 on = param ? 1 : 0;
  804. /*
  805. * make register setting for default
  806. * (weak sig detect ON) come from INI file
  807. */
  808. int m1ThreshLow = on ?
  809. aniState->iniDef.m1ThreshLow : m1ThreshLow_off;
  810. int m2ThreshLow = on ?
  811. aniState->iniDef.m2ThreshLow : m2ThreshLow_off;
  812. int m1Thresh = on ?
  813. aniState->iniDef.m1Thresh : m1Thresh_off;
  814. int m2Thresh = on ?
  815. aniState->iniDef.m2Thresh : m2Thresh_off;
  816. int m2CountThr = on ?
  817. aniState->iniDef.m2CountThr : m2CountThr_off;
  818. int m2CountThrLow = on ?
  819. aniState->iniDef.m2CountThrLow : m2CountThrLow_off;
  820. int m1ThreshLowExt = on ?
  821. aniState->iniDef.m1ThreshLowExt : m1ThreshLowExt_off;
  822. int m2ThreshLowExt = on ?
  823. aniState->iniDef.m2ThreshLowExt : m2ThreshLowExt_off;
  824. int m1ThreshExt = on ?
  825. aniState->iniDef.m1ThreshExt : m1ThreshExt_off;
  826. int m2ThreshExt = on ?
  827. aniState->iniDef.m2ThreshExt : m2ThreshExt_off;
  828. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  829. AR_PHY_SFCORR_LOW_M1_THRESH_LOW,
  830. m1ThreshLow);
  831. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  832. AR_PHY_SFCORR_LOW_M2_THRESH_LOW,
  833. m2ThreshLow);
  834. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  835. AR_PHY_SFCORR_M1_THRESH, m1Thresh);
  836. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  837. AR_PHY_SFCORR_M2_THRESH, m2Thresh);
  838. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  839. AR_PHY_SFCORR_M2COUNT_THR, m2CountThr);
  840. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  841. AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW,
  842. m2CountThrLow);
  843. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  844. AR_PHY_SFCORR_EXT_M1_THRESH_LOW, m1ThreshLowExt);
  845. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  846. AR_PHY_SFCORR_EXT_M2_THRESH_LOW, m2ThreshLowExt);
  847. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  848. AR_PHY_SFCORR_EXT_M1_THRESH, m1ThreshExt);
  849. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  850. AR_PHY_SFCORR_EXT_M2_THRESH, m2ThreshExt);
  851. if (on)
  852. REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,
  853. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  854. else
  855. REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,
  856. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  857. if (on != aniState->ofdmWeakSigDetect) {
  858. ath_dbg(common, ANI,
  859. "** ch %d: ofdm weak signal: %s=>%s\n",
  860. chan->channel,
  861. aniState->ofdmWeakSigDetect ?
  862. "on" : "off",
  863. on ? "on" : "off");
  864. if (on)
  865. ah->stats.ast_ani_ofdmon++;
  866. else
  867. ah->stats.ast_ani_ofdmoff++;
  868. aniState->ofdmWeakSigDetect = on;
  869. }
  870. break;
  871. }
  872. case ATH9K_ANI_FIRSTEP_LEVEL:{
  873. u32 level = param;
  874. value = level * 2;
  875. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  876. AR_PHY_FIND_SIG_FIRSTEP, value);
  877. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG_LOW,
  878. AR_PHY_FIND_SIG_FIRSTEP_LOW, value);
  879. if (level != aniState->firstepLevel) {
  880. ath_dbg(common, ANI,
  881. "** ch %d: level %d=>%d[def:%d] firstep[level]=%d ini=%d\n",
  882. chan->channel,
  883. aniState->firstepLevel,
  884. level,
  885. ATH9K_ANI_FIRSTEP_LVL,
  886. value,
  887. aniState->iniDef.firstep);
  888. ath_dbg(common, ANI,
  889. "** ch %d: level %d=>%d[def:%d] firstep_low[level]=%d ini=%d\n",
  890. chan->channel,
  891. aniState->firstepLevel,
  892. level,
  893. ATH9K_ANI_FIRSTEP_LVL,
  894. value,
  895. aniState->iniDef.firstepLow);
  896. if (level > aniState->firstepLevel)
  897. ah->stats.ast_ani_stepup++;
  898. else if (level < aniState->firstepLevel)
  899. ah->stats.ast_ani_stepdown++;
  900. aniState->firstepLevel = level;
  901. }
  902. break;
  903. }
  904. case ATH9K_ANI_SPUR_IMMUNITY_LEVEL:{
  905. u32 level = param;
  906. value = (level + 1) * 2;
  907. REG_RMW_FIELD(ah, AR_PHY_TIMING5,
  908. AR_PHY_TIMING5_CYCPWR_THR1, value);
  909. REG_RMW_FIELD(ah, AR_PHY_EXT_CCA,
  910. AR_PHY_EXT_TIMING5_CYCPWR_THR1, value - 1);
  911. if (level != aniState->spurImmunityLevel) {
  912. ath_dbg(common, ANI,
  913. "** ch %d: level %d=>%d[def:%d] cycpwrThr1[level]=%d ini=%d\n",
  914. chan->channel,
  915. aniState->spurImmunityLevel,
  916. level,
  917. ATH9K_ANI_SPUR_IMMUNE_LVL,
  918. value,
  919. aniState->iniDef.cycpwrThr1);
  920. ath_dbg(common, ANI,
  921. "** ch %d: level %d=>%d[def:%d] cycpwrThr1Ext[level]=%d ini=%d\n",
  922. chan->channel,
  923. aniState->spurImmunityLevel,
  924. level,
  925. ATH9K_ANI_SPUR_IMMUNE_LVL,
  926. value,
  927. aniState->iniDef.cycpwrThr1Ext);
  928. if (level > aniState->spurImmunityLevel)
  929. ah->stats.ast_ani_spurup++;
  930. else if (level < aniState->spurImmunityLevel)
  931. ah->stats.ast_ani_spurdown++;
  932. aniState->spurImmunityLevel = level;
  933. }
  934. break;
  935. }
  936. case ATH9K_ANI_MRC_CCK:
  937. /*
  938. * You should not see this as AR5008, AR9001, AR9002
  939. * does not have hardware support for MRC CCK.
  940. */
  941. WARN_ON(1);
  942. break;
  943. default:
  944. ath_dbg(common, ANI, "invalid cmd %u\n", cmd);
  945. return false;
  946. }
  947. ath_dbg(common, ANI,
  948. "ANI parameters: SI=%d, ofdmWS=%s FS=%d MRCcck=%s listenTime=%d ofdmErrs=%d cckErrs=%d\n",
  949. aniState->spurImmunityLevel,
  950. aniState->ofdmWeakSigDetect ? "on" : "off",
  951. aniState->firstepLevel,
  952. aniState->mrcCCK ? "on" : "off",
  953. aniState->listenTime,
  954. aniState->ofdmPhyErrCount,
  955. aniState->cckPhyErrCount);
  956. return true;
  957. }
  958. static void ar5008_hw_do_getnf(struct ath_hw *ah,
  959. int16_t nfarray[NUM_NF_READINGS])
  960. {
  961. int16_t nf;
  962. nf = MS(REG_READ(ah, AR_PHY_CCA), AR_PHY_MINCCA_PWR);
  963. nfarray[0] = sign_extend32(nf, 8);
  964. nf = MS(REG_READ(ah, AR_PHY_CH1_CCA), AR_PHY_CH1_MINCCA_PWR);
  965. nfarray[1] = sign_extend32(nf, 8);
  966. nf = MS(REG_READ(ah, AR_PHY_CH2_CCA), AR_PHY_CH2_MINCCA_PWR);
  967. nfarray[2] = sign_extend32(nf, 8);
  968. if (!IS_CHAN_HT40(ah->curchan))
  969. return;
  970. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
  971. nfarray[3] = sign_extend32(nf, 8);
  972. nf = MS(REG_READ(ah, AR_PHY_CH1_EXT_CCA), AR_PHY_CH1_EXT_MINCCA_PWR);
  973. nfarray[4] = sign_extend32(nf, 8);
  974. nf = MS(REG_READ(ah, AR_PHY_CH2_EXT_CCA), AR_PHY_CH2_EXT_MINCCA_PWR);
  975. nfarray[5] = sign_extend32(nf, 8);
  976. }
  977. /*
  978. * Initialize the ANI register values with default (ini) values.
  979. * This routine is called during a (full) hardware reset after
  980. * all the registers are initialised from the INI.
  981. */
  982. static void ar5008_hw_ani_cache_ini_regs(struct ath_hw *ah)
  983. {
  984. struct ath_common *common = ath9k_hw_common(ah);
  985. struct ath9k_channel *chan = ah->curchan;
  986. struct ar5416AniState *aniState = &ah->ani;
  987. struct ath9k_ani_default *iniDef;
  988. u32 val;
  989. iniDef = &aniState->iniDef;
  990. ath_dbg(common, ANI, "ver %d.%d opmode %u chan %d Mhz\n",
  991. ah->hw_version.macVersion,
  992. ah->hw_version.macRev,
  993. ah->opmode,
  994. chan->channel);
  995. val = REG_READ(ah, AR_PHY_SFCORR);
  996. iniDef->m1Thresh = MS(val, AR_PHY_SFCORR_M1_THRESH);
  997. iniDef->m2Thresh = MS(val, AR_PHY_SFCORR_M2_THRESH);
  998. iniDef->m2CountThr = MS(val, AR_PHY_SFCORR_M2COUNT_THR);
  999. val = REG_READ(ah, AR_PHY_SFCORR_LOW);
  1000. iniDef->m1ThreshLow = MS(val, AR_PHY_SFCORR_LOW_M1_THRESH_LOW);
  1001. iniDef->m2ThreshLow = MS(val, AR_PHY_SFCORR_LOW_M2_THRESH_LOW);
  1002. iniDef->m2CountThrLow = MS(val, AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW);
  1003. val = REG_READ(ah, AR_PHY_SFCORR_EXT);
  1004. iniDef->m1ThreshExt = MS(val, AR_PHY_SFCORR_EXT_M1_THRESH);
  1005. iniDef->m2ThreshExt = MS(val, AR_PHY_SFCORR_EXT_M2_THRESH);
  1006. iniDef->m1ThreshLowExt = MS(val, AR_PHY_SFCORR_EXT_M1_THRESH_LOW);
  1007. iniDef->m2ThreshLowExt = MS(val, AR_PHY_SFCORR_EXT_M2_THRESH_LOW);
  1008. iniDef->firstep = REG_READ_FIELD(ah,
  1009. AR_PHY_FIND_SIG,
  1010. AR_PHY_FIND_SIG_FIRSTEP);
  1011. iniDef->firstepLow = REG_READ_FIELD(ah,
  1012. AR_PHY_FIND_SIG_LOW,
  1013. AR_PHY_FIND_SIG_FIRSTEP_LOW);
  1014. iniDef->cycpwrThr1 = REG_READ_FIELD(ah,
  1015. AR_PHY_TIMING5,
  1016. AR_PHY_TIMING5_CYCPWR_THR1);
  1017. iniDef->cycpwrThr1Ext = REG_READ_FIELD(ah,
  1018. AR_PHY_EXT_CCA,
  1019. AR_PHY_EXT_TIMING5_CYCPWR_THR1);
  1020. /* these levels just got reset to defaults by the INI */
  1021. aniState->spurImmunityLevel = ATH9K_ANI_SPUR_IMMUNE_LVL;
  1022. aniState->firstepLevel = ATH9K_ANI_FIRSTEP_LVL;
  1023. aniState->ofdmWeakSigDetect = true;
  1024. aniState->mrcCCK = false; /* not available on pre AR9003 */
  1025. }
  1026. static void ar5008_hw_set_nf_limits(struct ath_hw *ah)
  1027. {
  1028. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_2GHZ;
  1029. ah->nf_2g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_2GHZ;
  1030. ah->nf_2g.nominal = AR_PHY_CCA_NOM_VAL_5416_2GHZ;
  1031. ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_5416_5GHZ;
  1032. ah->nf_5g.min = AR_PHY_CCA_MIN_GOOD_VAL_5416_5GHZ;
  1033. ah->nf_5g.nominal = AR_PHY_CCA_NOM_VAL_5416_5GHZ;
  1034. }
  1035. static void ar5008_hw_set_radar_params(struct ath_hw *ah,
  1036. struct ath_hw_radar_conf *conf)
  1037. {
  1038. u32 radar_0 = 0, radar_1;
  1039. if (!conf) {
  1040. REG_CLR_BIT(ah, AR_PHY_RADAR_0, AR_PHY_RADAR_0_ENA);
  1041. return;
  1042. }
  1043. radar_0 |= AR_PHY_RADAR_0_ENA | AR_PHY_RADAR_0_FFT_ENA;
  1044. radar_0 |= SM(conf->fir_power, AR_PHY_RADAR_0_FIRPWR);
  1045. radar_0 |= SM(conf->radar_rssi, AR_PHY_RADAR_0_RRSSI);
  1046. radar_0 |= SM(conf->pulse_height, AR_PHY_RADAR_0_HEIGHT);
  1047. radar_0 |= SM(conf->pulse_rssi, AR_PHY_RADAR_0_PRSSI);
  1048. radar_0 |= SM(conf->pulse_inband, AR_PHY_RADAR_0_INBAND);
  1049. radar_1 = REG_READ(ah, AR_PHY_RADAR_1);
  1050. radar_1 &= ~(AR_PHY_RADAR_1_MAXLEN | AR_PHY_RADAR_1_RELSTEP_THRESH |
  1051. AR_PHY_RADAR_1_RELPWR_THRESH);
  1052. radar_1 |= AR_PHY_RADAR_1_MAX_RRSSI;
  1053. radar_1 |= AR_PHY_RADAR_1_BLOCK_CHECK;
  1054. radar_1 |= SM(conf->pulse_maxlen, AR_PHY_RADAR_1_MAXLEN);
  1055. radar_1 |= SM(conf->pulse_inband_step, AR_PHY_RADAR_1_RELSTEP_THRESH);
  1056. radar_1 |= SM(conf->radar_inband, AR_PHY_RADAR_1_RELPWR_THRESH);
  1057. REG_WRITE(ah, AR_PHY_RADAR_0, radar_0);
  1058. REG_WRITE(ah, AR_PHY_RADAR_1, radar_1);
  1059. if (conf->ext_channel)
  1060. REG_SET_BIT(ah, AR_PHY_RADAR_EXT, AR_PHY_RADAR_EXT_ENA);
  1061. else
  1062. REG_CLR_BIT(ah, AR_PHY_RADAR_EXT, AR_PHY_RADAR_EXT_ENA);
  1063. }
  1064. static void ar5008_hw_set_radar_conf(struct ath_hw *ah)
  1065. {
  1066. struct ath_hw_radar_conf *conf = &ah->radar_conf;
  1067. conf->fir_power = -33;
  1068. conf->radar_rssi = 20;
  1069. conf->pulse_height = 10;
  1070. conf->pulse_rssi = 15;
  1071. conf->pulse_inband = 15;
  1072. conf->pulse_maxlen = 255;
  1073. conf->pulse_inband_step = 12;
  1074. conf->radar_inband = 8;
  1075. }
  1076. static void ar5008_hw_init_txpower_cck(struct ath_hw *ah, int16_t *rate_array)
  1077. {
  1078. #define CCK_DELTA(_ah, x) ((OLC_FOR_AR9280_20_LATER(_ah)) ? max((x) - 2, 0) : (x))
  1079. ah->tx_power[0] = CCK_DELTA(ah, rate_array[rate1l]);
  1080. ah->tx_power[1] = CCK_DELTA(ah, min(rate_array[rate2l],
  1081. rate_array[rate2s]));
  1082. ah->tx_power[2] = CCK_DELTA(ah, min(rate_array[rate5_5l],
  1083. rate_array[rate5_5s]));
  1084. ah->tx_power[3] = CCK_DELTA(ah, min(rate_array[rate11l],
  1085. rate_array[rate11s]));
  1086. #undef CCK_DELTA
  1087. }
  1088. static void ar5008_hw_init_txpower_ofdm(struct ath_hw *ah, int16_t *rate_array,
  1089. int offset)
  1090. {
  1091. int i, idx = 0;
  1092. for (i = offset; i < offset + AR5008_OFDM_RATES; i++) {
  1093. ah->tx_power[i] = rate_array[idx];
  1094. idx++;
  1095. }
  1096. }
  1097. static void ar5008_hw_init_txpower_ht(struct ath_hw *ah, int16_t *rate_array,
  1098. int ss_offset, int ds_offset,
  1099. bool is_40, int ht40_delta)
  1100. {
  1101. int i, mcs_idx = (is_40) ? AR5008_HT40_SHIFT : AR5008_HT20_SHIFT;
  1102. for (i = ss_offset; i < ss_offset + AR5008_HT_SS_RATES; i++) {
  1103. ah->tx_power[i] = rate_array[mcs_idx] + ht40_delta;
  1104. mcs_idx++;
  1105. }
  1106. memcpy(&ah->tx_power[ds_offset], &ah->tx_power[ss_offset],
  1107. AR5008_HT_SS_RATES);
  1108. }
  1109. void ar5008_hw_init_rate_txpower(struct ath_hw *ah, int16_t *rate_array,
  1110. struct ath9k_channel *chan, int ht40_delta)
  1111. {
  1112. if (IS_CHAN_5GHZ(chan)) {
  1113. ar5008_hw_init_txpower_ofdm(ah, rate_array,
  1114. AR5008_11NA_OFDM_SHIFT);
  1115. if (IS_CHAN_HT20(chan) || IS_CHAN_HT40(chan)) {
  1116. ar5008_hw_init_txpower_ht(ah, rate_array,
  1117. AR5008_11NA_HT_SS_SHIFT,
  1118. AR5008_11NA_HT_DS_SHIFT,
  1119. IS_CHAN_HT40(chan),
  1120. ht40_delta);
  1121. }
  1122. } else {
  1123. ar5008_hw_init_txpower_cck(ah, rate_array);
  1124. ar5008_hw_init_txpower_ofdm(ah, rate_array,
  1125. AR5008_11NG_OFDM_SHIFT);
  1126. if (IS_CHAN_HT20(chan) || IS_CHAN_HT40(chan)) {
  1127. ar5008_hw_init_txpower_ht(ah, rate_array,
  1128. AR5008_11NG_HT_SS_SHIFT,
  1129. AR5008_11NG_HT_DS_SHIFT,
  1130. IS_CHAN_HT40(chan),
  1131. ht40_delta);
  1132. }
  1133. }
  1134. }
  1135. int ar5008_hw_attach_phy_ops(struct ath_hw *ah)
  1136. {
  1137. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  1138. static const u32 ar5416_cca_regs[6] = {
  1139. AR_PHY_CCA,
  1140. AR_PHY_CH1_CCA,
  1141. AR_PHY_CH2_CCA,
  1142. AR_PHY_EXT_CCA,
  1143. AR_PHY_CH1_EXT_CCA,
  1144. AR_PHY_CH2_EXT_CCA
  1145. };
  1146. int ret;
  1147. ret = ar5008_hw_rf_alloc_ext_banks(ah);
  1148. if (ret)
  1149. return ret;
  1150. priv_ops->rf_set_freq = ar5008_hw_set_channel;
  1151. priv_ops->spur_mitigate_freq = ar5008_hw_spur_mitigate;
  1152. priv_ops->set_rf_regs = ar5008_hw_set_rf_regs;
  1153. priv_ops->set_channel_regs = ar5008_hw_set_channel_regs;
  1154. priv_ops->init_bb = ar5008_hw_init_bb;
  1155. priv_ops->process_ini = ar5008_hw_process_ini;
  1156. priv_ops->set_rfmode = ar5008_hw_set_rfmode;
  1157. priv_ops->mark_phy_inactive = ar5008_hw_mark_phy_inactive;
  1158. priv_ops->set_delta_slope = ar5008_hw_set_delta_slope;
  1159. priv_ops->rfbus_req = ar5008_hw_rfbus_req;
  1160. priv_ops->rfbus_done = ar5008_hw_rfbus_done;
  1161. priv_ops->restore_chainmask = ar5008_restore_chainmask;
  1162. priv_ops->do_getnf = ar5008_hw_do_getnf;
  1163. priv_ops->set_radar_params = ar5008_hw_set_radar_params;
  1164. priv_ops->ani_control = ar5008_hw_ani_control_new;
  1165. priv_ops->ani_cache_ini_regs = ar5008_hw_ani_cache_ini_regs;
  1166. if (AR_SREV_9100(ah) || AR_SREV_9160_10_OR_LATER(ah))
  1167. priv_ops->compute_pll_control = ar9160_hw_compute_pll_control;
  1168. else
  1169. priv_ops->compute_pll_control = ar5008_hw_compute_pll_control;
  1170. ar5008_hw_set_nf_limits(ah);
  1171. ar5008_hw_set_radar_conf(ah);
  1172. memcpy(ah->nf_regs, ar5416_cca_regs, sizeof(ah->nf_regs));
  1173. return 0;
  1174. }