ar9003_phy.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142
  1. /*
  2. * Copyright (c) 2010 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 "ar9003_phy.h"
  18. /**
  19. * ar9003_hw_set_channel - set channel on single-chip device
  20. * @ah: atheros hardware structure
  21. * @chan:
  22. *
  23. * This is the function to change channel on single-chip devices, that is
  24. * all devices after ar9280.
  25. *
  26. * This function takes the channel value in MHz and sets
  27. * hardware channel value. Assumes writes have been enabled to analog bus.
  28. *
  29. * Actual Expression,
  30. *
  31. * For 2GHz channel,
  32. * Channel Frequency = (3/4) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)
  33. * (freq_ref = 40MHz)
  34. *
  35. * For 5GHz channel,
  36. * Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^10)
  37. * (freq_ref = 40MHz/(24>>amodeRefSel))
  38. *
  39. * For 5GHz channels which are 5MHz spaced,
  40. * Channel Frequency = (3/2) * freq_ref * (chansel[8:0] + chanfrac[16:0]/2^17)
  41. * (freq_ref = 40MHz)
  42. */
  43. static int ar9003_hw_set_channel(struct ath_hw *ah, struct ath9k_channel *chan)
  44. {
  45. u16 bMode, fracMode = 0, aModeRefSel = 0;
  46. u32 freq, channelSel = 0, reg32 = 0;
  47. struct chan_centers centers;
  48. int loadSynthChannel;
  49. ath9k_hw_get_channel_centers(ah, chan, &centers);
  50. freq = centers.synth_center;
  51. if (freq < 4800) { /* 2 GHz, fractional mode */
  52. channelSel = CHANSEL_2G(freq);
  53. /* Set to 2G mode */
  54. bMode = 1;
  55. } else {
  56. channelSel = CHANSEL_5G(freq);
  57. /* Doubler is ON, so, divide channelSel by 2. */
  58. channelSel >>= 1;
  59. /* Set to 5G mode */
  60. bMode = 0;
  61. }
  62. /* Enable fractional mode for all channels */
  63. fracMode = 1;
  64. aModeRefSel = 0;
  65. loadSynthChannel = 0;
  66. reg32 = (bMode << 29);
  67. REG_WRITE(ah, AR_PHY_SYNTH_CONTROL, reg32);
  68. /* Enable Long shift Select for Synthesizer */
  69. REG_RMW_FIELD(ah, AR_PHY_65NM_CH0_SYNTH4,
  70. AR_PHY_SYNTH4_LONG_SHIFT_SELECT, 1);
  71. /* Program Synth. setting */
  72. reg32 = (channelSel << 2) | (fracMode << 30) |
  73. (aModeRefSel << 28) | (loadSynthChannel << 31);
  74. REG_WRITE(ah, AR_PHY_65NM_CH0_SYNTH7, reg32);
  75. /* Toggle Load Synth channel bit */
  76. loadSynthChannel = 1;
  77. reg32 = (channelSel << 2) | (fracMode << 30) |
  78. (aModeRefSel << 28) | (loadSynthChannel << 31);
  79. REG_WRITE(ah, AR_PHY_65NM_CH0_SYNTH7, reg32);
  80. ah->curchan = chan;
  81. ah->curchan_rad_index = -1;
  82. return 0;
  83. }
  84. /**
  85. * ar9003_hw_spur_mitigate - convert baseband spur frequency
  86. * @ah: atheros hardware structure
  87. * @chan:
  88. *
  89. * For single-chip solutions. Converts to baseband spur frequency given the
  90. * input channel frequency and compute register settings below.
  91. *
  92. * Spur mitigation for MRC CCK
  93. */
  94. static void ar9003_hw_spur_mitigate_mrc_cck(struct ath_hw *ah,
  95. struct ath9k_channel *chan)
  96. {
  97. u32 spur_freq[4] = { 2420, 2440, 2464, 2480 };
  98. int cur_bb_spur, negative = 0, cck_spur_freq;
  99. int i;
  100. /*
  101. * Need to verify range +/- 10 MHz in control channel, otherwise spur
  102. * is out-of-band and can be ignored.
  103. */
  104. for (i = 0; i < 4; i++) {
  105. negative = 0;
  106. cur_bb_spur = spur_freq[i] - chan->channel;
  107. if (cur_bb_spur < 0) {
  108. negative = 1;
  109. cur_bb_spur = -cur_bb_spur;
  110. }
  111. if (cur_bb_spur < 10) {
  112. cck_spur_freq = (int)((cur_bb_spur << 19) / 11);
  113. if (negative == 1)
  114. cck_spur_freq = -cck_spur_freq;
  115. cck_spur_freq = cck_spur_freq & 0xfffff;
  116. REG_RMW_FIELD(ah, AR_PHY_AGC_CONTROL,
  117. AR_PHY_AGC_CONTROL_YCOK_MAX, 0x7);
  118. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  119. AR_PHY_CCK_SPUR_MIT_SPUR_RSSI_THR, 0x7f);
  120. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  121. AR_PHY_CCK_SPUR_MIT_SPUR_FILTER_TYPE,
  122. 0x2);
  123. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  124. AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT,
  125. 0x1);
  126. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  127. AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ,
  128. cck_spur_freq);
  129. return;
  130. }
  131. }
  132. REG_RMW_FIELD(ah, AR_PHY_AGC_CONTROL,
  133. AR_PHY_AGC_CONTROL_YCOK_MAX, 0x5);
  134. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  135. AR_PHY_CCK_SPUR_MIT_USE_CCK_SPUR_MIT, 0x0);
  136. REG_RMW_FIELD(ah, AR_PHY_CCK_SPUR_MIT,
  137. AR_PHY_CCK_SPUR_MIT_CCK_SPUR_FREQ, 0x0);
  138. }
  139. /* Clean all spur register fields */
  140. static void ar9003_hw_spur_ofdm_clear(struct ath_hw *ah)
  141. {
  142. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  143. AR_PHY_TIMING4_ENABLE_SPUR_FILTER, 0);
  144. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  145. AR_PHY_TIMING11_SPUR_FREQ_SD, 0);
  146. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  147. AR_PHY_TIMING11_SPUR_DELTA_PHASE, 0);
  148. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  149. AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD, 0);
  150. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  151. AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC, 0);
  152. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  153. AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR, 0);
  154. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  155. AR_PHY_TIMING4_ENABLE_SPUR_RSSI, 0);
  156. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  157. AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI, 0);
  158. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  159. AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT, 0);
  160. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  161. AR_PHY_SPUR_REG_ENABLE_MASK_PPM, 0);
  162. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  163. AR_PHY_TIMING4_ENABLE_PILOT_MASK, 0);
  164. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  165. AR_PHY_TIMING4_ENABLE_CHAN_MASK, 0);
  166. REG_RMW_FIELD(ah, AR_PHY_PILOT_SPUR_MASK,
  167. AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_IDX_A, 0);
  168. REG_RMW_FIELD(ah, AR_PHY_SPUR_MASK_A,
  169. AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_IDX_A, 0);
  170. REG_RMW_FIELD(ah, AR_PHY_CHAN_SPUR_MASK,
  171. AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_IDX_A, 0);
  172. REG_RMW_FIELD(ah, AR_PHY_PILOT_SPUR_MASK,
  173. AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_A, 0);
  174. REG_RMW_FIELD(ah, AR_PHY_CHAN_SPUR_MASK,
  175. AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_A, 0);
  176. REG_RMW_FIELD(ah, AR_PHY_SPUR_MASK_A,
  177. AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_A, 0);
  178. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  179. AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0);
  180. }
  181. static void ar9003_hw_spur_ofdm(struct ath_hw *ah,
  182. int freq_offset,
  183. int spur_freq_sd,
  184. int spur_delta_phase,
  185. int spur_subchannel_sd)
  186. {
  187. int mask_index = 0;
  188. /* OFDM Spur mitigation */
  189. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  190. AR_PHY_TIMING4_ENABLE_SPUR_FILTER, 0x1);
  191. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  192. AR_PHY_TIMING11_SPUR_FREQ_SD, spur_freq_sd);
  193. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  194. AR_PHY_TIMING11_SPUR_DELTA_PHASE, spur_delta_phase);
  195. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  196. AR_PHY_SFCORR_EXT_SPUR_SUBCHANNEL_SD, spur_subchannel_sd);
  197. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  198. AR_PHY_TIMING11_USE_SPUR_FILTER_IN_AGC, 0x1);
  199. REG_RMW_FIELD(ah, AR_PHY_TIMING11,
  200. AR_PHY_TIMING11_USE_SPUR_FILTER_IN_SELFCOR, 0x1);
  201. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  202. AR_PHY_TIMING4_ENABLE_SPUR_RSSI, 0x1);
  203. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  204. AR_PHY_SPUR_REG_SPUR_RSSI_THRESH, 34);
  205. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  206. AR_PHY_SPUR_REG_EN_VIT_SPUR_RSSI, 1);
  207. if (REG_READ_FIELD(ah, AR_PHY_MODE,
  208. AR_PHY_MODE_DYNAMIC) == 0x1)
  209. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  210. AR_PHY_SPUR_REG_ENABLE_NF_RSSI_SPUR_MIT, 1);
  211. mask_index = (freq_offset << 4) / 5;
  212. if (mask_index < 0)
  213. mask_index = mask_index - 1;
  214. mask_index = mask_index & 0x7f;
  215. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  216. AR_PHY_SPUR_REG_ENABLE_MASK_PPM, 0x1);
  217. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  218. AR_PHY_TIMING4_ENABLE_PILOT_MASK, 0x1);
  219. REG_RMW_FIELD(ah, AR_PHY_TIMING4,
  220. AR_PHY_TIMING4_ENABLE_CHAN_MASK, 0x1);
  221. REG_RMW_FIELD(ah, AR_PHY_PILOT_SPUR_MASK,
  222. AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_IDX_A, mask_index);
  223. REG_RMW_FIELD(ah, AR_PHY_SPUR_MASK_A,
  224. AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_IDX_A, mask_index);
  225. REG_RMW_FIELD(ah, AR_PHY_CHAN_SPUR_MASK,
  226. AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_IDX_A, mask_index);
  227. REG_RMW_FIELD(ah, AR_PHY_PILOT_SPUR_MASK,
  228. AR_PHY_PILOT_SPUR_MASK_CF_PILOT_MASK_A, 0xc);
  229. REG_RMW_FIELD(ah, AR_PHY_CHAN_SPUR_MASK,
  230. AR_PHY_CHAN_SPUR_MASK_CF_CHAN_MASK_A, 0xc);
  231. REG_RMW_FIELD(ah, AR_PHY_SPUR_MASK_A,
  232. AR_PHY_SPUR_MASK_A_CF_PUNC_MASK_A, 0xa0);
  233. REG_RMW_FIELD(ah, AR_PHY_SPUR_REG,
  234. AR_PHY_SPUR_REG_MASK_RATE_CNTL, 0xff);
  235. }
  236. static void ar9003_hw_spur_ofdm_work(struct ath_hw *ah,
  237. struct ath9k_channel *chan,
  238. int freq_offset)
  239. {
  240. int spur_freq_sd = 0;
  241. int spur_subchannel_sd = 0;
  242. int spur_delta_phase = 0;
  243. if (IS_CHAN_HT40(chan)) {
  244. if (freq_offset < 0) {
  245. if (REG_READ_FIELD(ah, AR_PHY_GEN_CTRL,
  246. AR_PHY_GC_DYN2040_PRI_CH) == 0x0)
  247. spur_subchannel_sd = 1;
  248. else
  249. spur_subchannel_sd = 0;
  250. spur_freq_sd = ((freq_offset + 10) << 9) / 11;
  251. } else {
  252. if (REG_READ_FIELD(ah, AR_PHY_GEN_CTRL,
  253. AR_PHY_GC_DYN2040_PRI_CH) == 0x0)
  254. spur_subchannel_sd = 0;
  255. else
  256. spur_subchannel_sd = 1;
  257. spur_freq_sd = ((freq_offset - 10) << 9) / 11;
  258. }
  259. spur_delta_phase = (freq_offset << 17) / 5;
  260. } else {
  261. spur_subchannel_sd = 0;
  262. spur_freq_sd = (freq_offset << 9) /11;
  263. spur_delta_phase = (freq_offset << 18) / 5;
  264. }
  265. spur_freq_sd = spur_freq_sd & 0x3ff;
  266. spur_delta_phase = spur_delta_phase & 0xfffff;
  267. ar9003_hw_spur_ofdm(ah,
  268. freq_offset,
  269. spur_freq_sd,
  270. spur_delta_phase,
  271. spur_subchannel_sd);
  272. }
  273. /* Spur mitigation for OFDM */
  274. static void ar9003_hw_spur_mitigate_ofdm(struct ath_hw *ah,
  275. struct ath9k_channel *chan)
  276. {
  277. int synth_freq;
  278. int range = 10;
  279. int freq_offset = 0;
  280. int mode;
  281. u8* spurChansPtr;
  282. unsigned int i;
  283. struct ar9300_eeprom *eep = &ah->eeprom.ar9300_eep;
  284. if (IS_CHAN_5GHZ(chan)) {
  285. spurChansPtr = &(eep->modalHeader5G.spurChans[0]);
  286. mode = 0;
  287. }
  288. else {
  289. spurChansPtr = &(eep->modalHeader2G.spurChans[0]);
  290. mode = 1;
  291. }
  292. if (spurChansPtr[0] == 0)
  293. return; /* No spur in the mode */
  294. if (IS_CHAN_HT40(chan)) {
  295. range = 19;
  296. if (REG_READ_FIELD(ah, AR_PHY_GEN_CTRL,
  297. AR_PHY_GC_DYN2040_PRI_CH) == 0x0)
  298. synth_freq = chan->channel - 10;
  299. else
  300. synth_freq = chan->channel + 10;
  301. } else {
  302. range = 10;
  303. synth_freq = chan->channel;
  304. }
  305. ar9003_hw_spur_ofdm_clear(ah);
  306. for (i = 0; spurChansPtr[i] && i < 5; i++) {
  307. freq_offset = FBIN2FREQ(spurChansPtr[i], mode) - synth_freq;
  308. if (abs(freq_offset) < range) {
  309. ar9003_hw_spur_ofdm_work(ah, chan, freq_offset);
  310. break;
  311. }
  312. }
  313. }
  314. static void ar9003_hw_spur_mitigate(struct ath_hw *ah,
  315. struct ath9k_channel *chan)
  316. {
  317. ar9003_hw_spur_mitigate_mrc_cck(ah, chan);
  318. ar9003_hw_spur_mitigate_ofdm(ah, chan);
  319. }
  320. static u32 ar9003_hw_compute_pll_control(struct ath_hw *ah,
  321. struct ath9k_channel *chan)
  322. {
  323. u32 pll;
  324. pll = SM(0x5, AR_RTC_9300_PLL_REFDIV);
  325. if (chan && IS_CHAN_HALF_RATE(chan))
  326. pll |= SM(0x1, AR_RTC_9300_PLL_CLKSEL);
  327. else if (chan && IS_CHAN_QUARTER_RATE(chan))
  328. pll |= SM(0x2, AR_RTC_9300_PLL_CLKSEL);
  329. if (chan && IS_CHAN_5GHZ(chan)) {
  330. pll |= SM(0x28, AR_RTC_9300_PLL_DIV);
  331. /*
  332. * When doing fast clock, set PLL to 0x142c
  333. */
  334. if (IS_CHAN_A_5MHZ_SPACED(chan))
  335. pll = 0x142c;
  336. } else
  337. pll |= SM(0x2c, AR_RTC_9300_PLL_DIV);
  338. return pll;
  339. }
  340. static void ar9003_hw_set_channel_regs(struct ath_hw *ah,
  341. struct ath9k_channel *chan)
  342. {
  343. u32 phymode;
  344. u32 enableDacFifo = 0;
  345. enableDacFifo =
  346. (REG_READ(ah, AR_PHY_GEN_CTRL) & AR_PHY_GC_ENABLE_DAC_FIFO);
  347. /* Enable 11n HT, 20 MHz */
  348. phymode = AR_PHY_GC_HT_EN | AR_PHY_GC_SINGLE_HT_LTF1 | AR_PHY_GC_WALSH |
  349. AR_PHY_GC_SHORT_GI_40 | enableDacFifo;
  350. /* Configure baseband for dynamic 20/40 operation */
  351. if (IS_CHAN_HT40(chan)) {
  352. phymode |= AR_PHY_GC_DYN2040_EN;
  353. /* Configure control (primary) channel at +-10MHz */
  354. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  355. (chan->chanmode == CHANNEL_G_HT40PLUS))
  356. phymode |= AR_PHY_GC_DYN2040_PRI_CH;
  357. }
  358. /* make sure we preserve INI settings */
  359. phymode |= REG_READ(ah, AR_PHY_GEN_CTRL);
  360. /* turn off Green Field detection for STA for now */
  361. phymode &= ~AR_PHY_GC_GF_DETECT_EN;
  362. REG_WRITE(ah, AR_PHY_GEN_CTRL, phymode);
  363. /* Configure MAC for 20/40 operation */
  364. ath9k_hw_set11nmac2040(ah);
  365. /* global transmit timeout (25 TUs default)*/
  366. REG_WRITE(ah, AR_GTXTO, 25 << AR_GTXTO_TIMEOUT_LIMIT_S);
  367. /* carrier sense timeout */
  368. REG_WRITE(ah, AR_CST, 0xF << AR_CST_TIMEOUT_LIMIT_S);
  369. }
  370. static void ar9003_hw_init_bb(struct ath_hw *ah,
  371. struct ath9k_channel *chan)
  372. {
  373. u32 synthDelay;
  374. /*
  375. * Wait for the frequency synth to settle (synth goes on
  376. * via AR_PHY_ACTIVE_EN). Read the phy active delay register.
  377. * Value is in 100ns increments.
  378. */
  379. synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  380. if (IS_CHAN_B(chan))
  381. synthDelay = (4 * synthDelay) / 22;
  382. else
  383. synthDelay /= 10;
  384. /* Activate the PHY (includes baseband activate + synthesizer on) */
  385. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_EN);
  386. /*
  387. * There is an issue if the AP starts the calibration before
  388. * the base band timeout completes. This could result in the
  389. * rx_clear false triggering. As a workaround we add delay an
  390. * extra BASE_ACTIVATE_DELAY usecs to ensure this condition
  391. * does not happen.
  392. */
  393. udelay(synthDelay + BASE_ACTIVATE_DELAY);
  394. }
  395. void ar9003_hw_set_chain_masks(struct ath_hw *ah, u8 rx, u8 tx)
  396. {
  397. switch (rx) {
  398. case 0x5:
  399. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  400. AR_PHY_SWAP_ALT_CHAIN);
  401. case 0x3:
  402. case 0x1:
  403. case 0x2:
  404. case 0x7:
  405. REG_WRITE(ah, AR_PHY_RX_CHAINMASK, rx);
  406. REG_WRITE(ah, AR_PHY_CAL_CHAINMASK, rx);
  407. break;
  408. default:
  409. break;
  410. }
  411. REG_WRITE(ah, AR_SELFGEN_MASK, tx);
  412. if (tx == 0x5) {
  413. REG_SET_BIT(ah, AR_PHY_ANALOG_SWAP,
  414. AR_PHY_SWAP_ALT_CHAIN);
  415. }
  416. }
  417. /*
  418. * Override INI values with chip specific configuration.
  419. */
  420. static void ar9003_hw_override_ini(struct ath_hw *ah)
  421. {
  422. u32 val;
  423. /*
  424. * Set the RX_ABORT and RX_DIS and clear it only after
  425. * RXE is set for MAC. This prevents frames with
  426. * corrupted descriptor status.
  427. */
  428. REG_SET_BIT(ah, AR_DIAG_SW, (AR_DIAG_RX_DIS | AR_DIAG_RX_ABORT));
  429. /*
  430. * For AR9280 and above, there is a new feature that allows
  431. * Multicast search based on both MAC Address and Key ID. By default,
  432. * this feature is enabled. But since the driver is not using this
  433. * feature, we switch it off; otherwise multicast search based on
  434. * MAC addr only will fail.
  435. */
  436. val = REG_READ(ah, AR_PCU_MISC_MODE2) & (~AR_ADHOC_MCAST_KEYID_ENABLE);
  437. REG_WRITE(ah, AR_PCU_MISC_MODE2,
  438. val | AR_AGG_WEP_ENABLE_FIX | AR_AGG_WEP_ENABLE);
  439. }
  440. static void ar9003_hw_prog_ini(struct ath_hw *ah,
  441. struct ar5416IniArray *iniArr,
  442. int column)
  443. {
  444. unsigned int i, regWrites = 0;
  445. /* New INI format: Array may be undefined (pre, core, post arrays) */
  446. if (!iniArr->ia_array)
  447. return;
  448. /*
  449. * New INI format: Pre, core, and post arrays for a given subsystem
  450. * may be modal (> 2 columns) or non-modal (2 columns). Determine if
  451. * the array is non-modal and force the column to 1.
  452. */
  453. if (column >= iniArr->ia_columns)
  454. column = 1;
  455. for (i = 0; i < iniArr->ia_rows; i++) {
  456. u32 reg = INI_RA(iniArr, i, 0);
  457. u32 val = INI_RA(iniArr, i, column);
  458. REG_WRITE(ah, reg, val);
  459. /*
  460. * Determine if this is a shift register value, and insert the
  461. * configured delay if so.
  462. */
  463. if (reg >= 0x16000 && reg < 0x17000
  464. && ah->config.analog_shiftreg)
  465. udelay(100);
  466. DO_DELAY(regWrites);
  467. }
  468. }
  469. static int ar9003_hw_process_ini(struct ath_hw *ah,
  470. struct ath9k_channel *chan)
  471. {
  472. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  473. unsigned int regWrites = 0, i;
  474. struct ieee80211_channel *channel = chan->chan;
  475. u32 modesIndex, freqIndex;
  476. switch (chan->chanmode) {
  477. case CHANNEL_A:
  478. case CHANNEL_A_HT20:
  479. modesIndex = 1;
  480. freqIndex = 1;
  481. break;
  482. case CHANNEL_A_HT40PLUS:
  483. case CHANNEL_A_HT40MINUS:
  484. modesIndex = 2;
  485. freqIndex = 1;
  486. break;
  487. case CHANNEL_G:
  488. case CHANNEL_G_HT20:
  489. case CHANNEL_B:
  490. modesIndex = 4;
  491. freqIndex = 2;
  492. break;
  493. case CHANNEL_G_HT40PLUS:
  494. case CHANNEL_G_HT40MINUS:
  495. modesIndex = 3;
  496. freqIndex = 2;
  497. break;
  498. default:
  499. return -EINVAL;
  500. }
  501. for (i = 0; i < ATH_INI_NUM_SPLIT; i++) {
  502. ar9003_hw_prog_ini(ah, &ah->iniSOC[i], modesIndex);
  503. ar9003_hw_prog_ini(ah, &ah->iniMac[i], modesIndex);
  504. ar9003_hw_prog_ini(ah, &ah->iniBB[i], modesIndex);
  505. ar9003_hw_prog_ini(ah, &ah->iniRadio[i], modesIndex);
  506. }
  507. REG_WRITE_ARRAY(&ah->iniModesRxGain, 1, regWrites);
  508. REG_WRITE_ARRAY(&ah->iniModesTxGain, modesIndex, regWrites);
  509. /*
  510. * For 5GHz channels requiring Fast Clock, apply
  511. * different modal values.
  512. */
  513. if (IS_CHAN_A_5MHZ_SPACED(chan))
  514. REG_WRITE_ARRAY(&ah->iniModesAdditional,
  515. modesIndex, regWrites);
  516. ar9003_hw_override_ini(ah);
  517. ar9003_hw_set_channel_regs(ah, chan);
  518. ar9003_hw_set_chain_masks(ah, ah->rxchainmask, ah->txchainmask);
  519. /* Set TX power */
  520. ah->eep_ops->set_txpower(ah, chan,
  521. ath9k_regd_get_ctl(regulatory, chan),
  522. channel->max_antenna_gain * 2,
  523. channel->max_power * 2,
  524. min((u32) MAX_RATE_POWER,
  525. (u32) regulatory->power_limit));
  526. return 0;
  527. }
  528. static void ar9003_hw_set_rfmode(struct ath_hw *ah,
  529. struct ath9k_channel *chan)
  530. {
  531. u32 rfMode = 0;
  532. if (chan == NULL)
  533. return;
  534. rfMode |= (IS_CHAN_B(chan) || IS_CHAN_G(chan))
  535. ? AR_PHY_MODE_DYNAMIC : AR_PHY_MODE_OFDM;
  536. if (IS_CHAN_A_5MHZ_SPACED(chan))
  537. rfMode |= (AR_PHY_MODE_DYNAMIC | AR_PHY_MODE_DYN_CCK_DISABLE);
  538. REG_WRITE(ah, AR_PHY_MODE, rfMode);
  539. }
  540. static void ar9003_hw_mark_phy_inactive(struct ath_hw *ah)
  541. {
  542. REG_WRITE(ah, AR_PHY_ACTIVE, AR_PHY_ACTIVE_DIS);
  543. }
  544. static void ar9003_hw_set_delta_slope(struct ath_hw *ah,
  545. struct ath9k_channel *chan)
  546. {
  547. u32 coef_scaled, ds_coef_exp, ds_coef_man;
  548. u32 clockMhzScaled = 0x64000000;
  549. struct chan_centers centers;
  550. /*
  551. * half and quarter rate can divide the scaled clock by 2 or 4
  552. * scale for selected channel bandwidth
  553. */
  554. if (IS_CHAN_HALF_RATE(chan))
  555. clockMhzScaled = clockMhzScaled >> 1;
  556. else if (IS_CHAN_QUARTER_RATE(chan))
  557. clockMhzScaled = clockMhzScaled >> 2;
  558. /*
  559. * ALGO -> coef = 1e8/fcarrier*fclock/40;
  560. * scaled coef to provide precision for this floating calculation
  561. */
  562. ath9k_hw_get_channel_centers(ah, chan, &centers);
  563. coef_scaled = clockMhzScaled / centers.synth_center;
  564. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  565. &ds_coef_exp);
  566. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  567. AR_PHY_TIMING3_DSC_MAN, ds_coef_man);
  568. REG_RMW_FIELD(ah, AR_PHY_TIMING3,
  569. AR_PHY_TIMING3_DSC_EXP, ds_coef_exp);
  570. /*
  571. * For Short GI,
  572. * scaled coeff is 9/10 that of normal coeff
  573. */
  574. coef_scaled = (9 * coef_scaled) / 10;
  575. ath9k_hw_get_delta_slope_vals(ah, coef_scaled, &ds_coef_man,
  576. &ds_coef_exp);
  577. /* for short gi */
  578. REG_RMW_FIELD(ah, AR_PHY_SGI_DELTA,
  579. AR_PHY_SGI_DSC_MAN, ds_coef_man);
  580. REG_RMW_FIELD(ah, AR_PHY_SGI_DELTA,
  581. AR_PHY_SGI_DSC_EXP, ds_coef_exp);
  582. }
  583. static bool ar9003_hw_rfbus_req(struct ath_hw *ah)
  584. {
  585. REG_WRITE(ah, AR_PHY_RFBUS_REQ, AR_PHY_RFBUS_REQ_EN);
  586. return ath9k_hw_wait(ah, AR_PHY_RFBUS_GRANT, AR_PHY_RFBUS_GRANT_EN,
  587. AR_PHY_RFBUS_GRANT_EN, AH_WAIT_TIMEOUT);
  588. }
  589. /*
  590. * Wait for the frequency synth to settle (synth goes on via PHY_ACTIVE_EN).
  591. * Read the phy active delay register. Value is in 100ns increments.
  592. */
  593. static void ar9003_hw_rfbus_done(struct ath_hw *ah)
  594. {
  595. u32 synthDelay = REG_READ(ah, AR_PHY_RX_DELAY) & AR_PHY_RX_DELAY_DELAY;
  596. if (IS_CHAN_B(ah->curchan))
  597. synthDelay = (4 * synthDelay) / 22;
  598. else
  599. synthDelay /= 10;
  600. udelay(synthDelay + BASE_ACTIVATE_DELAY);
  601. REG_WRITE(ah, AR_PHY_RFBUS_REQ, 0);
  602. }
  603. /*
  604. * Set the interrupt and GPIO values so the ISR can disable RF
  605. * on a switch signal. Assumes GPIO port and interrupt polarity
  606. * are set prior to call.
  607. */
  608. static void ar9003_hw_enable_rfkill(struct ath_hw *ah)
  609. {
  610. /* Connect rfsilent_bb_l to baseband */
  611. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL,
  612. AR_GPIO_INPUT_EN_VAL_RFSILENT_BB);
  613. /* Set input mux for rfsilent_bb_l to GPIO #0 */
  614. REG_CLR_BIT(ah, AR_GPIO_INPUT_MUX2,
  615. AR_GPIO_INPUT_MUX2_RFSILENT);
  616. /*
  617. * Configure the desired GPIO port for input and
  618. * enable baseband rf silence.
  619. */
  620. ath9k_hw_cfg_gpio_input(ah, ah->rfkill_gpio);
  621. REG_SET_BIT(ah, AR_PHY_TEST, RFSILENT_BB);
  622. }
  623. static void ar9003_hw_set_diversity(struct ath_hw *ah, bool value)
  624. {
  625. u32 v = REG_READ(ah, AR_PHY_CCK_DETECT);
  626. if (value)
  627. v |= AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
  628. else
  629. v &= ~AR_PHY_CCK_DETECT_BB_ENABLE_ANT_FAST_DIV;
  630. REG_WRITE(ah, AR_PHY_CCK_DETECT, v);
  631. }
  632. static bool ar9003_hw_ani_control(struct ath_hw *ah,
  633. enum ath9k_ani_cmd cmd, int param)
  634. {
  635. struct ar5416AniState *aniState = ah->curani;
  636. struct ath_common *common = ath9k_hw_common(ah);
  637. switch (cmd & ah->ani_function) {
  638. case ATH9K_ANI_NOISE_IMMUNITY_LEVEL:{
  639. u32 level = param;
  640. if (level >= ARRAY_SIZE(ah->totalSizeDesired)) {
  641. ath_print(common, ATH_DBG_ANI,
  642. "level out of range (%u > %u)\n",
  643. level,
  644. (unsigned)ARRAY_SIZE(ah->totalSizeDesired));
  645. return false;
  646. }
  647. REG_RMW_FIELD(ah, AR_PHY_DESIRED_SZ,
  648. AR_PHY_DESIRED_SZ_TOT_DES,
  649. ah->totalSizeDesired[level]);
  650. REG_RMW_FIELD(ah, AR_PHY_AGC,
  651. AR_PHY_AGC_COARSE_LOW,
  652. ah->coarse_low[level]);
  653. REG_RMW_FIELD(ah, AR_PHY_AGC,
  654. AR_PHY_AGC_COARSE_HIGH,
  655. ah->coarse_high[level]);
  656. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  657. AR_PHY_FIND_SIG_FIRPWR, ah->firpwr[level]);
  658. if (level > aniState->noiseImmunityLevel)
  659. ah->stats.ast_ani_niup++;
  660. else if (level < aniState->noiseImmunityLevel)
  661. ah->stats.ast_ani_nidown++;
  662. aniState->noiseImmunityLevel = level;
  663. break;
  664. }
  665. case ATH9K_ANI_OFDM_WEAK_SIGNAL_DETECTION:{
  666. const int m1ThreshLow[] = { 127, 50 };
  667. const int m2ThreshLow[] = { 127, 40 };
  668. const int m1Thresh[] = { 127, 0x4d };
  669. const int m2Thresh[] = { 127, 0x40 };
  670. const int m2CountThr[] = { 31, 16 };
  671. const int m2CountThrLow[] = { 63, 48 };
  672. u32 on = param ? 1 : 0;
  673. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  674. AR_PHY_SFCORR_LOW_M1_THRESH_LOW,
  675. m1ThreshLow[on]);
  676. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  677. AR_PHY_SFCORR_LOW_M2_THRESH_LOW,
  678. m2ThreshLow[on]);
  679. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  680. AR_PHY_SFCORR_M1_THRESH, m1Thresh[on]);
  681. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  682. AR_PHY_SFCORR_M2_THRESH, m2Thresh[on]);
  683. REG_RMW_FIELD(ah, AR_PHY_SFCORR,
  684. AR_PHY_SFCORR_M2COUNT_THR, m2CountThr[on]);
  685. REG_RMW_FIELD(ah, AR_PHY_SFCORR_LOW,
  686. AR_PHY_SFCORR_LOW_M2COUNT_THR_LOW,
  687. m2CountThrLow[on]);
  688. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  689. AR_PHY_SFCORR_EXT_M1_THRESH_LOW, m1ThreshLow[on]);
  690. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  691. AR_PHY_SFCORR_EXT_M2_THRESH_LOW, m2ThreshLow[on]);
  692. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  693. AR_PHY_SFCORR_EXT_M1_THRESH, m1Thresh[on]);
  694. REG_RMW_FIELD(ah, AR_PHY_SFCORR_EXT,
  695. AR_PHY_SFCORR_EXT_M2_THRESH, m2Thresh[on]);
  696. if (on)
  697. REG_SET_BIT(ah, AR_PHY_SFCORR_LOW,
  698. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  699. else
  700. REG_CLR_BIT(ah, AR_PHY_SFCORR_LOW,
  701. AR_PHY_SFCORR_LOW_USE_SELF_CORR_LOW);
  702. if (!on != aniState->ofdmWeakSigDetectOff) {
  703. if (on)
  704. ah->stats.ast_ani_ofdmon++;
  705. else
  706. ah->stats.ast_ani_ofdmoff++;
  707. aniState->ofdmWeakSigDetectOff = !on;
  708. }
  709. break;
  710. }
  711. case ATH9K_ANI_CCK_WEAK_SIGNAL_THR:{
  712. const int weakSigThrCck[] = { 8, 6 };
  713. u32 high = param ? 1 : 0;
  714. REG_RMW_FIELD(ah, AR_PHY_CCK_DETECT,
  715. AR_PHY_CCK_DETECT_WEAK_SIG_THR_CCK,
  716. weakSigThrCck[high]);
  717. if (high != aniState->cckWeakSigThreshold) {
  718. if (high)
  719. ah->stats.ast_ani_cckhigh++;
  720. else
  721. ah->stats.ast_ani_ccklow++;
  722. aniState->cckWeakSigThreshold = high;
  723. }
  724. break;
  725. }
  726. case ATH9K_ANI_FIRSTEP_LEVEL:{
  727. const int firstep[] = { 0, 4, 8 };
  728. u32 level = param;
  729. if (level >= ARRAY_SIZE(firstep)) {
  730. ath_print(common, ATH_DBG_ANI,
  731. "level out of range (%u > %u)\n",
  732. level,
  733. (unsigned) ARRAY_SIZE(firstep));
  734. return false;
  735. }
  736. REG_RMW_FIELD(ah, AR_PHY_FIND_SIG,
  737. AR_PHY_FIND_SIG_FIRSTEP,
  738. firstep[level]);
  739. if (level > aniState->firstepLevel)
  740. ah->stats.ast_ani_stepup++;
  741. else if (level < aniState->firstepLevel)
  742. ah->stats.ast_ani_stepdown++;
  743. aniState->firstepLevel = level;
  744. break;
  745. }
  746. case ATH9K_ANI_SPUR_IMMUNITY_LEVEL:{
  747. const int cycpwrThr1[] = { 2, 4, 6, 8, 10, 12, 14, 16 };
  748. u32 level = param;
  749. if (level >= ARRAY_SIZE(cycpwrThr1)) {
  750. ath_print(common, ATH_DBG_ANI,
  751. "level out of range (%u > %u)\n",
  752. level,
  753. (unsigned) ARRAY_SIZE(cycpwrThr1));
  754. return false;
  755. }
  756. REG_RMW_FIELD(ah, AR_PHY_TIMING5,
  757. AR_PHY_TIMING5_CYCPWR_THR1,
  758. cycpwrThr1[level]);
  759. if (level > aniState->spurImmunityLevel)
  760. ah->stats.ast_ani_spurup++;
  761. else if (level < aniState->spurImmunityLevel)
  762. ah->stats.ast_ani_spurdown++;
  763. aniState->spurImmunityLevel = level;
  764. break;
  765. }
  766. case ATH9K_ANI_PRESENT:
  767. break;
  768. default:
  769. ath_print(common, ATH_DBG_ANI,
  770. "invalid cmd %u\n", cmd);
  771. return false;
  772. }
  773. ath_print(common, ATH_DBG_ANI, "ANI parameters:\n");
  774. ath_print(common, ATH_DBG_ANI,
  775. "noiseImmunityLevel=%d, spurImmunityLevel=%d, "
  776. "ofdmWeakSigDetectOff=%d\n",
  777. aniState->noiseImmunityLevel,
  778. aniState->spurImmunityLevel,
  779. !aniState->ofdmWeakSigDetectOff);
  780. ath_print(common, ATH_DBG_ANI,
  781. "cckWeakSigThreshold=%d, "
  782. "firstepLevel=%d, listenTime=%d\n",
  783. aniState->cckWeakSigThreshold,
  784. aniState->firstepLevel,
  785. aniState->listenTime);
  786. ath_print(common, ATH_DBG_ANI,
  787. "cycleCount=%d, ofdmPhyErrCount=%d, cckPhyErrCount=%d\n\n",
  788. aniState->cycleCount,
  789. aniState->ofdmPhyErrCount,
  790. aniState->cckPhyErrCount);
  791. return true;
  792. }
  793. static void ar9003_hw_nf_sanitize_2g(struct ath_hw *ah, s16 *nf)
  794. {
  795. struct ath_common *common = ath9k_hw_common(ah);
  796. if (*nf > ah->nf_2g_max) {
  797. ath_print(common, ATH_DBG_CALIBRATE,
  798. "2 GHz NF (%d) > MAX (%d), "
  799. "correcting to MAX",
  800. *nf, ah->nf_2g_max);
  801. *nf = ah->nf_2g_max;
  802. } else if (*nf < ah->nf_2g_min) {
  803. ath_print(common, ATH_DBG_CALIBRATE,
  804. "2 GHz NF (%d) < MIN (%d), "
  805. "correcting to MIN",
  806. *nf, ah->nf_2g_min);
  807. *nf = ah->nf_2g_min;
  808. }
  809. }
  810. static void ar9003_hw_nf_sanitize_5g(struct ath_hw *ah, s16 *nf)
  811. {
  812. struct ath_common *common = ath9k_hw_common(ah);
  813. if (*nf > ah->nf_5g_max) {
  814. ath_print(common, ATH_DBG_CALIBRATE,
  815. "5 GHz NF (%d) > MAX (%d), "
  816. "correcting to MAX",
  817. *nf, ah->nf_5g_max);
  818. *nf = ah->nf_5g_max;
  819. } else if (*nf < ah->nf_5g_min) {
  820. ath_print(common, ATH_DBG_CALIBRATE,
  821. "5 GHz NF (%d) < MIN (%d), "
  822. "correcting to MIN",
  823. *nf, ah->nf_5g_min);
  824. *nf = ah->nf_5g_min;
  825. }
  826. }
  827. static void ar9003_hw_nf_sanitize(struct ath_hw *ah, s16 *nf)
  828. {
  829. if (IS_CHAN_2GHZ(ah->curchan))
  830. ar9003_hw_nf_sanitize_2g(ah, nf);
  831. else
  832. ar9003_hw_nf_sanitize_5g(ah, nf);
  833. }
  834. static void ar9003_hw_do_getnf(struct ath_hw *ah,
  835. int16_t nfarray[NUM_NF_READINGS])
  836. {
  837. struct ath_common *common = ath9k_hw_common(ah);
  838. int16_t nf;
  839. nf = MS(REG_READ(ah, AR_PHY_CCA_0), AR_PHY_MINCCA_PWR);
  840. if (nf & 0x100)
  841. nf = 0 - ((nf ^ 0x1ff) + 1);
  842. ar9003_hw_nf_sanitize(ah, &nf);
  843. ath_print(common, ATH_DBG_CALIBRATE,
  844. "NF calibrated [ctl] [chain 0] is %d\n", nf);
  845. nfarray[0] = nf;
  846. nf = MS(REG_READ(ah, AR_PHY_CCA_1), AR_PHY_CH1_MINCCA_PWR);
  847. if (nf & 0x100)
  848. nf = 0 - ((nf ^ 0x1ff) + 1);
  849. ar9003_hw_nf_sanitize(ah, &nf);
  850. ath_print(common, ATH_DBG_CALIBRATE,
  851. "NF calibrated [ctl] [chain 1] is %d\n", nf);
  852. nfarray[1] = nf;
  853. nf = MS(REG_READ(ah, AR_PHY_CCA_2), AR_PHY_CH2_MINCCA_PWR);
  854. if (nf & 0x100)
  855. nf = 0 - ((nf ^ 0x1ff) + 1);
  856. ar9003_hw_nf_sanitize(ah, &nf);
  857. ath_print(common, ATH_DBG_CALIBRATE,
  858. "NF calibrated [ctl] [chain 2] is %d\n", nf);
  859. nfarray[2] = nf;
  860. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA), AR_PHY_EXT_MINCCA_PWR);
  861. if (nf & 0x100)
  862. nf = 0 - ((nf ^ 0x1ff) + 1);
  863. ar9003_hw_nf_sanitize(ah, &nf);
  864. ath_print(common, ATH_DBG_CALIBRATE,
  865. "NF calibrated [ext] [chain 0] is %d\n", nf);
  866. nfarray[3] = nf;
  867. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_1), AR_PHY_CH1_EXT_MINCCA_PWR);
  868. if (nf & 0x100)
  869. nf = 0 - ((nf ^ 0x1ff) + 1);
  870. ar9003_hw_nf_sanitize(ah, &nf);
  871. ath_print(common, ATH_DBG_CALIBRATE,
  872. "NF calibrated [ext] [chain 1] is %d\n", nf);
  873. nfarray[4] = nf;
  874. nf = MS(REG_READ(ah, AR_PHY_EXT_CCA_2), AR_PHY_CH2_EXT_MINCCA_PWR);
  875. if (nf & 0x100)
  876. nf = 0 - ((nf ^ 0x1ff) + 1);
  877. ar9003_hw_nf_sanitize(ah, &nf);
  878. ath_print(common, ATH_DBG_CALIBRATE,
  879. "NF calibrated [ext] [chain 2] is %d\n", nf);
  880. nfarray[5] = nf;
  881. }
  882. void ar9003_hw_set_nf_limits(struct ath_hw *ah)
  883. {
  884. ah->nf_2g_max = AR_PHY_CCA_MAX_GOOD_VAL_9300_2GHZ;
  885. ah->nf_2g_min = AR_PHY_CCA_MIN_GOOD_VAL_9300_2GHZ;
  886. ah->nf_5g_max = AR_PHY_CCA_MAX_GOOD_VAL_9300_5GHZ;
  887. ah->nf_5g_min = AR_PHY_CCA_MIN_GOOD_VAL_9300_5GHZ;
  888. }
  889. /*
  890. * Find out which of the RX chains are enabled
  891. */
  892. static u32 ar9003_hw_get_rx_chainmask(struct ath_hw *ah)
  893. {
  894. u32 chain = REG_READ(ah, AR_PHY_RX_CHAINMASK);
  895. /*
  896. * The bits [2:0] indicate the rx chain mask and are to be
  897. * interpreted as follows:
  898. * 00x => Only chain 0 is enabled
  899. * 01x => Chain 1 and 0 enabled
  900. * 1xx => Chain 2,1 and 0 enabled
  901. */
  902. return chain & 0x7;
  903. }
  904. static void ar9003_hw_loadnf(struct ath_hw *ah, struct ath9k_channel *chan)
  905. {
  906. struct ath9k_nfcal_hist *h;
  907. unsigned i, j;
  908. int32_t val;
  909. const u32 ar9300_cca_regs[6] = {
  910. AR_PHY_CCA_0,
  911. AR_PHY_CCA_1,
  912. AR_PHY_CCA_2,
  913. AR_PHY_EXT_CCA,
  914. AR_PHY_EXT_CCA_1,
  915. AR_PHY_EXT_CCA_2,
  916. };
  917. u8 chainmask, rx_chain_status;
  918. struct ath_common *common = ath9k_hw_common(ah);
  919. rx_chain_status = ar9003_hw_get_rx_chainmask(ah);
  920. chainmask = 0x3F;
  921. h = ah->nfCalHist;
  922. for (i = 0; i < NUM_NF_READINGS; i++) {
  923. if (chainmask & (1 << i)) {
  924. val = REG_READ(ah, ar9300_cca_regs[i]);
  925. val &= 0xFFFFFE00;
  926. val |= (((u32) (h[i].privNF) << 1) & 0x1ff);
  927. REG_WRITE(ah, ar9300_cca_regs[i], val);
  928. }
  929. }
  930. /*
  931. * Load software filtered NF value into baseband internal minCCApwr
  932. * variable.
  933. */
  934. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  935. AR_PHY_AGC_CONTROL_ENABLE_NF);
  936. REG_CLR_BIT(ah, AR_PHY_AGC_CONTROL,
  937. AR_PHY_AGC_CONTROL_NO_UPDATE_NF);
  938. REG_SET_BIT(ah, AR_PHY_AGC_CONTROL, AR_PHY_AGC_CONTROL_NF);
  939. /*
  940. * Wait for load to complete, should be fast, a few 10s of us.
  941. * The max delay was changed from an original 250us to 10000us
  942. * since 250us often results in NF load timeout and causes deaf
  943. * condition during stress testing 12/12/2009
  944. */
  945. for (j = 0; j < 1000; j++) {
  946. if ((REG_READ(ah, AR_PHY_AGC_CONTROL) &
  947. AR_PHY_AGC_CONTROL_NF) == 0)
  948. break;
  949. udelay(10);
  950. }
  951. /*
  952. * We timed out waiting for the noisefloor to load, probably due to an
  953. * in-progress rx. Simply return here and allow the load plenty of time
  954. * to complete before the next calibration interval. We need to avoid
  955. * trying to load -50 (which happens below) while the previous load is
  956. * still in progress as this can cause rx deafness. Instead by returning
  957. * here, the baseband nf cal will just be capped by our present
  958. * noisefloor until the next calibration timer.
  959. */
  960. if (j == 1000) {
  961. ath_print(common, ATH_DBG_ANY, "Timeout while waiting for nf "
  962. "to load: AR_PHY_AGC_CONTROL=0x%x\n",
  963. REG_READ(ah, AR_PHY_AGC_CONTROL));
  964. }
  965. /*
  966. * Restore maxCCAPower register parameter again so that we're not capped
  967. * by the median we just loaded. This will be initial (and max) value
  968. * of next noise floor calibration the baseband does.
  969. */
  970. for (i = 0; i < NUM_NF_READINGS; i++) {
  971. if (chainmask & (1 << i)) {
  972. val = REG_READ(ah, ar9300_cca_regs[i]);
  973. val &= 0xFFFFFE00;
  974. val |= (((u32) (-50) << 1) & 0x1ff);
  975. REG_WRITE(ah, ar9300_cca_regs[i], val);
  976. }
  977. }
  978. }
  979. void ar9003_hw_attach_phy_ops(struct ath_hw *ah)
  980. {
  981. struct ath_hw_private_ops *priv_ops = ath9k_hw_private_ops(ah);
  982. priv_ops->rf_set_freq = ar9003_hw_set_channel;
  983. priv_ops->spur_mitigate_freq = ar9003_hw_spur_mitigate;
  984. priv_ops->compute_pll_control = ar9003_hw_compute_pll_control;
  985. priv_ops->set_channel_regs = ar9003_hw_set_channel_regs;
  986. priv_ops->init_bb = ar9003_hw_init_bb;
  987. priv_ops->process_ini = ar9003_hw_process_ini;
  988. priv_ops->set_rfmode = ar9003_hw_set_rfmode;
  989. priv_ops->mark_phy_inactive = ar9003_hw_mark_phy_inactive;
  990. priv_ops->set_delta_slope = ar9003_hw_set_delta_slope;
  991. priv_ops->rfbus_req = ar9003_hw_rfbus_req;
  992. priv_ops->rfbus_done = ar9003_hw_rfbus_done;
  993. priv_ops->enable_rfkill = ar9003_hw_enable_rfkill;
  994. priv_ops->set_diversity = ar9003_hw_set_diversity;
  995. priv_ops->ani_control = ar9003_hw_ani_control;
  996. priv_ops->do_getnf = ar9003_hw_do_getnf;
  997. priv_ops->loadnf = ar9003_hw_loadnf;
  998. }