hw.c 75 KB

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