hw.c 75 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982
  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 <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <asm/unaligned.h>
  20. #include "hw.h"
  21. #include "hw-ops.h"
  22. #include "rc.h"
  23. #include "ar9003_mac.h"
  24. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
  25. MODULE_AUTHOR("Atheros Communications");
  26. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  27. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  28. MODULE_LICENSE("Dual BSD/GPL");
  29. static int __init ath9k_init(void)
  30. {
  31. return 0;
  32. }
  33. module_init(ath9k_init);
  34. static void __exit ath9k_exit(void)
  35. {
  36. return;
  37. }
  38. module_exit(ath9k_exit);
  39. /* Private hardware callbacks */
  40. static void ath9k_hw_init_cal_settings(struct ath_hw *ah)
  41. {
  42. ath9k_hw_private_ops(ah)->init_cal_settings(ah);
  43. }
  44. static void ath9k_hw_init_mode_regs(struct ath_hw *ah)
  45. {
  46. ath9k_hw_private_ops(ah)->init_mode_regs(ah);
  47. }
  48. static u32 ath9k_hw_compute_pll_control(struct ath_hw *ah,
  49. struct ath9k_channel *chan)
  50. {
  51. return ath9k_hw_private_ops(ah)->compute_pll_control(ah, chan);
  52. }
  53. static void ath9k_hw_init_mode_gain_regs(struct ath_hw *ah)
  54. {
  55. if (!ath9k_hw_private_ops(ah)->init_mode_gain_regs)
  56. return;
  57. ath9k_hw_private_ops(ah)->init_mode_gain_regs(ah);
  58. }
  59. static void ath9k_hw_ani_cache_ini_regs(struct ath_hw *ah)
  60. {
  61. /* You will not have this callback if using the old ANI */
  62. if (!ath9k_hw_private_ops(ah)->ani_cache_ini_regs)
  63. return;
  64. ath9k_hw_private_ops(ah)->ani_cache_ini_regs(ah);
  65. }
  66. /********************/
  67. /* Helper Functions */
  68. /********************/
  69. static void ath9k_hw_set_clockrate(struct ath_hw *ah)
  70. {
  71. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  72. struct ath_common *common = ath9k_hw_common(ah);
  73. unsigned int clockrate;
  74. /* AR9287 v1.3+ uses async FIFO and runs the MAC at 117 MHz */
  75. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah))
  76. clockrate = 117;
  77. else if (!ah->curchan) /* should really check for CCK instead */
  78. clockrate = ATH9K_CLOCK_RATE_CCK;
  79. else if (conf->channel->band == IEEE80211_BAND_2GHZ)
  80. clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
  81. else if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
  82. clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
  83. else
  84. clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
  85. if (conf_is_ht40(conf))
  86. clockrate *= 2;
  87. if (ah->curchan) {
  88. if (IS_CHAN_HALF_RATE(ah->curchan))
  89. clockrate /= 2;
  90. if (IS_CHAN_QUARTER_RATE(ah->curchan))
  91. clockrate /= 4;
  92. }
  93. common->clockrate = clockrate;
  94. }
  95. static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
  96. {
  97. struct ath_common *common = ath9k_hw_common(ah);
  98. return usecs * common->clockrate;
  99. }
  100. bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
  101. {
  102. int i;
  103. BUG_ON(timeout < AH_TIME_QUANTUM);
  104. for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
  105. if ((REG_READ(ah, reg) & mask) == val)
  106. return true;
  107. udelay(AH_TIME_QUANTUM);
  108. }
  109. ath_dbg(ath9k_hw_common(ah), ATH_DBG_ANY,
  110. "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
  111. timeout, reg, REG_READ(ah, reg), mask, val);
  112. return false;
  113. }
  114. EXPORT_SYMBOL(ath9k_hw_wait);
  115. void ath9k_hw_write_array(struct ath_hw *ah, struct ar5416IniArray *array,
  116. int column, unsigned int *writecnt)
  117. {
  118. int r;
  119. ENABLE_REGWRITE_BUFFER(ah);
  120. for (r = 0; r < array->ia_rows; r++) {
  121. REG_WRITE(ah, INI_RA(array, r, 0),
  122. INI_RA(array, r, column));
  123. DO_DELAY(*writecnt);
  124. }
  125. REGWRITE_BUFFER_FLUSH(ah);
  126. }
  127. u32 ath9k_hw_reverse_bits(u32 val, u32 n)
  128. {
  129. u32 retval;
  130. int i;
  131. for (i = 0, retval = 0; i < n; i++) {
  132. retval = (retval << 1) | (val & 1);
  133. val >>= 1;
  134. }
  135. return retval;
  136. }
  137. u16 ath9k_hw_computetxtime(struct ath_hw *ah,
  138. u8 phy, int kbps,
  139. u32 frameLen, u16 rateix,
  140. bool shortPreamble)
  141. {
  142. u32 bitsPerSymbol, numBits, numSymbols, phyTime, txTime;
  143. if (kbps == 0)
  144. return 0;
  145. switch (phy) {
  146. case WLAN_RC_PHY_CCK:
  147. phyTime = CCK_PREAMBLE_BITS + CCK_PLCP_BITS;
  148. if (shortPreamble)
  149. phyTime >>= 1;
  150. numBits = frameLen << 3;
  151. txTime = CCK_SIFS_TIME + phyTime + ((numBits * 1000) / kbps);
  152. break;
  153. case WLAN_RC_PHY_OFDM:
  154. if (ah->curchan && IS_CHAN_QUARTER_RATE(ah->curchan)) {
  155. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_QUARTER) / 1000;
  156. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  157. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  158. txTime = OFDM_SIFS_TIME_QUARTER
  159. + OFDM_PREAMBLE_TIME_QUARTER
  160. + (numSymbols * OFDM_SYMBOL_TIME_QUARTER);
  161. } else if (ah->curchan &&
  162. IS_CHAN_HALF_RATE(ah->curchan)) {
  163. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME_HALF) / 1000;
  164. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  165. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  166. txTime = OFDM_SIFS_TIME_HALF +
  167. OFDM_PREAMBLE_TIME_HALF
  168. + (numSymbols * OFDM_SYMBOL_TIME_HALF);
  169. } else {
  170. bitsPerSymbol = (kbps * OFDM_SYMBOL_TIME) / 1000;
  171. numBits = OFDM_PLCP_BITS + (frameLen << 3);
  172. numSymbols = DIV_ROUND_UP(numBits, bitsPerSymbol);
  173. txTime = OFDM_SIFS_TIME + OFDM_PREAMBLE_TIME
  174. + (numSymbols * OFDM_SYMBOL_TIME);
  175. }
  176. break;
  177. default:
  178. ath_err(ath9k_hw_common(ah),
  179. "Unknown phy %u (rate ix %u)\n", phy, rateix);
  180. txTime = 0;
  181. break;
  182. }
  183. return txTime;
  184. }
  185. EXPORT_SYMBOL(ath9k_hw_computetxtime);
  186. void ath9k_hw_get_channel_centers(struct ath_hw *ah,
  187. struct ath9k_channel *chan,
  188. struct chan_centers *centers)
  189. {
  190. int8_t extoff;
  191. if (!IS_CHAN_HT40(chan)) {
  192. centers->ctl_center = centers->ext_center =
  193. centers->synth_center = chan->channel;
  194. return;
  195. }
  196. if ((chan->chanmode == CHANNEL_A_HT40PLUS) ||
  197. (chan->chanmode == CHANNEL_G_HT40PLUS)) {
  198. centers->synth_center =
  199. chan->channel + HT40_CHANNEL_CENTER_SHIFT;
  200. extoff = 1;
  201. } else {
  202. centers->synth_center =
  203. chan->channel - HT40_CHANNEL_CENTER_SHIFT;
  204. extoff = -1;
  205. }
  206. centers->ctl_center =
  207. centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
  208. /* 25 MHz spacing is supported by hw but not on upper layers */
  209. centers->ext_center =
  210. centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
  211. }
  212. /******************/
  213. /* Chip Revisions */
  214. /******************/
  215. static void ath9k_hw_read_revisions(struct ath_hw *ah)
  216. {
  217. u32 val;
  218. switch (ah->hw_version.devid) {
  219. case AR5416_AR9100_DEVID:
  220. ah->hw_version.macVersion = AR_SREV_VERSION_9100;
  221. break;
  222. case AR9300_DEVID_AR9330:
  223. ah->hw_version.macVersion = AR_SREV_VERSION_9330;
  224. if (ah->get_mac_revision) {
  225. ah->hw_version.macRev = ah->get_mac_revision();
  226. } else {
  227. val = REG_READ(ah, AR_SREV);
  228. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  229. }
  230. return;
  231. case AR9300_DEVID_AR9340:
  232. ah->hw_version.macVersion = AR_SREV_VERSION_9340;
  233. val = REG_READ(ah, AR_SREV);
  234. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  235. return;
  236. }
  237. val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
  238. if (val == 0xFF) {
  239. val = REG_READ(ah, AR_SREV);
  240. ah->hw_version.macVersion =
  241. (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
  242. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  243. if (AR_SREV_9462(ah))
  244. ah->is_pciexpress = true;
  245. else
  246. ah->is_pciexpress = (val &
  247. AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
  248. } else {
  249. if (!AR_SREV_9100(ah))
  250. ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
  251. ah->hw_version.macRev = val & AR_SREV_REVISION;
  252. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
  253. ah->is_pciexpress = true;
  254. }
  255. }
  256. /************************************/
  257. /* HW Attach, Detach, Init Routines */
  258. /************************************/
  259. static void ath9k_hw_disablepcie(struct ath_hw *ah)
  260. {
  261. if (!AR_SREV_5416(ah))
  262. return;
  263. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  264. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  265. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
  266. REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
  267. REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
  268. REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
  269. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  270. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  271. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
  272. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  273. }
  274. static void ath9k_hw_aspm_init(struct ath_hw *ah)
  275. {
  276. struct ath_common *common = ath9k_hw_common(ah);
  277. if (common->bus_ops->aspm_init)
  278. common->bus_ops->aspm_init(common);
  279. }
  280. /* This should work for all families including legacy */
  281. static bool ath9k_hw_chip_test(struct ath_hw *ah)
  282. {
  283. struct ath_common *common = ath9k_hw_common(ah);
  284. u32 regAddr[2] = { AR_STA_ID0 };
  285. u32 regHold[2];
  286. static const u32 patternData[4] = {
  287. 0x55555555, 0xaaaaaaaa, 0x66666666, 0x99999999
  288. };
  289. int i, j, loop_max;
  290. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  291. loop_max = 2;
  292. regAddr[1] = AR_PHY_BASE + (8 << 2);
  293. } else
  294. loop_max = 1;
  295. for (i = 0; i < loop_max; i++) {
  296. u32 addr = regAddr[i];
  297. u32 wrData, rdData;
  298. regHold[i] = REG_READ(ah, addr);
  299. for (j = 0; j < 0x100; j++) {
  300. wrData = (j << 16) | j;
  301. REG_WRITE(ah, addr, wrData);
  302. rdData = REG_READ(ah, addr);
  303. if (rdData != wrData) {
  304. ath_err(common,
  305. "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
  306. addr, wrData, rdData);
  307. return false;
  308. }
  309. }
  310. for (j = 0; j < 4; j++) {
  311. wrData = patternData[j];
  312. REG_WRITE(ah, addr, wrData);
  313. rdData = REG_READ(ah, addr);
  314. if (wrData != rdData) {
  315. ath_err(common,
  316. "address test failed addr: 0x%08x - wr:0x%08x != rd:0x%08x\n",
  317. addr, wrData, rdData);
  318. return false;
  319. }
  320. }
  321. REG_WRITE(ah, regAddr[i], regHold[i]);
  322. }
  323. udelay(100);
  324. return true;
  325. }
  326. static void ath9k_hw_init_config(struct ath_hw *ah)
  327. {
  328. int i;
  329. ah->config.dma_beacon_response_time = 2;
  330. ah->config.sw_beacon_response_time = 10;
  331. ah->config.additional_swba_backoff = 0;
  332. ah->config.ack_6mb = 0x0;
  333. ah->config.cwm_ignore_extcca = 0;
  334. ah->config.pcie_clock_req = 0;
  335. ah->config.pcie_waen = 0;
  336. ah->config.analog_shiftreg = 1;
  337. ah->config.enable_ani = true;
  338. for (i = 0; i < AR_EEPROM_MODAL_SPURS; i++) {
  339. ah->config.spurchans[i][0] = AR_NO_SPUR;
  340. ah->config.spurchans[i][1] = AR_NO_SPUR;
  341. }
  342. /* PAPRD needs some more work to be enabled */
  343. ah->config.paprd_disable = 1;
  344. ah->config.rx_intr_mitigation = true;
  345. ah->config.pcieSerDesWrite = true;
  346. /*
  347. * We need this for PCI devices only (Cardbus, PCI, miniPCI)
  348. * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
  349. * This means we use it for all AR5416 devices, and the few
  350. * minor PCI AR9280 devices out there.
  351. *
  352. * Serialization is required because these devices do not handle
  353. * well the case of two concurrent reads/writes due to the latency
  354. * involved. During one read/write another read/write can be issued
  355. * on another CPU while the previous read/write may still be working
  356. * on our hardware, if we hit this case the hardware poops in a loop.
  357. * We prevent this by serializing reads and writes.
  358. *
  359. * This issue is not present on PCI-Express devices or pre-AR5416
  360. * devices (legacy, 802.11abg).
  361. */
  362. if (num_possible_cpus() > 1)
  363. ah->config.serialize_regmode = SER_REG_MODE_AUTO;
  364. }
  365. static void ath9k_hw_init_defaults(struct ath_hw *ah)
  366. {
  367. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  368. regulatory->country_code = CTRY_DEFAULT;
  369. regulatory->power_limit = MAX_RATE_POWER;
  370. ah->hw_version.magic = AR5416_MAGIC;
  371. ah->hw_version.subvendorid = 0;
  372. ah->atim_window = 0;
  373. ah->sta_id1_defaults =
  374. AR_STA_ID1_CRPT_MIC_ENABLE |
  375. AR_STA_ID1_MCAST_KSRCH;
  376. if (AR_SREV_9100(ah))
  377. ah->sta_id1_defaults |= AR_STA_ID1_AR9100_BA_FIX;
  378. ah->enable_32kHz_clock = DONT_USE_32KHZ;
  379. ah->slottime = ATH9K_SLOT_TIME_9;
  380. ah->globaltxtimeout = (u32) -1;
  381. ah->power_mode = ATH9K_PM_UNDEFINED;
  382. }
  383. static int ath9k_hw_init_macaddr(struct ath_hw *ah)
  384. {
  385. struct ath_common *common = ath9k_hw_common(ah);
  386. u32 sum;
  387. int i;
  388. u16 eeval;
  389. static const u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
  390. sum = 0;
  391. for (i = 0; i < 3; i++) {
  392. eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
  393. sum += eeval;
  394. common->macaddr[2 * i] = eeval >> 8;
  395. common->macaddr[2 * i + 1] = eeval & 0xff;
  396. }
  397. if (sum == 0 || sum == 0xffff * 3)
  398. return -EADDRNOTAVAIL;
  399. return 0;
  400. }
  401. static int ath9k_hw_post_init(struct ath_hw *ah)
  402. {
  403. struct ath_common *common = ath9k_hw_common(ah);
  404. int ecode;
  405. if (common->bus_ops->ath_bus_type != ATH_USB) {
  406. if (!ath9k_hw_chip_test(ah))
  407. return -ENODEV;
  408. }
  409. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  410. ecode = ar9002_hw_rf_claim(ah);
  411. if (ecode != 0)
  412. return ecode;
  413. }
  414. ecode = ath9k_hw_eeprom_init(ah);
  415. if (ecode != 0)
  416. return ecode;
  417. ath_dbg(ath9k_hw_common(ah), ATH_DBG_CONFIG,
  418. "Eeprom VER: %d, REV: %d\n",
  419. ah->eep_ops->get_eeprom_ver(ah),
  420. ah->eep_ops->get_eeprom_rev(ah));
  421. ecode = ath9k_hw_rf_alloc_ext_banks(ah);
  422. if (ecode) {
  423. ath_err(ath9k_hw_common(ah),
  424. "Failed allocating banks for external radio\n");
  425. ath9k_hw_rf_free_ext_banks(ah);
  426. return ecode;
  427. }
  428. if (!AR_SREV_9100(ah) && !AR_SREV_9340(ah)) {
  429. ath9k_hw_ani_setup(ah);
  430. ath9k_hw_ani_init(ah);
  431. }
  432. return 0;
  433. }
  434. static void ath9k_hw_attach_ops(struct ath_hw *ah)
  435. {
  436. if (AR_SREV_9300_20_OR_LATER(ah))
  437. ar9003_hw_attach_ops(ah);
  438. else
  439. ar9002_hw_attach_ops(ah);
  440. }
  441. /* Called for all hardware families */
  442. static int __ath9k_hw_init(struct ath_hw *ah)
  443. {
  444. struct ath_common *common = ath9k_hw_common(ah);
  445. int r = 0;
  446. ath9k_hw_read_revisions(ah);
  447. /*
  448. * Read back AR_WA into a permanent copy and set bits 14 and 17.
  449. * We need to do this to avoid RMW of this register. We cannot
  450. * read the reg when chip is asleep.
  451. */
  452. ah->WARegVal = REG_READ(ah, AR_WA);
  453. ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
  454. AR_WA_ASPM_TIMER_BASED_DISABLE);
  455. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  456. ath_err(common, "Couldn't reset chip\n");
  457. return -EIO;
  458. }
  459. if (AR_SREV_9462(ah))
  460. ah->WARegVal &= ~AR_WA_D3_L1_DISABLE;
  461. ath9k_hw_init_defaults(ah);
  462. ath9k_hw_init_config(ah);
  463. ath9k_hw_attach_ops(ah);
  464. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  465. ath_err(common, "Couldn't wakeup chip\n");
  466. return -EIO;
  467. }
  468. if (ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
  469. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
  470. ((AR_SREV_9160(ah) || AR_SREV_9280(ah)) &&
  471. !ah->is_pciexpress)) {
  472. ah->config.serialize_regmode =
  473. SER_REG_MODE_ON;
  474. } else {
  475. ah->config.serialize_regmode =
  476. SER_REG_MODE_OFF;
  477. }
  478. }
  479. ath_dbg(common, ATH_DBG_RESET, "serialize_regmode is %d\n",
  480. ah->config.serialize_regmode);
  481. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  482. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
  483. else
  484. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
  485. switch (ah->hw_version.macVersion) {
  486. case AR_SREV_VERSION_5416_PCI:
  487. case AR_SREV_VERSION_5416_PCIE:
  488. case AR_SREV_VERSION_9160:
  489. case AR_SREV_VERSION_9100:
  490. case AR_SREV_VERSION_9280:
  491. case AR_SREV_VERSION_9285:
  492. case AR_SREV_VERSION_9287:
  493. case AR_SREV_VERSION_9271:
  494. case AR_SREV_VERSION_9300:
  495. case AR_SREV_VERSION_9330:
  496. case AR_SREV_VERSION_9485:
  497. case AR_SREV_VERSION_9340:
  498. case AR_SREV_VERSION_9462:
  499. break;
  500. default:
  501. ath_err(common,
  502. "Mac Chip Rev 0x%02x.%x is not supported by this driver\n",
  503. ah->hw_version.macVersion, ah->hw_version.macRev);
  504. return -EOPNOTSUPP;
  505. }
  506. if (AR_SREV_9271(ah) || AR_SREV_9100(ah) || AR_SREV_9340(ah) ||
  507. AR_SREV_9330(ah))
  508. ah->is_pciexpress = false;
  509. ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
  510. ath9k_hw_init_cal_settings(ah);
  511. ah->ani_function = ATH9K_ANI_ALL;
  512. if (AR_SREV_9280_20_OR_LATER(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  513. ah->ani_function &= ~ATH9K_ANI_NOISE_IMMUNITY_LEVEL;
  514. if (!AR_SREV_9300_20_OR_LATER(ah))
  515. ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
  516. ath9k_hw_init_mode_regs(ah);
  517. if (!ah->is_pciexpress)
  518. ath9k_hw_disablepcie(ah);
  519. if (!AR_SREV_9300_20_OR_LATER(ah))
  520. ar9002_hw_cck_chan14_spread(ah);
  521. r = ath9k_hw_post_init(ah);
  522. if (r)
  523. return r;
  524. ath9k_hw_init_mode_gain_regs(ah);
  525. r = ath9k_hw_fill_cap_info(ah);
  526. if (r)
  527. return r;
  528. if (ah->is_pciexpress)
  529. ath9k_hw_aspm_init(ah);
  530. r = ath9k_hw_init_macaddr(ah);
  531. if (r) {
  532. ath_err(common, "Failed to initialize MAC address\n");
  533. return r;
  534. }
  535. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  536. ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
  537. else
  538. ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
  539. if (AR_SREV_9330(ah))
  540. ah->bb_watchdog_timeout_ms = 85;
  541. else
  542. ah->bb_watchdog_timeout_ms = 25;
  543. common->state = ATH_HW_INITIALIZED;
  544. return 0;
  545. }
  546. int ath9k_hw_init(struct ath_hw *ah)
  547. {
  548. int ret;
  549. struct ath_common *common = ath9k_hw_common(ah);
  550. /* These are all the AR5008/AR9001/AR9002 hardware family of chipsets */
  551. switch (ah->hw_version.devid) {
  552. case AR5416_DEVID_PCI:
  553. case AR5416_DEVID_PCIE:
  554. case AR5416_AR9100_DEVID:
  555. case AR9160_DEVID_PCI:
  556. case AR9280_DEVID_PCI:
  557. case AR9280_DEVID_PCIE:
  558. case AR9285_DEVID_PCIE:
  559. case AR9287_DEVID_PCI:
  560. case AR9287_DEVID_PCIE:
  561. case AR2427_DEVID_PCIE:
  562. case AR9300_DEVID_PCIE:
  563. case AR9300_DEVID_AR9485_PCIE:
  564. case AR9300_DEVID_AR9330:
  565. case AR9300_DEVID_AR9340:
  566. case AR9300_DEVID_AR9580:
  567. case AR9300_DEVID_AR9462:
  568. break;
  569. default:
  570. if (common->bus_ops->ath_bus_type == ATH_USB)
  571. break;
  572. ath_err(common, "Hardware device ID 0x%04x not supported\n",
  573. ah->hw_version.devid);
  574. return -EOPNOTSUPP;
  575. }
  576. ret = __ath9k_hw_init(ah);
  577. if (ret) {
  578. ath_err(common,
  579. "Unable to initialize hardware; initialization status: %d\n",
  580. ret);
  581. return ret;
  582. }
  583. return 0;
  584. }
  585. EXPORT_SYMBOL(ath9k_hw_init);
  586. static void ath9k_hw_init_qos(struct ath_hw *ah)
  587. {
  588. ENABLE_REGWRITE_BUFFER(ah);
  589. REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
  590. REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
  591. REG_WRITE(ah, AR_QOS_NO_ACK,
  592. SM(2, AR_QOS_NO_ACK_TWO_BIT) |
  593. SM(5, AR_QOS_NO_ACK_BIT_OFF) |
  594. SM(0, AR_QOS_NO_ACK_BYTE_OFF));
  595. REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
  596. REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
  597. REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
  598. REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
  599. REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
  600. REGWRITE_BUFFER_FLUSH(ah);
  601. }
  602. u32 ar9003_get_pll_sqsum_dvc(struct ath_hw *ah)
  603. {
  604. REG_CLR_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
  605. udelay(100);
  606. REG_SET_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
  607. while ((REG_READ(ah, PLL4) & PLL4_MEAS_DONE) == 0)
  608. udelay(100);
  609. return (REG_READ(ah, PLL3) & SQSUM_DVC_MASK) >> 3;
  610. }
  611. EXPORT_SYMBOL(ar9003_get_pll_sqsum_dvc);
  612. static void ath9k_hw_init_pll(struct ath_hw *ah,
  613. struct ath9k_channel *chan)
  614. {
  615. u32 pll;
  616. if (AR_SREV_9485(ah)) {
  617. /* program BB PLL ki and kd value, ki=0x4, kd=0x40 */
  618. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  619. AR_CH0_BB_DPLL2_PLL_PWD, 0x1);
  620. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  621. AR_CH0_DPLL2_KD, 0x40);
  622. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  623. AR_CH0_DPLL2_KI, 0x4);
  624. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  625. AR_CH0_BB_DPLL1_REFDIV, 0x5);
  626. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  627. AR_CH0_BB_DPLL1_NINI, 0x58);
  628. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  629. AR_CH0_BB_DPLL1_NFRAC, 0x0);
  630. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  631. AR_CH0_BB_DPLL2_OUTDIV, 0x1);
  632. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  633. AR_CH0_BB_DPLL2_LOCAL_PLL, 0x1);
  634. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  635. AR_CH0_BB_DPLL2_EN_NEGTRIG, 0x1);
  636. /* program BB PLL phase_shift to 0x6 */
  637. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
  638. AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x6);
  639. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  640. AR_CH0_BB_DPLL2_PLL_PWD, 0x0);
  641. udelay(1000);
  642. } else if (AR_SREV_9330(ah)) {
  643. u32 ddr_dpll2, pll_control2, kd;
  644. if (ah->is_clk_25mhz) {
  645. ddr_dpll2 = 0x18e82f01;
  646. pll_control2 = 0xe04a3d;
  647. kd = 0x1d;
  648. } else {
  649. ddr_dpll2 = 0x19e82f01;
  650. pll_control2 = 0x886666;
  651. kd = 0x3d;
  652. }
  653. /* program DDR PLL ki and kd value */
  654. REG_WRITE(ah, AR_CH0_DDR_DPLL2, ddr_dpll2);
  655. /* program DDR PLL phase_shift */
  656. REG_RMW_FIELD(ah, AR_CH0_DDR_DPLL3,
  657. AR_CH0_DPLL3_PHASE_SHIFT, 0x1);
  658. REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
  659. udelay(1000);
  660. /* program refdiv, nint, frac to RTC register */
  661. REG_WRITE(ah, AR_RTC_PLL_CONTROL2, pll_control2);
  662. /* program BB PLL kd and ki value */
  663. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KD, kd);
  664. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KI, 0x06);
  665. /* program BB PLL phase_shift */
  666. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
  667. AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x1);
  668. } else if (AR_SREV_9340(ah)) {
  669. u32 regval, pll2_divint, pll2_divfrac, refdiv;
  670. REG_WRITE(ah, AR_RTC_PLL_CONTROL, 0x1142c);
  671. udelay(1000);
  672. REG_SET_BIT(ah, AR_PHY_PLL_MODE, 0x1 << 16);
  673. udelay(100);
  674. if (ah->is_clk_25mhz) {
  675. pll2_divint = 0x54;
  676. pll2_divfrac = 0x1eb85;
  677. refdiv = 3;
  678. } else {
  679. pll2_divint = 88;
  680. pll2_divfrac = 0;
  681. refdiv = 5;
  682. }
  683. regval = REG_READ(ah, AR_PHY_PLL_MODE);
  684. regval |= (0x1 << 16);
  685. REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
  686. udelay(100);
  687. REG_WRITE(ah, AR_PHY_PLL_CONTROL, (refdiv << 27) |
  688. (pll2_divint << 18) | pll2_divfrac);
  689. udelay(100);
  690. regval = REG_READ(ah, AR_PHY_PLL_MODE);
  691. regval = (regval & 0x80071fff) | (0x1 << 30) | (0x1 << 13) |
  692. (0x4 << 26) | (0x18 << 19);
  693. REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
  694. REG_WRITE(ah, AR_PHY_PLL_MODE,
  695. REG_READ(ah, AR_PHY_PLL_MODE) & 0xfffeffff);
  696. udelay(1000);
  697. }
  698. pll = ath9k_hw_compute_pll_control(ah, chan);
  699. REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
  700. if (AR_SREV_9485(ah) || AR_SREV_9340(ah) || AR_SREV_9330(ah))
  701. udelay(1000);
  702. /* Switch the core clock for ar9271 to 117Mhz */
  703. if (AR_SREV_9271(ah)) {
  704. udelay(500);
  705. REG_WRITE(ah, 0x50040, 0x304);
  706. }
  707. udelay(RTC_PLL_SETTLE_DELAY);
  708. REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
  709. if (AR_SREV_9340(ah)) {
  710. if (ah->is_clk_25mhz) {
  711. REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x17c << 1);
  712. REG_WRITE(ah, AR_SLP32_MODE, 0x0010f3d7);
  713. REG_WRITE(ah, AR_SLP32_INC, 0x0001e7ae);
  714. } else {
  715. REG_WRITE(ah, AR_RTC_DERIVED_CLK, 0x261 << 1);
  716. REG_WRITE(ah, AR_SLP32_MODE, 0x0010f400);
  717. REG_WRITE(ah, AR_SLP32_INC, 0x0001e800);
  718. }
  719. udelay(100);
  720. }
  721. }
  722. static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
  723. enum nl80211_iftype opmode)
  724. {
  725. u32 sync_default = AR_INTR_SYNC_DEFAULT;
  726. u32 imr_reg = AR_IMR_TXERR |
  727. AR_IMR_TXURN |
  728. AR_IMR_RXERR |
  729. AR_IMR_RXORN |
  730. AR_IMR_BCNMISC;
  731. if (AR_SREV_9340(ah))
  732. sync_default &= ~AR_INTR_SYNC_HOST1_FATAL;
  733. if (AR_SREV_9300_20_OR_LATER(ah)) {
  734. imr_reg |= AR_IMR_RXOK_HP;
  735. if (ah->config.rx_intr_mitigation)
  736. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  737. else
  738. imr_reg |= AR_IMR_RXOK_LP;
  739. } else {
  740. if (ah->config.rx_intr_mitigation)
  741. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  742. else
  743. imr_reg |= AR_IMR_RXOK;
  744. }
  745. if (ah->config.tx_intr_mitigation)
  746. imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
  747. else
  748. imr_reg |= AR_IMR_TXOK;
  749. if (opmode == NL80211_IFTYPE_AP)
  750. imr_reg |= AR_IMR_MIB;
  751. ENABLE_REGWRITE_BUFFER(ah);
  752. REG_WRITE(ah, AR_IMR, imr_reg);
  753. ah->imrs2_reg |= AR_IMR_S2_GTT;
  754. REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
  755. if (!AR_SREV_9100(ah)) {
  756. REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
  757. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, sync_default);
  758. REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
  759. }
  760. REGWRITE_BUFFER_FLUSH(ah);
  761. if (AR_SREV_9300_20_OR_LATER(ah)) {
  762. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
  763. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
  764. REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
  765. REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
  766. }
  767. }
  768. static void ath9k_hw_set_sifs_time(struct ath_hw *ah, u32 us)
  769. {
  770. u32 val = ath9k_hw_mac_to_clks(ah, us - 2);
  771. val = min(val, (u32) 0xFFFF);
  772. REG_WRITE(ah, AR_D_GBL_IFS_SIFS, val);
  773. }
  774. static void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
  775. {
  776. u32 val = ath9k_hw_mac_to_clks(ah, us);
  777. val = min(val, (u32) 0xFFFF);
  778. REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
  779. }
  780. static void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
  781. {
  782. u32 val = ath9k_hw_mac_to_clks(ah, us);
  783. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
  784. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
  785. }
  786. static void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
  787. {
  788. u32 val = ath9k_hw_mac_to_clks(ah, us);
  789. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
  790. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
  791. }
  792. static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
  793. {
  794. if (tu > 0xFFFF) {
  795. ath_dbg(ath9k_hw_common(ah), ATH_DBG_XMIT,
  796. "bad global tx timeout %u\n", tu);
  797. ah->globaltxtimeout = (u32) -1;
  798. return false;
  799. } else {
  800. REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
  801. ah->globaltxtimeout = tu;
  802. return true;
  803. }
  804. }
  805. void ath9k_hw_init_global_settings(struct ath_hw *ah)
  806. {
  807. struct ath_common *common = ath9k_hw_common(ah);
  808. struct ieee80211_conf *conf = &common->hw->conf;
  809. const struct ath9k_channel *chan = ah->curchan;
  810. int acktimeout, ctstimeout;
  811. int slottime;
  812. int sifstime;
  813. int rx_lat = 0, tx_lat = 0, eifs = 0;
  814. u32 reg;
  815. ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET, "ah->misc_mode 0x%x\n",
  816. ah->misc_mode);
  817. if (!chan)
  818. return;
  819. if (ah->misc_mode != 0)
  820. REG_SET_BIT(ah, AR_PCU_MISC, ah->misc_mode);
  821. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  822. rx_lat = 41;
  823. else
  824. rx_lat = 37;
  825. tx_lat = 54;
  826. if (IS_CHAN_HALF_RATE(chan)) {
  827. eifs = 175;
  828. rx_lat *= 2;
  829. tx_lat *= 2;
  830. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  831. tx_lat += 11;
  832. slottime = 13;
  833. sifstime = 32;
  834. } else if (IS_CHAN_QUARTER_RATE(chan)) {
  835. eifs = 340;
  836. rx_lat = (rx_lat * 4) - 1;
  837. tx_lat *= 4;
  838. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  839. tx_lat += 22;
  840. slottime = 21;
  841. sifstime = 64;
  842. } else {
  843. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
  844. eifs = AR_D_GBL_IFS_EIFS_ASYNC_FIFO;
  845. reg = AR_USEC_ASYNC_FIFO;
  846. } else {
  847. eifs = REG_READ(ah, AR_D_GBL_IFS_EIFS)/
  848. common->clockrate;
  849. reg = REG_READ(ah, AR_USEC);
  850. }
  851. rx_lat = MS(reg, AR_USEC_RX_LAT);
  852. tx_lat = MS(reg, AR_USEC_TX_LAT);
  853. slottime = ah->slottime;
  854. if (IS_CHAN_5GHZ(chan))
  855. sifstime = 16;
  856. else
  857. sifstime = 10;
  858. }
  859. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  860. acktimeout = slottime + sifstime + 3 * ah->coverage_class;
  861. ctstimeout = acktimeout;
  862. /*
  863. * Workaround for early ACK timeouts, add an offset to match the
  864. * initval's 64us ack timeout value.
  865. * This was initially only meant to work around an issue with delayed
  866. * BA frames in some implementations, but it has been found to fix ACK
  867. * timeout issues in other cases as well.
  868. */
  869. if (conf->channel && conf->channel->band == IEEE80211_BAND_2GHZ)
  870. acktimeout += 64 - sifstime - ah->slottime;
  871. ath9k_hw_set_sifs_time(ah, sifstime);
  872. ath9k_hw_setslottime(ah, slottime);
  873. ath9k_hw_set_ack_timeout(ah, acktimeout);
  874. ath9k_hw_set_cts_timeout(ah, ctstimeout);
  875. if (ah->globaltxtimeout != (u32) -1)
  876. ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
  877. REG_WRITE(ah, AR_D_GBL_IFS_EIFS, ath9k_hw_mac_to_clks(ah, eifs));
  878. REG_RMW(ah, AR_USEC,
  879. (common->clockrate - 1) |
  880. SM(rx_lat, AR_USEC_RX_LAT) |
  881. SM(tx_lat, AR_USEC_TX_LAT),
  882. AR_USEC_TX_LAT | AR_USEC_RX_LAT | AR_USEC_USEC);
  883. }
  884. EXPORT_SYMBOL(ath9k_hw_init_global_settings);
  885. void ath9k_hw_deinit(struct ath_hw *ah)
  886. {
  887. struct ath_common *common = ath9k_hw_common(ah);
  888. if (common->state < ATH_HW_INITIALIZED)
  889. goto free_hw;
  890. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  891. free_hw:
  892. ath9k_hw_rf_free_ext_banks(ah);
  893. }
  894. EXPORT_SYMBOL(ath9k_hw_deinit);
  895. /*******/
  896. /* INI */
  897. /*******/
  898. u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
  899. {
  900. u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
  901. if (IS_CHAN_B(chan))
  902. ctl |= CTL_11B;
  903. else if (IS_CHAN_G(chan))
  904. ctl |= CTL_11G;
  905. else
  906. ctl |= CTL_11A;
  907. return ctl;
  908. }
  909. /****************************************/
  910. /* Reset and Channel Switching Routines */
  911. /****************************************/
  912. static inline void ath9k_hw_set_dma(struct ath_hw *ah)
  913. {
  914. struct ath_common *common = ath9k_hw_common(ah);
  915. ENABLE_REGWRITE_BUFFER(ah);
  916. /*
  917. * set AHB_MODE not to do cacheline prefetches
  918. */
  919. if (!AR_SREV_9300_20_OR_LATER(ah))
  920. REG_SET_BIT(ah, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
  921. /*
  922. * let mac dma reads be in 128 byte chunks
  923. */
  924. REG_RMW(ah, AR_TXCFG, AR_TXCFG_DMASZ_128B, AR_TXCFG_DMASZ_MASK);
  925. REGWRITE_BUFFER_FLUSH(ah);
  926. /*
  927. * Restore TX Trigger Level to its pre-reset value.
  928. * The initial value depends on whether aggregation is enabled, and is
  929. * adjusted whenever underruns are detected.
  930. */
  931. if (!AR_SREV_9300_20_OR_LATER(ah))
  932. REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
  933. ENABLE_REGWRITE_BUFFER(ah);
  934. /*
  935. * let mac dma writes be in 128 byte chunks
  936. */
  937. REG_RMW(ah, AR_RXCFG, AR_RXCFG_DMASZ_128B, AR_RXCFG_DMASZ_MASK);
  938. /*
  939. * Setup receive FIFO threshold to hold off TX activities
  940. */
  941. REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
  942. if (AR_SREV_9300_20_OR_LATER(ah)) {
  943. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
  944. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
  945. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  946. ah->caps.rx_status_len);
  947. }
  948. /*
  949. * reduce the number of usable entries in PCU TXBUF to avoid
  950. * wrap around issues.
  951. */
  952. if (AR_SREV_9285(ah)) {
  953. /* For AR9285 the number of Fifos are reduced to half.
  954. * So set the usable tx buf size also to half to
  955. * avoid data/delimiter underruns
  956. */
  957. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  958. AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE);
  959. } else if (!AR_SREV_9271(ah)) {
  960. REG_WRITE(ah, AR_PCU_TXBUF_CTRL,
  961. AR_PCU_TXBUF_CTRL_USABLE_SIZE);
  962. }
  963. REGWRITE_BUFFER_FLUSH(ah);
  964. if (AR_SREV_9300_20_OR_LATER(ah))
  965. ath9k_hw_reset_txstatus_ring(ah);
  966. }
  967. static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
  968. {
  969. u32 mask = AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC;
  970. u32 set = AR_STA_ID1_KSRCH_MODE;
  971. switch (opmode) {
  972. case NL80211_IFTYPE_ADHOC:
  973. case NL80211_IFTYPE_MESH_POINT:
  974. set |= AR_STA_ID1_ADHOC;
  975. REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  976. break;
  977. case NL80211_IFTYPE_AP:
  978. set |= AR_STA_ID1_STA_AP;
  979. /* fall through */
  980. case NL80211_IFTYPE_STATION:
  981. REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  982. break;
  983. default:
  984. if (!ah->is_monitoring)
  985. set = 0;
  986. break;
  987. }
  988. REG_RMW(ah, AR_STA_ID1, set, mask);
  989. }
  990. void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
  991. u32 *coef_mantissa, u32 *coef_exponent)
  992. {
  993. u32 coef_exp, coef_man;
  994. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  995. if ((coef_scaled >> coef_exp) & 0x1)
  996. break;
  997. coef_exp = 14 - (coef_exp - COEF_SCALE_S);
  998. coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
  999. *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
  1000. *coef_exponent = coef_exp - 16;
  1001. }
  1002. static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
  1003. {
  1004. u32 rst_flags;
  1005. u32 tmpReg;
  1006. if (AR_SREV_9100(ah)) {
  1007. REG_RMW_FIELD(ah, AR_RTC_DERIVED_CLK,
  1008. AR_RTC_DERIVED_CLK_PERIOD, 1);
  1009. (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
  1010. }
  1011. ENABLE_REGWRITE_BUFFER(ah);
  1012. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1013. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1014. udelay(10);
  1015. }
  1016. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1017. AR_RTC_FORCE_WAKE_ON_INT);
  1018. if (AR_SREV_9100(ah)) {
  1019. rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
  1020. AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
  1021. } else {
  1022. tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  1023. if (tmpReg &
  1024. (AR_INTR_SYNC_LOCAL_TIMEOUT |
  1025. AR_INTR_SYNC_RADM_CPL_TIMEOUT)) {
  1026. u32 val;
  1027. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  1028. val = AR_RC_HOSTIF;
  1029. if (!AR_SREV_9300_20_OR_LATER(ah))
  1030. val |= AR_RC_AHB;
  1031. REG_WRITE(ah, AR_RC, val);
  1032. } else if (!AR_SREV_9300_20_OR_LATER(ah))
  1033. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  1034. rst_flags = AR_RTC_RC_MAC_WARM;
  1035. if (type == ATH9K_RESET_COLD)
  1036. rst_flags |= AR_RTC_RC_MAC_COLD;
  1037. }
  1038. if (AR_SREV_9330(ah)) {
  1039. int npend = 0;
  1040. int i;
  1041. /* AR9330 WAR:
  1042. * call external reset function to reset WMAC if:
  1043. * - doing a cold reset
  1044. * - we have pending frames in the TX queues
  1045. */
  1046. for (i = 0; i < AR_NUM_QCU; i++) {
  1047. npend = ath9k_hw_numtxpending(ah, i);
  1048. if (npend)
  1049. break;
  1050. }
  1051. if (ah->external_reset &&
  1052. (npend || type == ATH9K_RESET_COLD)) {
  1053. int reset_err = 0;
  1054. ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
  1055. "reset MAC via external reset\n");
  1056. reset_err = ah->external_reset();
  1057. if (reset_err) {
  1058. ath_err(ath9k_hw_common(ah),
  1059. "External reset failed, err=%d\n",
  1060. reset_err);
  1061. return false;
  1062. }
  1063. REG_WRITE(ah, AR_RTC_RESET, 1);
  1064. }
  1065. }
  1066. REG_WRITE(ah, AR_RTC_RC, rst_flags);
  1067. REGWRITE_BUFFER_FLUSH(ah);
  1068. udelay(50);
  1069. REG_WRITE(ah, AR_RTC_RC, 0);
  1070. if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
  1071. ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
  1072. "RTC stuck in MAC reset\n");
  1073. return false;
  1074. }
  1075. if (!AR_SREV_9100(ah))
  1076. REG_WRITE(ah, AR_RC, 0);
  1077. if (AR_SREV_9100(ah))
  1078. udelay(50);
  1079. return true;
  1080. }
  1081. static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
  1082. {
  1083. ENABLE_REGWRITE_BUFFER(ah);
  1084. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1085. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1086. udelay(10);
  1087. }
  1088. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1089. AR_RTC_FORCE_WAKE_ON_INT);
  1090. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1091. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  1092. REG_WRITE(ah, AR_RTC_RESET, 0);
  1093. REGWRITE_BUFFER_FLUSH(ah);
  1094. if (!AR_SREV_9300_20_OR_LATER(ah))
  1095. udelay(2);
  1096. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1097. REG_WRITE(ah, AR_RC, 0);
  1098. REG_WRITE(ah, AR_RTC_RESET, 1);
  1099. if (!ath9k_hw_wait(ah,
  1100. AR_RTC_STATUS,
  1101. AR_RTC_STATUS_M,
  1102. AR_RTC_STATUS_ON,
  1103. AH_WAIT_TIMEOUT)) {
  1104. ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
  1105. "RTC not waking up\n");
  1106. return false;
  1107. }
  1108. return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
  1109. }
  1110. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
  1111. {
  1112. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1113. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1114. udelay(10);
  1115. }
  1116. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1117. AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
  1118. switch (type) {
  1119. case ATH9K_RESET_POWER_ON:
  1120. return ath9k_hw_set_reset_power_on(ah);
  1121. case ATH9K_RESET_WARM:
  1122. case ATH9K_RESET_COLD:
  1123. return ath9k_hw_set_reset(ah, type);
  1124. default:
  1125. return false;
  1126. }
  1127. }
  1128. static bool ath9k_hw_chip_reset(struct ath_hw *ah,
  1129. struct ath9k_channel *chan)
  1130. {
  1131. if (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)) {
  1132. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON))
  1133. return false;
  1134. } else if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  1135. return false;
  1136. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1137. return false;
  1138. ah->chip_fullsleep = false;
  1139. ath9k_hw_init_pll(ah, chan);
  1140. ath9k_hw_set_rfmode(ah, chan);
  1141. return true;
  1142. }
  1143. static bool ath9k_hw_channel_change(struct ath_hw *ah,
  1144. struct ath9k_channel *chan)
  1145. {
  1146. struct ath_common *common = ath9k_hw_common(ah);
  1147. u32 qnum;
  1148. int r;
  1149. bool edma = !!(ah->caps.hw_caps & ATH9K_HW_CAP_EDMA);
  1150. bool band_switch, mode_diff;
  1151. u8 ini_reloaded;
  1152. band_switch = (chan->channelFlags & (CHANNEL_2GHZ | CHANNEL_5GHZ)) !=
  1153. (ah->curchan->channelFlags & (CHANNEL_2GHZ |
  1154. CHANNEL_5GHZ));
  1155. mode_diff = (chan->chanmode != ah->curchan->chanmode);
  1156. for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
  1157. if (ath9k_hw_numtxpending(ah, qnum)) {
  1158. ath_dbg(common, ATH_DBG_QUEUE,
  1159. "Transmit frames pending on queue %d\n", qnum);
  1160. return false;
  1161. }
  1162. }
  1163. if (!ath9k_hw_rfbus_req(ah)) {
  1164. ath_err(common, "Could not kill baseband RX\n");
  1165. return false;
  1166. }
  1167. if (edma && (band_switch || mode_diff)) {
  1168. ath9k_hw_mark_phy_inactive(ah);
  1169. udelay(5);
  1170. ath9k_hw_init_pll(ah, NULL);
  1171. if (ath9k_hw_fast_chan_change(ah, chan, &ini_reloaded)) {
  1172. ath_err(common, "Failed to do fast channel change\n");
  1173. return false;
  1174. }
  1175. }
  1176. ath9k_hw_set_channel_regs(ah, chan);
  1177. r = ath9k_hw_rf_set_freq(ah, chan);
  1178. if (r) {
  1179. ath_err(common, "Failed to set channel\n");
  1180. return false;
  1181. }
  1182. ath9k_hw_set_clockrate(ah);
  1183. ath9k_hw_apply_txpower(ah, chan);
  1184. ath9k_hw_rfbus_done(ah);
  1185. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  1186. ath9k_hw_set_delta_slope(ah, chan);
  1187. ath9k_hw_spur_mitigate_freq(ah, chan);
  1188. if (edma && (band_switch || mode_diff)) {
  1189. ah->ah_flags |= AH_FASTCC;
  1190. if (band_switch || ini_reloaded)
  1191. ah->eep_ops->set_board_values(ah, chan);
  1192. ath9k_hw_init_bb(ah, chan);
  1193. if (band_switch || ini_reloaded)
  1194. ath9k_hw_init_cal(ah, chan);
  1195. ah->ah_flags &= ~AH_FASTCC;
  1196. }
  1197. return true;
  1198. }
  1199. static void ath9k_hw_apply_gpio_override(struct ath_hw *ah)
  1200. {
  1201. u32 gpio_mask = ah->gpio_mask;
  1202. int i;
  1203. for (i = 0; gpio_mask; i++, gpio_mask >>= 1) {
  1204. if (!(gpio_mask & 1))
  1205. continue;
  1206. ath9k_hw_cfg_output(ah, i, AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1207. ath9k_hw_set_gpio(ah, i, !!(ah->gpio_val & BIT(i)));
  1208. }
  1209. }
  1210. bool ath9k_hw_check_alive(struct ath_hw *ah)
  1211. {
  1212. int count = 50;
  1213. u32 reg;
  1214. if (AR_SREV_9285_12_OR_LATER(ah))
  1215. return true;
  1216. do {
  1217. reg = REG_READ(ah, AR_OBS_BUS_1);
  1218. if ((reg & 0x7E7FFFEF) == 0x00702400)
  1219. continue;
  1220. switch (reg & 0x7E000B00) {
  1221. case 0x1E000000:
  1222. case 0x52000B00:
  1223. case 0x18000B00:
  1224. continue;
  1225. default:
  1226. return true;
  1227. }
  1228. } while (count-- > 0);
  1229. return false;
  1230. }
  1231. EXPORT_SYMBOL(ath9k_hw_check_alive);
  1232. int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
  1233. struct ath9k_hw_cal_data *caldata, bool bChannelChange)
  1234. {
  1235. struct ath_common *common = ath9k_hw_common(ah);
  1236. u32 saveLedState;
  1237. struct ath9k_channel *curchan = ah->curchan;
  1238. u32 saveDefAntenna;
  1239. u32 macStaId1;
  1240. u64 tsf = 0;
  1241. int i, r;
  1242. bool allow_fbs = false;
  1243. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1244. return -EIO;
  1245. if (curchan && !ah->chip_fullsleep)
  1246. ath9k_hw_getnf(ah, curchan);
  1247. ah->caldata = caldata;
  1248. if (caldata &&
  1249. (chan->channel != caldata->channel ||
  1250. (chan->channelFlags & ~CHANNEL_CW_INT) !=
  1251. (caldata->channelFlags & ~CHANNEL_CW_INT))) {
  1252. /* Operating channel changed, reset channel calibration data */
  1253. memset(caldata, 0, sizeof(*caldata));
  1254. ath9k_init_nfcal_hist_buffer(ah, chan);
  1255. }
  1256. ah->noise = ath9k_hw_getchan_noise(ah, chan);
  1257. if (AR_SREV_9280(ah) && common->bus_ops->ath_bus_type == ATH_PCI)
  1258. bChannelChange = false;
  1259. if (caldata &&
  1260. caldata->done_txiqcal_once &&
  1261. caldata->done_txclcal_once &&
  1262. caldata->rtt_hist.num_readings)
  1263. allow_fbs = true;
  1264. if (bChannelChange &&
  1265. (ah->chip_fullsleep != true) &&
  1266. (ah->curchan != NULL) &&
  1267. (chan->channel != ah->curchan->channel) &&
  1268. (allow_fbs ||
  1269. ((chan->channelFlags & CHANNEL_ALL) ==
  1270. (ah->curchan->channelFlags & CHANNEL_ALL)))) {
  1271. if (ath9k_hw_channel_change(ah, chan)) {
  1272. ath9k_hw_loadnf(ah, ah->curchan);
  1273. ath9k_hw_start_nfcal(ah, true);
  1274. if (AR_SREV_9271(ah))
  1275. ar9002_hw_load_ani_reg(ah, chan);
  1276. return 0;
  1277. }
  1278. }
  1279. saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
  1280. if (saveDefAntenna == 0)
  1281. saveDefAntenna = 1;
  1282. macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
  1283. /* For chips on which RTC reset is done, save TSF before it gets cleared */
  1284. if (AR_SREV_9100(ah) ||
  1285. (AR_SREV_9280(ah) && ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL)))
  1286. tsf = ath9k_hw_gettsf64(ah);
  1287. saveLedState = REG_READ(ah, AR_CFG_LED) &
  1288. (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
  1289. AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
  1290. ath9k_hw_mark_phy_inactive(ah);
  1291. ah->paprd_table_write_done = false;
  1292. /* Only required on the first reset */
  1293. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1294. REG_WRITE(ah,
  1295. AR9271_RESET_POWER_DOWN_CONTROL,
  1296. AR9271_RADIO_RF_RST);
  1297. udelay(50);
  1298. }
  1299. if (!ath9k_hw_chip_reset(ah, chan)) {
  1300. ath_err(common, "Chip reset failed\n");
  1301. return -EINVAL;
  1302. }
  1303. /* Only required on the first reset */
  1304. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1305. ah->htc_reset_init = false;
  1306. REG_WRITE(ah,
  1307. AR9271_RESET_POWER_DOWN_CONTROL,
  1308. AR9271_GATE_MAC_CTL);
  1309. udelay(50);
  1310. }
  1311. /* Restore TSF */
  1312. if (tsf)
  1313. ath9k_hw_settsf64(ah, tsf);
  1314. if (AR_SREV_9280_20_OR_LATER(ah))
  1315. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
  1316. if (!AR_SREV_9300_20_OR_LATER(ah))
  1317. ar9002_hw_enable_async_fifo(ah);
  1318. r = ath9k_hw_process_ini(ah, chan);
  1319. if (r)
  1320. return r;
  1321. /*
  1322. * Some AR91xx SoC devices frequently fail to accept TSF writes
  1323. * right after the chip reset. When that happens, write a new
  1324. * value after the initvals have been applied, with an offset
  1325. * based on measured time difference
  1326. */
  1327. if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
  1328. tsf += 1500;
  1329. ath9k_hw_settsf64(ah, tsf);
  1330. }
  1331. /* Setup MFP options for CCMP */
  1332. if (AR_SREV_9280_20_OR_LATER(ah)) {
  1333. /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
  1334. * frames when constructing CCMP AAD. */
  1335. REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
  1336. 0xc7ff);
  1337. ah->sw_mgmt_crypto = false;
  1338. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  1339. /* Disable hardware crypto for management frames */
  1340. REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
  1341. AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
  1342. REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
  1343. AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
  1344. ah->sw_mgmt_crypto = true;
  1345. } else
  1346. ah->sw_mgmt_crypto = true;
  1347. if (IS_CHAN_OFDM(chan) || IS_CHAN_HT(chan))
  1348. ath9k_hw_set_delta_slope(ah, chan);
  1349. ath9k_hw_spur_mitigate_freq(ah, chan);
  1350. ah->eep_ops->set_board_values(ah, chan);
  1351. ENABLE_REGWRITE_BUFFER(ah);
  1352. REG_WRITE(ah, AR_STA_ID0, get_unaligned_le32(common->macaddr));
  1353. REG_WRITE(ah, AR_STA_ID1, get_unaligned_le16(common->macaddr + 4)
  1354. | macStaId1
  1355. | AR_STA_ID1_RTS_USE_DEF
  1356. | (ah->config.
  1357. ack_6mb ? AR_STA_ID1_ACKCTS_6MB : 0)
  1358. | ah->sta_id1_defaults);
  1359. ath_hw_setbssidmask(common);
  1360. REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
  1361. ath9k_hw_write_associd(ah);
  1362. REG_WRITE(ah, AR_ISR, ~0);
  1363. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  1364. REGWRITE_BUFFER_FLUSH(ah);
  1365. ath9k_hw_set_operating_mode(ah, ah->opmode);
  1366. r = ath9k_hw_rf_set_freq(ah, chan);
  1367. if (r)
  1368. return r;
  1369. ath9k_hw_set_clockrate(ah);
  1370. ENABLE_REGWRITE_BUFFER(ah);
  1371. for (i = 0; i < AR_NUM_DCU; i++)
  1372. REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
  1373. REGWRITE_BUFFER_FLUSH(ah);
  1374. ah->intr_txqs = 0;
  1375. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1376. ath9k_hw_resettxqueue(ah, i);
  1377. ath9k_hw_init_interrupt_masks(ah, ah->opmode);
  1378. ath9k_hw_ani_cache_ini_regs(ah);
  1379. ath9k_hw_init_qos(ah);
  1380. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1381. ath9k_hw_cfg_gpio_input(ah, ah->rfkill_gpio);
  1382. ath9k_hw_init_global_settings(ah);
  1383. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
  1384. REG_SET_BIT(ah, AR_MAC_PCU_LOGIC_ANALYZER,
  1385. AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
  1386. REG_RMW_FIELD(ah, AR_AHB_MODE, AR_AHB_CUSTOM_BURST_EN,
  1387. AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
  1388. REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
  1389. AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
  1390. }
  1391. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
  1392. ath9k_hw_set_dma(ah);
  1393. REG_WRITE(ah, AR_OBS, 8);
  1394. if (ah->config.rx_intr_mitigation) {
  1395. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, 500);
  1396. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, 2000);
  1397. }
  1398. if (ah->config.tx_intr_mitigation) {
  1399. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
  1400. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
  1401. }
  1402. ath9k_hw_init_bb(ah, chan);
  1403. if (caldata) {
  1404. caldata->done_txiqcal_once = false;
  1405. caldata->done_txclcal_once = false;
  1406. caldata->rtt_hist.num_readings = 0;
  1407. }
  1408. if (!ath9k_hw_init_cal(ah, chan))
  1409. return -EIO;
  1410. ath9k_hw_loadnf(ah, chan);
  1411. ath9k_hw_start_nfcal(ah, true);
  1412. ENABLE_REGWRITE_BUFFER(ah);
  1413. ath9k_hw_restore_chainmask(ah);
  1414. REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
  1415. REGWRITE_BUFFER_FLUSH(ah);
  1416. /*
  1417. * For big endian systems turn on swapping for descriptors
  1418. */
  1419. if (AR_SREV_9100(ah)) {
  1420. u32 mask;
  1421. mask = REG_READ(ah, AR_CFG);
  1422. if (mask & (AR_CFG_SWRB | AR_CFG_SWTB | AR_CFG_SWRG)) {
  1423. ath_dbg(common, ATH_DBG_RESET,
  1424. "CFG Byte Swap Set 0x%x\n", mask);
  1425. } else {
  1426. mask =
  1427. INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
  1428. REG_WRITE(ah, AR_CFG, mask);
  1429. ath_dbg(common, ATH_DBG_RESET,
  1430. "Setting CFG 0x%x\n", REG_READ(ah, AR_CFG));
  1431. }
  1432. } else {
  1433. if (common->bus_ops->ath_bus_type == ATH_USB) {
  1434. /* Configure AR9271 target WLAN */
  1435. if (AR_SREV_9271(ah))
  1436. REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
  1437. else
  1438. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1439. }
  1440. #ifdef __BIG_ENDIAN
  1441. else if (AR_SREV_9330(ah) || AR_SREV_9340(ah))
  1442. REG_RMW(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB, 0);
  1443. else
  1444. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1445. #endif
  1446. }
  1447. if (ah->btcoex_hw.enabled)
  1448. ath9k_hw_btcoex_enable(ah);
  1449. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1450. ar9003_hw_bb_watchdog_config(ah);
  1451. ar9003_hw_disable_phy_restart(ah);
  1452. }
  1453. ath9k_hw_apply_gpio_override(ah);
  1454. return 0;
  1455. }
  1456. EXPORT_SYMBOL(ath9k_hw_reset);
  1457. /******************************/
  1458. /* Power Management (Chipset) */
  1459. /******************************/
  1460. /*
  1461. * Notify Power Mgt is disabled in self-generated frames.
  1462. * If requested, force chip to sleep.
  1463. */
  1464. static void ath9k_set_power_sleep(struct ath_hw *ah, int setChip)
  1465. {
  1466. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1467. if (setChip) {
  1468. if (AR_SREV_9462(ah)) {
  1469. REG_WRITE(ah, AR_TIMER_MODE,
  1470. REG_READ(ah, AR_TIMER_MODE) & 0xFFFFFF00);
  1471. REG_WRITE(ah, AR_NDP2_TIMER_MODE, REG_READ(ah,
  1472. AR_NDP2_TIMER_MODE) & 0xFFFFFF00);
  1473. REG_WRITE(ah, AR_SLP32_INC,
  1474. REG_READ(ah, AR_SLP32_INC) & 0xFFF00000);
  1475. /* xxx Required for WLAN only case ? */
  1476. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
  1477. udelay(100);
  1478. }
  1479. /*
  1480. * Clear the RTC force wake bit to allow the
  1481. * mac to go to sleep.
  1482. */
  1483. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
  1484. if (AR_SREV_9462(ah))
  1485. udelay(100);
  1486. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1487. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  1488. /* Shutdown chip. Active low */
  1489. if (!AR_SREV_5416(ah) &&
  1490. !AR_SREV_9271(ah) && !AR_SREV_9462_10(ah)) {
  1491. REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
  1492. udelay(2);
  1493. }
  1494. }
  1495. /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
  1496. REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
  1497. }
  1498. /*
  1499. * Notify Power Management is enabled in self-generating
  1500. * frames. If request, set power mode of chip to
  1501. * auto/normal. Duration in units of 128us (1/8 TU).
  1502. */
  1503. static void ath9k_set_power_network_sleep(struct ath_hw *ah, int setChip)
  1504. {
  1505. u32 val;
  1506. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1507. if (setChip) {
  1508. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1509. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1510. /* Set WakeOnInterrupt bit; clear ForceWake bit */
  1511. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1512. AR_RTC_FORCE_WAKE_ON_INT);
  1513. } else {
  1514. /* When chip goes into network sleep, it could be waken
  1515. * up by MCI_INT interrupt caused by BT's HW messages
  1516. * (LNA_xxx, CONT_xxx) which chould be in a very fast
  1517. * rate (~100us). This will cause chip to leave and
  1518. * re-enter network sleep mode frequently, which in
  1519. * consequence will have WLAN MCI HW to generate lots of
  1520. * SYS_WAKING and SYS_SLEEPING messages which will make
  1521. * BT CPU to busy to process.
  1522. */
  1523. if (AR_SREV_9462(ah)) {
  1524. val = REG_READ(ah, AR_MCI_INTERRUPT_RX_MSG_EN) &
  1525. ~AR_MCI_INTERRUPT_RX_HW_MSG_MASK;
  1526. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, val);
  1527. }
  1528. /*
  1529. * Clear the RTC force wake bit to allow the
  1530. * mac to go to sleep.
  1531. */
  1532. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE,
  1533. AR_RTC_FORCE_WAKE_EN);
  1534. if (AR_SREV_9462(ah))
  1535. udelay(30);
  1536. }
  1537. }
  1538. /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
  1539. if (AR_SREV_9300_20_OR_LATER(ah))
  1540. REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
  1541. }
  1542. static bool ath9k_hw_set_power_awake(struct ath_hw *ah, int setChip)
  1543. {
  1544. u32 val;
  1545. int i;
  1546. /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
  1547. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1548. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1549. udelay(10);
  1550. }
  1551. if (setChip) {
  1552. if ((REG_READ(ah, AR_RTC_STATUS) &
  1553. AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
  1554. if (ath9k_hw_set_reset_reg(ah,
  1555. ATH9K_RESET_POWER_ON) != true) {
  1556. return false;
  1557. }
  1558. if (!AR_SREV_9300_20_OR_LATER(ah))
  1559. ath9k_hw_init_pll(ah, NULL);
  1560. }
  1561. if (AR_SREV_9100(ah))
  1562. REG_SET_BIT(ah, AR_RTC_RESET,
  1563. AR_RTC_RESET_EN);
  1564. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1565. AR_RTC_FORCE_WAKE_EN);
  1566. udelay(50);
  1567. for (i = POWER_UP_TIME / 50; i > 0; i--) {
  1568. val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
  1569. if (val == AR_RTC_STATUS_ON)
  1570. break;
  1571. udelay(50);
  1572. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1573. AR_RTC_FORCE_WAKE_EN);
  1574. }
  1575. if (i == 0) {
  1576. ath_err(ath9k_hw_common(ah),
  1577. "Failed to wakeup in %uus\n",
  1578. POWER_UP_TIME / 20);
  1579. return false;
  1580. }
  1581. }
  1582. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1583. return true;
  1584. }
  1585. bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
  1586. {
  1587. struct ath_common *common = ath9k_hw_common(ah);
  1588. int status = true, setChip = true;
  1589. static const char *modes[] = {
  1590. "AWAKE",
  1591. "FULL-SLEEP",
  1592. "NETWORK SLEEP",
  1593. "UNDEFINED"
  1594. };
  1595. if (ah->power_mode == mode)
  1596. return status;
  1597. ath_dbg(common, ATH_DBG_RESET, "%s -> %s\n",
  1598. modes[ah->power_mode], modes[mode]);
  1599. switch (mode) {
  1600. case ATH9K_PM_AWAKE:
  1601. status = ath9k_hw_set_power_awake(ah, setChip);
  1602. break;
  1603. case ATH9K_PM_FULL_SLEEP:
  1604. ath9k_set_power_sleep(ah, setChip);
  1605. ah->chip_fullsleep = true;
  1606. break;
  1607. case ATH9K_PM_NETWORK_SLEEP:
  1608. ath9k_set_power_network_sleep(ah, setChip);
  1609. break;
  1610. default:
  1611. ath_err(common, "Unknown power mode %u\n", mode);
  1612. return false;
  1613. }
  1614. ah->power_mode = mode;
  1615. /*
  1616. * XXX: If this warning never comes up after a while then
  1617. * simply keep the ATH_DBG_WARN_ON_ONCE() but make
  1618. * ath9k_hw_setpower() return type void.
  1619. */
  1620. if (!(ah->ah_flags & AH_UNPLUGGED))
  1621. ATH_DBG_WARN_ON_ONCE(!status);
  1622. return status;
  1623. }
  1624. EXPORT_SYMBOL(ath9k_hw_setpower);
  1625. /*******************/
  1626. /* Beacon Handling */
  1627. /*******************/
  1628. void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
  1629. {
  1630. int flags = 0;
  1631. ENABLE_REGWRITE_BUFFER(ah);
  1632. switch (ah->opmode) {
  1633. case NL80211_IFTYPE_ADHOC:
  1634. case NL80211_IFTYPE_MESH_POINT:
  1635. REG_SET_BIT(ah, AR_TXCFG,
  1636. AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
  1637. REG_WRITE(ah, AR_NEXT_NDP_TIMER, next_beacon +
  1638. TU_TO_USEC(ah->atim_window ? ah->atim_window : 1));
  1639. flags |= AR_NDP_TIMER_EN;
  1640. case NL80211_IFTYPE_AP:
  1641. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, next_beacon);
  1642. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, next_beacon -
  1643. TU_TO_USEC(ah->config.dma_beacon_response_time));
  1644. REG_WRITE(ah, AR_NEXT_SWBA, next_beacon -
  1645. TU_TO_USEC(ah->config.sw_beacon_response_time));
  1646. flags |=
  1647. AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
  1648. break;
  1649. default:
  1650. ath_dbg(ath9k_hw_common(ah), ATH_DBG_BEACON,
  1651. "%s: unsupported opmode: %d\n",
  1652. __func__, ah->opmode);
  1653. return;
  1654. break;
  1655. }
  1656. REG_WRITE(ah, AR_BEACON_PERIOD, beacon_period);
  1657. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, beacon_period);
  1658. REG_WRITE(ah, AR_SWBA_PERIOD, beacon_period);
  1659. REG_WRITE(ah, AR_NDP_PERIOD, beacon_period);
  1660. REGWRITE_BUFFER_FLUSH(ah);
  1661. REG_SET_BIT(ah, AR_TIMER_MODE, flags);
  1662. }
  1663. EXPORT_SYMBOL(ath9k_hw_beaconinit);
  1664. void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
  1665. const struct ath9k_beacon_state *bs)
  1666. {
  1667. u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
  1668. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1669. struct ath_common *common = ath9k_hw_common(ah);
  1670. ENABLE_REGWRITE_BUFFER(ah);
  1671. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, TU_TO_USEC(bs->bs_nexttbtt));
  1672. REG_WRITE(ah, AR_BEACON_PERIOD,
  1673. TU_TO_USEC(bs->bs_intval));
  1674. REG_WRITE(ah, AR_DMA_BEACON_PERIOD,
  1675. TU_TO_USEC(bs->bs_intval));
  1676. REGWRITE_BUFFER_FLUSH(ah);
  1677. REG_RMW_FIELD(ah, AR_RSSI_THR,
  1678. AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
  1679. beaconintval = bs->bs_intval;
  1680. if (bs->bs_sleepduration > beaconintval)
  1681. beaconintval = bs->bs_sleepduration;
  1682. dtimperiod = bs->bs_dtimperiod;
  1683. if (bs->bs_sleepduration > dtimperiod)
  1684. dtimperiod = bs->bs_sleepduration;
  1685. if (beaconintval == dtimperiod)
  1686. nextTbtt = bs->bs_nextdtim;
  1687. else
  1688. nextTbtt = bs->bs_nexttbtt;
  1689. ath_dbg(common, ATH_DBG_BEACON, "next DTIM %d\n", bs->bs_nextdtim);
  1690. ath_dbg(common, ATH_DBG_BEACON, "next beacon %d\n", nextTbtt);
  1691. ath_dbg(common, ATH_DBG_BEACON, "beacon period %d\n", beaconintval);
  1692. ath_dbg(common, ATH_DBG_BEACON, "DTIM period %d\n", dtimperiod);
  1693. ENABLE_REGWRITE_BUFFER(ah);
  1694. REG_WRITE(ah, AR_NEXT_DTIM,
  1695. TU_TO_USEC(bs->bs_nextdtim - SLEEP_SLOP));
  1696. REG_WRITE(ah, AR_NEXT_TIM, TU_TO_USEC(nextTbtt - SLEEP_SLOP));
  1697. REG_WRITE(ah, AR_SLEEP1,
  1698. SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
  1699. | AR_SLEEP1_ASSUME_DTIM);
  1700. if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
  1701. beacontimeout = (BEACON_TIMEOUT_VAL << 3);
  1702. else
  1703. beacontimeout = MIN_BEACON_TIMEOUT_VAL;
  1704. REG_WRITE(ah, AR_SLEEP2,
  1705. SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
  1706. REG_WRITE(ah, AR_TIM_PERIOD, TU_TO_USEC(beaconintval));
  1707. REG_WRITE(ah, AR_DTIM_PERIOD, TU_TO_USEC(dtimperiod));
  1708. REGWRITE_BUFFER_FLUSH(ah);
  1709. REG_SET_BIT(ah, AR_TIMER_MODE,
  1710. AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
  1711. AR_DTIM_TIMER_EN);
  1712. /* TSF Out of Range Threshold */
  1713. REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
  1714. }
  1715. EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
  1716. /*******************/
  1717. /* HW Capabilities */
  1718. /*******************/
  1719. static u8 fixup_chainmask(u8 chip_chainmask, u8 eeprom_chainmask)
  1720. {
  1721. eeprom_chainmask &= chip_chainmask;
  1722. if (eeprom_chainmask)
  1723. return eeprom_chainmask;
  1724. else
  1725. return chip_chainmask;
  1726. }
  1727. int ath9k_hw_fill_cap_info(struct ath_hw *ah)
  1728. {
  1729. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1730. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  1731. struct ath_common *common = ath9k_hw_common(ah);
  1732. struct ath_btcoex_hw *btcoex_hw = &ah->btcoex_hw;
  1733. unsigned int chip_chainmask;
  1734. u16 eeval;
  1735. u8 ant_div_ctl1, tx_chainmask, rx_chainmask;
  1736. eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
  1737. regulatory->current_rd = eeval;
  1738. if (ah->opmode != NL80211_IFTYPE_AP &&
  1739. ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
  1740. if (regulatory->current_rd == 0x64 ||
  1741. regulatory->current_rd == 0x65)
  1742. regulatory->current_rd += 5;
  1743. else if (regulatory->current_rd == 0x41)
  1744. regulatory->current_rd = 0x43;
  1745. ath_dbg(common, ATH_DBG_REGULATORY,
  1746. "regdomain mapped to 0x%x\n", regulatory->current_rd);
  1747. }
  1748. eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
  1749. if ((eeval & (AR5416_OPFLAGS_11G | AR5416_OPFLAGS_11A)) == 0) {
  1750. ath_err(common,
  1751. "no band has been marked as supported in EEPROM\n");
  1752. return -EINVAL;
  1753. }
  1754. if (eeval & AR5416_OPFLAGS_11A)
  1755. pCap->hw_caps |= ATH9K_HW_CAP_5GHZ;
  1756. if (eeval & AR5416_OPFLAGS_11G)
  1757. pCap->hw_caps |= ATH9K_HW_CAP_2GHZ;
  1758. if (AR_SREV_9485(ah) || AR_SREV_9285(ah) || AR_SREV_9330(ah))
  1759. chip_chainmask = 1;
  1760. else if (!AR_SREV_9280_20_OR_LATER(ah))
  1761. chip_chainmask = 7;
  1762. else if (!AR_SREV_9300_20_OR_LATER(ah) || AR_SREV_9340(ah))
  1763. chip_chainmask = 3;
  1764. else
  1765. chip_chainmask = 7;
  1766. pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
  1767. /*
  1768. * For AR9271 we will temporarilly uses the rx chainmax as read from
  1769. * the EEPROM.
  1770. */
  1771. if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
  1772. !(eeval & AR5416_OPFLAGS_11A) &&
  1773. !(AR_SREV_9271(ah)))
  1774. /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
  1775. pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
  1776. else if (AR_SREV_9100(ah))
  1777. pCap->rx_chainmask = 0x7;
  1778. else
  1779. /* Use rx_chainmask from EEPROM. */
  1780. pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
  1781. pCap->tx_chainmask = fixup_chainmask(chip_chainmask, pCap->tx_chainmask);
  1782. pCap->rx_chainmask = fixup_chainmask(chip_chainmask, pCap->rx_chainmask);
  1783. ah->txchainmask = pCap->tx_chainmask;
  1784. ah->rxchainmask = pCap->rx_chainmask;
  1785. ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
  1786. /* enable key search for every frame in an aggregate */
  1787. if (AR_SREV_9300_20_OR_LATER(ah))
  1788. ah->misc_mode |= AR_PCU_ALWAYS_PERFORM_KEYSEARCH;
  1789. common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
  1790. if (ah->hw_version.devid != AR2427_DEVID_PCIE)
  1791. pCap->hw_caps |= ATH9K_HW_CAP_HT;
  1792. else
  1793. pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
  1794. if (AR_SREV_9271(ah))
  1795. pCap->num_gpio_pins = AR9271_NUM_GPIO;
  1796. else if (AR_DEVID_7010(ah))
  1797. pCap->num_gpio_pins = AR7010_NUM_GPIO;
  1798. else if (AR_SREV_9300_20_OR_LATER(ah))
  1799. pCap->num_gpio_pins = AR9300_NUM_GPIO;
  1800. else if (AR_SREV_9287_11_OR_LATER(ah))
  1801. pCap->num_gpio_pins = AR9287_NUM_GPIO;
  1802. else if (AR_SREV_9285_12_OR_LATER(ah))
  1803. pCap->num_gpio_pins = AR9285_NUM_GPIO;
  1804. else if (AR_SREV_9280_20_OR_LATER(ah))
  1805. pCap->num_gpio_pins = AR928X_NUM_GPIO;
  1806. else
  1807. pCap->num_gpio_pins = AR_NUM_GPIO;
  1808. if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah)) {
  1809. pCap->hw_caps |= ATH9K_HW_CAP_CST;
  1810. pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
  1811. } else {
  1812. pCap->rts_aggr_limit = (8 * 1024);
  1813. }
  1814. #if defined(CONFIG_RFKILL) || defined(CONFIG_RFKILL_MODULE)
  1815. ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
  1816. if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
  1817. ah->rfkill_gpio =
  1818. MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
  1819. ah->rfkill_polarity =
  1820. MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
  1821. pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
  1822. }
  1823. #endif
  1824. if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
  1825. pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
  1826. else
  1827. pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
  1828. if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
  1829. pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
  1830. else
  1831. pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
  1832. if (common->btcoex_enabled) {
  1833. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1834. btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
  1835. btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO_9300;
  1836. btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO_9300;
  1837. btcoex_hw->btpriority_gpio = ATH_BTPRIORITY_GPIO_9300;
  1838. } else if (AR_SREV_9280_20_OR_LATER(ah)) {
  1839. btcoex_hw->btactive_gpio = ATH_BTACTIVE_GPIO_9280;
  1840. btcoex_hw->wlanactive_gpio = ATH_WLANACTIVE_GPIO_9280;
  1841. if (AR_SREV_9285(ah)) {
  1842. btcoex_hw->scheme = ATH_BTCOEX_CFG_3WIRE;
  1843. btcoex_hw->btpriority_gpio =
  1844. ATH_BTPRIORITY_GPIO_9285;
  1845. } else {
  1846. btcoex_hw->scheme = ATH_BTCOEX_CFG_2WIRE;
  1847. }
  1848. }
  1849. } else {
  1850. btcoex_hw->scheme = ATH_BTCOEX_CFG_NONE;
  1851. }
  1852. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1853. pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_FASTCLOCK;
  1854. if (!AR_SREV_9330(ah) && !AR_SREV_9485(ah))
  1855. pCap->hw_caps |= ATH9K_HW_CAP_LDPC;
  1856. pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
  1857. pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
  1858. pCap->rx_status_len = sizeof(struct ar9003_rxs);
  1859. pCap->tx_desc_len = sizeof(struct ar9003_txc);
  1860. pCap->txs_len = sizeof(struct ar9003_txs);
  1861. if (!ah->config.paprd_disable &&
  1862. ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
  1863. pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
  1864. } else {
  1865. pCap->tx_desc_len = sizeof(struct ath_desc);
  1866. if (AR_SREV_9280_20(ah))
  1867. pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
  1868. }
  1869. if (AR_SREV_9300_20_OR_LATER(ah))
  1870. pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
  1871. if (AR_SREV_9300_20_OR_LATER(ah))
  1872. ah->ent_mode = REG_READ(ah, AR_ENT_OTP);
  1873. if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
  1874. pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
  1875. if (AR_SREV_9285(ah))
  1876. if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
  1877. ant_div_ctl1 =
  1878. ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
  1879. if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1))
  1880. pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
  1881. }
  1882. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1883. if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
  1884. pCap->hw_caps |= ATH9K_HW_CAP_APM;
  1885. }
  1886. if (AR_SREV_9330(ah) || AR_SREV_9485(ah)) {
  1887. ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
  1888. /*
  1889. * enable the diversity-combining algorithm only when
  1890. * both enable_lna_div and enable_fast_div are set
  1891. * Table for Diversity
  1892. * ant_div_alt_lnaconf bit 0-1
  1893. * ant_div_main_lnaconf bit 2-3
  1894. * ant_div_alt_gaintb bit 4
  1895. * ant_div_main_gaintb bit 5
  1896. * enable_ant_div_lnadiv bit 6
  1897. * enable_ant_fast_div bit 7
  1898. */
  1899. if ((ant_div_ctl1 >> 0x6) == 0x3)
  1900. pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
  1901. }
  1902. if (AR_SREV_9485_10(ah)) {
  1903. pCap->pcie_lcr_extsync_en = true;
  1904. pCap->pcie_lcr_offset = 0x80;
  1905. }
  1906. tx_chainmask = pCap->tx_chainmask;
  1907. rx_chainmask = pCap->rx_chainmask;
  1908. while (tx_chainmask || rx_chainmask) {
  1909. if (tx_chainmask & BIT(0))
  1910. pCap->max_txchains++;
  1911. if (rx_chainmask & BIT(0))
  1912. pCap->max_rxchains++;
  1913. tx_chainmask >>= 1;
  1914. rx_chainmask >>= 1;
  1915. }
  1916. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1917. ah->enabled_cals |= TX_IQ_CAL;
  1918. if (!AR_SREV_9330(ah))
  1919. ah->enabled_cals |= TX_IQ_ON_AGC_CAL;
  1920. }
  1921. if (AR_SREV_9462(ah))
  1922. pCap->hw_caps |= ATH9K_HW_CAP_RTT;
  1923. return 0;
  1924. }
  1925. /****************************/
  1926. /* GPIO / RFKILL / Antennae */
  1927. /****************************/
  1928. static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah,
  1929. u32 gpio, u32 type)
  1930. {
  1931. int addr;
  1932. u32 gpio_shift, tmp;
  1933. if (gpio > 11)
  1934. addr = AR_GPIO_OUTPUT_MUX3;
  1935. else if (gpio > 5)
  1936. addr = AR_GPIO_OUTPUT_MUX2;
  1937. else
  1938. addr = AR_GPIO_OUTPUT_MUX1;
  1939. gpio_shift = (gpio % 6) * 5;
  1940. if (AR_SREV_9280_20_OR_LATER(ah)
  1941. || (addr != AR_GPIO_OUTPUT_MUX1)) {
  1942. REG_RMW(ah, addr, (type << gpio_shift),
  1943. (0x1f << gpio_shift));
  1944. } else {
  1945. tmp = REG_READ(ah, addr);
  1946. tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
  1947. tmp &= ~(0x1f << gpio_shift);
  1948. tmp |= (type << gpio_shift);
  1949. REG_WRITE(ah, addr, tmp);
  1950. }
  1951. }
  1952. void ath9k_hw_cfg_gpio_input(struct ath_hw *ah, u32 gpio)
  1953. {
  1954. u32 gpio_shift;
  1955. BUG_ON(gpio >= ah->caps.num_gpio_pins);
  1956. if (AR_DEVID_7010(ah)) {
  1957. gpio_shift = gpio;
  1958. REG_RMW(ah, AR7010_GPIO_OE,
  1959. (AR7010_GPIO_OE_AS_INPUT << gpio_shift),
  1960. (AR7010_GPIO_OE_MASK << gpio_shift));
  1961. return;
  1962. }
  1963. gpio_shift = gpio << 1;
  1964. REG_RMW(ah,
  1965. AR_GPIO_OE_OUT,
  1966. (AR_GPIO_OE_OUT_DRV_NO << gpio_shift),
  1967. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  1968. }
  1969. EXPORT_SYMBOL(ath9k_hw_cfg_gpio_input);
  1970. u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
  1971. {
  1972. #define MS_REG_READ(x, y) \
  1973. (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & (AR_GPIO_BIT(y)))
  1974. if (gpio >= ah->caps.num_gpio_pins)
  1975. return 0xffffffff;
  1976. if (AR_DEVID_7010(ah)) {
  1977. u32 val;
  1978. val = REG_READ(ah, AR7010_GPIO_IN);
  1979. return (MS(val, AR7010_GPIO_IN_VAL) & AR_GPIO_BIT(gpio)) == 0;
  1980. } else if (AR_SREV_9300_20_OR_LATER(ah))
  1981. return (MS(REG_READ(ah, AR_GPIO_IN), AR9300_GPIO_IN_VAL) &
  1982. AR_GPIO_BIT(gpio)) != 0;
  1983. else if (AR_SREV_9271(ah))
  1984. return MS_REG_READ(AR9271, gpio) != 0;
  1985. else if (AR_SREV_9287_11_OR_LATER(ah))
  1986. return MS_REG_READ(AR9287, gpio) != 0;
  1987. else if (AR_SREV_9285_12_OR_LATER(ah))
  1988. return MS_REG_READ(AR9285, gpio) != 0;
  1989. else if (AR_SREV_9280_20_OR_LATER(ah))
  1990. return MS_REG_READ(AR928X, gpio) != 0;
  1991. else
  1992. return MS_REG_READ(AR, gpio) != 0;
  1993. }
  1994. EXPORT_SYMBOL(ath9k_hw_gpio_get);
  1995. void ath9k_hw_cfg_output(struct ath_hw *ah, u32 gpio,
  1996. u32 ah_signal_type)
  1997. {
  1998. u32 gpio_shift;
  1999. if (AR_DEVID_7010(ah)) {
  2000. gpio_shift = gpio;
  2001. REG_RMW(ah, AR7010_GPIO_OE,
  2002. (AR7010_GPIO_OE_AS_OUTPUT << gpio_shift),
  2003. (AR7010_GPIO_OE_MASK << gpio_shift));
  2004. return;
  2005. }
  2006. ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
  2007. gpio_shift = 2 * gpio;
  2008. REG_RMW(ah,
  2009. AR_GPIO_OE_OUT,
  2010. (AR_GPIO_OE_OUT_DRV_ALL << gpio_shift),
  2011. (AR_GPIO_OE_OUT_DRV << gpio_shift));
  2012. }
  2013. EXPORT_SYMBOL(ath9k_hw_cfg_output);
  2014. void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
  2015. {
  2016. if (AR_DEVID_7010(ah)) {
  2017. val = val ? 0 : 1;
  2018. REG_RMW(ah, AR7010_GPIO_OUT, ((val&1) << gpio),
  2019. AR_GPIO_BIT(gpio));
  2020. return;
  2021. }
  2022. if (AR_SREV_9271(ah))
  2023. val = ~val;
  2024. REG_RMW(ah, AR_GPIO_IN_OUT, ((val & 1) << gpio),
  2025. AR_GPIO_BIT(gpio));
  2026. }
  2027. EXPORT_SYMBOL(ath9k_hw_set_gpio);
  2028. u32 ath9k_hw_getdefantenna(struct ath_hw *ah)
  2029. {
  2030. return REG_READ(ah, AR_DEF_ANTENNA) & 0x7;
  2031. }
  2032. EXPORT_SYMBOL(ath9k_hw_getdefantenna);
  2033. void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
  2034. {
  2035. REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
  2036. }
  2037. EXPORT_SYMBOL(ath9k_hw_setantenna);
  2038. /*********************/
  2039. /* General Operation */
  2040. /*********************/
  2041. u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
  2042. {
  2043. u32 bits = REG_READ(ah, AR_RX_FILTER);
  2044. u32 phybits = REG_READ(ah, AR_PHY_ERR);
  2045. if (phybits & AR_PHY_ERR_RADAR)
  2046. bits |= ATH9K_RX_FILTER_PHYRADAR;
  2047. if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
  2048. bits |= ATH9K_RX_FILTER_PHYERR;
  2049. return bits;
  2050. }
  2051. EXPORT_SYMBOL(ath9k_hw_getrxfilter);
  2052. void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
  2053. {
  2054. u32 phybits;
  2055. ENABLE_REGWRITE_BUFFER(ah);
  2056. if (AR_SREV_9462(ah))
  2057. bits |= ATH9K_RX_FILTER_CONTROL_WRAPPER;
  2058. REG_WRITE(ah, AR_RX_FILTER, bits);
  2059. phybits = 0;
  2060. if (bits & ATH9K_RX_FILTER_PHYRADAR)
  2061. phybits |= AR_PHY_ERR_RADAR;
  2062. if (bits & ATH9K_RX_FILTER_PHYERR)
  2063. phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
  2064. REG_WRITE(ah, AR_PHY_ERR, phybits);
  2065. if (phybits)
  2066. REG_SET_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
  2067. else
  2068. REG_CLR_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
  2069. REGWRITE_BUFFER_FLUSH(ah);
  2070. }
  2071. EXPORT_SYMBOL(ath9k_hw_setrxfilter);
  2072. bool ath9k_hw_phy_disable(struct ath_hw *ah)
  2073. {
  2074. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  2075. return false;
  2076. ath9k_hw_init_pll(ah, NULL);
  2077. return true;
  2078. }
  2079. EXPORT_SYMBOL(ath9k_hw_phy_disable);
  2080. bool ath9k_hw_disable(struct ath_hw *ah)
  2081. {
  2082. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  2083. return false;
  2084. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
  2085. return false;
  2086. ath9k_hw_init_pll(ah, NULL);
  2087. return true;
  2088. }
  2089. EXPORT_SYMBOL(ath9k_hw_disable);
  2090. static int get_antenna_gain(struct ath_hw *ah, struct ath9k_channel *chan)
  2091. {
  2092. enum eeprom_param gain_param;
  2093. if (IS_CHAN_2GHZ(chan))
  2094. gain_param = EEP_ANTENNA_GAIN_2G;
  2095. else
  2096. gain_param = EEP_ANTENNA_GAIN_5G;
  2097. return ah->eep_ops->get_eeprom(ah, gain_param);
  2098. }
  2099. void ath9k_hw_apply_txpower(struct ath_hw *ah, struct ath9k_channel *chan)
  2100. {
  2101. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  2102. struct ieee80211_channel *channel;
  2103. int chan_pwr, new_pwr, max_gain;
  2104. int ant_gain, ant_reduction = 0;
  2105. if (!chan)
  2106. return;
  2107. channel = chan->chan;
  2108. chan_pwr = min_t(int, channel->max_power * 2, MAX_RATE_POWER);
  2109. new_pwr = min_t(int, chan_pwr, reg->power_limit);
  2110. max_gain = chan_pwr - new_pwr + channel->max_antenna_gain * 2;
  2111. ant_gain = get_antenna_gain(ah, chan);
  2112. if (ant_gain > max_gain)
  2113. ant_reduction = ant_gain - max_gain;
  2114. ah->eep_ops->set_txpower(ah, chan,
  2115. ath9k_regd_get_ctl(reg, chan),
  2116. ant_reduction, new_pwr, false);
  2117. }
  2118. void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit, bool test)
  2119. {
  2120. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  2121. struct ath9k_channel *chan = ah->curchan;
  2122. struct ieee80211_channel *channel = chan->chan;
  2123. reg->power_limit = min_t(int, limit, MAX_RATE_POWER);
  2124. if (test)
  2125. channel->max_power = MAX_RATE_POWER / 2;
  2126. ath9k_hw_apply_txpower(ah, chan);
  2127. if (test)
  2128. channel->max_power = DIV_ROUND_UP(reg->max_power_level, 2);
  2129. }
  2130. EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
  2131. void ath9k_hw_setopmode(struct ath_hw *ah)
  2132. {
  2133. ath9k_hw_set_operating_mode(ah, ah->opmode);
  2134. }
  2135. EXPORT_SYMBOL(ath9k_hw_setopmode);
  2136. void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
  2137. {
  2138. REG_WRITE(ah, AR_MCAST_FIL0, filter0);
  2139. REG_WRITE(ah, AR_MCAST_FIL1, filter1);
  2140. }
  2141. EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
  2142. void ath9k_hw_write_associd(struct ath_hw *ah)
  2143. {
  2144. struct ath_common *common = ath9k_hw_common(ah);
  2145. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
  2146. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
  2147. ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
  2148. }
  2149. EXPORT_SYMBOL(ath9k_hw_write_associd);
  2150. #define ATH9K_MAX_TSF_READ 10
  2151. u64 ath9k_hw_gettsf64(struct ath_hw *ah)
  2152. {
  2153. u32 tsf_lower, tsf_upper1, tsf_upper2;
  2154. int i;
  2155. tsf_upper1 = REG_READ(ah, AR_TSF_U32);
  2156. for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
  2157. tsf_lower = REG_READ(ah, AR_TSF_L32);
  2158. tsf_upper2 = REG_READ(ah, AR_TSF_U32);
  2159. if (tsf_upper2 == tsf_upper1)
  2160. break;
  2161. tsf_upper1 = tsf_upper2;
  2162. }
  2163. WARN_ON( i == ATH9K_MAX_TSF_READ );
  2164. return (((u64)tsf_upper1 << 32) | tsf_lower);
  2165. }
  2166. EXPORT_SYMBOL(ath9k_hw_gettsf64);
  2167. void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
  2168. {
  2169. REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
  2170. REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
  2171. }
  2172. EXPORT_SYMBOL(ath9k_hw_settsf64);
  2173. void ath9k_hw_reset_tsf(struct ath_hw *ah)
  2174. {
  2175. if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
  2176. AH_TSF_WRITE_TIMEOUT))
  2177. ath_dbg(ath9k_hw_common(ah), ATH_DBG_RESET,
  2178. "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
  2179. REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
  2180. }
  2181. EXPORT_SYMBOL(ath9k_hw_reset_tsf);
  2182. void ath9k_hw_set_tsfadjust(struct ath_hw *ah, u32 setting)
  2183. {
  2184. if (setting)
  2185. ah->misc_mode |= AR_PCU_TX_ADD_TSF;
  2186. else
  2187. ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
  2188. }
  2189. EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
  2190. void ath9k_hw_set11nmac2040(struct ath_hw *ah)
  2191. {
  2192. struct ieee80211_conf *conf = &ath9k_hw_common(ah)->hw->conf;
  2193. u32 macmode;
  2194. if (conf_is_ht40(conf) && !ah->config.cwm_ignore_extcca)
  2195. macmode = AR_2040_JOINED_RX_CLEAR;
  2196. else
  2197. macmode = 0;
  2198. REG_WRITE(ah, AR_2040_MODE, macmode);
  2199. }
  2200. /* HW Generic timers configuration */
  2201. static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
  2202. {
  2203. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2204. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2205. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2206. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2207. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2208. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2209. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2210. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2211. {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
  2212. {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
  2213. AR_NDP2_TIMER_MODE, 0x0002},
  2214. {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
  2215. AR_NDP2_TIMER_MODE, 0x0004},
  2216. {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
  2217. AR_NDP2_TIMER_MODE, 0x0008},
  2218. {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
  2219. AR_NDP2_TIMER_MODE, 0x0010},
  2220. {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
  2221. AR_NDP2_TIMER_MODE, 0x0020},
  2222. {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
  2223. AR_NDP2_TIMER_MODE, 0x0040},
  2224. {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
  2225. AR_NDP2_TIMER_MODE, 0x0080}
  2226. };
  2227. /* HW generic timer primitives */
  2228. /* compute and clear index of rightmost 1 */
  2229. static u32 rightmost_index(struct ath_gen_timer_table *timer_table, u32 *mask)
  2230. {
  2231. u32 b;
  2232. b = *mask;
  2233. b &= (0-b);
  2234. *mask &= ~b;
  2235. b *= debruijn32;
  2236. b >>= 27;
  2237. return timer_table->gen_timer_index[b];
  2238. }
  2239. u32 ath9k_hw_gettsf32(struct ath_hw *ah)
  2240. {
  2241. return REG_READ(ah, AR_TSF_L32);
  2242. }
  2243. EXPORT_SYMBOL(ath9k_hw_gettsf32);
  2244. struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
  2245. void (*trigger)(void *),
  2246. void (*overflow)(void *),
  2247. void *arg,
  2248. u8 timer_index)
  2249. {
  2250. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2251. struct ath_gen_timer *timer;
  2252. timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
  2253. if (timer == NULL) {
  2254. ath_err(ath9k_hw_common(ah),
  2255. "Failed to allocate memory for hw timer[%d]\n",
  2256. timer_index);
  2257. return NULL;
  2258. }
  2259. /* allocate a hardware generic timer slot */
  2260. timer_table->timers[timer_index] = timer;
  2261. timer->index = timer_index;
  2262. timer->trigger = trigger;
  2263. timer->overflow = overflow;
  2264. timer->arg = arg;
  2265. return timer;
  2266. }
  2267. EXPORT_SYMBOL(ath_gen_timer_alloc);
  2268. void ath9k_hw_gen_timer_start(struct ath_hw *ah,
  2269. struct ath_gen_timer *timer,
  2270. u32 trig_timeout,
  2271. u32 timer_period)
  2272. {
  2273. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2274. u32 tsf, timer_next;
  2275. BUG_ON(!timer_period);
  2276. set_bit(timer->index, &timer_table->timer_mask.timer_bits);
  2277. tsf = ath9k_hw_gettsf32(ah);
  2278. timer_next = tsf + trig_timeout;
  2279. ath_dbg(ath9k_hw_common(ah), ATH_DBG_HWTIMER,
  2280. "current tsf %x period %x timer_next %x\n",
  2281. tsf, timer_period, timer_next);
  2282. /*
  2283. * Program generic timer registers
  2284. */
  2285. REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
  2286. timer_next);
  2287. REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
  2288. timer_period);
  2289. REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2290. gen_tmr_configuration[timer->index].mode_mask);
  2291. if (AR_SREV_9462(ah)) {
  2292. /*
  2293. * Starting from AR9462, each generic timer can select which tsf
  2294. * to use. But we still follow the old rule, 0 - 7 use tsf and
  2295. * 8 - 15 use tsf2.
  2296. */
  2297. if ((timer->index < AR_GEN_TIMER_BANK_1_LEN))
  2298. REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
  2299. (1 << timer->index));
  2300. else
  2301. REG_SET_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
  2302. (1 << timer->index));
  2303. }
  2304. /* Enable both trigger and thresh interrupt masks */
  2305. REG_SET_BIT(ah, AR_IMR_S5,
  2306. (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
  2307. SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
  2308. }
  2309. EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
  2310. void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  2311. {
  2312. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2313. if ((timer->index < AR_FIRST_NDP_TIMER) ||
  2314. (timer->index >= ATH_MAX_GEN_TIMER)) {
  2315. return;
  2316. }
  2317. /* Clear generic timer enable bits. */
  2318. REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2319. gen_tmr_configuration[timer->index].mode_mask);
  2320. /* Disable both trigger and thresh interrupt masks */
  2321. REG_CLR_BIT(ah, AR_IMR_S5,
  2322. (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
  2323. SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
  2324. clear_bit(timer->index, &timer_table->timer_mask.timer_bits);
  2325. }
  2326. EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
  2327. void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
  2328. {
  2329. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2330. /* free the hardware generic timer slot */
  2331. timer_table->timers[timer->index] = NULL;
  2332. kfree(timer);
  2333. }
  2334. EXPORT_SYMBOL(ath_gen_timer_free);
  2335. /*
  2336. * Generic Timer Interrupts handling
  2337. */
  2338. void ath_gen_timer_isr(struct ath_hw *ah)
  2339. {
  2340. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2341. struct ath_gen_timer *timer;
  2342. struct ath_common *common = ath9k_hw_common(ah);
  2343. u32 trigger_mask, thresh_mask, index;
  2344. /* get hardware generic timer interrupt status */
  2345. trigger_mask = ah->intr_gen_timer_trigger;
  2346. thresh_mask = ah->intr_gen_timer_thresh;
  2347. trigger_mask &= timer_table->timer_mask.val;
  2348. thresh_mask &= timer_table->timer_mask.val;
  2349. trigger_mask &= ~thresh_mask;
  2350. while (thresh_mask) {
  2351. index = rightmost_index(timer_table, &thresh_mask);
  2352. timer = timer_table->timers[index];
  2353. BUG_ON(!timer);
  2354. ath_dbg(common, ATH_DBG_HWTIMER,
  2355. "TSF overflow for Gen timer %d\n", index);
  2356. timer->overflow(timer->arg);
  2357. }
  2358. while (trigger_mask) {
  2359. index = rightmost_index(timer_table, &trigger_mask);
  2360. timer = timer_table->timers[index];
  2361. BUG_ON(!timer);
  2362. ath_dbg(common, ATH_DBG_HWTIMER,
  2363. "Gen timer[%d] trigger\n", index);
  2364. timer->trigger(timer->arg);
  2365. }
  2366. }
  2367. EXPORT_SYMBOL(ath_gen_timer_isr);
  2368. /********/
  2369. /* HTC */
  2370. /********/
  2371. void ath9k_hw_htc_resetinit(struct ath_hw *ah)
  2372. {
  2373. ah->htc_reset_init = true;
  2374. }
  2375. EXPORT_SYMBOL(ath9k_hw_htc_resetinit);
  2376. static struct {
  2377. u32 version;
  2378. const char * name;
  2379. } ath_mac_bb_names[] = {
  2380. /* Devices with external radios */
  2381. { AR_SREV_VERSION_5416_PCI, "5416" },
  2382. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2383. { AR_SREV_VERSION_9100, "9100" },
  2384. { AR_SREV_VERSION_9160, "9160" },
  2385. /* Single-chip solutions */
  2386. { AR_SREV_VERSION_9280, "9280" },
  2387. { AR_SREV_VERSION_9285, "9285" },
  2388. { AR_SREV_VERSION_9287, "9287" },
  2389. { AR_SREV_VERSION_9271, "9271" },
  2390. { AR_SREV_VERSION_9300, "9300" },
  2391. { AR_SREV_VERSION_9330, "9330" },
  2392. { AR_SREV_VERSION_9340, "9340" },
  2393. { AR_SREV_VERSION_9485, "9485" },
  2394. { AR_SREV_VERSION_9462, "9462" },
  2395. };
  2396. /* For devices with external radios */
  2397. static struct {
  2398. u16 version;
  2399. const char * name;
  2400. } ath_rf_names[] = {
  2401. { 0, "5133" },
  2402. { AR_RAD5133_SREV_MAJOR, "5133" },
  2403. { AR_RAD5122_SREV_MAJOR, "5122" },
  2404. { AR_RAD2133_SREV_MAJOR, "2133" },
  2405. { AR_RAD2122_SREV_MAJOR, "2122" }
  2406. };
  2407. /*
  2408. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2409. */
  2410. static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
  2411. {
  2412. int i;
  2413. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2414. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2415. return ath_mac_bb_names[i].name;
  2416. }
  2417. }
  2418. return "????";
  2419. }
  2420. /*
  2421. * Return the RF name. "????" is returned if the RF is unknown.
  2422. * Used for devices with external radios.
  2423. */
  2424. static const char *ath9k_hw_rf_name(u16 rf_version)
  2425. {
  2426. int i;
  2427. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2428. if (ath_rf_names[i].version == rf_version) {
  2429. return ath_rf_names[i].name;
  2430. }
  2431. }
  2432. return "????";
  2433. }
  2434. void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
  2435. {
  2436. int used;
  2437. /* chipsets >= AR9280 are single-chip */
  2438. if (AR_SREV_9280_20_OR_LATER(ah)) {
  2439. used = snprintf(hw_name, len,
  2440. "Atheros AR%s Rev:%x",
  2441. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2442. ah->hw_version.macRev);
  2443. }
  2444. else {
  2445. used = snprintf(hw_name, len,
  2446. "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
  2447. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2448. ah->hw_version.macRev,
  2449. ath9k_hw_rf_name((ah->hw_version.analog5GhzRev &
  2450. AR_RADIO_SREV_MAJOR)),
  2451. ah->hw_version.phyRev);
  2452. }
  2453. hw_name[used] = '\0';
  2454. }
  2455. EXPORT_SYMBOL(ath9k_hw_name);