hw.c 84 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339
  1. /*
  2. * Copyright (c) 2008-2011 Atheros Communications Inc.
  3. *
  4. * Permission to use, copy, modify, and/or distribute this software for any
  5. * purpose with or without fee is hereby granted, provided that the above
  6. * copyright notice and this permission notice appear in all copies.
  7. *
  8. * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
  9. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  10. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
  11. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  12. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  13. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
  14. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  15. */
  16. #include <linux/io.h>
  17. #include <linux/slab.h>
  18. #include <linux/module.h>
  19. #include <linux/time.h>
  20. #include <linux/bitops.h>
  21. #include <linux/etherdevice.h>
  22. #include <linux/gpio.h>
  23. #include <asm/unaligned.h>
  24. #include "hw.h"
  25. #include "hw-ops.h"
  26. #include "ar9003_mac.h"
  27. #include "ar9003_mci.h"
  28. #include "ar9003_phy.h"
  29. #include "ath9k.h"
  30. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type);
  31. MODULE_AUTHOR("Atheros Communications");
  32. MODULE_DESCRIPTION("Support for Atheros 802.11n wireless LAN cards.");
  33. MODULE_SUPPORTED_DEVICE("Atheros 802.11n WLAN cards");
  34. MODULE_LICENSE("Dual BSD/GPL");
  35. static void ath9k_hw_set_clockrate(struct ath_hw *ah)
  36. {
  37. struct ath_common *common = ath9k_hw_common(ah);
  38. struct ath9k_channel *chan = ah->curchan;
  39. unsigned int clockrate;
  40. /* AR9287 v1.3+ uses async FIFO and runs the MAC at 117 MHz */
  41. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah))
  42. clockrate = 117;
  43. else if (!chan) /* should really check for CCK instead */
  44. clockrate = ATH9K_CLOCK_RATE_CCK;
  45. else if (IS_CHAN_2GHZ(chan))
  46. clockrate = ATH9K_CLOCK_RATE_2GHZ_OFDM;
  47. else if (ah->caps.hw_caps & ATH9K_HW_CAP_FASTCLOCK)
  48. clockrate = ATH9K_CLOCK_FAST_RATE_5GHZ_OFDM;
  49. else
  50. clockrate = ATH9K_CLOCK_RATE_5GHZ_OFDM;
  51. if (chan) {
  52. if (IS_CHAN_HT40(chan))
  53. clockrate *= 2;
  54. if (IS_CHAN_HALF_RATE(chan))
  55. clockrate /= 2;
  56. if (IS_CHAN_QUARTER_RATE(chan))
  57. clockrate /= 4;
  58. }
  59. common->clockrate = clockrate;
  60. }
  61. static u32 ath9k_hw_mac_to_clks(struct ath_hw *ah, u32 usecs)
  62. {
  63. struct ath_common *common = ath9k_hw_common(ah);
  64. return usecs * common->clockrate;
  65. }
  66. bool ath9k_hw_wait(struct ath_hw *ah, u32 reg, u32 mask, u32 val, u32 timeout)
  67. {
  68. int i;
  69. BUG_ON(timeout < AH_TIME_QUANTUM);
  70. for (i = 0; i < (timeout / AH_TIME_QUANTUM); i++) {
  71. if ((REG_READ(ah, reg) & mask) == val)
  72. return true;
  73. udelay(AH_TIME_QUANTUM);
  74. }
  75. ath_dbg(ath9k_hw_common(ah), ANY,
  76. "timeout (%d us) on reg 0x%x: 0x%08x & 0x%08x != 0x%08x\n",
  77. timeout, reg, REG_READ(ah, reg), mask, val);
  78. return false;
  79. }
  80. EXPORT_SYMBOL(ath9k_hw_wait);
  81. void ath9k_hw_synth_delay(struct ath_hw *ah, struct ath9k_channel *chan,
  82. int hw_delay)
  83. {
  84. hw_delay /= 10;
  85. if (IS_CHAN_HALF_RATE(chan))
  86. hw_delay *= 2;
  87. else if (IS_CHAN_QUARTER_RATE(chan))
  88. hw_delay *= 4;
  89. udelay(hw_delay + BASE_ACTIVATE_DELAY);
  90. }
  91. void ath9k_hw_write_array(struct ath_hw *ah, const struct ar5416IniArray *array,
  92. int column, unsigned int *writecnt)
  93. {
  94. int r;
  95. ENABLE_REGWRITE_BUFFER(ah);
  96. for (r = 0; r < array->ia_rows; r++) {
  97. REG_WRITE(ah, INI_RA(array, r, 0),
  98. INI_RA(array, r, column));
  99. DO_DELAY(*writecnt);
  100. }
  101. REGWRITE_BUFFER_FLUSH(ah);
  102. }
  103. void ath9k_hw_read_array(struct ath_hw *ah, u32 array[][2], int size)
  104. {
  105. u32 *tmp_reg_list, *tmp_data;
  106. int i;
  107. tmp_reg_list = kmalloc(size * sizeof(u32), GFP_KERNEL);
  108. if (!tmp_reg_list) {
  109. dev_err(ah->dev, "%s: tmp_reg_list: alloc filed\n", __func__);
  110. return;
  111. }
  112. tmp_data = kmalloc(size * sizeof(u32), GFP_KERNEL);
  113. if (!tmp_data) {
  114. dev_err(ah->dev, "%s tmp_data: alloc filed\n", __func__);
  115. goto error_tmp_data;
  116. }
  117. for (i = 0; i < size; i++)
  118. tmp_reg_list[i] = array[i][0];
  119. REG_READ_MULTI(ah, tmp_reg_list, tmp_data, size);
  120. for (i = 0; i < size; i++)
  121. array[i][1] = tmp_data[i];
  122. kfree(tmp_data);
  123. error_tmp_data:
  124. kfree(tmp_reg_list);
  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 (IS_CHAN_HT40PLUS(chan)) {
  196. centers->synth_center =
  197. chan->channel + HT40_CHANNEL_CENTER_SHIFT;
  198. extoff = 1;
  199. } else {
  200. centers->synth_center =
  201. chan->channel - HT40_CHANNEL_CENTER_SHIFT;
  202. extoff = -1;
  203. }
  204. centers->ctl_center =
  205. centers->synth_center - (extoff * HT40_CHANNEL_CENTER_SHIFT);
  206. /* 25 MHz spacing is supported by hw but not on upper layers */
  207. centers->ext_center =
  208. centers->synth_center + (extoff * HT40_CHANNEL_CENTER_SHIFT);
  209. }
  210. /******************/
  211. /* Chip Revisions */
  212. /******************/
  213. static void ath9k_hw_read_revisions(struct ath_hw *ah)
  214. {
  215. u32 val;
  216. if (ah->get_mac_revision)
  217. ah->hw_version.macRev = ah->get_mac_revision();
  218. switch (ah->hw_version.devid) {
  219. case AR5416_AR9100_DEVID:
  220. ah->hw_version.macVersion = AR_SREV_VERSION_9100;
  221. break;
  222. case AR9300_DEVID_AR9330:
  223. ah->hw_version.macVersion = AR_SREV_VERSION_9330;
  224. if (!ah->get_mac_revision) {
  225. val = REG_READ(ah, AR_SREV);
  226. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  227. }
  228. return;
  229. case AR9300_DEVID_AR9340:
  230. ah->hw_version.macVersion = AR_SREV_VERSION_9340;
  231. return;
  232. case AR9300_DEVID_QCA955X:
  233. ah->hw_version.macVersion = AR_SREV_VERSION_9550;
  234. return;
  235. case AR9300_DEVID_AR953X:
  236. ah->hw_version.macVersion = AR_SREV_VERSION_9531;
  237. return;
  238. case AR9300_DEVID_QCA956X:
  239. ah->hw_version.macVersion = AR_SREV_VERSION_9561;
  240. return;
  241. }
  242. val = REG_READ(ah, AR_SREV) & AR_SREV_ID;
  243. if (val == 0xFF) {
  244. val = REG_READ(ah, AR_SREV);
  245. ah->hw_version.macVersion =
  246. (val & AR_SREV_VERSION2) >> AR_SREV_TYPE2_S;
  247. ah->hw_version.macRev = MS(val, AR_SREV_REVISION2);
  248. if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
  249. ah->is_pciexpress = true;
  250. else
  251. ah->is_pciexpress = (val &
  252. AR_SREV_TYPE2_HOST_MODE) ? 0 : 1;
  253. } else {
  254. if (!AR_SREV_9100(ah))
  255. ah->hw_version.macVersion = MS(val, AR_SREV_VERSION);
  256. ah->hw_version.macRev = val & AR_SREV_REVISION;
  257. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCIE)
  258. ah->is_pciexpress = true;
  259. }
  260. }
  261. /************************************/
  262. /* HW Attach, Detach, Init Routines */
  263. /************************************/
  264. static void ath9k_hw_disablepcie(struct ath_hw *ah)
  265. {
  266. if (!AR_SREV_5416(ah))
  267. return;
  268. REG_WRITE(ah, AR_PCIE_SERDES, 0x9248fc00);
  269. REG_WRITE(ah, AR_PCIE_SERDES, 0x24924924);
  270. REG_WRITE(ah, AR_PCIE_SERDES, 0x28000029);
  271. REG_WRITE(ah, AR_PCIE_SERDES, 0x57160824);
  272. REG_WRITE(ah, AR_PCIE_SERDES, 0x25980579);
  273. REG_WRITE(ah, AR_PCIE_SERDES, 0x00000000);
  274. REG_WRITE(ah, AR_PCIE_SERDES, 0x1aaabe40);
  275. REG_WRITE(ah, AR_PCIE_SERDES, 0xbe105554);
  276. REG_WRITE(ah, AR_PCIE_SERDES, 0x000e1007);
  277. REG_WRITE(ah, AR_PCIE_SERDES2, 0x00000000);
  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. struct ath_common *common = ath9k_hw_common(ah);
  328. ah->config.dma_beacon_response_time = 1;
  329. ah->config.sw_beacon_response_time = 6;
  330. ah->config.cwm_ignore_extcca = false;
  331. ah->config.analog_shiftreg = 1;
  332. ah->config.rx_intr_mitigation = true;
  333. if (AR_SREV_9300_20_OR_LATER(ah)) {
  334. ah->config.rimt_last = 500;
  335. ah->config.rimt_first = 2000;
  336. } else {
  337. ah->config.rimt_last = 250;
  338. ah->config.rimt_first = 700;
  339. }
  340. if (AR_SREV_9462(ah) || AR_SREV_9565(ah))
  341. ah->config.pll_pwrsave = 7;
  342. /*
  343. * We need this for PCI devices only (Cardbus, PCI, miniPCI)
  344. * _and_ if on non-uniprocessor systems (Multiprocessor/HT).
  345. * This means we use it for all AR5416 devices, and the few
  346. * minor PCI AR9280 devices out there.
  347. *
  348. * Serialization is required because these devices do not handle
  349. * well the case of two concurrent reads/writes due to the latency
  350. * involved. During one read/write another read/write can be issued
  351. * on another CPU while the previous read/write may still be working
  352. * on our hardware, if we hit this case the hardware poops in a loop.
  353. * We prevent this by serializing reads and writes.
  354. *
  355. * This issue is not present on PCI-Express devices or pre-AR5416
  356. * devices (legacy, 802.11abg).
  357. */
  358. if (num_possible_cpus() > 1)
  359. ah->config.serialize_regmode = SER_REG_MODE_AUTO;
  360. if (NR_CPUS > 1 && ah->config.serialize_regmode == SER_REG_MODE_AUTO) {
  361. if (ah->hw_version.macVersion == AR_SREV_VERSION_5416_PCI ||
  362. ((AR_SREV_9160(ah) || AR_SREV_9280(ah) || AR_SREV_9287(ah)) &&
  363. !ah->is_pciexpress)) {
  364. ah->config.serialize_regmode = SER_REG_MODE_ON;
  365. } else {
  366. ah->config.serialize_regmode = SER_REG_MODE_OFF;
  367. }
  368. }
  369. ath_dbg(common, RESET, "serialize_regmode is %d\n",
  370. ah->config.serialize_regmode);
  371. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  372. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD >> 1;
  373. else
  374. ah->config.max_txtrig_level = MAX_TX_FIFO_THRESHOLD;
  375. }
  376. static void ath9k_hw_init_defaults(struct ath_hw *ah)
  377. {
  378. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  379. regulatory->country_code = CTRY_DEFAULT;
  380. regulatory->power_limit = MAX_RATE_POWER;
  381. ah->hw_version.magic = AR5416_MAGIC;
  382. ah->hw_version.subvendorid = 0;
  383. ah->sta_id1_defaults = AR_STA_ID1_CRPT_MIC_ENABLE |
  384. AR_STA_ID1_MCAST_KSRCH;
  385. if (AR_SREV_9100(ah))
  386. ah->sta_id1_defaults |= AR_STA_ID1_AR9100_BA_FIX;
  387. ah->slottime = 9;
  388. ah->globaltxtimeout = (u32) -1;
  389. ah->power_mode = ATH9K_PM_UNDEFINED;
  390. ah->htc_reset_init = true;
  391. ah->tpc_enabled = false;
  392. ah->ani_function = ATH9K_ANI_ALL;
  393. if (!AR_SREV_9300_20_OR_LATER(ah))
  394. ah->ani_function &= ~ATH9K_ANI_MRC_CCK;
  395. if (AR_SREV_9285(ah) || AR_SREV_9271(ah))
  396. ah->tx_trig_level = (AR_FTRIG_256B >> AR_FTRIG_S);
  397. else
  398. ah->tx_trig_level = (AR_FTRIG_512B >> AR_FTRIG_S);
  399. }
  400. static void ath9k_hw_init_macaddr(struct ath_hw *ah)
  401. {
  402. struct ath_common *common = ath9k_hw_common(ah);
  403. int i;
  404. u16 eeval;
  405. static const u32 EEP_MAC[] = { EEP_MAC_LSW, EEP_MAC_MID, EEP_MAC_MSW };
  406. /* MAC address may already be loaded via ath9k_platform_data */
  407. if (is_valid_ether_addr(common->macaddr))
  408. return;
  409. for (i = 0; i < 3; i++) {
  410. eeval = ah->eep_ops->get_eeprom(ah, EEP_MAC[i]);
  411. common->macaddr[2 * i] = eeval >> 8;
  412. common->macaddr[2 * i + 1] = eeval & 0xff;
  413. }
  414. if (is_valid_ether_addr(common->macaddr))
  415. return;
  416. ath_err(common, "eeprom contains invalid mac address: %pM\n",
  417. common->macaddr);
  418. random_ether_addr(common->macaddr);
  419. ath_err(common, "random mac address will be used: %pM\n",
  420. common->macaddr);
  421. return;
  422. }
  423. static int ath9k_hw_post_init(struct ath_hw *ah)
  424. {
  425. struct ath_common *common = ath9k_hw_common(ah);
  426. int ecode;
  427. if (common->bus_ops->ath_bus_type != ATH_USB) {
  428. if (!ath9k_hw_chip_test(ah))
  429. return -ENODEV;
  430. }
  431. if (!AR_SREV_9300_20_OR_LATER(ah)) {
  432. ecode = ar9002_hw_rf_claim(ah);
  433. if (ecode != 0)
  434. return ecode;
  435. }
  436. ecode = ath9k_hw_eeprom_init(ah);
  437. if (ecode != 0)
  438. return ecode;
  439. ath_dbg(ath9k_hw_common(ah), CONFIG, "Eeprom VER: %d, REV: %d\n",
  440. ah->eep_ops->get_eeprom_ver(ah),
  441. ah->eep_ops->get_eeprom_rev(ah));
  442. ath9k_hw_ani_init(ah);
  443. /*
  444. * EEPROM needs to be initialized before we do this.
  445. * This is required for regulatory compliance.
  446. */
  447. if (AR_SREV_9300_20_OR_LATER(ah)) {
  448. u16 regdmn = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
  449. if ((regdmn & 0xF0) == CTL_FCC) {
  450. ah->nf_2g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_FCC_2GHZ;
  451. ah->nf_5g.max = AR_PHY_CCA_MAX_GOOD_VAL_9300_FCC_5GHZ;
  452. }
  453. }
  454. return 0;
  455. }
  456. static int ath9k_hw_attach_ops(struct ath_hw *ah)
  457. {
  458. if (!AR_SREV_9300_20_OR_LATER(ah))
  459. return ar9002_hw_attach_ops(ah);
  460. ar9003_hw_attach_ops(ah);
  461. return 0;
  462. }
  463. /* Called for all hardware families */
  464. static int __ath9k_hw_init(struct ath_hw *ah)
  465. {
  466. struct ath_common *common = ath9k_hw_common(ah);
  467. int r = 0;
  468. ath9k_hw_read_revisions(ah);
  469. switch (ah->hw_version.macVersion) {
  470. case AR_SREV_VERSION_5416_PCI:
  471. case AR_SREV_VERSION_5416_PCIE:
  472. case AR_SREV_VERSION_9160:
  473. case AR_SREV_VERSION_9100:
  474. case AR_SREV_VERSION_9280:
  475. case AR_SREV_VERSION_9285:
  476. case AR_SREV_VERSION_9287:
  477. case AR_SREV_VERSION_9271:
  478. case AR_SREV_VERSION_9300:
  479. case AR_SREV_VERSION_9330:
  480. case AR_SREV_VERSION_9485:
  481. case AR_SREV_VERSION_9340:
  482. case AR_SREV_VERSION_9462:
  483. case AR_SREV_VERSION_9550:
  484. case AR_SREV_VERSION_9565:
  485. case AR_SREV_VERSION_9531:
  486. case AR_SREV_VERSION_9561:
  487. break;
  488. default:
  489. ath_err(common,
  490. "Mac Chip Rev 0x%02x.%x is not supported by this driver\n",
  491. ah->hw_version.macVersion, ah->hw_version.macRev);
  492. return -EOPNOTSUPP;
  493. }
  494. /*
  495. * Read back AR_WA into a permanent copy and set bits 14 and 17.
  496. * We need to do this to avoid RMW of this register. We cannot
  497. * read the reg when chip is asleep.
  498. */
  499. if (AR_SREV_9300_20_OR_LATER(ah)) {
  500. ah->WARegVal = REG_READ(ah, AR_WA);
  501. ah->WARegVal |= (AR_WA_D3_L1_DISABLE |
  502. AR_WA_ASPM_TIMER_BASED_DISABLE);
  503. }
  504. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  505. ath_err(common, "Couldn't reset chip\n");
  506. return -EIO;
  507. }
  508. if (AR_SREV_9565(ah)) {
  509. ah->WARegVal |= AR_WA_BIT22;
  510. REG_WRITE(ah, AR_WA, ah->WARegVal);
  511. }
  512. ath9k_hw_init_defaults(ah);
  513. ath9k_hw_init_config(ah);
  514. r = ath9k_hw_attach_ops(ah);
  515. if (r)
  516. return r;
  517. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE)) {
  518. ath_err(common, "Couldn't wakeup chip\n");
  519. return -EIO;
  520. }
  521. if (AR_SREV_9271(ah) || AR_SREV_9100(ah) || AR_SREV_9340(ah) ||
  522. AR_SREV_9330(ah) || AR_SREV_9550(ah))
  523. ah->is_pciexpress = false;
  524. ah->hw_version.phyRev = REG_READ(ah, AR_PHY_CHIP_ID);
  525. ath9k_hw_init_cal_settings(ah);
  526. if (!ah->is_pciexpress)
  527. ath9k_hw_disablepcie(ah);
  528. r = ath9k_hw_post_init(ah);
  529. if (r)
  530. return r;
  531. ath9k_hw_init_mode_gain_regs(ah);
  532. r = ath9k_hw_fill_cap_info(ah);
  533. if (r)
  534. return r;
  535. ath9k_hw_init_macaddr(ah);
  536. ath9k_hw_init_hang_checks(ah);
  537. common->state = ATH_HW_INITIALIZED;
  538. return 0;
  539. }
  540. int ath9k_hw_init(struct ath_hw *ah)
  541. {
  542. int ret;
  543. struct ath_common *common = ath9k_hw_common(ah);
  544. /* These are all the AR5008/AR9001/AR9002/AR9003 hardware family of chipsets */
  545. switch (ah->hw_version.devid) {
  546. case AR5416_DEVID_PCI:
  547. case AR5416_DEVID_PCIE:
  548. case AR5416_AR9100_DEVID:
  549. case AR9160_DEVID_PCI:
  550. case AR9280_DEVID_PCI:
  551. case AR9280_DEVID_PCIE:
  552. case AR9285_DEVID_PCIE:
  553. case AR9287_DEVID_PCI:
  554. case AR9287_DEVID_PCIE:
  555. case AR2427_DEVID_PCIE:
  556. case AR9300_DEVID_PCIE:
  557. case AR9300_DEVID_AR9485_PCIE:
  558. case AR9300_DEVID_AR9330:
  559. case AR9300_DEVID_AR9340:
  560. case AR9300_DEVID_QCA955X:
  561. case AR9300_DEVID_AR9580:
  562. case AR9300_DEVID_AR9462:
  563. case AR9485_DEVID_AR1111:
  564. case AR9300_DEVID_AR9565:
  565. case AR9300_DEVID_AR953X:
  566. case AR9300_DEVID_QCA956X:
  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. ath_dynack_init(ah);
  583. return 0;
  584. }
  585. EXPORT_SYMBOL(ath9k_hw_init);
  586. static void ath9k_hw_init_qos(struct ath_hw *ah)
  587. {
  588. ENABLE_REGWRITE_BUFFER(ah);
  589. REG_WRITE(ah, AR_MIC_QOS_CONTROL, 0x100aa);
  590. REG_WRITE(ah, AR_MIC_QOS_SELECT, 0x3210);
  591. REG_WRITE(ah, AR_QOS_NO_ACK,
  592. SM(2, AR_QOS_NO_ACK_TWO_BIT) |
  593. SM(5, AR_QOS_NO_ACK_BIT_OFF) |
  594. SM(0, AR_QOS_NO_ACK_BYTE_OFF));
  595. REG_WRITE(ah, AR_TXOP_X, AR_TXOP_X_VAL);
  596. REG_WRITE(ah, AR_TXOP_0_3, 0xFFFFFFFF);
  597. REG_WRITE(ah, AR_TXOP_4_7, 0xFFFFFFFF);
  598. REG_WRITE(ah, AR_TXOP_8_11, 0xFFFFFFFF);
  599. REG_WRITE(ah, AR_TXOP_12_15, 0xFFFFFFFF);
  600. REGWRITE_BUFFER_FLUSH(ah);
  601. }
  602. u32 ar9003_get_pll_sqsum_dvc(struct ath_hw *ah)
  603. {
  604. struct ath_common *common = ath9k_hw_common(ah);
  605. int i = 0;
  606. REG_CLR_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
  607. udelay(100);
  608. REG_SET_BIT(ah, PLL3, PLL3_DO_MEAS_MASK);
  609. while ((REG_READ(ah, PLL4) & PLL4_MEAS_DONE) == 0) {
  610. udelay(100);
  611. if (WARN_ON_ONCE(i >= 100)) {
  612. ath_err(common, "PLL4 meaurement not done\n");
  613. break;
  614. }
  615. i++;
  616. }
  617. return (REG_READ(ah, PLL3) & SQSUM_DVC_MASK) >> 3;
  618. }
  619. EXPORT_SYMBOL(ar9003_get_pll_sqsum_dvc);
  620. static void ath9k_hw_init_pll(struct ath_hw *ah,
  621. struct ath9k_channel *chan)
  622. {
  623. u32 pll;
  624. pll = ath9k_hw_compute_pll_control(ah, chan);
  625. if (AR_SREV_9485(ah) || AR_SREV_9565(ah)) {
  626. /* program BB PLL ki and kd value, ki=0x4, kd=0x40 */
  627. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  628. AR_CH0_BB_DPLL2_PLL_PWD, 0x1);
  629. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  630. AR_CH0_DPLL2_KD, 0x40);
  631. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  632. AR_CH0_DPLL2_KI, 0x4);
  633. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  634. AR_CH0_BB_DPLL1_REFDIV, 0x5);
  635. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  636. AR_CH0_BB_DPLL1_NINI, 0x58);
  637. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL1,
  638. AR_CH0_BB_DPLL1_NFRAC, 0x0);
  639. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  640. AR_CH0_BB_DPLL2_OUTDIV, 0x1);
  641. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  642. AR_CH0_BB_DPLL2_LOCAL_PLL, 0x1);
  643. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  644. AR_CH0_BB_DPLL2_EN_NEGTRIG, 0x1);
  645. /* program BB PLL phase_shift to 0x6 */
  646. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
  647. AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x6);
  648. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2,
  649. AR_CH0_BB_DPLL2_PLL_PWD, 0x0);
  650. udelay(1000);
  651. } else if (AR_SREV_9330(ah)) {
  652. u32 ddr_dpll2, pll_control2, kd;
  653. if (ah->is_clk_25mhz) {
  654. ddr_dpll2 = 0x18e82f01;
  655. pll_control2 = 0xe04a3d;
  656. kd = 0x1d;
  657. } else {
  658. ddr_dpll2 = 0x19e82f01;
  659. pll_control2 = 0x886666;
  660. kd = 0x3d;
  661. }
  662. /* program DDR PLL ki and kd value */
  663. REG_WRITE(ah, AR_CH0_DDR_DPLL2, ddr_dpll2);
  664. /* program DDR PLL phase_shift */
  665. REG_RMW_FIELD(ah, AR_CH0_DDR_DPLL3,
  666. AR_CH0_DPLL3_PHASE_SHIFT, 0x1);
  667. REG_WRITE(ah, AR_RTC_PLL_CONTROL,
  668. pll | AR_RTC_9300_PLL_BYPASS);
  669. udelay(1000);
  670. /* program refdiv, nint, frac to RTC register */
  671. REG_WRITE(ah, AR_RTC_PLL_CONTROL2, pll_control2);
  672. /* program BB PLL kd and ki value */
  673. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KD, kd);
  674. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL2, AR_CH0_DPLL2_KI, 0x06);
  675. /* program BB PLL phase_shift */
  676. REG_RMW_FIELD(ah, AR_CH0_BB_DPLL3,
  677. AR_CH0_BB_DPLL3_PHASE_SHIFT, 0x1);
  678. } else if (AR_SREV_9340(ah) || AR_SREV_9550(ah) || AR_SREV_9531(ah) ||
  679. AR_SREV_9561(ah)) {
  680. u32 regval, pll2_divint, pll2_divfrac, refdiv;
  681. REG_WRITE(ah, AR_RTC_PLL_CONTROL,
  682. pll | AR_RTC_9300_SOC_PLL_BYPASS);
  683. udelay(1000);
  684. REG_SET_BIT(ah, AR_PHY_PLL_MODE, 0x1 << 16);
  685. udelay(100);
  686. if (ah->is_clk_25mhz) {
  687. if (AR_SREV_9531(ah) || AR_SREV_9561(ah)) {
  688. pll2_divint = 0x1c;
  689. pll2_divfrac = 0xa3d2;
  690. refdiv = 1;
  691. } else {
  692. pll2_divint = 0x54;
  693. pll2_divfrac = 0x1eb85;
  694. refdiv = 3;
  695. }
  696. } else {
  697. if (AR_SREV_9340(ah)) {
  698. pll2_divint = 88;
  699. pll2_divfrac = 0;
  700. refdiv = 5;
  701. } else {
  702. pll2_divint = 0x11;
  703. pll2_divfrac = (AR_SREV_9531(ah) ||
  704. AR_SREV_9561(ah)) ?
  705. 0x26665 : 0x26666;
  706. refdiv = 1;
  707. }
  708. }
  709. regval = REG_READ(ah, AR_PHY_PLL_MODE);
  710. if (AR_SREV_9531(ah) || AR_SREV_9561(ah))
  711. regval |= (0x1 << 22);
  712. else
  713. regval |= (0x1 << 16);
  714. REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
  715. udelay(100);
  716. REG_WRITE(ah, AR_PHY_PLL_CONTROL, (refdiv << 27) |
  717. (pll2_divint << 18) | pll2_divfrac);
  718. udelay(100);
  719. regval = REG_READ(ah, AR_PHY_PLL_MODE);
  720. if (AR_SREV_9340(ah))
  721. regval = (regval & 0x80071fff) |
  722. (0x1 << 30) |
  723. (0x1 << 13) |
  724. (0x4 << 26) |
  725. (0x18 << 19);
  726. else if (AR_SREV_9531(ah) || AR_SREV_9561(ah)) {
  727. regval = (regval & 0x01c00fff) |
  728. (0x1 << 31) |
  729. (0x2 << 29) |
  730. (0xa << 25) |
  731. (0x1 << 19);
  732. if (AR_SREV_9531(ah))
  733. regval |= (0x6 << 12);
  734. } else
  735. regval = (regval & 0x80071fff) |
  736. (0x3 << 30) |
  737. (0x1 << 13) |
  738. (0x4 << 26) |
  739. (0x60 << 19);
  740. REG_WRITE(ah, AR_PHY_PLL_MODE, regval);
  741. if (AR_SREV_9531(ah) || AR_SREV_9561(ah))
  742. REG_WRITE(ah, AR_PHY_PLL_MODE,
  743. REG_READ(ah, AR_PHY_PLL_MODE) & 0xffbfffff);
  744. else
  745. REG_WRITE(ah, AR_PHY_PLL_MODE,
  746. REG_READ(ah, AR_PHY_PLL_MODE) & 0xfffeffff);
  747. udelay(1000);
  748. }
  749. if (AR_SREV_9565(ah))
  750. pll |= 0x40000;
  751. REG_WRITE(ah, AR_RTC_PLL_CONTROL, pll);
  752. if (AR_SREV_9485(ah) || AR_SREV_9340(ah) || AR_SREV_9330(ah) ||
  753. AR_SREV_9550(ah))
  754. udelay(1000);
  755. /* Switch the core clock for ar9271 to 117Mhz */
  756. if (AR_SREV_9271(ah)) {
  757. udelay(500);
  758. REG_WRITE(ah, 0x50040, 0x304);
  759. }
  760. udelay(RTC_PLL_SETTLE_DELAY);
  761. REG_WRITE(ah, AR_RTC_SLEEP_CLK, AR_RTC_FORCE_DERIVED_CLK);
  762. }
  763. static void ath9k_hw_init_interrupt_masks(struct ath_hw *ah,
  764. enum nl80211_iftype opmode)
  765. {
  766. u32 sync_default = AR_INTR_SYNC_DEFAULT;
  767. u32 imr_reg = AR_IMR_TXERR |
  768. AR_IMR_TXURN |
  769. AR_IMR_RXERR |
  770. AR_IMR_RXORN |
  771. AR_IMR_BCNMISC;
  772. if (AR_SREV_9340(ah) || AR_SREV_9550(ah) || AR_SREV_9531(ah) ||
  773. AR_SREV_9561(ah))
  774. sync_default &= ~AR_INTR_SYNC_HOST1_FATAL;
  775. if (AR_SREV_9300_20_OR_LATER(ah)) {
  776. imr_reg |= AR_IMR_RXOK_HP;
  777. if (ah->config.rx_intr_mitigation)
  778. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  779. else
  780. imr_reg |= AR_IMR_RXOK_LP;
  781. } else {
  782. if (ah->config.rx_intr_mitigation)
  783. imr_reg |= AR_IMR_RXINTM | AR_IMR_RXMINTR;
  784. else
  785. imr_reg |= AR_IMR_RXOK;
  786. }
  787. if (ah->config.tx_intr_mitigation)
  788. imr_reg |= AR_IMR_TXINTM | AR_IMR_TXMINTR;
  789. else
  790. imr_reg |= AR_IMR_TXOK;
  791. ENABLE_REGWRITE_BUFFER(ah);
  792. REG_WRITE(ah, AR_IMR, imr_reg);
  793. ah->imrs2_reg |= AR_IMR_S2_GTT;
  794. REG_WRITE(ah, AR_IMR_S2, ah->imrs2_reg);
  795. if (!AR_SREV_9100(ah)) {
  796. REG_WRITE(ah, AR_INTR_SYNC_CAUSE, 0xFFFFFFFF);
  797. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, sync_default);
  798. REG_WRITE(ah, AR_INTR_SYNC_MASK, 0);
  799. }
  800. REGWRITE_BUFFER_FLUSH(ah);
  801. if (AR_SREV_9300_20_OR_LATER(ah)) {
  802. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_ENABLE, 0);
  803. REG_WRITE(ah, AR_INTR_PRIO_ASYNC_MASK, 0);
  804. REG_WRITE(ah, AR_INTR_PRIO_SYNC_ENABLE, 0);
  805. REG_WRITE(ah, AR_INTR_PRIO_SYNC_MASK, 0);
  806. }
  807. }
  808. static void ath9k_hw_set_sifs_time(struct ath_hw *ah, u32 us)
  809. {
  810. u32 val = ath9k_hw_mac_to_clks(ah, us - 2);
  811. val = min(val, (u32) 0xFFFF);
  812. REG_WRITE(ah, AR_D_GBL_IFS_SIFS, val);
  813. }
  814. void ath9k_hw_setslottime(struct ath_hw *ah, u32 us)
  815. {
  816. u32 val = ath9k_hw_mac_to_clks(ah, us);
  817. val = min(val, (u32) 0xFFFF);
  818. REG_WRITE(ah, AR_D_GBL_IFS_SLOT, val);
  819. }
  820. void ath9k_hw_set_ack_timeout(struct ath_hw *ah, u32 us)
  821. {
  822. u32 val = ath9k_hw_mac_to_clks(ah, us);
  823. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_ACK));
  824. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_ACK, val);
  825. }
  826. void ath9k_hw_set_cts_timeout(struct ath_hw *ah, u32 us)
  827. {
  828. u32 val = ath9k_hw_mac_to_clks(ah, us);
  829. val = min(val, (u32) MS(0xFFFFFFFF, AR_TIME_OUT_CTS));
  830. REG_RMW_FIELD(ah, AR_TIME_OUT, AR_TIME_OUT_CTS, val);
  831. }
  832. static bool ath9k_hw_set_global_txtimeout(struct ath_hw *ah, u32 tu)
  833. {
  834. if (tu > 0xFFFF) {
  835. ath_dbg(ath9k_hw_common(ah), XMIT, "bad global tx timeout %u\n",
  836. tu);
  837. ah->globaltxtimeout = (u32) -1;
  838. return false;
  839. } else {
  840. REG_RMW_FIELD(ah, AR_GTXTO, AR_GTXTO_TIMEOUT_LIMIT, tu);
  841. ah->globaltxtimeout = tu;
  842. return true;
  843. }
  844. }
  845. void ath9k_hw_init_global_settings(struct ath_hw *ah)
  846. {
  847. struct ath_common *common = ath9k_hw_common(ah);
  848. const struct ath9k_channel *chan = ah->curchan;
  849. int acktimeout, ctstimeout, ack_offset = 0;
  850. int slottime;
  851. int sifstime;
  852. int rx_lat = 0, tx_lat = 0, eifs = 0;
  853. u32 reg;
  854. ath_dbg(ath9k_hw_common(ah), RESET, "ah->misc_mode 0x%x\n",
  855. ah->misc_mode);
  856. if (!chan)
  857. return;
  858. if (ah->misc_mode != 0)
  859. REG_SET_BIT(ah, AR_PCU_MISC, ah->misc_mode);
  860. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  861. rx_lat = 41;
  862. else
  863. rx_lat = 37;
  864. tx_lat = 54;
  865. if (IS_CHAN_5GHZ(chan))
  866. sifstime = 16;
  867. else
  868. sifstime = 10;
  869. if (IS_CHAN_HALF_RATE(chan)) {
  870. eifs = 175;
  871. rx_lat *= 2;
  872. tx_lat *= 2;
  873. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  874. tx_lat += 11;
  875. sifstime = 32;
  876. ack_offset = 16;
  877. slottime = 13;
  878. } else if (IS_CHAN_QUARTER_RATE(chan)) {
  879. eifs = 340;
  880. rx_lat = (rx_lat * 4) - 1;
  881. tx_lat *= 4;
  882. if (IS_CHAN_A_FAST_CLOCK(ah, chan))
  883. tx_lat += 22;
  884. sifstime = 64;
  885. ack_offset = 32;
  886. slottime = 21;
  887. } else {
  888. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
  889. eifs = AR_D_GBL_IFS_EIFS_ASYNC_FIFO;
  890. reg = AR_USEC_ASYNC_FIFO;
  891. } else {
  892. eifs = REG_READ(ah, AR_D_GBL_IFS_EIFS)/
  893. common->clockrate;
  894. reg = REG_READ(ah, AR_USEC);
  895. }
  896. rx_lat = MS(reg, AR_USEC_RX_LAT);
  897. tx_lat = MS(reg, AR_USEC_TX_LAT);
  898. slottime = ah->slottime;
  899. }
  900. /* As defined by IEEE 802.11-2007 17.3.8.6 */
  901. slottime += 3 * ah->coverage_class;
  902. acktimeout = slottime + sifstime + ack_offset;
  903. ctstimeout = acktimeout;
  904. /*
  905. * Workaround for early ACK timeouts, add an offset to match the
  906. * initval's 64us ack timeout value. Use 48us for the CTS timeout.
  907. * This was initially only meant to work around an issue with delayed
  908. * BA frames in some implementations, but it has been found to fix ACK
  909. * timeout issues in other cases as well.
  910. */
  911. if (IS_CHAN_2GHZ(chan) &&
  912. !IS_CHAN_HALF_RATE(chan) && !IS_CHAN_QUARTER_RATE(chan)) {
  913. acktimeout += 64 - sifstime - ah->slottime;
  914. ctstimeout += 48 - sifstime - ah->slottime;
  915. }
  916. if (ah->dynack.enabled) {
  917. acktimeout = ah->dynack.ackto;
  918. ctstimeout = acktimeout;
  919. slottime = (acktimeout - 3) / 2;
  920. } else {
  921. ah->dynack.ackto = acktimeout;
  922. }
  923. ath9k_hw_set_sifs_time(ah, sifstime);
  924. ath9k_hw_setslottime(ah, slottime);
  925. ath9k_hw_set_ack_timeout(ah, acktimeout);
  926. ath9k_hw_set_cts_timeout(ah, ctstimeout);
  927. if (ah->globaltxtimeout != (u32) -1)
  928. ath9k_hw_set_global_txtimeout(ah, ah->globaltxtimeout);
  929. REG_WRITE(ah, AR_D_GBL_IFS_EIFS, ath9k_hw_mac_to_clks(ah, eifs));
  930. REG_RMW(ah, AR_USEC,
  931. (common->clockrate - 1) |
  932. SM(rx_lat, AR_USEC_RX_LAT) |
  933. SM(tx_lat, AR_USEC_TX_LAT),
  934. AR_USEC_TX_LAT | AR_USEC_RX_LAT | AR_USEC_USEC);
  935. }
  936. EXPORT_SYMBOL(ath9k_hw_init_global_settings);
  937. void ath9k_hw_deinit(struct ath_hw *ah)
  938. {
  939. struct ath_common *common = ath9k_hw_common(ah);
  940. if (common->state < ATH_HW_INITIALIZED)
  941. return;
  942. ath9k_hw_setpower(ah, ATH9K_PM_FULL_SLEEP);
  943. }
  944. EXPORT_SYMBOL(ath9k_hw_deinit);
  945. /*******/
  946. /* INI */
  947. /*******/
  948. u32 ath9k_regd_get_ctl(struct ath_regulatory *reg, struct ath9k_channel *chan)
  949. {
  950. u32 ctl = ath_regd_get_band_ctl(reg, chan->chan->band);
  951. if (IS_CHAN_2GHZ(chan))
  952. ctl |= CTL_11G;
  953. else
  954. ctl |= CTL_11A;
  955. return ctl;
  956. }
  957. /****************************************/
  958. /* Reset and Channel Switching Routines */
  959. /****************************************/
  960. static inline void ath9k_hw_set_dma(struct ath_hw *ah)
  961. {
  962. struct ath_common *common = ath9k_hw_common(ah);
  963. int txbuf_size;
  964. ENABLE_REGWRITE_BUFFER(ah);
  965. /*
  966. * set AHB_MODE not to do cacheline prefetches
  967. */
  968. if (!AR_SREV_9300_20_OR_LATER(ah))
  969. REG_SET_BIT(ah, AR_AHB_MODE, AR_AHB_PREFETCH_RD_EN);
  970. /*
  971. * let mac dma reads be in 128 byte chunks
  972. */
  973. REG_RMW(ah, AR_TXCFG, AR_TXCFG_DMASZ_128B, AR_TXCFG_DMASZ_MASK);
  974. REGWRITE_BUFFER_FLUSH(ah);
  975. /*
  976. * Restore TX Trigger Level to its pre-reset value.
  977. * The initial value depends on whether aggregation is enabled, and is
  978. * adjusted whenever underruns are detected.
  979. */
  980. if (!AR_SREV_9300_20_OR_LATER(ah))
  981. REG_RMW_FIELD(ah, AR_TXCFG, AR_FTRIG, ah->tx_trig_level);
  982. ENABLE_REGWRITE_BUFFER(ah);
  983. /*
  984. * let mac dma writes be in 128 byte chunks
  985. */
  986. REG_RMW(ah, AR_RXCFG, AR_RXCFG_DMASZ_128B, AR_RXCFG_DMASZ_MASK);
  987. /*
  988. * Setup receive FIFO threshold to hold off TX activities
  989. */
  990. REG_WRITE(ah, AR_RXFIFO_CFG, 0x200);
  991. if (AR_SREV_9300_20_OR_LATER(ah)) {
  992. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_HP, 0x1);
  993. REG_RMW_FIELD(ah, AR_RXBP_THRESH, AR_RXBP_THRESH_LP, 0x1);
  994. ath9k_hw_set_rx_bufsize(ah, common->rx_bufsize -
  995. ah->caps.rx_status_len);
  996. }
  997. /*
  998. * reduce the number of usable entries in PCU TXBUF to avoid
  999. * wrap around issues.
  1000. */
  1001. if (AR_SREV_9285(ah)) {
  1002. /* For AR9285 the number of Fifos are reduced to half.
  1003. * So set the usable tx buf size also to half to
  1004. * avoid data/delimiter underruns
  1005. */
  1006. txbuf_size = AR_9285_PCU_TXBUF_CTRL_USABLE_SIZE;
  1007. } else if (AR_SREV_9340_13_OR_LATER(ah)) {
  1008. /* Uses fewer entries for AR934x v1.3+ to prevent rx overruns */
  1009. txbuf_size = AR_9340_PCU_TXBUF_CTRL_USABLE_SIZE;
  1010. } else {
  1011. txbuf_size = AR_PCU_TXBUF_CTRL_USABLE_SIZE;
  1012. }
  1013. if (!AR_SREV_9271(ah))
  1014. REG_WRITE(ah, AR_PCU_TXBUF_CTRL, txbuf_size);
  1015. REGWRITE_BUFFER_FLUSH(ah);
  1016. if (AR_SREV_9300_20_OR_LATER(ah))
  1017. ath9k_hw_reset_txstatus_ring(ah);
  1018. }
  1019. static void ath9k_hw_set_operating_mode(struct ath_hw *ah, int opmode)
  1020. {
  1021. u32 mask = AR_STA_ID1_STA_AP | AR_STA_ID1_ADHOC;
  1022. u32 set = AR_STA_ID1_KSRCH_MODE;
  1023. ENABLE_REG_RMW_BUFFER(ah);
  1024. switch (opmode) {
  1025. case NL80211_IFTYPE_ADHOC:
  1026. if (!AR_SREV_9340_13(ah)) {
  1027. set |= AR_STA_ID1_ADHOC;
  1028. REG_SET_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  1029. break;
  1030. }
  1031. /* fall through */
  1032. case NL80211_IFTYPE_OCB:
  1033. case NL80211_IFTYPE_MESH_POINT:
  1034. case NL80211_IFTYPE_AP:
  1035. set |= AR_STA_ID1_STA_AP;
  1036. /* fall through */
  1037. case NL80211_IFTYPE_STATION:
  1038. REG_CLR_BIT(ah, AR_CFG, AR_CFG_AP_ADHOC_INDICATION);
  1039. break;
  1040. default:
  1041. if (!ah->is_monitoring)
  1042. set = 0;
  1043. break;
  1044. }
  1045. REG_RMW(ah, AR_STA_ID1, set, mask);
  1046. REG_RMW_BUFFER_FLUSH(ah);
  1047. }
  1048. void ath9k_hw_get_delta_slope_vals(struct ath_hw *ah, u32 coef_scaled,
  1049. u32 *coef_mantissa, u32 *coef_exponent)
  1050. {
  1051. u32 coef_exp, coef_man;
  1052. for (coef_exp = 31; coef_exp > 0; coef_exp--)
  1053. if ((coef_scaled >> coef_exp) & 0x1)
  1054. break;
  1055. coef_exp = 14 - (coef_exp - COEF_SCALE_S);
  1056. coef_man = coef_scaled + (1 << (COEF_SCALE_S - coef_exp - 1));
  1057. *coef_mantissa = coef_man >> (COEF_SCALE_S - coef_exp);
  1058. *coef_exponent = coef_exp - 16;
  1059. }
  1060. /* AR9330 WAR:
  1061. * call external reset function to reset WMAC if:
  1062. * - doing a cold reset
  1063. * - we have pending frames in the TX queues.
  1064. */
  1065. static bool ath9k_hw_ar9330_reset_war(struct ath_hw *ah, int type)
  1066. {
  1067. int i, npend = 0;
  1068. for (i = 0; i < AR_NUM_QCU; i++) {
  1069. npend = ath9k_hw_numtxpending(ah, i);
  1070. if (npend)
  1071. break;
  1072. }
  1073. if (ah->external_reset &&
  1074. (npend || type == ATH9K_RESET_COLD)) {
  1075. int reset_err = 0;
  1076. ath_dbg(ath9k_hw_common(ah), RESET,
  1077. "reset MAC via external reset\n");
  1078. reset_err = ah->external_reset();
  1079. if (reset_err) {
  1080. ath_err(ath9k_hw_common(ah),
  1081. "External reset failed, err=%d\n",
  1082. reset_err);
  1083. return false;
  1084. }
  1085. REG_WRITE(ah, AR_RTC_RESET, 1);
  1086. }
  1087. return true;
  1088. }
  1089. static bool ath9k_hw_set_reset(struct ath_hw *ah, int type)
  1090. {
  1091. u32 rst_flags;
  1092. u32 tmpReg;
  1093. if (AR_SREV_9100(ah)) {
  1094. REG_RMW_FIELD(ah, AR_RTC_DERIVED_CLK,
  1095. AR_RTC_DERIVED_CLK_PERIOD, 1);
  1096. (void)REG_READ(ah, AR_RTC_DERIVED_CLK);
  1097. }
  1098. ENABLE_REGWRITE_BUFFER(ah);
  1099. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1100. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1101. udelay(10);
  1102. }
  1103. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1104. AR_RTC_FORCE_WAKE_ON_INT);
  1105. if (AR_SREV_9100(ah)) {
  1106. rst_flags = AR_RTC_RC_MAC_WARM | AR_RTC_RC_MAC_COLD |
  1107. AR_RTC_RC_COLD_RESET | AR_RTC_RC_WARM_RESET;
  1108. } else {
  1109. tmpReg = REG_READ(ah, AR_INTR_SYNC_CAUSE);
  1110. if (AR_SREV_9340(ah))
  1111. tmpReg &= AR9340_INTR_SYNC_LOCAL_TIMEOUT;
  1112. else
  1113. tmpReg &= AR_INTR_SYNC_LOCAL_TIMEOUT |
  1114. AR_INTR_SYNC_RADM_CPL_TIMEOUT;
  1115. if (tmpReg) {
  1116. u32 val;
  1117. REG_WRITE(ah, AR_INTR_SYNC_ENABLE, 0);
  1118. val = AR_RC_HOSTIF;
  1119. if (!AR_SREV_9300_20_OR_LATER(ah))
  1120. val |= AR_RC_AHB;
  1121. REG_WRITE(ah, AR_RC, val);
  1122. } else if (!AR_SREV_9300_20_OR_LATER(ah))
  1123. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  1124. rst_flags = AR_RTC_RC_MAC_WARM;
  1125. if (type == ATH9K_RESET_COLD)
  1126. rst_flags |= AR_RTC_RC_MAC_COLD;
  1127. }
  1128. if (AR_SREV_9330(ah)) {
  1129. if (!ath9k_hw_ar9330_reset_war(ah, type))
  1130. return false;
  1131. }
  1132. if (ath9k_hw_mci_is_enabled(ah))
  1133. ar9003_mci_check_gpm_offset(ah);
  1134. /* DMA HALT added to resolve ar9300 and ar9580 bus error during
  1135. * RTC_RC reg read
  1136. */
  1137. if (AR_SREV_9300(ah) || AR_SREV_9580(ah)) {
  1138. REG_SET_BIT(ah, AR_CFG, AR_CFG_HALT_REQ);
  1139. ath9k_hw_wait(ah, AR_CFG, AR_CFG_HALT_ACK, AR_CFG_HALT_ACK,
  1140. 20 * AH_WAIT_TIMEOUT);
  1141. REG_CLR_BIT(ah, AR_CFG, AR_CFG_HALT_REQ);
  1142. }
  1143. REG_WRITE(ah, AR_RTC_RC, rst_flags);
  1144. REGWRITE_BUFFER_FLUSH(ah);
  1145. if (AR_SREV_9300_20_OR_LATER(ah))
  1146. udelay(50);
  1147. else if (AR_SREV_9100(ah))
  1148. mdelay(10);
  1149. else
  1150. udelay(100);
  1151. REG_WRITE(ah, AR_RTC_RC, 0);
  1152. if (!ath9k_hw_wait(ah, AR_RTC_RC, AR_RTC_RC_M, 0, AH_WAIT_TIMEOUT)) {
  1153. ath_dbg(ath9k_hw_common(ah), RESET, "RTC stuck in MAC reset\n");
  1154. return false;
  1155. }
  1156. if (!AR_SREV_9100(ah))
  1157. REG_WRITE(ah, AR_RC, 0);
  1158. if (AR_SREV_9100(ah))
  1159. udelay(50);
  1160. return true;
  1161. }
  1162. static bool ath9k_hw_set_reset_power_on(struct ath_hw *ah)
  1163. {
  1164. ENABLE_REGWRITE_BUFFER(ah);
  1165. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1166. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1167. udelay(10);
  1168. }
  1169. REG_WRITE(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN |
  1170. AR_RTC_FORCE_WAKE_ON_INT);
  1171. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1172. REG_WRITE(ah, AR_RC, AR_RC_AHB);
  1173. REG_WRITE(ah, AR_RTC_RESET, 0);
  1174. REGWRITE_BUFFER_FLUSH(ah);
  1175. udelay(2);
  1176. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1177. REG_WRITE(ah, AR_RC, 0);
  1178. REG_WRITE(ah, AR_RTC_RESET, 1);
  1179. if (!ath9k_hw_wait(ah,
  1180. AR_RTC_STATUS,
  1181. AR_RTC_STATUS_M,
  1182. AR_RTC_STATUS_ON,
  1183. AH_WAIT_TIMEOUT)) {
  1184. ath_dbg(ath9k_hw_common(ah), RESET, "RTC not waking up\n");
  1185. return false;
  1186. }
  1187. return ath9k_hw_set_reset(ah, ATH9K_RESET_WARM);
  1188. }
  1189. static bool ath9k_hw_set_reset_reg(struct ath_hw *ah, u32 type)
  1190. {
  1191. bool ret = false;
  1192. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1193. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1194. udelay(10);
  1195. }
  1196. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1197. AR_RTC_FORCE_WAKE_EN | AR_RTC_FORCE_WAKE_ON_INT);
  1198. if (!ah->reset_power_on)
  1199. type = ATH9K_RESET_POWER_ON;
  1200. switch (type) {
  1201. case ATH9K_RESET_POWER_ON:
  1202. ret = ath9k_hw_set_reset_power_on(ah);
  1203. if (ret)
  1204. ah->reset_power_on = true;
  1205. break;
  1206. case ATH9K_RESET_WARM:
  1207. case ATH9K_RESET_COLD:
  1208. ret = ath9k_hw_set_reset(ah, type);
  1209. break;
  1210. default:
  1211. break;
  1212. }
  1213. return ret;
  1214. }
  1215. static bool ath9k_hw_chip_reset(struct ath_hw *ah,
  1216. struct ath9k_channel *chan)
  1217. {
  1218. int reset_type = ATH9K_RESET_WARM;
  1219. if (AR_SREV_9280(ah)) {
  1220. if (ah->eep_ops->get_eeprom(ah, EEP_OL_PWRCTRL))
  1221. reset_type = ATH9K_RESET_POWER_ON;
  1222. else
  1223. reset_type = ATH9K_RESET_COLD;
  1224. } else if (ah->chip_fullsleep || REG_READ(ah, AR_Q_TXE) ||
  1225. (REG_READ(ah, AR_CR) & AR_CR_RXE))
  1226. reset_type = ATH9K_RESET_COLD;
  1227. if (!ath9k_hw_set_reset_reg(ah, reset_type))
  1228. return false;
  1229. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1230. return false;
  1231. ah->chip_fullsleep = false;
  1232. if (AR_SREV_9330(ah))
  1233. ar9003_hw_internal_regulator_apply(ah);
  1234. ath9k_hw_init_pll(ah, chan);
  1235. return true;
  1236. }
  1237. static bool ath9k_hw_channel_change(struct ath_hw *ah,
  1238. struct ath9k_channel *chan)
  1239. {
  1240. struct ath_common *common = ath9k_hw_common(ah);
  1241. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1242. bool band_switch = false, mode_diff = false;
  1243. u8 ini_reloaded = 0;
  1244. u32 qnum;
  1245. int r;
  1246. if (pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH) {
  1247. u32 flags_diff = chan->channelFlags ^ ah->curchan->channelFlags;
  1248. band_switch = !!(flags_diff & CHANNEL_5GHZ);
  1249. mode_diff = !!(flags_diff & ~CHANNEL_HT);
  1250. }
  1251. for (qnum = 0; qnum < AR_NUM_QCU; qnum++) {
  1252. if (ath9k_hw_numtxpending(ah, qnum)) {
  1253. ath_dbg(common, QUEUE,
  1254. "Transmit frames pending on queue %d\n", qnum);
  1255. return false;
  1256. }
  1257. }
  1258. if (!ath9k_hw_rfbus_req(ah)) {
  1259. ath_err(common, "Could not kill baseband RX\n");
  1260. return false;
  1261. }
  1262. if (band_switch || mode_diff) {
  1263. ath9k_hw_mark_phy_inactive(ah);
  1264. udelay(5);
  1265. if (band_switch)
  1266. ath9k_hw_init_pll(ah, chan);
  1267. if (ath9k_hw_fast_chan_change(ah, chan, &ini_reloaded)) {
  1268. ath_err(common, "Failed to do fast channel change\n");
  1269. return false;
  1270. }
  1271. }
  1272. ath9k_hw_set_channel_regs(ah, chan);
  1273. r = ath9k_hw_rf_set_freq(ah, chan);
  1274. if (r) {
  1275. ath_err(common, "Failed to set channel\n");
  1276. return false;
  1277. }
  1278. ath9k_hw_set_clockrate(ah);
  1279. ath9k_hw_apply_txpower(ah, chan, false);
  1280. ath9k_hw_set_delta_slope(ah, chan);
  1281. ath9k_hw_spur_mitigate_freq(ah, chan);
  1282. if (band_switch || ini_reloaded)
  1283. ah->eep_ops->set_board_values(ah, chan);
  1284. ath9k_hw_init_bb(ah, chan);
  1285. ath9k_hw_rfbus_done(ah);
  1286. if (band_switch || ini_reloaded) {
  1287. ah->ah_flags |= AH_FASTCC;
  1288. ath9k_hw_init_cal(ah, chan);
  1289. ah->ah_flags &= ~AH_FASTCC;
  1290. }
  1291. return true;
  1292. }
  1293. static void ath9k_hw_apply_gpio_override(struct ath_hw *ah)
  1294. {
  1295. u32 gpio_mask = ah->gpio_mask;
  1296. int i;
  1297. for (i = 0; gpio_mask; i++, gpio_mask >>= 1) {
  1298. if (!(gpio_mask & 1))
  1299. continue;
  1300. ath9k_hw_gpio_request_out(ah, i, NULL,
  1301. AR_GPIO_OUTPUT_MUX_AS_OUTPUT);
  1302. ath9k_hw_set_gpio(ah, i, !!(ah->gpio_val & BIT(i)));
  1303. ath9k_hw_gpio_free(ah, i);
  1304. }
  1305. }
  1306. void ath9k_hw_check_nav(struct ath_hw *ah)
  1307. {
  1308. struct ath_common *common = ath9k_hw_common(ah);
  1309. u32 val;
  1310. val = REG_READ(ah, AR_NAV);
  1311. if (val != 0xdeadbeef && val > 0x7fff) {
  1312. ath_dbg(common, BSTUCK, "Abnormal NAV: 0x%x\n", val);
  1313. REG_WRITE(ah, AR_NAV, 0);
  1314. }
  1315. }
  1316. EXPORT_SYMBOL(ath9k_hw_check_nav);
  1317. bool ath9k_hw_check_alive(struct ath_hw *ah)
  1318. {
  1319. int count = 50;
  1320. u32 reg, last_val;
  1321. if (AR_SREV_9300(ah))
  1322. return !ath9k_hw_detect_mac_hang(ah);
  1323. if (AR_SREV_9285_12_OR_LATER(ah))
  1324. return true;
  1325. last_val = REG_READ(ah, AR_OBS_BUS_1);
  1326. do {
  1327. reg = REG_READ(ah, AR_OBS_BUS_1);
  1328. if (reg != last_val)
  1329. return true;
  1330. udelay(1);
  1331. last_val = reg;
  1332. if ((reg & 0x7E7FFFEF) == 0x00702400)
  1333. continue;
  1334. switch (reg & 0x7E000B00) {
  1335. case 0x1E000000:
  1336. case 0x52000B00:
  1337. case 0x18000B00:
  1338. continue;
  1339. default:
  1340. return true;
  1341. }
  1342. } while (count-- > 0);
  1343. return false;
  1344. }
  1345. EXPORT_SYMBOL(ath9k_hw_check_alive);
  1346. static void ath9k_hw_init_mfp(struct ath_hw *ah)
  1347. {
  1348. /* Setup MFP options for CCMP */
  1349. if (AR_SREV_9280_20_OR_LATER(ah)) {
  1350. /* Mask Retry(b11), PwrMgt(b12), MoreData(b13) to 0 in mgmt
  1351. * frames when constructing CCMP AAD. */
  1352. REG_RMW_FIELD(ah, AR_AES_MUTE_MASK1, AR_AES_MUTE_MASK1_FC_MGMT,
  1353. 0xc7ff);
  1354. if (AR_SREV_9271(ah) || AR_DEVID_7010(ah))
  1355. ah->sw_mgmt_crypto_tx = true;
  1356. else
  1357. ah->sw_mgmt_crypto_tx = false;
  1358. ah->sw_mgmt_crypto_rx = false;
  1359. } else if (AR_SREV_9160_10_OR_LATER(ah)) {
  1360. /* Disable hardware crypto for management frames */
  1361. REG_CLR_BIT(ah, AR_PCU_MISC_MODE2,
  1362. AR_PCU_MISC_MODE2_MGMT_CRYPTO_ENABLE);
  1363. REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
  1364. AR_PCU_MISC_MODE2_NO_CRYPTO_FOR_NON_DATA_PKT);
  1365. ah->sw_mgmt_crypto_tx = true;
  1366. ah->sw_mgmt_crypto_rx = true;
  1367. } else {
  1368. ah->sw_mgmt_crypto_tx = true;
  1369. ah->sw_mgmt_crypto_rx = true;
  1370. }
  1371. }
  1372. static void ath9k_hw_reset_opmode(struct ath_hw *ah,
  1373. u32 macStaId1, u32 saveDefAntenna)
  1374. {
  1375. struct ath_common *common = ath9k_hw_common(ah);
  1376. ENABLE_REGWRITE_BUFFER(ah);
  1377. REG_RMW(ah, AR_STA_ID1, macStaId1
  1378. | AR_STA_ID1_RTS_USE_DEF
  1379. | ah->sta_id1_defaults,
  1380. ~AR_STA_ID1_SADH_MASK);
  1381. ath_hw_setbssidmask(common);
  1382. REG_WRITE(ah, AR_DEF_ANTENNA, saveDefAntenna);
  1383. ath9k_hw_write_associd(ah);
  1384. REG_WRITE(ah, AR_ISR, ~0);
  1385. REG_WRITE(ah, AR_RSSI_THR, INIT_RSSI_THR);
  1386. REGWRITE_BUFFER_FLUSH(ah);
  1387. ath9k_hw_set_operating_mode(ah, ah->opmode);
  1388. }
  1389. static void ath9k_hw_init_queues(struct ath_hw *ah)
  1390. {
  1391. int i;
  1392. ENABLE_REGWRITE_BUFFER(ah);
  1393. for (i = 0; i < AR_NUM_DCU; i++)
  1394. REG_WRITE(ah, AR_DQCUMASK(i), 1 << i);
  1395. REGWRITE_BUFFER_FLUSH(ah);
  1396. ah->intr_txqs = 0;
  1397. for (i = 0; i < ATH9K_NUM_TX_QUEUES; i++)
  1398. ath9k_hw_resettxqueue(ah, i);
  1399. }
  1400. /*
  1401. * For big endian systems turn on swapping for descriptors
  1402. */
  1403. static void ath9k_hw_init_desc(struct ath_hw *ah)
  1404. {
  1405. struct ath_common *common = ath9k_hw_common(ah);
  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, RESET, "CFG Byte Swap Set 0x%x\n",
  1411. mask);
  1412. } else {
  1413. mask = INIT_CONFIG_STATUS | AR_CFG_SWRB | AR_CFG_SWTB;
  1414. REG_WRITE(ah, AR_CFG, mask);
  1415. ath_dbg(common, RESET, "Setting CFG 0x%x\n",
  1416. REG_READ(ah, AR_CFG));
  1417. }
  1418. } else {
  1419. if (common->bus_ops->ath_bus_type == ATH_USB) {
  1420. /* Configure AR9271 target WLAN */
  1421. if (AR_SREV_9271(ah))
  1422. REG_WRITE(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB);
  1423. else
  1424. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1425. }
  1426. #ifdef __BIG_ENDIAN
  1427. else if (AR_SREV_9330(ah) || AR_SREV_9340(ah) ||
  1428. AR_SREV_9550(ah) || AR_SREV_9531(ah) ||
  1429. AR_SREV_9561(ah))
  1430. REG_RMW(ah, AR_CFG, AR_CFG_SWRB | AR_CFG_SWTB, 0);
  1431. else
  1432. REG_WRITE(ah, AR_CFG, AR_CFG_SWTD | AR_CFG_SWRD);
  1433. #endif
  1434. }
  1435. }
  1436. /*
  1437. * Fast channel change:
  1438. * (Change synthesizer based on channel freq without resetting chip)
  1439. */
  1440. static int ath9k_hw_do_fastcc(struct ath_hw *ah, struct ath9k_channel *chan)
  1441. {
  1442. struct ath_common *common = ath9k_hw_common(ah);
  1443. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1444. int ret;
  1445. if (AR_SREV_9280(ah) && common->bus_ops->ath_bus_type == ATH_PCI)
  1446. goto fail;
  1447. if (ah->chip_fullsleep)
  1448. goto fail;
  1449. if (!ah->curchan)
  1450. goto fail;
  1451. if (chan->channel == ah->curchan->channel)
  1452. goto fail;
  1453. if ((ah->curchan->channelFlags | chan->channelFlags) &
  1454. (CHANNEL_HALF | CHANNEL_QUARTER))
  1455. goto fail;
  1456. /*
  1457. * If cross-band fcc is not supoprted, bail out if channelFlags differ.
  1458. */
  1459. if (!(pCap->hw_caps & ATH9K_HW_CAP_FCC_BAND_SWITCH) &&
  1460. ((chan->channelFlags ^ ah->curchan->channelFlags) & ~CHANNEL_HT))
  1461. goto fail;
  1462. if (!ath9k_hw_check_alive(ah))
  1463. goto fail;
  1464. /*
  1465. * For AR9462, make sure that calibration data for
  1466. * re-using are present.
  1467. */
  1468. if (AR_SREV_9462(ah) && (ah->caldata &&
  1469. (!test_bit(TXIQCAL_DONE, &ah->caldata->cal_flags) ||
  1470. !test_bit(TXCLCAL_DONE, &ah->caldata->cal_flags) ||
  1471. !test_bit(RTT_DONE, &ah->caldata->cal_flags))))
  1472. goto fail;
  1473. ath_dbg(common, RESET, "FastChannelChange for %d -> %d\n",
  1474. ah->curchan->channel, chan->channel);
  1475. ret = ath9k_hw_channel_change(ah, chan);
  1476. if (!ret)
  1477. goto fail;
  1478. if (ath9k_hw_mci_is_enabled(ah))
  1479. ar9003_mci_2g5g_switch(ah, false);
  1480. ath9k_hw_loadnf(ah, ah->curchan);
  1481. ath9k_hw_start_nfcal(ah, true);
  1482. if (AR_SREV_9271(ah))
  1483. ar9002_hw_load_ani_reg(ah, chan);
  1484. return 0;
  1485. fail:
  1486. return -EINVAL;
  1487. }
  1488. u32 ath9k_hw_get_tsf_offset(struct timespec *last, struct timespec *cur)
  1489. {
  1490. struct timespec ts;
  1491. s64 usec;
  1492. if (!cur) {
  1493. getrawmonotonic(&ts);
  1494. cur = &ts;
  1495. }
  1496. usec = cur->tv_sec * 1000000ULL + cur->tv_nsec / 1000;
  1497. usec -= last->tv_sec * 1000000ULL + last->tv_nsec / 1000;
  1498. return (u32) usec;
  1499. }
  1500. EXPORT_SYMBOL(ath9k_hw_get_tsf_offset);
  1501. int ath9k_hw_reset(struct ath_hw *ah, struct ath9k_channel *chan,
  1502. struct ath9k_hw_cal_data *caldata, bool fastcc)
  1503. {
  1504. struct ath_common *common = ath9k_hw_common(ah);
  1505. u32 saveLedState;
  1506. u32 saveDefAntenna;
  1507. u32 macStaId1;
  1508. struct timespec tsf_ts;
  1509. u32 tsf_offset;
  1510. u64 tsf = 0;
  1511. int r;
  1512. bool start_mci_reset = false;
  1513. bool save_fullsleep = ah->chip_fullsleep;
  1514. if (ath9k_hw_mci_is_enabled(ah)) {
  1515. start_mci_reset = ar9003_mci_start_reset(ah, chan);
  1516. if (start_mci_reset)
  1517. return 0;
  1518. }
  1519. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  1520. return -EIO;
  1521. if (ah->curchan && !ah->chip_fullsleep)
  1522. ath9k_hw_getnf(ah, ah->curchan);
  1523. ah->caldata = caldata;
  1524. if (caldata && (chan->channel != caldata->channel ||
  1525. chan->channelFlags != caldata->channelFlags)) {
  1526. /* Operating channel changed, reset channel calibration data */
  1527. memset(caldata, 0, sizeof(*caldata));
  1528. ath9k_init_nfcal_hist_buffer(ah, chan);
  1529. } else if (caldata) {
  1530. clear_bit(PAPRD_PACKET_SENT, &caldata->cal_flags);
  1531. }
  1532. ah->noise = ath9k_hw_getchan_noise(ah, chan, chan->noisefloor);
  1533. if (fastcc) {
  1534. r = ath9k_hw_do_fastcc(ah, chan);
  1535. if (!r)
  1536. return r;
  1537. }
  1538. if (ath9k_hw_mci_is_enabled(ah))
  1539. ar9003_mci_stop_bt(ah, save_fullsleep);
  1540. saveDefAntenna = REG_READ(ah, AR_DEF_ANTENNA);
  1541. if (saveDefAntenna == 0)
  1542. saveDefAntenna = 1;
  1543. macStaId1 = REG_READ(ah, AR_STA_ID1) & AR_STA_ID1_BASE_RATE_11B;
  1544. /* Save TSF before chip reset, a cold reset clears it */
  1545. getrawmonotonic(&tsf_ts);
  1546. tsf = ath9k_hw_gettsf64(ah);
  1547. saveLedState = REG_READ(ah, AR_CFG_LED) &
  1548. (AR_CFG_LED_ASSOC_CTL | AR_CFG_LED_MODE_SEL |
  1549. AR_CFG_LED_BLINK_THRESH_SEL | AR_CFG_LED_BLINK_SLOW);
  1550. ath9k_hw_mark_phy_inactive(ah);
  1551. ah->paprd_table_write_done = false;
  1552. /* Only required on the first reset */
  1553. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1554. REG_WRITE(ah,
  1555. AR9271_RESET_POWER_DOWN_CONTROL,
  1556. AR9271_RADIO_RF_RST);
  1557. udelay(50);
  1558. }
  1559. if (!ath9k_hw_chip_reset(ah, chan)) {
  1560. ath_err(common, "Chip reset failed\n");
  1561. return -EINVAL;
  1562. }
  1563. /* Only required on the first reset */
  1564. if (AR_SREV_9271(ah) && ah->htc_reset_init) {
  1565. ah->htc_reset_init = false;
  1566. REG_WRITE(ah,
  1567. AR9271_RESET_POWER_DOWN_CONTROL,
  1568. AR9271_GATE_MAC_CTL);
  1569. udelay(50);
  1570. }
  1571. /* Restore TSF */
  1572. tsf_offset = ath9k_hw_get_tsf_offset(&tsf_ts, NULL);
  1573. ath9k_hw_settsf64(ah, tsf + tsf_offset);
  1574. if (AR_SREV_9280_20_OR_LATER(ah))
  1575. REG_SET_BIT(ah, AR_GPIO_INPUT_EN_VAL, AR_GPIO_JTAG_DISABLE);
  1576. if (!AR_SREV_9300_20_OR_LATER(ah))
  1577. ar9002_hw_enable_async_fifo(ah);
  1578. r = ath9k_hw_process_ini(ah, chan);
  1579. if (r)
  1580. return r;
  1581. ath9k_hw_set_rfmode(ah, chan);
  1582. if (ath9k_hw_mci_is_enabled(ah))
  1583. ar9003_mci_reset(ah, false, IS_CHAN_2GHZ(chan), save_fullsleep);
  1584. /*
  1585. * Some AR91xx SoC devices frequently fail to accept TSF writes
  1586. * right after the chip reset. When that happens, write a new
  1587. * value after the initvals have been applied.
  1588. */
  1589. if (AR_SREV_9100(ah) && (ath9k_hw_gettsf64(ah) < tsf)) {
  1590. tsf_offset = ath9k_hw_get_tsf_offset(&tsf_ts, NULL);
  1591. ath9k_hw_settsf64(ah, tsf + tsf_offset);
  1592. }
  1593. ath9k_hw_init_mfp(ah);
  1594. ath9k_hw_set_delta_slope(ah, chan);
  1595. ath9k_hw_spur_mitigate_freq(ah, chan);
  1596. ah->eep_ops->set_board_values(ah, chan);
  1597. ath9k_hw_reset_opmode(ah, macStaId1, saveDefAntenna);
  1598. r = ath9k_hw_rf_set_freq(ah, chan);
  1599. if (r)
  1600. return r;
  1601. ath9k_hw_set_clockrate(ah);
  1602. ath9k_hw_init_queues(ah);
  1603. ath9k_hw_init_interrupt_masks(ah, ah->opmode);
  1604. ath9k_hw_ani_cache_ini_regs(ah);
  1605. ath9k_hw_init_qos(ah);
  1606. if (ah->caps.hw_caps & ATH9K_HW_CAP_RFSILENT)
  1607. ath9k_hw_gpio_request_in(ah, ah->rfkill_gpio, "ath9k-rfkill");
  1608. ath9k_hw_init_global_settings(ah);
  1609. if (AR_SREV_9287(ah) && AR_SREV_9287_13_OR_LATER(ah)) {
  1610. REG_SET_BIT(ah, AR_MAC_PCU_LOGIC_ANALYZER,
  1611. AR_MAC_PCU_LOGIC_ANALYZER_DISBUG20768);
  1612. REG_RMW_FIELD(ah, AR_AHB_MODE, AR_AHB_CUSTOM_BURST_EN,
  1613. AR_AHB_CUSTOM_BURST_ASYNC_FIFO_VAL);
  1614. REG_SET_BIT(ah, AR_PCU_MISC_MODE2,
  1615. AR_PCU_MISC_MODE2_ENABLE_AGGWEP);
  1616. }
  1617. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PRESERVE_SEQNUM);
  1618. ath9k_hw_set_dma(ah);
  1619. if (!ath9k_hw_mci_is_enabled(ah))
  1620. REG_WRITE(ah, AR_OBS, 8);
  1621. ENABLE_REG_RMW_BUFFER(ah);
  1622. if (ah->config.rx_intr_mitigation) {
  1623. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_LAST, ah->config.rimt_last);
  1624. REG_RMW_FIELD(ah, AR_RIMT, AR_RIMT_FIRST, ah->config.rimt_first);
  1625. }
  1626. if (ah->config.tx_intr_mitigation) {
  1627. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_LAST, 300);
  1628. REG_RMW_FIELD(ah, AR_TIMT, AR_TIMT_FIRST, 750);
  1629. }
  1630. REG_RMW_BUFFER_FLUSH(ah);
  1631. ath9k_hw_init_bb(ah, chan);
  1632. if (caldata) {
  1633. clear_bit(TXIQCAL_DONE, &caldata->cal_flags);
  1634. clear_bit(TXCLCAL_DONE, &caldata->cal_flags);
  1635. }
  1636. if (!ath9k_hw_init_cal(ah, chan))
  1637. return -EIO;
  1638. if (ath9k_hw_mci_is_enabled(ah) && ar9003_mci_end_reset(ah, chan, caldata))
  1639. return -EIO;
  1640. ENABLE_REGWRITE_BUFFER(ah);
  1641. ath9k_hw_restore_chainmask(ah);
  1642. REG_WRITE(ah, AR_CFG_LED, saveLedState | AR_CFG_SCLK_32KHZ);
  1643. REGWRITE_BUFFER_FLUSH(ah);
  1644. ath9k_hw_gen_timer_start_tsf2(ah);
  1645. ath9k_hw_init_desc(ah);
  1646. if (ath9k_hw_btcoex_is_enabled(ah))
  1647. ath9k_hw_btcoex_enable(ah);
  1648. if (ath9k_hw_mci_is_enabled(ah))
  1649. ar9003_mci_check_bt(ah);
  1650. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1651. ath9k_hw_loadnf(ah, chan);
  1652. ath9k_hw_start_nfcal(ah, true);
  1653. }
  1654. if (AR_SREV_9300_20_OR_LATER(ah))
  1655. ar9003_hw_bb_watchdog_config(ah);
  1656. if (ah->config.hw_hang_checks & HW_PHYRESTART_CLC_WAR)
  1657. ar9003_hw_disable_phy_restart(ah);
  1658. ath9k_hw_apply_gpio_override(ah);
  1659. if (AR_SREV_9565(ah) && common->bt_ant_diversity)
  1660. REG_SET_BIT(ah, AR_BTCOEX_WL_LNADIV, AR_BTCOEX_WL_LNADIV_FORCE_ON);
  1661. if (ah->hw->conf.radar_enabled) {
  1662. /* set HW specific DFS configuration */
  1663. ah->radar_conf.ext_channel = IS_CHAN_HT40(chan);
  1664. ath9k_hw_set_radar_params(ah);
  1665. }
  1666. return 0;
  1667. }
  1668. EXPORT_SYMBOL(ath9k_hw_reset);
  1669. /******************************/
  1670. /* Power Management (Chipset) */
  1671. /******************************/
  1672. /*
  1673. * Notify Power Mgt is disabled in self-generated frames.
  1674. * If requested, force chip to sleep.
  1675. */
  1676. static void ath9k_set_power_sleep(struct ath_hw *ah)
  1677. {
  1678. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1679. if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
  1680. REG_CLR_BIT(ah, AR_TIMER_MODE, 0xff);
  1681. REG_CLR_BIT(ah, AR_NDP2_TIMER_MODE, 0xff);
  1682. REG_CLR_BIT(ah, AR_SLP32_INC, 0xfffff);
  1683. /* xxx Required for WLAN only case ? */
  1684. REG_WRITE(ah, AR_MCI_INTERRUPT_RX_MSG_EN, 0);
  1685. udelay(100);
  1686. }
  1687. /*
  1688. * Clear the RTC force wake bit to allow the
  1689. * mac to go to sleep.
  1690. */
  1691. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
  1692. if (ath9k_hw_mci_is_enabled(ah))
  1693. udelay(100);
  1694. if (!AR_SREV_9100(ah) && !AR_SREV_9300_20_OR_LATER(ah))
  1695. REG_WRITE(ah, AR_RC, AR_RC_AHB | AR_RC_HOSTIF);
  1696. /* Shutdown chip. Active low */
  1697. if (!AR_SREV_5416(ah) && !AR_SREV_9271(ah)) {
  1698. REG_CLR_BIT(ah, AR_RTC_RESET, AR_RTC_RESET_EN);
  1699. udelay(2);
  1700. }
  1701. /* Clear Bit 14 of AR_WA after putting chip into Full Sleep mode. */
  1702. if (AR_SREV_9300_20_OR_LATER(ah))
  1703. REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
  1704. }
  1705. /*
  1706. * Notify Power Management is enabled in self-generating
  1707. * frames. If request, set power mode of chip to
  1708. * auto/normal. Duration in units of 128us (1/8 TU).
  1709. */
  1710. static void ath9k_set_power_network_sleep(struct ath_hw *ah)
  1711. {
  1712. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1713. REG_SET_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1714. if (!(pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)) {
  1715. /* Set WakeOnInterrupt bit; clear ForceWake bit */
  1716. REG_WRITE(ah, AR_RTC_FORCE_WAKE,
  1717. AR_RTC_FORCE_WAKE_ON_INT);
  1718. } else {
  1719. /* When chip goes into network sleep, it could be waken
  1720. * up by MCI_INT interrupt caused by BT's HW messages
  1721. * (LNA_xxx, CONT_xxx) which chould be in a very fast
  1722. * rate (~100us). This will cause chip to leave and
  1723. * re-enter network sleep mode frequently, which in
  1724. * consequence will have WLAN MCI HW to generate lots of
  1725. * SYS_WAKING and SYS_SLEEPING messages which will make
  1726. * BT CPU to busy to process.
  1727. */
  1728. if (ath9k_hw_mci_is_enabled(ah))
  1729. REG_CLR_BIT(ah, AR_MCI_INTERRUPT_RX_MSG_EN,
  1730. AR_MCI_INTERRUPT_RX_HW_MSG_MASK);
  1731. /*
  1732. * Clear the RTC force wake bit to allow the
  1733. * mac to go to sleep.
  1734. */
  1735. REG_CLR_BIT(ah, AR_RTC_FORCE_WAKE, AR_RTC_FORCE_WAKE_EN);
  1736. if (ath9k_hw_mci_is_enabled(ah))
  1737. udelay(30);
  1738. }
  1739. /* Clear Bit 14 of AR_WA after putting chip into Net Sleep mode. */
  1740. if (AR_SREV_9300_20_OR_LATER(ah))
  1741. REG_WRITE(ah, AR_WA, ah->WARegVal & ~AR_WA_D3_L1_DISABLE);
  1742. }
  1743. static bool ath9k_hw_set_power_awake(struct ath_hw *ah)
  1744. {
  1745. u32 val;
  1746. int i;
  1747. /* Set Bits 14 and 17 of AR_WA before powering on the chip. */
  1748. if (AR_SREV_9300_20_OR_LATER(ah)) {
  1749. REG_WRITE(ah, AR_WA, ah->WARegVal);
  1750. udelay(10);
  1751. }
  1752. if ((REG_READ(ah, AR_RTC_STATUS) &
  1753. AR_RTC_STATUS_M) == AR_RTC_STATUS_SHUTDOWN) {
  1754. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_POWER_ON)) {
  1755. return false;
  1756. }
  1757. if (!AR_SREV_9300_20_OR_LATER(ah))
  1758. ath9k_hw_init_pll(ah, NULL);
  1759. }
  1760. if (AR_SREV_9100(ah))
  1761. REG_SET_BIT(ah, AR_RTC_RESET,
  1762. AR_RTC_RESET_EN);
  1763. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1764. AR_RTC_FORCE_WAKE_EN);
  1765. if (AR_SREV_9100(ah))
  1766. mdelay(10);
  1767. else
  1768. udelay(50);
  1769. for (i = POWER_UP_TIME / 50; i > 0; i--) {
  1770. val = REG_READ(ah, AR_RTC_STATUS) & AR_RTC_STATUS_M;
  1771. if (val == AR_RTC_STATUS_ON)
  1772. break;
  1773. udelay(50);
  1774. REG_SET_BIT(ah, AR_RTC_FORCE_WAKE,
  1775. AR_RTC_FORCE_WAKE_EN);
  1776. }
  1777. if (i == 0) {
  1778. ath_err(ath9k_hw_common(ah),
  1779. "Failed to wakeup in %uus\n",
  1780. POWER_UP_TIME / 20);
  1781. return false;
  1782. }
  1783. if (ath9k_hw_mci_is_enabled(ah))
  1784. ar9003_mci_set_power_awake(ah);
  1785. REG_CLR_BIT(ah, AR_STA_ID1, AR_STA_ID1_PWR_SAV);
  1786. return true;
  1787. }
  1788. bool ath9k_hw_setpower(struct ath_hw *ah, enum ath9k_power_mode mode)
  1789. {
  1790. struct ath_common *common = ath9k_hw_common(ah);
  1791. int status = true;
  1792. static const char *modes[] = {
  1793. "AWAKE",
  1794. "FULL-SLEEP",
  1795. "NETWORK SLEEP",
  1796. "UNDEFINED"
  1797. };
  1798. if (ah->power_mode == mode)
  1799. return status;
  1800. ath_dbg(common, RESET, "%s -> %s\n",
  1801. modes[ah->power_mode], modes[mode]);
  1802. switch (mode) {
  1803. case ATH9K_PM_AWAKE:
  1804. status = ath9k_hw_set_power_awake(ah);
  1805. break;
  1806. case ATH9K_PM_FULL_SLEEP:
  1807. if (ath9k_hw_mci_is_enabled(ah))
  1808. ar9003_mci_set_full_sleep(ah);
  1809. ath9k_set_power_sleep(ah);
  1810. ah->chip_fullsleep = true;
  1811. break;
  1812. case ATH9K_PM_NETWORK_SLEEP:
  1813. ath9k_set_power_network_sleep(ah);
  1814. break;
  1815. default:
  1816. ath_err(common, "Unknown power mode %u\n", mode);
  1817. return false;
  1818. }
  1819. ah->power_mode = mode;
  1820. /*
  1821. * XXX: If this warning never comes up after a while then
  1822. * simply keep the ATH_DBG_WARN_ON_ONCE() but make
  1823. * ath9k_hw_setpower() return type void.
  1824. */
  1825. if (!(ah->ah_flags & AH_UNPLUGGED))
  1826. ATH_DBG_WARN_ON_ONCE(!status);
  1827. return status;
  1828. }
  1829. EXPORT_SYMBOL(ath9k_hw_setpower);
  1830. /*******************/
  1831. /* Beacon Handling */
  1832. /*******************/
  1833. void ath9k_hw_beaconinit(struct ath_hw *ah, u32 next_beacon, u32 beacon_period)
  1834. {
  1835. int flags = 0;
  1836. ENABLE_REGWRITE_BUFFER(ah);
  1837. switch (ah->opmode) {
  1838. case NL80211_IFTYPE_ADHOC:
  1839. REG_SET_BIT(ah, AR_TXCFG,
  1840. AR_TXCFG_ADHOC_BEACON_ATIM_TX_POLICY);
  1841. case NL80211_IFTYPE_MESH_POINT:
  1842. case NL80211_IFTYPE_AP:
  1843. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, next_beacon);
  1844. REG_WRITE(ah, AR_NEXT_DMA_BEACON_ALERT, next_beacon -
  1845. TU_TO_USEC(ah->config.dma_beacon_response_time));
  1846. REG_WRITE(ah, AR_NEXT_SWBA, next_beacon -
  1847. TU_TO_USEC(ah->config.sw_beacon_response_time));
  1848. flags |=
  1849. AR_TBTT_TIMER_EN | AR_DBA_TIMER_EN | AR_SWBA_TIMER_EN;
  1850. break;
  1851. default:
  1852. ath_dbg(ath9k_hw_common(ah), BEACON,
  1853. "%s: unsupported opmode: %d\n", __func__, ah->opmode);
  1854. return;
  1855. break;
  1856. }
  1857. REG_WRITE(ah, AR_BEACON_PERIOD, beacon_period);
  1858. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, beacon_period);
  1859. REG_WRITE(ah, AR_SWBA_PERIOD, beacon_period);
  1860. REGWRITE_BUFFER_FLUSH(ah);
  1861. REG_SET_BIT(ah, AR_TIMER_MODE, flags);
  1862. }
  1863. EXPORT_SYMBOL(ath9k_hw_beaconinit);
  1864. void ath9k_hw_set_sta_beacon_timers(struct ath_hw *ah,
  1865. const struct ath9k_beacon_state *bs)
  1866. {
  1867. u32 nextTbtt, beaconintval, dtimperiod, beacontimeout;
  1868. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1869. struct ath_common *common = ath9k_hw_common(ah);
  1870. ENABLE_REGWRITE_BUFFER(ah);
  1871. REG_WRITE(ah, AR_NEXT_TBTT_TIMER, bs->bs_nexttbtt);
  1872. REG_WRITE(ah, AR_BEACON_PERIOD, bs->bs_intval);
  1873. REG_WRITE(ah, AR_DMA_BEACON_PERIOD, bs->bs_intval);
  1874. REGWRITE_BUFFER_FLUSH(ah);
  1875. REG_RMW_FIELD(ah, AR_RSSI_THR,
  1876. AR_RSSI_THR_BM_THR, bs->bs_bmissthreshold);
  1877. beaconintval = bs->bs_intval;
  1878. if (bs->bs_sleepduration > beaconintval)
  1879. beaconintval = bs->bs_sleepduration;
  1880. dtimperiod = bs->bs_dtimperiod;
  1881. if (bs->bs_sleepduration > dtimperiod)
  1882. dtimperiod = bs->bs_sleepduration;
  1883. if (beaconintval == dtimperiod)
  1884. nextTbtt = bs->bs_nextdtim;
  1885. else
  1886. nextTbtt = bs->bs_nexttbtt;
  1887. ath_dbg(common, BEACON, "next DTIM %u\n", bs->bs_nextdtim);
  1888. ath_dbg(common, BEACON, "next beacon %u\n", nextTbtt);
  1889. ath_dbg(common, BEACON, "beacon period %u\n", beaconintval);
  1890. ath_dbg(common, BEACON, "DTIM period %u\n", dtimperiod);
  1891. ENABLE_REGWRITE_BUFFER(ah);
  1892. REG_WRITE(ah, AR_NEXT_DTIM, bs->bs_nextdtim - SLEEP_SLOP);
  1893. REG_WRITE(ah, AR_NEXT_TIM, nextTbtt - SLEEP_SLOP);
  1894. REG_WRITE(ah, AR_SLEEP1,
  1895. SM((CAB_TIMEOUT_VAL << 3), AR_SLEEP1_CAB_TIMEOUT)
  1896. | AR_SLEEP1_ASSUME_DTIM);
  1897. if (pCap->hw_caps & ATH9K_HW_CAP_AUTOSLEEP)
  1898. beacontimeout = (BEACON_TIMEOUT_VAL << 3);
  1899. else
  1900. beacontimeout = MIN_BEACON_TIMEOUT_VAL;
  1901. REG_WRITE(ah, AR_SLEEP2,
  1902. SM(beacontimeout, AR_SLEEP2_BEACON_TIMEOUT));
  1903. REG_WRITE(ah, AR_TIM_PERIOD, beaconintval);
  1904. REG_WRITE(ah, AR_DTIM_PERIOD, dtimperiod);
  1905. REGWRITE_BUFFER_FLUSH(ah);
  1906. REG_SET_BIT(ah, AR_TIMER_MODE,
  1907. AR_TBTT_TIMER_EN | AR_TIM_TIMER_EN |
  1908. AR_DTIM_TIMER_EN);
  1909. /* TSF Out of Range Threshold */
  1910. REG_WRITE(ah, AR_TSFOOR_THRESHOLD, bs->bs_tsfoor_threshold);
  1911. }
  1912. EXPORT_SYMBOL(ath9k_hw_set_sta_beacon_timers);
  1913. /*******************/
  1914. /* HW Capabilities */
  1915. /*******************/
  1916. static u8 fixup_chainmask(u8 chip_chainmask, u8 eeprom_chainmask)
  1917. {
  1918. eeprom_chainmask &= chip_chainmask;
  1919. if (eeprom_chainmask)
  1920. return eeprom_chainmask;
  1921. else
  1922. return chip_chainmask;
  1923. }
  1924. /**
  1925. * ath9k_hw_dfs_tested - checks if DFS has been tested with used chipset
  1926. * @ah: the atheros hardware data structure
  1927. *
  1928. * We enable DFS support upstream on chipsets which have passed a series
  1929. * of tests. The testing requirements are going to be documented. Desired
  1930. * test requirements are documented at:
  1931. *
  1932. * http://wireless.kernel.org/en/users/Drivers/ath9k/dfs
  1933. *
  1934. * Once a new chipset gets properly tested an individual commit can be used
  1935. * to document the testing for DFS for that chipset.
  1936. */
  1937. static bool ath9k_hw_dfs_tested(struct ath_hw *ah)
  1938. {
  1939. switch (ah->hw_version.macVersion) {
  1940. /* for temporary testing DFS with 9280 */
  1941. case AR_SREV_VERSION_9280:
  1942. /* AR9580 will likely be our first target to get testing on */
  1943. case AR_SREV_VERSION_9580:
  1944. return true;
  1945. default:
  1946. return false;
  1947. }
  1948. }
  1949. static void ath9k_gpio_cap_init(struct ath_hw *ah)
  1950. {
  1951. struct ath9k_hw_capabilities *pCap = &ah->caps;
  1952. if (AR_SREV_9271(ah)) {
  1953. pCap->num_gpio_pins = AR9271_NUM_GPIO;
  1954. pCap->gpio_mask = AR9271_GPIO_MASK;
  1955. } else if (AR_DEVID_7010(ah)) {
  1956. pCap->num_gpio_pins = AR7010_NUM_GPIO;
  1957. pCap->gpio_mask = AR7010_GPIO_MASK;
  1958. } else if (AR_SREV_9287(ah)) {
  1959. pCap->num_gpio_pins = AR9287_NUM_GPIO;
  1960. pCap->gpio_mask = AR9287_GPIO_MASK;
  1961. } else if (AR_SREV_9285(ah)) {
  1962. pCap->num_gpio_pins = AR9285_NUM_GPIO;
  1963. pCap->gpio_mask = AR9285_GPIO_MASK;
  1964. } else if (AR_SREV_9280(ah)) {
  1965. pCap->num_gpio_pins = AR9280_NUM_GPIO;
  1966. pCap->gpio_mask = AR9280_GPIO_MASK;
  1967. } else if (AR_SREV_9300(ah)) {
  1968. pCap->num_gpio_pins = AR9300_NUM_GPIO;
  1969. pCap->gpio_mask = AR9300_GPIO_MASK;
  1970. } else if (AR_SREV_9330(ah)) {
  1971. pCap->num_gpio_pins = AR9330_NUM_GPIO;
  1972. pCap->gpio_mask = AR9330_GPIO_MASK;
  1973. } else if (AR_SREV_9340(ah)) {
  1974. pCap->num_gpio_pins = AR9340_NUM_GPIO;
  1975. pCap->gpio_mask = AR9340_GPIO_MASK;
  1976. } else if (AR_SREV_9462(ah)) {
  1977. pCap->num_gpio_pins = AR9462_NUM_GPIO;
  1978. pCap->gpio_mask = AR9462_GPIO_MASK;
  1979. } else if (AR_SREV_9485(ah)) {
  1980. pCap->num_gpio_pins = AR9485_NUM_GPIO;
  1981. pCap->gpio_mask = AR9485_GPIO_MASK;
  1982. } else if (AR_SREV_9531(ah)) {
  1983. pCap->num_gpio_pins = AR9531_NUM_GPIO;
  1984. pCap->gpio_mask = AR9531_GPIO_MASK;
  1985. } else if (AR_SREV_9550(ah)) {
  1986. pCap->num_gpio_pins = AR9550_NUM_GPIO;
  1987. pCap->gpio_mask = AR9550_GPIO_MASK;
  1988. } else if (AR_SREV_9561(ah)) {
  1989. pCap->num_gpio_pins = AR9561_NUM_GPIO;
  1990. pCap->gpio_mask = AR9561_GPIO_MASK;
  1991. } else if (AR_SREV_9565(ah)) {
  1992. pCap->num_gpio_pins = AR9565_NUM_GPIO;
  1993. pCap->gpio_mask = AR9565_GPIO_MASK;
  1994. } else if (AR_SREV_9580(ah)) {
  1995. pCap->num_gpio_pins = AR9580_NUM_GPIO;
  1996. pCap->gpio_mask = AR9580_GPIO_MASK;
  1997. } else {
  1998. pCap->num_gpio_pins = AR_NUM_GPIO;
  1999. pCap->gpio_mask = AR_GPIO_MASK;
  2000. }
  2001. }
  2002. int ath9k_hw_fill_cap_info(struct ath_hw *ah)
  2003. {
  2004. struct ath9k_hw_capabilities *pCap = &ah->caps;
  2005. struct ath_regulatory *regulatory = ath9k_hw_regulatory(ah);
  2006. struct ath_common *common = ath9k_hw_common(ah);
  2007. u16 eeval;
  2008. u8 ant_div_ctl1, tx_chainmask, rx_chainmask;
  2009. eeval = ah->eep_ops->get_eeprom(ah, EEP_REG_0);
  2010. regulatory->current_rd = eeval;
  2011. if (ah->opmode != NL80211_IFTYPE_AP &&
  2012. ah->hw_version.subvendorid == AR_SUBVENDOR_ID_NEW_A) {
  2013. if (regulatory->current_rd == 0x64 ||
  2014. regulatory->current_rd == 0x65)
  2015. regulatory->current_rd += 5;
  2016. else if (regulatory->current_rd == 0x41)
  2017. regulatory->current_rd = 0x43;
  2018. ath_dbg(common, REGULATORY, "regdomain mapped to 0x%x\n",
  2019. regulatory->current_rd);
  2020. }
  2021. eeval = ah->eep_ops->get_eeprom(ah, EEP_OP_MODE);
  2022. if (eeval & AR5416_OPFLAGS_11A) {
  2023. if (ah->disable_5ghz)
  2024. ath_warn(common, "disabling 5GHz band\n");
  2025. else
  2026. pCap->hw_caps |= ATH9K_HW_CAP_5GHZ;
  2027. }
  2028. if (eeval & AR5416_OPFLAGS_11G) {
  2029. if (ah->disable_2ghz)
  2030. ath_warn(common, "disabling 2GHz band\n");
  2031. else
  2032. pCap->hw_caps |= ATH9K_HW_CAP_2GHZ;
  2033. }
  2034. if ((pCap->hw_caps & (ATH9K_HW_CAP_2GHZ | ATH9K_HW_CAP_5GHZ)) == 0) {
  2035. ath_err(common, "both bands are disabled\n");
  2036. return -EINVAL;
  2037. }
  2038. ath9k_gpio_cap_init(ah);
  2039. if (AR_SREV_9485(ah) ||
  2040. AR_SREV_9285(ah) ||
  2041. AR_SREV_9330(ah) ||
  2042. AR_SREV_9565(ah))
  2043. pCap->chip_chainmask = 1;
  2044. else if (!AR_SREV_9280_20_OR_LATER(ah))
  2045. pCap->chip_chainmask = 7;
  2046. else if (!AR_SREV_9300_20_OR_LATER(ah) ||
  2047. AR_SREV_9340(ah) ||
  2048. AR_SREV_9462(ah) ||
  2049. AR_SREV_9531(ah))
  2050. pCap->chip_chainmask = 3;
  2051. else
  2052. pCap->chip_chainmask = 7;
  2053. pCap->tx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_TX_MASK);
  2054. /*
  2055. * For AR9271 we will temporarilly uses the rx chainmax as read from
  2056. * the EEPROM.
  2057. */
  2058. if ((ah->hw_version.devid == AR5416_DEVID_PCI) &&
  2059. !(eeval & AR5416_OPFLAGS_11A) &&
  2060. !(AR_SREV_9271(ah)))
  2061. /* CB71: GPIO 0 is pulled down to indicate 3 rx chains */
  2062. pCap->rx_chainmask = ath9k_hw_gpio_get(ah, 0) ? 0x5 : 0x7;
  2063. else if (AR_SREV_9100(ah))
  2064. pCap->rx_chainmask = 0x7;
  2065. else
  2066. /* Use rx_chainmask from EEPROM. */
  2067. pCap->rx_chainmask = ah->eep_ops->get_eeprom(ah, EEP_RX_MASK);
  2068. pCap->tx_chainmask = fixup_chainmask(pCap->chip_chainmask, pCap->tx_chainmask);
  2069. pCap->rx_chainmask = fixup_chainmask(pCap->chip_chainmask, pCap->rx_chainmask);
  2070. ah->txchainmask = pCap->tx_chainmask;
  2071. ah->rxchainmask = pCap->rx_chainmask;
  2072. ah->misc_mode |= AR_PCU_MIC_NEW_LOC_ENA;
  2073. /* enable key search for every frame in an aggregate */
  2074. if (AR_SREV_9300_20_OR_LATER(ah))
  2075. ah->misc_mode |= AR_PCU_ALWAYS_PERFORM_KEYSEARCH;
  2076. common->crypt_caps |= ATH_CRYPT_CAP_CIPHER_AESCCM;
  2077. if (ah->hw_version.devid != AR2427_DEVID_PCIE)
  2078. pCap->hw_caps |= ATH9K_HW_CAP_HT;
  2079. else
  2080. pCap->hw_caps &= ~ATH9K_HW_CAP_HT;
  2081. if (AR_SREV_9160_10_OR_LATER(ah) || AR_SREV_9100(ah))
  2082. pCap->rts_aggr_limit = ATH_AMPDU_LIMIT_MAX;
  2083. else
  2084. pCap->rts_aggr_limit = (8 * 1024);
  2085. #ifdef CONFIG_ATH9K_RFKILL
  2086. ah->rfsilent = ah->eep_ops->get_eeprom(ah, EEP_RF_SILENT);
  2087. if (ah->rfsilent & EEP_RFSILENT_ENABLED) {
  2088. ah->rfkill_gpio =
  2089. MS(ah->rfsilent, EEP_RFSILENT_GPIO_SEL);
  2090. ah->rfkill_polarity =
  2091. MS(ah->rfsilent, EEP_RFSILENT_POLARITY);
  2092. pCap->hw_caps |= ATH9K_HW_CAP_RFSILENT;
  2093. }
  2094. #endif
  2095. if (AR_SREV_9271(ah) || AR_SREV_9300_20_OR_LATER(ah))
  2096. pCap->hw_caps |= ATH9K_HW_CAP_AUTOSLEEP;
  2097. else
  2098. pCap->hw_caps &= ~ATH9K_HW_CAP_AUTOSLEEP;
  2099. if (AR_SREV_9280(ah) || AR_SREV_9285(ah))
  2100. pCap->hw_caps &= ~ATH9K_HW_CAP_4KB_SPLITTRANS;
  2101. else
  2102. pCap->hw_caps |= ATH9K_HW_CAP_4KB_SPLITTRANS;
  2103. if (AR_SREV_9300_20_OR_LATER(ah)) {
  2104. pCap->hw_caps |= ATH9K_HW_CAP_EDMA | ATH9K_HW_CAP_FASTCLOCK;
  2105. if (!AR_SREV_9330(ah) && !AR_SREV_9485(ah) &&
  2106. !AR_SREV_9561(ah) && !AR_SREV_9565(ah))
  2107. pCap->hw_caps |= ATH9K_HW_CAP_LDPC;
  2108. pCap->rx_hp_qdepth = ATH9K_HW_RX_HP_QDEPTH;
  2109. pCap->rx_lp_qdepth = ATH9K_HW_RX_LP_QDEPTH;
  2110. pCap->rx_status_len = sizeof(struct ar9003_rxs);
  2111. pCap->tx_desc_len = sizeof(struct ar9003_txc);
  2112. pCap->txs_len = sizeof(struct ar9003_txs);
  2113. } else {
  2114. pCap->tx_desc_len = sizeof(struct ath_desc);
  2115. if (AR_SREV_9280_20(ah))
  2116. pCap->hw_caps |= ATH9K_HW_CAP_FASTCLOCK;
  2117. }
  2118. if (AR_SREV_9300_20_OR_LATER(ah))
  2119. pCap->hw_caps |= ATH9K_HW_CAP_RAC_SUPPORTED;
  2120. if (AR_SREV_9561(ah))
  2121. ah->ent_mode = 0x3BDA000;
  2122. else if (AR_SREV_9300_20_OR_LATER(ah))
  2123. ah->ent_mode = REG_READ(ah, AR_ENT_OTP);
  2124. if (AR_SREV_9287_11_OR_LATER(ah) || AR_SREV_9271(ah))
  2125. pCap->hw_caps |= ATH9K_HW_CAP_SGI_20;
  2126. if (AR_SREV_9285(ah)) {
  2127. if (ah->eep_ops->get_eeprom(ah, EEP_MODAL_VER) >= 3) {
  2128. ant_div_ctl1 =
  2129. ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
  2130. if ((ant_div_ctl1 & 0x1) && ((ant_div_ctl1 >> 3) & 0x1)) {
  2131. pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
  2132. ath_info(common, "Enable LNA combining\n");
  2133. }
  2134. }
  2135. }
  2136. if (AR_SREV_9300_20_OR_LATER(ah)) {
  2137. if (ah->eep_ops->get_eeprom(ah, EEP_CHAIN_MASK_REDUCE))
  2138. pCap->hw_caps |= ATH9K_HW_CAP_APM;
  2139. }
  2140. if (AR_SREV_9330(ah) || AR_SREV_9485(ah) || AR_SREV_9565(ah)) {
  2141. ant_div_ctl1 = ah->eep_ops->get_eeprom(ah, EEP_ANT_DIV_CTL1);
  2142. if ((ant_div_ctl1 >> 0x6) == 0x3) {
  2143. pCap->hw_caps |= ATH9K_HW_CAP_ANT_DIV_COMB;
  2144. ath_info(common, "Enable LNA combining\n");
  2145. }
  2146. }
  2147. if (ath9k_hw_dfs_tested(ah))
  2148. pCap->hw_caps |= ATH9K_HW_CAP_DFS;
  2149. tx_chainmask = pCap->tx_chainmask;
  2150. rx_chainmask = pCap->rx_chainmask;
  2151. while (tx_chainmask || rx_chainmask) {
  2152. if (tx_chainmask & BIT(0))
  2153. pCap->max_txchains++;
  2154. if (rx_chainmask & BIT(0))
  2155. pCap->max_rxchains++;
  2156. tx_chainmask >>= 1;
  2157. rx_chainmask >>= 1;
  2158. }
  2159. if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
  2160. if (!(ah->ent_mode & AR_ENT_OTP_49GHZ_DISABLE))
  2161. pCap->hw_caps |= ATH9K_HW_CAP_MCI;
  2162. if (AR_SREV_9462_20_OR_LATER(ah))
  2163. pCap->hw_caps |= ATH9K_HW_CAP_RTT;
  2164. }
  2165. if (AR_SREV_9300_20_OR_LATER(ah) &&
  2166. ah->eep_ops->get_eeprom(ah, EEP_PAPRD))
  2167. pCap->hw_caps |= ATH9K_HW_CAP_PAPRD;
  2168. #ifdef CONFIG_ATH9K_WOW
  2169. if (AR_SREV_9462_20_OR_LATER(ah) || AR_SREV_9565_11_OR_LATER(ah))
  2170. ah->wow.max_patterns = MAX_NUM_PATTERN;
  2171. else
  2172. ah->wow.max_patterns = MAX_NUM_PATTERN_LEGACY;
  2173. #endif
  2174. return 0;
  2175. }
  2176. /****************************/
  2177. /* GPIO / RFKILL / Antennae */
  2178. /****************************/
  2179. static void ath9k_hw_gpio_cfg_output_mux(struct ath_hw *ah, u32 gpio, u32 type)
  2180. {
  2181. int addr;
  2182. u32 gpio_shift, tmp;
  2183. if (gpio > 11)
  2184. addr = AR_GPIO_OUTPUT_MUX3;
  2185. else if (gpio > 5)
  2186. addr = AR_GPIO_OUTPUT_MUX2;
  2187. else
  2188. addr = AR_GPIO_OUTPUT_MUX1;
  2189. gpio_shift = (gpio % 6) * 5;
  2190. if (AR_SREV_9280_20_OR_LATER(ah) ||
  2191. (addr != AR_GPIO_OUTPUT_MUX1)) {
  2192. REG_RMW(ah, addr, (type << gpio_shift),
  2193. (0x1f << gpio_shift));
  2194. } else {
  2195. tmp = REG_READ(ah, addr);
  2196. tmp = ((tmp & 0x1F0) << 1) | (tmp & ~0x1F0);
  2197. tmp &= ~(0x1f << gpio_shift);
  2198. tmp |= (type << gpio_shift);
  2199. REG_WRITE(ah, addr, tmp);
  2200. }
  2201. }
  2202. /* BSP should set the corresponding MUX register correctly.
  2203. */
  2204. static void ath9k_hw_gpio_cfg_soc(struct ath_hw *ah, u32 gpio, bool out,
  2205. const char *label)
  2206. {
  2207. if (ah->caps.gpio_requested & BIT(gpio))
  2208. return;
  2209. /* may be requested by BSP, free anyway */
  2210. gpio_free(gpio);
  2211. if (gpio_request_one(gpio, out ? GPIOF_OUT_INIT_LOW : GPIOF_IN, label))
  2212. return;
  2213. ah->caps.gpio_requested |= BIT(gpio);
  2214. }
  2215. static void ath9k_hw_gpio_cfg_wmac(struct ath_hw *ah, u32 gpio, bool out,
  2216. u32 ah_signal_type)
  2217. {
  2218. u32 gpio_set, gpio_shift = gpio;
  2219. if (AR_DEVID_7010(ah)) {
  2220. gpio_set = out ?
  2221. AR7010_GPIO_OE_AS_OUTPUT : AR7010_GPIO_OE_AS_INPUT;
  2222. REG_RMW(ah, AR7010_GPIO_OE, gpio_set << gpio_shift,
  2223. AR7010_GPIO_OE_MASK << gpio_shift);
  2224. } else if (AR_SREV_SOC(ah)) {
  2225. gpio_set = out ? 1 : 0;
  2226. REG_RMW(ah, AR_GPIO_OE_OUT, gpio_set << gpio_shift,
  2227. gpio_set << gpio_shift);
  2228. } else {
  2229. gpio_shift = gpio << 1;
  2230. gpio_set = out ?
  2231. AR_GPIO_OE_OUT_DRV_ALL : AR_GPIO_OE_OUT_DRV_NO;
  2232. REG_RMW(ah, AR_GPIO_OE_OUT, gpio_set << gpio_shift,
  2233. AR_GPIO_OE_OUT_DRV << gpio_shift);
  2234. if (out)
  2235. ath9k_hw_gpio_cfg_output_mux(ah, gpio, ah_signal_type);
  2236. }
  2237. }
  2238. static void ath9k_hw_gpio_request(struct ath_hw *ah, u32 gpio, bool out,
  2239. const char *label, u32 ah_signal_type)
  2240. {
  2241. WARN_ON(gpio >= ah->caps.num_gpio_pins);
  2242. if (BIT(gpio) & ah->caps.gpio_mask)
  2243. ath9k_hw_gpio_cfg_wmac(ah, gpio, out, ah_signal_type);
  2244. else if (AR_SREV_SOC(ah))
  2245. ath9k_hw_gpio_cfg_soc(ah, gpio, out, label);
  2246. else
  2247. WARN_ON(1);
  2248. }
  2249. void ath9k_hw_gpio_request_in(struct ath_hw *ah, u32 gpio, const char *label)
  2250. {
  2251. ath9k_hw_gpio_request(ah, gpio, false, label, 0);
  2252. }
  2253. EXPORT_SYMBOL(ath9k_hw_gpio_request_in);
  2254. void ath9k_hw_gpio_request_out(struct ath_hw *ah, u32 gpio, const char *label,
  2255. u32 ah_signal_type)
  2256. {
  2257. ath9k_hw_gpio_request(ah, gpio, true, label, ah_signal_type);
  2258. }
  2259. EXPORT_SYMBOL(ath9k_hw_gpio_request_out);
  2260. void ath9k_hw_gpio_free(struct ath_hw *ah, u32 gpio)
  2261. {
  2262. if (!AR_SREV_SOC(ah))
  2263. return;
  2264. WARN_ON(gpio >= ah->caps.num_gpio_pins);
  2265. if (ah->caps.gpio_requested & BIT(gpio)) {
  2266. gpio_free(gpio);
  2267. ah->caps.gpio_requested &= ~BIT(gpio);
  2268. }
  2269. }
  2270. EXPORT_SYMBOL(ath9k_hw_gpio_free);
  2271. u32 ath9k_hw_gpio_get(struct ath_hw *ah, u32 gpio)
  2272. {
  2273. u32 val = 0xffffffff;
  2274. #define MS_REG_READ(x, y) \
  2275. (MS(REG_READ(ah, AR_GPIO_IN_OUT), x##_GPIO_IN_VAL) & BIT(y))
  2276. WARN_ON(gpio >= ah->caps.num_gpio_pins);
  2277. if (BIT(gpio) & ah->caps.gpio_mask) {
  2278. if (AR_SREV_9271(ah))
  2279. val = MS_REG_READ(AR9271, gpio);
  2280. else if (AR_SREV_9287(ah))
  2281. val = MS_REG_READ(AR9287, gpio);
  2282. else if (AR_SREV_9285(ah))
  2283. val = MS_REG_READ(AR9285, gpio);
  2284. else if (AR_SREV_9280(ah))
  2285. val = MS_REG_READ(AR928X, gpio);
  2286. else if (AR_DEVID_7010(ah))
  2287. val = REG_READ(ah, AR7010_GPIO_IN) & BIT(gpio);
  2288. else if (AR_SREV_9300_20_OR_LATER(ah))
  2289. val = REG_READ(ah, AR_GPIO_IN) & BIT(gpio);
  2290. else
  2291. val = MS_REG_READ(AR, gpio);
  2292. } else if (BIT(gpio) & ah->caps.gpio_requested) {
  2293. val = gpio_get_value(gpio) & BIT(gpio);
  2294. } else {
  2295. WARN_ON(1);
  2296. }
  2297. return !!val;
  2298. }
  2299. EXPORT_SYMBOL(ath9k_hw_gpio_get);
  2300. void ath9k_hw_set_gpio(struct ath_hw *ah, u32 gpio, u32 val)
  2301. {
  2302. WARN_ON(gpio >= ah->caps.num_gpio_pins);
  2303. if (AR_DEVID_7010(ah) || AR_SREV_9271(ah))
  2304. val = !val;
  2305. else
  2306. val = !!val;
  2307. if (BIT(gpio) & ah->caps.gpio_mask) {
  2308. u32 out_addr = AR_DEVID_7010(ah) ?
  2309. AR7010_GPIO_OUT : AR_GPIO_IN_OUT;
  2310. REG_RMW(ah, out_addr, val << gpio, BIT(gpio));
  2311. } else if (BIT(gpio) & ah->caps.gpio_requested) {
  2312. gpio_set_value(gpio, val);
  2313. } else {
  2314. WARN_ON(1);
  2315. }
  2316. }
  2317. EXPORT_SYMBOL(ath9k_hw_set_gpio);
  2318. void ath9k_hw_setantenna(struct ath_hw *ah, u32 antenna)
  2319. {
  2320. REG_WRITE(ah, AR_DEF_ANTENNA, (antenna & 0x7));
  2321. }
  2322. EXPORT_SYMBOL(ath9k_hw_setantenna);
  2323. /*********************/
  2324. /* General Operation */
  2325. /*********************/
  2326. u32 ath9k_hw_getrxfilter(struct ath_hw *ah)
  2327. {
  2328. u32 bits = REG_READ(ah, AR_RX_FILTER);
  2329. u32 phybits = REG_READ(ah, AR_PHY_ERR);
  2330. if (phybits & AR_PHY_ERR_RADAR)
  2331. bits |= ATH9K_RX_FILTER_PHYRADAR;
  2332. if (phybits & (AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING))
  2333. bits |= ATH9K_RX_FILTER_PHYERR;
  2334. return bits;
  2335. }
  2336. EXPORT_SYMBOL(ath9k_hw_getrxfilter);
  2337. void ath9k_hw_setrxfilter(struct ath_hw *ah, u32 bits)
  2338. {
  2339. u32 phybits;
  2340. ENABLE_REGWRITE_BUFFER(ah);
  2341. REG_WRITE(ah, AR_RX_FILTER, bits);
  2342. phybits = 0;
  2343. if (bits & ATH9K_RX_FILTER_PHYRADAR)
  2344. phybits |= AR_PHY_ERR_RADAR;
  2345. if (bits & ATH9K_RX_FILTER_PHYERR)
  2346. phybits |= AR_PHY_ERR_OFDM_TIMING | AR_PHY_ERR_CCK_TIMING;
  2347. REG_WRITE(ah, AR_PHY_ERR, phybits);
  2348. if (phybits)
  2349. REG_SET_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
  2350. else
  2351. REG_CLR_BIT(ah, AR_RXCFG, AR_RXCFG_ZLFDMA);
  2352. REGWRITE_BUFFER_FLUSH(ah);
  2353. }
  2354. EXPORT_SYMBOL(ath9k_hw_setrxfilter);
  2355. bool ath9k_hw_phy_disable(struct ath_hw *ah)
  2356. {
  2357. if (ath9k_hw_mci_is_enabled(ah))
  2358. ar9003_mci_bt_gain_ctrl(ah);
  2359. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_WARM))
  2360. return false;
  2361. ath9k_hw_init_pll(ah, NULL);
  2362. ah->htc_reset_init = true;
  2363. return true;
  2364. }
  2365. EXPORT_SYMBOL(ath9k_hw_phy_disable);
  2366. bool ath9k_hw_disable(struct ath_hw *ah)
  2367. {
  2368. if (!ath9k_hw_setpower(ah, ATH9K_PM_AWAKE))
  2369. return false;
  2370. if (!ath9k_hw_set_reset_reg(ah, ATH9K_RESET_COLD))
  2371. return false;
  2372. ath9k_hw_init_pll(ah, NULL);
  2373. return true;
  2374. }
  2375. EXPORT_SYMBOL(ath9k_hw_disable);
  2376. static int get_antenna_gain(struct ath_hw *ah, struct ath9k_channel *chan)
  2377. {
  2378. enum eeprom_param gain_param;
  2379. if (IS_CHAN_2GHZ(chan))
  2380. gain_param = EEP_ANTENNA_GAIN_2G;
  2381. else
  2382. gain_param = EEP_ANTENNA_GAIN_5G;
  2383. return ah->eep_ops->get_eeprom(ah, gain_param);
  2384. }
  2385. void ath9k_hw_apply_txpower(struct ath_hw *ah, struct ath9k_channel *chan,
  2386. bool test)
  2387. {
  2388. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  2389. struct ieee80211_channel *channel;
  2390. int chan_pwr, new_pwr;
  2391. if (!chan)
  2392. return;
  2393. channel = chan->chan;
  2394. chan_pwr = min_t(int, channel->max_power * 2, MAX_RATE_POWER);
  2395. new_pwr = min_t(int, chan_pwr, reg->power_limit);
  2396. ah->eep_ops->set_txpower(ah, chan,
  2397. ath9k_regd_get_ctl(reg, chan),
  2398. get_antenna_gain(ah, chan), new_pwr, test);
  2399. }
  2400. void ath9k_hw_set_txpowerlimit(struct ath_hw *ah, u32 limit, bool test)
  2401. {
  2402. struct ath_regulatory *reg = ath9k_hw_regulatory(ah);
  2403. struct ath9k_channel *chan = ah->curchan;
  2404. struct ieee80211_channel *channel = chan->chan;
  2405. reg->power_limit = min_t(u32, limit, MAX_RATE_POWER);
  2406. if (test)
  2407. channel->max_power = MAX_RATE_POWER / 2;
  2408. ath9k_hw_apply_txpower(ah, chan, test);
  2409. if (test)
  2410. channel->max_power = DIV_ROUND_UP(reg->max_power_level, 2);
  2411. }
  2412. EXPORT_SYMBOL(ath9k_hw_set_txpowerlimit);
  2413. void ath9k_hw_setopmode(struct ath_hw *ah)
  2414. {
  2415. ath9k_hw_set_operating_mode(ah, ah->opmode);
  2416. }
  2417. EXPORT_SYMBOL(ath9k_hw_setopmode);
  2418. void ath9k_hw_setmcastfilter(struct ath_hw *ah, u32 filter0, u32 filter1)
  2419. {
  2420. REG_WRITE(ah, AR_MCAST_FIL0, filter0);
  2421. REG_WRITE(ah, AR_MCAST_FIL1, filter1);
  2422. }
  2423. EXPORT_SYMBOL(ath9k_hw_setmcastfilter);
  2424. void ath9k_hw_write_associd(struct ath_hw *ah)
  2425. {
  2426. struct ath_common *common = ath9k_hw_common(ah);
  2427. REG_WRITE(ah, AR_BSS_ID0, get_unaligned_le32(common->curbssid));
  2428. REG_WRITE(ah, AR_BSS_ID1, get_unaligned_le16(common->curbssid + 4) |
  2429. ((common->curaid & 0x3fff) << AR_BSS_ID1_AID_S));
  2430. }
  2431. EXPORT_SYMBOL(ath9k_hw_write_associd);
  2432. #define ATH9K_MAX_TSF_READ 10
  2433. u64 ath9k_hw_gettsf64(struct ath_hw *ah)
  2434. {
  2435. u32 tsf_lower, tsf_upper1, tsf_upper2;
  2436. int i;
  2437. tsf_upper1 = REG_READ(ah, AR_TSF_U32);
  2438. for (i = 0; i < ATH9K_MAX_TSF_READ; i++) {
  2439. tsf_lower = REG_READ(ah, AR_TSF_L32);
  2440. tsf_upper2 = REG_READ(ah, AR_TSF_U32);
  2441. if (tsf_upper2 == tsf_upper1)
  2442. break;
  2443. tsf_upper1 = tsf_upper2;
  2444. }
  2445. WARN_ON( i == ATH9K_MAX_TSF_READ );
  2446. return (((u64)tsf_upper1 << 32) | tsf_lower);
  2447. }
  2448. EXPORT_SYMBOL(ath9k_hw_gettsf64);
  2449. void ath9k_hw_settsf64(struct ath_hw *ah, u64 tsf64)
  2450. {
  2451. REG_WRITE(ah, AR_TSF_L32, tsf64 & 0xffffffff);
  2452. REG_WRITE(ah, AR_TSF_U32, (tsf64 >> 32) & 0xffffffff);
  2453. }
  2454. EXPORT_SYMBOL(ath9k_hw_settsf64);
  2455. void ath9k_hw_reset_tsf(struct ath_hw *ah)
  2456. {
  2457. if (!ath9k_hw_wait(ah, AR_SLP32_MODE, AR_SLP32_TSF_WRITE_STATUS, 0,
  2458. AH_TSF_WRITE_TIMEOUT))
  2459. ath_dbg(ath9k_hw_common(ah), RESET,
  2460. "AR_SLP32_TSF_WRITE_STATUS limit exceeded\n");
  2461. REG_WRITE(ah, AR_RESET_TSF, AR_RESET_TSF_ONCE);
  2462. }
  2463. EXPORT_SYMBOL(ath9k_hw_reset_tsf);
  2464. void ath9k_hw_set_tsfadjust(struct ath_hw *ah, bool set)
  2465. {
  2466. if (set)
  2467. ah->misc_mode |= AR_PCU_TX_ADD_TSF;
  2468. else
  2469. ah->misc_mode &= ~AR_PCU_TX_ADD_TSF;
  2470. }
  2471. EXPORT_SYMBOL(ath9k_hw_set_tsfadjust);
  2472. void ath9k_hw_set11nmac2040(struct ath_hw *ah, struct ath9k_channel *chan)
  2473. {
  2474. u32 macmode;
  2475. if (IS_CHAN_HT40(chan) && !ah->config.cwm_ignore_extcca)
  2476. macmode = AR_2040_JOINED_RX_CLEAR;
  2477. else
  2478. macmode = 0;
  2479. REG_WRITE(ah, AR_2040_MODE, macmode);
  2480. }
  2481. /* HW Generic timers configuration */
  2482. static const struct ath_gen_timer_configuration gen_tmr_configuration[] =
  2483. {
  2484. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2485. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2486. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2487. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2488. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2489. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2490. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2491. {AR_NEXT_NDP_TIMER, AR_NDP_PERIOD, AR_TIMER_MODE, 0x0080},
  2492. {AR_NEXT_NDP2_TIMER, AR_NDP2_PERIOD, AR_NDP2_TIMER_MODE, 0x0001},
  2493. {AR_NEXT_NDP2_TIMER + 1*4, AR_NDP2_PERIOD + 1*4,
  2494. AR_NDP2_TIMER_MODE, 0x0002},
  2495. {AR_NEXT_NDP2_TIMER + 2*4, AR_NDP2_PERIOD + 2*4,
  2496. AR_NDP2_TIMER_MODE, 0x0004},
  2497. {AR_NEXT_NDP2_TIMER + 3*4, AR_NDP2_PERIOD + 3*4,
  2498. AR_NDP2_TIMER_MODE, 0x0008},
  2499. {AR_NEXT_NDP2_TIMER + 4*4, AR_NDP2_PERIOD + 4*4,
  2500. AR_NDP2_TIMER_MODE, 0x0010},
  2501. {AR_NEXT_NDP2_TIMER + 5*4, AR_NDP2_PERIOD + 5*4,
  2502. AR_NDP2_TIMER_MODE, 0x0020},
  2503. {AR_NEXT_NDP2_TIMER + 6*4, AR_NDP2_PERIOD + 6*4,
  2504. AR_NDP2_TIMER_MODE, 0x0040},
  2505. {AR_NEXT_NDP2_TIMER + 7*4, AR_NDP2_PERIOD + 7*4,
  2506. AR_NDP2_TIMER_MODE, 0x0080}
  2507. };
  2508. /* HW generic timer primitives */
  2509. u32 ath9k_hw_gettsf32(struct ath_hw *ah)
  2510. {
  2511. return REG_READ(ah, AR_TSF_L32);
  2512. }
  2513. EXPORT_SYMBOL(ath9k_hw_gettsf32);
  2514. void ath9k_hw_gen_timer_start_tsf2(struct ath_hw *ah)
  2515. {
  2516. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2517. if (timer_table->tsf2_enabled) {
  2518. REG_SET_BIT(ah, AR_DIRECT_CONNECT, AR_DC_AP_STA_EN);
  2519. REG_SET_BIT(ah, AR_RESET_TSF, AR_RESET_TSF2_ONCE);
  2520. }
  2521. }
  2522. struct ath_gen_timer *ath_gen_timer_alloc(struct ath_hw *ah,
  2523. void (*trigger)(void *),
  2524. void (*overflow)(void *),
  2525. void *arg,
  2526. u8 timer_index)
  2527. {
  2528. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2529. struct ath_gen_timer *timer;
  2530. if ((timer_index < AR_FIRST_NDP_TIMER) ||
  2531. (timer_index >= ATH_MAX_GEN_TIMER))
  2532. return NULL;
  2533. if ((timer_index > AR_FIRST_NDP_TIMER) &&
  2534. !AR_SREV_9300_20_OR_LATER(ah))
  2535. return NULL;
  2536. timer = kzalloc(sizeof(struct ath_gen_timer), GFP_KERNEL);
  2537. if (timer == NULL)
  2538. return NULL;
  2539. /* allocate a hardware generic timer slot */
  2540. timer_table->timers[timer_index] = timer;
  2541. timer->index = timer_index;
  2542. timer->trigger = trigger;
  2543. timer->overflow = overflow;
  2544. timer->arg = arg;
  2545. if ((timer_index > AR_FIRST_NDP_TIMER) && !timer_table->tsf2_enabled) {
  2546. timer_table->tsf2_enabled = true;
  2547. ath9k_hw_gen_timer_start_tsf2(ah);
  2548. }
  2549. return timer;
  2550. }
  2551. EXPORT_SYMBOL(ath_gen_timer_alloc);
  2552. void ath9k_hw_gen_timer_start(struct ath_hw *ah,
  2553. struct ath_gen_timer *timer,
  2554. u32 timer_next,
  2555. u32 timer_period)
  2556. {
  2557. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2558. u32 mask = 0;
  2559. timer_table->timer_mask |= BIT(timer->index);
  2560. /*
  2561. * Program generic timer registers
  2562. */
  2563. REG_WRITE(ah, gen_tmr_configuration[timer->index].next_addr,
  2564. timer_next);
  2565. REG_WRITE(ah, gen_tmr_configuration[timer->index].period_addr,
  2566. timer_period);
  2567. REG_SET_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2568. gen_tmr_configuration[timer->index].mode_mask);
  2569. if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
  2570. /*
  2571. * Starting from AR9462, each generic timer can select which tsf
  2572. * to use. But we still follow the old rule, 0 - 7 use tsf and
  2573. * 8 - 15 use tsf2.
  2574. */
  2575. if ((timer->index < AR_GEN_TIMER_BANK_1_LEN))
  2576. REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
  2577. (1 << timer->index));
  2578. else
  2579. REG_SET_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
  2580. (1 << timer->index));
  2581. }
  2582. if (timer->trigger)
  2583. mask |= SM(AR_GENTMR_BIT(timer->index),
  2584. AR_IMR_S5_GENTIMER_TRIG);
  2585. if (timer->overflow)
  2586. mask |= SM(AR_GENTMR_BIT(timer->index),
  2587. AR_IMR_S5_GENTIMER_THRESH);
  2588. REG_SET_BIT(ah, AR_IMR_S5, mask);
  2589. if ((ah->imask & ATH9K_INT_GENTIMER) == 0) {
  2590. ah->imask |= ATH9K_INT_GENTIMER;
  2591. ath9k_hw_set_interrupts(ah);
  2592. }
  2593. }
  2594. EXPORT_SYMBOL(ath9k_hw_gen_timer_start);
  2595. void ath9k_hw_gen_timer_stop(struct ath_hw *ah, struct ath_gen_timer *timer)
  2596. {
  2597. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2598. /* Clear generic timer enable bits. */
  2599. REG_CLR_BIT(ah, gen_tmr_configuration[timer->index].mode_addr,
  2600. gen_tmr_configuration[timer->index].mode_mask);
  2601. if (AR_SREV_9462(ah) || AR_SREV_9565(ah)) {
  2602. /*
  2603. * Need to switch back to TSF if it was using TSF2.
  2604. */
  2605. if ((timer->index >= AR_GEN_TIMER_BANK_1_LEN)) {
  2606. REG_CLR_BIT(ah, AR_MAC_PCU_GEN_TIMER_TSF_SEL,
  2607. (1 << timer->index));
  2608. }
  2609. }
  2610. /* Disable both trigger and thresh interrupt masks */
  2611. REG_CLR_BIT(ah, AR_IMR_S5,
  2612. (SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_THRESH) |
  2613. SM(AR_GENTMR_BIT(timer->index), AR_IMR_S5_GENTIMER_TRIG)));
  2614. timer_table->timer_mask &= ~BIT(timer->index);
  2615. if (timer_table->timer_mask == 0) {
  2616. ah->imask &= ~ATH9K_INT_GENTIMER;
  2617. ath9k_hw_set_interrupts(ah);
  2618. }
  2619. }
  2620. EXPORT_SYMBOL(ath9k_hw_gen_timer_stop);
  2621. void ath_gen_timer_free(struct ath_hw *ah, struct ath_gen_timer *timer)
  2622. {
  2623. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2624. /* free the hardware generic timer slot */
  2625. timer_table->timers[timer->index] = NULL;
  2626. kfree(timer);
  2627. }
  2628. EXPORT_SYMBOL(ath_gen_timer_free);
  2629. /*
  2630. * Generic Timer Interrupts handling
  2631. */
  2632. void ath_gen_timer_isr(struct ath_hw *ah)
  2633. {
  2634. struct ath_gen_timer_table *timer_table = &ah->hw_gen_timers;
  2635. struct ath_gen_timer *timer;
  2636. unsigned long trigger_mask, thresh_mask;
  2637. unsigned int index;
  2638. /* get hardware generic timer interrupt status */
  2639. trigger_mask = ah->intr_gen_timer_trigger;
  2640. thresh_mask = ah->intr_gen_timer_thresh;
  2641. trigger_mask &= timer_table->timer_mask;
  2642. thresh_mask &= timer_table->timer_mask;
  2643. for_each_set_bit(index, &thresh_mask, ARRAY_SIZE(timer_table->timers)) {
  2644. timer = timer_table->timers[index];
  2645. if (!timer)
  2646. continue;
  2647. if (!timer->overflow)
  2648. continue;
  2649. trigger_mask &= ~BIT(index);
  2650. timer->overflow(timer->arg);
  2651. }
  2652. for_each_set_bit(index, &trigger_mask, ARRAY_SIZE(timer_table->timers)) {
  2653. timer = timer_table->timers[index];
  2654. if (!timer)
  2655. continue;
  2656. if (!timer->trigger)
  2657. continue;
  2658. timer->trigger(timer->arg);
  2659. }
  2660. }
  2661. EXPORT_SYMBOL(ath_gen_timer_isr);
  2662. /********/
  2663. /* HTC */
  2664. /********/
  2665. static struct {
  2666. u32 version;
  2667. const char * name;
  2668. } ath_mac_bb_names[] = {
  2669. /* Devices with external radios */
  2670. { AR_SREV_VERSION_5416_PCI, "5416" },
  2671. { AR_SREV_VERSION_5416_PCIE, "5418" },
  2672. { AR_SREV_VERSION_9100, "9100" },
  2673. { AR_SREV_VERSION_9160, "9160" },
  2674. /* Single-chip solutions */
  2675. { AR_SREV_VERSION_9280, "9280" },
  2676. { AR_SREV_VERSION_9285, "9285" },
  2677. { AR_SREV_VERSION_9287, "9287" },
  2678. { AR_SREV_VERSION_9271, "9271" },
  2679. { AR_SREV_VERSION_9300, "9300" },
  2680. { AR_SREV_VERSION_9330, "9330" },
  2681. { AR_SREV_VERSION_9340, "9340" },
  2682. { AR_SREV_VERSION_9485, "9485" },
  2683. { AR_SREV_VERSION_9462, "9462" },
  2684. { AR_SREV_VERSION_9550, "9550" },
  2685. { AR_SREV_VERSION_9565, "9565" },
  2686. { AR_SREV_VERSION_9531, "9531" },
  2687. { AR_SREV_VERSION_9561, "9561" },
  2688. };
  2689. /* For devices with external radios */
  2690. static struct {
  2691. u16 version;
  2692. const char * name;
  2693. } ath_rf_names[] = {
  2694. { 0, "5133" },
  2695. { AR_RAD5133_SREV_MAJOR, "5133" },
  2696. { AR_RAD5122_SREV_MAJOR, "5122" },
  2697. { AR_RAD2133_SREV_MAJOR, "2133" },
  2698. { AR_RAD2122_SREV_MAJOR, "2122" }
  2699. };
  2700. /*
  2701. * Return the MAC/BB name. "????" is returned if the MAC/BB is unknown.
  2702. */
  2703. static const char *ath9k_hw_mac_bb_name(u32 mac_bb_version)
  2704. {
  2705. int i;
  2706. for (i=0; i<ARRAY_SIZE(ath_mac_bb_names); i++) {
  2707. if (ath_mac_bb_names[i].version == mac_bb_version) {
  2708. return ath_mac_bb_names[i].name;
  2709. }
  2710. }
  2711. return "????";
  2712. }
  2713. /*
  2714. * Return the RF name. "????" is returned if the RF is unknown.
  2715. * Used for devices with external radios.
  2716. */
  2717. static const char *ath9k_hw_rf_name(u16 rf_version)
  2718. {
  2719. int i;
  2720. for (i=0; i<ARRAY_SIZE(ath_rf_names); i++) {
  2721. if (ath_rf_names[i].version == rf_version) {
  2722. return ath_rf_names[i].name;
  2723. }
  2724. }
  2725. return "????";
  2726. }
  2727. void ath9k_hw_name(struct ath_hw *ah, char *hw_name, size_t len)
  2728. {
  2729. int used;
  2730. /* chipsets >= AR9280 are single-chip */
  2731. if (AR_SREV_9280_20_OR_LATER(ah)) {
  2732. used = scnprintf(hw_name, len,
  2733. "Atheros AR%s Rev:%x",
  2734. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2735. ah->hw_version.macRev);
  2736. }
  2737. else {
  2738. used = scnprintf(hw_name, len,
  2739. "Atheros AR%s MAC/BB Rev:%x AR%s RF Rev:%x",
  2740. ath9k_hw_mac_bb_name(ah->hw_version.macVersion),
  2741. ah->hw_version.macRev,
  2742. ath9k_hw_rf_name((ah->hw_version.analog5GhzRev
  2743. & AR_RADIO_SREV_MAJOR)),
  2744. ah->hw_version.phyRev);
  2745. }
  2746. hw_name[used] = '\0';
  2747. }
  2748. EXPORT_SYMBOL(ath9k_hw_name);