hw.c 85 KB

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