hw.c 84 KB

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